@streamoji/aitwin 0.1.6 → 0.1.7

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.
@@ -0,0 +1 @@
1
+ (function(){"use strict";function j(e,t,n){return{width:e.crop?.source_width??t,height:e.crop?.source_height??n}}function Q(e,t){const n=e.crop;return{x:t?.source_x??n?.x??0,y:t?.source_y??n?.y??0,width:t?.source_w??n?.width??e.frame_width??0,height:t?.source_h??n?.height??e.frame_height??0}}function X(e){return e?(e.w??0)>0&&(e.h??0)>0:!1}function Y(e){const t=e.trim();if(t.length%2!==0)throw new Error("Invalid hex key");const n=new Uint8Array(t.length/2);for(let r=0;r<t.length;r+=2)n[r/2]=Number.parseInt(t.slice(r,r+2),16);return n}function I(e){return Uint8Array.from(e)}function Z(e,t){const n=new Uint8Array(e.length+t.length);return n.set(e,0),n.set(t,e.length),n}async function V(e,t){if(!t)throw new Error("Asset key is required for encrypted assets");if(e.byteLength<28)throw new Error("Encrypted payload too small");const n=new Uint8Array(e),r=I(n.subarray(0,12)),s=I(n.subarray(12,28)),a=I(n.subarray(28)),o=I(Z(a,s)),_=await crypto.subtle.importKey("raw",Y(t),{name:"AES-GCM"},!1,["decrypt"]);return crypto.subtle.decrypt({name:"AES-GCM",iv:r,tagLength:128},_,o)}async function M(e,t){const n=await fetch(e);if(!n.ok)throw new Error(`Failed to load encrypted asset: ${e}`);const r=await n.arrayBuffer();return V(r,t)}async function tt(e,t){if(!t)throw new Error("Missing asset key for encrypted JSON");const n=await M(e,t),r=new TextDecoder().decode(n);return JSON.parse(r)}async function et(e){const t=new Blob([e],{type:"image/webp"});return createImageBitmap(t)}async function nt(e){const t=new Blob([e],{type:"image/png"});return createImageBitmap(t)}const p=15,C=["t01_0.17","t02_0.33","t03_0.50","t04_0.67","t05_0.83"],rt=C.length,H=new Set(["aa__kk","aa__nn","aa__sil","CH__aa","CH__DD","CH__E","CH__FF","CH__I","CH__kk","CH__nn","CH__O","CH__PP","CH__RR","CH__sil","CH__SS","CH__TH","CH__U","DD__aa","DD__E","DD__FF","DD__I","DD__kk","DD__nn","DD__O","DD__PP","DD__RR","DD__sil","DD__SS","DD__TH","DD__U","E__aa","E__FF","E__I","E__kk","E__nn","E__O","E__PP","E__RR","E__sil","E__SS","E__TH","E__U","FF__aa","FF__I","FF__kk","FF__nn","FF__O","FF__PP","FF__RR","FF__sil","FF__SS","FF__TH","FF__U","I__aa","I__kk","I__nn","I__O","I__PP","I__RR","I__sil","I__SS","I__TH","I__U","kk__nn","kk__sil","nn__sil","O__aa","O__kk","O__nn","O__PP","O__RR","O__sil","O__SS","O__TH","O__U","PP__aa","PP__kk","PP__nn","PP__RR","PP__sil","PP__SS","PP__TH","PP__U","RR__aa","RR__kk","RR__nn","RR__sil","RR__SS","RR__TH","RR__U","SS__aa","SS__kk","SS__nn","SS__sil","SS__TH","SS__U","TH__aa","TH__kk","TH__nn","TH__sil","TH__U","U__aa","U__kk","U__nn","U__sil"]);function v(e,t){return`${e}__${t}`}function U(e,t){return`pairs/${e}/${t}_minus_sil.png`}function q(e){return`${e}_minus_sil.png`}function st(e,t){if(e===t)return[];const n=[],r=v(e,t);if(H.has(r)){for(const a of C)n.push(U(r,a));return n}const s=v(t,e);if(H.has(s)){for(let a=rt-1;a>=0;a--)n.push(U(s,C[a]));return n}return[]}function at(e){return e<16?[]:e<35?[2]:e<51?[0,4]:e<67?[0,2,4]:e<83?[0,1,2,3]:[0,1,2,3,4]}function ot(e,t){return at(t).map(r=>e[r]).filter(r=>r!=null)}function it(e,t,n){if(!n)return`${e}/${t}`;const r=t.split("/").pop()??t,s=r.includes(".")?r.slice(0,r.lastIndexOf(".")):r;return`${e}/${s}.bin`}function ct(e,t,n,r,s=p){const a=new OffscreenCanvas(n,r),o=a.getContext("2d",{willReadFrequently:!0});o.drawImage(e,0,0,n,r);const _=o.getImageData(0,0,n,r),i=o.createImageData(n,r);i.data.set(_.data);const f=new OffscreenCanvas(n,r).getContext("2d",{willReadFrequently:!0});for(const D of t){f.clearRect(0,0,n,r),f.drawImage(D,0,0,n,r);const d=f.getImageData(0,0,n,r);for(let c=0;c<i.data.length;c+=4){const E=d.data[c],O=d.data[c+1],G=d.data[c+2];Math.max(E,O,G)>s&&(i.data[c]=E,i.data[c+1]=O,i.data[c+2]=G,i.data[c+3]=255)}}return o.putImageData(i,0,0),a}const N=-1;function _t(e){return e<0}let S=0,L=Promise.resolve();function b(e,t){const n=t>0?-t:N,r={type:"renderDone",requestId:e,generation:n};self.postMessage(r)}async function lt(e){const t=typeof e.liveSeq=="number"?e.liveSeq:0;if(t>0&&(S=t),!u)throw new Error("Worker not initialized");if(t>0&&t!==S){b(e.requestId,t);return}if(await m(w??void 0),t>0&&t!==S){b(e.requestId,t);return}if(await J(u,e.diffPath,e.expressionDiffPath??null,e.threshold??p),t>0&&t!==S){b(e.requestId,t);return}const n=t>0?-t:N;await B(n),b(e.requestId,t)}let u=null,y=null,w=null,x=null,F="sil";const k=new Map;function g(){if(!x)throw new Error("Worker assets not configured");return x}function W(){const e=g();return e.encrypted?`${e.binBase}/sil.bin`:`${e.twinBase}/sil.png`}function ft(){const e=g();return e.encrypted?`${e.binBase}/atlas.bin`:`${e.twinBase}/atlas.json`}function ut(){const e=g();return e.encrypted?`${e.binBase}/expression_atlas.bin`:`${e.twinBase}/expression_atlas.json`}function dt(){const e=g();return e.encrypted?e.binBase:e.twinBase}function R(){return g().encrypted}async function $(e){const t=await fetch(e);if(!t.ok)throw new Error(`Failed to load image: ${e}`);const n=await t.blob();return createImageBitmap(n)}async function z(e,t,n){const r=R(),s=r?await tt(e,n):await fetch(e).then(l=>{if(!l.ok)throw new Error(`Failed to load ${e}`);return l.json()}),a=s.sheets?.[0];if(!a?.path)throw new Error(`${e} has no sheets[0].path`);const o=r?await et(await M(it(t,a.path,r),n??"")):await $(`${t}/${a.path}`),_=new Map;for(const l of s.cells??[])l.path&&_.set(l.path,l);const i=new Map;for(const l of s.sheets??[])i.set(l.index,l);return{atlas:o,atlasMeta:s,cellByPath:_,sheetByIndex:i,diffBase:t}}async function m(e){if(R()&&!e)throw new Error("Encrypted assets enabled but no key provided");if(y&&(!R()||w===(e??null)))return y;w=e??null,k.clear();const t=dt();return y=(async()=>{const n=R()?await nt(await M(W(),e??"")):await $(W()),r=await z(ft(),t,e);let s=null;try{s=await z(ut(),t,e)}catch{s=null}return{sil:n,viseme:r,expression:s}})(),y}function A(e){return j(e.viseme.atlasMeta,e.sil.width,e.sil.height)}function wt(e,t){if(!e.sheetByIndex.get(t.sheet??0))throw new Error(`Unknown sheet index: ${t.sheet}`);const r=new OffscreenCanvas(t.w,t.h);return r.getContext("2d",{willReadFrequently:!0}).drawImage(e.atlas,t.x,t.y,t.w,t.h,0,0,t.w,t.h),r}async function ht(e){return createImageBitmap(e)}async function K(e,t,n){const r=`${t.diffBase}::${n}`,s=k.get(r);if(s)return s;const a=(async()=>{const o=t.cellByPath.get(n);if(!o)throw new Error(`No atlas cell for path: ${n}`);const _=Q(t.atlasMeta,o),i=A(e);if(X(o)){const D=wt(t,o),d=new OffscreenCanvas(i.width,i.height),c=d.getContext("2d");return c.fillStyle="#000",c.fillRect(0,0,i.width,i.height),c.drawImage(D,_.x,_.y),ht(d)}const l=`${t.diffBase}/${n}`,f=await $(l);if(f.width===i.width&&f.height===i.height)return f;throw f.close(),new Error(`Diff PNG wrong size for ${n}`)})();return k.set(r,a),a}function P(e,t){const n={type:"status",label:e,generation:t};self.postMessage(n)}function pt(e,t){const n={type:"error",requestId:e,message:t};self.postMessage(n)}async function B(e){if(!u)return;const t=_t(e);if(!t&&e!==h)return;const n=await createImageBitmap(u);if(!t&&e!==h){n.close();return}const r={type:"frame",bitmap:n,generation:e};self.postMessage(r,{transfer:[n]})}function yt(e,t,n){return new Promise((r,s)=>{setTimeout(()=>{t!==n?s(new DOMException("Aborted","AbortError")):r()},e)})}async function J(e,t,n,r=p){const s=await m(w??void 0),{width:a,height:o}=A(s);(e.width!==a||e.height!==o)&&(e.width=a,e.height=o);const _=[];if(t){if(!s.viseme.cellByPath.get(t))throw new Error(`No viseme atlas cell for path: ${t}`);_.push(await K(s,s.viseme,t))}if(n&&s.expression){if(!s.expression.cellByPath.get(n))throw new Error(`No expression atlas cell for path: ${n}`);_.push(await K(s,s.expression,n))}const i=e.getContext("2d");if(i.clearRect(0,0,a,o),_.length===0){i.drawImage(s.sil,0,0,a,o);return}const l=ct(s.sil,_,a,o,r);i.drawImage(l,0,0)}async function T(e,t,n,r,s=p){await J(e,t,n,s),await B(r)}async function gt(e,t,n){F="sil";const r=await m(w??void 0);if(t!==n)throw new DOMException("Aborted","AbortError");const{width:s,height:a}=A(r);e.width=s,e.height=a;const o=e.getContext("2d");o.clearRect(0,0,s,a),o.drawImage(r.sil,0,0,s,a),await B(t)}async function mt(e,t,n,r,s={}){const a=s.from??F,o=s.transitionDurationMs??400,_=s.gapMs??o,i=s.threshold??p;if(await m(w??void 0),a===t){if(P(t,n),await T(e,q(t),null,n,i),n!==r)throw new DOMException("Aborted","AbortError");F=t;return}const l=st(a,t),f=ot(l,_),D=q(t),d=f.length>0?o/f.length:0;f.length===0&&P(`${a} → ${t} (no pair; final only)`,n);for(let c=0;c<f.length;c++){if(n!==r)throw new DOMException("Aborted","AbortError");const E=f[c],O=E.split("/").pop()?.replace("_minus_sil.png","")??`frame ${c}`;if(P(`${a} → ${t} (${O})`,n),await T(e,E,null,n,i),n!==r)throw new DOMException("Aborted","AbortError");d>0&&await yt(d,n,r)}if(n!==r)throw new DOMException("Aborted","AbortError");if(P(t,n),await T(e,D,null,n,i),n!==r)throw new DOMException("Aborted","AbortError");F=t}let h=0;self.onmessage=e=>{const t=e.data;(async()=>{try{switch(t.type){case"init":{u=new OffscreenCanvas(1,1);const n={type:"ready",requestId:t.requestId};self.postMessage(n);break}case"loadAssets":{x=t.urls,y=null,w=null,k.clear();const n=await m(t.keyHex),{width:r,height:s}=A(n),a={type:"loadAssetsDone",requestId:t.requestId,width:r,height:s};self.postMessage(a);break}case"drawFrame":{await new Promise((n,r)=>{L=L.then(()=>lt(t)).then(n).catch(r)});break}case"renderSil":{if(h=t.generation,!u)throw new Error("Worker not initialized");await gt(u,t.generation,h);const n={type:"renderDone",requestId:t.requestId,generation:t.generation};self.postMessage(n);break}case"renderViseme":{if(h=t.generation,!u)throw new Error("Worker not initialized");await mt(u,t.to,t.generation,h,{from:t.from,transitionDurationMs:t.transitionDurationMs,gapMs:t.gapMs,threshold:t.threshold});const n={type:"renderDone",requestId:t.requestId,generation:t.generation};self.postMessage(n);break}default:break}}catch(n){if(n instanceof DOMException&&n.name==="AbortError"){const r={type:"renderAborted",requestId:t.requestId,generation:"generation"in t?t.generation:h};self.postMessage(r);return}pt(t.requestId,n instanceof Error?n.message:String(n))}})()}})();
package/dist/index.d.ts CHANGED
@@ -138,9 +138,9 @@ export declare type TtsEngineId = typeof TTS_ENGINE_GOOGLE | typeof TTS_ENGINE_I
138
138
 
139
139
  /** Runtime face bundle URLs (R2 custom-faces or local /twins/…). */
140
140
  export declare type TwinAssetUrls = {
141
- /** sil.png, idle.mp4 */
141
+ /** Plain assets: sil.png, idle.mp4, atlas.json */
142
142
  twinBase: string;
143
- /** atlas.bin and encrypted sheets */
143
+ /** Encrypted bundle: sil.bin, idle.bin, atlas.bin, sheets */
144
144
  binBase: string;
145
145
  encrypted: boolean;
146
146
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamoji/aitwin",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Embeddable React AI twin face with TTS lipsync",
5
5
  "type": "module",
6
6
  "main": "./dist/aitwin.cjs",
@@ -1 +0,0 @@
1
- (function(){"use strict";function G(e,t,n){return{width:e.crop?.source_width??t,height:e.crop?.source_height??n}}function j(e,t){const n=e.crop;return{x:t?.source_x??n?.x??0,y:t?.source_y??n?.y??0,width:t?.source_w??n?.width??e.frame_width??0,height:t?.source_h??n?.height??e.frame_height??0}}function Q(e){return e?(e.w??0)>0&&(e.h??0)>0:!1}function X(e){const t=e.trim();if(t.length%2!==0)throw new Error("Invalid hex key");const n=new Uint8Array(t.length/2);for(let r=0;r<t.length;r+=2)n[r/2]=Number.parseInt(t.slice(r,r+2),16);return n}function I(e){return Uint8Array.from(e)}function Y(e,t){const n=new Uint8Array(e.length+t.length);return n.set(e,0),n.set(t,e.length),n}async function Z(e,t){if(!t)throw new Error("Asset key is required for encrypted assets");if(e.byteLength<28)throw new Error("Encrypted payload too small");const n=new Uint8Array(e),r=I(n.subarray(0,12)),s=I(n.subarray(12,28)),a=I(n.subarray(28)),o=I(Y(a,s)),_=await crypto.subtle.importKey("raw",X(t),{name:"AES-GCM"},!1,["decrypt"]);return crypto.subtle.decrypt({name:"AES-GCM",iv:r,tagLength:128},_,o)}async function B(e,t){const n=await fetch(e);if(!n.ok)throw new Error(`Failed to load encrypted asset: ${e}`);const r=await n.arrayBuffer();return Z(r,t)}async function V(e,t){if(!t)throw new Error("Missing asset key for encrypted JSON");const n=await B(e,t),r=new TextDecoder().decode(n);return JSON.parse(r)}async function tt(e){const t=new Blob([e],{type:"image/webp"});return createImageBitmap(t)}const p=15,O=["t01_0.17","t02_0.33","t03_0.50","t04_0.67","t05_0.83"],et=O.length,H=new Set(["aa__kk","aa__nn","aa__sil","CH__aa","CH__DD","CH__E","CH__FF","CH__I","CH__kk","CH__nn","CH__O","CH__PP","CH__RR","CH__sil","CH__SS","CH__TH","CH__U","DD__aa","DD__E","DD__FF","DD__I","DD__kk","DD__nn","DD__O","DD__PP","DD__RR","DD__sil","DD__SS","DD__TH","DD__U","E__aa","E__FF","E__I","E__kk","E__nn","E__O","E__PP","E__RR","E__sil","E__SS","E__TH","E__U","FF__aa","FF__I","FF__kk","FF__nn","FF__O","FF__PP","FF__RR","FF__sil","FF__SS","FF__TH","FF__U","I__aa","I__kk","I__nn","I__O","I__PP","I__RR","I__sil","I__SS","I__TH","I__U","kk__nn","kk__sil","nn__sil","O__aa","O__kk","O__nn","O__PP","O__RR","O__sil","O__SS","O__TH","O__U","PP__aa","PP__kk","PP__nn","PP__RR","PP__sil","PP__SS","PP__TH","PP__U","RR__aa","RR__kk","RR__nn","RR__sil","RR__SS","RR__TH","RR__U","SS__aa","SS__kk","SS__nn","SS__sil","SS__TH","SS__U","TH__aa","TH__kk","TH__nn","TH__sil","TH__U","U__aa","U__kk","U__nn","U__sil"]);function v(e,t){return`${e}__${t}`}function U(e,t){return`pairs/${e}/${t}_minus_sil.png`}function q(e){return`${e}_minus_sil.png`}function nt(e,t){if(e===t)return[];const n=[],r=v(e,t);if(H.has(r)){for(const a of O)n.push(U(r,a));return n}const s=v(t,e);if(H.has(s)){for(let a=et-1;a>=0;a--)n.push(U(s,O[a]));return n}return[]}function rt(e){return e<16?[]:e<35?[2]:e<51?[0,4]:e<67?[0,2,4]:e<83?[0,1,2,3]:[0,1,2,3,4]}function st(e,t){return rt(t).map(r=>e[r]).filter(r=>r!=null)}function at(e,t,n){if(!n)return`${e}/${t}`;const r=t.split("/").pop()??t,s=r.includes(".")?r.slice(0,r.lastIndexOf(".")):r;return`${e}/${s}.bin`}function ot(e,t,n,r,s=p){const a=new OffscreenCanvas(n,r),o=a.getContext("2d",{willReadFrequently:!0});o.drawImage(e,0,0,n,r);const _=o.getImageData(0,0,n,r),i=o.createImageData(n,r);i.data.set(_.data);const f=new OffscreenCanvas(n,r).getContext("2d",{willReadFrequently:!0});for(const D of t){f.clearRect(0,0,n,r),f.drawImage(D,0,0,n,r);const d=f.getImageData(0,0,n,r);for(let c=0;c<i.data.length;c+=4){const E=d.data[c],P=d.data[c+1],J=d.data[c+2];Math.max(E,P,J)>s&&(i.data[c]=E,i.data[c+1]=P,i.data[c+2]=J,i.data[c+3]=255)}}return o.putImageData(i,0,0),a}const N=-1;function it(e){return e<0}let S=0,L=Promise.resolve();function F(e,t){const n=t>0?-t:N,r={type:"renderDone",requestId:e,generation:n};self.postMessage(r)}async function ct(e){const t=typeof e.liveSeq=="number"?e.liveSeq:0;if(t>0&&(S=t),!u)throw new Error("Worker not initialized");if(t>0&&t!==S){F(e.requestId,t);return}if(await m(w??void 0),t>0&&t!==S){F(e.requestId,t);return}if(await K(u,e.diffPath,e.expressionDiffPath??null,e.threshold??p),t>0&&t!==S){F(e.requestId,t);return}const n=t>0?-t:N;await $(n),F(e.requestId,t)}let u=null,y=null,w=null,M=null,b="sil";const k=new Map;function g(){if(!M)throw new Error("Worker assets not configured");return M}function _t(){return`${g().twinBase}/sil.png`}function lt(){const e=g();return e.encrypted?`${e.binBase}/atlas.bin`:`${e.twinBase}/atlas.json`}function ft(){const e=g();return e.encrypted?`${e.binBase}/expression_atlas.bin`:`${e.twinBase}/expression_atlas.json`}function ut(){const e=g();return e.encrypted?e.binBase:e.twinBase}function x(){return g().encrypted}async function C(e){const t=await fetch(e);if(!t.ok)throw new Error(`Failed to load image: ${e}`);const n=await t.blob();return createImageBitmap(n)}async function W(e,t,n){const r=x(),s=r?await V(e,n):await fetch(e).then(l=>{if(!l.ok)throw new Error(`Failed to load ${e}`);return l.json()}),a=s.sheets?.[0];if(!a?.path)throw new Error(`${e} has no sheets[0].path`);const o=r?await tt(await B(at(t,a.path,r),n??"")):await C(`${t}/${a.path}`),_=new Map;for(const l of s.cells??[])l.path&&_.set(l.path,l);const i=new Map;for(const l of s.sheets??[])i.set(l.index,l);return{atlas:o,atlasMeta:s,cellByPath:_,sheetByIndex:i,diffBase:t}}async function m(e){if(x()&&!e)throw new Error("Encrypted assets enabled but no key provided");if(y&&(!x()||w===(e??null)))return y;w=e??null,k.clear();const t=ut();return y=(async()=>{const n=await C(_t()),r=await W(lt(),t,e);let s=null;try{s=await W(ft(),t,e)}catch{s=null}return{sil:n,viseme:r,expression:s}})(),y}function R(e){return G(e.viseme.atlasMeta,e.sil.width,e.sil.height)}function dt(e,t){if(!e.sheetByIndex.get(t.sheet??0))throw new Error(`Unknown sheet index: ${t.sheet}`);const r=new OffscreenCanvas(t.w,t.h);return r.getContext("2d",{willReadFrequently:!0}).drawImage(e.atlas,t.x,t.y,t.w,t.h,0,0,t.w,t.h),r}async function wt(e){return createImageBitmap(e)}async function z(e,t,n){const r=`${t.diffBase}::${n}`,s=k.get(r);if(s)return s;const a=(async()=>{const o=t.cellByPath.get(n);if(!o)throw new Error(`No atlas cell for path: ${n}`);const _=j(t.atlasMeta,o),i=R(e);if(Q(o)){const D=dt(t,o),d=new OffscreenCanvas(i.width,i.height),c=d.getContext("2d");return c.fillStyle="#000",c.fillRect(0,0,i.width,i.height),c.drawImage(D,_.x,_.y),wt(d)}const l=`${t.diffBase}/${n}`,f=await C(l);if(f.width===i.width&&f.height===i.height)return f;throw f.close(),new Error(`Diff PNG wrong size for ${n}`)})();return k.set(r,a),a}function A(e,t){const n={type:"status",label:e,generation:t};self.postMessage(n)}function ht(e,t){const n={type:"error",requestId:e,message:t};self.postMessage(n)}async function $(e){if(!u)return;const t=it(e);if(!t&&e!==h)return;const n=await createImageBitmap(u);if(!t&&e!==h){n.close();return}const r={type:"frame",bitmap:n,generation:e};self.postMessage(r,{transfer:[n]})}function pt(e,t,n){return new Promise((r,s)=>{setTimeout(()=>{t!==n?s(new DOMException("Aborted","AbortError")):r()},e)})}async function K(e,t,n,r=p){const s=await m(w??void 0),{width:a,height:o}=R(s);(e.width!==a||e.height!==o)&&(e.width=a,e.height=o);const _=[];if(t){if(!s.viseme.cellByPath.get(t))throw new Error(`No viseme atlas cell for path: ${t}`);_.push(await z(s,s.viseme,t))}if(n&&s.expression){if(!s.expression.cellByPath.get(n))throw new Error(`No expression atlas cell for path: ${n}`);_.push(await z(s,s.expression,n))}const i=e.getContext("2d");if(i.clearRect(0,0,a,o),_.length===0){i.drawImage(s.sil,0,0,a,o);return}const l=ot(s.sil,_,a,o,r);i.drawImage(l,0,0)}async function T(e,t,n,r,s=p){await K(e,t,n,s),await $(r)}async function yt(e,t,n){b="sil";const r=await m(w??void 0);if(t!==n)throw new DOMException("Aborted","AbortError");const{width:s,height:a}=R(r);e.width=s,e.height=a;const o=e.getContext("2d");o.clearRect(0,0,s,a),o.drawImage(r.sil,0,0,s,a),await $(t)}async function gt(e,t,n,r,s={}){const a=s.from??b,o=s.transitionDurationMs??400,_=s.gapMs??o,i=s.threshold??p;if(await m(w??void 0),a===t){if(A(t,n),await T(e,q(t),null,n,i),n!==r)throw new DOMException("Aborted","AbortError");b=t;return}const l=nt(a,t),f=st(l,_),D=q(t),d=f.length>0?o/f.length:0;f.length===0&&A(`${a} → ${t} (no pair; final only)`,n);for(let c=0;c<f.length;c++){if(n!==r)throw new DOMException("Aborted","AbortError");const E=f[c],P=E.split("/").pop()?.replace("_minus_sil.png","")??`frame ${c}`;if(A(`${a} → ${t} (${P})`,n),await T(e,E,null,n,i),n!==r)throw new DOMException("Aborted","AbortError");d>0&&await pt(d,n,r)}if(n!==r)throw new DOMException("Aborted","AbortError");if(A(t,n),await T(e,D,null,n,i),n!==r)throw new DOMException("Aborted","AbortError");b=t}let h=0;self.onmessage=e=>{const t=e.data;(async()=>{try{switch(t.type){case"init":{u=new OffscreenCanvas(1,1);const n={type:"ready",requestId:t.requestId};self.postMessage(n);break}case"loadAssets":{M=t.urls,y=null,w=null,k.clear();const n=await m(t.keyHex),{width:r,height:s}=R(n),a={type:"loadAssetsDone",requestId:t.requestId,width:r,height:s};self.postMessage(a);break}case"drawFrame":{await new Promise((n,r)=>{L=L.then(()=>ct(t)).then(n).catch(r)});break}case"renderSil":{if(h=t.generation,!u)throw new Error("Worker not initialized");await yt(u,t.generation,h);const n={type:"renderDone",requestId:t.requestId,generation:t.generation};self.postMessage(n);break}case"renderViseme":{if(h=t.generation,!u)throw new Error("Worker not initialized");await gt(u,t.to,t.generation,h,{from:t.from,transitionDurationMs:t.transitionDurationMs,gapMs:t.gapMs,threshold:t.threshold});const n={type:"renderDone",requestId:t.requestId,generation:t.generation};self.postMessage(n);break}default:break}}catch(n){if(n instanceof DOMException&&n.name==="AbortError"){const r={type:"renderAborted",requestId:t.requestId,generation:"generation"in t?t.generation:h};self.postMessage(r);return}ht(t.requestId,n instanceof Error?n.message:String(n))}})()}})();