@copilotkit/react-native 1.57.1 → 1.57.3
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/README.md +11 -1
- package/dist/index.cjs +752 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +394 -6
- package/dist/index.d.mts +394 -6
- package/dist/index.mjs +725 -14
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills.cjs +86 -73
- package/dist/polyfills.cjs.map +1 -1
- package/dist/polyfills.mjs +86 -73
- package/dist/polyfills.mjs.map +1 -1
- package/package.json +21 -5
- package/src/CopilotChat.tsx +293 -0
- package/src/CopilotKitProvider.tsx +55 -13
- package/src/CopilotModal.tsx +32 -0
- package/src/CopilotPopup.tsx +359 -0
- package/src/CopilotSidebar.tsx +357 -0
- package/src/__tests__/__mocks__/expo-document-picker.ts +13 -0
- package/src/__tests__/__mocks__/expo-file-system.ts +12 -0
- package/src/__tests__/attachments-integration.test.tsx +336 -0
- package/src/__tests__/copilot-chat-agentid.test.tsx +305 -0
- package/src/__tests__/copilot-chat-attachments.test.tsx +259 -0
- package/src/__tests__/copilot-popup.test.tsx +558 -0
- package/src/__tests__/copilot-sidebar.test.tsx +500 -0
- package/src/__tests__/copilotkit-provider.test.tsx +3 -0
- package/src/__tests__/headless-integration.test.tsx +55 -1
- package/src/__tests__/streaming-fetch.test.ts +84 -44
- package/src/__tests__/use-attachments.test.ts +410 -0
- package/src/hooks/use-attachments.ts +274 -0
- package/src/index.ts +77 -3
- package/src/polyfills.ts +3 -2
- package/src/streaming-fetch.ts +135 -104
- package/tsdown.config.ts +1 -0
- package/vitest.config.mjs +19 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Stub for expo-document-picker — replaced at runtime by vi.mock() in tests.
|
|
2
|
+
// This file exists so Vite's import analysis can resolve the module.
|
|
3
|
+
export async function getDocumentAsync(_options?: unknown): Promise<{
|
|
4
|
+
canceled: boolean;
|
|
5
|
+
assets: Array<{
|
|
6
|
+
uri: string;
|
|
7
|
+
name: string;
|
|
8
|
+
size: number;
|
|
9
|
+
mimeType: string;
|
|
10
|
+
}>;
|
|
11
|
+
}> {
|
|
12
|
+
return { canceled: true, assets: [] };
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Stub for expo-file-system — replaced at runtime by vi.mock() in tests.
|
|
2
|
+
// This file exists so Vite's import analysis can resolve the module.
|
|
3
|
+
export const EncodingType = {
|
|
4
|
+
Base64: "base64",
|
|
5
|
+
} as const;
|
|
6
|
+
|
|
7
|
+
export async function readAsStringAsync(
|
|
8
|
+
_uri: string,
|
|
9
|
+
_options?: unknown,
|
|
10
|
+
): Promise<string> {
|
|
11
|
+
return "";
|
|
12
|
+
}
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
// packages/react-native/src/__tests__/attachments-integration.test.tsx
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { render, act } from "@testing-library/react";
|
|
4
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
5
|
+
|
|
6
|
+
// ─── Mocks ────────────────────────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
const mockGetDocumentAsync = vi.fn();
|
|
9
|
+
const mockReadAsStringAsync = vi.fn();
|
|
10
|
+
|
|
11
|
+
vi.mock("expo-document-picker", () => ({
|
|
12
|
+
getDocumentAsync: (...args: any[]) => mockGetDocumentAsync(...args),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
vi.mock("expo-file-system", () => ({
|
|
16
|
+
readAsStringAsync: (...args: any[]) => mockReadAsStringAsync(...args),
|
|
17
|
+
EncodingType: { Base64: "base64" },
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock("react-native", () => {
|
|
21
|
+
const _React = require("react");
|
|
22
|
+
const View = ({ children, style, testID }: any) =>
|
|
23
|
+
_React.createElement("div", { "data-testid": testID, style }, children);
|
|
24
|
+
const Text = ({ children, style, testID }: any) =>
|
|
25
|
+
_React.createElement("span", { "data-testid": testID, style }, children);
|
|
26
|
+
const Pressable = ({ children, onPress, testID, style }: any) =>
|
|
27
|
+
_React.createElement(
|
|
28
|
+
"div",
|
|
29
|
+
{ "data-testid": testID, onClick: onPress, style },
|
|
30
|
+
children,
|
|
31
|
+
);
|
|
32
|
+
return {
|
|
33
|
+
View,
|
|
34
|
+
Text,
|
|
35
|
+
Pressable,
|
|
36
|
+
StyleSheet: { create: (s: any) => s, hairlineWidth: 1 },
|
|
37
|
+
Platform: { OS: "ios" },
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const hoisted = vi.hoisted(() => {
|
|
42
|
+
const _React = require("react");
|
|
43
|
+
return {
|
|
44
|
+
RealContext: _React.createContext(null),
|
|
45
|
+
MockCoreConstructor: vi.fn(),
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
let mockAgent: any;
|
|
50
|
+
let unsubscribeMock: ReturnType<typeof vi.fn>;
|
|
51
|
+
let mockRunAgent: ReturnType<typeof vi.fn>;
|
|
52
|
+
|
|
53
|
+
function createMockCore() {
|
|
54
|
+
return {
|
|
55
|
+
subscribe: vi.fn((_sub: any) => ({ unsubscribe: unsubscribeMock })),
|
|
56
|
+
subscribeToAgentWithOptions: vi.fn(() => ({ unsubscribe: vi.fn() })),
|
|
57
|
+
setRuntimeUrl: vi.fn(),
|
|
58
|
+
setRuntimeTransport: vi.fn(),
|
|
59
|
+
setHeaders: vi.fn(),
|
|
60
|
+
setCredentials: vi.fn(),
|
|
61
|
+
setProperties: vi.fn(),
|
|
62
|
+
setDebug: vi.fn(),
|
|
63
|
+
setDefaultThrottleMs: vi.fn(),
|
|
64
|
+
getAgent: vi.fn(() => undefined),
|
|
65
|
+
runAgent: (...args: any[]) => mockRunAgent(...args),
|
|
66
|
+
runtimeUrl: "https://api.test",
|
|
67
|
+
runtimeTransport: "auto",
|
|
68
|
+
runtimeConnectionStatus: "Disconnected",
|
|
69
|
+
headers: {},
|
|
70
|
+
agents: {},
|
|
71
|
+
defaultThrottleMs: undefined,
|
|
72
|
+
addTool: vi.fn(),
|
|
73
|
+
removeTool: vi.fn(),
|
|
74
|
+
getTool: vi.fn(() => undefined),
|
|
75
|
+
addHookRenderToolCall: vi.fn(),
|
|
76
|
+
registerThreadStore: vi.fn(),
|
|
77
|
+
unregisterThreadStore: vi.fn(),
|
|
78
|
+
intelligence: undefined,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
83
|
+
|
|
84
|
+
vi.mock("@copilotkit/react-core/v2/headless", () => {
|
|
85
|
+
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
86
|
+
hoisted.MockCoreConstructor(...args);
|
|
87
|
+
const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
|
|
88
|
+
if (instance) Object.assign(this, instance);
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
CopilotKitCoreReact,
|
|
92
|
+
useAgent: () => {
|
|
93
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
94
|
+
if (!ctx) {
|
|
95
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
96
|
+
}
|
|
97
|
+
return { agent: mockAgent };
|
|
98
|
+
},
|
|
99
|
+
useFrontendTool: () => {},
|
|
100
|
+
useComponent: () => {},
|
|
101
|
+
useHumanInTheLoop: () => {},
|
|
102
|
+
useInterrupt: () => {},
|
|
103
|
+
useSuggestions: () => {},
|
|
104
|
+
useConfigureSuggestions: () => {},
|
|
105
|
+
useAgentContext: () => {},
|
|
106
|
+
useThreads: () => ({
|
|
107
|
+
threads: [],
|
|
108
|
+
isLoading: false,
|
|
109
|
+
error: null,
|
|
110
|
+
hasMoreThreads: false,
|
|
111
|
+
isFetchingMoreThreads: false,
|
|
112
|
+
fetchMoreThreads: () => {},
|
|
113
|
+
renameThread: async () => {},
|
|
114
|
+
archiveThread: async () => {},
|
|
115
|
+
deleteThread: async () => {},
|
|
116
|
+
}),
|
|
117
|
+
CopilotChatConfigurationProvider: ({ children }: any) => children,
|
|
118
|
+
useCopilotChatConfiguration: () => null,
|
|
119
|
+
CopilotChatDefaultLabels: {},
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
vi.mock("@copilotkit/react-core/v2/context", () => {
|
|
124
|
+
const _React = require("react");
|
|
125
|
+
return {
|
|
126
|
+
CopilotKitContext: hoisted.RealContext,
|
|
127
|
+
LicenseContext: _React.createContext({
|
|
128
|
+
status: null,
|
|
129
|
+
license: null,
|
|
130
|
+
checkFeature: () => true,
|
|
131
|
+
getLimit: () => null,
|
|
132
|
+
}),
|
|
133
|
+
useCopilotKit: () => {
|
|
134
|
+
const ctx = _React.useContext(hoisted.RealContext);
|
|
135
|
+
if (!ctx) {
|
|
136
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
137
|
+
}
|
|
138
|
+
return { copilotkit: ctx };
|
|
139
|
+
},
|
|
140
|
+
useLicenseContext: () => ({
|
|
141
|
+
status: null,
|
|
142
|
+
license: null,
|
|
143
|
+
checkFeature: () => true,
|
|
144
|
+
getLimit: () => null,
|
|
145
|
+
}),
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
vi.mock("@copilotkit/shared", () => ({
|
|
150
|
+
DEFAULT_AGENT_ID: "default",
|
|
151
|
+
randomUUID: () => "test-uuid-" + Math.random().toString(36).slice(2, 8),
|
|
152
|
+
getModalityFromMimeType: (mimeType: string) => {
|
|
153
|
+
if (mimeType.startsWith("image/")) return "image";
|
|
154
|
+
if (mimeType.startsWith("audio/")) return "audio";
|
|
155
|
+
if (mimeType.startsWith("video/")) return "video";
|
|
156
|
+
return "document";
|
|
157
|
+
},
|
|
158
|
+
formatFileSize: (bytes: number) => `${bytes} B`,
|
|
159
|
+
createLicenseContextValue: () => ({
|
|
160
|
+
status: null,
|
|
161
|
+
license: null,
|
|
162
|
+
checkFeature: () => true,
|
|
163
|
+
getLimit: () => null,
|
|
164
|
+
}),
|
|
165
|
+
}));
|
|
166
|
+
|
|
167
|
+
import { CopilotKitProvider } from "../CopilotKitProvider";
|
|
168
|
+
import { CopilotChat, useCopilotChatContext } from "../CopilotChat";
|
|
169
|
+
import type { CopilotChatContextValue } from "../CopilotChat";
|
|
170
|
+
|
|
171
|
+
// ─── Tests ────────────────────────────────────────────────────────────────────
|
|
172
|
+
|
|
173
|
+
describe("Attachments integration: full pick -> attach -> submit flow", () => {
|
|
174
|
+
beforeEach(() => {
|
|
175
|
+
unsubscribeMock = vi.fn();
|
|
176
|
+
mockRunAgent = vi.fn().mockResolvedValue(undefined);
|
|
177
|
+
mockAgent = {
|
|
178
|
+
addMessage: vi.fn(),
|
|
179
|
+
isRunning: false,
|
|
180
|
+
messages: [],
|
|
181
|
+
threadId: undefined,
|
|
182
|
+
};
|
|
183
|
+
mockCoreInstance = createMockCore();
|
|
184
|
+
hoisted.MockCoreConstructor.mockClear();
|
|
185
|
+
hoisted.MockCoreConstructor.mockReturnValue(mockCoreInstance);
|
|
186
|
+
vi.clearAllMocks();
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
afterEach(() => {
|
|
190
|
+
vi.clearAllMocks();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("pick a file, see it in attachments, submit message with attachment data", async () => {
|
|
194
|
+
// Setup: DocumentPicker returns a JPEG file
|
|
195
|
+
mockGetDocumentAsync.mockResolvedValue({
|
|
196
|
+
canceled: false,
|
|
197
|
+
assets: [
|
|
198
|
+
{
|
|
199
|
+
uri: "file:///cache/photo.jpg",
|
|
200
|
+
name: "photo.jpg",
|
|
201
|
+
size: 4096,
|
|
202
|
+
mimeType: "image/jpeg",
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
});
|
|
206
|
+
mockReadAsStringAsync.mockResolvedValue("aGVsbG8="); // base64 "hello"
|
|
207
|
+
|
|
208
|
+
let chatCtx: CopilotChatContextValue | null = null;
|
|
209
|
+
|
|
210
|
+
function Consumer() {
|
|
211
|
+
chatCtx = useCopilotChatContext();
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
render(
|
|
216
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
217
|
+
<CopilotChat agentId="test-agent" attachments={{ enabled: true }}>
|
|
218
|
+
<Consumer />
|
|
219
|
+
</CopilotChat>
|
|
220
|
+
</CopilotKitProvider>,
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
// Verify initial state
|
|
224
|
+
expect(chatCtx!.attachments).toEqual([]);
|
|
225
|
+
expect(chatCtx!.attachmentsEnabled).toBe(true);
|
|
226
|
+
|
|
227
|
+
// Step 1: Open picker
|
|
228
|
+
await act(async () => {
|
|
229
|
+
await chatCtx!.openPicker();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Step 2: Verify attachment appeared
|
|
233
|
+
expect(chatCtx!.attachments).toHaveLength(1);
|
|
234
|
+
expect(chatCtx!.attachments[0]).toMatchObject({
|
|
235
|
+
type: "image",
|
|
236
|
+
filename: "photo.jpg",
|
|
237
|
+
size: 4096,
|
|
238
|
+
status: "ready",
|
|
239
|
+
source: {
|
|
240
|
+
type: "data",
|
|
241
|
+
value: "aGVsbG8=",
|
|
242
|
+
mimeType: "image/jpeg",
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// Step 3: Submit message with text + attachment
|
|
247
|
+
await act(async () => {
|
|
248
|
+
await chatCtx!.submitMessage("Check this photo");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
// Step 4: Verify addMessage was called with InputContent array
|
|
252
|
+
expect(mockAgent.addMessage).toHaveBeenCalledTimes(1);
|
|
253
|
+
const call = mockAgent.addMessage.mock.calls[0][0];
|
|
254
|
+
expect(call.role).toBe("user");
|
|
255
|
+
expect(Array.isArray(call.content)).toBe(true);
|
|
256
|
+
expect(call.content).toHaveLength(2);
|
|
257
|
+
|
|
258
|
+
// Text part
|
|
259
|
+
expect(call.content[0]).toMatchObject({
|
|
260
|
+
type: "text",
|
|
261
|
+
text: "Check this photo",
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// Image part
|
|
265
|
+
expect(call.content[1]).toMatchObject({
|
|
266
|
+
type: "image",
|
|
267
|
+
source: {
|
|
268
|
+
type: "data",
|
|
269
|
+
value: "aGVsbG8=",
|
|
270
|
+
mimeType: "image/jpeg",
|
|
271
|
+
},
|
|
272
|
+
metadata: {
|
|
273
|
+
filename: "photo.jpg",
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// Step 5: Verify attachments queue is now empty (consumed)
|
|
278
|
+
expect(chatCtx!.attachments).toHaveLength(0);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("submit without attachments sends plain text", async () => {
|
|
282
|
+
let chatCtx: CopilotChatContextValue | null = null;
|
|
283
|
+
|
|
284
|
+
function Consumer() {
|
|
285
|
+
chatCtx = useCopilotChatContext();
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
render(
|
|
290
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
291
|
+
<CopilotChat agentId="test-agent" attachments={{ enabled: true }}>
|
|
292
|
+
<Consumer />
|
|
293
|
+
</CopilotChat>
|
|
294
|
+
</CopilotKitProvider>,
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
await act(async () => {
|
|
298
|
+
await chatCtx!.submitMessage("Just text");
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
expect(mockAgent.addMessage).toHaveBeenCalledWith(
|
|
302
|
+
expect.objectContaining({
|
|
303
|
+
role: "user",
|
|
304
|
+
content: "Just text",
|
|
305
|
+
}),
|
|
306
|
+
);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("canceled picker does not add attachments", async () => {
|
|
310
|
+
mockGetDocumentAsync.mockResolvedValue({
|
|
311
|
+
canceled: true,
|
|
312
|
+
assets: [],
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
let chatCtx: CopilotChatContextValue | null = null;
|
|
316
|
+
|
|
317
|
+
function Consumer() {
|
|
318
|
+
chatCtx = useCopilotChatContext();
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
render(
|
|
323
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
324
|
+
<CopilotChat agentId="test-agent" attachments={{ enabled: true }}>
|
|
325
|
+
<Consumer />
|
|
326
|
+
</CopilotChat>
|
|
327
|
+
</CopilotKitProvider>,
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
await act(async () => {
|
|
331
|
+
await chatCtx!.openPicker();
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
expect(chatCtx!.attachments).toHaveLength(0);
|
|
335
|
+
});
|
|
336
|
+
});
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { render } from "@testing-library/react";
|
|
3
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
// ─── Mocks ────────────────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
// Track the agentId passed to useAgent across renders
|
|
8
|
+
let capturedAgentId: string | undefined;
|
|
9
|
+
|
|
10
|
+
const hoisted = vi.hoisted(() => {
|
|
11
|
+
const _React = require("react");
|
|
12
|
+
return {
|
|
13
|
+
RealContext: _React.createContext(null),
|
|
14
|
+
MockCoreConstructor: vi.fn(),
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
let unsubscribeMock: ReturnType<typeof vi.fn>;
|
|
19
|
+
|
|
20
|
+
function createMockCore() {
|
|
21
|
+
return {
|
|
22
|
+
subscribe: vi.fn((_subscriber: any) => ({
|
|
23
|
+
unsubscribe: unsubscribeMock,
|
|
24
|
+
})),
|
|
25
|
+
subscribeToAgentWithOptions: vi.fn((_agent: any, _handlers: any) => ({
|
|
26
|
+
unsubscribe: vi.fn(),
|
|
27
|
+
})),
|
|
28
|
+
setRuntimeUrl: vi.fn(),
|
|
29
|
+
setRuntimeTransport: vi.fn(),
|
|
30
|
+
setHeaders: vi.fn(),
|
|
31
|
+
setCredentials: vi.fn(),
|
|
32
|
+
setProperties: vi.fn(),
|
|
33
|
+
setDebug: vi.fn(),
|
|
34
|
+
setDefaultThrottleMs: vi.fn(),
|
|
35
|
+
getAgent: vi.fn(() => undefined),
|
|
36
|
+
runtimeUrl: "https://api.test",
|
|
37
|
+
runtimeTransport: "auto",
|
|
38
|
+
runtimeConnectionStatus: "Disconnected",
|
|
39
|
+
headers: {},
|
|
40
|
+
agents: {},
|
|
41
|
+
defaultThrottleMs: undefined,
|
|
42
|
+
addTool: vi.fn(),
|
|
43
|
+
removeTool: vi.fn(),
|
|
44
|
+
getTool: vi.fn(() => undefined),
|
|
45
|
+
addHookRenderToolCall: vi.fn(),
|
|
46
|
+
registerThreadStore: vi.fn(),
|
|
47
|
+
unregisterThreadStore: vi.fn(),
|
|
48
|
+
intelligence: undefined,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
53
|
+
|
|
54
|
+
vi.mock("@copilotkit/react-core/v2/headless", () => {
|
|
55
|
+
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
56
|
+
hoisted.MockCoreConstructor(...args);
|
|
57
|
+
const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
|
|
58
|
+
if (instance) Object.assign(this, instance);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
CopilotKitCoreReact,
|
|
62
|
+
useAgent: (props: any) => {
|
|
63
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
64
|
+
if (!ctx) {
|
|
65
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
66
|
+
}
|
|
67
|
+
capturedAgentId = props?.agentId;
|
|
68
|
+
return { agent: {} };
|
|
69
|
+
},
|
|
70
|
+
useFrontendTool: () => {},
|
|
71
|
+
useComponent: () => {},
|
|
72
|
+
useHumanInTheLoop: () => {},
|
|
73
|
+
useInterrupt: () => {},
|
|
74
|
+
useSuggestions: () => {},
|
|
75
|
+
useConfigureSuggestions: () => {},
|
|
76
|
+
useAgentContext: () => {},
|
|
77
|
+
useThreads: () => ({
|
|
78
|
+
threads: [],
|
|
79
|
+
isLoading: false,
|
|
80
|
+
error: null,
|
|
81
|
+
hasMoreThreads: false,
|
|
82
|
+
isFetchingMoreThreads: false,
|
|
83
|
+
fetchMoreThreads: () => {},
|
|
84
|
+
renameThread: async () => {},
|
|
85
|
+
archiveThread: async () => {},
|
|
86
|
+
deleteThread: async () => {},
|
|
87
|
+
}),
|
|
88
|
+
CopilotChatConfigurationProvider: ({ children }: any) => children,
|
|
89
|
+
useCopilotChatConfiguration: () => null,
|
|
90
|
+
CopilotChatDefaultLabels: {},
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
vi.mock("@copilotkit/react-core/v2/context", () => {
|
|
95
|
+
const _React = require("react");
|
|
96
|
+
return {
|
|
97
|
+
CopilotKitContext: hoisted.RealContext,
|
|
98
|
+
LicenseContext: _React.createContext({
|
|
99
|
+
status: null,
|
|
100
|
+
license: null,
|
|
101
|
+
checkFeature: () => true,
|
|
102
|
+
getLimit: () => null,
|
|
103
|
+
}),
|
|
104
|
+
useCopilotKit: () => {
|
|
105
|
+
const ctx = _React.useContext(hoisted.RealContext);
|
|
106
|
+
if (!ctx) {
|
|
107
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
108
|
+
}
|
|
109
|
+
return ctx;
|
|
110
|
+
},
|
|
111
|
+
useLicenseContext: () => ({
|
|
112
|
+
status: null,
|
|
113
|
+
license: null,
|
|
114
|
+
checkFeature: () => true,
|
|
115
|
+
getLimit: () => null,
|
|
116
|
+
}),
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
vi.mock("@copilotkit/shared", () => ({
|
|
121
|
+
DEFAULT_AGENT_ID: "default",
|
|
122
|
+
createLicenseContextValue: () => ({
|
|
123
|
+
status: null,
|
|
124
|
+
license: null,
|
|
125
|
+
checkFeature: () => true,
|
|
126
|
+
getLimit: () => null,
|
|
127
|
+
}),
|
|
128
|
+
}));
|
|
129
|
+
|
|
130
|
+
// Import after mocks
|
|
131
|
+
import { CopilotKitProvider } from "../CopilotKitProvider";
|
|
132
|
+
import { CopilotChat } from "../CopilotChat";
|
|
133
|
+
import { CopilotModal } from "../CopilotModal";
|
|
134
|
+
|
|
135
|
+
// ─── Tests ────────────────────────────────────────────────────────────────────
|
|
136
|
+
|
|
137
|
+
describe("CopilotChat agentId resolution", () => {
|
|
138
|
+
beforeEach(() => {
|
|
139
|
+
capturedAgentId = undefined;
|
|
140
|
+
unsubscribeMock = vi.fn();
|
|
141
|
+
mockCoreInstance = createMockCore();
|
|
142
|
+
hoisted.MockCoreConstructor.mockClear();
|
|
143
|
+
hoisted.MockCoreConstructor.mockReturnValue(mockCoreInstance);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
afterEach(() => {
|
|
147
|
+
vi.clearAllMocks();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("uses agentId when provided", () => {
|
|
151
|
+
render(
|
|
152
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
153
|
+
<CopilotChat agentId="my-agent" />
|
|
154
|
+
</CopilotKitProvider>,
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
expect(capturedAgentId).toBe("my-agent");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("falls back to agentName when agentId is not provided", () => {
|
|
161
|
+
render(
|
|
162
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
163
|
+
<CopilotChat agentName="legacy-agent" />
|
|
164
|
+
</CopilotKitProvider>,
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
expect(capturedAgentId).toBe("legacy-agent");
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("agentId takes priority over agentName when both provided", () => {
|
|
171
|
+
render(
|
|
172
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
173
|
+
<CopilotChat agentId="primary" agentName="legacy" />
|
|
174
|
+
</CopilotKitProvider>,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
expect(capturedAgentId).toBe("primary");
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("falls back to DEFAULT_AGENT_ID when neither provided", () => {
|
|
181
|
+
render(
|
|
182
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
183
|
+
<CopilotChat />
|
|
184
|
+
</CopilotKitProvider>,
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
expect(capturedAgentId).toBe("default");
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("logs deprecation warning when agentName is used without agentId", () => {
|
|
191
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
192
|
+
|
|
193
|
+
render(
|
|
194
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
195
|
+
<CopilotChat agentName="old-name" />
|
|
196
|
+
</CopilotKitProvider>,
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
200
|
+
"[CopilotKit] agentName is deprecated, use agentId instead",
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
warnSpy.mockRestore();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("does not log deprecation warning when agentId is used", () => {
|
|
207
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
208
|
+
|
|
209
|
+
render(
|
|
210
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
211
|
+
<CopilotChat agentId="my-agent" />
|
|
212
|
+
</CopilotKitProvider>,
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
216
|
+
|
|
217
|
+
warnSpy.mockRestore();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("does not log deprecation warning when both agentId and agentName are provided", () => {
|
|
221
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
222
|
+
|
|
223
|
+
render(
|
|
224
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
225
|
+
<CopilotChat agentId="primary" agentName="legacy" />
|
|
226
|
+
</CopilotKitProvider>,
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
230
|
+
|
|
231
|
+
warnSpy.mockRestore();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("renders children", () => {
|
|
235
|
+
const { getByText } = render(
|
|
236
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
237
|
+
<CopilotChat agentId="my-agent">
|
|
238
|
+
<span>Hello Chat</span>
|
|
239
|
+
</CopilotChat>
|
|
240
|
+
</CopilotKitProvider>,
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
expect(getByText("Hello Chat")).toBeTruthy();
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe("CopilotModal agentId resolution", () => {
|
|
248
|
+
beforeEach(() => {
|
|
249
|
+
capturedAgentId = undefined;
|
|
250
|
+
unsubscribeMock = vi.fn();
|
|
251
|
+
mockCoreInstance = createMockCore();
|
|
252
|
+
hoisted.MockCoreConstructor.mockClear();
|
|
253
|
+
hoisted.MockCoreConstructor.mockReturnValue(mockCoreInstance);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
afterEach(() => {
|
|
257
|
+
vi.clearAllMocks();
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("uses agentId when provided", () => {
|
|
261
|
+
render(
|
|
262
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
263
|
+
<CopilotModal agentId="modal-agent" />
|
|
264
|
+
</CopilotKitProvider>,
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
expect(capturedAgentId).toBe("modal-agent");
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("falls back to agentName for backwards compatibility", () => {
|
|
271
|
+
render(
|
|
272
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
273
|
+
<CopilotModal agentName="legacy-modal" />
|
|
274
|
+
</CopilotKitProvider>,
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
expect(capturedAgentId).toBe("legacy-modal");
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("agentId takes priority over agentName", () => {
|
|
281
|
+
render(
|
|
282
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
283
|
+
<CopilotModal agentId="new" agentName="old" />
|
|
284
|
+
</CopilotKitProvider>,
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
expect(capturedAgentId).toBe("new");
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("logs deprecation warning when agentName used without agentId", () => {
|
|
291
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
292
|
+
|
|
293
|
+
render(
|
|
294
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
295
|
+
<CopilotModal agentName="old-modal" />
|
|
296
|
+
</CopilotKitProvider>,
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
300
|
+
"[CopilotKit] agentName is deprecated, use agentId instead",
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
warnSpy.mockRestore();
|
|
304
|
+
});
|
|
305
|
+
});
|