"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgentService = void 0;
const config_1 = require("../config");
const ConfigAgent_1 = require("../agents/configurable/ConfigAgent");
class AgentService {
    tools;
    events;
    agents = new Map();
    agentContext = null;
    constructor(tools, events) {
        this.tools = tools;
        this.events = events;
        this.wireUp();
    }
    wireUp() {
        this.tools.addTool({
            type: "function",
            function: {
                name: "agentCall",
                description: `Allows an agent to ask another agent a question. Useful for getting help from agents that are configured for specific goals.
        ${this.getAgentDescriptions()}`,
                parameters: {
                    type: "object",
                    positional: true,
                    properties: {
                        agentName: {
                            type: "string",
                            description: `The name of the agent to call. Available agents: ${this.listAgents()}`,
                        },
                        query: {
                            type: "string",
                            description: `The query to send to the agent`,
                        },
                    },
                    required: ["agentName", "query"],
                },
            },
        });
        this.events.on("agents:register", (data) => {
            console.log(`Agent registered: ${data.name}`);
            const { name, agent } = data;
            this.registerAgentByName(name, agent);
        });
        this.events.on("agents:call", (data) => {
            console.log(`Agent called: ${data.name}`);
            const { name, query, resolve, reject } = data;
            this.callAgent(name, query).then(resolve).catch(reject);
        });
    }
    registerAgent(agent) {
        this.registerAgentByName(agent.name, agent);
    }
    registerAgentByName(name, agent) {
        this.agents.set(name, agent);
    }
    setAgentContext(context) {
        this.agentContext = context;
    }
    getAgentContext() {
        return this.agentContext ?? { Tools: this.tools, Events: this.events };
    }
    getAgent(name) {
        const agent = this.agents.get(name);
        if (!agent) {
            throw new Error(`Agent ${name} not found. Options are: ${this.listAgents()}`);
        }
        return agent;
    }
    listAgents() {
        return Array.from(this.agents.keys());
    }
    getAgentDescriptions() {
        return Object.keys(this.agents).map((key) => {
            const agent = this.getAgent(key);
            return `name: ${agent.name} \n description: ${agent.description}`;
        });
    }
    loadAgentsFromConfig(context) {
        const config = (0, config_1.getConfigSync)();
        const agents = config.agents || [];
        for (const agent of agents) {
            this.registerAgent(new ConfigAgent_1.ConfigAgent(agent, context));
        }
    }
    async callAgent(name, query) {
        const agent = this.agents.get(name);
        if (!agent) {
            return `Agent ${name} not found. Options are: ${this.listAgents()}`;
        }
        return agent.call(query);
    }
}
exports.AgentService = AgentService;
//# sourceMappingURL=AgentService.js.map