@assistant-ui/react-generative-ui 0.0.6 → 0.0.8

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 (201) hide show
  1. package/dist/JSONGenerativeUI.client.d.ts +4 -2
  2. package/dist/JSONGenerativeUI.client.d.ts.map +1 -1
  3. package/dist/JSONGenerativeUI.client.js +9 -2
  4. package/dist/JSONGenerativeUI.client.js.map +1 -1
  5. package/dist/JSONGenerativeUI.server.d.ts +0 -1
  6. package/dist/JSONGenerativeUI.server.d.ts.map +1 -1
  7. package/dist/JSONGenerativeUI.shared.d.ts +8 -1
  8. package/dist/JSONGenerativeUI.shared.d.ts.map +1 -1
  9. package/dist/JSONGenerativeUI.shared.js.map +1 -1
  10. package/dist/actionRegistry.d.ts +32 -0
  11. package/dist/actionRegistry.d.ts.map +1 -0
  12. package/dist/actionRegistry.js +41 -0
  13. package/dist/actionRegistry.js.map +1 -0
  14. package/dist/buildPresentParameters.d.ts +5 -5
  15. package/dist/buildPresentParameters.d.ts.map +1 -1
  16. package/dist/buildPresentParameters.js +20 -6
  17. package/dist/buildPresentParameters.js.map +1 -1
  18. package/dist/constants.d.ts.map +1 -1
  19. package/dist/defineGenerativeComponents.d.ts +0 -1
  20. package/dist/defineGenerativeComponents.d.ts.map +1 -1
  21. package/dist/generativeUIToJSX.d.ts +15 -10
  22. package/dist/generativeUIToJSX.d.ts.map +1 -1
  23. package/dist/generativeUIToJSX.js +47 -17
  24. package/dist/generativeUIToJSX.js.map +1 -1
  25. package/dist/index.d.ts +8 -4
  26. package/dist/index.js +5 -2
  27. package/dist/index.server.d.ts +13 -0
  28. package/dist/index.server.js +10 -0
  29. package/dist/index.shared.d.ts +11 -0
  30. package/dist/index.shared.js +9 -0
  31. package/dist/ir.d.ts +115 -0
  32. package/dist/ir.d.ts.map +1 -0
  33. package/dist/ir.js +175 -0
  34. package/dist/ir.js.map +1 -0
  35. package/dist/node_modules/.pnpm/@types_json-schema@7.0.15/node_modules/@types/json-schema/index.d.ts +3 -3
  36. package/dist/node_modules/.pnpm/@types_json-schema@7.0.15/node_modules/@types/json-schema/index.d.ts.map +1 -1
  37. package/dist/renderGenerativeUI.d.ts +5 -5
  38. package/dist/renderGenerativeUI.d.ts.map +1 -1
  39. package/dist/renderGenerativeUI.js +18 -45
  40. package/dist/renderGenerativeUI.js.map +1 -1
  41. package/dist/slack/constants.d.ts +111 -0
  42. package/dist/slack/constants.d.ts.map +1 -0
  43. package/dist/slack/constants.js +168 -0
  44. package/dist/slack/constants.js.map +1 -0
  45. package/dist/slack/decodeBlockAction.d.ts +11 -0
  46. package/dist/slack/decodeBlockAction.d.ts.map +1 -0
  47. package/dist/slack/decodeBlockAction.js +40 -0
  48. package/dist/slack/decodeBlockAction.js.map +1 -0
  49. package/dist/slack/fromSlackBlocks.d.ts +30 -0
  50. package/dist/slack/fromSlackBlocks.d.ts.map +1 -0
  51. package/dist/slack/fromSlackBlocks.js +447 -0
  52. package/dist/slack/fromSlackBlocks.js.map +1 -0
  53. package/dist/slack/toSlackBlocks.d.ts +7 -0
  54. package/dist/slack/toSlackBlocks.d.ts.map +1 -0
  55. package/dist/slack/toSlackBlocks.js +649 -0
  56. package/dist/slack/toSlackBlocks.js.map +1 -0
  57. package/dist/slack/types.d.ts +178 -0
  58. package/dist/slack/types.d.ts.map +1 -0
  59. package/dist/slack/types.js +0 -0
  60. package/dist/slack.d.ts +5 -0
  61. package/dist/slack.js +4 -0
  62. package/dist/teams/boundSpec.d.ts +20 -0
  63. package/dist/teams/boundSpec.d.ts.map +1 -0
  64. package/dist/teams/boundSpec.js +67 -0
  65. package/dist/teams/boundSpec.js.map +1 -0
  66. package/dist/teams/constants.d.ts +57 -0
  67. package/dist/teams/constants.d.ts.map +1 -0
  68. package/dist/teams/constants.js +92 -0
  69. package/dist/teams/constants.js.map +1 -0
  70. package/dist/teams/decodeSubmitData.d.ts +25 -0
  71. package/dist/teams/decodeSubmitData.d.ts.map +1 -0
  72. package/dist/teams/decodeSubmitData.js +44 -0
  73. package/dist/teams/decodeSubmitData.js.map +1 -0
  74. package/dist/teams/toAdaptiveCard.d.ts +39 -0
  75. package/dist/teams/toAdaptiveCard.d.ts.map +1 -0
  76. package/dist/teams/toAdaptiveCard.js +496 -0
  77. package/dist/teams/toAdaptiveCard.js.map +1 -0
  78. package/dist/teams/toTeamsAttachments.d.ts +13 -0
  79. package/dist/teams/toTeamsAttachments.d.ts.map +1 -0
  80. package/dist/teams/toTeamsAttachments.js +95 -0
  81. package/dist/teams/toTeamsAttachments.js.map +1 -0
  82. package/dist/teams/types.d.ts +196 -0
  83. package/dist/teams/types.d.ts.map +1 -0
  84. package/dist/teams/types.js +0 -0
  85. package/dist/teams.d.ts +5 -0
  86. package/dist/teams.js +4 -0
  87. package/dist/types.d.ts +41 -34
  88. package/dist/types.d.ts.map +1 -1
  89. package/dist/vocabulary/alert.d.ts +28 -0
  90. package/dist/vocabulary/alert.d.ts.map +1 -0
  91. package/dist/vocabulary/alert.js +58 -0
  92. package/dist/vocabulary/alert.js.map +1 -0
  93. package/dist/vocabulary/collectFormValues.d.ts +23 -0
  94. package/dist/vocabulary/collectFormValues.d.ts.map +1 -0
  95. package/dist/vocabulary/collectFormValues.js +32 -0
  96. package/dist/vocabulary/collectFormValues.js.map +1 -0
  97. package/dist/vocabulary/data.d.ts +52 -0
  98. package/dist/vocabulary/data.d.ts.map +1 -0
  99. package/dist/vocabulary/data.js +294 -0
  100. package/dist/vocabulary/data.js.map +1 -0
  101. package/dist/vocabulary/dispatch.d.ts +11 -0
  102. package/dist/vocabulary/dispatch.d.ts.map +1 -0
  103. package/dist/vocabulary/dispatch.js +21 -0
  104. package/dist/vocabulary/dispatch.js.map +1 -0
  105. package/dist/vocabulary/fact.d.ts +15 -0
  106. package/dist/vocabulary/fact.d.ts.map +1 -0
  107. package/dist/vocabulary/fact.js +24 -0
  108. package/dist/vocabulary/fact.js.map +1 -0
  109. package/dist/vocabulary/form.d.ts +14 -0
  110. package/dist/vocabulary/form.d.ts.map +1 -0
  111. package/dist/vocabulary/form.js +23 -0
  112. package/dist/vocabulary/form.js.map +1 -0
  113. package/dist/vocabulary/icon.d.ts +44 -0
  114. package/dist/vocabulary/icon.d.ts.map +1 -0
  115. package/dist/vocabulary/icon.js +272 -0
  116. package/dist/vocabulary/icon.js.map +1 -0
  117. package/dist/vocabulary/index.d.ts +15 -0
  118. package/dist/vocabulary/index.d.ts.map +1 -0
  119. package/dist/vocabulary/index.js +36 -0
  120. package/dist/vocabulary/index.js.map +1 -0
  121. package/dist/vocabulary/interactive.d.ts +93 -0
  122. package/dist/vocabulary/interactive.d.ts.map +1 -0
  123. package/dist/vocabulary/interactive.js +172 -0
  124. package/dist/vocabulary/interactive.js.map +1 -0
  125. package/dist/vocabulary/layout.d.ts +82 -0
  126. package/dist/vocabulary/layout.d.ts.map +1 -0
  127. package/dist/vocabulary/layout.js +136 -0
  128. package/dist/vocabulary/layout.js.map +1 -0
  129. package/dist/vocabulary/list.d.ts +17 -0
  130. package/dist/vocabulary/list.d.ts.map +1 -0
  131. package/dist/vocabulary/list.js +48 -0
  132. package/dist/vocabulary/list.js.map +1 -0
  133. package/dist/vocabulary/media.d.ts +28 -0
  134. package/dist/vocabulary/media.d.ts.map +1 -0
  135. package/dist/vocabulary/media.js +42 -0
  136. package/dist/vocabulary/media.js.map +1 -0
  137. package/dist/vocabulary/text.d.ts +60 -0
  138. package/dist/vocabulary/text.d.ts.map +1 -0
  139. package/dist/vocabulary/text.js +48 -0
  140. package/dist/vocabulary/text.js.map +1 -0
  141. package/package.json +21 -13
  142. package/src/JSONGenerativeUI.client.tsx +10 -2
  143. package/src/JSONGenerativeUI.shared.ts +8 -0
  144. package/src/actionRegistry.test.ts +90 -0
  145. package/src/actionRegistry.ts +88 -0
  146. package/src/buildPresentParameters.ts +38 -16
  147. package/src/generativeUIToJSX.test.ts +186 -0
  148. package/src/generativeUIToJSX.ts +112 -21
  149. package/src/index.server.ts +2 -0
  150. package/src/index.shared.ts +61 -0
  151. package/src/index.ts +2 -26
  152. package/src/ir.test.ts +203 -0
  153. package/src/ir.ts +279 -0
  154. package/src/renderGenerativeUI.test.tsx +69 -2
  155. package/src/renderGenerativeUI.tsx +49 -82
  156. package/src/slack/constants.ts +217 -0
  157. package/src/slack/decodeBlockAction.test.ts +135 -0
  158. package/src/slack/decodeBlockAction.ts +66 -0
  159. package/src/slack/fromSlackBlocks.test.ts +1145 -0
  160. package/src/slack/fromSlackBlocks.ts +709 -0
  161. package/src/slack/toSlackBlocks.test.ts +1936 -0
  162. package/src/slack/toSlackBlocks.ts +1319 -0
  163. package/src/slack/types.ts +230 -0
  164. package/src/slack.ts +37 -0
  165. package/src/teams/boundSpec.ts +102 -0
  166. package/src/teams/constants.ts +127 -0
  167. package/src/teams/decodeSubmitData.test.ts +148 -0
  168. package/src/teams/decodeSubmitData.ts +57 -0
  169. package/src/teams/toAdaptiveCard.test.ts +1410 -0
  170. package/src/teams/toAdaptiveCard.ts +762 -0
  171. package/src/teams/toTeamsAttachments.test.ts +29 -0
  172. package/src/teams/toTeamsAttachments.ts +137 -0
  173. package/src/teams/types.ts +237 -0
  174. package/src/teams.ts +34 -0
  175. package/src/types.ts +57 -42
  176. package/src/vocabulary/alert.test.tsx +136 -0
  177. package/src/vocabulary/alert.tsx +66 -0
  178. package/src/vocabulary/collectFormValues.test.ts +127 -0
  179. package/src/vocabulary/collectFormValues.ts +58 -0
  180. package/src/vocabulary/data.test.tsx +567 -0
  181. package/src/vocabulary/data.tsx +494 -0
  182. package/src/vocabulary/dispatch.ts +22 -0
  183. package/src/vocabulary/fact.test.tsx +15 -0
  184. package/src/vocabulary/fact.tsx +22 -0
  185. package/src/vocabulary/form.test.tsx +112 -0
  186. package/src/vocabulary/form.tsx +34 -0
  187. package/src/vocabulary/icon.test.tsx +64 -0
  188. package/src/vocabulary/icon.tsx +174 -0
  189. package/src/vocabulary/index.test.ts +63 -0
  190. package/src/vocabulary/index.ts +33 -0
  191. package/src/vocabulary/interactive.dispatch.test.tsx +352 -0
  192. package/src/vocabulary/interactive.test.tsx +248 -0
  193. package/src/vocabulary/interactive.tsx +269 -0
  194. package/src/vocabulary/layout.test.tsx +289 -0
  195. package/src/vocabulary/layout.tsx +229 -0
  196. package/src/vocabulary/list.test.tsx +195 -0
  197. package/src/vocabulary/list.tsx +48 -0
  198. package/src/vocabulary/media.test.tsx +85 -0
  199. package/src/vocabulary/media.tsx +56 -0
  200. package/src/vocabulary/text.test.tsx +73 -0
  201. package/src/vocabulary/text.tsx +59 -0
@@ -0,0 +1,709 @@
1
+ import type { Action, UIElement } from "../ir";
2
+ import {
3
+ ACTIONS_ELEMENT_CAP,
4
+ CARD_ACTIONS_CAP,
5
+ CAROUSEL_CARD_CAP,
6
+ CONTEXT_ELEMENT_CAP,
7
+ DATA_TABLE_COLUMN_CAP,
8
+ DATA_TABLE_ROW_CAP,
9
+ FACT_FIELD_CAP,
10
+ INBOUND_BLOCK_CAP,
11
+ RADIO_OPTION_CAP,
12
+ SELECT_OPTION_CAP,
13
+ } from "./constants";
14
+ import type { FromSlackBlocksResult, SlackConversionWarning } from "./types";
15
+
16
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
17
+ typeof value === "object" && value !== null && !Array.isArray(value);
18
+
19
+ const isDefined = <T>(value: T | undefined): value is T => value !== undefined;
20
+
21
+ const asString = (value: unknown): string =>
22
+ typeof value === "string" ? value : "";
23
+
24
+ const textOf = (value: unknown): string =>
25
+ isRecord(value) && typeof value["text"] === "string" ? value["text"] : "";
26
+
27
+ const warn = (
28
+ warnings: SlackConversionWarning[],
29
+ code: SlackConversionWarning["code"],
30
+ component: string,
31
+ detail: string,
32
+ ) => {
33
+ warnings.push({ code, component, detail });
34
+ };
35
+
36
+ /**
37
+ * The shared bounded-iteration primitive: slices `value` to `cap` entries
38
+ * without ever reading past that many indices, so a hostile array (sparse or
39
+ * proxied with a fabricated `length`) cannot stall the event loop. `slice`
40
+ * itself performs the bounded read; only its own reported length is
41
+ * inspected to detect truncation.
42
+ */
43
+ const clampArray = (
44
+ value: unknown,
45
+ cap: number,
46
+ ): { readonly items: unknown[]; readonly truncated: boolean } => {
47
+ if (!Array.isArray(value)) return { items: [], truncated: false };
48
+ return { items: value.slice(0, cap), truncated: value.length > cap };
49
+ };
50
+
51
+ const boundedArray = (
52
+ value: unknown,
53
+ cap: number,
54
+ warnings: SlackConversionWarning[],
55
+ component: string,
56
+ detail: string,
57
+ ): unknown[] => {
58
+ const { items, truncated } = clampArray(value, cap);
59
+ if (truncated) warn(warnings, "clamped", component, detail);
60
+ return items;
61
+ };
62
+
63
+ const parseValuePayload = (value: unknown): Record<string, unknown> => {
64
+ if (typeof value !== "string") return {};
65
+ try {
66
+ const parsed: unknown = JSON.parse(value);
67
+ return isRecord(parsed) ? parsed : {};
68
+ } catch {
69
+ return {};
70
+ }
71
+ };
72
+
73
+ const decodeAction = (actionId: unknown, value: unknown): Action => ({
74
+ ...parseValuePayload(value),
75
+ type: typeof actionId === "string" ? actionId : "action",
76
+ });
77
+
78
+ const headerFrom = (block: Record<string, unknown>): UIElement => ({
79
+ $type: "Header",
80
+ text: textOf(block["text"]),
81
+ });
82
+
83
+ const imageFrom = (block: Record<string, unknown>): UIElement => ({
84
+ $type: "Image",
85
+ src: asString(block["image_url"]),
86
+ alt: asString(block["alt_text"]),
87
+ });
88
+
89
+ const markdownFrom = (block: Record<string, unknown>): UIElement => ({
90
+ $type: "Markdown",
91
+ value: asString(block["text"]),
92
+ });
93
+
94
+ const FACT_FIELD_PATTERN = /^\*([\s\S]*?)\*\n([\s\S]*)$/;
95
+
96
+ const factFrom = (
97
+ field: unknown,
98
+ warnings: SlackConversionWarning[],
99
+ ): UIElement => {
100
+ const text = textOf(field);
101
+ const match = FACT_FIELD_PATTERN.exec(text);
102
+ if (match) {
103
+ const [, label, value] = match;
104
+ return { $type: "Fact", label, value };
105
+ }
106
+ warn(
107
+ warnings,
108
+ "fallback",
109
+ "Fact",
110
+ "A section field that did not match the label/value format was kept as a value-only fact.",
111
+ );
112
+ return { $type: "Fact", label: "", value: text };
113
+ };
114
+
115
+ const listViewItemFrom = (
116
+ text: string,
117
+ button: Record<string, unknown>,
118
+ ): UIElement => ({
119
+ $type: "ListViewItem",
120
+ $action: decodeAction(button["action_id"], button["value"]),
121
+ children: { $type: "Text", value: text },
122
+ });
123
+
124
+ /**
125
+ * Recognizes a section block that stands for a `ListViewItem`: mrkdwn text
126
+ * paired with a button accessory, and no `fields` (a fields section is
127
+ * always a Facts section, never a list row).
128
+ */
129
+ const listViewItemFromSection = (block: unknown): UIElement | undefined => {
130
+ if (!isRecord(block) || block["type"] !== "section") return undefined;
131
+ if (Array.isArray(block["fields"])) return undefined;
132
+ const accessory = block["accessory"];
133
+ if (!isRecord(accessory) || accessory["type"] !== "button") {
134
+ return undefined;
135
+ }
136
+ return listViewItemFrom(textOf(block["text"]), accessory);
137
+ };
138
+
139
+ const isDividerBlock = (block: unknown): boolean =>
140
+ isRecord(block) && block["type"] === "divider";
141
+
142
+ const sectionFrom = (
143
+ block: Record<string, unknown>,
144
+ warnings: SlackConversionWarning[],
145
+ ): UIElement[] => {
146
+ const fields = block["fields"];
147
+ if (Array.isArray(fields)) {
148
+ return boundedArray(
149
+ fields,
150
+ FACT_FIELD_CAP,
151
+ warnings,
152
+ "Fact",
153
+ `fields were clamped to ${FACT_FIELD_CAP} entries.`,
154
+ ).map((field) => factFrom(field, warnings));
155
+ }
156
+ return [{ $type: "Text", value: textOf(block["text"]) }];
157
+ };
158
+
159
+ const captionFrom = (element: unknown): UIElement => ({
160
+ $type: "Caption",
161
+ value: textOf(element),
162
+ });
163
+
164
+ const contextFrom = (
165
+ block: Record<string, unknown>,
166
+ warnings: SlackConversionWarning[],
167
+ ): UIElement => {
168
+ const elements = boundedArray(
169
+ block["elements"],
170
+ CONTEXT_ELEMENT_CAP,
171
+ warnings,
172
+ "Row",
173
+ `elements were clamped to ${CONTEXT_ELEMENT_CAP} entries.`,
174
+ );
175
+ if (elements.length > 1) {
176
+ return { $type: "Row", children: elements.map(captionFrom) };
177
+ }
178
+ return {
179
+ $type: "Caption",
180
+ value: elements.length === 1 ? textOf(elements[0]) : "",
181
+ };
182
+ };
183
+
184
+ const optionFrom = (
185
+ option: unknown,
186
+ ): { label: string; value: string } | undefined => {
187
+ if (!isRecord(option) || typeof option["value"] !== "string") {
188
+ return undefined;
189
+ }
190
+ return { label: textOf(option["text"]), value: option["value"] };
191
+ };
192
+
193
+ const buttonFrom = (element: Record<string, unknown>): UIElement => {
194
+ const style = element["style"];
195
+ return {
196
+ $type: "Button",
197
+ label: textOf(element["text"]),
198
+ ...(style === "primary" || style === "danger"
199
+ ? { buttonStyle: style }
200
+ : {}),
201
+ $action: decodeAction(element["action_id"], element["value"]),
202
+ };
203
+ };
204
+
205
+ const selectFrom = (
206
+ element: Record<string, unknown>,
207
+ warnings: SlackConversionWarning[],
208
+ ): UIElement => {
209
+ const options = boundedArray(
210
+ element["options"],
211
+ SELECT_OPTION_CAP,
212
+ warnings,
213
+ "Select",
214
+ `options were clamped to ${SELECT_OPTION_CAP} entries.`,
215
+ );
216
+ const placeholder = element["placeholder"];
217
+ return {
218
+ $type: "Select",
219
+ options: options.map(optionFrom).filter(isDefined),
220
+ ...(isRecord(placeholder) ? { placeholder: textOf(placeholder) } : {}),
221
+ $action: decodeAction(element["action_id"], element["value"]),
222
+ };
223
+ };
224
+
225
+ const datePickerFrom = (element: Record<string, unknown>): UIElement => ({
226
+ $type: "DatePicker",
227
+ ...(typeof element["initial_date"] === "string"
228
+ ? { value: element["initial_date"] }
229
+ : {}),
230
+ $action: decodeAction(element["action_id"], element["value"]),
231
+ });
232
+
233
+ const checkboxFrom = (element: Record<string, unknown>): UIElement => {
234
+ const options = Array.isArray(element["options"]) ? element["options"] : [];
235
+ const first = options[0];
236
+ const initialOptions = Array.isArray(element["initial_options"])
237
+ ? element["initial_options"]
238
+ : [];
239
+ const firstValue =
240
+ isRecord(first) && typeof first["value"] === "string" ? first["value"] : "";
241
+ const firstChecked = initialOptions
242
+ .slice(0, SELECT_OPTION_CAP)
243
+ .some((option) => isRecord(option) && option["value"] === firstValue);
244
+ return {
245
+ $type: "Checkbox",
246
+ label: isRecord(first) ? textOf(first["text"]) : "",
247
+ name: firstValue,
248
+ ...(firstChecked ? { defaultChecked: true } : {}),
249
+ $action: decodeAction(element["action_id"], element["value"]),
250
+ };
251
+ };
252
+
253
+ const radioGroupFrom = (
254
+ element: Record<string, unknown>,
255
+ warnings: SlackConversionWarning[],
256
+ ): UIElement => {
257
+ const options = boundedArray(
258
+ element["options"],
259
+ RADIO_OPTION_CAP,
260
+ warnings,
261
+ "RadioGroup",
262
+ `options were clamped to ${RADIO_OPTION_CAP} entries.`,
263
+ );
264
+ const initialOption = element["initial_option"];
265
+ const initialValue =
266
+ isRecord(initialOption) && typeof initialOption["value"] === "string"
267
+ ? initialOption["value"]
268
+ : undefined;
269
+ return {
270
+ $type: "RadioGroup",
271
+ options: options.map(optionFrom).filter(isDefined),
272
+ ...(initialValue !== undefined ? { defaultValue: initialValue } : {}),
273
+ $action: decodeAction(element["action_id"], element["value"]),
274
+ };
275
+ };
276
+
277
+ const actionElementFrom = (
278
+ element: unknown,
279
+ warnings: SlackConversionWarning[],
280
+ ): UIElement | undefined => {
281
+ if (isRecord(element)) {
282
+ switch (element["type"]) {
283
+ case "button":
284
+ return buttonFrom(element);
285
+ case "static_select":
286
+ return selectFrom(element, warnings);
287
+ case "datepicker":
288
+ return datePickerFrom(element);
289
+ case "checkboxes":
290
+ return checkboxFrom(element);
291
+ case "radio_buttons":
292
+ return radioGroupFrom(element, warnings);
293
+ }
294
+ }
295
+ warn(
296
+ warnings,
297
+ "dropped",
298
+ isRecord(element) && typeof element["type"] === "string"
299
+ ? element["type"]
300
+ : "unknown",
301
+ "Unknown action element type was dropped.",
302
+ );
303
+ return undefined;
304
+ };
305
+
306
+ const actionsFrom = (
307
+ block: Record<string, unknown>,
308
+ warnings: SlackConversionWarning[],
309
+ ): UIElement[] => {
310
+ const elements = boundedArray(
311
+ block["elements"],
312
+ ACTIONS_ELEMENT_CAP,
313
+ warnings,
314
+ "Actions",
315
+ `elements were clamped to ${ACTIONS_ELEMENT_CAP} entries.`,
316
+ );
317
+ return elements
318
+ .map((element) => actionElementFrom(element, warnings))
319
+ .filter(isDefined);
320
+ };
321
+
322
+ const inputFrom = (
323
+ block: Record<string, unknown>,
324
+ warnings: SlackConversionWarning[],
325
+ ): UIElement[] => {
326
+ const element = block["element"];
327
+ if (!isRecord(element) || element["type"] !== "plain_text_input") {
328
+ warn(
329
+ warnings,
330
+ "dropped",
331
+ "Input",
332
+ "Unknown input element type was dropped.",
333
+ );
334
+ return [];
335
+ }
336
+ const placeholder = element["placeholder"];
337
+ return [
338
+ {
339
+ $type: "Input",
340
+ label: textOf(block["label"]),
341
+ ...(isRecord(placeholder) ? { placeholder: textOf(placeholder) } : {}),
342
+ ...(element["multiline"] === true ? { multiline: true } : {}),
343
+ $action: decodeAction(element["action_id"], undefined),
344
+ },
345
+ ];
346
+ };
347
+
348
+ type CardFooterButton = { readonly label: string; readonly $action: Action };
349
+
350
+ const cardFooterButtonFrom = (button: unknown): CardFooterButton | undefined =>
351
+ isRecord(button)
352
+ ? {
353
+ label: textOf(button["text"]),
354
+ $action: decodeAction(button["action_id"], button["value"]),
355
+ }
356
+ : undefined;
357
+
358
+ /**
359
+ * Assigns a card's footer actions by style rather than position: the
360
+ * primary-styled button is the confirm action and a non-primary button is
361
+ * cancel, since that is the pairing {@link toSlackBlocks} itself emits.
362
+ * Position is only a fallback for input that does not carry that pairing
363
+ * (zero or multiple primary-styled buttons among two or more actions).
364
+ */
365
+ const assignCardFooterActions = (
366
+ actions: unknown[],
367
+ warnings: SlackConversionWarning[],
368
+ ): {
369
+ readonly confirm: CardFooterButton | undefined;
370
+ readonly cancel: CardFooterButton | undefined;
371
+ readonly leftover: unknown[];
372
+ } => {
373
+ const isPrimaryButton = (action: unknown): boolean =>
374
+ isRecord(action) && action["style"] === "primary";
375
+ const primaryButtons = actions.filter(isPrimaryButton);
376
+
377
+ let confirmSource: unknown = undefined;
378
+ let cancelSource: unknown = undefined;
379
+ let leftover: unknown[] = [];
380
+
381
+ if (primaryButtons.length === 1) {
382
+ const [confirmButton] = primaryButtons;
383
+ const [cancelButton, ...rest] = actions.filter(
384
+ (action) => !isPrimaryButton(action),
385
+ );
386
+ confirmSource = confirmButton;
387
+ cancelSource = cancelButton;
388
+ leftover = rest;
389
+ } else if (primaryButtons.length === 0 && actions.length <= 1) {
390
+ const [onlyButton] = actions;
391
+ cancelSource = onlyButton;
392
+ } else if (actions.length > 0) {
393
+ const [first, second, ...rest] = actions;
394
+ confirmSource = first;
395
+ cancelSource = second;
396
+ leftover = rest;
397
+ warn(
398
+ warnings,
399
+ "fallback",
400
+ "Card",
401
+ "Confirm and cancel buttons could not be assigned by style and fell back to position.",
402
+ );
403
+ }
404
+
405
+ return {
406
+ confirm: cardFooterButtonFrom(confirmSource),
407
+ cancel: cardFooterButtonFrom(cancelSource),
408
+ leftover,
409
+ };
410
+ };
411
+
412
+ const cardFrom = (
413
+ block: Record<string, unknown>,
414
+ warnings: SlackConversionWarning[],
415
+ ): UIElement => {
416
+ const titleText = textOf(block["title"]);
417
+ const children: UIElement[] = [];
418
+ const heroImage = block["hero_image"];
419
+ if (isRecord(heroImage)) {
420
+ children.push({
421
+ $type: "Image",
422
+ src: asString(heroImage["image_url"]),
423
+ alt: asString(heroImage["alt_text"]),
424
+ });
425
+ }
426
+ const body = block["body"];
427
+ if (isRecord(body)) {
428
+ children.push({ $type: "Text", value: textOf(body) });
429
+ }
430
+ const subtext = block["subtext"];
431
+ if (isRecord(subtext)) {
432
+ children.push({ $type: "Caption", value: textOf(subtext) });
433
+ }
434
+ const actions = boundedArray(
435
+ block["actions"],
436
+ CARD_ACTIONS_CAP,
437
+ warnings,
438
+ "Card",
439
+ `actions were clamped to ${CARD_ACTIONS_CAP} entries.`,
440
+ );
441
+ const { confirm, cancel, leftover } = assignCardFooterActions(
442
+ actions,
443
+ warnings,
444
+ );
445
+ if (leftover.length > 0) {
446
+ for (const extra of leftover) {
447
+ if (isRecord(extra)) children.push(buttonFrom(extra));
448
+ }
449
+ warn(
450
+ warnings,
451
+ "fallback",
452
+ "Card",
453
+ "Card actions beyond confirm and cancel were appended as Button children.",
454
+ );
455
+ }
456
+ return {
457
+ $type: "Card",
458
+ ...(titleText ? { title: titleText } : {}),
459
+ ...(children.length > 0 ? { children } : {}),
460
+ ...(confirm !== undefined ? { confirm } : {}),
461
+ ...(cancel !== undefined ? { cancel } : {}),
462
+ };
463
+ };
464
+
465
+ const carouselFrom = (
466
+ block: Record<string, unknown>,
467
+ warnings: SlackConversionWarning[],
468
+ ): UIElement => {
469
+ const elements = boundedArray(
470
+ block["elements"],
471
+ CAROUSEL_CARD_CAP,
472
+ warnings,
473
+ "Carousel",
474
+ `cards were clamped to ${CAROUSEL_CARD_CAP} entries.`,
475
+ );
476
+ const cards = elements
477
+ .filter(isRecord)
478
+ .map((card) => cardFrom(card, warnings));
479
+ return { $type: "Carousel", children: cards };
480
+ };
481
+
482
+ const ALERT_LEVEL_TONES: Record<string, string> = {
483
+ error: "danger",
484
+ success: "success",
485
+ warning: "warning",
486
+ info: "info",
487
+ };
488
+
489
+ const alertFrom = (block: Record<string, unknown>): UIElement => {
490
+ const level = block["level"];
491
+ const tone = typeof level === "string" ? ALERT_LEVEL_TONES[level] : undefined;
492
+ return {
493
+ $type: "Alert",
494
+ description: textOf(block["text"]),
495
+ ...(tone !== undefined ? { tone } : {}),
496
+ };
497
+ };
498
+
499
+ const dataTableCellValue = (cell: unknown): string | number => {
500
+ if (!isRecord(cell)) return "";
501
+ const text = asString(cell["text"]);
502
+ if (cell["type"] === "raw_number") {
503
+ const parsed = Number(text);
504
+ if (Number.isFinite(parsed)) return parsed;
505
+ }
506
+ return text;
507
+ };
508
+
509
+ const dataTableFrom = (
510
+ block: Record<string, unknown>,
511
+ warnings: SlackConversionWarning[],
512
+ ): UIElement => {
513
+ const { items: rows, truncated: rowsTruncated } = clampArray(
514
+ block["rows"],
515
+ DATA_TABLE_ROW_CAP + 1,
516
+ );
517
+ if (rowsTruncated) {
518
+ warn(
519
+ warnings,
520
+ "clamped",
521
+ "Table",
522
+ `rows were clamped to ${DATA_TABLE_ROW_CAP} entries.`,
523
+ );
524
+ }
525
+ const [headerRow, ...dataRows] = rows;
526
+ const { items: headerCells, truncated: headerTruncated } = clampArray(
527
+ headerRow,
528
+ DATA_TABLE_COLUMN_CAP,
529
+ );
530
+ let columnsTruncated = headerTruncated;
531
+ const normalizedRows = dataRows.map((row) => {
532
+ const { items: cells, truncated } = clampArray(row, DATA_TABLE_COLUMN_CAP);
533
+ if (truncated) columnsTruncated = true;
534
+ return cells;
535
+ });
536
+ if (columnsTruncated) {
537
+ warn(
538
+ warnings,
539
+ "clamped",
540
+ "Table",
541
+ `columns were clamped to ${DATA_TABLE_COLUMN_CAP} entries.`,
542
+ );
543
+ }
544
+ const headerAllEmpty = headerCells.every(
545
+ (cell) => !isRecord(cell) || asString(cell["text"]) === "",
546
+ );
547
+ const columns = headerCells.map((cell) => ({
548
+ label: isRecord(cell) ? asString(cell["text"]) : "",
549
+ }));
550
+ return {
551
+ $type: "Table",
552
+ ...(headerAllEmpty ? {} : { columns }),
553
+ rows: normalizedRows.map((row) => row.map(dataTableCellValue)),
554
+ };
555
+ };
556
+
557
+ function convertBlock(
558
+ block: unknown,
559
+ warnings: SlackConversionWarning[],
560
+ ): UIElement[] {
561
+ if (isRecord(block)) {
562
+ switch (block["type"]) {
563
+ case "header":
564
+ return [headerFrom(block)];
565
+ case "section":
566
+ return sectionFrom(block, warnings);
567
+ case "context":
568
+ return [contextFrom(block, warnings)];
569
+ case "image":
570
+ return [imageFrom(block)];
571
+ case "divider":
572
+ return [{ $type: "Divider" }];
573
+ case "actions":
574
+ return actionsFrom(block, warnings);
575
+ case "input":
576
+ return inputFrom(block, warnings);
577
+ case "markdown":
578
+ return [markdownFrom(block)];
579
+ case "card":
580
+ return [cardFrom(block, warnings)];
581
+ case "carousel":
582
+ return [carouselFrom(block, warnings)];
583
+ case "alert":
584
+ return [alertFrom(block)];
585
+ case "data_table":
586
+ return [dataTableFrom(block, warnings)];
587
+ }
588
+ }
589
+ warn(
590
+ warnings,
591
+ "dropped",
592
+ isRecord(block) && typeof block["type"] === "string"
593
+ ? block["type"]
594
+ : "unknown",
595
+ "Unknown block type was dropped.",
596
+ );
597
+ return [];
598
+ }
599
+
600
+ const extractBlockList = (input: unknown): unknown[] | undefined => {
601
+ if (Array.isArray(input)) return input;
602
+ if (isRecord(input) && Array.isArray(input["blocks"])) return input["blocks"];
603
+ return undefined;
604
+ };
605
+
606
+ /**
607
+ * Walks the (already bounded) block list, folding a run of one or more
608
+ * consecutive `ListViewItem`-shaped section blocks, optionally separated by
609
+ * single divider blocks, into one `ListView` container instead of emitting
610
+ * root-level `ListViewItem` nodes or the interleaved dividers. A divider that
611
+ * is not directly between two such items is left for {@link convertBlock} to
612
+ * decode as a literal `Divider`.
613
+ */
614
+ function convertBlockList(
615
+ blocks: unknown[],
616
+ warnings: SlackConversionWarning[],
617
+ ): UIElement[] {
618
+ const nodes: UIElement[] = [];
619
+ let index = 0;
620
+ while (index < blocks.length) {
621
+ const firstItem = listViewItemFromSection(blocks[index]);
622
+ if (firstItem === undefined) {
623
+ nodes.push(...convertBlock(blocks[index], warnings));
624
+ index += 1;
625
+ continue;
626
+ }
627
+ const items: UIElement[] = [firstItem];
628
+ let cursor = index + 1;
629
+ while (true) {
630
+ if (isDividerBlock(blocks[cursor])) {
631
+ const candidate = listViewItemFromSection(blocks[cursor + 1]);
632
+ if (candidate === undefined) break;
633
+ items.push(candidate);
634
+ cursor += 2;
635
+ continue;
636
+ }
637
+ const candidate = listViewItemFromSection(blocks[cursor]);
638
+ if (candidate === undefined) break;
639
+ items.push(candidate);
640
+ cursor += 1;
641
+ }
642
+ nodes.push({ $type: "ListView", children: items });
643
+ index = cursor;
644
+ }
645
+ return nodes;
646
+ }
647
+
648
+ /**
649
+ * Converts Slack Block Kit JSON back into a generative-ui IR tree, inverting
650
+ * {@link toSlackBlocks}. Accepts a blocks array or a `{ blocks }` wrapper;
651
+ * malformed input resolves to an empty node list plus a warning, and an
652
+ * unrecognized block or action element is skipped with a "dropped" warning.
653
+ * Every array read along the way (blocks, fields, options, elements, cards,
654
+ * rows, cells) is bounded to its site's cap before it is mapped over, so a
655
+ * hostile array (sparse or proxied with a fabricated `length`) is clamped
656
+ * with a "clamped" warning instead of stalling the event loop. Never throws.
657
+ *
658
+ * The Slack shape is lossier than the IR, so some information cannot be
659
+ * recovered: a context block's elements always decode as `Caption` (the
660
+ * original `Badge` vs `Caption` distinction does not survive the round trip),
661
+ * a button's `buttonStyle` is only recovered for the `primary`/`danger` Slack
662
+ * styles, a native alert block's `title` and `description` merge into a
663
+ * single `description` string, and a card's layout collapses to its `title`,
664
+ * a flat `children` list (image, then text, then caption, in that order),
665
+ * and `confirm`/`cancel` footer buttons. A section field's label and value
666
+ * are split on the first `*`+newline delimiter, so a label or value that
667
+ * itself contains that exact delimiter cannot be split back unambiguously.
668
+ * A data-table cell that Slack carries as `raw_text` always decodes as a
669
+ * string, even when its text is the stringified form of a boolean, since the
670
+ * Slack shape does not distinguish a boolean from ordinary text.
671
+ */
672
+ export function fromSlackBlocks(blocks: unknown): FromSlackBlocksResult {
673
+ const warnings: SlackConversionWarning[] = [];
674
+ try {
675
+ const list = extractBlockList(blocks);
676
+ if (list === undefined) {
677
+ return {
678
+ nodes: [],
679
+ warnings: [
680
+ {
681
+ code: "dropped",
682
+ component: "Root",
683
+ detail: "Input could not be converted.",
684
+ },
685
+ ],
686
+ };
687
+ }
688
+ const boundedList = boundedArray(
689
+ list,
690
+ INBOUND_BLOCK_CAP,
691
+ warnings,
692
+ "Root",
693
+ `blocks were clamped to ${INBOUND_BLOCK_CAP} entries.`,
694
+ );
695
+ const nodes = convertBlockList(boundedList, warnings);
696
+ return { nodes, warnings };
697
+ } catch {
698
+ return {
699
+ nodes: [],
700
+ warnings: [
701
+ {
702
+ code: "dropped",
703
+ component: "Root",
704
+ detail: "Input could not be converted.",
705
+ },
706
+ ],
707
+ };
708
+ }
709
+ }