@assistant-ui/react-generative-ui 0.0.24 → 0.1.0

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 (338) hide show
  1. package/README.md +2 -0
  2. package/dist/a2ui.d.ts +1 -4
  3. package/dist/a2ui.d.ts.map +1 -1
  4. package/dist/a2ui.js +1 -4
  5. package/dist/index.d.ts +1 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -10
  8. package/dist/index.server.d.ts +1 -2
  9. package/dist/index.server.d.ts.map +1 -1
  10. package/dist/index.server.js +1 -10
  11. package/dist/ir.d.ts +1 -111
  12. package/dist/ir.d.ts.map +1 -1
  13. package/dist/ir.js +1 -175
  14. package/dist/slack.d.ts +1 -4
  15. package/dist/slack.d.ts.map +1 -1
  16. package/dist/slack.js +1 -4
  17. package/dist/teams.d.ts +1 -4
  18. package/dist/teams.d.ts.map +1 -1
  19. package/dist/teams.js +1 -4
  20. package/package.json +4 -5
  21. package/src/a2ui.ts +1 -22
  22. package/src/index.server.ts +1 -2
  23. package/src/index.test.ts +75 -0
  24. package/src/index.ts +1 -2
  25. package/src/ir.ts +1 -279
  26. package/src/slack.ts +1 -39
  27. package/src/teams.ts +1 -35
  28. package/dist/JSONGenerativeUI.client.d.ts +0 -28
  29. package/dist/JSONGenerativeUI.client.d.ts.map +0 -1
  30. package/dist/JSONGenerativeUI.client.js +0 -101
  31. package/dist/JSONGenerativeUI.client.js.map +0 -1
  32. package/dist/JSONGenerativeUI.server.d.ts +0 -21
  33. package/dist/JSONGenerativeUI.server.d.ts.map +0 -1
  34. package/dist/JSONGenerativeUI.server.js +0 -32
  35. package/dist/JSONGenerativeUI.server.js.map +0 -1
  36. package/dist/JSONGenerativeUI.shared.d.ts +0 -63
  37. package/dist/JSONGenerativeUI.shared.d.ts.map +0 -1
  38. package/dist/JSONGenerativeUI.shared.js +0 -31
  39. package/dist/JSONGenerativeUI.shared.js.map +0 -1
  40. package/dist/RadioGroupScope.d.ts +0 -6
  41. package/dist/RadioGroupScope.d.ts.map +0 -1
  42. package/dist/RadioGroupScope.js +0 -25
  43. package/dist/RadioGroupScope.js.map +0 -1
  44. package/dist/a2ui/BindingContext.d.ts +0 -11
  45. package/dist/a2ui/BindingContext.d.ts.map +0 -1
  46. package/dist/a2ui/BindingContext.js +0 -12
  47. package/dist/a2ui/BindingContext.js.map +0 -1
  48. package/dist/a2ui/PresentRenderer.d.ts +0 -11
  49. package/dist/a2ui/PresentRenderer.d.ts.map +0 -1
  50. package/dist/a2ui/PresentRenderer.js +0 -39
  51. package/dist/a2ui/PresentRenderer.js.map +0 -1
  52. package/dist/a2ui/convert.d.ts +0 -15
  53. package/dist/a2ui/convert.d.ts.map +0 -1
  54. package/dist/a2ui/convert.js +0 -728
  55. package/dist/a2ui/convert.js.map +0 -1
  56. package/dist/a2ui/dataModel.d.ts +0 -9
  57. package/dist/a2ui/dataModel.d.ts.map +0 -1
  58. package/dist/a2ui/dataModel.js +0 -44
  59. package/dist/a2ui/dataModel.js.map +0 -1
  60. package/dist/a2ui/reducer.d.ts +0 -8
  61. package/dist/a2ui/reducer.d.ts.map +0 -1
  62. package/dist/a2ui/reducer.js +0 -242
  63. package/dist/a2ui/reducer.js.map +0 -1
  64. package/dist/a2ui/snapshot.d.ts +0 -3
  65. package/dist/a2ui/snapshot.d.ts.map +0 -1
  66. package/dist/a2ui/snapshot.js +0 -33
  67. package/dist/a2ui/snapshot.js.map +0 -1
  68. package/dist/a2ui/types.d.ts +0 -88
  69. package/dist/a2ui/types.d.ts.map +0 -1
  70. package/dist/a2ui/types.js +0 -6
  71. package/dist/a2ui/types.js.map +0 -1
  72. package/dist/a2ui/valueFunctions.d.ts +0 -16
  73. package/dist/a2ui/valueFunctions.d.ts.map +0 -1
  74. package/dist/a2ui/valueFunctions.js +0 -332
  75. package/dist/a2ui/valueFunctions.js.map +0 -1
  76. package/dist/actionRegistry.d.ts +0 -29
  77. package/dist/actionRegistry.d.ts.map +0 -1
  78. package/dist/actionRegistry.js +0 -41
  79. package/dist/actionRegistry.js.map +0 -1
  80. package/dist/answeredValues.d.ts +0 -16
  81. package/dist/answeredValues.d.ts.map +0 -1
  82. package/dist/answeredValues.js +0 -37
  83. package/dist/answeredValues.js.map +0 -1
  84. package/dist/buildPresentParameters.d.ts +0 -21
  85. package/dist/buildPresentParameters.d.ts.map +0 -1
  86. package/dist/buildPresentParameters.js +0 -95
  87. package/dist/buildPresentParameters.js.map +0 -1
  88. package/dist/constants.d.ts +0 -15
  89. package/dist/constants.d.ts.map +0 -1
  90. package/dist/constants.js +0 -19
  91. package/dist/constants.js.map +0 -1
  92. package/dist/convert/boundSpec.d.ts +0 -48
  93. package/dist/convert/boundSpec.d.ts.map +0 -1
  94. package/dist/convert/boundSpec.js +0 -108
  95. package/dist/convert/boundSpec.js.map +0 -1
  96. package/dist/convert/copyBounded.d.ts +0 -25
  97. package/dist/convert/copyBounded.d.ts.map +0 -1
  98. package/dist/convert/copyBounded.js +0 -38
  99. package/dist/convert/copyBounded.js.map +0 -1
  100. package/dist/convert/isElement.d.ts +0 -3
  101. package/dist/convert/isElement.d.ts.map +0 -1
  102. package/dist/convert/isElement.js +0 -6
  103. package/dist/convert/isElement.js.map +0 -1
  104. package/dist/convert/takeRun.d.ts +0 -6
  105. package/dist/convert/takeRun.d.ts.map +0 -1
  106. package/dist/convert/takeRun.js +0 -20
  107. package/dist/convert/takeRun.js.map +0 -1
  108. package/dist/defineGenerativeComponents.d.ts +0 -29
  109. package/dist/defineGenerativeComponents.d.ts.map +0 -1
  110. package/dist/defineGenerativeComponents.js +0 -34
  111. package/dist/defineGenerativeComponents.js.map +0 -1
  112. package/dist/fieldReferences.d.ts +0 -9
  113. package/dist/fieldReferences.d.ts.map +0 -1
  114. package/dist/fieldReferences.js +0 -53
  115. package/dist/fieldReferences.js.map +0 -1
  116. package/dist/generativeUIToJSX.d.ts +0 -18
  117. package/dist/generativeUIToJSX.d.ts.map +0 -1
  118. package/dist/generativeUIToJSX.js +0 -76
  119. package/dist/generativeUIToJSX.js.map +0 -1
  120. package/dist/index.shared.d.ts +0 -15
  121. package/dist/index.shared.d.ts.map +0 -1
  122. package/dist/index.shared.js +0 -9
  123. package/dist/ir.js.map +0 -1
  124. package/dist/renderGenerativeUI.d.ts +0 -14
  125. package/dist/renderGenerativeUI.d.ts.map +0 -1
  126. package/dist/renderGenerativeUI.js +0 -83
  127. package/dist/renderGenerativeUI.js.map +0 -1
  128. package/dist/scopeSchema.d.ts +0 -6
  129. package/dist/scopeSchema.d.ts.map +0 -1
  130. package/dist/scopeSchema.js +0 -56
  131. package/dist/scopeSchema.js.map +0 -1
  132. package/dist/slack/constants.d.ts +0 -93
  133. package/dist/slack/constants.d.ts.map +0 -1
  134. package/dist/slack/constants.js +0 -146
  135. package/dist/slack/constants.js.map +0 -1
  136. package/dist/slack/decodeBlockAction.d.ts +0 -10
  137. package/dist/slack/decodeBlockAction.d.ts.map +0 -1
  138. package/dist/slack/decodeBlockAction.js +0 -129
  139. package/dist/slack/decodeBlockAction.js.map +0 -1
  140. package/dist/slack/fromSlackBlocks.d.ts +0 -27
  141. package/dist/slack/fromSlackBlocks.d.ts.map +0 -1
  142. package/dist/slack/fromSlackBlocks.js +0 -483
  143. package/dist/slack/fromSlackBlocks.js.map +0 -1
  144. package/dist/slack/toSlackBlocks.d.ts +0 -4
  145. package/dist/slack/toSlackBlocks.d.ts.map +0 -1
  146. package/dist/slack/toSlackBlocks.js +0 -901
  147. package/dist/slack/toSlackBlocks.js.map +0 -1
  148. package/dist/slack/types.d.ts +0 -204
  149. package/dist/slack/types.d.ts.map +0 -1
  150. package/dist/slack/types.js +0 -0
  151. package/dist/teams/constants.d.ts +0 -34
  152. package/dist/teams/constants.d.ts.map +0 -1
  153. package/dist/teams/constants.js +0 -68
  154. package/dist/teams/constants.js.map +0 -1
  155. package/dist/teams/decodeSubmitData.d.ts +0 -26
  156. package/dist/teams/decodeSubmitData.d.ts.map +0 -1
  157. package/dist/teams/decodeSubmitData.js +0 -70
  158. package/dist/teams/decodeSubmitData.js.map +0 -1
  159. package/dist/teams/temporalId.d.ts +0 -5
  160. package/dist/teams/temporalId.d.ts.map +0 -1
  161. package/dist/teams/temporalId.js +0 -28
  162. package/dist/teams/temporalId.js.map +0 -1
  163. package/dist/teams/toAdaptiveCard.d.ts +0 -45
  164. package/dist/teams/toAdaptiveCard.d.ts.map +0 -1
  165. package/dist/teams/toAdaptiveCard.js +0 -642
  166. package/dist/teams/toAdaptiveCard.js.map +0 -1
  167. package/dist/teams/toTeamsAttachments.d.ts +0 -10
  168. package/dist/teams/toTeamsAttachments.d.ts.map +0 -1
  169. package/dist/teams/toTeamsAttachments.js +0 -99
  170. package/dist/teams/toTeamsAttachments.js.map +0 -1
  171. package/dist/teams/types.d.ts +0 -224
  172. package/dist/teams/types.d.ts.map +0 -1
  173. package/dist/teams/types.js +0 -0
  174. package/dist/temporal.d.ts +0 -33
  175. package/dist/temporal.d.ts.map +0 -1
  176. package/dist/temporal.js +0 -153
  177. package/dist/temporal.js.map +0 -1
  178. package/dist/types.d.ts +0 -99
  179. package/dist/types.d.ts.map +0 -1
  180. package/dist/types.js +0 -0
  181. package/dist/vocabulary/alert.d.ts +0 -32
  182. package/dist/vocabulary/alert.d.ts.map +0 -1
  183. package/dist/vocabulary/alert.js +0 -160
  184. package/dist/vocabulary/alert.js.map +0 -1
  185. package/dist/vocabulary/collectFormValues.d.ts +0 -23
  186. package/dist/vocabulary/collectFormValues.d.ts.map +0 -1
  187. package/dist/vocabulary/collectFormValues.js +0 -44
  188. package/dist/vocabulary/collectFormValues.js.map +0 -1
  189. package/dist/vocabulary/data.d.ts +0 -66
  190. package/dist/vocabulary/data.d.ts.map +0 -1
  191. package/dist/vocabulary/data.js +0 -503
  192. package/dist/vocabulary/data.js.map +0 -1
  193. package/dist/vocabulary/dispatch.d.ts +0 -9
  194. package/dist/vocabulary/dispatch.d.ts.map +0 -1
  195. package/dist/vocabulary/dispatch.js +0 -50
  196. package/dist/vocabulary/dispatch.js.map +0 -1
  197. package/dist/vocabulary/fact.d.ts +0 -19
  198. package/dist/vocabulary/fact.d.ts.map +0 -1
  199. package/dist/vocabulary/fact.js +0 -51
  200. package/dist/vocabulary/fact.js.map +0 -1
  201. package/dist/vocabulary/form.d.ts +0 -11
  202. package/dist/vocabulary/form.d.ts.map +0 -1
  203. package/dist/vocabulary/form.js +0 -23
  204. package/dist/vocabulary/form.js.map +0 -1
  205. package/dist/vocabulary/formatValue.d.ts +0 -7
  206. package/dist/vocabulary/formatValue.d.ts.map +0 -1
  207. package/dist/vocabulary/formatValue.js +0 -76
  208. package/dist/vocabulary/formatValue.js.map +0 -1
  209. package/dist/vocabulary/icon.d.ts +0 -41
  210. package/dist/vocabulary/icon.d.ts.map +0 -1
  211. package/dist/vocabulary/icon.js +0 -272
  212. package/dist/vocabulary/icon.js.map +0 -1
  213. package/dist/vocabulary/index.d.ts +0 -12
  214. package/dist/vocabulary/index.d.ts.map +0 -1
  215. package/dist/vocabulary/index.js +0 -36
  216. package/dist/vocabulary/index.js.map +0 -1
  217. package/dist/vocabulary/interactive.d.ts +0 -230
  218. package/dist/vocabulary/interactive.d.ts.map +0 -1
  219. package/dist/vocabulary/interactive.js +0 -584
  220. package/dist/vocabulary/interactive.js.map +0 -1
  221. package/dist/vocabulary/layout.d.ts +0 -79
  222. package/dist/vocabulary/layout.d.ts.map +0 -1
  223. package/dist/vocabulary/layout.js +0 -139
  224. package/dist/vocabulary/layout.js.map +0 -1
  225. package/dist/vocabulary/list.d.ts +0 -14
  226. package/dist/vocabulary/list.d.ts.map +0 -1
  227. package/dist/vocabulary/list.js +0 -50
  228. package/dist/vocabulary/list.js.map +0 -1
  229. package/dist/vocabulary/media.d.ts +0 -25
  230. package/dist/vocabulary/media.d.ts.map +0 -1
  231. package/dist/vocabulary/media.js +0 -42
  232. package/dist/vocabulary/media.js.map +0 -1
  233. package/dist/vocabulary/text.d.ts +0 -57
  234. package/dist/vocabulary/text.d.ts.map +0 -1
  235. package/dist/vocabulary/text.js +0 -49
  236. package/dist/vocabulary/text.js.map +0 -1
  237. package/dist/vocabulary/toTextContent.d.ts +0 -2
  238. package/dist/vocabulary/toTextContent.d.ts.map +0 -1
  239. package/dist/vocabulary/toTextContent.js +0 -6
  240. package/dist/vocabulary/toTextContent.js.map +0 -1
  241. package/src/JSONGenerativeUI.client.tsx +0 -168
  242. package/src/JSONGenerativeUI.server.tsx +0 -39
  243. package/src/JSONGenerativeUI.shared.ts +0 -90
  244. package/src/JSONGenerativeUI.test.tsx +0 -1298
  245. package/src/RadioGroupScope.server.test.tsx +0 -24
  246. package/src/RadioGroupScope.tsx +0 -34
  247. package/src/a2ui/BindingContext.ts +0 -26
  248. package/src/a2ui/PresentRenderer.tsx +0 -83
  249. package/src/a2ui/convert.test.ts +0 -2923
  250. package/src/a2ui/convert.ts +0 -1207
  251. package/src/a2ui/dataModel.ts +0 -69
  252. package/src/a2ui/reducer.test.ts +0 -422
  253. package/src/a2ui/reducer.ts +0 -363
  254. package/src/a2ui/snapshot.test.ts +0 -52
  255. package/src/a2ui/snapshot.ts +0 -52
  256. package/src/a2ui/types.ts +0 -114
  257. package/src/a2ui/valueFunctions.test.ts +0 -271
  258. package/src/a2ui/valueFunctions.ts +0 -523
  259. package/src/actionRegistry.test.ts +0 -90
  260. package/src/actionRegistry.ts +0 -88
  261. package/src/answeredValues.test.ts +0 -33
  262. package/src/answeredValues.tsx +0 -70
  263. package/src/buildPresentParameters.test.ts +0 -246
  264. package/src/buildPresentParameters.ts +0 -130
  265. package/src/constants.ts +0 -17
  266. package/src/convert/boundSpec.test.ts +0 -192
  267. package/src/convert/boundSpec.ts +0 -134
  268. package/src/convert/copyBounded.test.ts +0 -131
  269. package/src/convert/copyBounded.ts +0 -38
  270. package/src/convert/isElement.ts +0 -6
  271. package/src/convert/takeRun.test.ts +0 -64
  272. package/src/convert/takeRun.ts +0 -19
  273. package/src/defineGenerativeComponents.ts +0 -38
  274. package/src/fieldReferences.test.ts +0 -66
  275. package/src/fieldReferences.ts +0 -83
  276. package/src/generativeUIToJSX.test.ts +0 -268
  277. package/src/generativeUIToJSX.ts +0 -152
  278. package/src/index.server.test.ts +0 -106
  279. package/src/index.shared.ts +0 -61
  280. package/src/ir.test.ts +0 -203
  281. package/src/renderGenerativeUI.test.tsx +0 -373
  282. package/src/renderGenerativeUI.tsx +0 -165
  283. package/src/scopeSchema.test.ts +0 -72
  284. package/src/scopeSchema.ts +0 -71
  285. package/src/slack/a2uiSlack.test.ts +0 -198
  286. package/src/slack/constants.ts +0 -194
  287. package/src/slack/datePicker.test.ts +0 -612
  288. package/src/slack/decodeBlockAction.test.ts +0 -233
  289. package/src/slack/decodeBlockAction.ts +0 -232
  290. package/src/slack/fromSlackBlocks.test.ts +0 -1465
  291. package/src/slack/fromSlackBlocks.ts +0 -789
  292. package/src/slack/toSlackBlocks.test.ts +0 -3065
  293. package/src/slack/toSlackBlocks.ts +0 -1818
  294. package/src/slack/types.ts +0 -264
  295. package/src/teams/constants.ts +0 -93
  296. package/src/teams/decodeSubmitData.test.ts +0 -397
  297. package/src/teams/decodeSubmitData.ts +0 -110
  298. package/src/teams/temporal.test.ts +0 -766
  299. package/src/teams/temporalId.ts +0 -56
  300. package/src/teams/toAdaptiveCard.test.ts +0 -1897
  301. package/src/teams/toAdaptiveCard.ts +0 -1057
  302. package/src/teams/toTeamsAttachments.test.ts +0 -74
  303. package/src/teams/toTeamsAttachments.ts +0 -133
  304. package/src/teams/types.ts +0 -266
  305. package/src/temporal.test.ts +0 -337
  306. package/src/temporal.ts +0 -263
  307. package/src/types.ts +0 -119
  308. package/src/vocabulary/alert.test.tsx +0 -427
  309. package/src/vocabulary/alert.tsx +0 -217
  310. package/src/vocabulary/collectFormValues.test.ts +0 -389
  311. package/src/vocabulary/collectFormValues.ts +0 -116
  312. package/src/vocabulary/data.test.tsx +0 -858
  313. package/src/vocabulary/data.tsx +0 -819
  314. package/src/vocabulary/datepicker.dom.test.tsx +0 -1457
  315. package/src/vocabulary/dispatch.ts +0 -83
  316. package/src/vocabulary/fact.test.tsx +0 -94
  317. package/src/vocabulary/fact.tsx +0 -54
  318. package/src/vocabulary/form.test.tsx +0 -113
  319. package/src/vocabulary/form.tsx +0 -39
  320. package/src/vocabulary/formatValue.test.ts +0 -72
  321. package/src/vocabulary/formatValue.ts +0 -127
  322. package/src/vocabulary/icon.test.tsx +0 -64
  323. package/src/vocabulary/icon.tsx +0 -174
  324. package/src/vocabulary/index.test.ts +0 -65
  325. package/src/vocabulary/index.ts +0 -33
  326. package/src/vocabulary/interactive.dispatch.test.tsx +0 -454
  327. package/src/vocabulary/interactive.dom.test.tsx +0 -1747
  328. package/src/vocabulary/interactive.test.tsx +0 -377
  329. package/src/vocabulary/interactive.tsx +0 -1109
  330. package/src/vocabulary/layout.test.tsx +0 -323
  331. package/src/vocabulary/layout.tsx +0 -247
  332. package/src/vocabulary/list.test.tsx +0 -222
  333. package/src/vocabulary/list.tsx +0 -50
  334. package/src/vocabulary/media.test.tsx +0 -85
  335. package/src/vocabulary/media.tsx +0 -56
  336. package/src/vocabulary/text.test.tsx +0 -102
  337. package/src/vocabulary/text.tsx +0 -60
  338. package/src/vocabulary/toTextContent.ts +0 -2
@@ -1,1057 +0,0 @@
1
- import {
2
- MAX_TRAVERSAL_DEPTH,
3
- boundSpec,
4
- clampReasonDetail,
5
- } from "../convert/boundSpec";
6
- import { copyBounded } from "../convert/copyBounded";
7
- import { isElement } from "../convert/isElement";
8
- import { takeRun } from "../convert/takeRun";
9
- import {
10
- normalizeSpec,
11
- type NormalizedUIElement,
12
- type NormalizedUINode,
13
- } from "../ir";
14
- import {
15
- classifyTemporal,
16
- splitTemporalMinutes,
17
- temporalOffsetLabel,
18
- } from "../temporal";
19
- import {
20
- factTrend,
21
- formatFactDelta,
22
- formatValue,
23
- } from "../vocabulary/formatValue";
24
- import {
25
- CHOICE_OPTION_CAP,
26
- PAYLOAD_SOFT_CAP,
27
- PRIMARY_ACTION_CAP,
28
- TABLE_COLUMN_CAP,
29
- TABLE_ROW_CAP,
30
- buildCard,
31
- buildSubmitAction,
32
- utf8ByteLength,
33
- } from "./constants";
34
- import { encodeTemporalInputId } from "./temporalId";
35
- import type {
36
- AdaptiveCardResult,
37
- TeamsActionSet,
38
- TeamsAdaptiveCard,
39
- TeamsCardElement,
40
- TeamsColumn,
41
- TeamsContainer,
42
- TeamsContainerStyle,
43
- TeamsConversionWarning,
44
- TeamsFact,
45
- TeamsInputChoice,
46
- TeamsTable,
47
- TeamsTableColumnDefinition,
48
- TeamsTableRow,
49
- TeamsTextBlock,
50
- TeamsTextSize,
51
- ToAdaptiveCardOptions,
52
- } from "./types";
53
-
54
- /** Shared mutable conversion state threaded through one top-level conversion. */
55
- export interface ConversionContext {
56
- readonly warnings: TeamsConversionWarning[];
57
- usedInputIds: Set<string>;
58
- }
59
-
60
- const isRecord = (value: unknown): value is Record<string, unknown> =>
61
- typeof value === "object" && value !== null && !Array.isArray(value);
62
-
63
- const asString = (value: unknown): string =>
64
- typeof value === "string" ? value : "";
65
-
66
- const asFiniteNumber = (value: unknown): number | undefined =>
67
- typeof value === "number" && Number.isFinite(value) ? value : undefined;
68
-
69
- const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
70
-
71
- const SUBTLE_COLORS = new Set([
72
- "secondary",
73
- "alpha-70",
74
- "white",
75
- "white-70",
76
- "white-50",
77
- ]);
78
- const isSubtleColor = (color: unknown): boolean =>
79
- typeof color === "string" && SUBTLE_COLORS.has(color);
80
-
81
- const BOLDER_WEIGHTS = new Set(["medium", "semibold", "bold"]);
82
- const isBolderWeight = (weight: unknown): boolean =>
83
- typeof weight === "string" && BOLDER_WEIGHTS.has(weight);
84
-
85
- const TEXT_SIZE_MAP: Record<string, TeamsTextSize> = {
86
- sm: "small",
87
- lg: "medium",
88
- xl: "large",
89
- "2xl": "extraLarge",
90
- "3xl": "extraLarge",
91
- };
92
-
93
- const warn = (
94
- context: ConversionContext,
95
- code: TeamsConversionWarning["code"],
96
- component: string,
97
- detail: string,
98
- ) => {
99
- context.warnings.push({ code, component, detail });
100
- };
101
-
102
- /**
103
- * The `aui` key is reserved for the submit envelope (see
104
- * {@link buildSubmitData} and `decodeSubmitData`): Adaptive Cards merge
105
- * every input's current value into the same submit object keyed by its
106
- * `id`, so an input id of `aui` would clobber the envelope. Renamed to an
107
- * unused id derived from `aui_` instead of being emitted as-is.
108
- */
109
- const RESERVED_INPUT_ID = "aui";
110
-
111
- function reservedSafeId(
112
- id: string,
113
- component: string,
114
- context: ConversionContext,
115
- ): string {
116
- const reserved = id === RESERVED_INPUT_ID;
117
- const temporalReserved = /^_*aui:datetime:/.test(id);
118
- const base = reserved
119
- ? `${RESERVED_INPUT_ID}_`
120
- : temporalReserved
121
- ? `_${id}`
122
- : id;
123
- let candidate = base;
124
- let n = 2;
125
- while (context.usedInputIds.has(candidate)) {
126
- candidate = `${base}_${n}`;
127
- n += 1;
128
- }
129
- if (reserved) {
130
- warn(
131
- context,
132
- "fallback",
133
- component,
134
- `the input id "${RESERVED_INPUT_ID}" collides with the submit envelope's reserved key and was renamed to "${candidate}".`,
135
- );
136
- } else if (temporalReserved) {
137
- warn(
138
- context,
139
- "fallback",
140
- component,
141
- `the input id "${id}" collides with the reserved datetime namespace and was renamed to "${candidate}".`,
142
- );
143
- } else if (candidate !== base) {
144
- warn(
145
- context,
146
- "fallback",
147
- component,
148
- `the input id "${base}" was already used on this card and was renamed to "${candidate}".`,
149
- );
150
- }
151
- context.usedInputIds.add(candidate);
152
- return candidate;
153
- }
154
-
155
- /**
156
- * The shared bounded-iteration primitive: copies `value` to `cap` entries
157
- * without ever reading past that many indices, so a hostile array (sparse or
158
- * proxied with a fabricated `length`) cannot stall the event loop.
159
- */
160
- const clampArray = (
161
- value: unknown,
162
- cap: number,
163
- ): { readonly items: unknown[]; readonly truncated: boolean } => {
164
- if (!Array.isArray(value)) return { items: [], truncated: false };
165
- return copyBounded(value, cap);
166
- };
167
-
168
- const normalizedList = (
169
- node: NormalizedUINode | undefined,
170
- ): NormalizedUINode[] => {
171
- if (node === undefined || node === null) return [];
172
- if (!Array.isArray(node)) return [node];
173
- return node.flatMap((child) => normalizedList(child));
174
- };
175
-
176
- function textBlock(
177
- text: string,
178
- extra?: Partial<Omit<TeamsTextBlock, "type" | "text" | "wrap">>,
179
- ): TeamsTextBlock {
180
- return { type: "TextBlock", text, wrap: true, ...extra };
181
- }
182
-
183
- function applyPending(
184
- element: TeamsCardElement,
185
- separator: boolean,
186
- spacing: boolean,
187
- ): TeamsCardElement {
188
- if (!separator && !spacing) return element;
189
- return {
190
- ...element,
191
- ...(separator ? { separator: true as const } : {}),
192
- ...(spacing ? { spacing: "large" as const } : {}),
193
- };
194
- }
195
-
196
- const toChoice = (option: unknown): TeamsInputChoice | undefined => {
197
- if (!isRecord(option) || typeof option["value"] !== "string") {
198
- return undefined;
199
- }
200
- const label = asString(option["label"]);
201
- const description = asString(option["description"]);
202
- return {
203
- title: description
204
- ? [label, description].filter(Boolean).join(": ")
205
- : label,
206
- value: option["value"],
207
- };
208
- };
209
-
210
- const choicesFrom = (
211
- optionsProp: unknown,
212
- component: string,
213
- context: ConversionContext,
214
- ): TeamsInputChoice[] => {
215
- const { items, truncated } = clampArray(optionsProp, CHOICE_OPTION_CAP);
216
- if (truncated) {
217
- warn(
218
- context,
219
- "clamped",
220
- component,
221
- `options were clamped to ${CHOICE_OPTION_CAP} entries.`,
222
- );
223
- }
224
- const choices: TeamsInputChoice[] = [];
225
- for (const item of items) {
226
- const choice = toChoice(item);
227
- if (choice !== undefined) choices.push(choice);
228
- }
229
- const dropped = items.length - choices.length;
230
- if (dropped > 0) {
231
- warn(
232
- context,
233
- "dropped",
234
- component,
235
- `${dropped} ${dropped === 1 ? "option was" : "options were"} dropped for want of a string value.`,
236
- );
237
- }
238
- return choices;
239
- };
240
-
241
- function convertFacts(facts: readonly NormalizedUIElement[]): TeamsCardElement {
242
- const set: TeamsFact[] = facts.map((fact) => {
243
- const value = asString(fact.props["value"]);
244
- const delta = fact.props["delta"];
245
- const deltaText =
246
- typeof delta === "string"
247
- ? formatFactDelta(delta, factTrend(delta, fact.props["trend"]))
248
- : undefined;
249
- return {
250
- title: asString(fact.props["label"]),
251
- value: deltaText === undefined ? value : `${value} (${deltaText})`,
252
- };
253
- });
254
- return { type: "FactSet", facts: set };
255
- }
256
-
257
- // Action.Submit has no visual style concept, so a Button's `buttonStyle`
258
- // (positive/destructive or otherwise) has nothing to map onto and is dropped.
259
- function convertButtons(
260
- buttons: readonly NormalizedUIElement[],
261
- context: ConversionContext,
262
- ): TeamsActionSet {
263
- if (buttons.length > PRIMARY_ACTION_CAP) {
264
- warn(
265
- context,
266
- "fallback",
267
- "Button",
268
- `actions beyond ${PRIMARY_ACTION_CAP} were set to secondary mode.`,
269
- );
270
- }
271
- const actions = buttons.map((button, index) =>
272
- buildSubmitAction(
273
- asString(button.props["label"]),
274
- button.action,
275
- index >= PRIMARY_ACTION_CAP ? "secondary" : undefined,
276
- ),
277
- );
278
- return { type: "ActionSet", actions };
279
- }
280
-
281
- function withCompanionSubmit(
282
- element: NormalizedUIElement,
283
- input: TeamsCardElement,
284
- context: ConversionContext,
285
- ): TeamsCardElement[] {
286
- if (element.action === undefined) return [input];
287
- warn(
288
- context,
289
- "fallback",
290
- element.type,
291
- "Teams inputs cannot dispatch on change; a companion submit action was appended.",
292
- );
293
- return [
294
- input,
295
- {
296
- type: "ActionSet",
297
- actions: [buildSubmitAction("Submit", element.action)],
298
- },
299
- ];
300
- }
301
-
302
- function cardFooterActionSet(
303
- props: Readonly<Record<string, unknown>>,
304
- ): TeamsActionSet | undefined {
305
- const actions = (["confirm", "cancel"] as const)
306
- .map((key) => props[key])
307
- .filter(isRecord)
308
- .map((footer) =>
309
- buildSubmitAction(asString(footer["label"]), footer["$action"]),
310
- );
311
- return actions.length > 0 ? { type: "ActionSet", actions } : undefined;
312
- }
313
-
314
- const ALERT_STYLE_MAP: Record<string, TeamsContainerStyle> = {
315
- info: "accent",
316
- success: "good",
317
- warning: "warning",
318
- danger: "attention",
319
- };
320
-
321
- function convertTable(
322
- props: Readonly<Record<string, unknown>>,
323
- context: ConversionContext,
324
- ): TeamsTable {
325
- const { items: rawColumns, truncated: columnsTruncated } = clampArray(
326
- props["columns"],
327
- TABLE_COLUMN_CAP,
328
- );
329
- const { items: rawRows, truncated: rowsTruncated } = clampArray(
330
- props["rows"],
331
- TABLE_ROW_CAP,
332
- );
333
- if (columnsTruncated) {
334
- warn(
335
- context,
336
- "clamped",
337
- "Table",
338
- `columns were clamped to ${TABLE_COLUMN_CAP} entries.`,
339
- );
340
- }
341
- if (rowsTruncated) {
342
- warn(
343
- context,
344
- "clamped",
345
- "Table",
346
- `rows were clamped to ${TABLE_ROW_CAP} entries.`,
347
- );
348
- }
349
-
350
- const unlabeled = rawColumns.filter(
351
- (column) => !isRecord(column) || typeof column["label"] !== "string",
352
- ).length;
353
- if (unlabeled > 0) {
354
- warn(
355
- context,
356
- "dropped",
357
- "Table",
358
- `${unlabeled} column ${unlabeled === 1 ? "header was" : "headers were"} left blank for want of a string label.`,
359
- );
360
- }
361
- const hasColumns = rawColumns.length > 0;
362
- const columns: TeamsTableColumnDefinition[] = rawColumns.map(() => ({
363
- width: 1 as const,
364
- }));
365
- const headerRow: TeamsTableRow | undefined = hasColumns
366
- ? {
367
- type: "TableRow",
368
- cells: rawColumns.map((column) => ({
369
- type: "TableCell" as const,
370
- items: [textBlock(isRecord(column) ? asString(column["label"]) : "")],
371
- })),
372
- }
373
- : undefined;
374
- const dataRows: TeamsTableRow[] = rawRows.map((row) => ({
375
- type: "TableRow",
376
- cells: (Array.isArray(row)
377
- ? copyBounded(row, TABLE_COLUMN_CAP).items
378
- : []
379
- ).map((cell, index) => ({
380
- type: "TableCell" as const,
381
- items: [
382
- textBlock(
383
- formatValue(
384
- cell,
385
- isRecord(rawColumns[index])
386
- ? rawColumns[index]["format"]
387
- : undefined,
388
- ),
389
- ),
390
- ],
391
- })),
392
- }));
393
-
394
- return {
395
- type: "Table",
396
- firstRowAsHeaders: hasColumns,
397
- columns,
398
- rows: headerRow !== undefined ? [headerRow, ...dataRows] : dataRows,
399
- };
400
- }
401
-
402
- function convertListViewItem(
403
- element: NormalizedUIElement,
404
- context: ConversionContext,
405
- depth: number,
406
- ): TeamsContainer {
407
- const items = convertSequence(element.children, context, depth + 1);
408
- const selectAction = isRecord(element.action)
409
- ? buildSubmitAction("Open", element.action)
410
- : undefined;
411
- return {
412
- type: "Container",
413
- items,
414
- ...(selectAction !== undefined ? { selectAction } : {}),
415
- };
416
- }
417
-
418
- /**
419
- * Converts a child whose output is thrown away, and reports whether anything
420
- * was lost with it. A scratch context keeps the ids it claims out of the card,
421
- * where they would rename a control that survives; only its `dropped` warnings
422
- * are forwarded, because those describe the tree the caller wrote, while a
423
- * clamp or a rename would describe content never delivered.
424
- */
425
- export const discardedChild = (
426
- child: NormalizedUINode,
427
- context: ConversionContext,
428
- depth: number,
429
- ): boolean => {
430
- const scratch: ConversionContext = {
431
- ...context,
432
- warnings: [],
433
- usedInputIds: new Set(context.usedInputIds),
434
- };
435
- const produced = convertSequence(child, scratch, depth).length > 0;
436
- const lost = scratch.warnings.filter((warning) => warning.code === "dropped");
437
- context.warnings.push(...lost);
438
- return produced || lost.length > 0;
439
- };
440
-
441
- export function convertElement(
442
- element: NormalizedUIElement,
443
- context: ConversionContext,
444
- depth: number,
445
- ): TeamsCardElement[] {
446
- const { props } = element;
447
- switch (element.type) {
448
- case "Header":
449
- return [
450
- textBlock(asString(props["text"]), {
451
- size: "large",
452
- weight: "bolder",
453
- style: "heading",
454
- }),
455
- ];
456
- case "Text": {
457
- const mappedSize =
458
- typeof props["size"] === "string"
459
- ? TEXT_SIZE_MAP[props["size"]]
460
- : undefined;
461
- return [
462
- textBlock(asString(props["value"]), {
463
- ...(mappedSize !== undefined ? { size: mappedSize } : {}),
464
- ...(isBolderWeight(props["weight"]) ? { weight: "bolder" } : {}),
465
- ...(isSubtleColor(props["color"]) ? { isSubtle: true } : {}),
466
- }),
467
- ];
468
- }
469
- case "Caption":
470
- return [
471
- textBlock(asString(props["value"]), { size: "small", isSubtle: true }),
472
- ];
473
- case "Badge":
474
- return [
475
- textBlock(asString(props["value"]), { size: "small", isSubtle: true }),
476
- ];
477
- case "Markdown":
478
- // Teams renders a strict subset of markdown (no headings, tables, images,
479
- // preformatted blocks, or blockquotes); unsupported syntax renders literally.
480
- return [textBlock(asString(props["value"]))];
481
- case "Fact":
482
- return [convertFacts([element])];
483
- case "Image": {
484
- const size = props["size"];
485
- let mappedSize: "small" | "medium" | "large" | undefined;
486
- if (size === "sm") mappedSize = "small";
487
- else if (size === "md") mappedSize = "medium";
488
- else if (size === "lg") mappedSize = "large";
489
- else if (typeof size === "number") {
490
- warn(context, "dropped", "Image", "A numeric size was dropped.");
491
- }
492
- return [
493
- {
494
- type: "Image",
495
- url: asString(props["src"]),
496
- altText: asString(props["alt"]),
497
- ...(mappedSize !== undefined ? { size: mappedSize } : {}),
498
- },
499
- ];
500
- }
501
- case "Divider":
502
- case "Spacer":
503
- case "Icon":
504
- return [];
505
- case "Button":
506
- return [convertButtons([element], context)];
507
- case "Select": {
508
- const name = asString(props["name"]);
509
- const placeholder = asString(props["placeholder"]);
510
- const label = asString(props["label"]);
511
- const defaultValue = props["defaultValue"];
512
- const input: TeamsCardElement = {
513
- type: "Input.ChoiceSet",
514
- id: reservedSafeId(name || "select", "Select", context),
515
- style: "compact",
516
- choices: choicesFrom(props["options"], "Select", context),
517
- ...(typeof defaultValue === "string" && defaultValue
518
- ? { value: defaultValue }
519
- : {}),
520
- ...(placeholder ? { placeholder } : {}),
521
- ...(label ? { label } : {}),
522
- };
523
- return withCompanionSubmit(element, input, context);
524
- }
525
- case "RadioGroup": {
526
- const name = asString(props["name"]);
527
- const label = asString(props["label"]);
528
- const defaultValue = props["defaultValue"];
529
- const input: TeamsCardElement = {
530
- type: "Input.ChoiceSet",
531
- id: reservedSafeId(name || "radiogroup", "RadioGroup", context),
532
- style: "expanded",
533
- choices: choicesFrom(props["options"], "RadioGroup", context),
534
- ...(typeof defaultValue === "string" && defaultValue
535
- ? { value: defaultValue }
536
- : {}),
537
- ...(label ? { label } : {}),
538
- };
539
- return withCompanionSubmit(element, input, context);
540
- }
541
- case "CheckboxGroup": {
542
- const name = asString(props["name"]);
543
- const label = asString(props["label"]);
544
- const checked = Array.isArray(props["defaultValue"])
545
- ? props["defaultValue"].filter(
546
- (value): value is string =>
547
- typeof value === "string" && value !== "",
548
- )
549
- : [];
550
- const input: TeamsCardElement = {
551
- type: "Input.ChoiceSet",
552
- id: reservedSafeId(name || "checkboxgroup", "CheckboxGroup", context),
553
- style: "expanded",
554
- isMultiSelect: true,
555
- choices: choicesFrom(props["options"], "CheckboxGroup", context),
556
- ...(checked.length > 0 ? { value: checked.join(",") } : {}),
557
- ...(label ? { label } : {}),
558
- };
559
- return withCompanionSubmit(element, input, context);
560
- }
561
- case "Checkbox": {
562
- const name = asString(props["name"]);
563
- const label = asString(props["label"]);
564
- const input: TeamsCardElement = {
565
- type: "Input.Toggle",
566
- id: reservedSafeId(name || label || "checkbox", "Checkbox", context),
567
- title: label,
568
- value: props["defaultChecked"] === true ? "true" : "false",
569
- valueOn: "true",
570
- valueOff: "false",
571
- };
572
- return withCompanionSubmit(element, input, context);
573
- }
574
- case "Slider": {
575
- const name = asString(props["name"]);
576
- const label = asString(props["label"]);
577
- const min = asFiniteNumber(props["min"]);
578
- const max = asFiniteNumber(props["max"]);
579
- const defaultValue = asFiniteNumber(props["defaultValue"]);
580
- const input = {
581
- type: "Input.Number",
582
- id: reservedSafeId(name || "slider", "Slider", context),
583
- ...(label ? { label } : {}),
584
- ...(min !== undefined ? { min } : {}),
585
- ...(max !== undefined ? { max } : {}),
586
- ...(defaultValue !== undefined ? { value: defaultValue } : {}),
587
- } as unknown as TeamsCardElement;
588
- return withCompanionSubmit(element, input, context);
589
- }
590
- case "Input": {
591
- const name = asString(props["name"]);
592
- const label = asString(props["label"]);
593
- const placeholder = asString(props["placeholder"]);
594
- const defaultValue = props["defaultValue"];
595
- const input: TeamsCardElement = {
596
- type: "Input.Text",
597
- id: reservedSafeId(name || "input", "Input", context),
598
- ...(props["inputType"] === "password" ? { style: "password" } : {}),
599
- ...(label ? { label } : {}),
600
- ...(placeholder ? { placeholder } : {}),
601
- ...(typeof defaultValue === "string" && defaultValue
602
- ? { value: defaultValue }
603
- : {}),
604
- ...(props["multiline"] === true &&
605
- (props["inputType"] === undefined || props["inputType"] === "text")
606
- ? { isMultiline: true }
607
- : {}),
608
- };
609
- return withCompanionSubmit(element, input, context);
610
- }
611
- case "DatePicker": {
612
- const name = asString(props["name"]);
613
- const label = asString(props["label"]);
614
- const mode = props["inputType"];
615
- if (mode === "time") {
616
- const id = reservedSafeId(name || "datepicker", "DatePicker", context);
617
- const rawValue = asString(props["value"]);
618
- const parts = splitTemporalMinutes(rawValue);
619
- if (
620
- classifyTemporal(rawValue).kind === "time" &&
621
- parts?.droppedPrecision
622
- ) {
623
- warn(
624
- context,
625
- "dropped",
626
- "DatePicker",
627
- "Nonzero seconds were dropped from the time value.",
628
- );
629
- }
630
- const value = parts?.time;
631
- const rawMin = asString(props["min"]);
632
- const minParts = splitTemporalMinutes(rawMin);
633
- let min = minParts?.time;
634
- if (
635
- classifyTemporal(rawMin).kind === "time" &&
636
- minParts?.droppedPrecision
637
- ) {
638
- const minuteOfDay =
639
- Number(minParts.time.slice(0, 2)) * 60 +
640
- Number(minParts.time.slice(3, 5)) +
641
- 1;
642
- if (minuteOfDay === 24 * 60) {
643
- min = undefined;
644
- warn(
645
- context,
646
- "dropped",
647
- "DatePicker",
648
- "The time minimum rounds past 23:59 and was dropped.",
649
- );
650
- } else {
651
- min = `${String(Math.floor(minuteOfDay / 60)).padStart(2, "0")}:${String(minuteOfDay % 60).padStart(2, "0")}`;
652
- }
653
- }
654
- const rawMax = asString(props["max"]);
655
- const maxParts = splitTemporalMinutes(rawMax);
656
- const max = maxParts?.time;
657
- if (
658
- classifyTemporal(rawMax).kind === "time" &&
659
- maxParts?.droppedPrecision
660
- ) {
661
- warn(
662
- context,
663
- "dropped",
664
- "DatePicker",
665
- rawMax.slice(6, 8) === "00"
666
- ? "Nonzero fractional seconds were dropped from the time maximum."
667
- : /\.\d*[1-9]/.test(rawMax)
668
- ? "Nonzero seconds and fractional seconds were dropped from the time maximum."
669
- : "Nonzero seconds were dropped from the time maximum.",
670
- );
671
- }
672
- const input: TeamsCardElement = {
673
- type: "Input.Time",
674
- id,
675
- ...(label ? { label } : {}),
676
- ...(classifyTemporal(rawValue).kind === "time" && value !== undefined
677
- ? { value }
678
- : {}),
679
- ...(classifyTemporal(rawMin).kind === "time" && min !== undefined
680
- ? { min }
681
- : {}),
682
- ...(classifyTemporal(rawMax).kind === "time" && max !== undefined
683
- ? { max }
684
- : {}),
685
- };
686
- return withCompanionSubmit(element, input, context);
687
- }
688
- if (mode === "datetime") {
689
- const rawValue = asString(props["value"]);
690
- const temporal = classifyTemporal(rawValue);
691
- const fieldId = reservedSafeId(
692
- name || "datepicker",
693
- "DatePicker",
694
- context,
695
- );
696
- const dateId = encodeTemporalInputId({ fieldId, role: "date" });
697
- const timeId = encodeTemporalInputId({
698
- fieldId,
699
- role: "time",
700
- ...(temporal.kind === "instant" ? { previousValue: rawValue } : {}),
701
- });
702
- const parts = splitTemporalMinutes(rawValue);
703
- if (temporal.kind === "floating" && parts?.droppedPrecision) {
704
- warn(
705
- context,
706
- "dropped",
707
- "DatePicker",
708
- "Nonzero seconds were dropped from the datetime value.",
709
- );
710
- }
711
- const offsetLabel = temporalOffsetLabel(rawValue);
712
- const boundDate = (raw: unknown) => {
713
- const value = asString(raw);
714
- return classifyTemporal(value).kind === "date"
715
- ? value
716
- : splitTemporalMinutes(
717
- value,
718
- temporal.kind === "instant" ? temporal.offset : undefined,
719
- )?.date;
720
- };
721
- const min = boundDate(props["min"]);
722
- const max = boundDate(props["max"]);
723
- const dateInput: TeamsCardElement = {
724
- type: "Input.Date",
725
- id: dateId,
726
- ...(label ? { label } : {}),
727
- ...(parts?.date !== undefined ? { value: parts.date } : {}),
728
- ...(min !== undefined ? { min } : {}),
729
- ...(max !== undefined ? { max } : {}),
730
- };
731
- const timeInput: TeamsCardElement = {
732
- type: "Input.Time",
733
- id: timeId,
734
- ...(label || offsetLabel
735
- ? {
736
- label: `${label ? `${label} time` : "Time"}${offsetLabel ? ` (${offsetLabel})` : ""}`,
737
- }
738
- : {}),
739
- ...(parts?.time !== undefined ? { value: parts.time } : {}),
740
- };
741
- if (element.action === undefined) return [dateInput, timeInput];
742
- const [input, submit] = withCompanionSubmit(
743
- element,
744
- timeInput,
745
- context,
746
- );
747
- return [dateInput, input!, submit!];
748
- }
749
- if (mode !== undefined && mode !== "date") {
750
- warn(
751
- context,
752
- "dropped",
753
- "DatePicker",
754
- "Unsupported inputType was dropped; the picker was rendered as a date input.",
755
- );
756
- }
757
- const rawValue = props["value"];
758
- const value =
759
- typeof rawValue === "string" && DATE_PATTERN.test(rawValue)
760
- ? rawValue
761
- : undefined;
762
- const rawMin = props["min"];
763
- const min =
764
- typeof rawMin === "string" && DATE_PATTERN.test(rawMin)
765
- ? rawMin
766
- : undefined;
767
- const rawMax = props["max"];
768
- const max =
769
- typeof rawMax === "string" && DATE_PATTERN.test(rawMax)
770
- ? rawMax
771
- : undefined;
772
- const input: TeamsCardElement = {
773
- type: "Input.Date",
774
- id: reservedSafeId(name || "datepicker", "DatePicker", context),
775
- ...(label ? { label } : {}),
776
- ...(value !== undefined ? { value } : {}),
777
- ...(min !== undefined ? { min } : {}),
778
- ...(max !== undefined ? { max } : {}),
779
- };
780
- return withCompanionSubmit(element, input, context);
781
- }
782
- case "Form":
783
- return [
784
- ...convertSequence(element.children, context, depth + 1),
785
- {
786
- type: "ActionSet",
787
- actions: [buildSubmitAction("Submit", element.action)],
788
- },
789
- ];
790
- case "Card": {
791
- const titleText = asString(props["title"]);
792
- const items: TeamsCardElement[] = [];
793
- if (titleText) {
794
- items.push(
795
- textBlock(titleText, {
796
- size: "large",
797
- weight: "bolder",
798
- style: "heading",
799
- }),
800
- );
801
- }
802
- items.push(...convertSequence(element.children, context, depth + 1));
803
- const container: TeamsContainer = {
804
- type: "Container",
805
- style: "default",
806
- items,
807
- };
808
- const actionSet = cardFooterActionSet(props);
809
- return actionSet !== undefined ? [container, actionSet] : [container];
810
- }
811
- case "Col":
812
- case "Box":
813
- return [
814
- {
815
- type: "Container",
816
- items: convertSequence(element.children, context, depth + 1),
817
- },
818
- ];
819
- case "Row": {
820
- const children = normalizedList(element.children);
821
- if (children.length > 3) {
822
- warn(context, "advisory", "Row", "Teams recommends at most 3 columns.");
823
- }
824
- const columns: TeamsColumn[] = children.map((child) => ({
825
- type: "Column",
826
- width: "auto",
827
- items: convertSequence(child, context, depth + 1),
828
- }));
829
- return [{ type: "ColumnSet", columns }];
830
- }
831
- case "ListView": {
832
- const containers: TeamsContainer[] = [];
833
- let discarded = 0;
834
- for (const child of normalizedList(element.children)) {
835
- if (isElement(child) && child.type === "ListViewItem") {
836
- containers.push(convertListViewItem(child, context, depth + 1));
837
- continue;
838
- }
839
- if (discardedChild(child, context, depth + 1)) discarded += 1;
840
- }
841
- if (discarded > 0) {
842
- warn(
843
- context,
844
- "dropped",
845
- "ListView",
846
- `${discarded} non-item ${discarded === 1 ? "child was" : "children were"} dropped.`,
847
- );
848
- }
849
- return containers.map((container, index) =>
850
- index > 0 ? { ...container, separator: true } : container,
851
- );
852
- }
853
- case "ListViewItem":
854
- return [convertListViewItem(element, context, depth)];
855
- case "Table":
856
- return [convertTable(props, context)];
857
- case "Alert": {
858
- const tone = props["tone"] ?? "info";
859
- const style =
860
- typeof tone === "string"
861
- ? (ALERT_STYLE_MAP[tone] ?? "default")
862
- : "default";
863
- const titleText = asString(props["title"]);
864
- const description = asString(props["description"]);
865
- const items: TeamsCardElement[] = [];
866
- if (titleText) items.push(textBlock(titleText, { weight: "bolder" }));
867
- if (description) items.push(textBlock(description));
868
- return [{ type: "Container", style, items }];
869
- }
870
- case "Carousel": {
871
- warn(
872
- context,
873
- "fallback",
874
- "Carousel",
875
- "A carousel was rendered as sequential cards because it is not at the root.",
876
- );
877
- const cards: NormalizedUIElement[] = [];
878
- let droppedCards = 0;
879
- for (const child of normalizedList(element.children)) {
880
- if (isElement(child) && child.type === "Card") {
881
- cards.push(child);
882
- continue;
883
- }
884
- if (discardedChild(child, context, depth + 1)) droppedCards += 1;
885
- }
886
- if (droppedCards > 0) {
887
- warn(
888
- context,
889
- "dropped",
890
- "Carousel",
891
- `${droppedCards} non-card ${droppedCards === 1 ? "child was" : "children were"} dropped.`,
892
- );
893
- }
894
- return cards.flatMap((card) => convertElement(card, context, depth + 1));
895
- }
896
- case "Chart":
897
- warn(
898
- context,
899
- "dropped",
900
- "Chart",
901
- "Chart was replaced by a Teams omission note.",
902
- );
903
- return [
904
- textBlock("Chart omitted on Teams.", { isSubtle: true, size: "small" }),
905
- ];
906
- default:
907
- warn(
908
- context,
909
- "dropped",
910
- element.type,
911
- "Unknown component type was dropped.",
912
- );
913
- return [];
914
- }
915
- }
916
-
917
- /**
918
- * Walks a normalized sequence into card body elements. Nested arrays are
919
- * flattened into one flat sibling sequence first (via {@link normalizedList}),
920
- * so a `Divider`/`Fact`/`Button` run merges correctly even when the model
921
- * wrapped some of its siblings in their own array. Consecutive `Fact`
922
- * siblings merge into one FactSet and consecutive `Button` siblings merge
923
- * into one ActionSet; a `Divider`/`Spacer` emits nothing itself but sets
924
- * `separator`/`spacing` on the next emitted sibling (a trailing or orphan
925
- * one is dropped silently, since there is no sibling left to carry it).
926
- * `depth` advances by exactly one per element-nesting level: this call does
927
- * not itself add a level, since flattening a sibling array or converting a
928
- * sibling element is not a descent; only a container's own recursive
929
- * `convertSequence(children, depth + 1)` call is.
930
- */
931
- export function convertSequence(
932
- node: NormalizedUINode | undefined,
933
- context: ConversionContext,
934
- depth: number,
935
- ): TeamsCardElement[] {
936
- if (node === undefined || node === null || depth > MAX_TRAVERSAL_DEPTH) {
937
- return [];
938
- }
939
- const nodes = normalizedList(node);
940
- const items: TeamsCardElement[] = [];
941
- let pendingSeparator = false;
942
- let pendingSpacing = false;
943
-
944
- const emit = (converted: TeamsCardElement[]) => {
945
- const [first, ...rest] = converted;
946
- if (first === undefined) return;
947
- items.push(applyPending(first, pendingSeparator, pendingSpacing), ...rest);
948
- pendingSeparator = false;
949
- pendingSpacing = false;
950
- };
951
-
952
- for (let index = 0; index < nodes.length;) {
953
- const current = nodes[index];
954
- if (current === undefined || current === null) {
955
- index += 1;
956
- continue;
957
- }
958
- if (isElement(current) && current.type === "Divider") {
959
- pendingSeparator = true;
960
- index += 1;
961
- continue;
962
- }
963
- if (isElement(current) && current.type === "Spacer") {
964
- pendingSpacing = true;
965
- index += 1;
966
- continue;
967
- }
968
- if (isElement(current) && current.type === "Fact") {
969
- const { run: facts, next } = takeRun(
970
- nodes,
971
- index,
972
- (candidate) => candidate.type === "Fact",
973
- );
974
- index = next;
975
- emit([convertFacts(facts)]);
976
- continue;
977
- }
978
- if (isElement(current) && current.type === "Button") {
979
- const { run: buttons, next } = takeRun(
980
- nodes,
981
- index,
982
- (candidate) => candidate.type === "Button",
983
- );
984
- index = next;
985
- emit([convertButtons(buttons, context)]);
986
- continue;
987
- }
988
- if (typeof current === "string" || typeof current === "number") {
989
- emit([textBlock(String(current))]);
990
- } else {
991
- emit(convertElement(current as NormalizedUIElement, context, depth));
992
- }
993
- index += 1;
994
- }
995
- return items;
996
- }
997
-
998
- /** Builds a full card (schema envelope plus body) from an already-normalized root, checking the soft payload budget. */
999
- export function convertRootToCard(
1000
- root: NormalizedUINode | readonly NormalizedUINode[],
1001
- context: ConversionContext,
1002
- ): TeamsAdaptiveCard {
1003
- context.usedInputIds = new Set();
1004
- const body = convertSequence(root, context, 0);
1005
- const card = buildCard(body);
1006
- const size = utf8ByteLength(JSON.stringify(card));
1007
- if (size > PAYLOAD_SOFT_CAP) {
1008
- warn(
1009
- context,
1010
- "advisory",
1011
- "Root",
1012
- `the card is ${size} bytes, over the ${PAYLOAD_SOFT_CAP}-byte soft budget kept below Teams' 100 KB bot message limit.`,
1013
- );
1014
- }
1015
- return card;
1016
- }
1017
-
1018
- /**
1019
- * Converts a generative-ui tree into a Microsoft Teams Adaptive Card and
1020
- * non-fatal conversion warnings. Sizes, weights, and colors map to Adaptive
1021
- * Card's semantic enums rather than raw values. An Input/Select/RadioGroup/
1022
- * CheckboxGroup/Checkbox/Slider/DatePicker whose id would be the reserved
1023
- * {@link RESERVED_INPUT_ID} is renamed with a warning (see `decodeSubmitData`).
1024
- * Never throws: an unknown `$type` is skipped with a "dropped" warning, and a
1025
- * malformed input resolves to an empty card plus a "dropped" warning instead
1026
- * of throwing.
1027
- */
1028
- export function toAdaptiveCard(
1029
- node: unknown,
1030
- _options?: ToAdaptiveCardOptions,
1031
- ): AdaptiveCardResult {
1032
- const context: ConversionContext = {
1033
- warnings: [],
1034
- usedInputIds: new Set(),
1035
- };
1036
- try {
1037
- const bounded = boundSpec(node, (reason) =>
1038
- warn(context, "clamped", "Root", clampReasonDetail(reason)),
1039
- );
1040
- const { root } = normalizeSpec(bounded as never);
1041
- return {
1042
- card: convertRootToCard(root, context),
1043
- warnings: context.warnings,
1044
- };
1045
- } catch {
1046
- return {
1047
- card: buildCard([]),
1048
- warnings: [
1049
- {
1050
- code: "dropped",
1051
- component: "Root",
1052
- detail: "Input could not be converted.",
1053
- },
1054
- ],
1055
- };
1056
- }
1057
- }