@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.cjs CHANGED
@@ -426,7 +426,16 @@ function toolClass(name) {
426
426
  if (name.startsWith("mf.")) return "mf";
427
427
  return "app";
428
428
  }
429
+ function emitEvent(win, e) {
430
+ const cb = mfGlobal(win).onEvent;
431
+ if (typeof cb !== "function") return;
432
+ try {
433
+ cb(e);
434
+ } catch {
435
+ }
436
+ }
429
437
  function emitToolEvent(win, e) {
438
+ emitEvent(win, { type: "tool", ...e });
430
439
  const cb = mfGlobal(win).onToolEvent;
431
440
  if (typeof cb !== "function") return;
432
441
  try {
@@ -1915,6 +1924,21 @@ var EmbedHost = class {
1915
1924
  __testHandle(msg) {
1916
1925
  this.handle(msg);
1917
1926
  }
1927
+ /**
1928
+ * The eager stub's own emit-and-swallow for the host's `onEvent` telemetry hook.
1929
+ * Deliberately does NOT import the registry's `emitEvent`: pulling the lazy ./context
1930
+ * chunk (where the registry lives) into the size-budgeted stub would blow it, so this
1931
+ * ~5-line duplication across the bundle boundary is intentional. Host telemetry is
1932
+ * host code — a throw here must never break the widget.
1933
+ */
1934
+ emit(e) {
1935
+ const cb = globalThis.matterfact?.onEvent;
1936
+ if (typeof cb !== "function") return;
1937
+ try {
1938
+ cb(e);
1939
+ } catch {
1940
+ }
1941
+ }
1918
1942
  handle(msg) {
1919
1943
  switch (msg.type) {
1920
1944
  case "widget.ready":
@@ -1927,8 +1951,10 @@ var EmbedHost = class {
1927
1951
  this.send({ type: "host.theme", mode: this.themeMode() });
1928
1952
  this.flush();
1929
1953
  if (this.inline) void this.loadContext();
1954
+ this.emit({ type: "ready" });
1930
1955
  break;
1931
1956
  case "widget.setOpen":
1957
+ this.emit({ type: msg.open ? "open" : "close" });
1932
1958
  if (this.inline) break;
1933
1959
  this.open = msg.open;
1934
1960
  this.place();
@@ -2117,11 +2143,16 @@ var EmbedHost = class {
2117
2143
  if (this.hostEl) this.hostEl.style.display = "none";
2118
2144
  break;
2119
2145
  case "widget.needsAuth":
2146
+ this.emit({ type: "auth", phase: "required" });
2120
2147
  void this.provideAuth();
2121
2148
  break;
2122
2149
  case "widget.navigate":
2150
+ this.emit({ type: "navigate", href: msg.href });
2123
2151
  void this.loadContext().then((m) => m.navigateHost(msg.href));
2124
2152
  break;
2153
+ case "widget.chat":
2154
+ this.emit({ type: "chat", phase: msg.phase, chatId: msg.chatId });
2155
+ break;
2125
2156
  }
2126
2157
  }
2127
2158
  /**
@@ -2234,7 +2265,9 @@ var EmbedHost = class {
2234
2265
  try {
2235
2266
  const token = await provider();
2236
2267
  if (token) this.send({ type: "host.auth", token, expiresAt: 0 });
2268
+ this.emit({ type: "auth", phase: token ? "granted" : "failed" });
2237
2269
  } catch {
2270
+ this.emit({ type: "auth", phase: "failed" });
2238
2271
  }
2239
2272
  }
2240
2273
  loadContext() {
@@ -2291,13 +2324,14 @@ function writeSitemapGlobal(sitemap) {
2291
2324
  const w = window;
2292
2325
  (w.matterfact ?? (w.matterfact = {})).sitemap = sitemap;
2293
2326
  }
2294
- function writeToolGlobals(artifacts, resolve, tools, onToolEvent) {
2327
+ function writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent) {
2295
2328
  if (typeof window === "undefined") return;
2296
2329
  const mf = window.matterfact ?? (window.matterfact = {});
2297
2330
  mf.artifacts = artifacts ?? [];
2298
2331
  mf.resolve = resolve ?? {};
2299
2332
  mf.tools = tools ?? [];
2300
2333
  mf.onToolEvent = onToolEvent;
2334
+ mf.onEvent = onEvent;
2301
2335
  }
2302
2336
  function toolDescriptorKey(artifacts, resolve, tools) {
2303
2337
  return JSON.stringify({
@@ -2338,7 +2372,8 @@ function MatterfactAgent({
2338
2372
  artifacts,
2339
2373
  resolve,
2340
2374
  tools,
2341
- onToolEvent
2375
+ onToolEvent,
2376
+ onEvent
2342
2377
  }) {
2343
2378
  const authRef = (0, import_react.useRef)(getAuthToken);
2344
2379
  authRef.current = getAuthToken;
@@ -2353,14 +2388,14 @@ function MatterfactAgent({
2353
2388
  writeSitemapGlobal(sitemap);
2354
2389
  }, [sitemapKey]);
2355
2390
  (0, import_react.useEffect)(() => {
2356
- writeToolGlobals(artifacts, resolve, tools, onToolEvent);
2391
+ writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent);
2357
2392
  });
2358
2393
  const toolsKey = toolDescriptorKey(artifacts, resolve, tools);
2359
2394
  (0, import_react.useEffect)(() => {
2360
2395
  if (typeof window === "undefined") return;
2361
2396
  if (inline && !slot.current) return;
2362
2397
  writeActionsGlobal(actionsRef.current);
2363
- writeToolGlobals(artifacts, resolve, tools, onToolEvent);
2398
+ writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent);
2364
2399
  const config = {
2365
2400
  publishableKey,
2366
2401
  origin: widgetOrigin2 || DEFAULT_ORIGIN,
@@ -2381,6 +2416,13 @@ function MatterfactAgent({
2381
2416
  host = mount(config);
2382
2417
  } catch (e) {
2383
2418
  console.error("[matterfact] failed to mount the embed widget", e);
2419
+ try {
2420
+ window.matterfact?.onEvent?.({
2421
+ type: "error",
2422
+ message: e instanceof Error ? e.message : String(e)
2423
+ });
2424
+ } catch {
2425
+ }
2384
2426
  }
2385
2427
  return () => host?.destroy();
2386
2428
  }, [
@@ -2426,7 +2468,7 @@ function MatterfactArtifact({
2426
2468
  title: `matterfact artifact ${slug}`,
2427
2469
  className: className2,
2428
2470
  style: { width: "100%", height: 600, border: 0, ...style },
2429
- sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox"
2471
+ sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox allow-downloads"
2430
2472
  }
2431
2473
  );
2432
2474
  }