@smooai/chat-widget 0.4.0 → 0.5.1

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.ts CHANGED
@@ -190,6 +190,45 @@ declare function mountChatWidget(config: ChatWidgetConfig, target?: HTMLElement)
190
190
  */
191
191
  declare function mountFullPageChat(config: Omit<ChatWidgetConfig, 'mode'>, target?: HTMLElement): SmoothAgentChatElement;
192
192
  //#endregion
193
+ //#region src/loader-core.d.ts
194
+ /**
195
+ * Tiny, dependency-free deferred loader for `@smooai/chat-widget`.
196
+ *
197
+ * The recommended embed: a host includes this *small* script eagerly, and it
198
+ * defers injecting the real (heavier) `chat-widget.global.js` module until the
199
+ * page is past its critical render — so the widget never competes with the host
200
+ * page's LCP/TBT. It triggers the real load on the **first** of:
201
+ *
202
+ * - `window` `load` → `requestIdleCallback` (idle time), or
203
+ * - user intent (`pointerdown` / `keydown` / `scroll`), or
204
+ * - an 8s fallback.
205
+ *
206
+ * After the module loads (which registers `<smooth-agent-chat>` and exposes
207
+ * `window.SmoothAgentChat`), it mounts the widget with the host's config.
208
+ *
209
+ * Deliberately imports nothing from the widget itself — this file must stay a
210
+ * few hundred bytes so including it eagerly is free.
211
+ *
212
+ * ## Config
213
+ * Set a global before/with the loader (any `ChatWidgetConfig`, plus an optional
214
+ * `src` pointing at the module bundle):
215
+ *
216
+ * ```html
217
+ * <script>window.SmoothAgentChatConfig = { endpoint: 'wss://…/ws', agentId: '…' };</script>
218
+ * <script src="https://cdn/…/chat-widget-loader.global.js" async></script>
219
+ * ```
220
+ *
221
+ * Or, for the simplest installs, `data-*` attributes on the loader's own tag
222
+ * (`data-endpoint`, `data-agent-id`, `data-primary`, `data-mode`, `data-src`).
223
+ * If no config is found, the module is still loaded (registering the element) so
224
+ * a `<smooth-agent-chat …>` placed directly in markup upgrades itself.
225
+ */
226
+ /**
227
+ * Install the deferred loader. Idempotent per call; the IIFE entry
228
+ * (`loader.ts`) invokes it once on script execution.
229
+ */
230
+ declare function initChatWidgetLoader(): void;
231
+ //#endregion
193
232
  //#region src/conversation.d.ts
194
233
  type Role = 'user' | 'assistant';
195
234
  interface ChatMessage {
@@ -283,5 +322,5 @@ declare class ConversationController {
283
322
  disconnect(): void;
284
323
  }
285
324
  //#endregion
286
- export { type ChatMessage, type ChatWidgetConfig, type ChatWidgetMode, type ChatWidgetTheme, type Citation, type ConnectionStatus, ConversationController, type ConversationEvents, ELEMENT_TAG, type Role, SmoothAgentChatElement, defineChatWidget, mountChatWidget, mountFullPageChat };
325
+ export { type ChatMessage, type ChatWidgetConfig, type ChatWidgetMode, type ChatWidgetTheme, type Citation, type ConnectionStatus, ConversationController, type ConversationEvents, ELEMENT_TAG, type Role, SmoothAgentChatElement, defineChatWidget, initChatWidgetLoader, mountChatWidget, mountFullPageChat };
287
326
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/config.ts","../src/element.ts","../src/conversation.ts"],"mappings":";;;;;;AAOA;;;;UAAiB,eAAA;EAIb;EAFA,IAAA;EAMA;EAJA,UAAA;EAQA;EANA,OAAA;EAUA;EARA,WAAA;EAYA;EAVA,SAAA;EAkBA;EAhBA,eAAA;EAoBA;EAlBA,mBAAA;EAkBsB;EAhBtB,UAAA;EA2BsB;EAzBtB,cAAA;EAyBsB;EAvBtB,MAAA;EAyBa;EAnBb,iBAAA;;EAEA,qBAAA;EAsBA;EApBA,kBAAA;EAyBO;EAvBP,sBAAA;AAAA;;;;;;;;;KAWQ,cAAA;AAAA,UAEK,gBAAA;EAuCb;;;;EAlCA,QAAA;EAyCuB;;;;EApCvB,IAAA,GAAO,cAAA;ECxCa;ED0CpB,OAAA;EC1CoB;ED4CpB,SAAA;ECAS;EDET,QAAA;;EAEA,SAAA;ECmDkB;EDjDlB,SAAA;ECNmD;EDQnD,WAAA;ECRwC;EDUxC,QAAA;ECLiB;EDOjB,sBAAA;ECLQ;EDOR,SAAA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AA;EAE3B,KAAA;EACA,iBAAA;AAAA;EAEF,IAAA;EAAiB,MAAA;EAAiB,iBAAA;AAAA;AAAA,UAEzB,QAAA;EACb,IAAA;EACA,KAAA;EACA,KAAA;AAAA;AAAA,UAGa,kBAAA;EFJ2B;EEMxC,UAAA,GAAa,QAAA,EAAU,WAAA;EFDN;EEGjB,QAAA,GAAW,MAAA,EAAQ,gBAAA,EAAkB,MAAA;EFD7B;EEGR,WAAA,IAAe,SAAA,EAAW,SAAA;AAAA;AAAA,cAyCjB,sBAAA;EAAA,iBACQ,MAAA;EAAA,iBACA,MAAA;EAAA,QACT,MAAA;EAAA,QACA,SAAA;EAAA,iBACS,QAAA;EAAA,QACT,MAAA;EAAA,QACA,GAAA;EFlCA;EAAA,QEoCA,QAAA;EFlCA;EAAA,QEoCA,eAAA;EAAA,QACA,SAAA;cAEI,MAAA,EAAQ,gBAAA,EAAkB,MAAA,EAAQ,kBAAA;EAAA,IAM1C,gBAAA,IAAoB,gBAAA;EFnCxB;EEwCA,WAAA,CAAY,IAAA,EAAM,QAAA;EAAA,QAIV,YAAA;EFzBR;EE+BA,SAAA,CAAU,IAAA;EF/BgB;EEqC1B,WAAA,CAAY,QAAA;EAAA,QAMJ,MAAA;EAAA,QAKA,SAAA;EAAA,QAKA,YAAA;EFCA;EEKF,OAAA,IAAW,OAAA;EF2OT;;;;EElNF,IAAA,CAAK,IAAA,WAAe,OAAA;EFsSlB;EAAA,QElOA,eAAA;EFsPA;EE/MR,UAAA;AAAA"}
package/dist/index.js CHANGED
@@ -1452,6 +1452,142 @@ function mountFullPageChat(config, target = document.body) {
1452
1452
  }, target);
1453
1453
  }
1454
1454
  //#endregion
1455
- export { ConversationController, ELEMENT_TAG, SmoothAgentChatElement, defineChatWidget, mountChatWidget, mountFullPageChat };
1455
+ //#region src/loader-core.ts
1456
+ /**
1457
+ * Tiny, dependency-free deferred loader for `@smooai/chat-widget`.
1458
+ *
1459
+ * The recommended embed: a host includes this *small* script eagerly, and it
1460
+ * defers injecting the real (heavier) `chat-widget.global.js` module until the
1461
+ * page is past its critical render — so the widget never competes with the host
1462
+ * page's LCP/TBT. It triggers the real load on the **first** of:
1463
+ *
1464
+ * - `window` `load` → `requestIdleCallback` (idle time), or
1465
+ * - user intent (`pointerdown` / `keydown` / `scroll`), or
1466
+ * - an 8s fallback.
1467
+ *
1468
+ * After the module loads (which registers `<smooth-agent-chat>` and exposes
1469
+ * `window.SmoothAgentChat`), it mounts the widget with the host's config.
1470
+ *
1471
+ * Deliberately imports nothing from the widget itself — this file must stay a
1472
+ * few hundred bytes so including it eagerly is free.
1473
+ *
1474
+ * ## Config
1475
+ * Set a global before/with the loader (any `ChatWidgetConfig`, plus an optional
1476
+ * `src` pointing at the module bundle):
1477
+ *
1478
+ * ```html
1479
+ * <script>window.SmoothAgentChatConfig = { endpoint: 'wss://…/ws', agentId: '…' };<\/script>
1480
+ * <script src="https://cdn/…/chat-widget-loader.global.js" async><\/script>
1481
+ * ```
1482
+ *
1483
+ * Or, for the simplest installs, `data-*` attributes on the loader's own tag
1484
+ * (`data-endpoint`, `data-agent-id`, `data-primary`, `data-mode`, `data-src`).
1485
+ * If no config is found, the module is still loaded (registering the element) so
1486
+ * a `<smooth-agent-chat …>` placed directly in markup upgrades itself.
1487
+ */
1488
+ /** Must match `ELEMENT_TAG` in `element.ts`; inlined to avoid importing it. */
1489
+ const ELEMENT_TAG$1 = "smooth-agent-chat";
1490
+ /** Default module filename, resolved as a sibling of the loader script. */
1491
+ const DEFAULT_MODULE = "chat-widget.global.js";
1492
+ /**
1493
+ * Resolve the module URL: explicit `src` (config or `data-src`) wins, else a
1494
+ * sibling of the loader script, else the bare default filename.
1495
+ */
1496
+ function resolveModuleSrc(win, script) {
1497
+ const fromConfig = win.SmoothAgentChatConfig?.src;
1498
+ if (fromConfig) return fromConfig;
1499
+ const fromAttr = script?.getAttribute("data-src");
1500
+ if (fromAttr) return fromAttr;
1501
+ const selfSrc = script?.src;
1502
+ if (selfSrc) return selfSrc.replace(/[^/]*$/, DEFAULT_MODULE);
1503
+ return DEFAULT_MODULE;
1504
+ }
1505
+ /**
1506
+ * The widget mount config: the `SmoothAgentChatConfig` global (minus the
1507
+ * loader-only `src`), or `data-*` attributes. `undefined` ⇒ no programmatic
1508
+ * mount (a markup element will upgrade itself once the module registers it).
1509
+ */
1510
+ function resolveMountConfig(win, script) {
1511
+ if (win.SmoothAgentChatConfig) {
1512
+ const { src: _src, ...config } = win.SmoothAgentChatConfig;
1513
+ return Object.keys(config).length > 0 ? config : void 0;
1514
+ }
1515
+ if (!script) return void 0;
1516
+ const endpoint = script.getAttribute("data-endpoint");
1517
+ const agentId = script.getAttribute("data-agent-id");
1518
+ if (!endpoint && !agentId) return void 0;
1519
+ const config = {};
1520
+ if (endpoint) config.endpoint = endpoint;
1521
+ if (agentId) config.agentId = agentId;
1522
+ const primary = script.getAttribute("data-primary");
1523
+ if (primary) config.theme = { primary };
1524
+ const mode = script.getAttribute("data-mode");
1525
+ if (mode) config.mode = mode;
1526
+ return config;
1527
+ }
1528
+ /**
1529
+ * Install the deferred loader. Idempotent per call; the IIFE entry
1530
+ * (`loader.ts`) invokes it once on script execution.
1531
+ */
1532
+ function initChatWidgetLoader() {
1533
+ const win = window;
1534
+ const script = document.currentScript ?? null;
1535
+ const moduleSrc = resolveModuleSrc(win, script);
1536
+ let loaded = false;
1537
+ let idleId;
1538
+ let fallbackId;
1539
+ function mountWhenReady() {
1540
+ const config = resolveMountConfig(win, script);
1541
+ if (!config) return;
1542
+ const apply = () => {
1543
+ try {
1544
+ win.SmoothAgentChat?.mount(config);
1545
+ } catch (error) {
1546
+ console.error("[chat-widget loader] mount failed", error);
1547
+ }
1548
+ };
1549
+ if (win.SmoothAgentChat) apply();
1550
+ else if (window.customElements?.whenDefined) window.customElements.whenDefined(ELEMENT_TAG$1).then(apply, apply);
1551
+ else apply();
1552
+ }
1553
+ function teardown() {
1554
+ if (idleId !== void 0) {
1555
+ window.cancelIdleCallback?.(idleId);
1556
+ window.clearTimeout(idleId);
1557
+ }
1558
+ if (fallbackId !== void 0) window.clearTimeout(fallbackId);
1559
+ window.removeEventListener("pointerdown", load);
1560
+ window.removeEventListener("keydown", load);
1561
+ window.removeEventListener("scroll", load);
1562
+ }
1563
+ function load() {
1564
+ if (loaded) return;
1565
+ loaded = true;
1566
+ teardown();
1567
+ const tag = document.createElement("script");
1568
+ tag.src = moduleSrc;
1569
+ tag.onload = mountWhenReady;
1570
+ tag.onerror = () => console.error("[chat-widget loader] failed to load", moduleSrc);
1571
+ document.head.appendChild(tag);
1572
+ }
1573
+ function schedule() {
1574
+ if (window.requestIdleCallback) idleId = window.requestIdleCallback(() => load(), { timeout: 5e3 });
1575
+ else idleId = window.setTimeout(load, 2500);
1576
+ fallbackId = window.setTimeout(load, 8e3);
1577
+ }
1578
+ window.addEventListener("pointerdown", load, {
1579
+ once: true,
1580
+ passive: true
1581
+ });
1582
+ window.addEventListener("keydown", load, { once: true });
1583
+ window.addEventListener("scroll", load, {
1584
+ once: true,
1585
+ passive: true
1586
+ });
1587
+ if (document.readyState === "complete") schedule();
1588
+ else window.addEventListener("load", schedule, { once: true });
1589
+ }
1590
+ //#endregion
1591
+ export { ConversationController, ELEMENT_TAG, SmoothAgentChatElement, defineChatWidget, initChatWidgetLoader, mountChatWidget, mountFullPageChat };
1456
1592
 
1457
1593
  //# sourceMappingURL=index.js.map