"use strict";
/**
 * 模型验证工具 - 批量验证所有模型是否可用
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAllModels = validateAllModels;
exports.validateModelsByType = validateModelsByType;
exports.validateModel = validateModel;
const openai_1 = require("@langchain/openai");
const index_1 = require("./index");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
 * 验证单个模型是否可用
 */
async function validateModel(model) {
    const result = {
        id: model.id,
        name: model.name,
        input: model.input,
        vendor: model.vendorName || model.vendor,
        available: false,
    };
    // 检查 baseUrl 和 apiKey（现在在 vendor 级别，通过 config 注入到 model）
    if (!model.apiKey || !model.baseUrl) {
        result.error = '缺少 apiKey 或 baseUrl';
        return result;
    }
    const startTime = Date.now();
    try {
        // 只有 text 类型用 LangChain 测试
        if (model.input?.includes('text')) {
            const llm = new openai_1.ChatOpenAI({
                model: model.id,
                apiKey: model.apiKey,
                configuration: { baseURL: model.baseUrl },
                temperature: 0.7,
                timeout: 15000,
            });
            await llm.invoke('你好');
        }
        else {
            // TTS/Image/Video 暂时标记为待验证（需要不同的 SDK）
            result.available = false;
            result.responseTime = Date.now() - startTime;
            result.error = '需要手动验证';
            return result;
        }
        result.available = true;
        result.responseTime = Date.now() - startTime;
    }
    catch (error) {
        result.responseTime = Date.now() - startTime;
        const errorMsg = error.response?.data?.message || error.message || '未知错误';
        result.error = errorMsg;
        // 检查是否是认证错误（key 无效）
        if (error.response?.status === 401 || errorMsg.includes('invalid')) {
            result.error = `认证失败: ${errorMsg}`;
        }
        else if (error.response?.status === 403) {
            result.error = `权限不足: ${errorMsg}`;
        }
        else if (error.response?.status === 429) {
            result.error = `限流: ${errorMsg}`;
        }
    }
    return result;
}
/**
 * 批量验证所有模型
 */
async function validateAllModels() {
    const models = index_1.config.models.list.filter((m) => m.enabled !== false);
    const results = [];
    console.log('\n========== 模型验证开始 ==========\n');
    for (const model of models) {
        process.stdout.write(`验证 ${model.id}... `);
        const result = await validateModel(model);
        results.push(result);
        if (result.available) {
            console.log(`✅ 可用 (${result.responseTime}ms)`);
        }
        else {
            console.log(`❌ 不可用 - ${result.error}`);
        }
    }
    const availableCount = results.filter(r => r.available).length;
    const unavailableCount = results.filter(r => !r.available).length;
    console.log('\n========== 验证结果 ==========');
    console.log(`总计: ${models.length} | 可用: ${availableCount} | 不可用: ${unavailableCount}`);
    const report = {
        timestamp: new Date().toISOString(),
        totalModels: models.length,
        availableModels: availableCount,
        unavailableModels: unavailableCount,
        results,
    };
    // 保存报告到文件
    const reportPath = path_1.default.join(process.cwd(), 'model-validation-report.json');
    fs_1.default.writeFileSync(reportPath, JSON.stringify(report, null, 2));
    console.log(`\n详细报告已保存: ${reportPath}`);
    return report;
}
/**
 * 仅验证特定类型的模型
 */
async function validateModelsByType(type) {
    const models = index_1.config.models.getModelsByType(type);
    const results = [];
    console.log(`\n========== 验证 ${type} 模型 ==========\n`);
    for (const model of models) {
        process.stdout.write(`验证 ${model.id}... `);
        const result = await validateModel(model);
        results.push(result);
        if (result.available) {
            console.log(`✅ 可用 (${result.responseTime}ms)`);
        }
        else {
            console.log(`❌ 不可用 - ${result.error}`);
        }
    }
    const report = {
        timestamp: new Date().toISOString(),
        totalModels: models.length,
        availableModels: results.filter(r => r.available).length,
        unavailableModels: results.filter(r => !r.available).length,
        results,
    };
    return report;
}
// 如果直接运行此文件，执行验证
if (require.main === module) {
    const args = process.argv.slice(2);
    const type = args[0];
    (async () => {
        if (type) {
            await validateModelsByType(type);
        }
        else {
            await validateAllModels();
        }
        process.exit(0);
    })();
}
