@copilotkit/react-core 1.51.3-next.6 → 1.51.3-next.7

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 (97) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/chunk-77IVITG3.mjs +158 -0
  3. package/dist/chunk-77IVITG3.mjs.map +1 -0
  4. package/dist/chunk-BKMJ4LC7.mjs +119 -0
  5. package/dist/chunk-BKMJ4LC7.mjs.map +1 -0
  6. package/dist/chunk-C3YJYDK4.mjs +189 -0
  7. package/dist/chunk-C3YJYDK4.mjs.map +1 -0
  8. package/dist/{chunk-GIU66J37.mjs → chunk-DQXCQWSG.mjs} +47 -5
  9. package/dist/chunk-DQXCQWSG.mjs.map +1 -0
  10. package/dist/{chunk-HBMPXNW2.mjs → chunk-LO4RRITI.mjs} +71 -18
  11. package/dist/chunk-LO4RRITI.mjs.map +1 -0
  12. package/dist/{chunk-3G4VFRVV.mjs → chunk-NXHQDCZF.mjs} +2 -2
  13. package/dist/{chunk-FDOMAPJY.mjs → chunk-QD7EID4N.mjs} +1 -1
  14. package/dist/chunk-QD7EID4N.mjs.map +1 -0
  15. package/dist/{chunk-YTQHRJUA.mjs → chunk-VKNLTZJE.mjs} +2 -2
  16. package/dist/{chunk-4RRUJHCI.mjs → chunk-VP43SLSZ.mjs} +2 -2
  17. package/dist/{chunk-MF2ZSLBV.mjs → chunk-XZFIJ7XF.mjs} +2 -2
  18. package/dist/components/copilot-provider/copilotkit.js +437 -150
  19. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  20. package/dist/components/copilot-provider/copilotkit.mjs +5 -3
  21. package/dist/components/copilot-provider/index.js +437 -150
  22. package/dist/components/copilot-provider/index.js.map +1 -1
  23. package/dist/components/copilot-provider/index.mjs +5 -3
  24. package/dist/components/index.js +437 -150
  25. package/dist/components/index.js.map +1 -1
  26. package/dist/components/index.mjs +5 -3
  27. package/dist/context/coagent-state-renders-context.d.ts +1 -0
  28. package/dist/context/coagent-state-renders-context.js.map +1 -1
  29. package/dist/context/coagent-state-renders-context.mjs +1 -1
  30. package/dist/context/index.js.map +1 -1
  31. package/dist/context/index.mjs +1 -1
  32. package/dist/hooks/index.js +512 -212
  33. package/dist/hooks/index.js.map +1 -1
  34. package/dist/hooks/index.mjs +19 -17
  35. package/dist/hooks/use-coagent-state-render-bridge.helpers.d.ts +92 -0
  36. package/dist/hooks/use-coagent-state-render-bridge.helpers.js +231 -0
  37. package/dist/hooks/use-coagent-state-render-bridge.helpers.js.map +1 -0
  38. package/dist/hooks/use-coagent-state-render-bridge.helpers.mjs +24 -0
  39. package/dist/hooks/use-coagent-state-render-bridge.helpers.mjs.map +1 -0
  40. package/dist/hooks/use-coagent-state-render-bridge.js +334 -72
  41. package/dist/hooks/use-coagent-state-render-bridge.js.map +1 -1
  42. package/dist/hooks/use-coagent-state-render-bridge.mjs +4 -2
  43. package/dist/hooks/use-coagent-state-render-registry.d.ts +25 -0
  44. package/dist/hooks/use-coagent-state-render-registry.js +358 -0
  45. package/dist/hooks/use-coagent-state-render-registry.js.map +1 -0
  46. package/dist/hooks/use-coagent-state-render-registry.mjs +9 -0
  47. package/dist/hooks/use-coagent-state-render-registry.mjs.map +1 -0
  48. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  49. package/dist/hooks/use-coagent-state-render.mjs +2 -2
  50. package/dist/hooks/use-copilot-chat-headless_c.js +414 -114
  51. package/dist/hooks/use-copilot-chat-headless_c.js.map +1 -1
  52. package/dist/hooks/use-copilot-chat-headless_c.mjs +7 -5
  53. package/dist/hooks/use-copilot-chat.js +406 -106
  54. package/dist/hooks/use-copilot-chat.js.map +1 -1
  55. package/dist/hooks/use-copilot-chat.mjs +7 -5
  56. package/dist/hooks/use-copilot-chat_internal.js +406 -106
  57. package/dist/hooks/use-copilot-chat_internal.js.map +1 -1
  58. package/dist/hooks/use-copilot-chat_internal.mjs +6 -4
  59. package/dist/hooks/use-langgraph-interrupt-render.mjs +1 -1
  60. package/dist/index.js +651 -311
  61. package/dist/index.js.map +1 -1
  62. package/dist/index.mjs +22 -20
  63. package/dist/lib/copilot-task.js.map +1 -1
  64. package/dist/lib/copilot-task.mjs +6 -4
  65. package/dist/lib/index.js.map +1 -1
  66. package/dist/lib/index.mjs +6 -4
  67. package/dist/setupTests.js +1 -0
  68. package/dist/setupTests.js.map +1 -1
  69. package/dist/setupTests.mjs +1 -0
  70. package/dist/setupTests.mjs.map +1 -1
  71. package/dist/test-helpers/copilot-context.d.ts +14 -0
  72. package/dist/test-helpers/copilot-context.js +128 -0
  73. package/dist/test-helpers/copilot-context.js.map +1 -0
  74. package/dist/test-helpers/copilot-context.mjs +74 -0
  75. package/dist/test-helpers/copilot-context.mjs.map +1 -0
  76. package/dist/types/index.mjs +1 -1
  77. package/package.json +5 -5
  78. package/src/components/copilot-provider/copilotkit.tsx +56 -0
  79. package/src/context/coagent-state-renders-context.tsx +1 -0
  80. package/src/hooks/__tests__/use-coagent-state-render-bridge.helpers.test.ts +100 -0
  81. package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +892 -37
  82. package/src/hooks/__tests__/use-coagent-state-render.test.tsx +334 -0
  83. package/src/hooks/use-coagent-state-render-bridge.helpers.ts +311 -0
  84. package/src/hooks/use-coagent-state-render-bridge.tsx +25 -120
  85. package/src/hooks/use-coagent-state-render-registry.ts +215 -0
  86. package/src/hooks/use-copilot-chat_internal.ts +93 -34
  87. package/src/setupTests.ts +1 -0
  88. package/src/test-helpers/copilot-context.ts +91 -0
  89. package/dist/chunk-3X3I7OJV.mjs +0 -172
  90. package/dist/chunk-3X3I7OJV.mjs.map +0 -1
  91. package/dist/chunk-FDOMAPJY.mjs.map +0 -1
  92. package/dist/chunk-GIU66J37.mjs.map +0 -1
  93. package/dist/chunk-HBMPXNW2.mjs.map +0 -1
  94. /package/dist/{chunk-3G4VFRVV.mjs.map → chunk-NXHQDCZF.mjs.map} +0 -0
  95. /package/dist/{chunk-YTQHRJUA.mjs.map → chunk-VKNLTZJE.mjs.map} +0 -0
  96. /package/dist/{chunk-4RRUJHCI.mjs.map → chunk-VP43SLSZ.mjs.map} +0 -0
  97. /package/dist/{chunk-MF2ZSLBV.mjs.map → chunk-XZFIJ7XF.mjs.map} +0 -0
@@ -85,8 +85,8 @@ __export(copilotkit_exports, {
85
85
  defaultCopilotContextCategories: () => defaultCopilotContextCategories
86
86
  });
87
87
  module.exports = __toCommonJS(copilotkit_exports);
88
- var import_react14 = require("react");
89
- var import_react15 = require("@copilotkitnext/react");
88
+ var import_react15 = require("react");
89
+ var import_react16 = require("@copilotkitnext/react");
90
90
 
91
91
  // src/context/copilot-context.tsx
92
92
  var import_react = __toESM(require("react"));
@@ -325,7 +325,7 @@ function setsHaveIntersection(setA, setB) {
325
325
 
326
326
  // src/components/copilot-provider/copilotkit.tsx
327
327
  var import_react_dom = require("react-dom");
328
- var import_shared12 = require("@copilotkit/shared");
328
+ var import_shared13 = require("@copilotkit/shared");
329
329
 
330
330
  // src/hooks/use-flat-category-store.ts
331
331
  var import_react3 = require("react");
@@ -1227,8 +1227,8 @@ function useCoAgentStateRenders() {
1227
1227
  }
1228
1228
 
1229
1229
  // src/hooks/use-coagent-state-render-bridge.tsx
1230
- var import_react10 = require("@copilotkitnext/react");
1231
- var import_react11 = require("react");
1230
+ var import_react11 = require("@copilotkitnext/react");
1231
+ var import_react12 = require("react");
1232
1232
 
1233
1233
  // src/context/threads-context.tsx
1234
1234
  var import_react9 = require("react");
@@ -1258,6 +1258,9 @@ function useThreads() {
1258
1258
  }
1259
1259
 
1260
1260
  // src/hooks/use-coagent-state-render-bridge.tsx
1261
+ var import_shared10 = require("@copilotkit/shared");
1262
+
1263
+ // src/hooks/use-coagent-state-render-bridge.helpers.ts
1261
1264
  var import_shared9 = require("@copilotkit/shared");
1262
1265
  function getStateWithoutConstantKeys(state) {
1263
1266
  if (!state)
@@ -1280,16 +1283,294 @@ function areStatesEquals(a, b) {
1280
1283
  ]);
1281
1284
  return JSON.stringify(aWithoutConstantKeys) === JSON.stringify(bWithoutConstantKeys);
1282
1285
  }
1286
+ function isPlaceholderMessageId(messageId) {
1287
+ return !!messageId && messageId.startsWith("coagent-state-render-");
1288
+ }
1289
+ function isPlaceholderMessageName(messageName) {
1290
+ return messageName === "coagent-state-render";
1291
+ }
1292
+ function readCachedMessageEntry(entry) {
1293
+ if (!entry || typeof entry !== "object") {
1294
+ return { snapshot: entry, runId: void 0 };
1295
+ }
1296
+ const snapshot = "snapshot" in entry ? entry.snapshot : entry;
1297
+ const runId = "runId" in entry ? entry.runId : void 0;
1298
+ return { snapshot, runId };
1299
+ }
1300
+ function getEffectiveRunId({
1301
+ existingClaimRunId,
1302
+ cachedMessageRunId,
1303
+ runId
1304
+ }) {
1305
+ return existingClaimRunId || cachedMessageRunId || runId || "pending";
1306
+ }
1307
+ function resolveClaim({
1308
+ claims,
1309
+ context,
1310
+ stateSnapshot
1311
+ }) {
1312
+ const { messageId, stateRenderId, runId, messageIndex } = context;
1313
+ const existing = claims[messageId];
1314
+ if (existing) {
1315
+ const canRender = existing.stateRenderId === stateRenderId;
1316
+ const shouldUpdateRunId = canRender && runId && (!existing.runId || existing.runId === "pending");
1317
+ return {
1318
+ canRender,
1319
+ action: canRender ? "existing" /* Existing */ : "block" /* Block */,
1320
+ updateRunId: shouldUpdateRunId ? runId : void 0
1321
+ };
1322
+ }
1323
+ const normalizedRunId = runId != null ? runId : "pending";
1324
+ const renderClaimedByOtherMessageEntry = Object.entries(claims).find(
1325
+ ([, claim]) => {
1326
+ var _a;
1327
+ return claim.stateRenderId === stateRenderId && ((_a = claim.runId) != null ? _a : "pending") === normalizedRunId && (0, import_shared9.dataToUUID)(getStateWithoutConstantKeys(claim.stateSnapshot)) === (0, import_shared9.dataToUUID)(getStateWithoutConstantKeys(stateSnapshot));
1328
+ }
1329
+ );
1330
+ const renderClaimedByOtherMessage = renderClaimedByOtherMessageEntry == null ? void 0 : renderClaimedByOtherMessageEntry[1];
1331
+ const claimedMessageId = renderClaimedByOtherMessageEntry == null ? void 0 : renderClaimedByOtherMessageEntry[0];
1332
+ if (renderClaimedByOtherMessage) {
1333
+ if (messageIndex !== void 0 && renderClaimedByOtherMessage.messageIndex !== void 0 && messageIndex > renderClaimedByOtherMessage.messageIndex) {
1334
+ return {
1335
+ canRender: true,
1336
+ action: "override" /* Override */,
1337
+ nextClaim: { stateRenderId, runId, messageIndex },
1338
+ lockOthers: runId === renderClaimedByOtherMessage.runId || isPlaceholderMessageId(claimedMessageId)
1339
+ };
1340
+ }
1341
+ if (runId && renderClaimedByOtherMessage.runId && runId !== renderClaimedByOtherMessage.runId) {
1342
+ return {
1343
+ canRender: true,
1344
+ action: "override" /* Override */,
1345
+ nextClaim: { stateRenderId, runId, messageIndex },
1346
+ lockOthers: isPlaceholderMessageId(claimedMessageId)
1347
+ };
1348
+ }
1349
+ if (isPlaceholderMessageId(claimedMessageId)) {
1350
+ return {
1351
+ canRender: true,
1352
+ action: "override" /* Override */,
1353
+ nextClaim: { stateRenderId, runId, messageIndex },
1354
+ lockOthers: true
1355
+ };
1356
+ }
1357
+ if (stateSnapshot && renderClaimedByOtherMessage.stateSnapshot && !areStatesEquals(renderClaimedByOtherMessage.stateSnapshot, stateSnapshot)) {
1358
+ return {
1359
+ canRender: true,
1360
+ action: "override" /* Override */,
1361
+ nextClaim: { stateRenderId, runId }
1362
+ };
1363
+ }
1364
+ return { canRender: false, action: "block" /* Block */ };
1365
+ }
1366
+ if (!runId) {
1367
+ return { canRender: false, action: "block" /* Block */ };
1368
+ }
1369
+ return {
1370
+ canRender: true,
1371
+ action: "create" /* Create */,
1372
+ nextClaim: { stateRenderId, runId, messageIndex }
1373
+ };
1374
+ }
1375
+ function selectSnapshot({
1376
+ messageId,
1377
+ messageName,
1378
+ allowLiveState,
1379
+ skipLatestCache,
1380
+ stateRenderId,
1381
+ effectiveRunId,
1382
+ stateSnapshotProp,
1383
+ agentState,
1384
+ agentMessages,
1385
+ existingClaim,
1386
+ caches
1387
+ }) {
1388
+ var _a, _b, _c, _d, _e, _f;
1389
+ const lastAssistantId = agentMessages ? (_a = [...agentMessages].reverse().find((msg) => msg.role === "assistant")) == null ? void 0 : _a.id : void 0;
1390
+ const latestSnapshot = stateRenderId !== void 0 ? caches.byStateRenderAndRun[`${stateRenderId}::latest`] : void 0;
1391
+ const messageIndex = agentMessages ? agentMessages.findIndex((msg) => msg.id === messageId) : -1;
1392
+ const messageRole = messageIndex >= 0 && agentMessages ? (_b = agentMessages[messageIndex]) == null ? void 0 : _b.role : void 0;
1393
+ let previousUserMessageId;
1394
+ if (messageIndex > 0 && agentMessages) {
1395
+ for (let i = messageIndex - 1; i >= 0; i -= 1) {
1396
+ if (((_c = agentMessages[i]) == null ? void 0 : _c.role) === "user") {
1397
+ previousUserMessageId = (_d = agentMessages[i]) == null ? void 0 : _d.id;
1398
+ break;
1399
+ }
1400
+ }
1401
+ }
1402
+ const liveStateIsStale = stateSnapshotProp === void 0 && latestSnapshot !== void 0 && agentState !== void 0 && areStatesEquals(latestSnapshot, agentState);
1403
+ const shouldUseLiveState = (Boolean(allowLiveState) || !lastAssistantId || messageId === lastAssistantId) && !liveStateIsStale;
1404
+ const snapshot = stateSnapshotProp ? (0, import_shared9.parseJson)(stateSnapshotProp, stateSnapshotProp) : shouldUseLiveState ? agentState : void 0;
1405
+ const hasSnapshotKeys = !!(snapshot && Object.keys(snapshot).length > 0);
1406
+ const allowEmptySnapshot = snapshot !== void 0 && !hasSnapshotKeys && (stateSnapshotProp !== void 0 || shouldUseLiveState);
1407
+ const messageCacheEntry = caches.byMessageId[messageId];
1408
+ const cachedMessageSnapshot = readCachedMessageEntry(messageCacheEntry).snapshot;
1409
+ const cacheKey = stateRenderId !== void 0 ? `${stateRenderId}::${effectiveRunId}` : void 0;
1410
+ let cachedSnapshot = cachedMessageSnapshot != null ? cachedMessageSnapshot : caches.byMessageId[messageId];
1411
+ if (cachedSnapshot === void 0 && cacheKey && caches.byStateRenderAndRun[cacheKey] !== void 0) {
1412
+ cachedSnapshot = caches.byStateRenderAndRun[cacheKey];
1413
+ }
1414
+ if (cachedSnapshot === void 0 && stateRenderId && previousUserMessageId && caches.byStateRenderAndRun[`${stateRenderId}::pending:${previousUserMessageId}`] !== void 0) {
1415
+ cachedSnapshot = caches.byStateRenderAndRun[`${stateRenderId}::pending:${previousUserMessageId}`];
1416
+ }
1417
+ if (cachedSnapshot === void 0 && !skipLatestCache && stateRenderId && messageRole !== "assistant" && (stateSnapshotProp !== void 0 || agentState && Object.keys(agentState).length > 0)) {
1418
+ cachedSnapshot = caches.byStateRenderAndRun[`${stateRenderId}::latest`];
1419
+ }
1420
+ const snapshotForClaim = (existingClaim == null ? void 0 : existingClaim.locked) ? (_e = existingClaim.stateSnapshot) != null ? _e : cachedSnapshot : hasSnapshotKeys ? snapshot : (_f = existingClaim == null ? void 0 : existingClaim.stateSnapshot) != null ? _f : cachedSnapshot;
1421
+ return { snapshot, hasSnapshotKeys, cachedSnapshot, allowEmptySnapshot, snapshotForClaim };
1422
+ }
1423
+
1424
+ // src/hooks/use-coagent-state-render-registry.ts
1425
+ var import_react10 = require("react");
1426
+ var LAST_SNAPSHOTS_BY_RENDER_AND_RUN = "__lastSnapshotsByStateRenderIdAndRun";
1427
+ var LAST_SNAPSHOTS_BY_MESSAGE = "__lastSnapshotsByMessageId";
1428
+ function getClaimsStore(claimsRef) {
1429
+ return claimsRef.current;
1430
+ }
1431
+ function getSnapshotCaches(claimsRef) {
1432
+ var _a, _b;
1433
+ const store = getClaimsStore(claimsRef);
1434
+ return {
1435
+ byStateRenderAndRun: (_a = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _a : {},
1436
+ byMessageId: (_b = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _b : {}
1437
+ };
1438
+ }
1439
+ function useStateRenderRegistry({
1440
+ agentId,
1441
+ stateRenderId,
1442
+ message,
1443
+ messageIndex,
1444
+ stateSnapshot,
1445
+ agentState,
1446
+ agentMessages,
1447
+ claimsRef
1448
+ }) {
1449
+ var _a, _b, _c, _d, _e, _f;
1450
+ const store = getClaimsStore(claimsRef);
1451
+ const runId = message.runId;
1452
+ const cachedMessageEntry = (_a = store[LAST_SNAPSHOTS_BY_MESSAGE]) == null ? void 0 : _a[message.id];
1453
+ const { runId: cachedMessageRunId } = readCachedMessageEntry(cachedMessageEntry);
1454
+ const existingClaimRunId = (_b = claimsRef.current[message.id]) == null ? void 0 : _b.runId;
1455
+ const effectiveRunId = getEffectiveRunId({
1456
+ existingClaimRunId,
1457
+ cachedMessageRunId,
1458
+ runId
1459
+ });
1460
+ (0, import_react10.useEffect)(() => {
1461
+ return () => {
1462
+ var _a2, _b2, _c2, _d2;
1463
+ const existingClaim2 = claimsRef.current[message.id];
1464
+ if ((existingClaim2 == null ? void 0 : existingClaim2.stateSnapshot) && Object.keys(existingClaim2.stateSnapshot).length > 0) {
1465
+ const snapshotCache = __spreadValues({}, (_a2 = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _a2 : {});
1466
+ const cacheKey = `${existingClaim2.stateRenderId}::${(_b2 = existingClaim2.runId) != null ? _b2 : "pending"}`;
1467
+ snapshotCache[cacheKey] = existingClaim2.stateSnapshot;
1468
+ snapshotCache[`${existingClaim2.stateRenderId}::latest`] = existingClaim2.stateSnapshot;
1469
+ store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
1470
+ const messageCache = __spreadValues({}, (_c2 = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _c2 : {});
1471
+ messageCache[message.id] = {
1472
+ snapshot: existingClaim2.stateSnapshot,
1473
+ runId: (_d2 = existingClaim2.runId) != null ? _d2 : effectiveRunId
1474
+ };
1475
+ store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
1476
+ }
1477
+ delete claimsRef.current[message.id];
1478
+ };
1479
+ }, [claimsRef, effectiveRunId, message.id]);
1480
+ if (!stateRenderId) {
1481
+ return { canRender: false };
1482
+ }
1483
+ const caches = getSnapshotCaches(claimsRef);
1484
+ const existingClaim = claimsRef.current[message.id];
1485
+ const { snapshot, hasSnapshotKeys, allowEmptySnapshot, snapshotForClaim } = selectSnapshot({
1486
+ messageId: message.id,
1487
+ messageName: message.name,
1488
+ allowLiveState: isPlaceholderMessageName(message.name) || isPlaceholderMessageId(message.id),
1489
+ skipLatestCache: isPlaceholderMessageName(message.name) || isPlaceholderMessageId(message.id),
1490
+ stateRenderId,
1491
+ effectiveRunId,
1492
+ stateSnapshotProp: stateSnapshot,
1493
+ agentState,
1494
+ agentMessages,
1495
+ existingClaim,
1496
+ caches
1497
+ });
1498
+ const resolution = resolveClaim({
1499
+ claims: claimsRef.current,
1500
+ context: {
1501
+ agentId,
1502
+ messageId: message.id,
1503
+ stateRenderId,
1504
+ runId: effectiveRunId,
1505
+ messageIndex
1506
+ },
1507
+ stateSnapshot: snapshotForClaim
1508
+ });
1509
+ if (resolution.action === "block" /* Block */) {
1510
+ return { canRender: false };
1511
+ }
1512
+ if (resolution.updateRunId && claimsRef.current[message.id]) {
1513
+ claimsRef.current[message.id].runId = resolution.updateRunId;
1514
+ }
1515
+ if (resolution.nextClaim) {
1516
+ claimsRef.current[message.id] = resolution.nextClaim;
1517
+ }
1518
+ if (resolution.lockOthers) {
1519
+ Object.entries(claimsRef.current).forEach(([id, claim]) => {
1520
+ if (id !== message.id && claim.stateRenderId === stateRenderId) {
1521
+ claim.locked = true;
1522
+ }
1523
+ });
1524
+ }
1525
+ if (existingClaim && !existingClaim.locked && (agentMessages == null ? void 0 : agentMessages.length)) {
1526
+ const indexInAgentMessages = agentMessages.findIndex((msg) => msg.id === message.id);
1527
+ if (indexInAgentMessages >= 0 && indexInAgentMessages < agentMessages.length - 1) {
1528
+ existingClaim.locked = true;
1529
+ }
1530
+ }
1531
+ const existingSnapshot = claimsRef.current[message.id].stateSnapshot;
1532
+ const snapshotChanged = stateSnapshot && existingSnapshot !== void 0 && !areStatesEquals(existingSnapshot, snapshot);
1533
+ if (snapshot && (stateSnapshot || hasSnapshotKeys || allowEmptySnapshot) && (!claimsRef.current[message.id].locked || snapshotChanged)) {
1534
+ if (!claimsRef.current[message.id].locked || snapshotChanged) {
1535
+ claimsRef.current[message.id].stateSnapshot = snapshot;
1536
+ const snapshotCache = __spreadValues({}, (_c = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _c : {});
1537
+ const cacheKey = `${stateRenderId}::${effectiveRunId}`;
1538
+ snapshotCache[cacheKey] = snapshot;
1539
+ snapshotCache[`${stateRenderId}::latest`] = snapshot;
1540
+ store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
1541
+ const messageCache = __spreadValues({}, (_d = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _d : {});
1542
+ messageCache[message.id] = { snapshot, runId: effectiveRunId };
1543
+ store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
1544
+ if (stateSnapshot) {
1545
+ claimsRef.current[message.id].locked = true;
1546
+ }
1547
+ }
1548
+ } else if (snapshotForClaim) {
1549
+ const existingSnapshot2 = claimsRef.current[message.id].stateSnapshot;
1550
+ if (!existingSnapshot2) {
1551
+ claimsRef.current[message.id].stateSnapshot = snapshotForClaim;
1552
+ const snapshotCache = __spreadValues({}, (_e = store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN]) != null ? _e : {});
1553
+ const cacheKey = `${stateRenderId}::${effectiveRunId}`;
1554
+ snapshotCache[cacheKey] = snapshotForClaim;
1555
+ snapshotCache[`${stateRenderId}::latest`] = snapshotForClaim;
1556
+ store[LAST_SNAPSHOTS_BY_RENDER_AND_RUN] = snapshotCache;
1557
+ const messageCache = __spreadValues({}, (_f = store[LAST_SNAPSHOTS_BY_MESSAGE]) != null ? _f : {});
1558
+ messageCache[message.id] = { snapshot: snapshotForClaim, runId: effectiveRunId };
1559
+ store[LAST_SNAPSHOTS_BY_MESSAGE] = messageCache;
1560
+ }
1561
+ }
1562
+ return { canRender: true };
1563
+ }
1564
+
1565
+ // src/hooks/use-coagent-state-render-bridge.tsx
1283
1566
  function useCoagentStateRenderBridge(agentId, props) {
1284
1567
  var _a;
1285
- const { stateSnapshot, messageIndexInRun, message } = props;
1568
+ const { stateSnapshot, message } = props;
1286
1569
  const { coAgentStateRenders, claimsRef } = useCoAgentStateRenders();
1287
- const { agent } = (0, import_react10.useAgent)({ agentId });
1288
- const [nodeName, setNodeName] = (0, import_react11.useState)(void 0);
1289
- const [, forceUpdate] = (0, import_react11.useState)(0);
1290
- const runId = (_a = props.runId) != null ? _a : message.runId;
1291
- const effectiveRunId = runId || "pending";
1292
- (0, import_react11.useEffect)(() => {
1570
+ const { agent } = (0, import_react11.useAgent)({ agentId });
1571
+ const [nodeName, setNodeName] = (0, import_react12.useState)(void 0);
1572
+ const [, forceUpdate] = (0, import_react12.useState)(0);
1573
+ (0, import_react12.useEffect)(() => {
1293
1574
  if (!agent)
1294
1575
  return;
1295
1576
  const subscriber = {
@@ -1312,103 +1593,69 @@ function useCoagentStateRenderBridge(agentId, props) {
1312
1593
  unsubscribe();
1313
1594
  };
1314
1595
  }, [agentId, nodeName]);
1315
- const getStateRender = (0, import_react11.useCallback)(
1596
+ const getStateRender = (0, import_react12.useCallback)(
1316
1597
  (messageId) => {
1317
- return Object.entries(coAgentStateRenders).find(([stateRenderId, stateRender]) => {
1598
+ return Object.entries(coAgentStateRenders).find(([stateRenderId2, stateRender2]) => {
1318
1599
  if (claimsRef.current[messageId]) {
1319
- return stateRenderId === claimsRef.current[messageId].stateRenderId;
1600
+ return stateRenderId2 === claimsRef.current[messageId].stateRenderId;
1320
1601
  }
1321
- const matchingAgentName = stateRender.name === agentId;
1322
- const matchesNodeContext = stateRender.nodeName ? stateRender.nodeName === nodeName : true;
1602
+ const matchingAgentName = stateRender2.name === agentId;
1603
+ const matchesNodeContext = stateRender2.nodeName ? stateRender2.nodeName === nodeName : true;
1323
1604
  return matchingAgentName && matchesNodeContext;
1324
1605
  });
1325
1606
  },
1326
1607
  [coAgentStateRenders, nodeName, agentId]
1327
1608
  );
1328
- const handleRenderRequest = ({
1609
+ const stateRenderEntry = (0, import_react12.useMemo)(() => getStateRender(message.id), [getStateRender, message.id]);
1610
+ const stateRenderId = stateRenderEntry == null ? void 0 : stateRenderEntry[0];
1611
+ const stateRender = stateRenderEntry == null ? void 0 : stateRenderEntry[1];
1612
+ const registryMessage = __spreadProps(__spreadValues({}, message), {
1613
+ runId: (_a = props.runId) != null ? _a : message.runId
1614
+ });
1615
+ const { canRender } = useStateRenderRegistry({
1616
+ agentId,
1329
1617
  stateRenderId,
1330
- messageId,
1331
- runId: runId2,
1332
- stateSnapshot: renderSnapshot
1333
- }) => {
1334
- if (claimsRef.current[messageId]) {
1335
- const canRender = claimsRef.current[messageId].stateRenderId === stateRenderId;
1336
- if (canRender && runId2 && (!claimsRef.current[messageId].runId || claimsRef.current[messageId].runId === "pending")) {
1337
- claimsRef.current[messageId].runId = runId2;
1338
- }
1339
- return canRender;
1340
- }
1341
- const renderClaimedByOtherMessage = Object.values(claimsRef.current).find(
1342
- (c) => c.stateRenderId === stateRenderId && (0, import_shared9.dataToUUID)(getStateWithoutConstantKeys(c.stateSnapshot)) === (0, import_shared9.dataToUUID)(getStateWithoutConstantKeys(renderSnapshot))
1343
- );
1344
- if (renderClaimedByOtherMessage) {
1345
- if (renderSnapshot && renderClaimedByOtherMessage.stateSnapshot && !areStatesEquals(renderClaimedByOtherMessage.stateSnapshot, renderSnapshot)) {
1346
- claimsRef.current[messageId] = { stateRenderId, runId: runId2 };
1347
- return true;
1348
- }
1349
- return false;
1350
- }
1351
- if (!runId2) {
1352
- return false;
1353
- }
1354
- claimsRef.current[messageId] = { stateRenderId, runId: runId2 };
1355
- return true;
1356
- };
1357
- return (0, import_react11.useMemo)(() => {
1358
- var _a2, _b, _c;
1359
- if (messageIndexInRun !== 0) {
1360
- return null;
1361
- }
1362
- const [stateRenderId, stateRender] = (_a2 = getStateRender(message.id)) != null ? _a2 : [];
1618
+ message: registryMessage,
1619
+ messageIndex: props.messageIndex,
1620
+ stateSnapshot,
1621
+ agentState: agent == null ? void 0 : agent.state,
1622
+ agentMessages: agent == null ? void 0 : agent.messages,
1623
+ claimsRef
1624
+ });
1625
+ return (0, import_react12.useMemo)(() => {
1626
+ var _a2, _b;
1363
1627
  if (!stateRender || !stateRenderId) {
1364
1628
  return null;
1365
1629
  }
1366
- const snapshot = stateSnapshot ? (0, import_shared9.parseJson)(stateSnapshot, stateSnapshot) : agent == null ? void 0 : agent.state;
1367
- const canRender = handleRenderRequest({
1368
- stateRenderId,
1369
- messageId: message.id,
1370
- runId: effectiveRunId,
1371
- stateSnapshot: snapshot
1372
- });
1373
1630
  if (!canRender) {
1374
1631
  return null;
1375
1632
  }
1376
- if (snapshot) {
1377
- const existingSnapshot = claimsRef.current[message.id].stateSnapshot;
1378
- const snapshotChanged = stateSnapshot && existingSnapshot !== void 0 && !areStatesEquals(existingSnapshot, snapshot);
1379
- if (!claimsRef.current[message.id].locked || snapshotChanged) {
1380
- claimsRef.current[message.id].stateSnapshot = snapshot;
1381
- if (stateSnapshot) {
1382
- claimsRef.current[message.id].locked = true;
1383
- }
1384
- }
1385
- }
1386
1633
  if (stateRender.handler) {
1387
1634
  stateRender.handler({
1388
- state: stateSnapshot ? (0, import_shared9.parseJson)(stateSnapshot, stateSnapshot) : (_b = agent == null ? void 0 : agent.state) != null ? _b : {},
1635
+ state: stateSnapshot ? (0, import_shared10.parseJson)(stateSnapshot, stateSnapshot) : (_a2 = agent == null ? void 0 : agent.state) != null ? _a2 : {},
1389
1636
  nodeName: nodeName != null ? nodeName : ""
1390
1637
  });
1391
1638
  }
1392
1639
  if (stateRender.render) {
1393
- const status = (agent == null ? void 0 : agent.isRunning) ? "inProgress" : "complete";
1640
+ const status = (agent == null ? void 0 : agent.isRunning) ? "inProgress" /* InProgress */ : "complete" /* Complete */;
1394
1641
  if (typeof stateRender.render === "string")
1395
1642
  return stateRender.render;
1396
1643
  return stateRender.render({
1397
1644
  status,
1398
1645
  // Always use state from claim, to make sure the state does not seem "wiped" for a fraction of a second
1399
- state: (_c = claimsRef.current[message.id].stateSnapshot) != null ? _c : {},
1646
+ state: (_b = claimsRef.current[message.id].stateSnapshot) != null ? _b : {},
1400
1647
  nodeName: nodeName != null ? nodeName : ""
1401
1648
  });
1402
1649
  }
1403
1650
  }, [
1404
- getStateRender,
1405
- stateSnapshot,
1651
+ stateRender,
1652
+ stateRenderId,
1406
1653
  agent == null ? void 0 : agent.state,
1407
1654
  agent == null ? void 0 : agent.isRunning,
1408
1655
  nodeName,
1409
- effectiveRunId,
1410
1656
  message.id,
1411
- messageIndexInRun
1657
+ stateSnapshot,
1658
+ canRender
1412
1659
  ]);
1413
1660
  }
1414
1661
  function CoAgentStateRenderBridge(props) {
@@ -1416,13 +1663,13 @@ function CoAgentStateRenderBridge(props) {
1416
1663
  }
1417
1664
 
1418
1665
  // src/components/CopilotListeners.tsx
1419
- var import_react12 = require("react");
1420
- var import_react13 = require("@copilotkitnext/react");
1421
- var import_shared10 = require("@copilotkit/shared");
1666
+ var import_react13 = require("react");
1667
+ var import_react14 = require("@copilotkitnext/react");
1422
1668
  var import_shared11 = require("@copilotkit/shared");
1669
+ var import_shared12 = require("@copilotkit/shared");
1423
1670
  var usePredictStateSubscription = (agent) => {
1424
- const predictStateToolsRef = (0, import_react12.useRef)([]);
1425
- const getSubscriber = (0, import_react12.useCallback)(
1671
+ const predictStateToolsRef = (0, import_react13.useRef)([]);
1672
+ const getSubscriber = (0, import_react13.useCallback)(
1426
1673
  (agent2) => ({
1427
1674
  onCustomEvent: ({ event }) => {
1428
1675
  if (event.name === "PredictState") {
@@ -1433,7 +1680,7 @@ var usePredictStateSubscription = (agent) => {
1433
1680
  predictStateToolsRef.current.forEach((t) => {
1434
1681
  if ((t == null ? void 0 : t.tool) !== toolCallName)
1435
1682
  return;
1436
- const emittedState = typeof partialToolCallArgs === "string" ? (0, import_shared10.parseJson)(partialToolCallArgs, partialToolCallArgs) : partialToolCallArgs;
1683
+ const emittedState = typeof partialToolCallArgs === "string" ? (0, import_shared11.parseJson)(partialToolCallArgs, partialToolCallArgs) : partialToolCallArgs;
1437
1684
  agent2.setState({
1438
1685
  [t.state_key]: emittedState[t.state_key]
1439
1686
  });
@@ -1442,7 +1689,7 @@ var usePredictStateSubscription = (agent) => {
1442
1689
  }),
1443
1690
  []
1444
1691
  );
1445
- (0, import_react12.useEffect)(() => {
1692
+ (0, import_react13.useEffect)(() => {
1446
1693
  if (!agent)
1447
1694
  return;
1448
1695
  const subscriber = getSubscriber(agent);
@@ -1453,16 +1700,16 @@ var usePredictStateSubscription = (agent) => {
1453
1700
  }, [agent, getSubscriber]);
1454
1701
  };
1455
1702
  function CopilotListeners() {
1456
- const { copilotkit } = (0, import_react13.useCopilotKit)();
1457
- const existingConfig = (0, import_react13.useCopilotChatConfiguration)();
1703
+ const { copilotkit } = (0, import_react14.useCopilotKit)();
1704
+ const existingConfig = (0, import_react14.useCopilotChatConfiguration)();
1458
1705
  const resolvedAgentId = existingConfig == null ? void 0 : existingConfig.agentId;
1459
1706
  const { setBannerError } = useToast();
1460
- const { agent } = (0, import_react13.useAgent)({ agentId: resolvedAgentId });
1707
+ const { agent } = (0, import_react14.useAgent)({ agentId: resolvedAgentId });
1461
1708
  usePredictStateSubscription(agent);
1462
- (0, import_react12.useEffect)(() => {
1709
+ (0, import_react13.useEffect)(() => {
1463
1710
  const subscriber = {
1464
1711
  onError: ({ error }) => {
1465
- setBannerError(new import_shared11.CopilotKitLowLevelError({ error, message: error.message }));
1712
+ setBannerError(new import_shared12.CopilotKitLowLevelError({ error, message: error.message }));
1466
1713
  }
1467
1714
  };
1468
1715
  const subscription = copilotkit.subscribe(subscriber);
@@ -1480,9 +1727,9 @@ function CopilotKit(_a) {
1480
1727
  const enabled = shouldShowDevConsole(props.showDevConsole);
1481
1728
  const showInspector = shouldShowDevConsole(props.enableInspector);
1482
1729
  const publicApiKey = props.publicApiKey || props.publicLicenseKey;
1483
- const renderArr = (0, import_react14.useMemo)(() => [{ render: CoAgentStateRenderBridge }], []);
1730
+ const renderArr = (0, import_react15.useMemo)(() => [{ render: CoAgentStateRenderBridge }], []);
1484
1731
  return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(ToastProvider, { enabled, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(CopilotErrorBoundary, { publicApiKey, showUsageBanner: enabled, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(ThreadsProvider, { threadId: props.threadId, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
1485
- import_react15.CopilotKitProvider,
1732
+ import_react16.CopilotKitProvider,
1486
1733
  __spreadProps(__spreadValues({}, props), {
1487
1734
  showDevConsole: showInspector,
1488
1735
  renderCustomMessages: renderArr,
@@ -1491,44 +1738,83 @@ function CopilotKit(_a) {
1491
1738
  })
1492
1739
  ) }) }) });
1493
1740
  }
1741
+ function CopilotKitErrorBridge() {
1742
+ const { copilotkit } = (0, import_react16.useCopilotKit)();
1743
+ const { onError, copilotApiConfig } = useCopilotContext();
1744
+ (0, import_react15.useEffect)(() => {
1745
+ if (!copilotkit)
1746
+ return;
1747
+ const subscription = copilotkit.subscribe({
1748
+ onError: (event) => __async(this, null, function* () {
1749
+ const errorEvent = {
1750
+ type: "error",
1751
+ timestamp: Date.now(),
1752
+ context: __spreadValues({
1753
+ source: "agent",
1754
+ request: {
1755
+ operation: event.code || "unknown",
1756
+ url: copilotApiConfig == null ? void 0 : copilotApiConfig.chatApiEndpoint,
1757
+ startTime: Date.now()
1758
+ },
1759
+ technical: {
1760
+ environment: "browser",
1761
+ userAgent: typeof navigator !== "undefined" ? navigator.userAgent : void 0,
1762
+ stackTrace: event.error.stack
1763
+ }
1764
+ }, event.context),
1765
+ error: event.error
1766
+ };
1767
+ try {
1768
+ yield onError(errorEvent);
1769
+ } catch (handlerError) {
1770
+ console.error("Error in onError handler:", handlerError);
1771
+ }
1772
+ })
1773
+ });
1774
+ return () => {
1775
+ subscription.unsubscribe();
1776
+ };
1777
+ }, [copilotkit, onError, copilotApiConfig]);
1778
+ return null;
1779
+ }
1494
1780
  function CopilotKitInternal(cpkProps) {
1495
1781
  var _b;
1496
1782
  const _a = cpkProps, { children } = _a, props = __objRest(_a, ["children"]);
1497
1783
  validateProps(cpkProps);
1498
1784
  const publicApiKey = props.publicLicenseKey || props.publicApiKey;
1499
- const chatApiEndpoint = props.runtimeUrl || import_shared12.COPILOT_CLOUD_CHAT_URL;
1500
- const [actions, setActions] = (0, import_react14.useState)({});
1501
- const [registeredActionConfigs, setRegisteredActionConfigs] = (0, import_react14.useState)(/* @__PURE__ */ new Map());
1502
- const chatComponentsCache = (0, import_react14.useRef)({
1785
+ const chatApiEndpoint = props.runtimeUrl || import_shared13.COPILOT_CLOUD_CHAT_URL;
1786
+ const [actions, setActions] = (0, import_react15.useState)({});
1787
+ const [registeredActionConfigs, setRegisteredActionConfigs] = (0, import_react15.useState)(/* @__PURE__ */ new Map());
1788
+ const chatComponentsCache = (0, import_react15.useRef)({
1503
1789
  actions: {},
1504
1790
  coAgentStateRenders: {}
1505
1791
  });
1506
1792
  const { addElement, removeElement, printTree, getAllElements } = use_tree_default();
1507
- const [isLoading, setIsLoading] = (0, import_react14.useState)(false);
1508
- const [chatInstructions, setChatInstructions] = (0, import_react14.useState)("");
1509
- const [authStates, setAuthStates] = (0, import_react14.useState)({});
1510
- const [extensions, setExtensions] = (0, import_react14.useState)({});
1511
- const [additionalInstructions, setAdditionalInstructions] = (0, import_react14.useState)([]);
1793
+ const [isLoading, setIsLoading] = (0, import_react15.useState)(false);
1794
+ const [chatInstructions, setChatInstructions] = (0, import_react15.useState)("");
1795
+ const [authStates, setAuthStates] = (0, import_react15.useState)({});
1796
+ const [extensions, setExtensions] = (0, import_react15.useState)({});
1797
+ const [additionalInstructions, setAdditionalInstructions] = (0, import_react15.useState)([]);
1512
1798
  const {
1513
1799
  addElement: addDocument,
1514
1800
  removeElement: removeDocument,
1515
1801
  allElements: allDocuments
1516
1802
  } = use_flat_category_store_default();
1517
- const setAction = (0, import_react14.useCallback)((id, action) => {
1803
+ const setAction = (0, import_react15.useCallback)((id, action) => {
1518
1804
  setActions((prevPoints) => {
1519
1805
  return __spreadProps(__spreadValues({}, prevPoints), {
1520
1806
  [id]: action
1521
1807
  });
1522
1808
  });
1523
1809
  }, []);
1524
- const removeAction = (0, import_react14.useCallback)((id) => {
1810
+ const removeAction = (0, import_react15.useCallback)((id) => {
1525
1811
  setActions((prevPoints) => {
1526
1812
  const newPoints = __spreadValues({}, prevPoints);
1527
1813
  delete newPoints[id];
1528
1814
  return newPoints;
1529
1815
  });
1530
1816
  }, []);
1531
- const getContextString = (0, import_react14.useCallback)(
1817
+ const getContextString = (0, import_react15.useCallback)(
1532
1818
  (documents, categories) => {
1533
1819
  const documentsString = documents.map((document) => {
1534
1820
  return `${document.name} (${document.sourceApplication}):
@@ -1541,46 +1827,46 @@ ${nonDocumentStrings}`;
1541
1827
  },
1542
1828
  [printTree]
1543
1829
  );
1544
- const addContext = (0, import_react14.useCallback)(
1830
+ const addContext = (0, import_react15.useCallback)(
1545
1831
  (context, parentId, categories = defaultCopilotContextCategories) => {
1546
1832
  return addElement(context, categories, parentId);
1547
1833
  },
1548
1834
  [addElement]
1549
1835
  );
1550
- const removeContext = (0, import_react14.useCallback)(
1836
+ const removeContext = (0, import_react15.useCallback)(
1551
1837
  (id) => {
1552
1838
  removeElement(id);
1553
1839
  },
1554
1840
  [removeElement]
1555
1841
  );
1556
- const getAllContext = (0, import_react14.useCallback)(() => {
1842
+ const getAllContext = (0, import_react15.useCallback)(() => {
1557
1843
  return getAllElements();
1558
1844
  }, [getAllElements]);
1559
- const getFunctionCallHandler = (0, import_react14.useCallback)(
1845
+ const getFunctionCallHandler = (0, import_react15.useCallback)(
1560
1846
  (customEntryPoints) => {
1561
1847
  return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || actions));
1562
1848
  },
1563
1849
  [actions]
1564
1850
  );
1565
- const getDocumentsContext = (0, import_react14.useCallback)(
1851
+ const getDocumentsContext = (0, import_react15.useCallback)(
1566
1852
  (categories) => {
1567
1853
  return allDocuments(categories);
1568
1854
  },
1569
1855
  [allDocuments]
1570
1856
  );
1571
- const addDocumentContext = (0, import_react14.useCallback)(
1857
+ const addDocumentContext = (0, import_react15.useCallback)(
1572
1858
  (documentPointer, categories = defaultCopilotContextCategories) => {
1573
1859
  return addDocument(documentPointer, categories);
1574
1860
  },
1575
1861
  [addDocument]
1576
1862
  );
1577
- const removeDocumentContext = (0, import_react14.useCallback)(
1863
+ const removeDocumentContext = (0, import_react15.useCallback)(
1578
1864
  (documentId) => {
1579
1865
  removeDocument(documentId);
1580
1866
  },
1581
1867
  [removeDocument]
1582
1868
  );
1583
- const copilotApiConfig = (0, import_react14.useMemo)(() => {
1869
+ const copilotApiConfig = (0, import_react15.useMemo)(() => {
1584
1870
  var _a2, _b2;
1585
1871
  let cloud = void 0;
1586
1872
  if (publicApiKey) {
@@ -1616,7 +1902,7 @@ ${nonDocumentStrings}`;
1616
1902
  props.cloudRestrictToTopic,
1617
1903
  props.guardrails_c
1618
1904
  ]);
1619
- const headers = (0, import_react14.useMemo)(() => {
1905
+ const headers = (0, import_react15.useMemo)(() => {
1620
1906
  const authHeaders = Object.values(authStates || {}).reduce((acc, state) => {
1621
1907
  if (state.status === "authenticated" && state.authHeaders) {
1622
1908
  return __spreadValues(__spreadValues({}, acc), Object.entries(state.authHeaders).reduce(
@@ -1628,27 +1914,27 @@ ${nonDocumentStrings}`;
1628
1914
  }
1629
1915
  return acc;
1630
1916
  }, {});
1631
- return __spreadValues(__spreadValues(__spreadValues({}, copilotApiConfig.headers || {}), copilotApiConfig.publicApiKey ? { [import_shared12.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: copilotApiConfig.publicApiKey } : {}), authHeaders);
1917
+ return __spreadValues(__spreadValues(__spreadValues({}, copilotApiConfig.headers || {}), copilotApiConfig.publicApiKey ? { [import_shared13.COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: copilotApiConfig.publicApiKey } : {}), authHeaders);
1632
1918
  }, [copilotApiConfig.headers, copilotApiConfig.publicApiKey, authStates]);
1633
- const [internalErrorHandlers, _setInternalErrorHandler] = (0, import_react14.useState)({});
1634
- const setInternalErrorHandler = (0, import_react14.useCallback)((handler) => {
1919
+ const [internalErrorHandlers, _setInternalErrorHandler] = (0, import_react15.useState)({});
1920
+ const setInternalErrorHandler = (0, import_react15.useCallback)((handler) => {
1635
1921
  _setInternalErrorHandler((prev) => __spreadValues(__spreadValues({}, prev), handler));
1636
1922
  }, []);
1637
- const removeInternalErrorHandler = (0, import_react14.useCallback)((key) => {
1923
+ const removeInternalErrorHandler = (0, import_react15.useCallback)((key) => {
1638
1924
  _setInternalErrorHandler((prev) => {
1639
1925
  const _a2 = prev, { [key]: _removed } = _a2, rest = __objRest(_a2, [__restKey(key)]);
1640
1926
  return rest;
1641
1927
  });
1642
1928
  }, []);
1643
- const onErrorRef = (0, import_react14.useRef)(props.onError);
1644
- (0, import_react14.useEffect)(() => {
1929
+ const onErrorRef = (0, import_react15.useRef)(props.onError);
1930
+ (0, import_react15.useEffect)(() => {
1645
1931
  onErrorRef.current = props.onError;
1646
1932
  }, [props.onError]);
1647
- const internalHandlersRef = (0, import_react14.useRef)({});
1648
- (0, import_react14.useEffect)(() => {
1933
+ const internalHandlersRef = (0, import_react15.useRef)({});
1934
+ (0, import_react15.useEffect)(() => {
1649
1935
  internalHandlersRef.current = internalErrorHandlers;
1650
1936
  }, [internalErrorHandlers]);
1651
- const handleErrors = (0, import_react14.useCallback)(
1937
+ const handleErrors = (0, import_react15.useCallback)(
1652
1938
  (error) => __async(this, null, function* () {
1653
1939
  if (copilotApiConfig.publicApiKey && onErrorRef.current) {
1654
1940
  try {
@@ -1668,14 +1954,14 @@ ${nonDocumentStrings}`;
1668
1954
  }),
1669
1955
  [copilotApiConfig.publicApiKey]
1670
1956
  );
1671
- const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react14.useState)({});
1672
- const addChatSuggestionConfiguration = (0, import_react14.useCallback)(
1957
+ const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react15.useState)({});
1958
+ const addChatSuggestionConfiguration = (0, import_react15.useCallback)(
1673
1959
  (id, suggestion) => {
1674
1960
  setChatSuggestionConfiguration((prev) => __spreadProps(__spreadValues({}, prev), { [id]: suggestion }));
1675
1961
  },
1676
1962
  [setChatSuggestionConfiguration]
1677
1963
  );
1678
- const removeChatSuggestionConfiguration = (0, import_react14.useCallback)(
1964
+ const removeChatSuggestionConfiguration = (0, import_react15.useCallback)(
1679
1965
  (id) => {
1680
1966
  setChatSuggestionConfiguration((prev) => {
1681
1967
  const _a2 = prev, { [id]: _ } = _a2, rest = __objRest(_a2, [__restKey(id)]);
@@ -1684,10 +1970,10 @@ ${nonDocumentStrings}`;
1684
1970
  },
1685
1971
  [setChatSuggestionConfiguration]
1686
1972
  );
1687
- const [availableAgents, setAvailableAgents] = (0, import_react14.useState)([]);
1688
- const [coagentStates, setCoagentStates] = (0, import_react14.useState)({});
1689
- const coagentStatesRef = (0, import_react14.useRef)({});
1690
- const setCoagentStatesWithRef = (0, import_react14.useCallback)(
1973
+ const [availableAgents, setAvailableAgents] = (0, import_react15.useState)([]);
1974
+ const [coagentStates, setCoagentStates] = (0, import_react15.useState)({});
1975
+ const coagentStatesRef = (0, import_react15.useRef)({});
1976
+ const setCoagentStatesWithRef = (0, import_react15.useCallback)(
1691
1977
  (value) => {
1692
1978
  const newValue = typeof value === "function" ? value(coagentStatesRef.current) : value;
1693
1979
  coagentStatesRef.current = newValue;
@@ -1703,8 +1989,8 @@ ${nonDocumentStrings}`;
1703
1989
  agentName: props.agent
1704
1990
  };
1705
1991
  }
1706
- const [agentSession, setAgentSession] = (0, import_react14.useState)(initialAgentSession);
1707
- (0, import_react14.useEffect)(() => {
1992
+ const [agentSession, setAgentSession] = (0, import_react15.useState)(initialAgentSession);
1993
+ (0, import_react15.useEffect)(() => {
1708
1994
  if (props.agent) {
1709
1995
  setAgentSession({
1710
1996
  agentName: props.agent
@@ -1714,7 +2000,7 @@ ${nonDocumentStrings}`;
1714
2000
  }
1715
2001
  }, [props.agent]);
1716
2002
  const { threadId, setThreadId: setInternalThreadId } = useThreads();
1717
- const setThreadId = (0, import_react14.useCallback)(
2003
+ const setThreadId = (0, import_react15.useCallback)(
1718
2004
  (value) => {
1719
2005
  if (props.threadId) {
1720
2006
  throw new Error("Cannot call setThreadId() when threadId is provided via props.");
@@ -1723,11 +2009,11 @@ ${nonDocumentStrings}`;
1723
2009
  },
1724
2010
  [props.threadId]
1725
2011
  );
1726
- const [runId, setRunId] = (0, import_react14.useState)(null);
1727
- const chatAbortControllerRef = (0, import_react14.useRef)(null);
2012
+ const [runId, setRunId] = (0, import_react15.useState)(null);
2013
+ const chatAbortControllerRef = (0, import_react15.useRef)(null);
1728
2014
  const showDevConsole = shouldShowDevConsole(props.showDevConsole);
1729
- const [interruptActions, _setInterruptActions] = (0, import_react14.useState)({});
1730
- const setInterruptAction = (0, import_react14.useCallback)((action) => {
2015
+ const [interruptActions, _setInterruptActions] = (0, import_react15.useState)({});
2016
+ const setInterruptAction = (0, import_react15.useCallback)((action) => {
1731
2017
  _setInterruptActions((prev) => {
1732
2018
  var _a2;
1733
2019
  if (action == null || !action.id) {
@@ -1738,14 +2024,14 @@ ${nonDocumentStrings}`;
1738
2024
  });
1739
2025
  });
1740
2026
  }, []);
1741
- const removeInterruptAction = (0, import_react14.useCallback)((actionId) => {
2027
+ const removeInterruptAction = (0, import_react15.useCallback)((actionId) => {
1742
2028
  _setInterruptActions((prev) => {
1743
2029
  const _a2 = prev, { [actionId]: _ } = _a2, rest = __objRest(_a2, [__restKey(actionId)]);
1744
2030
  return rest;
1745
2031
  });
1746
2032
  }, []);
1747
- const [interruptEventQueue, setInterruptEventQueue] = (0, import_react14.useState)({});
1748
- const addInterruptEvent = (0, import_react14.useCallback)((queuedEvent) => {
2033
+ const [interruptEventQueue, setInterruptEventQueue] = (0, import_react15.useState)({});
2034
+ const addInterruptEvent = (0, import_react15.useCallback)((queuedEvent) => {
1749
2035
  setInterruptEventQueue((prev) => {
1750
2036
  const threadQueue = prev[queuedEvent.threadId] || [];
1751
2037
  return __spreadProps(__spreadValues({}, prev), {
@@ -1753,7 +2039,7 @@ ${nonDocumentStrings}`;
1753
2039
  });
1754
2040
  });
1755
2041
  }, []);
1756
- const resolveInterruptEvent = (0, import_react14.useCallback)(
2042
+ const resolveInterruptEvent = (0, import_react15.useCallback)(
1757
2043
  (threadId2, eventId, response) => {
1758
2044
  setInterruptEventQueue((prev) => {
1759
2045
  const threadQueue = prev[threadId2] || [];
@@ -1766,20 +2052,20 @@ ${nonDocumentStrings}`;
1766
2052
  },
1767
2053
  []
1768
2054
  );
1769
- const memoizedChildren = (0, import_react14.useMemo)(() => children, [children]);
1770
- const [bannerError, setBannerError] = (0, import_react14.useState)(null);
1771
- const agentLock = (0, import_react14.useMemo)(() => {
2055
+ const memoizedChildren = (0, import_react15.useMemo)(() => children, [children]);
2056
+ const [bannerError, setBannerError] = (0, import_react15.useState)(null);
2057
+ const agentLock = (0, import_react15.useMemo)(() => {
1772
2058
  var _a2;
1773
2059
  return (_a2 = props.agent) != null ? _a2 : null;
1774
2060
  }, [props.agent]);
1775
- const forwardedParameters = (0, import_react14.useMemo)(
2061
+ const forwardedParameters = (0, import_react15.useMemo)(
1776
2062
  () => {
1777
2063
  var _a2;
1778
2064
  return (_a2 = props.forwardedParameters) != null ? _a2 : {};
1779
2065
  },
1780
2066
  [props.forwardedParameters]
1781
2067
  );
1782
- const updateExtensions = (0, import_react14.useCallback)(
2068
+ const updateExtensions = (0, import_react15.useCallback)(
1783
2069
  (newExtensions) => {
1784
2070
  setExtensions((prev) => {
1785
2071
  const resolved = typeof newExtensions === "function" ? newExtensions(prev) : newExtensions;
@@ -1791,7 +2077,7 @@ ${nonDocumentStrings}`;
1791
2077
  },
1792
2078
  [setExtensions]
1793
2079
  );
1794
- const updateAuthStates = (0, import_react14.useCallback)(
2080
+ const updateAuthStates = (0, import_react15.useCallback)(
1795
2081
  (newAuthStates) => {
1796
2082
  setAuthStates((prev) => {
1797
2083
  const resolved = typeof newAuthStates === "function" ? newAuthStates(prev) : newAuthStates;
@@ -1803,8 +2089,8 @@ ${nonDocumentStrings}`;
1803
2089
  },
1804
2090
  [setAuthStates]
1805
2091
  );
1806
- const handleSetRegisteredActions = (0, import_react14.useCallback)((actionConfig) => {
1807
- const key = actionConfig.action.name || (0, import_shared12.randomUUID)();
2092
+ const handleSetRegisteredActions = (0, import_react15.useCallback)((actionConfig) => {
2093
+ const key = actionConfig.action.name || (0, import_shared13.randomUUID)();
1808
2094
  setRegisteredActionConfigs((prev) => {
1809
2095
  const newMap = new Map(prev);
1810
2096
  newMap.set(key, actionConfig);
@@ -1812,21 +2098,21 @@ ${nonDocumentStrings}`;
1812
2098
  });
1813
2099
  return key;
1814
2100
  }, []);
1815
- const handleRemoveRegisteredAction = (0, import_react14.useCallback)((actionKey) => {
2101
+ const handleRemoveRegisteredAction = (0, import_react15.useCallback)((actionKey) => {
1816
2102
  setRegisteredActionConfigs((prev) => {
1817
2103
  const newMap = new Map(prev);
1818
2104
  newMap.delete(actionKey);
1819
2105
  return newMap;
1820
2106
  });
1821
2107
  }, []);
1822
- const RegisteredActionsRenderer = (0, import_react14.useMemo)(() => {
2108
+ const RegisteredActionsRenderer = (0, import_react15.useMemo)(() => {
1823
2109
  return () => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_jsx_runtime7.Fragment, { children: Array.from(registeredActionConfigs.entries()).map(([key, config]) => {
1824
2110
  const Component = config.component;
1825
2111
  return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Component, { action: config.action }, key);
1826
2112
  }) });
1827
2113
  }, [registeredActionConfigs]);
1828
2114
  return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
1829
- import_react15.CopilotChatConfigurationProvider,
2115
+ import_react16.CopilotChatConfigurationProvider,
1830
2116
  {
1831
2117
  agentId: (_b = props.agent) != null ? _b : "default",
1832
2118
  threadId,
@@ -1893,6 +2179,7 @@ ${nonDocumentStrings}`;
1893
2179
  },
1894
2180
  children: [
1895
2181
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(CopilotListeners, {}),
2182
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(CopilotKitErrorBridge, {}),
1896
2183
  /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(CoAgentStateRendersProvider, { children: [
1897
2184
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(MessagesTapProvider, { children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(CopilotMessages, { children: [
1898
2185
  memoizedChildren,
@@ -1947,12 +2234,12 @@ function validateProps(props) {
1947
2234
  const cloudFeatures = Object.keys(props).filter((key) => key.endsWith("_c"));
1948
2235
  const hasApiKey = props.publicApiKey || props.publicLicenseKey;
1949
2236
  if (!props.runtimeUrl && !hasApiKey) {
1950
- throw new import_shared12.ConfigurationError(
2237
+ throw new import_shared13.ConfigurationError(
1951
2238
  "Missing required prop: 'runtimeUrl' or 'publicApiKey' or 'publicLicenseKey'"
1952
2239
  );
1953
2240
  }
1954
2241
  if (cloudFeatures.length > 0 && !hasApiKey) {
1955
- throw new import_shared12.MissingPublicApiKeyError(
2242
+ throw new import_shared13.MissingPublicApiKeyError(
1956
2243
  `Missing required prop: 'publicApiKey' or 'publicLicenseKey' to use cloud features: ${cloudFeatures.map(formatFeatureName).join(", ")}`
1957
2244
  );
1958
2245
  }