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))