@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.
- package/CHANGELOG.md +16 -0
- package/dist/{chunk-VM7CVIET.mjs → chunk-32JPCD6C.mjs} +10 -10
- package/dist/chunk-32JPCD6C.mjs.map +1 -0
- package/dist/{chunk-XGRBCWK6.mjs → chunk-47GMU44Z.mjs} +4 -1
- package/dist/chunk-47GMU44Z.mjs.map +1 -0
- package/dist/{chunk-PYULBXCD.mjs → chunk-4NR26FFD.mjs} +8 -8
- package/dist/{chunk-DF4YG4PF.mjs → chunk-57MBVQPX.mjs} +2 -2
- package/dist/chunk-5RPZ4T2V.mjs +18 -0
- package/dist/chunk-5RPZ4T2V.mjs.map +1 -0
- package/dist/{chunk-JZQOCH4A.mjs → chunk-A3QJUCBZ.mjs} +4 -4
- package/dist/{chunk-LNAQ7JG3.mjs → chunk-AC2GVFW6.mjs} +2 -2
- package/dist/chunk-AWFAFFO5.mjs +221 -0
- package/dist/chunk-AWFAFFO5.mjs.map +1 -0
- package/dist/chunk-BJ7N5YF5.mjs +79 -0
- package/dist/chunk-BJ7N5YF5.mjs.map +1 -0
- package/dist/{chunk-KV25ZRMH.mjs → chunk-CZWJ4VJG.mjs} +6 -6
- package/dist/chunk-CZWJ4VJG.mjs.map +1 -0
- package/dist/{chunk-YAF2LATQ.mjs → chunk-EFL5OBKN.mjs} +1 -1
- package/dist/chunk-EFL5OBKN.mjs.map +1 -0
- package/dist/{chunk-4CFY3CON.mjs → chunk-EQQSE6KM.mjs} +2 -2
- package/dist/{chunk-JWAXDYOW.mjs → chunk-FAUNHSQU.mjs} +3 -3
- package/dist/chunk-ICIK2BSB.mjs +17 -0
- package/dist/chunk-ICIK2BSB.mjs.map +1 -0
- package/dist/chunk-KDAZGZ24.mjs +1 -0
- package/dist/{chunk-NXCJELW7.mjs → chunk-KTD5U4PK.mjs} +3 -3
- package/dist/{chunk-3OQM3NEK.mjs → chunk-N4WEHORG.mjs} +2 -2
- package/dist/{chunk-Q6FZZJ5A.mjs → chunk-OL3VRQKU.mjs} +2 -2
- package/dist/{chunk-WUORFPJ7.mjs → chunk-PH6RVMFU.mjs} +6 -6
- package/dist/{chunk-RGKZCCPA.mjs → chunk-SMLWBM4N.mjs} +5 -5
- package/dist/{chunk-OMVNJ7S3.mjs → chunk-TUPJR242.mjs} +36 -21
- package/dist/chunk-TUPJR242.mjs.map +1 -0
- package/dist/{chunk-SGF6C7I6.mjs → chunk-U6JSCZCE.mjs} +11 -16
- package/dist/chunk-U6JSCZCE.mjs.map +1 -0
- package/dist/{chunk-4XVBXDCX.mjs → chunk-X7ETNW3P.mjs} +8 -8
- package/dist/{chunk-VOMGRGWT.mjs → chunk-XGRBVNW2.mjs} +12 -12
- package/dist/chunk-XGRBVNW2.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.js +12 -1
- package/dist/components/copilot-provider/copilot-messages.js.map +1 -1
- package/dist/components/copilot-provider/copilot-messages.mjs +4 -4
- package/dist/components/copilot-provider/copilotkit-props.d.ts +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +1 -1
- package/dist/components/copilot-provider/copilotkit.js +307 -550
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +10 -10
- package/dist/components/copilot-provider/index.d.ts +1 -1
- package/dist/components/copilot-provider/index.js +307 -550
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +10 -10
- package/dist/components/error-boundary/error-boundary.js +172 -323
- package/dist/components/error-boundary/error-boundary.js.map +1 -1
- package/dist/components/error-boundary/error-boundary.mjs +4 -4
- package/dist/components/error-boundary/error-utils.js.map +1 -1
- package/dist/components/error-boundary/error-utils.mjs +2 -2
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.js +307 -550
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +10 -10
- package/dist/components/toast/toast-provider.js.map +1 -1
- package/dist/components/toast/toast-provider.mjs +1 -1
- package/dist/components/usage-banner.d.ts +8 -3
- package/dist/components/usage-banner.js +172 -302
- package/dist/components/usage-banner.js.map +1 -1
- package/dist/components/usage-banner.mjs +3 -1
- package/dist/context/copilot-context.d.ts +1 -1
- package/dist/context/copilot-context.js +3 -0
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.js +3 -0
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +4 -4
- package/dist/{copilot-context-bd88d30d.d.ts → copilot-context-78fc727c.d.ts} +3 -1
- package/dist/hooks/index.d.ts +5 -3
- package/dist/hooks/index.js +143 -57
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +42 -34
- package/dist/hooks/use-chat.d.ts +1 -1
- package/dist/hooks/use-chat.js +12 -1
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +6 -6
- package/dist/hooks/use-coagent-state-render.js +3 -0
- package/dist/hooks/use-coagent-state-render.js.map +1 -1
- package/dist/hooks/use-coagent-state-render.mjs +3 -3
- package/dist/hooks/use-coagent.d.ts +1 -1
- package/dist/hooks/use-coagent.js +15 -4
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +16 -16
- package/dist/hooks/use-copilot-action.js +3 -0
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +4 -4
- package/dist/hooks/use-copilot-additional-instructions.js +3 -0
- package/dist/hooks/use-copilot-additional-instructions.js.map +1 -1
- package/dist/hooks/use-copilot-additional-instructions.mjs +2 -2
- package/dist/hooks/use-copilot-authenticated-action.js +3 -0
- package/dist/hooks/use-copilot-authenticated-action.js.map +1 -1
- package/dist/hooks/use-copilot-authenticated-action.mjs +5 -5
- package/dist/hooks/use-copilot-chat-light.d.ts +211 -0
- package/dist/hooks/use-copilot-chat-light.js +1770 -0
- package/dist/hooks/use-copilot-chat-light.js.map +1 -0
- package/dist/hooks/use-copilot-chat-light.mjs +28 -0
- package/dist/hooks/use-copilot-chat-light.mjs.map +1 -0
- package/dist/hooks/use-copilot-chat.d.ts +9 -81
- package/dist/hooks/use-copilot-chat.js +77 -5
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +18 -16
- package/dist/hooks/use-copilot-chat_internal.d.ts +91 -0
- package/dist/hooks/use-copilot-chat_internal.js +1760 -0
- package/dist/hooks/use-copilot-chat_internal.js.map +1 -0
- package/dist/hooks/use-copilot-chat_internal.mjs +29 -0
- package/dist/hooks/use-copilot-chat_internal.mjs.map +1 -0
- package/dist/hooks/use-copilot-readable.js +3 -0
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- package/dist/hooks/use-copilot-runtime-client.js +9 -1
- package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
- package/dist/hooks/use-copilot-runtime-client.mjs +3 -3
- package/dist/hooks/use-langgraph-interrupt-render.js +3 -0
- package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt-render.mjs +2 -2
- package/dist/hooks/use-langgraph-interrupt.d.ts +1 -1
- package/dist/hooks/use-langgraph-interrupt.js +15 -4
- package/dist/hooks/use-langgraph-interrupt.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt.mjs +16 -16
- package/dist/hooks/use-make-copilot-document-readable.js +3 -0
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +384 -449
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +47 -39
- package/dist/lib/copilot-task.d.ts +1 -1
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +12 -12
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +12 -12
- package/dist/types/interrupt-action.d.ts +1 -1
- package/dist/utils/dev-console.d.ts +1 -1
- package/dist/utils/dev-console.js +9 -1
- package/dist/utils/dev-console.js.map +1 -1
- package/dist/utils/dev-console.mjs +1 -1
- package/dist/utils/extract.d.ts +1 -1
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +10 -10
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +9 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +11 -11
- package/dist/utils/suggestions.d.ts +1 -1
- package/dist/utils/suggestions.js.map +1 -1
- package/dist/utils/suggestions.mjs +10 -10
- package/package.json +3 -3
- package/src/components/copilot-provider/copilotkit.tsx +16 -5
- package/src/components/error-boundary/error-boundary.tsx +5 -15
- package/src/components/toast/toast-provider.tsx +1 -3
- package/src/components/usage-banner.tsx +187 -315
- package/src/context/copilot-context.tsx +12 -1
- package/src/hooks/__tests__/use-coagent-config.test.ts +1 -1
- package/src/hooks/index.ts +6 -0
- package/src/hooks/use-coagent.ts +1 -1
- package/src/hooks/use-copilot-chat-light.ts +219 -0
- package/src/hooks/use-copilot-chat.ts +89 -430
- package/src/hooks/use-copilot-chat_internal.ts +437 -0
- package/src/hooks/use-langgraph-interrupt.ts +1 -1
- package/src/utils/dev-console.ts +18 -2
- package/dist/chunk-57K2ZJ5F.mjs +0 -348
- package/dist/chunk-57K2ZJ5F.mjs.map +0 -1
- package/dist/chunk-CQPYJIBH.mjs +0 -1
- package/dist/chunk-GFJW4RIM.mjs +0 -9
- package/dist/chunk-GFJW4RIM.mjs.map +0 -1
- package/dist/chunk-KV25ZRMH.mjs.map +0 -1
- package/dist/chunk-OMVNJ7S3.mjs.map +0 -1
- package/dist/chunk-SGF6C7I6.mjs.map +0 -1
- package/dist/chunk-VM7CVIET.mjs.map +0 -1
- package/dist/chunk-VOMGRGWT.mjs.map +0 -1
- package/dist/chunk-XGRBCWK6.mjs.map +0 -1
- package/dist/chunk-YAF2LATQ.mjs.map +0 -1
- /package/dist/{chunk-PYULBXCD.mjs.map → chunk-4NR26FFD.mjs.map} +0 -0
- /package/dist/{chunk-DF4YG4PF.mjs.map → chunk-57MBVQPX.mjs.map} +0 -0
- /package/dist/{chunk-JZQOCH4A.mjs.map → chunk-A3QJUCBZ.mjs.map} +0 -0
- /package/dist/{chunk-LNAQ7JG3.mjs.map → chunk-AC2GVFW6.mjs.map} +0 -0
- /package/dist/{chunk-4CFY3CON.mjs.map → chunk-EQQSE6KM.mjs.map} +0 -0
- /package/dist/{chunk-JWAXDYOW.mjs.map → chunk-FAUNHSQU.mjs.map} +0 -0
- /package/dist/{chunk-CQPYJIBH.mjs.map → chunk-KDAZGZ24.mjs.map} +0 -0
- /package/dist/{chunk-NXCJELW7.mjs.map → chunk-KTD5U4PK.mjs.map} +0 -0
- /package/dist/{chunk-3OQM3NEK.mjs.map → chunk-N4WEHORG.mjs.map} +0 -0
- /package/dist/{chunk-Q6FZZJ5A.mjs.map → chunk-OL3VRQKU.mjs.map} +0 -0
- /package/dist/{chunk-WUORFPJ7.mjs.map → chunk-PH6RVMFU.mjs.map} +0 -0
- /package/dist/{chunk-RGKZCCPA.mjs.map → chunk-SMLWBM4N.mjs.map} +0 -0
- /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 {
|
|
84
|
-
import {
|
|
85
|
-
|
|
86
|
-
import {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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 (
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
168
|
+
// Return internal result if publicApiKey is available, otherwise return fallback
|
|
169
|
+
if (hasPublicApiKey) {
|
|
170
|
+
return internalResult;
|
|
171
|
+
}
|
|
483
172
|
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
}, [value]);
|
|
487
|
-
|
|
488
|
-
return ref;
|
|
173
|
+
// Return non-functional fallback when no publicApiKey
|
|
174
|
+
return createNonFunctionalReturn();
|
|
489
175
|
}
|
|
490
176
|
|
|
491
|
-
export
|
|
492
|
-
|
|
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 };
|