@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
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import goldens from "./fixtures/prompt-goldens.json";
|
|
4
|
+
import { buildGenerativeUIMarkdownGuidelines } from "../a2ui-prompts";
|
|
5
|
+
import { buildGenerativeUIPrompt } from "../generative-ui/prompt";
|
|
6
|
+
import {
|
|
7
|
+
defaultGenerativeUIPromptSpec,
|
|
8
|
+
selectPromptItems,
|
|
9
|
+
} from "../generative-ui/prompt-spec";
|
|
10
|
+
import type {
|
|
11
|
+
GenerativeUIComponentManifest,
|
|
12
|
+
GenerativeUIManifest,
|
|
13
|
+
} from "../generative-ui/types";
|
|
14
|
+
|
|
15
|
+
interface Flags {
|
|
16
|
+
functions: boolean;
|
|
17
|
+
events: boolean;
|
|
18
|
+
templates: boolean;
|
|
19
|
+
checks: boolean;
|
|
20
|
+
surfaces: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const childList = { $ref: "common_types.json#/$defs/ChildList" };
|
|
24
|
+
|
|
25
|
+
/** The manifest the goldens were captured with, for one set of flags. */
|
|
26
|
+
function goldenManifest(f: Flags): GenerativeUIManifest {
|
|
27
|
+
const components: Record<string, GenerativeUIComponentManifest> = {
|
|
28
|
+
Text: {
|
|
29
|
+
description: "Shows text",
|
|
30
|
+
props: {
|
|
31
|
+
type: "object",
|
|
32
|
+
properties: { text: { type: "string", description: "The text" } },
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
Column: {
|
|
36
|
+
description: "A column",
|
|
37
|
+
props: {
|
|
38
|
+
type: "object",
|
|
39
|
+
properties: {
|
|
40
|
+
children: f.templates
|
|
41
|
+
? childList
|
|
42
|
+
: { type: "array", items: { type: "string" } },
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
...(f.templates ? { slots: { children: "multiple" as const } } : {}),
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
if (f.events) {
|
|
49
|
+
components.Button = {
|
|
50
|
+
description: "A button",
|
|
51
|
+
props: { type: "object", properties: { label: { type: "string" } } },
|
|
52
|
+
events: { onClick: { description: "Action" } },
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (f.checks) {
|
|
56
|
+
components.TextField = {
|
|
57
|
+
description: "An input",
|
|
58
|
+
props: {
|
|
59
|
+
type: "object",
|
|
60
|
+
properties: { value: { type: "string" }, checks: { type: "array" } },
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
catalogId: "test/goldens@1",
|
|
66
|
+
components,
|
|
67
|
+
functions: {
|
|
68
|
+
formatCurrency: {
|
|
69
|
+
description: "Formats money",
|
|
70
|
+
parameters: {
|
|
71
|
+
type: "object",
|
|
72
|
+
description: "{value: number, currency: string}",
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
...(f.functions
|
|
76
|
+
? {
|
|
77
|
+
addToCart: {
|
|
78
|
+
description: "Adds to cart",
|
|
79
|
+
parameters: {
|
|
80
|
+
type: "object",
|
|
81
|
+
properties: { id: { type: "string" } },
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
: {}),
|
|
86
|
+
},
|
|
87
|
+
targets: [
|
|
88
|
+
{ id: "chat", kind: "chat" },
|
|
89
|
+
{
|
|
90
|
+
id: "sidebar",
|
|
91
|
+
kind: "inline",
|
|
92
|
+
description: "The side panel",
|
|
93
|
+
components: ["Text"],
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
surfaces: f.surfaces
|
|
97
|
+
? {
|
|
98
|
+
s1: {
|
|
99
|
+
target: "chat",
|
|
100
|
+
dataModel: { name: "Ada" },
|
|
101
|
+
rejected: ["updateComponents: unknown component"],
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
: {},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const sha = (text: string) => createHash("sha256").update(text).digest("hex");
|
|
109
|
+
|
|
110
|
+
describe("prompt builders without a spec", () => {
|
|
111
|
+
it.each(goldens.map((g) => [JSON.stringify(g.flags), g] as const))(
|
|
112
|
+
"render today's text for %s",
|
|
113
|
+
(_name, golden) => {
|
|
114
|
+
const { surfaces: _surfaces, ...features } = golden.flags;
|
|
115
|
+
expect(sha(buildGenerativeUIMarkdownGuidelines(features))).toBe(
|
|
116
|
+
golden.markdown,
|
|
117
|
+
);
|
|
118
|
+
expect(sha(buildGenerativeUIPrompt(goldenManifest(golden.flags)))).toBe(
|
|
119
|
+
golden.tool,
|
|
120
|
+
);
|
|
121
|
+
},
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("prompt builders with a spec", () => {
|
|
126
|
+
const noFeatures = {
|
|
127
|
+
functions: false,
|
|
128
|
+
events: false,
|
|
129
|
+
templates: false,
|
|
130
|
+
checks: false,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
it("render the default spec exactly like no spec", () => {
|
|
134
|
+
const spec = defaultGenerativeUIPromptSpec();
|
|
135
|
+
expect(buildGenerativeUIMarkdownGuidelines(undefined, spec)).toBe(
|
|
136
|
+
buildGenerativeUIMarkdownGuidelines(),
|
|
137
|
+
);
|
|
138
|
+
const manifest = goldenManifest({ ...noFeatures, surfaces: true });
|
|
139
|
+
expect(buildGenerativeUIPrompt(manifest, spec)).toBe(
|
|
140
|
+
buildGenerativeUIPrompt(manifest),
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("use the intro, the paragraph and the rules of the spec", () => {
|
|
145
|
+
const spec = defaultGenerativeUIPromptSpec();
|
|
146
|
+
spec.markdown.intro = "Draw UI in a2ui fences.";
|
|
147
|
+
spec.markdown.whenToShow = "";
|
|
148
|
+
spec.markdown.rules = ["Rule one.", "Rule two."];
|
|
149
|
+
expect(buildGenerativeUIMarkdownGuidelines(noFeatures, spec)).toBe(
|
|
150
|
+
"Draw UI in a2ui fences.\n\nRULES\n- Rule one.\n- Rule two.\n",
|
|
151
|
+
);
|
|
152
|
+
spec.tool.intro = "Use render_ui.";
|
|
153
|
+
spec.tool.rules = ["Only rule."];
|
|
154
|
+
const tool = buildGenerativeUIPrompt(
|
|
155
|
+
goldenManifest({ ...noFeatures, surfaces: false }),
|
|
156
|
+
spec,
|
|
157
|
+
);
|
|
158
|
+
expect(tool).toContain("## Generative UI\nUse render_ui.\n\n");
|
|
159
|
+
expect(tool).toContain("Rules:\n1. Only rule.\n");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("include a block only when all of its features hold", () => {
|
|
163
|
+
const items = [
|
|
164
|
+
"always",
|
|
165
|
+
{ when: ["checks" as const], items: ["checks only"] },
|
|
166
|
+
{
|
|
167
|
+
when: ["events" as const, "noFunctions" as const],
|
|
168
|
+
items: ["events, no functions"],
|
|
169
|
+
},
|
|
170
|
+
];
|
|
171
|
+
const features = {
|
|
172
|
+
functions: false,
|
|
173
|
+
noFunctions: true,
|
|
174
|
+
events: true,
|
|
175
|
+
templates: false,
|
|
176
|
+
checks: false,
|
|
177
|
+
agentCalls: false,
|
|
178
|
+
};
|
|
179
|
+
expect(selectPromptItems(items, features)).toEqual([
|
|
180
|
+
"always",
|
|
181
|
+
"events, no functions",
|
|
182
|
+
]);
|
|
183
|
+
expect(
|
|
184
|
+
selectPromptItems(items, {
|
|
185
|
+
...features,
|
|
186
|
+
checks: true,
|
|
187
|
+
noFunctions: false,
|
|
188
|
+
}),
|
|
189
|
+
).toEqual(["always", "checks only"]);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { defaultGenerativeUIPromptSpec } from "../generative-ui/prompt-spec";
|
|
3
|
+
import type { GenerativeUISchemaSettings } from "../generative-ui/prompt-spec";
|
|
4
|
+
import {
|
|
5
|
+
applyDescriptionOverrides,
|
|
6
|
+
renderGenerativeUIComponentList,
|
|
7
|
+
renderGenerativeUISchema,
|
|
8
|
+
schemaCoverage,
|
|
9
|
+
} from "../generative-ui/schema-render";
|
|
10
|
+
import type { GenerativeUIManifest } from "../generative-ui/types";
|
|
11
|
+
|
|
12
|
+
const manifest: GenerativeUIManifest = {
|
|
13
|
+
catalogId: "test/schema@1",
|
|
14
|
+
components: {
|
|
15
|
+
Text: {
|
|
16
|
+
description: "Shows text",
|
|
17
|
+
props: {
|
|
18
|
+
type: "object",
|
|
19
|
+
properties: {
|
|
20
|
+
text: { type: "string", description: "The text" },
|
|
21
|
+
size: { enum: ["sm", "md"] },
|
|
22
|
+
},
|
|
23
|
+
required: ["text"],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
Button: {
|
|
27
|
+
description: "A button",
|
|
28
|
+
props: { type: "object", properties: { label: { type: "string" } } },
|
|
29
|
+
events: { onClick: { description: "Action" } },
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
functions: {
|
|
33
|
+
formatCurrency: {
|
|
34
|
+
description: "Formats money",
|
|
35
|
+
parameters: { type: "object", description: "{value: number}" },
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
targets: [{ id: "chat", kind: "chat" }],
|
|
39
|
+
surfaces: {},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const settings = (
|
|
43
|
+
notation: GenerativeUISchemaSettings["notation"],
|
|
44
|
+
): GenerativeUISchemaSettings => ({
|
|
45
|
+
notation,
|
|
46
|
+
propDescriptions: true,
|
|
47
|
+
commonProps: true,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("renderGenerativeUISchema", () => {
|
|
51
|
+
it("writes the lines schema that ships by default", () => {
|
|
52
|
+
expect(
|
|
53
|
+
renderGenerativeUISchema(
|
|
54
|
+
manifest,
|
|
55
|
+
defaultGenerativeUIPromptSpec().schema.markdown,
|
|
56
|
+
),
|
|
57
|
+
).toBe(renderGenerativeUISchema(manifest, settings("lines")));
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it.each(["compact", "json-schema", "ts-signature", "lines"] as const)(
|
|
61
|
+
"covers every component and prop in %s notation",
|
|
62
|
+
(notation) => {
|
|
63
|
+
const text = renderGenerativeUISchema(manifest, settings(notation));
|
|
64
|
+
expect(schemaCoverage(manifest, text).missing).toEqual([]);
|
|
65
|
+
},
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
it("writes one signature line per component, with events", () => {
|
|
69
|
+
const text = renderGenerativeUISchema(manifest, settings("ts-signature"));
|
|
70
|
+
expect(text).toContain(
|
|
71
|
+
'- Text(text: string // The text, size?: "sm" | "md"): Shows text',
|
|
72
|
+
);
|
|
73
|
+
expect(text).toContain(" events: onClick(Action)");
|
|
74
|
+
expect(text).toContain("- formatCurrency{value: number}: Formats money");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("separates a function name from parameters that are not an object", () => {
|
|
78
|
+
const withCalls: GenerativeUIManifest = {
|
|
79
|
+
...manifest,
|
|
80
|
+
functions: {
|
|
81
|
+
clearCart: { parameters: { type: "object", properties: {} } },
|
|
82
|
+
setVolume: { parameters: { type: "number" } },
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
for (const notation of ["lines", "ts-signature"] as const) {
|
|
86
|
+
const text = renderGenerativeUISchema(withCalls, settings(notation));
|
|
87
|
+
expect(text).toContain("clearCart{}");
|
|
88
|
+
expect(text).toContain("setVolume(number)");
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("keeps the quotes of enum values that would read as a type or a number", () => {
|
|
93
|
+
const text = renderGenerativeUISchema(
|
|
94
|
+
{
|
|
95
|
+
...manifest,
|
|
96
|
+
components: {
|
|
97
|
+
Icon: {
|
|
98
|
+
props: {
|
|
99
|
+
type: "object",
|
|
100
|
+
properties: {
|
|
101
|
+
size: { anyOf: [{ enum: ["sm", "md"] }, { type: "number" }] },
|
|
102
|
+
format: { enum: ["number", "currency"] },
|
|
103
|
+
level: { enum: ["1", "2"] },
|
|
104
|
+
tone: { enum: ["calm", "loud"] },
|
|
105
|
+
stringFlags: { enum: ["true", "false"] },
|
|
106
|
+
booleanFlags: { enum: [true, false] },
|
|
107
|
+
note: {
|
|
108
|
+
type: "string",
|
|
109
|
+
description: "First line\nIcon: not a component",
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
settings("lines"),
|
|
117
|
+
);
|
|
118
|
+
expect(text).toContain(' size: "sm"|"md"|number');
|
|
119
|
+
expect(text).toContain(' format: "number"|"currency"');
|
|
120
|
+
expect(text).toContain(' level: "1"|"2"');
|
|
121
|
+
expect(text).toContain(" tone: calm|loud");
|
|
122
|
+
expect(text).toContain(' stringFlags: "true"|"false"');
|
|
123
|
+
expect(text).toContain(" booleanFlags: true|false");
|
|
124
|
+
expect(text).toContain(" note: string - First line Icon: not a component");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("writes the status of a deleted or failed surface in signature notation", () => {
|
|
128
|
+
const text = renderGenerativeUISchema(
|
|
129
|
+
{
|
|
130
|
+
...manifest,
|
|
131
|
+
surfaces: {
|
|
132
|
+
old: { target: "chat", status: "deleted" },
|
|
133
|
+
bad: { target: "chat", status: "failed" },
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
settings("ts-signature"),
|
|
137
|
+
);
|
|
138
|
+
expect(text).toContain("- old (target chat) status: deleted");
|
|
139
|
+
expect(text).toContain("- bad (target chat) status: failed");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("writes lines: one line per prop, bare enums, required marked *, events as action props", () => {
|
|
143
|
+
const text = renderGenerativeUISchema(manifest, settings("lines"));
|
|
144
|
+
expect(text).toContain(
|
|
145
|
+
"Text: Shows text\n text*: string - The text\n size: sm|md",
|
|
146
|
+
);
|
|
147
|
+
expect(text).toContain(
|
|
148
|
+
"Button: A button\n label: string\n onClick: action - Action",
|
|
149
|
+
);
|
|
150
|
+
expect(text).toContain("formatCurrency{value: number}: Formats money");
|
|
151
|
+
expect(text).not.toContain('\\"');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("writes slots as componentId and childList props in lines notation", () => {
|
|
155
|
+
const withSlots: GenerativeUIManifest = {
|
|
156
|
+
...manifest,
|
|
157
|
+
components: {
|
|
158
|
+
Card: {
|
|
159
|
+
props: { type: "object", properties: {} },
|
|
160
|
+
slots: { child: "single", children: "multiple" },
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
const text = renderGenerativeUISchema(withSlots, settings("lines"));
|
|
165
|
+
expect(text).toContain("Card\n child: componentId\n children: childList");
|
|
166
|
+
expect(text).not.toContain("slots");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("leaves the prop descriptions out on request", () => {
|
|
170
|
+
for (const notation of [
|
|
171
|
+
"compact",
|
|
172
|
+
"json-schema",
|
|
173
|
+
"ts-signature",
|
|
174
|
+
"lines",
|
|
175
|
+
] as const) {
|
|
176
|
+
const text = renderGenerativeUISchema(manifest, {
|
|
177
|
+
...settings(notation),
|
|
178
|
+
propDescriptions: false,
|
|
179
|
+
});
|
|
180
|
+
expect(text).not.toContain("The text");
|
|
181
|
+
expect(text).toContain("Shows text");
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("renderGenerativeUIComponentList", () => {
|
|
187
|
+
it("writes each notation for the tool guide and covers the catalog", () => {
|
|
188
|
+
for (const notation of [
|
|
189
|
+
"compact",
|
|
190
|
+
"json-schema",
|
|
191
|
+
"ts-signature",
|
|
192
|
+
"lines",
|
|
193
|
+
] as const) {
|
|
194
|
+
const text = renderGenerativeUIComponentList(
|
|
195
|
+
manifest.components,
|
|
196
|
+
settings(notation),
|
|
197
|
+
);
|
|
198
|
+
expect(schemaCoverage(manifest, text).missing).toEqual([]);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe("applyDescriptionOverrides", () => {
|
|
204
|
+
it("replaces only the named descriptions and reports unknown keys", () => {
|
|
205
|
+
const { manifest: next, unknown } = applyDescriptionOverrides(manifest, {
|
|
206
|
+
Text: "Prose in a surface",
|
|
207
|
+
"Text.size": "How big",
|
|
208
|
+
Nope: "x",
|
|
209
|
+
"Text.nope": "y",
|
|
210
|
+
});
|
|
211
|
+
expect(next.components.Text.description).toBe("Prose in a surface");
|
|
212
|
+
expect(next.components.Text.props.properties).toMatchObject({
|
|
213
|
+
size: { description: "How big" },
|
|
214
|
+
text: { description: "The text" },
|
|
215
|
+
});
|
|
216
|
+
expect(next.components.Button).toEqual(manifest.components.Button);
|
|
217
|
+
expect(unknown).toEqual(["Nope", "Text.nope"]);
|
|
218
|
+
// The input is not changed.
|
|
219
|
+
expect(manifest.components.Text.description).toBe("Shows text");
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
describe("applyDescriptionOverrides with inherited keys", () => {
|
|
224
|
+
it("treats __proto__ as unknown and never touches Object.prototype", () => {
|
|
225
|
+
const { unknown } = applyDescriptionOverrides(manifest, {
|
|
226
|
+
__proto__: "polluted",
|
|
227
|
+
"Text.__proto__": "polluted",
|
|
228
|
+
"Text.constructor": "polluted",
|
|
229
|
+
} as Record<string, string>);
|
|
230
|
+
expect(({} as { description?: string }).description).toBeUndefined();
|
|
231
|
+
expect(unknown).toEqual(
|
|
232
|
+
expect.arrayContaining(["Text.__proto__", "Text.constructor"]),
|
|
233
|
+
);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe("schemaCoverage", () => {
|
|
238
|
+
it("reports a prop that the text leaves out", () => {
|
|
239
|
+
const text = renderGenerativeUISchema(
|
|
240
|
+
manifest,
|
|
241
|
+
settings("ts-signature"),
|
|
242
|
+
).replace("size", "");
|
|
243
|
+
expect(schemaCoverage(manifest, text).missing).toEqual(["Text.size"]);
|
|
244
|
+
});
|
|
245
|
+
});
|
package/src/a2ui-envelope.ts
CHANGED
|
@@ -29,7 +29,7 @@ export const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;
|
|
|
29
29
|
/** What the client renders and calls, and how the model must write UI. */
|
|
30
30
|
export interface CopilotKitUIEnvelope {
|
|
31
31
|
/** The A2UI v1.0 renderer capabilities. */
|
|
32
|
-
"
|
|
32
|
+
"1.0": {
|
|
33
33
|
/**
|
|
34
34
|
* The catalog that the client renders comes first, then the catalogs of
|
|
35
35
|
* its `extends`. An operation can name any of them.
|
|
@@ -66,7 +66,7 @@ export interface CopilotKitUIEnvelope {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
type InlineCatalog = NonNullable<
|
|
69
|
-
CopilotKitUIEnvelope["
|
|
69
|
+
CopilotKitUIEnvelope["1.0"]["inlineCatalogs"]
|
|
70
70
|
>[number];
|
|
71
71
|
type Extensions = CopilotKitUIEnvelope["metadata"]["extensions"];
|
|
72
72
|
|
|
@@ -149,7 +149,7 @@ function isInlineCatalog(value: unknown): value is InlineCatalog {
|
|
|
149
149
|
);
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
function isCapabilities(value: unknown): value is CopilotKitUIEnvelope["
|
|
152
|
+
function isCapabilities(value: unknown): value is CopilotKitUIEnvelope["1.0"] {
|
|
153
153
|
return (
|
|
154
154
|
isRecord(value) &&
|
|
155
155
|
Array.isArray(value.supportedCatalogIds) &&
|
|
@@ -222,7 +222,7 @@ function isExtensions(value: unknown): value is Extensions {
|
|
|
222
222
|
function isEnvelope(value: unknown): value is CopilotKitUIEnvelope {
|
|
223
223
|
return (
|
|
224
224
|
isRecord(value) &&
|
|
225
|
-
isCapabilities(value["
|
|
225
|
+
isCapabilities(value["1.0"]) &&
|
|
226
226
|
isRecord(value.metadata) &&
|
|
227
227
|
isExtensions(value.metadata.extensions)
|
|
228
228
|
);
|
|
@@ -282,7 +282,7 @@ export function readCopilotKitUIEnvelope(forwardedProps: unknown) {
|
|
|
282
282
|
* ```
|
|
283
283
|
*/
|
|
284
284
|
export function envelopeToManifest(envelope: CopilotKitUIEnvelope) {
|
|
285
|
-
const { supportedCatalogIds, inlineCatalogs } = envelope["
|
|
285
|
+
const { supportedCatalogIds, inlineCatalogs } = envelope["1.0"];
|
|
286
286
|
const catalogId = supportedCatalogIds[0];
|
|
287
287
|
const catalog = inlineCatalogs?.find(
|
|
288
288
|
(entry) => entry.catalogId === catalogId,
|
package/src/a2ui-prompts.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import { isA2UIBasicFunction } from "./generative-ui/basic-functions";
|
|
2
2
|
import { A2UI_CHILD_LIST_REF } from "./generative-ui/child-refs";
|
|
3
|
-
import { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from "./generative-ui/types";
|
|
4
3
|
import type { GenerativeUIManifest } from "./generative-ui/types";
|
|
4
|
+
import {
|
|
5
|
+
defaultGenerativeUIPromptSpec,
|
|
6
|
+
GUIDE_COMPONENTS,
|
|
7
|
+
generativeUIPromptFeatures,
|
|
8
|
+
selectPromptItems,
|
|
9
|
+
} from "./generative-ui/prompt-spec";
|
|
10
|
+
import type { GenerativeUIPromptSpec } from "./generative-ui/prompt-spec";
|
|
11
|
+
export {
|
|
12
|
+
GENERATIVE_UI_MARKDOWN_WHEN_TO_SHOW,
|
|
13
|
+
GENERATIVE_UI_WHEN_TO_SHOW,
|
|
14
|
+
} from "./generative-ui/prompt-spec";
|
|
5
15
|
/**
|
|
6
16
|
* Default A2UI generation and design guideline prompts.
|
|
7
17
|
*
|
|
@@ -118,14 +128,6 @@ Design principles:
|
|
|
118
128
|
|
|
119
129
|
Use the SAME surfaceId as the main surface. Match action names to button action event names.`;
|
|
120
130
|
|
|
121
|
-
/**
|
|
122
|
-
* When UI helps the user, for both transports: a light nudge to reach for UI
|
|
123
|
-
* where it conveys the answer better than prose, and to stay in prose
|
|
124
|
-
* otherwise.
|
|
125
|
-
*/
|
|
126
|
-
export const GENERATIVE_UI_WHEN_TO_SHOW =
|
|
127
|
-
"Show UI when it conveys the answer better than prose: to illustrate a concept (a diagram, a chart of how something changes), to compare options side by side, to let the user explore or adjust values, or to collect input. A small, focused surface next to your explanation often lands better than a long description. Reply in prose alone for a quick fact, a short answer, or conversation, and never add UI only as decoration.";
|
|
128
|
-
|
|
129
131
|
/** Description of the context entry that carries `GENERATIVE_UI_MARKDOWN_GUIDELINES`. */
|
|
130
132
|
export const GENERATIVE_UI_MARKDOWN_GUIDELINES_DESCRIPTION =
|
|
131
133
|
"Generative UI markdown guide: how to write UI surfaces inside ```a2ui code fences in a normal markdown reply.";
|
|
@@ -154,6 +156,11 @@ export interface GenerativeUIGuideFeatures {
|
|
|
154
156
|
templates: boolean;
|
|
155
157
|
/** A component has a "checks" prop: validation rules on inputs. */
|
|
156
158
|
checks: boolean;
|
|
159
|
+
/**
|
|
160
|
+
* The component names of the catalog. A guide line that names a standard
|
|
161
|
+
* component missing from it is left out. Unset keeps every line.
|
|
162
|
+
*/
|
|
163
|
+
components?: readonly string[];
|
|
157
164
|
}
|
|
158
165
|
|
|
159
166
|
/** Every optional rule group. Use it when the manifest is not known. */
|
|
@@ -190,6 +197,7 @@ export function generativeUIGuideFeatures(
|
|
|
190
197
|
return Object.values(node).some(hasChildList);
|
|
191
198
|
};
|
|
192
199
|
return {
|
|
200
|
+
components: Object.keys(manifest.components),
|
|
193
201
|
// App functions only: the built-in ones are always there and have
|
|
194
202
|
// their own rules.
|
|
195
203
|
functions: Object.keys(manifest.functions).some(
|
|
@@ -212,44 +220,22 @@ export function generativeUIGuideFeatures(
|
|
|
212
220
|
};
|
|
213
221
|
}
|
|
214
222
|
|
|
215
|
-
/**
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
'Write a component as one flat object: {"id":"title","component":"<Name>","text":"Hello"}.',
|
|
232
|
-
'Put the component name in "component" (never "type"). Put props next to it, never inside a "props" object.',
|
|
233
|
-
'A component may take "accessibility":{"label":"<1 to 3 words>","description":"<what it does>"} for screen readers: on the component it describes, when nothing visible names it. Not on every component, and "live":"polite" only on text that changes by itself.',
|
|
234
|
-
'Use only the components, props, events, and slots in the schema. A component with "includesCommonProps" also takes every prop in "commonProps".',
|
|
235
|
-
'Give a prop a literal value, or bind it to the data model with {"@path":"/x"}. Never write {"path":"/x"} as a binding.',
|
|
236
|
-
'A prop can also be a function value that updates with the data: {"@call":"formatCurrency","args":{"value":{"@path":"/total"},"currency":"EUR"}}. Use only the functions under "functions" in the schema. Write a literal when the value never changes.',
|
|
237
|
-
'formatString interpolates ${...}: {"@call":"formatString","args":{"value":"${/qty} × ${formatCurrency(value: /price, currency: \'EUR\')}"}}. Inside ${...}, a path has no quotes, a string has single quotes, and a function takes named args.',
|
|
238
|
-
'${...} is evaluated only in the value of formatString and in the item of series; in other strings it stays literal text. Inside formatString, ${...} does not support operators. For arithmetic use calc, with the values as named args: {"@call":"calc","args":{"expression":"seats * 29 * (annual ? 0.8 : 1)","seats":{"@path":"/seats"},"annual":{"@path":"/annual"}}}. Inside formatString: ${formatCurrency(value: calc(expression: \'seats * 29\', seats: /seats), currency: \'USD\')}. The UI then follows the user\'s input with no round trip.',
|
|
239
|
-
'For a computed list (chart data, template items) use series. Its description under "functions" in the schema gives the rules and an example. Bind the "data" of a Chart or the "items" of a List to it; a template path stays a literal path.',
|
|
240
|
-
'Bind a writable prop to let the user edit it: "value":{"@path":"/form/email"}. The user\'s input goes to that path.',
|
|
241
|
-
'To change data, write updateDataModel with a "path" and a "value": {"version":"v1.0","updateDataModel":{"surfaceId":"plan-picker","path":"/seats","value":12}}. Always give "value".',
|
|
242
|
-
'Set "value":null to delete the key at "path".',
|
|
243
|
-
];
|
|
244
|
-
|
|
245
|
-
/** The last rules of every markdown guideline, after the optional groups. */
|
|
246
|
-
const MARKDOWN_CLOSING_RULES = [
|
|
247
|
-
'The schema lists the surfaces that already exist under "surfaces". "dataModel" holds the values that the user entered: read them, and do not write those paths. "rejected" lists operations that the client dropped: send corrected operations in a new fence for that surfaceId. "status":"deleted" means the surface is gone: never use its id again. "status":"failed" means its createSurface was rejected: send a corrected createSurface with that id.',
|
|
248
|
-
"Never make a surface that is only prose. Write headings, paragraphs, lists of text, intros, and summaries as markdown outside the fences, also between two fences.",
|
|
249
|
-
"Put in a fence only what markdown cannot show: charts, tables, maps, images, badges, inputs, buttons, cards with controls.",
|
|
250
|
-
"Use Card only for an interactive panel (controls, a form, a configurator). Put everything else in a plain layout component.",
|
|
251
|
-
"Never wrap the whole reply in a fence.",
|
|
252
|
-
];
|
|
223
|
+
/**
|
|
224
|
+
* Whether a guide line names only standard components (`GUIDE_COMPONENTS`,
|
|
225
|
+
* also in the plural) that the catalog has. With no catalog, every line fits.
|
|
226
|
+
*/
|
|
227
|
+
function namesOnlyCatalogComponents(catalog: readonly string[] | undefined) {
|
|
228
|
+
if (!catalog) return () => true;
|
|
229
|
+
const has = new Set(catalog);
|
|
230
|
+
return (line: string) =>
|
|
231
|
+
(line.match(/\b[A-Z][A-Za-z0-9]*\b/g) ?? []).every((word) => {
|
|
232
|
+
const name =
|
|
233
|
+
!GUIDE_COMPONENTS.has(word) && word.endsWith("s")
|
|
234
|
+
? word.slice(0, -1)
|
|
235
|
+
: word;
|
|
236
|
+
return !GUIDE_COMPONENTS.has(name) || has.has(name);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
253
239
|
|
|
254
240
|
/**
|
|
255
241
|
* Builds the markdown-transport guideline. The model writes A2UI v1.0
|
|
@@ -258,60 +244,25 @@ const MARKDOWN_CLOSING_RULES = [
|
|
|
258
244
|
* none of them, so the guideline stays small.
|
|
259
245
|
*
|
|
260
246
|
* @param features - The optional rule groups to include. Defaults to all.
|
|
247
|
+
* @param spec - The wording. Defaults to the prompts that ship.
|
|
261
248
|
* @returns The guideline text, one rule per line.
|
|
262
249
|
*/
|
|
263
250
|
export function buildGenerativeUIMarkdownGuidelines(
|
|
264
251
|
features: GenerativeUIGuideFeatures = ALL_GENERATIVE_UI_GUIDE_FEATURES,
|
|
252
|
+
spec: GenerativeUIPromptSpec = defaultGenerativeUIPromptSpec(),
|
|
265
253
|
) {
|
|
266
|
-
const {
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
rules
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
}
|
|
279
|
-
if (checks) {
|
|
280
|
-
rules.push(
|
|
281
|
-
'Validate an input with "checks": [{"condition":{"@call":"email","args":{"value":{"@path":"/form/email"}}},"message":"Enter a valid email"}]. The check functions are required, regex, length, numeric and email; a condition can also be a boolean {"@path"}. A failed check shows the message under the input and disables a Button that has the same checks.',
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
rules.push(
|
|
285
|
-
'A prop typed componentId takes the id of another component, and a childList takes ids ["a","b"]: "child":"card", "children":["a","b"], "tabs":[{"title":"Specs","child":"specs"}].',
|
|
286
|
-
);
|
|
287
|
-
if (templates) {
|
|
288
|
-
rules.push(
|
|
289
|
-
'To repeat a component for each item of a list, give a childList a template: "children":{"componentId":"row","path":"/items"}.',
|
|
290
|
-
'Inside a template, paths are relative to the item ("name", not "/name").',
|
|
291
|
-
'Inside a template, {"@call":"@index"} gives the position of the item, from 0. To count from 1, write {"@call":"@index","args":{"offset":1}}.',
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
rules.push(
|
|
295
|
-
"Updating a component replaces all of its props. Send every prop again.",
|
|
296
|
-
"To change a surface in this reply, write another fence with the same surfaceId. Do not send createSurface again. The surface changes where it already is. A surface in a target can also be changed from a later reply; a chat surface cannot, because the chat is a transcript: a later reply with the same surfaceId makes a new surface.",
|
|
297
|
-
"Use a new surfaceId for a different view. Never use a surfaceId again, also after the surface is deleted.",
|
|
298
|
-
);
|
|
299
|
-
if (functions && GENERATIVE_UI_AGENT_FUNCTION_CALLS) {
|
|
300
|
-
rules.push(
|
|
301
|
-
'To run an app function now, with no click, write a callRendererFunction line in a fence: {"version":"v1.0","callRendererFunction":{"functionCallId":"<unique id>","callFunction":{"@call":"<function>","args":{...}}}}. It needs no surface.',
|
|
302
|
-
'Give every callRendererFunction a new functionCallId. Call only a function from the schema with "callers": "UI or agent" or "agent only"; a function without "callers" is for the UI only. A function with "callers": "agent only" is only for callRendererFunction: do not bind an event to it.',
|
|
303
|
-
'The result of a callRendererFunction comes back to you as a developer message: {"version":"v1.0","rendererFunctionResponse":{"functionCallId":"<id>","value":...}}. If the call failed, it has "error" in place of "value". Then you get a new turn.',
|
|
304
|
-
"Do not guess the result of a callRendererFunction. Wait for the developer message.",
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
rules.push(...MARKDOWN_CLOSING_RULES);
|
|
308
|
-
return `You can show interactive UI in your reply. Write A2UI operations inside a fenced code block tagged a2ui.
|
|
309
|
-
|
|
310
|
-
${GENERATIVE_UI_WHEN_TO_SHOW}
|
|
311
|
-
|
|
312
|
-
RULES
|
|
313
|
-
${rules.map((rule) => `- ${rule}`).join("\n")}
|
|
314
|
-
`;
|
|
254
|
+
const { intro, rules } = spec.markdown;
|
|
255
|
+
const fits = namesOnlyCatalogComponents(features.components);
|
|
256
|
+
const selected = selectPromptItems(
|
|
257
|
+
rules,
|
|
258
|
+
generativeUIPromptFeatures(features),
|
|
259
|
+
).filter(fits);
|
|
260
|
+
const whenToShow = spec.markdown.whenToShow
|
|
261
|
+
.split("\n")
|
|
262
|
+
.filter(fits)
|
|
263
|
+
.join("\n");
|
|
264
|
+
const when = whenToShow ? `${whenToShow}\n\n` : "";
|
|
265
|
+
return `${intro}\n\n${when}RULES\n${selected.map((rule) => `- ${rule}`).join("\n")}\n`;
|
|
315
266
|
}
|
|
316
267
|
|
|
317
268
|
/**
|