@copilotkit/react-core 1.10.0-next.1 → 1.10.0-next.10

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 (204) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/dist/{chunk-KV25ZRMH.mjs → chunk-2TSNHEIS.mjs} +6 -6
  3. package/dist/chunk-2TSNHEIS.mjs.map +1 -0
  4. package/dist/{chunk-DF4YG4PF.mjs → chunk-3RHHNUVV.mjs} +2 -2
  5. package/dist/{chunk-LNAQ7JG3.mjs → chunk-6EKLRL7B.mjs} +2 -2
  6. package/dist/{chunk-JZQOCH4A.mjs → chunk-ADZDXHVC.mjs} +4 -4
  7. package/dist/{chunk-4CFY3CON.mjs → chunk-CLMDRYEN.mjs} +2 -2
  8. package/dist/{chunk-VM7CVIET.mjs → chunk-COEUPDRL.mjs} +20 -21
  9. package/dist/chunk-COEUPDRL.mjs.map +1 -0
  10. package/dist/{chunk-RGKZCCPA.mjs → chunk-DLEXVOQE.mjs} +5 -5
  11. package/dist/{chunk-YAF2LATQ.mjs → chunk-EFL5OBKN.mjs} +1 -1
  12. package/dist/chunk-EFL5OBKN.mjs.map +1 -0
  13. package/dist/{chunk-JWAXDYOW.mjs → chunk-FAUNHSQU.mjs} +3 -3
  14. package/dist/chunk-ICIK2BSB.mjs +17 -0
  15. package/dist/chunk-ICIK2BSB.mjs.map +1 -0
  16. package/dist/chunk-IHAZJF3V.mjs +34 -0
  17. package/dist/chunk-IHAZJF3V.mjs.map +1 -0
  18. package/dist/{chunk-Q6FZZJ5A.mjs → chunk-IN7GE4NO.mjs} +2 -2
  19. package/dist/{chunk-4XVBXDCX.mjs → chunk-JBLMXZ3O.mjs} +8 -8
  20. package/dist/{chunk-NXCJELW7.mjs → chunk-JJDXTTEN.mjs} +3 -3
  21. package/dist/chunk-KDAZGZ24.mjs +1 -0
  22. package/dist/{chunk-VOMGRGWT.mjs → chunk-L6HQIJ74.mjs} +33 -17
  23. package/dist/chunk-L6HQIJ74.mjs.map +1 -0
  24. package/dist/{chunk-OMVNJ7S3.mjs → chunk-LVWV62JZ.mjs} +37 -24
  25. package/dist/chunk-LVWV62JZ.mjs.map +1 -0
  26. package/dist/{chunk-3OQM3NEK.mjs → chunk-N4WEHORG.mjs} +2 -2
  27. package/dist/chunk-OKRZF3DD.mjs +225 -0
  28. package/dist/chunk-OKRZF3DD.mjs.map +1 -0
  29. package/dist/{chunk-SGF6C7I6.mjs → chunk-Q42NJFXR.mjs} +11 -16
  30. package/dist/chunk-Q42NJFXR.mjs.map +1 -0
  31. package/dist/chunk-QGE7U4NV.mjs +85 -0
  32. package/dist/chunk-QGE7U4NV.mjs.map +1 -0
  33. package/dist/{chunk-XGRBCWK6.mjs → chunk-TEMLWRRT.mjs} +3 -3
  34. package/dist/chunk-TEMLWRRT.mjs.map +1 -0
  35. package/dist/{chunk-PYULBXCD.mjs → chunk-TWYUYC4F.mjs} +44 -10
  36. package/dist/chunk-TWYUYC4F.mjs.map +1 -0
  37. package/dist/{chunk-WUORFPJ7.mjs → chunk-X2DNXTME.mjs} +6 -6
  38. package/dist/{chunk-DCTJZ742.mjs → chunk-ZLQVRPDS.mjs} +5 -2
  39. package/dist/chunk-ZLQVRPDS.mjs.map +1 -0
  40. package/dist/components/copilot-provider/copilot-messages.d.ts +17 -1
  41. package/dist/components/copilot-provider/copilot-messages.js +46 -7
  42. package/dist/components/copilot-provider/copilot-messages.js.map +1 -1
  43. package/dist/components/copilot-provider/copilot-messages.mjs +11 -7
  44. package/dist/components/copilot-provider/copilotkit-props.d.ts +5 -3
  45. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
  46. package/dist/components/copilot-provider/copilotkit.d.ts +1 -2
  47. package/dist/components/copilot-provider/copilotkit.js +337 -555
  48. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  49. package/dist/components/copilot-provider/copilotkit.mjs +10 -10
  50. package/dist/components/copilot-provider/index.d.ts +1 -2
  51. package/dist/components/copilot-provider/index.js +337 -555
  52. package/dist/components/copilot-provider/index.js.map +1 -1
  53. package/dist/components/copilot-provider/index.mjs +10 -10
  54. package/dist/components/error-boundary/error-boundary.js +176 -323
  55. package/dist/components/error-boundary/error-boundary.js.map +1 -1
  56. package/dist/components/error-boundary/error-boundary.mjs +4 -4
  57. package/dist/components/error-boundary/error-utils.js.map +1 -1
  58. package/dist/components/error-boundary/error-utils.mjs +2 -2
  59. package/dist/components/index.d.ts +1 -2
  60. package/dist/components/index.js +337 -555
  61. package/dist/components/index.js.map +1 -1
  62. package/dist/components/index.mjs +10 -10
  63. package/dist/components/toast/toast-provider.js.map +1 -1
  64. package/dist/components/toast/toast-provider.mjs +1 -1
  65. package/dist/components/usage-banner.d.ts +10 -4
  66. package/dist/components/usage-banner.js +176 -302
  67. package/dist/components/usage-banner.js.map +1 -1
  68. package/dist/components/usage-banner.mjs +3 -1
  69. package/dist/context/copilot-context.d.ts +1 -2
  70. package/dist/context/copilot-context.js +2 -2
  71. package/dist/context/copilot-context.js.map +1 -1
  72. package/dist/context/copilot-context.mjs +1 -1
  73. package/dist/context/copilot-messages-context.d.ts +11 -15
  74. package/dist/context/copilot-messages-context.js +4 -1
  75. package/dist/context/copilot-messages-context.js.map +1 -1
  76. package/dist/context/copilot-messages-context.mjs +1 -1
  77. package/dist/context/index.d.ts +2 -2
  78. package/dist/context/index.js +6 -3
  79. package/dist/context/index.js.map +1 -1
  80. package/dist/context/index.mjs +5 -5
  81. package/dist/{copilot-context-bd88d30d.d.ts → copilot-context-256f9020.d.ts} +4 -22
  82. package/dist/hooks/index.d.ts +7 -5
  83. package/dist/hooks/index.js +250 -122
  84. package/dist/hooks/index.js.map +1 -1
  85. package/dist/hooks/index.mjs +42 -34
  86. package/dist/hooks/use-chat.d.ts +1 -2
  87. package/dist/hooks/use-chat.js +11 -3
  88. package/dist/hooks/use-chat.js.map +1 -1
  89. package/dist/hooks/use-chat.mjs +6 -6
  90. package/dist/hooks/use-coagent-state-render.js +2 -2
  91. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  92. package/dist/hooks/use-coagent-state-render.mjs +3 -3
  93. package/dist/hooks/use-coagent.d.ts +3 -4
  94. package/dist/hooks/use-coagent.js +114 -81
  95. package/dist/hooks/use-coagent.js.map +1 -1
  96. package/dist/hooks/use-coagent.mjs +16 -16
  97. package/dist/hooks/use-copilot-action.js +2 -2
  98. package/dist/hooks/use-copilot-action.js.map +1 -1
  99. package/dist/hooks/use-copilot-action.mjs +4 -4
  100. package/dist/hooks/use-copilot-additional-instructions.js +2 -2
  101. package/dist/hooks/use-copilot-additional-instructions.js.map +1 -1
  102. package/dist/hooks/use-copilot-additional-instructions.mjs +2 -2
  103. package/dist/hooks/use-copilot-authenticated-action.js +2 -2
  104. package/dist/hooks/use-copilot-authenticated-action.js.map +1 -1
  105. package/dist/hooks/use-copilot-authenticated-action.mjs +5 -5
  106. package/dist/hooks/use-copilot-chat-headless_c.d.ts +33 -0
  107. package/dist/hooks/use-copilot-chat-headless_c.js +1837 -0
  108. package/dist/hooks/use-copilot-chat-headless_c.js.map +1 -0
  109. package/dist/hooks/use-copilot-chat-headless_c.mjs +31 -0
  110. package/dist/hooks/use-copilot-chat-headless_c.mjs.map +1 -0
  111. package/dist/hooks/use-copilot-chat.d.ts +52 -82
  112. package/dist/hooks/use-copilot-chat.js +60 -13
  113. package/dist/hooks/use-copilot-chat.js.map +1 -1
  114. package/dist/hooks/use-copilot-chat.mjs +16 -17
  115. package/dist/hooks/use-copilot-chat_internal.d.ts +177 -0
  116. package/dist/hooks/use-copilot-chat_internal.js +1770 -0
  117. package/dist/hooks/use-copilot-chat_internal.js.map +1 -0
  118. package/dist/hooks/use-copilot-chat_internal.mjs +29 -0
  119. package/dist/hooks/use-copilot-chat_internal.mjs.map +1 -0
  120. package/dist/hooks/use-copilot-readable.js +2 -2
  121. package/dist/hooks/use-copilot-readable.js.map +1 -1
  122. package/dist/hooks/use-copilot-readable.mjs +2 -2
  123. package/dist/hooks/use-copilot-runtime-client.js +9 -1
  124. package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
  125. package/dist/hooks/use-copilot-runtime-client.mjs +3 -3
  126. package/dist/hooks/use-langgraph-interrupt-render.js +2 -2
  127. package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
  128. package/dist/hooks/use-langgraph-interrupt-render.mjs +3 -3
  129. package/dist/hooks/use-langgraph-interrupt.d.ts +3 -4
  130. package/dist/hooks/use-langgraph-interrupt.js +32 -11
  131. package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
  132. package/dist/hooks/use-langgraph-interrupt.mjs +16 -16
  133. package/dist/hooks/use-make-copilot-document-readable.js +2 -2
  134. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  135. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  136. package/dist/index-08c43df1.d.ts +36 -0
  137. package/dist/index.d.ts +5 -3
  138. package/dist/index.js +460 -471
  139. package/dist/index.js.map +1 -1
  140. package/dist/index.mjs +47 -39
  141. package/dist/lib/copilot-task.d.ts +1 -2
  142. package/dist/lib/copilot-task.js.map +1 -1
  143. package/dist/lib/copilot-task.mjs +12 -12
  144. package/dist/lib/index.d.ts +1 -2
  145. package/dist/lib/index.js.map +1 -1
  146. package/dist/lib/index.mjs +12 -12
  147. package/dist/types/interrupt-action.d.ts +2 -3
  148. package/dist/utils/dev-console.d.ts +1 -1
  149. package/dist/utils/dev-console.js +9 -1
  150. package/dist/utils/dev-console.js.map +1 -1
  151. package/dist/utils/dev-console.mjs +1 -1
  152. package/dist/utils/extract.d.ts +2 -2
  153. package/dist/utils/extract.js.map +1 -1
  154. package/dist/utils/extract.mjs +10 -10
  155. package/dist/utils/index.d.ts +3 -3
  156. package/dist/utils/index.js +9 -1
  157. package/dist/utils/index.js.map +1 -1
  158. package/dist/utils/index.mjs +11 -11
  159. package/dist/utils/suggestions.d.ts +2 -2
  160. package/dist/utils/suggestions.js.map +1 -1
  161. package/dist/utils/suggestions.mjs +10 -10
  162. package/package.json +3 -3
  163. package/src/components/copilot-provider/copilot-messages.tsx +57 -1
  164. package/src/components/copilot-provider/copilotkit-props.tsx +4 -1
  165. package/src/components/copilot-provider/copilotkit.tsx +20 -10
  166. package/src/components/error-boundary/error-boundary.tsx +5 -15
  167. package/src/components/toast/toast-provider.tsx +1 -3
  168. package/src/components/usage-banner.tsx +193 -316
  169. package/src/context/copilot-context.tsx +12 -6
  170. package/src/context/copilot-messages-context.tsx +7 -1
  171. package/src/hooks/__tests__/use-coagent-config.test.ts +8 -1
  172. package/src/hooks/index.ts +7 -2
  173. package/src/hooks/use-coagent.ts +17 -19
  174. package/src/hooks/use-copilot-chat-headless_c.ts +187 -0
  175. package/src/hooks/use-copilot-chat.ts +64 -495
  176. package/src/hooks/use-copilot-chat_internal.ts +543 -0
  177. package/src/hooks/use-langgraph-interrupt.ts +1 -1
  178. package/src/utils/dev-console.ts +18 -2
  179. package/dist/chunk-57K2ZJ5F.mjs +0 -348
  180. package/dist/chunk-57K2ZJ5F.mjs.map +0 -1
  181. package/dist/chunk-CQPYJIBH.mjs +0 -1
  182. package/dist/chunk-DCTJZ742.mjs.map +0 -1
  183. package/dist/chunk-GFJW4RIM.mjs +0 -9
  184. package/dist/chunk-GFJW4RIM.mjs.map +0 -1
  185. package/dist/chunk-KV25ZRMH.mjs.map +0 -1
  186. package/dist/chunk-OMVNJ7S3.mjs.map +0 -1
  187. package/dist/chunk-PYULBXCD.mjs.map +0 -1
  188. package/dist/chunk-SGF6C7I6.mjs.map +0 -1
  189. package/dist/chunk-VM7CVIET.mjs.map +0 -1
  190. package/dist/chunk-VOMGRGWT.mjs.map +0 -1
  191. package/dist/chunk-XGRBCWK6.mjs.map +0 -1
  192. package/dist/chunk-YAF2LATQ.mjs.map +0 -1
  193. /package/dist/{chunk-DF4YG4PF.mjs.map → chunk-3RHHNUVV.mjs.map} +0 -0
  194. /package/dist/{chunk-LNAQ7JG3.mjs.map → chunk-6EKLRL7B.mjs.map} +0 -0
  195. /package/dist/{chunk-JZQOCH4A.mjs.map → chunk-ADZDXHVC.mjs.map} +0 -0
  196. /package/dist/{chunk-4CFY3CON.mjs.map → chunk-CLMDRYEN.mjs.map} +0 -0
  197. /package/dist/{chunk-RGKZCCPA.mjs.map → chunk-DLEXVOQE.mjs.map} +0 -0
  198. /package/dist/{chunk-JWAXDYOW.mjs.map → chunk-FAUNHSQU.mjs.map} +0 -0
  199. /package/dist/{chunk-Q6FZZJ5A.mjs.map → chunk-IN7GE4NO.mjs.map} +0 -0
  200. /package/dist/{chunk-4XVBXDCX.mjs.map → chunk-JBLMXZ3O.mjs.map} +0 -0
  201. /package/dist/{chunk-NXCJELW7.mjs.map → chunk-JJDXTTEN.mjs.map} +0 -0
  202. /package/dist/{chunk-CQPYJIBH.mjs.map → chunk-KDAZGZ24.mjs.map} +0 -0
  203. /package/dist/{chunk-3OQM3NEK.mjs.map → chunk-N4WEHORG.mjs.map} +0 -0
  204. /package/dist/{chunk-WUORFPJ7.mjs.map → chunk-X2DNXTME.mjs.map} +0 -0
@@ -0,0 +1,543 @@
1
+ import { useRef, useEffect, useCallback, useState, useMemo } from "react";
2
+ import { AgentSession, useCopilotContext, CopilotContextParams } from "../context/copilot-context";
3
+ import { useCopilotMessagesContext, CopilotMessagesContextParams } from "../context";
4
+ import { SystemMessageFunction } from "../types";
5
+ import { useChat, AppendMessageOptions } from "./use-chat";
6
+ import { defaultCopilotContextCategories } from "../components";
7
+ import { CoAgentStateRenderHandlerArguments } from "@copilotkit/shared";
8
+ import { useAsyncCallback } from "../components/error-boundary/error-utils";
9
+ import { reloadSuggestions as generateSuggestions } from "../utils";
10
+ import type { SuggestionItem } from "../utils";
11
+
12
+ import { Message } from "@copilotkit/shared";
13
+ import {
14
+ Role as gqlRole,
15
+ TextMessage,
16
+ aguiToGQL,
17
+ gqlToAGUI,
18
+ Message as DeprecatedGqlMessage,
19
+ } from "@copilotkit/runtime-client-gql";
20
+ import { useLangGraphInterruptRender } from "./use-langgraph-interrupt-render";
21
+
22
+ export interface UseCopilotChatOptions {
23
+ /**
24
+ * A unique identifier for the chat. If not provided, a random one will be
25
+ * generated. When provided, the `useChat` hook with the same `id` will
26
+ * have shared states across components.
27
+ */
28
+ id?: string;
29
+
30
+ /**
31
+ * HTTP headers to be sent with the API request.
32
+ */
33
+ headers?: Record<string, string> | Headers;
34
+
35
+ /**
36
+ * Initial messages to populate the chat with.
37
+ */
38
+ initialMessages?: Message[];
39
+
40
+ /**
41
+ * A function to generate the system message. Defaults to `defaultSystemMessage`.
42
+ */
43
+ makeSystemMessage?: SystemMessageFunction;
44
+ }
45
+
46
+ export interface MCPServerConfig {
47
+ endpoint: string;
48
+ apiKey?: string;
49
+ }
50
+
51
+ export interface UseCopilotChatReturn {
52
+ /**
53
+ * @deprecated use `messages` instead, this is an old non ag-ui version of the messages
54
+ * Array of messages currently visible in the chat interface
55
+ *
56
+ * This is the visible messages, not the raw messages from the runtime client.
57
+ */
58
+ visibleMessages: DeprecatedGqlMessage[];
59
+
60
+ /**
61
+ * The messages that are currently in the chat in AG-UI format.
62
+ */
63
+ messages: Message[];
64
+
65
+ /** @deprecated use `sendMessage` instead */
66
+ appendMessage: (message: DeprecatedGqlMessage, options?: AppendMessageOptions) => Promise<void>;
67
+
68
+ /**
69
+ * Send a new message to the chat
70
+ *
71
+ * ```tsx
72
+ * await sendMessage({
73
+ * id: "123",
74
+ * role: "user",
75
+ * content: "Hello, process this request",
76
+ * });
77
+ * ```
78
+ */
79
+ sendMessage: (message: Message, options?: AppendMessageOptions) => Promise<void>;
80
+
81
+ /**
82
+ * Replace all messages in the chat
83
+ *
84
+ * ```tsx
85
+ * setMessages([
86
+ * { id: "123", role: "user", content: "Hello, process this request" },
87
+ * { id: "456", role: "assistant", content: "Hello, I'm the assistant" },
88
+ * ]);
89
+ * ```
90
+ *
91
+ * **Deprecated** non-ag-ui version:
92
+ *
93
+ * ```tsx
94
+ * setMessages([
95
+ * new TextMessage({
96
+ * content: "Hello, process this request",
97
+ * role: gqlRole.User,
98
+ * }),
99
+ * new TextMessage({
100
+ * content: "Hello, I'm the assistant",
101
+ * role: gqlRole.Assistant,
102
+ * ]);
103
+ * ```
104
+ *
105
+ */
106
+ setMessages: (messages: Message[] | DeprecatedGqlMessage[]) => void;
107
+
108
+ /**
109
+ * Remove a specific message by ID
110
+ *
111
+ * ```tsx
112
+ * deleteMessage("123");
113
+ * ```
114
+ */
115
+ deleteMessage: (messageId: string) => void;
116
+
117
+ /**
118
+ * Regenerate the response for a specific message
119
+ *
120
+ * ```tsx
121
+ * reloadMessages("123");
122
+ * ```
123
+ */
124
+ reloadMessages: (messageId: string) => Promise<void>;
125
+
126
+ /**
127
+ * Stop the current message generation
128
+ *
129
+ * ```tsx
130
+ * if (isLoading) {
131
+ * stopGeneration();
132
+ * }
133
+ * ```
134
+ */
135
+ stopGeneration: () => void;
136
+
137
+ /**
138
+ * Clear all messages and reset chat state
139
+ *
140
+ * ```tsx
141
+ * reset();
142
+ * console.log(messages); // []
143
+ * ```
144
+ */
145
+ reset: () => void;
146
+
147
+ /**
148
+ * Whether the chat is currently generating a response
149
+ *
150
+ * ```tsx
151
+ * if (isLoading) {
152
+ * console.log("Loading...");
153
+ * } else {
154
+ * console.log("Not loading");
155
+ * }
156
+ */
157
+ isLoading: boolean;
158
+
159
+ /** Manually trigger chat completion (advanced usage) */
160
+ runChatCompletion: () => Promise<Message[]>;
161
+
162
+ /** MCP (Model Context Protocol) server configurations */
163
+ mcpServers: MCPServerConfig[];
164
+
165
+ /** Update MCP server configurations */
166
+ setMcpServers: (mcpServers: MCPServerConfig[]) => void;
167
+
168
+ /**
169
+ * Current suggestions array
170
+ * Use this to read the current suggestions or in conjunction with setSuggestions for manual control
171
+ */
172
+ suggestions: SuggestionItem[];
173
+
174
+ /**
175
+ * Manually set suggestions
176
+ * Useful for manual mode or custom suggestion workflows
177
+ */
178
+ setSuggestions: (suggestions: SuggestionItem[]) => void;
179
+
180
+ /**
181
+ * Trigger AI-powered suggestion generation
182
+ * Uses configurations from useCopilotChatSuggestions hooks
183
+ * Respects global debouncing - only one generation can run at a time
184
+ *
185
+ * ```tsx
186
+ * generateSuggestions();
187
+ * console.log(suggestions); // [suggestion1, suggestion2, suggestion3]
188
+ * ```
189
+ */
190
+ generateSuggestions: () => Promise<void>;
191
+
192
+ /**
193
+ * Clear all current suggestions
194
+ * Also resets suggestion generation state
195
+ */
196
+ resetSuggestions: () => void;
197
+
198
+ /** Whether suggestions are currently being generated */
199
+ isLoadingSuggestions: boolean;
200
+
201
+ /** Interrupt content for human-in-the-loop workflows */
202
+ interrupt: string | React.ReactElement | null;
203
+ }
204
+
205
+ let globalSuggestionPromise: Promise<void> | null = null;
206
+
207
+ export function useCopilotChat(options: UseCopilotChatOptions = {}): UseCopilotChatReturn {
208
+ const makeSystemMessage = options.makeSystemMessage ?? defaultSystemMessage;
209
+ const {
210
+ getContextString,
211
+ getFunctionCallHandler,
212
+ copilotApiConfig,
213
+ isLoading,
214
+ setIsLoading,
215
+ chatInstructions,
216
+ actions,
217
+ coagentStatesRef,
218
+ setCoagentStatesWithRef,
219
+ coAgentStateRenders,
220
+ agentSession,
221
+ setAgentSession,
222
+ forwardedParameters,
223
+ agentLock,
224
+ threadId,
225
+ setThreadId,
226
+ runId,
227
+ setRunId,
228
+ chatAbortControllerRef,
229
+ extensions,
230
+ setExtensions,
231
+ langGraphInterruptAction,
232
+ setLangGraphInterruptAction,
233
+ chatSuggestionConfiguration,
234
+
235
+ runtimeClient,
236
+ } = useCopilotContext();
237
+ const { messages, setMessages, suggestions, setSuggestions } = useCopilotMessagesContext();
238
+
239
+ // Simple state for MCP servers (keep for interface compatibility)
240
+ const [mcpServers, setLocalMcpServers] = useState<MCPServerConfig[]>([]);
241
+
242
+ // Basic suggestion state for programmatic control
243
+ const suggestionsAbortControllerRef = useRef<AbortController | null>(null);
244
+ const isLoadingSuggestionsRef = useRef<boolean>(false);
245
+
246
+ const abortSuggestions = useCallback(
247
+ (clear: boolean = true) => {
248
+ suggestionsAbortControllerRef.current?.abort("suggestions aborted by user");
249
+ suggestionsAbortControllerRef.current = null;
250
+ if (clear) {
251
+ setSuggestions([]);
252
+ }
253
+ },
254
+ [setSuggestions],
255
+ );
256
+
257
+ // Memoize context with stable dependencies only
258
+ const stableContext = useMemo(() => {
259
+ return {
260
+ actions,
261
+ copilotApiConfig,
262
+ chatSuggestionConfiguration,
263
+ messages,
264
+ setMessages,
265
+ getContextString,
266
+ runtimeClient,
267
+ };
268
+ }, [
269
+ JSON.stringify(Object.keys(actions)),
270
+ copilotApiConfig.chatApiEndpoint,
271
+ messages.length,
272
+ Object.keys(chatSuggestionConfiguration).length,
273
+ ]);
274
+
275
+ // Programmatic suggestion generation function
276
+ const generateSuggestionsFunc = useCallback(async () => {
277
+ // If a global suggestion is running, ignore this call
278
+ if (globalSuggestionPromise) {
279
+ return globalSuggestionPromise;
280
+ }
281
+
282
+ globalSuggestionPromise = (async () => {
283
+ try {
284
+ abortSuggestions();
285
+ isLoadingSuggestionsRef.current = true;
286
+ suggestionsAbortControllerRef.current = new AbortController();
287
+
288
+ setSuggestions([]);
289
+
290
+ await generateSuggestions(
291
+ stableContext as CopilotContextParams & CopilotMessagesContextParams,
292
+ chatSuggestionConfiguration,
293
+ setSuggestions,
294
+ suggestionsAbortControllerRef,
295
+ );
296
+ } catch (error) {
297
+ // Re-throw to allow caller to handle the error
298
+ throw error;
299
+ } finally {
300
+ isLoadingSuggestionsRef.current = false;
301
+ globalSuggestionPromise = null;
302
+ }
303
+ })();
304
+
305
+ return globalSuggestionPromise;
306
+ }, [stableContext, chatSuggestionConfiguration, setSuggestions, abortSuggestions]);
307
+
308
+ const resetSuggestions = useCallback(() => {
309
+ setSuggestions([]);
310
+ }, [setSuggestions]);
311
+
312
+ // MCP servers logic
313
+ useEffect(() => {
314
+ if (mcpServers.length > 0) {
315
+ const serversCopy = [...mcpServers];
316
+ copilotApiConfig.mcpServers = serversCopy;
317
+ if (!copilotApiConfig.properties) {
318
+ copilotApiConfig.properties = {};
319
+ }
320
+ copilotApiConfig.properties.mcpServers = serversCopy;
321
+ }
322
+ }, [mcpServers, copilotApiConfig]);
323
+
324
+ const setMcpServers = useCallback((servers: MCPServerConfig[]) => {
325
+ setLocalMcpServers(servers);
326
+ }, []);
327
+
328
+ // Move these function declarations above the useChat call
329
+ const onCoAgentStateRender = useAsyncCallback(
330
+ async (args: CoAgentStateRenderHandlerArguments) => {
331
+ const { name, nodeName, state } = args;
332
+ let action = Object.values(coAgentStateRenders).find(
333
+ (action) => action.name === name && action.nodeName === nodeName,
334
+ );
335
+ if (!action) {
336
+ action = Object.values(coAgentStateRenders).find(
337
+ (action) => action.name === name && !action.nodeName,
338
+ );
339
+ }
340
+ if (action) {
341
+ await action.handler?.({ state, nodeName });
342
+ }
343
+ },
344
+ [coAgentStateRenders],
345
+ );
346
+
347
+ const makeSystemMessageCallback = useCallback(() => {
348
+ const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
349
+ // this always gets the latest context string
350
+ const contextString = getContextString([], defaultCopilotContextCategories); // TODO: make the context categories configurable
351
+
352
+ return new TextMessage({
353
+ content: systemMessageMaker(contextString, chatInstructions),
354
+ role: gqlRole.System,
355
+ });
356
+ }, [getContextString, makeSystemMessage, chatInstructions]);
357
+
358
+ const deleteMessage = useCallback(
359
+ (messageId: string) => {
360
+ setMessages((prev) => prev.filter((message) => message.id !== messageId));
361
+ },
362
+ [setMessages],
363
+ );
364
+
365
+ // Get chat helpers with updated config
366
+ const { append, reload, stop, runChatCompletion } = useChat({
367
+ ...options,
368
+ actions: Object.values(actions),
369
+ copilotConfig: copilotApiConfig,
370
+ initialMessages: aguiToGQL(options.initialMessages || []),
371
+ onFunctionCall: getFunctionCallHandler(),
372
+ onCoAgentStateRender,
373
+ messages,
374
+ setMessages,
375
+ makeSystemMessageCallback,
376
+ isLoading,
377
+ setIsLoading,
378
+ coagentStatesRef,
379
+ setCoagentStatesWithRef,
380
+ agentSession,
381
+ setAgentSession,
382
+ forwardedParameters,
383
+ threadId,
384
+ setThreadId,
385
+ runId,
386
+ setRunId,
387
+ chatAbortControllerRef,
388
+ agentLock,
389
+ extensions,
390
+ setExtensions,
391
+ langGraphInterruptAction,
392
+ setLangGraphInterruptAction,
393
+ });
394
+
395
+ const latestAppend = useUpdatedRef(append);
396
+ const latestAppendFunc = useAsyncCallback(
397
+ async (message: DeprecatedGqlMessage, options?: AppendMessageOptions) => {
398
+ abortSuggestions(options?.clearSuggestions);
399
+ return await latestAppend.current(message, options);
400
+ },
401
+ [latestAppend],
402
+ );
403
+
404
+ const latestSendMessageFunc = useAsyncCallback(
405
+ async (message: Message, options?: AppendMessageOptions) => {
406
+ abortSuggestions(options?.clearSuggestions);
407
+ return await latestAppend.current(aguiToGQL([message])[0] as DeprecatedGqlMessage, options);
408
+ },
409
+ [latestAppend],
410
+ );
411
+
412
+ const latestReload = useUpdatedRef(reload);
413
+ const latestReloadFunc = useAsyncCallback(
414
+ async (messageId: string) => {
415
+ return await latestReload.current(messageId);
416
+ },
417
+ [latestReload],
418
+ );
419
+
420
+ const latestStop = useUpdatedRef(stop);
421
+ const latestStopFunc = useCallback(() => {
422
+ return latestStop.current();
423
+ }, [latestStop]);
424
+
425
+ const latestDelete = useUpdatedRef(deleteMessage);
426
+ const latestDeleteFunc = useCallback(
427
+ (messageId: string) => {
428
+ return latestDelete.current(messageId);
429
+ },
430
+ [latestDelete],
431
+ );
432
+
433
+ const latestSetMessages = useUpdatedRef(setMessages);
434
+ const latestSetMessagesFunc = useCallback(
435
+ (messages: Message[] | DeprecatedGqlMessage[]) => {
436
+ if (messages.every((message) => message instanceof DeprecatedGqlMessage)) {
437
+ return latestSetMessages.current(messages as DeprecatedGqlMessage[]);
438
+ }
439
+ return latestSetMessages.current(aguiToGQL(messages));
440
+ },
441
+ [latestSetMessages],
442
+ );
443
+
444
+ const latestRunChatCompletion = useUpdatedRef(runChatCompletion);
445
+ const latestRunChatCompletionFunc = useAsyncCallback(async () => {
446
+ return await latestRunChatCompletion.current!();
447
+ }, [latestRunChatCompletion]);
448
+
449
+ const reset = useCallback(() => {
450
+ latestStopFunc();
451
+ setMessages([]);
452
+ setRunId(null);
453
+ setCoagentStatesWithRef({});
454
+ let initialAgentSession: AgentSession | null = null;
455
+ if (agentLock) {
456
+ initialAgentSession = {
457
+ agentName: agentLock,
458
+ };
459
+ }
460
+ setAgentSession(initialAgentSession);
461
+ // Reset suggestions when chat is reset
462
+ resetSuggestions();
463
+ }, [
464
+ latestStopFunc,
465
+ setMessages,
466
+ setThreadId,
467
+ setCoagentStatesWithRef,
468
+ setAgentSession,
469
+ agentLock,
470
+ resetSuggestions,
471
+ ]);
472
+
473
+ const latestReset = useUpdatedRef(reset);
474
+ const latestResetFunc = useCallback(() => {
475
+ return latestReset.current();
476
+ }, [latestReset]);
477
+
478
+ const interrupt = useLangGraphInterruptRender();
479
+
480
+ return {
481
+ visibleMessages: messages,
482
+ messages: gqlToAGUI(messages, actions, coAgentStateRenders),
483
+ sendMessage: latestSendMessageFunc,
484
+ appendMessage: latestAppendFunc,
485
+ setMessages: latestSetMessagesFunc,
486
+ reloadMessages: latestReloadFunc,
487
+ stopGeneration: latestStopFunc,
488
+ reset: latestResetFunc,
489
+ deleteMessage: latestDeleteFunc,
490
+ runChatCompletion: latestRunChatCompletionFunc,
491
+ isLoading,
492
+ mcpServers,
493
+ setMcpServers,
494
+ suggestions,
495
+ setSuggestions,
496
+ generateSuggestions: generateSuggestionsFunc,
497
+ resetSuggestions,
498
+ isLoadingSuggestions: isLoadingSuggestionsRef.current,
499
+ interrupt,
500
+ };
501
+ }
502
+
503
+ // store `value` in a ref and update
504
+ // it whenever it changes.
505
+ function useUpdatedRef<T>(value: T) {
506
+ const ref = useRef(value);
507
+
508
+ useEffect(() => {
509
+ ref.current = value;
510
+ }, [value]);
511
+
512
+ return ref;
513
+ }
514
+
515
+ export function defaultSystemMessage(
516
+ contextString: string,
517
+ additionalInstructions?: string,
518
+ ): string {
519
+ return (
520
+ `
521
+ Please act as an efficient, competent, conscientious, and industrious professional assistant.
522
+
523
+ Help 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.
524
+ Always be polite and respectful, and prefer brevity over verbosity.
525
+
526
+ The user has provided you with the following context:
527
+ \`\`\`
528
+ ${contextString}
529
+ \`\`\`
530
+
531
+ They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
532
+
533
+ Please assist them as best you can.
534
+
535
+ You 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.
536
+
537
+ If you would like to call a function, call it without saying anything else.
538
+ In case of a function error:
539
+ - If this error stems from incorrect function parameters or syntax, you may retry with corrected arguments.
540
+ - If the error's source is unclear or seems unrelated to your input, do not attempt further retries.
541
+ ` + (additionalInstructions ? `\n\n${additionalInstructions}` : "")
542
+ );
543
+ }
@@ -1,7 +1,7 @@
1
1
  import { useContext, useEffect, useMemo } from "react";
2
2
  import { CopilotContext } from "../context/copilot-context";
3
3
  import { LangGraphInterruptRender } from "../types/interrupt-action";
4
- import { useCopilotChat } from "./use-copilot-chat";
4
+ import { useCopilotChat } from "./use-copilot-chat_internal";
5
5
  import { useToast } from "../components/toast/toast-provider";
6
6
  import { dataToUUID } from "@copilotkit/shared";
7
7
 
@@ -1,3 +1,19 @@
1
- export function shouldShowDevConsole(showDevConsole: boolean): boolean {
2
- return showDevConsole;
1
+ function isLocalhost(): boolean {
2
+ if (typeof window === "undefined") return false;
3
+
4
+ return (
5
+ window.location.hostname === "localhost" ||
6
+ window.location.hostname === "127.0.0.1" ||
7
+ window.location.hostname === "0.0.0.0"
8
+ );
9
+ }
10
+
11
+ export function shouldShowDevConsole(showDevConsole?: boolean): boolean {
12
+ // If explicitly set, use that value
13
+ if (showDevConsole !== undefined) {
14
+ return showDevConsole;
15
+ }
16
+
17
+ // If not set, default to true on localhost
18
+ return isLocalhost();
3
19
  }