@copilotkit/react-core 1.10.0-next.3 → 1.10.0-next.5

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