@@ -433,6 +433,15 @@ func (h *ConnectionHandler) handleChatMessage(ctx context.Context, text string)
433433}
434434
435435func (h * ConnectionHandler ) genResponseByLLM (ctx context.Context , messages []providers.Message , round int ) error {
436+ defer func () {
437+ if r := recover (); r != nil {
438+ h .logger .Error (fmt .Sprintf ("genResponseByLLM发生panic: %v" , r ))
439+ errorMsg := "抱歉,处理您的请求时发生了错误"
440+ h .tts_last_text_index = 1 // 重置文本索引
441+ h .SpeakAndPlay (errorMsg , 1 , round )
442+ }
443+ }()
444+
436445 llmStartTime := time .Now ()
437446 h .logger .FormatInfo ("开始生成LLM回复, round:%d 打印message" , round )
438447 for _ , msg := range messages {
@@ -463,6 +472,14 @@ func (h *ConnectionHandler) genResponseByLLM(ctx context.Context, messages []pro
463472 content := response .Content
464473 toolCall := response .ToolCalls
465474
475+ if response .Error != "" {
476+ h .logger .Error (fmt .Sprintf ("LLM响应错误: %s" , response .Error ))
477+ errorMsg := "抱歉,服务暂时不可用,请稍后再试"
478+ h .tts_last_text_index = 1 // 重置文本索引
479+ h .SpeakAndPlay (errorMsg , 1 , round )
480+ return fmt .Errorf ("LLM响应错误: %s" , response .Error )
481+ }
482+
466483 if content != "" {
467484 // 累加content_arguments
468485 contentArguments += content
@@ -486,11 +503,23 @@ func (h *ConnectionHandler) genResponseByLLM(ctx context.Context, messages []pro
486503 }
487504
488505 if content != "" {
506+ if strings .Contains (content , "服务响应异常" ) {
507+ h .logger .Error (fmt .Sprintf ("检测到LLM服务异常: %s" , content ))
508+ errorMsg := "抱歉,服务暂时不可用,请稍后再试"
509+ h .tts_last_text_index = 1 // 重置文本索引
510+ h .SpeakAndPlay (errorMsg , 1 , round )
511+ return fmt .Errorf ("LLM服务异常" )
512+ }
513+
489514 if ! toolCallFlag {
490515 responseMessage = append (responseMessage , content )
491516 }
492517 // 处理分段
493518 fullText := utils .JoinStrings (responseMessage )
519+ if len (fullText ) <= processedChars {
520+ h .logger .Warn (fmt .Sprintf ("文本处理异常: fullText长度=%d, processedChars=%d" , len (fullText ), processedChars ))
521+ continue
522+ }
494523 currentText := fullText [processedChars :]
495524
496525 // 按标点符号分割
@@ -562,12 +591,17 @@ func (h *ConnectionHandler) genResponseByLLM(ctx context.Context, messages []pro
562591 }
563592
564593 // 处理剩余文本
565- remainingText := utils .JoinStrings (responseMessage )[processedChars :]
566- if remainingText != "" {
567- textIndex ++
568- h .logger .Info (fmt .Sprintf ("LLM回复分段[剩余文本]: %s, index: %d, round:%d" , remainingText , textIndex , round ))
569- h .tts_last_text_index = textIndex
570- h .SpeakAndPlay (remainingText , textIndex , round )
594+ fullResponse := utils .JoinStrings (responseMessage )
595+ if len (fullResponse ) > processedChars {
596+ remainingText := fullResponse [processedChars :]
597+ if remainingText != "" {
598+ textIndex ++
599+ h .logger .Info (fmt .Sprintf ("LLM回复分段[剩余文本]: %s, index: %d, round:%d" , remainingText , textIndex , round ))
600+ h .tts_last_text_index = textIndex
601+ h .SpeakAndPlay (remainingText , textIndex , round )
602+ }
603+ } else {
604+ h .logger .Info (fmt .Sprintf ("无剩余文本需要处理: fullResponse长度=%d, processedChars=%d" , len (fullResponse ), processedChars ))
571605 }
572606
573607 // 分析回复并发送相应的情绪
0 commit comments