@copilotkit/react-core 1.9.2-next.8 → 1.9.2
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 +193 -0
- package/dist/{chunk-ERXWDCY6.mjs → chunk-36MGCCPZ.mjs} +2 -2
- package/dist/{chunk-CCESTGAM.mjs → chunk-3OQM3NEK.mjs} +2 -2
- package/dist/{chunk-7G6RR4HE.mjs → chunk-3Q4F7RF2.mjs} +2 -2
- package/dist/chunk-57K2ZJ5F.mjs +348 -0
- package/dist/chunk-57K2ZJ5F.mjs.map +1 -0
- package/dist/{chunk-UBNRUXEK.mjs → chunk-5BSUSFHM.mjs} +2 -2
- package/dist/{chunk-RN3ZRHI7.mjs → chunk-AD7DWJNW.mjs} +66 -25
- package/dist/chunk-AD7DWJNW.mjs.map +1 -0
- package/dist/{chunk-JPMIAGI6.mjs → chunk-BVK7PLK6.mjs} +2 -2
- package/dist/{chunk-VJCHRQ7Q.mjs → chunk-DGON3GZX.mjs} +39 -6
- package/dist/chunk-DGON3GZX.mjs.map +1 -0
- 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-S4BOATBG.mjs → chunk-FN3UA2ZE.mjs} +3 -3
- package/dist/{chunk-ISYBUDL4.mjs → chunk-JWAXDYOW.mjs} +11 -12
- package/dist/chunk-JWAXDYOW.mjs.map +1 -0
- package/dist/{chunk-T4ZKC4X4.mjs → chunk-KIXKBJUV.mjs} +3 -3
- package/dist/{chunk-I4JPQECN.mjs → chunk-LFAZTKBK.mjs} +5 -5
- package/dist/{chunk-JHIZ5HAI.mjs → chunk-NJA5ZLAZ.mjs} +29 -10
- package/dist/chunk-NJA5ZLAZ.mjs.map +1 -0
- package/dist/{chunk-ZHEEHGLS.mjs → chunk-QGT4JO7R.mjs} +35 -6
- package/dist/chunk-QGT4JO7R.mjs.map +1 -0
- package/dist/{chunk-JXF732XG.mjs → chunk-S5QUEHJC.mjs} +195 -77
- package/dist/chunk-S5QUEHJC.mjs.map +1 -0
- package/dist/{chunk-QQZLIEXK.mjs → chunk-SJJNFYGQ.mjs} +3 -3
- package/dist/{chunk-CMQV4XNY.mjs → chunk-VDADWRS3.mjs} +2 -2
- package/dist/chunk-YAF2LATQ.mjs +310 -0
- package/dist/chunk-YAF2LATQ.mjs.map +1 -0
- package/dist/{chunk-VF6UPRKM.mjs → chunk-ZGMZ5WJI.mjs} +4 -4
- package/dist/components/copilot-provider/copilot-messages.js +37 -4
- 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 +354 -258
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +10 -10
- package/dist/components/copilot-provider/index.d.ts +1 -1
- package/dist/components/copilot-provider/index.js +354 -258
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +10 -10
- package/dist/components/error-boundary/error-boundary.js +135 -146
- package/dist/components/error-boundary/error-boundary.js.map +1 -1
- package/dist/components/error-boundary/error-boundary.mjs +4 -4
- package/dist/components/error-boundary/error-utils.js.map +1 -1
- package/dist/components/error-boundary/error-utils.mjs +2 -2
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.js +354 -258
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +10 -10
- package/dist/components/toast/toast-provider.js +118 -85
- package/dist/components/toast/toast-provider.js.map +1 -1
- package/dist/components/toast/toast-provider.mjs +1 -1
- package/dist/components/usage-banner.js +135 -146
- package/dist/components/usage-banner.js.map +1 -1
- package/dist/components/usage-banner.mjs +1 -1
- 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 +1 -1
- 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 +249 -88
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +32 -32
- package/dist/hooks/use-chat.d.ts +1 -1
- package/dist/hooks/use-chat.js +287 -174
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +5 -5
- 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 +3 -3
- package/dist/hooks/use-coagent.d.ts +1 -1
- package/dist/hooks/use-coagent.js +224 -82
- 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 +4 -4
- 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 +5 -5
- package/dist/hooks/use-copilot-chat.d.ts +1 -1
- package/dist/hooks/use-copilot-chat.js +195 -82
- 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 +8 -8
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
- 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 +195 -82
- 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 +593 -336
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +33 -33
- 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 +10 -10
- 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 +43 -4
- package/src/components/copilot-provider/copilotkit-props.tsx +13 -8
- package/src/components/copilot-provider/copilotkit.tsx +61 -19
- package/src/components/toast/toast-provider.tsx +49 -24
- package/src/components/usage-banner.tsx +144 -147
- 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 +249 -61
- package/src/hooks/use-coagent.ts +41 -0
- package/src/hooks/use-copilot-action.ts +51 -9
- package/src/hooks/use-copilot-runtime-client.ts +12 -50
- package/src/setupTests.ts +26 -0
- package/tsconfig.json +5 -2
- package/dist/chunk-HD2GE3DK.mjs +0 -359
- package/dist/chunk-HD2GE3DK.mjs.map +0 -1
- package/dist/chunk-ISYBUDL4.mjs.map +0 -1
- package/dist/chunk-JHIZ5HAI.mjs.map +0 -1
- package/dist/chunk-JXF732XG.mjs.map +0 -1
- package/dist/chunk-RN3ZRHI7.mjs.map +0 -1
- package/dist/chunk-VJCHRQ7Q.mjs.map +0 -1
- package/dist/chunk-VRXANACV.mjs +0 -277
- package/dist/chunk-VRXANACV.mjs.map +0 -1
- package/dist/chunk-ZHEEHGLS.mjs.map +0 -1
- /package/dist/{chunk-ERXWDCY6.mjs.map → chunk-36MGCCPZ.mjs.map} +0 -0
- /package/dist/{chunk-CCESTGAM.mjs.map → chunk-3OQM3NEK.mjs.map} +0 -0
- /package/dist/{chunk-7G6RR4HE.mjs.map → chunk-3Q4F7RF2.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-S4BOATBG.mjs.map → chunk-FN3UA2ZE.mjs.map} +0 -0
- /package/dist/{chunk-T4ZKC4X4.mjs.map → chunk-KIXKBJUV.mjs.map} +0 -0
- /package/dist/{chunk-I4JPQECN.mjs.map → chunk-LFAZTKBK.mjs.map} +0 -0
- /package/dist/{chunk-QQZLIEXK.mjs.map → chunk-SJJNFYGQ.mjs.map} +0 -0
- /package/dist/{chunk-CMQV4XNY.mjs.map → chunk-VDADWRS3.mjs.map} +0 -0
- /package/dist/{chunk-VF6UPRKM.mjs.map → chunk-ZGMZ5WJI.mjs.map} +0 -0
|
@@ -159,7 +159,7 @@ var emptyCopilotContext = {
|
|
|
159
159
|
langGraphInterruptAction: null,
|
|
160
160
|
setLangGraphInterruptAction: () => null,
|
|
161
161
|
removeLangGraphInterruptAction: () => null,
|
|
162
|
-
|
|
162
|
+
onError: void 0
|
|
163
163
|
};
|
|
164
164
|
var CopilotContext = import_react.default.createContext(emptyCopilotContext);
|
|
165
165
|
function useCopilotContext() {
|
|
@@ -178,6 +178,7 @@ var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
|
178
178
|
|
|
179
179
|
// src/hooks/use-chat.ts
|
|
180
180
|
var import_react5 = require("react");
|
|
181
|
+
var import_react_dom = require("react-dom");
|
|
181
182
|
var import_shared4 = require("@copilotkit/shared");
|
|
182
183
|
var import_runtime_client_gql3 = require("@copilotkit/runtime-client-gql");
|
|
183
184
|
|
|
@@ -234,13 +235,13 @@ function shouldShowDevConsole(showDevConsole) {
|
|
|
234
235
|
// src/hooks/use-copilot-runtime-client.ts
|
|
235
236
|
var useCopilotRuntimeClient = (options) => {
|
|
236
237
|
const { setBannerError } = useToast();
|
|
237
|
-
const _a = options, { showDevConsole,
|
|
238
|
+
const _a = options, { showDevConsole, onError } = _a, runtimeOptions = __objRest(_a, ["showDevConsole", "onError"]);
|
|
238
239
|
const lastStructuredErrorRef = (0, import_react3.useRef)(null);
|
|
239
240
|
const traceUIError = (error, originalError) => __async(void 0, null, function* () {
|
|
240
|
-
if (!
|
|
241
|
+
if (!onError || !runtimeOptions.publicApiKey)
|
|
241
242
|
return;
|
|
242
243
|
try {
|
|
243
|
-
const
|
|
244
|
+
const errorEvent = {
|
|
244
245
|
type: "error",
|
|
245
246
|
timestamp: Date.now(),
|
|
246
247
|
context: {
|
|
@@ -251,16 +252,16 @@ var useCopilotRuntimeClient = (options) => {
|
|
|
251
252
|
startTime: Date.now()
|
|
252
253
|
},
|
|
253
254
|
technical: {
|
|
254
|
-
environment:
|
|
255
|
+
environment: "browser",
|
|
255
256
|
userAgent: typeof navigator !== "undefined" ? navigator.userAgent : void 0,
|
|
256
257
|
stackTrace: originalError instanceof Error ? originalError.stack : void 0
|
|
257
258
|
}
|
|
258
259
|
},
|
|
259
260
|
error
|
|
260
261
|
};
|
|
261
|
-
yield
|
|
262
|
-
} catch (
|
|
263
|
-
console.error("Error in
|
|
262
|
+
yield onError(errorEvent);
|
|
263
|
+
} catch (error2) {
|
|
264
|
+
console.error("Error in onError handler:", error2);
|
|
264
265
|
}
|
|
265
266
|
});
|
|
266
267
|
const runtimeClient = (0, import_react3.useMemo)(() => {
|
|
@@ -324,7 +325,7 @@ var useCopilotRuntimeClient = (options) => {
|
|
|
324
325
|
setBannerError(warningError);
|
|
325
326
|
}
|
|
326
327
|
}));
|
|
327
|
-
}, [runtimeOptions, setBannerError, showDevConsole,
|
|
328
|
+
}, [runtimeOptions, setBannerError, showDevConsole, onError]);
|
|
328
329
|
return runtimeClient;
|
|
329
330
|
};
|
|
330
331
|
function createStructuredError(gqlError) {
|
|
@@ -496,6 +497,35 @@ function useChat(options) {
|
|
|
496
497
|
} = options;
|
|
497
498
|
const runChatCompletionRef = (0, import_react5.useRef)();
|
|
498
499
|
const addErrorToast = useErrorToast();
|
|
500
|
+
const { setBannerError } = useToast();
|
|
501
|
+
const { onError } = useCopilotContext();
|
|
502
|
+
const traceUIError = (error, originalError) => __async(this, null, function* () {
|
|
503
|
+
if (!onError || !(copilotConfig == null ? void 0 : copilotConfig.publicApiKey))
|
|
504
|
+
return;
|
|
505
|
+
try {
|
|
506
|
+
const traceEvent = {
|
|
507
|
+
type: "error",
|
|
508
|
+
timestamp: Date.now(),
|
|
509
|
+
context: {
|
|
510
|
+
source: "ui",
|
|
511
|
+
request: {
|
|
512
|
+
operation: "useChatCompletion",
|
|
513
|
+
url: copilotConfig.chatApiEndpoint,
|
|
514
|
+
startTime: Date.now()
|
|
515
|
+
},
|
|
516
|
+
technical: {
|
|
517
|
+
environment: "browser",
|
|
518
|
+
userAgent: typeof navigator !== "undefined" ? navigator.userAgent : void 0,
|
|
519
|
+
stackTrace: originalError instanceof Error ? originalError.stack : void 0
|
|
520
|
+
}
|
|
521
|
+
},
|
|
522
|
+
error
|
|
523
|
+
};
|
|
524
|
+
yield onError(traceEvent);
|
|
525
|
+
} catch (traceError) {
|
|
526
|
+
console.error("Error in use-chat onError handler:", traceError);
|
|
527
|
+
}
|
|
528
|
+
});
|
|
499
529
|
const agentSessionRef = (0, import_react5.useRef)(agentSession);
|
|
500
530
|
agentSessionRef.current = agentSession;
|
|
501
531
|
const runIdRef = (0, import_react5.useRef)(runId);
|
|
@@ -515,7 +545,7 @@ function useChat(options) {
|
|
|
515
545
|
const pendingAppendsRef = (0, import_react5.useRef)([]);
|
|
516
546
|
const runChatCompletion = useAsyncCallback(
|
|
517
547
|
(previousMessages) => __async(this, null, function* () {
|
|
518
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
548
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
|
|
519
549
|
setIsLoading(true);
|
|
520
550
|
const interruptEvent = langGraphInterruptAction == null ? void 0 : langGraphInterruptAction.event;
|
|
521
551
|
if ((interruptEvent == null ? void 0 : interruptEvent.name) === import_runtime_client_gql3.MetaEventName.LangGraphInterruptEvent && (interruptEvent == null ? void 0 : interruptEvent.value) && !(interruptEvent == null ? void 0 : interruptEvent.response) && agentSessionRef.current) {
|
|
@@ -647,20 +677,53 @@ function useChat(options) {
|
|
|
647
677
|
messages2 = (0, import_runtime_client_gql3.convertGqlOutputToMessages)(
|
|
648
678
|
(0, import_runtime_client_gql3.filterAdjacentAgentStateMessages)(rawMessagesResponse)
|
|
649
679
|
);
|
|
650
|
-
if (messages2.length === 0) {
|
|
651
|
-
continue;
|
|
652
|
-
}
|
|
653
680
|
newMessages = [];
|
|
654
681
|
if (((_k = value.generateCopilotResponse.status) == null ? void 0 : _k.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
|
|
682
|
+
const guardrailsReason = ((_l = value.generateCopilotResponse.status.details) == null ? void 0 : _l.guardrailsReason) || "";
|
|
655
683
|
newMessages = [
|
|
656
684
|
new import_runtime_client_gql3.TextMessage({
|
|
657
685
|
role: import_runtime_client_gql3.MessageRole.Assistant,
|
|
658
|
-
content:
|
|
686
|
+
content: guardrailsReason
|
|
659
687
|
})
|
|
660
688
|
];
|
|
689
|
+
const guardrailsError = new import_shared4.CopilotKitError({
|
|
690
|
+
message: `Guardrails validation failed: ${guardrailsReason}`,
|
|
691
|
+
code: import_shared4.CopilotKitErrorCode.MISUSE
|
|
692
|
+
});
|
|
693
|
+
yield traceUIError(guardrailsError, {
|
|
694
|
+
statusReason: value.generateCopilotResponse.status.reason,
|
|
695
|
+
statusDetails: value.generateCopilotResponse.status.details
|
|
696
|
+
});
|
|
661
697
|
setMessages([...previousMessages, ...newMessages]);
|
|
662
698
|
break;
|
|
663
|
-
}
|
|
699
|
+
}
|
|
700
|
+
if (((_m = value.generateCopilotResponse.status) == null ? void 0 : _m.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "UNKNOWN_ERROR") {
|
|
701
|
+
const errorMessage = ((_n = value.generateCopilotResponse.status.details) == null ? void 0 : _n.description) || "An unknown error occurred";
|
|
702
|
+
const statusDetails = value.generateCopilotResponse.status.details;
|
|
703
|
+
const originalError = (statusDetails == null ? void 0 : statusDetails.originalError) || (statusDetails == null ? void 0 : statusDetails.error);
|
|
704
|
+
const originalCode = (originalError == null ? void 0 : originalError.code) || ((_o = originalError == null ? void 0 : originalError.extensions) == null ? void 0 : _o.code);
|
|
705
|
+
const originalSeverity = (originalError == null ? void 0 : originalError.severity) || ((_p = originalError == null ? void 0 : originalError.extensions) == null ? void 0 : _p.severity);
|
|
706
|
+
const originalVisibility = (originalError == null ? void 0 : originalError.visibility) || ((_q = originalError == null ? void 0 : originalError.extensions) == null ? void 0 : _q.visibility);
|
|
707
|
+
let errorCode = import_shared4.CopilotKitErrorCode.NETWORK_ERROR;
|
|
708
|
+
if (originalCode && Object.values(import_shared4.CopilotKitErrorCode).includes(originalCode)) {
|
|
709
|
+
errorCode = originalCode;
|
|
710
|
+
}
|
|
711
|
+
const structuredError = new import_shared4.CopilotKitError({
|
|
712
|
+
message: errorMessage,
|
|
713
|
+
code: errorCode,
|
|
714
|
+
severity: originalSeverity,
|
|
715
|
+
visibility: originalVisibility
|
|
716
|
+
});
|
|
717
|
+
setBannerError(structuredError);
|
|
718
|
+
yield traceUIError(structuredError, {
|
|
719
|
+
statusReason: value.generateCopilotResponse.status.reason,
|
|
720
|
+
statusDetails: value.generateCopilotResponse.status.details,
|
|
721
|
+
originalErrorCode: originalCode,
|
|
722
|
+
preservedStructure: !!originalCode
|
|
723
|
+
});
|
|
724
|
+
setIsLoading(false);
|
|
725
|
+
break;
|
|
726
|
+
} else if (messages2.length > 0) {
|
|
664
727
|
newMessages = [...messages2];
|
|
665
728
|
for (const message of messages2) {
|
|
666
729
|
if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
|
|
@@ -682,17 +745,22 @@ function useChat(options) {
|
|
|
682
745
|
lastAgentStateMessage.state.messages
|
|
683
746
|
);
|
|
684
747
|
}
|
|
685
|
-
setCoagentStatesWithRef((prevAgentStates) =>
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
748
|
+
setCoagentStatesWithRef((prevAgentStates) => {
|
|
749
|
+
var _a2;
|
|
750
|
+
return __spreadProps(__spreadValues({}, prevAgentStates), {
|
|
751
|
+
[lastAgentStateMessage.agentName]: {
|
|
752
|
+
name: lastAgentStateMessage.agentName,
|
|
753
|
+
state: lastAgentStateMessage.state,
|
|
754
|
+
running: lastAgentStateMessage.running,
|
|
755
|
+
active: lastAgentStateMessage.active,
|
|
756
|
+
threadId: lastAgentStateMessage.threadId,
|
|
757
|
+
nodeName: lastAgentStateMessage.nodeName,
|
|
758
|
+
runId: lastAgentStateMessage.runId,
|
|
759
|
+
// Preserve existing config from previous state
|
|
760
|
+
config: (_a2 = prevAgentStates[lastAgentStateMessage.agentName]) == null ? void 0 : _a2.config
|
|
761
|
+
}
|
|
762
|
+
});
|
|
763
|
+
});
|
|
696
764
|
if (lastAgentStateMessage.running) {
|
|
697
765
|
setAgentSession({
|
|
698
766
|
threadId: lastAgentStateMessage.threadId,
|
|
@@ -722,6 +790,39 @@ function useChat(options) {
|
|
|
722
790
|
newMessages
|
|
723
791
|
);
|
|
724
792
|
let didExecuteAction = false;
|
|
793
|
+
const executeActionFromMessage = (currentAction, actionMessage) => __async(this, null, function* () {
|
|
794
|
+
var _a2;
|
|
795
|
+
const isInterruptAction = interruptMessages.find((m) => m.id === actionMessage.id);
|
|
796
|
+
followUp = (_a2 = currentAction == null ? void 0 : currentAction.followUp) != null ? _a2 : !isInterruptAction;
|
|
797
|
+
if (currentAction == null ? void 0 : currentAction._setActivatingMessageId) {
|
|
798
|
+
currentAction._setActivatingMessageId(actionMessage.id);
|
|
799
|
+
}
|
|
800
|
+
const resultMessage = yield executeAction({
|
|
801
|
+
onFunctionCall,
|
|
802
|
+
message: actionMessage,
|
|
803
|
+
chatAbortControllerRef,
|
|
804
|
+
onError: (error) => {
|
|
805
|
+
addErrorToast([error]);
|
|
806
|
+
console.error(`Failed to execute action ${actionMessage.name}: ${error}`);
|
|
807
|
+
},
|
|
808
|
+
setMessages,
|
|
809
|
+
getFinalMessages: () => finalMessages,
|
|
810
|
+
isRenderAndWait: (currentAction == null ? void 0 : currentAction._isRenderAndWait) || false
|
|
811
|
+
});
|
|
812
|
+
didExecuteAction = true;
|
|
813
|
+
const messageIndex = finalMessages.findIndex((msg) => msg.id === actionMessage.id);
|
|
814
|
+
finalMessages.splice(messageIndex + 1, 0, resultMessage);
|
|
815
|
+
if (currentAction == null ? void 0 : currentAction._isRenderAndWait) {
|
|
816
|
+
const messagesForImmediateUpdate = [...finalMessages];
|
|
817
|
+
(0, import_react_dom.flushSync)(() => {
|
|
818
|
+
setMessages(messagesForImmediateUpdate);
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
if (currentAction == null ? void 0 : currentAction._setActivatingMessageId) {
|
|
822
|
+
currentAction._setActivatingMessageId(null);
|
|
823
|
+
}
|
|
824
|
+
return resultMessage;
|
|
825
|
+
});
|
|
725
826
|
if (onFunctionCall) {
|
|
726
827
|
const lastMessages = [];
|
|
727
828
|
for (let i = finalMessages.length - 1; i >= 0; i--) {
|
|
@@ -738,37 +839,28 @@ function useChat(options) {
|
|
|
738
839
|
(action2) => action2.name === message.name
|
|
739
840
|
);
|
|
740
841
|
const currentResultMessagePairedFeAction = message.isResultMessage() ? getPairedFeAction(actions, message) : null;
|
|
741
|
-
const executeActionFromMessage = (action2, message2) => __async(this, null, function* () {
|
|
742
|
-
var _a2;
|
|
743
|
-
const isInterruptAction = interruptMessages.find((m) => m.id === message2.id);
|
|
744
|
-
followUp = (_a2 = action2 == null ? void 0 : action2.followUp) != null ? _a2 : !isInterruptAction;
|
|
745
|
-
const resultMessage = yield executeAction({
|
|
746
|
-
onFunctionCall,
|
|
747
|
-
previousMessages,
|
|
748
|
-
message: message2,
|
|
749
|
-
chatAbortControllerRef,
|
|
750
|
-
onError: (error) => {
|
|
751
|
-
addErrorToast([error]);
|
|
752
|
-
console.error(`Failed to execute action ${message2.name}: ${error}`);
|
|
753
|
-
}
|
|
754
|
-
});
|
|
755
|
-
didExecuteAction = true;
|
|
756
|
-
const messageIndex = finalMessages.findIndex((msg) => msg.id === message2.id);
|
|
757
|
-
finalMessages.splice(messageIndex + 1, 0, resultMessage);
|
|
758
|
-
return resultMessage;
|
|
759
|
-
});
|
|
760
842
|
if (action && message.isActionExecutionMessage()) {
|
|
761
|
-
const
|
|
762
|
-
const
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
843
|
+
const isRenderAndWaitAction = (action == null ? void 0 : action._isRenderAndWait) || false;
|
|
844
|
+
const alreadyProcessed = isRenderAndWaitAction && finalMessages.some(
|
|
845
|
+
(fm) => fm.isResultMessage() && fm.actionExecutionId === message.id
|
|
846
|
+
);
|
|
847
|
+
if (alreadyProcessed) {
|
|
848
|
+
} else {
|
|
849
|
+
const resultMessage = yield executeActionFromMessage(
|
|
850
|
+
action,
|
|
851
|
+
message
|
|
852
|
+
);
|
|
853
|
+
const pairedFeAction = getPairedFeAction(actions, resultMessage);
|
|
854
|
+
if (pairedFeAction) {
|
|
855
|
+
const newExecutionMessage = new import_runtime_client_gql3.ActionExecutionMessage({
|
|
856
|
+
name: pairedFeAction.name,
|
|
857
|
+
arguments: (0, import_shared4.parseJson)(resultMessage.result, resultMessage.result),
|
|
858
|
+
status: message.status,
|
|
859
|
+
createdAt: message.createdAt,
|
|
860
|
+
parentMessageId: message.parentMessageId
|
|
861
|
+
});
|
|
862
|
+
yield executeActionFromMessage(pairedFeAction, newExecutionMessage);
|
|
863
|
+
}
|
|
772
864
|
}
|
|
773
865
|
} else if (message.isResultMessage() && currentResultMessagePairedFeAction) {
|
|
774
866
|
const newExecutionMessage = new import_runtime_client_gql3.ActionExecutionMessage({
|
|
@@ -786,16 +878,12 @@ function useChat(options) {
|
|
|
786
878
|
}
|
|
787
879
|
setMessages(finalMessages);
|
|
788
880
|
}
|
|
789
|
-
if (
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
(didExecuteAction || // the last message is a server side result
|
|
793
|
-
!isAgentRun && finalMessages.length && finalMessages[finalMessages.length - 1].isResultMessage()) && // the user did not stop generation
|
|
794
|
-
!((_m = chatAbortControllerRef.current) == null ? void 0 : _m.signal.aborted)
|
|
795
|
-
) {
|
|
881
|
+
if (followUp !== false && (didExecuteAction || // the last message is a server side result
|
|
882
|
+
!isAgentRun && finalMessages.length && finalMessages[finalMessages.length - 1].isResultMessage()) && // the user did not stop generation
|
|
883
|
+
!((_r = chatAbortControllerRef.current) == null ? void 0 : _r.signal.aborted)) {
|
|
796
884
|
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
797
885
|
return yield runChatCompletionRef.current(finalMessages);
|
|
798
|
-
} else if ((
|
|
886
|
+
} else if ((_s = chatAbortControllerRef.current) == null ? void 0 : _s.signal.aborted) {
|
|
799
887
|
const repairedMessages = finalMessages.filter((message, actionExecutionIndex) => {
|
|
800
888
|
if (message.isActionExecutionMessage()) {
|
|
801
889
|
return finalMessages.find(
|
|
@@ -806,7 +894,7 @@ function useChat(options) {
|
|
|
806
894
|
});
|
|
807
895
|
const repairedMessageIds = repairedMessages.map((message) => message.id);
|
|
808
896
|
setMessages(repairedMessages);
|
|
809
|
-
if ((
|
|
897
|
+
if ((_t = agentSessionRef.current) == null ? void 0 : _t.nodeName) {
|
|
810
898
|
setAgentSession({
|
|
811
899
|
threadId: agentSessionRef.current.threadId,
|
|
812
900
|
agentName: agentSessionRef.current.agentName,
|
|
@@ -900,21 +988,35 @@ function useChat(options) {
|
|
|
900
988
|
[isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
|
|
901
989
|
);
|
|
902
990
|
const reload = useAsyncCallback(
|
|
903
|
-
(
|
|
991
|
+
(reloadMessageId) => __async(this, null, function* () {
|
|
904
992
|
if (isLoading || messages.length === 0) {
|
|
905
993
|
return;
|
|
906
994
|
}
|
|
907
|
-
const
|
|
908
|
-
if (
|
|
909
|
-
console.warn(`Message with id ${
|
|
995
|
+
const reloadMessageIndex = messages.findIndex((msg) => msg.id === reloadMessageId);
|
|
996
|
+
if (reloadMessageIndex === -1) {
|
|
997
|
+
console.warn(`Message with id ${reloadMessageId} not found`);
|
|
910
998
|
return;
|
|
911
999
|
}
|
|
912
|
-
|
|
913
|
-
if (
|
|
914
|
-
|
|
1000
|
+
const reloadMessageRole = messages[reloadMessageIndex].role;
|
|
1001
|
+
if (reloadMessageRole !== import_runtime_client_gql3.MessageRole.Assistant) {
|
|
1002
|
+
console.warn(`Regenerate cannot be performed on ${reloadMessageRole} role`);
|
|
1003
|
+
return;
|
|
915
1004
|
}
|
|
916
|
-
|
|
917
|
-
|
|
1005
|
+
let historyCutoff = [];
|
|
1006
|
+
if (messages.length > 2) {
|
|
1007
|
+
const lastUserMessageBeforeRegenerate = messages.slice(0, reloadMessageIndex).reverse().find(
|
|
1008
|
+
(msg) => (
|
|
1009
|
+
// @ts-expect-error -- message has role
|
|
1010
|
+
msg.role === import_runtime_client_gql3.MessageRole.User
|
|
1011
|
+
)
|
|
1012
|
+
);
|
|
1013
|
+
const indexOfLastUserMessageBeforeRegenerate = messages.findIndex(
|
|
1014
|
+
(msg) => msg.id === lastUserMessageBeforeRegenerate.id
|
|
1015
|
+
);
|
|
1016
|
+
historyCutoff = messages.slice(0, indexOfLastUserMessageBeforeRegenerate + 1);
|
|
1017
|
+
}
|
|
1018
|
+
setMessages(historyCutoff);
|
|
1019
|
+
return runChatCompletionAndHandleFunctionCall(historyCutoff);
|
|
918
1020
|
}),
|
|
919
1021
|
[isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
|
|
920
1022
|
);
|
|
@@ -949,20 +1051,31 @@ function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
|
|
|
949
1051
|
function executeAction(_0) {
|
|
950
1052
|
return __async(this, arguments, function* ({
|
|
951
1053
|
onFunctionCall,
|
|
952
|
-
previousMessages,
|
|
953
1054
|
message,
|
|
954
1055
|
chatAbortControllerRef,
|
|
955
|
-
onError
|
|
1056
|
+
onError,
|
|
1057
|
+
setMessages,
|
|
1058
|
+
getFinalMessages,
|
|
1059
|
+
isRenderAndWait
|
|
956
1060
|
}) {
|
|
957
1061
|
let result;
|
|
958
1062
|
let error = null;
|
|
1063
|
+
const currentMessagesForHandler = getFinalMessages();
|
|
1064
|
+
const handlerReturnedPromise = onFunctionCall({
|
|
1065
|
+
messages: currentMessagesForHandler,
|
|
1066
|
+
name: message.name,
|
|
1067
|
+
args: message.arguments
|
|
1068
|
+
});
|
|
1069
|
+
if (isRenderAndWait) {
|
|
1070
|
+
const currentMessagesForRender = getFinalMessages();
|
|
1071
|
+
(0, import_react_dom.flushSync)(() => {
|
|
1072
|
+
setMessages([...currentMessagesForRender]);
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
959
1075
|
try {
|
|
960
1076
|
result = yield Promise.race([
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
name: message.name,
|
|
964
|
-
args: message.arguments
|
|
965
|
-
}),
|
|
1077
|
+
handlerReturnedPromise,
|
|
1078
|
+
// Await the promise returned by the handler
|
|
966
1079
|
new Promise(
|
|
967
1080
|
(resolve) => {
|
|
968
1081
|
var _a;
|
|
@@ -1010,7 +1123,7 @@ function getPairedFeAction(actions, message) {
|
|
|
1010
1123
|
|
|
1011
1124
|
// src/components/copilot-provider/copilotkit.tsx
|
|
1012
1125
|
var import_react7 = require("react");
|
|
1013
|
-
var
|
|
1126
|
+
var import_react_dom2 = require("react-dom");
|
|
1014
1127
|
var import_shared5 = require("@copilotkit/shared");
|
|
1015
1128
|
|
|
1016
1129
|
// src/context/copilot-messages-context.tsx
|