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 CHANGED
@@ -405,6 +405,7 @@ This ensures all configuration values are set to sensible defaults while allowin
405
405
  | `initialMessages` | `AgentWidgetMessage[]` | Seed the conversation transcript. |
406
406
  | `suggestionChips` | `string[]` | Render quick reply buttons above the composer. |
407
407
  | `postprocessMessage` | `(ctx) => string` | Transform message text before it renders (return HTML). Combine with `markdownPostprocessor` for rich output. |
408
+ | `parserType` | `"plain" \| "json" \| "regex-json" \| "xml"` | Built-in parser type selector. Easy way to choose a parser without importing functions. Options: `"plain"` (default), `"json"` (partial-json), `"regex-json"` (regex-based), `"xml"`. If both `parserType` and `streamParser` are provided, `streamParser` takes precedence. |
408
409
  | `streamParser` | `() => AgentWidgetStreamParser` | Custom stream parser for detecting formats and extracting text from streaming responses. Handles JSON, XML, or custom formats. See [Stream Parser Configuration](#stream-parser-configuration) below. |
409
410
  | `clearChatHistoryStorageKey` | `string` | Additional localStorage key to clear when the clear chat button is clicked. The widget automatically clears `"vanilla-agent-chat-history"` by default. Use this option to clear additional keys (e.g., if you're using a custom storage key). |
410
411
  | `formEndpoint` | `string` | Endpoint used by built-in directives (defaults to `/form`). |
@@ -415,14 +416,30 @@ All options are safe to mutate via `initAgentWidget(...).update(newConfig)`.
415
416
 
416
417
  ### Stream Parser Configuration
417
418
 
418
- The widget can parse structured responses (JSON, XML, etc.) that stream in chunk by chunk, extracting the `text` field for display. By default, it uses a schema-stream based JSON parser. You can provide a custom parser to handle different formats, structures, or parsing strategies.
419
+ The widget can parse structured responses (JSON, XML, etc.) that stream in chunk by chunk, extracting the `text` field for display. By default, it uses a plain text parser. You can easily select a built-in parser using `parserType`, or provide a custom parser via `streamParser`.
419
420
 
420
421
  **Key benefits of the unified stream parser:**
421
422
  - **Format detection**: Automatically detects if content matches your parser's format
422
423
  - **Extensible**: Handle JSON, XML, or any custom structured format
423
424
  - **Incremental parsing**: Extract text as it streams in, not just when complete
424
425
 
425
- **Using built-in parsers with ESM/Modules:**
426
+ **Quick start with `parserType` (recommended):**
427
+
428
+ The easiest way to use a built-in parser is with the `parserType` option:
429
+
430
+ ```javascript
431
+ import { initAgentWidget } from 'vanilla-agent';
432
+
433
+ const controller = initAgentWidget({
434
+ target: '#chat-root',
435
+ config: {
436
+ apiUrl: '/api/chat/dispatch',
437
+ parserType: 'json' // Options: 'plain', 'json', 'regex-json', 'xml'
438
+ }
439
+ });
440
+ ```
441
+
442
+ **Using built-in parsers with `streamParser` (ESM/Modules):**
426
443
 
427
444
  ```javascript
428
445
  import { initAgentWidget, createPlainTextParser, createJsonStreamParser, createXmlParser } from 'vanilla-agent';
@@ -456,6 +473,23 @@ const controller = initAgentWidget({
456
473
 
457
474
  **Using with automatic installer script:**
458
475
 
476
+ ```html
477
+ <script>
478
+ window.siteAgentConfig = {
479
+ target: 'body',
480
+ config: {
481
+ apiUrl: '/api/chat/dispatch',
482
+ parserType: 'json' // Simple way to select parser - no function imports needed!
483
+ }
484
+ };
485
+ </script>
486
+ <script src="https://cdn.jsdelivr.net/npm/vanilla-agent@latest/dist/install.global.js"></script>
487
+ ```
488
+
489
+ **Alternative: Using `streamParser` with installer script:**
490
+
491
+ If you need a custom parser, you can still use `streamParser`:
492
+
459
493
  ```html
460
494
  <script>
461
495
  window.siteAgentConfig = {
@@ -522,11 +556,15 @@ const jsonParser = () => {
522
556
  };
523
557
  };
524
558
 
525
- const controller = initAgentWidget({
559
+ initAgentWidget({
526
560
  target: '#chat-root',
527
561
  config: {
528
562
  apiUrl: '/api/chat/dispatch',
529
- streamParser: jsonParser
563
+ streamParser: jsonParser,
564
+ postprocessMessage: ({ text, raw }) => {
565
+ // raw contains the structured payload (JSON, XML, etc.)
566
+ return markdownPostprocessor(text);
567
+ }
530
568
  }
531
569
  });
532
570
  ```
@@ -576,7 +614,7 @@ interface AgentWidgetStreamParser {
576
614
  }
577
615
  ```
578
616
 
579
- The parser's `processChunk` method is called for each chunk. If the content matches your parser's format, return the extracted text. If it doesn't match (or text isn't available yet), return `null` and the content will be treated as plain text. This allows you to display the text value as it streams in without showing raw structured characters.
617
+ The parser's `processChunk` method is called for each chunk. If the content matches your parser's format, return the extracted text and the raw payload. Built-in parsers already do this, so action handlers and middleware can read the original structured content without re-implementing a parser. Return `null` if the chunk isn't ready yet—the widget will keep waiting or fall back to plain text.
580
618
 
581
619
  ### Optional proxy server
582
620