@copilotkit/shared 1.82.0 → 1.84.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/dist/a2ui-envelope.cjs +2 -2
- package/dist/a2ui-envelope.cjs.map +1 -1
- package/dist/a2ui-envelope.d.cts +1 -1
- package/dist/a2ui-envelope.d.cts.map +1 -1
- package/dist/a2ui-envelope.d.mts +1 -1
- package/dist/a2ui-envelope.d.mts.map +1 -1
- package/dist/a2ui-envelope.mjs +2 -2
- package/dist/a2ui-envelope.mjs.map +1 -1
- package/dist/a2ui-prompts.cjs +21 -62
- package/dist/a2ui-prompts.cjs.map +1 -1
- package/dist/a2ui-prompts.d.cts +8 -1
- package/dist/a2ui-prompts.d.cts.map +1 -1
- package/dist/a2ui-prompts.d.mts +8 -1
- package/dist/a2ui-prompts.d.mts.map +1 -1
- package/dist/a2ui-prompts.mjs +22 -62
- package/dist/a2ui-prompts.mjs.map +1 -1
- package/dist/generative-ui/basic-functions.cjs +1 -1
- package/dist/generative-ui/basic-functions.cjs.map +1 -1
- package/dist/generative-ui/basic-functions.mjs +1 -1
- package/dist/generative-ui/basic-functions.mjs.map +1 -1
- package/dist/generative-ui/compact-schema.cjs +187 -0
- package/dist/generative-ui/compact-schema.cjs.map +1 -0
- package/dist/generative-ui/compact-schema.d.cts +55 -0
- package/dist/generative-ui/compact-schema.d.cts.map +1 -0
- package/dist/generative-ui/compact-schema.d.mts +55 -0
- package/dist/generative-ui/compact-schema.d.mts.map +1 -0
- package/dist/generative-ui/compact-schema.mjs +185 -0
- package/dist/generative-ui/compact-schema.mjs.map +1 -0
- package/dist/generative-ui/prompt-spec.cjs +225 -0
- package/dist/generative-ui/prompt-spec.cjs.map +1 -0
- package/dist/generative-ui/prompt-spec.d.cts +94 -0
- package/dist/generative-ui/prompt-spec.d.cts.map +1 -0
- package/dist/generative-ui/prompt-spec.d.mts +94 -0
- package/dist/generative-ui/prompt-spec.d.mts.map +1 -0
- package/dist/generative-ui/prompt-spec.mjs +220 -0
- package/dist/generative-ui/prompt-spec.mjs.map +1 -0
- package/dist/generative-ui/prompt.cjs +10 -36
- package/dist/generative-ui/prompt.cjs.map +1 -1
- package/dist/generative-ui/prompt.d.cts +2 -1
- package/dist/generative-ui/prompt.d.cts.map +1 -1
- package/dist/generative-ui/prompt.d.mts +2 -1
- package/dist/generative-ui/prompt.d.mts.map +1 -1
- package/dist/generative-ui/prompt.mjs +12 -38
- package/dist/generative-ui/prompt.mjs.map +1 -1
- package/dist/generative-ui/schema-render.cjs +242 -0
- package/dist/generative-ui/schema-render.cjs.map +1 -0
- package/dist/generative-ui/schema-render.d.cts +44 -0
- package/dist/generative-ui/schema-render.d.cts.map +1 -0
- package/dist/generative-ui/schema-render.d.mts +44 -0
- package/dist/generative-ui/schema-render.d.mts.map +1 -0
- package/dist/generative-ui/schema-render.mjs +239 -0
- package/dist/generative-ui/schema-render.mjs.map +1 -0
- package/dist/generative-ui/types.cjs +2 -2
- package/dist/generative-ui/types.cjs.map +1 -1
- package/dist/generative-ui/types.d.cts +7 -7
- package/dist/generative-ui/types.d.cts.map +1 -1
- package/dist/generative-ui/types.d.mts +7 -7
- package/dist/generative-ui/types.d.mts.map +1 -1
- package/dist/generative-ui/types.mjs +2 -2
- package/dist/generative-ui/types.mjs.map +1 -1
- package/dist/index.cjs +14 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -1
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +756 -165
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/package.json +1 -1
- package/src/__tests__/a2ui-envelope.test.ts +18 -13
- package/src/__tests__/a2ui-prompts.test.ts +47 -14
- package/src/__tests__/fixtures/prompt-goldens.json +354 -0
- package/src/__tests__/prompt-spec.test.ts +191 -0
- package/src/__tests__/schema-render.test.ts +245 -0
- package/src/a2ui-envelope.ts +5 -5
- package/src/a2ui-prompts.ts +47 -96
- package/src/generative-ui/__tests__/render-ui-args.test.ts +7 -7
- package/src/generative-ui/__tests__/validate-operation.test.ts +19 -13
- package/src/generative-ui/basic-functions.ts +1 -1
- package/src/generative-ui/compact-schema.ts +277 -0
- package/src/generative-ui/index.ts +3 -0
- package/src/generative-ui/prompt-spec.ts +335 -0
- package/src/generative-ui/prompt.ts +25 -75
- package/src/generative-ui/schema-render.ts +374 -0
- package/src/generative-ui/types.ts +10 -7
- package/src/utils/__tests__/conditions.test.ts +153 -43
|
@@ -2,11 +2,11 @@ import { describe, expect, it } from "vitest";
|
|
|
2
2
|
import { RenderUIArgsStream, extractRenderUIArgs } from "../render-ui-args";
|
|
3
3
|
|
|
4
4
|
const opA = {
|
|
5
|
-
version: "
|
|
5
|
+
version: "1.0",
|
|
6
6
|
createSurface: { surfaceId: "s" },
|
|
7
7
|
};
|
|
8
8
|
const opB = {
|
|
9
|
-
version: "
|
|
9
|
+
version: "1.0",
|
|
10
10
|
updateComponents: {
|
|
11
11
|
surfaceId: "s",
|
|
12
12
|
components: [
|
|
@@ -20,12 +20,12 @@ const opB = {
|
|
|
20
20
|
},
|
|
21
21
|
};
|
|
22
22
|
const opC = {
|
|
23
|
-
version: "
|
|
23
|
+
version: "1.0",
|
|
24
24
|
updateDataModel: { surfaceId: "s", path: "/a", value: [1, { b: "]" }] },
|
|
25
25
|
};
|
|
26
26
|
/** One createSurface with the data model and the root inline. */
|
|
27
27
|
const inlineCreate = {
|
|
28
|
-
version: "
|
|
28
|
+
version: "1.0",
|
|
29
29
|
createSurface: {
|
|
30
30
|
surfaceId: "plan",
|
|
31
31
|
dataModel: { seats: 10, plans: [{ name: "Team" }] },
|
|
@@ -40,7 +40,7 @@ const inlineCreate = {
|
|
|
40
40
|
},
|
|
41
41
|
};
|
|
42
42
|
const functionCall = {
|
|
43
|
-
version: "
|
|
43
|
+
version: "1.0",
|
|
44
44
|
callRendererFunction: {
|
|
45
45
|
functionCallId: "f1",
|
|
46
46
|
callFunction: { "@call": "addToCart", args: { productId: "p{1}" } },
|
|
@@ -75,7 +75,7 @@ describe("extractRenderUIArgs", () => {
|
|
|
75
75
|
|
|
76
76
|
it("returns null for a balanced element that is not valid JSON", () => {
|
|
77
77
|
const text =
|
|
78
|
-
'{"target":"chat","operations":[{"version":"
|
|
78
|
+
'{"target":"chat","operations":[{"version":"1.0", bad},' +
|
|
79
79
|
JSON.stringify(opA) +
|
|
80
80
|
"]}";
|
|
81
81
|
expect(extractRenderUIArgs(text).operations).toEqual([null, opA]);
|
|
@@ -140,7 +140,7 @@ describe("RenderUIArgsStream", () => {
|
|
|
140
140
|
|
|
141
141
|
it("keeps the parser state across deltas that split strings, escapes and braces", () => {
|
|
142
142
|
const tricky = {
|
|
143
|
-
version: "
|
|
143
|
+
version: "1.0",
|
|
144
144
|
updateDataModel: { surfaceId: "s", path: "/q", value: 'a "}{" \\ ]' },
|
|
145
145
|
};
|
|
146
146
|
const text = JSON.stringify({
|
|
@@ -62,19 +62,19 @@ const functions = {
|
|
|
62
62
|
const knownSurfaces = new Set(["s1"]);
|
|
63
63
|
|
|
64
64
|
const update = (...components: unknown[]) => ({
|
|
65
|
-
version: "
|
|
65
|
+
version: "1.0",
|
|
66
66
|
updateComponents: { surfaceId: "s1", components },
|
|
67
67
|
});
|
|
68
68
|
const setData = (fields: { path?: string; value?: unknown }) => ({
|
|
69
|
-
version: "
|
|
69
|
+
version: "1.0",
|
|
70
70
|
updateDataModel: { surfaceId: "s1", ...fields },
|
|
71
71
|
});
|
|
72
72
|
const create = (fields: Record<string, unknown>) => ({
|
|
73
|
-
version: "
|
|
73
|
+
version: "1.0",
|
|
74
74
|
createSurface: { surfaceId: "s2", ...fields },
|
|
75
75
|
});
|
|
76
76
|
const callFunction = (fields: Record<string, unknown>) => ({
|
|
77
|
-
version: "
|
|
77
|
+
version: "1.0",
|
|
78
78
|
callRendererFunction: { functionCallId: "f1", ...fields },
|
|
79
79
|
});
|
|
80
80
|
/** A valid ProductCard with `props` added or replaced. */
|
|
@@ -90,23 +90,29 @@ const reasonOf = (
|
|
|
90
90
|
};
|
|
91
91
|
|
|
92
92
|
describe("validateOperation: version and kind", () => {
|
|
93
|
-
it("rejects
|
|
93
|
+
it("rejects the pre-release v1.0 spelling", () => {
|
|
94
|
+
expect(
|
|
95
|
+
reasonOf({ version: "v1.0", deleteSurface: { surfaceId: "s1" } }),
|
|
96
|
+
).toBe('unsupported version "v1.0"; use "1.0"');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("rejects a v0.9 operation with a reason that names 1.0", () => {
|
|
94
100
|
expect(
|
|
95
101
|
reasonOf({ version: "v0.9", deleteSurface: { surfaceId: "s1" } }),
|
|
96
|
-
).toBe('unsupported version "v0.9"; use "
|
|
102
|
+
).toBe('unsupported version "v0.9"; use "1.0"');
|
|
97
103
|
});
|
|
98
104
|
|
|
99
105
|
it("rejects an operation without a version", () => {
|
|
100
106
|
expect(reasonOf({ deleteSurface: { surfaceId: "s1" } })).toBe(
|
|
101
|
-
'operation needs "version":"
|
|
107
|
+
'operation needs "version":"1.0"',
|
|
102
108
|
);
|
|
103
109
|
});
|
|
104
110
|
|
|
105
111
|
it("rejects ops with zero or several kinds", () => {
|
|
106
|
-
expect(reasonOf({ version: "
|
|
112
|
+
expect(reasonOf({ version: "1.0" })).toMatch(/exactly one/);
|
|
107
113
|
expect(
|
|
108
114
|
reasonOf({
|
|
109
|
-
version: "
|
|
115
|
+
version: "1.0",
|
|
110
116
|
deleteSurface: { surfaceId: "s1" },
|
|
111
117
|
updateDataModel: { surfaceId: "s1", value: 1 },
|
|
112
118
|
}),
|
|
@@ -133,7 +139,7 @@ describe("validateOperation: createSurface", () => {
|
|
|
133
139
|
};
|
|
134
140
|
const check = (catalogId: string) =>
|
|
135
141
|
validateOperation(
|
|
136
|
-
{ version: "
|
|
142
|
+
{ version: "1.0", createSurface: { surfaceId: "s2", catalogId } },
|
|
137
143
|
options,
|
|
138
144
|
);
|
|
139
145
|
expect(check("copilotkit/standard@1")).toEqual({ ok: true });
|
|
@@ -146,13 +152,13 @@ describe("validateOperation: createSurface", () => {
|
|
|
146
152
|
|
|
147
153
|
it("rejects a createSurface without a surfaceId", () => {
|
|
148
154
|
expect(
|
|
149
|
-
reasonOf({ version: "
|
|
155
|
+
reasonOf({ version: "1.0", createSurface: { catalogId: "shop" } }),
|
|
150
156
|
).toBe("createSurface.surfaceId is required");
|
|
151
157
|
});
|
|
152
158
|
|
|
153
159
|
it("rejects an existing surface", () => {
|
|
154
160
|
expect(
|
|
155
|
-
reasonOf({ version: "
|
|
161
|
+
reasonOf({ version: "1.0", createSurface: { surfaceId: "s1" } }),
|
|
156
162
|
).toBe('surface "s1" already exists');
|
|
157
163
|
});
|
|
158
164
|
|
|
@@ -201,7 +207,7 @@ describe("validateOperation: surface updates", () => {
|
|
|
201
207
|
it("rejects updates to surfaces that do not exist", () => {
|
|
202
208
|
expect(
|
|
203
209
|
reasonOf({
|
|
204
|
-
version: "
|
|
210
|
+
version: "1.0",
|
|
205
211
|
updateComponents: {
|
|
206
212
|
surfaceId: "missing",
|
|
207
213
|
components: [{ id: "root", component: "ProductGrid" }],
|
|
@@ -189,7 +189,7 @@ export const COPILOTKIT_FUNCTIONS: Readonly<
|
|
|
189
189
|
> = rendererOnly({
|
|
190
190
|
calc: {
|
|
191
191
|
description:
|
|
192
|
-
"The value of an arithmetic expression over the other args, by name: + - * / % ( ), comparisons, && || !, cond ? a : b, min max round floor ceil abs, 'text' + name",
|
|
192
|
+
"The value of an arithmetic expression over the other args, by name: + - * / % ( ), x ^ y (power), comparisons, && || !, cond ? a : b, min max round floor ceil abs pow sqrt exp ln log10, the constants pi and e, 'text' + name",
|
|
193
193
|
parameters:
|
|
194
194
|
"{ expression: string; [name: string]: number | string | boolean }",
|
|
195
195
|
returnType: "any",
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { childRefKind } from "./child-refs";
|
|
2
|
+
import { functionCallers } from "./basic-functions";
|
|
3
|
+
import { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from "./types";
|
|
4
|
+
import type { GenerativeUIJSONSchema, GenerativeUIManifest } from "./types";
|
|
5
|
+
|
|
6
|
+
/** Objects nested deeper than this show as `object`. */
|
|
7
|
+
const MAX_OBJECT_DEPTH = 2;
|
|
8
|
+
|
|
9
|
+
/** A prop must appear verbatim in at least this many components to be hoisted. */
|
|
10
|
+
const COMMON_PROP_MIN_COMPONENTS = 8;
|
|
11
|
+
|
|
12
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
13
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A JSON Schema of a list of schemas (`anyOf`, `oneOf`), else an empty list. */
|
|
17
|
+
function schemaList(value: unknown) {
|
|
18
|
+
return Array.isArray(value) ? value.filter(isRecord) : [];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Puts a top-level union in parentheses, so that `[]` applies to all of
|
|
23
|
+
* it. A union inside braces needs none.
|
|
24
|
+
*/
|
|
25
|
+
function asArrayItem(type: string) {
|
|
26
|
+
let outside = type;
|
|
27
|
+
for (let inner = outside.replace(/\{[^{}]*\}/g, ""); inner !== outside; ) {
|
|
28
|
+
outside = inner;
|
|
29
|
+
inner = outside.replace(/\{[^{}]*\}/g, "");
|
|
30
|
+
}
|
|
31
|
+
return outside.includes(" | ") ? `(${type})[]` : `${type}[]`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A JSON Schema as a short TypeScript-like type, for example
|
|
36
|
+
* `{ label: string; size?: "sm" | "md" }`.
|
|
37
|
+
*/
|
|
38
|
+
export function compactType(
|
|
39
|
+
schema: GenerativeUIJSONSchema,
|
|
40
|
+
depth: number,
|
|
41
|
+
): string {
|
|
42
|
+
const ref = childRefKind(schema);
|
|
43
|
+
if (ref === "single") return "componentId";
|
|
44
|
+
if (ref === "multiple")
|
|
45
|
+
return "componentId[] | { componentId: string; path: string }";
|
|
46
|
+
if ("const" in schema) return JSON.stringify(schema.const);
|
|
47
|
+
if (Array.isArray(schema.enum)) {
|
|
48
|
+
return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
|
|
49
|
+
}
|
|
50
|
+
const variants = [...schemaList(schema.anyOf), ...schemaList(schema.oneOf)];
|
|
51
|
+
if (variants.length > 0) {
|
|
52
|
+
return variants.map((variant) => compactType(variant, depth)).join(" | ");
|
|
53
|
+
}
|
|
54
|
+
const types = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
55
|
+
const summaries = types.map((type) => {
|
|
56
|
+
switch (type) {
|
|
57
|
+
case "string":
|
|
58
|
+
case "boolean":
|
|
59
|
+
case "null":
|
|
60
|
+
return type;
|
|
61
|
+
case "number":
|
|
62
|
+
case "integer":
|
|
63
|
+
return "number";
|
|
64
|
+
case "array":
|
|
65
|
+
return isRecord(schema.items)
|
|
66
|
+
? asArrayItem(compactType(schema.items, depth))
|
|
67
|
+
: "unknown[]";
|
|
68
|
+
case "object":
|
|
69
|
+
return compactObject(schema, depth);
|
|
70
|
+
default:
|
|
71
|
+
return "unknown";
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return summaries.join(" | ");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** An object schema as `{ key: type; other?: type }`. */
|
|
78
|
+
function compactObject(schema: GenerativeUIJSONSchema, depth: number) {
|
|
79
|
+
const properties = isRecord(schema.properties) ? schema.properties : {};
|
|
80
|
+
const entries = Object.entries(properties);
|
|
81
|
+
if (entries.length === 0) {
|
|
82
|
+
return isRecord(schema.additionalProperties)
|
|
83
|
+
? `Record<string, ${compactType(schema.additionalProperties, depth + 1)}>`
|
|
84
|
+
: "object";
|
|
85
|
+
}
|
|
86
|
+
if (depth >= MAX_OBJECT_DEPTH) return "object";
|
|
87
|
+
const required = Array.isArray(schema.required) ? schema.required : [];
|
|
88
|
+
const fields = entries.map(([key, value]) => {
|
|
89
|
+
const type = isRecord(value) ? compactType(value, depth + 1) : "unknown";
|
|
90
|
+
return `${key}${required.includes(key) ? "" : "?"}: ${type}`;
|
|
91
|
+
});
|
|
92
|
+
return `{ ${fields.join("; ")} }`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A schema as its type, with its description after `//`. */
|
|
96
|
+
function describedType(schema: GenerativeUIJSONSchema, depth: number) {
|
|
97
|
+
const type = compactType(schema, depth);
|
|
98
|
+
return typeof schema.description === "string"
|
|
99
|
+
? `${type} // ${schema.description}`
|
|
100
|
+
: type;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The props of a component: `{ "label": "string", "size?": "..." }`. With
|
|
105
|
+
* `descriptions`, each type carries its description after `//`.
|
|
106
|
+
*/
|
|
107
|
+
export function compactProps(
|
|
108
|
+
schema: GenerativeUIJSONSchema,
|
|
109
|
+
descriptions = true,
|
|
110
|
+
) {
|
|
111
|
+
const properties = isRecord(schema.properties) ? schema.properties : {};
|
|
112
|
+
const required = Array.isArray(schema.required) ? schema.required : [];
|
|
113
|
+
const props: Record<string, string> = {};
|
|
114
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
115
|
+
if (!isRecord(value)) continue;
|
|
116
|
+
const name = required.includes(key) ? key : `${key}?`;
|
|
117
|
+
props[name] = descriptions
|
|
118
|
+
? describedType(value, 1)
|
|
119
|
+
: compactType(value, 1);
|
|
120
|
+
}
|
|
121
|
+
return props;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The prop set worth listing once. Candidates are (key, type) pairs that at
|
|
126
|
+
* least {@link COMMON_PROP_MIN_COMPONENTS} components carry verbatim. The
|
|
127
|
+
* candidate subset of each component is a possible common set, scored by
|
|
128
|
+
* the characters it saves (set size times the components that contain all
|
|
129
|
+
* of it). `undefined` when no set is shared by enough components.
|
|
130
|
+
*/
|
|
131
|
+
function findCommonProps(propsList: Array<Record<string, string>>) {
|
|
132
|
+
const pairKey = (key: string, type: string) => `${key}\u0000${type}`;
|
|
133
|
+
const counts = new Map<string, number>();
|
|
134
|
+
for (const props of propsList) {
|
|
135
|
+
for (const [key, type] of Object.entries(props)) {
|
|
136
|
+
const id = pairKey(key, type);
|
|
137
|
+
counts.set(id, (counts.get(id) ?? 0) + 1);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
let best: Record<string, string> | undefined;
|
|
141
|
+
let bestScore = 0;
|
|
142
|
+
const seen = new Set<string>();
|
|
143
|
+
for (const props of propsList) {
|
|
144
|
+
const subset = Object.entries(props).filter(
|
|
145
|
+
([key, type]) =>
|
|
146
|
+
(counts.get(pairKey(key, type)) ?? 0) >= COMMON_PROP_MIN_COMPONENTS,
|
|
147
|
+
);
|
|
148
|
+
if (subset.length < 2) continue;
|
|
149
|
+
const signature = subset
|
|
150
|
+
.map(([key, type]) => pairKey(key, type))
|
|
151
|
+
.join("\n");
|
|
152
|
+
if (seen.has(signature)) continue;
|
|
153
|
+
seen.add(signature);
|
|
154
|
+
const members = propsList.filter((other) =>
|
|
155
|
+
subset.every(([key, type]) => other[key] === type),
|
|
156
|
+
).length;
|
|
157
|
+
if (members < COMMON_PROP_MIN_COMPONENTS) continue;
|
|
158
|
+
const size = subset.reduce(
|
|
159
|
+
(total, [key, type]) => total + key.length + type.length,
|
|
160
|
+
0,
|
|
161
|
+
);
|
|
162
|
+
const score = size * (members - 1);
|
|
163
|
+
if (score > bestScore) {
|
|
164
|
+
bestScore = score;
|
|
165
|
+
best = Object.fromEntries(subset);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return best;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** One component of the compact schema. */
|
|
172
|
+
interface CompactComponent {
|
|
173
|
+
description?: string;
|
|
174
|
+
/** True when the component also takes every prop in `commonProps`. */
|
|
175
|
+
includesCommonProps?: true;
|
|
176
|
+
props: Record<string, string>;
|
|
177
|
+
events?: Record<string, string>;
|
|
178
|
+
slots?: Record<string, "single" | "multiple">;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The component schema of the markdown transport, from the per-run
|
|
183
|
+
* manifest. It is smaller than the manifest: each prop is one short type,
|
|
184
|
+
* and the props that many components share verbatim (the layout props of
|
|
185
|
+
* the containers) are listed once in `commonProps`; a component that takes
|
|
186
|
+
* them is flagged `includesCommonProps`.
|
|
187
|
+
* It has the catalog id, the targets with their descriptions and their
|
|
188
|
+
* component lists, the functions with their parameters, the components
|
|
189
|
+
* with their props, events and slots, and the surfaces of the thread. A surface lists the
|
|
190
|
+
* values that the user set and the operations that the client dropped.
|
|
191
|
+
*
|
|
192
|
+
* @param manifest - The manifest from `buildGenerativeUIManifest`.
|
|
193
|
+
* @param options - `propDescriptions` (default true) writes each prop's
|
|
194
|
+
* description; `commonProps` (default true) lists shared props once.
|
|
195
|
+
* @returns A JSON-ready object.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* const value = JSON.stringify(
|
|
200
|
+
* buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),
|
|
201
|
+
* );
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export function buildCompactGenerativeUISchema(
|
|
205
|
+
manifest: Pick<
|
|
206
|
+
GenerativeUIManifest,
|
|
207
|
+
"catalogId" | "components" | "functions" | "targets" | "surfaces"
|
|
208
|
+
>,
|
|
209
|
+
options: { propDescriptions?: boolean; commonProps?: boolean } = {},
|
|
210
|
+
) {
|
|
211
|
+
const { propDescriptions = true, commonProps = true } = options;
|
|
212
|
+
const components: Record<string, CompactComponent> = {};
|
|
213
|
+
for (const [name, component] of Object.entries(manifest.components)) {
|
|
214
|
+
const events = Object.entries(component.events ?? {}).map(
|
|
215
|
+
([event, definition]) => [
|
|
216
|
+
event,
|
|
217
|
+
definition.description ?? compactType(definition.parameters, 0),
|
|
218
|
+
],
|
|
219
|
+
);
|
|
220
|
+
components[name] = {
|
|
221
|
+
...(component.description ? { description: component.description } : {}),
|
|
222
|
+
props: compactProps(component.props, propDescriptions),
|
|
223
|
+
...(events.length > 0 ? { events: Object.fromEntries(events) } : {}),
|
|
224
|
+
...(component.slots ? { slots: component.slots } : {}),
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
const common = commonProps
|
|
228
|
+
? findCommonProps(
|
|
229
|
+
Object.values(components).map((component) => component.props),
|
|
230
|
+
)
|
|
231
|
+
: undefined;
|
|
232
|
+
if (common) {
|
|
233
|
+
const commonKeys = Object.keys(common);
|
|
234
|
+
for (const component of Object.values(components)) {
|
|
235
|
+
const includesCommon = commonKeys.every(
|
|
236
|
+
(key) => component.props[key] === common[key],
|
|
237
|
+
);
|
|
238
|
+
if (!includesCommon) continue;
|
|
239
|
+
component.includesCommonProps = true;
|
|
240
|
+
component.props = Object.fromEntries(
|
|
241
|
+
Object.entries(component.props).filter(([key]) => !(key in common)),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
// A built-in function carries its short signature in the description of
|
|
246
|
+
// its parameters (see `builtInFunctionManifests`); an app function gets
|
|
247
|
+
// its JSON Schema made short. A function is for the UI unless it says
|
|
248
|
+
// that the agent may call it too, or only the agent.
|
|
249
|
+
const functions = Object.fromEntries(
|
|
250
|
+
Object.entries(manifest.functions).map(([name, fn]) => {
|
|
251
|
+
const signature = fn.parameters.description;
|
|
252
|
+
const hasSignature =
|
|
253
|
+
typeof signature === "string" &&
|
|
254
|
+
signature.startsWith("{") &&
|
|
255
|
+
Object.keys(fn.parameters).length === 2;
|
|
256
|
+
const callers = functionCallers(fn);
|
|
257
|
+
return [
|
|
258
|
+
name,
|
|
259
|
+
{
|
|
260
|
+
...(fn.description ? { description: fn.description } : {}),
|
|
261
|
+
parameters: hasSignature ? signature : compactType(fn.parameters, 0),
|
|
262
|
+
...(callers.agent && GENERATIVE_UI_AGENT_FUNCTION_CALLS
|
|
263
|
+
? { callers: callers.renderer ? "UI or agent" : "agent only" }
|
|
264
|
+
: {}),
|
|
265
|
+
},
|
|
266
|
+
];
|
|
267
|
+
}),
|
|
268
|
+
);
|
|
269
|
+
return {
|
|
270
|
+
catalogId: manifest.catalogId,
|
|
271
|
+
targets: manifest.targets,
|
|
272
|
+
functions,
|
|
273
|
+
...(common ? { commonProps: common } : {}),
|
|
274
|
+
components,
|
|
275
|
+
surfaces: manifest.surfaces,
|
|
276
|
+
};
|
|
277
|
+
}
|