vanilla-agent 1.31.0 → 1.32.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/dist/index.cjs +14 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -2
- package/dist/index.d.ts +33 -2
- package/dist/index.global.js +23 -23
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +14 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +25 -6
- package/src/types.ts +31 -0
- package/src/ui.ts +26 -8
- package/src/utils/actions.ts +7 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vanilla-agent",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.32.0",
|
|
4
4
|
"description": "Themeable, plugable streaming agent widget for websites, in plain JS with support for voice input and reasoning / tool output.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
package/src/client.ts
CHANGED
|
@@ -139,15 +139,21 @@ export class AgentWidgetClient {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
private async _doInitSession(): Promise<ClientSession> {
|
|
142
|
+
// Get stored session_id if available (for session resumption)
|
|
143
|
+
const storedSessionId = this.config.getStoredSessionId?.() || null;
|
|
144
|
+
|
|
145
|
+
const requestBody: Record<string, unknown> = {
|
|
146
|
+
token: this.config.clientToken,
|
|
147
|
+
...(this.config.flowId && { flow_id: this.config.flowId }),
|
|
148
|
+
...(storedSessionId && { session_id: storedSessionId }),
|
|
149
|
+
};
|
|
150
|
+
|
|
142
151
|
const response = await fetch(this.getClientApiUrl('init'), {
|
|
143
152
|
method: 'POST',
|
|
144
153
|
headers: {
|
|
145
154
|
'Content-Type': 'application/json',
|
|
146
155
|
},
|
|
147
|
-
body: JSON.stringify(
|
|
148
|
-
token: this.config.clientToken,
|
|
149
|
-
flow_id: this.config.flowId,
|
|
150
|
-
}),
|
|
156
|
+
body: JSON.stringify(requestBody),
|
|
151
157
|
});
|
|
152
158
|
|
|
153
159
|
if (!response.ok) {
|
|
@@ -162,6 +168,12 @@ export class AgentWidgetClient {
|
|
|
162
168
|
}
|
|
163
169
|
|
|
164
170
|
const data: ClientInitResponse = await response.json();
|
|
171
|
+
|
|
172
|
+
// Store the new session_id for future resumption
|
|
173
|
+
if (this.config.setStoredSessionId) {
|
|
174
|
+
this.config.setStoredSessionId(data.session_id);
|
|
175
|
+
}
|
|
176
|
+
|
|
165
177
|
return {
|
|
166
178
|
sessionId: data.session_id,
|
|
167
179
|
expiresAt: new Date(data.expires_at),
|
|
@@ -381,6 +393,13 @@ export class AgentWidgetClient {
|
|
|
381
393
|
const basePayload = await this.buildPayload(options.messages);
|
|
382
394
|
|
|
383
395
|
// Build the chat request payload with message IDs for feedback tracking
|
|
396
|
+
// Filter out session_id from metadata if present (it's only for local storage)
|
|
397
|
+
const sanitizedMetadata = basePayload.metadata
|
|
398
|
+
? Object.fromEntries(
|
|
399
|
+
Object.entries(basePayload.metadata).filter(([key]) => key !== 'session_id')
|
|
400
|
+
)
|
|
401
|
+
: undefined;
|
|
402
|
+
|
|
384
403
|
const chatRequest: ClientChatRequest = {
|
|
385
404
|
session_id: session.sessionId,
|
|
386
405
|
messages: options.messages.map(m => ({
|
|
@@ -390,8 +409,8 @@ export class AgentWidgetClient {
|
|
|
390
409
|
})),
|
|
391
410
|
// Include pre-generated assistant message ID if provided
|
|
392
411
|
...(options.assistantMessageId && { assistant_message_id: options.assistantMessageId }),
|
|
393
|
-
// Include metadata/context from middleware if present
|
|
394
|
-
...(
|
|
412
|
+
// Include metadata/context from middleware if present (excluding session_id)
|
|
413
|
+
...(sanitizedMetadata && Object.keys(sanitizedMetadata).length > 0 && { metadata: sanitizedMetadata }),
|
|
395
414
|
...(basePayload.context && { context: basePayload.context }),
|
|
396
415
|
};
|
|
397
416
|
|
package/src/types.ts
CHANGED
|
@@ -1114,6 +1114,37 @@ export type AgentWidgetConfig = {
|
|
|
1114
1114
|
* ```
|
|
1115
1115
|
*/
|
|
1116
1116
|
onSessionExpired?: () => void;
|
|
1117
|
+
/**
|
|
1118
|
+
* Get stored session ID for session resumption (client token mode only).
|
|
1119
|
+
* Called when initializing a new session to check if there's a previous session_id
|
|
1120
|
+
* that should be passed to /client/init to resume the same conversation record.
|
|
1121
|
+
*
|
|
1122
|
+
* @example
|
|
1123
|
+
* ```typescript
|
|
1124
|
+
* config: {
|
|
1125
|
+
* getStoredSessionId: () => {
|
|
1126
|
+
* const stored = localStorage.getItem('session_id');
|
|
1127
|
+
* return stored || null;
|
|
1128
|
+
* }
|
|
1129
|
+
* }
|
|
1130
|
+
* ```
|
|
1131
|
+
*/
|
|
1132
|
+
getStoredSessionId?: () => string | null;
|
|
1133
|
+
/**
|
|
1134
|
+
* Store session ID for session resumption (client token mode only).
|
|
1135
|
+
* Called when a new session is initialized to persist the session_id
|
|
1136
|
+
* so it can be used to resume the conversation later.
|
|
1137
|
+
*
|
|
1138
|
+
* @example
|
|
1139
|
+
* ```typescript
|
|
1140
|
+
* config: {
|
|
1141
|
+
* setStoredSessionId: (sessionId) => {
|
|
1142
|
+
* localStorage.setItem('session_id', sessionId);
|
|
1143
|
+
* }
|
|
1144
|
+
* }
|
|
1145
|
+
* ```
|
|
1146
|
+
*/
|
|
1147
|
+
setStoredSessionId?: (sessionId: string) => void;
|
|
1117
1148
|
/**
|
|
1118
1149
|
* Static headers to include with each request.
|
|
1119
1150
|
* For dynamic headers (e.g., auth tokens), use `getHeaders` instead.
|
package/src/ui.ts
CHANGED
|
@@ -204,8 +204,8 @@ export const createAgentExperience = (
|
|
|
204
204
|
}
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
const
|
|
208
|
-
const
|
|
207
|
+
const getSessionMetadata = () => persistentMetadata;
|
|
208
|
+
const updateSessionMetadata = (
|
|
209
209
|
updater: (prev: Record<string, unknown>) => Record<string, unknown>
|
|
210
210
|
) => {
|
|
211
211
|
const next = updater({ ...persistentMetadata }) ?? {};
|
|
@@ -226,8 +226,8 @@ export const createAgentExperience = (
|
|
|
226
226
|
let actionManager = createActionManager({
|
|
227
227
|
parsers: resolvedActionParsers,
|
|
228
228
|
handlers: resolvedActionHandlers,
|
|
229
|
-
|
|
230
|
-
|
|
229
|
+
getSessionMetadata,
|
|
230
|
+
updateSessionMetadata,
|
|
231
231
|
emit: eventBus.emit,
|
|
232
232
|
documentRef: typeof document !== "undefined" ? document : null
|
|
233
233
|
});
|
|
@@ -758,7 +758,7 @@ export const createAgentExperience = (
|
|
|
758
758
|
});
|
|
759
759
|
};
|
|
760
760
|
const persistVoiceMetadata = () => {
|
|
761
|
-
|
|
761
|
+
updateSessionMetadata((prev) => ({
|
|
762
762
|
...prev,
|
|
763
763
|
voiceState: {
|
|
764
764
|
active: voiceState.active,
|
|
@@ -1282,6 +1282,24 @@ export const createAgentExperience = (
|
|
|
1282
1282
|
textarea.style.fontWeight = fontWeight;
|
|
1283
1283
|
};
|
|
1284
1284
|
|
|
1285
|
+
// Add session ID persistence callbacks for client token mode
|
|
1286
|
+
// These allow the widget to resume conversations by passing session_id to /client/init
|
|
1287
|
+
if (config.clientToken) {
|
|
1288
|
+
config = {
|
|
1289
|
+
...config,
|
|
1290
|
+
getStoredSessionId: () => {
|
|
1291
|
+
const storedId = persistentMetadata['session_id'];
|
|
1292
|
+
return typeof storedId === 'string' ? storedId : null;
|
|
1293
|
+
},
|
|
1294
|
+
setStoredSessionId: (sessionId: string) => {
|
|
1295
|
+
updateSessionMetadata((prev) => ({
|
|
1296
|
+
...prev,
|
|
1297
|
+
session_id: sessionId,
|
|
1298
|
+
}));
|
|
1299
|
+
},
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1285
1303
|
session = new AgentWidgetSession(config, {
|
|
1286
1304
|
onMessagesChanged(messages) {
|
|
1287
1305
|
renderMessagesWithPlugins(messagesWrapper, messages, postprocess);
|
|
@@ -2570,8 +2588,8 @@ export const createAgentExperience = (
|
|
|
2570
2588
|
actionManager = createActionManager({
|
|
2571
2589
|
parsers: nextParsers,
|
|
2572
2590
|
handlers: nextHandlers,
|
|
2573
|
-
|
|
2574
|
-
|
|
2591
|
+
getSessionMetadata,
|
|
2592
|
+
updateSessionMetadata,
|
|
2575
2593
|
emit: eventBus.emit,
|
|
2576
2594
|
documentRef: typeof document !== "undefined" ? document : null
|
|
2577
2595
|
});
|
|
@@ -3015,7 +3033,7 @@ export const createAgentExperience = (
|
|
|
3015
3033
|
updatePersistentMetadata(
|
|
3016
3034
|
updater: (prev: Record<string, unknown>) => Record<string, unknown>
|
|
3017
3035
|
) {
|
|
3018
|
-
|
|
3036
|
+
updateSessionMetadata(updater);
|
|
3019
3037
|
},
|
|
3020
3038
|
on(event, handler) {
|
|
3021
3039
|
return eventBus.on(event, handler);
|
package/src/utils/actions.ts
CHANGED
|
@@ -19,8 +19,8 @@ type ActionManagerProcessContext = {
|
|
|
19
19
|
type ActionManagerOptions = {
|
|
20
20
|
parsers: AgentWidgetActionParser[];
|
|
21
21
|
handlers: AgentWidgetActionHandler[];
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
getSessionMetadata: () => Record<string, unknown>;
|
|
23
|
+
updateSessionMetadata: (
|
|
24
24
|
updater: (prev: Record<string, unknown>) => Record<string, unknown>
|
|
25
25
|
) => void;
|
|
26
26
|
emit: <K extends keyof AgentWidgetControllerEventMap>(
|
|
@@ -123,18 +123,18 @@ const ensureArrayOfStrings = (value: unknown): string[] => {
|
|
|
123
123
|
|
|
124
124
|
export const createActionManager = (options: ActionManagerOptions) => {
|
|
125
125
|
let processedIds = new Set(
|
|
126
|
-
ensureArrayOfStrings(options.
|
|
126
|
+
ensureArrayOfStrings(options.getSessionMetadata().processedActionMessageIds)
|
|
127
127
|
);
|
|
128
128
|
|
|
129
129
|
const syncFromMetadata = () => {
|
|
130
130
|
processedIds = new Set(
|
|
131
|
-
ensureArrayOfStrings(options.
|
|
131
|
+
ensureArrayOfStrings(options.getSessionMetadata().processedActionMessageIds)
|
|
132
132
|
);
|
|
133
133
|
};
|
|
134
134
|
|
|
135
135
|
const persistProcessedIds = () => {
|
|
136
136
|
const latestIds = Array.from(processedIds);
|
|
137
|
-
options.
|
|
137
|
+
options.updateSessionMetadata((prev) => ({
|
|
138
138
|
...prev,
|
|
139
139
|
processedActionMessageIds: latestIds
|
|
140
140
|
}));
|
|
@@ -195,8 +195,8 @@ export const createActionManager = (options: ActionManagerOptions) => {
|
|
|
195
195
|
try {
|
|
196
196
|
const handlerResult = handler(action, {
|
|
197
197
|
message: context.message,
|
|
198
|
-
metadata: options.
|
|
199
|
-
updateMetadata: options.
|
|
198
|
+
metadata: options.getSessionMetadata(),
|
|
199
|
+
updateMetadata: options.updateSessionMetadata,
|
|
200
200
|
document: options.documentRef
|
|
201
201
|
} as AgentWidgetActionContext) as AgentWidgetActionHandlerResult | void;
|
|
202
202
|
|