@copilotkit/react-core 1.54.1 → 1.55.0-next.7
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 +117 -116
- package/dist/copilotkit-B3Mb1yVE.cjs +7975 -0
- package/dist/copilotkit-B3Mb1yVE.cjs.map +1 -0
- package/dist/copilotkit-DBzgOMby.d.cts +2182 -0
- package/dist/copilotkit-DBzgOMby.d.cts.map +1 -0
- package/dist/copilotkit-DNYSFuz5.mjs +7562 -0
- package/dist/copilotkit-DNYSFuz5.mjs.map +1 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts +2182 -0
- package/dist/copilotkit-Dy5w3qEV.d.mts.map +1 -0
- package/dist/index.cjs +27 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -5
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1941 -35
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/index.cjs +77 -7
- package/dist/v2/index.css +1 -2
- package/dist/v2/index.d.cts +6 -4
- package/dist/v2/index.d.mts +6 -4
- package/dist/v2/index.mjs +7 -4
- package/dist/v2/index.umd.js +5725 -24
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +37 -9
- package/scripts/scope-preflight.mjs +101 -0
- package/src/components/CopilotListeners.tsx +2 -6
- package/src/components/copilot-provider/copilot-messages.tsx +1 -1
- package/src/components/copilot-provider/copilotkit-props.tsx +1 -1
- package/src/components/copilot-provider/copilotkit.tsx +4 -4
- package/src/context/copilot-messages-context.tsx +1 -1
- package/src/hooks/__tests__/use-coagent-config.test.ts +2 -2
- package/src/hooks/__tests__/use-coagent-state-render.e2e.test.tsx +2 -2
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +3 -7
- package/src/hooks/__tests__/use-frontend-tool-available.test.tsx +1 -1
- package/src/hooks/__tests__/use-frontend-tool-remount.e2e.test.tsx +4 -4
- package/src/hooks/use-agent-nodename.ts +1 -1
- package/src/hooks/use-coagent-state-render-bridge.tsx +1 -4
- package/src/hooks/use-coagent.ts +1 -1
- package/src/hooks/use-configure-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat-suggestions.tsx +2 -2
- package/src/hooks/use-copilot-chat_internal.ts +2 -2
- package/src/hooks/use-copilot-readable.ts +1 -1
- package/src/hooks/use-frontend-tool.ts +2 -2
- package/src/hooks/use-human-in-the-loop.ts +2 -2
- package/src/hooks/use-langgraph-interrupt.ts +2 -5
- package/src/hooks/use-lazy-tool-renderer.tsx +1 -1
- package/src/hooks/use-render-tool-call.ts +1 -1
- package/src/lib/copilot-task.ts +1 -1
- package/src/setupTests.ts +18 -14
- package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +176 -0
- package/src/v2/__tests__/globalSetup.ts +14 -0
- package/src/v2/__tests__/setup.ts +93 -0
- package/src/v2/__tests__/utils/test-helpers.tsx +470 -0
- package/src/v2/a2ui/A2UIMessageRenderer.tsx +206 -0
- package/src/v2/components/CopilotKitInspector.tsx +50 -0
- package/src/v2/components/MCPAppsActivityRenderer.tsx +785 -0
- package/src/v2/components/WildcardToolCallRender.tsx +86 -0
- package/src/v2/components/__tests__/license-warning-banner.test.tsx +46 -0
- package/src/v2/components/chat/CopilotChat.tsx +431 -0
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +375 -0
- package/src/v2/components/chat/CopilotChatAudioRecorder.tsx +350 -0
- package/src/v2/components/chat/CopilotChatInput.tsx +1302 -0
- package/src/v2/components/chat/CopilotChatMessageView.tsx +556 -0
- package/src/v2/components/chat/CopilotChatReasoningMessage.tsx +252 -0
- package/src/v2/components/chat/CopilotChatSuggestionPill.tsx +59 -0
- package/src/v2/components/chat/CopilotChatSuggestionView.tsx +133 -0
- package/src/v2/components/chat/CopilotChatToggleButton.tsx +171 -0
- package/src/v2/components/chat/CopilotChatToolCallsView.tsx +40 -0
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +388 -0
- package/src/v2/components/chat/CopilotChatView.tsx +598 -0
- package/src/v2/components/chat/CopilotModalHeader.tsx +129 -0
- package/src/v2/components/chat/CopilotPopup.tsx +81 -0
- package/src/v2/components/chat/CopilotPopupView.tsx +317 -0
- package/src/v2/components/chat/CopilotSidebar.tsx +76 -0
- package/src/v2/components/chat/CopilotSidebarView.tsx +255 -0
- package/src/v2/components/chat/__tests__/CopilotChat.e2e.test.tsx +1113 -0
- package/src/v2/components/chat/__tests__/CopilotChat.onError.test.tsx +73 -0
- package/src/v2/components/chat/__tests__/CopilotChat.slots.e2e.test.tsx +432 -0
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +150 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.slots.e2e.test.tsx +624 -0
- package/src/v2/components/chat/__tests__/CopilotChatAssistantMessage.test.tsx +702 -0
- package/src/v2/components/chat/__tests__/CopilotChatCssClasses.test.tsx +107 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.slots.e2e.test.tsx +929 -0
- package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +986 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.slots.e2e.test.tsx +1004 -0
- package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx +169 -0
- package/src/v2/components/chat/__tests__/CopilotChatSuggestionView.slots.e2e.test.tsx +530 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +782 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +2413 -0
- package/src/v2/components/chat/__tests__/CopilotChatUserMessage.slots.e2e.test.tsx +621 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.onClick.e2e.test.tsx +853 -0
- package/src/v2/components/chat/__tests__/CopilotChatView.slots.e2e.test.tsx +1050 -0
- package/src/v2/components/chat/__tests__/CopilotModalHeader.slots.e2e.test.tsx +484 -0
- package/src/v2/components/chat/__tests__/CopilotPopupView.slots.e2e.test.tsx +612 -0
- package/src/v2/components/chat/__tests__/CopilotSidebarView.slots.e2e.test.tsx +502 -0
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +1011 -0
- package/src/v2/components/chat/__tests__/setup.ts +1 -0
- package/src/v2/components/chat/index.ts +79 -0
- package/src/v2/components/index.ts +7 -0
- package/src/v2/components/license-warning-banner.tsx +198 -0
- package/src/v2/components/ui/button.tsx +123 -0
- package/src/v2/components/ui/dropdown-menu.tsx +258 -0
- package/src/v2/components/ui/tooltip.tsx +60 -0
- package/src/v2/hooks/__tests__/standard-schema-types.test.tsx +152 -0
- package/src/v2/hooks/__tests__/standard-schema.test.tsx +282 -0
- package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +132 -0
- package/src/v2/hooks/__tests__/use-agent-context.test.tsx +401 -0
- package/src/v2/hooks/__tests__/use-agent-error-state.test.tsx +44 -0
- package/src/v2/hooks/__tests__/use-agent-stability.test.tsx +205 -0
- package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +148 -0
- package/src/v2/hooks/__tests__/use-component.test.tsx +123 -0
- package/src/v2/hooks/__tests__/use-configure-suggestions.e2e.test.tsx +696 -0
- package/src/v2/hooks/__tests__/use-default-render-tool.test.tsx +153 -0
- package/src/v2/hooks/__tests__/use-frontend-tool-available.test.tsx +167 -0
- package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +2129 -0
- package/src/v2/hooks/__tests__/use-human-in-the-loop.e2e.test.tsx +1261 -0
- package/src/v2/hooks/__tests__/use-interrupt.test.tsx +397 -0
- package/src/v2/hooks/__tests__/use-katex-styles.test.tsx +56 -0
- package/src/v2/hooks/__tests__/use-keyboard-height.test.tsx +192 -0
- package/src/v2/hooks/__tests__/use-render-tool.test.tsx +259 -0
- package/src/v2/hooks/__tests__/use-suggestions.e2e.test.tsx +524 -0
- package/src/v2/hooks/__tests__/use-threads.test.tsx +433 -0
- package/src/v2/hooks/__tests__/zod-regression.test.tsx +311 -0
- package/src/v2/hooks/index.ts +18 -0
- package/src/v2/hooks/use-agent-context.tsx +45 -0
- package/src/v2/hooks/use-agent.tsx +155 -0
- package/src/v2/hooks/use-component.tsx +89 -0
- package/src/v2/hooks/use-configure-suggestions.tsx +187 -0
- package/src/v2/hooks/use-default-render-tool.tsx +254 -0
- package/src/v2/hooks/use-frontend-tool.tsx +43 -0
- package/src/v2/hooks/use-human-in-the-loop.tsx +81 -0
- package/src/v2/hooks/use-interrupt.tsx +305 -0
- package/src/v2/hooks/use-keyboard-height.tsx +67 -0
- package/src/v2/hooks/use-render-activity-message.tsx +73 -0
- package/src/v2/hooks/use-render-custom-messages.tsx +93 -0
- package/src/v2/hooks/use-render-tool-call.tsx +175 -0
- package/src/v2/hooks/use-render-tool.tsx +181 -0
- package/src/v2/hooks/use-suggestions.tsx +91 -0
- package/src/v2/hooks/use-threads.tsx +256 -0
- package/src/v2/hooks/useKatexStyles.ts +27 -0
- package/src/v2/index.css +1 -1
- package/src/v2/index.ts +18 -2
- package/src/v2/lib/__tests__/completePartialMarkdown.test.ts +495 -0
- package/src/v2/lib/__tests__/renderSlot.test.tsx +588 -0
- package/src/v2/lib/react-core.ts +156 -0
- package/src/v2/lib/slots.tsx +143 -0
- package/src/v2/lib/transcription-client.ts +184 -0
- package/src/v2/lib/utils.ts +8 -0
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +162 -0
- package/src/v2/providers/CopilotKitProvider.tsx +600 -0
- package/src/v2/providers/__tests__/CopilotChatConfigurationProvider.test.tsx +546 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.license.test.tsx +101 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.onError.test.tsx +69 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.renderCustomMessages.e2e.test.tsx +881 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.stability.test.tsx +740 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +642 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx +294 -0
- package/src/v2/providers/index.ts +14 -0
- package/src/v2/styles/globals.css +230 -0
- package/src/v2/types/__tests__/defineToolCallRenderer.test.tsx +525 -0
- package/src/v2/types/defineToolCallRenderer.ts +65 -0
- package/src/v2/types/frontend-tool.ts +8 -0
- package/src/v2/types/human-in-the-loop.ts +33 -0
- package/src/v2/types/index.ts +7 -0
- package/src/v2/types/interrupt.ts +15 -0
- package/src/v2/types/react-activity-message-renderer.ts +27 -0
- package/src/v2/types/react-custom-message-renderer.ts +17 -0
- package/src/v2/types/react-tool-call-renderer.ts +32 -0
- package/tsdown.config.ts +34 -10
- package/vitest.config.mjs +4 -3
- package/LICENSE +0 -21
- package/dist/copilotkit-BRPQ2sqS.d.cts +0 -670
- package/dist/copilotkit-BRPQ2sqS.d.cts.map +0 -1
- package/dist/copilotkit-C94ayZbs.cjs +0 -2161
- package/dist/copilotkit-C94ayZbs.cjs.map +0 -1
- package/dist/copilotkit-CwZMFmSK.d.mts +0 -670
- package/dist/copilotkit-CwZMFmSK.d.mts.map +0 -1
- package/dist/copilotkit-Yh_Ld_FX.mjs +0 -2031
- package/dist/copilotkit-Yh_Ld_FX.mjs.map +0 -1
- package/dist/v2/index.css.map +0 -1
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
4
|
+
import type { FrontendTool } from "@copilotkit/core";
|
|
5
|
+
import type React from "react";
|
|
6
|
+
import {
|
|
7
|
+
createContext,
|
|
8
|
+
useContext,
|
|
9
|
+
type ReactNode,
|
|
10
|
+
useMemo,
|
|
11
|
+
useEffect,
|
|
12
|
+
useReducer,
|
|
13
|
+
useRef,
|
|
14
|
+
useState,
|
|
15
|
+
} from "react";
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { CopilotKitInspector } from "../components/CopilotKitInspector";
|
|
18
|
+
import { LicenseWarningBanner } from "../components/license-warning-banner";
|
|
19
|
+
import {
|
|
20
|
+
createLicenseContextValue,
|
|
21
|
+
type LicenseContextValue,
|
|
22
|
+
} from "@copilotkit/shared";
|
|
23
|
+
import type { CopilotKitCoreErrorCode } from "@copilotkit/core";
|
|
24
|
+
import {
|
|
25
|
+
MCPAppsActivityContentSchema,
|
|
26
|
+
MCPAppsActivityRenderer,
|
|
27
|
+
MCPAppsActivityType,
|
|
28
|
+
} from "../components/MCPAppsActivityRenderer";
|
|
29
|
+
import { createA2UIMessageRenderer } from "../a2ui/A2UIMessageRenderer";
|
|
30
|
+
import { viewerTheme } from "@copilotkit/a2ui-renderer";
|
|
31
|
+
import type { Theme as A2UITheme } from "@copilotkit/a2ui-renderer";
|
|
32
|
+
import { CopilotKitCoreReact } from "../lib/react-core";
|
|
33
|
+
import type {
|
|
34
|
+
ReactActivityMessageRenderer,
|
|
35
|
+
ReactToolCallRenderer,
|
|
36
|
+
} from "../types";
|
|
37
|
+
import type { ReactFrontendTool } from "../types/frontend-tool";
|
|
38
|
+
import type { ReactHumanInTheLoop } from "../types/human-in-the-loop";
|
|
39
|
+
import type { ReactCustomMessageRenderer } from "../types/react-custom-message-renderer";
|
|
40
|
+
|
|
41
|
+
const HEADER_NAME = "X-CopilotCloud-Public-Api-Key";
|
|
42
|
+
const COPILOT_CLOUD_CHAT_URL = "https://api.cloud.copilotkit.ai/copilotkit/v1";
|
|
43
|
+
|
|
44
|
+
// Define the context value interface - idiomatic React naming
|
|
45
|
+
export interface CopilotKitContextValue {
|
|
46
|
+
copilotkit: CopilotKitCoreReact;
|
|
47
|
+
/**
|
|
48
|
+
* Set of tool call IDs currently being executed.
|
|
49
|
+
* This is tracked at the provider level to ensure tool execution events
|
|
50
|
+
* are captured even before child components mount.
|
|
51
|
+
*/
|
|
52
|
+
executingToolCallIds: ReadonlySet<string>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Empty set for default context value
|
|
56
|
+
const EMPTY_SET: ReadonlySet<string> = new Set();
|
|
57
|
+
|
|
58
|
+
// Create the CopilotKit context
|
|
59
|
+
const CopilotKitContext = createContext<CopilotKitContextValue>({
|
|
60
|
+
copilotkit: null!,
|
|
61
|
+
executingToolCallIds: EMPTY_SET,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const LicenseContext = createContext<LicenseContextValue>(
|
|
65
|
+
createLicenseContextValue(null),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
export const useLicenseContext = (): LicenseContextValue =>
|
|
69
|
+
useContext(LicenseContext);
|
|
70
|
+
|
|
71
|
+
// Provider props interface
|
|
72
|
+
export interface CopilotKitProviderProps {
|
|
73
|
+
children: ReactNode;
|
|
74
|
+
runtimeUrl?: string;
|
|
75
|
+
headers?: Record<string, string>;
|
|
76
|
+
/**
|
|
77
|
+
* Credentials mode for fetch requests (e.g., "include" for HTTP-only cookies in cross-origin requests).
|
|
78
|
+
*/
|
|
79
|
+
credentials?: RequestCredentials;
|
|
80
|
+
/**
|
|
81
|
+
* The Copilot Cloud public API key.
|
|
82
|
+
*/
|
|
83
|
+
publicApiKey?: string;
|
|
84
|
+
/**
|
|
85
|
+
* Alias for `publicApiKey`
|
|
86
|
+
**/
|
|
87
|
+
publicLicenseKey?: string;
|
|
88
|
+
/**
|
|
89
|
+
* Signed license token for offline verification of premium features.
|
|
90
|
+
* Obtain from https://cloud.copilotkit.ai.
|
|
91
|
+
*/
|
|
92
|
+
licenseToken?: string;
|
|
93
|
+
properties?: Record<string, unknown>;
|
|
94
|
+
useSingleEndpoint?: boolean;
|
|
95
|
+
agents__unsafe_dev_only?: Record<string, AbstractAgent>;
|
|
96
|
+
selfManagedAgents?: Record<string, AbstractAgent>;
|
|
97
|
+
renderToolCalls?: ReactToolCallRenderer<any>[];
|
|
98
|
+
renderActivityMessages?: ReactActivityMessageRenderer<any>[];
|
|
99
|
+
renderCustomMessages?: ReactCustomMessageRenderer[];
|
|
100
|
+
frontendTools?: ReactFrontendTool[];
|
|
101
|
+
humanInTheLoop?: ReactHumanInTheLoop[];
|
|
102
|
+
showDevConsole?: boolean | "auto";
|
|
103
|
+
/**
|
|
104
|
+
* Error handler called when CopilotKit encounters an error.
|
|
105
|
+
* Fires for all error types (runtime connection failures, agent errors, tool errors).
|
|
106
|
+
*/
|
|
107
|
+
onError?: (event: {
|
|
108
|
+
error: Error;
|
|
109
|
+
code: CopilotKitCoreErrorCode;
|
|
110
|
+
context: Record<string, any>;
|
|
111
|
+
}) => void | Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Configuration for the A2UI (Agent-to-UI) renderer.
|
|
114
|
+
* The built-in A2UI renderer is activated automatically when the runtime reports
|
|
115
|
+
* that `a2ui` is configured in `CopilotRuntime`. This prop is optional and only
|
|
116
|
+
* needed if you want to override the default theme.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```tsx
|
|
120
|
+
* <CopilotKit runtimeUrl="/api/copilotkit" a2ui={{ theme: myCustomTheme }}>
|
|
121
|
+
* {children}
|
|
122
|
+
* </CopilotKit>
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
a2ui?: {
|
|
126
|
+
/**
|
|
127
|
+
* Override the default A2UI viewer theme.
|
|
128
|
+
* When omitted, the built-in `viewerTheme` from `@copilotkit/a2ui-renderer` is used.
|
|
129
|
+
*/
|
|
130
|
+
theme?: A2UITheme;
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Small helper to normalize array props to a stable reference and warn
|
|
135
|
+
function useStableArrayProp<T>(
|
|
136
|
+
prop: T[] | undefined,
|
|
137
|
+
warningMessage?: string,
|
|
138
|
+
isMeaningfulChange?: (initial: T[], next: T[]) => boolean,
|
|
139
|
+
): T[] {
|
|
140
|
+
const empty = useMemo<T[]>(() => [], []);
|
|
141
|
+
const value = prop ?? empty;
|
|
142
|
+
const initial = useRef(value);
|
|
143
|
+
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
if (
|
|
146
|
+
warningMessage &&
|
|
147
|
+
value !== initial.current &&
|
|
148
|
+
(isMeaningfulChange ? isMeaningfulChange(initial.current, value) : true)
|
|
149
|
+
) {
|
|
150
|
+
console.error(warningMessage);
|
|
151
|
+
}
|
|
152
|
+
}, [value, warningMessage]);
|
|
153
|
+
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Provider component
|
|
158
|
+
export const CopilotKitProvider: React.FC<CopilotKitProviderProps> = ({
|
|
159
|
+
children,
|
|
160
|
+
runtimeUrl,
|
|
161
|
+
headers = {},
|
|
162
|
+
credentials,
|
|
163
|
+
publicApiKey,
|
|
164
|
+
publicLicenseKey,
|
|
165
|
+
licenseToken,
|
|
166
|
+
properties = {},
|
|
167
|
+
agents__unsafe_dev_only: agents = {},
|
|
168
|
+
selfManagedAgents = {},
|
|
169
|
+
renderToolCalls,
|
|
170
|
+
renderActivityMessages,
|
|
171
|
+
renderCustomMessages,
|
|
172
|
+
frontendTools,
|
|
173
|
+
humanInTheLoop,
|
|
174
|
+
showDevConsole = false,
|
|
175
|
+
useSingleEndpoint = false,
|
|
176
|
+
onError,
|
|
177
|
+
a2ui,
|
|
178
|
+
}) => {
|
|
179
|
+
const [shouldRenderInspector, setShouldRenderInspector] = useState(false);
|
|
180
|
+
const [runtimeA2UIEnabled, setRuntimeA2UIEnabled] = useState(false);
|
|
181
|
+
const [runtimeLicenseStatus, setRuntimeLicenseStatus] = useState<
|
|
182
|
+
string | undefined
|
|
183
|
+
>(undefined);
|
|
184
|
+
|
|
185
|
+
useEffect(() => {
|
|
186
|
+
if (typeof window === "undefined") {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (showDevConsole === true) {
|
|
191
|
+
// Explicitly show the inspector
|
|
192
|
+
setShouldRenderInspector(true);
|
|
193
|
+
} else if (showDevConsole === "auto") {
|
|
194
|
+
// Show on localhost or 127.0.0.1 only
|
|
195
|
+
const localhostHosts = new Set(["localhost", "127.0.0.1"]);
|
|
196
|
+
if (localhostHosts.has(window.location.hostname)) {
|
|
197
|
+
setShouldRenderInspector(true);
|
|
198
|
+
} else {
|
|
199
|
+
setShouldRenderInspector(false);
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
// showDevConsole is false or undefined (default false)
|
|
203
|
+
setShouldRenderInspector(false);
|
|
204
|
+
}
|
|
205
|
+
}, [showDevConsole]);
|
|
206
|
+
|
|
207
|
+
// Normalize array props to stable references with clear dev warnings
|
|
208
|
+
const renderToolCallsList = useStableArrayProp<ReactToolCallRenderer<any>>(
|
|
209
|
+
renderToolCalls,
|
|
210
|
+
"renderToolCalls must be a stable array. If you want to dynamically add or remove tools, use `useFrontendTool` instead.",
|
|
211
|
+
(initial, next) => {
|
|
212
|
+
// Only warn if the shape (names+agentId) changed. Allow identity changes
|
|
213
|
+
// to support updated closures from parents (e.g., Storybook state).
|
|
214
|
+
const key = (rc?: ReactToolCallRenderer<unknown>) =>
|
|
215
|
+
`${rc?.agentId ?? ""}:${rc?.name ?? ""}`;
|
|
216
|
+
const setFrom = (arr: ReactToolCallRenderer<unknown>[]) =>
|
|
217
|
+
new Set(arr.map(key));
|
|
218
|
+
const a = setFrom(initial);
|
|
219
|
+
const b = setFrom(next);
|
|
220
|
+
if (a.size !== b.size) return true;
|
|
221
|
+
for (const k of a) if (!b.has(k)) return true;
|
|
222
|
+
return false;
|
|
223
|
+
},
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
const renderCustomMessagesList =
|
|
227
|
+
useStableArrayProp<ReactCustomMessageRenderer>(
|
|
228
|
+
renderCustomMessages,
|
|
229
|
+
"renderCustomMessages must be a stable array.",
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const renderActivityMessagesList = useStableArrayProp<
|
|
233
|
+
ReactActivityMessageRenderer<any>
|
|
234
|
+
>(renderActivityMessages, "renderActivityMessages must be a stable array.");
|
|
235
|
+
|
|
236
|
+
// Built-in activity renderers that are always included
|
|
237
|
+
const builtInActivityRenderers = useMemo<
|
|
238
|
+
ReactActivityMessageRenderer<any>[]
|
|
239
|
+
>(() => {
|
|
240
|
+
const renderers: ReactActivityMessageRenderer<any>[] = [
|
|
241
|
+
{
|
|
242
|
+
activityType: MCPAppsActivityType,
|
|
243
|
+
content: MCPAppsActivityContentSchema,
|
|
244
|
+
render: MCPAppsActivityRenderer,
|
|
245
|
+
},
|
|
246
|
+
];
|
|
247
|
+
|
|
248
|
+
if (runtimeA2UIEnabled) {
|
|
249
|
+
renderers.unshift(
|
|
250
|
+
createA2UIMessageRenderer({ theme: a2ui?.theme ?? viewerTheme }),
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return renderers;
|
|
255
|
+
}, [runtimeA2UIEnabled, a2ui]);
|
|
256
|
+
|
|
257
|
+
// Combine user-provided activity renderers with built-in ones
|
|
258
|
+
// User-provided renderers take precedence (come first) so they can override built-ins
|
|
259
|
+
const allActivityRenderers = useMemo(() => {
|
|
260
|
+
return [...renderActivityMessagesList, ...builtInActivityRenderers];
|
|
261
|
+
}, [renderActivityMessagesList, builtInActivityRenderers]);
|
|
262
|
+
|
|
263
|
+
const resolvedPublicKey = publicApiKey ?? publicLicenseKey;
|
|
264
|
+
const mergedAgents = useMemo(
|
|
265
|
+
() => ({ ...agents, ...selfManagedAgents }),
|
|
266
|
+
[agents, selfManagedAgents],
|
|
267
|
+
);
|
|
268
|
+
const hasLocalAgents = mergedAgents && Object.keys(mergedAgents).length > 0;
|
|
269
|
+
|
|
270
|
+
// Merge a provided publicApiKey into headers (without overwriting an explicit header).
|
|
271
|
+
const mergedHeaders = useMemo(() => {
|
|
272
|
+
if (!resolvedPublicKey) return headers;
|
|
273
|
+
if (headers[HEADER_NAME]) return headers;
|
|
274
|
+
return {
|
|
275
|
+
...headers,
|
|
276
|
+
[HEADER_NAME]: resolvedPublicKey,
|
|
277
|
+
};
|
|
278
|
+
}, [headers, resolvedPublicKey]);
|
|
279
|
+
|
|
280
|
+
if (!runtimeUrl && !resolvedPublicKey && !hasLocalAgents) {
|
|
281
|
+
const message =
|
|
282
|
+
"Missing required prop: 'runtimeUrl' or 'publicApiKey' or 'publicLicenseKey'";
|
|
283
|
+
if (process.env.NODE_ENV === "production") {
|
|
284
|
+
throw new Error(message);
|
|
285
|
+
} else {
|
|
286
|
+
// In dev/test we warn but allow to facilitate local agents and unit tests.
|
|
287
|
+
console.warn(message);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const chatApiEndpoint =
|
|
292
|
+
runtimeUrl ?? (resolvedPublicKey ? COPILOT_CLOUD_CHAT_URL : undefined);
|
|
293
|
+
|
|
294
|
+
const frontendToolsList = useStableArrayProp<ReactFrontendTool>(
|
|
295
|
+
frontendTools,
|
|
296
|
+
"frontendTools must be a stable array. If you want to dynamically add or remove tools, use `useFrontendTool` instead.",
|
|
297
|
+
);
|
|
298
|
+
const humanInTheLoopList = useStableArrayProp<ReactHumanInTheLoop>(
|
|
299
|
+
humanInTheLoop,
|
|
300
|
+
"humanInTheLoop must be a stable array. If you want to dynamically add or remove human-in-the-loop tools, use `useHumanInTheLoop` instead.",
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
// Note: warnings for array identity changes are handled by useStableArrayProp
|
|
304
|
+
|
|
305
|
+
// Process humanInTheLoop tools to create handlers and add render components
|
|
306
|
+
const processedHumanInTheLoopTools = useMemo(() => {
|
|
307
|
+
const processedTools: FrontendTool[] = [];
|
|
308
|
+
const processedRenderToolCalls: ReactToolCallRenderer<unknown>[] = [];
|
|
309
|
+
|
|
310
|
+
humanInTheLoopList.forEach((tool) => {
|
|
311
|
+
// Create a promise-based handler for each human-in-the-loop tool
|
|
312
|
+
const frontendTool: FrontendTool = {
|
|
313
|
+
name: tool.name,
|
|
314
|
+
description: tool.description,
|
|
315
|
+
parameters: tool.parameters,
|
|
316
|
+
followUp: tool.followUp,
|
|
317
|
+
...(tool.agentId && { agentId: tool.agentId }),
|
|
318
|
+
handler: async () => {
|
|
319
|
+
// This handler will be replaced by the hook when it runs
|
|
320
|
+
// For provider-level tools, we create a basic handler that waits for user interaction
|
|
321
|
+
return new Promise((resolve) => {
|
|
322
|
+
// The actual implementation will be handled by the render component
|
|
323
|
+
// This is a placeholder that the hook will override
|
|
324
|
+
console.warn(
|
|
325
|
+
`Human-in-the-loop tool '${tool.name}' called but no interactive handler is set up.`,
|
|
326
|
+
);
|
|
327
|
+
resolve(undefined);
|
|
328
|
+
});
|
|
329
|
+
},
|
|
330
|
+
};
|
|
331
|
+
processedTools.push(frontendTool);
|
|
332
|
+
|
|
333
|
+
// Add the render component to renderToolCalls
|
|
334
|
+
if (tool.render) {
|
|
335
|
+
processedRenderToolCalls.push({
|
|
336
|
+
name: tool.name,
|
|
337
|
+
args: tool.parameters!,
|
|
338
|
+
render: tool.render,
|
|
339
|
+
...(tool.agentId && { agentId: tool.agentId }),
|
|
340
|
+
} as ReactToolCallRenderer<unknown>);
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
return { tools: processedTools, renderToolCalls: processedRenderToolCalls };
|
|
345
|
+
}, [humanInTheLoopList]);
|
|
346
|
+
|
|
347
|
+
// Combine all tools for CopilotKitCore
|
|
348
|
+
const allTools = useMemo(() => {
|
|
349
|
+
const tools: FrontendTool[] = [];
|
|
350
|
+
|
|
351
|
+
// Add frontend tools
|
|
352
|
+
tools.push(...frontendToolsList);
|
|
353
|
+
|
|
354
|
+
// Add processed human-in-the-loop tools
|
|
355
|
+
tools.push(...processedHumanInTheLoopTools.tools);
|
|
356
|
+
|
|
357
|
+
return tools;
|
|
358
|
+
}, [frontendToolsList, processedHumanInTheLoopTools]);
|
|
359
|
+
|
|
360
|
+
// Combine all render tool calls
|
|
361
|
+
const allRenderToolCalls = useMemo(() => {
|
|
362
|
+
const combined: ReactToolCallRenderer<unknown>[] = [...renderToolCallsList];
|
|
363
|
+
|
|
364
|
+
// Add render components from frontend tools
|
|
365
|
+
frontendToolsList.forEach((tool) => {
|
|
366
|
+
if (tool.render) {
|
|
367
|
+
// For wildcard tools without parameters, default to z.any()
|
|
368
|
+
const args =
|
|
369
|
+
tool.parameters || (tool.name === "*" ? z.any() : undefined);
|
|
370
|
+
if (args) {
|
|
371
|
+
combined.push({
|
|
372
|
+
name: tool.name,
|
|
373
|
+
args: args,
|
|
374
|
+
render: tool.render,
|
|
375
|
+
} as ReactToolCallRenderer<unknown>);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
// Add render components from human-in-the-loop tools
|
|
381
|
+
combined.push(...processedHumanInTheLoopTools.renderToolCalls);
|
|
382
|
+
|
|
383
|
+
return combined;
|
|
384
|
+
}, [renderToolCallsList, frontendToolsList, processedHumanInTheLoopTools]);
|
|
385
|
+
|
|
386
|
+
// Stable instance: created once for the provider lifetime.
|
|
387
|
+
// Updates are applied via setter effects below rather than recreating the instance.
|
|
388
|
+
const copilotkitRef = useRef<CopilotKitCoreReact | null>(null);
|
|
389
|
+
if (copilotkitRef.current === null) {
|
|
390
|
+
copilotkitRef.current = new CopilotKitCoreReact({
|
|
391
|
+
runtimeUrl: chatApiEndpoint,
|
|
392
|
+
runtimeTransport: useSingleEndpoint ? "single" : "rest",
|
|
393
|
+
headers: mergedHeaders,
|
|
394
|
+
credentials,
|
|
395
|
+
properties,
|
|
396
|
+
agents__unsafe_dev_only: mergedAgents,
|
|
397
|
+
tools: allTools,
|
|
398
|
+
renderToolCalls: allRenderToolCalls,
|
|
399
|
+
renderActivityMessages: allActivityRenderers,
|
|
400
|
+
renderCustomMessages: renderCustomMessagesList,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
const copilotkit = copilotkitRef.current;
|
|
404
|
+
|
|
405
|
+
// Sync runtime-reported state once runtime info is fetched
|
|
406
|
+
useEffect(() => {
|
|
407
|
+
const subscription = copilotkit.subscribe({
|
|
408
|
+
onRuntimeConnectionStatusChanged: () => {
|
|
409
|
+
setRuntimeA2UIEnabled(copilotkit.a2uiEnabled);
|
|
410
|
+
setRuntimeLicenseStatus(copilotkit.licenseStatus);
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
return () => {
|
|
414
|
+
subscription.unsubscribe();
|
|
415
|
+
};
|
|
416
|
+
}, [copilotkit]);
|
|
417
|
+
|
|
418
|
+
// Subscribe to render tool calls changes to force re-renders
|
|
419
|
+
const [, forceUpdate] = useReducer((x) => x + 1, 0);
|
|
420
|
+
|
|
421
|
+
useEffect(() => {
|
|
422
|
+
const subscription = copilotkit.subscribe({
|
|
423
|
+
onRenderToolCallsChanged: () => {
|
|
424
|
+
forceUpdate();
|
|
425
|
+
},
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
return () => {
|
|
429
|
+
subscription.unsubscribe();
|
|
430
|
+
};
|
|
431
|
+
}, [copilotkit]);
|
|
432
|
+
|
|
433
|
+
// Track executing tool call IDs at the provider level.
|
|
434
|
+
// This is critical for HITL reconnection: when connecting to a thread with
|
|
435
|
+
// pending tool calls, the onToolExecutionStart event fires before child components
|
|
436
|
+
// (like CopilotChatToolCallsView) mount. By tracking at the provider level,
|
|
437
|
+
// we ensure the executing state is captured and available when children mount.
|
|
438
|
+
const [executingToolCallIds, setExecutingToolCallIds] = useState<
|
|
439
|
+
ReadonlySet<string>
|
|
440
|
+
>(() => new Set());
|
|
441
|
+
|
|
442
|
+
useEffect(() => {
|
|
443
|
+
const subscription = copilotkit.subscribe({
|
|
444
|
+
onToolExecutionStart: ({ toolCallId }) => {
|
|
445
|
+
setExecutingToolCallIds((prev) => {
|
|
446
|
+
if (prev.has(toolCallId)) return prev;
|
|
447
|
+
const next = new Set(prev);
|
|
448
|
+
next.add(toolCallId);
|
|
449
|
+
return next;
|
|
450
|
+
});
|
|
451
|
+
},
|
|
452
|
+
onToolExecutionEnd: ({ toolCallId }) => {
|
|
453
|
+
setExecutingToolCallIds((prev) => {
|
|
454
|
+
if (!prev.has(toolCallId)) return prev;
|
|
455
|
+
const next = new Set(prev);
|
|
456
|
+
next.delete(toolCallId);
|
|
457
|
+
return next;
|
|
458
|
+
});
|
|
459
|
+
},
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
return () => {
|
|
463
|
+
subscription.unsubscribe();
|
|
464
|
+
};
|
|
465
|
+
}, [copilotkit]);
|
|
466
|
+
|
|
467
|
+
// onError subscription — forward core errors to user callback
|
|
468
|
+
const onErrorRef = useRef(onError);
|
|
469
|
+
useEffect(() => {
|
|
470
|
+
onErrorRef.current = onError;
|
|
471
|
+
}, [onError]);
|
|
472
|
+
|
|
473
|
+
useEffect(() => {
|
|
474
|
+
if (!onErrorRef.current) return;
|
|
475
|
+
|
|
476
|
+
const subscription = copilotkit.subscribe({
|
|
477
|
+
onError: (event) => {
|
|
478
|
+
onErrorRef.current?.({
|
|
479
|
+
error: event.error,
|
|
480
|
+
code: event.code,
|
|
481
|
+
context: event.context,
|
|
482
|
+
});
|
|
483
|
+
},
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
return () => {
|
|
487
|
+
subscription.unsubscribe();
|
|
488
|
+
};
|
|
489
|
+
}, [copilotkit]);
|
|
490
|
+
|
|
491
|
+
useEffect(() => {
|
|
492
|
+
copilotkit.setRuntimeUrl(chatApiEndpoint);
|
|
493
|
+
copilotkit.setRuntimeTransport(useSingleEndpoint ? "single" : "rest");
|
|
494
|
+
copilotkit.setHeaders(mergedHeaders);
|
|
495
|
+
copilotkit.setCredentials(credentials);
|
|
496
|
+
copilotkit.setProperties(properties);
|
|
497
|
+
copilotkit.setAgents__unsafe_dev_only(mergedAgents);
|
|
498
|
+
}, [
|
|
499
|
+
copilotkit,
|
|
500
|
+
chatApiEndpoint,
|
|
501
|
+
mergedHeaders,
|
|
502
|
+
credentials,
|
|
503
|
+
properties,
|
|
504
|
+
mergedAgents,
|
|
505
|
+
useSingleEndpoint,
|
|
506
|
+
]);
|
|
507
|
+
|
|
508
|
+
// Sync render/tool arrays to the stable instance via setters.
|
|
509
|
+
// On mount, the constructor already receives the correct initial values,
|
|
510
|
+
// so we skip the first invocation. This is critical because child hooks
|
|
511
|
+
// (e.g., useFrontendTool, useHumanInTheLoop) register tools dynamically
|
|
512
|
+
// via addTool()/setRenderToolCalls() in their own effects, which fire
|
|
513
|
+
// BEFORE parent effects (React fires effects bottom-up). If the parent
|
|
514
|
+
// setter effects ran on mount, they would overwrite the children's tools.
|
|
515
|
+
const didMountRef = useRef(false);
|
|
516
|
+
|
|
517
|
+
useEffect(() => {
|
|
518
|
+
if (!didMountRef.current) return;
|
|
519
|
+
copilotkit.setTools(allTools);
|
|
520
|
+
}, [copilotkit, allTools]);
|
|
521
|
+
|
|
522
|
+
useEffect(() => {
|
|
523
|
+
if (!didMountRef.current) return;
|
|
524
|
+
copilotkit.setRenderToolCalls(allRenderToolCalls);
|
|
525
|
+
}, [copilotkit, allRenderToolCalls]);
|
|
526
|
+
|
|
527
|
+
useEffect(() => {
|
|
528
|
+
if (!didMountRef.current) return;
|
|
529
|
+
copilotkit.setRenderActivityMessages(allActivityRenderers);
|
|
530
|
+
}, [copilotkit, allActivityRenderers]);
|
|
531
|
+
|
|
532
|
+
useEffect(() => {
|
|
533
|
+
if (!didMountRef.current) return;
|
|
534
|
+
copilotkit.setRenderCustomMessages(renderCustomMessagesList);
|
|
535
|
+
}, [copilotkit, renderCustomMessagesList]);
|
|
536
|
+
|
|
537
|
+
// Mark mount complete — must be declared AFTER the setter effects
|
|
538
|
+
// so it runs last in the effect queue on the initial mount cycle.
|
|
539
|
+
useEffect(() => {
|
|
540
|
+
didMountRef.current = true;
|
|
541
|
+
}, []);
|
|
542
|
+
|
|
543
|
+
const contextValue = useMemo<CopilotKitContextValue>(
|
|
544
|
+
() => ({ copilotkit, executingToolCallIds }),
|
|
545
|
+
[copilotkit, executingToolCallIds],
|
|
546
|
+
);
|
|
547
|
+
|
|
548
|
+
// License context — driven by server-reported status via /info endpoint
|
|
549
|
+
const licenseContextValue = useMemo(
|
|
550
|
+
() => createLicenseContextValue(null),
|
|
551
|
+
[],
|
|
552
|
+
);
|
|
553
|
+
|
|
554
|
+
return (
|
|
555
|
+
<CopilotKitContext.Provider value={contextValue}>
|
|
556
|
+
<LicenseContext.Provider value={licenseContextValue}>
|
|
557
|
+
{children}
|
|
558
|
+
{shouldRenderInspector ? (
|
|
559
|
+
<CopilotKitInspector core={copilotkit} />
|
|
560
|
+
) : null}
|
|
561
|
+
{/* License warnings — driven by server-reported status */}
|
|
562
|
+
{runtimeLicenseStatus === "none" && !resolvedPublicKey && (
|
|
563
|
+
<LicenseWarningBanner type="no_license" />
|
|
564
|
+
)}
|
|
565
|
+
{runtimeLicenseStatus === "expired" && (
|
|
566
|
+
<LicenseWarningBanner type="expired" />
|
|
567
|
+
)}
|
|
568
|
+
{runtimeLicenseStatus === "invalid" && (
|
|
569
|
+
<LicenseWarningBanner type="invalid" />
|
|
570
|
+
)}
|
|
571
|
+
{runtimeLicenseStatus === "expiring" && (
|
|
572
|
+
<LicenseWarningBanner type="expiring" />
|
|
573
|
+
)}
|
|
574
|
+
</LicenseContext.Provider>
|
|
575
|
+
</CopilotKitContext.Provider>
|
|
576
|
+
);
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
// Hook to use the CopilotKit instance - returns the full context value
|
|
580
|
+
export const useCopilotKit = (): CopilotKitContextValue => {
|
|
581
|
+
const context = useContext(CopilotKitContext);
|
|
582
|
+
const [, forceUpdate] = useReducer((x) => x + 1, 0);
|
|
583
|
+
|
|
584
|
+
if (!context) {
|
|
585
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
586
|
+
}
|
|
587
|
+
useEffect(() => {
|
|
588
|
+
const subscription = context.copilotkit.subscribe({
|
|
589
|
+
onRuntimeConnectionStatusChanged: () => {
|
|
590
|
+
forceUpdate();
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
return () => {
|
|
594
|
+
subscription.unsubscribe();
|
|
595
|
+
};
|
|
596
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
597
|
+
}, []);
|
|
598
|
+
|
|
599
|
+
return context;
|
|
600
|
+
};
|