var e=(function(){let e=typeof document<`u`&&document.createElement(`link`).relList;return e&&e.supports&&e.supports(`modulepreload`)?`modulepreload`:`preload`})(),t=function(e){return`/`+e},n={},r=function(r,i,a){let o=Promise.resolve();if(i&&i.length>0){let r=document.getElementsByTagName(`link`),s=document.querySelector(`meta[property=csp-nonce]`),c=s?.nonce||s?.getAttribute(`nonce`);function l(e){return Promise.all(e.map(e=>Promise.resolve(e).then(e=>({status:`fulfilled`,value:e}),e=>({status:`rejected`,reason:e}))))}o=l(i.map(i=>{if(i=t(i,a),i in n)return;n[i]=!0;let o=i.endsWith(`.css`),s=o?`[rel="stylesheet"]`:``;if(a)for(let e=r.length-1;e>=0;e--){let t=r[e];if(t.href===i&&(!o||t.rel===`stylesheet`))return}else if(document.querySelector(`link[href="${i}"]${s}`))return;let l=document.createElement(`link`);if(l.rel=o?`stylesheet`:e,o||(l.as=`script`),l.crossOrigin=``,l.href=i,c&&l.setAttribute(`nonce`,c),document.head.appendChild(l),o)return new Promise((e,t)=>{l.addEventListener(`load`,e),l.addEventListener(`error`,()=>t(Error(`Unable to preload CSS for ${i}`)))})}))}function s(e){let t=new Event(`vite:preloadError`,{cancelable:!0});if(t.payload=e,window.dispatchEvent(t),!t.defaultPrevented)throw e}return o.then(e=>{for(let t of e||[])t.status===`rejected`&&s(t.reason);return r().catch(s)})},i=null,a=``,o=Promise.resolve(),s=null,c=new Map,l=e=>e?{inputTokens:e.prompt_tokens,outputTokens:e.completion_tokens,totalTokens:e.total_tokens}:void 0,u=(e,t,n)=>{e.postMessage({type:`error`,code:t,message:n})},d=()=>!!(globalThis.navigator&&`gpu`in globalThis.navigator),f=async e=>{if(!d())throw Error(`WebGPU is not available in this browser context`);if(!i){s??=r(()=>import(`../assets/lib-b4BISYKv.js`),[]);let{MLCEngine:e}=await s;i=new e}return a!==e&&(await i.reload(e),a=e),i},p=e=>e.map(e=>({role:e.role,content:e.content})),m=e=>e.choices.map(e=>e.delta.content??``).filter(Boolean).join(``),h=async(e,t)=>{let n={aborted:!1,owner:e};c.set(t.requestId,n);try{let r=await f(t.model);if(n.aborted)throw new DOMException(`Request was aborted`,`AbortError`);e.postMessage({type:`start`,provider:`webllm`,model:t.model});let i=await r.chat.completions.create({messages:p(t.messages),stream:!0,stream_options:{include_usage:!0},temperature:t.temperature,max_tokens:t.maxOutputTokens,response_format:t.responseFormat===`json`?{type:`json_object`}:{type:`text`},extra_body:{enable_thinking:t.reasoning===!0}}),a;for await(let t of i){if(n.aborted)throw new DOMException(`Request was aborted`,`AbortError`);a=t.usage??a;let r=m(t);r&&e.postMessage({type:`delta`,delta:r})}e.postMessage({type:`done`,usage:l(a)})}catch(t){u(e,n.aborted||t instanceof DOMException&&t.name===`AbortError`?`ABORTED`:`PROVIDER_UNAVAILABLE`,t instanceof Error?t.message:`WebLLM request failed`)}finally{c.delete(t.requestId)}};chrome.runtime.onConnect.addListener(e=>{if(e.name!==`ai-runtime-webllm`)return;let t=new Set;e.onMessage.addListener(n=>{if(n.type===`webllm.status`){e.postMessage({type:`status`,available:d(),reason:d()?void 0:`WebGPU is not available`});return}if(n.type===`webllm.abort`){let t=c.get(n.requestId);t?.owner===e&&(t.aborted=!0,i?.interruptGenerate());return}t.add(n.input.requestId),o=o.catch(()=>{}).then(()=>h(e,n.input)).finally(()=>{t.delete(n.input.requestId)})}),e.onDisconnect.addListener(()=>{for(let n of t){let t=c.get(n);t?.owner===e&&(t.aborted=!0)}t.size>0&&i?.interruptGenerate()})});