@copilotkit/react-core 1.4.2-tyler-reset-chat.2 → 1.4.3-pre.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.
Files changed (87) hide show
  1. package/CHANGELOG.md +10 -17
  2. package/dist/{chunk-USL3EHJB.mjs → chunk-AG7FH7OD.mjs} +2 -2
  3. package/dist/{chunk-6QKA3SNN.mjs → chunk-DHGXL5PC.mjs} +16 -12
  4. package/dist/{chunk-6QKA3SNN.mjs.map → chunk-DHGXL5PC.mjs.map} +1 -1
  5. package/dist/{chunk-24AHWFXB.mjs → chunk-EUIBVFV6.mjs} +20 -41
  6. package/dist/chunk-EUIBVFV6.mjs.map +1 -0
  7. package/dist/{chunk-3AYELZJS.mjs → chunk-JHEAUB3Z.mjs} +2 -2
  8. package/dist/{chunk-YTYNTCQL.mjs → chunk-O22KGHOQ.mjs} +21 -37
  9. package/dist/chunk-O22KGHOQ.mjs.map +1 -0
  10. package/dist/{chunk-JR55I3FL.mjs → chunk-OT67R4NB.mjs} +2 -2
  11. package/dist/{chunk-XUPO37VH.mjs → chunk-UOVONDR6.mjs} +2 -2
  12. package/dist/{chunk-UYKBEKKH.mjs → chunk-XBVKTDXP.mjs} +16 -41
  13. package/dist/chunk-XBVKTDXP.mjs.map +1 -0
  14. package/dist/{chunk-2JP64U3A.mjs → chunk-XQFVXX6R.mjs} +5 -10
  15. package/dist/{chunk-2JP64U3A.mjs.map → chunk-XQFVXX6R.mjs.map} +1 -1
  16. package/dist/{chunk-3R4J2TPH.mjs → chunk-Y7MI4PBB.mjs} +2 -2
  17. package/dist/components/copilot-provider/copilotkit.js +18 -19
  18. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  19. package/dist/components/copilot-provider/copilotkit.mjs +2 -2
  20. package/dist/components/copilot-provider/index.js +18 -19
  21. package/dist/components/copilot-provider/index.js.map +1 -1
  22. package/dist/components/copilot-provider/index.mjs +2 -2
  23. package/dist/components/index.js +18 -19
  24. package/dist/components/index.js.map +1 -1
  25. package/dist/components/index.mjs +2 -2
  26. package/dist/context/copilot-context.d.ts +2 -6
  27. package/dist/context/copilot-context.js +4 -9
  28. package/dist/context/copilot-context.js.map +1 -1
  29. package/dist/context/copilot-context.mjs +1 -1
  30. package/dist/context/index.js +4 -9
  31. package/dist/context/index.js.map +1 -1
  32. package/dist/context/index.mjs +1 -1
  33. package/dist/hooks/index.d.ts +1 -2
  34. package/dist/hooks/index.js +52 -122
  35. package/dist/hooks/index.js.map +1 -1
  36. package/dist/hooks/index.mjs +16 -22
  37. package/dist/hooks/use-chat.d.ts +6 -22
  38. package/dist/hooks/use-chat.js +19 -40
  39. package/dist/hooks/use-chat.js.map +1 -1
  40. package/dist/hooks/use-chat.mjs +1 -1
  41. package/dist/hooks/use-coagent-state-render.js +4 -9
  42. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  43. package/dist/hooks/use-coagent-state-render.mjs +2 -2
  44. package/dist/hooks/use-coagent.d.ts +1 -14
  45. package/dist/hooks/use-coagent.js +52 -122
  46. package/dist/hooks/use-coagent.js.map +1 -1
  47. package/dist/hooks/use-coagent.mjs +6 -12
  48. package/dist/hooks/use-copilot-action.js +4 -9
  49. package/dist/hooks/use-copilot-action.js.map +1 -1
  50. package/dist/hooks/use-copilot-action.mjs +2 -2
  51. package/dist/hooks/use-copilot-chat.d.ts +1 -1
  52. package/dist/hooks/use-copilot-chat.js +35 -86
  53. package/dist/hooks/use-copilot-chat.js.map +1 -1
  54. package/dist/hooks/use-copilot-chat.mjs +5 -5
  55. package/dist/hooks/use-copilot-readable.js +4 -9
  56. package/dist/hooks/use-copilot-readable.js.map +1 -1
  57. package/dist/hooks/use-copilot-readable.mjs +2 -2
  58. package/dist/hooks/use-make-copilot-document-readable.js +4 -9
  59. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  60. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  61. package/dist/index.d.ts +1 -1
  62. package/dist/index.js +66 -132
  63. package/dist/index.js.map +1 -1
  64. package/dist/index.mjs +17 -23
  65. package/dist/lib/copilot-task.js.map +1 -1
  66. package/dist/lib/copilot-task.mjs +3 -3
  67. package/dist/lib/index.js.map +1 -1
  68. package/dist/lib/index.mjs +3 -3
  69. package/dist/utils/extract.js.map +1 -1
  70. package/dist/utils/extract.mjs +2 -2
  71. package/dist/utils/index.js.map +1 -1
  72. package/dist/utils/index.mjs +2 -2
  73. package/package.json +5 -5
  74. package/src/components/copilot-provider/copilotkit.tsx +18 -10
  75. package/src/context/copilot-context.tsx +9 -22
  76. package/src/hooks/index.ts +1 -1
  77. package/src/hooks/use-chat.ts +28 -62
  78. package/src/hooks/use-coagent.ts +24 -38
  79. package/src/hooks/use-copilot-chat.ts +14 -41
  80. package/dist/chunk-24AHWFXB.mjs.map +0 -1
  81. package/dist/chunk-UYKBEKKH.mjs.map +0 -1
  82. package/dist/chunk-YTYNTCQL.mjs.map +0 -1
  83. /package/dist/{chunk-USL3EHJB.mjs.map → chunk-AG7FH7OD.mjs.map} +0 -0
  84. /package/dist/{chunk-3AYELZJS.mjs.map → chunk-JHEAUB3Z.mjs.map} +0 -0
  85. /package/dist/{chunk-JR55I3FL.mjs.map → chunk-OT67R4NB.mjs.map} +0 -0
  86. /package/dist/{chunk-XUPO37VH.mjs.map → chunk-UOVONDR6.mjs.map} +0 -0
  87. /package/dist/{chunk-3R4J2TPH.mjs.map → chunk-Y7MI4PBB.mjs.map} +0 -0
@@ -254,6 +254,22 @@ export function CopilotKitInternal({ children, ...props }: CopilotKitProps) {
254
254
  };
255
255
 
256
256
  const [coagentStates, setCoagentStates] = useState<Record<string, CoagentState>>({});
257
+ const coagentStatesRef = useRef<Record<string, CoagentState>>({});
258
+ const setCoagentStatesWithRef = useCallback(
259
+ (
260
+ value:
261
+ | Record<string, CoagentState>
262
+ | ((prev: Record<string, CoagentState>) => Record<string, CoagentState>),
263
+ ) => {
264
+ const newValue = typeof value === "function" ? value(coagentStatesRef.current) : value;
265
+ coagentStatesRef.current = newValue;
266
+ setCoagentStates((prev) => {
267
+ return newValue;
268
+ });
269
+ },
270
+ [],
271
+ );
272
+
257
273
  let initialAgentSession: AgentSession | null = null;
258
274
  if (props.agent) {
259
275
  initialAgentSession = {
@@ -262,10 +278,6 @@ export function CopilotKitInternal({ children, ...props }: CopilotKitProps) {
262
278
  }
263
279
 
264
280
  const [agentSession, setAgentSession] = useState<AgentSession | null>(initialAgentSession);
265
- const [threadId, setThreadId] = useState<string | null>(null);
266
- const [runId, setRunId] = useState<string | null>(null);
267
-
268
- const chatAbortControllerRef = useRef<AbortController | null>(null);
269
281
 
270
282
  const showDevConsole = props.showDevConsole === undefined ? "auto" : props.showDevConsole;
271
283
 
@@ -297,15 +309,11 @@ export function CopilotKitInternal({ children, ...props }: CopilotKitProps) {
297
309
  showDevConsole,
298
310
  coagentStates,
299
311
  setCoagentStates,
312
+ coagentStatesRef,
313
+ setCoagentStatesWithRef,
300
314
  agentSession,
301
315
  setAgentSession,
302
316
  runtimeClient,
303
- agentLock: props.agent || null,
304
- threadId,
305
- setThreadId,
306
- runId,
307
- setRunId,
308
- chatAbortControllerRef,
309
317
  }}
310
318
  >
311
319
  <CopilotMessages>{children}</CopilotMessages>
@@ -132,21 +132,16 @@ export interface CopilotContextParams {
132
132
  // agents
133
133
  coagentStates: Record<string, CoagentState>;
134
134
  setCoagentStates: React.Dispatch<React.SetStateAction<Record<string, CoagentState>>>;
135
+ coagentStatesRef: React.RefObject<Record<string, CoagentState>>;
136
+ setCoagentStatesWithRef: (
137
+ value:
138
+ | Record<string, CoagentState>
139
+ | ((prev: Record<string, CoagentState>) => Record<string, CoagentState>),
140
+ ) => void;
141
+
135
142
  agentSession: AgentSession | null;
136
143
  setAgentSession: React.Dispatch<React.SetStateAction<AgentSession | null>>;
137
144
 
138
- agentLock: string | null;
139
-
140
- threadId: string | null;
141
- setThreadId: React.Dispatch<React.SetStateAction<string | null>>;
142
-
143
- runId: string | null;
144
- setRunId: React.Dispatch<React.SetStateAction<string | null>>;
145
-
146
- // The chat abort controller can be used to stop generation globally,
147
- // i.e. when using `stop()` from `useChat`
148
- chatAbortControllerRef: React.MutableRefObject<AbortController | null>;
149
-
150
145
  // runtime
151
146
  runtimeClient: CopilotRuntimeClient;
152
147
  }
@@ -198,19 +193,11 @@ const emptyCopilotContext: CopilotContextParams = {
198
193
  showDevConsole: "auto",
199
194
  coagentStates: {},
200
195
  setCoagentStates: () => {},
196
+ coagentStatesRef: { current: {} },
197
+ setCoagentStatesWithRef: () => {},
201
198
 
202
199
  agentSession: null,
203
200
  setAgentSession: () => {},
204
-
205
- agentLock: null,
206
-
207
- threadId: null,
208
- setThreadId: () => {},
209
-
210
- runId: null,
211
- setRunId: () => {},
212
-
213
- chatAbortControllerRef: { current: null },
214
201
  };
215
202
 
216
203
  export const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);
@@ -7,5 +7,5 @@ export { useCoAgentStateRender } from "./use-coagent-state-render";
7
7
  export { useMakeCopilotDocumentReadable } from "./use-make-copilot-document-readable";
8
8
  export { type UseChatHelpers } from "./use-chat";
9
9
  export { useCopilotReadable } from "./use-copilot-readable";
10
- export { useCoAgent, type HintFunction, runAgent, startAgent, stopAgent } from "./use-coagent";
10
+ export { useCoAgent } from "./use-coagent";
11
11
  export { useCopilotRuntimeClient } from "./use-copilot-runtime-client";
@@ -81,12 +81,12 @@ export type UseChatOptions = {
81
81
  /**
82
82
  * The current list of coagent states.
83
83
  */
84
- coagentStates: Record<string, CoagentState>;
84
+ coagentStatesRef: React.RefObject<Record<string, CoagentState>>;
85
85
 
86
86
  /**
87
87
  * setState-powered method to update the agent states
88
88
  */
89
- setCoagentStates: React.Dispatch<React.SetStateAction<Record<string, CoagentState>>>;
89
+ setCoagentStatesWithRef: React.Dispatch<React.SetStateAction<Record<string, CoagentState>>>;
90
90
 
91
91
  /**
92
92
  * The current agent session.
@@ -97,27 +97,6 @@ export type UseChatOptions = {
97
97
  * setState-powered method to update the agent session
98
98
  */
99
99
  setAgentSession: React.Dispatch<React.SetStateAction<AgentSession | null>>;
100
-
101
- /**
102
- * The current thread ID.
103
- */
104
- threadId: string | null;
105
- /**
106
- * set the current thread ID
107
- */
108
- setThreadId: (threadId: string | null) => void;
109
- /**
110
- * The current run ID.
111
- */
112
- runId: string | null;
113
- /**
114
- * set the current run ID
115
- */
116
- setRunId: (runId: string | null) => void;
117
- /**
118
- * The global chat abort controller.
119
- */
120
- chatAbortControllerRef: React.MutableRefObject<AbortController | null>;
121
100
  };
122
101
 
123
102
  export type UseChatHelpers = {
@@ -137,6 +116,11 @@ export type UseChatHelpers = {
137
116
  * Abort the current request immediately, keep the generated tokens if any.
138
117
  */
139
118
  stop: () => void;
119
+
120
+ /**
121
+ * Run the chat completion.
122
+ */
123
+ runChatCompletion: () => Promise<Message[]>;
140
124
  };
141
125
 
142
126
  export function useChat(options: UseChatOptions): UseChatHelpers {
@@ -151,29 +135,23 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
151
135
  actions,
152
136
  onFunctionCall,
153
137
  onCoAgentStateRender,
154
- setCoagentStates,
155
- coagentStates,
138
+ setCoagentStatesWithRef,
139
+ coagentStatesRef,
156
140
  agentSession,
157
141
  setAgentSession,
158
- threadId,
159
- setThreadId,
160
- runId,
161
- setRunId,
162
- chatAbortControllerRef,
163
142
  } = options;
143
+
144
+ const abortControllerRef = useRef<AbortController>();
145
+ const threadIdRef = useRef<string | null>(null);
146
+ const runIdRef = useRef<string | null>(null);
164
147
  const { addGraphQLErrorsToast } = useToast();
148
+
165
149
  const runChatCompletionRef = useRef<(previousMessages: Message[]) => Promise<Message[]>>();
166
- // We need to keep a ref of coagent states because of renderAndWait - making sure
150
+ // We need to keep a ref of coagent states and session because of renderAndWait - making sure
167
151
  // the latest state is sent to the API
168
152
  // This is a workaround and needs to be addressed in the future
169
- const coagentStatesRef = useRef<Record<string, CoagentState>>(coagentStates);
170
- coagentStatesRef.current = coagentStates;
171
153
  const agentSessionRef = useRef<AgentSession | null>(agentSession);
172
154
  agentSessionRef.current = agentSession;
173
- const threadIdRef = useRef<string | null>(threadId);
174
- threadIdRef.current = threadId;
175
- const runIdRef = useRef<string | null>(runId);
176
- runIdRef.current = runId;
177
155
 
178
156
  const publicApiKey = copilotConfig.publicApiKey;
179
157
 
@@ -200,8 +178,8 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
200
178
  role: Role.Assistant,
201
179
  }),
202
180
  ];
203
-
204
- chatAbortControllerRef.current = new AbortController();
181
+ const abortController = new AbortController();
182
+ abortControllerRef.current = abortController;
205
183
 
206
184
  setMessages([...previousMessages, ...newMessages]);
207
185
 
@@ -266,13 +244,13 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
266
244
  agentSession: agentSessionRef.current,
267
245
  }
268
246
  : {}),
269
- agentStates: Object.values(coagentStatesRef.current).map((state) => ({
247
+ agentStates: Object.values(coagentStatesRef.current!).map((state) => ({
270
248
  agentName: state.name,
271
249
  state: JSON.stringify(state.state),
272
250
  })),
273
251
  },
274
252
  properties: copilotConfig.properties,
275
- signal: chatAbortControllerRef.current?.signal,
253
+ signal: abortControllerRef.current?.signal,
276
254
  }),
277
255
  );
278
256
 
@@ -298,9 +276,6 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
298
276
  }
299
277
 
300
278
  if (done) {
301
- if (chatAbortControllerRef.current.signal.aborted) {
302
- return []
303
- }
304
279
  break;
305
280
  }
306
281
 
@@ -311,9 +286,6 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
311
286
  threadIdRef.current = value.generateCopilotResponse.threadId || null;
312
287
  runIdRef.current = value.generateCopilotResponse.runId || null;
313
288
 
314
- setThreadId(threadIdRef.current);
315
- setRunId(runIdRef.current);
316
-
317
289
  const messages = convertGqlOutputToMessages(
318
290
  filterAdjacentAgentStateMessages(value.generateCopilotResponse.messages),
319
291
  );
@@ -365,19 +337,12 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
365
337
  followUp = action.followUp;
366
338
  }
367
339
 
368
- const result = await Promise.race([
369
- onFunctionCall({
370
- messages: previousMessages,
371
- name: message.name,
372
- args: message.arguments,
373
- }),
374
- new Promise((_, reject) => chatAbortControllerRef.current?.signal.addEventListener('abort', () => reject(new Error('Operation was aborted'))))
375
- ])
376
- if (chatAbortControllerRef.current.signal.aborted){
377
- actionResults[message.id] = "";
378
- } else {
379
- actionResults[message.id] = result;
380
- }
340
+ const result = await onFunctionCall({
341
+ messages: previousMessages,
342
+ name: message.name,
343
+ args: message.arguments,
344
+ });
345
+ actionResults[message.id] = result;
381
346
  } catch (e) {
382
347
  actionResults[message.id] = `Failed to execute action ${message.name}`;
383
348
  console.error(`Failed to execute action ${message.name}: ${e}`);
@@ -418,7 +383,7 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
418
383
  .find((message) => message.isAgentStateMessage());
419
384
 
420
385
  if (lastAgentStateMessage) {
421
- setCoagentStates((prevAgentStates) => ({
386
+ setCoagentStatesWithRef((prevAgentStates) => ({
422
387
  ...prevAgentStates,
423
388
  [lastAgentStateMessage.agentName]: {
424
389
  name: lastAgentStateMessage.agentName,
@@ -504,12 +469,13 @@ export function useChat(options: UseChatOptions): UseChatHelpers {
504
469
  };
505
470
 
506
471
  const stop = (): void => {
507
- chatAbortControllerRef.current?.abort("Stop was called");
472
+ abortControllerRef.current?.abort();
508
473
  };
509
474
 
510
475
  return {
511
476
  append,
512
477
  reload,
513
478
  stop,
479
+ runChatCompletion: () => runChatCompletionRef.current!(messages),
514
480
  };
515
481
  }
@@ -97,7 +97,8 @@ import {
97
97
  } from "../context";
98
98
  import { CoagentState } from "../types/coagent-state";
99
99
  import { useCopilotChat } from "./use-copilot-chat";
100
- import { AgentStateMessage, Message, Role, TextMessage } from "@copilotkit/runtime-client-gql";
100
+ import { Message } from "@copilotkit/runtime-client-gql";
101
+ import { flushSync } from "react-dom";
101
102
 
102
103
  interface WithInternalStateManagementAndInitial<T> {
103
104
  /**
@@ -218,8 +219,8 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
218
219
  const generalContext = useCopilotContext();
219
220
  const messagesContext = useCopilotMessagesContext();
220
221
  const context = { ...generalContext, ...messagesContext };
221
- const { coagentStates, setCoagentStates } = context;
222
- const { appendMessage } = useCopilotChat();
222
+ const { coagentStates, coagentStatesRef, setCoagentStatesWithRef } = context;
223
+ const { appendMessage, runChatCompletion } = useCopilotChat();
223
224
 
224
225
  const getCoagentState = (coagentStates: Record<string, CoagentState>, name: string) => {
225
226
  if (coagentStates[name]) {
@@ -239,19 +240,16 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
239
240
 
240
241
  // if we manage state internally, we need to provide a function to set the state
241
242
  const setState = (newState: T | ((prevState: T | undefined) => T)) => {
242
- setCoagentStates((prevAgentStates) => {
243
- let coagentState: CoagentState = getCoagentState(prevAgentStates, name);
243
+ let coagentState: CoagentState = getCoagentState(coagentStatesRef.current || {}, name);
244
+ const updatedState =
245
+ typeof newState === "function" ? (newState as Function)(coagentState.state) : newState;
244
246
 
245
- const updatedState =
246
- typeof newState === "function" ? (newState as Function)(coagentState.state) : newState;
247
-
248
- return {
249
- ...prevAgentStates,
250
- [name]: {
251
- ...coagentState,
252
- state: updatedState,
253
- },
254
- };
247
+ setCoagentStatesWithRef({
248
+ ...coagentStatesRef.current,
249
+ [name]: {
250
+ ...coagentState,
251
+ state: updatedState,
252
+ },
255
253
  });
256
254
  };
257
255
 
@@ -266,11 +264,7 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
266
264
  } else if (coagentStates[name] === undefined) {
267
265
  setState(options.initialState === undefined ? {} : options.initialState);
268
266
  }
269
- }, [
270
- isExternalStateManagement(options) ? JSON.stringify(options.state) : undefined,
271
- // reset initialstate on reset
272
- coagentStates[name] === undefined,
273
- ]);
267
+ }, [isExternalStateManagement(options) ? JSON.stringify(options.state) : undefined]);
274
268
 
275
269
  // Return the state and setState function
276
270
  return {
@@ -286,44 +280,32 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
286
280
  stopAgent(name, context);
287
281
  },
288
282
  run: (hint?: HintFunction) => {
289
- return runAgent(name, context, appendMessage, hint);
283
+ return runAgent(name, context, appendMessage, runChatCompletion, hint);
290
284
  },
291
285
  };
292
286
  }
293
287
 
294
- export function startAgent(name: string, context: CopilotContextParams) {
288
+ function startAgent(name: string, context: CopilotContextParams) {
295
289
  const { setAgentSession } = context;
296
290
  setAgentSession({
297
291
  agentName: name,
298
292
  });
299
293
  }
300
294
 
301
- export function stopAgent(name: string, context: CopilotContextParams) {
295
+ function stopAgent(name: string, context: CopilotContextParams) {
302
296
  const { agentSession, setAgentSession } = context;
303
297
  if (agentSession && agentSession.agentName === name) {
304
298
  setAgentSession(null);
305
- context.setCoagentStates((prevAgentStates) => {
306
- return {
307
- ...prevAgentStates,
308
- [name]: {
309
- ...prevAgentStates[name],
310
- running: false,
311
- active: false,
312
- threadId: undefined,
313
- nodeName: undefined,
314
- runId: undefined,
315
- },
316
- };
317
- });
318
299
  } else {
319
300
  console.warn(`No agent session found for ${name}`);
320
301
  }
321
302
  }
322
303
 
323
- export async function runAgent(
304
+ async function runAgent(
324
305
  name: string,
325
306
  context: CopilotContextParams & CopilotMessagesContextParams,
326
307
  appendMessage: (message: Message) => Promise<void>,
308
+ runChatCompletion: () => Promise<Message[]>,
327
309
  hint?: HintFunction,
328
310
  ) {
329
311
  const { agentSession, setAgentSession } = context;
@@ -341,12 +323,16 @@ export async function runAgent(
341
323
  }
342
324
  }
343
325
 
344
- let state = context.coagentStates?.[name]?.state || {};
326
+ let state = context.coagentStatesRef.current?.[name]?.state || {};
345
327
 
346
328
  if (hint) {
347
329
  const hintMessage = hint({ previousState, currentState: state });
348
330
  if (hintMessage) {
349
331
  await appendMessage(hintMessage);
332
+ } else {
333
+ await runChatCompletion();
350
334
  }
335
+ } else {
336
+ await runChatCompletion();
351
337
  }
352
338
  }
@@ -39,7 +39,7 @@
39
39
  * ```
40
40
  */
41
41
  import { useRef, useEffect, useCallback } from "react";
42
- import { AgentSession, useCopilotContext } from "../context/copilot-context";
42
+ import { useCopilotContext } from "../context/copilot-context";
43
43
  import { Message, Role, TextMessage } from "@copilotkit/runtime-client-gql";
44
44
  import { SystemMessageFunction } from "../types";
45
45
  import { useChat } from "./use-chat";
@@ -78,8 +78,8 @@ export interface UseCopilotChatReturn {
78
78
  deleteMessage: (messageId: string) => void;
79
79
  reloadMessages: () => Promise<void>;
80
80
  stopGeneration: () => void;
81
- reset: () => void;
82
81
  isLoading: boolean;
82
+ runChatCompletion: () => Promise<Message[]>;
83
83
  }
84
84
 
85
85
  export function useCopilotChat({
@@ -94,18 +94,11 @@ export function useCopilotChat({
94
94
  setIsLoading,
95
95
  chatInstructions,
96
96
  actions,
97
-
98
- coagentStates,
99
- setCoagentStates,
97
+ coagentStatesRef,
98
+ setCoagentStatesWithRef,
100
99
  coAgentStateRenders,
101
100
  agentSession,
102
101
  setAgentSession,
103
- agentLock,
104
- threadId,
105
- setThreadId,
106
- runId,
107
- setRunId,
108
- chatAbortControllerRef,
109
102
  } = useCopilotContext();
110
103
  const { messages, setMessages } = useCopilotMessagesContext();
111
104
 
@@ -148,7 +141,7 @@ export function useCopilotChat({
148
141
  [coAgentStateRenders],
149
142
  );
150
143
 
151
- const { append, reload, stop } = useChat({
144
+ const { append, reload, stop, runChatCompletion } = useChat({
152
145
  ...options,
153
146
  actions: Object.values(actions),
154
147
  copilotConfig: copilotApiConfig,
@@ -160,20 +153,15 @@ export function useCopilotChat({
160
153
  makeSystemMessageCallback,
161
154
  isLoading,
162
155
  setIsLoading,
163
- coagentStates,
164
- setCoagentStates,
156
+ coagentStatesRef,
157
+ setCoagentStatesWithRef,
165
158
  agentSession,
166
159
  setAgentSession,
167
- threadId,
168
- setThreadId,
169
- runId,
170
- setRunId,
171
- chatAbortControllerRef,
172
160
  });
173
161
 
174
- // this is a workaround born out of a bug that Athena incessantly ran into.
162
+ // this is a workaround born out of a bug that Athena insessently ran into.
175
163
  // We could not find the origin of the bug, however, it was clear that an outdated version of the append function was being used somehow --
176
- // it referenced the old state of the messages array, and not the latest one.
164
+ // it referecned the old state of the messages array, and not the latest one.
177
165
  //
178
166
  // We want to make copilotkit as abuse-proof as possible, so we are adding this workaround to ensure that the latest version of the append function is always used.
179
167
  //
@@ -213,25 +201,10 @@ export function useCopilotChat({
213
201
  [latestSetMessages],
214
202
  );
215
203
 
216
- const reset = useCallback(() => {
217
- latestStopFunc();
218
- setMessages([]);
219
- setThreadId(null);
220
- setRunId(null);
221
- setCoagentStates({});
222
- let initialAgentSession: AgentSession | null = null;
223
- if (agentLock) {
224
- initialAgentSession = {
225
- agentName: agentLock,
226
- };
227
- }
228
- setAgentSession(initialAgentSession);
229
- }, [latestStopFunc, setMessages, setThreadId, setCoagentStates, setAgentSession, agentLock]);
230
-
231
- const latestReset = useUpdatedRef(reset);
232
- const latestResetFunc = useCallback(() => {
233
- return latestReset.current();
234
- }, [latestReset]);
204
+ const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
205
+ const latestRunChatCompletionFunc = useCallback(() => {
206
+ return latestRunChatCompletion.current!();
207
+ }, [latestRunChatCompletion]);
235
208
 
236
209
  return {
237
210
  visibleMessages: messages,
@@ -239,8 +212,8 @@ export function useCopilotChat({
239
212
  setMessages: latestSetMessagesFunc,
240
213
  reloadMessages: latestReloadFunc,
241
214
  stopGeneration: latestStopFunc,
242
- reset: latestResetFunc,
243
215
  deleteMessage: latestDeleteFunc,
216
+ runChatCompletion: latestRunChatCompletionFunc,
244
217
  isLoading,
245
218
  };
246
219
  }
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-chat.ts"],"sourcesContent":["import { useRef } from \"react\";\nimport {\n FunctionCallHandler,\n COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,\n actionParametersToJsonSchema,\n CoAgentStateRenderHandler,\n} from \"@copilotkit/shared\";\nimport {\n Message,\n TextMessage,\n ResultMessage,\n convertMessagesToGqlInput,\n filterAdjacentAgentStateMessages,\n filterAgentStateMessages,\n convertGqlOutputToMessages,\n MessageStatusCode,\n MessageRole,\n Role,\n CopilotRequestType,\n ActionInputAvailability,\n} from \"@copilotkit/runtime-client-gql\";\n\nimport { CopilotApiConfig } from \"../context\";\nimport { FrontendAction } from \"../types/frontend-action\";\nimport { CoagentState } from \"../types/coagent-state\";\nimport { AgentSession } from \"../context/copilot-context\";\nimport { useToast } from \"../components/toast/toast-provider\";\nimport { useCopilotRuntimeClient } from \"./use-copilot-runtime-client\";\n\nexport type UseChatOptions = {\n /**\n * System messages of the chat. Defaults to an empty array.\n */\n initialMessages?: Message[];\n /**\n * Callback function to be called when a function call is received.\n * If the function returns a `ChatRequest` object, the request will be sent\n * automatically to the API and will be used to update the chat.\n */\n onFunctionCall?: FunctionCallHandler;\n\n /**\n * Callback function to be called when a coagent action is received.\n */\n onCoAgentStateRender?: CoAgentStateRenderHandler;\n\n /**\n * Function definitions to be sent to the API.\n */\n actions: FrontendAction<any>[];\n\n /**\n * The CopilotKit API configuration.\n */\n copilotConfig: CopilotApiConfig;\n\n /**\n * The current list of messages in the chat.\n */\n messages: Message[];\n /**\n * The setState-powered method to update the chat messages.\n */\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n\n /**\n * A callback to get the latest system message.\n */\n makeSystemMessageCallback: () => TextMessage;\n\n /**\n * Whether the API request is in progress\n */\n isLoading: boolean;\n\n /**\n * setState-powered method to update the isChatLoading value\n */\n setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;\n\n /**\n * The current list of coagent states.\n */\n coagentStates: Record<string, CoagentState>;\n\n /**\n * setState-powered method to update the agent states\n */\n setCoagentStates: React.Dispatch<React.SetStateAction<Record<string, CoagentState>>>;\n\n /**\n * The current agent session.\n */\n agentSession: AgentSession | null;\n\n /**\n * setState-powered method to update the agent session\n */\n setAgentSession: React.Dispatch<React.SetStateAction<AgentSession | null>>;\n\n /**\n * The current thread ID.\n */\n threadId: string | null;\n /**\n * set the current thread ID\n */\n setThreadId: (threadId: string | null) => void;\n /**\n * The current run ID.\n */\n runId: string | null;\n /**\n * set the current run ID\n */\n setRunId: (runId: string | null) => void;\n /**\n * The global chat abort controller.\n */\n chatAbortControllerRef: React.MutableRefObject<AbortController | null>;\n};\n\nexport type UseChatHelpers = {\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n */\n append: (message: Message) => Promise<void>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: () => Promise<void>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n};\n\nexport function useChat(options: UseChatOptions): UseChatHelpers {\n const {\n messages,\n setMessages,\n makeSystemMessageCallback,\n copilotConfig,\n setIsLoading,\n initialMessages,\n isLoading,\n actions,\n onFunctionCall,\n onCoAgentStateRender,\n setCoagentStates,\n coagentStates,\n agentSession,\n setAgentSession,\n threadId,\n setThreadId,\n runId,\n setRunId,\n chatAbortControllerRef,\n } = options;\n const { addGraphQLErrorsToast } = useToast();\n const runChatCompletionRef = useRef<(previousMessages: Message[]) => Promise<Message[]>>();\n // We need to keep a ref of coagent states because of renderAndWait - making sure\n // the latest state is sent to the API\n // This is a workaround and needs to be addressed in the future\n const coagentStatesRef = useRef<Record<string, CoagentState>>(coagentStates);\n coagentStatesRef.current = coagentStates;\n const agentSessionRef = useRef<AgentSession | null>(agentSession);\n agentSessionRef.current = agentSession;\n const threadIdRef = useRef<string | null>(threadId);\n threadIdRef.current = threadId;\n const runIdRef = useRef<string | null>(runId);\n runIdRef.current = runId;\n\n const publicApiKey = copilotConfig.publicApiKey;\n\n const headers = {\n ...(copilotConfig.headers || {}),\n ...(publicApiKey ? { [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {}),\n };\n\n const runtimeClient = useCopilotRuntimeClient({\n url: copilotConfig.chatApiEndpoint,\n publicApiKey: copilotConfig.publicApiKey,\n headers,\n credentials: copilotConfig.credentials,\n });\n\n const runChatCompletion = async (previousMessages: Message[]): Promise<Message[]> => {\n setIsLoading(true);\n\n // this message is just a placeholder. It will disappear once the first real message\n // is received\n let newMessages: Message[] = [\n new TextMessage({\n content: \"\",\n role: Role.Assistant,\n }),\n ];\n\n chatAbortControllerRef.current = new AbortController();\n\n setMessages([...previousMessages, ...newMessages]);\n\n const systemMessage = makeSystemMessageCallback();\n\n const messagesWithContext = [systemMessage, ...(initialMessages || []), ...previousMessages];\n\n const stream = runtimeClient.asStream(\n runtimeClient.generateCopilotResponse({\n data: {\n frontend: {\n actions: actions\n .filter(\n (action) =>\n action.available !== ActionInputAvailability.Disabled || !action.disabled,\n )\n .map((action) => {\n let available: ActionInputAvailability | undefined =\n ActionInputAvailability.Enabled;\n if (action.disabled) {\n available = ActionInputAvailability.Disabled;\n } else if (action.available === \"disabled\") {\n available = ActionInputAvailability.Disabled;\n } else if (action.available === \"remote\") {\n available = ActionInputAvailability.Remote;\n }\n return {\n name: action.name,\n description: action.description || \"\",\n jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters || [])),\n available,\n };\n }),\n url: window.location.href,\n },\n threadId: threadIdRef.current,\n runId: runIdRef.current,\n messages: convertMessagesToGqlInput(filterAgentStateMessages(messagesWithContext)),\n ...(copilotConfig.cloud\n ? {\n cloud: {\n ...(copilotConfig.cloud.guardrails?.input?.restrictToTopic?.enabled\n ? {\n guardrails: {\n inputValidationRules: {\n allowList:\n copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,\n denyList:\n copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics,\n },\n },\n }\n : {}),\n },\n }\n : {}),\n metadata: {\n requestType: CopilotRequestType.Chat,\n },\n ...(agentSessionRef.current\n ? {\n agentSession: agentSessionRef.current,\n }\n : {}),\n agentStates: Object.values(coagentStatesRef.current).map((state) => ({\n agentName: state.name,\n state: JSON.stringify(state.state),\n })),\n },\n properties: copilotConfig.properties,\n signal: chatAbortControllerRef.current?.signal,\n }),\n );\n\n const guardrailsEnabled =\n copilotConfig.cloud?.guardrails?.input?.restrictToTopic.enabled || false;\n\n const reader = stream.getReader();\n\n let actionResults: { [id: string]: string } = {};\n let executedCoAgentStateRenders: string[] = [];\n let followUp: FrontendAction[\"followUp\"] = undefined;\n\n try {\n while (true) {\n let done, value;\n\n try {\n const readResult = await reader.read();\n done = readResult.done;\n value = readResult.value;\n } catch (readError) {\n break;\n }\n\n if (done) {\n if (chatAbortControllerRef.current.signal.aborted) {\n return []\n }\n break;\n }\n\n if (!value?.generateCopilotResponse) {\n continue;\n }\n\n threadIdRef.current = value.generateCopilotResponse.threadId || null;\n runIdRef.current = value.generateCopilotResponse.runId || null;\n\n setThreadId(threadIdRef.current);\n setRunId(runIdRef.current);\n\n const messages = convertGqlOutputToMessages(\n filterAdjacentAgentStateMessages(value.generateCopilotResponse.messages),\n );\n\n if (messages.length === 0) {\n continue;\n }\n\n newMessages = [];\n\n // request failed, display error message\n if (\n value.generateCopilotResponse.status?.__typename === \"FailedResponseStatus\" &&\n value.generateCopilotResponse.status.reason === \"GUARDRAILS_VALIDATION_FAILED\"\n ) {\n newMessages = [\n new TextMessage({\n role: MessageRole.Assistant,\n content: value.generateCopilotResponse.status.details?.guardrailsReason || \"\",\n }),\n ];\n }\n\n // add messages to the chat\n else {\n for (const message of messages) {\n newMessages.push(message);\n // execute regular action executions\n if (\n message.isActionExecutionMessage() &&\n message.status.code !== MessageStatusCode.Pending &&\n message.scope === \"client\" &&\n onFunctionCall\n ) {\n if (!(message.id in actionResults)) {\n // Do not execute a function call if guardrails are enabled but the status is not known\n if (guardrailsEnabled && value.generateCopilotResponse.status === undefined) {\n break;\n }\n // execute action\n try {\n // We update the message state before calling the handler so that the render\n // function can be called with `executing` state\n setMessages([...previousMessages, ...newMessages]);\n\n const action = actions.find((action) => action.name === message.name);\n\n if (action) {\n followUp = action.followUp;\n }\n\n const result = await Promise.race([\n onFunctionCall({\n messages: previousMessages,\n name: message.name,\n args: message.arguments,\n }),\n new Promise((_, reject) => chatAbortControllerRef.current?.signal.addEventListener('abort', () => reject(new Error('Operation was aborted'))))\n ])\n if (chatAbortControllerRef.current.signal.aborted){\n actionResults[message.id] = \"\";\n } else {\n actionResults[message.id] = result;\n }\n } catch (e) {\n actionResults[message.id] = `Failed to execute action ${message.name}`;\n console.error(`Failed to execute action ${message.name}: ${e}`);\n }\n }\n // add the result message\n newMessages.push(\n new ResultMessage({\n result: ResultMessage.encodeResult(actionResults[message.id]),\n actionExecutionId: message.id,\n actionName: message.name,\n }),\n );\n }\n // execute coagent actions\n if (\n message.isAgentStateMessage() &&\n !message.active &&\n !executedCoAgentStateRenders.includes(message.id) &&\n onCoAgentStateRender\n ) {\n // Do not execute a coagent action if guardrails are enabled but the status is not known\n if (guardrailsEnabled && value.generateCopilotResponse.status === undefined) {\n break;\n }\n // execute coagent action\n await onCoAgentStateRender({\n name: message.agentName,\n nodeName: message.nodeName,\n state: message.state,\n });\n executedCoAgentStateRenders.push(message.id);\n }\n }\n\n const lastAgentStateMessage = [...messages]\n .reverse()\n .find((message) => message.isAgentStateMessage());\n\n if (lastAgentStateMessage) {\n setCoagentStates((prevAgentStates) => ({\n ...prevAgentStates,\n [lastAgentStateMessage.agentName]: {\n name: lastAgentStateMessage.agentName,\n state: lastAgentStateMessage.state,\n running: lastAgentStateMessage.running,\n active: lastAgentStateMessage.active,\n threadId: lastAgentStateMessage.threadId,\n nodeName: lastAgentStateMessage.nodeName,\n runId: lastAgentStateMessage.runId,\n },\n }));\n if (lastAgentStateMessage.running) {\n setAgentSession({\n threadId: lastAgentStateMessage.threadId,\n agentName: lastAgentStateMessage.agentName,\n nodeName: lastAgentStateMessage.nodeName,\n });\n } else {\n setAgentSession(null);\n }\n }\n }\n\n if (newMessages.length > 0) {\n // Update message state\n setMessages([...previousMessages, ...newMessages]);\n }\n }\n\n if (\n // if followUp is not explicitly false\n followUp !== false &&\n // if we have client side results\n (Object.values(actionResults).length ||\n // or the last message we received is a result\n (newMessages.length && newMessages[newMessages.length - 1].isResultMessage()))\n ) {\n // run the completion again and return the result\n\n // wait for next tick to make sure all the react state updates\n // - tried using react-dom's flushSync, but it did not work\n await new Promise((resolve) => setTimeout(resolve, 10));\n\n return await runChatCompletionRef.current!([...previousMessages, ...newMessages]);\n } else {\n return newMessages.slice();\n }\n } finally {\n setIsLoading(false);\n }\n };\n\n runChatCompletionRef.current = runChatCompletion;\n\n const runChatCompletionAndHandleFunctionCall = async (messages: Message[]): Promise<void> => {\n await runChatCompletionRef.current!(messages);\n };\n\n const append = async (message: Message): Promise<void> => {\n if (isLoading) {\n return;\n }\n\n const newMessages = [...messages, message];\n setMessages(newMessages);\n return runChatCompletionAndHandleFunctionCall(newMessages);\n };\n\n const reload = async (): Promise<void> => {\n if (isLoading || messages.length === 0) {\n return;\n }\n let newMessages = [...messages];\n const lastMessage = messages[messages.length - 1];\n\n if (lastMessage.isTextMessage() && lastMessage.role === \"assistant\") {\n newMessages = newMessages.slice(0, -1);\n }\n\n setMessages(newMessages);\n\n return runChatCompletionAndHandleFunctionCall(newMessages);\n };\n\n const stop = (): void => {\n chatAbortControllerRef.current?.abort(\"Stop was called\");\n };\n\n return {\n append,\n reload,\n stop,\n };\n}\n"],"mappings":";;;;;;;;;;;;;AAAA,SAAS,cAAc;AACvB;AAAA,EAEE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAyHA,SAAS,QAAQ,SAAyC;AAC/D,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AACJ,QA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,UAAU;AAE/B,QAAM,yCAAyC,CAAOA,cAAuC;AAC3F,UAAM,qBAAqB,QAASA,SAAQ;AAAA,EAC9C;AAEA,QAAM,SAAS,CAAO,YAAoC;AACxD,QAAI,WAAW;AACb;AAAA,IACF;AAEA,UAAM,cAAc,CAAC,GAAG,UAAU,OAAO;AACzC,gBAAY,WAAW;AACvB,WAAO,uCAAuC,WAAW;AAAA,EAC3D;AAEA,QAAM,SAAS,MAA2B;AACxC,QAAI,aAAa,SAAS,WAAW,GAAG;AACtC;AAAA,IACF;AACA,QAAI,cAAc,CAAC,GAAG,QAAQ;AAC9B,UAAM,cAAc,SAAS,SAAS,SAAS,CAAC;AAEhD,QAAI,YAAY,cAAc,KAAK,YAAY,SAAS,aAAa;AACnE,oBAAc,YAAY,MAAM,GAAG,EAAE;AAAA,IACvC;AAEA,gBAAY,WAAW;AAEvB,WAAO,uCAAuC,WAAW;AAAA,EAC3D;AAEA,QAAM,OAAO,MAAY;AAzf3B;AA0fI,iCAAuB,YAAvB,mBAAgC,MAAM;AAAA,EACxC;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":["messages","action","_a"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-copilot-chat.ts"],"sourcesContent":["/**\n * `useCopilotChat` is a React hook that lets you directly interact with the\n * Copilot instance. Use to implement a fully custom UI (headless UI) or to\n * programmatically interact with the Copilot instance managed by the default\n * UI.\n *\n * ## Usage\n *\n * ### Simple Usage\n *\n * ```tsx\n * import { useCopilotChat } from \"@copilotkit/react-core\";\n * import { Role, TextMessage } from \"@copilotkit/runtime-client-gql\";\n *\n * export function YourComponent() {\n * const { appendMessage } = useCopilotChat();\n *\n * appendMessage(\n * new TextMessage({\n * content: \"Hello World\",\n * role: Role.User,\n * }),\n * );\n * }\n * ```\n *\n * `useCopilotChat` returns an object with the following properties:\n *\n * ```tsx\n * const {\n * visibleMessages, // An array of messages that are currently visible in the chat.\n * appendMessage, // A function to append a message to the chat.\n * setMessages, // A function to set the messages in the chat.\n * deleteMessage, // A function to delete a message from the chat.\n * reloadMessages, // A function to reload the messages from the API.\n * stopGeneration, // A function to stop the generation of the next message.\n * isLoading, // A boolean indicating if the chat is loading.\n * } = useCopilotChat();\n * ```\n */\nimport { useRef, useEffect, useCallback } from \"react\";\nimport { AgentSession, useCopilotContext } from \"../context/copilot-context\";\nimport { Message, Role, TextMessage } from \"@copilotkit/runtime-client-gql\";\nimport { SystemMessageFunction } from \"../types\";\nimport { useChat } from \"./use-chat\";\nimport { defaultCopilotContextCategories } from \"../components\";\nimport { MessageStatusCode } from \"@copilotkit/runtime-client-gql\";\nimport { CoAgentStateRenderHandlerArguments } from \"@copilotkit/shared\";\nimport { useCopilotMessagesContext } from \"../context\";\n\nexport interface UseCopilotChatOptions {\n /**\n * A unique identifier for the chat. If not provided, a random one will be\n * generated. When provided, the `useChat` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n\n /**\n * HTTP headers to be sent with the API request.\n */\n headers?: Record<string, string> | Headers;\n /**\n * System messages of the chat. Defaults to an empty array.\n */\n initialMessages?: Message[];\n\n /**\n * A function to generate the system message. Defaults to `defaultSystemMessage`.\n */\n makeSystemMessage?: SystemMessageFunction;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n appendMessage: (message: Message) => Promise<void>;\n setMessages: (messages: Message[]) => void;\n deleteMessage: (messageId: string) => void;\n reloadMessages: () => Promise<void>;\n stopGeneration: () => void;\n reset: () => void;\n isLoading: boolean;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n ...options\n}: UseCopilotChatOptions = {}): UseCopilotChatReturn {\n const {\n getContextString,\n getFunctionCallHandler,\n copilotApiConfig,\n isLoading,\n setIsLoading,\n chatInstructions,\n actions,\n\n coagentStates,\n setCoagentStates,\n coAgentStateRenders,\n agentSession,\n setAgentSession,\n agentLock,\n threadId,\n setThreadId,\n runId,\n setRunId,\n chatAbortControllerRef,\n } = useCopilotContext();\n const { messages, setMessages } = useCopilotMessagesContext();\n\n // We need to ensure that makeSystemMessageCallback always uses the latest\n // useCopilotReadable data.\n const latestGetContextString = useUpdatedRef(getContextString);\n const deleteMessage = useCallback(\n (messageId: string) => {\n setMessages((prev) => prev.filter((message) => message.id !== messageId));\n },\n [setMessages],\n );\n\n const makeSystemMessageCallback = useCallback(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n // this always gets the latest context string\n const contextString = latestGetContextString.current([], defaultCopilotContextCategories); // TODO: make the context categories configurable\n\n return new TextMessage({\n content: systemMessageMaker(contextString, chatInstructions),\n role: Role.System,\n });\n }, [getContextString, makeSystemMessage, chatInstructions]);\n\n const onCoAgentStateRender = useCallback(\n async (args: CoAgentStateRenderHandlerArguments) => {\n const { name, nodeName, state } = args;\n let action = Object.values(coAgentStateRenders).find(\n (action) => action.name === name && action.nodeName === nodeName,\n );\n if (!action) {\n action = Object.values(coAgentStateRenders).find(\n (action) => action.name === name && !action.nodeName,\n );\n }\n if (action) {\n await action.handler?.({ state, nodeName });\n }\n },\n [coAgentStateRenders],\n );\n\n const { append, reload, stop } = useChat({\n ...options,\n actions: Object.values(actions),\n copilotConfig: copilotApiConfig,\n initialMessages: options.initialMessages || [],\n onFunctionCall: getFunctionCallHandler(),\n onCoAgentStateRender,\n messages,\n setMessages,\n makeSystemMessageCallback,\n isLoading,\n setIsLoading,\n coagentStates,\n setCoagentStates,\n agentSession,\n setAgentSession,\n threadId,\n setThreadId,\n runId,\n setRunId,\n chatAbortControllerRef,\n });\n\n // this is a workaround born out of a bug that Athena incessantly ran into.\n // We could not find the origin of the bug, however, it was clear that an outdated version of the append function was being used somehow --\n // it referenced the old state of the messages array, and not the latest one.\n //\n // We want to make copilotkit as abuse-proof as possible, so we are adding this workaround to ensure that the latest version of the append function is always used.\n //\n // How does this work?\n // we store the relevant function in a ref that is always up-to-date, and then we use that ref in the callback.\n const latestAppend = useUpdatedRef(append);\n const latestAppendFunc = useCallback(\n (message: Message) => {\n return latestAppend.current(message);\n },\n [latestAppend],\n );\n\n const latestReload = useUpdatedRef(reload);\n const latestReloadFunc = useCallback(() => {\n return latestReload.current();\n }, [latestReload]);\n\n const latestStop = useUpdatedRef(stop);\n const latestStopFunc = useCallback(() => {\n return latestStop.current();\n }, [latestStop]);\n\n const latestDelete = useUpdatedRef(deleteMessage);\n const latestDeleteFunc = useCallback(\n (messageId: string) => {\n return latestDelete.current(messageId);\n },\n [latestDelete],\n );\n\n const latestSetMessages = useUpdatedRef(setMessages);\n const latestSetMessagesFunc = useCallback(\n (messages: Message[]) => {\n return latestSetMessages.current(messages);\n },\n [latestSetMessages],\n );\n\n const reset = useCallback(() => {\n latestStopFunc();\n setMessages([]);\n setThreadId(null);\n setRunId(null);\n setCoagentStates({});\n let initialAgentSession: AgentSession | null = null;\n if (agentLock) {\n initialAgentSession = {\n agentName: agentLock,\n };\n }\n setAgentSession(initialAgentSession);\n }, [latestStopFunc, setMessages, setThreadId, setCoagentStates, setAgentSession, agentLock]);\n\n const latestReset = useUpdatedRef(reset);\n const latestResetFunc = useCallback(() => {\n return latestReset.current();\n }, [latestReset]);\n\n return {\n visibleMessages: messages,\n appendMessage: latestAppendFunc,\n setMessages: latestSetMessagesFunc,\n reloadMessages: latestReloadFunc,\n stopGeneration: latestStopFunc,\n reset: latestResetFunc,\n deleteMessage: latestDeleteFunc,\n isLoading,\n };\n}\n\n// store `value` in a ref and update\n// it whenever it changes.\nfunction useUpdatedRef<T>(value: T) {\n const ref = useRef(value);\n\n useEffect(() => {\n ref.current = value;\n }, [value]);\n\n return ref;\n}\n\nexport function defaultSystemMessage(\n contextString: string,\n additionalInstructions?: string,\n): string {\n return (\n `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.\n\nPlease assist them as best you can.\n\nYou can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.\n\nIf you would like to call a function, call it without saying anything else.\n` + (additionalInstructions ? `\\n\\n${additionalInstructions}` : \"\")\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAwCA,SAAS,QAAQ,WAAW,mBAAmB;AAE/C,SAAkB,MAAM,mBAAmB;AA0CpC,SAAS,eAAe,KAGJ,CAAC,GAAyB;AAHtB,eAC7B;AAAA;AAAA,EArFF,IAoF+B,IAE1B,oBAF0B,IAE1B;AAAA,IADH;AAAA;AAGA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IAEA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,kBAAkB;AACtB,QAAM,EAAE,UAAU,YAAY,IAAI,0BAA0B;AAI5D,QAAM,yBAAyB,cAAc,gBAAgB;AAC7D,QAAM,gBAAgB;AAAA,IACpB,CAAC,cAAsB;AACrB,kBAAY,CAAC,SAAS,KAAK,OAAO,CAAC,YAAY,QAAQ,OAAO,SAAS,CAAC;AAAA,IAC1E;AAAA,IACA,CAAC,WAAW;AAAA,EACd;AAEA,QAAM,4BAA4B,YAAY,MAAM;AAClD,UAAM,qBAAqB,qBAAqB;AAEhD,UAAM,gBAAgB,uBAAuB,QAAQ,CAAC,GAAG,+BAA+B;AAExF,WAAO,IAAI,YAAY;AAAA,MACrB,SAAS,mBAAmB,eAAe,gBAAgB;AAAA,MAC3D,MAAM,KAAK;AAAA,IACb,CAAC;AAAA,EACH,GAAG,CAAC,kBAAkB,mBAAmB,gBAAgB,CAAC;AAE1D,QAAM,uBAAuB;AAAA,IAC3B,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AA;AAAA;AAAA;AAAA,KAUG,yBAAyB;AAAA;AAAA,EAAO,2BAA2B;AAEhE;","names":["_a","action","messages"]}
@@ -1 +0,0 @@
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)</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 { useEffect } 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 { AgentStateMessage, Message, Role, TextMessage } from \"@copilotkit/runtime-client-gql\";\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\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\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\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.\n */\nexport function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentReturnType<T> {\n const isExternalStateManagement = (\n options: UseCoagentOptions<T>,\n ): options is WithExternalStateManagement<T> => {\n return \"state\" in options && \"setState\" in options;\n };\n\n const { name } = options;\n\n const isInternalStateManagementWithInitial = (\n options: UseCoagentOptions<T>,\n ): options is WithInternalStateManagementAndInitial<T> => {\n return \"initialState\" in options;\n };\n\n const generalContext = useCopilotContext();\n const messagesContext = useCopilotMessagesContext();\n const context = { ...generalContext, ...messagesContext };\n const { coagentStates, setCoagentStates } = context;\n const { appendMessage } = useCopilotChat();\n\n const getCoagentState = (coagentStates: Record<string, CoagentState>, name: string) => {\n if (coagentStates[name]) {\n return coagentStates[name];\n } else {\n return {\n name,\n state: isInternalStateManagementWithInitial(options) ? options.initialState : {},\n running: false,\n active: false,\n threadId: undefined,\n nodeName: undefined,\n runId: undefined,\n };\n }\n };\n\n // if we manage state internally, we need to provide a function to set the state\n const setState = (newState: T | ((prevState: T | undefined) => T)) => {\n setCoagentStates((prevAgentStates) => {\n let coagentState: CoagentState = getCoagentState(prevAgentStates, name);\n\n const updatedState =\n typeof newState === \"function\" ? (newState as Function)(coagentState.state) : newState;\n\n return {\n ...prevAgentStates,\n [name]: {\n ...coagentState,\n state: updatedState,\n },\n };\n });\n };\n\n const coagentState = getCoagentState(coagentStates, name);\n\n const state = isExternalStateManagement(options) ? options.state : coagentState.state;\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 // Return the state and setState function\n return {\n name,\n nodeName: coagentState.nodeName,\n state,\n setState,\n running: coagentState.running,\n start: () => {\n startAgent(name, context);\n },\n stop: () => {\n stopAgent(name, context);\n },\n run: (hint?: HintFunction) => {\n return runAgent(name, context, appendMessage, hint);\n },\n };\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 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.coagentStates?.[name]?.state || {};\n\n if (hint) {\n const hintMessage = hint({ previousState, currentState: state });\n if (hintMessage)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