@helpai/elements 1.8.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.
package/index.d.ts CHANGED
@@ -93,8 +93,8 @@ type Position = 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
93
93
  type Mode = 'floating' | 'inline' | 'standalone' | 'page' | 'modal' | 'drawer';
94
94
  /** End-user text-size preference — scales the whole type ramp. */
95
95
  type TextSize = 'small' | 'normal' | 'large';
96
- /** Theme preference. `auto` follows `prefers-color-scheme`. */
97
- type ThemePreference = 'auto' | 'light' | 'dark';
96
+ /** Theme preference. `auto` follows `prefers-color-scheme`; `host` follows the page's own light/dark. */
97
+ type ThemePreference = 'auto' | 'light' | 'dark' | 'host';
98
98
  /**
99
99
  * How the assistant's reply is delivered.
100
100
  *
@@ -591,6 +591,8 @@ interface ResolvedModules {
591
591
  }
592
592
  /** CSS-custom-property overrides applied as inline styles on the host. */
593
593
  interface ThemeOverrides {
594
+ /** Light/dark beside the overrides — `host` keeps a deployment's accent while the page sets the mode. */
595
+ mode?: ThemePreference;
594
596
  accent?: string;
595
597
  accentText?: string;
596
598
  radius?: string;
@@ -896,6 +898,8 @@ interface ResolvedTracking {
896
898
  * alongside at the top level — they're function / DOM types that can't be
897
899
  * serialised on the wire.
898
900
  */
901
+ /** A signed user token, or a function that returns a fresh one (see {@link ChatWidgetOptions.userToken}). */
902
+ type UserTokenSource = string | (() => string | Promise<string>);
899
903
  interface ChatWidgetOptions {
900
904
  /**
901
905
  * Stable identifier for this widget instance. Use when mounting more
@@ -971,6 +975,14 @@ interface ChatWidgetOptions {
971
975
  * interface (`get` / `set` / `remove` / `clear`).
972
976
  */
973
977
  storage?: ClientStorage;
978
+ /**
979
+ * Proves who the logged-in visitor is: a JWT your server signs (HS256) with the deployment's secret key, carrying
980
+ * `sub` (your user id) and `exp`, optionally `name` and `email`. A function is called when a token is first
981
+ * needed and again when the server rejects the current one as expired, so return a fresh token from it. Their
982
+ * conversations then follow them to any device; `chat.login(token)` sets it after sign-in, `chat.reset()` on
983
+ * sign-out. Sent only to the agent API, as `x-user-token`.
984
+ */
985
+ userToken?: UserTokenSource;
974
986
  onMessage?: (msg: {
975
987
  role: string;
976
988
  text: string;
@@ -1081,6 +1093,7 @@ interface ResolvedOptions {
1081
1093
  tracking: ResolvedTracking;
1082
1094
  /** Resolved storage adapter — defaults to `LocalStorageAdapter`. */
1083
1095
  storage: ClientStorage;
1096
+ userToken?: UserTokenSource;
1084
1097
  onMessage?: ChatWidgetOptions['onMessage'];
1085
1098
  onOpen?: ChatWidgetOptions['onOpen'];
1086
1099
  onClose?: ChatWidgetOptions['onClose'];
@@ -1211,8 +1224,15 @@ declare const widgetSettingsSchema: z.ZodObject<{
1211
1224
  theme: z.ZodDefault<z.ZodUnion<readonly [z.ZodEnum<{
1212
1225
  auto: "auto";
1213
1226
  dark: "dark";
1227
+ host: "host";
1214
1228
  light: "light";
1215
1229
  }>, z.ZodObject<{
1230
+ mode: z.ZodOptional<z.ZodEnum<{
1231
+ auto: "auto";
1232
+ dark: "dark";
1233
+ host: "host";
1234
+ light: "light";
1235
+ }>>;
1216
1236
  accent: z.ZodOptional<z.ZodString>;
1217
1237
  accentText: z.ZodOptional<z.ZodString>;
1218
1238
  radius: z.ZodOptional<z.ZodString>;
@@ -1546,8 +1566,15 @@ declare const widgetConfigSchema: z.ZodObject<{
1546
1566
  theme: z.ZodDefault<z.ZodUnion<readonly [z.ZodEnum<{
1547
1567
  auto: "auto";
1548
1568
  dark: "dark";
1569
+ host: "host";
1549
1570
  light: "light";
1550
1571
  }>, z.ZodObject<{
1572
+ mode: z.ZodOptional<z.ZodEnum<{
1573
+ auto: "auto";
1574
+ dark: "dark";
1575
+ host: "host";
1576
+ light: "light";
1577
+ }>>;
1551
1578
  accent: z.ZodOptional<z.ZodString>;
1552
1579
  accentText: z.ZodOptional<z.ZodString>;
1553
1580
  radius: z.ZodOptional<z.ZodString>;
@@ -1882,8 +1909,15 @@ declare const widgetConfigPartialSchema: z.ZodObject<{
1882
1909
  theme: z.ZodOptional<z.ZodDefault<z.ZodUnion<readonly [z.ZodEnum<{
1883
1910
  auto: "auto";
1884
1911
  dark: "dark";
1912
+ host: "host";
1885
1913
  light: "light";
1886
1914
  }>, z.ZodObject<{
1915
+ mode: z.ZodOptional<z.ZodEnum<{
1916
+ auto: "auto";
1917
+ dark: "dark";
1918
+ host: "host";
1919
+ light: "light";
1920
+ }>>;
1887
1921
  accent: z.ZodOptional<z.ZodString>;
1888
1922
  accentText: z.ZodOptional<z.ZodString>;
1889
1923
  radius: z.ZodOptional<z.ZodString>;
@@ -2180,8 +2214,15 @@ declare const widgetSettingsPartialSchema: z.ZodObject<{
2180
2214
  theme: z.ZodOptional<z.ZodDefault<z.ZodUnion<readonly [z.ZodEnum<{
2181
2215
  auto: "auto";
2182
2216
  dark: "dark";
2217
+ host: "host";
2183
2218
  light: "light";
2184
2219
  }>, z.ZodObject<{
2220
+ mode: z.ZodOptional<z.ZodEnum<{
2221
+ auto: "auto";
2222
+ dark: "dark";
2223
+ host: "host";
2224
+ light: "light";
2225
+ }>>;
2185
2226
  accent: z.ZodOptional<z.ZodString>;
2186
2227
  accentText: z.ZodOptional<z.ZodString>;
2187
2228
  radius: z.ZodOptional<z.ZodString>;
@@ -2532,8 +2573,15 @@ declare const serverConfigSchema: z.ZodObject<{
2532
2573
  theme: z.ZodOptional<z.ZodDefault<z.ZodUnion<readonly [z.ZodEnum<{
2533
2574
  auto: "auto";
2534
2575
  dark: "dark";
2576
+ host: "host";
2535
2577
  light: "light";
2536
2578
  }>, z.ZodObject<{
2579
+ mode: z.ZodOptional<z.ZodEnum<{
2580
+ auto: "auto";
2581
+ dark: "dark";
2582
+ host: "host";
2583
+ light: "light";
2584
+ }>>;
2537
2585
  accent: z.ZodOptional<z.ZodString>;
2538
2586
  accentText: z.ZodOptional<z.ZodString>;
2539
2587
  radius: z.ZodOptional<z.ZodString>;
@@ -2820,8 +2868,15 @@ declare const handshakeResponseSchema: z.ZodObject<{
2820
2868
  theme: z.ZodOptional<z.ZodDefault<z.ZodUnion<readonly [z.ZodEnum<{
2821
2869
  auto: "auto";
2822
2870
  dark: "dark";
2871
+ host: "host";
2823
2872
  light: "light";
2824
2873
  }>, z.ZodObject<{
2874
+ mode: z.ZodOptional<z.ZodEnum<{
2875
+ auto: "auto";
2876
+ dark: "dark";
2877
+ host: "host";
2878
+ light: "light";
2879
+ }>>;
2825
2880
  accent: z.ZodOptional<z.ZodString>;
2826
2881
  accentText: z.ZodOptional<z.ZodString>;
2827
2882
  radius: z.ZodOptional<z.ZodString>;
@@ -3174,6 +3229,24 @@ interface EventMap {
3174
3229
  previous: string;
3175
3230
  current: string;
3176
3231
  };
3232
+ /** The widget's state changed — open, ready, unread or the conversation. Payload is the whole new state. */
3233
+ state: WidgetState;
3234
+ /** How many conversations have replies the visitor hasn't read changed. Payload is the new count. */
3235
+ unread: number;
3236
+ }
3237
+ /** What a page can read about the widget at any moment (`chat.state`), and the `state` event's payload. */
3238
+ interface WidgetState {
3239
+ /** The panel is showing. Always `true` for `inline`, `page` and `standalone`. */
3240
+ open: boolean;
3241
+ /** The widget has connected (its first handshake settled) and takes commands right away. */
3242
+ ready: boolean;
3243
+ /**
3244
+ * Conversations with replies the visitor hasn't read. Known once the widget has been opened in this page load
3245
+ * (a closed widget makes no requests beyond the handshake), and kept live from then on.
3246
+ */
3247
+ unread: number;
3248
+ /** The visitor's current conversation, once there is one. */
3249
+ conversationId?: string;
3177
3250
  }
3178
3251
  type EventName = keyof EventMap;
3179
3252
  /**
@@ -3302,6 +3375,37 @@ export interface ChatWidgetInstance {
3302
3375
  * ```
3303
3376
  */
3304
3377
  ask: (text: string, options?: AskOptions) => void;
3378
+ /**
3379
+ * The widget's state right now: `open`, `ready`, `unread` (conversations with unread replies) and the current
3380
+ * `conversationId`. The `state` event delivers each change; `unread` fires when the count changes.
3381
+ *
3382
+ * @example
3383
+ * ```ts
3384
+ * helpLink.dataset.unread = String(chat.state.unread);
3385
+ * chat.on('unread', (count) => (helpLink.dataset.unread = String(count)));
3386
+ * ```
3387
+ */
3388
+ readonly state: WidgetState;
3389
+ /** Resolves once the widget has connected; commands sent after it act at once. Resolves once, never rejects. */
3390
+ readonly ready: Promise<void>;
3391
+ /**
3392
+ * Start over as a new anonymous visitor — call it when your user logs out. Forgets the stored visitor, their current
3393
+ * conversation, preferences and the user context set with `setUserContext`, then reconnects as a first visit.
3394
+ * Listeners added with `on()` stay.
3395
+ */
3396
+ reset: () => void;
3397
+ /**
3398
+ * Sign the visitor in as your logged-in user, after they log in to your site: `userToken` is a JWT your server
3399
+ * signs with the deployment's secret key (`sub`, `exp`; optional `name`, `email`), or a function that returns a
3400
+ * fresh one. The widget reconnects as that user: their earlier chats on this browser move to them, and their
3401
+ * conversations follow them to any device. Call `reset()` when they log out.
3402
+ *
3403
+ * @example
3404
+ * ```ts
3405
+ * chat.login(async () => (await fetch('/api/help-ai-token')).text());
3406
+ * ```
3407
+ */
3408
+ login: (userToken: UserTokenSource) => void;
3305
3409
  /**
3306
3410
  * Subscribe to a lifecycle event. Returns an `off` function. See
3307
3411
  * {@link EventMap} for the full set + payload shapes — currently
@@ -3445,4 +3549,4 @@ export declare const brand: {
3445
3549
  readonly domain: string;
3446
3550
  readonly siteUrl: string;
3447
3551
  };
3448
- export type { AskOptions, Asset, BlocksConfig, ChatWidgetOptions, ConnectionConfig, ConnectionConfigPartial, EventMap, EventName, HandshakeResponse, HookListener, Link, LogEntry, LogLevel, OpenTarget, ResolvedOptions, ServerConfig, SiteConfig, WidgetConfig, WidgetConfigPartial, WidgetSettings, WidgetSettingsPartial };
3552
+ export type { AskOptions, Asset, BlocksConfig, ChatWidgetOptions, ConnectionConfig, ConnectionConfigPartial, EventMap, EventName, HandshakeResponse, HookListener, Link, LogEntry, LogLevel, OpenTarget, ResolvedOptions, ServerConfig, SiteConfig, UserTokenSource, WidgetConfig, WidgetConfigPartial, WidgetSettings, WidgetSettingsPartial, WidgetState };