@copilotkit/react-core 1.54.1 → 1.55.0-next.8
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 +127 -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,156 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ReactActivityMessageRenderer, ReactToolCallRenderer } from "../types";
|
|
3
|
+
import { ReactCustomMessageRenderer } from "../types/react-custom-message-renderer";
|
|
4
|
+
import {
|
|
5
|
+
CopilotKitCore,
|
|
6
|
+
type CopilotKitCoreConfig,
|
|
7
|
+
type CopilotKitCoreSubscriber,
|
|
8
|
+
type CopilotKitCoreSubscription,
|
|
9
|
+
} from "@copilotkit/core";
|
|
10
|
+
|
|
11
|
+
export interface CopilotKitCoreReactConfig extends CopilotKitCoreConfig {
|
|
12
|
+
// Add any additional configuration properties specific to the React implementation
|
|
13
|
+
renderToolCalls?: ReactToolCallRenderer<any>[];
|
|
14
|
+
renderActivityMessages?: ReactActivityMessageRenderer<any>[];
|
|
15
|
+
|
|
16
|
+
// Add custom message renderers
|
|
17
|
+
renderCustomMessages?: ReactCustomMessageRenderer[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface CopilotKitCoreReactSubscriber extends CopilotKitCoreSubscriber {
|
|
21
|
+
onRenderToolCallsChanged?: (event: {
|
|
22
|
+
copilotkit: CopilotKitCore;
|
|
23
|
+
renderToolCalls: ReactToolCallRenderer<any>[];
|
|
24
|
+
}) => void | Promise<void>;
|
|
25
|
+
onInterruptElementChanged?: (event: {
|
|
26
|
+
copilotkit: CopilotKitCore;
|
|
27
|
+
interruptElement: React.ReactElement | null;
|
|
28
|
+
}) => void | Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class CopilotKitCoreReact extends CopilotKitCore {
|
|
32
|
+
private _renderToolCalls: ReactToolCallRenderer<any>[] = [];
|
|
33
|
+
private _hookRenderToolCalls: Map<string, ReactToolCallRenderer<any>> =
|
|
34
|
+
new Map();
|
|
35
|
+
private _cachedMergedRenderToolCalls: ReactToolCallRenderer<any>[] | null =
|
|
36
|
+
null;
|
|
37
|
+
private _renderCustomMessages: ReactCustomMessageRenderer[] = [];
|
|
38
|
+
private _renderActivityMessages: ReactActivityMessageRenderer<any>[] = [];
|
|
39
|
+
private _interruptElement: React.ReactElement | null = null;
|
|
40
|
+
|
|
41
|
+
constructor(config: CopilotKitCoreReactConfig) {
|
|
42
|
+
super(config);
|
|
43
|
+
this._renderToolCalls = config.renderToolCalls ?? [];
|
|
44
|
+
this._renderCustomMessages = config.renderCustomMessages ?? [];
|
|
45
|
+
this._renderActivityMessages = config.renderActivityMessages ?? [];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get renderCustomMessages(): Readonly<ReactCustomMessageRenderer[]> {
|
|
49
|
+
return this._renderCustomMessages;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get renderActivityMessages(): Readonly<ReactActivityMessageRenderer<any>>[] {
|
|
53
|
+
return this._renderActivityMessages;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get renderToolCalls(): Readonly<ReactToolCallRenderer<any>>[] {
|
|
57
|
+
if (this._hookRenderToolCalls.size === 0) {
|
|
58
|
+
return this._renderToolCalls;
|
|
59
|
+
}
|
|
60
|
+
if (this._cachedMergedRenderToolCalls) {
|
|
61
|
+
return this._cachedMergedRenderToolCalls;
|
|
62
|
+
}
|
|
63
|
+
// Merge: hook entries override prop entries with the same key
|
|
64
|
+
const merged = new Map<string, ReactToolCallRenderer<any>>();
|
|
65
|
+
for (const rc of this._renderToolCalls) {
|
|
66
|
+
merged.set(`${rc.agentId ?? ""}:${rc.name}`, rc);
|
|
67
|
+
}
|
|
68
|
+
for (const [key, rc] of this._hookRenderToolCalls) {
|
|
69
|
+
merged.set(key, rc);
|
|
70
|
+
}
|
|
71
|
+
this._cachedMergedRenderToolCalls = Array.from(merged.values());
|
|
72
|
+
return this._cachedMergedRenderToolCalls;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setRenderActivityMessages(
|
|
76
|
+
renderers: ReactActivityMessageRenderer<any>[],
|
|
77
|
+
): void {
|
|
78
|
+
this._renderActivityMessages = renderers;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
setRenderCustomMessages(renderers: ReactCustomMessageRenderer[]): void {
|
|
82
|
+
this._renderCustomMessages = renderers;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
setRenderToolCalls(renderToolCalls: ReactToolCallRenderer<any>[]): void {
|
|
86
|
+
this._renderToolCalls = renderToolCalls;
|
|
87
|
+
this._cachedMergedRenderToolCalls = null;
|
|
88
|
+
this._notifyRenderToolCallsChanged();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
addHookRenderToolCall(entry: ReactToolCallRenderer<any>): void {
|
|
92
|
+
const key = `${entry.agentId ?? ""}:${entry.name}`;
|
|
93
|
+
this._hookRenderToolCalls.set(key, entry);
|
|
94
|
+
this._cachedMergedRenderToolCalls = null;
|
|
95
|
+
this._notifyRenderToolCallsChanged();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
removeHookRenderToolCall(name: string, agentId?: string): void {
|
|
99
|
+
const key = `${agentId ?? ""}:${name}`;
|
|
100
|
+
if (this._hookRenderToolCalls.delete(key)) {
|
|
101
|
+
this._cachedMergedRenderToolCalls = null;
|
|
102
|
+
this._notifyRenderToolCallsChanged();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private _notifyRenderToolCallsChanged(): void {
|
|
107
|
+
void this.notifySubscribers((subscriber) => {
|
|
108
|
+
const reactSubscriber = subscriber as CopilotKitCoreReactSubscriber;
|
|
109
|
+
if (reactSubscriber.onRenderToolCallsChanged) {
|
|
110
|
+
reactSubscriber.onRenderToolCallsChanged({
|
|
111
|
+
copilotkit: this,
|
|
112
|
+
renderToolCalls: this.renderToolCalls,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}, "Subscriber onRenderToolCallsChanged error:");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get interruptElement(): React.ReactElement | null {
|
|
119
|
+
return this._interruptElement;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
setInterruptElement(element: React.ReactElement | null): void {
|
|
123
|
+
this._interruptElement = element;
|
|
124
|
+
void this.notifySubscribers((subscriber) => {
|
|
125
|
+
const reactSubscriber = subscriber as CopilotKitCoreReactSubscriber;
|
|
126
|
+
reactSubscriber.onInterruptElementChanged?.({
|
|
127
|
+
copilotkit: this,
|
|
128
|
+
interruptElement: this._interruptElement,
|
|
129
|
+
});
|
|
130
|
+
}, "Subscriber onInterruptElementChanged error:");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Override to accept React-specific subscriber type
|
|
134
|
+
subscribe(
|
|
135
|
+
subscriber: CopilotKitCoreReactSubscriber,
|
|
136
|
+
): CopilotKitCoreSubscription {
|
|
137
|
+
return super.subscribe(subscriber);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Wait for pending React state updates before the follow-up agent run.
|
|
142
|
+
*
|
|
143
|
+
* When a frontend tool handler calls setState(), React 18 batches the update
|
|
144
|
+
* and schedules a commit via its internal scheduler (MessageChannel). The
|
|
145
|
+
* useAgentContext hook registers context via useLayoutEffect, which runs
|
|
146
|
+
* synchronously after React commits that batch.
|
|
147
|
+
*
|
|
148
|
+
* Awaiting a zero-delay timeout yields to the macrotask queue. React's
|
|
149
|
+
* MessageChannel task runs first, committing the pending state and running
|
|
150
|
+
* useLayoutEffect (which updates the context store). The follow-up runAgent
|
|
151
|
+
* call then reads fresh context.
|
|
152
|
+
*/
|
|
153
|
+
async waitForPendingFrameworkUpdates(): Promise<void> {
|
|
154
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 0));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { twMerge } from "tailwind-merge";
|
|
3
|
+
|
|
4
|
+
/** Existing union (unchanged) */
|
|
5
|
+
export type SlotValue<C extends React.ComponentType<any>> =
|
|
6
|
+
| C
|
|
7
|
+
| string
|
|
8
|
+
| Partial<React.ComponentProps<C>>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Shallow equality comparison for objects.
|
|
12
|
+
*/
|
|
13
|
+
export function shallowEqual<T extends Record<string, unknown>>(
|
|
14
|
+
obj1: T,
|
|
15
|
+
obj2: T,
|
|
16
|
+
): boolean {
|
|
17
|
+
const keys1 = Object.keys(obj1);
|
|
18
|
+
const keys2 = Object.keys(obj2);
|
|
19
|
+
|
|
20
|
+
if (keys1.length !== keys2.length) return false;
|
|
21
|
+
|
|
22
|
+
for (const key of keys1) {
|
|
23
|
+
if (obj1[key] !== obj2[key]) return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Utility: concrete React elements for every slot */
|
|
30
|
+
type SlotElements<S> = { [K in keyof S]: React.ReactElement };
|
|
31
|
+
|
|
32
|
+
export type WithSlots<
|
|
33
|
+
S extends Record<string, React.ComponentType<any>>,
|
|
34
|
+
Rest = {},
|
|
35
|
+
> = {
|
|
36
|
+
/** Per‑slot overrides */
|
|
37
|
+
[K in keyof S]?: SlotValue<S[K]>;
|
|
38
|
+
} & {
|
|
39
|
+
children?: (props: SlotElements<S> & Rest) => React.ReactNode;
|
|
40
|
+
} & Omit<Rest, "children">;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Check if a value is a React component type (function, class, forwardRef, memo, etc.)
|
|
44
|
+
*/
|
|
45
|
+
export function isReactComponentType(
|
|
46
|
+
value: unknown,
|
|
47
|
+
): value is React.ComponentType<any> {
|
|
48
|
+
if (typeof value === "function") {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
// forwardRef, memo, lazy have $$typeof but are not valid elements
|
|
52
|
+
if (
|
|
53
|
+
value &&
|
|
54
|
+
typeof value === "object" &&
|
|
55
|
+
"$$typeof" in value &&
|
|
56
|
+
!React.isValidElement(value)
|
|
57
|
+
) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Internal function to render a slot value as a React element (non-memoized).
|
|
65
|
+
*/
|
|
66
|
+
function renderSlotElement(
|
|
67
|
+
slot: SlotValue<React.ComponentType<any>> | undefined,
|
|
68
|
+
DefaultComponent: React.ComponentType<any>,
|
|
69
|
+
props: Record<string, unknown>,
|
|
70
|
+
): React.ReactElement {
|
|
71
|
+
if (typeof slot === "string") {
|
|
72
|
+
// When slot is a string, treat it as a className and merge with existing className
|
|
73
|
+
const existingClassName = props.className as string | undefined;
|
|
74
|
+
return React.createElement(DefaultComponent, {
|
|
75
|
+
...props,
|
|
76
|
+
className: twMerge(existingClassName, slot),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Check if slot is a React component type (function, forwardRef, memo, etc.)
|
|
81
|
+
if (isReactComponentType(slot)) {
|
|
82
|
+
return React.createElement(slot, props);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// If slot is a plain object (not a React element), treat it as props override
|
|
86
|
+
if (slot && typeof slot === "object" && !React.isValidElement(slot)) {
|
|
87
|
+
return React.createElement(DefaultComponent, {
|
|
88
|
+
...props,
|
|
89
|
+
...slot,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return React.createElement(DefaultComponent, props);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Internal memoized wrapper component for renderSlot.
|
|
98
|
+
* Uses forwardRef to support ref forwarding.
|
|
99
|
+
*/
|
|
100
|
+
const MemoizedSlotWrapper = React.memo(
|
|
101
|
+
React.forwardRef<unknown, any>(function MemoizedSlotWrapper(props, ref) {
|
|
102
|
+
const { $slot, $component, ...rest } = props;
|
|
103
|
+
const propsWithRef: Record<string, unknown> =
|
|
104
|
+
ref !== null ? { ...rest, ref } : rest;
|
|
105
|
+
return renderSlotElement($slot, $component, propsWithRef);
|
|
106
|
+
}),
|
|
107
|
+
(prev: any, next: any) => {
|
|
108
|
+
// Compare slot and component references
|
|
109
|
+
if (prev.$slot !== next.$slot) return false;
|
|
110
|
+
if (prev.$component !== next.$component) return false;
|
|
111
|
+
|
|
112
|
+
// Shallow compare remaining props (ref is handled separately by React)
|
|
113
|
+
const { $slot: _ps, $component: _pc, ...prevRest } = prev;
|
|
114
|
+
const { $slot: _ns, $component: _nc, ...nextRest } = next;
|
|
115
|
+
return shallowEqual(
|
|
116
|
+
prevRest as Record<string, unknown>,
|
|
117
|
+
nextRest as Record<string, unknown>,
|
|
118
|
+
);
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Renders a slot value as a memoized React element.
|
|
124
|
+
* Automatically prevents unnecessary re-renders using shallow prop comparison.
|
|
125
|
+
* Supports ref forwarding.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* renderSlot(customInput, CopilotChatInput, { onSubmit: handleSubmit })
|
|
129
|
+
*/
|
|
130
|
+
export function renderSlot<
|
|
131
|
+
C extends React.ComponentType<any>,
|
|
132
|
+
P = React.ComponentProps<C>,
|
|
133
|
+
>(
|
|
134
|
+
slot: SlotValue<C> | undefined,
|
|
135
|
+
DefaultComponent: C,
|
|
136
|
+
props: P,
|
|
137
|
+
): React.ReactElement {
|
|
138
|
+
return React.createElement(MemoizedSlotWrapper, {
|
|
139
|
+
...props,
|
|
140
|
+
$slot: slot,
|
|
141
|
+
$component: DefaultComponent,
|
|
142
|
+
} as any);
|
|
143
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { CopilotKitCoreReact } from "./react-core";
|
|
2
|
+
import {
|
|
3
|
+
TranscriptionErrorCode,
|
|
4
|
+
type TranscriptionErrorResponse,
|
|
5
|
+
} from "@copilotkit/shared";
|
|
6
|
+
|
|
7
|
+
export interface TranscriptionResult {
|
|
8
|
+
text: string;
|
|
9
|
+
size: number;
|
|
10
|
+
type: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Error info parsed from transcription endpoint error responses.
|
|
15
|
+
*/
|
|
16
|
+
export interface TranscriptionErrorInfo {
|
|
17
|
+
code: TranscriptionErrorCode;
|
|
18
|
+
message: string;
|
|
19
|
+
retryable: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Re-export error code enum for convenience
|
|
23
|
+
export { TranscriptionErrorCode };
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Convert a Blob to a base64 string
|
|
27
|
+
*/
|
|
28
|
+
async function blobToBase64(blob: Blob): Promise<string> {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const reader = new FileReader();
|
|
31
|
+
reader.onloadend = () => {
|
|
32
|
+
const result = reader.result as string;
|
|
33
|
+
// Remove the data URL prefix to get pure base64
|
|
34
|
+
const base64 = result.split(",")[1];
|
|
35
|
+
resolve(base64 ?? "");
|
|
36
|
+
};
|
|
37
|
+
reader.onerror = () => reject(new Error("Failed to read audio data"));
|
|
38
|
+
reader.readAsDataURL(blob);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Check if an error response matches our expected format
|
|
44
|
+
*/
|
|
45
|
+
function isTranscriptionErrorResponse(
|
|
46
|
+
data: unknown,
|
|
47
|
+
): data is TranscriptionErrorResponse {
|
|
48
|
+
return (
|
|
49
|
+
typeof data === "object" &&
|
|
50
|
+
data !== null &&
|
|
51
|
+
"error" in data &&
|
|
52
|
+
"message" in data &&
|
|
53
|
+
typeof (data as TranscriptionErrorResponse).error === "string" &&
|
|
54
|
+
typeof (data as TranscriptionErrorResponse).message === "string"
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Parse error info from a transcription error response
|
|
60
|
+
*/
|
|
61
|
+
function parseTranscriptionError(
|
|
62
|
+
response: TranscriptionErrorResponse,
|
|
63
|
+
): TranscriptionErrorInfo {
|
|
64
|
+
return {
|
|
65
|
+
code: response.error,
|
|
66
|
+
message: response.message,
|
|
67
|
+
retryable: response.retryable ?? false,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Custom error type for transcription failures.
|
|
73
|
+
* Extends Error with transcription-specific info for contextual error handling.
|
|
74
|
+
*/
|
|
75
|
+
export class TranscriptionError extends Error {
|
|
76
|
+
public readonly info: TranscriptionErrorInfo;
|
|
77
|
+
|
|
78
|
+
constructor(info: TranscriptionErrorInfo) {
|
|
79
|
+
super(info.message);
|
|
80
|
+
this.name = "TranscriptionError";
|
|
81
|
+
this.info = info;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Transcribe an audio blob using the CopilotKit runtime
|
|
87
|
+
*
|
|
88
|
+
* Supports both REST mode (multipart/form-data) and single-endpoint mode (base64 JSON)
|
|
89
|
+
*
|
|
90
|
+
* @throws {TranscriptionError} When transcription fails with typed error information
|
|
91
|
+
*/
|
|
92
|
+
export async function transcribeAudio(
|
|
93
|
+
core: CopilotKitCoreReact,
|
|
94
|
+
audioBlob: Blob,
|
|
95
|
+
filename: string = "recording.webm",
|
|
96
|
+
): Promise<TranscriptionResult> {
|
|
97
|
+
const runtimeUrl = core.runtimeUrl;
|
|
98
|
+
if (!runtimeUrl) {
|
|
99
|
+
throw new TranscriptionError({
|
|
100
|
+
code: TranscriptionErrorCode.INVALID_REQUEST,
|
|
101
|
+
message: "Runtime URL is not configured",
|
|
102
|
+
retryable: false,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const headers: Record<string, string> = { ...core.headers };
|
|
107
|
+
let response: Response;
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
if (core.runtimeTransport === "single") {
|
|
111
|
+
// Single-endpoint mode: POST JSON with base64 audio
|
|
112
|
+
const base64Audio = await blobToBase64(audioBlob);
|
|
113
|
+
|
|
114
|
+
headers["Content-Type"] = "application/json";
|
|
115
|
+
|
|
116
|
+
response = await fetch(runtimeUrl, {
|
|
117
|
+
method: "POST",
|
|
118
|
+
headers,
|
|
119
|
+
body: JSON.stringify({
|
|
120
|
+
method: "transcribe",
|
|
121
|
+
body: {
|
|
122
|
+
audio: base64Audio,
|
|
123
|
+
mimeType: audioBlob.type || "audio/webm",
|
|
124
|
+
filename,
|
|
125
|
+
},
|
|
126
|
+
}),
|
|
127
|
+
});
|
|
128
|
+
} else {
|
|
129
|
+
// REST mode: POST multipart/form-data to /transcribe
|
|
130
|
+
// Don't set Content-Type - browser will set it with boundary for FormData
|
|
131
|
+
delete headers["Content-Type"];
|
|
132
|
+
|
|
133
|
+
const formData = new FormData();
|
|
134
|
+
formData.append("audio", audioBlob, filename);
|
|
135
|
+
|
|
136
|
+
response = await fetch(`${runtimeUrl}/transcribe`, {
|
|
137
|
+
method: "POST",
|
|
138
|
+
headers,
|
|
139
|
+
body: formData,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
} catch (error) {
|
|
143
|
+
// Network error - fetch failed
|
|
144
|
+
throw new TranscriptionError({
|
|
145
|
+
code: TranscriptionErrorCode.NETWORK_ERROR,
|
|
146
|
+
message:
|
|
147
|
+
error instanceof Error ? error.message : "Network request failed",
|
|
148
|
+
retryable: true,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (!response.ok) {
|
|
153
|
+
let errorData: unknown;
|
|
154
|
+
try {
|
|
155
|
+
errorData = await response.json();
|
|
156
|
+
} catch {
|
|
157
|
+
// Could not parse error response
|
|
158
|
+
throw new TranscriptionError({
|
|
159
|
+
code: TranscriptionErrorCode.PROVIDER_ERROR,
|
|
160
|
+
message: `HTTP ${response.status}: ${response.statusText}`,
|
|
161
|
+
retryable: response.status >= 500,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// If we got a typed error response, use it
|
|
166
|
+
if (isTranscriptionErrorResponse(errorData)) {
|
|
167
|
+
throw new TranscriptionError(parseTranscriptionError(errorData));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Unknown error format
|
|
171
|
+
throw new TranscriptionError({
|
|
172
|
+
code: TranscriptionErrorCode.PROVIDER_ERROR,
|
|
173
|
+
message:
|
|
174
|
+
typeof errorData === "object" &&
|
|
175
|
+
errorData !== null &&
|
|
176
|
+
"message" in errorData
|
|
177
|
+
? String((errorData as { message: unknown }).message)
|
|
178
|
+
: "Transcription failed",
|
|
179
|
+
retryable: response.status >= 500,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return (await response.json()) as TranscriptionResult;
|
|
184
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
ReactNode,
|
|
6
|
+
useEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
} from "react";
|
|
11
|
+
import { DEFAULT_AGENT_ID, randomUUID } from "@copilotkit/shared";
|
|
12
|
+
|
|
13
|
+
// Default labels
|
|
14
|
+
export const CopilotChatDefaultLabels = {
|
|
15
|
+
chatInputPlaceholder: "Type a message...",
|
|
16
|
+
chatInputToolbarStartTranscribeButtonLabel: "Transcribe",
|
|
17
|
+
chatInputToolbarCancelTranscribeButtonLabel: "Cancel",
|
|
18
|
+
chatInputToolbarFinishTranscribeButtonLabel: "Finish",
|
|
19
|
+
chatInputToolbarAddButtonLabel: "Add photos or files",
|
|
20
|
+
chatInputToolbarToolsButtonLabel: "Tools",
|
|
21
|
+
assistantMessageToolbarCopyCodeLabel: "Copy",
|
|
22
|
+
assistantMessageToolbarCopyCodeCopiedLabel: "Copied",
|
|
23
|
+
assistantMessageToolbarCopyMessageLabel: "Copy",
|
|
24
|
+
assistantMessageToolbarThumbsUpLabel: "Good response",
|
|
25
|
+
assistantMessageToolbarThumbsDownLabel: "Bad response",
|
|
26
|
+
assistantMessageToolbarReadAloudLabel: "Read aloud",
|
|
27
|
+
assistantMessageToolbarRegenerateLabel: "Regenerate",
|
|
28
|
+
userMessageToolbarCopyMessageLabel: "Copy",
|
|
29
|
+
userMessageToolbarEditMessageLabel: "Edit",
|
|
30
|
+
chatDisclaimerText:
|
|
31
|
+
"AI can make mistakes. Please verify important information.",
|
|
32
|
+
chatToggleOpenLabel: "Open chat",
|
|
33
|
+
chatToggleCloseLabel: "Close chat",
|
|
34
|
+
modalHeaderTitle: "CopilotKit Chat",
|
|
35
|
+
welcomeMessageText: "How can I help you today?",
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type CopilotChatLabels = typeof CopilotChatDefaultLabels;
|
|
39
|
+
|
|
40
|
+
// Define the full configuration interface
|
|
41
|
+
export interface CopilotChatConfigurationValue {
|
|
42
|
+
labels: CopilotChatLabels;
|
|
43
|
+
agentId: string;
|
|
44
|
+
threadId: string;
|
|
45
|
+
isModalOpen: boolean;
|
|
46
|
+
setModalOpen: (open: boolean) => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Create the configuration context
|
|
50
|
+
const CopilotChatConfiguration =
|
|
51
|
+
createContext<CopilotChatConfigurationValue | null>(null);
|
|
52
|
+
|
|
53
|
+
// Provider props interface
|
|
54
|
+
export interface CopilotChatConfigurationProviderProps {
|
|
55
|
+
children: ReactNode;
|
|
56
|
+
labels?: Partial<CopilotChatLabels>;
|
|
57
|
+
agentId?: string;
|
|
58
|
+
threadId?: string;
|
|
59
|
+
isModalDefaultOpen?: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Provider component
|
|
63
|
+
export const CopilotChatConfigurationProvider: React.FC<
|
|
64
|
+
CopilotChatConfigurationProviderProps
|
|
65
|
+
> = ({ children, labels, agentId, threadId, isModalDefaultOpen }) => {
|
|
66
|
+
const parentConfig = useContext(CopilotChatConfiguration);
|
|
67
|
+
|
|
68
|
+
const mergedLabels: CopilotChatLabels = useMemo(
|
|
69
|
+
() => ({
|
|
70
|
+
...CopilotChatDefaultLabels,
|
|
71
|
+
...(parentConfig?.labels ?? {}),
|
|
72
|
+
...(labels ?? {}),
|
|
73
|
+
}),
|
|
74
|
+
[labels, parentConfig?.labels],
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const resolvedAgentId = agentId ?? parentConfig?.agentId ?? DEFAULT_AGENT_ID;
|
|
78
|
+
|
|
79
|
+
const resolvedThreadId = useMemo(() => {
|
|
80
|
+
if (threadId) {
|
|
81
|
+
return threadId;
|
|
82
|
+
}
|
|
83
|
+
if (parentConfig?.threadId) {
|
|
84
|
+
return parentConfig.threadId;
|
|
85
|
+
}
|
|
86
|
+
return randomUUID();
|
|
87
|
+
}, [threadId, parentConfig?.threadId]);
|
|
88
|
+
|
|
89
|
+
const resolvedDefaultOpen = isModalDefaultOpen ?? true;
|
|
90
|
+
|
|
91
|
+
const [internalModalOpen, setInternalModalOpen] =
|
|
92
|
+
useState<boolean>(resolvedDefaultOpen);
|
|
93
|
+
|
|
94
|
+
const hasExplicitDefault = isModalDefaultOpen !== undefined;
|
|
95
|
+
|
|
96
|
+
// When this provider owns its modal state, wrap the setter so that changes
|
|
97
|
+
// propagate upward to any ancestor provider. This allows an outer
|
|
98
|
+
// CopilotChatConfigurationProvider (e.g. a user's layout-level provider) to
|
|
99
|
+
// observe open/close events that originate deep in the tree — fixing the
|
|
100
|
+
// "outer hook always returns true" regression (CPK-7152 Behavior B).
|
|
101
|
+
const setAndSync = useCallback(
|
|
102
|
+
(open: boolean) => {
|
|
103
|
+
setInternalModalOpen(open);
|
|
104
|
+
parentConfig?.setModalOpen(open);
|
|
105
|
+
},
|
|
106
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
107
|
+
[parentConfig?.setModalOpen],
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// Sync parent → child: when an ancestor's modal state is changed externally
|
|
111
|
+
// (e.g. the user calls setModalOpen from an outer hook), reflect that change
|
|
112
|
+
// in our own state so the sidebar/popup responds accordingly.
|
|
113
|
+
// Skip the initial mount so that our own isModalDefaultOpen is respected and
|
|
114
|
+
// not immediately overwritten by the parent's current value.
|
|
115
|
+
const isMounted = useRef(false);
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (!hasExplicitDefault) return;
|
|
118
|
+
if (!isMounted.current) {
|
|
119
|
+
isMounted.current = true;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (parentConfig?.isModalOpen === undefined) return;
|
|
123
|
+
setInternalModalOpen(parentConfig.isModalOpen);
|
|
124
|
+
}, [parentConfig?.isModalOpen, hasExplicitDefault]);
|
|
125
|
+
|
|
126
|
+
const resolvedIsModalOpen = hasExplicitDefault
|
|
127
|
+
? internalModalOpen
|
|
128
|
+
: (parentConfig?.isModalOpen ?? internalModalOpen);
|
|
129
|
+
const resolvedSetModalOpen = hasExplicitDefault
|
|
130
|
+
? setAndSync
|
|
131
|
+
: (parentConfig?.setModalOpen ?? setInternalModalOpen);
|
|
132
|
+
|
|
133
|
+
const configurationValue: CopilotChatConfigurationValue = useMemo(
|
|
134
|
+
() => ({
|
|
135
|
+
labels: mergedLabels,
|
|
136
|
+
agentId: resolvedAgentId,
|
|
137
|
+
threadId: resolvedThreadId,
|
|
138
|
+
isModalOpen: resolvedIsModalOpen,
|
|
139
|
+
setModalOpen: resolvedSetModalOpen,
|
|
140
|
+
}),
|
|
141
|
+
[
|
|
142
|
+
mergedLabels,
|
|
143
|
+
resolvedAgentId,
|
|
144
|
+
resolvedThreadId,
|
|
145
|
+
resolvedIsModalOpen,
|
|
146
|
+
resolvedSetModalOpen,
|
|
147
|
+
],
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
return (
|
|
151
|
+
<CopilotChatConfiguration.Provider value={configurationValue}>
|
|
152
|
+
{children}
|
|
153
|
+
</CopilotChatConfiguration.Provider>
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// Hook to use the full configuration
|
|
158
|
+
export const useCopilotChatConfiguration =
|
|
159
|
+
(): CopilotChatConfigurationValue | null => {
|
|
160
|
+
const configuration = useContext(CopilotChatConfiguration);
|
|
161
|
+
return configuration;
|
|
162
|
+
};
|