"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getToolsForModel = getToolsForModel;
const file_read_1 = require("../../services/tools/file_read");
const bash_1 = require("../../services/tools/bash");
const file_edit_replace_string_1 = require("../../services/tools/file_edit_replace_string");
// DISABLED: import { createFileEditReplaceLinesTool } from "@/services/tools/file_edit_replace_lines";
const file_edit_insert_1 = require("../../services/tools/file_edit_insert");
const propose_plan_1 = require("../../services/tools/propose_plan");
const todo_1 = require("../../services/tools/todo");
const status_set_1 = require("../../services/tools/status_set");
const wrapWithInitWait_1 = require("../../services/tools/wrapWithInitWait");
const log_1 = require("../../services/log");
/**
 * Get tools available for a specific model with configuration
 *
 * Providers are lazy-loaded to reduce startup time. AI SDK providers are only
 * imported when actually needed for a specific model.
 *
 * @param modelString The model string in format "provider:model-id"
 * @param config Required configuration for tools
 * @param workspaceId Workspace ID for init state tracking (required for runtime tools)
 * @param initStateManager Init state manager for runtime tools to wait for initialization
 * @returns Promise resolving to record of tools available for the model
 */
async function getToolsForModel(modelString, config, workspaceId, initStateManager) {
    const [provider, modelId] = modelString.split(":");
    // Helper to reduce repetition when wrapping runtime tools
    const wrap = (tool) => (0, wrapWithInitWait_1.wrapWithInitWait)(tool, workspaceId, initStateManager);
    // Runtime-dependent tools need to wait for workspace initialization
    // Wrap them to handle init waiting centrally instead of in each tool
    const runtimeTools = {
        file_read: wrap((0, file_read_1.createFileReadTool)(config)),
        file_edit_replace_string: wrap((0, file_edit_replace_string_1.createFileEditReplaceStringTool)(config)),
        // DISABLED: file_edit_replace_lines - causes models (particularly GPT-5-Codex)
        // to leave repository in broken state due to issues with concurrent file modifications
        // and line number miscalculations. Use file_edit_replace_string or file_edit_insert instead.
        // file_edit_replace_lines: wrap(createFileEditReplaceLinesTool(config)),
        file_edit_insert: wrap((0, file_edit_insert_1.createFileEditInsertTool)(config)),
        bash: wrap((0, bash_1.createBashTool)(config)),
    };
    // Non-runtime tools execute immediately (no init wait needed)
    const nonRuntimeTools = {
        propose_plan: (0, propose_plan_1.createProposePlanTool)(config),
        todo_write: (0, todo_1.createTodoWriteTool)(config),
        todo_read: (0, todo_1.createTodoReadTool)(config),
        status_set: (0, status_set_1.createStatusSetTool)(config),
    };
    // Base tools available for all models
    const baseTools = {
        ...runtimeTools,
        ...nonRuntimeTools,
    };
    // Try to add provider-specific web search tools if available
    // Lazy-load providers to avoid loading all AI SDKs at startup
    try {
        switch (provider) {
            case "anthropic": {
                const { anthropic } = await Promise.resolve().then(() => __importStar(require("@ai-sdk/anthropic")));
                return {
                    ...baseTools,
                    web_search: anthropic.tools.webSearch_20250305({ maxUses: 1000 }),
                };
            }
            case "openai": {
                // Only add web search for models that support it
                if (modelId.includes("gpt-5") || modelId.includes("gpt-4")) {
                    const { openai } = await Promise.resolve().then(() => __importStar(require("@ai-sdk/openai")));
                    return {
                        ...baseTools,
                        web_search: openai.tools.webSearch({
                            searchContextSize: "high",
                        }),
                    };
                }
                break;
            }
        }
    }
    catch (error) {
        // If tools aren't available, just return base tools
        log_1.log.error(`No web search tools available for ${provider}:`, error);
    }
    return baseTools;
}
//# sourceMappingURL=tools.js.map