import { InMemoryWorkspace } from '../../agent-utils/index.js';
import { Rag, ArrayRecombiner } from '../../agent-core/index.js';
import { JavaDeveloperAgent } from '../JavaDeveloper/index.js';
import { VueDeveloperAgent } from '../VueDeveloper/index.js';
import { SynthesizerAgent } from '../Synthesizer/index.js';
const workspace = new InMemoryWorkspace();
const AGENT_COLLECTION_NAME = 'agents-for-prediction';
let isDbInitialized = false;
export const findBestAgent = async (queryOrVector, context) => {
    const allAgents = [JavaDeveloperAgent, VueDeveloperAgent, SynthesizerAgent].map(agentClass => new agentClass(context.cloneEmpty()));
    const agentsWithPrompts = allAgents.map(agent => {
        return {
            expertise: `${agent.config.prompts.expertise}\n${agent.config.functions.map(x => x.name).join('\n')}`,
            persona: agent.config.prompts.initialMessages()[0].content ?? '',
            agent,
        };
    });
    let rag;
    if (!isDbInitialized) {
        rag = await Rag.standard(context, AGENT_COLLECTION_NAME, workspace)
            .selector(x => x.expertise)
            .addItems(agentsWithPrompts)
            .forceAddItemsToCollection();
        isDbInitialized = true;
    }
    else {
        rag = Rag.standard(context, AGENT_COLLECTION_NAME, workspace, agentsWithPrompts).selector(x => x.expertise);
    }
    const agents = await rag.query(queryOrVector).recombine(ArrayRecombiner.standard({
        limit: 1,
    }));
    await context.logger.info(`### Selected agent:\n-> ${agents.map(x => x.agent.config.prompts.name)[0]}`);
    const agentWithPrompt = agents[0];
    return [
        agentWithPrompt.agent,
        agentWithPrompt.agent.config.functions.map(f => f.getDefinition()),
        agentWithPrompt.persona,
        agentsWithPrompts.map(x => x.agent.config.functions).flat(),
    ];
};
