fix(speech): skip a canceled stream's remaining chunks inside one feed or finish

This commit is contained in:
Kelvin Amoaba
2026-11-09 22:19:52 +00:00
parent a8efa24766
commit d19f394abd
3 changed files with 13 additions and 0 deletions
+2
View File
@@ -29,4 +29,6 @@ export type SttRecognizer = {
finish(streamId: string): Promise<void>
/** Drop the stream's audio undecoded. */
discard(streamId: string): void
/** Called on a cancel's arrival, ahead of the queue: skip the stream's remaining chunks; the running decode finishes. */
cancel(streamId: string): void
}
@@ -174,9 +174,15 @@ export async function createTranscribeCppRecognizer(
})
}
// Canceled streams, until their queued discard runs; also covers a finish that already closed its stream.
const canceled = new Set<string>()
const decode = async (streamId: string, stream: StreamCoverage, chunks: Float32Array[]) => {
let undecoded = chunks.reduce((total, chunk) => total + chunk.length, 0)
for (const chunk of chunks) {
if (canceled.has(streamId)) {
return
}
const startedAt = performance.now()
const text = await transcribeChunk(chunk, stream.options)
const decodeMs = performance.now() - startedAt
@@ -254,7 +260,11 @@ export async function createTranscribeCppRecognizer(
}
},
discard(streamId) {
canceled.delete(streamId)
closeStream(streamId)?.vad?.dispose()
},
cancel(streamId) {
canceled.add(streamId)
}
}
}
+1
View File
@@ -114,6 +114,7 @@ async function handleStop(streamId: string): Promise<void> {
// A cancel answers at once, even during Finish, and the stream's queued audio is skipped rather than decoded.
function cancelStream(streamId: string): void {
discarded.add(streamId)
recognizer?.cancel(streamId)
const input = audioPorts.get(streamId)
audioPorts.delete(streamId)
input?.port.close()