// ─── Tool Registry ─────────────────────────────────────────────────
/**
 * MCP-inspired tool registry. The brain queries this to know
 * what tools exist. Tools self-register their schemas.
 */
export class ToolRegistry {
    tools = new Map();
    /** Register a tool */
    register(tool) {
        if (this.tools.has(tool.name)) {
            throw new Error(`Tool "${tool.name}" is already registered.`);
        }
        this.tools.set(tool.name, tool);
    }
    /** Get a tool by name */
    get(name) {
        return this.tools.get(name);
    }
    /** Get all tool definitions (for sending to the LLM) */
    getDefinitions() {
        return Array.from(this.tools.values()).map((t) => t.definition);
    }
    /** Get all tool names */
    getNames() {
        return Array.from(this.tools.keys());
    }
    /** Execute a tool by name */
    async execute(name, params, state) {
        const tool = this.tools.get(name);
        if (!tool) {
            return {
                success: false,
                output: "",
                error: `Unknown tool: "${name}". Available tools: ${this.getNames().join(", ")}`,
            };
        }
        try {
            return await tool.execute(params, state);
        }
        catch (err) {
            return {
                success: false,
                output: "",
                error: `Tool "${name}" threw: ${err instanceof Error ? err.message : String(err)}`,
            };
        }
    }
    /** Get a formatted summary of all tools for the system prompt */
    getToolSummary() {
        const tools = Array.from(this.tools.values());
        return tools
            .map((t) => {
            const params = Object.entries(t.definition.parameters.properties)
                .map(([k, v]) => `    - ${k} (${v.type}): ${v.description}${v.default !== undefined ? ` [default: ${String(v.default)}]` : ""}`)
                .join("\n");
            const required = t.definition.parameters.required?.join(", ") ?? "none";
            return `• ${t.name}: ${t.description}\n  Required params: ${required}\n${params}`;
        })
            .join("\n\n");
    }
}
//# sourceMappingURL=tool-registry.js.map