@linxiraos/pi-tui 1.1.27 → 1.2.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 (376) hide show
  1. package/CHANGELOG.md +50 -461
  2. package/README.md +7 -5
  3. package/dist/types/app-keybindings.d.ts +1 -1
  4. package/dist/types/apps/cleanse-board.d.ts +1 -1
  5. package/dist/types/apps/debug/log-viewer.d.ts +4 -0
  6. package/dist/types/apps/debug/raw-sse-buffer.d.ts +2 -0
  7. package/dist/types/apps/git/git-tui.d.ts +2 -2
  8. package/dist/types/apps/git/help.d.ts +3 -1
  9. package/dist/types/apps/if-bench-board.d.ts +2 -2
  10. package/dist/types/apps/setup-model-picker.d.ts +1 -1
  11. package/dist/types/autocomplete.d.ts +5 -2
  12. package/dist/types/charts/chart-plan.d.ts +674 -0
  13. package/dist/types/charts/chart-svg.d.ts +59 -0
  14. package/dist/types/charts/table-data.d.ts +175 -0
  15. package/dist/types/chat/assistant-message.d.ts +5 -0
  16. package/dist/types/chat/chat-transcript-builder.d.ts +2 -0
  17. package/dist/types/chat/compaction-summary-message.d.ts +1 -0
  18. package/dist/types/chat/execution-shared.d.ts +6 -5
  19. package/dist/types/chat/fence-figure.d.ts +42 -0
  20. package/dist/types/chat/figure-markdown.d.ts +42 -0
  21. package/dist/types/chat/messages.d.ts +1 -1
  22. package/dist/types/chat/svg-figure.d.ts +33 -0
  23. package/dist/types/chat/svg-source.d.ts +49 -0
  24. package/dist/types/chat/table-chart.d.ts +89 -0
  25. package/dist/types/chat/tool-execution.d.ts +2 -0
  26. package/dist/types/chat/user-message.d.ts +15 -2
  27. package/dist/types/chrome/shared.d.ts +9 -0
  28. package/dist/types/chrome/transcript-container.d.ts +6 -0
  29. package/dist/types/components/box.d.ts +1 -0
  30. package/dist/types/components/disclosure.d.ts +1 -0
  31. package/dist/types/components/editor.d.ts +15 -4
  32. package/dist/types/components/form.d.ts +1 -0
  33. package/dist/types/components/image.d.ts +12 -44
  34. package/dist/types/components/key-value-list.d.ts +1 -0
  35. package/dist/types/components/layout/row.d.ts +1 -0
  36. package/dist/types/components/layout/stack.d.ts +1 -0
  37. package/dist/types/components/markdown.d.ts +36 -3
  38. package/dist/types/components/scroll-view.d.ts +1 -0
  39. package/dist/types/components/section.d.ts +1 -0
  40. package/dist/types/components/table.d.ts +1 -0
  41. package/dist/types/components/text.d.ts +1 -0
  42. package/dist/types/components/tree-view.d.ts +7 -0
  43. package/dist/types/components/truncated-text.d.ts +1 -0
  44. package/dist/types/fuzzy.d.ts +29 -1
  45. package/dist/types/key-hint-format.d.ts +1 -1
  46. package/dist/types/keybinding-matchers.d.ts +3 -0
  47. package/dist/types/keybindings.d.ts +1 -1
  48. package/dist/types/kitty-graphics.d.ts +2 -2
  49. package/dist/types/loop-watchdog.d.ts +8 -7
  50. package/dist/types/multiplexers/cmux.d.ts +16 -0
  51. package/dist/types/multiplexers/herdr.d.ts +26 -0
  52. package/dist/types/multiplexers/notify.d.ts +13 -0
  53. package/dist/types/multiplexers/orca.d.ts +12 -0
  54. package/dist/types/multiplexers/screen.d.ts +9 -0
  55. package/dist/types/multiplexers/tmux.d.ts +29 -0
  56. package/dist/types/multiplexers/types.d.ts +100 -0
  57. package/dist/types/multiplexers/wmux.d.ts +12 -0
  58. package/dist/types/multiplexers/zellij.d.ts +12 -0
  59. package/dist/types/native/backend.d.ts +3 -2
  60. package/dist/types/native/blobs.d.ts +7 -3
  61. package/dist/types/native/encode.d.ts +3 -4
  62. package/dist/types/native/overlay.d.ts +5 -0
  63. package/dist/types/overlays/agent-hub-projection.d.ts +6 -5
  64. package/dist/types/overlays/agent-hub-types.d.ts +9 -0
  65. package/dist/types/overlays/agent-hub.d.ts +2 -0
  66. package/dist/types/overlays/agent-transcript-viewer.d.ts +11 -2
  67. package/dist/types/overlays/agents-hub.d.ts +9 -0
  68. package/dist/types/overlays/annotation-overlay.d.ts +19 -2
  69. package/dist/types/overlays/annotation-types.d.ts +2 -0
  70. package/dist/types/overlays/ask-dialog.d.ts +5 -2
  71. package/dist/types/overlays/history-search.d.ts +10 -0
  72. package/dist/types/overlays/hook-selector.d.ts +9 -4
  73. package/dist/types/overlays/jobs-panel.d.ts +1 -7
  74. package/dist/types/overlays/model-browser.d.ts +54 -7
  75. package/dist/types/overlays/model-hub.d.ts +23 -0
  76. package/dist/types/overlays/oauth-selector.d.ts +0 -1
  77. package/dist/types/overlays/plan-review-overlay.d.ts +3 -2
  78. package/dist/types/overlays/session-observer-registry.d.ts +2 -0
  79. package/dist/types/overlays/session-selector.d.ts +6 -2
  80. package/dist/types/overlays/settings-defs.d.ts +1 -0
  81. package/dist/types/overlays/usage-dashboard.d.ts +8 -4
  82. package/dist/types/overlays/usage-display.d.ts +3 -1
  83. package/dist/types/prompt/composer.d.ts +2 -7
  84. package/dist/types/prompt/custom-editor.d.ts +11 -8
  85. package/dist/types/prompt/image-source.d.ts +2 -1
  86. package/dist/types/prompt/interactive-context-helpers.d.ts +4 -1
  87. package/dist/types/prompt/model-mention-autocomplete.d.ts +1 -1
  88. package/dist/types/prompt/welcome.d.ts +19 -42
  89. package/dist/types/render/composite.d.ts +2 -0
  90. package/dist/types/render/output-pane.d.ts +1 -0
  91. package/dist/types/render/render-utils.d.ts +1 -0
  92. package/dist/types/render/tool-card.d.ts +6 -0
  93. package/dist/types/render/width-aware-text.d.ts +2 -0
  94. package/dist/types/setup/scenes/splash.d.ts +1 -1
  95. package/dist/types/setup/scenes/types.d.ts +1 -0
  96. package/dist/types/status-line/component.d.ts +3 -0
  97. package/dist/types/status-line/host.d.ts +4 -1
  98. package/dist/types/status-line/segments.d.ts +2 -0
  99. package/dist/types/status-line/types.d.ts +14 -3
  100. package/dist/types/terminal-capabilities.d.ts +27 -35
  101. package/dist/types/terminal-multiplexer.d.ts +127 -14
  102. package/dist/types/terminal.d.ts +22 -6
  103. package/dist/types/theme/active-symbols.d.ts +1 -1
  104. package/dist/types/theme/schema.d.ts +11 -2
  105. package/dist/types/theme/symbols.d.ts +1 -1
  106. package/dist/types/theme/theme-class.d.ts +7 -0
  107. package/dist/types/theme/theme.d.ts +1 -1
  108. package/dist/types/theme/tui-adapters.d.ts +3 -0
  109. package/dist/types/tmux.d.ts +11 -4
  110. package/dist/types/tools/daemon.d.ts +1 -1
  111. package/dist/types/tools/eval.d.ts +22 -3
  112. package/dist/types/tools/glob.d.ts +4 -0
  113. package/dist/types/tools/native-view.d.ts +47 -0
  114. package/dist/types/tools/output-meta.d.ts +11 -1
  115. package/dist/types/tools/renderer.d.ts +37 -2
  116. package/dist/types/tools/streaming-output.d.ts +20 -1
  117. package/dist/types/tools/task.d.ts +0 -4
  118. package/dist/types/tools/write.d.ts +9 -2
  119. package/dist/types/tools/yield.d.ts +22 -0
  120. package/dist/types/tui.d.ts +31 -0
  121. package/dist/types/utils.d.ts +6 -0
  122. package/dist/types/vim.d.ts +19 -5
  123. package/package.json +13 -9
  124. package/src/app-keybindings.d.ts +1 -1
  125. package/src/app-keybindings.ts +1 -1
  126. package/src/apps/autoresearch-dashboard.ts +7 -3
  127. package/src/apps/cleanse-board.ts +2 -2
  128. package/src/apps/cleanse-picker.ts +1 -1
  129. package/src/apps/debug/log-viewer.ts +188 -72
  130. package/src/apps/debug/raw-sse-buffer.ts +8 -0
  131. package/src/apps/debug/raw-sse.ts +39 -5
  132. package/src/apps/git/diff-pane.ts +26 -5
  133. package/src/apps/git/git-tui.ts +2 -2
  134. package/src/apps/git/help.ts +29 -20
  135. package/src/apps/if-bench-board.ts +2 -2
  136. package/src/apps/ps-top.ts +2 -2
  137. package/src/apps/setup-model-picker.ts +1 -1
  138. package/src/autocomplete.d.ts +5 -2
  139. package/src/autocomplete.ts +67 -43
  140. package/src/charts/chart-plan.d.ts +674 -0
  141. package/src/charts/chart-plan.ts +5081 -0
  142. package/src/charts/chart-svg.d.ts +59 -0
  143. package/src/charts/chart-svg.ts +4657 -0
  144. package/src/charts/table-data.d.ts +175 -0
  145. package/src/charts/table-data.ts +1478 -0
  146. package/src/chat/advisor-message.ts +11 -0
  147. package/src/chat/assistant-message.d.ts +5 -0
  148. package/src/chat/assistant-message.ts +219 -50
  149. package/src/chat/chat-transcript-builder.d.ts +2 -0
  150. package/src/chat/chat-transcript-builder.ts +5 -3
  151. package/src/chat/compaction-summary-message.d.ts +1 -0
  152. package/src/chat/compaction-summary-message.ts +8 -0
  153. package/src/chat/execution-shared.d.ts +6 -5
  154. package/src/chat/execution-shared.ts +42 -25
  155. package/src/chat/fence-figure.d.ts +42 -0
  156. package/src/chat/fence-figure.ts +80 -0
  157. package/src/chat/figure-markdown.d.ts +42 -0
  158. package/src/chat/figure-markdown.ts +174 -0
  159. package/src/chat/messages.d.ts +1 -1
  160. package/src/chat/messages.ts +1 -1
  161. package/src/chat/read-tool-group.ts +6 -4
  162. package/src/chat/skill-message.ts +6 -0
  163. package/src/chat/svg-figure.d.ts +33 -0
  164. package/src/chat/svg-figure.ts +399 -0
  165. package/src/chat/svg-source.d.ts +49 -0
  166. package/src/chat/svg-source.ts +188 -0
  167. package/src/chat/table-chart.d.ts +89 -0
  168. package/src/chat/table-chart.ts +294 -0
  169. package/src/chat/tool-execution.d.ts +2 -0
  170. package/src/chat/tool-execution.ts +80 -6
  171. package/src/chat/user-message.d.ts +15 -2
  172. package/src/chat/user-message.ts +55 -3
  173. package/src/chrome/shared.ts +53 -0
  174. package/src/chrome/transcript-container.d.ts +6 -0
  175. package/src/chrome/transcript-container.ts +93 -13
  176. package/src/components/box.d.ts +1 -0
  177. package/src/components/box.ts +5 -0
  178. package/src/components/disclosure.d.ts +1 -0
  179. package/src/components/disclosure.ts +8 -0
  180. package/src/components/editor.d.ts +15 -4
  181. package/src/components/editor.ts +294 -46
  182. package/src/components/form.d.ts +1 -0
  183. package/src/components/form.ts +8 -0
  184. package/src/components/image.d.ts +12 -44
  185. package/src/components/image.ts +47 -151
  186. package/src/components/key-value-list.d.ts +1 -0
  187. package/src/components/key-value-list.ts +4 -0
  188. package/src/components/layout/row.d.ts +1 -0
  189. package/src/components/layout/row.ts +7 -0
  190. package/src/components/layout/stack.d.ts +1 -0
  191. package/src/components/layout/stack.ts +7 -0
  192. package/src/components/markdown.d.ts +36 -3
  193. package/src/components/markdown.ts +433 -318
  194. package/src/components/scroll-view.d.ts +1 -0
  195. package/src/components/scroll-view.ts +5 -0
  196. package/src/components/section.d.ts +1 -0
  197. package/src/components/section.ts +5 -0
  198. package/src/components/settings-list.ts +2 -0
  199. package/src/components/table.d.ts +1 -0
  200. package/src/components/table.ts +4 -0
  201. package/src/components/text.d.ts +1 -0
  202. package/src/components/text.ts +7 -0
  203. package/src/components/tree-view.d.ts +7 -0
  204. package/src/components/tree-view.ts +38 -8
  205. package/src/components/truncated-text.d.ts +1 -0
  206. package/src/components/truncated-text.ts +5 -0
  207. package/src/desktop-notify.ts +1 -1
  208. package/src/fuzzy.d.ts +29 -1
  209. package/src/fuzzy.ts +115 -30
  210. package/src/key-hint-format.d.ts +1 -1
  211. package/src/key-hint-format.ts +1 -1
  212. package/src/keybinding-matchers.d.ts +3 -0
  213. package/src/keybinding-matchers.ts +6 -2
  214. package/src/keybindings.d.ts +1 -1
  215. package/src/keybindings.ts +1 -1
  216. package/src/kitty-graphics.d.ts +2 -2
  217. package/src/kitty-graphics.ts +4 -4
  218. package/src/loop-watchdog.d.ts +8 -7
  219. package/src/loop-watchdog.ts +18 -13
  220. package/src/multiplexers/cmux.d.ts +16 -0
  221. package/src/multiplexers/cmux.ts +29 -0
  222. package/src/multiplexers/herdr.d.ts +26 -0
  223. package/src/multiplexers/herdr.ts +64 -0
  224. package/src/multiplexers/notify.d.ts +13 -0
  225. package/src/multiplexers/notify.ts +24 -0
  226. package/src/multiplexers/orca.d.ts +12 -0
  227. package/src/multiplexers/orca.ts +16 -0
  228. package/src/multiplexers/screen.d.ts +9 -0
  229. package/src/multiplexers/screen.ts +15 -0
  230. package/src/multiplexers/tmux.d.ts +29 -0
  231. package/src/multiplexers/tmux.ts +40 -0
  232. package/src/multiplexers/types.d.ts +100 -0
  233. package/src/multiplexers/types.ts +110 -0
  234. package/src/multiplexers/wmux.d.ts +12 -0
  235. package/src/multiplexers/wmux.ts +16 -0
  236. package/src/multiplexers/zellij.d.ts +12 -0
  237. package/src/multiplexers/zellij.ts +24 -0
  238. package/src/native/backend.d.ts +3 -2
  239. package/src/native/backend.ts +174 -18
  240. package/src/native/blobs.d.ts +7 -3
  241. package/src/native/blobs.ts +47 -12
  242. package/src/native/encode.d.ts +3 -4
  243. package/src/native/encode.ts +58 -31
  244. package/src/native/overlay.d.ts +5 -0
  245. package/src/native/overlay.ts +13 -0
  246. package/src/native/reconcile.ts +4 -0
  247. package/src/overlays/agent-hub-projection.d.ts +6 -5
  248. package/src/overlays/agent-hub-projection.ts +124 -13
  249. package/src/overlays/agent-hub-types.d.ts +9 -0
  250. package/src/overlays/agent-hub-types.ts +6 -0
  251. package/src/overlays/agent-hub.ts +46 -31
  252. package/src/overlays/agent-transcript-viewer.ts +91 -23
  253. package/src/overlays/agents-hub.ts +28 -8
  254. package/src/overlays/annotation-overlay.ts +293 -90
  255. package/src/overlays/annotation-types.ts +2 -0
  256. package/src/overlays/ask-dialog.ts +7 -10
  257. package/src/overlays/btw-history-panel.ts +12 -1
  258. package/src/overlays/copy-selector.ts +30 -8
  259. package/src/overlays/extensions/state-manager.ts +52 -12
  260. package/src/overlays/history-search.ts +100 -7
  261. package/src/overlays/hook-selector.ts +12 -5
  262. package/src/overlays/jobs-panel.ts +28 -46
  263. package/src/overlays/model-browser.ts +341 -110
  264. package/src/overlays/model-hub.ts +300 -49
  265. package/src/overlays/move-overlay.ts +28 -14
  266. package/src/overlays/oauth-selector.ts +54 -70
  267. package/src/overlays/plan-review-overlay.ts +194 -70
  268. package/src/overlays/plugin-settings.ts +11 -5
  269. package/src/overlays/report-panel.ts +2 -2
  270. package/src/overlays/rewind-selector.ts +86 -17
  271. package/src/overlays/session-observer-registry.d.ts +2 -0
  272. package/src/overlays/session-observer-registry.ts +10 -2
  273. package/src/overlays/session-selector.ts +64 -26
  274. package/src/overlays/settings-defs.ts +4 -3
  275. package/src/overlays/settings-selector.ts +33 -8
  276. package/src/overlays/snapcompact-shape-preview.ts +2 -3
  277. package/src/overlays/stats-notice.ts +1 -1
  278. package/src/overlays/tree-selector.ts +41 -20
  279. package/src/overlays/usage-dashboard.ts +100 -50
  280. package/src/overlays/usage-display.ts +10 -4
  281. package/src/prompt/attachment-chips.ts +15 -11
  282. package/src/prompt/composer-attachments.ts +6 -4
  283. package/src/prompt/composer-cache.ts +4 -6
  284. package/src/prompt/composer.ts +6 -37
  285. package/src/prompt/custom-editor.ts +54 -26
  286. package/src/prompt/image-source.ts +2 -1
  287. package/src/prompt/interactive-context-helpers.ts +30 -18
  288. package/src/prompt/macos-spelling.ts +7 -0
  289. package/src/prompt/magic-keywords.ts +11 -2
  290. package/src/prompt/model-mention-autocomplete.ts +14 -14
  291. package/src/prompt/tips.txt +6 -5
  292. package/src/prompt/welcome.ts +235 -411
  293. package/src/prompt/word-completion.ts +38 -5
  294. package/src/render/composite.d.ts +2 -0
  295. package/src/render/composite.ts +64 -0
  296. package/src/render/output-pane.d.ts +1 -0
  297. package/src/render/output-pane.ts +6 -0
  298. package/src/render/render-utils.d.ts +1 -0
  299. package/src/render/render-utils.ts +11 -0
  300. package/src/render/tool-card.d.ts +6 -0
  301. package/src/render/tool-card.ts +15 -0
  302. package/src/render/width-aware-text.d.ts +2 -0
  303. package/src/render/width-aware-text.ts +5 -0
  304. package/src/setup/scenes/model.ts +2 -1
  305. package/src/setup/scenes/splash.ts +116 -53
  306. package/src/setup/scenes/types.ts +1 -0
  307. package/src/status-line/component.ts +292 -78
  308. package/src/status-line/host.ts +4 -1
  309. package/src/status-line/segments.ts +40 -22
  310. package/src/status-line/types.ts +15 -3
  311. package/src/terminal-capabilities.d.ts +27 -35
  312. package/src/terminal-capabilities.ts +75 -189
  313. package/src/terminal-multiplexer.d.ts +127 -14
  314. package/src/terminal-multiplexer.ts +112 -33
  315. package/src/terminal.d.ts +22 -6
  316. package/src/terminal.ts +139 -37
  317. package/src/theme/active-symbols.d.ts +1 -1
  318. package/src/theme/active-symbols.ts +1 -1
  319. package/src/theme/schema-validation.ts +1 -0
  320. package/src/theme/schema.d.ts +11 -2
  321. package/src/theme/schema.ts +14 -1
  322. package/src/theme/session-color.ts +1 -7
  323. package/src/theme/symbols.d.ts +1 -1
  324. package/src/theme/symbols.ts +6 -1
  325. package/src/theme/theme-class.d.ts +7 -0
  326. package/src/theme/theme-class.ts +26 -1
  327. package/src/theme/theme-schema.json +5 -1
  328. package/src/theme/theme.d.ts +1 -1
  329. package/src/theme/theme.ts +8 -4
  330. package/src/theme/tui-adapters.d.ts +3 -0
  331. package/src/theme/tui-adapters.ts +68 -0
  332. package/src/tmux.d.ts +11 -4
  333. package/src/tmux.ts +47 -11
  334. package/src/tools/ast-edit.ts +59 -35
  335. package/src/tools/ast-grep.ts +35 -16
  336. package/src/tools/bash.ts +92 -42
  337. package/src/tools/daemon.d.ts +1 -1
  338. package/src/tools/daemon.ts +1 -1
  339. package/src/tools/default-renderer.ts +35 -19
  340. package/src/tools/edit.ts +142 -120
  341. package/src/tools/eval-format/index.ts +28 -2
  342. package/src/tools/eval-format/javascript.ts +7 -5
  343. package/src/tools/eval.d.ts +22 -3
  344. package/src/tools/eval.ts +364 -208
  345. package/src/tools/fetch.ts +5 -6
  346. package/src/tools/glob.d.ts +4 -0
  347. package/src/tools/glob.ts +33 -65
  348. package/src/tools/grep.ts +81 -48
  349. package/src/tools/index.ts +2 -0
  350. package/src/tools/lsp.ts +81 -59
  351. package/src/tools/mcp.ts +111 -68
  352. package/src/tools/native-view.d.ts +47 -0
  353. package/src/tools/native-view.ts +102 -1
  354. package/src/tools/output-meta.d.ts +11 -1
  355. package/src/tools/output-meta.ts +23 -3
  356. package/src/tools/read.ts +7 -5
  357. package/src/tools/renderer.d.ts +37 -2
  358. package/src/tools/renderer.ts +42 -2
  359. package/src/tools/streaming-output.d.ts +20 -1
  360. package/src/tools/streaming-output.ts +149 -50
  361. package/src/tools/task.d.ts +0 -4
  362. package/src/tools/task.ts +112 -59
  363. package/src/tools/todo.ts +119 -30
  364. package/src/tools/wait.ts +34 -24
  365. package/src/tools/web-search.ts +1 -1
  366. package/src/tools/write.d.ts +9 -2
  367. package/src/tools/write.ts +147 -95
  368. package/src/tools/yield.d.ts +22 -0
  369. package/src/tools/yield.ts +225 -0
  370. package/src/tui.d.ts +31 -0
  371. package/src/tui.ts +309 -160
  372. package/src/utils.d.ts +6 -0
  373. package/src/utils.ts +19 -0
  374. package/src/vim.d.ts +19 -5
  375. package/src/vim.ts +333 -18
  376. package/src/windows-input-mode.ts +8 -0
@@ -0,0 +1,24 @@
1
+ import type { TerminalMultiplexerModule } from "./types";
2
+
3
+ export const zellijMultiplexer = {
4
+ id: "zellij",
5
+ precedence: "session",
6
+ sessionEnvKeys: ["ZELLIJ"],
7
+ ownsScreenGrid: true,
8
+ isInside(env: NodeJS.ProcessEnv = Bun.env): boolean {
9
+ return Boolean(env.ZELLIJ);
10
+ },
11
+ notifier: {
12
+ tier: "inBand",
13
+ // Zellij drops OSCs and has no DCS passthrough; a bare BEL raises its bell
14
+ // flag. Bell notifications already provide that signal.
15
+ send({ sequence, write }) {
16
+ if (sequence === null) return false;
17
+ write(`${sequence}\x07`);
18
+ return true;
19
+ },
20
+ },
21
+ // Zellij currently breaks OSC 11 passthrough on macOS, so terminal-derived
22
+ // appearance cannot be trusted there.
23
+ backgroundQueryBrokenOn: ["darwin"],
24
+ } as const satisfies TerminalMultiplexerModule;
@@ -48,8 +48,9 @@ export interface NativeBackendOptions {
48
48
  /**
49
49
  * The reply a v1 terminal is assumed to give before its real `hello` arrives
50
50
  * (the `TERM_PROGRAM=tern` optimistic start): the whole vocabulary, the
51
- * default APC limit and credits, the terminal's width, the appearance zeta
52
- * already detected, and full motion.
51
+ * default APC limit and credits, the terminal's width, the appearance omp
52
+ * already detected, full motion, and `blobs`, so a resumed session's images
53
+ * are asked about rather than resent before the reply comes.
53
54
  */
54
55
  export declare function assumedTspHello(terminal: Terminal): TspHello;
55
56
  export declare class NativeBackend {
@@ -14,8 +14,18 @@
14
14
  * render while blocked only marks the surface dirty; the next `ack` reconciles
15
15
  * once against the last sent state, so every intermediate change coalesces
16
16
  * into one frame (`set`s merged, `text append`s joined).
17
+ *
18
+ * Blobs. Each image's bytes reach the terminal once per connection. A
19
+ * terminal with the `blobs` feature is asked first which it already holds:
20
+ * under `TERN_BLOB_DIR` (Tern's on-disk blob cache, on this machine) every new
21
+ * blob is written there and then asked about, so its bytes never cross the
22
+ * pty; otherwise the first pass after (re)connecting asks, so a resumed
23
+ * session doesn't resend what the terminal kept. Whatever the reply lacks
24
+ * (or a reply that never comes) is sent inline with `b`. Frames never wait:
25
+ * the terminal draws an image whose blob arrives after the frame.
17
26
  */
18
27
  import * as fs from "node:fs";
28
+ import * as path from "node:path";
19
29
  import * as logger from "@linxiraos/pi-utils/logger";
20
30
  import {
21
31
  TSP_DEFAULT_APC_LIMIT,
@@ -40,7 +50,7 @@ import {
40
50
  } from "../theme/theme";
41
51
  import type { Component, OverlayOptions, RenderScheduler, RenderTimer } from "../tui";
42
52
  import { TspDocument } from "./apply";
43
- import { getNativeBlob } from "./blobs";
53
+ import { getNativeBlob, type NativeBlob } from "./blobs";
44
54
  import { node } from "./describe";
45
55
  import { encodeTspJson, encodeTspMessage, type TspHello, TspReader, splitTspMessage } from "./encode";
46
56
  import type { DescribeContext, NativeChild, NativeNode, NativeSurface, NativeUiEvent } from "./node";
@@ -115,6 +125,44 @@ const RECENT_FRAMES = 64;
115
125
  const STALLED_ACK_MS = 5000;
116
126
  /** Role of the session's surfaces; a screen page may name its own. */
117
127
  const SESSION_ROLE = "zeta.session";
128
+ /** A `blobs` query unanswered this long counts its ids as missing: they go inline. */
129
+ const BLOB_REPLY_MS = 3000;
130
+ /** Expired queries kept to pair late replies with their queries, at most. */
131
+ const MAX_BLOB_QUERIES = 16;
132
+
133
+ /** A `blobs` query awaiting its reply; `timer` is unset once its ids no longer depend on it. */
134
+ interface BlobQuery {
135
+ readonly ids: readonly string[];
136
+ timer: RenderTimer | undefined;
137
+ }
138
+
139
+ /**
140
+ * Save `blob` in the terminal's blob cache as `<dir>/<id>` (written aside,
141
+ * then renamed; skipped when present). A failure only means the terminal
142
+ * won't find it, so the blob goes inline.
143
+ */
144
+ async function cacheBlob(dir: string, blob: NativeBlob): Promise<void> {
145
+ const target = path.join(dir, blob.id);
146
+ const temp = `${target}.tmp${process.pid}`;
147
+ try {
148
+ if (await Bun.file(target).exists()) return;
149
+ // Not `Bun.write`: it would create a missing folder, which only Tern may.
150
+ await fs.promises.writeFile(temp, blob.bytes);
151
+ await fs.promises.rename(temp, target);
152
+ } catch (error) {
153
+ logger.debug("TSP: could not save a blob to the terminal's cache", { dir, id: blob.id, error: String(error) });
154
+ await fs.promises.rm(temp, { force: true }).catch(() => {});
155
+ }
156
+ }
157
+
158
+ /**
159
+ * A `b` message's parameters. Tern names a blob by the sha256 of the bytes it
160
+ * receives and ignores `id`, but Tern 0.5.3 and earlier reject a blob without
161
+ * it, so it stays until those are gone.
162
+ */
163
+ function blobParams(blob: NativeBlob): Record<string, string> {
164
+ return { id: blob.id, mime: blob.mime };
165
+ }
118
166
 
119
167
  class NativeContext implements DescribeContext {
120
168
  cols: number;
@@ -157,8 +205,9 @@ function sameSet(a: ReadonlySet<string>, b: ReadonlySet<string>): boolean {
157
205
  /**
158
206
  * The reply a v1 terminal is assumed to give before its real `hello` arrives
159
207
  * (the `TERM_PROGRAM=tern` optimistic start): the whole vocabulary, the
160
- * default APC limit and credits, the terminal's width, the appearance zeta
161
- * already detected, and full motion.
208
+ * default APC limit and credits, the terminal's width, the appearance omp
209
+ * already detected, full motion, and `blobs`, so a resumed session's images
210
+ * are asked about rather than resent before the reply comes.
162
211
  */
163
212
  export function assumedTspHello(terminal: Terminal): TspHello {
164
213
  const appearance = terminal.appearance;
@@ -167,6 +216,7 @@ export function assumedTspHello(terminal: Terminal): TspHello {
167
216
  v: TSP_VERSION,
168
217
  term: "tern",
169
218
  kinds: TSP_KINDS,
219
+ features: ["blobs"],
170
220
  apc: TSP_DEFAULT_APC_LIMIT,
171
221
  credits: TSP_DEFAULT_CREDITS,
172
222
  cols: terminal.columns,
@@ -186,7 +236,6 @@ class Surface {
186
236
  acked = 0;
187
237
  /** Send times of unacknowledged frames, oldest first. */
188
238
  unacked: number[] = [];
189
- uploaded = new Set<string>();
190
239
  focus: string | null = null;
191
240
  dirty = false;
192
241
 
@@ -256,6 +305,14 @@ export class NativeBackend {
256
305
  #paletteKey: string | undefined;
257
306
  /** Serialized palette last sent, to skip resends of an unchanged theme. */
258
307
  #paletteSent: string | undefined;
308
+ /** Pending delivery owns bytes until sent or confirmed held; settled descriptions may already be released. */
309
+ #blobs = new Map<string, NativeBlob | "sent">();
310
+ /** `blobs` queries sent, oldest first: the terminal answers each once, in order. */
311
+ #blobQueries: BlobQuery[] = [];
312
+ /** The next pass with new blobs asks the terminal which it holds (a fresh connection). */
313
+ #askHeld = true;
314
+ /** Bumped when the connection resets or stops, so blob cache writes finishing later are dropped. */
315
+ #blobEpoch = 0;
259
316
 
260
317
  constructor(host: NativeHost, hello: TspHello, options: NativeBackendOptions = {}) {
261
318
  this.#host = host;
@@ -369,6 +426,9 @@ export class NativeBackend {
369
426
  this.#unbindTheme = undefined;
370
427
  this.#useNerdSymbols(false);
371
428
  this.#clearStallTimer();
429
+ // Replies no longer reach the backend: forget the queries outright.
430
+ this.#resetBlobs();
431
+ this.#blobQueries = [];
372
432
  }
373
433
 
374
434
  /**
@@ -383,13 +443,14 @@ export class NativeBackend {
383
443
  this.#live = true;
384
444
  this.#useNerdSymbols(true);
385
445
  this.#watchTheme();
446
+ this.#resetBlobs();
386
447
  const surface = this.#inline;
387
448
  surface.reconciler.detachLive();
388
449
  surface.unacked = [];
389
450
  surface.acked = surface.seq;
390
451
  surface.focus = null;
391
452
  surface.dirty = false;
392
- this.#write("o", { id: surface.id, mode: "inline", title: "zeta", role: SESSION_ROLE, adopt: true });
453
+ this.#write("o", { id: surface.id, mode: "inline", title: "omp", role: SESSION_ROLE, adopt: true });
393
454
  this.#sendPalette(surface);
394
455
  // After the `o`, as in `start()`.
395
456
  setNativeRendering(true);
@@ -491,7 +552,7 @@ export class NativeBackend {
491
552
  logger.debug("TSP frame", { sf: surface.id, ops: ops.length, rows: surface.reconciler.fallbackCount });
492
553
  }
493
554
  if (ops.length === 0) return;
494
- this.#uploadBlobs(surface, ops);
555
+ this.#uploadBlobs(ops);
495
556
  this.#sendFrame(surface, ops);
496
557
  }
497
558
 
@@ -505,6 +566,7 @@ export class NativeBackend {
505
566
  this.#record("in", raw.verb, raw.params, raw.body);
506
567
  const message = this.#reader.feed(sequence);
507
568
  if (message?.verb === "e") this.#handleEvent(message.event);
569
+ else if (message?.reply.r === "blobs") this.#onBlobsReply(message.reply.have);
508
570
  return true;
509
571
  }
510
572
 
@@ -530,7 +592,7 @@ export class NativeBackend {
530
592
  }
531
593
 
532
594
  #open(surface: Surface): void {
533
- this.#write("o", { id: surface.id, mode: surface.mode, title: "zeta", role: surface.role });
595
+ this.#write("o", { id: surface.id, mode: surface.mode, title: "omp", role: surface.role });
534
596
  this.#sendPalette(surface);
535
597
  }
536
598
 
@@ -583,25 +645,118 @@ export class NativeBackend {
583
645
  this.#write("f", frame);
584
646
  }
585
647
 
586
- #uploadBlobs(surface: Surface, ops: readonly TspOp[]): void {
648
+ /** Deliver the blobs `ops` reference that this connection hasn't handled yet (see the module doc). */
649
+ #uploadBlobs(ops: readonly TspOp[]): void {
587
650
  const ids = new Set<string>();
588
651
  for (const op of ops) collectBlobs(op, ids);
652
+ const fresh: NativeBlob[] = [];
589
653
  for (const id of ids) {
590
- if (surface.uploaded.has(id)) continue;
654
+ if (this.#blobs.has(id)) continue;
591
655
  const blob = getNativeBlob(id);
592
- if (!blob) {
593
- logger.warn("TSP: image references an unregistered blob", { id });
594
- continue;
656
+ if (blob) fresh.push(blob);
657
+ else logger.warn("TSP: image references an unregistered blob", { id });
658
+ }
659
+ if (fresh.length === 0) return;
660
+ if (!this.#cx.feature("blobs")) {
661
+ for (const blob of fresh) this.#sendBlob(blob);
662
+ return;
663
+ }
664
+ const dir = Bun.env.TERN_BLOB_DIR;
665
+ if (dir) {
666
+ for (const blob of fresh) this.#blobs.set(blob.id, blob);
667
+ void this.#cacheBlobs(dir, fresh);
668
+ return;
669
+ }
670
+ if (this.#askHeld) {
671
+ this.#askHeld = false;
672
+ this.#askBlobs(fresh);
673
+ return;
674
+ }
675
+ for (const blob of fresh) this.#sendBlob(blob);
676
+ }
677
+
678
+ /** Write `blobs` to the terminal's cache, then ask about the ones still pending. */
679
+ async #cacheBlobs(dir: string, blobs: readonly NativeBlob[]): Promise<void> {
680
+ const epoch = this.#blobEpoch;
681
+ await Promise.all(blobs.map(blob => cacheBlob(dir, blob)));
682
+ if (epoch !== this.#blobEpoch) return;
683
+ const pending = blobs.filter(blob => this.#blobs.get(blob.id) === blob);
684
+ if (pending.length > 0) this.#askBlobs(pending);
685
+ }
686
+
687
+ /** Ask the terminal which of `blobs` it holds; the reply (or its absence) settles them. */
688
+ #askBlobs(blobs: readonly NativeBlob[]): void {
689
+ for (const blob of blobs) this.#blobs.set(blob.id, blob);
690
+ const ids = blobs.map(blob => blob.id);
691
+ const query: BlobQuery = { ids, timer: undefined };
692
+ query.timer = this.#scheduler.scheduleRender(() => {
693
+ query.timer = undefined;
694
+ logger.warn("TSP: no reply to a blobs query; sending its blobs inline", { ids: ids.length });
695
+ this.#sendMissing(ids);
696
+ }, BLOB_REPLY_MS);
697
+ // Expired queries stay only to pair late replies; a terminal that never answers mustn't grow the list.
698
+ while (this.#blobQueries.length >= MAX_BLOB_QUERIES && !this.#blobQueries[0]!.timer) this.#blobQueries.shift();
699
+ this.#blobQueries.push(query);
700
+ this.#write("q", { q: "blobs", ids });
701
+ }
702
+
703
+ /**
704
+ * The reply to the oldest outstanding query. Any `have` id still asked
705
+ * about is held by the terminal (whichever query asked); the query's other
706
+ * ids go inline.
707
+ */
708
+ #onBlobsReply(have: readonly string[]): void {
709
+ const query = this.#blobQueries.shift();
710
+ for (const id of have) {
711
+ const blob = this.#blobs.get(id);
712
+ if (blob === undefined || blob === "sent") continue;
713
+ // The bytes went another way; a recording still carries them so a replay without them shows the image.
714
+ if (this.#recordPath) {
715
+ const body = Buffer.from(blob.bytes.buffer, blob.bytes.byteOffset, blob.bytes.byteLength).toString(
716
+ "base64",
717
+ );
718
+ this.#record("out", "b", blobParams(blob), body);
595
719
  }
596
- surface.uploaded.add(id);
597
- const body = Buffer.from(blob.bytes.buffer, blob.bytes.byteOffset, blob.bytes.byteLength).toString("base64");
598
- // The full body, so a replay (Tern's `surface-play`) shows the image.
599
- this.#record("out", "b", { id, mime: blob.mime }, body);
600
- this.#host.terminal.write(encodeTspMessage("b", body, { id, mime: blob.mime }, this.#limit));
720
+ this.#blobs.set(id, "sent");
721
+ }
722
+ if (!query?.timer) return;
723
+ query.timer.cancel();
724
+ query.timer = undefined;
725
+ this.#sendMissing(query.ids);
726
+ }
727
+
728
+ /** Send inline every id of `ids` still waiting on a query. */
729
+ #sendMissing(ids: readonly string[]): void {
730
+ for (const id of ids) {
731
+ const blob = this.#blobs.get(id);
732
+ if (blob !== undefined && blob !== "sent") this.#sendBlob(blob);
733
+ }
734
+ }
735
+
736
+ #sendBlob(blob: NativeBlob): void {
737
+ this.#blobs.set(blob.id, "sent");
738
+ const body = Buffer.from(blob.bytes.buffer, blob.bytes.byteOffset, blob.bytes.byteLength).toString("base64");
739
+ // The full body, so a replay (Tern's `surface-play`) shows the image.
740
+ this.#record("out", "b", blobParams(blob), body);
741
+ this.#host.terminal.write(encodeTspMessage("b", body, blobParams(blob), this.#limit));
742
+ }
743
+
744
+ /**
745
+ * A new connection (resume, or a fresh inline surface after `gone`): the
746
+ * terminal is asked again. Outstanding queries stay, without their
747
+ * deadlines, only to pair their replies.
748
+ */
749
+ #resetBlobs(): void {
750
+ this.#blobEpoch++;
751
+ this.#blobs.clear();
752
+ this.#askHeld = true;
753
+ for (const query of this.#blobQueries) {
754
+ query.timer?.cancel();
755
+ query.timer = undefined;
601
756
  }
602
757
  }
603
758
 
604
- #write(verb: "o" | "f" | "t" | "x", body: unknown): void {
759
+ #write(verb: "o" | "f" | "t" | "x" | "q", body: unknown): void {
605
760
  this.#record("out", verb, undefined, body);
606
761
  this.#host.terminal.write(encodeTspJson(verb, body, undefined, this.#limit));
607
762
  }
@@ -665,6 +820,7 @@ export class NativeBackend {
665
820
  case "gone":
666
821
  if (event.ids.includes(this.#inline.id)) {
667
822
  // Adopt found nothing (evicted, or another pane): start over.
823
+ this.#resetBlobs();
668
824
  this.#inline = this.#newSurface("inline");
669
825
  if (this.#live) this.#open(this.#inline);
670
826
  this.#host.requestRender();
@@ -2,8 +2,10 @@
2
2
  * Content-addressed binary blobs (images) for `image` nodes.
3
3
  *
4
4
  * `describe()` registers the bytes and puts the returned id in `image.p.blob`;
5
- * the native backend uploads each referenced blob once per surface with verb
6
- * `b` before the frame that first references it.
5
+ * the native backend delivers each referenced blob once per terminal
6
+ * connection (asking the terminal first which it holds, or through its blob
7
+ * cache), else uploads it with verb `b` before the frame that first
8
+ * references it.
7
9
  */
8
10
  import type { TspProps } from "@linxiraos/pi-wire";
9
11
  import type { NativeNode } from "./node";
@@ -12,10 +14,12 @@ export interface NativeBlob {
12
14
  readonly mime: string;
13
15
  readonly bytes: Uint8Array;
14
16
  }
15
- /** Register `bytes` and return their content address (sha256 hex) for `image.p.blob`. */
17
+ /** Register bytes for process-lifetime, id-only use. Prefer {@link nativeImageNode} for owned images. */
16
18
  export declare function registerNativeBlob(bytes: Uint8Array, mime: string): string;
17
19
  /** A registered blob by id. */
18
20
  export declare function getNativeBlob(id: string): NativeBlob | undefined;
21
+ /** An image whose props own its bytes, including while hidden, unmounted, or awaiting upload. */
22
+ export declare function nativeImageNode(bytes: Uint8Array, mime: string, p?: Omit<TspProps<"image">, "blob" | "builtin">, key?: string): NativeNode;
19
23
  /**
20
24
  * An `image` node for a base64 payload: the bytes are registered as a blob and
21
25
  * the pixel size probed from the header. Decoding and hashing cost real time,
@@ -2,8 +2,10 @@
2
2
  * Content-addressed binary blobs (images) for `image` nodes.
3
3
  *
4
4
  * `describe()` registers the bytes and puts the returned id in `image.p.blob`;
5
- * the native backend uploads each referenced blob once per surface with verb
6
- * `b` before the frame that first references it.
5
+ * the native backend delivers each referenced blob once per terminal
6
+ * connection (asking the terminal first which it holds, or through its blob
7
+ * cache), else uploads it with verb `b` before the frame that first
8
+ * references it.
7
9
  */
8
10
  import type { TspProps } from "@linxiraos/pi-wire";
9
11
  import { getImageDimensions } from "../terminal-capabilities";
@@ -16,7 +18,13 @@ export interface NativeBlob {
16
18
  readonly bytes: Uint8Array;
17
19
  }
18
20
 
19
- const blobs = new Map<string, NativeBlob>();
21
+ const blobs = new Map<string, WeakRef<NativeBlob>>();
22
+ const registeredBlobs = new Map<string, NativeBlob>();
23
+ const releasedBlobs = new FinalizationRegistry<{ id: string; reference: WeakRef<NativeBlob> }>(({ id, reference }) => {
24
+ if (blobs.get(id) === reference) blobs.delete(id);
25
+ });
26
+ // Props spreads preserve ownership; wire serialization omits symbol keys.
27
+ const kBlob = Symbol("native.blob");
20
28
  // Registration usually repeats with the same bytes object on every describe;
21
29
  // the tag skips rehashing it.
22
30
  const kBlobId = Symbol("native.blobId");
@@ -25,19 +33,42 @@ interface BlobTagged {
25
33
  [kBlobId]?: string;
26
34
  }
27
35
 
28
- /** Register `bytes` and return their content address (sha256 hex) for `image.p.blob`. */
29
- export function registerNativeBlob(bytes: Uint8Array, mime: string): string {
36
+ function getOrCreateBlob(bytes: Uint8Array, mime: string): NativeBlob {
30
37
  const known = (bytes as BlobTagged)[kBlobId];
31
- if (known !== undefined) return known;
32
- const id = new Bun.CryptoHasher("sha256").update(bytes).digest("hex");
38
+ const id = known ?? new Bun.CryptoHasher("sha256").update(bytes).digest("hex");
33
39
  (bytes as BlobTagged)[kBlobId] = id;
34
- if (!blobs.has(id)) blobs.set(id, { id, mime, bytes });
35
- return id;
40
+ let blob = blobs.get(id)?.deref();
41
+ if (!blob) {
42
+ blob = { id, mime, bytes };
43
+ const reference = new WeakRef(blob);
44
+ blobs.set(id, reference);
45
+ releasedBlobs.register(blob, { id, reference });
46
+ }
47
+ return blob;
48
+ }
49
+
50
+ /** Register bytes for process-lifetime, id-only use. Prefer {@link nativeImageNode} for owned images. */
51
+ export function registerNativeBlob(bytes: Uint8Array, mime: string): string {
52
+ const blob = getOrCreateBlob(bytes, mime);
53
+ registeredBlobs.set(blob.id, blob);
54
+ return blob.id;
36
55
  }
37
56
 
38
57
  /** A registered blob by id. */
39
58
  export function getNativeBlob(id: string): NativeBlob | undefined {
40
- return blobs.get(id);
59
+ return blobs.get(id)?.deref();
60
+ }
61
+
62
+ /** An image whose props own its bytes, including while hidden, unmounted, or awaiting upload. */
63
+ export function nativeImageNode(
64
+ bytes: Uint8Array,
65
+ mime: string,
66
+ p?: Omit<TspProps<"image">, "blob" | "builtin">,
67
+ key?: string,
68
+ ): NativeNode {
69
+ const blob = getOrCreateBlob(bytes, mime);
70
+ const props = { ...p, blob: blob.id, [kBlob]: blob };
71
+ return node("image", props, undefined, key);
41
72
  }
42
73
 
43
74
  /**
@@ -51,9 +82,13 @@ export function base64ImageNode(
51
82
  p?: Omit<TspProps<"image">, "blob" | "builtin" | "w" | "h">,
52
83
  key?: string,
53
84
  ): NativeNode {
54
- const blob = registerNativeBlob(Buffer.from(data, "base64"), mimeType);
55
85
  const size = getImageDimensions(data, mimeType);
56
- return node("image", size ? { ...p, blob, w: size.widthPx, h: size.heightPx } : { ...p, blob }, undefined, key);
86
+ return nativeImageNode(
87
+ Buffer.from(data, "base64"),
88
+ mimeType,
89
+ size ? { ...p, w: size.widthPx, h: size.heightPx } : p,
90
+ key,
91
+ );
57
92
  }
58
93
 
59
94
  /** `image` nodes for base64 payloads, one per slot key, rebuilt only when the slot's payload changes. */
@@ -15,11 +15,10 @@ export type TspIncoming = {
15
15
  };
16
16
  /** Message parameters (`k=v`, ASCII, never containing `;`). */
17
17
  export type TspParams = Readonly<Record<string, string | number>>;
18
- /** Split `body` into pieces of at most `limit` UTF-8 bytes, never inside a code point. */
19
- export declare function splitUtf8(body: string, limit: number): string[];
20
18
  /**
21
- * Encode one logical message, chunked when `body` exceeds `limit` UTF-8 bytes.
22
- * Returns the complete byte string to write (all chunks, in order).
19
+ * Encode one logical message, chunked when `body` exceeds `limit` UTF-8 bytes:
20
+ * the first chunk carries `params`, every chunk `c=<id>` and all but the last
21
+ * `m=1`. Returns the complete byte string to write (all chunks, in order).
23
22
  */
24
23
  export declare function encodeTspMessage(verb: TspVerb, body: string, params?: TspParams, limit?: number): string;
25
24
  /** Encode a JSON message body. */
@@ -6,7 +6,9 @@
6
6
  * negotiated `apc` limit is split across messages with the same verb, tagged
7
7
  * `c=<chunk-id>`, with `m=1` on every chunk but the last; the receiver joins
8
8
  * the bodies byte-wise. Chunks split on code-point boundaries, so every chunk
9
- * is valid UTF-8 on its own and the joined bytes equal the original body.
9
+ * is valid UTF-8 on its own and the joined bytes equal the original body, and
10
+ * never where the next chunk would lead with a `key=value;` segment the
11
+ * receiver would read as a parameter (Tern's `tsp_chunks`).
10
12
  */
11
13
  import { isRecord } from "@linxiraos/pi-utils/type-guards";
12
14
  import {
@@ -34,36 +36,58 @@ export type TspParams = Readonly<Record<string, string | number>>;
34
36
 
35
37
  let nextChunkId = 1;
36
38
 
37
- /** UTF-8 byte length of one UTF-16 code unit sequence starting at `i`, and its unit count. */
38
- function codePointBytes(text: string, i: number): { bytes: number; units: number } {
39
- const c = text.charCodeAt(i);
40
- if (c < 0x80) return { bytes: 1, units: 1 };
41
- if (c < 0x800) return { bytes: 2, units: 1 };
42
- if (c >= 0xd800 && c <= 0xdbff && i + 1 < text.length) {
43
- const next = text.charCodeAt(i + 1);
44
- if (next >= 0xdc00 && next <= 0xdfff) return { bytes: 4, units: 2 };
39
+ /** Whether `bytes` from `at` leads with a `key=value;` segment, which a receiver reads as one more parameter. */
40
+ function leadsWithParameter(bytes: Uint8Array, at: number): boolean {
41
+ let i = at;
42
+ // Key bytes: `[A-Za-z0-9_-]`.
43
+ for (; i < bytes.length; i++) {
44
+ const b = bytes[i]!;
45
+ if (
46
+ !((b >= 0x30 && b <= 0x39) || (b >= 0x41 && b <= 0x5a) || (b >= 0x61 && b <= 0x7a) || b === 0x5f || b === 0x2d)
47
+ )
48
+ break;
45
49
  }
46
- return { bytes: 3, units: 1 };
50
+ if (i === at || bytes[i] !== 0x3d) return false;
51
+ i++;
52
+ // Value bytes: printable ASCII other than `;`.
53
+ while (i < bytes.length && bytes[i]! >= 0x21 && bytes[i]! <= 0x7e && bytes[i] !== 0x3b) i++;
54
+ return bytes[i] === 0x3b;
47
55
  }
48
56
 
49
- /** Split `body` into pieces of at most `limit` UTF-8 bytes, never inside a code point. */
50
- export function splitUtf8(body: string, limit: number): string[] {
51
- const max = Math.max(4, Math.trunc(limit));
57
+ /**
58
+ * Where the chunk of `bytes` starting at `start` ends: the safe cut nearest
59
+ * below `start + limit` (above `start`), else the nearest above it, else the
60
+ * end. A cut is safe on a UTF-8 character boundary where the rest doesn't
61
+ * lead with a parameter-shaped segment.
62
+ */
63
+ function chunkEnd(bytes: Uint8Array, start: number, limit: number): number {
64
+ const target = start + limit;
65
+ if (target >= bytes.length) return bytes.length;
66
+ const safe = (at: number): boolean => (bytes[at]! & 0xc0) !== 0x80 && !leadsWithParameter(bytes, at);
67
+ for (let at = target; at > start; at--) if (safe(at)) return at;
68
+ for (let at = target + 1; at < bytes.length; at++) if (safe(at)) return at;
69
+ return bytes.length;
70
+ }
71
+
72
+ /**
73
+ * Chunk bodies for `body` of `byteLength` UTF-8 bytes, cut as Tern's
74
+ * `tsp_chunks` cuts. Pure ASCII without `;` (every base64 blob) has no
75
+ * parameter-shaped segment and no multibyte character, so it is cut every
76
+ * `limit` characters.
77
+ */
78
+ function chunkBodies(body: string, byteLength: number, limit: number): string[] {
79
+ const max = Math.max(1, Math.trunc(limit));
52
80
  const pieces: string[] = [];
53
- let start = 0;
54
- let bytes = 0;
55
- let i = 0;
56
- while (i < body.length) {
57
- const step = codePointBytes(body, i);
58
- if (bytes + step.bytes > max) {
59
- pieces.push(body.slice(start, i));
60
- start = i;
61
- bytes = 0;
62
- }
63
- bytes += step.bytes;
64
- i += step.units;
81
+ if (byteLength === body.length && !body.includes(";")) {
82
+ for (let at = 0; at < body.length; at += max) pieces.push(body.slice(at, at + max));
83
+ return pieces;
84
+ }
85
+ const bytes = Buffer.from(body, "utf8");
86
+ for (let start = 0; start < bytes.length;) {
87
+ const end = chunkEnd(bytes, start, max);
88
+ pieces.push(bytes.toString("utf8", start, end));
89
+ start = end;
65
90
  }
66
- pieces.push(body.slice(start));
67
91
  return pieces;
68
92
  }
69
93
 
@@ -79,8 +103,9 @@ function encodeParams(params: TspParams | undefined): string {
79
103
  }
80
104
 
81
105
  /**
82
- * Encode one logical message, chunked when `body` exceeds `limit` UTF-8 bytes.
83
- * Returns the complete byte string to write (all chunks, in order).
106
+ * Encode one logical message, chunked when `body` exceeds `limit` UTF-8 bytes:
107
+ * the first chunk carries `params`, every chunk `c=<id>` and all but the last
108
+ * `m=1`. Returns the complete byte string to write (all chunks, in order).
84
109
  */
85
110
  export function encodeTspMessage(
86
111
  verb: TspVerb,
@@ -90,13 +115,15 @@ export function encodeTspMessage(
90
115
  ): string {
91
116
  const base = encodeParams(params);
92
117
  // Fast path: ASCII-length bound first, exact byte count only near the limit.
93
- if (body.length * 3 <= limit || Buffer.byteLength(body, "utf8") <= limit) return frame(verb, base, body);
118
+ if (body.length * 3 <= limit) return frame(verb, base, body);
119
+ const byteLength = Buffer.byteLength(body, "utf8");
120
+ if (byteLength <= limit) return frame(verb, base, body);
94
121
  const chunkId = (nextChunkId++).toString(36);
95
- const pieces = splitUtf8(body, limit);
122
+ const pieces = chunkBodies(body, byteLength, limit);
96
123
  let out = "";
97
124
  for (let i = 0; i < pieces.length; i++) {
98
125
  const more = i < pieces.length - 1 ? ";m=1" : "";
99
- out += frame(verb, `${base};c=${chunkId}${more}`, pieces[i]!);
126
+ out += frame(verb, `${i === 0 ? base : ""};c=${chunkId}${more}`, pieces[i]!);
100
127
  }
101
128
  return out;
102
129
  }
@@ -58,6 +58,11 @@ export interface ActionButtonOptions {
58
58
  * which the component routes to the same code path as the key.
59
59
  */
60
60
  export declare function actionButton(label: string, act: string, options?: ActionButtonOptions): NativeNode;
61
+ /**
62
+ * A bare `esc` keycap button for a view's top-right corner: a click sends
63
+ * `act` (default `close`), which the component routes to its Esc path.
64
+ */
65
+ export declare function escCloseButton(act?: string, key?: string): NativeNode;
61
66
  /** A row of {@link actionButton}s (role `zeta.actions`); `null` entries become the spacer that end-aligns what follows. */
62
67
  export declare function actionBar(buttons: readonly (NativeNode | null)[], key?: string): NativeNode;
63
68
  /** Spans of an annotation callout: a dim `label:` then the note. */
@@ -119,6 +119,19 @@ export function actionButton(label: string, act: string, options: ActionButtonOp
119
119
  );
120
120
  }
121
121
 
122
+ /**
123
+ * A bare `esc` keycap button for a view's top-right corner: a click sends
124
+ * `act` (default `close`), which the component routes to its Esc path.
125
+ */
126
+ export function escCloseButton(act = "close", key = "esc-close"): NativeNode {
127
+ return node(
128
+ "row",
129
+ { role: "zeta.btn", align: "center", actions: { click: act }, title: `Close ${formatTooltipKey("escape")}` },
130
+ [kbd("escape")],
131
+ key,
132
+ );
133
+ }
134
+
122
135
  /** A row of {@link actionButton}s (role `zeta.actions`); `null` entries become the spacer that end-aligns what follows. */
123
136
  export function actionBar(buttons: readonly (NativeNode | null)[], key = "actions"): NativeNode {
124
137
  const children = buttons.map(button => button ?? node("spacer", { grow: 1 }));
@@ -30,6 +30,7 @@
30
30
  import * as logger from "@linxiraos/pi-utils/logger";
31
31
  import { TSP_TEXT_KINDS, type TspKind, type TspNode, type TspOp, type TspScrollBy } from "@linxiraos/pi-wire";
32
32
  import { type Component, Container, CURSOR_MARKER } from "../tui";
33
+ import { getNativeBlob } from "./blobs";
33
34
  import { normalizeIconProps } from "./icons";
34
35
  import type { DescribeContext, NativeChild, NativeNode, NativeRevealAt } from "./node";
35
36
  import { isNativeSettled } from "./settle";
@@ -291,6 +292,7 @@ export class Reconciler {
291
292
  this.#prevPortals = portals;
292
293
  this.#regions = next;
293
294
  for (const entry of this.#dels) this.#deleteEntry(entry);
295
+ this.#dels = [];
294
296
  // Parked to survive a parent/child swap but absent from the new description.
295
297
  for (const state of this.#parked) {
296
298
  if (!state.parked) continue;
@@ -711,6 +713,8 @@ export class Reconciler {
711
713
  owner: Owner,
712
714
  entries: readonly Entry[],
713
715
  ): Readonly<Record<string, unknown>> | undefined {
716
+ // Dereferencing pins the blob through this synchronous reconcile/upload job, even after settling drops its node.
717
+ if (node.k === "image" && node.p?.blob) getNativeBlob(node.p.blob);
714
718
  return normalizeIconProps(node.k, this.#resolveRefs(node, owner, entries));
715
719
  }
716
720