@soverius-ai/browser-models 0.0.0-stage → 0.2.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 soverius-ai contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,32 @@
1
- # Temporary Holding Version
1
+ # @soverius-ai/browser-models
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Run language models in the browser with one engine interface: the Chrome/Edge Prompt API, WebLLM and transformers.js. No AG-UI, no dependencies.
4
+
5
+ ```bash
6
+ pnpm add @soverius-ai/browser-models
7
+ pnpm add @mlc-ai/web-llm # only for WebLlmEngine
8
+ pnpm add @huggingface/transformers # only for TransformersJsEngine
9
+ ```
10
+
11
+ ```ts
12
+ import { PromptApiEngine } from '@soverius-ai/browser-models/prompt-api';
13
+
14
+ const engine = new PromptApiEngine({ sampling: 'balanced' });
15
+ if ((await engine.availability()) !== 'unavailable') {
16
+ await engine.load();
17
+ for await (const part of engine.generate({
18
+ messages: [{ role: 'user', content: 'Hi' }],
19
+ }))
20
+ if (part.type === 'text') output.textContent += part.text;
21
+ }
22
+ ```
23
+
24
+ Entries: `.`, `/prompt-api`, `/web-llm`, `/web-llm/worker`, `/transformers`, `/engines` (all three, by `EngineKind`).
25
+
26
+ | Package | For |
27
+ | ------------------------------------------------------- | ----------------------------------------------- |
28
+ | `@soverius-ai/browser-models-agui` | AG-UI agent, for CopilotKit or any AG-UI client |
29
+ | `@soverius-ai/browser-models-tools` | tool calling, MCP, WebMCP, generative UI |
30
+ | `@soverius-ai/browser-models-angular`, `-react`, `-vue` | framework bindings |
31
+
32
+ [Docs](https://browser-models.soverius.ai/docs) · [Playground](https://browser-models.soverius.ai/playground) · by [Soverius AI](https://soverius.ai) · MIT
@@ -0,0 +1 @@
1
+ import{c as o}from"./chunk-T3BB5C4O.js";var i=class{#t;#n;#r=new Set;#e;constructor(e,r){this.#t=e,this.#n=r}get state(){return this.#e?this.#e.ready?"ready":"loading":"idle"}async get({onProgress:e,signal:r}={}){r?.throwIfAborted();let t=this.#e??=this.#o(),{signal:n}=t.cancel;e&&this.#r.add(e);try{return await o(t.loaded,r?AbortSignal.any([r,n]):n)}finally{e&&this.#r.delete(e)}}async reset(){let e=this.#e;this.#e=void 0,e&&(e.ready?await this.#n(await e.loaded):e.cancel.abort(new DOMException("The model was unloaded.","AbortError")))}#o(){let e=new AbortController,r={cancel:e,ready:!1,loaded:this.#t(t=>this.#r.forEach(n=>n(t)),e.signal)};return r.loaded.then(()=>{r.ready=!0},()=>{this.#e===r&&(this.#e=void 0)}),r}};export{i as a};
@@ -0,0 +1 @@
1
+ import{a as f}from"./chunk-EFSLG2O3.js";import{a as g}from"./chunk-4RAISIWO.js";import{b as y}from"./chunk-T3BB5C4O.js";function w(l="soverius-transformers",t=()=>navigator.storage.getDirectory()){let e=async()=>(await t()).getDirectoryHandle(l,{create:!0});return{async match(n){try{let r=await(await e()).getFileHandle(await m(n));return new Response(await r.getFile())}catch{return}},async put(n,r){if(!r.body)return;let a=await m(n),i=await e(),d=await i.getFileHandle(a,{create:!0});try{await r.body.pipeTo(await d.createWritable())}catch(o){throw await i.removeEntry(a).catch(()=>{}),o}},async delete(n){try{return await(await e()).removeEntry(await m(n)),!0}catch{return!1}}}}async function m(l){let t=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(l));return Array.from(new Uint8Array(t),e=>e.toString(16).padStart(2,"0")).join("")}var k=class{id;kind="transformers";supportsConstraint=!1;contextWindowSize;#e;#t;#a;#p;#s;#o;#n;#d=Promise.resolve();#l=!1;#c;constructor(t,e={}){this.id=`transformers:${t}`,this.contextWindowSize=e.contextWindowSize,this.#e=t,this.#t=e.task??"text-generation",this.#a=e.device??"webgpu",this.#p=e.dtype??(this.#a==="webgpu"?"q4f16":"q4");let n=e.sampling===void 0?0:y[e.sampling];this.#s={...e.native,max_new_tokens:e.maxOutputTokens??1024,...n>0?{do_sample:!0,temperature:n}:{do_sample:e.native?.do_sample??!1}},this.#o=e.storage==="opfs"?w():void 0,this.#n=new g((r,a)=>{let i=this.#h(r,a,this.#d);return this.#d=i.catch(()=>{}),i},r=>r.dispose())}async availability(){if(this.#a==="webgpu"&&!("gpu"in navigator))return"unavailable";if(this.#n.state==="loading"||this.#l)return"downloading";if(this.#n.state==="ready")return"available";let{ModelRegistry:t}=await this.#i();return(this.#t==="text-generation"?await t.is_pipeline_cached(this.#t,this.#e,this.#r()):await t.is_cached(this.#e,this.#r()))?"available":"downloadable"}async load(t){await this.#n.get(t)}async unload(){await this.#n.reset()}async delete(){this.#l=!0;try{await this.#d,await this.unload(),await this.#u()}finally{this.#l=!1}}async#u(){let{ModelRegistry:t}=await this.#i();this.#t==="text-generation"?await t.clear_pipeline_cache(this.#t,this.#e,this.#r()):await t.clear_cache(this.#e,this.#r())}async*generate({messages:t,signal:e}){let n=await this.#n.get(e?{signal:e}:{}),{TextStreamer:r,InterruptableStoppingCriteria:a}=await this.#i(),i=new a,d=()=>i.interrupt();e?.addEventListener("abort",d);let o=new f,c=0,h=new r(n.tokenizer,{skip_prompt:!0,skip_special_tokens:!0,callback_function:s=>{s&&o.push({type:"text",text:s})},token_callback_function:s=>{c+=s.length}}),p=b(n.tokenizer,t);n.run(t,{streamer:h,stopping:i}).then(()=>o.close(),s=>o.fail(s));let u=!1;try{yield*o,u=!0,e?.throwIfAborted();let s=this.#s.max_new_tokens;yield{type:"finish",reason:s!=null&&c>=s?"length":"stop",usage:{totalTokens:(p??0)+c,...p===void 0?{}:{promptTokens:p},outputTokens:c}}}finally{e?.removeEventListener("abort",d),u||d()}}#r(){return{device:this.#a,dtype:this.#p}}async#h(t,e,n){await n;let r=i=>{i.status==="progress_total"&&t(i.progress/100)},a=await(this.#t==="text-generation"?this.#m(r):this.#y(r));return e.aborted&&(await a.dispose(),e.throwIfAborted()),a}async#m(t){let{pipeline:e}=await this.#i(),n=await e("text-generation",this.#e,{...this.#r(),progress_callback:t});return{tokenizer:n.tokenizer,run:(r,{streamer:a,stopping:i})=>n([...r],{...this.#s,streamer:a,stopping_criteria:i}),dispose:()=>n.dispose()}}async#y(t){let{AutoProcessor:e,AutoModelForImageTextToText:n}=await this.#i(),[r,a]=await Promise.all([e.from_pretrained(this.#e),n.from_pretrained(this.#e,{...this.#r(),progress_callback:t})]),i=r.tokenizer;if(!i)throw await a.dispose(),new Error(`${this.#e} has no tokenizer.`);return{tokenizer:i,run:async(d,{streamer:o,stopping:c})=>{let h={add_generation_prompt:!0,enable_thinking:!1},p=r.apply_chat_template([...d],h),u=await r(p,null,null,{add_special_tokens:!1});return a.generate({...u,...this.#s,streamer:o,stopping_criteria:c})},dispose:()=>a.dispose()}}#i(){return this.#c??=import("@huggingface/transformers").then(t=>(this.#o&&(t.env.useCustomCache=!0,t.env.customCache=this.#o),t),t=>{throw this.#c=void 0,t}),this.#c}};function b(l,t){try{let e=l.apply_chat_template([...t],{tokenize:!0,add_generation_prompt:!0,return_tensor:!1,return_dict:!1});return Array.isArray(e)?e.length:void 0}catch{return}}export{k as a};
@@ -0,0 +1 @@
1
+ var e=class{#s=[];#t=!1;#e;#i;push(i){this.#s.push(i),this.#i?.()}close(){this.#t=!0,this.#i?.()}fail(i){this.#e=i??new Error("Failed."),this.close()}async*[Symbol.asyncIterator](){for(;;){let i=this.#s.shift();if(i!==void 0){yield i;continue}if(this.#e)throw this.#e;if(this.#t)return;await new Promise(s=>this.#i=s),this.#i=void 0}}};export{e as a};
@@ -0,0 +1 @@
1
+ import{a as h}from"./chunk-4RAISIWO.js";import{b as f}from"./chunk-T3BB5C4O.js";var y=class{id;kind="web-llm";supportsConstraint=!0;#n;#e;#t;#i;#a;constructor(e,n={}){this.id=`web-llm:${e}`,this.#n=e,this.#e=n,this.#t=new h((t,a)=>this.#r(t,a),u)}get contextWindowSize(){return this.#e.contextWindowSize??this.#e.native?.context_window_size??this.#a}async availability(){if(!("gpu"in navigator))return"unavailable";if(this.#t.state==="loading")return"downloading";if(this.#t.state==="ready")return"available";let{hasModelInCache:e}=await this.#o();return await e(this.#n,this.#e.appConfig)?"available":"downloadable"}async load(e){await this.#t.get(e)}async unload(){await this.#t.reset()}async delete(){await this.unload();let{deleteModelAllInfoInCache:e}=await this.#o();await e(this.#n,this.#e.appConfig)}async*generate({messages:e,constraint:n,signal:t}){let{mlc:a}=await this.#t.get(t?{signal:t}:{}),{maxOutputTokens:d}=this.#e,r=()=>{a.interruptGenerate()};t?.addEventListener("abort",r);let s=!1;try{let l=await a.chat.completions.create({messages:e.map(o=>({role:o.role,content:o.content})),stream:!0,stream_options:{include_usage:!0},...d===void 0?{}:{max_tokens:d},...n?{response_format:{type:"json_object",schema:JSON.stringify(n)}}:{}}),i="stop",p;for await(let o of l){let[c]=o.choices;c?.delta.content&&(yield{type:"text",text:c.delta.content}),c?.finish_reason==="length"&&(i="length"),o.usage&&(p={totalTokens:o.usage.total_tokens,promptTokens:o.usage.prompt_tokens,outputTokens:o.usage.completion_tokens})}s=!0,t?.throwIfAborted(),yield{type:"finish",reason:i,...p?{usage:p}:{}}}finally{t?.removeEventListener("abort",r),s||r()}}async#r(e,n){let t=await this.#o();n.throwIfAborted(),this.#a=(this.#e.appConfig??t.prebuiltAppConfig).model_list.find(i=>i.model_id===this.#n)?.overrides?.context_window_size;let a={...this.#e.appConfig?{appConfig:this.#e.appConfig}:{},initProgressCallback:i=>e(i.progress)},{worker:d=w}=this.#e,r=d===!1?void 0:d(),s={worker:r,mlc:r?new t.WebWorkerMLCEngine(r,a):new t.MLCEngine(a)},l=()=>{u(s).catch(()=>{})};n.addEventListener("abort",l);try{await s.mlc.reload(this.#n,this.#s())}catch(i){throw n.aborted||await u(s).catch(()=>{}),i}finally{n.removeEventListener("abort",l)}return n.throwIfAborted(),s}#s(){let{native:e,contextWindowSize:n,sampling:t}=this.#e;return n===void 0&&t===void 0?e:{...e,...n===void 0?{}:{context_window_size:n},...t===void 0?{}:{temperature:f[t]}}}#o(){return this.#i??=import("@mlc-ai/web-llm").catch(e=>{throw this.#i=void 0,e}),this.#i}};function w(){return new Worker(new URL("./web-llm-default.worker.js",import.meta.url),{type:"module"})}async function u({mlc:m,worker:e}){await m.unload(),e?.terminate()}export{y as a};
@@ -0,0 +1 @@
1
+ var a=(e=>(e.PromptApi="prompt-api",e.WebLlm="web-llm",e.TransformersJs="transformers",e))(a||{}),i={"most-predictable":0,predictable:.2,"slightly-predictable":.4,balanced:.7,"slightly-creative":.9,creative:1.1,"most-creative":1.3};function s(t,n){return n?(n.throwIfAborted(),new Promise((o,e)=>{let r=()=>e(n.reason);n.addEventListener("abort",r,{once:!0}),t.then(o,e).finally(()=>n.removeEventListener("abort",r))})):t}function d(t){return t instanceof Error&&t.name==="AbortError"}export{a,i as b,s as c,d};
@@ -0,0 +1 @@
1
+ import{WebWorkerMLCEngineHandler as o}from"@mlc-ai/web-llm";function s(){let e=new o;self.onmessage=n=>e.onmessage(n)}export{s as a};
@@ -0,0 +1 @@
1
+ import{a as c}from"./chunk-4RAISIWO.js";var h=class{id="prompt-api";kind="prompt-api";supportsConstraint=!0;#i;#t;#n;#e;#o;#a;constructor(e={}){let n=e.languages??["en"];this.#i=e.languageModel,this.#t={expectedInputs:[{type:"text",languages:[...n]}],expectedOutputs:[{type:"text",languages:[...n]}]},this.#a=e.sampling,this.#n=new c((t,a)=>this.#d(t,a),async()=>{})}get contextWindowSize(){return this.#o}async availability(){let e=this.#s();return e?e.availability(this.#t):"unavailable"}async load(e){await this.#n.get(e)}async unload(){await this.#n.reset(),this.#e?.session.destroy(),this.#e=void 0}async*generate({messages:e,constraint:n,signal:t}){let a=e.at(-1);if(a?.role!=="user")throw new Error("The last message sent to the Prompt API must be from the user.");let s=e.slice(0,-1),i=this.#e,l=i&&!i.busy&&M(i.history,s);i&&!i.busy&&!l&&i.session.destroy();let o=l?i.session:await this.#r().create({...this.#t,...this.#a?{samplingMode:this.#a}:{},initialPrompts:[...s],...t?{signal:t}:{}});this.#o=o.contextWindow??o.inputQuota;let r={session:o,history:s,busy:!0};this.#e=r;let u="",p=!1;try{let f=o.promptStreaming(a.content,{...t?{signal:t}:{},...n?{responseConstraint:n}:{}}).getReader();for(;;){let{done:m,value:y}=await f.read();if(m)break;u+=y,yield{type:"text",text:y}}t?.throwIfAborted(),r.history=[...s,a,{role:"assistant",content:u}],p=!0;let g=o.contextUsage??o.inputUsage;yield{type:"finish",reason:"stop",...g===void 0?{}:{usage:{totalTokens:g}}}}finally{r.busy=!1,(!p||this.#e!==r)&&(o.destroy(),this.#e===r&&(this.#e=void 0))}}async#d(e,n){let t=this.#r();if(await t.availability(this.#t)==="available")return;(await t.create({...this.#t,signal:n,monitor(s){s.addEventListener("downloadprogress",i=>e(i.loaded))}})).destroy()}#s(){return this.#i??(typeof LanguageModel>"u"?void 0:LanguageModel)}#r(){let e=this.#s();if(!e)throw new Error("The Prompt API (LanguageModel) is not available in this browser.");return e}};function M(d,e){return d.length===e.length&&d.every((n,t)=>{let a=e[t];return a!==void 0&&n.role===a.role&&n.content.trim()===a.content.trim()})}export{h as a};
@@ -0,0 +1,11 @@
1
+ /**
2
+ * A transformers.js cache (`env.customCache`) that stores model files in the Origin Private
3
+ * File System instead of the Cache API. Files are named by the SHA-256 of their URL and
4
+ * streamed to disk, so large weights never sit in memory.
5
+ */
6
+ export interface FileCache {
7
+ match(key: string): Promise<Response | undefined>;
8
+ put(key: string, response: Response): Promise<void>;
9
+ delete(key: string): Promise<boolean>;
10
+ }
11
+ export declare function opfsCache(directoryName?: string, root?: () => Promise<FileSystemDirectoryHandle>): FileCache;
@@ -0,0 +1,41 @@
1
+ /// <reference types="dom-chromium-ai" preserve="true" />
2
+ import { EngineKind, type Availability, type Engine, type EnginePart, type GenerateRequest, type LoadOptions, type ModelSettings } from '../lib/engine.ts';
3
+ /** Native Prompt API declarations supplied by @types/dom-chromium-ai. */
4
+ export type LanguageModelApi = Pick<typeof LanguageModel, 'availability' | 'create'>;
5
+ /** The languages a session must handle, as the browser's Prompt API expects them. */
6
+ export type LanguageModelExpectations = Pick<LanguageModelCreateCoreOptions, 'expectedInputs' | 'expectedOutputs'>;
7
+ /** A Prompt API session (`LanguageModel`). */
8
+ export type LanguageModelSession = LanguageModel;
9
+ /**
10
+ * Takes the same `ModelSettings` as the other engines, so one settings object fits all three.
11
+ * The browser sets the context size and the reply length: only `sampling` applies here.
12
+ */
13
+ export interface PromptApiOptions extends ModelSettings {
14
+ /** Languages of the conversation, for example `['en', 'de']`. Default `['en']`. */
15
+ readonly languages?: readonly string[];
16
+ /** The API to use. Defaults to the browser's global `LanguageModel`. */
17
+ readonly languageModel?: LanguageModelApi;
18
+ }
19
+ /**
20
+ * Engine for the model built into the browser: Gemini Nano in Chrome, Phi-4-mini in Edge.
21
+ * The browser downloads, stores and removes the model itself, so there is no `delete()`.
22
+ * `load()` may need a user gesture.
23
+ */
24
+ export declare class PromptApiEngine implements Engine {
25
+ #private;
26
+ readonly id = "prompt-api";
27
+ readonly kind = EngineKind.PromptApi;
28
+ readonly supportsConstraint = true;
29
+ constructor(options?: PromptApiOptions);
30
+ /**
31
+ * The browser sets the context size and only a session tells it, so it is known from the
32
+ * first answer on.
33
+ */
34
+ get contextWindowSize(): number | undefined;
35
+ /** What the browser answers for the configured languages; `unavailable` without the Prompt API. */
36
+ availability(): Promise<Availability>;
37
+ load(options?: LoadOptions): Promise<void>;
38
+ /** Stops a download this page is waiting for and destroys the kept session. */
39
+ unload(): Promise<void>;
40
+ generate({ messages, constraint, signal, }: GenerateRequest): AsyncGenerator<EnginePart>;
41
+ }
@@ -0,0 +1,20 @@
1
+ import type { LoadOptions } from '../lib/engine.ts';
2
+ type Progress = (fraction: number) => void;
3
+ /**
4
+ * One model load shared by every caller of an engine. Each caller gets progress and can stop
5
+ * waiting with its own signal; the load itself only stops on `reset()`.
6
+ */
7
+ export declare class SharedLoad<T> {
8
+ #private;
9
+ /**
10
+ * `start` loads the model. When its signal aborts, it has to free what it created.
11
+ * `free` releases a loaded model.
12
+ */
13
+ constructor(start: (onProgress: Progress, signal: AbortSignal) => Promise<T>, free: (loaded: T) => Promise<unknown>);
14
+ get state(): 'idle' | 'loading' | 'ready';
15
+ /** Starts the load unless it is running or done, and waits for the model. */
16
+ get({ onProgress, signal }?: LoadOptions): Promise<T>;
17
+ /** Stops a load in progress, or frees the loaded model. The next `get()` loads again. */
18
+ reset(): Promise<void>;
19
+ }
20
+ export {};
@@ -0,0 +1,58 @@
1
+ import { EngineKind, type Availability, type Engine, type EnginePart, type GenerateRequest, type LoadOptions, type ModelSettings, type Sampling } from '../lib/engine.ts';
2
+ import type { GenerationConfig } from '@huggingface/transformers';
3
+ export interface TransformersJsOptions extends ModelSettings {
4
+ /**
5
+ * How the model is loaded.
6
+ * - `text-generation` (default): text-only models, for example "onnx-community/Qwen3-0.6B-ONNX".
7
+ * - `image-text-to-text`: multimodal models loaded with `AutoProcessor`, for example
8
+ * "onnx-community/gemma-4-E4B-it-ONNX". This engine sends them text only.
9
+ */
10
+ readonly task?: 'text-generation' | 'image-text-to-text';
11
+ /** Where the model runs. `webgpu` is fast; `wasm` works without a GPU. Default `webgpu`. */
12
+ readonly device?: 'webgpu' | 'wasm';
13
+ /** Weight precision. Default `q4f16` on WebGPU and `q4` on WASM. */
14
+ readonly dtype?: 'q4f16' | 'q4' | 'q8' | 'fp16' | 'fp32';
15
+ /** Default 1024. */
16
+ readonly maxOutputTokens?: number;
17
+ /**
18
+ * Sets `do_sample` and `temperature`. Default: always the likeliest word (`do_sample: false`),
19
+ * like `most-predictable`.
20
+ */
21
+ readonly sampling?: Sampling;
22
+ /**
23
+ * How many tokens the agent may send before it compacts the conversation. transformers.js
24
+ * has no context size to set: the context is the model's own, limited only by memory.
25
+ * Without this setting long conversations are never compacted and can run out of GPU memory.
26
+ */
27
+ readonly contextWindowSize?: number;
28
+ /**
29
+ * Any other transformers.js generation setting, passed to every run as it is: `top_p`,
30
+ * `top_k`, `repetition_penalty` and so on. The settings above win over the same option in here.
31
+ */
32
+ readonly native?: Partial<GenerationConfig>;
33
+ /**
34
+ * Where model files are stored. `cache-api` (default) is transformers.js' own choice;
35
+ * `opfs` stores them in the Origin Private File System. transformers.js keeps this setting
36
+ * globally, so it applies to every transformers.js model on the page.
37
+ */
38
+ readonly storage?: 'cache-api' | 'opfs';
39
+ }
40
+ /**
41
+ * Engine for transformers.js: ONNX models from the Hugging Face Hub. Weights are stored by
42
+ * transformers.js (Cache API). The library is loaded on first use, so it is not part of
43
+ * your initial bundle.
44
+ */
45
+ export declare class TransformersJsEngine implements Engine {
46
+ #private;
47
+ readonly id: string;
48
+ readonly kind = EngineKind.TransformersJs;
49
+ readonly supportsConstraint = false;
50
+ readonly contextWindowSize: number | undefined;
51
+ constructor(modelId: string, options?: TransformersJsOptions);
52
+ /** `unavailable` without WebGPU (on `device: 'webgpu'`); `available` once the files are cached. */
53
+ availability(): Promise<Availability>;
54
+ load(options?: LoadOptions): Promise<void>;
55
+ unload(): Promise<void>;
56
+ delete(): Promise<void>;
57
+ generate({ messages, signal, }: GenerateRequest): AsyncGenerator<EnginePart>;
58
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Starts WebLLM inside a Web Worker. Use it as the whole worker file:
3
+ *
4
+ * ```ts
5
+ * // web-llm.worker.ts
6
+ * import { startWebLlmWorker } from '@soverius-ai/browser-models/web-llm/worker';
7
+ * startWebLlmWorker();
8
+ * ```
9
+ */
10
+ export declare function startWebLlmWorker(): void;
@@ -0,0 +1,48 @@
1
+ import { EngineKind, type Availability, type Engine, type EnginePart, type GenerateRequest, type LoadOptions, type ModelSettings, type Sampling } from '../lib/engine.ts';
2
+ import type { AppConfig, ChatOptions } from '@mlc-ai/web-llm';
3
+ export interface WebLlmOptions extends ModelSettings {
4
+ /**
5
+ * Where the model runs. Default: a Web Worker of this package, so the page stays responsive.
6
+ * `false` runs it on the main thread. A function creates your own worker, whose file calls
7
+ * `startWebLlmWorker()` from `@soverius-ai/browser-models/web-llm/worker`. The worker is
8
+ * created once per load; `unload()` and `delete()` terminate it.
9
+ */
10
+ readonly worker?: false | (() => Worker);
11
+ /** WebLLM app config, for example `{ ...prebuiltAppConfig, cacheBackend: 'opfs' }`. */
12
+ readonly appConfig?: AppConfig;
13
+ /**
14
+ * Default: what the model was built with: 4096 for most prebuilt WebLLM models, 2048 or 1024
15
+ * for small ones. A call that needs more fails with WebLLM's
16
+ * `ContextWindowSizeExceededError`. A larger window needs more GPU memory.
17
+ */
18
+ readonly contextWindowSize?: number;
19
+ /** Default: until the model stops or the context is full. */
20
+ readonly maxOutputTokens?: number;
21
+ /** Sets `temperature`. Default: the model's own setting. */
22
+ readonly sampling?: Sampling;
23
+ /**
24
+ * Any other WebLLM chat option, passed to `reload()` as it is: `top_p`, `repetition_penalty`,
25
+ * `sliding_window_size` and so on. The settings above win over the same option in here.
26
+ */
27
+ readonly native?: ChatOptions;
28
+ }
29
+ /**
30
+ * Engine for WebLLM: models compiled for WebGPU, for example "Qwen3-0.6B-q4f16_1-MLC".
31
+ * Weights are stored by WebLLM itself (Cache API by default). The WebLLM library is
32
+ * loaded on first use, so it is not part of your initial bundle.
33
+ */
34
+ export declare class WebLlmEngine implements Engine {
35
+ #private;
36
+ readonly id: string;
37
+ readonly kind = EngineKind.WebLlm;
38
+ readonly supportsConstraint = true;
39
+ constructor(modelId: string, options?: WebLlmOptions);
40
+ /** The `contextWindowSize` option, or what the model was built with (known after `load()`). */
41
+ get contextWindowSize(): number | undefined;
42
+ /** `unavailable` without WebGPU; `available` once WebLLM has the model in its cache. */
43
+ availability(): Promise<Availability>;
44
+ load(options?: LoadOptions): Promise<void>;
45
+ unload(): Promise<void>;
46
+ delete(): Promise<void>;
47
+ generate({ messages, constraint, signal, }: GenerateRequest): AsyncGenerator<EnginePart>;
48
+ }
package/engines.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * `@soverius-ai/browser-models/engines`: all three engines, created by `EngineKind`. Needs both engine libraries.
3
+ *
4
+ * @module all-engines
5
+ */
6
+ import { EngineKind } from './lib/engine.ts';
7
+ import { PromptApiEngine, type PromptApiOptions } from './engines/prompt-api.ts';
8
+ import { TransformersJsEngine, type TransformersJsOptions } from './engines/transformers.ts';
9
+ import { WebLlmEngine, type WebLlmOptions } from './engines/web-llm.ts';
10
+ /** The engine class and the constructor arguments of every `EngineKind`. */
11
+ export interface EngineTypes {
12
+ [EngineKind.PromptApi]: {
13
+ engine: PromptApiEngine;
14
+ args: [options?: PromptApiOptions];
15
+ };
16
+ [EngineKind.WebLlm]: {
17
+ engine: WebLlmEngine;
18
+ args: [modelId: string, options?: WebLlmOptions];
19
+ };
20
+ [EngineKind.TransformersJs]: {
21
+ engine: TransformersJsEngine;
22
+ args: [modelId: string, options?: TransformersJsOptions];
23
+ };
24
+ }
25
+ /** Any engine of this library. `switch (engine.kind)` narrows it to its class. */
26
+ export type BrowserEngine = EngineTypes[EngineKind]['engine'];
27
+ /**
28
+ * Creates any engine of this library by its kind, for apps that install all three engine
29
+ * libraries. Apps that use fewer import the engine classes from their own entry points, so
30
+ * the other libraries need not be installed.
31
+ *
32
+ * ```ts
33
+ * Engines.create(EngineKind.WebLlm, 'gemma-4-E2B-it-q4f16_1-MLC', { contextWindowSize: 16384 });
34
+ * Engines.create(EngineKind.PromptApi, { sampling: 'balanced' });
35
+ * ```
36
+ */
37
+ export declare class Engines {
38
+ /** A new engine of `kind`, with that engine's own constructor arguments. */
39
+ static create<Kind extends EngineKind>(kind: Kind, ...args: EngineTypes[Kind]['args']): EngineTypes[Kind]['engine'];
40
+ }
41
+ export { EngineKind };
42
+ export * from './engines/prompt-api.ts';
43
+ export * from './engines/transformers.ts';
44
+ export * from './engines/web-llm.ts';
package/engines.js ADDED
@@ -0,0 +1 @@
1
+ import{a as i}from"./chunk-UZW7NPZG.js";import{a as r}from"./chunk-MPBLCIMW.js";import{a as s}from"./chunk-6TJV26R3.js";import"./chunk-EFSLG2O3.js";import"./chunk-4RAISIWO.js";import{a as g}from"./chunk-T3BB5C4O.js";var m={"prompt-api":n=>new i(n),"web-llm":(n,e)=>new r(n,e),transformers:(n,e)=>new s(n,e)},o=class{static create(e,...p){let t=m[e];return t(...p)}};export{g as EngineKind,o as Engines,i as PromptApiEngine,s as TransformersJsEngine,r as WebLlmEngine};
package/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * `@soverius-ai/browser-models`: the engine interface, its settings and stream parts, `Models`, `ModelStore` and `OutputParser`. No engine library needed.
3
+ *
4
+ * @module core
5
+ */
6
+ export { EngineKind, SAMPLING_TEMPERATURE, type Availability, type ChatMessage, type Engine, type EnginePart, type FinishReason, type GenerateRequest, type JsonSchema, type LoadOptions, type ModelSettings, type Sampling, type Usage, } from './lib/engine.ts';
7
+ export { AsyncQueue } from './lib/async-queue.ts';
8
+ export { untilAborted } from './lib/abort.ts';
9
+ export { OutputParser, type OutputPart } from './lib/output-parser.ts';
10
+ export { ModelStore, type ModelSource, type ModelState, type ModelStatus, type StorageUse, } from './model-store.ts';
11
+ export { Models } from './models.ts';
package/index.js ADDED
@@ -0,0 +1 @@
1
+ import{a as M}from"./chunk-EFSLG2O3.js";import{a as m,b as k,c as b,d as y}from"./chunk-T3BB5C4O.js";var f=[{kind:"tool-call",open:"```tool_call",close:"```"},{kind:"tool-result",open:"```tool_result",close:"```"},{kind:"tool-call",open:"<tool_call>",close:"</tool_call>"},{kind:"reasoning",open:"<think>",close:"</think>"}],d=class{#e="";#t;push(e){this.#e+=e;let t=[];for(;;){let i=this.#t;if(i){let o=this.#e.indexOf(i.close);if(i.kind==="tool-call"){if(o===-1){let l=this.#e.length-a(this.#e,[i.close]),u=this.#e.slice(0,l).trim();return u&&t.push({kind:"tool-call-partial",raw:u}),t}t.push({kind:"tool-call",raw:this.#e.slice(0,o).trim()})}else if(i.kind==="tool-result"){if(o===-1)return this.#e=this.#e.slice(this.#e.length-a(this.#e,[i.close])),t}else{let l=o===-1?this.#e.length-a(this.#e,[i.close]):o;if(r(t,"reasoning",this.#e.slice(0,l)),o===-1)return this.#e=this.#e.slice(l),t;t.push({kind:"reasoning-end"})}this.#e=this.#e.slice(o+i.close.length),this.#t=void 0;continue}let n=this.#i();if(n){r(t,"text",this.#e.slice(0,n.index)),this.#e=this.#e.slice(n.index+n.block.open.length),this.#t=n.block;continue}let g=a(this.#e,f.map(o=>o.open));return r(t,"text",this.#e.slice(0,this.#e.length-g)),this.#e=this.#e.slice(this.#e.length-g),t}}end(){let e=[];return this.#t?.kind==="tool-call"?e.push({kind:"tool-call-cut",raw:this.#e.trim()}):this.#t?.kind==="reasoning"?(r(e,"reasoning",this.#e),e.push({kind:"reasoning-end"})):this.#t?.kind==="tool-result"||r(e,"text",this.#e),this.#e="",this.#t=void 0,e}#i(){let e;for(let t of f){let i=this.#e.indexOf(t.open);i!==-1&&(!e||i<e.index)&&(e={index:i,block:t})}return e}};function r(s,e,t){t&&s.push({kind:e,text:t})}function a(s,e){let t=0;for(let i of e)for(let n=Math.min(i.length-1,s.length);n>t;n--)if(s.endsWith(i.slice(0,n))){t=n;break}return t}var h=class{agent;modelIds;#e=new Set;#t;constructor(e){this.agent=e,this.modelIds=e.modelIds,this.#t={selectedModel:e.selectedModel,availability:"checking",progress:void 0,error:void 0,models:[],storage:void 0},this.refresh()}getState=()=>this.#t;subscribe=e=>(this.#e.add(e),()=>this.#e.delete(e));async selectModel(e){let t=this.#t.selectedModel;this.agent.selectModel(e),this.#i({selectedModel:e,error:void 0,progress:void 0}),t!==e&&await this.#n(e,()=>this.agent.engine(t).unload()),await this.refresh()}async refresh(){let e=this.#t.selectedModel;this.#i({availability:"checking"});let t=await this.#s(this.agent.engine(e));this.#t.selectedModel===e&&this.#i({availability:t})}async refreshModels(){let e=await Promise.all(this.modelIds.map(async i=>{let n=this.agent.engine(i);return{id:i,engineId:n.id,availability:await this.#s(n),canDelete:n.delete!==void 0}})),t=await globalThis.navigator?.storage?.estimate?.();this.#i({models:e,storage:t?{usage:t.usage??0,quota:t.quota??0}:void 0})}async download(){let e=this.#t.selectedModel;this.#i({availability:"downloading",error:void 0}),await this.#n(e,()=>this.agent.engine(e).load({onProgress:t=>{this.#t.selectedModel===e&&this.#i({progress:t})}})),this.#t.selectedModel===e&&(this.#i({progress:void 0}),await this.refresh(),this.#t.models.length>0&&await this.refreshModels())}async deleteModel(e){await this.#n(this.#t.selectedModel,async()=>this.agent.engine(e).delete?.()),await this.refreshModels(),e===this.#t.selectedModel&&await this.refresh()}#i(e){this.#t={...this.#t,...e};for(let t of this.#e)t()}async#n(e,t){try{await t()}catch(i){!y(i)&&this.#t.selectedModel===e&&this.#i({error:p(i)})}}async#s(e){try{return await e.availability()}catch(t){return this.#i({error:p(t)}),"unavailable"}}};function p(s){return s instanceof Error?s.message:String(s)}var c=class{#e;#t;constructor(e,t){if(!(t in e))throw new Error(`defaultModel "${t}" is not one of the registered models.`);this.#e=e,this.#t=t}get modelIds(){return Object.keys(this.#e)}get selectedModel(){return this.#t}selectModel(e){if(!this.has(e))throw new Error(`Unknown model "${e}".`);this.#t=e}has(e){return Object.hasOwn(this.#e,e)}engine(e=this.#t){if(!this.has(e))throw new Error(`Unknown model "${e}".`);return this.#e[e]}};export{M as AsyncQueue,m as EngineKind,h as ModelStore,c as Models,d as OutputParser,k as SAMPLING_TEMPERATURE,b as untilAborted};
package/lib/abort.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Waits for a shared promise, but stops waiting when `signal` aborts. The work itself goes on
3
+ * (a model download is shared by every caller), only this caller gives up.
4
+ */
5
+ export declare function untilAborted<T>(promise: Promise<T>, signal: AbortSignal | undefined): Promise<T>;
6
+ /**
7
+ * True for the error of a stopped operation: an aborted signal, or a load that ended because
8
+ * the model was unloaded. Nothing failed, so it is not shown to the user.
9
+ */
10
+ export declare function isAbortError(error: unknown): boolean;
@@ -0,0 +1,12 @@
1
+ /** Turns callbacks into an async iterable: push values in, read them with `for await`. */
2
+ export declare class AsyncQueue<T> implements AsyncIterable<T> {
3
+ #private;
4
+ /** Adds a value for the reader. */
5
+ push(item: T): void;
6
+ /** Ends the stream after the values pushed so far. */
7
+ close(): void;
8
+ /** Ends the stream with `error`: the reader throws it after the values pushed so far. */
9
+ fail(error: unknown): void;
10
+ /** Reads the values with `for await`. Read once. */
11
+ [Symbol.asyncIterator](): AsyncGenerator<T>;
12
+ }
@@ -0,0 +1,107 @@
1
+ /** The engines of this library. `engine.kind` tells which one an engine is. */
2
+ export declare enum EngineKind {
3
+ PromptApi = "prompt-api",
4
+ WebLlm = "web-llm",
5
+ TransformersJs = "transformers"
6
+ }
7
+ /** Whether a model can be used right now, needs a download first, or cannot run on this device. */
8
+ export type Availability = 'available' | 'downloadable' | 'downloading' | 'unavailable';
9
+ /** One message in the plain chat format that engines understand. */
10
+ export interface ChatMessage {
11
+ readonly role: 'system' | 'user' | 'assistant';
12
+ readonly content: string;
13
+ }
14
+ /** A JSON Schema object, as used for tool arguments. */
15
+ export type JsonSchema = Readonly<Record<string, unknown>>;
16
+ /** Options of `Engine.load()`. */
17
+ export interface LoadOptions {
18
+ /** Download or load progress from 0 to 1. */
19
+ readonly onProgress?: (fraction: number) => void;
20
+ readonly signal?: AbortSignal;
21
+ }
22
+ /** What `Engine.generate()` gets. */
23
+ export interface GenerateRequest {
24
+ readonly messages: readonly ChatMessage[];
25
+ /** Force the output to match this JSON Schema. Only used when `supportsConstraint` is true. */
26
+ readonly constraint?: JsonSchema;
27
+ readonly signal?: AbortSignal;
28
+ }
29
+ /**
30
+ * How freely the model picks its words, from always the likeliest word to very varied. These
31
+ * are the levels of the Prompt API; WebLLM and transformers.js turn them into a temperature.
32
+ */
33
+ export type Sampling = 'most-predictable' | 'predictable' | 'slightly-predictable' | 'balanced' | 'slightly-creative' | 'creative' | 'most-creative';
34
+ /** The temperature WebLLM and transformers.js use for each sampling level. */
35
+ export declare const SAMPLING_TEMPERATURE: Readonly<Record<Sampling, number>>;
36
+ /**
37
+ * Settings that mean the same for every engine. Each engine takes the ones it supports, so a
38
+ * setting it cannot honour does not compile, and maps them to its library.
39
+ */
40
+ export interface ModelSettings {
41
+ /**
42
+ * How many tokens fit into the context: system prompt, conversation, tool results and the
43
+ * reply. The agent compacts the conversation to fit it.
44
+ */
45
+ readonly contextWindowSize?: number;
46
+ /** Longest reply in tokens. */
47
+ readonly maxOutputTokens?: number;
48
+ readonly sampling?: Sampling;
49
+ }
50
+ /** Tokens of one model call, as the engine counted them. */
51
+ export interface Usage {
52
+ /** Prompt and reply together: what the context holds after the call. */
53
+ readonly totalTokens: number;
54
+ readonly promptTokens?: number;
55
+ readonly outputTokens?: number;
56
+ }
57
+ /** `stop`: the model ended the reply. `length`: the reply reached `maxOutputTokens`. */
58
+ export type FinishReason = 'stop' | 'length';
59
+ /** A piece of a streamed reply. */
60
+ export type EnginePart = {
61
+ readonly type: 'text';
62
+ readonly text: string;
63
+ }
64
+ /** The last part: why the reply ended and, when the engine knows, the tokens it used. */
65
+ | {
66
+ readonly type: 'finish';
67
+ readonly reason: FinishReason;
68
+ readonly usage?: Usage;
69
+ };
70
+ /**
71
+ * A model running in the browser. Engines only turn chat messages into text:
72
+ * tool calling, repair and the AG-UI protocol are handled by the agent.
73
+ */
74
+ export interface Engine {
75
+ /** Stable id, for example "prompt-api" or "web-llm:Qwen3-1.7B-q4f16_1-MLC". */
76
+ readonly id: string;
77
+ /** Which engine of this library it is. Not set for an engine of your own. */
78
+ readonly kind?: EngineKind;
79
+ /** True when `generate` can force JSON output with `constraint`. */
80
+ readonly supportsConstraint: boolean;
81
+ /**
82
+ * How many tokens the model's context holds, prompt and reply together. The agent's
83
+ * compaction reads it after `load()` and cuts the conversation before it gets that long.
84
+ * Leave it out, or undefined, when the engine does not know: the compaction then leaves the
85
+ * conversation as it is.
86
+ */
87
+ readonly contextWindowSize?: number | undefined;
88
+ /** Whether the model can run here, needs a download, or is not supported. Downloads nothing. */
89
+ availability(): Promise<Availability>;
90
+ /** Downloads and loads the model. Browsers may require a user gesture for this. */
91
+ load(options?: LoadOptions): Promise<void>;
92
+ /**
93
+ * Streams the reply: `text` parts while the model writes, then one `finish` part. A reply
94
+ * without a `finish` part counts as stopped by the model. Throws when `signal` aborts.
95
+ */
96
+ generate(request: GenerateRequest): AsyncIterable<EnginePart>;
97
+ /**
98
+ * Frees the model (memory, GPU buffers, worker) and stops a load in progress; callers
99
+ * waiting for that load are rejected. Downloaded files stay, so the next `load()` is fast.
100
+ */
101
+ unload(): Promise<void>;
102
+ /**
103
+ * Unloads the model and removes its files from browser storage, using the library's own
104
+ * API. Optional because the Prompt API model belongs to the browser: a page cannot delete it.
105
+ */
106
+ delete?(): Promise<void>;
107
+ }
@@ -0,0 +1,44 @@
1
+ /** A piece of model output. */
2
+ export type OutputPart = {
3
+ readonly kind: 'text';
4
+ readonly text: string;
5
+ }
6
+ /** Part of the model's thinking (`<think>…</think>`), streamed as it arrives. */
7
+ | {
8
+ readonly kind: 'reasoning';
9
+ readonly text: string;
10
+ }
11
+ /** The thinking block is closed. */
12
+ | {
13
+ readonly kind: 'reasoning-end';
14
+ }
15
+ /** The raw body so far of a tool call the model is still writing. */
16
+ | {
17
+ readonly kind: 'tool-call-partial';
18
+ readonly raw: string;
19
+ }
20
+ /** The raw body of one tool call, emitted when the call is complete. */
21
+ | {
22
+ readonly kind: 'tool-call';
23
+ readonly raw: string;
24
+ }
25
+ /** A tool call the output ended in the middle of, for example at the token limit. */
26
+ | {
27
+ readonly kind: 'tool-call-cut';
28
+ readonly raw: string;
29
+ };
30
+ /**
31
+ * Splits streamed model output into text, thinking and tool calls. Text is passed on as
32
+ * soon as it cannot be the start of a block, so block syntax never shows up in the chat.
33
+ *
34
+ * The agent reads every reply with it (thinking → `REASONING_*`, tool calls → `TOOL_CALL_*`),
35
+ * and so do the compaction and tool calling, to keep thinking out of summaries and arguments.
36
+ * It is in the core so a chat without AG-UI can split thinking from the answer too.
37
+ */
38
+ export declare class OutputParser {
39
+ #private;
40
+ /** Feeds the next text fragment and returns the parts that are complete. */
41
+ push(fragment: string): OutputPart[];
42
+ /** Call when the stream ends. An unclosed tool call is returned as `tool-call-cut`. */
43
+ end(): OutputPart[];
44
+ }
@@ -0,0 +1,75 @@
1
+ import type { Availability, Engine } from './lib/engine.ts';
2
+ /**
3
+ * What the model store needs from an agent. Every `BrowserModelsAgent` fits, whatever its model ids,
4
+ * so typed agents (`BrowserModelsAgent<{ nano: Engine }>`) can be passed without casts.
5
+ */
6
+ export interface ModelSource {
7
+ readonly modelIds: readonly string[];
8
+ readonly selectedModel: string;
9
+ /** Switches to another registered model. */
10
+ selectModel(modelId: string): void;
11
+ /** The engine of a model, by default the selected one. */
12
+ engine(modelId?: string): Engine;
13
+ }
14
+ /** One registered model and whether it is on this device. */
15
+ export interface ModelStatus {
16
+ readonly id: string;
17
+ /** The engine id, for example "web-llm:Qwen3-0.6B-q4f16_1-MLC". */
18
+ readonly engineId: string;
19
+ /** `available` means downloaded (or, for the Prompt API, installed by the browser). */
20
+ readonly availability: Availability;
21
+ /** True when the app can delete the download. The Prompt API model belongs to the browser. */
22
+ readonly canDelete: boolean;
23
+ }
24
+ /** Browser storage used by this site, in bytes. */
25
+ export interface StorageUse {
26
+ readonly usage: number;
27
+ readonly quota: number;
28
+ }
29
+ /** Everything a UI needs to show about the agent's models. Replaced, never changed in place. */
30
+ export interface ModelState {
31
+ /** The model the chat uses. */
32
+ readonly selectedModel: string;
33
+ /** Whether the selected model can run, needs a download, or is not supported here. */
34
+ readonly availability: Availability | 'checking';
35
+ /** Download progress from 0 to 1 while `download()` runs. */
36
+ readonly progress: number | undefined;
37
+ /** The last error, if any. */
38
+ readonly error: string | undefined;
39
+ /** All registered models and whether they are downloaded. Filled by `refreshModels()`. */
40
+ readonly models: readonly ModelStatus[];
41
+ /** Browser storage used by this site. Filled by `refreshModels()`. */
42
+ readonly storage: StorageUse | undefined;
43
+ }
44
+ /**
45
+ * Framework-neutral model state for one agent. Angular, React and Vue wrap it:
46
+ * `subscribe` + `getState` fit `useSyncExternalStore`, signals and refs alike.
47
+ * The selected model is checked right away.
48
+ */
49
+ export declare class ModelStore {
50
+ #private;
51
+ readonly agent: ModelSource;
52
+ /** The ids of all registered models, for a model picker. */
53
+ readonly modelIds: readonly string[];
54
+ constructor(agent: ModelSource);
55
+ /** The current state. A new object after every change. */
56
+ readonly getState: () => ModelState;
57
+ /** Calls `listener` after every state change. Returns a function that stops it. */
58
+ readonly subscribe: (listener: () => void) => (() => void);
59
+ /**
60
+ * Switches the chat to another registered model and unloads the previous one: models can
61
+ * take gigabytes of GPU memory. There is no automatic fallback.
62
+ */
63
+ selectModel(modelId: string): Promise<void>;
64
+ /** Checks the selected model's availability again. */
65
+ refresh(): Promise<void>;
66
+ /**
67
+ * Checks every registered model. Call it when you show a model list: it loads each
68
+ * engine's library (WebLLM, transformers.js) to ask its cache.
69
+ */
70
+ refreshModels(): Promise<void>;
71
+ /** Downloads and loads the selected model. Call it from a click: browsers may require a user gesture. */
72
+ download(): Promise<void>;
73
+ /** Deletes a downloaded model with its engine's official API. */
74
+ deleteModel(modelId: string): Promise<void>;
75
+ }
package/models.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type { Engine } from './lib/engine.ts';
2
+ import type { ModelSource } from './model-store.ts';
3
+ /**
4
+ * The models an app may use, by id, and the one it uses now. Selection is always explicit:
5
+ * there is no automatic fallback. Pass it to `ModelStore` for a model picker without an agent;
6
+ * `BrowserModelsAgent` keeps its models in one too. Ids are checked by TypeScript.
7
+ *
8
+ * ```ts
9
+ * const models = new Models({ nano: new PromptApiEngine(), gemma }, 'gemma');
10
+ * if ((await models.engine('nano').availability()) !== 'unavailable') models.selectModel('nano');
11
+ * ```
12
+ */
13
+ export declare class Models<Engines extends Readonly<Record<string, Engine>> = Readonly<Record<string, Engine>>> implements ModelSource {
14
+ #private;
15
+ constructor(engines: Engines, defaultModel: keyof Engines & string);
16
+ /** The ids of the registered models. */
17
+ get modelIds(): readonly (keyof Engines & string)[];
18
+ /** The model in use. Starts as `defaultModel`. */
19
+ get selectedModel(): keyof Engines & string;
20
+ /** Switches to another registered model. */
21
+ selectModel(modelId: keyof Engines & string): void;
22
+ /** True when `modelId` is registered, for ids that come from outside, like a request. */
23
+ has(modelId: string): modelId is keyof Engines & string;
24
+ /** The engine of a model (default: the selected one). */
25
+ engine<Id extends keyof Engines & string>(modelId?: Id): Engines[Id];
26
+ }
package/package.json CHANGED
@@ -1,6 +1,69 @@
1
1
  {
2
2
  "name": "@soverius-ai/browser-models",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "main": "./index.js",
6
+ "types": "./index.d.ts",
7
+ "dependencies": {
8
+ "@types/dom-chromium-ai": "^0.0.18"
9
+ },
10
+ "sideEffects": [
11
+ "./web-llm-default.worker.js"
12
+ ],
13
+ "license": "MIT",
14
+ "homepage": "https://browser-models.soverius.ai",
15
+ "description": "Run models in the browser: one engine interface for the Prompt API, WebLLM and transformers.js.",
16
+ "peerDependencies": {
17
+ "@mlc-ai/web-llm": "^0.2.88",
18
+ "@huggingface/transformers": "^4.3.2"
19
+ },
20
+ "peerDependenciesMeta": {
21
+ "@mlc-ai/web-llm": {
22
+ "optional": true
23
+ },
24
+ "@huggingface/transformers": {
25
+ "optional": true
26
+ }
27
+ },
28
+ "exports": {
29
+ "./package.json": "./package.json",
30
+ ".": {
31
+ "types": "./index.d.ts",
32
+ "import": "./index.js",
33
+ "default": "./index.js"
34
+ },
35
+ "./prompt-api": {
36
+ "types": "./prompt-api.d.ts",
37
+ "import": "./prompt-api.js",
38
+ "default": "./prompt-api.js"
39
+ },
40
+ "./web-llm": {
41
+ "types": "./web-llm.d.ts",
42
+ "import": "./web-llm.js",
43
+ "default": "./web-llm.js"
44
+ },
45
+ "./web-llm/worker": {
46
+ "types": "./web-llm-worker.d.ts",
47
+ "import": "./web-llm-worker.js",
48
+ "default": "./web-llm-worker.js"
49
+ },
50
+ "./transformers": {
51
+ "types": "./transformers.d.ts",
52
+ "import": "./transformers.js",
53
+ "default": "./transformers.js"
54
+ },
55
+ "./engines": {
56
+ "types": "./engines.d.ts",
57
+ "import": "./engines.js",
58
+ "default": "./engines.js"
59
+ }
60
+ },
61
+ "repository": {
62
+ "type": "git",
63
+ "url": "git+https://github.com/Soverius-AI/browser-models.git",
64
+ "directory": "libs/agent/browser-models"
65
+ },
66
+ "publishConfig": {
67
+ "access": "public"
68
+ }
6
69
  }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `@soverius-ai/browser-models/prompt-api`: the model built into the browser (Gemini Nano in Chrome, Phi-4-mini in Edge).
3
+ *
4
+ * @module prompt-api
5
+ */
6
+ export * from './engines/prompt-api.ts';
package/prompt-api.js ADDED
@@ -0,0 +1 @@
1
+ import{a as o}from"./chunk-UZW7NPZG.js";import"./chunk-4RAISIWO.js";import"./chunk-T3BB5C4O.js";export{o as PromptApiEngine};
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `@soverius-ai/browser-models/transformers`: ONNX models from the Hugging Face Hub on transformers.js. Needs `@huggingface/transformers`.
3
+ *
4
+ * @module transformers
5
+ */
6
+ export * from './engines/transformers.ts';
@@ -0,0 +1 @@
1
+ import{a as o}from"./chunk-6TJV26R3.js";import"./chunk-EFSLG2O3.js";import"./chunk-4RAISIWO.js";import"./chunk-T3BB5C4O.js";export{o as TransformersJsEngine};
@@ -0,0 +1 @@
1
+ import{a as r}from"./chunk-T5TQFW45.js";r();
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `@soverius-ai/browser-models/web-llm/worker`: the worker file of `WebLlmEngine`. Needs `@mlc-ai/web-llm`.
3
+ *
4
+ * @module web-llm-worker
5
+ */
6
+ export * from './engines/web-llm-worker.ts';
@@ -0,0 +1 @@
1
+ import{a as o}from"./chunk-T5TQFW45.js";export{o as startWebLlmWorker};
package/web-llm.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `@soverius-ai/browser-models/web-llm`: models compiled for WebGPU by WebLLM. Needs `@mlc-ai/web-llm`.
3
+ *
4
+ * @module web-llm
5
+ */
6
+ export * from './engines/web-llm.ts';
package/web-llm.js ADDED
@@ -0,0 +1 @@
1
+ import{a as o}from"./chunk-MPBLCIMW.js";import"./chunk-4RAISIWO.js";import"./chunk-T3BB5C4O.js";export{o as WebLlmEngine};