@matterfact/embed 0.11.3 → 0.13.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.
Files changed (44) hide show
  1. package/README.md +41 -0
  2. package/dist/chunk-C7DXO37G.js +2 -0
  3. package/dist/{chunk-CTZEDOH7.js → chunk-JLJG3MDX.js} +101 -51
  4. package/dist/chunk-JLJG3MDX.js.map +1 -0
  5. package/dist/chunk-QZCUQZJK.js +3 -0
  6. package/dist/chunk-QZCUQZJK.js.map +7 -0
  7. package/dist/{chunk-AANODHBV.js → chunk-S6OT47DL.js} +100 -49
  8. package/dist/chunk-S6OT47DL.js.map +1 -0
  9. package/dist/{chunk-JO3GWFMJ.js → chunk-UA4Y2O64.js} +2 -2
  10. package/dist/{context-FR7VFENN.js → context-DAXACLSP.js} +3 -3
  11. package/dist/{context-FR7VFENN.js.map → context-DAXACLSP.js.map} +1 -1
  12. package/dist/{context-IK5MECUW.js → context-IJK345PN.js} +4 -2
  13. package/dist/embed.js +1 -1
  14. package/dist/embed.js.map +3 -3
  15. package/dist/index.cjs +167 -53
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +83 -10
  18. package/dist/index.d.ts +83 -10
  19. package/dist/index.js +63 -6
  20. package/dist/index.js.map +1 -1
  21. package/dist/react.cjs +159 -52
  22. package/dist/react.cjs.map +1 -1
  23. package/dist/react.d.cts +29 -2
  24. package/dist/react.d.ts +29 -2
  25. package/dist/react.js +54 -5
  26. package/dist/react.js.map +1 -1
  27. package/dist/{snapshot-2V5SDSH2.js → snapshot-EVBNVVCT.js} +2 -2
  28. package/dist/{snapshot-Y5BF2UJR.js → snapshot-P5SVKSPB.js} +3 -3
  29. package/dist/{snapshot-Y5BF2UJR.js.map → snapshot-P5SVKSPB.js.map} +1 -1
  30. package/examples/embed-demo/.env.example +7 -3
  31. package/examples/embed-demo/README.md +25 -4
  32. package/examples/embed-demo/src/App.tsx +67 -7
  33. package/examples/embed-demo/src/styles.css +52 -0
  34. package/examples/embed-demo/vite.config.ts +3 -2
  35. package/package.json +1 -1
  36. package/dist/chunk-AANODHBV.js.map +0 -1
  37. package/dist/chunk-CTZEDOH7.js.map +0 -1
  38. package/dist/chunk-SKJFF7RD.js +0 -2
  39. package/dist/chunk-Y7I25VHL.js +0 -3
  40. package/dist/chunk-Y7I25VHL.js.map +0 -7
  41. /package/dist/{chunk-SKJFF7RD.js.map → chunk-C7DXO37G.js.map} +0 -0
  42. /package/dist/{chunk-JO3GWFMJ.js.map → chunk-UA4Y2O64.js.map} +0 -0
  43. /package/dist/{context-IK5MECUW.js.map → context-IJK345PN.js.map} +0 -0
  44. /package/dist/{snapshot-2V5SDSH2.js.map → snapshot-EVBNVVCT.js.map} +0 -0
package/dist/react.d.cts CHANGED
@@ -108,7 +108,30 @@ interface ToolEvent {
108
108
  * - `chat` — a chat-turn moment. Content-free: phase + opaque chatId only.
109
109
  * - `tool` — a host-tool lifecycle event (the `ToolEvent` shape, tagged). Also
110
110
  * still delivered untagged to the legacy `onToolEvent` hook for back-compat.
111
+ * - `artifactParams` — an artifact param changed in the widget.
111
112
  */
113
+ /**
114
+ * A value an artifact param can carry. Mirrors the app's `ParamValue`
115
+ * (`enum` / `enum[]` / `entity` / `date` / `daterange` / `boolean` / `number`)
116
+ * DELIBERATELY BY COPY, not by import: this is a wire contract between two
117
+ * independently-deployed artifacts, and a shared type would let a rename
118
+ * compile on both sides while still breaking the wire. Bump PROTOCOL_VERSION
119
+ * instead. See this file's header.
120
+ */
121
+ type ArtifactParamValue = string | string[] | number | boolean
122
+ /** `daterange`: a [from, to] pair of YYYY-MM-DD strings. */
123
+ | [string, string];
124
+ /**
125
+ * Artifact params on the wire, keyed by declared param name.
126
+ *
127
+ * Values are typed but NOT verified against the artifact's declared spec —
128
+ * a host can send anything, and the widget's param store coerces each value
129
+ * to its declared type (or drops it) on arrival. This type says what a
130
+ * well-behaved host means to send, not what the widget trusts it to have sent.
131
+ */
132
+ type ArtifactParams = {
133
+ [name: string]: ArtifactParamValue;
134
+ };
112
135
  type MatterfactEvent = {
113
136
  type: 'ready';
114
137
  } | {
@@ -130,7 +153,11 @@ type MatterfactEvent = {
130
153
  chatId?: string;
131
154
  } | ({
132
155
  type: 'tool';
133
- } & ToolEvent);
156
+ } & ToolEvent) | {
157
+ type: 'artifactParams';
158
+ params: ArtifactParams;
159
+ source: 'user' | 'context';
160
+ };
134
161
 
135
162
  /**
136
163
  * The Hoist adapter — a pure transform from a `HoistRuntime` (already read out of
@@ -255,7 +282,7 @@ interface MatterfactAgentProps {
255
282
  * Default `true`. Pass `false` for a host that wants agent chat with nothing
256
283
  * about the page ever reaching it.
257
284
  */
258
- pageContext?: boolean;
285
+ pageContext?: boolean | 'full' | 'declared' | 'off';
259
286
  /**
260
287
  * Force dev mode programmatically. The URL trigger (`?mfdev=1`, see `loader.ts`)
261
288
  * still works unchanged — this is the React-prop equivalent, and the two are
package/dist/react.d.ts CHANGED
@@ -108,7 +108,30 @@ interface ToolEvent {
108
108
  * - `chat` — a chat-turn moment. Content-free: phase + opaque chatId only.
109
109
  * - `tool` — a host-tool lifecycle event (the `ToolEvent` shape, tagged). Also
110
110
  * still delivered untagged to the legacy `onToolEvent` hook for back-compat.
111
+ * - `artifactParams` — an artifact param changed in the widget.
111
112
  */
113
+ /**
114
+ * A value an artifact param can carry. Mirrors the app's `ParamValue`
115
+ * (`enum` / `enum[]` / `entity` / `date` / `daterange` / `boolean` / `number`)
116
+ * DELIBERATELY BY COPY, not by import: this is a wire contract between two
117
+ * independently-deployed artifacts, and a shared type would let a rename
118
+ * compile on both sides while still breaking the wire. Bump PROTOCOL_VERSION
119
+ * instead. See this file's header.
120
+ */
121
+ type ArtifactParamValue = string | string[] | number | boolean
122
+ /** `daterange`: a [from, to] pair of YYYY-MM-DD strings. */
123
+ | [string, string];
124
+ /**
125
+ * Artifact params on the wire, keyed by declared param name.
126
+ *
127
+ * Values are typed but NOT verified against the artifact's declared spec —
128
+ * a host can send anything, and the widget's param store coerces each value
129
+ * to its declared type (or drops it) on arrival. This type says what a
130
+ * well-behaved host means to send, not what the widget trusts it to have sent.
131
+ */
132
+ type ArtifactParams = {
133
+ [name: string]: ArtifactParamValue;
134
+ };
112
135
  type MatterfactEvent = {
113
136
  type: 'ready';
114
137
  } | {
@@ -130,7 +153,11 @@ type MatterfactEvent = {
130
153
  chatId?: string;
131
154
  } | ({
132
155
  type: 'tool';
133
- } & ToolEvent);
156
+ } & ToolEvent) | {
157
+ type: 'artifactParams';
158
+ params: ArtifactParams;
159
+ source: 'user' | 'context';
160
+ };
134
161
 
135
162
  /**
136
163
  * The Hoist adapter — a pure transform from a `HoistRuntime` (already read out of
@@ -255,7 +282,7 @@ interface MatterfactAgentProps {
255
282
  * Default `true`. Pass `false` for a host that wants agent chat with nothing
256
283
  * about the page ever reaching it.
257
284
  */
258
- pageContext?: boolean;
285
+ pageContext?: boolean | 'full' | 'declared' | 'off';
259
286
  /**
260
287
  * Force dev mode programmatically. The URL trigger (`?mfdev=1`, see `loader.ts`)
261
288
  * still works unchanged — this is the React-prop equivalent, and the two are
package/dist/react.js CHANGED
@@ -229,11 +229,19 @@ function dockBox(g, vw, _vh) {
229
229
  // src/loader.ts
230
230
  function devRequested() {
231
231
  try {
232
- return new URLSearchParams(location.search).get("mfdev") === "1";
232
+ if (new URLSearchParams(location.search).get("mfdev") === "1") return true;
233
+ try {
234
+ return localStorage.getItem("mfdev") === "1";
235
+ } catch {
236
+ return false;
237
+ }
233
238
  } catch {
234
239
  return false;
235
240
  }
236
241
  }
242
+ function alog(m, x) {
243
+ if (devRequested()) console.info("[embed auth] host: " + m, x ?? "");
244
+ }
237
245
  var POS_KEY = "mf.embed.pos";
238
246
  var EmbedHost = class {
239
247
  constructor(config) {
@@ -495,6 +503,11 @@ var EmbedHost = class {
495
503
  case "widget.requestContext":
496
504
  void this.loadContext().then((m) => m.provideContext());
497
505
  break;
506
+ case "widget.pageContextMax":
507
+ void this.loadContext().then((m) => {
508
+ m.setPageContextMax(msg.mode);
509
+ });
510
+ break;
498
511
  case "widget.requestSnapshot":
499
512
  void this.loadContext().then((m) => m.sendSnapshot(this.send));
500
513
  break;
@@ -600,6 +613,13 @@ var EmbedHost = class {
600
613
  case "widget.chat":
601
614
  this.emit({ type: "chat", phase: msg.phase, chatId: msg.chatId });
602
615
  break;
616
+ case "widget.artifactParams":
617
+ this.emit({
618
+ type: "artifactParams",
619
+ params: msg.params,
620
+ source: msg.source
621
+ });
622
+ break;
603
623
  }
604
624
  }
605
625
  /**
@@ -707,18 +727,26 @@ var EmbedHost = class {
707
727
  */
708
728
  async provideAuth() {
709
729
  const provider = this.config.authTokenProvider ?? globalThis.matterfact?.getEmbedAuthToken;
710
- if (!provider) return;
711
- if (++this.ac > 5) return;
730
+ if (!provider) {
731
+ alog("no provider");
732
+ return;
733
+ }
734
+ if (++this.ac > 5) {
735
+ alog("storm cap");
736
+ return;
737
+ }
712
738
  try {
713
739
  const token = await provider();
714
740
  if (token) this.send({ type: "host.auth", token, expiresAt: 0 });
715
741
  this.emit({ type: "auth", phase: token ? "granted" : "failed" });
716
- } catch {
742
+ alog(token ? "token" : "empty");
743
+ } catch (e) {
717
744
  this.emit({ type: "auth", phase: "failed" });
745
+ alog("threw", e);
718
746
  }
719
747
  }
720
748
  loadContext() {
721
- this.context ?? (this.context = import("./context-IK5MECUW.js").then((m) => {
749
+ this.context ?? (this.context = import("./context-IJK345PN.js").then((m) => {
722
750
  m.start(
723
751
  this.send,
724
752
  this.config.origin,
@@ -737,6 +765,23 @@ var EmbedHost = class {
737
765
  this.queue = [];
738
766
  for (const m of pending) this.send(m);
739
767
  }
768
+ /**
769
+ * Push artifact param values into the widget — a PARTIAL patch; only the named
770
+ * params change. Buffered like every other host→widget message until the widget
771
+ * says it's listening.
772
+ *
773
+ * Explicit and deliberate, so it applies even to a param whose page-context binding
774
+ * the viewer has overridden (spec §7.3).
775
+ */
776
+ setArtifactParams(params) {
777
+ if (params == null || typeof params !== "object" || Array.isArray(params)) {
778
+ console.error(
779
+ "[matterfact] setArtifactParams expects an object of param values"
780
+ );
781
+ return;
782
+ }
783
+ this.send({ type: "host.artifactParams", params });
784
+ }
740
785
  /** Tear down: stop listening and remove the host element. For the React wrapper's
741
786
  * unmount — the vanilla `<script>` loader lives for the page's lifetime and never
742
787
  * calls this. */
@@ -754,6 +799,10 @@ var EmbedHost = class {
754
799
  function mount(config) {
755
800
  const host = new EmbedHost(config);
756
801
  host.mount();
802
+ window.matterfact = {
803
+ ...window.matterfact ?? {},
804
+ setArtifactParams: (params) => host.setArtifactParams(params)
805
+ };
757
806
  return host;
758
807
  }
759
808