@matterfact/embed 0.15.0 → 0.17.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/react.d.cts CHANGED
@@ -295,6 +295,15 @@ interface MatterfactAgentProps {
295
295
  widgetOrigin?: string;
296
296
  /** Label for THIS embedding, for per-surface usage/history attribution. */
297
297
  surface?: string;
298
+ /**
299
+ * Host-site share-link template. When set, "Share chat" links are minted in this
300
+ * format (`{share_token}` is replaced), and when this page is loaded at a URL
301
+ * matching the template the widget auto-opens that shared conversation.
302
+ * Exactly one `{share_token}`, as a whole path segment or whole query value,
303
+ * e.g. "https://portal.acme.com/assistant?mf_share={share_token}".
304
+ * Read at mount; change requires a remount.
305
+ */
306
+ shareDeeplinkFormat?: string;
298
307
  /** Resolved as `prop ?? MatterfactAuthProvider's theme ?? 'auto'`. */
299
308
  theme?: 'light' | 'dark' | 'auto';
300
309
  /**
@@ -409,7 +418,7 @@ interface MatterfactAgentProps {
409
418
  */
410
419
  onEvent?: (e: MatterfactEvent) => void;
411
420
  }
412
- declare function MatterfactAgent({ publishableKey: publishableKeyProp, widgetOrigin: widgetOriginProp, surface, theme: themeProp, getAuthToken: getAuthTokenProp, getPageContext, inline, className, style, pageContext, dev, actions, sitemap, artifacts, resolve, tools, onToolEvent, onEvent, }: MatterfactAgentProps): react_jsx_runtime.JSX.Element | null;
421
+ declare function MatterfactAgent({ publishableKey: publishableKeyProp, widgetOrigin: widgetOriginProp, surface, shareDeeplinkFormat, theme: themeProp, getAuthToken: getAuthTokenProp, getPageContext, inline, className, style, pageContext, dev, actions, sitemap, artifacts, resolve, tools, onToolEvent, onEvent, }: MatterfactAgentProps): react_jsx_runtime.JSX.Element | null;
413
422
  interface MatterfactArtifactProps {
414
423
  /** The artifact's name (its slug). */
415
424
  name?: string;
@@ -446,23 +455,50 @@ interface MatterfactArtifactProps {
446
455
  className?: string;
447
456
  /** Applied to the `<iframe>` element. Merged over the default fill. */
448
457
  style?: React.CSSProperties;
458
+ /**
459
+ * Drive the artifact's declared params from your page — the company it's about, the
460
+ * time window, the peer set. View-only: a param change re-renders the artifact from
461
+ * data it already has, and never re-queries or runs the agent.
462
+ *
463
+ * Pass your whole param state; the component works out what actually changed and
464
+ * pushes only that. That matters, because a push is authoritative for every key it
465
+ * names: re-asserting a param the reader has since changed in the artifact's own
466
+ * control bar would silently overwrite their choice. Dropping a key stops driving it
467
+ * and leaves its current value alone — there is no "unset".
468
+ *
469
+ * NOTE: `MatterfactAgent`'s `setArtifactParams` steers artifacts rendered INSIDE the
470
+ * chat. This prop steers THIS artifact. They are different iframes; neither reaches
471
+ * the other.
472
+ */
473
+ params?: Record<string, unknown>;
474
+ /**
475
+ * Fired when a param changes inside the artifact — `'user'` when the reader used its
476
+ * control bar, `'context'` when a bound param followed the host page's context.
477
+ *
478
+ * Never fires for your own `params` pushes, so feeding this straight back into the
479
+ * `params` prop cannot loop.
480
+ */
481
+ onParamsChange?: (params: Record<string, unknown>, source: 'user' | 'context') => void;
449
482
  }
450
483
  /**
451
484
  * Render a matterfact artifact inline in a host page. Chrome-free, cross-origin. When a
452
485
  * MatterfactAgent is embedded on the same page, the loader sees this iframe and both
453
486
  * makes the agent aware of it and (via its token) lets the agent read its data.
454
487
  *
455
- * Two auth modes, `token` wins when both are available:
456
- * - TOKEN mode (`token` given): a standalone share link, `?t=`. This is the LIVE
457
- * third-party embed path its `src` format must stay byte-identical.
458
- * - SESSION mode (no `token`, an ancestor `MatterfactAuthProvider` is present):
459
- * tokenless, `?k=<publishableKey>&o=<host origin>`, the same authentication
460
- * `MatterfactAgent` uses.
488
+ * Two auth modes; a provider wins when both are available:
489
+ * - SESSION mode (an ancestor `MatterfactAuthProvider` is present): tokenless,
490
+ * `?k=<publishableKey>&o=<host origin>`, the same authentication
491
+ * `MatterfactAgent` uses. Chosen even when a `token` is ALSO given — with an
492
+ * identity the embed should read as the signed-in user, and it spares the
493
+ * host from stripping a legacy token to get session auth.
494
+ * - TOKEN mode (`token` given, no provider): a standalone share link, `?t=`.
495
+ * This is the LIVE third-party embed path — its `src` format must stay
496
+ * byte-identical.
461
497
  * With neither, there is no way for the iframe to authenticate, so no iframe is
462
498
  * built at all — matching the "incomplete link" placeholder the embed artifact
463
499
  * page itself renders for a missing owner/token.
464
500
  */
465
- declare function MatterfactArtifact({ name: nameProp, slug, owner, token, widgetOrigin: widgetOriginProp, theme: themeProp, className, style, }: MatterfactArtifactProps): react_jsx_runtime.JSX.Element;
501
+ declare function MatterfactArtifact({ name: nameProp, slug, owner, token, widgetOrigin: widgetOriginProp, theme: themeProp, className, style, params, onParamsChange, }: MatterfactArtifactProps): react_jsx_runtime.JSX.Element;
466
502
  /** The two live-drivable doc params, mirroring `EmbedDocumentPage`'s `HostDocParams`. */
467
503
  type MatterfactDocParams = {
468
504
  ticker?: string;
package/dist/react.d.ts CHANGED
@@ -295,6 +295,15 @@ interface MatterfactAgentProps {
295
295
  widgetOrigin?: string;
296
296
  /** Label for THIS embedding, for per-surface usage/history attribution. */
297
297
  surface?: string;
298
+ /**
299
+ * Host-site share-link template. When set, "Share chat" links are minted in this
300
+ * format (`{share_token}` is replaced), and when this page is loaded at a URL
301
+ * matching the template the widget auto-opens that shared conversation.
302
+ * Exactly one `{share_token}`, as a whole path segment or whole query value,
303
+ * e.g. "https://portal.acme.com/assistant?mf_share={share_token}".
304
+ * Read at mount; change requires a remount.
305
+ */
306
+ shareDeeplinkFormat?: string;
298
307
  /** Resolved as `prop ?? MatterfactAuthProvider's theme ?? 'auto'`. */
299
308
  theme?: 'light' | 'dark' | 'auto';
300
309
  /**
@@ -409,7 +418,7 @@ interface MatterfactAgentProps {
409
418
  */
410
419
  onEvent?: (e: MatterfactEvent) => void;
411
420
  }
412
- declare function MatterfactAgent({ publishableKey: publishableKeyProp, widgetOrigin: widgetOriginProp, surface, theme: themeProp, getAuthToken: getAuthTokenProp, getPageContext, inline, className, style, pageContext, dev, actions, sitemap, artifacts, resolve, tools, onToolEvent, onEvent, }: MatterfactAgentProps): react_jsx_runtime.JSX.Element | null;
421
+ declare function MatterfactAgent({ publishableKey: publishableKeyProp, widgetOrigin: widgetOriginProp, surface, shareDeeplinkFormat, theme: themeProp, getAuthToken: getAuthTokenProp, getPageContext, inline, className, style, pageContext, dev, actions, sitemap, artifacts, resolve, tools, onToolEvent, onEvent, }: MatterfactAgentProps): react_jsx_runtime.JSX.Element | null;
413
422
  interface MatterfactArtifactProps {
414
423
  /** The artifact's name (its slug). */
415
424
  name?: string;
@@ -446,23 +455,50 @@ interface MatterfactArtifactProps {
446
455
  className?: string;
447
456
  /** Applied to the `<iframe>` element. Merged over the default fill. */
448
457
  style?: React.CSSProperties;
458
+ /**
459
+ * Drive the artifact's declared params from your page — the company it's about, the
460
+ * time window, the peer set. View-only: a param change re-renders the artifact from
461
+ * data it already has, and never re-queries or runs the agent.
462
+ *
463
+ * Pass your whole param state; the component works out what actually changed and
464
+ * pushes only that. That matters, because a push is authoritative for every key it
465
+ * names: re-asserting a param the reader has since changed in the artifact's own
466
+ * control bar would silently overwrite their choice. Dropping a key stops driving it
467
+ * and leaves its current value alone — there is no "unset".
468
+ *
469
+ * NOTE: `MatterfactAgent`'s `setArtifactParams` steers artifacts rendered INSIDE the
470
+ * chat. This prop steers THIS artifact. They are different iframes; neither reaches
471
+ * the other.
472
+ */
473
+ params?: Record<string, unknown>;
474
+ /**
475
+ * Fired when a param changes inside the artifact — `'user'` when the reader used its
476
+ * control bar, `'context'` when a bound param followed the host page's context.
477
+ *
478
+ * Never fires for your own `params` pushes, so feeding this straight back into the
479
+ * `params` prop cannot loop.
480
+ */
481
+ onParamsChange?: (params: Record<string, unknown>, source: 'user' | 'context') => void;
449
482
  }
450
483
  /**
451
484
  * Render a matterfact artifact inline in a host page. Chrome-free, cross-origin. When a
452
485
  * MatterfactAgent is embedded on the same page, the loader sees this iframe and both
453
486
  * makes the agent aware of it and (via its token) lets the agent read its data.
454
487
  *
455
- * Two auth modes, `token` wins when both are available:
456
- * - TOKEN mode (`token` given): a standalone share link, `?t=`. This is the LIVE
457
- * third-party embed path its `src` format must stay byte-identical.
458
- * - SESSION mode (no `token`, an ancestor `MatterfactAuthProvider` is present):
459
- * tokenless, `?k=<publishableKey>&o=<host origin>`, the same authentication
460
- * `MatterfactAgent` uses.
488
+ * Two auth modes; a provider wins when both are available:
489
+ * - SESSION mode (an ancestor `MatterfactAuthProvider` is present): tokenless,
490
+ * `?k=<publishableKey>&o=<host origin>`, the same authentication
491
+ * `MatterfactAgent` uses. Chosen even when a `token` is ALSO given — with an
492
+ * identity the embed should read as the signed-in user, and it spares the
493
+ * host from stripping a legacy token to get session auth.
494
+ * - TOKEN mode (`token` given, no provider): a standalone share link, `?t=`.
495
+ * This is the LIVE third-party embed path — its `src` format must stay
496
+ * byte-identical.
461
497
  * With neither, there is no way for the iframe to authenticate, so no iframe is
462
498
  * built at all — matching the "incomplete link" placeholder the embed artifact
463
499
  * page itself renders for a missing owner/token.
464
500
  */
465
- declare function MatterfactArtifact({ name: nameProp, slug, owner, token, widgetOrigin: widgetOriginProp, theme: themeProp, className, style, }: MatterfactArtifactProps): react_jsx_runtime.JSX.Element;
501
+ declare function MatterfactArtifact({ name: nameProp, slug, owner, token, widgetOrigin: widgetOriginProp, theme: themeProp, className, style, params, onParamsChange, }: MatterfactArtifactProps): react_jsx_runtime.JSX.Element;
466
502
  /** The two live-drivable doc params, mirroring `EmbedDocumentPage`'s `HostDocParams`. */
467
503
  type MatterfactDocParams = {
468
504
  ticker?: string;
package/dist/react.js CHANGED
@@ -2,10 +2,10 @@
2
2
  "use client";
3
3
 
4
4
  // src/react.tsx
5
- import { useEffect as useEffect2, useRef } from "react";
5
+ import { useCallback, useEffect as useEffect2, useRef } from "react";
6
6
 
7
7
  // src/protocol.ts
8
- var PROTOCOL_VERSION = 3;
8
+ var PROTOCOL_VERSION = 4;
9
9
  var CHANNEL = "mf-embed";
10
10
  function envelope(payload, id) {
11
11
  return {
@@ -227,6 +227,7 @@ function dockBox(g, vw, _vh) {
227
227
  }
228
228
 
229
229
  // src/loader.ts
230
+ var EMBED_VERSION = "0.17.0";
230
231
  function devRequested() {
231
232
  try {
232
233
  if (new URLSearchParams(location.search).get("mfdev") === "1") return true;
@@ -314,6 +315,8 @@ var EmbedHost = class {
314
315
  this.inline = !!config.container;
315
316
  }
316
317
  mount() {
318
+ const w = window;
319
+ (w.matterfact ?? (w.matterfact = {})).embedVersion = EMBED_VERSION;
317
320
  const host = document.createElement("div");
318
321
  this.hostEl = host;
319
322
  host.id = "matterfact-embed";
@@ -357,7 +360,7 @@ var EmbedHost = class {
357
360
  "sandbox",
358
361
  "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox allow-downloads"
359
362
  );
360
- iframe.setAttribute("allow", "microphone; clipboard-write");
363
+ iframe.setAttribute("allow", "microphone; clipboard-write; web-share");
361
364
  iframe.src = `${this.config.origin}/embed/chat?k=${encodeURIComponent(
362
365
  this.config.publishableKey
363
366
  )}&o=${encodeURIComponent(location.origin)}` + (this.config.surface ? `&s=${encodeURIComponent(this.config.surface)}` : "") + // Either trigger works: the URL param (no redeploy needed) OR the config's
@@ -370,6 +373,17 @@ var EmbedHost = class {
370
373
  window.addEventListener("resize", this.onViewportResize);
371
374
  this.place();
372
375
  }
376
+ const fmt = this.config.shareDeeplinkFormat;
377
+ if (fmt) {
378
+ this.send({ type: "host.deeplinkFormat", format: fmt });
379
+ void import("./deeplink-IBFUWANV.js").then((m) => {
380
+ if (this.destroyed) return;
381
+ this.deeplinkStop = m.installDeeplinkWatch(fmt, (token) => {
382
+ this.send({ type: "host.openShare", token });
383
+ });
384
+ }).catch(() => {
385
+ });
386
+ }
373
387
  }
374
388
  /**
375
389
  * Test seam. The shadow root is CLOSED, so a test cannot reach the iframe to forge a
@@ -793,6 +807,9 @@ var EmbedHost = class {
793
807
  this.iframe = null;
794
808
  this.shadow = null;
795
809
  this.ready = false;
810
+ this.destroyed = true;
811
+ this.deeplinkStop?.();
812
+ this.deeplinkStop = null;
796
813
  void this.context?.then((m) => m.stop());
797
814
  }
798
815
  };
@@ -909,6 +926,7 @@ function MatterfactAgent({
909
926
  publishableKey: publishableKeyProp,
910
927
  widgetOrigin: widgetOriginProp,
911
928
  surface = "",
929
+ shareDeeplinkFormat,
912
930
  theme: themeProp,
913
931
  getAuthToken: getAuthTokenProp,
914
932
  getPageContext,
@@ -970,7 +988,8 @@ function MatterfactAgent({
970
988
  pageContextProvider: async () => await pageContextRef.current?.() ?? null,
971
989
  container: inline ? slot.current : null,
972
990
  pageContext,
973
- dev
991
+ dev,
992
+ shareDeeplinkFormat
974
993
  };
975
994
  let host = null;
976
995
  try {
@@ -990,6 +1009,7 @@ function MatterfactAgent({
990
1009
  publishableKey,
991
1010
  widgetOrigin,
992
1011
  surface,
1012
+ shareDeeplinkFormat,
993
1013
  theme,
994
1014
  inline,
995
1015
  pageContext,
@@ -1011,6 +1031,13 @@ function MatterfactAgent({
1011
1031
  );
1012
1032
  }
1013
1033
  var DEFAULT_ARTIFACT_ORIGIN = "https://app.matterfact.com";
1034
+ function snapshot(params) {
1035
+ const out = /* @__PURE__ */ new Map();
1036
+ for (const [key, value] of Object.entries(params ?? {})) {
1037
+ out.set(key, JSON.stringify(value ?? null));
1038
+ }
1039
+ return out;
1040
+ }
1014
1041
  function MatterfactArtifact({
1015
1042
  name: nameProp,
1016
1043
  slug,
@@ -1019,7 +1046,9 @@ function MatterfactArtifact({
1019
1046
  widgetOrigin: widgetOriginProp,
1020
1047
  theme: themeProp,
1021
1048
  className,
1022
- style
1049
+ style,
1050
+ params,
1051
+ onParamsChange
1023
1052
  }) {
1024
1053
  const ctx = useMatterfactConfig();
1025
1054
  const artifactName = nameProp ?? slug;
@@ -1030,12 +1059,72 @@ function MatterfactArtifact({
1030
1059
  }
1031
1060
  const origin = widgetOriginProp ?? ctx?.widgetOrigin ?? DEFAULT_ARTIFACT_ORIGIN;
1032
1061
  const theme = themeProp ?? ctx?.theme ?? "auto";
1062
+ const frame = useRef(null);
1063
+ const changeRef = useRef(onParamsChange);
1064
+ changeRef.current = onParamsChange;
1065
+ const paramsRef = useRef(params);
1066
+ paramsRef.current = params;
1067
+ const posted = useRef(/* @__PURE__ */ new Map());
1068
+ const post = useCallback(
1069
+ (values) => {
1070
+ if (Object.keys(values).length === 0) return;
1071
+ frame.current?.contentWindow?.postMessage(
1072
+ { type: "host.artifactParams", params: values },
1073
+ origin
1074
+ );
1075
+ },
1076
+ [origin]
1077
+ );
1078
+ const postAll = useCallback(() => {
1079
+ posted.current = snapshot(paramsRef.current);
1080
+ post({ ...paramsRef.current });
1081
+ }, [post]);
1082
+ const onLoad = useCallback(() => postAll(), [postAll]);
1083
+ useEffect2(() => {
1084
+ if (typeof window === "undefined") return;
1085
+ const onMessage = (event) => {
1086
+ if (!frame.current || event.source !== frame.current.contentWindow) return;
1087
+ const data = event.data;
1088
+ if (!data || typeof data !== "object") return;
1089
+ if (data.type === "widget.artifactReady") {
1090
+ postAll();
1091
+ return;
1092
+ }
1093
+ if (data.type === "widget.artifactParams" && data.params) {
1094
+ const source = data.source === "context" ? "context" : "user";
1095
+ try {
1096
+ changeRef.current?.(data.params, source);
1097
+ } catch {
1098
+ }
1099
+ }
1100
+ };
1101
+ window.addEventListener("message", onMessage);
1102
+ return () => window.removeEventListener("message", onMessage);
1103
+ }, [postAll]);
1104
+ const paramsKey = JSON.stringify(params ?? null);
1105
+ const settled = useRef(false);
1106
+ useEffect2(() => {
1107
+ const next = snapshot(paramsRef.current);
1108
+ if (!settled.current) {
1109
+ settled.current = true;
1110
+ posted.current = next;
1111
+ return;
1112
+ }
1113
+ const changed = {};
1114
+ for (const [key, json] of next) {
1115
+ if (posted.current.get(key) !== json) {
1116
+ changed[key] = paramsRef.current[key];
1117
+ }
1118
+ }
1119
+ posted.current = next;
1120
+ post(changed);
1121
+ }, [paramsKey, post]);
1033
1122
  let src;
1034
- if (token) {
1035
- src = `${origin}/embed/artifacts/${encodeURIComponent(artifactName)}?owner=${encodeURIComponent(owner)}&t=${encodeURIComponent(token)}&theme=${theme}`;
1036
- } else if (ctx) {
1123
+ if (ctx) {
1037
1124
  const hostOrigin = typeof window === "undefined" ? "" : window.location.origin;
1038
1125
  src = `${origin}/embed/artifacts/${encodeURIComponent(artifactName)}?owner=${encodeURIComponent(owner)}&k=${encodeURIComponent(ctx.publishableKey)}&o=${encodeURIComponent(hostOrigin)}&theme=${theme}`;
1126
+ } else if (token) {
1127
+ src = `${origin}/embed/artifacts/${encodeURIComponent(artifactName)}?owner=${encodeURIComponent(owner)}&t=${encodeURIComponent(token)}&theme=${theme}`;
1039
1128
  } else {
1040
1129
  return /* @__PURE__ */ jsx2(
1041
1130
  "div",
@@ -1057,7 +1146,9 @@ function MatterfactArtifact({
1057
1146
  return /* @__PURE__ */ jsx2(
1058
1147
  "iframe",
1059
1148
  {
1149
+ ref: frame,
1060
1150
  src,
1151
+ onLoad,
1061
1152
  title: `matterfact artifact ${artifactName}`,
1062
1153
  className,
1063
1154
  style: { width: "100%", height: 600, border: 0, ...style },
@@ -1070,10 +1161,10 @@ function diffDocParams(params, sent) {
1070
1161
  const changed = {};
1071
1162
  for (const [key, value] of Object.entries(params)) {
1072
1163
  if (value === void 0) continue;
1073
- const snapshot = JSON.stringify(value);
1074
- if (sent.get(key) === snapshot) continue;
1164
+ const snapshot2 = JSON.stringify(value);
1165
+ if (sent.get(key) === snapshot2) continue;
1075
1166
  changed[key] = value;
1076
- sent.set(key, snapshot);
1167
+ sent.set(key, snapshot2);
1077
1168
  }
1078
1169
  return changed;
1079
1170
  }