import { z } from "zod";
import { createGeneralChatAgent, GENERAL_CHAT_SYSTEM_PROMPT } from "./general.js";
import { createResearchChatAgent, RESEARCH_CHAT_SYSTEM_PROMPT } from "./research.js";
import { createTherapistChatAgent, THERAPIST_CHAT_SYSTEM_PROMPT } from "./therapist.js";
import { buildChatAgentSystemPrompt, chatAgentSessionsDir, } from "./shared.js";
import { parseChatAgentId } from "./types.js";
export { CHAT_AGENT_IDS, parseChatAgentId } from "./types.js";
export { chatAgentSessionsDir } from "./shared.js";
export const CHAT_AGENT_LABELS = {
    general: "General Agent",
    therapist: "Therapist Agent",
    research: "Research Agent",
};
const CHAT_AGENT_PROMPTS = {
    general: GENERAL_CHAT_SYSTEM_PROMPT,
    therapist: THERAPIST_CHAT_SYSTEM_PROMPT,
    research: RESEARCH_CHAT_SYSTEM_PROMPT,
};
const delegationParameters = z.object({
    agent: z.enum(["general", "therapist", "research"]).describe("Agent to hand off to"),
});
const chatAgentControllers = new WeakMap();
function createDelegationTool(getController) {
    return {
        name: "delegate_to_agent",
        description: "Hand the entire ongoing conversation to a different active agent. Therapist handles emotional support, reflection, coping, relationships, and wellbeing. Research handles evidence, sources, current information, comparisons, and deep analysis. General handles broad conversation that no longer needs a specialist. This is a persistent handoff, not a one-off subtask.",
        parameters: delegationParameters,
        executionMode: "sequential",
        async execute({ agent }) {
            const controller = getController();
            if (agent === controller.current) {
                return `Already running as ${CHAT_AGENT_LABELS[controller.current]}; continue directly.`;
            }
            await controller.switchTo(agent, true);
            return `Handoff complete. ${CHAT_AGENT_LABELS[agent]} is now the active agent for this conversation. Continue from the full conversation history in that role.`;
        },
    };
}
/** Switch an existing chat session without resetting its conversation history. */
export async function switchChatAgent(session, agentId, notify = true) {
    const controller = chatAgentControllers.get(session);
    if (!controller)
        throw new Error("Session is not a chat agent");
    return controller.switchTo(agentId, notify);
}
export function createChatAgent(agentId, options, delegationEnabled = true) {
    let session;
    const sessionOptions = delegationEnabled
        ? {
            ...options,
            additionalTools: [
                ...(options.additionalTools ?? []),
                createDelegationTool(() => controller),
            ],
        }
        : {
            ...options,
            additionalTools: (options.additionalTools ?? []).filter((tool) => tool.name !== "delegate_to_agent"),
        };
    switch (agentId) {
        case "therapist":
            session = createTherapistChatAgent(sessionOptions);
            break;
        case "research":
            session = createResearchChatAgent(sessionOptions);
            break;
        case "general":
            session = createGeneralChatAgent(sessionOptions);
            break;
    }
    const controller = {
        current: agentId,
        async switchTo(nextAgent, notify) {
            if (nextAgent === controller.current)
                return false;
            controller.current = nextAgent;
            session.setCustomSystemPrompt(buildChatAgentSystemPrompt(nextAgent, CHAT_AGENT_PROMPTS[nextAgent], options.cwd, delegationEnabled), `ggchat:${nextAgent}`);
            if (notify)
                await options.onAgentChange?.(nextAgent);
            return true;
        },
    };
    chatAgentControllers.set(session, controller);
    return session;
}
export function sessionsDirForChatAgent(coderSessionsDir, value) {
    return chatAgentSessionsDir(coderSessionsDir, parseChatAgentId(value));
}
//# sourceMappingURL=index.js.map