@matterfact/embed 0.10.0 → 0.11.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.
Files changed (41) hide show
  1. package/README.md +9 -6
  2. package/dist/{chunk-CFDPCZO3.js → chunk-AANODHBV.js} +11 -2
  3. package/dist/chunk-AANODHBV.js.map +1 -0
  4. package/dist/{chunk-4AE5WINC.js → chunk-CTZEDOH7.js} +12 -3
  5. package/dist/chunk-CTZEDOH7.js.map +1 -0
  6. package/dist/{chunk-DXLHTFB4.js → chunk-JO3GWFMJ.js} +2 -2
  7. package/dist/{chunk-RL6VIGWK.js → chunk-SKJFF7RD.js} +2 -2
  8. package/dist/chunk-Y7I25VHL.js +3 -0
  9. package/dist/chunk-Y7I25VHL.js.map +7 -0
  10. package/dist/{context-SSQ4HUP3.js → context-FR7VFENN.js} +3 -3
  11. package/dist/{context-SSQ4HUP3.js.map → context-FR7VFENN.js.map} +1 -1
  12. package/dist/{context-HOOW63MO.js → context-IK5MECUW.js} +2 -2
  13. package/dist/embed.js +1 -1
  14. package/dist/embed.js.map +3 -3
  15. package/dist/index.cjs +33 -0
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +56 -1
  18. package/dist/index.d.ts +56 -1
  19. package/dist/index.js +25 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/react.cjs +47 -5
  22. package/dist/react.cjs.map +1 -1
  23. package/dist/react.d.cts +51 -2
  24. package/dist/react.d.ts +51 -2
  25. package/dist/react.js +39 -6
  26. package/dist/react.js.map +1 -1
  27. package/dist/{snapshot-EYRXPLRC.js → snapshot-2V5SDSH2.js} +2 -2
  28. package/dist/{snapshot-CRY2IBX6.js → snapshot-Y5BF2UJR.js} +3 -3
  29. package/dist/{snapshot-CRY2IBX6.js.map → snapshot-Y5BF2UJR.js.map} +1 -1
  30. package/examples/embed-demo/README.md +22 -0
  31. package/examples/embed-demo/src/App.tsx +105 -2
  32. package/examples/embed-demo/src/styles.css +29 -0
  33. package/package.json +1 -1
  34. package/dist/chunk-4AE5WINC.js.map +0 -1
  35. package/dist/chunk-CFDPCZO3.js.map +0 -1
  36. package/dist/chunk-TWMHQF7O.js +0 -3
  37. package/dist/chunk-TWMHQF7O.js.map +0 -7
  38. /package/dist/{chunk-DXLHTFB4.js.map → chunk-JO3GWFMJ.js.map} +0 -0
  39. /package/dist/{chunk-RL6VIGWK.js.map → chunk-SKJFF7RD.js.map} +0 -0
  40. /package/dist/{context-HOOW63MO.js.map → context-IK5MECUW.js.map} +0 -0
  41. /package/dist/{snapshot-EYRXPLRC.js.map → snapshot-2V5SDSH2.js.map} +0 -0
package/dist/react.d.cts CHANGED
@@ -93,6 +93,44 @@ interface ToolEvent {
93
93
  error?: string;
94
94
  ms?: number;
95
95
  }
96
+ /**
97
+ * One embed lifecycle event, surfaced to the host via `onEvent` — the host's single
98
+ * telemetry hook. Fired loader-side (lifecycle) and widget-side (chat); a throwing or
99
+ * slow callback NEVER blocks or breaks the mount/call/auth/run path.
100
+ *
101
+ * - `ready` — the widget connected (protocol handshake done).
102
+ * - `open`/`close` — the corner widget was opened/closed. (Inline has no open event —
103
+ * the host's own panel is the open/close control.)
104
+ * - `navigate` — the agent deeplinked the host to one of its own routes.
105
+ * - `auth` — host-auth passthrough: `required` (widget asked), `granted` (a
106
+ * token was handed over), `failed` (no provider result / the provider threw).
107
+ * - `error` — the widget failed to mount / load.
108
+ * - `chat` — a chat-turn moment. Content-free: phase + opaque chatId only.
109
+ * - `tool` — a host-tool lifecycle event (the `ToolEvent` shape, tagged). Also
110
+ * still delivered untagged to the legacy `onToolEvent` hook for back-compat.
111
+ */
112
+ type MatterfactEvent = {
113
+ type: 'ready';
114
+ } | {
115
+ type: 'open';
116
+ } | {
117
+ type: 'close';
118
+ } | {
119
+ type: 'navigate';
120
+ href: string;
121
+ } | {
122
+ type: 'auth';
123
+ phase: 'required' | 'granted' | 'failed';
124
+ } | {
125
+ type: 'error';
126
+ message: string;
127
+ } | {
128
+ type: 'chat';
129
+ phase: 'message' | 'response-start' | 'response-end';
130
+ chatId?: string;
131
+ } | ({
132
+ type: 'tool';
133
+ } & ToolEvent);
96
134
 
97
135
  /**
98
136
  * The Hoist adapter — a pure transform from a `HoistRuntime` (already read out of
@@ -277,10 +315,21 @@ interface MatterfactAgentProps {
277
315
  * Host telemetry hook: fired on every host-tool advertise/call/result so you can pipe
278
316
  * calls into your own observability. Never blocks or breaks the call path — a throw here
279
317
  * is swallowed. Sugar over `window.matterfact.onToolEvent`.
318
+ *
319
+ * NOTE: this is now a SUBSET of `onEvent` (the tool events only), kept for back-compat.
320
+ * Prefer `onEvent` for new integrations — it delivers these same tool events (tagged
321
+ * `type:'tool'`) alongside the widget's lifecycle and chat events.
280
322
  */
281
323
  onToolEvent?: (e: ToolEvent) => void;
324
+ /**
325
+ * UNIFIED host telemetry hook: fired on every embed lifecycle moment — `ready`,
326
+ * `open`/`close`, `navigate`, `auth`, `error`, `chat`, and `tool` — so you can pipe the
327
+ * whole widget into your own observability from one place. Never blocks or breaks the
328
+ * widget; a throw here is swallowed. Sugar over `window.matterfact.onEvent`.
329
+ */
330
+ onEvent?: (e: MatterfactEvent) => void;
282
331
  }
283
- declare function MatterfactAgent({ publishableKey, widgetOrigin, surface, theme, getAuthToken, getPageContext, inline, className, style, pageContext, dev, actions, sitemap, artifacts, resolve, tools, onToolEvent, }: MatterfactAgentProps): react_jsx_runtime.JSX.Element | null;
332
+ declare function MatterfactAgent({ publishableKey, widgetOrigin, surface, theme, getAuthToken, getPageContext, inline, className, style, pageContext, dev, actions, sitemap, artifacts, resolve, tools, onToolEvent, onEvent, }: MatterfactAgentProps): react_jsx_runtime.JSX.Element | null;
284
333
  interface MatterfactArtifactProps {
285
334
  /** The artifact's slug/name. */
286
335
  slug: string;
@@ -301,4 +350,4 @@ interface MatterfactArtifactProps {
301
350
  */
302
351
  declare function MatterfactArtifact({ slug, owner, token, widgetOrigin, theme, className, style, }: MatterfactArtifactProps): react_jsx_runtime.JSX.Element;
303
352
 
304
- export { MatterfactAgent, type MatterfactAgentProps, MatterfactArtifact, type MatterfactArtifactProps };
353
+ export { type HostResolvers, type HostToolDef, MatterfactAgent, type MatterfactAgentProps, MatterfactArtifact, type MatterfactArtifactProps };
package/dist/react.d.ts CHANGED
@@ -93,6 +93,44 @@ interface ToolEvent {
93
93
  error?: string;
94
94
  ms?: number;
95
95
  }
96
+ /**
97
+ * One embed lifecycle event, surfaced to the host via `onEvent` — the host's single
98
+ * telemetry hook. Fired loader-side (lifecycle) and widget-side (chat); a throwing or
99
+ * slow callback NEVER blocks or breaks the mount/call/auth/run path.
100
+ *
101
+ * - `ready` — the widget connected (protocol handshake done).
102
+ * - `open`/`close` — the corner widget was opened/closed. (Inline has no open event —
103
+ * the host's own panel is the open/close control.)
104
+ * - `navigate` — the agent deeplinked the host to one of its own routes.
105
+ * - `auth` — host-auth passthrough: `required` (widget asked), `granted` (a
106
+ * token was handed over), `failed` (no provider result / the provider threw).
107
+ * - `error` — the widget failed to mount / load.
108
+ * - `chat` — a chat-turn moment. Content-free: phase + opaque chatId only.
109
+ * - `tool` — a host-tool lifecycle event (the `ToolEvent` shape, tagged). Also
110
+ * still delivered untagged to the legacy `onToolEvent` hook for back-compat.
111
+ */
112
+ type MatterfactEvent = {
113
+ type: 'ready';
114
+ } | {
115
+ type: 'open';
116
+ } | {
117
+ type: 'close';
118
+ } | {
119
+ type: 'navigate';
120
+ href: string;
121
+ } | {
122
+ type: 'auth';
123
+ phase: 'required' | 'granted' | 'failed';
124
+ } | {
125
+ type: 'error';
126
+ message: string;
127
+ } | {
128
+ type: 'chat';
129
+ phase: 'message' | 'response-start' | 'response-end';
130
+ chatId?: string;
131
+ } | ({
132
+ type: 'tool';
133
+ } & ToolEvent);
96
134
 
97
135
  /**
98
136
  * The Hoist adapter — a pure transform from a `HoistRuntime` (already read out of
@@ -277,10 +315,21 @@ interface MatterfactAgentProps {
277
315
  * Host telemetry hook: fired on every host-tool advertise/call/result so you can pipe
278
316
  * calls into your own observability. Never blocks or breaks the call path — a throw here
279
317
  * is swallowed. Sugar over `window.matterfact.onToolEvent`.
318
+ *
319
+ * NOTE: this is now a SUBSET of `onEvent` (the tool events only), kept for back-compat.
320
+ * Prefer `onEvent` for new integrations — it delivers these same tool events (tagged
321
+ * `type:'tool'`) alongside the widget's lifecycle and chat events.
280
322
  */
281
323
  onToolEvent?: (e: ToolEvent) => void;
324
+ /**
325
+ * UNIFIED host telemetry hook: fired on every embed lifecycle moment — `ready`,
326
+ * `open`/`close`, `navigate`, `auth`, `error`, `chat`, and `tool` — so you can pipe the
327
+ * whole widget into your own observability from one place. Never blocks or breaks the
328
+ * widget; a throw here is swallowed. Sugar over `window.matterfact.onEvent`.
329
+ */
330
+ onEvent?: (e: MatterfactEvent) => void;
282
331
  }
283
- declare function MatterfactAgent({ publishableKey, widgetOrigin, surface, theme, getAuthToken, getPageContext, inline, className, style, pageContext, dev, actions, sitemap, artifacts, resolve, tools, onToolEvent, }: MatterfactAgentProps): react_jsx_runtime.JSX.Element | null;
332
+ declare function MatterfactAgent({ publishableKey, widgetOrigin, surface, theme, getAuthToken, getPageContext, inline, className, style, pageContext, dev, actions, sitemap, artifacts, resolve, tools, onToolEvent, onEvent, }: MatterfactAgentProps): react_jsx_runtime.JSX.Element | null;
284
333
  interface MatterfactArtifactProps {
285
334
  /** The artifact's slug/name. */
286
335
  slug: string;
@@ -301,4 +350,4 @@ interface MatterfactArtifactProps {
301
350
  */
302
351
  declare function MatterfactArtifact({ slug, owner, token, widgetOrigin, theme, className, style, }: MatterfactArtifactProps): react_jsx_runtime.JSX.Element;
303
352
 
304
- export { MatterfactAgent, type MatterfactAgentProps, MatterfactArtifact, type MatterfactArtifactProps };
353
+ export { type HostResolvers, type HostToolDef, MatterfactAgent, type MatterfactAgentProps, MatterfactArtifact, type MatterfactArtifactProps };
package/dist/react.js CHANGED
@@ -371,6 +371,21 @@ var EmbedHost = class {
371
371
  __testHandle(msg) {
372
372
  this.handle(msg);
373
373
  }
374
+ /**
375
+ * The eager stub's own emit-and-swallow for the host's `onEvent` telemetry hook.
376
+ * Deliberately does NOT import the registry's `emitEvent`: pulling the lazy ./context
377
+ * chunk (where the registry lives) into the size-budgeted stub would blow it, so this
378
+ * ~5-line duplication across the bundle boundary is intentional. Host telemetry is
379
+ * host code — a throw here must never break the widget.
380
+ */
381
+ emit(e) {
382
+ const cb = globalThis.matterfact?.onEvent;
383
+ if (typeof cb !== "function") return;
384
+ try {
385
+ cb(e);
386
+ } catch {
387
+ }
388
+ }
374
389
  handle(msg) {
375
390
  switch (msg.type) {
376
391
  case "widget.ready":
@@ -383,8 +398,10 @@ var EmbedHost = class {
383
398
  this.send({ type: "host.theme", mode: this.themeMode() });
384
399
  this.flush();
385
400
  if (this.inline) void this.loadContext();
401
+ this.emit({ type: "ready" });
386
402
  break;
387
403
  case "widget.setOpen":
404
+ this.emit({ type: msg.open ? "open" : "close" });
388
405
  if (this.inline) break;
389
406
  this.open = msg.open;
390
407
  this.place();
@@ -573,11 +590,16 @@ var EmbedHost = class {
573
590
  if (this.hostEl) this.hostEl.style.display = "none";
574
591
  break;
575
592
  case "widget.needsAuth":
593
+ this.emit({ type: "auth", phase: "required" });
576
594
  void this.provideAuth();
577
595
  break;
578
596
  case "widget.navigate":
597
+ this.emit({ type: "navigate", href: msg.href });
579
598
  void this.loadContext().then((m) => m.navigateHost(msg.href));
580
599
  break;
600
+ case "widget.chat":
601
+ this.emit({ type: "chat", phase: msg.phase, chatId: msg.chatId });
602
+ break;
581
603
  }
582
604
  }
583
605
  /**
@@ -690,11 +712,13 @@ var EmbedHost = class {
690
712
  try {
691
713
  const token = await provider();
692
714
  if (token) this.send({ type: "host.auth", token, expiresAt: 0 });
715
+ this.emit({ type: "auth", phase: token ? "granted" : "failed" });
693
716
  } catch {
717
+ this.emit({ type: "auth", phase: "failed" });
694
718
  }
695
719
  }
696
720
  loadContext() {
697
- this.context ?? (this.context = import("./context-HOOW63MO.js").then((m) => {
721
+ this.context ?? (this.context = import("./context-IK5MECUW.js").then((m) => {
698
722
  m.start(
699
723
  this.send,
700
724
  this.config.origin,
@@ -747,13 +771,14 @@ function writeSitemapGlobal(sitemap) {
747
771
  const w = window;
748
772
  (w.matterfact ?? (w.matterfact = {})).sitemap = sitemap;
749
773
  }
750
- function writeToolGlobals(artifacts, resolve, tools, onToolEvent) {
774
+ function writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent) {
751
775
  if (typeof window === "undefined") return;
752
776
  const mf = window.matterfact ?? (window.matterfact = {});
753
777
  mf.artifacts = artifacts ?? [];
754
778
  mf.resolve = resolve ?? {};
755
779
  mf.tools = tools ?? [];
756
780
  mf.onToolEvent = onToolEvent;
781
+ mf.onEvent = onEvent;
757
782
  }
758
783
  function toolDescriptorKey(artifacts, resolve, tools) {
759
784
  return JSON.stringify({
@@ -794,7 +819,8 @@ function MatterfactAgent({
794
819
  artifacts,
795
820
  resolve,
796
821
  tools,
797
- onToolEvent
822
+ onToolEvent,
823
+ onEvent
798
824
  }) {
799
825
  const authRef = useRef(getAuthToken);
800
826
  authRef.current = getAuthToken;
@@ -809,14 +835,14 @@ function MatterfactAgent({
809
835
  writeSitemapGlobal(sitemap);
810
836
  }, [sitemapKey]);
811
837
  useEffect(() => {
812
- writeToolGlobals(artifacts, resolve, tools, onToolEvent);
838
+ writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent);
813
839
  });
814
840
  const toolsKey = toolDescriptorKey(artifacts, resolve, tools);
815
841
  useEffect(() => {
816
842
  if (typeof window === "undefined") return;
817
843
  if (inline && !slot.current) return;
818
844
  writeActionsGlobal(actionsRef.current);
819
- writeToolGlobals(artifacts, resolve, tools, onToolEvent);
845
+ writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent);
820
846
  const config = {
821
847
  publishableKey,
822
848
  origin: widgetOrigin || DEFAULT_ORIGIN,
@@ -837,6 +863,13 @@ function MatterfactAgent({
837
863
  host = mount(config);
838
864
  } catch (e) {
839
865
  console.error("[matterfact] failed to mount the embed widget", e);
866
+ try {
867
+ window.matterfact?.onEvent?.({
868
+ type: "error",
869
+ message: e instanceof Error ? e.message : String(e)
870
+ });
871
+ } catch {
872
+ }
840
873
  }
841
874
  return () => host?.destroy();
842
875
  }, [
@@ -882,7 +915,7 @@ function MatterfactArtifact({
882
915
  title: `matterfact artifact ${slug}`,
883
916
  className,
884
917
  style: { width: "100%", height: 600, border: 0, ...style },
885
- sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"
918
+ sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox allow-downloads"
886
919
  }
887
920
  );
888
921
  }