@copilotkit/react-core 1.6.0-next.6 → 1.6.0-next.7

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.
@@ -16,7 +16,7 @@ import {
16
16
  startAgent,
17
17
  stopAgent,
18
18
  useCoAgent
19
- } from "../chunk-HM7CCAUL.mjs";
19
+ } from "../chunk-ADTDNMYG.mjs";
20
20
  import {
21
21
  useCopilotAuthenticatedAction_c
22
22
  } from "../chunk-2F6RYBYS.mjs";
@@ -1042,9 +1042,6 @@ function useCoAgent(options) {
1042
1042
  const { addToast } = useToast();
1043
1043
  const lastLoadedThreadId = (0, import_react9.useRef)();
1044
1044
  const lastLoadedState = (0, import_react9.useRef)();
1045
- const isExternalStateManagement = (options2) => {
1046
- return "state" in options2 && "setState" in options2;
1047
- };
1048
1045
  const { name } = options;
1049
1046
  (0, import_react9.useEffect)(() => {
1050
1047
  if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === name)) {
@@ -1053,44 +1050,27 @@ function useCoAgent(options) {
1053
1050
  addToast({ type: "warning", message });
1054
1051
  }
1055
1052
  }, [availableAgents]);
1056
- const isInternalStateManagementWithInitial = (options2) => {
1057
- return "initialState" in options2;
1058
- };
1059
1053
  const messagesContext = useCopilotMessagesContext();
1060
1054
  const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
1061
1055
  const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } = context;
1062
1056
  const { appendMessage, runChatCompletion } = useCopilotChat();
1063
- const getCoagentState = (coagentStates2, name2) => {
1064
- var _a;
1065
- if (coagentStates2[name2]) {
1066
- return coagentStates2[name2];
1067
- } else {
1068
- return {
1069
- name: name2,
1070
- state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
1071
- configurable: (_a = options.configurable) != null ? _a : {},
1072
- running: false,
1073
- active: false,
1074
- threadId: void 0,
1075
- nodeName: void 0,
1076
- runId: void 0
1077
- };
1078
- }
1079
- };
1080
1057
  const runtimeClient = useCopilotRuntimeClient({
1081
1058
  url: copilotApiConfig.chatApiEndpoint,
1082
1059
  publicApiKey: copilotApiConfig.publicApiKey,
1083
1060
  credentials: copilotApiConfig.credentials
1084
1061
  });
1085
- const setState = (newState) => {
1086
- let coagentState2 = getCoagentState(coagentStatesRef.current || {}, name);
1087
- const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
1088
- setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
1089
- [name]: __spreadProps(__spreadValues({}, coagentState2), {
1090
- state: updatedState
1091
- })
1092
- }));
1093
- };
1062
+ const setState = (0, import_react9.useCallback)(
1063
+ (newState) => {
1064
+ let coagentState = getCoagentState({ coagentStates, name, options });
1065
+ const updatedState = typeof newState === "function" ? newState(coagentState.state) : newState;
1066
+ setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
1067
+ [name]: __spreadProps(__spreadValues({}, coagentState), {
1068
+ state: updatedState
1069
+ })
1070
+ }));
1071
+ },
1072
+ [coagentStates, name]
1073
+ );
1094
1074
  (0, import_react9.useEffect)(() => {
1095
1075
  const fetchAgentState = () => __async(this, null, function* () {
1096
1076
  var _a, _b, _c, _d;
@@ -1112,7 +1092,6 @@ function useCoAgent(options) {
1112
1092
  });
1113
1093
  void fetchAgentState();
1114
1094
  }, [threadId]);
1115
- const coagentState = getCoagentState(coagentStates, name);
1116
1095
  (0, import_react9.useEffect)(() => {
1117
1096
  if (isExternalStateManagement(options)) {
1118
1097
  setState(options.state);
@@ -1130,17 +1109,20 @@ function useCoAgent(options) {
1130
1109
  }),
1131
1110
  [name, context, appendMessage, runChatCompletion]
1132
1111
  );
1133
- return {
1134
- name,
1135
- nodeName: coagentState.nodeName,
1136
- threadId: coagentState.threadId,
1137
- running: coagentState.running,
1138
- state: coagentState.state,
1139
- setState: isExternalStateManagement(options) ? options.setState : setState,
1140
- start: () => startAgent(name, context),
1141
- stop: () => stopAgent(name, context),
1142
- run: runAgentCallback
1143
- };
1112
+ return (0, import_react9.useMemo)(() => {
1113
+ const coagentState = getCoagentState({ coagentStates, name, options });
1114
+ return {
1115
+ name,
1116
+ nodeName: coagentState.nodeName,
1117
+ threadId: coagentState.threadId,
1118
+ running: coagentState.running,
1119
+ state: coagentState.state,
1120
+ setState: isExternalStateManagement(options) ? options.setState : setState,
1121
+ start: () => startAgent(name, context),
1122
+ stop: () => stopAgent(name, context),
1123
+ run: runAgentCallback
1124
+ };
1125
+ }, [name, coagentStates, options, setState, runAgentCallback]);
1144
1126
  }
1145
1127
  function startAgent(name, context) {
1146
1128
  const { setAgentSession } = context;
@@ -1196,6 +1178,33 @@ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
1196
1178
  }
1197
1179
  });
1198
1180
  }
1181
+ var isExternalStateManagement = (options) => {
1182
+ return "state" in options && "setState" in options;
1183
+ };
1184
+ var isInternalStateManagementWithInitial = (options) => {
1185
+ return "initialState" in options;
1186
+ };
1187
+ var getCoagentState = ({
1188
+ coagentStates,
1189
+ name,
1190
+ options
1191
+ }) => {
1192
+ var _a;
1193
+ if (coagentStates[name]) {
1194
+ return coagentStates[name];
1195
+ } else {
1196
+ return {
1197
+ name,
1198
+ state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
1199
+ configurable: (_a = options.configurable) != null ? _a : {},
1200
+ running: false,
1201
+ active: false,
1202
+ threadId: void 0,
1203
+ nodeName: void 0,
1204
+ runId: void 0
1205
+ };
1206
+ }
1207
+ };
1199
1208
  // Annotate the CommonJS export names for ESM import in node:
1200
1209
  0 && (module.exports = {
1201
1210
  runAgent,