@helpai/elements 1.9.0 → 1.10.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.
@@ -818,6 +818,8 @@ interface TrackingOptions {
818
818
  * alongside at the top level — they're function / DOM types that can't be
819
819
  * serialised on the wire.
820
820
  */
821
+ /** A signed user token, or a function that returns a fresh one (see {@link ChatWidgetOptions.userToken}). */
822
+ type UserTokenSource = string | (() => string | Promise<string>);
821
823
  interface ChatWidgetOptions {
822
824
  /**
823
825
  * Stable identifier for this widget instance. Use when mounting more
@@ -893,6 +895,14 @@ interface ChatWidgetOptions {
893
895
  * interface (`get` / `set` / `remove` / `clear`).
894
896
  */
895
897
  storage?: ClientStorage;
898
+ /**
899
+ * Proves who the logged-in visitor is: a JWT your server signs (HS256) with the deployment's secret key, carrying
900
+ * `sub` (your user id) and `exp`, optionally `name` and `email`. A function is called when a token is first
901
+ * needed and again when the server rejects the current one as expired, so return a fresh token from it. Their
902
+ * conversations then follow them to any device; `chat.login(token)` sets it after sign-in, `chat.reset()` on
903
+ * sign-out. Sent only to the agent API, as `x-user-token`.
904
+ */
905
+ userToken?: UserTokenSource;
896
906
  onMessage?: (msg: {
897
907
  role: string;
898
908
  text: string;
@@ -1489,6 +1499,18 @@ interface ChatWidgetInstance {
1489
1499
  * Listeners added with `on()` stay.
1490
1500
  */
1491
1501
  reset: () => void;
1502
+ /**
1503
+ * Sign the visitor in as your logged-in user, after they log in to your site: `userToken` is a JWT your server
1504
+ * signs with the deployment's secret key (`sub`, `exp`; optional `name`, `email`), or a function that returns a
1505
+ * fresh one. The widget reconnects as that user: their earlier chats on this browser move to them, and their
1506
+ * conversations follow them to any device. Call `reset()` when they log out.
1507
+ *
1508
+ * @example
1509
+ * ```ts
1510
+ * chat.login(async () => (await fetch('/api/help-ai-token')).text());
1511
+ * ```
1512
+ */
1513
+ login: (userToken: UserTokenSource) => void;
1492
1514
  /**
1493
1515
  * Subscribe to a lifecycle event. Returns an `off` function. See
1494
1516
  * {@link EventMap} for the full set + payload shapes — currently
@@ -1557,7 +1579,7 @@ export type WebAiChatAttributes = Partial<Record<WebAiChatAttributeName, string>
1557
1579
  'ai-agent-deployment-id': string;
1558
1580
  };
1559
1581
  /** A page's `<web-ai-chat>`: the same commands, state and events as the `mount()` handle. */
1560
- export type WebAiChatElement = HTMLElement & Pick<ChatWidgetInstance, 'open' | 'close' | 'expand' | 'openForm' | 'ask' | 'on' | 'state' | 'ready' | 'reset'>;
1582
+ export type WebAiChatElement = HTMLElement & Pick<ChatWidgetInstance, 'open' | 'close' | 'expand' | 'openForm' | 'ask' | 'on' | 'state' | 'ready' | 'reset' | 'login'>;
1561
1583
  declare global {
1562
1584
  interface HTMLElementTagNameMap {
1563
1585
  'web-ai-chat': WebAiChatElement;