From 03b356682285cee64cdcc418fa3fe28e0851bff4 Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sun, 14 Jun 2026 11:55:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20STT=20=E6=9C=8D=E5=8A=A1=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E9=87=8D=E5=A4=8D=E9=BB=98=E8=AE=A4=E5=80=BC=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20timeout=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Deepgram/MiMo STT 超时从 config 传入 - 去除 model/endpoint 的 fallback 默认值,由 config 层保证 --- backend/internal/ai/stt/deepgram.go | 17 +++++++++-------- backend/internal/ai/stt/mimo.go | 15 ++++++++------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/backend/internal/ai/stt/deepgram.go b/backend/internal/ai/stt/deepgram.go index 54da674..8d19246 100644 --- a/backend/internal/ai/stt/deepgram.go +++ b/backend/internal/ai/stt/deepgram.go @@ -18,21 +18,22 @@ type DeepgramService struct { apiKey string model string endpoint string + timeout time.Duration logger *zap.SugaredLogger } // NewDeepgramService 创建 Deepgram STT 服务。 -func NewDeepgramService(apiKey, model, endpoint string, logger *zap.SugaredLogger) *DeepgramService { - if model == "" { - model = "nova-2" - } - if endpoint == "" { - endpoint = "wss://api.deepgram.com/v1/listen" +// model、endpoint 由 config 层保证非空,timeoutSec 为 0 时默认 5 秒。 +func NewDeepgramService(apiKey, model, endpoint string, timeoutSec int, logger *zap.SugaredLogger) *DeepgramService { + timeout := time.Duration(timeoutSec) * time.Second + if timeout <= 0 { + timeout = 5 * time.Second } return &DeepgramService{ apiKey: apiKey, model: model, endpoint: endpoint, + timeout: timeout, logger: logger, } } @@ -57,8 +58,8 @@ func (d *DeepgramService) Recognize(ctx context.Context, audio []byte, opts Opti // 构建 WebSocket URL,附带查询参数 wsURL := d.buildURL(opts) - // 5 秒总超时 - ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + // 总超时 + ctx, cancel := context.WithTimeout(ctx, d.timeout) defer cancel() // 建立 WebSocket 连接 diff --git a/backend/internal/ai/stt/mimo.go b/backend/internal/ai/stt/mimo.go index 887b285..68e0c0d 100644 --- a/backend/internal/ai/stt/mimo.go +++ b/backend/internal/ai/stt/mimo.go @@ -21,21 +21,22 @@ type MiMoService struct { apiKey string model string endpoint string + timeout time.Duration logger *zap.SugaredLogger } // NewMiMoService 创建 MiMo STT 服务。 -func NewMiMoService(apiKey, model, endpoint string, logger *zap.SugaredLogger) *MiMoService { - if model == "" { - model = "mimo-v2.5-asr" - } - if endpoint == "" { - endpoint = "https://api.xiaomimimo.com/v1" +// model、endpoint 由 config 层保证非空,timeoutSec 为 0 时默认 10 秒。 +func NewMiMoService(apiKey, model, endpoint string, timeoutSec int, logger *zap.SugaredLogger) *MiMoService { + timeout := time.Duration(timeoutSec) * time.Second + if timeout <= 0 { + timeout = 10 * time.Second } return &MiMoService{ apiKey: apiKey, model: model, endpoint: endpoint, + timeout: timeout, logger: logger, } } @@ -126,7 +127,7 @@ func (m *MiMoService) Recognize(ctx context.Context, audio []byte, opts Options) url := strings.TrimRight(m.endpoint, "/") + "/chat/completions" - ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + ctx, cancel := context.WithTimeout(ctx, m.timeout) defer cancel() req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))