@@ -25,6 +25,38 @@ interface OpenAIResponse {
2525 choices ?: OpenAIResponseChoice [ ] ;
2626}
2727
28+ export interface ConnectionTestResult {
29+ success : boolean ;
30+ statusCode ?: number ;
31+ statusText ?: string ;
32+ errorType ?: 'network' | 'auth' | 'timeout' | 'server' | 'unknown' ;
33+ message : string ;
34+ }
35+
36+ function getStatusCodeMeaning ( statusCode : number , language : string ) : string {
37+ const meanings : Record < number , { zh : string ; en : string } > = {
38+ 400 : { zh : '请求参数错误' , en : 'Bad Request' } ,
39+ 401 : { zh : 'API密钥无效或已过期' , en : 'Invalid or expired API key' } ,
40+ 403 : { zh : '没有权限访问该资源' , en : 'Forbidden - no permission' } ,
41+ 404 : { zh : 'API端点或模型不存在' , en : 'API endpoint or model not found' } ,
42+ 408 : { zh : '请求超时' , en : 'Request timeout' } ,
43+ 429 : { zh : '请求过于频繁,已达到速率限制' , en : 'Rate limit exceeded' } ,
44+ 500 : { zh : '服务器内部错误' , en : 'Internal server error' } ,
45+ 502 : { zh : '网关错误,服务器暂时不可用' , en : 'Bad Gateway' } ,
46+ 503 : { zh : '服务暂时不可用,请稍后重试' , en : 'Service unavailable' } ,
47+ 504 : { zh : '网关超时' , en : 'Gateway timeout' } ,
48+ } ;
49+ return meanings [ statusCode ] ?. [ language as 'zh' | 'en' ] || ( language === 'zh' ? '未知错误' : 'Unknown error' ) ;
50+ }
51+
52+ function getErrorTypeFromStatus ( statusCode : number ) : ConnectionTestResult [ 'errorType' ] {
53+ if ( statusCode === 401 || statusCode === 403 ) return 'auth' ;
54+ if ( statusCode === 408 || statusCode === 504 ) return 'timeout' ;
55+ if ( statusCode >= 500 ) return 'server' ;
56+ if ( statusCode >= 400 ) return 'unknown' ;
57+ return 'unknown' ;
58+ }
59+
2860export class AIService {
2961 private config : AIConfig ;
3062 private language : string ;
@@ -442,14 +474,23 @@ Focus on practicality and accurate categorization to help users quickly understa
442474 }
443475 }
444476
445- async testConnection ( ) : Promise < boolean > {
477+ async testConnection ( ) : Promise < ConnectionTestResult > {
478+ const apiType = this . getApiType ( ) ;
479+ const timeoutMs = apiType === 'openai-responses' || this . config . reasoningEffort ? 30000 : 10000 ;
480+
446481 try {
447482 const base = new URL ( this . config . baseUrl ) ;
448- if ( base . protocol !== 'http:' && base . protocol !== 'https:' ) return false ;
483+ if ( base . protocol !== 'http:' && base . protocol !== 'https:' ) {
484+ return {
485+ success : false ,
486+ errorType : 'unknown' ,
487+ message : this . language === 'zh'
488+ ? '无效的协议,请使用 http:// 或 https://'
489+ : 'Invalid protocol, please use http:// or https://' ,
490+ } ;
491+ }
449492
450493 const controller = new AbortController ( ) ;
451- const apiType = this . getApiType ( ) ;
452- const timeoutMs = apiType === 'openai-responses' || this . config . reasoningEffort ? 30000 : 10000 ;
453494 const timeoutId = setTimeout ( ( ) => controller . abort ( ) , timeoutMs ) ;
454495 try {
455496 const content = await this . requestText ( {
@@ -459,12 +500,92 @@ Focus on practicality and accurate categorization to help users quickly understa
459500 maxTokens : 50 ,
460501 signal : controller . signal ,
461502 } ) ;
462- return ! ! content ;
503+ if ( content ) {
504+ return {
505+ success : true ,
506+ message : this . language === 'zh' ? '连接成功' : 'Connection successful' ,
507+ } ;
508+ }
509+ return {
510+ success : false ,
511+ errorType : 'unknown' ,
512+ message : this . language === 'zh' ? '未收到响应内容' : 'No content received' ,
513+ } ;
463514 } finally {
464515 clearTimeout ( timeoutId ) ;
465516 }
466- } catch {
467- return false ;
517+ } catch ( error ) {
518+ const err = error as Error ;
519+ const errorMessage = err . message || '' ;
520+
521+ // 解析状态码
522+ const statusMatch = errorMessage . match ( / ( \d { 3 } ) / ) ;
523+ const statusCode = statusMatch ? parseInt ( statusMatch [ 1 ] , 10 ) : undefined ;
524+
525+ // 处理超时错误
526+ if ( errorMessage . includes ( 'timeout' ) || errorMessage . includes ( 'abort' ) || err . name === 'AbortError' ) {
527+ return {
528+ success : false ,
529+ errorType : 'timeout' ,
530+ message : this . language === 'zh'
531+ ? `连接超时(${ timeoutMs / 1000 } 秒)。请检查:1. 网络连接是否正常 2. API端点是否正确 3. 服务器是否响应缓慢`
532+ : `Connection timeout (${ timeoutMs / 1000 } s). Please check: 1. Network connection 2. API endpoint 3. Server response time` ,
533+ } ;
534+ }
535+
536+ // 处理网络错误
537+ if ( errorMessage . includes ( 'fetch' ) || errorMessage . includes ( 'network' ) || errorMessage . includes ( 'Failed to fetch' ) ) {
538+ return {
539+ success : false ,
540+ errorType : 'network' ,
541+ message : this . language === 'zh'
542+ ? '网络连接失败。请检查:1. 网络连接是否正常 2. API端点地址是否正确 3. 防火墙或代理设置'
543+ : 'Network connection failed. Please check: 1. Network connection 2. API endpoint 3. Firewall or proxy settings' ,
544+ } ;
545+ }
546+
547+ // 如果有状态码,提供详细的错误信息
548+ if ( statusCode ) {
549+ const meaning = getStatusCodeMeaning ( statusCode , this . language ) ;
550+ const errorType = getErrorTypeFromStatus ( statusCode ) ?? 'unknown' ;
551+ const suggestions : Record < string , { zh : string ; en : string } > = {
552+ auth : {
553+ zh : '请检查 API 密钥是否正确,或密钥是否已过期' ,
554+ en : 'Please check if the API key is correct or expired' ,
555+ } ,
556+ timeout : {
557+ zh : '请求超时,请稍后重试或检查网络连接' ,
558+ en : 'Request timeout, please retry later or check network' ,
559+ } ,
560+ server : {
561+ zh : '服务器端错误,请稍后重试或联系服务提供商' ,
562+ en : 'Server error, please retry later or contact provider' ,
563+ } ,
564+ unknown : {
565+ zh : '请检查 API 端点、模型名称和请求参数是否正确' ,
566+ en : 'Please check API endpoint, model name and request parameters' ,
567+ } ,
568+ } ;
569+
570+ return {
571+ success : false ,
572+ statusCode,
573+ statusText : meaning ,
574+ errorType,
575+ message : this . language === 'zh'
576+ ? `HTTP ${ statusCode } - ${ meaning } \n建议:${ suggestions [ errorType ] . zh } `
577+ : `HTTP ${ statusCode } - ${ meaning } \nSuggestion: ${ suggestions [ errorType ] . en } ` ,
578+ } ;
579+ }
580+
581+ // 默认错误
582+ return {
583+ success : false ,
584+ errorType : 'unknown' ,
585+ message : this . language === 'zh'
586+ ? `连接失败:${ errorMessage || '未知错误' } \n请检查 API 端点、API 密钥和模型名称是否正确`
587+ : `Connection failed: ${ errorMessage || 'Unknown error' } \nPlease check API endpoint, API key and model name` ,
588+ } ;
468589 }
469590 }
470591
0 commit comments