@copilotkit/react-core 1.55.0-next.8 → 1.55.0
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 +48 -5
- package/dist/{copilotkit-DNYSFuz5.mjs → copilotkit-BY5S1-0P.mjs} +2772 -858
- package/dist/copilotkit-BY5S1-0P.mjs.map +1 -0
- package/dist/{copilotkit-Dy5w3qEV.d.mts → copilotkit-BuhSUZHb.d.mts} +230 -17
- package/dist/copilotkit-BuhSUZHb.d.mts.map +1 -0
- package/dist/{copilotkit-B3Mb1yVE.cjs → copilotkit-Bz5-ImDl.cjs} +2776 -832
- package/dist/copilotkit-Bz5-ImDl.cjs.map +1 -0
- package/dist/{copilotkit-DBzgOMby.d.cts → copilotkit-dwDWYpya.d.cts} +230 -17
- package/dist/copilotkit-dwDWYpya.d.cts.map +1 -0
- package/dist/index.cjs +9 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +9 -4
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1624 -396
- package/dist/index.umd.js.map +1 -1
- package/dist/v2/index.cjs +13 -1
- package/dist/v2/index.css +1 -1
- package/dist/v2/index.d.cts +3 -3
- package/dist/v2/index.d.mts +3 -3
- package/dist/v2/index.mjs +3 -2
- package/dist/v2/index.umd.js +2746 -790
- package/dist/v2/index.umd.js.map +1 -1
- package/package.json +62 -54
- package/scripts/scope-preflight.mjs +1 -2
- package/src/components/CopilotListeners.tsx +41 -8
- package/src/components/copilot-provider/__tests__/copilot-messages-key.test.tsx +92 -0
- package/src/components/copilot-provider/copilotkit-props.tsx +4 -2
- package/src/components/copilot-provider/copilotkit.tsx +3 -3
- package/src/components/toast/toast-provider.tsx +269 -194
- package/src/hooks/__tests__/use-copilot-chat-internal-connect.test.tsx +27 -16
- package/src/hooks/use-copilot-chat_internal.ts +15 -4
- package/src/v2/__tests__/A2UIMessageRenderer.test.tsx +86 -22
- package/src/v2/__tests__/utils/test-helpers.tsx +107 -7
- package/src/v2/a2ui/A2UICatalogContext.tsx +79 -0
- package/src/v2/a2ui/A2UIMessageRenderer.tsx +125 -37
- package/src/v2/a2ui/A2UIToolCallRenderer.tsx +290 -0
- package/src/v2/components/CopilotKitInspector.tsx +2 -0
- package/src/v2/components/OpenGenerativeUIRenderer.tsx +598 -0
- package/src/v2/components/__tests__/OpenGenerativeUIRenderer.test.tsx +665 -0
- package/src/v2/components/chat/CopilotChat.tsx +197 -52
- package/src/v2/components/chat/CopilotChatAssistantMessage.tsx +17 -2
- package/src/v2/components/chat/CopilotChatAttachmentQueue.tsx +481 -0
- package/src/v2/components/chat/CopilotChatAttachmentRenderer.tsx +139 -0
- package/src/v2/components/chat/CopilotChatInput.tsx +146 -77
- package/src/v2/components/chat/CopilotChatMessageView.tsx +260 -151
- package/src/v2/components/chat/CopilotChatSuggestionView.tsx +1 -0
- package/src/v2/components/chat/CopilotChatUserMessage.tsx +54 -0
- package/src/v2/components/chat/CopilotChatView.tsx +179 -66
- package/src/v2/components/chat/__tests__/CopilotChat.attachments.test.tsx +168 -0
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx +63 -2
- package/src/v2/components/chat/__tests__/CopilotChatInput.test.tsx +544 -1
- package/src/v2/components/chat/__tests__/CopilotChatPerf.e2e.test.tsx +268 -0
- package/src/v2/components/chat/__tests__/CopilotChatPropsRerender.e2e.test.tsx +249 -0
- package/src/v2/components/chat/__tests__/CopilotChatToolRendering.e2e.test.tsx +5 -2
- package/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsx +5 -2
- package/src/v2/components/chat/__tests__/MCPAppsActivityRenderer.e2e.test.tsx +60 -3
- package/src/v2/components/chat/__tests__/copilot-chat-throttle.test.tsx +138 -0
- package/src/v2/components/chat/index.ts +9 -0
- package/src/v2/components/chat/scroll-element-context.ts +13 -0
- package/src/v2/hooks/__tests__/use-agent-context-timing.e2e.test.tsx +8 -0
- package/src/v2/hooks/__tests__/use-agent-thread-isolation.test.tsx +327 -0
- package/src/v2/hooks/__tests__/use-agent-throttle.test.tsx +1003 -0
- package/src/v2/hooks/__tests__/use-agent.e2e.test.tsx +13 -2
- package/src/v2/hooks/__tests__/use-attachments.test.tsx +169 -0
- package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.tsx +23 -4
- package/src/v2/hooks/__tests__/use-threads.test.tsx +54 -0
- package/src/v2/hooks/index.ts +5 -0
- package/src/v2/hooks/use-agent.tsx +220 -15
- package/src/v2/hooks/use-attachments.tsx +269 -0
- package/src/v2/hooks/use-frontend-tool.tsx +5 -2
- package/src/v2/hooks/use-render-activity-message.tsx +9 -2
- package/src/v2/hooks/use-render-custom-messages.tsx +6 -1
- package/src/v2/hooks/use-threads.tsx +35 -15
- package/src/v2/index.ts +5 -1
- package/src/v2/lib/__tests__/processPartialHtml.test.ts +112 -0
- package/src/v2/lib/__tests__/slots.test.ts +56 -0
- package/src/v2/lib/processPartialHtml.ts +45 -0
- package/src/v2/lib/slots.tsx +42 -1
- package/src/v2/providers/CopilotChatConfigurationProvider.tsx +9 -3
- package/src/v2/providers/CopilotKitProvider.tsx +268 -32
- package/src/v2/providers/SandboxFunctionsContext.ts +10 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.sandboxFunctions.test.tsx +198 -0
- package/src/v2/providers/__tests__/CopilotKitProvider.test.tsx +71 -0
- package/src/v2/providers/index.ts +7 -0
- package/src/v2/styles/globals.css +2 -1
- package/src/v2/types/index.ts +1 -0
- package/src/v2/types/sandbox-function.ts +11 -0
- package/dist/copilotkit-B3Mb1yVE.cjs.map +0 -1
- package/dist/copilotkit-DBzgOMby.d.cts.map +0 -1
- package/dist/copilotkit-DNYSFuz5.mjs.map +0 -1
- package/dist/copilotkit-Dy5w3qEV.d.mts.map +0 -1
- package/src/v2/components/__tests__/license-warning-banner.test.tsx +0 -46
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
import { render, cleanup, act } from "@testing-library/react";
|
|
2
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import {
|
|
6
|
+
OpenGenerativeUIActivityRenderer,
|
|
7
|
+
OpenGenerativeUIContent,
|
|
8
|
+
} from "../OpenGenerativeUIRenderer";
|
|
9
|
+
import { SandboxFunctionsContext } from "../../providers/SandboxFunctionsContext";
|
|
10
|
+
import type { SandboxFunction } from "../../types/sandbox-function";
|
|
11
|
+
|
|
12
|
+
// Mock @jetbrains/websandbox
|
|
13
|
+
const mockRun = vi.fn().mockResolvedValue(undefined);
|
|
14
|
+
const mockDestroy = vi.fn();
|
|
15
|
+
let mockIframe: HTMLIFrameElement;
|
|
16
|
+
let mockPromiseResolve: () => void;
|
|
17
|
+
let mockPromise: Promise<unknown>;
|
|
18
|
+
|
|
19
|
+
function resetMockPromise() {
|
|
20
|
+
mockPromise = new Promise<void>((resolve) => {
|
|
21
|
+
mockPromiseResolve = resolve;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const mockCreate = vi.fn(() => {
|
|
26
|
+
mockIframe = document.createElement("iframe");
|
|
27
|
+
return {
|
|
28
|
+
iframe: mockIframe,
|
|
29
|
+
promise: mockPromise,
|
|
30
|
+
run: mockRun,
|
|
31
|
+
destroy: mockDestroy,
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
vi.mock("@jetbrains/websandbox", () => ({
|
|
36
|
+
default: {
|
|
37
|
+
create: (...args: unknown[]) => mockCreate(...args),
|
|
38
|
+
},
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
/** Flush the dynamic import() microtask so the sandbox gets created */
|
|
42
|
+
async function flushImport() {
|
|
43
|
+
await act(async () => {
|
|
44
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function renderRenderer(content: OpenGenerativeUIContent) {
|
|
49
|
+
return render(
|
|
50
|
+
<OpenGenerativeUIActivityRenderer
|
|
51
|
+
activityType="open-generative-ui"
|
|
52
|
+
content={content}
|
|
53
|
+
message={{}}
|
|
54
|
+
agent={{}}
|
|
55
|
+
/>,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
describe("OpenGenerativeUIActivityRenderer", () => {
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
vi.clearAllMocks();
|
|
62
|
+
resetMockPromise();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
afterEach(() => {
|
|
66
|
+
cleanup();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("renders placeholder when no html", async () => {
|
|
70
|
+
const { container } = renderRenderer({ initialHeight: 300 });
|
|
71
|
+
await flushImport();
|
|
72
|
+
|
|
73
|
+
const div = container.firstElementChild as HTMLElement;
|
|
74
|
+
expect(div.style.height).toBe("300px");
|
|
75
|
+
expect(div.querySelector("svg")).not.toBeNull();
|
|
76
|
+
expect(mockCreate).not.toHaveBeenCalled();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("creates sandbox when html is complete", async () => {
|
|
80
|
+
const html = "<head></head><body><p>Hello</p></body>";
|
|
81
|
+
renderRenderer({ html: [html], htmlComplete: true });
|
|
82
|
+
await flushImport();
|
|
83
|
+
|
|
84
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
85
|
+
const [, options] = mockCreate.mock.calls[0];
|
|
86
|
+
expect(options.frameContent).toBe(html);
|
|
87
|
+
expect(options.frameContainer).toBeInstanceOf(HTMLElement);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("creates preview sandbox when html is streaming (not complete)", async () => {
|
|
91
|
+
renderRenderer({
|
|
92
|
+
html: ["<head></head><body>partial"],
|
|
93
|
+
htmlComplete: false,
|
|
94
|
+
cssComplete: true,
|
|
95
|
+
});
|
|
96
|
+
await flushImport();
|
|
97
|
+
|
|
98
|
+
// Preview sandbox is created with empty body template
|
|
99
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
100
|
+
const [, options] = mockCreate.mock.calls[0];
|
|
101
|
+
expect(options.frameContent).toBe("<head></head><body></body>");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("wraps html missing <head>", async () => {
|
|
105
|
+
renderRenderer({
|
|
106
|
+
html: ["<body><p>No head</p></body>"],
|
|
107
|
+
htmlComplete: true,
|
|
108
|
+
});
|
|
109
|
+
await flushImport();
|
|
110
|
+
|
|
111
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
112
|
+
const [, options] = mockCreate.mock.calls[0];
|
|
113
|
+
expect(options.frameContent).toContain("<head></head>");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("joins html chunks when complete", async () => {
|
|
117
|
+
renderRenderer({
|
|
118
|
+
html: ["<head></head>", "<body>", "<p>Hello</p>", "</body>"],
|
|
119
|
+
htmlComplete: true,
|
|
120
|
+
});
|
|
121
|
+
await flushImport();
|
|
122
|
+
|
|
123
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
124
|
+
const [, options] = mockCreate.mock.calls[0];
|
|
125
|
+
expect(options.frameContent).toBe("<head></head><body><p>Hello</p></body>");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("destroys sandbox on unmount", async () => {
|
|
129
|
+
const { unmount } = renderRenderer({
|
|
130
|
+
html: ["<head></head><body></body>"],
|
|
131
|
+
htmlComplete: true,
|
|
132
|
+
});
|
|
133
|
+
await flushImport();
|
|
134
|
+
|
|
135
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
136
|
+
unmount();
|
|
137
|
+
expect(mockDestroy).toHaveBeenCalledTimes(1);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("injects jsFunctions via run()", async () => {
|
|
141
|
+
const jsFunctions = "function greet() { return 'hi'; }";
|
|
142
|
+
renderRenderer({
|
|
143
|
+
html: ["<head></head><body></body>"],
|
|
144
|
+
htmlComplete: true,
|
|
145
|
+
jsFunctions,
|
|
146
|
+
});
|
|
147
|
+
await flushImport();
|
|
148
|
+
|
|
149
|
+
// Resolve sandbox promise
|
|
150
|
+
await act(async () => {
|
|
151
|
+
mockPromiseResolve();
|
|
152
|
+
await mockPromise;
|
|
153
|
+
});
|
|
154
|
+
await flushImport();
|
|
155
|
+
|
|
156
|
+
expect(mockRun).toHaveBeenCalledWith(jsFunctions);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("executes jsExpressions sequentially", async () => {
|
|
160
|
+
renderRenderer({
|
|
161
|
+
html: ["<head></head><body></body>"],
|
|
162
|
+
htmlComplete: true,
|
|
163
|
+
jsExpressions: ["expr1()", "expr2()"],
|
|
164
|
+
});
|
|
165
|
+
await flushImport();
|
|
166
|
+
|
|
167
|
+
await act(async () => {
|
|
168
|
+
mockPromiseResolve();
|
|
169
|
+
await mockPromise;
|
|
170
|
+
});
|
|
171
|
+
await flushImport();
|
|
172
|
+
|
|
173
|
+
expect(mockRun).toHaveBeenCalledWith("expr1()");
|
|
174
|
+
expect(mockRun).toHaveBeenCalledWith("expr2()");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("tracks index — no re-execution on re-render", async () => {
|
|
178
|
+
const { rerender } = render(
|
|
179
|
+
<OpenGenerativeUIActivityRenderer
|
|
180
|
+
activityType="open-generative-ui"
|
|
181
|
+
content={{
|
|
182
|
+
html: ["<head></head><body></body>"],
|
|
183
|
+
htmlComplete: true,
|
|
184
|
+
jsExpressions: ["expr1()"],
|
|
185
|
+
}}
|
|
186
|
+
message={{}}
|
|
187
|
+
agent={{}}
|
|
188
|
+
/>,
|
|
189
|
+
);
|
|
190
|
+
await flushImport();
|
|
191
|
+
|
|
192
|
+
await act(async () => {
|
|
193
|
+
mockPromiseResolve();
|
|
194
|
+
await mockPromise;
|
|
195
|
+
});
|
|
196
|
+
await flushImport();
|
|
197
|
+
|
|
198
|
+
const callCountAfterFirst = mockRun.mock.calls.filter(
|
|
199
|
+
(c: unknown[]) => c[0] === "expr1()",
|
|
200
|
+
).length;
|
|
201
|
+
expect(callCountAfterFirst).toBe(1);
|
|
202
|
+
|
|
203
|
+
// Re-render with additional expression
|
|
204
|
+
rerender(
|
|
205
|
+
<OpenGenerativeUIActivityRenderer
|
|
206
|
+
activityType="open-generative-ui"
|
|
207
|
+
content={{
|
|
208
|
+
html: ["<head></head><body></body>"],
|
|
209
|
+
htmlComplete: true,
|
|
210
|
+
jsExpressions: ["expr1()", "expr2()"],
|
|
211
|
+
}}
|
|
212
|
+
message={{}}
|
|
213
|
+
agent={{}}
|
|
214
|
+
/>,
|
|
215
|
+
);
|
|
216
|
+
await flushImport();
|
|
217
|
+
|
|
218
|
+
// expr1 should NOT have been called again
|
|
219
|
+
const expr1Calls = mockRun.mock.calls.filter(
|
|
220
|
+
(c: unknown[]) => c[0] === "expr1()",
|
|
221
|
+
).length;
|
|
222
|
+
expect(expr1Calls).toBe(1);
|
|
223
|
+
|
|
224
|
+
// expr2 should have been called
|
|
225
|
+
const expr2Calls = mockRun.mock.calls.filter(
|
|
226
|
+
(c: unknown[]) => c[0] === "expr2()",
|
|
227
|
+
).length;
|
|
228
|
+
expect(expr2Calls).toBe(1);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("queues JS before sandbox ready", async () => {
|
|
232
|
+
renderRenderer({
|
|
233
|
+
html: ["<head></head><body></body>"],
|
|
234
|
+
htmlComplete: true,
|
|
235
|
+
jsFunctions: "function foo() {}",
|
|
236
|
+
jsExpressions: ["foo()"],
|
|
237
|
+
});
|
|
238
|
+
await flushImport();
|
|
239
|
+
|
|
240
|
+
// Sandbox not ready yet — run should not have been called
|
|
241
|
+
expect(mockRun).not.toHaveBeenCalled();
|
|
242
|
+
|
|
243
|
+
// Now resolve
|
|
244
|
+
await act(async () => {
|
|
245
|
+
mockPromiseResolve();
|
|
246
|
+
await mockPromise;
|
|
247
|
+
});
|
|
248
|
+
await flushImport();
|
|
249
|
+
|
|
250
|
+
// Both should have been flushed
|
|
251
|
+
expect(mockRun).toHaveBeenCalledWith("function foo() {}");
|
|
252
|
+
expect(mockRun).toHaveBeenCalledWith("foo()");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("recreates sandbox when html changes", async () => {
|
|
256
|
+
const { rerender } = render(
|
|
257
|
+
<OpenGenerativeUIActivityRenderer
|
|
258
|
+
activityType="open-generative-ui"
|
|
259
|
+
content={{ html: ["<head></head><body>v1</body>"], htmlComplete: true }}
|
|
260
|
+
message={{}}
|
|
261
|
+
agent={{}}
|
|
262
|
+
/>,
|
|
263
|
+
);
|
|
264
|
+
await flushImport();
|
|
265
|
+
|
|
266
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
267
|
+
await act(async () => {
|
|
268
|
+
mockPromiseResolve();
|
|
269
|
+
await mockPromise;
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// Change html — need a new promise for the new sandbox
|
|
273
|
+
resetMockPromise();
|
|
274
|
+
rerender(
|
|
275
|
+
<OpenGenerativeUIActivityRenderer
|
|
276
|
+
activityType="open-generative-ui"
|
|
277
|
+
content={{ html: ["<head></head><body>v2</body>"], htmlComplete: true }}
|
|
278
|
+
message={{}}
|
|
279
|
+
agent={{}}
|
|
280
|
+
/>,
|
|
281
|
+
);
|
|
282
|
+
await flushImport();
|
|
283
|
+
|
|
284
|
+
// Old sandbox destroyed, new one created
|
|
285
|
+
expect(mockDestroy).toHaveBeenCalledTimes(1);
|
|
286
|
+
expect(mockCreate).toHaveBeenCalledTimes(2);
|
|
287
|
+
|
|
288
|
+
const [, options] = mockCreate.mock.calls[1];
|
|
289
|
+
expect(options.frameContent).toContain("v2");
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
describe("sandboxFunctions / localApi", () => {
|
|
293
|
+
function renderWithSandboxFunctions(
|
|
294
|
+
content: OpenGenerativeUIContent,
|
|
295
|
+
sandboxFunctions: SandboxFunction[],
|
|
296
|
+
) {
|
|
297
|
+
return render(
|
|
298
|
+
<SandboxFunctionsContext.Provider value={sandboxFunctions}>
|
|
299
|
+
<OpenGenerativeUIActivityRenderer
|
|
300
|
+
activityType="open-generative-ui"
|
|
301
|
+
content={content}
|
|
302
|
+
message={{}}
|
|
303
|
+
agent={{}}
|
|
304
|
+
/>
|
|
305
|
+
</SandboxFunctionsContext.Provider>,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
it("passes localApi built from sandbox functions to websandbox", async () => {
|
|
310
|
+
const handler = vi.fn().mockResolvedValue(42);
|
|
311
|
+
const fns: SandboxFunction[] = [
|
|
312
|
+
{
|
|
313
|
+
name: "addToCart",
|
|
314
|
+
description: "Add item to cart",
|
|
315
|
+
parameters: z.object({ itemId: z.string() }),
|
|
316
|
+
handler,
|
|
317
|
+
},
|
|
318
|
+
];
|
|
319
|
+
|
|
320
|
+
renderWithSandboxFunctions(
|
|
321
|
+
{ html: ["<head></head><body>test</body>"], htmlComplete: true },
|
|
322
|
+
fns,
|
|
323
|
+
);
|
|
324
|
+
await flushImport();
|
|
325
|
+
|
|
326
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
327
|
+
const [localApi] = mockCreate.mock.calls[0];
|
|
328
|
+
expect(localApi).toHaveProperty("addToCart");
|
|
329
|
+
expect(localApi.addToCart).toBe(handler);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it("passes empty localApi when no sandbox functions", async () => {
|
|
333
|
+
renderWithSandboxFunctions(
|
|
334
|
+
{ html: ["<head></head><body>test</body>"], htmlComplete: true },
|
|
335
|
+
[],
|
|
336
|
+
);
|
|
337
|
+
await flushImport();
|
|
338
|
+
|
|
339
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
340
|
+
const [localApi] = mockCreate.mock.calls[0];
|
|
341
|
+
expect(Object.keys(localApi)).toHaveLength(0);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it("recreates sandbox when sandbox functions change", async () => {
|
|
345
|
+
const handler1 = vi.fn();
|
|
346
|
+
const fns1: SandboxFunction[] = [
|
|
347
|
+
{
|
|
348
|
+
name: "fn1",
|
|
349
|
+
description: "first",
|
|
350
|
+
parameters: z.object({}),
|
|
351
|
+
handler: handler1,
|
|
352
|
+
},
|
|
353
|
+
];
|
|
354
|
+
|
|
355
|
+
const { rerender } = render(
|
|
356
|
+
<SandboxFunctionsContext.Provider value={fns1}>
|
|
357
|
+
<OpenGenerativeUIActivityRenderer
|
|
358
|
+
activityType="open-generative-ui"
|
|
359
|
+
content={{
|
|
360
|
+
html: ["<head></head><body>test</body>"],
|
|
361
|
+
htmlComplete: true,
|
|
362
|
+
}}
|
|
363
|
+
message={{}}
|
|
364
|
+
agent={{}}
|
|
365
|
+
/>
|
|
366
|
+
</SandboxFunctionsContext.Provider>,
|
|
367
|
+
);
|
|
368
|
+
await flushImport();
|
|
369
|
+
|
|
370
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
371
|
+
await act(async () => {
|
|
372
|
+
mockPromiseResolve();
|
|
373
|
+
await mockPromise;
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
// Change sandbox functions
|
|
377
|
+
const handler2 = vi.fn();
|
|
378
|
+
const fns2: SandboxFunction[] = [
|
|
379
|
+
{
|
|
380
|
+
name: "fn2",
|
|
381
|
+
description: "second",
|
|
382
|
+
parameters: z.object({}),
|
|
383
|
+
handler: handler2,
|
|
384
|
+
},
|
|
385
|
+
];
|
|
386
|
+
|
|
387
|
+
resetMockPromise();
|
|
388
|
+
rerender(
|
|
389
|
+
<SandboxFunctionsContext.Provider value={fns2}>
|
|
390
|
+
<OpenGenerativeUIActivityRenderer
|
|
391
|
+
activityType="open-generative-ui"
|
|
392
|
+
content={{
|
|
393
|
+
html: ["<head></head><body>test</body>"],
|
|
394
|
+
htmlComplete: true,
|
|
395
|
+
}}
|
|
396
|
+
message={{}}
|
|
397
|
+
agent={{}}
|
|
398
|
+
/>
|
|
399
|
+
</SandboxFunctionsContext.Provider>,
|
|
400
|
+
);
|
|
401
|
+
await flushImport();
|
|
402
|
+
|
|
403
|
+
// Old sandbox destroyed, new one created with new localApi
|
|
404
|
+
expect(mockDestroy).toHaveBeenCalledTimes(1);
|
|
405
|
+
expect(mockCreate).toHaveBeenCalledTimes(2);
|
|
406
|
+
const [localApi] = mockCreate.mock.calls[1];
|
|
407
|
+
expect(localApi).toHaveProperty("fn2");
|
|
408
|
+
expect(localApi.fn2).toBe(handler2);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it("includes multiple sandbox functions in localApi", async () => {
|
|
412
|
+
const handlerA = vi.fn();
|
|
413
|
+
const handlerB = vi.fn();
|
|
414
|
+
const fns: SandboxFunction[] = [
|
|
415
|
+
{
|
|
416
|
+
name: "fnA",
|
|
417
|
+
description: "A",
|
|
418
|
+
parameters: z.object({}),
|
|
419
|
+
handler: handlerA,
|
|
420
|
+
},
|
|
421
|
+
{
|
|
422
|
+
name: "fnB",
|
|
423
|
+
description: "B",
|
|
424
|
+
parameters: z.object({}),
|
|
425
|
+
handler: handlerB,
|
|
426
|
+
},
|
|
427
|
+
];
|
|
428
|
+
|
|
429
|
+
renderWithSandboxFunctions(
|
|
430
|
+
{ html: ["<head></head><body>test</body>"], htmlComplete: true },
|
|
431
|
+
fns,
|
|
432
|
+
);
|
|
433
|
+
await flushImport();
|
|
434
|
+
|
|
435
|
+
const [localApi] = mockCreate.mock.calls[0];
|
|
436
|
+
expect(Object.keys(localApi)).toHaveLength(2);
|
|
437
|
+
expect(localApi.fnA).toBe(handlerA);
|
|
438
|
+
expect(localApi.fnB).toBe(handlerB);
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
describe("progressive streaming preview", () => {
|
|
443
|
+
it("creates preview sandbox when chunks arrive but htmlComplete is false", async () => {
|
|
444
|
+
renderRenderer({
|
|
445
|
+
html: ["<body><div>Hello</div>"],
|
|
446
|
+
htmlComplete: false,
|
|
447
|
+
cssComplete: true,
|
|
448
|
+
generating: true,
|
|
449
|
+
});
|
|
450
|
+
await flushImport();
|
|
451
|
+
|
|
452
|
+
// Preview sandbox created with empty body template
|
|
453
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
454
|
+
const [localApi, options] = mockCreate.mock.calls[0];
|
|
455
|
+
expect(options.frameContent).toBe("<head></head><body></body>");
|
|
456
|
+
expect(Object.keys(localApi)).toHaveLength(0);
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it("updates preview on re-render with more chunks (after throttle)", async () => {
|
|
460
|
+
const { rerender } = render(
|
|
461
|
+
<OpenGenerativeUIActivityRenderer
|
|
462
|
+
activityType="open-generative-ui"
|
|
463
|
+
content={{
|
|
464
|
+
html: ["<body><div>Hello</div>"],
|
|
465
|
+
htmlComplete: false,
|
|
466
|
+
cssComplete: true,
|
|
467
|
+
generating: true,
|
|
468
|
+
}}
|
|
469
|
+
message={{}}
|
|
470
|
+
agent={{}}
|
|
471
|
+
/>,
|
|
472
|
+
);
|
|
473
|
+
await flushImport();
|
|
474
|
+
|
|
475
|
+
// Resolve preview sandbox promise
|
|
476
|
+
await act(async () => {
|
|
477
|
+
mockPromiseResolve();
|
|
478
|
+
await mockPromise;
|
|
479
|
+
});
|
|
480
|
+
await flushImport();
|
|
481
|
+
|
|
482
|
+
// Should have called run with innerHTML update (initial content — immediate flush)
|
|
483
|
+
const innerHtmlCalls = mockRun.mock.calls.filter(
|
|
484
|
+
(c: unknown[]) =>
|
|
485
|
+
typeof c[0] === "string" &&
|
|
486
|
+
(c[0] as string).includes("document.body.innerHTML"),
|
|
487
|
+
);
|
|
488
|
+
expect(innerHtmlCalls.length).toBeGreaterThanOrEqual(1);
|
|
489
|
+
|
|
490
|
+
// Re-render with more chunks — throttled by the outer wrapper
|
|
491
|
+
mockRun.mockClear();
|
|
492
|
+
rerender(
|
|
493
|
+
<OpenGenerativeUIActivityRenderer
|
|
494
|
+
activityType="open-generative-ui"
|
|
495
|
+
content={{
|
|
496
|
+
html: ["<body><div>Hello</div>", "<p>World</p>"],
|
|
497
|
+
htmlComplete: false,
|
|
498
|
+
cssComplete: true,
|
|
499
|
+
generating: true,
|
|
500
|
+
}}
|
|
501
|
+
message={{}}
|
|
502
|
+
agent={{}}
|
|
503
|
+
/>,
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
// Wait for the 1s throttle window to pass
|
|
507
|
+
await act(async () => {
|
|
508
|
+
await new Promise((r) => setTimeout(r, 1100));
|
|
509
|
+
});
|
|
510
|
+
await flushImport();
|
|
511
|
+
|
|
512
|
+
// Should have updated innerHTML with new content after throttle
|
|
513
|
+
const updateCalls = mockRun.mock.calls.filter(
|
|
514
|
+
(c: unknown[]) =>
|
|
515
|
+
typeof c[0] === "string" &&
|
|
516
|
+
(c[0] as string).includes("document.body.innerHTML"),
|
|
517
|
+
);
|
|
518
|
+
expect(updateCalls.length).toBeGreaterThanOrEqual(1);
|
|
519
|
+
expect(updateCalls[updateCalls.length - 1]![0]).toContain("World");
|
|
520
|
+
}, 10000);
|
|
521
|
+
|
|
522
|
+
it("destroys preview and creates final sandbox when htmlComplete arrives", async () => {
|
|
523
|
+
const { rerender } = render(
|
|
524
|
+
<OpenGenerativeUIActivityRenderer
|
|
525
|
+
activityType="open-generative-ui"
|
|
526
|
+
content={{
|
|
527
|
+
html: ["<body><div>Hello</div>"],
|
|
528
|
+
htmlComplete: false,
|
|
529
|
+
cssComplete: true,
|
|
530
|
+
generating: true,
|
|
531
|
+
}}
|
|
532
|
+
message={{}}
|
|
533
|
+
agent={{}}
|
|
534
|
+
/>,
|
|
535
|
+
);
|
|
536
|
+
await flushImport();
|
|
537
|
+
|
|
538
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
539
|
+
|
|
540
|
+
// Now complete the HTML
|
|
541
|
+
resetMockPromise();
|
|
542
|
+
rerender(
|
|
543
|
+
<OpenGenerativeUIActivityRenderer
|
|
544
|
+
activityType="open-generative-ui"
|
|
545
|
+
content={{
|
|
546
|
+
html: [
|
|
547
|
+
"<head><style>body{margin:0}</style></head><body><div>Hello</div></body>",
|
|
548
|
+
],
|
|
549
|
+
htmlComplete: true,
|
|
550
|
+
cssComplete: true,
|
|
551
|
+
generating: false,
|
|
552
|
+
}}
|
|
553
|
+
message={{}}
|
|
554
|
+
agent={{}}
|
|
555
|
+
/>,
|
|
556
|
+
);
|
|
557
|
+
await flushImport();
|
|
558
|
+
|
|
559
|
+
// Preview destroyed, final sandbox created
|
|
560
|
+
expect(mockDestroy).toHaveBeenCalledTimes(1);
|
|
561
|
+
expect(mockCreate).toHaveBeenCalledTimes(2);
|
|
562
|
+
|
|
563
|
+
const [, options] = mockCreate.mock.calls[1];
|
|
564
|
+
expect(options.frameContent).toContain("<style>");
|
|
565
|
+
expect(options.frameContent).toContain("Hello");
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
it("does not show preview until cssComplete, then starts streaming HTML immediately", async () => {
|
|
569
|
+
// Phase 1: HTML chunks arrive but CSS is not yet complete — no preview
|
|
570
|
+
const { rerender } = render(
|
|
571
|
+
<OpenGenerativeUIActivityRenderer
|
|
572
|
+
activityType="open-generative-ui"
|
|
573
|
+
content={{
|
|
574
|
+
html: ["<body><div>Streaming content</div>"],
|
|
575
|
+
htmlComplete: false,
|
|
576
|
+
generating: true,
|
|
577
|
+
// cssComplete is NOT set — CSS still generating
|
|
578
|
+
}}
|
|
579
|
+
message={{}}
|
|
580
|
+
agent={{}}
|
|
581
|
+
/>,
|
|
582
|
+
);
|
|
583
|
+
await flushImport();
|
|
584
|
+
|
|
585
|
+
// No sandbox created — placeholder shown while CSS is pending
|
|
586
|
+
expect(mockCreate).not.toHaveBeenCalled();
|
|
587
|
+
|
|
588
|
+
// Phase 2: CSS completes — preview should appear immediately with the
|
|
589
|
+
// HTML chunks that have already accumulated
|
|
590
|
+
rerender(
|
|
591
|
+
<OpenGenerativeUIActivityRenderer
|
|
592
|
+
activityType="open-generative-ui"
|
|
593
|
+
content={{
|
|
594
|
+
css: "body { margin: 0; color: red; }",
|
|
595
|
+
cssComplete: true,
|
|
596
|
+
html: ["<body><div>Streaming content</div>"],
|
|
597
|
+
htmlComplete: false,
|
|
598
|
+
generating: true,
|
|
599
|
+
}}
|
|
600
|
+
message={{}}
|
|
601
|
+
agent={{}}
|
|
602
|
+
/>,
|
|
603
|
+
);
|
|
604
|
+
await flushImport();
|
|
605
|
+
|
|
606
|
+
// Preview sandbox created now that CSS is complete
|
|
607
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
608
|
+
const [, options] = mockCreate.mock.calls[0];
|
|
609
|
+
expect(options.frameContent).toBe("<head></head><body></body>");
|
|
610
|
+
|
|
611
|
+
// Resolve sandbox promise so preview content is applied
|
|
612
|
+
await act(async () => {
|
|
613
|
+
mockPromiseResolve();
|
|
614
|
+
await mockPromise;
|
|
615
|
+
});
|
|
616
|
+
await flushImport();
|
|
617
|
+
|
|
618
|
+
// CSS should be injected into <head> as a <style> tag
|
|
619
|
+
const headCalls = mockRun.mock.calls.filter(
|
|
620
|
+
(c: unknown[]) =>
|
|
621
|
+
typeof c[0] === "string" &&
|
|
622
|
+
(c[0] as string).includes("document.head.innerHTML"),
|
|
623
|
+
);
|
|
624
|
+
expect(headCalls.length).toBeGreaterThanOrEqual(1);
|
|
625
|
+
expect(headCalls[0][0]).toContain("body { margin: 0; color: red; }");
|
|
626
|
+
|
|
627
|
+
// HTML body content should be rendered in the preview
|
|
628
|
+
const bodyCalls = mockRun.mock.calls.filter(
|
|
629
|
+
(c: unknown[]) =>
|
|
630
|
+
typeof c[0] === "string" &&
|
|
631
|
+
(c[0] as string).includes("document.body.innerHTML"),
|
|
632
|
+
);
|
|
633
|
+
expect(bodyCalls.length).toBeGreaterThanOrEqual(1);
|
|
634
|
+
expect(bodyCalls[0][0]).toContain("Streaming content");
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
it("does not create preview when no meaningful body content", async () => {
|
|
638
|
+
renderRenderer({
|
|
639
|
+
html: ["<head><style>.foo { color: red; }</style></head>"],
|
|
640
|
+
htmlComplete: false,
|
|
641
|
+
generating: true,
|
|
642
|
+
});
|
|
643
|
+
await flushImport();
|
|
644
|
+
|
|
645
|
+
// No preview — only head content, no body
|
|
646
|
+
expect(mockCreate).not.toHaveBeenCalled();
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
it("skips preview entirely for fast completion", async () => {
|
|
650
|
+
renderRenderer({
|
|
651
|
+
html: ["<head></head><body><div>Done</div></body>"],
|
|
652
|
+
htmlComplete: true,
|
|
653
|
+
generating: false,
|
|
654
|
+
});
|
|
655
|
+
await flushImport();
|
|
656
|
+
|
|
657
|
+
// Only final sandbox created, no preview
|
|
658
|
+
expect(mockCreate).toHaveBeenCalledTimes(1);
|
|
659
|
+
const [, options] = mockCreate.mock.calls[0];
|
|
660
|
+
expect(options.frameContent).toContain("Done");
|
|
661
|
+
// Final sandbox uses localApi, not empty object
|
|
662
|
+
expect(options.frameContent).not.toBe("<head></head><body></body>");
|
|
663
|
+
});
|
|
664
|
+
});
|
|
665
|
+
});
|