@copilotkit/react-core 1.85.0 → 1.86.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 (70) hide show
  1. package/dist/MarkdownSurfaces-BCVdnDAk.cjs +161 -0
  2. package/dist/MarkdownSurfaces-BCVdnDAk.cjs.map +1 -0
  3. package/dist/MarkdownSurfaces-CjJduaJR.mjs +149 -0
  4. package/dist/MarkdownSurfaces-CjJduaJR.mjs.map +1 -0
  5. package/dist/{MarkdownSurfaces-3cGU5_0H.cjs → chat-BnsrQ0VS.cjs} +667 -320
  6. package/dist/chat-BnsrQ0VS.cjs.map +1 -0
  7. package/dist/{MarkdownSurfaces-D-W4f9Dy.mjs → chat-IWySXWXP.mjs} +647 -312
  8. package/dist/chat-IWySXWXP.mjs.map +1 -0
  9. package/dist/{copilotkit-D6mTCuNU.mjs → copilotkit-B1OLgEU8.mjs} +6 -354
  10. package/dist/copilotkit-B1OLgEU8.mjs.map +1 -0
  11. package/dist/{copilotkit-Dn4PbHJV.d.mts → copilotkit-C7G95NO-.d.mts} +4 -4
  12. package/dist/{copilotkit-Dn4PbHJV.d.mts.map → copilotkit-C7G95NO-.d.mts.map} +1 -1
  13. package/dist/{copilotkit-BpKHgURs.cjs → copilotkit-Ca0IcTZU.cjs} +79 -427
  14. package/dist/copilotkit-Ca0IcTZU.cjs.map +1 -0
  15. package/dist/{copilotkit-CuTAoQF2.d.cts → copilotkit-DwD7D6jJ.d.cts} +2 -2
  16. package/dist/{copilotkit-CuTAoQF2.d.cts.map → copilotkit-DwD7D6jJ.d.cts.map} +1 -1
  17. package/dist/hooks-DMSkcNOl.d.cts +89 -0
  18. package/dist/hooks-DMSkcNOl.d.cts.map +1 -0
  19. package/dist/hooks-EwGvqYsW.d.mts +89 -0
  20. package/dist/hooks-EwGvqYsW.d.mts.map +1 -0
  21. package/dist/index.cjs +8 -8
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.cts +2 -2
  24. package/dist/index.d.mts +2 -2
  25. package/dist/index.mjs +2 -2
  26. package/dist/index.umd.js +645 -140
  27. package/dist/index.umd.js.map +1 -1
  28. package/dist/refs-DL7ITFFD.cjs +43 -0
  29. package/dist/refs-DL7ITFFD.cjs.map +1 -0
  30. package/dist/refs-DiZXJ63p.mjs +31 -0
  31. package/dist/refs-DiZXJ63p.mjs.map +1 -0
  32. package/dist/{runtime-context-Er5UIbZA.d.mts → runtime-context-B4fKyzo6.d.mts} +31 -9
  33. package/dist/runtime-context-B4fKyzo6.d.mts.map +1 -0
  34. package/dist/{runtime-context-DrVNUYLi.d.cts → runtime-context-Da7T5vy4.d.cts} +31 -9
  35. package/dist/runtime-context-Da7T5vy4.d.cts.map +1 -0
  36. package/dist/use-generative-ui-root-CaTyBFNR.cjs +390 -0
  37. package/dist/use-generative-ui-root-CaTyBFNR.cjs.map +1 -0
  38. package/dist/use-generative-ui-root-WI9ERMpo.mjs +385 -0
  39. package/dist/use-generative-ui-root-WI9ERMpo.mjs.map +1 -0
  40. package/dist/v2/generative-ui/native.cjs +66 -0
  41. package/dist/v2/generative-ui/native.cjs.map +1 -0
  42. package/dist/v2/generative-ui/native.d.cts +155 -0
  43. package/dist/v2/generative-ui/native.d.cts.map +1 -0
  44. package/dist/v2/generative-ui/native.d.mts +155 -0
  45. package/dist/v2/generative-ui/native.d.mts.map +1 -0
  46. package/dist/v2/generative-ui/native.mjs +24 -0
  47. package/dist/v2/generative-ui/native.mjs.map +1 -0
  48. package/dist/v2/generative-ui.cjs +45 -70
  49. package/dist/v2/generative-ui.cjs.map +1 -1
  50. package/dist/v2/generative-ui.d.cts +229 -308
  51. package/dist/v2/generative-ui.d.cts.map +1 -1
  52. package/dist/v2/generative-ui.d.mts +229 -308
  53. package/dist/v2/generative-ui.d.mts.map +1 -1
  54. package/dist/v2/generative-ui.mjs +4 -29
  55. package/dist/v2/generative-ui.mjs.map +1 -1
  56. package/dist/v2/headless.d.cts +185 -294
  57. package/dist/v2/headless.d.cts.map +1 -1
  58. package/dist/v2/index.cjs +5 -5
  59. package/dist/v2/index.d.cts +2 -2
  60. package/dist/v2/index.d.mts +2 -2
  61. package/dist/v2/index.mjs +2 -2
  62. package/dist/v2/index.umd.js +645 -140
  63. package/dist/v2/index.umd.js.map +1 -1
  64. package/package.json +14 -10
  65. package/dist/MarkdownSurfaces-3cGU5_0H.cjs.map +0 -1
  66. package/dist/MarkdownSurfaces-D-W4f9Dy.mjs.map +0 -1
  67. package/dist/copilotkit-BpKHgURs.cjs.map +0 -1
  68. package/dist/copilotkit-D6mTCuNU.mjs.map +0 -1
  69. package/dist/runtime-context-DrVNUYLi.d.cts.map +0 -1
  70. package/dist/runtime-context-Er5UIbZA.d.mts.map +0 -1
@@ -1825,7 +1825,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
1825
1825
 
1826
1826
  //#endregion
1827
1827
  //#region package.json
1828
- var version = "1.85.0";
1828
+ var version = "1.86.0";
1829
1829
 
1830
1830
  //#endregion
1831
1831
  //#region src/v2/components/CopilotKitInspector.tsx
@@ -3491,6 +3491,81 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3491
3491
  return null;
3492
3492
  }
3493
3493
 
3494
+ //#endregion
3495
+ //#region src/v2/generative-ui/runtime-context.ts
3496
+ const GenerativeUIRuntimeContext = (0, react.createContext)(null);
3497
+ function useGenerativeUIRuntime() {
3498
+ return (0, react.useContext)(GenerativeUIRuntimeContext);
3499
+ }
3500
+ function useRequiredGenerativeUIRuntime() {
3501
+ const runtime = (0, react.useContext)(GenerativeUIRuntimeContext);
3502
+ if (!runtime) throw new Error("Generative UI components must be rendered inside a generative UI provider: <CopilotKitProvider generativeUI={{ ui }}> on the web, or <GenerativeUIProvider> from @copilotkit/react-native/generative-ui on React Native.");
3503
+ return runtime;
3504
+ }
3505
+ const GenerativeUINodeContext = (0, react.createContext)(null);
3506
+
3507
+ //#endregion
3508
+ //#region src/v2/generative-ui/hooks.ts
3509
+ const noopSubscribe$1 = () => () => {};
3510
+ const zero = () => 0;
3511
+ /** Re-renders on any surface-manager change. */
3512
+ function useManagerVersion(runtime) {
3513
+ return (0, react.useSyncExternalStore)(runtime?.manager.subscribe ?? noopSubscribe$1, runtime?.manager.getVersion ?? zero, zero);
3514
+ }
3515
+
3516
+ //#endregion
3517
+ //#region src/v2/generative-ui/activity.ts
3518
+ /**
3519
+ * The rejected operations of a surface (the runtime's first, then the
3520
+ * client's) and the error that goes with them, for a wrapper: a generic
3521
+ * message, with the same list as its `cause`. The `cause` is not enumerable,
3522
+ * so `JSON.stringify`, a spread and error reporters leave the list out.
3523
+ * `clientRejected` is appended to in place by the manager, so its length is
3524
+ * part of the key.
3525
+ */
3526
+ function useGenerativeUIRejected(serverRejected, clientRejected) {
3527
+ const rejected = (0, react.useMemo)(() => [...serverRejected ?? [], ...clientRejected ?? []], [
3528
+ serverRejected,
3529
+ clientRejected,
3530
+ clientRejected?.length ?? 0
3531
+ ]);
3532
+ return {
3533
+ rejected,
3534
+ error: (0, react.useMemo)(() => rejected.length ? (0, _copilotkit_shared_generative_ui.withCause)(/* @__PURE__ */ new Error(`${rejected.length} UI update(s) could not be shown`), rejected) : void 0, [rejected])
3535
+ };
3536
+ }
3537
+ /**
3538
+ * What a renderer of one surface activity message shows: the surface (or the
3539
+ * failed surface whose `createSurface` was rejected), and its rejected
3540
+ * operations with their error (see {@link useGenerativeUIRejected}). It
3541
+ * ingests `content` into the manager (idempotent: the root ingests the
3542
+ * agent's messages too, and this covers an agent the root has not seen yet),
3543
+ * and renders again on every change of the manager.
3544
+ *
3545
+ * `surface` is undefined when there is no runtime, thread or content, when
3546
+ * the surface's target is not `target`, or when the surface does not exist
3547
+ * (the agent deleted it). Both rejected lists cover only the latest
3548
+ * `render_ui` call (see the manager's ingest).
3549
+ */
3550
+ function useGenerativeUIActivitySurface({ runtime, threadId, agentId, content, target }) {
3551
+ useManagerVersion(runtime);
3552
+ (0, react.useLayoutEffect)(() => {
3553
+ if (runtime && threadId && content) runtime.manager.ingest(threadId, agentId, content);
3554
+ }, [
3555
+ runtime,
3556
+ threadId,
3557
+ agentId,
3558
+ content
3559
+ ]);
3560
+ const surface = runtime && threadId && content && content.target === target ? runtime.manager.getSurface(threadId, content.surfaceId) ?? runtime.manager.getFailedSurface(threadId, content.surfaceId) : void 0;
3561
+ const { rejected, error } = useGenerativeUIRejected(content?.rejected, surface?.clientRejected);
3562
+ return {
3563
+ surface,
3564
+ rejected,
3565
+ error
3566
+ };
3567
+ }
3568
+
3494
3569
  //#endregion
3495
3570
  //#region src/v2/generative-ui/markdown/context.ts
3496
3571
  /**
@@ -3572,9 +3647,38 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3572
3647
  //#endregion
3573
3648
  //#region src/v2/generative-ui/use-generative-ui-root.ts
3574
3649
  const isDevelopment = () => process.env.NODE_ENV !== "production";
3650
+ /**
3651
+ * The malformed-content warnings already printed, by manager, so each
3652
+ * distinct issue warns once while the manager lives.
3653
+ */
3654
+ const warnedMalformed = /* @__PURE__ */ new WeakMap();
3655
+ /**
3656
+ * Ingests the surface activity messages of `messages`. A message whose
3657
+ * content is malformed (for example `null`) is skipped, in development with a
3658
+ * warning once for each distinct issue, so it cannot throw inside the
3659
+ * agent's run; the messages after it are still ingested.
3660
+ */
3575
3661
  function ingestMessages(manager, agent, messages) {
3576
- if (!agent.threadId) return;
3577
- for (const message of messages) if (message.role === "activity" && message.activityType === _copilotkit_shared.GENERATIVE_UI_ACTIVITY_TYPE) manager.ingest(agent.threadId, agent.agentId, message.content);
3662
+ const { threadId } = agent;
3663
+ if (!threadId) return;
3664
+ for (const message of messages) {
3665
+ if (message.role !== "activity" || message.activityType !== _copilotkit_shared.GENERATIVE_UI_ACTIVITY_TYPE) continue;
3666
+ const parsed = (0, _copilotkit_core_generative_ui.parseGenerativeUISurfaceContent)(message.content);
3667
+ if (parsed.content) {
3668
+ manager.ingest(threadId, agent.agentId, parsed.content);
3669
+ continue;
3670
+ }
3671
+ if (!isDevelopment()) continue;
3672
+ const warning = `[generativeUI] skipped an activity message of thread ${JSON.stringify(threadId)}: its content is malformed: ${parsed.issues.join("; ")}.`;
3673
+ let warned = warnedMalformed.get(manager);
3674
+ if (!warned) {
3675
+ warned = /* @__PURE__ */ new Set();
3676
+ warnedMalformed.set(manager, warned);
3677
+ }
3678
+ if (warned.has(warning)) continue;
3679
+ warned.add(warning);
3680
+ console.warn(warning);
3681
+ }
3578
3682
  }
3579
3683
  /** Provider-side wiring: manager, dispatcher, agent subscriptions, envelope. */
3580
3684
  function useGenerativeUIRoot(copilotkit, config) {
@@ -3840,29 +3944,35 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3840
3944
  }
3841
3945
 
3842
3946
  //#endregion
3843
- //#region src/v2/generative-ui/runtime-context.ts
3844
- const GenerativeUIRuntimeContext = (0, react.createContext)(null);
3845
- function useGenerativeUIRuntime() {
3846
- return (0, react.useContext)(GenerativeUIRuntimeContext);
3847
- }
3848
- function useRequiredGenerativeUIRuntime() {
3849
- const runtime = (0, react.useContext)(GenerativeUIRuntimeContext);
3850
- if (!runtime) throw new Error("Generative UI components must be rendered inside <CopilotKitProvider generativeUI={{ ui }}>.");
3851
- return runtime;
3947
+ //#region src/v2/generative-ui/host.tsx
3948
+ const DISPLAY_CONTENTS = { display: "contents" };
3949
+ /**
3950
+ * The root element is always there, so a theme change does not remount the
3951
+ * surface. `display: contents` keeps it out of the layout, and the CSS
3952
+ * variables still reach every component of the surface. The `a2ui-surface`
3953
+ * class scopes the standard catalog's surface CSS rules. The theme is a
3954
+ * stylesheet, not an inline style, so that its `dark` values can apply in
3955
+ * dark mode only. The values are checked in `themeStyleSheet`, and the HTML
3956
+ * is set as is, so the server render keeps the quotes of the rule.
3957
+ */
3958
+ function DomSurfaceRoot({ surfaceId, theme, children }) {
3959
+ const sheet = react.default.useMemo(() => (0, _copilotkit_shared_generative_ui.themeStyleSheet)(theme), [theme]);
3960
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3961
+ className: "a2ui-surface",
3962
+ "data-generative-ui-root": surfaceId,
3963
+ "data-a2ui-theme": sheet?.id,
3964
+ style: DISPLAY_CONTENTS,
3965
+ children: [sheet ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("style", { dangerouslySetInnerHTML: { __html: sheet.css } }) : null, children]
3966
+ });
3852
3967
  }
3853
- const GenerativeUINodeContext = (0, react.createContext)(null);
3854
-
3855
- //#endregion
3856
- //#region src/v2/generative-ui/hooks.ts
3857
- const noopSubscribe$1 = () => () => {};
3858
- const zero = () => 0;
3859
- /** Re-renders on any surface-manager change. */
3860
- function useManagerVersion(runtime) {
3861
- return (0, react.useSyncExternalStore)(runtime?.manager.subscribe ?? noopSubscribe$1, runtime?.manager.getVersion ?? zero, zero);
3968
+ function DomAccessibilityWrapper({ attributes, children }) {
3969
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
3970
+ "data-a2ui-accessibility": "",
3971
+ style: DISPLAY_CONTENTS,
3972
+ ...attributes,
3973
+ children
3974
+ });
3862
3975
  }
3863
-
3864
- //#endregion
3865
- //#region src/v2/generative-ui/boundaries.tsx
3866
3976
  function DefaultNodeFallback({ message, componentId, retry }) {
3867
3977
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3868
3978
  role: "alert",
@@ -3890,7 +4000,24 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3890
4000
  ]
3891
4001
  });
3892
4002
  }
4003
+ /** The DOM host: what a surface draws on the web. */
4004
+ const domGenerativeUIHost = {
4005
+ SurfaceRoot: DomSurfaceRoot,
4006
+ AccessibilityWrapper: DomAccessibilityWrapper,
4007
+ NodeFallback: DefaultNodeFallback
4008
+ };
3893
4009
  /**
4010
+ * The host elements of the surfaces below. Defaults to the DOM host, so a
4011
+ * web app never sets it.
4012
+ */
4013
+ const GenerativeUIHostContext = (0, react.createContext)(domGenerativeUIHost);
4014
+ function useGenerativeUIHost() {
4015
+ return (0, react.useContext)(GenerativeUIHostContext);
4016
+ }
4017
+
4018
+ //#endregion
4019
+ //#region src/v2/generative-ui/boundaries.tsx
4020
+ /**
3894
4021
  * A child that has not streamed yet renders nothing. The prose keeps
3895
4022
  * streaming above the surface, and each component appears as soon as it is
3896
4023
  * complete. A grey block in its place would read as a broken layout.
@@ -3898,18 +4025,188 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3898
4025
  function NodePlaceholder(_props) {
3899
4026
  return null;
3900
4027
  }
3901
- /** Per-node boundary. Resets on retry or a new agent revision, not on data ticks. */
3902
- var NodeBoundary = class extends react.Component {
4028
+ /**
4029
+ * The error that a fallback threw, told apart from the error of the
4030
+ * component it stood in for. The fallback's own error is the `cause`.
4031
+ */
4032
+ function fallbackError(error, componentId) {
4033
+ const subject = componentId === void 0 ? "the surface" : componentId;
4034
+ return (0, _copilotkit_shared_generative_ui.withCause)(/* @__PURE__ */ new Error(`The error fallback of ${subject} threw: ${error.message}`), error);
4035
+ }
4036
+ function createFailureLog() {
4037
+ const last = /* @__PURE__ */ new Map();
4038
+ const released = /* @__PURE__ */ new Set();
4039
+ const releasedSinceSettle = /* @__PURE__ */ new Set();
4040
+ const heldBack = /* @__PURE__ */ new Map();
4041
+ return {
4042
+ report(runtime, slot, info, context = "") {
4043
+ released.delete(slot);
4044
+ const signature = [
4045
+ info.kind,
4046
+ info.error.name,
4047
+ info.error.message,
4048
+ context
4049
+ ].join("\n");
4050
+ if (last.get(slot) === signature) {
4051
+ if (releasedSinceSettle.has(slot)) heldBack.set(slot, {
4052
+ runtime,
4053
+ info,
4054
+ signature
4055
+ });
4056
+ return;
4057
+ }
4058
+ heldBack.delete(slot);
4059
+ last.set(slot, signature);
4060
+ (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, info);
4061
+ },
4062
+ clear(slot) {
4063
+ released.delete(slot);
4064
+ last.delete(slot);
4065
+ heldBack.delete(slot);
4066
+ },
4067
+ retain(slot) {
4068
+ released.delete(slot);
4069
+ },
4070
+ release(slot) {
4071
+ heldBack.delete(slot);
4072
+ if (!last.has(slot)) return;
4073
+ released.add(slot);
4074
+ releasedSinceSettle.add(slot);
4075
+ },
4076
+ settle(missed = false) {
4077
+ for (const slot of missed ? releasedSinceSettle : released) last.delete(slot);
4078
+ if (missed) for (const [slot, { runtime, info, signature }] of heldBack) {
4079
+ if (!releasedSinceSettle.has(slot) || last.has(slot)) continue;
4080
+ last.set(slot, signature);
4081
+ (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, info);
4082
+ }
4083
+ heldBack.clear();
4084
+ released.clear();
4085
+ releasedSinceSettle.clear();
4086
+ }
4087
+ };
4088
+ }
4089
+ /** Outside a surface, every failure is reported. */
4090
+ const UNLOGGED = {
4091
+ report: (runtime, _slot, info) => (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, info),
4092
+ clear: () => {},
4093
+ retain: () => {},
4094
+ release: () => {},
4095
+ settle: () => {}
4096
+ };
4097
+ /** The failure log of the surface being rendered; set by `SurfaceView`. */
4098
+ const GenerativeUIFailureLogContext = (0, react.createContext)(UNLOGGED);
4099
+ function useFailureLog() {
4100
+ return (0, react.useContext)(GenerativeUIFailureLogContext);
4101
+ }
4102
+ /**
4103
+ * The slots of a failure log, per node place (one rendered copy of a node,
4104
+ * see `placeOf` in SurfaceView) and surface boundary.
4105
+ */
4106
+ const failureSlots = {
4107
+ node: (place) => `node ${place}`,
4108
+ appFallback: (place) => `app-fallback ${place}`,
4109
+ renderFallback: (place) => `render-fallback ${place}`,
4110
+ fallback: (place) => `fallback ${place}`,
4111
+ cycle: (place) => `cycle ${place}`,
4112
+ props: (place) => `props ${place}`,
4113
+ surface: (level) => `surface ${level}`,
4114
+ surfaceFallback: (level) => `surface-fallback ${level}`
4115
+ };
4116
+ function sameKeys(a, b) {
4117
+ return a.length === b.length && a.every((value, i) => Object.is(value, b[i]));
4118
+ }
4119
+ var FallbackGuard = class extends react.Component {
3903
4120
  constructor(..._args) {
3904
4121
  super(..._args);
4122
+ this.state = {
4123
+ failed: false,
4124
+ resetKeys: this.props.resetKeys
4125
+ };
4126
+ }
4127
+ static getDerivedStateFromError(thrown) {
4128
+ return {
4129
+ failed: true,
4130
+ error: (0, _copilotkit_shared_generative_ui.toError)(thrown)
4131
+ };
4132
+ }
4133
+ static getDerivedStateFromProps(props, state) {
4134
+ return sameKeys(props.resetKeys, state.resetKeys) ? null : {
4135
+ failed: false,
4136
+ error: void 0,
4137
+ resetKeys: props.resetKeys
4138
+ };
4139
+ }
4140
+ report(error) {
4141
+ const { runtime, surfaceId, componentId, log, slot, context } = this.props;
4142
+ log.report(runtime, slot, {
4143
+ kind: "render",
4144
+ surfaceId,
4145
+ ...componentId === void 0 ? {} : { componentId },
4146
+ error: fallbackError(error, componentId)
4147
+ }, context);
4148
+ }
4149
+ componentDidCatch(thrown) {
4150
+ this.report(this.state.error ?? (0, _copilotkit_shared_generative_ui.toError)(thrown));
4151
+ }
4152
+ componentDidMount() {
4153
+ if (!this.state.failed) {
4154
+ this.props.log.clear(this.props.slot);
4155
+ return;
4156
+ }
4157
+ if (this.state.error) this.report(this.state.error);
4158
+ }
4159
+ componentDidUpdate() {
4160
+ if (!this.state.failed) this.props.log.clear(this.props.slot);
4161
+ }
4162
+ render() {
4163
+ return this.state.failed ? this.props.replacement ?? null : this.props.children;
4164
+ }
4165
+ };
4166
+ /**
4167
+ * The host's `NodeFallback`, guarded: when it throws, the node renders
4168
+ * nothing and the error is reported, and the parent node keeps rendering.
4169
+ * Every place that shows the host fallback goes through this.
4170
+ */
4171
+ function GuardedNodeFallback({ surfaceId, componentId, runtime, message, retry, slot, revision }) {
4172
+ const { NodeFallback } = useGenerativeUIHost();
4173
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FallbackGuard, {
4174
+ surfaceId,
4175
+ componentId,
4176
+ runtime,
4177
+ log: useFailureLog(),
4178
+ slot,
4179
+ context: message,
4180
+ resetKeys: [message, revision],
4181
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeFallback, {
4182
+ message,
4183
+ componentId: componentId ?? "surface",
4184
+ retry
4185
+ })
4186
+ });
4187
+ }
4188
+ /**
4189
+ * Calls the app's `errorBoundary.fallback` in a render of its own, so that
4190
+ * the guard above it catches what it throws.
4191
+ */
4192
+ function AppErrorFallback({ render, ...props }) {
4193
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: render(props) });
4194
+ }
4195
+ /**
4196
+ * Per-node boundary. Resets on retry or a new agent revision, not on data
4197
+ * ticks. A failure that stays the same across revisions is reported once.
4198
+ */
4199
+ var NodeBoundary = class extends react.Component {
4200
+ constructor(..._args2) {
4201
+ super(..._args2);
3905
4202
  this.state = {
3906
4203
  error: null,
3907
4204
  revision: this.props.revision
3908
4205
  };
3909
4206
  this.retry = () => this.setState({ error: null });
3910
4207
  }
3911
- static getDerivedStateFromError(error) {
3912
- return { error };
4208
+ static getDerivedStateFromError(thrown) {
4209
+ return { error: (0, _copilotkit_shared_generative_ui.toError)(thrown) };
3913
4210
  }
3914
4211
  static getDerivedStateFromProps(props, state) {
3915
4212
  return props.revision !== state.revision ? {
@@ -3917,53 +4214,138 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3917
4214
  revision: props.revision
3918
4215
  } : null;
3919
4216
  }
3920
- componentDidCatch(error) {
3921
- const { runtime, surfaceId, componentId } = this.props;
3922
- (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, {
4217
+ componentDidCatch(thrown) {
4218
+ const { runtime, surfaceId, componentId, log, place } = this.props;
4219
+ log.clear(failureSlots.fallback(place));
4220
+ log.report(runtime, failureSlots.node(place), {
3923
4221
  kind: "render",
3924
4222
  surfaceId,
3925
4223
  componentId,
3926
- error
4224
+ error: this.state.error ?? (0, _copilotkit_shared_generative_ui.toError)(thrown)
4225
+ });
4226
+ }
4227
+ slots() {
4228
+ const { place } = this.props;
4229
+ return [
4230
+ failureSlots.node(place),
4231
+ failureSlots.appFallback(place),
4232
+ failureSlots.renderFallback(place)
4233
+ ];
4234
+ }
4235
+ clearSlots() {
4236
+ for (const slot of this.slots()) this.props.log.clear(slot);
4237
+ }
4238
+ componentDidMount() {
4239
+ for (const slot of this.slots()) this.props.log.retain(slot);
4240
+ if (!this.state.error) {
4241
+ this.clearSlots();
4242
+ return;
4243
+ }
4244
+ const { runtime, surfaceId, componentId, log, place } = this.props;
4245
+ log.report(runtime, failureSlots.node(place), {
4246
+ kind: "render",
4247
+ surfaceId,
4248
+ componentId,
4249
+ error: this.state.error
3927
4250
  });
3928
4251
  }
4252
+ componentDidUpdate(prevProps) {
4253
+ if (prevProps.place !== this.props.place) {
4254
+ const { log } = this.props;
4255
+ log.release(failureSlots.node(prevProps.place));
4256
+ log.release(failureSlots.appFallback(prevProps.place));
4257
+ log.release(failureSlots.renderFallback(prevProps.place));
4258
+ for (const slot of this.slots()) log.retain(slot);
4259
+ }
4260
+ if (!this.state.error) this.clearSlots();
4261
+ }
4262
+ componentWillUnmount() {
4263
+ for (const slot of this.slots()) this.props.log.release(slot);
4264
+ }
3929
4265
  render() {
3930
4266
  const { error } = this.state;
3931
4267
  if (!error) return this.props.children;
3932
- const { runtime, surfaceId, componentId } = this.props;
3933
- const fallback = runtime.errorFallback;
3934
- return fallback ? fallback({
3935
- error,
3936
- retry: this.retry,
4268
+ const { runtime, surfaceId, componentId, place, log } = this.props;
4269
+ const hostFallback = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
3937
4270
  surfaceId,
3938
- componentId
3939
- }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
4271
+ componentId,
4272
+ runtime,
3940
4273
  message: error.message,
4274
+ retry: this.retry,
4275
+ slot: failureSlots.renderFallback(place)
4276
+ });
4277
+ const fallback = runtime.errorFallback;
4278
+ if (!fallback) return hostFallback;
4279
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FallbackGuard, {
4280
+ surfaceId,
3941
4281
  componentId,
3942
- retry: this.retry
4282
+ runtime,
4283
+ log,
4284
+ slot: failureSlots.appFallback(place),
4285
+ context: error.message,
4286
+ resetKeys: [error],
4287
+ replacement: hostFallback,
4288
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AppErrorFallback, {
4289
+ render: fallback,
4290
+ error,
4291
+ retry: this.retry,
4292
+ surfaceId,
4293
+ componentId
4294
+ })
3943
4295
  });
3944
4296
  }
3945
4297
  };
3946
- /** Outer boundary: a failure here must not reach the chat or the host page. */
4298
+ /**
4299
+ * Surface boundary: a failure here must not reach the chat or the host page.
4300
+ * It resets on a new revision, and on Retry when `retryable`. A failure that
4301
+ * stays the same across revisions is reported once.
4302
+ */
3947
4303
  var SurfaceBoundary = class extends react.Component {
3948
- constructor(..._args2) {
3949
- super(..._args2);
3950
- this.state = { error: null };
4304
+ constructor(..._args3) {
4305
+ super(..._args3);
4306
+ this.state = {
4307
+ error: null,
4308
+ revision: this.props.revision
4309
+ };
4310
+ this.retry = () => this.setState({ error: null });
3951
4311
  }
3952
- static getDerivedStateFromError(error) {
3953
- return { error };
4312
+ static getDerivedStateFromError(thrown) {
4313
+ return { error: (0, _copilotkit_shared_generative_ui.toError)(thrown) };
4314
+ }
4315
+ static getDerivedStateFromProps(props, state) {
4316
+ return props.revision !== state.revision ? {
4317
+ error: null,
4318
+ revision: props.revision
4319
+ } : null;
3954
4320
  }
3955
- componentDidCatch(error) {
3956
- (0, _copilotkit_core_generative_ui.reportErrorSafely)(this.props.runtime, {
4321
+ componentDidCatch(thrown) {
4322
+ const { runtime, surfaceId, log, level } = this.props;
4323
+ log.report(runtime, failureSlots.surface(level), {
3957
4324
  kind: "render",
3958
- surfaceId: this.props.surfaceId,
3959
- error
4325
+ surfaceId,
4326
+ error: this.state.error ?? (0, _copilotkit_shared_generative_ui.toError)(thrown)
3960
4327
  });
3961
4328
  }
4329
+ clearSlots() {
4330
+ const { log, level } = this.props;
4331
+ log.clear(failureSlots.surface(level));
4332
+ log.clear(failureSlots.surfaceFallback(level));
4333
+ }
4334
+ componentDidMount() {
4335
+ if (!this.state.error) this.clearSlots();
4336
+ }
4337
+ componentDidUpdate() {
4338
+ if (!this.state.error) this.clearSlots();
4339
+ }
3962
4340
  render() {
3963
4341
  if (!this.state.error) return this.props.children;
3964
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
4342
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
4343
+ surfaceId: this.props.surfaceId,
4344
+ componentId: void 0,
4345
+ runtime: this.props.runtime,
3965
4346
  message: "this generated UI failed to render",
3966
- componentId: "surface"
4347
+ retry: this.props.retryable ? this.retry : void 0,
4348
+ slot: failureSlots.surfaceFallback(this.props.level)
3967
4349
  });
3968
4350
  }
3969
4351
  };
@@ -3971,7 +4353,6 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3971
4353
  //#endregion
3972
4354
  //#region src/v2/generative-ui/SurfaceView.tsx
3973
4355
  const NO_ANCESTORS = [];
3974
- const ROOT_STYLE = { display: "contents" };
3975
4356
  const undefinedSnapshot = () => void 0;
3976
4357
  const noopSubscribe = () => () => {};
3977
4358
  const zeroSnapshot = () => 0;
@@ -3981,23 +4362,80 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3981
4362
  }
3982
4363
  function SurfaceView({ surface }) {
3983
4364
  const runtime = useRequiredGenerativeUIRuntime();
3984
- const { theme } = runtime;
3985
- const sheet = (0, react.useMemo)(() => (0, _copilotkit_shared_generative_ui.themeStyleSheet)(theme), [theme]);
3986
- return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3987
- className: "a2ui-surface",
3988
- "data-generative-ui-root": surface.surfaceId,
3989
- "data-a2ui-theme": sheet?.id,
3990
- style: ROOT_STYLE,
3991
- children: [sheet ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("style", { dangerouslySetInnerHTML: { __html: sheet.css } }) : null, /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceBoundary, {
4365
+ const { SurfaceRoot } = useGenerativeUIHost();
4366
+ const appliedCount = (0, react.useSyncExternalStore)(runtime.manager.subscribe, () => surface.operations.length, undefinedSnapshot);
4367
+ const revision = (0, react.useMemo)(() => ({
4368
+ surface,
4369
+ appliedCount
4370
+ }), [surface, appliedCount]);
4371
+ const [scopedLog, setScopedLog] = (0, react.useState)(() => ({
4372
+ key: surface.key,
4373
+ log: createFailureLog()
4374
+ }));
4375
+ let failureLog = scopedLog.log;
4376
+ if (scopedLog.key !== surface.key) {
4377
+ const next = {
4378
+ key: surface.key,
4379
+ log: createFailureLog()
4380
+ };
4381
+ setScopedLog(next);
4382
+ failureLog = next.log;
4383
+ }
4384
+ const { threadId, surfaceId } = surface;
4385
+ const { manager } = runtime;
4386
+ const seen = (0, react.useRef)(void 0);
4387
+ (0, react.useLayoutEffect)(() => {
4388
+ const contentOf = () => {
4389
+ const current = manager.getSurface(threadId, surfaceId);
4390
+ return {
4391
+ log: failureLog,
4392
+ current,
4393
+ applied: current?.operations.length
4394
+ };
4395
+ };
4396
+ const observe = (missed) => {
4397
+ const last = seen.current;
4398
+ const next = contentOf();
4399
+ seen.current = next;
4400
+ if (last?.log === failureLog && (next.current !== last.current || next.applied !== last.applied)) failureLog.settle(missed);
4401
+ };
4402
+ observe(true);
4403
+ return manager.subscribe(() => observe(false));
4404
+ }, [
4405
+ manager,
4406
+ threadId,
4407
+ surfaceId,
4408
+ failureLog
4409
+ ]);
4410
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GenerativeUIFailureLogContext.Provider, {
4411
+ value: failureLog,
4412
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceBoundary, {
3992
4413
  surfaceId: surface.surfaceId,
3993
4414
  runtime,
3994
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
3995
- surface,
3996
- componentId: _copilotkit_shared.GENERATIVE_UI_ROOT_ID,
3997
- basePath: void 0,
3998
- ancestors: NO_ANCESTORS
4415
+ log: failureLog,
4416
+ level: "outer",
4417
+ revision,
4418
+ retryable: true,
4419
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceRoot, {
4420
+ surfaceId: surface.surfaceId,
4421
+ theme: runtime.theme,
4422
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceBoundary, {
4423
+ surfaceId: surface.surfaceId,
4424
+ runtime,
4425
+ log: failureLog,
4426
+ level: "inner",
4427
+ revision,
4428
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
4429
+ surface,
4430
+ componentId: _copilotkit_shared.GENERATIVE_UI_ROOT_ID,
4431
+ basePath: void 0,
4432
+ ancestors: NO_ANCESTORS,
4433
+ parentPlace: "",
4434
+ via: ""
4435
+ })
4436
+ })
3999
4437
  })
4000
- })]
4438
+ })
4001
4439
  });
4002
4440
  }
4003
4441
  /**
@@ -4022,27 +4460,63 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4022
4460
  * repeats both.
4023
4461
  */
4024
4462
  function scopedId(componentId, basePath) {
4025
- return `${basePath ?? ""}#${componentId}`;
4463
+ return `${basePath === void 0 ? "" : JSON.stringify(basePath)}#${JSON.stringify(componentId)}`;
4464
+ }
4465
+ /**
4466
+ * Where one rendered copy of a node is: the place of its parent, the slot
4467
+ * or child reference of the parent it renders in, and its scoped id. Names
4468
+ * the node's failure-log slots. A scoped id alone is not enough: the same
4469
+ * id can render under two parents, and a cyclic reference repeats the
4470
+ * scoped id of an ancestor. Each copy keeps its own memory of what it
4471
+ * reported, so one copy that renders, goes away or remounts does not clear
4472
+ * the memory of another that still fails.
4473
+ */
4474
+ function placeOf(parentPlace, via, position) {
4475
+ return `${parentPlace}/${via === "" ? "" : JSON.stringify(via)}${position}`;
4026
4476
  }
4027
- const NodeView = (0, react.memo)(function NodeView({ surface, componentId, basePath, ancestors }) {
4477
+ const NodeView = (0, react.memo)(function NodeView({ surface, componentId, basePath, ancestors, parentPlace, via }) {
4028
4478
  const runtime = useRequiredGenerativeUIRuntime();
4479
+ const log = useFailureLog();
4029
4480
  const component = useComponentModel(runtime.manager, surface.model, componentId);
4030
4481
  const properties = useComponentProperties(runtime.manager, component);
4031
- if (ancestors.includes(scopedId(componentId, basePath))) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
4482
+ const position = scopedId(componentId, basePath);
4483
+ const place = placeOf(parentPlace, via, position);
4484
+ (0, react.useEffect)(() => {
4485
+ const slots = [
4486
+ failureSlots.fallback(place),
4487
+ failureSlots.cycle(place),
4488
+ failureSlots.props(place)
4489
+ ];
4490
+ for (const slot of slots) log.retain(slot);
4491
+ return () => {
4492
+ for (const slot of slots) log.release(slot);
4493
+ };
4494
+ }, [log, place]);
4495
+ if (ancestors.includes(position)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
4496
+ surfaceId: surface.surfaceId,
4497
+ componentId,
4498
+ runtime,
4032
4499
  message: `cyclic reference to ${componentId}`,
4033
- componentId
4500
+ slot: failureSlots.cycle(place),
4501
+ revision: properties
4034
4502
  });
4035
4503
  if (!component || !properties) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodePlaceholder, { componentId });
4036
4504
  const definition = runtime.ui.components.get(component.type);
4037
- if (!definition) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
4505
+ if (!definition) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
4506
+ surfaceId: surface.surfaceId,
4507
+ componentId,
4508
+ runtime,
4038
4509
  message: `unknown component "${component.type}"`,
4039
- componentId
4510
+ slot: failureSlots.fallback(place),
4511
+ revision: properties
4040
4512
  });
4041
4513
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeBoundary, {
4042
4514
  surfaceId: surface.surfaceId,
4043
4515
  componentId,
4516
+ place,
4044
4517
  revision: properties,
4045
4518
  runtime,
4519
+ log,
4046
4520
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ResolvedNode, {
4047
4521
  surface,
4048
4522
  componentId,
@@ -4050,7 +4524,8 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4050
4524
  properties,
4051
4525
  runtime,
4052
4526
  basePath,
4053
- ancestors
4527
+ ancestors,
4528
+ place
4054
4529
  })
4055
4530
  }, component.type);
4056
4531
  });
@@ -4058,7 +4533,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4058
4533
  * Renders the component of a template once for each item of the list at the
4059
4534
  * template path, keyed by index. Renders nothing when there is no list.
4060
4535
  */
4061
- function TemplateChildren({ surface, template, basePath, ancestors }) {
4536
+ function TemplateChildren({ surface, template, basePath, ancestors, parentPlace, via }) {
4062
4537
  const dataModel = surface.model.dataModel;
4063
4538
  const listPath = (0, _copilotkit_core_generative_ui.resolvePath)(template.path, basePath);
4064
4539
  const subscribe = (0, react.useCallback)((onChange) => {
@@ -4074,17 +4549,25 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4074
4549
  surface,
4075
4550
  componentId: template.componentId,
4076
4551
  basePath: (0, _copilotkit_core_generative_ui.joinPath)(listPath, String(index)),
4077
- ancestors
4552
+ ancestors,
4553
+ parentPlace,
4554
+ via
4078
4555
  }, index)) });
4079
4556
  }
4080
4557
  /**
4081
4558
  * The props the model sets, with their dynamic values resolved from the
4082
4559
  * surface data model: `{"@path"}` bindings, `{"@call"}` function values and
4083
- * `{"@call":"@index"}` in a template, at any depth. A literal writable prop
4084
- * that the component set shows the local value from `overrides`. Bound
4085
- * props, events and slots are left out, and so are the keys of the A2UI
4086
- * component envelope; `accessibility` is resolved the same way and given
4087
- * on its own.
4560
+ * `{"@call":"@index"}` in a template, at any depth. Bound props, events and
4561
+ * slots are left out, and so are the keys of the A2UI component envelope;
4562
+ * `accessibility` is resolved the same way and given on its own.
4563
+ *
4564
+ * A writable prop that is not bound to a path and that the component set
4565
+ * shows the local value from `overrides`, also when the model never set the
4566
+ * prop. That is intended: the user's edit wins over the model's literal
4567
+ * value, also over a newer literal the agent sends later, until the node
4568
+ * remounts. A literal is not data the agent and the user share; a prop
4569
+ * that both must write is bound to a path, and the setter writes the data
4570
+ * model.
4088
4571
  *
4089
4572
  * A prop is resolved again only when its raw value or a data model value it
4090
4573
  * read has changed, so the result keeps its identity otherwise. A function
@@ -4095,7 +4578,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4095
4578
  const units = (0, react.useMemo)(() => {
4096
4579
  const list = [];
4097
4580
  for (const key of Object.keys(properties)) {
4098
- if (definition.events?.[key] || definition.slots?.[key] || Object.hasOwn(definition.selectors, key) || _copilotkit_shared.A2UI_COMPONENT_ENVELOPE_KEYS.has(key)) continue;
4581
+ if (definition.events?.[key] || definition.slots?.[key] || (0, _copilotkit_shared_generative_ui.hasOwnKey)(definition.selectors, key) || _copilotkit_shared.A2UI_COMPONENT_ENVELOPE_KEYS.has(key)) continue;
4099
4582
  list.push({
4100
4583
  key,
4101
4584
  raw: properties[key]
@@ -4196,9 +4679,10 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4196
4679
  for (const [key, selector] of selectors) try {
4197
4680
  values[key] = selector(state, modelProps);
4198
4681
  } catch (error) {
4682
+ const { message } = (0, _copilotkit_shared_generative_ui.toError)(error);
4199
4683
  return {
4200
4684
  ok: false,
4201
- problem: `${key}: selector threw: ${error instanceof Error ? error.message : String(error)}`
4685
+ problem: `${key}: selector threw: ${message}`
4202
4686
  };
4203
4687
  }
4204
4688
  return {
@@ -4209,8 +4693,10 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4209
4693
  /**
4210
4694
  * The values of the bound props of a node, read from the app state.
4211
4695
  *
4212
- * The selectors run again only when the state object or the parsed model
4213
- * props change. When the new values are shallow-equal to the last ones, the
4696
+ * The selectors get the checked model props: the resolved values that
4697
+ * passed the props schema (without the nulls it rejected), not the
4698
+ * schema's parsed output, so its defaults and transforms are not applied
4699
+ * yet. They run again only when the state object or those props change. When the new values are shallow-equal to the last ones, the
4214
4700
  * last snapshot is kept. So the node re-renders only when a selected value
4215
4701
  * changes. A selector that returns a new array on each call cannot cause a
4216
4702
  * render loop: with the same inputs, the cached snapshot comes back.
@@ -4240,7 +4726,10 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4240
4726
  * Checks the model-set props with the props schema before the selectors run.
4241
4727
  * The bound props are not set yet, so their issues do not count. Neither do
4242
4728
  * issues with no path (a check over several props), which the full check
4243
- * after the selectors reports.
4729
+ * after the selectors reports. The exception is the issue that an async
4730
+ * props schema gives (`isAsyncIssue`): the render checks props
4731
+ * synchronously only, so the full check would fail the same way, and it is
4732
+ * reported here.
4244
4733
  */
4245
4734
  function checkModelProps(definition, modelData) {
4246
4735
  if (!definition.props) return {
@@ -4259,7 +4748,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4259
4748
  if (!result.success) {
4260
4749
  const issues = result.issues.filter((issue) => {
4261
4750
  const key = (0, _copilotkit_shared_generative_ui.issueKey)(issue);
4262
- return key === void 0 ? (0, _copilotkit_shared_generative_ui.isAsyncIssue)(issue) : !Object.hasOwn(definition.selectors, key);
4751
+ return key === void 0 ? (0, _copilotkit_shared_generative_ui.isAsyncIssue)(issue) : !(0, _copilotkit_shared_generative_ui.hasOwnKey)(definition.selectors, key);
4263
4752
  });
4264
4753
  if (issues.length > 0) return {
4265
4754
  ok: false,
@@ -4322,7 +4811,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4322
4811
  function useEventProps(runtime, surface, componentId, definition, properties, basePath) {
4323
4812
  const latest = (0, react.useRef)(properties);
4324
4813
  latest.current = properties;
4325
- const boundNames = Object.keys(definition.events ?? {}).filter((name) => Object.hasOwn(definition.eventFunctions, name) || (0, _copilotkit_shared.isActionBinding)(properties[name])).join("|");
4814
+ const boundNames = Object.keys(definition.events ?? {}).filter((name) => (0, _copilotkit_shared_generative_ui.hasOwnKey)(definition.eventFunctions, name) || (0, _copilotkit_shared.isActionBinding)(properties[name])).join("|");
4326
4815
  return (0, react.useMemo)(() => {
4327
4816
  const handlers = {};
4328
4817
  const names = boundNames ? boundNames.split("|") : [];
@@ -4334,7 +4823,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4334
4823
  name,
4335
4824
  parameters: definition.events?.[name]?.parameters
4336
4825
  };
4337
- const functionName = Object.hasOwn(definition.eventFunctions, name) ? definition.eventFunctions[name] : void 0;
4826
+ const functionName = (0, _copilotkit_shared_generative_ui.hasOwnKey)(definition.eventFunctions, name) ? definition.eventFunctions[name] : void 0;
4338
4827
  handlers[name] = functionName === void 0 ? (args) => (0, _copilotkit_core_generative_ui.invokeBoundEvent)({
4339
4828
  ...call,
4340
4829
  binding: latest.current[name],
@@ -4360,7 +4849,9 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4360
4849
  * The setter props of a node: `setValue` for the writable prop `value`. For
4361
4850
  * a prop the model bound to `{"@path"}`, the setter writes the data model
4362
4851
  * and makes the path user-owned. For a literal prop, it sets `overrides`, a
4363
- * local value of this mounted node. The setters keep their identity.
4852
+ * local value of this mounted node that wins over the model's value until
4853
+ * the node remounts (see `useModelDataProps`). The setters keep their
4854
+ * identity.
4364
4855
  */
4365
4856
  function useSetterProps(runtime, surface, componentId, definition, properties, basePath) {
4366
4857
  const [overrides, setOverrides] = (0, react.useState)({});
@@ -4383,7 +4874,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4383
4874
  try {
4384
4875
  runtime.manager.setOwnedValue(surface.key, (0, _copilotkit_core_generative_ui.resolvePath)(binding["@path"], basePath), value);
4385
4876
  } catch (cause) {
4386
- const message = cause instanceof Error ? cause.message : String(cause);
4877
+ const { message } = (0, _copilotkit_shared_generative_ui.toError)(cause);
4387
4878
  (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, {
4388
4879
  kind: "props",
4389
4880
  surfaceId: surface.surfaceId,
@@ -4402,29 +4893,36 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4402
4893
  ])
4403
4894
  };
4404
4895
  }
4405
- function useSlotProps(surface, definition, properties, componentId, basePath, ancestors) {
4896
+ function useSlotProps(surface, definition, properties, componentId, basePath, ancestors, place) {
4406
4897
  return (0, react.useMemo)(() => {
4407
4898
  const childAncestors = [...ancestors, scopedId(componentId, basePath)];
4408
4899
  const slots = {};
4409
4900
  for (const [name, kind] of Object.entries(definition.slots ?? {})) {
4410
4901
  const value = properties[name];
4902
+ const via = `${name}:`;
4411
4903
  if (kind === "single" && typeof value === "string") slots[name] = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
4412
4904
  surface,
4413
4905
  componentId: value,
4414
4906
  basePath,
4415
- ancestors: childAncestors
4907
+ ancestors: childAncestors,
4908
+ parentPlace: place,
4909
+ via
4416
4910
  });
4417
4911
  else if (kind === "multiple" && isTemplateChildren(value)) slots[name] = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TemplateChildren, {
4418
4912
  surface,
4419
4913
  template: value,
4420
4914
  basePath,
4421
- ancestors: childAncestors
4915
+ ancestors: childAncestors,
4916
+ parentPlace: place,
4917
+ via
4422
4918
  });
4423
4919
  else if (kind === "multiple" && Array.isArray(value)) slots[name] = [...new Set(value.filter((id) => typeof id === "string"))].map((id) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
4424
4920
  surface,
4425
4921
  componentId: id,
4426
4922
  basePath,
4427
- ancestors: childAncestors
4923
+ ancestors: childAncestors,
4924
+ parentPlace: place,
4925
+ via
4428
4926
  }, id));
4429
4927
  }
4430
4928
  return slots;
@@ -4434,7 +4932,8 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4434
4932
  properties,
4435
4933
  componentId,
4436
4934
  basePath,
4437
- ancestors
4935
+ ancestors,
4936
+ place
4438
4937
  ]);
4439
4938
  }
4440
4939
  /**
@@ -4442,33 +4941,43 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4442
4941
  * `childList()`, at any depth) replaced by the rendered component. Without
4443
4942
  * references, the parsed props come back as they are.
4444
4943
  */
4445
- function useChildRefProps(surface, refs, props, componentId, basePath, ancestors) {
4944
+ function useChildRefProps(surface, refs, props, componentId, basePath, ancestors, place) {
4446
4945
  return (0, react.useMemo)(() => {
4447
4946
  if (props === void 0 || refs.length === 0) return props;
4448
4947
  const childAncestors = [...ancestors, scopedId(componentId, basePath)];
4449
- const render = (kind, value) => {
4948
+ const render = (kind, value, via) => {
4450
4949
  if (kind === "single") return typeof value === "string" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
4451
4950
  surface,
4452
4951
  componentId: value,
4453
4952
  basePath,
4454
- ancestors: childAncestors
4953
+ ancestors: childAncestors,
4954
+ parentPlace: place,
4955
+ via
4455
4956
  }) : void 0;
4456
4957
  if (isTemplateChildren(value)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TemplateChildren, {
4457
4958
  surface,
4458
4959
  template: value,
4459
4960
  basePath,
4460
- ancestors: childAncestors
4961
+ ancestors: childAncestors,
4962
+ parentPlace: place,
4963
+ via
4461
4964
  });
4462
4965
  if (!Array.isArray(value)) return void 0;
4463
4966
  return [...new Set(value.filter((id) => typeof id === "string"))].map((id) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
4464
4967
  surface,
4465
4968
  componentId: id,
4466
4969
  basePath,
4467
- ancestors: childAncestors
4970
+ ancestors: childAncestors,
4971
+ parentPlace: place,
4972
+ via
4468
4973
  }, id));
4469
4974
  };
4470
4975
  let result = props;
4471
- for (const ref of refs) result = (0, _copilotkit_shared.mapAtChildRef)(result, ref.path, (found) => render(ref.kind, found));
4976
+ for (const ref of refs) {
4977
+ let found = 0;
4978
+ const path = ref.path.join(".");
4979
+ result = (0, _copilotkit_shared.mapAtChildRef)(result, ref.path, (value) => render(ref.kind, value, `${path}[${found++}]:`));
4980
+ }
4472
4981
  return result;
4473
4982
  }, [
4474
4983
  surface,
@@ -4476,15 +4985,17 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4476
4985
  props,
4477
4986
  componentId,
4478
4987
  basePath,
4479
- ancestors
4988
+ ancestors,
4989
+ place
4480
4990
  ]);
4481
4991
  }
4482
- function ResolvedNode({ surface, componentId, definition, properties, runtime, basePath, ancestors }) {
4992
+ function ResolvedNode({ surface, componentId, definition, properties, runtime, basePath, ancestors, place }) {
4993
+ const { AccessibilityWrapper } = useGenerativeUIHost();
4483
4994
  const { overrides, setters } = useSetterProps(runtime, surface, componentId, definition, properties, basePath);
4484
4995
  const dataProps = useDataProps(runtime, surface, definition, properties, basePath, overrides);
4485
4996
  const eventProps = useEventProps(runtime, surface, componentId, definition, properties, basePath);
4486
- const slotProps = useSlotProps(surface, definition, properties, componentId, basePath, ancestors);
4487
- const childRefProps = useChildRefProps(surface, definition.childRefs, dataProps.ok ? dataProps.props : void 0, componentId, basePath, ancestors);
4997
+ const slotProps = useSlotProps(surface, definition, properties, componentId, basePath, ancestors, place);
4998
+ const childRefProps = useChildRefProps(surface, definition.childRefs, dataProps.ok ? dataProps.props : void 0, componentId, basePath, ancestors, place);
4488
4999
  const accessibility = dataProps.ok ? dataProps.accessibility : void 0;
4489
5000
  const nodeContext = (0, react.useMemo)(() => ({
4490
5001
  surfaceKey: surface.key,
@@ -4498,8 +5009,19 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4498
5009
  accessibility
4499
5010
  ]);
4500
5011
  const problem = dataProps.ok ? void 0 : dataProps.problem;
5012
+ const log = useFailureLog();
5013
+ (0, react.useEffect)(() => {
5014
+ if (!problem) {
5015
+ log.clear(failureSlots.fallback(place));
5016
+ log.clear(failureSlots.props(place));
5017
+ }
5018
+ }, [
5019
+ problem,
5020
+ log,
5021
+ place
5022
+ ]);
4501
5023
  (0, react.useEffect)(() => {
4502
- if (problem) (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, {
5024
+ if (problem) log.report(runtime, failureSlots.props(place), {
4503
5025
  kind: "props",
4504
5026
  surfaceId: surface.surfaceId,
4505
5027
  componentId,
@@ -4507,13 +5029,19 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4507
5029
  });
4508
5030
  }, [
4509
5031
  problem,
5032
+ log,
5033
+ place,
4510
5034
  runtime,
4511
5035
  surface.surfaceId,
4512
5036
  componentId
4513
5037
  ]);
4514
- if (!dataProps.ok) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
5038
+ if (!dataProps.ok) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
5039
+ surfaceId: surface.surfaceId,
5040
+ componentId,
5041
+ runtime,
4515
5042
  message: `invalid props (${problem})`,
4516
- componentId
5043
+ slot: failureSlots.fallback(place),
5044
+ revision: properties
4517
5045
  });
4518
5046
  const Component = definition.component;
4519
5047
  const rendered = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, {
@@ -4525,10 +5053,8 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4525
5053
  const hasEnvelope = properties.accessibility !== void 0 && definition.accessibility !== "self";
4526
5054
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GenerativeUINodeContext.Provider, {
4527
5055
  value: nodeContext,
4528
- children: hasEnvelope ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
4529
- "data-a2ui-accessibility": "",
4530
- style: ROOT_STYLE,
4531
- ...(0, _copilotkit_core_generative_ui.wrapperAria)((0, _copilotkit_core_generative_ui.accessibilityAttributes)(accessibility)),
5056
+ children: hasEnvelope ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AccessibilityWrapper, {
5057
+ attributes: (0, _copilotkit_core_generative_ui.wrapperAria)((0, _copilotkit_core_generative_ui.accessibilityAttributes)(accessibility)),
4532
5058
  children: rendered
4533
5059
  }) : rendered
4534
5060
  });
@@ -4548,39 +5074,18 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4548
5074
  })]
4549
5075
  });
4550
5076
  }
4551
- const ContentSchema = zod.z.object({
4552
- surfaceId: zod.z.string(),
4553
- target: zod.z.string(),
4554
- catalogId: zod.z.string(),
4555
- operations: zod.z.array(zod.z.any()),
4556
- status: zod.z.enum([
4557
- "streaming",
4558
- "ready",
4559
- "error"
4560
- ]),
4561
- rejected: zod.z.array(zod.z.object({
4562
- index: zod.z.number(),
4563
- reason: zod.z.string()
4564
- })).default([])
4565
- }).passthrough();
4566
5077
  function GenerativeUIChatActivity({ content, agent }) {
4567
5078
  const runtime = useGenerativeUIRuntime();
4568
5079
  const threadId = useCopilotChatConfiguration()?.threadId ?? agent?.threadId;
4569
5080
  const Wrapper = (0, react.useContext)(GenerativeUIChatSlotContext) ?? DefaultGenerativeUIChatCard;
4570
- useManagerVersion(runtime);
4571
- (0, react.useLayoutEffect)(() => {
4572
- if (runtime && threadId) runtime.manager.ingest(threadId, agent?.agentId, content);
4573
- }, [
5081
+ const { surface, rejected, error } = useGenerativeUIActivitySurface({
4574
5082
  runtime,
4575
5083
  threadId,
4576
- agent?.agentId,
4577
- content
4578
- ]);
4579
- if (!runtime || !threadId || content.target !== _copilotkit_shared.GENERATIVE_UI_CHAT_TARGET) return null;
4580
- const surface = runtime.manager.getSurface(threadId, content.surfaceId) ?? runtime.manager.getFailedSurface(threadId, content.surfaceId);
5084
+ agentId: agent?.agentId,
5085
+ content,
5086
+ target: _copilotkit_shared.GENERATIVE_UI_CHAT_TARGET
5087
+ });
4581
5088
  if (!surface) return null;
4582
- const rejected = [...content.rejected ?? [], ...surface.clientRejected];
4583
- const error = rejected.length ? new Error(`${rejected.length} UI update(s) could not be shown`, { cause: rejected }) : void 0;
4584
5089
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Wrapper, {
4585
5090
  surfaceId: content.surfaceId,
4586
5091
  isStreaming: content.status === "streaming",
@@ -4592,7 +5097,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4592
5097
  function createGenerativeUIActivityRenderer() {
4593
5098
  return {
4594
5099
  activityType: _copilotkit_shared.GENERATIVE_UI_ACTIVITY_TYPE,
4595
- content: ContentSchema,
5100
+ content: _copilotkit_core_generative_ui.GenerativeUISurfaceContentSchema,
4596
5101
  render: GenerativeUIChatActivity
4597
5102
  };
4598
5103
  }