"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.callWorkflowLLM = callWorkflowLLM;
const llm_1 = require("../../../services/llm");
const defaults_1 = require("../../../modules/defaults");
const logger_1 = require("../../../lib/logger");
/**
 * Call the LLM with messages and optional tool IDs.
 *
 * Tool IDs are resolved from the caller's module-level toolDefCache so
 * that only lightweight string arrays flow through the durable pipe.
 */
async function callWorkflowLLM(messages, toolIds, toolDefCache, logPrefix) {
    let tools;
    if (toolIds?.length) {
        tools = toolIds
            .map((id) => toolDefCache.get(id))
            .filter((t) => !!t);
    }
    const t0 = Date.now();
    const response = await (0, llm_1.callLLM)({
        model: defaults_1.LLM_MODEL_PRIMARY,
        messages,
        temperature: 0,
        ...(tools?.length ? { tools } : {}),
        ...(!tools?.length ? { max_tokens: defaults_1.LLM_MAX_TOKENS_DEFAULT } : {}),
    });
    const usage = response.usage;
    logger_1.loggerRegistry.info(`[${logPrefix}:callLLM] ${Date.now() - t0}ms | in=${usage?.prompt_tokens} out=${usage?.completion_tokens} total=${usage?.total_tokens} | tool_calls=${response.tool_calls?.length || 0}`);
    return response;
}
