vanilla-agent 1.2.0 → 1.4.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.d.cts CHANGED
@@ -206,6 +206,74 @@ type AgentWidgetVoiceRecognitionConfig = {
206
206
  recordingBorderColor?: string;
207
207
  showRecordingIndicator?: boolean;
208
208
  };
209
+ type AgentWidgetToolCallConfig = {
210
+ backgroundColor?: string;
211
+ borderColor?: string;
212
+ borderWidth?: string;
213
+ borderRadius?: string;
214
+ headerBackgroundColor?: string;
215
+ headerTextColor?: string;
216
+ headerPaddingX?: string;
217
+ headerPaddingY?: string;
218
+ contentBackgroundColor?: string;
219
+ contentTextColor?: string;
220
+ contentPaddingX?: string;
221
+ contentPaddingY?: string;
222
+ codeBlockBackgroundColor?: string;
223
+ codeBlockBorderColor?: string;
224
+ codeBlockTextColor?: string;
225
+ toggleTextColor?: string;
226
+ labelTextColor?: string;
227
+ };
228
+ /**
229
+ * Interface for pluggable stream parsers that extract text from streaming responses.
230
+ * Parsers handle incremental parsing to extract text values from structured formats (JSON, XML, etc.).
231
+ *
232
+ * @example
233
+ * ```typescript
234
+ * const jsonParser: AgentWidgetStreamParser = {
235
+ * processChunk: async (content) => {
236
+ * // Extract text from JSON - return null if not JSON or text not available yet
237
+ * if (!content.trim().startsWith('{')) return null;
238
+ * const match = content.match(/"text"\s*:\s*"([^"]*)"/);
239
+ * return match ? match[1] : null;
240
+ * },
241
+ * getExtractedText: () => extractedText
242
+ * };
243
+ * ```
244
+ */
245
+ interface AgentWidgetStreamParserResult {
246
+ /**
247
+ * The extracted text to display (may be partial during streaming)
248
+ */
249
+ text: string | null;
250
+ /**
251
+ * Optional: The raw accumulated content (useful for middleware that needs the original format)
252
+ */
253
+ raw?: string;
254
+ }
255
+ interface AgentWidgetStreamParser {
256
+ /**
257
+ * Process a chunk of content and return the extracted text (if available).
258
+ * This method is called for each chunk as it arrives during streaming.
259
+ * Return null if the content doesn't match this parser's format or if text is not yet available.
260
+ *
261
+ * @param accumulatedContent - The full accumulated content so far (including new chunk)
262
+ * @returns The extracted text value and optionally raw content, or null if not yet available or format doesn't match
263
+ */
264
+ processChunk(accumulatedContent: string): Promise<AgentWidgetStreamParserResult | string | null> | AgentWidgetStreamParserResult | string | null;
265
+ /**
266
+ * Get the currently extracted text value (may be partial).
267
+ * This is called synchronously to get the latest extracted text without processing.
268
+ *
269
+ * @returns The currently extracted text value, or null if not yet available
270
+ */
271
+ getExtractedText(): string | null;
272
+ /**
273
+ * Clean up any resources when parsing is complete.
274
+ */
275
+ close?(): Promise<void> | void;
276
+ }
209
277
  type AgentWidgetConfig = {
210
278
  apiUrl?: string;
211
279
  flowId?: string;
@@ -227,12 +295,44 @@ type AgentWidgetConfig = {
227
295
  sendButton?: AgentWidgetSendButtonConfig;
228
296
  statusIndicator?: AgentWidgetStatusIndicatorConfig;
229
297
  voiceRecognition?: AgentWidgetVoiceRecognitionConfig;
298
+ toolCall?: AgentWidgetToolCallConfig;
230
299
  postprocessMessage?: (context: {
231
300
  text: string;
232
301
  message: AgentWidgetMessage;
233
302
  streaming: boolean;
234
303
  }) => string;
235
304
  plugins?: AgentWidgetPlugin[];
305
+ /**
306
+ * Custom stream parser for extracting text from streaming structured responses.
307
+ * Handles incremental parsing of JSON, XML, or other formats.
308
+ * If not provided, uses the default JSON parser.
309
+ *
310
+ * @example
311
+ * ```typescript
312
+ * streamParser: () => ({
313
+ * processChunk: async (content) => {
314
+ * // Return null if not your format, or extracted text if available
315
+ * if (!content.trim().startsWith('{')) return null;
316
+ * return extractText(content);
317
+ * },
318
+ * getExtractedText: () => extractedText
319
+ * })
320
+ * ```
321
+ */
322
+ streamParser?: () => AgentWidgetStreamParser;
323
+ /**
324
+ * Additional localStorage key to clear when the clear chat button is clicked.
325
+ * The widget automatically clears `"vanilla-agent-chat-history"` by default.
326
+ * Use this option to clear additional keys (e.g., if you're using a custom storage key).
327
+ *
328
+ * @example
329
+ * ```typescript
330
+ * config: {
331
+ * clearChatHistoryStorageKey: "my-custom-chat-history"
332
+ * }
333
+ * ```
334
+ */
335
+ clearChatHistoryStorageKey?: string;
236
336
  };
237
337
  type AgentWidgetMessageRole = "user" | "assistant" | "system";
238
338
  type AgentWidgetReasoning = {
@@ -256,6 +356,22 @@ type AgentWidgetToolCall = {
256
356
  durationMs?: number;
257
357
  };
258
358
  type AgentWidgetMessageVariant = "assistant" | "reasoning" | "tool";
359
+ /**
360
+ * Represents a message in the chat conversation.
361
+ *
362
+ * @property id - Unique message identifier
363
+ * @property role - Message role: "user", "assistant", or "system"
364
+ * @property content - Message text content
365
+ * @property createdAt - ISO timestamp when message was created
366
+ * @property streaming - Whether message is still streaming (for assistant messages)
367
+ * @property variant - Message variant for assistant messages: "assistant", "reasoning", or "tool"
368
+ * @property sequence - Message ordering number
369
+ * @property reasoning - Reasoning data for assistant reasoning messages
370
+ * @property toolCall - Tool call data for assistant tool messages
371
+ * @property tools - Array of tool calls
372
+ * @property viaVoice - Set to `true` when a user message is sent via voice recognition.
373
+ * Useful for implementing voice-specific behaviors like auto-reactivation.
374
+ */
259
375
  type AgentWidgetMessage = {
260
376
  id: string;
261
377
  role: AgentWidgetMessageRole;
@@ -267,6 +383,7 @@ type AgentWidgetMessage = {
267
383
  reasoning?: AgentWidgetReasoning;
268
384
  toolCall?: AgentWidgetToolCall;
269
385
  tools?: AgentWidgetToolCall[];
386
+ viaVoice?: boolean;
270
387
  };
271
388
  type AgentWidgetEvent = {
272
389
  type: "message";
@@ -297,6 +414,7 @@ type Controller = {
297
414
  submitMessage: (message?: string) => boolean;
298
415
  startVoiceRecognition: () => boolean;
299
416
  stopVoiceRecognition: () => boolean;
417
+ injectTestMessage: (event: AgentWidgetEvent) => void;
300
418
  };
301
419
  declare const createAgentExperience: (mount: HTMLElement, initialConfig?: AgentWidgetConfig) => Controller;
302
420
  type AgentWidgetController = Controller;
@@ -327,7 +445,10 @@ declare class AgentWidgetSession {
327
445
  getMessages(): AgentWidgetMessage[];
328
446
  getStatus(): AgentWidgetSessionStatus;
329
447
  isStreaming(): boolean;
330
- sendMessage(rawInput: string): Promise<void>;
448
+ injectTestEvent(event: AgentWidgetEvent): void;
449
+ sendMessage(rawInput: string, options?: {
450
+ viaVoice?: boolean;
451
+ }): Promise<void>;
331
452
  cancel(): void;
332
453
  clearMessages(): void;
333
454
  private handleEvent;
@@ -350,6 +471,7 @@ declare class AgentWidgetClient {
350
471
  private readonly apiUrl;
351
472
  private readonly headers;
352
473
  private readonly debug;
474
+ private readonly createStreamParser;
353
475
  constructor(config?: AgentWidgetConfig);
354
476
  dispatch(options: DispatchOptions, onEvent: SSEHandler): Promise<void>;
355
477
  private streamResponse;
@@ -372,6 +494,33 @@ declare const escapeHtml: (text: string) => string;
372
494
  */
373
495
  declare const directivePostprocessor: (text: string) => string;
374
496
 
497
+ /**
498
+ * Plain text parser - passes through text as-is without any parsing.
499
+ * This is the default parser.
500
+ */
501
+ declare const createPlainTextParser: () => AgentWidgetStreamParser;
502
+ /**
503
+ * JSON parser using regex-based extraction.
504
+ * Extracts the 'text' field from JSON responses using regex patterns.
505
+ * This is a simpler regex-based alternative to createJsonStreamParser.
506
+ * Less robust for complex/malformed JSON but has no external dependencies.
507
+ */
508
+ declare const createRegexJsonParser: () => AgentWidgetStreamParser;
509
+ /**
510
+ * JSON stream parser using partial-json library.
511
+ * Extracts the 'text' field from JSON responses using the partial-json library,
512
+ * which is specifically designed for parsing incomplete JSON from LLMs.
513
+ * This is the recommended parser as it's more robust than regex.
514
+ *
515
+ * Library: https://github.com/promplate/partial-json-parser-js
516
+ */
517
+ declare const createJsonStreamParser: () => AgentWidgetStreamParser;
518
+ /**
519
+ * XML stream parser.
520
+ * Extracts text from <text>...</text> tags in XML responses.
521
+ */
522
+ declare const createXmlParser: () => AgentWidgetStreamParser;
523
+
375
524
  declare class PluginRegistry {
376
525
  private plugins;
377
526
  /**
@@ -409,4 +558,4 @@ declare const DEFAULT_WIDGET_CONFIG: Partial<AgentWidgetConfig>;
409
558
  */
410
559
  declare function mergeWithDefaults(config?: Partial<AgentWidgetConfig>): Partial<AgentWidgetConfig>;
411
560
 
412
- export { AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetMessage, type AgentWidgetPlugin, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetTheme, DEFAULT_WIDGET_CONFIG, createAgentExperience, initAgentWidget as default, directivePostprocessor, escapeHtml, initAgentWidget, markdownPostprocessor, mergeWithDefaults, pluginRegistry };
561
+ export { AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetMessage, type AgentWidgetPlugin, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetStreamParser, type AgentWidgetStreamParserResult, type AgentWidgetTheme, DEFAULT_WIDGET_CONFIG, createAgentExperience, createJsonStreamParser, createPlainTextParser, createRegexJsonParser, createXmlParser, initAgentWidget as default, directivePostprocessor, escapeHtml, initAgentWidget, markdownPostprocessor, mergeWithDefaults, pluginRegistry };
package/dist/index.d.ts CHANGED
@@ -206,6 +206,74 @@ type AgentWidgetVoiceRecognitionConfig = {
206
206
  recordingBorderColor?: string;
207
207
  showRecordingIndicator?: boolean;
208
208
  };
209
+ type AgentWidgetToolCallConfig = {
210
+ backgroundColor?: string;
211
+ borderColor?: string;
212
+ borderWidth?: string;
213
+ borderRadius?: string;
214
+ headerBackgroundColor?: string;
215
+ headerTextColor?: string;
216
+ headerPaddingX?: string;
217
+ headerPaddingY?: string;
218
+ contentBackgroundColor?: string;
219
+ contentTextColor?: string;
220
+ contentPaddingX?: string;
221
+ contentPaddingY?: string;
222
+ codeBlockBackgroundColor?: string;
223
+ codeBlockBorderColor?: string;
224
+ codeBlockTextColor?: string;
225
+ toggleTextColor?: string;
226
+ labelTextColor?: string;
227
+ };
228
+ /**
229
+ * Interface for pluggable stream parsers that extract text from streaming responses.
230
+ * Parsers handle incremental parsing to extract text values from structured formats (JSON, XML, etc.).
231
+ *
232
+ * @example
233
+ * ```typescript
234
+ * const jsonParser: AgentWidgetStreamParser = {
235
+ * processChunk: async (content) => {
236
+ * // Extract text from JSON - return null if not JSON or text not available yet
237
+ * if (!content.trim().startsWith('{')) return null;
238
+ * const match = content.match(/"text"\s*:\s*"([^"]*)"/);
239
+ * return match ? match[1] : null;
240
+ * },
241
+ * getExtractedText: () => extractedText
242
+ * };
243
+ * ```
244
+ */
245
+ interface AgentWidgetStreamParserResult {
246
+ /**
247
+ * The extracted text to display (may be partial during streaming)
248
+ */
249
+ text: string | null;
250
+ /**
251
+ * Optional: The raw accumulated content (useful for middleware that needs the original format)
252
+ */
253
+ raw?: string;
254
+ }
255
+ interface AgentWidgetStreamParser {
256
+ /**
257
+ * Process a chunk of content and return the extracted text (if available).
258
+ * This method is called for each chunk as it arrives during streaming.
259
+ * Return null if the content doesn't match this parser's format or if text is not yet available.
260
+ *
261
+ * @param accumulatedContent - The full accumulated content so far (including new chunk)
262
+ * @returns The extracted text value and optionally raw content, or null if not yet available or format doesn't match
263
+ */
264
+ processChunk(accumulatedContent: string): Promise<AgentWidgetStreamParserResult | string | null> | AgentWidgetStreamParserResult | string | null;
265
+ /**
266
+ * Get the currently extracted text value (may be partial).
267
+ * This is called synchronously to get the latest extracted text without processing.
268
+ *
269
+ * @returns The currently extracted text value, or null if not yet available
270
+ */
271
+ getExtractedText(): string | null;
272
+ /**
273
+ * Clean up any resources when parsing is complete.
274
+ */
275
+ close?(): Promise<void> | void;
276
+ }
209
277
  type AgentWidgetConfig = {
210
278
  apiUrl?: string;
211
279
  flowId?: string;
@@ -227,12 +295,44 @@ type AgentWidgetConfig = {
227
295
  sendButton?: AgentWidgetSendButtonConfig;
228
296
  statusIndicator?: AgentWidgetStatusIndicatorConfig;
229
297
  voiceRecognition?: AgentWidgetVoiceRecognitionConfig;
298
+ toolCall?: AgentWidgetToolCallConfig;
230
299
  postprocessMessage?: (context: {
231
300
  text: string;
232
301
  message: AgentWidgetMessage;
233
302
  streaming: boolean;
234
303
  }) => string;
235
304
  plugins?: AgentWidgetPlugin[];
305
+ /**
306
+ * Custom stream parser for extracting text from streaming structured responses.
307
+ * Handles incremental parsing of JSON, XML, or other formats.
308
+ * If not provided, uses the default JSON parser.
309
+ *
310
+ * @example
311
+ * ```typescript
312
+ * streamParser: () => ({
313
+ * processChunk: async (content) => {
314
+ * // Return null if not your format, or extracted text if available
315
+ * if (!content.trim().startsWith('{')) return null;
316
+ * return extractText(content);
317
+ * },
318
+ * getExtractedText: () => extractedText
319
+ * })
320
+ * ```
321
+ */
322
+ streamParser?: () => AgentWidgetStreamParser;
323
+ /**
324
+ * Additional localStorage key to clear when the clear chat button is clicked.
325
+ * The widget automatically clears `"vanilla-agent-chat-history"` by default.
326
+ * Use this option to clear additional keys (e.g., if you're using a custom storage key).
327
+ *
328
+ * @example
329
+ * ```typescript
330
+ * config: {
331
+ * clearChatHistoryStorageKey: "my-custom-chat-history"
332
+ * }
333
+ * ```
334
+ */
335
+ clearChatHistoryStorageKey?: string;
236
336
  };
237
337
  type AgentWidgetMessageRole = "user" | "assistant" | "system";
238
338
  type AgentWidgetReasoning = {
@@ -256,6 +356,22 @@ type AgentWidgetToolCall = {
256
356
  durationMs?: number;
257
357
  };
258
358
  type AgentWidgetMessageVariant = "assistant" | "reasoning" | "tool";
359
+ /**
360
+ * Represents a message in the chat conversation.
361
+ *
362
+ * @property id - Unique message identifier
363
+ * @property role - Message role: "user", "assistant", or "system"
364
+ * @property content - Message text content
365
+ * @property createdAt - ISO timestamp when message was created
366
+ * @property streaming - Whether message is still streaming (for assistant messages)
367
+ * @property variant - Message variant for assistant messages: "assistant", "reasoning", or "tool"
368
+ * @property sequence - Message ordering number
369
+ * @property reasoning - Reasoning data for assistant reasoning messages
370
+ * @property toolCall - Tool call data for assistant tool messages
371
+ * @property tools - Array of tool calls
372
+ * @property viaVoice - Set to `true` when a user message is sent via voice recognition.
373
+ * Useful for implementing voice-specific behaviors like auto-reactivation.
374
+ */
259
375
  type AgentWidgetMessage = {
260
376
  id: string;
261
377
  role: AgentWidgetMessageRole;
@@ -267,6 +383,7 @@ type AgentWidgetMessage = {
267
383
  reasoning?: AgentWidgetReasoning;
268
384
  toolCall?: AgentWidgetToolCall;
269
385
  tools?: AgentWidgetToolCall[];
386
+ viaVoice?: boolean;
270
387
  };
271
388
  type AgentWidgetEvent = {
272
389
  type: "message";
@@ -297,6 +414,7 @@ type Controller = {
297
414
  submitMessage: (message?: string) => boolean;
298
415
  startVoiceRecognition: () => boolean;
299
416
  stopVoiceRecognition: () => boolean;
417
+ injectTestMessage: (event: AgentWidgetEvent) => void;
300
418
  };
301
419
  declare const createAgentExperience: (mount: HTMLElement, initialConfig?: AgentWidgetConfig) => Controller;
302
420
  type AgentWidgetController = Controller;
@@ -327,7 +445,10 @@ declare class AgentWidgetSession {
327
445
  getMessages(): AgentWidgetMessage[];
328
446
  getStatus(): AgentWidgetSessionStatus;
329
447
  isStreaming(): boolean;
330
- sendMessage(rawInput: string): Promise<void>;
448
+ injectTestEvent(event: AgentWidgetEvent): void;
449
+ sendMessage(rawInput: string, options?: {
450
+ viaVoice?: boolean;
451
+ }): Promise<void>;
331
452
  cancel(): void;
332
453
  clearMessages(): void;
333
454
  private handleEvent;
@@ -350,6 +471,7 @@ declare class AgentWidgetClient {
350
471
  private readonly apiUrl;
351
472
  private readonly headers;
352
473
  private readonly debug;
474
+ private readonly createStreamParser;
353
475
  constructor(config?: AgentWidgetConfig);
354
476
  dispatch(options: DispatchOptions, onEvent: SSEHandler): Promise<void>;
355
477
  private streamResponse;
@@ -372,6 +494,33 @@ declare const escapeHtml: (text: string) => string;
372
494
  */
373
495
  declare const directivePostprocessor: (text: string) => string;
374
496
 
497
+ /**
498
+ * Plain text parser - passes through text as-is without any parsing.
499
+ * This is the default parser.
500
+ */
501
+ declare const createPlainTextParser: () => AgentWidgetStreamParser;
502
+ /**
503
+ * JSON parser using regex-based extraction.
504
+ * Extracts the 'text' field from JSON responses using regex patterns.
505
+ * This is a simpler regex-based alternative to createJsonStreamParser.
506
+ * Less robust for complex/malformed JSON but has no external dependencies.
507
+ */
508
+ declare const createRegexJsonParser: () => AgentWidgetStreamParser;
509
+ /**
510
+ * JSON stream parser using partial-json library.
511
+ * Extracts the 'text' field from JSON responses using the partial-json library,
512
+ * which is specifically designed for parsing incomplete JSON from LLMs.
513
+ * This is the recommended parser as it's more robust than regex.
514
+ *
515
+ * Library: https://github.com/promplate/partial-json-parser-js
516
+ */
517
+ declare const createJsonStreamParser: () => AgentWidgetStreamParser;
518
+ /**
519
+ * XML stream parser.
520
+ * Extracts text from <text>...</text> tags in XML responses.
521
+ */
522
+ declare const createXmlParser: () => AgentWidgetStreamParser;
523
+
375
524
  declare class PluginRegistry {
376
525
  private plugins;
377
526
  /**
@@ -409,4 +558,4 @@ declare const DEFAULT_WIDGET_CONFIG: Partial<AgentWidgetConfig>;
409
558
  */
410
559
  declare function mergeWithDefaults(config?: Partial<AgentWidgetConfig>): Partial<AgentWidgetConfig>;
411
560
 
412
- export { AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetMessage, type AgentWidgetPlugin, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetTheme, DEFAULT_WIDGET_CONFIG, createAgentExperience, initAgentWidget as default, directivePostprocessor, escapeHtml, initAgentWidget, markdownPostprocessor, mergeWithDefaults, pluginRegistry };
561
+ export { AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetMessage, type AgentWidgetPlugin, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetStreamParser, type AgentWidgetStreamParserResult, type AgentWidgetTheme, DEFAULT_WIDGET_CONFIG, createAgentExperience, createJsonStreamParser, createPlainTextParser, createRegexJsonParser, createXmlParser, initAgentWidget as default, directivePostprocessor, escapeHtml, initAgentWidget, markdownPostprocessor, mergeWithDefaults, pluginRegistry };