@dudko.dev/agent-web 0.0.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.
- package/LICENSE +21 -0
- package/README.md +176 -0
- package/dist/chunk-R7H4HSYT.js +22 -0
- package/dist/chunk-R7H4HSYT.js.map +1 -0
- package/dist/index.cjs +1407 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +835 -0
- package/dist/index.d.ts +835 -0
- package/dist/index.js +1298 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp.cjs +111 -0
- package/dist/mcp.cjs.map +1 -0
- package/dist/mcp.d.cts +47 -0
- package/dist/mcp.d.ts +47 -0
- package/dist/mcp.js +83 -0
- package/dist/mcp.js.map +1 -0
- package/dist/webllm-IM3UD6YI.js +9 -0
- package/dist/webllm-IM3UD6YI.js.map +1 -0
- package/package.json +152 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Siarhei Dudko
|
|
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
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# @dudko.dev/agent-web
|
|
2
|
+
|
|
3
|
+
Headless, **universal**, configurable **in-browser** LLM agent built on the
|
|
4
|
+
[Vercel AI SDK](https://ai-sdk.dev). One package drives **any** model from the
|
|
5
|
+
browser:
|
|
6
|
+
|
|
7
|
+
- ☁️ **Cloud providers, bring-your-own-key** — OpenAI, Anthropic, Google, xAI,
|
|
8
|
+
DeepSeek, any OpenAI-compatible server, and the Vercel AI Gateway.
|
|
9
|
+
- 🖥️ **Local WebGPU models** — via [WebLLM](https://github.com/mlc-ai/web-llm)
|
|
10
|
+
(`@browser-ai/web-llm`); no server, no key, data never leaves the device.
|
|
11
|
+
- 🔌 **Any AI SDK `LanguageModel`** — e.g. Chrome/Edge built-in AI.
|
|
12
|
+
|
|
13
|
+
It gives you **simple text generation, streaming, native tool-calling, and
|
|
14
|
+
structured output**, plus a high-level **plan → execute → replan → synthesize**
|
|
15
|
+
agent — with host-defined **tools**, optional **MCP**, **system prompts**,
|
|
16
|
+
**IndexedDB** storage, and an **encrypted token vault**. UI-agnostic: it streams
|
|
17
|
+
typed events; you render them however you like.
|
|
18
|
+
|
|
19
|
+
> Browser sibling of the Node package
|
|
20
|
+
> [`@dudko.dev/agent`](https://www.npmjs.com/package/@dudko.dev/agent).
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @dudko.dev/agent-web ai zod
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then add **only the providers you use** (all optional peers, dynamically
|
|
29
|
+
imported):
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# cloud, pick what you need
|
|
33
|
+
npm install @ai-sdk/openai # or @ai-sdk/anthropic, @ai-sdk/google, @ai-sdk/xai, @ai-sdk/deepseek, @ai-sdk/openai-compatible
|
|
34
|
+
# local WebGPU models
|
|
35
|
+
npm install @browser-ai/web-llm @mlc-ai/web-llm
|
|
36
|
+
# optional: HTTP MCP
|
|
37
|
+
npm install @modelcontextprotocol/sdk
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Quick start — cloud model, bring-your-own-key
|
|
41
|
+
|
|
42
|
+
Keys are stored **encrypted at rest** (WebCrypto, IndexedDB) and referenced by
|
|
43
|
+
id — never inlined into config. See [security](docs/security.md).
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import {
|
|
47
|
+
createAgent,
|
|
48
|
+
defineTool,
|
|
49
|
+
VaultCredentialStore,
|
|
50
|
+
} from '@dudko.dev/agent-web'
|
|
51
|
+
import { z } from 'zod'
|
|
52
|
+
|
|
53
|
+
// 1. Store the user's key once (e.g. from a settings form). Encrypted at rest.
|
|
54
|
+
const credentials = new VaultCredentialStore()
|
|
55
|
+
await credentials.setApiKey('openai', userProvidedKey)
|
|
56
|
+
|
|
57
|
+
// 2. Tools = your app's actions.
|
|
58
|
+
const tools = {
|
|
59
|
+
add_text: defineTool({
|
|
60
|
+
description: 'Add a text block to the page.',
|
|
61
|
+
inputSchema: z.object({ text: z.string(), x: z.number().optional() }),
|
|
62
|
+
execute: async ({ text, x }) => addTextBlock(text, x), // your code
|
|
63
|
+
}),
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 3. Create the agent. The key is fetched from the vault at build time.
|
|
67
|
+
const agent = await createAgent({
|
|
68
|
+
model: { providerType: 'openai', model: 'gpt-4o-mini', credentialRef: 'openai' },
|
|
69
|
+
credentials,
|
|
70
|
+
tools,
|
|
71
|
+
describeState: () => serializeMyCanvas(), // optional grounding
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
// 4. Run. Every step is a typed event.
|
|
75
|
+
const result = await agent.run('Add a centered title and a totals line', {
|
|
76
|
+
onEvent: (e) => {
|
|
77
|
+
if (e.type === 'plan.created') console.log('plan:', e.plan.steps)
|
|
78
|
+
if (e.type === 'step.tool-call') console.log('tool:', e.name, e.input)
|
|
79
|
+
if (e.type === 'final') console.log('done:', e.text)
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
console.log(result.final, `— ${result.applied} changes`)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
> **Direct browser calls & CORS:** not every provider allows direct BYOK calls
|
|
86
|
+
> from a browser origin. Google (Gemini) and openai-compatible/gateway are the
|
|
87
|
+
> reliable direct paths; Anthropic works (a required header is injected for you);
|
|
88
|
+
> OpenAI/xAI/DeepSeek usually need a proxy. See
|
|
89
|
+
> [docs/providers.md](docs/providers.md).
|
|
90
|
+
|
|
91
|
+
## Quick start — local WebGPU model (no key, offline)
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { createAgent, createWebLLMModel, isWebGPUAvailable } from '@dudko.dev/agent-web'
|
|
95
|
+
|
|
96
|
+
if (!isWebGPUAvailable()) throw new Error('WebGPU required for local models')
|
|
97
|
+
|
|
98
|
+
const model = await createWebLLMModel('Llama-3.2-3B-Instruct-q4f16_1-MLC', {
|
|
99
|
+
initProgressCallback: (r) => console.log('loading', Math.round(r.progress * 100), '%'),
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
// Local models default to the robust "prompted" tool-mode automatically.
|
|
103
|
+
const agent = await createAgent({ model, tools /* ...same as above */ })
|
|
104
|
+
await agent.run('Summarize the current page and add a heading')
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
You can also pass a model by spec: `{ providerType: 'web-llm', model: '…' }`.
|
|
108
|
+
|
|
109
|
+
## Simple generation & tool-calling (no agent loop)
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { generate, stream, generateStructured, runToolLoop } from '@dudko.dev/agent-web'
|
|
113
|
+
import { z } from 'zod'
|
|
114
|
+
|
|
115
|
+
const { text } = await generate(model, { prompt: 'Write a haiku about WebGPU' })
|
|
116
|
+
|
|
117
|
+
const { textStream } = stream(model, { prompt: 'Explain IndexedDB' })
|
|
118
|
+
for await (const chunk of textStream) process.stdout.write(chunk)
|
|
119
|
+
|
|
120
|
+
const { object } = await generateStructured(
|
|
121
|
+
model,
|
|
122
|
+
z.object({ title: z.string(), tags: z.array(z.string()) }),
|
|
123
|
+
{ prompt: 'Suggest a title and tags for this article: …' },
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
// One round of tool-calling (native or prompted), normalized result:
|
|
127
|
+
const { text: reply, toolCalls } = await runToolLoop(model, {
|
|
128
|
+
mode: 'native',
|
|
129
|
+
prompt: 'Add a title',
|
|
130
|
+
tools,
|
|
131
|
+
})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Optional: MCP tools over HTTP
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
import { connectMcpHttp } from '@dudko.dev/agent-web/mcp'
|
|
138
|
+
|
|
139
|
+
const mcp = await connectMcpHttp({
|
|
140
|
+
docs: { url: 'https://my-mcp-server.example/mcp', headers: { Authorization: `Bearer ${t}` } },
|
|
141
|
+
})
|
|
142
|
+
const agent = await createAgent({ model, tools: { ...tools, ...mcp.tools } })
|
|
143
|
+
// ... later: await mcp.close()
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Browsers can only speak the **HTTP (StreamableHTTP)** transport — stdio MCP is
|
|
147
|
+
Node-only. The connector lives in the `./mcp` subpath so the MCP SDK never
|
|
148
|
+
enters your core bundle.
|
|
149
|
+
|
|
150
|
+
## Configuration (highlights)
|
|
151
|
+
|
|
152
|
+
| Option | Default | Purpose |
|
|
153
|
+
| --- | --- | --- |
|
|
154
|
+
| `model` | — | a `LanguageModel` or `ProviderModelSpec` (the executor/default) |
|
|
155
|
+
| `planner` / `synthesizer` | = `model` | per-stage model overrides (inherit base provider/creds if partial) |
|
|
156
|
+
| `credentials` | — | a `CredentialStore` for `credentialRef` keys |
|
|
157
|
+
| `tools` | `{}` | host tools (`defineTool`) |
|
|
158
|
+
| `toolMode` | `'auto'` | `native` \| `prompted` \| `auto` (cloud→native, local→prompted) |
|
|
159
|
+
| `systemPrompt` | — | prepended to every phase |
|
|
160
|
+
| `describeState` | — | serialize world state into prompt context |
|
|
161
|
+
| `memory` | — | `ContextStore` (`IndexedDBStore` / `MemoryStore`) |
|
|
162
|
+
| `maxIterations` / `maxStepsPerTask` / `maxRevisions` | 8 / 4 / 2 | loop caps |
|
|
163
|
+
| `chatTimeoutMs` | 120000 | per-call watchdog |
|
|
164
|
+
| `replan` / `synthesize` | `true` | toggle phases |
|
|
165
|
+
| `compressAfterChars` | 12000 | summarize old history past this size |
|
|
166
|
+
|
|
167
|
+
## Docs
|
|
168
|
+
|
|
169
|
+
- [docs/design.md](docs/design.md) — architecture & rationale.
|
|
170
|
+
- [docs/providers.md](docs/providers.md) — every provider, CORS & direct-vs-proxy.
|
|
171
|
+
- [docs/security.md](docs/security.md) — the token vault & its threat model.
|
|
172
|
+
- [docs/tasks.md](docs/tasks.md) — status & roadmap.
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
MIT © Siarhei Dudko
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/providers/webllm.ts
|
|
2
|
+
var isWebGPUAvailable = () => typeof navigator !== "undefined" && "gpu" in navigator;
|
|
3
|
+
var createWebLLMModel = async (model, options) => {
|
|
4
|
+
let mod;
|
|
5
|
+
try {
|
|
6
|
+
mod = await import("@browser-ai/web-llm");
|
|
7
|
+
} catch {
|
|
8
|
+
throw new Error(
|
|
9
|
+
'Local models require "@browser-ai/web-llm" and its peer "@mlc-ai/web-llm". Install: npm install @browser-ai/web-llm @mlc-ai/web-llm'
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
return mod.webLLM(
|
|
13
|
+
model,
|
|
14
|
+
options
|
|
15
|
+
);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
isWebGPUAvailable,
|
|
20
|
+
createWebLLMModel
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=chunk-R7H4HSYT.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/providers/webllm.ts"],"sourcesContent":["import type { LanguageModel } from 'ai'\n\n/** WebGPU feature-detect — WebLLM requires it. Call before offering local models. */\nexport const isWebGPUAvailable = (): boolean =>\n typeof navigator !== 'undefined' && 'gpu' in navigator\n\nexport interface WebLLMModelOptions {\n /**\n * Progress callback for weight download / engine init. `report.progress` is\n * 0..1. Forwarded to WebLLM's `initProgressCallback`.\n */\n initProgressCallback?: (report: { progress: number; text: string }) => void\n /** Any other @browser-ai/web-llm setting (temperature, worker handler, …). */\n [k: string]: unknown\n}\n\n/**\n * Build an AI SDK `LanguageModel` backed by WebLLM (WebGPU) via the community\n * provider `@browser-ai/web-llm` (an optional peer, dynamically imported so it\n * is code-split and never loads until a local model is actually used). Accepts\n * any WebLLM prebuilt model id, e.g. \"Llama-3.2-3B-Instruct-q4f16_1-MLC\".\n */\nexport const createWebLLMModel = async (\n model: string,\n options?: WebLLMModelOptions,\n): Promise<LanguageModel> => {\n let mod: typeof import('@browser-ai/web-llm')\n try {\n mod = await import('@browser-ai/web-llm')\n } catch {\n throw new Error(\n 'Local models require \"@browser-ai/web-llm\" and its peer \"@mlc-ai/web-llm\". Install: npm install @browser-ai/web-llm @mlc-ai/web-llm',\n )\n }\n // webLLM(modelId, settings) returns a LanguageModel. The model id is a strict\n // union in the provider's types; we accept any string and cast at the seam.\n return mod.webLLM(\n model as Parameters<typeof mod.webLLM>[0],\n options as Parameters<typeof mod.webLLM>[1],\n )\n}\n"],"mappings":";AAGO,IAAM,oBAAoB,MAC/B,OAAO,cAAc,eAAe,SAAS;AAkBxC,IAAM,oBAAoB,OAC/B,OACA,YAC2B;AAC3B,MAAI;AACJ,MAAI;AACF,UAAM,MAAM,OAAO,qBAAqB;AAAA,EAC1C,QAAQ;AACN,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAGA,SAAO,IAAI;AAAA,IACT;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|