@kabmo-labs/kabmo 0.16.0 → 0.18.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 (381) hide show
  1. package/README.md +2 -2
  2. package/dist/client/index.html +1 -1
  3. package/dist/client/index.js +552 -471
  4. package/dist/client/static/js/index-hp75vsv9.css +1 -0
  5. package/dist/client/static/js/{index-cpydmygd.js → index-tc09dtca.js} +897 -886
  6. package/package.json +1 -1
  7. package/src/client/components/common/MarkdownRenderer.tsx +136 -89
  8. package/src/client/components/common/anchored-menu/index.test.tsx +141 -0
  9. package/src/client/components/common/anchored-menu/index.tsx +143 -0
  10. package/src/client/components/common/code-surface/bracket-guide.test.ts +2 -2
  11. package/src/client/components/common/code-surface/bracket-guide.ts +2 -2
  12. package/src/client/components/common/code-surface/conflict-markers.ts +5 -5
  13. package/src/client/components/common/code-surface/extensions.ts +7 -4
  14. package/src/client/components/common/code-surface/theme.ts +34 -9
  15. package/src/client/components/common/code-surface/whitespace.test.ts +223 -0
  16. package/src/client/components/common/code-surface/whitespace.ts +140 -0
  17. package/src/client/components/common/markdown-review/ReviewPopover.tsx +144 -0
  18. package/src/client/components/common/markdown-review/ReviewSummary.tsx +108 -0
  19. package/src/client/components/common/markdown-review/index.tsx +129 -0
  20. package/src/client/components/common/{server-switcher/index.tsx → server-control/Panel.tsx} +92 -44
  21. package/src/client/components/common/server-control/index.test.tsx +217 -0
  22. package/src/client/components/common/server-control/index.tsx +120 -0
  23. package/src/client/components/layout/desktop-layout/TopNavbar.tsx +8 -50
  24. package/src/client/components/layout/session-sidebar/CategoryHeader.tsx +42 -21
  25. package/src/client/components/layout/session-sidebar/CategoryHeader.worktree.test.tsx +36 -17
  26. package/src/client/components/layout/session-sidebar/CategoryItem.tsx +8 -7
  27. package/src/client/components/layout/session-sidebar/about-modal/NotesDialog.tsx +94 -0
  28. package/src/client/components/layout/session-sidebar/about-modal/VersionLine.test.tsx +68 -0
  29. package/src/client/components/layout/session-sidebar/about-modal/VersionLine.tsx +36 -0
  30. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +57 -34
  31. package/src/client/components/layout/session-sidebar/modern/WorkspaceHeader.tsx +28 -10
  32. package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +7 -6
  33. package/src/client/components/layout/session-sidebar/modern/workspace-header.test.tsx +1 -1
  34. package/src/client/components/layout/update-popup/ReleaseNotes.tsx +8 -3
  35. package/src/client/components/layout/update-popup/index.tsx +5 -1
  36. package/src/client/components/mobile/mobile-main-view/Header.tsx +6 -12
  37. package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +13 -15
  38. package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +23 -1
  39. package/src/client/components/mobile/mobile-right-sidebar/MissingFileBanner.tsx +30 -0
  40. package/src/client/components/mobile/mobile-right-sidebar/PreviewScroller.tsx +18 -2
  41. package/src/client/components/mobile/mobile-session-sidebar/Header.tsx +41 -13
  42. package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +22 -67
  43. package/src/client/components/mobile/mobile-session-sidebar/ItemMenu.tsx +110 -0
  44. package/src/client/components/settings/Modal.tsx +9 -3
  45. package/src/client/components/settings/ProjectSelectorDropdown.tsx +19 -9
  46. package/src/client/components/settings/SettingsSidebarList.tsx +20 -0
  47. package/src/client/components/settings/categories/AgentSettings.tsx +38 -44
  48. package/src/client/components/settings/categories/CommandSettings.tsx +2 -2
  49. package/src/client/components/settings/categories/McpSettings.tsx +4 -21
  50. package/src/client/components/settings/categories/OmpSettings.tsx +147 -194
  51. package/src/client/components/settings/categories/ProjectSettings.tsx +122 -156
  52. package/src/client/components/settings/categories/SkillCatalogSettings.tsx +220 -260
  53. package/src/client/components/settings/categories/SkillSettings.tsx +8 -5
  54. package/src/client/components/settings/categories/TokenUsageSettings.tsx +211 -229
  55. package/src/client/components/settings/categories/agent-settings/DetailPane.tsx +229 -237
  56. package/src/client/components/settings/categories/agent-settings/ModelPicker.tsx +71 -0
  57. package/src/client/components/settings/categories/agent-settings/SidebarList.tsx +14 -2
  58. package/src/client/components/settings/categories/agent-settings/levels.ts +38 -0
  59. package/src/client/components/settings/categories/behavior-settings/Editor.tsx +27 -7
  60. package/src/client/components/settings/categories/chat-settings/ToolRenderSection.tsx +23 -5
  61. package/src/client/components/settings/categories/command-settings/DetailPane.tsx +75 -33
  62. package/src/client/components/settings/categories/command-settings/SidebarList.tsx +7 -0
  63. package/src/client/components/settings/categories/mcp-settings/DetailPane.tsx +91 -76
  64. package/src/client/components/settings/categories/mcp-settings/EnvVarsSection.tsx +83 -0
  65. package/src/client/components/settings/categories/mcp-settings/ImportModal.tsx +26 -13
  66. package/src/client/components/settings/categories/mcp-settings/SidebarList.tsx +20 -0
  67. package/src/client/components/settings/categories/omp-settings/ConfigTabs.tsx +97 -36
  68. package/src/client/components/settings/categories/omp-settings/EngineHeader.tsx +151 -0
  69. package/src/client/components/settings/categories/omp-settings/ExtensionsPanel.tsx +68 -32
  70. package/src/client/components/settings/categories/omp-settings/OrderedChips.tsx +119 -0
  71. package/src/client/components/settings/categories/omp-settings/PresetBar.tsx +14 -7
  72. package/src/client/components/settings/categories/omp-settings/RoleEditor.tsx +15 -6
  73. package/src/client/components/settings/categories/omp-settings/RowEditor.tsx +165 -226
  74. package/src/client/components/settings/categories/omp-settings/types.ts +28 -0
  75. package/src/client/components/settings/categories/project-settings/AccentPalette.test.tsx +189 -0
  76. package/src/client/components/settings/categories/project-settings/AccentPalette.tsx +156 -24
  77. package/src/client/components/settings/categories/project-settings/AddProjectModal.tsx +175 -0
  78. package/src/client/components/settings/categories/project-settings/DeleteProjectModal.tsx +107 -0
  79. package/src/client/components/settings/categories/project-settings/DetailsForm.tsx +40 -24
  80. package/src/client/components/settings/categories/project-settings/DetailsHeader.tsx +17 -7
  81. package/src/client/components/settings/categories/project-settings/IconGrid.tsx +164 -69
  82. package/src/client/components/settings/categories/project-settings/ModelDropdown.test.tsx +122 -0
  83. package/src/client/components/settings/categories/project-settings/ModelDropdown.tsx +124 -53
  84. package/src/client/components/settings/categories/project-settings/NameInput.tsx +57 -11
  85. package/src/client/components/settings/categories/project-settings/PathField.tsx +122 -0
  86. package/src/client/components/settings/categories/project-settings/SidebarList.tsx +100 -59
  87. package/src/client/components/settings/categories/skill-catalog/GitImportPanel.tsx +253 -0
  88. package/src/client/components/settings/categories/skill-catalog/SkillCard.tsx +89 -0
  89. package/src/client/components/settings/categories/skill-catalog/SourceRail.tsx +122 -0
  90. package/src/client/components/settings/categories/skill-catalog/catalog-ui.test.tsx +179 -0
  91. package/src/client/components/settings/categories/skill-settings/DetailHeader.tsx +236 -0
  92. package/src/client/components/settings/categories/skill-settings/DetailPane.tsx +164 -239
  93. package/src/client/components/settings/categories/skill-settings/InstructionsEditor.tsx +84 -0
  94. package/src/client/components/settings/categories/skill-settings/SidebarList.tsx +144 -87
  95. package/src/client/components/settings/categories/skill-settings/pane.test.tsx +188 -0
  96. package/src/client/components/settings/categories/skill-settings/row-meta.ts +58 -0
  97. package/src/client/components/settings/categories/token-usage-settings/Breakdown.tsx +242 -102
  98. package/src/client/components/settings/categories/token-usage-settings/Chart.tsx +130 -56
  99. package/src/client/components/settings/categories/token-usage-settings/Controls.tsx +135 -0
  100. package/src/client/components/settings/categories/token-usage-settings/MetricsGrid.tsx +55 -33
  101. package/src/client/components/settings/categories/token-usage-settings/Provenance.tsx +59 -0
  102. package/src/client/components/settings/categories/token-usage-settings/panel.test.tsx +239 -0
  103. package/src/client/components/workspace/browser-panel/Screencast.test.ts +1 -0
  104. package/src/client/components/workspace/browser-panel/Screencast.tsx +17 -22
  105. package/src/client/components/workspace/browser-panel/TabStrip.test.tsx +158 -0
  106. package/src/client/components/workspace/browser-panel/TabStrip.tsx +90 -0
  107. package/src/client/components/workspace/browser-panel/index.tsx +50 -4
  108. package/src/client/components/workspace/browser-toolbar/index.tsx +41 -1
  109. package/src/client/components/workspace/chat-timeline/ThinkingSection.tsx +3 -1
  110. package/src/client/components/workspace/chat-timeline/ToolCallingSection.tsx +2 -0
  111. package/src/client/components/workspace/chat-timeline/block-chrome.test.tsx +13 -2
  112. package/src/client/components/workspace/chat-timeline/index.tsx +6 -1
  113. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.test.ts +88 -0
  114. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.tsx +24 -1
  115. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Proc.test.ts +46 -0
  116. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Proc.tsx +15 -6
  117. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.test.ts +34 -0
  118. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.tsx +5 -1
  119. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.test.ts +29 -0
  120. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +11 -3
  121. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ArtifactChip.tsx +7 -6
  122. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/card-header.test.ts +24 -1
  123. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/card-header.ts +6 -2
  124. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.test.tsx +237 -0
  125. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.tsx +191 -0
  126. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/styles.test.tsx +1 -1
  127. package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +21 -7
  128. package/src/client/components/workspace/diff-panel/changes.test.ts +51 -0
  129. package/src/client/components/workspace/diff-panel/changes.ts +28 -12
  130. package/src/client/components/workspace/diff-panel/merge.test.ts +28 -0
  131. package/src/client/components/workspace/diff-panel/merge.ts +21 -1
  132. package/src/client/components/workspace/editor/Body.tsx +7 -0
  133. package/src/client/components/workspace/editor/FileDocument.tsx +15 -21
  134. package/src/client/components/workspace/editor/FilePane.tsx +12 -0
  135. package/src/client/components/workspace/editor/PreviewPane.tsx +70 -0
  136. package/src/client/components/workspace/editor/Toolbar.tsx +17 -1
  137. package/src/client/components/workspace/editor/index.tsx +25 -22
  138. package/src/client/components/workspace/editor/toolbar-clusters.test.ts +14 -0
  139. package/src/client/components/workspace/editor/toolbar-clusters.tsx +12 -86
  140. package/src/client/components/workspace/editor/toolbar-toggle.tsx +53 -0
  141. package/src/client/components/workspace/editor/toolbar-write-clusters.tsx +112 -0
  142. package/src/client/components/workspace/editor/viewers/SvgPreview.tsx +18 -2
  143. package/src/client/components/workspace/editor/viewers/viewers.test.ts +39 -0
  144. package/src/client/components/workspace/file-explorer/ContextMenu.tsx +18 -5
  145. package/src/client/components/workspace/file-explorer/Modals.tsx +15 -1
  146. package/src/client/components/workspace/file-explorer/RowLabel.tsx +68 -0
  147. package/src/client/components/workspace/file-explorer/TreeItem.tsx +47 -57
  148. package/src/client/components/workspace/file-explorer/collapsed-chain.test.ts +167 -0
  149. package/src/client/components/workspace/file-explorer/drag-props.ts +42 -0
  150. package/src/client/components/workspace/file-explorer/drag.test.ts +43 -0
  151. package/src/client/components/workspace/file-explorer/index.tsx +21 -20
  152. package/src/client/components/workspace/git-panel/commit-modal.test.ts +5 -3
  153. package/src/client/components/workspace/user-browser-panel/AddressBar.tsx +5 -0
  154. package/src/client/components/workspace/user-browser-panel/AnnotateBar.test.tsx +201 -0
  155. package/src/client/components/workspace/user-browser-panel/AnnotateBar.tsx +112 -0
  156. package/src/client/components/workspace/user-browser-panel/AnnotateLayer.test.tsx +225 -0
  157. package/src/client/components/workspace/user-browser-panel/AnnotateLayer.tsx +217 -0
  158. package/src/client/components/workspace/user-browser-panel/AnnotateTarget.tsx +99 -0
  159. package/src/client/components/workspace/user-browser-panel/Frame.tsx +30 -1
  160. package/src/client/components/workspace/user-browser-panel/index.tsx +81 -2
  161. package/src/client/data/agent-data/builtin.ts +0 -10
  162. package/src/client/data/agent-data/orchestrator.ts +0 -45
  163. package/src/client/data/agent-data/specialist.ts +0 -100
  164. package/src/client/data/mock/token-usage.ts +170 -120
  165. package/src/client/data/settings/omp-schema.json +123 -4
  166. package/src/client/data/settings/project.ts +9 -28
  167. package/src/client/data/settings/skill.ts +6 -32
  168. package/src/client/hooks/browser/use-annotate.test.ts +143 -0
  169. package/src/client/hooks/browser/use-annotate.ts +98 -0
  170. package/src/client/hooks/browser/use-close-tab.test.ts +148 -0
  171. package/src/client/hooks/browser/use-close-tab.ts +67 -0
  172. package/src/client/hooks/browser/use-screencast.ts +13 -1
  173. package/src/client/hooks/chat/composer/browser-context.test.ts +117 -0
  174. package/src/client/hooks/chat/composer/browser-context.ts +40 -16
  175. package/src/client/hooks/chat/composer/items.test.ts +1 -1
  176. package/src/client/hooks/chat/composer/review-context.ts +38 -0
  177. package/src/client/hooks/chat/composer/use-draft.test.ts +132 -0
  178. package/src/client/hooks/chat/composer/use-draft.ts +31 -2
  179. package/src/client/hooks/chat/timeline/pending-model.ts +28 -0
  180. package/src/client/hooks/chat/timeline/row-window.test.ts +15 -2
  181. package/src/client/hooks/editor/use-document-annotations.ts +124 -0
  182. package/src/client/hooks/editor/use-editor-panel-ui.ts +52 -0
  183. package/src/client/hooks/editor/use-file-review-mode.ts +35 -0
  184. package/src/client/hooks/editor/use-preview-review.ts +148 -0
  185. package/src/client/hooks/settings/agents.ts +149 -0
  186. package/src/client/hooks/settings/crud-list.test.ts +28 -6
  187. package/src/client/hooks/settings/crud-list.ts +96 -57
  188. package/src/client/hooks/settings/omp-config.ts +164 -0
  189. package/src/client/hooks/settings/projects/index.ts +294 -0
  190. package/src/client/hooks/settings/projects.test.ts +237 -0
  191. package/src/client/hooks/settings/skill-catalog.ts +101 -9
  192. package/src/client/hooks/settings/skill-git-import.ts +153 -0
  193. package/src/client/hooks/settings/useTokenUsage.ts +125 -0
  194. package/src/client/hooks/settings/write-feedback.test.ts +104 -0
  195. package/src/client/hooks/settings/write-feedback.ts +63 -0
  196. package/src/client/hooks/shell/servers.test.ts +166 -0
  197. package/src/client/hooks/{desktop → shell}/servers.ts +17 -14
  198. package/src/client/hooks/ui/markdown-hydration.ts +125 -0
  199. package/src/client/hooks/ui/updates/index.test.ts +100 -1
  200. package/src/client/hooks/ui/updates/index.ts +50 -0
  201. package/src/client/hooks/ui/updates/notes.test.ts +138 -0
  202. package/src/client/hooks/ui/updates/notes.ts +57 -0
  203. package/src/client/hooks/ui/updates/shell-download.ts +1 -1
  204. package/src/client/hooks/ui/updates/shell-update.ts +2 -6
  205. package/src/client/hooks/workspace/file-row-drag.ts +41 -0
  206. package/src/client/hooks/workspace/file-tree-actions.ts +25 -1
  207. package/src/client/hooks/workspace/file-tree-edits.ts +23 -6
  208. package/src/client/hooks/workspace/root-drop-target.ts +77 -0
  209. package/src/client/hooks/workspace/unfolded-chains.ts +165 -0
  210. package/src/client/lib/browser/element-at.test.ts +229 -0
  211. package/src/client/lib/browser/element-at.ts +139 -0
  212. package/src/client/lib/desktop/dropped-paths.ts +1 -1
  213. package/src/client/lib/desktop/titlebar.test.ts +12 -3
  214. package/src/client/lib/desktop/titlebar.ts +1 -1
  215. package/src/client/lib/review/client.ts +73 -0
  216. package/src/client/lib/router/search-params.ts +42 -20
  217. package/src/client/lib/shell/desktop.ts +117 -0
  218. package/src/client/lib/shell/servers.ts +75 -0
  219. package/src/client/lib/shell/transport.ts +216 -0
  220. package/src/client/main.tsx +9 -0
  221. package/src/client/tailwind.css +65 -0
  222. package/src/server/lib/btw/lifecycle.server.ts +2 -2
  223. package/src/server/lib/db/schema.ts +7 -0
  224. package/src/server/lib/db/seed-terminal.test.ts +4 -0
  225. package/src/server/lib/fs/git-merge-conflict.test.ts +14 -1
  226. package/src/server/lib/omp/config/agent-definitions.test.ts +273 -0
  227. package/src/server/lib/omp/config/agents.ts +231 -61
  228. package/src/server/lib/omp/config/definitions.test.ts +9 -105
  229. package/src/server/lib/omp/config/extensions.ts +16 -7
  230. package/src/server/lib/omp/config/omp-config-files.test.ts +27 -0
  231. package/src/server/lib/omp/config/skill-sources.ts +49 -0
  232. package/src/server/lib/omp/config/skills-git-install.ts +155 -0
  233. package/src/server/lib/omp/config/skills-git-repo.ts +288 -0
  234. package/src/server/lib/omp/config/skills-git.test.ts +220 -0
  235. package/src/server/lib/omp/rpc/constants.test.ts +11 -0
  236. package/src/server/lib/omp/rpc/constants.ts +15 -0
  237. package/src/server/lib/omp/rpc/process.ts +11 -14
  238. package/src/server/lib/omp/rpc/session-commands.ts +6 -3
  239. package/src/server/lib/omp/rpc/utility-agent-dir.test.ts +134 -0
  240. package/src/server/lib/omp/rpc/utility-agent-dir.ts +138 -0
  241. package/src/server/lib/omp/rpc/utility.ts +12 -0
  242. package/src/server/lib/review/schema.server.ts +45 -0
  243. package/src/server/lib/review/store.server.ts +138 -0
  244. package/src/server/lib/terminal/attach-replay.test.ts +132 -0
  245. package/src/server/lib/terminal/output.test.ts +166 -0
  246. package/src/server/lib/terminal/output.ts +104 -0
  247. package/src/server/lib/terminal/runtime.server.ts +5 -0
  248. package/src/server/lib/terminal/send-result.test.ts +33 -0
  249. package/src/server/lib/terminal/send-result.ts +32 -0
  250. package/src/server/lib/terminal/session.server.ts +45 -11
  251. package/src/server/lib/terminal/store-liveness.test.ts +4 -0
  252. package/src/server/lib/updates/active-notes.test.ts +149 -0
  253. package/src/server/lib/updates/active-notes.ts +75 -0
  254. package/src/server/lib/updates/changelog-file.ts +42 -11
  255. package/src/server/lib/updates/changelog.test.ts +15 -1
  256. package/src/server/lib/updates/omp.ts +42 -15
  257. package/src/server/lib/updates/release-url.ts +27 -1
  258. package/src/server/lib/usage/index-parse.test.ts +200 -0
  259. package/src/server/lib/usage/index-parse.ts +178 -0
  260. package/src/server/lib/usage/index-query.server.ts +248 -0
  261. package/src/server/lib/usage/index-scan.server.ts +343 -0
  262. package/src/server/lib/usage/index-schema.server.ts +82 -0
  263. package/src/server/lib/usage/index.test.ts +234 -0
  264. package/src/server/lib/usage/providers.server.ts +4 -53
  265. package/src/server/lib/workspace/project-model.server.test.ts +102 -0
  266. package/src/server/lib/workspace/project-model.server.ts +47 -0
  267. package/src/server/lib/workspace/project-patch.server.ts +156 -0
  268. package/src/server/routes/agent/new.ts +8 -1
  269. package/src/server/routes/browser/index.ts +3 -0
  270. package/src/server/routes/browser/stream.ts +31 -2
  271. package/src/server/routes/browser/tabs.test.ts +94 -0
  272. package/src/server/routes/browser/tabs.ts +85 -0
  273. package/src/server/routes/index.ts +2 -0
  274. package/src/server/routes/review/bindings.ts +11 -0
  275. package/src/server/routes/review/index.ts +127 -0
  276. package/src/server/routes/review/routes.test.ts +191 -0
  277. package/src/server/routes/settings/agents.test.ts +180 -0
  278. package/src/server/routes/settings/agents.ts +99 -94
  279. package/src/server/routes/settings/mcp.test.ts +135 -0
  280. package/src/server/routes/settings/mcp.ts +14 -6
  281. package/src/server/routes/settings/projects.ts +53 -19
  282. package/src/server/routes/settings/settings-projects-routes.test.ts +214 -0
  283. package/src/server/routes/settings/settings-store-routes.test.ts +11 -115
  284. package/src/server/routes/settings/skills-catalog-actions.ts +195 -0
  285. package/src/server/routes/settings/skills.test.ts +225 -0
  286. package/src/server/routes/settings/skills.ts +18 -15
  287. package/src/server/routes/telemetry/tokens.test.ts +145 -0
  288. package/src/server/routes/telemetry/tokens.ts +70 -149
  289. package/src/server/routes/terminal/ws.ts +6 -5
  290. package/src/server/routes/updates/index.ts +2 -0
  291. package/src/server/routes/updates/notes.ts +34 -0
  292. package/src/server/routes/updates/routes.test.ts +49 -0
  293. package/src/shared/lib/browser/annotate.test.ts +283 -0
  294. package/src/shared/lib/browser/annotate.ts +231 -0
  295. package/src/shared/lib/browser/browser-viewer.test.ts +26 -4
  296. package/src/shared/lib/browser/frame-gate.test.ts +91 -0
  297. package/src/shared/lib/browser/frame-gate.ts +69 -0
  298. package/src/shared/lib/browser/frame-params.test.ts +43 -0
  299. package/src/shared/lib/browser/frame-params.ts +60 -0
  300. package/src/shared/lib/browser/frame-pipeline.test.ts +132 -0
  301. package/src/shared/lib/browser/frame-pipeline.ts +80 -0
  302. package/src/shared/lib/browser/page-context.ts +60 -4
  303. package/src/shared/lib/browser/snapshot.test.ts +115 -0
  304. package/src/shared/lib/browser/snapshot.ts +112 -0
  305. package/src/shared/lib/browser/tab-close.test.ts +74 -0
  306. package/src/shared/lib/browser/tab-close.ts +77 -0
  307. package/src/shared/lib/browser/viewer.ts +45 -26
  308. package/src/shared/lib/chat/timeline/block-chrome.ts +19 -0
  309. package/src/shared/lib/chat/timeline/tool-render-mode.ts +17 -4
  310. package/src/shared/lib/chat/tool/artifact.ts +55 -20
  311. package/src/shared/lib/chat/tool/bash-facts.ts +96 -0
  312. package/src/shared/lib/chat/tool/minimized.test.ts +98 -0
  313. package/src/shared/lib/chat/tool/minimized.ts +64 -0
  314. package/src/shared/lib/chat/tool/summary.test.ts +28 -8
  315. package/src/shared/lib/chat/tool/summary.ts +3 -32
  316. package/src/shared/lib/fs/folder-chain.test.ts +81 -1
  317. package/src/shared/lib/fs/folder-chain.ts +84 -0
  318. package/src/shared/lib/markdown/blocks/anchor.ts +63 -0
  319. package/src/shared/lib/markdown/blocks/blocks.test.ts +141 -0
  320. package/src/shared/lib/markdown/blocks/renderer.ts +120 -0
  321. package/src/shared/lib/markdown/blocks/scan.ts +100 -0
  322. package/src/shared/lib/markdown/blocks/types.ts +62 -0
  323. package/src/shared/lib/markdown/marked.ts +58 -147
  324. package/src/shared/lib/markdown/math-extensions.ts +168 -0
  325. package/src/shared/lib/markdown/review/prompt.ts +77 -0
  326. package/src/shared/lib/markdown/review/reconcile.ts +55 -0
  327. package/src/shared/lib/markdown/review/review.test.ts +96 -0
  328. package/src/shared/lib/markdown/xml-sections.test.ts +140 -0
  329. package/src/shared/lib/markdown/xml-sections.ts +139 -0
  330. package/src/shared/lib/models/provider/label.ts +57 -0
  331. package/src/shared/lib/omp/config/agent-file.test.ts +54 -0
  332. package/src/shared/lib/omp/config/agent-file.ts +43 -0
  333. package/src/shared/lib/omp/config/row-value.test.ts +95 -0
  334. package/src/shared/lib/omp/config/row-value.ts +97 -0
  335. package/src/shared/lib/omp/config/schema.test.ts +66 -0
  336. package/src/shared/lib/omp/config/schema.ts +6 -0
  337. package/src/shared/lib/omp/rpc/frame.test.ts +48 -47
  338. package/src/shared/lib/omp/rpc/frame.ts +49 -40
  339. package/src/shared/lib/omp/session/job-progress.test.ts +37 -1
  340. package/src/shared/lib/omp/session/job-progress.ts +31 -0
  341. package/src/shared/lib/omp/session/proc.ts +5 -0
  342. package/src/shared/lib/omp/session/wait.ts +5 -0
  343. package/src/shared/lib/ui/menu-anchor.test.ts +69 -0
  344. package/src/shared/lib/ui/menu-anchor.ts +89 -0
  345. package/src/shared/lib/ui/mobile-overlay.ts +61 -0
  346. package/src/shared/lib/ui/svg-scope.test.ts +97 -0
  347. package/src/shared/lib/ui/svg-scope.ts +134 -0
  348. package/src/shared/lib/usage/breakdown.test.ts +197 -0
  349. package/src/shared/lib/usage/breakdown.ts +241 -0
  350. package/src/shared/lib/usage/range.test.ts +204 -0
  351. package/src/shared/lib/usage/range.ts +240 -0
  352. package/src/shared/lib/usage/view.test.ts +85 -0
  353. package/src/shared/lib/usage/view.ts +74 -0
  354. package/src/shared/lib/workspace/project-accent.test.ts +69 -0
  355. package/src/shared/lib/workspace/project-accent.ts +63 -0
  356. package/src/shared/lib/workspace/project-icon.ts +40 -0
  357. package/src/shared/lib/workspace/project-model.test.ts +79 -0
  358. package/src/shared/lib/workspace/project-model.ts +64 -0
  359. package/src/shared/types/browser.ts +78 -0
  360. package/src/shared/types/index.ts +1 -0
  361. package/src/shared/types/settings/agent.ts +28 -6
  362. package/src/shared/types/settings/skill.ts +53 -3
  363. package/src/shared/types/settings/state.ts +4 -3
  364. package/src/shared/types/settings/write.ts +6 -0
  365. package/src/shared/types/tokenUsage.ts +128 -21
  366. package/src/shared/types/updates.ts +20 -0
  367. package/src/test-support/file-explorer-disk.test-util.ts +25 -1
  368. package/src/test-support/file-explorer-drag.test-util.ts +88 -0
  369. package/src/test-support/file-explorer.test-util.ts +47 -47
  370. package/dist/client/static/js/index-fjnq2z2d.css +0 -1
  371. package/src/client/components/common/code-surface/indent-guide.ts +0 -104
  372. package/src/client/components/common/server-switcher/index.test.tsx +0 -150
  373. package/src/client/components/settings/categories/skill-catalog/AddSourceModal.tsx +0 -133
  374. package/src/client/hooks/desktop/servers.test.ts +0 -73
  375. package/src/client/lib/desktop/bridge.ts +0 -203
  376. package/src/server/lib/omp/session/usage/aggregate-shaping.test.ts +0 -149
  377. package/src/server/lib/omp/session/usage/aggregate.test.ts +0 -243
  378. package/src/server/lib/omp/session/usage/aggregate.ts +0 -330
  379. package/src/server/lib/omp/session/usage/shaping.ts +0 -130
  380. package/src/shared/lib/code/editor/indent-guide-rule.test.ts +0 -65
  381. package/src/shared/lib/code/editor/indent-guide-rule.ts +0 -47
@@ -29,6 +29,8 @@ import { afterEach, describe, expect, test } from 'bun:test';
29
29
  type Shell = {
30
30
  __kabmoCall?: (method: string, params?: Record<string, unknown>) => Promise<unknown>;
31
31
  __kabmoDesktop?: Record<string, unknown>;
32
+ /** macOS and Linux post through this handler — the SAME name iOS uses. */
33
+ webkit?: { messageHandlers?: { kabmo?: { postMessage: (message: unknown) => void } } };
32
34
  };
33
35
 
34
36
  const scope = () => globalThis as unknown as { window?: Shell };
@@ -38,9 +40,16 @@ async function titlebar() {
38
40
  }
39
41
 
40
42
  function shell(identity: Record<string, unknown> | null) {
41
- scope().window = identity === null
42
- ? { __kabmoCall: async () => null }
43
- : { __kabmoCall: async () => null, __kabmoDesktop: identity };
43
+ // The WebKit handler is part of the REAL desktop shape, not test decoration:
44
+ // macOS and Linux post over `window.webkit.messageHandlers.kabmo`, so a fixture
45
+ // without it let a phone-first host check read every desktop shell as `ios` and
46
+ // drop this inset — the wordmark was drawn under the traffic lights.
47
+ const window: Shell = {
48
+ __kabmoCall: async () => null,
49
+ webkit: { messageHandlers: { kabmo: { postMessage: () => {} } } },
50
+ };
51
+ if (identity !== null) window.__kabmoDesktop = identity;
52
+ scope().window = window;
44
53
  }
45
54
 
46
55
  const MACOS = { platform: 'macos', shellVersion: '0.1.0', overlayTitlebar: true, trafficLightInset: '5rem' };
@@ -36,7 +36,7 @@
36
36
  * identity, and the answer is the caller's own padding rather than a guess.
37
37
  */
38
38
 
39
- import { isDesktopShell } from '@/client/lib/desktop/bridge';
39
+ import { isDesktopShell } from '@/client/lib/shell/transport';
40
40
 
41
41
  /** Width macOS reserves for its traffic lights, plus the gap after them. */
42
42
  export const TRAFFIC_LIGHT_INSET = '5rem';
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The review surface's HTTP calls, typed.
8
+ *
9
+ * One module because three callers share the contract: the loading hook, the
10
+ * save, and the delete. `root`/`repo` travel on every request — a document in a
11
+ * nested repo is a different document from a same-named path at the workspace
12
+ * root, and the server keys its rows by the resolved pair.
13
+ *
14
+ * A refusal is REPORTED, never swallowed: a save that silently failed would
15
+ * leave the reader believing a judgement is stored when it is not, which is the
16
+ * one failure this feature cannot tolerate.
17
+ */
18
+
19
+ import type { AnnotationDraft, BlockVerdict, DocumentAnnotation, ReviewTarget } from '@/shared/lib/markdown/blocks/types';
20
+
21
+ /** The body a save sends, minus the scope the server resolves from root/repo. */
22
+ export interface SaveAnnotationBody {
23
+ path: string;
24
+ root?: string | null;
25
+ repo?: string | null;
26
+ blockKey: string;
27
+ lineStart: number;
28
+ lineEnd: number;
29
+ verdict: BlockVerdict;
30
+ note: string;
31
+ quote: string;
32
+ }
33
+
34
+ function scopeParams(target: ReviewTarget): URLSearchParams {
35
+ const params = new URLSearchParams({ path: target.path });
36
+ if (target.root) params.set('root', target.root);
37
+ if (target.repo && target.repo !== '.') params.set('repo', target.repo);
38
+ return params;
39
+ }
40
+
41
+ /** The refusal text a failed request carries, or a status-shaped fallback. */
42
+ async function refusal(response: Response): Promise<string> {
43
+ const payload = (await response.json().catch(() => null)) as { error?: unknown } | null;
44
+ return typeof payload?.error === 'string' ? payload.error : `Request failed (${response.status})`;
45
+ }
46
+
47
+ /** Every judgement stored for one document. */
48
+ export async function fetchAnnotations(target: ReviewTarget, signal?: AbortSignal): Promise<DocumentAnnotation[]> {
49
+ const response = await fetch(`/api/review?${scopeParams(target).toString()}`, { signal });
50
+ if (!response.ok) throw new Error(await refusal(response));
51
+ const payload = (await response.json()) as { annotations?: DocumentAnnotation[] };
52
+ return Array.isArray(payload.annotations) ? payload.annotations : [];
53
+ }
54
+
55
+ /** Store one judgement, answering the row as the server stored it. */
56
+ export async function saveAnnotation(target: ReviewTarget, draft: Omit<AnnotationDraft, 'scope' | 'path'>): Promise<DocumentAnnotation> {
57
+ const body: SaveAnnotationBody = { ...draft, path: target.path, root: target.root, repo: target.repo };
58
+ const response = await fetch('/api/review', {
59
+ method: 'POST',
60
+ headers: { 'Content-Type': 'application/json' },
61
+ body: JSON.stringify(body),
62
+ });
63
+ if (!response.ok) throw new Error(await refusal(response));
64
+ const payload = (await response.json()) as { annotation?: DocumentAnnotation };
65
+ if (!payload.annotation) throw new Error('The server answered without the saved annotation');
66
+ return payload.annotation;
67
+ }
68
+
69
+ /** Remove one judgement. A missing row is an error, not a silent success. */
70
+ export async function removeAnnotation(id: string): Promise<void> {
71
+ const response = await fetch(`/api/review?id=${encodeURIComponent(id)}`, { method: 'DELETE' });
72
+ if (!response.ok) throw new Error(await refusal(response));
73
+ }
@@ -23,29 +23,54 @@ type SetParams = (next: SetParamsInit | SetParamsFn, options?: { replace?: boole
23
23
  export type SetSearchParams = SetParams;
24
24
 
25
25
  const listeners = new Set<() => void>();
26
- let currentSearch = typeof window === 'undefined' ? '' : window.location.search;
27
26
 
28
27
  function emit(): void {
29
28
  for (const listener of listeners) listener();
30
29
  }
31
30
 
32
- function subscribe(listener: () => void): () => void {
33
- listeners.add(listener);
34
- return () => listeners.delete(listener);
31
+ /**
32
+ * The search string of the window in scope RIGHT NOW, never a value cached when
33
+ * this module was first evaluated.
34
+ *
35
+ * A module-level `currentSearch` reads as a harmless cache and is not: it pins
36
+ * the store to whichever `window` existed at import time, and so does the
37
+ * `popstate` listener below. In the browser there is one window forever and the
38
+ * two are equivalent; under `bun test` every DOM suite installs its OWN window,
39
+ * so the pinned pair made the router answer a URL nobody can see and miss
40
+ * `popstate` on the window the suite is actually driving — measured: a suite
41
+ * that set `?sessionId=` and mounted the composer read `''` and drew the
42
+ * landing, failing a case that passes when its file runs alone.
43
+ */
44
+ function liveSearch(): string {
45
+ return typeof window === 'undefined' ? '' : window.location.search;
35
46
  }
36
47
 
37
- /** Re-read `location.search` and notify only when it actually changed. */
38
- function syncFromLocation(): void {
39
- const next = typeof window === 'undefined' ? '' : window.location.search;
40
- if (next === currentSearch) return;
41
- currentSearch = next;
48
+ /**
49
+ * The window this store is currently listening on.
50
+ *
51
+ * Rebound on subscribe rather than once at module load, for the reason above:
52
+ * a suite that replaces `globalThis.window` must get a router that follows it.
53
+ */
54
+ let boundWindow: Window | undefined;
55
+
56
+ function onPopState(): void {
42
57
  emit();
43
58
  }
44
59
 
45
- if (typeof window !== 'undefined') {
60
+ function bindToCurrentWindow(): void {
61
+ const next = typeof window === 'undefined' ? undefined : window;
62
+ if (next === boundWindow) return;
63
+ boundWindow?.removeEventListener('popstate', onPopState);
64
+ boundWindow = next;
46
65
  // Back/forward navigation. `pushState`/`replaceState` do NOT fire popstate,
47
66
  // so setParams notifies directly.
48
- window.addEventListener('popstate', syncFromLocation);
67
+ next?.addEventListener('popstate', onPopState);
68
+ }
69
+
70
+ function subscribe(listener: () => void): () => void {
71
+ listeners.add(listener);
72
+ bindToCurrentWindow();
73
+ return () => listeners.delete(listener);
49
74
  }
50
75
 
51
76
  function toSearchString(init: SetParamsInit): string {
@@ -63,22 +88,19 @@ function resolveNext(next: SetParamsInit | SetParamsFn, current: URLSearchParams
63
88
  return toSearchString(next);
64
89
  }
65
90
 
66
- function getSnapshot(): string {
67
- return currentSearch;
68
- }
69
-
70
91
  export function useSearchParams(): [URLSearchParams, SetParams] {
71
92
  // Preact's `useSyncExternalStore` takes only (subscribe, getSnapshot) — no
72
- // server-snapshot argument — and the app is client-rendered, so `currentSearch`
73
- // is the single source of truth.
74
- const search = useSyncExternalStore(subscribe, getSnapshot);
93
+ // server-snapshot argument — and the app is client-rendered, so the live
94
+ // `location.search` is the single source of truth. It is read on demand, and
95
+ // `useSyncExternalStore` compares snapshots by value, so an unchanged URL
96
+ // costs one string comparison and no render.
97
+ const search = useSyncExternalStore(subscribe, liveSearch);
75
98
 
76
99
  const setParams = useCallback<SetParams>((next, options) => {
77
- const searchString = resolveNext(next, new URLSearchParams(currentSearch));
100
+ const searchString = resolveNext(next, new URLSearchParams(liveSearch()));
78
101
  const url = `${window.location.pathname}${searchString}${window.location.hash}`;
79
102
  if (options?.replace) window.history.replaceState(null, '', url);
80
103
  else window.history.pushState(null, '', url);
81
- currentSearch = searchString;
82
104
  emit();
83
105
  }, []);
84
106
 
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The commands only the DESKTOP shells answer: their identity, their window
8
+ * verbs, and installing their own update.
9
+ *
10
+ * A phone has none of these. It draws no window chrome (its buttons are the
11
+ * system's), it cannot replace the app it runs inside, and its identity arrives
12
+ * on the User-Agent rather than over a bridge. Keeping them out of
13
+ * `transport.ts` is what lets that module stay about one question — "which shell
14
+ * is this?" — instead of about every verb five shells do not share.
15
+ */
16
+
17
+ import { callShell, isDesktopShell } from '@/client/lib/shell/transport';
18
+
19
+ /** What the desktop shells report about themselves. */
20
+ export interface DesktopInfo {
21
+ platform: string;
22
+ shellVersion: string;
23
+ /**
24
+ * The machine's architecture, in the canonical vocabulary (`arm64` / `x64`).
25
+ *
26
+ * Every desktop artifact is per-architecture, so without this the console can
27
+ * only offer the release page. Absent on a shell too old to report it, which
28
+ * degrades the offer to that page rather than breaking it.
29
+ */
30
+ arch?: string;
31
+ /** True when the shell hides the native title bar and the page draws it. */
32
+ overlayTitlebar: boolean;
33
+ /**
34
+ * macOS: the width of the traffic lights, which float over the page's LEFT
35
+ * edge. Absent on a platform whose window controls are not on the left.
36
+ */
37
+ trafficLightInset?: string;
38
+ /**
39
+ * True when the shell is borderless and has NO controls of its own, so the page
40
+ * must draw minimise / maximise / close.
41
+ *
42
+ * Windows and Linux. macOS gets its traffic lights from the system over a
43
+ * full-size content view; a borderless Windows window has nothing at all, and
44
+ * the webview there is an HWND — a WPF element cannot be drawn on top of it
45
+ * (the airspace problem) — so the page is the only surface that can. Linux could
46
+ * draw native buttons in a `GtkOverlay` (measured: an overlay child does receive
47
+ * clicks over a WebView), but the page's set already exists, is tested, and is
48
+ * the one every other shell's users see.
49
+ */
50
+ windowControls?: boolean;
51
+ }
52
+
53
+ declare global {
54
+ interface Window {
55
+ __kabmoDesktop?: DesktopInfo;
56
+ }
57
+ }
58
+
59
+ export function desktopInfo(): Promise<DesktopInfo | null> {
60
+ if (!isDesktopShell()) return Promise.resolve(null);
61
+ const injected = typeof window === 'undefined' ? undefined : window.__kabmoDesktop;
62
+ // The shell injects this before the page's first script, so the navbar's first
63
+ // paint is already correct; the command is only a fallback for a shell that did
64
+ // not (an older build).
65
+ if (injected) return Promise.resolve(injected);
66
+ return callShell<DesktopInfo>('desktop_info');
67
+ }
68
+
69
+ /** Minimise, maximise/restore and close, for a shell that draws no controls. */
70
+ export function windowMinimize(): Promise<void> {
71
+ return callShell<void>('window_minimize');
72
+ }
73
+
74
+ export function windowToggleMaximize(): Promise<void> {
75
+ return callShell<void>('window_maximize');
76
+ }
77
+
78
+ export function windowClose(): Promise<void> {
79
+ return callShell<void>('window_close');
80
+ }
81
+
82
+ /**
83
+ * What the shell did with an update it was handed.
84
+ *
85
+ * The shell owns the whole run — download, verify, replace, relaunch — because
86
+ * only it can do any of those things: a page cannot replace the executable it is
87
+ * running inside, and a page's "download" leaves a file in a folder the user has
88
+ * to find. This is the report it sends back.
89
+ *
90
+ * `relaunching` is the honest terminal state on success: the shell is about to
91
+ * quit, so a caller that keeps a spinner going is describing a page that is
92
+ * already gone.
93
+ */
94
+ export interface ShellUpdateOutcome {
95
+ success: boolean;
96
+ /** The version the shell installed, when it could read one. */
97
+ version?: string | null;
98
+ /** A sentence for the user — the shell's own, never a generic failure. */
99
+ message: string;
100
+ relaunching?: boolean;
101
+ }
102
+
103
+ /**
104
+ * Ask the shell to install the build it just offered.
105
+ *
106
+ * `url` and `version` are the console's answer, passed through rather than
107
+ * re-derived: the shell has no release list of its own, and asking it to fetch one
108
+ * would be a second reader of the same GitHub endpoint.
109
+ *
110
+ * The shell VERIFIES the bytes before installing them (the platform's own check —
111
+ * a code signature, an archive shape, a version floor). A console that is
112
+ * compromised can therefore make the shell download, but not make it run
113
+ * something the platform will not vouch for.
114
+ */
115
+ export function applyShellUpdate(url: string, version: string): Promise<ShellUpdateOutcome> {
116
+ return callShell<ShellUpdateOutcome>('update_apply', { url, version });
117
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The shell's server list, as the console reads and changes it.
8
+ *
9
+ * The same four commands work in all five shells. Two more — `servers_start` and
10
+ * `servers_stop` — exist only where a shell can own a child process (the desktop
11
+ * trio). A phone refuses them, and the panel knows not to offer them from
12
+ * `startable: false` in the state rather than from a user agent.
13
+ *
14
+ * Nothing here decides which shell answered; that is `transport.ts`.
15
+ */
16
+
17
+ import { callShell } from '@/client/lib/shell/transport';
18
+
19
+ /** One server in the shell's list, or one found on this machine. */
20
+ export interface ShellServer {
21
+ id: string;
22
+ label: string;
23
+ url: string;
24
+ kind: 'remote' | 'managed';
25
+ port?: number;
26
+ running: boolean;
27
+ /** This shell started it (as opposed to a server found on the machine). */
28
+ owned: boolean;
29
+ version?: string;
30
+ mode?: string;
31
+ pid?: number;
32
+ startedAt?: string;
33
+ /** `stored` for the list, `record` for a local instance found on disk. */
34
+ source: 'stored' | 'record';
35
+ }
36
+
37
+ export interface ShellServersState {
38
+ entries: ShellServer[];
39
+ /** Local instances this shell did NOT start — a `kabmo serve`, a dev server. */
40
+ detected: ShellServer[];
41
+ activeId?: string;
42
+ activeUrl?: string;
43
+ /**
44
+ * Whether this shell can start a server of its own. Absent reads as `true`, so
45
+ * the three desktop shells (which all can) need no change; a phone answers
46
+ * `false` and the panel then says nothing about starting one.
47
+ */
48
+ startable?: boolean;
49
+ }
50
+
51
+ export function readServers(): Promise<ShellServersState> {
52
+ return callShell<ShellServersState>('servers_state');
53
+ }
54
+
55
+ export function addRemoteServer(label: string, url: string): Promise<ShellServersState> {
56
+ return callShell<ShellServersState>('servers_add', { label, url });
57
+ }
58
+
59
+ export function removeServer(id: string): Promise<ShellServersState> {
60
+ return callShell<ShellServersState>('servers_remove', { id });
61
+ }
62
+
63
+ /** Point the window at a server. The shell navigates, so this page goes away. */
64
+ export function activateServer(id: string): Promise<ShellServersState> {
65
+ return callShell<ShellServersState>('servers_activate', { id });
66
+ }
67
+
68
+ /** Start a local server as the shell's child, then navigate to it. */
69
+ export function startLocalServer(port?: number, label?: string): Promise<ShellServersState> {
70
+ return callShell<ShellServersState>('servers_start', { port: port ?? null, label: label ?? null });
71
+ }
72
+
73
+ export function stopLocalServer(id: string): Promise<ShellServersState> {
74
+ return callShell<ShellServersState>('servers_stop', { id });
75
+ }
@@ -0,0 +1,216 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The shell transport, and the reason every other module asks this one whether it
8
+ * is running inside a shell.
9
+ *
10
+ * FIVE shells speak to this page, and they do NOT all inject the same thing:
11
+ *
12
+ * - `packages/macos` / `packages/windows` / `packages/linux` install
13
+ * `window.__kabmoCall` themselves, at document start, together with
14
+ * `window.__kabmoDesktop` (their identity). Nothing in this file has to run for
15
+ * them: `installShellTransport` is a no-op when a call function already exists.
16
+ * - `packages/android` exposes only `window.kabmoHost` (a `@JavascriptInterface`),
17
+ * and `packages/ios` only `window.webkit.messageHandlers.kabmo`. Neither can
18
+ * inject script before the page, so THIS module adapts their transport into
19
+ * `__kabmoCall` — one shape for the whole console — from
20
+ * `installShellTransport()` in `main.tsx`.
21
+ *
22
+ * The split between `isDesktopShell()` and `isMobileShell()` is load-bearing, not
23
+ * bookkeeping: `titlebarInsetLeft` treats "a shell, with no identity" as a macOS
24
+ * window whose traffic lights float over the page's left edge. A phone that
25
+ * answered `isDesktopShell() === true` would reserve 5rem for controls it does
26
+ * not have and draw its wordmark 80px from the edge.
27
+ *
28
+ * Params cross the mobile transport FORM-ENCODED (`k=v&k=v`), not as JSON:
29
+ * Android's `org.json` is a stub in a JVM unit test, so a JSON wire would leave
30
+ * the parser that most needs a test as the only one that cannot have one. State
31
+ * comes back as JSON TEXT for the same reason — the native side builds one string,
32
+ * and the page parses it in the one place that has a real JSON parser.
33
+ */
34
+
35
+ /** Which shell host is answering, once one is present. */
36
+ export type ShellHostKind = 'desktop' | 'android' | 'ios';
37
+
38
+ /** What a phone's native side exposes. Android only; iOS uses WebKit messages. */
39
+ interface AndroidHost {
40
+ post: (method: string, id: string, params: string) => void;
41
+ }
42
+
43
+ declare global {
44
+ interface Window {
45
+ __kabmoCall?: (method: string, params?: Record<string, unknown>) => Promise<unknown>;
46
+ __kabmoReply?: (id: number, ok: boolean, payload: unknown) => void;
47
+ /** Set by `installShellTransport` — which host answered this page. */
48
+ __kabmoShellHost?: 'android' | 'ios';
49
+ kabmoHost?: AndroidHost;
50
+ webkit?: {
51
+ messageHandlers?: {
52
+ kabmo?: { postMessage: (message: unknown) => void };
53
+ };
54
+ };
55
+ }
56
+ }
57
+
58
+ /** The host object a phone's native side has already installed, if any. */
59
+ function detectedHost(): 'android' | 'ios' | null {
60
+ if (typeof window === 'undefined') return null;
61
+ if (window.kabmoHost) return 'android';
62
+ if (window.webkit?.messageHandlers?.kabmo) return 'ios';
63
+ return null;
64
+ }
65
+
66
+ /**
67
+ * Which shell this page is inside, or null in a browser.
68
+ *
69
+ * A desktop shell is identified by the transport it installed by itself; a phone
70
+ * by the host object its native side exposes (or by the marker
71
+ * `installShellTransport` wrote once it adapted that host).
72
+ */
73
+ export function shellHost(): ShellHostKind | null {
74
+ if (typeof window === 'undefined') return null;
75
+ const marked = window.__kabmoShellHost;
76
+ if (marked) return marked;
77
+ // The DESKTOP trio is asked FIRST, and not for tidiness: macOS and Linux post
78
+ // over `window.webkit.messageHandlers.kabmo` — the very handler name iOS uses
79
+ // — so a phone-first check read every macOS/Linux shell as `ios`,
80
+ // `isDesktopShell()` answered false, and `titlebarInsetLeft` stopped reserving
81
+ // the 5rem macOS keeps for its traffic lights. The wordmark and the drawer's
82
+ // title were drawn UNDER close / minimise / maximise.
83
+ if (window.__kabmoDesktop) return 'desktop';
84
+ if (typeof window.__kabmoCall === 'function') return 'desktop';
85
+ return detectedHost();
86
+ }
87
+
88
+ /** True once some shell answers `__kabmoCall` — desktop or a phone. */
89
+ export function hasShellBridge(): boolean {
90
+ if (typeof window === 'undefined') return false;
91
+ if (typeof window.__kabmoCall === 'function') return true;
92
+ return detectedHost() !== null;
93
+ }
94
+
95
+ /** True in the macOS / Windows / Linux shells. */
96
+ export function isDesktopShell(): boolean {
97
+ return shellHost() === 'desktop';
98
+ }
99
+
100
+ /** True in the Android / iOS shells. */
101
+ export function isMobileShell(): boolean {
102
+ const host = shellHost();
103
+ return host === 'android' || host === 'ios';
104
+ }
105
+
106
+ /** `{ label: 'NAS', port: 3000 }` → `label=NAS&port=3000`. */
107
+ function encodeParams(params: Record<string, unknown>): string {
108
+ const parts: string[] = [];
109
+ for (const [key, value] of Object.entries(params)) {
110
+ if (value === undefined) continue;
111
+ const text = value === null ? '' : String(value);
112
+ parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(text)}`);
113
+ }
114
+ return parts.join('&');
115
+ }
116
+
117
+ /**
118
+ * How long a phone's shell gets to answer one command.
119
+ *
120
+ * `servers_state` PROBES every stored URL (`GET /api/health`, 6 s each), so the
121
+ * budget has to cover a short list of unreachable servers rather than a single
122
+ * round trip. A timeout here reports the shell as unresponsive, which is the
123
+ * honest answer for a native thread that never replied.
124
+ */
125
+ const REPLY_TIMEOUT_MS = 60_000;
126
+
127
+ /**
128
+ * Adapt a phone's native transport into the `__kabmoCall` shape the console uses.
129
+ *
130
+ * Idempotent, and a no-op in a browser and in a desktop shell (both either have no
131
+ * host or already have the function). Installed before the first render, so the
132
+ * server control is drawn on the first paint.
133
+ */
134
+ export function installShellTransport(): void {
135
+ if (typeof window === 'undefined') return;
136
+ // A desktop shell installed its own transport before the page's scripts, and
137
+ // macOS/Linux expose the same WebKit handler name a phone does — so this test
138
+ // is the identity, not `detectedHost()`.
139
+ if (window.__kabmoDesktop) return;
140
+ if (typeof window.__kabmoCall === 'function') return;
141
+ const host = detectedHost();
142
+ if (!host) return;
143
+
144
+ window.__kabmoShellHost = host;
145
+
146
+ // The bare globals, not `window.setTimeout`: this file's tests install a
147
+ // minimal `window`, and the timer is the one thing they must not have to fake.
148
+ /** The handle this runtime hands back: a number in a browser, an object under Bun. */
149
+ type TimerHandle = ReturnType<typeof setTimeout>;
150
+
151
+ interface PendingReply {
152
+ resolve: (value: unknown) => void;
153
+ reject: (reason: Error) => void;
154
+ timer: TimerHandle;
155
+ }
156
+ const pending = new Map<number, PendingReply>();
157
+ let next = 1;
158
+
159
+ window.__kabmoReply = (id, ok, payload) => {
160
+ const entry = pending.get(id);
161
+ if (!entry) return;
162
+ pending.delete(id);
163
+ clearTimeout(entry.timer);
164
+ if (!ok) {
165
+ entry.reject(new Error(typeof payload === 'string' ? payload : 'The shell refused that command.'));
166
+ return;
167
+ }
168
+ // State crosses as JSON TEXT (see this module's header): an unparseable answer
169
+ // is still the shell's answer, so it resolves as-is rather than turning a
170
+ // working command into a failure.
171
+ if (typeof payload !== 'string') {
172
+ entry.resolve(payload);
173
+ return;
174
+ }
175
+ try {
176
+ entry.resolve(JSON.parse(payload));
177
+ } catch {
178
+ entry.resolve(payload);
179
+ }
180
+ };
181
+
182
+ window.__kabmoCall = (method, params) => {
183
+ const { promise, resolve, reject } = Promise.withResolvers<unknown>();
184
+ const id = next++;
185
+ const timer = setTimeout(() => {
186
+ pending.delete(id);
187
+ reject(new Error(`The shell did not answer ${method} within 60s.`));
188
+ }, REPLY_TIMEOUT_MS);
189
+ pending.set(id, { resolve, reject, timer });
190
+ const form = encodeParams(params ?? {});
191
+ if (host === 'android') {
192
+ window.kabmoHost?.post(method, String(id), form);
193
+ } else {
194
+ window.webkit?.messageHandlers?.kabmo?.postMessage({ id, method, params: form });
195
+ }
196
+ return promise;
197
+ };
198
+ }
199
+
200
+ /** One command over whichever transport this shell installed. */
201
+ export async function callShell<T>(method: string, params: Record<string, unknown> = {}): Promise<T> {
202
+ const invoke = typeof window === 'undefined' ? undefined : window.__kabmoCall;
203
+ if (!invoke) throw new Error('Not running inside a Kabmo shell.');
204
+ return (await invoke(method, params)) as T;
205
+ }
206
+
207
+ /**
208
+ * A refused command's message.
209
+ *
210
+ * The shell sends its own sentence (a refused URL, a server that exited), which is
211
+ * what the UI shows. Nothing is stripped: a message that genuinely starts with
212
+ * "Error" would lose its first word.
213
+ */
214
+ export function bridgeError(error: unknown): string {
215
+ return error instanceof Error ? error.message : String(error);
216
+ }
@@ -12,6 +12,7 @@ import { installUiKit } from '@/client/lib/plugins/kit';
12
12
  import { installScrollbarActivity } from '@/client/lib/scrollbar-activity';
13
13
  import { installBackDispatcher } from '@/client/lib/native/back';
14
14
  import { installPalettePublisher } from '@/client/lib/native/palette';
15
+ import { installShellTransport } from '@/client/lib/shell/transport';
15
16
  import { installResumeDispatcher } from '@/client/lib/native/resume';
16
17
  import { installFileLaunchHandler } from '@/client/lib/launch/file-handlers';
17
18
  import { installOpenPaths } from '@/client/lib/native/open-paths';
@@ -79,6 +80,14 @@ installBackDispatcher();
79
80
  // asks on the load it is hiding, not on a later interaction.
80
81
  installPalettePublisher();
81
82
 
83
+ // A phone's shell exposes its own transport (`window.kabmoHost` on Android,
84
+ // `window.webkit.messageHandlers.kabmo` on iOS) and cannot inject script before
85
+ // the page — so the console adapts it into `__kabmoCall` here, before the first
86
+ // render, which is what makes the header's server control appear on the first
87
+ // paint instead of after a round trip. A no-op in a browser and in the three
88
+ // desktop shells, which install `__kabmoCall` themselves.
89
+ installShellTransport();
90
+
82
91
  // The native shell calls `window.__kabmoResume()` when its app returns to the
83
92
  // foreground. A backgrounded Android WebView fires no `visibilitychange` and
84
93
  // gets no `close` for a socket the server reaped, so the shell's own lifecycle