vanilla-agent 1.5.0 → 1.7.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/README.md +43 -5
- package/dist/index.cjs +7 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +163 -23
- package/dist/index.d.ts +163 -23
- package/dist/index.global.js +52 -52
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +7 -7
- package/dist/index.js.map +1 -1
- package/dist/widget.css +25 -8
- package/package.json +8 -3
- package/src/client.test.ts +1 -1
- package/src/client.ts +132 -29
- package/src/components/message-bubble.ts +6 -1
- package/src/components/reasoning-bubble.ts +2 -0
- package/src/components/tool-bubble.ts +2 -0
- package/src/index.ts +6 -0
- package/src/runtime/init.ts +5 -33
- package/src/session.ts +15 -0
- package/src/styles/widget.css +25 -8
- package/src/types.ts +140 -1
- package/src/ui.ts +414 -20
- package/src/utils/actions.ts +228 -0
- package/src/utils/events.ts +41 -0
- package/src/utils/formatting.test.ts +10 -4
- package/src/utils/formatting.ts +22 -10
- package/src/utils/storage.ts +72 -0
package/src/types.ts
CHANGED
|
@@ -1,5 +1,104 @@
|
|
|
1
1
|
import type { AgentWidgetPlugin } from "./plugins/types";
|
|
2
2
|
|
|
3
|
+
export type AgentWidgetContextProviderContext = {
|
|
4
|
+
messages: AgentWidgetMessage[];
|
|
5
|
+
config: AgentWidgetConfig;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type AgentWidgetContextProvider = (
|
|
9
|
+
context: AgentWidgetContextProviderContext
|
|
10
|
+
) =>
|
|
11
|
+
| Record<string, unknown>
|
|
12
|
+
| void
|
|
13
|
+
| Promise<Record<string, unknown> | void>;
|
|
14
|
+
|
|
15
|
+
export type AgentWidgetRequestPayloadMessage = {
|
|
16
|
+
role: AgentWidgetMessageRole;
|
|
17
|
+
content: string;
|
|
18
|
+
createdAt: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type AgentWidgetRequestPayload = {
|
|
22
|
+
messages: AgentWidgetRequestPayloadMessage[];
|
|
23
|
+
flowId?: string;
|
|
24
|
+
context?: Record<string, unknown>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type AgentWidgetRequestMiddlewareContext = {
|
|
28
|
+
payload: AgentWidgetRequestPayload;
|
|
29
|
+
config: AgentWidgetConfig;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type AgentWidgetRequestMiddleware = (
|
|
33
|
+
context: AgentWidgetRequestMiddlewareContext
|
|
34
|
+
) => AgentWidgetRequestPayload | void | Promise<AgentWidgetRequestPayload | void>;
|
|
35
|
+
|
|
36
|
+
export type AgentWidgetParsedAction = {
|
|
37
|
+
type: string;
|
|
38
|
+
payload: Record<string, unknown>;
|
|
39
|
+
raw?: unknown;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type AgentWidgetActionParserInput = {
|
|
43
|
+
text: string;
|
|
44
|
+
message: AgentWidgetMessage;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type AgentWidgetActionParser = (
|
|
48
|
+
input: AgentWidgetActionParserInput
|
|
49
|
+
) => AgentWidgetParsedAction | null | undefined;
|
|
50
|
+
|
|
51
|
+
export type AgentWidgetActionHandlerResult = {
|
|
52
|
+
handled?: boolean;
|
|
53
|
+
displayText?: string;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type AgentWidgetActionContext = {
|
|
57
|
+
message: AgentWidgetMessage;
|
|
58
|
+
metadata: Record<string, unknown>;
|
|
59
|
+
updateMetadata: (
|
|
60
|
+
updater: (prev: Record<string, unknown>) => Record<string, unknown>
|
|
61
|
+
) => void;
|
|
62
|
+
document: Document | null;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type AgentWidgetActionHandler = (
|
|
66
|
+
action: AgentWidgetParsedAction,
|
|
67
|
+
context: AgentWidgetActionContext
|
|
68
|
+
) => AgentWidgetActionHandlerResult | void;
|
|
69
|
+
|
|
70
|
+
export type AgentWidgetStoredState = {
|
|
71
|
+
messages?: AgentWidgetMessage[];
|
|
72
|
+
metadata?: Record<string, unknown>;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export interface AgentWidgetStorageAdapter {
|
|
76
|
+
load?: () =>
|
|
77
|
+
| AgentWidgetStoredState
|
|
78
|
+
| null
|
|
79
|
+
| Promise<AgentWidgetStoredState | null>;
|
|
80
|
+
save?: (state: AgentWidgetStoredState) => void | Promise<void>;
|
|
81
|
+
clear?: () => void | Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type AgentWidgetVoiceStateEvent = {
|
|
85
|
+
active: boolean;
|
|
86
|
+
source: "user" | "auto" | "restore" | "system";
|
|
87
|
+
timestamp: number;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export type AgentWidgetActionEventPayload = {
|
|
91
|
+
action: AgentWidgetParsedAction;
|
|
92
|
+
message: AgentWidgetMessage;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export type AgentWidgetControllerEventMap = {
|
|
96
|
+
"assistant:message": AgentWidgetMessage;
|
|
97
|
+
"assistant:complete": AgentWidgetMessage;
|
|
98
|
+
"voice:state": AgentWidgetVoiceStateEvent;
|
|
99
|
+
"action:detected": AgentWidgetActionEventPayload;
|
|
100
|
+
};
|
|
101
|
+
|
|
3
102
|
export type AgentWidgetFeatureFlags = {
|
|
4
103
|
showReasoning?: boolean;
|
|
5
104
|
showToolCalls?: boolean;
|
|
@@ -136,6 +235,7 @@ export type AgentWidgetVoiceRecognitionConfig = {
|
|
|
136
235
|
recordingBackgroundColor?: string;
|
|
137
236
|
recordingBorderColor?: string;
|
|
138
237
|
showRecordingIndicator?: boolean;
|
|
238
|
+
autoResume?: boolean | "assistant";
|
|
139
239
|
};
|
|
140
240
|
|
|
141
241
|
export type AgentWidgetToolCallConfig = {
|
|
@@ -182,7 +282,9 @@ export interface AgentWidgetStreamParserResult {
|
|
|
182
282
|
text: string | null;
|
|
183
283
|
|
|
184
284
|
/**
|
|
185
|
-
*
|
|
285
|
+
* The raw accumulated content. Built-in parsers always populate this so
|
|
286
|
+
* downstream middleware (action handlers, logging, etc.) can
|
|
287
|
+
* inspect/parse the original structured payload.
|
|
186
288
|
*/
|
|
187
289
|
raw?: string;
|
|
188
290
|
}
|
|
@@ -239,8 +341,14 @@ export type AgentWidgetConfig = {
|
|
|
239
341
|
text: string;
|
|
240
342
|
message: AgentWidgetMessage;
|
|
241
343
|
streaming: boolean;
|
|
344
|
+
raw?: string;
|
|
242
345
|
}) => string;
|
|
243
346
|
plugins?: AgentWidgetPlugin[];
|
|
347
|
+
contextProviders?: AgentWidgetContextProvider[];
|
|
348
|
+
requestMiddleware?: AgentWidgetRequestMiddleware;
|
|
349
|
+
actionParsers?: AgentWidgetActionParser[];
|
|
350
|
+
actionHandlers?: AgentWidgetActionHandler[];
|
|
351
|
+
storageAdapter?: AgentWidgetStorageAdapter;
|
|
244
352
|
/**
|
|
245
353
|
* Custom stream parser for extracting text from streaming structured responses.
|
|
246
354
|
* Handles incremental parsing of JSON, XML, or other formats.
|
|
@@ -272,6 +380,31 @@ export type AgentWidgetConfig = {
|
|
|
272
380
|
* ```
|
|
273
381
|
*/
|
|
274
382
|
clearChatHistoryStorageKey?: string;
|
|
383
|
+
/**
|
|
384
|
+
* Built-in parser type selector. Provides an easy way to choose a parser without importing functions.
|
|
385
|
+
* If both `parserType` and `streamParser` are provided, `streamParser` takes precedence.
|
|
386
|
+
*
|
|
387
|
+
* - `"plain"` - Plain text parser (default). Passes through text as-is.
|
|
388
|
+
* - `"json"` - JSON parser using partial-json. Extracts `text` field from JSON objects incrementally.
|
|
389
|
+
* - `"regex-json"` - Regex-based JSON parser. Less robust but faster fallback for simple JSON.
|
|
390
|
+
* - `"xml"` - XML parser. Extracts text content from XML tags.
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```typescript
|
|
394
|
+
* config: {
|
|
395
|
+
* parserType: "json" // Use built-in JSON parser
|
|
396
|
+
* }
|
|
397
|
+
* ```
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```typescript
|
|
401
|
+
* config: {
|
|
402
|
+
* parserType: "json",
|
|
403
|
+
* streamParser: () => customParser() // Custom parser overrides parserType
|
|
404
|
+
* }
|
|
405
|
+
* ```
|
|
406
|
+
*/
|
|
407
|
+
parserType?: "plain" | "json" | "regex-json" | "xml";
|
|
275
408
|
};
|
|
276
409
|
|
|
277
410
|
export type AgentWidgetMessageRole = "user" | "assistant" | "system";
|
|
@@ -328,6 +461,11 @@ export type AgentWidgetMessage = {
|
|
|
328
461
|
toolCall?: AgentWidgetToolCall;
|
|
329
462
|
tools?: AgentWidgetToolCall[];
|
|
330
463
|
viaVoice?: boolean;
|
|
464
|
+
/**
|
|
465
|
+
* Raw structured payload for this message (e.g., JSON action response).
|
|
466
|
+
* Populated automatically when structured parsers run.
|
|
467
|
+
*/
|
|
468
|
+
rawContent?: string;
|
|
331
469
|
};
|
|
332
470
|
|
|
333
471
|
export type AgentWidgetEvent =
|
|
@@ -341,4 +479,5 @@ export type AgentWidgetInitOptions = {
|
|
|
341
479
|
useShadowDom?: boolean;
|
|
342
480
|
onReady?: () => void;
|
|
343
481
|
windowKey?: string; // If provided, stores the controller on window[windowKey] for global access
|
|
482
|
+
debugTools?: boolean;
|
|
344
483
|
};
|