@copilotkit/shared 1.84.2-canary.1793807121 → 1.84.2-canary.openbot-intel-fixes

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 (248) hide show
  1. package/README.md +1 -1
  2. package/dist/a2ui-catalog-schema.cjs +14 -5
  3. package/dist/a2ui-catalog-schema.cjs.map +1 -1
  4. package/dist/a2ui-catalog-schema.d.cts +4 -2
  5. package/dist/a2ui-catalog-schema.d.cts.map +1 -1
  6. package/dist/a2ui-catalog-schema.d.mts +4 -2
  7. package/dist/a2ui-catalog-schema.d.mts.map +1 -1
  8. package/dist/a2ui-catalog-schema.mjs +14 -5
  9. package/dist/a2ui-catalog-schema.mjs.map +1 -1
  10. package/dist/event-transforms/open-generative-ui-middleware.cjs +7 -6
  11. package/dist/event-transforms/open-generative-ui-middleware.cjs.map +1 -1
  12. package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +1 -1
  13. package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +1 -1
  14. package/dist/event-transforms/open-generative-ui-middleware.mjs +7 -6
  15. package/dist/event-transforms/open-generative-ui-middleware.mjs.map +1 -1
  16. package/dist/generative-ui/catalog/catalog-export.cjs +329 -0
  17. package/dist/generative-ui/catalog/catalog-export.cjs.map +1 -0
  18. package/dist/generative-ui/catalog/catalog-export.d.cts +92 -0
  19. package/dist/generative-ui/catalog/catalog-export.d.cts.map +1 -0
  20. package/dist/generative-ui/catalog/catalog-export.d.mts +92 -0
  21. package/dist/generative-ui/catalog/catalog-export.d.mts.map +1 -0
  22. package/dist/generative-ui/catalog/catalog-export.mjs +328 -0
  23. package/dist/generative-ui/catalog/catalog-export.mjs.map +1 -0
  24. package/dist/generative-ui/catalog/catalog.cjs +314 -0
  25. package/dist/generative-ui/catalog/catalog.cjs.map +1 -0
  26. package/dist/generative-ui/catalog/catalog.d.cts +407 -0
  27. package/dist/generative-ui/catalog/catalog.d.cts.map +1 -0
  28. package/dist/generative-ui/catalog/catalog.d.mts +407 -0
  29. package/dist/generative-ui/catalog/catalog.d.mts.map +1 -0
  30. package/dist/generative-ui/catalog/catalog.mjs +311 -0
  31. package/dist/generative-ui/catalog/catalog.mjs.map +1 -0
  32. package/dist/generative-ui/catalog/errors.cjs +13 -0
  33. package/dist/generative-ui/catalog/errors.cjs.map +1 -0
  34. package/dist/generative-ui/catalog/errors.d.cts +46 -0
  35. package/dist/generative-ui/catalog/errors.d.cts.map +1 -0
  36. package/dist/generative-ui/catalog/errors.d.mts +46 -0
  37. package/dist/generative-ui/catalog/errors.d.mts.map +1 -0
  38. package/dist/generative-ui/catalog/errors.mjs +12 -0
  39. package/dist/generative-ui/catalog/errors.mjs.map +1 -0
  40. package/dist/generative-ui/catalog/index.d.mts +6 -0
  41. package/dist/generative-ui/catalog/json-schema.cjs +19 -0
  42. package/dist/generative-ui/catalog/json-schema.cjs.map +1 -0
  43. package/dist/generative-ui/catalog/json-schema.d.cts +13 -0
  44. package/dist/generative-ui/catalog/json-schema.d.cts.map +1 -0
  45. package/dist/generative-ui/catalog/json-schema.d.mts +13 -0
  46. package/dist/generative-ui/catalog/json-schema.d.mts.map +1 -0
  47. package/dist/generative-ui/catalog/json-schema.mjs +18 -0
  48. package/dist/generative-ui/catalog/json-schema.mjs.map +1 -0
  49. package/dist/generative-ui/catalog/refs.cjs +20 -0
  50. package/dist/generative-ui/catalog/refs.cjs.map +1 -0
  51. package/dist/generative-ui/catalog/refs.d.cts +54 -0
  52. package/dist/generative-ui/catalog/refs.d.cts.map +1 -0
  53. package/dist/generative-ui/catalog/refs.d.mts +54 -0
  54. package/dist/generative-ui/catalog/refs.d.mts.map +1 -0
  55. package/dist/generative-ui/catalog/refs.mjs +18 -0
  56. package/dist/generative-ui/catalog/refs.mjs.map +1 -0
  57. package/dist/generative-ui/catalog/schema.cjs +74 -0
  58. package/dist/generative-ui/catalog/schema.cjs.map +1 -0
  59. package/dist/generative-ui/catalog/schema.d.cts +43 -0
  60. package/dist/generative-ui/catalog/schema.d.cts.map +1 -0
  61. package/dist/generative-ui/catalog/schema.d.mts +43 -0
  62. package/dist/generative-ui/catalog/schema.d.mts.map +1 -0
  63. package/dist/generative-ui/catalog/schema.mjs +68 -0
  64. package/dist/generative-ui/catalog/schema.mjs.map +1 -0
  65. package/dist/generative-ui/index.cjs +247 -0
  66. package/dist/generative-ui/index.d.cts +33 -0
  67. package/dist/generative-ui/index.d.mts +35 -0
  68. package/dist/generative-ui/index.mjs +34 -0
  69. package/dist/generative-ui/is-record.cjs +6 -1
  70. package/dist/generative-ui/is-record.cjs.map +1 -1
  71. package/dist/generative-ui/is-record.d.cts +6 -0
  72. package/dist/generative-ui/is-record.d.cts.map +1 -0
  73. package/dist/generative-ui/is-record.d.mts +6 -0
  74. package/dist/generative-ui/is-record.d.mts.map +1 -0
  75. package/dist/generative-ui/is-record.mjs +6 -2
  76. package/dist/generative-ui/is-record.mjs.map +1 -1
  77. package/dist/generative-ui/standard/chart.cjs +56 -0
  78. package/dist/generative-ui/standard/chart.cjs.map +1 -0
  79. package/dist/generative-ui/standard/chart.d.cts +142 -0
  80. package/dist/generative-ui/standard/chart.d.cts.map +1 -0
  81. package/dist/generative-ui/standard/chart.d.mts +142 -0
  82. package/dist/generative-ui/standard/chart.d.mts.map +1 -0
  83. package/dist/generative-ui/standard/chart.mjs +54 -0
  84. package/dist/generative-ui/standard/chart.mjs.map +1 -0
  85. package/dist/generative-ui/standard/checks.cjs +74 -0
  86. package/dist/generative-ui/standard/checks.cjs.map +1 -0
  87. package/dist/generative-ui/standard/checks.d.cts +74 -0
  88. package/dist/generative-ui/standard/checks.d.cts.map +1 -0
  89. package/dist/generative-ui/standard/checks.d.mts +74 -0
  90. package/dist/generative-ui/standard/checks.d.mts.map +1 -0
  91. package/dist/generative-ui/standard/checks.mjs +71 -0
  92. package/dist/generative-ui/standard/checks.mjs.map +1 -0
  93. package/dist/generative-ui/standard/components.cjs +23 -0
  94. package/dist/generative-ui/standard/components.cjs.map +1 -0
  95. package/dist/generative-ui/standard/components.d.cts +24 -0
  96. package/dist/generative-ui/standard/components.d.cts.map +1 -0
  97. package/dist/generative-ui/standard/components.d.mts +24 -0
  98. package/dist/generative-ui/standard/components.d.mts.map +1 -0
  99. package/dist/generative-ui/standard/components.mjs +22 -0
  100. package/dist/generative-ui/standard/components.mjs.map +1 -0
  101. package/dist/generative-ui/standard/composites.cjs +143 -0
  102. package/dist/generative-ui/standard/composites.cjs.map +1 -0
  103. package/dist/generative-ui/standard/composites.d.cts +500 -0
  104. package/dist/generative-ui/standard/composites.d.cts.map +1 -0
  105. package/dist/generative-ui/standard/composites.d.mts +500 -0
  106. package/dist/generative-ui/standard/composites.d.mts.map +1 -0
  107. package/dist/generative-ui/standard/composites.mjs +132 -0
  108. package/dist/generative-ui/standard/composites.mjs.map +1 -0
  109. package/dist/generative-ui/standard/data.cjs +176 -0
  110. package/dist/generative-ui/standard/data.cjs.map +1 -0
  111. package/dist/generative-ui/standard/data.d.cts +745 -0
  112. package/dist/generative-ui/standard/data.d.cts.map +1 -0
  113. package/dist/generative-ui/standard/data.d.mts +745 -0
  114. package/dist/generative-ui/standard/data.d.mts.map +1 -0
  115. package/dist/generative-ui/standard/data.mjs +167 -0
  116. package/dist/generative-ui/standard/data.mjs.map +1 -0
  117. package/dist/generative-ui/standard/declarations.cjs +40 -0
  118. package/dist/generative-ui/standard/declarations.cjs.map +1 -0
  119. package/dist/generative-ui/standard/declarations.d.cts +4604 -0
  120. package/dist/generative-ui/standard/declarations.d.cts.map +1 -0
  121. package/dist/generative-ui/standard/declarations.d.mts +4605 -0
  122. package/dist/generative-ui/standard/declarations.d.mts.map +1 -0
  123. package/dist/generative-ui/standard/declarations.mjs +39 -0
  124. package/dist/generative-ui/standard/declarations.mjs.map +1 -0
  125. package/dist/generative-ui/standard/feedback.cjs +97 -0
  126. package/dist/generative-ui/standard/feedback.cjs.map +1 -0
  127. package/dist/generative-ui/standard/feedback.d.cts +227 -0
  128. package/dist/generative-ui/standard/feedback.d.cts.map +1 -0
  129. package/dist/generative-ui/standard/feedback.d.mts +227 -0
  130. package/dist/generative-ui/standard/feedback.d.mts.map +1 -0
  131. package/dist/generative-ui/standard/feedback.mjs +90 -0
  132. package/dist/generative-ui/standard/feedback.mjs.map +1 -0
  133. package/dist/generative-ui/standard/index.d.mts +16 -0
  134. package/dist/generative-ui/standard/inputs.cjs +333 -0
  135. package/dist/generative-ui/standard/inputs.cjs.map +1 -0
  136. package/dist/generative-ui/standard/inputs.d.cts +2658 -0
  137. package/dist/generative-ui/standard/inputs.d.cts.map +1 -0
  138. package/dist/generative-ui/standard/inputs.d.mts +2658 -0
  139. package/dist/generative-ui/standard/inputs.d.mts.map +1 -0
  140. package/dist/generative-ui/standard/inputs.mjs +318 -0
  141. package/dist/generative-ui/standard/inputs.mjs.map +1 -0
  142. package/dist/generative-ui/standard/layout.cjs +289 -0
  143. package/dist/generative-ui/standard/layout.cjs.map +1 -0
  144. package/dist/generative-ui/standard/layout.d.cts +2854 -0
  145. package/dist/generative-ui/standard/layout.d.cts.map +1 -0
  146. package/dist/generative-ui/standard/layout.d.mts +2854 -0
  147. package/dist/generative-ui/standard/layout.d.mts.map +1 -0
  148. package/dist/generative-ui/standard/layout.mjs +269 -0
  149. package/dist/generative-ui/standard/layout.mjs.map +1 -0
  150. package/dist/generative-ui/standard/map.cjs +27 -0
  151. package/dist/generative-ui/standard/map.cjs.map +1 -0
  152. package/dist/generative-ui/standard/map.d.cts +93 -0
  153. package/dist/generative-ui/standard/map.d.cts.map +1 -0
  154. package/dist/generative-ui/standard/map.d.mts +93 -0
  155. package/dist/generative-ui/standard/map.d.mts.map +1 -0
  156. package/dist/generative-ui/standard/map.mjs +25 -0
  157. package/dist/generative-ui/standard/map.mjs.map +1 -0
  158. package/dist/generative-ui/standard/media.cjs +215 -0
  159. package/dist/generative-ui/standard/media.cjs.map +1 -0
  160. package/dist/generative-ui/standard/media.d.cts +968 -0
  161. package/dist/generative-ui/standard/media.d.cts.map +1 -0
  162. package/dist/generative-ui/standard/media.d.mts +968 -0
  163. package/dist/generative-ui/standard/media.d.mts.map +1 -0
  164. package/dist/generative-ui/standard/media.mjs +205 -0
  165. package/dist/generative-ui/standard/media.mjs.map +1 -0
  166. package/dist/generative-ui/standard/parameter-order.cjs +231 -0
  167. package/dist/generative-ui/standard/parameter-order.cjs.map +1 -0
  168. package/dist/generative-ui/standard/parameter-order.d.cts +84 -0
  169. package/dist/generative-ui/standard/parameter-order.d.cts.map +1 -0
  170. package/dist/generative-ui/standard/parameter-order.d.mts +84 -0
  171. package/dist/generative-ui/standard/parameter-order.d.mts.map +1 -0
  172. package/dist/generative-ui/standard/parameter-order.mjs +230 -0
  173. package/dist/generative-ui/standard/parameter-order.mjs.map +1 -0
  174. package/dist/generative-ui/standard/styles.cjs +62 -0
  175. package/dist/generative-ui/standard/styles.cjs.map +1 -0
  176. package/dist/generative-ui/standard/styles.d.cts +17 -0
  177. package/dist/generative-ui/standard/styles.d.cts.map +1 -0
  178. package/dist/generative-ui/standard/styles.d.mts +17 -0
  179. package/dist/generative-ui/standard/styles.d.mts.map +1 -0
  180. package/dist/generative-ui/standard/styles.mjs +60 -0
  181. package/dist/generative-ui/standard/styles.mjs.map +1 -0
  182. package/dist/generative-ui/standard/text.cjs +226 -0
  183. package/dist/generative-ui/standard/text.cjs.map +1 -0
  184. package/dist/generative-ui/standard/text.d.cts +687 -0
  185. package/dist/generative-ui/standard/text.d.cts.map +1 -0
  186. package/dist/generative-ui/standard/text.d.mts +687 -0
  187. package/dist/generative-ui/standard/text.d.mts.map +1 -0
  188. package/dist/generative-ui/standard/text.mjs +209 -0
  189. package/dist/generative-ui/standard/text.mjs.map +1 -0
  190. package/dist/generative-ui/standard/theme.cjs +115 -0
  191. package/dist/generative-ui/standard/theme.cjs.map +1 -0
  192. package/dist/generative-ui/standard/theme.d.cts +60 -0
  193. package/dist/generative-ui/standard/theme.d.cts.map +1 -0
  194. package/dist/generative-ui/standard/theme.d.mts +60 -0
  195. package/dist/generative-ui/standard/theme.d.mts.map +1 -0
  196. package/dist/generative-ui/standard/theme.mjs +112 -0
  197. package/dist/generative-ui/standard/theme.mjs.map +1 -0
  198. package/dist/generative-ui/standard/tokens.cjs +336 -0
  199. package/dist/generative-ui/standard/tokens.cjs.map +1 -0
  200. package/dist/generative-ui/standard/tokens.d.cts +76 -0
  201. package/dist/generative-ui/standard/tokens.d.cts.map +1 -0
  202. package/dist/generative-ui/standard/tokens.d.mts +76 -0
  203. package/dist/generative-ui/standard/tokens.d.mts.map +1 -0
  204. package/dist/generative-ui/standard/tokens.mjs +305 -0
  205. package/dist/generative-ui/standard/tokens.mjs.map +1 -0
  206. package/dist/index.cjs.map +1 -1
  207. package/dist/index.d.cts +10 -10
  208. package/dist/index.d.mts +10 -10
  209. package/dist/index.mjs.map +1 -1
  210. package/dist/index.umd.js +16 -7
  211. package/dist/index.umd.js.map +1 -1
  212. package/dist/package.cjs +1 -1
  213. package/dist/package.mjs +1 -1
  214. package/package.json +7 -3
  215. package/src/__tests__/a2ui-catalog-schema.test.ts +26 -0
  216. package/src/a2ui-catalog-schema.ts +14 -5
  217. package/src/event-transforms/open-generative-ui-middleware.ts +19 -10
  218. package/src/generative-ui/base.ts +9 -0
  219. package/src/generative-ui/catalog/catalog-export.ts +478 -0
  220. package/src/generative-ui/catalog/catalog.ts +1166 -0
  221. package/src/generative-ui/catalog/errors.ts +102 -0
  222. package/src/generative-ui/catalog/index.ts +6 -0
  223. package/src/generative-ui/catalog/json-schema.ts +23 -0
  224. package/src/generative-ui/catalog/refs.ts +86 -0
  225. package/src/generative-ui/catalog/schema.ts +107 -0
  226. package/src/generative-ui/index.ts +7 -0
  227. package/src/generative-ui/is-record.ts +6 -1
  228. package/src/generative-ui/standard/__tests__/theme.test.ts +61 -0
  229. package/src/generative-ui/standard/__tests__/tokens.test.ts +139 -0
  230. package/src/generative-ui/standard/chart.ts +79 -0
  231. package/src/generative-ui/standard/checks.ts +98 -0
  232. package/src/generative-ui/standard/components.ts +35 -0
  233. package/src/generative-ui/standard/composites.ts +158 -0
  234. package/src/generative-ui/standard/data.ts +213 -0
  235. package/src/generative-ui/standard/declarations.ts +57 -0
  236. package/src/generative-ui/standard/feedback.ts +105 -0
  237. package/src/generative-ui/standard/index.ts +19 -0
  238. package/src/generative-ui/standard/inputs.ts +423 -0
  239. package/src/generative-ui/standard/layout.ts +312 -0
  240. package/src/generative-ui/standard/map.ts +32 -0
  241. package/src/generative-ui/standard/media.ts +265 -0
  242. package/src/generative-ui/standard/parameter-order.ts +155 -0
  243. package/src/generative-ui/standard/styles.ts +79 -0
  244. package/src/generative-ui/standard/text.ts +210 -0
  245. package/src/generative-ui/standard/theme.ts +198 -0
  246. package/src/generative-ui/standard/tokens.ts +317 -0
  247. package/src/index.ts +1 -1
  248. package/tsdown.config.ts +1 -0
@@ -0,0 +1,155 @@
1
+ import type { GenerativeUIComponentDeclaration } from "./components";
2
+ import { toJsonSchema } from "../catalog/json-schema";
3
+
4
+ /** CopilotKit catalog design, not an A2UI-mandated list of signatures.
5
+ * Express maps positional arguments using the exported JSON Schema property order:
6
+ * https://a2ui.org/specification/proposals/express/a2ui_express/#schema-driven-key-mapping
7
+ * Keep these prefixes stable. Optional styling is best passed by name.
8
+ */
9
+ export const STANDARD_ARGUMENT_PREFIXES = {
10
+ Box: ["children"],
11
+ Column: ["children"],
12
+ Row: ["children"],
13
+ Grid: ["children", "columns"],
14
+ GridItem: ["children"],
15
+ Card: ["children", "child"],
16
+ Flow: ["children"],
17
+ FlowItem: ["children"],
18
+ Carousel: ["children", "activeIndex"],
19
+ CarouselItem: ["children"],
20
+ CarouselMediaItem: ["url", "children", "onClick"],
21
+ Spacer: ["minSize"],
22
+ Divider: ["axis"],
23
+ Tabs: ["tabs", "selectedIndex"],
24
+ Title: ["text"],
25
+ Text: ["text", "children"],
26
+ Caption: ["text"],
27
+ Label: ["text", "htmlFor"],
28
+ Bold: ["text"],
29
+ Italic: ["text"],
30
+ Underline: ["text"],
31
+ Strikethrough: ["text"],
32
+ Badge: ["label"],
33
+ Blockquote: ["text"],
34
+ Code: ["text", "language"],
35
+ Markdown: ["text"],
36
+ Math: ["value"],
37
+ ShimmerText: ["text"],
38
+ Link: ["text", "href"],
39
+ Tooltip: ["trigger", "content"],
40
+ Button: ["label", "onClick"],
41
+ Pressable: ["children", "onClick", "href"],
42
+ Form: ["children", "onSubmit"],
43
+ TextField: ["label", "value"],
44
+ TextArea: ["label", "value"],
45
+ Select: ["label", "options", "value"],
46
+ CheckBox: ["label", "value"],
47
+ Radio: ["label", "value"],
48
+ RadioGroup: ["label", "options", "value"],
49
+ Slider: ["label", "value", "min", "max"],
50
+ SegmentedControl: ["label", "options", "value"],
51
+ ChoicePicker: ["label", "options", "value"],
52
+ DateTimeInput: ["label", "value"],
53
+ PulseIndicator: ["label"],
54
+ FlashcardFlip: ["front", "back"],
55
+ Popover: ["children", "open"],
56
+ PopoverTrigger: ["children"],
57
+ PopoverContent: ["children"],
58
+ Modal: ["trigger", "content", "title"],
59
+ Table: ["columns", "rows", "children"],
60
+ TableSection: ["children"],
61
+ TableRow: ["children", "label"],
62
+ TableCell: ["text", "children"],
63
+ List: ["items", "children", "action"],
64
+ ListItem: ["label", "value", "action", "children"],
65
+ Chart: ["data", "xKey", "series", "chartType"],
66
+ Map: ["markers", "selected", "route"],
67
+ Image: ["url", "description", "onOverlayClick"],
68
+ Icon: ["name", "label"],
69
+ Favicon: ["url", "alt"],
70
+ Svg: ["paths", "viewBox"],
71
+ Video: ["url", "title", "captions"],
72
+ AudioPlayer: ["url", "description"],
73
+ HtmlView: ["html", "title"],
74
+ LinkCard: ["href", "title", "onClick"],
75
+ Citation: ["index", "title", "url"],
76
+ FollowUp: ["items", "onFollowUp", "title"],
77
+ EntityCard: ["title", "actions", "onAction", "onClick"],
78
+ ImageGrid: ["images"],
79
+ } as const;
80
+
81
+ const PRESENTATION_PARAMETERS = new Set([
82
+ "align",
83
+ "aspectRatio",
84
+ "background",
85
+ "border",
86
+ "borderColor",
87
+ "clip",
88
+ "color",
89
+ "columnGap",
90
+ "disableAutoSpacing",
91
+ "flex",
92
+ "flush",
93
+ "fontSize",
94
+ "fontWeight",
95
+ "gap",
96
+ "height",
97
+ "highlight",
98
+ "hoverBackground",
99
+ "inline",
100
+ "insetBorder",
101
+ "italic",
102
+ "justify",
103
+ "lineHeight",
104
+ "lineThrough",
105
+ "maxHeight",
106
+ "maxLines",
107
+ "maxSize",
108
+ "maxWidth",
109
+ "minHeight",
110
+ "minLines",
111
+ "minSize",
112
+ "minWidth",
113
+ "padding",
114
+ "pill",
115
+ "preserveWhitespace",
116
+ "radius",
117
+ "role",
118
+ "rowGap",
119
+ "scrollable",
120
+ "shadow",
121
+ "shadowStyle",
122
+ "size",
123
+ "strokeWeight",
124
+ "strong",
125
+ "tabularNums",
126
+ "textAlign",
127
+ "truncate",
128
+ "underline",
129
+ "weight",
130
+ "width",
131
+ "wrap",
132
+ ]);
133
+
134
+ /** Put content/actions before presentation, across props, slots and events. */
135
+ export function standardParameterOrder(
136
+ name: keyof typeof STANDARD_ARGUMENT_PREFIXES,
137
+ definition: GenerativeUIComponentDeclaration,
138
+ ): string[] {
139
+ const keys = [
140
+ ...Object.keys(
141
+ definition.props ? (toJsonSchema(definition.props).properties ?? {}) : {},
142
+ ),
143
+ ...Object.keys(definition.slots ?? {}),
144
+ ...Object.keys(definition.events ?? {}),
145
+ ];
146
+ const prefix: readonly string[] = STANDARD_ARGUMENT_PREFIXES[name];
147
+ const remainder = keys.filter((key) => !prefix.includes(key));
148
+ // Keep unknown prefix keys: createGenerativeUI rejects them, catching a
149
+ // renamed property whose calling convention was not updated.
150
+ return [
151
+ ...prefix,
152
+ ...remainder.filter((key) => !PRESENTATION_PARAMETERS.has(key)),
153
+ ...remainder.filter((key) => PRESENTATION_PARAMETERS.has(key)),
154
+ ];
155
+ }
@@ -0,0 +1,79 @@
1
+ import { DARK_THEME_CSS } from "./theme";
2
+ import { borderColor, textColor, CONTROL_BG, SHADOW_XS } from "./tokens";
3
+
4
+ /**
5
+ * The stylesheet for what inline styles cannot do: pseudo-elements (slider
6
+ * track and thumb, placeholder), `:checked` and `:first-child` states,
7
+ * heading fallbacks, and resets for host prose styles. It is added once per
8
+ * document.
9
+ *
10
+ * Typography is inherited: a surface takes the font, size, line height and
11
+ * color of the text around it (the chat prose). Headings take the heading
12
+ * styles of the host.
13
+ */
14
+
15
+ export const STANDARD_STYLE_ID = "a2ui-standard-catalog";
16
+ export const ACCENT = "var(--a2ui-color-accent, #0d0d0d)";
17
+
18
+ const FOCUS_RING = "2px solid var(--a2ui-color-focus, #0285ff)";
19
+
20
+ const CHECK_SVG =
21
+ "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E\")";
22
+
23
+ const SLIDER_TRACK = "var(--a2ui-color-slider-track, rgba(0, 0, 0, 0.04))";
24
+ const THUMB = `width:20px;height:20px;box-sizing:border-box;border-radius:9999px;background:${CONTROL_BG};border:1px solid ${borderColor("default")};box-shadow:${SHADOW_XS};cursor:pointer`;
25
+
26
+ export const STANDARD_CSS = [
27
+ // The built-in dark values, under a `.dark` class. Every theme rule wins
28
+ // over them.
29
+ DARK_THEME_CSS,
30
+ // The surface (and its root component) never gets wider than the chat column.
31
+ ".a2ui-surface{min-width:0;max-width:100%;box-sizing:border-box}",
32
+ // The surface inherits the font, size, line height and ink of the host.
33
+ ".a2ui-surface{font:inherit;color:inherit}",
34
+ // Heading sizes only for hosts without heading styles (zero specificity).
35
+ ":where(.a2ui-surface) :where(h2){font-size:1.5em;font-weight:600;line-height:1.3}",
36
+ ":where(.a2ui-surface) :where(h3){font-size:1.25em;font-weight:600;line-height:1.3}",
37
+ ":where(.a2ui-surface) :where(h4){font-size:1em;font-weight:600;line-height:1.4}",
38
+ // Controls never fall back to the UA font.
39
+ "[data-a2ui-std] :is(button,input,select,textarea),[data-a2ui-input]{font:inherit;color:inherit}",
40
+ // Key/value rows: the last text of a space-between row sits flush right.
41
+ '[data-a2ui-row="spaceBetween"]>[data-a2ui-text]:last-child:not(:first-child){text-align:right;margin-inline-start:auto}',
42
+ `[data-a2ui-input]:focus-visible{outline:${FOCUS_RING};outline-offset:1px}`,
43
+ // A scrollable container can take keyboard focus, so it shows a focus ring.
44
+ `[data-a2ui-scroll]:focus-visible{outline:${FOCUS_RING};outline-offset:2px}`,
45
+ `[data-a2ui-input]::placeholder{color:${textColor("tertiary")};opacity:1}`,
46
+ // Slider: 24px pill track, 2px inset white thumb (no filled range).
47
+ `.a2ui-std-range{-webkit-appearance:none;appearance:none;display:block;width:100%;height:24px;margin:0;padding:0 2px;box-sizing:border-box;border-radius:9999px;background:${SLIDER_TRACK};cursor:pointer}`,
48
+ ".a2ui-std-range:disabled{cursor:not-allowed;opacity:.5}",
49
+ ".a2ui-std-range::-webkit-slider-runnable-track{height:24px;background:transparent;border:0}",
50
+ `.a2ui-std-range::-webkit-slider-thumb{-webkit-appearance:none;appearance:none;margin-top:2px;${THUMB}}`,
51
+ ".a2ui-std-range::-moz-range-track{height:24px;background:transparent;border:0}",
52
+ `.a2ui-std-range::-moz-range-thumb{${THUMB}}`,
53
+ // Checkbox and radio: 20px round control, filled when checked.
54
+ `.a2ui-std-check{-webkit-appearance:none;appearance:none;flex-shrink:0;width:20px;height:20px;margin:0;box-sizing:border-box;border:1px solid ${borderColor("strong")};border-radius:9999px;background:${CONTROL_BG} center/14px 14px no-repeat;cursor:pointer}`,
55
+ '.a2ui-std-check[data-shape="square"]{border-radius:6px}',
56
+ `.a2ui-std-check:checked{background-color:${ACCENT};border-color:${ACCENT};background-image:${CHECK_SVG}}`,
57
+ `.a2ui-std-check[type="radio"]:checked{background-image:none;background-color:${CONTROL_BG};border:6px solid ${ACCENT}}`,
58
+ ".a2ui-std-check:disabled{cursor:not-allowed}",
59
+ // Hosts often wrap chat markdown in a "prose" stylesheet (Tailwind
60
+ // typography) that adds margins to headings, paragraphs, lists and tables.
61
+ // A surface is an app panel spaced by gaps, so these rules remove those
62
+ // margins. A Markdown component keeps them, because it reads like the chat
63
+ // prose. The selector must be stronger than `.prose :where(h2)`. Hosts
64
+ // change the margins with `--a2ui-heading-margin` and `--a2ui-block-margin`.
65
+ ".a2ui-surface :is(h1,h2,h3,h4,h5,h6):not(.a2ui-markdown *){margin:var(--a2ui-heading-margin,0)}",
66
+ ".a2ui-surface :is(p,table,figure,figcaption,blockquote,pre):not(.a2ui-markdown *){margin:var(--a2ui-block-margin,0)}",
67
+ ".a2ui-markdown>:first-child{margin-top:0}",
68
+ ".a2ui-markdown>:last-child{margin-bottom:0}",
69
+ // Only the List component removes native bullets. Markdown keeps them.
70
+ ".a2ui-surface :is(.a2ui-std-list,.a2ui-std-list>li){margin:0}",
71
+ ".a2ui-surface .a2ui-std-table{font-size:inherit;line-height:inherit}",
72
+ // Table: 8px cells, flush outer edges, hairline row dividers, no outer border.
73
+ ".a2ui-std-table-wrap{width:100%;max-width:100%;min-width:0;overflow-x:auto}",
74
+ ".a2ui-std-cell{padding:8px}",
75
+ ".a2ui-std-cell:first-child{padding-inline-start:0}",
76
+ ".a2ui-std-cell:last-child{padding-inline-end:0}",
77
+ `.a2ui-std-cell[data-divider]{border-bottom:1px solid ${borderColor("subtle")}}`,
78
+ ".a2ui-std-table>tbody:last-child>tr:last-child>.a2ui-std-cell{border-bottom:0}",
79
+ ].join("\n");
@@ -0,0 +1,210 @@
1
+ import { z } from "zod";
2
+ import { CHILDREN_SLOT, sizeValue, weightProp } from "./layout";
3
+ import { TEXT_COLORS, TEXT_SIZES, TITLE_SIZES, WEIGHTS } from "./tokens";
4
+
5
+ // ---- Schemas ----
6
+
7
+ const colorProp = z
8
+ .string()
9
+ .optional()
10
+ .describe(`One of ${TEXT_COLORS.join("|")} or a CSS color`);
11
+ const fontWeight = z.enum(WEIGHTS).optional();
12
+ const textAlign = z.enum(["start", "center", "end"]).optional();
13
+
14
+ const typographyProps = {
15
+ color: colorProp,
16
+ fontWeight,
17
+ textAlign,
18
+ italic: z.boolean().optional(),
19
+ truncate: z.boolean().optional(),
20
+ maxLines: z.number().optional(),
21
+ tabularNums: z.boolean().optional(),
22
+ width: sizeValue.optional(),
23
+ };
24
+
25
+ export const typographySchema = z.object(typographyProps);
26
+ export const titleSchema = z.object({
27
+ ...typographyProps,
28
+ text: z.string().describe("Heading text"),
29
+ size: z.enum(TITLE_SIZES).optional().describe('Default "md"'),
30
+ strong: z.boolean().optional().describe("Default true"),
31
+ });
32
+ export const textSchema = z.object({
33
+ ...typographyProps,
34
+ weight: weightProp,
35
+ text: z
36
+ .union([z.string(), z.number()])
37
+ .optional()
38
+ .describe("Text with inline markdown. A bound number shows as text"),
39
+ size: z.enum(TEXT_SIZES).optional().describe('Default "md"'),
40
+ fontSize: sizeValue
41
+ .optional()
42
+ .describe("Pixels when a number. Wins over size"),
43
+ lineHeight: z
44
+ .union([z.number(), z.string()])
45
+ .optional()
46
+ .describe("A unitless ratio, or CSS"),
47
+ minLines: z
48
+ .number()
49
+ .optional()
50
+ .describe("Reserves the height of this many lines"),
51
+ strong: z.boolean().optional().describe("Default false"),
52
+ underline: z.boolean().optional(),
53
+ lineThrough: z.boolean().optional(),
54
+ inline: z.boolean().optional(),
55
+ preserveWhitespace: z.boolean().optional(),
56
+ highlight: z
57
+ .union([z.enum(["green", "orange", "pink"]), z.boolean()])
58
+ .optional(),
59
+ });
60
+ export const CAPTION_SIZES = ["sm", "md", "lg"] as const;
61
+ export const captionSchema = z.object({
62
+ text: z.union([z.string(), z.number()]).describe("Caption text"),
63
+ color: colorProp,
64
+ size: z.enum(CAPTION_SIZES).optional().describe('Default "lg"'),
65
+ fontWeight: fontWeight.describe('Default "normal"'),
66
+ textAlign,
67
+ truncate: z.boolean().optional(),
68
+ maxLines: z.number().optional(),
69
+ });
70
+ export const labelSchema = z.object({
71
+ text: z.union([z.string(), z.number()]).describe("Label text"),
72
+ htmlFor: z.string().optional(),
73
+ color: colorProp,
74
+ size: z.enum(TEXT_SIZES).optional().describe('Default "sm"'),
75
+ fontWeight: fontWeight.describe('Default "medium"'),
76
+ textAlign,
77
+ truncate: z.boolean().optional(),
78
+ width: sizeValue.optional(),
79
+ });
80
+ export const boldSchema = z.object({ text: z.string(), fontWeight });
81
+ export const inlineTextSchema = z.object({ text: z.string() });
82
+ export const blockquoteSchema = z.object({
83
+ text: z.string().describe("The quoted text, with inline markdown"),
84
+ });
85
+ export const BADGE_SIZES = ["sm", "md", "lg"] as const;
86
+ export const badgeSchema = z.object({
87
+ label: z.string().describe("Badge text"),
88
+ color: z
89
+ .enum(["secondary", "success", "danger", "warning", "info", "discovery"])
90
+ .optional()
91
+ .describe('Default "secondary"'),
92
+ size: z.enum(BADGE_SIZES).optional().describe('Default "md"'),
93
+ pill: z.boolean().optional().describe("Default true"),
94
+ variant: z
95
+ .enum(["soft", "solid", "outline"])
96
+ .optional()
97
+ .describe('Default "soft"'),
98
+ fontWeight: fontWeight.describe('Default "medium"'),
99
+ maxWidth: sizeValue.optional(),
100
+ padding: sizeValue.optional(),
101
+ });
102
+ export const codeSchema = z.object({
103
+ text: z.string().describe("Source code"),
104
+ language: z.string().optional(),
105
+ });
106
+ export const markdownSchema = z.object({
107
+ text: z.string().describe("Markdown source"),
108
+ streaming: z.boolean().optional().describe("Default false"),
109
+ });
110
+ export const mathSchema = z.object({
111
+ value: z.string().describe("TeX source"),
112
+ block: z.boolean().optional().describe("Default false"),
113
+ });
114
+ export const shimmerSchema = z.object({
115
+ ...typographyProps,
116
+ text: z.string(),
117
+ size: z.enum(TEXT_SIZES).optional().describe('Default "md"'),
118
+ lineThrough: z.boolean().optional(),
119
+ });
120
+ export const linkSchema = z.object({
121
+ text: z.string().describe("Link text"),
122
+ href: z.string().describe("URL"),
123
+ title: z.string().optional(),
124
+ });
125
+
126
+ /**
127
+ * The standard text components, without their framework components.
128
+ *
129
+ * - Text has a "multiple" `children` slot. The children render inline
130
+ * after the text.
131
+ * - Tooltip has two "single" slots: `trigger` and `content`.
132
+ * - Link is a plain link. It opens http, https and mailto URLs only.
133
+ */
134
+ export const textDeclarations = {
135
+ Title: {
136
+ description:
137
+ 'A heading in a Card or next to another component; a heading alone is markdown. Size "2xl" at most once per surface, "lg" for sections. "sm" is an h4, "md"/"lg" an h3 (default "md"), "xl" and up an h2. Bold unless strong is false.',
138
+ props: titleSchema,
139
+ },
140
+ Text: {
141
+ description:
142
+ 'Body text in a Card or next to another component; a paragraph alone is markdown outside the fence. Color "default" or "secondary". `text` takes inline markdown (**bold**, *italic*, ~~strike~~, ++underline++, `code`, [link](url)). `children` ids render inline after it, e.g. a Badge.',
143
+ props: textSchema,
144
+ slots: CHILDREN_SLOT,
145
+ },
146
+ Caption: {
147
+ description:
148
+ "Small secondary text: a hint under a control, or a label in a Row. `text` can have inline markdown.",
149
+ props: captionSchema,
150
+ },
151
+ Label: {
152
+ description: "The label of a form field.",
153
+ props: labelSchema,
154
+ },
155
+ Bold: {
156
+ description: "Bold inline text.",
157
+ props: boldSchema,
158
+ },
159
+ Italic: {
160
+ description: "Italic inline text.",
161
+ props: inlineTextSchema,
162
+ },
163
+ Underline: {
164
+ description: "Underlined inline text.",
165
+ props: inlineTextSchema,
166
+ },
167
+ Strikethrough: {
168
+ description: "Struck-through inline text.",
169
+ props: inlineTextSchema,
170
+ },
171
+ Badge: {
172
+ description:
173
+ "A small status pill. Use the colors success, danger and warning only to show a status, not for decoration.",
174
+ props: badgeSchema,
175
+ },
176
+ Blockquote: {
177
+ description:
178
+ "A block quotation with a line on the left. `text` can have inline markdown.",
179
+ props: blockquoteSchema,
180
+ },
181
+ Code: {
182
+ description: "A block of code in a monospace font.",
183
+ props: codeSchema,
184
+ },
185
+ Markdown: {
186
+ description:
187
+ "A block of GitHub-flavored markdown. Use it only next to other components, for example in a table cell or next to a chart. Markdown alone goes outside the fence.",
188
+ props: markdownSchema,
189
+ },
190
+ Math: {
191
+ description:
192
+ "A TeX math formula, typeset with KaTeX. Set block to true for a centered formula on its own line.",
193
+ props: mathSchema,
194
+ },
195
+ ShimmerText: {
196
+ description:
197
+ "Text with a moving shimmer. It shows that something is loading or pending.",
198
+ props: shimmerSchema,
199
+ },
200
+ Link: {
201
+ description:
202
+ "A link. Only http, https and mailto URLs work. It opens in a new tab.",
203
+ props: linkSchema,
204
+ },
205
+ Tooltip: {
206
+ description:
207
+ "Shows `content` when the user points at or focuses `trigger`. `trigger`: one child id, a Text, Icon or Badge (not a Button or Link). `content`: one child id of short text, no links or buttons.",
208
+ slots: { trigger: "single", content: "single" } as const,
209
+ },
210
+ };
@@ -0,0 +1,198 @@
1
+ import type { BACKGROUNDS, BORDER_COLORS, RADII, TEXT_COLORS } from "./tokens";
2
+
3
+ type TextColor = (typeof TEXT_COLORS)[number];
4
+ type BorderColor = (typeof BORDER_COLORS)[number];
5
+ type Background = (typeof BACKGROUNDS)[number];
6
+ type Radius = Exclude<(typeof RADII)[number], "full">;
7
+ type Tint = "info" | "warning" | "caution" | "success" | "danger" | "discovery";
8
+ type ChartSeries = 1 | 2 | 3 | 4 | 5 | 6;
9
+
10
+ /** Every CSS variable that the standard components read. */
11
+ export type GenerativeUIThemeVariable =
12
+ | `--a2ui-color-text-${TextColor}`
13
+ | `--a2ui-color-${Background}`
14
+ | `--a2ui-color-border-${BorderColor}`
15
+ | `--a2ui-color-${Tint}-soft`
16
+ | `--a2ui-radius-${Radius}`
17
+ | `--a2ui-chart-${ChartSeries}`
18
+ | "--a2ui-color-accent"
19
+ | "--a2ui-color-on-accent"
20
+ | "--a2ui-color-panel"
21
+ | "--a2ui-color-control"
22
+ | "--a2ui-color-focus"
23
+ | "--a2ui-color-shimmer"
24
+ | "--a2ui-color-chart-cursor"
25
+ | "--a2ui-color-slider-track"
26
+ | "--a2ui-color-badge-secondary"
27
+ | "--a2ui-color-border-control"
28
+ | "--a2ui-color-highlight-green"
29
+ | "--a2ui-color-highlight-orange"
30
+ | "--a2ui-color-highlight-pink"
31
+ | "--a2ui-color-backdrop"
32
+ | "--a2ui-color-overlay"
33
+ | "--a2ui-color-rating"
34
+ | "--a2ui-shadow-xs"
35
+ | "--a2ui-shadow-sm"
36
+ | "--a2ui-shadow-soft"
37
+ | "--a2ui-shadow-md"
38
+ | "--a2ui-font-family"
39
+ | "--a2ui-heading-margin"
40
+ | "--a2ui-block-margin";
41
+
42
+ /** Values for the theme variables. A name that is not one is a type error. */
43
+ export type GenerativeUIThemeVariables = {
44
+ [Variable in GenerativeUIThemeVariable]?: string;
45
+ };
46
+
47
+ /** The values of one color scheme, plus the map style of that scheme. */
48
+ export type GenerativeUIThemeValues = GenerativeUIThemeVariables & {
49
+ /** A MapLibre style URL for the Map component. */
50
+ mapStyle?: string;
51
+ };
52
+
53
+ /**
54
+ * The look of generated UI. Values at the top level apply in light and in
55
+ * dark mode. Values in `dark` replace them in dark mode. Dark mode is on
56
+ * when an ancestor of the surface has the `dark` class, like the CopilotKit
57
+ * chat.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const theme: GenerativeUITheme = {
62
+ * "--a2ui-color-accent": "#d97706",
63
+ * "--a2ui-font-family": "Inter, sans-serif",
64
+ * dark: { "--a2ui-color-panel": "#1f2937" },
65
+ * };
66
+ * ```
67
+ */
68
+ export type GenerativeUITheme = GenerativeUIThemeValues & {
69
+ dark?: GenerativeUIThemeValues;
70
+ };
71
+
72
+ /**
73
+ * The built-in dark values. The light values are the fallbacks in the
74
+ * components, so a surface is light when no variable is set.
75
+ */
76
+ export const DARK_THEME: GenerativeUIThemeVariables = {
77
+ "--a2ui-color-text-default": "#ececec",
78
+ "--a2ui-color-text-prose": "#ececec",
79
+ "--a2ui-color-text-primary": "#ececec",
80
+ "--a2ui-color-text-emphasis": "#fff",
81
+ "--a2ui-color-text-secondary": "#b4b4b4",
82
+ "--a2ui-color-text-tertiary": "#8e8e8e",
83
+ "--a2ui-color-text-success": "#4fd1a5",
84
+ "--a2ui-color-text-info": "#6cb6ff",
85
+ "--a2ui-color-text-warning": "#e3b341",
86
+ "--a2ui-color-text-caution": "#e3b341",
87
+ "--a2ui-color-text-danger": "#ff7b72",
88
+ "--a2ui-color-text-discovery": "#bc9cff",
89
+ "--a2ui-color-surface": "#1f1f1f",
90
+ "--a2ui-color-surface-secondary": "#2a2a2a",
91
+ "--a2ui-color-surface-tertiary": "#3a3a3a",
92
+ "--a2ui-color-surface-elevated": "#262626",
93
+ "--a2ui-color-surface-elevated-secondary": "#2f2f2f",
94
+ "--a2ui-color-surface-inverted": "#f2f2f2",
95
+ "--a2ui-color-surface-soft": "#232323",
96
+ "--a2ui-color-info": "rgba(80, 160, 255, 0.14)",
97
+ "--a2ui-color-warning": "rgba(240, 180, 60, 0.14)",
98
+ "--a2ui-color-caution": "rgba(240, 180, 60, 0.14)",
99
+ "--a2ui-color-success": "rgba(60, 200, 150, 0.14)",
100
+ "--a2ui-color-danger": "rgba(255, 110, 100, 0.14)",
101
+ "--a2ui-color-discovery": "rgba(170, 130, 255, 0.14)",
102
+ "--a2ui-color-info-soft": "rgba(80, 160, 255, 0.08)",
103
+ "--a2ui-color-warning-soft": "rgba(240, 180, 60, 0.08)",
104
+ "--a2ui-color-caution-soft": "rgba(240, 180, 60, 0.08)",
105
+ "--a2ui-color-success-soft": "rgba(60, 200, 150, 0.08)",
106
+ "--a2ui-color-danger-soft": "rgba(255, 110, 100, 0.08)",
107
+ "--a2ui-color-discovery-soft": "rgba(170, 130, 255, 0.08)",
108
+ "--a2ui-color-info-solid": "#3b8eea",
109
+ "--a2ui-color-warning-solid": "#c99a2e",
110
+ "--a2ui-color-caution-solid": "#c99a2e",
111
+ "--a2ui-color-success-solid": "#2fae86",
112
+ "--a2ui-color-danger-solid": "#e5534b",
113
+ "--a2ui-color-discovery-solid": "#9a6ff0",
114
+ "--a2ui-color-border-default": "rgba(255, 255, 255, 0.12)",
115
+ "--a2ui-color-border-subtle": "rgba(255, 255, 255, 0.06)",
116
+ "--a2ui-color-border-strong": "#4a4a4a",
117
+ "--a2ui-color-border-info": "#2f5f8f",
118
+ "--a2ui-color-border-warning": "#6b5420",
119
+ "--a2ui-color-border-caution": "#6b5420",
120
+ "--a2ui-color-border-success": "#1f5f4b",
121
+ "--a2ui-color-border-danger": "#7a2f2b",
122
+ "--a2ui-color-border-discovery": "#4f3a80",
123
+ "--a2ui-color-border-control": "rgba(255, 255, 255, 0.2)",
124
+ "--a2ui-color-accent": "#f2f2f2",
125
+ "--a2ui-color-on-accent": "#0d0d0d",
126
+ "--a2ui-color-panel": "#262626",
127
+ "--a2ui-color-control": "#2f2f2f",
128
+ "--a2ui-color-focus": "#3b9bff",
129
+ "--a2ui-color-shimmer": "rgba(255, 255, 255, 0.12)",
130
+ "--a2ui-color-chart-cursor": "rgba(255, 255, 255, 0.06)",
131
+ "--a2ui-color-slider-track": "rgba(255, 255, 255, 0.1)",
132
+ "--a2ui-color-badge-secondary": "rgba(255, 255, 255, 0.08)",
133
+ "--a2ui-color-highlight-green": "#1d4a37",
134
+ "--a2ui-color-highlight-orange": "#4d3418",
135
+ "--a2ui-color-highlight-pink": "#4d2338",
136
+ "--a2ui-shadow-xs": "0 1px 2px rgba(0, 0, 0, 0.5)",
137
+ "--a2ui-shadow-sm": "0 1px 3px rgba(0, 0, 0, 0.5)",
138
+ "--a2ui-shadow-soft": "0 1px 3px rgba(0, 0, 0, 0.3)",
139
+ "--a2ui-shadow-md": "0 4px 12px rgba(0, 0, 0, 0.4)",
140
+ };
141
+
142
+ const VARIABLE_NAME = /^--a2ui-[a-z0-9-]+$/;
143
+ // A value goes into a stylesheet, so it must not be able to end the rule or
144
+ // the element.
145
+ const UNSAFE_VALUE = /[{};<>]/;
146
+
147
+ /** Matches the `dark` class on an ancestor of a surface, or on the surface. */
148
+ const DARK = ":where(.dark)";
149
+
150
+ /** The default dark values, as a rule that any theme rule beats. */
151
+ export const DARK_THEME_CSS = `:where(.dark .a2ui-surface,.a2ui-surface.dark){${declarations(DARK_THEME)}}`;
152
+
153
+ function declarations(values: GenerativeUIThemeValues | undefined) {
154
+ return Object.entries(values ?? {})
155
+ .filter(
156
+ ([name, value]) =>
157
+ VARIABLE_NAME.test(name) &&
158
+ typeof value === "string" &&
159
+ !UNSAFE_VALUE.test(value),
160
+ )
161
+ .map(([name, value]) => `${name}:${value}`)
162
+ .join(";");
163
+ }
164
+
165
+ /** A short, stable id for a stylesheet. */
166
+ function hashOf(text: string) {
167
+ let hash = 5381;
168
+ for (let index = 0; index < text.length; index++) {
169
+ hash = (hash * 33) ^ text.charCodeAt(index);
170
+ }
171
+ return `t${(hash >>> 0).toString(36)}`;
172
+ }
173
+
174
+ /**
175
+ * The stylesheet of a theme: its values on the surfaces that carry its id,
176
+ * then its dark values in dark mode. `undefined` when the theme sets no
177
+ * variable. Values that could break out of the rule are left out.
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * themeStyleSheet({ "--a2ui-color-accent": "red" });
182
+ * // { id: "t…", css: '.a2ui-surface[data-a2ui-theme="t…"]{--a2ui-color-accent:red}' }
183
+ * ```
184
+ */
185
+ export function themeStyleSheet(theme: GenerativeUITheme | undefined) {
186
+ const { dark, ...base } = theme ?? {};
187
+ const both = declarations(base);
188
+ const darkOnly = declarations(dark);
189
+ if (!both && !darkOnly) return undefined;
190
+ const id = hashOf(`${both}|${darkOnly}`);
191
+ // The class makes these rules win over an app rule on `.a2ui-surface`.
192
+ const self = `.a2ui-surface[data-a2ui-theme="${id}"]`;
193
+ const rules = [
194
+ both && `${self}{${both}}`,
195
+ darkOnly && `${DARK} ${self},${self}${DARK}{${darkOnly}}`,
196
+ ].filter(Boolean);
197
+ return { id, css: rules.join("") };
198
+ }