test_ha.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * HA 实测:故意触发失败,看 invokeWithRetry 是否真的链式 fallback
  3. */
  4. import * as dotenv from 'dotenv';
  5. dotenv.config();
  6. import { callLLMWithMessages, AllProvidersFailedError } from '/data/ai/audio/server/dist/services/llm/index';
  7. const messages = [
  8. { role: 'system', content: 'You are a test assistant.' },
  9. { role: 'user', content: 'Reply with: HA_OK' },
  10. ];
  11. async function main() {
  12. const t0 = Date.now();
  13. console.log(`\n[TEST-1] 故意不存在的 modelId (模拟调用失败)`);
  14. console.log('start:', new Date().toISOString());
  15. try {
  16. const result = await callLLMWithMessages(messages, 'non-existent-model-id-xxx', 50);
  17. console.log(`[TEST-1] ❌ 意外成功: ${result.substring(0, 50)}`);
  18. } catch (e: any) {
  19. const elapsed = Date.now() - t0;
  20. console.log(`[TEST-1] ✅ 捕获错误 (${elapsed}ms)`);
  21. console.log(` 错误名: ${e.name}`);
  22. console.log(` 错误名(errorMessage): ${e.message?.substring(0, 200)}`);
  23. if (e.name === 'AllProvidersFailedError') {
  24. console.log(` ✅ HA 系统正确抛出了 AllProvidersFailedError`);
  25. console.log(` 尝试过的供应商: ${e.attempts?.length || 0}`);
  26. for (const a of e.attempts || []) {
  27. console.log(` - ${a.provider}/${a.model}: ${a.error.substring(0, 100)} (${a.attempts}次, ${a.errorType})`);
  28. }
  29. } else {
  30. console.log(` 错误类型: ${typeof e}, name=${e.name}`);
  31. }
  32. }
  33. }
  34. main().then(() => process.exit(0)).catch(e => { console.error('FATAL:', e); process.exit(1); });