@copilotkit/react-core 1.9.2-next.9 → 1.9.3-next.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 (147) hide show
  1. package/CHANGELOG.md +191 -0
  2. package/dist/{chunk-ERXWDCY6.mjs → chunk-36MGCCPZ.mjs} +2 -2
  3. package/dist/{chunk-UBNRUXEK.mjs → chunk-5BSUSFHM.mjs} +2 -2
  4. package/dist/{chunk-G7LYGERN.mjs → chunk-6ZLSC4KB.mjs} +124 -73
  5. package/dist/chunk-6ZLSC4KB.mjs.map +1 -0
  6. package/dist/{chunk-JDEWNLNP.mjs → chunk-BSAVFYRQ.mjs} +11 -11
  7. package/dist/{chunk-JDEWNLNP.mjs.map → chunk-BSAVFYRQ.mjs.map} +1 -1
  8. package/dist/{chunk-JPMIAGI6.mjs → chunk-BVK7PLK6.mjs} +2 -2
  9. package/dist/{chunk-FXK6RQIN.mjs → chunk-CUAFWKTQ.mjs} +4 -4
  10. package/dist/{chunk-XFOTNHYA.mjs → chunk-DKZTPL66.mjs} +2 -2
  11. package/dist/{chunk-XFOTNHYA.mjs.map → chunk-DKZTPL66.mjs.map} +1 -1
  12. package/dist/{chunk-EF5BNM34.mjs → chunk-FN3UA2ZE.mjs} +3 -3
  13. package/dist/{chunk-EXU7GWLC.mjs → chunk-GEE5AMYL.mjs} +9 -9
  14. package/dist/{chunk-WOGURSAL.mjs → chunk-GIMSRCVW.mjs} +64 -23
  15. package/dist/chunk-GIMSRCVW.mjs.map +1 -0
  16. package/dist/{chunk-55QZ2SVJ.mjs → chunk-JWAXDYOW.mjs} +8 -8
  17. package/dist/chunk-JWAXDYOW.mjs.map +1 -0
  18. package/dist/{chunk-3YHYWAHK.mjs → chunk-KIXKBJUV.mjs} +2 -2
  19. package/dist/{chunk-ADIITPD2.mjs → chunk-KLENTCQV.mjs} +34 -8
  20. package/dist/{chunk-ADIITPD2.mjs.map → chunk-KLENTCQV.mjs.map} +1 -1
  21. package/dist/{chunk-OF4SZTLL.mjs → chunk-NGQN3JRJ.mjs} +3 -3
  22. package/dist/{chunk-NQVCZQ5T.mjs → chunk-NJA5ZLAZ.mjs} +27 -8
  23. package/dist/chunk-NJA5ZLAZ.mjs.map +1 -0
  24. package/dist/{chunk-SJJNFYGQ.mjs → chunk-SGF6C7I6.mjs} +4 -4
  25. package/dist/{chunk-CMQV4XNY.mjs → chunk-VDADWRS3.mjs} +2 -2
  26. package/dist/components/copilot-provider/copilot-messages.js +7 -7
  27. package/dist/components/copilot-provider/copilot-messages.js.map +1 -1
  28. package/dist/components/copilot-provider/copilot-messages.mjs +3 -3
  29. package/dist/components/copilot-provider/copilotkit-props.d.ts +14 -9
  30. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
  31. package/dist/components/copilot-provider/copilotkit.d.ts +1 -1
  32. package/dist/components/copilot-provider/copilotkit.js +70 -29
  33. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  34. package/dist/components/copilot-provider/copilotkit.mjs +9 -9
  35. package/dist/components/copilot-provider/index.d.ts +1 -1
  36. package/dist/components/copilot-provider/index.js +70 -29
  37. package/dist/components/copilot-provider/index.js.map +1 -1
  38. package/dist/components/copilot-provider/index.mjs +9 -9
  39. package/dist/components/error-boundary/error-boundary.mjs +2 -2
  40. package/dist/components/index.d.ts +1 -1
  41. package/dist/components/index.js +70 -29
  42. package/dist/components/index.js.map +1 -1
  43. package/dist/components/index.mjs +9 -9
  44. package/dist/context/copilot-context.d.ts +1 -1
  45. package/dist/context/copilot-context.js +1 -1
  46. package/dist/context/copilot-context.js.map +1 -1
  47. package/dist/context/copilot-context.mjs +1 -1
  48. package/dist/context/index.d.ts +1 -1
  49. package/dist/context/index.js +1 -1
  50. package/dist/context/index.js.map +1 -1
  51. package/dist/context/index.mjs +4 -4
  52. package/dist/{copilot-context-3da805ab.d.ts → copilot-context-3ab4fdf5.d.ts} +3 -3
  53. package/dist/hooks/index.d.ts +1 -1
  54. package/dist/hooks/index.js +179 -83
  55. package/dist/hooks/index.js.map +1 -1
  56. package/dist/hooks/index.mjs +23 -23
  57. package/dist/hooks/use-chat.d.ts +1 -1
  58. package/dist/hooks/use-chat.js +220 -169
  59. package/dist/hooks/use-chat.js.map +1 -1
  60. package/dist/hooks/use-chat.mjs +4 -4
  61. package/dist/hooks/use-coagent-state-render.js +1 -1
  62. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  63. package/dist/hooks/use-coagent-state-render.mjs +2 -2
  64. package/dist/hooks/use-coagent.d.ts +1 -1
  65. package/dist/hooks/use-coagent.js +154 -77
  66. package/dist/hooks/use-coagent.js.map +1 -1
  67. package/dist/hooks/use-coagent.mjs +14 -14
  68. package/dist/hooks/use-copilot-action.js +26 -7
  69. package/dist/hooks/use-copilot-action.js.map +1 -1
  70. package/dist/hooks/use-copilot-action.mjs +2 -2
  71. package/dist/hooks/use-copilot-additional-instructions.js +1 -1
  72. package/dist/hooks/use-copilot-additional-instructions.js.map +1 -1
  73. package/dist/hooks/use-copilot-additional-instructions.mjs +2 -2
  74. package/dist/hooks/use-copilot-authenticated-action.js +26 -7
  75. package/dist/hooks/use-copilot-authenticated-action.js.map +1 -1
  76. package/dist/hooks/use-copilot-authenticated-action.mjs +3 -3
  77. package/dist/hooks/use-copilot-chat.d.ts +1 -1
  78. package/dist/hooks/use-copilot-chat.js +128 -77
  79. package/dist/hooks/use-copilot-chat.js.map +1 -1
  80. package/dist/hooks/use-copilot-chat.mjs +13 -13
  81. package/dist/hooks/use-copilot-readable.js +1 -1
  82. package/dist/hooks/use-copilot-readable.js.map +1 -1
  83. package/dist/hooks/use-copilot-readable.mjs +2 -2
  84. package/dist/hooks/use-copilot-runtime-client.d.ts +2 -2
  85. package/dist/hooks/use-copilot-runtime-client.js +7 -7
  86. package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
  87. package/dist/hooks/use-copilot-runtime-client.mjs +1 -1
  88. package/dist/hooks/use-langgraph-interrupt-render.js +1 -1
  89. package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
  90. package/dist/hooks/use-langgraph-interrupt-render.mjs +2 -2
  91. package/dist/hooks/use-langgraph-interrupt.d.ts +1 -1
  92. package/dist/hooks/use-langgraph-interrupt.js +128 -77
  93. package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
  94. package/dist/hooks/use-langgraph-interrupt.mjs +14 -14
  95. package/dist/hooks/use-make-copilot-document-readable.js +1 -1
  96. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  97. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  98. package/dist/index.d.ts +1 -1
  99. package/dist/index.js +240 -103
  100. package/dist/index.js.map +1 -1
  101. package/dist/index.mjs +27 -27
  102. package/dist/lib/copilot-task.d.ts +1 -1
  103. package/dist/lib/copilot-task.js.map +1 -1
  104. package/dist/lib/copilot-task.mjs +11 -11
  105. package/dist/lib/index.d.ts +1 -1
  106. package/dist/lib/index.js.map +1 -1
  107. package/dist/lib/index.mjs +11 -11
  108. package/dist/setupTests.d.ts +2 -0
  109. package/dist/setupTests.js +26 -0
  110. package/dist/setupTests.js.map +1 -0
  111. package/dist/setupTests.mjs +24 -0
  112. package/dist/setupTests.mjs.map +1 -0
  113. package/dist/types/interrupt-action.d.ts +1 -1
  114. package/dist/utils/extract.d.ts +1 -1
  115. package/dist/utils/extract.js.map +1 -1
  116. package/dist/utils/extract.mjs +9 -9
  117. package/dist/utils/index.d.ts +1 -1
  118. package/dist/utils/index.js.map +1 -1
  119. package/dist/utils/index.mjs +10 -10
  120. package/jest.config.js +4 -0
  121. package/package.json +6 -3
  122. package/src/components/copilot-provider/__tests__/{copilotkit-trace.test.tsx → copilotkit-error.test.tsx} +17 -17
  123. package/src/components/copilot-provider/copilot-messages.tsx +7 -7
  124. package/src/components/copilot-provider/copilotkit-props.tsx +13 -8
  125. package/src/components/copilot-provider/copilotkit.tsx +61 -19
  126. package/src/context/copilot-context.tsx +4 -4
  127. package/src/hooks/__tests__/use-coagent-config.test.ts +284 -0
  128. package/src/hooks/use-chat.ts +149 -61
  129. package/src/hooks/use-coagent.ts +36 -0
  130. package/src/hooks/use-copilot-action.ts +51 -9
  131. package/src/hooks/use-copilot-runtime-client.ts +11 -12
  132. package/src/setupTests.ts +26 -0
  133. package/tsconfig.json +5 -2
  134. package/dist/chunk-55QZ2SVJ.mjs.map +0 -1
  135. package/dist/chunk-G7LYGERN.mjs.map +0 -1
  136. package/dist/chunk-NQVCZQ5T.mjs.map +0 -1
  137. package/dist/chunk-WOGURSAL.mjs.map +0 -1
  138. /package/dist/{chunk-ERXWDCY6.mjs.map → chunk-36MGCCPZ.mjs.map} +0 -0
  139. /package/dist/{chunk-UBNRUXEK.mjs.map → chunk-5BSUSFHM.mjs.map} +0 -0
  140. /package/dist/{chunk-JPMIAGI6.mjs.map → chunk-BVK7PLK6.mjs.map} +0 -0
  141. /package/dist/{chunk-FXK6RQIN.mjs.map → chunk-CUAFWKTQ.mjs.map} +0 -0
  142. /package/dist/{chunk-EF5BNM34.mjs.map → chunk-FN3UA2ZE.mjs.map} +0 -0
  143. /package/dist/{chunk-EXU7GWLC.mjs.map → chunk-GEE5AMYL.mjs.map} +0 -0
  144. /package/dist/{chunk-3YHYWAHK.mjs.map → chunk-KIXKBJUV.mjs.map} +0 -0
  145. /package/dist/{chunk-OF4SZTLL.mjs.map → chunk-NGQN3JRJ.mjs.map} +0 -0
  146. /package/dist/{chunk-SJJNFYGQ.mjs.map → chunk-SGF6C7I6.mjs.map} +0 -0
  147. /package/dist/{chunk-CMQV4XNY.mjs.map → chunk-VDADWRS3.mjs.map} +0 -0
@@ -173,7 +173,7 @@ var emptyCopilotContext = {
173
173
  langGraphInterruptAction: null,
174
174
  setLangGraphInterruptAction: () => null,
175
175
  removeLangGraphInterruptAction: () => null,
176
- onTrace: void 0
176
+ onError: void 0
177
177
  };
178
178
  var CopilotContext = import_react.default.createContext(emptyCopilotContext);
179
179
  function useCopilotContext() {
@@ -192,6 +192,7 @@ var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
192
192
 
193
193
  // src/hooks/use-chat.ts
194
194
  var import_react5 = require("react");
195
+ var import_react_dom = require("react-dom");
195
196
  var import_shared4 = require("@copilotkit/shared");
196
197
  var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
197
198
 
@@ -248,13 +249,13 @@ function shouldShowDevConsole(showDevConsole) {
248
249
  // src/hooks/use-copilot-runtime-client.ts
249
250
  var useCopilotRuntimeClient = (options) => {
250
251
  const { setBannerError } = useToast();
251
- const _a = options, { showDevConsole, onTrace } = _a, runtimeOptions = __objRest(_a, ["showDevConsole", "onTrace"]);
252
+ const _a = options, { showDevConsole, onError } = _a, runtimeOptions = __objRest(_a, ["showDevConsole", "onError"]);
252
253
  const lastStructuredErrorRef = (0, import_react3.useRef)(null);
253
254
  const traceUIError = (error, originalError) => __async(void 0, null, function* () {
254
- if (!onTrace || !runtimeOptions.publicApiKey)
255
+ if (!onError || !runtimeOptions.publicApiKey)
255
256
  return;
256
257
  try {
257
- const traceEvent = {
258
+ const errorEvent = {
258
259
  type: "error",
259
260
  timestamp: Date.now(),
260
261
  context: {
@@ -272,9 +273,9 @@ var useCopilotRuntimeClient = (options) => {
272
273
  },
273
274
  error
274
275
  };
275
- yield onTrace(traceEvent);
276
- } catch (traceError) {
277
- console.error("Error in onTrace handler:", traceError);
276
+ yield onError(errorEvent);
277
+ } catch (error2) {
278
+ console.error("Error in onError handler:", error2);
278
279
  }
279
280
  });
280
281
  const runtimeClient = (0, import_react3.useMemo)(() => {
@@ -338,7 +339,7 @@ var useCopilotRuntimeClient = (options) => {
338
339
  setBannerError(warningError);
339
340
  }
340
341
  }));
341
- }, [runtimeOptions, setBannerError, showDevConsole, onTrace]);
342
+ }, [runtimeOptions, setBannerError, showDevConsole, onError]);
342
343
  return runtimeClient;
343
344
  };
344
345
  function createStructuredError(gqlError) {
@@ -511,9 +512,9 @@ function useChat(options) {
511
512
  const runChatCompletionRef = (0, import_react5.useRef)();
512
513
  const addErrorToast = useErrorToast();
513
514
  const { setBannerError } = useToast();
514
- const { onTrace } = useCopilotContext();
515
+ const { onError } = useCopilotContext();
515
516
  const traceUIError = (error, originalError) => __async(this, null, function* () {
516
- if (!onTrace || !(copilotConfig == null ? void 0 : copilotConfig.publicApiKey))
517
+ if (!onError || !(copilotConfig == null ? void 0 : copilotConfig.publicApiKey))
517
518
  return;
518
519
  try {
519
520
  const traceEvent = {
@@ -534,9 +535,9 @@ function useChat(options) {
534
535
  },
535
536
  error
536
537
  };
537
- yield onTrace(traceEvent);
538
+ yield onError(traceEvent);
538
539
  } catch (traceError) {
539
- console.error("Error in use-chat onTrace handler:", traceError);
540
+ console.error("Error in use-chat onError handler:", traceError);
540
541
  }
541
542
  });
542
543
  const agentSessionRef = (0, import_react5.useRef)(agentSession);
@@ -758,17 +759,22 @@ function useChat(options) {
758
759
  lastAgentStateMessage.state.messages
759
760
  );
760
761
  }
761
- setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
762
- [lastAgentStateMessage.agentName]: {
763
- name: lastAgentStateMessage.agentName,
764
- state: lastAgentStateMessage.state,
765
- running: lastAgentStateMessage.running,
766
- active: lastAgentStateMessage.active,
767
- threadId: lastAgentStateMessage.threadId,
768
- nodeName: lastAgentStateMessage.nodeName,
769
- runId: lastAgentStateMessage.runId
770
- }
771
- }));
762
+ setCoagentStatesWithRef((prevAgentStates) => {
763
+ var _a2;
764
+ return __spreadProps(__spreadValues({}, prevAgentStates), {
765
+ [lastAgentStateMessage.agentName]: {
766
+ name: lastAgentStateMessage.agentName,
767
+ state: lastAgentStateMessage.state,
768
+ running: lastAgentStateMessage.running,
769
+ active: lastAgentStateMessage.active,
770
+ threadId: lastAgentStateMessage.threadId,
771
+ nodeName: lastAgentStateMessage.nodeName,
772
+ runId: lastAgentStateMessage.runId,
773
+ // Preserve existing config from previous state
774
+ config: (_a2 = prevAgentStates[lastAgentStateMessage.agentName]) == null ? void 0 : _a2.config
775
+ }
776
+ });
777
+ });
772
778
  if (lastAgentStateMessage.running) {
773
779
  setAgentSession({
774
780
  threadId: lastAgentStateMessage.threadId,
@@ -798,6 +804,39 @@ function useChat(options) {
798
804
  newMessages
799
805
  );
800
806
  let didExecuteAction = false;
807
+ const executeActionFromMessage = (currentAction, actionMessage) => __async(this, null, function* () {
808
+ var _a2;
809
+ const isInterruptAction = interruptMessages.find((m) => m.id === actionMessage.id);
810
+ followUp = (_a2 = currentAction == null ? void 0 : currentAction.followUp) != null ? _a2 : !isInterruptAction;
811
+ if (currentAction == null ? void 0 : currentAction._setActivatingMessageId) {
812
+ currentAction._setActivatingMessageId(actionMessage.id);
813
+ }
814
+ const resultMessage = yield executeAction({
815
+ onFunctionCall,
816
+ message: actionMessage,
817
+ chatAbortControllerRef,
818
+ onError: (error) => {
819
+ addErrorToast([error]);
820
+ console.error(`Failed to execute action ${actionMessage.name}: ${error}`);
821
+ },
822
+ setMessages,
823
+ getFinalMessages: () => finalMessages,
824
+ isRenderAndWait: (currentAction == null ? void 0 : currentAction._isRenderAndWait) || false
825
+ });
826
+ didExecuteAction = true;
827
+ const messageIndex = finalMessages.findIndex((msg) => msg.id === actionMessage.id);
828
+ finalMessages.splice(messageIndex + 1, 0, resultMessage);
829
+ if (currentAction == null ? void 0 : currentAction._isRenderAndWait) {
830
+ const messagesForImmediateUpdate = [...finalMessages];
831
+ (0, import_react_dom.flushSync)(() => {
832
+ setMessages(messagesForImmediateUpdate);
833
+ });
834
+ }
835
+ if (currentAction == null ? void 0 : currentAction._setActivatingMessageId) {
836
+ currentAction._setActivatingMessageId(null);
837
+ }
838
+ return resultMessage;
839
+ });
801
840
  if (onFunctionCall) {
802
841
  const lastMessages = [];
803
842
  for (let i = finalMessages.length - 1; i >= 0; i--) {
@@ -814,37 +853,28 @@ function useChat(options) {
814
853
  (action2) => action2.name === message.name
815
854
  );
816
855
  const currentResultMessagePairedFeAction = message.isResultMessage() ? getPairedFeAction(actions, message) : null;
817
- const executeActionFromMessage = (action2, message2) => __async(this, null, function* () {
818
- var _a2;
819
- const isInterruptAction = interruptMessages.find((m) => m.id === message2.id);
820
- followUp = (_a2 = action2 == null ? void 0 : action2.followUp) != null ? _a2 : !isInterruptAction;
821
- const resultMessage = yield executeAction({
822
- onFunctionCall,
823
- previousMessages,
824
- message: message2,
825
- chatAbortControllerRef,
826
- onError: (error) => {
827
- addErrorToast([error]);
828
- console.error(`Failed to execute action ${message2.name}: ${error}`);
829
- }
830
- });
831
- didExecuteAction = true;
832
- const messageIndex = finalMessages.findIndex((msg) => msg.id === message2.id);
833
- finalMessages.splice(messageIndex + 1, 0, resultMessage);
834
- return resultMessage;
835
- });
836
856
  if (action && message.isActionExecutionMessage()) {
837
- const resultMessage = yield executeActionFromMessage(action, message);
838
- const pairedFeAction = getPairedFeAction(actions, resultMessage);
839
- if (pairedFeAction) {
840
- const newExecutionMessage = new import_runtime_client_gql3.ActionExecutionMessage({
841
- name: pairedFeAction.name,
842
- arguments: (0, import_shared4.parseJson)(resultMessage.result, resultMessage.result),
843
- status: message.status,
844
- createdAt: message.createdAt,
845
- parentMessageId: message.parentMessageId
846
- });
847
- yield executeActionFromMessage(pairedFeAction, newExecutionMessage);
857
+ const isRenderAndWaitAction = (action == null ? void 0 : action._isRenderAndWait) || false;
858
+ const alreadyProcessed = isRenderAndWaitAction && finalMessages.some(
859
+ (fm) => fm.isResultMessage() && fm.actionExecutionId === message.id
860
+ );
861
+ if (alreadyProcessed) {
862
+ } else {
863
+ const resultMessage = yield executeActionFromMessage(
864
+ action,
865
+ message
866
+ );
867
+ const pairedFeAction = getPairedFeAction(actions, resultMessage);
868
+ if (pairedFeAction) {
869
+ const newExecutionMessage = new import_runtime_client_gql3.ActionExecutionMessage({
870
+ name: pairedFeAction.name,
871
+ arguments: (0, import_shared4.parseJson)(resultMessage.result, resultMessage.result),
872
+ status: message.status,
873
+ createdAt: message.createdAt,
874
+ parentMessageId: message.parentMessageId
875
+ });
876
+ yield executeActionFromMessage(pairedFeAction, newExecutionMessage);
877
+ }
848
878
  }
849
879
  } else if (message.isResultMessage() && currentResultMessagePairedFeAction) {
850
880
  const newExecutionMessage = new import_runtime_client_gql3.ActionExecutionMessage({
@@ -862,13 +892,9 @@ function useChat(options) {
862
892
  }
863
893
  setMessages(finalMessages);
864
894
  }
865
- if (
866
- // if followUp is not explicitly false
867
- followUp !== false && // and we executed an action
868
- (didExecuteAction || // the last message is a server side result
869
- !isAgentRun && finalMessages.length && finalMessages[finalMessages.length - 1].isResultMessage()) && // the user did not stop generation
870
- !((_r = chatAbortControllerRef.current) == null ? void 0 : _r.signal.aborted)
871
- ) {
895
+ if (followUp !== false && (didExecuteAction || // the last message is a server side result
896
+ !isAgentRun && finalMessages.length && finalMessages[finalMessages.length - 1].isResultMessage()) && // the user did not stop generation
897
+ !((_r = chatAbortControllerRef.current) == null ? void 0 : _r.signal.aborted)) {
872
898
  yield new Promise((resolve) => setTimeout(resolve, 10));
873
899
  return yield runChatCompletionRef.current(finalMessages);
874
900
  } else if ((_s = chatAbortControllerRef.current) == null ? void 0 : _s.signal.aborted) {
@@ -976,21 +1002,35 @@ function useChat(options) {
976
1002
  [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
977
1003
  );
978
1004
  const reload = useAsyncCallback(
979
- (messageId) => __async(this, null, function* () {
1005
+ (reloadMessageId) => __async(this, null, function* () {
980
1006
  if (isLoading || messages.length === 0) {
981
1007
  return;
982
1008
  }
983
- const index = messages.findIndex((msg) => msg.id === messageId);
984
- if (index === -1) {
985
- console.warn(`Message with id ${messageId} not found`);
1009
+ const reloadMessageIndex = messages.findIndex((msg) => msg.id === reloadMessageId);
1010
+ if (reloadMessageIndex === -1) {
1011
+ console.warn(`Message with id ${reloadMessageId} not found`);
986
1012
  return;
987
1013
  }
988
- let newMessages = messages.slice(0, index);
989
- if (newMessages.length > 0 && newMessages[newMessages.length - 1].isAgentStateMessage()) {
990
- newMessages = newMessages.slice(0, newMessages.length - 1);
1014
+ const reloadMessageRole = messages[reloadMessageIndex].role;
1015
+ if (reloadMessageRole !== import_runtime_client_gql3.MessageRole.Assistant) {
1016
+ console.warn(`Regenerate cannot be performed on ${reloadMessageRole} role`);
1017
+ return;
991
1018
  }
992
- setMessages(newMessages);
993
- return runChatCompletionAndHandleFunctionCall(newMessages);
1019
+ let historyCutoff = [];
1020
+ if (messages.length > 2) {
1021
+ const lastUserMessageBeforeRegenerate = messages.slice(0, reloadMessageIndex).reverse().find(
1022
+ (msg) => (
1023
+ // @ts-expect-error -- message has role
1024
+ msg.role === import_runtime_client_gql3.MessageRole.User
1025
+ )
1026
+ );
1027
+ const indexOfLastUserMessageBeforeRegenerate = messages.findIndex(
1028
+ (msg) => msg.id === lastUserMessageBeforeRegenerate.id
1029
+ );
1030
+ historyCutoff = messages.slice(0, indexOfLastUserMessageBeforeRegenerate + 1);
1031
+ }
1032
+ setMessages(historyCutoff);
1033
+ return runChatCompletionAndHandleFunctionCall(historyCutoff);
994
1034
  }),
995
1035
  [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
996
1036
  );
@@ -1025,20 +1065,31 @@ function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
1025
1065
  function executeAction(_0) {
1026
1066
  return __async(this, arguments, function* ({
1027
1067
  onFunctionCall,
1028
- previousMessages,
1029
1068
  message,
1030
1069
  chatAbortControllerRef,
1031
- onError
1070
+ onError,
1071
+ setMessages,
1072
+ getFinalMessages,
1073
+ isRenderAndWait
1032
1074
  }) {
1033
1075
  let result;
1034
1076
  let error = null;
1077
+ const currentMessagesForHandler = getFinalMessages();
1078
+ const handlerReturnedPromise = onFunctionCall({
1079
+ messages: currentMessagesForHandler,
1080
+ name: message.name,
1081
+ args: message.arguments
1082
+ });
1083
+ if (isRenderAndWait) {
1084
+ const currentMessagesForRender = getFinalMessages();
1085
+ (0, import_react_dom.flushSync)(() => {
1086
+ setMessages([...currentMessagesForRender]);
1087
+ });
1088
+ }
1035
1089
  try {
1036
1090
  result = yield Promise.race([
1037
- onFunctionCall({
1038
- messages: previousMessages,
1039
- name: message.name,
1040
- args: message.arguments
1041
- }),
1091
+ handlerReturnedPromise,
1092
+ // Await the promise returned by the handler
1042
1093
  new Promise(
1043
1094
  (resolve) => {
1044
1095
  var _a;
@@ -1086,7 +1137,7 @@ function getPairedFeAction(actions, message) {
1086
1137
 
1087
1138
  // src/components/copilot-provider/copilotkit.tsx
1088
1139
  var import_react7 = require("react");
1089
- var import_react_dom = require("react-dom");
1140
+ var import_react_dom2 = require("react-dom");
1090
1141
  var import_shared5 = require("@copilotkit/shared");
1091
1142
 
1092
1143
  // src/context/copilot-messages-context.tsx
@@ -1331,6 +1382,7 @@ function useCopilotAction(action, dependencies) {
1331
1382
  const { setAction, removeAction, actions, chatComponentsCache } = useCopilotContext();
1332
1383
  const idRef = (0, import_react9.useRef)((0, import_shared6.randomId)());
1333
1384
  const renderAndWaitRef = (0, import_react9.useRef)(null);
1385
+ const activatingMessageIdRef = (0, import_react9.useRef)(null);
1334
1386
  const { addToast } = useToast();
1335
1387
  action = __spreadValues({}, action);
1336
1388
  if (
@@ -1338,30 +1390,48 @@ function useCopilotAction(action, dependencies) {
1338
1390
  isFrontendAction(action) && // check if renderAndWaitForResponse is set
1339
1391
  (action.renderAndWait || action.renderAndWaitForResponse)
1340
1392
  ) {
1393
+ action._isRenderAndWait = true;
1341
1394
  const renderAndWait = action.renderAndWait || action.renderAndWaitForResponse;
1342
1395
  action.renderAndWait = void 0;
1343
1396
  action.renderAndWaitForResponse = void 0;
1397
+ action._setActivatingMessageId = (id) => {
1398
+ activatingMessageIdRef.current = id;
1399
+ };
1344
1400
  action.handler = useAsyncCallback(() => __async(this, null, function* () {
1401
+ const currentActivatingId = activatingMessageIdRef.current;
1345
1402
  let resolve;
1346
1403
  let reject;
1347
1404
  const promise = new Promise((resolvePromise, rejectPromise) => {
1348
1405
  resolve = resolvePromise;
1349
1406
  reject = rejectPromise;
1350
1407
  });
1351
- renderAndWaitRef.current = { promise, resolve, reject };
1352
- return yield promise;
1408
+ renderAndWaitRef.current = {
1409
+ promise,
1410
+ resolve,
1411
+ reject,
1412
+ messageId: currentActivatingId
1413
+ };
1414
+ const result = yield promise;
1415
+ return result;
1353
1416
  }), []);
1354
1417
  action.render = (props) => {
1418
+ const currentRenderMessageId = props.messageId;
1355
1419
  let status = props.status;
1356
- if (props.status === "executing" && !renderAndWaitRef.current) {
1357
- status = "inProgress";
1420
+ if (props.status === "executing") {
1421
+ if (!renderAndWaitRef.current || !renderAndWaitRef.current.promise) {
1422
+ status = "inProgress";
1423
+ } else if (renderAndWaitRef.current.messageId !== currentRenderMessageId && activatingMessageIdRef.current !== currentRenderMessageId) {
1424
+ status = "inProgress";
1425
+ }
1358
1426
  }
1359
1427
  const waitProps = {
1360
1428
  status,
1361
1429
  args: props.args,
1362
1430
  result: props.result,
1363
- handler: status === "executing" ? renderAndWaitRef.current.resolve : void 0,
1364
- respond: status === "executing" ? renderAndWaitRef.current.resolve : void 0
1431
+ // handler and respond should only be provided if this is the truly active instance
1432
+ // and its promise infrastructure is ready.
1433
+ handler: status === "executing" && renderAndWaitRef.current && renderAndWaitRef.current.messageId === currentRenderMessageId ? renderAndWaitRef.current.resolve : void 0,
1434
+ respond: status === "executing" && renderAndWaitRef.current && renderAndWaitRef.current.messageId === currentRenderMessageId ? renderAndWaitRef.current.resolve : void 0
1365
1435
  };
1366
1436
  const isNoArgsRenderWait = (_fn) => {
1367
1437
  var _a;
@@ -1630,6 +1700,32 @@ function useCoAgent(options) {
1630
1700
  // reset initialstate on reset
1631
1701
  coagentStates[name] === void 0
1632
1702
  ]);
1703
+ (0, import_react13.useEffect)(() => {
1704
+ const newConfig = options.config ? options.config : options.configurable ? { configurable: options.configurable } : void 0;
1705
+ if (newConfig === void 0)
1706
+ return;
1707
+ setCoagentStatesWithRef((prev) => {
1708
+ var _a;
1709
+ const existing = (_a = prev[name]) != null ? _a : {
1710
+ name,
1711
+ state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
1712
+ config: {},
1713
+ running: false,
1714
+ active: false,
1715
+ threadId: void 0,
1716
+ nodeName: void 0,
1717
+ runId: void 0
1718
+ };
1719
+ if (JSON.stringify(existing.config) === JSON.stringify(newConfig)) {
1720
+ return prev;
1721
+ }
1722
+ return __spreadProps(__spreadValues({}, prev), {
1723
+ [name]: __spreadProps(__spreadValues({}, existing), {
1724
+ config: newConfig
1725
+ })
1726
+ });
1727
+ });
1728
+ }, [JSON.stringify(options.config), JSON.stringify(options.configurable)]);
1633
1729
  const runAgentCallback = useAsyncCallback(
1634
1730
  (hint) => __async(this, null, function* () {
1635
1731
  yield runAgent(name, context, appendMessage, runChatCompletion, hint);