@copilotkit/react-core 1.5.0-tyler-reset-chat.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/CHANGELOG.md +158 -4
  2. package/README.md +2 -0
  3. package/dist/{chunk-3AYELZJS.mjs → chunk-35EN6BG4.mjs} +2 -2
  4. package/dist/{chunk-3AYELZJS.mjs.map → chunk-35EN6BG4.mjs.map} +1 -1
  5. package/dist/{chunk-SEPYQHH7.mjs → chunk-42N5VKIX.mjs} +34 -28
  6. package/dist/chunk-42N5VKIX.mjs.map +1 -0
  7. package/dist/{chunk-USL3EHJB.mjs → chunk-5FYKUKG3.mjs} +2 -2
  8. package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
  9. package/dist/chunk-7LRDVJH5.mjs.map +1 -0
  10. package/dist/{chunk-CZMEZR6F.mjs → chunk-BT6WK2JZ.mjs} +34 -19
  11. package/dist/chunk-BT6WK2JZ.mjs.map +1 -0
  12. package/dist/{chunk-3R4J2TPH.mjs → chunk-EUU6NNYU.mjs} +29 -13
  13. package/dist/chunk-EUU6NNYU.mjs.map +1 -0
  14. package/dist/chunk-QCUP6HLK.mjs +37 -0
  15. package/dist/chunk-QCUP6HLK.mjs.map +1 -0
  16. package/dist/chunk-QTDCEDOC.mjs +392 -0
  17. package/dist/chunk-QTDCEDOC.mjs.map +1 -0
  18. package/dist/{chunk-JR55I3FL.mjs → chunk-QX6V774L.mjs} +6 -8
  19. package/dist/chunk-QX6V774L.mjs.map +1 -0
  20. package/dist/{chunk-2KCEHGSI.mjs → chunk-SFPANIOY.mjs} +99 -49
  21. package/dist/chunk-SFPANIOY.mjs.map +1 -0
  22. package/dist/{chunk-2JP64U3A.mjs → chunk-TQN3EZWQ.mjs} +4 -1
  23. package/dist/chunk-TQN3EZWQ.mjs.map +1 -0
  24. package/dist/{chunk-XUPO37VH.mjs → chunk-V3PFWGIY.mjs} +2 -2
  25. package/dist/{chunk-6QKA3SNN.mjs → chunk-VMP6JWBB.mjs} +21 -5
  26. package/dist/chunk-VMP6JWBB.mjs.map +1 -0
  27. package/dist/chunk-XERJQUHA.mjs +31 -0
  28. package/dist/chunk-XERJQUHA.mjs.map +1 -0
  29. package/dist/components/copilot-provider/copilotkit.js +173 -92
  30. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  31. package/dist/components/copilot-provider/copilotkit.mjs +5 -4
  32. package/dist/components/copilot-provider/index.js +173 -92
  33. package/dist/components/copilot-provider/index.js.map +1 -1
  34. package/dist/components/copilot-provider/index.mjs +5 -4
  35. package/dist/components/error-boundary/error-boundary.d.ts +22 -0
  36. package/dist/components/error-boundary/error-boundary.js +183 -0
  37. package/dist/components/error-boundary/error-boundary.js.map +1 -0
  38. package/dist/components/error-boundary/error-boundary.mjs +12 -0
  39. package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
  40. package/dist/components/error-boundary/error-utils.d.ts +11 -0
  41. package/dist/components/error-boundary/error-utils.js +177 -0
  42. package/dist/components/error-boundary/error-utils.js.map +1 -0
  43. package/dist/components/error-boundary/error-utils.mjs +13 -0
  44. package/dist/components/error-boundary/error-utils.mjs.map +1 -0
  45. package/dist/components/index.js +173 -92
  46. package/dist/components/index.js.map +1 -1
  47. package/dist/components/index.mjs +5 -4
  48. package/dist/components/toast/toast-provider.d.ts +2 -1
  49. package/dist/components/toast/toast-provider.js +76 -62
  50. package/dist/components/toast/toast-provider.js.map +1 -1
  51. package/dist/components/toast/toast-provider.mjs +1 -1
  52. package/dist/context/copilot-context.d.ts +4 -2
  53. package/dist/context/copilot-context.js +3 -0
  54. package/dist/context/copilot-context.js.map +1 -1
  55. package/dist/context/copilot-context.mjs +1 -1
  56. package/dist/context/index.d.ts +1 -1
  57. package/dist/context/index.js +3 -0
  58. package/dist/context/index.js.map +1 -1
  59. package/dist/context/index.mjs +1 -1
  60. package/dist/hooks/index.js +554 -308
  61. package/dist/hooks/index.js.map +1 -1
  62. package/dist/hooks/index.mjs +13 -11
  63. package/dist/hooks/use-chat.d.ts +6 -2
  64. package/dist/hooks/use-chat.js +434 -219
  65. package/dist/hooks/use-chat.js.map +1 -1
  66. package/dist/hooks/use-chat.mjs +4 -3
  67. package/dist/hooks/use-coagent-state-render.d.ts +2 -2
  68. package/dist/hooks/use-coagent-state-render.js +3 -0
  69. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  70. package/dist/hooks/use-coagent-state-render.mjs +2 -2
  71. package/dist/hooks/use-coagent.d.ts +1 -1
  72. package/dist/hooks/use-coagent.js +510 -277
  73. package/dist/hooks/use-coagent.js.map +1 -1
  74. package/dist/hooks/use-coagent.mjs +9 -7
  75. package/dist/hooks/use-copilot-action.d.ts +12 -2
  76. package/dist/hooks/use-copilot-action.js +157 -16
  77. package/dist/hooks/use-copilot-action.js.map +1 -1
  78. package/dist/hooks/use-copilot-action.mjs +4 -2
  79. package/dist/hooks/use-copilot-chat.d.ts +1 -0
  80. package/dist/hooks/use-copilot-chat.js +483 -253
  81. package/dist/hooks/use-copilot-chat.js.map +1 -1
  82. package/dist/hooks/use-copilot-chat.mjs +8 -6
  83. package/dist/hooks/use-copilot-readable.js +3 -0
  84. package/dist/hooks/use-copilot-readable.js.map +1 -1
  85. package/dist/hooks/use-copilot-readable.mjs +2 -2
  86. package/dist/hooks/use-copilot-runtime-client.js +110 -4
  87. package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
  88. package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
  89. package/dist/hooks/use-make-copilot-document-readable.js +3 -0
  90. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  91. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  92. package/dist/index.d.ts +1 -1
  93. package/dist/index.js +616 -401
  94. package/dist/index.js.map +1 -1
  95. package/dist/index.mjs +14 -12
  96. package/dist/lib/copilot-task.d.ts +1 -1
  97. package/dist/lib/copilot-task.js +33 -13
  98. package/dist/lib/copilot-task.js.map +1 -1
  99. package/dist/lib/copilot-task.mjs +7 -5
  100. package/dist/lib/index.d.ts +1 -1
  101. package/dist/lib/index.js +33 -13
  102. package/dist/lib/index.js.map +1 -1
  103. package/dist/lib/index.mjs +7 -5
  104. package/dist/types/frontend-action.d.ts +21 -2
  105. package/dist/types/frontend-action.js +34 -0
  106. package/dist/types/frontend-action.js.map +1 -1
  107. package/dist/types/frontend-action.mjs +7 -0
  108. package/dist/types/index.d.ts +2 -1
  109. package/dist/types/index.js.map +1 -1
  110. package/dist/utils/extract.js.map +1 -1
  111. package/dist/utils/extract.mjs +5 -4
  112. package/dist/utils/index.js.map +1 -1
  113. package/dist/utils/index.mjs +5 -4
  114. package/package.json +5 -5
  115. package/src/components/copilot-provider/copilotkit.tsx +22 -1
  116. package/src/components/error-boundary/error-boundary.tsx +42 -0
  117. package/src/components/error-boundary/error-utils.tsx +95 -0
  118. package/src/components/toast/toast-provider.tsx +10 -49
  119. package/src/context/copilot-context.tsx +17 -2
  120. package/src/hooks/use-chat.ts +375 -279
  121. package/src/hooks/use-coagent-state-render.ts +2 -2
  122. package/src/hooks/use-coagent.ts +34 -28
  123. package/src/hooks/use-copilot-action.ts +50 -15
  124. package/src/hooks/use-copilot-chat.ts +28 -14
  125. package/src/hooks/use-copilot-runtime-client.ts +4 -0
  126. package/src/lib/copilot-task.ts +2 -8
  127. package/src/types/frontend-action.ts +55 -2
  128. package/src/types/index.ts +5 -1
  129. package/dist/chunk-2JP64U3A.mjs.map +0 -1
  130. package/dist/chunk-2KCEHGSI.mjs.map +0 -1
  131. package/dist/chunk-3R4J2TPH.mjs.map +0 -1
  132. package/dist/chunk-6EN7J4V2.mjs +0 -317
  133. package/dist/chunk-6EN7J4V2.mjs.map +0 -1
  134. package/dist/chunk-6QKA3SNN.mjs.map +0 -1
  135. package/dist/chunk-CZMEZR6F.mjs.map +0 -1
  136. package/dist/chunk-JR55I3FL.mjs.map +0 -1
  137. package/dist/chunk-ODN4H66E.mjs.map +0 -1
  138. package/dist/chunk-SEPYQHH7.mjs.map +0 -1
  139. /package/dist/{chunk-USL3EHJB.mjs.map → chunk-5FYKUKG3.mjs.map} +0 -0
  140. /package/dist/{chunk-XUPO37VH.mjs.map → chunk-V3PFWGIY.mjs.map} +0 -0
@@ -86,7 +86,7 @@ __export(components_exports, {
86
86
  module.exports = __toCommonJS(components_exports);
87
87
 
88
88
  // src/components/copilot-provider/copilotkit.tsx
89
- var import_react8 = require("react");
89
+ var import_react10 = require("react");
90
90
 
91
91
  // src/context/copilot-context.tsx
92
92
  var import_react = __toESM(require("react"));
@@ -137,6 +137,9 @@ var emptyCopilotContext = {
137
137
  coagentStates: {},
138
138
  setCoagentStates: () => {
139
139
  },
140
+ coagentStatesRef: { current: {} },
141
+ setCoagentStatesWithRef: () => {
142
+ },
140
143
  agentSession: null,
141
144
  setAgentSession: () => {
142
145
  },
@@ -385,6 +388,9 @@ function CopilotMessages(_a) {
385
388
  }
386
389
 
387
390
  // src/components/toast/toast-provider.tsx
391
+ var import_react7 = require("react");
392
+
393
+ // src/components/error-boundary/error-utils.tsx
388
394
  var import_react6 = require("react");
389
395
 
390
396
  // src/components/toast/exclamation-mark-icon.tsx
@@ -414,11 +420,82 @@ var ExclamationMarkIcon = ({
414
420
  }
415
421
  );
416
422
 
417
- // src/components/toast/toast-provider.tsx
423
+ // src/components/error-boundary/error-utils.tsx
418
424
  var import_jsx_runtime3 = require("react/jsx-runtime");
419
- var ToastContext = (0, import_react6.createContext)(void 0);
425
+ function ErrorToast({ errors }) {
426
+ const errorsToRender = errors.map((error, idx) => {
427
+ var _a, _b, _c;
428
+ const message = "extensions" in error ? (_b = (_a = error.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message : error.message;
429
+ const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
430
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
431
+ "div",
432
+ {
433
+ style: {
434
+ marginTop: idx === 0 ? 0 : 10,
435
+ marginBottom: 14
436
+ },
437
+ children: [
438
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
439
+ code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
440
+ "div",
441
+ {
442
+ style: {
443
+ fontWeight: "600",
444
+ marginBottom: 4
445
+ },
446
+ children: [
447
+ "Copilot Cloud Error:",
448
+ " ",
449
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
450
+ ]
451
+ }
452
+ ),
453
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
454
+ ]
455
+ },
456
+ idx
457
+ );
458
+ });
459
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
460
+ "div",
461
+ {
462
+ style: {
463
+ fontSize: "13px",
464
+ maxWidth: "600px"
465
+ },
466
+ children: [
467
+ errorsToRender,
468
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
469
+ ]
470
+ }
471
+ );
472
+ }
473
+ function useErrorToast() {
474
+ const { addToast } = useToast();
475
+ return (0, import_react6.useCallback)(
476
+ (error) => {
477
+ const errorId = error.map((err) => {
478
+ var _a, _b;
479
+ const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
480
+ const stack = err.stack || "";
481
+ return btoa(message + stack).slice(0, 32);
482
+ }).join("|");
483
+ addToast({
484
+ type: "error",
485
+ id: errorId,
486
+ // Toast libraries typically dedupe by id
487
+ message: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ErrorToast, { errors: error })
488
+ });
489
+ },
490
+ [addToast]
491
+ );
492
+ }
493
+
494
+ // src/components/toast/toast-provider.tsx
495
+ var import_jsx_runtime4 = require("react/jsx-runtime");
496
+ var ToastContext = (0, import_react7.createContext)(void 0);
420
497
  function useToast() {
421
- const context = (0, import_react6.useContext)(ToastContext);
498
+ const context = (0, import_react7.useContext)(ToastContext);
422
499
  if (!context) {
423
500
  throw new Error("useToast must be used within a ToastProvider");
424
501
  }
@@ -428,68 +505,28 @@ function ToastProvider({
428
505
  enabled,
429
506
  children
430
507
  }) {
431
- const [toasts, setToasts] = (0, import_react6.useState)([]);
432
- const addToast = (0, import_react6.useCallback)((toast) => {
433
- const id = Math.random().toString(36).substring(2, 9);
434
- setToasts((currentToasts) => [...currentToasts, __spreadProps(__spreadValues({}, toast), { id })]);
508
+ const [toasts, setToasts] = (0, import_react7.useState)([]);
509
+ const addToast = (0, import_react7.useCallback)((toast) => {
510
+ var _a;
511
+ const id = (_a = toast.id) != null ? _a : Math.random().toString(36).substring(2, 9);
512
+ setToasts((currentToasts) => {
513
+ if (currentToasts.find((toast2) => toast2.id === id))
514
+ return currentToasts;
515
+ return [...currentToasts, __spreadProps(__spreadValues({}, toast), { id })];
516
+ });
435
517
  if (toast.duration) {
436
518
  setTimeout(() => {
437
519
  removeToast(id);
438
520
  }, toast.duration);
439
521
  }
440
522
  }, []);
441
- const addGraphQLErrorsToast = (0, import_react6.useCallback)((errors) => {
442
- const errorsToRender = errors.map((error, idx) => {
443
- var _a;
444
- const message = error.message;
445
- const code = (_a = error.extensions) == null ? void 0 : _a.code;
446
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
447
- "div",
448
- {
449
- style: {
450
- marginTop: idx === 0 ? 0 : 10,
451
- marginBottom: 14
452
- },
453
- children: [
454
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
455
- code && /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
456
- "div",
457
- {
458
- style: {
459
- fontWeight: "600",
460
- marginBottom: 4
461
- },
462
- children: [
463
- "Copilot Cloud Error:",
464
- " ",
465
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
466
- ]
467
- }
468
- ),
469
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message })
470
- ]
471
- },
472
- idx
473
- );
474
- });
523
+ const addGraphQLErrorsToast = (0, import_react7.useCallback)((errors) => {
475
524
  addToast({
476
525
  type: "error",
477
- message: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
478
- "div",
479
- {
480
- style: {
481
- fontSize: "13px",
482
- maxWidth: "600px"
483
- },
484
- children: [
485
- errorsToRender,
486
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This is a Copilot Cloud error, and it only displays during local development." })
487
- ]
488
- }
489
- )
526
+ message: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ErrorToast, { errors })
490
527
  });
491
528
  }, []);
492
- const removeToast = (0, import_react6.useCallback)((id) => {
529
+ const removeToast = (0, import_react7.useCallback)((id) => {
493
530
  setToasts((currentToasts) => currentToasts.filter((toast) => toast.id !== id));
494
531
  }, []);
495
532
  const value = {
@@ -499,8 +536,8 @@ function ToastProvider({
499
536
  removeToast,
500
537
  enabled
501
538
  };
502
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(ToastContext.Provider, { value, children: [
503
- /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
539
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(ToastContext.Provider, { value, children: [
540
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
504
541
  "div",
505
542
  {
506
543
  style: {
@@ -514,7 +551,7 @@ function ToastProvider({
514
551
  gap: "0.5rem"
515
552
  },
516
553
  children: [
517
- toasts.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
554
+ toasts.length > 1 && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { textAlign: "right" }, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
518
555
  "button",
519
556
  {
520
557
  onClick: () => setToasts([]),
@@ -529,7 +566,7 @@ function ToastProvider({
529
566
  children: "Close All"
530
567
  }
531
568
  ) }),
532
- toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
569
+ toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
533
570
  Toast,
534
571
  {
535
572
  message: toast.message,
@@ -555,7 +592,7 @@ function Toast({
555
592
  warning: "#eab308",
556
593
  error: "#ef4444"
557
594
  };
558
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
595
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
559
596
  "div",
560
597
  {
561
598
  style: {
@@ -568,8 +605,8 @@ function Toast({
568
605
  minWidth: "200px"
569
606
  },
570
607
  children: [
571
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: message }),
572
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
608
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: message }),
609
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
573
610
  "button",
574
611
  {
575
612
  onClick: onClose,
@@ -594,14 +631,17 @@ function Toast({
594
631
 
595
632
  // src/hooks/use-copilot-runtime-client.ts
596
633
  var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
597
- var import_react7 = require("react");
634
+ var import_react8 = require("react");
598
635
  var useCopilotRuntimeClient = (options) => {
599
636
  const { addGraphQLErrorsToast } = useToast();
600
- const runtimeClient = (0, import_react7.useMemo)(() => {
637
+ const addErrorToast = useErrorToast();
638
+ const runtimeClient = (0, import_react8.useMemo)(() => {
601
639
  return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
602
640
  handleGQLErrors: (error) => {
603
641
  if (error.graphQLErrors.length) {
604
642
  addGraphQLErrorsToast(error.graphQLErrors);
643
+ } else {
644
+ addErrorToast([error]);
605
645
  }
606
646
  }
607
647
  }));
@@ -623,13 +663,41 @@ function getHostname() {
623
663
  return "";
624
664
  }
625
665
 
666
+ // src/components/error-boundary/error-boundary.tsx
667
+ var import_react9 = __toESM(require("react"));
668
+ var import_jsx_runtime5 = require("react/jsx-runtime");
669
+ var CopilotErrorBoundary = class extends import_react9.default.Component {
670
+ constructor(props) {
671
+ super(props);
672
+ this.state = { hasError: false };
673
+ }
674
+ static getDerivedStateFromError(error) {
675
+ return { hasError: true, error };
676
+ }
677
+ componentDidCatch(error, errorInfo) {
678
+ console.error("CopilotKit Error:", error, errorInfo);
679
+ }
680
+ render() {
681
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(ErrorToast2, { error: this.state.error, children: this.props.children });
682
+ }
683
+ };
684
+ function ErrorToast2({ error, children }) {
685
+ const addErrorToast = useErrorToast();
686
+ (0, import_react9.useEffect)(() => {
687
+ if (error) {
688
+ addErrorToast([error]);
689
+ }
690
+ }, [error, addErrorToast]);
691
+ return children;
692
+ }
693
+
626
694
  // src/components/copilot-provider/copilotkit.tsx
627
- var import_jsx_runtime4 = require("react/jsx-runtime");
695
+ var import_jsx_runtime6 = require("react/jsx-runtime");
628
696
  function CopilotKit(_a) {
629
697
  var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
630
698
  const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
631
699
  const enabled = shouldShowDevConsole(showDevConsole);
632
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(ToastProvider, { enabled, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CopilotKitInternal, __spreadProps(__spreadValues({}, props), { children })) });
700
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(ToastProvider, { enabled, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotErrorBoundary, { children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotKitInternal, __spreadProps(__spreadValues({}, props), { children })) }) });
633
701
  }
634
702
  function CopilotKitInternal(_a) {
635
703
  var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
@@ -639,49 +707,49 @@ function CopilotKitInternal(_a) {
639
707
  );
640
708
  }
641
709
  const chatApiEndpoint = props.runtimeUrl || import_shared3.COPILOT_CLOUD_CHAT_URL;
642
- const [actions, setActions] = (0, import_react8.useState)({});
643
- const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react8.useState)({});
644
- const chatComponentsCache = (0, import_react8.useRef)({
710
+ const [actions, setActions] = (0, import_react10.useState)({});
711
+ const [coAgentStateRenders, setCoAgentStateRenders] = (0, import_react10.useState)({});
712
+ const chatComponentsCache = (0, import_react10.useRef)({
645
713
  actions: {},
646
714
  coAgentStateRenders: {}
647
715
  });
648
716
  const { addElement, removeElement, printTree } = use_tree_default();
649
- const [isLoading, setIsLoading] = (0, import_react8.useState)(false);
650
- const [chatInstructions, setChatInstructions] = (0, import_react8.useState)("");
717
+ const [isLoading, setIsLoading] = (0, import_react10.useState)(false);
718
+ const [chatInstructions, setChatInstructions] = (0, import_react10.useState)("");
651
719
  const {
652
720
  addElement: addDocument,
653
721
  removeElement: removeDocument,
654
722
  allElements: allDocuments
655
723
  } = use_flat_category_store_default();
656
- const setAction = (0, import_react8.useCallback)((id, action) => {
724
+ const setAction = (0, import_react10.useCallback)((id, action) => {
657
725
  setActions((prevPoints) => {
658
726
  return __spreadProps(__spreadValues({}, prevPoints), {
659
727
  [id]: action
660
728
  });
661
729
  });
662
730
  }, []);
663
- const removeAction = (0, import_react8.useCallback)((id) => {
731
+ const removeAction = (0, import_react10.useCallback)((id) => {
664
732
  setActions((prevPoints) => {
665
733
  const newPoints = __spreadValues({}, prevPoints);
666
734
  delete newPoints[id];
667
735
  return newPoints;
668
736
  });
669
737
  }, []);
670
- const setCoAgentStateRender = (0, import_react8.useCallback)((id, stateRender) => {
738
+ const setCoAgentStateRender = (0, import_react10.useCallback)((id, stateRender) => {
671
739
  setCoAgentStateRenders((prevPoints) => {
672
740
  return __spreadProps(__spreadValues({}, prevPoints), {
673
741
  [id]: stateRender
674
742
  });
675
743
  });
676
744
  }, []);
677
- const removeCoAgentStateRender = (0, import_react8.useCallback)((id) => {
745
+ const removeCoAgentStateRender = (0, import_react10.useCallback)((id) => {
678
746
  setCoAgentStateRenders((prevPoints) => {
679
747
  const newPoints = __spreadValues({}, prevPoints);
680
748
  delete newPoints[id];
681
749
  return newPoints;
682
750
  });
683
751
  }, []);
684
- const getContextString = (0, import_react8.useCallback)(
752
+ const getContextString = (0, import_react10.useCallback)(
685
753
  (documents, categories) => {
686
754
  const documentsString = documents.map((document) => {
687
755
  return `${document.name} (${document.sourceApplication}):
@@ -694,37 +762,37 @@ ${nonDocumentStrings}`;
694
762
  },
695
763
  [printTree]
696
764
  );
697
- const addContext = (0, import_react8.useCallback)(
765
+ const addContext = (0, import_react10.useCallback)(
698
766
  (context, parentId, categories = defaultCopilotContextCategories) => {
699
767
  return addElement(context, categories, parentId);
700
768
  },
701
769
  [addElement]
702
770
  );
703
- const removeContext = (0, import_react8.useCallback)(
771
+ const removeContext = (0, import_react10.useCallback)(
704
772
  (id) => {
705
773
  removeElement(id);
706
774
  },
707
775
  [removeElement]
708
776
  );
709
- const getFunctionCallHandler = (0, import_react8.useCallback)(
777
+ const getFunctionCallHandler = (0, import_react10.useCallback)(
710
778
  (customEntryPoints) => {
711
779
  return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || actions));
712
780
  },
713
781
  [actions]
714
782
  );
715
- const getDocumentsContext = (0, import_react8.useCallback)(
783
+ const getDocumentsContext = (0, import_react10.useCallback)(
716
784
  (categories) => {
717
785
  return allDocuments(categories);
718
786
  },
719
787
  [allDocuments]
720
788
  );
721
- const addDocumentContext = (0, import_react8.useCallback)(
789
+ const addDocumentContext = (0, import_react10.useCallback)(
722
790
  (documentPointer, categories = defaultCopilotContextCategories) => {
723
791
  return addDocument(documentPointer, categories);
724
792
  },
725
793
  [addDocument]
726
794
  );
727
- const removeDocumentContext = (0, import_react8.useCallback)(
795
+ const removeDocumentContext = (0, import_react10.useCallback)(
728
796
  (documentId) => {
729
797
  removeDocument(documentId);
730
798
  },
@@ -737,7 +805,7 @@ ${nonDocumentStrings}`;
737
805
  );
738
806
  }
739
807
  }
740
- const copilotApiConfig = (0, import_react8.useMemo)(() => {
808
+ const copilotApiConfig = (0, import_react10.useMemo)(() => {
741
809
  var _a2, _b2;
742
810
  let cloud = void 0;
743
811
  if (props.publicApiKey) {
@@ -779,7 +847,7 @@ ${nonDocumentStrings}`;
779
847
  headers,
780
848
  credentials: copilotApiConfig.credentials
781
849
  });
782
- const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react8.useState)({});
850
+ const [chatSuggestionConfiguration, setChatSuggestionConfiguration] = (0, import_react10.useState)({});
783
851
  const addChatSuggestionConfiguration = (id, suggestion) => {
784
852
  setChatSuggestionConfiguration((prev) => __spreadProps(__spreadValues({}, prev), { [id]: suggestion }));
785
853
  };
@@ -789,19 +857,30 @@ ${nonDocumentStrings}`;
789
857
  return rest;
790
858
  });
791
859
  };
792
- const [coagentStates, setCoagentStates] = (0, import_react8.useState)({});
860
+ const [coagentStates, setCoagentStates] = (0, import_react10.useState)({});
861
+ const coagentStatesRef = (0, import_react10.useRef)({});
862
+ const setCoagentStatesWithRef = (0, import_react10.useCallback)(
863
+ (value) => {
864
+ const newValue = typeof value === "function" ? value(coagentStatesRef.current) : value;
865
+ coagentStatesRef.current = newValue;
866
+ setCoagentStates((prev) => {
867
+ return newValue;
868
+ });
869
+ },
870
+ []
871
+ );
793
872
  let initialAgentSession = null;
794
873
  if (props.agent) {
795
874
  initialAgentSession = {
796
875
  agentName: props.agent
797
876
  };
798
877
  }
799
- const [agentSession, setAgentSession] = (0, import_react8.useState)(initialAgentSession);
800
- const [threadId, setThreadId] = (0, import_react8.useState)(null);
801
- const [runId, setRunId] = (0, import_react8.useState)(null);
802
- const chatAbortControllerRef = (0, import_react8.useRef)(null);
878
+ const [agentSession, setAgentSession] = (0, import_react10.useState)(initialAgentSession);
879
+ const [threadId, setThreadId] = (0, import_react10.useState)(null);
880
+ const [runId, setRunId] = (0, import_react10.useState)(null);
881
+ const chatAbortControllerRef = (0, import_react10.useRef)(null);
803
882
  const showDevConsole = props.showDevConsole === void 0 ? "auto" : props.showDevConsole;
804
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
883
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
805
884
  CopilotContext.Provider,
806
885
  {
807
886
  value: {
@@ -830,6 +909,8 @@ ${nonDocumentStrings}`;
830
909
  showDevConsole,
831
910
  coagentStates,
832
911
  setCoagentStates,
912
+ coagentStatesRef,
913
+ setCoagentStatesWithRef,
833
914
  agentSession,
834
915
  setAgentSession,
835
916
  runtimeClient,
@@ -840,7 +921,7 @@ ${nonDocumentStrings}`;
840
921
  setRunId,
841
922
  chatAbortControllerRef
842
923
  },
843
- children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(CopilotMessages, { children })
924
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(CopilotMessages, { children })
844
925
  }
845
926
  );
846
927
  }