@degreesign/chat 0.1.3 → 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 +120 -60
- 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/chat.d.ts +5 -2
- 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/index.d.ts +2 -2
- 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 +56 -7
- package/package.json +7 -3
|
@@ -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:()=>D}),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"),f=()=>{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},p=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})(p,p),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,p,p),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=["<|channel>thought\n","<|channel>thought","..."],v=["<channel|>","..."],k=(e,t)=>t.length?[...e?[{type:"text",text:e}]:[],...t.map(e=>({type:"image",url:e}))]:e,b=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[...b(e),{type:"text",text:"\n"},...b(t)]){const e=n[n.length-1];"text"==a.type&&"text"==e?.type?e.text+=a.text:n.push(a)}return n},M=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,x);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,x);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: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:k(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 M({model:e,logging:t.logging,onReason:t.onReason,onText:t.onText,start:()=>n(r),stopped:a})};let _,L,j="",R=!1;const W=e=>`${e.wasm}|${e.compatWasm}|${e.compatWorker}`,q=e=>{const t=f();return{wasm:e?.wasm||t.wasm,compatWasm:e?.compatWasm||t.compatWasm,compatWorker:e?.compatWorker||t.compatWorker}},$=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)}},B=async()=>{if(_)return _;const e=await n.e(924).then(()=>n(577)),t=L||q(),a=new e.Wllama({default:t.wasm},{logger:$(e.LoggerWithoutDebug)});return L||(L=t,j=W(t)),a.setCompat({wasm:t.compatWasm,worker:t.compatWorker}),_=a,a},I=async()=>{const e=_;_=void 0;try{await(e?.exit())}catch{}},O=(e,t)=>e.name.includes(t)||!!e.metadata?.originalURL?.includes(t),P=e=>[e.cpu.file,...e.cpu.mmproj?[e.cpu.mmproj.file]:[]],A=(e,t)=>`https://huggingface.co/${e}/resolve/main/${t}`,U=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 U(t.url)});t.push({role:"user",content:e})}return t},S=async(e,t,n)=>{if(R=!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 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}})},E={backend:i.cpu,startLLM:async({model:e,limits:t,onProgress:n,signal:a,logging:o,assets:r})=>{R=!0===o;const s=q(r),l=W(s);_&&l!=j&&await I(),L=s,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 B(),f=A(e.cpu.repo,e.cpu.file),p=e.cpu.mmproj?.file,h=p?A(e.cpu.repo,p):"";if(a?.aborted)throw await I(),new Error("Cancelled");if(p){const t=await m.cacheManager.list(),n=t.some(t=>O(t,e.cpu.file)),a=t.some(e=>O(e,p));n&&!a&&await m.cacheManager.deleteMany(t=>O(t,e.cpu.file))}try{await m.loadModelFromUrl(h?{url:f,mmprojUrl:h}:f,{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 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:i.cpu,contextSize:8192,chat:t=>S(m,e,t),unload:async()=>{if(_==m)await I();else try{await m.exit()}catch{}}}},cached:async e=>{try{const t=await B(),n=await t.cacheManager.list();return P(e).every(e=>n.some(t=>O(t,e)))}catch{return!1}},hasCache:async()=>{try{const e=await B();return(await e.cacheManager.list()).length>0}catch{return!1}},remove:async e=>{try{const t=await B();await t.cacheManager.deleteMany(t=>P(e).some(e=>O(t,e)))}catch{}},purge:async()=>{try{const e=await B();await e.cacheManager.clear()}catch{}}},Q=((e={})=>{const t=e.engines?.cpu||E;let n,a,o,r,l,g,u,m=i.none,f=s.idle,p=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=>{f=e},k=()=>f==s.ready&&!!n&&!!a;return{load:async({model:e,limits:u,assets:f,logging:b,onProgress:T})=>{if(k()&&a?.id==e.id)return!0;if(o&&r==e.id)return g=T,T(h.percent,h.phase),await o;o&&(g=void 0,l?.abort(),await o.catch(()=>!1)),g=T;const M=++p,C=(async()=>{await(async()=>{try{if("undefined"==typeof navigator)return;await(navigator.storage?.persist?.())}catch{}})();const o=y();if(M!=p)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),M!=p)return!1;const r=await t.cached(e);if(M!=p)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(),M!=p)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:f,onProgress:(e,t)=>{M==p&&x(e,t)}});return M!=p||g.signal.aborted?(await r.unload(),!1):(n=r,a=e,m=o,v(s.ready),x(1,c.load),!0)}catch(e){return M!=p||(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()=>{p++,l?.abort(),l=void 0,r=void 0;const e=n;n=void 0,a=void 0,v(s.idle),await(e?.unload())},cancelLoad:()=>{p++,l?.abort(),l=void 0,r=void 0,f==s.downloading&&v(s.idle)},ready:k,loading:()=>!k()&&f==s.downloading,status:()=>f,backend:()=>m,model:()=>a}})(),D=(e=>{let t,n=0;const a=t=>{const n=e.status(),a=e.backend(),o=g(t.input.model),i={availability:n==s.ready?r.online:n==s.error||n==s.unsupported?r.offline:r.away,status:n,backend:a,supportsImages:!!o&&u(o),ready:e.ready(),loading:e.loading(),requesting:t.requesting,phase:t.phase,percent:t.percent,...t.error?{error:t.error}:{}};t.input.onStatus?.(i)},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 f=((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)}})(r.limits),p=await e.load({model:i,limits:f,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(!p)return t.cancelled||t.error||(t.error="Model not ready"),void a(t);const h=u(i),x=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 v,k="",b="";try{v=await e.chat({messages:x,limits:f,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||(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 a(t)}t.token==n&&(t.requesting=!1,v||t.cancelled||(t.error="No reply"),a(t))};return o=>{t?.controller.abort();const r={token:++n,input:o,controller:new AbortController,percent:e.ready()?100:0,phase:e.ready()?c.load:c.download,requesting:!1,cancelled:!1};return t=r,a(r),i(r),()=>(t=>{t.token==n&&(t.cancelled=!0,t.controller.abort(),e.status()!=s.ready?(e.cancelLoad(),a(t)):e.unload().then(()=>a(t)))})(r)}})(Q);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,f=e=>!!e.cpu.mmproj,p=()=>{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=["<|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,T=(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},M=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: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:b(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=T(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 M({model:e,logging:t.logging,onReason:t.onReason,onText:t.onText,start:()=>n(o),stopped:a})};let L,j,R="",W=!1;const $=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}},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 r.e(924).then(()=>r(577)),t=j||q(),n=new e.Wllama({default:t.wasm},{logger:B(e.LoggerWithoutDebug)});return j||(j=t,R=$(t)),n.setCompat({wasm:t.compatWasm,worker:t.compatWorker}),L=n,n},I=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}`,E=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 E(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 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}})},S={backend:l.cpu,startLLM:async({model:e,limits:t,onProgress:n,signal:a,logging:o,assets:r})=>{W=!0===o;const s=q(r),i=$(s);L&&i!=R&&await I(),j=s,R=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 P(),f=U(e.cpu.repo,e.cpu.file),p=e.cpu.mmproj?.file,h=p?U(e.cpu.repo,p):"";if(a?.aborted)throw await I(),new Error("Cancelled");if(p){const t=await m.cacheManager.list(),n=t.some(t=>O(t,e.cpu.file)),a=t.some(e=>O(e,p));n&&!a&&await m.cacheManager.deleteMany(t=>O(t,e.cpu.file))}try{await m.loadModelFromUrl(h?{url:f,mmprojUrl:h}:f,{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 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:l.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 A(e).every(e=>n.some(t=>O(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=>A(e).some(e=>O(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,r,s,i,g,m=l.none,f=c.idle,p=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=>{f=e},v=()=>f==c.ready&&!!n&&!!a;return{load:async({model:e,limits:g,assets:f,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=++p,M=(async()=>{await(async()=>{try{if("undefined"==typeof navigator)return;await(navigator.storage?.persist?.())}catch{}})();const o=w();if(T!=p)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!=p)return!1;const r=await t.cached(e);if(T!=p)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!=p)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:f,onProgress:(e,t)=>{T==p&&x(e,t)}});return T!=p||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!=p||(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()=>{p++,s?.abort(),s=void 0,r=void 0;const e=n;n=void 0,a=void 0,k(c.idle),await(e?.unload())},cancelLoad:()=>{p++,s?.abort(),s=void 0,r=void 0,f==c.downloading&&k(c.idle)},ready:v,loading:()=>!v()&&f==c.downloading,status:()=>f,backend:()=>m,model:()=>a}})(),V=(e=>{let t,n=0;const a=t=>{const n=e.status(),a=e.backend(),o=m(t.input.model),r={availability:n==c.ready?i.online:n==c.error||n==c.unsupported?i.offline:i.away,status:n,backend:a,supportsImages:!!o&&f(o),ready:e.ready(),loading:e.loading(),requesting:t.requesting,phase:t.phase,percent:t.percent,...t.error?{error:t.error}:{}};t.input.onStatus?.(r)},o=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&&!f(i))return t.error="Model does not accept images",void a(t);const u=((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)}})(o.limits),p=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(!p)return t.cancelled||t.error||(t.error="Model not ready"),void a(t);const h=f(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 k,v="",b="";try{k=await e.chat({messages:w,limits:u,reasoning:!0===o.reasoning,logging:r,onReason:e=>{t.token!=n||t.cancelled||(v+=e,o.onThinking?.(v))},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,k||t.cancelled||(t.error="No reply"),a(t))};return r=>{t?.controller.abort();const s={token:++n,input:r,controller:new AbortController,percent:e.ready()?100:0,phase:e.ready()?d.load:d.download,requesting:!1,cancelled:!1};return t=s,a(s),o(s),()=>(t=>{t.token==n&&(t.cancelled=!0,t.controller.abort(),e.status()!=c.ready?(e.cancelLoad(),a(t)):e.unload().then(()=>a(t)))})(s)}})(D);export{g as ChatModelId,i as ChatStatus,l as LlmBackend,d as LlmPhase,s as LlmRole,c as LlmStatus,V as createChat};
|
|
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 */
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! For license information please see engine-litert.node.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/types.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ interface ChatModel {
|
|
|
13
13
|
/** Whether the model supports chain-of-thought reasoning */
|
|
14
14
|
reasoning: boolean;
|
|
15
15
|
/** wllama (CPU) artifact */
|
|
16
|
-
cpu
|
|
16
|
+
cpu?: {
|
|
17
17
|
repo: string;
|
|
18
18
|
file: string;
|
|
19
19
|
/** Vision projector, present when the CPU build accepts images */
|
|
@@ -21,6 +21,13 @@ interface ChatModel {
|
|
|
21
21
|
file: string;
|
|
22
22
|
};
|
|
23
23
|
};
|
|
24
|
+
/** LiteRT-LM (WebGPU) artifact */
|
|
25
|
+
litert?: {
|
|
26
|
+
repo: string;
|
|
27
|
+
file: string;
|
|
28
|
+
/** Whether the build accepts images */
|
|
29
|
+
vision: boolean;
|
|
30
|
+
};
|
|
24
31
|
}
|
|
25
32
|
/** Token, prompt and thread budgets of an on-device generation */
|
|
26
33
|
interface LlmLimits {
|
|
@@ -76,6 +83,8 @@ interface ChatInput {
|
|
|
76
83
|
/** Model and request state, reported on every change */
|
|
77
84
|
onStatus?: (snapshot: ChatStatusSnapshot) => void;
|
|
78
85
|
}
|
|
86
|
+
/** Model preload request, a subset of ChatInput */
|
|
87
|
+
type ChatPreloadInput = Pick<ChatInput, `model` | `limits` | `logging` | `onStatus`>;
|
|
79
88
|
/** Stop the current request, its model load, or free the loaded model */
|
|
80
89
|
type CancelChat = () => void;
|
|
81
90
|
/** wllama wasm assets used by the CPU engine */
|
|
@@ -87,21 +96,39 @@ interface WllamaAssets {
|
|
|
87
96
|
/** URL of the wllama compat worker script */
|
|
88
97
|
compatWorker: string;
|
|
89
98
|
}
|
|
99
|
+
/** LiteRT-LM wasm assets used by the GPU engine */
|
|
100
|
+
interface LitertAssets {
|
|
101
|
+
/** URL of the LiteRT-LM wasm glue script */
|
|
102
|
+
script: string;
|
|
103
|
+
/** URL of the LiteRT-LM wasm binary */
|
|
104
|
+
wasm: string;
|
|
105
|
+
}
|
|
106
|
+
/** Navigator with the WebGPU entry point, absent in Node */
|
|
107
|
+
interface GpuNavigator extends Navigator {
|
|
108
|
+
gpu?: object;
|
|
109
|
+
}
|
|
110
|
+
/** Request state that builds one status snapshot */
|
|
111
|
+
interface ChatState {
|
|
112
|
+
/** Whether a reply is streaming right now */
|
|
113
|
+
requesting: boolean;
|
|
114
|
+
phase: LlmPhase;
|
|
115
|
+
/** Download or load progress, 0..100 */
|
|
116
|
+
percent: number;
|
|
117
|
+
/** Last request error, cleared when a reply starts */
|
|
118
|
+
error?: string;
|
|
119
|
+
}
|
|
90
120
|
/** One live chat request */
|
|
91
|
-
interface ActiveCall {
|
|
121
|
+
interface ActiveCall extends ChatState {
|
|
92
122
|
token: number;
|
|
93
123
|
input: ChatInput;
|
|
94
124
|
controller: AbortController;
|
|
95
|
-
percent: number;
|
|
96
|
-
phase: LlmPhase;
|
|
97
|
-
requesting: boolean;
|
|
98
125
|
cancelled: boolean;
|
|
99
|
-
error?: string;
|
|
100
126
|
}
|
|
101
127
|
/** Engine and backend overrides used by tests */
|
|
102
128
|
interface RuntimeDeps {
|
|
103
129
|
engines?: {
|
|
104
130
|
cpu?: LlmEngine;
|
|
131
|
+
litert?: LlmEngine;
|
|
105
132
|
};
|
|
106
133
|
backend?: LlmBackend;
|
|
107
134
|
}
|
|
@@ -231,6 +258,28 @@ type EngineMessage = {
|
|
|
231
258
|
role: `assistant`;
|
|
232
259
|
content: string;
|
|
233
260
|
};
|
|
261
|
+
/** One LiteRT-LM content segment */
|
|
262
|
+
type LitertContentPart = import('@litert-lm/core').ContentPart;
|
|
263
|
+
/** One LiteRT-LM chat turn */
|
|
264
|
+
type LitertMessage = import('@litert-lm/core').Message;
|
|
265
|
+
/** One LiteRT-LM engine */
|
|
266
|
+
type LitertEngine = import('@litert-lm/core').Engine;
|
|
267
|
+
/** The LiteRT-LM module namespace */
|
|
268
|
+
type LitertModule = typeof import('@litert-lm/core');
|
|
269
|
+
/** Params of one LiteRT-LM chat call */
|
|
270
|
+
interface LitertChatParams {
|
|
271
|
+
engine: LitertEngine;
|
|
272
|
+
model: ChatModel;
|
|
273
|
+
mod: LitertModule;
|
|
274
|
+
params: EngineChatParams;
|
|
275
|
+
}
|
|
276
|
+
/** Params of one LiteRT-LM stream */
|
|
277
|
+
interface StreamLitertParams {
|
|
278
|
+
engine: LitertEngine;
|
|
279
|
+
mod: LitertModule;
|
|
280
|
+
params: EngineChatParams;
|
|
281
|
+
messages: EngineMessage[];
|
|
282
|
+
}
|
|
234
283
|
/** Streamed delta, reasoning may be inline or structured */
|
|
235
284
|
interface EngineDelta {
|
|
236
285
|
content?: string;
|
|
@@ -267,4 +316,4 @@ interface EngineRunParams {
|
|
|
267
316
|
/** Whether the caller stopped the generation */
|
|
268
317
|
stopped: () => boolean;
|
|
269
318
|
}
|
|
270
|
-
export type { ChatMessage, ChatModel, LlmLimits, ChatStatusSnapshot, ChatInput, CancelChat, WllamaAssets, ActiveCall, RuntimeDeps, RuntimeLoadParams, RuntimeChatParams, Runtime, EngineChatParams, StartLlmParams, LlmRuntime, LlmEngine, EngineMessage, EngineContentPart, WllamaContentPart, WllamaMessage, EngineDelta, EngineChunk, StreamReplyParams, EngineRunParams, };
|
|
319
|
+
export type { ChatMessage, ChatModel, LlmLimits, ChatStatusSnapshot, ChatInput, ChatPreloadInput, ChatState, CancelChat, WllamaAssets, LitertAssets, GpuNavigator, ActiveCall, RuntimeDeps, RuntimeLoadParams, RuntimeChatParams, Runtime, EngineChatParams, StartLlmParams, LlmRuntime, LlmEngine, EngineMessage, EngineContentPart, WllamaContentPart, WllamaMessage, LitertContentPart, LitertMessage, LitertEngine, LitertModule, LitertChatParams, StreamLitertParams, EngineDelta, EngineChunk, StreamReplyParams, EngineRunParams, };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@degreesign/chat",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Headless on-device AI chat for browsers
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Headless on-device AI chat for browsers and Node with wllama CPU and LiteRT WebGPU engines",
|
|
5
5
|
"main": "dist/node/chat.node.min.js",
|
|
6
6
|
"module": "dist/browser/chat.min.mjs",
|
|
7
7
|
"browser": "dist/browser/chat.min.mjs",
|
|
@@ -36,6 +36,9 @@
|
|
|
36
36
|
"on-device",
|
|
37
37
|
"local inference",
|
|
38
38
|
"wllama",
|
|
39
|
+
"litert",
|
|
40
|
+
"webgpu",
|
|
41
|
+
"gemma",
|
|
39
42
|
"typescript",
|
|
40
43
|
"browser",
|
|
41
44
|
"node",
|
|
@@ -58,6 +61,7 @@
|
|
|
58
61
|
"dist/"
|
|
59
62
|
],
|
|
60
63
|
"dependencies": {
|
|
64
|
+
"@litert-lm/core": "0.19.0-nightly.20261024",
|
|
61
65
|
"@wllama/wllama": "^3.8.1",
|
|
62
66
|
"@wllama/wllama-compat": "^3.8.1"
|
|
63
67
|
},
|
|
@@ -70,4 +74,4 @@
|
|
|
70
74
|
"webpack": "^5.97.1",
|
|
71
75
|
"webpack-cli": "^6.0.1"
|
|
72
76
|
}
|
|
73
|
-
}
|
|
77
|
+
}
|