@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
package/src/hooks/use-coagent.ts
CHANGED
|
@@ -89,19 +89,16 @@
|
|
|
89
89
|
*/
|
|
90
90
|
|
|
91
91
|
import { useCallback, useEffect, useMemo, useRef } from "react";
|
|
92
|
-
import {
|
|
93
|
-
CopilotContextParams,
|
|
94
|
-
CopilotMessagesContextParams,
|
|
95
|
-
useCopilotContext,
|
|
96
|
-
useCopilotMessagesContext,
|
|
97
|
-
} from "../context";
|
|
92
|
+
import { CopilotContextParams, useCopilotContext } from "../context";
|
|
98
93
|
import { CoagentState } from "../types/coagent-state";
|
|
99
|
-
import { useCopilotChat } from "./use-copilot-
|
|
94
|
+
import { useCopilotChat } from "./use-copilot-chat_internal";
|
|
100
95
|
import { Message } from "@copilotkit/shared";
|
|
101
96
|
import { useAsyncCallback } from "../components/error-boundary/error-utils";
|
|
102
97
|
import { useToast } from "../components/toast/toast-provider";
|
|
103
98
|
import { useCopilotRuntimeClient } from "./use-copilot-runtime-client";
|
|
104
99
|
import { parseJson, CopilotKitAgentDiscoveryError } from "@copilotkit/shared";
|
|
100
|
+
import { useMessagesTap } from "../components/copilot-provider/copilot-messages";
|
|
101
|
+
import { Message as GqlMessage } from "@copilotkit/runtime-client-gql";
|
|
105
102
|
|
|
106
103
|
interface UseCoagentOptionsBase {
|
|
107
104
|
/**
|
|
@@ -212,8 +209,8 @@ export type HintFunction = (params: HintFunctionParams) => Message | undefined;
|
|
|
212
209
|
* we refer to as CoAgents, checkout the documentation at https://docs.copilotkit.ai/coagents/quickstart/langgraph.
|
|
213
210
|
*/
|
|
214
211
|
export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentReturnType<T> {
|
|
215
|
-
const
|
|
216
|
-
const { availableAgents } =
|
|
212
|
+
const context = useCopilotContext();
|
|
213
|
+
const { availableAgents } = context;
|
|
217
214
|
const { setBannerError } = useToast();
|
|
218
215
|
const lastLoadedThreadId = useRef<string>();
|
|
219
216
|
const lastLoadedState = useRef<any>();
|
|
@@ -233,11 +230,11 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
|
|
|
233
230
|
}
|
|
234
231
|
}, [availableAgents]);
|
|
235
232
|
|
|
236
|
-
const
|
|
237
|
-
|
|
233
|
+
const { getMessagesFromTap } = useMessagesTap();
|
|
234
|
+
|
|
238
235
|
const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } =
|
|
239
236
|
context;
|
|
240
|
-
const {
|
|
237
|
+
const { sendMessage, runChatCompletion } = useCopilotChat();
|
|
241
238
|
const headers = {
|
|
242
239
|
...(copilotApiConfig.headers || {}),
|
|
243
240
|
};
|
|
@@ -349,9 +346,9 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
|
|
|
349
346
|
|
|
350
347
|
const runAgentCallback = useAsyncCallback(
|
|
351
348
|
async (hint?: HintFunction) => {
|
|
352
|
-
await runAgent(name, context,
|
|
349
|
+
await runAgent(name, context, getMessagesFromTap(), sendMessage, runChatCompletion, hint);
|
|
353
350
|
},
|
|
354
|
-
[name, context,
|
|
351
|
+
[name, context, sendMessage, runChatCompletion],
|
|
355
352
|
);
|
|
356
353
|
|
|
357
354
|
// Return the state and setState function
|
|
@@ -402,8 +399,9 @@ export function stopAgent(name: string, context: CopilotContextParams) {
|
|
|
402
399
|
|
|
403
400
|
export async function runAgent(
|
|
404
401
|
name: string,
|
|
405
|
-
context: CopilotContextParams
|
|
406
|
-
|
|
402
|
+
context: CopilotContextParams,
|
|
403
|
+
messages: GqlMessage[],
|
|
404
|
+
sendMessage: (message: Message) => Promise<void>,
|
|
407
405
|
runChatCompletion: () => Promise<Message[]>,
|
|
408
406
|
hint?: HintFunction,
|
|
409
407
|
) {
|
|
@@ -415,8 +413,8 @@ export async function runAgent(
|
|
|
415
413
|
}
|
|
416
414
|
|
|
417
415
|
let previousState: any = null;
|
|
418
|
-
for (let i =
|
|
419
|
-
const message =
|
|
416
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
417
|
+
const message = messages[i];
|
|
420
418
|
if (message.isAgentStateMessage() && message.agentName === name) {
|
|
421
419
|
previousState = message.state;
|
|
422
420
|
}
|
|
@@ -427,7 +425,7 @@ export async function runAgent(
|
|
|
427
425
|
if (hint) {
|
|
428
426
|
const hintMessage = hint({ previousState, currentState: state });
|
|
429
427
|
if (hintMessage) {
|
|
430
|
-
await
|
|
428
|
+
await sendMessage(hintMessage);
|
|
431
429
|
} else {
|
|
432
430
|
await runChatCompletion();
|
|
433
431
|
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useCopilotChatHeadless_c` is for building fully custom UI (headless UI) implementations.
|
|
3
|
+
*
|
|
4
|
+
* <Callout type="info" title="This is a premium feature">
|
|
5
|
+
* Use of `useCopilotchatHeadless_c` requires a public license key.
|
|
6
|
+
*
|
|
7
|
+
* Sign up for free at https://cloud.copilotkit.ai/ to get your public license key.
|
|
8
|
+
*
|
|
9
|
+
* Usage is generous, free to get started, and easily slots into existing applications, either self-hosted or using Copilot Cloud.
|
|
10
|
+
* </Callout>
|
|
11
|
+
*
|
|
12
|
+
* ## Key Features:
|
|
13
|
+
*
|
|
14
|
+
* - **Fully headless**: Build your own fully custom UI's for your agentic applications.
|
|
15
|
+
* - **Advanced Suggestions**: Direct access to suggestions array with full control
|
|
16
|
+
* - **Interrupt Handling**: Support for advanced interrupt functionality
|
|
17
|
+
* - **MCP Server Support**: Model Context Protocol server configurations
|
|
18
|
+
* - **Chat Controls**: Complete set of chat management functions
|
|
19
|
+
* - **Loading States**: Comprehensive loading state management
|
|
20
|
+
*
|
|
21
|
+
* ## Usage:
|
|
22
|
+
*
|
|
23
|
+
* ### Basic Setup
|
|
24
|
+
*
|
|
25
|
+
* ```tsx
|
|
26
|
+
* import { CopilotKit } from "@copilotkit/react-core";
|
|
27
|
+
* import { useCopilotChatHeadless_c } from "@copilotkit/react-core";
|
|
28
|
+
*
|
|
29
|
+
* export function App() {
|
|
30
|
+
* return (
|
|
31
|
+
* <CopilotKit publicApiKey="your-free-subscription-key">
|
|
32
|
+
* <YourComponent />
|
|
33
|
+
* </CopilotKit>
|
|
34
|
+
* );
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* export function YourComponent() {
|
|
38
|
+
* const { messages, sendMessage, isLoading } = useCopilotChatHeadless_c();
|
|
39
|
+
*
|
|
40
|
+
* const handleSendMessage = async () => {
|
|
41
|
+
* await sendMessage({
|
|
42
|
+
* id: "123",
|
|
43
|
+
* role: "user",
|
|
44
|
+
* content: "Hello World",
|
|
45
|
+
* });
|
|
46
|
+
* };
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <div>
|
|
50
|
+
* {messages.map(msg => <div key={msg.id}>{msg.content}</div>)}
|
|
51
|
+
* <button onClick={handleSendMessage} disabled={isLoading}>
|
|
52
|
+
* Send Message
|
|
53
|
+
* </button>
|
|
54
|
+
* </div>
|
|
55
|
+
* );
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* ### Working with Suggestions
|
|
60
|
+
*
|
|
61
|
+
* ```tsx
|
|
62
|
+
* import { useCopilotChatHeadless_c, useCopilotChatSuggestions } from "@copilotkit/react-core";
|
|
63
|
+
*
|
|
64
|
+
* export function SuggestionExample() {
|
|
65
|
+
* const {
|
|
66
|
+
* suggestions,
|
|
67
|
+
* setSuggestions,
|
|
68
|
+
* generateSuggestions,
|
|
69
|
+
* isLoadingSuggestions
|
|
70
|
+
* } = useCopilotChatHeadless_c();
|
|
71
|
+
*
|
|
72
|
+
* // Configure AI suggestion generation
|
|
73
|
+
* useCopilotChatSuggestions({
|
|
74
|
+
* instructions: "Suggest helpful actions based on the current context",
|
|
75
|
+
* maxSuggestions: 3
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* return (
|
|
79
|
+
* <div>
|
|
80
|
+
* {suggestions.map(suggestion => (
|
|
81
|
+
* <button key={suggestion.title}>{suggestion.title}</button>
|
|
82
|
+
* ))}
|
|
83
|
+
* <button onClick={generateSuggestions} disabled={isLoadingSuggestions}>
|
|
84
|
+
* Generate Suggestions
|
|
85
|
+
* </button>
|
|
86
|
+
* </div>
|
|
87
|
+
* );
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
import { useEffect } from "react";
|
|
92
|
+
import { useCopilotContext } from "../context/copilot-context";
|
|
93
|
+
import {
|
|
94
|
+
useCopilotChat as useCopilotChatInternal,
|
|
95
|
+
defaultSystemMessage,
|
|
96
|
+
UseCopilotChatOptions as UseCopilotChatOptions_c,
|
|
97
|
+
UseCopilotChatReturn as UseCopilotChatReturn_c,
|
|
98
|
+
MCPServerConfig,
|
|
99
|
+
} from "./use-copilot-chat_internal";
|
|
100
|
+
|
|
101
|
+
import {
|
|
102
|
+
ErrorVisibility,
|
|
103
|
+
Severity,
|
|
104
|
+
CopilotKitError,
|
|
105
|
+
CopilotKitErrorCode,
|
|
106
|
+
styledConsole,
|
|
107
|
+
} from "@copilotkit/shared";
|
|
108
|
+
|
|
109
|
+
// Non-functional fallback implementation
|
|
110
|
+
const createNonFunctionalReturn = (): UseCopilotChatReturn_c => ({
|
|
111
|
+
visibleMessages: [],
|
|
112
|
+
messages: [],
|
|
113
|
+
sendMessage: async () => {},
|
|
114
|
+
appendMessage: async () => {},
|
|
115
|
+
setMessages: () => {},
|
|
116
|
+
deleteMessage: () => {},
|
|
117
|
+
reloadMessages: async () => {},
|
|
118
|
+
stopGeneration: () => {},
|
|
119
|
+
reset: () => {},
|
|
120
|
+
isLoading: false,
|
|
121
|
+
runChatCompletion: async () => [],
|
|
122
|
+
mcpServers: [],
|
|
123
|
+
setMcpServers: () => {},
|
|
124
|
+
suggestions: [],
|
|
125
|
+
setSuggestions: () => {},
|
|
126
|
+
generateSuggestions: async () => {},
|
|
127
|
+
resetSuggestions: () => {},
|
|
128
|
+
isLoadingSuggestions: false,
|
|
129
|
+
interrupt: null,
|
|
130
|
+
});
|
|
131
|
+
/**
|
|
132
|
+
* Enterprise React hook that provides complete chat functionality for fully custom UI implementations.
|
|
133
|
+
* Includes all advanced features like direct message access, suggestions array, interrupt handling, and MCP support.
|
|
134
|
+
*
|
|
135
|
+
* **Requires a publicApiKey** - Sign up for free at https://cloud.copilotkit.ai/
|
|
136
|
+
*
|
|
137
|
+
* @param options - Configuration options for the chat
|
|
138
|
+
* @returns Complete chat interface with all enterprise features
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```tsx
|
|
142
|
+
* const { messages, sendMessage, suggestions, interrupt } = useCopilotChatHeadless_c();
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
function useCopilotChatHeadless_c(options: UseCopilotChatOptions_c = {}): UseCopilotChatReturn_c {
|
|
146
|
+
const { copilotApiConfig, setBannerError } = useCopilotContext();
|
|
147
|
+
|
|
148
|
+
// Check if publicApiKey is available
|
|
149
|
+
const hasPublicApiKey = Boolean(copilotApiConfig.publicApiKey);
|
|
150
|
+
|
|
151
|
+
// Always call the internal hook (follows rules of hooks)
|
|
152
|
+
const internalResult = useCopilotChatInternal(options);
|
|
153
|
+
|
|
154
|
+
// Set banner error when no public API key is provided
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
if (!hasPublicApiKey) {
|
|
157
|
+
setBannerError(
|
|
158
|
+
new CopilotKitError({
|
|
159
|
+
message:
|
|
160
|
+
// add link to documentation here
|
|
161
|
+
"You're using useCopilotChatHeadless_c, a subscription-only feature, which offers extensive headless chat capabilities. To continue, you'll need to provide a free subscription key.",
|
|
162
|
+
code: CopilotKitErrorCode.MISSING_PUBLIC_API_KEY_ERROR,
|
|
163
|
+
severity: Severity.WARNING,
|
|
164
|
+
visibility: ErrorVisibility.BANNER,
|
|
165
|
+
}),
|
|
166
|
+
);
|
|
167
|
+
styledConsole.logCopilotKitPlatformMessage();
|
|
168
|
+
} else {
|
|
169
|
+
setBannerError(null); // Clear banner when API key is provided
|
|
170
|
+
}
|
|
171
|
+
}, [hasPublicApiKey]); // Removed setBannerError dependency
|
|
172
|
+
|
|
173
|
+
// Return internal result if publicApiKey is available, otherwise return fallback
|
|
174
|
+
if (hasPublicApiKey) {
|
|
175
|
+
return internalResult;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Return non-functional fallback when no publicApiKey
|
|
179
|
+
return createNonFunctionalReturn();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export { defaultSystemMessage, useCopilotChatHeadless_c };
|
|
183
|
+
export type { UseCopilotChatOptions_c, UseCopilotChatReturn_c, MCPServerConfig };
|
|
184
|
+
|
|
185
|
+
const noKeyWarning = () => {
|
|
186
|
+
styledConsole.logCopilotKitPlatformMessage();
|
|
187
|
+
};
|