vanilla-agent 1.22.0 → 1.24.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
@@ -89,6 +89,7 @@ type AgentWidgetRequestPayload = {
89
89
  messages: AgentWidgetRequestPayloadMessage[];
90
90
  flowId?: string;
91
91
  context?: Record<string, unknown>;
92
+ metadata?: Record<string, unknown>;
92
93
  };
93
94
  type AgentWidgetRequestMiddlewareContext = {
94
95
  payload: AgentWidgetRequestPayload;
@@ -145,6 +146,23 @@ type AgentWidgetMessageFeedback = {
145
146
  };
146
147
  /**
147
148
  * Configuration for message action buttons (copy, upvote, downvote)
149
+ *
150
+ * **Client Token Mode**: When using `clientToken`, feedback is automatically
151
+ * sent to your Travrse backend. Just enable the buttons and you're done!
152
+ * The `onFeedback` and `onCopy` callbacks are optional for additional local handling.
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * // With clientToken - feedback is automatic!
157
+ * config: {
158
+ * clientToken: 'ct_live_...',
159
+ * messageActions: {
160
+ * showUpvote: true,
161
+ * showDownvote: true,
162
+ * // No onFeedback needed - sent to backend automatically
163
+ * }
164
+ * }
165
+ * ```
148
166
  */
149
167
  type AgentWidgetMessageActionsConfig = {
150
168
  /**
@@ -158,13 +176,15 @@ type AgentWidgetMessageActionsConfig = {
158
176
  */
159
177
  showCopy?: boolean;
160
178
  /**
161
- * Show upvote button
162
- * @default false (requires backend)
179
+ * Show upvote button.
180
+ * When using `clientToken`, feedback is sent to the backend automatically.
181
+ * @default false
163
182
  */
164
183
  showUpvote?: boolean;
165
184
  /**
166
- * Show downvote button
167
- * @default false (requires backend)
185
+ * Show downvote button.
186
+ * When using `clientToken`, feedback is sent to the backend automatically.
187
+ * @default false
168
188
  */
169
189
  showDownvote?: boolean;
170
190
  /**
@@ -185,11 +205,19 @@ type AgentWidgetMessageActionsConfig = {
185
205
  */
186
206
  layout?: "pill-inside" | "row-inside";
187
207
  /**
188
- * Callback when user submits feedback (upvote/downvote)
208
+ * Callback when user submits feedback (upvote/downvote).
209
+ *
210
+ * **Note**: When using `clientToken`, feedback is AUTOMATICALLY sent to your
211
+ * backend via `/v1/client/feedback`. This callback is called IN ADDITION to
212
+ * the automatic submission, useful for updating local UI or analytics.
189
213
  */
190
214
  onFeedback?: (feedback: AgentWidgetMessageFeedback) => void;
191
215
  /**
192
- * Callback when user copies a message
216
+ * Callback when user copies a message.
217
+ *
218
+ * **Note**: When using `clientToken`, copy events are AUTOMATICALLY tracked
219
+ * via `/v1/client/feedback`. This callback is called IN ADDITION to the
220
+ * automatic tracking.
193
221
  */
194
222
  onCopy?: (message: AgentWidgetMessage) => void;
195
223
  };
@@ -569,9 +597,31 @@ type ClientInitResponse = {
569
597
  type ClientChatRequest = {
570
598
  session_id: string;
571
599
  messages: Array<{
600
+ id?: string;
572
601
  role: 'user' | 'assistant' | 'system';
573
602
  content: string;
574
603
  }>;
604
+ /** ID for the expected assistant response message */
605
+ assistant_message_id?: string;
606
+ metadata?: Record<string, unknown>;
607
+ context?: Record<string, unknown>;
608
+ };
609
+ /**
610
+ * Feedback types supported by the API
611
+ */
612
+ type ClientFeedbackType = 'upvote' | 'downvote' | 'copy' | 'csat' | 'nps';
613
+ /**
614
+ * Request payload for /v1/client/feedback endpoint
615
+ */
616
+ type ClientFeedbackRequest = {
617
+ session_id: string;
618
+ /** Required for upvote, downvote, copy feedback types */
619
+ message_id?: string;
620
+ type: ClientFeedbackType;
621
+ /** Required for csat (1-5) and nps (0-10) feedback types */
622
+ rating?: number;
623
+ /** Optional comment for any feedback type */
624
+ comment?: string;
575
625
  };
576
626
  /**
577
627
  * Context provided to header render functions
@@ -1358,6 +1408,132 @@ type AgentWidgetInitOptions = {
1358
1408
  debugTools?: boolean;
1359
1409
  };
1360
1410
 
1411
+ type DispatchOptions = {
1412
+ messages: AgentWidgetMessage[];
1413
+ signal?: AbortSignal;
1414
+ /** Pre-generated ID for the expected assistant response (for feedback tracking) */
1415
+ assistantMessageId?: string;
1416
+ };
1417
+ type SSEHandler = (event: AgentWidgetEvent) => void;
1418
+ declare class AgentWidgetClient {
1419
+ private config;
1420
+ private readonly apiUrl;
1421
+ private readonly headers;
1422
+ private readonly debug;
1423
+ private readonly createStreamParser;
1424
+ private readonly contextProviders;
1425
+ private readonly requestMiddleware?;
1426
+ private readonly customFetch?;
1427
+ private readonly parseSSEEvent?;
1428
+ private readonly getHeaders?;
1429
+ private clientSession;
1430
+ private sessionInitPromise;
1431
+ constructor(config?: AgentWidgetConfig);
1432
+ /**
1433
+ * Check if running in client token mode
1434
+ */
1435
+ isClientTokenMode(): boolean;
1436
+ /**
1437
+ * Get the appropriate API URL based on mode
1438
+ */
1439
+ private getClientApiUrl;
1440
+ /**
1441
+ * Get the current client session (if any)
1442
+ */
1443
+ getClientSession(): ClientSession | null;
1444
+ /**
1445
+ * Initialize session for client token mode.
1446
+ * Called automatically on first message if not already initialized.
1447
+ */
1448
+ initSession(): Promise<ClientSession>;
1449
+ private _doInitSession;
1450
+ /**
1451
+ * Clear the current client session
1452
+ */
1453
+ clearClientSession(): void;
1454
+ /**
1455
+ * Get the feedback API URL
1456
+ */
1457
+ private getFeedbackApiUrl;
1458
+ /**
1459
+ * Send feedback for a message (client token mode only).
1460
+ * Supports upvote, downvote, copy, csat, and nps feedback types.
1461
+ *
1462
+ * @param feedback - The feedback request payload
1463
+ * @returns Promise that resolves when feedback is sent successfully
1464
+ * @throws Error if not in client token mode or if session is invalid
1465
+ *
1466
+ * @example
1467
+ * ```typescript
1468
+ * // Message feedback (upvote/downvote/copy)
1469
+ * await client.sendFeedback({
1470
+ * session_id: sessionId,
1471
+ * message_id: messageId,
1472
+ * type: 'upvote'
1473
+ * });
1474
+ *
1475
+ * // CSAT feedback (1-5 rating)
1476
+ * await client.sendFeedback({
1477
+ * session_id: sessionId,
1478
+ * type: 'csat',
1479
+ * rating: 5,
1480
+ * comment: 'Great experience!'
1481
+ * });
1482
+ *
1483
+ * // NPS feedback (0-10 rating)
1484
+ * await client.sendFeedback({
1485
+ * session_id: sessionId,
1486
+ * type: 'nps',
1487
+ * rating: 9
1488
+ * });
1489
+ * ```
1490
+ */
1491
+ sendFeedback(feedback: ClientFeedbackRequest): Promise<void>;
1492
+ /**
1493
+ * Submit message feedback (upvote, downvote, or copy).
1494
+ * Convenience method for sendFeedback with message-level feedback.
1495
+ *
1496
+ * @param messageId - The ID of the message to provide feedback for
1497
+ * @param type - The feedback type: 'upvote', 'downvote', or 'copy'
1498
+ */
1499
+ submitMessageFeedback(messageId: string, type: 'upvote' | 'downvote' | 'copy'): Promise<void>;
1500
+ /**
1501
+ * Submit CSAT (Customer Satisfaction) feedback.
1502
+ * Convenience method for sendFeedback with CSAT feedback.
1503
+ *
1504
+ * @param rating - Rating from 1 to 5
1505
+ * @param comment - Optional comment
1506
+ */
1507
+ submitCSATFeedback(rating: number, comment?: string): Promise<void>;
1508
+ /**
1509
+ * Submit NPS (Net Promoter Score) feedback.
1510
+ * Convenience method for sendFeedback with NPS feedback.
1511
+ *
1512
+ * @param rating - Rating from 0 to 10
1513
+ * @param comment - Optional comment
1514
+ */
1515
+ submitNPSFeedback(rating: number, comment?: string): Promise<void>;
1516
+ /**
1517
+ * Send a message - handles both proxy and client token modes
1518
+ */
1519
+ dispatch(options: DispatchOptions, onEvent: SSEHandler): Promise<void>;
1520
+ /**
1521
+ * Client token mode dispatch
1522
+ */
1523
+ private dispatchClientToken;
1524
+ /**
1525
+ * Proxy mode dispatch (original implementation)
1526
+ */
1527
+ private dispatchProxy;
1528
+ private buildPayload;
1529
+ /**
1530
+ * Handle custom SSE event parsing via parseSSEEvent callback
1531
+ * Returns true if event was handled, false otherwise
1532
+ */
1533
+ private handleCustomSSEEvent;
1534
+ private streamResponse;
1535
+ }
1536
+
1361
1537
  type AgentWidgetSessionStatus = "idle" | "connecting" | "connected" | "error";
1362
1538
  type SessionCallbacks = {
1363
1539
  onMessagesChanged: (messages: AgentWidgetMessage[]) => void;
@@ -1402,6 +1578,34 @@ declare class AgentWidgetSession {
1402
1578
  * Clear session (on expiry or error)
1403
1579
  */
1404
1580
  clearClientSession(): void;
1581
+ /**
1582
+ * Get the underlying client instance (for advanced use cases like feedback)
1583
+ */
1584
+ getClient(): AgentWidgetClient;
1585
+ /**
1586
+ * Submit message feedback (upvote, downvote, or copy) to the API.
1587
+ * Only available in client token mode.
1588
+ *
1589
+ * @param messageId - The ID of the message to provide feedback for
1590
+ * @param type - The feedback type: 'upvote', 'downvote', or 'copy'
1591
+ */
1592
+ submitMessageFeedback(messageId: string, type: 'upvote' | 'downvote' | 'copy'): Promise<void>;
1593
+ /**
1594
+ * Submit CSAT (Customer Satisfaction) feedback to the API.
1595
+ * Only available in client token mode.
1596
+ *
1597
+ * @param rating - Rating from 1 to 5
1598
+ * @param comment - Optional comment
1599
+ */
1600
+ submitCSATFeedback(rating: number, comment?: string): Promise<void>;
1601
+ /**
1602
+ * Submit NPS (Net Promoter Score) feedback to the API.
1603
+ * Only available in client token mode.
1604
+ *
1605
+ * @param rating - Rating from 0 to 10
1606
+ * @param comment - Optional comment
1607
+ */
1608
+ submitNPSFeedback(rating: number, comment?: string): Promise<void>;
1405
1609
  updateConfig(next: AgentWidgetConfig): void;
1406
1610
  getMessages(): AgentWidgetMessage[];
1407
1611
  getStatus(): AgentWidgetSessionStatus;
@@ -1423,6 +1627,62 @@ declare class AgentWidgetSession {
1423
1627
  private sortMessages;
1424
1628
  }
1425
1629
 
1630
+ /**
1631
+ * Feedback UI components for CSAT and NPS collection
1632
+ */
1633
+ type CSATFeedbackOptions = {
1634
+ /** Callback when user submits CSAT feedback */
1635
+ onSubmit: (rating: number, comment?: string) => void | Promise<void>;
1636
+ /** Callback when user dismisses the feedback form */
1637
+ onDismiss?: () => void;
1638
+ /** Title text */
1639
+ title?: string;
1640
+ /** Subtitle/question text */
1641
+ subtitle?: string;
1642
+ /** Placeholder for optional comment field */
1643
+ commentPlaceholder?: string;
1644
+ /** Submit button text */
1645
+ submitText?: string;
1646
+ /** Skip button text */
1647
+ skipText?: string;
1648
+ /** Show comment field */
1649
+ showComment?: boolean;
1650
+ /** Rating labels (5 items for ratings 1-5) */
1651
+ ratingLabels?: [string, string, string, string, string];
1652
+ };
1653
+ type NPSFeedbackOptions = {
1654
+ /** Callback when user submits NPS feedback */
1655
+ onSubmit: (rating: number, comment?: string) => void | Promise<void>;
1656
+ /** Callback when user dismisses the feedback form */
1657
+ onDismiss?: () => void;
1658
+ /** Title text */
1659
+ title?: string;
1660
+ /** Subtitle/question text */
1661
+ subtitle?: string;
1662
+ /** Placeholder for optional comment field */
1663
+ commentPlaceholder?: string;
1664
+ /** Submit button text */
1665
+ submitText?: string;
1666
+ /** Skip button text */
1667
+ skipText?: string;
1668
+ /** Show comment field */
1669
+ showComment?: boolean;
1670
+ /** Low label (left side) */
1671
+ lowLabel?: string;
1672
+ /** High label (right side) */
1673
+ highLabel?: string;
1674
+ };
1675
+ /**
1676
+ * Create a CSAT (Customer Satisfaction) feedback form
1677
+ * Rating scale: 1-5
1678
+ */
1679
+ declare function createCSATFeedback(options: CSATFeedbackOptions): HTMLElement;
1680
+ /**
1681
+ * Create an NPS (Net Promoter Score) feedback form
1682
+ * Rating scale: 0-10
1683
+ */
1684
+ declare function createNPSFeedback(options: NPSFeedbackOptions): HTMLElement;
1685
+
1426
1686
  type Controller = {
1427
1687
  update: (config: AgentWidgetConfig) => void;
1428
1688
  destroy: () => void;
@@ -1444,6 +1704,10 @@ type Controller = {
1444
1704
  isOpen: () => boolean;
1445
1705
  isVoiceActive: () => boolean;
1446
1706
  getState: () => AgentWidgetStateSnapshot;
1707
+ showCSATFeedback: (options?: Partial<CSATFeedbackOptions>) => void;
1708
+ showNPSFeedback: (options?: Partial<NPSFeedbackOptions>) => void;
1709
+ submitCSATFeedback: (rating: number, comment?: string) => Promise<void>;
1710
+ submitNPSFeedback: (rating: number, comment?: string) => Promise<void>;
1447
1711
  };
1448
1712
  declare const createAgentExperience: (mount: HTMLElement, initialConfig?: AgentWidgetConfig, runtimeOptions?: {
1449
1713
  debugTools?: boolean;
@@ -1455,68 +1719,6 @@ type AgentWidgetInitHandle = AgentWidgetController & {
1455
1719
  };
1456
1720
  declare const initAgentWidget: (options: AgentWidgetInitOptions) => AgentWidgetInitHandle;
1457
1721
 
1458
- type DispatchOptions = {
1459
- messages: AgentWidgetMessage[];
1460
- signal?: AbortSignal;
1461
- };
1462
- type SSEHandler = (event: AgentWidgetEvent) => void;
1463
- declare class AgentWidgetClient {
1464
- private config;
1465
- private readonly apiUrl;
1466
- private readonly headers;
1467
- private readonly debug;
1468
- private readonly createStreamParser;
1469
- private readonly contextProviders;
1470
- private readonly requestMiddleware?;
1471
- private readonly customFetch?;
1472
- private readonly parseSSEEvent?;
1473
- private readonly getHeaders?;
1474
- private clientSession;
1475
- private sessionInitPromise;
1476
- constructor(config?: AgentWidgetConfig);
1477
- /**
1478
- * Check if running in client token mode
1479
- */
1480
- isClientTokenMode(): boolean;
1481
- /**
1482
- * Get the appropriate API URL based on mode
1483
- */
1484
- private getClientApiUrl;
1485
- /**
1486
- * Get the current client session (if any)
1487
- */
1488
- getClientSession(): ClientSession | null;
1489
- /**
1490
- * Initialize session for client token mode.
1491
- * Called automatically on first message if not already initialized.
1492
- */
1493
- initSession(): Promise<ClientSession>;
1494
- private _doInitSession;
1495
- /**
1496
- * Clear the current client session
1497
- */
1498
- clearClientSession(): void;
1499
- /**
1500
- * Send a message - handles both proxy and client token modes
1501
- */
1502
- dispatch(options: DispatchOptions, onEvent: SSEHandler): Promise<void>;
1503
- /**
1504
- * Client token mode dispatch
1505
- */
1506
- private dispatchClientToken;
1507
- /**
1508
- * Proxy mode dispatch (original implementation)
1509
- */
1510
- private dispatchProxy;
1511
- private buildPayload;
1512
- /**
1513
- * Handle custom SSE event parsing via parseSSEEvent callback
1514
- * Returns true if event was handled, false otherwise
1515
- */
1516
- private handleCustomSSEEvent;
1517
- private streamResponse;
1518
- }
1519
-
1520
1722
  declare const createLocalStorageAdapter: (key?: string) => AgentWidgetStorageAdapter;
1521
1723
 
1522
1724
  type ActionManagerProcessContext = {
@@ -1652,6 +1854,26 @@ declare const createFlexibleJsonStreamParser: (textExtractor?: (parsed: any) =>
1652
1854
  */
1653
1855
  declare const createXmlParser: () => AgentWidgetStreamParser;
1654
1856
 
1857
+ /**
1858
+ * Message ID utilities for client-side message tracking
1859
+ * Used for feedback integration with the Travrse API
1860
+ */
1861
+ /**
1862
+ * Generate a unique message ID for tracking
1863
+ * Format: msg_{timestamp_base36}_{random_8chars}
1864
+ */
1865
+ declare function generateMessageId(): string;
1866
+ /**
1867
+ * Generate a unique user message ID
1868
+ * Format: usr_{timestamp_base36}_{random_8chars}
1869
+ */
1870
+ declare function generateUserMessageId(): string;
1871
+ /**
1872
+ * Generate a unique assistant message ID
1873
+ * Format: ast_{timestamp_base36}_{random_8chars}
1874
+ */
1875
+ declare function generateAssistantMessageId(): string;
1876
+
1655
1877
  type CodeFormat = "esm" | "script-installer" | "script-manual" | "script-advanced" | "react-component" | "react-advanced";
1656
1878
  declare function generateCodeSnippet(config: any, format?: CodeFormat): string;
1657
1879
 
@@ -1928,4 +2150,4 @@ declare const getHeaderLayout: (layoutName: string) => HeaderLayoutRenderer;
1928
2150
  */
1929
2151
  declare const buildHeaderWithLayout: (config: AgentWidgetConfig, layoutConfig?: AgentWidgetHeaderLayoutConfig, context?: Partial<HeaderLayoutContext>) => HeaderElements;
1930
2152
 
1931
- export { type AgentWidgetAvatarConfig, AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetCustomFetch, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetHeaderLayoutConfig, type AgentWidgetHeadersFunction, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetLayoutConfig, type AgentWidgetMarkdownConfig, type AgentWidgetMarkdownOptions, type AgentWidgetMarkdownRendererOverrides, type AgentWidgetMessage, type AgentWidgetMessageActionsConfig, type AgentWidgetMessageFeedback, type AgentWidgetMessageLayoutConfig, type AgentWidgetPlugin, type AgentWidgetRequestPayload, type AgentWidgetSSEEventParser, type AgentWidgetSSEEventResult, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetStreamParser, type AgentWidgetStreamParserResult, type AgentWidgetTheme, type AgentWidgetTimestampConfig, type ClientChatRequest, type ClientInitResponse, type ClientSession, type CodeFormat, type ComponentContext, type ComponentDirective, type ComponentRenderer, type ComposerBuildContext, type ComposerElements, DEFAULT_WIDGET_CONFIG, type HeaderBuildContext, type HeaderElements, type HeaderLayoutContext, type HeaderLayoutRenderer, type HeaderRenderContext, type MarkdownProcessorOptions, type MessageActionCallbacks, type MessageRenderContext, type MessageTransform, type SlotRenderContext, type SlotRenderer, type WidgetLayoutSlot, attachHeaderToContainer, buildComposer, buildDefaultHeader, buildExpandedHeader, buildHeader, buildHeaderWithLayout, buildMinimalHeader, componentRegistry, createActionManager, createAgentExperience, createBubbleWithLayout, createComponentMiddleware, createComponentStreamParser, createDirectivePostprocessor, createFlexibleJsonStreamParser, createJsonStreamParser, createLocalStorageAdapter, createMarkdownProcessor, createMarkdownProcessorFromConfig, createMessageActions, createPlainTextParser, createRegexJsonParser, createStandardBubble, createTypingIndicator, createXmlParser, initAgentWidget as default, defaultActionHandlers, defaultJsonActionParser, directivePostprocessor, escapeHtml, extractComponentDirectiveFromMessage, generateCodeSnippet, getHeaderLayout, hasComponentDirective, headerLayouts, initAgentWidget, isComponentDirectiveType, markdownPostprocessor, mergeWithDefaults, pluginRegistry, renderComponentDirective };
2153
+ export { type AgentWidgetAvatarConfig, AgentWidgetClient, type AgentWidgetConfig, type AgentWidgetController, type AgentWidgetCustomFetch, type AgentWidgetEvent, type AgentWidgetFeatureFlags, type AgentWidgetHeaderLayoutConfig, type AgentWidgetHeadersFunction, type AgentWidgetInitHandle, type AgentWidgetInitOptions, type AgentWidgetLauncherConfig, type AgentWidgetLayoutConfig, type AgentWidgetMarkdownConfig, type AgentWidgetMarkdownOptions, type AgentWidgetMarkdownRendererOverrides, type AgentWidgetMessage, type AgentWidgetMessageActionsConfig, type AgentWidgetMessageFeedback, type AgentWidgetMessageLayoutConfig, type AgentWidgetPlugin, type AgentWidgetRequestPayload, type AgentWidgetSSEEventParser, type AgentWidgetSSEEventResult, AgentWidgetSession, type AgentWidgetSessionStatus, type AgentWidgetStreamParser, type AgentWidgetStreamParserResult, type AgentWidgetTheme, type AgentWidgetTimestampConfig, type CSATFeedbackOptions, type ClientChatRequest, type ClientFeedbackRequest, type ClientFeedbackType, type ClientInitResponse, type ClientSession, type CodeFormat, type ComponentContext, type ComponentDirective, type ComponentRenderer, type ComposerBuildContext, type ComposerElements, DEFAULT_WIDGET_CONFIG, type HeaderBuildContext, type HeaderElements, type HeaderLayoutContext, type HeaderLayoutRenderer, type HeaderRenderContext, type MarkdownProcessorOptions, type MessageActionCallbacks, type MessageRenderContext, type MessageTransform, type NPSFeedbackOptions, type SlotRenderContext, type SlotRenderer, type WidgetLayoutSlot, attachHeaderToContainer, buildComposer, buildDefaultHeader, buildExpandedHeader, buildHeader, buildHeaderWithLayout, buildMinimalHeader, componentRegistry, createActionManager, createAgentExperience, createBubbleWithLayout, createCSATFeedback, createComponentMiddleware, createComponentStreamParser, createDirectivePostprocessor, createFlexibleJsonStreamParser, createJsonStreamParser, createLocalStorageAdapter, createMarkdownProcessor, createMarkdownProcessorFromConfig, createMessageActions, createNPSFeedback, createPlainTextParser, createRegexJsonParser, createStandardBubble, createTypingIndicator, createXmlParser, initAgentWidget as default, defaultActionHandlers, defaultJsonActionParser, directivePostprocessor, escapeHtml, extractComponentDirectiveFromMessage, generateAssistantMessageId, generateCodeSnippet, generateMessageId, generateUserMessageId, getHeaderLayout, hasComponentDirective, headerLayouts, initAgentWidget, isComponentDirectiveType, markdownPostprocessor, mergeWithDefaults, pluginRegistry, renderComponentDirective };