@dudko.dev/agent-web-ui 0.0.0 → 0.0.1

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 (273) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +251 -5
  3. package/dist/canvas.cjs +266 -0
  4. package/dist/canvas.cjs.map +1 -0
  5. package/dist/canvas.d.cts +105 -0
  6. package/dist/canvas.d.ts +105 -0
  7. package/dist/canvas.js +200 -0
  8. package/dist/canvas.js.map +1 -0
  9. package/dist/chunk-4EHUITGU.js +342 -0
  10. package/dist/chunk-4EHUITGU.js.map +1 -0
  11. package/dist/chunk-7O64ZI6Z.js +125 -0
  12. package/dist/chunk-7O64ZI6Z.js.map +1 -0
  13. package/dist/chunk-A55Q377T.js +342 -0
  14. package/dist/chunk-A55Q377T.js.map +1 -0
  15. package/dist/chunk-EL2YCROY.js +127 -0
  16. package/dist/chunk-EL2YCROY.js.map +1 -0
  17. package/dist/chunk-GB7RZJMG.js +98 -0
  18. package/dist/chunk-GB7RZJMG.js.map +1 -0
  19. package/dist/chunk-KZQB35LS.js +22 -0
  20. package/dist/chunk-KZQB35LS.js.map +1 -0
  21. package/dist/chunk-P2F2P3JC.js +400 -0
  22. package/dist/chunk-P2F2P3JC.js.map +1 -0
  23. package/dist/chunk-TPK25QGK.js +342 -0
  24. package/dist/chunk-TPK25QGK.js.map +1 -0
  25. package/dist/en-CcNUz2tN.d.cts +342 -0
  26. package/dist/en-CcNUz2tN.d.ts +342 -0
  27. package/dist/index.cjs +4143 -0
  28. package/dist/index.cjs.map +1 -0
  29. package/dist/index.d.cts +919 -0
  30. package/dist/index.d.ts +919 -0
  31. package/dist/index.js +3771 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/labels-B5xpIGf_.d.cts +25 -0
  34. package/dist/labels-B5xpIGf_.d.ts +25 -0
  35. package/dist/labels-D5BrHct4.d.cts +1208 -0
  36. package/dist/labels-UuWI7wPo.d.ts +1208 -0
  37. package/dist/locales/be.cjs +794 -0
  38. package/dist/locales/be.cjs.map +1 -0
  39. package/dist/locales/be.d.cts +14 -0
  40. package/dist/locales/be.d.ts +14 -0
  41. package/dist/locales/be.js +374 -0
  42. package/dist/locales/be.js.map +1 -0
  43. package/dist/locales/bg.cjs +747 -0
  44. package/dist/locales/bg.cjs.map +1 -0
  45. package/dist/locales/bg.d.cts +14 -0
  46. package/dist/locales/bg.d.ts +14 -0
  47. package/dist/locales/bg.js +327 -0
  48. package/dist/locales/bg.js.map +1 -0
  49. package/dist/locales/ca.cjs +773 -0
  50. package/dist/locales/ca.cjs.map +1 -0
  51. package/dist/locales/ca.d.cts +14 -0
  52. package/dist/locales/ca.d.ts +14 -0
  53. package/dist/locales/ca.js +353 -0
  54. package/dist/locales/ca.js.map +1 -0
  55. package/dist/locales/cs.cjs +794 -0
  56. package/dist/locales/cs.cjs.map +1 -0
  57. package/dist/locales/cs.d.cts +14 -0
  58. package/dist/locales/cs.d.ts +14 -0
  59. package/dist/locales/cs.js +374 -0
  60. package/dist/locales/cs.js.map +1 -0
  61. package/dist/locales/da.cjs +747 -0
  62. package/dist/locales/da.cjs.map +1 -0
  63. package/dist/locales/da.d.cts +14 -0
  64. package/dist/locales/da.d.ts +14 -0
  65. package/dist/locales/da.js +327 -0
  66. package/dist/locales/da.js.map +1 -0
  67. package/dist/locales/de.cjs +756 -0
  68. package/dist/locales/de.cjs.map +1 -0
  69. package/dist/locales/de.d.cts +14 -0
  70. package/dist/locales/de.d.ts +14 -0
  71. package/dist/locales/de.js +336 -0
  72. package/dist/locales/de.js.map +1 -0
  73. package/dist/locales/el.cjs +756 -0
  74. package/dist/locales/el.cjs.map +1 -0
  75. package/dist/locales/el.d.cts +14 -0
  76. package/dist/locales/el.d.ts +14 -0
  77. package/dist/locales/el.js +336 -0
  78. package/dist/locales/el.js.map +1 -0
  79. package/dist/locales/en.cjs +753 -0
  80. package/dist/locales/en.cjs.map +1 -0
  81. package/dist/locales/en.d.cts +10 -0
  82. package/dist/locales/en.d.ts +10 -0
  83. package/dist/locales/en.js +333 -0
  84. package/dist/locales/en.js.map +1 -0
  85. package/dist/locales/es-419.cjs +778 -0
  86. package/dist/locales/es-419.cjs.map +1 -0
  87. package/dist/locales/es-419.d.cts +16 -0
  88. package/dist/locales/es-419.d.ts +16 -0
  89. package/dist/locales/es-419.js +23 -0
  90. package/dist/locales/es-419.js.map +1 -0
  91. package/dist/locales/es.cjs +769 -0
  92. package/dist/locales/es.cjs.map +1 -0
  93. package/dist/locales/es.d.cts +14 -0
  94. package/dist/locales/es.d.ts +14 -0
  95. package/dist/locales/es.js +14 -0
  96. package/dist/locales/es.js.map +1 -0
  97. package/dist/locales/fi.cjs +753 -0
  98. package/dist/locales/fi.cjs.map +1 -0
  99. package/dist/locales/fi.d.cts +14 -0
  100. package/dist/locales/fi.d.ts +14 -0
  101. package/dist/locales/fi.js +333 -0
  102. package/dist/locales/fi.js.map +1 -0
  103. package/dist/locales/fr-CA.cjs +777 -0
  104. package/dist/locales/fr-CA.cjs.map +1 -0
  105. package/dist/locales/fr-CA.d.cts +17 -0
  106. package/dist/locales/fr-CA.d.ts +17 -0
  107. package/dist/locales/fr-CA.js +22 -0
  108. package/dist/locales/fr-CA.js.map +1 -0
  109. package/dist/locales/fr.cjs +769 -0
  110. package/dist/locales/fr.cjs.map +1 -0
  111. package/dist/locales/fr.d.cts +14 -0
  112. package/dist/locales/fr.d.ts +14 -0
  113. package/dist/locales/fr.js +14 -0
  114. package/dist/locales/fr.js.map +1 -0
  115. package/dist/locales/gn.cjs +750 -0
  116. package/dist/locales/gn.cjs.map +1 -0
  117. package/dist/locales/gn.d.cts +16 -0
  118. package/dist/locales/gn.d.ts +16 -0
  119. package/dist/locales/gn.js +330 -0
  120. package/dist/locales/gn.js.map +1 -0
  121. package/dist/locales/hr.cjs +769 -0
  122. package/dist/locales/hr.cjs.map +1 -0
  123. package/dist/locales/hr.d.cts +14 -0
  124. package/dist/locales/hr.d.ts +14 -0
  125. package/dist/locales/hr.js +349 -0
  126. package/dist/locales/hr.js.map +1 -0
  127. package/dist/locales/ht.cjs +750 -0
  128. package/dist/locales/ht.cjs.map +1 -0
  129. package/dist/locales/ht.d.cts +16 -0
  130. package/dist/locales/ht.d.ts +16 -0
  131. package/dist/locales/ht.js +330 -0
  132. package/dist/locales/ht.js.map +1 -0
  133. package/dist/locales/hu.cjs +753 -0
  134. package/dist/locales/hu.cjs.map +1 -0
  135. package/dist/locales/hu.d.cts +14 -0
  136. package/dist/locales/hu.d.ts +14 -0
  137. package/dist/locales/hu.js +333 -0
  138. package/dist/locales/hu.js.map +1 -0
  139. package/dist/locales/index.cjs +537 -0
  140. package/dist/locales/index.cjs.map +1 -0
  141. package/dist/locales/index.d.cts +38 -0
  142. package/dist/locales/index.d.ts +38 -0
  143. package/dist/locales/index.js +105 -0
  144. package/dist/locales/index.js.map +1 -0
  145. package/dist/locales/it.cjs +777 -0
  146. package/dist/locales/it.cjs.map +1 -0
  147. package/dist/locales/it.d.cts +14 -0
  148. package/dist/locales/it.d.ts +14 -0
  149. package/dist/locales/it.js +357 -0
  150. package/dist/locales/it.js.map +1 -0
  151. package/dist/locales/nb.cjs +747 -0
  152. package/dist/locales/nb.cjs.map +1 -0
  153. package/dist/locales/nb.d.cts +14 -0
  154. package/dist/locales/nb.d.ts +14 -0
  155. package/dist/locales/nb.js +327 -0
  156. package/dist/locales/nb.js.map +1 -0
  157. package/dist/locales/nl.cjs +753 -0
  158. package/dist/locales/nl.cjs.map +1 -0
  159. package/dist/locales/nl.d.cts +14 -0
  160. package/dist/locales/nl.d.ts +14 -0
  161. package/dist/locales/nl.js +333 -0
  162. package/dist/locales/nl.js.map +1 -0
  163. package/dist/locales/pl.cjs +794 -0
  164. package/dist/locales/pl.cjs.map +1 -0
  165. package/dist/locales/pl.d.cts +14 -0
  166. package/dist/locales/pl.d.ts +14 -0
  167. package/dist/locales/pl.js +374 -0
  168. package/dist/locales/pl.js.map +1 -0
  169. package/dist/locales/pt-BR.cjs +773 -0
  170. package/dist/locales/pt-BR.cjs.map +1 -0
  171. package/dist/locales/pt-BR.d.cts +14 -0
  172. package/dist/locales/pt-BR.d.ts +14 -0
  173. package/dist/locales/pt-BR.js +353 -0
  174. package/dist/locales/pt-BR.js.map +1 -0
  175. package/dist/locales/pt.cjs +777 -0
  176. package/dist/locales/pt.cjs.map +1 -0
  177. package/dist/locales/pt.d.cts +14 -0
  178. package/dist/locales/pt.d.ts +14 -0
  179. package/dist/locales/pt.js +357 -0
  180. package/dist/locales/pt.js.map +1 -0
  181. package/dist/locales/qu.cjs +756 -0
  182. package/dist/locales/qu.cjs.map +1 -0
  183. package/dist/locales/qu.d.cts +16 -0
  184. package/dist/locales/qu.d.ts +16 -0
  185. package/dist/locales/qu.js +336 -0
  186. package/dist/locales/qu.js.map +1 -0
  187. package/dist/locales/ro.cjs +769 -0
  188. package/dist/locales/ro.cjs.map +1 -0
  189. package/dist/locales/ro.d.cts +14 -0
  190. package/dist/locales/ro.d.ts +14 -0
  191. package/dist/locales/ro.js +349 -0
  192. package/dist/locales/ro.js.map +1 -0
  193. package/dist/locales/ru.cjs +789 -0
  194. package/dist/locales/ru.cjs.map +1 -0
  195. package/dist/locales/ru.d.cts +14 -0
  196. package/dist/locales/ru.d.ts +14 -0
  197. package/dist/locales/ru.js +369 -0
  198. package/dist/locales/ru.js.map +1 -0
  199. package/dist/locales/sk.cjs +794 -0
  200. package/dist/locales/sk.cjs.map +1 -0
  201. package/dist/locales/sk.d.cts +14 -0
  202. package/dist/locales/sk.d.ts +14 -0
  203. package/dist/locales/sk.js +374 -0
  204. package/dist/locales/sk.js.map +1 -0
  205. package/dist/locales/sq.cjs +753 -0
  206. package/dist/locales/sq.cjs.map +1 -0
  207. package/dist/locales/sq.d.cts +14 -0
  208. package/dist/locales/sq.d.ts +14 -0
  209. package/dist/locales/sq.js +333 -0
  210. package/dist/locales/sq.js.map +1 -0
  211. package/dist/locales/sr-Latn.cjs +841 -0
  212. package/dist/locales/sr-Latn.cjs.map +1 -0
  213. package/dist/locales/sr-Latn.d.cts +17 -0
  214. package/dist/locales/sr-Latn.d.ts +17 -0
  215. package/dist/locales/sr-Latn.js +86 -0
  216. package/dist/locales/sr-Latn.js.map +1 -0
  217. package/dist/locales/sr.cjs +769 -0
  218. package/dist/locales/sr.cjs.map +1 -0
  219. package/dist/locales/sr.d.cts +14 -0
  220. package/dist/locales/sr.d.ts +14 -0
  221. package/dist/locales/sr.js +14 -0
  222. package/dist/locales/sr.js.map +1 -0
  223. package/dist/locales/sv.cjs +753 -0
  224. package/dist/locales/sv.cjs.map +1 -0
  225. package/dist/locales/sv.d.cts +14 -0
  226. package/dist/locales/sv.d.ts +14 -0
  227. package/dist/locales/sv.js +333 -0
  228. package/dist/locales/sv.js.map +1 -0
  229. package/dist/locales/tr.cjs +750 -0
  230. package/dist/locales/tr.cjs.map +1 -0
  231. package/dist/locales/tr.d.cts +14 -0
  232. package/dist/locales/tr.d.ts +14 -0
  233. package/dist/locales/tr.js +330 -0
  234. package/dist/locales/tr.js.map +1 -0
  235. package/dist/locales/uk.cjs +794 -0
  236. package/dist/locales/uk.cjs.map +1 -0
  237. package/dist/locales/uk.d.cts +14 -0
  238. package/dist/locales/uk.d.ts +14 -0
  239. package/dist/locales/uk.js +374 -0
  240. package/dist/locales/uk.js.map +1 -0
  241. package/dist/markdown.cjs +620 -0
  242. package/dist/markdown.cjs.map +1 -0
  243. package/dist/markdown.d.cts +153 -0
  244. package/dist/markdown.d.ts +153 -0
  245. package/dist/markdown.js +573 -0
  246. package/dist/markdown.js.map +1 -0
  247. package/dist/mcp-app-entry-BA-Ca39z.d.ts +1480 -0
  248. package/dist/mcp-app-entry-tybABaHg.d.cts +1480 -0
  249. package/dist/mcp-app-policy-DXKQ2jjM.d.cts +73 -0
  250. package/dist/mcp-app-policy-DXKQ2jjM.d.ts +73 -0
  251. package/dist/mcp-app-sandbox.cjs +323 -0
  252. package/dist/mcp-app-sandbox.cjs.map +1 -0
  253. package/dist/mcp-app-sandbox.d.cts +70 -0
  254. package/dist/mcp-app-sandbox.d.ts +70 -0
  255. package/dist/mcp-app-sandbox.js +209 -0
  256. package/dist/mcp-app-sandbox.js.map +1 -0
  257. package/dist/mcp-app.cjs +980 -0
  258. package/dist/mcp-app.cjs.map +1 -0
  259. package/dist/mcp-app.d.cts +3 -0
  260. package/dist/mcp-app.d.ts +3 -0
  261. package/dist/mcp-app.js +816 -0
  262. package/dist/mcp-app.js.map +1 -0
  263. package/dist/pipeline.cjs +845 -0
  264. package/dist/pipeline.cjs.map +1 -0
  265. package/dist/pipeline.css +1407 -0
  266. package/dist/pipeline.d.cts +348 -0
  267. package/dist/pipeline.d.ts +348 -0
  268. package/dist/pipeline.js +778 -0
  269. package/dist/pipeline.js.map +1 -0
  270. package/dist/styles.css +4109 -0
  271. package/dist/types-BmvgYUzF.d.cts +19 -0
  272. package/dist/types-Cr5gmUts.d.ts +19 -0
  273. package/package.json +155 -8
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/markdown/index.ts","../src/named-files.ts","../src/markdown/parse.ts","../src/markdown/stream.ts","../src/markdown/render.ts"],"sourcesContent":["// `@dudko.dev/agent-web-ui/markdown`: the answers' Markdown as a tree any front end can draw -\n// the parser (`parse.ts`), the parser of a text still being written (`stream.ts`) and the helpers\n// every renderer needs (`render.ts`). Pure: no framework, no DOM, no import but this package's\n// own `workspacePathOf`. Not in the main entry: a host without Markdown does not carry it.\nexport * from './parse.js'\nexport * from './stream.js'\nexport * from './render.js'\n","/**\n * The workspace files an answer NAMES in its text (`<MessageList showNamedFiles>`): pure, no\n * React, no DOM, no import. What it reads as a path, exactly:\n *\n * - an inline code span whose whole content is an absolute path - `` `/attachments/a.png` ``\n * (one or more backticks, CommonMark's rule: closed by a run of the same length; the one space\n * inside each end is dropped);\n * - the destination of a Markdown link or image - `[a](/media/a.jpg)`, `![a](/media/a.jpg \"t\")`,\n * `[a](</notes/my notes.md>)` - percent-decoded.\n *\n * Nothing else: a path in plain words (`see /notes.md`), in a fenced code block (``` or ~~~ - a\n * listing, a command, an example: not something the answer hands over), in an indented block or\n * in a URL (`https://…`, `//host/…`) is not read. A path is absolute, one line, at most 512\n * characters, with no empty, `.` or `..` segment, no backslash or control character and no\n * trailing slash (a folder) - it is never normalised into another path: what is looked up is what\n * the answer wrote. Then {@link namedFilesOf} keeps only the paths that EXIST in the workspace's\n * listing - the one check that matters: a path that is not a file of the workspace is nothing.\n *\n * The scan is linear in the text, whatever the text holds (it runs while a message is drawn,\n * and the text is the model's - which a fetched page can steer): code spans by one pass over the\n * runs of backticks, links by a pattern that cannot backtrack across brackets, and no more than\n * {@link NAMED_FILES_TEXT_MAX} characters of it.\n */\n\n/** The most files an answer is shown with (see {@link namedFilesOf}). */\nexport const NAMED_FILES_MAX = 12\n\n/** The most distinct paths kept from one text (a bound on what is looked up, not on the scan). */\nexport const NAMED_FILES_SCAN_MAX = 200\n\n/**\n * The most of a text that is scanned: the first 100,000 characters. That is about 25,000 tokens -\n * more than an answer that hands files over is (above most models' default output limit for one\n * answer); past it, the text is a dump (a file pasted back, a runaway answer), whose paths are a\n * listing, not files handed over. It bounds the scan's (linear) work to a few milliseconds.\n */\nexport const NAMED_FILES_TEXT_MAX = 100_000\n\nconst PATH_MAX = 512\n\n/** `raw` as a workspace path, or `undefined` when it is not one (see the module's rules). */\nexport const workspacePathOf = (raw: string): string | undefined => {\n if (raw.length < 2 || raw.length > PATH_MAX) return undefined\n if (raw[0] !== '/' || raw[1] === '/') return undefined\n // Control characters (a line break among them), DEL, a backslash.\n // eslint-disable-next-line no-control-regex\n if (/[\\u0000-\\u001f\\u007f\\\\]/.test(raw)) return undefined\n const segments = raw.slice(1).split('/')\n for (const s of segments) {\n if (!s || s === '.' || s === '..' || s.trim() !== s) return undefined\n }\n return raw\n}\n\n/** The text with its fenced code blocks blanked out (the same length, line breaks kept). */\nconst withoutFences = (text: string): string => {\n const lines = text.split('\\n')\n let fence: { char: string; size: number } | undefined\n for (let i = 0; i < lines.length; i++) {\n const open = /^ {0,3}(`{3,}|~{3,})/.exec(lines[i])\n if (!fence) {\n if (open && !(open[1][0] === '`' && lines[i].slice(open[0].length).includes('`'))) {\n fence = { char: open[1][0], size: open[1].length }\n lines[i] = ' '.repeat(lines[i].length)\n }\n continue\n }\n const close = /^ {0,3}(`{3,}|~{3,})\\s*$/.exec(lines[i])\n if (close && close[1][0] === fence.char && close[1].length >= fence.size) fence = undefined\n lines[i] = ' '.repeat(lines[i].length)\n }\n return lines.join('\\n')\n}\n\nconst decoded = (s: string): string => {\n if (!s.includes('%')) return s\n try {\n return decodeURIComponent(s)\n } catch {\n return s\n }\n}\n\n/**\n * The code spans of a text, as CommonMark reads them: a run of backticks opens a span that the\n * next run of the SAME length closes (a run with no such run after it is just backticks). One\n * pass over the runs, and the closer of each found from a map of the next run of each length -\n * linear, where a regex with a back-reference tried every closer for every opener (a run of 300\n * backticks before 20,000 characters took seconds).\n */\nconst codeSpansOf = (text: string): { at: number; end: number; inner: string }[] => {\n const runs: { at: number; size: number }[] = []\n for (let i = text.indexOf('`'); i >= 0; i = text.indexOf('`', i)) {\n let j = i + 1\n while (j < text.length && text.charCodeAt(j) === 96) j++\n runs.push({ at: i, size: j - i })\n i = j\n }\n const next = new Array<number>(runs.length)\n const later = new Map<number, number>()\n for (let k = runs.length - 1; k >= 0; k--) {\n next[k] = later.get(runs[k].size) ?? -1\n later.set(runs[k].size, k)\n }\n const spans: { at: number; end: number; inner: string }[] = []\n for (let k = 0; k < runs.length;) {\n const close = next[k]\n if (close < 0) {\n k++\n continue\n }\n const open = runs[k]\n const end = runs[close].at + runs[close].size\n spans.push({ at: open.at, end, inner: text.slice(open.at + open.size, runs[close].at) })\n // The runs between the two are the span's content.\n k = close + 1\n }\n return spans\n}\n\n/**\n * A link or image: `[text](dest \"title\")`, the destination bare or in `<…>`. The text holds no\n * bracket at all - so a line of `[` is read once, not once from every `[` to the line's end.\n */\nconst LINK =\n /!?\\[[^[\\]\\n]*\\]\\(\\s*(<[^<>\\n]+>|[^\\s()<>]+)(?:\\s+(?:\"[^\"\\n]*\"|'[^'\\n]*'|\\([^()\\n]*\\)))?\\s*\\)/g\n\n/**\n * The workspace paths a text names, in the order they appear, each once, at most\n * {@link NAMED_FILES_SCAN_MAX}. Whether they exist is {@link namedFilesOf}'s question.\n */\nexport const pathsNamedIn = (text: string): string[] => {\n if (!text) return []\n const head = text.length > NAMED_FILES_TEXT_MAX ? text.slice(0, NAMED_FILES_TEXT_MAX) : text\n if (!head.includes('/')) return []\n const found: { at: number; path: string }[] = []\n let masked = withoutFences(head)\n // Code spans first, then blanked: a link written inside one is code, not a link.\n const spans = codeSpansOf(masked)\n const parts: string[] = []\n let from = 0\n for (const span of spans) {\n let inner = span.inner\n if (inner.length > 2 && inner.startsWith(' ') && inner.endsWith(' ')) inner = inner.slice(1, -1)\n const path = workspacePathOf(inner)\n if (path) found.push({ at: span.at, path })\n parts.push(masked.slice(from, span.at), ' '.repeat(span.end - span.at))\n from = span.end\n }\n masked = parts.join('') + masked.slice(from)\n for (const m of masked.matchAll(LINK)) {\n const dest = m[1].startsWith('<') ? m[1].slice(1, -1) : m[1]\n const path = workspacePathOf(decoded(dest))\n if (path) found.push({ at: m.index, path })\n }\n found.sort((a, b) => a.at - b.at)\n const out: string[] = []\n const seen = new Set<string>()\n for (const { path } of found) {\n if (seen.has(path)) continue\n seen.add(path)\n out.push(path)\n if (out.length >= NAMED_FILES_SCAN_MAX) break\n }\n return out\n}\n\nexport interface NamedFilesOptions {\n /** Paths left out - the files the answer wrote, the media it made: they have chips already. */\n exclude?: Iterable<string>\n /** At most this many (default {@link NAMED_FILES_MAX}). */\n max?: number\n}\n\n/**\n * The paths of `named` (from {@link pathsNamedIn}) that are files of the workspace - `exists`\n * answers from its listing, never by reading or fetching anything -, without `exclude`, in order,\n * at most `max` (default 12: each chip of a picture reads its bytes for the thumbnail, and an\n * answer that lists a folder of two hundred files would otherwise read them all - the chips are\n * for the files the answer hands over, not a second file browser; the Files panel has them all).\n */\nexport const namedFilesOf = (\n named: readonly string[],\n exists: (path: string) => boolean,\n { exclude, max = NAMED_FILES_MAX }: NamedFilesOptions = {},\n): string[] => {\n const skip = new Set(exclude ?? [])\n const out: string[] = []\n for (const path of named) {\n if (out.length >= max) break\n if (skip.has(path) || !exists(path)) continue\n skip.add(path)\n out.push(path)\n }\n return out\n}\n","/**\n * Markdown of an answer → blocks and inline runs (pure: no framework, no DOM). A renderer turns\n * them into elements - the React package's `<Markdown>` (`@dudko.dev/agent-web-react/markdown`),\n * or one of any other front end - never into HTML. What answers use: paragraphs with their line\n * breaks, headings, lists (nested), task boxes as text, code blocks, quotes, rules, GFM tables,\n * and inline **bold**, *italic*, ~~struck~~, `code`, links and bare URLs. Raw HTML stays text. A\n * link is a link only for http(s) and mailto ({@link safeHref}) - anything else (`javascript:`,\n * `data:`, `//host`) is its text -, or a FILE when its destination is a workspace path (bare or in\n * `<…>`, with an optional title; `workspacePathOf`, after percent-decoding): `{ t: 'file' }`,\n * which a renderer draws as a link-styled control that opens the file - only if the workspace has\n * it, else as the link's words. A code span whose whole content is such a path carries it too\n * (`path`), for the same control. A link's words hold no link of their own: no bare URL, no path.\n * An image (`![a](…)`) is its `!` and a link (or a file): no picture is ever loaded from an answer.\n *\n * An answer is drawn while it is written: `stream.ts` has the rules for a text that is not\n * finished (what is held back, what is drawn as if closed), so that nothing already on the\n * screen changes its kind or its height when the rest arrives.\n */\n\nimport { workspacePathOf } from '../named-files.js'\n\nexport type MarkdownInline =\n | { t: 'text'; v: string }\n | { t: 'br' }\n | { t: 'code'; v: string; path?: string }\n | { t: 'strong' | 'em' | 'del'; c: MarkdownInline[] }\n | { t: 'link'; href: string; c: MarkdownInline[] }\n | { t: 'file'; path: string; c: MarkdownInline[] }\n\nexport type MarkdownAlign = 'left' | 'center' | 'right' | undefined\n\nexport interface MarkdownListItem {\n c: MarkdownInline[]\n sub?: MarkdownListBlock\n}\nexport interface MarkdownListBlock {\n t: 'list'\n ordered: boolean\n start: number\n items: MarkdownListItem[]\n}\n\nexport type MarkdownBlock =\n | { t: 'p'; c: MarkdownInline[] }\n | { t: 'h'; level: 1 | 2 | 3 | 4 | 5 | 6; c: MarkdownInline[] }\n | { t: 'code'; lang: string; v: string }\n | { t: 'quote'; c: MarkdownBlock[] }\n | { t: 'hr' }\n | MarkdownListBlock\n | { t: 'table'; align: MarkdownAlign[]; head: MarkdownInline[][]; rows: MarkdownInline[][][] }\n\nconst decoded = (s: string): string => {\n if (!s.includes('%')) return s\n try {\n return decodeURIComponent(s)\n } catch {\n return s\n }\n}\n\n/** The longest workspace path (the React package's `workspacePathOf` refuses a longer one). */\nconst PATH_MAX = 512\n/**\n * The longest destination that can still name such a path: each of its characters written at\n * most as `%E2%80%AE` (nine characters for one). A longer one is refused before it is read.\n */\nconst DEST_MAX = 9 * PATH_MAX\n/** The longest title a link to a file may carry - as long as a path. */\nconst TITLE_MAX = PATH_MAX\n\n/**\n * The workspace path a link's destination (what is between its parentheses) names, or\n * `undefined` - read exactly as an answer's link is (`parseMarkdownInline`'s file links): bare or in\n * `<…>` (which may hold spaces and parentheses), with an optional `\"title\"` / `'title'` after\n * it, percent-decoded BEFORE the check - `%2e%2e` is `..`, `%2F%2Fhost` is `//host`, `%5C` a\n * backslash, all refused - and then the React package's `workspacePathOf`, the rules its file\n * chips use (absolute, no `..` / `.` / empty segment, no `//host`, no backslash or control\n * character, never normalised).\n */\nexport const workspaceLinkPath = (raw: string): string | undefined => {\n const runs = parseMarkdownInline(`[x](${raw})`)\n return runs.length === 1 && runs[0].t === 'file' ? runs[0].path : undefined\n}\n\n/**\n * The URL a link may point to, or `undefined`: `http://`, `https://` or `mailto:` (any case), then\n * no blank at all - as the text holds it, never decoded (`&#106;avascript:`, `java%73cript:` and\n * `java\\tscript:` are no scheme of these, so text). Nothing relative or protocol-relative\n * (`/a`, `//host`), no `javascript:`, `data:`, `vbscript:`, `file:` or any other scheme. Blanks\n * around it are dropped.\n */\nexport const safeHref = (url: string): string | undefined => {\n const u = url.trim()\n return /^(https?:\\/\\/|mailto:)[^\\s]+$/i.test(u) ? u : undefined\n}\n\n// ── inline ──────────────────────────────────────────────────────────────────\n\ntype Delim = '*' | '_' | '~'\ntype Token =\n | MarkdownInline\n | {\n t: 'delim'\n ch: Delim\n n: 1 | 2\n open: boolean\n close: boolean\n /** Not inside a word on its left (`2*3` is not the start of emphasis still to close). */\n lead?: boolean\n match?: number\n }\n\nconst spaceCode = (code: number) =>\n code === 32 || (code >= 9 && code <= 13) || (code > 127 && /\\s/.test(String.fromCharCode(code)))\nconst isSpace = (c: string | undefined) => c === undefined || spaceCode(c.charCodeAt(0))\nconst isWord = (c: string | undefined) => {\n if (c === undefined) return false\n const code = c.charCodeAt(0)\n if (code < 128)\n return (code >= 48 && code <= 57) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122)\n return /[\\p{L}\\p{N}]/u.test(c)\n}\nconst URL_START = /^https?:\\/\\//i\nconst URL_TAIL = /[.,;:!?'\")\\]]+$/\n\n/**\n * For each position, where the next `ch` is at or after it (`text.length` when none): built in\n * one pass from the end, so every \"find the closing bracket\" below is a lookup, not a scan.\n */\nconst nextOf = (text: string, test: (code: number) => boolean): Int32Array => {\n const next = new Int32Array(text.length + 1)\n next[text.length] = text.length\n for (let i = text.length - 1; i >= 0; i--) next[i] = test(text.charCodeAt(i)) ? i : next[i + 1]\n return next\n}\n\nconst pushText = (out: MarkdownInline[], v: string) => {\n if (!v) return\n const last = out[out.length - 1]\n if (last?.t === 'text') last.v += v\n else out.push({ t: 'text', v })\n}\n\n/**\n * Inline runs of one paragraph, heading, list item or table cell. A single scan, linear in the\n * text (no backtracking regex: a long line of unmatched `**`, `_`, backticks or brackets costs\n * the same as plain text): code spans first, then links and bare URLs, then emphasis matched\n * with one opener stack per kind of delimiter. `words`: the words of a link, where nothing is a\n * link again - a bare URL is text, a code span is no path.\n */\nexport const parseMarkdownInline = (\n source: string,\n depth = 0,\n tail = false,\n words = false,\n): MarkdownInline[] => {\n // The end of a text still being written: a run of markers there may be half of a longer one,\n // an opener or a closer. It waits for what follows (see `Parse.streaming`).\n const text = tail ? source.replace(/[*_~`]+$/, '') : source\n if (depth > 8) return text ? [{ t: 'text', v: text }] : []\n const n = text.length\n const closeBracket = nextOf(text, (c) => c === 93)\n const closeParen = nextOf(text, (c) => c === 41)\n const newline = nextOf(text, (c) => c === 10)\n const space = nextOf(text, spaceCode)\n // Backtick runs by length, in order: a code span closes at the next run of the same length.\n const runs = new Map<number, number[]>()\n for (let i = 0; i < n;) {\n if (text[i] !== '`') {\n i++\n continue\n }\n let j = i\n while (j < n && text[j] === '`') j++\n const list = runs.get(j - i) ?? []\n list.push(i)\n runs.set(j - i, list)\n i = j\n }\n const cursor = new Map<number, number>()\n\n // A link to a workspace file - `[words](/path)`, `(</my file (1).md>)`, `(/a.md \"title\")` -\n // read from the lookups below, never by a scan from each `[`. Only a destination that can be\n // one is read (sliced, decoded, checked): a bare one holds no `(` and a `<…>` one no `<` - so it\n // holds no other link's address of its kind, and no character is read for more than two links\n // -, it is no longer than a path can be written, and the `[`s before one `](` share one\n // reading. `'open'`: the text ends inside such a link, as it may still be.\n let lookups: Record<'lt' | 'gt' | 'openParen' | 'dq' | 'sq' | 'solid', Int32Array> | undefined\n const look = () =>\n (lookups ??= {\n lt: nextOf(text, (c) => c === 60),\n gt: nextOf(text, (c) => c === 62),\n openParen: nextOf(text, (c) => c === 40),\n dq: nextOf(text, (c) => c === 34),\n sq: nextOf(text, (c) => c === 39),\n solid: nextOf(text, (c) => !spaceCode(c)),\n })\n type FileLink = { path: string; end: number } | 'open' | undefined\n let readAt = -1\n let read: FileLink\n const fileLinkAt = (close: number): FileLink => {\n if (close !== readAt) {\n readAt = close\n read = readFileLink(close + 2)\n }\n return read\n }\n const readFileLink = (start: number): FileLink => {\n const x = look()\n const line = newline[start]\n const p = x.solid[start]\n if (p >= n || p > line) return undefined\n let from: number\n let to: number\n let after: number\n if (text[p] === '<') {\n from = p + 1\n if (from < n && text[from] !== '/') return undefined\n to = x.gt[from]\n const bad = Math.min(x.lt[from], line)\n if (to >= n) return bad >= n && n - from <= DEST_MAX ? 'open' : undefined\n if (bad < to || to - from > DEST_MAX) return undefined\n after = to + 1\n } else if (text[p] === '/') {\n from = p\n if (text[p + 1] === '/') return undefined\n to = Math.min(space[p], closeParen[p])\n const bad = Math.min(x.lt[p], x.gt[p], x.openParen[p])\n if (to >= n) return bad >= n && n - from <= DEST_MAX ? 'open' : undefined\n if (bad < to || to - from > DEST_MAX) return undefined\n after = to\n } else return undefined\n // The destination is whole: a path, or not a link to a file at all.\n const path = workspacePathOf(decoded(text.slice(from, to)))\n if (!path) return undefined\n // Then blanks, an optional title, blanks, and `)` - each found by a lookup.\n let q = x.solid[after]\n if (q > line) return undefined\n if (q >= n) return 'open'\n if ((text[q] === '\"' || text[q] === \"'\") && q > after) {\n const shut = (text[q] === '\"' ? x.dq : x.sq)[q + 1]\n if (shut >= n) return line >= n && n - q <= TITLE_MAX ? 'open' : undefined\n if (shut > line || shut - q > TITLE_MAX) return undefined\n q = x.solid[shut + 1]\n if (q > line) return undefined\n if (q >= n) return 'open'\n }\n return text[q] === ')' ? { path, end: q } : undefined\n }\n\n const tokens: Token[] = []\n const flush = (from: number, to: number) => {\n // Text, with its line breaks.\n let at = from\n while (at < to) {\n const nl = Math.min(newline[at], to)\n if (nl > at) tokens.push({ t: 'text', v: text.slice(at, nl) })\n if (nl < to) tokens.push({ t: 'br' })\n at = nl + 1\n }\n }\n let plain = 0\n let i = 0\n while (i < n) {\n const c = text[i]\n if (c === '`') {\n let j = i\n while (j < n && text[j] === '`') j++\n const len = j - i\n const list = runs.get(len)!\n let k = cursor.get(len) ?? 0\n while (k < list.length && list[k] <= i) k++\n cursor.set(len, k)\n if (k < list.length) {\n flush(plain, i)\n const end = list[k]\n cursor.set(len, k + 1)\n const body = text.slice(j, end).replace(/\\n/g, ' ')\n const v = /^ .* $/s.test(body) && body.trim() ? body.slice(1, -1) : body\n // A span that is a workspace path, whole: a file the answer hands over. Only a closed one\n // - one still being written is no path yet -, and not in a link's words.\n const path = !words && v.includes('/') ? workspacePathOf(v) : undefined\n tokens.push(path ? { t: 'code', v, path } : { t: 'code', v })\n i = plain = end + len\n } else if (tail) {\n // Still being written: code up to where the text ends, as it will be when it closes.\n flush(plain, i)\n const body = text.slice(j).replace(/\\n/g, ' ')\n tokens.push({ t: 'code', v: body.startsWith(' ') && body.trim() ? body.slice(1) : body })\n i = plain = n\n } else i = j\n continue\n }\n if (c === '[') {\n const close = closeBracket[i + 1]\n if (close < n && close > i + 1 && close < newline[i] && text[close + 1] === '(') {\n const end = closeParen[close + 2]\n const href =\n end < n && space[close + 2] >= end ? safeHref(text.slice(close + 2, end)) : undefined\n if (href) {\n flush(plain, i)\n tokens.push({\n t: 'link',\n href,\n c: parseMarkdownInline(text.slice(i + 1, close), depth + 1, false, true),\n })\n i = plain = end + 1\n continue\n }\n // A link with no words (`[](/a.png)`) is text, as for any link. The `!` of an image stays\n // text too: dropped when the link completes, it would be text taken off the screen while\n // the answer streams.\n const file = fileLinkAt(close)\n if (file !== undefined && file !== 'open') {\n flush(plain, i)\n tokens.push({\n t: 'file',\n path: file.path,\n c: parseMarkdownInline(text.slice(i + 1, close), depth + 1, false, true),\n })\n i = plain = file.end + 1\n continue\n }\n if (tail && (file === 'open' || (end >= n && space[close + 2] >= n))) {\n // A link whose address (or title) is still being written: its words, as they will\n // read - never its `](`, address or title.\n flush(plain, i)\n tokens.push(...parseMarkdownInline(text.slice(i + 1, close), depth + 1, false, true))\n i = plain = n\n continue\n }\n }\n if (tail && newline[i] >= n && (close >= n || close === n - 1)) {\n // A bracket still open, or closed by the last character: it may be a link's text, so it\n // is drawn without its brackets until what follows says (`(` - a link - or anything else).\n flush(plain, i)\n if (close >= n) {\n i = plain = i + 1\n continue\n }\n tokens.push(...parseMarkdownInline(text.slice(i + 1, close), depth + 1))\n i = plain = n\n continue\n }\n i++\n continue\n }\n if (\n !words &&\n (c === 'h' || c === 'H') &&\n URL_START.test(text.slice(i, i + 8)) &&\n !isWord(text[i - 1])\n ) {\n let end = space[i]\n for (let k = i; k < end; k++) {\n if ('<>()'.includes(text[k])) {\n end = k\n break\n }\n }\n const url = text.slice(i, end).replace(URL_TAIL, '')\n if (url.length > 8) {\n flush(plain, i)\n tokens.push({ t: 'link', href: url, c: [{ t: 'text', v: url }] })\n i = plain = i + url.length\n continue\n }\n i++\n continue\n }\n if (c === '*' || c === '_' || c === '~') {\n let j = i\n while (j < n && text[j] === c) j++\n const before = text[i - 1]\n const after = text[j]\n let open = !isSpace(after)\n let close = !isSpace(before)\n // An underscore inside a word (snake_case) is not emphasis.\n if (c === '_' && isWord(before) && isWord(after)) open = close = false\n const len = j - i\n if ((c === '~' && len !== 2) || (!open && !close)) {\n i = j\n continue\n }\n flush(plain, i)\n // A run of three opens strong then em, and closes em then strong.\n const sizes: (1 | 2)[] =\n c === '~' ? [2] : len === 1 ? [1] : len === 2 ? [2] : open && !close ? [2, 1] : [1, 2]\n let used = 0\n for (const size of sizes) {\n tokens.push({ t: 'delim', ch: c, n: size, open, close, lead: !isWord(before) })\n used += size\n }\n if (len > used) tokens.push({ t: 'text', v: c.repeat(len - used) })\n i = plain = j\n continue\n }\n i++\n }\n flush(plain, n)\n\n // Emphasis: a closer takes the nearest opener of its kind; openers of other kinds above that\n // one can no longer close and become text. Each opener is pushed and popped once.\n const stacks: number[][] = [[], [], [], [], [], []]\n const kindOf = (tok: { ch: Delim; n: 1 | 2 }) =>\n (tok.ch === '*' ? 0 : tok.ch === '_' ? 2 : 4) + tok.n - 1\n for (let idx = 0; idx < tokens.length; idx++) {\n const tok = tokens[idx]\n if (tok.t !== 'delim') continue\n const own = stacks[kindOf(tok)]\n const top = own.length ? own[own.length - 1] : -1\n if (tok.close && top >= 0 && top < idx - 1) {\n own.pop()\n ;(tokens[top] as { match?: number }).match = idx\n tok.match = top\n for (const st of stacks) while (st.length && st[st.length - 1] > top) st.pop()\n continue\n }\n if (tok.open) own.push(idx)\n }\n if (tail) {\n // Emphasis still open where the text ends is emphasis already: it closes there, the\n // innermost first. (Openers inside a matched pair were dropped above; these nest.)\n const open = stacks.flat().sort((a, b) => b - a)\n for (const idx of open) {\n const opener = tokens[idx] as Token & { t: 'delim' }\n if (!opener.lead) continue\n opener.match = tokens.length\n tokens.push({ t: 'delim', ch: opener.ch, n: opener.n, open: false, close: true, match: idx })\n }\n }\n\n const root: MarkdownInline[] = []\n const frames: {\n out: MarkdownInline[]\n node?: MarkdownInline & { c: MarkdownInline[] }\n until: number\n }[] = [{ out: root, until: -1 }]\n tokens.forEach((tok, idx) => {\n const frame = frames[frames.length - 1]\n if (tok.t !== 'delim') {\n if (tok.t === 'text') pushText(frame.out, tok.v)\n else frame.out.push(tok)\n return\n }\n if (tok.match !== undefined && tok.match > idx) {\n const node = {\n t: tok.ch === '~' ? 'del' : tok.n === 2 ? 'strong' : 'em',\n c: [],\n } as MarkdownInline & { c: MarkdownInline[] }\n frames.push({ out: node.c, node, until: tok.match })\n return\n }\n if (tok.match !== undefined && frame.until === idx && frame.node) {\n frames.pop()\n frames[frames.length - 1].out.push(frame.node)\n return\n }\n pushText(frame.out, tok.ch.repeat(tok.n))\n })\n return root\n}\n\n// ── blocks ──────────────────────────────────────────────────────────────────\n\n// Any indentation: a model puts a code block under a list item with the item's indent (Gemini:\n// 8 spaces), and this scanner has no indented code blocks for it to be instead.\nconst FENCE = /^([ \\t]*)(`{3,}|~{3,})\\s*([\\w+#.-]*)/\nconst HEADING = /^ {0,3}(#{1,6})\\s+(.*?)\\s*#*\\s*$/\nconst HR = /^ {0,3}([-*_])(?:\\s*\\1){2,}\\s*$/\nconst BULLET = /^( *)([-*+])\\s+(.*)$/\nconst ORDERED = /^( *)(\\d{1,9})[.)]\\s+(.*)$/\nconst QUOTE = /^ {0,3}>\\s?(.*)$/\nconst TABLE_RULE = /^\\s*\\|?\\s*:?-{1,}:?\\s*(\\|\\s*:?-{1,}:?\\s*)*\\|?\\s*$/\n\n/**\n * The deepest a list nests (counting the quotes and lists around it): far past any answer's, and\n * the bound that keeps a text of ever-deeper items linear (`tests/markdown.test.ts`, \"time\").\n */\nconst LIST_NESTING_MAX = 8\n\nconst cells = (line: string): string[] => {\n let s = line.trim()\n if (s.startsWith('|')) s = s.slice(1)\n if (s.endsWith('|') && !s.endsWith('\\\\|')) s = s.slice(0, -1)\n const out: string[] = []\n let cur = ''\n for (let i = 0; i < s.length; i++) {\n if (s[i] === '\\\\' && s[i + 1] === '|') {\n cur += '|'\n i++\n } else if (s[i] === '|') {\n out.push(cur.trim())\n cur = ''\n } else cur += s[i]\n }\n out.push(cur.trim())\n return out\n}\n\nconst alignOf = (cell: string): MarkdownAlign => {\n const c = cell.trim()\n const left = c.startsWith(':')\n const right = c.endsWith(':')\n return left && right ? 'center' : right ? 'right' : left ? 'left' : undefined\n}\n\nconst startsBlock = (line: string, next: string | undefined): boolean =>\n FENCE.test(line) ||\n HEADING.test(line) ||\n HR.test(line) ||\n BULLET.test(line) ||\n ORDERED.test(line) ||\n QUOTE.test(line) ||\n (line.includes('|') && next !== undefined && TABLE_RULE.test(next) && next.includes('-'))\n\n/**\n * The blocks of a Markdown text. `streaming`: the text is an answer still being written, so\n * what ends at its end is drawn as it will be when it is finished (an inline construct still\n * open - `parseMarkdownInline`'s `tail`); `stream.ts` decides what of the last line is drawn at all.\n */\nexport const parseMarkdown = (text: string, depth = 0, streaming = false): MarkdownBlock[] => {\n const lines = text.replace(/\\r\\n?/g, '\\n').split('\\n')\n const out: MarkdownBlock[] = []\n let i = 0\n /** Whether line `k` is the last of a text still being written. */\n const last = (k: number) => streaming && k >= lines.length - 1\n while (i < lines.length) {\n const line = lines[i]\n if (!line.trim()) {\n i++\n continue\n }\n const fence = FENCE.exec(line)\n if (fence) {\n const body: string[] = []\n // The code's own indentation is kept; the fence's is taken off every line.\n const indent = new RegExp(`^[ \\\\t]{0,${fence[1].length}}`)\n i++\n while (i < lines.length && !lines[i].trimStart().startsWith(fence[2])) {\n body.push(lines[i++].replace(indent, ''))\n }\n i++\n out.push({ t: 'code', lang: fence[3], v: body.join('\\n') })\n continue\n }\n const heading = HEADING.exec(line)\n if (heading) {\n out.push({\n t: 'h',\n level: heading[1].length as 1 | 2 | 3 | 4 | 5 | 6,\n c: parseMarkdownInline(heading[2], 0, last(i)),\n })\n i++\n continue\n }\n if (HR.test(line)) {\n out.push({ t: 'hr' })\n i++\n continue\n }\n if (QUOTE.test(line) && depth < 4) {\n const body: string[] = []\n while (i < lines.length && QUOTE.test(lines[i])) body.push(QUOTE.exec(lines[i++])![1])\n out.push({\n t: 'quote',\n c: parseMarkdown(body.join('\\n'), depth + 1, streaming && i >= lines.length),\n })\n continue\n }\n const next = lines[i + 1]\n if (line.includes('|') && next !== undefined && TABLE_RULE.test(next) && next.includes('-')) {\n const head = cells(line)\n const align = cells(next).map(alignOf)\n i += 2\n const rows: MarkdownInline[][][] = []\n while (i < lines.length && lines[i].trim() && lines[i].includes('|')) {\n const row = cells(lines[i])\n // The cell being written is the last one that has anything in it.\n let open = -1\n if (last(i)) for (let k = 0; k < row.length; k++) if (row[k] !== '') open = k\n i++\n rows.push(head.map((_, k) => parseMarkdownInline(row[k] ?? '', 0, k === open)))\n }\n out.push({ t: 'table', align, head: head.map((h) => parseMarkdownInline(h)), rows })\n continue\n }\n if (BULLET.test(line) || ORDERED.test(line)) {\n const ordered = !BULLET.test(line)\n const first = (ordered ? ORDERED : BULLET).exec(line)!\n const indent = first[1].length\n const list: MarkdownListBlock = {\n t: 'list',\n ordered,\n start: ordered ? Number(first[2]) : 1,\n items: [],\n }\n while (i < lines.length) {\n const l = lines[i]\n const m = (ordered ? ORDERED : BULLET).exec(l)\n if (m && m[1].length <= indent + 1) {\n list.items.push({ c: parseMarkdownInline(m[3], 0, last(i)) })\n i++\n continue\n }\n const nested = BULLET.exec(l) ?? ORDERED.exec(l)\n if (nested && nested[1].length > indent + 1 && list.items.length) {\n if (depth >= LIST_NESTING_MAX) {\n // As deep as lists go: a deeper item is one more item of this list (never dropped).\n list.items.push({ c: parseMarkdownInline(nested[3], 0, last(i)) })\n i++\n continue\n }\n // The deeper items go under the item before them, a level at a time. Each level reads\n // the lines under it again, so the levels are bounded (`LIST_NESTING_MAX`): linear on\n // any staircase of indents.\n const subLines: string[] = []\n while (i < lines.length) {\n const s = BULLET.exec(lines[i]) ?? ORDERED.exec(lines[i])\n if (!s || s[1].length <= indent + 1) break\n subLines.push(lines[i++].slice(nested[1].length))\n }\n const sub = parseMarkdown(\n subLines.join('\\n'),\n depth + 1,\n streaming && i >= lines.length,\n ).find((b): b is MarkdownListBlock => b.t === 'list')\n if (sub) list.items[list.items.length - 1].sub = sub\n continue\n }\n // A continuation line of the item (indented, not blank, not a new block).\n if (\n l.trim() &&\n /^\\s+/.test(l) &&\n !startsBlock(l.trim(), lines[i + 1]) &&\n list.items.length\n ) {\n const item = list.items[list.items.length - 1]\n item.c.push({ t: 'br' }, ...parseMarkdownInline(l.trim(), 0, last(i)))\n i++\n continue\n }\n break\n }\n out.push(list)\n continue\n }\n // A paragraph: up to a blank line or the start of another block; line breaks are kept.\n const body: string[] = [line]\n i++\n while (i < lines.length && lines[i].trim() && !startsBlock(lines[i], lines[i + 1])) {\n body.push(lines[i++])\n }\n out.push({\n t: 'p',\n c: parseMarkdownInline(\n body.map((l) => l.trim()).join('\\n'),\n 0,\n streaming && i >= lines.length,\n ),\n })\n }\n return out\n}\n","import { parseMarkdown, type MarkdownBlock } from './parse.js'\n\n/**\n * An answer's Markdown WHILE IT IS WRITTEN (pure). The text grows a few characters at a time and\n * is drawn each time; drawn naively, what is on the screen keeps changing its kind - a `##`\n * that is a paragraph until its space arrives, a table's first row that is a line of pipes until\n * the rule under it, a fence that closes one backtick at a time, `**` that shows until its pair -\n * and each change moves everything under it. The rules, each pinned by `tests/markdown-stream.test.ts`:\n *\n * 1. A finished line is drawn as it will stay. Only the LAST line can be held back, and only\n * while it cannot be told what it is:\n * - it is nothing but marker characters so far (`#`, `-`, `1.`, `>`, `` ` ``, `|`, `*`, `---`);\n * - inside a code block, it may be the closing fence (or, still blank, its indent);\n * - it starts with `|` and may be a table's header (held with the rule line under it, until\n * that line is complete: then it is a table, or two lines of text).\n * 2. An inline construct open where the text ends is drawn as if it closed there (`parseMarkdownInline`'s\n * `tail`): emphasis and code at once, a link as its text until its address is whole; a run of\n * markers at the very end waits.\n * 3. Blocks before the last blank line (outside a code block) are finished: they are parsed once\n * and kept, as the same objects - so each piece costs the size of the last block, not of the\n * answer, and React leaves the finished blocks alone.\n *\n * What is held back is at most one short line (or a table's two head lines), for as long as it\n * takes the model to write it. A finished answer is parsed whole, without any of this.\n */\n\n/** A fence at any indentation, as `parse.ts` reads it. */\nconst FENCE = /^[ \\t]*(`{3,}|~{3,})/\n/** A line of marker characters only: it can still become a heading, a list, a rule, a fence… */\nconst MARKERS_ONLY = /^[\\s#>*+\\-_|`~=:\\d.)]{0,16}$/\n/** What a table's rule line looks like, whole or in part. */\nconst RULE_SO_FAR = /^\\s*\\|?[\\s:|-]*$/\n\nconst pipes = (line: string | undefined): boolean => line !== undefined && line.includes('|')\nconst blank = (line: string | undefined): boolean => line === undefined || !line.trim()\n\n/**\n * The part of a text still being written that is drawn now: `text` without what its last line\n * cannot yet be told to be (rule 1). Never longer than `text`; equal to it when nothing is held.\n */\nexport const markdownSettled = (text: string): string => {\n const lines = text.split('\\n')\n const n = lines.length\n // The code block the last line is in, if any (its fence).\n let fence: string | undefined\n for (let i = 0; i < n - 1; i++) {\n const line = lines[i]\n if (fence) {\n if (line.trimStart().startsWith(fence)) fence = undefined\n } else {\n const m = FENCE.exec(line)\n if (m) fence = m[1]\n }\n }\n const lastLine = lines[n - 1]\n const cut = (count: number) => lines.slice(0, n - count).join('\\n') + (n > count ? '\\n' : '')\n if (fence) {\n // The closing fence, one character at a time, is not a line of code - nor is its indent\n // (a fence under a list item is written 8 spaces in), so a line of blanks waits too.\n const t = lastLine.trim()\n return !t || fence.startsWith(t) ? cut(1) : text\n }\n // A table's header, and the rule line being written under it.\n const inTable = (k: number) => pipes(lines[k]) && !blank(lines[k])\n if (\n n >= 2 &&\n lines[n - 2].trimStart().startsWith('|') &&\n RULE_SO_FAR.test(lastLine) &&\n !inTable(n - 3)\n )\n return cut(2)\n if (lastLine.trimStart().startsWith('|') && !inTable(n - 2)) return cut(1)\n if (lastLine && MARKERS_ONLY.test(lastLine)) return cut(1)\n return text\n}\n\n/** Where the finished blocks of `text` end, looking from `from` (a block boundary): rule 3. */\nconst finishedEnd = (text: string, from: number): number => {\n let fence: string | undefined\n let end = from\n let at = from\n while (at < text.length) {\n const nl = text.indexOf('\\n', at)\n if (nl < 0) break\n const line = text.slice(at, nl)\n if (fence) {\n if (line.trimStart().startsWith(fence)) fence = undefined\n } else {\n const m = FENCE.exec(line)\n if (m) fence = m[1]\n // A blank line outside a code block ends every block before it.\n else if (!line.trim()) end = nl + 1\n }\n at = nl + 1\n }\n return end\n}\n\n/** The parse of a growing text so far: what is finished, and what was drawn last. */\nexport interface MarkdownStream {\n /** The text the finished blocks came from (a prefix of every later text). */\n done: string\n finished: MarkdownBlock[]\n blocks: MarkdownBlock[]\n}\n\nexport const emptyMarkdownStream = (): MarkdownStream => ({ done: '', finished: [], blocks: [] })\n\n/**\n * The blocks of `text`, an answer still being written, given the parse of what it was before.\n * A text that is not a continuation (another answer) starts over.\n */\nexport const parseMarkdownStreaming = (prev: MarkdownStream, text: string): MarkdownStream => {\n const source = text.includes('\\r') ? text.replace(/\\r\\n?/g, '\\n') : text\n const base = source.startsWith(prev.done) ? prev : emptyMarkdownStream()\n const end = finishedEnd(source, base.done.length)\n const finished =\n end > base.done.length\n ? [...base.finished, ...parseMarkdown(source.slice(base.done.length, end))]\n : base.finished\n // One line break at the end ends no block: the last line is still the last line.\n const tail = parseMarkdown(markdownSettled(source.slice(end)).replace(/\\n$/, ''), 0, true)\n return {\n done: source.slice(0, end),\n finished,\n blocks: tail.length ? [...finished, ...tail] : finished,\n }\n}\n","import { parseMarkdown, type MarkdownBlock, type MarkdownInline } from './parse.js'\n\n/**\n * What every renderer of the parsed tree needs and is not React: the parses cached by text, the\n * words of inline runs, and which table cells wrap. The React package's `<Markdown>` uses these;\n * a renderer of another front end (a web component, Vue, Svelte) should too, so the same answer\n * looks and reads the same everywhere.\n */\n\n/** How many parses {@link parseMarkdownCached} keeps (the oldest go first). */\nexport const MARKDOWN_CACHE_MAX = 64\n\nconst parsed = new Map<string, MarkdownBlock[]>()\n\n/**\n * The blocks of a FINISHED text, cached by the text: a saved chat opened again, or a re-render,\n * does not parse again. Bounded ({@link MARKDOWN_CACHE_MAX}); the parse itself is linear. The\n * blocks are shared between callers - never change them. A text still being written goes through\n * `parseMarkdownStreaming` instead (each piece would be an entry here).\n */\nexport const parseMarkdownCached = (text: string): MarkdownBlock[] => {\n const hit = parsed.get(text)\n if (hit) return hit\n const blocks = parseMarkdown(text)\n parsed.set(text, blocks)\n if (parsed.size > MARKDOWN_CACHE_MAX) parsed.delete(parsed.keys().next().value!)\n return blocks\n}\n\n/** The words of inline runs, as a screen reader is to name them (a link's or a file's words). */\nexport const markdownPlainText = (runs: MarkdownInline[]): string =>\n runs\n .map((r) =>\n r.t === 'text' || r.t === 'code' ? r.v : r.t === 'br' ? ' ' : markdownPlainText(r.c),\n )\n .join('')\n\n/**\n * A table cell longer than this many characters wraps, in a column of its own width; every other\n * cell is one line, and a table wider than the screen scrolls in its box. So a table's rows keep\n * their height when another row arrives while the answer is written (columns that share the\n * width would re-wrap each other's cells).\n */\nexport const MARKDOWN_LONG_CELL = 48\n\nconst lengthOf = (runs: MarkdownInline[]): number =>\n runs.reduce(\n (n, r) =>\n n + (r.t === 'text' || r.t === 'code' ? r.v.length : r.t === 'br' ? 1 : lengthOf(r.c)),\n 0,\n )\n\n/** Whether a table cell wraps (see {@link MARKDOWN_LONG_CELL}). */\nexport const markdownCellIsLong = (runs: MarkdownInline[]): boolean =>\n lengthOf(runs) > MARKDOWN_LONG_CELL\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAA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;AAGO,IAAM,oBAAoB,CAAC,SAChC,KACG;AAAA,EAAI,CAAC,MACJ,EAAE,MAAM,UAAU,EAAE,MAAM,SAAS,EAAE,IAAI,EAAE,MAAM,OAAO,MAAM,kBAAkB,EAAE,CAAC;AACrF,EACC,KAAK,EAAE;AAQL,IAAM,qBAAqB;AAElC,IAAM,WAAW,CAAC,SAChB,KAAK;AAAA,EACH,CAAC,GAAG,MACF,KAAK,EAAE,MAAM,UAAU,EAAE,MAAM,SAAS,EAAE,EAAE,SAAS,EAAE,MAAM,OAAO,IAAI,SAAS,EAAE,CAAC;AAAA,EACtF;AACF;AAGK,IAAM,qBAAqB,CAAC,SACjC,SAAS,IAAI,IAAI;","names":["PATH_MAX","i","body","FENCE"]}
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Markdown of an answer → blocks and inline runs (pure: no framework, no DOM). A renderer turns
3
+ * them into elements - the React package's `<Markdown>` (`@dudko.dev/agent-web-react/markdown`),
4
+ * or one of any other front end - never into HTML. What answers use: paragraphs with their line
5
+ * breaks, headings, lists (nested), task boxes as text, code blocks, quotes, rules, GFM tables,
6
+ * and inline **bold**, *italic*, ~~struck~~, `code`, links and bare URLs. Raw HTML stays text. A
7
+ * link is a link only for http(s) and mailto ({@link safeHref}) - anything else (`javascript:`,
8
+ * `data:`, `//host`) is its text -, or a FILE when its destination is a workspace path (bare or in
9
+ * `<…>`, with an optional title; `workspacePathOf`, after percent-decoding): `{ t: 'file' }`,
10
+ * which a renderer draws as a link-styled control that opens the file - only if the workspace has
11
+ * it, else as the link's words. A code span whose whole content is such a path carries it too
12
+ * (`path`), for the same control. A link's words hold no link of their own: no bare URL, no path.
13
+ * An image (`![a](…)`) is its `!` and a link (or a file): no picture is ever loaded from an answer.
14
+ *
15
+ * An answer is drawn while it is written: `stream.ts` has the rules for a text that is not
16
+ * finished (what is held back, what is drawn as if closed), so that nothing already on the
17
+ * screen changes its kind or its height when the rest arrives.
18
+ */
19
+ type MarkdownInline = {
20
+ t: 'text';
21
+ v: string;
22
+ } | {
23
+ t: 'br';
24
+ } | {
25
+ t: 'code';
26
+ v: string;
27
+ path?: string;
28
+ } | {
29
+ t: 'strong' | 'em' | 'del';
30
+ c: MarkdownInline[];
31
+ } | {
32
+ t: 'link';
33
+ href: string;
34
+ c: MarkdownInline[];
35
+ } | {
36
+ t: 'file';
37
+ path: string;
38
+ c: MarkdownInline[];
39
+ };
40
+ type MarkdownAlign = 'left' | 'center' | 'right' | undefined;
41
+ interface MarkdownListItem {
42
+ c: MarkdownInline[];
43
+ sub?: MarkdownListBlock;
44
+ }
45
+ interface MarkdownListBlock {
46
+ t: 'list';
47
+ ordered: boolean;
48
+ start: number;
49
+ items: MarkdownListItem[];
50
+ }
51
+ type MarkdownBlock = {
52
+ t: 'p';
53
+ c: MarkdownInline[];
54
+ } | {
55
+ t: 'h';
56
+ level: 1 | 2 | 3 | 4 | 5 | 6;
57
+ c: MarkdownInline[];
58
+ } | {
59
+ t: 'code';
60
+ lang: string;
61
+ v: string;
62
+ } | {
63
+ t: 'quote';
64
+ c: MarkdownBlock[];
65
+ } | {
66
+ t: 'hr';
67
+ } | MarkdownListBlock | {
68
+ t: 'table';
69
+ align: MarkdownAlign[];
70
+ head: MarkdownInline[][];
71
+ rows: MarkdownInline[][][];
72
+ };
73
+ /**
74
+ * The workspace path a link's destination (what is between its parentheses) names, or
75
+ * `undefined` - read exactly as an answer's link is (`parseMarkdownInline`'s file links): bare or in
76
+ * `<…>` (which may hold spaces and parentheses), with an optional `"title"` / `'title'` after
77
+ * it, percent-decoded BEFORE the check - `%2e%2e` is `..`, `%2F%2Fhost` is `//host`, `%5C` a
78
+ * backslash, all refused - and then the React package's `workspacePathOf`, the rules its file
79
+ * chips use (absolute, no `..` / `.` / empty segment, no `//host`, no backslash or control
80
+ * character, never normalised).
81
+ */
82
+ declare const workspaceLinkPath: (raw: string) => string | undefined;
83
+ /**
84
+ * The URL a link may point to, or `undefined`: `http://`, `https://` or `mailto:` (any case), then
85
+ * no blank at all - as the text holds it, never decoded (`&#106;avascript:`, `java%73cript:` and
86
+ * `java\tscript:` are no scheme of these, so text). Nothing relative or protocol-relative
87
+ * (`/a`, `//host`), no `javascript:`, `data:`, `vbscript:`, `file:` or any other scheme. Blanks
88
+ * around it are dropped.
89
+ */
90
+ declare const safeHref: (url: string) => string | undefined;
91
+ /**
92
+ * Inline runs of one paragraph, heading, list item or table cell. A single scan, linear in the
93
+ * text (no backtracking regex: a long line of unmatched `**`, `_`, backticks or brackets costs
94
+ * the same as plain text): code spans first, then links and bare URLs, then emphasis matched
95
+ * with one opener stack per kind of delimiter. `words`: the words of a link, where nothing is a
96
+ * link again - a bare URL is text, a code span is no path.
97
+ */
98
+ declare const parseMarkdownInline: (source: string, depth?: number, tail?: boolean, words?: boolean) => MarkdownInline[];
99
+ /**
100
+ * The blocks of a Markdown text. `streaming`: the text is an answer still being written, so
101
+ * what ends at its end is drawn as it will be when it is finished (an inline construct still
102
+ * open - `parseMarkdownInline`'s `tail`); `stream.ts` decides what of the last line is drawn at all.
103
+ */
104
+ declare const parseMarkdown: (text: string, depth?: number, streaming?: boolean) => MarkdownBlock[];
105
+
106
+ /**
107
+ * The part of a text still being written that is drawn now: `text` without what its last line
108
+ * cannot yet be told to be (rule 1). Never longer than `text`; equal to it when nothing is held.
109
+ */
110
+ declare const markdownSettled: (text: string) => string;
111
+ /** The parse of a growing text so far: what is finished, and what was drawn last. */
112
+ interface MarkdownStream {
113
+ /** The text the finished blocks came from (a prefix of every later text). */
114
+ done: string;
115
+ finished: MarkdownBlock[];
116
+ blocks: MarkdownBlock[];
117
+ }
118
+ declare const emptyMarkdownStream: () => MarkdownStream;
119
+ /**
120
+ * The blocks of `text`, an answer still being written, given the parse of what it was before.
121
+ * A text that is not a continuation (another answer) starts over.
122
+ */
123
+ declare const parseMarkdownStreaming: (prev: MarkdownStream, text: string) => MarkdownStream;
124
+
125
+ /**
126
+ * What every renderer of the parsed tree needs and is not React: the parses cached by text, the
127
+ * words of inline runs, and which table cells wrap. The React package's `<Markdown>` uses these;
128
+ * a renderer of another front end (a web component, Vue, Svelte) should too, so the same answer
129
+ * looks and reads the same everywhere.
130
+ */
131
+ /** How many parses {@link parseMarkdownCached} keeps (the oldest go first). */
132
+ declare const MARKDOWN_CACHE_MAX = 64;
133
+ /**
134
+ * The blocks of a FINISHED text, cached by the text: a saved chat opened again, or a re-render,
135
+ * does not parse again. Bounded ({@link MARKDOWN_CACHE_MAX}); the parse itself is linear. The
136
+ * blocks are shared between callers - never change them. A text still being written goes through
137
+ * `parseMarkdownStreaming` instead (each piece would be an entry here).
138
+ */
139
+ declare const parseMarkdownCached: (text: string) => MarkdownBlock[];
140
+ /** The words of inline runs, as a screen reader is to name them (a link's or a file's words). */
141
+ declare const markdownPlainText: (runs: MarkdownInline[]) => string;
142
+ /**
143
+ * A table cell longer than this many characters wraps, in a column of its own width; every other
144
+ * cell is one line, and a table wider than the screen scrolls in its box. So a table's rows keep
145
+ * their height when another row arrives while the answer is written (columns that share the
146
+ * width would re-wrap each other's cells).
147
+ */
148
+ declare const MARKDOWN_LONG_CELL = 48;
149
+ /** Whether a table cell wraps (see {@link MARKDOWN_LONG_CELL}). */
150
+ declare const markdownCellIsLong: (runs: MarkdownInline[]) => boolean;
151
+
152
+ export { MARKDOWN_CACHE_MAX, MARKDOWN_LONG_CELL, emptyMarkdownStream, markdownCellIsLong, markdownPlainText, markdownSettled, parseMarkdown, parseMarkdownCached, parseMarkdownInline, parseMarkdownStreaming, safeHref, workspaceLinkPath };
153
+ export type { MarkdownAlign, MarkdownBlock, MarkdownInline, MarkdownListBlock, MarkdownListItem, MarkdownStream };
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Markdown of an answer → blocks and inline runs (pure: no framework, no DOM). A renderer turns
3
+ * them into elements - the React package's `<Markdown>` (`@dudko.dev/agent-web-react/markdown`),
4
+ * or one of any other front end - never into HTML. What answers use: paragraphs with their line
5
+ * breaks, headings, lists (nested), task boxes as text, code blocks, quotes, rules, GFM tables,
6
+ * and inline **bold**, *italic*, ~~struck~~, `code`, links and bare URLs. Raw HTML stays text. A
7
+ * link is a link only for http(s) and mailto ({@link safeHref}) - anything else (`javascript:`,
8
+ * `data:`, `//host`) is its text -, or a FILE when its destination is a workspace path (bare or in
9
+ * `<…>`, with an optional title; `workspacePathOf`, after percent-decoding): `{ t: 'file' }`,
10
+ * which a renderer draws as a link-styled control that opens the file - only if the workspace has
11
+ * it, else as the link's words. A code span whose whole content is such a path carries it too
12
+ * (`path`), for the same control. A link's words hold no link of their own: no bare URL, no path.
13
+ * An image (`![a](…)`) is its `!` and a link (or a file): no picture is ever loaded from an answer.
14
+ *
15
+ * An answer is drawn while it is written: `stream.ts` has the rules for a text that is not
16
+ * finished (what is held back, what is drawn as if closed), so that nothing already on the
17
+ * screen changes its kind or its height when the rest arrives.
18
+ */
19
+ type MarkdownInline = {
20
+ t: 'text';
21
+ v: string;
22
+ } | {
23
+ t: 'br';
24
+ } | {
25
+ t: 'code';
26
+ v: string;
27
+ path?: string;
28
+ } | {
29
+ t: 'strong' | 'em' | 'del';
30
+ c: MarkdownInline[];
31
+ } | {
32
+ t: 'link';
33
+ href: string;
34
+ c: MarkdownInline[];
35
+ } | {
36
+ t: 'file';
37
+ path: string;
38
+ c: MarkdownInline[];
39
+ };
40
+ type MarkdownAlign = 'left' | 'center' | 'right' | undefined;
41
+ interface MarkdownListItem {
42
+ c: MarkdownInline[];
43
+ sub?: MarkdownListBlock;
44
+ }
45
+ interface MarkdownListBlock {
46
+ t: 'list';
47
+ ordered: boolean;
48
+ start: number;
49
+ items: MarkdownListItem[];
50
+ }
51
+ type MarkdownBlock = {
52
+ t: 'p';
53
+ c: MarkdownInline[];
54
+ } | {
55
+ t: 'h';
56
+ level: 1 | 2 | 3 | 4 | 5 | 6;
57
+ c: MarkdownInline[];
58
+ } | {
59
+ t: 'code';
60
+ lang: string;
61
+ v: string;
62
+ } | {
63
+ t: 'quote';
64
+ c: MarkdownBlock[];
65
+ } | {
66
+ t: 'hr';
67
+ } | MarkdownListBlock | {
68
+ t: 'table';
69
+ align: MarkdownAlign[];
70
+ head: MarkdownInline[][];
71
+ rows: MarkdownInline[][][];
72
+ };
73
+ /**
74
+ * The workspace path a link's destination (what is between its parentheses) names, or
75
+ * `undefined` - read exactly as an answer's link is (`parseMarkdownInline`'s file links): bare or in
76
+ * `<…>` (which may hold spaces and parentheses), with an optional `"title"` / `'title'` after
77
+ * it, percent-decoded BEFORE the check - `%2e%2e` is `..`, `%2F%2Fhost` is `//host`, `%5C` a
78
+ * backslash, all refused - and then the React package's `workspacePathOf`, the rules its file
79
+ * chips use (absolute, no `..` / `.` / empty segment, no `//host`, no backslash or control
80
+ * character, never normalised).
81
+ */
82
+ declare const workspaceLinkPath: (raw: string) => string | undefined;
83
+ /**
84
+ * The URL a link may point to, or `undefined`: `http://`, `https://` or `mailto:` (any case), then
85
+ * no blank at all - as the text holds it, never decoded (`&#106;avascript:`, `java%73cript:` and
86
+ * `java\tscript:` are no scheme of these, so text). Nothing relative or protocol-relative
87
+ * (`/a`, `//host`), no `javascript:`, `data:`, `vbscript:`, `file:` or any other scheme. Blanks
88
+ * around it are dropped.
89
+ */
90
+ declare const safeHref: (url: string) => string | undefined;
91
+ /**
92
+ * Inline runs of one paragraph, heading, list item or table cell. A single scan, linear in the
93
+ * text (no backtracking regex: a long line of unmatched `**`, `_`, backticks or brackets costs
94
+ * the same as plain text): code spans first, then links and bare URLs, then emphasis matched
95
+ * with one opener stack per kind of delimiter. `words`: the words of a link, where nothing is a
96
+ * link again - a bare URL is text, a code span is no path.
97
+ */
98
+ declare const parseMarkdownInline: (source: string, depth?: number, tail?: boolean, words?: boolean) => MarkdownInline[];
99
+ /**
100
+ * The blocks of a Markdown text. `streaming`: the text is an answer still being written, so
101
+ * what ends at its end is drawn as it will be when it is finished (an inline construct still
102
+ * open - `parseMarkdownInline`'s `tail`); `stream.ts` decides what of the last line is drawn at all.
103
+ */
104
+ declare const parseMarkdown: (text: string, depth?: number, streaming?: boolean) => MarkdownBlock[];
105
+
106
+ /**
107
+ * The part of a text still being written that is drawn now: `text` without what its last line
108
+ * cannot yet be told to be (rule 1). Never longer than `text`; equal to it when nothing is held.
109
+ */
110
+ declare const markdownSettled: (text: string) => string;
111
+ /** The parse of a growing text so far: what is finished, and what was drawn last. */
112
+ interface MarkdownStream {
113
+ /** The text the finished blocks came from (a prefix of every later text). */
114
+ done: string;
115
+ finished: MarkdownBlock[];
116
+ blocks: MarkdownBlock[];
117
+ }
118
+ declare const emptyMarkdownStream: () => MarkdownStream;
119
+ /**
120
+ * The blocks of `text`, an answer still being written, given the parse of what it was before.
121
+ * A text that is not a continuation (another answer) starts over.
122
+ */
123
+ declare const parseMarkdownStreaming: (prev: MarkdownStream, text: string) => MarkdownStream;
124
+
125
+ /**
126
+ * What every renderer of the parsed tree needs and is not React: the parses cached by text, the
127
+ * words of inline runs, and which table cells wrap. The React package's `<Markdown>` uses these;
128
+ * a renderer of another front end (a web component, Vue, Svelte) should too, so the same answer
129
+ * looks and reads the same everywhere.
130
+ */
131
+ /** How many parses {@link parseMarkdownCached} keeps (the oldest go first). */
132
+ declare const MARKDOWN_CACHE_MAX = 64;
133
+ /**
134
+ * The blocks of a FINISHED text, cached by the text: a saved chat opened again, or a re-render,
135
+ * does not parse again. Bounded ({@link MARKDOWN_CACHE_MAX}); the parse itself is linear. The
136
+ * blocks are shared between callers - never change them. A text still being written goes through
137
+ * `parseMarkdownStreaming` instead (each piece would be an entry here).
138
+ */
139
+ declare const parseMarkdownCached: (text: string) => MarkdownBlock[];
140
+ /** The words of inline runs, as a screen reader is to name them (a link's or a file's words). */
141
+ declare const markdownPlainText: (runs: MarkdownInline[]) => string;
142
+ /**
143
+ * A table cell longer than this many characters wraps, in a column of its own width; every other
144
+ * cell is one line, and a table wider than the screen scrolls in its box. So a table's rows keep
145
+ * their height when another row arrives while the answer is written (columns that share the
146
+ * width would re-wrap each other's cells).
147
+ */
148
+ declare const MARKDOWN_LONG_CELL = 48;
149
+ /** Whether a table cell wraps (see {@link MARKDOWN_LONG_CELL}). */
150
+ declare const markdownCellIsLong: (runs: MarkdownInline[]) => boolean;
151
+
152
+ export { MARKDOWN_CACHE_MAX, MARKDOWN_LONG_CELL, emptyMarkdownStream, markdownCellIsLong, markdownPlainText, markdownSettled, parseMarkdown, parseMarkdownCached, parseMarkdownInline, parseMarkdownStreaming, safeHref, workspaceLinkPath };
153
+ export type { MarkdownAlign, MarkdownBlock, MarkdownInline, MarkdownListBlock, MarkdownListItem, MarkdownStream };