voice: pre-route quiet-hours toggle, whisper ctx cancellation, stale reply fix
- resolveQuietToggle runs in HandlePushToTalk before the router so 'тихий режим' works regardless of classifier confidence. - whisper_full() runs in a goroutine with ctx.Done() select so the handler returns promptly on timeout/shutdown. - StubReplier.IntentQuery no longer claims query is unimplemented.
This commit is contained in:
@@ -34,6 +34,9 @@ func newWhisperHandler(modelPath string) (*whisperHandler, error) {
|
||||
}
|
||||
|
||||
func (h *whisperHandler) Transcribe(ctx context.Context, req worker.TranscribeReq) (worker.TranscribeResp, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return worker.TranscribeResp{}, fmt.Errorf("whisper: context done before transcribe: %w", err)
|
||||
}
|
||||
a := req.Audio
|
||||
if len(a.Bytes) == 0 {
|
||||
return worker.TranscribeResp{}, fmt.Errorf("whisper: empty audio")
|
||||
@@ -59,10 +62,25 @@ func (h *whisperHandler) Transcribe(ctx context.Context, req worker.TranscribeRe
|
||||
params.language = lang
|
||||
params.detect_language = false
|
||||
|
||||
// CGo blocks the goroutine; whisper has no portable CGo-friendly abort
|
||||
// callback. Run in a goroutine so the caller's context cancellation at
|
||||
// least returns promptly — the CGo goroutine leaks until whisper finishes
|
||||
// but the caller doesn't hang.
|
||||
type result struct {
|
||||
code int
|
||||
}
|
||||
ch := make(chan result, 1)
|
||||
cSamples := (*C.float)(unsafe.Pointer(&samples[0]))
|
||||
res := C.whisper_full(h.ctx, params, cSamples, C.int(nSamples))
|
||||
if res != 0 {
|
||||
return worker.TranscribeResp{}, fmt.Errorf("whisper: full failed: %d", int(res))
|
||||
go func() {
|
||||
ch <- result{code: int(C.whisper_full(h.ctx, params, cSamples, C.int(nSamples)))}
|
||||
}()
|
||||
select {
|
||||
case r := <-ch:
|
||||
if r.code != 0 {
|
||||
return worker.TranscribeResp{}, fmt.Errorf("whisper: full failed: %d", r.code)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return worker.TranscribeResp{}, fmt.Errorf("whisper: %w", ctx.Err())
|
||||
}
|
||||
|
||||
nSegments := int(C.whisper_full_n_segments(h.ctx))
|
||||
|
||||
Reference in New Issue
Block a user