@copilotkit/vue 2.0.0-rc.8 → 2.0.0-rc.9
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/dist/hooks/use-copilot-action.d.ts.map +1 -1
- package/dist/hooks/use-frontend-tool.d.ts +1 -1
- package/dist/hooks/use-frontend-tool.d.ts.map +1 -1
- package/dist/hooks/zod-parameters.d.ts +14 -0
- package/dist/hooks/zod-parameters.d.ts.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +392 -290
- package/dist/index.mjs.map +1 -1
- package/dist/use-render-activity-message-B59A4R48.js +18097 -0
- package/dist/use-render-activity-message-B59A4R48.js.map +1 -0
- package/dist/use-render-activity-message-u0OVXjd8.cjs +92 -0
- package/dist/use-render-activity-message-u0OVXjd8.cjs.map +1 -0
- package/dist/v2/components/OpenGenerativeUIRenderer.d.ts +13 -45
- package/dist/v2/components/OpenGenerativeUIRenderer.d.ts.map +1 -1
- package/dist/v2/components/a2ui/A2UICatalogContext.d.ts +1 -1
- package/dist/v2/components/a2ui/A2UICatalogContext.d.ts.map +1 -1
- package/dist/v2/components/a2ui/A2UIRecoveryStates.d.ts +13 -5
- package/dist/v2/components/a2ui/A2UIRecoveryStates.d.ts.map +1 -1
- package/dist/v2/components/a2ui.d.ts +3 -7
- package/dist/v2/components/a2ui.d.ts.map +1 -1
- package/dist/v2/index.cjs +1 -1
- package/dist/v2/index.mjs +173 -173
- package/dist/v2/lib/processPartialHtml.d.ts +1 -2
- package/dist/v2/lib/processPartialHtml.d.ts.map +1 -1
- package/dist/v2/providers/CopilotKitProvider.vue.d.ts +1 -1
- package/dist/v2/providers/CopilotKitProvider.vue.d.ts.map +1 -1
- package/package.json +10 -10
- package/src/hooks/__tests__/zod-parameters.test.ts +520 -0
- package/src/hooks/use-copilot-action.ts +2 -1
- package/src/hooks/use-frontend-tool.ts +3 -6
- package/src/hooks/zod-parameters.ts +162 -0
- package/src/v2/components/A2UIMessageRenderer.ts +3 -3
- package/src/v2/components/OpenGenerativeUIRenderer.ts +51 -373
- package/src/v2/components/__tests__/A2UICatalogContext.test.ts +1 -1
- package/src/v2/components/__tests__/OpenGenerativeUIRenderer.test.ts +55 -6
- package/src/v2/components/a2ui/A2UIBuiltInToolCallRenderer.ts +1 -1
- package/src/v2/components/a2ui/A2UICatalogContext.ts +10 -102
- package/src/v2/components/a2ui/A2UIRecoveryStates.ts +1 -1
- package/src/v2/components/a2ui.ts +1 -1
- package/src/v2/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.ts +26 -9
- package/src/v2/hooks/__tests__/zod-regression.test.ts +1 -1
- package/src/v2/lib/processPartialHtml.ts +6 -21
- package/src/v2/providers/CopilotKitProvider.vue +26 -34
- package/src/v2/providers/__tests__/CopilotKitProvider.a2ui.test.ts +1 -1
- package/vite.config.ts +2 -2
- package/dist/use-render-activity-message-C5LCG9a9.js +0 -16451
- package/dist/use-render-activity-message-C5LCG9a9.js.map +0 -1
- package/dist/use-render-activity-message-CPzguixR.cjs +0 -74
- package/dist/use-render-activity-message-CPzguixR.cjs.map +0 -1
- package/src/v2/lib/__tests__/processPartialHtml.test.ts +0 -84
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { actionParametersToJsonSchema } from "@copilotkit/shared";
|
|
3
|
+
import type { Parameter } from "@copilotkit/shared";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Convert a JSON Schema to a Zod schema. Resolves `$ref` against `$defs` /
|
|
7
|
+
* `definitions`, maps `anyOf` / `oneOf` to unions, and falls back to
|
|
8
|
+
* `z.any()` for circular refs and unsupported types.
|
|
9
|
+
*
|
|
10
|
+
* Moved here from `@copilotkit/shared` in 2.0 so that shared does not depend
|
|
11
|
+
* on zod.
|
|
12
|
+
*/
|
|
13
|
+
export function convertJsonSchemaToZodSchema(
|
|
14
|
+
jsonSchema: any,
|
|
15
|
+
required: boolean,
|
|
16
|
+
definitions?: Record<string, any>,
|
|
17
|
+
visitedRefs?: Set<string>,
|
|
18
|
+
): z.ZodSchema {
|
|
19
|
+
// Resolve $ref references
|
|
20
|
+
if (jsonSchema.$ref && definitions) {
|
|
21
|
+
const refPath = jsonSchema.$ref.replace(
|
|
22
|
+
/^#\/\$defs\/|^#\/definitions\//,
|
|
23
|
+
"",
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
// Detect circular $ref cycles
|
|
27
|
+
const refs = visitedRefs ?? new Set<string>();
|
|
28
|
+
if (refs.has(refPath)) {
|
|
29
|
+
console.warn(
|
|
30
|
+
`[CopilotKit] Circular $ref detected for "${refPath}" — falling back to z.any()`,
|
|
31
|
+
);
|
|
32
|
+
let schema = z.any();
|
|
33
|
+
if (jsonSchema.description) {
|
|
34
|
+
schema = schema.describe(jsonSchema.description);
|
|
35
|
+
}
|
|
36
|
+
return required ? schema : schema.optional();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const resolved = definitions[refPath];
|
|
40
|
+
if (resolved) {
|
|
41
|
+
// Clone the set so sibling branches don't see each other's visited refs
|
|
42
|
+
const nextRefs = new Set(refs);
|
|
43
|
+
nextRefs.add(refPath);
|
|
44
|
+
return convertJsonSchemaToZodSchema(
|
|
45
|
+
resolved,
|
|
46
|
+
required,
|
|
47
|
+
definitions,
|
|
48
|
+
nextRefs,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Collect top-level definitions for $ref resolution
|
|
54
|
+
const defs = definitions ?? jsonSchema.$defs ?? jsonSchema.definitions;
|
|
55
|
+
|
|
56
|
+
// Handle null-union types like ["string", "null"]
|
|
57
|
+
if (Array.isArray(jsonSchema.type)) {
|
|
58
|
+
const types = jsonSchema.type as string[];
|
|
59
|
+
const hasNull = types.includes("null");
|
|
60
|
+
const nonNullTypes = types.filter((t: string) => t !== "null");
|
|
61
|
+
const resolvedType = nonNullTypes.length > 0 ? nonNullTypes[0] : "string";
|
|
62
|
+
const innerSchema = convertJsonSchemaToZodSchema(
|
|
63
|
+
{ ...jsonSchema, type: resolvedType },
|
|
64
|
+
true,
|
|
65
|
+
defs,
|
|
66
|
+
visitedRefs,
|
|
67
|
+
);
|
|
68
|
+
let schema = hasNull ? z.union([innerSchema, z.null()]) : innerSchema;
|
|
69
|
+
if (jsonSchema.description) {
|
|
70
|
+
schema = schema.describe(jsonSchema.description);
|
|
71
|
+
}
|
|
72
|
+
return required ? schema : schema.optional();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Handle anyOf / oneOf as z.union
|
|
76
|
+
const unionVariants = jsonSchema.anyOf ?? jsonSchema.oneOf;
|
|
77
|
+
if (Array.isArray(unionVariants) && unionVariants.length > 0) {
|
|
78
|
+
if (unionVariants.length === 1) {
|
|
79
|
+
return convertJsonSchemaToZodSchema(
|
|
80
|
+
unionVariants[0],
|
|
81
|
+
required,
|
|
82
|
+
defs,
|
|
83
|
+
visitedRefs,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
const schemas = unionVariants.map((v: any) =>
|
|
87
|
+
convertJsonSchemaToZodSchema(v, true, defs, visitedRefs),
|
|
88
|
+
);
|
|
89
|
+
let schema = z.union(
|
|
90
|
+
schemas as [z.ZodSchema, z.ZodSchema, ...z.ZodSchema[]],
|
|
91
|
+
);
|
|
92
|
+
if (jsonSchema.description) {
|
|
93
|
+
schema = schema.describe(jsonSchema.description);
|
|
94
|
+
}
|
|
95
|
+
return required ? schema : schema.optional();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (jsonSchema.type === "object") {
|
|
99
|
+
const spec: { [key: string]: z.ZodSchema } = {};
|
|
100
|
+
|
|
101
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
102
|
+
return !required ? z.object(spec).optional() : z.object(spec);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
106
|
+
spec[key] = convertJsonSchemaToZodSchema(
|
|
107
|
+
value,
|
|
108
|
+
jsonSchema.required ? jsonSchema.required.includes(key) : false,
|
|
109
|
+
defs,
|
|
110
|
+
visitedRefs,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
let schema = z.object(spec).describe(jsonSchema.description);
|
|
114
|
+
return required ? schema : schema.optional();
|
|
115
|
+
} else if (jsonSchema.type === "string") {
|
|
116
|
+
if (jsonSchema.enum && jsonSchema.enum.length > 0) {
|
|
117
|
+
let schema = z
|
|
118
|
+
.enum(jsonSchema.enum as [string, ...string[]])
|
|
119
|
+
.describe(jsonSchema.description);
|
|
120
|
+
return required ? schema : schema.optional();
|
|
121
|
+
}
|
|
122
|
+
let schema = z.string().describe(jsonSchema.description);
|
|
123
|
+
return required ? schema : schema.optional();
|
|
124
|
+
} else if (jsonSchema.type === "number" || jsonSchema.type === "integer") {
|
|
125
|
+
let schema = z.number().describe(jsonSchema.description);
|
|
126
|
+
return required ? schema : schema.optional();
|
|
127
|
+
} else if (jsonSchema.type === "boolean") {
|
|
128
|
+
let schema = z.boolean().describe(jsonSchema.description);
|
|
129
|
+
return required ? schema : schema.optional();
|
|
130
|
+
} else if (jsonSchema.type === "array") {
|
|
131
|
+
let itemSchema = convertJsonSchemaToZodSchema(
|
|
132
|
+
jsonSchema.items,
|
|
133
|
+
true,
|
|
134
|
+
defs,
|
|
135
|
+
visitedRefs,
|
|
136
|
+
);
|
|
137
|
+
let schema = z.array(itemSchema).describe(jsonSchema.description);
|
|
138
|
+
return required ? schema : schema.optional();
|
|
139
|
+
} else if (jsonSchema.type === "null") {
|
|
140
|
+
let schema = z.null().describe(jsonSchema.description);
|
|
141
|
+
return required ? schema : schema.optional();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Fallback: accept any value rather than throwing
|
|
145
|
+
console.warn(
|
|
146
|
+
`[CopilotKit] Unsupported JSON schema type "${jsonSchema.type ?? "unknown"}" — falling back to z.any()`,
|
|
147
|
+
);
|
|
148
|
+
let schema = z.any();
|
|
149
|
+
if (jsonSchema.description) {
|
|
150
|
+
schema = schema.describe(jsonSchema.description);
|
|
151
|
+
}
|
|
152
|
+
return required ? schema : schema.optional();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Convert v1 `Parameter[]` action parameters to a Zod object schema. */
|
|
156
|
+
export function getZodParameters<T extends [] | Parameter[] | undefined>(
|
|
157
|
+
parameters: T,
|
|
158
|
+
): any {
|
|
159
|
+
if (!parameters) return z.object({});
|
|
160
|
+
const jsonParams = actionParametersToJsonSchema(parameters);
|
|
161
|
+
return convertJsonSchemaToZodSchema(jsonParams, true);
|
|
162
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { computed, defineComponent, h } from "vue";
|
|
2
2
|
import type { PropType } from "vue";
|
|
3
|
-
import
|
|
3
|
+
import * as z from "zod";
|
|
4
4
|
import type { VueActivityMessageRenderer } from "../types";
|
|
5
5
|
import type { A2UITheme } from "../types";
|
|
6
6
|
import A2UISurfaceActivityRenderer from "./A2UISurfaceActivityRenderer.vue";
|
|
@@ -31,7 +31,7 @@ export type A2UIMessageRendererOptions = {
|
|
|
31
31
|
* The `a2ui-surface` activity carries the WHOLE generative-UI lifecycle on one
|
|
32
32
|
* stable messageId: pre-paint `status` ("building" | "retrying" | "failed")
|
|
33
33
|
* with recovery detail, then `a2ui_operations` on paint. The states swap in
|
|
34
|
-
* place, so the painted surface replaces the skeleton. `.
|
|
34
|
+
* place, so the painted surface replaces the skeleton. `.loose()` keeps
|
|
35
35
|
* the operations and any future fields.
|
|
36
36
|
*/
|
|
37
37
|
const A2UISurfaceContentSchema = z
|
|
@@ -39,7 +39,7 @@ const A2UISurfaceContentSchema = z
|
|
|
39
39
|
a2ui_operations: z.array(z.any()).optional(),
|
|
40
40
|
...A2UILifecycleFields,
|
|
41
41
|
})
|
|
42
|
-
.
|
|
42
|
+
.loose();
|
|
43
43
|
|
|
44
44
|
export function createA2UIMessageRenderer(
|
|
45
45
|
options: A2UIMessageRendererOptions,
|
|
@@ -1,14 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
defineComponent,
|
|
3
|
+
h,
|
|
4
|
+
onBeforeUnmount,
|
|
5
|
+
onMounted,
|
|
6
|
+
ref,
|
|
7
|
+
shallowRef,
|
|
8
|
+
watch,
|
|
9
|
+
} from "vue";
|
|
2
10
|
import type { PropType } from "vue";
|
|
3
|
-
import
|
|
4
|
-
import { ToolCallStatus } from "@copilotkit/core";
|
|
11
|
+
import * as z from "zod";
|
|
5
12
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
OPEN_GENERATIVE_UI_ACTIVITY_TYPE,
|
|
14
|
+
ToolCallStatus,
|
|
15
|
+
ɵOpenGenerativeUIController,
|
|
16
|
+
} from "@copilotkit/core";
|
|
17
|
+
import type {
|
|
18
|
+
GenerateSandboxedUiArgs,
|
|
19
|
+
OpenGenerativeUIContent,
|
|
20
|
+
} from "@copilotkit/core";
|
|
9
21
|
import { useSandboxFunctions } from "../providers/SandboxFunctionsContext";
|
|
10
22
|
|
|
11
|
-
export
|
|
23
|
+
export type { GenerateSandboxedUiArgs, OpenGenerativeUIContent };
|
|
24
|
+
|
|
25
|
+
export const OpenGenerativeUIActivityType = OPEN_GENERATIVE_UI_ACTIVITY_TYPE;
|
|
12
26
|
|
|
13
27
|
export const OpenGenerativeUIContentSchema = z.object({
|
|
14
28
|
initialHeight: z.number().optional(),
|
|
@@ -21,11 +35,7 @@ export const OpenGenerativeUIContentSchema = z.object({
|
|
|
21
35
|
jsFunctionsComplete: z.boolean().optional(),
|
|
22
36
|
jsExpressions: z.array(z.string()).optional(),
|
|
23
37
|
jsExpressionsComplete: z.boolean().optional(),
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
export type OpenGenerativeUIContent = z.infer<
|
|
27
|
-
typeof OpenGenerativeUIContentSchema
|
|
28
|
-
>;
|
|
38
|
+
}) satisfies z.ZodType<OpenGenerativeUIContent>;
|
|
29
39
|
|
|
30
40
|
export const GenerateSandboxedUiArgsSchema = z.object({
|
|
31
41
|
initialHeight: z.number().optional(),
|
|
@@ -34,68 +44,12 @@ export const GenerateSandboxedUiArgsSchema = z.object({
|
|
|
34
44
|
html: z.string().optional(),
|
|
35
45
|
jsFunctions: z.string().optional(),
|
|
36
46
|
jsExpressions: z.array(z.string()).optional(),
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
export type GenerateSandboxedUiArgs = z.infer<
|
|
40
|
-
typeof GenerateSandboxedUiArgsSchema
|
|
41
|
-
>;
|
|
42
|
-
|
|
43
|
-
function shouldFlushImmediately(
|
|
44
|
-
previous: OpenGenerativeUIContent | null,
|
|
45
|
-
next: OpenGenerativeUIContent,
|
|
46
|
-
): boolean {
|
|
47
|
-
if (next.cssComplete && (!previous || !previous.cssComplete)) return true;
|
|
48
|
-
if (next.htmlComplete) return true;
|
|
49
|
-
if (next.generating === false) return true;
|
|
50
|
-
if (next.jsFunctions && (!previous || !previous.jsFunctions)) return true;
|
|
51
|
-
if (
|
|
52
|
-
(next.jsExpressions?.length ?? 0) > (previous?.jsExpressions?.length ?? 0)
|
|
53
|
-
)
|
|
54
|
-
return true;
|
|
55
|
-
if (next.html?.length && (!previous || !previous.html?.length)) return true;
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function ensureHead(html: string): string {
|
|
60
|
-
if (/<head[\s>]/i.test(html)) return html;
|
|
61
|
-
return `<head></head>${html}`;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function injectCssIntoHtml(html: string, css: string): string {
|
|
65
|
-
const headCloseIdx = html.indexOf("</head>");
|
|
66
|
-
if (headCloseIdx !== -1) {
|
|
67
|
-
return (
|
|
68
|
-
html.slice(0, headCloseIdx) +
|
|
69
|
-
`<style>${css}</style>` +
|
|
70
|
-
html.slice(headCloseIdx)
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
return `<head><style>${css}</style></head>${html}`;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
type SandboxInstance = {
|
|
77
|
-
iframe: HTMLIFrameElement;
|
|
78
|
-
promise: Promise<unknown>;
|
|
79
|
-
run: (code: string | Function) => Promise<unknown>;
|
|
80
|
-
destroy: () => void;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
type WebsandboxModule = {
|
|
84
|
-
create: (
|
|
85
|
-
localApi: Record<string, unknown>,
|
|
86
|
-
options: {
|
|
87
|
-
frameContainer: HTMLElement;
|
|
88
|
-
frameContent: string;
|
|
89
|
-
allowAdditionalAttributes: string;
|
|
90
|
-
},
|
|
91
|
-
) => SandboxInstance;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
async function loadWebsandbox(): Promise<WebsandboxModule> {
|
|
95
|
-
const mod = (await import("@jetbrains/websandbox")) as any;
|
|
96
|
-
return (mod.default?.default ?? mod.default) as WebsandboxModule;
|
|
97
|
-
}
|
|
47
|
+
}) satisfies z.ZodType<GenerateSandboxedUiArgs>;
|
|
98
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Renders Open Generative UI content. The core controller throttles the
|
|
51
|
+
* content and runs the websandbox iframes inside the container div.
|
|
52
|
+
*/
|
|
99
53
|
export const OpenGenerativeUIRenderer = defineComponent({
|
|
100
54
|
name: "OpenGenerativeUIRenderer",
|
|
101
55
|
props: {
|
|
@@ -107,307 +61,32 @@ export const OpenGenerativeUIRenderer = defineComponent({
|
|
|
107
61
|
setup(props) {
|
|
108
62
|
const sandboxFunctions = useSandboxFunctions();
|
|
109
63
|
const containerRef = ref<HTMLElement | null>(null);
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
const sandboxReady = ref(false);
|
|
116
|
-
const previewReady = ref(false);
|
|
117
|
-
const executedExpressionIndex = ref(0);
|
|
118
|
-
const jsFunctionsInjected = ref(false);
|
|
119
|
-
const pendingQueue = ref<string[]>([]);
|
|
120
|
-
|
|
121
|
-
const localApi = computed(() => {
|
|
122
|
-
const api: Record<string, unknown> = {};
|
|
123
|
-
for (const fn of sandboxFunctions.value) {
|
|
124
|
-
api[fn.name] = fn.handler;
|
|
125
|
-
}
|
|
126
|
-
return api;
|
|
64
|
+
const controller = new ɵOpenGenerativeUIController();
|
|
65
|
+
controller.setContent(props.content);
|
|
66
|
+
const state = shallowRef(controller.getState());
|
|
67
|
+
const unsubscribe = controller.subscribe(() => {
|
|
68
|
+
state.value = controller.getState();
|
|
127
69
|
});
|
|
128
70
|
|
|
129
71
|
watch(
|
|
130
72
|
() => props.content,
|
|
131
|
-
(
|
|
132
|
-
latestContent.value = next;
|
|
133
|
-
if (shouldFlushImmediately(throttledContent.value, next)) {
|
|
134
|
-
if (throttleTimer.value !== null) {
|
|
135
|
-
window.clearTimeout(throttleTimer.value);
|
|
136
|
-
throttleTimer.value = null;
|
|
137
|
-
}
|
|
138
|
-
throttledContent.value = next;
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
if (throttleTimer.value === null) {
|
|
142
|
-
throttleTimer.value = window.setTimeout(() => {
|
|
143
|
-
throttledContent.value = latestContent.value;
|
|
144
|
-
throttleTimer.value = null;
|
|
145
|
-
}, 1000);
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
{ immediate: true },
|
|
73
|
+
(content) => controller.setContent(content),
|
|
149
74
|
);
|
|
150
|
-
|
|
151
|
-
onBeforeUnmount(() => {
|
|
152
|
-
if (throttleTimer.value !== null) {
|
|
153
|
-
window.clearTimeout(throttleTimer.value);
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
const partialHtml = computed(() => {
|
|
158
|
-
if (throttledContent.value.htmlComplete) return undefined;
|
|
159
|
-
if (!throttledContent.value.html?.length) return undefined;
|
|
160
|
-
return throttledContent.value.html.join("");
|
|
161
|
-
});
|
|
162
|
-
const previewBody = computed(() =>
|
|
163
|
-
partialHtml.value ? processPartialHtml(partialHtml.value) : undefined,
|
|
164
|
-
);
|
|
165
|
-
const previewStyles = computed(() =>
|
|
166
|
-
partialHtml.value ? extractCompleteStyles(partialHtml.value) : "",
|
|
167
|
-
);
|
|
168
|
-
const fullHtml = computed(() =>
|
|
169
|
-
throttledContent.value.htmlComplete && throttledContent.value.html?.length
|
|
170
|
-
? throttledContent.value.html.join("")
|
|
171
|
-
: undefined,
|
|
172
|
-
);
|
|
173
|
-
const css = computed(() =>
|
|
174
|
-
throttledContent.value.cssComplete
|
|
175
|
-
? throttledContent.value.css
|
|
176
|
-
: undefined,
|
|
177
|
-
);
|
|
178
|
-
const hasPreview = computed(
|
|
179
|
-
() =>
|
|
180
|
-
!!throttledContent.value.cssComplete &&
|
|
181
|
-
!!previewBody.value &&
|
|
182
|
-
previewBody.value.trim().length > 0,
|
|
183
|
-
);
|
|
184
|
-
const hasVisibleSandbox = computed(
|
|
185
|
-
() => !!fullHtml.value || hasPreview.value,
|
|
186
|
-
);
|
|
187
|
-
const resolvedHeight = computed(
|
|
188
|
-
() => `${throttledContent.value.initialHeight ?? 200}px`,
|
|
189
|
-
);
|
|
190
|
-
|
|
191
|
-
const destroyPreview = () => {
|
|
192
|
-
if (previewSandboxRef.value) {
|
|
193
|
-
previewSandboxRef.value.destroy();
|
|
194
|
-
previewSandboxRef.value = null;
|
|
195
|
-
}
|
|
196
|
-
previewReady.value = false;
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
const destroyFinal = () => {
|
|
200
|
-
if (sandboxRef.value) {
|
|
201
|
-
sandboxRef.value.destroy();
|
|
202
|
-
sandboxRef.value = null;
|
|
203
|
-
}
|
|
204
|
-
sandboxReady.value = false;
|
|
205
|
-
pendingQueue.value = [];
|
|
206
|
-
executedExpressionIndex.value = 0;
|
|
207
|
-
jsFunctionsInjected.value = false;
|
|
208
|
-
};
|
|
209
|
-
|
|
210
75
|
watch(
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
fullHtml,
|
|
214
|
-
css,
|
|
215
|
-
previewBody,
|
|
216
|
-
previewStyles,
|
|
217
|
-
() => containerRef.value,
|
|
218
|
-
],
|
|
219
|
-
async ([previewVisible, htmlComplete], _previous, onCleanup) => {
|
|
220
|
-
if (!previewVisible || htmlComplete || !containerRef.value) return;
|
|
221
|
-
if (previewSandboxRef.value) return;
|
|
222
|
-
|
|
223
|
-
let cancelled = false;
|
|
224
|
-
try {
|
|
225
|
-
const Websandbox = await loadWebsandbox();
|
|
226
|
-
if (cancelled || !containerRef.value) return;
|
|
227
|
-
|
|
228
|
-
const sandbox = Websandbox.create(
|
|
229
|
-
{},
|
|
230
|
-
{
|
|
231
|
-
frameContainer: containerRef.value,
|
|
232
|
-
frameContent: "<head></head><body></body>",
|
|
233
|
-
allowAdditionalAttributes: "",
|
|
234
|
-
},
|
|
235
|
-
);
|
|
236
|
-
previewSandboxRef.value = sandbox;
|
|
237
|
-
sandbox.iframe.setAttribute(
|
|
238
|
-
"data-testid",
|
|
239
|
-
"open-generative-ui-preview-sandbox",
|
|
240
|
-
);
|
|
241
|
-
sandbox.iframe.style.width = "100%";
|
|
242
|
-
sandbox.iframe.style.height = "100%";
|
|
243
|
-
sandbox.iframe.style.border = "none";
|
|
244
|
-
sandbox.iframe.style.backgroundColor = "transparent";
|
|
245
|
-
|
|
246
|
-
sandbox.promise.then(() => {
|
|
247
|
-
if (cancelled || !previewSandboxRef.value) return;
|
|
248
|
-
previewReady.value = true;
|
|
249
|
-
void sandbox.run(
|
|
250
|
-
"var s=document.createElement('style');s.textContent='html, body { overflow: hidden !important; }';document.head.appendChild(s);",
|
|
251
|
-
);
|
|
252
|
-
});
|
|
253
|
-
} catch (error) {
|
|
254
|
-
console.error(
|
|
255
|
-
"[OpenGenerativeUI] Failed to load sandbox module:",
|
|
256
|
-
error,
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
onCleanup(() => {
|
|
261
|
-
cancelled = true;
|
|
262
|
-
});
|
|
263
|
-
},
|
|
264
|
-
{ immediate: true },
|
|
265
|
-
);
|
|
266
|
-
|
|
267
|
-
watch(
|
|
268
|
-
[previewBody, previewStyles, css, previewReady],
|
|
269
|
-
([body, styles, cssText, ready]) => {
|
|
270
|
-
if (!previewSandboxRef.value || !ready) return;
|
|
271
|
-
const headParts: string[] = [];
|
|
272
|
-
if (cssText) headParts.push(`<style>${cssText}</style>`);
|
|
273
|
-
if (styles) headParts.push(styles);
|
|
274
|
-
if (headParts.length) {
|
|
275
|
-
void previewSandboxRef.value.run(
|
|
276
|
-
`document.head.innerHTML = ${JSON.stringify(headParts.join(""))}`,
|
|
277
|
-
);
|
|
278
|
-
}
|
|
279
|
-
if (body) {
|
|
280
|
-
void previewSandboxRef.value.run(
|
|
281
|
-
`document.body.innerHTML = ${JSON.stringify(body)}`,
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
},
|
|
76
|
+
sandboxFunctions,
|
|
77
|
+
(functions) => controller.setSandboxFunctions(functions),
|
|
285
78
|
{ immediate: true },
|
|
286
79
|
);
|
|
287
|
-
|
|
288
|
-
watch(
|
|
289
|
-
[fullHtml, css, localApi, () => containerRef.value],
|
|
290
|
-
async ([html, cssText, api], _previous, onCleanup) => {
|
|
291
|
-
destroyFinal();
|
|
292
|
-
if (!html || !containerRef.value) return;
|
|
293
|
-
destroyPreview();
|
|
294
|
-
|
|
295
|
-
let cancelled = false;
|
|
296
|
-
try {
|
|
297
|
-
const Websandbox = await loadWebsandbox();
|
|
298
|
-
if (cancelled || !containerRef.value) return;
|
|
299
|
-
const htmlWithHead = ensureHead(html);
|
|
300
|
-
const htmlWithCss = cssText
|
|
301
|
-
? injectCssIntoHtml(htmlWithHead, cssText)
|
|
302
|
-
: htmlWithHead;
|
|
303
|
-
const sandbox = Websandbox.create(api, {
|
|
304
|
-
frameContainer: containerRef.value,
|
|
305
|
-
frameContent: htmlWithCss,
|
|
306
|
-
allowAdditionalAttributes: "",
|
|
307
|
-
});
|
|
308
|
-
sandboxRef.value = sandbox;
|
|
309
|
-
sandbox.iframe.setAttribute(
|
|
310
|
-
"data-testid",
|
|
311
|
-
"open-generative-ui-final-sandbox",
|
|
312
|
-
);
|
|
313
|
-
sandbox.iframe.style.width = "100%";
|
|
314
|
-
sandbox.iframe.style.height = "100%";
|
|
315
|
-
sandbox.iframe.style.border = "none";
|
|
316
|
-
sandbox.iframe.style.backgroundColor = "transparent";
|
|
317
|
-
|
|
318
|
-
sandbox.promise.then(() => {
|
|
319
|
-
if (cancelled || !sandboxRef.value) return;
|
|
320
|
-
sandboxReady.value = true;
|
|
321
|
-
void sandbox.run(
|
|
322
|
-
"var s=document.createElement('style');s.textContent='html, body { overflow: hidden !important; }';document.head.appendChild(s);",
|
|
323
|
-
);
|
|
324
|
-
const functionsCode = throttledContent.value.jsFunctions;
|
|
325
|
-
if (functionsCode && !jsFunctionsInjected.value) {
|
|
326
|
-
jsFunctionsInjected.value = true;
|
|
327
|
-
pendingQueue.value.unshift(functionsCode);
|
|
328
|
-
}
|
|
329
|
-
const expressions = throttledContent.value.jsExpressions;
|
|
330
|
-
if (expressions?.length) {
|
|
331
|
-
const startIndex = executedExpressionIndex.value;
|
|
332
|
-
if (startIndex < expressions.length) {
|
|
333
|
-
pendingQueue.value.push(...expressions.slice(startIndex));
|
|
334
|
-
executedExpressionIndex.value = expressions.length;
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
const queue = [...pendingQueue.value];
|
|
338
|
-
pendingQueue.value = [];
|
|
339
|
-
for (const code of queue) {
|
|
340
|
-
void sandbox.run(code);
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
} catch (error) {
|
|
344
|
-
console.error(
|
|
345
|
-
"[OpenGenerativeUI] Failed to load sandbox module:",
|
|
346
|
-
error,
|
|
347
|
-
);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
onCleanup(() => {
|
|
351
|
-
cancelled = true;
|
|
352
|
-
});
|
|
353
|
-
},
|
|
354
|
-
{ immediate: true },
|
|
355
|
-
);
|
|
356
|
-
|
|
357
|
-
watch(
|
|
358
|
-
() => throttledContent.value.jsFunctions,
|
|
359
|
-
(functionsCode) => {
|
|
360
|
-
if (!functionsCode || jsFunctionsInjected.value) return;
|
|
361
|
-
jsFunctionsInjected.value = true;
|
|
362
|
-
if (sandboxReady.value && sandboxRef.value) {
|
|
363
|
-
void sandboxRef.value.run(functionsCode);
|
|
364
|
-
} else {
|
|
365
|
-
pendingQueue.value.push(functionsCode);
|
|
366
|
-
}
|
|
367
|
-
},
|
|
368
|
-
);
|
|
369
|
-
|
|
370
|
-
watch(
|
|
371
|
-
() => throttledContent.value.jsExpressions,
|
|
372
|
-
(expressions) => {
|
|
373
|
-
if (!expressions?.length) return;
|
|
374
|
-
const startIndex = executedExpressionIndex.value;
|
|
375
|
-
if (startIndex >= expressions.length) return;
|
|
376
|
-
const newExpressions = expressions.slice(startIndex);
|
|
377
|
-
executedExpressionIndex.value = expressions.length;
|
|
378
|
-
if (sandboxReady.value && sandboxRef.value) {
|
|
379
|
-
for (const expression of newExpressions) {
|
|
380
|
-
void sandboxRef.value.run(expression);
|
|
381
|
-
}
|
|
382
|
-
} else {
|
|
383
|
-
pendingQueue.value.push(...newExpressions);
|
|
384
|
-
}
|
|
385
|
-
},
|
|
386
|
-
{ deep: true },
|
|
387
|
-
);
|
|
388
|
-
|
|
389
|
-
const isGenerating = computed(
|
|
390
|
-
() => throttledContent.value.generating !== false,
|
|
391
|
-
);
|
|
392
|
-
watch(
|
|
393
|
-
[hasPreview, fullHtml],
|
|
394
|
-
([previewVisible, html]) => {
|
|
395
|
-
if (html) destroyPreview();
|
|
396
|
-
if (!previewVisible) destroyPreview();
|
|
397
|
-
},
|
|
398
|
-
{ immediate: true },
|
|
399
|
-
);
|
|
400
|
-
|
|
80
|
+
onMounted(() => controller.setHost(containerRef.value));
|
|
401
81
|
onBeforeUnmount(() => {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
destroyPreview();
|
|
406
|
-
destroyFinal();
|
|
82
|
+
unsubscribe();
|
|
83
|
+
controller.destroy();
|
|
407
84
|
});
|
|
408
85
|
|
|
409
|
-
return () =>
|
|
410
|
-
|
|
86
|
+
return () => {
|
|
87
|
+
const { phase, generating, height } = state.value;
|
|
88
|
+
const hasVisibleSandbox = phase !== "placeholder";
|
|
89
|
+
return h(
|
|
411
90
|
"div",
|
|
412
91
|
{
|
|
413
92
|
ref: containerRef,
|
|
@@ -415,20 +94,18 @@ export const OpenGenerativeUIRenderer = defineComponent({
|
|
|
415
94
|
style: {
|
|
416
95
|
position: "relative",
|
|
417
96
|
width: "100%",
|
|
418
|
-
height:
|
|
97
|
+
height: `${height}px`,
|
|
419
98
|
borderRadius: "8px",
|
|
420
|
-
backgroundColor: hasVisibleSandbox
|
|
421
|
-
|
|
422
|
-
: "#f5f5f5",
|
|
423
|
-
border: hasVisibleSandbox.value ? "none" : "1px solid #e0e0e0",
|
|
99
|
+
backgroundColor: hasVisibleSandbox ? "transparent" : "#f5f5f5",
|
|
100
|
+
border: hasVisibleSandbox ? "none" : "1px solid #e0e0e0",
|
|
424
101
|
overflow: "hidden",
|
|
425
|
-
display: hasVisibleSandbox
|
|
426
|
-
alignItems: hasVisibleSandbox
|
|
427
|
-
justifyContent: hasVisibleSandbox
|
|
102
|
+
display: hasVisibleSandbox ? "block" : "flex",
|
|
103
|
+
alignItems: hasVisibleSandbox ? undefined : "center",
|
|
104
|
+
justifyContent: hasVisibleSandbox ? undefined : "center",
|
|
428
105
|
},
|
|
429
106
|
},
|
|
430
107
|
[
|
|
431
|
-
!hasVisibleSandbox
|
|
108
|
+
!hasVisibleSandbox
|
|
432
109
|
? h("div", { "data-testid": "open-generative-ui-placeholder" }, [
|
|
433
110
|
h(
|
|
434
111
|
"svg",
|
|
@@ -461,7 +138,7 @@ export const OpenGenerativeUIRenderer = defineComponent({
|
|
|
461
138
|
),
|
|
462
139
|
])
|
|
463
140
|
: null,
|
|
464
|
-
|
|
141
|
+
generating
|
|
465
142
|
? h("div", {
|
|
466
143
|
"data-testid": "open-generative-ui-progress-overlay",
|
|
467
144
|
style: {
|
|
@@ -473,6 +150,7 @@ export const OpenGenerativeUIRenderer = defineComponent({
|
|
|
473
150
|
: null,
|
|
474
151
|
],
|
|
475
152
|
);
|
|
153
|
+
};
|
|
476
154
|
},
|
|
477
155
|
});
|
|
478
156
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { z } from "zod";
|
|
2
|
+
import { z } from "zod-v3";
|
|
3
3
|
import { Catalog } from "@a2ui/web_core/v0_9";
|
|
4
4
|
import type { ComponentApi } from "@a2ui/web_core/v0_9";
|
|
5
5
|
import { buildCatalogContextValue } from "../a2ui/A2UICatalogContext";
|