Agent completed work jubii

This commit is contained in:
Lucas Winther
2026-07-09 08:07:32 +02:00
parent 6466d3a45c
commit eb72a2e646
29 changed files with 2051 additions and 0 deletions
+142
View File
@@ -0,0 +1,142 @@
/**
* Inference Web Worker (Decisions.md D-003 — inference never runs on the main
* thread). Hosts the Transformers.js `automatic-speech-recognition` pipeline.
*
* M1: hardcoded-model, WASM-only. WebGPU detection + fallback lands in M2
* (D-004); model switching/disposal also lands in M2.
*/
import {
pipeline,
WhisperTextStreamer,
type AutomaticSpeechRecognitionPipeline,
type ProgressInfo,
} from "@huggingface/transformers";
import {
isUiToWorkerMessage,
type LoadMessage,
type ResultMessage,
type TranscriptChunk,
type WorkerToUiMessage,
} from "./protocol";
// `lib: ["DOM"]` types `self` as Window; give it the worker-scope shape we use.
interface DedicatedWorkerScope {
postMessage(message: WorkerToUiMessage): void;
addEventListener(
type: "message",
listener: (event: MessageEvent<unknown>) => void,
): void;
}
const scope = self as unknown as DedicatedWorkerScope;
function post(message: WorkerToUiMessage): void {
scope.postMessage(message);
}
function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
/** Forward Transformers.js progress events to the UI as load-progress messages. */
function forwardProgress(info: ProgressInfo): void {
switch (info.status) {
case "initiate":
case "download":
case "done":
post({ type: "load-progress", status: info.status, file: info.file });
break;
case "progress":
post({
type: "load-progress",
status: "progress",
file: info.file,
loaded: info.loaded,
total: info.total,
progress: info.progress,
});
break;
case "ready":
post({ type: "load-progress", status: "ready" });
break;
default:
// e.g. "progress_total" — the UI derives totals per-file itself.
break;
}
}
let asrPromise: Promise<AutomaticSpeechRecognitionPipeline> | null = null;
function load(message: LoadMessage): Promise<AutomaticSpeechRecognitionPipeline> {
// Idempotent for M1's single hardcoded model: repeated "load" messages
// reuse the in-flight/completed pipeline.
asrPromise ??= pipeline("automatic-speech-recognition", message.modelId, {
device: message.device,
progress_callback: forwardProgress,
});
return asrPromise;
}
async function transcribe(audio: Float32Array): Promise<void> {
if (asrPromise === null) {
post({
type: "error",
stage: "transcribe",
message: "No model loaded — send a \"load\" message first.",
});
return;
}
const asr = await asrPromise;
const streamer = new WhisperTextStreamer(
// The ASR pipeline for Whisper models always carries a WhisperTokenizer.
asr.tokenizer as ConstructorParameters<typeof WhisperTextStreamer>[0],
{
skip_prompt: true,
callback_function: (text: string) => post({ type: "partial", text }),
},
);
const output = await asr(audio, {
chunk_length_s: 30,
stride_length_s: 5,
return_timestamps: true,
streamer,
});
const single = (Array.isArray(output) ? output[0] : output) as {
text?: string;
chunks?: TranscriptChunk[];
};
const result: ResultMessage = {
type: "result",
text: (single.text ?? "").trim(),
chunks: single.chunks ?? [],
};
post(result);
}
scope.addEventListener("message", (event) => {
const data = event.data;
if (!isUiToWorkerMessage(data)) {
post({
type: "error",
stage: "transcribe",
message: "Malformed message sent to inference worker.",
});
return;
}
switch (data.type) {
case "load":
load(data).catch((error: unknown) => {
asrPromise = null; // allow retry after a failed download
post({ type: "error", stage: "load", message: errorMessage(error) });
});
break;
case "transcribe":
transcribe(data.audio).catch((error: unknown) => {
post({ type: "error", stage: "transcribe", message: errorMessage(error) });
});
break;
}
});