farming-code 2.2.29 → 2.2.32
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.
- package/README.md +25 -66
- package/README.zh_cn.md +15 -66
- package/THIRD_PARTY_NOTICES.md +7 -0
- package/backend/acp/{client-services.js → client-services.cjs} +132 -134
- package/backend/acp/packaged-claude-acp.cjs +37 -0
- package/backend/acp/packaged-codex-acp.cjs +37 -0
- package/backend/acp/patch-decisions.cjs +182 -0
- package/backend/acp/permission-security.cjs +131 -0
- package/backend/{acp-checkpoint-store.js → acp-checkpoint-store.cjs} +90 -59
- package/backend/{acp-runtime.js → acp-runtime.cjs} +1219 -1125
- package/backend/acp-session-state.cjs +622 -0
- package/backend/acp-transcript.cjs +525 -0
- package/backend/agent-env.cjs +257 -0
- package/backend/agent-extension-discovery.cjs +452 -0
- package/backend/agent-extension-inventory.cjs +116 -0
- package/backend/agent-json-stream.cjs +283 -0
- package/backend/agent-lifecycle-journal.cjs +185 -0
- package/backend/{agent-manager.js → agent-manager.cjs} +3116 -4275
- package/backend/agent-order-transaction.cjs +85 -0
- package/backend/agent-order.cjs +159 -0
- package/backend/agent-provider-session.cjs +98 -0
- package/backend/agent-runtime-binding.cjs +179 -0
- package/backend/agent-session-history.cjs +1022 -0
- package/backend/agent-session-inventory.cjs +291 -0
- package/backend/async-cache.cjs +104 -0
- package/backend/atomic-json-store.cjs +67 -0
- package/backend/auth.cjs +261 -0
- package/backend/authoritative-inventory-cache.cjs +383 -0
- package/backend/business-health.cjs +68 -0
- package/backend/chat-runtime.cjs +50 -0
- package/backend/claude-settings.cjs +132 -0
- package/backend/cli-agents.cjs +435 -0
- package/backend/codex-context-window.cjs +196 -0
- package/backend/codex-models.cjs +228 -0
- package/backend/codex-session-archive.cjs +82 -0
- package/backend/codex-session-history.cjs +590 -0
- package/backend/{codex-terminal-profile.js → codex-terminal-profile.cjs} +149 -155
- package/backend/codex-transcript-sanitizer.cjs +130 -0
- package/backend/codex-transcript.cjs +2107 -0
- package/backend/command-runner-child.cjs +105 -0
- package/backend/config-manager.cjs +1263 -0
- package/backend/control-api.cjs +439 -0
- package/backend/data/LICENSE.cua +21 -0
- package/backend/executable-discovery.cjs +204 -0
- package/backend/farming-agent-bootstrap.cjs +81 -0
- package/backend/farming-agent-bootstrap.zh_cn.md +6 -2
- package/backend/{farming-app-cli.js → farming-app-cli.cjs} +441 -493
- package/backend/farming-cli.cjs +414 -0
- package/backend/farming-net-pass.cjs +319 -0
- package/backend/farming-net-registry.cjs +123 -0
- package/backend/farming-net-server.cjs +309 -0
- package/backend/farming-session-store.cjs +651 -0
- package/backend/git-worktree-info.cjs +181 -0
- package/backend/haiku-token.cjs +828 -0
- package/backend/index-html.cjs +96 -0
- package/backend/input-parts.cjs +68 -0
- package/backend/input-routing.cjs +40 -0
- package/backend/{local-session-engine.js → local-session-engine.cjs} +276 -282
- package/backend/main-agent-skills.cjs +309 -0
- package/backend/main-page-session.cjs +132 -0
- package/backend/native-pty-controller-generation.cjs +139 -0
- package/backend/{native-pty-host-client.js → native-pty-host-client.cjs} +338 -346
- package/backend/native-pty-host-identity.cjs +125 -0
- package/backend/native-pty-host-path.cjs +68 -0
- package/backend/{native-pty-host.js → native-pty-host.cjs} +480 -569
- package/backend/native-session-engine.cjs +296 -0
- package/backend/network.cjs +57 -0
- package/backend/npm-update-helper.cjs +399 -0
- package/backend/packaged-node-pty.cjs +120 -0
- package/backend/preview-session-manager.cjs +89 -0
- package/backend/provider-adapters.cjs +492 -0
- package/backend/provider-home-configuration.cjs +211 -0
- package/backend/provider-session-id.cjs +79 -0
- package/backend/provider-session-service.cjs +333 -0
- package/backend/qr-share-tickets.cjs +119 -0
- package/backend/resource-broadcast-protocol.cjs +50 -0
- package/backend/review-diff-router.cjs +202 -0
- package/backend/review-diff-service.cjs +1267 -0
- package/backend/review-session-router.cjs +93 -0
- package/backend/review-session-service.cjs +470 -0
- package/backend/review-session-store.cjs +161 -0
- package/backend/review-state-router.cjs +132 -0
- package/backend/review-state-store.cjs +299 -0
- package/backend/run-history-store.cjs +97 -0
- package/backend/runtime-dependency-manager.cjs +893 -0
- package/backend/runtime-executable-invocation.cjs +37 -0
- package/backend/runtime-observation.cjs +96 -0
- package/backend/server-process-identity.cjs +70 -0
- package/backend/server.cjs +3356 -0
- package/backend/session-engine-bridge.cjs +144 -0
- package/backend/session-engine-router.cjs +105 -0
- package/backend/session-engine.cjs +64 -0
- package/backend/session-stream-protocol.cjs +181 -0
- package/backend/shell-busy-integration.cjs +442 -0
- package/backend/slash-command-discovery.cjs +423 -0
- package/backend/storage-layout.cjs +263 -0
- package/backend/system-monitor.cjs +96 -0
- package/backend/terminal-attach-checkpoint.cjs +73 -0
- package/backend/terminal-exit-quiescence.cjs +57 -0
- package/backend/{terminal-reducer-flow-control.js → terminal-reducer-flow-control.cjs} +45 -22
- package/backend/terminal-runtime-cleanup.cjs +99 -0
- package/backend/{terminal-screen-state.js → terminal-screen-state.cjs} +100 -108
- package/backend/{terminal-screen-worker-pool.js → terminal-screen-worker-pool.cjs} +89 -79
- package/backend/{terminal-screen-worker-thread.js → terminal-screen-worker-thread.cjs} +64 -65
- package/backend/terminal-screen-worker.cjs +352 -0
- package/backend/terminal-state-serialization.cjs +147 -0
- package/backend/{terminal-status.js → terminal-status.cjs} +137 -187
- package/backend/theme-manager.cjs +185 -0
- package/backend/update-service.cjs +675 -0
- package/backend/usage-forecast.cjs +95 -0
- package/backend/{usage-history-client.js → usage-history-client.cjs} +144 -94
- package/backend/usage-history-smoke.cjs +85 -0
- package/backend/usage-history-worker.cjs +60 -0
- package/backend/{usage-monitor.js → usage-monitor.cjs} +363 -390
- package/backend/workspace-directory.cjs +239 -0
- package/backend/workspace-discovery.cjs +271 -0
- package/backend/{workspace-file-router.js → workspace-file-router.cjs} +213 -282
- package/backend/{workspace-file-service.js → workspace-file-service.cjs} +1139 -1318
- package/backend/workspace-root-registry.cjs +192 -0
- package/bin/farming +1 -1
- package/config/farming.deploy.env.example +1 -1
- package/config/farming.install.env.example +1 -1
- package/dist/acp/codex-acp-1.1.4.mjs +405 -8
- package/dist/assets/App-BCkdpWB6.css +1 -0
- package/dist/assets/App-CRjKpaGp.js +697 -0
- package/dist/assets/CodeSelect-Ci8sKZPU.js +1 -0
- package/dist/assets/CodeSelect-T2eNzrOc.css +1 -0
- package/dist/assets/{FileEditorMarkdownPreview-Cs9cLv8a.js → FileEditorMarkdownPreview-V06QHlAg.js} +55 -55
- package/dist/assets/FileEditorPane-BX-Wjg9U.js +2 -0
- package/dist/assets/IconGlyphs-YhV7tqd4.js +1 -0
- package/dist/assets/ProjectFilesSection-BDT2_r0o.js +12 -0
- package/dist/assets/ReviewPage-BqAs5sG1.js +3 -0
- package/dist/assets/code-dark-B7eM_9G9.css +1 -0
- package/dist/assets/{cssMode-Bdg5RpcJ.js → cssMode-ADdAlxTO.js} +1 -1
- package/dist/assets/{editor.api2-D74ufkbr.js → editor.api2-BFLAr0YF.js} +162 -162
- package/dist/assets/file-icons-BpeqWkIB.js +1 -0
- package/dist/assets/{freemarker2-DfMDJfnE.js → freemarker2-C90q5jrp.js} +1 -1
- package/dist/assets/{handlebars-BEdPtzMT.js → handlebars-BwUQcEDq.js} +1 -1
- package/dist/assets/{html-B1fPb1QD.js → html-1s3uPKfh.js} +1 -1
- package/dist/assets/{htmlMode-CY2ZjHxH.js → htmlMode-xtKD99de.js} +1 -1
- package/dist/assets/{index-CQNaZc3l.js → index-C8MchCem.js} +3 -3
- package/dist/assets/{javascript-BV2OsWA0.js → javascript-Ph7ijyny.js} +1 -1
- package/dist/assets/{jsonMode-zd07aeuX.js → jsonMode-D-jGAnFH.js} +1 -1
- package/dist/assets/linkComputer-CbGyI_qx.js +1 -0
- package/dist/assets/{liquid-BParI24X.js → liquid-0IamE_cI.js} +1 -1
- package/dist/assets/{lspLanguageFeatures-BzredkoV.js → lspLanguageFeatures-BxnQunWF.js} +1 -1
- package/dist/assets/main-CLKXa6hf.css +1 -0
- package/dist/assets/{mdx-vGzUJN98.js → mdx-DdNmfivn.js} +1 -1
- package/dist/assets/{monaco.contribution-BmaeBQEc.js → monaco.contribution-B43jsUs1.js} +2 -2
- package/dist/assets/{python-CUwdLgsI.js → python-IAt4X5TS.js} +1 -1
- package/dist/assets/{razor-BScebUxU.js → razor-DRkFYW0S.js} +1 -1
- package/dist/assets/review-C8-oPKXk.css +1 -0
- package/dist/assets/{tsMode-DzUAibXK.js → tsMode-_d_4U2gf.js} +1 -1
- package/dist/assets/{typescript-D9EOeM7N.js → typescript-C7zcMq9l.js} +1 -1
- package/dist/assets/{workers-DAiomYZM.js → workers-D0nOyJzf.js} +1 -1
- package/dist/assets/workspace-editor-model-DND_Y-Bj.js +1 -0
- package/dist/assets/workspace-editor-monaco-BPWhgcIB.js +4 -0
- package/dist/assets/workspace-editor-monaco-N641qPjX.js +1 -0
- package/dist/assets/workspace-view-state-k1bTlJ_Z.js +13 -0
- package/dist/assets/{xml-wWl1dhkQ.js → xml-BR-HvxT8.js} +1 -1
- package/dist/assets/{yaml-Dc2WDaOZ.js → yaml-YtFQwh4q.js} +1 -1
- package/dist/index.html +2 -1
- package/extensions/browser/backend/agent-browser-runtime.cjs +1040 -0
- package/extensions/browser/backend/agent-capability.cjs +85 -0
- package/extensions/browser/backend/browser-mcp-server.cjs +655 -0
- package/extensions/browser/backend/browser-resource-manager.cjs +1486 -0
- package/extensions/browser/backend/browser-resource-store.cjs +228 -0
- package/extensions/browser/backend/browser-router.cjs +203 -0
- package/extensions/browser/backend/executable-discovery.cjs +248 -0
- package/extensions/browser/backend/farming-browser-client.cjs +116 -0
- package/extensions/browser/backend/index.cjs +32 -0
- package/extensions/browser/backend/managed-chromium-installer.cjs +833 -0
- package/extensions/browser/bin/farming-browser +16 -7
- package/extensions/computer/backend/agent-capability.cjs +72 -0
- package/extensions/computer/backend/computer-constants.cjs +59 -0
- package/extensions/computer/backend/computer-mcp-server.cjs +182 -0
- package/extensions/computer/backend/computer-resource-manager.cjs +1219 -0
- package/extensions/computer/backend/computer-resource-store.cjs +190 -0
- package/extensions/computer/backend/computer-router.cjs +257 -0
- package/extensions/computer/backend/cua-tools.json +3067 -0
- package/extensions/computer/backend/farming-computer-client.cjs +84 -0
- package/extensions/computer/backend/index.cjs +35 -0
- package/extensions/computer/backend/isolated-browser-provider.cjs +157 -0
- package/extensions/computer/bin/farming-computer +131 -0
- package/frontend/farming-net/app.js +255 -262
- package/frontend/ghostty-loader.js +24 -20
- package/frontend/reading-anchor.js +210 -193
- package/frontend/runtime-paths.js +24 -26
- package/frontend/session-bridge.js +90 -111
- package/frontend/session-modal-bridge.js +184 -302
- package/frontend/skin-bridge.js +20 -23
- package/frontend/skins/crt/app.js +7686 -7901
- package/frontend/skins/crt/effects.js +7 -6
- package/frontend/skins/crt/index.html +6 -6
- package/frontend/skins/crt/styles/markdown.css +4 -4
- package/frontend/terminal-bridge.js +174 -181
- package/frontend/terminal-replay.js +320 -345
- package/frontend/theme-loader.js +55 -71
- package/index.html +1 -0
- package/package.json +64 -37
- package/shared/backend-connection-status.js +38 -69
- package/shared/browser-protocol.js +235 -110
- package/shared/websocket-liveness.js +40 -0
- package/backend/acp/packaged-claude-acp.js +0 -13
- package/backend/acp/packaged-codex-acp.js +0 -13
- package/backend/acp/patch-decisions.js +0 -153
- package/backend/acp/permission-security.js +0 -108
- package/backend/acp-session-state.js +0 -644
- package/backend/acp-transcript.js +0 -512
- package/backend/agent-env.js +0 -250
- package/backend/agent-json-stream.js +0 -268
- package/backend/agent-lifecycle-journal.js +0 -133
- package/backend/agent-memory-report.js +0 -583
- package/backend/agent-order.js +0 -166
- package/backend/agent-provider-session.js +0 -76
- package/backend/agent-runtime-binding.js +0 -174
- package/backend/agent-session-history.js +0 -953
- package/backend/async-cache.js +0 -94
- package/backend/atomic-json-store.js +0 -37
- package/backend/auth.js +0 -251
- package/backend/bundle-update-helper.js +0 -113
- package/backend/chat-runtime.js +0 -28
- package/backend/claude-settings.js +0 -121
- package/backend/cli-agents.js +0 -486
- package/backend/codex-context-window.js +0 -189
- package/backend/codex-models.js +0 -226
- package/backend/codex-rollout-follower.js +0 -326
- package/backend/codex-session-archive.js +0 -65
- package/backend/codex-session-history.js +0 -647
- package/backend/codex-transcript-sanitizer.js +0 -115
- package/backend/codex-transcript.js +0 -2564
- package/backend/command-runner-child.js +0 -67
- package/backend/config-manager.js +0 -1133
- package/backend/control-api.js +0 -455
- package/backend/executable-discovery.js +0 -216
- package/backend/farming-agent-bootstrap.js +0 -49
- package/backend/farming-cli.js +0 -457
- package/backend/farming-net-pass.js +0 -285
- package/backend/farming-net-registry.js +0 -112
- package/backend/farming-net-server.js +0 -298
- package/backend/farming-session-store.js +0 -677
- package/backend/git-worktree-info.js +0 -181
- package/backend/haiku-token.js +0 -772
- package/backend/index-html.js +0 -62
- package/backend/input-parts.js +0 -54
- package/backend/input-routing.js +0 -24
- package/backend/json-cli-runtime.js +0 -275
- package/backend/main-agent-skills.js +0 -305
- package/backend/main-page-session.js +0 -122
- package/backend/native-pty-controller-generation.js +0 -106
- package/backend/native-pty-host-identity.js +0 -86
- package/backend/native-pty-host-path.js +0 -42
- package/backend/native-session-engine.js +0 -268
- package/backend/network.js +0 -23
- package/backend/npm-update-helper.js +0 -374
- package/backend/packaged-node-pty.js +0 -99
- package/backend/preview-session-manager.js +0 -68
- package/backend/provider-adapters.js +0 -399
- package/backend/provider-session-id.js +0 -47
- package/backend/provider-session-service.js +0 -345
- package/backend/qr-share-tickets.js +0 -100
- package/backend/review-diff-router.js +0 -180
- package/backend/review-diff-service.js +0 -1270
- package/backend/review-session-router.js +0 -74
- package/backend/review-session-service.js +0 -446
- package/backend/review-session-store.js +0 -151
- package/backend/review-state-router.js +0 -116
- package/backend/review-state-store.js +0 -286
- package/backend/run-history-store.js +0 -67
- package/backend/runtime-dependency-manager.js +0 -759
- package/backend/runtime-executable-invocation.js +0 -20
- package/backend/runtime-observation.js +0 -81
- package/backend/server-process-identity.js +0 -47
- package/backend/server.js +0 -3267
- package/backend/session-engine-bridge.js +0 -129
- package/backend/session-engine-router.js +0 -66
- package/backend/session-engine.js +0 -39
- package/backend/session-stream-protocol.js +0 -185
- package/backend/shell-busy-integration.js +0 -472
- package/backend/slash-command-discovery.js +0 -433
- package/backend/storage-layout.js +0 -190
- package/backend/system-monitor.js +0 -77
- package/backend/terminal-attach-checkpoint.js +0 -74
- package/backend/terminal-exit-quiescence.js +0 -39
- package/backend/terminal-runtime-cleanup.js +0 -80
- package/backend/terminal-screen-worker.js +0 -337
- package/backend/terminal-state-serialization.js +0 -127
- package/backend/theme-manager.js +0 -163
- package/backend/update-service.js +0 -1545
- package/backend/usage-forecast.js +0 -86
- package/backend/usage-history-scanner.ts +0 -2718
- package/backend/usage-history-smoke.js +0 -64
- package/backend/usage-history-worker.js +0 -34
- package/backend/workspace-directory.js +0 -232
- package/backend/workspace-discovery.js +0 -311
- package/backend/workspace-root-registry.js +0 -166
- package/dist/assets/App-BKNUILuL.css +0 -1
- package/dist/assets/App-Ex2oVVXO.js +0 -693
- package/dist/assets/FileEditorPane-CNtuGjQ3.js +0 -2
- package/dist/assets/IconGlyphs-BClgag6E.js +0 -1
- package/dist/assets/ProjectFilesSection-Cf2BEcAN.js +0 -12
- package/dist/assets/ReviewPage-BRuxFaH5.js +0 -3
- package/dist/assets/code-dark-BEZTQwYZ.css +0 -1
- package/dist/assets/file-icons-BOlcJ0ZF.js +0 -1
- package/dist/assets/main-DK9Sy7-c.css +0 -1
- package/dist/assets/review-BcnwQtJv.css +0 -1
- package/dist/assets/workspace-editor-model-CSGCl21x.js +0 -1
- package/dist/assets/workspace-editor-monaco-CJNwbpCy.js +0 -4
- package/dist/assets/workspace-editor-monaco-CO9w8YV0.js +0 -1
- package/dist/assets/workspace-view-state-BjKPYp0E.js +0 -7
- package/extensions/browser/backend/agent-browser-runtime.js +0 -1041
- package/extensions/browser/backend/agent-capability.js +0 -55
- package/extensions/browser/backend/browser-mcp-server.js +0 -621
- package/extensions/browser/backend/browser-resource-manager.js +0 -1309
- package/extensions/browser/backend/browser-resource-store.js +0 -190
- package/extensions/browser/backend/browser-router.js +0 -133
- package/extensions/browser/backend/executable-discovery.js +0 -224
- package/extensions/browser/backend/farming-browser-client.js +0 -83
- package/extensions/browser/backend/index.js +0 -7
- package/extensions/browser/frontend/BrowserSidebarPortals.tsx +0 -360
- package/extensions/browser/frontend/BrowserViewer.tsx +0 -500
- package/extensions/browser/frontend/browser-resource-state.ts +0 -60
- package/extensions/browser/frontend/browser.css +0 -567
- package/extensions/browser/frontend/types.ts +0 -31
- package/extensions/browser/frontend/useBrowserResources.ts +0 -158
- package/frontend/vendor/ghostty-web/index.d.ts +0 -1842
- package/shared/backend-connection-status.d.ts +0 -37
- package/shared/browser-protocol.d.ts +0 -5
- /package/dist/assets/{api-w2x5QfxB.js → api-ByCV9MEr.js} +0 -0
- /package/dist/assets/{core-vPkV8P_m.js → core-Czwdx12k.js} +0 -0
- /package/dist/assets/{fsharp-DNZNZyEE.js → fsharp-BjPpkoJe.js} +0 -0
- /package/dist/assets/{go-_kPJjNc_.js → go-BkIDmSUP.js} +0 -0
- /package/dist/assets/{graphql-4HCNVt5g.js → graphql-CCJTpqi1.js} +0 -0
- /package/dist/assets/{hcl-C87ymY0o.js → hcl-s4z8lUFg.js} +0 -0
- /package/dist/assets/{ini-BYrrGNTc.js → ini-CyUW1I7Z.js} +0 -0
- /package/dist/assets/{java-B211rJbk.js → java-CFCcGRiM.js} +0 -0
- /package/dist/assets/{julia-DVPsRtBm.js → julia-DQyH_EYb.js} +0 -0
- /package/dist/assets/{kotlin-BYyNerjp.js → kotlin-B8hA5O51.js} +0 -0
- /package/dist/assets/{less-EC9UblGE.js → less-DaVdaas0.js} +0 -0
- /package/dist/assets/{lexon-B1yuUdqW.js → lexon-DLBhPd3y.js} +0 -0
- /package/dist/assets/{lua-Bo6GFzxT.js → lua-gB1c_cif.js} +0 -0
- /package/dist/assets/{m3-C-Y9cBVI.js → m3-C2T3z3gB.js} +0 -0
- /package/dist/assets/{markdown-BHu-wLbe.js → markdown-CKq-Euj1.js} +0 -0
- /package/dist/assets/{mips-Dkds26rX.js → mips-Bcs5qIDX.js} +0 -0
- /package/dist/assets/{msdax-PU4g-8_g.js → msdax-CBP48FHF.js} +0 -0
- /package/dist/assets/{mysql-fPpuGpS7.js → mysql-c7tiPcFj.js} +0 -0
- /package/dist/assets/{objective-c-Wy1e5JUf.js → objective-c-D4BZkeEV.js} +0 -0
- /package/dist/assets/{pascal-BIXwvrqU.js → pascal-CJDZg0MN.js} +0 -0
- /package/dist/assets/{pascaligo-DwR0AtL_.js → pascaligo-Bygi8Ynl.js} +0 -0
- /package/dist/assets/{perl-CGHAl8my.js → perl-aS8bkf7E.js} +0 -0
- /package/dist/assets/{pgsql-B91M73E2.js → pgsql-CzNAYFYl.js} +0 -0
- /package/dist/assets/{php-C70ttrCx.js → php-DOtoEFTT.js} +0 -0
- /package/dist/assets/{pla-BNrXaRh0.js → pla-DugRnao1.js} +0 -0
- /package/dist/assets/{postiats-Dv2IAZR9.js → postiats-DsXwusKh.js} +0 -0
- /package/dist/assets/{powerquery-CkY5Wq7J.js → powerquery-DL3o72oD.js} +0 -0
- /package/dist/assets/{powershell-uGIYLU3C.js → powershell-C1qHekys.js} +0 -0
- /package/dist/assets/{protobuf-CavfmJu-.js → protobuf-DHgsc-cH.js} +0 -0
- /package/dist/assets/{pug-Drqnyitx.js → pug-FZXlE_Iu.js} +0 -0
- /package/dist/assets/{qrcode-7uGsTEmi.js → qrcode-DJDTdjL7.js} +0 -0
- /package/dist/assets/{qsharp-C7yF-Dti.js → qsharp-bLyhI3DT.js} +0 -0
- /package/dist/assets/{r-HXLGP0_-.js → r-DjFGXbAa.js} +0 -0
- /package/dist/assets/{redis-C4y6mf2p.js → redis-DSoZ_V6u.js} +0 -0
- /package/dist/assets/{redshift-DdQrFPrv.js → redshift-CzkdCsrg.js} +0 -0
- /package/dist/assets/{restructuredtext-CBrf2PNj.js → restructuredtext-B_XPrGCQ.js} +0 -0
- /package/dist/assets/{ruby-CVtMoX2B.js → ruby-Cry2RneJ.js} +0 -0
- /package/dist/assets/{rust-Ckq4sbMP.js → rust-CWzdbFq1.js} +0 -0
- /package/dist/assets/{sb-er66OpE3.js → sb-DmofU59h.js} +0 -0
- /package/dist/assets/{scala-BdWZz-hH.js → scala-BUC2l-vN.js} +0 -0
- /package/dist/assets/{scheme-kTV_s2kP.js → scheme-CNIB_72f.js} +0 -0
- /package/dist/assets/{scss-Bymy2VJQ.js → scss-DcL4-NbX.js} +0 -0
- /package/dist/assets/{shell-bwEP1LQ1.js → shell-CgAGvSSA.js} +0 -0
- /package/dist/assets/{solidity-DCddeBgL.js → solidity-DSkt6O_P.js} +0 -0
- /package/dist/assets/{sophia-BMN3AQig.js → sophia--5ytcjDN.js} +0 -0
- /package/dist/assets/{sparql-CSXOLjzi.js → sparql-C1Srr7cD.js} +0 -0
- /package/dist/assets/{sql-COg8ewmK.js → sql-2k_O5fj7.js} +0 -0
- /package/dist/assets/{st-CIaZtBS_.js → st-DCCCPqfz.js} +0 -0
- /package/dist/assets/{swift-DXiOhUHu.js → swift-Dd8mAfOt.js} +0 -0
- /package/dist/assets/{systemverilog-BTDE6Dr2.js → systemverilog-B0-aZYXu.js} +0 -0
- /package/dist/assets/{tcl-B0zy9NvV.js → tcl-CZAZwPVX.js} +0 -0
- /package/dist/assets/{twig-D594l4Z1.js → twig-D-Ag0tdS.js} +0 -0
- /package/dist/assets/{typespec-XKVjaPqo.js → typespec-B7kTFp0Z.js} +0 -0
- /package/dist/assets/{useWorkspaceMenuKeyboard-Bpy_s69Z.js → useWorkspaceMenuKeyboard-uYZpDxhU.js} +0 -0
- /package/dist/assets/{vb-B2wYoKRV.js → vb-20w1JPum.js} +0 -0
- /package/dist/assets/{wgsl-D0Yf2LbK.js → wgsl-CLJfqFt0.js} +0 -0
- /package/dist/assets/{workspace-working-copy-C3FSR3N6.js → workspace-working-copy-Cfz07cOd.js} +0 -0
|
@@ -0,0 +1,1486 @@
|
|
|
1
|
+
// Generated by scripts/build-backend-runtime.ts. Do not edit.
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var browser_resource_manager_exports = {};
|
|
31
|
+
__export(browser_resource_manager_exports, {
|
|
32
|
+
BrowserResourceManager: () => BrowserResourceManager,
|
|
33
|
+
browserError: () => browserError,
|
|
34
|
+
externalBrowserFailure: () => externalBrowserFailure,
|
|
35
|
+
normalizeUrl: () => normalizeUrl
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(browser_resource_manager_exports);
|
|
38
|
+
var storageLayout = __toESM(require("../../../backend/storage-layout.cjs"));
|
|
39
|
+
var import_server_process_identity = require("../../../backend/server-process-identity.cjs");
|
|
40
|
+
var import_browser_resource_store = require("./browser-resource-store.cjs");
|
|
41
|
+
var import_agent_browser_runtime = require("./agent-browser-runtime.cjs");
|
|
42
|
+
var import_executable_discovery = require("./executable-discovery.cjs");
|
|
43
|
+
const fs = require("fs");
|
|
44
|
+
const path = require("path");
|
|
45
|
+
const crypto = require("crypto");
|
|
46
|
+
const { EventEmitter } = require("events");
|
|
47
|
+
const MAX_VIEWER_BUFFER_BYTES = 2 * 1024 * 1024;
|
|
48
|
+
const VIEWER_RESIZE_SETTLE_MS = 80;
|
|
49
|
+
const BROWSER_RECOVERY_TIMEOUT_MS = 5e3;
|
|
50
|
+
const BROWSER_RECOVERY_POLL_MS = 100;
|
|
51
|
+
const MAX_UPLOAD_FILES = 20;
|
|
52
|
+
const INACTIVE_AGENT_STATUSES = /* @__PURE__ */ new Set(["dead", "error", "exited", "stopped"]);
|
|
53
|
+
function errorMessage(error) {
|
|
54
|
+
return error instanceof Error ? error.message : String(error);
|
|
55
|
+
}
|
|
56
|
+
function errorCode(error) {
|
|
57
|
+
if (!error || typeof error !== "object" || !("code" in error)) return "";
|
|
58
|
+
return typeof error.code === "string" ? error.code : "";
|
|
59
|
+
}
|
|
60
|
+
function publicResource(resource, collectionRevision) {
|
|
61
|
+
return {
|
|
62
|
+
id: resource.id,
|
|
63
|
+
ownerType: resource.ownerType,
|
|
64
|
+
ownerAgentId: resource.ownerAgentId,
|
|
65
|
+
projectRootId: resource.projectRootId,
|
|
66
|
+
workspace: resource.workspace,
|
|
67
|
+
name: resource.name,
|
|
68
|
+
status: resource.status,
|
|
69
|
+
generation: resource.generation,
|
|
70
|
+
revision: resource.revision,
|
|
71
|
+
collectionRevision,
|
|
72
|
+
url: resource.url,
|
|
73
|
+
title: resource.title,
|
|
74
|
+
browserKind: resource.browserKind,
|
|
75
|
+
error: resource.error,
|
|
76
|
+
createdAt: resource.createdAt,
|
|
77
|
+
updatedAt: resource.updatedAt
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function browserError(message, status = 400, code = "BROWSER_INVALID_REQUEST") {
|
|
81
|
+
const error = new Error(message);
|
|
82
|
+
error.status = status;
|
|
83
|
+
error.code = code;
|
|
84
|
+
return error;
|
|
85
|
+
}
|
|
86
|
+
function browserOwnerKey(resource) {
|
|
87
|
+
return resource.ownerType === "agent" ? `agent:${resource.ownerAgentId}` : `project:${resource.projectRootId}`;
|
|
88
|
+
}
|
|
89
|
+
function externalBrowserFailure(action, cause) {
|
|
90
|
+
const error = browserError(
|
|
91
|
+
`${action}; verify the Browser plugin's external CDP address and the browser's /json/version endpoint`,
|
|
92
|
+
500,
|
|
93
|
+
"BROWSER_EXTERNAL_CDP_FAILED"
|
|
94
|
+
);
|
|
95
|
+
error.cause = cause;
|
|
96
|
+
return error;
|
|
97
|
+
}
|
|
98
|
+
function normalizeUrl(value) {
|
|
99
|
+
const input = String(value || "").trim();
|
|
100
|
+
if (!input) return "about:blank";
|
|
101
|
+
if (input === "about:blank") return input;
|
|
102
|
+
let url = input;
|
|
103
|
+
if (!/^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(input)) {
|
|
104
|
+
const authority = input.split(/[/?#]/, 1)[0];
|
|
105
|
+
const hostname = authority.replace(/:\d+$/, "").replace(/^\[|\]$/g, "").toLowerCase();
|
|
106
|
+
const explicitPort = authority.match(/:(\d+)$/)?.[1] || "";
|
|
107
|
+
const isIpLiteral = /^\d{1,3}(?:\.\d{1,3}){3}$/.test(hostname) || authority.startsWith("[");
|
|
108
|
+
const localHost = hostname === "localhost" || hostname.endsWith(".localhost") || !hostname.includes(".") || isIpLiteral;
|
|
109
|
+
url = `${localHost || explicitPort && explicitPort !== "443" ? "http" : "https"}://${input}`;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const parsed = new URL(url);
|
|
113
|
+
if (!["http:", "https:"].includes(parsed.protocol)) {
|
|
114
|
+
throw browserError("Browser navigation supports only http, https, and about:blank URLs");
|
|
115
|
+
}
|
|
116
|
+
return parsed.href;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if (error && typeof error === "object" && "status" in error) throw error;
|
|
119
|
+
throw browserError("Invalid Browser URL");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function browserSiteScope(value) {
|
|
123
|
+
const input = String(value || "").trim();
|
|
124
|
+
if (!input || input === "about:blank") return null;
|
|
125
|
+
try {
|
|
126
|
+
const parsed = new URL(normalizeUrl(input));
|
|
127
|
+
if (!["http:", "https:"].includes(parsed.protocol) || !parsed.host) return null;
|
|
128
|
+
return {
|
|
129
|
+
scopeKey: `site:${parsed.origin.toLowerCase()}`,
|
|
130
|
+
site: parsed.host.toLowerCase()
|
|
131
|
+
};
|
|
132
|
+
} catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function tabResourceName(tab) {
|
|
137
|
+
const title = String(tab?.title || "").trim();
|
|
138
|
+
if (title) return title.slice(0, 120);
|
|
139
|
+
try {
|
|
140
|
+
return new URL(String(tab?.url || "")).hostname.slice(0, 120) || "Browser";
|
|
141
|
+
} catch {
|
|
142
|
+
return "Browser";
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function pathInside(root, candidate) {
|
|
146
|
+
return candidate === root || candidate.startsWith(`${root}${path.sep}`);
|
|
147
|
+
}
|
|
148
|
+
function resolveWorkspaceInputFile(resource, value) {
|
|
149
|
+
const workspace = fs.realpathSync(resource.workspace);
|
|
150
|
+
const requested = path.resolve(resource.workspace, String(value || ""));
|
|
151
|
+
let resolved;
|
|
152
|
+
try {
|
|
153
|
+
resolved = fs.realpathSync(requested);
|
|
154
|
+
} catch {
|
|
155
|
+
throw browserError(`Upload file does not exist: ${value}`);
|
|
156
|
+
}
|
|
157
|
+
if (!pathInside(workspace, resolved)) {
|
|
158
|
+
throw browserError("Browser uploads must stay inside the Browser Project workspace");
|
|
159
|
+
}
|
|
160
|
+
if (!fs.statSync(resolved).isFile()) {
|
|
161
|
+
throw browserError(`Browser upload path is not a file: ${value}`);
|
|
162
|
+
}
|
|
163
|
+
return resolved;
|
|
164
|
+
}
|
|
165
|
+
function resolveWorkspaceOutputFile(resource, value) {
|
|
166
|
+
const requestedValue = String(value || "").trim();
|
|
167
|
+
if (!requestedValue) throw browserError("Download output path is required");
|
|
168
|
+
const workspace = fs.realpathSync(resource.workspace);
|
|
169
|
+
const requested = path.resolve(resource.workspace, requestedValue);
|
|
170
|
+
if (!pathInside(path.resolve(resource.workspace), requested)) {
|
|
171
|
+
throw browserError("Browser downloads must stay inside the Browser Project workspace");
|
|
172
|
+
}
|
|
173
|
+
let parent;
|
|
174
|
+
try {
|
|
175
|
+
parent = fs.realpathSync(path.dirname(requested));
|
|
176
|
+
} catch {
|
|
177
|
+
throw browserError("Browser download parent directory does not exist");
|
|
178
|
+
}
|
|
179
|
+
if (!pathInside(workspace, parent)) {
|
|
180
|
+
throw browserError("Browser downloads must stay inside the Browser Project workspace");
|
|
181
|
+
}
|
|
182
|
+
if (fs.existsSync(requested)) {
|
|
183
|
+
throw browserError("Browser download target already exists");
|
|
184
|
+
}
|
|
185
|
+
return requested;
|
|
186
|
+
}
|
|
187
|
+
class BrowserResourceManager extends EventEmitter {
|
|
188
|
+
configDir;
|
|
189
|
+
store;
|
|
190
|
+
isolatedBrowserProvider;
|
|
191
|
+
discoverExecutable;
|
|
192
|
+
discoverBrowserOptions;
|
|
193
|
+
getBrowserSettings;
|
|
194
|
+
saveBrowserSelection;
|
|
195
|
+
createRuntime;
|
|
196
|
+
recoverRuntime;
|
|
197
|
+
isEnabled;
|
|
198
|
+
readProcessIdentity;
|
|
199
|
+
killProcessGroup;
|
|
200
|
+
wait;
|
|
201
|
+
scheduleTimeout;
|
|
202
|
+
cancelTimeout;
|
|
203
|
+
runtimes = /* @__PURE__ */ new Map();
|
|
204
|
+
sessions = /* @__PURE__ */ new Map();
|
|
205
|
+
operations = /* @__PURE__ */ new Map();
|
|
206
|
+
stopAdmissions = /* @__PURE__ */ new Map();
|
|
207
|
+
disposed = false;
|
|
208
|
+
runtimeCapability = null;
|
|
209
|
+
browserOptions = [];
|
|
210
|
+
isolatedBrowserCapability = null;
|
|
211
|
+
capabilityProbeSignature = "";
|
|
212
|
+
capabilityRefreshPromise = null;
|
|
213
|
+
constructor(options) {
|
|
214
|
+
super();
|
|
215
|
+
this.configDir = options.configDir;
|
|
216
|
+
this.store = options.store || new import_browser_resource_store.BrowserResourceStore(options.configDir);
|
|
217
|
+
this.isolatedBrowserProvider = options.isolatedBrowserProvider || null;
|
|
218
|
+
this.discoverExecutable = options.discoverExecutable || ((selection) => (0, import_executable_discovery.discoverBrowserRuntime)({
|
|
219
|
+
...options,
|
|
220
|
+
...selection
|
|
221
|
+
}));
|
|
222
|
+
this.discoverBrowserOptions = options.discoverBrowserOptions || (options.discoverExecutable ? () => [] : () => (0, import_executable_discovery.discoverBrowserExecutables)());
|
|
223
|
+
this.getBrowserSettings = typeof options.getBrowserSettings === "function" ? options.getBrowserSettings : () => ({ browserSource: "system", browserExecutablePath: "", browserExternalCdpUrl: "" });
|
|
224
|
+
this.saveBrowserSelection = typeof options.saveBrowserSelection === "function" ? options.saveBrowserSelection : () => {
|
|
225
|
+
};
|
|
226
|
+
this.createRuntime = options.createRuntime || ((input) => new import_agent_browser_runtime.AgentBrowserRuntime(input));
|
|
227
|
+
this.recoverRuntime = options.recoverRuntime || ((input) => import_agent_browser_runtime.AgentBrowserRuntime.recover(input));
|
|
228
|
+
this.isEnabled = typeof options.isEnabled === "function" ? options.isEnabled : () => true;
|
|
229
|
+
this.readProcessIdentity = options.readProcessIdentity || import_server_process_identity.readServerProcessIdentity;
|
|
230
|
+
this.killProcessGroup = options.killProcessGroup || ((processGroupId, signal) => process.kill(-processGroupId, signal));
|
|
231
|
+
this.wait = options.wait || ((durationMs) => new Promise((resolve) => setTimeout(resolve, durationMs)));
|
|
232
|
+
this.scheduleTimeout = options.scheduleTimeout || setTimeout;
|
|
233
|
+
this.cancelTimeout = options.cancelTimeout || clearTimeout;
|
|
234
|
+
}
|
|
235
|
+
async init() {
|
|
236
|
+
this.store.init();
|
|
237
|
+
await this.refreshCapability();
|
|
238
|
+
const interrupted = this.store.list().filter(
|
|
239
|
+
(resource) => Boolean(resource.processIdentity) || ["running", "starting", "stopping"].includes(resource.status)
|
|
240
|
+
);
|
|
241
|
+
const groups = /* @__PURE__ */ new Map();
|
|
242
|
+
for (const resource of interrupted) {
|
|
243
|
+
const key = resource.runtimeKind === "agent-browser" ? resource.sessionId || resource.id : resource.id;
|
|
244
|
+
const group = groups.get(key) || [];
|
|
245
|
+
group.push(resource);
|
|
246
|
+
groups.set(key, group);
|
|
247
|
+
}
|
|
248
|
+
await Promise.all([...groups.values()].map((resources) => {
|
|
249
|
+
const owner = resources.find((resource) => resource.processIdentity) || resources[0];
|
|
250
|
+
if (!owner) return Promise.resolve();
|
|
251
|
+
return this.recoverInterruptedRuntime(owner, resources);
|
|
252
|
+
}));
|
|
253
|
+
}
|
|
254
|
+
async recoverInterruptedRuntime(resource, relatedResources = [resource]) {
|
|
255
|
+
if (resource.runtimeKind === "agent-browser") {
|
|
256
|
+
const capability = this.runtimeCapability?.agentBrowserPath ? this.runtimeCapability : await this.discoverExecutable({ source: "isolated" });
|
|
257
|
+
let runtimeError = null;
|
|
258
|
+
if (!capability || capability.error || !capability.agentBrowserPath) {
|
|
259
|
+
runtimeError = new Error(
|
|
260
|
+
capability?.error || "The exact agent-browser runtime required to clean up this Browser is unavailable"
|
|
261
|
+
);
|
|
262
|
+
} else {
|
|
263
|
+
try {
|
|
264
|
+
await this.recoverRuntime({
|
|
265
|
+
id: resource.sessionId || resource.id,
|
|
266
|
+
generation: resource.sessionGeneration || resource.generation,
|
|
267
|
+
processIdentity: resource.processIdentity,
|
|
268
|
+
configDir: this.configDir,
|
|
269
|
+
profileDir: storageLayout.browserProfileDir(
|
|
270
|
+
this.configDir,
|
|
271
|
+
resource.sessionId || resource.id
|
|
272
|
+
),
|
|
273
|
+
agentBrowserPath: capability.agentBrowserPath,
|
|
274
|
+
readProcessIdentity: this.readProcessIdentity,
|
|
275
|
+
wait: this.wait
|
|
276
|
+
});
|
|
277
|
+
} catch (error) {
|
|
278
|
+
runtimeError = error;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
for (const related of relatedResources) {
|
|
282
|
+
this.store.update(related.id, {
|
|
283
|
+
status: "failed",
|
|
284
|
+
error: runtimeError ? `agent-browser Session cleanup failed: ${errorMessage(runtimeError)}` : "Farming restarted and cleaned up the previous Browser runtime",
|
|
285
|
+
...!runtimeError ? { processIdentity: null } : {},
|
|
286
|
+
tabId: ""
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const expected = resource.processIdentity;
|
|
292
|
+
if (!expected) {
|
|
293
|
+
this.store.update(resource.id, {
|
|
294
|
+
status: "failed",
|
|
295
|
+
error: resource.browserKind === "external-cdp" ? "Farming restarted and disconnected from the external Browser" : resource.browserKind === "isolated-computer" ? "Farming restarted and stopped the isolated Browser runtime" : "Farming restarted before the Browser runtime identity was committed",
|
|
296
|
+
processIdentity: null
|
|
297
|
+
});
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const current = await this.readProcessIdentity(expected.pid);
|
|
301
|
+
if (!(0, import_server_process_identity.matchingProcessIdentity)(expected, current)) {
|
|
302
|
+
this.store.update(resource.id, {
|
|
303
|
+
status: "failed",
|
|
304
|
+
error: "Farming restarted after the previous Browser runtime exited",
|
|
305
|
+
processIdentity: null
|
|
306
|
+
});
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (expected.processGroupId !== expected.pid) {
|
|
310
|
+
this.store.update(resource.id, {
|
|
311
|
+
status: "failed",
|
|
312
|
+
error: `Previous Browser process ${expected.pid} has an unsafe process-group identity; stop it manually`
|
|
313
|
+
});
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
try {
|
|
317
|
+
this.killProcessGroup(expected.processGroupId, "SIGKILL");
|
|
318
|
+
} catch (error) {
|
|
319
|
+
if (errorCode(error) !== "ESRCH") {
|
|
320
|
+
const permission = errorCode(error) === "EPERM" || errorCode(error) === "EACCES";
|
|
321
|
+
this.store.update(resource.id, {
|
|
322
|
+
status: "failed",
|
|
323
|
+
error: permission ? `Farming cannot clean up previous Browser process ${expected.pid} because it lacks permission` : `Farming could not clean up previous Browser process ${expected.pid}: ${errorMessage(error)}`
|
|
324
|
+
});
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const startedAt = Date.now();
|
|
329
|
+
while ((0, import_server_process_identity.matchingProcessIdentity)(expected, await this.readProcessIdentity(expected.pid))) {
|
|
330
|
+
if (Date.now() - startedAt >= BROWSER_RECOVERY_TIMEOUT_MS) {
|
|
331
|
+
this.store.update(resource.id, {
|
|
332
|
+
status: "failed",
|
|
333
|
+
error: `Previous Browser process ${expected.pid} did not exit after SIGKILL`
|
|
334
|
+
});
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
await this.wait(BROWSER_RECOVERY_POLL_MS);
|
|
338
|
+
}
|
|
339
|
+
this.store.update(resource.id, {
|
|
340
|
+
status: "failed",
|
|
341
|
+
error: "Farming restarted and cleaned up the previous Browser runtime",
|
|
342
|
+
processIdentity: null
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
capability() {
|
|
346
|
+
const executable = this.runtimeCapability;
|
|
347
|
+
const runnable = executable && !executable.error;
|
|
348
|
+
const enabled = this.isEnabled() === true;
|
|
349
|
+
const selection = this.browserSelection();
|
|
350
|
+
return {
|
|
351
|
+
enabled,
|
|
352
|
+
available: enabled && Boolean(runnable),
|
|
353
|
+
browser: runnable ? { kind: executable.kind, path: executable.path } : null,
|
|
354
|
+
selection,
|
|
355
|
+
options: this.browserOptions.map((option) => ({ kind: option.kind, path: option.path })),
|
|
356
|
+
...this.isolatedBrowserCapability ? { isolated: this.isolatedBrowserCapability } : {},
|
|
357
|
+
message: !enabled ? "Browser extension is disabled" : executable?.error || (runnable ? "" : "Choose a local Chromium browser or prepare the isolated Browser runtime")
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
browserSelection(settings = this.getBrowserSettings()) {
|
|
361
|
+
const source = settings?.browserSource;
|
|
362
|
+
return {
|
|
363
|
+
source: source && ["external-cdp", "isolated"].includes(source) ? source : "system",
|
|
364
|
+
executablePath: String(settings?.browserExecutablePath || ""),
|
|
365
|
+
externalCdpUrl: String(settings?.browserExternalCdpUrl || "http://127.0.0.1:9222")
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
browserCapabilitySignature(selection, browserOptions, isolatedBrowserCapability = this.isolatedBrowserCapability) {
|
|
369
|
+
const executablePaths = [...new Set([
|
|
370
|
+
...browserOptions.map((option) => option.path),
|
|
371
|
+
selection.executablePath
|
|
372
|
+
].filter(Boolean))].sort();
|
|
373
|
+
return JSON.stringify({
|
|
374
|
+
selection,
|
|
375
|
+
options: browserOptions.map((option) => ({ kind: option.kind, path: option.path })),
|
|
376
|
+
executables: executablePaths.map((executablePath) => {
|
|
377
|
+
try {
|
|
378
|
+
const stat = fs.statSync(executablePath);
|
|
379
|
+
return { path: executablePath, size: stat.size, mtimeMs: stat.mtimeMs };
|
|
380
|
+
} catch {
|
|
381
|
+
return { path: executablePath, missing: true };
|
|
382
|
+
}
|
|
383
|
+
}),
|
|
384
|
+
isolatedBrowserCapability
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
async probeCapability(selection = this.browserSelection(), discoveredOptions, discoveredIsolatedCapability) {
|
|
388
|
+
const browserOptions = discoveredOptions || this.discoverBrowserOptions();
|
|
389
|
+
const selectedOption = browserOptions.find((option) => option.path === selection.executablePath);
|
|
390
|
+
const isolatedBrowserCapability = discoveredIsolatedCapability !== void 0 ? discoveredIsolatedCapability : this.isolatedBrowserProvider ? await this.isolatedBrowserProvider.capability() : null;
|
|
391
|
+
let runtimeCapability = await this.discoverExecutable({
|
|
392
|
+
source: selection.source,
|
|
393
|
+
executablePath: selection.executablePath,
|
|
394
|
+
executableKind: selectedOption?.kind,
|
|
395
|
+
externalCdpUrl: selection.externalCdpUrl
|
|
396
|
+
});
|
|
397
|
+
if (selection.source === "isolated" && isolatedBrowserCapability?.available === true) {
|
|
398
|
+
runtimeCapability = await this.discoverExecutable({ source: "isolated" });
|
|
399
|
+
}
|
|
400
|
+
if (selection.source === "isolated" && isolatedBrowserCapability?.available !== true) {
|
|
401
|
+
runtimeCapability = {
|
|
402
|
+
kind: "isolated-computer",
|
|
403
|
+
path: "",
|
|
404
|
+
error: isolatedBrowserCapability?.dockerAvailable === true ? "Prepare the isolated Browser runtime before selecting it" : "Docker is required for the isolated Browser"
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
return { browserOptions, isolatedBrowserCapability, runtimeCapability };
|
|
408
|
+
}
|
|
409
|
+
async refreshCapability(selection, options = {}) {
|
|
410
|
+
if (options.reuseVerified && this.capabilityRefreshPromise) {
|
|
411
|
+
return this.capabilityRefreshPromise;
|
|
412
|
+
}
|
|
413
|
+
const refresh = async () => {
|
|
414
|
+
let desiredSelection = selection || this.browserSelection();
|
|
415
|
+
const browserOptions = this.discoverBrowserOptions();
|
|
416
|
+
if (!selection && desiredSelection.source === "system" && !desiredSelection.executablePath) {
|
|
417
|
+
const defaultBrowser = browserOptions[0];
|
|
418
|
+
if (defaultBrowser) {
|
|
419
|
+
this.saveBrowserSelection({
|
|
420
|
+
source: "system",
|
|
421
|
+
executablePath: defaultBrowser.path
|
|
422
|
+
});
|
|
423
|
+
desiredSelection = {
|
|
424
|
+
...desiredSelection,
|
|
425
|
+
executablePath: defaultBrowser.path
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
const cachedIsolatedCapability = options.reuseVerified && this.isolatedBrowserProvider ? await this.isolatedBrowserProvider.capability() : void 0;
|
|
430
|
+
const signature = this.browserCapabilitySignature(
|
|
431
|
+
desiredSelection,
|
|
432
|
+
browserOptions,
|
|
433
|
+
cachedIsolatedCapability
|
|
434
|
+
);
|
|
435
|
+
if (options.reuseVerified && signature === this.capabilityProbeSignature) {
|
|
436
|
+
this.browserOptions = browserOptions;
|
|
437
|
+
if (cachedIsolatedCapability !== void 0) {
|
|
438
|
+
this.isolatedBrowserCapability = cachedIsolatedCapability;
|
|
439
|
+
}
|
|
440
|
+
return this.runtimeCapability;
|
|
441
|
+
}
|
|
442
|
+
const probe = await this.probeCapability(
|
|
443
|
+
desiredSelection,
|
|
444
|
+
browserOptions,
|
|
445
|
+
cachedIsolatedCapability
|
|
446
|
+
);
|
|
447
|
+
this.browserOptions = browserOptions;
|
|
448
|
+
this.isolatedBrowserCapability = probe.isolatedBrowserCapability;
|
|
449
|
+
this.runtimeCapability = probe.runtimeCapability;
|
|
450
|
+
this.capabilityProbeSignature = this.browserCapabilitySignature(
|
|
451
|
+
desiredSelection,
|
|
452
|
+
probe.browserOptions,
|
|
453
|
+
probe.isolatedBrowserCapability
|
|
454
|
+
);
|
|
455
|
+
return this.runtimeCapability;
|
|
456
|
+
};
|
|
457
|
+
if (!options.reuseVerified) return refresh();
|
|
458
|
+
this.capabilityRefreshPromise = refresh().finally(() => {
|
|
459
|
+
this.capabilityRefreshPromise = null;
|
|
460
|
+
});
|
|
461
|
+
return this.capabilityRefreshPromise;
|
|
462
|
+
}
|
|
463
|
+
async prepareIsolatedBrowser() {
|
|
464
|
+
if (!this.isolatedBrowserProvider) {
|
|
465
|
+
throw browserError("The isolated Browser runtime is unavailable", 503, "ISOLATED_BROWSER_UNAVAILABLE");
|
|
466
|
+
}
|
|
467
|
+
await this.isolatedBrowserProvider.prepare();
|
|
468
|
+
await this.refreshCapability();
|
|
469
|
+
return this.capability();
|
|
470
|
+
}
|
|
471
|
+
list() {
|
|
472
|
+
this.requireEnabled();
|
|
473
|
+
return this.store.list().map((resource) => publicResource(resource, this.store.revision));
|
|
474
|
+
}
|
|
475
|
+
snapshot() {
|
|
476
|
+
this.requireEnabled();
|
|
477
|
+
return this.stateSnapshot();
|
|
478
|
+
}
|
|
479
|
+
stateSnapshot() {
|
|
480
|
+
return {
|
|
481
|
+
collectionRevision: this.store.revision,
|
|
482
|
+
resources: this.store.list().map((resource) => publicResource(resource, this.store.revision))
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
get(id) {
|
|
486
|
+
this.requireEnabled();
|
|
487
|
+
return publicResource(this.requireStored(id), this.store.revision);
|
|
488
|
+
}
|
|
489
|
+
permissionDecision(agentId, tool, input = {}) {
|
|
490
|
+
if (["browser_list", "browser_stop"].includes(tool)) {
|
|
491
|
+
return { requiresApproval: false, scopeKey: "", site: "" };
|
|
492
|
+
}
|
|
493
|
+
if (tool === "browser_open" || tool === "browser_navigate") {
|
|
494
|
+
const site2 = browserSiteScope(input.url);
|
|
495
|
+
return site2 ? { requiresApproval: true, ...site2 } : { requiresApproval: false, scopeKey: "", site: "" };
|
|
496
|
+
}
|
|
497
|
+
const browserId = String(input.browserId || "");
|
|
498
|
+
const resource = browserId ? this.store.get(browserId) : null;
|
|
499
|
+
if (!resource || resource.ownerType !== "agent" || resource.ownerAgentId !== agentId) {
|
|
500
|
+
return { requiresApproval: true, scopeKey: "", site: "" };
|
|
501
|
+
}
|
|
502
|
+
const site = browserSiteScope(resource.url);
|
|
503
|
+
return site ? { requiresApproval: true, ...site } : { requiresApproval: false, scopeKey: "", site: "" };
|
|
504
|
+
}
|
|
505
|
+
create(input) {
|
|
506
|
+
this.requireAvailable();
|
|
507
|
+
if (this.disposed) throw browserError("Browser manager is stopping", 503, "BROWSER_MANAGER_STOPPING");
|
|
508
|
+
const resource = this.store.create({
|
|
509
|
+
projectRootId: input.projectRootId,
|
|
510
|
+
workspace: input.workspace,
|
|
511
|
+
ownerType: input.ownerType,
|
|
512
|
+
ownerAgentId: input.ownerAgentId,
|
|
513
|
+
name: input.name,
|
|
514
|
+
url: normalizeUrl(input.url)
|
|
515
|
+
});
|
|
516
|
+
this.emitResource(resource);
|
|
517
|
+
return publicResource(resource, this.store.revision);
|
|
518
|
+
}
|
|
519
|
+
rename(id, name) {
|
|
520
|
+
this.requireEnabled();
|
|
521
|
+
const title = String(name || "").trim();
|
|
522
|
+
if (!title) throw browserError("Browser name is required");
|
|
523
|
+
const resource = this.requireStored(id);
|
|
524
|
+
const next = this.store.update(resource.id, {
|
|
525
|
+
name: title.slice(0, 120),
|
|
526
|
+
autoName: false
|
|
527
|
+
});
|
|
528
|
+
this.emitResource(next);
|
|
529
|
+
return publicResource(next, this.store.revision);
|
|
530
|
+
}
|
|
531
|
+
start(id) {
|
|
532
|
+
this.requireEnabled();
|
|
533
|
+
return this.enqueue(id, async () => {
|
|
534
|
+
const resource = this.requireStored(id);
|
|
535
|
+
const existingBinding = this.runtimes.get(id);
|
|
536
|
+
if (resource.status === "running" && existingBinding) {
|
|
537
|
+
return publicResource(resource, this.store.revision);
|
|
538
|
+
}
|
|
539
|
+
if (existingBinding) {
|
|
540
|
+
throw browserError(
|
|
541
|
+
"A previous Browser runtime still owns this resource; stop it before restarting",
|
|
542
|
+
409,
|
|
543
|
+
"BROWSER_RUNTIME_OWNED"
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
if (resource.status === "starting" || resource.status === "stopping") {
|
|
547
|
+
throw browserError(`Browser is ${resource.status}`, 409, "BROWSER_BUSY");
|
|
548
|
+
}
|
|
549
|
+
if (resource.processIdentity) {
|
|
550
|
+
const blockedIdentity = resource.processIdentity;
|
|
551
|
+
throw browserError(
|
|
552
|
+
`Previous Browser process ${blockedIdentity.pid} still requires cleanup`,
|
|
553
|
+
409,
|
|
554
|
+
"BROWSER_RECOVERY_CLEANUP_REQUIRED"
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
const executable = await this.refreshCapability();
|
|
558
|
+
if (!executable || executable.error || !executable.agentBrowserPath) {
|
|
559
|
+
const failed = this.store.update(id, {
|
|
560
|
+
status: "failed",
|
|
561
|
+
error: executable?.error || "Choose a local Chromium browser or prepare the isolated Browser runtime"
|
|
562
|
+
});
|
|
563
|
+
this.emitResource(failed);
|
|
564
|
+
throw browserError(failed.error, 503, "BROWSER_EXECUTABLE_NOT_FOUND");
|
|
565
|
+
}
|
|
566
|
+
const generation = resource.generation + 1;
|
|
567
|
+
const starting = this.store.update(id, {
|
|
568
|
+
status: "starting",
|
|
569
|
+
generation,
|
|
570
|
+
browserKind: executable.kind,
|
|
571
|
+
runtimeKind: "agent-browser",
|
|
572
|
+
error: "",
|
|
573
|
+
processIdentity: null
|
|
574
|
+
});
|
|
575
|
+
this.emitResource(starting);
|
|
576
|
+
const reusableSession = [...this.sessions.values()].find((session2) => !session2.closing && session2.ownerKey === browserOwnerKey(resource) && session2.projectRootId === resource.projectRootId && session2.browserKind === executable.kind);
|
|
577
|
+
if (reusableSession) {
|
|
578
|
+
try {
|
|
579
|
+
let running;
|
|
580
|
+
let binding2;
|
|
581
|
+
const operation = (reusableSession.actionChain || Promise.resolve()).catch(() => {
|
|
582
|
+
}).then(async () => {
|
|
583
|
+
const tab = await reusableSession.runtime.createTab(
|
|
584
|
+
resource.url,
|
|
585
|
+
["external-cdp", "isolated-computer"].includes(executable.kind) ? `farming-${resource.id}-g${generation}` : ""
|
|
586
|
+
);
|
|
587
|
+
binding2 = this.createBinding(reusableSession, {
|
|
588
|
+
...starting,
|
|
589
|
+
tabId: tab.tabId
|
|
590
|
+
});
|
|
591
|
+
reusableSession.bindings.set(id, binding2);
|
|
592
|
+
reusableSession.activeResourceId = id;
|
|
593
|
+
this.runtimes.set(id, binding2);
|
|
594
|
+
running = this.store.update(id, {
|
|
595
|
+
status: "running",
|
|
596
|
+
sessionId: reusableSession.id,
|
|
597
|
+
sessionGeneration: reusableSession.generation,
|
|
598
|
+
tabId: tab.tabId,
|
|
599
|
+
url: tab.url || resource.url,
|
|
600
|
+
title: tab.title || "",
|
|
601
|
+
error: "",
|
|
602
|
+
processIdentity: null
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
reusableSession.actionChain = operation;
|
|
606
|
+
await operation;
|
|
607
|
+
if (!running || !binding2) {
|
|
608
|
+
throw browserError("Browser tab creation did not commit ownership", 500, "BROWSER_START_FAILED");
|
|
609
|
+
}
|
|
610
|
+
this.emitResource(running);
|
|
611
|
+
this.broadcastRuntimeState(binding2);
|
|
612
|
+
return publicResource(running, this.store.revision);
|
|
613
|
+
} catch (error) {
|
|
614
|
+
const failed = this.store.update(id, {
|
|
615
|
+
status: "failed",
|
|
616
|
+
error: errorMessage(error) || "Failed to create Browser tab",
|
|
617
|
+
tabId: ""
|
|
618
|
+
});
|
|
619
|
+
this.emitResource(failed);
|
|
620
|
+
throw browserError(failed.error, 500, "BROWSER_START_FAILED");
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
const sessionId = resource.sessionId || id;
|
|
624
|
+
const previousSessionGeneration = this.store.list().filter((candidate) => candidate.sessionId === sessionId).reduce((maximum, candidate) => Math.max(maximum, candidate.sessionGeneration || 0), 0);
|
|
625
|
+
const sessionGeneration = previousSessionGeneration + 1;
|
|
626
|
+
let isolatedLeaseKey = "";
|
|
627
|
+
let externalCdpUrl = executable.cdpUrl || "";
|
|
628
|
+
if (executable.kind === "isolated-computer") {
|
|
629
|
+
try {
|
|
630
|
+
if (!this.isolatedBrowserProvider) {
|
|
631
|
+
throw browserError(
|
|
632
|
+
"The isolated Browser runtime is unavailable",
|
|
633
|
+
503,
|
|
634
|
+
"ISOLATED_BROWSER_UNAVAILABLE"
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
const isolated = await this.isolatedBrowserProvider.acquire({
|
|
638
|
+
ownerAgentId: resource.ownerAgentId,
|
|
639
|
+
ownerKey: browserOwnerKey(resource),
|
|
640
|
+
projectRootId: resource.projectRootId,
|
|
641
|
+
workspace: resource.workspace
|
|
642
|
+
});
|
|
643
|
+
externalCdpUrl = isolated.cdpUrl;
|
|
644
|
+
isolatedLeaseKey = isolated.leaseKey;
|
|
645
|
+
} catch (error) {
|
|
646
|
+
const failed = this.store.update(id, {
|
|
647
|
+
status: "failed",
|
|
648
|
+
error: errorMessage(error) || "Failed to start the isolated Browser runtime"
|
|
649
|
+
});
|
|
650
|
+
this.emitResource(failed);
|
|
651
|
+
throw error;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
let runtime;
|
|
655
|
+
try {
|
|
656
|
+
runtime = this.createRuntime({
|
|
657
|
+
id: sessionId,
|
|
658
|
+
generation: sessionGeneration,
|
|
659
|
+
configDir: this.configDir,
|
|
660
|
+
agentBrowserPath: executable.agentBrowserPath,
|
|
661
|
+
executablePath: executable.path,
|
|
662
|
+
externalCdpUrl,
|
|
663
|
+
profileDir: storageLayout.browserProfileDir(this.configDir, sessionId)
|
|
664
|
+
});
|
|
665
|
+
} catch (error) {
|
|
666
|
+
if (isolatedLeaseKey && this.isolatedBrowserProvider) {
|
|
667
|
+
await this.isolatedBrowserProvider.release(isolatedLeaseKey).catch(() => null);
|
|
668
|
+
}
|
|
669
|
+
const failed = this.store.update(id, {
|
|
670
|
+
status: "failed",
|
|
671
|
+
error: errorMessage(error) || "Failed to create Browser runtime"
|
|
672
|
+
});
|
|
673
|
+
this.emitResource(failed);
|
|
674
|
+
throw error;
|
|
675
|
+
}
|
|
676
|
+
const session = {
|
|
677
|
+
id: sessionId,
|
|
678
|
+
generation: sessionGeneration,
|
|
679
|
+
projectRootId: resource.projectRootId,
|
|
680
|
+
ownerKey: browserOwnerKey(resource),
|
|
681
|
+
browserKind: executable.kind,
|
|
682
|
+
runtime,
|
|
683
|
+
bindings: /* @__PURE__ */ new Map(),
|
|
684
|
+
activeResourceId: id,
|
|
685
|
+
processOwnerResourceId: id,
|
|
686
|
+
actionChain: Promise.resolve(),
|
|
687
|
+
reconcilingTabs: Promise.resolve(),
|
|
688
|
+
initializing: true,
|
|
689
|
+
isolatedLeaseKey,
|
|
690
|
+
closing: false
|
|
691
|
+
};
|
|
692
|
+
const binding = this.createBinding(session, starting);
|
|
693
|
+
session.bindings.set(id, binding);
|
|
694
|
+
this.sessions.set(sessionId, session);
|
|
695
|
+
this.runtimes.set(id, binding);
|
|
696
|
+
this.bindSession(session);
|
|
697
|
+
try {
|
|
698
|
+
const metadata = await runtime.start(resource.url);
|
|
699
|
+
const tabs = await runtime.listTabs();
|
|
700
|
+
const tab = tabs.find((candidate) => candidate.active) || tabs[0];
|
|
701
|
+
if (!tab) throw new Error("agent-browser did not report the Browser tab");
|
|
702
|
+
binding.tabId = tab.tabId;
|
|
703
|
+
if (this.runtimes.get(id) !== binding) {
|
|
704
|
+
throw browserError("Browser startup lost runtime ownership", 409, "BROWSER_START_REPLACED");
|
|
705
|
+
}
|
|
706
|
+
const running = this.store.update(id, {
|
|
707
|
+
status: "running",
|
|
708
|
+
sessionId,
|
|
709
|
+
sessionGeneration,
|
|
710
|
+
tabId: tab.tabId,
|
|
711
|
+
url: metadata.url || resource.url,
|
|
712
|
+
title: metadata.title || "",
|
|
713
|
+
error: ""
|
|
714
|
+
});
|
|
715
|
+
session.initializing = false;
|
|
716
|
+
this.emitResource(running);
|
|
717
|
+
this.broadcastRuntimeState(binding);
|
|
718
|
+
return publicResource(running, this.store.revision);
|
|
719
|
+
} catch (error) {
|
|
720
|
+
session.initializing = false;
|
|
721
|
+
let cleanupError = null;
|
|
722
|
+
try {
|
|
723
|
+
await runtime.close();
|
|
724
|
+
} catch (closeError) {
|
|
725
|
+
cleanupError = closeError;
|
|
726
|
+
}
|
|
727
|
+
if (!cleanupError && isolatedLeaseKey && this.isolatedBrowserProvider) {
|
|
728
|
+
try {
|
|
729
|
+
await this.isolatedBrowserProvider.release(isolatedLeaseKey);
|
|
730
|
+
} catch (releaseError) {
|
|
731
|
+
cleanupError = releaseError;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
if (!cleanupError && this.runtimes.get(id) === binding) this.runtimes.delete(id);
|
|
735
|
+
if (!cleanupError && this.sessions.get(sessionId) === session) this.sessions.delete(sessionId);
|
|
736
|
+
const current = this.store.get(id);
|
|
737
|
+
const failureMessage = executable.kind === "external-cdp" ? externalBrowserFailure("External Browser connection failed", error).message : executable.kind === "isolated-computer" ? `Isolated Browser connection failed: ${errorMessage(error)}` : errorMessage(error) || "Failed to start Browser";
|
|
738
|
+
const failed = current?.generation === generation ? this.store.update(id, {
|
|
739
|
+
status: "failed",
|
|
740
|
+
error: cleanupError ? `${failureMessage}; cleanup failed` : failureMessage,
|
|
741
|
+
tabId: "",
|
|
742
|
+
...!cleanupError ? { processIdentity: null } : {}
|
|
743
|
+
}) : null;
|
|
744
|
+
if (failed) this.emitResource(failed);
|
|
745
|
+
throw browserError(
|
|
746
|
+
failed?.error || errorMessage(error) || "Failed to start Browser",
|
|
747
|
+
500,
|
|
748
|
+
"BROWSER_START_FAILED"
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
stop(id, internal = false) {
|
|
754
|
+
if (!internal) this.requireEnabled();
|
|
755
|
+
this.stopAdmissions.set(id, (this.stopAdmissions.get(id) || 0) + 1);
|
|
756
|
+
return this.enqueue(id, async () => {
|
|
757
|
+
const resource = this.requireStored(id);
|
|
758
|
+
const binding = this.runtimes.get(id);
|
|
759
|
+
if (!binding) {
|
|
760
|
+
if (resource.processIdentity) {
|
|
761
|
+
await this.recoverInterruptedRuntime(resource);
|
|
762
|
+
const recovered = this.requireStored(id);
|
|
763
|
+
if (recovered.processIdentity) {
|
|
764
|
+
const blockedIdentity = recovered.processIdentity;
|
|
765
|
+
throw browserError(
|
|
766
|
+
recovered.error || `Previous Browser process ${blockedIdentity.pid} still requires cleanup`,
|
|
767
|
+
500,
|
|
768
|
+
"BROWSER_RECOVERY_CLEANUP_REQUIRED"
|
|
769
|
+
);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
const stopped2 = this.store.update(id, {
|
|
773
|
+
status: "stopped",
|
|
774
|
+
error: "",
|
|
775
|
+
processIdentity: null
|
|
776
|
+
});
|
|
777
|
+
this.emitResource(stopped2);
|
|
778
|
+
return publicResource(stopped2, this.store.revision);
|
|
779
|
+
}
|
|
780
|
+
const stopping = this.store.update(id, { status: "stopping", error: "" });
|
|
781
|
+
this.emitResource(stopping);
|
|
782
|
+
this.broadcastRuntimeState(binding);
|
|
783
|
+
const { session } = binding;
|
|
784
|
+
session.closing = session.bindings.size === 1;
|
|
785
|
+
let isolatedReleaseError = null;
|
|
786
|
+
try {
|
|
787
|
+
const closeOperation = (session.actionChain || Promise.resolve()).catch(() => {
|
|
788
|
+
}).then(() => session.bindings.size > 1 ? session.runtime.closeTab(binding.tabId) : session.runtime.close());
|
|
789
|
+
session.actionChain = closeOperation;
|
|
790
|
+
await closeOperation;
|
|
791
|
+
if (session.bindings.size === 1 && session.isolatedLeaseKey && this.isolatedBrowserProvider) {
|
|
792
|
+
try {
|
|
793
|
+
await this.isolatedBrowserProvider.release(session.isolatedLeaseKey);
|
|
794
|
+
session.isolatedLeaseKey = "";
|
|
795
|
+
} catch (error) {
|
|
796
|
+
isolatedReleaseError = error;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
} catch (error) {
|
|
800
|
+
session.closing = false;
|
|
801
|
+
if (resource.browserKind === "external-cdp") {
|
|
802
|
+
throw externalBrowserFailure("External Browser targets could not be closed", error);
|
|
803
|
+
}
|
|
804
|
+
throw error;
|
|
805
|
+
}
|
|
806
|
+
if (isolatedReleaseError) {
|
|
807
|
+
session.closing = false;
|
|
808
|
+
const failed = this.store.update(id, {
|
|
809
|
+
status: "failed",
|
|
810
|
+
error: `Browser closed, but its isolated container could not be stopped: ${errorMessage(isolatedReleaseError)}`
|
|
811
|
+
});
|
|
812
|
+
this.emitResource(failed);
|
|
813
|
+
this.broadcastRuntimeState(binding);
|
|
814
|
+
throw browserError(failed.error, 500, "ISOLATED_BROWSER_RELEASE_FAILED");
|
|
815
|
+
}
|
|
816
|
+
session.bindings.delete(id);
|
|
817
|
+
if (this.runtimes.get(id) === binding) this.runtimes.delete(id);
|
|
818
|
+
if (session.bindings.size === 0 && this.sessions.get(session.id) === session) {
|
|
819
|
+
this.sessions.delete(session.id);
|
|
820
|
+
}
|
|
821
|
+
session.closing = false;
|
|
822
|
+
if (session.processOwnerResourceId === id && session.bindings.size > 0) {
|
|
823
|
+
const nextOwner = session.bindings.values().next().value;
|
|
824
|
+
if (!nextOwner) throw new Error("Browser Session lost its remaining resource owner");
|
|
825
|
+
session.processOwnerResourceId = nextOwner.id;
|
|
826
|
+
const ownerResource = this.store.update(nextOwner.id, {
|
|
827
|
+
processIdentity: resource.processIdentity
|
|
828
|
+
});
|
|
829
|
+
this.emitResource(ownerResource);
|
|
830
|
+
this.broadcastRuntimeState(nextOwner);
|
|
831
|
+
}
|
|
832
|
+
const stopped = this.store.update(id, {
|
|
833
|
+
status: "stopped",
|
|
834
|
+
error: "",
|
|
835
|
+
processIdentity: null,
|
|
836
|
+
tabId: ""
|
|
837
|
+
});
|
|
838
|
+
this.emitResource(stopped);
|
|
839
|
+
this.broadcastRuntimeState(binding);
|
|
840
|
+
this.releaseViewerState(binding);
|
|
841
|
+
return publicResource(stopped, this.store.revision);
|
|
842
|
+
}).finally(() => {
|
|
843
|
+
const remainingStops = (this.stopAdmissions.get(id) || 1) - 1;
|
|
844
|
+
if (remainingStops > 0) this.stopAdmissions.set(id, remainingStops);
|
|
845
|
+
else this.stopAdmissions.delete(id);
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
async delete(id, internal = false) {
|
|
849
|
+
if (!internal) this.requireEnabled();
|
|
850
|
+
await this.stop(id, internal);
|
|
851
|
+
const resource = this.requireStored(id);
|
|
852
|
+
const ownerKey = browserOwnerKey(resource);
|
|
853
|
+
const deletesLastIsolatedOwner = resource.browserKind === "isolated-computer" && this.isolatedBrowserProvider && !this.store.list().some((candidate) => candidate.id !== resource.id && candidate.browserKind === "isolated-computer" && browserOwnerKey(candidate) === ownerKey);
|
|
854
|
+
if (deletesLastIsolatedOwner) {
|
|
855
|
+
await this.isolatedBrowserProvider.deleteOwner(ownerKey);
|
|
856
|
+
}
|
|
857
|
+
this.store.delete(id);
|
|
858
|
+
const sessionId = resource.sessionId || id;
|
|
859
|
+
const profileDir = storageLayout.browserProfileDir(this.configDir, sessionId);
|
|
860
|
+
const browsersDir = path.resolve(storageLayout.browserResourcesDir(this.configDir));
|
|
861
|
+
const resourceDir = path.resolve(profileDir, "..");
|
|
862
|
+
const sessionStillReferenced = this.store.list().some((candidate) => candidate.sessionId === sessionId);
|
|
863
|
+
if (!sessionStillReferenced && resourceDir.startsWith(`${browsersDir}${path.sep}`) && import_browser_resource_store.RESOURCE_ID_RE.test(sessionId)) {
|
|
864
|
+
fs.rmSync(resourceDir, { recursive: true, force: true });
|
|
865
|
+
}
|
|
866
|
+
this.emit("deleted", { id, collectionRevision: this.store.revision });
|
|
867
|
+
return { id, collectionRevision: this.store.revision };
|
|
868
|
+
}
|
|
869
|
+
navigate(id, url) {
|
|
870
|
+
this.requireEnabled();
|
|
871
|
+
const normalized = normalizeUrl(url);
|
|
872
|
+
return this.withRuntime(id, async (runtime, binding) => {
|
|
873
|
+
const metadata = await runtime.navigate(normalized);
|
|
874
|
+
this.updateMetadata(binding, metadata);
|
|
875
|
+
return this.get(id);
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
goBack(id) {
|
|
879
|
+
return this.withRuntime(id, async (runtime, binding) => {
|
|
880
|
+
const metadata = await runtime.goBack();
|
|
881
|
+
this.updateMetadata(binding, metadata);
|
|
882
|
+
return this.get(id);
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
goForward(id) {
|
|
886
|
+
return this.withRuntime(id, async (runtime, binding) => {
|
|
887
|
+
const metadata = await runtime.goForward();
|
|
888
|
+
this.updateMetadata(binding, metadata);
|
|
889
|
+
return this.get(id);
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
reload(id) {
|
|
893
|
+
return this.withRuntime(id, async (runtime, binding) => {
|
|
894
|
+
const metadata = await runtime.reload();
|
|
895
|
+
this.updateMetadata(binding, metadata);
|
|
896
|
+
return this.get(id);
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
action(id, input) {
|
|
900
|
+
this.requireEnabled();
|
|
901
|
+
const kind = String(input?.kind || "").trim();
|
|
902
|
+
if (kind === "snapshot") return this.withRuntime(id, (runtime) => runtime.snapshot());
|
|
903
|
+
if (kind === "screenshot") return this.withRuntime(id, (runtime) => runtime.screenshot());
|
|
904
|
+
if (kind === "navigate") return this.navigate(id, input.url);
|
|
905
|
+
if (kind === "back") return this.goBack(id);
|
|
906
|
+
if (kind === "forward") return this.goForward(id);
|
|
907
|
+
if (kind === "reload") return this.reload(id);
|
|
908
|
+
if (kind === "click") return this.withRuntime(id, (runtime) => runtime.click(input));
|
|
909
|
+
if ([
|
|
910
|
+
"dblclick",
|
|
911
|
+
"hover",
|
|
912
|
+
"focus",
|
|
913
|
+
"check",
|
|
914
|
+
"uncheck",
|
|
915
|
+
"scrollintoview",
|
|
916
|
+
"highlight"
|
|
917
|
+
].includes(kind)) {
|
|
918
|
+
return this.withRuntime(id, (runtime) => runtime.elementAction(kind, input));
|
|
919
|
+
}
|
|
920
|
+
if (kind === "type") return this.withRuntime(id, (runtime) => runtime.type(input, false));
|
|
921
|
+
if (kind === "fill") return this.withRuntime(id, (runtime) => runtime.type(input, true));
|
|
922
|
+
if (kind === "keyboard") return this.withRuntime(id, (runtime) => runtime.keyboard(input));
|
|
923
|
+
if (kind === "press") return this.withRuntime(id, (runtime) => runtime.press(input));
|
|
924
|
+
if (kind === "select") return this.withRuntime(id, (runtime) => runtime.select(input));
|
|
925
|
+
if (kind === "drag") return this.withRuntime(id, (runtime) => runtime.drag(input));
|
|
926
|
+
if (kind === "wait") return this.withRuntime(id, (runtime) => runtime.waitFor(input));
|
|
927
|
+
if (kind === "get") return this.withRuntime(id, (runtime) => runtime.get(input));
|
|
928
|
+
if (kind === "is") return this.withRuntime(id, (runtime) => runtime.is(input));
|
|
929
|
+
if (kind === "find") return this.withRuntime(id, (runtime) => runtime.find(input));
|
|
930
|
+
if (kind === "eval") return this.withRuntime(id, (runtime) => runtime.evaluate(input));
|
|
931
|
+
if (kind === "console" || kind === "errors") {
|
|
932
|
+
return this.withRuntime(id, (runtime) => runtime.debugLog(kind, input));
|
|
933
|
+
}
|
|
934
|
+
if (kind === "network") return this.withRuntime(id, (runtime) => runtime.network(input));
|
|
935
|
+
if (kind === "cookies") return this.withRuntime(id, (runtime) => runtime.cookies(input));
|
|
936
|
+
if (kind === "storage") return this.withRuntime(id, (runtime) => runtime.storage(input));
|
|
937
|
+
if (kind === "frame") return this.withRuntime(id, (runtime) => runtime.frame(input));
|
|
938
|
+
if (kind === "dialog") return this.withRuntime(id, (runtime) => runtime.dialog(input));
|
|
939
|
+
if (kind === "upload") {
|
|
940
|
+
const resource = this.requireStored(id);
|
|
941
|
+
const requestedFiles = Array.isArray(input?.files) ? input.files : [];
|
|
942
|
+
if (requestedFiles.length === 0 || requestedFiles.length > MAX_UPLOAD_FILES) {
|
|
943
|
+
throw browserError(`Browser upload requires between 1 and ${MAX_UPLOAD_FILES} files`);
|
|
944
|
+
}
|
|
945
|
+
const files = requestedFiles.map((file) => resolveWorkspaceInputFile(resource, file));
|
|
946
|
+
return this.withRuntime(id, (runtime) => runtime.upload({ ...input, files }));
|
|
947
|
+
}
|
|
948
|
+
if (kind === "download") {
|
|
949
|
+
const resource = this.requireStored(id);
|
|
950
|
+
const target = resolveWorkspaceOutputFile(resource, input?.path);
|
|
951
|
+
return this.withRuntime(id, async (runtime) => {
|
|
952
|
+
const resourceDir = path.dirname(storageLayout.browserProfileDir(
|
|
953
|
+
this.configDir,
|
|
954
|
+
resource.sessionId || id
|
|
955
|
+
));
|
|
956
|
+
const downloadDir = path.join(resourceDir, "downloads");
|
|
957
|
+
fs.mkdirSync(downloadDir, { recursive: true, mode: 448 });
|
|
958
|
+
const temporaryPath = path.join(
|
|
959
|
+
downloadDir,
|
|
960
|
+
`${crypto.randomUUID()}-${path.basename(target)}`
|
|
961
|
+
);
|
|
962
|
+
try {
|
|
963
|
+
await runtime.download({ ...input, outputPath: temporaryPath });
|
|
964
|
+
const stat = fs.statSync(temporaryPath);
|
|
965
|
+
if (!stat.isFile()) throw browserError("Browser download did not produce a regular file");
|
|
966
|
+
fs.copyFileSync(temporaryPath, target, fs.constants.COPYFILE_EXCL);
|
|
967
|
+
return {
|
|
968
|
+
ok: true,
|
|
969
|
+
path: path.relative(resource.workspace, target) || path.basename(target),
|
|
970
|
+
size: stat.size
|
|
971
|
+
};
|
|
972
|
+
} finally {
|
|
973
|
+
fs.rmSync(temporaryPath, { force: true });
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
if (kind === "scroll") return this.withRuntime(id, async (runtime) => {
|
|
978
|
+
await runtime.wheel(input);
|
|
979
|
+
return { ok: true };
|
|
980
|
+
});
|
|
981
|
+
throw browserError(`Unsupported Browser action: ${kind || "(missing)"}`);
|
|
982
|
+
}
|
|
983
|
+
attachViewer(id, ws) {
|
|
984
|
+
this.requireEnabled();
|
|
985
|
+
const resource = this.requireStored(id);
|
|
986
|
+
const binding = this.runtimes.get(id);
|
|
987
|
+
ws.send(JSON.stringify({
|
|
988
|
+
type: "browser-state",
|
|
989
|
+
resource: publicResource(resource, this.store.revision)
|
|
990
|
+
}));
|
|
991
|
+
if (!binding || resource.status !== "running") return () => {
|
|
992
|
+
};
|
|
993
|
+
binding.viewers.add(ws);
|
|
994
|
+
if (binding.latestFrame) ws.send(JSON.stringify(binding.latestFrame));
|
|
995
|
+
void this.withRuntime(id, () => {
|
|
996
|
+
}).catch((error) => {
|
|
997
|
+
if (ws.readyState === 1) {
|
|
998
|
+
ws.send(JSON.stringify({ type: "browser-error", message: errorMessage(error) || "Browser tab failed" }));
|
|
999
|
+
}
|
|
1000
|
+
});
|
|
1001
|
+
const onMessage = (raw) => {
|
|
1002
|
+
let message;
|
|
1003
|
+
try {
|
|
1004
|
+
message = JSON.parse(raw.toString());
|
|
1005
|
+
} catch {
|
|
1006
|
+
return;
|
|
1007
|
+
}
|
|
1008
|
+
const operation = message.type === "resize" ? this.scheduleViewerResize(binding, ws, message) : this.handleViewerMessage(binding, ws, message);
|
|
1009
|
+
void Promise.resolve(operation).catch((error) => {
|
|
1010
|
+
if (ws.readyState === 1) {
|
|
1011
|
+
ws.send(JSON.stringify({ type: "browser-error", message: errorMessage(error) || "Browser input failed" }));
|
|
1012
|
+
}
|
|
1013
|
+
});
|
|
1014
|
+
};
|
|
1015
|
+
ws.on("message", onMessage);
|
|
1016
|
+
const detach = () => {
|
|
1017
|
+
binding.viewers.delete(ws);
|
|
1018
|
+
binding.viewerGeometries.delete(ws);
|
|
1019
|
+
if (binding.viewerViewportOwner === ws) {
|
|
1020
|
+
binding.viewerViewportOwner = binding.viewers.values().next().value || null;
|
|
1021
|
+
if (binding.viewerViewportOwner) {
|
|
1022
|
+
const geometry = binding.viewerGeometries.get(binding.viewerViewportOwner);
|
|
1023
|
+
if (geometry) {
|
|
1024
|
+
void this.scheduleViewerResize(binding, binding.viewerViewportOwner, geometry).catch(() => {
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
} else {
|
|
1028
|
+
this.clearViewerResize(binding);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
ws.off("message", onMessage);
|
|
1032
|
+
};
|
|
1033
|
+
ws.once("close", detach);
|
|
1034
|
+
return detach;
|
|
1035
|
+
}
|
|
1036
|
+
handleViewerMessage(binding, viewer, message) {
|
|
1037
|
+
const resource = this.requireStored(binding.id);
|
|
1038
|
+
if (resource.status !== "running" || this.stopAdmissions.has(binding.id) || message.generation !== binding.generation || this.runtimes.get(binding.id) !== binding) {
|
|
1039
|
+
return Promise.reject(browserError(
|
|
1040
|
+
"Browser Viewer input is no longer admitted",
|
|
1041
|
+
409,
|
|
1042
|
+
"BROWSER_NOT_RUNNING"
|
|
1043
|
+
));
|
|
1044
|
+
}
|
|
1045
|
+
const { session } = binding;
|
|
1046
|
+
const next = (session.actionChain || Promise.resolve()).catch(() => {
|
|
1047
|
+
}).then(async () => {
|
|
1048
|
+
await this.activateBinding(binding);
|
|
1049
|
+
return this.performViewerMessage(binding, viewer, message);
|
|
1050
|
+
});
|
|
1051
|
+
session.actionChain = next;
|
|
1052
|
+
return next;
|
|
1053
|
+
}
|
|
1054
|
+
scheduleViewerResize(binding, viewer, message) {
|
|
1055
|
+
const resource = this.requireStored(binding.id);
|
|
1056
|
+
if (resource.status !== "running" || this.stopAdmissions.has(binding.id) || message.generation !== binding.generation || this.runtimes.get(binding.id) !== binding) {
|
|
1057
|
+
return Promise.reject(browserError(
|
|
1058
|
+
"Browser Viewer resize is no longer admitted",
|
|
1059
|
+
409,
|
|
1060
|
+
"BROWSER_NOT_RUNNING"
|
|
1061
|
+
));
|
|
1062
|
+
}
|
|
1063
|
+
const width = Math.round(Number(message.width));
|
|
1064
|
+
const height = Math.round(Number(message.height));
|
|
1065
|
+
const requestedDeviceScaleFactor = Number(message.deviceScaleFactor);
|
|
1066
|
+
if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) {
|
|
1067
|
+
return Promise.reject(browserError("Browser Viewer size is invalid"));
|
|
1068
|
+
}
|
|
1069
|
+
const deviceScaleFactor = Number.isFinite(requestedDeviceScaleFactor) ? Math.max(1, Math.min(2, requestedDeviceScaleFactor)) : 1;
|
|
1070
|
+
const geometry = {
|
|
1071
|
+
type: "resize",
|
|
1072
|
+
generation: binding.generation,
|
|
1073
|
+
width,
|
|
1074
|
+
height,
|
|
1075
|
+
deviceScaleFactor
|
|
1076
|
+
};
|
|
1077
|
+
binding.viewerGeometries.set(viewer, geometry);
|
|
1078
|
+
if (message.claim === true || !binding.viewerViewportOwner || !binding.viewers.has(binding.viewerViewportOwner)) {
|
|
1079
|
+
binding.viewerViewportOwner = viewer;
|
|
1080
|
+
}
|
|
1081
|
+
if (binding.viewerViewportOwner !== viewer) return Promise.resolve();
|
|
1082
|
+
binding.pendingViewerResize = { viewer, geometry };
|
|
1083
|
+
if (binding.viewerResizeTimer) this.cancelTimeout(binding.viewerResizeTimer);
|
|
1084
|
+
binding.viewerResizeTimer = this.scheduleTimeout(() => {
|
|
1085
|
+
binding.viewerResizeTimer = null;
|
|
1086
|
+
const pending = binding.pendingViewerResize;
|
|
1087
|
+
binding.pendingViewerResize = null;
|
|
1088
|
+
if (!pending || binding.viewerViewportOwner !== pending.viewer) return;
|
|
1089
|
+
void this.handleViewerMessage(binding, pending.viewer, pending.geometry).catch((error) => {
|
|
1090
|
+
if (pending.viewer.readyState === 1) {
|
|
1091
|
+
pending.viewer.send(JSON.stringify({
|
|
1092
|
+
type: "browser-error",
|
|
1093
|
+
message: errorMessage(error) || "Browser resize failed"
|
|
1094
|
+
}));
|
|
1095
|
+
}
|
|
1096
|
+
});
|
|
1097
|
+
}, VIEWER_RESIZE_SETTLE_MS);
|
|
1098
|
+
binding.viewerResizeTimer.unref?.();
|
|
1099
|
+
return Promise.resolve();
|
|
1100
|
+
}
|
|
1101
|
+
clearViewerResize(runtime) {
|
|
1102
|
+
if (runtime.viewerResizeTimer) this.cancelTimeout(runtime.viewerResizeTimer);
|
|
1103
|
+
runtime.viewerResizeTimer = null;
|
|
1104
|
+
runtime.pendingViewerResize = null;
|
|
1105
|
+
}
|
|
1106
|
+
releaseViewerState(runtime) {
|
|
1107
|
+
this.clearViewerResize(runtime);
|
|
1108
|
+
runtime.viewerGeometries?.clear?.();
|
|
1109
|
+
runtime.viewerViewportOwner = null;
|
|
1110
|
+
runtime.viewers?.clear?.();
|
|
1111
|
+
}
|
|
1112
|
+
async performViewerMessage(binding, viewer, message) {
|
|
1113
|
+
if (message.generation !== binding.generation || this.runtimes.get(binding.id) !== binding) {
|
|
1114
|
+
throw browserError("Browser Viewer generation is stale", 409, "BROWSER_STALE_GENERATION");
|
|
1115
|
+
}
|
|
1116
|
+
const runtime = binding.session.runtime;
|
|
1117
|
+
if (message.type === "resize") {
|
|
1118
|
+
if (binding.viewerViewportOwner !== viewer) return;
|
|
1119
|
+
if (typeof message.width !== "number" || typeof message.height !== "number" || typeof message.deviceScaleFactor !== "number") {
|
|
1120
|
+
throw browserError("Browser Viewer size is invalid");
|
|
1121
|
+
}
|
|
1122
|
+
await runtime.resize({
|
|
1123
|
+
width: message.width,
|
|
1124
|
+
height: message.height,
|
|
1125
|
+
deviceScaleFactor: message.deviceScaleFactor
|
|
1126
|
+
});
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
1129
|
+
if (message.type === "pointer") {
|
|
1130
|
+
await runtime.pointer(message);
|
|
1131
|
+
return;
|
|
1132
|
+
}
|
|
1133
|
+
if (message.type === "wheel") {
|
|
1134
|
+
await runtime.wheel(message);
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
if (message.type === "key") {
|
|
1138
|
+
await runtime.press(message);
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
if (message.type === "text") {
|
|
1142
|
+
await runtime.insertText(String(message.text || ""));
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
async dispose() {
|
|
1146
|
+
this.disposed = true;
|
|
1147
|
+
const sessions = [...this.sessions.values()];
|
|
1148
|
+
const results = await Promise.allSettled(sessions.map(async (session) => {
|
|
1149
|
+
await session.runtime.close();
|
|
1150
|
+
if (session.isolatedLeaseKey && this.isolatedBrowserProvider) {
|
|
1151
|
+
await this.isolatedBrowserProvider.release(session.isolatedLeaseKey);
|
|
1152
|
+
session.isolatedLeaseKey = "";
|
|
1153
|
+
}
|
|
1154
|
+
}));
|
|
1155
|
+
const failures = results.filter((result) => result.status === "rejected").map((result) => result.reason?.message || String(result.reason));
|
|
1156
|
+
if (failures.length > 0) {
|
|
1157
|
+
throw new Error(`Browser runtime cleanup failed: ${failures.join("; ")}`);
|
|
1158
|
+
}
|
|
1159
|
+
for (const binding of this.runtimes.values()) this.releaseViewerState(binding);
|
|
1160
|
+
this.runtimes.clear();
|
|
1161
|
+
this.sessions.clear();
|
|
1162
|
+
}
|
|
1163
|
+
async stopAll() {
|
|
1164
|
+
const ids = [...this.runtimes.keys()];
|
|
1165
|
+
const results = [];
|
|
1166
|
+
for (const id of ids) {
|
|
1167
|
+
try {
|
|
1168
|
+
results.push({ status: "fulfilled", value: await this.stop(id) });
|
|
1169
|
+
} catch (reason) {
|
|
1170
|
+
results.push({ status: "rejected", reason });
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
const failures = results.filter((result) => result.status === "rejected").map((result) => errorMessage(result.reason));
|
|
1174
|
+
if (failures.length > 0) {
|
|
1175
|
+
throw browserError(
|
|
1176
|
+
`Browser extension could not stop every running Browser: ${failures.join("; ")}`,
|
|
1177
|
+
500,
|
|
1178
|
+
"BROWSER_DISABLE_FAILED"
|
|
1179
|
+
);
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
async reconcileAgentLifecycle(agentStates) {
|
|
1183
|
+
const agents = new Map(agentStates.map((agent) => [String(agent.id || ""), agent]));
|
|
1184
|
+
const resources = this.store.list().filter((resource) => resource.ownerType === "agent");
|
|
1185
|
+
for (const resource of resources) {
|
|
1186
|
+
const owner = agents.get(resource.ownerAgentId);
|
|
1187
|
+
if (!owner) {
|
|
1188
|
+
await this.delete(resource.id, true);
|
|
1189
|
+
continue;
|
|
1190
|
+
}
|
|
1191
|
+
const preservesBrowserRuntime = ["permission-restart", "runtime-switch"].includes(
|
|
1192
|
+
String(owner.lifecycleOperation?.type || "")
|
|
1193
|
+
);
|
|
1194
|
+
const ownerStopped = !preservesBrowserRuntime && (owner.archived === true || INACTIVE_AGENT_STATUSES.has(String(owner.status || "")));
|
|
1195
|
+
if (ownerStopped && (this.runtimes.has(resource.id) || ["running", "starting", "stopping"].includes(resource.status) || Boolean(resource.processIdentity))) {
|
|
1196
|
+
await this.stop(resource.id, true);
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
requireEnabled() {
|
|
1201
|
+
if (this.isEnabled() !== true) {
|
|
1202
|
+
throw browserError("Browser extension is disabled", 409, "BROWSER_EXTENSION_DISABLED");
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
requireAvailable() {
|
|
1206
|
+
this.requireEnabled();
|
|
1207
|
+
const executable = this.runtimeCapability;
|
|
1208
|
+
if (!executable || executable.error) {
|
|
1209
|
+
throw browserError(
|
|
1210
|
+
executable?.error || "Choose a local Chromium browser or prepare the isolated Browser runtime",
|
|
1211
|
+
503,
|
|
1212
|
+
"BROWSER_EXECUTABLE_NOT_FOUND"
|
|
1213
|
+
);
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
requireStored(id) {
|
|
1217
|
+
if (!import_browser_resource_store.RESOURCE_ID_RE.test(String(id || ""))) {
|
|
1218
|
+
throw browserError("Invalid Browser resource id");
|
|
1219
|
+
}
|
|
1220
|
+
const resource = this.store.get(id);
|
|
1221
|
+
if (!resource) throw browserError("Browser resource not found", 404, "BROWSER_NOT_FOUND");
|
|
1222
|
+
return resource;
|
|
1223
|
+
}
|
|
1224
|
+
enqueue(id, operation) {
|
|
1225
|
+
const previous = this.operations.get(id) || Promise.resolve();
|
|
1226
|
+
const next = previous.catch(() => {
|
|
1227
|
+
}).then(operation);
|
|
1228
|
+
this.operations.set(id, next);
|
|
1229
|
+
return next.finally(() => {
|
|
1230
|
+
if (this.operations.get(id) === next) this.operations.delete(id);
|
|
1231
|
+
});
|
|
1232
|
+
}
|
|
1233
|
+
createBinding(session, resource) {
|
|
1234
|
+
return {
|
|
1235
|
+
id: resource.id,
|
|
1236
|
+
generation: resource.generation,
|
|
1237
|
+
session,
|
|
1238
|
+
tabId: resource.tabId || "",
|
|
1239
|
+
viewers: /* @__PURE__ */ new Set(),
|
|
1240
|
+
viewerGeometries: /* @__PURE__ */ new Map(),
|
|
1241
|
+
viewerViewportOwner: null,
|
|
1242
|
+
viewerResizeTimer: null,
|
|
1243
|
+
pendingViewerResize: null,
|
|
1244
|
+
latestFrame: null
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
async activateBinding(binding) {
|
|
1248
|
+
const { session } = binding;
|
|
1249
|
+
if (!binding.tabId) throw browserError("Browser tab is unavailable", 409, "BROWSER_TAB_UNAVAILABLE");
|
|
1250
|
+
if (session.runtime.activeTabId !== binding.tabId || session.runtime.streamTabId !== binding.tabId) {
|
|
1251
|
+
await session.runtime.switchTab(binding.tabId);
|
|
1252
|
+
}
|
|
1253
|
+
session.activeResourceId = binding.id;
|
|
1254
|
+
}
|
|
1255
|
+
withRuntime(id, operation) {
|
|
1256
|
+
const binding = this.runtimes.get(id);
|
|
1257
|
+
const resource = this.requireStored(id);
|
|
1258
|
+
if (!binding || resource.status !== "running") {
|
|
1259
|
+
throw browserError("Browser is not running", 409, "BROWSER_NOT_RUNNING");
|
|
1260
|
+
}
|
|
1261
|
+
if (this.stopAdmissions.has(id)) {
|
|
1262
|
+
throw browserError("Browser is stopping", 409, "BROWSER_STOPPING");
|
|
1263
|
+
}
|
|
1264
|
+
if (resource.generation !== binding.generation || resource.sessionId !== binding.session.id || resource.sessionGeneration !== binding.session.generation || resource.tabId !== binding.tabId) {
|
|
1265
|
+
throw browserError(
|
|
1266
|
+
"Browser runtime ownership changed; refresh the Browser Resource before retrying",
|
|
1267
|
+
409,
|
|
1268
|
+
"BROWSER_STALE_GENERATION"
|
|
1269
|
+
);
|
|
1270
|
+
}
|
|
1271
|
+
const { session } = binding;
|
|
1272
|
+
const next = (session.actionChain || Promise.resolve()).catch(() => {
|
|
1273
|
+
}).then(async () => {
|
|
1274
|
+
await this.activateBinding(binding);
|
|
1275
|
+
return operation(session.runtime, binding);
|
|
1276
|
+
});
|
|
1277
|
+
session.actionChain = next;
|
|
1278
|
+
return next;
|
|
1279
|
+
}
|
|
1280
|
+
bindSession(session) {
|
|
1281
|
+
const { runtime } = session;
|
|
1282
|
+
runtime.on("process-identity", (processIdentity) => {
|
|
1283
|
+
const owner = this.store.get(session.processOwnerResourceId);
|
|
1284
|
+
if (this.sessions.get(session.id) !== session || !owner || owner.sessionId && owner.sessionId !== session.id) return;
|
|
1285
|
+
const next = this.store.update(owner.id, { processIdentity });
|
|
1286
|
+
this.emitResource(next);
|
|
1287
|
+
});
|
|
1288
|
+
runtime.on("frame", (frame) => {
|
|
1289
|
+
const binding = [...session.bindings.values()].find((candidate) => candidate.tabId === runtime.streamTabId) || session.bindings.get(session.activeResourceId);
|
|
1290
|
+
if (!binding) return;
|
|
1291
|
+
const resourceFrame = {
|
|
1292
|
+
...frame,
|
|
1293
|
+
generation: binding.generation
|
|
1294
|
+
};
|
|
1295
|
+
binding.latestFrame = resourceFrame;
|
|
1296
|
+
for (const viewer of binding.viewers) {
|
|
1297
|
+
if (viewer.readyState === 1 && (Number(viewer.bufferedAmount) || 0) <= MAX_VIEWER_BUFFER_BYTES) {
|
|
1298
|
+
viewer.send(JSON.stringify(resourceFrame));
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
});
|
|
1302
|
+
runtime.on("metadata", (metadata) => {
|
|
1303
|
+
const binding = [...session.bindings.values()].find((candidate) => candidate.tabId === runtime.activeTabId) || session.bindings.get(session.activeResourceId);
|
|
1304
|
+
if (binding) this.updateMetadata(binding, metadata);
|
|
1305
|
+
});
|
|
1306
|
+
runtime.on("tabs", (event) => {
|
|
1307
|
+
if (session.initializing || session.closing) return;
|
|
1308
|
+
const next = (session.actionChain || Promise.resolve()).catch(() => {
|
|
1309
|
+
}).then(() => this.reconcileTabs(session, event));
|
|
1310
|
+
session.actionChain = next;
|
|
1311
|
+
});
|
|
1312
|
+
runtime.on("error", (error) => {
|
|
1313
|
+
for (const binding of session.bindings.values()) {
|
|
1314
|
+
for (const viewer of binding.viewers) {
|
|
1315
|
+
if (viewer.readyState === 1) {
|
|
1316
|
+
viewer.send(JSON.stringify({ type: "browser-error", message: errorMessage(error) || "Browser runtime failed" }));
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
});
|
|
1321
|
+
runtime.once("exit", (message) => {
|
|
1322
|
+
void this.handleRuntimeExit(session, message);
|
|
1323
|
+
});
|
|
1324
|
+
}
|
|
1325
|
+
async reconcileTabs(session, event) {
|
|
1326
|
+
if (this.sessions.get(session.id) !== session || session.closing) return;
|
|
1327
|
+
const tabs = Array.isArray(event?.tabs) ? event.tabs.filter((tab) => tab.type === "page") : [];
|
|
1328
|
+
const newlyObservedTabIds = new Set(
|
|
1329
|
+
Array.isArray(event?.newTabIds) ? event.newTabIds.map(String) : []
|
|
1330
|
+
);
|
|
1331
|
+
const byTabId = new Map([...session.bindings.values()].map((binding) => [binding.tabId, binding]));
|
|
1332
|
+
const opener = session.bindings.get(session.activeResourceId) || null;
|
|
1333
|
+
const opened = [];
|
|
1334
|
+
for (const tab of tabs) {
|
|
1335
|
+
let binding = byTabId.get(tab.tabId);
|
|
1336
|
+
if (!binding && newlyObservedTabIds.has(tab.tabId) && (!session.runtime.externalCdpUrl || event.popupAdmitted)) {
|
|
1337
|
+
session.runtime.ownedTabIds.add(tab.tabId);
|
|
1338
|
+
const created = this.store.createRunningTab({
|
|
1339
|
+
projectRootId: opener?.session.projectRootId || session.projectRootId,
|
|
1340
|
+
ownerType: opener ? this.store.get(opener.id)?.ownerType : void 0,
|
|
1341
|
+
ownerAgentId: opener ? this.store.get(opener.id)?.ownerAgentId : void 0,
|
|
1342
|
+
workspace: (opener ? this.store.get(opener.id)?.workspace : void 0) || this.store.list().find((resource) => resource.sessionId === session.id)?.workspace,
|
|
1343
|
+
name: tabResourceName(tab),
|
|
1344
|
+
url: tab.url,
|
|
1345
|
+
title: tab.title,
|
|
1346
|
+
browserKind: session.browserKind,
|
|
1347
|
+
sessionId: session.id,
|
|
1348
|
+
sessionGeneration: session.generation,
|
|
1349
|
+
tabId: tab.tabId
|
|
1350
|
+
});
|
|
1351
|
+
binding = this.createBinding(session, created);
|
|
1352
|
+
session.bindings.set(created.id, binding);
|
|
1353
|
+
this.runtimes.set(created.id, binding);
|
|
1354
|
+
byTabId.set(tab.tabId, binding);
|
|
1355
|
+
this.emitResource(created);
|
|
1356
|
+
opened.push(publicResource(created, this.store.revision));
|
|
1357
|
+
}
|
|
1358
|
+
if (!binding) continue;
|
|
1359
|
+
const current = this.store.get(binding.id);
|
|
1360
|
+
if (!current || current.status !== "running") continue;
|
|
1361
|
+
if (current.url !== tab.url || current.title !== tab.title) {
|
|
1362
|
+
const updated = this.store.update(binding.id, {
|
|
1363
|
+
url: tab.url || current.url,
|
|
1364
|
+
title: tab.title || "",
|
|
1365
|
+
...current.autoName ? { name: tabResourceName(tab) } : {}
|
|
1366
|
+
});
|
|
1367
|
+
this.emitResource(updated);
|
|
1368
|
+
this.broadcastRuntimeState(binding);
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
const liveTabIds = new Set(tabs.map((tab) => tab.tabId));
|
|
1372
|
+
for (const binding of [...session.bindings.values()]) {
|
|
1373
|
+
if (liveTabIds.has(binding.tabId)) continue;
|
|
1374
|
+
const current = this.store.get(binding.id);
|
|
1375
|
+
if (!current || current.status === "stopping") continue;
|
|
1376
|
+
session.bindings.delete(binding.id);
|
|
1377
|
+
this.runtimes.delete(binding.id);
|
|
1378
|
+
if (session.processOwnerResourceId === binding.id && session.bindings.size > 0) {
|
|
1379
|
+
const nextOwner = session.bindings.values().next().value;
|
|
1380
|
+
if (!nextOwner) throw new Error("Browser Session lost its remaining resource owner");
|
|
1381
|
+
session.processOwnerResourceId = nextOwner.id;
|
|
1382
|
+
const transferred = this.store.update(nextOwner.id, {
|
|
1383
|
+
processIdentity: current.processIdentity
|
|
1384
|
+
});
|
|
1385
|
+
this.emitResource(transferred);
|
|
1386
|
+
this.broadcastRuntimeState(nextOwner);
|
|
1387
|
+
}
|
|
1388
|
+
const stopped = this.store.update(binding.id, {
|
|
1389
|
+
status: "stopped",
|
|
1390
|
+
tabId: "",
|
|
1391
|
+
processIdentity: null,
|
|
1392
|
+
error: ""
|
|
1393
|
+
});
|
|
1394
|
+
this.emitResource(stopped);
|
|
1395
|
+
this.broadcastRuntimeState(binding);
|
|
1396
|
+
this.releaseViewerState(binding);
|
|
1397
|
+
}
|
|
1398
|
+
const activeTab = tabs.find((tab) => tab.active);
|
|
1399
|
+
const activeBinding = activeTab ? byTabId.get(activeTab.tabId) : null;
|
|
1400
|
+
if (activeBinding) {
|
|
1401
|
+
session.activeResourceId = activeBinding.id;
|
|
1402
|
+
if (session.runtime.streamTabId !== activeBinding.tabId) {
|
|
1403
|
+
await session.runtime.switchTab(activeBinding.tabId);
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
if (opened.length > 0 && opener) {
|
|
1407
|
+
const message = JSON.stringify({
|
|
1408
|
+
type: "browser-tab-opened",
|
|
1409
|
+
resource: opened.at(-1)
|
|
1410
|
+
});
|
|
1411
|
+
for (const viewer of opener.viewers) {
|
|
1412
|
+
if (viewer.readyState === 1) viewer.send(message);
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
updateMetadata(binding, metadata) {
|
|
1417
|
+
const current = this.store.get(binding.id);
|
|
1418
|
+
if (!current || current.generation !== binding.generation || !metadata) return;
|
|
1419
|
+
const next = this.store.update(binding.id, {
|
|
1420
|
+
url: String(metadata.url || current.url),
|
|
1421
|
+
title: String(metadata.title || "")
|
|
1422
|
+
});
|
|
1423
|
+
this.emitResource(next);
|
|
1424
|
+
this.broadcastRuntimeState(binding);
|
|
1425
|
+
}
|
|
1426
|
+
emitResource(resource) {
|
|
1427
|
+
this.emit("resource", publicResource(resource, this.store.revision));
|
|
1428
|
+
}
|
|
1429
|
+
broadcastRuntimeState(binding) {
|
|
1430
|
+
const resource = this.store.get(binding.id);
|
|
1431
|
+
if (!resource) return;
|
|
1432
|
+
const message = JSON.stringify({
|
|
1433
|
+
type: "browser-state",
|
|
1434
|
+
resource: publicResource(resource, this.store.revision)
|
|
1435
|
+
});
|
|
1436
|
+
for (const viewer of binding.viewers || []) {
|
|
1437
|
+
if (viewer.readyState === 1) viewer.send(message);
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
async handleRuntimeExit(session, message) {
|
|
1441
|
+
if (this.sessions.get(session.id) !== session) return;
|
|
1442
|
+
const failedBindings = [...session.bindings.values()];
|
|
1443
|
+
for (const binding of failedBindings) {
|
|
1444
|
+
const current = this.store.get(binding.id);
|
|
1445
|
+
if (!current) continue;
|
|
1446
|
+
const failed = this.store.update(binding.id, {
|
|
1447
|
+
status: "failed",
|
|
1448
|
+
error: current.browserKind === "external-cdp" ? "External Browser connection exited" : current.browserKind === "isolated-computer" ? "Isolated Browser connection exited" : message || "Browser connection exited"
|
|
1449
|
+
});
|
|
1450
|
+
this.emitResource(failed);
|
|
1451
|
+
this.broadcastRuntimeState(binding);
|
|
1452
|
+
}
|
|
1453
|
+
try {
|
|
1454
|
+
await session.runtime.close();
|
|
1455
|
+
if (session.isolatedLeaseKey && this.isolatedBrowserProvider) {
|
|
1456
|
+
await this.isolatedBrowserProvider.release(session.isolatedLeaseKey);
|
|
1457
|
+
session.isolatedLeaseKey = "";
|
|
1458
|
+
}
|
|
1459
|
+
this.sessions.delete(session.id);
|
|
1460
|
+
for (const binding of failedBindings) {
|
|
1461
|
+
this.runtimes.delete(binding.id);
|
|
1462
|
+
this.releaseViewerState(binding);
|
|
1463
|
+
const cleaned = this.store.update(binding.id, { processIdentity: null });
|
|
1464
|
+
if (cleaned) this.emitResource(cleaned);
|
|
1465
|
+
}
|
|
1466
|
+
} catch (error) {
|
|
1467
|
+
for (const binding of failedBindings) {
|
|
1468
|
+
const current = this.store.get(binding.id);
|
|
1469
|
+
if (!current) continue;
|
|
1470
|
+
const cleanupFailed = this.store.update(binding.id, {
|
|
1471
|
+
status: "failed",
|
|
1472
|
+
error: current.browserKind === "external-cdp" ? `${current.error}; target cleanup failed` : `${current.error}; cleanup failed: ${errorMessage(error)}`
|
|
1473
|
+
});
|
|
1474
|
+
this.emitResource(cleanupFailed);
|
|
1475
|
+
this.broadcastRuntimeState(binding);
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1481
|
+
0 && (module.exports = {
|
|
1482
|
+
BrowserResourceManager,
|
|
1483
|
+
browserError,
|
|
1484
|
+
externalBrowserFailure,
|
|
1485
|
+
normalizeUrl
|
|
1486
|
+
});
|