@copilotkit/react-core 1.85.0 → 1.87.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 (69) 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-Dn4PbHJV.d.mts → copilotkit-BNPEXnEE.d.mts} +11 -6
  10. package/dist/{copilotkit-Dn4PbHJV.d.mts.map → copilotkit-BNPEXnEE.d.mts.map} +1 -1
  11. package/dist/{copilotkit-CuTAoQF2.d.cts → copilotkit-Bop-sY6y.d.cts} +9 -4
  12. package/dist/{copilotkit-CuTAoQF2.d.cts.map → copilotkit-Bop-sY6y.d.cts.map} +1 -1
  13. package/dist/{copilotkit-BpKHgURs.cjs → copilotkit-CZ9tI_J1.cjs} +203 -452
  14. package/dist/copilotkit-CZ9tI_J1.cjs.map +1 -0
  15. package/dist/{copilotkit-D6mTCuNU.mjs → copilotkit-ChqQa52S.mjs} +131 -380
  16. package/dist/{copilotkit-BpKHgURs.cjs.map → copilotkit-ChqQa52S.mjs.map} +1 -1
  17. package/dist/hooks-CEFWIeKR.d.cts +89 -0
  18. package/dist/hooks-CEFWIeKR.d.cts.map +1 -0
  19. package/dist/hooks-DFgYRjj3.d.mts +89 -0
  20. package/dist/hooks-DFgYRjj3.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 +769 -165
  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 +378 -457
  51. package/dist/v2/generative-ui.d.cts.map +1 -1
  52. package/dist/v2/generative-ui.d.mts +378 -457
  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 +769 -165
  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-D6mTCuNU.mjs.map +0 -1
  68. package/dist/runtime-context-DrVNUYLi.d.cts.map +0 -1
  69. package/dist/runtime-context-Er5UIbZA.d.mts.map +0 -1
package/dist/index.umd.js CHANGED
@@ -625,7 +625,7 @@ react_markdown = __toESM(react_markdown);
625
625
 
626
626
  //#endregion
627
627
  //#region package.json
628
- var version = "1.85.0";
628
+ var version = "1.87.0";
629
629
 
630
630
  //#endregion
631
631
  //#region src/v2/components/CopilotKitInspector.tsx
@@ -1821,7 +1821,7 @@ react_markdown = __toESM(react_markdown);
1821
1821
  function warnAboutUnpaintedSurfaces(grouped) {
1822
1822
  for (const [surfaceId, operations] of grouped) {
1823
1823
  if (surfaceHasRenderableContent(operations)) continue;
1824
- const cause = operations.filter((o) => o?.updateComponents).length === 0 ? "no updateComponents operation arrived, so the surface was never given anything to draw" : "its components address their values by \"path\" and no updateDataModel carried a non-empty value, so every bound component drew empty";
1824
+ const cause = operations.flatMap(getOperationComponents).length === 0 ? "no updateComponents operation arrived, so the surface was never given anything to draw" : "its components address their values by \"path\" and no updateDataModel carried a non-empty value, so every bound component drew empty";
1825
1825
  console.warn(`[CopilotKit] A2UI surface "${surfaceId}" received operations and never painted after ${String(PAINT_FALLBACK_MS)}ms: ${cause}. Operations received: ${describeOperationKinds(operations)}. The payload was accepted, so check what the agent sent rather than the client wiring. This warning is development-only.`);
1826
1826
  }
1827
1827
  }
@@ -1834,13 +1834,9 @@ react_markdown = __toESM(react_markdown);
1834
1834
  */
1835
1835
  function describeComponentIds(operations) {
1836
1836
  const ids = [];
1837
- for (const operation of operations) {
1838
- const components = operation?.updateComponents?.components;
1839
- if (!Array.isArray(components)) continue;
1840
- for (const component of components) {
1841
- const id = component?.id;
1842
- if (typeof id === "string" && !ids.includes(id)) ids.push(id);
1843
- }
1837
+ for (const operation of operations) for (const component of getOperationComponents(operation)) {
1838
+ const id = component?.id;
1839
+ if (typeof id === "string" && !ids.includes(id)) ids.push(id);
1844
1840
  }
1845
1841
  return ids.length === 0 ? "none" : ids.map((id) => `"${id}"`).join(", ");
1846
1842
  }
@@ -1867,9 +1863,9 @@ react_markdown = __toESM(react_markdown);
1867
1863
  */
1868
1864
  function warnAboutUnresolvedRoot(surfaceId, operations, surface) {
1869
1865
  if (surface?.componentsModel?.get?.(_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID)) return;
1870
- const componentOps = operations.filter((o) => o?.updateComponents);
1871
- if (componentOps.length === 0) return;
1872
- const cause = componentOps.some((o) => o.updateComponents?.components?.some?.((c) => c?.id === _copilotkit_a2ui_renderer.ROOT_COMPONENT_ID)) ? `a component with id "${_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID}" WAS sent, so the components did not reach the surface's model — check for an A2UI render error above, or a catalog that took none of them` : `the components it received are named ${describeComponentIds(operations)}, and none of them is "${_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID}" — rename the entry-point component to "${_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID}", and make every other component reachable from it through child/children`;
1866
+ const components = operations.flatMap(getOperationComponents);
1867
+ if (components.length === 0) return;
1868
+ const cause = components.some((c) => c?.id === _copilotkit_a2ui_renderer.ROOT_COMPONENT_ID) ? `a component with id "${_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID}" WAS sent, so the components did not reach the surface's model — check for an A2UI render error above, or a catalog that took none of them` : `the components it received are named ${describeComponentIds(operations)}, and none of them is "${_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID}" — rename the entry-point component to "${_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID}", and make every other component reachable from it through child/children`;
1873
1869
  console.warn(`[CopilotKit] A2UI surface "${surfaceId}" has no "${_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID}" component ${String(PAINT_FALLBACK_MS)}ms after its last operations were processed, so it is showing the placeholder for a component that has not arrived yet and will keep showing it: ${cause}. Operations received: ${describeOperationKinds(operations)}. This warning is development-only.`);
1874
1870
  }
1875
1871
  function createA2UIMessageRenderer(options) {
@@ -2035,7 +2031,10 @@ react_markdown = __toESM(react_markdown);
2035
2031
  surfaceId,
2036
2032
  operations,
2037
2033
  onReady
2038
- }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(A2UISurfaceOrError, { surfaceId })]
2034
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(A2UISurfaceOrError, {
2035
+ surfaceId,
2036
+ onReady
2037
+ })]
2039
2038
  })
2040
2039
  });
2041
2040
  }
@@ -2043,15 +2042,60 @@ react_markdown = __toESM(react_markdown);
2043
2042
  * Renders the A2UI surface, or an error message if processing failed.
2044
2043
  * Must be a child of A2UIProvider to access the error state.
2045
2044
  */
2046
- function A2UISurfaceOrError({ surfaceId }) {
2045
+ function A2UISurfaceOrError({ surfaceId, onReady }) {
2047
2046
  const error = (0, _copilotkit_a2ui_renderer.useA2UIError)();
2048
- if (error) return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2047
+ (0, react.useEffect)(() => {
2048
+ if (error) onReady?.();
2049
+ }, [error, onReady]);
2050
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [error && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2051
+ role: "alert",
2049
2052
  className: "cpk:rounded-lg cpk:border cpk:border-red-200 cpk:bg-red-50 cpk:p-3 cpk:text-sm cpk:text-red-700",
2050
2053
  children: ["A2UI render error: ", error]
2051
- });
2052
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_copilotkit_a2ui_renderer.A2UIRenderer, {
2054
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_copilotkit_a2ui_renderer.A2UIRenderer, {
2053
2055
  surfaceId,
2054
2056
  className: "cpk:flex cpk:flex-1"
2057
+ })] });
2058
+ }
2059
+ /**
2060
+ * Full activity snapshots repeat creation. Native 1.0 can carry the current
2061
+ * components and data inline there, so replay changed initialization data and
2062
+ * components as updates for an existing surface. Compare against the previous
2063
+ * wire snapshot, rather than the SDK's mutable data model, to retain local edits.
2064
+ * Retain invalid or version-changing creates for the
2065
+ * provider/SDK to reject, instead of sanitizing them while replaying.
2066
+ */
2067
+ function replaySnapshotOperations(operations, existing, previousOperations) {
2068
+ if (!existing) return operations;
2069
+ let nativeSurfaceDeleted = false;
2070
+ return operations.flatMap((operation) => {
2071
+ if (operation?.version === "1.0" && operation.deleteSurface?.surfaceId === existing.id) nativeSurfaceDeleted = true;
2072
+ if (nativeSurfaceDeleted) return [operation];
2073
+ if (!operation?.createSurface) return [operation];
2074
+ const version = String(operation.version ?? "v0.9").replace(/^v/, "");
2075
+ if (version !== String(existing.protocolVersion).replace(/^v/, "")) return [operation];
2076
+ if (version === "0.9") return [];
2077
+ if (operation.version !== "1.0") return [operation];
2078
+ const validation = _copilotkit_a2ui_renderer.CreateSurfaceMessageSchemaV1.safeParse(operation);
2079
+ if (!validation.success) return [operation];
2080
+ const create = validation.data.createSurface;
2081
+ if (create.catalogId !== void 0 && create.catalogId !== existing.catalog?.id) return [operation];
2082
+ const updates = [];
2083
+ const previousCreate = previousOperations.find((previous) => previous?.version === "1.0" && previous.createSurface?.surfaceId === create.surfaceId)?.createSurface;
2084
+ if (Object.hasOwn(create, "dataModel") && (!previousCreate || !Object.hasOwn(previousCreate, "dataModel") || JSON.stringify(create.dataModel) !== JSON.stringify(previousCreate.dataModel))) updates.push({
2085
+ version: "1.0",
2086
+ updateDataModel: {
2087
+ surfaceId: create.surfaceId,
2088
+ value: create.dataModel
2089
+ }
2090
+ });
2091
+ if (Object.hasOwn(create, "components")) updates.push({
2092
+ version: "1.0",
2093
+ updateComponents: {
2094
+ surfaceId: create.surfaceId,
2095
+ components: create.components
2096
+ }
2097
+ });
2098
+ return updates;
2055
2099
  });
2056
2100
  }
2057
2101
  /**
@@ -2064,10 +2108,11 @@ react_markdown = __toESM(react_markdown);
2064
2108
  (0, react.useEffect)(() => {
2065
2109
  const hash = JSON.stringify(operations);
2066
2110
  if (hash === lastHashRef.current) return;
2111
+ const previousOperations = lastHashRef.current ? JSON.parse(lastHashRef.current) : [];
2067
2112
  lastHashRef.current = hash;
2068
- const ops = getSurface(surfaceId) ? operations.filter((op) => !op?.createSurface) : operations;
2113
+ const ops = replaySnapshotOperations(operations, getSurface(surfaceId), previousOperations);
2069
2114
  processMessages(ops.map(stripRoutingSurfaceId));
2070
- if (onReady && surfaceHasRenderableContent(operations)) onReady();
2115
+ if (onReady && getSurface(surfaceId)?.componentsModel.get(_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID) && surfaceHasRenderableContent(operations)) onReady();
2071
2116
  if (!IS_DEVELOPMENT) return;
2072
2117
  if (!getSurface(surfaceId)) {
2073
2118
  const missingSurfaceCheck = setTimeout(() => {
@@ -2097,15 +2142,26 @@ react_markdown = __toESM(react_markdown);
2097
2142
  * arrival rather than a fixed delay. (OSS-162)
2098
2143
  */
2099
2144
  function surfaceHasRenderableContent(operations) {
2100
- const componentOps = operations.filter((o) => o?.updateComponents);
2101
- if (!componentOps.length) return false;
2102
- if (!JSON.stringify(componentOps).includes("\"path\"")) return true;
2145
+ const components = operations.flatMap(getOperationComponents);
2146
+ if (!components.length) return false;
2147
+ const serialized = JSON.stringify(components);
2148
+ if (!(serialized.includes("\"path\"") || serialized.includes("\"@path\""))) return true;
2103
2149
  return operations.some((o) => {
2104
- const v = o?.updateDataModel?.value;
2150
+ const v = o?.updateDataModel?.value ?? (o?.version === "1.0" ? o?.createSurface?.dataModel : void 0);
2151
+ if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
2152
+ const path = o?.updateDataModel?.path ?? "/";
2153
+ const key = o?.version === "1.0" ? "@path" : "path";
2154
+ return v !== "" && serialized.includes(JSON.stringify({ [key]: path }));
2155
+ }
2105
2156
  if (!v || typeof v !== "object") return false;
2106
2157
  return Object.values(v).some((x) => Array.isArray(x) ? x.length > 0 : x !== null && x !== void 0 && x !== "");
2107
2158
  });
2108
2159
  }
2160
+ /** Component definitions can arrive in an update or a native inline create. */
2161
+ function getOperationComponents(operation) {
2162
+ const components = operation?.updateComponents?.components ?? (operation?.version === "1.0" ? operation?.createSurface?.components : void 0);
2163
+ return Array.isArray(components) ? components : [];
2164
+ }
2109
2165
  const V0_9_ACTION_KEYS = [
2110
2166
  "createSurface",
2111
2167
  "updateComponents",
@@ -2128,7 +2184,7 @@ react_markdown = __toESM(react_markdown);
2128
2184
  * message, otherwise the operation unchanged.
2129
2185
  */
2130
2186
  function stripRoutingSurfaceId(operation) {
2131
- if (!operation || typeof operation !== "object" || !("surfaceId" in operation) || !V0_9_ACTION_KEYS.some((key) => key in operation)) return operation;
2187
+ if (!operation || typeof operation !== "object" || !(operation.version === void 0 || operation.version === "v0.9") || !("surfaceId" in operation) || !V0_9_ACTION_KEYS.some((key) => key in operation)) return operation;
2132
2188
  const { surfaceId: _routingId, ...message } = operation;
2133
2189
  return message;
2134
2190
  }
@@ -2201,6 +2257,47 @@ react_markdown = __toESM(react_markdown);
2201
2257
  return null;
2202
2258
  }
2203
2259
 
2260
+ //#endregion
2261
+ //#region src/v2/a2ui/generation-guidelines.ts
2262
+ /** Native 1.0 tool guidance. Structural template paths retain their own syntax. */
2263
+ const A2UI_V1_GENERATION_GUIDELINES = `Generate A2UI 1.0 JSON.
2264
+
2265
+ Call render_a2ui with a unique surfaceId, the required components array, and optional
2266
+ root data object. Use only component names, properties and functions declared in
2267
+ the supplied catalog. Match its protocolVersion, catalogId and schemas.
2268
+
2269
+ Every component has a unique id and a component type. Exactly one component has
2270
+ id="root", the surface entry point. All other components must be reachable from
2271
+ root through child/children; never reference the same component as its own child,
2272
+ and never create a cycle.
2273
+
2274
+ Use literal values unless the property's catalog schema permits a dynamic value.
2275
+ Native value binding uses { "@path": "/form/name" }. A plain property schema does
2276
+ not accept a binding object. Pre-fill editable values via the data argument:
2277
+ "data": { "form": { "name": "Alice" } }
2278
+ "value": { "@path": "/form/name" }
2279
+
2280
+ Native function expressions use { "@call": "functionName", "args": { ... } },
2281
+ with only the parameters declared by the catalog; do not add returnType.
2282
+
2283
+ REPEATING CONTENT uses a structural children format, distinct from value binding:
2284
+ children: { componentId: "card-id", path: "/items" }
2285
+ The structural key remains path. Initialize its data array with real items before
2286
+ expecting a visible list. Components inside a template use relative value bindings:
2287
+ "text": { "@path": "name" }
2288
+ An absolute binding such as { "@path": "/name" } reads from the root instead.
2289
+
2290
+ Buttons send nested event actions, with bound context to capture edited values:
2291
+ "action": { "event": { "name": "submit", "context": { "name": { "@path": "/form/name" } } } }
2292
+ Local function actions use { "functionCall": { "@call": "functionName", "args": { ... } } }.
2293
+ Do not rename literal objects' path keys: only @path denotes native value binding.
2294
+ Keep an existing surface's protocol and catalog when updating it; create a new
2295
+ surface to use a different protocol.`;
2296
+ /** Preserve legacy prompt text exactly unless the selected catalog opts into 1.0. */
2297
+ function a2uiGenerationGuidelines(protocolVersion) {
2298
+ return protocolVersion === "1.0" ? A2UI_V1_GENERATION_GUIDELINES : _copilotkit_shared.A2UI_DEFAULT_GENERATION_GUIDELINES;
2299
+ }
2300
+
2204
2301
  //#endregion
2205
2302
  //#region src/v2/a2ui/A2UICatalogContext.tsx
2206
2303
  /**
@@ -2220,6 +2317,7 @@ react_markdown = __toESM(react_markdown);
2220
2317
  const capabilitiesValue = (0, react.useMemo)(() => (0, _copilotkit_a2ui_renderer.buildCatalogContextValue)(catalog, { includeSchema: includeSchema !== false }), [catalog, includeSchema]);
2221
2318
  const schemaValue = (0, react.useMemo)(() => includeSchema !== false ? JSON.stringify((0, _copilotkit_a2ui_renderer.extractCatalogComponentSchemas)(catalog)) : null, [catalog, includeSchema]);
2222
2319
  const a2uiAgentsKey = copilotkit?.a2uiAgents?.join(",");
2320
+ const generationGuidelines = a2uiGenerationGuidelines(catalog?.protocolVersion);
2223
2321
  (0, react.useLayoutEffect)(() => {
2224
2322
  if (!copilotkit) return;
2225
2323
  const agentIds = copilotkit.a2uiAgents;
@@ -2238,7 +2336,7 @@ react_markdown = __toESM(react_markdown);
2238
2336
  }));
2239
2337
  ids.push(copilotkit.addContext({
2240
2338
  description: "A2UI generation guidelines — protocol rules, tool arguments, path rules, data model format, and form/two-way-binding instructions.",
2241
- value: _copilotkit_shared.A2UI_DEFAULT_GENERATION_GUIDELINES,
2339
+ value: generationGuidelines,
2242
2340
  ...scope
2243
2341
  }));
2244
2342
  ids.push(copilotkit.addContext({
@@ -2254,11 +2352,87 @@ react_markdown = __toESM(react_markdown);
2254
2352
  copilotkit,
2255
2353
  capabilitiesValue,
2256
2354
  schemaValue,
2257
- a2uiAgentsKey
2355
+ a2uiAgentsKey,
2356
+ generationGuidelines
2258
2357
  ]);
2259
2358
  return null;
2260
2359
  }
2261
2360
 
2361
+ //#endregion
2362
+ //#region src/v2/generative-ui/runtime-context.ts
2363
+ const GenerativeUIRuntimeContext = (0, react.createContext)(null);
2364
+ function useGenerativeUIRuntime() {
2365
+ return (0, react.useContext)(GenerativeUIRuntimeContext);
2366
+ }
2367
+ function useRequiredGenerativeUIRuntime() {
2368
+ const runtime = (0, react.useContext)(GenerativeUIRuntimeContext);
2369
+ 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.");
2370
+ return runtime;
2371
+ }
2372
+ const GenerativeUINodeContext = (0, react.createContext)(null);
2373
+
2374
+ //#endregion
2375
+ //#region src/v2/generative-ui/hooks.ts
2376
+ const noopSubscribe$1 = () => () => {};
2377
+ const zero = () => 0;
2378
+ /** Re-renders on any surface-manager change. */
2379
+ function useManagerVersion(runtime) {
2380
+ return (0, react.useSyncExternalStore)(runtime?.manager.subscribe ?? noopSubscribe$1, runtime?.manager.getVersion ?? zero, zero);
2381
+ }
2382
+
2383
+ //#endregion
2384
+ //#region src/v2/generative-ui/activity.ts
2385
+ /**
2386
+ * The rejected operations of a surface (the runtime's first, then the
2387
+ * client's) and the error that goes with them, for a wrapper: a generic
2388
+ * message, with the same list as its `cause`. The `cause` is not enumerable,
2389
+ * so `JSON.stringify`, a spread and error reporters leave the list out.
2390
+ * `clientRejected` is appended to in place by the manager, so its length is
2391
+ * part of the key.
2392
+ */
2393
+ function useGenerativeUIRejected(serverRejected, clientRejected) {
2394
+ const rejected = (0, react.useMemo)(() => [...serverRejected ?? [], ...clientRejected ?? []], [
2395
+ serverRejected,
2396
+ clientRejected,
2397
+ clientRejected?.length ?? 0
2398
+ ]);
2399
+ return {
2400
+ rejected,
2401
+ 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])
2402
+ };
2403
+ }
2404
+ /**
2405
+ * What a renderer of one surface activity message shows: the surface (or the
2406
+ * failed surface whose `createSurface` was rejected), and its rejected
2407
+ * operations with their error (see {@link useGenerativeUIRejected}). It
2408
+ * ingests `content` into the manager (idempotent: the root ingests the
2409
+ * agent's messages too, and this covers an agent the root has not seen yet),
2410
+ * and renders again on every change of the manager.
2411
+ *
2412
+ * `surface` is undefined when there is no runtime, thread or content, when
2413
+ * the surface's target is not `target`, or when the surface does not exist
2414
+ * (the agent deleted it). Both rejected lists cover only the latest
2415
+ * `render_ui` call (see the manager's ingest).
2416
+ */
2417
+ function useGenerativeUIActivitySurface({ runtime, threadId, agentId, content, target }) {
2418
+ useManagerVersion(runtime);
2419
+ (0, react.useLayoutEffect)(() => {
2420
+ if (runtime && threadId && content) runtime.manager.ingest(threadId, agentId, content);
2421
+ }, [
2422
+ runtime,
2423
+ threadId,
2424
+ agentId,
2425
+ content
2426
+ ]);
2427
+ const surface = runtime && threadId && content && content.target === target ? runtime.manager.getSurface(threadId, content.surfaceId) ?? runtime.manager.getFailedSurface(threadId, content.surfaceId) : void 0;
2428
+ const { rejected, error } = useGenerativeUIRejected(content?.rejected, surface?.clientRejected);
2429
+ return {
2430
+ surface,
2431
+ rejected,
2432
+ error
2433
+ };
2434
+ }
2435
+
2262
2436
  //#endregion
2263
2437
  //#region src/v2/generative-ui/markdown/context.ts
2264
2438
  /**
@@ -2340,9 +2514,38 @@ react_markdown = __toESM(react_markdown);
2340
2514
  //#endregion
2341
2515
  //#region src/v2/generative-ui/use-generative-ui-root.ts
2342
2516
  const isDevelopment = () => process.env.NODE_ENV !== "production";
2517
+ /**
2518
+ * The malformed-content warnings already printed, by manager, so each
2519
+ * distinct issue warns once while the manager lives.
2520
+ */
2521
+ const warnedMalformed = /* @__PURE__ */ new WeakMap();
2522
+ /**
2523
+ * Ingests the surface activity messages of `messages`. A message whose
2524
+ * content is malformed (for example `null`) is skipped, in development with a
2525
+ * warning once for each distinct issue, so it cannot throw inside the
2526
+ * agent's run; the messages after it are still ingested.
2527
+ */
2343
2528
  function ingestMessages(manager, agent, messages) {
2344
- if (!agent.threadId) return;
2345
- 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);
2529
+ const { threadId } = agent;
2530
+ if (!threadId) return;
2531
+ for (const message of messages) {
2532
+ if (message.role !== "activity" || message.activityType !== _copilotkit_shared.GENERATIVE_UI_ACTIVITY_TYPE) continue;
2533
+ const parsed = (0, _copilotkit_core_generative_ui.parseGenerativeUISurfaceContent)(message.content);
2534
+ if (parsed.content) {
2535
+ manager.ingest(threadId, agent.agentId, parsed.content);
2536
+ continue;
2537
+ }
2538
+ if (!isDevelopment()) continue;
2539
+ const warning = `[generativeUI] skipped an activity message of thread ${JSON.stringify(threadId)}: its content is malformed: ${parsed.issues.join("; ")}.`;
2540
+ let warned = warnedMalformed.get(manager);
2541
+ if (!warned) {
2542
+ warned = /* @__PURE__ */ new Set();
2543
+ warnedMalformed.set(manager, warned);
2544
+ }
2545
+ if (warned.has(warning)) continue;
2546
+ warned.add(warning);
2547
+ console.warn(warning);
2548
+ }
2346
2549
  }
2347
2550
  /** Provider-side wiring: manager, dispatcher, agent subscriptions, envelope. */
2348
2551
  function useGenerativeUIRoot(copilotkit, config) {
@@ -2608,29 +2811,35 @@ react_markdown = __toESM(react_markdown);
2608
2811
  }
2609
2812
 
2610
2813
  //#endregion
2611
- //#region src/v2/generative-ui/runtime-context.ts
2612
- const GenerativeUIRuntimeContext = (0, react.createContext)(null);
2613
- function useGenerativeUIRuntime() {
2614
- return (0, react.useContext)(GenerativeUIRuntimeContext);
2615
- }
2616
- function useRequiredGenerativeUIRuntime() {
2617
- const runtime = (0, react.useContext)(GenerativeUIRuntimeContext);
2618
- if (!runtime) throw new Error("Generative UI components must be rendered inside <CopilotKitProvider generativeUI={{ ui }}>.");
2619
- return runtime;
2814
+ //#region src/v2/generative-ui/host.tsx
2815
+ const DISPLAY_CONTENTS = { display: "contents" };
2816
+ /**
2817
+ * The root element is always there, so a theme change does not remount the
2818
+ * surface. `display: contents` keeps it out of the layout, and the CSS
2819
+ * variables still reach every component of the surface. The `a2ui-surface`
2820
+ * class scopes the standard catalog's surface CSS rules. The theme is a
2821
+ * stylesheet, not an inline style, so that its `dark` values can apply in
2822
+ * dark mode only. The values are checked in `themeStyleSheet`, and the HTML
2823
+ * is set as is, so the server render keeps the quotes of the rule.
2824
+ */
2825
+ function DomSurfaceRoot({ surfaceId, theme, children }) {
2826
+ const sheet = react.default.useMemo(() => (0, _copilotkit_shared_generative_ui.themeStyleSheet)(theme), [theme]);
2827
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2828
+ className: "a2ui-surface",
2829
+ "data-generative-ui-root": surfaceId,
2830
+ "data-a2ui-theme": sheet?.id,
2831
+ style: DISPLAY_CONTENTS,
2832
+ children: [sheet ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("style", { dangerouslySetInnerHTML: { __html: sheet.css } }) : null, children]
2833
+ });
2620
2834
  }
2621
- const GenerativeUINodeContext = (0, react.createContext)(null);
2622
-
2623
- //#endregion
2624
- //#region src/v2/generative-ui/hooks.ts
2625
- const noopSubscribe$1 = () => () => {};
2626
- const zero = () => 0;
2627
- /** Re-renders on any surface-manager change. */
2628
- function useManagerVersion(runtime) {
2629
- return (0, react.useSyncExternalStore)(runtime?.manager.subscribe ?? noopSubscribe$1, runtime?.manager.getVersion ?? zero, zero);
2835
+ function DomAccessibilityWrapper({ attributes, children }) {
2836
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
2837
+ "data-a2ui-accessibility": "",
2838
+ style: DISPLAY_CONTENTS,
2839
+ ...attributes,
2840
+ children
2841
+ });
2630
2842
  }
2631
-
2632
- //#endregion
2633
- //#region src/v2/generative-ui/boundaries.tsx
2634
2843
  function DefaultNodeFallback({ message, componentId, retry }) {
2635
2844
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2636
2845
  role: "alert",
@@ -2658,7 +2867,24 @@ react_markdown = __toESM(react_markdown);
2658
2867
  ]
2659
2868
  });
2660
2869
  }
2870
+ /** The DOM host: what a surface draws on the web. */
2871
+ const domGenerativeUIHost = {
2872
+ SurfaceRoot: DomSurfaceRoot,
2873
+ AccessibilityWrapper: DomAccessibilityWrapper,
2874
+ NodeFallback: DefaultNodeFallback
2875
+ };
2661
2876
  /**
2877
+ * The host elements of the surfaces below. Defaults to the DOM host, so a
2878
+ * web app never sets it.
2879
+ */
2880
+ const GenerativeUIHostContext = (0, react.createContext)(domGenerativeUIHost);
2881
+ function useGenerativeUIHost() {
2882
+ return (0, react.useContext)(GenerativeUIHostContext);
2883
+ }
2884
+
2885
+ //#endregion
2886
+ //#region src/v2/generative-ui/boundaries.tsx
2887
+ /**
2662
2888
  * A child that has not streamed yet renders nothing. The prose keeps
2663
2889
  * streaming above the surface, and each component appears as soon as it is
2664
2890
  * complete. A grey block in its place would read as a broken layout.
@@ -2666,18 +2892,188 @@ react_markdown = __toESM(react_markdown);
2666
2892
  function NodePlaceholder(_props) {
2667
2893
  return null;
2668
2894
  }
2669
- /** Per-node boundary. Resets on retry or a new agent revision, not on data ticks. */
2670
- var NodeBoundary = class extends react.Component {
2895
+ /**
2896
+ * The error that a fallback threw, told apart from the error of the
2897
+ * component it stood in for. The fallback's own error is the `cause`.
2898
+ */
2899
+ function fallbackError(error, componentId) {
2900
+ const subject = componentId === void 0 ? "the surface" : componentId;
2901
+ return (0, _copilotkit_shared_generative_ui.withCause)(/* @__PURE__ */ new Error(`The error fallback of ${subject} threw: ${error.message}`), error);
2902
+ }
2903
+ function createFailureLog() {
2904
+ const last = /* @__PURE__ */ new Map();
2905
+ const released = /* @__PURE__ */ new Set();
2906
+ const releasedSinceSettle = /* @__PURE__ */ new Set();
2907
+ const heldBack = /* @__PURE__ */ new Map();
2908
+ return {
2909
+ report(runtime, slot, info, context = "") {
2910
+ released.delete(slot);
2911
+ const signature = [
2912
+ info.kind,
2913
+ info.error.name,
2914
+ info.error.message,
2915
+ context
2916
+ ].join("\n");
2917
+ if (last.get(slot) === signature) {
2918
+ if (releasedSinceSettle.has(slot)) heldBack.set(slot, {
2919
+ runtime,
2920
+ info,
2921
+ signature
2922
+ });
2923
+ return;
2924
+ }
2925
+ heldBack.delete(slot);
2926
+ last.set(slot, signature);
2927
+ (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, info);
2928
+ },
2929
+ clear(slot) {
2930
+ released.delete(slot);
2931
+ last.delete(slot);
2932
+ heldBack.delete(slot);
2933
+ },
2934
+ retain(slot) {
2935
+ released.delete(slot);
2936
+ },
2937
+ release(slot) {
2938
+ heldBack.delete(slot);
2939
+ if (!last.has(slot)) return;
2940
+ released.add(slot);
2941
+ releasedSinceSettle.add(slot);
2942
+ },
2943
+ settle(missed = false) {
2944
+ for (const slot of missed ? releasedSinceSettle : released) last.delete(slot);
2945
+ if (missed) for (const [slot, { runtime, info, signature }] of heldBack) {
2946
+ if (!releasedSinceSettle.has(slot) || last.has(slot)) continue;
2947
+ last.set(slot, signature);
2948
+ (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, info);
2949
+ }
2950
+ heldBack.clear();
2951
+ released.clear();
2952
+ releasedSinceSettle.clear();
2953
+ }
2954
+ };
2955
+ }
2956
+ /** Outside a surface, every failure is reported. */
2957
+ const UNLOGGED = {
2958
+ report: (runtime, _slot, info) => (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, info),
2959
+ clear: () => {},
2960
+ retain: () => {},
2961
+ release: () => {},
2962
+ settle: () => {}
2963
+ };
2964
+ /** The failure log of the surface being rendered; set by `SurfaceView`. */
2965
+ const GenerativeUIFailureLogContext = (0, react.createContext)(UNLOGGED);
2966
+ function useFailureLog() {
2967
+ return (0, react.useContext)(GenerativeUIFailureLogContext);
2968
+ }
2969
+ /**
2970
+ * The slots of a failure log, per node place (one rendered copy of a node,
2971
+ * see `placeOf` in SurfaceView) and surface boundary.
2972
+ */
2973
+ const failureSlots = {
2974
+ node: (place) => `node ${place}`,
2975
+ appFallback: (place) => `app-fallback ${place}`,
2976
+ renderFallback: (place) => `render-fallback ${place}`,
2977
+ fallback: (place) => `fallback ${place}`,
2978
+ cycle: (place) => `cycle ${place}`,
2979
+ props: (place) => `props ${place}`,
2980
+ surface: (level) => `surface ${level}`,
2981
+ surfaceFallback: (level) => `surface-fallback ${level}`
2982
+ };
2983
+ function sameKeys(a, b) {
2984
+ return a.length === b.length && a.every((value, i) => Object.is(value, b[i]));
2985
+ }
2986
+ var FallbackGuard = class extends react.Component {
2671
2987
  constructor(..._args) {
2672
2988
  super(..._args);
2989
+ this.state = {
2990
+ failed: false,
2991
+ resetKeys: this.props.resetKeys
2992
+ };
2993
+ }
2994
+ static getDerivedStateFromError(thrown) {
2995
+ return {
2996
+ failed: true,
2997
+ error: (0, _copilotkit_shared_generative_ui.toError)(thrown)
2998
+ };
2999
+ }
3000
+ static getDerivedStateFromProps(props, state) {
3001
+ return sameKeys(props.resetKeys, state.resetKeys) ? null : {
3002
+ failed: false,
3003
+ error: void 0,
3004
+ resetKeys: props.resetKeys
3005
+ };
3006
+ }
3007
+ report(error) {
3008
+ const { runtime, surfaceId, componentId, log, slot, context } = this.props;
3009
+ log.report(runtime, slot, {
3010
+ kind: "render",
3011
+ surfaceId,
3012
+ ...componentId === void 0 ? {} : { componentId },
3013
+ error: fallbackError(error, componentId)
3014
+ }, context);
3015
+ }
3016
+ componentDidCatch(thrown) {
3017
+ this.report(this.state.error ?? (0, _copilotkit_shared_generative_ui.toError)(thrown));
3018
+ }
3019
+ componentDidMount() {
3020
+ if (!this.state.failed) {
3021
+ this.props.log.clear(this.props.slot);
3022
+ return;
3023
+ }
3024
+ if (this.state.error) this.report(this.state.error);
3025
+ }
3026
+ componentDidUpdate() {
3027
+ if (!this.state.failed) this.props.log.clear(this.props.slot);
3028
+ }
3029
+ render() {
3030
+ return this.state.failed ? this.props.replacement ?? null : this.props.children;
3031
+ }
3032
+ };
3033
+ /**
3034
+ * The host's `NodeFallback`, guarded: when it throws, the node renders
3035
+ * nothing and the error is reported, and the parent node keeps rendering.
3036
+ * Every place that shows the host fallback goes through this.
3037
+ */
3038
+ function GuardedNodeFallback({ surfaceId, componentId, runtime, message, retry, slot, revision }) {
3039
+ const { NodeFallback } = useGenerativeUIHost();
3040
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FallbackGuard, {
3041
+ surfaceId,
3042
+ componentId,
3043
+ runtime,
3044
+ log: useFailureLog(),
3045
+ slot,
3046
+ context: message,
3047
+ resetKeys: [message, revision],
3048
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeFallback, {
3049
+ message,
3050
+ componentId: componentId ?? "surface",
3051
+ retry
3052
+ })
3053
+ });
3054
+ }
3055
+ /**
3056
+ * Calls the app's `errorBoundary.fallback` in a render of its own, so that
3057
+ * the guard above it catches what it throws.
3058
+ */
3059
+ function AppErrorFallback({ render, ...props }) {
3060
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: render(props) });
3061
+ }
3062
+ /**
3063
+ * Per-node boundary. Resets on retry or a new agent revision, not on data
3064
+ * ticks. A failure that stays the same across revisions is reported once.
3065
+ */
3066
+ var NodeBoundary = class extends react.Component {
3067
+ constructor(..._args2) {
3068
+ super(..._args2);
2673
3069
  this.state = {
2674
3070
  error: null,
2675
3071
  revision: this.props.revision
2676
3072
  };
2677
3073
  this.retry = () => this.setState({ error: null });
2678
3074
  }
2679
- static getDerivedStateFromError(error) {
2680
- return { error };
3075
+ static getDerivedStateFromError(thrown) {
3076
+ return { error: (0, _copilotkit_shared_generative_ui.toError)(thrown) };
2681
3077
  }
2682
3078
  static getDerivedStateFromProps(props, state) {
2683
3079
  return props.revision !== state.revision ? {
@@ -2685,53 +3081,138 @@ react_markdown = __toESM(react_markdown);
2685
3081
  revision: props.revision
2686
3082
  } : null;
2687
3083
  }
2688
- componentDidCatch(error) {
2689
- const { runtime, surfaceId, componentId } = this.props;
2690
- (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, {
3084
+ componentDidCatch(thrown) {
3085
+ const { runtime, surfaceId, componentId, log, place } = this.props;
3086
+ log.clear(failureSlots.fallback(place));
3087
+ log.report(runtime, failureSlots.node(place), {
2691
3088
  kind: "render",
2692
3089
  surfaceId,
2693
3090
  componentId,
2694
- error
3091
+ error: this.state.error ?? (0, _copilotkit_shared_generative_ui.toError)(thrown)
3092
+ });
3093
+ }
3094
+ slots() {
3095
+ const { place } = this.props;
3096
+ return [
3097
+ failureSlots.node(place),
3098
+ failureSlots.appFallback(place),
3099
+ failureSlots.renderFallback(place)
3100
+ ];
3101
+ }
3102
+ clearSlots() {
3103
+ for (const slot of this.slots()) this.props.log.clear(slot);
3104
+ }
3105
+ componentDidMount() {
3106
+ for (const slot of this.slots()) this.props.log.retain(slot);
3107
+ if (!this.state.error) {
3108
+ this.clearSlots();
3109
+ return;
3110
+ }
3111
+ const { runtime, surfaceId, componentId, log, place } = this.props;
3112
+ log.report(runtime, failureSlots.node(place), {
3113
+ kind: "render",
3114
+ surfaceId,
3115
+ componentId,
3116
+ error: this.state.error
2695
3117
  });
2696
3118
  }
3119
+ componentDidUpdate(prevProps) {
3120
+ if (prevProps.place !== this.props.place) {
3121
+ const { log } = this.props;
3122
+ log.release(failureSlots.node(prevProps.place));
3123
+ log.release(failureSlots.appFallback(prevProps.place));
3124
+ log.release(failureSlots.renderFallback(prevProps.place));
3125
+ for (const slot of this.slots()) log.retain(slot);
3126
+ }
3127
+ if (!this.state.error) this.clearSlots();
3128
+ }
3129
+ componentWillUnmount() {
3130
+ for (const slot of this.slots()) this.props.log.release(slot);
3131
+ }
2697
3132
  render() {
2698
3133
  const { error } = this.state;
2699
3134
  if (!error) return this.props.children;
2700
- const { runtime, surfaceId, componentId } = this.props;
2701
- const fallback = runtime.errorFallback;
2702
- return fallback ? fallback({
2703
- error,
2704
- retry: this.retry,
3135
+ const { runtime, surfaceId, componentId, place, log } = this.props;
3136
+ const hostFallback = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
2705
3137
  surfaceId,
2706
- componentId
2707
- }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
3138
+ componentId,
3139
+ runtime,
2708
3140
  message: error.message,
3141
+ retry: this.retry,
3142
+ slot: failureSlots.renderFallback(place)
3143
+ });
3144
+ const fallback = runtime.errorFallback;
3145
+ if (!fallback) return hostFallback;
3146
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FallbackGuard, {
3147
+ surfaceId,
2709
3148
  componentId,
2710
- retry: this.retry
3149
+ runtime,
3150
+ log,
3151
+ slot: failureSlots.appFallback(place),
3152
+ context: error.message,
3153
+ resetKeys: [error],
3154
+ replacement: hostFallback,
3155
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AppErrorFallback, {
3156
+ render: fallback,
3157
+ error,
3158
+ retry: this.retry,
3159
+ surfaceId,
3160
+ componentId
3161
+ })
2711
3162
  });
2712
3163
  }
2713
3164
  };
2714
- /** Outer boundary: a failure here must not reach the chat or the host page. */
3165
+ /**
3166
+ * Surface boundary: a failure here must not reach the chat or the host page.
3167
+ * It resets on a new revision, and on Retry when `retryable`. A failure that
3168
+ * stays the same across revisions is reported once.
3169
+ */
2715
3170
  var SurfaceBoundary = class extends react.Component {
2716
- constructor(..._args2) {
2717
- super(..._args2);
2718
- this.state = { error: null };
3171
+ constructor(..._args3) {
3172
+ super(..._args3);
3173
+ this.state = {
3174
+ error: null,
3175
+ revision: this.props.revision
3176
+ };
3177
+ this.retry = () => this.setState({ error: null });
2719
3178
  }
2720
- static getDerivedStateFromError(error) {
2721
- return { error };
3179
+ static getDerivedStateFromError(thrown) {
3180
+ return { error: (0, _copilotkit_shared_generative_ui.toError)(thrown) };
2722
3181
  }
2723
- componentDidCatch(error) {
2724
- (0, _copilotkit_core_generative_ui.reportErrorSafely)(this.props.runtime, {
3182
+ static getDerivedStateFromProps(props, state) {
3183
+ return props.revision !== state.revision ? {
3184
+ error: null,
3185
+ revision: props.revision
3186
+ } : null;
3187
+ }
3188
+ componentDidCatch(thrown) {
3189
+ const { runtime, surfaceId, log, level } = this.props;
3190
+ log.report(runtime, failureSlots.surface(level), {
2725
3191
  kind: "render",
2726
- surfaceId: this.props.surfaceId,
2727
- error
3192
+ surfaceId,
3193
+ error: this.state.error ?? (0, _copilotkit_shared_generative_ui.toError)(thrown)
2728
3194
  });
2729
3195
  }
3196
+ clearSlots() {
3197
+ const { log, level } = this.props;
3198
+ log.clear(failureSlots.surface(level));
3199
+ log.clear(failureSlots.surfaceFallback(level));
3200
+ }
3201
+ componentDidMount() {
3202
+ if (!this.state.error) this.clearSlots();
3203
+ }
3204
+ componentDidUpdate() {
3205
+ if (!this.state.error) this.clearSlots();
3206
+ }
2730
3207
  render() {
2731
3208
  if (!this.state.error) return this.props.children;
2732
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
3209
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
3210
+ surfaceId: this.props.surfaceId,
3211
+ componentId: void 0,
3212
+ runtime: this.props.runtime,
2733
3213
  message: "this generated UI failed to render",
2734
- componentId: "surface"
3214
+ retry: this.props.retryable ? this.retry : void 0,
3215
+ slot: failureSlots.surfaceFallback(this.props.level)
2735
3216
  });
2736
3217
  }
2737
3218
  };
@@ -2739,7 +3220,6 @@ react_markdown = __toESM(react_markdown);
2739
3220
  //#endregion
2740
3221
  //#region src/v2/generative-ui/SurfaceView.tsx
2741
3222
  const NO_ANCESTORS = [];
2742
- const ROOT_STYLE = { display: "contents" };
2743
3223
  const undefinedSnapshot = () => void 0;
2744
3224
  const noopSubscribe = () => () => {};
2745
3225
  const zeroSnapshot = () => 0;
@@ -2749,23 +3229,80 @@ react_markdown = __toESM(react_markdown);
2749
3229
  }
2750
3230
  function SurfaceView({ surface }) {
2751
3231
  const runtime = useRequiredGenerativeUIRuntime();
2752
- const { theme } = runtime;
2753
- const sheet = (0, react.useMemo)(() => (0, _copilotkit_shared_generative_ui.themeStyleSheet)(theme), [theme]);
2754
- return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
2755
- className: "a2ui-surface",
2756
- "data-generative-ui-root": surface.surfaceId,
2757
- "data-a2ui-theme": sheet?.id,
2758
- style: ROOT_STYLE,
2759
- children: [sheet ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("style", { dangerouslySetInnerHTML: { __html: sheet.css } }) : null, /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceBoundary, {
3232
+ const { SurfaceRoot } = useGenerativeUIHost();
3233
+ const appliedCount = (0, react.useSyncExternalStore)(runtime.manager.subscribe, () => surface.operations.length, undefinedSnapshot);
3234
+ const revision = (0, react.useMemo)(() => ({
3235
+ surface,
3236
+ appliedCount
3237
+ }), [surface, appliedCount]);
3238
+ const [scopedLog, setScopedLog] = (0, react.useState)(() => ({
3239
+ key: surface.key,
3240
+ log: createFailureLog()
3241
+ }));
3242
+ let failureLog = scopedLog.log;
3243
+ if (scopedLog.key !== surface.key) {
3244
+ const next = {
3245
+ key: surface.key,
3246
+ log: createFailureLog()
3247
+ };
3248
+ setScopedLog(next);
3249
+ failureLog = next.log;
3250
+ }
3251
+ const { threadId, surfaceId } = surface;
3252
+ const { manager } = runtime;
3253
+ const seen = (0, react.useRef)(void 0);
3254
+ (0, react.useLayoutEffect)(() => {
3255
+ const contentOf = () => {
3256
+ const current = manager.getSurface(threadId, surfaceId);
3257
+ return {
3258
+ log: failureLog,
3259
+ current,
3260
+ applied: current?.operations.length
3261
+ };
3262
+ };
3263
+ const observe = (missed) => {
3264
+ const last = seen.current;
3265
+ const next = contentOf();
3266
+ seen.current = next;
3267
+ if (last?.log === failureLog && (next.current !== last.current || next.applied !== last.applied)) failureLog.settle(missed);
3268
+ };
3269
+ observe(true);
3270
+ return manager.subscribe(() => observe(false));
3271
+ }, [
3272
+ manager,
3273
+ threadId,
3274
+ surfaceId,
3275
+ failureLog
3276
+ ]);
3277
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GenerativeUIFailureLogContext.Provider, {
3278
+ value: failureLog,
3279
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceBoundary, {
2760
3280
  surfaceId: surface.surfaceId,
2761
3281
  runtime,
2762
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
2763
- surface,
2764
- componentId: _copilotkit_shared.GENERATIVE_UI_ROOT_ID,
2765
- basePath: void 0,
2766
- ancestors: NO_ANCESTORS
3282
+ log: failureLog,
3283
+ level: "outer",
3284
+ revision,
3285
+ retryable: true,
3286
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceRoot, {
3287
+ surfaceId: surface.surfaceId,
3288
+ theme: runtime.theme,
3289
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceBoundary, {
3290
+ surfaceId: surface.surfaceId,
3291
+ runtime,
3292
+ log: failureLog,
3293
+ level: "inner",
3294
+ revision,
3295
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
3296
+ surface,
3297
+ componentId: _copilotkit_shared.GENERATIVE_UI_ROOT_ID,
3298
+ basePath: void 0,
3299
+ ancestors: NO_ANCESTORS,
3300
+ parentPlace: "",
3301
+ via: ""
3302
+ })
3303
+ })
2767
3304
  })
2768
- })]
3305
+ })
2769
3306
  });
2770
3307
  }
2771
3308
  /**
@@ -2790,27 +3327,63 @@ react_markdown = __toESM(react_markdown);
2790
3327
  * repeats both.
2791
3328
  */
2792
3329
  function scopedId(componentId, basePath) {
2793
- return `${basePath ?? ""}#${componentId}`;
3330
+ return `${basePath === void 0 ? "" : JSON.stringify(basePath)}#${JSON.stringify(componentId)}`;
3331
+ }
3332
+ /**
3333
+ * Where one rendered copy of a node is: the place of its parent, the slot
3334
+ * or child reference of the parent it renders in, and its scoped id. Names
3335
+ * the node's failure-log slots. A scoped id alone is not enough: the same
3336
+ * id can render under two parents, and a cyclic reference repeats the
3337
+ * scoped id of an ancestor. Each copy keeps its own memory of what it
3338
+ * reported, so one copy that renders, goes away or remounts does not clear
3339
+ * the memory of another that still fails.
3340
+ */
3341
+ function placeOf(parentPlace, via, position) {
3342
+ return `${parentPlace}/${via === "" ? "" : JSON.stringify(via)}${position}`;
2794
3343
  }
2795
- const NodeView = (0, react.memo)(function NodeView({ surface, componentId, basePath, ancestors }) {
3344
+ const NodeView = (0, react.memo)(function NodeView({ surface, componentId, basePath, ancestors, parentPlace, via }) {
2796
3345
  const runtime = useRequiredGenerativeUIRuntime();
3346
+ const log = useFailureLog();
2797
3347
  const component = useComponentModel(runtime.manager, surface.model, componentId);
2798
3348
  const properties = useComponentProperties(runtime.manager, component);
2799
- if (ancestors.includes(scopedId(componentId, basePath))) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
3349
+ const position = scopedId(componentId, basePath);
3350
+ const place = placeOf(parentPlace, via, position);
3351
+ (0, react.useEffect)(() => {
3352
+ const slots = [
3353
+ failureSlots.fallback(place),
3354
+ failureSlots.cycle(place),
3355
+ failureSlots.props(place)
3356
+ ];
3357
+ for (const slot of slots) log.retain(slot);
3358
+ return () => {
3359
+ for (const slot of slots) log.release(slot);
3360
+ };
3361
+ }, [log, place]);
3362
+ if (ancestors.includes(position)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
3363
+ surfaceId: surface.surfaceId,
3364
+ componentId,
3365
+ runtime,
2800
3366
  message: `cyclic reference to ${componentId}`,
2801
- componentId
3367
+ slot: failureSlots.cycle(place),
3368
+ revision: properties
2802
3369
  });
2803
3370
  if (!component || !properties) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodePlaceholder, { componentId });
2804
3371
  const definition = runtime.ui.components.get(component.type);
2805
- if (!definition) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
3372
+ if (!definition) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
3373
+ surfaceId: surface.surfaceId,
3374
+ componentId,
3375
+ runtime,
2806
3376
  message: `unknown component "${component.type}"`,
2807
- componentId
3377
+ slot: failureSlots.fallback(place),
3378
+ revision: properties
2808
3379
  });
2809
3380
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeBoundary, {
2810
3381
  surfaceId: surface.surfaceId,
2811
3382
  componentId,
3383
+ place,
2812
3384
  revision: properties,
2813
3385
  runtime,
3386
+ log,
2814
3387
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ResolvedNode, {
2815
3388
  surface,
2816
3389
  componentId,
@@ -2818,7 +3391,8 @@ react_markdown = __toESM(react_markdown);
2818
3391
  properties,
2819
3392
  runtime,
2820
3393
  basePath,
2821
- ancestors
3394
+ ancestors,
3395
+ place
2822
3396
  })
2823
3397
  }, component.type);
2824
3398
  });
@@ -2826,7 +3400,7 @@ react_markdown = __toESM(react_markdown);
2826
3400
  * Renders the component of a template once for each item of the list at the
2827
3401
  * template path, keyed by index. Renders nothing when there is no list.
2828
3402
  */
2829
- function TemplateChildren({ surface, template, basePath, ancestors }) {
3403
+ function TemplateChildren({ surface, template, basePath, ancestors, parentPlace, via }) {
2830
3404
  const dataModel = surface.model.dataModel;
2831
3405
  const listPath = (0, _copilotkit_core_generative_ui.resolvePath)(template.path, basePath);
2832
3406
  const subscribe = (0, react.useCallback)((onChange) => {
@@ -2842,17 +3416,25 @@ react_markdown = __toESM(react_markdown);
2842
3416
  surface,
2843
3417
  componentId: template.componentId,
2844
3418
  basePath: (0, _copilotkit_core_generative_ui.joinPath)(listPath, String(index)),
2845
- ancestors
3419
+ ancestors,
3420
+ parentPlace,
3421
+ via
2846
3422
  }, index)) });
2847
3423
  }
2848
3424
  /**
2849
3425
  * The props the model sets, with their dynamic values resolved from the
2850
3426
  * surface data model: `{"@path"}` bindings, `{"@call"}` function values and
2851
- * `{"@call":"@index"}` in a template, at any depth. A literal writable prop
2852
- * that the component set shows the local value from `overrides`. Bound
2853
- * props, events and slots are left out, and so are the keys of the A2UI
2854
- * component envelope; `accessibility` is resolved the same way and given
2855
- * on its own.
3427
+ * `{"@call":"@index"}` in a template, at any depth. Bound props, events and
3428
+ * slots are left out, and so are the keys of the A2UI component envelope;
3429
+ * `accessibility` is resolved the same way and given on its own.
3430
+ *
3431
+ * A writable prop that is not bound to a path and that the component set
3432
+ * shows the local value from `overrides`, also when the model never set the
3433
+ * prop. That is intended: the user's edit wins over the model's literal
3434
+ * value, also over a newer literal the agent sends later, until the node
3435
+ * remounts. A literal is not data the agent and the user share; a prop
3436
+ * that both must write is bound to a path, and the setter writes the data
3437
+ * model.
2856
3438
  *
2857
3439
  * A prop is resolved again only when its raw value or a data model value it
2858
3440
  * read has changed, so the result keeps its identity otherwise. A function
@@ -2863,7 +3445,7 @@ react_markdown = __toESM(react_markdown);
2863
3445
  const units = (0, react.useMemo)(() => {
2864
3446
  const list = [];
2865
3447
  for (const key of Object.keys(properties)) {
2866
- if (definition.events?.[key] || definition.slots?.[key] || Object.hasOwn(definition.selectors, key) || _copilotkit_shared.A2UI_COMPONENT_ENVELOPE_KEYS.has(key)) continue;
3448
+ 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;
2867
3449
  list.push({
2868
3450
  key,
2869
3451
  raw: properties[key]
@@ -2964,9 +3546,10 @@ react_markdown = __toESM(react_markdown);
2964
3546
  for (const [key, selector] of selectors) try {
2965
3547
  values[key] = selector(state, modelProps);
2966
3548
  } catch (error) {
3549
+ const { message } = (0, _copilotkit_shared_generative_ui.toError)(error);
2967
3550
  return {
2968
3551
  ok: false,
2969
- problem: `${key}: selector threw: ${error instanceof Error ? error.message : String(error)}`
3552
+ problem: `${key}: selector threw: ${message}`
2970
3553
  };
2971
3554
  }
2972
3555
  return {
@@ -2977,8 +3560,10 @@ react_markdown = __toESM(react_markdown);
2977
3560
  /**
2978
3561
  * The values of the bound props of a node, read from the app state.
2979
3562
  *
2980
- * The selectors run again only when the state object or the parsed model
2981
- * props change. When the new values are shallow-equal to the last ones, the
3563
+ * The selectors get the checked model props: the resolved values that
3564
+ * passed the props schema (without the nulls it rejected), not the
3565
+ * schema's parsed output, so its defaults and transforms are not applied
3566
+ * 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
2982
3567
  * last snapshot is kept. So the node re-renders only when a selected value
2983
3568
  * changes. A selector that returns a new array on each call cannot cause a
2984
3569
  * render loop: with the same inputs, the cached snapshot comes back.
@@ -3008,7 +3593,10 @@ react_markdown = __toESM(react_markdown);
3008
3593
  * Checks the model-set props with the props schema before the selectors run.
3009
3594
  * The bound props are not set yet, so their issues do not count. Neither do
3010
3595
  * issues with no path (a check over several props), which the full check
3011
- * after the selectors reports.
3596
+ * after the selectors reports. The exception is the issue that an async
3597
+ * props schema gives (`isAsyncIssue`): the render checks props
3598
+ * synchronously only, so the full check would fail the same way, and it is
3599
+ * reported here.
3012
3600
  */
3013
3601
  function checkModelProps(definition, modelData) {
3014
3602
  if (!definition.props) return {
@@ -3027,7 +3615,7 @@ react_markdown = __toESM(react_markdown);
3027
3615
  if (!result.success) {
3028
3616
  const issues = result.issues.filter((issue) => {
3029
3617
  const key = (0, _copilotkit_shared_generative_ui.issueKey)(issue);
3030
- return key === void 0 ? (0, _copilotkit_shared_generative_ui.isAsyncIssue)(issue) : !Object.hasOwn(definition.selectors, key);
3618
+ return key === void 0 ? (0, _copilotkit_shared_generative_ui.isAsyncIssue)(issue) : !(0, _copilotkit_shared_generative_ui.hasOwnKey)(definition.selectors, key);
3031
3619
  });
3032
3620
  if (issues.length > 0) return {
3033
3621
  ok: false,
@@ -3090,7 +3678,7 @@ react_markdown = __toESM(react_markdown);
3090
3678
  function useEventProps(runtime, surface, componentId, definition, properties, basePath) {
3091
3679
  const latest = (0, react.useRef)(properties);
3092
3680
  latest.current = properties;
3093
- const boundNames = Object.keys(definition.events ?? {}).filter((name) => Object.hasOwn(definition.eventFunctions, name) || (0, _copilotkit_shared.isActionBinding)(properties[name])).join("|");
3681
+ const boundNames = Object.keys(definition.events ?? {}).filter((name) => (0, _copilotkit_shared_generative_ui.hasOwnKey)(definition.eventFunctions, name) || (0, _copilotkit_shared.isActionBinding)(properties[name])).join("|");
3094
3682
  return (0, react.useMemo)(() => {
3095
3683
  const handlers = {};
3096
3684
  const names = boundNames ? boundNames.split("|") : [];
@@ -3102,7 +3690,7 @@ react_markdown = __toESM(react_markdown);
3102
3690
  name,
3103
3691
  parameters: definition.events?.[name]?.parameters
3104
3692
  };
3105
- const functionName = Object.hasOwn(definition.eventFunctions, name) ? definition.eventFunctions[name] : void 0;
3693
+ const functionName = (0, _copilotkit_shared_generative_ui.hasOwnKey)(definition.eventFunctions, name) ? definition.eventFunctions[name] : void 0;
3106
3694
  handlers[name] = functionName === void 0 ? (args) => (0, _copilotkit_core_generative_ui.invokeBoundEvent)({
3107
3695
  ...call,
3108
3696
  binding: latest.current[name],
@@ -3128,7 +3716,9 @@ react_markdown = __toESM(react_markdown);
3128
3716
  * The setter props of a node: `setValue` for the writable prop `value`. For
3129
3717
  * a prop the model bound to `{"@path"}`, the setter writes the data model
3130
3718
  * and makes the path user-owned. For a literal prop, it sets `overrides`, a
3131
- * local value of this mounted node. The setters keep their identity.
3719
+ * local value of this mounted node that wins over the model's value until
3720
+ * the node remounts (see `useModelDataProps`). The setters keep their
3721
+ * identity.
3132
3722
  */
3133
3723
  function useSetterProps(runtime, surface, componentId, definition, properties, basePath) {
3134
3724
  const [overrides, setOverrides] = (0, react.useState)({});
@@ -3151,7 +3741,7 @@ react_markdown = __toESM(react_markdown);
3151
3741
  try {
3152
3742
  runtime.manager.setOwnedValue(surface.key, (0, _copilotkit_core_generative_ui.resolvePath)(binding["@path"], basePath), value);
3153
3743
  } catch (cause) {
3154
- const message = cause instanceof Error ? cause.message : String(cause);
3744
+ const { message } = (0, _copilotkit_shared_generative_ui.toError)(cause);
3155
3745
  (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, {
3156
3746
  kind: "props",
3157
3747
  surfaceId: surface.surfaceId,
@@ -3170,29 +3760,36 @@ react_markdown = __toESM(react_markdown);
3170
3760
  ])
3171
3761
  };
3172
3762
  }
3173
- function useSlotProps(surface, definition, properties, componentId, basePath, ancestors) {
3763
+ function useSlotProps(surface, definition, properties, componentId, basePath, ancestors, place) {
3174
3764
  return (0, react.useMemo)(() => {
3175
3765
  const childAncestors = [...ancestors, scopedId(componentId, basePath)];
3176
3766
  const slots = {};
3177
3767
  for (const [name, kind] of Object.entries(definition.slots ?? {})) {
3178
3768
  const value = properties[name];
3769
+ const via = `${name}:`;
3179
3770
  if (kind === "single" && typeof value === "string") slots[name] = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
3180
3771
  surface,
3181
3772
  componentId: value,
3182
3773
  basePath,
3183
- ancestors: childAncestors
3774
+ ancestors: childAncestors,
3775
+ parentPlace: place,
3776
+ via
3184
3777
  });
3185
3778
  else if (kind === "multiple" && isTemplateChildren(value)) slots[name] = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TemplateChildren, {
3186
3779
  surface,
3187
3780
  template: value,
3188
3781
  basePath,
3189
- ancestors: childAncestors
3782
+ ancestors: childAncestors,
3783
+ parentPlace: place,
3784
+ via
3190
3785
  });
3191
3786
  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, {
3192
3787
  surface,
3193
3788
  componentId: id,
3194
3789
  basePath,
3195
- ancestors: childAncestors
3790
+ ancestors: childAncestors,
3791
+ parentPlace: place,
3792
+ via
3196
3793
  }, id));
3197
3794
  }
3198
3795
  return slots;
@@ -3202,7 +3799,8 @@ react_markdown = __toESM(react_markdown);
3202
3799
  properties,
3203
3800
  componentId,
3204
3801
  basePath,
3205
- ancestors
3802
+ ancestors,
3803
+ place
3206
3804
  ]);
3207
3805
  }
3208
3806
  /**
@@ -3210,33 +3808,43 @@ react_markdown = __toESM(react_markdown);
3210
3808
  * `childList()`, at any depth) replaced by the rendered component. Without
3211
3809
  * references, the parsed props come back as they are.
3212
3810
  */
3213
- function useChildRefProps(surface, refs, props, componentId, basePath, ancestors) {
3811
+ function useChildRefProps(surface, refs, props, componentId, basePath, ancestors, place) {
3214
3812
  return (0, react.useMemo)(() => {
3215
3813
  if (props === void 0 || refs.length === 0) return props;
3216
3814
  const childAncestors = [...ancestors, scopedId(componentId, basePath)];
3217
- const render = (kind, value) => {
3815
+ const render = (kind, value, via) => {
3218
3816
  if (kind === "single") return typeof value === "string" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
3219
3817
  surface,
3220
3818
  componentId: value,
3221
3819
  basePath,
3222
- ancestors: childAncestors
3820
+ ancestors: childAncestors,
3821
+ parentPlace: place,
3822
+ via
3223
3823
  }) : void 0;
3224
3824
  if (isTemplateChildren(value)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TemplateChildren, {
3225
3825
  surface,
3226
3826
  template: value,
3227
3827
  basePath,
3228
- ancestors: childAncestors
3828
+ ancestors: childAncestors,
3829
+ parentPlace: place,
3830
+ via
3229
3831
  });
3230
3832
  if (!Array.isArray(value)) return void 0;
3231
3833
  return [...new Set(value.filter((id) => typeof id === "string"))].map((id) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
3232
3834
  surface,
3233
3835
  componentId: id,
3234
3836
  basePath,
3235
- ancestors: childAncestors
3837
+ ancestors: childAncestors,
3838
+ parentPlace: place,
3839
+ via
3236
3840
  }, id));
3237
3841
  };
3238
3842
  let result = props;
3239
- for (const ref of refs) result = (0, _copilotkit_shared.mapAtChildRef)(result, ref.path, (found) => render(ref.kind, found));
3843
+ for (const ref of refs) {
3844
+ let found = 0;
3845
+ const path = ref.path.join(".");
3846
+ result = (0, _copilotkit_shared.mapAtChildRef)(result, ref.path, (value) => render(ref.kind, value, `${path}[${found++}]:`));
3847
+ }
3240
3848
  return result;
3241
3849
  }, [
3242
3850
  surface,
@@ -3244,15 +3852,17 @@ react_markdown = __toESM(react_markdown);
3244
3852
  props,
3245
3853
  componentId,
3246
3854
  basePath,
3247
- ancestors
3855
+ ancestors,
3856
+ place
3248
3857
  ]);
3249
3858
  }
3250
- function ResolvedNode({ surface, componentId, definition, properties, runtime, basePath, ancestors }) {
3859
+ function ResolvedNode({ surface, componentId, definition, properties, runtime, basePath, ancestors, place }) {
3860
+ const { AccessibilityWrapper } = useGenerativeUIHost();
3251
3861
  const { overrides, setters } = useSetterProps(runtime, surface, componentId, definition, properties, basePath);
3252
3862
  const dataProps = useDataProps(runtime, surface, definition, properties, basePath, overrides);
3253
3863
  const eventProps = useEventProps(runtime, surface, componentId, definition, properties, basePath);
3254
- const slotProps = useSlotProps(surface, definition, properties, componentId, basePath, ancestors);
3255
- const childRefProps = useChildRefProps(surface, definition.childRefs, dataProps.ok ? dataProps.props : void 0, componentId, basePath, ancestors);
3864
+ const slotProps = useSlotProps(surface, definition, properties, componentId, basePath, ancestors, place);
3865
+ const childRefProps = useChildRefProps(surface, definition.childRefs, dataProps.ok ? dataProps.props : void 0, componentId, basePath, ancestors, place);
3256
3866
  const accessibility = dataProps.ok ? dataProps.accessibility : void 0;
3257
3867
  const nodeContext = (0, react.useMemo)(() => ({
3258
3868
  surfaceKey: surface.key,
@@ -3266,8 +3876,19 @@ react_markdown = __toESM(react_markdown);
3266
3876
  accessibility
3267
3877
  ]);
3268
3878
  const problem = dataProps.ok ? void 0 : dataProps.problem;
3879
+ const log = useFailureLog();
3269
3880
  (0, react.useEffect)(() => {
3270
- if (problem) (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, {
3881
+ if (!problem) {
3882
+ log.clear(failureSlots.fallback(place));
3883
+ log.clear(failureSlots.props(place));
3884
+ }
3885
+ }, [
3886
+ problem,
3887
+ log,
3888
+ place
3889
+ ]);
3890
+ (0, react.useEffect)(() => {
3891
+ if (problem) log.report(runtime, failureSlots.props(place), {
3271
3892
  kind: "props",
3272
3893
  surfaceId: surface.surfaceId,
3273
3894
  componentId,
@@ -3275,13 +3896,19 @@ react_markdown = __toESM(react_markdown);
3275
3896
  });
3276
3897
  }, [
3277
3898
  problem,
3899
+ log,
3900
+ place,
3278
3901
  runtime,
3279
3902
  surface.surfaceId,
3280
3903
  componentId
3281
3904
  ]);
3282
- if (!dataProps.ok) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
3905
+ if (!dataProps.ok) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
3906
+ surfaceId: surface.surfaceId,
3907
+ componentId,
3908
+ runtime,
3283
3909
  message: `invalid props (${problem})`,
3284
- componentId
3910
+ slot: failureSlots.fallback(place),
3911
+ revision: properties
3285
3912
  });
3286
3913
  const Component = definition.component;
3287
3914
  const rendered = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, {
@@ -3293,10 +3920,8 @@ react_markdown = __toESM(react_markdown);
3293
3920
  const hasEnvelope = properties.accessibility !== void 0 && definition.accessibility !== "self";
3294
3921
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GenerativeUINodeContext.Provider, {
3295
3922
  value: nodeContext,
3296
- children: hasEnvelope ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
3297
- "data-a2ui-accessibility": "",
3298
- style: ROOT_STYLE,
3299
- ...(0, _copilotkit_core_generative_ui.wrapperAria)((0, _copilotkit_core_generative_ui.accessibilityAttributes)(accessibility)),
3923
+ children: hasEnvelope ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AccessibilityWrapper, {
3924
+ attributes: (0, _copilotkit_core_generative_ui.wrapperAria)((0, _copilotkit_core_generative_ui.accessibilityAttributes)(accessibility)),
3300
3925
  children: rendered
3301
3926
  }) : rendered
3302
3927
  });
@@ -3316,39 +3941,18 @@ react_markdown = __toESM(react_markdown);
3316
3941
  })]
3317
3942
  });
3318
3943
  }
3319
- const ContentSchema = zod.z.object({
3320
- surfaceId: zod.z.string(),
3321
- target: zod.z.string(),
3322
- catalogId: zod.z.string(),
3323
- operations: zod.z.array(zod.z.any()),
3324
- status: zod.z.enum([
3325
- "streaming",
3326
- "ready",
3327
- "error"
3328
- ]),
3329
- rejected: zod.z.array(zod.z.object({
3330
- index: zod.z.number(),
3331
- reason: zod.z.string()
3332
- })).default([])
3333
- }).passthrough();
3334
3944
  function GenerativeUIChatActivity({ content, agent }) {
3335
3945
  const runtime = useGenerativeUIRuntime();
3336
3946
  const threadId = useCopilotChatConfiguration()?.threadId ?? agent?.threadId;
3337
3947
  const Wrapper = (0, react.useContext)(GenerativeUIChatSlotContext) ?? DefaultGenerativeUIChatCard;
3338
- useManagerVersion(runtime);
3339
- (0, react.useLayoutEffect)(() => {
3340
- if (runtime && threadId) runtime.manager.ingest(threadId, agent?.agentId, content);
3341
- }, [
3948
+ const { surface, rejected, error } = useGenerativeUIActivitySurface({
3342
3949
  runtime,
3343
3950
  threadId,
3344
- agent?.agentId,
3345
- content
3346
- ]);
3347
- if (!runtime || !threadId || content.target !== _copilotkit_shared.GENERATIVE_UI_CHAT_TARGET) return null;
3348
- const surface = runtime.manager.getSurface(threadId, content.surfaceId) ?? runtime.manager.getFailedSurface(threadId, content.surfaceId);
3951
+ agentId: agent?.agentId,
3952
+ content,
3953
+ target: _copilotkit_shared.GENERATIVE_UI_CHAT_TARGET
3954
+ });
3349
3955
  if (!surface) return null;
3350
- const rejected = [...content.rejected ?? [], ...surface.clientRejected];
3351
- const error = rejected.length ? new Error(`${rejected.length} UI update(s) could not be shown`, { cause: rejected }) : void 0;
3352
3956
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Wrapper, {
3353
3957
  surfaceId: content.surfaceId,
3354
3958
  isStreaming: content.status === "streaming",
@@ -3360,7 +3964,7 @@ react_markdown = __toESM(react_markdown);
3360
3964
  function createGenerativeUIActivityRenderer() {
3361
3965
  return {
3362
3966
  activityType: _copilotkit_shared.GENERATIVE_UI_ACTIVITY_TYPE,
3363
- content: ContentSchema,
3967
+ content: _copilotkit_core_generative_ui.GenerativeUISurfaceContentSchema,
3364
3968
  render: GenerativeUIChatActivity
3365
3969
  };
3366
3970
  }