@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
@@ -32,7 +32,10 @@ import { codeSafeAutolink } from '@/shared/lib/markdown/autolink';
32
32
  import remend from 'remend';
33
33
  import { highlightCode } from '@/shared/lib/code/syntax-highlight';
34
34
  import { directiveBlockHtml, isDirectiveLanguage, parseDirectiveAttributes } from '@/shared/lib/markdown/directives';
35
- import { MATH_PENDING_CLASS } from '@/shared/lib/markdown/katex';
35
+ import { mathExtensions, setProseEscaped } from '@/shared/lib/markdown/math-extensions';
36
+ import { registerBlockMapper } from '@/shared/lib/markdown/blocks/renderer';
37
+ import { mapBlocks } from '@/shared/lib/markdown/blocks/scan';
38
+ import type { MdBlock } from '@/shared/lib/markdown/blocks/types';
36
39
 
37
40
  /** Escape HTML for safe interpolation inside highlighted output. */
38
41
  function escapeHtml(value: string): string {
@@ -44,56 +47,6 @@ function escapeHtml(value: string): string {
44
47
  .replace(/'/g, ''');
45
48
  }
46
49
 
47
- /**
48
- * True while `renderMarkdown` parses prose it escaped itself. Set around the
49
- * `marked.parse` call, read by `renderMathPlaceholder`. Synchronous by
50
- * construction: `marked.parse` never awaits, so no nested `renderMarkdown`
51
- * can observe the flag mid-parse.
52
- */
53
- let proseEscaped = false;
54
-
55
- /**
56
- * Emit a math placeholder instead of rendering KaTeX inline.
57
- *
58
- * `katex` is ~522 kB of JS worth loading only when a message contains math, so
59
- * it is not imported here. The placeholder carries the TeX source and the
60
- * display flag; `katex.ts` swaps it for real markup once the library lands
61
- * (see `hydrateMathBlocks`).
62
- *
63
- * Two things happen to the captured source here, and both exist because this
64
- * renderer is the one place all four math extensions funnel through:
65
- *
66
- * 1. **Entities are reversed** when `renderMarkdown` escaped the prose itself
67
- * (`proseEscaped`). `escapeHtmlOutsideCode` rewrites the source BEFORE
68
- * marked sees it, so a tokenizer captures `x &lt; y` rather than `x < y` and
69
- * KaTeX reports a syntax error for a perfectly good formula. Only the three
70
- * entities that escaper emits are reversed, and only when it ran — a
71
- * document (`allowHtml`) whose own text legitimately holds `&amp;` is left
72
- * alone.
73
- * 2. **The result is percent-encoded**, the way a mermaid block carries its
74
- * source. HTML-escaping the payload instead re-escapes what step 1 just
75
- * restored, and the attribute round-trip hides the extra layer from both
76
- * ends. An encoded payload holds no HTML-special character, so nothing
77
- * between here and `renderPlaceholder` can alter it.
78
- */
79
- function renderMathPlaceholder(tex: string, displayMode: boolean): string {
80
- const restored = proseEscaped
81
- ? tex.replace(/&(amp|lt|gt);/g, (_, name: string) => (name === 'amp' ? '&' : name === 'lt' ? '<' : '>'))
82
- : tex;
83
- const display = displayMode ? ' data-math-display="1"' : '';
84
- const span = `<span class="${MATH_PENDING_CLASS}" data-math="${encodeURIComponent(restored)}"${display}></span>`;
85
- // Block math must stay inside a block element: DOMPurify drops a bare
86
- // `<span>` that sits at the top level of the fragment, which silently deleted
87
- // every `$$…$$` formula. KaTeX's own output is inline-level too, so the old
88
- // synchronous renderer relied on the same wrapper being present.
89
- return displayMode ? `<p>${span}</p>` : span;
90
- }
91
-
92
- interface TokenLike {
93
- type: string;
94
- text?: string;
95
- raw?: string;
96
- }
97
50
 
98
51
  const marked = new Marked({
99
52
  gfm: true,
@@ -190,97 +143,15 @@ marked.use({
190
143
  },
191
144
  });
192
145
 
193
- marked.use({
194
- extensions: [
195
- // \( ... \) inline math
196
- {
197
- name: 'inlineMath',
198
- level: 'inline',
199
- start(src: string) {
200
- return src.match(/\\\(/)?.index;
201
- },
202
- tokenizer(src: string) {
203
- const match = /^\\\(([\s\S]+?)\\\)/.exec(src);
204
- return match
205
- ? { type: 'inlineMath', raw: match[0], text: match[1] }
206
- : undefined;
207
- },
208
- renderer(token: TokenLike) {
209
- return renderMathPlaceholder(token.text ?? '', false);
210
- },
211
- },
212
- // \[ ... \] block math
213
- {
214
- name: 'blockMath',
215
- level: 'block',
216
- start(src: string) {
217
- return src.match(/^\\\[/)?.index;
218
- },
219
- tokenizer(src: string) {
220
- const match = /^\\\[([\s\S]+?)\\\]/.exec(src);
221
- return match
222
- ? { type: 'blockMath', raw: match[0], text: match[1] }
223
- : undefined;
224
- },
225
- renderer(token: TokenLike) {
226
- return renderMathPlaceholder(token.text ?? '', true);
227
- },
228
- },
229
- // $$ ... $$ display math (block-level, must be its own line)
230
- {
231
- name: 'blockMathDisplay',
232
- level: 'block',
233
- start(src: string) {
234
- return src.match(/^\$\$/)?.index;
235
- },
236
- tokenizer(src: string) {
237
- const match = /^\$\$([\s\S]+?)\$\$(?:\n|$)/.exec(src);
238
- return match
239
- ? { type: 'blockMathDisplay', raw: match[0], text: match[1] }
240
- : undefined;
241
- },
242
- renderer(token: TokenLike) {
243
- return renderMathPlaceholder(token.text ?? '', true);
244
- },
245
- },
246
- // $ ... $ inline math. The flanking rules are the whole defence: a single
247
- // `$` is the one delimiter that collides with ordinary text, and this
248
- // pipeline renders shell output, currency, SQL and awk programs all day.
249
- //
250
- // opening $ — not preceded by a word char (checked in `start`, the only
251
- // place the preceding character is visible), and not
252
- // followed by a digit, `$` or `{`. The digit guard is what
253
- // makes `$5`, `$1` and `$10` text; `{` keeps `${HOME}` out.
254
- // closing $ — not preceded by whitespace, and not followed by a WORD
255
- // char or `$`. That trailing guard is load-bearing twice
256
- // over: it rejects `PATH=$PATH:$HOME` (closer followed by
257
- // `H`) and `a$b$c` (followed by `c`), and it is the reason
258
- // the rule no longer depends on where `start` happened to
259
- // land — the tokenizer is anchored at `^` and cannot see
260
- // the preceding character, so `a$b$c` was math while
261
- // `ax$b$c` was text on the identical rule.
262
- //
263
- // `$n$ = 3` and `$x^2$ benar` still parse: a space after the closer is not
264
- // a word char.
265
- {
266
- name: 'inlineMathDollar',
267
- level: 'inline',
268
- start(src: string) {
269
- const match = /(^|[^\w$])\$(?!\$)(?!\s)/.exec(src);
270
- return match ? match.index + (match[1]?.length ?? 0) : undefined;
271
- },
272
- tokenizer(src: string) {
273
- const match = /^\$(?![\d${])((?:\\\$|[^$])*?)(?<!\s)\$(?![\w$])/.exec(src);
274
- return match
275
- ? { type: 'inlineMathDollar', raw: match[0], text: match[1] }
276
- : undefined;
277
- },
278
- renderer(token: TokenLike) {
279
- return renderMathPlaceholder(token.text ?? '', false);
280
- },
281
- },
282
- ],
283
- });
146
+ marked.use({ extensions: mathExtensions() });
147
+
148
+ // LAST, and the order is the contract: the mapper captures these renderers as
149
+ // they stand, so registering it before the overrides above would capture the
150
+ // un-wrapped `code`/`table` and every previewed document would lose its copy
151
+ // buttons and its table scroller. A math block's token is not in this table at
152
+ // all (it belongs to the extension), which is why the mapper also wraps
153
+ // `defaults.extensions.renderers` — see `blocks/renderer.ts`.
154
+ registerBlockMapper(marked);
284
155
 
285
156
  /** Escape HTML special chars in text, leaving backtick-delimited code intact
286
157
  * (inline `code` and fenced ``` blocks) so code like `a < b` is not mangled
@@ -313,23 +184,63 @@ export interface RenderMarkdownOptions {
313
184
  allowHtml?: boolean;
314
185
  }
315
186
 
187
+ /**
188
+ * The healed source both entry points parse — one spelling of the pipeline, so
189
+ * the preview and the mapped preview can never disagree about what a document
190
+ * says.
191
+ */
192
+ function heal(markdown: string, allowHtml: boolean): string {
193
+ const source = allowHtml ? markdown : escapeHtmlOutsideCode(markdown);
194
+ return remend(source, { katex: true, inlineKatex: false })
195
+ .replace(INCOMPLETE_LINK_RE, (_, text: string) => `<span class="md-incomplete-link">${text}</span>`);
196
+ }
197
+
316
198
  /**
317
199
  * Heal streaming markdown (remend) then parse to HTML via marked.
318
200
  * Synchronous and side-effect free — safe in both server loaders and the
319
201
  * browser. The output must be sanitized with DOMPurify before injection.
320
202
  */
321
203
  export function renderMarkdown(markdown: string, options: RenderMarkdownOptions = {}): string {
322
- const source = options.allowHtml ? markdown : escapeHtmlOutsideCode(markdown);
323
- const healed = remend(source, { katex: true, inlineKatex: false })
324
- .replace(INCOMPLETE_LINK_RE, (_, text: string) => `<span class="md-incomplete-link">${text}</span>`);
204
+ const healed = heal(markdown, options.allowHtml === true);
325
205
  // The math renderers need to know the prose was escaped, so they can hand
326
206
  // KaTeX the source the author wrote instead of `x &lt; y`. `marked.parse`
327
207
  // never awaits, so no nested render can observe this mid-parse.
328
- proseEscaped = !options.allowHtml;
208
+ setProseEscaped(options.allowHtml !== true);
329
209
  try {
330
210
  return (marked.parse(healed) as string).trim();
331
211
  } finally {
332
- proseEscaped = false;
212
+ setProseEscaped(false);
213
+ }
214
+ }
215
+
216
+ /** The mapped render: the same HTML, plus the block each element came from. */
217
+ export interface MappedMarkdown {
218
+ html: string;
219
+ blocks: MdBlock[];
220
+ }
221
+
222
+ /**
223
+ * Render a document AND say which source block each rendered element is.
224
+ *
225
+ * The preview needs the second half: a reviewer points at a paragraph, and a
226
+ * line number cannot express that (the render has no lines). The identity rides
227
+ * on `data-md-block`, a content-derived anchor, so it survives an edit above the
228
+ * block — see `blocks/anchor.ts`.
229
+ *
230
+ * Deliberately NOT `marked.parse(healed)`: that lexes a second time and the
231
+ * fresh tokens carry no tag. One lexer pass, then `parser(tokens)` on those
232
+ * objects. The attributes are a strict no-op for an untagged token, so the
233
+ * chat path (`renderMarkdown`) stays byte-identical.
234
+ */
235
+ export function renderMappedMarkdown(markdown: string, options: RenderMarkdownOptions = {}): MappedMarkdown {
236
+ const healed = heal(markdown, options.allowHtml === true);
237
+ const { tokens, blocks } = mapBlocks(marked, healed);
238
+ setProseEscaped(options.allowHtml !== true);
239
+ try {
240
+ const html = (marked.parser(tokens) as string).trim();
241
+ return { html, blocks };
242
+ } finally {
243
+ setProseEscaped(false);
333
244
  }
334
245
  }
335
246
 
@@ -0,0 +1,168 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The four math extensions, split out of `marked.ts` for the per-file ceiling —
8
+ * and because they are the one part of the pipeline with rules that do not fit
9
+ * in a renderer override: each is a tokenizer plus a renderer, registered
10
+ * through `marked.use({ extensions })`.
11
+ *
12
+ * ## The placeholder is never the formula
13
+ *
14
+ * KaTeX is ~522 kB of JS worth fetching only when a message contains math, so
15
+ * this module emits a span carrying the source and the display flag; `katex.ts`
16
+ * swaps it for real markup once the library lands.
17
+ *
18
+ * ## The prose-escape flag is shared state, and that is deliberate
19
+ *
20
+ * `renderMarkdown` escapes HTML in prose before marked sees it, so a tokenizer
21
+ * captures `x &lt; y` rather than `x < y` — and KaTeX would report a syntax
22
+ * error for a perfectly good formula. The flag is set for the duration of one
23
+ * synchronous `parse`, which is why `setProseEscaped` exists instead of a
24
+ * parameter threaded through four renderers. `marked.parse` never awaits, so no
25
+ * nested render can observe it mid-parse.
26
+ */
27
+
28
+ import type { TokenizerAndRendererExtension } from 'marked';
29
+
30
+ import { MATH_PENDING_CLASS } from '@/shared/lib/markdown/katex';
31
+
32
+ /**
33
+ * True while `renderMarkdown` parses prose it escaped itself, read by the math
34
+ * renderers below.
35
+ */
36
+ let proseEscaped = false;
37
+
38
+ /** Set for the duration of one parse; always paired with `false`. */
39
+ export function setProseEscaped(value: boolean): void {
40
+ proseEscaped = value;
41
+ }
42
+
43
+ interface TokenLike {
44
+ type: string;
45
+ text?: string;
46
+ raw?: string;
47
+ }
48
+
49
+ /**
50
+ * Emit a math placeholder instead of rendering KaTeX inline.
51
+ *
52
+ * Two things happen to the captured source, and both exist because this is the
53
+ * one place all four math extensions funnel through:
54
+ *
55
+ * 1. **Entities are reversed** when the prose was escaped. Only the three
56
+ * entities that escaper emits are reversed, and only when it ran — a document
57
+ * (`allowHtml`) whose own text legitimately holds `&amp;` is left alone.
58
+ * 2. **The result is percent-encoded**, the way a mermaid block carries its
59
+ * source. HTML-escaping the payload instead re-escapes what step 1 just
60
+ * restored, and the attribute round-trip hides the extra layer from both
61
+ * ends. An encoded payload holds no HTML-special character, so nothing
62
+ * between here and injection can alter it.
63
+ */
64
+ export function renderMathPlaceholder(tex: string, displayMode: boolean): string {
65
+ const restored = proseEscaped
66
+ ? tex.replace(/&(amp|lt|gt);/g, (_, name: string) => (name === 'amp' ? '&' : name === 'lt' ? '<' : '>'))
67
+ : tex;
68
+ const display = displayMode ? ' data-math-display="1"' : '';
69
+ const span = `<span class="${MATH_PENDING_CLASS}" data-math="${encodeURIComponent(restored)}"${display}></span>`;
70
+ // Block math must stay inside a block element: DOMPurify drops a bare
71
+ // `<span>` that sits at the top level of the fragment, which silently deleted
72
+ // every `$$…$$` formula. KaTeX's own output is inline-level too, so the old
73
+ // synchronous renderer relied on the same wrapper being present.
74
+ return displayMode ? `<p>${span}</p>` : span;
75
+ }
76
+
77
+ /** The four extensions, in the order `marked.ts` registers them. */
78
+ export function mathExtensions(): TokenizerAndRendererExtension[] {
79
+ return [
80
+ // \( ... \) inline math
81
+ {
82
+ name: 'inlineMath',
83
+ level: 'inline',
84
+ start(src: string) {
85
+ return src.match(/\\\(/)?.index;
86
+ },
87
+ tokenizer(src: string) {
88
+ const match = /^\\\(([\s\S]+?)\\\)/.exec(src);
89
+ return match
90
+ ? { type: 'inlineMath', raw: match[0], text: match[1] }
91
+ : undefined;
92
+ },
93
+ renderer(token: TokenLike) {
94
+ return renderMathPlaceholder(token.text ?? '', false);
95
+ },
96
+ },
97
+ // \[ ... \] block math
98
+ {
99
+ name: 'blockMath',
100
+ level: 'block',
101
+ start(src: string) {
102
+ return src.match(/^\\\[/)?.index;
103
+ },
104
+ tokenizer(src: string) {
105
+ const match = /^\\\[([\s\S]+?)\\\]/.exec(src);
106
+ return match
107
+ ? { type: 'blockMath', raw: match[0], text: match[1] }
108
+ : undefined;
109
+ },
110
+ renderer(token: TokenLike) {
111
+ return renderMathPlaceholder(token.text ?? '', true);
112
+ },
113
+ },
114
+ // $$ ... $$ display math (block-level, must be its own line)
115
+ {
116
+ name: 'blockMathDisplay',
117
+ level: 'block',
118
+ start(src: string) {
119
+ return src.match(/^\$\$/)?.index;
120
+ },
121
+ tokenizer(src: string) {
122
+ const match = /^\$\$([\s\S]+?)\$\$(?:\n|$)/.exec(src);
123
+ return match
124
+ ? { type: 'blockMathDisplay', raw: match[0], text: match[1] }
125
+ : undefined;
126
+ },
127
+ renderer(token: TokenLike) {
128
+ return renderMathPlaceholder(token.text ?? '', true);
129
+ },
130
+ },
131
+ // $ ... $ inline math. The flanking rules are the whole defence: a single
132
+ // `$` is the one delimiter that collides with ordinary text, and this
133
+ // pipeline renders shell output, currency, SQL and awk programs all day.
134
+ //
135
+ // opening $ — not preceded by a word char (checked in `start`, the only
136
+ // place the preceding character is visible), and not
137
+ // followed by a digit, `$` or `{`. The digit guard is what
138
+ // makes `$5`, `$1` and `$10` text; `{` keeps `${HOME}` out.
139
+ // closing $ — not preceded by whitespace, and not followed by a WORD
140
+ // char or `$`. That trailing guard is load-bearing twice
141
+ // over: it rejects `PATH=$PATH:$HOME` (closer followed by
142
+ // `H`) and `a$b$c` (followed by `c`), and it is the reason
143
+ // the rule no longer depends on where `start` happened to
144
+ // land — the tokenizer is anchored at `^` and cannot see
145
+ // the preceding character, so `a$b$c` was math while
146
+ // `ax$b$c` was text on the identical rule.
147
+ //
148
+ // `$n$ = 3` and `$x^2$ benar` still parse: a space after the closer is not
149
+ // a word char.
150
+ {
151
+ name: 'inlineMathDollar',
152
+ level: 'inline',
153
+ start(src: string) {
154
+ const match = /(^|[^\w$])\$(?!\$)(?!\s)/.exec(src);
155
+ return match ? match.index + (match[1]?.length ?? 0) : undefined;
156
+ },
157
+ tokenizer(src: string) {
158
+ const match = /^\$(?![\d${])((?:\\\$|[^$])*?)(?<!\s)\$(?![\w$])/.exec(src);
159
+ return match
160
+ ? { type: 'inlineMathDollar', raw: match[0], text: match[1] }
161
+ : undefined;
162
+ },
163
+ renderer(token: TokenLike) {
164
+ return renderMathPlaceholder(token.text ?? '', false);
165
+ },
166
+ },
167
+ ];
168
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * A document review as the text the agent receives.
8
+ *
9
+ * The same shape the browser panel's page context uses: the judgements become
10
+ * PROMPT TEXT in the composer draft, never a second store the agent has to be
11
+ * taught to read. Each line names the block by its line range and quotes it, so
12
+ * the agent can find the paragraph without counting, and each mark says which
13
+ * answer was chosen in words (`good`, `needs work`) rather than a glyph alone.
14
+ *
15
+ * The closing instruction is load-bearing: without it, a list of verdicts reads
16
+ * as a description of the document instead of a decision about it — and a
17
+ * `good` block is exactly the instruction "leave this alone".
18
+ */
19
+
20
+ import type { BlockVerdict, DocumentAnnotation } from '@/shared/lib/markdown/blocks/types';
21
+
22
+ /** One `scope\0repo` pair as the prompt spells it: a path a reader can act on. */
23
+ export interface ReviewPromptDocument {
24
+ /** The document's path, relative to the workspace (or nested repo). */
25
+ path: string;
26
+ annotations: readonly Pick<DocumentAnnotation, 'lineStart' | 'lineEnd' | 'verdict' | 'note' | 'quote'>[];
27
+ }
28
+
29
+ const VERDICT_LABEL: Record<BlockVerdict, string> = {
30
+ good: 'good — leave it as it is',
31
+ 'needs-work': 'needs work',
32
+ };
33
+
34
+ function rangeOf(annotation: ReviewPromptDocument['annotations'][number]): string {
35
+ return annotation.lineStart === annotation.lineEnd
36
+ ? `L${annotation.lineStart}`
37
+ : `L${annotation.lineStart}-${annotation.lineEnd}`;
38
+ }
39
+
40
+ /**
41
+ * The block this review adds to the draft; empty when nothing was judged.
42
+ *
43
+ * An empty review produces NO text rather than a header claiming zero marks: the
44
+ * button that sends it is disabled in that state, and a stray header would be
45
+ * something the user has to delete before typing.
46
+ */
47
+ export function formatReviewBlock(document: ReviewPromptDocument): string {
48
+ const marks = document.annotations.filter((annotation) => annotation.verdict === 'good' || annotation.verdict === 'needs-work');
49
+ if (marks.length === 0) return '';
50
+ const lines = marks.map((annotation) => {
51
+ const quote = annotation.quote ? ` «${annotation.quote}»` : '';
52
+ const note = annotation.note.trim();
53
+ return `- ${rangeOf(annotation)}${quote} — ${VERDICT_LABEL[annotation.verdict]}${note ? `: ${note}` : ''}`;
54
+ });
55
+ return [
56
+ `[Review dokumen] ${document.path}`,
57
+ ...lines,
58
+ 'Ini keputusan user atas blok-blok di atas: blok bertanda "needs work" perlu diperbaiki sesuai catatannya, blok bertanda "good" jangan diubah.',
59
+ ].join('\n');
60
+ }
61
+
62
+ /** A blank draft gets the block alone; otherwise one blank line separates them. */
63
+ export function appendReviewBlock(previous: string, block: string): string {
64
+ if (!block) return previous;
65
+ if (!previous) return block;
66
+ const separator = previous.endsWith('\n\n') ? '' : previous.endsWith('\n') ? '\n' : '\n\n';
67
+ return `${previous}${separator}${block}`;
68
+ }
69
+
70
+ /** DOM event a document review dispatches to append its block to the draft. */
71
+ export const REVIEW_INCLUDE_EVENT = 'omp:review-include';
72
+
73
+ /** Hand a finished review to the composer, whichever tree is listening. */
74
+ export function emitReviewBlock(block: string): void {
75
+ if (typeof window === 'undefined' || !block) return;
76
+ window.dispatchEvent(new CustomEvent<string>(REVIEW_INCLUDE_EVENT, { detail: block }));
77
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Which stored marks still describe a block on screen, and which do not.
8
+ *
9
+ * A mark names its block by content (`type:hash#n`), so editing a block detaches
10
+ * its mark: the stored quote no longer matches anything rendered. That is a
11
+ * real state the reader must SEE — the note is still what they wrote, and the
12
+ * only copy of it — so it is reported as `outdated` with its quote intact and a
13
+ * way to remove it, never deleted behind their back.
14
+ *
15
+ * A mark that matches nothing because the DOCUMENT was not rendered at all (a
16
+ * refused read, an unsupported kind) is a different fact and is not this
17
+ * module's business: the caller only reconciles against a real block list.
18
+ */
19
+
20
+ import type { DocumentAnnotation, MdBlock } from '@/shared/lib/markdown/blocks/types';
21
+
22
+ export interface ReconcileResult<T extends DocumentAnnotation> {
23
+ /** Marks whose block is on screen, keyed by block key. */
24
+ attached: Map<string, T>;
25
+ /** Marks whose block is no longer rendered, newest first. */
26
+ outdated: T[];
27
+ }
28
+
29
+ /** Attach stored marks to the blocks the render produced. */
30
+ export function reconcileAnnotations<T extends DocumentAnnotation>(
31
+ blocks: readonly MdBlock[],
32
+ annotations: readonly T[],
33
+ ): ReconcileResult<T> {
34
+ const byKey = new Map(blocks.map((block) => [block.key, block]));
35
+ const attached = new Map<string, T>();
36
+ const outdated: T[] = [];
37
+
38
+ for (const annotation of annotations) {
39
+ if (byKey.has(annotation.blockKey)) attached.set(annotation.blockKey, annotation);
40
+ else outdated.push(annotation);
41
+ }
42
+ outdated.sort((left, right) => right.updatedAt - left.updatedAt);
43
+ return { attached, outdated };
44
+ }
45
+
46
+ /** The verdict counts the summary chip prints. */
47
+ export function tallyAnnotations<T extends DocumentAnnotation>(attached: ReadonlyMap<string, T>): { good: number; needsWork: number } {
48
+ let good = 0;
49
+ let needsWork = 0;
50
+ for (const annotation of attached.values()) {
51
+ if (annotation.verdict === 'good') good += 1;
52
+ else needsWork += 1;
53
+ }
54
+ return { good, needsWork };
55
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Reconciliation and the prompt: what a stored judgement becomes once the
8
+ * document has moved.
9
+ *
10
+ * The behaviours pinned here are the ones a user notices when they are wrong:
11
+ * a mark that follows its block, a mark that says it is outdated instead of
12
+ * disappearing, and composer text that names a block precisely enough for the
13
+ * agent to find it.
14
+ */
15
+
16
+ import { describe, expect, test } from 'bun:test';
17
+
18
+ import { reconcileAnnotations, tallyAnnotations } from '@/shared/lib/markdown/review/reconcile';
19
+ import { appendReviewBlock, formatReviewBlock } from '@/shared/lib/markdown/review/prompt';
20
+ import type { DocumentAnnotation, MdBlock } from '@/shared/lib/markdown/blocks/types';
21
+
22
+ const block = (key: string, quote = key): MdBlock => ({ key, type: 'paragraph', lineStart: 1, lineEnd: 1, quote });
23
+
24
+ const annotation = (overrides: Partial<DocumentAnnotation> & { id: string; blockKey: string }): DocumentAnnotation => ({
25
+ scope: 'root\u0000.',
26
+ path: 'NOTES.md',
27
+ lineStart: 1,
28
+ lineEnd: 1,
29
+ verdict: 'good',
30
+ note: '',
31
+ quote: 'x',
32
+ createdAt: 1,
33
+ updatedAt: 1,
34
+ ...overrides,
35
+ });
36
+
37
+ describe('reconcileAnnotations', () => {
38
+ test('a mark follows its block, and a mark whose block is gone is OUTDATED', () => {
39
+ const blocks = [block('a'), block('b')];
40
+ const result = reconcileAnnotations(blocks, [
41
+ annotation({ id: '1', blockKey: 'b' }),
42
+ annotation({ id: '2', blockKey: 'gone', updatedAt: 5 }),
43
+ ]);
44
+ expect([...result.attached.keys()]).toEqual(['b']);
45
+ expect(result.outdated.map((row) => row.id)).toEqual(['2']);
46
+ // The note is still there: it is the user's only copy.
47
+ expect(result.outdated[0].quote).toBe('x');
48
+ });
49
+
50
+ test('outdated marks are newest first, so the last edit is at the top', () => {
51
+ const result = reconcileAnnotations([], [
52
+ annotation({ id: 'old', blockKey: 'x', updatedAt: 1 }),
53
+ annotation({ id: 'new', blockKey: 'y', updatedAt: 9 }),
54
+ ]);
55
+ expect(result.outdated.map((row) => row.id)).toEqual(['new', 'old']);
56
+ });
57
+
58
+ test('the tally counts what is attached, not what is stored', () => {
59
+ const result = reconcileAnnotations([block('a'), block('b')], [
60
+ annotation({ id: '1', blockKey: 'a', verdict: 'good' }),
61
+ annotation({ id: '2', blockKey: 'b', verdict: 'needs-work' }),
62
+ annotation({ id: '3', blockKey: 'outdated', verdict: 'needs-work' }),
63
+ ]);
64
+ expect(tallyAnnotations(result.attached)).toEqual({ good: 1, needsWork: 1 });
65
+ });
66
+ });
67
+
68
+ describe('formatReviewBlock', () => {
69
+ test('names each block by range and quote, and says what the verdict means', () => {
70
+ const text = formatReviewBlock({
71
+ path: 'docs/NOTES.md',
72
+ annotations: [
73
+ annotation({ id: '1', blockKey: 'a', lineStart: 3, lineEnd: 5, verdict: 'needs-work', note: 'basi', quote: '## Notes' }),
74
+ annotation({ id: '2', blockKey: 'b', lineStart: 9, lineEnd: 9, verdict: 'good', quote: 'Run gates' }),
75
+ ],
76
+ });
77
+ expect(text).toContain('[Review dokumen] docs/NOTES.md');
78
+ expect(text).toContain('- L3-5 «## Notes» — needs work: basi');
79
+ expect(text).toContain('- L9 «Run gates» — good');
80
+ // The instruction is what turns a list of verdicts into a decision.
81
+ expect(text).toContain('jangan diubah');
82
+ });
83
+
84
+ test('an empty review produces no text at all', () => {
85
+ expect(formatReviewBlock({ path: 'a.md', annotations: [] })).toBe('');
86
+ });
87
+
88
+ test('a blank draft takes the block; an existing one is separated by a blank line', () => {
89
+ expect(appendReviewBlock('', 'BLOCK')).toBe('BLOCK');
90
+ expect(appendReviewBlock('draft', 'BLOCK')).toBe('draft\n\nBLOCK');
91
+ // One blank line either way: a single trailing newline still needs a separator.
92
+ expect(appendReviewBlock('draft\n', 'BLOCK')).toBe('draft\n\nBLOCK');
93
+ expect(appendReviewBlock('draft\n\n', 'BLOCK')).toBe('draft\n\nBLOCK');
94
+ expect(appendReviewBlock('draft', '')).toBe('draft');
95
+ });
96
+ });