@copilotkit/react-core 1.6.0-next.1 → 1.6.0-next.11
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 +77 -0
- package/dist/{chunk-XERJQUHA.mjs → chunk-4CEQJ2X6.mjs} +2 -2
- package/dist/{chunk-XERJQUHA.mjs.map → chunk-4CEQJ2X6.mjs.map} +1 -1
- package/dist/{chunk-VMYLY5KI.mjs → chunk-4EC4JK7H.mjs} +58 -48
- package/dist/chunk-4EC4JK7H.mjs.map +1 -0
- package/dist/{chunk-ZAU4EMBM.mjs → chunk-F7O6L7C3.mjs} +4 -5
- package/dist/chunk-F7O6L7C3.mjs.map +1 -0
- package/dist/{chunk-2DPTBHGN.mjs → chunk-FUO5LKSJ.mjs} +115 -63
- package/dist/chunk-FUO5LKSJ.mjs.map +1 -0
- package/dist/{chunk-4ED56DQX.mjs → chunk-HBCP2KNA.mjs} +3 -4
- package/dist/chunk-HBCP2KNA.mjs.map +1 -0
- package/dist/{chunk-Z3U4UBG2.mjs → chunk-T4CAQC6B.mjs} +2 -2
- package/dist/{chunk-JFCNSGNU.mjs → chunk-Y2T56VSE.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit-props.d.ts +1 -2
- package/dist/components/copilot-provider/copilotkit.d.ts +1 -2
- package/dist/components/copilot-provider/index.d.ts +1 -2
- package/dist/components/index.d.ts +1 -2
- package/dist/context/copilot-context.d.ts +10 -148
- package/dist/context/index.d.ts +1 -2
- package/dist/copilot-context-9ae8990b.d.ts +183 -0
- package/dist/hooks/index.d.ts +1 -2
- package/dist/hooks/index.js +180 -123
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +6 -6
- package/dist/hooks/use-chat.d.ts +1 -2
- package/dist/hooks/use-chat.js +113 -64
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +2 -2
- package/dist/hooks/use-coagent.d.ts +1 -2
- package/dist/hooks/use-coagent.js +171 -112
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +4 -4
- package/dist/hooks/use-copilot-chat.d.ts +1 -2
- package/dist/hooks/use-copilot-chat.js +116 -67
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +3 -3
- package/dist/hooks/use-langgraph-interrupt-render.js +3 -4
- package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt-render.mjs +1 -1
- package/dist/hooks/use-langgraph-interrupt.d.ts +9 -2
- package/dist/hooks/use-langgraph-interrupt.js +117 -69
- package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt.mjs +4 -4
- package/dist/index.d.ts +1 -2
- package/dist/index.js +187 -130
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -7
- package/dist/lib/copilot-task.d.ts +1 -2
- package/dist/lib/copilot-task.js +1 -1
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +2 -2
- package/dist/lib/index.d.ts +1 -2
- package/dist/lib/index.js +1 -1
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +2 -2
- package/dist/types/frontend-action.d.ts +2 -1
- package/dist/types/frontend-action.js +1 -1
- package/dist/types/frontend-action.js.map +1 -1
- package/dist/types/frontend-action.mjs +1 -1
- package/dist/types/interrupt-action.d.ts +10 -35
- package/dist/types/interrupt-action.js.map +1 -1
- package/dist/utils/extract.d.ts +1 -2
- package/dist/utils/index.d.ts +1 -2
- package/package.json +3 -3
- package/src/hooks/use-chat.ts +134 -61
- package/src/hooks/use-coagent.ts +70 -56
- package/src/hooks/use-langgraph-interrupt-render.ts +7 -3
- package/src/hooks/use-langgraph-interrupt.ts +1 -2
- package/src/types/frontend-action.ts +5 -2
- package/src/types/interrupt-action.ts +16 -12
- package/dist/chunk-2DPTBHGN.mjs.map +0 -1
- package/dist/chunk-4ED56DQX.mjs.map +0 -1
- package/dist/chunk-VMYLY5KI.mjs.map +0 -1
- package/dist/chunk-ZAU4EMBM.mjs.map +0 -1
- /package/dist/{chunk-Z3U4UBG2.mjs.map → chunk-T4CAQC6B.mjs.map} +0 -0
- /package/dist/{chunk-JFCNSGNU.mjs.map → chunk-Y2T56VSE.mjs.map} +0 -0
|
@@ -190,7 +190,7 @@ function useCopilotMessagesContext() {
|
|
|
190
190
|
|
|
191
191
|
// src/hooks/use-copilot-chat.ts
|
|
192
192
|
var import_react8 = require("react");
|
|
193
|
-
var
|
|
193
|
+
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
194
194
|
|
|
195
195
|
// src/hooks/use-chat.ts
|
|
196
196
|
var import_react6 = require("react");
|
|
@@ -202,7 +202,7 @@ var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
|
|
|
202
202
|
var import_shared = require("@copilotkit/shared");
|
|
203
203
|
function processActionsForRuntimeRequest(actions) {
|
|
204
204
|
const filteredActions = actions.filter(
|
|
205
|
-
(action) => action.available !== import_runtime_client_gql.ActionInputAvailability.Disabled && action.disabled !== true && action.name !== "*"
|
|
205
|
+
(action) => action.available !== import_runtime_client_gql.ActionInputAvailability.Disabled && action.disabled !== true && action.name !== "*" && action.available != "frontend" && !action.pairedAction
|
|
206
206
|
).map((action) => {
|
|
207
207
|
let available = import_runtime_client_gql.ActionInputAvailability.Enabled;
|
|
208
208
|
if (action.disabled) {
|
|
@@ -379,7 +379,6 @@ var useCopilotRuntimeClient = (options) => {
|
|
|
379
379
|
};
|
|
380
380
|
|
|
381
381
|
// src/hooks/use-chat.ts
|
|
382
|
-
var import_runtime_client_gql4 = require("@copilotkit/runtime-client-gql");
|
|
383
382
|
function useChat(options) {
|
|
384
383
|
const {
|
|
385
384
|
messages,
|
|
@@ -428,7 +427,7 @@ function useChat(options) {
|
|
|
428
427
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
429
428
|
setIsLoading(true);
|
|
430
429
|
const interruptEvent = langGraphInterruptAction == null ? void 0 : langGraphInterruptAction.event;
|
|
431
|
-
if ((interruptEvent == null ? void 0 : interruptEvent.name) ===
|
|
430
|
+
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) {
|
|
432
431
|
addErrorToast([
|
|
433
432
|
new Error(
|
|
434
433
|
"A message was sent while interrupt is active. This will cause failure on the agent side"
|
|
@@ -522,19 +521,16 @@ function useChat(options) {
|
|
|
522
521
|
setExtensions(extensionsRef.current);
|
|
523
522
|
let rawMessagesResponse = value.generateCopilotResponse.messages;
|
|
524
523
|
((_i = (_h = value.generateCopilotResponse) == null ? void 0 : _h.metaEvents) != null ? _i : []).forEach((ev) => {
|
|
525
|
-
if (ev.name ===
|
|
524
|
+
if (ev.name === import_runtime_client_gql3.MetaEventName.LangGraphInterruptEvent) {
|
|
526
525
|
let eventValue = (0, import_runtime_client_gql3.langGraphInterruptEvent)(ev).value;
|
|
527
|
-
|
|
528
|
-
eventValue = JSON.parse(eventValue);
|
|
529
|
-
} catch (e) {
|
|
530
|
-
}
|
|
526
|
+
eventValue = (0, import_shared2.parseJson)(eventValue, eventValue);
|
|
531
527
|
setLangGraphInterruptAction({
|
|
532
528
|
event: __spreadProps(__spreadValues({}, (0, import_runtime_client_gql3.langGraphInterruptEvent)(ev)), {
|
|
533
529
|
value: eventValue
|
|
534
530
|
})
|
|
535
531
|
});
|
|
536
532
|
}
|
|
537
|
-
if (ev.name ===
|
|
533
|
+
if (ev.name === import_runtime_client_gql3.MetaEventName.CopilotKitLangGraphInterruptEvent) {
|
|
538
534
|
const data = ev.data;
|
|
539
535
|
rawMessagesResponse = [...rawMessagesResponse, ...data.messages];
|
|
540
536
|
interruptMessages = (0, import_runtime_client_gql3.convertGqlOutputToMessages)(
|
|
@@ -615,7 +611,7 @@ function useChat(options) {
|
|
|
615
611
|
setMessages([...previousMessages, ...newMessages]);
|
|
616
612
|
}
|
|
617
613
|
}
|
|
618
|
-
|
|
614
|
+
let finalMessages = constructFinalMessages(
|
|
619
615
|
[...syncedMessages, ...interruptMessages],
|
|
620
616
|
previousMessages,
|
|
621
617
|
newMessages
|
|
@@ -625,7 +621,7 @@ function useChat(options) {
|
|
|
625
621
|
const lastMessages = [];
|
|
626
622
|
for (let i = finalMessages.length - 1; i >= 0; i--) {
|
|
627
623
|
const message = finalMessages[i];
|
|
628
|
-
if (message.isActionExecutionMessage() && message.status.code !== import_runtime_client_gql3.MessageStatusCode.Pending) {
|
|
624
|
+
if ((message.isActionExecutionMessage() || message.isResultMessage()) && message.status.code !== import_runtime_client_gql3.MessageStatusCode.Pending) {
|
|
629
625
|
lastMessages.unshift(message);
|
|
630
626
|
} else {
|
|
631
627
|
break;
|
|
@@ -633,59 +629,51 @@ function useChat(options) {
|
|
|
633
629
|
}
|
|
634
630
|
for (const message of lastMessages) {
|
|
635
631
|
setMessages(finalMessages);
|
|
636
|
-
const action = actions.find(
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
"abort",
|
|
653
|
-
() => resolve("Operation was aborted by the user")
|
|
654
|
-
);
|
|
655
|
-
}
|
|
656
|
-
),
|
|
657
|
-
// if the user stopped generation, we also abort consecutive actions
|
|
658
|
-
new Promise((resolve) => {
|
|
659
|
-
var _a2;
|
|
660
|
-
if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
|
|
661
|
-
resolve("Operation was aborted by the user");
|
|
662
|
-
}
|
|
663
|
-
})
|
|
664
|
-
]);
|
|
665
|
-
} catch (e) {
|
|
666
|
-
error = e;
|
|
667
|
-
addErrorToast([error]);
|
|
668
|
-
result = `Failed to execute action ${message.name}. ${error.message}`;
|
|
669
|
-
console.error(`Failed to execute action ${message.name}: ${error}`);
|
|
670
|
-
}
|
|
632
|
+
const action = actions.find(
|
|
633
|
+
(action2) => action2.name === message.name
|
|
634
|
+
);
|
|
635
|
+
const currentResultMessagePairedFeAction = message.isResultMessage() ? getPairedFeAction(actions, message) : null;
|
|
636
|
+
const executeActionFromMessage = (action2, message2) => __async(this, null, function* () {
|
|
637
|
+
followUp = action2 == null ? void 0 : action2.followUp;
|
|
638
|
+
const resultMessage = yield executeAction({
|
|
639
|
+
onFunctionCall,
|
|
640
|
+
previousMessages,
|
|
641
|
+
message: message2,
|
|
642
|
+
chatAbortControllerRef,
|
|
643
|
+
onError: (error) => {
|
|
644
|
+
addErrorToast([error]);
|
|
645
|
+
console.error(`Failed to execute action ${message2.name}: ${error}`);
|
|
646
|
+
}
|
|
647
|
+
});
|
|
671
648
|
didExecuteAction = true;
|
|
672
|
-
const messageIndex = finalMessages.findIndex((msg) => msg.id ===
|
|
673
|
-
finalMessages.splice(
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
649
|
+
const messageIndex = finalMessages.findIndex((msg) => msg.id === message2.id);
|
|
650
|
+
finalMessages.splice(messageIndex + 1, 0, resultMessage);
|
|
651
|
+
return resultMessage;
|
|
652
|
+
});
|
|
653
|
+
if (action && message.isActionExecutionMessage()) {
|
|
654
|
+
const resultMessage = yield executeActionFromMessage(action, message);
|
|
655
|
+
const pairedFeAction = getPairedFeAction(actions, resultMessage);
|
|
656
|
+
if (pairedFeAction) {
|
|
657
|
+
const newExecutionMessage = new import_runtime_client_gql3.ActionExecutionMessage({
|
|
658
|
+
name: pairedFeAction.name,
|
|
659
|
+
arguments: (0, import_shared2.parseJson)(resultMessage.result, resultMessage.result),
|
|
660
|
+
status: message.status,
|
|
661
|
+
createdAt: message.createdAt,
|
|
662
|
+
parentMessageId: message.parentMessageId
|
|
663
|
+
});
|
|
664
|
+
yield executeActionFromMessage(pairedFeAction, newExecutionMessage);
|
|
665
|
+
}
|
|
666
|
+
} else if (message.isResultMessage() && currentResultMessagePairedFeAction) {
|
|
667
|
+
const newExecutionMessage = new import_runtime_client_gql3.ActionExecutionMessage({
|
|
668
|
+
name: currentResultMessagePairedFeAction.name,
|
|
669
|
+
arguments: (0, import_shared2.parseJson)(message.result, message.result),
|
|
670
|
+
status: message.status,
|
|
671
|
+
createdAt: message.createdAt
|
|
672
|
+
});
|
|
673
|
+
finalMessages.push(newExecutionMessage);
|
|
674
|
+
yield executeActionFromMessage(
|
|
675
|
+
currentResultMessagePairedFeAction,
|
|
676
|
+
newExecutionMessage
|
|
689
677
|
);
|
|
690
678
|
}
|
|
691
679
|
}
|
|
@@ -756,7 +744,7 @@ function useChat(options) {
|
|
|
756
744
|
if (!event)
|
|
757
745
|
return acc;
|
|
758
746
|
switch (event.name) {
|
|
759
|
-
case
|
|
747
|
+
case import_runtime_client_gql3.MetaEventName.LangGraphInterruptEvent:
|
|
760
748
|
if (event.response) {
|
|
761
749
|
setLangGraphInterruptAction(null);
|
|
762
750
|
const value = event.value;
|
|
@@ -832,6 +820,67 @@ function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
|
|
|
832
820
|
}
|
|
833
821
|
return finalMessages;
|
|
834
822
|
}
|
|
823
|
+
function executeAction(_0) {
|
|
824
|
+
return __async(this, arguments, function* ({
|
|
825
|
+
onFunctionCall,
|
|
826
|
+
previousMessages,
|
|
827
|
+
message,
|
|
828
|
+
chatAbortControllerRef,
|
|
829
|
+
onError
|
|
830
|
+
}) {
|
|
831
|
+
let result;
|
|
832
|
+
let error = null;
|
|
833
|
+
try {
|
|
834
|
+
result = yield Promise.race([
|
|
835
|
+
onFunctionCall({
|
|
836
|
+
messages: previousMessages,
|
|
837
|
+
name: message.name,
|
|
838
|
+
args: message.arguments
|
|
839
|
+
}),
|
|
840
|
+
new Promise(
|
|
841
|
+
(resolve) => {
|
|
842
|
+
var _a;
|
|
843
|
+
return (_a = chatAbortControllerRef.current) == null ? void 0 : _a.signal.addEventListener(
|
|
844
|
+
"abort",
|
|
845
|
+
() => resolve("Operation was aborted by the user")
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
),
|
|
849
|
+
// if the user stopped generation, we also abort consecutive actions
|
|
850
|
+
new Promise((resolve) => {
|
|
851
|
+
var _a;
|
|
852
|
+
if ((_a = chatAbortControllerRef.current) == null ? void 0 : _a.signal.aborted) {
|
|
853
|
+
resolve("Operation was aborted by the user");
|
|
854
|
+
}
|
|
855
|
+
})
|
|
856
|
+
]);
|
|
857
|
+
} catch (e) {
|
|
858
|
+
onError(e);
|
|
859
|
+
}
|
|
860
|
+
return new import_runtime_client_gql3.ResultMessage({
|
|
861
|
+
id: "result-" + message.id,
|
|
862
|
+
result: import_runtime_client_gql3.ResultMessage.encodeResult(
|
|
863
|
+
error ? {
|
|
864
|
+
content: result,
|
|
865
|
+
error: JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)))
|
|
866
|
+
} : result
|
|
867
|
+
),
|
|
868
|
+
actionExecutionId: message.id,
|
|
869
|
+
actionName: message.name
|
|
870
|
+
});
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
function getPairedFeAction(actions, message) {
|
|
874
|
+
let actionName = null;
|
|
875
|
+
if (message.isActionExecutionMessage()) {
|
|
876
|
+
actionName = message.name;
|
|
877
|
+
} else if (message.isResultMessage()) {
|
|
878
|
+
actionName = message.actionName;
|
|
879
|
+
}
|
|
880
|
+
return actions.find(
|
|
881
|
+
(action) => action.name === actionName && action.available === "frontend" || action.pairedAction === actionName
|
|
882
|
+
);
|
|
883
|
+
}
|
|
835
884
|
|
|
836
885
|
// src/components/copilot-provider/copilotkit.tsx
|
|
837
886
|
var import_react7 = require("react");
|
|
@@ -883,9 +932,9 @@ function useCopilotChat(_a = {}) {
|
|
|
883
932
|
const makeSystemMessageCallback = (0, import_react8.useCallback)(() => {
|
|
884
933
|
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
885
934
|
const contextString = latestGetContextString.current([], defaultCopilotContextCategories);
|
|
886
|
-
return new
|
|
935
|
+
return new import_runtime_client_gql4.TextMessage({
|
|
887
936
|
content: systemMessageMaker(contextString, chatInstructions),
|
|
888
|
-
role:
|
|
937
|
+
role: import_runtime_client_gql4.Role.System
|
|
889
938
|
});
|
|
890
939
|
}, [getContextString, makeSystemMessage, chatInstructions]);
|
|
891
940
|
const onCoAgentStateRender = useAsyncCallback(
|
|
@@ -1037,15 +1086,13 @@ ${additionalInstructions}` : "");
|
|
|
1037
1086
|
}
|
|
1038
1087
|
|
|
1039
1088
|
// src/hooks/use-coagent.ts
|
|
1089
|
+
var import_shared4 = require("@copilotkit/shared");
|
|
1040
1090
|
function useCoAgent(options) {
|
|
1041
1091
|
const generalContext = useCopilotContext();
|
|
1042
1092
|
const { availableAgents } = generalContext;
|
|
1043
1093
|
const { addToast } = useToast();
|
|
1044
1094
|
const lastLoadedThreadId = (0, import_react9.useRef)();
|
|
1045
1095
|
const lastLoadedState = (0, import_react9.useRef)();
|
|
1046
|
-
const isExternalStateManagement = (options2) => {
|
|
1047
|
-
return "state" in options2 && "setState" in options2;
|
|
1048
|
-
};
|
|
1049
1096
|
const { name } = options;
|
|
1050
1097
|
(0, import_react9.useEffect)(() => {
|
|
1051
1098
|
if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === name)) {
|
|
@@ -1054,44 +1101,27 @@ function useCoAgent(options) {
|
|
|
1054
1101
|
addToast({ type: "warning", message });
|
|
1055
1102
|
}
|
|
1056
1103
|
}, [availableAgents]);
|
|
1057
|
-
const isInternalStateManagementWithInitial = (options2) => {
|
|
1058
|
-
return "initialState" in options2;
|
|
1059
|
-
};
|
|
1060
1104
|
const messagesContext = useCopilotMessagesContext();
|
|
1061
1105
|
const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
|
|
1062
1106
|
const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } = context;
|
|
1063
1107
|
const { appendMessage, runChatCompletion } = useCopilotChat();
|
|
1064
|
-
const getCoagentState = (coagentStates2, name2) => {
|
|
1065
|
-
var _a;
|
|
1066
|
-
if (coagentStates2[name2]) {
|
|
1067
|
-
return coagentStates2[name2];
|
|
1068
|
-
} else {
|
|
1069
|
-
return {
|
|
1070
|
-
name: name2,
|
|
1071
|
-
state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
|
|
1072
|
-
configurable: (_a = options.configurable) != null ? _a : {},
|
|
1073
|
-
running: false,
|
|
1074
|
-
active: false,
|
|
1075
|
-
threadId: void 0,
|
|
1076
|
-
nodeName: void 0,
|
|
1077
|
-
runId: void 0
|
|
1078
|
-
};
|
|
1079
|
-
}
|
|
1080
|
-
};
|
|
1081
1108
|
const runtimeClient = useCopilotRuntimeClient({
|
|
1082
1109
|
url: copilotApiConfig.chatApiEndpoint,
|
|
1083
1110
|
publicApiKey: copilotApiConfig.publicApiKey,
|
|
1084
1111
|
credentials: copilotApiConfig.credentials
|
|
1085
1112
|
});
|
|
1086
|
-
const setState = (
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1113
|
+
const setState = (0, import_react9.useCallback)(
|
|
1114
|
+
(newState) => {
|
|
1115
|
+
let coagentState = getCoagentState({ coagentStates, name, options });
|
|
1116
|
+
const updatedState = typeof newState === "function" ? newState(coagentState.state) : newState;
|
|
1117
|
+
setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
|
|
1118
|
+
[name]: __spreadProps(__spreadValues({}, coagentState), {
|
|
1119
|
+
state: updatedState
|
|
1120
|
+
})
|
|
1121
|
+
}));
|
|
1122
|
+
},
|
|
1123
|
+
[coagentStates, name]
|
|
1124
|
+
);
|
|
1095
1125
|
(0, import_react9.useEffect)(() => {
|
|
1096
1126
|
const fetchAgentState = () => __async(this, null, function* () {
|
|
1097
1127
|
var _a, _b, _c, _d;
|
|
@@ -1107,13 +1137,12 @@ function useCoAgent(options) {
|
|
|
1107
1137
|
if (((_d = (_c = result.data) == null ? void 0 : _c.loadAgentState) == null ? void 0 : _d.threadExists) && newState && newState != "{}") {
|
|
1108
1138
|
lastLoadedState.current = newState;
|
|
1109
1139
|
lastLoadedThreadId.current = threadId;
|
|
1110
|
-
const fetchedState =
|
|
1140
|
+
const fetchedState = (0, import_shared4.parseJson)(newState, {});
|
|
1111
1141
|
isExternalStateManagement(options) ? options.setState(fetchedState) : setState(fetchedState);
|
|
1112
1142
|
}
|
|
1113
1143
|
});
|
|
1114
1144
|
void fetchAgentState();
|
|
1115
1145
|
}, [threadId]);
|
|
1116
|
-
const coagentState = getCoagentState(coagentStates, name);
|
|
1117
1146
|
(0, import_react9.useEffect)(() => {
|
|
1118
1147
|
if (isExternalStateManagement(options)) {
|
|
1119
1148
|
setState(options.state);
|
|
@@ -1131,17 +1160,20 @@ function useCoAgent(options) {
|
|
|
1131
1160
|
}),
|
|
1132
1161
|
[name, context, appendMessage, runChatCompletion]
|
|
1133
1162
|
);
|
|
1134
|
-
return {
|
|
1135
|
-
name,
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1163
|
+
return (0, import_react9.useMemo)(() => {
|
|
1164
|
+
const coagentState = getCoagentState({ coagentStates, name, options });
|
|
1165
|
+
return {
|
|
1166
|
+
name,
|
|
1167
|
+
nodeName: coagentState.nodeName,
|
|
1168
|
+
threadId: coagentState.threadId,
|
|
1169
|
+
running: coagentState.running,
|
|
1170
|
+
state: coagentState.state,
|
|
1171
|
+
setState: isExternalStateManagement(options) ? options.setState : setState,
|
|
1172
|
+
start: () => startAgent(name, context),
|
|
1173
|
+
stop: () => stopAgent(name, context),
|
|
1174
|
+
run: runAgentCallback
|
|
1175
|
+
};
|
|
1176
|
+
}, [name, coagentStates, options, setState, runAgentCallback]);
|
|
1145
1177
|
}
|
|
1146
1178
|
function startAgent(name, context) {
|
|
1147
1179
|
const { setAgentSession } = context;
|
|
@@ -1197,6 +1229,33 @@ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
|
|
|
1197
1229
|
}
|
|
1198
1230
|
});
|
|
1199
1231
|
}
|
|
1232
|
+
var isExternalStateManagement = (options) => {
|
|
1233
|
+
return "state" in options && "setState" in options;
|
|
1234
|
+
};
|
|
1235
|
+
var isInternalStateManagementWithInitial = (options) => {
|
|
1236
|
+
return "initialState" in options;
|
|
1237
|
+
};
|
|
1238
|
+
var getCoagentState = ({
|
|
1239
|
+
coagentStates,
|
|
1240
|
+
name,
|
|
1241
|
+
options
|
|
1242
|
+
}) => {
|
|
1243
|
+
var _a;
|
|
1244
|
+
if (coagentStates[name]) {
|
|
1245
|
+
return coagentStates[name];
|
|
1246
|
+
} else {
|
|
1247
|
+
return {
|
|
1248
|
+
name,
|
|
1249
|
+
state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
|
|
1250
|
+
configurable: (_a = options.configurable) != null ? _a : {},
|
|
1251
|
+
running: false,
|
|
1252
|
+
active: false,
|
|
1253
|
+
threadId: void 0,
|
|
1254
|
+
nodeName: void 0,
|
|
1255
|
+
runId: void 0
|
|
1256
|
+
};
|
|
1257
|
+
}
|
|
1258
|
+
};
|
|
1200
1259
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1201
1260
|
0 && (module.exports = {
|
|
1202
1261
|
runAgent,
|