@myrialabs/clopen 0.4.47 → 0.4.49

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 (390) hide show
  1. package/README.md +11 -11
  2. package/backend/artifacts/project/sources.ts +1 -1
  3. package/backend/auth/auth-service.ts +52 -358
  4. package/backend/auth/onboarding.test.ts +2 -3
  5. package/backend/auth/permissions.test.ts +10 -20
  6. package/backend/auth/permissions.ts +4 -290
  7. package/backend/auth/rate-limiter.test.ts +8 -9
  8. package/backend/auth/rate-limiter.ts +0 -2
  9. package/backend/auth/tokens.ts +1 -8
  10. package/backend/bootstrap.ts +7 -0
  11. package/backend/chat/attachments.test.ts +88 -0
  12. package/backend/chat/attachments.ts +159 -0
  13. package/backend/chat/engine-handoff.test.ts +0 -2
  14. package/backend/chat/message-pages.test.ts +163 -0
  15. package/backend/chat/message-pages.ts +233 -0
  16. package/backend/chat/stream-manager.ts +91 -67
  17. package/backend/chat/turn-answer.test.ts +64 -0
  18. package/backend/chat/turn-answer.ts +125 -0
  19. package/backend/chat-apps/awaiting.test.ts +39 -0
  20. package/backend/chat-apps/awaiting.ts +49 -0
  21. package/backend/chat-apps/conversation.ts +746 -0
  22. package/backend/chat-apps/inbound.ts +324 -0
  23. package/backend/chat-apps/index.ts +309 -0
  24. package/backend/chat-apps/link-events.ts +20 -0
  25. package/backend/chat-apps/media.test.ts +62 -0
  26. package/backend/chat-apps/media.ts +208 -0
  27. package/backend/chat-apps/messages.test.ts +229 -0
  28. package/backend/chat-apps/messages.ts +365 -0
  29. package/backend/chat-apps/outbound.ts +243 -0
  30. package/backend/chat-apps/pairing.test.ts +45 -0
  31. package/backend/chat-apps/pairing.ts +74 -0
  32. package/backend/chat-apps/platforms/index.ts +20 -0
  33. package/backend/chat-apps/platforms/telegram.test.ts +35 -0
  34. package/backend/chat-apps/platforms/telegram.ts +191 -0
  35. package/backend/chat-apps/platforms/types.ts +114 -0
  36. package/backend/chat-apps/questions.ts +133 -0
  37. package/backend/chat-apps/runtime.ts +572 -0
  38. package/backend/chat-apps/storage.test.ts +201 -0
  39. package/backend/chat-apps/turns.test.ts +45 -0
  40. package/backend/chat-apps/turns.ts +208 -0
  41. package/backend/containers/pty.ts +3 -11
  42. package/backend/control/actions/accounts.ts +89 -0
  43. package/backend/control/actions/files.ts +116 -0
  44. package/backend/control/actions/models.ts +188 -0
  45. package/backend/control/actions/projects.ts +272 -0
  46. package/backend/control/actions/sessions.ts +280 -0
  47. package/backend/control/actions/skills.ts +111 -0
  48. package/backend/control/control.test.ts +169 -0
  49. package/backend/control/define.ts +20 -0
  50. package/backend/control/folders.test.ts +67 -0
  51. package/backend/control/index.ts +213 -0
  52. package/backend/control/invoke.ts +44 -0
  53. package/backend/control/links.ts +35 -0
  54. package/backend/control/paging.ts +26 -0
  55. package/backend/control/types.ts +117 -0
  56. package/backend/control/workspace.ts +127 -0
  57. package/backend/database/migrations/084_create_channel_links.ts +71 -0
  58. package/backend/database/migrations/085_add_home_projects.ts +36 -0
  59. package/backend/database/migrations/086_single_owner.ts +418 -0
  60. package/backend/database/migrations/087_create_blob_line_stats.ts +37 -0
  61. package/backend/database/migrations/index.ts +28 -0
  62. package/backend/database/queries/auth-queries.ts +7 -85
  63. package/backend/database/queries/blob-line-stats-queries.ts +48 -0
  64. package/backend/database/queries/channel-queries.ts +270 -0
  65. package/backend/database/queries/db-client-connection-queries.ts +18 -56
  66. package/backend/database/queries/file-share-queries.ts +5 -23
  67. package/backend/database/queries/git-identity-queries.ts +31 -42
  68. package/backend/database/queries/graph-queries.ts +1 -1
  69. package/backend/database/queries/index.ts +15 -1
  70. package/backend/database/queries/message-queries.test.ts +146 -0
  71. package/backend/database/queries/message-queries.ts +42 -1
  72. package/backend/database/queries/permission-set-queries.ts +1 -1
  73. package/backend/database/queries/profile-queries.ts +1 -1
  74. package/backend/database/queries/project-mcp-trust-queries.ts +1 -1
  75. package/backend/database/queries/project-queries.ts +50 -63
  76. package/backend/database/queries/session-queries.ts +28 -56
  77. package/backend/database/queries/ssh-connection-queries.ts +18 -41
  78. package/backend/db-client/env/diff.ts +9 -33
  79. package/backend/db-client/env/env.test.ts +8 -8
  80. package/backend/db-client/env/service.ts +1 -1
  81. package/backend/db-client/integrations/projector.ts +0 -5
  82. package/backend/engine/adapters/claude/message-converter.test.ts +47 -0
  83. package/backend/engine/adapters/claude/message-converter.ts +25 -2
  84. package/backend/engine/adapters/cline/message-converter.ts +0 -1
  85. package/backend/engine/adapters/cline/stream.ts +11 -15
  86. package/backend/engine/adapters/codex/message-converter.ts +0 -2
  87. package/backend/engine/adapters/copilot/message-converter.ts +1 -2
  88. package/backend/engine/adapters/cursor/message-converter.ts +0 -1
  89. package/backend/engine/adapters/opencode/message-converter.ts +2 -4
  90. package/backend/engine/adapters/opencode/stream.ts +11 -0
  91. package/backend/engine/adapters/parent-scope.test.ts +38 -0
  92. package/backend/engine/adapters/parent-scope.ts +44 -0
  93. package/backend/engine/adapters/pi/message-converter.ts +0 -1
  94. package/backend/engine/adapters/pi/stream.ts +4 -5
  95. package/backend/engine/adapters/qwen/message-converter.ts +0 -2
  96. package/backend/engine/bootstrap-default-engine.ts +2 -2
  97. package/backend/engine/docs/adapter-contract.md +8 -1
  98. package/backend/engine/install-runner.ts +8 -17
  99. package/backend/files/file-shares.ts +20 -44
  100. package/backend/files/file-size-limit.ts +3 -3
  101. package/backend/files/file-watcher.ts +19 -0
  102. package/backend/files/image-processing.ts +1 -1
  103. package/backend/files/os-clipboard.ts +2 -2
  104. package/backend/git/contributors.test.ts +35 -0
  105. package/backend/git/contributors.ts +46 -0
  106. package/backend/git/git-executor.ts +25 -5
  107. package/backend/git/git-service.ts +420 -19
  108. package/backend/git/identity/credential-helper.test.ts +2 -7
  109. package/backend/git/identity/import-local.test.ts +11 -16
  110. package/backend/git/identity/import-local.ts +12 -12
  111. package/backend/git/identity/index.ts +10 -24
  112. package/backend/git/identity/resolve.ts +13 -14
  113. package/backend/git/identity/scope-config.ts +33 -28
  114. package/backend/git/identity/service.ts +44 -47
  115. package/backend/git/op-lock.test.ts +111 -0
  116. package/backend/git/op-lock.ts +180 -0
  117. package/backend/git/remote-sync.test.ts +249 -0
  118. package/backend/host/metrics.ts +1 -1
  119. package/backend/http/bearer-auth.ts +1 -2
  120. package/backend/http/files-download.test.ts +8 -9
  121. package/backend/http/files-download.ts +2 -2
  122. package/backend/http/files-shared.test.ts +62 -89
  123. package/backend/http/files-shared.ts +2 -2
  124. package/backend/http/files-upload.test.ts +3 -7
  125. package/backend/http/files-upload.ts +3 -3
  126. package/backend/http/notes-images.test.ts +15 -38
  127. package/backend/http/notes-images.ts +6 -8
  128. package/backend/http/ssh-sftp.ts +5 -10
  129. package/backend/http/static-assets.test.ts +78 -0
  130. package/backend/http/static-assets.ts +104 -0
  131. package/backend/index.ts +32 -16
  132. package/backend/integrations/accounts.ts +61 -3
  133. package/backend/integrations/index.ts +2 -2
  134. package/backend/integrations/registry/define.ts +15 -0
  135. package/backend/integrations/registry/index.ts +2 -0
  136. package/backend/integrations/registry/providers/telegram.ts +54 -0
  137. package/backend/integrations/registry/types.ts +21 -1
  138. package/backend/integrations/webhooks/gateway.ts +7 -6
  139. package/backend/integrations/webhooks/verify.test.ts +30 -0
  140. package/backend/integrations/webhooks/verify.ts +4 -0
  141. package/backend/mcp/external/project.ts +1 -1
  142. package/backend/mcp/internal/remote-server.test.ts +1 -3
  143. package/backend/mcp/internal/remote-server.ts +4 -4
  144. package/backend/mcp/internal/servers/browser-automation/actions/dialogs.ts +67 -0
  145. package/backend/mcp/internal/servers/browser-automation/actions/index.ts +3 -1
  146. package/backend/mcp/internal/servers/browser-automation/actions/input.ts +5 -5
  147. package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +157 -28
  148. package/backend/mcp/internal/servers/browser-automation/context.ts +1 -0
  149. package/backend/mcp/internal/servers/browser-automation/description.ts +5 -2
  150. package/backend/mcp/internal/servers/browser-automation/format.ts +24 -1
  151. package/backend/mcp/internal/servers/browser-automation/runner.ts +68 -3
  152. package/backend/memory/redact.ts +1 -1
  153. package/backend/notes/access.test.ts +28 -29
  154. package/backend/notes/access.ts +6 -11
  155. package/backend/permissions/service.ts +1 -1
  156. package/backend/preview/browser/browser-clipboard.test.ts +46 -0
  157. package/backend/preview/browser/browser-clipboard.ts +67 -0
  158. package/backend/preview/browser/browser-dialog-handler.ts +95 -6
  159. package/backend/preview/browser/browser-host-bridge.ts +9 -0
  160. package/backend/preview/browser/browser-interaction-handler.ts +316 -86
  161. package/backend/preview/browser/browser-mcp-control.ts +34 -0
  162. package/backend/preview/browser/browser-pool.ts +67 -6
  163. package/backend/preview/browser/browser-preview-service.ts +383 -29
  164. package/backend/preview/browser/browser-tab-manager.ts +66 -23
  165. package/backend/preview/browser/browser-video-capture.ts +92 -5
  166. package/backend/preview/browser/element-locator.ts +190 -17
  167. package/backend/preview/browser/keyboard-chords.test.ts +68 -0
  168. package/backend/preview/browser/keyboard-chords.ts +132 -0
  169. package/backend/preview/browser/paste.ts +81 -0
  170. package/backend/preview/browser/process-usage.ts +142 -0
  171. package/backend/preview/browser/profile-lock.ts +209 -0
  172. package/backend/preview/browser/scripts/host-bridge.ts +59 -0
  173. package/backend/preview/browser/scripts/video-stream.ts +58 -0
  174. package/backend/preview/browser/types.ts +40 -2
  175. package/backend/preview/browser/viewport.test.ts +45 -0
  176. package/backend/preview/browser/viewport.ts +96 -0
  177. package/backend/profiles/service.ts +2 -2
  178. package/backend/project/status-manager.ts +43 -172
  179. package/backend/projects/home.ts +61 -0
  180. package/backend/projects/project-cleanup.integration.test.ts +2 -2
  181. package/backend/projects/project-cleanup.ts +0 -8
  182. package/backend/skills/service.ts +2 -3
  183. package/backend/snapshot/blob-store.ts +9 -1
  184. package/backend/snapshot/helpers.ts +3 -11
  185. package/backend/snapshot/line-stats.test.ts +80 -0
  186. package/backend/snapshot/line-stats.ts +64 -0
  187. package/backend/snapshot/restore.ts +252 -0
  188. package/backend/snapshot/snapshot-service.ts +14 -27
  189. package/backend/snapshot/turn-changes.test.ts +10 -5
  190. package/backend/snapshot/turn-changes.ts +78 -40
  191. package/backend/ssh/ptykit.ts +1 -2
  192. package/backend/terminal/ptykit.ts +10 -8
  193. package/backend/tunnel/public-origin.ts +31 -20
  194. package/backend/utils/user-helpers.ts +0 -55
  195. package/backend/utils/ws-wire.test.ts +87 -0
  196. package/backend/utils/ws-wire.ts +189 -0
  197. package/backend/utils/ws.ts +101 -192
  198. package/backend/worktrees/branching/engine.ts +3 -16
  199. package/backend/worktrees/branching/index.ts +1 -1
  200. package/backend/worktrees/branching/projector.ts +1 -1
  201. package/backend/worktrees/branching/service.ts +13 -25
  202. package/backend/ws/access.ts +12 -21
  203. package/backend/ws/artifacts/generate.ts +2 -2
  204. package/backend/ws/auth/device.ts +16 -71
  205. package/backend/ws/auth/index.ts +4 -22
  206. package/backend/ws/auth/login.test.ts +14 -60
  207. package/backend/ws/auth/login.ts +24 -124
  208. package/backend/ws/auth/status.ts +0 -1
  209. package/backend/ws/chat/composer.ts +118 -118
  210. package/backend/ws/chat/stream.ts +105 -57
  211. package/backend/ws/chat-apps/index.ts +117 -0
  212. package/backend/ws/containers/actions.ts +11 -25
  213. package/backend/ws/containers/index.ts +3 -4
  214. package/backend/ws/containers/list.ts +10 -36
  215. package/backend/ws/containers/logs.ts +2 -2
  216. package/backend/ws/containers/tunnel.ts +1 -2
  217. package/backend/ws/db-client/access.ts +2 -16
  218. package/backend/ws/db-client/accounts.ts +5 -15
  219. package/backend/ws/db-client/connections.ts +17 -25
  220. package/backend/ws/db-client/context.ts +1 -1
  221. package/backend/ws/db-client/env.ts +7 -10
  222. package/backend/ws/db-client/index.ts +2 -2
  223. package/backend/ws/db-client/io.ts +5 -5
  224. package/backend/ws/db-client/query-history-retention.test.ts +8 -8
  225. package/backend/ws/db-client/query.ts +18 -17
  226. package/backend/ws/db-client/schema.ts +13 -13
  227. package/backend/ws/db-client/structure.ts +35 -35
  228. package/backend/ws/db-client/supabase.ts +4 -21
  229. package/backend/ws/deployments/context.ts +1 -1
  230. package/backend/ws/deployments/crud.ts +0 -5
  231. package/backend/ws/engine/access.ts +7 -20
  232. package/backend/ws/engine/claude/accounts.ts +4 -4
  233. package/backend/ws/engine/cline/accounts.ts +5 -5
  234. package/backend/ws/engine/codex/accounts.ts +3 -3
  235. package/backend/ws/engine/pi/accounts.ts +5 -5
  236. package/backend/ws/files/clipboard.ts +8 -27
  237. package/backend/ws/files/image-edit.ts +6 -6
  238. package/backend/ws/files/index.ts +1 -1
  239. package/backend/ws/files/path-access.test.ts +32 -131
  240. package/backend/ws/files/path-access.ts +8 -87
  241. package/backend/ws/files/read.ts +45 -19
  242. package/backend/ws/files/reveal.ts +3 -3
  243. package/backend/ws/files/search.ts +7 -7
  244. package/backend/ws/files/share.ts +9 -13
  245. package/backend/ws/files/state.ts +9 -12
  246. package/backend/ws/files/watch.ts +3 -3
  247. package/backend/ws/files/write.ts +30 -31
  248. package/backend/ws/git/branch.ts +80 -59
  249. package/backend/ws/git/commit.ts +63 -44
  250. package/backend/ws/git/conflict.ts +16 -18
  251. package/backend/ws/git/identity-context.ts +8 -17
  252. package/backend/ws/git/identity.ts +17 -27
  253. package/backend/ws/git/index.ts +3 -1
  254. package/backend/ws/git/log.ts +52 -0
  255. package/backend/ws/git/op-state.ts +139 -0
  256. package/backend/ws/git/remote.ts +241 -141
  257. package/backend/ws/git/repo-context.ts +67 -0
  258. package/backend/ws/git/staging.ts +18 -28
  259. package/backend/ws/git/status.ts +5 -2
  260. package/backend/ws/index.ts +5 -1
  261. package/backend/ws/instructions/crud.ts +1 -1
  262. package/backend/ws/integrations/crud.ts +1 -3
  263. package/backend/ws/mcp/crud.ts +3 -5
  264. package/backend/ws/mcp/tools.ts +1 -1
  265. package/backend/ws/memory/crud.ts +14 -46
  266. package/backend/ws/memory/index.ts +2 -2
  267. package/backend/ws/messages/crud.ts +78 -8
  268. package/backend/ws/notes/index.ts +33 -39
  269. package/backend/ws/permissions/crud.ts +3 -4
  270. package/backend/ws/ports/index.ts +1 -2
  271. package/backend/ws/ports/kill.ts +5 -10
  272. package/backend/ws/ports/list.ts +4 -10
  273. package/backend/ws/preview/access.ts +1 -1
  274. package/backend/ws/preview/browser/interact.ts +52 -79
  275. package/backend/ws/preview/browser/native-ui.ts +24 -0
  276. package/backend/ws/preview/browser/stats.ts +98 -1
  277. package/backend/ws/preview/browser/tab-info.ts +15 -0
  278. package/backend/ws/preview/browser/tab.ts +19 -4
  279. package/backend/ws/preview/index.ts +29 -0
  280. package/backend/ws/preview/watchers.ts +84 -0
  281. package/backend/ws/profiles/crud.ts +6 -8
  282. package/backend/ws/project-artifacts/index.ts +30 -31
  283. package/backend/ws/projects/crud.ts +33 -35
  284. package/backend/ws/projects/index.ts +3 -8
  285. package/backend/ws/projects/info.ts +3 -3
  286. package/backend/ws/projects/overview.ts +19 -30
  287. package/backend/ws/projects/status.ts +17 -57
  288. package/backend/ws/sessions/crud.ts +42 -57
  289. package/backend/ws/sessions/index.ts +1 -1
  290. package/backend/ws/settings/crud.ts +1 -1
  291. package/backend/ws/skills/crud.ts +3 -4
  292. package/backend/ws/skills/index.ts +1 -1
  293. package/backend/ws/snapshot/changes.ts +12 -13
  294. package/backend/ws/snapshot/restore.ts +11 -295
  295. package/backend/ws/snapshot/timeline.ts +2 -4
  296. package/backend/ws/ssh/access.ts +6 -21
  297. package/backend/ws/ssh/connections.ts +21 -31
  298. package/backend/ws/ssh/forwards.ts +12 -12
  299. package/backend/ws/ssh/sftp.ts +34 -34
  300. package/backend/ws/ssh/tunnel.ts +1 -2
  301. package/backend/ws/stack/install.ts +7 -17
  302. package/backend/ws/subagents/crud.ts +1 -1
  303. package/backend/ws/terminal/tunnel.ts +2 -2
  304. package/backend/ws/tunnel/share.ts +0 -2
  305. package/backend/ws/user/crud.ts +1 -59
  306. package/backend/ws/user/index.ts +1 -1
  307. package/backend/ws/work/context.ts +1 -1
  308. package/backend/ws/work/crud.ts +0 -5
  309. package/backend/ws/workspace/state.ts +10 -13
  310. package/backend/ws/worktrees/branching.ts +7 -36
  311. package/backend/ws/worktrees/crud.ts +2 -2
  312. package/bin/clopen.ts +46 -41
  313. package/dist/assets/addon-clipboard-wHJhZAA4.js.br +0 -0
  314. package/dist/assets/addon-clipboard-wHJhZAA4.js.gz +0 -0
  315. package/dist/assets/addon-fit-DX4qG4td.js.br +1 -0
  316. package/dist/assets/addon-fit-DX4qG4td.js.gz +0 -0
  317. package/dist/assets/addon-ligatures-DpDY8N_X.js.br +0 -0
  318. package/dist/assets/addon-ligatures-DpDY8N_X.js.gz +0 -0
  319. package/dist/assets/addon-unicode11-Bt8F3D7-.js.br +0 -0
  320. package/dist/assets/addon-unicode11-Bt8F3D7-.js.gz +0 -0
  321. package/dist/assets/addon-web-links-DIbG5aQx.js.br +0 -0
  322. package/dist/assets/addon-web-links-DIbG5aQx.js.gz +0 -0
  323. package/dist/assets/diff.worker-BoHLxmr2.js +7 -0
  324. package/dist/assets/diff.worker-BoHLxmr2.js.br +0 -0
  325. package/dist/assets/diff.worker-BoHLxmr2.js.gz +0 -0
  326. package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.br +0 -0
  327. package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.gz +0 -0
  328. package/dist/assets/{index-CwuKQelV.js → index-CrVWZH_D.js} +1 -1
  329. package/dist/assets/index-CrVWZH_D.js.br +0 -0
  330. package/dist/assets/index-CrVWZH_D.js.gz +0 -0
  331. package/dist/assets/{index-BTcR1byF.js → index-DD0-3EWg.js} +1 -1
  332. package/dist/assets/index-DD0-3EWg.js.br +0 -0
  333. package/dist/assets/index-DD0-3EWg.js.gz +0 -0
  334. package/dist/assets/main-D2_TmcwQ.js +1240 -0
  335. package/dist/assets/main-D2_TmcwQ.js.br +0 -0
  336. package/dist/assets/main-D2_TmcwQ.js.gz +0 -0
  337. package/dist/assets/main-Yucigcmf.css +1 -0
  338. package/dist/assets/main-Yucigcmf.css.br +0 -0
  339. package/dist/assets/main-Yucigcmf.css.gz +0 -0
  340. package/dist/assets/preview-video.worker-DcpDhCWL.js.br +0 -0
  341. package/dist/assets/preview-video.worker-DcpDhCWL.js.gz +0 -0
  342. package/dist/assets/xterm-B-qIQCd3.js.br +0 -0
  343. package/dist/assets/xterm-B-qIQCd3.js.gz +0 -0
  344. package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.br +0 -0
  345. package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.gz +0 -0
  346. package/dist/fonts/dm-sans.css.br +0 -0
  347. package/dist/fonts/dm-sans.css.gz +0 -0
  348. package/dist/index.html +2 -2
  349. package/dist/index.html.br +0 -0
  350. package/dist/index.html.gz +0 -0
  351. package/dist/sw.js.br +0 -0
  352. package/dist/sw.js.gz +0 -0
  353. package/package.json +3 -3
  354. package/shared/constants/attachments.ts +14 -0
  355. package/shared/constants/engine-tools.ts +1 -1
  356. package/shared/constants/preview.test.ts +259 -0
  357. package/shared/constants/preview.ts +1018 -57
  358. package/shared/constants/tool-icons.ts +5 -0
  359. package/shared/types/containers/container.ts +1 -1
  360. package/shared/types/database/schema.ts +9 -6
  361. package/shared/types/git-identity.ts +1 -1
  362. package/shared/types/git.ts +116 -0
  363. package/shared/types/integrations.ts +7 -0
  364. package/shared/types/stores/settings.ts +1 -1
  365. package/shared/types/unified/index.ts +1 -1
  366. package/shared/types/unified/message.ts +0 -7
  367. package/shared/types/unified/stream.ts +13 -5
  368. package/shared/types/unified/tool.ts +15 -1
  369. package/shared/utils/ask-user-question.test.ts +319 -0
  370. package/shared/utils/ask-user-question.ts +154 -0
  371. package/shared/utils/diff-calculator.test.ts +178 -41
  372. package/shared/utils/diff-calculator.ts +383 -100
  373. package/shared/utils/home-project.ts +25 -0
  374. package/shared/utils/logger.ts +2 -0
  375. package/shared/utils/process-supervisor.test.ts +172 -0
  376. package/shared/utils/process-supervisor.ts +251 -0
  377. package/shared/utils/ws-chunk.test.ts +33 -0
  378. package/shared/utils/ws-chunk.ts +73 -0
  379. package/shared/utils/ws-client-chunks.test.ts +45 -0
  380. package/shared/utils/ws-client.ts +43 -0
  381. package/shared/utils/ws-server.ts +49 -21
  382. package/backend/auth/session-invalidation.test.ts +0 -112
  383. package/backend/auth/session-invalidation.ts +0 -77
  384. package/backend/project/status-manager.test.ts +0 -32
  385. package/backend/ws/auth/invites.ts +0 -94
  386. package/backend/ws/auth/users.ts +0 -112
  387. package/backend/ws/projects/presence.ts +0 -46
  388. package/backend/ws/stack/access.ts +0 -31
  389. package/dist/assets/main-BiDNMWb3.css +0 -1
  390. package/dist/assets/main-BlhKWz2B.js +0 -1241
@@ -5,15 +5,19 @@ import { BrowserConsoleManager } from './browser-console-manager.js';
5
5
  import { BrowserInteractionHandler, type AutonomousRunOutcome } from './browser-interaction-handler.js';
6
6
  import { BrowserNavigationTracker } from './browser-navigation-tracker.js';
7
7
  import { BrowserVideoCapture } from './browser-video-capture.js';
8
- import { BrowserDialogHandler } from './browser-dialog-handler.js';
8
+ import { BrowserDialogHandler, type PendingDialogInfo } from './browser-dialog-handler.js';
9
9
  import { BrowserNativeUIHandler } from './browser-native-ui-handler.js';
10
10
  import { BrowserHostBridge, type HostResponse } from './browser-host-bridge.js';
11
11
  import { BrowserTabLifecycle } from './browser-tab-lifecycle.js';
12
+ import { WorkspaceClipboard, type ClipboardSource } from './browser-clipboard.js';
12
13
  import { browserMcpControl } from './browser-mcp-control.js';
13
14
  import { browserPool } from './browser-pool.js';
14
15
  import { ws } from '$backend/utils/ws';
15
- import { getViewportDimensions } from '$shared/constants/preview.js';
16
+ import { defaultRotation, type DeviceRequest } from '$shared/constants/preview.js';
17
+ import { applyViewport, type EmulationPatch } from './viewport';
16
18
  import { debug } from '$shared/utils/logger';
19
+ import { emitToPreviewWatchers, forgetAgentCursor, rememberAgentCursor } from '../../ws/preview/watchers';
20
+ import { sleep } from '$shared/utils/async';
17
21
  import { scopeProjectId } from '$shared/utils/workspace-scope';
18
22
  import type {
19
23
  BrowserSiteData,
@@ -23,6 +27,8 @@ import type {
23
27
  BrowserAutonomousAction,
24
28
  DeviceSize,
25
29
  Rotation,
30
+ ViewportSize,
31
+ PageColors,
26
32
  BrowserDialogResponse,
27
33
  BrowserSelectResponse,
28
34
  BrowserContextMenuResponse,
@@ -34,6 +40,17 @@ import type {
34
40
  ClientStreamFeedback
35
41
  } from './types';
36
42
 
43
+ /**
44
+ * How often a watched phone or tablet tab has its colours re-read. A page can
45
+ * change them without navigating — a theme toggle, an agent's colorScheme —
46
+ * and the bands around it would otherwise keep the old ones until the next
47
+ * load. One small evaluate per watched tab; most rounds emit nothing.
48
+ */
49
+ const PAGE_COLORS_POLL_MS = 1500;
50
+
51
+ /** Time a window needs after leaving fullscreen before an override sticks. */
52
+ const WINDOW_SETTLE_MS = 150;
53
+
37
54
  /**
38
55
  * Browser Preview Service
39
56
  *
@@ -57,6 +74,8 @@ export class BrowserPreviewService extends EventEmitter {
57
74
  private nativeUIHandler: BrowserNativeUIHandler;
58
75
  private hostBridge: BrowserHostBridge;
59
76
  private lifecycle: BrowserTabLifecycle;
77
+ /** What was copied last in this workspace — see browser-clipboard.ts. */
78
+ private clipboard = new WorkspaceClipboard();
60
79
 
61
80
  // Store context menu info for later action execution
62
81
  private contextMenus = new Map<string, BrowserContextMenuInfo>();
@@ -95,17 +114,28 @@ export class BrowserPreviewService extends EventEmitter {
95
114
  this.consoleManager = new BrowserConsoleManager({
96
115
  isWatched: (tabId) => this.lifecycle.hasViewers(tabId)
97
116
  });
98
- this.interactionHandler = new BrowserInteractionHandler();
117
+ this.interactionHandler = new BrowserInteractionHandler({
118
+ readClipboard: () => this.clipboard.read(),
119
+ getPendingDialog: (tabId) => this.dialogHandler.getPendingDialog(tabId),
120
+ waitForDialog: (tabId) => this.dialogHandler.waitForDialog(tabId)
121
+ });
99
122
  this.navigationTracker = new BrowserNavigationTracker();
100
123
  this.videoCapture = new BrowserVideoCapture();
101
- this.dialogHandler = new BrowserDialogHandler();
124
+ this.dialogHandler = new BrowserDialogHandler({
125
+ isAgentHeld: (tabId) => browserMcpControl.isTabControlled(tabId, this.projectId)
126
+ });
102
127
  this.nativeUIHandler = new BrowserNativeUIHandler();
103
128
  this.hostBridge = new BrowserHostBridge();
104
129
 
105
130
  // Forward events from handlers to main service
106
131
  this.setupEventForwarding();
132
+
133
+ this.pageColorsTimer = setInterval(() => void this.pollPageColors(), PAGE_COLORS_POLL_MS);
107
134
  }
108
135
 
136
+ /** Re-reads the page colours of streaming handheld tabs; see `pollPageColors`. */
137
+ private pageColorsTimer: ReturnType<typeof setInterval> | null = null;
138
+
109
139
  /**
110
140
  * Tabs that must keep running even though nobody is watching them.
111
141
  *
@@ -262,6 +292,12 @@ export class BrowserPreviewService extends EventEmitter {
262
292
  this.videoCapture.on('connection-state', (data) => {
263
293
  this.emit('preview:browser-webcodecs-connection-state', data);
264
294
  });
295
+ // The captured surface lost the page's shape (Chrome's own fullscreen
296
+ // stranded it). Detected from the frames themselves, so it is caught
297
+ // whatever caused it — not only when the page's shim saw the fullscreen.
298
+ this.videoCapture.on('surface-drift', (data: { sessionId: string }) => {
299
+ void this.restoreEmulatedViewport(data.sessionId, null);
300
+ });
265
301
  this.videoCapture.on('cursor-change', (data) => {
266
302
  this.emit('preview:browser-cursor-change', data);
267
303
  });
@@ -293,8 +329,18 @@ export class BrowserPreviewService extends EventEmitter {
293
329
  this.applyFullscreenState(data.tabId, data.requestId, data.payload);
294
330
  return;
295
331
  }
332
+ if (this.answerClipboardRequest(data.tabId, data.requestId, data.kind, data.payload)) return;
296
333
  this.emit('preview:browser-host-request', data);
297
334
  });
335
+ // A clipboard read the viewer answered tells us what their device holds.
336
+ this.hostBridge.on('clipboard-observed', (data: { text: string }) => {
337
+ this.clipboard.write(data.text, 'device');
338
+ });
339
+ // Everything copied anywhere but on the device goes down to the devices,
340
+ // which is what keeps the user's clipboard and the workspace's in step.
341
+ this.clipboard.on('changed', (data) => {
342
+ this.emit('preview:browser-clipboard-sync', data);
343
+ });
298
344
  this.hostBridge.on('download', (data) => {
299
345
  this.emit('preview:browser-download', data);
300
346
  });
@@ -304,6 +350,9 @@ export class BrowserPreviewService extends EventEmitter {
304
350
 
305
351
  // Forward native UI events
306
352
  this.nativeUIHandler.on('copy-to-clipboard', (data) => {
353
+ // Into the workspace clipboard, which mirrors it to the devices; the
354
+ // event itself is only the viewer's confirmation that it happened.
355
+ this.clipboard.write(data.text, 'menu');
307
356
  this.emit('preview:browser-copy-to-clipboard', data);
308
357
  });
309
358
  this.nativeUIHandler.on('open-url-new-tab', (data) => {
@@ -351,10 +400,12 @@ export class BrowserPreviewService extends EventEmitter {
351
400
  url?: string,
352
401
  deviceSize: DeviceSize = 'laptop',
353
402
  rotation?: Rotation,
354
- launchId?: string
403
+ launchId?: string,
404
+ viewport?: ViewportSize | null,
405
+ device?: { deviceModel?: string | null; emulation?: EmulationPatch | null }
355
406
  ): Promise<BrowserTab> {
356
407
  // Use device-appropriate default rotation if not specified
357
- const actualRotation = rotation || ((deviceSize === 'desktop' || deviceSize === 'laptop') ? 'landscape' : 'portrait');
408
+ const actualRotation = rotation || defaultRotation(deviceSize);
358
409
 
359
410
  // Dialog interception and the capability shims both have to be in place
360
411
  // before the first navigation: a page can call alert() or getUserMedia()
@@ -369,7 +420,10 @@ export class BrowserPreviewService extends EventEmitter {
369
420
  const tab = await this.tabManager.createTab(url, deviceSize, actualRotation, {
370
421
  setActive: true,
371
422
  preNavigationSetup,
372
- launchId
423
+ launchId,
424
+ viewport,
425
+ deviceModel: device?.deviceModel,
426
+ emulation: device?.emulation
373
427
  });
374
428
 
375
429
  // Setup console, navigation tracking, and pre-inject streaming scripts in parallel
@@ -610,11 +664,89 @@ export class BrowserPreviewService extends EventEmitter {
610
664
  };
611
665
 
612
666
  this.emit('preview:browser-tab-meta', meta);
667
+ await this.refreshPageColors(tabId);
613
668
  } catch (error) {
614
669
  debug.warn('preview', `⚠️ Failed to refresh tab meta for ${tabId}:`, error);
615
670
  }
616
671
  }
617
672
 
673
+ /**
674
+ * Read the colours a page asks to be framed in, as a mobile browser does:
675
+ * the status bar takes the page's `theme-color` (honouring its `media`),
676
+ * and everything else the root background, falling through to the body
677
+ * and then to the canvas default for the page's colour scheme.
678
+ *
679
+ * Read from the DOM, never sampled from pixels — a sampled edge picks up
680
+ * whatever header, image or button happens to touch it, which is how three
681
+ * sides of one page came out three different colours.
682
+ */
683
+ private async readPageColors(page: Page): Promise<PageColors | null> {
684
+ const read = page.evaluate(() => {
685
+ const probe = document.createElement('canvas').getContext('2d');
686
+ const normalise = (colour: string | null | undefined): string | null => {
687
+ if (!colour || !probe) return null;
688
+ probe.fillStyle = '#000001';
689
+ probe.fillStyle = colour;
690
+ const value = String(probe.fillStyle);
691
+ return value === '#000001' && colour.trim().toLowerCase() !== '#000001' ? null : value;
692
+ };
693
+ const opaque = (colour: string | null): string | null => {
694
+ if (!colour) return null;
695
+ const alpha = /rgba?\([^)]*,\s*([\d.]+)\s*\)/.exec(colour);
696
+ return alpha && Number(alpha[1]) === 0 ? null : colour;
697
+ };
698
+
699
+ let theme: string | null = null;
700
+ for (const meta of Array.from(document.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"]'))) {
701
+ const media = meta.getAttribute('media');
702
+ if (!media || matchMedia(media).matches) {
703
+ theme = opaque(normalise(meta.content));
704
+ if (theme) break;
705
+ }
706
+ }
707
+
708
+ const root = opaque(normalise(getComputedStyle(document.documentElement).backgroundColor));
709
+ const body = document.body ? opaque(normalise(getComputedStyle(document.body).backgroundColor)) : null;
710
+ const darkCanvas =
711
+ getComputedStyle(document.documentElement).colorScheme.includes('dark') &&
712
+ matchMedia('(prefers-color-scheme: dark)').matches;
713
+ const background = root ?? body ?? (darkCanvas ? '#121212' : '#ffffff');
714
+
715
+ return { statusBar: theme ?? background, background };
716
+ });
717
+ // A page mid-navigation can reject after the timeout has already won;
718
+ // caught here so that late rejection is not left unhandled.
719
+ const settled = read.catch(() => null);
720
+ const timeout = new Promise<null>((resolve) => setTimeout(() => resolve(null), 1000));
721
+ return await Promise.race([settled, timeout]);
722
+ }
723
+
724
+ /** Re-read a tab's page colours and tell viewers when they changed. */
725
+ private async refreshPageColors(tabId: string): Promise<void> {
726
+ const tab = this.getTab(tabId);
727
+ if (!tab || tab.page.isClosed()) return;
728
+
729
+ const colors = await this.readPageColors(tab.page);
730
+ if (!colors) return;
731
+ if (tab.pageColors?.statusBar === colors.statusBar && tab.pageColors?.background === colors.background) return;
732
+
733
+ tab.pageColors = colors;
734
+ this.emit('preview:browser-page-colors', { tabId, ...colors, timestamp: Date.now() });
735
+ }
736
+
737
+ /**
738
+ * Only phones and tablets have bands to paint, and only a tab someone is
739
+ * watching needs them current. Sleeping tabs are left alone — running
740
+ * script in a frozen page would wake it.
741
+ */
742
+ private async pollPageColors(): Promise<void> {
743
+ for (const tab of this.tabManager.getAllTabs()) {
744
+ if (tab.deviceSize !== 'mobile' && tab.deviceSize !== 'tablet') continue;
745
+ if (!this.videoCapture.isStreaming(tab.id) || this.lifecycle.isFrozen(tab.id)) continue;
746
+ await this.refreshPageColors(tab.id);
747
+ }
748
+ }
749
+
618
750
  /**
619
751
  * Resolve the page's favicon to an absolute URL, falling back to the
620
752
  * origin's /favicon.ico the way a browser does.
@@ -727,11 +859,29 @@ export class BrowserPreviewService extends EventEmitter {
727
859
  }
728
860
 
729
861
  /**
730
- * Change viewport settings (device size and rotation) for an existing tab
862
+ * Change the device class, orientation or exact size of an existing tab,
863
+ * and rebuild the capture around it.
864
+ *
865
+ * One call for every caller — the viewer's device menu, a drag, an agent's
866
+ * `set_viewport`. The agent's path used to stop at the page's viewport,
867
+ * leaving the capture sized for the old shape until something else
868
+ * happened to restart it.
731
869
  */
732
- async setViewport(tabId: string, deviceSize: DeviceSize, rotation: Rotation): Promise<boolean> {
870
+ async setViewport(
871
+ tabId: string,
872
+ request: DeviceRequest & { emulation?: EmulationPatch | null }
873
+ ): Promise<boolean> {
733
874
  await this.ensureTabAwake(tabId);
734
- return await this.tabManager.setViewport(tabId, deviceSize, rotation);
875
+ const ok = await this.tabManager.setViewport(tabId, request);
876
+ if (!ok) return false;
877
+
878
+ const tab = this.getTab(tabId);
879
+ // False only means nothing is streaming yet; the stream that starts
880
+ // later is sized from the viewport just set.
881
+ if (tab) await this.videoCapture.updateViewport(tabId, tab);
882
+ // A colour-scheme change recolours the page without a navigation.
883
+ void this.refreshPageColors(tabId);
884
+ return true;
735
885
  }
736
886
 
737
887
  /**
@@ -956,17 +1106,22 @@ export class BrowserPreviewService extends EventEmitter {
956
1106
  * it only happens on a fullscreen the page did not ask for, which is rare
957
1107
  * enough that the restart it costs is not worth trying to avoid.
958
1108
  */
959
- private async restoreEmulatedViewport(tabId: string, requestId: string): Promise<void> {
1109
+ private async restoreEmulatedViewport(tabId: string, requestId: string | null): Promise<void> {
960
1110
  try {
961
1111
  const tab = this.getTab(tabId);
962
1112
  if (tab) {
963
- const { width, height } = getViewportDimensions(tab.deviceSize, tab.rotation);
1113
+ const { width, height } = tab.viewport;
1114
+
1115
+ // A fullscreen that never exited leaves the window itself in
1116
+ // `fullscreen` state, and no viewport override survives that — the
1117
+ // surface stays the window's shape. Put the window back first.
1118
+ await this.normalizeWindow(tab.page);
964
1119
 
965
1120
  // The capture path re-sends the override and recomputes the screencast
966
1121
  // geometry with it. With no stream running there is no geometry to
967
1122
  // recompute and the override is all that is needed.
968
- const restored = await this.videoCapture.updateViewport(tabId, tab, width, height);
969
- if (!restored) await tab.page.setViewport({ width, height });
1123
+ const restored = await this.videoCapture.updateViewport(tabId, tab);
1124
+ if (!restored) await applyViewport(tab.page, tab);
970
1125
 
971
1126
  debug.log('preview', `🖥️ Restored emulated viewport for tab ${tabId}: ${width}x${height}`);
972
1127
  }
@@ -975,7 +1130,40 @@ export class BrowserPreviewService extends EventEmitter {
975
1130
  } finally {
976
1131
  // The page holds a promise on this. Nothing awaits it, but an unsettled
977
1132
  // request sits in the pending map until it times out.
978
- this.hostBridge.respond(requestId, { ok: true });
1133
+ if (requestId) this.hostBridge.respond(requestId, { ok: true });
1134
+ }
1135
+ }
1136
+
1137
+ /** Take a tab's window out of fullscreen/maximized/minimized, if it is in one. */
1138
+ private async normalizeWindow(page: Page): Promise<void> {
1139
+ const cdp = await page.createCDPSession().catch(() => null);
1140
+ if (!cdp) return;
1141
+ try {
1142
+ const { windowId, bounds } = (await cdp.send('Browser.getWindowForTarget')) as {
1143
+ windowId: number;
1144
+ bounds: { windowState?: string };
1145
+ };
1146
+ if (bounds.windowState && bounds.windowState !== 'normal') {
1147
+ await cdp.send('Browser.setWindowBounds', { windowId, bounds: { windowState: 'normal' } });
1148
+
1149
+ // The window resizes asynchronously. A viewport override sent
1150
+ // before it has settled is applied to the old surface and then
1151
+ // stranded again by the resize — measured: back at 1366×681.
1152
+ for (let attempt = 0; attempt < 20; attempt++) {
1153
+ const current = (await cdp.send('Browser.getWindowBounds', { windowId })) as {
1154
+ bounds: { windowState?: string };
1155
+ };
1156
+ if (current.bounds.windowState === 'normal') break;
1157
+ await sleep(25);
1158
+ }
1159
+ await sleep(WINDOW_SETTLE_MS);
1160
+ debug.log('preview', `🪟 Window was left ${bounds.windowState}; set back to normal`);
1161
+ }
1162
+ } catch {
1163
+ // Not every build lets a page session ask about its window; the
1164
+ // viewport override below is the part that matters.
1165
+ } finally {
1166
+ await cdp.detach().catch(() => {});
979
1167
  }
980
1168
  }
981
1169
 
@@ -1033,21 +1221,13 @@ export class BrowserPreviewService extends EventEmitter {
1033
1221
  this.fullscreenTabs.delete(tabId);
1034
1222
  this.emit('preview:browser-fullscreen-state', { tabId, active: false });
1035
1223
 
1036
- const { width, height } = getViewportDimensions(tab.deviceSize, tab.rotation);
1037
- const restored = await this.videoCapture.updateViewport(tabId, tab, width, height);
1038
- if (!restored) await tab.page.setViewport({ width, height }).catch(() => {});
1224
+ const restored = await this.videoCapture.updateViewport(tabId, tab);
1225
+ if (!restored) await applyViewport(tab.page, tab).catch(() => {});
1039
1226
 
1040
1227
  debug.log('preview', `🖥️ Forced tab ${tabId} out of full screen`);
1041
1228
  return true;
1042
1229
  }
1043
1230
 
1044
- async updateWebCodecsViewport(tabId: string, width: number, height: number): Promise<boolean> {
1045
- const tab = this.getTab(tabId);
1046
- if (!tab) {
1047
- return false;
1048
- }
1049
- return await this.videoCapture.updateViewport(tabId, tab, width, height);
1050
- }
1051
1231
 
1052
1232
  async getWebCodecsOffer(tabId: string, viewerId: string): Promise<RTCSessionDescriptionInit | null> {
1053
1233
  const tab = this.getTab(tabId);
@@ -1178,6 +1358,21 @@ export class BrowserPreviewService extends EventEmitter {
1178
1358
  return await this.dialogHandler.respondToDialog(response);
1179
1359
  }
1180
1360
 
1361
+ /** The dialog a tab's page is blocked on, if any. */
1362
+ getPendingDialog(tabId: string): PendingDialogInfo | null {
1363
+ return this.dialogHandler.getPendingDialog(tabId);
1364
+ }
1365
+
1366
+ /** Resolve when a tab raises a dialog; `dispose` once the race is decided. */
1367
+ waitForDialog(tabId: string): { promise: Promise<PendingDialogInfo>; dispose: () => void } {
1368
+ return this.dialogHandler.waitForDialog(tabId);
1369
+ }
1370
+
1371
+ /** Answer the dialog a tab has open. Null when there was none to answer. */
1372
+ async respondToTabDialog(tabId: string, accept: boolean, promptText?: string): Promise<PendingDialogInfo | null> {
1373
+ return this.dialogHandler.respondForTab(tabId, accept, promptText);
1374
+ }
1375
+
1181
1376
  // ============================================================================
1182
1377
  // Native UI Methods (Select & Context Menu)
1183
1378
  // ============================================================================
@@ -1234,6 +1429,119 @@ export class BrowserPreviewService extends EventEmitter {
1234
1429
  return result;
1235
1430
  }
1236
1431
 
1432
+ // ============================================================================
1433
+ // Diagnostics
1434
+ // ============================================================================
1435
+
1436
+ /**
1437
+ * What every tab of this workspace is doing and costing, for the
1438
+ * diagnostics panel.
1439
+ *
1440
+ * Nothing here runs script in a page: a frozen tab must stay frozen for
1441
+ * being looked at, and a tab paused on a dialog would hang the read. Page
1442
+ * metrics come over the DevTools protocol, which answers either way, and
1443
+ * stream counters are only read from tabs that are actually streaming.
1444
+ */
1445
+ async getDiagnostics() {
1446
+ const tabs = await Promise.all(
1447
+ this.getAllTabs().map(async (tab) => {
1448
+ const frozen = this.lifecycle.isFrozen(tab.id);
1449
+ const dialog = this.dialogHandler.getPendingDialog(tab.id);
1450
+ const owner = browserMcpControl.getTabOwner(tab.id);
1451
+
1452
+ let jsHeapUsedBytes: number | null = null;
1453
+ let domNodes: number | null = null;
1454
+ if (!tab.page.isClosed()) {
1455
+ const metrics = await Promise.race([
1456
+ tab.page.metrics().catch(() => null),
1457
+ new Promise<null>((resolve) => setTimeout(() => resolve(null), 1500))
1458
+ ]);
1459
+ jsHeapUsedBytes = metrics?.JSHeapUsedSize ?? null;
1460
+ domNodes = metrics?.Nodes ?? null;
1461
+ }
1462
+
1463
+ const streaming = this.isWebCodecsActive(tab.id);
1464
+ const stream =
1465
+ streaming && !frozen && !dialog
1466
+ ? await Promise.race([
1467
+ this.getWebCodecsStats(tab.id).catch(() => null),
1468
+ new Promise<null>((resolve) => setTimeout(() => resolve(null), 1500))
1469
+ ])
1470
+ : null;
1471
+
1472
+ return {
1473
+ id: tab.id,
1474
+ title: tab.title,
1475
+ url: tab.page.isClosed() ? tab.url : tab.page.url(),
1476
+ state: frozen ? ('frozen' as const) : ('active' as const),
1477
+ watched: this.lifecycle.hasViewers(tab.id),
1478
+ isLoading: !!tab.isLoading,
1479
+ openedBy: tab.openedBy?.kind ?? 'user',
1480
+ agentHeld: !!owner,
1481
+ dialog: dialog ? { type: dialog.type, message: dialog.message } : null,
1482
+ createdAt: tab.createdAt,
1483
+ lastAccessedAt: tab.lastAccessedAt,
1484
+ jsHeapUsedBytes,
1485
+ domNodes,
1486
+ stream: stream
1487
+ ? {
1488
+ videoBytesSent: stream.videoBytesSent ?? 0,
1489
+ videoFramesEncoded: stream.videoFramesEncoded ?? 0,
1490
+ connectionState: stream.connectionState ?? 'unknown'
1491
+ }
1492
+ : null
1493
+ };
1494
+ })
1495
+ );
1496
+
1497
+ return { tabs, clipboard: this.clipboard.describe() };
1498
+ }
1499
+
1500
+ // ============================================================================
1501
+ // Clipboard
1502
+ // ============================================================================
1503
+
1504
+ /**
1505
+ * Clipboard traffic from a page, answered here rather than by a viewer.
1506
+ *
1507
+ * Writes (and copy events) go into the workspace clipboard straight away:
1508
+ * there is no decision in them, and waiting on a device meant a page's
1509
+ * "Copy" button failed whenever nobody was watching. Reads are answered from
1510
+ * the workspace clipboard while an agent holds the tab — the agent is the
1511
+ * user there, and a prompt on someone's screen would stall its run. A read
1512
+ * on a tab a person is driving still asks that person, as a browser would.
1513
+ */
1514
+ private answerClipboardRequest(tabId: string, requestId: string, kind: string, payload: unknown): boolean {
1515
+ if (kind === 'clipboard-write' || kind === 'clipboard-copied') {
1516
+ const text = (payload as { text?: unknown } | null)?.text;
1517
+ if (typeof text === 'string') this.clipboard.write(text, 'page');
1518
+ this.hostBridge.respond(requestId, { ok: true });
1519
+ return true;
1520
+ }
1521
+
1522
+ if (kind === 'clipboard-read' && browserMcpControl.isTabControlled(tabId, this.projectId)) {
1523
+ this.hostBridge.respond(requestId, { ok: true, result: this.clipboard.read() });
1524
+ return true;
1525
+ }
1526
+
1527
+ return false;
1528
+ }
1529
+
1530
+ /** The workspace clipboard's text. */
1531
+ getClipboardText(): string {
1532
+ return this.clipboard.read();
1533
+ }
1534
+
1535
+ /** Who last wrote the workspace clipboard, for diagnostics. */
1536
+ describeClipboard() {
1537
+ return this.clipboard.describe();
1538
+ }
1539
+
1540
+ /** Record a copy made somewhere other than a page (the user's device, the agent). */
1541
+ setClipboardText(text: string, source: ClipboardSource): void {
1542
+ this.clipboard.write(text, source);
1543
+ }
1544
+
1237
1545
  // ============================================================================
1238
1546
  // Host Bridge Methods
1239
1547
  // ============================================================================
@@ -1285,6 +1593,10 @@ export class BrowserPreviewService extends EventEmitter {
1285
1593
  // Cleanup Methods
1286
1594
  // ============================================================================
1287
1595
  async cleanup() {
1596
+ if (this.pageColorsTimer) {
1597
+ clearInterval(this.pageColorsTimer);
1598
+ this.pageColorsTimer = null;
1599
+ }
1288
1600
  // Clear all cursor tracking
1289
1601
  this.interactionHandler.clearAllSessionCursors();
1290
1602
  // Release host-bridge scratch dirs and unblock any parked page promises
@@ -1451,6 +1763,10 @@ class BrowserPreviewServiceManager {
1451
1763
  });
1452
1764
 
1453
1765
  // Forward live tab metadata (title, favicon, back/forward availability)
1766
+ service.on('preview:browser-page-colors', (data) => {
1767
+ ws.emit.project(roomId, 'preview:browser-page-colors', { ...data, projectId });
1768
+ });
1769
+
1454
1770
  service.on('preview:browser-tab-meta', (data) => {
1455
1771
  ws.emit.project(roomId, 'preview:browser-tab-meta', { ...data, projectId });
1456
1772
  });
@@ -1471,12 +1787,15 @@ class BrowserPreviewServiceManager {
1471
1787
  });
1472
1788
 
1473
1789
  // Forward console events
1790
+ // Console lines are a panel's live detail, as frequent as the page is
1791
+ // chatty: only connections with a Preview open receive them. A panel that
1792
+ // opens later seeds itself from the console history.
1474
1793
  service.on('preview:browser-console-message', (data) => {
1475
- ws.emit.project(roomId, 'preview:browser-console-message', data);
1794
+ emitToPreviewWatchers(roomId, 'preview:browser-console-message', data);
1476
1795
  });
1477
1796
 
1478
1797
  service.on('preview:browser-console-clear', (data) => {
1479
- ws.emit.project(roomId, 'preview:browser-console-clear', data);
1798
+ emitToPreviewWatchers(roomId, 'preview:browser-console-clear', data);
1480
1799
  });
1481
1800
 
1482
1801
  // Forward dialog events
@@ -1511,6 +1830,12 @@ class BrowserPreviewServiceManager {
1511
1830
  ws.emit.project(roomId, 'preview:browser-copy-to-clipboard', data);
1512
1831
  });
1513
1832
 
1833
+ // The workspace clipboard changed — every device of this project writes it
1834
+ // to its own clipboard (when focused; otherwise on its next focus).
1835
+ service.on('preview:browser-clipboard-sync', (data) => {
1836
+ ws.emit.project(roomId, 'preview:browser-clipboard-sync', { ...data, projectId });
1837
+ });
1838
+
1514
1839
  service.on('preview:browser-open-url-new-tab', (data) => {
1515
1840
  ws.emit.project(roomId, 'preview:browser-open-url-new-tab', data);
1516
1841
  });
@@ -1589,6 +1914,7 @@ class BrowserPreviewServiceManager {
1589
1914
  // No longer pinned: an unwatched tab the agent has let go of is free
1590
1915
  // to fall asleep on the usual timer.
1591
1916
  this.services.get(data.projectId)?.noteTabActivity(data.browserTabId);
1917
+ forgetAgentCursor(data.projectId, data.browserTabId);
1592
1918
  ws.emit.project(data.projectId, 'preview:browser-mcp-control-end', {
1593
1919
  browserTabId: data.browserTabId,
1594
1920
  projectId: data.projectId,
@@ -1605,21 +1931,49 @@ class BrowserPreviewServiceManager {
1605
1931
  });
1606
1932
  });
1607
1933
 
1934
+ // The agent's cursor moves many times a second while it works and is only
1935
+ // drawn inside a Preview, so its steps go to connections with one open;
1936
+ // where it came to rest is remembered for a Preview that opens later.
1608
1937
  browserMcpControl.on('cursor-position', (data) => {
1609
1938
  if (!data.projectId) return;
1610
- ws.emit.project(data.projectId, 'preview:browser-mcp-cursor-position', {
1939
+ const cursor = {
1611
1940
  sessionId: data.tabId,
1612
1941
  x: data.x,
1613
1942
  y: data.y,
1614
1943
  pressed: data.pressed ?? false,
1615
1944
  timestamp: data.timestamp,
1616
1945
  source: 'mcp' as const
1946
+ };
1947
+ rememberAgentCursor(data.projectId, cursor);
1948
+ emitToPreviewWatchers(data.projectId, 'preview:browser-mcp-cursor-position', cursor);
1949
+ });
1950
+
1951
+ browserMcpControl.on('cursor-glide', (data) => {
1952
+ if (!data.projectId) return;
1953
+ rememberAgentCursor(data.projectId, {
1954
+ sessionId: data.tabId,
1955
+ x: data.to.x,
1956
+ y: data.to.y,
1957
+ pressed: data.pressed ?? false,
1958
+ timestamp: data.timestamp,
1959
+ source: 'mcp' as const
1960
+ });
1961
+ emitToPreviewWatchers(data.projectId, 'preview:browser-mcp-cursor-glide', {
1962
+ sessionId: data.tabId,
1963
+ from: data.from,
1964
+ control: data.control,
1965
+ to: data.to,
1966
+ durationMs: data.durationMs,
1967
+ pressed: data.pressed,
1968
+ easing: data.easing,
1969
+ timestamp: data.timestamp,
1970
+ source: 'mcp' as const
1617
1971
  });
1618
1972
  });
1619
1973
 
1620
1974
  browserMcpControl.on('cursor-click', (data) => {
1621
1975
  if (!data.projectId) return;
1622
- ws.emit.project(data.projectId, 'preview:browser-mcp-cursor-click', {
1976
+ emitToPreviewWatchers(data.projectId, 'preview:browser-mcp-cursor-click', {
1623
1977
  sessionId: data.tabId,
1624
1978
  x: data.x,
1625
1979
  y: data.y,