fix: STT 服务去除重复默认值,新增 timeout 参数

- Deepgram/MiMo STT 超时从 config 传入
- 去除 model/endpoint 的 fallback 默认值,由 config 层保证
This commit is contained in:
hhs
2026-06-14 11:55:18 +08:00
parent f1ce28966c
commit 03b3566822
2 changed files with 17 additions and 15 deletions

View File

@@ -18,21 +18,22 @@ type DeepgramService struct {
apiKey string apiKey string
model string model string
endpoint string endpoint string
timeout time.Duration
logger *zap.SugaredLogger logger *zap.SugaredLogger
} }
// NewDeepgramService 创建 Deepgram STT 服务。 // NewDeepgramService 创建 Deepgram STT 服务。
func NewDeepgramService(apiKey, model, endpoint string, logger *zap.SugaredLogger) *DeepgramService { // model、endpoint 由 config 层保证非空timeoutSec 为 0 时默认 5 秒。
if model == "" { func NewDeepgramService(apiKey, model, endpoint string, timeoutSec int, logger *zap.SugaredLogger) *DeepgramService {
model = "nova-2" timeout := time.Duration(timeoutSec) * time.Second
} if timeout <= 0 {
if endpoint == "" { timeout = 5 * time.Second
endpoint = "wss://api.deepgram.com/v1/listen"
} }
return &DeepgramService{ return &DeepgramService{
apiKey: apiKey, apiKey: apiKey,
model: model, model: model,
endpoint: endpoint, endpoint: endpoint,
timeout: timeout,
logger: logger, logger: logger,
} }
} }
@@ -57,8 +58,8 @@ func (d *DeepgramService) Recognize(ctx context.Context, audio []byte, opts Opti
// 构建 WebSocket URL附带查询参数 // 构建 WebSocket URL附带查询参数
wsURL := d.buildURL(opts) wsURL := d.buildURL(opts)
// 5 秒总超时 // 总超时
ctx, cancel := context.WithTimeout(ctx, 5*time.Second) ctx, cancel := context.WithTimeout(ctx, d.timeout)
defer cancel() defer cancel()
// 建立 WebSocket 连接 // 建立 WebSocket 连接

View File

@@ -21,21 +21,22 @@ type MiMoService struct {
apiKey string apiKey string
model string model string
endpoint string endpoint string
timeout time.Duration
logger *zap.SugaredLogger logger *zap.SugaredLogger
} }
// NewMiMoService 创建 MiMo STT 服务。 // NewMiMoService 创建 MiMo STT 服务。
func NewMiMoService(apiKey, model, endpoint string, logger *zap.SugaredLogger) *MiMoService { // model、endpoint 由 config 层保证非空timeoutSec 为 0 时默认 10 秒。
if model == "" { func NewMiMoService(apiKey, model, endpoint string, timeoutSec int, logger *zap.SugaredLogger) *MiMoService {
model = "mimo-v2.5-asr" timeout := time.Duration(timeoutSec) * time.Second
} if timeout <= 0 {
if endpoint == "" { timeout = 10 * time.Second
endpoint = "https://api.xiaomimimo.com/v1"
} }
return &MiMoService{ return &MiMoService{
apiKey: apiKey, apiKey: apiKey,
model: model, model: model,
endpoint: endpoint, endpoint: endpoint,
timeout: timeout,
logger: logger, logger: logger,
} }
} }
@@ -126,7 +127,7 @@ func (m *MiMoService) Recognize(ctx context.Context, audio []byte, opts Options)
url := strings.TrimRight(m.endpoint, "/") + "/chat/completions" url := strings.TrimRight(m.endpoint, "/") + "/chat/completions"
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) ctx, cancel := context.WithTimeout(ctx, m.timeout)
defer cancel() defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))