@copilotkit/shared 1.80.0 → 1.81.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 +154 -0
- package/dist/a2ui-envelope.cjs.map +1 -0
- package/dist/a2ui-envelope.d.cts +101 -0
- package/dist/a2ui-envelope.d.cts.map +1 -0
- package/dist/a2ui-envelope.d.mts +101 -0
- package/dist/a2ui-envelope.d.mts.map +1 -0
- package/dist/a2ui-envelope.mjs +151 -0
- package/dist/a2ui-envelope.mjs.map +1 -0
- package/dist/a2ui-prompts.cjs +139 -0
- package/dist/a2ui-prompts.cjs.map +1 -1
- package/dist/a2ui-prompts.d.cts +60 -1
- package/dist/a2ui-prompts.d.cts.map +1 -1
- package/dist/a2ui-prompts.d.mts +60 -1
- package/dist/a2ui-prompts.d.mts.map +1 -1
- package/dist/a2ui-prompts.mjs +134 -1
- package/dist/a2ui-prompts.mjs.map +1 -1
- package/dist/generative-ui/basic-functions.cjs +177 -0
- package/dist/generative-ui/basic-functions.cjs.map +1 -0
- package/dist/generative-ui/basic-functions.d.cts +108 -0
- package/dist/generative-ui/basic-functions.d.cts.map +1 -0
- package/dist/generative-ui/basic-functions.d.mts +108 -0
- package/dist/generative-ui/basic-functions.d.mts.map +1 -0
- package/dist/generative-ui/basic-functions.mjs +167 -0
- package/dist/generative-ui/basic-functions.mjs.map +1 -0
- package/dist/generative-ui/child-refs.cjs +161 -0
- package/dist/generative-ui/child-refs.cjs.map +1 -0
- package/dist/generative-ui/child-refs.d.cts +68 -0
- package/dist/generative-ui/child-refs.d.cts.map +1 -0
- package/dist/generative-ui/child-refs.d.mts +68 -0
- package/dist/generative-ui/child-refs.d.mts.map +1 -0
- package/dist/generative-ui/child-refs.mjs +152 -0
- package/dist/generative-ui/child-refs.mjs.map +1 -0
- package/dist/generative-ui/is-record.cjs +10 -0
- package/dist/generative-ui/is-record.cjs.map +1 -0
- package/dist/generative-ui/is-record.mjs +9 -0
- package/dist/generative-ui/is-record.mjs.map +1 -0
- package/dist/generative-ui/prompt.cjs +91 -0
- package/dist/generative-ui/prompt.cjs.map +1 -0
- package/dist/generative-ui/prompt.d.cts +13 -0
- package/dist/generative-ui/prompt.d.cts.map +1 -0
- package/dist/generative-ui/prompt.d.mts +13 -0
- package/dist/generative-ui/prompt.d.mts.map +1 -0
- package/dist/generative-ui/prompt.mjs +90 -0
- package/dist/generative-ui/prompt.mjs.map +1 -0
- package/dist/generative-ui/render-ui-args.cjs +160 -0
- package/dist/generative-ui/render-ui-args.cjs.map +1 -0
- package/dist/generative-ui/render-ui-args.d.cts +40 -0
- package/dist/generative-ui/render-ui-args.d.cts.map +1 -0
- package/dist/generative-ui/render-ui-args.d.mts +40 -0
- package/dist/generative-ui/render-ui-args.d.mts.map +1 -0
- package/dist/generative-ui/render-ui-args.mjs +159 -0
- package/dist/generative-ui/render-ui-args.mjs.map +1 -0
- package/dist/generative-ui/types.cjs +101 -0
- package/dist/generative-ui/types.cjs.map +1 -0
- package/dist/generative-ui/types.d.cts +270 -0
- package/dist/generative-ui/types.d.cts.map +1 -0
- package/dist/generative-ui/types.d.mts +270 -0
- package/dist/generative-ui/types.d.mts.map +1 -0
- package/dist/generative-ui/types.mjs +86 -0
- package/dist/generative-ui/types.mjs.map +1 -0
- package/dist/generative-ui/validate-operation.cjs +405 -0
- package/dist/generative-ui/validate-operation.cjs.map +1 -0
- package/dist/generative-ui/validate-operation.d.cts +86 -0
- package/dist/generative-ui/validate-operation.d.cts.map +1 -0
- package/dist/generative-ui/validate-operation.d.mts +86 -0
- package/dist/generative-ui/validate-operation.d.mts.map +1 -0
- package/dist/generative-ui/validate-operation.mjs +399 -0
- package/dist/generative-ui/validate-operation.mjs.map +1 -0
- package/dist/index.cjs +63 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +9 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +9 -2
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1421 -20
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/utils/dashboard-url.cjs +19 -0
- package/dist/utils/dashboard-url.cjs.map +1 -0
- package/dist/utils/dashboard-url.mjs +18 -0
- package/dist/utils/dashboard-url.mjs.map +1 -0
- package/dist/utils/index.cjs +2 -1
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.mts.map +1 -1
- package/dist/utils/index.mjs +2 -1
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/inspector-learning.cjs +10 -7
- package/dist/utils/inspector-learning.cjs.map +1 -1
- package/dist/utils/inspector-learning.d.cts.map +1 -1
- package/dist/utils/inspector-learning.d.mts.map +1 -1
- package/dist/utils/inspector-learning.mjs +11 -7
- package/dist/utils/inspector-learning.mjs.map +1 -1
- package/dist/utils/inspector-metadata.cjs +2 -3
- package/dist/utils/inspector-metadata.cjs.map +1 -1
- package/dist/utils/inspector-metadata.d.cts.map +1 -1
- package/dist/utils/inspector-metadata.d.mts.map +1 -1
- package/dist/utils/inspector-metadata.mjs +3 -3
- package/dist/utils/inspector-metadata.mjs.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/a2ui-envelope.test.ts +434 -0
- package/src/__tests__/a2ui-prompts.test.ts +362 -0
- package/src/a2ui-envelope.ts +309 -0
- package/src/a2ui-prompts.ts +207 -0
- package/src/generative-ui/__tests__/render-ui-args.test.ts +175 -0
- package/src/generative-ui/__tests__/validate-operation.test.ts +977 -0
- package/src/generative-ui/basic-functions.ts +282 -0
- package/src/generative-ui/child-refs.ts +210 -0
- package/src/generative-ui/index.ts +6 -0
- package/src/generative-ui/is-record.ts +4 -0
- package/src/generative-ui/prompt.ts +174 -0
- package/src/generative-ui/render-ui-args.ts +199 -0
- package/src/generative-ui/types.ts +314 -0
- package/src/generative-ui/validate-operation.ts +723 -0
- package/src/index.ts +15 -0
- package/src/utils/dashboard-url.ts +17 -0
- package/src/utils/index.ts +7 -1
- package/src/utils/inspector-learning.test.ts +58 -0
- package/src/utils/inspector-learning.ts +26 -5
- package/src/utils/inspector-metadata.test.ts +2 -1
- package/src/utils/inspector-metadata.ts +3 -13
- package/src/utils.test.ts +71 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
buildGenerativeUIMarkdownGuidelines,
|
|
4
|
+
generativeUIGuideFeatures,
|
|
5
|
+
GENERATIVE_UI_MARKDOWN_GUIDELINES,
|
|
6
|
+
GENERATIVE_UI_MARKDOWN_GUIDELINES_DESCRIPTION,
|
|
7
|
+
GENERATIVE_UI_MARKDOWN_SCHEMA_CONTEXT_DESCRIPTION,
|
|
8
|
+
GENERATIVE_UI_WHEN_TO_SHOW,
|
|
9
|
+
} from "../a2ui-prompts";
|
|
10
|
+
import { COPILOTKIT_FUNCTIONS } from "../generative-ui/basic-functions";
|
|
11
|
+
import { buildGenerativeUIPrompt } from "../generative-ui/prompt";
|
|
12
|
+
import { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from "../generative-ui/types";
|
|
13
|
+
|
|
14
|
+
const guide = GENERATIVE_UI_MARKDOWN_GUIDELINES;
|
|
15
|
+
|
|
16
|
+
describe("GENERATIVE_UI_MARKDOWN_GUIDELINES", () => {
|
|
17
|
+
it("tells the model to write fences, and never to call a tool", () => {
|
|
18
|
+
expect(guide).toContain("```a2ui");
|
|
19
|
+
expect(guide).not.toContain("render_ui");
|
|
20
|
+
expect(guide).not.toContain("render_a2ui");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("places a fence in the chat or in a named target", () => {
|
|
24
|
+
expect(guide).toContain(
|
|
25
|
+
"Open a fence with ```a2ui to show the surface in the chat, at the place of the fence.",
|
|
26
|
+
);
|
|
27
|
+
expect(guide).toContain(
|
|
28
|
+
"Open a fence with ```a2ui target=<id> to show the surface in a named target.",
|
|
29
|
+
);
|
|
30
|
+
expect(guide).toContain(
|
|
31
|
+
'When a target has "components", its surfaces can use only those components.',
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("asks for one JSON object per line, each with the v1.0 version and no catalogId", () => {
|
|
36
|
+
expect(guide).toContain("write one JSON object per line and nothing else");
|
|
37
|
+
expect(guide).toContain('Give every operation "version":"v1.0".');
|
|
38
|
+
expect(guide).not.toContain("v0.9");
|
|
39
|
+
expect(guide).not.toContain("catalogId");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("starts a surface with one createSurface that holds the data and the root, then streams the children", () => {
|
|
43
|
+
expect(guide).toContain(
|
|
44
|
+
'Start a surface with one createSurface. Put all the initial data in "dataModel" and the "root" component in "components".',
|
|
45
|
+
);
|
|
46
|
+
expect(guide).toContain(
|
|
47
|
+
'{"version":"v1.0","createSurface":{"surfaceId":"plan-picker","dataModel":{ ...initial data... },"components":[{"id":"root"',
|
|
48
|
+
);
|
|
49
|
+
expect(guide.indexOf('"createSurface"')).toBeLessThan(
|
|
50
|
+
guide.indexOf('"updateComponents"'),
|
|
51
|
+
);
|
|
52
|
+
expect(guide).toContain('Keep "dataModel" flat');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("keeps components flat, which live models got wrong", () => {
|
|
56
|
+
expect(guide).toContain(
|
|
57
|
+
'Put the component name in "component" (never "type")',
|
|
58
|
+
);
|
|
59
|
+
expect(guide).toContain('never inside a "props" object');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("describes @path bindings for data and for writable inputs, and forbids the path form", () => {
|
|
63
|
+
expect(guide).toContain('bind it to the data model with {"@path":"/x"}');
|
|
64
|
+
expect(guide).toContain('Never write {"path":"/x"} as a binding.');
|
|
65
|
+
expect(guide).toContain(
|
|
66
|
+
'Bind a writable prop to let the user edit it: "value":{"@path":"/form/email"}.',
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("requires a value in updateDataModel and deletes with null", () => {
|
|
71
|
+
expect(guide).toContain('Always give "value".');
|
|
72
|
+
expect(guide).toContain('Set "value":null to delete the key at "path".');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("describes events with userMessage, function calls with @call, and that an unbound button does nothing", () => {
|
|
76
|
+
expect(guide).toContain(
|
|
77
|
+
'{"event":{"name":"<name>","userMessage":"<text>","context":{...}}}',
|
|
78
|
+
);
|
|
79
|
+
expect(guide).toContain(
|
|
80
|
+
'{"functionCall":{"@call":"<function>","args":{...}}}',
|
|
81
|
+
);
|
|
82
|
+
expect(guide).toContain(
|
|
83
|
+
'{"event":{"name":"choose","userMessage":"Show me the Team plan"}}',
|
|
84
|
+
);
|
|
85
|
+
expect(guide).toContain('"context":{"email":{"@path":"/form/email"}}');
|
|
86
|
+
expect(guide).toContain('Never a flat {"event":"<name>"}.');
|
|
87
|
+
expect(guide).toContain(
|
|
88
|
+
"A button or other event prop without an event or functionCall does nothing.",
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("describes function values and the formatString grammar", () => {
|
|
93
|
+
expect(guide).toContain(
|
|
94
|
+
'A prop can also be a function value that updates with the data: {"@call":"formatCurrency"',
|
|
95
|
+
);
|
|
96
|
+
expect(guide).toContain(
|
|
97
|
+
'Use only the functions under "functions" in the schema.',
|
|
98
|
+
);
|
|
99
|
+
expect(guide).toContain("formatString interpolates ${...}");
|
|
100
|
+
expect(guide).toContain(
|
|
101
|
+
"Inside formatString, ${...} does not support operators",
|
|
102
|
+
);
|
|
103
|
+
expect(guide).toContain(
|
|
104
|
+
'{"@call":"calc","args":{"expression":"seats * 29 * (annual ? 0.8 : 1)"',
|
|
105
|
+
);
|
|
106
|
+
expect(guide).toContain(
|
|
107
|
+
'use series. Its description under "functions" in the schema',
|
|
108
|
+
);
|
|
109
|
+
expect(guide).toContain('the "items" of a List');
|
|
110
|
+
expect(guide).not.toContain("template path of a list");
|
|
111
|
+
expect(guide).toContain('{"@call":"@index","args":{"offset":1}}');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("describes checks with the validator functions", () => {
|
|
115
|
+
expect(guide).toContain('Validate an input with "checks"');
|
|
116
|
+
expect(guide).toContain("required, regex, length, numeric and email");
|
|
117
|
+
expect(guide).toContain("disables a Button that has the same checks");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("describes slots with child ids and with a template of relative paths", () => {
|
|
121
|
+
expect(guide).toContain('"tabs":[{"title":"Specs","child":"specs"}]');
|
|
122
|
+
expect(guide).toContain('"children":["a","b"]');
|
|
123
|
+
expect(guide).toContain('"children":{"componentId":"row","path":"/items"}');
|
|
124
|
+
expect(guide).toContain(
|
|
125
|
+
'paths are relative to the item ("name", not "/name")',
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("updates a surface from a later fence with the same surfaceId in the reply, and a target from a later reply", () => {
|
|
130
|
+
expect(guide).toContain(
|
|
131
|
+
"To change a surface in this reply, write another fence with the same surfaceId.",
|
|
132
|
+
);
|
|
133
|
+
expect(guide).toContain(
|
|
134
|
+
"A surface in a target can also be changed from a later reply; a chat surface cannot",
|
|
135
|
+
);
|
|
136
|
+
expect(guide).toContain(
|
|
137
|
+
"Never use a surfaceId again, also after the surface is deleted.",
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it.skipIf(GENERATIVE_UI_AGENT_FUNCTION_CALLS)(
|
|
142
|
+
"does not mention callRendererFunction while the agent path is off",
|
|
143
|
+
() => {
|
|
144
|
+
expect(guide).not.toContain("callRendererFunction");
|
|
145
|
+
expect(guide).not.toContain("rendererFunctionResponse");
|
|
146
|
+
},
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
it.skipIf(!GENERATIVE_UI_AGENT_FUNCTION_CALLS)(
|
|
150
|
+
"describes callRendererFunction, its unique id, and the developer-message response",
|
|
151
|
+
() => {
|
|
152
|
+
expect(guide).toContain(
|
|
153
|
+
'{"version":"v1.0","callRendererFunction":{"functionCallId":"<unique id>","callFunction":{"@call":"<function>","args":{...}}}}',
|
|
154
|
+
);
|
|
155
|
+
expect(guide).toContain(
|
|
156
|
+
"Give every callRendererFunction a new functionCallId.",
|
|
157
|
+
);
|
|
158
|
+
expect(guide).toContain('"callers": "UI or agent" or "agent only"');
|
|
159
|
+
expect(guide).toContain(
|
|
160
|
+
'{"version":"v1.0","rendererFunctionResponse":{"functionCallId":"<id>","value":...}}',
|
|
161
|
+
);
|
|
162
|
+
expect(guide).toContain("Then you get a new turn.");
|
|
163
|
+
},
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
it("keeps prose outside the fences and Card for interactive panels", () => {
|
|
167
|
+
expect(guide).toContain("Never make a surface that is only prose.");
|
|
168
|
+
expect(guide).toContain("Use Card only for an interactive panel");
|
|
169
|
+
expect(guide).toContain("Never wrap the whole reply in a fence.");
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe("markdown context entry descriptions", () => {
|
|
174
|
+
it("names the schema entry differently from the a2ui-middleware schema key", () => {
|
|
175
|
+
// The LangGraph and Strands adapters move an entry with this exact
|
|
176
|
+
// description out of the prompt and into the render_a2ui tool state.
|
|
177
|
+
expect(GENERATIVE_UI_MARKDOWN_SCHEMA_CONTEXT_DESCRIPTION).not.toBe(
|
|
178
|
+
"A2UI Component Schema — available components for generating UI surfaces. Use these component names and properties when creating A2UI operations.",
|
|
179
|
+
);
|
|
180
|
+
expect(GENERATIVE_UI_MARKDOWN_SCHEMA_CONTEXT_DESCRIPTION).not.toBe(
|
|
181
|
+
GENERATIVE_UI_MARKDOWN_GUIDELINES_DESCRIPTION,
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("generativeUIGuideFeatures", () => {
|
|
187
|
+
const props = { type: "object", properties: {} };
|
|
188
|
+
|
|
189
|
+
it("finds mounted functions, event props, and multiple slots", () => {
|
|
190
|
+
expect(
|
|
191
|
+
generativeUIGuideFeatures({
|
|
192
|
+
components: {
|
|
193
|
+
List: { props, slots: { items: "multiple" } },
|
|
194
|
+
Button: { props, events: { onClick: { parameters: props } } },
|
|
195
|
+
},
|
|
196
|
+
functions: { save: { parameters: props } },
|
|
197
|
+
}),
|
|
198
|
+
).toEqual({
|
|
199
|
+
functions: true,
|
|
200
|
+
events: true,
|
|
201
|
+
templates: true,
|
|
202
|
+
checks: false,
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("finds none when the components have no events or multiple slots and no function is mounted", () => {
|
|
207
|
+
expect(
|
|
208
|
+
generativeUIGuideFeatures({
|
|
209
|
+
components: {
|
|
210
|
+
Card: { props, slots: { child: "single" }, events: {} },
|
|
211
|
+
},
|
|
212
|
+
functions: {},
|
|
213
|
+
}),
|
|
214
|
+
).toEqual({
|
|
215
|
+
functions: false,
|
|
216
|
+
events: false,
|
|
217
|
+
templates: false,
|
|
218
|
+
checks: false,
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
describe("buildGenerativeUIMarkdownGuidelines", () => {
|
|
224
|
+
const none = {
|
|
225
|
+
functions: false,
|
|
226
|
+
events: false,
|
|
227
|
+
templates: false,
|
|
228
|
+
checks: false,
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
it("has every rule group by default", () => {
|
|
232
|
+
expect(buildGenerativeUIMarkdownGuidelines()).toBe(
|
|
233
|
+
GENERATIVE_UI_MARKDOWN_GUIDELINES,
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("leaves out the function, event, and template rules when they are off", () => {
|
|
238
|
+
const small = buildGenerativeUIMarkdownGuidelines(none);
|
|
239
|
+
expect(small).not.toContain("callRendererFunction");
|
|
240
|
+
expect(small).not.toContain("functionCall");
|
|
241
|
+
expect(small).not.toContain("userMessage");
|
|
242
|
+
expect(small).not.toContain("@index");
|
|
243
|
+
expect(small).not.toContain('"checks"');
|
|
244
|
+
expect(small).toContain("Never wrap the whole reply in a fence.");
|
|
245
|
+
expect(small.length).toBeLessThan(GENERATIVE_UI_MARKDOWN_GUIDELINES.length);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("keeps events and openUrl, but no app functions, when none is mounted", () => {
|
|
249
|
+
const text = buildGenerativeUIMarkdownGuidelines({ ...none, events: true });
|
|
250
|
+
expect(text).toContain("userMessage");
|
|
251
|
+
expect(text).toContain('"@call":"openUrl"');
|
|
252
|
+
expect(text).not.toContain("an app function from the schema");
|
|
253
|
+
expect(text).not.toContain("callRendererFunction");
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("adds the checks rule for a component with a checks prop", () => {
|
|
257
|
+
const text = buildGenerativeUIMarkdownGuidelines({ ...none, checks: true });
|
|
258
|
+
expect(text).toContain('Validate an input with "checks"');
|
|
259
|
+
expect(
|
|
260
|
+
generativeUIGuideFeatures({
|
|
261
|
+
components: {
|
|
262
|
+
TextField: {
|
|
263
|
+
props: {
|
|
264
|
+
type: "object",
|
|
265
|
+
properties: { checks: { type: "array" } },
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
functions: {},
|
|
270
|
+
}).checks,
|
|
271
|
+
).toBe(true);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("adds the template and @index rules for a multiple slot", () => {
|
|
275
|
+
const text = buildGenerativeUIMarkdownGuidelines({
|
|
276
|
+
...none,
|
|
277
|
+
templates: true,
|
|
278
|
+
});
|
|
279
|
+
expect(text).toContain("@index");
|
|
280
|
+
expect(text).toContain("componentId");
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
describe("GENERATIVE_UI_WHEN_TO_SHOW", () => {
|
|
285
|
+
it("encourages UI where it conveys the answer better, and prose otherwise, on both transports", () => {
|
|
286
|
+
expect(GENERATIVE_UI_WHEN_TO_SHOW).toContain("illustrate a concept");
|
|
287
|
+
expect(GENERATIVE_UI_WHEN_TO_SHOW).toContain("prose alone");
|
|
288
|
+
expect(GENERATIVE_UI_WHEN_TO_SHOW).toContain("decoration");
|
|
289
|
+
expect(buildGenerativeUIMarkdownGuidelines()).toContain(
|
|
290
|
+
GENERATIVE_UI_WHEN_TO_SHOW,
|
|
291
|
+
);
|
|
292
|
+
expect(
|
|
293
|
+
buildGenerativeUIPrompt({
|
|
294
|
+
catalogId: "test",
|
|
295
|
+
targets: [],
|
|
296
|
+
components: {},
|
|
297
|
+
functions: {},
|
|
298
|
+
surfaces: {},
|
|
299
|
+
}),
|
|
300
|
+
).toContain(GENERATIVE_UI_WHEN_TO_SHOW);
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
describe("computed data guidance", () => {
|
|
305
|
+
const series = COPILOTKIT_FUNCTIONS.series;
|
|
306
|
+
const markdownGuide = buildGenerativeUIMarkdownGuidelines();
|
|
307
|
+
const toolPrompt = buildGenerativeUIPrompt({
|
|
308
|
+
catalogId: "test",
|
|
309
|
+
targets: [],
|
|
310
|
+
components: {},
|
|
311
|
+
functions: {
|
|
312
|
+
series: {
|
|
313
|
+
description: series.description,
|
|
314
|
+
parameters: { type: "object", description: series.parameters },
|
|
315
|
+
returnType: series.returnType,
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
surfaces: {},
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("the series description carries the rules and the example", () => {
|
|
322
|
+
expect(series.description).toContain('"month":"${i}"');
|
|
323
|
+
expect(series.description).toContain(
|
|
324
|
+
'"monthlyCost":{"@path":"/monthlyCost"}',
|
|
325
|
+
);
|
|
326
|
+
expect(series.description).toContain(
|
|
327
|
+
"whole-expression string retains the result type",
|
|
328
|
+
);
|
|
329
|
+
expect(series.description).toContain("resolve before the series loop");
|
|
330
|
+
expect(series.description).not.toContain('"variable":"m"');
|
|
331
|
+
expect(series.description).not.toContain("WORKAROUND");
|
|
332
|
+
expect(series.description).not.toContain("a2ui-agent-sdk");
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it.each([
|
|
336
|
+
["markdown", markdownGuide],
|
|
337
|
+
["tool", toolPrompt],
|
|
338
|
+
])(
|
|
339
|
+
"points to series and scopes ${...} in the %s transport",
|
|
340
|
+
(_transport, prompt) => {
|
|
341
|
+
expect(prompt).toContain(
|
|
342
|
+
"For a computed list (chart data, template items) use series. Its description under",
|
|
343
|
+
);
|
|
344
|
+
expect(prompt).toContain(
|
|
345
|
+
"${...} is evaluated only in the value of formatString and in the item of series",
|
|
346
|
+
);
|
|
347
|
+
expect(prompt).toContain(
|
|
348
|
+
"Inside formatString, ${...} does not support operators",
|
|
349
|
+
);
|
|
350
|
+
expect(prompt).not.toContain('"variable":"m"');
|
|
351
|
+
expect(prompt).not.toContain("There are no operators in ${...}");
|
|
352
|
+
},
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
it("gives the series description once, in the function list of the tool prompt", () => {
|
|
356
|
+
expect(toolPrompt.split(series.description)).toHaveLength(2);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("leaves the series description to the schema in the markdown guide", () => {
|
|
360
|
+
expect(markdownGuide).not.toContain(series.description);
|
|
361
|
+
});
|
|
362
|
+
});
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The generative UI envelope that the client sends on every run as
|
|
3
|
+
* `forwardedProps.copilotkit.ui`. It has the A2UI v1.0 renderer capabilities
|
|
4
|
+
* shape, so a backend that speaks A2UI v1.0 can read the catalog as it is.
|
|
5
|
+
* CopilotKit data goes in `metadata.extensions`, under `copilotkit_` keys.
|
|
6
|
+
* The runtime reads it to pick the transport and to build the guide.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
GENERATIVE_UI_MAX_REJECTION_LENGTH,
|
|
10
|
+
GENERATIVE_UI_MAX_SUMMARY_REJECTIONS,
|
|
11
|
+
GENERATIVE_UI_MAX_TARGET_COMPONENTS,
|
|
12
|
+
} from "./generative-ui/types";
|
|
13
|
+
import type {
|
|
14
|
+
A2UIActionMessage,
|
|
15
|
+
GenerativeUIComponentManifest,
|
|
16
|
+
GenerativeUIFunctionManifest,
|
|
17
|
+
GenerativeUIManifest,
|
|
18
|
+
GenerativeUISurfaceSummary,
|
|
19
|
+
GenerativeUITarget,
|
|
20
|
+
} from "./generative-ui/types";
|
|
21
|
+
import { isRecord } from "./generative-ui/is-record";
|
|
22
|
+
|
|
23
|
+
/** Top-level `forwardedProps` key that holds the CopilotKit run properties. */
|
|
24
|
+
export const COPILOTKIT_FORWARDED_PROPS_KEY = "copilotkit";
|
|
25
|
+
|
|
26
|
+
/** Version of the envelope shape (not of the UI protocol that it carries). */
|
|
27
|
+
export const COPILOTKIT_UI_ENVELOPE_FORMAT = 3;
|
|
28
|
+
|
|
29
|
+
/** What the client renders and calls, and how the model must write UI. */
|
|
30
|
+
export interface CopilotKitUIEnvelope {
|
|
31
|
+
/** The A2UI v1.0 renderer capabilities. */
|
|
32
|
+
"v1.0": {
|
|
33
|
+
/**
|
|
34
|
+
* The catalog that the client renders comes first, then the catalogs of
|
|
35
|
+
* its `extends`. An operation can name any of them.
|
|
36
|
+
*/
|
|
37
|
+
supportedCatalogIds: string[];
|
|
38
|
+
inlineCatalogs?: Array<{
|
|
39
|
+
catalogId: string;
|
|
40
|
+
protocolVersion: "1.0";
|
|
41
|
+
/** Not there when the client is set up with `includeCatalog: false`. */
|
|
42
|
+
components?: Record<string, GenerativeUIComponentManifest>;
|
|
43
|
+
/** The app functions that are mounted now. */
|
|
44
|
+
functions: Record<string, GenerativeUIFunctionManifest>;
|
|
45
|
+
/** The markdown guideline. Only the markdown transport sends it. */
|
|
46
|
+
instructions?: string;
|
|
47
|
+
}>;
|
|
48
|
+
};
|
|
49
|
+
metadata: {
|
|
50
|
+
extensions: {
|
|
51
|
+
/** Envelope shape version. */
|
|
52
|
+
copilotkit_format: typeof COPILOTKIT_UI_ENVELOPE_FORMAT;
|
|
53
|
+
/** "tool": the model calls the render tool. "markdown": the model writes ```a2ui fences. */
|
|
54
|
+
copilotkit_transport: "tool" | "markdown";
|
|
55
|
+
copilotkit_targets: GenerativeUIManifest["targets"];
|
|
56
|
+
copilotkit_surfaces: GenerativeUIManifest["surfaces"];
|
|
57
|
+
/** True when the client also sent the guide as context entries on this run. */
|
|
58
|
+
copilotkit_contextSent: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* The A2UI action that started this run, when a user event did: the
|
|
61
|
+
* structured form of the user message that the event added.
|
|
62
|
+
*/
|
|
63
|
+
copilotkit_action?: A2UIActionMessage;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type InlineCatalog = NonNullable<
|
|
69
|
+
CopilotKitUIEnvelope["v1.0"]["inlineCatalogs"]
|
|
70
|
+
>[number];
|
|
71
|
+
type Extensions = CopilotKitUIEnvelope["metadata"]["extensions"];
|
|
72
|
+
|
|
73
|
+
function isOptionalString(value: unknown) {
|
|
74
|
+
return value === undefined || typeof value === "string";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function isNonEmptyString(value: unknown): value is string {
|
|
78
|
+
return typeof value === "string" && value !== "";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function isRecordOf<T>(
|
|
82
|
+
value: unknown,
|
|
83
|
+
isEntry: (entry: unknown) => entry is T,
|
|
84
|
+
): value is Record<string, T> {
|
|
85
|
+
return isRecord(value) && Object.values(value).every(isEntry);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A function, or an event of a component: a description and a parameter schema. */
|
|
89
|
+
function isSchemaEntry(value: unknown): value is GenerativeUIFunctionManifest {
|
|
90
|
+
return (
|
|
91
|
+
isRecord(value) &&
|
|
92
|
+
isRecord(value.parameters) &&
|
|
93
|
+
isOptionalString(value.description)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function isSlotKind(value: unknown): value is "single" | "multiple" {
|
|
98
|
+
return value === "single" || value === "multiple";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* `validateOperation` reads `properties` and `required` of a props schema,
|
|
103
|
+
* so those two must have the right shape when they are there.
|
|
104
|
+
*/
|
|
105
|
+
function isPropsSchema(value: unknown): value is Record<string, unknown> {
|
|
106
|
+
return (
|
|
107
|
+
isRecord(value) &&
|
|
108
|
+
(value.properties === undefined || isRecord(value.properties)) &&
|
|
109
|
+
(value.required === undefined ||
|
|
110
|
+
(Array.isArray(value.required) &&
|
|
111
|
+
value.required.every((name) => typeof name === "string")))
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function isComponentManifest(
|
|
116
|
+
value: unknown,
|
|
117
|
+
): value is GenerativeUIComponentManifest {
|
|
118
|
+
if (
|
|
119
|
+
!isRecord(value) ||
|
|
120
|
+
!isPropsSchema(value.props) ||
|
|
121
|
+
!isOptionalString(value.description) ||
|
|
122
|
+
(value.events !== undefined && !isRecordOf(value.events, isSchemaEntry)) ||
|
|
123
|
+
(value.slots !== undefined && !isRecordOf(value.slots, isSlotKind))
|
|
124
|
+
)
|
|
125
|
+
return false;
|
|
126
|
+
const order = value.parameterOrder;
|
|
127
|
+
if (order === undefined) return true;
|
|
128
|
+
const keys = new Set([
|
|
129
|
+
...Object.keys(value.props.properties ?? {}),
|
|
130
|
+
...Object.keys(value.slots ?? {}),
|
|
131
|
+
...Object.keys(value.events ?? {}),
|
|
132
|
+
]);
|
|
133
|
+
return (
|
|
134
|
+
Array.isArray(order) &&
|
|
135
|
+
new Set(order).size === order.length &&
|
|
136
|
+
order.every((key) => typeof key === "string" && keys.has(key))
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function isInlineCatalog(value: unknown): value is InlineCatalog {
|
|
141
|
+
return (
|
|
142
|
+
isRecord(value) &&
|
|
143
|
+
isNonEmptyString(value.catalogId) &&
|
|
144
|
+
value.protocolVersion === "1.0" &&
|
|
145
|
+
(value.components === undefined ||
|
|
146
|
+
isRecordOf(value.components, isComponentManifest)) &&
|
|
147
|
+
isRecordOf(value.functions, isSchemaEntry) &&
|
|
148
|
+
isOptionalString(value.instructions)
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function isCapabilities(value: unknown): value is CopilotKitUIEnvelope["v1.0"] {
|
|
153
|
+
return (
|
|
154
|
+
isRecord(value) &&
|
|
155
|
+
Array.isArray(value.supportedCatalogIds) &&
|
|
156
|
+
value.supportedCatalogIds.length > 0 &&
|
|
157
|
+
value.supportedCatalogIds.every(isNonEmptyString) &&
|
|
158
|
+
(value.inlineCatalogs === undefined ||
|
|
159
|
+
(Array.isArray(value.inlineCatalogs) &&
|
|
160
|
+
value.inlineCatalogs.every(isInlineCatalog)))
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** A short list of component names. */
|
|
165
|
+
function isComponentNameList(value: unknown): value is string[] {
|
|
166
|
+
return (
|
|
167
|
+
Array.isArray(value) &&
|
|
168
|
+
value.length <= GENERATIVE_UI_MAX_TARGET_COMPONENTS &&
|
|
169
|
+
value.every(isNonEmptyString)
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function isTarget(value: unknown): value is GenerativeUITarget {
|
|
174
|
+
return (
|
|
175
|
+
isRecord(value) &&
|
|
176
|
+
isNonEmptyString(value.id) &&
|
|
177
|
+
(value.kind === "chat" || value.kind === "inline") &&
|
|
178
|
+
isOptionalString(value.description) &&
|
|
179
|
+
(value.components === undefined || isComponentNameList(value.components))
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A short list of short strings. The summary limits keep the guide small. */
|
|
184
|
+
function isRejectionList(value: unknown): value is string[] {
|
|
185
|
+
return (
|
|
186
|
+
Array.isArray(value) &&
|
|
187
|
+
value.length <= GENERATIVE_UI_MAX_SUMMARY_REJECTIONS &&
|
|
188
|
+
value.every(
|
|
189
|
+
(entry) =>
|
|
190
|
+
typeof entry === "string" &&
|
|
191
|
+
entry.length <= GENERATIVE_UI_MAX_REJECTION_LENGTH,
|
|
192
|
+
)
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function isSurfaceEntry(value: unknown): value is GenerativeUISurfaceSummary {
|
|
197
|
+
return (
|
|
198
|
+
isRecord(value) &&
|
|
199
|
+
isNonEmptyString(value.target) &&
|
|
200
|
+
(value.dataModel === undefined || isRecord(value.dataModel)) &&
|
|
201
|
+
(value.rejected === undefined || isRejectionList(value.rejected)) &&
|
|
202
|
+
(value.status === undefined ||
|
|
203
|
+
value.status === "deleted" ||
|
|
204
|
+
value.status === "failed")
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Checks the `copilotkit_` keys only. Other extensions can be there too. */
|
|
209
|
+
function isExtensions(value: unknown): value is Extensions {
|
|
210
|
+
return (
|
|
211
|
+
isRecord(value) &&
|
|
212
|
+
value.copilotkit_format === COPILOTKIT_UI_ENVELOPE_FORMAT &&
|
|
213
|
+
(value.copilotkit_transport === "tool" ||
|
|
214
|
+
value.copilotkit_transport === "markdown") &&
|
|
215
|
+
Array.isArray(value.copilotkit_targets) &&
|
|
216
|
+
value.copilotkit_targets.every(isTarget) &&
|
|
217
|
+
isRecordOf(value.copilotkit_surfaces, isSurfaceEntry) &&
|
|
218
|
+
typeof value.copilotkit_contextSent === "boolean"
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function isEnvelope(value: unknown): value is CopilotKitUIEnvelope {
|
|
223
|
+
return (
|
|
224
|
+
isRecord(value) &&
|
|
225
|
+
isCapabilities(value["v1.0"]) &&
|
|
226
|
+
isRecord(value.metadata) &&
|
|
227
|
+
isExtensions(value.metadata.extensions)
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Reads the generative UI envelope from the `forwardedProps` of a run.
|
|
233
|
+
*
|
|
234
|
+
* The envelope comes from the client, so it is not trusted: every field is
|
|
235
|
+
* checked. Returns `undefined` when the envelope is not there, or when it is
|
|
236
|
+
* malformed (for example a wrong `copilotkit_format` or
|
|
237
|
+
* `copilotkit_transport`, or no catalog id).
|
|
238
|
+
*
|
|
239
|
+
* @param forwardedProps - The `forwardedProps` of the run input.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
244
|
+
* if (envelope?.metadata.extensions.copilotkit_transport === "markdown") {
|
|
245
|
+
* // The model writes ```a2ui fences. Add no render tool.
|
|
246
|
+
* }
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
export function readCopilotKitUIEnvelope(forwardedProps: unknown) {
|
|
250
|
+
if (!isRecord(forwardedProps)) return undefined;
|
|
251
|
+
const namespace = forwardedProps[COPILOTKIT_FORWARDED_PROPS_KEY];
|
|
252
|
+
if (!isRecord(namespace)) return undefined;
|
|
253
|
+
const envelope = namespace.ui;
|
|
254
|
+
return isEnvelope(envelope) ? envelope : undefined;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Turns an envelope into the manifest that the runtime and the prompts use.
|
|
259
|
+
*
|
|
260
|
+
* The catalog is the first of `supportedCatalogIds`, and `catalogIds` lists
|
|
261
|
+
* all of them. The components, functions, and instructions come from the
|
|
262
|
+
* inline catalog of the first id.
|
|
263
|
+
* With no such inline catalog, there are no components and no functions.
|
|
264
|
+
*
|
|
265
|
+
* The result also has:
|
|
266
|
+
* - `transport` and `contextSent`, from the extensions.
|
|
267
|
+
* - `instructions`, when the inline catalog has them.
|
|
268
|
+
* - `hasComponents`: false when the client left the component schemas out.
|
|
269
|
+
* The runtime cannot check operations without them.
|
|
270
|
+
* - `action`: the A2UI action message of the user event that started the
|
|
271
|
+
* run, when one did.
|
|
272
|
+
*
|
|
273
|
+
* @param envelope - An envelope from `readCopilotKitUIEnvelope`.
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```ts
|
|
277
|
+
* const envelope = readCopilotKitUIEnvelope(input.forwardedProps);
|
|
278
|
+
* const manifest = envelope && envelopeToManifest(envelope);
|
|
279
|
+
* if (manifest?.transport === "tool" && manifest.hasComponents) {
|
|
280
|
+
* // Add the render tool and validate operations against manifest.components.
|
|
281
|
+
* }
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
export function envelopeToManifest(envelope: CopilotKitUIEnvelope) {
|
|
285
|
+
const { supportedCatalogIds, inlineCatalogs } = envelope["v1.0"];
|
|
286
|
+
const catalogId = supportedCatalogIds[0];
|
|
287
|
+
const catalog = inlineCatalogs?.find(
|
|
288
|
+
(entry) => entry.catalogId === catalogId,
|
|
289
|
+
);
|
|
290
|
+
const extensions = envelope.metadata.extensions;
|
|
291
|
+
const manifest: GenerativeUIManifest = {
|
|
292
|
+
catalogId,
|
|
293
|
+
catalogIds: supportedCatalogIds,
|
|
294
|
+
components: catalog?.components ?? {},
|
|
295
|
+
functions: catalog?.functions ?? {},
|
|
296
|
+
targets: extensions.copilotkit_targets,
|
|
297
|
+
surfaces: extensions.copilotkit_surfaces,
|
|
298
|
+
};
|
|
299
|
+
return {
|
|
300
|
+
...manifest,
|
|
301
|
+
transport: extensions.copilotkit_transport,
|
|
302
|
+
contextSent: extensions.copilotkit_contextSent,
|
|
303
|
+
instructions: catalog?.instructions,
|
|
304
|
+
hasComponents: catalog?.components !== undefined,
|
|
305
|
+
...(extensions.copilotkit_action === undefined
|
|
306
|
+
? {}
|
|
307
|
+
: { action: extensions.copilotkit_action }),
|
|
308
|
+
};
|
|
309
|
+
}
|