Skip to content

Open source CapCut alternative.

TypeScript

fix(site): a dictation always ends with an answer [rebuild]

David Leb5ff846f3d2b
diff --git a/site/src/cut/server/mic.ts b/site/src/cut/server/mic.ts
index c4001e17..7009b0bd 100644
--- a/site/src/cut/server/mic.ts
+++ b/site/src/cut/server/mic.ts
@@ -36,9 +36,12 @@ const IDLE_MS = 15000;
// ~250ms) — a new start must never kill it mid-speech. Anything staler is an
// orphan a new start may reclaim without waiting out the idle reaper.
const STALE_MS = 1500;
+// How long a confirmed session may take to flush its final text. cut-stt bounds
+// its own finish well inside this, so passing it means the process is gone.
+const STOP_MS = 8000;
// A confirmed session flushing its final text feeds no PCM by design; stopMic
-// waits up to 8s for the flush, so only past that is the process fair game.
-const FINALIZE_MS = 9000;
+// waits out STOP_MS for the flush, so only past that is the process fair game.
+const FINALIZE_MS = STOP_MS + 1000;
const { jobs, retire } = createJobRegistry<MicJob>("__cutMicJobs");
function clearIdle(job: MicJob): void {
@@ -172,7 +175,9 @@ export function feedMic(id: string, pcm: Buffer): boolean {
return true;
}
-/** Close the input so the model flushes its final text, then wait for it. */
+/** Close the input so the model flushes its final text, then wait for it. A
+ * process that misses the deadline is not coming back, so it is killed here
+ * rather than left holding the next dictation's slot. */
export async function stopMic(id: string): Promise<string | null> {
const job = jobs.get(id);
if (!job) return null;
@@ -181,7 +186,7 @@ export async function stopMic(id: string): Promise<string | null> {
job.finalizeAt = Date.now();
job.proc.stdin?.end();
}
- await withTimeout(job.finished, 8000);
+ if (!(await withTimeout(job.finished, STOP_MS))) job.proc.kill("SIGKILL");
return job.text;
}
@@ -197,13 +202,14 @@ export function cancelMic(id: string): boolean {
return true;
}
-function withTimeout(p: Promise<void>, ms: number): Promise<void> {
+/** Resolves true when `p` settled in time, false when the deadline won. */
+function withTimeout(p: Promise<void>, ms: number): Promise<boolean> {
return new Promise((resolve) => {
- const t = setTimeout(resolve, ms);
+ const t = setTimeout(() => resolve(false), ms);
t.unref();
void p.finally(() => {
clearTimeout(t);
- resolve();
+ resolve(true);
});
});
}
diff --git a/site/src/cut/server/native/cut-stt.swift b/site/src/cut/server/native/cut-stt.swift
index 58217f1f..efc4cd8b 100644
--- a/site/src/cut/server/native/cut-stt.swift
+++ b/site/src/cut/server/native/cut-stt.swift
@@ -108,11 +108,46 @@ struct Main {
// MARK: - Live streaming (stdin PCM → partial transcripts)
+/// Scratch shared by the stdin reader, the results loop, and the finish
+/// watchdog: whether any audio ever arrived, and the newest text to fall back
+/// on. Each of the three runs on its own thread, so the lock is the contract.
+final class LiveState: @unchecked Sendable {
+ private let lock = NSLock()
+ private var heardAudio = false
+ private var newest = ""
+ func markAudio() {
+ lock.lock()
+ heardAudio = true
+ lock.unlock()
+ }
+ var receivedAudio: Bool {
+ lock.lock()
+ defer { lock.unlock() }
+ return heardAudio
+ }
+ func record(_ text: String) {
+ lock.lock()
+ newest = text
+ lock.unlock()
+ }
+ var latest: String {
+ lock.lock()
+ defer { lock.unlock() }
+ return newest
+ }
+}
+
+// The finish is bounded: whatever the model has said by here is the final text.
+// The parent waits on this process for its transcript, so a finalize that stops
+// answering has to end as an answer, not as a stall.
+let finishDeadline = Duration.seconds(6)
+
@MainActor
func runLive(locale: Locale) async {
// The browser pipes raw mic audio in this exact format; keep in sync with the
// client's downsampler in lib/micTranscribe.ts.
let liveSampleRate = 16000.0
+ let live = LiveState()
do {
let transcriber = SpeechTranscriber(
locale: locale,
@@ -161,6 +196,7 @@ func runLive(locale: Locale) async {
let converted = convertBuffer(buffer, using: converter, to: analyzerFormat)
{
inputBuilder.yield(AnalyzerInput(buffer: converted))
+ live.markAudio()
}
}
inputBuilder.finish()
@@ -177,8 +213,10 @@ func runLive(locale: Locale) async {
let piece = String(result.text.characters)
if result.isFinal {
finalized += piece
+ live.record(finalized)
emit("partial", finalized)
} else {
+ live.record(finalized + piece)
emit("partial", finalized + piece)
}
}
@@ -190,8 +228,22 @@ func runLive(locale: Locale) async {
await withCheckedContinuation { cont in
DispatchQueue.global().async { readerDone.wait(); cont.resume() }
}
+ // A session that was never fed has nothing to flush, and the analyzer's
+ // finalize never returns for an input that carried no audio — answer with
+ // the empty transcript and go.
+ guard live.receivedAudio else {
+ emit("final", "")
+ exit(0)
+ }
+ let watchdog = Task { @MainActor in
+ try? await Task.sleep(for: finishDeadline)
+ guard !Task.isCancelled else { return }
+ emit("final", live.latest)
+ exit(0)
+ }
try await analyzer.finalizeAndFinishThroughEndOfInput()
let finalText = try await results.value
+ watchdog.cancel()
emit("final", finalText)
} catch {
emit("error", "\(error)")