@copilotkit/react-core 1.6.0-next.5 → 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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # ui
2
2
 
3
+ ## 1.6.0-next.7
4
+
5
+ ### Patch Changes
6
+
7
+ - d800f03: - fix: use memoization in useCoAgent internal functions
8
+ - @copilotkit/runtime-client-gql@1.6.0-next.7
9
+ - @copilotkit/shared@1.6.0-next.7
10
+
11
+ ## 1.6.0-next.6
12
+
13
+ ### Patch Changes
14
+
15
+ - @copilotkit/runtime-client-gql@1.6.0-next.6
16
+ - @copilotkit/shared@1.6.0-next.6
17
+
3
18
  ## 1.6.0-next.5
4
19
 
5
20
  ### Patch Changes
@@ -21,7 +21,7 @@ import {
21
21
  } from "./chunk-SKC7AJIV.mjs";
22
22
 
23
23
  // src/hooks/use-coagent.ts
24
- import { useEffect, useRef } from "react";
24
+ import { useCallback, useEffect, useMemo, useRef } from "react";
25
25
  import { parseJson } from "@copilotkit/shared";
26
26
  function useCoAgent(options) {
27
27
  const generalContext = useCopilotContext();
@@ -29,9 +29,6 @@ function useCoAgent(options) {
29
29
  const { addToast } = useToast();
30
30
  const lastLoadedThreadId = useRef();
31
31
  const lastLoadedState = useRef();
32
- const isExternalStateManagement = (options2) => {
33
- return "state" in options2 && "setState" in options2;
34
- };
35
32
  const { name } = options;
36
33
  useEffect(() => {
37
34
  if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === name)) {
@@ -40,44 +37,27 @@ function useCoAgent(options) {
40
37
  addToast({ type: "warning", message });
41
38
  }
42
39
  }, [availableAgents]);
43
- const isInternalStateManagementWithInitial = (options2) => {
44
- return "initialState" in options2;
45
- };
46
40
  const messagesContext = useCopilotMessagesContext();
47
41
  const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
48
42
  const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } = context;
49
43
  const { appendMessage, runChatCompletion } = useCopilotChat();
50
- const getCoagentState = (coagentStates2, name2) => {
51
- var _a;
52
- if (coagentStates2[name2]) {
53
- return coagentStates2[name2];
54
- } else {
55
- return {
56
- name: name2,
57
- state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
58
- configurable: (_a = options.configurable) != null ? _a : {},
59
- running: false,
60
- active: false,
61
- threadId: void 0,
62
- nodeName: void 0,
63
- runId: void 0
64
- };
65
- }
66
- };
67
44
  const runtimeClient = useCopilotRuntimeClient({
68
45
  url: copilotApiConfig.chatApiEndpoint,
69
46
  publicApiKey: copilotApiConfig.publicApiKey,
70
47
  credentials: copilotApiConfig.credentials
71
48
  });
72
- const setState = (newState) => {
73
- let coagentState2 = getCoagentState(coagentStatesRef.current || {}, name);
74
- const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
75
- setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
76
- [name]: __spreadProps(__spreadValues({}, coagentState2), {
77
- state: updatedState
78
- })
79
- }));
80
- };
49
+ const setState = useCallback(
50
+ (newState) => {
51
+ let coagentState = getCoagentState({ coagentStates, name, options });
52
+ const updatedState = typeof newState === "function" ? newState(coagentState.state) : newState;
53
+ setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
54
+ [name]: __spreadProps(__spreadValues({}, coagentState), {
55
+ state: updatedState
56
+ })
57
+ }));
58
+ },
59
+ [coagentStates, name]
60
+ );
81
61
  useEffect(() => {
82
62
  const fetchAgentState = () => __async(this, null, function* () {
83
63
  var _a, _b, _c, _d;
@@ -99,7 +79,6 @@ function useCoAgent(options) {
99
79
  });
100
80
  void fetchAgentState();
101
81
  }, [threadId]);
102
- const coagentState = getCoagentState(coagentStates, name);
103
82
  useEffect(() => {
104
83
  if (isExternalStateManagement(options)) {
105
84
  setState(options.state);
@@ -117,17 +96,20 @@ function useCoAgent(options) {
117
96
  }),
118
97
  [name, context, appendMessage, runChatCompletion]
119
98
  );
120
- return {
121
- name,
122
- nodeName: coagentState.nodeName,
123
- threadId: coagentState.threadId,
124
- running: coagentState.running,
125
- state: coagentState.state,
126
- setState: isExternalStateManagement(options) ? options.setState : setState,
127
- start: () => startAgent(name, context),
128
- stop: () => stopAgent(name, context),
129
- run: runAgentCallback
130
- };
99
+ return useMemo(() => {
100
+ const coagentState = getCoagentState({ coagentStates, name, options });
101
+ return {
102
+ name,
103
+ nodeName: coagentState.nodeName,
104
+ threadId: coagentState.threadId,
105
+ running: coagentState.running,
106
+ state: coagentState.state,
107
+ setState: isExternalStateManagement(options) ? options.setState : setState,
108
+ start: () => startAgent(name, context),
109
+ stop: () => stopAgent(name, context),
110
+ run: runAgentCallback
111
+ };
112
+ }, [name, coagentStates, options, setState, runAgentCallback]);
131
113
  }
132
114
  function startAgent(name, context) {
133
115
  const { setAgentSession } = context;
@@ -183,6 +165,33 @@ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
183
165
  }
184
166
  });
185
167
  }
168
+ var isExternalStateManagement = (options) => {
169
+ return "state" in options && "setState" in options;
170
+ };
171
+ var isInternalStateManagementWithInitial = (options) => {
172
+ return "initialState" in options;
173
+ };
174
+ var getCoagentState = ({
175
+ coagentStates,
176
+ name,
177
+ options
178
+ }) => {
179
+ var _a;
180
+ if (coagentStates[name]) {
181
+ return coagentStates[name];
182
+ } else {
183
+ return {
184
+ name,
185
+ state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
186
+ configurable: (_a = options.configurable) != null ? _a : {},
187
+ running: false,
188
+ active: false,
189
+ threadId: void 0,
190
+ nodeName: void 0,
191
+ runId: void 0
192
+ };
193
+ }
194
+ };
186
195
 
187
196
  export {
188
197
  useCoAgent,
@@ -190,4 +199,4 @@ export {
190
199
  stopAgent,
191
200
  runAgent
192
201
  };
193
- //# sourceMappingURL=chunk-HM7CCAUL.mjs.map
202
+ //# sourceMappingURL=chunk-ADTDNMYG.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-coagent.ts"],"sourcesContent":["/**\n * <Callout type=\"info\">\n * Usage of this hook assumes some additional setup in your application, for more information\n * on that see the CoAgents <span className=\"text-blue-500\">[getting started guide](/coagents/quickstart/langgraph)</span>.\n * </Callout>\n * <Frame className=\"my-12\">\n * <img\n * src=\"/images/coagents/SharedStateCoAgents.gif\"\n * alt=\"CoAgents demonstration\"\n * className=\"w-auto\"\n * />\n * </Frame>\n *\n * This hook is used to integrate an agent into your application. With its use, you can\n * render and update the state of an agent, allowing for a dynamic and interactive experience.\n * We call these shared state experiences agentic copilots, or CoAgents for short.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCoAgent } from \"@copilotkit/react-core\";\n *\n * type AgentState = {\n * count: number;\n * }\n *\n * const agent = useCoAgent<AgentState>({\n * name: \"my-agent\",\n * initialState: {\n * count: 0,\n * },\n * });\n *\n * ```\n *\n * `useCoAgent` returns an object with the following properties:\n *\n * ```tsx\n * const {\n * name, // The name of the agent currently being used.\n * nodeName, // The name of the current LangGraph node.\n * state, // The current state of the agent.\n * setState, // A function to update the state of the agent.\n * running, // A boolean indicating if the agent is currently running.\n * start, // A function to start the agent.\n * stop, // A function to stop the agent.\n * run, // A function to re-run the agent. Takes a HintFunction to inform the agent why it is being re-run.\n * } = agent;\n * ```\n *\n * Finally we can leverage these properties to create reactive experiences with the agent!\n *\n * ```tsx\n * const { state, setState } = useCoAgent<AgentState>({\n * name: \"my-agent\",\n * initialState: {\n * count: 0,\n * },\n * });\n *\n * return (\n * <div>\n * <p>Count: {state.count}</p>\n * <button onClick={() => setState({ count: state.count + 1 })}>Increment</button>\n * </div>\n * );\n * ```\n *\n * This reactivity is bidirectional, meaning that changes to the state from the agent will be reflected in the UI and vice versa.\n *\n * ## Parameters\n * <PropertyReference name=\"options\" type=\"UseCoagentOptions<T>\" required>\n * The options to use when creating the coagent.\n * <PropertyReference name=\"name\" type=\"string\" required>\n * The name of the agent to use.\n * </PropertyReference>\n * <PropertyReference name=\"initialState\" type=\"T | any\">\n * The initial state of the agent.\n * </PropertyReference>\n * <PropertyReference name=\"state\" type=\"T | any\">\n * State to manage externally if you are using this hook with external state management.\n * </PropertyReference>\n * <PropertyReference name=\"setState\" type=\"(newState: T | ((prevState: T | undefined) => T)) => void\">\n * A function to update the state of the agent if you are using this hook with external state management.\n * </PropertyReference>\n * </PropertyReference>\n */\n\nimport { useCallback, useEffect, useMemo, useRef } from \"react\";\nimport {\n CopilotContextParams,\n CopilotMessagesContextParams,\n useCopilotContext,\n useCopilotMessagesContext,\n} from \"../context\";\nimport { CoagentState } from \"../types/coagent-state\";\nimport { useCopilotChat } from \"./use-copilot-chat\";\nimport { Message } from \"@copilotkit/runtime-client-gql\";\nimport { useAsyncCallback } from \"../components/error-boundary/error-utils\";\nimport { useToast } from \"../components/toast/toast-provider\";\nimport { useCopilotRuntimeClient } from \"./use-copilot-runtime-client\";\nimport { parseJson } from \"@copilotkit/shared\";\n\ninterface WithInternalStateManagementAndInitial<T> {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The initial state of the agent.\n */\n initialState: T;\n /**\n * Config to pass to a LangGraph Agent\n */\n configurable?: Record<string, any>;\n}\n\ninterface WithInternalStateManagement {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * Optional initialState with default type any\n */\n initialState?: any;\n /**\n * Config to pass to a LangGraph Agent\n */\n configurable?: Record<string, any>;\n}\n\ninterface WithExternalStateManagement<T> {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The current state of the agent.\n */\n state: T;\n /**\n * A function to update the state of the agent.\n */\n setState: (newState: T | ((prevState: T | undefined) => T)) => void;\n /**\n * Config to pass to a LangGraph Agent\n */\n configurable?: Record<string, any>;\n}\n\ntype UseCoagentOptions<T> =\n | WithInternalStateManagementAndInitial<T>\n | WithInternalStateManagement\n | WithExternalStateManagement<T>;\n\nexport interface UseCoagentReturnType<T> {\n /**\n * The name of the agent being used.\n */\n name: string;\n /**\n * The name of the current LangGraph node.\n */\n nodeName?: string;\n /**\n * The ID of the thread the agent is running in.\n */\n threadId?: string;\n /**\n * A boolean indicating if the agent is currently running.\n */\n running: boolean;\n /**\n * The current state of the agent.\n */\n state: T;\n /**\n * A function to update the state of the agent.\n */\n setState: (newState: T | ((prevState: T | undefined) => T)) => void;\n /**\n * A function to start the agent.\n */\n start: () => void;\n /**\n * A function to stop the agent.\n */\n stop: () => void;\n /**\n * A function to re-run the agent. The hint function can be used to provide a hint to the agent\n * about why it is being re-run again.\n */\n run: (hint?: HintFunction) => Promise<void>;\n}\n\nexport interface HintFunctionParams {\n /**\n * The previous state of the agent.\n */\n previousState: any;\n /**\n * The current state of the agent.\n */\n currentState: any;\n}\n\nexport type HintFunction = (params: HintFunctionParams) => Message | undefined;\n\n/**\n * This hook is used to integrate an agent into your application. With its use, you can\n * render and update the state of the agent, allowing for a dynamic and interactive experience.\n * We call these shared state experiences \"agentic copilots\". To get started using agentic copilots, which\n * we refer to as CoAgents, checkout the documentation at https://docs.copilotkit.ai/coagents/quickstart/langgraph.\n */\nexport function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentReturnType<T> {\n const generalContext = useCopilotContext();\n const { availableAgents } = generalContext;\n const { addToast } = useToast();\n const lastLoadedThreadId = useRef<string>();\n const lastLoadedState = useRef<any>();\n\n const { name } = options;\n useEffect(() => {\n if (availableAgents?.length && !availableAgents.some((a) => a.name === name)) {\n const message = `(useCoAgent): Agent \"${name}\" not found. Make sure the agent exists and is properly configured.`;\n console.warn(message);\n addToast({ type: \"warning\", message });\n }\n }, [availableAgents]);\n\n const messagesContext = useCopilotMessagesContext();\n const context = { ...generalContext, ...messagesContext };\n const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } =\n context;\n const { appendMessage, runChatCompletion } = useCopilotChat();\n\n const runtimeClient = useCopilotRuntimeClient({\n url: copilotApiConfig.chatApiEndpoint,\n publicApiKey: copilotApiConfig.publicApiKey,\n credentials: copilotApiConfig.credentials,\n });\n\n // if we manage state internally, we need to provide a function to set the state\n const setState = useCallback(\n (newState: T | ((prevState: T | undefined) => T)) => {\n // coagentStatesRef.current || {}\n let coagentState: CoagentState = getCoagentState({ coagentStates, name, options });\n const updatedState =\n typeof newState === \"function\" ? (newState as Function)(coagentState.state) : newState;\n\n setCoagentStatesWithRef({\n ...coagentStatesRef.current,\n [name]: {\n ...coagentState,\n state: updatedState,\n },\n });\n },\n [coagentStates, name],\n );\n\n useEffect(() => {\n const fetchAgentState = async () => {\n if (!threadId || threadId === lastLoadedThreadId.current) return;\n\n const result = await runtimeClient.loadAgentState({\n threadId,\n agentName: name,\n });\n\n const newState = result.data?.loadAgentState?.state;\n if (newState === lastLoadedState.current) return;\n\n if (result.data?.loadAgentState?.threadExists && newState && newState != \"{}\") {\n lastLoadedState.current = newState;\n lastLoadedThreadId.current = threadId;\n const fetchedState = parseJson(newState, {});\n isExternalStateManagement(options)\n ? options.setState(fetchedState)\n : setState(fetchedState);\n }\n };\n void fetchAgentState();\n }, [threadId]);\n\n // Sync internal state with external state if state management is external\n useEffect(() => {\n if (isExternalStateManagement(options)) {\n setState(options.state);\n } else if (coagentStates[name] === undefined) {\n setState(options.initialState === undefined ? {} : options.initialState);\n }\n }, [\n isExternalStateManagement(options) ? JSON.stringify(options.state) : undefined,\n // reset initialstate on reset\n coagentStates[name] === undefined,\n ]);\n\n const runAgentCallback = useAsyncCallback(\n async (hint?: HintFunction) => {\n await runAgent(name, context, appendMessage, runChatCompletion, hint);\n },\n [name, context, appendMessage, runChatCompletion],\n );\n\n // Return the state and setState function\n return useMemo(() => {\n const coagentState = getCoagentState({ coagentStates, name, options });\n return {\n name,\n nodeName: coagentState.nodeName,\n threadId: coagentState.threadId,\n running: coagentState.running,\n state: coagentState.state,\n setState: isExternalStateManagement(options) ? options.setState : setState,\n start: () => startAgent(name, context),\n stop: () => stopAgent(name, context),\n run: runAgentCallback,\n };\n }, [name, coagentStates, options, setState, runAgentCallback]);\n}\n\nexport function startAgent(name: string, context: CopilotContextParams) {\n const { setAgentSession } = context;\n setAgentSession({\n agentName: name,\n });\n}\n\nexport function stopAgent(name: string, context: CopilotContextParams) {\n const { agentSession, setAgentSession } = context;\n if (agentSession && agentSession.agentName === name) {\n setAgentSession(null);\n context.setCoagentStates((prevAgentStates) => {\n return {\n ...prevAgentStates,\n [name]: {\n ...prevAgentStates[name],\n running: false,\n active: false,\n threadId: undefined,\n nodeName: undefined,\n runId: undefined,\n },\n };\n });\n } else {\n console.warn(`No agent session found for ${name}`);\n }\n}\n\nexport async function runAgent(\n name: string,\n context: CopilotContextParams & CopilotMessagesContextParams,\n appendMessage: (message: Message) => Promise<void>,\n runChatCompletion: () => Promise<Message[]>,\n hint?: HintFunction,\n) {\n const { agentSession, setAgentSession } = context;\n if (!agentSession || agentSession.agentName !== name) {\n setAgentSession({\n agentName: name,\n });\n }\n\n let previousState: any = null;\n for (let i = context.messages.length - 1; i >= 0; i--) {\n const message = context.messages[i];\n if (message.isAgentStateMessage() && message.agentName === name) {\n previousState = message.state;\n }\n }\n\n let state = context.coagentStatesRef.current?.[name]?.state || {};\n\n if (hint) {\n const hintMessage = hint({ previousState, currentState: state });\n if (hintMessage) {\n await appendMessage(hintMessage);\n } else {\n await runChatCompletion();\n }\n } else {\n await runChatCompletion();\n }\n}\n\nconst isExternalStateManagement = <T>(\n options: UseCoagentOptions<T>,\n): options is WithExternalStateManagement<T> => {\n return \"state\" in options && \"setState\" in options;\n};\n\nconst isInternalStateManagementWithInitial = <T>(\n options: UseCoagentOptions<T>,\n): options is WithInternalStateManagementAndInitial<T> => {\n return \"initialState\" in options;\n};\n\nconst getCoagentState = <T>({\n coagentStates,\n name,\n options,\n}: {\n coagentStates: Record<string, CoagentState>;\n name: string;\n options: UseCoagentOptions<T>;\n}) => {\n if (coagentStates[name]) {\n return coagentStates[name];\n } else {\n return {\n name,\n state: isInternalStateManagementWithInitial<T>(options) ? options.initialState : {},\n configurable: options.configurable ?? {},\n running: false,\n active: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;AAAA,MAC1B;AAAA,IACF;AAEA,QAAI,UAAQ,mBAAQ,iBAAiB,YAAzB,mBAAmC,UAAnC,mBAA0C,UAAS,CAAC;AAEhE,QAAI,MAAM;AACR,YAAM,cAAc,KAAK,EAAE,eAAe,cAAc,MAAM,CAAC;AAC/D,UAAI,aAAa;AACf,cAAM,cAAc,WAAW;AAAA,MACjC,OAAO;AACL,cAAM,kBAAkB;AAAA,MAC1B;AAAA,IACF,OAAO;AACL,YAAM,kBAAkB;AAAA,IAC1B;AAAA,EACF;AAAA;AAEA,IAAM,4BAA4B,CAChC,YAC8C;AAC9C,SAAO,WAAW,WAAW,cAAc;AAC7C;AAEA,IAAM,uCAAuC,CAC3C,YACwD;AACxD,SAAO,kBAAkB;AAC3B;AAEA,IAAM,kBAAkB,CAAI;AAAA,EAC1B;AAAA,EACA;AAAA,EACA;AACF,MAIM;AA3ZN;AA4ZE,MAAI,cAAc,IAAI,GAAG;AACvB,WAAO,cAAc,IAAI;AAAA,EAC3B,OAAO;AACL,WAAO;AAAA,MACL;AAAA,MACA,OAAO,qCAAwC,OAAO,IAAI,QAAQ,eAAe,CAAC;AAAA,MAClF,eAAc,aAAQ,iBAAR,YAAwB,CAAC;AAAA,MACvC,SAAS;AAAA,MACT,QAAQ;AAAA,MACR,UAAU;AAAA,MACV,UAAU;AAAA,MACV,OAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
@@ -1268,9 +1268,6 @@ function useCoAgent(options) {
1268
1268
  const { addToast } = useToast();
1269
1269
  const lastLoadedThreadId = (0, import_react13.useRef)();
1270
1270
  const lastLoadedState = (0, import_react13.useRef)();
1271
- const isExternalStateManagement = (options2) => {
1272
- return "state" in options2 && "setState" in options2;
1273
- };
1274
1271
  const { name } = options;
1275
1272
  (0, import_react13.useEffect)(() => {
1276
1273
  if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === name)) {
@@ -1279,44 +1276,27 @@ function useCoAgent(options) {
1279
1276
  addToast({ type: "warning", message });
1280
1277
  }
1281
1278
  }, [availableAgents]);
1282
- const isInternalStateManagementWithInitial = (options2) => {
1283
- return "initialState" in options2;
1284
- };
1285
1279
  const messagesContext = useCopilotMessagesContext();
1286
1280
  const context = __spreadValues(__spreadValues({}, generalContext), messagesContext);
1287
1281
  const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } = context;
1288
1282
  const { appendMessage, runChatCompletion } = useCopilotChat();
1289
- const getCoagentState = (coagentStates2, name2) => {
1290
- var _a;
1291
- if (coagentStates2[name2]) {
1292
- return coagentStates2[name2];
1293
- } else {
1294
- return {
1295
- name: name2,
1296
- state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
1297
- configurable: (_a = options.configurable) != null ? _a : {},
1298
- running: false,
1299
- active: false,
1300
- threadId: void 0,
1301
- nodeName: void 0,
1302
- runId: void 0
1303
- };
1304
- }
1305
- };
1306
1283
  const runtimeClient = useCopilotRuntimeClient({
1307
1284
  url: copilotApiConfig.chatApiEndpoint,
1308
1285
  publicApiKey: copilotApiConfig.publicApiKey,
1309
1286
  credentials: copilotApiConfig.credentials
1310
1287
  });
1311
- const setState = (newState) => {
1312
- let coagentState2 = getCoagentState(coagentStatesRef.current || {}, name);
1313
- const updatedState = typeof newState === "function" ? newState(coagentState2.state) : newState;
1314
- setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
1315
- [name]: __spreadProps(__spreadValues({}, coagentState2), {
1316
- state: updatedState
1317
- })
1318
- }));
1319
- };
1288
+ const setState = (0, import_react13.useCallback)(
1289
+ (newState) => {
1290
+ let coagentState = getCoagentState({ coagentStates, name, options });
1291
+ const updatedState = typeof newState === "function" ? newState(coagentState.state) : newState;
1292
+ setCoagentStatesWithRef(__spreadProps(__spreadValues({}, coagentStatesRef.current), {
1293
+ [name]: __spreadProps(__spreadValues({}, coagentState), {
1294
+ state: updatedState
1295
+ })
1296
+ }));
1297
+ },
1298
+ [coagentStates, name]
1299
+ );
1320
1300
  (0, import_react13.useEffect)(() => {
1321
1301
  const fetchAgentState = () => __async(this, null, function* () {
1322
1302
  var _a, _b, _c, _d;
@@ -1338,7 +1318,6 @@ function useCoAgent(options) {
1338
1318
  });
1339
1319
  void fetchAgentState();
1340
1320
  }, [threadId]);
1341
- const coagentState = getCoagentState(coagentStates, name);
1342
1321
  (0, import_react13.useEffect)(() => {
1343
1322
  if (isExternalStateManagement(options)) {
1344
1323
  setState(options.state);
@@ -1356,17 +1335,20 @@ function useCoAgent(options) {
1356
1335
  }),
1357
1336
  [name, context, appendMessage, runChatCompletion]
1358
1337
  );
1359
- return {
1360
- name,
1361
- nodeName: coagentState.nodeName,
1362
- threadId: coagentState.threadId,
1363
- running: coagentState.running,
1364
- state: coagentState.state,
1365
- setState: isExternalStateManagement(options) ? options.setState : setState,
1366
- start: () => startAgent(name, context),
1367
- stop: () => stopAgent(name, context),
1368
- run: runAgentCallback
1369
- };
1338
+ return (0, import_react13.useMemo)(() => {
1339
+ const coagentState = getCoagentState({ coagentStates, name, options });
1340
+ return {
1341
+ name,
1342
+ nodeName: coagentState.nodeName,
1343
+ threadId: coagentState.threadId,
1344
+ running: coagentState.running,
1345
+ state: coagentState.state,
1346
+ setState: isExternalStateManagement(options) ? options.setState : setState,
1347
+ start: () => startAgent(name, context),
1348
+ stop: () => stopAgent(name, context),
1349
+ run: runAgentCallback
1350
+ };
1351
+ }, [name, coagentStates, options, setState, runAgentCallback]);
1370
1352
  }
1371
1353
  function startAgent(name, context) {
1372
1354
  const { setAgentSession } = context;
@@ -1422,6 +1404,33 @@ function runAgent(name, context, appendMessage, runChatCompletion, hint) {
1422
1404
  }
1423
1405
  });
1424
1406
  }
1407
+ var isExternalStateManagement = (options) => {
1408
+ return "state" in options && "setState" in options;
1409
+ };
1410
+ var isInternalStateManagementWithInitial = (options) => {
1411
+ return "initialState" in options;
1412
+ };
1413
+ var getCoagentState = ({
1414
+ coagentStates,
1415
+ name,
1416
+ options
1417
+ }) => {
1418
+ var _a;
1419
+ if (coagentStates[name]) {
1420
+ return coagentStates[name];
1421
+ } else {
1422
+ return {
1423
+ name,
1424
+ state: isInternalStateManagementWithInitial(options) ? options.initialState : {},
1425
+ configurable: (_a = options.configurable) != null ? _a : {},
1426
+ running: false,
1427
+ active: false,
1428
+ threadId: void 0,
1429
+ nodeName: void 0,
1430
+ runId: void 0
1431
+ };
1432
+ }
1433
+ };
1425
1434
 
1426
1435
  // src/hooks/use-copilot-authenticated-action.ts
1427
1436
  var import_react14 = require("react");