@tai42/studio-sdk 0.2.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 (422) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +15 -0
  3. package/README.md +318 -0
  4. package/SECURITY.md +51 -0
  5. package/dist/components/badge.d.ts +13 -0
  6. package/dist/components/badge.d.ts.map +1 -0
  7. package/dist/components/badge.js +14 -0
  8. package/dist/components/badge.js.map +1 -0
  9. package/dist/components/checkbox.d.ts +24 -0
  10. package/dist/components/checkbox.d.ts.map +1 -0
  11. package/dist/components/checkbox.js +31 -0
  12. package/dist/components/checkbox.js.map +1 -0
  13. package/dist/components/code-block.d.ts +7 -0
  14. package/dist/components/code-block.d.ts.map +1 -0
  15. package/dist/components/code-block.js +23 -0
  16. package/dist/components/code-block.js.map +1 -0
  17. package/dist/components/completion-input.d.ts +31 -0
  18. package/dist/components/completion-input.d.ts.map +1 -0
  19. package/dist/components/completion-input.js +61 -0
  20. package/dist/components/completion-input.js.map +1 -0
  21. package/dist/components/components.css +2212 -0
  22. package/dist/components/confirm-dialog.d.ts +40 -0
  23. package/dist/components/confirm-dialog.d.ts.map +1 -0
  24. package/dist/components/confirm-dialog.js +11 -0
  25. package/dist/components/confirm-dialog.js.map +1 -0
  26. package/dist/components/control-styles.d.ts +12 -0
  27. package/dist/components/control-styles.d.ts.map +1 -0
  28. package/dist/components/control-styles.js +14 -0
  29. package/dist/components/control-styles.js.map +1 -0
  30. package/dist/components/copy-field.d.ts +8 -0
  31. package/dist/components/copy-field.d.ts.map +1 -0
  32. package/dist/components/copy-field.js +106 -0
  33. package/dist/components/copy-field.js.map +1 -0
  34. package/dist/components/dialog.d.ts +21 -0
  35. package/dist/components/dialog.d.ts.map +1 -0
  36. package/dist/components/dialog.js +25 -0
  37. package/dist/components/dialog.js.map +1 -0
  38. package/dist/components/drawer.d.ts +22 -0
  39. package/dist/components/drawer.d.ts.map +1 -0
  40. package/dist/components/drawer.js +32 -0
  41. package/dist/components/drawer.js.map +1 -0
  42. package/dist/components/error-boundary.d.ts +33 -0
  43. package/dist/components/error-boundary.d.ts.map +1 -0
  44. package/dist/components/error-boundary.js +36 -0
  45. package/dist/components/error-boundary.js.map +1 -0
  46. package/dist/components/extension-combo-builder.d.ts +70 -0
  47. package/dist/components/extension-combo-builder.d.ts.map +1 -0
  48. package/dist/components/extension-combo-builder.js +154 -0
  49. package/dist/components/extension-combo-builder.js.map +1 -0
  50. package/dist/components/extension-grouping.d.ts +65 -0
  51. package/dist/components/extension-grouping.d.ts.map +1 -0
  52. package/dist/components/extension-grouping.js +78 -0
  53. package/dist/components/extension-grouping.js.map +1 -0
  54. package/dist/components/extension-picker.d.ts +35 -0
  55. package/dist/components/extension-picker.d.ts.map +1 -0
  56. package/dist/components/extension-picker.js +31 -0
  57. package/dist/components/extension-picker.js.map +1 -0
  58. package/dist/components/external-link.d.ts +19 -0
  59. package/dist/components/external-link.d.ts.map +1 -0
  60. package/dist/components/external-link.js +12 -0
  61. package/dist/components/external-link.js.map +1 -0
  62. package/dist/components/field.d.ts +64 -0
  63. package/dist/components/field.d.ts.map +1 -0
  64. package/dist/components/field.js +56 -0
  65. package/dist/components/field.js.map +1 -0
  66. package/dist/components/fleet-report.d.ts +39 -0
  67. package/dist/components/fleet-report.d.ts.map +1 -0
  68. package/dist/components/fleet-report.js +50 -0
  69. package/dist/components/fleet-report.js.map +1 -0
  70. package/dist/components/fonts.css +22 -0
  71. package/dist/components/icons.d.ts +142 -0
  72. package/dist/components/icons.d.ts.map +1 -0
  73. package/dist/components/icons.js +139 -0
  74. package/dist/components/icons.js.map +1 -0
  75. package/dist/components/inputs.d.ts +52 -0
  76. package/dist/components/inputs.d.ts.map +1 -0
  77. package/dist/components/inputs.js +35 -0
  78. package/dist/components/inputs.js.map +1 -0
  79. package/dist/components/json-diff.d.ts +40 -0
  80. package/dist/components/json-diff.d.ts.map +1 -0
  81. package/dist/components/json-diff.js +119 -0
  82. package/dist/components/json-diff.js.map +1 -0
  83. package/dist/components/json-tree.d.ts +8 -0
  84. package/dist/components/json-tree.d.ts.map +1 -0
  85. package/dist/components/json-tree.js +70 -0
  86. package/dist/components/json-tree.js.map +1 -0
  87. package/dist/components/modal-focus.d.ts +15 -0
  88. package/dist/components/modal-focus.d.ts.map +1 -0
  89. package/dist/components/modal-focus.js +136 -0
  90. package/dist/components/modal-focus.js.map +1 -0
  91. package/dist/components/page-header.d.ts +56 -0
  92. package/dist/components/page-header.d.ts.map +1 -0
  93. package/dist/components/page-header.js +24 -0
  94. package/dist/components/page-header.js.map +1 -0
  95. package/dist/components/primitives.d.ts +186 -0
  96. package/dist/components/primitives.d.ts.map +1 -0
  97. package/dist/components/primitives.js +204 -0
  98. package/dist/components/primitives.js.map +1 -0
  99. package/dist/components/progress-bar.d.ts +29 -0
  100. package/dist/components/progress-bar.d.ts.map +1 -0
  101. package/dist/components/progress-bar.js +48 -0
  102. package/dist/components/progress-bar.js.map +1 -0
  103. package/dist/components/radio-group.d.ts +37 -0
  104. package/dist/components/radio-group.d.ts.map +1 -0
  105. package/dist/components/radio-group.js +56 -0
  106. package/dist/components/radio-group.js.map +1 -0
  107. package/dist/components/reveal-input.d.ts +13 -0
  108. package/dist/components/reveal-input.d.ts.map +1 -0
  109. package/dist/components/reveal-input.js +31 -0
  110. package/dist/components/reveal-input.js.map +1 -0
  111. package/dist/components/scroll-region.d.ts +85 -0
  112. package/dist/components/scroll-region.d.ts.map +1 -0
  113. package/dist/components/scroll-region.js +574 -0
  114. package/dist/components/scroll-region.js.map +1 -0
  115. package/dist/components/select.d.ts +53 -0
  116. package/dist/components/select.d.ts.map +1 -0
  117. package/dist/components/select.js +33 -0
  118. package/dist/components/select.js.map +1 -0
  119. package/dist/components/table.d.ts +59 -0
  120. package/dist/components/table.d.ts.map +1 -0
  121. package/dist/components/table.js +20 -0
  122. package/dist/components/table.js.map +1 -0
  123. package/dist/components/tabs.d.ts +15 -0
  124. package/dist/components/tabs.d.ts.map +1 -0
  125. package/dist/components/tabs.js +13 -0
  126. package/dist/components/tabs.js.map +1 -0
  127. package/dist/components/tags.d.ts +46 -0
  128. package/dist/components/tags.d.ts.map +1 -0
  129. package/dist/components/tags.js +68 -0
  130. package/dist/components/tags.js.map +1 -0
  131. package/dist/components/tokens.css +392 -0
  132. package/dist/components/tokens.d.ts +15 -0
  133. package/dist/components/tokens.d.ts.map +1 -0
  134. package/dist/components/tokens.js +101 -0
  135. package/dist/components/tokens.js.map +1 -0
  136. package/dist/components/tool-picker.d.ts +24 -0
  137. package/dist/components/tool-picker.d.ts.map +1 -0
  138. package/dist/components/tool-picker.js +97 -0
  139. package/dist/components/tool-picker.js.map +1 -0
  140. package/dist/components/tooltip.d.ts +17 -0
  141. package/dist/components/tooltip.d.ts.map +1 -0
  142. package/dist/components/tooltip.js +18 -0
  143. package/dist/components/tooltip.js.map +1 -0
  144. package/dist/components/version-history-panel.d.ts +71 -0
  145. package/dist/components/version-history-panel.d.ts.map +1 -0
  146. package/dist/components/version-history-panel.js +148 -0
  147. package/dist/components/version-history-panel.js.map +1 -0
  148. package/dist/download.d.ts +7 -0
  149. package/dist/download.d.ts.map +1 -0
  150. package/dist/download.js +18 -0
  151. package/dist/download.js.map +1 -0
  152. package/dist/element-slot.d.ts +4 -0
  153. package/dist/element-slot.d.ts.map +1 -0
  154. package/dist/element-slot.js +20 -0
  155. package/dist/element-slot.js.map +1 -0
  156. package/dist/elicitation/ElicitationForm.d.ts +34 -0
  157. package/dist/elicitation/ElicitationForm.d.ts.map +1 -0
  158. package/dist/elicitation/ElicitationForm.js +24 -0
  159. package/dist/elicitation/ElicitationForm.js.map +1 -0
  160. package/dist/errors.d.ts +9 -0
  161. package/dist/errors.d.ts.map +1 -0
  162. package/dist/errors.js +15 -0
  163. package/dist/errors.js.map +1 -0
  164. package/dist/extension-combos.d.ts +13 -0
  165. package/dist/extension-combos.d.ts.map +1 -0
  166. package/dist/extension-combos.js +9 -0
  167. package/dist/extension-combos.js.map +1 -0
  168. package/dist/hooks/useApi.d.ts +1222 -0
  169. package/dist/hooks/useApi.d.ts.map +1 -0
  170. package/dist/hooks/useApi.js +22 -0
  171. package/dist/hooks/useApi.js.map +1 -0
  172. package/dist/hooks/useAuth.d.ts +13 -0
  173. package/dist/hooks/useAuth.d.ts.map +1 -0
  174. package/dist/hooks/useAuth.js +53 -0
  175. package/dist/hooks/useAuth.js.map +1 -0
  176. package/dist/hooks/useBreakpoint.d.ts +10 -0
  177. package/dist/hooks/useBreakpoint.d.ts.map +1 -0
  178. package/dist/hooks/useBreakpoint.js +66 -0
  179. package/dist/hooks/useBreakpoint.js.map +1 -0
  180. package/dist/hooks/useCapabilities.d.ts +88 -0
  181. package/dist/hooks/useCapabilities.d.ts.map +1 -0
  182. package/dist/hooks/useCapabilities.js +203 -0
  183. package/dist/hooks/useCapabilities.js.map +1 -0
  184. package/dist/hooks/useSse.d.ts +13 -0
  185. package/dist/hooks/useSse.d.ts.map +1 -0
  186. package/dist/hooks/useSse.js +298 -0
  187. package/dist/hooks/useSse.js.map +1 -0
  188. package/dist/hooks/useTheme.d.ts +20 -0
  189. package/dist/hooks/useTheme.d.ts.map +1 -0
  190. package/dist/hooks/useTheme.js +97 -0
  191. package/dist/hooks/useTheme.js.map +1 -0
  192. package/dist/hooks/useUnauthorized.d.ts +3 -0
  193. package/dist/hooks/useUnauthorized.d.ts.map +1 -0
  194. package/dist/hooks/useUnauthorized.js +18 -0
  195. package/dist/hooks/useUnauthorized.js.map +1 -0
  196. package/dist/host.d.ts +11 -0
  197. package/dist/host.d.ts.map +1 -0
  198. package/dist/host.js +10 -0
  199. package/dist/host.js.map +1 -0
  200. package/dist/index.d.ts +118 -0
  201. package/dist/index.d.ts.map +1 -0
  202. package/dist/index.js +91 -0
  203. package/dist/index.js.map +1 -0
  204. package/dist/navigation/context.d.ts +25 -0
  205. package/dist/navigation/context.d.ts.map +1 -0
  206. package/dist/navigation/context.js +45 -0
  207. package/dist/navigation/context.js.map +1 -0
  208. package/dist/navigation/index.d.ts +4 -0
  209. package/dist/navigation/index.d.ts.map +1 -0
  210. package/dist/navigation/index.js +2 -0
  211. package/dist/navigation/index.js.map +1 -0
  212. package/dist/navigation/types.d.ts +97 -0
  213. package/dist/navigation/types.d.ts.map +1 -0
  214. package/dist/navigation/types.js +16 -0
  215. package/dist/navigation/types.js.map +1 -0
  216. package/dist/plugin/host-state.d.ts +45 -0
  217. package/dist/plugin/host-state.d.ts.map +1 -0
  218. package/dist/plugin/host-state.js +62 -0
  219. package/dist/plugin/host-state.js.map +1 -0
  220. package/dist/plugin/registry.d.ts +35 -0
  221. package/dist/plugin/registry.d.ts.map +1 -0
  222. package/dist/plugin/registry.js +129 -0
  223. package/dist/plugin/registry.js.map +1 -0
  224. package/dist/plugin/types.d.ts +165 -0
  225. package/dist/plugin/types.d.ts.map +1 -0
  226. package/dist/plugin/types.js +2 -0
  227. package/dist/plugin/types.js.map +1 -0
  228. package/dist/plugin/version.d.ts +34 -0
  229. package/dist/plugin/version.d.ts.map +1 -0
  230. package/dist/plugin/version.js +37 -0
  231. package/dist/plugin/version.js.map +1 -0
  232. package/dist/query-keys.d.ts +31 -0
  233. package/dist/query-keys.d.ts.map +1 -0
  234. package/dist/query-keys.js +31 -0
  235. package/dist/query-keys.js.map +1 -0
  236. package/dist/schema-editor/SchemaEditor.d.ts +43 -0
  237. package/dist/schema-editor/SchemaEditor.d.ts.map +1 -0
  238. package/dist/schema-editor/SchemaEditor.js +106 -0
  239. package/dist/schema-editor/SchemaEditor.js.map +1 -0
  240. package/dist/schema-editor/index.d.ts +10 -0
  241. package/dist/schema-editor/index.d.ts.map +1 -0
  242. package/dist/schema-editor/index.js +8 -0
  243. package/dist/schema-editor/index.js.map +1 -0
  244. package/dist/schema-editor/lint.d.ts +37 -0
  245. package/dist/schema-editor/lint.d.ts.map +1 -0
  246. package/dist/schema-editor/lint.js +49 -0
  247. package/dist/schema-editor/lint.js.map +1 -0
  248. package/dist/schema-form/SchemaForm.d.ts +70 -0
  249. package/dist/schema-form/SchemaForm.d.ts.map +1 -0
  250. package/dist/schema-form/SchemaForm.js +13 -0
  251. package/dist/schema-form/SchemaForm.js.map +1 -0
  252. package/dist/schema-form/array-field.d.ts +20 -0
  253. package/dist/schema-form/array-field.d.ts.map +1 -0
  254. package/dist/schema-form/array-field.js +39 -0
  255. package/dist/schema-form/array-field.js.map +1 -0
  256. package/dist/schema-form/classify.d.ts +87 -0
  257. package/dist/schema-form/classify.d.ts.map +1 -0
  258. package/dist/schema-form/classify.js +199 -0
  259. package/dist/schema-form/classify.js.map +1 -0
  260. package/dist/schema-form/context.d.ts +4 -0
  261. package/dist/schema-form/context.d.ts.map +1 -0
  262. package/dist/schema-form/context.js +14 -0
  263. package/dist/schema-form/context.js.map +1 -0
  264. package/dist/schema-form/default-value.d.ts +8 -0
  265. package/dist/schema-form/default-value.d.ts.map +1 -0
  266. package/dist/schema-form/default-value.js +64 -0
  267. package/dist/schema-form/default-value.js.map +1 -0
  268. package/dist/schema-form/enum-field.d.ts +19 -0
  269. package/dist/schema-form/enum-field.d.ts.map +1 -0
  270. package/dist/schema-form/enum-field.js +26 -0
  271. package/dist/schema-form/enum-field.js.map +1 -0
  272. package/dist/schema-form/field-group.d.ts +9 -0
  273. package/dist/schema-form/field-group.d.ts.map +1 -0
  274. package/dist/schema-form/field-group.js +32 -0
  275. package/dist/schema-form/field-group.js.map +1 -0
  276. package/dist/schema-form/field-node.d.ts +24 -0
  277. package/dist/schema-form/field-node.d.ts.map +1 -0
  278. package/dist/schema-form/field-node.js +51 -0
  279. package/dist/schema-form/field-node.js.map +1 -0
  280. package/dist/schema-form/index.d.ts +13 -0
  281. package/dist/schema-form/index.d.ts.map +1 -0
  282. package/dist/schema-form/index.js +11 -0
  283. package/dist/schema-form/index.js.map +1 -0
  284. package/dist/schema-form/media-field.d.ts +24 -0
  285. package/dist/schema-form/media-field.d.ts.map +1 -0
  286. package/dist/schema-form/media-field.js +169 -0
  287. package/dist/schema-form/media-field.js.map +1 -0
  288. package/dist/schema-form/media.d.ts +40 -0
  289. package/dist/schema-form/media.d.ts.map +1 -0
  290. package/dist/schema-form/media.js +65 -0
  291. package/dist/schema-form/media.js.map +1 -0
  292. package/dist/schema-form/object-fields.d.ts +20 -0
  293. package/dist/schema-form/object-fields.d.ts.map +1 -0
  294. package/dist/schema-form/object-fields.js +24 -0
  295. package/dist/schema-form/object-fields.js.map +1 -0
  296. package/dist/schema-form/resolve.d.ts +41 -0
  297. package/dist/schema-form/resolve.d.ts.map +1 -0
  298. package/dist/schema-form/resolve.js +102 -0
  299. package/dist/schema-form/resolve.js.map +1 -0
  300. package/dist/schema-form/string-field.d.ts +20 -0
  301. package/dist/schema-form/string-field.d.ts.map +1 -0
  302. package/dist/schema-form/string-field.js +68 -0
  303. package/dist/schema-form/string-field.js.map +1 -0
  304. package/dist/schema-form/styles.d.ts +18 -0
  305. package/dist/schema-form/styles.d.ts.map +1 -0
  306. package/dist/schema-form/styles.js +18 -0
  307. package/dist/schema-form/styles.js.map +1 -0
  308. package/dist/schema-form/types.d.ts +67 -0
  309. package/dist/schema-form/types.d.ts.map +1 -0
  310. package/dist/schema-form/types.js +10 -0
  311. package/dist/schema-form/types.js.map +1 -0
  312. package/dist/schema-form/union-field.d.ts +24 -0
  313. package/dist/schema-form/union-field.d.ts.map +1 -0
  314. package/dist/schema-form/union-field.js +55 -0
  315. package/dist/schema-form/union-field.js.map +1 -0
  316. package/dist/schema-form/union.d.ts +4 -0
  317. package/dist/schema-form/union.d.ts.map +1 -0
  318. package/dist/schema-form/union.js +68 -0
  319. package/dist/schema-form/union.js.map +1 -0
  320. package/dist/schema-form/unsupported-notice.d.ts +18 -0
  321. package/dist/schema-form/unsupported-notice.d.ts.map +1 -0
  322. package/dist/schema-form/unsupported-notice.js +8 -0
  323. package/dist/schema-form/unsupported-notice.js.map +1 -0
  324. package/dist/schema-form/validate.d.ts +18 -0
  325. package/dist/schema-form/validate.d.ts.map +1 -0
  326. package/dist/schema-form/validate.js +162 -0
  327. package/dist/schema-form/validate.js.map +1 -0
  328. package/dist/structured-output/StructuredOutput.d.ts +27 -0
  329. package/dist/structured-output/StructuredOutput.d.ts.map +1 -0
  330. package/dist/structured-output/StructuredOutput.js +41 -0
  331. package/dist/structured-output/StructuredOutput.js.map +1 -0
  332. package/dist/testing.d.ts +80 -0
  333. package/dist/testing.d.ts.map +1 -0
  334. package/dist/testing.js +207 -0
  335. package/dist/testing.js.map +1 -0
  336. package/package.json +101 -0
  337. package/src/components/badge.tsx +31 -0
  338. package/src/components/checkbox.tsx +93 -0
  339. package/src/components/code-block.tsx +37 -0
  340. package/src/components/completion-input.tsx +137 -0
  341. package/src/components/components.css +2212 -0
  342. package/src/components/confirm-dialog.tsx +77 -0
  343. package/src/components/control-styles.ts +14 -0
  344. package/src/components/copy-field.tsx +178 -0
  345. package/src/components/dialog.tsx +76 -0
  346. package/src/components/drawer.tsx +83 -0
  347. package/src/components/error-boundary.tsx +56 -0
  348. package/src/components/extension-combo-builder.tsx +305 -0
  349. package/src/components/extension-grouping.ts +116 -0
  350. package/src/components/extension-picker.tsx +97 -0
  351. package/src/components/external-link.tsx +40 -0
  352. package/src/components/field.tsx +169 -0
  353. package/src/components/fleet-report.tsx +136 -0
  354. package/src/components/fonts.css +22 -0
  355. package/src/components/icons.tsx +504 -0
  356. package/src/components/inputs.tsx +125 -0
  357. package/src/components/json-diff.tsx +192 -0
  358. package/src/components/json-tree.tsx +129 -0
  359. package/src/components/modal-focus.ts +150 -0
  360. package/src/components/page-header.tsx +107 -0
  361. package/src/components/primitives.tsx +489 -0
  362. package/src/components/progress-bar.tsx +100 -0
  363. package/src/components/radio-group.tsx +160 -0
  364. package/src/components/reveal-input.tsx +87 -0
  365. package/src/components/scroll-region.tsx +703 -0
  366. package/src/components/select.tsx +149 -0
  367. package/src/components/table.tsx +94 -0
  368. package/src/components/tabs.tsx +51 -0
  369. package/src/components/tags.tsx +137 -0
  370. package/src/components/tokens.css +392 -0
  371. package/src/components/tokens.ts +102 -0
  372. package/src/components/tool-picker.tsx +175 -0
  373. package/src/components/tooltip.tsx +46 -0
  374. package/src/components/version-history-panel.tsx +407 -0
  375. package/src/css.d.ts +2 -0
  376. package/src/download.ts +17 -0
  377. package/src/element-slot.ts +22 -0
  378. package/src/elicitation/ElicitationForm.tsx +87 -0
  379. package/src/errors.ts +12 -0
  380. package/src/extension-combos.ts +18 -0
  381. package/src/hooks/useApi.ts +27 -0
  382. package/src/hooks/useAuth.ts +67 -0
  383. package/src/hooks/useBreakpoint.ts +78 -0
  384. package/src/hooks/useCapabilities.ts +242 -0
  385. package/src/hooks/useSse.ts +314 -0
  386. package/src/hooks/useTheme.ts +133 -0
  387. package/src/hooks/useUnauthorized.ts +20 -0
  388. package/src/host.ts +15 -0
  389. package/src/index.ts +267 -0
  390. package/src/navigation/context.tsx +90 -0
  391. package/src/navigation/index.ts +9 -0
  392. package/src/navigation/types.ts +90 -0
  393. package/src/plugin/host-state.ts +93 -0
  394. package/src/plugin/registry.ts +174 -0
  395. package/src/plugin/types.ts +178 -0
  396. package/src/plugin/version.ts +44 -0
  397. package/src/query-keys.ts +34 -0
  398. package/src/schema-editor/SchemaEditor.tsx +186 -0
  399. package/src/schema-editor/index.ts +9 -0
  400. package/src/schema-editor/lint.ts +70 -0
  401. package/src/schema-form/SchemaForm.tsx +107 -0
  402. package/src/schema-form/array-field.tsx +110 -0
  403. package/src/schema-form/classify.ts +302 -0
  404. package/src/schema-form/context.ts +17 -0
  405. package/src/schema-form/default-value.ts +65 -0
  406. package/src/schema-form/enum-field.tsx +65 -0
  407. package/src/schema-form/field-group.tsx +71 -0
  408. package/src/schema-form/field-node.tsx +161 -0
  409. package/src/schema-form/index.ts +12 -0
  410. package/src/schema-form/media-field.tsx +281 -0
  411. package/src/schema-form/media.ts +69 -0
  412. package/src/schema-form/object-fields.tsx +74 -0
  413. package/src/schema-form/resolve.ts +114 -0
  414. package/src/schema-form/string-field.tsx +161 -0
  415. package/src/schema-form/styles.ts +20 -0
  416. package/src/schema-form/types.ts +71 -0
  417. package/src/schema-form/union-field.tsx +122 -0
  418. package/src/schema-form/union.ts +78 -0
  419. package/src/schema-form/unsupported-notice.tsx +39 -0
  420. package/src/schema-form/validate.ts +183 -0
  421. package/src/structured-output/StructuredOutput.tsx +81 -0
  422. package/src/testing.ts +235 -0
@@ -0,0 +1,703 @@
1
+ /**
2
+ * `ScrollRegion` — a horizontally scrollable container that becomes a keyboard
3
+ * target ONLY while it actually overflows.
4
+ *
5
+ * A pane that scrolls must be reachable without a pointer (WCAG 2.1.1), which
6
+ * needs `tabIndex`, `role="region"` and a name. Applying those unconditionally
7
+ * would litter every screen with phantom landmarks and dead tab stops for tables
8
+ * that happen to fit, so the attributes are driven by a live measurement:
9
+ * `scrollWidth > clientWidth`, re-taken whenever the container or its content
10
+ * resizes and whenever the content is replaced.
11
+ *
12
+ * `useOverflowRegion` is the same measurement for a component that IS its own
13
+ * scrolling box (a `<pre>`, a JSON pane) and therefore cannot be wrapped in a
14
+ * `<div>` without gaining a second scroller. `useProseScrollRegions` applies the
15
+ * rules to the surfaces React never renders — the tables and code blocks inside
16
+ * `dangerouslySetInnerHTML` (rendered README/markdown), which cannot be wrapped
17
+ * in a component and are therefore instrumented imperatively.
18
+ *
19
+ * Both hooks hand back a CALLBACK REF for the element they measure, rather than
20
+ * reading one the caller holds: a ref object tells a hook nothing about WHEN it
21
+ * is filled, so a hook that read one would never instrument an element that
22
+ * mounts later than the hook (a README that arrives with the second render) and
23
+ * would never notice one being swapped for another.
24
+ */
25
+ import { useCallback, useState } from 'react';
26
+ import type { CSSProperties, ReactNode, Ref, RefCallback } from 'react';
27
+
28
+ export interface ScrollRegionProps {
29
+ /** The region's accessible name, applied only while it actually scrolls. */
30
+ readonly label: string;
31
+ readonly children: ReactNode;
32
+ readonly className?: string;
33
+ readonly style?: CSSProperties;
34
+ /**
35
+ * The one arbitrary attribute this component forwards. The measured attributes
36
+ * — `tabindex`, `role` and `aria-label` — are the component's own and appear
37
+ * and disappear with the overflow, so a general attribute spread would let a
38
+ * caller set them statically and defeat the measurement. A test hook cannot,
39
+ * which is why it is the exception rather than the first of a set.
40
+ */
41
+ readonly 'data-testid'?: string;
42
+ /** A consumer ref for the scrolling `div` itself. */
43
+ readonly ref?: Ref<HTMLDivElement>;
44
+ }
45
+
46
+ /**
47
+ * The names for a prose surface that has no heading above it.
48
+ *
49
+ * They say what the surface IS and nothing about which document it came from,
50
+ * because this hook cannot know: it instruments whatever markup the caller
51
+ * injected. A default that named one document put that document's name on every
52
+ * caller's regions, and a region a reader hears the wrong name for is worse than
53
+ * a plain one. A caller that CAN say which document it is showing passes its own
54
+ * `labels`; a surface with a heading above it takes that heading instead, which
55
+ * is the usual case.
56
+ */
57
+ const DEFAULT_PROSE_LABELS = { table: 'Table', pre: 'Code block' } as const;
58
+
59
+ /** The fallback name per instrumented surface, when no heading precedes it. */
60
+ export interface ProseScrollLabels {
61
+ readonly table?: string;
62
+ readonly pre?: string;
63
+ }
64
+
65
+ const HEADING_SELECTOR = 'h1, h2, h3, h4, h5, h6';
66
+
67
+ /**
68
+ * Headings AND the surfaces they name, in one selector: a single query returns
69
+ * both interleaved in document order, which is what lets one walk name every
70
+ * surface (see `labelledProseSurfaces`).
71
+ */
72
+ const PROSE_SURFACE_SELECTOR = `${HEADING_SELECTOR}, table, pre`;
73
+
74
+ const SCROLL_REGION_CLASS = 'tai-scroll-region';
75
+
76
+ /**
77
+ * The most surfaces one rendered document is given regions for.
78
+ *
79
+ * The prose is publisher-authored and unbounded in length, and every region is
80
+ * an entry in the landmark list a screen-reader user navigates the page by: a
81
+ * README with more surfaces than this turns that list into the very thing the
82
+ * regions exist to spare a reader. Past the cap a table still gets its scrolling
83
+ * wrapper — the wrapper is what keeps it inside the column — but no name and no
84
+ * tab stop, so the landmark list stays a list a person can read.
85
+ */
86
+ const MAX_PROSE_REGIONS = 200;
87
+
88
+ function overflows(element: HTMLElement): boolean {
89
+ return element.scrollWidth > element.clientWidth;
90
+ }
91
+
92
+ /**
93
+ * Whether the element should carry the region attributes right now.
94
+ *
95
+ * A region that stops overflowing keeps its tab stop for as long as it holds
96
+ * focus: taking `tabindex` off the focused element drops the reader onto the
97
+ * document body, and a window resize is not their doing. Only an element that
98
+ * already has the stop can be the active element, so this can hold a stop open
99
+ * but never invent one.
100
+ */
101
+ function needsRegion(element: HTMLElement): boolean {
102
+ return overflows(element) || element.ownerDocument.activeElement === element;
103
+ }
104
+
105
+ /**
106
+ * Attaches `element` to a consumer ref of either form, and returns the detach
107
+ * for it. A callback ref that answers with its own cleanup gets that cleanup
108
+ * called rather than a second call with `null` — the contract React itself
109
+ * follows for a ref it owns.
110
+ */
111
+ function attachRef(ref: Ref<HTMLElement> | undefined, element: HTMLElement): () => void {
112
+ if (ref === undefined || ref === null) return () => undefined;
113
+ if (typeof ref !== 'function') {
114
+ ref.current = element;
115
+ return () => {
116
+ ref.current = null;
117
+ };
118
+ }
119
+ const cleanup = ref(element);
120
+ if (typeof cleanup === 'function') return cleanup;
121
+ return () => {
122
+ ref(null);
123
+ };
124
+ }
125
+
126
+ /**
127
+ * ONE `ResizeObserver` and ONE `MutationObserver` for every scrolling box in the
128
+ * document, rather than a pair per mount.
129
+ *
130
+ * `useOverflowRegion` is per-INSTANCE — a timeline renders two `CodeBlock`s per
131
+ * tool call and windows none of them — so an observer per mount grows with the
132
+ * content, and each one is a separate callback the browser schedules and a
133
+ * separate registration it maintains. One observer of each kind costs one of
134
+ * each however many boxes are on screen, and each callback still only measures
135
+ * the boxes whose records it was handed. (`useProseScrollRegions` keeps its own
136
+ * pair: there is one of it per rendered document, not one per surface, and its
137
+ * callbacks drive DOM writes rather than a React state setter.)
138
+ *
139
+ * Created on first use, because the module is imported in environments that have
140
+ * no `ResizeObserver` at all until a test installs one.
141
+ */
142
+ type ResizeHandler = () => void;
143
+
144
+ /**
145
+ * The handlers waiting on each observed element. A SET, not a single handler:
146
+ * one element can be both a box and another box's child — a `<pre>` inside a
147
+ * `ScrollRegion` is exactly that — and both owners must still hear its resize.
148
+ */
149
+ const resizeHandlers = new WeakMap<Element, Set<ResizeHandler>>();
150
+ let sharedResize: ResizeObserver | undefined;
151
+
152
+ function sharedResizeObserver(): ResizeObserver {
153
+ sharedResize ??= new ResizeObserver((entries) => {
154
+ // A box and its children are observed separately, so one frame can deliver
155
+ // several entries owned by the same handler; measuring once is enough.
156
+ const fired = new Set<ResizeHandler>();
157
+ for (const entry of entries) {
158
+ for (const handler of resizeHandlers.get(entry.target) ?? []) fired.add(handler);
159
+ }
160
+ for (const handler of fired) handler();
161
+ });
162
+ return sharedResize;
163
+ }
164
+
165
+ /**
166
+ * Registers `handler` for `target`'s resizes, observing the element only the
167
+ * first time anyone asks for it: re-observing an element already under a
168
+ * `ResizeObserver` re-arms its initial notification, which spends a forced
169
+ * layout on a measurement nobody asked for.
170
+ */
171
+ function holdResizeTarget(target: Element, handler: ResizeHandler): void {
172
+ const handlers = resizeHandlers.get(target);
173
+ if (handlers !== undefined) {
174
+ handlers.add(handler);
175
+ return;
176
+ }
177
+ // Registered before the element is observed, so the initial notification the
178
+ // observation arms reaches the handler that asked for it.
179
+ resizeHandlers.set(target, new Set<ResizeHandler>([handler]));
180
+ sharedResizeObserver().observe(target);
181
+ }
182
+
183
+ /** Stops observing `target` for `handler`, and entirely once nobody wants it. */
184
+ function releaseResizeTarget(target: Element, handler: ResizeHandler): void {
185
+ const handlers = resizeHandlers.get(target);
186
+ if (handlers === undefined) return;
187
+ handlers.delete(handler);
188
+ if (handlers.size > 0) return;
189
+ resizeHandlers.delete(target);
190
+ sharedResizeObserver().unobserve(target);
191
+ }
192
+
193
+ /** What a box wants to hear about: its content replaced, appended or edited. */
194
+ const CONTENT_OBSERVER_INIT: MutationObserverInit = {
195
+ childList: true,
196
+ subtree: true,
197
+ characterData: true,
198
+ };
199
+
200
+ type ContentHandler = () => void;
201
+
202
+ /**
203
+ * The handlers waiting on each observed box, and the boxes the shared observer
204
+ * currently holds. Keyed by `Node` because a `characterData` record names the
205
+ * TEXT node that changed, and the walk to the box that owns it goes through
206
+ * whatever nodes lie between.
207
+ */
208
+ const contentHandlers = new WeakMap<Node, Set<ContentHandler>>();
209
+ const contentTargets = new Set<Element>();
210
+ let sharedContent: MutationObserver | undefined;
211
+ /** Registrations the shared observer still holds for released boxes. */
212
+ let staleContentTargets = 0;
213
+
214
+ function deliverContentRecords(records: readonly MutationRecord[]): void {
215
+ const fired = new Set<ContentHandler>();
216
+ for (const record of records) {
217
+ // A `childList` record names the changed parent and a `characterData` record
218
+ // the text node itself, either of them at any depth under a box. Every box
219
+ // on the ancestor chain therefore owns the change, exactly as a separate
220
+ // observer on each of them would have seen it — a `<pre>` inside a
221
+ // `ScrollRegion` measures both.
222
+ for (let node: Node | null = record.target; node !== null; node = node.parentNode) {
223
+ for (const handler of contentHandlers.get(node) ?? []) fired.add(handler);
224
+ }
225
+ }
226
+ for (const handler of fired) handler();
227
+ }
228
+
229
+ function sharedContentObserver(): MutationObserver {
230
+ sharedContent ??= new MutationObserver(deliverContentRecords);
231
+ return sharedContent;
232
+ }
233
+
234
+ function holdContentTarget(target: Element, handler: ContentHandler): void {
235
+ let handlers = contentHandlers.get(target);
236
+ if (handlers === undefined) {
237
+ handlers = new Set<ContentHandler>();
238
+ contentHandlers.set(target, handlers);
239
+ contentTargets.add(target);
240
+ sharedContentObserver().observe(target, CONTENT_OBSERVER_INIT);
241
+ }
242
+ handlers.add(handler);
243
+ }
244
+
245
+ /**
246
+ * Stops watching `target`'s content for `handler`.
247
+ *
248
+ * `MutationObserver` has no `unobserve`: the only way to drop one registration
249
+ * is to disconnect the observer and re-observe everything else, which would cost
250
+ * one registration per surviving box every time a box unmounts. A released box
251
+ * is therefore dropped from the handler map at once — its records find no
252
+ * handler and are ignored — and the observer is rebuilt only once the dead
253
+ * registrations outnumber the live ones, which keeps the total registration work
254
+ * proportional to the number of mounts. Records already queued survive the
255
+ * rebuild: they are taken off the observer and delivered by hand.
256
+ */
257
+ function releaseContentTarget(target: Element, handler: ContentHandler): void {
258
+ const handlers = contentHandlers.get(target);
259
+ if (handlers === undefined) return;
260
+ handlers.delete(handler);
261
+ if (handlers.size > 0) return;
262
+ contentHandlers.delete(target);
263
+ contentTargets.delete(target);
264
+ staleContentTargets += 1;
265
+ if (staleContentTargets <= contentTargets.size) return;
266
+
267
+ const observer = sharedContentObserver();
268
+ const queued = observer.takeRecords();
269
+ observer.disconnect();
270
+ staleContentTargets = 0;
271
+ for (const live of contentTargets) observer.observe(live, CONTENT_OBSERVER_INIT);
272
+ if (queued.length > 0) deliverContentRecords(queued);
273
+ }
274
+
275
+ /** The attribute set a scrolling box wears; every value is absent while it fits. */
276
+ export interface OverflowRegionAttributes {
277
+ /** Attach to the scrolling element; it is what the measurement follows. */
278
+ readonly ref: RefCallback<HTMLElement>;
279
+ readonly tabIndex?: 0;
280
+ readonly role?: 'region';
281
+ readonly 'aria-label'?: string;
282
+ }
283
+
284
+ /**
285
+ * The conditional region attributes for a scrolling box, alongside the ref that
286
+ * names it. The ref must go on the scrolling box ITSELF — a `<pre>`, a JSON
287
+ * pane, or the `div` `ScrollRegion` renders. Use the hook when wrapping the
288
+ * content in a `ScrollRegion` would give the surface a second scroller.
289
+ *
290
+ * The box is measured as it is attached, whenever it or any child resizes, and
291
+ * whenever its content changes — replaced, appended, or edited in place. A
292
+ * replaced child is a NEW element, so the registration is re-pointed at the
293
+ * current children before each measurement, adding and dropping only the ones
294
+ * that actually changed; both sides of that are registrations on the shared
295
+ * observers, not observers of this mount's own. The only DOM change a
296
+ * measurement can cause is the region ATTRIBUTES this hook returns, and
297
+ * attributes are deliberately left unobserved — that, not an absence of
298
+ * mutation, is what stops the pair from re-triggering each other.
299
+ *
300
+ * @param ref - a consumer ref that wants the same element, or `undefined`.
301
+ * @param label - its accessible name, applied only while it actually scrolls.
302
+ */
303
+ export function useOverflowRegion(
304
+ ref: Ref<HTMLElement> | undefined,
305
+ label: string,
306
+ ): OverflowRegionAttributes {
307
+ const [scrollable, setScrollable] = useState(false);
308
+
309
+ // Attached and released with the element, so a box that mounts later than the
310
+ // hook, or is swapped for another, is instrumented exactly once either way.
311
+ const measuredRef = useCallback(
312
+ (box: HTMLElement): (() => void) => {
313
+ const detachConsumer = attachRef(ref, box);
314
+
315
+ const measure = (): void => {
316
+ setScrollable(needsRegion(box));
317
+ };
318
+
319
+ // The box gives resize; its children give the overflowing width. The
320
+ // shared observer is released target by target rather than disconnected,
321
+ // because every other scrolling box on the page is registered on the same
322
+ // one — and only the targets that actually came or went are touched, so an
323
+ // edit in place re-registers nothing and costs one measurement.
324
+ let observed: Element[] = [];
325
+ const observeAll = (): void => {
326
+ const current = [box, ...box.children];
327
+ const kept = new Set<Element>(current);
328
+ for (const target of observed) {
329
+ if (!kept.has(target)) releaseResizeTarget(target, measure);
330
+ }
331
+ for (const target of current) holdResizeTarget(target, measure);
332
+ observed = current;
333
+ measure();
334
+ };
335
+
336
+ // The whole subtree, text included: content is as often EDITED IN PLACE —
337
+ // a longer code string, a different JSON body — as it is replaced, and
338
+ // React reuses the element when it does, so watching the direct child list
339
+ // alone would freeze the measurement at whatever the first content needed.
340
+ // ATTRIBUTES are deliberately absent: the region attributes are what a
341
+ // measurement writes, and observing them would make each pass trigger the
342
+ // next.
343
+ holdContentTarget(box, observeAll);
344
+ observeAll();
345
+
346
+ // A stop held open only because the box had focus outlives its reason the
347
+ // moment the reader leaves, so re-measure then and let it go.
348
+ const releaseHeldStop = (): void => {
349
+ measure();
350
+ };
351
+ box.addEventListener('blur', releaseHeldStop);
352
+
353
+ return () => {
354
+ for (const target of observed) releaseResizeTarget(target, measure);
355
+ observed = [];
356
+ releaseContentTarget(box, observeAll);
357
+ box.removeEventListener('blur', releaseHeldStop);
358
+ detachConsumer();
359
+ };
360
+ },
361
+ [ref],
362
+ );
363
+
364
+ if (!scrollable) return { ref: measuredRef };
365
+ return { ref: measuredRef, tabIndex: 0, role: 'region', 'aria-label': label };
366
+ }
367
+
368
+ export function ScrollRegion({
369
+ label,
370
+ children,
371
+ className,
372
+ style,
373
+ 'data-testid': testId,
374
+ ref,
375
+ }: ScrollRegionProps) {
376
+ const region = useOverflowRegion(ref, label);
377
+
378
+ return (
379
+ <div
380
+ className={
381
+ className === undefined ? SCROLL_REGION_CLASS : `${SCROLL_REGION_CLASS} ${className}`
382
+ }
383
+ style={style}
384
+ data-testid={testId}
385
+ {...region}
386
+ >
387
+ {children}
388
+ </div>
389
+ );
390
+ }
391
+
392
+ /**
393
+ * The wrapper a prose table sits inside, creating it on first pass. Idempotent:
394
+ * a table already inside a `.tai-scroll-region` keeps the wrapper it has.
395
+ */
396
+ function ensureScrollWrapper(table: HTMLTableElement): HTMLElement {
397
+ const parent = table.parentElement;
398
+ if (parent?.classList.contains(SCROLL_REGION_CLASS) === true) return parent;
399
+ const wrapper = table.ownerDocument.createElement('div');
400
+ wrapper.className = SCROLL_REGION_CLASS;
401
+ table.replaceWith(wrapper);
402
+ wrapper.append(table);
403
+ return wrapper;
404
+ }
405
+
406
+ /** A heading that can name the surfaces below it, and the text it would give. */
407
+ interface ProseHeading {
408
+ readonly element: Element;
409
+ readonly text: string;
410
+ }
411
+
412
+ /** A scrollable prose surface, with the heading text that names it. */
413
+ interface LabelledProseSurface {
414
+ readonly element: HTMLElement;
415
+ readonly heading: string | undefined;
416
+ }
417
+
418
+ /**
419
+ * The text of the last heading in `headings` that does not ENCLOSE `surface`.
420
+ *
421
+ * A heading wrapping the surface does precede it in document order, but it is
422
+ * not a section the surface sits UNDER, so it is stepped over in favour of the
423
+ * heading before it — which is the answer an outward walk from the surface
424
+ * reaches. In well-formed prose no heading contains a table or a code block, so
425
+ * this returns the last heading on the first look.
426
+ */
427
+ function namingHeadingText(
428
+ headings: readonly ProseHeading[],
429
+ surface: Element,
430
+ ): string | undefined {
431
+ for (let index = headings.length - 1; index >= 0; index -= 1) {
432
+ const heading = headings[index];
433
+ if (heading !== undefined && !heading.element.contains(surface)) return heading.text;
434
+ }
435
+ return undefined;
436
+ }
437
+
438
+ /**
439
+ * Every scrollable prose surface under `root` — each `<table>` and each `<pre>`
440
+ * — paired with the text of the nearest heading PRECEDING it, or `undefined`
441
+ * when no heading does.
442
+ *
443
+ * One query returns the headings and the surfaces interleaved in document order,
444
+ * so a single walk over that list names all of them: the heading a surface
445
+ * belongs to is the last one seen before reaching it. Naming each surface by
446
+ * searching backwards from it instead would re-read the same prose once per
447
+ * surface, at a cost that grows with the product of the two.
448
+ *
449
+ * Blank headings never enter the list, so a decorative empty heading names
450
+ * nothing and the section above it is used instead. A heading INSIDE a surface
451
+ * (a `<pre>` or a table cell holding one) comes AFTER that surface in document
452
+ * order and so can never name it, though it does name later surfaces — exactly
453
+ * as being the last heading before them implies.
454
+ *
455
+ * Read in full before the pass wraps anything, and safely so: a name depends
456
+ * only on what PRECEDES its surface, and `ensureScrollWrapper` puts the wrapper
457
+ * in the table's own place, moving nothing else and leaving every heading where
458
+ * it was — so a name stays true whether its table is wrapped yet or not.
459
+ */
460
+ function labelledProseSurfaces(root: Element): LabelledProseSurface[] {
461
+ const headings: ProseHeading[] = [];
462
+ const surfaces: LabelledProseSurface[] = [];
463
+
464
+ for (const node of root.querySelectorAll<HTMLElement>(PROSE_SURFACE_SELECTOR)) {
465
+ if (node.matches(HEADING_SELECTOR)) {
466
+ // textContent is nullable under strict DOM typings; the local eslint
467
+ // profile assumes non-null, so the necessary guard trips its rule.
468
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
469
+ const text = (node.textContent ?? '').trim();
470
+ if (text !== '') headings.push({ element: node, text });
471
+ continue;
472
+ }
473
+ surfaces.push({ element: node, heading: namingHeadingText(headings, node) });
474
+ }
475
+ return surfaces;
476
+ }
477
+
478
+ /** An instrumented prose surface, with the name it wears while it scrolls. */
479
+ interface TrackedSurface {
480
+ readonly surface: HTMLElement;
481
+ readonly label: string;
482
+ }
483
+
484
+ /** A prose surface's scrolling box and the name it would take on its own. */
485
+ interface ProseSurfaceBox {
486
+ readonly surface: HTMLElement;
487
+ readonly name: string;
488
+ }
489
+
490
+ /**
491
+ * The same surfaces, each given a name no other one in the list carries.
492
+ *
493
+ * A name is only as useful as it is distinguishing, and one heading names every
494
+ * surface under it: two wide tables in one section, or two code blocks under no
495
+ * heading at all, would otherwise be two landmarks a reader hears identically
496
+ * and cannot choose between. A name more than one surface claims is therefore
497
+ * numbered, in document order, and a name only one surface claims is left alone.
498
+ */
499
+ function uniquelyNamed(boxes: readonly ProseSurfaceBox[]): TrackedSurface[] {
500
+ const claims = new Map<string, number>();
501
+ for (const { name } of boxes) claims.set(name, (claims.get(name) ?? 0) + 1);
502
+
503
+ const taken = new Map<string, number>();
504
+ return boxes.map(({ surface, name }) => {
505
+ if (claims.get(name) === 1) return { surface, label: name };
506
+ const ordinal = (taken.get(name) ?? 0) + 1;
507
+ taken.set(name, ordinal);
508
+ return { surface, label: `${name} (${String(ordinal)})` };
509
+ });
510
+ }
511
+
512
+ /** Strips the region attributes from a surface that is not a region now. */
513
+ function clearScrollRegionAttributes(wrapper: HTMLElement): void {
514
+ wrapper.removeAttribute('tabindex');
515
+ wrapper.removeAttribute('role');
516
+ wrapper.removeAttribute('aria-label');
517
+ }
518
+
519
+ /** Applies the same conditional attribute set `ScrollRegion` renders. */
520
+ function applyScrollRegionAttributes(
521
+ wrapper: HTMLElement,
522
+ label: string,
523
+ scrolling: boolean,
524
+ ): void {
525
+ if (!scrolling) {
526
+ clearScrollRegionAttributes(wrapper);
527
+ return;
528
+ }
529
+ wrapper.setAttribute('tabindex', '0');
530
+ wrapper.setAttribute('role', 'region');
531
+ wrapper.setAttribute('aria-label', label);
532
+ }
533
+
534
+ /**
535
+ * Re-applies the region attributes to `tracked`, reading ALL of them before
536
+ * writing any.
537
+ *
538
+ * `needsRegion` reads `scrollWidth`, which forces layout if anything has been
539
+ * written since the last one — so measuring and writing surface by surface makes
540
+ * the browser re-lay-out the prose once per surface, every time. Two phases cost
541
+ * one layout for the whole batch however many surfaces it holds.
542
+ */
543
+ function refreshRegions(tracked: readonly TrackedSurface[]): void {
544
+ const scrolling = tracked.map((entry) => needsRegion(entry.surface));
545
+ for (const [index, entry] of tracked.entries()) {
546
+ applyScrollRegionAttributes(entry.surface, entry.label, scrolling[index] === true);
547
+ }
548
+ }
549
+
550
+ /**
551
+ * Instruments the scrollable surfaces under the element the returned ref is
552
+ * attached to — every `<table>` and every `<pre>` — as scroll regions. React
553
+ * cannot wrap markup it did not create, so this walks the DOM instead. A table
554
+ * is moved into a `div.tai-scroll-region` (once — the pass is idempotent)
555
+ * because the table itself is not the scrolling box; a `<pre>` already IS its
556
+ * own scrolling box, so it is instrumented in place. Either way the scrolling
557
+ * element carries the same conditional `tabindex`/`role`/`aria-label` as
558
+ * `ScrollRegion`.
559
+ *
560
+ * The name is the nearest heading preceding the surface — read for all of them
561
+ * in one document-order pass — so a reader landing on the region hears which
562
+ * section it belongs to; `labels` covers a surface with no heading above it, and
563
+ * a name shared by several surfaces is numbered so no two regions answer to the
564
+ * same one.
565
+ *
566
+ * The pass runs from the ref callback rather than an effect, which puts it in
567
+ * the commit phase, BEFORE the browser paints: `.tai-prose table` is
568
+ * `width: 100%` with no overflow of its own — the scroller exists only on the
569
+ * wrapper this builds — so a pass that ran after paint would show one frame at
570
+ * 320/360 px with the table overflowing the document, then jolt sideways. A
571
+ * passive-effect probe measured exactly that: at first paint wrappers=0 and the
572
+ * table's parent was `.tai-prose`.
573
+ *
574
+ * Injected HTML is replaced wholesale when its source changes, so the pass is
575
+ * re-run from a `MutationObserver` on the subtree rather than on attachment
576
+ * alone.
577
+ *
578
+ * @param labels - the names for surfaces with no preceding heading.
579
+ * @returns the ref for the element whose subtree holds the injected markup.
580
+ */
581
+ export function useProseScrollRegions(labels?: ProseScrollLabels): RefCallback<HTMLElement> {
582
+ const tableLabel = labels?.table ?? DEFAULT_PROSE_LABELS.table;
583
+ const preLabel = labels?.pre ?? DEFAULT_PROSE_LABELS.pre;
584
+
585
+ return useCallback(
586
+ (root: HTMLElement): (() => void) => {
587
+ // Every observed element, mapped to the surface whose width it reports and
588
+ // that surface's current name. This is what lets a resize re-measure just
589
+ // the surfaces that moved: an observer callback carries the elements that
590
+ // resized, and each of them answers here with the region it belongs to.
591
+ const surfaceOf = new WeakMap<Element, TrackedSurface>();
592
+ // What the observer holds and what wears region attributes right now. A
593
+ // re-instrumentation replaces the surfaces wholesale, and the elements it
594
+ // drops are detached ones: kept observed they would be registrations on
595
+ // dead nodes, kept named they would be landmarks nobody can reach.
596
+ let observed = new Set<Element>();
597
+ let instrumented = new Set<HTMLElement>();
598
+
599
+ // The box gives resize; its children give the overflowing width. A table
600
+ // that grows wider inside a parent-constrained wrapper resizes nothing
601
+ // else, so watching the wrapper alone would freeze the mount-time
602
+ // measurement. Membership of `observed` doubles as the re-observation
603
+ // guard: re-observing an element already under a ResizeObserver re-arms
604
+ // its initial notification, which would make each pass trigger the next
605
+ // one forever.
606
+ const track = (entry: TrackedSurface, next: Set<Element>): void => {
607
+ for (const target of [entry.surface, ...entry.surface.children]) {
608
+ // Rewritten even when already observed: a re-run may have found a new
609
+ // heading above the surface, and the name has to follow it.
610
+ surfaceOf.set(target, entry);
611
+ next.add(target);
612
+ if (!observed.has(target)) resizeObserver.observe(target);
613
+ }
614
+ };
615
+
616
+ // A pass over the whole subtree. It reads the observers declared below it,
617
+ // which is safe because nothing calls it until both exist.
618
+ const instrument = (): void => {
619
+ // Wrapping mutates the subtree; pause the observer so this pass cannot
620
+ // re-trigger itself, and drop the records it generated before resuming.
621
+ mutationObserver.disconnect();
622
+ // Every name comes from one document-order read of the prose, taken
623
+ // before the first wrapper goes in. The list is a static snapshot and
624
+ // the surfaces in it stay the same elements: wrapping moves a table one
625
+ // level down, into a `div` standing exactly where the table stood, so
626
+ // neither the remaining entries nor the names already computed for them
627
+ // change. Every table is wrapped, cap or no cap — the wrapper is what
628
+ // keeps a `width: 100%` table inside the column.
629
+ const boxes = labelledProseSurfaces(root).map(({ element, heading }) => {
630
+ const table = element instanceof HTMLTableElement;
631
+ return {
632
+ surface: table ? ensureScrollWrapper(element) : element,
633
+ name: heading ?? (table ? tableLabel : preLabel),
634
+ };
635
+ });
636
+ const tracked = uniquelyNamed(boxes.slice(0, MAX_PROSE_REGIONS));
637
+
638
+ const next = new Set<Element>();
639
+ for (const entry of tracked) track(entry, next);
640
+ for (const target of observed) {
641
+ if (!next.has(target)) resizeObserver.unobserve(target);
642
+ }
643
+ observed = next;
644
+
645
+ const dropped = instrumented;
646
+ instrumented = new Set(tracked.map((entry) => entry.surface));
647
+ // Wrapping is done for every surface before the first measurement, so
648
+ // the whole pass costs one layout rather than one per surface.
649
+ refreshRegions(tracked);
650
+ for (const surface of dropped) {
651
+ if (!instrumented.has(surface) && root.contains(surface)) {
652
+ clearScrollRegionAttributes(surface);
653
+ }
654
+ }
655
+ mutationObserver.takeRecords();
656
+ mutationObserver.observe(root, { childList: true, subtree: true });
657
+ };
658
+
659
+ // Only the surfaces that actually resized are re-measured. Re-running the
660
+ // whole instrumentation pass from here re-queried the entire prose subtree
661
+ // on every resize frame, and a document with many surfaces spends that
662
+ // cost once per frame for the whole time a pane is being dragged.
663
+ const resizeObserver = new ResizeObserver((entries) => {
664
+ // A surface and its children are observed separately, so one frame can
665
+ // deliver several entries naming the same region; measuring it once is
666
+ // enough.
667
+ const affected = new Map<HTMLElement, TrackedSurface>();
668
+ for (const entry of entries) {
669
+ const tracked = surfaceOf.get(entry.target);
670
+ if (tracked !== undefined) affected.set(tracked.surface, tracked);
671
+ }
672
+ refreshRegions([...affected.values()]);
673
+ });
674
+ const mutationObserver = new MutationObserver(instrument);
675
+
676
+ // A stop held open only because the region had focus outlives its reason
677
+ // the moment the reader leaves. `focusout` bubbles, so one listener on the
678
+ // root covers every instrumented surface under it, however often they are
679
+ // replaced; the attribute pair is exactly what this hook writes, so it is
680
+ // also what identifies a region among the prose's other focusable content.
681
+ const releaseHeldStop = (event: FocusEvent): void => {
682
+ const left = event.target;
683
+ if (!(left instanceof HTMLElement)) return;
684
+ if (left.getAttribute('role') !== 'region' || !left.hasAttribute('tabindex')) return;
685
+ // The stop being released belongs to exactly one region, so re-measure
686
+ // that one rather than re-walking the prose. A region this hook did not
687
+ // instrument is not ours to release.
688
+ const tracked = surfaceOf.get(left);
689
+ if (tracked !== undefined) refreshRegions([tracked]);
690
+ };
691
+ root.addEventListener('focusout', releaseHeldStop);
692
+
693
+ instrument();
694
+
695
+ return () => {
696
+ resizeObserver.disconnect();
697
+ mutationObserver.disconnect();
698
+ root.removeEventListener('focusout', releaseHeldStop);
699
+ };
700
+ },
701
+ [tableLabel, preLabel],
702
+ );
703
+ }