@assistant-ui/react-generative-ui 0.0.7 → 0.0.9

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.
Files changed (181) hide show
  1. package/dist/JSONGenerativeUI.client.d.ts +0 -1
  2. package/dist/JSONGenerativeUI.client.d.ts.map +1 -1
  3. package/dist/JSONGenerativeUI.server.d.ts +0 -1
  4. package/dist/JSONGenerativeUI.server.d.ts.map +1 -1
  5. package/dist/JSONGenerativeUI.shared.d.ts +0 -1
  6. package/dist/JSONGenerativeUI.shared.d.ts.map +1 -1
  7. package/dist/actionRegistry.d.ts +2 -11
  8. package/dist/actionRegistry.d.ts.map +1 -1
  9. package/dist/actionRegistry.js.map +1 -1
  10. package/dist/buildPresentParameters.d.ts +5 -5
  11. package/dist/buildPresentParameters.d.ts.map +1 -1
  12. package/dist/buildPresentParameters.js +10 -5
  13. package/dist/buildPresentParameters.js.map +1 -1
  14. package/dist/constants.d.ts.map +1 -1
  15. package/dist/defineGenerativeComponents.d.ts +0 -1
  16. package/dist/defineGenerativeComponents.d.ts.map +1 -1
  17. package/dist/generativeUIToJSX.d.ts +15 -10
  18. package/dist/generativeUIToJSX.d.ts.map +1 -1
  19. package/dist/generativeUIToJSX.js +47 -17
  20. package/dist/generativeUIToJSX.js.map +1 -1
  21. package/dist/index.d.ts +4 -3
  22. package/dist/index.js +2 -2
  23. package/dist/index.server.d.ts +4 -3
  24. package/dist/index.server.js +2 -2
  25. package/dist/index.shared.d.ts +3 -3
  26. package/dist/index.shared.js +2 -2
  27. package/dist/ir.d.ts +6 -6
  28. package/dist/ir.d.ts.map +1 -1
  29. package/dist/ir.js +27 -1
  30. package/dist/ir.js.map +1 -1
  31. package/dist/node_modules/.pnpm/@types_json-schema@7.0.15/node_modules/@types/json-schema/index.d.ts +3 -3
  32. package/dist/node_modules/.pnpm/@types_json-schema@7.0.15/node_modules/@types/json-schema/index.d.ts.map +1 -1
  33. package/dist/renderGenerativeUI.d.ts +0 -1
  34. package/dist/renderGenerativeUI.d.ts.map +1 -1
  35. package/dist/slack/constants.d.ts +111 -0
  36. package/dist/slack/constants.d.ts.map +1 -0
  37. package/dist/slack/constants.js +162 -0
  38. package/dist/slack/constants.js.map +1 -0
  39. package/dist/slack/decodeBlockAction.d.ts +11 -0
  40. package/dist/slack/decodeBlockAction.d.ts.map +1 -0
  41. package/dist/slack/decodeBlockAction.js +40 -0
  42. package/dist/slack/decodeBlockAction.js.map +1 -0
  43. package/dist/slack/fromSlackBlocks.d.ts +30 -0
  44. package/dist/slack/fromSlackBlocks.d.ts.map +1 -0
  45. package/dist/slack/fromSlackBlocks.js +447 -0
  46. package/dist/slack/fromSlackBlocks.js.map +1 -0
  47. package/dist/slack/toSlackBlocks.d.ts +7 -0
  48. package/dist/slack/toSlackBlocks.d.ts.map +1 -0
  49. package/dist/slack/toSlackBlocks.js +649 -0
  50. package/dist/slack/toSlackBlocks.js.map +1 -0
  51. package/dist/slack/types.d.ts +178 -0
  52. package/dist/slack/types.d.ts.map +1 -0
  53. package/dist/slack/types.js +0 -0
  54. package/dist/slack.d.ts +5 -0
  55. package/dist/slack.js +4 -0
  56. package/dist/teams/boundSpec.d.ts +20 -0
  57. package/dist/teams/boundSpec.d.ts.map +1 -0
  58. package/dist/teams/boundSpec.js +67 -0
  59. package/dist/teams/boundSpec.js.map +1 -0
  60. package/dist/teams/constants.d.ts +57 -0
  61. package/dist/teams/constants.d.ts.map +1 -0
  62. package/dist/teams/constants.js +92 -0
  63. package/dist/teams/constants.js.map +1 -0
  64. package/dist/teams/decodeSubmitData.d.ts +25 -0
  65. package/dist/teams/decodeSubmitData.d.ts.map +1 -0
  66. package/dist/teams/decodeSubmitData.js +44 -0
  67. package/dist/teams/decodeSubmitData.js.map +1 -0
  68. package/dist/teams/toAdaptiveCard.d.ts +39 -0
  69. package/dist/teams/toAdaptiveCard.d.ts.map +1 -0
  70. package/dist/teams/toAdaptiveCard.js +496 -0
  71. package/dist/teams/toAdaptiveCard.js.map +1 -0
  72. package/dist/teams/toTeamsAttachments.d.ts +13 -0
  73. package/dist/teams/toTeamsAttachments.d.ts.map +1 -0
  74. package/dist/teams/toTeamsAttachments.js +95 -0
  75. package/dist/teams/toTeamsAttachments.js.map +1 -0
  76. package/dist/teams/types.d.ts +196 -0
  77. package/dist/teams/types.d.ts.map +1 -0
  78. package/dist/teams/types.js +0 -0
  79. package/dist/teams.d.ts +5 -0
  80. package/dist/teams.js +4 -0
  81. package/dist/types.d.ts +7 -4
  82. package/dist/types.d.ts.map +1 -1
  83. package/dist/vocabulary/alert.d.ts +6 -12
  84. package/dist/vocabulary/alert.d.ts.map +1 -1
  85. package/dist/vocabulary/alert.js +13 -4
  86. package/dist/vocabulary/alert.js.map +1 -1
  87. package/dist/vocabulary/collectFormValues.d.ts +23 -0
  88. package/dist/vocabulary/collectFormValues.d.ts.map +1 -0
  89. package/dist/vocabulary/collectFormValues.js +32 -0
  90. package/dist/vocabulary/collectFormValues.js.map +1 -0
  91. package/dist/vocabulary/data.d.ts +16 -17
  92. package/dist/vocabulary/data.d.ts.map +1 -1
  93. package/dist/vocabulary/data.js +244 -13
  94. package/dist/vocabulary/data.js.map +1 -1
  95. package/dist/vocabulary/dispatch.d.ts +11 -0
  96. package/dist/vocabulary/dispatch.d.ts.map +1 -0
  97. package/dist/vocabulary/dispatch.js +21 -0
  98. package/dist/vocabulary/dispatch.js.map +1 -0
  99. package/dist/vocabulary/fact.d.ts +1 -6
  100. package/dist/vocabulary/fact.d.ts.map +1 -1
  101. package/dist/vocabulary/fact.js.map +1 -1
  102. package/dist/vocabulary/form.d.ts +14 -0
  103. package/dist/vocabulary/form.d.ts.map +1 -0
  104. package/dist/vocabulary/form.js +23 -0
  105. package/dist/vocabulary/form.js.map +1 -0
  106. package/dist/vocabulary/icon.d.ts +44 -0
  107. package/dist/vocabulary/icon.d.ts.map +1 -0
  108. package/dist/vocabulary/icon.js +272 -0
  109. package/dist/vocabulary/icon.js.map +1 -0
  110. package/dist/vocabulary/index.d.ts +0 -1
  111. package/dist/vocabulary/index.d.ts.map +1 -1
  112. package/dist/vocabulary/index.js +7 -1
  113. package/dist/vocabulary/index.js.map +1 -1
  114. package/dist/vocabulary/interactive.d.ts +45 -33
  115. package/dist/vocabulary/interactive.d.ts.map +1 -1
  116. package/dist/vocabulary/interactive.js +78 -29
  117. package/dist/vocabulary/interactive.js.map +1 -1
  118. package/dist/vocabulary/layout.d.ts +27 -23
  119. package/dist/vocabulary/layout.d.ts.map +1 -1
  120. package/dist/vocabulary/layout.js +78 -14
  121. package/dist/vocabulary/layout.js.map +1 -1
  122. package/dist/vocabulary/list.d.ts +17 -0
  123. package/dist/vocabulary/list.d.ts.map +1 -0
  124. package/dist/vocabulary/list.js +48 -0
  125. package/dist/vocabulary/list.js.map +1 -0
  126. package/dist/vocabulary/media.d.ts +3 -9
  127. package/dist/vocabulary/media.d.ts.map +1 -1
  128. package/dist/vocabulary/media.js +17 -7
  129. package/dist/vocabulary/media.js.map +1 -1
  130. package/dist/vocabulary/text.d.ts +3 -17
  131. package/dist/vocabulary/text.d.ts.map +1 -1
  132. package/dist/vocabulary/text.js.map +1 -1
  133. package/package.json +15 -7
  134. package/src/actionRegistry.ts +2 -10
  135. package/src/buildPresentParameters.ts +15 -9
  136. package/src/generativeUIToJSX.test.ts +186 -0
  137. package/src/generativeUIToJSX.ts +112 -21
  138. package/src/index.shared.ts +3 -0
  139. package/src/ir.ts +31 -5
  140. package/src/renderGenerativeUI.test.tsx +16 -2
  141. package/src/slack/constants.ts +212 -0
  142. package/src/slack/decodeBlockAction.test.ts +135 -0
  143. package/src/slack/decodeBlockAction.ts +66 -0
  144. package/src/slack/fromSlackBlocks.test.ts +1145 -0
  145. package/src/slack/fromSlackBlocks.ts +709 -0
  146. package/src/slack/toSlackBlocks.test.ts +1936 -0
  147. package/src/slack/toSlackBlocks.ts +1319 -0
  148. package/src/slack/types.ts +230 -0
  149. package/src/slack.ts +37 -0
  150. package/src/teams/boundSpec.ts +102 -0
  151. package/src/teams/constants.ts +127 -0
  152. package/src/teams/decodeSubmitData.test.ts +148 -0
  153. package/src/teams/decodeSubmitData.ts +57 -0
  154. package/src/teams/toAdaptiveCard.test.ts +1410 -0
  155. package/src/teams/toAdaptiveCard.ts +762 -0
  156. package/src/teams/toTeamsAttachments.test.ts +29 -0
  157. package/src/teams/toTeamsAttachments.ts +137 -0
  158. package/src/teams/types.ts +237 -0
  159. package/src/teams.ts +34 -0
  160. package/src/vocabulary/alert.test.tsx +72 -2
  161. package/src/vocabulary/alert.tsx +24 -5
  162. package/src/vocabulary/collectFormValues.test.ts +127 -0
  163. package/src/vocabulary/collectFormValues.ts +58 -0
  164. package/src/vocabulary/data.test.tsx +503 -3
  165. package/src/vocabulary/data.tsx +426 -18
  166. package/src/vocabulary/dispatch.ts +22 -0
  167. package/src/vocabulary/form.test.tsx +112 -0
  168. package/src/vocabulary/form.tsx +34 -0
  169. package/src/vocabulary/icon.test.tsx +64 -0
  170. package/src/vocabulary/icon.tsx +174 -0
  171. package/src/vocabulary/index.test.ts +7 -0
  172. package/src/vocabulary/index.ts +6 -0
  173. package/src/vocabulary/interactive.dispatch.test.tsx +243 -1
  174. package/src/vocabulary/interactive.test.tsx +143 -3
  175. package/src/vocabulary/interactive.tsx +134 -30
  176. package/src/vocabulary/layout.test.tsx +231 -3
  177. package/src/vocabulary/layout.tsx +168 -15
  178. package/src/vocabulary/list.test.tsx +195 -0
  179. package/src/vocabulary/list.tsx +48 -0
  180. package/src/vocabulary/media.test.tsx +36 -4
  181. package/src/vocabulary/media.tsx +27 -8
@@ -9,6 +9,265 @@ const cellSchema = z
9
9
  .union([z.string(), z.number(), z.boolean()])
10
10
  .describe("A cell value.");
11
11
 
12
+ const CHART_HEIGHT = 40;
13
+ const CHART_WIDTH = 100;
14
+
15
+ const clampValue = (value: unknown): number =>
16
+ typeof value === "number" && Number.isFinite(value) ? Math.max(0, value) : 0;
17
+
18
+ const yFor = (value: number, max: number): number =>
19
+ max > 0 ? CHART_HEIGHT - (value / max) * CHART_HEIGHT : CHART_HEIGHT;
20
+
21
+ const pointSchema = z.object({
22
+ label: z.string().optional().describe("Point label."),
23
+ value: z.number().describe("Point value."),
24
+ });
25
+
26
+ const seriesSchema = z.object({
27
+ label: z.string().optional().describe("Series label, shown in the legend."),
28
+ data: z.array(pointSchema).describe("Data points for this series."),
29
+ });
30
+
31
+ type ChartPoint = { label?: string; value: number };
32
+ type ChartSeriesInput = { label?: string; data: ChartPoint[] };
33
+ type NormalizedChartSeries = {
34
+ label: string | undefined;
35
+ values: number[];
36
+ labels: (string | undefined)[];
37
+ };
38
+
39
+ /** Pads every series to the longest series' length, treating a shorter series' missing points as `{ value: 0 }` so mismatched series never misalign or throw. */
40
+ function normalizeSeries(
41
+ seriesProp: unknown,
42
+ dataProp: unknown,
43
+ ): { count: number; series: NormalizedChartSeries[] } {
44
+ const rawSeries: ChartSeriesInput[] =
45
+ Array.isArray(seriesProp) && seriesProp.length > 0
46
+ ? (seriesProp as ChartSeriesInput[])
47
+ : [{ data: Array.isArray(dataProp) ? (dataProp as ChartPoint[]) : [] }];
48
+
49
+ const count = rawSeries.reduce((max, s) => {
50
+ const len = Array.isArray(s?.data) ? s.data.length : 0;
51
+ return Math.max(max, len);
52
+ }, 0);
53
+
54
+ const series = rawSeries.map((s) => {
55
+ const points = Array.isArray(s?.data) ? s.data : [];
56
+ const values: number[] = [];
57
+ const labels: (string | undefined)[] = [];
58
+ for (let i = 0; i < count; i++) {
59
+ const point = points[i];
60
+ values.push(clampValue(point?.value));
61
+ labels.push(typeof point?.label === "string" ? point.label : undefined);
62
+ }
63
+ return {
64
+ label: typeof s?.label === "string" ? s.label : undefined,
65
+ values,
66
+ labels,
67
+ };
68
+ });
69
+
70
+ return { count, series };
71
+ }
72
+
73
+ function computeMax(
74
+ series: NormalizedChartSeries[],
75
+ count: number,
76
+ stacked: boolean,
77
+ ): number {
78
+ if (stacked) {
79
+ let max = 0;
80
+ for (let i = 0; i < count; i++) {
81
+ let sum = 0;
82
+ for (const s of series) sum += s.values[i] ?? 0;
83
+ max = Math.max(max, sum);
84
+ }
85
+ return max;
86
+ }
87
+ let max = 0;
88
+ for (const s of series) for (const v of s.values) max = Math.max(max, v);
89
+ return max;
90
+ }
91
+
92
+ const NICE_MULTIPLES = [1, 2, 5];
93
+
94
+ /** The smallest of `{1, 2, 5} × 10^n` that is `>= max`, so axis ticks land on round numbers. */
95
+ function niceMax(max: number): number {
96
+ if (max <= 0) return 0;
97
+ const exponent = Math.floor(Math.log10(max));
98
+ for (const multiple of NICE_MULTIPLES) {
99
+ const candidate = multiple * 10 ** exponent;
100
+ if (candidate >= max) return candidate;
101
+ }
102
+ return 10 ** (exponent + 1);
103
+ }
104
+
105
+ const TICK_COUNT = 5;
106
+
107
+ /** `TICK_COUNT` evenly spaced ticks from `max` down to `0`, for a top-to-bottom y-axis column. */
108
+ function tickValues(max: number): number[] {
109
+ const ticks: number[] = [];
110
+ for (let i = 0; i < TICK_COUNT; i++) {
111
+ ticks.push((max * (TICK_COUNT - 1 - i)) / (TICK_COUNT - 1));
112
+ }
113
+ return ticks;
114
+ }
115
+
116
+ function formatTick(value: number): string {
117
+ return (Math.round(value * 100) / 100).toLocaleString("en-US");
118
+ }
119
+
120
+ type ChartVariant = "bar" | "line" | "sparkline" | "area";
121
+
122
+ const SERIES_PALETTE_SIZE = 5;
123
+
124
+ function renderSeriesMarks(
125
+ variant: ChartVariant,
126
+ series: NormalizedChartSeries[],
127
+ count: number,
128
+ scaleMax: number,
129
+ stacked: boolean,
130
+ ) {
131
+ if (variant === "bar") {
132
+ const slot = count > 0 ? CHART_WIDTH / count : 0;
133
+ const gap = slot * 0.2;
134
+ const groupWidth = slot - gap;
135
+ const seriesCount = series.length;
136
+ const perSeriesWidth =
137
+ stacked || seriesCount <= 1 ? groupWidth : groupWidth / seriesCount;
138
+ const cumulative = new Array(count).fill(0) as number[];
139
+
140
+ return series.map((s, seriesIndex) => {
141
+ const marks = s.values.map((value, i) => {
142
+ const height = scaleMax > 0 ? (value / scaleMax) * CHART_HEIGHT : 0;
143
+ const belowHeight =
144
+ stacked && scaleMax > 0
145
+ ? ((cumulative[i] ?? 0) / scaleMax) * CHART_HEIGHT
146
+ : 0;
147
+ const x =
148
+ stacked || seriesCount <= 1
149
+ ? i * slot + gap / 2
150
+ : i * slot + gap / 2 + seriesIndex * perSeriesWidth;
151
+ return (
152
+ <rect
153
+ key={i}
154
+ x={x}
155
+ y={CHART_HEIGHT - belowHeight - height}
156
+ width={perSeriesWidth}
157
+ height={height}
158
+ fill="currentColor"
159
+ />
160
+ );
161
+ });
162
+ if (stacked) {
163
+ for (let i = 0; i < count; i++) {
164
+ cumulative[i] = (cumulative[i] ?? 0) + (s.values[i] ?? 0);
165
+ }
166
+ }
167
+ return (
168
+ <g
169
+ key={seriesIndex}
170
+ data-aui="chart-series"
171
+ data-aui-series={seriesIndex % SERIES_PALETTE_SIZE}
172
+ >
173
+ {marks}
174
+ </g>
175
+ );
176
+ });
177
+ }
178
+
179
+ if (variant === "area") {
180
+ const cumulative = new Array(count).fill(0) as number[];
181
+
182
+ return series.map((s, seriesIndex) => {
183
+ const bottoms = stacked
184
+ ? cumulative.slice()
185
+ : (new Array(count).fill(0) as number[]);
186
+ if (stacked) {
187
+ for (let i = 0; i < count; i++) {
188
+ cumulative[i] = (cumulative[i] ?? 0) + (s.values[i] ?? 0);
189
+ }
190
+ }
191
+ const tops = stacked ? cumulative.slice() : s.values.slice();
192
+
193
+ if (count === 1) {
194
+ const y = yFor(tops[0] ?? 0, scaleMax);
195
+ return (
196
+ <g
197
+ key={seriesIndex}
198
+ data-aui="chart-series"
199
+ data-aui-series={seriesIndex % SERIES_PALETTE_SIZE}
200
+ >
201
+ <circle cx={CHART_WIDTH / 2} cy={y} r={2} fill="currentColor" />
202
+ </g>
203
+ );
204
+ }
205
+ if (count === 0) {
206
+ return (
207
+ <g
208
+ key={seriesIndex}
209
+ data-aui="chart-series"
210
+ data-aui-series={seriesIndex % SERIES_PALETTE_SIZE}
211
+ />
212
+ );
213
+ }
214
+
215
+ const topPoints = tops.map(
216
+ (v, i) => `${(i / (count - 1)) * CHART_WIDTH},${yFor(v, scaleMax)}`,
217
+ );
218
+ const bottomPoints = bottoms
219
+ .map(
220
+ (v, i) => `${(i / (count - 1)) * CHART_WIDTH},${yFor(v, scaleMax)}`,
221
+ )
222
+ .reverse();
223
+
224
+ return (
225
+ <g
226
+ key={seriesIndex}
227
+ data-aui="chart-series"
228
+ data-aui-series={seriesIndex % SERIES_PALETTE_SIZE}
229
+ >
230
+ <polygon
231
+ points={[...topPoints, ...bottomPoints].join(" ")}
232
+ fill="currentColor"
233
+ fillOpacity="0.25"
234
+ stroke="currentColor"
235
+ />
236
+ </g>
237
+ );
238
+ });
239
+ }
240
+
241
+ // line / sparkline: each series is always drawn independently. Stacking a line has no single standard meaning, so `stacked` is ignored (the caller never passes it for these variants).
242
+ return series.map((s, seriesIndex) => (
243
+ <g
244
+ key={seriesIndex}
245
+ data-aui="chart-series"
246
+ data-aui-series={seriesIndex % SERIES_PALETTE_SIZE}
247
+ >
248
+ {count === 1 ? (
249
+ <circle
250
+ cx={CHART_WIDTH / 2}
251
+ cy={yFor(s.values[0] ?? 0, scaleMax)}
252
+ r={2}
253
+ fill="currentColor"
254
+ />
255
+ ) : count > 1 ? (
256
+ <polyline
257
+ points={s.values
258
+ .map(
259
+ (v, i) =>
260
+ `${(i / (count - 1)) * CHART_WIDTH},${yFor(v, scaleMax)}`,
261
+ )
262
+ .join(" ")}
263
+ fill="none"
264
+ stroke="currentColor"
265
+ />
266
+ ) : null}
267
+ </g>
268
+ ));
269
+ }
270
+
12
271
  export const dataVocabulary = {
13
272
  Table: {
14
273
  description:
@@ -61,26 +320,175 @@ export const dataVocabulary = {
61
320
  },
62
321
  Chart: {
63
322
  description:
64
- "A chart placeholder. Carries variant and data for a client renderer to draw.",
323
+ "A chart. `variant` selects bar, line, sparkline, or area rendering. Provide `data` for a single series, or `series` for multiple named series (`series` takes precedence over `data` when both are present). Set `stacked` to accumulate series values instead of overlaying them, and `showAxis`/`showLegend` to add y-axis ticks, x-axis point labels, and a legend.",
65
324
  properties: z.object({
66
- variant: z.enum(["bar", "line", "sparkline"]).describe("Chart variant."),
325
+ variant: z
326
+ .enum(["bar", "line", "sparkline", "area"])
327
+ .describe("Chart variant."),
67
328
  data: z
68
- .array(
69
- z.object({
70
- label: z.string().optional(),
71
- value: z.number(),
72
- }),
73
- )
74
- .describe("Data points."),
75
- color: z.string().optional().describe("Series color."),
329
+ .array(pointSchema)
330
+ .optional()
331
+ .describe("Data points for a single series."),
332
+ series: z
333
+ .array(seriesSchema)
334
+ .optional()
335
+ .describe(
336
+ "Multiple named series; takes precedence over `data` when present.",
337
+ ),
338
+ stacked: z
339
+ .boolean()
340
+ .optional()
341
+ .describe(
342
+ "Accumulate series values instead of overlaying them (bar and area variants; ignored for line and sparkline).",
343
+ ),
344
+ showAxis: z
345
+ .boolean()
346
+ .optional()
347
+ .describe("Show y-axis ticks and x-axis point labels."),
348
+ showLegend: z
349
+ .boolean()
350
+ .optional()
351
+ .describe("Show a legend mapping each series to its color."),
352
+ color: z
353
+ .string()
354
+ .optional()
355
+ .describe(
356
+ "Series color (single-series mode only). One of `emphasis`, `secondary`, `alpha-70`, `white`, `white-70`, or `white-50`, matching Text's `color` tokens; other values have no visual effect.",
357
+ ),
76
358
  }),
77
- render: ({ variant, data, color }) => (
78
- <div
79
- data-aui="chart"
80
- data-aui-variant={variant}
81
- data-aui-color={color}
82
- data-aui-data={JSON.stringify(data)}
83
- />
84
- ),
359
+ render: ({
360
+ variant,
361
+ data,
362
+ series,
363
+ stacked,
364
+ showAxis,
365
+ showLegend,
366
+ color,
367
+ }) => {
368
+ const usesExtendedFeatures =
369
+ variant === "area" ||
370
+ !!stacked ||
371
+ !!showAxis ||
372
+ !!showLegend ||
373
+ (Array.isArray(series) && series.length > 0);
374
+
375
+ if (!usesExtendedFeatures) {
376
+ const points = Array.isArray(data) ? data : [];
377
+ const n = points.length;
378
+ const values = points.map((d) => clampValue(d?.value));
379
+ const max = values.reduce((m, v) => Math.max(m, v), 0);
380
+ const slot = n > 0 ? CHART_WIDTH / n : 0;
381
+ const gap = slot * 0.2;
382
+ const barWidth = slot - gap;
383
+
384
+ return (
385
+ <svg
386
+ data-aui="chart"
387
+ data-aui-variant={variant}
388
+ data-aui-color={color}
389
+ role="img"
390
+ aria-label={`${variant} chart with ${n} data points`}
391
+ viewBox={`0 0 ${CHART_WIDTH} ${CHART_HEIGHT}`}
392
+ preserveAspectRatio="none"
393
+ >
394
+ {variant === "bar" ? (
395
+ values.map((v, i) => {
396
+ const height = max > 0 ? (v / max) * CHART_HEIGHT : 0;
397
+ return (
398
+ <rect
399
+ key={i}
400
+ x={i * slot + gap / 2}
401
+ y={CHART_HEIGHT - height}
402
+ width={barWidth}
403
+ height={height}
404
+ fill="currentColor"
405
+ />
406
+ );
407
+ })
408
+ ) : n === 1 ? (
409
+ <circle
410
+ cx={CHART_WIDTH / 2}
411
+ cy={yFor(values[0] ?? 0, max)}
412
+ r={2}
413
+ fill="currentColor"
414
+ />
415
+ ) : n > 1 ? (
416
+ <polyline
417
+ points={values
418
+ .map(
419
+ (v, i) => `${(i / (n - 1)) * CHART_WIDTH},${yFor(v, max)}`,
420
+ )
421
+ .join(" ")}
422
+ fill="none"
423
+ stroke="currentColor"
424
+ />
425
+ ) : null}
426
+ </svg>
427
+ );
428
+ }
429
+
430
+ const { count, series: normalized } = normalizeSeries(series, data);
431
+ const stackedScale =
432
+ !!stacked && (variant === "bar" || variant === "area");
433
+ const rawMax = computeMax(normalized, count, stackedScale);
434
+ const scaleMax = showAxis ? niceMax(rawMax) : rawMax;
435
+ const marks = renderSeriesMarks(
436
+ variant,
437
+ normalized,
438
+ count,
439
+ scaleMax,
440
+ stackedScale,
441
+ );
442
+
443
+ const svg = (
444
+ <svg
445
+ data-aui="chart"
446
+ data-aui-variant={variant}
447
+ data-aui-color={color}
448
+ role="img"
449
+ aria-label={`${variant} chart with ${count} data points`}
450
+ viewBox={`0 0 ${CHART_WIDTH} ${CHART_HEIGHT}`}
451
+ preserveAspectRatio="none"
452
+ >
453
+ {marks}
454
+ </svg>
455
+ );
456
+
457
+ if (!showAxis && !showLegend) return svg;
458
+
459
+ return (
460
+ <div data-aui="chart-frame">
461
+ {showAxis ? (
462
+ <div data-aui="chart-ticks">
463
+ {tickValues(scaleMax).map((tick, i) => (
464
+ <div key={i}>{formatTick(tick)}</div>
465
+ ))}
466
+ </div>
467
+ ) : null}
468
+ {svg}
469
+ {showAxis ? (
470
+ <div data-aui="chart-xlabels">
471
+ {(normalized[0]?.labels ?? []).map((label, i) => (
472
+ <div key={i}>{label ?? ""}</div>
473
+ ))}
474
+ </div>
475
+ ) : null}
476
+ {showLegend ? (
477
+ <div data-aui="chart-legend">
478
+ {normalized.map((s, i) => (
479
+ <span
480
+ key={i}
481
+ data-aui="chart-legend-item"
482
+ data-aui-series={i % SERIES_PALETTE_SIZE}
483
+ >
484
+ <span data-aui="chart-legend-swatch" />
485
+ {s.label ?? `Series ${i + 1}`}
486
+ </span>
487
+ ))}
488
+ </div>
489
+ ) : null}
490
+ </div>
491
+ );
492
+ },
85
493
  },
86
494
  } satisfies GenerativeUILibrary;
@@ -0,0 +1,22 @@
1
+ import type { Action } from "../ir";
2
+ import type { GenerativeUIDispatch } from "../types";
3
+
4
+ export const actionAttr = (a: Action | undefined): string | undefined =>
5
+ a ? JSON.stringify(a) : undefined;
6
+
7
+ /**
8
+ * Fires `$action` through `$dispatch` when both are present, merging a runtime value into the payload under the reserved `$input` key (not `value`) so the user's input never clobbers a model-supplied `value` field. No-op when no registry is wired. The returned promise from an async handler is caught and re-thrown on a microtask so rejections surface rather than going unhandled.
9
+ */
10
+ export const fire = (
11
+ $action: Action | undefined,
12
+ $dispatch: GenerativeUIDispatch | undefined,
13
+ input?: unknown,
14
+ ) => {
15
+ if (!$action || !$dispatch) return;
16
+ const payload = input === undefined ? $action : { ...$action, $input: input };
17
+ void Promise.resolve($dispatch(payload)).catch((error) => {
18
+ queueMicrotask(() => {
19
+ throw error;
20
+ });
21
+ });
22
+ };
@@ -0,0 +1,112 @@
1
+ import { describe, it, expect, vi } from "vitest";
2
+ import type { ReactElement } from "react";
3
+ import { renderToStaticMarkup } from "react-dom/server";
4
+ import { renderGenerativeUI } from "../renderGenerativeUI";
5
+ import { createActionRegistry } from "../actionRegistry";
6
+ import { formVocabulary } from "./form";
7
+ import type { FormControlElementLike } from "./collectFormValues";
8
+
9
+ const render = (node: unknown) =>
10
+ renderToStaticMarkup(<>{renderGenerativeUI(node, formVocabulary)}</>);
11
+
12
+ const el = (
13
+ partial: Partial<FormControlElementLike>,
14
+ ): FormControlElementLike => ({
15
+ name: "",
16
+ type: "text",
17
+ value: "",
18
+ disabled: false,
19
+ ...partial,
20
+ });
21
+
22
+ const fakeSubmitEvent = (elements: FormControlElementLike[]) => ({
23
+ preventDefault: vi.fn(),
24
+ currentTarget: { elements },
25
+ });
26
+
27
+ describe("formVocabulary", () => {
28
+ it("Form renders a form element with children and a gap hook", () => {
29
+ const html = render({
30
+ $type: "Form",
31
+ gap: 2,
32
+ children: { $type: "Fact", label: "a", value: "b" } as never,
33
+ });
34
+ expect(html).toContain('<form data-aui="form" data-aui-gap="2">');
35
+ });
36
+
37
+ it("Form without gap or $action renders bare", () => {
38
+ expect(render({ $type: "Form" })).toBe('<form data-aui="form"></form>');
39
+ });
40
+
41
+ it("Form onSubmit prevents default and dispatches collected values keyed by name", () => {
42
+ const handler = vi.fn();
43
+ const registry = createActionRegistry({ save: handler });
44
+ const out = formVocabulary.Form.render({
45
+ $status: "done",
46
+ $action: { type: "save" },
47
+ $dispatch: registry.dispatch,
48
+ }) as ReactElement;
49
+ const onSubmit = (out.props as { onSubmit: (e: unknown) => void }).onSubmit;
50
+ const event = fakeSubmitEvent([
51
+ el({ name: "email", type: "email", value: "a@example.com" }),
52
+ el({ name: "agree", type: "checkbox", checked: true }),
53
+ el({ name: "size", type: "radio", value: "sm", checked: false }),
54
+ el({ name: "size", type: "radio", value: "lg", checked: true }),
55
+ el({ name: "", type: "text", value: "ignored" }),
56
+ el({ name: "off", type: "text", value: "ignored", disabled: true }),
57
+ ]);
58
+ onSubmit(event);
59
+ expect(event.preventDefault).toHaveBeenCalled();
60
+ expect(handler).toHaveBeenCalledWith({
61
+ payload: {
62
+ type: "save",
63
+ $input: { email: "a@example.com", agree: true, size: "lg" },
64
+ },
65
+ });
66
+ });
67
+
68
+ it("Form onSubmit collects repeated non-radio names into an array", () => {
69
+ const handler = vi.fn();
70
+ const registry = createActionRegistry({ save: handler });
71
+ const out = formVocabulary.Form.render({
72
+ $status: "done",
73
+ $action: { type: "save" },
74
+ $dispatch: registry.dispatch,
75
+ }) as ReactElement;
76
+ const onSubmit = (out.props as { onSubmit: (e: unknown) => void }).onSubmit;
77
+ onSubmit(
78
+ fakeSubmitEvent([
79
+ el({ name: "tag", type: "text", value: "a" }),
80
+ el({ name: "tag", type: "text", value: "b" }),
81
+ ]),
82
+ );
83
+ expect(handler).toHaveBeenCalledWith({
84
+ payload: { type: "save", $input: { tag: ["a", "b"] } },
85
+ });
86
+ });
87
+
88
+ it("Form onSubmit is silent (but still prevents default) when no $action is wired", () => {
89
+ const out = formVocabulary.Form.render({
90
+ $status: "done",
91
+ }) as ReactElement;
92
+ const onSubmit = (out.props as { onSubmit: (e: unknown) => void }).onSubmit;
93
+ const event = fakeSubmitEvent([
94
+ el({ name: "x", type: "text", value: "y" }),
95
+ ]);
96
+ expect(() => onSubmit(event)).not.toThrow();
97
+ expect(event.preventDefault).toHaveBeenCalled();
98
+ });
99
+
100
+ it("Form onSubmit is silent (but still prevents default) when $action is present but no $dispatch is wired", () => {
101
+ const out = formVocabulary.Form.render({
102
+ $status: "done",
103
+ $action: { type: "save" },
104
+ }) as ReactElement;
105
+ const onSubmit = (out.props as { onSubmit: (e: unknown) => void }).onSubmit;
106
+ const event = fakeSubmitEvent([
107
+ el({ name: "x", type: "text", value: "y" }),
108
+ ]);
109
+ expect(() => onSubmit(event)).not.toThrow();
110
+ expect(event.preventDefault).toHaveBeenCalled();
111
+ });
112
+ });
@@ -0,0 +1,34 @@
1
+ import { z } from "zod";
2
+ import type { GenerativeUILibrary } from "../types";
3
+ import { actionAttr, fire } from "./dispatch";
4
+ import { collectFormValuesFromEvent } from "./collectFormValues";
5
+
6
+ export const formVocabulary = {
7
+ Form: {
8
+ description:
9
+ "Wraps named child controls (Select/Input/Checkbox/RadioGroup/DatePicker with a `name`). Carries `$action`; on submit it fires with every named control's value, keyed by `name`.",
10
+ properties: z.object({
11
+ gap: z
12
+ .number()
13
+ .min(0)
14
+ .max(8)
15
+ .optional()
16
+ .describe(
17
+ "Gap between children in 4px units. 0 to 8 is the supported range.",
18
+ ),
19
+ }),
20
+ render: ({ gap, $action, $dispatch, children }) => (
21
+ <form
22
+ data-aui="form"
23
+ data-aui-gap={gap}
24
+ data-aui-action={actionAttr($action)}
25
+ onSubmit={(event) => {
26
+ event.preventDefault();
27
+ fire($action, $dispatch, collectFormValuesFromEvent(event));
28
+ }}
29
+ >
30
+ {children}
31
+ </form>
32
+ ),
33
+ },
34
+ } satisfies GenerativeUILibrary;
@@ -0,0 +1,64 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { renderToStaticMarkup } from "react-dom/server";
3
+ import { renderGenerativeUI } from "../renderGenerativeUI";
4
+ import { buildPresentParameters } from "../buildPresentParameters";
5
+ import { iconVocabulary } from "./icon";
6
+ import { ICON_NAMES } from "../ir";
7
+
8
+ const render = (node: unknown) =>
9
+ renderToStaticMarkup(<>{renderGenerativeUI(node, iconVocabulary)}</>);
10
+
11
+ describe("iconVocabulary", () => {
12
+ it("a known name renders an svg with the data-aui hooks and default size", () => {
13
+ const html = render({ $type: "Icon", name: "check" });
14
+ expect(html).toContain('data-aui="icon"');
15
+ expect(html).toContain('data-aui-name="check"');
16
+ expect(html).toContain('viewBox="0 0 24 24"');
17
+ expect(html).toContain('width="16"');
18
+ expect(html).toContain('height="16"');
19
+ expect(html).toContain('aria-hidden="true"');
20
+ });
21
+
22
+ it("every built-in name renders without throwing and without a data-aui-size attribute when size is omitted", () => {
23
+ for (const name of ICON_NAMES) {
24
+ const html = render({ $type: "Icon", name });
25
+ expect(html, name).toContain("<svg");
26
+ expect(html, name).not.toContain("data-aui-size=");
27
+ }
28
+ });
29
+
30
+ it("maps size tokens to pixel dimensions", () => {
31
+ expect(render({ $type: "Icon", name: "star", size: "sm" })).toContain(
32
+ 'width="14"',
33
+ );
34
+ expect(render({ $type: "Icon", name: "star", size: "md" })).toContain(
35
+ 'width="16"',
36
+ );
37
+ expect(render({ $type: "Icon", name: "star", size: "lg" })).toContain(
38
+ 'width="20"',
39
+ );
40
+ });
41
+
42
+ it("emits the size token as a data-aui-size hook when given", () => {
43
+ expect(render({ $type: "Icon", name: "bell", size: "lg" })).toContain(
44
+ 'data-aui-size="lg"',
45
+ );
46
+ });
47
+
48
+ it("an unknown name renders nothing rather than throwing", () => {
49
+ expect(() =>
50
+ render({ $type: "Icon", name: "not-a-real-icon" }),
51
+ ).not.toThrow();
52
+ expect(render({ $type: "Icon", name: "not-a-real-icon" })).toBe("");
53
+ });
54
+
55
+ it("exposes the closed name enum in buildPresentParameters", () => {
56
+ const schema = buildPresentParameters(iconVocabulary) as Record<
57
+ string,
58
+ unknown
59
+ >;
60
+ const properties = schema["properties"] as Record<string, unknown>;
61
+ const nameProp = properties["name"] as { enum?: unknown[] };
62
+ expect(nameProp.enum).toEqual([...ICON_NAMES]);
63
+ });
64
+ });