@caldrix-ai/caldrix 0.7.0 → 0.8.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/CHANGELOG.md +6 -0
- package/README.md +1 -1
- package/dist/index.js +1 -1
- package/host/caldrix_web_bg.wasm +0 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
Format after [Keep a Changelog](https://keepachangelog.com/); versions are independent semver, tagged
|
|
4
4
|
`caldrix-js-v<version>`.
|
|
5
5
|
|
|
6
|
+
## 0.8.0 — 2026-11-06
|
|
7
|
+
|
|
8
|
+
### Changed
|
|
9
|
+
|
|
10
|
+
- `Caldrix.load` and `Caldrix.check` of a repo whose `config.json` declares `torch_dtype` (or `dtype`) `float16` or `fp16` no longer refuse it: the f16 weights are widened exactly to an f32 plan, or narrowed once to a bf16 plan by round-to-nearest-even. Before, the load was a `validate` refusal naming the dtype (`… is not supported: no lane reads f16 weights`). `dtype: "f16"` is still a `request` refusal, `f16 is a source dtype only; plans run at f32 or bf16`. Pinned by the host crate's native tests (placement, session and precheck over a `float16` fixture); not run in a browser: no `float16` repo has loaded in one (`inferno/WEB-VERIFICATION.md` records nothing for it).
|
|
11
|
+
|
|
6
12
|
## 0.7.0 — 2026-11-05
|
|
7
13
|
|
|
8
14
|
### Added
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ This page states only what `inferno/WEB-VERIFICATION.md` records (in
|
|
|
8
8
|
the Caldrix repository, which is private, so you cannot open it), what the package's own tests run in
|
|
9
9
|
CI, and what the source does.
|
|
10
10
|
|
|
11
|
-
<!-- host-commit:
|
|
11
|
+
<!-- host-commit: f39f5b3e7d3c3cf4349d02a1fd4e1042e30efebc -->
|
|
12
12
|
|
|
13
13
|
The compiled host in `host/` is built from the `caldrix-web` crate at the commit in the marker above.
|
|
14
14
|
The release workflow overwrites that marker with the commit it builds; the value in a source checkout
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as N,b as i,d as J}from"./chunk-PZGTLL5H.js";function v(e){return typeof e=="object"&&e!==null}function te(e){return v(e)&&typeof e.token=="number"&&typeof e.text=="string"&&typeof e.done=="boolean"&&(e.finishReason===null||e.finishReason==="stop"||e.finishReason==="length"||e.finishReason==="abort")&&typeof e.promptTokens=="number"&&typeof e.completionTokens=="number"&&typeof e.cachedTokens=="number"&&(e.logprobs===void 0||Array.isArray(e.logprobs)&&e.logprobs.every(t=>v(t)&&typeof t.token=="number"&&typeof t.logprob=="number"))}function re(e){return!v(e)||typeof e.url!="string"?!1:e.failed===void 0||v(e.failed)&&typeof e.failed.url=="string"&&typeof e.failed.reason=="string"}function T(e){if(!v(e)||typeof e.id!="number")return!1;switch(e.type){case"ready":return e.id===0&&typeof e.url=="string";case"host":return re(e.hostWasm);case"progress":return typeof e.file=="string"&&typeof e.loaded=="number"&&(e.total===null||typeof e.total=="number")&&typeof e.repo=="string"&&typeof e.revision=="string"&&(e.provenance===void 0||typeof e.provenance=="string");case"loaded":return v(e.doctor)&&v(e.sources)&&(e.fallback===void 0||typeof e.fallback=="string")&&Object.values(e.sources).every(t=>v(t)&&(t.source==="cache"||t.source==="network")&&typeof t.verified=="boolean");case"checked":return e.backend==="webgpu"||e.backend==="wasm";case"fitted":return(e.backend==="webgpu"||e.backend==="wasm")&&(e.maxContext===null||typeof e.maxContext=="number")&&(e.contextClamp===null||typeof e.contextClamp=="string");case"delta":return te(e.delta);case"result":return"value"in e;case"error":return N(e.kind)&&typeof e.message=="string"&&(e.fatal===void 0||typeof e.fatal=="string");default:return!1}}function w(e){let t;try{t=JSON.stringify(e)??String(e)}catch{t=String(e)}return t.length>200&&(t=`${t.slice(0,200)}...`),new i("backend",`the worker sent a malformed message: ${t}`)}var C=(...e)=>new Uint8Array(e),ne={simd:C(0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,10,1,8,0,65,0,253,15,253,98,11),relaxedSimd:C(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),bulkMemory:C(0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,3,1,0,1,10,14,1,12,0,65,0,65,0,65,0,252,10,0,0,11),threads:C(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),memory64:C(0,97,115,109,1,0,0,0,5,3,1,4,1),tailCall:C(0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,6,1,4,0,18,0,11),exceptions:C(0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,16,1,14,0,2,105,31,64,1,3,0,0,11,0,11,26,11)};function H(){let e={};for(let[t,o]of Object.entries(ne))e[t]=WebAssembly.validate(o);return e}var oe=1e3,D=Symbol("timed out");async function K(e){let t,o=new Promise(n=>{t=setTimeout(()=>n(D),oe)});try{return await Promise.race([e,o])}finally{clearTimeout(t)}}var X=e=>e instanceof Error?e.message:String(e);function Q(){let e=globalThis.navigator;return typeof e=="object"&&e!==null?e:{}}function P(e){return typeof e=="number"&&Number.isFinite(e)?e:null}function se(){let e={available:!1,maxTextureSize:null,floatColorBuffer:null};try{let t=null;if(typeof OffscreenCanvas<"u"?t=new OffscreenCanvas(1,1):typeof document<"u"&&(t=document.createElement("canvas")),t===null)return e;let o=t,n=o.getContext("webgl2"),r=n??o.getContext("webgl");if(!r)return e;let s=P(r.getParameter(r.MAX_TEXTURE_SIZE)),l=r.getExtension(n?"EXT_color_buffer_float":"WEBGL_color_buffer_float")!==null;return r.getExtension("WEBGL_lose_context")?.loseContext(),{available:!0,maxTextureSize:s,floatColorBuffer:l}}catch{return e}}async function ae(e){let t=e.info??{};if(!e.info&&typeof e.requestAdapterInfo=="function")try{t=await e.requestAdapterInfo()}catch{t={}}let o={},n=[];if(e.limits)for(let r in e.limits){let s=e.limits[r];typeof s=="number"?(o[r]=s,Number.isSafeInteger(s)||n.push(`${r} = ${s} (not a safe integer)`)):typeof s!="function"&&n.push(`${r} = ${JSON.stringify(s)??String(s)} (not a number)`)}return{vendor:t.vendor??"",architecture:t.architecture??"",device:t.device??"",description:t.description??"",features:e.features?[...e.features]:[],limits:o,unrepresentable:n}}async function ie(){let e=Q().gpu;if(!e||typeof e.requestAdapter!="function")return{webgpu:null,available:!1,reason:"no navigator.gpu"};let t;try{let n=await K(e.requestAdapter());if(n===D)return{webgpu:null,available:!1,reason:"requestAdapter timed out"};t=n}catch(n){return{webgpu:null,available:!1,reason:`requestAdapter failed: ${X(n)}`}}if(!t)return{webgpu:null,available:!1,reason:"requestAdapter returned null"};let o=await ae(t);try{let n=await K(t.requestDevice());if(n===D)return{webgpu:o,available:!1,reason:"requestDevice timed out"};n.destroy?.()}catch(n){return{webgpu:o,available:!1,reason:`requestDevice refused: ${X(n)}`}}return{webgpu:o,available:!0}}function le(e){return e.webgpu.available&&e.webgpu.built?"webgpu":"wasm"}function ue(e,t,o){let n=e.webgpu?.unrepresentable??[],r=n.length>0?`; adapter limits ${n.join(", ")}: the host reads them as missing`:"";return de(e,t,o)+r}function de(e,t,o){if(o==="webgpu"){let r=e.webgpu,s=r.features.includes("shader-f16")?" with shader-f16":"";return`WebGPU adapter present (${r.vendor} ${r.architecture})${s}: webgpu`}let n=e.wasm.simd?"with simd128":"without simd128";return e.webgpu===null?`no WebGPU adapter (${t.reason??"unavailable"}): wasm ${n}`:`WebGPU adapter present but unusable (${t.reason}): wasm ${n}`}async function G(){let e=Q(),t=await ie(),o={wasm:H(),threads:{sharedArrayBuffer:typeof SharedArrayBuffer<"u",crossOriginIsolated:globalThis.crossOriginIsolated===!0,hardwareConcurrency:P(e.hardwareConcurrency)},webgl:se(),webgpu:t.webgpu,memory:{deviceMemoryGiB:P(e.deviceMemory)},tiers:{webgpu:t.available?{available:!0,built:!0}:{available:!1,built:!0,reason:t.reason},webgl:{available:!1,built:!1,reason:"not built"},wasm:{available:!0,built:!0}}};o.tiers.webgl.available=o.webgl.available;let n=le(o.tiers);return{...o,chosen:n,why:ue(o,t,n)}}function z(e){let t=e.webgpu===null?null:{vendor:e.webgpu.vendor,architecture:e.webgpu.architecture,device:e.webgpu.device,description:e.webgpu.description,features:e.webgpu.features,limits:Object.fromEntries(Object.entries(e.webgpu.limits).filter(([,o])=>Number.isSafeInteger(o)))};return JSON.stringify({wasm:{simd:e.wasm.simd,relaxedSimd:e.wasm.relaxedSimd,bulkMemory:e.wasm.bulkMemory,threads:e.wasm.threads,memory64:e.wasm.memory64,tailCall:e.wasm.tailCall,exceptions:e.wasm.exceptions},threads:{sharedArrayBuffer:e.threads.sharedArrayBuffer,crossOriginIsolated:e.threads.crossOriginIsolated,hardwareConcurrency:e.threads.hardwareConcurrency},webgpu:t,memory:{deviceMemoryGiB:e.memory.deviceMemoryGiB}})}var ce="https://huggingface.co";function fe(e){return typeof location<"u"?new URL(String(e),location.href):new URL(String(e))}var pe="https://cdn.jsdelivr.net/npm/@caldrix-ai/caldrix@0.7.0/";function S(e){return e instanceof Error?e.message:String(e)}function F(e,t){if(t!==void 0){if(typeof t!="string"&&!(t instanceof URL))throw new i("request",`assets.${e} is a string or a URL, not ${typeof t}`);try{return typeof location<"u"?new URL(t,location.href):new URL(t)}catch{throw new i("request",`assets.${e} ${JSON.stringify(String(t))} is not a URL`)}}}function be(e,t){let o=_(e,t),n=null,r=()=>{n!==null&&URL.revokeObjectURL(n),n=null};try{return!o&&!t?{worker:new Worker(new URL("./worker.js",import.meta.url),{type:"module"}),revoke:r,literal:!0}:o?(n=URL.createObjectURL(new Blob([`import ${JSON.stringify(e.href)};`],{type:"text/javascript"})),{worker:new Worker(n,{type:"module"}),revoke:r}):{worker:new Worker(e,{type:"module"}),revoke:r}}catch(s){throw r(),s}}function me(e){return typeof location<"u"&&e.origin!==location.origin}function _(e,t){return me(e)&&(t||e.protocol==="http:"||e.protocol==="https:")}function q(e){return!e.chosen&&!_(e.url,!1)?`the package's worker (dist/worker.js as the page's bundler emitted it, or ${e.url.href} unbundled)`:`the engine's worker from ${e.url.href}${_(e.url,e.chosen)?" (through a same-origin shim)":""}`}var R=class{constructor(t){this.reason=t}reason};function ge(e,t,o){return new Promise((n,r)=>{let s;try{s=be(e.url,e.chosen)}catch(u){r(new R(`the page could not construct it: ${S(u)}`));return}let{worker:l,revoke:c}=s,b=()=>d(o()),d=(u,a=e.url)=>{if(c(),l.onmessage=null,l.onerror=null,t?.removeEventListener("abort",b),u===null)return n({worker:l,url:a});l.terminate(),r(u)};t?.addEventListener("abort",b,{once:!0}),l.onerror=u=>d(new R(u.message||"it did not load")),l.onmessage=u=>{let a=u.data;if(!T(a)||a.id!==0)return d(w(a));if(a.type==="ready")return s.literal?URL.canParse(a.url)?d(null,new URL(a.url)):d(w(a)):d(null);if(a.type==="error")return d(new R(a.message));d(w(a))}})}async function he(e,t,o,n){let r=[];for(let d of e)try{let{worker:u,url:a}=await ge(d,o,n),f=r[0];return{worker:u,used:d,url:a,...f!==void 0?{failed:{what:q(f.t),reason:f.reason}}:{}}}catch(u){if(!(u instanceof R))throw u;r.push({t:d,reason:u.reason})}let[s,l]=r;if(s!==void 0&&l!==void 0){let d=l.t.url.origin;throw new i("backend",`${q(s.t)} did not start (${s.reason}), nor from the CDN fallback ${l.t.url.href} (${l.reason}); serve dist/ and host/ beside the bundle, set assets.worker to where the page serves them, or allow ${d} in the page's Content-Security-Policy (worker-src blob: for the same-origin shim that starts it, script-src ${d} for the modules it imports, connect-src ${d} for the .wasm)`)}let c=s,b=t&&c.t.source==="bundle"?"; the CDN fallback is off (assets.fallback is false)":"";throw new i("backend",`${q(c.t)} did not start: ${c.reason}${b}; caldrix.js runs inference in a Web Worker and has no main-thread mode`)}function we(e,t,o){let n=o!==void 0?`, and with it the host .wasm from ${o}`:"";return`${e.what} did not start (${e.reason}); it was loaded from the CDN fallback ${t}${n}`}function ke(e){return`the host .wasm could not be fetched from ${e.failed.url} (${e.failed.reason}); it was fetched from the CDN fallback ${e.url}`}function V(e){console.warn(`caldrix.js: ${e}. Serve dist/ and host/ beside the bundle or set assets.worker/assets.hostWasm; assets.fallback: false turns the fallback off.`)}function ye(e){let t={text:e.text,token:e.token,done:e.done};return e.logprobs!==void 0&&(t.logprobs=e.logprobs),e.done&&(e.finishReason!==null&&(t.finishReason=e.finishReason),t.usage={promptTokens:e.promptTokens,completionTokens:e.completionTokens,cachedTokens:e.cachedTokens}),t}var Y=4<<20;async function B(e,t){let o=t.backend??"auto";if(o==="webgl")throw new i("backend","the webgl backend is not built");if(o!=="auto"&&o!=="webgpu"&&o!=="wasm")throw new i("request",`backend ${JSON.stringify(o)} is not auto, webgpu, wasm or webgl`);let n=e.kind==="fit"?"":e.model;if(e.kind!=="fit"&&(typeof n!="string"||n===""))throw new i("request","load takes a model alias or a Hub repo id (owner/name[@revision]), a non-empty string");if(t.file!==void 0&&(typeof t.file!="string"||t.file===""))throw new i("request",`file is the name of a GGUF file in the repo, a non-empty string, not ${JSON.stringify(t.file)?.slice(0,80)}`);let r=F("worker",t.assets?.worker),s=F("hostWasm",t.assets?.hostWasm),l=t.assets?.fallback??!0;if(typeof l!="boolean")throw new i("request",`assets.fallback is true or false, not ${JSON.stringify(l)}`);let c=F("cdn",t.assets?.cdn)??new URL(pe);c.pathname.endsWith("/")||(c.pathname+="/");let b=[r!==void 0?{url:r,chosen:!0,source:"page"}:{url:fe(new URL("./worker.js",import.meta.url)),chosen:!1,source:"bundle"}];l&&r===void 0&&b.push({url:new URL("dist/worker.js",c),chosen:!0,source:"cdn"});let d=l&&s===void 0?new URL("host/caldrix_web_bg.wasm",c).href:void 0,u=t.storage??"auto";if(u!=="auto"&&u!=="f32")throw new i("request",`storage ${JSON.stringify(u)} is not auto or f32`);if(o==="wasm"&&u==="f32")throw new i("request",`storage "f32" is the WebGPU tier's weight storage; backend "wasm" takes none`);if(t.hubBaseUrl!==void 0){let g=t.hubBaseUrl,U=!1;if(typeof g=="string")try{let j=new URL(g);U=j.protocol==="http:"||j.protocol==="https:"}catch{U=!1}if(!U)throw new i("request",`hubBaseUrl must be an absolute http(s) URL, not ${JSON.stringify(g).slice(0,80)}`)}let a=()=>new i("fetch",e.kind==="fit"?"the fit was aborted":`${e.kind==="check"?"checking":"loading"} ${n} was aborted`),f=t.signal;if(f?.aborted)throw a();let h=await G(),m,y;if(o==="auto"&&t.dtype==="bf16"&&h.chosen==="webgpu")m="wasm",y=`bf16 requested: wasm (the WebGPU tier runs the f32 plan only); ${h.why}`;else if(o==="auto")m=h.chosen,y=h.why;else if(o==="webgpu"){if(!h.tiers.webgpu.available)throw new i("backend",`backend "webgpu" was asked for, but the probe found no usable WebGPU adapter: ${h.tiers.webgpu.reason??h.why}`);m="webgpu",y=`backend "webgpu" asked for; ${h.why}`}else m="wasm",y=`backend "wasm" asked for; ${h.why}`;let p=h.webgpu?.unrepresentable??[];if(f?.aborted)throw a();let k=await he(b,!l,f,a),{worker:W}=k,$=k.url,L=o==="auto"&&m==="webgpu"?{auto:!0}:{},O={...s!==void 0?{hostWasm:s.href}:{},...d!==void 0?{cdnWasm:d}:{}},I=z(h),ee=e.kind==="fit"?{id:1,type:"fit",facts:e.facts,backend:m,...L,...t.maxContext!==void 0?{maxContext:t.maxContext}:{},probeJson:I,...O}:{id:1,type:"load",model:e.model,...t.file!==void 0?{file:t.file}:{},probeJson:I,request:{backend:m,...t.dtype!==void 0?{dtype:t.dtype}:{},...t.maxContext!==void 0?{maxContext:t.maxContext}:{},...m==="webgpu"&&u==="f32"?{f16:!1}:{}},...L,...e.kind==="check"?{check:!0}:{},offline:t.offline??!1,hubBaseUrl:t.hubBaseUrl??ce,...O},x={worker:W,used:k.used,workerUrl:$,caps:h,why:y,hostWasm:s,request:ee,signal:f,aborted:a,fellBack:null,wasm:null,noted:g=>x.fellBack===null?g:new i(g.kind,`${g.message} (after a CDN fallback: ${x.fellBack})`,g.missing),host:g=>x.wasm!==null?!1:(x.wasm=g,g.failed!==void 0&&(x.fellBack=ke({...g,failed:g.failed}),V(x.fellBack)),!0),workerError:g=>{let U=g.kind==="probe"&&p.length>0?`; the browser gave ${p.join(", ")}, which the host reads as missing`:"";return new i(g.kind,g.message+U)}};if(k.failed!==void 0&&(x.fellBack=we(k.failed,$.href,s===void 0?d:void 0),V(x.fellBack)),f?.aborted)throw W.terminate(),x.noted(a());return x}function A(e){return e instanceof i?{ok:!1,kind:e.kind,message:e.message}:{ok:!1,kind:"backend",message:S(e)}}function Z(e,t,o){let{worker:n,workerUrl:r,signal:s,request:l}=e;return new Promise(c=>{let b=()=>u(e.aborted()),d=a=>{n.onmessage=null,n.onerror=null,s?.removeEventListener("abort",b),n.terminate(),c(a)},u=a=>d(A(e.noted(a)));if(s?.addEventListener("abort",b,{once:!0}),s?.aborted)return u(e.aborted());n.onerror=a=>u(new i("backend",`the engine's worker from ${r.href} failed: ${a.message||"it did not start"}`)),n.onmessage=a=>{let f=a.data;if(!T(f)||f.id!==l.id)return u(w(f));if(f.type==="host")return e.host(f.hostWasm)?void 0:u(w(f));if(f.type!=="progress")return f.type==="error"?u(e.workerError(f)):f.type!==t?u(w(f)):e.wasm===null?u(w(f)):d(o(f))};try{n.postMessage(l)}catch(a){d(A(a))}})}var M=class{static async load(t,o={}){let n=await B({kind:"load",model:t},o),{worker:r,workerUrl:s,signal:l,request:c,hostWasm:b,caps:d}=n;return new Promise((u,a)=>{let f=()=>m(n.aborted()),h=()=>{r.onmessage=null,r.onerror=null,l?.removeEventListener("abort",f)},m=y=>{h(),r.terminate(),a(n.noted(y))};if(l?.addEventListener("abort",f,{once:!0}),l?.aborted)return m(n.aborted());r.onerror=y=>m(new i("backend",`the engine's worker from ${s.href} failed: ${y.message||"it did not start"}`)),r.onmessage=y=>{let p=y.data;if(!T(p)||p.id!==c.id)return m(w(p));switch(p.type){case"host":return n.host(p.hostWasm)?void 0:m(w(p));case"progress":try{o.onProgress?.({file:p.file,loaded:p.loaded,total:p.total,repo:p.repo,revision:p.revision,...p.provenance!==void 0?{provenance:p.provenance}:{}})}catch(k){m(k instanceof i?k:new i("request",`onProgress threw: ${S(k)}`))}return;case"loaded":{let k=n.wasm;if(k===null)return m(w(p));h();let W=n.used.source,$=b!==void 0||W==="page"&&k.failed===void 0?"page":k.failed!==void 0||W==="cdn"?"cdn":"bundle",L={worker:{source:W,url:s.href},hostWasm:{source:$,url:k.url}},O={...p.doctor,source:p.sources,why:p.fallback!==void 0?`${p.fallback}; ${d.why}`:n.why,assets:L};u(new E(r,O));return}case"error":return m(n.workerError(p));default:return m(w(p))}},r.postMessage(c)})}static async check(t,o={}){let n;try{let r=o??{};n=await B({kind:"check",model:t},{...r.backend!==void 0?{backend:r.backend}:{},...r.dtype!==void 0?{dtype:r.dtype}:{},...r.maxContext!==void 0?{maxContext:r.maxContext}:{},...r.storage!==void 0?{storage:r.storage}:{},...r.hubBaseUrl!==void 0?{hubBaseUrl:r.hubBaseUrl}:{},...r.file!==void 0?{file:r.file}:{},...r.signal!==void 0?{signal:r.signal}:{},...r.assets!==void 0?{assets:r.assets}:{}})}catch(r){return A(r)}return Z(n,"checked",r=>({ok:!0,backend:r.backend}))}static async fit(t,o={}){let n;try{let r=typeof t=="object"&&t!==null?Object.getPrototypeOf(t):void 0;if(r!==Object.prototype&&r!==null)throw new i("request","fit takes a GGUF file's recorded facts, a plain object");let s;try{s=JSON.stringify(t)}catch(b){throw new i("request",`the facts do not serialise: ${S(b)}`)}if(typeof s!="string")throw new i("request","the facts do not serialise to a JSON string");let l=new TextEncoder().encode(s).byteLength;if(l>Y)throw new i("request",`the facts are ${l} bytes once serialised, past the ${Y} a fit takes`);let c=o??{};n=await B({kind:"fit",facts:s},{...c.backend!==void 0?{backend:c.backend}:{},...c.maxContext!==void 0?{maxContext:c.maxContext}:{},...c.signal!==void 0?{signal:c.signal}:{},...c.assets!==void 0?{assets:c.assets}:{}})}catch(r){return A(r)}return Z(n,"fitted",r=>({ok:!0,backend:r.backend,maxContext:r.maxContext,contextClamp:r.contextClamp}))}},E=class{#r;#n;#l=2;#t=!1;#e=null;#i=!1;#o=null;constructor(t,o){this.#r=t,this.#n=o,t.onmessage=n=>this.#d(n.data),t.onerror=n=>this.#a(new i("backend",`the engine's worker failed: ${n.message||"it stopped"}`))}chat(t,o={}){return this.#s(),this.#u({messages:t},o)}complete(t,o={}){this.#s();let n;if(typeof t=="string")n={text:t};else if(typeof t=="object"&&t!==null){let r=t;if(r.text!==void 0&&r.ids!==void 0)throw new i("request","complete takes exactly one of text and ids, not both");if(!Array.isArray(r.ids))throw new i("request","complete takes exactly one of text and ids");n={ids:r.ids}}else throw new i("request","complete takes exactly one of text and ids");return this.#u(n,o)}doctor(){this.#s();let t=Object.fromEntries(Object.entries(this.#n.source).map(([s,l])=>[s,{...l}])),{worker:o,hostWasm:n}=this.#n.assets,r=this.#n.aliases.map(s=>({...s}));return{...this.#n,source:t,aliases:r,assets:{worker:{...o},hostWasm:{...n}}}}dispose(){if(!this.#t){this.#t=!0;try{this.#r.postMessage({id:this.#l++,type:"dispose"})}catch{}this.#r.terminate(),this.#o?.sink(new i("request","the engine was disposed"))}}#s(){if(this.#t)throw new i("request","the engine was disposed; load a new one");if(this.#e)throw this.#e}#a(t){this.#e||this.#t||(this.#e=t,this.#r.terminate(),this.#o?.sink(t))}#d(t){if(this.#t||this.#e)return;if(!T(t))return this.#a(w(t));let o=this.#o;if(o===null||t.id!==o.id)return this.#a(w(t));switch(t.type){case"delta":return o.sink(ye(t.delta));case"error":return t.fatal!==void 0&&(this.#e=J(t.fatal)),o.sink(new i(t.kind,t.message));default:return this.#a(w(t))}}async*#u(t,o){if(this.#s(),this.#i)throw new i("request","a generation is already running on this engine");let{signal:n,...r}=o;if(n?.aborted){yield{text:"",done:!0,finishReason:"abort",usage:{promptTokens:0,completionTokens:0,cachedTokens:0}};return}this.#i=!0;let s=this.#l++,l=[],c=null;this.#o={id:s,sink:a=>{l.push(a),c?.()}};let b=()=>{!this.#t&&!this.#e&&this.#r.postMessage({id:s,type:"abort"})},d=!1,u=async()=>{for(;l.length===0;)await new Promise(a=>c=a);return c=null,l.shift()};try{for(this.#r.postMessage({id:s,type:"generate",prompt:t,options:r}),n?.addEventListener("abort",b,{once:!0});;){let a=await u();if(a instanceof i)throw d=!0,a;if(a.done&&(d=!0),yield a,a.done)return}}finally{if(n?.removeEventListener("abort",b),!d)try{for(b();!(this.#t||this.#e);){let a=await u();if(a instanceof i||a.done)break}}catch{}this.#o=null,this.#i=!1}}};export{M as Caldrix,i as CaldrixError,E as Engine,G as probe};
|
|
1
|
+
import{a as N,b as i,d as J}from"./chunk-PZGTLL5H.js";function v(e){return typeof e=="object"&&e!==null}function te(e){return v(e)&&typeof e.token=="number"&&typeof e.text=="string"&&typeof e.done=="boolean"&&(e.finishReason===null||e.finishReason==="stop"||e.finishReason==="length"||e.finishReason==="abort")&&typeof e.promptTokens=="number"&&typeof e.completionTokens=="number"&&typeof e.cachedTokens=="number"&&(e.logprobs===void 0||Array.isArray(e.logprobs)&&e.logprobs.every(t=>v(t)&&typeof t.token=="number"&&typeof t.logprob=="number"))}function re(e){return!v(e)||typeof e.url!="string"?!1:e.failed===void 0||v(e.failed)&&typeof e.failed.url=="string"&&typeof e.failed.reason=="string"}function T(e){if(!v(e)||typeof e.id!="number")return!1;switch(e.type){case"ready":return e.id===0&&typeof e.url=="string";case"host":return re(e.hostWasm);case"progress":return typeof e.file=="string"&&typeof e.loaded=="number"&&(e.total===null||typeof e.total=="number")&&typeof e.repo=="string"&&typeof e.revision=="string"&&(e.provenance===void 0||typeof e.provenance=="string");case"loaded":return v(e.doctor)&&v(e.sources)&&(e.fallback===void 0||typeof e.fallback=="string")&&Object.values(e.sources).every(t=>v(t)&&(t.source==="cache"||t.source==="network")&&typeof t.verified=="boolean");case"checked":return e.backend==="webgpu"||e.backend==="wasm";case"fitted":return(e.backend==="webgpu"||e.backend==="wasm")&&(e.maxContext===null||typeof e.maxContext=="number")&&(e.contextClamp===null||typeof e.contextClamp=="string");case"delta":return te(e.delta);case"result":return"value"in e;case"error":return N(e.kind)&&typeof e.message=="string"&&(e.fatal===void 0||typeof e.fatal=="string");default:return!1}}function w(e){let t;try{t=JSON.stringify(e)??String(e)}catch{t=String(e)}return t.length>200&&(t=`${t.slice(0,200)}...`),new i("backend",`the worker sent a malformed message: ${t}`)}var C=(...e)=>new Uint8Array(e),ne={simd:C(0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,10,1,8,0,65,0,253,15,253,98,11),relaxedSimd:C(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),bulkMemory:C(0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,3,1,0,1,10,14,1,12,0,65,0,65,0,65,0,252,10,0,0,11),threads:C(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),memory64:C(0,97,115,109,1,0,0,0,5,3,1,4,1),tailCall:C(0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,6,1,4,0,18,0,11),exceptions:C(0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,16,1,14,0,2,105,31,64,1,3,0,0,11,0,11,26,11)};function H(){let e={};for(let[t,o]of Object.entries(ne))e[t]=WebAssembly.validate(o);return e}var oe=1e3,D=Symbol("timed out");async function K(e){let t,o=new Promise(n=>{t=setTimeout(()=>n(D),oe)});try{return await Promise.race([e,o])}finally{clearTimeout(t)}}var X=e=>e instanceof Error?e.message:String(e);function Q(){let e=globalThis.navigator;return typeof e=="object"&&e!==null?e:{}}function P(e){return typeof e=="number"&&Number.isFinite(e)?e:null}function se(){let e={available:!1,maxTextureSize:null,floatColorBuffer:null};try{let t=null;if(typeof OffscreenCanvas<"u"?t=new OffscreenCanvas(1,1):typeof document<"u"&&(t=document.createElement("canvas")),t===null)return e;let o=t,n=o.getContext("webgl2"),r=n??o.getContext("webgl");if(!r)return e;let s=P(r.getParameter(r.MAX_TEXTURE_SIZE)),l=r.getExtension(n?"EXT_color_buffer_float":"WEBGL_color_buffer_float")!==null;return r.getExtension("WEBGL_lose_context")?.loseContext(),{available:!0,maxTextureSize:s,floatColorBuffer:l}}catch{return e}}async function ae(e){let t=e.info??{};if(!e.info&&typeof e.requestAdapterInfo=="function")try{t=await e.requestAdapterInfo()}catch{t={}}let o={},n=[];if(e.limits)for(let r in e.limits){let s=e.limits[r];typeof s=="number"?(o[r]=s,Number.isSafeInteger(s)||n.push(`${r} = ${s} (not a safe integer)`)):typeof s!="function"&&n.push(`${r} = ${JSON.stringify(s)??String(s)} (not a number)`)}return{vendor:t.vendor??"",architecture:t.architecture??"",device:t.device??"",description:t.description??"",features:e.features?[...e.features]:[],limits:o,unrepresentable:n}}async function ie(){let e=Q().gpu;if(!e||typeof e.requestAdapter!="function")return{webgpu:null,available:!1,reason:"no navigator.gpu"};let t;try{let n=await K(e.requestAdapter());if(n===D)return{webgpu:null,available:!1,reason:"requestAdapter timed out"};t=n}catch(n){return{webgpu:null,available:!1,reason:`requestAdapter failed: ${X(n)}`}}if(!t)return{webgpu:null,available:!1,reason:"requestAdapter returned null"};let o=await ae(t);try{let n=await K(t.requestDevice());if(n===D)return{webgpu:o,available:!1,reason:"requestDevice timed out"};n.destroy?.()}catch(n){return{webgpu:o,available:!1,reason:`requestDevice refused: ${X(n)}`}}return{webgpu:o,available:!0}}function le(e){return e.webgpu.available&&e.webgpu.built?"webgpu":"wasm"}function ue(e,t,o){let n=e.webgpu?.unrepresentable??[],r=n.length>0?`; adapter limits ${n.join(", ")}: the host reads them as missing`:"";return de(e,t,o)+r}function de(e,t,o){if(o==="webgpu"){let r=e.webgpu,s=r.features.includes("shader-f16")?" with shader-f16":"";return`WebGPU adapter present (${r.vendor} ${r.architecture})${s}: webgpu`}let n=e.wasm.simd?"with simd128":"without simd128";return e.webgpu===null?`no WebGPU adapter (${t.reason??"unavailable"}): wasm ${n}`:`WebGPU adapter present but unusable (${t.reason}): wasm ${n}`}async function G(){let e=Q(),t=await ie(),o={wasm:H(),threads:{sharedArrayBuffer:typeof SharedArrayBuffer<"u",crossOriginIsolated:globalThis.crossOriginIsolated===!0,hardwareConcurrency:P(e.hardwareConcurrency)},webgl:se(),webgpu:t.webgpu,memory:{deviceMemoryGiB:P(e.deviceMemory)},tiers:{webgpu:t.available?{available:!0,built:!0}:{available:!1,built:!0,reason:t.reason},webgl:{available:!1,built:!1,reason:"not built"},wasm:{available:!0,built:!0}}};o.tiers.webgl.available=o.webgl.available;let n=le(o.tiers);return{...o,chosen:n,why:ue(o,t,n)}}function z(e){let t=e.webgpu===null?null:{vendor:e.webgpu.vendor,architecture:e.webgpu.architecture,device:e.webgpu.device,description:e.webgpu.description,features:e.webgpu.features,limits:Object.fromEntries(Object.entries(e.webgpu.limits).filter(([,o])=>Number.isSafeInteger(o)))};return JSON.stringify({wasm:{simd:e.wasm.simd,relaxedSimd:e.wasm.relaxedSimd,bulkMemory:e.wasm.bulkMemory,threads:e.wasm.threads,memory64:e.wasm.memory64,tailCall:e.wasm.tailCall,exceptions:e.wasm.exceptions},threads:{sharedArrayBuffer:e.threads.sharedArrayBuffer,crossOriginIsolated:e.threads.crossOriginIsolated,hardwareConcurrency:e.threads.hardwareConcurrency},webgpu:t,memory:{deviceMemoryGiB:e.memory.deviceMemoryGiB}})}var ce="https://huggingface.co";function fe(e){return typeof location<"u"?new URL(String(e),location.href):new URL(String(e))}var pe="https://cdn.jsdelivr.net/npm/@caldrix-ai/caldrix@0.8.0/";function S(e){return e instanceof Error?e.message:String(e)}function F(e,t){if(t!==void 0){if(typeof t!="string"&&!(t instanceof URL))throw new i("request",`assets.${e} is a string or a URL, not ${typeof t}`);try{return typeof location<"u"?new URL(t,location.href):new URL(t)}catch{throw new i("request",`assets.${e} ${JSON.stringify(String(t))} is not a URL`)}}}function be(e,t){let o=_(e,t),n=null,r=()=>{n!==null&&URL.revokeObjectURL(n),n=null};try{return!o&&!t?{worker:new Worker(new URL("./worker.js",import.meta.url),{type:"module"}),revoke:r,literal:!0}:o?(n=URL.createObjectURL(new Blob([`import ${JSON.stringify(e.href)};`],{type:"text/javascript"})),{worker:new Worker(n,{type:"module"}),revoke:r}):{worker:new Worker(e,{type:"module"}),revoke:r}}catch(s){throw r(),s}}function me(e){return typeof location<"u"&&e.origin!==location.origin}function _(e,t){return me(e)&&(t||e.protocol==="http:"||e.protocol==="https:")}function q(e){return!e.chosen&&!_(e.url,!1)?`the package's worker (dist/worker.js as the page's bundler emitted it, or ${e.url.href} unbundled)`:`the engine's worker from ${e.url.href}${_(e.url,e.chosen)?" (through a same-origin shim)":""}`}var R=class{constructor(t){this.reason=t}reason};function ge(e,t,o){return new Promise((n,r)=>{let s;try{s=be(e.url,e.chosen)}catch(u){r(new R(`the page could not construct it: ${S(u)}`));return}let{worker:l,revoke:c}=s,b=()=>d(o()),d=(u,a=e.url)=>{if(c(),l.onmessage=null,l.onerror=null,t?.removeEventListener("abort",b),u===null)return n({worker:l,url:a});l.terminate(),r(u)};t?.addEventListener("abort",b,{once:!0}),l.onerror=u=>d(new R(u.message||"it did not load")),l.onmessage=u=>{let a=u.data;if(!T(a)||a.id!==0)return d(w(a));if(a.type==="ready")return s.literal?URL.canParse(a.url)?d(null,new URL(a.url)):d(w(a)):d(null);if(a.type==="error")return d(new R(a.message));d(w(a))}})}async function he(e,t,o,n){let r=[];for(let d of e)try{let{worker:u,url:a}=await ge(d,o,n),f=r[0];return{worker:u,used:d,url:a,...f!==void 0?{failed:{what:q(f.t),reason:f.reason}}:{}}}catch(u){if(!(u instanceof R))throw u;r.push({t:d,reason:u.reason})}let[s,l]=r;if(s!==void 0&&l!==void 0){let d=l.t.url.origin;throw new i("backend",`${q(s.t)} did not start (${s.reason}), nor from the CDN fallback ${l.t.url.href} (${l.reason}); serve dist/ and host/ beside the bundle, set assets.worker to where the page serves them, or allow ${d} in the page's Content-Security-Policy (worker-src blob: for the same-origin shim that starts it, script-src ${d} for the modules it imports, connect-src ${d} for the .wasm)`)}let c=s,b=t&&c.t.source==="bundle"?"; the CDN fallback is off (assets.fallback is false)":"";throw new i("backend",`${q(c.t)} did not start: ${c.reason}${b}; caldrix.js runs inference in a Web Worker and has no main-thread mode`)}function we(e,t,o){let n=o!==void 0?`, and with it the host .wasm from ${o}`:"";return`${e.what} did not start (${e.reason}); it was loaded from the CDN fallback ${t}${n}`}function ke(e){return`the host .wasm could not be fetched from ${e.failed.url} (${e.failed.reason}); it was fetched from the CDN fallback ${e.url}`}function V(e){console.warn(`caldrix.js: ${e}. Serve dist/ and host/ beside the bundle or set assets.worker/assets.hostWasm; assets.fallback: false turns the fallback off.`)}function ye(e){let t={text:e.text,token:e.token,done:e.done};return e.logprobs!==void 0&&(t.logprobs=e.logprobs),e.done&&(e.finishReason!==null&&(t.finishReason=e.finishReason),t.usage={promptTokens:e.promptTokens,completionTokens:e.completionTokens,cachedTokens:e.cachedTokens}),t}var Y=4<<20;async function B(e,t){let o=t.backend??"auto";if(o==="webgl")throw new i("backend","the webgl backend is not built");if(o!=="auto"&&o!=="webgpu"&&o!=="wasm")throw new i("request",`backend ${JSON.stringify(o)} is not auto, webgpu, wasm or webgl`);let n=e.kind==="fit"?"":e.model;if(e.kind!=="fit"&&(typeof n!="string"||n===""))throw new i("request","load takes a model alias or a Hub repo id (owner/name[@revision]), a non-empty string");if(t.file!==void 0&&(typeof t.file!="string"||t.file===""))throw new i("request",`file is the name of a GGUF file in the repo, a non-empty string, not ${JSON.stringify(t.file)?.slice(0,80)}`);let r=F("worker",t.assets?.worker),s=F("hostWasm",t.assets?.hostWasm),l=t.assets?.fallback??!0;if(typeof l!="boolean")throw new i("request",`assets.fallback is true or false, not ${JSON.stringify(l)}`);let c=F("cdn",t.assets?.cdn)??new URL(pe);c.pathname.endsWith("/")||(c.pathname+="/");let b=[r!==void 0?{url:r,chosen:!0,source:"page"}:{url:fe(new URL("./worker.js",import.meta.url)),chosen:!1,source:"bundle"}];l&&r===void 0&&b.push({url:new URL("dist/worker.js",c),chosen:!0,source:"cdn"});let d=l&&s===void 0?new URL("host/caldrix_web_bg.wasm",c).href:void 0,u=t.storage??"auto";if(u!=="auto"&&u!=="f32")throw new i("request",`storage ${JSON.stringify(u)} is not auto or f32`);if(o==="wasm"&&u==="f32")throw new i("request",`storage "f32" is the WebGPU tier's weight storage; backend "wasm" takes none`);if(t.hubBaseUrl!==void 0){let g=t.hubBaseUrl,U=!1;if(typeof g=="string")try{let j=new URL(g);U=j.protocol==="http:"||j.protocol==="https:"}catch{U=!1}if(!U)throw new i("request",`hubBaseUrl must be an absolute http(s) URL, not ${JSON.stringify(g).slice(0,80)}`)}let a=()=>new i("fetch",e.kind==="fit"?"the fit was aborted":`${e.kind==="check"?"checking":"loading"} ${n} was aborted`),f=t.signal;if(f?.aborted)throw a();let h=await G(),m,y;if(o==="auto"&&t.dtype==="bf16"&&h.chosen==="webgpu")m="wasm",y=`bf16 requested: wasm (the WebGPU tier runs the f32 plan only); ${h.why}`;else if(o==="auto")m=h.chosen,y=h.why;else if(o==="webgpu"){if(!h.tiers.webgpu.available)throw new i("backend",`backend "webgpu" was asked for, but the probe found no usable WebGPU adapter: ${h.tiers.webgpu.reason??h.why}`);m="webgpu",y=`backend "webgpu" asked for; ${h.why}`}else m="wasm",y=`backend "wasm" asked for; ${h.why}`;let p=h.webgpu?.unrepresentable??[];if(f?.aborted)throw a();let k=await he(b,!l,f,a),{worker:W}=k,$=k.url,L=o==="auto"&&m==="webgpu"?{auto:!0}:{},O={...s!==void 0?{hostWasm:s.href}:{},...d!==void 0?{cdnWasm:d}:{}},I=z(h),ee=e.kind==="fit"?{id:1,type:"fit",facts:e.facts,backend:m,...L,...t.maxContext!==void 0?{maxContext:t.maxContext}:{},probeJson:I,...O}:{id:1,type:"load",model:e.model,...t.file!==void 0?{file:t.file}:{},probeJson:I,request:{backend:m,...t.dtype!==void 0?{dtype:t.dtype}:{},...t.maxContext!==void 0?{maxContext:t.maxContext}:{},...m==="webgpu"&&u==="f32"?{f16:!1}:{}},...L,...e.kind==="check"?{check:!0}:{},offline:t.offline??!1,hubBaseUrl:t.hubBaseUrl??ce,...O},x={worker:W,used:k.used,workerUrl:$,caps:h,why:y,hostWasm:s,request:ee,signal:f,aborted:a,fellBack:null,wasm:null,noted:g=>x.fellBack===null?g:new i(g.kind,`${g.message} (after a CDN fallback: ${x.fellBack})`,g.missing),host:g=>x.wasm!==null?!1:(x.wasm=g,g.failed!==void 0&&(x.fellBack=ke({...g,failed:g.failed}),V(x.fellBack)),!0),workerError:g=>{let U=g.kind==="probe"&&p.length>0?`; the browser gave ${p.join(", ")}, which the host reads as missing`:"";return new i(g.kind,g.message+U)}};if(k.failed!==void 0&&(x.fellBack=we(k.failed,$.href,s===void 0?d:void 0),V(x.fellBack)),f?.aborted)throw W.terminate(),x.noted(a());return x}function A(e){return e instanceof i?{ok:!1,kind:e.kind,message:e.message}:{ok:!1,kind:"backend",message:S(e)}}function Z(e,t,o){let{worker:n,workerUrl:r,signal:s,request:l}=e;return new Promise(c=>{let b=()=>u(e.aborted()),d=a=>{n.onmessage=null,n.onerror=null,s?.removeEventListener("abort",b),n.terminate(),c(a)},u=a=>d(A(e.noted(a)));if(s?.addEventListener("abort",b,{once:!0}),s?.aborted)return u(e.aborted());n.onerror=a=>u(new i("backend",`the engine's worker from ${r.href} failed: ${a.message||"it did not start"}`)),n.onmessage=a=>{let f=a.data;if(!T(f)||f.id!==l.id)return u(w(f));if(f.type==="host")return e.host(f.hostWasm)?void 0:u(w(f));if(f.type!=="progress")return f.type==="error"?u(e.workerError(f)):f.type!==t?u(w(f)):e.wasm===null?u(w(f)):d(o(f))};try{n.postMessage(l)}catch(a){d(A(a))}})}var M=class{static async load(t,o={}){let n=await B({kind:"load",model:t},o),{worker:r,workerUrl:s,signal:l,request:c,hostWasm:b,caps:d}=n;return new Promise((u,a)=>{let f=()=>m(n.aborted()),h=()=>{r.onmessage=null,r.onerror=null,l?.removeEventListener("abort",f)},m=y=>{h(),r.terminate(),a(n.noted(y))};if(l?.addEventListener("abort",f,{once:!0}),l?.aborted)return m(n.aborted());r.onerror=y=>m(new i("backend",`the engine's worker from ${s.href} failed: ${y.message||"it did not start"}`)),r.onmessage=y=>{let p=y.data;if(!T(p)||p.id!==c.id)return m(w(p));switch(p.type){case"host":return n.host(p.hostWasm)?void 0:m(w(p));case"progress":try{o.onProgress?.({file:p.file,loaded:p.loaded,total:p.total,repo:p.repo,revision:p.revision,...p.provenance!==void 0?{provenance:p.provenance}:{}})}catch(k){m(k instanceof i?k:new i("request",`onProgress threw: ${S(k)}`))}return;case"loaded":{let k=n.wasm;if(k===null)return m(w(p));h();let W=n.used.source,$=b!==void 0||W==="page"&&k.failed===void 0?"page":k.failed!==void 0||W==="cdn"?"cdn":"bundle",L={worker:{source:W,url:s.href},hostWasm:{source:$,url:k.url}},O={...p.doctor,source:p.sources,why:p.fallback!==void 0?`${p.fallback}; ${d.why}`:n.why,assets:L};u(new E(r,O));return}case"error":return m(n.workerError(p));default:return m(w(p))}},r.postMessage(c)})}static async check(t,o={}){let n;try{let r=o??{};n=await B({kind:"check",model:t},{...r.backend!==void 0?{backend:r.backend}:{},...r.dtype!==void 0?{dtype:r.dtype}:{},...r.maxContext!==void 0?{maxContext:r.maxContext}:{},...r.storage!==void 0?{storage:r.storage}:{},...r.hubBaseUrl!==void 0?{hubBaseUrl:r.hubBaseUrl}:{},...r.file!==void 0?{file:r.file}:{},...r.signal!==void 0?{signal:r.signal}:{},...r.assets!==void 0?{assets:r.assets}:{}})}catch(r){return A(r)}return Z(n,"checked",r=>({ok:!0,backend:r.backend}))}static async fit(t,o={}){let n;try{let r=typeof t=="object"&&t!==null?Object.getPrototypeOf(t):void 0;if(r!==Object.prototype&&r!==null)throw new i("request","fit takes a GGUF file's recorded facts, a plain object");let s;try{s=JSON.stringify(t)}catch(b){throw new i("request",`the facts do not serialise: ${S(b)}`)}if(typeof s!="string")throw new i("request","the facts do not serialise to a JSON string");let l=new TextEncoder().encode(s).byteLength;if(l>Y)throw new i("request",`the facts are ${l} bytes once serialised, past the ${Y} a fit takes`);let c=o??{};n=await B({kind:"fit",facts:s},{...c.backend!==void 0?{backend:c.backend}:{},...c.maxContext!==void 0?{maxContext:c.maxContext}:{},...c.signal!==void 0?{signal:c.signal}:{},...c.assets!==void 0?{assets:c.assets}:{}})}catch(r){return A(r)}return Z(n,"fitted",r=>({ok:!0,backend:r.backend,maxContext:r.maxContext,contextClamp:r.contextClamp}))}},E=class{#r;#n;#l=2;#t=!1;#e=null;#i=!1;#o=null;constructor(t,o){this.#r=t,this.#n=o,t.onmessage=n=>this.#d(n.data),t.onerror=n=>this.#a(new i("backend",`the engine's worker failed: ${n.message||"it stopped"}`))}chat(t,o={}){return this.#s(),this.#u({messages:t},o)}complete(t,o={}){this.#s();let n;if(typeof t=="string")n={text:t};else if(typeof t=="object"&&t!==null){let r=t;if(r.text!==void 0&&r.ids!==void 0)throw new i("request","complete takes exactly one of text and ids, not both");if(!Array.isArray(r.ids))throw new i("request","complete takes exactly one of text and ids");n={ids:r.ids}}else throw new i("request","complete takes exactly one of text and ids");return this.#u(n,o)}doctor(){this.#s();let t=Object.fromEntries(Object.entries(this.#n.source).map(([s,l])=>[s,{...l}])),{worker:o,hostWasm:n}=this.#n.assets,r=this.#n.aliases.map(s=>({...s}));return{...this.#n,source:t,aliases:r,assets:{worker:{...o},hostWasm:{...n}}}}dispose(){if(!this.#t){this.#t=!0;try{this.#r.postMessage({id:this.#l++,type:"dispose"})}catch{}this.#r.terminate(),this.#o?.sink(new i("request","the engine was disposed"))}}#s(){if(this.#t)throw new i("request","the engine was disposed; load a new one");if(this.#e)throw this.#e}#a(t){this.#e||this.#t||(this.#e=t,this.#r.terminate(),this.#o?.sink(t))}#d(t){if(this.#t||this.#e)return;if(!T(t))return this.#a(w(t));let o=this.#o;if(o===null||t.id!==o.id)return this.#a(w(t));switch(t.type){case"delta":return o.sink(ye(t.delta));case"error":return t.fatal!==void 0&&(this.#e=J(t.fatal)),o.sink(new i(t.kind,t.message));default:return this.#a(w(t))}}async*#u(t,o){if(this.#s(),this.#i)throw new i("request","a generation is already running on this engine");let{signal:n,...r}=o;if(n?.aborted){yield{text:"",done:!0,finishReason:"abort",usage:{promptTokens:0,completionTokens:0,cachedTokens:0}};return}this.#i=!0;let s=this.#l++,l=[],c=null;this.#o={id:s,sink:a=>{l.push(a),c?.()}};let b=()=>{!this.#t&&!this.#e&&this.#r.postMessage({id:s,type:"abort"})},d=!1,u=async()=>{for(;l.length===0;)await new Promise(a=>c=a);return c=null,l.shift()};try{for(this.#r.postMessage({id:s,type:"generate",prompt:t,options:r}),n?.addEventListener("abort",b,{once:!0});;){let a=await u();if(a instanceof i)throw d=!0,a;if(a.done&&(d=!0),yield a,a.done)return}}finally{if(n?.removeEventListener("abort",b),!d)try{for(b();!(this.#t||this.#e);){let a=await u();if(a instanceof i||a.done)break}}catch{}this.#o=null,this.#i=!1}}};export{M as Caldrix,i as CaldrixError,E as Engine,G as probe};
|
package/host/caldrix_web_bg.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED