Worker pipeline and message protocol for local engines #9

Closed
opened 2026-10-25 14:58:13 +00:00 by dsh · 1 comment
Owner

Goal

Keep WASM/ONNX and model bytes off the UI thread. The Worker boundary every local engine shares.

Scope

  • Worker creation strategy that works with the /plugins route (it serves only client*.js chunks): Blob URL from an inline source string, or a pinned CDN module — decide and document. No arbitrary static route until the asset ADR (#18).
  • Protocol: { id, type: 'init' | 'synthesize' | 'cancel' | 'dispose', payload }; responses progress / result / error; correlate by id; reject duplicate ids.
  • Transferables: Float32Array PCM out, model bytes in; never structured-clone big buffers.
  • Progress events (download %, init, synth) forwarded to the settings UI; cancellation aborts the in-flight job without killing the worker.
  • Worker crash → typed error + one automatic restart; no infinite respawn loop.
  • Keep the worker engine-agnostic: it loads an engine module by id, so Piper/Kokoro plug in without protocol changes.
  • Unit-test the protocol state machine with a fake worker (postMessage/onmessage stub).

Acceptance

  • Synthesizing a 500-char sentence does not block the UI thread (verify with a 60 FPS rAF counter during synthesis).
  • Cancel mid-synthesis returns control in < 100 ms and leaves the worker reusable.

Notes

  • Depends on #8 (contract). Part of M1, reused by #10 and #16.
## Goal Keep WASM/ONNX and model bytes off the UI thread. The Worker boundary every local engine shares. ## Scope - [ ] Worker creation strategy that works with the `/plugins` route (it serves only `client*.js` chunks): Blob URL from an inline source string, or a pinned CDN module — decide and document. No arbitrary static route until the asset ADR (#18). - [ ] Protocol: `{ id, type: 'init' | 'synthesize' | 'cancel' | 'dispose', payload }`; responses `progress` / `result` / `error`; correlate by `id`; reject duplicate ids. - [ ] Transferables: `Float32Array` PCM out, model bytes in; never structured-clone big buffers. - [ ] Progress events (download %, init, synth) forwarded to the settings UI; cancellation aborts the in-flight job without killing the worker. - [ ] Worker crash → typed error + one automatic restart; no infinite respawn loop. - [ ] Keep the worker engine-agnostic: it loads an engine module by id, so Piper/Kokoro plug in without protocol changes. - [ ] Unit-test the protocol state machine with a fake worker (`postMessage`/`onmessage` stub). ## Acceptance - Synthesizing a 500-char sentence does not block the UI thread (verify with a 60 FPS rAF counter during synthesis). - Cancel mid-synthesis returns control in < 100 ms and leaves the worker reusable. ## Notes - Depends on #8 (contract). Part of M1, reused by #10 and #16.
Author
Owner

Done in f586bbc. The worker is built separately from src/worker/entry.js and embedded as a source string (the /plugins route still serves only client*.js): one Blob worker, request/response ids, progress forwarding, AbortSignal cancel (best-effort), crash → reject + respawn, dispose. WAV→PCM decoder lives in src/audio/wav.js so the adapter never needs AudioContext in the worker. 6 worker-client tests + 4 WAV tests; 110 total. Next: browser proof in #11.

**Done** in `f586bbc`. The worker is built separately from `src/worker/entry.js` and embedded as a source string (the `/plugins` route still serves only `client*.js`): one Blob worker, request/response ids, progress forwarding, AbortSignal cancel (best-effort), crash → reject + respawn, dispose. WAV→PCM decoder lives in `src/audio/wav.js` so the adapter never needs `AudioContext` in the worker. 6 worker-client tests + 4 WAV tests; 110 total. Next: browser proof in #11.
dsh closed this issue 2026-10-25 20:17:23 +00:00
Sign in to join this conversation.
No description provided.