@dudko.dev/agent-web-react 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 +258 -0
- package/dist/index.cjs +832 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +354 -0
- package/dist/index.d.ts +354 -0
- package/dist/index.js +793 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +447 -0
- package/package.json +122 -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,258 @@
|
|
|
1
|
+
# @dudko.dev/agent-web-react
|
|
2
|
+
|
|
3
|
+
React bindings for [`@dudko.dev/agent-web`](https://www.npmjs.com/package/@dudko.dev/agent-web) β
|
|
4
|
+
the headless, universal **in-browser LLM agent**. Drop the agent into any React
|
|
5
|
+
app with a single hook and (optionally) a set of pre-styled components:
|
|
6
|
+
|
|
7
|
+
- πͺ **`useAgent`** β build the agent from a config, stream its typed events into
|
|
8
|
+
a ready-to-render state (plan, steps, tool calls, streamed answer, token
|
|
9
|
+
usage, model-load progress, chat transcript), and get `run` / `stop` /
|
|
10
|
+
`reset` / `reload`.
|
|
11
|
+
- π§© **`<AgentProvider>` + components** β a drop-in `<AgentChat>` panel plus
|
|
12
|
+
`<PlanView>`, `<StepList>`, `<Composer>`, `<ModelLoadBar>`, `<UsageBadge>`,
|
|
13
|
+
and `<ApiKeyForm>`. Bring your own CSS or import the optional stylesheet.
|
|
14
|
+
- π **`useCredentials`** β store BYOK API keys **encrypted at rest**
|
|
15
|
+
(WebCrypto + IndexedDB).
|
|
16
|
+
- π₯οΈ **`useWebLLMModel`** β load a local WebGPU model with download progress.
|
|
17
|
+
- ποΈ **Headless-first** β the eventβUI logic is a pure, exported reducer
|
|
18
|
+
(`agentStateReducer`); the components are optional sugar on top.
|
|
19
|
+
|
|
20
|
+
[](https://www.npmjs.com/package/@dudko.dev/agent-web-react)
|
|
21
|
+
[](https://www.npmjs.com/package/@dudko.dev/agent-web-react)
|
|
22
|
+
[](https://www.npmjs.com/package/@dudko.dev/agent-web-react)
|
|
23
|
+

|
|
24
|
+
|
|
25
|
+
> **βΆοΈ [Live demo](https://dudko-dev.github.io/agent-web-react/)** β an agent that
|
|
26
|
+
> edits a sticky-notes board via tools. Cloud BYOK or local WebGPU, all in your
|
|
27
|
+
> browser. Source in [`demo/`](demo/).
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install @dudko.dev/agent-web-react @dudko.dev/agent-web react react-dom
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Then add **only the model providers you use** (optional peers of the core,
|
|
36
|
+
dynamically imported):
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# cloud, pick what you need
|
|
40
|
+
npm install @ai-sdk/openai # or @ai-sdk/anthropic, @ai-sdk/google, @ai-sdk/xai, @ai-sdk/deepseek, @ai-sdk/openai-compatible
|
|
41
|
+
# local WebGPU models
|
|
42
|
+
npm install @browser-ai/web-llm @mlc-ai/web-llm
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Import the optional stylesheet once (skip it if you style the components
|
|
46
|
+
yourself):
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import '@dudko.dev/agent-web-react/styles.css'
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Quick start β a drop-in chat panel
|
|
53
|
+
|
|
54
|
+
`<AgentProvider>` builds one agent and shares it; `<AgentChat>` renders it.
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { useMemo } from 'react'
|
|
58
|
+
import {
|
|
59
|
+
AgentProvider,
|
|
60
|
+
AgentChat,
|
|
61
|
+
useCredentials,
|
|
62
|
+
defineTool,
|
|
63
|
+
} from '@dudko.dev/agent-web-react'
|
|
64
|
+
import { z } from 'zod'
|
|
65
|
+
import '@dudko.dev/agent-web-react/styles.css'
|
|
66
|
+
|
|
67
|
+
export function Assistant() {
|
|
68
|
+
const credentials = useCredentials() // encrypted vault (WebCrypto + IndexedDB)
|
|
69
|
+
|
|
70
|
+
// Your app's actions, as tools. Keep the object referentially stable.
|
|
71
|
+
const tools = useMemo(
|
|
72
|
+
() => ({
|
|
73
|
+
add_text: defineTool({
|
|
74
|
+
description: 'Add a text block to the page.',
|
|
75
|
+
inputSchema: z.object({ text: z.string() }),
|
|
76
|
+
execute: async ({ text }) => addTextBlock(text), // your code
|
|
77
|
+
}),
|
|
78
|
+
}),
|
|
79
|
+
[],
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<AgentProvider
|
|
84
|
+
config={{
|
|
85
|
+
model: { providerType: 'google', model: 'gemini-2.0-flash', credentialRef: 'google' },
|
|
86
|
+
credentials: credentials.store,
|
|
87
|
+
tools,
|
|
88
|
+
describeState: () => serializeMyState(), // optional grounding
|
|
89
|
+
}}
|
|
90
|
+
>
|
|
91
|
+
<AgentChat title="Assistant" style={{ height: 520 }} />
|
|
92
|
+
</AgentProvider>
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Store the user's key once (encrypted at rest), e.g. from a settings form:
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
const credentials = useCredentials()
|
|
101
|
+
await credentials.setKey('google', userProvidedKey)
|
|
102
|
+
// or drop in <ApiKeyForm credentials={credentials} credentialRef="google" />
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## The `useAgent` hook (headless)
|
|
106
|
+
|
|
107
|
+
`useAgent` is the whole library in one hook β use it directly if you want your
|
|
108
|
+
own UI:
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
import { useAgent } from '@dudko.dev/agent-web-react'
|
|
112
|
+
|
|
113
|
+
function Custom() {
|
|
114
|
+
const agent = useAgent({
|
|
115
|
+
model: { providerType: 'openai', model: 'gpt-4o-mini', credentialRef: 'openai' },
|
|
116
|
+
credentials,
|
|
117
|
+
tools,
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
return (
|
|
121
|
+
<>
|
|
122
|
+
<button disabled={!agent.isReady} onClick={() => agent.run('Add a totals row')}>
|
|
123
|
+
Run
|
|
124
|
+
</button>
|
|
125
|
+
{agent.isRunning && <button onClick={agent.stop}>Stop</button>}
|
|
126
|
+
|
|
127
|
+
{/* Everything below is live, derived from the event stream: */}
|
|
128
|
+
{agent.plan && <p>{agent.plan.thought}</p>}
|
|
129
|
+
{agent.steps.map((s) => (
|
|
130
|
+
<div key={s.id}>
|
|
131
|
+
{s.index}/{s.total} Β· {s.step.description} Β· {s.status}
|
|
132
|
+
{s.toolCalls.map((c, i) => (
|
|
133
|
+
<code key={i}>{c.name}</code>
|
|
134
|
+
))}
|
|
135
|
+
</div>
|
|
136
|
+
))}
|
|
137
|
+
<p>{agent.finalText}</p>
|
|
138
|
+
<small>{agent.usage.totalTokens} tokens</small>
|
|
139
|
+
</>
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
`useAgent(config, options)` returns the full [`AgentUiState`](src/types.ts) plus:
|
|
145
|
+
|
|
146
|
+
| Field | Description |
|
|
147
|
+
| --- | --- |
|
|
148
|
+
| `run(goal)` | Start a run; resolves with the `RunResult` |
|
|
149
|
+
| `stop()` | Abort the in-flight run |
|
|
150
|
+
| `reset()` | Clear the whole conversation |
|
|
151
|
+
| `reload()` | Rebuild the agent (e.g. after storing a new key) |
|
|
152
|
+
| `status` | `idle` \| `initializing` \| `ready` \| `running` \| `error` |
|
|
153
|
+
| `isReady` / `isRunning` | convenience booleans |
|
|
154
|
+
| `messages` | chat transcript (`{ role, content, pending }[]`) |
|
|
155
|
+
| `plan` / `steps` | the live plan and per-step tool calls |
|
|
156
|
+
| `finalText` | the streamed final answer |
|
|
157
|
+
| `usage` | running token total |
|
|
158
|
+
| `modelLoad` | WebLLM download progress, when loading |
|
|
159
|
+
|
|
160
|
+
**Options:** `deps` (rebuild the agent when these change β e.g. on a model
|
|
161
|
+
switch), `onEvent` (tap the raw event stream), `autoStart`, `maxEvents`.
|
|
162
|
+
|
|
163
|
+
> Keep `tools` and `describeState` referentially stable (`useMemo` /
|
|
164
|
+
> `useCallback`); pass a changed `deps` array to rebuild the agent on a
|
|
165
|
+
> model/provider switch.
|
|
166
|
+
|
|
167
|
+
## Local WebGPU model (no key, offline)
|
|
168
|
+
|
|
169
|
+
```tsx
|
|
170
|
+
import { useAgent, useWebLLMModel, ModelLoadBar } from '@dudko.dev/agent-web-react'
|
|
171
|
+
|
|
172
|
+
function LocalAgent() {
|
|
173
|
+
const local = useWebLLMModel('Llama-3.2-3B-Instruct-q4f16_1-MLC')
|
|
174
|
+
const agent = useAgent(
|
|
175
|
+
{ model: local.model!, tools },
|
|
176
|
+
{ deps: [local.ready], autoStart: local.ready }, // build once the model is loaded
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
if (!local.ready) {
|
|
180
|
+
return local.loading ? (
|
|
181
|
+
<ModelLoadBar load={{ progress: local.progress, text: local.text }} />
|
|
182
|
+
) : (
|
|
183
|
+
<button disabled={!local.supported} onClick={local.load}>
|
|
184
|
+
Load local model
|
|
185
|
+
</button>
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
return <AgentChat controller={agent} />
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Components
|
|
193
|
+
|
|
194
|
+
All components are optional and styled by `styles.css` (class-prefixed `awr-`,
|
|
195
|
+
themeable via `--awr-*` custom properties, light + dark). Each accepts a
|
|
196
|
+
`className`; data components take plain props so you can use them standalone.
|
|
197
|
+
|
|
198
|
+
| Component | Purpose |
|
|
199
|
+
| --- | --- |
|
|
200
|
+
| `<AgentChat>` | Full panel: transcript + live activity + composer. Reads a `controller` prop or the `<AgentProvider>` context. |
|
|
201
|
+
| `<MessageList>` | The chat transcript. |
|
|
202
|
+
| `<Composer>` | Textarea + send/stop button (Enter to send). |
|
|
203
|
+
| `<PlanView>` | A plan's reasoning + step list. |
|
|
204
|
+
| `<StepList>` | Live execution steps with tool calls. |
|
|
205
|
+
| `<ModelLoadBar>` | WebLLM download/init progress. |
|
|
206
|
+
| `<UsageBadge>` | Compact token readout. |
|
|
207
|
+
| `<ApiKeyForm>` | BYOK key entry that writes to the encrypted vault. |
|
|
208
|
+
|
|
209
|
+
### Build your own UI
|
|
210
|
+
|
|
211
|
+
The components are a thin layer over the exported, **pure** reducer. Use it
|
|
212
|
+
directly if you'd rather render everything yourself:
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
import { agentStateReducer, createInitialAgentState } from '@dudko.dev/agent-web-react'
|
|
216
|
+
|
|
217
|
+
let state = createInitialAgentState()
|
|
218
|
+
state = agentStateReducer(state, { type: 'event', event }) // fold each AgentEvent
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## How it fits together
|
|
222
|
+
|
|
223
|
+
```
|
|
224
|
+
@dudko.dev/agent-web (peer) β the headless agent + providers + vault
|
|
225
|
+
@dudko.dev/agent-web-react β useAgent / <AgentProvider> / components
|
|
226
|
+
your app β tools, credentials, and where the panel goes
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
The React package re-exports the core primitives you usually need
|
|
230
|
+
(`createAgent`, `defineTool`, `VaultCredentialStore`, `createWebLLMModel`,
|
|
231
|
+
`isWebGPUAvailable`, and the key types), so a React app can import everything
|
|
232
|
+
from one place.
|
|
233
|
+
|
|
234
|
+
> **Direct browser calls & CORS:** not every provider allows direct BYOK calls
|
|
235
|
+
> from a browser origin. Google (Gemini), openai-compatible and the gateway are
|
|
236
|
+
> the reliable direct paths; Anthropic works (a required header is injected);
|
|
237
|
+
> OpenAI/xAI/DeepSeek usually need a proxy. See the core's
|
|
238
|
+
> [providers doc](https://github.com/dudko-dev/agent-web/blob/main/docs/providers.md).
|
|
239
|
+
> Ship only the **user's own** key to the browser β shared/app keys belong
|
|
240
|
+
> behind a proxy or the gateway.
|
|
241
|
+
|
|
242
|
+
## Demo & deployment
|
|
243
|
+
|
|
244
|
+
The [`demo/`](demo/) app (Vite + React) is deployed to GitHub Pages by
|
|
245
|
+
[`.github/workflows/deploy-demo.yml`](.github/workflows/deploy-demo.yml) on
|
|
246
|
+
every push to `main`. To run it locally:
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
npm install && npm run build # build the library into dist/
|
|
250
|
+
cd demo && npm install && npm run dev
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
The demo aliases `@dudko.dev/agent-web-react` to the built `../dist`, so rebuild
|
|
254
|
+
the library (`npm run build`) after changing its source.
|
|
255
|
+
|
|
256
|
+
## License
|
|
257
|
+
|
|
258
|
+
MIT Β© Siarhei Dudko
|