@kabmo-labs/kabmo 0.24.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (325) hide show
  1. package/dist/client/index.html +1 -1
  2. package/dist/client/index.js +585 -512
  3. package/dist/client/static/js/{index-jcr5twtp.js → index-95b3n20k.js} +1105 -1207
  4. package/dist/client/static/js/index-p2yw9ywf.css +1 -0
  5. package/package.json +2 -4
  6. package/src/cli/lib/args.js +24 -1
  7. package/src/cli/lib/args.test.js +16 -0
  8. package/src/cli/lib/commands/session.js +233 -0
  9. package/src/cli/lib/commands/session.test.js +103 -0
  10. package/src/cli/main.js +1 -1
  11. package/src/client/components/common/ComposerPicker.tsx +3 -0
  12. package/src/client/components/common/code-surface/shiki.test.ts +82 -0
  13. package/src/client/components/common/code-surface/shiki.ts +28 -7
  14. package/src/client/components/common/session-actions-menu/index.tsx +59 -2
  15. package/src/client/components/common/session-branch/BranchGuides.tsx +93 -0
  16. package/src/client/components/common/session-branch/SubagentRow.tsx +77 -0
  17. package/src/client/components/common/session-branch/index.tsx +169 -0
  18. package/src/client/components/common/session-branch/session-branch.test.tsx +240 -0
  19. package/src/client/components/common/session-branch/style.ts +29 -0
  20. package/src/client/components/common/session-row/StatusSlot.tsx +17 -5
  21. package/src/client/components/common/session-row/index.tsx +28 -3
  22. package/src/client/components/common/session-row/session-row.test.tsx +11 -0
  23. package/src/client/components/common/session-state-provider/index.tsx +1 -1
  24. package/src/client/components/layout/desktop-layout/resizer/separator-drag.ts +3 -3
  25. package/src/client/components/layout/session-sidebar/CategoryItem.tsx +70 -61
  26. package/src/client/components/layout/session-sidebar/SessionItem.batch.test.ts +3 -3
  27. package/src/client/components/layout/session-sidebar/SessionItem.test.ts +25 -2
  28. package/src/client/components/layout/session-sidebar/SessionItem.tsx +35 -7
  29. package/src/client/components/layout/session-sidebar/about-modal/HarnessRow.tsx +261 -0
  30. package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +39 -198
  31. package/src/client/components/layout/session-sidebar/about-modal/HarnessTableLog.test.tsx +114 -0
  32. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.test.tsx +130 -0
  33. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogDialog.tsx +128 -0
  34. package/src/client/components/layout/session-sidebar/about-modal/UpdateLogLine.tsx +56 -0
  35. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +44 -36
  36. package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +62 -47
  37. package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +63 -50
  38. package/src/client/components/mobile/mobile-session-sidebar/SessionRow.test.ts +5 -0
  39. package/src/client/components/mobile/mobile-session-sidebar/SessionRow.tsx +37 -12
  40. package/src/client/components/settings/Sidebar.tsx +1 -1
  41. package/src/client/components/settings/categories/NotificationSettings.test.tsx +92 -0
  42. package/src/client/components/settings/categories/NotificationSettings.tsx +112 -2
  43. package/src/client/components/settings/categories/harness-settings/HarnessCard.tsx +1 -1
  44. package/src/client/components/workspace/chat-timeline/AiMessageFooter.fork.test.ts +94 -0
  45. package/src/client/components/workspace/chat-timeline/AiMessageFooter.tsx +31 -5
  46. package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +64 -70
  47. package/src/client/components/workspace/chat-timeline/ComposerDockFrame.tsx +35 -0
  48. package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +128 -122
  49. package/src/client/components/workspace/chat-timeline/MessageItem.tsx +22 -1
  50. package/src/client/components/workspace/chat-timeline/MessageList.tsx +11 -5
  51. package/src/client/components/workspace/chat-timeline/PendingChatView.tsx +10 -0
  52. package/src/client/components/workspace/chat-timeline/RunFooter.tsx +12 -3
  53. package/src/client/components/workspace/chat-timeline/TimelineBody.tsx +3 -0
  54. package/src/client/components/workspace/chat-timeline/chat-input/AttachmentToolbar.tsx +2 -0
  55. package/src/client/components/workspace/chat-timeline/chat-input/ModeToggles.tsx +15 -1
  56. package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.test.tsx +85 -0
  57. package/src/client/components/workspace/chat-timeline/chat-input/SessionModeDropdown.tsx +84 -0
  58. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +2 -0
  59. package/src/client/components/workspace/chat-timeline/chat-input/modes-props.ts +5 -0
  60. package/src/client/components/workspace/chat-timeline/composer-dock-frame.test.tsx +155 -0
  61. package/src/client/components/workspace/chat-timeline/index.tsx +11 -20
  62. package/src/client/components/workspace/chat-timeline/target-picker/DraftTargetRow.tsx +115 -0
  63. package/src/client/components/workspace/chat-timeline/target-picker/ParentSessionPicker.tsx +173 -0
  64. package/src/client/components/workspace/chat-timeline/target-picker/parent-session-options.test.ts +49 -0
  65. package/src/client/components/workspace/terminal-panel/KeyBar.tsx +10 -8
  66. package/src/client/components/workspace/terminal-panel/{RealtimeXtermView.tsx → RealtimeTerminalView.tsx} +51 -58
  67. package/src/client/components/workspace/terminal-panel/fit.test.ts +37 -0
  68. package/src/client/components/workspace/terminal-panel/fit.ts +64 -0
  69. package/src/client/components/workspace/terminal-panel/fonts.test.ts +161 -0
  70. package/src/client/components/workspace/terminal-panel/fonts.ts +164 -0
  71. package/src/client/components/workspace/terminal-panel/grid.test.ts +52 -0
  72. package/src/client/components/workspace/terminal-panel/grid.ts +118 -0
  73. package/src/client/components/workspace/terminal-panel/index.tsx +25 -21
  74. package/src/client/components/workspace/terminal-panel/mount.ts +247 -223
  75. package/src/client/components/workspace/terminal-panel/navigation.test.ts +54 -0
  76. package/src/client/components/workspace/terminal-panel/navigation.ts +47 -0
  77. package/src/client/data/settings/omp-schema.json +1 -1
  78. package/src/client/data/theme/terminal.test.ts +25 -0
  79. package/src/client/data/theme/terminal.ts +96 -79
  80. package/src/client/hooks/chat/composer/items.test.ts +31 -26
  81. package/src/client/hooks/chat/composer/pick-mapping.test.ts +42 -0
  82. package/src/client/hooks/chat/omp/folder-auto-expand.ts +1 -1
  83. package/src/client/hooks/chat/omp/pending-adoption.ts +51 -0
  84. package/src/client/hooks/chat/omp/pending-child.test.ts +73 -0
  85. package/src/client/hooks/chat/omp/pending-entry.ts +59 -0
  86. package/src/client/hooks/chat/omp/pending-rows.ts +6 -2
  87. package/src/client/hooks/chat/omp/pending-sessions.test.ts +1 -2
  88. package/src/client/hooks/chat/omp/pending-sessions.ts +45 -69
  89. package/src/client/hooks/chat/omp/pending-waiting.ts +1 -1
  90. package/src/client/hooks/chat/omp/prompt-send.test.ts +7 -7
  91. package/src/client/hooks/chat/omp/prompt-send.ts +26 -14
  92. package/src/client/hooks/chat/omp/run-switch.ts +1 -1
  93. package/src/client/hooks/chat/omp/session-list.tsx +4 -0
  94. package/src/client/hooks/chat/subagents/roster.ts +122 -0
  95. package/src/client/hooks/chat/timeline/child-spawn.test.ts +55 -0
  96. package/src/client/hooks/chat/timeline/child-spawn.ts +45 -0
  97. package/src/client/hooks/chat/timeline/fork-request.test.ts +54 -0
  98. package/src/client/hooks/chat/timeline/fork-request.ts +43 -0
  99. package/src/client/hooks/chat/timeline/mode-availability.test.ts +1 -1
  100. package/src/client/hooks/chat/timeline/mode-availability.ts +33 -0
  101. package/src/client/hooks/chat/timeline/mode-markers.ts +7 -1
  102. package/src/client/hooks/chat/timeline/mode-post.ts +15 -3
  103. package/src/client/hooks/chat/timeline/modes-contract.ts +9 -4
  104. package/src/client/hooks/chat/timeline/modes.test.ts +56 -18
  105. package/src/client/hooks/chat/timeline/modes.ts +42 -29
  106. package/src/client/hooks/chat/timeline/pending-branch.ts +205 -0
  107. package/src/client/hooks/chat/timeline/pending-dispatch.ts +253 -0
  108. package/src/client/hooks/chat/timeline/prompt-text.test.ts +34 -1
  109. package/src/client/hooks/chat/timeline/prompt-text.ts +47 -20
  110. package/src/client/hooks/chat/timeline/send-execute.test.ts +39 -0
  111. package/src/client/hooks/chat/timeline/send-helpers.ts +30 -1
  112. package/src/client/hooks/chat/timeline/send-steer.test.ts +3 -1
  113. package/src/client/hooks/chat/timeline/send.test-util.ts +7 -1
  114. package/src/client/hooks/chat/timeline/send.test.ts +3 -1
  115. package/src/client/hooks/chat/timeline/send.ts +48 -133
  116. package/src/client/hooks/chat/timeline/session-harness.ts +6 -0
  117. package/src/client/hooks/chat/timeline/stop-with-queue.ts +42 -0
  118. package/src/client/hooks/chat/timeline/use-fork-session.ts +58 -0
  119. package/src/client/hooks/models/use-harness-update.test.ts +286 -0
  120. package/src/client/hooks/models/use-harness-update.ts +250 -0
  121. package/src/client/hooks/ui/input-required-alert.ts +12 -5
  122. package/src/client/hooks/ui/notifications.ts +45 -0
  123. package/src/client/hooks/ui/run-outcome-alert.ts +48 -0
  124. package/src/client/hooks/workspace/expanded-sessions.ts +36 -7
  125. package/src/client/hooks/workspace/terminal/index.ts +17 -3
  126. package/src/client/hooks/workspace/terminal/socket.ts +9 -4
  127. package/src/client/lib/native/open-session.ts +33 -0
  128. package/src/client/lib/notifications/os.test.ts +125 -0
  129. package/src/client/lib/notifications/os.ts +130 -0
  130. package/src/client/lib/plugins/sidebar-bridge.ts +2 -20
  131. package/src/client/lib/router/open-session.test.ts +75 -0
  132. package/src/client/lib/router/open-session.ts +34 -0
  133. package/src/client/lib/session-tree.test.ts +50 -0
  134. package/src/client/lib/session-tree.ts +59 -0
  135. package/src/client/lib/signals.ts +5 -1
  136. package/src/client/main.tsx +6 -0
  137. package/src/client/tailwind.css +17 -11
  138. package/src/server/index.ts +2 -0
  139. package/src/server/lib/auth/guard.ts +10 -0
  140. package/src/server/lib/btw/session-copy.server.ts +6 -44
  141. package/src/server/lib/btw/session-copy.test.ts +2 -1
  142. package/src/server/lib/db/schema.ts +5 -0
  143. package/src/server/lib/db/seed-terminal.test.ts +1 -1
  144. package/src/server/lib/db/seed.test.ts +1 -1
  145. package/src/server/lib/harness/acp/background-work.test.ts +262 -0
  146. package/src/server/lib/harness/acp/background-work.ts +320 -0
  147. package/src/server/lib/harness/acp/calls.ts +15 -1
  148. package/src/server/lib/harness/acp/client-capabilities.test.ts +55 -0
  149. package/src/server/lib/harness/acp/client-capabilities.ts +61 -0
  150. package/src/server/lib/harness/acp/client-fs.test.ts +135 -0
  151. package/src/server/lib/harness/acp/client-fs.ts +131 -0
  152. package/src/server/lib/harness/acp/client-initialize.test.ts +70 -0
  153. package/src/server/lib/harness/acp/client-initialize.ts +84 -0
  154. package/src/server/lib/harness/acp/client.ts +33 -23
  155. package/src/server/lib/harness/acp/commands.test.ts +5 -5
  156. package/src/server/lib/harness/acp/commands.ts +14 -2
  157. package/src/server/lib/harness/acp/frames-timing.test.ts +68 -0
  158. package/src/server/lib/harness/acp/frames-v2.test.ts +85 -0
  159. package/src/server/lib/harness/acp/frames.ts +71 -158
  160. package/src/server/lib/harness/acp/gateway-env.test.ts +64 -0
  161. package/src/server/lib/harness/acp/gateway-env.ts +70 -0
  162. package/src/server/lib/harness/acp/mapper.test.ts +40 -0
  163. package/src/server/lib/harness/acp/mapper.ts +12 -0
  164. package/src/server/lib/harness/acp/permissions.ts +6 -0
  165. package/src/server/lib/harness/acp/prompt.test.ts +20 -0
  166. package/src/server/lib/harness/acp/prompt.ts +19 -3
  167. package/src/server/lib/harness/acp/protocol.ts +41 -2
  168. package/src/server/lib/harness/acp/session-background.test.ts +129 -0
  169. package/src/server/lib/harness/acp/session-exit.ts +31 -0
  170. package/src/server/lib/harness/acp/session-facts.ts +1 -1
  171. package/src/server/lib/harness/acp/session-handshake.test.ts +197 -0
  172. package/src/server/lib/harness/acp/session-handshake.ts +110 -7
  173. package/src/server/lib/harness/acp/session-idle.ts +7 -6
  174. package/src/server/lib/harness/acp/session-listeners.ts +31 -0
  175. package/src/server/lib/harness/acp/session-notifications.ts +41 -2
  176. package/src/server/lib/harness/acp/session-observers.test.ts +63 -0
  177. package/src/server/lib/harness/acp/session-permissions.test.ts +29 -0
  178. package/src/server/lib/harness/acp/session-permissions.ts +36 -0
  179. package/src/server/lib/harness/acp/session-requests.test.ts +92 -0
  180. package/src/server/lib/harness/acp/session-requests.ts +89 -0
  181. package/src/server/lib/harness/acp/session-turns.ts +28 -8
  182. package/src/server/lib/harness/acp/session-usage.ts +4 -0
  183. package/src/server/lib/harness/acp/session-web-state.ts +30 -2
  184. package/src/server/lib/harness/acp/session.test-util.ts +42 -2
  185. package/src/server/lib/harness/acp/session.ts +115 -115
  186. package/src/server/lib/harness/acp/tool-calls.ts +223 -0
  187. package/src/server/lib/harness/acp/turn.test.ts +183 -0
  188. package/src/server/lib/harness/acp/turn.ts +102 -10
  189. package/src/server/lib/harness/acp/v2.test.ts +144 -0
  190. package/src/server/lib/harness/acp/v2.ts +220 -0
  191. package/src/server/lib/harness/acp/verbs.ts +58 -8
  192. package/src/server/lib/harness/launch.test.ts +25 -0
  193. package/src/server/lib/harness/launch.ts +24 -10
  194. package/src/server/lib/harness/session.ts +3 -0
  195. package/src/server/lib/harness/spawn.server.ts +15 -2
  196. package/src/server/lib/harness/update-job-store.test.ts +151 -0
  197. package/src/server/lib/harness/update-job-store.ts +115 -0
  198. package/src/server/lib/harness/update-job.server.ts +94 -0
  199. package/src/server/lib/harness/update-queue.server.ts +292 -0
  200. package/src/server/lib/harness/update-queue.test.ts +265 -0
  201. package/src/server/lib/harness/update-spawn.server.ts +96 -0
  202. package/src/server/lib/harness/update-worker.test.ts +153 -0
  203. package/src/server/lib/harness/update-worker.ts +202 -0
  204. package/src/server/lib/lifecycle/after-listen.ts +9 -0
  205. package/src/server/lib/lifecycle/paths.ts +28 -0
  206. package/src/server/lib/omp/rpc/manager.ts +5 -5
  207. package/src/server/lib/omp/rpc/prewarm-pool.ts +12 -3
  208. package/src/server/lib/omp/rpc/run-settle.server.ts +10 -1
  209. package/src/server/lib/omp/rpc/session-registry.ts +14 -2
  210. package/src/server/lib/omp/rpc/subagent-liveness.ts +5 -5
  211. package/src/server/lib/omp/rpc/terminal-settle.ts +58 -6
  212. package/src/server/lib/omp/session/delete-flow.server.ts +48 -0
  213. package/src/server/lib/omp/session/delete.server.ts +5 -0
  214. package/src/server/lib/omp/session/rewind-file.ts +39 -21
  215. package/src/server/lib/omp/session/sidebar-data.server.ts +16 -18
  216. package/src/server/lib/omp/session/transcript-copy.ts +123 -0
  217. package/src/server/lib/sessions/agent-token.server.test.ts +64 -0
  218. package/src/server/lib/sessions/agent-token.server.ts +134 -0
  219. package/src/server/lib/sessions/answers.server.test.ts +33 -0
  220. package/src/server/lib/sessions/answers.server.ts +37 -0
  221. package/src/server/lib/sessions/dispatch.server.test.ts +143 -0
  222. package/src/server/lib/sessions/dispatch.server.ts +128 -0
  223. package/src/server/lib/sessions/fork.server.test.ts +277 -0
  224. package/src/server/lib/sessions/fork.server.ts +329 -0
  225. package/src/server/lib/sessions/lifecycle.server.test.ts +75 -0
  226. package/src/server/lib/sessions/lifecycle.server.ts +71 -0
  227. package/src/server/lib/sessions/notify.server.test.ts +112 -0
  228. package/src/server/lib/sessions/notify.server.ts +212 -0
  229. package/src/server/lib/sessions/relation-types.ts +76 -0
  230. package/src/server/lib/sessions/relations.server.test.ts +207 -0
  231. package/src/server/lib/sessions/relations.server.ts +337 -0
  232. package/src/server/lib/sessions/resolve.server.ts +106 -0
  233. package/src/server/lib/sessions/schema.server.ts +82 -0
  234. package/src/server/lib/sessions/sidebar-relations.server.test.ts +61 -0
  235. package/src/server/lib/sessions/sidebar-relations.server.ts +47 -0
  236. package/src/server/lib/sessions/spawn.server.test.ts +237 -0
  237. package/src/server/lib/sessions/spawn.server.ts +335 -0
  238. package/src/server/lib/terminal/attach-replay.test.ts +2 -2
  239. package/src/server/lib/terminal/output.test.ts +2 -2
  240. package/src/server/lib/terminal/output.ts +1 -1
  241. package/src/server/lib/terminal/resize.test.ts +99 -0
  242. package/src/server/lib/terminal/runtime.server.ts +26 -6
  243. package/src/server/lib/terminal/send-result.test.ts +18 -1
  244. package/src/server/lib/terminal/send-result.ts +26 -2
  245. package/src/server/lib/terminal/session.server.ts +6 -6
  246. package/src/server/lib/usage/new-api.test.ts +92 -0
  247. package/src/server/lib/usage/new-api.ts +131 -0
  248. package/src/server/lib/usage/provider-endpoint.test.ts +106 -0
  249. package/src/server/lib/usage/provider-key.ts +33 -0
  250. package/src/server/lib/usage/providers.server.ts +10 -3
  251. package/src/server/lib/usage/vendor-balance.server.ts +4 -53
  252. package/src/server/lib/usage/vendor-http.ts +41 -0
  253. package/src/server/lib/usage/window.ts +55 -0
  254. package/src/server/routes/agent/command.ts +14 -1
  255. package/src/server/routes/agent/harness-command.ts +12 -5
  256. package/src/server/routes/agent/new.ts +15 -0
  257. package/src/server/routes/chat/stream.ts +7 -2
  258. package/src/server/routes/harness/add-provider.test.ts +9 -1
  259. package/src/server/routes/harness/import-action.ts +4 -0
  260. package/src/server/routes/harness/import.test.ts +1 -1
  261. package/src/server/routes/harness/index.ts +5 -1
  262. package/src/server/routes/harness/update-status.ts +32 -0
  263. package/src/server/routes/harness/update.test.ts +156 -0
  264. package/src/server/routes/harness/update.ts +64 -147
  265. package/src/server/routes/sessions/answer.ts +24 -0
  266. package/src/server/routes/sessions/batch-delete.ts +11 -5
  267. package/src/server/routes/sessions/body-readers.ts +43 -0
  268. package/src/server/routes/sessions/delete.ts +4 -2
  269. package/src/server/routes/sessions/fork.ts +89 -0
  270. package/src/server/routes/sessions/index.ts +14 -0
  271. package/src/server/routes/sessions/relations.ts +85 -0
  272. package/src/server/routes/sessions/routes.test.ts +1 -2
  273. package/src/server/routes/sessions/session.ts +23 -0
  274. package/src/server/routes/sessions/spawn.ts +137 -0
  275. package/src/server/routes/terminal/ws.ts +3 -3
  276. package/src/shared/lib/chat/composer/client.ts +31 -2
  277. package/src/shared/lib/chat/composer/delegation.test.ts +93 -0
  278. package/src/shared/lib/chat/composer/delegation.ts +206 -0
  279. package/src/shared/lib/chat/notice-text.ts +1 -1
  280. package/src/shared/lib/chat/omp/agent-events.ts +11 -0
  281. package/src/shared/lib/chat/omp/session-modes-signal.test.ts +41 -0
  282. package/src/shared/lib/chat/tool/summary-wait.test.ts +11 -0
  283. package/src/shared/lib/code/ansi.ts +1 -1
  284. package/src/shared/lib/fonts/nerd-symbols.css +4 -3
  285. package/src/shared/lib/fonts/nerd-symbols.test.ts +9 -9
  286. package/src/shared/lib/harness/mode-signal.ts +20 -0
  287. package/src/shared/lib/harness/provider-facts.test.ts +27 -0
  288. package/src/shared/lib/harness/providers.ts +35 -1
  289. package/src/shared/lib/harness/session-modes.ts +24 -0
  290. package/src/shared/lib/harness/update-job.ts +80 -0
  291. package/src/shared/lib/harness/update-plan.test.ts +21 -6
  292. package/src/shared/lib/harness/update-plan.ts +12 -4
  293. package/src/shared/lib/icons/tint-hues.generated.ts +34 -34
  294. package/src/shared/lib/notifications/outcomes.test.ts +96 -0
  295. package/src/shared/lib/notifications/outcomes.ts +64 -0
  296. package/src/shared/lib/notifications/settings.test.ts +49 -0
  297. package/src/shared/lib/notifications/settings.ts +34 -0
  298. package/src/shared/lib/omp/session/default-title.ts +5 -4
  299. package/src/shared/lib/omp/session/prewarm-misc.test.ts +3 -2
  300. package/src/shared/lib/omp/session/schema-migrations.ts +4 -0
  301. package/src/shared/lib/omp/session/stream-state.server.test.ts +19 -0
  302. package/src/shared/lib/omp/session/stream-state.server.ts +24 -4
  303. package/src/shared/lib/omp/session/wait-idle.test.ts +11 -0
  304. package/src/shared/lib/omp/session/wait-idle.ts +12 -2
  305. package/src/shared/lib/settings/defaults.ts +3 -0
  306. package/src/shared/lib/theme/catalog.test.ts +13 -9
  307. package/src/shared/lib/theme/color.test.ts +2 -2
  308. package/src/shared/lib/theme/color.ts +4 -4
  309. package/src/shared/lib/theme/theme.test.ts +1 -1
  310. package/src/shared/lib/workspace/terminal/input.test.ts +27 -0
  311. package/src/shared/lib/workspace/terminal/input.ts +33 -0
  312. package/src/shared/lib/workspace/terminal/keys.test.ts +1 -1
  313. package/src/shared/lib/workspace/terminal/keys.ts +7 -7
  314. package/src/shared/types/chat.ts +1 -1
  315. package/src/shared/types/harness-update.ts +89 -0
  316. package/src/shared/types/omp/agent.ts +19 -2
  317. package/src/shared/types/realtime.ts +2 -0
  318. package/src/shared/types/settings/state.ts +10 -0
  319. package/src/shared/types/workspace.ts +8 -0
  320. package/src/test-support/fake-omp-fork.ts +16 -2
  321. package/src/test-support/harness-table.test-util.ts +31 -1
  322. package/dist/client/static/js/index-z3w7n85q.css +0 -1
  323. package/src/client/components/common/subagent-list/index.tsx +0 -165
  324. package/src/client/components/workspace/terminal-panel/webgl.test.ts +0 -75
  325. package/src/client/components/workspace/terminal-panel/webgl.ts +0 -108
@@ -4,49 +4,61 @@
4
4
  */
5
5
 
6
6
  /**
7
- * Mounts an xterm instance on a container and keeps it bound to a PTY.
7
+ * Mounts a ghostty-web terminal on a container and keeps it bound to a PTY.
8
8
  *
9
- * Extracted from the view component so the mount lifecycle (xterm import, fit
9
+ * Extracted from the view component so the mount lifecycle (WASM import, fit
10
10
  * addon, input wiring, observers) stays readable and the component remains a
11
11
  * render layer. Everything the caller must know travels through `callbacks`;
12
12
  * the returned handle is the only way back in.
13
13
  *
14
- * Two behaviours here are deliberate:
14
+ * Three behaviours here are deliberate:
15
15
  *
16
16
  * - **Attach waits for a real grid.** The terminal panel stays mounted while
17
17
  * its view is CSS-hidden (that is what keeps a shell alive across panel
18
- * switches), and a hidden container measures 0px — fitting is skipped and
19
- * xterm keeps its 80x24 default. Attaching then would create the shell at a
20
- * size the panel never had, and the retained scrollback would replay at that
21
- * width forever. So attach is driven by the same ResizeObserver that detects
22
- * the panel becoming visible.
23
- * - **Raw bytes in, raw bytes out.** `onData`/`onBinary` forward keystrokes
24
- * untouched, and `write` takes a `Uint8Array` so xterm's streaming UTF-8
25
- * decoder reassembles a character split across two PTY reads.
26
- * - **xterm and the WebGL addon are dynamically imported on purpose.** xterm is
27
- * ~300 KB and the addon pulls a WebGL context into the panel; both stay out
28
- * of the initial client bundle until the terminal panel is actually opened.
29
- * The module shape varies by build, hence the probe pattern below.
18
+ * switches), and a hidden container measures 0px — fitting is skipped and the
19
+ * terminal keeps its 80x24 default. Attaching then would create the shell at
20
+ * a size the panel never had, and the retained scrollback would replay at
21
+ * that width forever. So attach is driven by the same ResizeObserver that
22
+ * detects the panel becoming visible.
23
+ * - **Raw bytes in, raw bytes out.** ghostty-web's WASM parser accepts a
24
+ * `Uint8Array`, so a character split across two PTY reads is reassembled by
25
+ * the parser rather than by a decode step here. `onData` text is forwarded
26
+ * unless it is a legacy X10 mouse report, which is a latin-1 byte payload
27
+ * (see `shared/lib/workspace/terminal/input.ts`).
28
+ * - **The renderer is dynamically imported on purpose.** ghostty-web is
29
+ * ~700 KB and is only needed once the terminal panel is opened, so it stays
30
+ * out of the initial client bundle.
31
+ *
32
+ * A theme change cannot recolor a running terminal: the WASM palette is fixed
33
+ * when the terminal is created and ANSI cells carry resolved RGB. `setTheme`
34
+ * therefore rebuilds the terminal with the new palette and clears it; the
35
+ * caller reattaches the socket so the server replays the retained scrollback.
36
+ * `clear()` after every open/reset is not cosmetic — the WASM allocator reuses
37
+ * pages and can hand a fresh terminal the previous screen's cells.
30
38
  */
31
39
 
32
- import type { Terminal } from '@xterm/xterm';
33
- import type { FitAddon } from '@xterm/addon-fit';
34
- import { createWebglControl } from '@/client/components/workspace/terminal-panel/webgl';
40
+ import type { FitAddon, Ghostty, Terminal } from 'ghostty-web';
41
+ import { terminalInputFrame } from '@/shared/lib/workspace/terminal/input';
42
+ import {
43
+ installCellMetrics,
44
+ waitForTerminalFonts,
45
+ watchTerminalFonts,
46
+ } from '@/client/components/workspace/terminal-panel/fonts';
47
+ import { installFullWidthFit } from '@/client/components/workspace/terminal-panel/fit';
48
+ import { createGridAnnouncer } from '@/client/components/workspace/terminal-panel/grid';
49
+ import { attachTouchScroll, handleKeyNavigation } from '@/client/components/workspace/terminal-panel/navigation';
35
50
  import {
36
- getXtermTheme,
37
- NERD_SYMBOLS_FAMILY,
38
- XTERM_FONT_FAMILY,
39
- safePatchFitAddon,
40
- safePatchRenderService,
41
- type XtermCore,
42
- type XtermModule,
51
+ getTerminalTheme,
52
+ TERMINAL_FONT_FAMILY,
53
+ TERMINAL_FONT_SIZE,
54
+ TERMINAL_SCROLLBACK_BYTES,
43
55
  } from '@/client/data/theme/terminal';
44
56
 
45
- export interface XtermMountCallbacks {
57
+ export interface TerminalMountCallbacks {
46
58
  /** Keystrokes, paste, mouse reports — straight to the PTY. */
47
59
  onInput: (data: string) => void;
48
- /** Non-UTF-8 payloads xterm reports separately (some mouse reports). */
49
- onBinaryInput: (data: string) => void;
60
+ /** Non-UTF-8 payloads (legacy X10 mouse reports), as latin-1 bytes. */
61
+ onBinaryInput: (bytes: Uint8Array) => void;
50
62
  /** The grid changed; the PTY must be resized to match. */
51
63
  onGridChange: (cols: number, rows: number) => void;
52
64
  /** The terminal is measurable and knows its size; attach with these. */
@@ -55,126 +67,163 @@ export interface XtermMountCallbacks {
55
67
  onScrolledUpChange: (scrolledUp: boolean) => void;
56
68
  }
57
69
 
58
- export interface XtermMount {
59
- term: Terminal;
60
- fitAddon: FitAddon;
70
+ export interface TerminalMount {
71
+ /** Raw PTY output. Bytes, not text: the WASM parser reassembles them. */
72
+ write: (bytes: Uint8Array) => void;
61
73
  /**
62
74
  * Lay the retained scrollback out at the size it was drawn for, then fit the
63
75
  * panel. Grid reports are suppressed until the replay has been consumed.
64
76
  */
65
77
  writeReplay: (bytes: Uint8Array, cols: number, rows: number) => void;
78
+ /** Drop the buffer and reset the cursor — used when the shell is replaced. */
79
+ reset: () => void;
80
+ focus: () => void;
81
+ /** The shell's DECCKM state, read when a key-bar button is pressed. */
82
+ applicationCursorKeys: () => boolean;
83
+ /** The current selection, empty when nothing is selected. */
84
+ getSelection: () => string;
85
+ /** Feed text in as a paste, with bracketed-paste markers when requested. */
86
+ paste: (text: string) => void;
87
+ scrollToBottom: () => void;
66
88
  /**
67
- * Hold or release the WebGL context. Releasing falls back to xterm's DOM
68
- * renderer, so the buffer stays readable while the panel is hidden; holding
69
- * again re-creates the context on the next shown frame.
89
+ * Rebuild the terminal with another palette. The caller must reattach the
90
+ * socket afterwards: the rebuilt terminal has no scrollback of its own.
70
91
  */
71
- setWebgl: (enabled: boolean) => void;
92
+ setTheme: (themeId: string) => void;
72
93
  dispose: () => void;
73
94
  }
74
95
 
96
+ interface TerminalInstance {
97
+ term: Terminal;
98
+ fitAddon: FitAddon;
99
+ disposables: { dispose(): void }[];
100
+ }
101
+
75
102
  /**
76
- * Mount xterm inside `container`. Resolves to null when the module could not be
77
- * loaded or the component unmounted while importing.
103
+ * Mount the terminal inside `container`. Resolves to null when the module could
104
+ * not be loaded or the component unmounted while importing.
78
105
  */
79
- export async function mountXterm(
106
+ export async function mountTerminal(
80
107
  container: HTMLElement,
81
108
  themeId: string,
82
- callbacks: XtermMountCallbacks,
109
+ callbacks: TerminalMountCallbacks,
83
110
  isCancelled: () => boolean,
84
- ): Promise<XtermMount | null> {
85
- // Dynamic on purpose: xterm is ~300 KB and is only needed once the terminal
86
- // panel is actually opened, so it stays out of the initial client bundle
87
- // (the module shape varies by build, hence the probes).
88
- const xtermModule = (await import('@xterm/xterm')) as unknown as XtermModule<typeof Terminal>;
89
- const fitModule = (await import('@xterm/addon-fit')) as unknown as XtermModule<typeof FitAddon>;
90
-
91
- const TerminalClass = xtermModule.Terminal || xtermModule.default?.Terminal || xtermModule.default;
92
- const FitAddonClass = fitModule.FitAddon || fitModule.default?.FitAddon || fitModule.default;
93
- if (!TerminalClass || !FitAddonClass || isCancelled()) return null;
94
-
95
- safePatchFitAddon(FitAddonClass);
111
+ ): Promise<TerminalMount | null> {
112
+ // One dynamic import, and the WASM module is loaded once per page. Fonts are
113
+ // fetched in parallel: the renderer measures its cell box at `open()`, and a
114
+ // face that lands after that is repaired by `watchTerminalFonts`.
115
+ // Dynamic on purpose: ghostty-web and its ~424 KB WASM are only needed once
116
+ // the terminal panel is opened, so a static import would put them in the
117
+ // initial client bundle for every page.
118
+ const module = await import('ghostty-web');
119
+ const [ghostty] = await Promise.all([module.Ghostty.load(), waitForTerminalFonts()]);
120
+ if (isCancelled()) return null;
96
121
 
97
- const term = new TerminalClass({
98
- cursorBlink: true,
99
- cursorStyle: 'block',
100
- fontSize: 12,
101
- lineHeight: 1.25,
102
- fontFamily: XTERM_FONT_FAMILY,
103
- theme: getXtermTheme(themeId),
104
- // No `convertEol`: the PTY already emits CRLF, and converting a lone LF
105
- // would re-introduce the line-feed doubling a real terminal avoids.
106
- scrollback: 10000,
107
- scrollSensitivity: 1.5,
108
- fastScrollSensitivity: 5,
109
- smoothScrollDuration: 0,
110
- allowProposedApi: true,
111
- }) as Terminal;
112
-
113
- term.open(container);
114
- safePatchRenderService(term);
115
-
116
- // WebGL renderer: powerline glyphs stay seamless, and the context is held only
117
- // while this panel is on screen (`webgl.ts` owns that decision and the numbers
118
- // behind it — 541.8 MB of graphics in an 817.7 MB footprint). It is attached by
119
- // the caller, which is the only thing that knows whether this view is the one
120
- // being looked at.
121
- const webgl = await createWebglControl(term);
122
-
123
- const fitAddon = new FitAddonClass() as FitAddon;
124
- term.loadAddon(fitAddon);
122
+ let currentThemeId = themeId;
123
+ let current: TerminalInstance | null = null;
124
+ let replaying = false;
125
+ let fitTimer: ReturnType<typeof setTimeout> | undefined;
126
+ let frame: number | null = null;
127
+ let disposed = false;
125
128
 
126
- // The bundled Nerd Font symbols face may still be in flight (a phone has no
127
- // Nerd Font of its own). See the helper for why the atlas has to be dropped
128
- // once it lands.
129
- const stopNerdFaceWatch = refreshAtlasWhenNerdFaceLoads(term);
129
+ const wire = (instance: TerminalInstance): void => {
130
+ const { term } = instance;
131
+ // ghostty-web's custom handler returns TRUE to consume the event, the
132
+ // inverse of xterm's contract. `handleKeyNavigation` is written for this
133
+ // one.
134
+ term.attachCustomKeyEventHandler((event: globalThis.KeyboardEvent) => handleKeyNavigation(term, event));
135
+ instance.disposables.push(
136
+ term.onScroll((viewportY: number) => callbacks.onScrolledUpChange(viewportY > 0)),
137
+ term.onData((data: string) => {
138
+ const frame = terminalInputFrame(data);
139
+ if (frame.bytes) callbacks.onBinaryInput(frame.bytes);
140
+ else callbacks.onInput(frame.text);
141
+ }),
142
+ // FitAddon changes the grid on fit; the shell has to hear about it or a
143
+ // TUI keeps drawing at the old width. The report goes through the settle
144
+ // gate in `grid.ts` — a fit during the panel's opening animation is a
145
+ // squeezed grid, not a size to send. Reports are dropped while the replay
146
+ // is being laid out: that grid belongs to the recorded history, not the
147
+ // panel.
148
+ term.onResize(() => {
149
+ if (replaying) return;
150
+ grid.schedule();
151
+ }),
152
+ );
153
+ };
130
154
 
131
- term.attachCustomKeyEventHandler((event: globalThis.KeyboardEvent) => handleKeyNavigation(term, event));
155
+ const create = (nextThemeId: string): TerminalInstance => {
156
+ const term = new module.Terminal({
157
+ cursorBlink: true,
158
+ cursorStyle: 'block',
159
+ fontSize: TERMINAL_FONT_SIZE,
160
+ fontFamily: TERMINAL_FONT_FAMILY,
161
+ theme: getTerminalTheme(nextThemeId),
162
+ // A byte budget, not xterm's line count. See `theme/terminal.ts`.
163
+ scrollback: TERMINAL_SCROLLBACK_BYTES,
164
+ // No `convertEol`: the PTY already emits CRLF, and converting a lone LF
165
+ // would re-introduce the line-feed doubling a real terminal avoids.
166
+ smoothScrollDuration: 0,
167
+ ghostty: ghostty as Ghostty,
168
+ });
169
+ term.open(container);
170
+ installCellMetrics(term);
171
+ // The WASM allocator reuses pages: a fresh terminal can inherit the previous
172
+ // screen's cells until something overwrites them. Clear before the first
173
+ // byte is shown.
174
+ term.clear();
175
+ const fitAddon = new module.FitAddon();
176
+ term.loadAddon(fitAddon);
177
+ installFullWidthFit(fitAddon, term);
178
+ const instance: TerminalInstance = { term, fitAddon, disposables: [] };
179
+ wire(instance);
180
+ return instance;
181
+ };
132
182
 
133
- const disposables = [
134
- term.onScroll(() => {
135
- const buffer = term.buffer.active;
136
- callbacks.onScrolledUpChange(buffer.viewportY < buffer.baseY);
137
- }),
138
- term.onData((data: string) => callbacks.onInput(data)),
139
- term.onBinary((data: string) => callbacks.onBinaryInput(data)),
140
- // FitAddon changes the grid on fit; the shell has to hear about it or a TUI
141
- // keeps drawing at the old width. Reports are dropped while the replay is
142
- // being laid out: that grid belongs to the recorded history, not the panel.
143
- term.onResize(({ cols, rows }) => {
144
- if (replaying) return;
145
- callbacks.onGridChange(cols, rows);
146
- }),
147
- ];
183
+ const disposeInstance = (instance: TerminalInstance | null): void => {
184
+ if (!instance) return;
185
+ for (const disposable of instance.disposables) {
186
+ try {
187
+ disposable.dispose();
188
+ } catch {}
189
+ }
190
+ try {
191
+ instance.fitAddon.dispose();
192
+ } catch {}
193
+ try {
194
+ instance.term.dispose();
195
+ } catch {}
196
+ };
148
197
 
149
- let attached = false;
150
- let fitTimer: ReturnType<typeof setTimeout> | null = null;
151
- let frame: number | null = null;
152
198
  /**
153
- * Set while the retained scrollback is being laid out at its recorded size.
154
- * That transient grid is not the panel's, so it must not be reported as a
155
- * resize — the shell would answer with a prompt repaint for a size the user
156
- * never chose, which is how a reattach used to print a second prompt.
199
+ * Fit to the container. Returns false when no fit was possible — the panel is
200
+ * hidden, or the cell has not been measured yet — which is what keeps the
201
+ * terminal's 80×24 default from ever being announced as a grid.
157
202
  */
158
- let replaying = false;
159
-
160
- const safeFit = () => {
161
- if (isCancelled()) return;
162
- if (container.clientWidth <= 0 || container.clientHeight <= 0) return;
163
- const core = (term as unknown as XtermCore)._core;
164
- const dims = core?._renderService?.dimensions;
165
- if (!dims?.css?.cell?.width || !dims?.css?.cell?.height) return;
203
+ const safeFit = (): boolean => {
204
+ if (disposed) return false;
205
+ if (container.clientWidth <= 0 || container.clientHeight <= 0) return false;
206
+ const metrics = current?.term.renderer?.getMetrics();
207
+ if (!metrics?.width || !metrics.height) return false;
166
208
  try {
167
- fitAddon.fit();
168
- } catch {}
209
+ current?.fitAddon.fit();
210
+ return true;
211
+ } catch {
212
+ return false;
213
+ }
169
214
  };
170
215
 
171
- /** Fit now, once xterm has consumed everything already queued. */
172
- const fitAfterWrites = () => {
173
- term.write('', () => {
174
- if (isCancelled()) return;
175
- replaying = false;
176
- safeFit();
177
- });
216
+ /** Fit now, once the terminal has consumed everything already queued. */
217
+ const finishReplay = (): void => {
218
+ if (disposed) return;
219
+ replaying = false;
220
+ safeFit();
221
+ // The replay laid the terminal out at the recorded grid, so the panel's own
222
+ // size has to be stated again: a resize the server already applied is a
223
+ // no-op, and one it missed (a mount that measured a squeezed panel) is
224
+ // corrected here instead of at the next page load.
225
+ grid.restate();
226
+ grid.schedule();
178
227
  };
179
228
 
180
229
  /**
@@ -182,137 +231,112 @@ export async function mountXterm(
182
231
  * on the panel's grid. Re-wrapping history at another width leaves wrapped
183
232
  * fragments the shell's own redraw never clears.
184
233
  */
185
- const writeReplay = (bytes: Uint8Array, cols: number, rows: number) => {
234
+ const writeReplay = (bytes: Uint8Array, cols: number, rows: number): void => {
235
+ const term = current?.term;
236
+ if (!term) return;
186
237
  replaying = true;
187
238
  if (term.cols !== cols || term.rows !== rows) term.resize(cols, rows);
188
239
  if (bytes.length === 0) {
189
- fitAfterWrites();
240
+ // ghostty-web's `write('')` throws: its zero-length allocation is not a
241
+ // valid TypedArray offset. Finish on the next frame instead.
242
+ requestAnimationFrame(finishReplay);
190
243
  return;
191
244
  }
192
- term.write(bytes, fitAfterWrites);
245
+ term.write(bytes, finishReplay);
193
246
  };
194
247
 
195
- const attachIfSized = () => {
196
- if (attached || isCancelled()) return;
197
- if (container.clientWidth <= 0 || container.clientHeight <= 0) return;
198
- const core = (term as unknown as XtermCore)._core;
199
- if (!core?._renderService?.dimensions?.css?.cell?.width) return;
200
- attached = true;
201
- callbacks.onReady(term.cols, term.rows);
202
- term.focus();
203
- };
248
+ const grid = createGridAnnouncer({
249
+ current: () => current?.term,
250
+ fit: safeFit,
251
+ onReady: (cols, rows) => {
252
+ callbacks.onReady(cols, rows);
253
+ current?.term.focus();
254
+ },
255
+ onGridChange: callbacks.onGridChange,
256
+ });
257
+
258
+ current = create(currentThemeId);
259
+ safeFit();
260
+ grid.schedule();
204
261
 
205
262
  const resizeObserver = new ResizeObserver(() => {
206
- if (fitTimer) clearTimeout(fitTimer);
263
+ clearTimeout(fitTimer);
207
264
  fitTimer = setTimeout(() => {
208
265
  safeFit();
209
266
  // A panel that was hidden at mount becomes measurable here, which is when
210
267
  // the shell should be created — at its real size.
211
- attachIfSized();
268
+ grid.schedule();
212
269
  }, 35);
213
270
  });
214
271
  resizeObserver.observe(container);
215
272
 
216
- const touch = attachTouchScroll(container, term);
273
+ const stopFontWatch = watchTerminalFonts(() => {
274
+ const term = current?.term;
275
+ if (!term) return;
276
+ const size = term.options.fontSize ?? TERMINAL_FONT_SIZE;
277
+ // ghostty-web measures once; a font-size round-trip is its public way to
278
+ // re-measure and resize the canvas.
279
+ term.options.fontSize = size + 0.1;
280
+ term.options.fontSize = size;
281
+ safeFit();
282
+ grid.schedule();
283
+ });
284
+
285
+ const touch = attachTouchScroll(container, () => current?.term ?? null);
217
286
 
218
287
  // The container may already be measurable (the view was active at mount), in
219
288
  // which case the observer never fires a first time.
220
289
  frame = requestAnimationFrame(() => {
221
290
  safeFit();
222
- attachIfSized();
291
+ grid.schedule();
223
292
  });
224
293
 
225
294
  return {
226
- term,
227
- fitAddon,
295
+ write(bytes: Uint8Array) {
296
+ current?.term.write(bytes);
297
+ },
228
298
  writeReplay,
229
- setWebgl: webgl.set,
299
+ reset() {
300
+ const term = current?.term;
301
+ if (!term) return;
302
+ term.reset();
303
+ // The rebuilt WASM terminal reuses pages; see `create`.
304
+ term.clear();
305
+ safeFit();
306
+ },
307
+ focus() {
308
+ current?.term.focus();
309
+ },
310
+ applicationCursorKeys() {
311
+ return current?.term.wasmTerm?.getMode(1, false) ?? false;
312
+ },
313
+ getSelection() {
314
+ return current?.term.getSelection() ?? '';
315
+ },
316
+ paste(text: string) {
317
+ current?.term.paste(text);
318
+ },
319
+ scrollToBottom() {
320
+ current?.term.scrollToBottom();
321
+ },
322
+ setTheme(nextThemeId: string) {
323
+ if (disposed || nextThemeId === currentThemeId) return;
324
+ currentThemeId = nextThemeId;
325
+ disposeInstance(current);
326
+ current = create(nextThemeId);
327
+ safeFit();
328
+ },
230
329
  dispose() {
330
+ disposed = true;
231
331
  if (frame !== null) cancelAnimationFrame(frame);
232
- clearTimeout(fitTimer ?? undefined);
233
- stopNerdFaceWatch();
332
+ clearTimeout(fitTimer);
333
+ grid.dispose();
334
+ stopFontWatch();
234
335
  touch();
235
- for (const disposable of disposables) disposable.dispose();
236
336
  resizeObserver.disconnect();
237
- try {
238
- fitAddon.dispose();
239
- } catch {}
240
- try {
241
- term.dispose();
242
- } catch {}
337
+ disposeInstance(current);
338
+ current = null;
243
339
  },
244
340
  };
245
341
  }
246
342
 
247
- /**
248
- * Re-rasterize the terminal once the bundled symbols face has loaded.
249
- *
250
- * xterm has no `document.fonts` listener: it rasterizes a glyph the first time
251
- * it draws it and keeps the result in a texture atlas, so a PUA glyph written
252
- * before the face arrived stays whatever the fallback painted — a tofu box —
253
- * for the life of the buffer. The browser does re-lay-out DOM text when a face
254
- * lands late (verified on the DOM renderer), but xterm's canvas does not get
255
- * that for free.
256
- *
257
- * `loadingdone` naming our own family is exactly the signal that this machine
258
- * had no Nerd Font of its own: the face is declared last in the stack and its
259
- * `unicode-range` covers only PUA, so a device with an installed Nerd Font
260
- * never fetches it (verified: zero requests with a local family present). That
261
- * makes this a no-op on the developer's laptop and the fix on a phone.
262
- *
263
- * `clearTextureAtlas()` drops the atlas and forces a full refresh, which is
264
- * what re-rasterizes the glyphs. It is a public API on `Terminal`, and the
265
- * atlas is small enough that a full refresh is cheap — this runs at most once
266
- * per page load.
267
- *
268
- * Returns a disposer.
269
- */
270
- function refreshAtlasWhenNerdFaceLoads(term: Terminal): () => void {
271
- if (typeof document === 'undefined' || !document.fonts) return () => {};
272
-
273
- const onLoadingDone = (event: FontFaceSetLoadEvent) => {
274
- const loaded = [...event.fontfaces].some((face) => face.family.replace(/['"]/g, '') === NERD_SYMBOLS_FAMILY);
275
- if (!loaded) return;
276
- try {
277
- term.clearTextureAtlas();
278
- } catch {
279
- // A disposed terminal has no render service; nothing to refresh.
280
- }
281
- };
282
-
283
- document.fonts.addEventListener('loadingdone', onLoadingDone);
284
- return () => document.fonts.removeEventListener('loadingdone', onLoadingDone);
285
- }
286
-
287
- /** Shift+PageUp/Home/End scroll the viewport instead of reaching the shell. */
288
- function handleKeyNavigation(term: Terminal, event: globalThis.KeyboardEvent): boolean {
289
- if (event.type !== 'keydown' || !event.shiftKey) return true;
290
- if (event.key === 'PageUp') { term.scrollPages(-1); return false; }
291
- if (event.key === 'PageDown') { term.scrollPages(1); return false; }
292
- if (event.key === 'Home') { term.scrollToTop(); return false; }
293
- if (event.key === 'End') { term.scrollToBottom(); return false; }
294
- if (event.key === 'ArrowUp') { term.scrollLines(-1); return false; }
295
- if (event.key === 'ArrowDown') { term.scrollLines(1); return false; }
296
- return true;
297
- }
298
-
299
- /** Touch drags scroll the scrollback; xterm has no touch handling of its own. */
300
- function attachTouchScroll(container: HTMLElement, term: Terminal): () => void {
301
- let startY = 0;
302
- const onStart = (event: globalThis.TouchEvent) => {
303
- if (event.touches.length === 1) startY = event.touches[0].clientY;
304
- };
305
- const onMove = (event: globalThis.TouchEvent) => {
306
- if (event.touches.length !== 1) return;
307
- const deltaY = startY - event.touches[0].clientY;
308
- if (Math.abs(deltaY) < 16) return;
309
- term.scrollLines(Math.trunc(deltaY / 16));
310
- startY = event.touches[0].clientY;
311
- };
312
- container.addEventListener('touchstart', onStart, { passive: true });
313
- container.addEventListener('touchmove', onMove, { passive: true });
314
- return () => {
315
- container.removeEventListener('touchstart', onStart);
316
- container.removeEventListener('touchmove', onMove);
317
- };
318
- }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { describe, expect, test } from 'bun:test';
7
+ import type { Terminal } from 'ghostty-web';
8
+ import { handleKeyNavigation } from '@/client/components/workspace/terminal-panel/navigation';
9
+
10
+ function fakeTerminal() {
11
+ const calls: string[] = [];
12
+ const term = {
13
+ scrollPages: (amount: number) => calls.push(`pages:${amount}`),
14
+ scrollLines: (amount: number) => calls.push(`lines:${amount}`),
15
+ scrollToTop: () => calls.push('top'),
16
+ scrollToBottom: () => calls.push('bottom'),
17
+ } as unknown as Terminal;
18
+ return { term, calls };
19
+ }
20
+
21
+ interface KeyInit extends KeyboardEventInit {
22
+ type: string;
23
+ key: string;
24
+ }
25
+
26
+ function key(init: KeyInit): globalThis.KeyboardEvent {
27
+ // The helpers read only `type`, `key` and `shiftKey`; a full DOM event is
28
+ // unavailable under Bun.
29
+ return init as unknown as globalThis.KeyboardEvent;
30
+ }
31
+
32
+ describe('handleKeyNavigation', () => {
33
+ test('consumes Shift+PageUp and scrolls the viewport', () => {
34
+ const { term, calls } = fakeTerminal();
35
+ expect(handleKeyNavigation(term, key({ type: 'keydown', key: 'PageUp', shiftKey: true }))).toBe(true);
36
+ expect(calls).toEqual(['pages:-1']);
37
+ });
38
+
39
+ test('lets an unmodified key reach the terminal encoder', () => {
40
+ const { term, calls } = fakeTerminal();
41
+ expect(handleKeyNavigation(term, key({ type: 'keydown', key: 'PageUp' }))).toBe(false);
42
+ expect(handleKeyNavigation(term, key({ type: 'keydown', key: 'a', shiftKey: true }))).toBe(false);
43
+ expect(calls).toEqual([]);
44
+ });
45
+
46
+ test('Shift+Home/End/Arrows are viewport gestures, not shell input', () => {
47
+ const { term, calls } = fakeTerminal();
48
+ handleKeyNavigation(term, key({ type: 'keydown', key: 'Home', shiftKey: true }));
49
+ handleKeyNavigation(term, key({ type: 'keydown', key: 'End', shiftKey: true }));
50
+ handleKeyNavigation(term, key({ type: 'keydown', key: 'ArrowUp', shiftKey: true }));
51
+ handleKeyNavigation(term, key({ type: 'keydown', key: 'ArrowDown', shiftKey: true }));
52
+ expect(calls).toEqual(['top', 'bottom', 'lines:-1', 'lines:1']);
53
+ });
54
+ });
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Keyboard and touch gestures the canvas renderer does not implement.
8
+ *
9
+ * ghostty-web's custom key handler returns `true` to CONSUME an event, which is
10
+ * the inverse of xterm's contract. This module is written for ghostty's one:
11
+ * an ordinary key must return false so the terminal's own encoder sees it.
12
+ */
13
+
14
+ import type { Terminal } from 'ghostty-web';
15
+
16
+ /** Shift+PageUp/Home/End scroll the viewport instead of reaching the shell. */
17
+ export function handleKeyNavigation(term: Terminal, event: globalThis.KeyboardEvent): boolean {
18
+ if (event.type !== 'keydown' || !event.shiftKey) return false;
19
+ if (event.key === 'PageUp') { term.scrollPages(-1); return true; }
20
+ if (event.key === 'PageDown') { term.scrollPages(1); return true; }
21
+ if (event.key === 'Home') { term.scrollToTop(); return true; }
22
+ if (event.key === 'End') { term.scrollToBottom(); return true; }
23
+ if (event.key === 'ArrowUp') { term.scrollLines(-1); return true; }
24
+ if (event.key === 'ArrowDown') { term.scrollLines(1); return true; }
25
+ return false;
26
+ }
27
+
28
+ /** Touch drags scroll the scrollback; ghostty-web has no touch handling. */
29
+ export function attachTouchScroll(container: HTMLElement, getTerminal: () => Terminal | null): () => void {
30
+ let startY = 0;
31
+ const onStart = (event: globalThis.TouchEvent) => {
32
+ if (event.touches.length === 1) startY = event.touches[0].clientY;
33
+ };
34
+ const onMove = (event: globalThis.TouchEvent) => {
35
+ if (event.touches.length !== 1) return;
36
+ const deltaY = startY - event.touches[0].clientY;
37
+ if (Math.abs(deltaY) < 16) return;
38
+ getTerminal()?.scrollLines(Math.trunc(deltaY / 16));
39
+ startY = event.touches[0].clientY;
40
+ };
41
+ container.addEventListener('touchstart', onStart, { passive: true });
42
+ container.addEventListener('touchmove', onMove, { passive: true });
43
+ return () => {
44
+ container.removeEventListener('touchstart', onStart);
45
+ container.removeEventListener('touchmove', onMove);
46
+ };
47
+ }