/**
 * get_tool_info — meta-tool for dynamic tool discovery.
 *
 * Lists available executable tools and returns JSON schema for a specific tool.
 * The agent calls this before execute_tool to understand parameters.
 *
 * Modeled after backend/agent/tools/get_tool_info.py.
 */
import { Type } from '@sinclair/typebox';
import { GET_TOOL_INFO } from '../../tool-names.js';
// ---------------------------------------------------------------------------
// Schema
// ---------------------------------------------------------------------------
const schema = Type.Object({
    tool_name: Type.String({
        description: 'The name of the tool to get parameter info about'
    })
});
// ---------------------------------------------------------------------------
// Description builder
// ---------------------------------------------------------------------------
function buildDescription(registry) {
    const lines = registry.map((entry) => `- ${entry.definition.name}: ${entry.summary}`);
    return [
        'Get detailed parameter information for a specific tool in JSON schema format.',
        'Use this to look up schemas/guidelines, then execute_tool to call them.',
        'If Runtime permissions bypass is disabled, use hidden filesystem tools instead of bash for file read/search/edit operations.',
        '',
        'Available:',
        ...lines,
        '',
        "Returns a JSON schema with the tool's name, description, and parameters object containing:",
        '- properties: Object describing each parameter (type, description, etc.)',
        '- required: Array of required parameter names'
    ].join('\n');
}
// ---------------------------------------------------------------------------
// Schema extraction
// ---------------------------------------------------------------------------
/** Extract a clean JSON schema from a TypeBox TSchema, filtering injected params. */
function extractSchema(parameters) {
    const schema = parameters;
    const properties = (schema.properties ?? {});
    const required = (schema.required ?? []);
    // Filter out any injected/internal parameters
    const internalParams = new Set(['tool_call_id', 'signal', 'ctx']);
    const filteredProperties = {};
    for (const [name, details] of Object.entries(properties)) {
        if (!internalParams.has(name)) {
            filteredProperties[name] = details;
        }
    }
    const filteredRequired = required.filter((name) => !internalParams.has(name));
    return { properties: filteredProperties, required: filteredRequired };
}
// ---------------------------------------------------------------------------
// Factory
// ---------------------------------------------------------------------------
export function createGetToolInfoTool(registry) {
    return {
        name: GET_TOOL_INFO,
        label: 'Get Tool Info',
        isReadOnly: true,
        description: buildDescription(registry),
        parameters: schema,
        async execute(_toolCallId, params) {
            const { tool_name } = params;
            const entry = registry.find((e) => e.definition.name === tool_name);
            if (!entry) {
                const available = registry.map((e) => e.definition.name);
                const text = JSON.stringify({ error: `Tool '${tool_name}' not found`, available_tools: available }, null, 2);
                return { content: [{ type: 'text', text }], details: undefined };
            }
            const { properties, required } = extractSchema(entry.definition.parameters);
            const toolSchema = {
                name: entry.definition.name,
                description: entry.definition.description,
                parameters: { type: 'object', properties, required }
            };
            return {
                content: [{ type: 'text', text: JSON.stringify(toolSchema, null, 2) }],
                details: undefined
            };
        }
    };
}
//# sourceMappingURL=get-tool-info.js.map