@kabmo-labs/kabmo 0.11.1 → 0.12.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 (324) hide show
  1. package/README.md +67 -421
  2. package/dist/client/index.html +1 -1
  3. package/dist/client/index.js +426 -413
  4. package/dist/client/static/js/{index-amss8pge.js → index-0smc9gm2.js} +1123 -747
  5. package/dist/client/static/js/index-shexbk5f.css +1 -0
  6. package/marketplace/marketplace.json +13 -3
  7. package/marketplace/plugins/command-runner/package.json +4 -2
  8. package/marketplace/plugins/git-forge/README.md +219 -0
  9. package/marketplace/plugins/git-forge/icon.svg +20 -0
  10. package/marketplace/plugins/git-forge/package.json +56 -0
  11. package/marketplace/plugins/git-forge/src/app.tsx +82 -0
  12. package/marketplace/plugins/git-forge/src/command.test.ts +181 -0
  13. package/marketplace/plugins/git-forge/src/command.ts +119 -0
  14. package/marketplace/plugins/git-forge/src/header.tsx +87 -0
  15. package/marketplace/plugins/git-forge/src/panel/Badge.tsx +76 -0
  16. package/marketplace/plugins/git-forge/src/panel/Change.tsx +150 -0
  17. package/marketplace/plugins/git-forge/src/panel/CiBadge.tsx +51 -0
  18. package/marketplace/plugins/git-forge/src/panel/Credential.tsx +107 -0
  19. package/marketplace/plugins/git-forge/src/panel/Glyph.tsx +70 -0
  20. package/marketplace/plugins/git-forge/src/panel/Header.tsx +255 -0
  21. package/marketplace/plugins/git-forge/src/panel/ItemView.tsx +198 -0
  22. package/marketplace/plugins/git-forge/src/panel/List.tsx +89 -0
  23. package/marketplace/plugins/git-forge/src/panel/Row.tsx +68 -0
  24. package/marketplace/plugins/git-forge/src/panel/Sidebar.tsx +94 -0
  25. package/marketplace/plugins/git-forge/src/panel/Write.tsx +236 -0
  26. package/marketplace/plugins/git-forge/src/panel/format.ts +78 -0
  27. package/marketplace/plugins/git-forge/src/panel/index.test.tsx +344 -0
  28. package/marketplace/plugins/git-forge/src/panel/index.tsx +193 -0
  29. package/marketplace/plugins/git-forge/src/panel/use-forge.ts +250 -0
  30. package/marketplace/plugins/git-forge/src/panel/write.test.tsx +269 -0
  31. package/marketplace/plugins/git-forge/src/protocol.ts +308 -0
  32. package/marketplace/plugins/git-forge/src/server/adapters.test.ts +274 -0
  33. package/marketplace/plugins/git-forge/src/server/bitbucket.ts +307 -0
  34. package/marketplace/plugins/git-forge/src/server/capabilities.test.ts +116 -0
  35. package/marketplace/plugins/git-forge/src/server/capabilities.ts +105 -0
  36. package/marketplace/plugins/git-forge/src/server/ci.test.ts +88 -0
  37. package/marketplace/plugins/git-forge/src/server/ci.ts +118 -0
  38. package/marketplace/plugins/git-forge/src/server/context.ts +76 -0
  39. package/marketplace/plugins/git-forge/src/server/diff.test.ts +127 -0
  40. package/marketplace/plugins/git-forge/src/server/diff.ts +120 -0
  41. package/marketplace/plugins/git-forge/src/server/filters.test.ts +138 -0
  42. package/marketplace/plugins/git-forge/src/server/filters.ts +138 -0
  43. package/marketplace/plugins/git-forge/src/server/forges.test.ts +235 -0
  44. package/marketplace/plugins/git-forge/src/server/gitea.ts +296 -0
  45. package/marketplace/plugins/git-forge/src/server/github-extras.ts +92 -0
  46. package/marketplace/plugins/git-forge/src/server/github.ts +320 -0
  47. package/marketplace/plugins/git-forge/src/server/gitlab-detail.ts +207 -0
  48. package/marketplace/plugins/git-forge/src/server/gitlab.test.ts +195 -0
  49. package/marketplace/plugins/git-forge/src/server/gitlab.ts +176 -0
  50. package/marketplace/plugins/git-forge/src/server/http.test.ts +270 -0
  51. package/marketplace/plugins/git-forge/src/server/http.ts +329 -0
  52. package/marketplace/plugins/git-forge/src/server/map.ts +120 -0
  53. package/marketplace/plugins/git-forge/src/server/registry.ts +47 -0
  54. package/marketplace/plugins/git-forge/src/server/remote.test.ts +146 -0
  55. package/marketplace/plugins/git-forge/src/server/remote.ts +188 -0
  56. package/marketplace/plugins/git-forge/src/server/scope-probe.test.ts +98 -0
  57. package/marketplace/plugins/git-forge/src/server/scope-probe.ts +128 -0
  58. package/marketplace/plugins/git-forge/src/server/scope.ts +66 -0
  59. package/marketplace/plugins/git-forge/src/server/test-context.ts +67 -0
  60. package/marketplace/plugins/git-forge/src/server/test-host.ts +51 -0
  61. package/marketplace/plugins/git-forge/src/server/token.test.ts +107 -0
  62. package/marketplace/plugins/git-forge/src/server/token.ts +139 -0
  63. package/marketplace/plugins/git-forge/src/server/write-http.test.ts +127 -0
  64. package/marketplace/plugins/git-forge/src/server/write-http.ts +110 -0
  65. package/marketplace/plugins/git-forge/src/server/writes.test.ts +218 -0
  66. package/marketplace/plugins/git-forge/src/server/writes.ts +313 -0
  67. package/marketplace/plugins/git-forge/src/server.ts +284 -0
  68. package/marketplace/plugins/git-forge/tsconfig.json +16 -0
  69. package/marketplace/plugins/session-info/package.json +4 -2
  70. package/marketplace/plugins/stats/package.json +4 -2
  71. package/package.json +6 -1
  72. package/src/client/components/common/code-surface/blame-hover.ts +188 -0
  73. package/src/client/components/common/code-surface/blame-panel.test.ts +304 -0
  74. package/src/client/components/common/code-surface/blame-panel.ts +306 -0
  75. package/src/client/components/common/code-surface/blame.test.ts +303 -0
  76. package/src/client/components/common/code-surface/blame.ts +227 -0
  77. package/src/client/components/common/code-surface/conflict-markers.test.ts +135 -0
  78. package/src/client/components/common/code-surface/conflict-markers.ts +144 -0
  79. package/src/client/components/common/code-surface/documents.ts +48 -1
  80. package/src/client/components/common/code-surface/extensions.test.ts +4 -0
  81. package/src/client/components/common/code-surface/extensions.ts +44 -0
  82. package/src/client/components/common/code-surface/fold-stack.test.ts +4 -0
  83. package/src/client/components/common/code-surface/indent.test.ts +5 -0
  84. package/src/client/components/common/code-surface/index.tsx +16 -2
  85. package/src/client/components/common/code-surface/lint-parsers.test.ts +109 -0
  86. package/src/client/components/common/code-surface/lint-parsers.ts +134 -0
  87. package/src/client/components/common/code-surface/lint.test.ts +101 -0
  88. package/src/client/components/common/code-surface/lint.ts +85 -0
  89. package/src/client/components/common/code-surface/props.ts +17 -0
  90. package/src/client/components/common/code-surface/surface-failures.test.ts +132 -0
  91. package/src/client/components/common/code-surface/theme.ts +71 -0
  92. package/src/client/components/common/header-controls/index.test.tsx +1 -1
  93. package/src/client/components/common/header-controls/plugin-entry.test.tsx +1 -1
  94. package/src/client/components/common/header-controls/shell-chrome.test.tsx +1 -1
  95. package/src/client/components/common/provider-icon/Plate.tsx +88 -0
  96. package/src/client/components/common/provider-icon/index.tsx +55 -11
  97. package/src/client/components/layout/session-sidebar/SessionsView.tsx +5 -2
  98. package/src/client/components/mobile/SessionSidebar.tsx +2 -10
  99. package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +6 -0
  100. package/src/client/components/mobile/mobile-right-sidebar/StatusBar.tsx +8 -0
  101. package/src/client/components/settings/categories/AppearanceSettings.tsx +3 -0
  102. package/src/client/components/settings/categories/ProviderSettings.tsx +43 -41
  103. package/src/client/components/settings/categories/appearance-settings/EditorBlameSection.tsx +97 -0
  104. package/src/client/components/settings/categories/panel-plugins/AvailableList.tsx +106 -54
  105. package/src/client/components/settings/categories/panel-plugins/GitFreshness.tsx +14 -1
  106. package/src/client/components/settings/categories/panel-plugins/InstalledList.tsx +39 -150
  107. package/src/client/components/settings/categories/panel-plugins/Meta.test.tsx +143 -0
  108. package/src/client/components/settings/categories/panel-plugins/Meta.tsx +157 -0
  109. package/src/client/components/settings/categories/panel-plugins/Placement.test.tsx +149 -0
  110. package/src/client/components/settings/categories/panel-plugins/Placement.tsx +99 -0
  111. package/src/client/components/settings/categories/panel-plugins/PluginCard.tsx +270 -0
  112. package/src/client/components/settings/categories/panel-plugins/Section.tsx +11 -3
  113. package/src/client/components/settings/categories/provider-settings/AddModelModal.tsx +25 -0
  114. package/src/client/components/settings/categories/provider-settings/AuthSection.tsx +135 -80
  115. package/src/client/components/settings/categories/provider-settings/DeleteProviderModal.tsx +51 -21
  116. package/src/client/components/settings/categories/provider-settings/EntryAdvanced.tsx +175 -0
  117. package/src/client/components/settings/categories/provider-settings/EntryModal.tsx +275 -0
  118. package/src/client/components/settings/categories/provider-settings/Header.tsx +40 -9
  119. package/src/client/components/settings/categories/provider-settings/LoginModal.tsx +142 -160
  120. package/src/client/components/settings/categories/provider-settings/LogoutProviderModal.tsx +2 -2
  121. package/src/client/components/settings/categories/provider-settings/ModelCapabilitiesModal.tsx +84 -46
  122. package/src/client/components/settings/categories/provider-settings/ModelsList.tsx +217 -102
  123. package/src/client/components/settings/categories/provider-settings/PresetPicker.tsx +5 -2
  124. package/src/client/components/settings/categories/provider-settings/SidebarList.plugin.test.ts +3 -3
  125. package/src/client/components/settings/categories/provider-settings/SidebarList.tsx +158 -102
  126. package/src/client/components/settings/categories/provider-settings/auth-section.test.tsx +135 -0
  127. package/src/client/components/settings/categories/provider-settings/groups.test.ts +98 -0
  128. package/src/client/components/settings/categories/provider-settings/groups.ts +108 -0
  129. package/src/client/components/settings/categories/provider-settings/layout.test.tsx +325 -0
  130. package/src/client/components/settings/categories/provider-settings/model-dialogs.test.tsx +135 -0
  131. package/src/client/components/settings/categories/provider-settings/preset-picker.test.tsx +73 -0
  132. package/src/client/components/settings/categories/usage-settings/ProviderCard.tsx +22 -2
  133. package/src/client/components/settings/categories/usage-settings/ProviderDetail.tsx +2 -0
  134. package/src/client/components/settings/categories/usage-settings/SidebarList.tsx +16 -6
  135. package/src/client/components/settings/categories/usage-settings/providers.ts +3 -0
  136. package/src/client/components/settings/categories/usage-settings/usage-and-picker.test.ts +19 -0
  137. package/src/client/components/settings/categories/usage-settings/usage-icons.test.tsx +143 -0
  138. package/src/client/components/workspace/chat-timeline/MinimapShortcuts.test.ts +2 -1
  139. package/src/client/components/workspace/chat-timeline/chat-input/GoalBanner.test.ts +23 -19
  140. package/src/client/components/workspace/chat-timeline/chat-input/ModeToggles.test.ts +23 -19
  141. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.test.ts +20 -18
  142. package/src/client/components/workspace/chat-timeline/chat-input/compact.test.tsx +19 -6
  143. package/src/client/components/workspace/chat-timeline/index.tsx +7 -16
  144. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.missing.test.ts +100 -0
  145. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.test.ts +3 -47
  146. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +8 -1
  147. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/wait-test-util.ts +62 -0
  148. package/src/client/components/workspace/diff-panel/HunkStepper.tsx +62 -0
  149. package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +29 -0
  150. package/src/client/components/workspace/diff-panel/Toolbar.tsx +32 -1
  151. package/src/client/components/workspace/diff-panel/changes.ts +69 -0
  152. package/src/client/components/workspace/diff-panel/chunks.ts +85 -0
  153. package/src/client/components/workspace/diff-panel/index.tsx +21 -1
  154. package/src/client/components/workspace/diff-panel/merge.test.ts +145 -0
  155. package/src/client/components/workspace/diff-panel/merge.ts +63 -68
  156. package/src/client/components/workspace/editor/EditorDropOverlay.tsx +6 -1
  157. package/src/client/components/workspace/editor/FileDocument.tsx +51 -1
  158. package/src/client/components/workspace/editor/FilePane.tsx +25 -1
  159. package/src/client/components/workspace/editor/StatusBar.tsx +19 -0
  160. package/src/client/components/workspace/editor/Toolbar.tsx +29 -5
  161. package/src/client/components/workspace/editor/toolbar-clusters.test.ts +86 -0
  162. package/src/client/components/workspace/editor/toolbar-clusters.tsx +48 -16
  163. package/src/client/components/workspace/editor/viewers/bytes.ts +16 -0
  164. package/src/client/components/workspace/git-panel/index.test.ts +46 -1
  165. package/src/client/components/workspace/git-panel/index.tsx +11 -38
  166. package/src/client/components/workspace/model-dropdown/Item.plugin.test.ts +66 -0
  167. package/src/client/components/workspace/model-dropdown/Item.tsx +71 -62
  168. package/src/client/components/workspace/model-dropdown/Section.tsx +31 -14
  169. package/src/client/components/workspace/model-dropdown/collapsed.ts +72 -0
  170. package/src/client/components/workspace/model-dropdown/index.tsx +42 -19
  171. package/src/client/components/workspace/model-dropdown/refresh.test.tsx +260 -0
  172. package/src/client/components/workspace/model-dropdown/trigger.test.ts +34 -0
  173. package/src/client/components/workspace/model-dropdown/use-catalog.ts +13 -2
  174. package/src/client/components/workspace/usage-panel/ProviderSelect.tsx +19 -3
  175. package/src/client/hooks/chat/omp/folder-auto-expand.ts +113 -0
  176. package/src/client/hooks/chat/omp/index.ts +20 -0
  177. package/src/client/hooks/chat/omp/pending-rows.ts +21 -0
  178. package/src/client/hooks/chat/omp/pending-sessions.test.ts +16 -1
  179. package/src/client/hooks/chat/omp/session-sidebar-controller.test.ts +102 -0
  180. package/src/client/hooks/chat/omp/session-sidebar-controller.ts +21 -1
  181. package/src/client/hooks/chat/omp/stream.ts +8 -1
  182. package/src/client/hooks/chat/timeline/index.ts +1 -1
  183. package/src/client/hooks/chat/timeline/new-chat-open.ts +60 -0
  184. package/src/client/hooks/editor/blame-settings.ts +67 -0
  185. package/src/client/hooks/editor/blame.test.ts +272 -0
  186. package/src/client/hooks/editor/blame.ts +232 -0
  187. package/src/client/hooks/editor/file-editor-types.ts +23 -0
  188. package/src/client/hooks/editor/save-file.test.ts +20 -0
  189. package/src/client/hooks/editor/use-editor-drop.test-util.ts +198 -0
  190. package/src/client/hooks/editor/use-editor-drop.test.ts +70 -175
  191. package/src/client/hooks/editor/use-editor-drop.ts +19 -36
  192. package/src/client/hooks/editor/use-file-editor.ts +7 -1
  193. package/src/client/hooks/settings/providers/actions.ts +90 -194
  194. package/src/client/hooks/settings/providers/api.ts +7 -1
  195. package/src/client/hooks/settings/providers/index.ts +171 -59
  196. package/src/client/hooks/settings/providers/login.test.ts +79 -0
  197. package/src/client/hooks/settings/providers/login.ts +276 -0
  198. package/src/client/hooks/settings/providers/messages.ts +28 -0
  199. package/src/client/hooks/settings/providers/model-actions.ts +178 -0
  200. package/src/client/hooks/ui/editor-error-toast.ts +47 -0
  201. package/src/client/hooks/ui/open-file.ts +7 -0
  202. package/src/client/hooks/ui/toasts.ts +7 -0
  203. package/src/client/hooks/workspace/file-tabs.ts +4 -1
  204. package/src/client/hooks/workspace/folder-expansion.test.ts +62 -0
  205. package/src/client/hooks/workspace/folder-expansion.ts +33 -0
  206. package/src/client/hooks/workspace/git-action-outcome.ts +86 -0
  207. package/src/client/hooks/workspace/git-actions.ts +29 -2
  208. package/src/client/hooks/workspace/plugin-contributions.test.ts +117 -0
  209. package/src/client/hooks/workspace/plugin-contributions.ts +146 -0
  210. package/src/client/lib/editor-errors.test.ts +85 -0
  211. package/src/client/lib/editor-errors.ts +71 -0
  212. package/src/client/lib/fs/resolve-open.test.ts +125 -0
  213. package/src/client/lib/fs/resolve-open.ts +90 -0
  214. package/src/client/lib/git/action-message.test.ts +82 -0
  215. package/src/client/lib/git/action-message.ts +160 -0
  216. package/src/client/lib/launch/device-files.test.ts +160 -0
  217. package/src/client/lib/launch/device-files.ts +197 -0
  218. package/src/client/lib/native/open-paths.test.ts +140 -0
  219. package/src/client/lib/native/open-paths.ts +157 -0
  220. package/src/client/lib/plugins/bootstrap.test.ts +1 -1
  221. package/src/client/main.tsx +20 -0
  222. package/src/server/db.server.test.ts +39 -0
  223. package/src/server/db.server.ts +21 -2
  224. package/src/server/lib/db/schema.ts +2 -0
  225. package/src/server/lib/fs/external-roots.test.ts +100 -0
  226. package/src/server/lib/fs/external-roots.ts +101 -0
  227. package/src/server/lib/fs/git-actions-branch.ts +15 -2
  228. package/src/server/lib/fs/git-actions-shared.ts +24 -0
  229. package/src/server/lib/fs/git-actions-sync.test.ts +18 -7
  230. package/src/server/lib/fs/git-actions-worktree.ts +5 -1
  231. package/src/server/lib/fs/git-blame.test.ts +206 -0
  232. package/src/server/lib/fs/git-blame.ts +189 -0
  233. package/src/server/lib/fs/root.ts +14 -0
  234. package/src/server/lib/http/same-origin.ts +28 -7
  235. package/src/server/lib/models/catalog.server.ts +230 -0
  236. package/src/server/lib/models/catalog.test.ts +83 -0
  237. package/src/server/lib/models/credential-origin.server.ts +108 -0
  238. package/src/server/lib/models/credential-origin.test.ts +132 -0
  239. package/src/server/lib/models/provider-icon-fetch.ts +115 -0
  240. package/src/server/lib/models/provider-icon-parse.test.ts +134 -0
  241. package/src/server/lib/models/provider-icon-parse.ts +139 -0
  242. package/src/server/lib/models/provider-icon-store.server.ts +118 -0
  243. package/src/server/lib/models/provider-icon-store.test.ts +107 -0
  244. package/src/server/lib/models/provider-icon.server.ts +209 -0
  245. package/src/server/lib/models/provider-items.server.ts +211 -37
  246. package/src/server/lib/models/provider-items.test.ts +201 -0
  247. package/src/server/lib/models/provider-registry.server.test.ts +27 -9
  248. package/src/server/lib/models/provider-registry.server.ts +160 -67
  249. package/src/server/lib/models/registry.server.test.ts +3 -3
  250. package/src/server/lib/models/registry.server.ts +8 -0
  251. package/src/server/lib/omp/config/definitions.test.ts +5 -4
  252. package/src/server/lib/omp/config/models-config.ts +42 -0
  253. package/src/server/lib/omp/config/provider-entry.test.ts +205 -0
  254. package/src/server/lib/omp/config/provider-entry.ts +197 -0
  255. package/src/server/lib/omp/config/provider-seeds.ts +10 -0
  256. package/src/server/lib/omp/core/models-json.ts +69 -0
  257. package/src/server/lib/omp/session/messages.ts +17 -3
  258. package/src/server/lib/omp/session/sidebar-data.canonical.test.ts +13 -3
  259. package/src/server/lib/panels/bundled-catalog.server.ts +7 -0
  260. package/src/server/lib/panels/catalog-offers.server.ts +68 -0
  261. package/src/server/lib/panels/git-update.server.test.ts +14 -0
  262. package/src/server/lib/panels/git-update.server.ts +63 -1
  263. package/src/server/lib/panels/manifest.test.ts +65 -0
  264. package/src/server/lib/panels/manifest.ts +55 -1
  265. package/src/server/lib/panels/offers.server.test.ts +191 -0
  266. package/src/server/lib/panels/offers.server.ts +108 -0
  267. package/src/server/lib/panels/registry.server.test.ts +35 -1
  268. package/src/server/lib/panels/registry.server.ts +51 -142
  269. package/src/server/lib/panels/scan.server.ts +162 -0
  270. package/src/server/lib/route-adapter.ts +13 -2
  271. package/src/server/lib/tray/tray.test.ts +2 -1
  272. package/src/server/lib/usage/providers.server.ts +4 -1
  273. package/src/server/routes/chat/session.ts +7 -1
  274. package/src/server/routes/fs/git-blame-loader.test.ts +146 -0
  275. package/src/server/routes/fs/git.ts +17 -0
  276. package/src/server/routes/fs/grant.test.ts +135 -0
  277. package/src/server/routes/fs/grant.ts +79 -0
  278. package/src/server/routes/fs/index.ts +2 -0
  279. package/src/server/routes/fs/scope.ts +34 -17
  280. package/src/server/routes/omp/login.ts +41 -14
  281. package/src/server/routes/omp/routes.test.ts +23 -10
  282. package/src/server/routes/settings/index.ts +8 -0
  283. package/src/server/routes/settings/provider-entry.ts +108 -0
  284. package/src/server/routes/settings/provider-icon.ts +99 -0
  285. package/src/server/routes/settings/provider-model.ts +4 -1
  286. package/src/server/routes/settings/provider-refresh.ts +87 -0
  287. package/src/server/routes/settings/providers.ts +31 -2
  288. package/src/shared/lib/chat/omp/fold.test.ts +34 -0
  289. package/src/shared/lib/chat/omp/tool-frames.test.ts +20 -0
  290. package/src/shared/lib/chat/omp/tool-frames.ts +5 -1
  291. package/src/shared/lib/chat/omp/tool-results.ts +29 -3
  292. package/src/shared/lib/chat/tool/wait-facts.ts +4 -0
  293. package/src/shared/lib/fs/git-blame.test.ts +266 -0
  294. package/src/shared/lib/fs/git-blame.ts +246 -0
  295. package/src/shared/lib/git/url.ts +18 -0
  296. package/src/shared/lib/markdown/sanitize.test.ts +57 -0
  297. package/src/shared/lib/markdown/sanitize.ts +30 -7
  298. package/src/shared/lib/models/provider/connection.ts +135 -0
  299. package/src/shared/lib/models/provider/dialect.test.ts +4 -2
  300. package/src/shared/lib/models/provider/dialect.ts +76 -6
  301. package/src/shared/lib/models/provider/models.ts +2 -0
  302. package/src/shared/lib/models/provider/origin.ts +94 -0
  303. package/src/shared/lib/models/provider/provider-ui.test.ts +7 -0
  304. package/src/shared/lib/models/server-cache.ts +8 -3
  305. package/src/shared/lib/omp/session/messages-map-pending.test.ts +110 -0
  306. package/src/shared/lib/omp/session/messages-map.ts +34 -2
  307. package/src/shared/lib/omp/session/prewarm-misc.test.ts +7 -90
  308. package/src/shared/lib/omp/session/prewarm.test.ts +133 -0
  309. package/src/shared/lib/omp/session/prewarm.ts +23 -1
  310. package/src/shared/lib/omp/session/wait.test.ts +28 -0
  311. package/src/shared/lib/omp/session/wait.ts +38 -3
  312. package/src/shared/lib/panels/slot-labels.test.ts +61 -0
  313. package/src/shared/lib/panels/slot-labels.ts +133 -0
  314. package/src/shared/lib/workspace/panel-ids.ts +11 -0
  315. package/src/shared/types/fs.ts +6 -0
  316. package/src/shared/types/index.ts +1 -0
  317. package/src/shared/types/models.ts +12 -0
  318. package/src/shared/types/panels-registry.ts +277 -0
  319. package/src/shared/types/panels.ts +46 -213
  320. package/src/shared/types/settings/provider.ts +58 -0
  321. package/src/shared/types/settings/usage.ts +10 -0
  322. package/src/test-support/pristine-globals.ts +7 -0
  323. package/dist/client/static/js/index-bq6qeayr.css +0 -1
  324. package/src/client/components/settings/categories/provider-settings/ReconnectModal.tsx +0 -184
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * What a card says a plugin contributes.
8
+ *
9
+ * Two facts are pinned here and both are easy to get wrong in a way nothing
10
+ * notices:
11
+ *
12
+ * - The live registration wins over the manifest's declaration. A card that
13
+ * showed the declaration would be repeating the author's intent at a reader
14
+ * who is deciding whether to switch the plugin on.
15
+ * - A fallback is MARKED as a declaration. `session-info` declares three slots
16
+ * and registers three, but a disabled plugin registers nothing at all — and a
17
+ * badge that looked identical in both cases would make a claim read as a fact.
18
+ */
19
+
20
+ import { beforeEach, describe, expect, test } from 'bun:test';
21
+ import { definePluginApp } from '@kabmo-labs/kabmo-sdk/app';
22
+ import { registerPluginBundle, resetPluginSlots } from '@/client/lib/plugins/slots';
23
+ import { resetListSlots } from '@/client/lib/plugins/list-slots';
24
+ import {
25
+ contributionBadges,
26
+ declaredContributions,
27
+ hasContributions,
28
+ readPluginContributions,
29
+ } from '@/client/hooks/workspace/plugin-contributions';
30
+
31
+ const Noop = () => null;
32
+
33
+ beforeEach(() => {
34
+ resetPluginSlots();
35
+ resetListSlots();
36
+ });
37
+
38
+ describe('declaredContributions', () => {
39
+ test('names each declared slot in words, in the canonical order', () => {
40
+ const declared = declaredContributions(['headerPanel', 'rightPanel']);
41
+ expect(declared.map((entry) => entry.label)).toEqual(['Panel view', 'Header button']);
42
+ expect(declared.every((entry) => entry.kind === 'panel')).toBe(true);
43
+ });
44
+
45
+ test('an undeclared manifest contributes nothing to show', () => {
46
+ expect(declaredContributions(undefined)).toEqual([]);
47
+ expect(declaredContributions([])).toEqual([]);
48
+ });
49
+ });
50
+
51
+ describe('readPluginContributions', () => {
52
+ test('reads the positional slots a bundle registered, in the card order', () => {
53
+ registerPluginBundle('demo', {
54
+ default: definePluginApp((app) => {
55
+ app.rightPanel({ id: 'r', title: 'Right', component: Noop });
56
+ app.headerPanel({ id: 'h', title: 'Header', component: Noop });
57
+ }),
58
+ });
59
+ const live = readPluginContributions('demo');
60
+ expect(live.live).toBe(true);
61
+ expect(live.panels.map((entry) => entry.label)).toEqual(['Panel view', 'Header button']);
62
+ // The title the bundle gave it rides along, so the badge can name the view.
63
+ expect(live.panels[0]?.title).toBe('Right');
64
+ });
65
+
66
+ test('counts the rows a bundle contributes to one list slot', () => {
67
+ registerPluginBundle('demo', {
68
+ default: definePluginApp((app) => {
69
+ app.messageAction({ id: 'copy', component: Noop });
70
+ app.messageAction({ id: 'quote', component: Noop });
71
+ }),
72
+ });
73
+ const live = readPluginContributions('demo');
74
+ expect(live.panels).toEqual([]);
75
+ expect(live.rows).toHaveLength(1);
76
+ // ONE badge per slot with a count, not one badge per row: a plugin adding
77
+ // four actions must not push a card's layout apart.
78
+ expect(live.rows[0]).toMatchObject({ label: 'Message actions', kind: 'row', count: 2 });
79
+ });
80
+
81
+ test('a plugin whose bundle never registered reads as not live', () => {
82
+ expect(readPluginContributions('missing')).toEqual({ live: false, panels: [], rows: [] });
83
+ });
84
+ });
85
+
86
+ describe('contributionBadges', () => {
87
+ test('a live registration is the answer, and it is not marked as a declaration', () => {
88
+ registerPluginBundle('demo', {
89
+ default: definePluginApp((app) => app.rightPanel({ id: 'r', title: 'Right', component: Noop })),
90
+ });
91
+ // The manifest claims a slot the bundle never registered. The bundle wins.
92
+ const badges = contributionBadges(readPluginContributions('demo'), ['rightPanel', 'settingsSection']);
93
+ expect(badges.fromDeclaration).toBe(false);
94
+ expect(badges.items.map((item) => item.label)).toEqual(['Panel view']);
95
+ });
96
+
97
+ test('a disabled plugin falls back to its declaration, marked as one', () => {
98
+ // Nothing registered — exactly the state of a switched-off plugin, whose
99
+ // bundle is never imported.
100
+ const badges = contributionBadges(readPluginContributions('demo'), ['rightPanel', 'panel']);
101
+ expect(badges.items.map((item) => item.label)).toEqual(['Panel view', 'Editor takeover']);
102
+ expect(badges.fromDeclaration).toBe(true);
103
+ });
104
+
105
+ test('nothing registered and nothing declared shows no badges at all', () => {
106
+ const badges = contributionBadges(readPluginContributions('demo'), undefined);
107
+ expect(badges.items).toEqual([]);
108
+ expect(badges.fromDeclaration).toBe(false);
109
+ });
110
+ });
111
+
112
+ describe('hasContributions', () => {
113
+ test('a plugin that registers nothing says so rather than looking empty', () => {
114
+ expect(hasContributions({ live: true, panels: [], rows: [] })).toBe(false);
115
+ expect(hasContributions({ live: true, panels: declaredContributions(['panel']), rows: [] })).toBe(true);
116
+ });
117
+ });
@@ -0,0 +1,146 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * What one installed plugin actually contributes, as its bundle registered it.
8
+ *
9
+ * The manifest may DECLARE slots, but a declaration is a claim: the bundle is
10
+ * what the host imported, and a plugin can register more, fewer or different
11
+ * slots than its manifest says. A card that showed the declaration would be
12
+ * repeating the author's intent back at the reader; this reads the live
13
+ * registration, and the card falls back to the declaration only for a plugin
14
+ * whose bundle is not loaded (disabled, failed, or not built yet) — with the
15
+ * fallback marked, so a claim never reads as a fact.
16
+ *
17
+ * Both stores are subscribed in ONE place. The positional slots live in
18
+ * `slots.ts` and the list rows in `list-slots.ts`, and they publish separately:
19
+ * a plugin that registers a panel and a message action does so in two writes,
20
+ * so a card reading only one of them would show half its contributions for a
21
+ * frame — or, worse, permanently if the other store's publish happened first.
22
+ */
23
+
24
+ import { useEffect, useState } from 'preact/hooks';
25
+ import type { ListSlotName } from '@kabmo-labs/kabmo-sdk';
26
+ import { LIST_SLOT_NAMES } from '@kabmo-labs/kabmo-sdk';
27
+ import type { PanelSlotName } from '@/shared/types';
28
+ import { PANEL_SLOT_NAMES } from '@/shared/types';
29
+ import { listSlotRows, subscribeListSlots } from '@/client/lib/plugins/list-slots';
30
+ import { pluginSlotState, subscribePluginSlots } from '@/client/lib/plugins/slots';
31
+ import { listSlotLabel, positionalSlotLabel } from '@/shared/lib/panels/slot-labels';
32
+
33
+ /** One contribution, ready for a badge. */
34
+ export interface PluginContribution {
35
+ /** Stable key for a list. */
36
+ key: string;
37
+ label: string;
38
+ detail: string;
39
+ kind: 'panel' | 'row';
40
+ /** How many rows this plugin contributes to a list slot; 1 for a panel. */
41
+ count: number;
42
+ /** The title the plugin gave it, when it gave one. */
43
+ title?: string;
44
+ }
45
+
46
+ /** Everything one plugin contributes, split by kind so a card can order it. */
47
+ export interface PluginContributions {
48
+ /** True once the bundle's registrations have been read. */
49
+ live: boolean;
50
+ panels: PluginContribution[];
51
+ rows: PluginContribution[];
52
+ }
53
+
54
+ const EMPTY: PluginContributions = { live: false, panels: [], rows: [] };
55
+
56
+ /**
57
+ * One plugin's live contributions, read from both stores.
58
+ *
59
+ * Exported because it is the whole decision, and a hook cannot be asked what it
60
+ * would return without a render: the tests drive this directly, and the hook is
61
+ * only the subscription around it.
62
+ */
63
+ export function readPluginContributions(pluginId: string): PluginContributions {
64
+ const slots = pluginSlotState().slots.get(pluginId);
65
+ if (!slots) return EMPTY;
66
+ const panels: PluginContribution[] = [];
67
+ for (const slot of PANEL_SLOT_NAMES) {
68
+ if (!slots.components[slot]) continue;
69
+ const label = positionalSlotLabel(slot);
70
+ const title = slots.titles[slot];
71
+ panels.push({
72
+ key: slot,
73
+ label: label.label,
74
+ detail: label.detail,
75
+ kind: 'panel',
76
+ count: 1,
77
+ ...(title ? { title } : {}),
78
+ });
79
+ }
80
+
81
+ const rows: PluginContribution[] = [];
82
+ for (const slot of LIST_SLOT_NAMES) {
83
+ const count = listSlotRows(slot).filter((entry) => entry.pluginId === pluginId).length;
84
+ if (count === 0) continue;
85
+ const label = listSlotLabel(slot as ListSlotName);
86
+ rows.push({ key: slot, label: label.label, detail: label.detail, kind: 'row', count });
87
+ }
88
+
89
+ return { live: true, panels, rows };
90
+ }
91
+
92
+ /**
93
+ * One plugin's live contributions.
94
+ *
95
+ * Re-read on either store's publish, because the two are written at different
96
+ * moments and a card must not settle on a half-read registration.
97
+ */
98
+ export function usePluginContributions(pluginId: string): PluginContributions {
99
+ const [state, setState] = useState<PluginContributions>(() => readPluginContributions(pluginId));
100
+ useEffect(() => {
101
+ setState(readPluginContributions(pluginId));
102
+ const offSlots = subscribePluginSlots(() => setState(readPluginContributions(pluginId)));
103
+ const offRows = subscribeListSlots(() => setState(readPluginContributions(pluginId)));
104
+ return () => {
105
+ offSlots();
106
+ offRows();
107
+ };
108
+ }, [pluginId]);
109
+ return state;
110
+ }
111
+
112
+ /**
113
+ * Declared contributions, for a plugin whose bundle has not registered anything.
114
+ *
115
+ * Used ONLY as a fallback, and the card marks it: a manifest's `slots` is the
116
+ * author's claim about what the plugin will do, while a live registration is
117
+ * what it does. Falling back is still better than a card that says nothing at
118
+ * all about a plugin the user has installed but disabled.
119
+ */
120
+ export function declaredContributions(slots: readonly PanelSlotName[] | undefined): PluginContribution[] {
121
+ if (!slots || slots.length === 0) return [];
122
+ const declared = new Set(slots);
123
+ // Ordered by the canonical table, not by the caller's array: the manifest
124
+ // parser already normalizes it, and a second caller must not be able to list
125
+ // the same plugin's slots in a different order.
126
+ return PANEL_SLOT_NAMES.filter((slot) => declared.has(slot)).map((slot) => {
127
+ const label = positionalSlotLabel(slot);
128
+ return { key: slot, label: label.label, detail: label.detail, kind: 'panel' as const, count: 1 };
129
+ });
130
+ }
131
+
132
+ /** The badges a card draws, live registrations first and declarations as a fallback. */
133
+ export function contributionBadges(
134
+ contributions: PluginContributions,
135
+ declared: readonly PanelSlotName[] | undefined,
136
+ ): { items: PluginContribution[]; fromDeclaration: boolean } {
137
+ const live = [...contributions.panels, ...contributions.rows];
138
+ if (live.length > 0) return { items: live, fromDeclaration: false };
139
+ const fallback = declaredContributions(declared);
140
+ return { items: fallback, fromDeclaration: fallback.length > 0 };
141
+ }
142
+
143
+ /** Whether a plugin registers anything at all — the answer a "loaded, but…" line needs. */
144
+ export function hasContributions(contributions: PluginContributions): boolean {
145
+ return contributions.panels.length > 0 || contributions.rows.length > 0;
146
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Editor exceptions: reported once, and never silently.
8
+ *
9
+ * The suppression is the part with a bug in it if it is wrong in either
10
+ * direction — a plugin that throws per viewport update would either bury the
11
+ * editor in toasts or say nothing at all, and both look like "the editor is
12
+ * fine" until the missing feature is noticed.
13
+ *
14
+ * The event is read through a real `window` (happy-dom) rather than a stub: the
15
+ * name and the `detail` shape are the contract the two shells subscribe to.
16
+ */
17
+
18
+ import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
19
+ import { Window } from 'happy-dom';
20
+
21
+ import {
22
+ EDITOR_ERROR_EVENT,
23
+ EDITOR_ERROR_WINDOW_MS,
24
+ reportEditorError,
25
+ resetEditorErrorReports,
26
+ } from '@/client/lib/editor-errors';
27
+ import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
28
+
29
+ interface Captured {
30
+ message: string;
31
+ }
32
+
33
+ let captured: Captured[] = [];
34
+
35
+ beforeAll(() => {
36
+ installDomGlobals(new Window({ url: 'http://localhost' }));
37
+ window.addEventListener(EDITOR_ERROR_EVENT, (event) => {
38
+ captured.push((event as CustomEvent<Captured>).detail);
39
+ });
40
+ });
41
+
42
+ afterAll(() => {
43
+ restoreDomGlobals();
44
+ });
45
+
46
+ beforeEach(() => {
47
+ resetEditorErrorReports();
48
+ captured = [];
49
+ });
50
+
51
+ describe('reportEditorError', () => {
52
+ test('an Error is reported with its message', () => {
53
+ expect(reportEditorError(new Error('decoration failed'), 1000)).toBe(true);
54
+ expect(captured).toEqual([{ message: 'decoration failed' }]);
55
+ });
56
+
57
+ test('the same message inside the window is suppressed', () => {
58
+ reportEditorError(new Error('boom'), 1000);
59
+ expect(reportEditorError(new Error('boom'), 1000 + EDITOR_ERROR_WINDOW_MS - 1)).toBe(false);
60
+ expect(captured).toHaveLength(1);
61
+ });
62
+
63
+ test('the same message is reported again after the window', () => {
64
+ reportEditorError(new Error('boom'), 1000);
65
+ expect(reportEditorError(new Error('boom'), 1000 + EDITOR_ERROR_WINDOW_MS)).toBe(true);
66
+ expect(captured).toHaveLength(2);
67
+ });
68
+
69
+ test('a different message is not suppressed by its neighbour', () => {
70
+ reportEditorError(new Error('one'), 1000);
71
+ expect(reportEditorError(new Error('two'), 1001)).toBe(true);
72
+ expect(captured.map((entry) => entry.message)).toEqual(['one', 'two']);
73
+ });
74
+
75
+ test('a thrown non-Error still has a sentence', () => {
76
+ expect(reportEditorError('plain string', 1000)).toBe(true);
77
+ expect(captured[0].message).toBe('plain string');
78
+ expect(reportEditorError(undefined, 2000)).toBe(true);
79
+ expect(captured[1].message).toBe('The editor hit an internal error');
80
+ });
81
+
82
+ test('the event name is the one the shells subscribe to', () => {
83
+ expect(EDITOR_ERROR_EVENT).toBe('kabmo:editor-error');
84
+ });
85
+ });
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Editor exceptions, surfaced instead of swallowed.
8
+ *
9
+ * A `ViewPlugin` that throws does not break the page: CodeMirror catches it and
10
+ * reports it through `EditorView.exceptionSink`, whose default is
11
+ * `console.error`. In a browser console nobody is looking at, that is a feature
12
+ * that silently stopped working — a decoration plugin that throws on every
13
+ * viewport update looks exactly like one that has nothing to draw.
14
+ *
15
+ * ## Deduplicated, because a plugin throws per update
16
+ *
17
+ * The same exception repeats on every keystroke and every scroll. A toast per
18
+ * occurrence would bury the editor it is describing, so one message is reported
19
+ * once per window (`EDITOR_ERROR_WINDOW_MS`) and the rest are dropped.
20
+ *
21
+ * ## A DOM event, not a store
22
+ *
23
+ * The surface is mounted by several owners (the desktop column, the phone's
24
+ * overlay, the diff panel) and the toast host lives in the layout above all of
25
+ * them. An event is the wiring this app already uses for a cross-tree signal
26
+ * (`omp:theme-changed`, `EDITOR_TYPING_CHANGED_EVENT`), and it keeps the editor
27
+ * from importing a layout component.
28
+ */
29
+
30
+ /** Fired with `{ detail: { message } }` when the editor reports an exception. */
31
+ export const EDITOR_ERROR_EVENT = 'kabmo:editor-error';
32
+
33
+ /** How long one message stays suppressed after being reported. */
34
+ export const EDITOR_ERROR_WINDOW_MS = 5000;
35
+
36
+ /** Message → the timestamp it was last reported at. */
37
+ const reported = new Map<string, number>();
38
+
39
+ /** Test seam: forget every suppression. */
40
+ export function resetEditorErrorReports(): void {
41
+ reported.clear();
42
+ }
43
+
44
+ /** The text a thrown value should be shown as. */
45
+ function messageOf(error: unknown): string {
46
+ if (error instanceof Error && error.message) return error.message;
47
+ if (typeof error === 'string' && error) return error;
48
+ return 'The editor hit an internal error';
49
+ }
50
+
51
+ /**
52
+ * Report an editor exception to whoever is listening.
53
+ *
54
+ * Returns whether it was actually emitted, so a test can assert the suppression
55
+ * without a DOM.
56
+ */
57
+ export function reportEditorError(error: unknown, now = Date.now()): boolean {
58
+ const message = messageOf(error);
59
+ const last = reported.get(message);
60
+ if (last !== undefined && now - last < EDITOR_ERROR_WINDOW_MS) return false;
61
+ reported.set(message, now);
62
+ // A long session must not grow this map by one entry per distinct message.
63
+ if (reported.size > 32) {
64
+ const cutoff = now - EDITOR_ERROR_WINDOW_MS;
65
+ for (const [key, at] of reported) if (at < cutoff) reported.delete(key);
66
+ }
67
+ if (typeof window !== 'undefined') {
68
+ window.dispatchEvent(new CustomEvent(EDITOR_ERROR_EVENT, { detail: { message } }));
69
+ }
70
+ return true;
71
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Turning an absolute reference into a tab's scope, and the ONE retry a real
8
+ * gesture earns.
9
+ *
10
+ * The rules under test are the ones that fail silently: a file inside a
11
+ * workspace must not be granted (a grant is a lasting permission and the list is
12
+ * bounded), a refusal that is not `outside_workspace` must not be retried, and a
13
+ * file the editor cannot draw must be reported rather than opened.
14
+ */
15
+
16
+ import { describe, expect, test } from 'bun:test';
17
+ import { resolveOpenableFile } from '@/client/lib/fs/resolve-open';
18
+
19
+ interface Call {
20
+ url: string;
21
+ method: string;
22
+ }
23
+
24
+ /** A stub `fetch` answering a scripted sequence, one response per call. */
25
+ function stubFetch(answers: { status: number; body: unknown }[]) {
26
+ const calls: Call[] = [];
27
+ const original = globalThis.fetch;
28
+ globalThis.fetch = (async (url: string, init?: RequestInit) => {
29
+ calls.push({ url: String(url), method: init?.method ?? 'GET' });
30
+ const next = answers.shift() ?? { status: 500, body: {} };
31
+ return { ok: next.status >= 200 && next.status < 300, status: next.status, json: async () => next.body };
32
+ }) as unknown as typeof globalThis.fetch;
33
+ return { calls, restore: () => (globalThis.fetch = original) };
34
+ }
35
+
36
+ describe('resolveOpenableFile', () => {
37
+ test('a file inside a workspace resolves through the scope route alone', async () => {
38
+ const stub = stubFetch([
39
+ { status: 200, body: { root: '/w', path: 'a/b.md', name: 'b.md', kind: 'text' } },
40
+ ]);
41
+ try {
42
+ const result = await resolveOpenableFile('/w/a/b.md');
43
+ expect(result.ok).toBe(true);
44
+ if (result.ok) expect(result.file).toEqual({ root: '/w', path: 'a/b.md', name: 'b.md', kind: 'text' });
45
+ // No grant: the file is already readable, and granting would evict a real
46
+ // external root from the bounded list.
47
+ expect(stub.calls.map((call) => call.method)).toEqual(['GET']);
48
+ } finally {
49
+ stub.restore();
50
+ }
51
+ });
52
+
53
+ test('a file outside every workspace is granted, then resolved', async () => {
54
+ const stub = stubFetch([
55
+ { status: 403, body: { error: 'outside', code: 'outside_workspace' } },
56
+ { status: 200, body: { root: '/Users/x/Desktop', path: 'r.pdf', name: 'r.pdf', kind: 'document' } },
57
+ ]);
58
+ try {
59
+ const result = await resolveOpenableFile('/Users/x/Desktop/r.pdf');
60
+ expect(result.ok).toBe(true);
61
+ if (result.ok) expect(result.file.root).toBe('/Users/x/Desktop');
62
+ expect(stub.calls.map((call) => call.method)).toEqual(['GET', 'POST']);
63
+ expect(stub.calls[1].url).toBe('/api/fs/grant');
64
+ } finally {
65
+ stub.restore();
66
+ }
67
+ });
68
+
69
+ test('any other refusal is final — no grant is attempted', async () => {
70
+ const stub = stubFetch([
71
+ { status: 404, body: { error: 'File not found' } },
72
+ { status: 200, body: { root: '/w', path: 'x', name: 'x' } },
73
+ ]);
74
+ try {
75
+ const result = await resolveOpenableFile('/w/x');
76
+ expect(result).toEqual({ ok: false, message: 'File not found' });
77
+ expect(stub.calls.length).toBe(1);
78
+ } finally {
79
+ stub.restore();
80
+ }
81
+ });
82
+
83
+ test('a binary or oversized file is refused with the server’s sentence', async () => {
84
+ const stub = stubFetch([
85
+ {
86
+ status: 200,
87
+ body: { root: '/w', path: 'a.bin', name: 'a.bin', kind: 'binary', error: 'This file is not text, so it cannot be shown in the editor.' },
88
+ },
89
+ ]);
90
+ try {
91
+ const result = await resolveOpenableFile('/w/a.bin');
92
+ expect(result.ok).toBe(false);
93
+ if (!result.ok) expect(result.message).toContain('not text');
94
+ } finally {
95
+ stub.restore();
96
+ }
97
+ });
98
+
99
+ test('a grant that refuses keeps the grant’s own reason', async () => {
100
+ const stub = stubFetch([
101
+ { status: 403, body: { error: 'outside', code: 'outside_workspace' } },
102
+ { status: 400, body: { error: 'That directory cannot be opened from here.' } },
103
+ ]);
104
+ try {
105
+ const result = await resolveOpenableFile('/r.pdf');
106
+ expect(result).toEqual({ ok: false, message: 'That directory cannot be opened from here.' });
107
+ } finally {
108
+ stub.restore();
109
+ }
110
+ });
111
+
112
+ test('an unreachable console is a sentence, not a throw', async () => {
113
+ const original = globalThis.fetch;
114
+ globalThis.fetch = (async () => {
115
+ throw new Error('offline');
116
+ }) as unknown as typeof globalThis.fetch;
117
+ try {
118
+ const result = await resolveOpenableFile('/w/a.md');
119
+ expect(result.ok).toBe(false);
120
+ if (!result.ok) expect(result.message).toContain('Could not reach the console');
121
+ } finally {
122
+ globalThis.fetch = original;
123
+ }
124
+ });
125
+ });
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Turning an absolute path the OS handed over into a tab's scope.
8
+ *
9
+ * Three gestures produce one: a file dropped on the editor, a double-click in
10
+ * Finder/Explorer, and "Open With → Kabmo". All three name a file ABSOLUTELY —
11
+ * a file manager cannot know which workspace the page considers active — while
12
+ * every other surface in Kabmo works from `root` + a path relative to it.
13
+ *
14
+ * The server does the translation (`/api/fs/scope`) through the allow-list that
15
+ * governs every fs route, and this module adds the one step a real gesture
16
+ * needs: a file outside every workspace is refused with
17
+ * `code: 'outside_workspace'`, and THAT is a refusal the user can answer — they
18
+ * pointed at the file — so it retries through `POST /api/fs/grant`, which
19
+ * registers the file's own directory and answers the same payload.
20
+ *
21
+ * The order matters and is not an optimisation. A file inside a workspace must
22
+ * NOT be granted: a grant is a lasting permission for a directory, and the list
23
+ * is bounded, so granting every ordinary open would evict the real ones.
24
+ */
25
+
26
+ /** The scope a tab needs, as both routes answer it. */
27
+ export interface ResolvedFileScope {
28
+ root: string;
29
+ path: string;
30
+ name: string;
31
+ /** The editor's classification: `text`, `image`, `document`, … */
32
+ kind?: string;
33
+ size?: number;
34
+ /** The sentence for a kind the editor cannot show, when there is one. */
35
+ error?: string;
36
+ }
37
+
38
+ export type OpenableFile =
39
+ | { ok: true; file: ResolvedFileScope }
40
+ | { ok: false; message: string };
41
+
42
+ /** The refusal body, which carries the one code this retries on. */
43
+ interface ScopeRefusal {
44
+ error?: string;
45
+ code?: string;
46
+ }
47
+
48
+ async function readBody(res: Response): Promise<ResolvedFileScope & ScopeRefusal> {
49
+ return ((await res.json().catch(() => null)) ?? {}) as ResolvedFileScope & ScopeRefusal;
50
+ }
51
+
52
+ /**
53
+ * Resolve `reference` (an absolute path, or a `file://` URL) to a scope a tab
54
+ * can open.
55
+ *
56
+ * A file the editor cannot show is reported here, with the server's own
57
+ * sentence, rather than opened as a tab whose whole content is that sentence.
58
+ */
59
+ export async function resolveOpenableFile(reference: string): Promise<OpenableFile> {
60
+ let res: Response;
61
+ try {
62
+ res = await fetch(`/api/fs/scope?path=${encodeURIComponent(reference)}`);
63
+ } catch {
64
+ return { ok: false, message: 'Could not reach the console to open that file.' };
65
+ }
66
+
67
+ let body = await readBody(res);
68
+ if (!res.ok && body.code === 'outside_workspace') {
69
+ // The file is real and the user pointed at it; the only thing missing is
70
+ // permission for its directory.
71
+ try {
72
+ res = await fetch('/api/fs/grant', {
73
+ method: 'POST',
74
+ headers: { 'content-type': 'application/json' },
75
+ body: JSON.stringify({ path: reference }),
76
+ });
77
+ } catch {
78
+ return { ok: false, message: 'Could not reach the console to open that file.' };
79
+ }
80
+ body = await readBody(res);
81
+ }
82
+
83
+ if (!res.ok || !body.path || !body.root) {
84
+ return { ok: false, message: body.error ?? `Could not open that file (HTTP ${res.status}).` };
85
+ }
86
+ if (body.kind === 'binary' || body.kind === 'too-large') {
87
+ return { ok: false, message: body.error ?? `${body.name ?? body.path} cannot be opened here.` };
88
+ }
89
+ return { ok: true, file: { root: body.root, path: body.path, name: body.name, kind: body.kind, size: body.size } };
90
+ }