@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,170 @@
1
+ // Framework-free style helpers of the standard components. Each framework
2
+ // binds the returned style objects to its own elements.
3
+ import {
4
+ SHADOW_SM,
5
+ backgroundColor,
6
+ borderColor,
7
+ radius,
8
+ spacing,
9
+ } from "./tokens";
10
+ import type { ALIGN, JUSTIFY } from "./tokens";
11
+ import type { LayoutProps } from "./layout";
12
+
13
+ /** An inline style object, with camelCase CSS properties as keys. */
14
+ export type StyleMap = Record<string, string | number | undefined>;
15
+
16
+ /**
17
+ * Equal when the JSON is equal. Use it as the equality check of a computed
18
+ * value over agent data, so a new object with the same content does not
19
+ * trigger a redraw.
20
+ */
21
+ export const jsonEqual = (a: unknown, b: unknown) =>
22
+ JSON.stringify(a) === JSON.stringify(b);
23
+
24
+ /** Hides an element on screen. Screen readers still read it. */
25
+ export const VISUALLY_HIDDEN: StyleMap = {
26
+ position: "absolute",
27
+ width: "1px",
28
+ height: "1px",
29
+ padding: 0,
30
+ margin: "-1px",
31
+ overflow: "hidden",
32
+ clipPath: "inset(50%)",
33
+ whiteSpace: "nowrap",
34
+ border: 0,
35
+ };
36
+
37
+ /** The outline of a focused control, for a `:focus-visible` rule. */
38
+ export const FOCUS_RING = "2px solid var(--a2ui-color-focus, #0285ff)";
39
+
40
+ /**
41
+ * Cuts the text after `lines` lines, with an ellipsis.
42
+ *
43
+ * @param lines - The number of lines to show.
44
+ */
45
+ export function lineClamp(lines: number): StyleMap {
46
+ return {
47
+ display: "-webkit-box",
48
+ WebkitLineClamp: lines,
49
+ WebkitBoxOrient: "vertical",
50
+ overflow: "hidden",
51
+ };
52
+ }
53
+
54
+ /** The `align-items` value of each `align` prop value. */
55
+ export const ALIGN_CSS: Record<(typeof ALIGN)[number], string> = {
56
+ start: "flex-start",
57
+ end: "flex-end",
58
+ center: "center",
59
+ baseline: "baseline",
60
+ stretch: "stretch",
61
+ };
62
+
63
+ /** The `justify-content` value of each `justify` prop value. */
64
+ export const JUSTIFY_CSS: Record<(typeof JUSTIFY)[number], string> = {
65
+ start: "flex-start",
66
+ end: "flex-end",
67
+ center: "center",
68
+ spaceBetween: "space-between",
69
+ spaceAround: "space-around",
70
+ spaceEvenly: "space-evenly",
71
+ stretch: "stretch",
72
+ };
73
+
74
+ /**
75
+ * A CSS length: pixels when `value` is a number, the raw CSS when it is a
76
+ * string, and `undefined` when it is not set.
77
+ *
78
+ * @example px(12) // "12px"
79
+ */
80
+ export function px(value: number | string | undefined) {
81
+ if (value === undefined) return undefined;
82
+ return typeof value === "number" ? `${value}px` : value;
83
+ }
84
+
85
+ /**
86
+ * The CSS of a `radius` prop: one radius for all corners, or one radius for
87
+ * each corner.
88
+ *
89
+ * @param value - A radius token, a raw CSS value, or an object of corners.
90
+ */
91
+ export function radiusStyle(value: LayoutProps["radius"]): StyleMap {
92
+ if (typeof value === "string" || typeof value === "number") {
93
+ return { borderRadius: radius(value) };
94
+ }
95
+ if (value === undefined) return {};
96
+ return {
97
+ borderTopLeftRadius: radius(value.topLeft),
98
+ borderTopRightRadius: radius(value.topRight),
99
+ borderBottomLeftRadius: radius(value.bottomLeft),
100
+ borderBottomRightRadius: radius(value.bottomRight),
101
+ };
102
+ }
103
+
104
+ /**
105
+ * A copy of `style` without the undefined entries, so a later spread never
106
+ * erases a value. An undefined longhand next to a shorthand is a removal:
107
+ * clearing `row-gap` resets the `gap` shorthand in real browsers.
108
+ */
109
+ export function defined(style: StyleMap): StyleMap {
110
+ const result: StyleMap = {};
111
+ for (const [key, value] of Object.entries(style)) {
112
+ if (value !== undefined) result[key] = value;
113
+ }
114
+ return result;
115
+ }
116
+
117
+ /**
118
+ * The `gap` of the layout props, with `rowGap` and `columnGap` winning over
119
+ * it. A renderer can apply a style object in sorted key order (Angular does),
120
+ * so `columnGap` would come before the `gap` shorthand and lose to it. A
121
+ * longhand therefore sets both longhands and no shorthand.
122
+ */
123
+ function gapStyle(props: LayoutProps): StyleMap {
124
+ const gap = spacing(props.gap) ?? (props.disableAutoSpacing ? 0 : undefined);
125
+ const rowGap = spacing(props.rowGap);
126
+ const columnGap = spacing(props.columnGap);
127
+ if (rowGap === undefined && columnGap === undefined) return { gap };
128
+ return { rowGap: rowGap ?? gap, columnGap: columnGap ?? gap };
129
+ }
130
+
131
+ /**
132
+ * The flex container CSS of the shared layout props (`align`, `gap`,
133
+ * `padding`, `radius`, sizes and the rest). An unset prop gives no CSS.
134
+ *
135
+ * @param props - The layout props of a container component.
136
+ */
137
+ export function layoutStyle(props: LayoutProps): StyleMap {
138
+ const overflow = props.scrollable
139
+ ? "auto"
140
+ : props.clip
141
+ ? "hidden"
142
+ : undefined;
143
+ return defined({
144
+ display: props.inline ? "inline-flex" : "flex",
145
+ alignItems: props.align ? ALIGN_CSS[props.align] : undefined,
146
+ justifyContent: props.justify ? JUSTIFY_CSS[props.justify] : undefined,
147
+ flexWrap: props.wrap,
148
+ ...gapStyle(props),
149
+ padding: props.flush ? 0 : spacing(props.padding),
150
+ background: backgroundColor(props.background),
151
+ border: props.border
152
+ ? `1px solid ${borderColor(props.borderColor ?? "default")}`
153
+ : undefined,
154
+ ...radiusStyle(props.radius),
155
+ width: spacing(props.width),
156
+ height: spacing(props.height),
157
+ // A container can shrink below its content and never gets wider than
158
+ // its parent.
159
+ minWidth: spacing(props.minWidth ?? props.minSize) ?? 0,
160
+ maxWidth: spacing(props.maxWidth ?? props.maxSize) ?? "100%",
161
+ minHeight: spacing(props.minHeight ?? props.minSize),
162
+ maxHeight: spacing(props.maxHeight ?? props.maxSize),
163
+ flex: props.flex,
164
+ flexGrow: props.weight,
165
+ aspectRatio: props.aspectRatio,
166
+ overflow,
167
+ boxShadow: props.shadow ? SHADOW_SM : undefined,
168
+ boxSizing: "border-box",
169
+ });
170
+ }
@@ -0,0 +1,51 @@
1
+ // Framework-free URL helpers of the standard components.
2
+
3
+ const SAFE_URL = /^(https?:|mailto:)/i;
4
+
5
+ /**
6
+ * The trimmed URL when its scheme is http, https or mailto, else
7
+ * `undefined`. Use it before a URL from agent data becomes an `href`.
8
+ *
9
+ * @example safeHref(" https://example.com ") // "https://example.com"
10
+ */
11
+ export function safeHref(url: string) {
12
+ const trimmed = url.trim();
13
+ return SAFE_URL.test(trimmed) ? trimmed : undefined;
14
+ }
15
+
16
+ /**
17
+ * The host name of a URL, with or without a scheme. `undefined` when the
18
+ * text is not a URL.
19
+ *
20
+ * @example hostOf("example.com/page") // "example.com"
21
+ */
22
+ export function hostOf(url: string) {
23
+ try {
24
+ return new URL(url.includes("://") ? url : `https://${url}`).hostname;
25
+ } catch {
26
+ return undefined;
27
+ }
28
+ }
29
+
30
+ /**
31
+ * The URL of the favicon of a host, from Google's favicon service.
32
+ *
33
+ * @param host - A host name, for example the result of `hostOf`.
34
+ */
35
+ export function faviconSrc(host: string) {
36
+ return `https://www.google.com/s2/favicons?domain=${encodeURIComponent(host)}&sz=64`;
37
+ }
38
+
39
+ /**
40
+ * Opens a URL in a new tab. An unsafe scheme (javascript:, data:) is
41
+ * ignored. Does nothing where there is no `window.open`, for example on
42
+ * the server or on React Native.
43
+ *
44
+ * @param href - The URL to open. `undefined` does nothing.
45
+ */
46
+ export function openUrl(href: string | undefined) {
47
+ if (!href || typeof window === "undefined") return;
48
+ if (typeof window.open !== "function") return; // hermes-ok: feature check
49
+ const safe = safeHref(href);
50
+ if (safe) window.open(safe, "_blank", "noopener,noreferrer"); // hermes-ok: checked above
51
+ }
@@ -0,0 +1,90 @@
1
+ // Turns any thrown value into an Error, for the error boundaries of the
2
+ // framework packages. Framework-free, and safe on Hermes.
3
+
4
+ /**
5
+ * Sets `cause` as an own property. `new Error(message, { cause })` needs
6
+ * ES2022 error options, which an engine without them (the JavaScriptCore of
7
+ * iOS before 15, which React Native 0.70 can still run on) ignores without a
8
+ * word, so it is set by hand on every engine.
9
+ */
10
+ export function withCause(error: Error, cause: unknown): Error {
11
+ Object.defineProperty(error, "cause", {
12
+ value: cause,
13
+ writable: true,
14
+ configurable: true,
15
+ enumerable: false,
16
+ });
17
+ return error;
18
+ }
19
+
20
+ /** The longest description of a thrown object's data. */
21
+ const MAX_DESCRIPTION = 200;
22
+
23
+ /**
24
+ * Reads what a thrown value says without letting the reading throw: a
25
+ * getter, a Proxy trap, a `toJSON` or a `toString` may throw too.
26
+ */
27
+ function attempt<T>(read: () => T): T | undefined {
28
+ try {
29
+ return read();
30
+ } catch {
31
+ return undefined;
32
+ }
33
+ }
34
+
35
+ /** The `message` of a thrown object, when it is a non-empty string. */
36
+ function messageOf(value: unknown): string | undefined {
37
+ if (typeof value !== "object" || value === null) return undefined;
38
+ const message = attempt(() => (value as { message?: unknown }).message);
39
+ return typeof message === "string" && message !== "" ? message : undefined;
40
+ }
41
+
42
+ /** A thrown value as text, for a value that is not an Error. */
43
+ function describeThrown(value: unknown): string {
44
+ if (typeof value === "string") return JSON.stringify(value);
45
+ if (typeof value === "object" && value !== null) {
46
+ // Its data, not "[object Object]".
47
+ const json = attempt(() => JSON.stringify(value));
48
+ if (typeof json === "string") {
49
+ return json.length > MAX_DESCRIPTION
50
+ ? `${json.slice(0, MAX_DESCRIPTION)}…`
51
+ : json;
52
+ }
53
+ }
54
+ return (
55
+ attempt(() => String(value)) ??
56
+ attempt(() => Object.prototype.toString.call(value)) ??
57
+ typeof value
58
+ );
59
+ }
60
+
61
+ /** An Error of this realm, or of another one (a frame, a VM context). */
62
+ function isError(value: unknown): value is Error {
63
+ // `instanceof` runs a Proxy's getPrototypeOf trap, which may throw.
64
+ if (attempt(() => value instanceof Error)) return true;
65
+ // Another realm's Error fails `instanceof`; its tag says what it is. A
66
+ // tag can be set on any object, so it must have a message as well.
67
+ return (
68
+ attempt(() => Object.prototype.toString.call(value)) === "[object Error]" &&
69
+ typeof attempt(() => (value as { message?: unknown }).message) === "string"
70
+ );
71
+ }
72
+
73
+ /**
74
+ * The Error for any thrown value. Code may throw anything: a string, or a
75
+ * falsy value (`undefined`, `null`, `0`, `""`) that a boundary which keeps
76
+ * the thrown value and tests it for truth would take for "no error". An
77
+ * Error comes back as it is; a non-empty string, or the string `message`
78
+ * of a thrown object, becomes the message; any other value is described in
79
+ * the message. A value that is not an Error is kept as the `cause`. This
80
+ * never throws, whatever the value.
81
+ */
82
+ export function toError(value: unknown): Error {
83
+ if (isError(value)) return value;
84
+ const message =
85
+ typeof value === "string" && value !== ""
86
+ ? value
87
+ : (messageOf(value) ??
88
+ `A value that is not an Error was thrown: ${describeThrown(value)}`);
89
+ return withCause(new Error(message), value);
90
+ }
package/src/index.ts CHANGED
@@ -181,3 +181,8 @@ export type { DebugEventEnvelope } from "./debug-event-envelope";
181
181
 
182
182
  export { GENERATE_SANDBOXED_UI_DESCRIPTION } from "./open-generative-ui-prompts";
183
183
  export * from "./generative-ui/base";
184
+ // On the root, not only on `@copilotkit/shared/generative-ui`: the core
185
+ // root entry needs it, and importing the generative UI barrel there would
186
+ // load its validators and catalogs in every app (Metro does not
187
+ // tree-shake), and leave the UMD build without a global for the subpath.
188
+ export { cloneValue } from "./generative-ui/clone-value";