@copilotkit/shared 2.0.0-rc.37 → 2.0.0-rc.39

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 (102) hide show
  1. package/dist/generative-ui/clone-value.cjs +364 -0
  2. package/dist/generative-ui/clone-value.cjs.map +1 -0
  3. package/dist/generative-ui/clone-value.d.cts +51 -0
  4. package/dist/generative-ui/clone-value.d.cts.map +1 -0
  5. package/dist/generative-ui/clone-value.d.mts +51 -0
  6. package/dist/generative-ui/clone-value.d.mts.map +1 -0
  7. package/dist/generative-ui/clone-value.mjs +363 -0
  8. package/dist/generative-ui/clone-value.mjs.map +1 -0
  9. package/dist/generative-ui/index.cjs +39 -0
  10. package/dist/generative-ui/index.d.cts +8 -2
  11. package/dist/generative-ui/index.d.mts +8 -2
  12. package/dist/generative-ui/index.mjs +8 -2
  13. package/dist/generative-ui/is-record.d.cts +3 -1
  14. package/dist/generative-ui/is-record.d.cts.map +1 -1
  15. package/dist/generative-ui/is-record.d.mts +3 -1
  16. package/dist/generative-ui/is-record.d.mts.map +1 -1
  17. package/dist/generative-ui/schema-render.cjs +2 -1
  18. package/dist/generative-ui/schema-render.cjs.map +1 -1
  19. package/dist/generative-ui/schema-render.d.cts.map +1 -1
  20. package/dist/generative-ui/schema-render.d.mts.map +1 -1
  21. package/dist/generative-ui/schema-render.mjs +2 -1
  22. package/dist/generative-ui/schema-render.mjs.map +1 -1
  23. package/dist/generative-ui/standard/chart-config.cjs +347 -0
  24. package/dist/generative-ui/standard/chart-config.cjs.map +1 -0
  25. package/dist/generative-ui/standard/chart-config.d.cts +97 -0
  26. package/dist/generative-ui/standard/chart-config.d.cts.map +1 -0
  27. package/dist/generative-ui/standard/chart-config.d.mts +97 -0
  28. package/dist/generative-ui/standard/chart-config.d.mts.map +1 -0
  29. package/dist/generative-ui/standard/chart-config.mjs +342 -0
  30. package/dist/generative-ui/standard/chart-config.mjs.map +1 -0
  31. package/dist/generative-ui/standard/composites.d.cts +2 -2
  32. package/dist/generative-ui/standard/composites.d.mts +2 -2
  33. package/dist/generative-ui/standard/declarations.d.cts +68 -68
  34. package/dist/generative-ui/standard/declarations.d.mts +68 -68
  35. package/dist/generative-ui/standard/index.d.mts +5 -1
  36. package/dist/generative-ui/standard/inputs.d.cts +22 -22
  37. package/dist/generative-ui/standard/inputs.d.mts +22 -22
  38. package/dist/generative-ui/standard/layout.d.cts +90 -90
  39. package/dist/generative-ui/standard/layout.d.mts +90 -90
  40. package/dist/generative-ui/standard/map-helpers.cjs +104 -0
  41. package/dist/generative-ui/standard/map-helpers.cjs.map +1 -0
  42. package/dist/generative-ui/standard/map-helpers.d.cts +88 -0
  43. package/dist/generative-ui/standard/map-helpers.d.cts.map +1 -0
  44. package/dist/generative-ui/standard/map-helpers.d.mts +88 -0
  45. package/dist/generative-ui/standard/map-helpers.d.mts.map +1 -0
  46. package/dist/generative-ui/standard/map-helpers.mjs +95 -0
  47. package/dist/generative-ui/standard/map-helpers.mjs.map +1 -0
  48. package/dist/generative-ui/standard/media.d.cts +6 -6
  49. package/dist/generative-ui/standard/media.d.mts +6 -6
  50. package/dist/generative-ui/standard/style-helpers.cjs +151 -0
  51. package/dist/generative-ui/standard/style-helpers.cjs.map +1 -0
  52. package/dist/generative-ui/standard/style-helpers.d.cts +56 -0
  53. package/dist/generative-ui/standard/style-helpers.d.cts.map +1 -0
  54. package/dist/generative-ui/standard/style-helpers.d.mts +56 -0
  55. package/dist/generative-ui/standard/style-helpers.d.mts.map +1 -0
  56. package/dist/generative-ui/standard/style-helpers.mjs +142 -0
  57. package/dist/generative-ui/standard/style-helpers.mjs.map +1 -0
  58. package/dist/generative-ui/standard/text.d.cts +14 -14
  59. package/dist/generative-ui/standard/text.d.mts +14 -14
  60. package/dist/generative-ui/standard/url.cjs +54 -0
  61. package/dist/generative-ui/standard/url.cjs.map +1 -0
  62. package/dist/generative-ui/standard/url.d.cts +32 -0
  63. package/dist/generative-ui/standard/url.d.cts.map +1 -0
  64. package/dist/generative-ui/standard/url.d.mts +32 -0
  65. package/dist/generative-ui/standard/url.d.mts.map +1 -0
  66. package/dist/generative-ui/standard/url.mjs +50 -0
  67. package/dist/generative-ui/standard/url.mjs.map +1 -0
  68. package/dist/generative-ui/thrown.cjs +69 -0
  69. package/dist/generative-ui/thrown.cjs.map +1 -0
  70. package/dist/generative-ui/thrown.d.cts +21 -0
  71. package/dist/generative-ui/thrown.d.cts.map +1 -0
  72. package/dist/generative-ui/thrown.d.mts +21 -0
  73. package/dist/generative-ui/thrown.d.mts.map +1 -0
  74. package/dist/generative-ui/thrown.mjs +67 -0
  75. package/dist/generative-ui/thrown.mjs.map +1 -0
  76. package/dist/index.cjs +2 -0
  77. package/dist/index.cjs.map +1 -1
  78. package/dist/index.d.cts +2 -1
  79. package/dist/index.d.cts.map +1 -1
  80. package/dist/index.d.mts +2 -1
  81. package/dist/index.d.mts.map +1 -1
  82. package/dist/index.mjs +2 -1
  83. package/dist/index.mjs.map +1 -1
  84. package/dist/index.umd.js +368 -2
  85. package/dist/index.umd.js.map +1 -1
  86. package/dist/package.cjs +1 -1
  87. package/dist/package.mjs +1 -1
  88. package/package.json +1 -1
  89. package/src/__tests__/schema-render.test.ts +78 -0
  90. package/src/generative-ui/__tests__/clone-value-differential.test.ts +480 -0
  91. package/src/generative-ui/__tests__/clone-value.test.ts +226 -0
  92. package/src/generative-ui/clone-value.ts +511 -0
  93. package/src/generative-ui/index.ts +6 -2
  94. package/src/generative-ui/schema-render.ts +2 -1
  95. package/src/generative-ui/standard/__tests__/standard-helpers.test.ts +407 -0
  96. package/src/generative-ui/standard/chart-config.ts +434 -0
  97. package/src/generative-ui/standard/index.ts +36 -0
  98. package/src/generative-ui/standard/map-helpers.ts +155 -0
  99. package/src/generative-ui/standard/style-helpers.ts +170 -0
  100. package/src/generative-ui/standard/url.ts +51 -0
  101. package/src/generative-ui/thrown.ts +90 -0
  102. package/src/index.ts +5 -0
@@ -0,0 +1,407 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { defined, layoutStyle, px, radiusStyle } from "../style-helpers";
3
+ import { faviconSrc, hostOf, openUrl, safeHref } from "../url";
4
+ import {
5
+ chartConfig,
6
+ makeFormatter,
7
+ normalizeSeries,
8
+ resolveCssColor,
9
+ withAlpha,
10
+ } from "../chart-config";
11
+ import {
12
+ guardNumericFilter,
13
+ guardStyleFilters,
14
+ isMapLibre,
15
+ isValidMarker,
16
+ } from "../map-helpers";
17
+
18
+ /**
19
+ * The shared tests run in node, which has no DOM. The chart helpers only
20
+ * pass the element to `getComputedStyle`, which these tests stub or leave
21
+ * out, so an empty object stands in for it.
22
+ */
23
+ function stubElement() {
24
+ return {} as Element;
25
+ }
26
+
27
+ /** Stubs `getComputedStyle` with fixed custom property values. */
28
+ function stubCustomProperties(values: Record<string, string>) {
29
+ vi.stubGlobal("getComputedStyle", () => ({
30
+ getPropertyValue: (name: string) => values[name] ?? "",
31
+ }));
32
+ }
33
+
34
+ afterEach(() => {
35
+ vi.unstubAllGlobals();
36
+ });
37
+
38
+ describe("layoutStyle", () => {
39
+ it("maps the layout props to flex container CSS and leaves out unset props", () => {
40
+ const style = layoutStyle({
41
+ align: "center",
42
+ justify: "spaceBetween",
43
+ padding: 2,
44
+ });
45
+ expect(style).toEqual({
46
+ display: "flex",
47
+ alignItems: "center",
48
+ justifyContent: "space-between",
49
+ padding: "calc(var(--spacing, 0.25rem) * 2)",
50
+ minWidth: 0,
51
+ maxWidth: "100%",
52
+ boxSizing: "border-box",
53
+ });
54
+ });
55
+
56
+ it("removes the padding for flush and sets overflow for scrollable and clip", () => {
57
+ expect(layoutStyle({ padding: 4, flush: true })).toMatchObject({
58
+ padding: 0,
59
+ });
60
+ expect(layoutStyle({ scrollable: true, clip: true })).toMatchObject({
61
+ overflow: "auto",
62
+ });
63
+ expect(layoutStyle({ clip: true })).toMatchObject({ overflow: "hidden" });
64
+ });
65
+
66
+ it("lets rowGap and columnGap win over gap, with no shorthand to override them", () => {
67
+ // A renderer can apply a style object in sorted key order, so a `gap`
68
+ // next to `columnGap` would be written last and erase it.
69
+ const both = layoutStyle({ gap: 2, columnGap: 6 });
70
+ expect(both).toMatchObject({
71
+ rowGap: "calc(var(--spacing, 0.25rem) * 2)",
72
+ columnGap: "calc(var(--spacing, 0.25rem) * 6)",
73
+ });
74
+ expect(both).not.toHaveProperty("gap");
75
+ const rowOnly = layoutStyle({ rowGap: 1 });
76
+ expect(rowOnly).toMatchObject({
77
+ rowGap: "calc(var(--spacing, 0.25rem) * 1)",
78
+ });
79
+ expect(rowOnly).not.toHaveProperty("columnGap");
80
+ });
81
+
82
+ it("keeps the gap shorthand when no longhand is set", () => {
83
+ expect(layoutStyle({ gap: 3 })).toMatchObject({
84
+ gap: "calc(var(--spacing, 0.25rem) * 3)",
85
+ });
86
+ expect(layoutStyle({ disableAutoSpacing: true })).toMatchObject({ gap: 0 });
87
+ expect(layoutStyle({})).not.toHaveProperty("gap");
88
+ });
89
+ });
90
+
91
+ describe("radiusStyle", () => {
92
+ it("gives one border radius for a token or a number", () => {
93
+ expect(radiusStyle("lg")).toEqual({
94
+ borderRadius: "var(--a2ui-radius-lg, 12px)",
95
+ });
96
+ expect(radiusStyle(8)).toEqual({ borderRadius: "8px" });
97
+ });
98
+
99
+ it("gives one radius for each corner of a corner object", () => {
100
+ expect(
101
+ radiusStyle({
102
+ topLeft: "lg",
103
+ topRight: "2px",
104
+ bottomLeft: "full",
105
+ bottomRight: "0",
106
+ }),
107
+ ).toEqual({
108
+ borderTopLeftRadius: "var(--a2ui-radius-lg, 12px)",
109
+ borderTopRightRadius: "2px",
110
+ borderBottomLeftRadius: "9999px",
111
+ borderBottomRightRadius: "0",
112
+ });
113
+ });
114
+
115
+ it("gives no CSS when the radius is not set", () => {
116
+ expect(radiusStyle(undefined)).toEqual({});
117
+ });
118
+ });
119
+
120
+ describe("px and defined", () => {
121
+ it("gives pixels for a number and passes a string through", () => {
122
+ expect(px(12)).toBe("12px");
123
+ expect(px("1rem")).toBe("1rem");
124
+ expect(px(undefined)).toBeUndefined();
125
+ });
126
+
127
+ it("drops undefined entries and keeps zero", () => {
128
+ expect(defined({ margin: 0, padding: undefined, color: "red" })).toEqual({
129
+ margin: 0,
130
+ color: "red",
131
+ });
132
+ });
133
+ });
134
+
135
+ describe("safeHref", () => {
136
+ it.each([
137
+ [" https://example.com/a ", "https://example.com/a"],
138
+ ["http://example.com", "http://example.com"],
139
+ ["HTTPS://EXAMPLE.COM", "HTTPS://EXAMPLE.COM"],
140
+ ["mailto:team@example.com", "mailto:team@example.com"],
141
+ ])("accepts %s", (url, expected) => {
142
+ expect(safeHref(url)).toBe(expected);
143
+ });
144
+
145
+ it.each(["javascript:alert(1)", " data:text/html,hi", "/relative/path"])(
146
+ "rejects %s",
147
+ (url) => {
148
+ expect(safeHref(url)).toBeUndefined();
149
+ },
150
+ );
151
+ });
152
+
153
+ describe("hostOf and faviconSrc", () => {
154
+ it("reads the host of a URL with or without a scheme", () => {
155
+ expect(hostOf("https://docs.example.com/page?q=1")).toBe(
156
+ "docs.example.com",
157
+ );
158
+ expect(hostOf("example.com/page")).toBe("example.com");
159
+ });
160
+
161
+ it("gives undefined for text that is not a URL", () => {
162
+ expect(hostOf("")).toBeUndefined();
163
+ expect(hostOf("http://")).toBeUndefined();
164
+ });
165
+
166
+ it("builds the favicon URL with the host encoded", () => {
167
+ expect(faviconSrc("a b.com")).toBe(
168
+ "https://www.google.com/s2/favicons?domain=a%20b.com&sz=64",
169
+ );
170
+ });
171
+ });
172
+
173
+ describe("openUrl", () => {
174
+ it("opens a safe URL in a new tab without an opener", () => {
175
+ const open = vi.fn();
176
+ vi.stubGlobal("window", { open });
177
+ openUrl(" https://example.com ");
178
+ expect(open).toHaveBeenCalledWith(
179
+ "https://example.com",
180
+ "_blank",
181
+ "noopener,noreferrer",
182
+ );
183
+ });
184
+
185
+ it("ignores an unsafe scheme", () => {
186
+ const open = vi.fn();
187
+ vi.stubGlobal("window", { open });
188
+ openUrl("javascript:alert(1)");
189
+ expect(open).not.toHaveBeenCalled();
190
+ });
191
+
192
+ it("does nothing on the server, where there is no window", () => {
193
+ expect(() => openUrl("https://example.com")).not.toThrow();
194
+ });
195
+ });
196
+
197
+ describe("chartConfig", () => {
198
+ it("maps a bar chart to chart.js labels, datasets and colors", () => {
199
+ const config = chartConfig(
200
+ {
201
+ xKey: "month",
202
+ data: [
203
+ { month: "Jan", cost: 290 },
204
+ { month: "Feb", cost: 580 },
205
+ ],
206
+ series: [{ key: "cost", label: "Monthly cost" }],
207
+ },
208
+ stubElement(),
209
+ );
210
+ expect(config).toMatchObject({
211
+ type: "bar",
212
+ data: {
213
+ labels: ["Jan", "Feb"],
214
+ datasets: [
215
+ {
216
+ label: "Monthly cost",
217
+ data: [290, 580],
218
+ // Without a computed style, a color token falls back to its default.
219
+ backgroundColor: "#7fb0f2",
220
+ borderRadius: 4,
221
+ },
222
+ ],
223
+ },
224
+ options: { indexAxis: "x" },
225
+ });
226
+ expect(config.plugins).toBeUndefined();
227
+ });
228
+
229
+ it("maps a pie chart to one dataset with a palette color for each slice", () => {
230
+ const config = chartConfig(
231
+ {
232
+ chartType: "pie",
233
+ xKey: "name",
234
+ data: [
235
+ { name: "A", share: 0.6 },
236
+ { name: "B", share: 0.4 },
237
+ ],
238
+ },
239
+ stubElement(),
240
+ );
241
+ expect(config).toMatchObject({
242
+ type: "pie",
243
+ data: {
244
+ labels: ["A", "B"],
245
+ datasets: [
246
+ {
247
+ label: "share",
248
+ data: [0.6, 0.4],
249
+ backgroundColor: ["#7fb0f2", "#2f6fd1"],
250
+ },
251
+ ],
252
+ },
253
+ options: { cutout: 0, radius: "80%", circumference: 360 },
254
+ });
255
+ });
256
+
257
+ it("paints with the color that the element's custom property sets", () => {
258
+ stubCustomProperties({ "--a2ui-chart-1": "#ff0000" });
259
+ const config = chartConfig(
260
+ { xKey: "q", data: [{ q: "Q1", a: 1 }] },
261
+ stubElement(),
262
+ );
263
+ expect(config).toMatchObject({
264
+ data: { datasets: [{ backgroundColor: "#ff0000" }] },
265
+ });
266
+ });
267
+
268
+ it("adds the value labels plugin only when showValueLabels is set", () => {
269
+ const config = chartConfig(
270
+ { xKey: "q", data: [{ q: "Q1", a: 1 }], showValueLabels: true },
271
+ stubElement(),
272
+ );
273
+ expect(config).toMatchObject({ plugins: [{ id: "a2uiValueLabels" }] });
274
+ });
275
+ });
276
+
277
+ describe("normalizeSeries", () => {
278
+ it("charts every number field except the x key when no series is set", () => {
279
+ expect(
280
+ normalizeSeries(
281
+ undefined,
282
+ [{ day: "Mon", food: 3, rent: 10, note: "x" }],
283
+ "day",
284
+ ),
285
+ ).toEqual([
286
+ { key: "food", label: "food", color: "var(--a2ui-chart-1, #7fb0f2)" },
287
+ { key: "rent", label: "rent", color: "var(--a2ui-chart-2, #2f6fd1)" },
288
+ ]);
289
+ });
290
+ });
291
+
292
+ describe("makeFormatter", () => {
293
+ it("gives no formatter when no format is set", () => {
294
+ expect(makeFormatter(undefined, undefined)).toBeUndefined();
295
+ });
296
+
297
+ it("formats currency in USD by default, compact from 10,000", () => {
298
+ const format = makeFormatter("currency", undefined)!;
299
+ expect(format(290)).toBe("$290");
300
+ expect(format(16_000)).toBe("$16K");
301
+ });
302
+
303
+ it("formats a fraction as a percentage and uses the requested currency", () => {
304
+ expect(makeFormatter("percent", undefined)!(0.25)).toBe("25%");
305
+ expect(makeFormatter("currency", "EUR")!(40)).toBe("€40");
306
+ });
307
+
308
+ it("falls back to plain numbers for an unknown currency code", () => {
309
+ expect(makeFormatter("currency", "not-a-code")!(1290)).toBe("1,290");
310
+ });
311
+
312
+ it("prints a value that is not a number as text", () => {
313
+ const format = makeFormatter("number", undefined)!;
314
+ expect(format("n/a")).toBe("n/a");
315
+ expect(format(null)).toBe("");
316
+ });
317
+ });
318
+
319
+ describe("resolveCssColor and withAlpha", () => {
320
+ it("reads a var() token from the computed style, else its fallback", () => {
321
+ stubCustomProperties({ "--brand": "#123456" });
322
+ expect(resolveCssColor("var(--brand, #000000)", stubElement())).toBe(
323
+ "#123456",
324
+ );
325
+ expect(resolveCssColor("var(--unset, #abcdef)", stubElement())).toBe(
326
+ "#abcdef",
327
+ );
328
+ });
329
+
330
+ it("passes a plain color through", () => {
331
+ expect(resolveCssColor("rebeccapurple", stubElement())).toBe(
332
+ "rebeccapurple",
333
+ );
334
+ });
335
+
336
+ it("adds an alpha channel to a #rrggbb color only", () => {
337
+ expect(withAlpha("#ff0000", 0.2)).toBe("#ff000033");
338
+ expect(withAlpha("red", 0.2)).toBe("red");
339
+ });
340
+ });
341
+
342
+ describe("guardNumericFilter and guardStyleFilters", () => {
343
+ it("guards numeric property comparisons with a has check, at any depth", () => {
344
+ expect(guardNumericFilter([">=", ["get", "ref_length"], 2])).toEqual([
345
+ "all",
346
+ ["has", "ref_length"],
347
+ [">=", ["get", "ref_length"], 2],
348
+ ]);
349
+ expect(
350
+ guardNumericFilter([
351
+ "all",
352
+ ["==", "class", "road"],
353
+ ["<", ["get", "rank"], 3],
354
+ ]),
355
+ ).toEqual([
356
+ "all",
357
+ ["==", "class", "road"],
358
+ ["all", ["has", "rank"], ["<", ["get", "rank"], 3]],
359
+ ]);
360
+ });
361
+
362
+ it("leaves comparisons that do not compare a property with a number", () => {
363
+ expect(guardNumericFilter(["==", ["get", "class"], "road"])).toEqual([
364
+ "==",
365
+ ["get", "class"],
366
+ "road",
367
+ ]);
368
+ expect(guardNumericFilter([">", ["zoom"], 3])).toEqual([">", ["zoom"], 3]);
369
+ });
370
+
371
+ it("guards the filter of each style layer and keeps layers without one", () => {
372
+ const style = {
373
+ layers: [{ id: "a", filter: [">", ["get", "n"], 1] }, { id: "b" }],
374
+ };
375
+ expect(guardStyleFilters(style)).toEqual({
376
+ layers: [
377
+ { id: "a", filter: ["all", ["has", "n"], [">", ["get", "n"], 1]] },
378
+ { id: "b" },
379
+ ],
380
+ });
381
+ });
382
+
383
+ it("returns a style without layers as it is", () => {
384
+ const style: { version: number; layers?: object[] } = { version: 8 };
385
+ expect(guardStyleFilters(style)).toBe(style);
386
+ });
387
+ });
388
+
389
+ describe("isValidMarker and isMapLibre", () => {
390
+ it("accepts a marker with finite coordinates only", () => {
391
+ expect(isValidMarker({ label: "Vienna", lat: 48.2, lng: 16.37 })).toBe(
392
+ true,
393
+ );
394
+ expect(isValidMarker({ label: "Nowhere", lat: Number.NaN, lng: 0 })).toBe(
395
+ false,
396
+ );
397
+ expect(
398
+ isValidMarker({ label: "Far", lat: 0, lng: Number.POSITIVE_INFINITY }),
399
+ ).toBe(false);
400
+ });
401
+
402
+ it("recognizes a module with a Map constructor", () => {
403
+ expect(isMapLibre({ Map: class {} })).toBe(true);
404
+ expect(isMapLibre({ Map: "not a constructor" })).toBe(false);
405
+ expect(isMapLibre(null)).toBe(false);
406
+ });
407
+ });