@causyne/agent 1.20.124 → 1.20.128

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 (254) hide show
  1. package/bin/causyne.js +1 -1
  2. package/docs/THIRD_PARTY_NOTICES.md +36 -2
  3. package/package.json +6 -4
  4. package/src/acp/protocol.ts +19 -1
  5. package/src/acp/server.js +282 -8
  6. package/src/acp/server.ts +284 -8
  7. package/src/agent/abortableDelay.js +2 -2
  8. package/src/agent/abortableDelay.ts +2 -2
  9. package/src/agent/errorClassifier.js +9 -1
  10. package/src/agent/errorClassifier.ts +8 -1
  11. package/src/agentLoop.js +142 -53
  12. package/src/agentLoop.ts +151 -50
  13. package/src/approvalPolicy.js +34 -10
  14. package/src/approvalPolicy.ts +42 -11
  15. package/src/browser/causyneBrowser.js +61 -3
  16. package/src/browser/causyneBrowser.ts +65 -3
  17. package/src/browser/urlPolicy.js +14 -1
  18. package/src/browser/urlPolicy.ts +12 -1
  19. package/src/cliMode.js +21 -9
  20. package/src/cliMode.ts +20 -8
  21. package/src/commands.js +365 -45
  22. package/src/commands.ts +403 -45
  23. package/src/config.js +19 -4
  24. package/src/config.ts +18 -4
  25. package/src/core/contextCompactor.js +54 -7
  26. package/src/core/contextCompactor.ts +63 -7
  27. package/src/core/goalLoop.js +257 -0
  28. package/src/core/goalLoop.ts +310 -0
  29. package/src/entrypoint.js +135 -3
  30. package/src/entrypoint.ts +136 -3
  31. package/src/game/causyneCoreBridge.js +686 -0
  32. package/src/game/causyneCoreBridge.ts +702 -0
  33. package/src/game/gameLauncher.js +287 -0
  34. package/src/game/gameLauncher.ts +352 -0
  35. package/src/game/gameProvisioner.js +425 -0
  36. package/src/game/gameProvisioner.ts +488 -0
  37. package/src/headlessCli.js +19 -12
  38. package/src/headlessCli.ts +22 -8
  39. package/src/infra/atomicWriter.js +40 -4
  40. package/src/infra/atomicWriter.ts +36 -4
  41. package/src/launchIntent.js +73 -3
  42. package/src/launchIntent.ts +76 -3
  43. package/src/lsp/lspClient.js +83 -12
  44. package/src/lsp/lspClient.ts +75 -12
  45. package/src/mcp/mcpRegistry.js +75 -8
  46. package/src/mcp/mcpRegistry.ts +70 -7
  47. package/src/memory/contextBudget.js +2 -0
  48. package/src/memory/contextBudget.ts +1 -0
  49. package/src/memory/episodicStore.js +40 -0
  50. package/src/memory/episodicStore.ts +46 -0
  51. package/src/memory.js +67 -0
  52. package/src/memory.ts +65 -0
  53. package/src/modelVariants.js +45 -0
  54. package/src/modelVariants.ts +54 -0
  55. package/src/outputRecovery.js +3 -2
  56. package/src/outputRecovery.ts +4 -2
  57. package/src/permissionPresets.js +327 -0
  58. package/src/permissionPresets.ts +361 -0
  59. package/src/personalization.js +365 -0
  60. package/src/personalization.ts +471 -0
  61. package/src/projectInstructions.js +30 -10
  62. package/src/projectInstructions.ts +31 -12
  63. package/src/providers/acp.js +3 -2
  64. package/src/providers/acp.ts +3 -2
  65. package/src/providers/antigravity.js +64 -9
  66. package/src/providers/antigravity.ts +75 -9
  67. package/src/providers/byokHttp.js +7 -1
  68. package/src/providers/byokHttp.ts +6 -1
  69. package/src/providers/byokStream.js +21 -8
  70. package/src/providers/byokStream.ts +17 -7
  71. package/src/providers/dashscope-byok.js +21 -5
  72. package/src/providers/dashscope-byok.ts +29 -11
  73. package/src/providers/moonshot.js +26 -7
  74. package/src/providers/moonshot.ts +34 -13
  75. package/src/providers/muse.js +6 -0
  76. package/src/providers/muse.ts +8 -0
  77. package/src/providers/ollama.js +39 -7
  78. package/src/providers/ollama.ts +36 -7
  79. package/src/providers/openai-compatible.js +16 -3
  80. package/src/providers/openai-compatible.ts +13 -3
  81. package/src/providers/openai.js +62 -4
  82. package/src/providers/openai.ts +50 -4
  83. package/src/providers/openrouter.js +19 -4
  84. package/src/providers/openrouter.ts +21 -4
  85. package/src/providers/qwen-byok.js +21 -5
  86. package/src/providers/qwen-byok.ts +29 -11
  87. package/src/providers/xai.js +26 -10
  88. package/src/providers/xai.ts +34 -16
  89. package/src/reporting.js +4 -2
  90. package/src/reporting.ts +4 -2
  91. package/src/sandbox.js +34 -0
  92. package/src/sandbox.ts +43 -0
  93. package/src/scheduler/scheduler.js +372 -0
  94. package/src/scheduler/scheduler.ts +433 -0
  95. package/src/sessionSearchIndex.js +267 -0
  96. package/src/sessionSearchIndex.ts +311 -0
  97. package/src/skillsManager.js +2 -3
  98. package/src/skillsManager.ts +2 -3
  99. package/src/toolPacks.js +6 -0
  100. package/src/toolPacks.ts +6 -0
  101. package/src/tools/applyPatch.js +1 -1
  102. package/src/tools/applyPatch.ts +1 -1
  103. package/src/tools/generateMediaViaApi.js +4 -3
  104. package/src/tools/generateMediaViaApi.ts +4 -3
  105. package/src/tools/memoryWrite.js +15 -1
  106. package/src/tools/memoryWrite.ts +16 -1
  107. package/src/tools/nativeEngine.js +2 -2
  108. package/src/tools/nativeEngine.ts +2 -2
  109. package/src/tools/openSourceToolProvisioner.js +2 -2
  110. package/src/tools/openSourceToolProvisioner.ts +2 -2
  111. package/src/tools/osSandbox.js +1 -1
  112. package/src/tools/osSandbox.ts +1 -1
  113. package/src/tools/payloadSanitizer.js +0 -1
  114. package/src/tools/payloadSanitizer.ts +0 -1
  115. package/src/tools/pdfReader.js +122 -20
  116. package/src/tools/pdfReader.ts +111 -20
  117. package/src/tools/presentDeliverable.js +103 -0
  118. package/src/tools/presentDeliverable.ts +127 -0
  119. package/src/tools/ptcRuntime.js +444 -0
  120. package/src/tools/ptcRuntime.ts +490 -0
  121. package/src/tools/ptyTerminal.js +1108 -0
  122. package/src/tools/ptyTerminal.ts +1245 -0
  123. package/src/tools/registry.js +21 -12
  124. package/src/tools/registry.ts +21 -12
  125. package/src/tools/toolOutputStore.js +26 -4
  126. package/src/tools/toolOutputStore.ts +22 -4
  127. package/src/tools/webFetch.js +158 -11
  128. package/src/tools/webFetch.ts +145 -11
  129. package/src/tools/webSearch.js +123 -36
  130. package/src/tools/webSearch.ts +124 -33
  131. package/src/tui/banner.js +5 -1
  132. package/src/tui/banner.ts +6 -1
  133. package/src/tui/chatRenderer.js +1 -2
  134. package/src/tui/chatRenderer.ts +1 -2
  135. package/src/tui/completion.js +1 -0
  136. package/src/tui/completion.ts +1 -0
  137. package/src/tui/composerControls.js +111 -27
  138. package/src/tui/composerControls.ts +124 -28
  139. package/src/tui/dialogs.js +3 -16
  140. package/src/tui/dialogs.ts +3 -16
  141. package/src/tui/home.js +1 -0
  142. package/src/tui/home.ts +1 -0
  143. package/src/tui/invocation.js +1 -2
  144. package/src/tui/invocation.ts +1 -2
  145. package/src/tui/modelPicker.js +22 -12
  146. package/src/tui/modelPicker.ts +22 -12
  147. package/src/tui/overlay.js +2 -5
  148. package/src/tui/overlay.ts +2 -5
  149. package/src/tui/prompt.js +6 -4
  150. package/src/tui/prompt.ts +6 -4
  151. package/src/tui/selectList.js +4 -12
  152. package/src/tui/selectList.ts +4 -12
  153. package/src/tui/setup.js +22 -9
  154. package/src/tui/setup.ts +26 -9
  155. package/src/tui/statusLine.ts +2 -0
  156. package/src/web/client/browserPanel.js +905 -0
  157. package/src/web/client/browserPanel.ts +872 -0
  158. package/src/web/client/chatStream.js +321 -0
  159. package/src/web/client/chatStream.ts +297 -0
  160. package/src/web/client/cliPanel.js +165 -0
  161. package/src/web/client/cliPanel.ts +171 -0
  162. package/src/web/client/composer.js +204 -0
  163. package/src/web/client/composer.ts +209 -0
  164. package/src/web/client/diffPanel.js +176 -0
  165. package/src/web/client/diffPanel.ts +203 -0
  166. package/src/web/client/dom.js +271 -0
  167. package/src/web/client/dom.ts +272 -0
  168. package/src/web/client/filesAndCanvas.js +233 -0
  169. package/src/web/client/filesAndCanvas.ts +240 -0
  170. package/src/web/client/gameLauncherUi.js +78 -0
  171. package/src/web/client/gameLauncherUi.ts +78 -0
  172. package/src/web/client/main.js +316 -0
  173. package/src/web/client/main.ts +391 -0
  174. package/src/web/client/markdown.js +364 -0
  175. package/src/web/client/markdown.ts +373 -0
  176. package/src/web/client/modelPicker.js +455 -0
  177. package/src/web/client/modelPicker.ts +451 -0
  178. package/src/web/client/permissionPresetsUi.js +307 -0
  179. package/src/web/client/permissionPresetsUi.ts +333 -0
  180. package/src/web/client/sessions.js +353 -0
  181. package/src/web/client/sessions.ts +372 -0
  182. package/src/web/client/settingsModal.js +824 -0
  183. package/src/web/client/settingsModal.ts +959 -0
  184. package/src/web/client/state.js +280 -0
  185. package/src/web/client/state.ts +259 -0
  186. package/src/web/client/streamSmoother.js +127 -0
  187. package/src/web/client/streamSmoother.ts +119 -0
  188. package/src/web/client/studioLayout.js +89 -0
  189. package/src/web/client/studioLayout.ts +103 -0
  190. package/src/web/client/studioShell.js +660 -0
  191. package/src/web/client/studioShell.ts +672 -0
  192. package/src/web/client/studioStart.js +77 -0
  193. package/src/web/client/studioStart.ts +89 -0
  194. package/src/web/client/terminalPanel.js +606 -0
  195. package/src/web/client/terminalPanel.ts +632 -0
  196. package/src/web/client/terminalTheme.js +249 -0
  197. package/src/web/client/terminalTheme.ts +258 -0
  198. package/src/web/client/turnView.js +934 -0
  199. package/src/web/client/turnView.ts +981 -0
  200. package/src/web/client/types.js +1 -0
  201. package/src/web/client/types.ts +322 -0
  202. package/src/web/client/validators.js +147 -0
  203. package/src/web/client/validators.ts +140 -0
  204. package/src/web/server/activeRuns.js +152 -0
  205. package/src/web/server/activeRuns.ts +171 -0
  206. package/src/web/server/routesBrowser.js +409 -0
  207. package/src/web/server/routesBrowser.ts +435 -0
  208. package/src/web/server/routesChat.js +304 -0
  209. package/src/web/server/routesChat.ts +347 -0
  210. package/src/web/server/routesCore.js +472 -0
  211. package/src/web/server/routesCore.ts +522 -0
  212. package/src/web/server/routesSettings.js +344 -0
  213. package/src/web/server/routesSettings.ts +397 -0
  214. package/src/web/server/routesTerminal.js +162 -0
  215. package/src/web/server/routesTerminal.ts +182 -0
  216. package/src/web/server/routesWorkspace.js +510 -0
  217. package/src/web/server/routesWorkspace.ts +546 -0
  218. package/src/web/server/security.js +98 -0
  219. package/src/web/server/security.ts +108 -0
  220. package/src/web/server/sessionTranscript.js +586 -0
  221. package/src/web/server/sessionTranscript.ts +566 -0
  222. package/src/web/server/staticAssets.js +104 -0
  223. package/src/web/server/staticAssets.ts +116 -0
  224. package/src/web/server/types.js +1 -0
  225. package/src/web/server/types.ts +135 -0
  226. package/src/web/server/workspaceFiles.js +368 -0
  227. package/src/web/server/workspaceFiles.ts +363 -0
  228. package/src/web/skybridgeServer.js +78 -2448
  229. package/src/web/skybridgeServer.ts +120 -2600
  230. package/src/web/skybridgeUi.js +20 -450
  231. package/src/web/skybridgeUi.ts +24 -455
  232. package/src/web/styles/agent.css +380 -0
  233. package/src/web/styles/composer.css +193 -0
  234. package/src/web/styles/overlays.css +935 -0
  235. package/src/web/styles/shell.css +387 -0
  236. package/src/web/styles/studio.css +951 -0
  237. package/src/web/styles/thread.css +221 -0
  238. package/src/web/styles/tokens.css +94 -0
  239. package/src/web/ui/brandMark.js +73 -0
  240. package/src/web/ui/brandMark.ts +82 -0
  241. package/src/web/ui/modalsTemplate.js +388 -0
  242. package/src/web/ui/modalsTemplate.ts +388 -0
  243. package/src/web/ui/sidebarTemplate.js +63 -0
  244. package/src/web/ui/sidebarTemplate.ts +64 -0
  245. package/src/web/ui/studioTemplate.js +256 -0
  246. package/src/web/ui/studioTemplate.ts +256 -0
  247. package/src/web/ui/workspaceTemplate.js +72 -0
  248. package/src/web/ui/workspaceTemplate.ts +73 -0
  249. package/src/worktreeManager.js +16 -1
  250. package/src/worktreeManager.ts +22 -1
  251. package/src/web/skybridgeClientScript.js +0 -3951
  252. package/src/web/skybridgeClientScript.ts +0 -3952
  253. package/src/web/skybridgeStyles.js +0 -827
  254. package/src/web/skybridgeStyles.ts +0 -828
@@ -0,0 +1,409 @@
1
+ import { auditLog } from '../../auditLog.js';
2
+ import { causyneBrowser, expandSearchMacro } from '../../browser/causyneBrowser.js';
3
+ import { discoverLocalCdpTabs, importExplicitUserBrowserData, syncLocalCdpTabToCausyne } from '../skybridgeBrowserBridge.js';
4
+ import { handleBrowserMcpRpc } from '../skybridgeBrowserMcp.js';
5
+ import { readJsonBody, sendJson } from './security.js';
6
+ const ALLOWED_INPUT_KINDS = new Set([
7
+ 'click', 'dblclick', 'contextmenu', 'mousedown', 'mousemove', 'mouseup', 'wheel', 'type', 'key', 'paste'
8
+ ]);
9
+ export async function handleBrowserRoutes(req, res, parsedUrl, ctx) {
10
+ const pathname = parsedUrl.pathname;
11
+ const { workspaceRoot, version } = ctx;
12
+ if (req.method === 'GET' && pathname === '/api/mcp/browser') {
13
+ res.statusCode = 405;
14
+ res.setHeader('Allow', 'POST');
15
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
16
+ res.end(JSON.stringify({ error: 'Use POST for Streamable HTTP MCP JSON-RPC requests.' }));
17
+ return true;
18
+ }
19
+ if (req.method === 'POST' && pathname === '/api/mcp/browser') {
20
+ const body = await readJsonBody(req);
21
+ const reply = await handleBrowserMcpRpc(body, {
22
+ workspaceRoot,
23
+ version,
24
+ onActivity: (activity) => {
25
+ if (activity.phase === 'start') {
26
+ ctx.setBrowserControlOwner('agent');
27
+ ctx.setBrowserAgentActivity({
28
+ active: true,
29
+ tool: activity.tool,
30
+ detail: activity.detail,
31
+ updatedAt: Date.now()
32
+ });
33
+ }
34
+ else {
35
+ ctx.setBrowserAgentActivity({ active: false, tool: '', detail: '', updatedAt: Date.now() });
36
+ }
37
+ ctx.broadcastBrowserNotice({
38
+ type: 'agent_activity',
39
+ controlOwner: ctx.getBrowserControlOwner(),
40
+ agentActivity: ctx.getBrowserAgentActivity()
41
+ });
42
+ }
43
+ });
44
+ if (reply === null) {
45
+ res.statusCode = 202;
46
+ res.end();
47
+ return true;
48
+ }
49
+ sendJson(res, 200, reply);
50
+ return true;
51
+ }
52
+ if (req.method === 'GET' && pathname === '/api/browser/state') {
53
+ const ensure = parsedUrl.searchParams.get('ensure') === '1';
54
+ const browser = causyneBrowser({ workspace: workspaceRoot });
55
+ const frame = await browser.captureLiveFrame({ ensureStarted: ensure });
56
+ sendJson(res, 200, {
57
+ ok: true,
58
+ controlOwner: ctx.getBrowserControlOwner(),
59
+ agentActivity: ctx.getBrowserAgentActivity(),
60
+ computerUsePolicy: 'tiered-approval',
61
+ ...frame
62
+ });
63
+ return true;
64
+ }
65
+ if (req.method === 'GET' && pathname === '/api/browser/stream') {
66
+ const browser = causyneBrowser({ workspace: workspaceRoot });
67
+ res.statusCode = 200;
68
+ res.setHeader('Content-Type', 'application/x-ndjson; charset=utf-8');
69
+ res.setHeader('Transfer-Encoding', 'chunked');
70
+ res.setHeader('X-Accel-Buffering', 'no');
71
+ const writeStreamLine = (payload) => {
72
+ if (res.writableEnded || res.destroyed)
73
+ return;
74
+ try {
75
+ res.write(`${JSON.stringify(payload)}\n`);
76
+ }
77
+ catch {
78
+ // Stream closed mid-write.
79
+ }
80
+ };
81
+ const unsub = browser.subscribeLiveStream((ev) => {
82
+ writeStreamLine({
83
+ ...ev,
84
+ controlOwner: ctx.getBrowserControlOwner(),
85
+ agentActivity: ctx.getBrowserAgentActivity(),
86
+ computerUsePolicy: 'tiered-approval'
87
+ });
88
+ });
89
+ ctx.browserStreamClients.add(writeStreamLine);
90
+ writeStreamLine({
91
+ type: 'agent_activity',
92
+ controlOwner: ctx.getBrowserControlOwner(),
93
+ agentActivity: ctx.getBrowserAgentActivity(),
94
+ computerUsePolicy: 'tiered-approval'
95
+ });
96
+ const keepAlive = setInterval(() => {
97
+ writeStreamLine({ type: 'ping', ts: Date.now() });
98
+ }, 15_000);
99
+ keepAlive.unref?.();
100
+ const cleanup = () => {
101
+ clearInterval(keepAlive);
102
+ ctx.browserStreamClients.delete(writeStreamLine);
103
+ unsub();
104
+ };
105
+ req.on('close', cleanup);
106
+ res.on('close', cleanup);
107
+ return true;
108
+ }
109
+ if (req.method === 'POST' && pathname === '/api/browser/input') {
110
+ const body = await readJsonBody(req);
111
+ const kind = typeof body.kind === 'string' ? body.kind.trim() : '';
112
+ if (!ALLOWED_INPUT_KINDS.has(kind)) {
113
+ sendJson(res, 400, { error: 'Unsupported live browser input kind.' });
114
+ return true;
115
+ }
116
+ ctx.setBrowserControlOwner('human');
117
+ const browser = causyneBrowser({ workspace: workspaceRoot });
118
+ const result = await browser.dispatchLiveInput({
119
+ kind: kind,
120
+ x: typeof body.x === 'number' ? body.x : undefined,
121
+ y: typeof body.y === 'number' ? body.y : undefined,
122
+ deltaX: typeof body.deltaX === 'number' ? body.deltaX : undefined,
123
+ deltaY: typeof body.deltaY === 'number' ? body.deltaY : undefined,
124
+ text: typeof body.text === 'string' ? body.text : undefined,
125
+ key: typeof body.key === 'string' ? body.key : undefined,
126
+ button: body.button === 'right' || body.button === 'middle' ? body.button : 'left',
127
+ skipFrame: true
128
+ });
129
+ sendJson(res, 200, { ok: result.ok, controlOwner: ctx.getBrowserControlOwner() });
130
+ return true;
131
+ }
132
+ if (req.method === 'POST' && pathname === '/api/browser/action') {
133
+ const body = await readJsonBody(req);
134
+ const action = typeof body.action === 'string' ? body.action.trim() : '';
135
+ const browser = causyneBrowser({ workspace: workspaceRoot });
136
+ if (action === 'setMode') {
137
+ ctx.setBrowserControlOwner('human');
138
+ const rawPref = typeof body.enginePreference === 'string' ? body.enginePreference.trim() : '';
139
+ const enginePreference = rawPref === 'camoufox'
140
+ || rawPref === 'chromium'
141
+ || rawPref === 'auto'
142
+ ? rawPref
143
+ : undefined;
144
+ const frame = await browser.setDisplayMode({
145
+ visible: typeof body.visible === 'boolean' ? body.visible : undefined,
146
+ enginePreference,
147
+ ensureStarted: body.ensureStarted === true
148
+ });
149
+ auditLog.log({
150
+ event: 'skybridge_browser_mode_switch',
151
+ visible: frame.visible,
152
+ engine: frame.engine,
153
+ enginePreference: frame.enginePreference
154
+ });
155
+ ctx.broadcastBrowserNotice({
156
+ type: 'meta',
157
+ controlOwner: ctx.getBrowserControlOwner(),
158
+ agentActivity: ctx.getBrowserAgentActivity(),
159
+ ...frame
160
+ });
161
+ sendJson(res, 200, {
162
+ ok: true,
163
+ controlOwner: ctx.getBrowserControlOwner(),
164
+ agentActivity: ctx.getBrowserAgentActivity(),
165
+ ...frame
166
+ });
167
+ return true;
168
+ }
169
+ if (action === 'takeControl') {
170
+ ctx.setBrowserControlOwner('human');
171
+ ctx.setBrowserAgentActivity({ active: false, tool: '', detail: '', updatedAt: Date.now() });
172
+ const frame = await browser.captureLiveFrame({ ensureStarted: false });
173
+ ctx.broadcastBrowserNotice({
174
+ type: 'agent_activity',
175
+ controlOwner: ctx.getBrowserControlOwner(),
176
+ agentActivity: ctx.getBrowserAgentActivity()
177
+ });
178
+ sendJson(res, 200, {
179
+ ok: true,
180
+ controlOwner: ctx.getBrowserControlOwner(),
181
+ agentActivity: ctx.getBrowserAgentActivity(),
182
+ ...frame
183
+ });
184
+ return true;
185
+ }
186
+ if (action === 'handToAgent') {
187
+ ctx.setBrowserControlOwner('agent');
188
+ const frame = await browser.captureLiveFrame({ ensureStarted: false });
189
+ ctx.broadcastBrowserNotice({
190
+ type: 'agent_activity',
191
+ controlOwner: ctx.getBrowserControlOwner(),
192
+ agentActivity: ctx.getBrowserAgentActivity()
193
+ });
194
+ sendJson(res, 200, {
195
+ ok: true,
196
+ controlOwner: ctx.getBrowserControlOwner(),
197
+ agentActivity: ctx.getBrowserAgentActivity(),
198
+ ...frame
199
+ });
200
+ return true;
201
+ }
202
+ if (action === 'start') {
203
+ ctx.setBrowserControlOwner('human');
204
+ let frame = await browser.captureLiveFrame({ ensureStarted: true });
205
+ const reqW = Number(body.width);
206
+ const reqH = Number(body.height);
207
+ if (frame.active && Number.isFinite(reqW) && Number.isFinite(reqH) && reqW >= 320 && reqH >= 240) {
208
+ const width = Math.max(320, Math.min(2560, Math.round(reqW)));
209
+ const height = Math.max(240, Math.min(1440, Math.round(reqH)));
210
+ try {
211
+ await browser.run([{ type: 'viewport', width, height }], { workspace: workspaceRoot });
212
+ frame = await browser.captureLiveFrame({ ensureStarted: false });
213
+ }
214
+ catch {
215
+ // Keep started frame if viewport resize raced with close.
216
+ }
217
+ }
218
+ sendJson(res, 200, { ok: true, controlOwner: ctx.getBrowserControlOwner(), ...frame });
219
+ return true;
220
+ }
221
+ if (action === 'close') {
222
+ const frame = await browser.closeLiveSession();
223
+ ctx.broadcastBrowserNotice({
224
+ type: 'closed',
225
+ controlOwner: ctx.getBrowserControlOwner(),
226
+ ...frame
227
+ });
228
+ sendJson(res, 200, { ok: true, controlOwner: ctx.getBrowserControlOwner(), ...frame });
229
+ return true;
230
+ }
231
+ if (action === 'goto' || action === 'newTab') {
232
+ ctx.setBrowserControlOwner('human');
233
+ let rawUrl = typeof body.url === 'string' ? body.url.trim() : '';
234
+ if (rawUrl.startsWith('@')) {
235
+ const idx = rawUrl.indexOf(' ');
236
+ const macro = idx === -1 ? rawUrl : rawUrl.slice(0, idx);
237
+ const q = idx === -1 ? '' : rawUrl.slice(idx + 1).trim();
238
+ const expanded = expandSearchMacro(macro, q);
239
+ if (!expanded) {
240
+ sendJson(res, 400, { error: 'Unsupported browser search macro.' });
241
+ return true;
242
+ }
243
+ rawUrl = expanded;
244
+ }
245
+ else if (rawUrl && !/^https?:\/\//i.test(rawUrl)) {
246
+ rawUrl = rawUrl.includes('.') || rawUrl.startsWith('localhost')
247
+ ? `https://${rawUrl}`
248
+ : (expandSearchMacro('@duckduckgo_search', rawUrl) || `https://duckduckgo.com/?q=${encodeURIComponent(rawUrl)}`);
249
+ }
250
+ const actions = [
251
+ action === 'newTab'
252
+ ? { type: 'newTab', ...(rawUrl ? { url: rawUrl } : {}) }
253
+ : { type: 'goto', url: rawUrl || 'about:blank' }
254
+ ];
255
+ const reqW = Number(body.width);
256
+ const reqH = Number(body.height);
257
+ if (Number.isFinite(reqW) && Number.isFinite(reqH) && reqW >= 320 && reqH >= 240) {
258
+ actions.push({
259
+ type: 'viewport',
260
+ width: Math.max(320, Math.min(2560, Math.round(reqW))),
261
+ height: Math.max(240, Math.min(1440, Math.round(reqH)))
262
+ });
263
+ }
264
+ await browser.run(actions, { workspace: workspaceRoot });
265
+ const frame = await browser.captureLiveFrame({ ensureStarted: true });
266
+ sendJson(res, 200, { ok: true, controlOwner: ctx.getBrowserControlOwner(), ...frame });
267
+ return true;
268
+ }
269
+ if (action === 'back' || action === 'forward' || action === 'reload') {
270
+ ctx.setBrowserControlOwner('human');
271
+ await browser.run([{ type: action }], { workspace: workspaceRoot });
272
+ const frame = await browser.captureLiveFrame({ ensureStarted: true });
273
+ sendJson(res, 200, { ok: true, controlOwner: ctx.getBrowserControlOwner(), ...frame });
274
+ return true;
275
+ }
276
+ if (action === 'switchTab' && typeof body.tabId === 'string' && body.tabId.trim()) {
277
+ ctx.setBrowserControlOwner('human');
278
+ await browser.run([{ type: 'switchTab', tabId: body.tabId.trim() }], { workspace: workspaceRoot });
279
+ const frame = await browser.captureLiveFrame({ ensureStarted: true });
280
+ sendJson(res, 200, { ok: true, controlOwner: ctx.getBrowserControlOwner(), ...frame });
281
+ return true;
282
+ }
283
+ if (action === 'closeTab' && typeof body.tabId === 'string' && body.tabId.trim()) {
284
+ ctx.setBrowserControlOwner('human');
285
+ const frame = await browser.closeLiveTab(body.tabId.trim());
286
+ if (!frame.active) {
287
+ ctx.broadcastBrowserNotice({
288
+ type: 'closed',
289
+ controlOwner: ctx.getBrowserControlOwner(),
290
+ ...frame
291
+ });
292
+ }
293
+ sendJson(res, 200, { ok: true, controlOwner: ctx.getBrowserControlOwner(), ...frame });
294
+ return true;
295
+ }
296
+ if (action === 'viewport') {
297
+ const pre = await browser.captureLiveFrame({ ensureStarted: false });
298
+ if (!pre.active) {
299
+ sendJson(res, 200, { ok: true, controlOwner: ctx.getBrowserControlOwner(), ...pre });
300
+ return true;
301
+ }
302
+ const width = Math.max(320, Math.min(2560, Math.round(Number(body.width) || 1280)));
303
+ const height = Math.max(240, Math.min(1440, Math.round(Number(body.height) || 800)));
304
+ try {
305
+ await browser.run([{ type: 'viewport', width, height }], { workspace: workspaceRoot });
306
+ }
307
+ catch {
308
+ // Browser closed concurrently during resize; return current closed state cleanly.
309
+ }
310
+ const frame = await browser.captureLiveFrame({ ensureStarted: false });
311
+ sendJson(res, 200, { ok: true, controlOwner: ctx.getBrowserControlOwner(), ...frame });
312
+ return true;
313
+ }
314
+ if (action === 'snapshot') {
315
+ const snap = await browser.snapshot(0);
316
+ const frame = await browser.captureLiveFrame({ ensureStarted: true });
317
+ sendJson(res, 200, {
318
+ ok: true,
319
+ controlOwner: ctx.getBrowserControlOwner(),
320
+ snapshot: snap.text,
321
+ refsCount: snap.refsCount,
322
+ ...frame
323
+ });
324
+ return true;
325
+ }
326
+ if (ALLOWED_INPUT_KINDS.has(action)) {
327
+ ctx.setBrowserControlOwner('human');
328
+ const frame = await browser.interactAt({
329
+ kind: action,
330
+ x: typeof body.x === 'number' ? body.x : undefined,
331
+ y: typeof body.y === 'number' ? body.y : undefined,
332
+ deltaX: typeof body.deltaX === 'number' ? body.deltaX : undefined,
333
+ deltaY: typeof body.deltaY === 'number' ? body.deltaY : undefined,
334
+ text: typeof body.text === 'string' ? body.text : undefined,
335
+ key: typeof body.key === 'string' ? body.key : undefined,
336
+ button: body.button === 'right' || body.button === 'middle' ? body.button : 'left'
337
+ });
338
+ sendJson(res, 200, {
339
+ ok: true,
340
+ controlOwner: ctx.getBrowserControlOwner(),
341
+ agentActivity: ctx.getBrowserAgentActivity(),
342
+ ...frame
343
+ });
344
+ return true;
345
+ }
346
+ sendJson(res, 400, { error: 'Unsupported shared browser action.' });
347
+ return true;
348
+ }
349
+ if (req.method === 'GET' && pathname === '/api/browser/cdp/tabs') {
350
+ const portParam = parsedUrl.searchParams.get('port') || '9222';
351
+ const discovered = await discoverLocalCdpTabs(portParam);
352
+ sendJson(res, 200, {
353
+ ok: true,
354
+ port: discovered.port,
355
+ tabs: discovered.tabs.map((t) => ({ id: t.id, title: t.title, url: t.url }))
356
+ });
357
+ return true;
358
+ }
359
+ if (req.method === 'POST' && pathname === '/api/browser/cdp/sync') {
360
+ const body = await readJsonBody(req);
361
+ const targetId = typeof body.targetId === 'string' ? body.targetId.trim() : '';
362
+ if (!targetId) {
363
+ sendJson(res, 400, { error: 'targetId is required to sync a local browser tab.' });
364
+ return true;
365
+ }
366
+ const synced = await syncLocalCdpTabToCausyne({
367
+ workspaceRoot,
368
+ port: body.port ?? 9222,
369
+ targetId,
370
+ includeCookies: body.includeCookies === true
371
+ });
372
+ ctx.setBrowserControlOwner('human');
373
+ auditLog.log({
374
+ event: 'skybridge_browser_cdp_sync',
375
+ importedCookies: synced.importedCookies
376
+ });
377
+ sendJson(res, 200, {
378
+ ok: true,
379
+ controlOwner: ctx.getBrowserControlOwner(),
380
+ syncedUrl: synced.syncedUrl,
381
+ importedCookies: synced.importedCookies,
382
+ ...synced.frame
383
+ });
384
+ return true;
385
+ }
386
+ if (req.method === 'POST' && pathname === '/api/browser/import') {
387
+ const body = await readJsonBody(req);
388
+ const rawText = typeof body.rawText === 'string' ? body.rawText : '';
389
+ const summary = await importExplicitUserBrowserData({
390
+ workspaceRoot,
391
+ rawText,
392
+ openFirstUrl: body.openFirstUrl !== false
393
+ });
394
+ const frame = await causyneBrowser({ workspace: workspaceRoot }).captureLiveFrame({ ensureStarted: false });
395
+ auditLog.log({
396
+ event: 'skybridge_browser_import',
397
+ importedCookies: summary.importedCookies,
398
+ urlsCount: summary.importedUrls.length
399
+ });
400
+ sendJson(res, 200, {
401
+ ok: true,
402
+ controlOwner: ctx.getBrowserControlOwner(),
403
+ ...summary,
404
+ ...frame
405
+ });
406
+ return true;
407
+ }
408
+ return false;
409
+ }