@assistant-ui/react-generative-ui 0.0.10 → 0.0.11

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.
@@ -0,0 +1,73 @@
1
+ //#region src/a2ui/types.d.ts
2
+ declare const A2UI_SURFACE_ID: unique symbol;
3
+ type A2uiVersion = "v0.9" | "v1.0";
4
+ interface A2uiTemplateChildren {
5
+ readonly template: {
6
+ readonly componentId: string;
7
+ readonly path: string;
8
+ };
9
+ }
10
+ interface ComponentNode extends Record<string, unknown> {
11
+ readonly id: string;
12
+ readonly component: string;
13
+ readonly children?: readonly string[] | A2uiTemplateChildren;
14
+ }
15
+ interface A2uiCreateSurfaceV09Payload {
16
+ readonly surfaceId: string;
17
+ readonly catalogId?: string;
18
+ readonly theme?: unknown;
19
+ readonly attachDataModel?: unknown;
20
+ }
21
+ interface A2uiCreateSurfaceV10Payload {
22
+ readonly surfaceId: string;
23
+ readonly surfaceProperties?: unknown;
24
+ readonly sendDataModel?: unknown;
25
+ readonly components?: readonly ComponentNode[];
26
+ readonly dataModel?: unknown;
27
+ }
28
+ interface A2uiUpdateComponentsPayload {
29
+ readonly surfaceId: string;
30
+ readonly components: readonly ComponentNode[];
31
+ }
32
+ interface A2uiUpdateDataModelPayload {
33
+ readonly surfaceId: string;
34
+ readonly path?: string;
35
+ readonly contents?: unknown;
36
+ readonly value?: unknown;
37
+ readonly data?: unknown;
38
+ }
39
+ interface A2uiDeleteSurfacePayload {
40
+ readonly surfaceId: string;
41
+ }
42
+ type A2uiCreateSurfaceOperation = {
43
+ readonly version: "v0.9";
44
+ readonly createSurface: A2uiCreateSurfaceV09Payload;
45
+ } | {
46
+ readonly version: "v1.0";
47
+ readonly createSurface: A2uiCreateSurfaceV10Payload;
48
+ };
49
+ interface A2uiUpdateComponentsOperation {
50
+ readonly version: A2uiVersion;
51
+ readonly updateComponents: A2uiUpdateComponentsPayload;
52
+ }
53
+ interface A2uiUpdateDataModelOperation {
54
+ readonly version: A2uiVersion;
55
+ readonly updateDataModel: A2uiUpdateDataModelPayload;
56
+ }
57
+ interface A2uiDeleteSurfaceOperation {
58
+ readonly version: A2uiVersion;
59
+ readonly deleteSurface: A2uiDeleteSurfacePayload;
60
+ }
61
+ type A2uiOperation = A2uiCreateSurfaceOperation | A2uiUpdateComponentsOperation | A2uiUpdateDataModelOperation | A2uiDeleteSurfaceOperation;
62
+ type A2uiSurfaceState = {
63
+ components: Map<string, Record<string, unknown>>;
64
+ dataModel: unknown;
65
+ };
66
+ type A2uiState = ReadonlyMap<string, A2uiSurfaceState>;
67
+ interface A2uiOperationResult {
68
+ readonly state: A2uiState;
69
+ readonly warnings: string[];
70
+ }
71
+ //#endregion
72
+ export { A2UI_SURFACE_ID, A2uiCreateSurfaceOperation, A2uiCreateSurfaceV09Payload, A2uiCreateSurfaceV10Payload, A2uiDeleteSurfaceOperation, A2uiDeleteSurfacePayload, A2uiOperation, A2uiOperationResult, A2uiState, A2uiSurfaceState, A2uiTemplateChildren, A2uiUpdateComponentsOperation, A2uiUpdateComponentsPayload, A2uiUpdateDataModelOperation, A2uiUpdateDataModelPayload, A2uiVersion, ComponentNode };
73
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","names":[],"sources":["../../src/a2ui/types.ts"],"mappings":";cAAa;KAED;UAEK;WACN;aACE;aACA;;;UAII,sBAAsB;WAC5B;WACA;WACA,+BAA+B;;UAGzB;WACN;WACA;WACA;WACA;;UAGM;WACN;WACA;WACA;WACA,sBAAsB;WACtB;;UAGM;WACN;WACA,qBAAqB;;UAGf;WACN;WACA;WACA;WACA;WACA;;UAGM;WACN;;KAGC;WAEG;WACA,eAAe;;WAGf;WACA,eAAe;;UAGb;WACN,SAAS;WACT,kBAAkB;;UAGZ;WACN,SAAS;WACT,iBAAiB;;UAGX;WACN,SAAS;WACT,eAAe;;KAGd,gBACR,6BACA,gCACA,+BACA;KAEQ;EACV,YAAY,YAAY;EACxB;;KAGU,YAAY,oBAAoB;UAE3B;WACN,OAAO;WACP"}
@@ -0,0 +1,6 @@
1
+ //#region src/a2ui/types.ts
2
+ const A2UI_SURFACE_ID = Symbol("a2uiSurfaceId");
3
+ //#endregion
4
+ export { A2UI_SURFACE_ID };
5
+
6
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","names":[],"sources":["../../src/a2ui/types.ts"],"sourcesContent":["export const A2UI_SURFACE_ID = Symbol(\"a2uiSurfaceId\");\n\nexport type A2uiVersion = \"v0.9\" | \"v1.0\";\n\nexport interface A2uiTemplateChildren {\n readonly template: {\n readonly componentId: string;\n readonly path: string;\n };\n}\n\nexport interface ComponentNode extends Record<string, unknown> {\n readonly id: string;\n readonly component: string;\n readonly children?: readonly string[] | A2uiTemplateChildren;\n}\n\nexport interface A2uiCreateSurfaceV09Payload {\n readonly surfaceId: string;\n readonly catalogId?: string;\n readonly theme?: unknown;\n readonly attachDataModel?: unknown;\n}\n\nexport interface A2uiCreateSurfaceV10Payload {\n readonly surfaceId: string;\n readonly surfaceProperties?: unknown;\n readonly sendDataModel?: unknown;\n readonly components?: readonly ComponentNode[];\n readonly dataModel?: unknown;\n}\n\nexport interface A2uiUpdateComponentsPayload {\n readonly surfaceId: string;\n readonly components: readonly ComponentNode[];\n}\n\nexport interface A2uiUpdateDataModelPayload {\n readonly surfaceId: string;\n readonly path?: string;\n readonly contents?: unknown;\n readonly value?: unknown;\n readonly data?: unknown;\n}\n\nexport interface A2uiDeleteSurfacePayload {\n readonly surfaceId: string;\n}\n\nexport type A2uiCreateSurfaceOperation =\n | {\n readonly version: \"v0.9\";\n readonly createSurface: A2uiCreateSurfaceV09Payload;\n }\n | {\n readonly version: \"v1.0\";\n readonly createSurface: A2uiCreateSurfaceV10Payload;\n };\n\nexport interface A2uiUpdateComponentsOperation {\n readonly version: A2uiVersion;\n readonly updateComponents: A2uiUpdateComponentsPayload;\n}\n\nexport interface A2uiUpdateDataModelOperation {\n readonly version: A2uiVersion;\n readonly updateDataModel: A2uiUpdateDataModelPayload;\n}\n\nexport interface A2uiDeleteSurfaceOperation {\n readonly version: A2uiVersion;\n readonly deleteSurface: A2uiDeleteSurfacePayload;\n}\n\nexport type A2uiOperation =\n | A2uiCreateSurfaceOperation\n | A2uiUpdateComponentsOperation\n | A2uiUpdateDataModelOperation\n | A2uiDeleteSurfaceOperation;\n\nexport type A2uiSurfaceState = {\n components: Map<string, Record<string, unknown>>;\n dataModel: unknown;\n};\n\nexport type A2uiState = ReadonlyMap<string, A2uiSurfaceState>;\n\nexport interface A2uiOperationResult {\n readonly state: A2uiState;\n readonly warnings: string[];\n}\n"],"mappings":";AAAA,MAAa,kBAAkB,OAAO,eAAe"}
package/dist/a2ui.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { A2uiCreateSurfaceOperation, A2uiCreateSurfaceV09Payload, A2uiCreateSurfaceV10Payload, A2uiDeleteSurfaceOperation, A2uiDeleteSurfacePayload, A2uiOperation, A2uiOperationResult, A2uiState, A2uiSurfaceState, A2uiTemplateChildren, A2uiUpdateComponentsOperation, A2uiUpdateComponentsPayload, A2uiUpdateDataModelOperation, A2uiUpdateDataModelPayload, A2uiVersion, ComponentNode } from "./a2ui/types.js";
2
+ import { applyA2uiOperations } from "./a2ui/reducer.js";
3
+ import { convertSurfaceToUISpec } from "./a2ui/convert.js";
4
+ export { type A2uiCreateSurfaceOperation, type A2uiCreateSurfaceV09Payload, type A2uiCreateSurfaceV10Payload, type A2uiDeleteSurfaceOperation, type A2uiDeleteSurfacePayload, type A2uiOperation, type A2uiOperationResult, type A2uiState, type A2uiSurfaceState, type A2uiTemplateChildren, type A2uiUpdateComponentsOperation, type A2uiUpdateComponentsPayload, type A2uiUpdateDataModelOperation, type A2uiUpdateDataModelPayload, type A2uiVersion, type ComponentNode, applyA2uiOperations, convertSurfaceToUISpec };
package/dist/a2ui.js ADDED
@@ -0,0 +1,3 @@
1
+ import { applyA2uiOperations } from "./a2ui/reducer.js";
2
+ import { convertSurfaceToUISpec } from "./a2ui/convert.js";
3
+ export { applyA2uiOperations, convertSurfaceToUISpec };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@assistant-ui/react-generative-ui",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "Generative UI components for assistant-ui",
5
5
  "keywords": [
6
6
  "generative-ui",
@@ -28,6 +28,10 @@
28
28
  "types": "./dist/ir.d.ts",
29
29
  "default": "./dist/ir.js"
30
30
  },
31
+ "./a2ui": {
32
+ "types": "./dist/a2ui.d.ts",
33
+ "default": "./dist/a2ui.js"
34
+ },
31
35
  "./slack": {
32
36
  "types": "./dist/slack.d.ts",
33
37
  "default": "./dist/slack.js"
@@ -46,7 +50,7 @@
46
50
  ],
47
51
  "sideEffects": false,
48
52
  "dependencies": {
49
- "assistant-stream": "^0.3.29"
53
+ "assistant-stream": "^0.3.31"
50
54
  },
51
55
  "peerDependencies": {
52
56
  "@assistant-ui/react": "^0.15.0",
@@ -66,7 +70,7 @@
66
70
  "react-dom": "^19.2.8",
67
71
  "vitest": "^4.1.10",
68
72
  "zod": "^4.4.3",
69
- "@assistant-ui/react": "0.15.0",
73
+ "@assistant-ui/react": "0.15.1",
70
74
  "@assistant-ui/x-buildutils": "0.0.20"
71
75
  },
72
76
  "publishConfig": {
@@ -0,0 +1,345 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { UIElement } from "../ir";
3
+ import { convertSurfaceToUISpec } from "./convert";
4
+ import { applyA2uiOperations } from "./reducer";
5
+ import type { A2uiSurfaceState } from "./types";
6
+
7
+ const surfaceFrom = (
8
+ components: readonly Record<string, unknown>[],
9
+ dataModel: unknown = {},
10
+ ): A2uiSurfaceState => ({
11
+ components: new Map(
12
+ components.map((component) => [
13
+ component["id"] as string,
14
+ { ...component },
15
+ ]),
16
+ ),
17
+ dataModel,
18
+ });
19
+
20
+ describe("convertSurfaceToUISpec", () => {
21
+ it("converts an operation-built A2UI surface to the exact canonical UISpec", () => {
22
+ const result = applyA2uiOperations(new Map(), [
23
+ {
24
+ version: "v0.9",
25
+ createSurface: { surfaceId: "main" },
26
+ },
27
+ {
28
+ version: "v0.9",
29
+ updateComponents: {
30
+ surfaceId: "main",
31
+ components: [
32
+ {
33
+ id: "root",
34
+ component: "Column",
35
+ gap: 2,
36
+ children: [
37
+ "heading",
38
+ "body",
39
+ "caption",
40
+ "image",
41
+ "row",
42
+ "card",
43
+ "divider",
44
+ "button",
45
+ "field",
46
+ "checkbox",
47
+ ],
48
+ },
49
+ {
50
+ id: "heading",
51
+ component: "Text",
52
+ variant: "h1",
53
+ text: { path: "/title" },
54
+ },
55
+ {
56
+ id: "body",
57
+ component: "Text",
58
+ text: { path: "/body" },
59
+ },
60
+ {
61
+ id: "caption",
62
+ component: "Text",
63
+ variant: "caption",
64
+ text: "Fine print",
65
+ },
66
+ {
67
+ id: "image",
68
+ component: "Image",
69
+ url: { path: "/imageUrl" },
70
+ altText: "Preview",
71
+ },
72
+ {
73
+ id: "row",
74
+ component: "Row",
75
+ gap: 1,
76
+ align: "center",
77
+ justify: "between",
78
+ children: ["rowText"],
79
+ },
80
+ {
81
+ id: "rowText",
82
+ component: "Text",
83
+ text: "Inside row",
84
+ },
85
+ {
86
+ id: "card",
87
+ component: "Card",
88
+ title: "Details",
89
+ padding: 3,
90
+ },
91
+ { id: "divider", component: "Divider", flush: true },
92
+ {
93
+ id: "button",
94
+ component: "Button",
95
+ label: "Open",
96
+ action: {
97
+ name: "open",
98
+ context: {
99
+ ticketId: { path: "/ticketId" },
100
+ literal: "kept",
101
+ missing: { path: "/missing" },
102
+ },
103
+ },
104
+ },
105
+ {
106
+ id: "field",
107
+ component: "TextField",
108
+ label: "Email",
109
+ placeholder: "you@example.com",
110
+ text: { path: "/form/email" },
111
+ },
112
+ {
113
+ id: "checkbox",
114
+ component: "CheckBox",
115
+ label: "Accepted",
116
+ value: { path: "/form/accepted" },
117
+ },
118
+ ],
119
+ },
120
+ },
121
+ {
122
+ version: "v0.9",
123
+ updateDataModel: {
124
+ surfaceId: "main",
125
+ data: {
126
+ title: "Welcome",
127
+ body: "**Hello**",
128
+ imageUrl: "https://example.com/image.png",
129
+ ticketId: "ticket-1",
130
+ form: { email: "ada@example.com", accepted: true },
131
+ },
132
+ },
133
+ },
134
+ ]);
135
+ const surface = result.state.get("main");
136
+ expect(surface).toBeDefined();
137
+
138
+ const converted = convertSurfaceToUISpec(surface!);
139
+
140
+ expect(converted.warnings).toEqual([]);
141
+ expect(converted.spec).toEqual({
142
+ $type: "Col",
143
+ gap: 2,
144
+ children: [
145
+ { $type: "Header", text: "Welcome" },
146
+ { $type: "Markdown", value: "**Hello**" },
147
+ { $type: "Caption", value: "Fine print" },
148
+ {
149
+ $type: "Image",
150
+ src: "https://example.com/image.png",
151
+ alt: "Preview",
152
+ },
153
+ {
154
+ $type: "Row",
155
+ gap: 1,
156
+ align: "center",
157
+ justify: "between",
158
+ children: [{ $type: "Markdown", value: "Inside row" }],
159
+ },
160
+ { $type: "Card", title: "Details", padding: 3 },
161
+ { $type: "Divider", flush: true },
162
+ {
163
+ $type: "Button",
164
+ label: "Open",
165
+ $action: {
166
+ type: "a2ui:action",
167
+ name: "open",
168
+ surfaceId: "main",
169
+ sourceComponentId: "button",
170
+ context: { ticketId: "ticket-1", literal: "kept" },
171
+ },
172
+ },
173
+ {
174
+ $type: "Input",
175
+ label: "Email",
176
+ placeholder: "you@example.com",
177
+ name: "email",
178
+ },
179
+ {
180
+ $type: "Checkbox",
181
+ label: "Accepted",
182
+ name: "accepted",
183
+ defaultChecked: true,
184
+ },
185
+ ],
186
+ });
187
+ });
188
+
189
+ it("drops an unresolvable bound prop without throwing", () => {
190
+ const surface = surfaceFrom([
191
+ {
192
+ id: "root",
193
+ component: "Text",
194
+ text: { path: "/missing" },
195
+ },
196
+ ]);
197
+
198
+ expect(convertSurfaceToUISpec(surface)).toEqual({
199
+ spec: { $type: "Markdown" },
200
+ warnings: [],
201
+ });
202
+ });
203
+
204
+ it("expands template children relative to each bound item", () => {
205
+ const surface = surfaceFrom(
206
+ [
207
+ {
208
+ id: "root",
209
+ component: "Column",
210
+ children: {
211
+ template: { componentId: "item", path: "/items" },
212
+ },
213
+ },
214
+ {
215
+ id: "item",
216
+ component: "Text",
217
+ text: { path: "/name" },
218
+ },
219
+ ],
220
+ { items: [{ name: "one" }, { name: "two" }] },
221
+ );
222
+
223
+ expect(convertSurfaceToUISpec(surface)).toEqual({
224
+ spec: {
225
+ $type: "ListView",
226
+ children: [
227
+ {
228
+ $type: "ListViewItem",
229
+ children: { $type: "Markdown", value: "one" },
230
+ },
231
+ {
232
+ $type: "ListViewItem",
233
+ children: { $type: "Markdown", value: "two" },
234
+ },
235
+ ],
236
+ },
237
+ warnings: [],
238
+ });
239
+ });
240
+
241
+ it("caps template expansion at 100 items", () => {
242
+ const surface = surfaceFrom(
243
+ [
244
+ {
245
+ id: "root",
246
+ component: "Column",
247
+ children: {
248
+ template: { componentId: "item", path: "/items" },
249
+ },
250
+ },
251
+ {
252
+ id: "item",
253
+ component: "Text",
254
+ text: { path: "/" },
255
+ },
256
+ ],
257
+ { items: Array.from({ length: 101 }, (_, index) => `item-${index}`) },
258
+ );
259
+
260
+ const result = convertSurfaceToUISpec(surface);
261
+ expect((result.spec as UIElement).children).toHaveLength(100);
262
+ expect(result.warnings).toContain(
263
+ "A2UI template expansion was capped at 100 items.",
264
+ );
265
+ });
266
+
267
+ it("truncates a component cycle", () => {
268
+ const surface = surfaceFrom([
269
+ {
270
+ id: "root",
271
+ component: "Row",
272
+ children: ["root"],
273
+ },
274
+ ]);
275
+
276
+ expect(convertSurfaceToUISpec(surface)).toEqual({
277
+ spec: { $type: "Row" },
278
+ warnings: ['A2UI component cycle detected at "root".'],
279
+ });
280
+ });
281
+
282
+ it("truncates component resolution at depth 32", () => {
283
+ const components = Array.from({ length: 34 }, (_, index) => ({
284
+ id: index === 0 ? "root" : `node-${index}`,
285
+ component: "Row",
286
+ ...(index < 33
287
+ ? { children: [index === 0 ? "node-1" : `node-${index + 1}`] }
288
+ : {}),
289
+ }));
290
+ const result = convertSurfaceToUISpec(surfaceFrom(components));
291
+ let node = result.spec as UIElement;
292
+ let count = 1;
293
+ while (Array.isArray(node.children) && node.children.length > 0) {
294
+ node = node.children[0] as UIElement;
295
+ count++;
296
+ }
297
+
298
+ expect(count).toBe(32);
299
+ expect(result.warnings).toContain("A2UI depth cap of 32 was reached.");
300
+ });
301
+
302
+ it("enforces the total emitted node budget", () => {
303
+ const childCount = 5001;
304
+ const components = [
305
+ {
306
+ id: "root",
307
+ component: "Row",
308
+ children: Array.from(
309
+ { length: childCount },
310
+ (_, index) => `child-${index}`,
311
+ ),
312
+ },
313
+ ...Array.from({ length: childCount }, (_, index) => ({
314
+ id: `child-${index}`,
315
+ component: "Divider",
316
+ })),
317
+ ];
318
+ const result = convertSurfaceToUISpec(surfaceFrom(components));
319
+
320
+ expect((result.spec as UIElement).children).toHaveLength(4999);
321
+ expect(result.warnings).toContain("A2UI node budget of 5000 was reached.");
322
+ });
323
+
324
+ it("skips an unknown component with a warning naming it", () => {
325
+ const result = convertSurfaceToUISpec(
326
+ surfaceFrom([{ id: "root", component: "VideoPlayer" }]),
327
+ );
328
+
329
+ expect(result.spec).toBeNull();
330
+ expect(result.warnings).toEqual([
331
+ 'Unknown A2UI component "VideoPlayer" was skipped.',
332
+ ]);
333
+ });
334
+
335
+ it("returns null with a warning when root is missing", () => {
336
+ expect(
337
+ convertSurfaceToUISpec(
338
+ surfaceFrom([{ id: "other", component: "Divider" }]),
339
+ ),
340
+ ).toEqual({
341
+ spec: null,
342
+ warnings: ['A2UI root component "root" was not found.'],
343
+ });
344
+ });
345
+ });