@mulmoclaude/core 0.12.0 → 0.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/helps/error-recovery.md +90 -0
- package/assets/helps/index.md +1 -0
- package/assets/helps/remote-host.md +122 -0
- package/dist/scheduler/adapter.d.ts +24 -1
- package/dist/scheduler/index.cjs +176 -75
- package/dist/scheduler/index.cjs.map +1 -1
- package/dist/scheduler/index.d.ts +2 -1
- package/dist/scheduler/index.js +172 -79
- package/dist/scheduler/index.js.map +1 -1
- package/dist/scheduler/task-manager.d.ts +19 -6
- package/dist/whisper/client-helpers.d.ts +22 -0
- package/dist/whisper/client.cjs +227 -104
- package/dist/whisper/client.cjs.map +1 -1
- package/dist/whisper/client.d.ts +50 -0
- package/dist/whisper/client.js +225 -105
- package/dist/whisper/client.js.map +1 -1
- package/dist/whisper/index.cjs +188 -132
- package/dist/whisper/index.cjs.map +1 -1
- package/dist/whisper/index.js +188 -132
- package/dist/whisper/index.js.map +1 -1
- package/dist/whisper/models.d.ts +6 -0
- package/dist/whisper/sidecar-helpers.d.ts +7 -0
- package/dist/whisper/sidecar.d.ts +24 -0
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","names":[],"sources":["../../src/whisper/client.ts"],"sourcesContent":["// @mulmoclaude/core/whisper/client — framework-neutral browser capture controller.\n// Records one utterance at a time with MediaRecorder, segments on pauses via a\n// Web Audio VAD, and sends each segment through an injected transport. State is\n// pushed via `onState`; the host wraps this into its own reactivity (Vue refs,\n// React state, …). No framework dependency. See plans/done/feat-extract-whisper-package.md.\n\n// Map a UI locale to a Whisper language code. UI language is a strong prior for\n// the spoken language; \"auto\" lets Whisper detect it from the audio.\nconst LOCALE_TO_WHISPER: Record<string, string> = {\n en: \"en\",\n ja: \"ja\",\n zh: \"zh\",\n ko: \"ko\",\n es: \"es\",\n \"pt-BR\": \"pt\",\n fr: \"fr\",\n de: \"de\",\n};\n\nexport function localeToWhisperLanguage(locale: string): string {\n return LOCALE_TO_WHISPER[locale] ?? \"auto\";\n}\n\n// VAD tuning. RMS over [-1,1] float samples; a pause is SILENCE_MS of\n// sub-threshold level after speech. MAX_SEGMENT_MS force-cuts a long unbroken\n// utterance so no clip exceeds Whisper's 30s window or the server's size cap.\nconst SPEECH_RMS = 0.015;\nconst SILENCE_MS = 800;\nconst MAX_SEGMENT_MS = 20_000;\nconst MONITOR_INTERVAL_MS = 100;\nconst AVAILABILITY_POLL_MS = 2_000;\n\nfunction pickRecorderMime(): string | undefined {\n const candidates = [\"audio/webm;codecs=opus\", \"audio/webm\", \"audio/mp4\"];\n if (typeof MediaRecorder === \"undefined\") return undefined;\n return candidates.find((type) => MediaRecorder.isTypeSupported(type));\n}\n\nfunction blobToDataUrl(blob: Blob): Promise<string> {\n return new Promise((resolve, reject) => {\n const reader = new FileReader();\n reader.onload = () => resolve(String(reader.result));\n reader.onerror = () => reject(reader.error ?? new Error(\"FileReader failed\"));\n reader.readAsDataURL(blob);\n });\n}\n\nfunction computeRms(buffer: Float32Array): number {\n let sum = 0;\n for (const sample of buffer) sum += sample * sample;\n return Math.sqrt(sum / buffer.length);\n}\n\nfunction toMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n\nexport interface VoiceCaptureTransport {\n /** Transcribe one segment. Throws on failure. */\n transcribe: (dataUrl: string, language: string) => Promise<{ text: string }>;\n /** Current readiness. `downloading` true keeps the controller polling so it\n * flips to ready as soon as a model download finishes. */\n getStatus: () => Promise<{ ready: boolean; downloading: boolean }>;\n}\n\nexport interface VoiceCaptureState {\n available: boolean;\n listening: boolean;\n transcribing: boolean;\n}\n\nexport interface VoiceCaptureCallbacks {\n /** A recognized (non-empty) segment transcript. */\n onTranscript: (text: string) => void;\n /** A segment produced no speech. */\n onEmpty?: () => void;\n /** A recoverable error message (transport failure, permission denied, etc.). */\n onError?: (message: string) => void;\n /** Pushed whenever available/listening/transcribing changes. */\n onState?: (state: VoiceCaptureState) => void;\n}\n\nexport interface VoiceCapture {\n refreshAvailability: () => Promise<void>;\n start: () => Promise<boolean>;\n stop: () => void;\n dispose: () => void;\n}\n\nexport function createVoiceCapture(transport: VoiceCaptureTransport, language: () => string, callbacks: VoiceCaptureCallbacks): VoiceCapture {\n let available = false;\n let listening = false;\n let transcribing = false;\n\n function emit(): void {\n callbacks.onState?.({ available, listening, transcribing });\n }\n function setAvailable(value: boolean): void {\n if (available !== value) {\n available = value;\n emit();\n }\n }\n function setListening(value: boolean): void {\n if (listening !== value) {\n listening = value;\n emit();\n }\n }\n\n let stream: MediaStream | null = null;\n let audioCtx: AudioContext | null = null;\n let analyser: AnalyserNode | null = null;\n let vadBuffer = new Float32Array(0);\n let monitorHandle: number | null = null;\n let recorder: MediaRecorder | null = null;\n let chunks: Blob[] = [];\n let mimeType = \"\";\n let segmentHasSpeech = false;\n let silenceStart: number | null = null;\n let segmentStart = 0;\n let pending = 0;\n let queue: Promise<void> = Promise.resolve();\n let availabilityPollHandle: number | null = null;\n // Bumped on stop(). Segments captured / sends resolved under an older\n // generation are dropped, so a late transcript never leaks across sessions.\n let generation = 0;\n let segmentGeneration = 0;\n // Single-flight guard for start(): true between entry and the moment capture\n // is set up (or the attempt aborts), so a second start can't race the first.\n let startInFlight = false;\n\n function setPending(delta: number): void {\n pending += delta;\n const next = pending > 0;\n if (transcribing !== next) {\n transcribing = next;\n emit();\n }\n }\n\n function stopAvailabilityPoll(): void {\n if (availabilityPollHandle !== null) {\n window.clearInterval(availabilityPollHandle);\n availabilityPollHandle = null;\n }\n }\n\n async function refreshAvailability(): Promise<void> {\n let status: { ready: boolean; downloading: boolean };\n try {\n status = await transport.getStatus();\n } catch {\n setAvailable(false);\n stopAvailabilityPoll();\n return;\n }\n setAvailable(status.ready);\n if (status.downloading) {\n if (availabilityPollHandle === null) {\n availabilityPollHandle = window.setInterval(() => {\n void refreshAvailability();\n }, AVAILABILITY_POLL_MS);\n }\n } else {\n stopAvailabilityPoll();\n }\n }\n\n async function sendSegment(blob: Blob, gen: number): Promise<void> {\n if (gen !== generation) return;\n try {\n const dataUrl = await blobToDataUrl(blob);\n const result = await transport.transcribe(dataUrl, language());\n if (gen !== generation) return;\n const text = result.text.trim();\n if (text.length === 0) callbacks.onEmpty?.();\n else callbacks.onTranscript(text);\n } catch (err) {\n // Generation-guard the failure path too: a send rejected after stop()/\n // session change belongs to a session the user already left.\n if (gen === generation) callbacks.onError?.(toMessage(err));\n }\n }\n\n // Serialize sends so transcripts append in capture order; `pending` keeps\n // `transcribing` true from enqueue until the send resolves.\n function enqueue(blob: Blob, gen: number): void {\n setPending(1);\n queue = queue\n .then(() => sendSegment(blob, gen))\n .catch(() => undefined)\n .finally(() => setPending(-1));\n }\n\n function containerType(): string {\n return mimeType.split(\";\")[0] || \"audio/webm\";\n }\n\n function onSegmentStop(): void {\n const hadSpeech = segmentHasSpeech;\n const gen = segmentGeneration;\n const blob = new Blob(chunks, { type: containerType() });\n if (listening) startRecorder();\n if (hadSpeech && blob.size > 0 && gen === generation) enqueue(blob, gen);\n }\n\n function startRecorder(): void {\n if (!stream) return;\n chunks = [];\n segmentHasSpeech = false;\n silenceStart = null;\n segmentStart = Date.now();\n segmentGeneration = generation;\n recorder = new MediaRecorder(stream, { mimeType });\n recorder.ondataavailable = (event) => {\n if (event.data.size > 0) chunks.push(event.data);\n };\n recorder.onstop = onSegmentStop;\n recorder.start();\n }\n\n function cutSegment(): void {\n if (recorder && recorder.state === \"recording\") recorder.stop();\n }\n\n function monitorTick(): void {\n if (!analyser) return;\n analyser.getFloatTimeDomainData(vadBuffer);\n const rms = computeRms(vadBuffer);\n const now = Date.now();\n if (rms > SPEECH_RMS) {\n segmentHasSpeech = true;\n silenceStart = null;\n } else if (segmentHasSpeech) {\n if (silenceStart === null) silenceStart = now;\n else if (now - silenceStart > SILENCE_MS) cutSegment();\n }\n if (segmentHasSpeech && now - segmentStart > MAX_SEGMENT_MS) cutSegment();\n }\n\n async function start(): Promise<boolean> {\n // Single-flight: `listening` only flips true AFTER getUserMedia resolves, so\n // a benign skip returns true (a start is already active / in progress).\n if (startInFlight || listening) return true;\n startInFlight = true;\n const startGen = generation;\n try {\n mimeType = pickRecorderMime() ?? \"\";\n if (!mimeType || !navigator.mediaDevices?.getUserMedia) {\n callbacks.onError?.(\"unsupported\");\n return false;\n }\n let acquired: MediaStream;\n try {\n acquired = await navigator.mediaDevices.getUserMedia({ audio: true });\n } catch {\n callbacks.onError?.(\"permission-denied\");\n return false;\n }\n // stop() bumps the generation; if it fired while we awaited permission\n // this start is stale — release the mic and abort.\n if (startGen !== generation) {\n acquired.getTracks().forEach((track) => track.stop());\n return false;\n }\n stream = acquired;\n audioCtx = new AudioContext();\n analyser = audioCtx.createAnalyser();\n analyser.fftSize = 2048;\n vadBuffer = new Float32Array(analyser.fftSize);\n audioCtx.createMediaStreamSource(stream).connect(analyser);\n setListening(true);\n startRecorder();\n monitorHandle = window.setInterval(monitorTick, MONITOR_INTERVAL_MS);\n return true;\n } finally {\n startInFlight = false;\n }\n }\n\n function stop(): void {\n // Bump the generation so any in-flight/queued segment is dropped rather than\n // applied after the user stops or switches sessions.\n generation += 1;\n setListening(false);\n if (monitorHandle !== null) {\n window.clearInterval(monitorHandle);\n monitorHandle = null;\n }\n if (recorder && recorder.state === \"recording\") recorder.stop();\n recorder = null;\n if (audioCtx) {\n audioCtx.close().catch(() => undefined);\n audioCtx = null;\n }\n analyser = null;\n stream?.getTracks().forEach((track) => track.stop());\n stream = null;\n }\n\n function dispose(): void {\n stopAvailabilityPoll();\n stop();\n }\n\n return { refreshAvailability, start, stop, dispose };\n}\n"],"mappings":";AAQA,IAAM,oBAA4C;CAChD,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,SAAS;CACT,IAAI;CACJ,IAAI;AACN;AAEA,SAAgB,wBAAwB,QAAwB;CAC9D,OAAO,kBAAkB,WAAW;AACtC;AAKA,IAAM,aAAa;AACnB,IAAM,aAAa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I;IACF,WAAW,MAAM,UAAU,aAAa,aAAa,EAAE,OAAO,KAAK,CAAC;GACtE,QAAQ;IACN,UAAU,UAAU,mBAAmB;IACvC,OAAO;GACT;GAGA,IAAI,aAAa,YAAY;IAC3B,SAAS,UAAU,CAAC,CAAC,SAAS,UAAU,MAAM,KAAK,CAAC;IACpD,OAAO;GACT;GACA,SAAS;GACT,WAAW,IAAI,aAAa;GAC5B,WAAW,SAAS,eAAe;GACnC,SAAS,UAAU;GACnB,YAAY,IAAI,aAAa,SAAS,OAAO;GAC7C,SAAS,wBAAwB,MAAM,CAAC,CAAC,QAAQ,QAAQ;GACzD,aAAa,IAAI;GACjB,cAAc;GACd,gBAAgB,OAAO,YAAY,aAAa,mBAAmB;GACnE,OAAO;EACT,UAAU;GACR,gBAAgB;EAClB;CACF;CAEA,SAAS,OAAa;EAGpB,cAAc;EACd,aAAa,KAAK;EAClB,IAAI,kBAAkB,MAAM;GAC1B,OAAO,cAAc,aAAa;GAClC,gBAAgB;EAClB;EACA,IAAI,YAAY,SAAS,UAAU,aAAa,SAAS,KAAK;EAC9D,WAAW;EACX,IAAI,UAAU;GACZ,SAAS,MAAM,CAAC,CAAC,YAAY,KAAA,CAAS;GACtC,WAAW;EACb;EACA,WAAW;EACX,QAAQ,UAAU,CAAC,CAAC,SAAS,UAAU,MAAM,KAAK,CAAC;EACnD,SAAS;CACX;CAEA,SAAS,UAAgB;EACvB,qBAAqB;EACrB,KAAK;CACP;CAEA,OAAO;EAAE;EAAqB;EAAO;EAAM;CAAQ;AACrD"}
|
|
1
|
+
{"version":3,"file":"client.js","names":[],"sources":["../../src/whisper/client-helpers.ts","../../src/whisper/client.ts"],"sourcesContent":["// Pure, framework-neutral helpers for the browser voice-capture controller.\n// Kept separate from `client.ts` so the audio math and VAD state machine can be\n// unit-tested without a DOM / MediaRecorder / Web Audio environment.\n\nexport function computeRms(buffer: Float32Array): number {\n let sum = 0;\n for (const sample of buffer) sum += sample * sample;\n return Math.sqrt(sum / buffer.length);\n}\n\n/** The recorder mime can carry a codec suffix (`audio/webm;codecs=opus`); the\n * Blob container type is just the part before the `;`. */\nexport function containerTypeFromMime(mimeType: string): string {\n return mimeType.split(\";\")[0] || \"audio/webm\";\n}\n\nexport interface VadState {\n readonly hasSpeech: boolean;\n readonly silenceStart: number | null;\n}\n\nexport interface VadConfig {\n readonly speechRms: number;\n readonly silenceMs: number;\n readonly maxSegmentMs: number;\n}\n\nexport interface VadDecision {\n readonly hasSpeech: boolean;\n readonly silenceStart: number | null;\n readonly cut: boolean;\n}\n\nfunction isSpeech(rms: number, config: VadConfig): boolean {\n return rms > config.speechRms;\n}\n\nfunction nextSilenceStart(state: VadState, rms: number, nowMs: number, config: VadConfig): number | null {\n if (isSpeech(rms, config)) return null;\n if (state.hasSpeech && state.silenceStart === null) return nowMs;\n return state.silenceStart;\n}\n\nfunction silenceExceeded(state: VadState, rms: number, nowMs: number, config: VadConfig): boolean {\n if (isSpeech(rms, config) || !state.hasSpeech || state.silenceStart === null) return false;\n return nowMs - state.silenceStart > config.silenceMs;\n}\n\n/** Advance the pause-detector one tick: fold the current RMS/time into the\n * segment state and decide whether the segment should be force-cut (either a\n * long-enough silence after speech, or the max-segment length reached). */\nexport function evaluateVad(state: VadState, segmentStartMs: number, rms: number, nowMs: number, config: VadConfig): VadDecision {\n const hasSpeech = isSpeech(rms, config) || state.hasSpeech;\n const silenceStart = nextSilenceStart(state, rms, nowMs, config);\n const maxCut = hasSpeech && nowMs - segmentStartMs > config.maxSegmentMs;\n return { hasSpeech, silenceStart, cut: silenceExceeded(state, rms, nowMs, config) || maxCut };\n}\n","// @mulmoclaude/core/whisper/client — framework-neutral browser capture controller.\n// Records one utterance at a time with MediaRecorder, segments on pauses via a\n// Web Audio VAD, and sends each segment through an injected transport. State is\n// pushed via `onState`; the host wraps this into its own reactivity (Vue refs,\n// React state, …). No framework dependency. See plans/done/feat-extract-whisper-package.md.\n\nimport { computeRms, containerTypeFromMime, evaluateVad, type VadConfig } from \"./client-helpers.ts\";\n\n// Map a UI locale to a Whisper language code. UI language is a strong prior for\n// the spoken language; \"auto\" lets Whisper detect it from the audio.\nconst LOCALE_TO_WHISPER: Record<string, string> = {\n en: \"en\",\n ja: \"ja\",\n zh: \"zh\",\n ko: \"ko\",\n es: \"es\",\n \"pt-BR\": \"pt\",\n fr: \"fr\",\n de: \"de\",\n};\n\nexport function localeToWhisperLanguage(locale: string): string {\n return LOCALE_TO_WHISPER[locale] ?? \"auto\";\n}\n\n// VAD tuning. RMS over [-1,1] float samples; a pause is SILENCE_MS of\n// sub-threshold level after speech. MAX_SEGMENT_MS force-cuts a long unbroken\n// utterance so no clip exceeds Whisper's 30s window or the server's size cap.\nconst SPEECH_RMS = 0.015;\nconst SILENCE_MS = 800;\nconst MAX_SEGMENT_MS = 20_000;\nconst MONITOR_INTERVAL_MS = 100;\nconst AVAILABILITY_POLL_MS = 2_000;\nconst VAD_CONFIG: VadConfig = { speechRms: SPEECH_RMS, silenceMs: SILENCE_MS, maxSegmentMs: MAX_SEGMENT_MS };\n\nfunction pickRecorderMime(): string | undefined {\n const candidates = [\"audio/webm;codecs=opus\", \"audio/webm\", \"audio/mp4\"];\n if (typeof MediaRecorder === \"undefined\") return undefined;\n return candidates.find((type) => MediaRecorder.isTypeSupported(type));\n}\n\nfunction blobToDataUrl(blob: Blob): Promise<string> {\n return new Promise((resolve, reject) => {\n const reader = new FileReader();\n reader.onload = () => resolve(String(reader.result));\n reader.onerror = () => reject(reader.error ?? new Error(\"FileReader failed\"));\n reader.readAsDataURL(blob);\n });\n}\n\nfunction toMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n\nexport interface VoiceCaptureTransport {\n /** Transcribe one segment. Throws on failure. */\n transcribe: (dataUrl: string, language: string) => Promise<{ text: string }>;\n /** Current readiness. `downloading` true keeps the controller polling so it\n * flips to ready as soon as a model download finishes. */\n getStatus: () => Promise<{ ready: boolean; downloading: boolean }>;\n}\n\nexport interface VoiceCaptureState {\n available: boolean;\n listening: boolean;\n transcribing: boolean;\n}\n\nexport interface VoiceCaptureCallbacks {\n /** A recognized (non-empty) segment transcript. */\n onTranscript: (text: string) => void;\n /** A segment produced no speech. */\n onEmpty?: () => void;\n /** A recoverable error message (transport failure, permission denied, etc.). */\n onError?: (message: string) => void;\n /** Pushed whenever available/listening/transcribing changes. */\n onState?: (state: VoiceCaptureState) => void;\n}\n\nexport interface VoiceCapture {\n refreshAvailability: () => Promise<void>;\n start: () => Promise<boolean>;\n stop: () => void;\n dispose: () => void;\n}\n\nexport interface CaptureStateController {\n setAvailable: (value: boolean) => void;\n setListening: (value: boolean) => void;\n setPending: (delta: number) => void;\n isListening: () => boolean;\n}\n\n// Owns the three observable flags. Each setter emits only on an actual change so\n// the host's reactivity never churns on a no-op write. `pending` is a private\n// counter; `transcribing` is true exactly while at least one send is in flight.\nexport function createCaptureState(onState?: (state: VoiceCaptureState) => void): CaptureStateController {\n let available = false;\n let listening = false;\n let transcribing = false;\n let pending = 0;\n\n function emit(): void {\n onState?.({ available, listening, transcribing });\n }\n function setAvailable(value: boolean): void {\n if (available !== value) {\n available = value;\n emit();\n }\n }\n function setListening(value: boolean): void {\n if (listening !== value) {\n listening = value;\n emit();\n }\n }\n function setPending(delta: number): void {\n pending += delta;\n const next = pending > 0;\n if (transcribing !== next) {\n transcribing = next;\n emit();\n }\n }\n\n return { setAvailable, setListening, setPending, isListening: () => listening };\n}\n\nexport interface AvailabilityPoller {\n refresh: () => Promise<void>;\n stop: () => void;\n}\n\n// Owns the polling timer. `refresh` mirrors transport readiness into\n// `setAvailable`; while a model is downloading it self-schedules so `available`\n// flips as soon as the download finishes. A `getStatus` throw fails closed\n// (unavailable) and tears the timer down.\nexport function createAvailabilityPoller(transport: VoiceCaptureTransport, setAvailable: (value: boolean) => void): AvailabilityPoller {\n let availabilityPollHandle: number | null = null;\n\n function stop(): void {\n if (availabilityPollHandle !== null) {\n window.clearInterval(availabilityPollHandle);\n availabilityPollHandle = null;\n }\n }\n\n async function refresh(): Promise<void> {\n let status: { ready: boolean; downloading: boolean };\n try {\n status = await transport.getStatus();\n } catch {\n setAvailable(false);\n stop();\n return;\n }\n setAvailable(status.ready);\n if (status.downloading) {\n if (availabilityPollHandle === null) {\n availabilityPollHandle = window.setInterval(() => {\n void refresh();\n }, AVAILABILITY_POLL_MS);\n }\n } else {\n stop();\n }\n }\n\n return { refresh, stop };\n}\n\nexport interface SegmentQueueOptions {\n transport: VoiceCaptureTransport;\n language: () => string;\n callbacks: VoiceCaptureCallbacks;\n setPending: (delta: number) => void;\n // Read-only epoch seam: the queue never mutates the generation, it only reads\n // the parent's current value to drop segments captured under an older session.\n getGeneration: () => number;\n}\n\nexport interface SegmentQueue {\n enqueue: (blob: Blob, gen: number) => void;\n}\n\n// Owns the serialized send chain. Each blob is transcribed in capture order; the\n// generation is checked at entry and again after transcription so a segment\n// belonging to a session the user already left is dropped silently.\nexport function createSegmentQueue(options: SegmentQueueOptions): SegmentQueue {\n const { transport, language, callbacks, setPending, getGeneration } = options;\n let queue: Promise<void> = Promise.resolve();\n\n async function sendSegment(blob: Blob, gen: number): Promise<void> {\n if (gen !== getGeneration()) return;\n try {\n const dataUrl = await blobToDataUrl(blob);\n const result = await transport.transcribe(dataUrl, language());\n if (gen !== getGeneration()) return;\n const text = result.text.trim();\n if (text.length === 0) callbacks.onEmpty?.();\n else callbacks.onTranscript(text);\n } catch (err) {\n // Generation-guard the failure path too: a send rejected after stop()/\n // session change belongs to a session the user already left.\n if (gen === getGeneration()) callbacks.onError?.(toMessage(err));\n }\n }\n\n // Serialize sends so transcripts append in capture order; `pending` keeps\n // `transcribing` true from enqueue until the send resolves.\n function enqueue(blob: Blob, gen: number): void {\n setPending(1);\n queue = queue\n .then(() => sendSegment(blob, gen))\n .catch(() => undefined)\n .finally(() => setPending(-1));\n }\n\n return { enqueue };\n}\n\nexport interface AudioGraph {\n /** True between `attach()` and the next `teardown()`. */\n isAttached: () => boolean;\n /** Read the current PCM window into an internal buffer and return it.\n * Callers must not mutate the returned array. */\n sample: () => Float32Array;\n /** Live mic stream — the outer needs it to hand to MediaRecorder. */\n getStream: () => MediaStream | null;\n /** Attach a fresh mic stream: create AudioContext + AnalyserNode + VAD buffer,\n * connect the source. Called only by `bootstrapCapture` after the caller has\n * passed the stale-generation / permission checks. */\n attach: (stream: MediaStream) => void;\n /** Close the AudioContext, stop the mic tracks, null everything. Safe to\n * call before `attach()` or twice in a row. */\n teardown: () => void;\n}\n\n// Owns the browser-side audio pipeline: mic stream + AudioContext + AnalyserNode\n// + the reusable Float32 buffer VAD reads samples into. Separated from\n// `createVoiceCapture` so its four browser-API fields don't inflate the parent\n// factory's line count and so the setup/teardown ordering is in one place.\nfunction createAudioGraph(): AudioGraph {\n let stream: MediaStream | null = null;\n let audioCtx: AudioContext | null = null;\n let analyser: AnalyserNode | null = null;\n let vadBuffer = new Float32Array(0);\n\n function attach(nextStream: MediaStream): void {\n stream = nextStream;\n audioCtx = new AudioContext();\n analyser = audioCtx.createAnalyser();\n analyser.fftSize = 2048;\n vadBuffer = new Float32Array(analyser.fftSize);\n audioCtx.createMediaStreamSource(nextStream).connect(analyser);\n }\n\n function sample(): Float32Array {\n analyser?.getFloatTimeDomainData(vadBuffer);\n return vadBuffer;\n }\n\n function teardown(): void {\n if (audioCtx) {\n audioCtx.close().catch(() => undefined);\n audioCtx = null;\n }\n analyser = null;\n stream?.getTracks().forEach((track) => track.stop());\n stream = null;\n }\n\n return { isAttached: () => analyser !== null, sample, getStream: () => stream, attach, teardown };\n}\n\ninterface BootstrappedCapture {\n audioGraph: AudioGraph;\n mimeType: string;\n}\n\n// Acquire the mic + probe MediaRecorder support + wire an AudioGraph, returning\n// null (with `callbacks.onError` fired) on any recoverable failure. Deliberately\n// does NOT touch `state` or start the recorder — the caller commits the session\n// only after this resolves successfully, so a stale `generation` detected mid-\n// flight can drop the tracks and bail without partially publishing state.\nasync function bootstrapCapture(callbacks: VoiceCaptureCallbacks, generationAtStart: number, generationNow: () => number): Promise<BootstrappedCapture | null> {\n const mimeType = pickRecorderMime() ?? \"\";\n if (!mimeType || !navigator.mediaDevices?.getUserMedia) {\n callbacks.onError?.(\"unsupported\");\n return null;\n }\n let acquired: MediaStream;\n try {\n acquired = await navigator.mediaDevices.getUserMedia({ audio: true });\n } catch {\n callbacks.onError?.(\"permission-denied\");\n return null;\n }\n if (generationAtStart !== generationNow()) {\n acquired.getTracks().forEach((track) => track.stop());\n return null;\n }\n const audioGraph = createAudioGraph();\n try {\n audioGraph.attach(acquired);\n } catch (err) {\n // teardown() is null-safe and closes any half-created AudioContext AND\n // stops the mic tracks — the pre-refactor code only stopped tracks and\n // leaked the context (codex/CodeRabbit findings on this PR).\n audioGraph.teardown();\n throw err;\n }\n return { audioGraph, mimeType };\n}\n\nexport interface RecorderSession {\n isRecording: () => boolean;\n hadSpeech: () => boolean;\n markSpeech: () => void;\n /** Start a new segment on `stream`, tagged with `gen`. Resets per-segment\n * state (chunks + hadSpeech) synchronously so a subsequent VAD tick sees\n * a fresh window. */\n startNext: (stream: MediaStream, mimeType: string, gen: number) => void;\n /** Force-close the current segment (silence exceeded or MAX_SEGMENT_MS). */\n cut: () => void;\n}\n\ninterface RecorderSessionDeps {\n /** Fires each time a segment finishes. The parent decides whether to\n * restart (via `session.startNext`) and whether to enqueue the blob. */\n onSegmentEnd: (blob: Blob, hadSpeech: boolean, gen: number) => void;\n}\n\n// Owns MediaRecorder + its per-segment scratchpad (chunks, hadSpeech, gen,\n// mimeType). The parent keeps VAD state and the global generation, and calls\n// `startNext` on both initial-start and post-onstop restart paths.\nfunction createRecorderSession(deps: RecorderSessionDeps): RecorderSession {\n let recorder: MediaRecorder | null = null;\n let chunks: Blob[] = [];\n let segmentHasSpeech = false;\n let sessionGen = 0;\n let sessionMimeType = \"\";\n\n function onStop(): void {\n // Snapshot BEFORE the parent's onSegmentEnd callback runs; `startNext`\n // (if the parent chooses to restart) will clear chunks synchronously.\n const hadSpeech = segmentHasSpeech;\n const gen = sessionGen;\n const blob = new Blob(chunks, { type: containerTypeFromMime(sessionMimeType) });\n deps.onSegmentEnd(blob, hadSpeech, gen);\n }\n\n function startNext(stream: MediaStream, mimeType: string, gen: number): void {\n chunks = [];\n segmentHasSpeech = false;\n sessionGen = gen;\n sessionMimeType = mimeType;\n recorder = new MediaRecorder(stream, { mimeType });\n recorder.ondataavailable = (event) => {\n if (event.data.size > 0) chunks.push(event.data);\n };\n recorder.onstop = onStop;\n recorder.start();\n }\n\n return {\n isRecording: () => recorder?.state === \"recording\",\n hadSpeech: () => segmentHasSpeech,\n markSpeech: () => {\n segmentHasSpeech = true;\n },\n startNext,\n cut: () => {\n if (recorder?.state === \"recording\") recorder.stop();\n },\n };\n}\n\n// Zero-values used to seed a fresh `CaptureRuntime` at the top of every\n// `createVoiceCapture` call. Kept module-scope so the seed doesn't inflate the\n// factory's line count.\nconst INITIAL_RUNTIME: Readonly<CaptureRuntime> = {\n audioGraph: null,\n monitorHandle: null,\n mimeType: \"\",\n silenceStart: null,\n segmentStart: 0,\n generation: 0,\n startInFlight: false,\n};\n\n// Mutable capture-time state, kept as one object so module-scope helpers\n// (`stopCapture`, `monitorTick`) can be typed against one shape rather than\n// threading each field through their signatures.\ninterface CaptureRuntime {\n audioGraph: AudioGraph | null;\n monitorHandle: number | null;\n mimeType: string;\n silenceStart: number | null;\n segmentStart: number;\n /** Bumped on stop(); segments captured under an older value are dropped so a\n * late transcript never leaks across sessions. */\n generation: number;\n /** Single-flight guard on start() — true between entry and completion so a\n * second start can't race the first before `listening` flips. */\n startInFlight: boolean;\n}\n\n// Tear down the browser-side resources. `state.setListening(false)` MUST happen\n// BEFORE `session.cut()` so the parent's onSegmentEnd callback sees\n// isListening()===false and does NOT restart. The monitor interval MUST clear\n// BEFORE `audioGraph.teardown()` so a scheduled tick can't run against a\n// half-closed AudioContext.\nfunction stopCapture(runtime: CaptureRuntime, state: CaptureStateController, session: RecorderSession): void {\n runtime.generation += 1;\n state.setListening(false);\n if (runtime.monitorHandle !== null) {\n window.clearInterval(runtime.monitorHandle);\n runtime.monitorHandle = null;\n }\n session.cut();\n runtime.audioGraph?.teardown();\n runtime.audioGraph = null;\n}\n\n// One VAD tick against the audio graph's current window. No-op when the graph\n// is not attached (defensive: `stopCapture` clears `monitorHandle` before\n// `audioGraph.teardown()`, so this is the belt-and-braces for any late fire).\nfunction monitorTick(runtime: CaptureRuntime, session: RecorderSession): void {\n if (!runtime.audioGraph?.isAttached()) return;\n const rms = computeRms(runtime.audioGraph.sample());\n const {\n hasSpeech,\n silenceStart: nextSilence,\n cut,\n } = evaluateVad({ hasSpeech: session.hadSpeech(), silenceStart: runtime.silenceStart }, runtime.segmentStart, rms, Date.now(), VAD_CONFIG);\n if (hasSpeech) session.markSpeech();\n runtime.silenceStart = nextSilence;\n if (cut) session.cut();\n}\n\nexport function createVoiceCapture(transport: VoiceCaptureTransport, language: () => string, callbacks: VoiceCaptureCallbacks): VoiceCapture {\n const state = createCaptureState(callbacks.onState);\n const poller = createAvailabilityPoller(transport, state.setAvailable);\n const runtime: CaptureRuntime = { ...INITIAL_RUNTIME };\n const segments = createSegmentQueue({ transport, language, callbacks, setPending: state.setPending, getGeneration: () => runtime.generation });\n\n const session = createRecorderSession({ onSegmentEnd: handleSegmentEnd });\n\n function handleSegmentEnd(blob: Blob, hadSpeech: boolean, gen: number): void {\n // Restart FIRST so audio-loss between segments is minimal; the snapshot\n // in blob/hadSpeech/gen is already independent of session state.\n if (state.isListening()) startRecorder();\n if (hadSpeech && blob.size > 0 && gen === runtime.generation) segments.enqueue(blob, gen);\n }\n\n function startRecorder(): void {\n const stream = runtime.audioGraph?.getStream();\n if (!stream) return;\n runtime.silenceStart = null;\n runtime.segmentStart = Date.now();\n session.startNext(stream, runtime.mimeType, runtime.generation);\n }\n\n async function start(): Promise<boolean> {\n if (runtime.startInFlight || state.isListening()) return true;\n runtime.startInFlight = true;\n const startGen = runtime.generation;\n try {\n const capture = await bootstrapCapture(callbacks, startGen, () => runtime.generation);\n if (!capture) return false;\n runtime.audioGraph = capture.audioGraph;\n runtime.mimeType = capture.mimeType;\n state.setListening(true);\n startRecorder();\n runtime.monitorHandle = window.setInterval(() => monitorTick(runtime, session), MONITOR_INTERVAL_MS);\n return true;\n } finally {\n runtime.startInFlight = false;\n }\n }\n\n function stop(): void {\n stopCapture(runtime, state, session);\n }\n\n function dispose(): void {\n poller.stop();\n stop();\n }\n\n return { refreshAvailability: poller.refresh, start, stop, dispose };\n}\n"],"mappings":";AAIA,SAAgB,WAAW,QAA8B;CACvD,IAAI,MAAM;CACV,KAAK,MAAM,UAAU,QAAQ,OAAO,SAAS;CAC7C,OAAO,KAAK,KAAK,MAAM,OAAO,MAAM;AACtC;;;AAIA,SAAgB,sBAAsB,UAA0B;CAC9D,OAAO,SAAS,MAAM,GAAG,CAAC,CAAC,MAAM;AACnC;AAmBA,SAAS,SAAS,KAAa,QAA4B;CACzD,OAAO,MAAM,OAAO;AACtB;AAEA,SAAS,iBAAiB,OAAiB,KAAa,OAAe,QAAkC;CACvG,IAAI,SAAS,KAAK,MAAM,GAAG,OAAO;CAClC,IAAI,MAAM,aAAa,MAAM,iBAAiB,MAAM,OAAO;CAC3D,OAAO,MAAM;AACf;AAEA,SAAS,gBAAgB,OAAiB,KAAa,OAAe,QAA4B;CAChG,IAAI,SAAS,KAAK,MAAM,KAAK,CAAC,MAAM,aAAa,MAAM,iBAAiB,MAAM,OAAO;CACrF,OAAO,QAAQ,MAAM,eAAe,OAAO;AAC7C;;;;AAKA,SAAgB,YAAY,OAAiB,gBAAwB,KAAa,OAAe,QAAgC;CAC/H,MAAM,YAAY,SAAS,KAAK,MAAM,KAAK,MAAM;CACjD,MAAM,eAAe,iBAAiB,OAAO,KAAK,OAAO,MAAM;CAC/D,MAAM,SAAS,aAAa,QAAQ,iBAAiB,OAAO;CAC5D,OAAO;EAAE;EAAW;EAAc,KAAK,gBAAgB,OAAO,KAAK,OAAO,MAAM,KAAK;CAAO;AAC9F;;;AC9CA,IAAM,oBAA4C;CAChD,IAA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,cAAc;GACd,QAAQ,gBAAgB,OAAO,kBAAkB,YAAY,SAAS,OAAO,GAAG,mBAAmB;GACnG,OAAO;EACT,UAAU;GACR,QAAQ,gBAAgB;EAC1B;CACF;CAEA,SAAS,OAAa;EACpB,YAAY,SAAS,OAAO,OAAO;CACrC;CAEA,SAAS,UAAgB;EACvB,OAAO,KAAK;EACZ,KAAK;CACP;CAEA,OAAO;EAAE,qBAAqB,OAAO;EAAS;EAAO;EAAM;CAAQ;AACrE"}
|
package/dist/whisper/index.cjs
CHANGED
|
@@ -87,63 +87,80 @@ function isModelReady(modelsDir, name) {
|
|
|
87
87
|
return false;
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
+
/** Report status precedence: an in-flight download wins, then an on-disk ready
|
|
91
|
+
* file, then the last transient state (idle if none). Pure so the precedence
|
|
92
|
+
* is unit-testable without touching the filesystem. */
|
|
93
|
+
function pickModelStatus(live, isReady) {
|
|
94
|
+
if (live?.state === "downloading") return live;
|
|
95
|
+
if (isReady) return { state: "ready" };
|
|
96
|
+
return live ?? { state: "idle" };
|
|
97
|
+
}
|
|
98
|
+
/** Parse a `Content-Length` header to a byte count; unknown / malformed → 0. */
|
|
99
|
+
function parseContentLength(header) {
|
|
100
|
+
return Number(header) || 0;
|
|
101
|
+
}
|
|
90
102
|
var DOWNLOAD_STALL_TIMEOUT_MS = ONE_MINUTE_MS;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
received += value.byteLength;
|
|
109
|
-
if (!fileStream.write(value)) await (0, node_events.once)(fileStream, "drain");
|
|
110
|
-
if (total > 0) downloadStatus.set(name, {
|
|
111
|
-
state: "downloading",
|
|
112
|
-
progress: received / total
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
fileStream.end();
|
|
116
|
-
await (0, node_events.once)(fileStream, "finish");
|
|
117
|
-
} catch (err) {
|
|
118
|
-
fileStream.destroy();
|
|
119
|
-
throw err;
|
|
103
|
+
/** Drain the response stream into the `.partial` file, resetting the stall
|
|
104
|
+
* timer on every chunk and publishing progress once the total size is known. */
|
|
105
|
+
async function streamToFile(body, partialPath, total, name, deps) {
|
|
106
|
+
const reader = body.getReader();
|
|
107
|
+
const fileStream = (0, node_fs.createWriteStream)(partialPath);
|
|
108
|
+
let received = 0;
|
|
109
|
+
try {
|
|
110
|
+
for (;;) {
|
|
111
|
+
const { done, value } = await reader.read();
|
|
112
|
+
if (done) break;
|
|
113
|
+
deps.onProgress();
|
|
114
|
+
received += value.byteLength;
|
|
115
|
+
if (!fileStream.write(value)) await (0, node_events.once)(fileStream, "drain");
|
|
116
|
+
if (total > 0) deps.downloadStatus.set(name, {
|
|
117
|
+
state: "downloading",
|
|
118
|
+
progress: received / total
|
|
119
|
+
});
|
|
120
120
|
}
|
|
121
|
+
fileStream.end();
|
|
122
|
+
await (0, node_events.once)(fileStream, "finish");
|
|
123
|
+
} catch (err) {
|
|
124
|
+
fileStream.on("error", () => {});
|
|
125
|
+
fileStream.destroy();
|
|
126
|
+
throw err;
|
|
121
127
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
128
|
+
}
|
|
129
|
+
/** Fetch a model into a `.partial` file, reject a truncated transfer via the
|
|
130
|
+
* size floor, then atomically rename it into place. */
|
|
131
|
+
async function downloadModel(name, modelsDir, downloadStatus) {
|
|
132
|
+
const spec = WHISPER_MODELS[name];
|
|
133
|
+
(0, node_fs.mkdirSync)(modelsDir, { recursive: true });
|
|
134
|
+
const dest = modelFilePath(modelsDir, name);
|
|
135
|
+
const partial = `${dest}.partial`;
|
|
136
|
+
const controller = new AbortController();
|
|
137
|
+
let stallTimer = null;
|
|
138
|
+
const resetStall = () => {
|
|
139
|
+
if (stallTimer) clearTimeout(stallTimer);
|
|
140
|
+
stallTimer = setTimeout(() => controller.abort(), DOWNLOAD_STALL_TIMEOUT_MS);
|
|
141
|
+
};
|
|
142
|
+
try {
|
|
143
|
+
const response = await fetch(spec.url, { signal: controller.signal });
|
|
144
|
+
if (!response.ok || !response.body) throw new Error(`download failed: HTTP ${response.status}`);
|
|
145
|
+
const total = parseContentLength(response.headers.get("content-length"));
|
|
146
|
+
resetStall();
|
|
147
|
+
await streamToFile(response.body, partial, total, name, {
|
|
148
|
+
downloadStatus,
|
|
149
|
+
onProgress: resetStall
|
|
150
|
+
});
|
|
151
|
+
if ((0, node_fs.statSync)(partial).size < spec.minBytes) {
|
|
152
|
+
(0, node_fs.unlinkSync)(partial);
|
|
153
|
+
throw new Error("downloaded file is smaller than expected — likely truncated");
|
|
146
154
|
}
|
|
155
|
+
(0, node_fs.renameSync)(partial, dest);
|
|
156
|
+
} finally {
|
|
157
|
+
if (stallTimer) clearTimeout(stallTimer);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function createModelDownloader(modelsDir, logger = NOOP_LOGGER) {
|
|
161
|
+
const downloadStatus = /* @__PURE__ */ new Map();
|
|
162
|
+
function getStatus(name) {
|
|
163
|
+
return pickModelStatus(downloadStatus.get(name), isModelReady(modelsDir, name));
|
|
147
164
|
}
|
|
148
165
|
async function ensure(name) {
|
|
149
166
|
if (isModelReady(modelsDir, name)) {
|
|
@@ -157,7 +174,7 @@ function createModelDownloader(modelsDir, logger = NOOP_LOGGER) {
|
|
|
157
174
|
});
|
|
158
175
|
logger.info("model download: start", { model: name });
|
|
159
176
|
try {
|
|
160
|
-
await
|
|
177
|
+
await downloadModel(name, modelsDir, downloadStatus);
|
|
161
178
|
downloadStatus.set(name, { state: "ready" });
|
|
162
179
|
logger.info("model download: ok", { model: name });
|
|
163
180
|
} catch (err) {
|
|
@@ -178,11 +195,38 @@ function createModelDownloader(modelsDir, logger = NOOP_LOGGER) {
|
|
|
178
195
|
};
|
|
179
196
|
}
|
|
180
197
|
//#endregion
|
|
198
|
+
//#region src/whisper/sidecar-helpers.ts
|
|
199
|
+
/** whisper-server CLI args to preload `modelPath` and bind to `host:port`. */
|
|
200
|
+
function buildServerArgs(modelPath, host, port) {
|
|
201
|
+
return [
|
|
202
|
+
"--model",
|
|
203
|
+
modelPath,
|
|
204
|
+
"--host",
|
|
205
|
+
host,
|
|
206
|
+
"--port",
|
|
207
|
+
String(port)
|
|
208
|
+
];
|
|
209
|
+
}
|
|
210
|
+
/** Append `chunk` to a bounded stderr tail, keeping only the last `maxChars`. */
|
|
211
|
+
function appendStderrTail(previous, chunk, maxChars) {
|
|
212
|
+
return (previous + chunk).slice(-maxChars);
|
|
213
|
+
}
|
|
214
|
+
/** Extract the transcript from a whisper-server `/inference` JSON response.
|
|
215
|
+
* Returns "" for any shape that lacks a string `text` field. */
|
|
216
|
+
function parseInferenceText(data) {
|
|
217
|
+
if (typeof data === "object" && data !== null && "text" in data) {
|
|
218
|
+
const { text } = data;
|
|
219
|
+
if (typeof text === "string") return text;
|
|
220
|
+
}
|
|
221
|
+
return "";
|
|
222
|
+
}
|
|
223
|
+
//#endregion
|
|
181
224
|
//#region src/whisper/sidecar.ts
|
|
182
225
|
var HOST = "127.0.0.1";
|
|
183
226
|
var READY_TIMEOUT_MS = 60 * ONE_SECOND_MS;
|
|
184
227
|
var READY_POLL_INTERVAL_MS = 500;
|
|
185
228
|
var INFERENCE_TIMEOUT_MS = 2 * ONE_MINUTE_MS;
|
|
229
|
+
var STDERR_TAIL_MAX_CHARS = 4e3;
|
|
186
230
|
async function findFreePort() {
|
|
187
231
|
return new Promise((resolve, reject) => {
|
|
188
232
|
const srv = (0, node_net.createServer)();
|
|
@@ -211,7 +255,7 @@ async function waitUntilReady(port) {
|
|
|
211
255
|
function drainStderr(proc, tail) {
|
|
212
256
|
proc.stderr?.setEncoding("utf8");
|
|
213
257
|
proc.stderr?.on("data", (chunk) => {
|
|
214
|
-
tail.text = (tail.text
|
|
258
|
+
tail.text = appendStderrTail(tail.text, chunk, STDERR_TAIL_MAX_CHARS);
|
|
215
259
|
});
|
|
216
260
|
}
|
|
217
261
|
function waitForReadyOrFailure(proc, port) {
|
|
@@ -241,76 +285,95 @@ function waitForReadyOrFailure(proc, port) {
|
|
|
241
285
|
});
|
|
242
286
|
});
|
|
243
287
|
}
|
|
244
|
-
function
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
288
|
+
async function postInference(port, wavPath, language) {
|
|
289
|
+
const buf = await (0, node_fs_promises.readFile)(wavPath);
|
|
290
|
+
const form = new FormData();
|
|
291
|
+
form.append("file", new Blob([buf], { type: "audio/wav" }), "audio.wav");
|
|
292
|
+
form.append("response_format", "json");
|
|
293
|
+
form.append("language", language || "auto");
|
|
294
|
+
let res;
|
|
295
|
+
try {
|
|
296
|
+
res = await fetch(`http://${HOST}:${port}/inference`, {
|
|
297
|
+
method: "POST",
|
|
298
|
+
body: form,
|
|
299
|
+
signal: AbortSignal.timeout(INFERENCE_TIMEOUT_MS)
|
|
300
|
+
});
|
|
301
|
+
} catch (err) {
|
|
302
|
+
throw new Error(`whisper-server request failed: ${errorMessage(err)}`);
|
|
248
303
|
}
|
|
249
|
-
|
|
304
|
+
if (!res.ok) throw new Error(`whisper-server returned HTTP ${res.status}`);
|
|
305
|
+
return parseInferenceText(await res.json());
|
|
250
306
|
}
|
|
251
|
-
function
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
let startingProc = null;
|
|
255
|
-
let startToken = 0;
|
|
256
|
-
function shutdown() {
|
|
257
|
-
startToken += 1;
|
|
258
|
-
if (startingProc) {
|
|
259
|
-
startingProc.kill();
|
|
260
|
-
startingProc = null;
|
|
261
|
-
}
|
|
262
|
-
if (sidecar) {
|
|
263
|
-
sidecar.proc.kill();
|
|
264
|
-
sidecar = null;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
async function startSidecar(model) {
|
|
268
|
-
const token = ++startToken;
|
|
269
|
-
const port = await findFreePort();
|
|
270
|
-
const args = [
|
|
271
|
-
"--model",
|
|
272
|
-
modelFilePath(modelsDir, model),
|
|
273
|
-
"--host",
|
|
274
|
-
HOST,
|
|
275
|
-
"--port",
|
|
276
|
-
String(port)
|
|
277
|
-
];
|
|
307
|
+
function defaultSpawnServer(modelsDir, serverBinary, logger) {
|
|
308
|
+
return (model, port) => {
|
|
309
|
+
const args = buildServerArgs(modelFilePath(modelsDir, model), HOST, port);
|
|
278
310
|
logger.info("sidecar: spawning", {
|
|
279
311
|
model,
|
|
280
312
|
port
|
|
281
313
|
});
|
|
282
|
-
|
|
314
|
+
return (0, node_child_process.spawn)(serverBinary, args, { stdio: [
|
|
283
315
|
"ignore",
|
|
284
316
|
"ignore",
|
|
285
317
|
"pipe"
|
|
286
318
|
] });
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function instrumentChildProcess(proc, model, logger, onExit) {
|
|
322
|
+
const stderrTail = { text: "" };
|
|
323
|
+
drainStderr(proc, stderrTail);
|
|
324
|
+
proc.on("error", (err) => logger.warn("sidecar: process error", {
|
|
325
|
+
model,
|
|
326
|
+
error: errorMessage(err)
|
|
327
|
+
}));
|
|
328
|
+
proc.on("exit", (code) => {
|
|
329
|
+
logger.warn("sidecar: exited", {
|
|
291
330
|
model,
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
proc.on("exit", (code) => {
|
|
295
|
-
logger.warn("sidecar: exited", {
|
|
296
|
-
model,
|
|
297
|
-
code,
|
|
298
|
-
stderrTail: stderrTail.text.slice(-500)
|
|
299
|
-
});
|
|
300
|
-
if (sidecar?.proc === proc) sidecar = null;
|
|
331
|
+
code,
|
|
332
|
+
stderrTail: stderrTail.text.slice(-500)
|
|
301
333
|
});
|
|
334
|
+
onExit(proc);
|
|
335
|
+
});
|
|
336
|
+
return stderrTail;
|
|
337
|
+
}
|
|
338
|
+
async function awaitReadyOrThrow(proc, port, stderrTail, waitReady) {
|
|
339
|
+
try {
|
|
340
|
+
await waitReady(proc, port);
|
|
341
|
+
} catch (err) {
|
|
342
|
+
proc.kill();
|
|
343
|
+
throw new Error(`whisper-server failed to start: ${errorMessage(err)} — stderr: ${stderrTail.text.slice(-500)}`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
function throwIfStartCancelled(startedToken, currentToken, proc) {
|
|
347
|
+
if (startedToken !== currentToken) {
|
|
348
|
+
proc.kill();
|
|
349
|
+
throw new Error("whisper-server start cancelled");
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
function createStartLifecycle(deps) {
|
|
353
|
+
const { allocatePort, spawnServer, waitReady, logger } = deps;
|
|
354
|
+
let sidecar = null;
|
|
355
|
+
let starting = null;
|
|
356
|
+
let startingProc = null;
|
|
357
|
+
let startToken = 0;
|
|
358
|
+
function shutdown() {
|
|
359
|
+
startToken += 1;
|
|
360
|
+
startingProc?.kill();
|
|
361
|
+
startingProc = null;
|
|
362
|
+
sidecar?.proc.kill();
|
|
363
|
+
sidecar = null;
|
|
364
|
+
}
|
|
365
|
+
async function startSidecar(model) {
|
|
366
|
+
const token = ++startToken;
|
|
367
|
+
const port = await allocatePort();
|
|
368
|
+
const proc = spawnServer(model, port);
|
|
369
|
+
startingProc = proc;
|
|
370
|
+
const stderrTail = instrumentChildProcess(proc, model, logger, (exited) => sidecar?.proc === exited && (sidecar = null));
|
|
302
371
|
try {
|
|
303
|
-
await
|
|
304
|
-
} catch (err) {
|
|
305
|
-
proc.kill();
|
|
306
|
-
throw new Error(`whisper-server failed to start: ${errorMessage(err)} — stderr: ${stderrTail.text.slice(-500)}`);
|
|
372
|
+
await awaitReadyOrThrow(proc, port, stderrTail, waitReady);
|
|
307
373
|
} finally {
|
|
308
374
|
if (startingProc === proc) startingProc = null;
|
|
309
375
|
}
|
|
310
|
-
|
|
311
|
-
proc.kill();
|
|
312
|
-
throw new Error("whisper-server start cancelled");
|
|
313
|
-
}
|
|
376
|
+
throwIfStartCancelled(token, startToken, proc);
|
|
314
377
|
sidecar = {
|
|
315
378
|
port,
|
|
316
379
|
proc,
|
|
@@ -323,9 +386,7 @@ function createSidecar(modelsDir, serverBinary = "whisper-server", logger = NOOP
|
|
|
323
386
|
return sidecar;
|
|
324
387
|
}
|
|
325
388
|
function beginStart(model) {
|
|
326
|
-
const promise = startSidecar(model).finally(() =>
|
|
327
|
-
starting = null;
|
|
328
|
-
});
|
|
389
|
+
const promise = startSidecar(model).finally(() => starting = null);
|
|
329
390
|
starting = {
|
|
330
391
|
model,
|
|
331
392
|
promise
|
|
@@ -344,9 +405,21 @@ function createSidecar(modelsDir, serverBinary = "whisper-server", logger = NOOP
|
|
|
344
405
|
return beginStart(model);
|
|
345
406
|
}
|
|
346
407
|
}
|
|
408
|
+
return {
|
|
409
|
+
ensureSidecar,
|
|
410
|
+
shutdown
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
function createSidecar(modelsDir, serverBinary = "whisper-server", logger = NOOP_LOGGER) {
|
|
414
|
+
const lifecycle = createStartLifecycle({
|
|
415
|
+
allocatePort: findFreePort,
|
|
416
|
+
spawnServer: defaultSpawnServer(modelsDir, serverBinary, logger),
|
|
417
|
+
waitReady: waitForReadyOrFailure,
|
|
418
|
+
logger
|
|
419
|
+
});
|
|
347
420
|
async function warmup(model) {
|
|
348
421
|
try {
|
|
349
|
-
await ensureSidecar(model);
|
|
422
|
+
await lifecycle.ensureSidecar(model);
|
|
350
423
|
} catch (err) {
|
|
351
424
|
logger.warn("sidecar: warmup failed", {
|
|
352
425
|
model,
|
|
@@ -355,29 +428,12 @@ function createSidecar(modelsDir, serverBinary = "whisper-server", logger = NOOP
|
|
|
355
428
|
}
|
|
356
429
|
}
|
|
357
430
|
async function transcribeWav(wavPath, language, model) {
|
|
358
|
-
|
|
359
|
-
const buf = await (0, node_fs_promises.readFile)(wavPath);
|
|
360
|
-
const form = new FormData();
|
|
361
|
-
form.append("file", new Blob([buf], { type: "audio/wav" }), "audio.wav");
|
|
362
|
-
form.append("response_format", "json");
|
|
363
|
-
form.append("language", language || "auto");
|
|
364
|
-
let res;
|
|
365
|
-
try {
|
|
366
|
-
res = await fetch(`http://${HOST}:${active.port}/inference`, {
|
|
367
|
-
method: "POST",
|
|
368
|
-
body: form,
|
|
369
|
-
signal: AbortSignal.timeout(INFERENCE_TIMEOUT_MS)
|
|
370
|
-
});
|
|
371
|
-
} catch (err) {
|
|
372
|
-
throw new Error(`whisper-server request failed: ${errorMessage(err)}`);
|
|
373
|
-
}
|
|
374
|
-
if (!res.ok) throw new Error(`whisper-server returned HTTP ${res.status}`);
|
|
375
|
-
return parseInferenceText(await res.json());
|
|
431
|
+
return postInference((await lifecycle.ensureSidecar(model)).port, wavPath, language);
|
|
376
432
|
}
|
|
377
433
|
return {
|
|
378
434
|
transcribeWav,
|
|
379
435
|
warmup,
|
|
380
|
-
shutdown
|
|
436
|
+
shutdown: lifecycle.shutdown
|
|
381
437
|
};
|
|
382
438
|
}
|
|
383
439
|
//#endregion
|