@assistant-ui/react-generative-ui 0.0.17 → 0.0.18
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/convert.d.ts.map +1 -1
- package/dist/a2ui/convert.js +11 -2
- package/dist/a2ui/convert.js.map +1 -1
- package/dist/a2ui/reducer.d.ts.map +1 -1
- package/dist/a2ui/reducer.js +63 -10
- package/dist/a2ui/reducer.js.map +1 -1
- package/dist/buildPresentParameters.js +4 -3
- package/dist/buildPresentParameters.js.map +1 -1
- package/dist/renderGenerativeUI.js +1 -1
- package/dist/renderGenerativeUI.js.map +1 -1
- package/dist/vocabulary/alert.d.ts +1 -1
- package/dist/vocabulary/alert.d.ts.map +1 -1
- package/dist/vocabulary/alert.js +21 -16
- package/dist/vocabulary/alert.js.map +1 -1
- package/dist/vocabulary/data.d.ts.map +1 -1
- package/dist/vocabulary/data.js +20 -12
- package/dist/vocabulary/data.js.map +1 -1
- package/dist/vocabulary/fact.d.ts.map +1 -1
- package/dist/vocabulary/fact.js +3 -2
- package/dist/vocabulary/fact.js.map +1 -1
- package/dist/vocabulary/interactive.d.ts +6 -5
- package/dist/vocabulary/interactive.d.ts.map +1 -1
- package/dist/vocabulary/interactive.js +28 -23
- package/dist/vocabulary/interactive.js.map +1 -1
- package/dist/vocabulary/layout.d.ts.map +1 -1
- package/dist/vocabulary/layout.js +7 -5
- package/dist/vocabulary/layout.js.map +1 -1
- package/dist/vocabulary/text.d.ts.map +1 -1
- package/dist/vocabulary/text.js +4 -3
- package/dist/vocabulary/text.js.map +1 -1
- package/dist/vocabulary/toTextContent.d.ts +5 -0
- package/dist/vocabulary/toTextContent.d.ts.map +1 -0
- package/dist/vocabulary/toTextContent.js +6 -0
- package/dist/vocabulary/toTextContent.js.map +1 -0
- package/package.json +4 -4
- package/src/a2ui/convert.test.ts +36 -0
- package/src/a2ui/convert.ts +21 -2
- package/src/a2ui/reducer.test.ts +223 -0
- package/src/a2ui/reducer.ts +76 -12
- package/src/buildPresentParameters.ts +7 -4
- package/src/renderGenerativeUI.test.tsx +57 -0
- package/src/renderGenerativeUI.tsx +3 -1
- package/src/vocabulary/alert.test.tsx +10 -0
- package/src/vocabulary/alert.tsx +16 -7
- package/src/vocabulary/data.test.tsx +40 -0
- package/src/vocabulary/data.tsx +47 -26
- package/src/vocabulary/fact.test.tsx +12 -0
- package/src/vocabulary/fact.tsx +3 -2
- package/src/vocabulary/interactive.test.tsx +41 -0
- package/src/vocabulary/interactive.tsx +42 -28
- package/src/vocabulary/layout.test.tsx +16 -0
- package/src/vocabulary/layout.tsx +8 -4
- package/src/vocabulary/text.test.tsx +29 -0
- package/src/vocabulary/text.tsx +4 -3
- package/src/vocabulary/toTextContent.ts +2 -0
package/src/a2ui/convert.ts
CHANGED
|
@@ -30,6 +30,23 @@ const isPlainObject = (value: unknown): value is Record<string, unknown> => {
|
|
|
30
30
|
return prototype === Object.prototype || prototype === null;
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
+
const setOwnProperty = (
|
|
34
|
+
target: Record<string, unknown>,
|
|
35
|
+
key: string,
|
|
36
|
+
value: unknown,
|
|
37
|
+
) => {
|
|
38
|
+
if (key === "__proto__") {
|
|
39
|
+
Object.defineProperty(target, key, {
|
|
40
|
+
value,
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
});
|
|
45
|
+
} else {
|
|
46
|
+
target[key] = value;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
33
50
|
const isBinding = (value: unknown): value is { readonly path: string } =>
|
|
34
51
|
isPlainObject(value) &&
|
|
35
52
|
Object.keys(value).length === 1 &&
|
|
@@ -90,7 +107,9 @@ const materialize = (value: unknown, source: unknown): unknown => {
|
|
|
90
107
|
const result: Record<string, unknown> = {};
|
|
91
108
|
for (const [key, entry] of Object.entries(value)) {
|
|
92
109
|
const resolved = materialize(entry, source);
|
|
93
|
-
if (resolved !== undefined)
|
|
110
|
+
if (resolved !== undefined) {
|
|
111
|
+
setOwnProperty(result, key, resolved);
|
|
112
|
+
}
|
|
94
113
|
}
|
|
95
114
|
return result;
|
|
96
115
|
};
|
|
@@ -419,7 +438,7 @@ function convertComponent(
|
|
|
419
438
|
for (const [key, value] of Object.entries(node)) {
|
|
420
439
|
if (key === "id" || key === "component" || key === "children") continue;
|
|
421
440
|
const resolved = materialize(value, dataSource);
|
|
422
|
-
if (resolved !== undefined) props
|
|
441
|
+
if (resolved !== undefined) setOwnProperty(props, key, resolved);
|
|
423
442
|
}
|
|
424
443
|
const mapped = mappedProps(node, props, dataSource, context);
|
|
425
444
|
if (!mapped) return null;
|
package/src/a2ui/reducer.test.ts
CHANGED
|
@@ -82,6 +82,229 @@ describe("applyA2uiOperations", () => {
|
|
|
82
82
|
});
|
|
83
83
|
});
|
|
84
84
|
|
|
85
|
+
it.each(["10001", "4294967294"])(
|
|
86
|
+
"rejects array pointer index %s beyond the auto-vivification limit",
|
|
87
|
+
(index) => {
|
|
88
|
+
const dataModel = { items: [] };
|
|
89
|
+
const state: A2uiState = new Map([
|
|
90
|
+
["main", { components: new Map(), dataModel }],
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
const result = applyA2uiOperations(state, [
|
|
94
|
+
{
|
|
95
|
+
version: "v1.0",
|
|
96
|
+
updateDataModel: {
|
|
97
|
+
surfaceId: "main",
|
|
98
|
+
path: `/items/${index}`,
|
|
99
|
+
value: "unsafe",
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
expect(result.warnings).toEqual([
|
|
105
|
+
"Operation at index 0 has an invalid JSON Pointer path.",
|
|
106
|
+
]);
|
|
107
|
+
expect(result.state.get("main")?.dataModel).toBe(dataModel);
|
|
108
|
+
expect(dataModel.items).toHaveLength(0);
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
it("allows bounded expansion, existing large indices, and appends", () => {
|
|
113
|
+
const items = new Array<string | undefined>(10_002);
|
|
114
|
+
items[10_001] = "existing";
|
|
115
|
+
const state: A2uiState = new Map([
|
|
116
|
+
["main", { components: new Map(), dataModel: { bounded: [], items } }],
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
const result = applyA2uiOperations(state, [
|
|
120
|
+
{
|
|
121
|
+
version: "v1.0",
|
|
122
|
+
updateDataModel: {
|
|
123
|
+
surfaceId: "main",
|
|
124
|
+
path: "/bounded/10000",
|
|
125
|
+
value: "limit",
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
version: "v1.0",
|
|
130
|
+
updateDataModel: {
|
|
131
|
+
surfaceId: "main",
|
|
132
|
+
path: "/items/10001",
|
|
133
|
+
value: "updated",
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
version: "v1.0",
|
|
138
|
+
updateDataModel: {
|
|
139
|
+
surfaceId: "main",
|
|
140
|
+
path: "/items/-",
|
|
141
|
+
value: "appended",
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
]);
|
|
145
|
+
|
|
146
|
+
const resultModel = result.state.get("main")!.dataModel as {
|
|
147
|
+
bounded: string[];
|
|
148
|
+
items: string[];
|
|
149
|
+
};
|
|
150
|
+
expect(result.warnings).toEqual([]);
|
|
151
|
+
expect(resultModel.bounded).toHaveLength(10_001);
|
|
152
|
+
expect(resultModel.bounded[10_000]).toBe("limit");
|
|
153
|
+
expect(resultModel.items).toHaveLength(10_003);
|
|
154
|
+
expect(resultModel.items[10_001]).toBe("updated");
|
|
155
|
+
expect(resultModel.items[10_002]).toBe("appended");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("stores prototype-named JSON Pointer segments as own properties", () => {
|
|
159
|
+
const state: A2uiState = new Map([
|
|
160
|
+
["main", { components: new Map(), dataModel: {} }],
|
|
161
|
+
]);
|
|
162
|
+
|
|
163
|
+
const result = applyA2uiOperations(state, [
|
|
164
|
+
{
|
|
165
|
+
version: "v1.0",
|
|
166
|
+
updateDataModel: {
|
|
167
|
+
surfaceId: "main",
|
|
168
|
+
path: "/__proto__/admin",
|
|
169
|
+
value: true,
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
]);
|
|
173
|
+
|
|
174
|
+
const resultModel = result.state.get("main")?.dataModel as Record<
|
|
175
|
+
string,
|
|
176
|
+
unknown
|
|
177
|
+
>;
|
|
178
|
+
expect(result.warnings).toEqual([]);
|
|
179
|
+
expect(Object.getPrototypeOf(resultModel)).toBe(Object.prototype);
|
|
180
|
+
expect(Object.hasOwn(resultModel, "__proto__")).toBe(true);
|
|
181
|
+
expect(resultModel["__proto__"]).toEqual({ admin: true });
|
|
182
|
+
expect(JSON.stringify(resultModel)).toBe('{"__proto__":{"admin":true}}');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("deletes object properties updated to null", () => {
|
|
186
|
+
const dataModel = {
|
|
187
|
+
profile: { name: "Ada", role: "admin" },
|
|
188
|
+
tags: ["first", "second"],
|
|
189
|
+
};
|
|
190
|
+
const state: A2uiState = new Map([
|
|
191
|
+
["main", { components: new Map(), dataModel }],
|
|
192
|
+
]);
|
|
193
|
+
|
|
194
|
+
const result = applyA2uiOperations(state, [
|
|
195
|
+
{
|
|
196
|
+
version: "v1.0",
|
|
197
|
+
updateDataModel: {
|
|
198
|
+
surfaceId: "main",
|
|
199
|
+
path: "/profile/name",
|
|
200
|
+
value: null,
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
version: "v1.0",
|
|
205
|
+
updateDataModel: {
|
|
206
|
+
surfaceId: "main",
|
|
207
|
+
path: "/tags/1",
|
|
208
|
+
value: null,
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
]);
|
|
212
|
+
|
|
213
|
+
expect(result.warnings).toEqual([]);
|
|
214
|
+
const resultModel = result.state.get("main")?.dataModel as typeof dataModel;
|
|
215
|
+
expect(resultModel.profile).toEqual({ role: "admin" });
|
|
216
|
+
expect(resultModel.tags).toHaveLength(2);
|
|
217
|
+
expect(resultModel.tags[0]).toBe("first");
|
|
218
|
+
expect(Object.prototype.hasOwnProperty.call(resultModel.tags, 1)).toBe(
|
|
219
|
+
false,
|
|
220
|
+
);
|
|
221
|
+
expect(dataModel).toEqual({
|
|
222
|
+
profile: { name: "Ada", role: "admin" },
|
|
223
|
+
tags: ["first", "second"],
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("preserves null values in v0.9 data model updates", () => {
|
|
228
|
+
const state: A2uiState = new Map([
|
|
229
|
+
[
|
|
230
|
+
"main",
|
|
231
|
+
{ components: new Map(), dataModel: { profile: { name: "Ada" } } },
|
|
232
|
+
],
|
|
233
|
+
]);
|
|
234
|
+
|
|
235
|
+
const result = applyA2uiOperations(state, [
|
|
236
|
+
{
|
|
237
|
+
version: "v0.9",
|
|
238
|
+
updateDataModel: {
|
|
239
|
+
surfaceId: "main",
|
|
240
|
+
path: "/profile/name",
|
|
241
|
+
value: null,
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
]);
|
|
245
|
+
|
|
246
|
+
expect(result.warnings).toEqual([]);
|
|
247
|
+
expect(result.state.get("main")?.dataModel).toEqual({
|
|
248
|
+
profile: { name: null },
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("does not create missing paths for null updates", () => {
|
|
253
|
+
const dataModel = { profile: { name: "Ada" }, tags: ["first", "second"] };
|
|
254
|
+
const state: A2uiState = new Map([
|
|
255
|
+
["main", { components: new Map(), dataModel }],
|
|
256
|
+
]);
|
|
257
|
+
|
|
258
|
+
const result = applyA2uiOperations(state, [
|
|
259
|
+
{
|
|
260
|
+
version: "v1.0",
|
|
261
|
+
updateDataModel: {
|
|
262
|
+
surfaceId: "main",
|
|
263
|
+
path: "/missing/value",
|
|
264
|
+
value: null,
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
version: "v1.0",
|
|
269
|
+
updateDataModel: {
|
|
270
|
+
surfaceId: "main",
|
|
271
|
+
path: "/tags/-",
|
|
272
|
+
value: null,
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
version: "v1.0",
|
|
277
|
+
updateDataModel: {
|
|
278
|
+
surfaceId: "main",
|
|
279
|
+
path: "/tags/7",
|
|
280
|
+
value: null,
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
]);
|
|
284
|
+
|
|
285
|
+
expect(result.warnings).toEqual([]);
|
|
286
|
+
expect(result.state.get("main")?.dataModel).toBe(dataModel);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("allows null to replace the data model root", () => {
|
|
290
|
+
const state: A2uiState = new Map([
|
|
291
|
+
[
|
|
292
|
+
"main",
|
|
293
|
+
{ components: new Map(), dataModel: { profile: { name: "Ada" } } },
|
|
294
|
+
],
|
|
295
|
+
]);
|
|
296
|
+
|
|
297
|
+
const result = applyA2uiOperations(state, [
|
|
298
|
+
{
|
|
299
|
+
version: "v1.0",
|
|
300
|
+
updateDataModel: { surfaceId: "main", value: null },
|
|
301
|
+
},
|
|
302
|
+
]);
|
|
303
|
+
|
|
304
|
+
expect(result.warnings).toEqual([]);
|
|
305
|
+
expect(result.state.get("main")?.dataModel).toBeNull();
|
|
306
|
+
});
|
|
307
|
+
|
|
85
308
|
it("clones the touched surface and component map", () => {
|
|
86
309
|
const components = new Map<string, Record<string, unknown>>([
|
|
87
310
|
["root", { id: "root", component: "Divider" }],
|
package/src/a2ui/reducer.ts
CHANGED
|
@@ -11,6 +11,9 @@ const OPERATION_KEYS = new Set([
|
|
|
11
11
|
"updateDataModel",
|
|
12
12
|
"deleteSurface",
|
|
13
13
|
]);
|
|
14
|
+
// This defensive ceiling is well above the renderer's displayed-item limit.
|
|
15
|
+
const MAX_AUTO_VIVIFY_ARRAY_INDEX = 10_000;
|
|
16
|
+
const INVALID_POINTER = Symbol("invalidPointer");
|
|
14
17
|
|
|
15
18
|
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
16
19
|
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -104,30 +107,82 @@ const setAtPointer = (
|
|
|
104
107
|
model: unknown,
|
|
105
108
|
path: string,
|
|
106
109
|
value: unknown,
|
|
110
|
+
nullDeletes: boolean,
|
|
107
111
|
): { readonly ok: boolean; readonly value: unknown } => {
|
|
108
112
|
const segments = decodePointer(path);
|
|
109
113
|
if (!segments) return { ok: false, value: model };
|
|
110
114
|
if (segments.length === 0) return { ok: true, value };
|
|
111
115
|
|
|
116
|
+
const remove = (current: unknown, index: number): unknown => {
|
|
117
|
+
const segment = segments[index]!;
|
|
118
|
+
const isLast = index === segments.length - 1;
|
|
119
|
+
|
|
120
|
+
if (Array.isArray(current)) {
|
|
121
|
+
if (segment !== "-" && !isArrayIndex(segment)) return current;
|
|
122
|
+
const targetIndex = segment === "-" ? current.length : Number(segment);
|
|
123
|
+
if (isLast) {
|
|
124
|
+
if (
|
|
125
|
+
segment === "-" ||
|
|
126
|
+
!Object.prototype.hasOwnProperty.call(current, targetIndex)
|
|
127
|
+
) {
|
|
128
|
+
return current;
|
|
129
|
+
}
|
|
130
|
+
const clone = current.slice();
|
|
131
|
+
// Preserve indices referenced by other JSON Pointers.
|
|
132
|
+
delete clone[targetIndex];
|
|
133
|
+
return clone;
|
|
134
|
+
}
|
|
135
|
+
const child = current[targetIndex];
|
|
136
|
+
if (!Array.isArray(child) && !isRecord(child)) return current;
|
|
137
|
+
const next = remove(child, index + 1);
|
|
138
|
+
if (next === child) return current;
|
|
139
|
+
const clone = current.slice();
|
|
140
|
+
clone[targetIndex] = next;
|
|
141
|
+
return clone;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!isRecord(current)) return current;
|
|
145
|
+
if (!Object.prototype.hasOwnProperty.call(current, segment)) return current;
|
|
146
|
+
if (isLast) {
|
|
147
|
+
const clone = { ...current };
|
|
148
|
+
delete clone[segment];
|
|
149
|
+
return clone;
|
|
150
|
+
}
|
|
151
|
+
const child = current[segment];
|
|
152
|
+
if (!Array.isArray(child) && !isRecord(child)) return current;
|
|
153
|
+
const next = remove(child, index + 1);
|
|
154
|
+
if (next === child) return current;
|
|
155
|
+
return { ...current, [segment]: next };
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
if (nullDeletes && value === null) {
|
|
159
|
+
return { ok: true, value: remove(model, 0) };
|
|
160
|
+
}
|
|
161
|
+
|
|
112
162
|
const update = (current: unknown, index: number): unknown => {
|
|
113
163
|
const segment = segments[index]!;
|
|
114
164
|
const isLast = index === segments.length - 1;
|
|
115
165
|
|
|
116
166
|
if (Array.isArray(current)) {
|
|
117
167
|
if (segment !== "-" && !isArrayIndex(segment)) return current;
|
|
168
|
+
const targetIndex = segment === "-" ? current.length : Number(segment);
|
|
169
|
+
if (
|
|
170
|
+
segment !== "-" &&
|
|
171
|
+
targetIndex >= current.length &&
|
|
172
|
+
targetIndex > MAX_AUTO_VIVIFY_ARRAY_INDEX
|
|
173
|
+
) {
|
|
174
|
+
return INVALID_POINTER;
|
|
175
|
+
}
|
|
176
|
+
const next = isLast ? value : update(current[targetIndex], index + 1);
|
|
177
|
+
if (next === INVALID_POINTER) return INVALID_POINTER;
|
|
118
178
|
const clone = current.slice();
|
|
119
|
-
|
|
120
|
-
clone[targetIndex] = isLast
|
|
121
|
-
? value
|
|
122
|
-
: update(clone[targetIndex], index + 1);
|
|
179
|
+
clone[targetIndex] = next;
|
|
123
180
|
return clone;
|
|
124
181
|
}
|
|
125
182
|
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const child = clone[segment];
|
|
130
|
-
clone[segment] = isLast
|
|
183
|
+
const record: Record<string, unknown> = isRecord(current) ? current : {};
|
|
184
|
+
const child = Object.hasOwn(record, segment) ? record[segment] : undefined;
|
|
185
|
+
const next = isLast
|
|
131
186
|
? value
|
|
132
187
|
: update(
|
|
133
188
|
child ??
|
|
@@ -137,10 +192,14 @@ const setAtPointer = (
|
|
|
137
192
|
: {}),
|
|
138
193
|
index + 1,
|
|
139
194
|
);
|
|
140
|
-
return
|
|
195
|
+
if (next === INVALID_POINTER) return INVALID_POINTER;
|
|
196
|
+
return { ...record, [segment]: next };
|
|
141
197
|
};
|
|
142
198
|
|
|
143
|
-
|
|
199
|
+
const result = update(model, 0);
|
|
200
|
+
return result === INVALID_POINTER
|
|
201
|
+
? { ok: false, value: model }
|
|
202
|
+
: { ok: true, value: result };
|
|
144
203
|
};
|
|
145
204
|
|
|
146
205
|
const dataModelValue = (
|
|
@@ -264,7 +323,12 @@ export function applyA2uiOperations(
|
|
|
264
323
|
);
|
|
265
324
|
continue;
|
|
266
325
|
}
|
|
267
|
-
const result = setAtPointer(
|
|
326
|
+
const result = setAtPointer(
|
|
327
|
+
surface.dataModel,
|
|
328
|
+
path,
|
|
329
|
+
update.value,
|
|
330
|
+
version === "v1.0",
|
|
331
|
+
);
|
|
268
332
|
if (!result.ok) {
|
|
269
333
|
warnings.push(
|
|
270
334
|
`Operation at index ${index} has an invalid JSON Pointer path.`,
|
|
@@ -29,7 +29,7 @@ export function buildPresentParameters(
|
|
|
29
29
|
// `children` are framework-reserved (see ir.ts), so drop any author-declared
|
|
30
30
|
// copies. On a name clash the first component's schema wins — props are an
|
|
31
31
|
// advisory hint here, not a strict per-component contract.
|
|
32
|
-
const props
|
|
32
|
+
const props = new Map<string, JSONSchema7Definition>();
|
|
33
33
|
const propOwners = new Map<string, string[]>();
|
|
34
34
|
for (const name of names) {
|
|
35
35
|
const propsSchema = toJSONSchema(library[name]!.properties);
|
|
@@ -41,8 +41,11 @@ export function buildPresentParameters(
|
|
|
41
41
|
}
|
|
42
42
|
for (const [key, schema] of Object.entries(propsSchema.properties ?? {})) {
|
|
43
43
|
if (key.startsWith("$") || key === "children") continue;
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
// secure-json-parse rejects the whole tool-argument payload on this key,
|
|
45
|
+
// so advertising it would cost the model the node rather than one prop.
|
|
46
|
+
if (key === "__proto__") continue;
|
|
47
|
+
if (!props.has(key)) {
|
|
48
|
+
props.set(key, schema);
|
|
46
49
|
}
|
|
47
50
|
propOwners.set(key, [...(propOwners.get(key) ?? []), name]);
|
|
48
51
|
}
|
|
@@ -78,7 +81,7 @@ export function buildPresentParameters(
|
|
|
78
81
|
"Stable identity for this UI node. Use it for list items that may reorder.",
|
|
79
82
|
anyOf: [{ type: "string" }, { type: "number" }],
|
|
80
83
|
},
|
|
81
|
-
...props,
|
|
84
|
+
...Object.fromEntries(props),
|
|
82
85
|
children: { $ref: "#/$defs/children" },
|
|
83
86
|
},
|
|
84
87
|
required: [TYPE_KEY],
|
|
@@ -114,6 +114,32 @@ describe("renderGenerativeUI", () => {
|
|
|
114
114
|
expect(html).toBe("");
|
|
115
115
|
});
|
|
116
116
|
|
|
117
|
+
it.each(["toString", "constructor"])(
|
|
118
|
+
"treats inherited %s as an unknown component",
|
|
119
|
+
(type) => {
|
|
120
|
+
const html = renderToStaticMarkup(
|
|
121
|
+
<>{renderGenerativeUI({ $type: type }, library)}</>,
|
|
122
|
+
);
|
|
123
|
+
expect(html).toBe("");
|
|
124
|
+
},
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
it("renders an explicitly registered prototype-named component", () => {
|
|
128
|
+
const prototypeNamedLibrary: GenerativeUILibrary = {
|
|
129
|
+
...library,
|
|
130
|
+
toString: {
|
|
131
|
+
description: "An explicitly registered component.",
|
|
132
|
+
properties: z.object({}),
|
|
133
|
+
render: () => <span>registered</span>,
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const html = renderToStaticMarkup(
|
|
138
|
+
<>{renderGenerativeUI({ $type: "toString" }, prototypeNamedLibrary)}</>,
|
|
139
|
+
);
|
|
140
|
+
expect(html).toBe("<span>registered</span>");
|
|
141
|
+
});
|
|
142
|
+
|
|
117
143
|
it("holds back a node whose `$type` is still streaming", () => {
|
|
118
144
|
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
119
145
|
try {
|
|
@@ -263,6 +289,37 @@ describe("buildPresentParameters", () => {
|
|
|
263
289
|
expect(schema.required).toEqual(["$type"]);
|
|
264
290
|
});
|
|
265
291
|
|
|
292
|
+
it("keeps props whose names are inherited from Object.prototype", () => {
|
|
293
|
+
const schema = buildPresentParameters({
|
|
294
|
+
PrototypeProps: {
|
|
295
|
+
description: "Declares props that shadow Object.prototype members.",
|
|
296
|
+
properties: z.object({
|
|
297
|
+
toString: z.string(),
|
|
298
|
+
valueOf: z.number(),
|
|
299
|
+
constructor: z.boolean(),
|
|
300
|
+
}),
|
|
301
|
+
render: () => null,
|
|
302
|
+
},
|
|
303
|
+
}) as any;
|
|
304
|
+
|
|
305
|
+
expect(schema.properties.toString.type).toBe("string");
|
|
306
|
+
expect(schema.properties.valueOf.type).toBe("number");
|
|
307
|
+
expect(schema.properties.constructor.type).toBe("boolean");
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("omits __proto__, which the tool-argument decoder rejects", () => {
|
|
311
|
+
const schema = buildPresentParameters({
|
|
312
|
+
PrototypeProps: {
|
|
313
|
+
description: "Declares an undeliverable prop name.",
|
|
314
|
+
properties: z.object({ ["__proto__"]: z.number(), label: z.string() }),
|
|
315
|
+
render: () => null,
|
|
316
|
+
},
|
|
317
|
+
}) as any;
|
|
318
|
+
|
|
319
|
+
expect(Object.hasOwn(schema.properties, "__proto__")).toBe(false);
|
|
320
|
+
expect(schema.properties.label.type).toBe("string");
|
|
321
|
+
});
|
|
322
|
+
|
|
266
323
|
it("names every component that declares the same prop in the dev warning", () => {
|
|
267
324
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
268
325
|
try {
|
|
@@ -61,7 +61,9 @@ function renderElement(
|
|
|
61
61
|
library: GenerativeUILibrary,
|
|
62
62
|
context: GenerativeUIRenderContext,
|
|
63
63
|
): ReactNode {
|
|
64
|
-
const entry = library
|
|
64
|
+
const entry = Object.hasOwn(library, element.type)
|
|
65
|
+
? library[element.type]
|
|
66
|
+
: undefined;
|
|
65
67
|
if (!entry) {
|
|
66
68
|
reportUnknownComponent(element.type, Object.keys(library));
|
|
67
69
|
return null;
|
|
@@ -26,6 +26,16 @@ describe("alertVocabulary", () => {
|
|
|
26
26
|
).toContain('data-aui-tone="danger"');
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
+
it("Alert ignores malformed text properties", () => {
|
|
30
|
+
expect(
|
|
31
|
+
render({
|
|
32
|
+
$type: "Alert",
|
|
33
|
+
title: { unexpected: true },
|
|
34
|
+
description: { unexpected: true },
|
|
35
|
+
}),
|
|
36
|
+
).toBe('<div data-aui="alert" data-aui-tone="info" role="alert"></div>');
|
|
37
|
+
});
|
|
38
|
+
|
|
29
39
|
it("Carousel wraps each child Card in a slide, capped at 10", () => {
|
|
30
40
|
const children = Array.from({ length: 12 }, (_, i) => ({
|
|
31
41
|
$type: "Card",
|
package/src/vocabulary/alert.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import { Children } from "react";
|
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import type { GenerativeUILibrary } from "../types";
|
|
4
4
|
import { ALERT_TONES } from "../ir";
|
|
5
|
+
import { toTextContent } from "./toTextContent";
|
|
5
6
|
|
|
6
7
|
const MAX_CARDS = 10;
|
|
7
8
|
|
|
@@ -20,13 +21,21 @@ export const alertVocabulary = {
|
|
|
20
21
|
.optional()
|
|
21
22
|
.describe("Severity tone; defaults to `info`."),
|
|
22
23
|
}),
|
|
23
|
-
render: ({ title, description, tone, children }) =>
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
{
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
render: ({ title, description, tone, children }) => {
|
|
25
|
+
const alertTitle = toTextContent(title);
|
|
26
|
+
const alertDescription = toTextContent(description);
|
|
27
|
+
return (
|
|
28
|
+
<div data-aui="alert" data-aui-tone={tone ?? "info"} role="alert">
|
|
29
|
+
{alertTitle ? (
|
|
30
|
+
<header data-aui="alert-title">{alertTitle}</header>
|
|
31
|
+
) : null}
|
|
32
|
+
{alertDescription ? (
|
|
33
|
+
<p data-aui="alert-desc">{alertDescription}</p>
|
|
34
|
+
) : null}
|
|
35
|
+
{children}
|
|
36
|
+
</div>
|
|
37
|
+
);
|
|
38
|
+
},
|
|
30
39
|
},
|
|
31
40
|
Carousel: {
|
|
32
41
|
description:
|
|
@@ -32,6 +32,32 @@ describe("dataVocabulary", () => {
|
|
|
32
32
|
expect(html).toContain("<td>only</td>");
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
+
it("Table ignores malformed collections instead of throwing", () => {
|
|
36
|
+
expect(
|
|
37
|
+
render({
|
|
38
|
+
$type: "Table",
|
|
39
|
+
columns: "not-an-array",
|
|
40
|
+
rows: [["kept", null, {}, false], null],
|
|
41
|
+
}),
|
|
42
|
+
).toBe(
|
|
43
|
+
'<table data-aui="table"><tbody><tr><td>kept</td><td></td><td></td><td>false</td></tr></tbody></table>',
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
expect(render({ $type: "Table", columns: [null], rows: [null] })).toBe(
|
|
47
|
+
'<table data-aui="table"></table>',
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
expect(
|
|
51
|
+
render({
|
|
52
|
+
$type: "Table",
|
|
53
|
+
columns: [null, { label: "Name" }],
|
|
54
|
+
rows: "not-an-array",
|
|
55
|
+
}),
|
|
56
|
+
).toBe(
|
|
57
|
+
'<table data-aui="table"><thead><tr><th data-aui="table-col"></th><th data-aui="table-col">Name</th></tr></thead></table>',
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
35
61
|
it("Markdown renders the value in a div", () => {
|
|
36
62
|
expect(render({ $type: "Markdown", value: "# hi" })).toBe(
|
|
37
63
|
'<div data-aui="markdown"># hi</div>',
|
|
@@ -52,6 +78,20 @@ describe("dataVocabulary", () => {
|
|
|
52
78
|
).toBe('<div data-aui="markdown">partial</div>');
|
|
53
79
|
});
|
|
54
80
|
|
|
81
|
+
it("Markdown ignores malformed streaming values", () => {
|
|
82
|
+
expect(
|
|
83
|
+
renderToStaticMarkup(
|
|
84
|
+
<>
|
|
85
|
+
{renderGenerativeUI(
|
|
86
|
+
{ $type: "Markdown", value: { unexpected: true } },
|
|
87
|
+
dataVocabulary,
|
|
88
|
+
{ status: "streaming" },
|
|
89
|
+
)}
|
|
90
|
+
</>,
|
|
91
|
+
),
|
|
92
|
+
).toBe('<div data-aui="markdown"></div>');
|
|
93
|
+
});
|
|
94
|
+
|
|
55
95
|
it("Chart bar variant renders one rect per data point", () => {
|
|
56
96
|
const html = render({
|
|
57
97
|
$type: "Chart",
|