@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.
- package/CHANGELOG.md +191 -0
- package/dist/{chunk-ERXWDCY6.mjs → chunk-36MGCCPZ.mjs} +2 -2
- package/dist/{chunk-UBNRUXEK.mjs → chunk-5BSUSFHM.mjs} +2 -2
- package/dist/{chunk-G7LYGERN.mjs → chunk-6ZLSC4KB.mjs} +124 -73
- package/dist/chunk-6ZLSC4KB.mjs.map +1 -0
- package/dist/{chunk-JDEWNLNP.mjs → chunk-BSAVFYRQ.mjs} +11 -11
- package/dist/{chunk-JDEWNLNP.mjs.map → chunk-BSAVFYRQ.mjs.map} +1 -1
- package/dist/{chunk-JPMIAGI6.mjs → chunk-BVK7PLK6.mjs} +2 -2
- package/dist/{chunk-FXK6RQIN.mjs → chunk-CUAFWKTQ.mjs} +4 -4
- package/dist/{chunk-XFOTNHYA.mjs → chunk-DKZTPL66.mjs} +2 -2
- package/dist/{chunk-XFOTNHYA.mjs.map → chunk-DKZTPL66.mjs.map} +1 -1
- package/dist/{chunk-EF5BNM34.mjs → chunk-FN3UA2ZE.mjs} +3 -3
- package/dist/{chunk-EXU7GWLC.mjs → chunk-GEE5AMYL.mjs} +9 -9
- package/dist/{chunk-WOGURSAL.mjs → chunk-GIMSRCVW.mjs} +64 -23
- package/dist/chunk-GIMSRCVW.mjs.map +1 -0
- package/dist/{chunk-55QZ2SVJ.mjs → chunk-JWAXDYOW.mjs} +8 -8
- package/dist/chunk-JWAXDYOW.mjs.map +1 -0
- package/dist/{chunk-3YHYWAHK.mjs → chunk-KIXKBJUV.mjs} +2 -2
- package/dist/{chunk-ADIITPD2.mjs → chunk-KLENTCQV.mjs} +34 -8
- package/dist/{chunk-ADIITPD2.mjs.map → chunk-KLENTCQV.mjs.map} +1 -1
- package/dist/{chunk-OF4SZTLL.mjs → chunk-NGQN3JRJ.mjs} +3 -3
- package/dist/{chunk-NQVCZQ5T.mjs → chunk-NJA5ZLAZ.mjs} +27 -8
- package/dist/chunk-NJA5ZLAZ.mjs.map +1 -0
- package/dist/{chunk-SJJNFYGQ.mjs → chunk-SGF6C7I6.mjs} +4 -4
- package/dist/{chunk-CMQV4XNY.mjs → chunk-VDADWRS3.mjs} +2 -2
- package/dist/components/copilot-provider/copilot-messages.js +7 -7
- package/dist/components/copilot-provider/copilot-messages.js.map +1 -1
- package/dist/components/copilot-provider/copilot-messages.mjs +3 -3
- package/dist/components/copilot-provider/copilotkit-props.d.ts +14 -9
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +1 -1
- package/dist/components/copilot-provider/copilotkit.js +70 -29
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +9 -9
- package/dist/components/copilot-provider/index.d.ts +1 -1
- package/dist/components/copilot-provider/index.js +70 -29
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +9 -9
- package/dist/components/error-boundary/error-boundary.mjs +2 -2
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.js +70 -29
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +9 -9
- package/dist/context/copilot-context.d.ts +1 -1
- package/dist/context/copilot-context.js +1 -1
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.js +1 -1
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +4 -4
- package/dist/{copilot-context-3da805ab.d.ts → copilot-context-3ab4fdf5.d.ts} +3 -3
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.js +179 -83
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +23 -23
- package/dist/hooks/use-chat.d.ts +1 -1
- package/dist/hooks/use-chat.js +220 -169
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +4 -4
- package/dist/hooks/use-coagent-state-render.js +1 -1
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +2 -2
- package/dist/hooks/use-coagent.d.ts +1 -1
- package/dist/hooks/use-coagent.js +154 -77
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +14 -14
- package/dist/hooks/use-copilot-action.js +26 -7
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +2 -2
- package/dist/hooks/use-copilot-additional-instructions.js +1 -1
- package/dist/hooks/use-copilot-additional-instructions.js.map +1 -1
- package/dist/hooks/use-copilot-additional-instructions.mjs +2 -2
- package/dist/hooks/use-copilot-authenticated-action.js +26 -7
- package/dist/hooks/use-copilot-authenticated-action.js.map +1 -1
- package/dist/hooks/use-copilot-authenticated-action.mjs +3 -3
- package/dist/hooks/use-copilot-chat.d.ts +1 -1
- package/dist/hooks/use-copilot-chat.js +128 -77
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +13 -13
- package/dist/hooks/use-copilot-readable.js +1 -1
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- package/dist/hooks/use-copilot-runtime-client.d.ts +2 -2
- package/dist/hooks/use-copilot-runtime-client.js +7 -7
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +1 -1
- package/dist/hooks/use-langgraph-interrupt-render.js +1 -1
- package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt-render.mjs +2 -2
- package/dist/hooks/use-langgraph-interrupt.d.ts +1 -1
- package/dist/hooks/use-langgraph-interrupt.js +128 -77
- package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt.mjs +14 -14
- package/dist/hooks/use-make-copilot-document-readable.js +1 -1
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +240 -103
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -27
- package/dist/lib/copilot-task.d.ts +1 -1
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +11 -11
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +11 -11
- package/dist/setupTests.d.ts +2 -0
- package/dist/setupTests.js +26 -0
- package/dist/setupTests.js.map +1 -0
- package/dist/setupTests.mjs +24 -0
- package/dist/setupTests.mjs.map +1 -0
- package/dist/types/interrupt-action.d.ts +1 -1
- package/dist/utils/extract.d.ts +1 -1
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +9 -9
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +10 -10
- package/jest.config.js +4 -0
- package/package.json +6 -3
- package/src/components/copilot-provider/__tests__/{copilotkit-trace.test.tsx → copilotkit-error.test.tsx} +17 -17
- package/src/components/copilot-provider/copilot-messages.tsx +7 -7
- package/src/components/copilot-provider/copilotkit-props.tsx +13 -8
- package/src/components/copilot-provider/copilotkit.tsx +61 -19
- package/src/context/copilot-context.tsx +4 -4
- package/src/hooks/__tests__/use-coagent-config.test.ts +284 -0
- package/src/hooks/use-chat.ts +149 -61
- package/src/hooks/use-coagent.ts +36 -0
- package/src/hooks/use-copilot-action.ts +51 -9
- package/src/hooks/use-copilot-runtime-client.ts +11 -12
- package/src/setupTests.ts +26 -0
- package/tsconfig.json +5 -2
- package/dist/chunk-55QZ2SVJ.mjs.map +0 -1
- package/dist/chunk-G7LYGERN.mjs.map +0 -1
- package/dist/chunk-NQVCZQ5T.mjs.map +0 -1
- package/dist/chunk-WOGURSAL.mjs.map +0 -1
- /package/dist/{chunk-ERXWDCY6.mjs.map → chunk-36MGCCPZ.mjs.map} +0 -0
- /package/dist/{chunk-UBNRUXEK.mjs.map → chunk-5BSUSFHM.mjs.map} +0 -0
- /package/dist/{chunk-JPMIAGI6.mjs.map → chunk-BVK7PLK6.mjs.map} +0 -0
- /package/dist/{chunk-FXK6RQIN.mjs.map → chunk-CUAFWKTQ.mjs.map} +0 -0
- /package/dist/{chunk-EF5BNM34.mjs.map → chunk-FN3UA2ZE.mjs.map} +0 -0
- /package/dist/{chunk-EXU7GWLC.mjs.map → chunk-GEE5AMYL.mjs.map} +0 -0
- /package/dist/{chunk-3YHYWAHK.mjs.map → chunk-KIXKBJUV.mjs.map} +0 -0
- /package/dist/{chunk-OF4SZTLL.mjs.map → chunk-NGQN3JRJ.mjs.map} +0 -0
- /package/dist/{chunk-SJJNFYGQ.mjs.map → chunk-SGF6C7I6.mjs.map} +0 -0
- /package/dist/{chunk-CMQV4XNY.mjs.map → chunk-VDADWRS3.mjs.map} +0 -0
package/dist/hooks/index.js
CHANGED
|
@@ -173,7 +173,7 @@ var emptyCopilotContext = {
|
|
|
173
173
|
langGraphInterruptAction: null,
|
|
174
174
|
setLangGraphInterruptAction: () => null,
|
|
175
175
|
removeLangGraphInterruptAction: () => null,
|
|
176
|
-
|
|
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,
|
|
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 (!
|
|
255
|
+
if (!onError || !runtimeOptions.publicApiKey)
|
|
255
256
|
return;
|
|
256
257
|
try {
|
|
257
|
-
const
|
|
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
|
|
276
|
-
} catch (
|
|
277
|
-
console.error("Error in
|
|
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,
|
|
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 {
|
|
515
|
+
const { onError } = useCopilotContext();
|
|
515
516
|
const traceUIError = (error, originalError) => __async(this, null, function* () {
|
|
516
|
-
if (!
|
|
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
|
|
538
|
+
yield onError(traceEvent);
|
|
538
539
|
} catch (traceError) {
|
|
539
|
-
console.error("Error in use-chat
|
|
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) =>
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
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
|
|
838
|
-
const
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
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
|
-
|
|
867
|
-
|
|
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
|
-
(
|
|
1005
|
+
(reloadMessageId) => __async(this, null, function* () {
|
|
980
1006
|
if (isLoading || messages.length === 0) {
|
|
981
1007
|
return;
|
|
982
1008
|
}
|
|
983
|
-
const
|
|
984
|
-
if (
|
|
985
|
-
console.warn(`Message with id ${
|
|
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
|
-
|
|
989
|
-
if (
|
|
990
|
-
|
|
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
|
-
|
|
993
|
-
|
|
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
|
-
|
|
1038
|
-
|
|
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
|
|
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 = {
|
|
1352
|
-
|
|
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"
|
|
1357
|
-
|
|
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
|
|
1364
|
-
|
|
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);
|