@caldrix-ai/caldrix 0.6.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 CHANGED
@@ -3,6 +3,18 @@
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
+
12
+ ## 0.7.0 — 2026-11-05
13
+
14
+ ### Added
15
+
16
+ - `Caldrix.fit(facts, opts?)` and its types `QuantFacts`, `FitOptions` and `FitResult`: whether a GGUF file would load in this tab, on which tier and at what context (`maxContext`, and `contextClamp` when the default context is clamped), from the facts recorded for it (its size, dimensions, `general.file_type` and tensor table), fetching nothing: no Hub listing, no range and no file. It runs `check`'s option checks, probe and tier choice and the precheck a load of the file runs, with the file's header replaced by the facts; a tensor type this build does not run is an `architecture` refusal naming it. It never rejects; an abort is `kind: "fetch"`. A facts admit is never a load guarantee: `check` and `load` judge the file's own header at the pinned commit. Run in headless Chromium against the loopback hub's tiny fixtures on the wasm tier and on the WebGPU tier on SwiftShader (a CPU), where the fit agreed with a load of the same file and made no request to the hub (`inferno/WEB-VERIFICATION.md` entry 24). Not run: facts the nightly recorded for a real repo; a real adapter.
17
+
6
18
  ## 0.6.0 — 2026-11-05
7
19
 
8
20
  ### 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: bbee99a429607e75344d560ed466c95c06e3be6a -->
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
@@ -41,6 +41,7 @@ The API, as the package's declarations have it:
41
41
  class Caldrix {
42
42
  static load(model: string, opts?: LoadOptions): Promise<Engine>;
43
43
  static check(model: string, opts?: CheckOptions): Promise<CheckResult>;
44
+ static fit(facts: QuantFacts, opts?: FitOptions): Promise<FitResult>;
44
45
  }
45
46
  class Engine {
46
47
  chat(messages: ChatMessage[], opts?: GenerateOptions): AsyncIterable<Delta>;
@@ -149,6 +150,46 @@ through `signal` (`checking <model> was aborted`), so a load might still succeed
149
150
  for the package's own files applies as on a load (off with `assets.fallback: false`), warned once per check on
150
151
  the console. The files a check fetches are written to the cache, and a later load reuses them.
151
152
 
153
+ ## Fitting a GGUF file from its recorded facts
154
+
155
+ ```ts
156
+ type FitOptions = Pick<CheckOptions, "backend" | "maxContext" | "signal" | "assets">;
157
+ type FitResult = { ok: true; backend: "webgpu" | "wasm"; maxContext: number | null; contextClamp: NonNullable<DoctorReport["contextClamp"]> | null } | { ok: false; kind: Kind; message: string };
158
+
159
+ // `facts` is a `QuantFacts`: what the nightly records for one GGUF file of a repo.
160
+ const r = await Caldrix.fit(facts);
161
+ if (r.ok) console.log(`a load would run on ${r.backend} at context ${r.maxContext}`);
162
+ else if (r.kind !== "fetch") console.log(`a load would be refused: ${r.message}`);
163
+ ```
164
+
165
+ `Caldrix.fit` says whether a GGUF file would load in this tab, on which tier and at what context,
166
+ from the facts recorded for it (`QuantFacts`: the file's name, size and digest, its architecture and
167
+ dimensions, the element count per tensor type, the base model (`base_model`), `general.file_type`, and the tensor table, each
168
+ tensor's name, type and dims), and it fetches nothing: no Hub listing, no range and no file. It takes
169
+ `check`'s tier and context options and `assets`, runs the same option checks, probe and tier choice,
170
+ and in its worker the same precheck a load of the file runs, with the file's header replaced by the
171
+ facts; on the WebGPU tier it opens the device and destroys it again, and under `backend: "auto"` a
172
+ WebGPU refusal answers wasm as `check`'s does. `{ ok: true }` carries the tier, `maxContext` (the
173
+ context a load would plan) and `contextClamp` (the clamp's line when the default context was
174
+ clamped, else `null`), as a load's `doctor()` reports them.
175
+
176
+ A caller holding a larger record (the nightly's `web-quants.json` entry, say) passes only these keys:
177
+ an unknown key is refused as `validate`, so a page projects each entry onto them. `fit` answers the
178
+ default weight storage for the tier; `FitOptions` has no `storage`, so a page that loads a dense file
179
+ with `storage: "f32"` gets `fit`'s answer for the default storage (quantized files are unaffected).
180
+
181
+ The tensor types `fit` admits are the ones this build runs: a tensor of any other type is
182
+ `{ ok: false, kind: "architecture" }`, its name and type in the message. Facts that are not a plain
183
+ object, or are past the size a fit takes once serialised, are `request`; facts the host refuses (an
184
+ unknown or missing key, a table past the GGUF reader's caps) are `validate`; a file past this tab's
185
+ limits is `fit`, and a worker that cannot start or answers out of protocol is `backend`. `fit` never rejects; `kind: "fetch"` (an abort through `signal`, `the fit was
186
+ aborted`) leaves the answer unknown.
187
+
188
+ A facts admit is never a load guarantee. `check` and `load` judge the file's own header at the commit
189
+ the listing pins, and the facts cannot show what only the file holds: the values of
190
+ `rope_freqs.weight`, a missing `attention.layer_norm_rms_epsilon`, or a tensor whose bytes or element
191
+ count disagree with the table. Facts recorded from an older commit of a repo answer for that file.
192
+
152
193
  ## Constrained decoding
153
194
 
154
195
  ```ts
@@ -186,7 +227,7 @@ one of the strings and stops. `{ json: <schema> }` yields a JSON value matching
186
227
  ## What has run, and where
187
228
 
188
229
  Only these model runs are recorded (`inferno/WEB-VERIFICATION.md`, entries 5, 8 and 9, entries 10,
189
- 11, 12 and 14 for the quantized aliases, entries 16 and 17 for the adapter and nf4 repos, and entry 22 for a GGUF file in a repo); nothing
230
+ 11, 12 and 14 for the quantized aliases, entries 16 and 17 for the adapter and nf4 repos, entry 22 for a GGUF file in a repo, and entry 24 for `Caldrix.fit`); nothing
190
231
  else is claimed.
191
232
 
192
233
  - **Model:** SmolLM2-135M-Instruct, one prompt, 8 greedy tokens; the adapter and nf4 repos below add a
@@ -210,11 +251,12 @@ else is claimed.
210
251
  - adapter and nf4 repos by id (a LoRA adapter over SmolLM2-135M-Instruct, and SmolLM2-360M-Instruct's bitsandbytes nf4 repo, each at a pinned sha): off-browser at the desk on both tiers (`inferno/WEB-VERIFICATION.md` entry 16: wasm both pairs and WebGPU the LoRA pair token-exact, the f32 plan at f32 weight storage; the WebGPU nf4 pair stopped at lavapipe's storage-buffer binding limit); in headless Chromium through `Caldrix.load` by repo id on both tiers, both pairs token-exact, the f32 plan at f32 weight storage, WebGPU on Dawn/SwiftShader, a CPU (entry 17).
211
252
  - `Caldrix.check`: in headless Chromium against the loopback hub (`inferno/WEB-VERIFICATION.md` entry 20): on a repo id, check and load agreed on the wasm tier and, under `--enable-unsafe-webgpu`, on the WebGPU tier on Dawn/SwiftShader, a CPU, and the check fetched no tokenizer file and no weight; an index past the wasm ceiling checked as `fit`, an unknown repo id as `fetch`.
212
253
  - a GGUF file in a repo by name (`file`): in headless Chromium through `Caldrix.load` against the loopback hub's tiny fixtures, on the wasm tier and on the WebGPU tier on Dawn/SwiftShader, a CPU, token-exact to the test harness's own tokens, with the name selecting the file, a digest mismatch refused as `digest`, and a header context past the wasm32 heap clamped and reported by `doctor`; off-browser, a rope-scaled quantized model token-exact on both tiers and the quantized row kernels past the grid dimension limit on lavapipe, a CPU (`inferno/WEB-VERIFICATION.md` entry 22).
254
+ - `Caldrix.fit`: in headless Chromium against the loopback hub's tiny fixtures, on the wasm tier and on the WebGPU tier on Dawn/SwiftShader, a CPU: the committed facts of the hub's Q8_0 file and of its long-context copy answered `ok` with the tier, and the `maxContext` and `contextClamp` a load of that file reported in `doctor()`, with no request to the hub; the facts with one tensor's type changed to one this build lacks answered `architecture` naming it; off-browser, the fit from facts equals the fit from the header on every tiny fixture (`inferno/WEB-VERIFICATION.md` entry 24).
213
255
  - **Not run:** any real GGUF repo by id in a browser; Llama 3.2 or any model with a 128k vocabulary in a browser; a 2D-grid dispatch on a real adapter (the WebGPU row kernels past the grid dimension limit ran on lavapipe only); the WebGPU heap clamp in a browser; any other WebGPU adapter or GPU; a headed browser; the browser stages inside a sandbox; the scalar
214
256
  (non-SIMD) wasm spelling in a browser; real-Hub digest behaviour (the runs used a loopback Hub stand-in); a real Hub listing for a repo id
215
257
  (the loopback hub served the pinned snapshots); any browser other than Playwright's headless Chromium; any other model;
216
258
  `Caldrix.check` against a real adapter's buffer limit (its unit tests only), against the real Hub, or in any other
217
- browser or on any other device. No performance figure is recorded
259
+ browser or on any other device; `Caldrix.fit` on facts the nightly recorded for a real repo, or on a real adapter. No performance figure is recorded
218
260
  or claimed.
219
261
 
220
262
  ## The default context
package/dist/index.d.ts CHANGED
@@ -223,6 +223,53 @@ type CheckResult = {
223
223
  kind: Kind;
224
224
  message: string;
225
225
  };
226
+ /**
227
+ * A GGUF file's facts as the nightly records them for each quant it promotes: the file, its size and
228
+ * digest, the architecture and dimensions read from its header, the element count per tensor type,
229
+ * the base model, the header's `general.file_type`, and the tensor table (`[name, type, dims]`).
230
+ */
231
+ interface QuantFacts {
232
+ file: string;
233
+ bytes: number;
234
+ sha256: string;
235
+ arch: string;
236
+ dims: {
237
+ hidden: number;
238
+ n_layers: number;
239
+ n_heads: number;
240
+ n_kv_heads: number;
241
+ head_dim: number;
242
+ ffn: number;
243
+ vocab: number;
244
+ context_length: number;
245
+ /** 1 when the file has no `output.weight` (the head is the embedding), else 0. */
246
+ tied: number;
247
+ };
248
+ /** Tensor type name to element count. */
249
+ elements: Record<string, number>;
250
+ base_model: string | null;
251
+ /** `general.file_type`, `null` when the header states none. */
252
+ file_type: number | null;
253
+ /** `[name, type name, dims]` of every tensor, in header order. */
254
+ tensors: [string, string, number[]][];
255
+ }
256
+ /** What `Caldrix.fit` takes: the tier and context options of a check, and its `signal` and `assets`. */
257
+ type FitOptions = Pick<CheckOptions, "backend" | "maxContext" | "signal" | "assets">;
258
+ /**
259
+ * `Caldrix.fit`'s answer: the tier a load of the file would end on in this tab, and the context it
260
+ * would be loaded at (`maxContext`, and `contextClamp`, the clamp's line when the default was
261
+ * clamped), or why it would be refused. `kind: "fetch"` (the fit was aborted) leaves it unknown.
262
+ */
263
+ type FitResult = {
264
+ ok: true;
265
+ backend: "webgpu" | "wasm";
266
+ maxContext: number | null;
267
+ contextClamp: NonNullable<DoctorReport["contextClamp"]> | null;
268
+ } | {
269
+ ok: false;
270
+ kind: Kind;
271
+ message: string;
272
+ };
226
273
  declare class Caldrix {
227
274
  /** Load a model, a built-in alias or a Hub repo id (`owner/name[@revision]`), into its own worker. */
228
275
  static load(model: string, opts?: LoadOptions): Promise<Engine>;
@@ -237,6 +284,19 @@ declare class Caldrix {
237
284
  * the answer unknown.
238
285
  */
239
286
  static check(model: string, opts?: CheckOptions): Promise<CheckResult>;
287
+ /**
288
+ * Whether a GGUF file would load in this tab, from the facts the nightly recorded for it, without
289
+ * fetching a byte of it: the same tier choice as `check` (on WebGPU the precheck and the device
290
+ * open, destroyed again; under `auto` a refusal of either moves the answer to wasm), with the
291
+ * file's head replaced by `facts`. No listing, range or file is fetched. Answers the tier and the
292
+ * context a load would plan (`maxContext`, and `contextClamp` when the default was clamped). A
293
+ * facts admit is no load guarantee: `check` and `load` judge the file's own header. Never
294
+ * rejects: every refusal is `{ ok: false, kind, message }` (`request` for `facts` that are not a
295
+ * plain object or past `FACTS_MAX_BYTES` once serialised, `validate` for facts the host refuses,
296
+ * `architecture` for a tensor type this build does not run, `fit` for a miss), and an abort is
297
+ * `kind: "fetch"`, the answer unknown.
298
+ */
299
+ static fit(facts: QuantFacts, opts?: FitOptions): Promise<FitResult>;
240
300
  }
241
301
  declare class Engine {
242
302
  #private;
@@ -305,4 +365,4 @@ interface Capabilities {
305
365
  }
306
366
  declare function probe(): Promise<Capabilities>;
307
367
 
308
- export { type AssetSource, Caldrix, CaldrixError, type Capabilities, type ChatMessage, type CheckOptions, type CheckResult, type Delta, type DoctorReport, Engine, type GenerateOptions, type GenerationOptions, type Kind, type LoadOptions, probe };
368
+ export { type AssetSource, Caldrix, CaldrixError, type Capabilities, type ChatMessage, type CheckOptions, type CheckResult, type Delta, type DoctorReport, Engine, type FitOptions, type FitResult, type GenerateOptions, type GenerationOptions, type Kind, type LoadOptions, type QuantFacts, probe };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{a as F,b as i,d as _}from"./chunk-PZGTLL5H.js";function W(e){return typeof e=="object"&&e!==null}function V(e){return W(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=>W(t)&&typeof t.token=="number"&&typeof t.logprob=="number"))}function Q(e){return!W(e)||typeof e.url!="string"?!1:e.failed===void 0||W(e.failed)&&typeof e.failed.url=="string"&&typeof e.failed.reason=="string"}function L(e){if(!W(e)||typeof e.id!="number")return!1;switch(e.type){case"ready":return e.id===0&&typeof e.url=="string";case"host":return Q(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 W(e.doctor)&&W(e.sources)&&(e.fallback===void 0||typeof e.fallback=="string")&&Object.values(e.sources).every(t=>W(t)&&(t.source==="cache"||t.source==="network")&&typeof t.verified=="boolean");case"checked":return e.backend==="webgpu"||e.backend==="wasm";case"delta":return V(e.delta);case"result":return"value"in e;case"error":return F(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),Z={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 j(){let e={};for(let[t,n]of Object.entries(Z))e[t]=WebAssembly.validate(n);return e}var Y=1e3,S=Symbol("timed out");async function N(e){let t,n=new Promise(r=>{t=setTimeout(()=>r(S),Y)});try{return await Promise.race([e,n])}finally{clearTimeout(t)}}var J=e=>e instanceof Error?e.message:String(e);function H(){let e=globalThis.navigator;return typeof e=="object"&&e!==null?e:{}}function E(e){return typeof e=="number"&&Number.isFinite(e)?e:null}function ee(){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 n=t,r=n.getContext("webgl2"),o=r??n.getContext("webgl");if(!o)return e;let a=E(o.getParameter(o.MAX_TEXTURE_SIZE)),u=o.getExtension(r?"EXT_color_buffer_float":"WEBGL_color_buffer_float")!==null;return o.getExtension("WEBGL_lose_context")?.loseContext(),{available:!0,maxTextureSize:a,floatColorBuffer:u}}catch{return e}}async function te(e){let t=e.info??{};if(!e.info&&typeof e.requestAdapterInfo=="function")try{t=await e.requestAdapterInfo()}catch{t={}}let n={},r=[];if(e.limits)for(let o in e.limits){let a=e.limits[o];typeof a=="number"?(n[o]=a,Number.isSafeInteger(a)||r.push(`${o} = ${a} (not a safe integer)`)):typeof a!="function"&&r.push(`${o} = ${JSON.stringify(a)??String(a)} (not a number)`)}return{vendor:t.vendor??"",architecture:t.architecture??"",device:t.device??"",description:t.description??"",features:e.features?[...e.features]:[],limits:n,unrepresentable:r}}async function re(){let e=H().gpu;if(!e||typeof e.requestAdapter!="function")return{webgpu:null,available:!1,reason:"no navigator.gpu"};let t;try{let r=await N(e.requestAdapter());if(r===S)return{webgpu:null,available:!1,reason:"requestAdapter timed out"};t=r}catch(r){return{webgpu:null,available:!1,reason:`requestAdapter failed: ${J(r)}`}}if(!t)return{webgpu:null,available:!1,reason:"requestAdapter returned null"};let n=await te(t);try{let r=await N(t.requestDevice());if(r===S)return{webgpu:n,available:!1,reason:"requestDevice timed out"};r.destroy?.()}catch(r){return{webgpu:n,available:!1,reason:`requestDevice refused: ${J(r)}`}}return{webgpu:n,available:!0}}function ne(e){return e.webgpu.available&&e.webgpu.built?"webgpu":"wasm"}function oe(e,t,n){let r=e.webgpu?.unrepresentable??[],o=r.length>0?`; adapter limits ${r.join(", ")}: the host reads them as missing`:"";return se(e,t,n)+o}function se(e,t,n){if(n==="webgpu"){let o=e.webgpu,a=o.features.includes("shader-f16")?" with shader-f16":"";return`WebGPU adapter present (${o.vendor} ${o.architecture})${a}: webgpu`}let r=e.wasm.simd?"with simd128":"without simd128";return e.webgpu===null?`no WebGPU adapter (${t.reason??"unavailable"}): wasm ${r}`:`WebGPU adapter present but unusable (${t.reason}): wasm ${r}`}async function O(){let e=H(),t=await re(),n={wasm:j(),threads:{sharedArrayBuffer:typeof SharedArrayBuffer<"u",crossOriginIsolated:globalThis.crossOriginIsolated===!0,hardwareConcurrency:E(e.hardwareConcurrency)},webgl:ee(),webgpu:t.webgpu,memory:{deviceMemoryGiB:E(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}}};n.tiers.webgl.available=n.webgl.available;let r=ne(n.tiers);return{...n,chosen:r,why:oe(n,t,r)}}function K(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(([,n])=>Number.isSafeInteger(n)))};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 ae="https://huggingface.co";function ie(e){return typeof location<"u"?new URL(String(e),location.href):new URL(String(e))}var le="https://cdn.jsdelivr.net/npm/@caldrix-ai/caldrix@0.6.0/";function I(e){return e instanceof Error?e.message:String(e)}function D(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 ue(e,t){let n=B(e,t),r=null,o=()=>{r!==null&&URL.revokeObjectURL(r),r=null};try{return!n&&!t?{worker:new Worker(new URL("./worker.js",import.meta.url),{type:"module"}),revoke:o,literal:!0}:n?(r=URL.createObjectURL(new Blob([`import ${JSON.stringify(e.href)};`],{type:"text/javascript"})),{worker:new Worker(r,{type:"module"}),revoke:o}):{worker:new Worker(e,{type:"module"}),revoke:o}}catch(a){throw o(),a}}function de(e){return typeof location<"u"&&e.origin!==location.origin}function B(e,t){return de(e)&&(t||e.protocol==="http:"||e.protocol==="https:")}function G(e){return!e.chosen&&!B(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}${B(e.url,e.chosen)?" (through a same-origin shim)":""}`}var R=class{constructor(t){this.reason=t}reason};function ce(e,t,n){return new Promise((r,o)=>{let a;try{a=ue(e.url,e.chosen)}catch(c){o(new R(`the page could not construct it: ${I(c)}`));return}let{worker:u,revoke:m}=a,l=()=>d(n()),d=(c,s=e.url)=>{if(m(),u.onmessage=null,u.onerror=null,t?.removeEventListener("abort",l),c===null)return r({worker:u,url:s});u.terminate(),o(c)};t?.addEventListener("abort",l,{once:!0}),u.onerror=c=>d(new R(c.message||"it did not load")),u.onmessage=c=>{let s=c.data;if(!L(s)||s.id!==0)return d(w(s));if(s.type==="ready")return a.literal?URL.canParse(s.url)?d(null,new URL(s.url)):d(w(s)):d(null);if(s.type==="error")return d(new R(s.message));d(w(s))}})}async function fe(e,t,n,r){let o=[];for(let d of e)try{let{worker:c,url:s}=await ce(d,n,r),b=o[0];return{worker:c,used:d,url:s,...b!==void 0?{failed:{what:G(b.t),reason:b.reason}}:{}}}catch(c){if(!(c instanceof R))throw c;o.push({t:d,reason:c.reason})}let[a,u]=o;if(a!==void 0&&u!==void 0){let d=u.t.url.origin;throw new i("backend",`${G(a.t)} did not start (${a.reason}), nor from the CDN fallback ${u.t.url.href} (${u.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 m=a,l=t&&m.t.source==="bundle"?"; the CDN fallback is off (assets.fallback is false)":"";throw new i("backend",`${G(m.t)} did not start: ${m.reason}${l}; caldrix.js runs inference in a Web Worker and has no main-thread mode`)}function pe(e,t,n){let r=n!==void 0?`, and with it the host .wasm from ${n}`:"";return`${e.what} did not start (${e.reason}); it was loaded from the CDN fallback ${t}${r}`}function be(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 X(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 me(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}async function z(e,t,n){let r=t.backend??"auto";if(r==="webgl")throw new i("backend","the webgl backend is not built");if(r!=="auto"&&r!=="webgpu"&&r!=="wasm")throw new i("request",`backend ${JSON.stringify(r)} is not auto, webgpu, wasm or webgl`);if(typeof e!="string"||e==="")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 o=D("worker",t.assets?.worker),a=D("hostWasm",t.assets?.hostWasm),u=t.assets?.fallback??!0;if(typeof u!="boolean")throw new i("request",`assets.fallback is true or false, not ${JSON.stringify(u)}`);let m=D("cdn",t.assets?.cdn)??new URL(le);m.pathname.endsWith("/")||(m.pathname+="/");let l=[o!==void 0?{url:o,chosen:!0,source:"page"}:{url:ie(new URL("./worker.js",import.meta.url)),chosen:!1,source:"bundle"}];u&&o===void 0&&l.push({url:new URL("dist/worker.js",m),chosen:!0,source:"cdn"});let d=u&&a===void 0?new URL("host/caldrix_web_bg.wasm",m).href:void 0,c=t.storage??"auto";if(c!=="auto"&&c!=="f32")throw new i("request",`storage ${JSON.stringify(c)} is not auto or f32`);if(r==="wasm"&&c==="f32")throw new i("request",`storage "f32" is the WebGPU tier's weight storage; backend "wasm" takes none`);if(t.hubBaseUrl!==void 0){let h=t.hubBaseUrl,x=!1;if(typeof h=="string")try{let M=new URL(h);x=M.protocol==="http:"||M.protocol==="https:"}catch{x=!1}if(!x)throw new i("request",`hubBaseUrl must be an absolute http(s) URL, not ${JSON.stringify(h).slice(0,80)}`)}let s=()=>new i("fetch",`${n?"checking":"loading"} ${e} was aborted`),b=t.signal;if(b?.aborted)throw s();let f=await O(),g,v;if(r==="auto"&&t.dtype==="bf16"&&f.chosen==="webgpu")g="wasm",v=`bf16 requested: wasm (the WebGPU tier runs the f32 plan only); ${f.why}`;else if(r==="auto")g=f.chosen,v=f.why;else if(r==="webgpu"){if(!f.tiers.webgpu.available)throw new i("backend",`backend "webgpu" was asked for, but the probe found no usable WebGPU adapter: ${f.tiers.webgpu.reason??f.why}`);g="webgpu",v=`backend "webgpu" asked for; ${f.why}`}else g="wasm",v=`backend "wasm" asked for; ${f.why}`;let p=f.webgpu?.unrepresentable??[];if(b?.aborted)throw s();let k=await fe(l,!u,b,s),{worker:U}=k,$=k.url,A={id:1,type:"load",model:e,...t.file!==void 0?{file:t.file}:{},probeJson:K(f),request:{backend:g,...t.dtype!==void 0?{dtype:t.dtype}:{},...t.maxContext!==void 0?{maxContext:t.maxContext}:{},...g==="webgpu"&&c==="f32"?{f16:!1}:{}},...r==="auto"&&g==="webgpu"?{auto:!0}:{},...n?{check:!0}:{},offline:t.offline??!1,hubBaseUrl:t.hubBaseUrl??ae,...a!==void 0?{hostWasm:a.href}:{},...d!==void 0?{cdnWasm:d}:{}},y={worker:U,used:k.used,workerUrl:$,caps:f,why:v,hostWasm:a,request:A,signal:b,aborted:s,fellBack:null,wasm:null,noted:h=>y.fellBack===null?h:new i(h.kind,`${h.message} (after a CDN fallback: ${y.fellBack})`,h.missing),host:h=>y.wasm!==null?!1:(y.wasm=h,h.failed!==void 0&&(y.fellBack=be({...h,failed:h.failed}),X(y.fellBack)),!0),workerError:h=>{let x=h.kind==="probe"&&p.length>0?`; the browser gave ${p.join(", ")}, which the host reads as missing`:"";return new i(h.kind,h.message+x)}};if(k.failed!==void 0&&(y.fellBack=pe(k.failed,$.href,a===void 0?d:void 0),X(y.fellBack)),b?.aborted)throw U.terminate(),y.noted(s());return y}function P(e){return e instanceof i?{ok:!1,kind:e.kind,message:e.message}:{ok:!1,kind:"backend",message:I(e)}}var q=class{static async load(t,n={}){let r=await z(t,n,!1),{worker:o,workerUrl:a,signal:u,request:m,hostWasm:l,caps:d}=r;return new Promise((c,s)=>{let b=()=>g(r.aborted()),f=()=>{o.onmessage=null,o.onerror=null,u?.removeEventListener("abort",b)},g=v=>{f(),o.terminate(),s(r.noted(v))};if(u?.addEventListener("abort",b,{once:!0}),u?.aborted)return g(r.aborted());o.onerror=v=>g(new i("backend",`the engine's worker from ${a.href} failed: ${v.message||"it did not start"}`)),o.onmessage=v=>{let p=v.data;if(!L(p)||p.id!==m.id)return g(w(p));switch(p.type){case"host":return r.host(p.hostWasm)?void 0:g(w(p));case"progress":try{n.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){g(k instanceof i?k:new i("request",`onProgress threw: ${I(k)}`))}return;case"loaded":{let k=r.wasm;if(k===null)return g(w(p));f();let U=r.used.source,$=l!==void 0||U==="page"&&k.failed===void 0?"page":k.failed!==void 0||U==="cdn"?"cdn":"bundle",A={worker:{source:U,url:a.href},hostWasm:{source:$,url:k.url}},y={...p.doctor,source:p.sources,why:p.fallback!==void 0?`${p.fallback}; ${d.why}`:r.why,assets:A};c(new T(o,y));return}case"error":return g(r.workerError(p));default:return g(w(p))}},o.postMessage(m)})}static async check(t,n={}){let r;try{let l=n??{};r=await z(t,{...l.backend!==void 0?{backend:l.backend}:{},...l.dtype!==void 0?{dtype:l.dtype}:{},...l.maxContext!==void 0?{maxContext:l.maxContext}:{},...l.storage!==void 0?{storage:l.storage}:{},...l.hubBaseUrl!==void 0?{hubBaseUrl:l.hubBaseUrl}:{},...l.file!==void 0?{file:l.file}:{},...l.signal!==void 0?{signal:l.signal}:{},...l.assets!==void 0?{assets:l.assets}:{}},!0)}catch(l){return P(l)}let{worker:o,workerUrl:a,signal:u,request:m}=r;return new Promise(l=>{let d=()=>s(r.aborted()),c=b=>{o.onmessage=null,o.onerror=null,u?.removeEventListener("abort",d),o.terminate(),l(b)},s=b=>c(P(r.noted(b)));if(u?.addEventListener("abort",d,{once:!0}),u?.aborted)return s(r.aborted());o.onerror=b=>s(new i("backend",`the engine's worker from ${a.href} failed: ${b.message||"it did not start"}`)),o.onmessage=b=>{let f=b.data;if(!L(f)||f.id!==m.id)return s(w(f));switch(f.type){case"host":return r.host(f.hostWasm)?void 0:s(w(f));case"progress":return;case"checked":return r.wasm===null?s(w(f)):c({ok:!0,backend:f.backend});case"error":return s(r.workerError(f));default:return s(w(f))}};try{o.postMessage(m)}catch(b){c(P(b))}})}},T=class{#r;#n;#l=2;#t=!1;#e=null;#i=!1;#o=null;constructor(t,n){this.#r=t,this.#n=n,t.onmessage=r=>this.#d(r.data),t.onerror=r=>this.#a(new i("backend",`the engine's worker failed: ${r.message||"it stopped"}`))}chat(t,n={}){return this.#s(),this.#u({messages:t},n)}complete(t,n={}){this.#s();let r;if(typeof t=="string")r={text:t};else if(typeof t=="object"&&t!==null){let o=t;if(o.text!==void 0&&o.ids!==void 0)throw new i("request","complete takes exactly one of text and ids, not both");if(!Array.isArray(o.ids))throw new i("request","complete takes exactly one of text and ids");r={ids:o.ids}}else throw new i("request","complete takes exactly one of text and ids");return this.#u(r,n)}doctor(){this.#s();let t=Object.fromEntries(Object.entries(this.#n.source).map(([a,u])=>[a,{...u}])),{worker:n,hostWasm:r}=this.#n.assets,o=this.#n.aliases.map(a=>({...a}));return{...this.#n,source:t,aliases:o,assets:{worker:{...n},hostWasm:{...r}}}}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(!L(t))return this.#a(w(t));let n=this.#o;if(n===null||t.id!==n.id)return this.#a(w(t));switch(t.type){case"delta":return n.sink(me(t.delta));case"error":return t.fatal!==void 0&&(this.#e=_(t.fatal)),n.sink(new i(t.kind,t.message));default:return this.#a(w(t))}}async*#u(t,n){if(this.#s(),this.#i)throw new i("request","a generation is already running on this engine");let{signal:r,...o}=n;if(r?.aborted){yield{text:"",done:!0,finishReason:"abort",usage:{promptTokens:0,completionTokens:0,cachedTokens:0}};return}this.#i=!0;let a=this.#l++,u=[],m=null;this.#o={id:a,sink:s=>{u.push(s),m?.()}};let l=()=>{!this.#t&&!this.#e&&this.#r.postMessage({id:a,type:"abort"})},d=!1,c=async()=>{for(;u.length===0;)await new Promise(s=>m=s);return m=null,u.shift()};try{for(this.#r.postMessage({id:a,type:"generate",prompt:t,options:o}),r?.addEventListener("abort",l,{once:!0});;){let s=await c();if(s instanceof i)throw d=!0,s;if(s.done&&(d=!0),yield s,s.done)return}}finally{if(r?.removeEventListener("abort",l),!d)try{for(l();!(this.#t||this.#e);){let s=await c();if(s instanceof i||s.done)break}}catch{}this.#o=null,this.#i=!1}}};export{q as Caldrix,i as CaldrixError,T as Engine,O 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/dist/worker.js CHANGED
@@ -1 +1 @@
1
- import{b as u,c as D,d as ne}from"./chunk-PZGTLL5H.js";var at="caldrix.js",Ce="x-caldrix-verified",ge="x-caldrix-absent";function lt(t){return[...new Uint8Array(t)].map(r=>r.toString(16).padStart(2,"0")).join("")}function ut(t){return t.trim().replace(/^W\//,"").replace(/^"|"$/g,"").toLowerCase()}function q(t){return t instanceof Error?t.message:String(t)}function Q(t,r){if(r===""||r==="."||r===".."||/[/\\?#]/.test(r)||/%[0-9a-fA-F]{2}/.test(r)||/[\u0000-\u001f\u007f]/.test(r))throw new u("validate",`${t} ${JSON.stringify(r)} is not a single safe path segment`);return encodeURIComponent(r)}function Te(t){return new u("backend",`${t} is not available; caldrix.js needs a secure context`)}function Fe(t,r,s,e){let n=t.split("/").map(f=>Q("repo",f)).join("/");return`${e.endsWith("/")?e.slice(0,-1):e}/${n}/resolve/${Q("revision",r)}/${Q("file",s)}`}function ct(t,r,s){let e=t.split("/").map(f=>Q("repo",f)).join("/"),n=s.endsWith("/")?s.slice(0,-1):s,a=r===null?"":`/revision/${r.split("/").map(f=>Q("revision",f)).join("%2F")}`;return`${n}/api/models/${e}${a}?blobs=true`}async function be(t){if(typeof caches>"u")throw Te("caches");try{return await caches.open(at)}catch(r){throw new u("backend",`opening the cache for ${t} failed: ${q(r)}`)}}async function pe(t){if(typeof crypto>"u"||!crypto.subtle)throw Te("crypto.subtle");return lt(await crypto.subtle.digest("SHA-256",t))}function Oe(t){return new u("fetch",`${t} is not in the cache and offline is set`,"offline-uncached")}async function De(t,r,s,e){let n=Fe(t,r,s,e.hubBaseUrl),a=await be(s),f;try{f=await a.match(n)}catch(A){throw new u("backend",`reading ${s} from the cache failed: ${q(A)}`)}let c=A=>new u("fetch",`fetching ${s} failed: HTTP 404${A}`,"not-found");if(f&&f.headers.get(ge)==="1"){if(e.rememberAbsent)throw c(" (remembered from an earlier load)");f=void 0}let h=!1;if(f){let A=new Uint8Array(await f.arrayBuffer());if(e.sha256===void 0)return{bytes:A,source:"cache",verified:f.headers.get(Ce)==="1",cached:!0};if(await pe(A)===e.sha256)return{bytes:A,source:"cache",verified:!0,cached:!0};h=!0;try{await a.delete(n)}catch{}}if(e.offline&&h)throw new u("fetch",`${s}: the cached copy fails the ${e.pinnedBy??"alias"}'s sha256 pin and offline is set`,"offline-uncached");if(e.offline)throw Oe(s);let y=(A,j)=>new u("fetch",`fetching ${s} ${A}`,j),b=()=>{if(e.signal?.aborted)throw y("was aborted")},k;try{k=await fetch(n,{signal:e.signal})}catch(A){throw y(e.signal?.aborted?"was aborted":`failed: ${q(A)}`)}if(k.status===404&&e.rememberAbsent){try{await a.put(n,new Response(null,{headers:{[ge]:"1"}}))}catch{}throw c("")}if(!k.ok||!k.body)throw y(`failed: HTTP ${k.status}`,k.status===404?"not-found":void 0);let P=k.headers.get("x-linked-etag"),w=k.headers.get("content-length"),i=w!==null&&/^\d+$/.test(w.trim())?Number(w):null,o=[],d=0,m=k.body.getReader();try{for(;;){let A;try{A=await m.read()}catch(j){throw y(e.signal?.aborted?"was aborted":`failed: ${q(j)}`)}if(A.done)break;if(o.push(A.value),d+=A.value.byteLength,e.bytes!==void 0&&d>e.bytes)throw new u("fetch",`${s}: the body passed its pinned size, ${e.bytes} bytes`);e.onProgress?.({file:s,loaded:d,total:i})}}finally{m.cancel().catch(()=>{})}if(b(),i!==null&&i!==d&&!k.headers.get("content-encoding"))throw y(`failed: Content-Length is ${i} but ${d} bytes arrived`);let p=new Uint8Array(d),g=0;for(let A of o)p.set(A,g),g+=A.byteLength;e.onProgress?.({file:s,loaded:d,total:i});let G=!1,T=null;if(e.sha256!==void 0){if(T=await pe(p),T!==e.sha256)throw new u("digest",`${s}: sha256 is ${T} but the ${e.pinnedBy??"alias"} pins ${e.sha256}`);G=!0}if(P!==null){let A=ut(P);if(T??=await pe(p),A!==T)throw new u("digest",`${s}: sha256 is ${T} but the Hub's X-Linked-Etag is ${A}`);G=!0}b();try{await a.put(n,new Response(p,{headers:{[Ce]:G?"1":"0"}}))}catch{return{bytes:p,source:"network",verified:G,cached:!1}}return{bytes:p,source:"network",verified:G,cached:!0}}async function He(t,r,s,e,n,a=0){if(!Number.isSafeInteger(e)||e<=0)throw new u("request",`a range of ${e} bytes of ${s} is not a positive integer`);if(!Number.isSafeInteger(a)||a<0||!Number.isSafeInteger(a+e))throw new u("request",`a range from byte ${a} of ${s} is not a non-negative integer`);let f=Fe(t,r,s,n.hubBaseUrl),c=await be(s),h;try{h=await c.match(f)}catch(o){throw new u("backend",`reading ${s} from the cache failed: ${q(o)}`)}if(h&&h.headers.get(ge)!=="1"){let o=new Uint8Array(await h.arrayBuffer());if(o.byteLength>=a+e)return o.slice(a,a+e)}if(n.offline)throw Oe(s);let y=a===0?`the first ${e} bytes`:`${e} bytes from byte ${a}`,b=o=>new u("fetch",`fetching ${y} of ${s} ${o}`),k;try{k=await fetch(f,{headers:{Range:`bytes=${a}-${a+e-1}`},signal:n.signal})}catch(o){throw b(n.signal?.aborted?"was aborted":`failed: ${q(o)}`)}if(k.status!==206&&k.status!==200||!k.body)throw b(`failed: HTTP ${k.status}`);if(k.status===200&&a!==0)throw k.body.cancel().catch(()=>{}),b("failed: the server answered HTTP 200 (the whole file) to a range past its head");let P=new Uint8Array(e),w=0,i=k.body.getReader();try{for(;;){let o;try{o=await i.read()}catch(m){throw b(n.signal?.aborted?"was aborted":`failed: ${q(m)}`)}if(o.done)break;let d=Math.min(o.value.byteLength,e-w);if(P.set(o.value.subarray(0,d),w),w+=o.value.byteLength,w>e||k.status===200&&w===e)break}}finally{i.cancel().catch(()=>{})}if(n.signal?.aborted)throw b("was aborted");if(w<e)throw b(`failed: the HTTP ${k.status} answer carried ${w} bytes, not the ${e} asked for`);if(k.status===206&&w>e)throw b(`failed: the HTTP 206 answer carried more than the ${e} bytes asked for`);return P}var Be=262144;async function _e(t,r,s){let e=ct(t,r,s.hubBaseUrl),n=`the Hub listing of ${t}`,a=await be(n);if(s.offline){let o;try{o=await a.match(e)}catch(d){throw new u("backend",`reading ${n} from the cache failed: ${q(d)}`)}if(!o)throw new u("fetch",`${n} is not in the cache and offline is set`,"offline-uncached");return{bytes:new Uint8Array(await o.arrayBuffer()),source:"cache"}}let f=o=>new u("fetch",`fetching ${n} ${o}`),c;try{c=await fetch(e,{signal:s.signal})}catch(o){throw f(s.signal?.aborted?"was aborted":`failed: ${q(o)}`)}let h=o=>{throw c.body?.cancel().catch(()=>{}),o},y=new URL(s.hubBaseUrl).origin;c.url&&new URL(c.url).origin!==y&&h(new u("fetch",`${n} was redirected off ${y} (to ${c.url})`)),c.status===401&&h(new u("fetch",`${t}: the Hub answered HTTP 401 for its listing: the repo does not exist, or it is private (the browser build sends no token; the caldrix CLI reads HF_TOKEN)`,"not-found")),c.status===404&&h(new u("fetch",`${t}${r?"@"+r:""}: no such repo or revision on the Hub (HTTP 404)`,"not-found")),(!c.ok||!c.body)&&h(f(`failed: HTTP ${c.status}`));let b=[],k=0,P=c.body.getReader();try{for(;;){let o;try{o=await P.read()}catch(d){throw f(s.signal?.aborted?"was aborted":`failed: ${q(d)}`)}if(o.done)break;if(k+=o.value.byteLength,k>Be)throw new u("validate",`${n} passed the 256 KiB (${Be}-byte) cap`);b.push(o.value)}}finally{P.cancel().catch(()=>{})}if(s.signal?.aborted)throw f("was aborted");let w=new Uint8Array(k),i=0;for(let o of b)w.set(o,i),i+=o.byteLength;try{await a.put(e,new Response(w))}catch{}return{bytes:w,source:"network"}}var me=null,ee=null;function we(t){return t instanceof Error?t.message:String(t)}async function ye(){return await import("../host/caldrix_web.js")}function ft(t){let r=globalThis.location?.href;return(r!==void 0?new URL(String(t),r):new URL(String(t))).href}async function Ne(t){let r;try{r=await fetch(t)}catch(s){throw new Error(we(s))}if(!r.ok)throw new Error(`${r.status} ${r.statusText}`.trim());return r}async function ke(t,r){if(me===null){let s=await ye(),e=t??ft(new URL("../host/caldrix_web_bg.wasm",import.meta.url)),n,a;try{n=await Ne(e),a={url:e}}catch(f){let c=we(f);if(r===void 0||r===e)throw new u("backend",`the browser host's .wasm could not be fetched from ${e} (${c}); serve host/ beside dist/ or set assets.hostWasm to where the page serves it`);try{n=await Ne(r)}catch(h){let y=new URL(r).origin;throw new u("backend",`the browser host's .wasm could not be fetched from ${e} (${c}) nor from the CDN fallback ${r} (${we(h)}); serve host/ beside dist/, set assets.hostWasm to where the page serves it, or allow ${y} in the page's Content-Security-Policy (connect-src ${y}, the .wasm is fetched)`)}a={url:r,failed:{url:e,reason:c}}}await s.default({module_or_path:n}),me=s,ee=a}return me}async function qe(t,r,s){let e=await ke(r,s);return new e.CaldrixWeb(t)}function Ie(){if(ee===null)throw new u("backend","the browser host is not open");return{...ee,...ee.failed!==void 0?{failed:{...ee.failed}}:{}}}async function ze(){return(await ke()).CaldrixWeb}async function je(t,r){let s=await ke(),e;try{let{instance:f}=await WebAssembly.instantiate(r,{env:{memory:s.CaldrixWeb.memory()}});e=f.exports}catch(f){throw new u("backend",`instantiating the emitted module failed: ${f instanceof Error?f.message:String(f)}`)}let{step:n,prefill:a}=e;if(typeof n!="function"||typeof a!="function")throw new u("backend","the emitted module does not export step and prefill");if(t.attach(n,a),t.argsPtr()===0)throw new u("backend","the host reports no model loaded after load")}var J={MAP_READ:1,COPY_SRC:4,COPY_DST:8,UNIFORM:64,STORAGE:128},dt=1,ht=["maxStorageBufferBindingSize","maxBufferSize","maxComputeWorkgroupsPerDimension","maxStorageBuffersPerShaderStage"],Me=(t,r)=>Number.isSafeInteger(t)&&t>=0&&t<r,Je=t=>Number.isSafeInteger(t)&&t>=0;function pt(t,r){let s=n=>new u("backend",`the host's WebGPU program is malformed: ${n}`),e;try{e=JSON.parse(t)}catch(n){throw s(n instanceof Error?n.message:String(n))}if(typeof e!="object"||e===null||!Array.isArray(e.kernels)||!Array.isArray(e.buffers))throw s("no kernels or buffers");if(e.kernels.forEach((n,a)=>{if(!(typeof n?.symbol=="string"&&typeof n.entry=="string"&&Array.isArray(n.buffers)&&n.buffers.every(c=>c==="f32"||c==="f16"||c==="u32")&&Je(n.scalars)))throw s(`kernel ${a}`)}),e.buffers.forEach((n,a)=>{if(!(typeof n?.word=="string"&&Je(n.bytes)&&(n.storage==="f32"||n.storage==="f16"||n.storage==="blocks")&&typeof n.placed=="boolean"))throw s(`buffer ${a}`)}),!Me(e.act,e.buffers.length))throw s("act is no buffer");if(!Me(e.logits,e.buffers.length))throw s("logits is no buffer");if(!Array.isArray(r)||r.length!==e.kernels.length||!r.every(n=>typeof n=="string"))throw s(`${e.kernels.length} kernels but ${Array.isArray(r)?r.length:"no"} WGSL sources`);return e}function gt(t,r){let s=[],e=f=>new u("backend",`the host's launch words are malformed: ${f}`),n=0,a=(f,c)=>{if(n+f>t.length)throw e(`launch ${s.length} ends inside its ${c}`);let h=t.subarray(n,n+f);return n+=f,h};for(;n<t.length;){let f=a(1,"kernel")[0],c=r.kernels[f];if(c===void 0)throw e(`launch ${s.length} names kernel ${f}; the program has ${r.kernels.length}`);let h=Array.from(a(a(1,"buffer count")[0],"buffers"));if(h.length!==c.buffers.length)throw e(`a launch of ${c.symbol} binds ${h.length} buffers; the kernel declares ${c.buffers.length}`);let y=h.find(w=>w>=r.buffers.length);if(y!==void 0)throw e(`a launch of ${c.symbol} binds buffer ${y}; the program has ${r.buffers.length}`);let[b,k]=a(2,"grid"),P=a(a(1,"uniform count")[0],"uniform").slice();if(P.length===0!=(c.scalars===0))throw e(`a launch of ${c.symbol} carries ${P.length} uniform words; the kernel declares ${c.scalars} scalars`);s.push({kernel:f,buffers:h,x:b,y:k,uniform:P})}return s}var K=t=>t instanceof Error?t.message:String(t),$e=class{constructor(r,s,e,n,a,f,c,h){this.adapter=r;this.description=s;this.host=e;this.device=n;this.program=a;this.pipelines=f;this.buffers=c;this.staging=h;n.lost.then(y=>{let b=`the WebGPU device was lost: ${y.message||y.reason||"no message"}`;!this.#e&&this.#t===null&&(this.#t=b),this.#r?.(b)})}adapter;description;host;device;program;pipelines;buffers;staging;#t=null;#e=!1;#r=null;#n=new Set;get failure(){return this.#t}get storage(){let r=s=>this.program.buffers.some(e=>e.storage===s);return r("f16")?"f16":r("blocks")?"blocks":"f32"}async run(r,s){if(this.#e)throw new u("request","the WebGPU runtime was destroyed (the engine was disposed)");if(this.failure!==null)throw this.#i(this.failure),ne(this.failure);let e;try{e=gt(r,this.program);let h=this.program.buffers[this.program.act];if(s.byteLength>h.bytes)throw new u("backend",`actIn is ${s.byteLength} bytes; the program's ${h.word} is ${h.bytes}`)}catch(h){throw this.#s(K(h),!1)}let n=this.device,a=[],f,c=new Promise((h,y)=>{this.#r=b=>y(new Error(b))});c.catch(()=>null);try{n.pushErrorScope("validation"),n.pushErrorScope("out-of-memory"),n.pushErrorScope("internal");let h=3,y=async()=>{let i=n.popErrorScope(),o=n.popErrorScope(),d=n.popErrorScope();h=0;let[m,p,g]=await Promise.all([i,o,d]);return p?`out of memory: ${p.message}`:g?`validation: ${g.message}`:m?`an internal error: ${m.message}`:null},b,k;try{s.byteLength>0&&n.queue.writeBuffer(this.buffers[this.program.act],0,s);let i=e.map(p=>{let g=p.buffers.map((G,T)=>({binding:T,resource:{buffer:this.buffers[G]}}));if(p.uniform.length>0){let G=n.createBuffer({size:p.uniform.byteLength,usage:J.UNIFORM|J.COPY_DST});a.push(G),this.#n.add(G),n.queue.writeBuffer(G,0,p.uniform),g.push({binding:p.buffers.length,resource:{buffer:G}})}return n.createBindGroup({layout:this.pipelines[p.kernel].getBindGroupLayout(0),entries:g})}),o=n.createCommandEncoder(),d=o.beginComputePass();e.forEach((p,g)=>{d.setPipeline(this.pipelines[p.kernel]),d.setBindGroup(0,i[g]),d.dispatchWorkgroups(p.x,p.y,1)}),d.end();let m=this.buffers[this.program.logits];o.copyBufferToBuffer(m,0,this.staging,0,m.size),n.queue.submit([o.finish()]),b=y(),k=this.staging.mapAsync(dt),b.catch(()=>null),k.catch(()=>null)}finally{for(;h-- >0;)n.popErrorScope().catch(()=>null)}if(this.failure!==null)throw new Error(this.failure);let P=await Promise.race([b,c]);if(this.#e)throw new u("request","the engine was disposed during a WebGPU call");if(P!==null)throw new Error(`the device reported ${P}`);try{await Promise.race([k,c])}catch(i){throw this.#e?new u("request","the engine was disposed during a WebGPU call"):new Error(`mapping the logits readback failed: ${K(i)}`)}if(this.#e)throw new u("request","the engine was disposed during a WebGPU call");let w=this.program.buffers[this.program.logits].bytes;f=new Uint8Array(this.staging.getMappedRange(0,this.staging.size).slice(0,w)),this.staging.unmap()}catch(h){throw h instanceof u&&h.kind==="request"?h:this.#s(K(h))}finally{this.#r=null;for(let h of a)this.#n.delete(h),this.#e||h.destroy()}this.host.writeLogits(f)}#s(r,s=!0){return this.#e?new u("request","the engine was disposed during a WebGPU call"):(s&&(this.#t??=r),this.#i(r)??new u("backend",r))}#i(r){try{this.host.failLaunch(r)}catch(s){let e=D(s);if(e.kind==="backend")return e}return null}destroy(){if(!this.#e){this.#e=!0,this.#r?.("the engine was disposed during a WebGPU call");for(let r of[...this.buffers,this.staging,...this.#n])try{r.destroy()}catch{}this.#n.clear();try{this.device.destroy()}catch{}}}};function bt(t,r){let s=t.limits[r];return typeof s=="number"?s:void 0}function mt(){return globalThis.navigator?.gpu}async function Ke(t,r,s=mt()){let e=y=>new u("backend",y);if(!s)throw e("the worker has no navigator.gpu, though the page's probe found a WebGPU adapter");let n;try{n=await s.requestAdapter()}catch(y){throw e(`the worker's requestAdapter failed: ${K(y)}`)}if(!n)throw e("the worker's requestAdapter returned null, though the page's probe found an adapter");if(r&&!n.features.has("shader-f16"))throw e("the host's precheck chose f16 weight storage, but the worker's adapter has no shader-f16");let a={};for(let y of ht){let b=bt(n,y),k=t.webgpu?.limits[y];if(k!==void 0&&(b===void 0||b<k))throw e(`the worker's adapter offers ${y} ${b??"none"}; the host prechecked against the probe's ${k}`);b!==void 0&&(a[y]=b)}let f;try{f=await n.requestDevice({requiredFeatures:r?["shader-f16"]:[],requiredLimits:a})}catch(y){throw e(`requestDevice refused${r?" shader-f16 with":""} the program's limits: ${K(y)}`)}let c=n.info??{};if(!n.info&&typeof n.requestAdapterInfo=="function")try{c=await n.requestAdapterInfo()}catch{c={}}let h=!1;return{device:f,f16:r,adapter:{vendor:c.vendor??"",architecture:c.architecture??""},description:c.description??"",destroy:()=>{if(!h){h=!0;try{f.destroy()}catch{}}}}}async function Ze(t,r,s){let e=c=>new u("backend",c),n=r.device,a=[],f=()=>{for(let c of a)c.destroy();r.destroy()};try{let c=pt(s.program,s.kernels),h=c.buffers.some(P=>P.storage==="f16");if(h!==r.f16)throw e(`the program stores weights ${h?"f16":"f32"}, but the device was opened for ${r.f16?"f16":"f32"} storage`);let y=[];for(let[P,w]of c.kernels.entries()){n.pushErrorScope("validation"),n.pushErrorScope("internal");let i=null,o=null;try{let p=n.createShaderModule({code:s.kernels[P],label:w.symbol});i=n.createComputePipeline({label:w.symbol,layout:"auto",compute:{module:p,entryPoint:w.entry}})}catch(p){o=p}let d=await n.popErrorScope(),m=await n.popErrorScope()??d;if(m)throw e(`kernel ${w.symbol}: creating its pipeline: ${m.message}`);if(i===null)throw e(`kernel ${w.symbol}: creating its pipeline: ${K(o)}`);y.push(i)}for(let[P,w]of c.buffers.entries()){n.pushErrorScope("validation"),n.pushErrorScope("out-of-memory");let i=null;try{let m=n.createBuffer({size:Math.max(4,Math.ceil(w.bytes/4)*4),usage:J.STORAGE|J.COPY_SRC|J.COPY_DST,label:w.word});if(a.push(m),w.placed){let p=t.bufferBytes(P);if(p.byteLength!==w.bytes)throw new u("backend",`buffer ${w.word}: bufferBytes gave ${p.byteLength} bytes; the buffer is ${w.bytes} at ${w.storage} storage`);if(p.byteLength%4!==0)throw new u("backend",`buffer ${w.word}: ${p.byteLength} bytes are not whole 4-byte words`);n.queue.writeBuffer(m,0,p)}}catch(m){i=m}let o=await n.popErrorScope(),d=await n.popErrorScope();if(i!==null)throw i instanceof u?i:D(i);if(o)throw e(`buffer ${w.word} (${w.bytes} bytes): out of memory: ${o.message}`);if(d)throw e(`buffer ${w.word} (${w.bytes} bytes): ${d.message}`)}let b=a[c.logits],k=n.createBuffer({size:b.size,usage:J.MAP_READ|J.COPY_DST,label:"logits readback"});return new $e({...r.adapter},r.description,t,n,c,y,a,k)}catch(c){throw f(),c}}var wt=4194304,Ae=["tokenizer.json","tokenizer.model","vocab.json","merges.txt","added_tokens.json","tokenizer_config.json","chat_template.jinja"],[Z,Pe,ie,We,Ee,Ye,Ve]=Ae;function se(t,r,s){return r!==null&&!(t===Pe||t===ie||t===We||t===Ee)&&r.siblings.some(n=>n.name===t)?r:s}var ve=(t,r)=>new u("validate",`${t}@${r} has no tokenizer.json, tokenizer.model, or vocab.json and merges.txt`),yt="the model has no tokenizer.json, and ";function Xe(t,r,s){let e=D(t);return e.kind!=="validate"||!e.message.startsWith(yt)?t:new u("validate",`${r}@${s}: ${e.message}`,e.missing)}var xe="adapter_model.safetensors",kt=["fit","request","probe"],Qe=8,et=64;function st(t){let r=Array.from(t);return r.length>et?`${r.slice(0,et).join("")}\u2026`:t}function tt(t){let r=t.slice(0,Qe).map(st).join(", ");return t.length>Qe?`${r}, \u2026`:r}function rt(){return new Promise(t=>{let r=new MessageChannel;r.port1.onmessage=()=>{r.port1.close(),t()},r.port2.postMessage(0)})}function $t(t,r,s){if(t!=="")return t;let e=[r,s].filter(n=>n!=="");return e.length>0?e.join(" "):"unnamed adapter"}async function nt(t,r,s){let e,n;try{e=s.kind==="prefill"?t.resolve("prefill",s.n):t.resolve("step",s.pos),n=t.actIn(s.kind==="prefill"?s.n:1)}catch(a){try{t.failLaunch(D(a).message)}catch{}throw a}await r.run(e,n)}async function vt(t,r,s,e){if(await nt(t,r,s),!!e.launched())for(t.finishStart();;){if(!await e.between())return;let n=t.beginNext();if("delta"in n){e.delta(JSON.parse(n.delta));return}if(await nt(t,r,n.launch),!e.launched())return;let a=JSON.parse(t.finishNext());if(e.delta(a),a.done)return}}function it(t,r){let s=null,e=null,n=!1,a=null,f=!1,c=[],h=i=>{let o={...i.doctor(),aliases:c.map(G=>({...G}))};if(e===null)return{...o,storage:null,adapter:null};let d=e.adapter,m=`webgpu (${$t(e.description,d.vendor,d.architecture)})`,p=`backend webgpu on ${o.device}`,g=o.line.startsWith(p)?`backend webgpu on ${m}${o.line.slice(p.length)}`:o.line;return{...o,device:m,line:g,storage:e.storage,adapter:{...d}}},y=new u("request","the engine was disposed during its load"),b=()=>{if(f)throw y};async function k(i){if(s!==null||n)throw new u("request","this engine already has a model loaded");n=!0;let o=null;try{o=await t.openHost(i.probeJson,i.hostWasm,i.cdnWasm),r({id:i.id,type:"host",hostWasm:t.wasmUsed()}),b();let d=await t.hostClass(),m=d.resolveModel(i.model);c=d.listAliases();let p=m.kind==="alias"?m:null,g=null;if(m.kind==="repo"){let l=await t.fetchListing(m.repo,m.revision,{hubBaseUrl:i.hubBaseUrl,offline:i.offline});b(),g=JSON.parse(d.parseListing(l.bytes))}if(i.file!==void 0&&g?.shape!=="gguf")throw new u("request",`file names a GGUF file in a repo that lists GGUF files; ${g!==null?`${g.id}@${g.sha}`:i.model} is not one`);let G=g!==null?g.id:m.repo,T;if(g!==null)T=g.sha;else if(p!==null)T=p.revision;else throw new u("backend","internal invariant broken: a repo id reached the file fetches without its Hub listing");let A={},j,oe={repo:G,revision:T,listing:g},ae=l=>({repo:l.id,revision:l.sha,listing:l}),le=(l,v)=>{let $=l.siblings.find(E=>E.name===v);if($===void 0)throw new u("validate",`${l.id}@${l.sha} has no ${v}`);return $.size},H=async(l,v,$,E,W)=>{let R;if(l.listing!==null){if($&&!l.listing.siblings.some(S=>S.name===v))return null;R={bytes:le(l.listing,v)}}try{let S=await t.fetchFile(l.repo,l.revision,v,{hubBaseUrl:i.hubBaseUrl,offline:i.offline,rememberAbsent:l.listing===null&&$,...R??{},...E??{},...W!==void 0?{pinnedBy:W}:{},onProgress:O=>r({id:i.id,type:"progress",file:O.file,loaded:O.loaded,total:O.total,repo:l.repo,revision:l.revision,...j!==void 0?{provenance:j}:{}})});return b(),A[v]={source:S.source,verified:S.verified},S.bytes}catch(S){if($&&S instanceof u&&S.missing==="not-found")return null;throw S}},V=async(l,v,$,E)=>await H(l,v,!1,$,E),Ge=(l,v,$)=>H(ae(l),v,$),Se={},_=o,X,te,re=null;if(p!==null&&p.format==="gguf")re={file:p.file,bytes:p.bytes,sha256:p.sha256,pinnedBy:"alias"};else if(g!==null&&g.shape==="gguf"){let l=g.gguf_files??[];if(i.file===void 0)throw new u("request",`${g.id}@${g.sha} lists GGUF files; name one with the file option: ${tt(l)}`);if(!l.includes(i.file))throw new u("request",`${st(i.file)} is not a GGUF file ${g.id}@${g.sha} offers; the choices are: ${tt(l)}`);let v=g.siblings.find($=>$.name===i.file);if(v===void 0)throw new u("validate",`${g.id}@${g.sha} has no ${i.file}`);re={file:i.file,bytes:v.size,...v.sha256?{sha256:v.sha256}:{},pinnedBy:"listing"}}if(re!==null){let l=re,v=(W,R=0)=>t.fetchRange(G,T,l.file,W,{hubBaseUrl:i.hubBaseUrl,offline:i.offline},R),E=await(async()=>{let W=await v(Math.min(wt,l.bytes));b();for(let O=d.ggufHeaderNeed(W,l.bytes);O!==0;O=d.ggufHeaderNeed(W,l.bytes))W=await v(O),b();let R=d.ggufRopeFreqsExtent(W,l.bytes);if(R===void 0||R[0]+R[1]<=W.byteLength)return{head:W};let S=await v(R[1],R[0]);return b(),{head:W,rope:S}})();X=W=>{if(E===null)throw new u("backend","internal invariant broken: a GGUF precheck after its head was released");return _.precheckGguf(E.head,l.bytes,W,E.rope)},te=async(W,R)=>{E=null;let S=await V(oe,l.file,{bytes:l.bytes,...l.sha256!==void 0?{sha256:l.sha256}:{}},l.pinnedBy);return R?_.loadWebGpuGguf(S,W):_.loadGguf(S,W)}}else{let l=null,v=g;if(g!==null&&g.shape==="adapter"){let C=`${g.id}@${g.sha}`,M=await Ge(g,"adapter_config.json",!1),x=JSON.parse(d.adapterBase(M,C)),F=U=>{let L=D(U);return L===y||L.message.startsWith(C)?L:new u(L.kind,`${C} names base ${x.repo}: ${L.message}`,L.missing)},B=g.siblings.some(U=>U.name===Z);try{let U=await t.fetchListing(x.repo,x.revision,{hubBaseUrl:i.hubBaseUrl,offline:i.offline});b(),v=JSON.parse(d.parseBaseListing(U.bytes,C,B))}catch(U){throw F(U)}l={listing:g,config:M,bytes:le(g,xe)}}let $=v,E=$!==null?ae($):oe,W=C=>$!==null?ae(se(C,l?.listing??null,$)):oe,R=await V(E,"config.json"),S=await H(E,"model.safetensors.index.json",!0),O=d.weightFiles(S);if($!==null){let C=se(Z,l?.listing??null,$);if(C.tokenizer===null)throw ve(C.id,C.sha);if(l!==null){let x=l.listing,F=new Set(["adapter_config.json",xe,...Ae.filter(L=>se(L,x,$)===x)]),U=["config.json",...S!==null?["model.safetensors.index.json"]:[],...O,...Ae.filter(L=>se(L,x,$)===$)].find(L=>F.has(L));if(U!==void 0)throw new u("validate",`${U} is required from both ${x.id}@${x.sha} (the adapter) and ${$.id}@${$.sha} (its base); a file is fetched from one repo only`)}let M=0;for(let x of O)M+=le($,x);Se={weightBytes:M,...l!==null?{adapterBytes:l.bytes,baseRepo:$.id,baseRevision:$.sha}:{}}}X=C=>_.precheck(R,S,C,l?.config),te=async(C,M)=>{let x=W(Z),F,B,U=null;if(x.listing!==null){let N=x.listing.tokenizer;if(N===null)throw ve(x.repo,x.revision);F=N,B=await V(x,N),N===ie&&(U=await V(x,We))}else if(F=Z,B=await H(x,Z,!0),B===null&&(F=Pe,B=await H(x,Pe,!0)),B===null&&(F=ie,B=await H(x,ie,!0),B!==null&&(U=await H(x,We,!0)),U===null&&(B=null)),B===null)throw ve(x.repo,x.revision);let L=F!==Z?await H(W(Ee),Ee,!0):null,fe=await H(W(Ye),Ye,!0),de=await H(W(Ve),Ve,!0);try{_.checkTokenizer(R,F,B,U,L,fe,de)}catch(N){throw Xe(N,x.repo,x.revision)}let he=[];for(let N of O)he.push(await V(E,N));let Ue=l!==null?[await Ge(l.listing,xe,!1)]:void 0,Le=l?.config;try{return M?_.loadWebGpu(R,F,B,U,L,fe,de,he,C,Le,Ue):_.load(R,F,B,U,L,fe,de,he,C,Le,Ue)}catch(N){throw Xe(N,x.repo,x.revision)}}}let I={...i.request,repo:G,revision:T,...g!==null&&g.id!==m.repo?{resolvedFrom:m.repo}:{},...Se},ue,z=null,ot=()=>{let l={...I,backend:"wasm"};delete l.f16,I=l},ce=l=>{let v=JSON.parse(l).provenance;j=typeof v=="string"?v:void 0},Re=l=>{ot();try{ce(X(JSON.stringify(I)))}catch(v){let $=D(v);throw new u($.kind,`${$.message} (backend "auto": WebGPU refused first: ${l.message})`)}ue=`webgpu refused: ${l.message}; wasm serves this load`};if(I.backend==="webgpu"){let l=null,v=null;try{l=X(JSON.stringify(I)),v=JSON.parse(l)}catch($){let E=D($);if(!(i.auto===!0&&kt.includes(E.kind)))throw E;Re(E)}if(v!==null){try{z=await t.openDevice(JSON.parse(i.probeJson),v.f16===!0)}catch($){let E=D($);if(i.auto!==!0)throw E;Re(E)}z!==null&&ce(l)}}else ce(X(JSON.stringify(I)));if(i.check===!0){z?.destroy(),z=null,b(),o=null,_.dispose(),r({id:i.id,type:"checked",backend:I.backend}),t.close();return}try{b();let l=JSON.stringify(I);if(z!==null){let v=await te(l,!0),$=z;z=null,e=await t.openRuntime(_,$,v)}else await t.instantiate(_,await te(l,!1))}finally{z?.destroy()}b(),s=o,r({id:i.id,type:"loaded",doctor:h(o),sources:A,...ue!==void 0?{fallback:ue}:{}})}catch(d){if(e?.destroy(),e=null,o!==null&&s!==o)try{o.dispose()}catch{}throw d}finally{n=!1}}async function P(i,o,d,m){let p;"messages"in i.prompt?p=o.startChatSplit(JSON.stringify(i.prompt.messages),m):"text"in i.prompt?p=o.startCompleteSplit(i.prompt.text,null,m):p=o.startCompleteSplit(null,Uint32Array.from(i.prompt.ids),m),await vt(o,d,p,{launched:()=>!f,between:async()=>(await rt(),!f),delta:g=>r({id:i.id,type:"delta",delta:g})})}async function w(i){if(a!==null)throw new u("request","a generation is already running on this engine");if(s===null)throw new u("request","no model is loaded");let o=s,d=JSON.stringify(i.options);if(e!==null){if(e.failure!==null)throw ne(e.failure);a=i.id;try{return await P(i,o,e,d)}catch(m){if(f)return;throw m}finally{a=null}}a=i.id;try{for(("messages"in i.prompt)?o.startChat(JSON.stringify(i.prompt.messages),d):("text"in i.prompt)?o.startComplete(i.prompt.text,null,d):o.startComplete(null,Uint32Array.from(i.prompt.ids),d);;){if(await rt(),f)return;let m=JSON.parse(o.next());if(r({id:i.id,type:"delta",delta:m}),m.done)return}}finally{a=null}}return async i=>{try{switch(i.type){case"load":return await k(i);case"generate":return await w(i);case"abort":a===i.id&&s?.abort();return;case"doctor":if(s===null)throw new u("request","no model is loaded");r({id:i.id,type:"result",value:h(s)});return;case"dispose":f=!0,e?.destroy(),e=null,s?.dispose(),s=null,r({id:i.id,type:"result",value:null}),t.close();return}}catch(o){if(f)return;let d=D(o),m=e?.failure??null;r({id:i.id,type:"error",kind:d.kind,message:d.message,...m!==null?{fatal:m}:{}})}}}var Y=globalThis,xt=it({openHost:qe,wasmUsed:Ie,hostClass:ze,instantiate:je,openDevice:(t,r)=>Ke(t,r),openRuntime:Ze,fetchFile:De,fetchListing:_e,fetchRange:He,close:()=>Y.close()},t=>Y.postMessage(t));Y.onmessage=t=>{xt(t.data)};ye().then(()=>Y.postMessage({id:0,type:"ready",url:Y.location.href}),t=>Y.postMessage({id:0,type:"error",kind:"backend",message:`the browser host's glue (host/caldrix_web.js, beside dist/worker.js) did not load (${t instanceof Error?t.message:String(t)})`}));
1
+ import{b as a,c as D,d as ne}from"./chunk-PZGTLL5H.js";var lt="caldrix.js",Be="x-caldrix-verified",ge="x-caldrix-absent";function ut(t){return[...new Uint8Array(t)].map(r=>r.toString(16).padStart(2,"0")).join("")}function ct(t){return t.trim().replace(/^W\//,"").replace(/^"|"$/g,"").toLowerCase()}function _(t){return t instanceof Error?t.message:String(t)}function V(t,r){if(r===""||r==="."||r===".."||/[/\\?#]/.test(r)||/%[0-9a-fA-F]{2}/.test(r)||/[\u0000-\u001f\u007f]/.test(r))throw new a("validate",`${t} ${JSON.stringify(r)} is not a single safe path segment`);return encodeURIComponent(r)}function Oe(t){return new a("backend",`${t} is not available; caldrix.js needs a secure context`)}function Fe(t,r,i,e){let n=t.split("/").map(u=>V("repo",u)).join("/");return`${e.endsWith("/")?e.slice(0,-1):e}/${n}/resolve/${V("revision",r)}/${V("file",i)}`}function ft(t,r,i){let e=t.split("/").map(u=>V("repo",u)).join("/"),n=i.endsWith("/")?i.slice(0,-1):i,o=r===null?"":`/revision/${r.split("/").map(u=>V("revision",u)).join("%2F")}`;return`${n}/api/models/${e}${o}?blobs=true`}async function be(t){if(typeof caches>"u")throw Oe("caches");try{return await caches.open(lt)}catch(r){throw new a("backend",`opening the cache for ${t} failed: ${_(r)}`)}}async function pe(t){if(typeof crypto>"u"||!crypto.subtle)throw Oe("crypto.subtle");return ut(await crypto.subtle.digest("SHA-256",t))}function De(t){return new a("fetch",`${t} is not in the cache and offline is set`,"offline-uncached")}async function He(t,r,i,e){let n=Fe(t,r,i,e.hubBaseUrl),o=await be(i),u;try{u=await o.match(n)}catch(x){throw new a("backend",`reading ${i} from the cache failed: ${_(x)}`)}let l=x=>new a("fetch",`fetching ${i} failed: HTTP 404${x}`,"not-found");if(u&&u.headers.get(ge)==="1"){if(e.rememberAbsent)throw l(" (remembered from an earlier load)");u=void 0}let g=!1;if(u){let x=new Uint8Array(await u.arrayBuffer());if(e.sha256===void 0)return{bytes:x,source:"cache",verified:u.headers.get(Be)==="1",cached:!0};if(await pe(x)===e.sha256)return{bytes:x,source:"cache",verified:!0,cached:!0};g=!0;try{await o.delete(n)}catch{}}if(e.offline&&g)throw new a("fetch",`${i}: the cached copy fails the ${e.pinnedBy??"alias"}'s sha256 pin and offline is set`,"offline-uncached");if(e.offline)throw De(i);let m=(x,j)=>new a("fetch",`fetching ${i} ${x}`,j),f=()=>{if(e.signal?.aborted)throw m("was aborted")},y;try{y=await fetch(n,{signal:e.signal})}catch(x){throw m(e.signal?.aborted?"was aborted":`failed: ${_(x)}`)}if(y.status===404&&e.rememberAbsent){try{await o.put(n,new Response(null,{headers:{[ge]:"1"}}))}catch{}throw l("")}if(!y.ok||!y.body)throw m(`failed: HTTP ${y.status}`,y.status===404?"not-found":void 0);let P=y.headers.get("x-linked-etag"),w=y.headers.get("content-length"),v=w!==null&&/^\d+$/.test(w.trim())?Number(w):null,s=[],c=0,b=y.body.getReader();try{for(;;){let x;try{x=await b.read()}catch(j){throw m(e.signal?.aborted?"was aborted":`failed: ${_(j)}`)}if(x.done)break;if(s.push(x.value),c+=x.value.byteLength,e.bytes!==void 0&&c>e.bytes)throw new a("fetch",`${i}: the body passed its pinned size, ${e.bytes} bytes`);e.onProgress?.({file:i,loaded:c,total:v})}}finally{b.cancel().catch(()=>{})}if(f(),v!==null&&v!==c&&!y.headers.get("content-encoding"))throw m(`failed: Content-Length is ${v} but ${c} bytes arrived`);let d=new Uint8Array(c),k=0;for(let x of s)d.set(x,k),k+=x.byteLength;e.onProgress?.({file:i,loaded:c,total:v});let h=!1,C=null;if(e.sha256!==void 0){if(C=await pe(d),C!==e.sha256)throw new a("digest",`${i}: sha256 is ${C} but the ${e.pinnedBy??"alias"} pins ${e.sha256}`);h=!0}if(P!==null){let x=ct(P);if(C??=await pe(d),x!==C)throw new a("digest",`${i}: sha256 is ${C} but the Hub's X-Linked-Etag is ${x}`);h=!0}f();try{await o.put(n,new Response(d,{headers:{[Be]:h?"1":"0"}}))}catch{return{bytes:d,source:"network",verified:h,cached:!1}}return{bytes:d,source:"network",verified:h,cached:!0}}async function qe(t,r,i,e,n,o=0){if(!Number.isSafeInteger(e)||e<=0)throw new a("request",`a range of ${e} bytes of ${i} is not a positive integer`);if(!Number.isSafeInteger(o)||o<0||!Number.isSafeInteger(o+e))throw new a("request",`a range from byte ${o} of ${i} is not a non-negative integer`);let u=Fe(t,r,i,n.hubBaseUrl),l=await be(i),g;try{g=await l.match(u)}catch(s){throw new a("backend",`reading ${i} from the cache failed: ${_(s)}`)}if(g&&g.headers.get(ge)!=="1"){let s=new Uint8Array(await g.arrayBuffer());if(s.byteLength>=o+e)return s.slice(o,o+e)}if(n.offline)throw De(i);let m=o===0?`the first ${e} bytes`:`${e} bytes from byte ${o}`,f=s=>new a("fetch",`fetching ${m} of ${i} ${s}`),y;try{y=await fetch(u,{headers:{Range:`bytes=${o}-${o+e-1}`},signal:n.signal})}catch(s){throw f(n.signal?.aborted?"was aborted":`failed: ${_(s)}`)}if(y.status!==206&&y.status!==200||!y.body)throw f(`failed: HTTP ${y.status}`);if(y.status===200&&o!==0)throw y.body.cancel().catch(()=>{}),f("failed: the server answered HTTP 200 (the whole file) to a range past its head");let P=new Uint8Array(e),w=0,v=y.body.getReader();try{for(;;){let s;try{s=await v.read()}catch(b){throw f(n.signal?.aborted?"was aborted":`failed: ${_(b)}`)}if(s.done)break;let c=Math.min(s.value.byteLength,e-w);if(P.set(s.value.subarray(0,c),w),w+=s.value.byteLength,w>e||y.status===200&&w===e)break}}finally{v.cancel().catch(()=>{})}if(n.signal?.aborted)throw f("was aborted");if(w<e)throw f(`failed: the HTTP ${y.status} answer carried ${w} bytes, not the ${e} asked for`);if(y.status===206&&w>e)throw f(`failed: the HTTP 206 answer carried more than the ${e} bytes asked for`);return P}var Te=262144;async function Ne(t,r,i){let e=ft(t,r,i.hubBaseUrl),n=`the Hub listing of ${t}`,o=await be(n);if(i.offline){let s;try{s=await o.match(e)}catch(c){throw new a("backend",`reading ${n} from the cache failed: ${_(c)}`)}if(!s)throw new a("fetch",`${n} is not in the cache and offline is set`,"offline-uncached");return{bytes:new Uint8Array(await s.arrayBuffer()),source:"cache"}}let u=s=>new a("fetch",`fetching ${n} ${s}`),l;try{l=await fetch(e,{signal:i.signal})}catch(s){throw u(i.signal?.aborted?"was aborted":`failed: ${_(s)}`)}let g=s=>{throw l.body?.cancel().catch(()=>{}),s},m=new URL(i.hubBaseUrl).origin;l.url&&new URL(l.url).origin!==m&&g(new a("fetch",`${n} was redirected off ${m} (to ${l.url})`)),l.status===401&&g(new a("fetch",`${t}: the Hub answered HTTP 401 for its listing: the repo does not exist, or it is private (the browser build sends no token; the caldrix CLI reads HF_TOKEN)`,"not-found")),l.status===404&&g(new a("fetch",`${t}${r?"@"+r:""}: no such repo or revision on the Hub (HTTP 404)`,"not-found")),(!l.ok||!l.body)&&g(u(`failed: HTTP ${l.status}`));let f=[],y=0,P=l.body.getReader();try{for(;;){let s;try{s=await P.read()}catch(c){throw u(i.signal?.aborted?"was aborted":`failed: ${_(c)}`)}if(s.done)break;if(y+=s.value.byteLength,y>Te)throw new a("validate",`${n} passed the 256 KiB (${Te}-byte) cap`);f.push(s.value)}}finally{P.cancel().catch(()=>{})}if(i.signal?.aborted)throw u("was aborted");let w=new Uint8Array(y),v=0;for(let s of f)w.set(s,v),v+=s.byteLength;try{await o.put(e,new Response(w))}catch{}return{bytes:w,source:"network"}}var me=null,X=null;function we(t){return t instanceof Error?t.message:String(t)}async function ye(){return await import("../host/caldrix_web.js")}function dt(t){let r=globalThis.location?.href;return(r!==void 0?new URL(String(t),r):new URL(String(t))).href}async function _e(t){let r;try{r=await fetch(t)}catch(i){throw new Error(we(i))}if(!r.ok)throw new Error(`${r.status} ${r.statusText}`.trim());return r}async function ke(t,r){if(me===null){let i=await ye(),e=t??dt(new URL("../host/caldrix_web_bg.wasm",import.meta.url)),n,o;try{n=await _e(e),o={url:e}}catch(u){let l=we(u);if(r===void 0||r===e)throw new a("backend",`the browser host's .wasm could not be fetched from ${e} (${l}); serve host/ beside dist/ or set assets.hostWasm to where the page serves it`);try{n=await _e(r)}catch(g){let m=new URL(r).origin;throw new a("backend",`the browser host's .wasm could not be fetched from ${e} (${l}) nor from the CDN fallback ${r} (${we(g)}); serve host/ beside dist/, set assets.hostWasm to where the page serves it, or allow ${m} in the page's Content-Security-Policy (connect-src ${m}, the .wasm is fetched)`)}o={url:r,failed:{url:e,reason:l}}}await i.default({module_or_path:n}),me=i,X=o}return me}async function Ie(t,r,i){let e=await ke(r,i);return new e.CaldrixWeb(t)}function ze(){if(X===null)throw new a("backend","the browser host is not open");return{...X,...X.failed!==void 0?{failed:{...X.failed}}:{}}}async function je(){return(await ke()).CaldrixWeb}async function Me(t,r){let i=await ke(),e;try{let{instance:u}=await WebAssembly.instantiate(r,{env:{memory:i.CaldrixWeb.memory()}});e=u.exports}catch(u){throw new a("backend",`instantiating the emitted module failed: ${u instanceof Error?u.message:String(u)}`)}let{step:n,prefill:o}=e;if(typeof n!="function"||typeof o!="function")throw new a("backend","the emitted module does not export step and prefill");if(t.attach(n,o),t.argsPtr()===0)throw new a("backend","the host reports no model loaded after load")}var z={MAP_READ:1,COPY_SRC:4,COPY_DST:8,UNIFORM:64,STORAGE:128},ht=1,pt=["maxStorageBufferBindingSize","maxBufferSize","maxComputeWorkgroupsPerDimension","maxStorageBuffersPerShaderStage"],Je=(t,r)=>Number.isSafeInteger(t)&&t>=0&&t<r,Ke=t=>Number.isSafeInteger(t)&&t>=0;function gt(t,r){let i=n=>new a("backend",`the host's WebGPU program is malformed: ${n}`),e;try{e=JSON.parse(t)}catch(n){throw i(n instanceof Error?n.message:String(n))}if(typeof e!="object"||e===null||!Array.isArray(e.kernels)||!Array.isArray(e.buffers))throw i("no kernels or buffers");if(e.kernels.forEach((n,o)=>{if(!(typeof n?.symbol=="string"&&typeof n.entry=="string"&&Array.isArray(n.buffers)&&n.buffers.every(l=>l==="f32"||l==="f16"||l==="u32")&&Ke(n.scalars)))throw i(`kernel ${o}`)}),e.buffers.forEach((n,o)=>{if(!(typeof n?.word=="string"&&Ke(n.bytes)&&(n.storage==="f32"||n.storage==="f16"||n.storage==="blocks")&&typeof n.placed=="boolean"))throw i(`buffer ${o}`)}),!Je(e.act,e.buffers.length))throw i("act is no buffer");if(!Je(e.logits,e.buffers.length))throw i("logits is no buffer");if(!Array.isArray(r)||r.length!==e.kernels.length||!r.every(n=>typeof n=="string"))throw i(`${e.kernels.length} kernels but ${Array.isArray(r)?r.length:"no"} WGSL sources`);return e}function bt(t,r){let i=[],e=u=>new a("backend",`the host's launch words are malformed: ${u}`),n=0,o=(u,l)=>{if(n+u>t.length)throw e(`launch ${i.length} ends inside its ${l}`);let g=t.subarray(n,n+u);return n+=u,g};for(;n<t.length;){let u=o(1,"kernel")[0],l=r.kernels[u];if(l===void 0)throw e(`launch ${i.length} names kernel ${u}; the program has ${r.kernels.length}`);let g=Array.from(o(o(1,"buffer count")[0],"buffers"));if(g.length!==l.buffers.length)throw e(`a launch of ${l.symbol} binds ${g.length} buffers; the kernel declares ${l.buffers.length}`);let m=g.find(w=>w>=r.buffers.length);if(m!==void 0)throw e(`a launch of ${l.symbol} binds buffer ${m}; the program has ${r.buffers.length}`);let[f,y]=o(2,"grid"),P=o(o(1,"uniform count")[0],"uniform").slice();if(P.length===0!=(l.scalars===0))throw e(`a launch of ${l.symbol} carries ${P.length} uniform words; the kernel declares ${l.scalars} scalars`);i.push({kernel:u,buffers:g,x:f,y,uniform:P})}return i}var J=t=>t instanceof Error?t.message:String(t),$e=class{constructor(r,i,e,n,o,u,l,g){this.adapter=r;this.description=i;this.host=e;this.device=n;this.program=o;this.pipelines=u;this.buffers=l;this.staging=g;n.lost.then(m=>{let f=`the WebGPU device was lost: ${m.message||m.reason||"no message"}`;!this.#e&&this.#t===null&&(this.#t=f),this.#r?.(f)})}adapter;description;host;device;program;pipelines;buffers;staging;#t=null;#e=!1;#r=null;#n=new Set;get failure(){return this.#t}get storage(){let r=i=>this.program.buffers.some(e=>e.storage===i);return r("f16")?"f16":r("blocks")?"blocks":"f32"}async run(r,i){if(this.#e)throw new a("request","the WebGPU runtime was destroyed (the engine was disposed)");if(this.failure!==null)throw this.#i(this.failure),ne(this.failure);let e;try{e=bt(r,this.program);let g=this.program.buffers[this.program.act];if(i.byteLength>g.bytes)throw new a("backend",`actIn is ${i.byteLength} bytes; the program's ${g.word} is ${g.bytes}`)}catch(g){throw this.#s(J(g),!1)}let n=this.device,o=[],u,l=new Promise((g,m)=>{this.#r=f=>m(new Error(f))});l.catch(()=>null);try{n.pushErrorScope("validation"),n.pushErrorScope("out-of-memory"),n.pushErrorScope("internal");let g=3,m=async()=>{let v=n.popErrorScope(),s=n.popErrorScope(),c=n.popErrorScope();g=0;let[b,d,k]=await Promise.all([v,s,c]);return d?`out of memory: ${d.message}`:k?`validation: ${k.message}`:b?`an internal error: ${b.message}`:null},f,y;try{i.byteLength>0&&n.queue.writeBuffer(this.buffers[this.program.act],0,i);let v=e.map(d=>{let k=d.buffers.map((h,C)=>({binding:C,resource:{buffer:this.buffers[h]}}));if(d.uniform.length>0){let h=n.createBuffer({size:d.uniform.byteLength,usage:z.UNIFORM|z.COPY_DST});o.push(h),this.#n.add(h),n.queue.writeBuffer(h,0,d.uniform),k.push({binding:d.buffers.length,resource:{buffer:h}})}return n.createBindGroup({layout:this.pipelines[d.kernel].getBindGroupLayout(0),entries:k})}),s=n.createCommandEncoder(),c=s.beginComputePass();e.forEach((d,k)=>{c.setPipeline(this.pipelines[d.kernel]),c.setBindGroup(0,v[k]),c.dispatchWorkgroups(d.x,d.y,1)}),c.end();let b=this.buffers[this.program.logits];s.copyBufferToBuffer(b,0,this.staging,0,b.size),n.queue.submit([s.finish()]),f=m(),y=this.staging.mapAsync(ht),f.catch(()=>null),y.catch(()=>null)}finally{for(;g-- >0;)n.popErrorScope().catch(()=>null)}if(this.failure!==null)throw new Error(this.failure);let P=await Promise.race([f,l]);if(this.#e)throw new a("request","the engine was disposed during a WebGPU call");if(P!==null)throw new Error(`the device reported ${P}`);try{await Promise.race([y,l])}catch(v){throw this.#e?new a("request","the engine was disposed during a WebGPU call"):new Error(`mapping the logits readback failed: ${J(v)}`)}if(this.#e)throw new a("request","the engine was disposed during a WebGPU call");let w=this.program.buffers[this.program.logits].bytes;u=new Uint8Array(this.staging.getMappedRange(0,this.staging.size).slice(0,w)),this.staging.unmap()}catch(g){throw g instanceof a&&g.kind==="request"?g:this.#s(J(g))}finally{this.#r=null;for(let g of o)this.#n.delete(g),this.#e||g.destroy()}this.host.writeLogits(u)}#s(r,i=!0){return this.#e?new a("request","the engine was disposed during a WebGPU call"):(i&&(this.#t??=r),this.#i(r)??new a("backend",r))}#i(r){try{this.host.failLaunch(r)}catch(i){let e=D(i);if(e.kind==="backend")return e}return null}destroy(){if(!this.#e){this.#e=!0,this.#r?.("the engine was disposed during a WebGPU call");for(let r of[...this.buffers,this.staging,...this.#n])try{r.destroy()}catch{}this.#n.clear();try{this.device.destroy()}catch{}}}};function mt(t,r){let i=t.limits[r];return typeof i=="number"?i:void 0}function wt(){return globalThis.navigator?.gpu}async function Ze(t,r,i=wt()){let e=m=>new a("backend",m);if(!i)throw e("the worker has no navigator.gpu, though the page's probe found a WebGPU adapter");let n;try{n=await i.requestAdapter()}catch(m){throw e(`the worker's requestAdapter failed: ${J(m)}`)}if(!n)throw e("the worker's requestAdapter returned null, though the page's probe found an adapter");if(r&&!n.features.has("shader-f16"))throw e("the host's precheck chose f16 weight storage, but the worker's adapter has no shader-f16");let o={};for(let m of pt){let f=mt(n,m),y=t.webgpu?.limits[m];if(y!==void 0&&(f===void 0||f<y))throw e(`the worker's adapter offers ${m} ${f??"none"}; the host prechecked against the probe's ${y}`);f!==void 0&&(o[m]=f)}let u;try{u=await n.requestDevice({requiredFeatures:r?["shader-f16"]:[],requiredLimits:o})}catch(m){throw e(`requestDevice refused${r?" shader-f16 with":""} the program's limits: ${J(m)}`)}let l=n.info??{};if(!n.info&&typeof n.requestAdapterInfo=="function")try{l=await n.requestAdapterInfo()}catch{l={}}let g=!1;return{device:u,f16:r,adapter:{vendor:l.vendor??"",architecture:l.architecture??""},description:l.description??"",destroy:()=>{if(!g){g=!0;try{u.destroy()}catch{}}}}}async function Ye(t,r,i){let e=l=>new a("backend",l),n=r.device,o=[],u=()=>{for(let l of o)l.destroy();r.destroy()};try{let l=gt(i.program,i.kernels),g=l.buffers.some(P=>P.storage==="f16");if(g!==r.f16)throw e(`the program stores weights ${g?"f16":"f32"}, but the device was opened for ${r.f16?"f16":"f32"} storage`);let m=[];for(let[P,w]of l.kernels.entries()){n.pushErrorScope("validation"),n.pushErrorScope("internal");let v=null,s=null;try{let d=n.createShaderModule({code:i.kernels[P],label:w.symbol});v=n.createComputePipeline({label:w.symbol,layout:"auto",compute:{module:d,entryPoint:w.entry}})}catch(d){s=d}let c=await n.popErrorScope(),b=await n.popErrorScope()??c;if(b)throw e(`kernel ${w.symbol}: creating its pipeline: ${b.message}`);if(v===null)throw e(`kernel ${w.symbol}: creating its pipeline: ${J(s)}`);m.push(v)}for(let[P,w]of l.buffers.entries()){n.pushErrorScope("validation"),n.pushErrorScope("out-of-memory");let v=null;try{let b=n.createBuffer({size:Math.max(4,Math.ceil(w.bytes/4)*4),usage:z.STORAGE|z.COPY_SRC|z.COPY_DST,label:w.word});if(o.push(b),w.placed){let d=t.bufferBytes(P);if(d.byteLength!==w.bytes)throw new a("backend",`buffer ${w.word}: bufferBytes gave ${d.byteLength} bytes; the buffer is ${w.bytes} at ${w.storage} storage`);if(d.byteLength%4!==0)throw new a("backend",`buffer ${w.word}: ${d.byteLength} bytes are not whole 4-byte words`);n.queue.writeBuffer(b,0,d)}}catch(b){v=b}let s=await n.popErrorScope(),c=await n.popErrorScope();if(v!==null)throw v instanceof a?v:D(v);if(s)throw e(`buffer ${w.word} (${w.bytes} bytes): out of memory: ${s.message}`);if(c)throw e(`buffer ${w.word} (${w.bytes} bytes): ${c.message}`)}let f=o[l.logits],y=n.createBuffer({size:f.size,usage:z.MAP_READ|z.COPY_DST,label:"logits readback"});return new $e({...r.adapter},r.description,t,n,l,m,o,y)}catch(l){throw u(),l}}var yt=4194304,Ae=["tokenizer.json","tokenizer.model","vocab.json","merges.txt","added_tokens.json","tokenizer_config.json","chat_template.jinja"],[K,Pe,ie,We,Ee,Ve,Xe]=Ae;function se(t,r,i){return r!==null&&!(t===Pe||t===ie||t===We||t===Ee)&&r.siblings.some(n=>n.name===t)?r:i}var ve=(t,r)=>new a("validate",`${t}@${r} has no tokenizer.json, tokenizer.model, or vocab.json and merges.txt`),kt="the model has no tokenizer.json, and ";function Qe(t,r,i){let e=D(t);return e.kind!=="validate"||!e.message.startsWith(kt)?t:new a("validate",`${r}@${i}: ${e.message}`,e.missing)}var xe="adapter_model.safetensors",$t=["fit","request","probe"],et=8,tt=64;function ot(t){let r=Array.from(t);return r.length>tt?`${r.slice(0,tt).join("")}\u2026`:t}function rt(t){let r=t.slice(0,et).map(ot).join(", ");return t.length>et?`${r}, \u2026`:r}function nt(){return new Promise(t=>{let r=new MessageChannel;r.port1.onmessage=()=>{r.port1.close(),t()},r.port2.postMessage(0)})}function vt(t,r,i){if(t!=="")return t;let e=[r,i].filter(n=>n!=="");return e.length>0?e.join(" "):"unnamed adapter"}async function st(t,r,i){let e,n;try{e=i.kind==="prefill"?t.resolve("prefill",i.n):t.resolve("step",i.pos),n=t.actIn(i.kind==="prefill"?i.n:1)}catch(o){try{t.failLaunch(D(o).message)}catch{}throw o}await r.run(e,n)}async function xt(t,r,i,e){if(await st(t,r,i),!!e.launched())for(t.finishStart();;){if(!await e.between())return;let n=t.beginNext();if("delta"in n){e.delta(JSON.parse(n.delta));return}if(await st(t,r,n.launch),!e.launched())return;let o=JSON.parse(t.finishNext());if(e.delta(o),o.done)return}}async function it(t,r,i,e,n){let o=m=>{let f={...i,backend:"wasm"};delete f.f16;let y;try{y=r(JSON.stringify(f))}catch(P){let w=D(P);throw new a(w.kind,`${w.message} (backend "auto": WebGPU refused first: ${m.message})`)}return{request:f,settled:y,device:null,fallback:`webgpu refused: ${m.message}; wasm serves this load`}};if(i.backend!=="webgpu")return{request:i,settled:r(JSON.stringify(i)),device:null};let u,l;try{u=r(JSON.stringify(i)),l=JSON.parse(u)}catch(m){let f=D(m);if(!(e&&$t.includes(f.kind)))throw f;return o(f)}let g;try{g=await t.openDevice(JSON.parse(n),l.f16===!0)}catch(m){let f=D(m);if(!e)throw f;return o(f)}return{request:i,settled:u,device:g}}function at(t,r){let i=null,e=null,n=!1,o=null,u=!1,l=[],g=s=>{let c={...s.doctor(),aliases:l.map(C=>({...C}))};if(e===null)return{...c,storage:null,adapter:null};let b=e.adapter,d=`webgpu (${vt(e.description,b.vendor,b.architecture)})`,k=`backend webgpu on ${c.device}`,h=c.line.startsWith(k)?`backend webgpu on ${d}${c.line.slice(k.length)}`:c.line;return{...c,device:d,line:h,storage:e.storage,adapter:{...b}}},m=new a("request","the engine was disposed during its load"),f=()=>{if(u)throw m};async function y(s){if(i!==null||n)throw new a("request","this engine already has a model loaded");n=!0;let c=null;try{c=await t.openHost(s.probeJson,s.hostWasm,s.cdnWasm),r({id:s.id,type:"host",hostWasm:t.wasmUsed()}),f();let b=await t.hostClass(),d=b.resolveModel(s.model);l=b.listAliases();let k=d.kind==="alias"?d:null,h=null;if(d.kind==="repo"){let p=await t.fetchListing(d.repo,d.revision,{hubBaseUrl:s.hubBaseUrl,offline:s.offline});f(),h=JSON.parse(b.parseListing(p.bytes))}if(s.file!==void 0&&h?.shape!=="gguf")throw new a("request",`file names a GGUF file in a repo that lists GGUF files; ${h!==null?`${h.id}@${h.sha}`:s.model} is not one`);let C=h!==null?h.id:d.repo,x;if(h!==null)x=h.sha;else if(k!==null)x=k.revision;else throw new a("backend","internal invariant broken: a repo id reached the file fetches without its Hub listing");let j={},oe,ae={repo:C,revision:x,listing:h},le=p=>({repo:p.id,revision:p.sha,listing:p}),ue=(p,W)=>{let $=p.siblings.find(T=>T.name===W);if($===void 0)throw new a("validate",`${p.id}@${p.sha} has no ${W}`);return $.size},H=async(p,W,$,T,E)=>{let S;if(p.listing!==null){if($&&!p.listing.siblings.some(R=>R.name===W))return null;S={bytes:ue(p.listing,W)}}try{let R=await t.fetchFile(p.repo,p.revision,W,{hubBaseUrl:s.hubBaseUrl,offline:s.offline,rememberAbsent:p.listing===null&&$,...S??{},...T??{},...E!==void 0?{pinnedBy:E}:{},onProgress:F=>r({id:s.id,type:"progress",file:F.file,loaded:F.loaded,total:F.total,repo:p.repo,revision:p.revision,...oe!==void 0?{provenance:oe}:{}})});return f(),j[W]={source:R.source,verified:R.verified},R.bytes}catch(R){if($&&R instanceof a&&R.missing==="not-found")return null;throw R}},Y=async(p,W,$,T)=>await H(p,W,!1,$,T),Re=(p,W,$)=>H(le(p),W,$),Se={},q=c,ce,Q,ee=null;if(k!==null&&k.format==="gguf")ee={file:k.file,bytes:k.bytes,sha256:k.sha256,pinnedBy:"alias"};else if(h!==null&&h.shape==="gguf"){let p=h.gguf_files??[];if(s.file===void 0)throw new a("request",`${h.id}@${h.sha} lists GGUF files; name one with the file option: ${rt(p)}`);if(!p.includes(s.file))throw new a("request",`${ot(s.file)} is not a GGUF file ${h.id}@${h.sha} offers; the choices are: ${rt(p)}`);let W=h.siblings.find($=>$.name===s.file);if(W===void 0)throw new a("validate",`${h.id}@${h.sha} has no ${s.file}`);ee={file:s.file,bytes:W.size,...W.sha256?{sha256:W.sha256}:{},pinnedBy:"listing"}}if(ee!==null){let p=ee,W=(E,S=0)=>t.fetchRange(C,x,p.file,E,{hubBaseUrl:s.hubBaseUrl,offline:s.offline},S),T=await(async()=>{let E=await W(Math.min(yt,p.bytes));f();for(let F=b.ggufHeaderNeed(E,p.bytes);F!==0;F=b.ggufHeaderNeed(E,p.bytes))E=await W(F),f();let S=b.ggufRopeFreqsExtent(E,p.bytes);if(S===void 0||S[0]+S[1]<=E.byteLength)return{head:E};let R=await W(S[1],S[0]);return f(),{head:E,rope:R}})();ce=E=>{if(T===null)throw new a("backend","internal invariant broken: a GGUF precheck after its head was released");return q.precheckGguf(T.head,p.bytes,E,T.rope)},Q=async(E,S)=>{T=null;let R=await Y(ae,p.file,{bytes:p.bytes,...p.sha256!==void 0?{sha256:p.sha256}:{}},p.pinnedBy);return S?q.loadWebGpuGguf(R,E):q.loadGguf(R,E)}}else{let p=null,W=h;if(h!==null&&h.shape==="adapter"){let L=`${h.id}@${h.sha}`,I=await Re(h,"adapter_config.json",!1),A=JSON.parse(b.adapterBase(I,L)),O=G=>{let U=D(G);return U===m||U.message.startsWith(L)?U:new a(U.kind,`${L} names base ${A.repo}: ${U.message}`,U.missing)},B=h.siblings.some(G=>G.name===K);try{let G=await t.fetchListing(A.repo,A.revision,{hubBaseUrl:s.hubBaseUrl,offline:s.offline});f(),W=JSON.parse(b.parseBaseListing(G.bytes,L,B))}catch(G){throw O(G)}p={listing:h,config:I,bytes:ue(h,xe)}}let $=W,T=$!==null?le($):ae,E=L=>$!==null?le(se(L,p?.listing??null,$)):ae,S=await Y(T,"config.json"),R=await H(T,"model.safetensors.index.json",!0),F=b.weightFiles(R);if($!==null){let L=se(K,p?.listing??null,$);if(L.tokenizer===null)throw ve(L.id,L.sha);if(p!==null){let A=p.listing,O=new Set(["adapter_config.json",xe,...Ae.filter(U=>se(U,A,$)===A)]),G=["config.json",...R!==null?["model.safetensors.index.json"]:[],...F,...Ae.filter(U=>se(U,A,$)===$)].find(U=>O.has(U));if(G!==void 0)throw new a("validate",`${G} is required from both ${A.id}@${A.sha} (the adapter) and ${$.id}@${$.sha} (its base); a file is fetched from one repo only`)}let I=0;for(let A of F)I+=ue($,A);Se={weightBytes:I,...p!==null?{adapterBytes:p.bytes,baseRepo:$.id,baseRevision:$.sha}:{}}}ce=L=>q.precheck(S,R,L,p?.config),Q=async(L,I)=>{let A=E(K),O,B,G=null;if(A.listing!==null){let N=A.listing.tokenizer;if(N===null)throw ve(A.repo,A.revision);O=N,B=await Y(A,N),N===ie&&(G=await Y(A,We))}else if(O=K,B=await H(A,K,!0),B===null&&(O=Pe,B=await H(A,Pe,!0)),B===null&&(O=ie,B=await H(A,ie,!0),B!==null&&(G=await H(A,We,!0)),G===null&&(B=null)),B===null)throw ve(A.repo,A.revision);let U=O!==K?await H(E(Ee),Ee,!0):null,fe=await H(E(Ve),Ve,!0),de=await H(E(Xe),Xe,!0);try{q.checkTokenizer(S,O,B,G,U,fe,de)}catch(N){throw Qe(N,A.repo,A.revision)}let he=[];for(let N of F)he.push(await Y(T,N));let Ue=p!==null?[await Re(p.listing,xe,!1)]:void 0,Le=p?.config;try{return I?q.loadWebGpu(S,O,B,G,U,fe,de,he,L,Le,Ue):q.load(S,O,B,G,U,fe,de,he,L,Le,Ue)}catch(N){throw Qe(N,A.repo,A.revision)}}}let te={...s.request,repo:C,revision:x,...h!==null&&h.id!==d.repo?{resolvedFrom:d.repo}:{},...Se},re=await it(t,ce,te,s.auto===!0,s.probeJson);te=re.request;let Ge=re.fallback,M=re.device,Ce=JSON.parse(re.settled).provenance;if(oe=typeof Ce=="string"?Ce:void 0,s.check===!0){M?.destroy(),M=null,f(),c=null,q.dispose(),r({id:s.id,type:"checked",backend:te.backend}),t.close();return}try{f();let p=JSON.stringify(te);if(M!==null){let W=await Q(p,!0),$=M;M=null,e=await t.openRuntime(q,$,W)}else await t.instantiate(q,await Q(p,!1))}finally{M?.destroy()}f(),i=c,r({id:s.id,type:"loaded",doctor:g(c),sources:j,...Ge!==void 0?{fallback:Ge}:{}})}catch(b){if(e?.destroy(),e=null,c!==null&&i!==c)try{c.dispose()}catch{}throw b}finally{n=!1}}async function P(s){if(i!==null||n)throw new a("request","this engine already has a model loaded");n=!0;let c=null;try{c=await t.openHost(s.probeJson,s.hostWasm,s.cdnWasm),r({id:s.id,type:"host",hostWasm:t.wasmUsed()}),f();let b=c,d={backend:s.backend,...s.maxContext!==void 0?{maxContext:s.maxContext}:{}},k=await it(t,C=>b.precheckFacts(s.facts,C),d,s.auto===!0,s.probeJson);k.device?.destroy(),f();let h=JSON.parse(k.settled);c=null,b.dispose(),r({id:s.id,type:"fitted",backend:k.request.backend,maxContext:typeof h.maxContext=="number"?h.maxContext:null,contextClamp:typeof h.contextClamp=="string"?h.contextClamp:null}),t.close()}catch(b){if(c!==null)try{c.dispose()}catch{}throw b}finally{n=!1}}async function w(s,c,b,d){let k;"messages"in s.prompt?k=c.startChatSplit(JSON.stringify(s.prompt.messages),d):"text"in s.prompt?k=c.startCompleteSplit(s.prompt.text,null,d):k=c.startCompleteSplit(null,Uint32Array.from(s.prompt.ids),d),await xt(c,b,k,{launched:()=>!u,between:async()=>(await nt(),!u),delta:h=>r({id:s.id,type:"delta",delta:h})})}async function v(s){if(o!==null)throw new a("request","a generation is already running on this engine");if(i===null)throw new a("request","no model is loaded");let c=i,b=JSON.stringify(s.options);if(e!==null){if(e.failure!==null)throw ne(e.failure);o=s.id;try{return await w(s,c,e,b)}catch(d){if(u)return;throw d}finally{o=null}}o=s.id;try{for(("messages"in s.prompt)?c.startChat(JSON.stringify(s.prompt.messages),b):("text"in s.prompt)?c.startComplete(s.prompt.text,null,b):c.startComplete(null,Uint32Array.from(s.prompt.ids),b);;){if(await nt(),u)return;let d=JSON.parse(c.next());if(r({id:s.id,type:"delta",delta:d}),d.done)return}}finally{o=null}}return async s=>{try{switch(s.type){case"load":return await y(s);case"fit":return await P(s);case"generate":return await v(s);case"abort":o===s.id&&i?.abort();return;case"doctor":if(i===null)throw new a("request","no model is loaded");r({id:s.id,type:"result",value:g(i)});return;case"dispose":u=!0,e?.destroy(),e=null,i?.dispose(),i=null,r({id:s.id,type:"result",value:null}),t.close();return}}catch(c){if(u)return;let b=D(c),d=e?.failure??null;r({id:s.id,type:"error",kind:b.kind,message:b.message,...d!==null?{fatal:d}:{}})}}}var Z=globalThis,At=at({openHost:Ie,wasmUsed:ze,hostClass:je,instantiate:Me,openDevice:(t,r)=>Ze(t,r),openRuntime:Ye,fetchFile:He,fetchListing:Ne,fetchRange:qe,close:()=>Z.close()},t=>Z.postMessage(t));Z.onmessage=t=>{At(t.data)};ye().then(()=>Z.postMessage({id:0,type:"ready",url:Z.location.href}),t=>Z.postMessage({id:0,type:"error",kind:"backend",message:`the browser host's glue (host/caldrix_web.js, beside dist/worker.js) did not load (${t instanceof Error?t.message:String(t)})`}));
@@ -160,6 +160,11 @@ export class CaldrixWeb {
160
160
  * `null`; `gguf_files` names the choosable GGUF files (`[]` unless the shape is `"gguf"`).
161
161
  */
162
162
  static parseListing(bytes: Uint8Array): string;
163
+ /**
164
+ * [`CaldrixWeb::precheck_gguf`] from a GGUF file's recorded facts (the nightly's JSON) in
165
+ * place of its head: no byte of the file is read. A facts admit is no load guarantee.
166
+ */
167
+ precheckFacts(facts_json: string, request_json: string): any;
163
168
  /**
164
169
  * [`CaldrixWeb::precheck`] over a GGUF file's head: `header` is enough of it for the header
165
170
  * to parse ([`CaldrixWeb::gguf_header_need`] answers 0), `file_bytes` the file's pinned size.
@@ -237,6 +242,7 @@ export interface InitOutput {
237
242
  readonly caldrixweb_parseBaseListing: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
238
243
  readonly caldrixweb_parseListing: (a: number, b: number, c: number) => void;
239
244
  readonly caldrixweb_precheck: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => void;
245
+ readonly caldrixweb_precheckFacts: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
240
246
  readonly caldrixweb_precheckGguf: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
241
247
  readonly caldrixweb_resolve: (a: number, b: number, c: number, d: number, e: number) => void;
242
248
  readonly caldrixweb_resolveAlias: (a: number, b: number, c: number) => void;
@@ -1 +1 @@
1
- class nt{__destroy_into_raw(){const e=this.__wbg_ptr;return this.__wbg_ptr=0,_t.unregister(this),e}free(){const e=this.__destroy_into_raw();t.__wbg_caldrixweb_free(e,0)}abort(){t.caldrixweb_abort(this.__wbg_ptr)}actIn(e){try{const o=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_actIn(o,this.__wbg_ptr,e);var n=i().getInt32(o+0,!0),_=i().getInt32(o+4,!0),r=i().getInt32(o+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static adapterBase(e,n){let _,r;try{const l=t.__wbindgen_add_to_stack_pointer(-16),v=p(e,t.__wbindgen_export),I=c,k=y(n,t.__wbindgen_export,t.__wbindgen_export2),m=c;t.caldrixweb_adapterBase(l,v,I,k,m);var o=i().getInt32(l+0,!0),s=i().getInt32(l+4,!0),d=i().getInt32(l+8,!0),w=i().getInt32(l+12,!0),u=o,b=s;if(w)throw u=0,b=0,g(d);return _=u,r=b,G(u,b)}finally{t.__wbindgen_add_to_stack_pointer(16),t.__wbindgen_export4(_,r,1)}}argsPtr(){return t.caldrixweb_argsPtr(this.__wbg_ptr)>>>0}attach(e,n){try{const o=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_attach(o,this.__wbg_ptr,h(e),h(n));var _=i().getInt32(o+0,!0),r=i().getInt32(o+4,!0);if(r)throw g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}beginNext(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_beginNext(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}bufferBytes(e){try{const o=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_bufferBytes(o,this.__wbg_ptr,e);var n=i().getInt32(o+0,!0),_=i().getInt32(o+4,!0),r=i().getInt32(o+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}checkTokenizer(e,n,_,r,o,s,d){try{const R=t.__wbindgen_add_to_stack_pointer(-16),C=p(e,t.__wbindgen_export),L=c,O=y(n,t.__wbindgen_export,t.__wbindgen_export2),U=c,E=p(_,t.__wbindgen_export),j=c;var w=f(r)?0:p(r,t.__wbindgen_export),u=c,b=f(o)?0:p(o,t.__wbindgen_export),l=c,v=f(s)?0:p(s,t.__wbindgen_export),I=c,k=f(d)?0:p(d,t.__wbindgen_export),m=c;t.caldrixweb_checkTokenizer(R,this.__wbg_ptr,C,L,O,U,E,j,w,u,b,l,v,I,k,m);var A=i().getInt32(R+0,!0),T=i().getInt32(R+4,!0);if(T)throw g(A)}finally{t.__wbindgen_add_to_stack_pointer(16)}}dispose(){t.caldrixweb_dispose(this.__wbg_ptr)}doctor(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_doctor(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}encode(e){try{const o=t.__wbindgen_add_to_stack_pointer(-16),s=y(e,t.__wbindgen_export,t.__wbindgen_export2),d=c;t.caldrixweb_encode(o,this.__wbg_ptr,s,d);var n=i().getInt32(o+0,!0),_=i().getInt32(o+4,!0),r=i().getInt32(o+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}failLaunch(e){try{const r=t.__wbindgen_add_to_stack_pointer(-16),o=y(e,t.__wbindgen_export,t.__wbindgen_export2),s=c;t.caldrixweb_failLaunch(r,this.__wbg_ptr,o,s);var n=i().getInt32(r+0,!0),_=i().getInt32(r+4,!0);if(_)throw g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}finishNext(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_finishNext(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}finishStart(){try{const _=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_finishStart(_,this.__wbg_ptr);var e=i().getInt32(_+0,!0),n=i().getInt32(_+4,!0);if(n)throw g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static ggufHeaderFirstRange(){return t.caldrixweb_ggufHeaderFirstRange()}static ggufHeaderNeed(e,n){try{const s=t.__wbindgen_add_to_stack_pointer(-16),d=p(e,t.__wbindgen_export),w=c;t.caldrixweb_ggufHeaderNeed(s,d,w,n);var _=i().getFloat64(s+0,!0),r=i().getInt32(s+8,!0),o=i().getInt32(s+12,!0);if(o)throw g(r);return _}finally{t.__wbindgen_add_to_stack_pointer(16)}}static ggufRopeFreqsExtent(e,n){try{const d=t.__wbindgen_add_to_stack_pointer(-16),w=p(e,t.__wbindgen_export),u=c;t.caldrixweb_ggufRopeFreqsExtent(d,w,u,n);var _=i().getInt32(d+0,!0),r=i().getInt32(d+4,!0),o=i().getInt32(d+8,!0),s=i().getInt32(d+12,!0);if(s)throw g(o);let b;return _!==0&&(b=ct(_,r).slice(),t.__wbindgen_export4(_,r*8,8)),b}finally{t.__wbindgen_add_to_stack_pointer(16)}}static listAliases(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_listAliases(r);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static listingCap(){return t.caldrixweb_listingCap()}loadGguf(e,n){try{const s=t.__wbindgen_add_to_stack_pointer(-16),d=p(e,t.__wbindgen_export),w=c,u=y(n,t.__wbindgen_export,t.__wbindgen_export2),b=c;t.caldrixweb_loadGguf(s,this.__wbg_ptr,d,w,u,b);var _=i().getInt32(s+0,!0),r=i().getInt32(s+4,!0),o=i().getInt32(s+8,!0);if(o)throw g(r);return g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}loadWebGpuGguf(e,n){try{const s=t.__wbindgen_add_to_stack_pointer(-16),d=p(e,t.__wbindgen_export),w=c,u=y(n,t.__wbindgen_export,t.__wbindgen_export2),b=c;t.caldrixweb_loadWebGpuGguf(s,this.__wbg_ptr,d,w,u,b);var _=i().getInt32(s+0,!0),r=i().getInt32(s+4,!0),o=i().getInt32(s+8,!0);if(o)throw g(r);return g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}loadWebGpu(e,n,_,r,o,s,d,w,u,b,l){try{const W=t.__wbindgen_add_to_stack_pointer(-16),q=p(e,t.__wbindgen_export),$=c,X=y(n,t.__wbindgen_export,t.__wbindgen_export2),Y=c,J=p(_,t.__wbindgen_export),K=c;var v=f(r)?0:p(r,t.__wbindgen_export),I=c,k=f(o)?0:p(o,t.__wbindgen_export),m=c,A=f(s)?0:p(s,t.__wbindgen_export),T=c,R=f(d)?0:p(d,t.__wbindgen_export),C=c;const Q=y(u,t.__wbindgen_export,t.__wbindgen_export2),Z=c;var L=f(b)?0:p(b,t.__wbindgen_export),O=c;t.caldrixweb_loadWebGpu(W,this.__wbg_ptr,q,$,X,Y,J,K,v,I,k,m,A,T,R,C,h(w),Q,Z,L,O,f(l)?0:h(l));var U=i().getInt32(W+0,!0),E=i().getInt32(W+4,!0),j=i().getInt32(W+8,!0);if(j)throw g(E);return g(U)}finally{t.__wbindgen_add_to_stack_pointer(16)}}load(e,n,_,r,o,s,d,w,u,b,l){try{const W=t.__wbindgen_add_to_stack_pointer(-16),q=p(e,t.__wbindgen_export),$=c,X=y(n,t.__wbindgen_export,t.__wbindgen_export2),Y=c,J=p(_,t.__wbindgen_export),K=c;var v=f(r)?0:p(r,t.__wbindgen_export),I=c,k=f(o)?0:p(o,t.__wbindgen_export),m=c,A=f(s)?0:p(s,t.__wbindgen_export),T=c,R=f(d)?0:p(d,t.__wbindgen_export),C=c;const Q=y(u,t.__wbindgen_export,t.__wbindgen_export2),Z=c;var L=f(b)?0:p(b,t.__wbindgen_export),O=c;t.caldrixweb_load(W,this.__wbg_ptr,q,$,X,Y,J,K,v,I,k,m,A,T,R,C,h(w),Q,Z,L,O,f(l)?0:h(l));var U=i().getInt32(W+0,!0),E=i().getInt32(W+4,!0),j=i().getInt32(W+8,!0);if(j)throw g(E);return g(U)}finally{t.__wbindgen_add_to_stack_pointer(16)}}logitsPtr(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_logitsPtr(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return e>>>0}finally{t.__wbindgen_add_to_stack_pointer(16)}}static memory(){const e=t.caldrixweb_memory();return g(e)}constructor(e){try{const o=t.__wbindgen_add_to_stack_pointer(-16),s=y(e,t.__wbindgen_export,t.__wbindgen_export2),d=c;t.caldrixweb_new(o,s,d);var n=i().getInt32(o+0,!0),_=i().getInt32(o+4,!0),r=i().getInt32(o+8,!0);if(r)throw g(_);return this.__wbg_ptr=n,_t.register(this,this.__wbg_ptr,this),this}finally{t.__wbindgen_add_to_stack_pointer(16)}}next(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_next(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static parseBaseListing(e,n,_){let r,o;try{const v=t.__wbindgen_add_to_stack_pointer(-16),I=p(e,t.__wbindgen_export),k=c,m=y(n,t.__wbindgen_export,t.__wbindgen_export2),A=c;t.caldrixweb_parseBaseListing(v,I,k,m,A,_);var s=i().getInt32(v+0,!0),d=i().getInt32(v+4,!0),w=i().getInt32(v+8,!0),u=i().getInt32(v+12,!0),b=s,l=d;if(u)throw b=0,l=0,g(w);return r=b,o=l,G(b,l)}finally{t.__wbindgen_add_to_stack_pointer(16),t.__wbindgen_export4(r,o,1)}}static parseListing(e){let n,_;try{const b=t.__wbindgen_add_to_stack_pointer(-16),l=p(e,t.__wbindgen_export),v=c;t.caldrixweb_parseListing(b,l,v);var r=i().getInt32(b+0,!0),o=i().getInt32(b+4,!0),s=i().getInt32(b+8,!0),d=i().getInt32(b+12,!0),w=r,u=o;if(d)throw w=0,u=0,g(s);return n=w,_=u,G(w,u)}finally{t.__wbindgen_add_to_stack_pointer(16),t.__wbindgen_export4(n,_,1)}}precheckGguf(e,n,_,r){try{const b=t.__wbindgen_add_to_stack_pointer(-16),l=p(e,t.__wbindgen_export),v=c,I=y(_,t.__wbindgen_export,t.__wbindgen_export2),k=c;var o=f(r)?0:p(r,t.__wbindgen_export),s=c;t.caldrixweb_precheckGguf(b,this.__wbg_ptr,l,v,n,I,k,o,s);var d=i().getInt32(b+0,!0),w=i().getInt32(b+4,!0),u=i().getInt32(b+8,!0);if(u)throw g(w);return g(d)}finally{t.__wbindgen_add_to_stack_pointer(16)}}precheck(e,n,_,r){try{const v=t.__wbindgen_add_to_stack_pointer(-16),I=p(e,t.__wbindgen_export),k=c;var o=f(n)?0:p(n,t.__wbindgen_export),s=c;const m=y(_,t.__wbindgen_export,t.__wbindgen_export2),A=c;var d=f(r)?0:p(r,t.__wbindgen_export),w=c;t.caldrixweb_precheck(v,this.__wbg_ptr,I,k,o,s,m,A,d,w);var u=i().getInt32(v+0,!0),b=i().getInt32(v+4,!0),l=i().getInt32(v+8,!0);if(l)throw g(b);return g(u)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static resolveAlias(e){try{const o=t.__wbindgen_add_to_stack_pointer(-16),s=y(e,t.__wbindgen_export,t.__wbindgen_export2),d=c;t.caldrixweb_resolveAlias(o,s,d);var n=i().getInt32(o+0,!0),_=i().getInt32(o+4,!0),r=i().getInt32(o+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static resolveModel(e){try{const o=t.__wbindgen_add_to_stack_pointer(-16),s=y(e,t.__wbindgen_export,t.__wbindgen_export2),d=c;t.caldrixweb_resolveModel(o,s,d);var n=i().getInt32(o+0,!0),_=i().getInt32(o+4,!0),r=i().getInt32(o+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}resolve(e,n){try{const s=t.__wbindgen_add_to_stack_pointer(-16),d=y(e,t.__wbindgen_export,t.__wbindgen_export2),w=c;t.caldrixweb_resolve(s,this.__wbg_ptr,d,w,n);var _=i().getInt32(s+0,!0),r=i().getInt32(s+4,!0),o=i().getInt32(s+8,!0);if(o)throw g(r);return g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}startChatSplit(e,n){try{const s=t.__wbindgen_add_to_stack_pointer(-16),d=y(e,t.__wbindgen_export,t.__wbindgen_export2),w=c,u=y(n,t.__wbindgen_export,t.__wbindgen_export2),b=c;t.caldrixweb_startChatSplit(s,this.__wbg_ptr,d,w,u,b);var _=i().getInt32(s+0,!0),r=i().getInt32(s+4,!0),o=i().getInt32(s+8,!0);if(o)throw g(r);return g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}startChat(e,n){try{const s=t.__wbindgen_add_to_stack_pointer(-16),d=y(e,t.__wbindgen_export,t.__wbindgen_export2),w=c,u=y(n,t.__wbindgen_export,t.__wbindgen_export2),b=c;t.caldrixweb_startChat(s,this.__wbg_ptr,d,w,u,b);var _=i().getInt32(s+0,!0),r=i().getInt32(s+4,!0),o=i().getInt32(s+8,!0);if(o)throw g(r);return _>>>0}finally{t.__wbindgen_add_to_stack_pointer(16)}}startCompleteSplit(e,n,_){try{const l=t.__wbindgen_add_to_stack_pointer(-16);var r=f(e)?0:y(e,t.__wbindgen_export,t.__wbindgen_export2),o=c,s=f(n)?0:at(n,t.__wbindgen_export),d=c;const v=y(_,t.__wbindgen_export,t.__wbindgen_export2),I=c;t.caldrixweb_startCompleteSplit(l,this.__wbg_ptr,r,o,s,d,v,I);var w=i().getInt32(l+0,!0),u=i().getInt32(l+4,!0),b=i().getInt32(l+8,!0);if(b)throw g(u);return g(w)}finally{t.__wbindgen_add_to_stack_pointer(16)}}startComplete(e,n,_){try{const l=t.__wbindgen_add_to_stack_pointer(-16);var r=f(e)?0:y(e,t.__wbindgen_export,t.__wbindgen_export2),o=c,s=f(n)?0:at(n,t.__wbindgen_export),d=c;const v=y(_,t.__wbindgen_export,t.__wbindgen_export2),I=c;t.caldrixweb_startComplete(l,this.__wbg_ptr,r,o,s,d,v,I);var w=i().getInt32(l+0,!0),u=i().getInt32(l+4,!0),b=i().getInt32(l+8,!0);if(b)throw g(u);return w>>>0}finally{t.__wbindgen_add_to_stack_pointer(16)}}vocab(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_vocab(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return e>>>0}finally{t.__wbindgen_add_to_stack_pointer(16)}}static weightFiles(e){try{const d=t.__wbindgen_add_to_stack_pointer(-16);var n=f(e)?0:p(e,t.__wbindgen_export),_=c;t.caldrixweb_weightFiles(d,n,_);var r=i().getInt32(d+0,!0),o=i().getInt32(d+4,!0),s=i().getInt32(d+8,!0);if(s)throw g(o);return g(r)}finally{t.__wbindgen_add_to_stack_pointer(16)}}writeLogits(e){try{const r=t.__wbindgen_add_to_stack_pointer(-16),o=p(e,t.__wbindgen_export),s=c;t.caldrixweb_writeLogits(r,this.__wbg_ptr,o,s);var n=i().getInt32(r+0,!0),_=i().getInt32(r+4,!0);if(_)throw g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}}Symbol.dispose&&(nt.prototype[Symbol.dispose]=nt.prototype.free);function rt(){return{__proto__:null,"./caldrix_web_bg.js":{__proto__:null,__wbg___wbindgen_memory_caa4a6165639c8b5:function(){const e=t.memory;return h(e)},__wbg___wbindgen_number_get_2e0e7dee9f701a71:function(e,n){const _=x(n),r=typeof _=="number"?_:void 0;i().setFloat64(e+8,f(r)?0:r,!0),i().setInt32(e+0,!f(r),!0)},__wbg___wbindgen_string_get_0380ccaa2f57f0d9:function(e,n){const _=x(n),r=typeof _=="string"?_:void 0;var o=f(r)?0:y(r,t.__wbindgen_export,t.__wbindgen_export2),s=c;i().setInt32(e+4,s,!0),i().setInt32(e+0,o,!0)},__wbg___wbindgen_throw_41e9ee4f547fc59a:function(e,n){throw new Error(G(e,n))},__wbg_call_1875a20c43a36133:function(){return H(function(e,n,_,r){const o=x(e).call(x(n),x(_),x(r));return h(o)},arguments)},__wbg_call_187d372bd5fdd4aa:function(){return H(function(e,n,_){const r=x(e).call(x(n),x(_));return h(r)},arguments)},__wbg_getRandomValues_a608c4436c19407a:function(){return H(function(e,n){globalThis.crypto.getRandomValues(tt(e,n))},arguments)},__wbg_get_unchecked_288889d017702237:function(e,n){const _=x(e)[n>>>0];return h(_)},__wbg_instanceof_Error_80a725f81f2e102d:function(e){let n;try{n=x(e)instanceof Error}catch{n=!1}return n},__wbg_instanceof_Uint8Array_828cef2aaacafc31:function(e){let n;try{n=x(e)instanceof Uint8Array}catch{n=!1}return n},__wbg_length_7f3c00c40364105e:function(e){return x(e).length},__wbg_length_d4bdea10311bd9cf:function(e){return x(e).length},__wbg_message_5f8387f0c32b90a7:function(e){const n=x(e).message;return h(n)},__wbg_new_617a8cdb8bb1130e:function(){const e=new Object;return h(e)},__wbg_new_ee2291f50781bf1d:function(){const e=new Array;return h(e)},__wbg_new_from_slice_37a3c3a194266cb8:function(e,n){const _=new Uint32Array(dt(e,n));return h(_)},__wbg_new_from_slice_9a868026ffa4208a:function(e,n){const _=new Uint8Array(tt(e,n));return h(_)},__wbg_prototypesetcall_bc27214492979395:function(e,n,_){Uint8Array.prototype.set.call(tt(e,n),x(_))},__wbg_push_2baf45db356cf468:function(e,n){return x(e).push(x(n))},__wbg_set_145a351398b48c65:function(){return H(function(e,n,_){return Reflect.set(x(e),x(n),x(_))},arguments)},__wbindgen_generic_0000000000000001:function(e){return h(e)},__wbindgen_generic_0000000000000002:function(e,n){const _=G(e,n);return h(_)},__wbindgen_object_drop_ref:function(e){g(e)}}}}const _t=typeof FinalizationRegistry>"u"?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(a=>t.__wbg_caldrixweb_free(a,1));function h(a){P===F.length&&F.push(F.length+1);const e=P;return P=F[e],F[e]=a,e}function st(a){a<1028||(F[a]=P,P=a)}function ct(a,e){return a=a>>>0,gt().subarray(a/8,a/8+e)}function dt(a,e){return a=a>>>0,it().subarray(a/4,a/4+e)}function tt(a,e){return a=a>>>0,S().subarray(a/1,a/1+e)}let M=null;function i(){return(M===null||M.buffer.detached===!0||M.buffer.detached===void 0&&M.buffer!==t.memory.buffer)&&(M=new DataView(t.memory.buffer)),M}let B=null;function gt(){return(B===null||B.byteLength===0)&&(B=new Float64Array(t.memory.buffer)),B}function G(a,e){return wt(a>>>0,e)}let D=null;function it(){return(D===null||D.byteLength===0)&&(D=new Uint32Array(t.memory.buffer)),D}let N=null;function S(){return(N===null||N.byteLength===0)&&(N=new Uint8Array(t.memory.buffer)),N}function x(a){return F[a]}function H(a,e){try{return a.apply(this,e)}catch(n){t.__wbindgen_export3(h(n))}}let F=new Array(1024).fill(void 0);F.push(void 0,null,!0,!1);let P=F.length;function f(a){return a==null}function at(a,e){const n=e(a.length*4,4)>>>0;return it().set(a,n/4),c=a.length,n}function p(a,e){const n=e(a.length*1,1)>>>0;return S().set(a,n/1),c=a.length,n}function y(a,e,n){if(n===void 0){const d=V.encode(a),w=e(d.length,1)>>>0;return S().subarray(w,w+d.length).set(d),c=d.length,w}let _=a.length,r=e(_,1)>>>0;const o=S();let s=0;for(;s<_;s++){const d=a.charCodeAt(s);if(d>127)break;o[r+s]=d}if(s!==_){s!==0&&(a=a.slice(s)),r=n(r,_,_=s+a.length*3,1)>>>0;const d=S().subarray(r+s,r+_),w=V.encodeInto(a,d);s+=w.written,r=n(r,_,s,1)>>>0}return c=s,r}function g(a){const e=x(a);return st(a),e}let z=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0});z.decode();const bt=2146435072;let et=0;function wt(a,e){return et+=e,et>=bt&&(z=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0}),z.decode(),et=e),z.decode(S().subarray(a,a+e))}const V=new TextEncoder;"encodeInto"in V||(V.encodeInto=function(a,e){const n=V.encode(a);return e.set(n),{read:a.length,written:n.length}});let c=0,pt,ut,t;function ot(a,e){return ut=a,t=a.exports,pt=e,M=null,B=null,D=null,N=null,t}async function lt(a,e){if(typeof Response=="function"&&a instanceof Response){if(!a.ok)throw new Error(`failed to fetch Wasm: ${a.status} ${a.statusText} fetching '${a.url}'`);if(typeof WebAssembly.instantiateStreaming=="function")try{return await WebAssembly.instantiateStreaming(a,e)}catch(r){if(n(a.type)&&a.headers.get("Content-Type")!=="application/wasm")console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",r);else throw r}const _=await a.arrayBuffer();return await WebAssembly.instantiate(_,e)}else{const _=await WebAssembly.instantiate(a,e);return _ instanceof WebAssembly.Instance?{instance:_,module:a}:_}function n(_){switch(_){case"basic":case"cors":case"default":return!0}return!1}}function ft(a){if(t!==void 0)return t;a!==void 0&&(Object.getPrototypeOf(a)===Object.prototype?{module:a}=a:console.warn("using deprecated parameters for `initSync()`; pass a single object instead"));const e=rt();a instanceof WebAssembly.Module||(a=new WebAssembly.Module(a));const n=new WebAssembly.Instance(a,e);return ot(n,a)}async function vt(a){if(t!==void 0)return t;a!==void 0&&(Object.getPrototypeOf(a)===Object.prototype?{module_or_path:a}=a:console.warn("using deprecated parameters for the initialization function; pass a single object instead")),a===void 0&&(a=new URL("caldrix_web_bg.wasm",import.meta.url));const e=rt();(typeof a=="string"||typeof Request=="function"&&a instanceof Request||typeof URL=="function"&&a instanceof URL)&&(a=fetch(a));const{instance:n,module:_}=await lt(await a,e);return ot(n,_)}export{nt as CaldrixWeb,vt as default,ft as initSync};
1
+ class nt{__destroy_into_raw(){const e=this.__wbg_ptr;return this.__wbg_ptr=0,_t.unregister(this),e}free(){const e=this.__destroy_into_raw();t.__wbg_caldrixweb_free(e,0)}abort(){t.caldrixweb_abort(this.__wbg_ptr)}actIn(e){try{const s=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_actIn(s,this.__wbg_ptr,e);var n=i().getInt32(s+0,!0),_=i().getInt32(s+4,!0),r=i().getInt32(s+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static adapterBase(e,n){let _,r;try{const l=t.__wbindgen_add_to_stack_pointer(-16),y=u(e,t.__wbindgen_export),I=c,k=f(n,t.__wbindgen_export,t.__wbindgen_export2),m=c;t.caldrixweb_adapterBase(l,y,I,k,m);var s=i().getInt32(l+0,!0),o=i().getInt32(l+4,!0),d=i().getInt32(l+8,!0),b=i().getInt32(l+12,!0),p=s,w=o;if(b)throw p=0,w=0,g(d);return _=p,r=w,G(p,w)}finally{t.__wbindgen_add_to_stack_pointer(16),t.__wbindgen_export4(_,r,1)}}argsPtr(){return t.caldrixweb_argsPtr(this.__wbg_ptr)>>>0}attach(e,n){try{const s=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_attach(s,this.__wbg_ptr,h(e),h(n));var _=i().getInt32(s+0,!0),r=i().getInt32(s+4,!0);if(r)throw g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}beginNext(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_beginNext(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}bufferBytes(e){try{const s=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_bufferBytes(s,this.__wbg_ptr,e);var n=i().getInt32(s+0,!0),_=i().getInt32(s+4,!0),r=i().getInt32(s+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}checkTokenizer(e,n,_,r,s,o,d){try{const R=t.__wbindgen_add_to_stack_pointer(-16),C=u(e,t.__wbindgen_export),L=c,O=f(n,t.__wbindgen_export,t.__wbindgen_export2),U=c,E=u(_,t.__wbindgen_export),j=c;var b=v(r)?0:u(r,t.__wbindgen_export),p=c,w=v(s)?0:u(s,t.__wbindgen_export),l=c,y=v(o)?0:u(o,t.__wbindgen_export),I=c,k=v(d)?0:u(d,t.__wbindgen_export),m=c;t.caldrixweb_checkTokenizer(R,this.__wbg_ptr,C,L,O,U,E,j,b,p,w,l,y,I,k,m);var A=i().getInt32(R+0,!0),T=i().getInt32(R+4,!0);if(T)throw g(A)}finally{t.__wbindgen_add_to_stack_pointer(16)}}dispose(){t.caldrixweb_dispose(this.__wbg_ptr)}doctor(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_doctor(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}encode(e){try{const s=t.__wbindgen_add_to_stack_pointer(-16),o=f(e,t.__wbindgen_export,t.__wbindgen_export2),d=c;t.caldrixweb_encode(s,this.__wbg_ptr,o,d);var n=i().getInt32(s+0,!0),_=i().getInt32(s+4,!0),r=i().getInt32(s+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}failLaunch(e){try{const r=t.__wbindgen_add_to_stack_pointer(-16),s=f(e,t.__wbindgen_export,t.__wbindgen_export2),o=c;t.caldrixweb_failLaunch(r,this.__wbg_ptr,s,o);var n=i().getInt32(r+0,!0),_=i().getInt32(r+4,!0);if(_)throw g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}finishNext(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_finishNext(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}finishStart(){try{const _=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_finishStart(_,this.__wbg_ptr);var e=i().getInt32(_+0,!0),n=i().getInt32(_+4,!0);if(n)throw g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static ggufHeaderFirstRange(){return t.caldrixweb_ggufHeaderFirstRange()}static ggufHeaderNeed(e,n){try{const o=t.__wbindgen_add_to_stack_pointer(-16),d=u(e,t.__wbindgen_export),b=c;t.caldrixweb_ggufHeaderNeed(o,d,b,n);var _=i().getFloat64(o+0,!0),r=i().getInt32(o+8,!0),s=i().getInt32(o+12,!0);if(s)throw g(r);return _}finally{t.__wbindgen_add_to_stack_pointer(16)}}static ggufRopeFreqsExtent(e,n){try{const d=t.__wbindgen_add_to_stack_pointer(-16),b=u(e,t.__wbindgen_export),p=c;t.caldrixweb_ggufRopeFreqsExtent(d,b,p,n);var _=i().getInt32(d+0,!0),r=i().getInt32(d+4,!0),s=i().getInt32(d+8,!0),o=i().getInt32(d+12,!0);if(o)throw g(s);let w;return _!==0&&(w=ct(_,r).slice(),t.__wbindgen_export4(_,r*8,8)),w}finally{t.__wbindgen_add_to_stack_pointer(16)}}static listAliases(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_listAliases(r);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static listingCap(){return t.caldrixweb_listingCap()}loadGguf(e,n){try{const o=t.__wbindgen_add_to_stack_pointer(-16),d=u(e,t.__wbindgen_export),b=c,p=f(n,t.__wbindgen_export,t.__wbindgen_export2),w=c;t.caldrixweb_loadGguf(o,this.__wbg_ptr,d,b,p,w);var _=i().getInt32(o+0,!0),r=i().getInt32(o+4,!0),s=i().getInt32(o+8,!0);if(s)throw g(r);return g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}loadWebGpuGguf(e,n){try{const o=t.__wbindgen_add_to_stack_pointer(-16),d=u(e,t.__wbindgen_export),b=c,p=f(n,t.__wbindgen_export,t.__wbindgen_export2),w=c;t.caldrixweb_loadWebGpuGguf(o,this.__wbg_ptr,d,b,p,w);var _=i().getInt32(o+0,!0),r=i().getInt32(o+4,!0),s=i().getInt32(o+8,!0);if(s)throw g(r);return g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}loadWebGpu(e,n,_,r,s,o,d,b,p,w,l){try{const W=t.__wbindgen_add_to_stack_pointer(-16),q=u(e,t.__wbindgen_export),$=c,X=f(n,t.__wbindgen_export,t.__wbindgen_export2),Y=c,J=u(_,t.__wbindgen_export),K=c;var y=v(r)?0:u(r,t.__wbindgen_export),I=c,k=v(s)?0:u(s,t.__wbindgen_export),m=c,A=v(o)?0:u(o,t.__wbindgen_export),T=c,R=v(d)?0:u(d,t.__wbindgen_export),C=c;const Q=f(p,t.__wbindgen_export,t.__wbindgen_export2),Z=c;var L=v(w)?0:u(w,t.__wbindgen_export),O=c;t.caldrixweb_loadWebGpu(W,this.__wbg_ptr,q,$,X,Y,J,K,y,I,k,m,A,T,R,C,h(b),Q,Z,L,O,v(l)?0:h(l));var U=i().getInt32(W+0,!0),E=i().getInt32(W+4,!0),j=i().getInt32(W+8,!0);if(j)throw g(E);return g(U)}finally{t.__wbindgen_add_to_stack_pointer(16)}}load(e,n,_,r,s,o,d,b,p,w,l){try{const W=t.__wbindgen_add_to_stack_pointer(-16),q=u(e,t.__wbindgen_export),$=c,X=f(n,t.__wbindgen_export,t.__wbindgen_export2),Y=c,J=u(_,t.__wbindgen_export),K=c;var y=v(r)?0:u(r,t.__wbindgen_export),I=c,k=v(s)?0:u(s,t.__wbindgen_export),m=c,A=v(o)?0:u(o,t.__wbindgen_export),T=c,R=v(d)?0:u(d,t.__wbindgen_export),C=c;const Q=f(p,t.__wbindgen_export,t.__wbindgen_export2),Z=c;var L=v(w)?0:u(w,t.__wbindgen_export),O=c;t.caldrixweb_load(W,this.__wbg_ptr,q,$,X,Y,J,K,y,I,k,m,A,T,R,C,h(b),Q,Z,L,O,v(l)?0:h(l));var U=i().getInt32(W+0,!0),E=i().getInt32(W+4,!0),j=i().getInt32(W+8,!0);if(j)throw g(E);return g(U)}finally{t.__wbindgen_add_to_stack_pointer(16)}}logitsPtr(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_logitsPtr(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return e>>>0}finally{t.__wbindgen_add_to_stack_pointer(16)}}static memory(){const e=t.caldrixweb_memory();return g(e)}constructor(e){try{const s=t.__wbindgen_add_to_stack_pointer(-16),o=f(e,t.__wbindgen_export,t.__wbindgen_export2),d=c;t.caldrixweb_new(s,o,d);var n=i().getInt32(s+0,!0),_=i().getInt32(s+4,!0),r=i().getInt32(s+8,!0);if(r)throw g(_);return this.__wbg_ptr=n,_t.register(this,this.__wbg_ptr,this),this}finally{t.__wbindgen_add_to_stack_pointer(16)}}next(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_next(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return g(e)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static parseBaseListing(e,n,_){let r,s;try{const y=t.__wbindgen_add_to_stack_pointer(-16),I=u(e,t.__wbindgen_export),k=c,m=f(n,t.__wbindgen_export,t.__wbindgen_export2),A=c;t.caldrixweb_parseBaseListing(y,I,k,m,A,_);var o=i().getInt32(y+0,!0),d=i().getInt32(y+4,!0),b=i().getInt32(y+8,!0),p=i().getInt32(y+12,!0),w=o,l=d;if(p)throw w=0,l=0,g(b);return r=w,s=l,G(w,l)}finally{t.__wbindgen_add_to_stack_pointer(16),t.__wbindgen_export4(r,s,1)}}static parseListing(e){let n,_;try{const w=t.__wbindgen_add_to_stack_pointer(-16),l=u(e,t.__wbindgen_export),y=c;t.caldrixweb_parseListing(w,l,y);var r=i().getInt32(w+0,!0),s=i().getInt32(w+4,!0),o=i().getInt32(w+8,!0),d=i().getInt32(w+12,!0),b=r,p=s;if(d)throw b=0,p=0,g(o);return n=b,_=p,G(b,p)}finally{t.__wbindgen_add_to_stack_pointer(16),t.__wbindgen_export4(n,_,1)}}precheckFacts(e,n){try{const o=t.__wbindgen_add_to_stack_pointer(-16),d=f(e,t.__wbindgen_export,t.__wbindgen_export2),b=c,p=f(n,t.__wbindgen_export,t.__wbindgen_export2),w=c;t.caldrixweb_precheckFacts(o,this.__wbg_ptr,d,b,p,w);var _=i().getInt32(o+0,!0),r=i().getInt32(o+4,!0),s=i().getInt32(o+8,!0);if(s)throw g(r);return g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}precheckGguf(e,n,_,r){try{const w=t.__wbindgen_add_to_stack_pointer(-16),l=u(e,t.__wbindgen_export),y=c,I=f(_,t.__wbindgen_export,t.__wbindgen_export2),k=c;var s=v(r)?0:u(r,t.__wbindgen_export),o=c;t.caldrixweb_precheckGguf(w,this.__wbg_ptr,l,y,n,I,k,s,o);var d=i().getInt32(w+0,!0),b=i().getInt32(w+4,!0),p=i().getInt32(w+8,!0);if(p)throw g(b);return g(d)}finally{t.__wbindgen_add_to_stack_pointer(16)}}precheck(e,n,_,r){try{const y=t.__wbindgen_add_to_stack_pointer(-16),I=u(e,t.__wbindgen_export),k=c;var s=v(n)?0:u(n,t.__wbindgen_export),o=c;const m=f(_,t.__wbindgen_export,t.__wbindgen_export2),A=c;var d=v(r)?0:u(r,t.__wbindgen_export),b=c;t.caldrixweb_precheck(y,this.__wbg_ptr,I,k,s,o,m,A,d,b);var p=i().getInt32(y+0,!0),w=i().getInt32(y+4,!0),l=i().getInt32(y+8,!0);if(l)throw g(w);return g(p)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static resolveAlias(e){try{const s=t.__wbindgen_add_to_stack_pointer(-16),o=f(e,t.__wbindgen_export,t.__wbindgen_export2),d=c;t.caldrixweb_resolveAlias(s,o,d);var n=i().getInt32(s+0,!0),_=i().getInt32(s+4,!0),r=i().getInt32(s+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}static resolveModel(e){try{const s=t.__wbindgen_add_to_stack_pointer(-16),o=f(e,t.__wbindgen_export,t.__wbindgen_export2),d=c;t.caldrixweb_resolveModel(s,o,d);var n=i().getInt32(s+0,!0),_=i().getInt32(s+4,!0),r=i().getInt32(s+8,!0);if(r)throw g(_);return g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}resolve(e,n){try{const o=t.__wbindgen_add_to_stack_pointer(-16),d=f(e,t.__wbindgen_export,t.__wbindgen_export2),b=c;t.caldrixweb_resolve(o,this.__wbg_ptr,d,b,n);var _=i().getInt32(o+0,!0),r=i().getInt32(o+4,!0),s=i().getInt32(o+8,!0);if(s)throw g(r);return g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}startChatSplit(e,n){try{const o=t.__wbindgen_add_to_stack_pointer(-16),d=f(e,t.__wbindgen_export,t.__wbindgen_export2),b=c,p=f(n,t.__wbindgen_export,t.__wbindgen_export2),w=c;t.caldrixweb_startChatSplit(o,this.__wbg_ptr,d,b,p,w);var _=i().getInt32(o+0,!0),r=i().getInt32(o+4,!0),s=i().getInt32(o+8,!0);if(s)throw g(r);return g(_)}finally{t.__wbindgen_add_to_stack_pointer(16)}}startChat(e,n){try{const o=t.__wbindgen_add_to_stack_pointer(-16),d=f(e,t.__wbindgen_export,t.__wbindgen_export2),b=c,p=f(n,t.__wbindgen_export,t.__wbindgen_export2),w=c;t.caldrixweb_startChat(o,this.__wbg_ptr,d,b,p,w);var _=i().getInt32(o+0,!0),r=i().getInt32(o+4,!0),s=i().getInt32(o+8,!0);if(s)throw g(r);return _>>>0}finally{t.__wbindgen_add_to_stack_pointer(16)}}startCompleteSplit(e,n,_){try{const l=t.__wbindgen_add_to_stack_pointer(-16);var r=v(e)?0:f(e,t.__wbindgen_export,t.__wbindgen_export2),s=c,o=v(n)?0:at(n,t.__wbindgen_export),d=c;const y=f(_,t.__wbindgen_export,t.__wbindgen_export2),I=c;t.caldrixweb_startCompleteSplit(l,this.__wbg_ptr,r,s,o,d,y,I);var b=i().getInt32(l+0,!0),p=i().getInt32(l+4,!0),w=i().getInt32(l+8,!0);if(w)throw g(p);return g(b)}finally{t.__wbindgen_add_to_stack_pointer(16)}}startComplete(e,n,_){try{const l=t.__wbindgen_add_to_stack_pointer(-16);var r=v(e)?0:f(e,t.__wbindgen_export,t.__wbindgen_export2),s=c,o=v(n)?0:at(n,t.__wbindgen_export),d=c;const y=f(_,t.__wbindgen_export,t.__wbindgen_export2),I=c;t.caldrixweb_startComplete(l,this.__wbg_ptr,r,s,o,d,y,I);var b=i().getInt32(l+0,!0),p=i().getInt32(l+4,!0),w=i().getInt32(l+8,!0);if(w)throw g(p);return b>>>0}finally{t.__wbindgen_add_to_stack_pointer(16)}}vocab(){try{const r=t.__wbindgen_add_to_stack_pointer(-16);t.caldrixweb_vocab(r,this.__wbg_ptr);var e=i().getInt32(r+0,!0),n=i().getInt32(r+4,!0),_=i().getInt32(r+8,!0);if(_)throw g(n);return e>>>0}finally{t.__wbindgen_add_to_stack_pointer(16)}}static weightFiles(e){try{const d=t.__wbindgen_add_to_stack_pointer(-16);var n=v(e)?0:u(e,t.__wbindgen_export),_=c;t.caldrixweb_weightFiles(d,n,_);var r=i().getInt32(d+0,!0),s=i().getInt32(d+4,!0),o=i().getInt32(d+8,!0);if(o)throw g(s);return g(r)}finally{t.__wbindgen_add_to_stack_pointer(16)}}writeLogits(e){try{const r=t.__wbindgen_add_to_stack_pointer(-16),s=u(e,t.__wbindgen_export),o=c;t.caldrixweb_writeLogits(r,this.__wbg_ptr,s,o);var n=i().getInt32(r+0,!0),_=i().getInt32(r+4,!0);if(_)throw g(n)}finally{t.__wbindgen_add_to_stack_pointer(16)}}}Symbol.dispose&&(nt.prototype[Symbol.dispose]=nt.prototype.free);function rt(){return{__proto__:null,"./caldrix_web_bg.js":{__proto__:null,__wbg___wbindgen_memory_caa4a6165639c8b5:function(){const e=t.memory;return h(e)},__wbg___wbindgen_number_get_2e0e7dee9f701a71:function(e,n){const _=x(n),r=typeof _=="number"?_:void 0;i().setFloat64(e+8,v(r)?0:r,!0),i().setInt32(e+0,!v(r),!0)},__wbg___wbindgen_string_get_0380ccaa2f57f0d9:function(e,n){const _=x(n),r=typeof _=="string"?_:void 0;var s=v(r)?0:f(r,t.__wbindgen_export,t.__wbindgen_export2),o=c;i().setInt32(e+4,o,!0),i().setInt32(e+0,s,!0)},__wbg___wbindgen_throw_41e9ee4f547fc59a:function(e,n){throw new Error(G(e,n))},__wbg_call_1875a20c43a36133:function(){return H(function(e,n,_,r){const s=x(e).call(x(n),x(_),x(r));return h(s)},arguments)},__wbg_call_187d372bd5fdd4aa:function(){return H(function(e,n,_){const r=x(e).call(x(n),x(_));return h(r)},arguments)},__wbg_getRandomValues_a608c4436c19407a:function(){return H(function(e,n){globalThis.crypto.getRandomValues(tt(e,n))},arguments)},__wbg_get_unchecked_288889d017702237:function(e,n){const _=x(e)[n>>>0];return h(_)},__wbg_instanceof_Error_80a725f81f2e102d:function(e){let n;try{n=x(e)instanceof Error}catch{n=!1}return n},__wbg_instanceof_Uint8Array_828cef2aaacafc31:function(e){let n;try{n=x(e)instanceof Uint8Array}catch{n=!1}return n},__wbg_length_7f3c00c40364105e:function(e){return x(e).length},__wbg_length_d4bdea10311bd9cf:function(e){return x(e).length},__wbg_message_5f8387f0c32b90a7:function(e){const n=x(e).message;return h(n)},__wbg_new_617a8cdb8bb1130e:function(){const e=new Object;return h(e)},__wbg_new_ee2291f50781bf1d:function(){const e=new Array;return h(e)},__wbg_new_from_slice_37a3c3a194266cb8:function(e,n){const _=new Uint32Array(dt(e,n));return h(_)},__wbg_new_from_slice_9a868026ffa4208a:function(e,n){const _=new Uint8Array(tt(e,n));return h(_)},__wbg_prototypesetcall_bc27214492979395:function(e,n,_){Uint8Array.prototype.set.call(tt(e,n),x(_))},__wbg_push_2baf45db356cf468:function(e,n){return x(e).push(x(n))},__wbg_set_145a351398b48c65:function(){return H(function(e,n,_){return Reflect.set(x(e),x(n),x(_))},arguments)},__wbindgen_generic_0000000000000001:function(e){return h(e)},__wbindgen_generic_0000000000000002:function(e,n){const _=G(e,n);return h(_)},__wbindgen_object_drop_ref:function(e){g(e)}}}}const _t=typeof FinalizationRegistry>"u"?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(a=>t.__wbg_caldrixweb_free(a,1));function h(a){P===F.length&&F.push(F.length+1);const e=P;return P=F[e],F[e]=a,e}function st(a){a<1028||(F[a]=P,P=a)}function ct(a,e){return a=a>>>0,gt().subarray(a/8,a/8+e)}function dt(a,e){return a=a>>>0,it().subarray(a/4,a/4+e)}function tt(a,e){return a=a>>>0,S().subarray(a/1,a/1+e)}let M=null;function i(){return(M===null||M.buffer.detached===!0||M.buffer.detached===void 0&&M.buffer!==t.memory.buffer)&&(M=new DataView(t.memory.buffer)),M}let B=null;function gt(){return(B===null||B.byteLength===0)&&(B=new Float64Array(t.memory.buffer)),B}function G(a,e){return bt(a>>>0,e)}let D=null;function it(){return(D===null||D.byteLength===0)&&(D=new Uint32Array(t.memory.buffer)),D}let N=null;function S(){return(N===null||N.byteLength===0)&&(N=new Uint8Array(t.memory.buffer)),N}function x(a){return F[a]}function H(a,e){try{return a.apply(this,e)}catch(n){t.__wbindgen_export3(h(n))}}let F=new Array(1024).fill(void 0);F.push(void 0,null,!0,!1);let P=F.length;function v(a){return a==null}function at(a,e){const n=e(a.length*4,4)>>>0;return it().set(a,n/4),c=a.length,n}function u(a,e){const n=e(a.length*1,1)>>>0;return S().set(a,n/1),c=a.length,n}function f(a,e,n){if(n===void 0){const d=V.encode(a),b=e(d.length,1)>>>0;return S().subarray(b,b+d.length).set(d),c=d.length,b}let _=a.length,r=e(_,1)>>>0;const s=S();let o=0;for(;o<_;o++){const d=a.charCodeAt(o);if(d>127)break;s[r+o]=d}if(o!==_){o!==0&&(a=a.slice(o)),r=n(r,_,_=o+a.length*3,1)>>>0;const d=S().subarray(r+o,r+_),b=V.encodeInto(a,d);o+=b.written,r=n(r,_,o,1)>>>0}return c=o,r}function g(a){const e=x(a);return st(a),e}let z=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0});z.decode();const wt=2146435072;let et=0;function bt(a,e){return et+=e,et>=wt&&(z=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0}),z.decode(),et=e),z.decode(S().subarray(a,a+e))}const V=new TextEncoder;"encodeInto"in V||(V.encodeInto=function(a,e){const n=V.encode(a);return e.set(n),{read:a.length,written:n.length}});let c=0,pt,ut,t;function ot(a,e){return ut=a,t=a.exports,pt=e,M=null,B=null,D=null,N=null,t}async function lt(a,e){if(typeof Response=="function"&&a instanceof Response){if(!a.ok)throw new Error(`failed to fetch Wasm: ${a.status} ${a.statusText} fetching '${a.url}'`);if(typeof WebAssembly.instantiateStreaming=="function")try{return await WebAssembly.instantiateStreaming(a,e)}catch(r){if(n(a.type)&&a.headers.get("Content-Type")!=="application/wasm")console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",r);else throw r}const _=await a.arrayBuffer();return await WebAssembly.instantiate(_,e)}else{const _=await WebAssembly.instantiate(a,e);return _ instanceof WebAssembly.Instance?{instance:_,module:a}:_}function n(_){switch(_){case"basic":case"cors":case"default":return!0}return!1}}function ft(a){if(t!==void 0)return t;a!==void 0&&(Object.getPrototypeOf(a)===Object.prototype?{module:a}=a:console.warn("using deprecated parameters for `initSync()`; pass a single object instead"));const e=rt();a instanceof WebAssembly.Module||(a=new WebAssembly.Module(a));const n=new WebAssembly.Instance(a,e);return ot(n,a)}async function vt(a){if(t!==void 0)return t;a!==void 0&&(Object.getPrototypeOf(a)===Object.prototype?{module_or_path:a}=a:console.warn("using deprecated parameters for the initialization function; pass a single object instead")),a===void 0&&(a=new URL("caldrix_web_bg.wasm",import.meta.url));const e=rt();(typeof a=="string"||typeof Request=="function"&&a instanceof Request||typeof URL=="function"&&a instanceof URL)&&(a=fetch(a));const{instance:n,module:_}=await lt(await a,e);return ot(n,_)}export{nt as CaldrixWeb,vt as default,ft as initSync};
Binary file
@@ -32,6 +32,7 @@ export const caldrixweb_next: (a: number, b: number) => void;
32
32
  export const caldrixweb_parseBaseListing: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
33
33
  export const caldrixweb_parseListing: (a: number, b: number, c: number) => void;
34
34
  export const caldrixweb_precheck: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => void;
35
+ export const caldrixweb_precheckFacts: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
35
36
  export const caldrixweb_precheckGguf: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
36
37
  export const caldrixweb_resolve: (a: number, b: number, c: number, d: number, e: number) => void;
37
38
  export const caldrixweb_resolveAlias: (a: number, b: number, c: number) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caldrix-ai/caldrix",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "private": false,
5
5
  "description": "Run a language model in the browser, in a worker, on WebAssembly or WebGPU; see the README for what has run.",
6
6
  "license": "Apache-2.0",