/**
 * LLM模型验证脚本
 * 用于验证实际接入的是哪个版本的模型
 */

import { LLMFactory } from '../src/common/llm/adapters/LLMFactory.js';
import { logger } from '../src/common/logging/index.js';

const TEST_PROMPT = "请用一句话简单介绍你自己，包括你的模型名称和版本。";

async function verifyModel(modelType: string, expectedModel: string) {
  console.log(`\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);
  console.log(`🔍 验证模型: ${modelType}`);
  console.log(`━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`);
  
  try {
    const adapter = LLMFactory.getAdapter(modelType as any);
    console.log(`✅ 适配器创建成功`);
    console.log(`   模型名称: ${adapter.modelName}`);
    console.log(`   期望模型: ${expectedModel}`);
    console.log(`   匹配状态: ${adapter.modelName === expectedModel ? '✅ 正确' : '❌ 不匹配'}`);
    
    console.log(`\n🚀 发送测试请求...`);
    const startTime = Date.now();
    
    const response = await adapter.chat([
      { role: 'user', content: TEST_PROMPT }
    ]);
    
    const duration = Date.now() - startTime;
    
    console.log(`\n📊 响应结果:`);
    console.log(`   实际返回模型: ${response.model}`);
    console.log(`   响应时间: ${duration}ms`);
    console.log(`   Token使用:`);
    console.log(`     - 输入: ${response.usage?.promptTokens || 0}`);
    console.log(`     - 输出: ${response.usage?.completionTokens || 0}`);
    console.log(`     - 总计: ${response.usage?.totalTokens || 0}`);
    console.log(`\n💬 模型回复:`);
    console.log(`   "${response.content}"`);
    
    // 验证是否匹配
    if (response.model === expectedModel) {
      console.log(`\n✅ 验证通过！实际调用的就是 ${expectedModel}`);
      return true;
    } else {
      console.log(`\n⚠️  警告！期望 ${expectedModel}，实际返回 ${response.model}`);
      return false;
    }
    
  } catch (error) {
    console.error(`\n❌ 验证失败:`, error);
    return false;
  }
}

async function main() {
  console.log('\n🔬 ASL模块LLM模型验证工具');
  console.log('=' .repeat(60));
  console.log('用途: 验证实际接入的模型版本是否正确\n');
  
  const models = [
    { type: 'deepseek-v3', expected: 'deepseek-chat', description: 'DeepSeek-V3' },
    { type: 'qwen3-72b', expected: 'qwen-max', description: 'Qwen最新最强模型' },
  ];
  
  const results: { model: string; passed: boolean }[] = [];
  
  for (const model of models) {
    const passed = await verifyModel(model.type, model.expected);
    results.push({ model: model.description, passed });
    
    // 避免API限流
    await new Promise(resolve => setTimeout(resolve, 2000));
  }
  
  // 总结
  console.log('\n\n' + '='.repeat(60));
  console.log('📊 验证总结');
  console.log('='.repeat(60));
  
  results.forEach(r => {
    console.log(`${r.passed ? '✅' : '❌'} ${r.model}: ${r.passed ? '通过' : '未通过'}`);
  });
  
  const allPassed = results.every(r => r.passed);
  
  if (allPassed) {
    console.log('\n🎉 所有模型验证通过！');
  } else {
    console.log('\n⚠️  部分模型验证未通过，请检查配置！');
  }
  
  console.log('='.repeat(60) + '\n');
}

main().catch(console.error);


