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