feat:语音对话优化

This commit is contained in:
2026-06-14 19:54:22 +08:00
parent 16105f5ac6
commit c3118dd72a
11 changed files with 156 additions and 52 deletions

View File

@@ -158,7 +158,8 @@ func (m *MiMoService) Recognize(ctx context.Context, audio []byte, opts Options)
}
if len(mResp.Choices) == 0 {
return "", fmt.Errorf("stt: mimo returned empty choices")
// MiMo 返回空结果,视为无法识别(非错误),返回空文本
return "", nil
}
text := strings.TrimSpace(mResp.Choices[0].Message.Content)

View File

@@ -103,9 +103,12 @@ func TestMiMoService_Recognize_EmptyChoices(t *testing.T) {
defer srv.Close()
wav := makeValidWAV([]byte{0x00, 0x00})
_, err := s.Recognize(context.Background(), wav, Options{})
if err == nil {
t.Fatal("expected error for empty choices")
text, err := s.Recognize(context.Background(), wav, Options{})
if err != nil {
t.Fatalf("unexpected error for empty choices: %v", err)
}
if text != "" {
t.Errorf("expected empty string for empty choices, got %q", text)
}
}

View File

@@ -133,24 +133,48 @@ func (p *Pipeline) ProcessQuery(
}
} else {
// 语音模式:执行 STT 语音识别
log.Infow("开始语音识别", "request_id", req.RequestID)
log.Infow("开始语音识别", "request_id", req.RequestID, "audio_bytes", len(audio))
sttResult, err := p.sttService.Recognize(ctx, audio, stt.Options{
Encoding: "pcm_s16le",
SampleRate: 16000,
Language: sess.Config.Language,
})
if err != nil {
log.Errorw("语音识别失败", "error", err)
log.Errorw("语音识别失败", "error", err, "audio_bytes", len(audio))
sender.SendError(models.WsError{
Type: "error",
RequestID: req.RequestID,
Code: "STT_ERROR",
Message: "语音识别失败",
Message: "语音识别失败: " + err.Error(),
})
return err
}
userText = sttResult
// STT 返回空文本:未识别到语音,发送结果后直接返回(不调 LLM
if strings.TrimSpace(userText) == "" {
log.Infow("语音识别结果为空", "request_id", req.RequestID)
userText = "(未识别到语音)"
if err := sender.SendSTTResult(models.WsSTTResult{
Type: "stt_result",
RequestID: req.RequestID,
Text: userText,
IsFinal: true,
}); err != nil {
log.Errorw("发送 STT 结果失败", "error", err)
}
// 发送空的 llm_done 以结束本轮处理
latency := time.Since(startTime).Milliseconds()
_ = sender.SendLLMDone(models.WsLLMDone{
Type: "llm_done",
RequestID: req.RequestID,
FullText: "",
Model: p.model,
LatencyMs: latency,
})
return nil
}
// 发送 STT 结果
if err := sender.SendSTTResult(models.WsSTTResult{
Type: "stt_result",