vanilla-agent 1.3.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 = {
@@ -314,6 +414,7 @@ type Controller = {
314
414
  submitMessage: (message?: string) => boolean;
315
415
  startVoiceRecognition: () => boolean;
316
416
  stopVoiceRecognition: () => boolean;
417
+ injectTestMessage: (event: AgentWidgetEvent) => void;
317
418
  };
318
419
  declare const createAgentExperience: (mount: HTMLElement, initialConfig?: AgentWidgetConfig) => Controller;
319
420
  type AgentWidgetController = Controller;
@@ -344,6 +445,7 @@ declare class AgentWidgetSession {
344
445
  getMessages(): AgentWidgetMessage[];
345
446
  getStatus(): AgentWidgetSessionStatus;
346
447
  isStreaming(): boolean;
448
+ injectTestEvent(event: AgentWidgetEvent): void;
347
449
  sendMessage(rawInput: string, options?: {
348
450
  viaVoice?: boolean;
349
451
  }): Promise<void>;
@@ -369,6 +471,7 @@ declare class AgentWidgetClient {
369
471
  private readonly apiUrl;
370
472
  private readonly headers;
371
473
  private readonly debug;
474
+ private readonly createStreamParser;
372
475
  constructor(config?: AgentWidgetConfig);
373
476
  dispatch(options: DispatchOptions, onEvent: SSEHandler): Promise<void>;
374
477
  private streamResponse;
@@ -391,6 +494,33 @@ declare const escapeHtml: (text: string) => string;
391
494
  */
392
495
  declare const directivePostprocessor: (text: string) => string;
393
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
+
394
524
  declare class PluginRegistry {
395
525
  private plugins;
396
526
  /**
@@ -428,4 +558,4 @@ declare const DEFAULT_WIDGET_CONFIG: Partial<AgentWidgetConfig>;
428
558
  */
429
559
  declare function mergeWithDefaults(config?: Partial<AgentWidgetConfig>): Partial<AgentWidgetConfig>;
430
560
 
431
- 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 = {
@@ -314,6 +414,7 @@ type Controller = {
314
414
  submitMessage: (message?: string) => boolean;
315
415
  startVoiceRecognition: () => boolean;
316
416
  stopVoiceRecognition: () => boolean;
417
+ injectTestMessage: (event: AgentWidgetEvent) => void;
317
418
  };
318
419
  declare const createAgentExperience: (mount: HTMLElement, initialConfig?: AgentWidgetConfig) => Controller;
319
420
  type AgentWidgetController = Controller;
@@ -344,6 +445,7 @@ declare class AgentWidgetSession {
344
445
  getMessages(): AgentWidgetMessage[];
345
446
  getStatus(): AgentWidgetSessionStatus;
346
447
  isStreaming(): boolean;
448
+ injectTestEvent(event: AgentWidgetEvent): void;
347
449
  sendMessage(rawInput: string, options?: {
348
450
  viaVoice?: boolean;
349
451
  }): Promise<void>;
@@ -369,6 +471,7 @@ declare class AgentWidgetClient {
369
471
  private readonly apiUrl;
370
472
  private readonly headers;
371
473
  private readonly debug;
474
+ private readonly createStreamParser;
372
475
  constructor(config?: AgentWidgetConfig);
373
476
  dispatch(options: DispatchOptions, onEvent: SSEHandler): Promise<void>;
374
477
  private streamResponse;
@@ -391,6 +494,33 @@ declare const escapeHtml: (text: string) => string;
391
494
  */
392
495
  declare const directivePostprocessor: (text: string) => string;
393
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
+
394
524
  declare class PluginRegistry {
395
525
  private plugins;
396
526
  /**
@@ -428,4 +558,4 @@ declare const DEFAULT_WIDGET_CONFIG: Partial<AgentWidgetConfig>;
428
558
  */
429
559
  declare function mergeWithDefaults(config?: Partial<AgentWidgetConfig>): Partial<AgentWidgetConfig>;
430
560
 
431
- 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 };