/**
 * On-demand connector tools
 *
 * Instead of registering hundreds of individual MCP connector tools with the
 * agent (which bloats every API call), this module provides two lightweight
 * proxy tools:
 *
 *   connector_list  — discover available connectors and their tools
 *   connector_call  — route a call to a specific connector tool
 *
 * The actual MCP tool wrappers are stored in a registry built from
 * ConnectorManager.buildTools() output.
 */
/**
 * Build a registry from ConnectorManager's wrapped tool objects.
 * Parses the namespaced tool names (e.g. "pipedrive__deals_search")
 * into connector → tool[] groupings.
 */
export function buildConnectorRegistry(tools) {
    const connectors = new Map();
    for (const tool of tools) {
        const sep = tool.name.indexOf('__');
        const connector = sep !== -1 ? tool.name.slice(0, sep) : tool.name;
        const localName = sep !== -1 ? tool.name.slice(sep + 2) : tool.name;
        if (!connectors.has(connector))
            connectors.set(connector, []);
        connectors.get(connector).push({
            name: tool.name,
            localName,
            connector,
            description: tool.description ?? '',
            parameters: tool.parameters,
            execute: tool.execute,
        });
    }
    return { connectors };
}
/**
 * Create two proxy tools for on-demand connector access.
 */
export function createConnectorTools(registry) {
    return [
        {
            name: 'connector_list',
            description: 'List available external connectors and their tools. Call with no arguments to see all connectors, or with connector name to see its tools, or with both connector and tool to see a tool\'s parameter schema.',
            parameters: {
                type: 'object',
                properties: {
                    connector: {
                        type: 'string',
                        description: 'Show tools for this connector (e.g. "pipedrive", "linear")',
                    },
                    tool: {
                        type: 'string',
                        description: 'Show the parameter schema for this specific tool',
                    },
                },
            },
            execute: async (_toolCallId, params) => {
                // Specific tool schema
                if (params.connector && params.tool) {
                    const tools = registry.connectors.get(params.connector);
                    if (!tools) {
                        return { content: [{ type: 'text', text: `Connector "${params.connector}" not found. Available: ${[...registry.connectors.keys()].join(', ')}` }] };
                    }
                    const tool = tools.find(t => t.localName === params.tool);
                    if (!tool) {
                        return { content: [{ type: 'text', text: `Tool "${params.tool}" not found in ${params.connector}.` }] };
                    }
                    return { content: [{ type: 'text', text: `${params.connector} / ${params.tool}\n${tool.description}\n\nParameters:\n${JSON.stringify(tool.parameters, null, 2)}` }] };
                }
                // Tools for a specific connector
                if (params.connector) {
                    const tools = registry.connectors.get(params.connector);
                    if (!tools) {
                        return { content: [{ type: 'text', text: `Connector "${params.connector}" not found. Available: ${[...registry.connectors.keys()].join(', ')}` }] };
                    }
                    const list = tools.map(t => `- ${t.localName}: ${t.description}`).join('\n');
                    return { content: [{ type: 'text', text: `${params.connector} (${tools.length} tools):\n${list}` }] };
                }
                // All connectors overview
                const summary = [...registry.connectors.entries()]
                    .map(([name, tools]) => `- ${name} (${tools.length} tools)`)
                    .join('\n');
                return { content: [{ type: 'text', text: `Available connectors:\n${summary}\n\nCall with connector to see available tools.` }] };
            },
        },
        {
            name: 'connector_call',
            description: 'Call a tool on an external service (Pipedrive, Linear, Granola, Asana, Obsidian). Use connector_list first to discover available tools and their parameters.',
            parameters: {
                type: 'object',
                properties: {
                    connector: {
                        type: 'string',
                        description: 'Connector name (e.g. "pipedrive", "linear", "granola")',
                    },
                    tool: {
                        type: 'string',
                        description: 'Tool name within the connector (e.g. "deals_search", "list_issues")',
                    },
                    arguments: {
                        type: 'object',
                        description: 'Arguments to pass to the tool',
                        additionalProperties: true,
                    },
                },
                required: ['connector', 'tool'],
            },
            execute: async (_toolCallId, params) => {
                const tools = registry.connectors.get(params.connector);
                if (!tools) {
                    return {
                        content: [{ type: 'text', text: `Connector "${params.connector}" not found. Available: ${[...registry.connectors.keys()].join(', ')}` }],
                        isError: true,
                    };
                }
                const tool = tools.find(t => t.localName === params.tool);
                if (!tool) {
                    const available = tools.slice(0, 20).map(t => t.localName).join(', ');
                    const suffix = tools.length > 20 ? ` ... and ${tools.length - 20} more` : '';
                    return {
                        content: [{ type: 'text', text: `Tool "${params.tool}" not found in ${params.connector}. Available: ${available}${suffix}` }],
                        isError: true,
                    };
                }
                return tool.execute(_toolCallId, params.arguments || {});
            },
        },
    ];
}
//# sourceMappingURL=connector-tools.js.map