import { toErrorMessage } from '../../../utils/error.js';
import { getRootLogger } from '../../../utils/logger.js';
import { connectorInstanceToTools, createConnectorRouterTool } from './adapter.js';
export class ConnectorToolRouter {
    manager;
    strategy;
    log;
    constructor(config) {
        this.manager = config.manager;
        this.strategy = config.strategy ?? 'per-method';
        this.log = getRootLogger().child({ component: 'ConnectorToolRouter' });
    }
    getTools() {
        if (this.strategy === 'router') {
            const connected = this.manager.listConnectedInstances();
            if (connected.length === 0)
                return [];
            return [createConnectorRouterTool(this.manager)];
        }
        const tools = [];
        for (const instance of this.manager.listConnectedInstances()) {
            try {
                tools.push(...connectorInstanceToTools(instance.id, this.manager));
            }
            catch (err) {
                this.log.error(`Failed to create tools for instance ${instance.id}`, {
                    error: toErrorMessage(err),
                });
            }
        }
        return tools;
    }
    registerTools(toolRegistry) {
        const tools = this.getTools();
        const names = [];
        for (const tool of tools) {
            toolRegistry.register(tool);
            names.push(tool.name);
        }
        this.log.info(`Registered ${names.length} connector tools`, { tools: names });
        return names;
    }
    unregisterTools(toolRegistry, toolNames) {
        for (const name of toolNames) {
            toolRegistry.unregister(name);
        }
        this.log.info(`Unregistered ${toolNames.length} connector tools`);
    }
    refreshTools(toolRegistry, previousNames) {
        this.unregisterTools(toolRegistry, previousNames);
        return this.registerTools(toolRegistry);
    }
}
//# sourceMappingURL=router.js.map