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