Track Piper model bytes in the worker
Build and publish frontend / publish (push) Successful in 47s

This commit is contained in:
Ein Anderssono
2026-10-16 08:02:17 +02:00
parent ab574acd39
commit 7f77b904ac
6 changed files with 95 additions and 8 deletions
+15 -5
View File
@@ -1,4 +1,5 @@
import {TtsSession} from '@mintplex-labs/piper-tts-web';
import {trackModelDownload} from './piper-download-progress.mjs';
import {localPiperModelUrl} from './piper-model-routing.mjs';
import {createOnnxWasmPaths} from './piper-onnx-config.mjs';
@@ -8,22 +9,27 @@ const localFetch = self.fetch.bind(self);
// Keep the TTS library's OPFS cache logic, but serve models/config from the
// same-origin static asset service instead of its built-in external model URL.
self.fetch = (resource, options) => {
self.fetch = async (resource, options) => {
const requestedUrl = resource instanceof Request ? resource.url : String(resource);
const localModelUrl = localPiperModelUrl(requestedUrl, self.location.origin);
if (!localModelUrl) return localFetch(resource, options);
const redirected = resource instanceof Request ? new Request(localModelUrl, resource) : localModelUrl;
return localFetch(redirected, options);
const response = await localFetch(redirected, options);
if (!activeRequestId || !localModelUrl.endsWith('.onnx')) return response;
return trackModelDownload(response, {
id: activeRequestId,
postMessage: message => self.postMessage(message),
});
};
let sessionPromise;
let inferenceQueue = Promise.resolve();
let activeRequestId = null;
async function getSession(onProgress) {
async function getSession() {
if (!sessionPromise) {
sessionPromise = TtsSession.create({
voiceId: VOICE_ID,
progress: onProgress,
wasmPaths: {
onnxWasm: createOnnxWasmPaths(self.location.origin),
piperData: `${ASSET_ROOT}/piper/piper_phonemize.data`,
@@ -40,13 +46,17 @@ async function getSession(onProgress) {
async function synthesizeMessage(event) {
const {id, text} = event.data || {};
if (!Number.isSafeInteger(id) || typeof text !== 'string') return;
activeRequestId = id;
self.postMessage({id, type: 'progress', progress: {stage: 'starting'}});
try {
const session = await getSession(progress => self.postMessage({id, type: 'progress', progress}));
const session = await getSession();
const blob = await session.predict(text);
const audio = await blob.arrayBuffer();
self.postMessage({id, type: 'audio', audio}, [audio]);
} catch (error) {
self.postMessage({id, type: 'error', error: String(error?.message || error)});
} finally {
activeRequestId = null;
}
}