Skip to content

Commit

Permalink
feat: add common version mange
Browse files Browse the repository at this point in the history
  • Loading branch information
cubxxw committed Dec 25, 2024
1 parent 9a181af commit c540c5a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
20 changes: 14 additions & 6 deletions internal/server/message/binary_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package message
import (
"bytes"
"fmt"
"strings"
"sync"

"github.com/gorilla/websocket"
"github.com/telepace/voiceflow/internal/storage"
"github.com/telepace/voiceflow/internal/stt"
"github.com/telepace/voiceflow/internal/tts"
"github.com/telepace/voiceflow/pkg/logger"
)

type BinaryMessageHandler struct {
Expand Down Expand Up @@ -88,12 +90,18 @@ func (h *BinaryMessageHandler) HandleEnd(conn *websocket.Conn, sessionID string)
go func() {
text, err := h.stt.Recognize(audioData, audioURL)
if err != nil {
// 发送错误响应
conn.WriteJSON(map[string]interface{}{
"type": "recognition_error",
"session_id": sessionID,
"error": err.Error(),
})
// 检查是否是最终错误(重试后仍然失败)
if strings.Contains(err.Error(), "使用默认语言重试失败") {
conn.WriteJSON(map[string]interface{}{
"type": "recognition_error",
"session_id": sessionID,
"error": err.Error(),
})
return
}

// 如果是其他错误,记录日志但继续等待重试结果
logger.Warnf("语音识别出现错误(可能正在重试): %v", err)
return
}

Expand Down
10 changes: 6 additions & 4 deletions internal/stt/assemblyai/assemblyai.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *STT) Recognize(audioData []byte, audioURL string) (string, error) {
func (s *STT) transcribeFromURL(audioURL string) (string, error) {
ctx := context.Background()

// 第一次尝试使用语言检测
// 第一次尝试��使用语言检测
params := s.buildParams()
transcript, err := s.client.Transcripts.TranscribeFromURL(ctx, audioURL, params)
if err != nil {
Expand All @@ -60,11 +60,13 @@ func (s *STT) transcribeFromURL(audioURL string) (string, error) {
params = s.buildParamsWithDefaultLanguage()
transcript, err = s.client.Transcripts.TranscribeFromURL(ctx, audioURL, params)
if err != nil {
return "", fmt.Errorf("使用默认语言重试失败: %v", err)
return "", fmt.Errorf("语言置信度低于阈值 %.2f,使用默认语言 %s 重试失败: %v",
s.cfg.AssemblyAI.LanguageConfidenceThreshold,
s.cfg.AssemblyAI.DefaultLanguageCode,
err)
}
} else {
return "", fmt.Errorf("转录请求失败: %v", err)
}
return "", fmt.Errorf("转录请求失败: %v", err)
}

// 使用指数退避策略,轮询转录状态
Expand Down

0 comments on commit c540c5a

Please sign in to comment.