@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,598 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React, {
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useMemo,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
} from "react";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { ToolCallStatus } from "@copilotkit/core";
|
|
12
|
+
import { useSandboxFunctions } from "../providers/SandboxFunctionsContext";
|
|
13
|
+
import {
|
|
14
|
+
processPartialHtml,
|
|
15
|
+
extractCompleteStyles,
|
|
16
|
+
} from "../lib/processPartialHtml";
|
|
17
|
+
|
|
18
|
+
export const OpenGenerativeUIActivityType = "open-generative-ui";
|
|
19
|
+
|
|
20
|
+
export const OpenGenerativeUIContentSchema = z.object({
|
|
21
|
+
initialHeight: z.number().optional(),
|
|
22
|
+
generating: z.boolean().optional(),
|
|
23
|
+
css: z.string().optional(),
|
|
24
|
+
cssComplete: z.boolean().optional(),
|
|
25
|
+
html: z.array(z.string()).optional(),
|
|
26
|
+
htmlComplete: z.boolean().optional(),
|
|
27
|
+
jsFunctions: z.string().optional(),
|
|
28
|
+
jsFunctionsComplete: z.boolean().optional(),
|
|
29
|
+
jsExpressions: z.array(z.string()).optional(),
|
|
30
|
+
jsExpressionsComplete: z.boolean().optional(),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export type OpenGenerativeUIContent = z.infer<
|
|
34
|
+
typeof OpenGenerativeUIContentSchema
|
|
35
|
+
>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Schema for the generateSandboxedUi tool call arguments.
|
|
39
|
+
* Used by the frontend tool renderer to display placeholder messages.
|
|
40
|
+
*/
|
|
41
|
+
export const GenerateSandboxedUiArgsSchema = z.object({
|
|
42
|
+
initialHeight: z.number().optional(),
|
|
43
|
+
placeholderMessages: z.array(z.string()).optional(),
|
|
44
|
+
css: z.string().optional(),
|
|
45
|
+
html: z.string().optional(),
|
|
46
|
+
jsFunctions: z.string().optional(),
|
|
47
|
+
jsExpressions: z.array(z.string()).optional(),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
export type GenerateSandboxedUiArgs = z.infer<
|
|
51
|
+
typeof GenerateSandboxedUiArgsSchema
|
|
52
|
+
>;
|
|
53
|
+
|
|
54
|
+
interface OpenGenerativeUIActivityRendererProps {
|
|
55
|
+
activityType: string;
|
|
56
|
+
content: OpenGenerativeUIContent;
|
|
57
|
+
message: unknown;
|
|
58
|
+
agent: unknown;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const THROTTLE_MS = 1000;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns true when the inner component should re-render immediately
|
|
65
|
+
* (no throttle delay).
|
|
66
|
+
*/
|
|
67
|
+
function shouldFlushImmediately(
|
|
68
|
+
prev: OpenGenerativeUIContent | null,
|
|
69
|
+
next: OpenGenerativeUIContent,
|
|
70
|
+
): boolean {
|
|
71
|
+
// CSS finished — switch from placeholder to preview
|
|
72
|
+
if (next.cssComplete && (!prev || !prev.cssComplete)) return true;
|
|
73
|
+
// Streaming done
|
|
74
|
+
if (next.htmlComplete) return true;
|
|
75
|
+
// Generation finished
|
|
76
|
+
if (next.generating === false) return true;
|
|
77
|
+
// jsFunctions appeared
|
|
78
|
+
if (next.jsFunctions && (!prev || !prev.jsFunctions)) return true;
|
|
79
|
+
// jsExpressions grew
|
|
80
|
+
if ((next.jsExpressions?.length ?? 0) > (prev?.jsExpressions?.length ?? 0))
|
|
81
|
+
return true;
|
|
82
|
+
// First html chunk arrived (first preview — no delay)
|
|
83
|
+
if (next.html?.length && (!prev || !prev.html?.length)) return true;
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Outer wrapper — absorbs every parent re-render but only forwards
|
|
89
|
+
* throttled content snapshots to the memoized inner component.
|
|
90
|
+
*/
|
|
91
|
+
export const OpenGenerativeUIActivityRenderer: React.FC<OpenGenerativeUIActivityRendererProps> =
|
|
92
|
+
function OpenGenerativeUIActivityRenderer({ content }) {
|
|
93
|
+
const latestContentRef = useRef(content);
|
|
94
|
+
latestContentRef.current = content;
|
|
95
|
+
|
|
96
|
+
const [throttledContent, setThrottledContent] =
|
|
97
|
+
useState<OpenGenerativeUIContent>(content);
|
|
98
|
+
const throttledContentRef = useRef(throttledContent);
|
|
99
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
100
|
+
|
|
101
|
+
// Synchronous state adjustment during render (React-approved pattern).
|
|
102
|
+
// When shouldFlushImmediately is true, update state before commit so the
|
|
103
|
+
// inner component sees the new content in the same render pass — no extra
|
|
104
|
+
// async cycle that would break test timing.
|
|
105
|
+
if (throttledContentRef.current !== content) {
|
|
106
|
+
if (shouldFlushImmediately(throttledContentRef.current, content)) {
|
|
107
|
+
if (timerRef.current !== null) {
|
|
108
|
+
clearTimeout(timerRef.current);
|
|
109
|
+
timerRef.current = null;
|
|
110
|
+
}
|
|
111
|
+
throttledContentRef.current = content;
|
|
112
|
+
setThrottledContent(content);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const flush = useCallback(() => {
|
|
117
|
+
timerRef.current = null;
|
|
118
|
+
const latest = latestContentRef.current;
|
|
119
|
+
throttledContentRef.current = latest;
|
|
120
|
+
setThrottledContent(latest);
|
|
121
|
+
}, []);
|
|
122
|
+
|
|
123
|
+
// Schedule throttled updates for non-immediate content changes
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
// Already up to date (initial render or synchronous flush above)
|
|
126
|
+
if (throttledContentRef.current === content) return;
|
|
127
|
+
|
|
128
|
+
// Schedule a throttled flush if none pending
|
|
129
|
+
if (timerRef.current === null) {
|
|
130
|
+
timerRef.current = setTimeout(flush, THROTTLE_MS);
|
|
131
|
+
}
|
|
132
|
+
}, [content, flush]);
|
|
133
|
+
|
|
134
|
+
// Cleanup timer on unmount
|
|
135
|
+
useEffect(() => {
|
|
136
|
+
return () => {
|
|
137
|
+
if (timerRef.current !== null) {
|
|
138
|
+
clearTimeout(timerRef.current);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}, []);
|
|
142
|
+
|
|
143
|
+
return <OpenGenerativeUIActivityRendererInner content={throttledContent} />;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// ---------------------------------------------------------------------------
|
|
147
|
+
// Inner component — all the expensive work, protected by React.memo
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
|
|
150
|
+
interface InnerProps {
|
|
151
|
+
content: OpenGenerativeUIContent;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function ensureHead(html: string): string {
|
|
155
|
+
if (/<head[\s>]/i.test(html)) return html;
|
|
156
|
+
return `<head></head>${html}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function injectCssIntoHtml(html: string, css: string): string {
|
|
160
|
+
const headCloseIdx = html.indexOf("</head>");
|
|
161
|
+
if (headCloseIdx !== -1) {
|
|
162
|
+
return (
|
|
163
|
+
html.slice(0, headCloseIdx) +
|
|
164
|
+
`<style>${css}</style>` +
|
|
165
|
+
html.slice(headCloseIdx)
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return `<head><style>${css}</style></head>${html}`;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const OpenGenerativeUIActivityRendererInner = React.memo(
|
|
172
|
+
function OpenGenerativeUIActivityRendererInner({ content }: InnerProps) {
|
|
173
|
+
const initialHeight = content.initialHeight ?? 200;
|
|
174
|
+
const [autoHeight, setAutoHeight] = useState<number | null>(null);
|
|
175
|
+
const sandboxFunctions = useSandboxFunctions();
|
|
176
|
+
|
|
177
|
+
const localApi = useMemo(() => {
|
|
178
|
+
const api: Record<string, Function> = {};
|
|
179
|
+
for (const fn of sandboxFunctions) {
|
|
180
|
+
api[fn.name] = fn.handler;
|
|
181
|
+
}
|
|
182
|
+
return api;
|
|
183
|
+
}, [sandboxFunctions]);
|
|
184
|
+
|
|
185
|
+
// Join html chunks only when streaming is complete
|
|
186
|
+
const fullHtml =
|
|
187
|
+
content.htmlComplete && content.html?.length
|
|
188
|
+
? content.html.join("")
|
|
189
|
+
: undefined;
|
|
190
|
+
|
|
191
|
+
// CSS from the dedicated parameter (available once cssComplete)
|
|
192
|
+
const css = content.cssComplete ? content.css : undefined;
|
|
193
|
+
|
|
194
|
+
// Derived state for preview streaming — gate on cssComplete so we
|
|
195
|
+
// show the placeholder until styles are ready.
|
|
196
|
+
const cssReady = !!content.cssComplete;
|
|
197
|
+
const partialHtml =
|
|
198
|
+
!content.htmlComplete && content.html?.length
|
|
199
|
+
? content.html.join("")
|
|
200
|
+
: undefined;
|
|
201
|
+
const previewBody = partialHtml
|
|
202
|
+
? processPartialHtml(partialHtml)
|
|
203
|
+
: undefined;
|
|
204
|
+
const previewStyles = partialHtml ? extractCompleteStyles(partialHtml) : "";
|
|
205
|
+
const hasPreview = cssReady && !!previewBody?.trim();
|
|
206
|
+
const hasVisibleSandbox = !!fullHtml || hasPreview;
|
|
207
|
+
|
|
208
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
209
|
+
const sandboxRef = useRef<{
|
|
210
|
+
run: (code: string | Function) => Promise<unknown>;
|
|
211
|
+
destroy: () => void;
|
|
212
|
+
iframe: HTMLIFrameElement;
|
|
213
|
+
} | null>(null);
|
|
214
|
+
const previewSandboxRef = useRef<{
|
|
215
|
+
run: (code: string | Function) => Promise<unknown>;
|
|
216
|
+
destroy: () => void;
|
|
217
|
+
iframe: HTMLIFrameElement;
|
|
218
|
+
} | null>(null);
|
|
219
|
+
const previewReadyRef = useRef(false);
|
|
220
|
+
const sandboxReadyRef = useRef(false);
|
|
221
|
+
const executedIndexRef = useRef(0);
|
|
222
|
+
const pendingQueueRef = useRef<string[]>([]);
|
|
223
|
+
const jsFunctionsInjectedRef = useRef(false);
|
|
224
|
+
|
|
225
|
+
// Effect 0 — Preview sandbox creation
|
|
226
|
+
useEffect(() => {
|
|
227
|
+
const container = containerRef.current;
|
|
228
|
+
if (!container || fullHtml || !hasPreview || previewSandboxRef.current)
|
|
229
|
+
return;
|
|
230
|
+
|
|
231
|
+
let cancelled = false;
|
|
232
|
+
|
|
233
|
+
import("@jetbrains/websandbox")
|
|
234
|
+
.then((mod: any) => {
|
|
235
|
+
if (cancelled) return;
|
|
236
|
+
|
|
237
|
+
const Websandbox = mod.default?.default ?? mod.default;
|
|
238
|
+
const sandbox = Websandbox.create(
|
|
239
|
+
{},
|
|
240
|
+
{
|
|
241
|
+
frameContainer: container,
|
|
242
|
+
frameContent: "<head></head><body></body>",
|
|
243
|
+
allowAdditionalAttributes: "",
|
|
244
|
+
},
|
|
245
|
+
);
|
|
246
|
+
previewSandboxRef.current = sandbox;
|
|
247
|
+
|
|
248
|
+
sandbox.iframe.style.width = "100%";
|
|
249
|
+
sandbox.iframe.style.height = "100%";
|
|
250
|
+
sandbox.iframe.style.border = "none";
|
|
251
|
+
sandbox.iframe.style.backgroundColor = "transparent";
|
|
252
|
+
|
|
253
|
+
sandbox.promise.then(() => {
|
|
254
|
+
if (cancelled) return;
|
|
255
|
+
previewReadyRef.current = true;
|
|
256
|
+
|
|
257
|
+
// Prevent scrollbars inside preview iframe
|
|
258
|
+
sandbox.run(`
|
|
259
|
+
var s = document.createElement('style');
|
|
260
|
+
s.textContent = 'html, body { overflow: hidden !important; }';
|
|
261
|
+
document.head.appendChild(s);
|
|
262
|
+
`);
|
|
263
|
+
|
|
264
|
+
// Inject CSS from the dedicated parameter + any inline styles from HTML
|
|
265
|
+
const headParts: string[] = [];
|
|
266
|
+
if (css) headParts.push(`<style>${css}</style>`);
|
|
267
|
+
if (previewStyles) headParts.push(previewStyles);
|
|
268
|
+
if (headParts.length) {
|
|
269
|
+
sandbox.run(
|
|
270
|
+
`document.head.innerHTML = ${JSON.stringify(headParts.join(""))}`,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
if (previewBody) {
|
|
274
|
+
sandbox.run(
|
|
275
|
+
`document.body.innerHTML = ${JSON.stringify(previewBody)}`,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
})
|
|
280
|
+
.catch((err: unknown) => {
|
|
281
|
+
console.error(
|
|
282
|
+
"[OpenGenerativeUI] Failed to load sandbox module:",
|
|
283
|
+
err,
|
|
284
|
+
);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
return () => {
|
|
288
|
+
cancelled = true;
|
|
289
|
+
};
|
|
290
|
+
}, [hasPreview, fullHtml]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
291
|
+
|
|
292
|
+
// Effect 0b — Preview content updates (body + styles)
|
|
293
|
+
useEffect(() => {
|
|
294
|
+
if (!previewSandboxRef.current || !previewReadyRef.current) return;
|
|
295
|
+
const headParts: string[] = [];
|
|
296
|
+
if (css) headParts.push(`<style>${css}</style>`);
|
|
297
|
+
if (previewStyles) headParts.push(previewStyles);
|
|
298
|
+
if (headParts.length) {
|
|
299
|
+
previewSandboxRef.current.run(
|
|
300
|
+
`document.head.innerHTML = ${JSON.stringify(headParts.join(""))}`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
if (!previewBody) return;
|
|
304
|
+
previewSandboxRef.current.run(
|
|
305
|
+
`document.body.innerHTML = ${JSON.stringify(previewBody)}`,
|
|
306
|
+
);
|
|
307
|
+
}, [previewBody, previewStyles, css]);
|
|
308
|
+
|
|
309
|
+
// Effect 1 — Final sandbox lifecycle (depends on fullHtml)
|
|
310
|
+
useEffect(() => {
|
|
311
|
+
const container = containerRef.current;
|
|
312
|
+
if (!container || !fullHtml) return;
|
|
313
|
+
|
|
314
|
+
// Destroy preview sandbox when transitioning to final
|
|
315
|
+
if (previewSandboxRef.current) {
|
|
316
|
+
previewSandboxRef.current.destroy();
|
|
317
|
+
previewSandboxRef.current = null;
|
|
318
|
+
previewReadyRef.current = false;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
let cancelled = false;
|
|
322
|
+
|
|
323
|
+
// Reset state for new html
|
|
324
|
+
executedIndexRef.current = 0;
|
|
325
|
+
jsFunctionsInjectedRef.current = false;
|
|
326
|
+
sandboxReadyRef.current = false;
|
|
327
|
+
pendingQueueRef.current = [];
|
|
328
|
+
|
|
329
|
+
// Dynamic import to avoid SSR issues (websandbox references `self` at module level)
|
|
330
|
+
const htmlContent = css ? injectCssIntoHtml(fullHtml, css) : fullHtml;
|
|
331
|
+
import("@jetbrains/websandbox")
|
|
332
|
+
.then((mod: any) => {
|
|
333
|
+
if (cancelled) return;
|
|
334
|
+
|
|
335
|
+
// websandbox ships a UMD bundle with its own webpack `default` export.
|
|
336
|
+
// Consumer bundlers (e.g. Next.js webpack) wrap CJS under another `.default`,
|
|
337
|
+
// resulting in mod.default.default for the actual Websandbox class.
|
|
338
|
+
const Websandbox = mod.default?.default ?? mod.default;
|
|
339
|
+
const sandbox = Websandbox.create(localApi, {
|
|
340
|
+
frameContainer: container,
|
|
341
|
+
frameContent: ensureHead(htmlContent),
|
|
342
|
+
allowAdditionalAttributes: "",
|
|
343
|
+
});
|
|
344
|
+
sandboxRef.current = sandbox;
|
|
345
|
+
|
|
346
|
+
// Style the iframe to fill container
|
|
347
|
+
sandbox.iframe.style.width = "100%";
|
|
348
|
+
sandbox.iframe.style.height = "100%";
|
|
349
|
+
sandbox.iframe.style.border = "none";
|
|
350
|
+
sandbox.iframe.style.backgroundColor = "transparent";
|
|
351
|
+
|
|
352
|
+
sandbox.promise.then(() => {
|
|
353
|
+
if (cancelled) return;
|
|
354
|
+
sandboxReadyRef.current = true;
|
|
355
|
+
|
|
356
|
+
// Prevent scrollbars — the container auto-sizes to fit content
|
|
357
|
+
sandbox.run(`
|
|
358
|
+
var s = document.createElement('style');
|
|
359
|
+
s.textContent = 'html, body { overflow: hidden !important; }';
|
|
360
|
+
document.head.appendChild(s);
|
|
361
|
+
`);
|
|
362
|
+
|
|
363
|
+
// Flush pending queue
|
|
364
|
+
const queue = pendingQueueRef.current;
|
|
365
|
+
pendingQueueRef.current = [];
|
|
366
|
+
for (const code of queue) {
|
|
367
|
+
sandbox.run(code);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
})
|
|
371
|
+
.catch((err: unknown) => {
|
|
372
|
+
console.error(
|
|
373
|
+
"[OpenGenerativeUI] Failed to load sandbox module:",
|
|
374
|
+
err,
|
|
375
|
+
);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
return () => {
|
|
379
|
+
cancelled = true;
|
|
380
|
+
// Destroy preview sandbox if it still exists
|
|
381
|
+
if (previewSandboxRef.current) {
|
|
382
|
+
previewSandboxRef.current.destroy();
|
|
383
|
+
previewSandboxRef.current = null;
|
|
384
|
+
previewReadyRef.current = false;
|
|
385
|
+
}
|
|
386
|
+
if (sandboxRef.current) {
|
|
387
|
+
sandboxRef.current.destroy();
|
|
388
|
+
sandboxRef.current = null;
|
|
389
|
+
}
|
|
390
|
+
sandboxReadyRef.current = false;
|
|
391
|
+
setAutoHeight(null);
|
|
392
|
+
};
|
|
393
|
+
}, [fullHtml, css, localApi]);
|
|
394
|
+
|
|
395
|
+
// Effect 2 — jsFunctions injection (depends on content.jsFunctions)
|
|
396
|
+
useEffect(() => {
|
|
397
|
+
if (!content.jsFunctions || jsFunctionsInjectedRef.current) return;
|
|
398
|
+
jsFunctionsInjectedRef.current = true;
|
|
399
|
+
|
|
400
|
+
const sandbox = sandboxRef.current;
|
|
401
|
+
if (sandboxReadyRef.current && sandbox) {
|
|
402
|
+
sandbox.run(content.jsFunctions);
|
|
403
|
+
} else {
|
|
404
|
+
pendingQueueRef.current.push(content.jsFunctions);
|
|
405
|
+
}
|
|
406
|
+
}, [content.jsFunctions]);
|
|
407
|
+
|
|
408
|
+
// Effect 3 — jsExpressions execution (depends on content.jsExpressions?.length)
|
|
409
|
+
useEffect(() => {
|
|
410
|
+
const expressions = content.jsExpressions;
|
|
411
|
+
if (!expressions || expressions.length === 0) return;
|
|
412
|
+
|
|
413
|
+
const startIndex = executedIndexRef.current;
|
|
414
|
+
if (startIndex >= expressions.length) return;
|
|
415
|
+
|
|
416
|
+
const newExprs = expressions.slice(startIndex);
|
|
417
|
+
executedIndexRef.current = expressions.length;
|
|
418
|
+
|
|
419
|
+
const sandbox = sandboxRef.current;
|
|
420
|
+
if (sandboxReadyRef.current && sandbox) {
|
|
421
|
+
(async () => {
|
|
422
|
+
for (const expr of newExprs) {
|
|
423
|
+
await sandbox.run(expr);
|
|
424
|
+
}
|
|
425
|
+
})();
|
|
426
|
+
} else {
|
|
427
|
+
pendingQueueRef.current.push(...newExprs);
|
|
428
|
+
}
|
|
429
|
+
}, [content.jsExpressions?.length]);
|
|
430
|
+
|
|
431
|
+
// Effect 4 — One-shot height measurement (fires once when generation completes)
|
|
432
|
+
// Uses body.scrollHeight (not documentElement.scrollHeight) because the latter
|
|
433
|
+
// is clamped to the iframe viewport and can never shrink below the current size.
|
|
434
|
+
const generationDone = content.generating === false;
|
|
435
|
+
useEffect(() => {
|
|
436
|
+
const sandbox = sandboxRef.current;
|
|
437
|
+
if (!generationDone || !sandbox) return;
|
|
438
|
+
|
|
439
|
+
let handled = false;
|
|
440
|
+
const onMessage = (e: MessageEvent) => {
|
|
441
|
+
if (handled) return;
|
|
442
|
+
if (
|
|
443
|
+
e.source === sandbox.iframe.contentWindow &&
|
|
444
|
+
e.data?.type === "__ck_resize"
|
|
445
|
+
) {
|
|
446
|
+
handled = true;
|
|
447
|
+
setAutoHeight(e.data.height);
|
|
448
|
+
window.removeEventListener("message", onMessage);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
window.addEventListener("message", onMessage);
|
|
452
|
+
|
|
453
|
+
const measureOnce = `
|
|
454
|
+
(function() {
|
|
455
|
+
var s = document.createElement('style');
|
|
456
|
+
s.textContent = 'body { height: auto !important; min-height: 0 !important; }';
|
|
457
|
+
document.head.appendChild(s);
|
|
458
|
+
var h = document.body.scrollHeight;
|
|
459
|
+
var cs = getComputedStyle(document.body);
|
|
460
|
+
h += parseFloat(cs.marginTop) || 0;
|
|
461
|
+
h += parseFloat(cs.marginBottom) || 0;
|
|
462
|
+
s.remove();
|
|
463
|
+
parent.postMessage({ type: "__ck_resize", height: Math.ceil(h) }, "*");
|
|
464
|
+
})();
|
|
465
|
+
`;
|
|
466
|
+
|
|
467
|
+
if (sandboxReadyRef.current) {
|
|
468
|
+
sandbox.run(measureOnce);
|
|
469
|
+
} else {
|
|
470
|
+
pendingQueueRef.current.push(measureOnce);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return () => {
|
|
474
|
+
window.removeEventListener("message", onMessage);
|
|
475
|
+
};
|
|
476
|
+
}, [generationDone]);
|
|
477
|
+
|
|
478
|
+
const height = autoHeight ?? initialHeight;
|
|
479
|
+
|
|
480
|
+
const isGenerating = content.generating !== false;
|
|
481
|
+
|
|
482
|
+
return (
|
|
483
|
+
<div
|
|
484
|
+
ref={containerRef}
|
|
485
|
+
style={{
|
|
486
|
+
position: "relative",
|
|
487
|
+
width: "100%",
|
|
488
|
+
height: `${height}px`,
|
|
489
|
+
borderRadius: "8px",
|
|
490
|
+
backgroundColor: hasVisibleSandbox ? "transparent" : "#f5f5f5",
|
|
491
|
+
border: hasVisibleSandbox ? "none" : "1px solid #e0e0e0",
|
|
492
|
+
display: hasVisibleSandbox ? "block" : "flex",
|
|
493
|
+
alignItems: hasVisibleSandbox ? undefined : "center",
|
|
494
|
+
justifyContent: hasVisibleSandbox ? undefined : "center",
|
|
495
|
+
overflow: "hidden",
|
|
496
|
+
}}
|
|
497
|
+
>
|
|
498
|
+
{isGenerating && (
|
|
499
|
+
<div
|
|
500
|
+
style={{
|
|
501
|
+
position: "absolute",
|
|
502
|
+
inset: 0,
|
|
503
|
+
zIndex: 10,
|
|
504
|
+
pointerEvents: "all",
|
|
505
|
+
backgroundColor: "rgba(255, 255, 255, 0.5)",
|
|
506
|
+
display: "flex",
|
|
507
|
+
alignItems: "center",
|
|
508
|
+
justifyContent: "center",
|
|
509
|
+
}}
|
|
510
|
+
>
|
|
511
|
+
<svg
|
|
512
|
+
width="48"
|
|
513
|
+
height="48"
|
|
514
|
+
viewBox="0 0 24 24"
|
|
515
|
+
fill="none"
|
|
516
|
+
style={{ animation: "ck-spin 1s linear infinite" }}
|
|
517
|
+
>
|
|
518
|
+
<circle cx="12" cy="12" r="10" stroke="#e0e0e0" strokeWidth="3" />
|
|
519
|
+
<path
|
|
520
|
+
d="M12 2a10 10 0 0 1 10 10"
|
|
521
|
+
stroke="#999"
|
|
522
|
+
strokeWidth="3"
|
|
523
|
+
strokeLinecap="round"
|
|
524
|
+
/>
|
|
525
|
+
</svg>
|
|
526
|
+
<style>{`@keyframes ck-spin { to { transform: rotate(360deg) } }`}</style>
|
|
527
|
+
</div>
|
|
528
|
+
)}
|
|
529
|
+
</div>
|
|
530
|
+
);
|
|
531
|
+
},
|
|
532
|
+
(prev, next) => prev.content === next.content,
|
|
533
|
+
);
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Frontend tool renderer for generateSandboxedUi.
|
|
537
|
+
* Displays placeholder messages while the UI is being generated.
|
|
538
|
+
*/
|
|
539
|
+
export const OpenGenerativeUIToolRenderer: React.FC<
|
|
540
|
+
| {
|
|
541
|
+
name: string;
|
|
542
|
+
args: Partial<GenerateSandboxedUiArgs>;
|
|
543
|
+
status: ToolCallStatus.InProgress;
|
|
544
|
+
result: undefined;
|
|
545
|
+
}
|
|
546
|
+
| {
|
|
547
|
+
name: string;
|
|
548
|
+
args: GenerateSandboxedUiArgs;
|
|
549
|
+
status: ToolCallStatus.Executing;
|
|
550
|
+
result: undefined;
|
|
551
|
+
}
|
|
552
|
+
| {
|
|
553
|
+
name: string;
|
|
554
|
+
args: GenerateSandboxedUiArgs;
|
|
555
|
+
status: ToolCallStatus.Complete;
|
|
556
|
+
result: string;
|
|
557
|
+
}
|
|
558
|
+
> = function OpenGenerativeUIToolRenderer(props) {
|
|
559
|
+
const [visibleMessageIndex, setVisibleMessageIndex] = useState(0);
|
|
560
|
+
const prevMessageCountRef = useRef(0);
|
|
561
|
+
|
|
562
|
+
const messages = props.args.placeholderMessages;
|
|
563
|
+
|
|
564
|
+
// Cycle through placeholder messages
|
|
565
|
+
useEffect(() => {
|
|
566
|
+
if (!messages || messages.length === 0) return;
|
|
567
|
+
|
|
568
|
+
// When a new message streams in, jump to it immediately
|
|
569
|
+
if (messages.length !== prevMessageCountRef.current) {
|
|
570
|
+
prevMessageCountRef.current = messages.length;
|
|
571
|
+
setVisibleMessageIndex(messages.length - 1);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// Auto-cycle every 3s while still in progress
|
|
575
|
+
if (props.status === ToolCallStatus.Complete) return;
|
|
576
|
+
const timer = setInterval(() => {
|
|
577
|
+
setVisibleMessageIndex((i) => (i + 1) % messages.length);
|
|
578
|
+
}, 5000);
|
|
579
|
+
return () => clearInterval(timer);
|
|
580
|
+
}, [messages?.length, props.status]);
|
|
581
|
+
|
|
582
|
+
// Don't render anything once complete — the activity renderer handles the UI
|
|
583
|
+
if (props.status === ToolCallStatus.Complete) return null;
|
|
584
|
+
|
|
585
|
+
if (!messages || messages.length === 0) return null;
|
|
586
|
+
|
|
587
|
+
return (
|
|
588
|
+
<div
|
|
589
|
+
style={{
|
|
590
|
+
padding: "8px 12px",
|
|
591
|
+
color: "#999",
|
|
592
|
+
fontSize: "14px",
|
|
593
|
+
}}
|
|
594
|
+
>
|
|
595
|
+
{messages[visibleMessageIndex] ?? messages[0]}
|
|
596
|
+
</div>
|
|
597
|
+
);
|
|
598
|
+
};
|