@soverius-ai/browser-models 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +23 -2
- package/package.json +85 -3
- package/src/agent.d.ts +52 -0
- package/src/agent.js +128 -0
- package/src/agent.js.map +1 -0
- package/src/copilotkit.d.ts +20 -0
- package/src/copilotkit.js +163 -0
- package/src/copilotkit.js.map +1 -0
- package/src/engines/opfs-cache.d.ts +11 -0
- package/src/engines/opfs-cache.js +43 -0
- package/src/engines/opfs-cache.js.map +1 -0
- package/src/engines/prompt-api.d.ts +50 -0
- package/src/engines/prompt-api.js +124 -0
- package/src/engines/prompt-api.js.map +1 -0
- package/src/engines/shared-load.d.ts +20 -0
- package/src/engines/shared-load.js +67 -0
- package/src/engines/shared-load.js.map +1 -0
- package/src/engines/transformers.d.ts +45 -0
- package/src/engines/transformers.js +209 -0
- package/src/engines/transformers.js.map +1 -0
- package/src/engines/web-llm-worker.d.ts +10 -0
- package/src/engines/web-llm-worker.js +15 -0
- package/src/engines/web-llm-worker.js.map +1 -0
- package/src/engines/web-llm.d.ts +41 -0
- package/src/engines/web-llm.js +135 -0
- package/src/engines/web-llm.js.map +1 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +6 -0
- package/src/index.js.map +1 -0
- package/src/lib/abort.d.ts +10 -0
- package/src/lib/abort.js +24 -0
- package/src/lib/abort.js.map +1 -0
- package/src/lib/async-queue.d.ts +8 -0
- package/src/lib/async-queue.js +35 -0
- package/src/lib/async-queue.js.map +1 -0
- package/src/lib/engine.d.ts +45 -0
- package/src/lib/engine.js +2 -0
- package/src/lib/engine.js.map +1 -0
- package/src/lib/parser.d.ts +40 -0
- package/src/lib/parser.js +124 -0
- package/src/lib/parser.js.map +1 -0
- package/src/lib/partial-json.d.ts +21 -0
- package/src/lib/partial-json.js +88 -0
- package/src/lib/partial-json.js.map +1 -0
- package/src/lib/prompt.d.ts +9 -0
- package/src/lib/prompt.js +82 -0
- package/src/lib/prompt.js.map +1 -0
- package/src/lib/repair.d.ts +47 -0
- package/src/lib/repair.js +217 -0
- package/src/lib/repair.js.map +1 -0
- package/src/lib/run.d.ts +40 -0
- package/src/lib/run.js +434 -0
- package/src/lib/run.js.map +1 -0
- package/src/lib/schema.d.ts +21 -0
- package/src/lib/schema.js +128 -0
- package/src/lib/schema.js.map +1 -0
- package/src/lib/tools.d.ts +33 -0
- package/src/lib/tools.js +20 -0
- package/src/lib/tools.js.map +1 -0
- package/src/mcp.d.ts +54 -0
- package/src/mcp.js +204 -0
- package/src/mcp.js.map +1 -0
- package/src/model-store.d.ts +72 -0
- package/src/model-store.js +139 -0
- package/src/model-store.js.map +1 -0
- package/src/prompt-api.d.ts +1 -0
- package/src/prompt-api.js +3 -0
- package/src/prompt-api.js.map +1 -0
- package/src/transformers.d.ts +1 -0
- package/src/transformers.js +3 -0
- package/src/transformers.js.map +1 -0
- package/src/web-llm-worker.d.ts +1 -0
- package/src/web-llm-worker.js +3 -0
- package/src/web-llm-worker.js.map +1 -0
- package/src/web-llm.d.ts +1 -0
- package/src/web-llm.js +3 -0
- package/src/web-llm.js.map +1 -0
- package/src/webmcp.d.ts +43 -0
- package/src/webmcp.js +58 -0
- package/src/webmcp.js.map +1 -0
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,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @soverius-ai/browser-models
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Run language models in the browser (Chrome/Edge Prompt API, WebLLM, transformers.js) as [AG-UI](https://docs.ag-ui.com) agents. CopilotKit or any AG-UI client works unchanged.
|
|
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 { BrowserModelsAgent } from '@soverius-ai/browser-models/agent';
|
|
13
|
+
import { PromptApiEngine } from '@soverius-ai/browser-models/prompt-api';
|
|
14
|
+
|
|
15
|
+
const agent = new BrowserModelsAgent({
|
|
16
|
+
models: { nano: new PromptApiEngine() },
|
|
17
|
+
defaultModel: 'nano',
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Entries: `.`, `/agent`, `/prompt-api`, `/web-llm`, `/web-llm/worker`, `/transformers`, `/webmcp`, `/mcp`, `/copilotkit` ([Packages](https://browser-models.soverius.ai/docs#packages)).
|
|
22
|
+
Framework packages: `@soverius-ai/browser-models-angular`, `@soverius-ai/browser-models-react`, `@soverius-ai/browser-models-vue`.
|
|
23
|
+
|
|
24
|
+
[Docs](https://browser-models.soverius.ai/docs) · [Playground](https://browser-models.soverius.ai/playground) · by [Soverius AI](https://soverius.ai) · MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,88 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soverius-ai/browser-models",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"types": "./src/index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@ag-ui/client": "1.0.2",
|
|
9
|
+
"@ag-ui/core": "1.0.2",
|
|
10
|
+
"jsonrepair": "^3.15.0",
|
|
11
|
+
"rxjs": "7.8.1",
|
|
12
|
+
"tslib": "^2.3.0",
|
|
13
|
+
"zod": "^4.2.0"
|
|
14
|
+
},
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"homepage": "https://browser-models.soverius.ai",
|
|
18
|
+
"description": "Run models in the browser (Prompt API, WebLLM, transformers.js) as AG-UI agents.",
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@mlc-ai/web-llm": "^0.2.88",
|
|
21
|
+
"@huggingface/transformers": "^4.3.2"
|
|
22
|
+
},
|
|
23
|
+
"peerDependenciesMeta": {
|
|
24
|
+
"@mlc-ai/web-llm": {
|
|
25
|
+
"optional": true
|
|
26
|
+
},
|
|
27
|
+
"@huggingface/transformers": {
|
|
28
|
+
"optional": true
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"exports": {
|
|
32
|
+
"./package.json": "./package.json",
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./src/index.d.ts",
|
|
35
|
+
"import": "./src/index.js",
|
|
36
|
+
"default": "./src/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./agent": {
|
|
39
|
+
"types": "./src/agent.d.ts",
|
|
40
|
+
"import": "./src/agent.js",
|
|
41
|
+
"default": "./src/agent.js"
|
|
42
|
+
},
|
|
43
|
+
"./prompt-api": {
|
|
44
|
+
"types": "./src/prompt-api.d.ts",
|
|
45
|
+
"import": "./src/prompt-api.js",
|
|
46
|
+
"default": "./src/prompt-api.js"
|
|
47
|
+
},
|
|
48
|
+
"./web-llm": {
|
|
49
|
+
"types": "./src/web-llm.d.ts",
|
|
50
|
+
"import": "./src/web-llm.js",
|
|
51
|
+
"default": "./src/web-llm.js"
|
|
52
|
+
},
|
|
53
|
+
"./web-llm/worker": {
|
|
54
|
+
"types": "./src/web-llm-worker.d.ts",
|
|
55
|
+
"import": "./src/web-llm-worker.js",
|
|
56
|
+
"default": "./src/web-llm-worker.js"
|
|
57
|
+
},
|
|
58
|
+
"./transformers": {
|
|
59
|
+
"types": "./src/transformers.d.ts",
|
|
60
|
+
"import": "./src/transformers.js",
|
|
61
|
+
"default": "./src/transformers.js"
|
|
62
|
+
},
|
|
63
|
+
"./webmcp": {
|
|
64
|
+
"types": "./src/webmcp.d.ts",
|
|
65
|
+
"import": "./src/webmcp.js",
|
|
66
|
+
"default": "./src/webmcp.js"
|
|
67
|
+
},
|
|
68
|
+
"./mcp": {
|
|
69
|
+
"types": "./src/mcp.d.ts",
|
|
70
|
+
"import": "./src/mcp.js",
|
|
71
|
+
"default": "./src/mcp.js"
|
|
72
|
+
},
|
|
73
|
+
"./copilotkit": {
|
|
74
|
+
"types": "./src/copilotkit.d.ts",
|
|
75
|
+
"import": "./src/copilotkit.js",
|
|
76
|
+
"default": "./src/copilotkit.js"
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"repository": {
|
|
80
|
+
"type": "git",
|
|
81
|
+
"url": "git+https://github.com/Soverius-AI/browser-models.git",
|
|
82
|
+
"directory": "libs/agent/browser-models"
|
|
83
|
+
},
|
|
84
|
+
"publishConfig": {
|
|
85
|
+
"access": "public"
|
|
86
|
+
},
|
|
87
|
+
"module": "./src/index.js"
|
|
6
88
|
}
|
package/src/agent.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { AbstractAgent, type BaseEvent, type RunAgentInput } from '@ag-ui/client';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import type { Engine } from './lib/engine.ts';
|
|
4
|
+
import type { RepairOptions } from './lib/repair.ts';
|
|
5
|
+
import type { LocalTool, LocalToolSource } from './lib/tools.ts';
|
|
6
|
+
export interface BrowserModelsAgentOptions<Models extends Readonly<Record<string, Engine>>> {
|
|
7
|
+
/** The models this agent may use, by id. Selection is always explicit. */
|
|
8
|
+
readonly models: Models;
|
|
9
|
+
/** Model used when a run does not pick one. Must be one of the `models` keys. */
|
|
10
|
+
readonly defaultModel: keyof Models & string;
|
|
11
|
+
readonly agentId?: string;
|
|
12
|
+
readonly description?: string;
|
|
13
|
+
/** Tools that run in the agent: tools, or sources like `new WebMcpTools()` read on every run. */
|
|
14
|
+
readonly localTools?: readonly (LocalTool | LocalToolSource)[];
|
|
15
|
+
readonly maxSteps?: number;
|
|
16
|
+
readonly repairHook?: RepairOptions['hook'];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* An AG-UI agent backed by a model running in the browser. Works with any AG-UI
|
|
20
|
+
* client, for example CopilotKit: `provideCopilotKit({ agents: { default: agent } })`.
|
|
21
|
+
* Model ids are checked by TypeScript.
|
|
22
|
+
*
|
|
23
|
+
* A run can pick a model with `forwardedProps: { soverius: { model: 'id' } }`.
|
|
24
|
+
* An unknown id fails the run; there is no automatic fallback.
|
|
25
|
+
*/
|
|
26
|
+
export declare class BrowserModelsAgent<Models extends Readonly<Record<string, Engine>> = Readonly<Record<string, Engine>>> extends AbstractAgent {
|
|
27
|
+
private options;
|
|
28
|
+
private selection;
|
|
29
|
+
private abortController;
|
|
30
|
+
constructor(options: BrowserModelsAgentOptions<Models>);
|
|
31
|
+
/** The model runs use unless a run picks one. Starts as `defaultModel`. */
|
|
32
|
+
get selectedModel(): keyof Models & string;
|
|
33
|
+
/** Switches the model for the next runs. Only registered ids are accepted. */
|
|
34
|
+
selectModel(modelId: keyof Models & string): void;
|
|
35
|
+
/** The ids of the registered models. */
|
|
36
|
+
get modelIds(): readonly (keyof Models & string)[];
|
|
37
|
+
/** The engine for a model id (default: the selected model), for availability or downloads. */
|
|
38
|
+
engine(modelId?: keyof Models & string): Engine;
|
|
39
|
+
clone(): BrowserModelsAgent<Models>;
|
|
40
|
+
/** Stops the running model and tools, like `HttpAgent.abortRun()`. Clients call it on "stop". */
|
|
41
|
+
abortRun(): void;
|
|
42
|
+
/** Sends every event of this agent to the soverius debugger. Returns a function that stops it. */
|
|
43
|
+
connectDevtools(target?: Window | undefined): () => void;
|
|
44
|
+
run(input: RunAgentInput): Observable<BaseEvent>;
|
|
45
|
+
}
|
|
46
|
+
/** Message format the soverius debugger listens for on `window`. */
|
|
47
|
+
export interface DevtoolsMessage {
|
|
48
|
+
readonly source: 'soverius-ai/devtools';
|
|
49
|
+
readonly agentId: string;
|
|
50
|
+
readonly event: BaseEvent;
|
|
51
|
+
}
|
|
52
|
+
export { ModelStore, type ModelState, type ModelStatus, type ModelSource, type StorageUse, } from './model-store.ts';
|
package/src/agent.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { AbstractAgent, } from '@ag-ui/client';
|
|
2
|
+
import { EventType } from '@ag-ui/core';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { AgentRun } from "./lib/run.js";
|
|
6
|
+
/**
|
|
7
|
+
* An AG-UI agent backed by a model running in the browser. Works with any AG-UI
|
|
8
|
+
* client, for example CopilotKit: `provideCopilotKit({ agents: { default: agent } })`.
|
|
9
|
+
* Model ids are checked by TypeScript.
|
|
10
|
+
*
|
|
11
|
+
* A run can pick a model with `forwardedProps: { soverius: { model: 'id' } }`.
|
|
12
|
+
* An unknown id fails the run; there is no automatic fallback.
|
|
13
|
+
*/
|
|
14
|
+
export class BrowserModelsAgent extends AbstractAgent {
|
|
15
|
+
// TypeScript-private fields, not #private: AbstractAgent.clone() copies via Object.create.
|
|
16
|
+
options;
|
|
17
|
+
// Shared with clones, so selectModel() also reaches the copies a client like CopilotKit makes.
|
|
18
|
+
selection;
|
|
19
|
+
// The current run's controller, for abortRun(). Each clone gets its own on its first run.
|
|
20
|
+
abortController;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
super({
|
|
23
|
+
...(options.agentId ? { agentId: options.agentId } : {}),
|
|
24
|
+
description: options.description ?? 'An agent running on a model in the browser',
|
|
25
|
+
});
|
|
26
|
+
if (!(options.defaultModel in options.models)) {
|
|
27
|
+
throw new Error(`defaultModel "${options.defaultModel}" is not one of the registered models.`);
|
|
28
|
+
}
|
|
29
|
+
this.options = options;
|
|
30
|
+
this.selection = { current: options.defaultModel };
|
|
31
|
+
}
|
|
32
|
+
/** The model runs use unless a run picks one. Starts as `defaultModel`. */
|
|
33
|
+
get selectedModel() {
|
|
34
|
+
return this.selection.current;
|
|
35
|
+
}
|
|
36
|
+
/** Switches the model for the next runs. Only registered ids are accepted. */
|
|
37
|
+
selectModel(modelId) {
|
|
38
|
+
if (!(modelId in this.options.models))
|
|
39
|
+
throw new Error(`Unknown model "${modelId}".`);
|
|
40
|
+
this.selection.current = modelId;
|
|
41
|
+
}
|
|
42
|
+
/** The ids of the registered models. */
|
|
43
|
+
get modelIds() {
|
|
44
|
+
return Object.keys(this.options.models);
|
|
45
|
+
}
|
|
46
|
+
/** The engine for a model id (default: the selected model), for availability or downloads. */
|
|
47
|
+
engine(modelId = this.selection.current) {
|
|
48
|
+
const engine = this.options.models[modelId];
|
|
49
|
+
if (!engine)
|
|
50
|
+
throw new Error(`Unknown model "${modelId}".`);
|
|
51
|
+
return engine;
|
|
52
|
+
}
|
|
53
|
+
clone() {
|
|
54
|
+
const copy = super.clone();
|
|
55
|
+
copy.options = this.options;
|
|
56
|
+
copy.selection = this.selection;
|
|
57
|
+
return copy;
|
|
58
|
+
}
|
|
59
|
+
/** Stops the running model and tools, like `HttpAgent.abortRun()`. Clients call it on "stop". */
|
|
60
|
+
abortRun() {
|
|
61
|
+
this.abortController?.abort();
|
|
62
|
+
super.abortRun();
|
|
63
|
+
}
|
|
64
|
+
/** Sends every event of this agent to the soverius debugger. Returns a function that stops it. */
|
|
65
|
+
connectDevtools(target = globalThis.window) {
|
|
66
|
+
// No window on the server (SSR): nothing to connect to.
|
|
67
|
+
if (!target)
|
|
68
|
+
return () => undefined;
|
|
69
|
+
const { unsubscribe } = this.subscribe({
|
|
70
|
+
onEvent: ({ event }) => {
|
|
71
|
+
const message = {
|
|
72
|
+
source: 'soverius-ai/devtools',
|
|
73
|
+
agentId: this.agentId ?? 'default',
|
|
74
|
+
event,
|
|
75
|
+
};
|
|
76
|
+
// '/' means "same origin as this page" and also works for opaque origins like file://.
|
|
77
|
+
target.postMessage(message, '/');
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
return unsubscribe;
|
|
81
|
+
}
|
|
82
|
+
run(input) {
|
|
83
|
+
return new Observable((subscriber) => {
|
|
84
|
+
const abort = new AbortController();
|
|
85
|
+
this.abortController = abort;
|
|
86
|
+
const modelId = modelFromInput(input) ?? this.selection.current;
|
|
87
|
+
const engine = this.options.models[modelId];
|
|
88
|
+
const stream = engine
|
|
89
|
+
? new AgentRun({
|
|
90
|
+
engine,
|
|
91
|
+
input,
|
|
92
|
+
signal: abort.signal,
|
|
93
|
+
...(this.options.localTools
|
|
94
|
+
? { localTools: this.options.localTools }
|
|
95
|
+
: {}),
|
|
96
|
+
...(this.options.maxSteps
|
|
97
|
+
? { maxSteps: this.options.maxSteps }
|
|
98
|
+
: {}),
|
|
99
|
+
...(this.options.repairHook
|
|
100
|
+
? { repairHook: this.options.repairHook }
|
|
101
|
+
: {}),
|
|
102
|
+
}).events()
|
|
103
|
+
: unknownModel(input, modelId);
|
|
104
|
+
(async () => {
|
|
105
|
+
for await (const event of stream)
|
|
106
|
+
subscriber.next(event);
|
|
107
|
+
subscriber.complete();
|
|
108
|
+
})().catch((error) => subscriber.error(error));
|
|
109
|
+
return () => abort.abort();
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const forwardedModel = z.object({ soverius: z.object({ model: z.string() }) });
|
|
114
|
+
/** The model a run picked with `forwardedProps: { soverius: { model } }`, if any. */
|
|
115
|
+
function modelFromInput(input) {
|
|
116
|
+
return forwardedModel.safeParse(input.forwardedProps).data?.soverius.model;
|
|
117
|
+
}
|
|
118
|
+
async function* unknownModel(input, modelId) {
|
|
119
|
+
yield {
|
|
120
|
+
type: EventType.RUN_STARTED,
|
|
121
|
+
threadId: input.threadId,
|
|
122
|
+
runId: input.runId,
|
|
123
|
+
};
|
|
124
|
+
yield { type: EventType.RUN_ERROR, message: `Unknown model "${modelId}".` };
|
|
125
|
+
}
|
|
126
|
+
// The model store is part of the agent entry point.
|
|
127
|
+
export { ModelStore, } from "./model-store.js";
|
|
128
|
+
//# sourceMappingURL=agent.js.map
|
package/src/agent.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../../../../libs/agent/browser-models/src/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,GAGd,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,SAAS,EAA2B,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAkBxC;;;;;;;GAOG;AACH,MAAM,OAAO,kBAIX,SAAQ,aAAa;IACrB,2FAA2F;IACnF,OAAO,CAAoC;IACnD,+FAA+F;IACvF,SAAS,CAAqC;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AM,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;AAE/E,qFAAqF;AACrF,SAAS,cAAc,CAAC,KAAoB;IAC1C,OAAO,cAAc,CAAC,SAAS,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC;AAC7E,CAAC;AAED,KAAK,SAAS,CAAC,CAAC,YAAY,CAC1B,KAAoB,EACpB,OAAe;IAEf,MAAM;QACJ,IAAI,EAAE,SAAS,CAAC,WAAW;QAC3B,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,KAAK,EAAE,KAAK,CAAC,KAAK;KACnB,CAAC;IACF,MAAM,EAAE,IAAI,EAAE,SAAS,CAAC,SAAS,EAAE,OAAO,EAAE,kBAAkB,OAAO,IAAI,EAAE,CAAC;AAC9E,CAAC;AAED,oDAAoD;AACpD,OAAO,EACL,UAAU,GAKX,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Middleware, type AbstractAgent, type BaseEvent, type RunAgentInput } from '@ag-ui/client';
|
|
2
|
+
import { type Observable } from 'rxjs';
|
|
3
|
+
/**
|
|
4
|
+
* CopilotKit's Open Generative UI for an agent that runs in the browser.
|
|
5
|
+
*
|
|
6
|
+
* The model calls `generateSandboxedUi` with CSS, HTML and JavaScript. CopilotKit shows that in a
|
|
7
|
+
* sandboxed iframe (JetBrains websandbox) from `open-generative-ui` activity messages. Its
|
|
8
|
+
* server runtime normally writes those messages; that code needs Node, so this middleware does
|
|
9
|
+
* the same job in the page: while the tool call streams, it reports what has arrived.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { OpenGenerativeUi } from '@soverius-ai/browser-models/copilotkit';
|
|
13
|
+
*
|
|
14
|
+
* agent.use(new OpenGenerativeUi());
|
|
15
|
+
* // and switch the feature on in CopilotKit: `openGenerativeUI: {}` on the provider
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class OpenGenerativeUi extends Middleware {
|
|
19
|
+
run(input: RunAgentInput, next: AbstractAgent): Observable<BaseEvent>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { EventType, Middleware, } from '@ag-ui/client';
|
|
2
|
+
import { concatMap, from } from 'rxjs';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { closeJson } from "./lib/partial-json.js";
|
|
5
|
+
/** The tool CopilotKit registers when `openGenerativeUI` is set, and the activity it renders. */
|
|
6
|
+
const TOOL_NAME = 'generateSandboxedUi';
|
|
7
|
+
const ACTIVITY_TYPE = 'open-generative-ui';
|
|
8
|
+
/** The tool calls in a `MESSAGES_SNAPSHOT`, as far as this module reads them. */
|
|
9
|
+
const correctedCalls = z.object({
|
|
10
|
+
messages: z.array(z.object({
|
|
11
|
+
toolCalls: z
|
|
12
|
+
.array(z.object({
|
|
13
|
+
id: z.string(),
|
|
14
|
+
function: z.object({ arguments: z.string() }),
|
|
15
|
+
}))
|
|
16
|
+
.optional(),
|
|
17
|
+
})),
|
|
18
|
+
});
|
|
19
|
+
/** What the model has written of the tool's arguments so far. */
|
|
20
|
+
const sandboxArguments = z
|
|
21
|
+
.object({
|
|
22
|
+
initialHeight: z.number(),
|
|
23
|
+
css: z.string(),
|
|
24
|
+
html: z.string(),
|
|
25
|
+
jsFunctions: z.string(),
|
|
26
|
+
jsExpressions: z.array(z.string()),
|
|
27
|
+
})
|
|
28
|
+
.partial();
|
|
29
|
+
/**
|
|
30
|
+
* CopilotKit's Open Generative UI for an agent that runs in the browser.
|
|
31
|
+
*
|
|
32
|
+
* The model calls `generateSandboxedUi` with CSS, HTML and JavaScript. CopilotKit shows that in a
|
|
33
|
+
* sandboxed iframe (JetBrains websandbox) from `open-generative-ui` activity messages. Its
|
|
34
|
+
* server runtime normally writes those messages; that code needs Node, so this middleware does
|
|
35
|
+
* the same job in the page: while the tool call streams, it reports what has arrived.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { OpenGenerativeUi } from '@soverius-ai/browser-models/copilotkit';
|
|
39
|
+
*
|
|
40
|
+
* agent.use(new OpenGenerativeUi());
|
|
41
|
+
* // and switch the feature on in CopilotKit: `openGenerativeUI: {}` on the provider
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export class OpenGenerativeUi extends Middleware {
|
|
45
|
+
run(input, next) {
|
|
46
|
+
const calls = new Map();
|
|
47
|
+
return this.runNext(input, next).pipe(concatMap((event) => from(withActivity(event, calls))));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** The event, with an activity update next to it when it belongs to a sandbox tool call. */
|
|
51
|
+
function withActivity(event, calls) {
|
|
52
|
+
const { type, toolCallId, toolCallName, delta } = event;
|
|
53
|
+
if (type === EventType.MESSAGES_SNAPSHOT) {
|
|
54
|
+
// The agent corrects a streamed call this way after repairing it: show the corrected page.
|
|
55
|
+
const corrected = correctedCalls.safeParse(event).data?.messages ?? [];
|
|
56
|
+
const updates = corrected
|
|
57
|
+
.flatMap((message) => message.toolCalls ?? [])
|
|
58
|
+
.flatMap((toolCall) => {
|
|
59
|
+
const open = calls.get(toolCall.id);
|
|
60
|
+
open?.replace(toolCall.function.arguments);
|
|
61
|
+
return open ? [open.activity(false)] : [];
|
|
62
|
+
});
|
|
63
|
+
return [event, ...updates];
|
|
64
|
+
}
|
|
65
|
+
if (!toolCallId)
|
|
66
|
+
return [event];
|
|
67
|
+
if (type === EventType.TOOL_CALL_START && toolCallName === TOOL_NAME) {
|
|
68
|
+
const call = new SandboxCall(toolCallId);
|
|
69
|
+
calls.set(toolCallId, call);
|
|
70
|
+
// The activity goes first, so the sandbox sits above the tool call in the chat.
|
|
71
|
+
return [call.activity(false), event];
|
|
72
|
+
}
|
|
73
|
+
const call = calls.get(toolCallId);
|
|
74
|
+
if (!call)
|
|
75
|
+
return [event];
|
|
76
|
+
if (type === EventType.TOOL_CALL_ARGS) {
|
|
77
|
+
call.write(delta ?? '');
|
|
78
|
+
return [event, call.activity(false)];
|
|
79
|
+
}
|
|
80
|
+
if (type === EventType.TOOL_CALL_END) {
|
|
81
|
+
calls.delete(toolCallId);
|
|
82
|
+
return [call.activity(true), event];
|
|
83
|
+
}
|
|
84
|
+
return [event];
|
|
85
|
+
}
|
|
86
|
+
/** The arguments of one `generateSandboxedUi` call while they stream in. */
|
|
87
|
+
class SandboxCall {
|
|
88
|
+
#messageId;
|
|
89
|
+
#json = '';
|
|
90
|
+
#written = {};
|
|
91
|
+
/** The HTML in the pieces it arrived in: the renderer appends each new piece. */
|
|
92
|
+
#html = [];
|
|
93
|
+
#htmlLength = 0;
|
|
94
|
+
constructor(toolCallId) {
|
|
95
|
+
this.#messageId = `${toolCallId}-activity`;
|
|
96
|
+
}
|
|
97
|
+
write(delta) {
|
|
98
|
+
this.#json += delta;
|
|
99
|
+
}
|
|
100
|
+
/** Replaces what was streamed with the complete, repaired arguments. */
|
|
101
|
+
replace(json) {
|
|
102
|
+
this.#json = json;
|
|
103
|
+
const html = sandboxArguments.safeParse(this.#read()).data?.html ?? '';
|
|
104
|
+
// The pieces shown so far only stay when the repaired HTML continues them.
|
|
105
|
+
if (!html.startsWith(this.#html.join(''))) {
|
|
106
|
+
this.#html.length = 0;
|
|
107
|
+
this.#htmlLength = 0;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** The activity message with everything that has arrived. `done`: the call is complete. */
|
|
111
|
+
activity(done) {
|
|
112
|
+
const written = this.#read();
|
|
113
|
+
const args = sandboxArguments.safeParse(written).data ?? {};
|
|
114
|
+
// The model writes the parameters in order: all but the last one are finished.
|
|
115
|
+
const keys = Object.keys(written);
|
|
116
|
+
const finished = (key) => keys.includes(key) && (done || key !== keys.at(-1));
|
|
117
|
+
const html = args.html ?? '';
|
|
118
|
+
if (html.length > this.#htmlLength) {
|
|
119
|
+
this.#html.push(html.slice(this.#htmlLength));
|
|
120
|
+
this.#htmlLength = html.length;
|
|
121
|
+
}
|
|
122
|
+
const expressions = args.jsExpressions ?? [];
|
|
123
|
+
return {
|
|
124
|
+
type: EventType.ACTIVITY_SNAPSHOT,
|
|
125
|
+
messageId: this.#messageId,
|
|
126
|
+
activityType: ACTIVITY_TYPE,
|
|
127
|
+
content: {
|
|
128
|
+
initialHeight: args.initialHeight,
|
|
129
|
+
generating: !done,
|
|
130
|
+
...(finished('css') && { css: args.css, cssComplete: true }),
|
|
131
|
+
...(this.#html.length > 0 && { html: [...this.#html] }),
|
|
132
|
+
...(finished('html') && { htmlComplete: true }),
|
|
133
|
+
...(finished('jsFunctions') && {
|
|
134
|
+
jsFunctions: args.jsFunctions,
|
|
135
|
+
jsFunctionsComplete: true,
|
|
136
|
+
}),
|
|
137
|
+
// An expression runs once it is complete: the last one may still be growing.
|
|
138
|
+
...(keys.includes('jsExpressions') && {
|
|
139
|
+
jsExpressions: finished('jsExpressions')
|
|
140
|
+
? expressions
|
|
141
|
+
: expressions.slice(0, -1),
|
|
142
|
+
}),
|
|
143
|
+
...(finished('jsExpressions') && { jsExpressionsComplete: true }),
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The arguments object so far. The JSON is cut off mid-way while the call streams; when a cut
|
|
149
|
+
* cannot be read, the last readable state stays.
|
|
150
|
+
*/
|
|
151
|
+
#read() {
|
|
152
|
+
try {
|
|
153
|
+
this.#written = z
|
|
154
|
+
.record(z.string(), z.unknown())
|
|
155
|
+
.parse(JSON.parse(closeJson(this.#json).json || '{}'));
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
// Keep what was readable before.
|
|
159
|
+
}
|
|
160
|
+
return this.#written;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=copilotkit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copilotkit.js","sourceRoot":"","sources":["../../../../../libs/agent/browser-models/src/copilotkit.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT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AC,QAAQ,CAAC;IACvB,CAAC;CACF"}
|
|
@@ -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,43 @@
|
|
|
1
|
+
export function opfsCache(directoryName = 'soverius-transformers', root = () => navigator.storage.getDirectory()) {
|
|
2
|
+
const directory = async () => (await root()).getDirectoryHandle(directoryName, { create: true });
|
|
3
|
+
return {
|
|
4
|
+
async match(key) {
|
|
5
|
+
try {
|
|
6
|
+
const handle = await (await directory()).getFileHandle(await fileName(key));
|
|
7
|
+
return new Response(await handle.getFile());
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
async put(key, response) {
|
|
14
|
+
if (!response.body)
|
|
15
|
+
return;
|
|
16
|
+
const name = await fileName(key);
|
|
17
|
+
const folder = await directory();
|
|
18
|
+
const handle = await folder.getFileHandle(name, { create: true });
|
|
19
|
+
try {
|
|
20
|
+
await response.body.pipeTo(await handle.createWritable());
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
// Never leave a half-written file that would look cached.
|
|
24
|
+
await folder.removeEntry(name).catch(() => undefined);
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
async delete(key) {
|
|
29
|
+
try {
|
|
30
|
+
await (await directory()).removeEntry(await fileName(key));
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
async function fileName(key) {
|
|
40
|
+
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(key));
|
|
41
|
+
return Array.from(new Uint8Array(hash), (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=opfs-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"opfs-cache.js","sourceRoot":"","sources":["../../../../../../libs/agent/browser-models/src/engines/opfs-cache.ts"],"names":[],"mappings":"AAWA,MAAM,UAAU,SAAS,CACvB,aAAa,GAAG,uBAAuB,EACvC,OAAiD,GAAG,EAAE,CACpD,SAAS,CAAC,OAAO,CAAC,YAAY,EAAE;IAElC,MAAM,SAAS,GAAG,KAAK,IAAI,EAAE,CAC3B,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAErE,OAAO;QACL,KAAK,CAAC,KAAK,CAAC,GAAG;YACb,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,MAAM,CACnB,MAAM,SAAS,EAAE,CAClB,CAAC,aAAa,CAAC,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;gBACrC,OAAO,IAAI,QAAQ,CAAC,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;YAC9C,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,SAAS,CAAC;YACnB,CAAC;QACH,CAAC;QAED,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ;YACrB,IAAI,CAAC,QAAQ,CAAC,IAAI;gBAAE,OAAO;YAC3B,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAC;YACjC,MAAM,MAAM,GAAG,MAAM,SAAS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
|
+
/** The part of the browser's built-in `LanguageModel` API (Chrome, Edge) this engine uses. */
|
|
3
|
+
export interface LanguageModelApi {
|
|
4
|
+
availability(options?: LanguageModelExpectations): Promise<Availability>;
|
|
5
|
+
create(options?: LanguageModelExpectations & {
|
|
6
|
+
initialPrompts?: readonly ChatMessage[];
|
|
7
|
+
signal?: AbortSignal;
|
|
8
|
+
monitor?: (monitor: EventTarget) => void;
|
|
9
|
+
}): Promise<LanguageModelSession>;
|
|
10
|
+
}
|
|
11
|
+
/** The languages a session reads and writes. Chrome checks support for them up front. */
|
|
12
|
+
export interface LanguageModelExpectations {
|
|
13
|
+
expectedInputs?: readonly {
|
|
14
|
+
type: 'text';
|
|
15
|
+
languages: readonly string[];
|
|
16
|
+
}[];
|
|
17
|
+
expectedOutputs?: readonly {
|
|
18
|
+
type: 'text';
|
|
19
|
+
languages: readonly string[];
|
|
20
|
+
}[];
|
|
21
|
+
}
|
|
22
|
+
export interface LanguageModelSession {
|
|
23
|
+
promptStreaming(input: string, options?: {
|
|
24
|
+
signal?: AbortSignal;
|
|
25
|
+
responseConstraint?: unknown;
|
|
26
|
+
}): ReadableStream<string>;
|
|
27
|
+
destroy(): void;
|
|
28
|
+
}
|
|
29
|
+
export interface PromptApiOptions {
|
|
30
|
+
/** Languages of the conversation, for example `['en', 'de']`. Default `['en']`. */
|
|
31
|
+
readonly languages?: readonly string[];
|
|
32
|
+
/** The API to use. Defaults to the browser's global `LanguageModel`. */
|
|
33
|
+
readonly languageModel?: LanguageModelApi;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Engine for the model built into the browser: Gemini Nano in Chrome, Phi-4-mini in Edge.
|
|
37
|
+
* The browser downloads, stores and removes the model itself, so there is no `delete()`.
|
|
38
|
+
* `load()` may need a user gesture.
|
|
39
|
+
*/
|
|
40
|
+
export declare class PromptApiEngine implements Engine {
|
|
41
|
+
#private;
|
|
42
|
+
readonly id = "prompt-api";
|
|
43
|
+
readonly supportsConstraint = true;
|
|
44
|
+
constructor(options?: PromptApiOptions);
|
|
45
|
+
availability(): Promise<Availability>;
|
|
46
|
+
load(options?: LoadOptions): Promise<void>;
|
|
47
|
+
/** Stops a download this page is waiting for and destroys the kept session. */
|
|
48
|
+
unload(): Promise<void>;
|
|
49
|
+
generate({ messages, constraint, signal, }: GenerateRequest): AsyncGenerator<string>;
|
|
50
|
+
}
|