"use strict";
/**
 * 统一的 LLM 服务 - 使用 LangChain 管理多模型
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLLM = getLLM;
exports.callLLM = callLLM;
exports.callLLMStream = callLLMStream;
exports.getAvailableModels = getAvailableModels;
exports.clearModelCache = clearModelCache;
const openai_1 = require("@langchain/openai");
const config_1 = require("../../config");
// 模型缓存
const modelCache = new Map();
/**
 * 获取 ChatOpenAI 实例
 */
function getLLM(modelId) {
    const id = modelId || config_1.config.models.textGeneration.defaultModel;
    if (modelCache.has(id)) {
        return modelCache.get(id);
    }
    const modelConfig = config_1.config.models.getModel(id);
    if (!modelConfig) {
        throw new Error(`模型 ${id} 不存在`);
    }
    if (!modelConfig.apiKey) {
        throw new Error(`模型 ${id} 缺少 API Key`);
    }
    const llm = new openai_1.ChatOpenAI({
        model: id,
        apiKey: modelConfig.apiKey,
        temperature: modelConfig.temperature,
        maxTokens: modelConfig.maxTokens,
        configuration: {
            baseURL: modelConfig.baseUrl,
        },
    });
    modelCache.set(id, llm);
    return llm;
}
/**
 * 统一调用 - 带自动切换
 */
async function callLLM(prompt, modelId) {
    const id = modelId || config_1.config.models.textGeneration.defaultModel;
    try {
        const llm = getLLM(id);
        const response = await llm.invoke(prompt);
        return response.content;
    }
    catch (error) {
        const errorMessage = error?.message || '';
        // 检查是否需要切换模型
        if (config_1.config.models.shouldSwitchModel(errorMessage)) {
            const nextModel = config_1.config.models.getNextModel(id, 'text');
            if (nextModel) {
                console.log(`[LLM] ${id} 失败，自动切换到 ${nextModel}`);
                return callLLM(prompt, nextModel);
            }
        }
        throw error;
    }
}
/**
 * 流式调用
 */
async function* callLLMStream(prompt, modelId) {
    const id = modelId || config_1.config.models.textGeneration.defaultModel;
    try {
        const llm = getLLM(id);
        const stream = await llm.stream(prompt);
        for await (const chunk of stream) {
            yield chunk.content;
        }
    }
    catch (error) {
        const errorMessage = error?.message || '';
        if (config_1.config.models.shouldSwitchModel(errorMessage)) {
            const nextModel = config_1.config.models.getNextModel(id, 'text');
            if (nextModel) {
                console.log(`[LLM] ${id} 失败，自动切换到 ${nextModel}`);
                yield* callLLMStream(prompt, nextModel);
                return;
            }
        }
        throw error;
    }
}
/**
 * 获取可用模型列表
 */
function getAvailableModels() {
    return config_1.config.models.getModelsByType('text');
}
/**
 * 清除模型缓存
 */
function clearModelCache() {
    modelCache.clear();
}
