@kabmo-labs/kabmo 0.17.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 (269) hide show
  1. package/dist/client/index.html +1 -1
  2. package/dist/client/index.js +538 -457
  3. package/dist/client/static/js/index-hp75vsv9.css +1 -0
  4. package/dist/client/static/js/{index-rerjq5th.js → index-tc09dtca.js} +823 -814
  5. package/package.json +1 -1
  6. package/src/client/components/common/MarkdownRenderer.tsx +136 -89
  7. package/src/client/components/common/anchored-menu/index.test.tsx +141 -0
  8. package/src/client/components/common/anchored-menu/index.tsx +143 -0
  9. package/src/client/components/common/markdown-review/ReviewPopover.tsx +144 -0
  10. package/src/client/components/common/markdown-review/ReviewSummary.tsx +108 -0
  11. package/src/client/components/common/markdown-review/index.tsx +129 -0
  12. package/src/client/components/common/server-control/Panel.tsx +33 -23
  13. package/src/client/components/common/server-control/index.test.tsx +11 -5
  14. package/src/client/components/common/server-control/index.tsx +10 -8
  15. package/src/client/components/layout/session-sidebar/CategoryHeader.tsx +42 -21
  16. package/src/client/components/layout/session-sidebar/CategoryHeader.worktree.test.tsx +36 -17
  17. package/src/client/components/layout/session-sidebar/CategoryItem.tsx +8 -7
  18. package/src/client/components/layout/session-sidebar/about-modal/NotesDialog.tsx +94 -0
  19. package/src/client/components/layout/session-sidebar/about-modal/VersionLine.test.tsx +68 -0
  20. package/src/client/components/layout/session-sidebar/about-modal/VersionLine.tsx +36 -0
  21. package/src/client/components/layout/session-sidebar/about-modal/index.tsx +57 -34
  22. package/src/client/components/layout/session-sidebar/modern/WorkspaceHeader.tsx +28 -10
  23. package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +7 -6
  24. package/src/client/components/layout/session-sidebar/modern/workspace-header.test.tsx +1 -1
  25. package/src/client/components/layout/update-popup/ReleaseNotes.tsx +8 -3
  26. package/src/client/components/layout/update-popup/index.tsx +5 -1
  27. package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +13 -15
  28. package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +23 -1
  29. package/src/client/components/mobile/mobile-right-sidebar/MissingFileBanner.tsx +30 -0
  30. package/src/client/components/mobile/mobile-right-sidebar/PreviewScroller.tsx +18 -2
  31. package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +22 -67
  32. package/src/client/components/mobile/mobile-session-sidebar/ItemMenu.tsx +110 -0
  33. package/src/client/components/settings/SettingsSidebarList.tsx +20 -0
  34. package/src/client/components/settings/categories/AgentSettings.tsx +23 -50
  35. package/src/client/components/settings/categories/CommandSettings.tsx +2 -2
  36. package/src/client/components/settings/categories/McpSettings.tsx +4 -21
  37. package/src/client/components/settings/categories/OmpSettings.tsx +147 -194
  38. package/src/client/components/settings/categories/SkillSettings.tsx +2 -2
  39. package/src/client/components/settings/categories/TokenUsageSettings.tsx +211 -229
  40. package/src/client/components/settings/categories/agent-settings/DetailPane.tsx +229 -237
  41. package/src/client/components/settings/categories/agent-settings/ModelPicker.tsx +71 -0
  42. package/src/client/components/settings/categories/agent-settings/SidebarList.tsx +9 -2
  43. package/src/client/components/settings/categories/agent-settings/levels.ts +38 -0
  44. package/src/client/components/settings/categories/behavior-settings/Editor.tsx +27 -7
  45. package/src/client/components/settings/categories/chat-settings/ToolRenderSection.tsx +23 -5
  46. package/src/client/components/settings/categories/command-settings/DetailPane.tsx +75 -33
  47. package/src/client/components/settings/categories/command-settings/SidebarList.tsx +7 -0
  48. package/src/client/components/settings/categories/mcp-settings/DetailPane.tsx +91 -76
  49. package/src/client/components/settings/categories/mcp-settings/EnvVarsSection.tsx +83 -0
  50. package/src/client/components/settings/categories/mcp-settings/ImportModal.tsx +26 -13
  51. package/src/client/components/settings/categories/mcp-settings/SidebarList.tsx +20 -0
  52. package/src/client/components/settings/categories/omp-settings/ConfigTabs.tsx +97 -36
  53. package/src/client/components/settings/categories/omp-settings/EngineHeader.tsx +151 -0
  54. package/src/client/components/settings/categories/omp-settings/ExtensionsPanel.tsx +68 -32
  55. package/src/client/components/settings/categories/omp-settings/OrderedChips.tsx +119 -0
  56. package/src/client/components/settings/categories/omp-settings/PresetBar.tsx +14 -7
  57. package/src/client/components/settings/categories/omp-settings/RoleEditor.tsx +15 -6
  58. package/src/client/components/settings/categories/omp-settings/RowEditor.tsx +165 -226
  59. package/src/client/components/settings/categories/omp-settings/types.ts +28 -0
  60. package/src/client/components/settings/categories/skill-settings/DetailHeader.tsx +35 -7
  61. package/src/client/components/settings/categories/skill-settings/DetailPane.tsx +34 -14
  62. package/src/client/components/settings/categories/skill-settings/SidebarList.tsx +21 -0
  63. package/src/client/components/settings/categories/skill-settings/pane.test.tsx +15 -6
  64. package/src/client/components/settings/categories/token-usage-settings/Breakdown.tsx +242 -102
  65. package/src/client/components/settings/categories/token-usage-settings/Chart.tsx +130 -56
  66. package/src/client/components/settings/categories/token-usage-settings/Controls.tsx +135 -0
  67. package/src/client/components/settings/categories/token-usage-settings/MetricsGrid.tsx +55 -33
  68. package/src/client/components/settings/categories/token-usage-settings/Provenance.tsx +59 -0
  69. package/src/client/components/settings/categories/token-usage-settings/panel.test.tsx +239 -0
  70. package/src/client/components/workspace/browser-panel/Screencast.test.ts +1 -0
  71. package/src/client/components/workspace/browser-panel/Screencast.tsx +17 -22
  72. package/src/client/components/workspace/browser-panel/TabStrip.test.tsx +158 -0
  73. package/src/client/components/workspace/browser-panel/TabStrip.tsx +90 -0
  74. package/src/client/components/workspace/browser-panel/index.tsx +50 -4
  75. package/src/client/components/workspace/browser-toolbar/index.tsx +41 -1
  76. package/src/client/components/workspace/chat-timeline/ThinkingSection.tsx +3 -1
  77. package/src/client/components/workspace/chat-timeline/ToolCallingSection.tsx +2 -0
  78. package/src/client/components/workspace/chat-timeline/block-chrome.test.tsx +13 -2
  79. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.test.ts +88 -0
  80. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.tsx +24 -1
  81. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.test.ts +34 -0
  82. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.tsx +5 -1
  83. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ArtifactChip.tsx +7 -6
  84. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.test.tsx +237 -0
  85. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.tsx +191 -0
  86. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/styles.test.tsx +1 -1
  87. package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +21 -7
  88. package/src/client/components/workspace/diff-panel/changes.test.ts +51 -0
  89. package/src/client/components/workspace/diff-panel/changes.ts +28 -12
  90. package/src/client/components/workspace/diff-panel/merge.test.ts +28 -0
  91. package/src/client/components/workspace/diff-panel/merge.ts +21 -1
  92. package/src/client/components/workspace/editor/Body.tsx +7 -0
  93. package/src/client/components/workspace/editor/FileDocument.tsx +15 -21
  94. package/src/client/components/workspace/editor/FilePane.tsx +12 -0
  95. package/src/client/components/workspace/editor/PreviewPane.tsx +70 -0
  96. package/src/client/components/workspace/editor/Toolbar.tsx +17 -1
  97. package/src/client/components/workspace/editor/index.tsx +25 -22
  98. package/src/client/components/workspace/editor/toolbar-clusters.test.ts +14 -0
  99. package/src/client/components/workspace/editor/toolbar-clusters.tsx +12 -86
  100. package/src/client/components/workspace/editor/toolbar-toggle.tsx +53 -0
  101. package/src/client/components/workspace/editor/toolbar-write-clusters.tsx +112 -0
  102. package/src/client/components/workspace/git-panel/commit-modal.test.ts +5 -3
  103. package/src/client/components/workspace/user-browser-panel/AddressBar.tsx +5 -0
  104. package/src/client/components/workspace/user-browser-panel/AnnotateBar.test.tsx +201 -0
  105. package/src/client/components/workspace/user-browser-panel/AnnotateBar.tsx +112 -0
  106. package/src/client/components/workspace/user-browser-panel/AnnotateLayer.test.tsx +225 -0
  107. package/src/client/components/workspace/user-browser-panel/AnnotateLayer.tsx +217 -0
  108. package/src/client/components/workspace/user-browser-panel/AnnotateTarget.tsx +99 -0
  109. package/src/client/components/workspace/user-browser-panel/Frame.tsx +30 -1
  110. package/src/client/components/workspace/user-browser-panel/index.tsx +81 -2
  111. package/src/client/data/agent-data/builtin.ts +0 -10
  112. package/src/client/data/agent-data/orchestrator.ts +0 -45
  113. package/src/client/data/agent-data/specialist.ts +0 -100
  114. package/src/client/data/mock/token-usage.ts +170 -120
  115. package/src/client/data/settings/omp-schema.json +33 -1
  116. package/src/client/hooks/browser/use-annotate.test.ts +143 -0
  117. package/src/client/hooks/browser/use-annotate.ts +98 -0
  118. package/src/client/hooks/browser/use-close-tab.test.ts +148 -0
  119. package/src/client/hooks/browser/use-close-tab.ts +67 -0
  120. package/src/client/hooks/browser/use-screencast.ts +13 -1
  121. package/src/client/hooks/chat/composer/browser-context.test.ts +117 -0
  122. package/src/client/hooks/chat/composer/browser-context.ts +40 -16
  123. package/src/client/hooks/chat/composer/items.test.ts +1 -1
  124. package/src/client/hooks/chat/composer/review-context.ts +38 -0
  125. package/src/client/hooks/chat/composer/use-draft.test.ts +132 -0
  126. package/src/client/hooks/chat/composer/use-draft.ts +31 -2
  127. package/src/client/hooks/chat/timeline/row-window.test.ts +15 -2
  128. package/src/client/hooks/editor/use-document-annotations.ts +124 -0
  129. package/src/client/hooks/editor/use-editor-panel-ui.ts +52 -0
  130. package/src/client/hooks/editor/use-file-review-mode.ts +35 -0
  131. package/src/client/hooks/editor/use-preview-review.ts +148 -0
  132. package/src/client/hooks/settings/agents.ts +149 -0
  133. package/src/client/hooks/settings/crud-list.test.ts +28 -6
  134. package/src/client/hooks/settings/crud-list.ts +96 -57
  135. package/src/client/hooks/settings/omp-config.ts +164 -0
  136. package/src/client/hooks/settings/useTokenUsage.ts +125 -0
  137. package/src/client/hooks/settings/write-feedback.test.ts +104 -0
  138. package/src/client/hooks/settings/write-feedback.ts +63 -0
  139. package/src/client/hooks/ui/markdown-hydration.ts +125 -0
  140. package/src/client/hooks/ui/updates/index.test.ts +100 -1
  141. package/src/client/hooks/ui/updates/index.ts +50 -0
  142. package/src/client/hooks/ui/updates/notes.test.ts +138 -0
  143. package/src/client/hooks/ui/updates/notes.ts +57 -0
  144. package/src/client/lib/browser/element-at.test.ts +229 -0
  145. package/src/client/lib/browser/element-at.ts +139 -0
  146. package/src/client/lib/review/client.ts +73 -0
  147. package/src/client/lib/router/search-params.ts +42 -20
  148. package/src/client/tailwind.css +65 -0
  149. package/src/server/lib/btw/lifecycle.server.ts +2 -2
  150. package/src/server/lib/db/schema.ts +7 -0
  151. package/src/server/lib/db/seed-terminal.test.ts +4 -0
  152. package/src/server/lib/omp/config/agent-definitions.test.ts +273 -0
  153. package/src/server/lib/omp/config/agents.ts +231 -61
  154. package/src/server/lib/omp/config/definitions.test.ts +9 -105
  155. package/src/server/lib/omp/config/extensions.ts +16 -7
  156. package/src/server/lib/omp/config/omp-config-files.test.ts +27 -0
  157. package/src/server/lib/omp/rpc/constants.test.ts +11 -0
  158. package/src/server/lib/omp/rpc/constants.ts +15 -0
  159. package/src/server/lib/omp/rpc/process.ts +11 -14
  160. package/src/server/lib/omp/rpc/session-commands.ts +6 -3
  161. package/src/server/lib/omp/rpc/utility-agent-dir.test.ts +134 -0
  162. package/src/server/lib/omp/rpc/utility-agent-dir.ts +138 -0
  163. package/src/server/lib/omp/rpc/utility.ts +12 -0
  164. package/src/server/lib/review/schema.server.ts +45 -0
  165. package/src/server/lib/review/store.server.ts +138 -0
  166. package/src/server/lib/terminal/attach-replay.test.ts +132 -0
  167. package/src/server/lib/terminal/output.test.ts +166 -0
  168. package/src/server/lib/terminal/output.ts +104 -0
  169. package/src/server/lib/terminal/runtime.server.ts +5 -0
  170. package/src/server/lib/terminal/send-result.test.ts +33 -0
  171. package/src/server/lib/terminal/send-result.ts +32 -0
  172. package/src/server/lib/terminal/session.server.ts +45 -11
  173. package/src/server/lib/terminal/store-liveness.test.ts +4 -0
  174. package/src/server/lib/updates/active-notes.test.ts +149 -0
  175. package/src/server/lib/updates/active-notes.ts +75 -0
  176. package/src/server/lib/updates/changelog-file.ts +42 -11
  177. package/src/server/lib/updates/changelog.test.ts +15 -1
  178. package/src/server/lib/updates/omp.ts +42 -15
  179. package/src/server/lib/updates/release-url.ts +27 -1
  180. package/src/server/lib/usage/index-parse.test.ts +200 -0
  181. package/src/server/lib/usage/index-parse.ts +178 -0
  182. package/src/server/lib/usage/index-query.server.ts +248 -0
  183. package/src/server/lib/usage/index-scan.server.ts +343 -0
  184. package/src/server/lib/usage/index-schema.server.ts +82 -0
  185. package/src/server/lib/usage/index.test.ts +234 -0
  186. package/src/server/lib/usage/providers.server.ts +4 -53
  187. package/src/server/routes/browser/index.ts +3 -0
  188. package/src/server/routes/browser/stream.ts +31 -2
  189. package/src/server/routes/browser/tabs.test.ts +94 -0
  190. package/src/server/routes/browser/tabs.ts +85 -0
  191. package/src/server/routes/index.ts +2 -0
  192. package/src/server/routes/review/bindings.ts +11 -0
  193. package/src/server/routes/review/index.ts +127 -0
  194. package/src/server/routes/review/routes.test.ts +191 -0
  195. package/src/server/routes/settings/agents.test.ts +180 -0
  196. package/src/server/routes/settings/agents.ts +95 -102
  197. package/src/server/routes/settings/mcp.test.ts +135 -0
  198. package/src/server/routes/settings/mcp.ts +14 -6
  199. package/src/server/routes/settings/settings-store-routes.test.ts +6 -113
  200. package/src/server/routes/telemetry/tokens.test.ts +145 -0
  201. package/src/server/routes/telemetry/tokens.ts +70 -149
  202. package/src/server/routes/terminal/ws.ts +6 -5
  203. package/src/server/routes/updates/index.ts +2 -0
  204. package/src/server/routes/updates/notes.ts +34 -0
  205. package/src/server/routes/updates/routes.test.ts +49 -0
  206. package/src/shared/lib/browser/annotate.test.ts +283 -0
  207. package/src/shared/lib/browser/annotate.ts +231 -0
  208. package/src/shared/lib/browser/browser-viewer.test.ts +26 -4
  209. package/src/shared/lib/browser/frame-gate.test.ts +91 -0
  210. package/src/shared/lib/browser/frame-gate.ts +69 -0
  211. package/src/shared/lib/browser/frame-params.test.ts +43 -0
  212. package/src/shared/lib/browser/frame-params.ts +60 -0
  213. package/src/shared/lib/browser/frame-pipeline.test.ts +132 -0
  214. package/src/shared/lib/browser/frame-pipeline.ts +80 -0
  215. package/src/shared/lib/browser/page-context.ts +60 -4
  216. package/src/shared/lib/browser/snapshot.test.ts +115 -0
  217. package/src/shared/lib/browser/snapshot.ts +112 -0
  218. package/src/shared/lib/browser/tab-close.test.ts +74 -0
  219. package/src/shared/lib/browser/tab-close.ts +77 -0
  220. package/src/shared/lib/browser/viewer.ts +45 -26
  221. package/src/shared/lib/chat/timeline/block-chrome.ts +19 -0
  222. package/src/shared/lib/chat/timeline/tool-render-mode.ts +17 -4
  223. package/src/shared/lib/chat/tool/artifact.ts +55 -20
  224. package/src/shared/lib/chat/tool/bash-facts.ts +96 -0
  225. package/src/shared/lib/chat/tool/minimized.test.ts +98 -0
  226. package/src/shared/lib/chat/tool/minimized.ts +64 -0
  227. package/src/shared/lib/chat/tool/summary.test.ts +28 -8
  228. package/src/shared/lib/chat/tool/summary.ts +3 -32
  229. package/src/shared/lib/markdown/blocks/anchor.ts +63 -0
  230. package/src/shared/lib/markdown/blocks/blocks.test.ts +141 -0
  231. package/src/shared/lib/markdown/blocks/renderer.ts +120 -0
  232. package/src/shared/lib/markdown/blocks/scan.ts +100 -0
  233. package/src/shared/lib/markdown/blocks/types.ts +62 -0
  234. package/src/shared/lib/markdown/marked.ts +58 -147
  235. package/src/shared/lib/markdown/math-extensions.ts +168 -0
  236. package/src/shared/lib/markdown/review/prompt.ts +77 -0
  237. package/src/shared/lib/markdown/review/reconcile.ts +55 -0
  238. package/src/shared/lib/markdown/review/review.test.ts +96 -0
  239. package/src/shared/lib/markdown/xml-sections.test.ts +140 -0
  240. package/src/shared/lib/markdown/xml-sections.ts +139 -0
  241. package/src/shared/lib/models/provider/label.ts +57 -0
  242. package/src/shared/lib/omp/config/agent-file.test.ts +54 -0
  243. package/src/shared/lib/omp/config/agent-file.ts +43 -0
  244. package/src/shared/lib/omp/config/row-value.test.ts +95 -0
  245. package/src/shared/lib/omp/config/row-value.ts +97 -0
  246. package/src/shared/lib/omp/config/schema.test.ts +31 -0
  247. package/src/shared/lib/omp/config/schema.ts +6 -0
  248. package/src/shared/lib/omp/rpc/frame.test.ts +48 -47
  249. package/src/shared/lib/omp/rpc/frame.ts +49 -40
  250. package/src/shared/lib/ui/menu-anchor.test.ts +69 -0
  251. package/src/shared/lib/ui/menu-anchor.ts +89 -0
  252. package/src/shared/lib/usage/breakdown.test.ts +197 -0
  253. package/src/shared/lib/usage/breakdown.ts +241 -0
  254. package/src/shared/lib/usage/range.test.ts +204 -0
  255. package/src/shared/lib/usage/range.ts +240 -0
  256. package/src/shared/lib/usage/view.test.ts +85 -0
  257. package/src/shared/lib/usage/view.ts +74 -0
  258. package/src/shared/types/browser.ts +78 -0
  259. package/src/shared/types/index.ts +1 -0
  260. package/src/shared/types/settings/agent.ts +28 -6
  261. package/src/shared/types/settings/state.ts +4 -3
  262. package/src/shared/types/settings/write.ts +6 -0
  263. package/src/shared/types/tokenUsage.ts +128 -21
  264. package/src/shared/types/updates.ts +20 -0
  265. package/dist/client/static/js/index-3ve31rzx.css +0 -1
  266. package/src/server/lib/omp/session/usage/aggregate-shaping.test.ts +0 -149
  267. package/src/server/lib/omp/session/usage/aggregate.test.ts +0 -243
  268. package/src/server/lib/omp/session/usage/aggregate.ts +0 -330
  269. package/src/server/lib/omp/session/usage/shaping.ts +0 -130
@@ -0,0 +1,143 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Annotating the user-browser panel.
8
+ *
9
+ * The failure this hook has to prevent is a mark outliving the page it was drawn
10
+ * on. An iframe's internal navigation is invisible from this origin — the host
11
+ * gets `onLoad` and nothing else — so a reload or a typed address is the only
12
+ * signal that the document under the marks changed. Sending that stale batch
13
+ * would point the agent at percentages of a page the user has left.
14
+ */
15
+
16
+ import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
17
+ import { Window } from 'happy-dom';
18
+ import { h, render } from 'preact';
19
+ import { act } from 'preact/test-utils';
20
+ import { useState } from 'preact/hooks';
21
+ import { useAnnotate, type UseAnnotateResult } from '@/client/hooks/browser/use-annotate';
22
+ import type { AnnotateShape } from '@/shared/types';
23
+
24
+ const DOM_GLOBALS = ['window', 'document', 'navigator', 'Node', 'Element', 'HTMLElement', 'Event'] as const;
25
+ const native: Partial<Record<(typeof DOM_GLOBALS)[number], unknown>> = {};
26
+
27
+ beforeAll(() => {
28
+ const win = new Window({ url: 'http://localhost' });
29
+ const target = globalThis as unknown as Record<string, unknown>;
30
+ for (const key of DOM_GLOBALS) {
31
+ native[key] = target[key];
32
+ target[key] = (win as unknown as Record<string, unknown>)[key];
33
+ }
34
+ });
35
+
36
+ afterAll(() => {
37
+ const target = globalThis as unknown as Record<string, unknown>;
38
+ for (const key of DOM_GLOBALS) {
39
+ if (native[key] === undefined) delete target[key];
40
+ else target[key] = native[key];
41
+ }
42
+ });
43
+
44
+ function mark(id: string, note = ''): AnnotateShape {
45
+ return { id, tool: 'box', start: { x: 0.1, y: 0.1 }, end: { x: 0.3, y: 0.3 }, label: '', note };
46
+ }
47
+
48
+ let container: HTMLElement | undefined;
49
+ let latest: UseAnnotateResult | undefined;
50
+ /** Drives the hook's page key, the way the panel's `frameKey` does. */
51
+ let navigate: (key: number) => void;
52
+
53
+ const api = (): UseAnnotateResult => {
54
+ if (!latest) throw new Error('hook not rendered');
55
+ return latest;
56
+ };
57
+
58
+ /** Mount the hook with a page key the test can change. */
59
+ function mount(): void {
60
+ const Probe = () => {
61
+ const [frameKey, setFrameKey] = useState(0);
62
+ navigate = setFrameKey;
63
+ latest = useAnnotate(frameKey);
64
+ return h('div', null);
65
+ };
66
+ act(() => {
67
+ render(h(Probe, null), container!);
68
+ });
69
+ }
70
+
71
+ beforeEach(() => {
72
+ container?.remove();
73
+ container = document.body.appendChild(document.createElement('div'));
74
+ latest = undefined;
75
+ });
76
+
77
+ describe('useAnnotate', () => {
78
+ test('starts closed, with the box tool and no marks', () => {
79
+ mount();
80
+ expect(api().active).toBe(false);
81
+ expect(api().tool).toBe('box');
82
+ expect(api().shapes).toEqual([]);
83
+ });
84
+
85
+ test('toggle opens the mode and close shuts it, dropping the selection', () => {
86
+ mount();
87
+ act(() => api().toggle());
88
+ expect(api().active).toBe(true);
89
+ act(() => api().add(mark('a')));
90
+ expect(api().selectedId).toBe('a');
91
+ act(() => api().close());
92
+ expect(api().active).toBe(false);
93
+ expect(api().selectedId).toBeNull();
94
+ // Closing the mode is not the same as discarding the marks.
95
+ expect(api().shapes).toHaveLength(1);
96
+ });
97
+
98
+ test('a finished mark is committed and becomes the one being described', () => {
99
+ mount();
100
+ act(() => api().add(mark('a')));
101
+ act(() => api().add(mark('b')));
102
+ expect(api().shapes.map((shape) => shape.id)).toEqual(['a', 'b']);
103
+ expect(api().selectedId).toBe('b');
104
+ });
105
+
106
+ test('a note is stored on its own mark only', () => {
107
+ mount();
108
+ act(() => api().add(mark('a')));
109
+ act(() => api().add(mark('b')));
110
+ act(() => api().setNote('a', 'tombol mati'));
111
+ expect(api().shapes.map((shape) => shape.note)).toEqual(['tombol mati', '']);
112
+ });
113
+
114
+ test('removing the selected mark clears the selection; removing another leaves it', () => {
115
+ mount();
116
+ act(() => api().add(mark('a')));
117
+ act(() => api().add(mark('b')));
118
+ act(() => api().remove('a'));
119
+ expect(api().selectedId).toBe('b');
120
+ act(() => api().remove('b'));
121
+ expect(api().selectedId).toBeNull();
122
+ expect(api().shapes).toEqual([]);
123
+ });
124
+
125
+ test('reset clears the marks but leaves the mode as it was', () => {
126
+ mount();
127
+ act(() => api().toggle());
128
+ act(() => api().add(mark('a')));
129
+ act(() => api().reset());
130
+ expect(api().shapes).toEqual([]);
131
+ expect(api().active).toBe(true);
132
+ });
133
+
134
+ test('a new page drops every mark drawn on the old one', () => {
135
+ mount();
136
+ act(() => api().add(mark('a')));
137
+ act(() => api().add(mark('b')));
138
+ expect(api().shapes).toHaveLength(2);
139
+ act(() => navigate(1));
140
+ expect(api().shapes).toEqual([]);
141
+ expect(api().selectedId).toBeNull();
142
+ });
143
+ });
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Annotating the user-browser panel.
8
+ *
9
+ * The marks belong to ONE page view. An iframe's internal navigation is not
10
+ * observable from this origin (`onLoad` is all the host gets), so a mark made
11
+ * before a navigation would otherwise point at a different document than the
12
+ * one it was drawn on. `frameKey` therefore clears the marks: a reload or a
13
+ * typed address is a new page, and keeping them would send the agent
14
+ * coordinates for a page the user is no longer looking at.
15
+ *
16
+ * The mark being DRAGGED is not state here — it lives in the overlay that draws
17
+ * it, and arrives as one finished shape through `add`. A pointermove-driven
18
+ * value in this hook would re-render the panel (iframe included) on every move.
19
+ */
20
+
21
+ import { useCallback, useEffect, useState } from 'preact/hooks';
22
+ import type { AnnotateShape, AnnotateTool } from '@/shared/types';
23
+
24
+ export interface UseAnnotateResult {
25
+ active: boolean;
26
+ tool: AnnotateTool;
27
+ shapes: AnnotateShape[];
28
+ /** The mark whose note field is open, if any. */
29
+ selectedId: string | null;
30
+ setTool: (tool: AnnotateTool) => void;
31
+ toggle: () => void;
32
+ close: () => void;
33
+ /** Commit one finished mark and open its note field. */
34
+ add: (shape: AnnotateShape) => void;
35
+ setNote: (id: string, note: string) => void;
36
+ /** Name the region a mark covers ("Ringkasan pesanan"); the user owns this fact. */
37
+ setLabel: (id: string, label: string) => void;
38
+ select: (id: string | null) => void;
39
+ remove: (id: string) => void;
40
+ reset: () => void;
41
+ }
42
+
43
+ let shapeSeq = 0;
44
+
45
+ /** Ids are unique per mark; the overlay stamps one when the drag starts. */
46
+ export function nextShapeId(): string {
47
+ shapeSeq += 1;
48
+ return `annot-${Date.now().toString(36)}-${shapeSeq}`;
49
+ }
50
+
51
+ export function useAnnotate(frameKey: number): UseAnnotateResult {
52
+ const [active, setActive] = useState(false);
53
+ const [tool, setTool] = useState<AnnotateTool>('box');
54
+ const [shapes, setShapes] = useState<AnnotateShape[]>([]);
55
+ const [selectedId, setSelectedId] = useState<string | null>(null);
56
+
57
+ // A new page invalidates every mark on the old one.
58
+ useEffect(() => {
59
+ setShapes([]);
60
+ setSelectedId(null);
61
+ }, [frameKey]);
62
+
63
+ const toggle = useCallback(() => {
64
+ setActive((prev) => !prev);
65
+ setSelectedId(null);
66
+ }, []);
67
+
68
+ const close = useCallback(() => {
69
+ setSelectedId(null);
70
+ setActive(false);
71
+ }, []);
72
+
73
+ const add = useCallback((shape: AnnotateShape) => {
74
+ setShapes((prev) => [...prev, shape]);
75
+ // A finished mark is the one the user is about to describe.
76
+ setSelectedId(shape.id);
77
+ }, []);
78
+
79
+ const setNote = useCallback((id: string, note: string) => {
80
+ setShapes((prev) => prev.map((shape) => (shape.id === id ? { ...shape, note } : shape)));
81
+ }, []);
82
+
83
+ const setLabel = useCallback((id: string, label: string) => {
84
+ setShapes((prev) => prev.map((shape) => (shape.id === id ? { ...shape, label } : shape)));
85
+ }, []);
86
+
87
+ const remove = useCallback((id: string) => {
88
+ setShapes((prev) => prev.filter((shape) => shape.id !== id));
89
+ setSelectedId((current) => (current === id ? null : current));
90
+ }, []);
91
+
92
+ const reset = useCallback(() => {
93
+ setShapes([]);
94
+ setSelectedId(null);
95
+ }, []);
96
+
97
+ return { active, tool, shapes, selectedId, setTool, toggle, close, add, setNote, setLabel, select: setSelectedId, remove, reset };
98
+ }
@@ -0,0 +1,148 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The client half of "close this tab".
8
+ *
9
+ * The rules live on the server (`tab-close.ts`, `routes/browser/tabs.ts`); what
10
+ * this hook must get right is the request it sends, the tab it names for the
11
+ * panel next, and — the failure that actually bites — that a refused close is
12
+ * REPORTED rather than looking like a tab that merely came back.
13
+ */
14
+
15
+ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
16
+ import { Window } from 'happy-dom';
17
+ import { h, render } from 'preact';
18
+ import { act } from 'preact/test-utils';
19
+
20
+ import { useCloseBrowserTab } from '@/client/hooks/browser/use-close-tab';
21
+
22
+ const DOM_GLOBALS = ['window', 'document', 'navigator', 'Node', 'Element', 'HTMLElement', 'Event'] as const;
23
+ const native: Partial<Record<(typeof DOM_GLOBALS)[number], unknown>> = {};
24
+
25
+ beforeAll(() => {
26
+ const win = new Window({ url: 'http://localhost' });
27
+ const target = globalThis as unknown as Record<string, unknown>;
28
+ for (const key of DOM_GLOBALS) {
29
+ native[key] = target[key];
30
+ target[key] = (win as unknown as Record<string, unknown>)[key];
31
+ }
32
+ });
33
+
34
+ afterAll(() => {
35
+ const target = globalThis as unknown as Record<string, unknown>;
36
+ for (const key of DOM_GLOBALS) {
37
+ if (native[key] === undefined) delete target[key];
38
+ else target[key] = native[key];
39
+ }
40
+ });
41
+
42
+ const globals = globalThis as unknown as Record<string, unknown>;
43
+ const nativeFetch = globals.fetch;
44
+
45
+ interface Captured {
46
+ url: string;
47
+ method: string;
48
+ body: unknown;
49
+ }
50
+
51
+ let captured: Captured[] = [];
52
+ let reply: { status: number; body: unknown } = { status: 200, body: { ok: true, next: 't3' } };
53
+ let hook: ReturnType<typeof useCloseBrowserTab> | null = null;
54
+
55
+ function Probe({ sessionId }: { sessionId: string }) {
56
+ hook = useCloseBrowserTab(sessionId);
57
+ return h('div', null, hook.closing ?? '');
58
+ }
59
+
60
+ beforeEach(() => {
61
+ captured = [];
62
+ reply = { status: 200, body: { ok: true, next: 't3' } };
63
+ globals.fetch = (url: string, init: RequestInit) => {
64
+ captured.push({ url, method: init.method ?? 'GET', body: JSON.parse(String(init.body)) });
65
+ return Promise.resolve(new Response(JSON.stringify(reply.body), { status: reply.status }));
66
+ };
67
+ hook = null;
68
+ });
69
+
70
+ afterEach(() => {
71
+ globals.fetch = nativeFetch;
72
+ });
73
+
74
+ async function mount(sessionId = 'session-1'): Promise<void> {
75
+ const host = document.createElement('div');
76
+ await act(async () => {
77
+ render(h(Probe, { sessionId }), host);
78
+ });
79
+ }
80
+
81
+ describe('useCloseBrowserTab', () => {
82
+ test('posts the target id to the session’s own close route', async () => {
83
+ await mount();
84
+ const result = await hook!.closeTab('t2');
85
+ expect(captured).toEqual([
86
+ { url: '/api/browser/session-1/tabs/close', method: 'POST', body: { targetId: 't2' } },
87
+ ]);
88
+ expect(result).toEqual({ ok: true, next: 't3' });
89
+ });
90
+
91
+ test('returns null when no tab is left to select', async () => {
92
+ reply = { status: 200, body: { ok: true, next: null } };
93
+ await mount();
94
+ expect(await hook!.closeTab('only')).toEqual({ ok: true, next: null });
95
+ });
96
+
97
+ test('a refusal is reported with the server’s own wording', async () => {
98
+ reply = { status: 403, body: { error: 'That tab does not belong to this session' } };
99
+ await mount();
100
+ expect(await hook!.closeTab('other')).toEqual({
101
+ ok: false,
102
+ next: null,
103
+ error: 'That tab does not belong to this session',
104
+ });
105
+ });
106
+
107
+ test('a refusal with no message still names the status', async () => {
108
+ reply = { status: 503, body: {} };
109
+ await mount();
110
+ expect((await hook!.closeTab('t1')).error).toBe('Close failed (503)');
111
+ });
112
+
113
+ test('a network failure does not look like a closed tab', async () => {
114
+ globals.fetch = () => Promise.reject(new Error('offline'));
115
+ await mount();
116
+ const result = await hook!.closeTab('t1');
117
+ expect(result.ok).toBe(false);
118
+ expect(result.error).toContain('offline');
119
+ });
120
+
121
+ test('a closed tab id is percent-encoded into the URL', async () => {
122
+ await mount('a/b');
123
+ await hook!.closeTab('t1');
124
+ expect(captured[0].url).toBe('/api/browser/a%2Fb/tabs/close');
125
+ });
126
+
127
+ test('an empty target is refused without a request', async () => {
128
+ await mount();
129
+ expect(await hook!.closeTab('')).toEqual({ ok: false, next: null, error: 'No tab to close' });
130
+ expect(captured).toEqual([]);
131
+ });
132
+
133
+ test('a close already in flight leaves its tab marked, then clears', async () => {
134
+ const { promise, resolve } = Promise.withResolvers<Response>();
135
+ globals.fetch = () => promise;
136
+ await mount();
137
+ let pending: Promise<unknown> | null = null;
138
+ await act(async () => {
139
+ pending = hook!.closeTab('t2');
140
+ });
141
+ expect(hook!.closing).toBe('t2');
142
+ await act(async () => {
143
+ resolve(new Response(JSON.stringify({ ok: true, next: null }), { status: 200 }));
144
+ await pending;
145
+ });
146
+ expect(hook!.closing).toBeNull();
147
+ });
148
+ });
@@ -0,0 +1,67 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * Closing one of the agent's browser tabs, from the panel.
8
+ *
9
+ * The server owns the rules (ownership, what CDP does to a screencast session);
10
+ * this hook owns only the client's half: refuse a second close while one is in
11
+ * flight, surface the refusal in words, and return the tab the panel should
12
+ * watch next so the strip does not jump to an unrelated page.
13
+ *
14
+ * Deliberately NOT optimistic: the tab strip is server state (the viewer's
15
+ * owned-target list), so removing a row before the browser confirms would show
16
+ * a tab that still exists — and a failed close would look like a refresh race.
17
+ */
18
+
19
+ import { useCallback, useState } from 'preact/hooks';
20
+
21
+ export interface CloseTabResult {
22
+ ok: boolean;
23
+ /** The tab to select once `targetId` is gone (null when none is left). */
24
+ next: string | null;
25
+ error?: string;
26
+ }
27
+
28
+ interface TabsCloseReply {
29
+ ok?: unknown;
30
+ error?: unknown;
31
+ next?: unknown;
32
+ }
33
+
34
+ function readNext(value: unknown): string | null {
35
+ return typeof value === 'string' && value ? value : null;
36
+ }
37
+
38
+ export function useCloseBrowserTab(sessionId: string | undefined) {
39
+ const [closing, setClosing] = useState<string | null>(null);
40
+
41
+ const closeTab = useCallback(
42
+ async (targetId: string): Promise<CloseTabResult> => {
43
+ if (!sessionId || !targetId) return { ok: false, next: null, error: 'No tab to close' };
44
+ setClosing(targetId);
45
+ try {
46
+ const response = await fetch(`/api/browser/${encodeURIComponent(sessionId)}/tabs/close`, {
47
+ method: 'POST',
48
+ headers: { 'content-type': 'application/json' },
49
+ body: JSON.stringify({ targetId }),
50
+ });
51
+ const body = (await response.json().catch(() => ({}))) as TabsCloseReply;
52
+ if (!response.ok) {
53
+ const error = typeof body.error === 'string' && body.error ? body.error : `Close failed (${response.status})`;
54
+ return { ok: false, next: null, error };
55
+ }
56
+ return { ok: true, next: readNext(body.next) };
57
+ } catch (error) {
58
+ return { ok: false, next: null, error: error instanceof Error ? error.message : String(error) };
59
+ } finally {
60
+ setClosing(null);
61
+ }
62
+ },
63
+ [sessionId],
64
+ );
65
+
66
+ return { closeTab, closing };
67
+ }
@@ -18,6 +18,8 @@ interface UseScreencastResult {
18
18
  */
19
19
  frame?: { data: string; mimeType: string } | null;
20
20
  actions: BrowserPanelAction[];
21
+ /** The session whose tabs these are; the tab-close request needs it. */
22
+ sessionId: string;
21
23
  /** Pin the viewer to one tab id; pass an empty string to follow the newest owned tab. */
22
24
  selectTarget: (targetId: string) => void;
23
25
  /** Drop the current SSE connection and open a fresh one. */
@@ -107,7 +109,13 @@ export function useScreencast(active = true): UseScreencastResult {
107
109
  source.addEventListener('state', (event) => {
108
110
  try {
109
111
  const next = JSON.parse((event as MessageEvent).data) as BrowserViewState;
110
- if (next.targetId !== currentTargetRef.current) setFrame(null);
112
+ // Do NOT clear the frame on a target change: the viewer seeds the new
113
+ // tab with a snapshot, and that frame can reach this handler BEFORE
114
+ // the state event that names the tab (measured: 26–47 ms from attach).
115
+ // Clearing here threw away the first picture of an idle tab, which is
116
+ // exactly the picture the snapshot exists to provide. A stale tab's
117
+ // pixels are dropped by the target check in the `frame` handler, and
118
+ // a non-live state below still clears.
111
119
  currentTargetRef.current = next.targetId;
112
120
  setState(next);
113
121
  if (next.status !== 'live') setFrame(null);
@@ -120,7 +128,10 @@ export function useScreencast(active = true): UseScreencastResult {
120
128
  try {
121
129
  const incoming = JSON.parse((event as MessageEvent).data) as BrowserViewFrame;
122
130
  const current = currentTargetRef.current;
131
+ // Adopt the frame's tab when no state has named one yet: the seed
132
+ // snapshot is the first thing this stream can deliver.
123
133
  if (current && incoming.targetId && incoming.targetId !== current) return;
134
+ if (!current && incoming.targetId) currentTargetRef.current = incoming.targetId;
124
135
  setFrame({ data: incoming.data, mimeType: incoming.mimeType });
125
136
  } catch {
126
137
  // Ignore malformed frame payloads.
@@ -180,6 +191,7 @@ export function useScreencast(active = true): UseScreencastResult {
180
191
  targetId: state.targetId,
181
192
  frame,
182
193
  actions,
194
+ sessionId,
183
195
  selectTarget,
184
196
  reconnect,
185
197
  };
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ /**
7
+ * The composer half of the browser-panel bridge.
8
+ *
9
+ * The failure this guards is a prompt that names a page the agent cannot place.
10
+ * The block is produced by `shared/lib/browser/page-context` (its own tests pin
11
+ * the text); what is pinned HERE is the wiring: an event appends, two events do
12
+ * not overwrite, and a malformed batch degrades to the plain block instead of
13
+ * rendering `NaN%` into the draft.
14
+ *
15
+ * An annotated page sends NO attachment — the user panel's page is a
16
+ * cross-origin iframe, so there are no bytes to attach.
17
+ */
18
+
19
+ import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test';
20
+ import { Window } from 'happy-dom';
21
+ import { h, render } from 'preact';
22
+ import { act } from 'preact/test-utils';
23
+
24
+ import { useBrowserPageContextInsert } from '@/client/hooks/chat/composer/browser-context';
25
+ import { emitBrowserPageContext } from '@/shared/lib/browser/page-context';
26
+
27
+ const DOM_GLOBALS = ['window', 'document', 'navigator', 'Node', 'Element', 'HTMLElement', 'Event', 'CustomEvent'] as const;
28
+ const native: Partial<Record<(typeof DOM_GLOBALS)[number], unknown>> = {};
29
+
30
+ beforeAll(() => {
31
+ const win = new Window({ url: 'http://localhost' });
32
+ const target = globalThis as unknown as Record<string, unknown>;
33
+ for (const key of DOM_GLOBALS) {
34
+ if (!(key in native)) native[key] = target[key];
35
+ target[key] = (win as unknown as Record<string, unknown>)[key];
36
+ }
37
+ });
38
+
39
+ afterAll(() => {
40
+ const target = globalThis as unknown as Record<string, unknown>;
41
+ for (const key of DOM_GLOBALS) {
42
+ if (native[key] === undefined) delete target[key];
43
+ else target[key] = native[key];
44
+ }
45
+ });
46
+
47
+ let container: HTMLElement | undefined;
48
+ let draft = '';
49
+
50
+ /** Mount the bridge over a draft string, the way the composer wires it. */
51
+ function mountBridge(): void {
52
+ draft = '';
53
+ const Probe = () => {
54
+ useBrowserPageContextInsert((next) => {
55
+ draft = typeof next === 'function' ? next(draft) : next;
56
+ });
57
+ return h('div', null);
58
+ };
59
+ container = document.body.appendChild(document.createElement('div'));
60
+ act(() => {
61
+ render(h(Probe, null), container!);
62
+ });
63
+ }
64
+
65
+ afterEach(() => {
66
+ if (container) render(null, container);
67
+ container?.remove();
68
+ container = undefined;
69
+ });
70
+
71
+ describe('useBrowserPageContextInsert', () => {
72
+ test('a plain page lands in the draft as the URL block', () => {
73
+ mountBridge();
74
+ act(() => emitBrowserPageContext({ url: 'https://a.test/x' }));
75
+ expect(draft).toBe('[Konteks halaman browser]\nURL: https://a.test/x\n(Lanjutkan dari halaman ini.)');
76
+ });
77
+
78
+ test('an annotated page lands as the block with its numbered marks', () => {
79
+ mountBridge();
80
+ act(() => emitBrowserPageContext({
81
+ url: 'https://a.test/x',
82
+ annotations: [{ id: 'a', tool: 'box', start: { x: 0.1, y: 0.1 }, end: { x: 0.2, y: 0.2 }, label: '', note: 'mati' }],
83
+ panel: { width: 1000, height: 500, viewportMode: 'laptop', zoom: 100 },
84
+ }));
85
+ expect(draft).toContain('Anotasi (1):');
86
+ expect(draft).toContain('1. kotak kiri 10.0%, atas 10.0%');
87
+ expect(draft).toContain('— mati');
88
+ });
89
+
90
+ test('two events append two blocks rather than overwriting the draft', () => {
91
+ mountBridge();
92
+ act(() => emitBrowserPageContext({ url: 'https://a.test' }));
93
+ act(() => emitBrowserPageContext({ url: 'https://b.test' }));
94
+ expect(draft.match(/\[Konteks halaman browser\]/g)).toHaveLength(2);
95
+ expect(draft).toContain('https://a.test');
96
+ expect(draft).toContain('https://b.test');
97
+ });
98
+
99
+ test('a detail with no url is ignored rather than pasting an empty block', () => {
100
+ mountBridge();
101
+ act(() => emitBrowserPageContext({ url: '' }));
102
+ expect(draft).toBe('');
103
+ });
104
+
105
+ test('a malformed mark batch degrades to the plain page block, never to NaN%', () => {
106
+ mountBridge();
107
+ act(() => emitBrowserPageContext({
108
+ url: 'https://a.test',
109
+ // A shape missing `end`/`note`, as a hand-written event or an older
110
+ // dispatch could carry: the reader must refuse it, not format it.
111
+ annotations: [{ id: 'a', tool: 'box', start: { x: 1, y: 0 } } as never],
112
+ panel: { width: 1000, height: 500, viewportMode: 'laptop', zoom: 100 },
113
+ }));
114
+ expect(draft).not.toContain('Anotasi');
115
+ expect(draft).toContain('URL: https://a.test');
116
+ });
117
+ });