@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/dist/index.js
ADDED
|
@@ -0,0 +1,793 @@
|
|
|
1
|
+
// src/hooks/use-agent.ts
|
|
2
|
+
import { useCallback, useEffect, useReducer, useRef, useState } from "react";
|
|
3
|
+
import {
|
|
4
|
+
createAgent
|
|
5
|
+
} from "@dudko.dev/agent-web";
|
|
6
|
+
|
|
7
|
+
// src/state.ts
|
|
8
|
+
var emptyUsage = () => ({ inputTokens: 0, outputTokens: 0, totalTokens: 0 });
|
|
9
|
+
var addUsage = (a, b) => ({
|
|
10
|
+
inputTokens: a.inputTokens + b.inputTokens,
|
|
11
|
+
outputTokens: a.outputTokens + b.outputTokens,
|
|
12
|
+
totalTokens: a.totalTokens + b.totalTokens
|
|
13
|
+
});
|
|
14
|
+
var createInitialAgentState = () => ({
|
|
15
|
+
status: "idle",
|
|
16
|
+
goal: void 0,
|
|
17
|
+
plan: void 0,
|
|
18
|
+
planThought: "",
|
|
19
|
+
steps: [],
|
|
20
|
+
finalText: "",
|
|
21
|
+
usage: emptyUsage(),
|
|
22
|
+
modelLoad: void 0,
|
|
23
|
+
replan: void 0,
|
|
24
|
+
error: void 0,
|
|
25
|
+
stopped: false,
|
|
26
|
+
messages: [],
|
|
27
|
+
events: [],
|
|
28
|
+
seq: 0
|
|
29
|
+
});
|
|
30
|
+
var resetRun = (state) => ({
|
|
31
|
+
...createInitialAgentState(),
|
|
32
|
+
status: state.status,
|
|
33
|
+
messages: state.messages,
|
|
34
|
+
seq: state.seq
|
|
35
|
+
});
|
|
36
|
+
var patchStep = (steps, id, fn) => steps.map((s) => s.id === id ? fn(s) : s);
|
|
37
|
+
var finalizeAssistant = (messages, content) => {
|
|
38
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
39
|
+
const m = messages[i];
|
|
40
|
+
if (m.role === "assistant" && m.pending) {
|
|
41
|
+
const next = messages.slice();
|
|
42
|
+
next[i] = { ...m, content: content || m.content, pending: false };
|
|
43
|
+
return next;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return messages;
|
|
47
|
+
};
|
|
48
|
+
var streamAssistant = (messages, content) => {
|
|
49
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
50
|
+
const m = messages[i];
|
|
51
|
+
if (m.role === "assistant" && m.pending) {
|
|
52
|
+
const next = messages.slice();
|
|
53
|
+
next[i] = { ...m, content };
|
|
54
|
+
return next;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return messages;
|
|
58
|
+
};
|
|
59
|
+
var applyEvent = (state, event) => {
|
|
60
|
+
switch (event.type) {
|
|
61
|
+
case "run.start": {
|
|
62
|
+
const base2 = resetRun(state);
|
|
63
|
+
const userId = `msg-${base2.seq}`;
|
|
64
|
+
const assistantId = `msg-${base2.seq + 1}`;
|
|
65
|
+
return {
|
|
66
|
+
...base2,
|
|
67
|
+
goal: event.goal,
|
|
68
|
+
messages: [
|
|
69
|
+
...base2.messages,
|
|
70
|
+
{ id: userId, role: "user", content: event.goal, pending: false },
|
|
71
|
+
{ id: assistantId, role: "assistant", content: "", pending: true }
|
|
72
|
+
],
|
|
73
|
+
seq: base2.seq + 2
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
case "model.load":
|
|
77
|
+
return { ...state, modelLoad: { progress: event.progress, text: event.text } };
|
|
78
|
+
case "plan.thought-delta":
|
|
79
|
+
return { ...state, planThought: state.planThought + event.delta };
|
|
80
|
+
case "plan.created":
|
|
81
|
+
return {
|
|
82
|
+
...state,
|
|
83
|
+
plan: event.plan,
|
|
84
|
+
planThought: state.planThought || event.plan.thought
|
|
85
|
+
};
|
|
86
|
+
case "plan.revised":
|
|
87
|
+
return { ...state, plan: event.plan, replan: { mode: "revise", reason: event.reason } };
|
|
88
|
+
case "plan.step-added":
|
|
89
|
+
return state;
|
|
90
|
+
case "step.start": {
|
|
91
|
+
const view = {
|
|
92
|
+
id: event.step.id,
|
|
93
|
+
index: event.index,
|
|
94
|
+
total: event.total,
|
|
95
|
+
step: event.step,
|
|
96
|
+
text: "",
|
|
97
|
+
toolCalls: [],
|
|
98
|
+
status: "running",
|
|
99
|
+
blocked: false
|
|
100
|
+
};
|
|
101
|
+
return { ...state, steps: [...state.steps, view] };
|
|
102
|
+
}
|
|
103
|
+
case "step.text-delta":
|
|
104
|
+
return {
|
|
105
|
+
...state,
|
|
106
|
+
steps: patchStep(state.steps, event.step.id, (s) => ({ ...s, text: s.text + event.delta }))
|
|
107
|
+
};
|
|
108
|
+
case "step.tool-call": {
|
|
109
|
+
const call = { name: event.name, input: event.input, status: "calling" };
|
|
110
|
+
return {
|
|
111
|
+
...state,
|
|
112
|
+
steps: patchStep(state.steps, event.step.id, (s) => ({
|
|
113
|
+
...s,
|
|
114
|
+
toolCalls: [...s.toolCalls, call]
|
|
115
|
+
}))
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
case "step.tool-result":
|
|
119
|
+
return {
|
|
120
|
+
...state,
|
|
121
|
+
steps: patchStep(state.steps, event.step.id, (s) => {
|
|
122
|
+
let done = false;
|
|
123
|
+
const toolCalls = s.toolCalls.slice().reverse().map((c) => {
|
|
124
|
+
if (!done && c.status === "calling" && c.name === event.name) {
|
|
125
|
+
done = true;
|
|
126
|
+
return { ...c, output: event.output, ok: event.ok, status: "done" };
|
|
127
|
+
}
|
|
128
|
+
return c;
|
|
129
|
+
}).reverse();
|
|
130
|
+
return { ...s, toolCalls };
|
|
131
|
+
})
|
|
132
|
+
};
|
|
133
|
+
case "step.complete":
|
|
134
|
+
return {
|
|
135
|
+
...state,
|
|
136
|
+
steps: patchStep(state.steps, event.step.id, (s) => ({
|
|
137
|
+
...s,
|
|
138
|
+
status: "done",
|
|
139
|
+
summary: event.result.summary,
|
|
140
|
+
blocked: event.result.blocked,
|
|
141
|
+
// The result carries the authoritative, ordered tool-call list.
|
|
142
|
+
toolCalls: event.result.toolCalls.map((c) => ({
|
|
143
|
+
name: c.name,
|
|
144
|
+
input: c.input,
|
|
145
|
+
output: c.output,
|
|
146
|
+
ok: c.ok,
|
|
147
|
+
status: "done"
|
|
148
|
+
}))
|
|
149
|
+
}))
|
|
150
|
+
};
|
|
151
|
+
case "replan.decision":
|
|
152
|
+
return { ...state, replan: { mode: event.mode, reason: event.reason } };
|
|
153
|
+
case "final.text-delta": {
|
|
154
|
+
const finalText = state.finalText + event.delta;
|
|
155
|
+
return { ...state, finalText, messages: streamAssistant(state.messages, finalText) };
|
|
156
|
+
}
|
|
157
|
+
case "final":
|
|
158
|
+
return {
|
|
159
|
+
...state,
|
|
160
|
+
finalText: event.text,
|
|
161
|
+
messages: finalizeAssistant(state.messages, event.text)
|
|
162
|
+
};
|
|
163
|
+
case "usage":
|
|
164
|
+
return { ...state, usage: addUsage(state.usage, event.usage) };
|
|
165
|
+
case "stopped":
|
|
166
|
+
return {
|
|
167
|
+
...state,
|
|
168
|
+
stopped: true,
|
|
169
|
+
messages: finalizeAssistant(state.messages, state.finalText || "Stopped.")
|
|
170
|
+
};
|
|
171
|
+
case "error":
|
|
172
|
+
return event.phase === "run" ? {
|
|
173
|
+
...state,
|
|
174
|
+
error: event.error,
|
|
175
|
+
messages: finalizeAssistant(state.messages, state.finalText || `Error: ${event.error}`)
|
|
176
|
+
} : { ...state, error: event.error };
|
|
177
|
+
case "retry":
|
|
178
|
+
return state;
|
|
179
|
+
default:
|
|
180
|
+
return state;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
var agentStateReducer = (state, action) => {
|
|
184
|
+
switch (action.type) {
|
|
185
|
+
case "reset":
|
|
186
|
+
return { ...createInitialAgentState(), status: state.status, seq: state.seq };
|
|
187
|
+
case "status":
|
|
188
|
+
return {
|
|
189
|
+
...state,
|
|
190
|
+
status: action.status,
|
|
191
|
+
error: action.status === "error" ? action.error : void 0
|
|
192
|
+
};
|
|
193
|
+
case "event": {
|
|
194
|
+
const next = applyEvent(state, action.event);
|
|
195
|
+
const cap = action.maxEvents ?? 200;
|
|
196
|
+
const events = cap > 0 ? [...state.events, action.event].slice(-cap) : [...state.events, action.event];
|
|
197
|
+
return { ...next, events };
|
|
198
|
+
}
|
|
199
|
+
default:
|
|
200
|
+
return state;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
// src/util.ts
|
|
205
|
+
var errMessage = (err) => err instanceof Error ? err.message : String(err);
|
|
206
|
+
|
|
207
|
+
// src/hooks/use-agent.ts
|
|
208
|
+
var useAgent = (config, options = {}) => {
|
|
209
|
+
const [state, dispatch] = useReducer(agentStateReducer, void 0, createInitialAgentState);
|
|
210
|
+
const agentRef = useRef(void 0);
|
|
211
|
+
const abortRef = useRef(void 0);
|
|
212
|
+
const [generation, setGeneration] = useState(0);
|
|
213
|
+
const configRef = useRef(config);
|
|
214
|
+
configRef.current = config;
|
|
215
|
+
const optionsRef = useRef(options);
|
|
216
|
+
optionsRef.current = options;
|
|
217
|
+
const reload = useCallback(() => setGeneration((g) => g + 1), []);
|
|
218
|
+
const { autoStart, deps } = options;
|
|
219
|
+
useEffect(() => {
|
|
220
|
+
agentRef.current = void 0;
|
|
221
|
+
const cfg = configRef.current;
|
|
222
|
+
const shouldBuild = (autoStart !== false || generation > 0) && Boolean(cfg.model);
|
|
223
|
+
if (!shouldBuild) {
|
|
224
|
+
dispatch({ type: "status", status: "idle" });
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
let cancelled = false;
|
|
228
|
+
dispatch({ type: "status", status: "initializing" });
|
|
229
|
+
createAgent(cfg).then((agent) => {
|
|
230
|
+
if (cancelled) return;
|
|
231
|
+
agentRef.current = agent;
|
|
232
|
+
dispatch({ type: "status", status: "ready" });
|
|
233
|
+
}).catch((err) => {
|
|
234
|
+
if (cancelled) return;
|
|
235
|
+
dispatch({ type: "status", status: "error", error: errMessage(err) });
|
|
236
|
+
});
|
|
237
|
+
return () => {
|
|
238
|
+
cancelled = true;
|
|
239
|
+
};
|
|
240
|
+
}, [generation, autoStart, ...deps ?? []]);
|
|
241
|
+
useEffect(() => () => abortRef.current?.abort(), []);
|
|
242
|
+
const run = useCallback(async (goal) => {
|
|
243
|
+
const agent = agentRef.current;
|
|
244
|
+
if (!agent || abortRef.current) return void 0;
|
|
245
|
+
const controller = new AbortController();
|
|
246
|
+
abortRef.current = controller;
|
|
247
|
+
dispatch({ type: "status", status: "running" });
|
|
248
|
+
try {
|
|
249
|
+
return await agent.run(goal, {
|
|
250
|
+
signal: controller.signal,
|
|
251
|
+
onEvent: (event) => {
|
|
252
|
+
dispatch({ type: "event", event, maxEvents: optionsRef.current.maxEvents });
|
|
253
|
+
try {
|
|
254
|
+
optionsRef.current.onEvent?.(event);
|
|
255
|
+
} catch {
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
} catch (err) {
|
|
260
|
+
dispatch({
|
|
261
|
+
type: "event",
|
|
262
|
+
event: { type: "error", phase: "run", error: errMessage(err) },
|
|
263
|
+
maxEvents: optionsRef.current.maxEvents
|
|
264
|
+
});
|
|
265
|
+
return void 0;
|
|
266
|
+
} finally {
|
|
267
|
+
abortRef.current = void 0;
|
|
268
|
+
dispatch({ type: "status", status: "ready" });
|
|
269
|
+
}
|
|
270
|
+
}, []);
|
|
271
|
+
const stop = useCallback(() => abortRef.current?.abort(), []);
|
|
272
|
+
const reset = useCallback(() => dispatch({ type: "reset" }), []);
|
|
273
|
+
return {
|
|
274
|
+
...state,
|
|
275
|
+
run,
|
|
276
|
+
stop,
|
|
277
|
+
reset,
|
|
278
|
+
reload,
|
|
279
|
+
agent: agentRef.current,
|
|
280
|
+
isRunning: state.status === "running",
|
|
281
|
+
isReady: state.status === "ready"
|
|
282
|
+
};
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
// src/hooks/use-credentials.ts
|
|
286
|
+
import { useCallback as useCallback2, useMemo, useState as useState2 } from "react";
|
|
287
|
+
import { VaultCredentialStore } from "@dudko.dev/agent-web";
|
|
288
|
+
var useCredentials = (store) => {
|
|
289
|
+
const resolved = useMemo(() => store ?? new VaultCredentialStore(), [store]);
|
|
290
|
+
const [version, setVersion] = useState2(0);
|
|
291
|
+
const setKey = useCallback2(
|
|
292
|
+
async (ref, key) => {
|
|
293
|
+
await resolved.setApiKey(ref, key);
|
|
294
|
+
setVersion((v) => v + 1);
|
|
295
|
+
},
|
|
296
|
+
[resolved]
|
|
297
|
+
);
|
|
298
|
+
const removeKey = useCallback2(
|
|
299
|
+
async (ref) => {
|
|
300
|
+
await resolved.deleteApiKey(ref);
|
|
301
|
+
setVersion((v) => v + 1);
|
|
302
|
+
},
|
|
303
|
+
[resolved]
|
|
304
|
+
);
|
|
305
|
+
const hasKey = useCallback2(
|
|
306
|
+
async (ref) => await resolved.getApiKey(ref) !== void 0,
|
|
307
|
+
[resolved]
|
|
308
|
+
);
|
|
309
|
+
return { store: resolved, setKey, removeKey, hasKey, version };
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// src/hooks/use-webllm-model.ts
|
|
313
|
+
import { useCallback as useCallback3, useRef as useRef2, useState as useState3 } from "react";
|
|
314
|
+
import { createWebLLMModel, isWebGPUAvailable } from "@dudko.dev/agent-web";
|
|
315
|
+
var useWebLLMModel = (modelId, options) => {
|
|
316
|
+
const [model, setModel] = useState3(void 0);
|
|
317
|
+
const [loading, setLoading] = useState3(false);
|
|
318
|
+
const [progress, setProgress] = useState3(0);
|
|
319
|
+
const [text, setText] = useState3("");
|
|
320
|
+
const [error, setError] = useState3(void 0);
|
|
321
|
+
const optionsRef = useRef2(options);
|
|
322
|
+
optionsRef.current = options;
|
|
323
|
+
const load = useCallback3(async () => {
|
|
324
|
+
if (!isWebGPUAvailable()) {
|
|
325
|
+
setError("WebGPU is not available in this browser.");
|
|
326
|
+
return void 0;
|
|
327
|
+
}
|
|
328
|
+
setLoading(true);
|
|
329
|
+
setError(void 0);
|
|
330
|
+
try {
|
|
331
|
+
const built = await createWebLLMModel(modelId, {
|
|
332
|
+
...optionsRef.current,
|
|
333
|
+
initProgressCallback: (report) => {
|
|
334
|
+
setProgress(report.progress);
|
|
335
|
+
setText(report.text);
|
|
336
|
+
optionsRef.current?.initProgressCallback?.(report);
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
setModel(built);
|
|
340
|
+
return built;
|
|
341
|
+
} catch (err) {
|
|
342
|
+
setError(errMessage(err));
|
|
343
|
+
return void 0;
|
|
344
|
+
} finally {
|
|
345
|
+
setLoading(false);
|
|
346
|
+
}
|
|
347
|
+
}, [modelId]);
|
|
348
|
+
return {
|
|
349
|
+
model,
|
|
350
|
+
load,
|
|
351
|
+
loading,
|
|
352
|
+
progress,
|
|
353
|
+
text,
|
|
354
|
+
error,
|
|
355
|
+
supported: isWebGPUAvailable(),
|
|
356
|
+
ready: model !== void 0
|
|
357
|
+
};
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
// src/context.tsx
|
|
361
|
+
import { createContext, useContext } from "react";
|
|
362
|
+
import { jsx } from "react/jsx-runtime";
|
|
363
|
+
var AgentContext = createContext(null);
|
|
364
|
+
var AgentProvider = ({ config, children, ...options }) => {
|
|
365
|
+
const agent = useAgent(config, options);
|
|
366
|
+
return /* @__PURE__ */ jsx(AgentContext.Provider, { value: agent, children });
|
|
367
|
+
};
|
|
368
|
+
var useAgentContext = () => {
|
|
369
|
+
const ctx = useContext(AgentContext);
|
|
370
|
+
if (!ctx) throw new Error("useAgentContext must be used within an <AgentProvider>");
|
|
371
|
+
return ctx;
|
|
372
|
+
};
|
|
373
|
+
var useOptionalAgentContext = () => useContext(AgentContext);
|
|
374
|
+
|
|
375
|
+
// src/components/AgentChat.tsx
|
|
376
|
+
import { useEffect as useEffect2, useRef as useRef3 } from "react";
|
|
377
|
+
|
|
378
|
+
// src/components/icons.tsx
|
|
379
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
380
|
+
var base = (size = 16) => ({
|
|
381
|
+
width: size,
|
|
382
|
+
height: size,
|
|
383
|
+
viewBox: "0 0 24 24",
|
|
384
|
+
fill: "none",
|
|
385
|
+
stroke: "currentColor",
|
|
386
|
+
strokeWidth: 2,
|
|
387
|
+
strokeLinecap: "round",
|
|
388
|
+
strokeLinejoin: "round",
|
|
389
|
+
"aria-hidden": true
|
|
390
|
+
});
|
|
391
|
+
var SendIcon = ({ size, className }) => /* @__PURE__ */ jsxs("svg", { ...base(size), className, children: [
|
|
392
|
+
/* @__PURE__ */ jsx2("path", { d: "M22 2 11 13" }),
|
|
393
|
+
/* @__PURE__ */ jsx2("path", { d: "M22 2 15 22l-4-9-9-4Z" })
|
|
394
|
+
] });
|
|
395
|
+
var StopIcon = ({ size, className }) => /* @__PURE__ */ jsx2("svg", { ...base(size), className, children: /* @__PURE__ */ jsx2("rect", { x: "6", y: "6", width: "12", height: "12", rx: "2" }) });
|
|
396
|
+
var SpinnerIcon = ({ size, className }) => /* @__PURE__ */ jsx2("svg", { ...base(size), className: ["awr-spin", className].filter(Boolean).join(" "), children: /* @__PURE__ */ jsx2("path", { d: "M21 12a9 9 0 1 1-6.219-8.56" }) });
|
|
397
|
+
var CheckIcon = ({ size, className }) => /* @__PURE__ */ jsx2("svg", { ...base(size), className, children: /* @__PURE__ */ jsx2("path", { d: "M20 6 9 17l-5-5" }) });
|
|
398
|
+
var AlertIcon = ({ size, className }) => /* @__PURE__ */ jsxs("svg", { ...base(size), className, children: [
|
|
399
|
+
/* @__PURE__ */ jsx2("path", { d: "M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0Z" }),
|
|
400
|
+
/* @__PURE__ */ jsx2("path", { d: "M12 9v4" }),
|
|
401
|
+
/* @__PURE__ */ jsx2("path", { d: "M12 17h.01" })
|
|
402
|
+
] });
|
|
403
|
+
var ToolIcon = ({ size, className }) => /* @__PURE__ */ jsx2("svg", { ...base(size), className, children: /* @__PURE__ */ jsx2("path", { d: "M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76Z" }) });
|
|
404
|
+
|
|
405
|
+
// src/components/Composer.tsx
|
|
406
|
+
import { useState as useState4 } from "react";
|
|
407
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
408
|
+
var Composer = ({
|
|
409
|
+
onSubmit,
|
|
410
|
+
busy = false,
|
|
411
|
+
onStop,
|
|
412
|
+
disabled = false,
|
|
413
|
+
placeholder = "Ask the agent to do something\u2026",
|
|
414
|
+
className
|
|
415
|
+
}) => {
|
|
416
|
+
const [text, setText] = useState4("");
|
|
417
|
+
const submit = () => {
|
|
418
|
+
const value = text.trim();
|
|
419
|
+
if (!value || busy || disabled) return;
|
|
420
|
+
setText("");
|
|
421
|
+
onSubmit(value);
|
|
422
|
+
};
|
|
423
|
+
const onKeyDown = (e) => {
|
|
424
|
+
if (e.key === "Enter" && !e.shiftKey) {
|
|
425
|
+
e.preventDefault();
|
|
426
|
+
submit();
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
return /* @__PURE__ */ jsxs2(
|
|
430
|
+
"form",
|
|
431
|
+
{
|
|
432
|
+
className: ["awr-composer", className].filter(Boolean).join(" "),
|
|
433
|
+
onSubmit: (e) => {
|
|
434
|
+
e.preventDefault();
|
|
435
|
+
submit();
|
|
436
|
+
},
|
|
437
|
+
children: [
|
|
438
|
+
/* @__PURE__ */ jsx3(
|
|
439
|
+
"textarea",
|
|
440
|
+
{
|
|
441
|
+
className: "awr-composer__input",
|
|
442
|
+
value: text,
|
|
443
|
+
onChange: (e) => setText(e.target.value),
|
|
444
|
+
onKeyDown,
|
|
445
|
+
placeholder,
|
|
446
|
+
disabled,
|
|
447
|
+
rows: 1
|
|
448
|
+
}
|
|
449
|
+
),
|
|
450
|
+
busy ? /* @__PURE__ */ jsx3(
|
|
451
|
+
"button",
|
|
452
|
+
{
|
|
453
|
+
type: "button",
|
|
454
|
+
className: "awr-composer__btn awr-composer__btn--stop",
|
|
455
|
+
onClick: onStop,
|
|
456
|
+
"aria-label": "Stop",
|
|
457
|
+
children: /* @__PURE__ */ jsx3(StopIcon, { size: 18 })
|
|
458
|
+
}
|
|
459
|
+
) : /* @__PURE__ */ jsx3(
|
|
460
|
+
"button",
|
|
461
|
+
{
|
|
462
|
+
type: "submit",
|
|
463
|
+
className: "awr-composer__btn",
|
|
464
|
+
disabled: disabled || !text.trim(),
|
|
465
|
+
"aria-label": "Send",
|
|
466
|
+
children: disabled ? /* @__PURE__ */ jsx3(SpinnerIcon, { size: 18 }) : /* @__PURE__ */ jsx3(SendIcon, { size: 18 })
|
|
467
|
+
}
|
|
468
|
+
)
|
|
469
|
+
]
|
|
470
|
+
}
|
|
471
|
+
);
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
// src/components/MessageList.tsx
|
|
475
|
+
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
476
|
+
var MessageList = ({ messages, empty, className }) => {
|
|
477
|
+
if (messages.length === 0) {
|
|
478
|
+
return empty ? /* @__PURE__ */ jsx4("div", { className: "awr-messages__empty", children: empty }) : null;
|
|
479
|
+
}
|
|
480
|
+
return /* @__PURE__ */ jsx4("div", { className: ["awr-messages", className].filter(Boolean).join(" "), children: messages.map((m) => /* @__PURE__ */ jsx4("div", { className: `awr-msg awr-msg--${m.role}`, children: /* @__PURE__ */ jsxs3("div", { className: "awr-msg__bubble", children: [
|
|
481
|
+
m.content,
|
|
482
|
+
m.pending && !m.content && /* @__PURE__ */ jsx4(SpinnerIcon, { size: 14, className: "awr-msg__pending" })
|
|
483
|
+
] }) }, m.id)) });
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
// src/components/ModelLoadBar.tsx
|
|
487
|
+
import { jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
488
|
+
var ModelLoadBar = ({ load, className }) => {
|
|
489
|
+
const pct = Math.round(Math.max(0, Math.min(1, load.progress)) * 100);
|
|
490
|
+
return /* @__PURE__ */ jsxs4("div", { className: ["awr-modelload", className].filter(Boolean).join(" "), children: [
|
|
491
|
+
/* @__PURE__ */ jsxs4("div", { className: "awr-modelload__head", children: [
|
|
492
|
+
/* @__PURE__ */ jsx5("span", { className: "awr-modelload__text", children: load.text || "Loading model\u2026" }),
|
|
493
|
+
/* @__PURE__ */ jsxs4("span", { className: "awr-modelload__pct", children: [
|
|
494
|
+
pct,
|
|
495
|
+
"%"
|
|
496
|
+
] })
|
|
497
|
+
] }),
|
|
498
|
+
/* @__PURE__ */ jsx5("div", { className: "awr-modelload__track", role: "progressbar", "aria-valuenow": pct, children: /* @__PURE__ */ jsx5("div", { className: "awr-modelload__fill", style: { width: `${pct}%` } }) })
|
|
499
|
+
] });
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
// src/components/format.ts
|
|
503
|
+
var previewValue = (value, max = 140) => {
|
|
504
|
+
if (value === void 0) return "";
|
|
505
|
+
let text;
|
|
506
|
+
if (typeof value === "string") text = value;
|
|
507
|
+
else {
|
|
508
|
+
try {
|
|
509
|
+
text = JSON.stringify(value);
|
|
510
|
+
} catch {
|
|
511
|
+
text = String(value);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
text = text.replace(/\s+/g, " ").trim();
|
|
515
|
+
return text.length > max ? `${text.slice(0, max - 1)}\u2026` : text;
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// src/components/StepList.tsx
|
|
519
|
+
import { jsx as jsx6, jsxs as jsxs5 } from "react/jsx-runtime";
|
|
520
|
+
var ToolCallRow = ({ call }) => {
|
|
521
|
+
const state = call.status === "calling" ? "calling" : call.ok === false ? "failed" : "ok";
|
|
522
|
+
return /* @__PURE__ */ jsxs5("li", { className: `awr-tool awr-tool--${state}`, children: [
|
|
523
|
+
/* @__PURE__ */ jsx6(ToolIcon, { size: 13, className: "awr-tool__icon" }),
|
|
524
|
+
/* @__PURE__ */ jsx6("code", { className: "awr-tool__name", children: call.name }),
|
|
525
|
+
previewValue(call.input) && /* @__PURE__ */ jsx6("span", { className: "awr-tool__args", children: previewValue(call.input) }),
|
|
526
|
+
call.status === "calling" && /* @__PURE__ */ jsx6(SpinnerIcon, { size: 12, className: "awr-tool__spin" }),
|
|
527
|
+
call.status === "done" && call.ok === false && /* @__PURE__ */ jsx6("span", { className: "awr-tool__badge", children: "failed" })
|
|
528
|
+
] });
|
|
529
|
+
};
|
|
530
|
+
var StepItem = ({ step }) => /* @__PURE__ */ jsxs5("li", { className: `awr-step awr-step--${step.blocked ? "blocked" : step.status}`, children: [
|
|
531
|
+
/* @__PURE__ */ jsxs5("div", { className: "awr-step__head", children: [
|
|
532
|
+
/* @__PURE__ */ jsx6("span", { className: "awr-step__marker", children: step.status === "running" ? /* @__PURE__ */ jsx6(SpinnerIcon, { size: 14 }) : step.blocked ? /* @__PURE__ */ jsx6(AlertIcon, { size: 14 }) : /* @__PURE__ */ jsx6(CheckIcon, { size: 14 }) }),
|
|
533
|
+
/* @__PURE__ */ jsxs5("span", { className: "awr-step__index", children: [
|
|
534
|
+
step.index,
|
|
535
|
+
"/",
|
|
536
|
+
step.total
|
|
537
|
+
] }),
|
|
538
|
+
/* @__PURE__ */ jsx6("span", { className: "awr-step__desc", children: step.step.description })
|
|
539
|
+
] }),
|
|
540
|
+
step.text.trim() && /* @__PURE__ */ jsx6("p", { className: "awr-step__text", children: step.text.trim() }),
|
|
541
|
+
step.toolCalls.length > 0 && /* @__PURE__ */ jsx6("ul", { className: "awr-step__tools", children: step.toolCalls.map((call, i) => /* @__PURE__ */ jsx6(ToolCallRow, { call }, i)) }),
|
|
542
|
+
step.blocked && step.summary && /* @__PURE__ */ jsx6("p", { className: "awr-step__blocked", children: step.summary })
|
|
543
|
+
] });
|
|
544
|
+
var StepList = ({ steps, className }) => {
|
|
545
|
+
if (steps.length === 0) return null;
|
|
546
|
+
return /* @__PURE__ */ jsx6("ol", { className: ["awr-steps", className].filter(Boolean).join(" "), children: steps.map((step) => /* @__PURE__ */ jsx6(StepItem, { step }, step.id)) });
|
|
547
|
+
};
|
|
548
|
+
|
|
549
|
+
// src/components/UsageBadge.tsx
|
|
550
|
+
import { jsxs as jsxs6 } from "react/jsx-runtime";
|
|
551
|
+
var fmt = (n) => n >= 1e3 ? `${(n / 1e3).toFixed(n >= 1e4 ? 0 : 1)}k` : String(n);
|
|
552
|
+
var UsageBadge = ({ usage, className }) => {
|
|
553
|
+
if (usage.totalTokens === 0) return null;
|
|
554
|
+
return /* @__PURE__ */ jsxs6(
|
|
555
|
+
"span",
|
|
556
|
+
{
|
|
557
|
+
className: ["awr-usage", className].filter(Boolean).join(" "),
|
|
558
|
+
title: `${usage.inputTokens} in + ${usage.outputTokens} out = ${usage.totalTokens} tokens`,
|
|
559
|
+
children: [
|
|
560
|
+
fmt(usage.inputTokens),
|
|
561
|
+
"\u2191 ",
|
|
562
|
+
fmt(usage.outputTokens),
|
|
563
|
+
"\u2193"
|
|
564
|
+
]
|
|
565
|
+
}
|
|
566
|
+
);
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// src/components/AgentChat.tsx
|
|
570
|
+
import { jsx as jsx7, jsxs as jsxs7 } from "react/jsx-runtime";
|
|
571
|
+
var statusLabel = (status) => {
|
|
572
|
+
switch (status) {
|
|
573
|
+
case "initializing":
|
|
574
|
+
return "Loading model\u2026";
|
|
575
|
+
case "running":
|
|
576
|
+
return "Working\u2026";
|
|
577
|
+
case "error":
|
|
578
|
+
return "Failed to start";
|
|
579
|
+
default:
|
|
580
|
+
return "Ready";
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
var AgentChat = ({
|
|
584
|
+
controller,
|
|
585
|
+
title,
|
|
586
|
+
placeholder,
|
|
587
|
+
showActivity = true,
|
|
588
|
+
showUsage = true,
|
|
589
|
+
emptyState = "Ask the agent to do something.",
|
|
590
|
+
className,
|
|
591
|
+
style
|
|
592
|
+
}) => {
|
|
593
|
+
const ctx = useOptionalAgentContext();
|
|
594
|
+
const agent = controller ?? ctx;
|
|
595
|
+
if (!agent) {
|
|
596
|
+
throw new Error(
|
|
597
|
+
"<AgentChat> needs a `controller` prop from useAgent(), or an enclosing <AgentProvider>."
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
const bodyRef = useRef3(null);
|
|
601
|
+
useEffect2(() => {
|
|
602
|
+
const el = bodyRef.current;
|
|
603
|
+
if (el) el.scrollTop = el.scrollHeight;
|
|
604
|
+
}, [agent.messages, agent.steps, agent.finalText, agent.modelLoad]);
|
|
605
|
+
const showReplan = agent.replan && agent.replan.mode !== "continue";
|
|
606
|
+
const loading = agent.modelLoad && agent.modelLoad.progress < 1 && agent.isRunning === false;
|
|
607
|
+
return /* @__PURE__ */ jsxs7("div", { className: ["awr-chat", className].filter(Boolean).join(" "), style, children: [
|
|
608
|
+
title && /* @__PURE__ */ jsx7("div", { className: "awr-chat__title", children: title }),
|
|
609
|
+
/* @__PURE__ */ jsxs7("div", { className: "awr-chat__body", ref: bodyRef, children: [
|
|
610
|
+
/* @__PURE__ */ jsx7(MessageList, { messages: agent.messages, empty: emptyState }),
|
|
611
|
+
showActivity && (agent.steps.length > 0 || agent.planThought) && /* @__PURE__ */ jsxs7("div", { className: "awr-activity", children: [
|
|
612
|
+
agent.planThought.trim() && /* @__PURE__ */ jsxs7("details", { className: "awr-activity__plan", children: [
|
|
613
|
+
/* @__PURE__ */ jsx7("summary", { children: "Plan" }),
|
|
614
|
+
/* @__PURE__ */ jsx7("p", { className: "awr-activity__thought", children: agent.planThought })
|
|
615
|
+
] }),
|
|
616
|
+
/* @__PURE__ */ jsx7(StepList, { steps: agent.steps }),
|
|
617
|
+
showReplan && /* @__PURE__ */ jsxs7("div", { className: "awr-activity__replan", children: [
|
|
618
|
+
"Replanned (",
|
|
619
|
+
agent.replan.mode,
|
|
620
|
+
"): ",
|
|
621
|
+
agent.replan.reason
|
|
622
|
+
] })
|
|
623
|
+
] }),
|
|
624
|
+
agent.modelLoad && (loading || agent.isRunning) && agent.modelLoad.progress < 1 && /* @__PURE__ */ jsx7(ModelLoadBar, { load: agent.modelLoad })
|
|
625
|
+
] }),
|
|
626
|
+
agent.error && agent.status === "error" && /* @__PURE__ */ jsxs7("div", { className: "awr-chat__error", children: [
|
|
627
|
+
/* @__PURE__ */ jsx7(AlertIcon, { size: 14 }),
|
|
628
|
+
" ",
|
|
629
|
+
agent.error
|
|
630
|
+
] }),
|
|
631
|
+
/* @__PURE__ */ jsxs7("div", { className: "awr-chat__footer", children: [
|
|
632
|
+
/* @__PURE__ */ jsxs7("div", { className: "awr-chat__meta", children: [
|
|
633
|
+
/* @__PURE__ */ jsxs7("span", { className: `awr-status awr-status--${agent.status}`, children: [
|
|
634
|
+
/* @__PURE__ */ jsx7("span", { className: "awr-status__dot" }),
|
|
635
|
+
statusLabel(agent.status)
|
|
636
|
+
] }),
|
|
637
|
+
showUsage && /* @__PURE__ */ jsx7(UsageBadge, { usage: agent.usage })
|
|
638
|
+
] }),
|
|
639
|
+
/* @__PURE__ */ jsx7(
|
|
640
|
+
Composer,
|
|
641
|
+
{
|
|
642
|
+
onSubmit: (text) => void agent.run(text),
|
|
643
|
+
busy: agent.isRunning,
|
|
644
|
+
onStop: agent.stop,
|
|
645
|
+
disabled: !agent.isReady && !agent.isRunning,
|
|
646
|
+
placeholder
|
|
647
|
+
}
|
|
648
|
+
)
|
|
649
|
+
] })
|
|
650
|
+
] });
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// src/components/PlanView.tsx
|
|
654
|
+
import { jsx as jsx8, jsxs as jsxs8 } from "react/jsx-runtime";
|
|
655
|
+
var PlanView = ({ plan, showThought = true, className }) => {
|
|
656
|
+
if (!plan.steps.length && !plan.thought) return null;
|
|
657
|
+
return /* @__PURE__ */ jsxs8("div", { className: ["awr-plan", className].filter(Boolean).join(" "), children: [
|
|
658
|
+
showThought && plan.thought.trim() && /* @__PURE__ */ jsx8("p", { className: "awr-plan__thought", children: plan.thought }),
|
|
659
|
+
plan.steps.length > 0 && /* @__PURE__ */ jsx8("ol", { className: "awr-plan__steps", children: plan.steps.map((step) => /* @__PURE__ */ jsx8("li", { className: "awr-plan__step", children: step.description }, step.id)) })
|
|
660
|
+
] });
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
// src/components/ApiKeyForm.tsx
|
|
664
|
+
import { useEffect as useEffect3, useState as useState5 } from "react";
|
|
665
|
+
import { jsx as jsx9, jsxs as jsxs9 } from "react/jsx-runtime";
|
|
666
|
+
var ApiKeyForm = ({
|
|
667
|
+
credentials,
|
|
668
|
+
credentialRef,
|
|
669
|
+
label = "API key",
|
|
670
|
+
placeholder = "sk-\u2026",
|
|
671
|
+
onChange,
|
|
672
|
+
className
|
|
673
|
+
}) => {
|
|
674
|
+
const [value, setValue] = useState5("");
|
|
675
|
+
const [present, setPresent] = useState5(false);
|
|
676
|
+
const [busy, setBusy] = useState5(false);
|
|
677
|
+
useEffect3(() => {
|
|
678
|
+
let active = true;
|
|
679
|
+
credentials.hasKey(credentialRef).then((has) => {
|
|
680
|
+
if (active) setPresent(has);
|
|
681
|
+
});
|
|
682
|
+
return () => {
|
|
683
|
+
active = false;
|
|
684
|
+
};
|
|
685
|
+
}, [credentials, credentialRef, credentials.version]);
|
|
686
|
+
const save = async () => {
|
|
687
|
+
const key = value.trim();
|
|
688
|
+
if (!key) return;
|
|
689
|
+
setBusy(true);
|
|
690
|
+
try {
|
|
691
|
+
await credentials.setKey(credentialRef, key);
|
|
692
|
+
setValue("");
|
|
693
|
+
setPresent(true);
|
|
694
|
+
onChange?.(true);
|
|
695
|
+
} finally {
|
|
696
|
+
setBusy(false);
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
const remove = async () => {
|
|
700
|
+
setBusy(true);
|
|
701
|
+
try {
|
|
702
|
+
await credentials.removeKey(credentialRef);
|
|
703
|
+
setPresent(false);
|
|
704
|
+
onChange?.(false);
|
|
705
|
+
} finally {
|
|
706
|
+
setBusy(false);
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
return /* @__PURE__ */ jsxs9("div", { className: ["awr-keyform", className].filter(Boolean).join(" "), children: [
|
|
710
|
+
/* @__PURE__ */ jsxs9("label", { className: "awr-keyform__label", children: [
|
|
711
|
+
label,
|
|
712
|
+
present && /* @__PURE__ */ jsxs9("span", { className: "awr-keyform__saved", children: [
|
|
713
|
+
/* @__PURE__ */ jsx9(CheckIcon, { size: 12 }),
|
|
714
|
+
" stored, encrypted"
|
|
715
|
+
] })
|
|
716
|
+
] }),
|
|
717
|
+
/* @__PURE__ */ jsxs9("div", { className: "awr-keyform__row", children: [
|
|
718
|
+
/* @__PURE__ */ jsx9(
|
|
719
|
+
"input",
|
|
720
|
+
{
|
|
721
|
+
className: "awr-keyform__input",
|
|
722
|
+
type: "password",
|
|
723
|
+
autoComplete: "off",
|
|
724
|
+
value,
|
|
725
|
+
placeholder: present ? "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022 (replace)" : placeholder,
|
|
726
|
+
onChange: (e) => setValue(e.target.value),
|
|
727
|
+
onKeyDown: (e) => {
|
|
728
|
+
if (e.key === "Enter") {
|
|
729
|
+
e.preventDefault();
|
|
730
|
+
void save();
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
disabled: busy
|
|
734
|
+
}
|
|
735
|
+
),
|
|
736
|
+
/* @__PURE__ */ jsx9(
|
|
737
|
+
"button",
|
|
738
|
+
{
|
|
739
|
+
type: "button",
|
|
740
|
+
className: "awr-keyform__btn",
|
|
741
|
+
onClick: () => void save(),
|
|
742
|
+
disabled: busy || !value.trim(),
|
|
743
|
+
children: "Save"
|
|
744
|
+
}
|
|
745
|
+
),
|
|
746
|
+
present && /* @__PURE__ */ jsx9(
|
|
747
|
+
"button",
|
|
748
|
+
{
|
|
749
|
+
type: "button",
|
|
750
|
+
className: "awr-keyform__btn awr-keyform__btn--ghost",
|
|
751
|
+
onClick: () => void remove(),
|
|
752
|
+
disabled: busy,
|
|
753
|
+
children: "Remove"
|
|
754
|
+
}
|
|
755
|
+
)
|
|
756
|
+
] })
|
|
757
|
+
] });
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
// src/index.ts
|
|
761
|
+
import {
|
|
762
|
+
createAgent as createAgent2,
|
|
763
|
+
defineTool,
|
|
764
|
+
createWebLLMModel as createWebLLMModel2,
|
|
765
|
+
isWebGPUAvailable as isWebGPUAvailable2,
|
|
766
|
+
VaultCredentialStore as VaultCredentialStore2,
|
|
767
|
+
MemoryCredentialStore
|
|
768
|
+
} from "@dudko.dev/agent-web";
|
|
769
|
+
export {
|
|
770
|
+
AgentChat,
|
|
771
|
+
AgentProvider,
|
|
772
|
+
ApiKeyForm,
|
|
773
|
+
Composer,
|
|
774
|
+
MemoryCredentialStore,
|
|
775
|
+
MessageList,
|
|
776
|
+
ModelLoadBar,
|
|
777
|
+
PlanView,
|
|
778
|
+
StepList,
|
|
779
|
+
UsageBadge,
|
|
780
|
+
VaultCredentialStore2 as VaultCredentialStore,
|
|
781
|
+
agentStateReducer,
|
|
782
|
+
createAgent2 as createAgent,
|
|
783
|
+
createInitialAgentState,
|
|
784
|
+
createWebLLMModel2 as createWebLLMModel,
|
|
785
|
+
defineTool,
|
|
786
|
+
isWebGPUAvailable2 as isWebGPUAvailable,
|
|
787
|
+
useAgent,
|
|
788
|
+
useAgentContext,
|
|
789
|
+
useCredentials,
|
|
790
|
+
useOptionalAgentContext,
|
|
791
|
+
useWebLLMModel
|
|
792
|
+
};
|
|
793
|
+
//# sourceMappingURL=index.js.map
|