/**
 * Pure name → `ToolDefinition[]` builder. Knows every per-tool contract and
 * how to thread runtime refs into each `createLlmTool`. Mode resolution lives
 * one layer up in `mode-host-tools.ts`.
 */
import { AgentExecuteCodeMode } from '../../contracts/agent-execute-code.js';
import { ToolLifecycleSource } from '../../core/core-types.js';
// Import contracts directly (not via registry.ts) to avoid a cycle with task/index.ts.
import { createWebsiteContract } from './create-website/index.js';
import { executeBashCommandContract } from './execute-bash-command/index.js';
import { executeCodeContract } from './execute-code/index.js';
import { executeToolContract } from './execute-tool/index.js';
import { createSessionToolInfoState, getToolInfoContract } from './get-tool-info/index.js';
import { modifySkillContract } from './modify-skill/index.js';
import { readSkillContract } from './read-skill/index.js';
import { refreshContextContract } from './refresh-context/index.js';
import { sandboxLlmContract } from './sandbox-llm/index.js';
import { switchModeContract } from './switch-mode/index.js';
import { takeScreenshotContract } from './take-screenshot/index.js';
import { taskContract } from './task/index.js';
import { BASH_COMMAND, CREATE_WEBSITE, EXECUTE_CODE, EXECUTE_TOOL, GET_TOOL_INFO, MODIFY_SKILL, READ_SKILL, REFRESH_CONTEXT, SANDBOX_LLM, SWITCH_MODE, TAKE_SCREENSHOT, TASK, WEB_CRAWL, WEB_SEARCH } from './tool-names.js';
import { webCrawlContract } from './web-crawl/index.js';
import { webSearchContract } from './web-search/index.js';
function withLifecycleSource(tool, lifecycleSource) {
    return { ...tool, lifecycleSource };
}
function hostMediated(tool) {
    return withLifecycleSource(tool, ToolLifecycleSource.HostTool);
}
function agentLoopOwned(tool) {
    return withLifecycleSource(tool, ToolLifecycleSource.AgentLoop);
}
export function buildHostToolList(options) {
    const { pendingHostToolRequests, sessionPolicy, agentMode, executeCodeSurface, streamFn, resolveModel, emitChildAgentEvent, toolNames, skipNames, spreadsheetReadOnly, promptPlatform } = options;
    // Read-only intent collapses to `Ask` for the gate. Otherwise inherit
    // the parent's agent mode so the parent session keeps its current behavior.
    const resolvedExecuteCodeMode = spreadsheetReadOnly === true ? AgentExecuteCodeMode.Ask : agentMode;
    const baseOptions = { pendingHostToolRequests, sessionPolicy };
    const sessionToolInfoState = options.sessionToolInfoState ?? createSessionToolInfoState();
    const disabledNames = new Set(options.disabledToolNames ?? []);
    // Executable-tier contracts dispatched via get_tool_info / execute_tool.
    const allExecutableContracts = {
        web_search: webSearchContract,
        web_crawl: webCrawlContract,
        sandbox_llm: sandboxLlmContract,
        create_website: createWebsiteContract
    };
    // Current policy only disables native web_search for private-gateway sessions,
    // so the lazy executable pair still has targets. If a future policy disables
    // every executable target, omit get_tool_info/execute_tool from the tool list.
    const executableContracts = Object.fromEntries(Object.entries(allExecutableContracts).filter(([name]) => !disabledNames.has(name)));
    // Every tool this runtime can construct. Executable-tier entries stay in the
    // dict even when not first-class so `execute_tool` can still dispatch them.
    const allTools = {
        [EXECUTE_CODE]: hostMediated(executeCodeContract.createLlmTool({
            pendingHostToolRequests,
            sessionPolicy,
            mode: resolvedExecuteCodeMode,
            surface: executeCodeSurface
        })),
        [BASH_COMMAND]: hostMediated(executeBashCommandContract.createLlmTool(baseOptions)),
        [MODIFY_SKILL]: hostMediated(modifySkillContract.createLlmTool(baseOptions)),
        [READ_SKILL]: hostMediated(readSkillContract.createLlmTool(baseOptions)),
        [REFRESH_CONTEXT]: agentLoopOwned(refreshContextContract.createLlmTool({
            ...baseOptions,
            scheduleRefresh: options.scheduleRefresh
        })),
        [TASK]: hostMediated(taskContract.createLlmTool({
            ...baseOptions,
            streamFn,
            resolveModel,
            getCurrentModel: options.getCurrentModel,
            getApiKey: options.getApiKey,
            getSpeed: options.getSpeed,
            preferCurrentModelForSubagents: options.preferCurrentModelForSubagents,
            allowedSubagents: options.allowedSubagents,
            disabledToolNames: options.disabledToolNames,
            agentMode,
            executeCodeSurface,
            emitChildAgentEvent,
            promptPlatform,
            buildChildTools: (childOptions) => buildHostToolList({
                pendingHostToolRequests,
                sessionPolicy: childOptions.sessionPolicy,
                agentMode,
                spreadsheetReadOnly: childOptions.spreadsheetReadOnly,
                executeCodeSurface,
                streamFn,
                resolveModel,
                getCurrentModel: options.getCurrentModel,
                getApiKey: options.getApiKey,
                getSpeed: options.getSpeed,
                preferCurrentModelForSubagents: options.preferCurrentModelForSubagents,
                // Nested subagents propagate their events through the same sink.
                emitChildAgentEvent,
                disabledToolNames: options.disabledToolNames,
                toolNames: childOptions.toolNames,
                allowedSubagents: childOptions.allowedSubagents,
                // Forwarded so nested `task` calls inside the subagent can also
                // resolve their children's prompts.
                promptPlatform,
                scheduleRefresh: options.scheduleRefresh
            })
        })),
        [TAKE_SCREENSHOT]: hostMediated(takeScreenshotContract.createLlmTool(baseOptions)),
        [CREATE_WEBSITE]: hostMediated(createWebsiteContract.createLlmTool(baseOptions)),
        [SANDBOX_LLM]: hostMediated(sandboxLlmContract.createLlmTool(baseOptions)),
        [WEB_CRAWL]: hostMediated(webCrawlContract.createLlmTool(baseOptions)),
        [WEB_SEARCH]: hostMediated(webSearchContract.createLlmTool(baseOptions)),
        [SWITCH_MODE]: hostMediated(switchModeContract.createLlmTool({
            ...baseOptions,
            applyRuntimeHandoff: options.applyRuntimeHandoff,
            getCurrentMode: () => agentMode
        })),
        [GET_TOOL_INFO]: agentLoopOwned(getToolInfoContract.createLlmTool({
            ...baseOptions,
            contractsByName: executableContracts,
            sessionState: sessionToolInfoState
        })),
        [EXECUTE_TOOL]: agentLoopOwned(executeToolContract.createLlmTool({
            ...baseOptions,
            contractsByName: executableContracts,
            sessionState: sessionToolInfoState
        }))
    };
    const skipSet = new Set([...(skipNames ?? []), ...(options.disabledToolNames ?? [])]);
    const selected = [];
    const seen = new Set();
    for (const name of toolNames) {
        if (seen.has(name) || skipSet.has(name))
            continue;
        const tool = allTools[name];
        if (!tool) {
            // Client-provided override may ship unknown names — log and continue.
            console.warn(`[build-tool-list] Ignoring unknown tool name: '${name}'`);
            continue;
        }
        selected.push(tool);
        seen.add(name);
    }
    return selected;
}
//# sourceMappingURL=build-tool-list.js.map