@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.
- package/CHANGELOG.md +87 -0
- package/dist/{chunk-KV25ZRMH.mjs → chunk-2TSNHEIS.mjs} +6 -6
- package/dist/chunk-2TSNHEIS.mjs.map +1 -0
- package/dist/{chunk-DF4YG4PF.mjs → chunk-3RHHNUVV.mjs} +2 -2
- package/dist/{chunk-LNAQ7JG3.mjs → chunk-6EKLRL7B.mjs} +2 -2
- package/dist/{chunk-JZQOCH4A.mjs → chunk-ADZDXHVC.mjs} +4 -4
- package/dist/{chunk-4CFY3CON.mjs → chunk-CLMDRYEN.mjs} +2 -2
- package/dist/{chunk-VM7CVIET.mjs → chunk-COEUPDRL.mjs} +20 -21
- package/dist/chunk-COEUPDRL.mjs.map +1 -0
- package/dist/{chunk-RGKZCCPA.mjs → chunk-DLEXVOQE.mjs} +5 -5
- package/dist/{chunk-YAF2LATQ.mjs → chunk-EFL5OBKN.mjs} +1 -1
- package/dist/chunk-EFL5OBKN.mjs.map +1 -0
- 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-IHAZJF3V.mjs +34 -0
- package/dist/chunk-IHAZJF3V.mjs.map +1 -0
- package/dist/{chunk-Q6FZZJ5A.mjs → chunk-IN7GE4NO.mjs} +2 -2
- package/dist/{chunk-4XVBXDCX.mjs → chunk-JBLMXZ3O.mjs} +8 -8
- package/dist/{chunk-NXCJELW7.mjs → chunk-JJDXTTEN.mjs} +3 -3
- package/dist/chunk-KDAZGZ24.mjs +1 -0
- package/dist/{chunk-VOMGRGWT.mjs → chunk-L6HQIJ74.mjs} +33 -17
- package/dist/chunk-L6HQIJ74.mjs.map +1 -0
- package/dist/{chunk-OMVNJ7S3.mjs → chunk-LVWV62JZ.mjs} +37 -24
- package/dist/chunk-LVWV62JZ.mjs.map +1 -0
- package/dist/{chunk-3OQM3NEK.mjs → chunk-N4WEHORG.mjs} +2 -2
- package/dist/chunk-OKRZF3DD.mjs +225 -0
- package/dist/chunk-OKRZF3DD.mjs.map +1 -0
- package/dist/{chunk-SGF6C7I6.mjs → chunk-Q42NJFXR.mjs} +11 -16
- package/dist/chunk-Q42NJFXR.mjs.map +1 -0
- package/dist/chunk-QGE7U4NV.mjs +85 -0
- package/dist/chunk-QGE7U4NV.mjs.map +1 -0
- package/dist/{chunk-XGRBCWK6.mjs → chunk-TEMLWRRT.mjs} +3 -3
- package/dist/chunk-TEMLWRRT.mjs.map +1 -0
- package/dist/{chunk-PYULBXCD.mjs → chunk-TWYUYC4F.mjs} +44 -10
- package/dist/chunk-TWYUYC4F.mjs.map +1 -0
- package/dist/{chunk-WUORFPJ7.mjs → chunk-X2DNXTME.mjs} +6 -6
- package/dist/{chunk-DCTJZ742.mjs → chunk-ZLQVRPDS.mjs} +5 -2
- package/dist/chunk-ZLQVRPDS.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-messages.d.ts +17 -1
- package/dist/components/copilot-provider/copilot-messages.js +46 -7
- package/dist/components/copilot-provider/copilot-messages.js.map +1 -1
- package/dist/components/copilot-provider/copilot-messages.mjs +11 -7
- package/dist/components/copilot-provider/copilotkit-props.d.ts +5 -3
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.d.ts +1 -2
- package/dist/components/copilot-provider/copilotkit.js +337 -555
- 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 -2
- package/dist/components/copilot-provider/index.js +337 -555
- 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 +176 -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 -2
- package/dist/components/index.js +337 -555
- 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 +10 -4
- package/dist/components/usage-banner.js +176 -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 -2
- package/dist/context/copilot-context.js +2 -2
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/copilot-messages-context.d.ts +11 -15
- package/dist/context/copilot-messages-context.js +4 -1
- package/dist/context/copilot-messages-context.js.map +1 -1
- package/dist/context/copilot-messages-context.mjs +1 -1
- package/dist/context/index.d.ts +2 -2
- package/dist/context/index.js +6 -3
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +5 -5
- package/dist/{copilot-context-bd88d30d.d.ts → copilot-context-256f9020.d.ts} +4 -22
- package/dist/hooks/index.d.ts +7 -5
- package/dist/hooks/index.js +250 -122
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +42 -34
- package/dist/hooks/use-chat.d.ts +1 -2
- package/dist/hooks/use-chat.js +11 -3
- 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 +2 -2
- 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 +3 -4
- package/dist/hooks/use-coagent.js +114 -81
- package/dist/hooks/use-coagent.js.map +1 -1
- package/dist/hooks/use-coagent.mjs +16 -16
- package/dist/hooks/use-copilot-action.js +2 -2
- 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 +2 -2
- 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 +2 -2
- 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-headless_c.d.ts +33 -0
- package/dist/hooks/use-copilot-chat-headless_c.js +1837 -0
- package/dist/hooks/use-copilot-chat-headless_c.js.map +1 -0
- package/dist/hooks/use-copilot-chat-headless_c.mjs +31 -0
- package/dist/hooks/use-copilot-chat-headless_c.mjs.map +1 -0
- package/dist/hooks/use-copilot-chat.d.ts +52 -82
- package/dist/hooks/use-copilot-chat.js +60 -13
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +16 -17
- package/dist/hooks/use-copilot-chat_internal.d.ts +177 -0
- package/dist/hooks/use-copilot-chat_internal.js +1770 -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 +2 -2
- 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 +2 -2
- package/dist/hooks/use-langgraph-interrupt-render.js.map +1 -1
- package/dist/hooks/use-langgraph-interrupt-render.mjs +3 -3
- package/dist/hooks/use-langgraph-interrupt.d.ts +3 -4
- package/dist/hooks/use-langgraph-interrupt.js +32 -11
- 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 +2 -2
- 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-08c43df1.d.ts +36 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +460 -471
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +47 -39
- package/dist/lib/copilot-task.d.ts +1 -2
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +12 -12
- package/dist/lib/index.d.ts +1 -2
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +12 -12
- package/dist/types/interrupt-action.d.ts +2 -3
- 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 +2 -2
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +10 -10
- package/dist/utils/index.d.ts +3 -3
- 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 +2 -2
- 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/copilot-messages.tsx +57 -1
- package/src/components/copilot-provider/copilotkit-props.tsx +4 -1
- package/src/components/copilot-provider/copilotkit.tsx +20 -10
- 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 +193 -316
- package/src/context/copilot-context.tsx +12 -6
- package/src/context/copilot-messages-context.tsx +7 -1
- package/src/hooks/__tests__/use-coagent-config.test.ts +8 -1
- package/src/hooks/index.ts +7 -2
- package/src/hooks/use-coagent.ts +17 -19
- package/src/hooks/use-copilot-chat-headless_c.ts +187 -0
- package/src/hooks/use-copilot-chat.ts +64 -495
- package/src/hooks/use-copilot-chat_internal.ts +543 -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-DCTJZ742.mjs.map +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-PYULBXCD.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-DF4YG4PF.mjs.map → chunk-3RHHNUVV.mjs.map} +0 -0
- /package/dist/{chunk-LNAQ7JG3.mjs.map → chunk-6EKLRL7B.mjs.map} +0 -0
- /package/dist/{chunk-JZQOCH4A.mjs.map → chunk-ADZDXHVC.mjs.map} +0 -0
- /package/dist/{chunk-4CFY3CON.mjs.map → chunk-CLMDRYEN.mjs.map} +0 -0
- /package/dist/{chunk-RGKZCCPA.mjs.map → chunk-DLEXVOQE.mjs.map} +0 -0
- /package/dist/{chunk-JWAXDYOW.mjs.map → chunk-FAUNHSQU.mjs.map} +0 -0
- /package/dist/{chunk-Q6FZZJ5A.mjs.map → chunk-IN7GE4NO.mjs.map} +0 -0
- /package/dist/{chunk-4XVBXDCX.mjs.map → chunk-JBLMXZ3O.mjs.map} +0 -0
- /package/dist/{chunk-NXCJELW7.mjs.map → chunk-JJDXTTEN.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-WUORFPJ7.mjs.map → chunk-X2DNXTME.mjs.map} +0 -0
|
@@ -1,519 +1,88 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `useCopilotChat` is a React hook
|
|
3
|
-
*
|
|
4
|
-
* programmatically interact with the Copilot instance managed by the default
|
|
5
|
-
* UI.
|
|
2
|
+
* `useCopilotChat` is a lightweight React hook for headless chat interactions.
|
|
3
|
+
* Perfect for controlling the prebuilt chat components programmatically.
|
|
6
4
|
*
|
|
7
|
-
*
|
|
5
|
+
* **Open Source Friendly** - Works without requiring a free subscription key.
|
|
8
6
|
*
|
|
9
|
-
*
|
|
7
|
+
* <Callout title="Looking for fully headless UI?">
|
|
8
|
+
* Get started with [useCopilotChatHeadless_c](https://docs.copilotkit.ai/reference/hooks/useCopilotChatHeadless_c).
|
|
9
|
+
* </Callout>
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
* import { useCopilotChat } from "@copilotkit/react-core";
|
|
13
|
-
* import { Role, TextMessage } from "@copilotkit/runtime-client-gql";
|
|
14
|
-
*
|
|
15
|
-
* export function YourComponent() {
|
|
16
|
-
* const { appendMessage } = useCopilotChat();
|
|
17
|
-
*
|
|
18
|
-
* appendMessage(
|
|
19
|
-
* new TextMessage({
|
|
20
|
-
* content: "Hello World",
|
|
21
|
-
* role: Role.User,
|
|
22
|
-
* }),
|
|
23
|
-
* );
|
|
11
|
+
* ## Use Cases:
|
|
24
12
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
13
|
+
* - **Programmatic Messaging**: Send messages without displaying chat UI
|
|
14
|
+
* - **Programmatic control**: Control prebuilt component programmatically
|
|
15
|
+
* - **Background Operations**: Trigger AI interactions in the background
|
|
16
|
+
* - **Fire-and-Forget**: Send messages without needing to read responses
|
|
29
17
|
*
|
|
30
|
-
*
|
|
18
|
+
* ## Usage:
|
|
31
19
|
*
|
|
32
20
|
* ```tsx
|
|
33
|
-
* import {
|
|
34
|
-
*
|
|
35
|
-
* export function YourComponent() {
|
|
36
|
-
* const {
|
|
37
|
-
* suggestions,
|
|
38
|
-
* setSuggestions,
|
|
39
|
-
* generateSuggestions,
|
|
40
|
-
* isLoadingSuggestions
|
|
41
|
-
* } = useCopilotChat();
|
|
42
|
-
*
|
|
43
|
-
* // Configure AI suggestion generation
|
|
44
|
-
* useCopilotChatSuggestions({
|
|
45
|
-
* instructions: "Suggest helpful actions based on the current context",
|
|
46
|
-
* maxSuggestions: 3
|
|
47
|
-
* });
|
|
48
|
-
*
|
|
49
|
-
* // Manual suggestion control
|
|
50
|
-
* const handleCustomSuggestion = () => {
|
|
51
|
-
* setSuggestions([{ title: "Custom Action", message: "Perform custom action" }]);
|
|
52
|
-
* };
|
|
53
|
-
*
|
|
54
|
-
* // Trigger AI generation
|
|
55
|
-
* const handleGenerateSuggestions = async () => {
|
|
56
|
-
* await generateSuggestions();
|
|
57
|
-
* };
|
|
58
|
-
* }
|
|
59
|
-
* ```
|
|
21
|
+
* import { TextMessage, MessageRole } from "@copilotkit/runtime-client-gql";
|
|
60
22
|
*
|
|
61
|
-
*
|
|
23
|
+
* const { appendMessage } = useCopilotChat();
|
|
62
24
|
*
|
|
63
|
-
*
|
|
64
|
-
* const {
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* isLoading, // A boolean indicating if the chat is loading.
|
|
73
|
-
*
|
|
74
|
-
* // Suggestion control (headless UI)
|
|
75
|
-
* suggestions, // Current suggestions array
|
|
76
|
-
* setSuggestions, // Manually set suggestions
|
|
77
|
-
* generateSuggestions, // Trigger AI suggestion generation
|
|
78
|
-
* resetSuggestions, // Clear all suggestions
|
|
79
|
-
* isLoadingSuggestions, // Whether suggestions are being generated
|
|
80
|
-
* } = useCopilotChat();
|
|
25
|
+
* // Example usage without naming conflicts
|
|
26
|
+
* const handleSendMessage = async (content: string) => {
|
|
27
|
+
* await appendMessage(
|
|
28
|
+
* new TextMessage({
|
|
29
|
+
* role: MessageRole.User,
|
|
30
|
+
* content,
|
|
31
|
+
* })
|
|
32
|
+
* );
|
|
33
|
+
* };
|
|
81
34
|
* ```
|
|
82
35
|
*/
|
|
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
36
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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;
|
|
37
|
+
import {
|
|
38
|
+
UseCopilotChatOptions,
|
|
39
|
+
useCopilotChat as useCopilotChatInternal,
|
|
40
|
+
UseCopilotChatReturn as UseCopilotChatReturnInternal,
|
|
41
|
+
} from "./use-copilot-chat_internal";
|
|
42
|
+
|
|
43
|
+
// Create a type that excludes message-related properties from the internal type
|
|
44
|
+
export type UseCopilotChatReturn = Omit<
|
|
45
|
+
UseCopilotChatReturnInternal,
|
|
46
|
+
| "messages"
|
|
47
|
+
| "sendMessage"
|
|
48
|
+
| "suggestions"
|
|
49
|
+
| "setSuggestions"
|
|
50
|
+
| "generateSuggestions"
|
|
51
|
+
| "isLoadingSuggestions"
|
|
52
|
+
| "resetSuggestions"
|
|
53
|
+
| "interrupt"
|
|
54
|
+
| "setMessages"
|
|
55
|
+
| "deleteMessage"
|
|
56
|
+
>;
|
|
194
57
|
|
|
58
|
+
/**
|
|
59
|
+
* A lightweight React hook for headless chat interactions.
|
|
60
|
+
* Perfect for programmatic messaging, background operations, and custom UI implementations.
|
|
61
|
+
*
|
|
62
|
+
* **Open Source Friendly** - Works without requiring a `publicApiKey`.
|
|
63
|
+
*/
|
|
195
64
|
export function useCopilotChat(options: UseCopilotChatOptions = {}): UseCopilotChatReturn {
|
|
196
|
-
const makeSystemMessage = options.makeSystemMessage ?? defaultSystemMessage;
|
|
197
65
|
const {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
66
|
+
visibleMessages,
|
|
67
|
+
appendMessage,
|
|
68
|
+
reloadMessages,
|
|
69
|
+
stopGeneration,
|
|
70
|
+
reset,
|
|
201
71
|
isLoading,
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
|
302
|
-
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,
|
|
323
|
-
);
|
|
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
|
-
};
|
|
437
|
-
}
|
|
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();
|
|
72
|
+
runChatCompletion,
|
|
73
|
+
mcpServers,
|
|
74
|
+
setMcpServers,
|
|
75
|
+
} = useCopilotChatInternal(options);
|
|
457
76
|
|
|
458
77
|
return {
|
|
459
|
-
visibleMessages
|
|
460
|
-
appendMessage
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
reset: latestResetFunc,
|
|
465
|
-
deleteMessage: latestDeleteFunc,
|
|
466
|
-
runChatCompletion: latestRunChatCompletionFunc,
|
|
78
|
+
visibleMessages,
|
|
79
|
+
appendMessage,
|
|
80
|
+
reloadMessages,
|
|
81
|
+
stopGeneration,
|
|
82
|
+
reset,
|
|
467
83
|
isLoading,
|
|
84
|
+
runChatCompletion,
|
|
468
85
|
mcpServers,
|
|
469
86
|
setMcpServers,
|
|
470
|
-
suggestions,
|
|
471
|
-
setSuggestions,
|
|
472
|
-
generateSuggestions: generateSuggestionsFunc,
|
|
473
|
-
resetSuggestions,
|
|
474
|
-
isLoadingSuggestions: isLoadingSuggestionsRef.current,
|
|
475
|
-
interrupt,
|
|
476
87
|
};
|
|
477
88
|
}
|
|
478
|
-
|
|
479
|
-
// store `value` in a ref and update
|
|
480
|
-
// it whenever it changes.
|
|
481
|
-
function useUpdatedRef<T>(value: T) {
|
|
482
|
-
const ref = useRef(value);
|
|
483
|
-
|
|
484
|
-
useEffect(() => {
|
|
485
|
-
ref.current = value;
|
|
486
|
-
}, [value]);
|
|
487
|
-
|
|
488
|
-
return ref;
|
|
489
|
-
}
|
|
490
|
-
|
|
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
|
-
}
|