webslm 0.3.1 → 0.4.1

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.
Files changed (40) hide show
  1. package/README.md +64 -12
  2. package/dist/ai-sdk/index.d.ts +1 -1
  3. package/dist/ai-sdk/index.js +1 -1
  4. package/dist/cache/models.d.ts +32 -3
  5. package/dist/cache/models.js +106 -11
  6. package/dist/cache/node-fs.d.ts +26 -3
  7. package/dist/cache/node-fs.js +43 -9
  8. package/dist/cache-management.d.ts +1 -1
  9. package/dist/curated.d.ts +31 -1
  10. package/dist/curated.js +49 -5
  11. package/dist/download.d.ts +4 -0
  12. package/dist/download.js +29 -4
  13. package/dist/engine.d.ts +33 -4
  14. package/dist/engine.js +451 -44
  15. package/dist/index.d.ts +2 -2
  16. package/dist/index.js +1 -1
  17. package/dist/models.d.ts +26 -6
  18. package/dist/models.js +63 -7
  19. package/dist/native.d.ts +165 -0
  20. package/dist/native.js +243 -0
  21. package/dist/protocol.d.ts +12 -2
  22. package/dist/recommended-sampling.d.ts +54 -0
  23. package/dist/recommended-sampling.js +73 -0
  24. package/dist/simple.d.ts +22 -8
  25. package/dist/simple.js +35 -9
  26. package/dist/types.d.ts +85 -17
  27. package/native-manifest.json +47 -0
  28. package/package.json +2 -1
  29. package/wasm/relaxed/webslm_wasm.d.ts +26 -4
  30. package/wasm/relaxed/webslm_wasm.js +70 -17
  31. package/wasm/relaxed/webslm_wasm_bg.wasm +0 -0
  32. package/wasm/relaxed/webslm_wasm_bg.wasm.d.ts +5 -3
  33. package/wasm/simd/webslm_wasm.d.ts +26 -4
  34. package/wasm/simd/webslm_wasm.js +70 -17
  35. package/wasm/simd/webslm_wasm_bg.wasm +0 -0
  36. package/wasm/simd/webslm_wasm_bg.wasm.d.ts +5 -3
  37. package/wasm/threads/webslm_wasm.d.ts +26 -4
  38. package/wasm/threads/webslm_wasm.js +75 -22
  39. package/wasm/threads/webslm_wasm_bg.wasm +0 -0
  40. package/wasm/threads/webslm_wasm_bg.wasm.d.ts +5 -3
package/dist/curated.js CHANGED
@@ -1,8 +1,19 @@
1
1
  /** A short list of checkpoints known to run well, for model pickers. */
2
+ import { RECOMMENDED_SAMPLING, samplingSet, settingsFor, } from "./recommended-sampling.js";
3
+ export { RECOMMENDED_SAMPLING } from "./recommended-sampling.js";
4
+ const SETS = RECOMMENDED_SAMPLING.sets;
5
+ const SMOLLM2 = SETS["smollm2"];
6
+ const LFM2 = SETS["lfm2"];
7
+ const LFM3 = SETS["lfm3"];
8
+ const QWEN3 = SETS["qwen3"];
9
+ const QWEN3_5 = SETS["qwen3_5"];
2
10
  /**
3
11
  * Checkpoints validated against `transformers`, smallest first: the first
4
12
  * ones are quick on a laptop CPU, the largest (LFM3 1.7B) wants WebGPU. Any supported repository works too: see
5
- * `searchModels` and `checkModel` for the others.
13
+ * `searchModels` and `checkModel` for the others. Larger models answer
14
+ * better: the 135M and 360M SmolLM2 checkpoints lose track of a
15
+ * conversation after a turn or two, while LFM2.5 350M and the Qwen
16
+ * checkpoints recall what was said earlier.
6
17
  */
7
18
  export const CURATED_MODELS = [
8
19
  {
@@ -10,21 +21,24 @@ export const CURATED_MODELS = [
10
21
  name: "SmolLM2 135M",
11
22
  params: "135M",
12
23
  sizeBytes: 269_060_552,
13
- note: "Fastest. Good for trying things out.",
24
+ note: "Fastest, but loses track of a conversation quickly.",
25
+ sampling: SMOLLM2,
14
26
  },
15
27
  {
16
28
  repo: "HuggingFaceTB/SmolLM2-360M-Instruct",
17
29
  name: "SmolLM2 360M",
18
30
  params: "360M",
19
31
  sizeBytes: 723_674_912,
20
- note: "Better answers, still quick.",
32
+ note: "Quick, short answers.",
33
+ sampling: SMOLLM2,
21
34
  },
22
35
  {
23
36
  repo: "LiquidAI/LFM2.5-350M",
24
37
  name: "LFM2.5 350M",
25
38
  params: "350M",
26
39
  sizeBytes: 708_984_464,
27
- note: "Convolution and attention hybrid.",
40
+ note: "Best small model here; follows a conversation well.",
41
+ sampling: LFM2,
28
42
  },
29
43
  {
30
44
  repo: "Qwen/Qwen3-0.6B",
@@ -33,14 +47,17 @@ export const CURATED_MODELS = [
33
47
  sizeBytes: 1_503_300_328,
34
48
  note: "Can think step by step.",
35
49
  thinking: true,
50
+ thinksByDefault: true,
51
+ sampling: QWEN3,
36
52
  },
37
53
  {
38
54
  repo: "Qwen/Qwen3.5-0.8B",
39
55
  name: "Qwen3.5 0.8B",
40
56
  params: "0.8B",
41
57
  sizeBytes: 1_746_942_600,
42
- note: "Linear-attention (Gated DeltaNet) hybrid. Thinks when asked.",
58
+ note: "Strong answers for its size, best on WebGPU. Thinks when asked.",
43
59
  thinking: true,
60
+ sampling: QWEN3_5,
44
61
  },
45
62
  {
46
63
  repo: "LiquidAI/LFM3-1.7B",
@@ -49,5 +66,32 @@ export const CURATED_MODELS = [
49
66
  sizeBytes: 3_457_635_448,
50
67
  note: "Strongest here, best on WebGPU. Thinks by default.",
51
68
  thinking: true,
69
+ thinksByDefault: true,
70
+ sampling: LFM3,
52
71
  },
53
72
  ];
73
+ /** The entry of {@link CURATED_MODELS} for a repository id (case-insensitive), if any. */
74
+ export function curatedModel(repo) {
75
+ if (repo === undefined)
76
+ return undefined;
77
+ const id = repo.toLowerCase();
78
+ return CURATED_MODELS.find((m) => m.repo.toLowerCase() === id);
79
+ }
80
+ /**
81
+ * The sampling a model card recommends for a checkpoint, with or without
82
+ * reasoning (`think` unset: the model's default), or `undefined` when there
83
+ * is none (its `generation_config.json` then applies). `checkpoint` is a
84
+ * repository id, or what identifies a checkpoint: the curated repositories
85
+ * use their card's settings whatever their `generation_config.json` says;
86
+ * other checkpoints of the same families (`model_type` `qwen3`, `qwen3_5`,
87
+ * `lfm2`, `lfm3`) use their family's when their `generation_config.json`
88
+ * does not ask for sampling. The table is the Rust engine's, so the
89
+ * command-line tool and the server pick the same settings. Generations
90
+ * start from it: options a call sets override the matching fields, and
91
+ * `temperature: 0` (greedy decoding) turns it off.
92
+ */
93
+ export function recommendedSampling(checkpoint, think) {
94
+ const traits = typeof checkpoint === "string" || checkpoint === undefined ? { repo: checkpoint } : checkpoint;
95
+ const set = samplingSet(traits);
96
+ return set === undefined ? undefined : settingsFor(set, think);
97
+ }
@@ -7,6 +7,10 @@
7
7
  * read from the cache without any network request: a revision given as a
8
8
  * commit sha, or a branch already resolved once (the cached ref is reused;
9
9
  * delete the cached model to pick up new commits).
10
+ *
11
+ * In a directory store (Node) a snapshot the command-line tool pulled is used
12
+ * as is, and a completed download also writes the command-line tool's
13
+ * manifest, so `webslm pull` and the package share the files.
10
14
  */
11
15
  import { type CachedFileInfo } from "./cache/models.js";
12
16
  import type { CacheStore } from "./cache/store.js";
package/dist/download.js CHANGED
@@ -7,8 +7,12 @@
7
7
  * read from the cache without any network request: a revision given as a
8
8
  * commit sha, or a branch already resolved once (the cached ref is reused;
9
9
  * delete the cached model to pick up new commits).
10
+ *
11
+ * In a directory store (Node) a snapshot the command-line tool pulled is used
12
+ * as is, and a completed download also writes the command-line tool's
13
+ * manifest, so `webslm pull` and the package share the files.
10
14
  */
11
- import { checkRepoId, filePath, readManifest, readRef, writeManifest, writeRef, } from "./cache/models.js";
15
+ import { checkRepoId, filePath, readCliManifest, readManifest, readRef, writeCliManifest, writeManifest, writeRef, } from "./cache/models.js";
12
16
  import { WebslmError, abortError, isAborted, toWebslmError } from "./errors.js";
13
17
  import { SAFETENSORS_INDEX, fetchRevision, fileUrl, hubFetch, isAdapterListing, selectCheckpointFiles, } from "./hub.js";
14
18
  const SHA_PATTERN = /^[0-9a-f]{40}$/;
@@ -21,8 +25,12 @@ export async function prepareHubCheckpoint(repoId, revision, options) {
21
25
  // Offline path: a known sha whose files are all complete.
22
26
  const knownSha = SHA_PATTERN.test(rev) ? rev : await readRef(store, repo, rev);
23
27
  if (knownSha !== undefined) {
24
- const manifest = await readManifest(store, repo, knownSha);
25
- if (manifest?.complete === true && (await filesPresent(store, manifest))) {
28
+ const ours = await readManifest(store, repo, knownSha);
29
+ const manifest = ours?.complete === true && (await filesPresent(store, ours))
30
+ ? ours
31
+ : await readCliManifest(store, repo, knownSha);
32
+ if (manifest !== undefined) {
33
+ await shareWithCli(store, manifest, rev);
26
34
  emitCachedProgress(manifest, emit);
27
35
  return reader(store, manifest, true);
28
36
  }
@@ -34,7 +42,9 @@ export async function prepareHubCheckpoint(repoId, revision, options) {
34
42
  const names = revisionInfo.files.map((f) => f.name);
35
43
  return withLock(`webslm:${repo}@${sha}`, async () => {
36
44
  const previous = await readManifest(store, repo, sha);
37
- const done = { ...previous?.files };
45
+ // Files the command-line tool pulled count as downloaded.
46
+ const pulled = await readCliManifest(store, repo, sha);
47
+ const done = { ...previous?.files, ...pulled?.files };
38
48
  let downloaded = false;
39
49
  const download = async (file, progress) => {
40
50
  const hubFile = hubFiles.get(file);
@@ -119,12 +129,27 @@ export async function prepareHubCheckpoint(repoId, revision, options) {
119
129
  await writeManifest(store, manifest);
120
130
  if (rev !== sha)
121
131
  await writeRef(store, repo, rev, sha);
132
+ await shareWithCli(store, manifest, rev);
122
133
  return reader(store, manifest, !downloaded);
123
134
  });
124
135
  }
125
136
  function manifestOf(repo, sha, files, complete) {
126
137
  return { version: 1, repo, sha, files, complete, updatedAt: new Date().toISOString() };
127
138
  }
139
+ /**
140
+ * Writes the command-line tool's manifest of a complete snapshot in a
141
+ * directory store (where the tool may share the cache); best effort.
142
+ */
143
+ async function shareWithCli(store, manifest, revision) {
144
+ if (store.kind !== "fs")
145
+ return;
146
+ try {
147
+ await writeCliManifest(store, manifest, revision);
148
+ }
149
+ catch {
150
+ // A read-only cache: the command-line tool will pull the files itself.
151
+ }
152
+ }
128
153
  async function filesPresent(store, manifest) {
129
154
  for (const [file, info] of Object.entries(manifest.files)) {
130
155
  if (!info.complete)
package/dist/engine.d.ts CHANGED
@@ -9,14 +9,34 @@
9
9
  * A model keeps reading its source after the load: applying or removing a
10
10
  * LoRA adapter reads the affected base matrices again. Reads of a `fetch`
11
11
  * source are forwarded to the page under the request being served.
12
+ *
13
+ * In Node, a model runs on the native engine (the Node-API addon, downloaded on first use
14
+ * and verified, see `native.ts`) when it is available, the source is a hub model and `backend` allows it; otherwise
15
+ * on the WebAssembly engine. Both take the same options and report the same
16
+ * descriptions: the requests below are served the same way for both.
12
17
  */
13
- import type { Request, Response } from "./protocol.js";
14
- import type { Activations, CpuInfo } from "./types.js";
18
+ import { type CacheStore } from "./cache/index.js";
19
+ import type { Request, Response, WireSource } from "./protocol.js";
20
+ import type { NativeAddon, NativeModelInfo } from "./native.js";
21
+ import type { Activations, Backend, CpuInfo, ModelInfo } from "./types.js";
15
22
  import { type FlavourBindings, type WasmFlavour } from "./wasm-flavour.js";
16
23
  /** How the engine talks back to the page. */
17
24
  export interface EngineHost {
18
25
  post(message: Response, transfer?: Transferable[]): void;
19
26
  }
27
+ type Reader = (path: string, start?: number, end?: number) => Promise<Uint8Array | null>;
28
+ /** A source opened for the engine: reads, and what is known of its files. */
29
+ interface OpenedSource {
30
+ readonly read: Reader;
31
+ readonly list?: string[];
32
+ readonly sizes: Map<string, number>;
33
+ readonly hashes: Map<string, string>;
34
+ readonly repo?: string;
35
+ readonly revision?: string;
36
+ readonly fromCache: boolean;
37
+ /** The store a hub checkpoint was downloaded to. */
38
+ readonly store?: CacheStore;
39
+ }
20
40
  /** The instantiated engine: its bindings and how it runs. */
21
41
  export interface EngineWasm {
22
42
  readonly bindings: FlavourBindings;
@@ -42,8 +62,9 @@ export interface EngineWasmOptions {
42
62
  */
43
63
  export declare function initEngineWasm(options?: EngineWasmOptions): Promise<EngineWasm>;
44
64
  /**
45
- * Human-readable CPU backend, e.g. `"CPU (wasm, relaxed SIMD, 8 threads)"` or
46
- * `"CPU (wasm, SIMD128, 1 thread, deterministic, f32 activations)"`.
65
+ * Human-readable CPU backend, e.g. `"CPU (wasm, relaxed SIMD, 8 threads)"`,
66
+ * `"CPU (wasm, SIMD128, 1 thread, deterministic, f32 activations)"` or
67
+ * `"CPU (native, neon-i8mm, 10 threads)"`.
47
68
  */
48
69
  export declare function describeCpu(cpu: CpuInfo, activations?: Activations): string;
49
70
  export declare class Engine {
@@ -52,3 +73,11 @@ export declare class Engine {
52
73
  /** Handles one message from the page. Answers are posted through the host. */
53
74
  handle(request: Request): void;
54
75
  }
76
+ /** The engine `backend` picks for `source`: the native addon, or why not. */
77
+ export declare function chooseNative(backend: Backend, source: WireSource, wasmFlavour?: WasmFlavour, cacheDir?: string): Promise<{
78
+ readonly addon?: NativeAddon;
79
+ readonly note?: string;
80
+ }>;
81
+ /** The {@link ModelInfo} of a model on the native engine. */
82
+ export declare function nativeModelInfo(raw: NativeModelInfo, opened: Pick<OpenedSource, "repo" | "revision" | "hashes" | "sizes" | "fromCache">, adapter: Pick<OpenedSource, "repo" | "revision"> | undefined, loadTimeMs: number): ModelInfo;
83
+ export {};