// Runtime worker errors can be logged without rejecting loadModel(). Bound that // wait and terminate the worker on cancellation, timeout, or a reported failure. export function startRuntime(start,{signal,stop,timeoutMs=120000,onFailure=()=>{}}){ return new Promise((resolve,reject)=>{ let settled=false,started=false,timer; const cleanup=()=>{clearTimeout(timer);signal?.removeEventListener('abort',abort);onFailure(null);}; const fail=error=>{if(settled)return;settled=true;cleanup();Promise.resolve().then(stop).catch(()=>{});reject(error);}; const abort=()=>fail(signal.reason||new DOMException('Loading canceled','AbortError')); onFailure(fail); if(signal?.aborted){abort();return;} signal?.addEventListener('abort',abort,{once:true}); timer=setTimeout(()=>fail(new Error('GPU setup did not finish within two minutes. Your downloaded weights are still available in this tab; retry setup or choose a smaller context.')),timeoutMs); Promise.resolve().then(()=>{if(settled)return;started=true;return start();}).then(value=>{if(settled){if(started)Promise.resolve().then(stop).catch(()=>{});return;}settled=true;cleanup();resolve(value);},fail); }); }