@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
@@ -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.87.0";
1829
1829
 
1830
1830
  //#endregion
1831
1831
  //#region src/v2/components/CopilotKitInspector.tsx
@@ -3053,7 +3053,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3053
3053
  function warnAboutUnpaintedSurfaces(grouped) {
3054
3054
  for (const [surfaceId, operations] of grouped) {
3055
3055
  if (surfaceHasRenderableContent(operations)) continue;
3056
- 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";
3056
+ 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";
3057
3057
  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.`);
3058
3058
  }
3059
3059
  }
@@ -3066,13 +3066,9 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3066
3066
  */
3067
3067
  function describeComponentIds(operations) {
3068
3068
  const ids = [];
3069
- for (const operation of operations) {
3070
- const components = operation?.updateComponents?.components;
3071
- if (!Array.isArray(components)) continue;
3072
- for (const component of components) {
3073
- const id = component?.id;
3074
- if (typeof id === "string" && !ids.includes(id)) ids.push(id);
3075
- }
3069
+ for (const operation of operations) for (const component of getOperationComponents(operation)) {
3070
+ const id = component?.id;
3071
+ if (typeof id === "string" && !ids.includes(id)) ids.push(id);
3076
3072
  }
3077
3073
  return ids.length === 0 ? "none" : ids.map((id) => `"${id}"`).join(", ");
3078
3074
  }
@@ -3099,9 +3095,9 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3099
3095
  */
3100
3096
  function warnAboutUnresolvedRoot(surfaceId, operations, surface) {
3101
3097
  if (surface?.componentsModel?.get?.(_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID)) return;
3102
- const componentOps = operations.filter((o) => o?.updateComponents);
3103
- if (componentOps.length === 0) return;
3104
- 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`;
3098
+ const components = operations.flatMap(getOperationComponents);
3099
+ if (components.length === 0) return;
3100
+ 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`;
3105
3101
  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.`);
3106
3102
  }
3107
3103
  function createA2UIMessageRenderer(options) {
@@ -3267,7 +3263,10 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3267
3263
  surfaceId,
3268
3264
  operations,
3269
3265
  onReady
3270
- }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(A2UISurfaceOrError, { surfaceId })]
3266
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(A2UISurfaceOrError, {
3267
+ surfaceId,
3268
+ onReady
3269
+ })]
3271
3270
  })
3272
3271
  });
3273
3272
  }
@@ -3275,15 +3274,60 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3275
3274
  * Renders the A2UI surface, or an error message if processing failed.
3276
3275
  * Must be a child of A2UIProvider to access the error state.
3277
3276
  */
3278
- function A2UISurfaceOrError({ surfaceId }) {
3277
+ function A2UISurfaceOrError({ surfaceId, onReady }) {
3279
3278
  const error = (0, _copilotkit_a2ui_renderer.useA2UIError)();
3280
- if (error) return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3279
+ (0, react.useEffect)(() => {
3280
+ if (error) onReady?.();
3281
+ }, [error, onReady]);
3282
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [error && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3283
+ role: "alert",
3281
3284
  className: "cpk:rounded-lg cpk:border cpk:border-red-200 cpk:bg-red-50 cpk:p-3 cpk:text-sm cpk:text-red-700",
3282
3285
  children: ["A2UI render error: ", error]
3283
- });
3284
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_copilotkit_a2ui_renderer.A2UIRenderer, {
3286
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_copilotkit_a2ui_renderer.A2UIRenderer, {
3285
3287
  surfaceId,
3286
3288
  className: "cpk:flex cpk:flex-1"
3289
+ })] });
3290
+ }
3291
+ /**
3292
+ * Full activity snapshots repeat creation. Native 1.0 can carry the current
3293
+ * components and data inline there, so replay changed initialization data and
3294
+ * components as updates for an existing surface. Compare against the previous
3295
+ * wire snapshot, rather than the SDK's mutable data model, to retain local edits.
3296
+ * Retain invalid or version-changing creates for the
3297
+ * provider/SDK to reject, instead of sanitizing them while replaying.
3298
+ */
3299
+ function replaySnapshotOperations(operations, existing, previousOperations) {
3300
+ if (!existing) return operations;
3301
+ let nativeSurfaceDeleted = false;
3302
+ return operations.flatMap((operation) => {
3303
+ if (operation?.version === "1.0" && operation.deleteSurface?.surfaceId === existing.id) nativeSurfaceDeleted = true;
3304
+ if (nativeSurfaceDeleted) return [operation];
3305
+ if (!operation?.createSurface) return [operation];
3306
+ const version = String(operation.version ?? "v0.9").replace(/^v/, "");
3307
+ if (version !== String(existing.protocolVersion).replace(/^v/, "")) return [operation];
3308
+ if (version === "0.9") return [];
3309
+ if (operation.version !== "1.0") return [operation];
3310
+ const validation = _copilotkit_a2ui_renderer.CreateSurfaceMessageSchemaV1.safeParse(operation);
3311
+ if (!validation.success) return [operation];
3312
+ const create = validation.data.createSurface;
3313
+ if (create.catalogId !== void 0 && create.catalogId !== existing.catalog?.id) return [operation];
3314
+ const updates = [];
3315
+ const previousCreate = previousOperations.find((previous) => previous?.version === "1.0" && previous.createSurface?.surfaceId === create.surfaceId)?.createSurface;
3316
+ if (Object.hasOwn(create, "dataModel") && (!previousCreate || !Object.hasOwn(previousCreate, "dataModel") || JSON.stringify(create.dataModel) !== JSON.stringify(previousCreate.dataModel))) updates.push({
3317
+ version: "1.0",
3318
+ updateDataModel: {
3319
+ surfaceId: create.surfaceId,
3320
+ value: create.dataModel
3321
+ }
3322
+ });
3323
+ if (Object.hasOwn(create, "components")) updates.push({
3324
+ version: "1.0",
3325
+ updateComponents: {
3326
+ surfaceId: create.surfaceId,
3327
+ components: create.components
3328
+ }
3329
+ });
3330
+ return updates;
3287
3331
  });
3288
3332
  }
3289
3333
  /**
@@ -3296,10 +3340,11 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3296
3340
  (0, react.useEffect)(() => {
3297
3341
  const hash = JSON.stringify(operations);
3298
3342
  if (hash === lastHashRef.current) return;
3343
+ const previousOperations = lastHashRef.current ? JSON.parse(lastHashRef.current) : [];
3299
3344
  lastHashRef.current = hash;
3300
- const ops = getSurface(surfaceId) ? operations.filter((op) => !op?.createSurface) : operations;
3345
+ const ops = replaySnapshotOperations(operations, getSurface(surfaceId), previousOperations);
3301
3346
  processMessages(ops.map(stripRoutingSurfaceId));
3302
- if (onReady && surfaceHasRenderableContent(operations)) onReady();
3347
+ if (onReady && getSurface(surfaceId)?.componentsModel.get(_copilotkit_a2ui_renderer.ROOT_COMPONENT_ID) && surfaceHasRenderableContent(operations)) onReady();
3303
3348
  if (!IS_DEVELOPMENT) return;
3304
3349
  if (!getSurface(surfaceId)) {
3305
3350
  const missingSurfaceCheck = setTimeout(() => {
@@ -3329,15 +3374,26 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3329
3374
  * arrival rather than a fixed delay. (OSS-162)
3330
3375
  */
3331
3376
  function surfaceHasRenderableContent(operations) {
3332
- const componentOps = operations.filter((o) => o?.updateComponents);
3333
- if (!componentOps.length) return false;
3334
- if (!JSON.stringify(componentOps).includes("\"path\"")) return true;
3377
+ const components = operations.flatMap(getOperationComponents);
3378
+ if (!components.length) return false;
3379
+ const serialized = JSON.stringify(components);
3380
+ if (!(serialized.includes("\"path\"") || serialized.includes("\"@path\""))) return true;
3335
3381
  return operations.some((o) => {
3336
- const v = o?.updateDataModel?.value;
3382
+ const v = o?.updateDataModel?.value ?? (o?.version === "1.0" ? o?.createSurface?.dataModel : void 0);
3383
+ if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
3384
+ const path = o?.updateDataModel?.path ?? "/";
3385
+ const key = o?.version === "1.0" ? "@path" : "path";
3386
+ return v !== "" && serialized.includes(JSON.stringify({ [key]: path }));
3387
+ }
3337
3388
  if (!v || typeof v !== "object") return false;
3338
3389
  return Object.values(v).some((x) => Array.isArray(x) ? x.length > 0 : x !== null && x !== void 0 && x !== "");
3339
3390
  });
3340
3391
  }
3392
+ /** Component definitions can arrive in an update or a native inline create. */
3393
+ function getOperationComponents(operation) {
3394
+ const components = operation?.updateComponents?.components ?? (operation?.version === "1.0" ? operation?.createSurface?.components : void 0);
3395
+ return Array.isArray(components) ? components : [];
3396
+ }
3341
3397
  const V0_9_ACTION_KEYS = [
3342
3398
  "createSurface",
3343
3399
  "updateComponents",
@@ -3360,7 +3416,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3360
3416
  * message, otherwise the operation unchanged.
3361
3417
  */
3362
3418
  function stripRoutingSurfaceId(operation) {
3363
- if (!operation || typeof operation !== "object" || !("surfaceId" in operation) || !V0_9_ACTION_KEYS.some((key) => key in operation)) return operation;
3419
+ 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;
3364
3420
  const { surfaceId: _routingId, ...message } = operation;
3365
3421
  return message;
3366
3422
  }
@@ -3433,6 +3489,47 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3433
3489
  return null;
3434
3490
  }
3435
3491
 
3492
+ //#endregion
3493
+ //#region src/v2/a2ui/generation-guidelines.ts
3494
+ /** Native 1.0 tool guidance. Structural template paths retain their own syntax. */
3495
+ const A2UI_V1_GENERATION_GUIDELINES = `Generate A2UI 1.0 JSON.
3496
+
3497
+ Call render_a2ui with a unique surfaceId, the required components array, and optional
3498
+ root data object. Use only component names, properties and functions declared in
3499
+ the supplied catalog. Match its protocolVersion, catalogId and schemas.
3500
+
3501
+ Every component has a unique id and a component type. Exactly one component has
3502
+ id="root", the surface entry point. All other components must be reachable from
3503
+ root through child/children; never reference the same component as its own child,
3504
+ and never create a cycle.
3505
+
3506
+ Use literal values unless the property's catalog schema permits a dynamic value.
3507
+ Native value binding uses { "@path": "/form/name" }. A plain property schema does
3508
+ not accept a binding object. Pre-fill editable values via the data argument:
3509
+ "data": { "form": { "name": "Alice" } }
3510
+ "value": { "@path": "/form/name" }
3511
+
3512
+ Native function expressions use { "@call": "functionName", "args": { ... } },
3513
+ with only the parameters declared by the catalog; do not add returnType.
3514
+
3515
+ REPEATING CONTENT uses a structural children format, distinct from value binding:
3516
+ children: { componentId: "card-id", path: "/items" }
3517
+ The structural key remains path. Initialize its data array with real items before
3518
+ expecting a visible list. Components inside a template use relative value bindings:
3519
+ "text": { "@path": "name" }
3520
+ An absolute binding such as { "@path": "/name" } reads from the root instead.
3521
+
3522
+ Buttons send nested event actions, with bound context to capture edited values:
3523
+ "action": { "event": { "name": "submit", "context": { "name": { "@path": "/form/name" } } } }
3524
+ Local function actions use { "functionCall": { "@call": "functionName", "args": { ... } } }.
3525
+ Do not rename literal objects' path keys: only @path denotes native value binding.
3526
+ Keep an existing surface's protocol and catalog when updating it; create a new
3527
+ surface to use a different protocol.`;
3528
+ /** Preserve legacy prompt text exactly unless the selected catalog opts into 1.0. */
3529
+ function a2uiGenerationGuidelines(protocolVersion) {
3530
+ return protocolVersion === "1.0" ? A2UI_V1_GENERATION_GUIDELINES : _copilotkit_shared.A2UI_DEFAULT_GENERATION_GUIDELINES;
3531
+ }
3532
+
3436
3533
  //#endregion
3437
3534
  //#region src/v2/a2ui/A2UICatalogContext.tsx
3438
3535
  /**
@@ -3452,6 +3549,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3452
3549
  const capabilitiesValue = (0, react.useMemo)(() => (0, _copilotkit_a2ui_renderer.buildCatalogContextValue)(catalog, { includeSchema: includeSchema !== false }), [catalog, includeSchema]);
3453
3550
  const schemaValue = (0, react.useMemo)(() => includeSchema !== false ? JSON.stringify((0, _copilotkit_a2ui_renderer.extractCatalogComponentSchemas)(catalog)) : null, [catalog, includeSchema]);
3454
3551
  const a2uiAgentsKey = copilotkit?.a2uiAgents?.join(",");
3552
+ const generationGuidelines = a2uiGenerationGuidelines(catalog?.protocolVersion);
3455
3553
  (0, react.useLayoutEffect)(() => {
3456
3554
  if (!copilotkit) return;
3457
3555
  const agentIds = copilotkit.a2uiAgents;
@@ -3470,7 +3568,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3470
3568
  }));
3471
3569
  ids.push(copilotkit.addContext({
3472
3570
  description: "A2UI generation guidelines — protocol rules, tool arguments, path rules, data model format, and form/two-way-binding instructions.",
3473
- value: _copilotkit_shared.A2UI_DEFAULT_GENERATION_GUIDELINES,
3571
+ value: generationGuidelines,
3474
3572
  ...scope
3475
3573
  }));
3476
3574
  ids.push(copilotkit.addContext({
@@ -3486,11 +3584,87 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3486
3584
  copilotkit,
3487
3585
  capabilitiesValue,
3488
3586
  schemaValue,
3489
- a2uiAgentsKey
3587
+ a2uiAgentsKey,
3588
+ generationGuidelines
3490
3589
  ]);
3491
3590
  return null;
3492
3591
  }
3493
3592
 
3593
+ //#endregion
3594
+ //#region src/v2/generative-ui/runtime-context.ts
3595
+ const GenerativeUIRuntimeContext = (0, react.createContext)(null);
3596
+ function useGenerativeUIRuntime() {
3597
+ return (0, react.useContext)(GenerativeUIRuntimeContext);
3598
+ }
3599
+ function useRequiredGenerativeUIRuntime() {
3600
+ const runtime = (0, react.useContext)(GenerativeUIRuntimeContext);
3601
+ 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.");
3602
+ return runtime;
3603
+ }
3604
+ const GenerativeUINodeContext = (0, react.createContext)(null);
3605
+
3606
+ //#endregion
3607
+ //#region src/v2/generative-ui/hooks.ts
3608
+ const noopSubscribe$1 = () => () => {};
3609
+ const zero = () => 0;
3610
+ /** Re-renders on any surface-manager change. */
3611
+ function useManagerVersion(runtime) {
3612
+ return (0, react.useSyncExternalStore)(runtime?.manager.subscribe ?? noopSubscribe$1, runtime?.manager.getVersion ?? zero, zero);
3613
+ }
3614
+
3615
+ //#endregion
3616
+ //#region src/v2/generative-ui/activity.ts
3617
+ /**
3618
+ * The rejected operations of a surface (the runtime's first, then the
3619
+ * client's) and the error that goes with them, for a wrapper: a generic
3620
+ * message, with the same list as its `cause`. The `cause` is not enumerable,
3621
+ * so `JSON.stringify`, a spread and error reporters leave the list out.
3622
+ * `clientRejected` is appended to in place by the manager, so its length is
3623
+ * part of the key.
3624
+ */
3625
+ function useGenerativeUIRejected(serverRejected, clientRejected) {
3626
+ const rejected = (0, react.useMemo)(() => [...serverRejected ?? [], ...clientRejected ?? []], [
3627
+ serverRejected,
3628
+ clientRejected,
3629
+ clientRejected?.length ?? 0
3630
+ ]);
3631
+ return {
3632
+ rejected,
3633
+ 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])
3634
+ };
3635
+ }
3636
+ /**
3637
+ * What a renderer of one surface activity message shows: the surface (or the
3638
+ * failed surface whose `createSurface` was rejected), and its rejected
3639
+ * operations with their error (see {@link useGenerativeUIRejected}). It
3640
+ * ingests `content` into the manager (idempotent: the root ingests the
3641
+ * agent's messages too, and this covers an agent the root has not seen yet),
3642
+ * and renders again on every change of the manager.
3643
+ *
3644
+ * `surface` is undefined when there is no runtime, thread or content, when
3645
+ * the surface's target is not `target`, or when the surface does not exist
3646
+ * (the agent deleted it). Both rejected lists cover only the latest
3647
+ * `render_ui` call (see the manager's ingest).
3648
+ */
3649
+ function useGenerativeUIActivitySurface({ runtime, threadId, agentId, content, target }) {
3650
+ useManagerVersion(runtime);
3651
+ (0, react.useLayoutEffect)(() => {
3652
+ if (runtime && threadId && content) runtime.manager.ingest(threadId, agentId, content);
3653
+ }, [
3654
+ runtime,
3655
+ threadId,
3656
+ agentId,
3657
+ content
3658
+ ]);
3659
+ const surface = runtime && threadId && content && content.target === target ? runtime.manager.getSurface(threadId, content.surfaceId) ?? runtime.manager.getFailedSurface(threadId, content.surfaceId) : void 0;
3660
+ const { rejected, error } = useGenerativeUIRejected(content?.rejected, surface?.clientRejected);
3661
+ return {
3662
+ surface,
3663
+ rejected,
3664
+ error
3665
+ };
3666
+ }
3667
+
3494
3668
  //#endregion
3495
3669
  //#region src/v2/generative-ui/markdown/context.ts
3496
3670
  /**
@@ -3572,9 +3746,38 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3572
3746
  //#endregion
3573
3747
  //#region src/v2/generative-ui/use-generative-ui-root.ts
3574
3748
  const isDevelopment = () => process.env.NODE_ENV !== "production";
3749
+ /**
3750
+ * The malformed-content warnings already printed, by manager, so each
3751
+ * distinct issue warns once while the manager lives.
3752
+ */
3753
+ const warnedMalformed = /* @__PURE__ */ new WeakMap();
3754
+ /**
3755
+ * Ingests the surface activity messages of `messages`. A message whose
3756
+ * content is malformed (for example `null`) is skipped, in development with a
3757
+ * warning once for each distinct issue, so it cannot throw inside the
3758
+ * agent's run; the messages after it are still ingested.
3759
+ */
3575
3760
  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);
3761
+ const { threadId } = agent;
3762
+ if (!threadId) return;
3763
+ for (const message of messages) {
3764
+ if (message.role !== "activity" || message.activityType !== _copilotkit_shared.GENERATIVE_UI_ACTIVITY_TYPE) continue;
3765
+ const parsed = (0, _copilotkit_core_generative_ui.parseGenerativeUISurfaceContent)(message.content);
3766
+ if (parsed.content) {
3767
+ manager.ingest(threadId, agent.agentId, parsed.content);
3768
+ continue;
3769
+ }
3770
+ if (!isDevelopment()) continue;
3771
+ const warning = `[generativeUI] skipped an activity message of thread ${JSON.stringify(threadId)}: its content is malformed: ${parsed.issues.join("; ")}.`;
3772
+ let warned = warnedMalformed.get(manager);
3773
+ if (!warned) {
3774
+ warned = /* @__PURE__ */ new Set();
3775
+ warnedMalformed.set(manager, warned);
3776
+ }
3777
+ if (warned.has(warning)) continue;
3778
+ warned.add(warning);
3779
+ console.warn(warning);
3780
+ }
3578
3781
  }
3579
3782
  /** Provider-side wiring: manager, dispatcher, agent subscriptions, envelope. */
3580
3783
  function useGenerativeUIRoot(copilotkit, config) {
@@ -3840,29 +4043,35 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3840
4043
  }
3841
4044
 
3842
4045
  //#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;
4046
+ //#region src/v2/generative-ui/host.tsx
4047
+ const DISPLAY_CONTENTS = { display: "contents" };
4048
+ /**
4049
+ * The root element is always there, so a theme change does not remount the
4050
+ * surface. `display: contents` keeps it out of the layout, and the CSS
4051
+ * variables still reach every component of the surface. The `a2ui-surface`
4052
+ * class scopes the standard catalog's surface CSS rules. The theme is a
4053
+ * stylesheet, not an inline style, so that its `dark` values can apply in
4054
+ * dark mode only. The values are checked in `themeStyleSheet`, and the HTML
4055
+ * is set as is, so the server render keeps the quotes of the rule.
4056
+ */
4057
+ function DomSurfaceRoot({ surfaceId, theme, children }) {
4058
+ const sheet = react.default.useMemo(() => (0, _copilotkit_shared_generative_ui.themeStyleSheet)(theme), [theme]);
4059
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
4060
+ className: "a2ui-surface",
4061
+ "data-generative-ui-root": surfaceId,
4062
+ "data-a2ui-theme": sheet?.id,
4063
+ style: DISPLAY_CONTENTS,
4064
+ children: [sheet ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("style", { dangerouslySetInnerHTML: { __html: sheet.css } }) : null, children]
4065
+ });
3852
4066
  }
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);
4067
+ function DomAccessibilityWrapper({ attributes, children }) {
4068
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
4069
+ "data-a2ui-accessibility": "",
4070
+ style: DISPLAY_CONTENTS,
4071
+ ...attributes,
4072
+ children
4073
+ });
3862
4074
  }
3863
-
3864
- //#endregion
3865
- //#region src/v2/generative-ui/boundaries.tsx
3866
4075
  function DefaultNodeFallback({ message, componentId, retry }) {
3867
4076
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
3868
4077
  role: "alert",
@@ -3890,7 +4099,24 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3890
4099
  ]
3891
4100
  });
3892
4101
  }
4102
+ /** The DOM host: what a surface draws on the web. */
4103
+ const domGenerativeUIHost = {
4104
+ SurfaceRoot: DomSurfaceRoot,
4105
+ AccessibilityWrapper: DomAccessibilityWrapper,
4106
+ NodeFallback: DefaultNodeFallback
4107
+ };
3893
4108
  /**
4109
+ * The host elements of the surfaces below. Defaults to the DOM host, so a
4110
+ * web app never sets it.
4111
+ */
4112
+ const GenerativeUIHostContext = (0, react.createContext)(domGenerativeUIHost);
4113
+ function useGenerativeUIHost() {
4114
+ return (0, react.useContext)(GenerativeUIHostContext);
4115
+ }
4116
+
4117
+ //#endregion
4118
+ //#region src/v2/generative-ui/boundaries.tsx
4119
+ /**
3894
4120
  * A child that has not streamed yet renders nothing. The prose keeps
3895
4121
  * streaming above the surface, and each component appears as soon as it is
3896
4122
  * complete. A grey block in its place would read as a broken layout.
@@ -3898,18 +4124,188 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3898
4124
  function NodePlaceholder(_props) {
3899
4125
  return null;
3900
4126
  }
3901
- /** Per-node boundary. Resets on retry or a new agent revision, not on data ticks. */
3902
- var NodeBoundary = class extends react.Component {
4127
+ /**
4128
+ * The error that a fallback threw, told apart from the error of the
4129
+ * component it stood in for. The fallback's own error is the `cause`.
4130
+ */
4131
+ function fallbackError(error, componentId) {
4132
+ const subject = componentId === void 0 ? "the surface" : componentId;
4133
+ return (0, _copilotkit_shared_generative_ui.withCause)(/* @__PURE__ */ new Error(`The error fallback of ${subject} threw: ${error.message}`), error);
4134
+ }
4135
+ function createFailureLog() {
4136
+ const last = /* @__PURE__ */ new Map();
4137
+ const released = /* @__PURE__ */ new Set();
4138
+ const releasedSinceSettle = /* @__PURE__ */ new Set();
4139
+ const heldBack = /* @__PURE__ */ new Map();
4140
+ return {
4141
+ report(runtime, slot, info, context = "") {
4142
+ released.delete(slot);
4143
+ const signature = [
4144
+ info.kind,
4145
+ info.error.name,
4146
+ info.error.message,
4147
+ context
4148
+ ].join("\n");
4149
+ if (last.get(slot) === signature) {
4150
+ if (releasedSinceSettle.has(slot)) heldBack.set(slot, {
4151
+ runtime,
4152
+ info,
4153
+ signature
4154
+ });
4155
+ return;
4156
+ }
4157
+ heldBack.delete(slot);
4158
+ last.set(slot, signature);
4159
+ (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, info);
4160
+ },
4161
+ clear(slot) {
4162
+ released.delete(slot);
4163
+ last.delete(slot);
4164
+ heldBack.delete(slot);
4165
+ },
4166
+ retain(slot) {
4167
+ released.delete(slot);
4168
+ },
4169
+ release(slot) {
4170
+ heldBack.delete(slot);
4171
+ if (!last.has(slot)) return;
4172
+ released.add(slot);
4173
+ releasedSinceSettle.add(slot);
4174
+ },
4175
+ settle(missed = false) {
4176
+ for (const slot of missed ? releasedSinceSettle : released) last.delete(slot);
4177
+ if (missed) for (const [slot, { runtime, info, signature }] of heldBack) {
4178
+ if (!releasedSinceSettle.has(slot) || last.has(slot)) continue;
4179
+ last.set(slot, signature);
4180
+ (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, info);
4181
+ }
4182
+ heldBack.clear();
4183
+ released.clear();
4184
+ releasedSinceSettle.clear();
4185
+ }
4186
+ };
4187
+ }
4188
+ /** Outside a surface, every failure is reported. */
4189
+ const UNLOGGED = {
4190
+ report: (runtime, _slot, info) => (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, info),
4191
+ clear: () => {},
4192
+ retain: () => {},
4193
+ release: () => {},
4194
+ settle: () => {}
4195
+ };
4196
+ /** The failure log of the surface being rendered; set by `SurfaceView`. */
4197
+ const GenerativeUIFailureLogContext = (0, react.createContext)(UNLOGGED);
4198
+ function useFailureLog() {
4199
+ return (0, react.useContext)(GenerativeUIFailureLogContext);
4200
+ }
4201
+ /**
4202
+ * The slots of a failure log, per node place (one rendered copy of a node,
4203
+ * see `placeOf` in SurfaceView) and surface boundary.
4204
+ */
4205
+ const failureSlots = {
4206
+ node: (place) => `node ${place}`,
4207
+ appFallback: (place) => `app-fallback ${place}`,
4208
+ renderFallback: (place) => `render-fallback ${place}`,
4209
+ fallback: (place) => `fallback ${place}`,
4210
+ cycle: (place) => `cycle ${place}`,
4211
+ props: (place) => `props ${place}`,
4212
+ surface: (level) => `surface ${level}`,
4213
+ surfaceFallback: (level) => `surface-fallback ${level}`
4214
+ };
4215
+ function sameKeys(a, b) {
4216
+ return a.length === b.length && a.every((value, i) => Object.is(value, b[i]));
4217
+ }
4218
+ var FallbackGuard = class extends react.Component {
3903
4219
  constructor(..._args) {
3904
4220
  super(..._args);
4221
+ this.state = {
4222
+ failed: false,
4223
+ resetKeys: this.props.resetKeys
4224
+ };
4225
+ }
4226
+ static getDerivedStateFromError(thrown) {
4227
+ return {
4228
+ failed: true,
4229
+ error: (0, _copilotkit_shared_generative_ui.toError)(thrown)
4230
+ };
4231
+ }
4232
+ static getDerivedStateFromProps(props, state) {
4233
+ return sameKeys(props.resetKeys, state.resetKeys) ? null : {
4234
+ failed: false,
4235
+ error: void 0,
4236
+ resetKeys: props.resetKeys
4237
+ };
4238
+ }
4239
+ report(error) {
4240
+ const { runtime, surfaceId, componentId, log, slot, context } = this.props;
4241
+ log.report(runtime, slot, {
4242
+ kind: "render",
4243
+ surfaceId,
4244
+ ...componentId === void 0 ? {} : { componentId },
4245
+ error: fallbackError(error, componentId)
4246
+ }, context);
4247
+ }
4248
+ componentDidCatch(thrown) {
4249
+ this.report(this.state.error ?? (0, _copilotkit_shared_generative_ui.toError)(thrown));
4250
+ }
4251
+ componentDidMount() {
4252
+ if (!this.state.failed) {
4253
+ this.props.log.clear(this.props.slot);
4254
+ return;
4255
+ }
4256
+ if (this.state.error) this.report(this.state.error);
4257
+ }
4258
+ componentDidUpdate() {
4259
+ if (!this.state.failed) this.props.log.clear(this.props.slot);
4260
+ }
4261
+ render() {
4262
+ return this.state.failed ? this.props.replacement ?? null : this.props.children;
4263
+ }
4264
+ };
4265
+ /**
4266
+ * The host's `NodeFallback`, guarded: when it throws, the node renders
4267
+ * nothing and the error is reported, and the parent node keeps rendering.
4268
+ * Every place that shows the host fallback goes through this.
4269
+ */
4270
+ function GuardedNodeFallback({ surfaceId, componentId, runtime, message, retry, slot, revision }) {
4271
+ const { NodeFallback } = useGenerativeUIHost();
4272
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FallbackGuard, {
4273
+ surfaceId,
4274
+ componentId,
4275
+ runtime,
4276
+ log: useFailureLog(),
4277
+ slot,
4278
+ context: message,
4279
+ resetKeys: [message, revision],
4280
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeFallback, {
4281
+ message,
4282
+ componentId: componentId ?? "surface",
4283
+ retry
4284
+ })
4285
+ });
4286
+ }
4287
+ /**
4288
+ * Calls the app's `errorBoundary.fallback` in a render of its own, so that
4289
+ * the guard above it catches what it throws.
4290
+ */
4291
+ function AppErrorFallback({ render, ...props }) {
4292
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: render(props) });
4293
+ }
4294
+ /**
4295
+ * Per-node boundary. Resets on retry or a new agent revision, not on data
4296
+ * ticks. A failure that stays the same across revisions is reported once.
4297
+ */
4298
+ var NodeBoundary = class extends react.Component {
4299
+ constructor(..._args2) {
4300
+ super(..._args2);
3905
4301
  this.state = {
3906
4302
  error: null,
3907
4303
  revision: this.props.revision
3908
4304
  };
3909
4305
  this.retry = () => this.setState({ error: null });
3910
4306
  }
3911
- static getDerivedStateFromError(error) {
3912
- return { error };
4307
+ static getDerivedStateFromError(thrown) {
4308
+ return { error: (0, _copilotkit_shared_generative_ui.toError)(thrown) };
3913
4309
  }
3914
4310
  static getDerivedStateFromProps(props, state) {
3915
4311
  return props.revision !== state.revision ? {
@@ -3917,53 +4313,138 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3917
4313
  revision: props.revision
3918
4314
  } : null;
3919
4315
  }
3920
- componentDidCatch(error) {
3921
- const { runtime, surfaceId, componentId } = this.props;
3922
- (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, {
4316
+ componentDidCatch(thrown) {
4317
+ const { runtime, surfaceId, componentId, log, place } = this.props;
4318
+ log.clear(failureSlots.fallback(place));
4319
+ log.report(runtime, failureSlots.node(place), {
3923
4320
  kind: "render",
3924
4321
  surfaceId,
3925
4322
  componentId,
3926
- error
4323
+ error: this.state.error ?? (0, _copilotkit_shared_generative_ui.toError)(thrown)
4324
+ });
4325
+ }
4326
+ slots() {
4327
+ const { place } = this.props;
4328
+ return [
4329
+ failureSlots.node(place),
4330
+ failureSlots.appFallback(place),
4331
+ failureSlots.renderFallback(place)
4332
+ ];
4333
+ }
4334
+ clearSlots() {
4335
+ for (const slot of this.slots()) this.props.log.clear(slot);
4336
+ }
4337
+ componentDidMount() {
4338
+ for (const slot of this.slots()) this.props.log.retain(slot);
4339
+ if (!this.state.error) {
4340
+ this.clearSlots();
4341
+ return;
4342
+ }
4343
+ const { runtime, surfaceId, componentId, log, place } = this.props;
4344
+ log.report(runtime, failureSlots.node(place), {
4345
+ kind: "render",
4346
+ surfaceId,
4347
+ componentId,
4348
+ error: this.state.error
3927
4349
  });
3928
4350
  }
4351
+ componentDidUpdate(prevProps) {
4352
+ if (prevProps.place !== this.props.place) {
4353
+ const { log } = this.props;
4354
+ log.release(failureSlots.node(prevProps.place));
4355
+ log.release(failureSlots.appFallback(prevProps.place));
4356
+ log.release(failureSlots.renderFallback(prevProps.place));
4357
+ for (const slot of this.slots()) log.retain(slot);
4358
+ }
4359
+ if (!this.state.error) this.clearSlots();
4360
+ }
4361
+ componentWillUnmount() {
4362
+ for (const slot of this.slots()) this.props.log.release(slot);
4363
+ }
3929
4364
  render() {
3930
4365
  const { error } = this.state;
3931
4366
  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,
4367
+ const { runtime, surfaceId, componentId, place, log } = this.props;
4368
+ const hostFallback = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
3937
4369
  surfaceId,
3938
- componentId
3939
- }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
4370
+ componentId,
4371
+ runtime,
3940
4372
  message: error.message,
4373
+ retry: this.retry,
4374
+ slot: failureSlots.renderFallback(place)
4375
+ });
4376
+ const fallback = runtime.errorFallback;
4377
+ if (!fallback) return hostFallback;
4378
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FallbackGuard, {
4379
+ surfaceId,
3941
4380
  componentId,
3942
- retry: this.retry
4381
+ runtime,
4382
+ log,
4383
+ slot: failureSlots.appFallback(place),
4384
+ context: error.message,
4385
+ resetKeys: [error],
4386
+ replacement: hostFallback,
4387
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AppErrorFallback, {
4388
+ render: fallback,
4389
+ error,
4390
+ retry: this.retry,
4391
+ surfaceId,
4392
+ componentId
4393
+ })
3943
4394
  });
3944
4395
  }
3945
4396
  };
3946
- /** Outer boundary: a failure here must not reach the chat or the host page. */
4397
+ /**
4398
+ * Surface boundary: a failure here must not reach the chat or the host page.
4399
+ * It resets on a new revision, and on Retry when `retryable`. A failure that
4400
+ * stays the same across revisions is reported once.
4401
+ */
3947
4402
  var SurfaceBoundary = class extends react.Component {
3948
- constructor(..._args2) {
3949
- super(..._args2);
3950
- this.state = { error: null };
4403
+ constructor(..._args3) {
4404
+ super(..._args3);
4405
+ this.state = {
4406
+ error: null,
4407
+ revision: this.props.revision
4408
+ };
4409
+ this.retry = () => this.setState({ error: null });
3951
4410
  }
3952
- static getDerivedStateFromError(error) {
3953
- return { error };
4411
+ static getDerivedStateFromError(thrown) {
4412
+ return { error: (0, _copilotkit_shared_generative_ui.toError)(thrown) };
3954
4413
  }
3955
- componentDidCatch(error) {
3956
- (0, _copilotkit_core_generative_ui.reportErrorSafely)(this.props.runtime, {
4414
+ static getDerivedStateFromProps(props, state) {
4415
+ return props.revision !== state.revision ? {
4416
+ error: null,
4417
+ revision: props.revision
4418
+ } : null;
4419
+ }
4420
+ componentDidCatch(thrown) {
4421
+ const { runtime, surfaceId, log, level } = this.props;
4422
+ log.report(runtime, failureSlots.surface(level), {
3957
4423
  kind: "render",
3958
- surfaceId: this.props.surfaceId,
3959
- error
4424
+ surfaceId,
4425
+ error: this.state.error ?? (0, _copilotkit_shared_generative_ui.toError)(thrown)
3960
4426
  });
3961
4427
  }
4428
+ clearSlots() {
4429
+ const { log, level } = this.props;
4430
+ log.clear(failureSlots.surface(level));
4431
+ log.clear(failureSlots.surfaceFallback(level));
4432
+ }
4433
+ componentDidMount() {
4434
+ if (!this.state.error) this.clearSlots();
4435
+ }
4436
+ componentDidUpdate() {
4437
+ if (!this.state.error) this.clearSlots();
4438
+ }
3962
4439
  render() {
3963
4440
  if (!this.state.error) return this.props.children;
3964
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
4441
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
4442
+ surfaceId: this.props.surfaceId,
4443
+ componentId: void 0,
4444
+ runtime: this.props.runtime,
3965
4445
  message: "this generated UI failed to render",
3966
- componentId: "surface"
4446
+ retry: this.props.retryable ? this.retry : void 0,
4447
+ slot: failureSlots.surfaceFallback(this.props.level)
3967
4448
  });
3968
4449
  }
3969
4450
  };
@@ -3971,7 +4452,6 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3971
4452
  //#endregion
3972
4453
  //#region src/v2/generative-ui/SurfaceView.tsx
3973
4454
  const NO_ANCESTORS = [];
3974
- const ROOT_STYLE = { display: "contents" };
3975
4455
  const undefinedSnapshot = () => void 0;
3976
4456
  const noopSubscribe = () => () => {};
3977
4457
  const zeroSnapshot = () => 0;
@@ -3981,23 +4461,80 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
3981
4461
  }
3982
4462
  function SurfaceView({ surface }) {
3983
4463
  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, {
4464
+ const { SurfaceRoot } = useGenerativeUIHost();
4465
+ const appliedCount = (0, react.useSyncExternalStore)(runtime.manager.subscribe, () => surface.operations.length, undefinedSnapshot);
4466
+ const revision = (0, react.useMemo)(() => ({
4467
+ surface,
4468
+ appliedCount
4469
+ }), [surface, appliedCount]);
4470
+ const [scopedLog, setScopedLog] = (0, react.useState)(() => ({
4471
+ key: surface.key,
4472
+ log: createFailureLog()
4473
+ }));
4474
+ let failureLog = scopedLog.log;
4475
+ if (scopedLog.key !== surface.key) {
4476
+ const next = {
4477
+ key: surface.key,
4478
+ log: createFailureLog()
4479
+ };
4480
+ setScopedLog(next);
4481
+ failureLog = next.log;
4482
+ }
4483
+ const { threadId, surfaceId } = surface;
4484
+ const { manager } = runtime;
4485
+ const seen = (0, react.useRef)(void 0);
4486
+ (0, react.useLayoutEffect)(() => {
4487
+ const contentOf = () => {
4488
+ const current = manager.getSurface(threadId, surfaceId);
4489
+ return {
4490
+ log: failureLog,
4491
+ current,
4492
+ applied: current?.operations.length
4493
+ };
4494
+ };
4495
+ const observe = (missed) => {
4496
+ const last = seen.current;
4497
+ const next = contentOf();
4498
+ seen.current = next;
4499
+ if (last?.log === failureLog && (next.current !== last.current || next.applied !== last.applied)) failureLog.settle(missed);
4500
+ };
4501
+ observe(true);
4502
+ return manager.subscribe(() => observe(false));
4503
+ }, [
4504
+ manager,
4505
+ threadId,
4506
+ surfaceId,
4507
+ failureLog
4508
+ ]);
4509
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GenerativeUIFailureLogContext.Provider, {
4510
+ value: failureLog,
4511
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceBoundary, {
3992
4512
  surfaceId: surface.surfaceId,
3993
4513
  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
4514
+ log: failureLog,
4515
+ level: "outer",
4516
+ revision,
4517
+ retryable: true,
4518
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceRoot, {
4519
+ surfaceId: surface.surfaceId,
4520
+ theme: runtime.theme,
4521
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SurfaceBoundary, {
4522
+ surfaceId: surface.surfaceId,
4523
+ runtime,
4524
+ log: failureLog,
4525
+ level: "inner",
4526
+ revision,
4527
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
4528
+ surface,
4529
+ componentId: _copilotkit_shared.GENERATIVE_UI_ROOT_ID,
4530
+ basePath: void 0,
4531
+ ancestors: NO_ANCESTORS,
4532
+ parentPlace: "",
4533
+ via: ""
4534
+ })
4535
+ })
3999
4536
  })
4000
- })]
4537
+ })
4001
4538
  });
4002
4539
  }
4003
4540
  /**
@@ -4022,27 +4559,63 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4022
4559
  * repeats both.
4023
4560
  */
4024
4561
  function scopedId(componentId, basePath) {
4025
- return `${basePath ?? ""}#${componentId}`;
4562
+ return `${basePath === void 0 ? "" : JSON.stringify(basePath)}#${JSON.stringify(componentId)}`;
4026
4563
  }
4027
- const NodeView = (0, react.memo)(function NodeView({ surface, componentId, basePath, ancestors }) {
4564
+ /**
4565
+ * Where one rendered copy of a node is: the place of its parent, the slot
4566
+ * or child reference of the parent it renders in, and its scoped id. Names
4567
+ * the node's failure-log slots. A scoped id alone is not enough: the same
4568
+ * id can render under two parents, and a cyclic reference repeats the
4569
+ * scoped id of an ancestor. Each copy keeps its own memory of what it
4570
+ * reported, so one copy that renders, goes away or remounts does not clear
4571
+ * the memory of another that still fails.
4572
+ */
4573
+ function placeOf(parentPlace, via, position) {
4574
+ return `${parentPlace}/${via === "" ? "" : JSON.stringify(via)}${position}`;
4575
+ }
4576
+ const NodeView = (0, react.memo)(function NodeView({ surface, componentId, basePath, ancestors, parentPlace, via }) {
4028
4577
  const runtime = useRequiredGenerativeUIRuntime();
4578
+ const log = useFailureLog();
4029
4579
  const component = useComponentModel(runtime.manager, surface.model, componentId);
4030
4580
  const properties = useComponentProperties(runtime.manager, component);
4031
- if (ancestors.includes(scopedId(componentId, basePath))) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
4581
+ const position = scopedId(componentId, basePath);
4582
+ const place = placeOf(parentPlace, via, position);
4583
+ (0, react.useEffect)(() => {
4584
+ const slots = [
4585
+ failureSlots.fallback(place),
4586
+ failureSlots.cycle(place),
4587
+ failureSlots.props(place)
4588
+ ];
4589
+ for (const slot of slots) log.retain(slot);
4590
+ return () => {
4591
+ for (const slot of slots) log.release(slot);
4592
+ };
4593
+ }, [log, place]);
4594
+ if (ancestors.includes(position)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
4595
+ surfaceId: surface.surfaceId,
4596
+ componentId,
4597
+ runtime,
4032
4598
  message: `cyclic reference to ${componentId}`,
4033
- componentId
4599
+ slot: failureSlots.cycle(place),
4600
+ revision: properties
4034
4601
  });
4035
4602
  if (!component || !properties) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodePlaceholder, { componentId });
4036
4603
  const definition = runtime.ui.components.get(component.type);
4037
- if (!definition) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
4604
+ if (!definition) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
4605
+ surfaceId: surface.surfaceId,
4606
+ componentId,
4607
+ runtime,
4038
4608
  message: `unknown component "${component.type}"`,
4039
- componentId
4609
+ slot: failureSlots.fallback(place),
4610
+ revision: properties
4040
4611
  });
4041
4612
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeBoundary, {
4042
4613
  surfaceId: surface.surfaceId,
4043
4614
  componentId,
4615
+ place,
4044
4616
  revision: properties,
4045
4617
  runtime,
4618
+ log,
4046
4619
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ResolvedNode, {
4047
4620
  surface,
4048
4621
  componentId,
@@ -4050,7 +4623,8 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4050
4623
  properties,
4051
4624
  runtime,
4052
4625
  basePath,
4053
- ancestors
4626
+ ancestors,
4627
+ place
4054
4628
  })
4055
4629
  }, component.type);
4056
4630
  });
@@ -4058,7 +4632,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4058
4632
  * Renders the component of a template once for each item of the list at the
4059
4633
  * template path, keyed by index. Renders nothing when there is no list.
4060
4634
  */
4061
- function TemplateChildren({ surface, template, basePath, ancestors }) {
4635
+ function TemplateChildren({ surface, template, basePath, ancestors, parentPlace, via }) {
4062
4636
  const dataModel = surface.model.dataModel;
4063
4637
  const listPath = (0, _copilotkit_core_generative_ui.resolvePath)(template.path, basePath);
4064
4638
  const subscribe = (0, react.useCallback)((onChange) => {
@@ -4074,17 +4648,25 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4074
4648
  surface,
4075
4649
  componentId: template.componentId,
4076
4650
  basePath: (0, _copilotkit_core_generative_ui.joinPath)(listPath, String(index)),
4077
- ancestors
4651
+ ancestors,
4652
+ parentPlace,
4653
+ via
4078
4654
  }, index)) });
4079
4655
  }
4080
4656
  /**
4081
4657
  * The props the model sets, with their dynamic values resolved from the
4082
4658
  * 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.
4659
+ * `{"@call":"@index"}` in a template, at any depth. Bound props, events and
4660
+ * slots are left out, and so are the keys of the A2UI component envelope;
4661
+ * `accessibility` is resolved the same way and given on its own.
4662
+ *
4663
+ * A writable prop that is not bound to a path and that the component set
4664
+ * shows the local value from `overrides`, also when the model never set the
4665
+ * prop. That is intended: the user's edit wins over the model's literal
4666
+ * value, also over a newer literal the agent sends later, until the node
4667
+ * remounts. A literal is not data the agent and the user share; a prop
4668
+ * that both must write is bound to a path, and the setter writes the data
4669
+ * model.
4088
4670
  *
4089
4671
  * A prop is resolved again only when its raw value or a data model value it
4090
4672
  * read has changed, so the result keeps its identity otherwise. A function
@@ -4095,7 +4677,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4095
4677
  const units = (0, react.useMemo)(() => {
4096
4678
  const list = [];
4097
4679
  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;
4680
+ 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
4681
  list.push({
4100
4682
  key,
4101
4683
  raw: properties[key]
@@ -4196,9 +4778,10 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4196
4778
  for (const [key, selector] of selectors) try {
4197
4779
  values[key] = selector(state, modelProps);
4198
4780
  } catch (error) {
4781
+ const { message } = (0, _copilotkit_shared_generative_ui.toError)(error);
4199
4782
  return {
4200
4783
  ok: false,
4201
- problem: `${key}: selector threw: ${error instanceof Error ? error.message : String(error)}`
4784
+ problem: `${key}: selector threw: ${message}`
4202
4785
  };
4203
4786
  }
4204
4787
  return {
@@ -4209,8 +4792,10 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4209
4792
  /**
4210
4793
  * The values of the bound props of a node, read from the app state.
4211
4794
  *
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
4795
+ * The selectors get the checked model props: the resolved values that
4796
+ * passed the props schema (without the nulls it rejected), not the
4797
+ * schema's parsed output, so its defaults and transforms are not applied
4798
+ * 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
4799
  * last snapshot is kept. So the node re-renders only when a selected value
4215
4800
  * changes. A selector that returns a new array on each call cannot cause a
4216
4801
  * render loop: with the same inputs, the cached snapshot comes back.
@@ -4240,7 +4825,10 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4240
4825
  * Checks the model-set props with the props schema before the selectors run.
4241
4826
  * The bound props are not set yet, so their issues do not count. Neither do
4242
4827
  * issues with no path (a check over several props), which the full check
4243
- * after the selectors reports.
4828
+ * after the selectors reports. The exception is the issue that an async
4829
+ * props schema gives (`isAsyncIssue`): the render checks props
4830
+ * synchronously only, so the full check would fail the same way, and it is
4831
+ * reported here.
4244
4832
  */
4245
4833
  function checkModelProps(definition, modelData) {
4246
4834
  if (!definition.props) return {
@@ -4259,7 +4847,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4259
4847
  if (!result.success) {
4260
4848
  const issues = result.issues.filter((issue) => {
4261
4849
  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);
4850
+ return key === void 0 ? (0, _copilotkit_shared_generative_ui.isAsyncIssue)(issue) : !(0, _copilotkit_shared_generative_ui.hasOwnKey)(definition.selectors, key);
4263
4851
  });
4264
4852
  if (issues.length > 0) return {
4265
4853
  ok: false,
@@ -4322,7 +4910,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4322
4910
  function useEventProps(runtime, surface, componentId, definition, properties, basePath) {
4323
4911
  const latest = (0, react.useRef)(properties);
4324
4912
  latest.current = properties;
4325
- const boundNames = Object.keys(definition.events ?? {}).filter((name) => Object.hasOwn(definition.eventFunctions, name) || (0, _copilotkit_shared.isActionBinding)(properties[name])).join("|");
4913
+ 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
4914
  return (0, react.useMemo)(() => {
4327
4915
  const handlers = {};
4328
4916
  const names = boundNames ? boundNames.split("|") : [];
@@ -4334,7 +4922,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4334
4922
  name,
4335
4923
  parameters: definition.events?.[name]?.parameters
4336
4924
  };
4337
- const functionName = Object.hasOwn(definition.eventFunctions, name) ? definition.eventFunctions[name] : void 0;
4925
+ const functionName = (0, _copilotkit_shared_generative_ui.hasOwnKey)(definition.eventFunctions, name) ? definition.eventFunctions[name] : void 0;
4338
4926
  handlers[name] = functionName === void 0 ? (args) => (0, _copilotkit_core_generative_ui.invokeBoundEvent)({
4339
4927
  ...call,
4340
4928
  binding: latest.current[name],
@@ -4360,7 +4948,9 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4360
4948
  * The setter props of a node: `setValue` for the writable prop `value`. For
4361
4949
  * a prop the model bound to `{"@path"}`, the setter writes the data model
4362
4950
  * 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.
4951
+ * local value of this mounted node that wins over the model's value until
4952
+ * the node remounts (see `useModelDataProps`). The setters keep their
4953
+ * identity.
4364
4954
  */
4365
4955
  function useSetterProps(runtime, surface, componentId, definition, properties, basePath) {
4366
4956
  const [overrides, setOverrides] = (0, react.useState)({});
@@ -4383,7 +4973,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4383
4973
  try {
4384
4974
  runtime.manager.setOwnedValue(surface.key, (0, _copilotkit_core_generative_ui.resolvePath)(binding["@path"], basePath), value);
4385
4975
  } catch (cause) {
4386
- const message = cause instanceof Error ? cause.message : String(cause);
4976
+ const { message } = (0, _copilotkit_shared_generative_ui.toError)(cause);
4387
4977
  (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, {
4388
4978
  kind: "props",
4389
4979
  surfaceId: surface.surfaceId,
@@ -4402,29 +4992,36 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4402
4992
  ])
4403
4993
  };
4404
4994
  }
4405
- function useSlotProps(surface, definition, properties, componentId, basePath, ancestors) {
4995
+ function useSlotProps(surface, definition, properties, componentId, basePath, ancestors, place) {
4406
4996
  return (0, react.useMemo)(() => {
4407
4997
  const childAncestors = [...ancestors, scopedId(componentId, basePath)];
4408
4998
  const slots = {};
4409
4999
  for (const [name, kind] of Object.entries(definition.slots ?? {})) {
4410
5000
  const value = properties[name];
5001
+ const via = `${name}:`;
4411
5002
  if (kind === "single" && typeof value === "string") slots[name] = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
4412
5003
  surface,
4413
5004
  componentId: value,
4414
5005
  basePath,
4415
- ancestors: childAncestors
5006
+ ancestors: childAncestors,
5007
+ parentPlace: place,
5008
+ via
4416
5009
  });
4417
5010
  else if (kind === "multiple" && isTemplateChildren(value)) slots[name] = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TemplateChildren, {
4418
5011
  surface,
4419
5012
  template: value,
4420
5013
  basePath,
4421
- ancestors: childAncestors
5014
+ ancestors: childAncestors,
5015
+ parentPlace: place,
5016
+ via
4422
5017
  });
4423
5018
  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
5019
  surface,
4425
5020
  componentId: id,
4426
5021
  basePath,
4427
- ancestors: childAncestors
5022
+ ancestors: childAncestors,
5023
+ parentPlace: place,
5024
+ via
4428
5025
  }, id));
4429
5026
  }
4430
5027
  return slots;
@@ -4434,7 +5031,8 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4434
5031
  properties,
4435
5032
  componentId,
4436
5033
  basePath,
4437
- ancestors
5034
+ ancestors,
5035
+ place
4438
5036
  ]);
4439
5037
  }
4440
5038
  /**
@@ -4442,33 +5040,43 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4442
5040
  * `childList()`, at any depth) replaced by the rendered component. Without
4443
5041
  * references, the parsed props come back as they are.
4444
5042
  */
4445
- function useChildRefProps(surface, refs, props, componentId, basePath, ancestors) {
5043
+ function useChildRefProps(surface, refs, props, componentId, basePath, ancestors, place) {
4446
5044
  return (0, react.useMemo)(() => {
4447
5045
  if (props === void 0 || refs.length === 0) return props;
4448
5046
  const childAncestors = [...ancestors, scopedId(componentId, basePath)];
4449
- const render = (kind, value) => {
5047
+ const render = (kind, value, via) => {
4450
5048
  if (kind === "single") return typeof value === "string" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
4451
5049
  surface,
4452
5050
  componentId: value,
4453
5051
  basePath,
4454
- ancestors: childAncestors
5052
+ ancestors: childAncestors,
5053
+ parentPlace: place,
5054
+ via
4455
5055
  }) : void 0;
4456
5056
  if (isTemplateChildren(value)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(TemplateChildren, {
4457
5057
  surface,
4458
5058
  template: value,
4459
5059
  basePath,
4460
- ancestors: childAncestors
5060
+ ancestors: childAncestors,
5061
+ parentPlace: place,
5062
+ via
4461
5063
  });
4462
5064
  if (!Array.isArray(value)) return void 0;
4463
5065
  return [...new Set(value.filter((id) => typeof id === "string"))].map((id) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NodeView, {
4464
5066
  surface,
4465
5067
  componentId: id,
4466
5068
  basePath,
4467
- ancestors: childAncestors
5069
+ ancestors: childAncestors,
5070
+ parentPlace: place,
5071
+ via
4468
5072
  }, id));
4469
5073
  };
4470
5074
  let result = props;
4471
- for (const ref of refs) result = (0, _copilotkit_shared.mapAtChildRef)(result, ref.path, (found) => render(ref.kind, found));
5075
+ for (const ref of refs) {
5076
+ let found = 0;
5077
+ const path = ref.path.join(".");
5078
+ result = (0, _copilotkit_shared.mapAtChildRef)(result, ref.path, (value) => render(ref.kind, value, `${path}[${found++}]:`));
5079
+ }
4472
5080
  return result;
4473
5081
  }, [
4474
5082
  surface,
@@ -4476,15 +5084,17 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4476
5084
  props,
4477
5085
  componentId,
4478
5086
  basePath,
4479
- ancestors
5087
+ ancestors,
5088
+ place
4480
5089
  ]);
4481
5090
  }
4482
- function ResolvedNode({ surface, componentId, definition, properties, runtime, basePath, ancestors }) {
5091
+ function ResolvedNode({ surface, componentId, definition, properties, runtime, basePath, ancestors, place }) {
5092
+ const { AccessibilityWrapper } = useGenerativeUIHost();
4483
5093
  const { overrides, setters } = useSetterProps(runtime, surface, componentId, definition, properties, basePath);
4484
5094
  const dataProps = useDataProps(runtime, surface, definition, properties, basePath, overrides);
4485
5095
  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);
5096
+ const slotProps = useSlotProps(surface, definition, properties, componentId, basePath, ancestors, place);
5097
+ const childRefProps = useChildRefProps(surface, definition.childRefs, dataProps.ok ? dataProps.props : void 0, componentId, basePath, ancestors, place);
4488
5098
  const accessibility = dataProps.ok ? dataProps.accessibility : void 0;
4489
5099
  const nodeContext = (0, react.useMemo)(() => ({
4490
5100
  surfaceKey: surface.key,
@@ -4498,8 +5108,19 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4498
5108
  accessibility
4499
5109
  ]);
4500
5110
  const problem = dataProps.ok ? void 0 : dataProps.problem;
5111
+ const log = useFailureLog();
4501
5112
  (0, react.useEffect)(() => {
4502
- if (problem) (0, _copilotkit_core_generative_ui.reportErrorSafely)(runtime, {
5113
+ if (!problem) {
5114
+ log.clear(failureSlots.fallback(place));
5115
+ log.clear(failureSlots.props(place));
5116
+ }
5117
+ }, [
5118
+ problem,
5119
+ log,
5120
+ place
5121
+ ]);
5122
+ (0, react.useEffect)(() => {
5123
+ if (problem) log.report(runtime, failureSlots.props(place), {
4503
5124
  kind: "props",
4504
5125
  surfaceId: surface.surfaceId,
4505
5126
  componentId,
@@ -4507,13 +5128,19 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4507
5128
  });
4508
5129
  }, [
4509
5130
  problem,
5131
+ log,
5132
+ place,
4510
5133
  runtime,
4511
5134
  surface.surfaceId,
4512
5135
  componentId
4513
5136
  ]);
4514
- if (!dataProps.ok) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DefaultNodeFallback, {
5137
+ if (!dataProps.ok) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GuardedNodeFallback, {
5138
+ surfaceId: surface.surfaceId,
5139
+ componentId,
5140
+ runtime,
4515
5141
  message: `invalid props (${problem})`,
4516
- componentId
5142
+ slot: failureSlots.fallback(place),
5143
+ revision: properties
4517
5144
  });
4518
5145
  const Component = definition.component;
4519
5146
  const rendered = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, {
@@ -4525,10 +5152,8 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4525
5152
  const hasEnvelope = properties.accessibility !== void 0 && definition.accessibility !== "self";
4526
5153
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GenerativeUINodeContext.Provider, {
4527
5154
  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)),
5155
+ children: hasEnvelope ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(AccessibilityWrapper, {
5156
+ attributes: (0, _copilotkit_core_generative_ui.wrapperAria)((0, _copilotkit_core_generative_ui.accessibilityAttributes)(accessibility)),
4532
5157
  children: rendered
4533
5158
  }) : rendered
4534
5159
  });
@@ -4548,39 +5173,18 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4548
5173
  })]
4549
5174
  });
4550
5175
  }
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
5176
  function GenerativeUIChatActivity({ content, agent }) {
4567
5177
  const runtime = useGenerativeUIRuntime();
4568
5178
  const threadId = useCopilotChatConfiguration()?.threadId ?? agent?.threadId;
4569
5179
  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
- }, [
5180
+ const { surface, rejected, error } = useGenerativeUIActivitySurface({
4574
5181
  runtime,
4575
5182
  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);
5183
+ agentId: agent?.agentId,
5184
+ content,
5185
+ target: _copilotkit_shared.GENERATIVE_UI_CHAT_TARGET
5186
+ });
4581
5187
  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
5188
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Wrapper, {
4585
5189
  surfaceId: content.surfaceId,
4586
5190
  isStreaming: content.status === "streaming",
@@ -4592,7 +5196,7 @@ _radix_ui_react_popover = __toESM(_radix_ui_react_popover);
4592
5196
  function createGenerativeUIActivityRenderer() {
4593
5197
  return {
4594
5198
  activityType: _copilotkit_shared.GENERATIVE_UI_ACTIVITY_TYPE,
4595
- content: ContentSchema,
5199
+ content: _copilotkit_core_generative_ui.GenerativeUISurfaceContentSchema,
4596
5200
  render: GenerativeUIChatActivity
4597
5201
  };
4598
5202
  }