import { createEditFileTool } from "./edit-file.js";
import { createListFilesTool } from "./list-files.js";
import { createReadFileTool } from "./read-file.js";
import { createRunCommandTool } from "./run-command.js";
import { createSearchTool } from "./search.js";
import { createWriteFileTool } from "./write-file.js";
function ensureInputObject(input) {
    if (typeof input !== "object" || input === null || Array.isArray(input)) {
        throw new Error("Tool input must be a JSON object.");
    }
    return input;
}
function toToolSchema(tool) {
    return {
        name: tool.name,
        description: tool.description,
        input_schema: tool.inputSchema,
    };
}
export class ToolRegistry {
    toolsByName = new Map();
    constructor(tools) {
        for (const tool of tools) {
            if (this.toolsByName.has(tool.name)) {
                throw new Error(`Duplicate tool registration for "${tool.name}".`);
            }
            this.toolsByName.set(tool.name, tool);
        }
    }
    static createDefault(config, hooks) {
        const tools = [
            createReadFileTool(config),
            createWriteFileTool(config, hooks ? { afterWrite: hooks.afterWrite } : undefined),
            createEditFileTool(config, hooks ? { afterEdit: hooks.afterEdit } : undefined),
            createSearchTool(config),
            createListFilesTool(config),
            createRunCommandTool(config, hooks ? { afterCommand: hooks.afterCommand } : undefined),
        ];
        return new ToolRegistry(tools);
    }
    getToolSchemas() {
        return [...this.toolsByName.values()].map(toToolSchema);
    }
    getDefinitions() {
        return [...this.toolsByName.values()];
    }
    async execute(name, input) {
        const tool = this.toolsByName.get(name);
        if (!tool) {
            return `Tool error: Unknown tool "${name}".`;
        }
        try {
            const inputObject = ensureInputObject(input);
            return await tool.execute(inputObject);
        }
        catch (error) {
            const message = error instanceof Error ? error.message : "Unknown tool failure";
            return `Tool error (${name}): ${message}`;
        }
    }
}
//# sourceMappingURL=registry.js.map