"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listSessions = listSessions;
exports.clearSession = clearSession;
exports.orchestratorChat = orchestratorChat;
const llm_1 = require("./llm");
const tools_1 = require("./tools");
const loader_1 = require("./prompts/loader");
const MAX_TURNS = 20;
const sessions = new Map();
function getOrCreateSession(sessionId) {
    if (!sessions.has(sessionId)) {
        sessions.set(sessionId, {
            id: sessionId,
            history: [],
            createdAt: new Date().toISOString(),
            lastActiveAt: new Date().toISOString()
        });
    }
    const session = sessions.get(sessionId);
    session.lastActiveAt = new Date().toISOString();
    return session;
}
function listSessions() {
    return Array.from(sessions.values()).map(s => ({
        id: s.id,
        messages: s.history.length,
        createdAt: s.createdAt,
        lastActiveAt: s.lastActiveAt
    }));
}
function clearSession(sessionId) {
    sessions.delete(sessionId);
}
// ---------------------------------------------------------------------------
// Main orchestrator chat — uses GLM-5 (Tier B) by default
// ---------------------------------------------------------------------------
async function orchestratorChat(sessionId, userMessage, ctx, opts) {
    const modelId = process.env.ORCHESTRATOR_MODEL ?? 'B'; // Tier B = GLM-5
    const llm = (0, llm_1.createLLM)(modelId, { temperature: 0.3 });
    const session = getOrCreateSession(sessionId);
    // Seed session from DB history if provided and session is fresh
    if (opts?.preloadedHistory && opts.preloadedHistory.length > 0 && session.history.length === 0) {
        session.history = [...opts.preloadedHistory];
    }
    const oaiTools = (0, llm_1.toOAITools)(tools_1.ALL_TOOLS);
    // Append user message
    session.history.push({ role: 'user', content: userMessage });
    let turn = 0;
    let finalReply = '';
    let finalReasoning = null;
    const toolCallNames = [];
    // Resolve system prompt from template — {{knowledge}} injects project/COO context
    const systemContent = (0, loader_1.resolvePrompt)('orchestrator', {
        knowledge: opts?.knowledgeContext ?? ''
    });
    // Build messages with system prompt prepended; keep last 40 for cost control
    const buildMessages = () => [
        { role: 'system', content: systemContent },
        ...session.history.slice(-40)
    ];
    while (turn < MAX_TURNS) {
        turn++;
        const response = await llm.chat(buildMessages(), oaiTools, 4096);
        // If GLM-5 is still reasoning (content null, finish_reason length) give it more tokens
        if (response.content === null && response.tool_calls.length === 0 && response.finish_reason === 'length') {
            // Retry with more tokens — model hit max_tokens during reasoning
            const retry = await llm.chat(buildMessages(), oaiTools, 8192);
            Object.assign(response, retry);
        }
        // Record reasoning for the final turn (informational, not stored in history)
        if (response.reasoning)
            finalReasoning = response.reasoning;
        // Only push assistant message if it has actual content or tool calls;
        // skip empty turns that result from mid-reasoning token exhaustion.
        const hasContent = response.content !== null && response.content !== '';
        const hasToolCalls = response.tool_calls.length > 0;
        if (hasContent || hasToolCalls) {
            const assistantMsg = {
                role: 'assistant',
                content: response.content,
                tool_calls: hasToolCalls ? response.tool_calls : undefined
            };
            session.history.push(assistantMsg);
        }
        // No tool calls — we have the final answer
        if (!hasToolCalls) {
            finalReply = response.content ?? '';
            break;
        }
        // Execute each tool call and collect results
        for (const tc of response.tool_calls) {
            const fnName = tc.function.name;
            let fnArgs = {};
            try {
                fnArgs = JSON.parse(tc.function.arguments || '{}');
            }
            catch { /* bad JSON */ }
            toolCallNames.push(fnName);
            let result;
            try {
                result = await (0, tools_1.executeTool)(fnName, fnArgs, ctx);
            }
            catch (err) {
                result = { error: err instanceof Error ? err.message : String(err) };
            }
            // Add tool result to history
            session.history.push({
                role: 'tool',
                tool_call_id: tc.id,
                name: fnName,
                content: typeof result === 'string' ? result : JSON.stringify(result)
            });
        }
    }
    if (turn >= MAX_TURNS && !finalReply) {
        finalReply = 'Hit the turn limit. Try a more specific request.';
    }
    return {
        reply: finalReply,
        reasoning: finalReasoning,
        sessionId,
        turns: turn,
        toolCalls: toolCallNames,
        model: llm.modelId,
        history: session.history
            .filter(m => m.role !== 'assistant' || m.content || m.tool_calls?.length)
            .slice(-40),
        memoryUpdates: ctx.memoryUpdates
    };
}
