@degreesign/chat 0.2.0 → 0.3.0
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.
- package/README.md +84 -57
- package/dist/assets/litert/litertlm_wasm_internal.js +1 -0
- package/dist/assets/litert/litertlm_wasm_internal.wasm +0 -0
- package/dist/browser/chat.min.mjs +1 -1
- package/dist/browser/engine-litert.min.mjs +2 -0
- package/dist/browser/engine-litert.min.mjs.LICENSE.txt +1 -0
- package/dist/engines/engine.d.ts +6 -1
- package/dist/engines/images.d.ts +3 -1
- package/dist/engines/litert.d.ts +5 -0
- package/dist/engines/wllama.d.ts +1 -5
- package/dist/enums.d.ts +3 -1
- package/dist/models.d.ts +3 -1
- package/dist/node/chat.node.min.js +1 -1
- package/dist/node/chat.node.min.mjs +1 -1
- package/dist/node/engine-litert.node.min.js +2 -0
- package/dist/node/engine-litert.node.min.js.LICENSE.txt +1 -0
- package/dist/node/engine-litert.node.min.mjs +2 -0
- package/dist/node/engine-litert.node.min.mjs.LICENSE.txt +1 -0
- package/dist/types.d.ts +43 -2
- package/package.json +7 -3
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see chat.min.mjs.LICENSE.txt */
|
|
2
|
-
var e={};const t={};function n(a){const o=t[a];if(void 0!==o)return o.exports;const r=t[a]={exports:{}};return e[a](r,r.exports,n),r.exports}var a,o,r,s,i,c;n.m=e,n.d=(e,t)=>{for(var a in t)n.o(t,a)&&!n.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},n.f={},n.e=e=>{const t=[];return n.f.j(e,t),Promise.all(t)},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{const e={924:()=>import("./engine-wllama.min.mjs")},t={792:0},a=e=>{let{__webpack_esm_ids__:a,__webpack_esm_modules__:o,__webpack_esm_runtime__:r}=e;var s,i,c=0;for(s in o)n.o(o,s)&&(n.m[s]=o[s]);for(r&&r(n);c<a.length;c++)i=a[c],n.o(t,i)&&t[i]&&t[i][0](),t[i]=0};n.f.j=(o,r)=>{let s=n.o(t,o)?t[o]:void 0;if(0!==s)if(s)r.push(s[1]);else{let n=e[o]().then(a,e=>{throw 0!==t[o]&&(t[o]=void 0),e});n=Promise.race([n,new Promise(e=>s=t[o]=[e])]),r.push(s[1]=n)}}})(),function(e){e.system="system",e.user="user",e.assistant="assistant"}(a||(a={})),function(e){e[e.online=0]="online",e[e.away=1]="away",e[e.offline=2]="offline"}(o||(o={})),function(e){e[e.idle=0]="idle",e[e.downloading=1]="downloading",e[e.ready=2]="ready",e[e.error=3]="error",e[e.unsupported=4]="unsupported"}(r||(r={})),function(e){e.cpu="cpu",e.none="none"}(s||(s={})),function(e){e.download="download",e.load="load"}(i||(i={})),function(e){e.qwen3_vl_2b="qwen3-vl-2b"}(c||(c={}));const l=[{id:c.qwen3_vl_2b,reasoning:!0,cpu:{repo:"Qwen/Qwen3-VL-2B-Thinking-GGUF",file:"Qwen3VL-2B-Thinking-Q4_K_M.gguf",mmproj:{file:"mmproj-Qwen3VL-2B-Thinking-Q8_0.gguf"}}}],d=e=>e?l.find(t=>t.id==e):void 0,g=e=>!!e.cpu.mmproj,u=new URL("./../assets/wllama/wllama.wasm",import.meta.url).href,m=new URL("./../assets/wllama/compat/wllama.wasm",import.meta.url).href,p=new URL("./../assets/wllama/compat/wllama.js",import.meta.url).href,f=288,h=async e=>{if(!e||"function"!=typeof fetch||"function"!=typeof createImageBitmap)return e;try{const t=await fetch(e),n=await createImageBitmap(await t.blob()),a=Math.min(n.width,n.height),o=Math.floor((n.width-a)/2),r=Math.floor((n.height-a)/2),s=((e,t)=>{if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(e,t);if("undefined"==typeof document)return;const n=document.createElement("canvas");return n.width=e,n.height=t,n})(f,f),i=s?s.getContext("2d"):null;if(!s||!i||a<1)return n.close(),e;if(i.drawImage(n,o,r,a,a,0,0,f,f),n.close(),(e=>"undefined"!=typeof OffscreenCanvas&&e instanceof OffscreenCanvas)(s)){const t=await s.convertToBlob({type:"image/jpeg",quality:.85});return await(e=>new Promise(t=>{if("undefined"==typeof FileReader)return void t("");const n=new FileReader;n.onerror=()=>t(""),n.onload=()=>t(`${n.result}`),n.readAsDataURL(e)}))(t)||e}return s.toDataURL("image/jpeg",.85)}catch{return e}},w={maxReplyTokens:200,maxThinkingTokens:64,maxReasoningTokens:150,maxImages:2,maxContentLength:1e3,promptCharBudget:1e4,maxThreads:2},y=(e,t,n)=>"number"==typeof e&&Number.isFinite(e)&&e>=n?Math.floor(e):t,x=(e={})=>{const t=w;return{maxReplyTokens:y(e.maxReplyTokens,t.maxReplyTokens,1),maxThinkingTokens:y(e.maxThinkingTokens,t.maxThinkingTokens,1),maxReasoningTokens:y(e.maxReasoningTokens,t.maxReasoningTokens,1),maxImages:y(e.maxImages,t.maxImages,0),maxContentLength:y(e.maxContentLength,t.maxContentLength,1),promptCharBudget:y(e.promptCharBudget,t.promptCharBudget,1),maxThreads:y(e.maxThreads,t.maxThreads,1)}},k=["<|channel>thought\n","<|channel>thought","..."],v=["<channel|>","..."],b=(e,t)=>t.length?[...e?[{type:"text",text:e}]:[],...t.map(e=>({type:"image",url:e}))]:e,_=e=>"string"==typeof e?e?[{type:"text",text:e}]:[]:e,M=(e,t)=>{if("string"==typeof e&&"string"==typeof t)return`${e}\n${t}`;const n=[];for(const a of[..._(e),{type:"text",text:"\n"},..._(t)]){const e=n[n.length-1];"text"==a.type&&"text"==e?.type?e.text+=a.text:n.push(a)}return n},T=async({model:e,logging:t,onReason:n,onText:a,start:o,stopped:r})=>{let s="",i="",c="";const l=((e,t)=>{let n="",a=!1;const o=(e,t)=>{let n=-1,a="";for(const o of t){const t=e.indexOf(o);t>=0&&(n<0||t<n)&&(n=t,a=o)}return{at:n,tag:a}},r=(e,t)=>{let n="";for(const a of t)for(let t=Math.min(e.length,a.length-1);t>n.length;t--)if(e.endsWith(a.slice(0,t))){n=e.slice(e.length-t);break}return n};return{feed:s=>{let i=n+s;for(n="";i;){if(a){const{at:t,tag:s}=o(i,v);if(t>=0){t&&e(i.slice(0,t)),i=i.slice(t+s.length),a=!1;continue}const c=r(i,v);e(i.slice(0,i.length-c.length)),n=c;break}{const{at:e,tag:s}=o(i,k);if(e>=0){e&&t(i.slice(0,e)),i=i.slice(e+s.length),i.startsWith("\n")&&(i=i.slice(1)),a=!0;continue}const c=r(i,k);t(i.slice(0,i.length-c.length)),n=c;break}}},flush:()=>{n&&(a?e(n):t(n),n="")}}})(e=>{i+=e,n(e)},e=>{s+=e,a(e)});try{const e=await o();for await(const t of e){if(r())break;const e=t?.choices?.[0],a=e?.delta,o="string"==typeof a?.reasoning_content?a.reasoning_content:"string"==typeof a?.reasoning?a.reasoning:"";e?.finish_reason&&(c=e.finish_reason),o&&(i+=o,n(o)),a?.content&&l.feed(a.content)}l.flush()}catch(e){t&&console.log("Local chat failed",e)}const d=r()?void 0:s||void 0;return t&&(console.log("Local chat reply",e.id,d?"****":""),console.log("Local chat finish",e.id,c||"none",s.length,i.length)),d},C=async({model:e,params:t,start:n,stopped:o})=>{const r=await(async e=>{const t=[];for(const n of e){if("string"==typeof n.content){t.push(n);continue}const e=[];for(const t of n.content)e.push("image"==t.type?{type:"image",url:await h(t.url)}:t);t.push({role:"user",content:e})}return t})(((e,t=w)=>{const n=t.maxContentLength,o=e.filter(e=>"string"==typeof e.content&&!!e.content.trim()||!(e.role!=a.user||!e.images?.length)),r=[],s=[];let i=0,c=t.maxImages;for(let e=o.length-1;e>=0;e--){const l=o[e],d=("string"==typeof l.content?l.content:"").slice(0,n),g=l.role==a.user?(l.images||[]).filter(e=>"string"==typeof e&&!!e).slice(0,Math.max(0,c)):[];if(d||g.length){if(d&&i+d.length>t.promptCharBudget)break;i+=d.length,c-=g.length,l.role==a.system?r.unshift(d):l.role==a.assistant?s.unshift({role:"assistant",content:d}):s.unshift({role:"user",content:b(d,g)})}}for(;s.length&&"user"!=s[0].role;)s.shift();const l=[];for(const e of s){const t=l[l.length-1];"assistant"==t?.role&&"assistant"==e.role?t.content=`${t.content}\n${e.content}`:"user"==t?.role&&"user"==e.role?t.content=M(t.content,e.content):"assistant"==e.role?l.push({role:"assistant",content:e.content}):l.push({role:"user",content:e.content})}const d=r.join("\n\n"),g=[];return d&&g.push({role:"system",content:d}),g.push(...l),g})(t.messages,t.limits));return t.logging&&((e,t)=>{console.log("Local chat call",e.id,t.map(({role:e,content:t})=>({role:e,content:"****",...Array.isArray(t)?{images:t.filter(e=>"image"==e.type).length}:{}})))})(e,r),await T({model:e,logging:t.logging,onReason:t.onReason,onText:t.onText,start:()=>n(r),stopped:o})};let L,R,j="",W=!1;const $=e=>`${e.wasm}|${e.compatWasm}|${e.compatWorker}`,q=e=>{const t={wasm:u,compatWasm:m,compatWorker:p};return{wasm:e?.wasm||t.wasm,compatWasm:e?.compatWasm||t.compatWasm,compatWorker:e?.compatWorker||t.compatWorker}},B=e=>{const t=e=>(...t)=>{W&&("string"==typeof t[0]&&t[0].includes("ggml_webgpu: Failed to get an adapter")||e(...t))};return{...e,log:t(e.log),warn:t(e.warn),error:t(e.error)}},P=async()=>{if(L)return L;const e=await n.e(924).then(()=>n(577)),t=R||q(),a=new e.Wllama({default:t.wasm},{logger:B(e.LoggerWithoutDebug)});return R||(R=t,j=$(t)),a.setCompat({wasm:t.compatWasm,worker:t.compatWorker}),L=a,a},I=async()=>{const e=L;L=void 0;try{await(e?.exit())}catch{}},U=(e,t)=>e.name.includes(t)||!!e.metadata?.originalURL?.includes(t),O=e=>[e.cpu.file,...e.cpu.mmproj?[e.cpu.mmproj.file]:[]],A=(e,t)=>`https://huggingface.co/${e}/resolve/main/${t}`,F=async e=>{if(!e.startsWith("data:"))return await(await fetch(e)).arrayBuffer();const t=e.indexOf(","),n=t>=0?e.slice(0,t):"";if(t<0)throw new Error("Invalid image data");if(!n.includes(";base64"))return await(await fetch(e)).arrayBuffer();const a=atob(e.slice(t+1)),o=new Uint8Array(a.length);for(let e=0;e<a.length;e++)o[e]=a.charCodeAt(e);return o.buffer},E=async e=>{const t=[];for(const n of e){if("string"==typeof n.content){"system"==n.role?t.push({role:"system",content:n.content}):"assistant"==n.role?t.push({role:"assistant",content:n.content}):t.push({role:"user",content:n.content});continue}const e=[];for(const t of n.content)e.push("text"==t.type?{type:"text",text:t.text}:{type:"image",data:await F(t.url)});t.push({role:"user",content:e})}return t},Q=async(e,t,n)=>{if(W=!0===n.logging,e.isModelLoaded())return await C({model:t,params:n,stopped:()=>!!n.signal?.aborted,start:async a=>{if((e=>e.some(e=>Array.isArray(e.content)&&e.content.some(e=>"image"==e.type)))(a)&&!e.supportInputModality("image"))throw new Error("Model does not accept images");return await e.createChatCompletion({messages:await E(a),...(o=n.limits,r=n.reasoning,{stream:!0,temperature:.7,max_tokens:r?o.maxReasoningTokens:o.maxReplyTokens}),abortSignal:n.signal,...t.reasoning?{chat_template_kwargs:{enable_thinking:n.reasoning}}:{}});var o,r}})},S={backend:s.cpu,startLLM:async({model:e,limits:t,onProgress:n,signal:a,logging:o,assets:r})=>{W=!0===o;const c=q(r),l=$(c);L&&l!=j&&await I(),R=c,j=l;const d=(g="undefined"!=typeof navigator&&navigator.hardwareConcurrency||4,u=t.maxThreads,Math.max(1,Math.min(Math.max(2,Math.min(8,(g||4)-1)),u)));var g,u;const m=await P(),p=A(e.cpu.repo,e.cpu.file),f=e.cpu.mmproj?.file,h=f?A(e.cpu.repo,f):"";if(a?.aborted)throw await I(),new Error("Cancelled");if(f){const t=await m.cacheManager.list(),n=t.some(t=>U(t,e.cpu.file)),a=t.some(e=>U(e,f));n&&!a&&await m.cacheManager.deleteMany(t=>U(t,e.cpu.file))}try{await m.loadModelFromUrl(h?{url:p,mmprojUrl:h}:p,{n_ctx:8192,n_threads:d,reasoning_budget_tokens:t.maxThinkingTokens,n_gpu_layers:0,useCache:!0,jinja:!0,signal:a,progressCallback:({loaded:e,total:t})=>{const a=t?e/t:0;n(a,a>=1?i.load:i.download)}})}catch(e){throw await I(),e}if(a?.aborted)throw await I(),new Error("Cancelled");if(!m.getLoadedContextInfo().n_ctx)throw await I(),new Error("Failed to create model context");return{model:e,backend:s.cpu,contextSize:8192,chat:t=>Q(m,e,t),unload:async()=>{if(L==m)await I();else try{await m.exit()}catch{}}}},cached:async e=>{try{const t=await P(),n=await t.cacheManager.list();return O(e).every(e=>n.some(t=>U(t,e)))}catch{return!1}},hasCache:async()=>{try{const e=await P();return(await e.cacheManager.list()).length>0}catch{return!1}},remove:async e=>{try{const t=await P();await t.cacheManager.deleteMany(t=>O(e).some(e=>U(t,e)))}catch{}},purge:async()=>{try{const e=await P();await e.cacheManager.clear()}catch{}}},D=((e={})=>{const t=e.engines?.cpu||S;let n,a,o,c,d,g,u,m=s.none,p=r.idle,f=0,h={percent:0,phase:i.download};const w=()=>e.backend||("undefined"==typeof window&&"undefined"==typeof self&&"undefined"==typeof Worker||"undefined"==typeof WebAssembly?s.none:s.cpu),y=async()=>{await t.purge()},x=(e,t)=>{h={percent:Math.max(0,Math.min(1,e)),phase:t||i.download},g?.(h.percent,h.phase)},k=e=>{p=e},v=()=>p==r.ready&&!!n&&!!a;return{load:async({model:e,limits:u,assets:p,logging:b,onProgress:_})=>{if(v()&&a?.id==e.id)return!0;if(o&&c==e.id)return g=_,_(h.percent,h.phase),await o;o&&(g=void 0,d?.abort(),await o.catch(()=>!1)),g=_;const M=++f,T=(async()=>{await(async()=>{try{if("undefined"==typeof navigator)return;await(navigator.storage?.persist?.())}catch{}})();const o=w();if(M!=f)return!1;if(o==s.none)return k(r.unsupported),!1;if(await(async e=>{if(a&&a.id!=e.id){const e=a,o=n;n=void 0,a=void 0,await(o?.unload()),await t.remove(e)}if(await t.hasCache())for(const n of l)n.id!=e.id&&await t.remove(n)})(e),M!=f)return!1;const c=await t.cached(e);if(M!=f)return!1;if(!c&&await(async()=>{try{if("undefined"==typeof navigator)return!1;if(!navigator.storage?.estimate)return!1;const{quota:e=0,usage:t=0}=await navigator.storage.estimate();return!!e&&t>=e}catch{return!1}})()&&await y(),M!=f)return!1;k(r.downloading),x(0,i.download),c&&x(1,i.load);const g=new AbortController;d=g;try{const s=await t.startLLM({model:e,limits:u,logging:b,signal:g.signal,assets:p,onProgress:(e,t)=>{M==f&&x(e,t)}});return M!=f||g.signal.aborted?(await s.unload(),!1):(n=s,a=e,m=o,k(r.ready),x(1,i.load),!0)}catch(e){return M!=f||(n=void 0,a=void 0,b&&console.log("Local model failed",e),(e=>["quota","no space","disk full","ns_error_dom_quota_reached"].some(t=>`${e?.name||""} ${e?.message||`${e}`}`.toLowerCase().includes(t)))(e)&&await y(),k(r.error)),!1}finally{d==g&&(d=void 0)}})().finally(()=>{o==T&&(o=void 0,c=void 0)});return o=T,c=e.id,await T},chat:async e=>{if(!v()||!n)return;for(;u;)await u.catch(()=>{});if(!v()||!n)return;const t=n.chat(e);u=t;try{return await t}finally{u==t&&(u=void 0)}},unload:async()=>{f++,d?.abort(),d=void 0,c=void 0;const e=n;n=void 0,a=void 0,k(r.idle),await(e?.unload())},cancelLoad:()=>{f++,d?.abort(),d=void 0,c=void 0,p==r.downloading&&k(r.idle)},ready:v,loading:()=>!v()&&p==r.downloading,status:()=>p,backend:()=>m,model:()=>a}})(),V=(e,t,n)=>{const a=e.status(),s=d(t);return{availability:a==r.ready?o.online:a==r.error||a==r.unsupported?o.offline:o.away,status:a,backend:e.backend(),supportsImages:!!s&&g(s),ready:e.ready(),loading:e.loading(),requesting:n.requesting,phase:n.phase,percent:n.percent,...n.error?{error:n.error}:{}}},G=(e=>{let t,n=0;const o=t=>t.input.onStatus?.(V(e,t.input.model,t));return s=>{t?.controller.abort();const c={token:++n,input:s,controller:new AbortController,percent:e.ready()?100:0,phase:e.ready()?i.load:i.download,requesting:!1,cancelled:!1};return t=c,o(c),(async t=>{const{input:r}=t,s=!0===r.logging,c=d(r.model),l=r.messages.some(e=>e.role!=a.system&&"string"==typeof e.content&&!!e.content.trim()),u=r.messages.some(e=>e.role==a.user&&!!e.images?.some(e=>"string"==typeof e&&!!e)),m=l||u;if(!c)return t.error="Unknown model",void o(t);if(!m)return void o(t);if(u&&!l&&!g(c))return t.error="Model does not accept images",void o(t);const p=x(r.limits),f=await e.load({model:c,limits:p,logging:s,onProgress:(e,a)=>{t.token==n&&(t.percent=Math.round(100*e),t.phase=a,o(t))}}).catch(e=>(t.token==n&&(t.error=`${e?.message||`${e}`}`),!1));if(t.token!=n)return;if(!f)return t.cancelled||t.error||(t.error="Model not ready"),void o(t);const h=g(c),w=u&&!h?r.messages.map(({images:e,...t})=>t):r.messages;if(u&&!h&&!l)return t.error="Model does not accept images",void o(t);t.requesting=!0,t.error=void 0,t.percent=100,t.phase=i.load,o(t);let y,k="",v="";try{y=await e.chat({messages:w,limits:p,reasoning:!0===r.reasoning,logging:s,onReason:e=>{t.token!=n||t.cancelled||(k+=e,r.onThinking?.(k))},onText:e=>{t.token!=n||t.cancelled||(v+=e,r.onResponse?.(v))},signal:t.controller.signal})}catch(e){if(t.token!=n)return;return t.requesting=!1,t.error=`${e?.message||`${e}`}`,void o(t)}t.token==n&&(t.requesting=!1,y||t.cancelled||(t.error="No reply"),o(t))})(c),()=>(t=>{t.token==n&&(t.cancelled=!0,t.controller.abort(),e.status()!=r.ready?(e.cancelLoad(),o(t)):e.unload().then(()=>o(t)))})(c)}})(D),N=(z=D,async e=>{const t=d(e.model),n=t=>e.onStatus?.(V(z,e.model,t));if(!t)return n({requesting:!1,phase:i.download,percent:0,error:"Unknown model"}),!1;let a,o=0,r=i.download;const s=z.ready()&&z.model()?.id==t.id;return await z.load({model:t,limits:x(e.limits),logging:!0===e.logging,onProgress:(e,t)=>{o=Math.round(100*e),r=t,n({requesting:!1,phase:r,percent:o})}}).catch(e=>(a=`${e?.message||`${e}`}`,!1))?(s&&n({requesting:!1,phase:i.load,percent:100}),!0):(n({requesting:!1,phase:r,percent:o,error:a||"Model not ready"}),!1)});var z;export{c as ChatModelId,o as ChatStatus,s as LlmBackend,i as LlmPhase,a as LlmRole,r as LlmStatus,G as createChat,N as preloadModel};
|
|
2
|
+
var e={};const t={};function n(a){const o=t[a];if(void 0!==o)return o.exports;const r=t[a]={exports:{}};return e[a](r,r.exports,n),r.exports}var a,o,r,s,i,c;n.m=e,n.d=(e,t)=>{for(var a in t)n.o(t,a)&&!n.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},n.f={},n.e=e=>{const t=[];return n.f.j(e,t),Promise.all(t)},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{const e={520:()=>import("./engine-litert.min.mjs"),924:()=>import("./engine-wllama.min.mjs")},t={792:0},a=e=>{let{__webpack_esm_ids__:a,__webpack_esm_modules__:o,__webpack_esm_runtime__:r}=e;var s,i,c=0;for(s in o)n.o(o,s)&&(n.m[s]=o[s]);for(r&&r(n);c<a.length;c++)i=a[c],n.o(t,i)&&t[i]&&t[i][0](),t[i]=0};n.f.j=(o,r)=>{let s=n.o(t,o)?t[o]:void 0;if(0!==s)if(s)r.push(s[1]);else{let n=e[o]().then(a,e=>{throw 0!==t[o]&&(t[o]=void 0),e});n=Promise.race([n,new Promise(e=>s=t[o]=[e])]),r.push(s[1]=n)}}})(),function(e){e.system="system",e.user="user",e.assistant="assistant"}(a||(a={})),function(e){e[e.online=0]="online",e[e.away=1]="away",e[e.offline=2]="offline"}(o||(o={})),function(e){e[e.idle=0]="idle",e[e.downloading=1]="downloading",e[e.ready=2]="ready",e[e.error=3]="error",e[e.unsupported=4]="unsupported"}(r||(r={})),function(e){e.cpu="cpu",e.gpu="gpu",e.none="none"}(s||(s={})),function(e){e.download="download",e.load="load"}(i||(i={})),function(e){e.qwen3_vl_2b="qwen3-vl-2b",e.gemma4_e2b="gemma4-e2b"}(c||(c={}));const l=[{id:c.qwen3_vl_2b,reasoning:!0,cpu:{repo:"Qwen/Qwen3-VL-2B-Thinking-GGUF",file:"Qwen3VL-2B-Thinking-Q4_K_M.gguf",mmproj:{file:"mmproj-Qwen3VL-2B-Thinking-Q8_0.gguf"}}},{id:c.gemma4_e2b,reasoning:!0,litert:{repo:"litert-community/gemma-4-E2B-it-litert-lm",file:"gemma-4-E2B-it.litertlm",vision:!0}}],d=e=>e?l.find(t=>t.id==e):void 0,m=e=>!!e.cpu?.mmproj||!!e.litert?.vision,g=new URL("./../assets/wllama/wllama.wasm",import.meta.url).href,u=new URL("./../assets/wllama/compat/wllama.wasm",import.meta.url).href,p=new URL("./../assets/wllama/compat/wllama.js",import.meta.url).href,f=new URL("./../assets/litert/litertlm_wasm_internal.js",import.meta.url).href,h=(new URL("./../assets/litert/litertlm_wasm_internal.wasm",import.meta.url).href,288),w=async e=>{if(!e.startsWith("data:"))return await(await fetch(e)).arrayBuffer();const t=e.indexOf(","),n=t>=0?e.slice(0,t):"";if(t<0)throw new Error("Invalid image data");if(!n.includes(";base64"))return await(await fetch(e)).arrayBuffer();const a=atob(e.slice(t+1)),o=new Uint8Array(a.length);for(let e=0;e<a.length;e++)o[e]=a.charCodeAt(e);return o.buffer},y=async e=>{if(!e||"function"!=typeof fetch||"function"!=typeof createImageBitmap)return e;try{const t=await fetch(e),n=await createImageBitmap(await t.blob()),a=Math.min(n.width,n.height),o=Math.floor((n.width-a)/2),r=Math.floor((n.height-a)/2),s=((e,t)=>{if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(e,t);if("undefined"==typeof document)return;const n=document.createElement("canvas");return n.width=e,n.height=t,n})(h,h),i=s?s.getContext("2d"):null;if(!s||!i||a<1)return n.close(),e;if(i.drawImage(n,o,r,a,a,0,0,h,h),n.close(),(e=>"undefined"!=typeof OffscreenCanvas&&e instanceof OffscreenCanvas)(s)){const t=await s.convertToBlob({type:"image/jpeg",quality:.85});return await(e=>new Promise(t=>{if("undefined"==typeof FileReader)return void t("");const n=new FileReader;n.onerror=()=>t(""),n.onload=()=>t(`${n.result}`),n.readAsDataURL(e)}))(t)||e}return s.toDataURL("image/jpeg",.85)}catch{return e}},x={maxReplyTokens:200,maxThinkingTokens:64,maxReasoningTokens:150,maxImages:2,maxContentLength:1e3,promptCharBudget:1e4,maxThreads:2},b=(e,t,n)=>"number"==typeof e&&Number.isFinite(e)&&e>=n?Math.floor(e):t,v=(e={})=>{const t=x;return{maxReplyTokens:b(e.maxReplyTokens,t.maxReplyTokens,1),maxThinkingTokens:b(e.maxThinkingTokens,t.maxThinkingTokens,1),maxReasoningTokens:b(e.maxReasoningTokens,t.maxReasoningTokens,1),maxImages:b(e.maxImages,t.maxImages,0),maxContentLength:b(e.maxContentLength,t.maxContentLength,1),promptCharBudget:b(e.promptCharBudget,t.promptCharBudget,1),maxThreads:b(e.maxThreads,t.maxThreads,1)}},k=(e,t)=>({stream:!0,temperature:.7,max_tokens:t?e.maxReasoningTokens:e.maxReplyTokens}),_=(e,t)=>`https://huggingface.co/${e}/resolve/main/${t}`,M=e=>e.some(e=>Array.isArray(e.content)&&e.content.some(e=>"image"==e.type)),T=["<|channel>thought\n","<|channel>thought","..."],L=["<channel|>","..."],C=(e,t)=>t.length?[...e?[{type:"text",text:e}]:[],...t.map(e=>({type:"image",url:e}))]:e,R=e=>"string"==typeof e?e?[{type:"text",text:e}]:[]:e,E=(e,t)=>{if("string"==typeof e&&"string"==typeof t)return`${e}\n${t}`;const n=[];for(const a of[...R(e),{type:"text",text:"\n"},...R(t)]){const e=n[n.length-1];"text"==a.type&&"text"==e?.type?e.text+=a.text:n.push(a)}return n},j=async({model:e,logging:t,onReason:n,onText:a,start:o,stopped:r})=>{let s="",i="",c="";const l=((e,t)=>{let n="",a=!1;const o=(e,t)=>{let n=-1,a="";for(const o of t){const t=e.indexOf(o);t>=0&&(n<0||t<n)&&(n=t,a=o)}return{at:n,tag:a}},r=(e,t)=>{let n="";for(const a of t)for(let t=Math.min(e.length,a.length-1);t>n.length;t--)if(e.endsWith(a.slice(0,t))){n=e.slice(e.length-t);break}return n};return{feed:s=>{let i=n+s;for(n="";i;){if(a){const{at:t,tag:s}=o(i,L);if(t>=0){t&&e(i.slice(0,t)),i=i.slice(t+s.length),a=!1;continue}const c=r(i,L);e(i.slice(0,i.length-c.length)),n=c;break}{const{at:e,tag:s}=o(i,T);if(e>=0){e&&t(i.slice(0,e)),i=i.slice(e+s.length),i.startsWith("\n")&&(i=i.slice(1)),a=!0;continue}const c=r(i,T);t(i.slice(0,i.length-c.length)),n=c;break}}},flush:()=>{n&&(a?e(n):t(n),n="")}}})(e=>{i+=e,n(e)},e=>{s+=e,a(e)});try{const e=await o();for await(const t of e){if(r())break;const e=t?.choices?.[0],a=e?.delta,o="string"==typeof a?.reasoning_content?a.reasoning_content:"string"==typeof a?.reasoning?a.reasoning:"";e?.finish_reason&&(c=e.finish_reason),o&&(i+=o,n(o)),a?.content&&l.feed(a.content)}l.flush()}catch(e){t&&console.log("Local chat failed",e)}const d=r()?void 0:s||void 0;return t&&(console.log("Local chat reply",e.id,d?"****":""),console.log("Local chat finish",e.id,c||"none",s.length,i.length)),d},W=async({model:e,params:t,start:n,stopped:o})=>{const r=await(async e=>{const t=[];for(const n of e){if("string"==typeof n.content){t.push(n);continue}const e=[];for(const t of n.content)e.push("image"==t.type?{type:"image",url:await y(t.url)}:t);t.push({role:"user",content:e})}return t})(((e,t=x)=>{const n=t.maxContentLength,o=e.filter(e=>"string"==typeof e.content&&!!e.content.trim()||!(e.role!=a.user||!e.images?.length)),r=[],s=[];let i=0,c=t.maxImages;for(let e=o.length-1;e>=0;e--){const l=o[e],d=("string"==typeof l.content?l.content:"").slice(0,n),m=l.role==a.user?(l.images||[]).filter(e=>"string"==typeof e&&!!e).slice(0,Math.max(0,c)):[];if(d||m.length){if(d&&i+d.length>t.promptCharBudget)break;i+=d.length,c-=m.length,l.role==a.system?r.unshift(d):l.role==a.assistant?s.unshift({role:"assistant",content:d}):s.unshift({role:"user",content:C(d,m)})}}for(;s.length&&"user"!=s[0].role;)s.shift();const l=[];for(const e of s){const t=l[l.length-1];"assistant"==t?.role&&"assistant"==e.role?t.content=`${t.content}\n${e.content}`:"user"==t?.role&&"user"==e.role?t.content=E(t.content,e.content):"assistant"==e.role?l.push({role:"assistant",content:e.content}):l.push({role:"user",content:e.content})}const d=r.join("\n\n"),m=[];return d&&m.push({role:"system",content:d}),m.push(...l),m})(t.messages,t.limits));return t.logging&&((e,t)=>{console.log("Local chat call",e.id,t.map(({role:e,content:t})=>({role:e,content:"****",...Array.isArray(t)?{images:t.filter(e=>"image"==e.type).length}:{}})))})(e,r),await j({model:e,logging:t.logging,onReason:t.onReason,onText:t.onText,start:()=>n(r),stopped:o})},$="litertlm-models",q={num_output_candidates:1,wait_for_weight_uploads:!0,num_decode_steps_per_sync:3,sequence_batch_size:0,supported_lora_ranks:[],max_top_k:40,enable_decode_logits:!1,enable_external_embeddings:!1,use_submodel:!1,use_autosized_ringbuffers:!0},P=async()=>{try{if("undefined"==typeof caches)return;return await caches.open($)}catch{return}},B=async e=>{const t=[];for(const n of e){if("string"==typeof n.content){t.push({role:n.role,content:n.content});continue}const e=[];for(const t of n.content)e.push("text"==t.type?{type:"text",text:t.text}:{type:"image",data:await w(t.url)});t.push({role:"user",content:e})}return t},U=async({engine:e,model:t,mod:n,params:a})=>await W({model:t,params:a,stopped:()=>!!a.signal?.aborted,start:async o=>{if(a.signal?.aborted)throw new Error("Cancelled");if(M(o)&&!t.litert?.vision)throw new Error("Model does not accept images");return async function*({engine:e,mod:t,params:n,messages:a}){const o=a[a.length-1],r=k(n.limits,n.reasoning);if("user"!=o?.role)throw new Error("No user turn");const s=await B(a.slice(0,-1)),i=(await B([o]))[0];if(!i)throw new Error("Empty prompt");const c=await e.createConversation({sessionConfig:{maxOutputTokens:r.max_tokens,samplerParams:{type:t.SamplerType.TOP_K,temperature:r.temperature,k:40,p:.95}},preface:{...s.length?{messages:s}:{},extra_context:{enable_thinking:n.reasoning}}});let l;const d=()=>{l&&l.cancel().catch(()=>{});try{c.cancel()}catch{}};n.signal?.addEventListener("abort",d,{once:!0});try{if(n.signal?.aborted)return;const e=c.sendMessageStreaming(i).getReader();for(l=e;;){const{done:t,value:n}=await e.read();if(t)break;const a={};let o="string"==typeof n.content?n.content:"";if(Array.isArray(n.content))for(const e of n.content)"text"==e.type&&(o+=e.text);o&&(a.content=o);const r=n.channels?.thought;r&&(a.reasoning=r),(a.content||a.reasoning)&&(yield{choices:[{delta:a}]})}}finally{n.signal?.removeEventListener("abort",d);try{await c.delete()}catch{}}}({engine:e,mod:n,params:a,messages:o})}}),I={backend:s.gpu,startLLM:async({model:e,limits:t,onProgress:a,signal:o,logging:r})=>{const c=e.litert;if(!c)throw new Error("Model has no LiteRT artifact");const l=await n.e(520).then(()=>n(507));if(await l.getOrLoadGlobalLiteRtLm(f),o?.aborted)throw new Error("Cancelled");const d=_(c.repo,c.file),m=await P(),g=m?await m.match(d):void 0;let u;if(g?.body)u=g.body,a(1,i.load);else{const e=await fetch(d,{signal:o});if(!e.ok||!e.body)throw new Error("Model download failed");const t=Number(e.headers.get("content-length"))||0;let n=0;const s=e.body.pipeThrough(new TransformStream({transform:(e,o)=>{n+=e.byteLength,a(t?n/t:0,i.download),o.enqueue(e)}}));if(m){const[e,n]=s.tee();m.put(d,new Response(n,{headers:{"content-type":"application/octet-stream",...t?{"content-length":`${t}`}:{}}})).catch(e=>{r&&console.log("Local model cache failed",e)}),u=e}else u=s}if(o?.aborted)throw new Error("Cancelled");a(1,i.load);const p=await l.Engine.create({model:u,mainExecutorSettings:{maxNumTokens:4096,...c.vision?{maxNumImages:Math.max(1,t.maxImages)}:{},backendConfig:q},...c.vision?{visionBackend:l.Backend.GPU}:{}});if(o?.aborted)throw await p.delete(),new Error("Cancelled");return{model:e,backend:s.gpu,contextSize:4096,chat:t=>U({engine:p,model:e,mod:l,params:t}),unload:async()=>{await p.delete()}}},cached:async e=>{try{const t=e.litert,n=await P();return!(!t||!n)&&!!await n.match(_(t.repo,t.file))}catch{return!1}},hasCache:async()=>{try{const e=await P();return!!e&&(await e.keys()).length>0}catch{return!1}},remove:async e=>{try{const t=e.litert,n=await P();if(!t||!n)return;await n.delete(_(t.repo,t.file))}catch{}},purge:async()=>{try{"undefined"!=typeof caches&&await caches.delete($)}catch{}}};let O,A,S="",F=!1;const N=e=>`${e.wasm}|${e.compatWasm}|${e.compatWorker}`,Q=e=>{const t={wasm:g,compatWasm:u,compatWorker:p};return{wasm:e?.wasm||t.wasm,compatWasm:e?.compatWasm||t.compatWasm,compatWorker:e?.compatWorker||t.compatWorker}},z=e=>{const t=e=>(...t)=>{F&&("string"==typeof t[0]&&t[0].includes("ggml_webgpu: Failed to get an adapter")||e(...t))};return{...e,log:t(e.log),warn:t(e.warn),error:t(e.error)}},G=async()=>{if(O)return O;const e=await n.e(924).then(()=>n(577)),t=A||Q(),a=new e.Wllama({default:t.wasm},{logger:z(e.LoggerWithoutDebug)});return A||(A=t,S=N(t)),a.setCompat({wasm:t.compatWasm,worker:t.compatWorker}),O=a,a},D=async()=>{const e=O;O=void 0;try{await(e?.exit())}catch{}},V=(e,t)=>e.name.includes(t)||!!e.metadata?.originalURL?.includes(t),K=e=>e.cpu?[e.cpu.file,...e.cpu.mmproj?[e.cpu.mmproj.file]:[]]:[],H=async e=>{const t=[];for(const n of e){if("string"==typeof n.content){"system"==n.role?t.push({role:"system",content:n.content}):"assistant"==n.role?t.push({role:"assistant",content:n.content}):t.push({role:"user",content:n.content});continue}const e=[];for(const t of n.content)e.push("text"==t.type?{type:"text",text:t.text}:{type:"image",data:await w(t.url)});t.push({role:"user",content:e})}return t},J={backend:s.cpu,startLLM:async({model:e,limits:t,onProgress:n,signal:a,logging:o,assets:r})=>{F=!0===o;const c=e.cpu;if(!c)throw new Error("Model has no CPU artifact");const l=Q(r),d=N(l);O&&d!=S&&await D(),A=l,S=d;const m=(g="undefined"!=typeof navigator&&navigator.hardwareConcurrency||4,u=t.maxThreads,Math.max(1,Math.min(Math.max(2,Math.min(8,(g||4)-1)),u)));var g,u;const p=await G(),f=_(c.repo,c.file),h=c.mmproj?.file,w=h?_(c.repo,h):"";if(a?.aborted)throw await D(),new Error("Cancelled");if(h){const e=await p.cacheManager.list(),t=e.some(e=>V(e,c.file)),n=e.some(e=>V(e,h));t&&!n&&await p.cacheManager.deleteMany(e=>V(e,c.file))}try{await p.loadModelFromUrl(w?{url:f,mmprojUrl:w}:f,{n_ctx:8192,n_threads:m,reasoning_budget_tokens:t.maxThinkingTokens,n_gpu_layers:0,useCache:!0,jinja:!0,signal:a,progressCallback:({loaded:e,total:t})=>{const a=t?e/t:0;n(a,a>=1?i.load:i.download)}})}catch(e){throw await D(),e}if(a?.aborted)throw await D(),new Error("Cancelled");if(!p.getLoadedContextInfo().n_ctx)throw await D(),new Error("Failed to create model context");return{model:e,backend:s.cpu,contextSize:8192,chat:t=>(async(e,t,n)=>{if(F=!0===n.logging,e.isModelLoaded())return await W({model:t,params:n,stopped:()=>!!n.signal?.aborted,start:async a=>{if(M(a)&&!e.supportInputModality("image"))throw new Error("Model does not accept images");return await e.createChatCompletion({messages:await H(a),...k(n.limits,n.reasoning),abortSignal:n.signal,...t.reasoning?{chat_template_kwargs:{enable_thinking:n.reasoning}}:{}})}})})(p,e,t),unload:async()=>{if(O==p)await D();else try{await p.exit()}catch{}}}},cached:async e=>{try{if(!e.cpu)return!1;const t=await G(),n=await t.cacheManager.list();return K(e).every(e=>n.some(t=>V(t,e)))}catch{return!1}},hasCache:async()=>{try{const e=await G();return(await e.cacheManager.list()).length>0}catch{return!1}},remove:async e=>{try{if(!e.cpu)return;const t=await G();await t.cacheManager.deleteMany(t=>K(e).some(e=>V(t,e)))}catch{}},purge:async()=>{try{const e=await G();await e.cacheManager.clear()}catch{}}},X=((e={})=>{const t=e.engines?.cpu||J,n=e.engines?.litert||I;let a,o,c,d,m,g,u,p,f=s.none,h=r.idle,w=0,y={percent:0,phase:i.download};const x=a=>{if(e.backend!=s.none)return e.backend==s.cpu?a.cpu?t:void 0:e.backend==s.gpu?a.litert?n:void 0:a.litert&&"undefined"!=typeof navigator&&navigator.gpu&&"undefined"!=typeof WebAssembly?n:!a.cpu||"undefined"==typeof window&&"undefined"==typeof self&&"undefined"==typeof Worker||"undefined"==typeof WebAssembly?void 0:t},b=(e,t)=>{y={percent:Math.max(0,Math.min(1,e)),phase:t||i.download},u?.(y.percent,y.phase)},v=e=>{h=e},k=()=>h==r.ready&&!!a&&!!o;return{load:async({model:e,limits:t,assets:n,logging:s,onProgress:p})=>{if(k()&&o?.id==e.id)return!0;if(d&&m==e.id)return u=p,p(y.percent,y.phase),await d;d&&(u=void 0,g?.abort(),await d.catch(()=>!1)),u=p;const h=++w,_=(async()=>{await(async()=>{try{if("undefined"==typeof navigator)return;await(navigator.storage?.persist?.())}catch{}})();const d=x(e);if(h!=w)return!1;if(!d)return v(r.unsupported),!1;if(await(async(e,t)=>{if(o&&o.id!=e.id){const e=o,t=c,n=a;a=void 0,o=void 0,c=void 0,await(n?.unload()),await(t?.remove(e))}if(await t.hasCache())for(const n of l)n.id!=e.id&&x(n)==t&&await t.remove(n)})(e,d),h!=w)return!1;const m=await d.cached(e);if(h!=w)return!1;if(!m&&await(async()=>{try{if("undefined"==typeof navigator)return!1;if(!navigator.storage?.estimate)return!1;const{quota:e=0,usage:t=0}=await navigator.storage.estimate();return!!e&&t>=e}catch{return!1}})()&&await d.purge(),h!=w)return!1;v(r.downloading),b(0,i.download),m&&b(1,i.load);const u=new AbortController;g=u;try{const l=await d.startLLM({model:e,limits:t,logging:s,signal:u.signal,assets:n,onProgress:(e,t)=>{h==w&&b(e,t)}});return h!=w||u.signal.aborted?(await l.unload(),!1):(a=l,o=e,c=d,f=d.backend,v(r.ready),b(1,i.load),!0)}catch(e){return h!=w||(a=void 0,o=void 0,c=void 0,s&&console.log("Local model failed",e),(e=>["quota","no space","disk full","ns_error_dom_quota_reached"].some(t=>`${e?.name||""} ${e?.message||`${e}`}`.toLowerCase().includes(t)))(e)&&await d.purge(),v(r.error)),!1}finally{g==u&&(g=void 0)}})().finally(()=>{d==_&&(d=void 0,m=void 0)});return d=_,m=e.id,await _},chat:async e=>{if(!k()||!a)return;for(;p;)await p.catch(()=>{});if(!k()||!a)return;const t=a.chat(e);p=t;try{return await t}finally{p==t&&(p=void 0)}},unload:async()=>{w++,g?.abort(),g=void 0,m=void 0;const e=a;a=void 0,o=void 0,c=void 0,v(r.idle),await(e?.unload())},cancelLoad:()=>{w++,g?.abort(),g=void 0,m=void 0,h==r.downloading&&v(r.idle)},ready:k,loading:()=>!k()&&h==r.downloading,status:()=>h,backend:()=>f,model:()=>o}})(),Y=(e,t,n)=>{const a=e.status(),s=d(t);return{availability:a==r.ready?o.online:a==r.error||a==r.unsupported?o.offline:o.away,status:a,backend:e.backend(),supportsImages:!!s&&m(s),ready:e.ready(),loading:e.loading(),requesting:n.requesting,phase:n.phase,percent:n.percent,...n.error?{error:n.error}:{}}},Z=(e=>{let t,n=0;const o=t=>t.input.onStatus?.(Y(e,t.input.model,t));return s=>{t?.controller.abort();const c={token:++n,input:s,controller:new AbortController,percent:e.ready()?100:0,phase:e.ready()?i.load:i.download,requesting:!1,cancelled:!1};return t=c,o(c),(async t=>{const{input:r}=t,s=!0===r.logging,c=d(r.model),l=r.messages.some(e=>e.role!=a.system&&"string"==typeof e.content&&!!e.content.trim()),g=r.messages.some(e=>e.role==a.user&&!!e.images?.some(e=>"string"==typeof e&&!!e)),u=l||g;if(!c)return t.error="Unknown model",void o(t);if(!u)return void o(t);if(g&&!l&&!m(c))return t.error="Model does not accept images",void o(t);const p=v(r.limits),f=await e.load({model:c,limits:p,logging:s,onProgress:(e,a)=>{t.token==n&&(t.percent=Math.round(100*e),t.phase=a,o(t))}}).catch(e=>(t.token==n&&(t.error=`${e?.message||`${e}`}`),!1));if(t.token!=n)return;if(!f)return t.cancelled||t.error||(t.error="Model not ready"),void o(t);const h=m(c),w=g&&!h?r.messages.map(({images:e,...t})=>t):r.messages;if(g&&!h&&!l)return t.error="Model does not accept images",void o(t);t.requesting=!0,t.error=void 0,t.percent=100,t.phase=i.load,o(t);let y,x="",b="";try{y=await e.chat({messages:w,limits:p,reasoning:!0===r.reasoning,logging:s,onReason:e=>{t.token!=n||t.cancelled||(x+=e,r.onThinking?.(x))},onText:e=>{t.token!=n||t.cancelled||(b+=e,r.onResponse?.(b))},signal:t.controller.signal})}catch(e){if(t.token!=n)return;return t.requesting=!1,t.error=`${e?.message||`${e}`}`,void o(t)}t.token==n&&(t.requesting=!1,y||t.cancelled||(t.error="No reply"),o(t))})(c),()=>(t=>{t.token==n&&(t.cancelled=!0,t.controller.abort(),e.status()!=r.ready?(e.cancelLoad(),o(t)):e.unload().then(()=>o(t)))})(c)}})(X),ee=(te=X,async e=>{const t=d(e.model),n=t=>e.onStatus?.(Y(te,e.model,t));if(!t)return n({requesting:!1,phase:i.download,percent:0,error:"Unknown model"}),!1;let a,o=0,r=i.download;const s=te.ready()&&te.model()?.id==t.id;return await te.load({model:t,limits:v(e.limits),logging:!0===e.logging,onProgress:(e,t)=>{o=Math.round(100*e),r=t,n({requesting:!1,phase:r,percent:o})}}).catch(e=>(a=`${e?.message||`${e}`}`,!1))?(s&&n({requesting:!1,phase:i.load,percent:100}),!0):(n({requesting:!1,phase:r,percent:o,error:a||"Model not ready"}),!1)});var te;export{c as ChatModelId,o as ChatStatus,s as LlmBackend,i as LlmPhase,a as LlmRole,r as LlmStatus,Z as createChat,ee as preloadModel};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! For license information please see engine-litert.min.mjs.LICENSE.txt */
|
|
2
|
+
export const __webpack_esm_ids__=[520];export const __webpack_esm_modules__={507(e,t,s){s.r(t),s.d(t,{AutoToolChat:()=>R,Backend:()=>U,Conversation:()=>$,EmbeddingEngine:()=>ce,Engine:()=>te,InputOverflowStrategy:()=>N,LiteRtLm:()=>g,LiteRtLmNotLoadedError:()=>i,SUPPORTED_AUDIO_MIME_TYPES:()=>ne,SUPPORTED_IMAGE_MIME_TYPES:()=>se,SamplerType:()=>G,Session:()=>Q,Wasm:()=>n,fillWasmEmbeddingEngineSettingsFromEmbeddingEngineSettings:()=>ie,fillWasmEngineSettingsFromEngineSettings:()=>W,getGlobalLiteRtLm:()=>o,getGlobalLiteRtLmPromise:()=>u,getOrLoadGlobalLiteRtLm:()=>T,hasGlobalLiteRtLm:()=>c,hasGlobalLiteRtLmPromise:()=>d,isWebMcpTool:()=>B,loadLiteRtLm:()=>E,sessionConfigToWasmSessionConfig:()=>K,setGlobalLiteRtLm:()=>l,setGlobalLiteRtLmPromise:()=>h,setupDefaultWebGpuDevice:()=>m,toTool:()=>C,unloadLiteRtLm:()=>_,wasmEngineSettingsToEngineSettings:()=>q,wasmSessionConfigToSessionConfig:()=>Y});var n={};s.r(n),s.d(n,{Backend:()=>U,InputOverflowStrategy:()=>N,ReadableStreamDataStreamWrapper:()=>I,SamplerType:()=>G});class i extends Error{constructor(){super("LiteRT-LM is not initialized yet. Please call loadLiteRtLm() and wait for its promise to resolve to load the LiteRT-LM WASM module.")}}let a,r;function o(){if(!a)throw new i;return a}function c(){return Boolean(a)}function l(e){a=e}function u(){return r}function d(){return Boolean(r)}function h(e){r=e}async function m(){await o().setupDefaultWebGpuDevice()}class g{liteRtLmWasm;static DEFAULT_WASM_PATH="https://cdn.jsdelivr.net/npm/@litert-lm/core@0.19.0-nightly.20261024/wasm";constructor(e){this.liteRtLmWasm=e,this.liteRtLmWasm.setupLogging()}async setupDefaultWebGpuDevice(){this.liteRtLmWasm.preinitializedWebGPUDevice||(this.liteRtLmWasm.preinitializedWebGPUDevice=await async function(){const e={powerPreference:"high-performance"},t=await navigator.gpu.requestAdapter(e);if(!t)throw new Error("No GPU adapter found.");const s={maxBufferSize:t.limits.maxBufferSize,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxStorageBuffersPerShaderStage:t.limits.maxStorageBuffersPerShaderStage,maxTextureDimension2D:t.limits.maxTextureDimension2D},n=[];for(const e of f)t.features.has(e)&&n.push(e);return await t.requestDevice({requiredFeatures:n,requiredLimits:s})}())}delete(){}}const f=["shader-f16","subgroups"];async function p(e){if("function"!=typeof importScripts){const t=document.createElement("script");return t.src=e.toString(),t.crossOrigin="anonymous",new Promise((e,s)=>{t.addEventListener("load",()=>{e()},!1),t.addEventListener("error",e=>{s(e)},!1),document.body.appendChild(t)})}importScripts(e.toString())}const y=new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,15,1,13,0,65,1,253,15,65,2,253,15,253,128,2,11]),w=new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]),b={relaxedSimd:void 0,threads:void 0,jspi:void 0};async function v(e){try{return await WebAssembly.instantiate(e),{supported:!0}}catch(e){return{supported:!1,error:e}}}const S={relaxedSimd:()=>(void 0===b.relaxedSimd&&(b.relaxedSimd=v(y)),b.relaxedSimd),threads:()=>{if(void 0===b.threads)try{"undefined"!=typeof MessageChannel&&(new MessageChannel).port1.postMessage(new SharedArrayBuffer(1)),b.threads=v(w)}catch(e){b.threads=Promise.resolve({supported:!1,error:e})}return b.threads},jspi:()=>{if(void 0===b.jspi)try{const e="Suspending"in WebAssembly;b.jspi=Promise.resolve({supported:e,error:e?void 0:new Error("JSPI is not supported")})}catch(e){b.jspi=Promise.resolve({supported:!1,error:e})}return b.jspi}};async function k(e){const t=S[e]?.();if(!t)throw new Error(`Unknown feature: ${e}`);return(await t).supported}async function x(e,t){const s=e,n=await k("relaxedSimd"),i=await k("jspi");let a="";a=n?i?"litertlm_wasm_internal.js":"litertlm_wasm_asyncify_internal.js":i?"litertlm_wasm_compat_internal.js":"litertlm_wasm_compat_asyncify_internal.js";let r=e;if(s.endsWith(".wasm"))throw new Error("Please load the `.js` file corresponding to the `.wasm` file, or load the directory containing it.");return s.endsWith(".js")||(r=function(e,t){return e?t?(e.endsWith("/")?e:e+"/")+(t.startsWith("/")?t.substring(1):t):e:t}(e,a)),(async(e,t,s,n,i)=>{if(t&&await p(t),!self.ModuleFactory)throw new Error("ModuleFactory not set.");if(s&&(await p(s),!self.ModuleFactory))throw new Error("ModuleFactory not set.");if(self.Module&&i){const e=self.Module;e.locateFile=i.locateFile,i.mainScriptUrlOrBlob&&(e.mainScriptUrlOrBlob=i.mainScriptUrlOrBlob)}const a=await self.ModuleFactory(self.Module||i);return self.ModuleFactory=self.Module=void 0,new e(a,n)})(g,r)}let P;function E(e,t){if(d())throw new Error("LiteRT-LM is already loading / loaded.");return h(x(e).then(async e=>(l(e),e)).catch(e=>{throw h(void 0),e})),u()}function _(){if(d()&&!c())throw new Error("LiteRT-LM is loading and can not be unloaded or canceled until it is finished loading.");c()&&(o().delete(),l(void 0),P=void 0),h(void 0)}function T(e){const t=u();if(t){if(e&&P!==e)throw new Error(`LiteRT-LM is already loading / loaded with a different path ${P} but was requested to load ${e}. To reload with a different path, call unloadLiteRtLm() first.`);return t}return P=e??g.DEFAULT_WASM_PATH,t??E(P)}function B(e){return"inputSchema"in e}function C(e){const{inputSchema:t,execute:s,...n}=e;return{type:"function",function:{...n,parameters:t}}}const A="Conversation is busy. A generation is already in progress.";class R{options;baseConversation=null;isBusy=!1;isCancelled=!1;tools=new Map;nextToolCallId=1;currentBatch=null;constructor(e){if(this.options=e,e.config?.preface?.tools&&e.config.preface.tools.length>0)throw new Error("AutoToolChat handles tools itself. Do not provide tools via ConversationConfig.preface.tools.");const t=e.tools||[];for(const e of t){let t;if(B(e))t={...C(e),execute:e.execute};else if("function"in e)t=e;else{const{execute:s,...n}=e;t={type:"function",function:n,execute:s}}const s=t.function.name;if(!s)throw new Error("Tool must have a name.");if(!t.execute)throw new Error(`Tool ${s} must have an execute function.`);if(this.tools.has(s))throw new Error(`Duplicate tool name ${s} provided.`);this.tools.set(s,t)}}async getConversation(){if(!this.baseConversation){const e=this.options.config?{...this.options.config}:{};this.tools.size>0&&(e.preface||(e.preface={}),e.preface.tools=Array.from(this.tools.values()).map(e=>{const{execute:t,...s}=e;return s})),this.baseConversation=await this.options.engine.createConversation(e)}return this.baseConversation}async sendMessage(e){if(this.isBusy)throw new Error(A);this.isBusy=!0,this.isCancelled=!1;try{const t=await this.getConversation();let s=e;const n=this.options.recurringToolCallLimit??25;let i=0;for(;i<n;){i++;const e=await t.sendMessage(s);if(e.tool_calls&&e.tool_calls.length>0){const t=await this.executeToolCalls(e.tool_calls);if(this.isCancelled)throw new Error("Conversation cancelled");s={role:"tool",content:t};continue}return e}throw new Error("Tool calling exceeded the recurring limit")}finally{this.isBusy=!1}}sendMessageStreaming(e){if(this.isBusy)throw new Error(A);this.isBusy=!0,this.isCancelled=!1;let t=!1;return new ReadableStream({start:s=>{let n=e,i=0;const a=this.options.recurringToolCallLimit??25,r=async()=>{if(i>=a)return this.isBusy=!1,void s.error(new Error("Tool calling exceeded the recurring limit"));i++;let e=[];try{const i=(await this.getConversation()).sendMessageStreaming(n);for await(const n of i){if(t||this.isCancelled)break;if(n.tool_calls&&n.tool_calls.length>0){e=e.concat(n.tool_calls);const t={...n};delete t.tool_calls,t.content&&s.enqueue(t)}else s.enqueue(n)}}catch(e){return this.isBusy=!1,void s.error(e)}if(!t&&!this.isCancelled){if(e.length>0){const s=await this.executeToolCalls(e);if(t||this.isCancelled)return;return n={role:"tool",content:s},void await r()}this.isBusy=!1,s.close()}};r().catch(e=>{t||this.isCancelled||(this.isBusy=!1,s.error(e))})},cancel:()=>{t=!0,this.cancel()}})}cancel(){this.isCancelled=!0,this.currentBatch&&this.currentBatch.cancel(),this.baseConversation&&this.baseConversation.cancel()}async getHistory(){return(await this.getConversation()).getHistory()}async getTokenCount(){return(await this.getConversation()).getTokenCount()}async getBenchmarkInfo(){return(await this.getConversation()).getBenchmarkInfo()}async delete(){this.baseConversation&&(await this.baseConversation.delete(),this.baseConversation=null)}async executeToolCalls(e){this.currentBatch=new M(this.tools,this.options.onToolProgress,()=>this.nextToolCallId++);try{return await this.currentBatch.executeAll(e)}finally{this.currentBatch=null}}}class M{tools;onToolProgress;getNextId;isCancelled=!1;constructor(e,t,s){this.tools=e,this.onToolProgress=t,this.getNextId=s}cancel(){this.isCancelled=!0}async executeAll(e){return Promise.all(e.map(async e=>{const t=this.tools.get(e.function.name),s=this.getNextId();let n;if(this.onToolProgress&&this.onToolProgress({id:s,name:e.function.name,arguments:e.function.arguments,status:"started"}),t)try{n=await t.execute(e.function.arguments),!this.isCancelled&&this.onToolProgress&&this.onToolProgress({id:s,name:e.function.name,arguments:e.function.arguments,status:"completed",result:n})}catch(t){const i=String(t);n={error:`Error executing tool ${e.function.name}: ${i}`},!this.isCancelled&&this.onToolProgress&&this.onToolProgress({id:s,name:e.function.name,arguments:e.function.arguments,status:"error",error:i})}else{const t=`Tool ${e.function.name} not found.`;n={error:t},!this.isCancelled&&this.onToolProgress&&this.onToolProgress({id:s,name:e.function.name,arguments:e.function.arguments,status:"error",error:t})}return{type:"tool_response",name:e.function.name,response:n}}))}}class L{size;data;fullyDiscarded=!1;discardedRegions=[];constructor(e){this.size=e}discard(e,t){if(this.fullyDiscarded)return;if(0===this.discardedRegions.length)return this.discardedRegions.push([e,t]),void this.checkFullyDiscarded();this.discardedRegions.push([e,t]),this.discardedRegions.sort((e,t)=>e[0]-t[0]);const s=[];for(const e of this.discardedRegions){const t=s[s.length-1];!t||t[1]<e[0]?s.push(e):t[1]=Math.max(t[1],e[1])}this.discardedRegions=s,this.checkFullyDiscarded()}getDiscardedRegions(e=0,t=this.size){const s=[];for(const[n,i]of this.discardedRegions){if(n>=t)break;i<=e||s.push([n,i])}return s}checkFullyDiscarded(){1===this.discardedRegions.length&&0===this.discardedRegions[0][0]&&this.discardedRegions[0][1]===this.size&&(this.fullyDiscarded=!0,this.data=void 0)}}class I{stream;getWasmHeap;bytesPerSlice;slices=[];streamDone=!1;globalPosition=0;reader;leftoverData;isReading=!1;constructor(e,t,s=1e7){this.stream=e,this.getWasmHeap=t,this.bytesPerSlice=s;try{this.reader={byobReader:this.stream.getReader({mode:"byob"})}}catch(e){this.reader={defaultReader:this.stream.getReader()}}}*getViews(e,t){if(e>=t)return;let s,n,i=e;for(;i<t;){const e=Math.floor(i/this.bytesPerSlice),a=i%this.bytesPerSlice,r=e*this.bytesPerSlice;let o=this.slices[e];o||(s&&n&&n.fullyDiscarded||(s=new Uint8Array(this.bytesPerSlice)),o=new L(this.bytesPerSlice),o.data=s,this.slices[e]=o);const c=Math.min(this.bytesPerSlice,t-r),l=()=>{o.discard(a,c)},u=c-a,d=e=>{o.data=new Uint8Array(e.buffer,0,e.buffer.byteLength),s=o.data},h=()=>{const t=o.getDiscardedRegions(a,c);if(t.length>0){const s=t.map(([e,t])=>`[${e+r}, ${t+r}]`);throw new Error(`Slice ${e} for data at ${i} overlaps discarded regions: [${s.join(", ")}]`)}if(!o.data)throw new Error(`No ArrayBuffer for slice ${e} at position ${i}`)},m=()=>(h(),new Uint8Array(o.data.buffer,o.data.byteOffset+a,o.data.byteOffset+u)),g=()=>{if(o.data)return new Uint8Array(o.data.buffer,o.data.byteOffset+a,u)};yield{getView:m,getUncheckedView:g,discardView:l,setView:d,checkReadable:h,viewLength:u},n=o,i+=u}}async readInternal(e,t,s,n){if(this.isReading)throw new Error("Concurrent reads are not supported");this.isReading=!0;try{let i;for(const{getUncheckedView:e,setView:s,viewLength:n}of this.getViews(this.globalPosition,t)){const t=e();let a;t?a=t:((!i||i.byteLength<n)&&(i=new ArrayBuffer(this.bytesPerSlice)),a=new Uint8Array(i,0,n));const r=await this.fillBufferFromStream(a);if(t?s(r):i=r.buffer,0===r.length)break}const a=Math.min(this.globalPosition,t+s);for(const{getView:s,discardView:i}of this.getViews(t,a)){const t=s();this.getWasmHeap().set(t,e),n&&i(),e+=t.length}const r=Math.max(a,t);for(const{getView:i,setView:a,discardView:o}of this.getViews(r,t+s)){if(this.streamDone&&this.globalPosition<=r)break;const t=await this.fillBufferFromStream(i());a(t),this.getWasmHeap().set(t,e),n&&o(),e+=t.length}}finally{this.isReading=!1}}async fillBufferFromStream(e){const t=e.byteLength;if(0===t)return e;const s=e.byteOffset;let n=0,i=e.buffer;for(;n<t;){if(this.streamDone&&(!this.leftoverData||0===this.leftoverData.length))throw new Error(`Read from stream returned early EOF at position ${this.globalPosition}. Expected ${t-n} more bytes.`);if(this.reader.byobReader){const e=new Uint8Array(i,s+n,t-n),a=await this.reader.byobReader.read(e);if(a.done&&(this.streamDone=!0),!a.value)throw new Error("Read from stream returned null value");this.globalPosition+=a.value.byteLength,n+=a.value.byteLength,i=a.value.buffer}else{let e;if(this.leftoverData&&this.leftoverData.length>0)e=this.leftoverData,this.leftoverData=void 0;else{const t=await this.reader.defaultReader.read();if(t.done&&(this.streamDone=!0,!t.value))continue;if(!t.value)throw new Error("Read from stream returned null value");e=t.value}const a=t-n,r=Math.min(e.length,a);new Uint8Array(i,s+n,r).set(e.subarray(0,r)),e.length>r&&(this.leftoverData=e.subarray(r)),this.globalPosition+=r,n+=r}}const a=new Uint8Array(i,s,n);if(a.length!==t)throw new Error(`Read from stream returned ${a.length} bytes, expected ${t}`);return a}doDiscard(e,t){for(const{discardView:s}of this.getViews(e,e+t))s()}async readAndDiscard(e,t,s){try{return await this.readInternal(e>>>0,D(t),D(s),!0),{}}catch(e){return{error:e}}}async readAndPreserve(e,t,s){try{return await this.readInternal(e>>>0,D(t),D(s),!1),{}}catch(e){return{error:e}}}discard(e,t){try{return this.doDiscard(D(e),D(t)),{}}catch(e){return{error:e}}}}function D(e){if("number"==typeof e)return e;if(e>Number.MAX_SAFE_INTEGER)throw new Error(`BigInt value ${e} too large to be represented as a number`);return Number(e)}const U={UNSPECIFIED:0,CPU_ARTISAN:1,GPU_ARTISAN:2,CPU:3,GPU:4,GOOGLE_TENSOR_ARTISAN:5,NPU:6},G={TYPE_UNSPECIFIED:0,TOP_K:1,TOP_P:2,GREEDY:3},N={CHUNK_AND_AVERAGE:0,TRUNCATE:1,ERROR:2};function O(e){const t=new Array(e.size());for(let s=0;s<e.size();++s)t[s]=e.get(s);return e.delete(),t}function V(e,t){for(const s of e)t.push_back(s)}function W(e,t,s,n=o().liteRtLmWasm){const i=e.getMutableMainExecutorSettings();if(i.setCacheDir(":nocache"),t.benchmarkEnabled&&e.enableBenchmark(),void 0!==t.maxVisionTokensPerImage&&e.setMaxVisionTokensPerImage(t.maxVisionTokensPerImage),void 0!==t.minVisionTokensPerImage&&e.setMinVisionTokensPerImage(t.minVisionTokensPerImage),t.mainExecutorSettings){const e=t.mainExecutorSettings;if(void 0!==e.maxNumTokens&&i.setMaxNumTokens(e.maxNumTokens),void 0!==e.maxNumImages&&i.setMaxNumImages(e.maxNumImages),void 0!==e.samplerBackend&&i.setSamplerBackend({value:e.samplerBackend}),void 0!==e.backendConfig){const t=e.backendConfig;if(s===U.CPU){const e=t,s={kv_increment_size:e.kv_increment_size??0,prefill_chunk_size:e.prefill_chunk_size??0,number_of_threads:e.number_of_threads??0};i.setBackendConfigCpu(s)}else if(s===U.GPU){const e=t,s={max_top_k:e.max_top_k??1,external_tensor_mode:e.external_tensor_mode??!1};i.setBackendConfigGpu(s)}else if(s===U.GPU_ARTISAN){const e=t,s=new n.VectorUint32;V(e.supported_lora_ranks??[],s);const a={num_output_candidates:e.num_output_candidates??1,wait_for_weight_uploads:e.wait_for_weight_uploads??!0,num_decode_steps_per_sync:e.num_decode_steps_per_sync??1,sequence_batch_size:e.sequence_batch_size??0,supported_lora_ranks:s,max_top_k:e.max_top_k??1,enable_decode_logits:e.enable_decode_logits??!1,enable_external_embeddings:e.enable_external_embeddings??!1,use_submodel:e.use_submodel??!0,use_autosized_ringbuffers:e.use_autosized_ringbuffers??!0};i.setBackendConfigGpuArtisan(a),s.delete()}}if(void 0!==e.advancedSettings){const t={...i.getAdvancedSettings(),...e.advancedSettings};i.setAdvancedSettings(t)}}}function q(e){const t=e.getMutableMainExecutorSettings();let s,n;try{s=t.getBackendConfigCpu(),n=U.CPU}catch(e){try{s=t.getBackendConfigGpu(),n=U.GPU}catch(e){try{const e=t.getBackendConfigGpuArtisan();s={...e,supported_lora_ranks:O(e.supported_lora_ranks)},n=U.GPU_ARTISAN}catch(e){throw new Error("Unsupported backend config")}}}return{backend:n,visionBackend:e.getVisionBackend()?.value,maxVisionTokensPerImage:e.getMaxVisionTokensPerImage(),minVisionTokensPerImage:e.getMinVisionTokensPerImage(),audioBackend:e.getAudioBackend()?.value,mainExecutorSettings:{maxNumTokens:t.getMaxNumTokens(),maxNumImages:t.getMaxNumImages(),samplerBackend:t.getSamplerBackend().value,backendConfig:s,advancedSettings:t.getAdvancedSettings()},benchmarkEnabled:!1}}class z{callbacks=new Set;add(e){return this.callbacks.add(e),()=>{this.callbacks.delete(e),e()}}run(){for(const e of this.callbacks)e();this.callbacks.clear()}}const F="Conversation is busy. A generation is already in progress.";class ${conversation;engine;mutexes;visualTokenBudget;isBusy=!1;constructor(e,t,s,n=0){this.conversation=e,this.engine=t,this.mutexes=s,this.visualTokenBudget=n}async sendMessage(e){if(this.isBusy)throw new Error(F);this.isBusy=!0;try{const{json:t,blobs:s}=await j(e);return await this.mutexes.executor.acquireAndRun(async()=>{const e=await this.conversation.sendMessage(t,s,this.visualTokenBudget);return JSON.parse(e)})}finally{this.isBusy=!1}}sendMessageStreaming(e){if(this.isBusy)throw new Error(F);this.isBusy=!0;let t=!1,s=!1;return new ReadableStream({start:n=>{(async()=>{const{json:i,blobs:a}=await j(e);await this.mutexes.executor.acquireAndRun(async()=>{await this.conversation.sendMessageAsync(i,a,(e,i,a)=>{if(!t&&!s){if(a)return s=!0,this.isBusy=!1,void n.error(new Error(a));if(e)try{const t=JSON.parse(e);n.enqueue(t)}catch(e){s=!0,this.isBusy=!1,n.error(e)}}},this.visualTokenBudget),await this.engine.waitUntilDone()}),t||s||(this.isBusy=!1,n.close())})().catch(e=>{t||s||(this.isBusy=!1,n.error(e))})},cancel:()=>{t=!0,this.isBusy=!1,this.cancel()}})}cancel(){this.conversation.cancelProcess()}async getHistory(){return this.mutexes.executor.acquireAndRun(()=>{const e=this.conversation.getHistory();return JSON.parse(e)})}async getTokenCount(){return this.mutexes.executor.acquireAndRun(()=>this.conversation.getTokenCount())}async getBenchmarkInfo(){return this.mutexes.executor.acquireAndRun(()=>this.conversation.getBenchmarkInfo())}async clone(){if(this.isBusy)throw new Error(F);this.isBusy=!0;try{return await this.mutexes.executor.acquireAndRun(()=>{const e=this.conversation.clone();return new $(e,this.engine,this.mutexes,this.visualTokenBudget)})}finally{this.isBusy=!1}}async delete(){await this.mutexes.executor.acquireAndRun(()=>{this.conversation.delete()})}}async function j(e){const t=[],s=await async function(e,t){const s=function(e){if("string"==typeof e)return{role:"user",content:e};return e}(e);if(!Array.isArray(s.content))return s;const n=[];for(const e of s.content){if("image"!==e.type&&"audio"!==e.type||!("data"in e)||void 0===e.data){n.push(e);continue}const{data:s,...i}=e;if("string"==typeof s){n.push({...i,blob:s});continue}const a=await H(s),r=t.length;t.push(a),n.push({...i,blob_index:r})}return{...s,content:n}}(e,t);return{json:JSON.stringify(s),blobs:t}}async function H(e){return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof ArrayBuffer?new Uint8Array(e):e}class J{mutexPromise=Promise.resolve();queueCount=0;get isLocked(){return this.queueCount>0}async acquireAndRun(e){this.queueCount++;const t=this.mutexPromise;let s;this.mutexPromise=new Promise(e=>{s=e});try{return await t,await e()}finally{this.queueCount--,s()}}}function K(e,t=o().liteRtLmWasm){const s=t.SessionConfig.createDefault();if(void 0!==e.audioModalityEnabled&&s.setAudioModalityEnabled(e.audioModalityEnabled),void 0!==e.visionModalityEnabled&&s.setVisionModalityEnabled(e.visionModalityEnabled),void 0!==e.samplerParams){const t=s.getMutableSamplerParams();void 0!==e.samplerParams.type&&t.setType({value:e.samplerParams.type}),void 0!==e.samplerParams.k&&t.setK(e.samplerParams.k),void 0!==e.samplerParams.p&&t.setP(e.samplerParams.p),void 0!==e.samplerParams.temperature&&t.setTemperature(e.samplerParams.temperature),void 0!==e.samplerParams.seed&&t.setSeed(e.samplerParams.seed)}if(void 0!==e.startTokenId&&s.setStartTokenId(e.startTokenId),void 0!==e.numOutputCandidates&&s.setNumOutputCandidates(e.numOutputCandidates),void 0!==e.samplerBackend&&s.setSamplerBackend({value:e.samplerBackend}),void 0!==e.applyPromptTemplateInSession&&s.setApplyPromptTemplateInSession(e.applyPromptTemplateInSession),void 0!==e.useExternalSampler&&s.setUseExternalSampler(e.useExternalSampler),void 0!==e.maxOutputTokens&&s.setMaxOutputTokens(e.maxOutputTokens),void 0!==e.stopTokenIds){const n=new t.VectorVectorInt;for(const s of e.stopTokenIds){const e=new t.VectorInt;V(s,e),n.push_back(e),e.delete()}s.setStopTokenIds(n),n.delete()}return s}function Y(e){const t=e.getMutableSamplerParams();return{audioModalityEnabled:e.getAudioModalityEnabled(),visionModalityEnabled:e.getVisionModalityEnabled(),samplerParams:{type:t.type().value,k:t.k(),p:t.p(),temperature:t.temperature(),seed:t.seed()},stopTokenIds:O(e.getStopTokenIds()).map(O),startTokenId:e.getStartTokenId(),numOutputCandidates:e.getNumOutputCandidates(),samplerBackend:e.getSamplerBackend().value,applyPromptTemplateInSession:e.getApplyPromptTemplateInSession(),useExternalSampler:e.getUseExternalSampler(),maxOutputTokens:e.getMaxOutputTokens()}}const X="Session is busy. An operation is already in progress.";class Q{session;mutexes;config;isBusy=!1;constructor(e,t){this.session=e,this.mutexes=t;const s=e.getSessionConfig();this.config=Y(s),s.delete()}async runPrefill(e){if(this.isBusy)throw new Error(X);this.isBusy=!0;try{await this.mutexes.executor.acquireAndRun(async()=>{await this.session.runPrefill(e)})}finally{this.isBusy=!1}}async runDecode(){if(this.isBusy)throw new Error(X);this.isBusy=!0;try{return await this.mutexes.executor.acquireAndRun(async()=>{const e=await this.session.runDecode();return new Z(e)})}finally{this.isBusy=!1}}cancel(){this.session.cancelProcess()}async clone(){if(this.isBusy)throw new Error(X);this.isBusy=!0;try{return await this.mutexes.executor.acquireAndRun(()=>{const e=this.session.clone();return new Q(e,this.mutexes)})}finally{this.isBusy=!1}}async delete(){await this.mutexes.executor.acquireAndRun(()=>{this.session.delete()})}}class Z{responses;constructor(e){this.responses=e}getTexts(){return O(this.responses.getTexts())}delete(){this.responses.delete()}}async function ee(e,t,s){const n=await async function(e){if(e instanceof ReadableStream)return e;if(e instanceof Blob)return e.stream();const t=e,s=await fetch(t,{credentials:"same-origin"});if(!s.ok)throw new Error(`Failed to fetch model file from ${t}`);return s.body}(e),i=new I(n,()=>t.HEAPU8),a=t.ReadableStreamDataStream.create(i);s.add(()=>{a.delete()});const r=t.ModelAssets.createStreaming(a),o=s.add(()=>{r.delete()});return{modelAssets:r,cleanupModelAssets:o}}class te{wasm;engine;deleteCallback;settings;mutexes={executor:new J};constructor(e,t,s,n){this.wasm=e,this.engine=t,this.deleteCallback=n;const i=t.getEngineSettings(),a=q(i);this.settings={...a,model:s},i.delete()}static async create(e,t=""){const s=await T(),n=s.liteRtLmWasm,i=e.backend??U.GPU;e={...e,backend:i};const a=e.mainExecutorSettings?.samplerBackend,r=e.visionBackend,o=e.audioBackend;i!==U.GPU&&i!==U.GPU_ARTISAN&&a!==U.GPU&&a!==U.GPU_ARTISAN&&r!==U.GPU&&o!==U.GPU||await s.setupDefaultWebGpuDevice();const c=new z;let l;try{const{modelAssets:s,cleanupModelAssets:a}=await ee(e.model,n,c),u=void 0===r&&void 0===o?await n.EngineSettings.createDefault(s,{value:i}):await n.EngineSettings.createDefaultMultimodal(s,{value:i},void 0===r?void 0:{value:r},void 0===o?void 0:{value:o});a();const d=c.add(()=>{u.delete()}),h=u.getMutableMainExecutorSettings().getBackend().value;W(u,e,h,n),u.setParallelFileSectionLoading(!1),u.setSingleThreadedExecution(!0);try{l=h===U.GPU_ARTISAN?await n.Engine.createLegacyEngine(u,t):await n.Engine.createEngine(u,t)}finally{if(h===U.GPU||r===U.GPU||o===U.GPU)try{await n.clearStoredWeightsStreams()}catch(e){console.error("Error clearing stored weights streams:",e)}}return d(),c.add(()=>{l.delete()}),new te(n,l,e.model,()=>c.run())}catch(e){throw c.run(),e}}withDefaultModalities(e){const t={...e};return void 0!==this.settings.visionBackend&&void 0===t.visionModalityEnabled&&(t.visionModalityEnabled=!0),void 0!==this.settings.audioBackend&&void 0===t.audioModalityEnabled&&(t.audioModalityEnabled=!0),t}async createSession(e={}){return this.mutexes.executor.acquireAndRun(()=>{const t=K(this.withDefaultModalities(e),this.wasm),s=this.engine.createSession(t);return t.delete(),new Q(s,this.mutexes)})}async createConversation(e){return e||void 0===this.settings.visionBackend&&void 0===this.settings.audioBackend||(e={filterChannelContentFromKvCache:!0}),this.mutexes.executor.acquireAndRun(async()=>{let t,s;if(e){s=K(this.withDefaultModalities(e.sessionConfig||{}),this.wasm);const n=e.preface?JSON.stringify(e.preface):"",i=this.settings.backend===U.GPU_ARTISAN&&Boolean(this.settings.mainExecutorSettings?.backendConfig?.use_autosized_ringbuffers),a=e.filterChannelContentFromKvCache??i;t=this.wasm.ConversationConfig.createCustom(this.engine,s,!!e.enableConstrainedDecoding,!!e.prefillPrefaceOnInit,a,n)}else t=this.wasm.ConversationConfig.createDefault(this.engine);const n=await this.wasm.Conversation.create(this.engine,t);return t.delete(),s&&s.delete(),new $(n,this.engine,this.mutexes,this.settings.maxVisionTokensPerImage??0)})}async delete(){await this.mutexes.executor.acquireAndRun(()=>{this.deleteCallback()})}}const se=new Set(["image/jpeg","image/png","image/bmp","image/gif","image/x-tga","image/vnd.adobe.photoshop","image/vnd.radiance","image/x-portable-pixmap","image/x-portable-graymap"]),ne=new Set(["audio/wav","audio/x-wav","audio/wave","audio/mp3","audio/mpeg","audio/flac","audio/x-flac"]);function ie(e,t){const s=e.getMutableMainExecutorSettings();s.setCacheDir(":nocache");const n=t.maxInputLength??1024;if(e.setMaxInputLength(n),e.setMinInputLength(t.minInputLength??n),void 0!==t.visionTokensPerImage&&e.setVisionTokensPerImage(t.visionTokensPerImage),t.mainExecutorSettings){const e=t.mainExecutorSettings;void 0!==e.numThreads&&s.setNumThreads(e.numThreads)}}function ae(e){return{normalize:e?.normalize??!0,insertSpecialTokens:e?.insertSpecialTokens??!0,inputOverflowStrategy:e?.inputOverflowStrategy??N.ERROR,visionTokensPerImage:e?.visionTokensPerImage,outputSize:e?.outputSize}}async function re(e){if("string"==typeof e)return{type:"text",text:e};if(e instanceof Blob){const t=e.type.toLowerCase().split(";")[0].trim();if(se.has(t)){return{type:"image",data:await e.arrayBuffer()}}if(ne.has(t)){return{type:"audio",data:await e.arrayBuffer()}}throw new Error(`Unsupported Blob mime type: "${e.type}". Supported image types: ${[...se].join(", ")}. Supported audio types: ${[...ne].join(", ")}.`)}if("object"==typeof e&&null!==e){if("image"===e.type){if(e.data instanceof Blob){return{type:"image",data:await e.data.arrayBuffer()}}return e}if("audio"===e.type){if(e.data instanceof Blob){return{type:"audio",data:await e.data.arrayBuffer()}}return e}return e}throw new Error(`Unsupported input part: ${JSON.stringify(e)}`)}async function oe(e){return Array.isArray(e)?Promise.all(e.map(e=>re(e))):re(e)}class ce{engine;deleteCallback;settings;mutex=new J;constructor(e,t,s){this.engine=e,this.deleteCallback=s,this.settings=t}static async create(e){const t=await T(),s=t.liteRtLmWasm,n=e.backend??U.GPU;e={...e,backend:n},n!==U.GPU&&n!==U.GPU_ARTISAN&&e.visionBackend!==U.GPU&&e.audioBackend!==U.GPU||await t.setupDefaultWebGpuDevice();const i=new z;let a;try{const{modelAssets:t,cleanupModelAssets:r}=await ee(e.model,s,i),o=s.EmbeddingEngineSettings.createDefaultMultimodal(t,{value:n},void 0!==e.visionBackend?{value:e.visionBackend}:void 0,void 0!==e.audioBackend?{value:e.audioBackend}:void 0);r();const c=i.add(()=>{o.delete()}),l=o.getMutableMainExecutorSettings().getBackend().value;ie(o,e);try{a=await s.EmbeddingEngine.createEngine(o)}finally{if(l===U.GPU||e.visionBackend===U.GPU||e.audioBackend===U.GPU)try{await s.clearStoredWeightsStreams()}catch(e){console.error("Error clearing stored weights streams:",e)}}return c(),i.add(()=>{a.delete()}),new ce(a,e,()=>i.run())}catch(e){throw i.run(),e}}async computeEmbedding(e,t={}){const s=await oe(e);return this.mutex.acquireAndRun(async()=>{const e=await this.engine.computeEmbedding(s,ae(t));return{embedding:O(e.embedding),inputLength:e.inputLength,truncatedLength:e.truncatedLength,numChunks:e.numChunks}})}async computeEmbeddingBatch(e,t={}){const s=await Promise.all(e.map(oe));return this.mutex.acquireAndRun(async()=>{const e=await this.engine.computeEmbeddingBatch(s,ae(t));try{const t=[],s=e.size();for(let n=0;n<s;n++){const s=e.get(n),i=O(s.embedding);t.push({embedding:i,inputLength:s.inputLength,truncatedLength:s.truncatedLength,numChunks:s.numChunks})}return t}finally{e.delete()}})}async delete(){await this.mutex.acquireAndRun(()=>{this.deleteCallback()})}}}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/*! MIT License. DegreeSign Chat SDK */
|
package/dist/engines/engine.d.ts
CHANGED
|
@@ -10,6 +10,11 @@ declare const replyOptions: (limits: LlmLimits, reasoning: boolean) => {
|
|
|
10
10
|
max_tokens: number;
|
|
11
11
|
};
|
|
12
12
|
declare const
|
|
13
|
+
/** Resolve one model file on the model host */
|
|
14
|
+
modelUrl: (repo: string, file: string) => string,
|
|
15
|
+
/** Whether any turn carries an image */
|
|
16
|
+
hasImages: (messages: EngineMessage[]) => boolean;
|
|
17
|
+
declare const
|
|
13
18
|
/** Map the host messages to OpenAI-style turns, bounded for local context */
|
|
14
19
|
buildMessages: (messages: ChatMessage[], limits?: LlmLimits) => EngineMessage[],
|
|
15
20
|
/** Split inline reasoning markers out of the streamed content */
|
|
@@ -25,4 +30,4 @@ streamReply: ({ model, logging, onReason, onText, start, stopped, }: StreamReply
|
|
|
25
30
|
declare const
|
|
26
31
|
/** Build the prompt and stream one reply through an engine */
|
|
27
32
|
runEngineChat: ({ model, params, start, stopped, }: EngineRunParams) => Promise<string | undefined>;
|
|
28
|
-
export { buildMessages, createThinkSplitter, logChatCall, runEngineChat, streamReply, replyOptions, defaultLlmLimits, resolveLlmLimits, };
|
|
33
|
+
export { buildMessages, createThinkSplitter, logChatCall, runEngineChat, streamReply, replyOptions, defaultLlmLimits, resolveLlmLimits, hasImages, modelUrl, };
|
package/dist/engines/images.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { EngineMessage } from '../types';
|
|
2
2
|
/** Square image side sent to an engine, a multiple of 48 */
|
|
3
3
|
declare const imageSide = 288;
|
|
4
|
+
/** Decode an image URL into bytes for an engine */
|
|
5
|
+
declare const imageBuffer: (url: string) => Promise<ArrayBuffer>;
|
|
4
6
|
/** Center-crop one image URL to a square and resize it for an engine */
|
|
5
7
|
declare const resizeImageUrl: (url: string) => Promise<string>;
|
|
6
8
|
/** Center-crop and resize every image part before an engine sees it */
|
|
7
9
|
declare const resizeImages: (messages: EngineMessage[]) => Promise<EngineMessage[]>;
|
|
8
|
-
export { imageSide, resizeImageUrl, resizeImages, };
|
|
10
|
+
export { imageSide, imageBuffer, resizeImageUrl, resizeImages, };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { EngineMessage, LitertMessage, LlmEngine } from '../types';
|
|
2
|
+
/** Map built messages to LiteRT turns */
|
|
3
|
+
declare const toLitertMessages: (messages: EngineMessage[]) => Promise<LitertMessage[]>;
|
|
4
|
+
declare const litertEngine: LlmEngine;
|
|
5
|
+
export { litertEngine, toLitertMessages, };
|
package/dist/engines/wllama.d.ts
CHANGED
|
@@ -2,12 +2,8 @@ import type { EngineMessage, LlmEngine, LlmRuntime, StartLlmParams, WllamaMessag
|
|
|
2
2
|
/** CPU threads after the hardware maximum and the caller cap */
|
|
3
3
|
declare const cpuThreads: (cores: number, maxThreads: number) => number;
|
|
4
4
|
declare const
|
|
5
|
-
/** Decode an image URL into bytes for wllama */
|
|
6
|
-
imageBuffer: (url: string) => Promise<ArrayBuffer>,
|
|
7
5
|
/** Map built messages to wllama turns */
|
|
8
6
|
toWllamaMessages: (messages: EngineMessage[]) => Promise<WllamaMessage[]>,
|
|
9
|
-
/** Whether any turn carries an image */
|
|
10
|
-
hasImages: (messages: EngineMessage[]) => boolean,
|
|
11
7
|
/** Start the CPU runtime, the unified engine entry */
|
|
12
8
|
startLLM: ({ model, limits, onProgress, signal, logging, assets: overrides, }: StartLlmParams) => Promise<LlmRuntime>, wllamaEngine: LlmEngine;
|
|
13
|
-
export { cpuThreads,
|
|
9
|
+
export { cpuThreads, startLLM, toWllamaMessages, wllamaEngine, };
|
package/dist/enums.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ declare enum LlmStatus {
|
|
|
21
21
|
/** Which engine runs the model on this device */
|
|
22
22
|
declare enum LlmBackend {
|
|
23
23
|
cpu = "cpu",
|
|
24
|
+
gpu = "gpu",
|
|
24
25
|
none = "none"
|
|
25
26
|
}
|
|
26
27
|
/** Download or load-into-memory phase of a model load */
|
|
@@ -30,6 +31,7 @@ declare enum LlmPhase {
|
|
|
30
31
|
}
|
|
31
32
|
/** Catalog ids of the on-device models */
|
|
32
33
|
declare enum ChatModelId {
|
|
33
|
-
qwen3_vl_2b = "qwen3-vl-2b"
|
|
34
|
+
qwen3_vl_2b = "qwen3-vl-2b",
|
|
35
|
+
gemma4_e2b = "gemma4-e2b"
|
|
34
36
|
}
|
|
35
37
|
export { LlmRole, ChatStatus, LlmStatus, LlmBackend, LlmPhase, ChatModelId, };
|
package/dist/models.d.ts
CHANGED
|
@@ -2,8 +2,10 @@ import type { ChatModel } from './types';
|
|
|
2
2
|
declare const llmModels: ChatModel[],
|
|
3
3
|
/** Context window requested from the CPU engine, a 32k KV cache needs 3.5 GB */
|
|
4
4
|
cpuContextWindow = 8192,
|
|
5
|
+
/** Context window requested from the GPU engine, the wasm heap holds the model */
|
|
6
|
+
litertContextWindow = 4096,
|
|
5
7
|
/** Look up a catalog entry by id */
|
|
6
8
|
findLlmModel: (id?: string) => ChatModel | undefined,
|
|
7
9
|
/** Whether a model accepts images */
|
|
8
10
|
supportsImages: (model: ChatModel) => boolean;
|
|
9
|
-
export { llmModels, cpuContextWindow, findLlmModel, supportsImages, };
|
|
11
|
+
export { llmModels, cpuContextWindow, litertContextWindow, findLlmModel, supportsImages, };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see chat.node.min.js.LICENSE.txt */
|
|
2
|
-
(()=>{"use strict";var e={};const t={};function n(a){const o=t[a];if(void 0!==o)return o.exports;const r=t[a]={exports:{}};return e[a](r,r.exports,n),r.exports}n.m=e,n.d=(e,t)=>{for(var a in t)n.o(t,a)&&!n.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},n.f={},n.e=e=>{const t=[];return n.f.require(e,t),Promise.all(t)},n.u=e=>"engine-wllama.node.min.js",n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{const e={792:1};n.f.require=(t,a)=>{if(!e[t]){const a=require("./"+n.u(t));e[t]||(t=>{const a=t.modules,o=t.ids,r=t.runtime;for(var s in a)n.o(a,s)&&(n.m[s]=a[s]);r&&r(n);for(var i=0;i<o.length;i++)e[o[i]]=1})(a)}}})();let a={};var o,r,s,i,c,l;n.r(a),n.d(a,{ChatModelId:()=>l,ChatStatus:()=>r,LlmBackend:()=>i,LlmPhase:()=>c,LlmRole:()=>o,LlmStatus:()=>s,createChat:()=>G,preloadModel:()=>N}),function(e){e.system="system",e.user="user",e.assistant="assistant"}(o||(o={})),function(e){e[e.online=0]="online",e[e.away=1]="away",e[e.offline=2]="offline"}(r||(r={})),function(e){e[e.idle=0]="idle",e[e.downloading=1]="downloading",e[e.ready=2]="ready",e[e.error=3]="error",e[e.unsupported=4]="unsupported"}(s||(s={})),function(e){e.cpu="cpu",e.none="none"}(i||(i={})),function(e){e.download="download",e.load="load"}(c||(c={})),function(e){e.qwen3_vl_2b="qwen3-vl-2b"}(l||(l={}));const d=[{id:l.qwen3_vl_2b,reasoning:!0,cpu:{repo:"Qwen/Qwen3-VL-2B-Thinking-GGUF",file:"Qwen3VL-2B-Thinking-Q4_K_M.gguf",mmproj:{file:"mmproj-Qwen3VL-2B-Thinking-Q8_0.gguf"}}}],g=e=>e?d.find(t=>t.id==e):void 0,u=e=>!!e.cpu.mmproj,m=require("node:path"),p=()=>{return e=(0,m.join)(__dirname,"../assets/wllama"),{wasm:(0,m.join)(e,"wllama.wasm"),compatWasm:(0,m.join)(e,"compat/wllama.wasm"),compatWorker:(0,m.join)(e,"compat/wllama.js")};var e},f=288,h=async e=>{if(!e||"function"!=typeof fetch||"function"!=typeof createImageBitmap)return e;try{const t=await fetch(e),n=await createImageBitmap(await t.blob()),a=Math.min(n.width,n.height),o=Math.floor((n.width-a)/2),r=Math.floor((n.height-a)/2),s=((e,t)=>{if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(e,t);if("undefined"==typeof document)return;const n=document.createElement("canvas");return n.width=e,n.height=t,n})(f,f),i=s?s.getContext("2d"):null;if(!s||!i||a<1)return n.close(),e;if(i.drawImage(n,o,r,a,a,0,0,f,f),n.close(),(e=>"undefined"!=typeof OffscreenCanvas&&e instanceof OffscreenCanvas)(s)){const t=await s.convertToBlob({type:"image/jpeg",quality:.85});return await(e=>new Promise(t=>{if("undefined"==typeof FileReader)return void t("");const n=new FileReader;n.onerror=()=>t(""),n.onload=()=>t(`${n.result}`),n.readAsDataURL(e)}))(t)||e}return s.toDataURL("image/jpeg",.85)}catch{return e}},y={maxReplyTokens:200,maxThinkingTokens:64,maxReasoningTokens:150,maxImages:2,maxContentLength:1e3,promptCharBudget:1e4,maxThreads:2},w=(e,t,n)=>"number"==typeof e&&Number.isFinite(e)&&e>=n?Math.floor(e):t,x=(e={})=>{const t=y;return{maxReplyTokens:w(e.maxReplyTokens,t.maxReplyTokens,1),maxThinkingTokens:w(e.maxThinkingTokens,t.maxThinkingTokens,1),maxReasoningTokens:w(e.maxReasoningTokens,t.maxReasoningTokens,1),maxImages:w(e.maxImages,t.maxImages,0),maxContentLength:w(e.maxContentLength,t.maxContentLength,1),promptCharBudget:w(e.promptCharBudget,t.promptCharBudget,1),maxThreads:w(e.maxThreads,t.maxThreads,1)}},v=["<|channel>thought\n","<|channel>thought","..."],k=["<channel|>","..."],b=(e,t)=>t.length?[...e?[{type:"text",text:e}]:[],...t.map(e=>({type:"image",url:e}))]:e,M=e=>"string"==typeof e?e?[{type:"text",text:e}]:[]:e,T=(e,t)=>{if("string"==typeof e&&"string"==typeof t)return`${e}\n${t}`;const n=[];for(const a of[...M(e),{type:"text",text:"\n"},...M(t)]){const e=n[n.length-1];"text"==a.type&&"text"==e?.type?e.text+=a.text:n.push(a)}return n},C=async({model:e,logging:t,onReason:n,onText:a,start:o,stopped:r})=>{let s="",i="",c="";const l=((e,t)=>{let n="",a=!1;const o=(e,t)=>{let n=-1,a="";for(const o of t){const t=e.indexOf(o);t>=0&&(n<0||t<n)&&(n=t,a=o)}return{at:n,tag:a}},r=(e,t)=>{let n="";for(const a of t)for(let t=Math.min(e.length,a.length-1);t>n.length;t--)if(e.endsWith(a.slice(0,t))){n=e.slice(e.length-t);break}return n};return{feed:s=>{let i=n+s;for(n="";i;){if(a){const{at:t,tag:s}=o(i,k);if(t>=0){t&&e(i.slice(0,t)),i=i.slice(t+s.length),a=!1;continue}const c=r(i,k);e(i.slice(0,i.length-c.length)),n=c;break}{const{at:e,tag:s}=o(i,v);if(e>=0){e&&t(i.slice(0,e)),i=i.slice(e+s.length),i.startsWith("\n")&&(i=i.slice(1)),a=!0;continue}const c=r(i,v);t(i.slice(0,i.length-c.length)),n=c;break}}},flush:()=>{n&&(a?e(n):t(n),n="")}}})(e=>{i+=e,n(e)},e=>{s+=e,a(e)});try{const e=await o();for await(const t of e){if(r())break;const e=t?.choices?.[0],a=e?.delta,o="string"==typeof a?.reasoning_content?a.reasoning_content:"string"==typeof a?.reasoning?a.reasoning:"";e?.finish_reason&&(c=e.finish_reason),o&&(i+=o,n(o)),a?.content&&l.feed(a.content)}l.flush()}catch(e){t&&console.log("Local chat failed",e)}const d=r()?void 0:s||void 0;return t&&(console.log("Local chat reply",e.id,d?"****":""),console.log("Local chat finish",e.id,c||"none",s.length,i.length)),d},_=async({model:e,params:t,start:n,stopped:a})=>{const r=await(async e=>{const t=[];for(const n of e){if("string"==typeof n.content){t.push(n);continue}const e=[];for(const t of n.content)e.push("image"==t.type?{type:"image",url:await h(t.url)}:t);t.push({role:"user",content:e})}return t})(((e,t=y)=>{const n=t.maxContentLength,a=e.filter(e=>"string"==typeof e.content&&!!e.content.trim()||!(e.role!=o.user||!e.images?.length)),r=[],s=[];let i=0,c=t.maxImages;for(let e=a.length-1;e>=0;e--){const l=a[e],d=("string"==typeof l.content?l.content:"").slice(0,n),g=l.role==o.user?(l.images||[]).filter(e=>"string"==typeof e&&!!e).slice(0,Math.max(0,c)):[];if(d||g.length){if(d&&i+d.length>t.promptCharBudget)break;i+=d.length,c-=g.length,l.role==o.system?r.unshift(d):l.role==o.assistant?s.unshift({role:"assistant",content:d}):s.unshift({role:"user",content:b(d,g)})}}for(;s.length&&"user"!=s[0].role;)s.shift();const l=[];for(const e of s){const t=l[l.length-1];"assistant"==t?.role&&"assistant"==e.role?t.content=`${t.content}\n${e.content}`:"user"==t?.role&&"user"==e.role?t.content=T(t.content,e.content):"assistant"==e.role?l.push({role:"assistant",content:e.content}):l.push({role:"user",content:e.content})}const d=r.join("\n\n"),g=[];return d&&g.push({role:"system",content:d}),g.push(...l),g})(t.messages,t.limits));return t.logging&&((e,t)=>{console.log("Local chat call",e.id,t.map(({role:e,content:t})=>({role:e,content:"****",...Array.isArray(t)?{images:t.filter(e=>"image"==e.type).length}:{}})))})(e,r),await C({model:e,logging:t.logging,onReason:t.onReason,onText:t.onText,start:()=>n(r),stopped:a})};let L,j,q="",R=!1;const W=e=>`${e.wasm}|${e.compatWasm}|${e.compatWorker}`,$=e=>{const t=p();return{wasm:e?.wasm||t.wasm,compatWasm:e?.compatWasm||t.compatWasm,compatWorker:e?.compatWorker||t.compatWorker}},B=e=>{const t=e=>(...t)=>{R&&("string"==typeof t[0]&&t[0].includes("ggml_webgpu: Failed to get an adapter")||e(...t))};return{...e,log:t(e.log),warn:t(e.warn),error:t(e.error)}},I=async()=>{if(L)return L;const e=await n.e(924).then(()=>n(577)),t=j||$(),a=new e.Wllama({default:t.wasm},{logger:B(e.LoggerWithoutDebug)});return j||(j=t,q=W(t)),a.setCompat({wasm:t.compatWasm,worker:t.compatWorker}),L=a,a},P=async()=>{const e=L;L=void 0;try{await(e?.exit())}catch{}},O=(e,t)=>e.name.includes(t)||!!e.metadata?.originalURL?.includes(t),A=e=>[e.cpu.file,...e.cpu.mmproj?[e.cpu.mmproj.file]:[]],U=(e,t)=>`https://huggingface.co/${e}/resolve/main/${t}`,S=async e=>{if(!e.startsWith("data:"))return await(await fetch(e)).arrayBuffer();const t=e.indexOf(","),n=t>=0?e.slice(0,t):"";if(t<0)throw new Error("Invalid image data");if(!n.includes(";base64"))return await(await fetch(e)).arrayBuffer();const a=atob(e.slice(t+1)),o=new Uint8Array(a.length);for(let e=0;e<a.length;e++)o[e]=a.charCodeAt(e);return o.buffer},F=async e=>{const t=[];for(const n of e){if("string"==typeof n.content){"system"==n.role?t.push({role:"system",content:n.content}):"assistant"==n.role?t.push({role:"assistant",content:n.content}):t.push({role:"user",content:n.content});continue}const e=[];for(const t of n.content)e.push("text"==t.type?{type:"text",text:t.text}:{type:"image",data:await S(t.url)});t.push({role:"user",content:e})}return t},E=async(e,t,n)=>{if(R=!0===n.logging,e.isModelLoaded())return await _({model:t,params:n,stopped:()=>!!n.signal?.aborted,start:async a=>{if((e=>e.some(e=>Array.isArray(e.content)&&e.content.some(e=>"image"==e.type)))(a)&&!e.supportInputModality("image"))throw new Error("Model does not accept images");return await e.createChatCompletion({messages:await F(a),...(o=n.limits,r=n.reasoning,{stream:!0,temperature:.7,max_tokens:r?o.maxReasoningTokens:o.maxReplyTokens}),abortSignal:n.signal,...t.reasoning?{chat_template_kwargs:{enable_thinking:n.reasoning}}:{}});var o,r}})},Q={backend:i.cpu,startLLM:async({model:e,limits:t,onProgress:n,signal:a,logging:o,assets:r})=>{R=!0===o;const s=$(r),l=W(s);L&&l!=q&&await P(),j=s,q=l;const d=(g="undefined"!=typeof navigator&&navigator.hardwareConcurrency||4,u=t.maxThreads,Math.max(1,Math.min(Math.max(2,Math.min(8,(g||4)-1)),u)));var g,u;const m=await I(),p=U(e.cpu.repo,e.cpu.file),f=e.cpu.mmproj?.file,h=f?U(e.cpu.repo,f):"";if(a?.aborted)throw await P(),new Error("Cancelled");if(f){const t=await m.cacheManager.list(),n=t.some(t=>O(t,e.cpu.file)),a=t.some(e=>O(e,f));n&&!a&&await m.cacheManager.deleteMany(t=>O(t,e.cpu.file))}try{await m.loadModelFromUrl(h?{url:p,mmprojUrl:h}:p,{n_ctx:8192,n_threads:d,reasoning_budget_tokens:t.maxThinkingTokens,n_gpu_layers:0,useCache:!0,jinja:!0,signal:a,progressCallback:({loaded:e,total:t})=>{const a=t?e/t:0;n(a,a>=1?c.load:c.download)}})}catch(e){throw await P(),e}if(a?.aborted)throw await P(),new Error("Cancelled");if(!m.getLoadedContextInfo().n_ctx)throw await P(),new Error("Failed to create model context");return{model:e,backend:i.cpu,contextSize:8192,chat:t=>E(m,e,t),unload:async()=>{if(L==m)await P();else try{await m.exit()}catch{}}}},cached:async e=>{try{const t=await I(),n=await t.cacheManager.list();return A(e).every(e=>n.some(t=>O(t,e)))}catch{return!1}},hasCache:async()=>{try{const e=await I();return(await e.cacheManager.list()).length>0}catch{return!1}},remove:async e=>{try{const t=await I();await t.cacheManager.deleteMany(t=>A(e).some(e=>O(t,e)))}catch{}},purge:async()=>{try{const e=await I();await e.cacheManager.clear()}catch{}}},D=((e={})=>{const t=e.engines?.cpu||Q;let n,a,o,r,l,g,u,m=i.none,p=s.idle,f=0,h={percent:0,phase:c.download};const y=()=>e.backend||("undefined"==typeof window&&"undefined"==typeof self&&"undefined"==typeof Worker||"undefined"==typeof WebAssembly?i.none:i.cpu),w=async()=>{await t.purge()},x=(e,t)=>{h={percent:Math.max(0,Math.min(1,e)),phase:t||c.download},g?.(h.percent,h.phase)},v=e=>{p=e},k=()=>p==s.ready&&!!n&&!!a;return{load:async({model:e,limits:u,assets:p,logging:b,onProgress:M})=>{if(k()&&a?.id==e.id)return!0;if(o&&r==e.id)return g=M,M(h.percent,h.phase),await o;o&&(g=void 0,l?.abort(),await o.catch(()=>!1)),g=M;const T=++f,C=(async()=>{await(async()=>{try{if("undefined"==typeof navigator)return;await(navigator.storage?.persist?.())}catch{}})();const o=y();if(T!=f)return!1;if(o==i.none)return v(s.unsupported),!1;if(await(async e=>{if(a&&a.id!=e.id){const e=a,o=n;n=void 0,a=void 0,await(o?.unload()),await t.remove(e)}if(await t.hasCache())for(const n of d)n.id!=e.id&&await t.remove(n)})(e),T!=f)return!1;const r=await t.cached(e);if(T!=f)return!1;if(!r&&await(async()=>{try{if("undefined"==typeof navigator)return!1;if(!navigator.storage?.estimate)return!1;const{quota:e=0,usage:t=0}=await navigator.storage.estimate();return!!e&&t>=e}catch{return!1}})()&&await w(),T!=f)return!1;v(s.downloading),x(0,c.download),r&&x(1,c.load);const g=new AbortController;l=g;try{const r=await t.startLLM({model:e,limits:u,logging:b,signal:g.signal,assets:p,onProgress:(e,t)=>{T==f&&x(e,t)}});return T!=f||g.signal.aborted?(await r.unload(),!1):(n=r,a=e,m=o,v(s.ready),x(1,c.load),!0)}catch(e){return T!=f||(n=void 0,a=void 0,b&&console.log("Local model failed",e),(e=>["quota","no space","disk full","ns_error_dom_quota_reached"].some(t=>`${e?.name||""} ${e?.message||`${e}`}`.toLowerCase().includes(t)))(e)&&await w(),v(s.error)),!1}finally{l==g&&(l=void 0)}})().finally(()=>{o==C&&(o=void 0,r=void 0)});return o=C,r=e.id,await C},chat:async e=>{if(!k()||!n)return;for(;u;)await u.catch(()=>{});if(!k()||!n)return;const t=n.chat(e);u=t;try{return await t}finally{u==t&&(u=void 0)}},unload:async()=>{f++,l?.abort(),l=void 0,r=void 0;const e=n;n=void 0,a=void 0,v(s.idle),await(e?.unload())},cancelLoad:()=>{f++,l?.abort(),l=void 0,r=void 0,p==s.downloading&&v(s.idle)},ready:k,loading:()=>!k()&&p==s.downloading,status:()=>p,backend:()=>m,model:()=>a}})(),V=(e,t,n)=>{const a=e.status(),o=g(t);return{availability:a==s.ready?r.online:a==s.error||a==s.unsupported?r.offline:r.away,status:a,backend:e.backend(),supportsImages:!!o&&u(o),ready:e.ready(),loading:e.loading(),requesting:n.requesting,phase:n.phase,percent:n.percent,...n.error?{error:n.error}:{}}},G=(e=>{let t,n=0;const a=t=>t.input.onStatus?.(V(e,t.input.model,t));return r=>{t?.controller.abort();const i={token:++n,input:r,controller:new AbortController,percent:e.ready()?100:0,phase:e.ready()?c.load:c.download,requesting:!1,cancelled:!1};return t=i,a(i),(async t=>{const{input:r}=t,s=!0===r.logging,i=g(r.model),l=r.messages.some(e=>e.role!=o.system&&"string"==typeof e.content&&!!e.content.trim()),d=r.messages.some(e=>e.role==o.user&&!!e.images?.some(e=>"string"==typeof e&&!!e)),m=l||d;if(!i)return t.error="Unknown model",void a(t);if(!m)return void a(t);if(d&&!l&&!u(i))return t.error="Model does not accept images",void a(t);const p=x(r.limits),f=await e.load({model:i,limits:p,logging:s,onProgress:(e,o)=>{t.token==n&&(t.percent=Math.round(100*e),t.phase=o,a(t))}}).catch(e=>(t.token==n&&(t.error=`${e?.message||`${e}`}`),!1));if(t.token!=n)return;if(!f)return t.cancelled||t.error||(t.error="Model not ready"),void a(t);const h=u(i),y=d&&!h?r.messages.map(({images:e,...t})=>t):r.messages;if(d&&!h&&!l)return t.error="Model does not accept images",void a(t);t.requesting=!0,t.error=void 0,t.percent=100,t.phase=c.load,a(t);let w,v="",k="";try{w=await e.chat({messages:y,limits:p,reasoning:!0===r.reasoning,logging:s,onReason:e=>{t.token!=n||t.cancelled||(v+=e,r.onThinking?.(v))},onText:e=>{t.token!=n||t.cancelled||(k+=e,r.onResponse?.(k))},signal:t.controller.signal})}catch(e){if(t.token!=n)return;return t.requesting=!1,t.error=`${e?.message||`${e}`}`,void a(t)}t.token==n&&(t.requesting=!1,w||t.cancelled||(t.error="No reply"),a(t))})(i),()=>(t=>{t.token==n&&(t.cancelled=!0,t.controller.abort(),e.status()!=s.ready?(e.cancelLoad(),a(t)):e.unload().then(()=>a(t)))})(i)}})(D),N=(z=D,async e=>{const t=g(e.model),n=t=>e.onStatus?.(V(z,e.model,t));if(!t)return n({requesting:!1,phase:c.download,percent:0,error:"Unknown model"}),!1;let a,o=0,r=c.download;const s=z.ready()&&z.model()?.id==t.id;return await z.load({model:t,limits:x(e.limits),logging:!0===e.logging,onProgress:(e,t)=>{o=Math.round(100*e),r=t,n({requesting:!1,phase:r,percent:o})}}).catch(e=>(a=`${e?.message||`${e}`}`,!1))?(s&&n({requesting:!1,phase:c.load,percent:100}),!0):(n({requesting:!1,phase:r,percent:o,error:a||"Model not ready"}),!1)});var z;module.exports=a})();
|
|
2
|
+
(()=>{"use strict";var e={};const t={};function n(a){const o=t[a];if(void 0!==o)return o.exports;const r=t[a]={exports:{}};return e[a](r,r.exports,n),r.exports}n.m=e,n.d=(e,t)=>{for(var a in t)n.o(t,a)&&!n.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},n.f={},n.e=e=>{const t=[];return n.f.require(e,t),Promise.all(t)},n.u=e=>({520:"engine-litert",924:"engine-wllama"}[e]+".node.min.js"),n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{const e={792:1};n.f.require=(t,a)=>{if(!e[t]){const a=require("./"+n.u(t));e[t]||(t=>{const a=t.modules,o=t.ids,r=t.runtime;for(var s in a)n.o(a,s)&&(n.m[s]=a[s]);r&&r(n);for(var i=0;i<o.length;i++)e[o[i]]=1})(a)}}})();let a={};var o,r,s,i,c,l;n.r(a),n.d(a,{ChatModelId:()=>l,ChatStatus:()=>r,LlmBackend:()=>i,LlmPhase:()=>c,LlmRole:()=>o,LlmStatus:()=>s,createChat:()=>Z,preloadModel:()=>ee}),function(e){e.system="system",e.user="user",e.assistant="assistant"}(o||(o={})),function(e){e[e.online=0]="online",e[e.away=1]="away",e[e.offline=2]="offline"}(r||(r={})),function(e){e[e.idle=0]="idle",e[e.downloading=1]="downloading",e[e.ready=2]="ready",e[e.error=3]="error",e[e.unsupported=4]="unsupported"}(s||(s={})),function(e){e.cpu="cpu",e.gpu="gpu",e.none="none"}(i||(i={})),function(e){e.download="download",e.load="load"}(c||(c={})),function(e){e.qwen3_vl_2b="qwen3-vl-2b",e.gemma4_e2b="gemma4-e2b"}(l||(l={}));const d=[{id:l.qwen3_vl_2b,reasoning:!0,cpu:{repo:"Qwen/Qwen3-VL-2B-Thinking-GGUF",file:"Qwen3VL-2B-Thinking-Q4_K_M.gguf",mmproj:{file:"mmproj-Qwen3VL-2B-Thinking-Q8_0.gguf"}}},{id:l.gemma4_e2b,reasoning:!0,litert:{repo:"litert-community/gemma-4-E2B-it-litert-lm",file:"gemma-4-E2B-it.litertlm",vision:!0}}],g=e=>e?d.find(t=>t.id==e):void 0,u=e=>!!e.cpu?.mmproj||!!e.litert?.vision,m=require("node:path"),p=()=>{return e=(0,m.join)(__dirname,"../assets/wllama"),{wasm:(0,m.join)(e,"wllama.wasm"),compatWasm:(0,m.join)(e,"compat/wllama.wasm"),compatWorker:(0,m.join)(e,"compat/wllama.js")};var e},f=()=>{return e=(0,m.join)(__dirname,"../assets/litert"),{script:(0,m.join)(e,"litertlm_wasm_internal.js"),wasm:(0,m.join)(e,"litertlm_wasm_internal.wasm")};var e},h=288,w=async e=>{if(!e.startsWith("data:"))return await(await fetch(e)).arrayBuffer();const t=e.indexOf(","),n=t>=0?e.slice(0,t):"";if(t<0)throw new Error("Invalid image data");if(!n.includes(";base64"))return await(await fetch(e)).arrayBuffer();const a=atob(e.slice(t+1)),o=new Uint8Array(a.length);for(let e=0;e<a.length;e++)o[e]=a.charCodeAt(e);return o.buffer},y=async e=>{if(!e||"function"!=typeof fetch||"function"!=typeof createImageBitmap)return e;try{const t=await fetch(e),n=await createImageBitmap(await t.blob()),a=Math.min(n.width,n.height),o=Math.floor((n.width-a)/2),r=Math.floor((n.height-a)/2),s=((e,t)=>{if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(e,t);if("undefined"==typeof document)return;const n=document.createElement("canvas");return n.width=e,n.height=t,n})(h,h),i=s?s.getContext("2d"):null;if(!s||!i||a<1)return n.close(),e;if(i.drawImage(n,o,r,a,a,0,0,h,h),n.close(),(e=>"undefined"!=typeof OffscreenCanvas&&e instanceof OffscreenCanvas)(s)){const t=await s.convertToBlob({type:"image/jpeg",quality:.85});return await(e=>new Promise(t=>{if("undefined"==typeof FileReader)return void t("");const n=new FileReader;n.onerror=()=>t(""),n.onload=()=>t(`${n.result}`),n.readAsDataURL(e)}))(t)||e}return s.toDataURL("image/jpeg",.85)}catch{return e}},x={maxReplyTokens:200,maxThinkingTokens:64,maxReasoningTokens:150,maxImages:2,maxContentLength:1e3,promptCharBudget:1e4,maxThreads:2},v=(e,t,n)=>"number"==typeof e&&Number.isFinite(e)&&e>=n?Math.floor(e):t,b=(e={})=>{const t=x;return{maxReplyTokens:v(e.maxReplyTokens,t.maxReplyTokens,1),maxThinkingTokens:v(e.maxThinkingTokens,t.maxThinkingTokens,1),maxReasoningTokens:v(e.maxReasoningTokens,t.maxReasoningTokens,1),maxImages:v(e.maxImages,t.maxImages,0),maxContentLength:v(e.maxContentLength,t.maxContentLength,1),promptCharBudget:v(e.promptCharBudget,t.promptCharBudget,1),maxThreads:v(e.maxThreads,t.maxThreads,1)}},k=(e,t)=>({stream:!0,temperature:.7,max_tokens:t?e.maxReasoningTokens:e.maxReplyTokens}),_=(e,t)=>`https://huggingface.co/${e}/resolve/main/${t}`,M=e=>e.some(e=>Array.isArray(e.content)&&e.content.some(e=>"image"==e.type)),C=["<|channel>thought\n","<|channel>thought","..."],T=["<channel|>","..."],L=(e,t)=>t.length?[...e?[{type:"text",text:e}]:[],...t.map(e=>({type:"image",url:e}))]:e,j=e=>"string"==typeof e?e?[{type:"text",text:e}]:[]:e,R=(e,t)=>{if("string"==typeof e&&"string"==typeof t)return`${e}\n${t}`;const n=[];for(const a of[...j(e),{type:"text",text:"\n"},...j(t)]){const e=n[n.length-1];"text"==a.type&&"text"==e?.type?e.text+=a.text:n.push(a)}return n},q=async({model:e,logging:t,onReason:n,onText:a,start:o,stopped:r})=>{let s="",i="",c="";const l=((e,t)=>{let n="",a=!1;const o=(e,t)=>{let n=-1,a="";for(const o of t){const t=e.indexOf(o);t>=0&&(n<0||t<n)&&(n=t,a=o)}return{at:n,tag:a}},r=(e,t)=>{let n="";for(const a of t)for(let t=Math.min(e.length,a.length-1);t>n.length;t--)if(e.endsWith(a.slice(0,t))){n=e.slice(e.length-t);break}return n};return{feed:s=>{let i=n+s;for(n="";i;){if(a){const{at:t,tag:s}=o(i,T);if(t>=0){t&&e(i.slice(0,t)),i=i.slice(t+s.length),a=!1;continue}const c=r(i,T);e(i.slice(0,i.length-c.length)),n=c;break}{const{at:e,tag:s}=o(i,C);if(e>=0){e&&t(i.slice(0,e)),i=i.slice(e+s.length),i.startsWith("\n")&&(i=i.slice(1)),a=!0;continue}const c=r(i,C);t(i.slice(0,i.length-c.length)),n=c;break}}},flush:()=>{n&&(a?e(n):t(n),n="")}}})(e=>{i+=e,n(e)},e=>{s+=e,a(e)});try{const e=await o();for await(const t of e){if(r())break;const e=t?.choices?.[0],a=e?.delta,o="string"==typeof a?.reasoning_content?a.reasoning_content:"string"==typeof a?.reasoning?a.reasoning:"";e?.finish_reason&&(c=e.finish_reason),o&&(i+=o,n(o)),a?.content&&l.feed(a.content)}l.flush()}catch(e){t&&console.log("Local chat failed",e)}const d=r()?void 0:s||void 0;return t&&(console.log("Local chat reply",e.id,d?"****":""),console.log("Local chat finish",e.id,c||"none",s.length,i.length)),d},E=async({model:e,params:t,start:n,stopped:a})=>{const r=await(async e=>{const t=[];for(const n of e){if("string"==typeof n.content){t.push(n);continue}const e=[];for(const t of n.content)e.push("image"==t.type?{type:"image",url:await y(t.url)}:t);t.push({role:"user",content:e})}return t})(((e,t=x)=>{const n=t.maxContentLength,a=e.filter(e=>"string"==typeof e.content&&!!e.content.trim()||!(e.role!=o.user||!e.images?.length)),r=[],s=[];let i=0,c=t.maxImages;for(let e=a.length-1;e>=0;e--){const l=a[e],d=("string"==typeof l.content?l.content:"").slice(0,n),g=l.role==o.user?(l.images||[]).filter(e=>"string"==typeof e&&!!e).slice(0,Math.max(0,c)):[];if(d||g.length){if(d&&i+d.length>t.promptCharBudget)break;i+=d.length,c-=g.length,l.role==o.system?r.unshift(d):l.role==o.assistant?s.unshift({role:"assistant",content:d}):s.unshift({role:"user",content:L(d,g)})}}for(;s.length&&"user"!=s[0].role;)s.shift();const l=[];for(const e of s){const t=l[l.length-1];"assistant"==t?.role&&"assistant"==e.role?t.content=`${t.content}\n${e.content}`:"user"==t?.role&&"user"==e.role?t.content=R(t.content,e.content):"assistant"==e.role?l.push({role:"assistant",content:e.content}):l.push({role:"user",content:e.content})}const d=r.join("\n\n"),g=[];return d&&g.push({role:"system",content:d}),g.push(...l),g})(t.messages,t.limits));return t.logging&&((e,t)=>{console.log("Local chat call",e.id,t.map(({role:e,content:t})=>({role:e,content:"****",...Array.isArray(t)?{images:t.filter(e=>"image"==e.type).length}:{}})))})(e,r),await q({model:e,logging:t.logging,onReason:t.onReason,onText:t.onText,start:()=>n(r),stopped:a})},W="litertlm-models",$={num_output_candidates:1,wait_for_weight_uploads:!0,num_decode_steps_per_sync:3,sequence_batch_size:0,supported_lora_ranks:[],max_top_k:40,enable_decode_logits:!1,enable_external_embeddings:!1,use_submodel:!1,use_autosized_ringbuffers:!0},B=async()=>{try{if("undefined"==typeof caches)return;return await caches.open(W)}catch{return}},P=async e=>{const t=[];for(const n of e){if("string"==typeof n.content){t.push({role:n.role,content:n.content});continue}const e=[];for(const t of n.content)e.push("text"==t.type?{type:"text",text:t.text}:{type:"image",data:await w(t.url)});t.push({role:"user",content:e})}return t},I=async({engine:e,model:t,mod:n,params:a})=>await E({model:t,params:a,stopped:()=>!!a.signal?.aborted,start:async o=>{if(a.signal?.aborted)throw new Error("Cancelled");if(M(o)&&!t.litert?.vision)throw new Error("Model does not accept images");return async function*({engine:e,mod:t,params:n,messages:a}){const o=a[a.length-1],r=k(n.limits,n.reasoning);if("user"!=o?.role)throw new Error("No user turn");const s=await P(a.slice(0,-1)),i=(await P([o]))[0];if(!i)throw new Error("Empty prompt");const c=await e.createConversation({sessionConfig:{maxOutputTokens:r.max_tokens,samplerParams:{type:t.SamplerType.TOP_K,temperature:r.temperature,k:40,p:.95}},preface:{...s.length?{messages:s}:{},extra_context:{enable_thinking:n.reasoning}}});let l;const d=()=>{l&&l.cancel().catch(()=>{});try{c.cancel()}catch{}};n.signal?.addEventListener("abort",d,{once:!0});try{if(n.signal?.aborted)return;const e=c.sendMessageStreaming(i).getReader();for(l=e;;){const{done:t,value:n}=await e.read();if(t)break;const a={};let o="string"==typeof n.content?n.content:"";if(Array.isArray(n.content))for(const e of n.content)"text"==e.type&&(o+=e.text);o&&(a.content=o);const r=n.channels?.thought;r&&(a.reasoning=r),(a.content||a.reasoning)&&(yield{choices:[{delta:a}]})}}finally{n.signal?.removeEventListener("abort",d);try{await c.delete()}catch{}}}({engine:e,mod:n,params:a,messages:o})}}),O={backend:i.gpu,startLLM:async({model:e,limits:t,onProgress:a,signal:o,logging:r})=>{const s=e.litert;if(!s)throw new Error("Model has no LiteRT artifact");const l=await n.e(520).then(()=>n(507));if(await l.getOrLoadGlobalLiteRtLm(f().script),o?.aborted)throw new Error("Cancelled");const d=_(s.repo,s.file),g=await B(),u=g?await g.match(d):void 0;let m;if(u?.body)m=u.body,a(1,c.load);else{const e=await fetch(d,{signal:o});if(!e.ok||!e.body)throw new Error("Model download failed");const t=Number(e.headers.get("content-length"))||0;let n=0;const s=e.body.pipeThrough(new TransformStream({transform:(e,o)=>{n+=e.byteLength,a(t?n/t:0,c.download),o.enqueue(e)}}));if(g){const[e,n]=s.tee();g.put(d,new Response(n,{headers:{"content-type":"application/octet-stream",...t?{"content-length":`${t}`}:{}}})).catch(e=>{r&&console.log("Local model cache failed",e)}),m=e}else m=s}if(o?.aborted)throw new Error("Cancelled");a(1,c.load);const p=await l.Engine.create({model:m,mainExecutorSettings:{maxNumTokens:4096,...s.vision?{maxNumImages:Math.max(1,t.maxImages)}:{},backendConfig:$},...s.vision?{visionBackend:l.Backend.GPU}:{}});if(o?.aborted)throw await p.delete(),new Error("Cancelled");return{model:e,backend:i.gpu,contextSize:4096,chat:t=>I({engine:p,model:e,mod:l,params:t}),unload:async()=>{await p.delete()}}},cached:async e=>{try{const t=e.litert,n=await B();return!(!t||!n)&&!!await n.match(_(t.repo,t.file))}catch{return!1}},hasCache:async()=>{try{const e=await B();return!!e&&(await e.keys()).length>0}catch{return!1}},remove:async e=>{try{const t=e.litert,n=await B();if(!t||!n)return;await n.delete(_(t.repo,t.file))}catch{}},purge:async()=>{try{"undefined"!=typeof caches&&await caches.delete(W)}catch{}}};let A,S,U="",F=!1;const N=e=>`${e.wasm}|${e.compatWasm}|${e.compatWorker}`,Q=e=>{const t=p();return{wasm:e?.wasm||t.wasm,compatWasm:e?.compatWasm||t.compatWasm,compatWorker:e?.compatWorker||t.compatWorker}},z=e=>{const t=e=>(...t)=>{F&&("string"==typeof t[0]&&t[0].includes("ggml_webgpu: Failed to get an adapter")||e(...t))};return{...e,log:t(e.log),warn:t(e.warn),error:t(e.error)}},G=async()=>{if(A)return A;const e=await n.e(924).then(()=>n(577)),t=S||Q(),a=new e.Wllama({default:t.wasm},{logger:z(e.LoggerWithoutDebug)});return S||(S=t,U=N(t)),a.setCompat({wasm:t.compatWasm,worker:t.compatWorker}),A=a,a},D=async()=>{const e=A;A=void 0;try{await(e?.exit())}catch{}},V=(e,t)=>e.name.includes(t)||!!e.metadata?.originalURL?.includes(t),K=e=>e.cpu?[e.cpu.file,...e.cpu.mmproj?[e.cpu.mmproj.file]:[]]:[],H=async e=>{const t=[];for(const n of e){if("string"==typeof n.content){"system"==n.role?t.push({role:"system",content:n.content}):"assistant"==n.role?t.push({role:"assistant",content:n.content}):t.push({role:"user",content:n.content});continue}const e=[];for(const t of n.content)e.push("text"==t.type?{type:"text",text:t.text}:{type:"image",data:await w(t.url)});t.push({role:"user",content:e})}return t},J={backend:i.cpu,startLLM:async({model:e,limits:t,onProgress:n,signal:a,logging:o,assets:r})=>{F=!0===o;const s=e.cpu;if(!s)throw new Error("Model has no CPU artifact");const l=Q(r),d=N(l);A&&d!=U&&await D(),S=l,U=d;const g=(u="undefined"!=typeof navigator&&navigator.hardwareConcurrency||4,m=t.maxThreads,Math.max(1,Math.min(Math.max(2,Math.min(8,(u||4)-1)),m)));var u,m;const p=await G(),f=_(s.repo,s.file),h=s.mmproj?.file,w=h?_(s.repo,h):"";if(a?.aborted)throw await D(),new Error("Cancelled");if(h){const e=await p.cacheManager.list(),t=e.some(e=>V(e,s.file)),n=e.some(e=>V(e,h));t&&!n&&await p.cacheManager.deleteMany(e=>V(e,s.file))}try{await p.loadModelFromUrl(w?{url:f,mmprojUrl:w}:f,{n_ctx:8192,n_threads:g,reasoning_budget_tokens:t.maxThinkingTokens,n_gpu_layers:0,useCache:!0,jinja:!0,signal:a,progressCallback:({loaded:e,total:t})=>{const a=t?e/t:0;n(a,a>=1?c.load:c.download)}})}catch(e){throw await D(),e}if(a?.aborted)throw await D(),new Error("Cancelled");if(!p.getLoadedContextInfo().n_ctx)throw await D(),new Error("Failed to create model context");return{model:e,backend:i.cpu,contextSize:8192,chat:t=>(async(e,t,n)=>{if(F=!0===n.logging,e.isModelLoaded())return await E({model:t,params:n,stopped:()=>!!n.signal?.aborted,start:async a=>{if(M(a)&&!e.supportInputModality("image"))throw new Error("Model does not accept images");return await e.createChatCompletion({messages:await H(a),...k(n.limits,n.reasoning),abortSignal:n.signal,...t.reasoning?{chat_template_kwargs:{enable_thinking:n.reasoning}}:{}})}})})(p,e,t),unload:async()=>{if(A==p)await D();else try{await p.exit()}catch{}}}},cached:async e=>{try{if(!e.cpu)return!1;const t=await G(),n=await t.cacheManager.list();return K(e).every(e=>n.some(t=>V(t,e)))}catch{return!1}},hasCache:async()=>{try{const e=await G();return(await e.cacheManager.list()).length>0}catch{return!1}},remove:async e=>{try{if(!e.cpu)return;const t=await G();await t.cacheManager.deleteMany(t=>K(e).some(e=>V(t,e)))}catch{}},purge:async()=>{try{const e=await G();await e.cacheManager.clear()}catch{}}},X=((e={})=>{const t=e.engines?.cpu||J,n=e.engines?.litert||O;let a,o,r,l,g,u,m,p,f=i.none,h=s.idle,w=0,y={percent:0,phase:c.download};const x=a=>{if(e.backend!=i.none)return e.backend==i.cpu?a.cpu?t:void 0:e.backend==i.gpu?a.litert?n:void 0:a.litert&&"undefined"!=typeof navigator&&navigator.gpu&&"undefined"!=typeof WebAssembly?n:!a.cpu||"undefined"==typeof window&&"undefined"==typeof self&&"undefined"==typeof Worker||"undefined"==typeof WebAssembly?void 0:t},v=(e,t)=>{y={percent:Math.max(0,Math.min(1,e)),phase:t||c.download},m?.(y.percent,y.phase)},b=e=>{h=e},k=()=>h==s.ready&&!!a&&!!o;return{load:async({model:e,limits:t,assets:n,logging:i,onProgress:p})=>{if(k()&&o?.id==e.id)return!0;if(l&&g==e.id)return m=p,p(y.percent,y.phase),await l;l&&(m=void 0,u?.abort(),await l.catch(()=>!1)),m=p;const h=++w,_=(async()=>{await(async()=>{try{if("undefined"==typeof navigator)return;await(navigator.storage?.persist?.())}catch{}})();const l=x(e);if(h!=w)return!1;if(!l)return b(s.unsupported),!1;if(await(async(e,t)=>{if(o&&o.id!=e.id){const e=o,t=r,n=a;a=void 0,o=void 0,r=void 0,await(n?.unload()),await(t?.remove(e))}if(await t.hasCache())for(const n of d)n.id!=e.id&&x(n)==t&&await t.remove(n)})(e,l),h!=w)return!1;const g=await l.cached(e);if(h!=w)return!1;if(!g&&await(async()=>{try{if("undefined"==typeof navigator)return!1;if(!navigator.storage?.estimate)return!1;const{quota:e=0,usage:t=0}=await navigator.storage.estimate();return!!e&&t>=e}catch{return!1}})()&&await l.purge(),h!=w)return!1;b(s.downloading),v(0,c.download),g&&v(1,c.load);const m=new AbortController;u=m;try{const d=await l.startLLM({model:e,limits:t,logging:i,signal:m.signal,assets:n,onProgress:(e,t)=>{h==w&&v(e,t)}});return h!=w||m.signal.aborted?(await d.unload(),!1):(a=d,o=e,r=l,f=l.backend,b(s.ready),v(1,c.load),!0)}catch(e){return h!=w||(a=void 0,o=void 0,r=void 0,i&&console.log("Local model failed",e),(e=>["quota","no space","disk full","ns_error_dom_quota_reached"].some(t=>`${e?.name||""} ${e?.message||`${e}`}`.toLowerCase().includes(t)))(e)&&await l.purge(),b(s.error)),!1}finally{u==m&&(u=void 0)}})().finally(()=>{l==_&&(l=void 0,g=void 0)});return l=_,g=e.id,await _},chat:async e=>{if(!k()||!a)return;for(;p;)await p.catch(()=>{});if(!k()||!a)return;const t=a.chat(e);p=t;try{return await t}finally{p==t&&(p=void 0)}},unload:async()=>{w++,u?.abort(),u=void 0,g=void 0;const e=a;a=void 0,o=void 0,r=void 0,b(s.idle),await(e?.unload())},cancelLoad:()=>{w++,u?.abort(),u=void 0,g=void 0,h==s.downloading&&b(s.idle)},ready:k,loading:()=>!k()&&h==s.downloading,status:()=>h,backend:()=>f,model:()=>o}})(),Y=(e,t,n)=>{const a=e.status(),o=g(t);return{availability:a==s.ready?r.online:a==s.error||a==s.unsupported?r.offline:r.away,status:a,backend:e.backend(),supportsImages:!!o&&u(o),ready:e.ready(),loading:e.loading(),requesting:n.requesting,phase:n.phase,percent:n.percent,...n.error?{error:n.error}:{}}},Z=(e=>{let t,n=0;const a=t=>t.input.onStatus?.(Y(e,t.input.model,t));return r=>{t?.controller.abort();const i={token:++n,input:r,controller:new AbortController,percent:e.ready()?100:0,phase:e.ready()?c.load:c.download,requesting:!1,cancelled:!1};return t=i,a(i),(async t=>{const{input:r}=t,s=!0===r.logging,i=g(r.model),l=r.messages.some(e=>e.role!=o.system&&"string"==typeof e.content&&!!e.content.trim()),d=r.messages.some(e=>e.role==o.user&&!!e.images?.some(e=>"string"==typeof e&&!!e)),m=l||d;if(!i)return t.error="Unknown model",void a(t);if(!m)return void a(t);if(d&&!l&&!u(i))return t.error="Model does not accept images",void a(t);const p=b(r.limits),f=await e.load({model:i,limits:p,logging:s,onProgress:(e,o)=>{t.token==n&&(t.percent=Math.round(100*e),t.phase=o,a(t))}}).catch(e=>(t.token==n&&(t.error=`${e?.message||`${e}`}`),!1));if(t.token!=n)return;if(!f)return t.cancelled||t.error||(t.error="Model not ready"),void a(t);const h=u(i),w=d&&!h?r.messages.map(({images:e,...t})=>t):r.messages;if(d&&!h&&!l)return t.error="Model does not accept images",void a(t);t.requesting=!0,t.error=void 0,t.percent=100,t.phase=c.load,a(t);let y,x="",v="";try{y=await e.chat({messages:w,limits:p,reasoning:!0===r.reasoning,logging:s,onReason:e=>{t.token!=n||t.cancelled||(x+=e,r.onThinking?.(x))},onText:e=>{t.token!=n||t.cancelled||(v+=e,r.onResponse?.(v))},signal:t.controller.signal})}catch(e){if(t.token!=n)return;return t.requesting=!1,t.error=`${e?.message||`${e}`}`,void a(t)}t.token==n&&(t.requesting=!1,y||t.cancelled||(t.error="No reply"),a(t))})(i),()=>(t=>{t.token==n&&(t.cancelled=!0,t.controller.abort(),e.status()!=s.ready?(e.cancelLoad(),a(t)):e.unload().then(()=>a(t)))})(i)}})(X),ee=(te=X,async e=>{const t=g(e.model),n=t=>e.onStatus?.(Y(te,e.model,t));if(!t)return n({requesting:!1,phase:c.download,percent:0,error:"Unknown model"}),!1;let a,o=0,r=c.download;const s=te.ready()&&te.model()?.id==t.id;return await te.load({model:t,limits:b(e.limits),logging:!0===e.logging,onProgress:(e,t)=>{o=Math.round(100*e),r=t,n({requesting:!1,phase:r,percent:o})}}).catch(e=>(a=`${e?.message||`${e}`}`,!1))?(s&&n({requesting:!1,phase:c.load,percent:100}),!0):(n({requesting:!1,phase:r,percent:o,error:a||"Model not ready"}),!1)});var te;module.exports=a})();
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see chat.node.min.mjs.LICENSE.txt */
|
|
2
|
-
import{dirname as e,join as t}from"node:path";import{fileURLToPath as n}from"node:url";var a={};const o={};function r(e){const t=o[e];if(void 0!==t)return t.exports;const n=o[e]={exports:{}};return a[e](n,n.exports,r),n.exports}var s,i,c,l,d,g;r.m=a,r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.f={},r.e=e=>{const t=[];return r.f.j(e,t),Promise.all(t)},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{const e={924:()=>import("./engine-wllama.node.min.mjs")},t={792:0},n=e=>{let{__webpack_esm_ids__:n,__webpack_esm_modules__:a,__webpack_esm_runtime__:o}=e;var s,i,c=0;for(s in a)r.o(a,s)&&(r.m[s]=a[s]);for(o&&o(r);c<n.length;c++)i=n[c],r.o(t,i)&&t[i]&&t[i][0](),t[i]=0};r.f.j=(a,o)=>{let s=r.o(t,a)?t[a]:void 0;if(0!==s)if(s)o.push(s[1]);else{let r=e[a]().then(n,e=>{throw 0!==t[a]&&(t[a]=void 0),e});r=Promise.race([r,new Promise(e=>s=t[a]=[e])]),o.push(s[1]=r)}}})(),function(e){e.system="system",e.user="user",e.assistant="assistant"}(s||(s={})),function(e){e[e.online=0]="online",e[e.away=1]="away",e[e.offline=2]="offline"}(i||(i={})),function(e){e[e.idle=0]="idle",e[e.downloading=1]="downloading",e[e.ready=2]="ready",e[e.error=3]="error",e[e.unsupported=4]="unsupported"}(c||(c={})),function(e){e.cpu="cpu",e.none="none"}(l||(l={})),function(e){e.download="download",e.load="load"}(d||(d={})),function(e){e.qwen3_vl_2b="qwen3-vl-2b"}(g||(g={}));const u=[{id:g.qwen3_vl_2b,reasoning:!0,cpu:{repo:"Qwen/Qwen3-VL-2B-Thinking-GGUF",file:"Qwen3VL-2B-Thinking-Q4_K_M.gguf",mmproj:{file:"mmproj-Qwen3VL-2B-Thinking-Q8_0.gguf"}}}],m=e=>e?u.find(t=>t.id==e):void 0,p=e=>!!e.cpu.mmproj,f=()=>{return a=t(e(n("file:///media/x/store/code/DegreeSign/ds_chat/src/assets/nodeEsm.js")),"../assets/wllama"),{wasm:t(a,"wllama.wasm"),compatWasm:t(a,"compat/wllama.wasm"),compatWorker:t(a,"compat/wllama.js")};var a},h=288,w=async e=>{if(!e||"function"!=typeof fetch||"function"!=typeof createImageBitmap)return e;try{const t=await fetch(e),n=await createImageBitmap(await t.blob()),a=Math.min(n.width,n.height),o=Math.floor((n.width-a)/2),r=Math.floor((n.height-a)/2),s=((e,t)=>{if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(e,t);if("undefined"==typeof document)return;const n=document.createElement("canvas");return n.width=e,n.height=t,n})(h,h),i=s?s.getContext("2d"):null;if(!s||!i||a<1)return n.close(),e;if(i.drawImage(n,o,r,a,a,0,0,h,h),n.close(),(e=>"undefined"!=typeof OffscreenCanvas&&e instanceof OffscreenCanvas)(s)){const t=await s.convertToBlob({type:"image/jpeg",quality:.85});return await(e=>new Promise(t=>{if("undefined"==typeof FileReader)return void t("");const n=new FileReader;n.onerror=()=>t(""),n.onload=()=>t(`${n.result}`),n.readAsDataURL(e)}))(t)||e}return s.toDataURL("image/jpeg",.85)}catch{return e}},y={maxReplyTokens:200,maxThinkingTokens:64,maxReasoningTokens:150,maxImages:2,maxContentLength:1e3,promptCharBudget:1e4,maxThreads:2},x=(e,t,n)=>"number"==typeof e&&Number.isFinite(e)&&e>=n?Math.floor(e):t,k=(e={})=>{const t=y;return{maxReplyTokens:x(e.maxReplyTokens,t.maxReplyTokens,1),maxThinkingTokens:x(e.maxThinkingTokens,t.maxThinkingTokens,1),maxReasoningTokens:x(e.maxReasoningTokens,t.maxReasoningTokens,1),maxImages:x(e.maxImages,t.maxImages,0),maxContentLength:x(e.maxContentLength,t.maxContentLength,1),promptCharBudget:x(e.promptCharBudget,t.promptCharBudget,1),maxThreads:x(e.maxThreads,t.maxThreads,1)}},v=["<|channel>thought\n","<|channel>thought","..."],b=["<channel|>","..."],_=(e,t)=>t.length?[...e?[{type:"text",text:e}]:[],...t.map(e=>({type:"image",url:e}))]:e,T=e=>"string"==typeof e?e?[{type:"text",text:e}]:[]:e,M=(e,t)=>{if("string"==typeof e&&"string"==typeof t)return`${e}\n${t}`;const n=[];for(const a of[...T(e),{type:"text",text:"\n"},...T(t)]){const e=n[n.length-1];"text"==a.type&&"text"==e?.type?e.text+=a.text:n.push(a)}return n},C=async({model:e,logging:t,onReason:n,onText:a,start:o,stopped:r})=>{let s="",i="",c="";const l=((e,t)=>{let n="",a=!1;const o=(e,t)=>{let n=-1,a="";for(const o of t){const t=e.indexOf(o);t>=0&&(n<0||t<n)&&(n=t,a=o)}return{at:n,tag:a}},r=(e,t)=>{let n="";for(const a of t)for(let t=Math.min(e.length,a.length-1);t>n.length;t--)if(e.endsWith(a.slice(0,t))){n=e.slice(e.length-t);break}return n};return{feed:s=>{let i=n+s;for(n="";i;){if(a){const{at:t,tag:s}=o(i,b);if(t>=0){t&&e(i.slice(0,t)),i=i.slice(t+s.length),a=!1;continue}const c=r(i,b);e(i.slice(0,i.length-c.length)),n=c;break}{const{at:e,tag:s}=o(i,v);if(e>=0){e&&t(i.slice(0,e)),i=i.slice(e+s.length),i.startsWith("\n")&&(i=i.slice(1)),a=!0;continue}const c=r(i,v);t(i.slice(0,i.length-c.length)),n=c;break}}},flush:()=>{n&&(a?e(n):t(n),n="")}}})(e=>{i+=e,n(e)},e=>{s+=e,a(e)});try{const e=await o();for await(const t of e){if(r())break;const e=t?.choices?.[0],a=e?.delta,o="string"==typeof a?.reasoning_content?a.reasoning_content:"string"==typeof a?.reasoning?a.reasoning:"";e?.finish_reason&&(c=e.finish_reason),o&&(i+=o,n(o)),a?.content&&l.feed(a.content)}l.flush()}catch(e){t&&console.log("Local chat failed",e)}const d=r()?void 0:s||void 0;return t&&(console.log("Local chat reply",e.id,d?"****":""),console.log("Local chat finish",e.id,c||"none",s.length,i.length)),d},L=async({model:e,params:t,start:n,stopped:a})=>{const o=await(async e=>{const t=[];for(const n of e){if("string"==typeof n.content){t.push(n);continue}const e=[];for(const t of n.content)e.push("image"==t.type?{type:"image",url:await w(t.url)}:t);t.push({role:"user",content:e})}return t})(((e,t=y)=>{const n=t.maxContentLength,a=e.filter(e=>"string"==typeof e.content&&!!e.content.trim()||!(e.role!=s.user||!e.images?.length)),o=[],r=[];let i=0,c=t.maxImages;for(let e=a.length-1;e>=0;e--){const l=a[e],d=("string"==typeof l.content?l.content:"").slice(0,n),g=l.role==s.user?(l.images||[]).filter(e=>"string"==typeof e&&!!e).slice(0,Math.max(0,c)):[];if(d||g.length){if(d&&i+d.length>t.promptCharBudget)break;i+=d.length,c-=g.length,l.role==s.system?o.unshift(d):l.role==s.assistant?r.unshift({role:"assistant",content:d}):r.unshift({role:"user",content:_(d,g)})}}for(;r.length&&"user"!=r[0].role;)r.shift();const l=[];for(const e of r){const t=l[l.length-1];"assistant"==t?.role&&"assistant"==e.role?t.content=`${t.content}\n${e.content}`:"user"==t?.role&&"user"==e.role?t.content=M(t.content,e.content):"assistant"==e.role?l.push({role:"assistant",content:e.content}):l.push({role:"user",content:e.content})}const d=o.join("\n\n"),g=[];return d&&g.push({role:"system",content:d}),g.push(...l),g})(t.messages,t.limits));return t.logging&&((e,t)=>{console.log("Local chat call",e.id,t.map(({role:e,content:t})=>({role:e,content:"****",...Array.isArray(t)?{images:t.filter(e=>"image"==e.type).length}:{}})))})(e,o),await C({model:e,logging:t.logging,onReason:t.onReason,onText:t.onText,start:()=>n(o),stopped:a})};let j,R,W="",$=!1;const q=e=>`${e.wasm}|${e.compatWasm}|${e.compatWorker}`,P=e=>{const t=f();return{wasm:e?.wasm||t.wasm,compatWasm:e?.compatWasm||t.compatWasm,compatWorker:e?.compatWorker||t.compatWorker}},B=e=>{const t=e=>(...t)=>{$&&("string"==typeof t[0]&&t[0].includes("ggml_webgpu: Failed to get an adapter")||e(...t))};return{...e,log:t(e.log),warn:t(e.warn),error:t(e.error)}},I=async()=>{if(j)return j;const e=await r.e(924).then(()=>r(577)),t=R||P(),n=new e.Wllama({default:t.wasm},{logger:B(e.LoggerWithoutDebug)});return R||(R=t,W=q(t)),n.setCompat({wasm:t.compatWasm,worker:t.compatWorker}),j=n,n},O=async()=>{const e=j;j=void 0;try{await(e?.exit())}catch{}},A=(e,t)=>e.name.includes(t)||!!e.metadata?.originalURL?.includes(t),U=e=>[e.cpu.file,...e.cpu.mmproj?[e.cpu.mmproj.file]:[]],E=(e,t)=>`https://huggingface.co/${e}/resolve/main/${t}`,F=async e=>{if(!e.startsWith("data:"))return await(await fetch(e)).arrayBuffer();const t=e.indexOf(","),n=t>=0?e.slice(0,t):"";if(t<0)throw new Error("Invalid image data");if(!n.includes(";base64"))return await(await fetch(e)).arrayBuffer();const a=atob(e.slice(t+1)),o=new Uint8Array(a.length);for(let e=0;e<a.length;e++)o[e]=a.charCodeAt(e);return o.buffer},S=async e=>{const t=[];for(const n of e){if("string"==typeof n.content){"system"==n.role?t.push({role:"system",content:n.content}):"assistant"==n.role?t.push({role:"assistant",content:n.content}):t.push({role:"user",content:n.content});continue}const e=[];for(const t of n.content)e.push("text"==t.type?{type:"text",text:t.text}:{type:"image",data:await F(t.url)});t.push({role:"user",content:e})}return t},Q=async(e,t,n)=>{if($=!0===n.logging,e.isModelLoaded())return await L({model:t,params:n,stopped:()=>!!n.signal?.aborted,start:async a=>{if((e=>e.some(e=>Array.isArray(e.content)&&e.content.some(e=>"image"==e.type)))(a)&&!e.supportInputModality("image"))throw new Error("Model does not accept images");return await e.createChatCompletion({messages:await S(a),...(o=n.limits,r=n.reasoning,{stream:!0,temperature:.7,max_tokens:r?o.maxReasoningTokens:o.maxReplyTokens}),abortSignal:n.signal,...t.reasoning?{chat_template_kwargs:{enable_thinking:n.reasoning}}:{}});var o,r}})},D={backend:l.cpu,startLLM:async({model:e,limits:t,onProgress:n,signal:a,logging:o,assets:r})=>{$=!0===o;const s=P(r),i=q(s);j&&i!=W&&await O(),R=s,W=i;const c=(g="undefined"!=typeof navigator&&navigator.hardwareConcurrency||4,u=t.maxThreads,Math.max(1,Math.min(Math.max(2,Math.min(8,(g||4)-1)),u)));var g,u;const m=await I(),p=E(e.cpu.repo,e.cpu.file),f=e.cpu.mmproj?.file,h=f?E(e.cpu.repo,f):"";if(a?.aborted)throw await O(),new Error("Cancelled");if(f){const t=await m.cacheManager.list(),n=t.some(t=>A(t,e.cpu.file)),a=t.some(e=>A(e,f));n&&!a&&await m.cacheManager.deleteMany(t=>A(t,e.cpu.file))}try{await m.loadModelFromUrl(h?{url:p,mmprojUrl:h}:p,{n_ctx:8192,n_threads:c,reasoning_budget_tokens:t.maxThinkingTokens,n_gpu_layers:0,useCache:!0,jinja:!0,signal:a,progressCallback:({loaded:e,total:t})=>{const a=t?e/t:0;n(a,a>=1?d.load:d.download)}})}catch(e){throw await O(),e}if(a?.aborted)throw await O(),new Error("Cancelled");if(!m.getLoadedContextInfo().n_ctx)throw await O(),new Error("Failed to create model context");return{model:e,backend:l.cpu,contextSize:8192,chat:t=>Q(m,e,t),unload:async()=>{if(j==m)await O();else try{await m.exit()}catch{}}}},cached:async e=>{try{const t=await I(),n=await t.cacheManager.list();return U(e).every(e=>n.some(t=>A(t,e)))}catch{return!1}},hasCache:async()=>{try{const e=await I();return(await e.cacheManager.list()).length>0}catch{return!1}},remove:async e=>{try{const t=await I();await t.cacheManager.deleteMany(t=>U(e).some(e=>A(t,e)))}catch{}},purge:async()=>{try{const e=await I();await e.cacheManager.clear()}catch{}}},V=((e={})=>{const t=e.engines?.cpu||D;let n,a,o,r,s,i,g,m=l.none,p=c.idle,f=0,h={percent:0,phase:d.download};const w=()=>e.backend||("undefined"==typeof window&&"undefined"==typeof self&&"undefined"==typeof Worker||"undefined"==typeof WebAssembly?l.none:l.cpu),y=async()=>{await t.purge()},x=(e,t)=>{h={percent:Math.max(0,Math.min(1,e)),phase:t||d.download},i?.(h.percent,h.phase)},k=e=>{p=e},v=()=>p==c.ready&&!!n&&!!a;return{load:async({model:e,limits:g,assets:p,logging:b,onProgress:_})=>{if(v()&&a?.id==e.id)return!0;if(o&&r==e.id)return i=_,_(h.percent,h.phase),await o;o&&(i=void 0,s?.abort(),await o.catch(()=>!1)),i=_;const T=++f,M=(async()=>{await(async()=>{try{if("undefined"==typeof navigator)return;await(navigator.storage?.persist?.())}catch{}})();const o=w();if(T!=f)return!1;if(o==l.none)return k(c.unsupported),!1;if(await(async e=>{if(a&&a.id!=e.id){const e=a,o=n;n=void 0,a=void 0,await(o?.unload()),await t.remove(e)}if(await t.hasCache())for(const n of u)n.id!=e.id&&await t.remove(n)})(e),T!=f)return!1;const r=await t.cached(e);if(T!=f)return!1;if(!r&&await(async()=>{try{if("undefined"==typeof navigator)return!1;if(!navigator.storage?.estimate)return!1;const{quota:e=0,usage:t=0}=await navigator.storage.estimate();return!!e&&t>=e}catch{return!1}})()&&await y(),T!=f)return!1;k(c.downloading),x(0,d.download),r&&x(1,d.load);const i=new AbortController;s=i;try{const r=await t.startLLM({model:e,limits:g,logging:b,signal:i.signal,assets:p,onProgress:(e,t)=>{T==f&&x(e,t)}});return T!=f||i.signal.aborted?(await r.unload(),!1):(n=r,a=e,m=o,k(c.ready),x(1,d.load),!0)}catch(e){return T!=f||(n=void 0,a=void 0,b&&console.log("Local model failed",e),(e=>["quota","no space","disk full","ns_error_dom_quota_reached"].some(t=>`${e?.name||""} ${e?.message||`${e}`}`.toLowerCase().includes(t)))(e)&&await y(),k(c.error)),!1}finally{s==i&&(s=void 0)}})().finally(()=>{o==M&&(o=void 0,r=void 0)});return o=M,r=e.id,await M},chat:async e=>{if(!v()||!n)return;for(;g;)await g.catch(()=>{});if(!v()||!n)return;const t=n.chat(e);g=t;try{return await t}finally{g==t&&(g=void 0)}},unload:async()=>{f++,s?.abort(),s=void 0,r=void 0;const e=n;n=void 0,a=void 0,k(c.idle),await(e?.unload())},cancelLoad:()=>{f++,s?.abort(),s=void 0,r=void 0,p==c.downloading&&k(c.idle)},ready:v,loading:()=>!v()&&p==c.downloading,status:()=>p,backend:()=>m,model:()=>a}})(),G=(e,t,n)=>{const a=e.status(),o=m(t);return{availability:a==c.ready?i.online:a==c.error||a==c.unsupported?i.offline:i.away,status:a,backend:e.backend(),supportsImages:!!o&&p(o),ready:e.ready(),loading:e.loading(),requesting:n.requesting,phase:n.phase,percent:n.percent,...n.error?{error:n.error}:{}}},N=(e=>{let t,n=0;const a=t=>t.input.onStatus?.(G(e,t.input.model,t));return o=>{t?.controller.abort();const r={token:++n,input:o,controller:new AbortController,percent:e.ready()?100:0,phase:e.ready()?d.load:d.download,requesting:!1,cancelled:!1};return t=r,a(r),(async t=>{const{input:o}=t,r=!0===o.logging,i=m(o.model),c=o.messages.some(e=>e.role!=s.system&&"string"==typeof e.content&&!!e.content.trim()),l=o.messages.some(e=>e.role==s.user&&!!e.images?.some(e=>"string"==typeof e&&!!e)),g=c||l;if(!i)return t.error="Unknown model",void a(t);if(!g)return void a(t);if(l&&!c&&!p(i))return t.error="Model does not accept images",void a(t);const u=k(o.limits),f=await e.load({model:i,limits:u,logging:r,onProgress:(e,o)=>{t.token==n&&(t.percent=Math.round(100*e),t.phase=o,a(t))}}).catch(e=>(t.token==n&&(t.error=`${e?.message||`${e}`}`),!1));if(t.token!=n)return;if(!f)return t.cancelled||t.error||(t.error="Model not ready"),void a(t);const h=p(i),w=l&&!h?o.messages.map(({images:e,...t})=>t):o.messages;if(l&&!h&&!c)return t.error="Model does not accept images",void a(t);t.requesting=!0,t.error=void 0,t.percent=100,t.phase=d.load,a(t);let y,x="",v="";try{y=await e.chat({messages:w,limits:u,reasoning:!0===o.reasoning,logging:r,onReason:e=>{t.token!=n||t.cancelled||(x+=e,o.onThinking?.(x))},onText:e=>{t.token!=n||t.cancelled||(v+=e,o.onResponse?.(v))},signal:t.controller.signal})}catch(e){if(t.token!=n)return;return t.requesting=!1,t.error=`${e?.message||`${e}`}`,void a(t)}t.token==n&&(t.requesting=!1,y||t.cancelled||(t.error="No reply"),a(t))})(r),()=>(t=>{t.token==n&&(t.cancelled=!0,t.controller.abort(),e.status()!=c.ready?(e.cancelLoad(),a(t)):e.unload().then(()=>a(t)))})(r)}})(V),z=(K=V,async e=>{const t=m(e.model),n=t=>e.onStatus?.(G(K,e.model,t));if(!t)return n({requesting:!1,phase:d.download,percent:0,error:"Unknown model"}),!1;let a,o=0,r=d.download;const s=K.ready()&&K.model()?.id==t.id;return await K.load({model:t,limits:k(e.limits),logging:!0===e.logging,onProgress:(e,t)=>{o=Math.round(100*e),r=t,n({requesting:!1,phase:r,percent:o})}}).catch(e=>(a=`${e?.message||`${e}`}`,!1))?(s&&n({requesting:!1,phase:d.load,percent:100}),!0):(n({requesting:!1,phase:r,percent:o,error:a||"Model not ready"}),!1)});var K;export{g as ChatModelId,i as ChatStatus,l as LlmBackend,d as LlmPhase,s as LlmRole,c as LlmStatus,N as createChat,z as preloadModel};
|
|
2
|
+
import{dirname as e,join as t}from"node:path";import{fileURLToPath as n}from"node:url";var a={};const o={};function r(e){const t=o[e];if(void 0!==t)return t.exports;const n=o[e]={exports:{}};return a[e](n,n.exports,r),n.exports}var s,i,c,l,d,g;r.m=a,r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.f={},r.e=e=>{const t=[];return r.f.j(e,t),Promise.all(t)},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{const e={520:()=>import("./engine-litert.node.min.mjs"),924:()=>import("./engine-wllama.node.min.mjs")},t={792:0},n=e=>{let{__webpack_esm_ids__:n,__webpack_esm_modules__:a,__webpack_esm_runtime__:o}=e;var s,i,c=0;for(s in a)r.o(a,s)&&(r.m[s]=a[s]);for(o&&o(r);c<n.length;c++)i=n[c],r.o(t,i)&&t[i]&&t[i][0](),t[i]=0};r.f.j=(a,o)=>{let s=r.o(t,a)?t[a]:void 0;if(0!==s)if(s)o.push(s[1]);else{let r=e[a]().then(n,e=>{throw 0!==t[a]&&(t[a]=void 0),e});r=Promise.race([r,new Promise(e=>s=t[a]=[e])]),o.push(s[1]=r)}}})(),function(e){e.system="system",e.user="user",e.assistant="assistant"}(s||(s={})),function(e){e[e.online=0]="online",e[e.away=1]="away",e[e.offline=2]="offline"}(i||(i={})),function(e){e[e.idle=0]="idle",e[e.downloading=1]="downloading",e[e.ready=2]="ready",e[e.error=3]="error",e[e.unsupported=4]="unsupported"}(c||(c={})),function(e){e.cpu="cpu",e.gpu="gpu",e.none="none"}(l||(l={})),function(e){e.download="download",e.load="load"}(d||(d={})),function(e){e.qwen3_vl_2b="qwen3-vl-2b",e.gemma4_e2b="gemma4-e2b"}(g||(g={}));const m=[{id:g.qwen3_vl_2b,reasoning:!0,cpu:{repo:"Qwen/Qwen3-VL-2B-Thinking-GGUF",file:"Qwen3VL-2B-Thinking-Q4_K_M.gguf",mmproj:{file:"mmproj-Qwen3VL-2B-Thinking-Q8_0.gguf"}}},{id:g.gemma4_e2b,reasoning:!0,litert:{repo:"litert-community/gemma-4-E2B-it-litert-lm",file:"gemma-4-E2B-it.litertlm",vision:!0}}],u=e=>e?m.find(t=>t.id==e):void 0,p=e=>!!e.cpu?.mmproj||!!e.litert?.vision,f=()=>{return a=t(e(n("file:///media/x/store/code/DegreeSign/ds_chat/src/assets/nodeEsm.js")),"../assets/wllama"),{wasm:t(a,"wllama.wasm"),compatWasm:t(a,"compat/wllama.wasm"),compatWorker:t(a,"compat/wllama.js")};var a},h=()=>{return a=t(e(n("file:///media/x/store/code/DegreeSign/ds_chat/src/assets/nodeEsm.js")),"../assets/litert"),{script:t(a,"litertlm_wasm_internal.js"),wasm:t(a,"litertlm_wasm_internal.wasm")};var a},w=288,y=async e=>{if(!e.startsWith("data:"))return await(await fetch(e)).arrayBuffer();const t=e.indexOf(","),n=t>=0?e.slice(0,t):"";if(t<0)throw new Error("Invalid image data");if(!n.includes(";base64"))return await(await fetch(e)).arrayBuffer();const a=atob(e.slice(t+1)),o=new Uint8Array(a.length);for(let e=0;e<a.length;e++)o[e]=a.charCodeAt(e);return o.buffer},x=async e=>{if(!e||"function"!=typeof fetch||"function"!=typeof createImageBitmap)return e;try{const t=await fetch(e),n=await createImageBitmap(await t.blob()),a=Math.min(n.width,n.height),o=Math.floor((n.width-a)/2),r=Math.floor((n.height-a)/2),s=((e,t)=>{if("undefined"!=typeof OffscreenCanvas)return new OffscreenCanvas(e,t);if("undefined"==typeof document)return;const n=document.createElement("canvas");return n.width=e,n.height=t,n})(w,w),i=s?s.getContext("2d"):null;if(!s||!i||a<1)return n.close(),e;if(i.drawImage(n,o,r,a,a,0,0,w,w),n.close(),(e=>"undefined"!=typeof OffscreenCanvas&&e instanceof OffscreenCanvas)(s)){const t=await s.convertToBlob({type:"image/jpeg",quality:.85});return await(e=>new Promise(t=>{if("undefined"==typeof FileReader)return void t("");const n=new FileReader;n.onerror=()=>t(""),n.onload=()=>t(`${n.result}`),n.readAsDataURL(e)}))(t)||e}return s.toDataURL("image/jpeg",.85)}catch{return e}},b={maxReplyTokens:200,maxThinkingTokens:64,maxReasoningTokens:150,maxImages:2,maxContentLength:1e3,promptCharBudget:1e4,maxThreads:2},v=(e,t,n)=>"number"==typeof e&&Number.isFinite(e)&&e>=n?Math.floor(e):t,k=(e={})=>{const t=b;return{maxReplyTokens:v(e.maxReplyTokens,t.maxReplyTokens,1),maxThinkingTokens:v(e.maxThinkingTokens,t.maxThinkingTokens,1),maxReasoningTokens:v(e.maxReasoningTokens,t.maxReasoningTokens,1),maxImages:v(e.maxImages,t.maxImages,0),maxContentLength:v(e.maxContentLength,t.maxContentLength,1),promptCharBudget:v(e.promptCharBudget,t.promptCharBudget,1),maxThreads:v(e.maxThreads,t.maxThreads,1)}},_=(e,t)=>({stream:!0,temperature:.7,max_tokens:t?e.maxReasoningTokens:e.maxReplyTokens}),T=(e,t)=>`https://huggingface.co/${e}/resolve/main/${t}`,M=e=>e.some(e=>Array.isArray(e.content)&&e.content.some(e=>"image"==e.type)),C=["<|channel>thought\n","<|channel>thought","..."],L=["<channel|>","..."],j=(e,t)=>t.length?[...e?[{type:"text",text:e}]:[],...t.map(e=>({type:"image",url:e}))]:e,E=e=>"string"==typeof e?e?[{type:"text",text:e}]:[]:e,R=(e,t)=>{if("string"==typeof e&&"string"==typeof t)return`${e}\n${t}`;const n=[];for(const a of[...E(e),{type:"text",text:"\n"},...E(t)]){const e=n[n.length-1];"text"==a.type&&"text"==e?.type?e.text+=a.text:n.push(a)}return n},W=async({model:e,logging:t,onReason:n,onText:a,start:o,stopped:r})=>{let s="",i="",c="";const l=((e,t)=>{let n="",a=!1;const o=(e,t)=>{let n=-1,a="";for(const o of t){const t=e.indexOf(o);t>=0&&(n<0||t<n)&&(n=t,a=o)}return{at:n,tag:a}},r=(e,t)=>{let n="";for(const a of t)for(let t=Math.min(e.length,a.length-1);t>n.length;t--)if(e.endsWith(a.slice(0,t))){n=e.slice(e.length-t);break}return n};return{feed:s=>{let i=n+s;for(n="";i;){if(a){const{at:t,tag:s}=o(i,L);if(t>=0){t&&e(i.slice(0,t)),i=i.slice(t+s.length),a=!1;continue}const c=r(i,L);e(i.slice(0,i.length-c.length)),n=c;break}{const{at:e,tag:s}=o(i,C);if(e>=0){e&&t(i.slice(0,e)),i=i.slice(e+s.length),i.startsWith("\n")&&(i=i.slice(1)),a=!0;continue}const c=r(i,C);t(i.slice(0,i.length-c.length)),n=c;break}}},flush:()=>{n&&(a?e(n):t(n),n="")}}})(e=>{i+=e,n(e)},e=>{s+=e,a(e)});try{const e=await o();for await(const t of e){if(r())break;const e=t?.choices?.[0],a=e?.delta,o="string"==typeof a?.reasoning_content?a.reasoning_content:"string"==typeof a?.reasoning?a.reasoning:"";e?.finish_reason&&(c=e.finish_reason),o&&(i+=o,n(o)),a?.content&&l.feed(a.content)}l.flush()}catch(e){t&&console.log("Local chat failed",e)}const d=r()?void 0:s||void 0;return t&&(console.log("Local chat reply",e.id,d?"****":""),console.log("Local chat finish",e.id,c||"none",s.length,i.length)),d},$=async({model:e,params:t,start:n,stopped:a})=>{const o=await(async e=>{const t=[];for(const n of e){if("string"==typeof n.content){t.push(n);continue}const e=[];for(const t of n.content)e.push("image"==t.type?{type:"image",url:await x(t.url)}:t);t.push({role:"user",content:e})}return t})(((e,t=b)=>{const n=t.maxContentLength,a=e.filter(e=>"string"==typeof e.content&&!!e.content.trim()||!(e.role!=s.user||!e.images?.length)),o=[],r=[];let i=0,c=t.maxImages;for(let e=a.length-1;e>=0;e--){const l=a[e],d=("string"==typeof l.content?l.content:"").slice(0,n),g=l.role==s.user?(l.images||[]).filter(e=>"string"==typeof e&&!!e).slice(0,Math.max(0,c)):[];if(d||g.length){if(d&&i+d.length>t.promptCharBudget)break;i+=d.length,c-=g.length,l.role==s.system?o.unshift(d):l.role==s.assistant?r.unshift({role:"assistant",content:d}):r.unshift({role:"user",content:j(d,g)})}}for(;r.length&&"user"!=r[0].role;)r.shift();const l=[];for(const e of r){const t=l[l.length-1];"assistant"==t?.role&&"assistant"==e.role?t.content=`${t.content}\n${e.content}`:"user"==t?.role&&"user"==e.role?t.content=R(t.content,e.content):"assistant"==e.role?l.push({role:"assistant",content:e.content}):l.push({role:"user",content:e.content})}const d=o.join("\n\n"),g=[];return d&&g.push({role:"system",content:d}),g.push(...l),g})(t.messages,t.limits));return t.logging&&((e,t)=>{console.log("Local chat call",e.id,t.map(({role:e,content:t})=>({role:e,content:"****",...Array.isArray(t)?{images:t.filter(e=>"image"==e.type).length}:{}})))})(e,o),await W({model:e,logging:t.logging,onReason:t.onReason,onText:t.onText,start:()=>n(o),stopped:a})},q="litertlm-models",P={num_output_candidates:1,wait_for_weight_uploads:!0,num_decode_steps_per_sync:3,sequence_batch_size:0,supported_lora_ranks:[],max_top_k:40,enable_decode_logits:!1,enable_external_embeddings:!1,use_submodel:!1,use_autosized_ringbuffers:!0},B=async()=>{try{if("undefined"==typeof caches)return;return await caches.open(q)}catch{return}},I=async e=>{const t=[];for(const n of e){if("string"==typeof n.content){t.push({role:n.role,content:n.content});continue}const e=[];for(const t of n.content)e.push("text"==t.type?{type:"text",text:t.text}:{type:"image",data:await y(t.url)});t.push({role:"user",content:e})}return t},O=async({engine:e,model:t,mod:n,params:a})=>await $({model:t,params:a,stopped:()=>!!a.signal?.aborted,start:async o=>{if(a.signal?.aborted)throw new Error("Cancelled");if(M(o)&&!t.litert?.vision)throw new Error("Model does not accept images");return async function*({engine:e,mod:t,params:n,messages:a}){const o=a[a.length-1],r=_(n.limits,n.reasoning);if("user"!=o?.role)throw new Error("No user turn");const s=await I(a.slice(0,-1)),i=(await I([o]))[0];if(!i)throw new Error("Empty prompt");const c=await e.createConversation({sessionConfig:{maxOutputTokens:r.max_tokens,samplerParams:{type:t.SamplerType.TOP_K,temperature:r.temperature,k:40,p:.95}},preface:{...s.length?{messages:s}:{},extra_context:{enable_thinking:n.reasoning}}});let l;const d=()=>{l&&l.cancel().catch(()=>{});try{c.cancel()}catch{}};n.signal?.addEventListener("abort",d,{once:!0});try{if(n.signal?.aborted)return;const e=c.sendMessageStreaming(i).getReader();for(l=e;;){const{done:t,value:n}=await e.read();if(t)break;const a={};let o="string"==typeof n.content?n.content:"";if(Array.isArray(n.content))for(const e of n.content)"text"==e.type&&(o+=e.text);o&&(a.content=o);const r=n.channels?.thought;r&&(a.reasoning=r),(a.content||a.reasoning)&&(yield{choices:[{delta:a}]})}}finally{n.signal?.removeEventListener("abort",d);try{await c.delete()}catch{}}}({engine:e,mod:n,params:a,messages:o})}}),A={backend:l.gpu,startLLM:async({model:e,limits:t,onProgress:n,signal:a,logging:o})=>{const s=e.litert;if(!s)throw new Error("Model has no LiteRT artifact");const i=await r.e(520).then(()=>r(507));if(await i.getOrLoadGlobalLiteRtLm(h().script),a?.aborted)throw new Error("Cancelled");const c=T(s.repo,s.file),g=await B(),m=g?await g.match(c):void 0;let u;if(m?.body)u=m.body,n(1,d.load);else{const e=await fetch(c,{signal:a});if(!e.ok||!e.body)throw new Error("Model download failed");const t=Number(e.headers.get("content-length"))||0;let r=0;const s=e.body.pipeThrough(new TransformStream({transform:(e,a)=>{r+=e.byteLength,n(t?r/t:0,d.download),a.enqueue(e)}}));if(g){const[e,n]=s.tee();g.put(c,new Response(n,{headers:{"content-type":"application/octet-stream",...t?{"content-length":`${t}`}:{}}})).catch(e=>{o&&console.log("Local model cache failed",e)}),u=e}else u=s}if(a?.aborted)throw new Error("Cancelled");n(1,d.load);const p=await i.Engine.create({model:u,mainExecutorSettings:{maxNumTokens:4096,...s.vision?{maxNumImages:Math.max(1,t.maxImages)}:{},backendConfig:P},...s.vision?{visionBackend:i.Backend.GPU}:{}});if(a?.aborted)throw await p.delete(),new Error("Cancelled");return{model:e,backend:l.gpu,contextSize:4096,chat:t=>O({engine:p,model:e,mod:i,params:t}),unload:async()=>{await p.delete()}}},cached:async e=>{try{const t=e.litert,n=await B();return!(!t||!n)&&!!await n.match(T(t.repo,t.file))}catch{return!1}},hasCache:async()=>{try{const e=await B();return!!e&&(await e.keys()).length>0}catch{return!1}},remove:async e=>{try{const t=e.litert,n=await B();if(!t||!n)return;await n.delete(T(t.repo,t.file))}catch{}},purge:async()=>{try{"undefined"!=typeof caches&&await caches.delete(q)}catch{}}};let S,U,F="",N=!1;const Q=e=>`${e.wasm}|${e.compatWasm}|${e.compatWorker}`,D=e=>{const t=f();return{wasm:e?.wasm||t.wasm,compatWasm:e?.compatWasm||t.compatWasm,compatWorker:e?.compatWorker||t.compatWorker}},z=e=>{const t=e=>(...t)=>{N&&("string"==typeof t[0]&&t[0].includes("ggml_webgpu: Failed to get an adapter")||e(...t))};return{...e,log:t(e.log),warn:t(e.warn),error:t(e.error)}},G=async()=>{if(S)return S;const e=await r.e(924).then(()=>r(577)),t=U||D(),n=new e.Wllama({default:t.wasm},{logger:z(e.LoggerWithoutDebug)});return U||(U=t,F=Q(t)),n.setCompat({wasm:t.compatWasm,worker:t.compatWorker}),S=n,n},V=async()=>{const e=S;S=void 0;try{await(e?.exit())}catch{}},K=(e,t)=>e.name.includes(t)||!!e.metadata?.originalURL?.includes(t),H=e=>e.cpu?[e.cpu.file,...e.cpu.mmproj?[e.cpu.mmproj.file]:[]]:[],J=async e=>{const t=[];for(const n of e){if("string"==typeof n.content){"system"==n.role?t.push({role:"system",content:n.content}):"assistant"==n.role?t.push({role:"assistant",content:n.content}):t.push({role:"user",content:n.content});continue}const e=[];for(const t of n.content)e.push("text"==t.type?{type:"text",text:t.text}:{type:"image",data:await y(t.url)});t.push({role:"user",content:e})}return t},X={backend:l.cpu,startLLM:async({model:e,limits:t,onProgress:n,signal:a,logging:o,assets:r})=>{N=!0===o;const s=e.cpu;if(!s)throw new Error("Model has no CPU artifact");const i=D(r),c=Q(i);S&&c!=F&&await V(),U=i,F=c;const g=(m="undefined"!=typeof navigator&&navigator.hardwareConcurrency||4,u=t.maxThreads,Math.max(1,Math.min(Math.max(2,Math.min(8,(m||4)-1)),u)));var m,u;const p=await G(),f=T(s.repo,s.file),h=s.mmproj?.file,w=h?T(s.repo,h):"";if(a?.aborted)throw await V(),new Error("Cancelled");if(h){const e=await p.cacheManager.list(),t=e.some(e=>K(e,s.file)),n=e.some(e=>K(e,h));t&&!n&&await p.cacheManager.deleteMany(e=>K(e,s.file))}try{await p.loadModelFromUrl(w?{url:f,mmprojUrl:w}:f,{n_ctx:8192,n_threads:g,reasoning_budget_tokens:t.maxThinkingTokens,n_gpu_layers:0,useCache:!0,jinja:!0,signal:a,progressCallback:({loaded:e,total:t})=>{const a=t?e/t:0;n(a,a>=1?d.load:d.download)}})}catch(e){throw await V(),e}if(a?.aborted)throw await V(),new Error("Cancelled");if(!p.getLoadedContextInfo().n_ctx)throw await V(),new Error("Failed to create model context");return{model:e,backend:l.cpu,contextSize:8192,chat:t=>(async(e,t,n)=>{if(N=!0===n.logging,e.isModelLoaded())return await $({model:t,params:n,stopped:()=>!!n.signal?.aborted,start:async a=>{if(M(a)&&!e.supportInputModality("image"))throw new Error("Model does not accept images");return await e.createChatCompletion({messages:await J(a),..._(n.limits,n.reasoning),abortSignal:n.signal,...t.reasoning?{chat_template_kwargs:{enable_thinking:n.reasoning}}:{}})}})})(p,e,t),unload:async()=>{if(S==p)await V();else try{await p.exit()}catch{}}}},cached:async e=>{try{if(!e.cpu)return!1;const t=await G(),n=await t.cacheManager.list();return H(e).every(e=>n.some(t=>K(t,e)))}catch{return!1}},hasCache:async()=>{try{const e=await G();return(await e.cacheManager.list()).length>0}catch{return!1}},remove:async e=>{try{if(!e.cpu)return;const t=await G();await t.cacheManager.deleteMany(t=>H(e).some(e=>K(t,e)))}catch{}},purge:async()=>{try{const e=await G();await e.cacheManager.clear()}catch{}}},Y=((e={})=>{const t=e.engines?.cpu||X,n=e.engines?.litert||A;let a,o,r,s,i,g,u,p,f=l.none,h=c.idle,w=0,y={percent:0,phase:d.download};const x=a=>{if(e.backend!=l.none)return e.backend==l.cpu?a.cpu?t:void 0:e.backend==l.gpu?a.litert?n:void 0:a.litert&&"undefined"!=typeof navigator&&navigator.gpu&&"undefined"!=typeof WebAssembly?n:!a.cpu||"undefined"==typeof window&&"undefined"==typeof self&&"undefined"==typeof Worker||"undefined"==typeof WebAssembly?void 0:t},b=(e,t)=>{y={percent:Math.max(0,Math.min(1,e)),phase:t||d.download},u?.(y.percent,y.phase)},v=e=>{h=e},k=()=>h==c.ready&&!!a&&!!o;return{load:async({model:e,limits:t,assets:n,logging:l,onProgress:p})=>{if(k()&&o?.id==e.id)return!0;if(s&&i==e.id)return u=p,p(y.percent,y.phase),await s;s&&(u=void 0,g?.abort(),await s.catch(()=>!1)),u=p;const h=++w,_=(async()=>{await(async()=>{try{if("undefined"==typeof navigator)return;await(navigator.storage?.persist?.())}catch{}})();const s=x(e);if(h!=w)return!1;if(!s)return v(c.unsupported),!1;if(await(async(e,t)=>{if(o&&o.id!=e.id){const e=o,t=r,n=a;a=void 0,o=void 0,r=void 0,await(n?.unload()),await(t?.remove(e))}if(await t.hasCache())for(const n of m)n.id!=e.id&&x(n)==t&&await t.remove(n)})(e,s),h!=w)return!1;const i=await s.cached(e);if(h!=w)return!1;if(!i&&await(async()=>{try{if("undefined"==typeof navigator)return!1;if(!navigator.storage?.estimate)return!1;const{quota:e=0,usage:t=0}=await navigator.storage.estimate();return!!e&&t>=e}catch{return!1}})()&&await s.purge(),h!=w)return!1;v(c.downloading),b(0,d.download),i&&b(1,d.load);const u=new AbortController;g=u;try{const i=await s.startLLM({model:e,limits:t,logging:l,signal:u.signal,assets:n,onProgress:(e,t)=>{h==w&&b(e,t)}});return h!=w||u.signal.aborted?(await i.unload(),!1):(a=i,o=e,r=s,f=s.backend,v(c.ready),b(1,d.load),!0)}catch(e){return h!=w||(a=void 0,o=void 0,r=void 0,l&&console.log("Local model failed",e),(e=>["quota","no space","disk full","ns_error_dom_quota_reached"].some(t=>`${e?.name||""} ${e?.message||`${e}`}`.toLowerCase().includes(t)))(e)&&await s.purge(),v(c.error)),!1}finally{g==u&&(g=void 0)}})().finally(()=>{s==_&&(s=void 0,i=void 0)});return s=_,i=e.id,await _},chat:async e=>{if(!k()||!a)return;for(;p;)await p.catch(()=>{});if(!k()||!a)return;const t=a.chat(e);p=t;try{return await t}finally{p==t&&(p=void 0)}},unload:async()=>{w++,g?.abort(),g=void 0,i=void 0;const e=a;a=void 0,o=void 0,r=void 0,v(c.idle),await(e?.unload())},cancelLoad:()=>{w++,g?.abort(),g=void 0,i=void 0,h==c.downloading&&v(c.idle)},ready:k,loading:()=>!k()&&h==c.downloading,status:()=>h,backend:()=>f,model:()=>o}})(),Z=(e,t,n)=>{const a=e.status(),o=u(t);return{availability:a==c.ready?i.online:a==c.error||a==c.unsupported?i.offline:i.away,status:a,backend:e.backend(),supportsImages:!!o&&p(o),ready:e.ready(),loading:e.loading(),requesting:n.requesting,phase:n.phase,percent:n.percent,...n.error?{error:n.error}:{}}},ee=(e=>{let t,n=0;const a=t=>t.input.onStatus?.(Z(e,t.input.model,t));return o=>{t?.controller.abort();const r={token:++n,input:o,controller:new AbortController,percent:e.ready()?100:0,phase:e.ready()?d.load:d.download,requesting:!1,cancelled:!1};return t=r,a(r),(async t=>{const{input:o}=t,r=!0===o.logging,i=u(o.model),c=o.messages.some(e=>e.role!=s.system&&"string"==typeof e.content&&!!e.content.trim()),l=o.messages.some(e=>e.role==s.user&&!!e.images?.some(e=>"string"==typeof e&&!!e)),g=c||l;if(!i)return t.error="Unknown model",void a(t);if(!g)return void a(t);if(l&&!c&&!p(i))return t.error="Model does not accept images",void a(t);const m=k(o.limits),f=await e.load({model:i,limits:m,logging:r,onProgress:(e,o)=>{t.token==n&&(t.percent=Math.round(100*e),t.phase=o,a(t))}}).catch(e=>(t.token==n&&(t.error=`${e?.message||`${e}`}`),!1));if(t.token!=n)return;if(!f)return t.cancelled||t.error||(t.error="Model not ready"),void a(t);const h=p(i),w=l&&!h?o.messages.map(({images:e,...t})=>t):o.messages;if(l&&!h&&!c)return t.error="Model does not accept images",void a(t);t.requesting=!0,t.error=void 0,t.percent=100,t.phase=d.load,a(t);let y,x="",b="";try{y=await e.chat({messages:w,limits:m,reasoning:!0===o.reasoning,logging:r,onReason:e=>{t.token!=n||t.cancelled||(x+=e,o.onThinking?.(x))},onText:e=>{t.token!=n||t.cancelled||(b+=e,o.onResponse?.(b))},signal:t.controller.signal})}catch(e){if(t.token!=n)return;return t.requesting=!1,t.error=`${e?.message||`${e}`}`,void a(t)}t.token==n&&(t.requesting=!1,y||t.cancelled||(t.error="No reply"),a(t))})(r),()=>(t=>{t.token==n&&(t.cancelled=!0,t.controller.abort(),e.status()!=c.ready?(e.cancelLoad(),a(t)):e.unload().then(()=>a(t)))})(r)}})(Y),te=(ne=Y,async e=>{const t=u(e.model),n=t=>e.onStatus?.(Z(ne,e.model,t));if(!t)return n({requesting:!1,phase:d.download,percent:0,error:"Unknown model"}),!1;let a,o=0,r=d.download;const s=ne.ready()&&ne.model()?.id==t.id;return await ne.load({model:t,limits:k(e.limits),logging:!0===e.logging,onProgress:(e,t)=>{o=Math.round(100*e),r=t,n({requesting:!1,phase:r,percent:o})}}).catch(e=>(a=`${e?.message||`${e}`}`,!1))?(s&&n({requesting:!1,phase:d.load,percent:100}),!0):(n({requesting:!1,phase:r,percent:o,error:a||"Model not ready"}),!1)});var ne;export{g as ChatModelId,i as ChatStatus,l as LlmBackend,d as LlmPhase,s as LlmRole,c as LlmStatus,ee as createChat,te as preloadModel};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! For license information please see engine-litert.node.min.js.LICENSE.txt */
|
|
2
|
+
"use strict";exports.ids=[520],exports.modules={507(e,t,s){s.r(t),s.d(t,{AutoToolChat:()=>R,Backend:()=>U,Conversation:()=>$,EmbeddingEngine:()=>ce,Engine:()=>te,InputOverflowStrategy:()=>N,LiteRtLm:()=>g,LiteRtLmNotLoadedError:()=>i,SUPPORTED_AUDIO_MIME_TYPES:()=>ne,SUPPORTED_IMAGE_MIME_TYPES:()=>se,SamplerType:()=>G,Session:()=>Q,Wasm:()=>n,fillWasmEmbeddingEngineSettingsFromEmbeddingEngineSettings:()=>ie,fillWasmEngineSettingsFromEngineSettings:()=>W,getGlobalLiteRtLm:()=>o,getGlobalLiteRtLmPromise:()=>u,getOrLoadGlobalLiteRtLm:()=>B,hasGlobalLiteRtLm:()=>c,hasGlobalLiteRtLmPromise:()=>d,isWebMcpTool:()=>_,loadLiteRtLm:()=>E,sessionConfigToWasmSessionConfig:()=>K,setGlobalLiteRtLm:()=>l,setGlobalLiteRtLmPromise:()=>h,setupDefaultWebGpuDevice:()=>m,toTool:()=>C,unloadLiteRtLm:()=>T,wasmEngineSettingsToEngineSettings:()=>q,wasmSessionConfigToSessionConfig:()=>Y});var n={};s.r(n),s.d(n,{Backend:()=>U,InputOverflowStrategy:()=>N,ReadableStreamDataStreamWrapper:()=>I,SamplerType:()=>G});class i extends Error{constructor(){super("LiteRT-LM is not initialized yet. Please call loadLiteRtLm() and wait for its promise to resolve to load the LiteRT-LM WASM module.")}}let a,r;function o(){if(!a)throw new i;return a}function c(){return Boolean(a)}function l(e){a=e}function u(){return r}function d(){return Boolean(r)}function h(e){r=e}async function m(){await o().setupDefaultWebGpuDevice()}class g{liteRtLmWasm;static DEFAULT_WASM_PATH="https://cdn.jsdelivr.net/npm/@litert-lm/core@0.19.0-nightly.20261024/wasm";constructor(e){this.liteRtLmWasm=e,this.liteRtLmWasm.setupLogging()}async setupDefaultWebGpuDevice(){this.liteRtLmWasm.preinitializedWebGPUDevice||(this.liteRtLmWasm.preinitializedWebGPUDevice=await async function(){const e={powerPreference:"high-performance"},t=await navigator.gpu.requestAdapter(e);if(!t)throw new Error("No GPU adapter found.");const s={maxBufferSize:t.limits.maxBufferSize,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxStorageBuffersPerShaderStage:t.limits.maxStorageBuffersPerShaderStage,maxTextureDimension2D:t.limits.maxTextureDimension2D},n=[];for(const e of f)t.features.has(e)&&n.push(e);return await t.requestDevice({requiredFeatures:n,requiredLimits:s})}())}delete(){}}const f=["shader-f16","subgroups"];async function p(e){if("function"!=typeof importScripts){const t=document.createElement("script");return t.src=e.toString(),t.crossOrigin="anonymous",new Promise((e,s)=>{t.addEventListener("load",()=>{e()},!1),t.addEventListener("error",e=>{s(e)},!1),document.body.appendChild(t)})}importScripts(e.toString())}const y=new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,15,1,13,0,65,1,253,15,65,2,253,15,253,128,2,11]),w=new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]),b={relaxedSimd:void 0,threads:void 0,jspi:void 0};async function v(e){try{return await WebAssembly.instantiate(e),{supported:!0}}catch(e){return{supported:!1,error:e}}}const S={relaxedSimd:()=>(void 0===b.relaxedSimd&&(b.relaxedSimd=v(y)),b.relaxedSimd),threads:()=>{if(void 0===b.threads)try{"undefined"!=typeof MessageChannel&&(new MessageChannel).port1.postMessage(new SharedArrayBuffer(1)),b.threads=v(w)}catch(e){b.threads=Promise.resolve({supported:!1,error:e})}return b.threads},jspi:()=>{if(void 0===b.jspi)try{const e="Suspending"in WebAssembly;b.jspi=Promise.resolve({supported:e,error:e?void 0:new Error("JSPI is not supported")})}catch(e){b.jspi=Promise.resolve({supported:!1,error:e})}return b.jspi}};async function k(e){const t=S[e]?.();if(!t)throw new Error(`Unknown feature: ${e}`);return(await t).supported}async function x(e,t){const s=e,n=await k("relaxedSimd"),i=await k("jspi");let a="";a=n?i?"litertlm_wasm_internal.js":"litertlm_wasm_asyncify_internal.js":i?"litertlm_wasm_compat_internal.js":"litertlm_wasm_compat_asyncify_internal.js";let r=e;if(s.endsWith(".wasm"))throw new Error("Please load the `.js` file corresponding to the `.wasm` file, or load the directory containing it.");return s.endsWith(".js")||(r=function(e,t){return e?t?(e.endsWith("/")?e:e+"/")+(t.startsWith("/")?t.substring(1):t):e:t}(e,a)),(async(e,t,s,n,i)=>{if(t&&await p(t),!self.ModuleFactory)throw new Error("ModuleFactory not set.");if(s&&(await p(s),!self.ModuleFactory))throw new Error("ModuleFactory not set.");if(self.Module&&i){const e=self.Module;e.locateFile=i.locateFile,i.mainScriptUrlOrBlob&&(e.mainScriptUrlOrBlob=i.mainScriptUrlOrBlob)}const a=await self.ModuleFactory(self.Module||i);return self.ModuleFactory=self.Module=void 0,new e(a,n)})(g,r)}let P;function E(e,t){if(d())throw new Error("LiteRT-LM is already loading / loaded.");return h(x(e).then(async e=>(l(e),e)).catch(e=>{throw h(void 0),e})),u()}function T(){if(d()&&!c())throw new Error("LiteRT-LM is loading and can not be unloaded or canceled until it is finished loading.");c()&&(o().delete(),l(void 0),P=void 0),h(void 0)}function B(e){const t=u();if(t){if(e&&P!==e)throw new Error(`LiteRT-LM is already loading / loaded with a different path ${P} but was requested to load ${e}. To reload with a different path, call unloadLiteRtLm() first.`);return t}return P=e??g.DEFAULT_WASM_PATH,t??E(P)}function _(e){return"inputSchema"in e}function C(e){const{inputSchema:t,execute:s,...n}=e;return{type:"function",function:{...n,parameters:t}}}const A="Conversation is busy. A generation is already in progress.";class R{options;baseConversation=null;isBusy=!1;isCancelled=!1;tools=new Map;nextToolCallId=1;currentBatch=null;constructor(e){if(this.options=e,e.config?.preface?.tools&&e.config.preface.tools.length>0)throw new Error("AutoToolChat handles tools itself. Do not provide tools via ConversationConfig.preface.tools.");const t=e.tools||[];for(const e of t){let t;if(_(e))t={...C(e),execute:e.execute};else if("function"in e)t=e;else{const{execute:s,...n}=e;t={type:"function",function:n,execute:s}}const s=t.function.name;if(!s)throw new Error("Tool must have a name.");if(!t.execute)throw new Error(`Tool ${s} must have an execute function.`);if(this.tools.has(s))throw new Error(`Duplicate tool name ${s} provided.`);this.tools.set(s,t)}}async getConversation(){if(!this.baseConversation){const e=this.options.config?{...this.options.config}:{};this.tools.size>0&&(e.preface||(e.preface={}),e.preface.tools=Array.from(this.tools.values()).map(e=>{const{execute:t,...s}=e;return s})),this.baseConversation=await this.options.engine.createConversation(e)}return this.baseConversation}async sendMessage(e){if(this.isBusy)throw new Error(A);this.isBusy=!0,this.isCancelled=!1;try{const t=await this.getConversation();let s=e;const n=this.options.recurringToolCallLimit??25;let i=0;for(;i<n;){i++;const e=await t.sendMessage(s);if(e.tool_calls&&e.tool_calls.length>0){const t=await this.executeToolCalls(e.tool_calls);if(this.isCancelled)throw new Error("Conversation cancelled");s={role:"tool",content:t};continue}return e}throw new Error("Tool calling exceeded the recurring limit")}finally{this.isBusy=!1}}sendMessageStreaming(e){if(this.isBusy)throw new Error(A);this.isBusy=!0,this.isCancelled=!1;let t=!1;return new ReadableStream({start:s=>{let n=e,i=0;const a=this.options.recurringToolCallLimit??25,r=async()=>{if(i>=a)return this.isBusy=!1,void s.error(new Error("Tool calling exceeded the recurring limit"));i++;let e=[];try{const i=(await this.getConversation()).sendMessageStreaming(n);for await(const n of i){if(t||this.isCancelled)break;if(n.tool_calls&&n.tool_calls.length>0){e=e.concat(n.tool_calls);const t={...n};delete t.tool_calls,t.content&&s.enqueue(t)}else s.enqueue(n)}}catch(e){return this.isBusy=!1,void s.error(e)}if(!t&&!this.isCancelled){if(e.length>0){const s=await this.executeToolCalls(e);if(t||this.isCancelled)return;return n={role:"tool",content:s},void await r()}this.isBusy=!1,s.close()}};r().catch(e=>{t||this.isCancelled||(this.isBusy=!1,s.error(e))})},cancel:()=>{t=!0,this.cancel()}})}cancel(){this.isCancelled=!0,this.currentBatch&&this.currentBatch.cancel(),this.baseConversation&&this.baseConversation.cancel()}async getHistory(){return(await this.getConversation()).getHistory()}async getTokenCount(){return(await this.getConversation()).getTokenCount()}async getBenchmarkInfo(){return(await this.getConversation()).getBenchmarkInfo()}async delete(){this.baseConversation&&(await this.baseConversation.delete(),this.baseConversation=null)}async executeToolCalls(e){this.currentBatch=new M(this.tools,this.options.onToolProgress,()=>this.nextToolCallId++);try{return await this.currentBatch.executeAll(e)}finally{this.currentBatch=null}}}class M{tools;onToolProgress;getNextId;isCancelled=!1;constructor(e,t,s){this.tools=e,this.onToolProgress=t,this.getNextId=s}cancel(){this.isCancelled=!0}async executeAll(e){return Promise.all(e.map(async e=>{const t=this.tools.get(e.function.name),s=this.getNextId();let n;if(this.onToolProgress&&this.onToolProgress({id:s,name:e.function.name,arguments:e.function.arguments,status:"started"}),t)try{n=await t.execute(e.function.arguments),!this.isCancelled&&this.onToolProgress&&this.onToolProgress({id:s,name:e.function.name,arguments:e.function.arguments,status:"completed",result:n})}catch(t){const i=String(t);n={error:`Error executing tool ${e.function.name}: ${i}`},!this.isCancelled&&this.onToolProgress&&this.onToolProgress({id:s,name:e.function.name,arguments:e.function.arguments,status:"error",error:i})}else{const t=`Tool ${e.function.name} not found.`;n={error:t},!this.isCancelled&&this.onToolProgress&&this.onToolProgress({id:s,name:e.function.name,arguments:e.function.arguments,status:"error",error:t})}return{type:"tool_response",name:e.function.name,response:n}}))}}class L{size;data;fullyDiscarded=!1;discardedRegions=[];constructor(e){this.size=e}discard(e,t){if(this.fullyDiscarded)return;if(0===this.discardedRegions.length)return this.discardedRegions.push([e,t]),void this.checkFullyDiscarded();this.discardedRegions.push([e,t]),this.discardedRegions.sort((e,t)=>e[0]-t[0]);const s=[];for(const e of this.discardedRegions){const t=s[s.length-1];!t||t[1]<e[0]?s.push(e):t[1]=Math.max(t[1],e[1])}this.discardedRegions=s,this.checkFullyDiscarded()}getDiscardedRegions(e=0,t=this.size){const s=[];for(const[n,i]of this.discardedRegions){if(n>=t)break;i<=e||s.push([n,i])}return s}checkFullyDiscarded(){1===this.discardedRegions.length&&0===this.discardedRegions[0][0]&&this.discardedRegions[0][1]===this.size&&(this.fullyDiscarded=!0,this.data=void 0)}}class I{stream;getWasmHeap;bytesPerSlice;slices=[];streamDone=!1;globalPosition=0;reader;leftoverData;isReading=!1;constructor(e,t,s=1e7){this.stream=e,this.getWasmHeap=t,this.bytesPerSlice=s;try{this.reader={byobReader:this.stream.getReader({mode:"byob"})}}catch(e){this.reader={defaultReader:this.stream.getReader()}}}*getViews(e,t){if(e>=t)return;let s,n,i=e;for(;i<t;){const e=Math.floor(i/this.bytesPerSlice),a=i%this.bytesPerSlice,r=e*this.bytesPerSlice;let o=this.slices[e];o||(s&&n&&n.fullyDiscarded||(s=new Uint8Array(this.bytesPerSlice)),o=new L(this.bytesPerSlice),o.data=s,this.slices[e]=o);const c=Math.min(this.bytesPerSlice,t-r),l=()=>{o.discard(a,c)},u=c-a,d=e=>{o.data=new Uint8Array(e.buffer,0,e.buffer.byteLength),s=o.data},h=()=>{const t=o.getDiscardedRegions(a,c);if(t.length>0){const s=t.map(([e,t])=>`[${e+r}, ${t+r}]`);throw new Error(`Slice ${e} for data at ${i} overlaps discarded regions: [${s.join(", ")}]`)}if(!o.data)throw new Error(`No ArrayBuffer for slice ${e} at position ${i}`)},m=()=>(h(),new Uint8Array(o.data.buffer,o.data.byteOffset+a,o.data.byteOffset+u)),g=()=>{if(o.data)return new Uint8Array(o.data.buffer,o.data.byteOffset+a,u)};yield{getView:m,getUncheckedView:g,discardView:l,setView:d,checkReadable:h,viewLength:u},n=o,i+=u}}async readInternal(e,t,s,n){if(this.isReading)throw new Error("Concurrent reads are not supported");this.isReading=!0;try{let i;for(const{getUncheckedView:e,setView:s,viewLength:n}of this.getViews(this.globalPosition,t)){const t=e();let a;t?a=t:((!i||i.byteLength<n)&&(i=new ArrayBuffer(this.bytesPerSlice)),a=new Uint8Array(i,0,n));const r=await this.fillBufferFromStream(a);if(t?s(r):i=r.buffer,0===r.length)break}const a=Math.min(this.globalPosition,t+s);for(const{getView:s,discardView:i}of this.getViews(t,a)){const t=s();this.getWasmHeap().set(t,e),n&&i(),e+=t.length}const r=Math.max(a,t);for(const{getView:i,setView:a,discardView:o}of this.getViews(r,t+s)){if(this.streamDone&&this.globalPosition<=r)break;const t=await this.fillBufferFromStream(i());a(t),this.getWasmHeap().set(t,e),n&&o(),e+=t.length}}finally{this.isReading=!1}}async fillBufferFromStream(e){const t=e.byteLength;if(0===t)return e;const s=e.byteOffset;let n=0,i=e.buffer;for(;n<t;){if(this.streamDone&&(!this.leftoverData||0===this.leftoverData.length))throw new Error(`Read from stream returned early EOF at position ${this.globalPosition}. Expected ${t-n} more bytes.`);if(this.reader.byobReader){const e=new Uint8Array(i,s+n,t-n),a=await this.reader.byobReader.read(e);if(a.done&&(this.streamDone=!0),!a.value)throw new Error("Read from stream returned null value");this.globalPosition+=a.value.byteLength,n+=a.value.byteLength,i=a.value.buffer}else{let e;if(this.leftoverData&&this.leftoverData.length>0)e=this.leftoverData,this.leftoverData=void 0;else{const t=await this.reader.defaultReader.read();if(t.done&&(this.streamDone=!0,!t.value))continue;if(!t.value)throw new Error("Read from stream returned null value");e=t.value}const a=t-n,r=Math.min(e.length,a);new Uint8Array(i,s+n,r).set(e.subarray(0,r)),e.length>r&&(this.leftoverData=e.subarray(r)),this.globalPosition+=r,n+=r}}const a=new Uint8Array(i,s,n);if(a.length!==t)throw new Error(`Read from stream returned ${a.length} bytes, expected ${t}`);return a}doDiscard(e,t){for(const{discardView:s}of this.getViews(e,e+t))s()}async readAndDiscard(e,t,s){try{return await this.readInternal(e>>>0,D(t),D(s),!0),{}}catch(e){return{error:e}}}async readAndPreserve(e,t,s){try{return await this.readInternal(e>>>0,D(t),D(s),!1),{}}catch(e){return{error:e}}}discard(e,t){try{return this.doDiscard(D(e),D(t)),{}}catch(e){return{error:e}}}}function D(e){if("number"==typeof e)return e;if(e>Number.MAX_SAFE_INTEGER)throw new Error(`BigInt value ${e} too large to be represented as a number`);return Number(e)}const U={UNSPECIFIED:0,CPU_ARTISAN:1,GPU_ARTISAN:2,CPU:3,GPU:4,GOOGLE_TENSOR_ARTISAN:5,NPU:6},G={TYPE_UNSPECIFIED:0,TOP_K:1,TOP_P:2,GREEDY:3},N={CHUNK_AND_AVERAGE:0,TRUNCATE:1,ERROR:2};function O(e){const t=new Array(e.size());for(let s=0;s<e.size();++s)t[s]=e.get(s);return e.delete(),t}function V(e,t){for(const s of e)t.push_back(s)}function W(e,t,s,n=o().liteRtLmWasm){const i=e.getMutableMainExecutorSettings();if(i.setCacheDir(":nocache"),t.benchmarkEnabled&&e.enableBenchmark(),void 0!==t.maxVisionTokensPerImage&&e.setMaxVisionTokensPerImage(t.maxVisionTokensPerImage),void 0!==t.minVisionTokensPerImage&&e.setMinVisionTokensPerImage(t.minVisionTokensPerImage),t.mainExecutorSettings){const e=t.mainExecutorSettings;if(void 0!==e.maxNumTokens&&i.setMaxNumTokens(e.maxNumTokens),void 0!==e.maxNumImages&&i.setMaxNumImages(e.maxNumImages),void 0!==e.samplerBackend&&i.setSamplerBackend({value:e.samplerBackend}),void 0!==e.backendConfig){const t=e.backendConfig;if(s===U.CPU){const e=t,s={kv_increment_size:e.kv_increment_size??0,prefill_chunk_size:e.prefill_chunk_size??0,number_of_threads:e.number_of_threads??0};i.setBackendConfigCpu(s)}else if(s===U.GPU){const e=t,s={max_top_k:e.max_top_k??1,external_tensor_mode:e.external_tensor_mode??!1};i.setBackendConfigGpu(s)}else if(s===U.GPU_ARTISAN){const e=t,s=new n.VectorUint32;V(e.supported_lora_ranks??[],s);const a={num_output_candidates:e.num_output_candidates??1,wait_for_weight_uploads:e.wait_for_weight_uploads??!0,num_decode_steps_per_sync:e.num_decode_steps_per_sync??1,sequence_batch_size:e.sequence_batch_size??0,supported_lora_ranks:s,max_top_k:e.max_top_k??1,enable_decode_logits:e.enable_decode_logits??!1,enable_external_embeddings:e.enable_external_embeddings??!1,use_submodel:e.use_submodel??!0,use_autosized_ringbuffers:e.use_autosized_ringbuffers??!0};i.setBackendConfigGpuArtisan(a),s.delete()}}if(void 0!==e.advancedSettings){const t={...i.getAdvancedSettings(),...e.advancedSettings};i.setAdvancedSettings(t)}}}function q(e){const t=e.getMutableMainExecutorSettings();let s,n;try{s=t.getBackendConfigCpu(),n=U.CPU}catch(e){try{s=t.getBackendConfigGpu(),n=U.GPU}catch(e){try{const e=t.getBackendConfigGpuArtisan();s={...e,supported_lora_ranks:O(e.supported_lora_ranks)},n=U.GPU_ARTISAN}catch(e){throw new Error("Unsupported backend config")}}}return{backend:n,visionBackend:e.getVisionBackend()?.value,maxVisionTokensPerImage:e.getMaxVisionTokensPerImage(),minVisionTokensPerImage:e.getMinVisionTokensPerImage(),audioBackend:e.getAudioBackend()?.value,mainExecutorSettings:{maxNumTokens:t.getMaxNumTokens(),maxNumImages:t.getMaxNumImages(),samplerBackend:t.getSamplerBackend().value,backendConfig:s,advancedSettings:t.getAdvancedSettings()},benchmarkEnabled:!1}}class z{callbacks=new Set;add(e){return this.callbacks.add(e),()=>{this.callbacks.delete(e),e()}}run(){for(const e of this.callbacks)e();this.callbacks.clear()}}const F="Conversation is busy. A generation is already in progress.";class ${conversation;engine;mutexes;visualTokenBudget;isBusy=!1;constructor(e,t,s,n=0){this.conversation=e,this.engine=t,this.mutexes=s,this.visualTokenBudget=n}async sendMessage(e){if(this.isBusy)throw new Error(F);this.isBusy=!0;try{const{json:t,blobs:s}=await j(e);return await this.mutexes.executor.acquireAndRun(async()=>{const e=await this.conversation.sendMessage(t,s,this.visualTokenBudget);return JSON.parse(e)})}finally{this.isBusy=!1}}sendMessageStreaming(e){if(this.isBusy)throw new Error(F);this.isBusy=!0;let t=!1,s=!1;return new ReadableStream({start:n=>{(async()=>{const{json:i,blobs:a}=await j(e);await this.mutexes.executor.acquireAndRun(async()=>{await this.conversation.sendMessageAsync(i,a,(e,i,a)=>{if(!t&&!s){if(a)return s=!0,this.isBusy=!1,void n.error(new Error(a));if(e)try{const t=JSON.parse(e);n.enqueue(t)}catch(e){s=!0,this.isBusy=!1,n.error(e)}}},this.visualTokenBudget),await this.engine.waitUntilDone()}),t||s||(this.isBusy=!1,n.close())})().catch(e=>{t||s||(this.isBusy=!1,n.error(e))})},cancel:()=>{t=!0,this.isBusy=!1,this.cancel()}})}cancel(){this.conversation.cancelProcess()}async getHistory(){return this.mutexes.executor.acquireAndRun(()=>{const e=this.conversation.getHistory();return JSON.parse(e)})}async getTokenCount(){return this.mutexes.executor.acquireAndRun(()=>this.conversation.getTokenCount())}async getBenchmarkInfo(){return this.mutexes.executor.acquireAndRun(()=>this.conversation.getBenchmarkInfo())}async clone(){if(this.isBusy)throw new Error(F);this.isBusy=!0;try{return await this.mutexes.executor.acquireAndRun(()=>{const e=this.conversation.clone();return new $(e,this.engine,this.mutexes,this.visualTokenBudget)})}finally{this.isBusy=!1}}async delete(){await this.mutexes.executor.acquireAndRun(()=>{this.conversation.delete()})}}async function j(e){const t=[],s=await async function(e,t){const s=function(e){if("string"==typeof e)return{role:"user",content:e};return e}(e);if(!Array.isArray(s.content))return s;const n=[];for(const e of s.content){if("image"!==e.type&&"audio"!==e.type||!("data"in e)||void 0===e.data){n.push(e);continue}const{data:s,...i}=e;if("string"==typeof s){n.push({...i,blob:s});continue}const a=await H(s),r=t.length;t.push(a),n.push({...i,blob_index:r})}return{...s,content:n}}(e,t);return{json:JSON.stringify(s),blobs:t}}async function H(e){return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof ArrayBuffer?new Uint8Array(e):e}class J{mutexPromise=Promise.resolve();queueCount=0;get isLocked(){return this.queueCount>0}async acquireAndRun(e){this.queueCount++;const t=this.mutexPromise;let s;this.mutexPromise=new Promise(e=>{s=e});try{return await t,await e()}finally{this.queueCount--,s()}}}function K(e,t=o().liteRtLmWasm){const s=t.SessionConfig.createDefault();if(void 0!==e.audioModalityEnabled&&s.setAudioModalityEnabled(e.audioModalityEnabled),void 0!==e.visionModalityEnabled&&s.setVisionModalityEnabled(e.visionModalityEnabled),void 0!==e.samplerParams){const t=s.getMutableSamplerParams();void 0!==e.samplerParams.type&&t.setType({value:e.samplerParams.type}),void 0!==e.samplerParams.k&&t.setK(e.samplerParams.k),void 0!==e.samplerParams.p&&t.setP(e.samplerParams.p),void 0!==e.samplerParams.temperature&&t.setTemperature(e.samplerParams.temperature),void 0!==e.samplerParams.seed&&t.setSeed(e.samplerParams.seed)}if(void 0!==e.startTokenId&&s.setStartTokenId(e.startTokenId),void 0!==e.numOutputCandidates&&s.setNumOutputCandidates(e.numOutputCandidates),void 0!==e.samplerBackend&&s.setSamplerBackend({value:e.samplerBackend}),void 0!==e.applyPromptTemplateInSession&&s.setApplyPromptTemplateInSession(e.applyPromptTemplateInSession),void 0!==e.useExternalSampler&&s.setUseExternalSampler(e.useExternalSampler),void 0!==e.maxOutputTokens&&s.setMaxOutputTokens(e.maxOutputTokens),void 0!==e.stopTokenIds){const n=new t.VectorVectorInt;for(const s of e.stopTokenIds){const e=new t.VectorInt;V(s,e),n.push_back(e),e.delete()}s.setStopTokenIds(n),n.delete()}return s}function Y(e){const t=e.getMutableSamplerParams();return{audioModalityEnabled:e.getAudioModalityEnabled(),visionModalityEnabled:e.getVisionModalityEnabled(),samplerParams:{type:t.type().value,k:t.k(),p:t.p(),temperature:t.temperature(),seed:t.seed()},stopTokenIds:O(e.getStopTokenIds()).map(O),startTokenId:e.getStartTokenId(),numOutputCandidates:e.getNumOutputCandidates(),samplerBackend:e.getSamplerBackend().value,applyPromptTemplateInSession:e.getApplyPromptTemplateInSession(),useExternalSampler:e.getUseExternalSampler(),maxOutputTokens:e.getMaxOutputTokens()}}const X="Session is busy. An operation is already in progress.";class Q{session;mutexes;config;isBusy=!1;constructor(e,t){this.session=e,this.mutexes=t;const s=e.getSessionConfig();this.config=Y(s),s.delete()}async runPrefill(e){if(this.isBusy)throw new Error(X);this.isBusy=!0;try{await this.mutexes.executor.acquireAndRun(async()=>{await this.session.runPrefill(e)})}finally{this.isBusy=!1}}async runDecode(){if(this.isBusy)throw new Error(X);this.isBusy=!0;try{return await this.mutexes.executor.acquireAndRun(async()=>{const e=await this.session.runDecode();return new Z(e)})}finally{this.isBusy=!1}}cancel(){this.session.cancelProcess()}async clone(){if(this.isBusy)throw new Error(X);this.isBusy=!0;try{return await this.mutexes.executor.acquireAndRun(()=>{const e=this.session.clone();return new Q(e,this.mutexes)})}finally{this.isBusy=!1}}async delete(){await this.mutexes.executor.acquireAndRun(()=>{this.session.delete()})}}class Z{responses;constructor(e){this.responses=e}getTexts(){return O(this.responses.getTexts())}delete(){this.responses.delete()}}async function ee(e,t,s){const n=await async function(e){if(e instanceof ReadableStream)return e;if(e instanceof Blob)return e.stream();const t=e,s=await fetch(t,{credentials:"same-origin"});if(!s.ok)throw new Error(`Failed to fetch model file from ${t}`);return s.body}(e),i=new I(n,()=>t.HEAPU8),a=t.ReadableStreamDataStream.create(i);s.add(()=>{a.delete()});const r=t.ModelAssets.createStreaming(a),o=s.add(()=>{r.delete()});return{modelAssets:r,cleanupModelAssets:o}}class te{wasm;engine;deleteCallback;settings;mutexes={executor:new J};constructor(e,t,s,n){this.wasm=e,this.engine=t,this.deleteCallback=n;const i=t.getEngineSettings(),a=q(i);this.settings={...a,model:s},i.delete()}static async create(e,t=""){const s=await B(),n=s.liteRtLmWasm,i=e.backend??U.GPU;e={...e,backend:i};const a=e.mainExecutorSettings?.samplerBackend,r=e.visionBackend,o=e.audioBackend;i!==U.GPU&&i!==U.GPU_ARTISAN&&a!==U.GPU&&a!==U.GPU_ARTISAN&&r!==U.GPU&&o!==U.GPU||await s.setupDefaultWebGpuDevice();const c=new z;let l;try{const{modelAssets:s,cleanupModelAssets:a}=await ee(e.model,n,c),u=void 0===r&&void 0===o?await n.EngineSettings.createDefault(s,{value:i}):await n.EngineSettings.createDefaultMultimodal(s,{value:i},void 0===r?void 0:{value:r},void 0===o?void 0:{value:o});a();const d=c.add(()=>{u.delete()}),h=u.getMutableMainExecutorSettings().getBackend().value;W(u,e,h,n),u.setParallelFileSectionLoading(!1),u.setSingleThreadedExecution(!0);try{l=h===U.GPU_ARTISAN?await n.Engine.createLegacyEngine(u,t):await n.Engine.createEngine(u,t)}finally{if(h===U.GPU||r===U.GPU||o===U.GPU)try{await n.clearStoredWeightsStreams()}catch(e){console.error("Error clearing stored weights streams:",e)}}return d(),c.add(()=>{l.delete()}),new te(n,l,e.model,()=>c.run())}catch(e){throw c.run(),e}}withDefaultModalities(e){const t={...e};return void 0!==this.settings.visionBackend&&void 0===t.visionModalityEnabled&&(t.visionModalityEnabled=!0),void 0!==this.settings.audioBackend&&void 0===t.audioModalityEnabled&&(t.audioModalityEnabled=!0),t}async createSession(e={}){return this.mutexes.executor.acquireAndRun(()=>{const t=K(this.withDefaultModalities(e),this.wasm),s=this.engine.createSession(t);return t.delete(),new Q(s,this.mutexes)})}async createConversation(e){return e||void 0===this.settings.visionBackend&&void 0===this.settings.audioBackend||(e={filterChannelContentFromKvCache:!0}),this.mutexes.executor.acquireAndRun(async()=>{let t,s;if(e){s=K(this.withDefaultModalities(e.sessionConfig||{}),this.wasm);const n=e.preface?JSON.stringify(e.preface):"",i=this.settings.backend===U.GPU_ARTISAN&&Boolean(this.settings.mainExecutorSettings?.backendConfig?.use_autosized_ringbuffers),a=e.filterChannelContentFromKvCache??i;t=this.wasm.ConversationConfig.createCustom(this.engine,s,!!e.enableConstrainedDecoding,!!e.prefillPrefaceOnInit,a,n)}else t=this.wasm.ConversationConfig.createDefault(this.engine);const n=await this.wasm.Conversation.create(this.engine,t);return t.delete(),s&&s.delete(),new $(n,this.engine,this.mutexes,this.settings.maxVisionTokensPerImage??0)})}async delete(){await this.mutexes.executor.acquireAndRun(()=>{this.deleteCallback()})}}const se=new Set(["image/jpeg","image/png","image/bmp","image/gif","image/x-tga","image/vnd.adobe.photoshop","image/vnd.radiance","image/x-portable-pixmap","image/x-portable-graymap"]),ne=new Set(["audio/wav","audio/x-wav","audio/wave","audio/mp3","audio/mpeg","audio/flac","audio/x-flac"]);function ie(e,t){const s=e.getMutableMainExecutorSettings();s.setCacheDir(":nocache");const n=t.maxInputLength??1024;if(e.setMaxInputLength(n),e.setMinInputLength(t.minInputLength??n),void 0!==t.visionTokensPerImage&&e.setVisionTokensPerImage(t.visionTokensPerImage),t.mainExecutorSettings){const e=t.mainExecutorSettings;void 0!==e.numThreads&&s.setNumThreads(e.numThreads)}}function ae(e){return{normalize:e?.normalize??!0,insertSpecialTokens:e?.insertSpecialTokens??!0,inputOverflowStrategy:e?.inputOverflowStrategy??N.ERROR,visionTokensPerImage:e?.visionTokensPerImage,outputSize:e?.outputSize}}async function re(e){if("string"==typeof e)return{type:"text",text:e};if(e instanceof Blob){const t=e.type.toLowerCase().split(";")[0].trim();if(se.has(t)){return{type:"image",data:await e.arrayBuffer()}}if(ne.has(t)){return{type:"audio",data:await e.arrayBuffer()}}throw new Error(`Unsupported Blob mime type: "${e.type}". Supported image types: ${[...se].join(", ")}. Supported audio types: ${[...ne].join(", ")}.`)}if("object"==typeof e&&null!==e){if("image"===e.type){if(e.data instanceof Blob){return{type:"image",data:await e.data.arrayBuffer()}}return e}if("audio"===e.type){if(e.data instanceof Blob){return{type:"audio",data:await e.data.arrayBuffer()}}return e}return e}throw new Error(`Unsupported input part: ${JSON.stringify(e)}`)}async function oe(e){return Array.isArray(e)?Promise.all(e.map(e=>re(e))):re(e)}class ce{engine;deleteCallback;settings;mutex=new J;constructor(e,t,s){this.engine=e,this.deleteCallback=s,this.settings=t}static async create(e){const t=await B(),s=t.liteRtLmWasm,n=e.backend??U.GPU;e={...e,backend:n},n!==U.GPU&&n!==U.GPU_ARTISAN&&e.visionBackend!==U.GPU&&e.audioBackend!==U.GPU||await t.setupDefaultWebGpuDevice();const i=new z;let a;try{const{modelAssets:t,cleanupModelAssets:r}=await ee(e.model,s,i),o=s.EmbeddingEngineSettings.createDefaultMultimodal(t,{value:n},void 0!==e.visionBackend?{value:e.visionBackend}:void 0,void 0!==e.audioBackend?{value:e.audioBackend}:void 0);r();const c=i.add(()=>{o.delete()}),l=o.getMutableMainExecutorSettings().getBackend().value;ie(o,e);try{a=await s.EmbeddingEngine.createEngine(o)}finally{if(l===U.GPU||e.visionBackend===U.GPU||e.audioBackend===U.GPU)try{await s.clearStoredWeightsStreams()}catch(e){console.error("Error clearing stored weights streams:",e)}}return c(),i.add(()=>{a.delete()}),new ce(a,e,()=>i.run())}catch(e){throw i.run(),e}}async computeEmbedding(e,t={}){const s=await oe(e);return this.mutex.acquireAndRun(async()=>{const e=await this.engine.computeEmbedding(s,ae(t));return{embedding:O(e.embedding),inputLength:e.inputLength,truncatedLength:e.truncatedLength,numChunks:e.numChunks}})}async computeEmbeddingBatch(e,t={}){const s=await Promise.all(e.map(oe));return this.mutex.acquireAndRun(async()=>{const e=await this.engine.computeEmbeddingBatch(s,ae(t));try{const t=[],s=e.size();for(let n=0;n<s;n++){const s=e.get(n),i=O(s.embedding);t.push({embedding:i,inputLength:s.inputLength,truncatedLength:s.truncatedLength,numChunks:s.numChunks})}return t}finally{e.delete()}})}async delete(){await this.mutex.acquireAndRun(()=>{this.deleteCallback()})}}}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/*! MIT License. DegreeSign Chat SDK */
|