mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
feat(browser-extension): add opencode-browser-cli with the relay and browse MCP server
packages/browser-extension/cli is the npm package `opencode-browser-cli`, with its own package.json and lockfile so its dependencies stay out of the monorepo's. It is Browser Control (anomalyco/browser-control 8c80ac2, MIT) ported into OpenCode Browser: renamed, on its own port (19988), accepting only OpenCode Browser's extension IDs, with files under ~/.local/share/opencode-browser. - The relay: a local CDP endpoint backed by the extension's chrome.debugger, shared by several clients, with named sessions that survive relay restarts and extension reloads, handoffs, recordings (CDP and tab capture), a flight recorder, network capture with a redacted HAR, secrets, and a journal. relay.log records lifecycle, connects (with the extension's build and origin), every HTTP request with its duration, and faults. - The `browse` MCP server (stdio) and the `opencode-browser` CLI: execute (Playwright API through patchright-core, which never enables CDP's Runtime domain, so pages can't detect the debugger that way; pageConsole.start() opts one page into console capture), session_* (session_adopt also takes a Chrome tab id), network_*, recording_*, flight_recorder_*, secrets_*, tabs_cleanup, status, skill. The agent guide is skills/browse/SKILL.md. - Human-like input (human-input.ts): curved, eased mouse paths, off-center clicks with a settle and hold, paced keys, gliding wheel scrolls. OPENCODE_BROWSER_HUMAN_INPUT=0 turns it off. - Setup (setup.ts): `install` installs a pinned runtime, adds the `browse` MCP server to opencode's global config (with a fingerprint so opencode restarts it on updates), restarts an outdated relay, copies the unpacked extension and reloads a connected unpacked build only when it is older (checking the reload took effect), then reports which browsers and profiles have the extension and opens its Web Store listing when none does (unless headless, --no-open, or a browser couldn't be read). It removes leftovers of earlier versions (native messaging host, opencode plugin, `opencode-browser` MCP entry). `extension` and `uninstall` round it out. Verified: cli typecheck and build; install on the user's machine (detected Helium/Ryan as default with the unpacked build, Chrome unreadable under macOS privacy protection, Arc without it; a second install neither restarts the relay nor reloads the extension); through the browse MCP tools in Helium: session_new, execute (goto, title, locators), screenshot, session_list, a CDP recording, session_delete.
This commit is contained in:
parent
0f0fd4243d
commit
982d17cdb8
65 changed files with 22824 additions and 0 deletions
2
packages/browser-extension/cli/.gitignore
vendored
Normal file
2
packages/browser-extension/cli/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
dist/
|
||||
extension/
|
||||
57
packages/browser-extension/cli/build.ts
Normal file
57
packages/browser-extension/cli/build.ts
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// Builds the published package: dist/cli.mjs (main.ts and everything it loads, with npm dependencies left
|
||||
// external and installed by npm) and extension/ (the built unpacked extension, keeping its manifest key so the
|
||||
// unpacked ID matches the host's allowed origins). Both carry this package's version.
|
||||
import { $ } from "bun"
|
||||
import { chmod, cp, rm } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
|
||||
const here = import.meta.dir
|
||||
const extension = path.resolve(here, "..")
|
||||
const pkg = await Bun.file(path.join(here, "package.json")).json()
|
||||
|
||||
const build = new Date().toISOString()
|
||||
await $`bun run build`.cwd(extension).env({ ...process.env, OPENCODE_BROWSER_BUILD: build }).quiet()
|
||||
await rm(path.join(here, "dist"), { recursive: true, force: true })
|
||||
await rm(path.join(here, "extension"), { recursive: true, force: true })
|
||||
|
||||
const result = await Bun.build({
|
||||
entrypoints: [path.join(here, "src/main.ts")],
|
||||
outdir: path.join(here, "dist"),
|
||||
naming: "cli.mjs",
|
||||
target: "node",
|
||||
format: "esm",
|
||||
// Runtime dependencies stay external; jsonc-parser (a dev dependency) is bundled.
|
||||
external: Object.keys(pkg.dependencies ?? {}),
|
||||
plugins: [
|
||||
{
|
||||
// jsonc-parser's default entry is UMD with runtime requires; its ESM build bundles cleanly.
|
||||
name: "jsonc-parser-esm",
|
||||
setup(build) {
|
||||
build.onResolve({ filter: /^jsonc-parser$/ }, () => ({
|
||||
path: path.join(path.dirname(Bun.resolveSync("jsonc-parser/package.json", here)), "lib/esm/main.js"),
|
||||
}))
|
||||
},
|
||||
},
|
||||
],
|
||||
define: {
|
||||
"globalThis.__OPENCODE_BROWSER_VERSION__": JSON.stringify(pkg.version),
|
||||
"globalThis.__OPENCODE_BROWSER_BUILD_ID__": JSON.stringify(new Date().toISOString()),
|
||||
},
|
||||
})
|
||||
if (!result.success) throw new AggregateError(result.logs, "build failed")
|
||||
const cli = path.join(here, "dist", "cli.mjs")
|
||||
const source = await Bun.file(cli).text()
|
||||
await Bun.write(cli, source.startsWith("#!") ? source : `#!/usr/bin/env node\n${source}`)
|
||||
await chmod(cli, 0o755)
|
||||
|
||||
await cp(path.join(extension, "dist"), path.join(here, "extension"), {
|
||||
recursive: true,
|
||||
filter: (file) => !file.endsWith(".map"),
|
||||
})
|
||||
const manifestFile = path.join(here, "extension", "manifest.json")
|
||||
const manifest = await Bun.file(manifestFile).json()
|
||||
manifest.version = pkg.version
|
||||
await Bun.write(manifestFile, JSON.stringify(manifest, null, 2) + "\n")
|
||||
// install compares this with the build a connected extension reports, to skip a reload that changes nothing.
|
||||
await Bun.write(path.join(here, "extension", "build.json"), JSON.stringify({ build }) + "\n")
|
||||
console.log(`built dist/cli.mjs and extension/ (${pkg.version})`)
|
||||
92
packages/browser-extension/cli/bun.lock
Normal file
92
packages/browser-extension/cli/bun.lock
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
{
|
||||
"lockfileVersion": 2,
|
||||
"configVersion": 1,
|
||||
"workspaces": {
|
||||
"": {
|
||||
"name": "opencode-browser-cli",
|
||||
"dependencies": {
|
||||
"@effect/platform-node": "4.0.1",
|
||||
"@effect/platform-node-shared": "4.0.1",
|
||||
"acorn": "8.18.0",
|
||||
"effect": "4.0.1",
|
||||
"patchright-core": "1.63.0",
|
||||
"pixelmatch": "7.2.0",
|
||||
"pngjs": "7.0.0",
|
||||
"ws": "8.22.0",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chrome": "0.3.4",
|
||||
"@types/node": "26.6.4",
|
||||
"@types/pngjs": "6.0.5",
|
||||
"@types/ws": "8.18.2",
|
||||
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
||||
"jsonc-parser": "3.3.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
"packages": {
|
||||
"@effect/platform-node": ["@effect/platform-node@4.0.1", "", { "dependencies": { "@effect/platform-node-shared": "^4.0.1", "undici": "^8.11.2" }, "peerDependencies": { "effect": "^4.0.1", "redis": ">=5.0.0 <7.0.0" } }, "sha512-FT0Fu5LO0/XP9zBpj0HqrtJNLluK4StNKqf6TSulUsjp8FPBsDVXGJiDuyoN5J2yVMT7tgvhxSwaDcObWVRIaQ=="],
|
||||
|
||||
"@effect/platform-node-shared": ["@effect/platform-node-shared@4.0.1", "", { "dependencies": { "@types/ws": "^8.18.1", "ws": "^8.22.0" }, "peerDependencies": { "effect": "^4.0.1" } }, "sha512-VanaejudEhKJ4mpmqnEzdncVUhN6sGSO9ZDND54mRxAlZxj+4dYAWjDTYAy9o4tJo9aALeP2U8cVcOFzEdSeLg=="],
|
||||
|
||||
"@redis/bloom": ["@redis/bloom@6.3.0", "", { "peerDependencies": { "@redis/client": "^6.3.0" } }, "sha512-NQ5poYpZr0jv6zazN1cy6JY5kIipKFZ3R9GTL4kOUTX3a9YAfY29U8bsE4lWhVQEM+G4V0BXVC57mubIdDijeA=="],
|
||||
|
||||
"@redis/client": ["@redis/client@6.3.0", "", { "peerDependencies": { "@node-rs/xxhash": "^1.1.0", "@opentelemetry/api": ">=1 <2" }, "optionalPeers": ["@node-rs/xxhash", "@opentelemetry/api"] }, "sha512-fHrjwGCBvAANGi+TuRuwKi83sIOzgHr8eh+Uf48w3fSX7Hhrz174NrWqfJhAC2vnN0d/tfaZpdr6+o6JC3QHwg=="],
|
||||
|
||||
"@redis/json": ["@redis/json@6.3.0", "", { "peerDependencies": { "@redis/client": "^6.3.0" } }, "sha512-yt6vRfPBXtW/3qm53cF81TSQpLMjWdW4/2E9WQ2GgbvsKkuCKCFQIcMiKlil1ZMnezK+IQuS795xKU5psbaegw=="],
|
||||
|
||||
"@redis/search": ["@redis/search@6.3.0", "", { "peerDependencies": { "@redis/client": "^6.3.0" } }, "sha512-RPXAZmKjZZG7Tm7m1w9keFdXzbL91PiSHQBfQRe9xwW40jNsTRbG0FwhIz4QlHPU4gKDAsCy8IQHj4yZl4H9rw=="],
|
||||
|
||||
"@redis/time-series": ["@redis/time-series@6.3.0", "", { "peerDependencies": { "@redis/client": "^6.3.0" } }, "sha512-+fJOB8mN1z5WRakCJNh5lVdz1KqvHGSOistsKeitD24QsCtPwyRQXWC0iq6T/i/ubLE/YcguZqPm8F598MKk4A=="],
|
||||
|
||||
"@types/chrome": ["@types/chrome@0.3.4", "", { "dependencies": { "@types/filesystem": "*", "@types/har-format": "*" } }, "sha512-dcySM5R3WAUVYFjylu6tvI4D03hPKjgAqk+tKZ/tm/vxL4mLgbnSsOEQ3J7V99qP/U3Oo9IORstJLmX2hPaDlA=="],
|
||||
|
||||
"@types/filesystem": ["@types/filesystem@0.0.36", "", { "dependencies": { "@types/filewriter": "*" } }, "sha512-vPDXOZuannb9FZdxgHnqSwAG/jvdGM8Wq+6N4D/d80z+D4HWH+bItqsZaVRQykAn6WEVeEkLm2oQigyHtgb0RA=="],
|
||||
|
||||
"@types/filewriter": ["@types/filewriter@0.0.33", "", {}, "sha512-xFU8ZXTw4gd358lb2jw25nxY9QAgqn2+bKKjKOYfNCzN4DKCFetK7sPtrlpg66Ywe3vWY9FNxprZawAh9wfJ3g=="],
|
||||
|
||||
"@types/har-format": ["@types/har-format@1.2.16", "", {}, "sha512-fluxdy7ryD3MV6h8pTfTYpy/xQzCFC7m89nOH9y94cNqJ1mDIDPut7MnRHI3F6qRmh/cT2fUjG1MLdCNb4hE9A=="],
|
||||
|
||||
"@types/node": ["@types/node@26.6.4", "", { "dependencies": { "undici-types": "~8.9.0" } }, "sha512-ldVPDCzj7fsaGZrLB0NuHuTvJcsNasysBAqMolr/cgxrLd1xbqxIr3XJiPnHHJUCxj5sNF1vnRj9aWnrVh5Jcg=="],
|
||||
|
||||
"@types/pngjs": ["@types/pngjs@6.0.5", "", { "dependencies": { "@types/node": "*" } }, "sha512-0k5eKfrA83JOZPppLtS2C7OUtyNAl2wKNxfyYl9Q5g9lPkgBl/9hNyAu6HuEH2J4XmIv2znEpkDd0SaZVxW6iQ=="],
|
||||
|
||||
"@types/ws": ["@types/ws@8.18.2", "", { "dependencies": { "@types/node": "*" } }, "sha512-67MQl+fpWKVTT1NYdnmo3U4sc/xPo/zQBncVnI74qmQa0z/b+1g6iYqNmGCPbxO+zz2aklb08a0oHfegiVd0/w=="],
|
||||
|
||||
"@typescript/native-preview": ["@typescript/native-preview@7.0.0-dev.20260707.2", "", { "optionalDependencies": { "@typescript/native-preview-darwin-arm64": "7.0.0-dev.20260707.2", "@typescript/native-preview-darwin-x64": "7.0.0-dev.20260707.2", "@typescript/native-preview-linux-arm": "7.0.0-dev.20260707.2", "@typescript/native-preview-linux-arm64": "7.0.0-dev.20260707.2", "@typescript/native-preview-linux-x64": "7.0.0-dev.20260707.2", "@typescript/native-preview-win32-arm64": "7.0.0-dev.20260707.2", "@typescript/native-preview-win32-x64": "7.0.0-dev.20260707.2" }, "bin": { "tsgo": "bin/tsgo" } }, "sha512-oUGp+Rep/hqMhPunyinsALUwSlzHINSxitifPiSaeqoKOKD2OlR9NE3TaPqwsl4NlGslsOSUXI1JotWQzpYCPg=="],
|
||||
|
||||
"@typescript/native-preview-darwin-arm64": ["@typescript/native-preview-darwin-arm64@7.0.0-dev.20260707.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-wny2pgKjGbiZtnOIHVa3tXC1UfDqxNEFzyPGmiqybedG8hipG2Nfp0l5UxbaKCjkLacUpH/W5bP2hBOMVhCOzg=="],
|
||||
|
||||
"@typescript/native-preview-darwin-x64": ["@typescript/native-preview-darwin-x64@7.0.0-dev.20260707.2", "", { "os": "darwin", "cpu": "x64" }, "sha512-Afc7M5zOwo+GpfcYwz5Z8HMB2tPVsui7nNIqEuuFB73MPdVqNn/Wmpe4tP4MRri0AtJnJknoHBaTJ/VDAp/Jhw=="],
|
||||
|
||||
"@typescript/native-preview-linux-arm": ["@typescript/native-preview-linux-arm@7.0.0-dev.20260707.2", "", { "os": "linux", "cpu": "arm" }, "sha512-hJm/UOqZTr9FHmR7uNm8VGX4oKtfWk0Jem0zPeJFNC8ckGUfSBueyiEYMZB+XmRc1aG4x1E46y3CplP4CLHvGQ=="],
|
||||
|
||||
"@typescript/native-preview-linux-arm64": ["@typescript/native-preview-linux-arm64@7.0.0-dev.20260707.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-iITBa2WjjTI5N9t5l7Z4KoOSI+2zBlhbvFzsD/f8qX8QoKjz/Y4DPyBDgezYi8nkqjjksbgSOJ3/ykzhwrB9cg=="],
|
||||
|
||||
"@typescript/native-preview-linux-x64": ["@typescript/native-preview-linux-x64@7.0.0-dev.20260707.2", "", { "os": "linux", "cpu": "x64" }, "sha512-du0dzi6y97Po5vDNdPJTyyijHCpaS22JLRnKZEJXBDaO9gCIymOv/5QQokFRuOlQm0bWl3i9PF4OVdGP6uAOQA=="],
|
||||
|
||||
"@typescript/native-preview-win32-arm64": ["@typescript/native-preview-win32-arm64@7.0.0-dev.20260707.2", "", { "os": "win32", "cpu": "arm64" }, "sha512-SsAwfhyHJ1akgBc+99z4+hwdbHsdWaKB8EwCNIMA6JfSLMeUjffrYvxu+vfMyxVtOVOz7RrRXRoiDiu4a2sCtg=="],
|
||||
|
||||
"@typescript/native-preview-win32-x64": ["@typescript/native-preview-win32-x64@7.0.0-dev.20260707.2", "", { "os": "win32", "cpu": "x64" }, "sha512-DL4u27stv0fo71sVhOzHSwE+YMZsbBijVI+kg5dLDLilSH79WFTJ8RSQ46vJrCMt+Gjlv/JOZP1PuLJDfioYeQ=="],
|
||||
|
||||
"acorn": ["acorn@8.18.0", "", { "bin": { "acorn": "bin/acorn" } }, "sha512-lGq+9yr1/GuAWaVYIHRjvvySG5/4VfKIvC8EWxStPdcDh/Ka7FG3twP6v4d5BkravUilhIAsG4Qj83t02LWUPQ=="],
|
||||
|
||||
"effect": ["effect@4.0.1", "", {}, "sha512-b1VlQG9g8fwxE5QnIZuPoOP/0MsqHJSRKxUijjoM80E5q95FXrX5yWtY5XI8G+GJsBQAVSDbLS458z9a++uVvw=="],
|
||||
|
||||
"jsonc-parser": ["jsonc-parser@3.3.1", "", {}, "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ=="],
|
||||
|
||||
"patchright-core": ["patchright-core@1.63.0", "", { "bin": { "patchright-core": "cli.js" } }, "sha512-nuRJVTMVKoOFzY8vmwFqJZR3F0trUCKiPzDSgCOPq/yHHTefm9czILV23dXJsi5mvS7sCy7U1CqG7edA/21DtQ=="],
|
||||
|
||||
"pixelmatch": ["pixelmatch@7.2.0", "", { "dependencies": { "pngjs": "^7.0.0" }, "bin": { "pixelmatch": "bin/pixelmatch" } }, "sha512-xhcb4yHu9sM/G7foGzoLtXYcC0zHEaOXXjRKhGup0fw78Nf2Tkiapv4EQyMzrbcmQPsllAI7DbFY2UT7PlI9Pg=="],
|
||||
|
||||
"pngjs": ["pngjs@7.0.0", "", {}, "sha512-LKWqWJRhstyYo9pGvgor/ivk2w94eSjE3RGVuzLGlr3NmD8bf7RcYGze1mNdEHRP6TRP6rMuDHk5t44hnTRyow=="],
|
||||
|
||||
"redis": ["redis@6.3.0", "", { "dependencies": { "@redis/bloom": "6.3.0", "@redis/client": "6.3.0", "@redis/json": "6.3.0", "@redis/search": "6.3.0", "@redis/time-series": "6.3.0" } }, "sha512-XFQbPie1lGpKeUZ8ySYY43yLnQy/iIQtHNvIr/1XdaABpb96rUSdm3Mfl+98FEVbv9ZJIzINMno5s0Nh8E5LEw=="],
|
||||
|
||||
"undici": ["undici@8.11.2", "", {}, "sha512-u4UB2/IrKdU6lFxumHmmo1a3fCQO5tzQllRorfoRS63txhrB7xTpSn1PftwC4qEHkOaqP95fCWW4lJzwErwzhQ=="],
|
||||
|
||||
"undici-types": ["undici-types@8.9.0", "", {}, "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg=="],
|
||||
|
||||
"ws": ["ws@8.22.0", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-Ydggc987+RO0AnWtZ/7Wq9FtNvcrL1b/RO0ud9mWjUPgDrsAAwQSF51sm2hm1XofbU/4jkpGEsLFsZZxU+1DOg=="],
|
||||
}
|
||||
}
|
||||
47
packages/browser-extension/cli/package.json
Normal file
47
packages/browser-extension/cli/package.json
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
{
|
||||
"name": "opencode-browser-cli",
|
||||
"version": "0.0.2",
|
||||
"description": "OpenCode Browser: connect the extension to opencode, and drive your browser from any agent (CLI, MCP, Playwright).",
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"opencode-browser": "dist/cli.mjs",
|
||||
"opencode-browser-cli": "dist/cli.mjs"
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"extension",
|
||||
"skills"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=22.19.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/anomalyco/opencode.git",
|
||||
"directory": "packages/browser-extension/cli"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "bun build.ts",
|
||||
"typecheck": "tsgo --noEmit -p tsconfig.json",
|
||||
"prepublishOnly": "bun build.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@effect/platform-node": "4.0.1",
|
||||
"@effect/platform-node-shared": "4.0.1",
|
||||
"acorn": "8.18.0",
|
||||
"effect": "4.0.1",
|
||||
"pixelmatch": "7.2.0",
|
||||
"patchright-core": "1.63.0",
|
||||
"pngjs": "7.0.0",
|
||||
"ws": "8.22.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chrome": "0.3.4",
|
||||
"@types/node": "26.6.4",
|
||||
"@types/pngjs": "6.0.5",
|
||||
"@types/ws": "8.18.2",
|
||||
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
||||
"jsonc-parser": "3.3.1"
|
||||
}
|
||||
}
|
||||
686
packages/browser-extension/cli/skills/browse/SKILL.md
Normal file
686
packages/browser-extension/cli/skills/browse/SKILL.md
Normal file
|
|
@ -0,0 +1,686 @@
|
|||
---
|
||||
name: browse
|
||||
description: Browse with the user's existing Chromium-family browser (OpenCode Browser) using deterministic Playwright. Use when asked to inspect, automate, test, or interact with a visible browser tab; continue an authenticated browser workflow; handle 2FA, passkeys, CAPTCHAs, or payment confirmation; record browser behavior; or capture an authenticated network flow.
|
||||
---
|
||||
|
||||
# browse (OpenCode Browser)
|
||||
|
||||
The `browse` tools (MCP server `browse`; the same commands as the `opencode-browser`
|
||||
CLI) drive the user's browser through the OpenCode Browser extension.
|
||||
OpenCode Browser is a **driver**, not an agent. The calling agent decides what to
|
||||
do; OpenCode Browser runs deterministic Playwright code in the user's visible
|
||||
browser.
|
||||
|
||||
Use one loop throughout: **inspect, act, verify**. Inspect the real page before
|
||||
choosing locators, act through the narrowest stable control, then verify the
|
||||
result through a URL or fresh page read. Never treat a successful click or human
|
||||
acknowledgment as proof that the task succeeded.
|
||||
|
||||
## Core Workflow
|
||||
|
||||
### 1. Run The Task Directly
|
||||
|
||||
Start with the requested browser work. Relay-backed commands start the detached
|
||||
relay and wait for the extension; do not start `opencode-browser serve` first.
|
||||
|
||||
```bash
|
||||
opencode-browser execute 'return { url: page.url(), title: await page.title() }'
|
||||
```
|
||||
|
||||
Use `opencode-browser doctor` only when setup or runtime behavior is unclear.
|
||||
`status` and `doctor` are observational and never start the relay.
|
||||
|
||||
MCP startup, tool discovery, `skill`, and `session_current` do not contact the
|
||||
relay. The first operational tool call starts it if needed; relay-backed
|
||||
observational tools report unavailability instead of starting it.
|
||||
|
||||
For an externally supervised relay, set `OPENCODE_BROWSER_AUTOSTART=false` to
|
||||
make ordinary calls fail when that relay is absent instead of launching another
|
||||
process. Existing relay connections and explicit `relay restart` still work.
|
||||
|
||||
Ordinary CLI/MCP/SDK calls never replace a running relay. On a build mismatch,
|
||||
coordinate with other agents before running `opencode-browser relay restart`.
|
||||
It preserves browser tabs and durable sessions but resets JavaScript state and
|
||||
snapshot refs. A busy or timed-out drain leaves the old relay running; finish
|
||||
recordings/captures and disconnect raw CDP clients rather than forcing a stop.
|
||||
|
||||
```bash
|
||||
opencode-browser doctor
|
||||
opencode-browser status --json
|
||||
```
|
||||
|
||||
Completion: one execute returns a page result and a readable session id, or
|
||||
`doctor` identifies the concrete setup failure.
|
||||
|
||||
### 2. Choose The Page Deliberately
|
||||
|
||||
DevTools-attached tabs
|
||||
are excluded from OpenCode Browser's inventory. `page.title()` reads time out
|
||||
after five seconds if the page execution context remains unavailable; the read
|
||||
timeout does not close or replace the tab.
|
||||
|
||||
A bare CLI execute creates a fresh session-owned page and prints the exact
|
||||
`--session <id>` continuation command, or you can pass a descriptive session
|
||||
name (including an optional emoji, which appears directly on the browser tab
|
||||
group and in-page status pill). Every later CLI call must pass that id or set
|
||||
`OPENCODE_BROWSER_SESSION`; bare execute never guesses from human-shell current
|
||||
state.
|
||||
|
||||
```bash
|
||||
opencode-browser session new "🎙️ elevenlabs"
|
||||
opencode-browser execute --session "🎙️ elevenlabs" 'return page.url()'
|
||||
opencode-browser execute 'return page.url()'
|
||||
```
|
||||
|
||||
MCP keeps one implicit process session. Omit `session` for that normal path, or
|
||||
call `session_new` and pass an explicit id when one MCP process needs multiple
|
||||
sessions.
|
||||
|
||||
To work in a tab the user already has open, ask them to click the OpenCode
|
||||
Browser toolbar button on it (the badge shows ON; another click takes it back).
|
||||
`status` then lists it with `owner=user` and its `tab=` id. Select it for one
|
||||
execute, or adopt it for sticky reuse with `session_adopt({ tabId })` (omit the
|
||||
selector when only one user tab is attached):
|
||||
|
||||
```bash
|
||||
opencode-browser session adopt --tab-id 123 --session github
|
||||
opencode-browser session adopt --target-url github.com --session github
|
||||
opencode-browser execute --target-url github.com 'return page.url()'
|
||||
```
|
||||
|
||||
`execute --target-url` selects a page for that call only. Continuing with just
|
||||
`--session` uses the session's default page, which may still be `about:blank`.
|
||||
For a multi-step task in an existing user tab, adopt it first. Always include
|
||||
`page.url()` when diagnosing an empty snapshot. Snapshot labels are compact
|
||||
descriptions; use `ref()` for actions rather than assuming their text is an
|
||||
exact Playwright accessible name.
|
||||
|
||||
`targetUrl` and `targetIndex` select existing attached pages; they never
|
||||
navigate. A URL selector must match exactly one page, and URL and index selectors
|
||||
cannot be combined. Adoption makes that tab the session default, closes the
|
||||
session's previous relay-created page, and is exclusive to one OpenCode Browser
|
||||
session. Reset or delete releases an adopted user tab without closing it.
|
||||
|
||||
Adoption binds ownership and exact target identity before initializing automation.
|
||||
Until the first execute resolves the page, session status reports `connected:
|
||||
false` and `pageUrl: null`; `adoptedUrl` is the registry-selected URL, not a fresh
|
||||
page read. A busy page can produce `session-page/adopted-initialization-timeout`
|
||||
on execute: user code did not run, and the exact adopted target is retained.
|
||||
Retry after the page settles; do not reset or adopt a different tab to recover.
|
||||
|
||||
Prefer adoption for authenticated browser state rather than reproducing login
|
||||
in a fresh page.
|
||||
|
||||
Each relay controls one browser/profile at a time. A second extension connection
|
||||
cannot replace a healthy active connection. If `status` or `doctor` reports
|
||||
rejected competing connections, keep the extension enabled only in the intended
|
||||
browser/profile. To switch browsers, disconnect the incumbent extension first;
|
||||
creating a new execute session does not switch browsers.
|
||||
|
||||
Completion: the selected page URL is the intended page, and later work either
|
||||
retains the returned session id or intentionally uses the MCP process session.
|
||||
|
||||
### 3. Inspect, Act, Verify
|
||||
|
||||
Inspect before guessing roles or selectors:
|
||||
|
||||
```js
|
||||
return await snapshot()
|
||||
```
|
||||
|
||||
Then act from the returned structure and verify the destination:
|
||||
|
||||
```js
|
||||
await ref("e12").click()
|
||||
await page.getByRole("heading", { name: "Settings" }).waitFor()
|
||||
if (!page.url().includes("/settings")) {
|
||||
throw new Error(`Unexpected destination: ${page.url()}`)
|
||||
}
|
||||
return { url: page.url(), heading: await page.getByRole("heading").first().innerText() }
|
||||
```
|
||||
|
||||
Use normal Playwright first. Keep dependent interactions in one execute when
|
||||
they rely on transient UI such as an open menu, selected rows, hover state, or
|
||||
an in-progress form.
|
||||
|
||||
Clicks, hovers and key presses already move and pace like a person (curved
|
||||
mouse paths, a short settle before pressing, human key gaps), so don't add your
|
||||
own `steps` or delays. `fill()` pastes text in one go; on sites that watch
|
||||
typing (sign-ups, logins behind bot protection), type with
|
||||
`locator.pressSequentially(text)` instead.
|
||||
|
||||
If native `locator.fill()` hangs because a browser extension interferes with
|
||||
focus, use the explicit input, textarea, or contenteditable fallback:
|
||||
|
||||
```js
|
||||
await fillInput(page.getByPlaceholder("Username"), "standard_user")
|
||||
```
|
||||
|
||||
Completion: the final return value contains evidence of the requested outcome,
|
||||
not merely evidence that an action was attempted.
|
||||
|
||||
### 4. Continue Or Finish Cleanly
|
||||
|
||||
Named sessions preserve their default page across short-lived CLI and MCP
|
||||
processes. They also survive relay restarts: OpenCode Browser restores the id,
|
||||
read-only mode, and exact default target. JavaScript `state` and snapshot refs
|
||||
are process-local and reset after a relay restart with an explicit warning.
|
||||
|
||||
```bash
|
||||
opencode-browser session list
|
||||
opencode-browser session reset github
|
||||
opencode-browser session delete github
|
||||
```
|
||||
|
||||
Deletion is idempotent for an explicit session id, so cleanup can be safely
|
||||
retried when that session is already absent.
|
||||
|
||||
Give a session a short, meaningful id (`github`, `checkout-test`): the tabs it
|
||||
opens are grouped in the browser under that name. Unnamed sessions are grouped as
|
||||
"Agent · <site>".
|
||||
|
||||
Tabs agents open close by themselves after about 10 minutes without use (no
|
||||
command and not visited by the user); tabs the user shared, pinned, or is
|
||||
looking at stay open. When a task is done, tidy up right away:
|
||||
|
||||
```bash
|
||||
opencode-browser tabs cleanup --idle-minutes 0 # MCP: tabs_cleanup { idleMinutes: 0 }
|
||||
```
|
||||
|
||||
Every execute is journaled under
|
||||
`<data root>/sessions/<id>/journal.jsonl` (`~/.local/share/opencode-browser` on
|
||||
macOS and Linux). The journal records code,
|
||||
status, duration, URL movement, warnings, handoffs, and bounded diagnostics.
|
||||
Never place credentials directly in execute source.
|
||||
|
||||
```bash
|
||||
opencode-browser journal --session github --limit 50
|
||||
```
|
||||
|
||||
Completion: retain the session only when follow-up work is expected; otherwise
|
||||
reset or delete session-owned pages and report any warnings that affect later
|
||||
work.
|
||||
|
||||
## Canonical Authenticated Flow
|
||||
|
||||
The distinguishing OpenCode Browser workflow is an authenticated tab plus a
|
||||
human-only prompt:
|
||||
|
||||
Attach and adopt the existing tab, inspect its real UI, fill ordinary fields,
|
||||
then register `handoff` before triggering WebAuthn, 2FA, CAPTCHA, or payment UI.
|
||||
After the user completes it, verify the authenticated destination. The same
|
||||
session can continue after an MCP process or relay restart.
|
||||
|
||||
When the prompt-triggering action may itself block, put only that action in
|
||||
`start`. OpenCode Browser presents and acknowledges WAIT before invoking it:
|
||||
|
||||
```js
|
||||
await handoff("Complete the security-key prompt, then continue", {
|
||||
timeoutMs: 600_000,
|
||||
start: () => page
|
||||
.getByRole("button", { name: /passkey|security key|sign in/i })
|
||||
.click({ timeout: 600_000 }),
|
||||
})
|
||||
|
||||
await page.waitForURL((url) => !url.pathname.startsWith("/login"))
|
||||
await page.getByRole("heading", { name: /account|dashboard/i }).waitFor()
|
||||
return { authenticatedUrl: page.url(), title: await page.title() }
|
||||
```
|
||||
|
||||
After a resolved handoff, OpenCode Browser waits through transient destination
|
||||
context replacement before returning, so this verification can remain in the
|
||||
same execute.
|
||||
|
||||
With `start`, the handoff deadline and target cancellation remain active until
|
||||
the action settles, even if the user has already pressed Continue. An early
|
||||
acknowledgment does not authorize an indefinitely pending action.
|
||||
|
||||
For a handoff on another page, pass `{ page: otherPage }`. Readiness checks that
|
||||
page, not the session default. If a non-default page was replaced or closed,
|
||||
inspect the remaining pages rather than assuming an old Playwright reference
|
||||
now identifies its replacement.
|
||||
|
||||
Tell the user what action is waiting. Human acknowledgment is not verification:
|
||||
always assert the expected URL or stable element after `handoff`. If the action
|
||||
was already completed and only the human step remains, call `handoff(message)`
|
||||
without `start`. The default timeout is ten minutes.
|
||||
|
||||
Completion: the prompt was presented only after WAIT was registered, the action
|
||||
settled, and the authenticated result was independently verified.
|
||||
|
||||
To turn a user-demonstrated flow into reusable Playwright, use `demonstrate()`.
|
||||
It uses the same exact-tab handoff, records clicks, edits, checkbox/select
|
||||
changes, and same-tab navigations, then returns editable code:
|
||||
|
||||
```ts
|
||||
return await demonstrate("Perform the workflow once, then continue")
|
||||
```
|
||||
|
||||
Password fields become explicit secret-source comments rather than copied
|
||||
values. Review selectors and add outcome assertions before reusing generated
|
||||
code; a demonstration records actions, not proof that the workflow succeeded.
|
||||
|
||||
### Password Manager Prompts
|
||||
|
||||
Ordinary webpage fields and accessible open-shadow-root controls remain usable.
|
||||
1Password's inline menus are extension-owned iframes, not ordinary webpage DOM.
|
||||
Chromium blocks one extension from debugging another extension's pages; toolbar
|
||||
popups and native unlock, Touch ID, and Windows Hello prompts are not supported
|
||||
Playwright control surfaces.
|
||||
|
||||
Focusing or filling a card-number or credential field can open the inline menu
|
||||
by itself, even inside a third-party payment iframe. While it is open, Chrome
|
||||
rejects every automation command for that tab; Playwright shows this as
|
||||
"Execution context was destroyed" or a locator timeout, so the execute result
|
||||
carries the `target/cross-extension-page` diagnostic and warning, and
|
||||
`opencode-browser status` marks the tab `protected-ui=true`.
|
||||
|
||||
`target/cross-extension-page` means a permission boundary. Ask the user to finish
|
||||
or dismiss the prompt and retry; do not reset the page, read vault contents, or
|
||||
weaken browser security to get around it. The page itself is healthy: do not
|
||||
treat the failure as an unresponsive tab or create a new page. Register `handoff` on the originating
|
||||
webpage before triggering a human-only prompt when possible. If the prompt
|
||||
already prevents attachment, give the user the required action directly rather
|
||||
than assuming an in-page handoff can be displayed. Verify the intended webpage
|
||||
state after the prompt is completed.
|
||||
|
||||
## Inspection Tools
|
||||
|
||||
Use the least expensive view that answers the question:
|
||||
|
||||
- `snapshot()` is the compact read-before-act default. It prioritizes semantic
|
||||
groups, alerts, lists, tables, headings, links, and controls. Text input and
|
||||
textarea values are omitted.
|
||||
- The default snapshot scopes to a single visible modal when present; portal
|
||||
dialogs outside `main` remain discoverable. Use an explicit `within` scope
|
||||
when you intentionally need background content. Repeated list wrappers have
|
||||
a bounded reservation so product links can still fit in a dense page.
|
||||
- Native number/search inputs are `spinbutton`/`searchbox`. Native disclosure
|
||||
controls are labeled `summary`, which is an element kind rather than an ARIA
|
||||
role; use the returned `ref()` to operate them.
|
||||
- `ref("e12")` resolves a control from the latest snapshot. Refs fail closed
|
||||
after navigation or incompatible DOM drift. Compatible refs keep the same id
|
||||
across repeated same-document captures.
|
||||
- `snapshot({ diff: true })` reports semantic changes from the compatible prior
|
||||
baseline. `snapshot({ delta: true })` returns a full first baseline and compact
|
||||
deltas afterward. Existing compatible refs remain usable.
|
||||
- `snapshot({ find: "checkout", context: 2 })` searches the bounded semantic
|
||||
snapshot and returns matching lines with nearby context and actionable refs.
|
||||
- `ariaSnapshot(target?, { timeout })` returns Playwright's detailed YAML aria
|
||||
tree when the compact snapshot omits needed structure. Native text-control
|
||||
values, custom ARIA range values, and editable content are omitted so they do
|
||||
not enter tool output. Await it separately; do not run other operations on
|
||||
the same page concurrently.
|
||||
- `screenshotWithLabels({ page?, path? })` adds visual labels and metadata when
|
||||
layout matters, and registers its `e1..eN` labels for `ref()`.
|
||||
- `screenshotDiff({ baseline, path?, threshold?, fullPage? })` compares a saved
|
||||
PNG (absolute path or Buffer) with the current session page at CSS-pixel scale.
|
||||
It returns `matches`, `changedPixels`, `changedRatio` (0..1), dimensions, and a
|
||||
red-highlighted PNG. Omit `path` to return the image as execute media; otherwise
|
||||
supply a fresh absolute `.png` path. Existing output files are never overwritten.
|
||||
|
||||
```js
|
||||
return await snapshot({ within: "main", maxItems: 200 })
|
||||
return await snapshot({ find: /checkout|payment/i, context: 2 })
|
||||
return await snapshot({ delta: true })
|
||||
// When layout matters, return the image through MCP so it can be inspected.
|
||||
return await screenshotWithLabels({ page })
|
||||
```
|
||||
|
||||
Saving an image and returning only `"ok"` proves file creation, not visual
|
||||
correctness. Return screenshot buffers through MCP when visual evidence matters.
|
||||
|
||||
For visual regression checks, save a baseline before changing the UI:
|
||||
|
||||
```ts
|
||||
await page.screenshot({ path: "/absolute/before.png", scale: "css" })
|
||||
// After the intended UI change, in the same viewport:
|
||||
return await screenshotDiff({ baseline: "/absolute/before.png" })
|
||||
```
|
||||
|
||||
The threshold defaults to 0.1 and controls per-pixel color tolerance, not the
|
||||
allowed changed area. Set it to 0 for exact pixels. Antialiasing changes count.
|
||||
Settle animations yourself and use the same viewport and `fullPage` setting for
|
||||
both captures. Dimension mismatches fail explicitly; images are never resized.
|
||||
Comparisons are limited to PNGs of 32 MiB / 16 megapixels each. Screenshots and
|
||||
diffs include visible page content: inspect for private information before sharing.
|
||||
|
||||
## Execute Interface
|
||||
|
||||
Execute code can use `page`, `context`, `browser`, persistent `state`, selected
|
||||
Node modules through `modules` and aliases such as `fs` and `path`, plus the
|
||||
OpenCode Browser helpers documented here. Execute code runs in Node. Use
|
||||
`page.evaluate` for `window`, `document`, storage, and same-origin `fetch`
|
||||
with page cookies. Single expressions auto-return;
|
||||
multi-statement scripts need `return`. Use `--file` for longer scripts:
|
||||
|
||||
```bash
|
||||
opencode-browser execute --session github --file ./perform-flow.js
|
||||
```
|
||||
|
||||
Human CLI output includes logs, warnings, and a concise aftermath. Logs hold your
|
||||
own `console.log` output; the page's console messages and uncaught errors are
|
||||
off by default, because capturing them lets the page detect a debugger (bot
|
||||
checks look for it). When you need them, for example to debug a broken page,
|
||||
turn them on for that page; they then appear in each execute's logs:
|
||||
|
||||
```js
|
||||
await pageConsole.start() // or pageConsole.start(otherPage)
|
||||
await pageConsole.stop() // when done, so the page looks undebugged again
|
||||
```
|
||||
|
||||
Use `--json`
|
||||
when another command needs to branch on `ok`, `value`, `error`, `warnings`, or
|
||||
`aftermath`:
|
||||
|
||||
```bash
|
||||
opencode-browser execute --json --session github '({ url: page.url() })' | jq .value.url
|
||||
```
|
||||
|
||||
Playwright downloads are unavailable through extension-backed tabs because
|
||||
Chromium blocks download artifact control through `chrome.debugger`. If the
|
||||
page exposes the payload through fetch or an API response, read the bytes in the
|
||||
page and write them with `fs`. Do not retry `page.waitForEvent("download")`.
|
||||
|
||||
Pages with WebMCP enabled can expose structured page tools. Discover and call
|
||||
them through the execute helper; names, descriptions, schemas, and results come
|
||||
from the page:
|
||||
|
||||
```ts
|
||||
const tools = await webmcp.list()
|
||||
const result = await webmcp.call("search_catalog", { query: "adapter" })
|
||||
return { tools, result }
|
||||
```
|
||||
|
||||
Discovery covers all frames. If the same name appears in multiple frames, pass
|
||||
the exact reported frame label as `{ frame }`. OpenCode Browser re-discovers the
|
||||
tool immediately before invoking it, so stale registrations fail directly.
|
||||
|
||||
## Safety
|
||||
|
||||
OpenCode Browser blocks CDP commands that would destroy shared browser state,
|
||||
including browser close and cookie/cache clearing. Never work around those
|
||||
guardrails.
|
||||
|
||||
For inspect-only work, use a read-only session:
|
||||
|
||||
```bash
|
||||
opencode-browser session new inspect-prod --read-only
|
||||
opencode-browser execute --session inspect-prod 'await page.goto("https://example.com"); return page.title()'
|
||||
```
|
||||
|
||||
Read-only sessions reject `Input.*`, so they cannot click or type through
|
||||
Playwright. `page.evaluate` can still mutate the DOM; read-only prevents trusted
|
||||
mistakes, not malicious code.
|
||||
|
||||
For destructive UI work, use a two-phase **read, confirm, verify** flow:
|
||||
|
||||
1. Read candidates and return exact stable identifiers or row text.
|
||||
2. Obtain user approval for those exact items.
|
||||
3. Re-select only approved items and assert the selected count.
|
||||
4. Read the confirmation dialog and throw unless it matches the approved action.
|
||||
5. Confirm, then verify through a fresh page read or independent CLI/API path.
|
||||
|
||||
Do not discover and confirm destructive candidates in one script unless the
|
||||
user already approved exact stable identifiers. Never globally auto-accept
|
||||
native dialogs; wait for the expected dialog and assert its type and message
|
||||
before accepting it.
|
||||
|
||||
## TypeScript Client
|
||||
|
||||
Applications and CLI tools can use `OpenCodeBrowserClient.origin()` for direct
|
||||
same-origin JSON requests authenticated by a session page (with optional default
|
||||
headers and automatic in-page `handoff` recovery when a session expires or
|
||||
redirects to login):
|
||||
|
||||
```ts
|
||||
import { OpenCodeBrowserClient } from "opencode-browser-cli"
|
||||
|
||||
const ubereats = OpenCodeBrowserClient.origin("https://www.ubereats.com", {
|
||||
session: "🥤 karma-cafe",
|
||||
startUrl: "/feed",
|
||||
headers: { "x-csrf-token": "x" },
|
||||
handoffOnAuthFailure: true,
|
||||
})
|
||||
|
||||
const store = await ubereats.post("/_p/api/getStoreV1", {
|
||||
storeUuid: "78cb1602-9f58-57bb-ad08-f6b8f80bb788",
|
||||
diningMode: "DELIVERY",
|
||||
})
|
||||
```
|
||||
|
||||
For Effect-native applications with schema decoding or `sensitive: true`
|
||||
`Redacted` responses, use `OpenCodeBrowserClient.Service`:
|
||||
|
||||
```ts
|
||||
import { OpenCodeBrowserClient } from "opencode-browser-cli"
|
||||
|
||||
const sensitive = yield* origin.json({
|
||||
path: "/api/session",
|
||||
method: "POST",
|
||||
body: {},
|
||||
response: SessionResponse,
|
||||
sensitive: true,
|
||||
})
|
||||
const session = OpenCodeBrowserClient.reveal(sensitive)
|
||||
```
|
||||
|
||||
## Authenticated Network Capture
|
||||
|
||||
Use network capture when the browser is needed to authenticate or discover a
|
||||
workflow, but repeated direct HTTP calls would be faster and more reliable.
|
||||
Capture each flow at least twice with different inputs so constants and
|
||||
parameters can be distinguished.
|
||||
|
||||
```bash
|
||||
opencode-browser network start --session github --url /api/ \
|
||||
--resource-type fetch --resource-type xhr
|
||||
opencode-browser execute --session github --file ./perform-flow.js
|
||||
opencode-browser network status --session github
|
||||
opencode-browser network stop --session github \
|
||||
--output ./github.har --secrets github
|
||||
```
|
||||
|
||||
Written artifacts replace credential-bearing headers, cookies, query fields,
|
||||
OAuth fragment fields, and structured body fields with stable references such as `${BC_SECRET_1}`.
|
||||
`--secrets github` stores lossless values separately in a mode-`0600` Secret
|
||||
Profile. Never copy profile values into source, output, diagnostics, or journals,
|
||||
and never deliberately return or log credentials.
|
||||
|
||||
Inspect the redacted artifact offline, generate one typed function per observed
|
||||
flow, then verify each function with a harmless live request. Run generated
|
||||
clients without exposing values:
|
||||
|
||||
```bash
|
||||
opencode-browser secrets status github
|
||||
opencode-browser secrets run github -- ./github-cli repositories
|
||||
```
|
||||
|
||||
Generated TypeScript applications can own that wrapper internally through the
|
||||
public SDK:
|
||||
|
||||
```ts
|
||||
import { SecretProfile } from "opencode-browser-cli"
|
||||
import { Effect } from "effect"
|
||||
|
||||
const result = await Effect.runPromise(SecretProfile.run({
|
||||
name: "github",
|
||||
command: process.execPath,
|
||||
args: ["./github-cli.js", "repositories"],
|
||||
}))
|
||||
```
|
||||
|
||||
The trusted worker receives `BC_SECRET_N` variables and its bounded output is
|
||||
redacted. The public SDK exposes profile metadata but never raw profile values.
|
||||
|
||||
Refresh credentials normally renewed by a page reload with:
|
||||
|
||||
```bash
|
||||
opencode-browser secrets refresh github --session github --url /api/
|
||||
```
|
||||
|
||||
If refresh requires login or a human prompt, reauthenticate in the adopted tab
|
||||
and repeat capture with the same profile. MCP exposes equivalent `network_*`
|
||||
and `secrets_*` tools.
|
||||
|
||||
Completion: the artifact contains references rather than credential values, the
|
||||
generated operation passes a harmless live check, and no secret value appears
|
||||
in source or output.
|
||||
|
||||
## Recording
|
||||
|
||||
Record an attached or session-owned tab with:
|
||||
|
||||
```bash
|
||||
opencode-browser recording start ./tmp/demo.mp4 --session github --mode cdp
|
||||
opencode-browser recording status --session github
|
||||
opencode-browser recording stop --session github
|
||||
```
|
||||
|
||||
Start, stop, and status accept `--json`. CDP stop/status results include a
|
||||
`quality` receipt: output dimensions/rate, source and retained-image counts/rates,
|
||||
coalesced/dropped frames, and `screenshotFallback`. The fallback means no
|
||||
compositor frames arrived and the video holds one stop-time screenshot; do not
|
||||
present that as recorded motion. Source counters count compositor events, not
|
||||
visually distinct frames. Low rates can be normal on a static page. Tab capture
|
||||
and older relays omit quality telemetry rather than inventing measurements.
|
||||
|
||||
Explicit frame rates must be integers from 1 through 60 in either mode; invalid
|
||||
values fail instead of silently clamping. The start result reports the chosen rate.
|
||||
|
||||
`--mode auto` uses tab capture for user-owned tabs and CDP for relay-owned tabs.
|
||||
Tab capture can include audio; CDP requires `ffmpeg` and has no audio. Use the
|
||||
command's `--help` for format and cursor options.
|
||||
|
||||
Playwright mouse actions automatically reveal the on-page Ghost Cursor
|
||||
(`distance-glide` motion + `tactile-bloom` click shockwave). When recording a
|
||||
user-facing proof or PR walkthrough video, opt into the `ghostCursor` helpers
|
||||
inside `execute` to focus attention on key steps and verified postconditions:
|
||||
|
||||
```ts
|
||||
await showGhostCursor()
|
||||
await ghostCursor.caption("Verify cluster & promote release", { step: "01", tone: "neutral" })
|
||||
await ghostCursor.zoom("#release-card", { scale: 1.45 })
|
||||
await page.locator("#promote-btn").click()
|
||||
await ghostCursor.keys("⌘+⇧+P", "Promote Release")
|
||||
await ghostCursor.resetZoom()
|
||||
await ghostCursor.spotlight("#status-badge", { label: "Verified", detail: "200 OK", tone: "success" })
|
||||
```
|
||||
|
||||
CDP recordings preserve the starting CSS viewport (not a fixed 720p canvas),
|
||||
use high-quality source frames, and default to 60 fps. Use `--frame-rate 30`
|
||||
for smaller files. Actual motion still depends on Chrome delivering new frames;
|
||||
60 fps output does not guarantee 60 distinct frames. Larger viewports cost more
|
||||
CPU, transport bandwidth, and storage. Set the viewport before recording and do
|
||||
not change viewport/emulation mid-recording. Odd dimensions round down to even.
|
||||
|
||||
For failures that are hard to reproduce, keep a rolling CDP frame buffer and
|
||||
save recent history after the problem occurs:
|
||||
|
||||
```bash
|
||||
opencode-browser flight-recorder start --session github --retention-ms 60000
|
||||
opencode-browser flight-recorder status --session github
|
||||
opencode-browser flight-recorder save-last ./tmp/failure.mp4 --session github --duration-ms 30000
|
||||
opencode-browser flight-recorder cancel --session github
|
||||
```
|
||||
|
||||
Saving does not stop buffering. The recorder is memory-bounded, reports retained
|
||||
duration/frame/byte/drop counters, writes a JSON receipt beside each clip, and is
|
||||
mutually exclusive with ordinary recording on the same tab. CLI recording and
|
||||
flight-recorder lifecycle operations are also available as MCP tools.
|
||||
|
||||
Inspect an encoded frame at native size before sharing: the whole viewport must
|
||||
fill the frame, small text must be readable, and motion must not be a repeated
|
||||
still image. Do not crop and upscale a low-resolution capture to call it HD.
|
||||
On an older installed relay that shrinks the page into a padded corner, record
|
||||
the defect and coordinate a recorder update; changing the file's resolution is
|
||||
not a repair.
|
||||
|
||||
Completion: stop the recorder, inspect the resulting media rather than only its
|
||||
existence, and report the viewport, state, and interaction path actually tested.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
1. Run `opencode-browser doctor`; it checks package metadata, CLI/relay build
|
||||
identity, extension protocol compatibility, sessions, targets, and artifacts.
|
||||
2. Use `status --json` to inspect exact sessions and target ownership.
|
||||
3. Reproduce once with the smallest execute before changing code.
|
||||
|
||||
Common diagnoses:
|
||||
|
||||
- `session-page/context-read-timeout; operation=page.title; timeoutMs=5000`:
|
||||
the title read did not finish within its budget. The execution context may be
|
||||
unavailable or busy; this does not prove a frozen renderer. The watchdog does
|
||||
not cancel the underlying read or trigger page replacement. A cached
|
||||
`page.url()` read can still work; retry the page read after the page settles.
|
||||
Ordinary missing-locator timeouts do not receive this diagnostic.
|
||||
- `connected:false`: run a relay-backed command and allow the extension startup
|
||||
or alarm wake-up to reconnect. A sleeping extension wakes on a 30-second
|
||||
alarm, so the command waits up to 35 seconds. Reload the unpacked extension
|
||||
only if that loop does not recover.
|
||||
- Incompatible extension protocol: update either the extension or npm package;
|
||||
exact extension and relay release versions do not need to match.
|
||||
- Competing browser/profile connections: the active browser is preserved and
|
||||
additional connections are rejected. Use one browser/profile per relay;
|
||||
repeatedly creating sessions or resetting tabs does not switch browsers.
|
||||
- Stale relay build: inspect `doctor`, then coordinate an explicit
|
||||
`opencode-browser relay restart`. It requires an exact managed instance and
|
||||
safe shutdown protocol 2. Legacy relays need a one-time coordinated manual
|
||||
stop; foreground/source or newer relays are never force-killed or downgraded.
|
||||
MCP observational tools remain available on a mismatch.
|
||||
- Unexpected restart: inspect private endpoint-scoped
|
||||
`~/.opencode-browser/relays/<port>/lifecycle.jsonl` for requester/build/instance
|
||||
metadata. Preparing or selecting a development candidate must not restart the
|
||||
daemon. Use isolated `runtime:prepare` / `runtime:select`, not a live checkout
|
||||
link, when developing OpenCode Browser itself.
|
||||
- `Target not found`: attach the intended tab, then select or adopt it using a
|
||||
unique URL substring or explicit index.
|
||||
- All targets disappeared: dismissing Chromium's debugging banner detaches every
|
||||
tab. Ask the user to click the toolbar button on it again.
|
||||
- Relay restarted: named sessions reclaim exact targets, but JavaScript `state`
|
||||
and snapshot refs reset. Continue after the warning.
|
||||
- Reset/delete after an extension update may wait briefly for target
|
||||
re-announcement; if the old relay-owned target is absent from the completed
|
||||
inventory, OpenCode Browser forgets the dead identity without closing a
|
||||
guessed tab.
|
||||
- Repeated execution-context errors: run one short follow-up so OpenCode Browser
|
||||
can health-check the page. A live page is kept: OpenCode Browser reconnects and
|
||||
re-resolves the same tab once, then fails with a `session-page/*-unresponsive`
|
||||
diagnosis if the page still does not answer. Blank or unknown URLs are preserved:
|
||||
they can contain unsaved content. Only a crashed or
|
||||
`chrome-error://` relay-owned page is closed and recreated. It never replaces
|
||||
an adopted user tab. Main-frame navigation clears an earlier crash diagnosis;
|
||||
child-frame navigation does not. When a page stays unresponsive (bot-protected sites can
|
||||
stall the main world for automation while rendering normally for the human),
|
||||
open a fresh tab with `context.newPage()` or hand the tab to the user.
|
||||
- Handoff ends with "page execution context did not become available": the user
|
||||
finished; only OpenCode Browser's view of the tab is stale. Run a short
|
||||
follow-up execute so the page is re-checked instead of assuming it was lost.
|
||||
- Fill timeout on login fields: inspect first, then try `fillInput` after
|
||||
confirming the selector or locator resolves. String selectors search open
|
||||
shadow roots recursively; closed shadow roots remain unavailable.
|
||||
- Click blocked by a verified dialog backdrop: inspect the exact target and
|
||||
blocker first. If the target is the approved action and Playwright actionability
|
||||
alone is stale, dispatch `await locator.evaluate((element) => element.click())`,
|
||||
then verify the result. Do not make dispatched clicks the default.
|
||||
- Hidden checkbox/radio input: click its visible associated label or wrapper,
|
||||
then verify `isChecked()`. Do not force-click an invisible input or infer an
|
||||
arbitrary ancestor.
|
||||
- Download wait fails: use fetch plus `fs`; extension-backed Playwright cannot
|
||||
retain a native download artifact.
|
||||
- Hover on an infinitely animated target: Playwright may never consider the
|
||||
element stable. Read its current `getBoundingClientRect()` and use
|
||||
`page.mouse.move()` when coordinate input is appropriate.
|
||||
- Chromium-protected pages such as the Chrome Web Store developer dashboard may
|
||||
detach `chrome.debugger`. Do not retry or bypass that boundary; open the page
|
||||
for manual operation.
|
||||
|
||||
For deeper relay diagnosis, restart with `OPENCODE_BROWSER_DEBUG=1`. Debug traces
|
||||
must never include expressions, arguments, results, headers, cookies, or form
|
||||
values.
|
||||
|
||||
Whenever OpenCode Browser fails, wedges, replaces a page/session, or behaves
|
||||
unexpectedly, create or update a `opencode-browser` project todo with the Browser
|
||||
Control version, safe session/page context, exact error, deterministic
|
||||
reproduction, expected versus actual behavior, and recovery attempted. Never
|
||||
include credentials, form values, or private account data.
|
||||
224
packages/browser-extension/cli/src/aria-snapshot.ts
Normal file
224
packages/browser-extension/cli/src/aria-snapshot.ts
Normal file
|
|
@ -0,0 +1,224 @@
|
|||
import { selectors, type BrowserContext, type Frame, type Locator, type Page } from "patchright-core"
|
||||
import { runtimeFailureKind } from "./runtime-diagnostics.ts"
|
||||
|
||||
const redactionCleanupErrorMessage = "OpenCode Browser could not confirm ARIA snapshot value-redaction cleanup"
|
||||
// Keep the engine as source text so bundlers cannot inject Node-only helpers into the browser function.
|
||||
const redactionSelectorSource = `({
|
||||
query(root, body) {
|
||||
const separator = body.indexOf("_")
|
||||
const action = body.slice(0, separator)
|
||||
const token = body.slice(separator + 1)
|
||||
const stateKey = "__opencodeBrowserAriaRedactionState__"
|
||||
let state = globalThis[stateKey]
|
||||
|
||||
if (action === "on") {
|
||||
if (!state) {
|
||||
state = {
|
||||
tokens: new Set(),
|
||||
editableNameAttributes: new Set(["alt", "aria-description", "aria-describedby", "aria-label", "aria-labelledby", "aria-placeholder", "placeholder", "title"]),
|
||||
nonTextInputTypes: new Set(["button", "checkbox", "file", "hidden", "image", "radio", "reset", "submit"]),
|
||||
valueAttributes: new Set(["aria-valuenow", "aria-valuetext", "value"]),
|
||||
valueRoles: new Set(["meter", "progressbar", "scrollbar", "separator", "slider", "spinbutton"]),
|
||||
inputValue: Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value"),
|
||||
textareaValue: Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value"),
|
||||
nodeValue: Object.getOwnPropertyDescriptor(Node.prototype, "nodeValue"),
|
||||
textContent: Object.getOwnPropertyDescriptor(Node.prototype, "textContent"),
|
||||
innerText: Object.getOwnPropertyDescriptor(HTMLElement.prototype, "innerText"),
|
||||
getAttribute: Object.getOwnPropertyDescriptor(Element.prototype, "getAttribute"),
|
||||
}
|
||||
globalThis[stateKey] = state
|
||||
const getAttribute = (element, name) => state.getAttribute.value.call(element, name)
|
||||
const explicitEditableState = (element) => {
|
||||
const value = getAttribute(element, "contenteditable")
|
||||
if (value === null) return undefined
|
||||
const normalized = value.trim().toLowerCase()
|
||||
if (normalized === "false") return false
|
||||
return normalized === "" || normalized === "true" || normalized === "plaintext-only" ? true : undefined
|
||||
}
|
||||
const composedParent = (node) => {
|
||||
if (node.parentNode) return node.parentNode
|
||||
const treeRoot = node.getRootNode?.()
|
||||
return treeRoot instanceof ShadowRoot ? treeRoot.host : null
|
||||
}
|
||||
const isInEditableSubtree = (node) => {
|
||||
let current = node.nodeType === Node.TEXT_NODE ? node.parentNode : node
|
||||
while (current) {
|
||||
if (current instanceof Element) {
|
||||
const explicit = explicitEditableState(current)
|
||||
if (explicit !== undefined) return explicit
|
||||
if (current instanceof HTMLElement && current.isContentEditable) return true
|
||||
}
|
||||
current = composedParent(current)
|
||||
}
|
||||
return false
|
||||
}
|
||||
const isEditableDescendant = (element) => explicitEditableState(element) !== true && isInEditableSubtree(element)
|
||||
const isNativeTextControl = (element) => element instanceof HTMLTextAreaElement
|
||||
|| (element instanceof HTMLInputElement && !state.nonTextInputTypes.has(element.type))
|
||||
const isValueRole = (element) => {
|
||||
const role = getAttribute(element, "role")
|
||||
return role !== null && role.trim().toLowerCase().split(/\\s+/).some((candidate) => state.valueRoles.has(candidate))
|
||||
}
|
||||
const hasRedactedText = (node) => isInEditableSubtree(node)
|
||||
|| (node.nodeType === Node.TEXT_NODE && node.parentElement && isNativeTextControl(node.parentElement))
|
||||
Object.defineProperty(HTMLInputElement.prototype, "value", {
|
||||
...state.inputValue,
|
||||
get() {
|
||||
return isNativeTextControl(this) ? "" : state.inputValue.get.call(this)
|
||||
},
|
||||
})
|
||||
Object.defineProperty(HTMLTextAreaElement.prototype, "value", { ...state.textareaValue, get() { return "" } })
|
||||
Object.defineProperty(Node.prototype, "nodeValue", {
|
||||
...state.nodeValue,
|
||||
get() {
|
||||
return this.nodeType === Node.TEXT_NODE && hasRedactedText(this)
|
||||
? ""
|
||||
: state.nodeValue.get.call(this)
|
||||
},
|
||||
})
|
||||
Object.defineProperty(Node.prototype, "textContent", {
|
||||
...state.textContent,
|
||||
get() {
|
||||
return (this instanceof Element && isNativeTextControl(this)) || hasRedactedText(this)
|
||||
? ""
|
||||
: state.textContent.get.call(this)
|
||||
},
|
||||
})
|
||||
Object.defineProperty(HTMLElement.prototype, "innerText", {
|
||||
...state.innerText,
|
||||
get() {
|
||||
return isNativeTextControl(this) || isInEditableSubtree(this) ? "" : state.innerText.get.call(this)
|
||||
},
|
||||
})
|
||||
Object.defineProperty(Element.prototype, "getAttribute", {
|
||||
...state.getAttribute,
|
||||
value(name) {
|
||||
const attribute = String(name).toLowerCase()
|
||||
if (state.valueAttributes.has(attribute)
|
||||
&& (isNativeTextControl(this) || isValueRole(this) || isInEditableSubtree(this))) return null
|
||||
if (state.editableNameAttributes.has(attribute) && isEditableDescendant(this)) return null
|
||||
return state.getAttribute.value.call(this, name)
|
||||
},
|
||||
})
|
||||
}
|
||||
state.tokens.add(token)
|
||||
} else if (action === "off" && state) {
|
||||
state.tokens.delete(token)
|
||||
if (state.tokens.size === 0) {
|
||||
Object.defineProperty(HTMLInputElement.prototype, "value", state.inputValue)
|
||||
Object.defineProperty(HTMLTextAreaElement.prototype, "value", state.textareaValue)
|
||||
Object.defineProperty(Node.prototype, "nodeValue", state.nodeValue)
|
||||
Object.defineProperty(Node.prototype, "textContent", state.textContent)
|
||||
Object.defineProperty(HTMLElement.prototype, "innerText", state.innerText)
|
||||
Object.defineProperty(Element.prototype, "getAttribute", state.getAttribute)
|
||||
delete globalThis[stateKey]
|
||||
}
|
||||
}
|
||||
|
||||
return root instanceof Element ? root : document.documentElement
|
||||
},
|
||||
queryAll(root, body) {
|
||||
return [this.query(root, body)]
|
||||
},
|
||||
})`
|
||||
|
||||
const redactionModuleId = globalThis.crypto.randomUUID().replaceAll("-", "")
|
||||
let nextRedactionSelector = 0
|
||||
let nextRedactionToken = 0
|
||||
const contextSelectors = new WeakMap<BrowserContext, Promise<string>>()
|
||||
const pageCleanup = new WeakMap<Page, {
|
||||
readonly pendingTokens: Map<string, { readonly frame: Frame; readonly selectorName: string }>
|
||||
queue: Promise<void>
|
||||
}>()
|
||||
|
||||
export async function ariaSnapshotWithoutTextControlValues(
|
||||
locator: Locator,
|
||||
options: { readonly timeout: number },
|
||||
): Promise<string> {
|
||||
const page = locator.page()
|
||||
const redactionSelectorName = await selectorForContext(page.context())
|
||||
const token = `${redactionModuleId}${++nextRedactionToken}`
|
||||
const handle = await locator.elementHandle({ timeout: options.timeout })
|
||||
if (!handle) throw new Error("ARIA snapshot target did not resolve to an element")
|
||||
const frame = await handle.ownerFrame().finally(() => handle.dispose())
|
||||
if (!frame) throw new Error("ARIA snapshot target is not attached to a frame")
|
||||
let snapshot: string | undefined
|
||||
let captureFailed = false
|
||||
let captureError: unknown
|
||||
try {
|
||||
snapshot = await locator
|
||||
.locator(`${redactionSelectorName}=on_${token}`)
|
||||
.ariaSnapshot(options)
|
||||
} catch (error) {
|
||||
captureFailed = true
|
||||
captureError = error
|
||||
}
|
||||
|
||||
try {
|
||||
await cleanupRedaction(page, frame, redactionSelectorName, token)
|
||||
} catch (cleanupError) {
|
||||
if (captureFailed) {
|
||||
const cleanupReasons = cleanupError instanceof AggregateError ? cleanupError.errors : [cleanupError]
|
||||
throw new AggregateError([captureError, ...cleanupReasons], redactionCleanupErrorMessage)
|
||||
}
|
||||
throw cleanupError
|
||||
}
|
||||
|
||||
if (captureFailed) throw captureError
|
||||
return snapshot!
|
||||
}
|
||||
|
||||
export async function registerAriaSnapshotSelector(context: BrowserContext): Promise<void> {
|
||||
await selectorForContext(context)
|
||||
}
|
||||
|
||||
async function selectorForContext(context: BrowserContext): Promise<string> {
|
||||
const existing = contextSelectors.get(context)
|
||||
if (existing) return existing
|
||||
|
||||
const name = `bcariaredact${redactionModuleId}${++nextRedactionSelector}`
|
||||
const registration = selectors.register(
|
||||
name,
|
||||
{ content: redactionSelectorSource },
|
||||
{ contentScript: true },
|
||||
).then(() => name, (error) => {
|
||||
contextSelectors.delete(context)
|
||||
throw error
|
||||
})
|
||||
contextSelectors.set(context, registration)
|
||||
return registration
|
||||
}
|
||||
|
||||
async function cleanupRedaction(page: Page, frame: Frame, redactionSelectorName: string, token: string): Promise<void> {
|
||||
let state = pageCleanup.get(page)
|
||||
if (!state) {
|
||||
state = { pendingTokens: new Map(), queue: Promise.resolve() }
|
||||
pageCleanup.set(page, state)
|
||||
}
|
||||
state.pendingTokens.set(token, { frame, selectorName: redactionSelectorName })
|
||||
|
||||
const cleanup = state.queue.then(async () => {
|
||||
const pending = [...state.pendingTokens]
|
||||
const results = await Promise.allSettled(pending.map(async ([pendingToken, target]) => {
|
||||
await target.frame.locator(`${target.selectorName}=off_${pendingToken}`).waitFor({ state: "attached", timeout: 1_000 })
|
||||
}))
|
||||
const failures: unknown[] = []
|
||||
for (let index = 0; index < results.length; index++) {
|
||||
const result = results[index]!
|
||||
const pendingToken = pending[index]![0]
|
||||
if (result.status === "fulfilled" || redactionContextIsGone(result.reason)) {
|
||||
state.pendingTokens.delete(pendingToken)
|
||||
} else {
|
||||
failures.push(result.reason)
|
||||
}
|
||||
}
|
||||
if (failures.length > 0) throw new AggregateError(failures, redactionCleanupErrorMessage)
|
||||
})
|
||||
state.queue = cleanup.catch(() => {})
|
||||
await cleanup
|
||||
}
|
||||
|
||||
function redactionContextIsGone(cause: unknown): boolean {
|
||||
const kind = runtimeFailureKind(cause)
|
||||
return kind === "context-destroyed" || kind === "context-missing" || kind === "target-closed"
|
||||
}
|
||||
373
packages/browser-extension/cli/src/auth-profile.ts
Normal file
373
packages/browser-extension/cli/src/auth-profile.ts
Normal file
|
|
@ -0,0 +1,373 @@
|
|||
import { dataRoot } from "./paths.ts"
|
||||
import { Effect, Schema, Semaphore } from "effect"
|
||||
import { spawn } from "node:child_process"
|
||||
import fs from "node:fs/promises"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { terminateChildProcess } from "./child-process.ts"
|
||||
import { isNodeError, writeJsonFileAtomically } from "./fs-durability.ts"
|
||||
import { redactKnownValues, type CredentialSlot } from "./network-redaction.ts"
|
||||
|
||||
const StoredCredentialSlot = Schema.Struct({
|
||||
ref: Schema.String.check(Schema.isPattern(/^BC_SECRET_[1-9][0-9]*$/)),
|
||||
value: Schema.String,
|
||||
sources: Schema.Array(Schema.String),
|
||||
expiresAt: Schema.optionalKey(Schema.String),
|
||||
})
|
||||
|
||||
const StoredAuthProfile = Schema.Struct({
|
||||
version: Schema.Literal(1),
|
||||
name: Schema.String,
|
||||
createdAt: Schema.String,
|
||||
updatedAt: Schema.String,
|
||||
slots: Schema.Array(StoredCredentialSlot),
|
||||
}).check(Schema.makeFilter((profile) => {
|
||||
const refs = profile.slots.map((slot) => slot.ref)
|
||||
if (new Set(refs).size !== refs.length) return "Auth profile refs must be unique"
|
||||
const sources = profile.slots.flatMap((slot) => slot.sources)
|
||||
return new Set(sources).size === sources.length ? undefined : "Auth profile sources must be unique"
|
||||
}))
|
||||
|
||||
interface AuthProfile extends Schema.Schema.Type<typeof StoredAuthProfile> {}
|
||||
|
||||
export type AuthProfileSummary = {
|
||||
readonly name: string
|
||||
readonly createdAt: string
|
||||
readonly updatedAt: string
|
||||
readonly slotCount: number
|
||||
readonly slots: readonly {
|
||||
readonly ref: string
|
||||
readonly sources: readonly string[]
|
||||
readonly expiresAt?: string
|
||||
readonly expired: boolean
|
||||
}[]
|
||||
}
|
||||
|
||||
export type AuthRunResult = {
|
||||
readonly exitCode: number
|
||||
readonly signal: NodeJS.Signals | null
|
||||
readonly stdout: string
|
||||
readonly stderr: string
|
||||
readonly stdoutTruncated: boolean
|
||||
readonly stderrTruncated: boolean
|
||||
readonly durationMs: number
|
||||
}
|
||||
|
||||
export interface AuthProfileOptions {
|
||||
readonly baseDir?: string
|
||||
}
|
||||
|
||||
export interface AuthRunOptions extends AuthProfileOptions {
|
||||
readonly name: string
|
||||
readonly command: string
|
||||
readonly args?: readonly string[]
|
||||
readonly cwd?: string
|
||||
readonly timeoutMs?: number
|
||||
readonly maxOutputBytes?: number
|
||||
}
|
||||
|
||||
export class AuthProfileError extends Schema.TaggedError<AuthProfileError>()(
|
||||
"AuthProfile.Error",
|
||||
{
|
||||
message: Schema.String,
|
||||
operation: Schema.String,
|
||||
reason: Schema.Literals(["invalid-name", "not-found", "read-failed", "invalid-json", "invalid-profile", "write-failed", "run-failed"]),
|
||||
cause: Schema.optionalKey(Schema.Defect()),
|
||||
},
|
||||
) {}
|
||||
|
||||
const defaultBaseDir = (): string => path.join(dataRoot(), "secrets")
|
||||
const writeLock = Semaphore.makeUnsafe(1)
|
||||
const profileLockTimeoutMs = 30_000
|
||||
const staleProfileLockMs = 60_000
|
||||
const maximumRunOutputBytes = 10_000_000
|
||||
const maximumTimeoutMs = 2_147_483_647
|
||||
|
||||
export const read = Effect.fn("AuthProfile.read")(function* (name: string, options: { readonly baseDir?: string } = {}) {
|
||||
const filePath = yield* profilePath(options.baseDir ?? defaultBaseDir(), name)
|
||||
const text = yield* Effect.tryPromise({
|
||||
try: () => fs.readFile(filePath, "utf8"),
|
||||
catch: (cause) => isNodeError(cause) && cause.code === "ENOENT"
|
||||
? new AuthProfileError({ message: `Auth profile not found: ${name}`, operation: "read", reason: "not-found", cause })
|
||||
: new AuthProfileError({ message: `Could not read auth profile: ${name}`, operation: "read", reason: "read-failed", cause }),
|
||||
})
|
||||
return yield* Schema.decodeUnknownEffect(StoredAuthProfile)(yield* Effect.try({
|
||||
try: () => JSON.parse(text),
|
||||
catch: (cause) => new AuthProfileError({ message: `Auth profile is not valid JSON: ${name}`, operation: "read", reason: "invalid-json", cause }),
|
||||
})).pipe(
|
||||
Effect.mapError((cause) => cause instanceof AuthProfileError
|
||||
? cause
|
||||
: new AuthProfileError({ message: `Auth profile has an invalid shape: ${name}`, operation: "read", reason: "invalid-profile", cause })),
|
||||
)
|
||||
})
|
||||
|
||||
export const readOptional = Effect.fn("AuthProfile.readOptional")(function* (name: string, options: { readonly baseDir?: string } = {}) {
|
||||
return yield* read(name, options).pipe(
|
||||
Effect.catchIf((error) => error.reason === "not-found", () => Effect.succeed(undefined)),
|
||||
)
|
||||
})
|
||||
|
||||
export function withLock<A, E, R>(
|
||||
name: string,
|
||||
options: { readonly baseDir?: string },
|
||||
effect: Effect.Effect<A, E, R>,
|
||||
): Effect.Effect<A, E | AuthProfileError, R> {
|
||||
const baseDir = options.baseDir ?? defaultBaseDir()
|
||||
const acquire = Effect.gen(function* () {
|
||||
const filePath = yield* profilePath(baseDir, name)
|
||||
const lockPath = `${filePath}.lock`
|
||||
yield* Effect.tryPromise({
|
||||
try: async () => {
|
||||
await fs.mkdir(baseDir, { recursive: true, mode: 0o700 })
|
||||
await fs.chmod(baseDir, 0o700)
|
||||
const deadline = Date.now() + profileLockTimeoutMs
|
||||
while (true) {
|
||||
try {
|
||||
const handle = await fs.open(lockPath, "wx", 0o600)
|
||||
let written = false
|
||||
try {
|
||||
await handle.writeFile(`${process.pid}\n`)
|
||||
written = true
|
||||
} finally {
|
||||
await handle.close()
|
||||
if (!written) await fs.unlink(lockPath).catch(() => {})
|
||||
}
|
||||
return
|
||||
} catch (cause) {
|
||||
if (!isNodeError(cause) || cause.code !== "EEXIST") throw cause
|
||||
const stat = await fs.stat(lockPath).catch(() => undefined)
|
||||
if (stat && Date.now() - stat.mtimeMs > staleProfileLockMs) {
|
||||
await fs.unlink(lockPath).catch(() => {})
|
||||
continue
|
||||
}
|
||||
if (Date.now() >= deadline) throw new Error(`Timed out waiting for auth profile lock: ${name}`)
|
||||
await new Promise((resolve) => setTimeout(resolve, 25))
|
||||
}
|
||||
}
|
||||
},
|
||||
catch: (cause) => new AuthProfileError({ message: `Could not lock auth profile: ${name}`, operation: "lock", reason: "write-failed", cause }),
|
||||
})
|
||||
return lockPath
|
||||
})
|
||||
return Effect.acquireUseRelease(
|
||||
acquire,
|
||||
() => effect,
|
||||
(lockPath) => Effect.tryPromise({
|
||||
try: () => fs.unlink(lockPath).catch(() => {}).then(() => {}),
|
||||
catch: (cause) => new AuthProfileError({ message: `Could not release auth profile lock: ${name}`, operation: "lock", reason: "write-failed", cause }),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
const writeProfile = Effect.fnUntraced(function* (options: {
|
||||
readonly name: string
|
||||
readonly slots: readonly CredentialSlot[]
|
||||
readonly baseDir?: string
|
||||
}) {
|
||||
const baseDir = options.baseDir ?? defaultBaseDir()
|
||||
const filePath = yield* profilePath(baseDir, options.name)
|
||||
const existing = yield* readOptional(options.name, { baseDir })
|
||||
const now = new Date().toISOString()
|
||||
const profile: AuthProfile = {
|
||||
version: 1,
|
||||
name: options.name,
|
||||
createdAt: existing?.createdAt ?? now,
|
||||
updatedAt: now,
|
||||
slots: options.slots.map((slot) => ({
|
||||
ref: slot.ref,
|
||||
value: slot.value,
|
||||
sources: [...slot.sources],
|
||||
...(slot.expiresAt ? { expiresAt: slot.expiresAt } : {}),
|
||||
})),
|
||||
}
|
||||
yield* Schema.decodeUnknownEffect(StoredAuthProfile)(profile).pipe(
|
||||
Effect.mapError((cause) => new AuthProfileError({ message: `Auth profile has an invalid shape: ${options.name}`, operation: "write", reason: "invalid-profile", cause })),
|
||||
)
|
||||
if (existing && sameSlots(existing.slots, profile.slots)) return summary(existing)
|
||||
yield* Effect.tryPromise({
|
||||
try: () => writeJsonFileAtomically(filePath, profile, { dirMode: 0o700 }),
|
||||
catch: (cause) => new AuthProfileError({ message: `Could not write auth profile: ${options.name}`, operation: "write", reason: "write-failed", cause }),
|
||||
})
|
||||
return summary(profile)
|
||||
})
|
||||
|
||||
export const write = Effect.fn("AuthProfile.write")(function* (options: {
|
||||
readonly name: string
|
||||
readonly slots: readonly CredentialSlot[]
|
||||
readonly baseDir?: string
|
||||
}) {
|
||||
return yield* writeLock.withPermit(writeProfile(options))
|
||||
})
|
||||
|
||||
export const status = Effect.fn("AuthProfile.status")(function* (name: string, options: AuthProfileOptions = {}) {
|
||||
return summary(yield* read(name, options))
|
||||
})
|
||||
|
||||
export const run = Effect.fn("AuthProfile.run")(function* (options: AuthRunOptions) {
|
||||
if (!options.command.trim()) {
|
||||
return yield* new AuthProfileError({ message: "Auth command must not be empty", operation: "run", reason: "run-failed" })
|
||||
}
|
||||
const timeoutMs = options.timeoutMs ?? 120_000
|
||||
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > maximumTimeoutMs) {
|
||||
return yield* new AuthProfileError({ message: `Auth command timeout must be an integer between 1 and ${maximumTimeoutMs} milliseconds`, operation: "run", reason: "run-failed" })
|
||||
}
|
||||
const maxOutputBytes = options.maxOutputBytes ?? 1_000_000
|
||||
if (!Number.isSafeInteger(maxOutputBytes) || maxOutputBytes < 0 || maxOutputBytes > maximumRunOutputBytes) {
|
||||
return yield* new AuthProfileError({ message: `Auth command output limit must be an integer between 0 and ${maximumRunOutputBytes} bytes`, operation: "run", reason: "run-failed" })
|
||||
}
|
||||
const profile = yield* read(options.name, { ...(options.baseDir ? { baseDir: options.baseDir } : {}) })
|
||||
const startedAt = Date.now()
|
||||
const maxSecretBytes = profile.slots.reduce((max, slot) => Math.max(max, Buffer.byteLength(slot.value)), 0)
|
||||
const result = yield* runChild({
|
||||
command: options.command,
|
||||
args: options.args ?? [],
|
||||
...(options.cwd ? { cwd: options.cwd } : {}),
|
||||
timeoutMs,
|
||||
maxOutputBytes: maxOutputBytes + maxSecretBytes,
|
||||
env: Object.fromEntries(profile.slots.map((slot) => [slot.ref, slot.value])),
|
||||
}).pipe(
|
||||
Effect.mapError((cause) => new AuthProfileError({ message: `Could not run command with auth profile ${options.name}`, operation: "run", reason: "run-failed", cause })),
|
||||
)
|
||||
const stdout = redactOutput(result.stdout, profile.slots, maxOutputBytes)
|
||||
const stderr = redactOutput(result.stderr, profile.slots, maxOutputBytes)
|
||||
return {
|
||||
...result,
|
||||
stdout: stdout.text,
|
||||
stderr: stderr.text,
|
||||
stdoutTruncated: result.stdoutTruncated || stdout.truncated,
|
||||
stderrTruncated: result.stderrTruncated || stderr.truncated,
|
||||
durationMs: Date.now() - startedAt,
|
||||
} satisfies AuthRunResult
|
||||
})
|
||||
|
||||
function summary(profile: AuthProfile): AuthProfileSummary {
|
||||
const now = Date.now()
|
||||
return {
|
||||
name: profile.name,
|
||||
createdAt: profile.createdAt,
|
||||
updatedAt: profile.updatedAt,
|
||||
slotCount: profile.slots.length,
|
||||
slots: profile.slots.map((slot) => ({
|
||||
ref: slot.ref,
|
||||
sources: [...slot.sources],
|
||||
...(slot.expiresAt ? { expiresAt: slot.expiresAt } : {}),
|
||||
expired: slot.expiresAt ? Date.parse(slot.expiresAt) <= now : false,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
function profilePath(baseDir: string, name: string): Effect.Effect<string, AuthProfileError> {
|
||||
return /^[a-z0-9][a-z0-9._-]{0,63}$/i.test(name)
|
||||
? Effect.succeed(path.join(baseDir, `${name}.json`))
|
||||
: Effect.fail(new AuthProfileError({ message: `Invalid auth profile name: ${name}`, operation: "path", reason: "invalid-name" }))
|
||||
}
|
||||
|
||||
function sameSlots(left: readonly CredentialSlot[], right: readonly CredentialSlot[]): boolean {
|
||||
return JSON.stringify(left) === JSON.stringify(right)
|
||||
}
|
||||
|
||||
function redactOutput(text: string, slots: readonly CredentialSlot[], maxBytes: number): { readonly text: string; readonly truncated: boolean } {
|
||||
const bytes = Buffer.from(text)
|
||||
let cutoff = Math.min(bytes.length, maxBytes)
|
||||
let changed = true
|
||||
while (changed) {
|
||||
changed = false
|
||||
for (const slot of slots) {
|
||||
if (!slot.value) continue
|
||||
const secret = Buffer.from(slot.value)
|
||||
let start = bytes.indexOf(secret)
|
||||
while (start >= 0 && start < cutoff) {
|
||||
if (start + secret.length > cutoff) {
|
||||
cutoff = start
|
||||
changed = true
|
||||
break
|
||||
}
|
||||
start = bytes.indexOf(secret, start + secret.length)
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
text: redactKnownValues(bytes.subarray(0, cutoff).toString("utf8"), slots),
|
||||
truncated: cutoff < bytes.length,
|
||||
}
|
||||
}
|
||||
|
||||
function runChild(options: {
|
||||
readonly command: string
|
||||
readonly args: readonly string[]
|
||||
readonly cwd?: string
|
||||
readonly timeoutMs: number
|
||||
readonly maxOutputBytes: number
|
||||
readonly env: Readonly<Record<string, string>>
|
||||
}): Effect.Effect<Omit<AuthRunResult, "durationMs">, Error> {
|
||||
return Effect.callback((resume) => {
|
||||
const child = spawn(options.command, [...options.args], {
|
||||
...(options.cwd ? { cwd: options.cwd } : {}),
|
||||
env: childEnvironment(options.env),
|
||||
detached: process.platform !== "win32",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
})
|
||||
const stdout: Buffer[] = []
|
||||
const stderr: Buffer[] = []
|
||||
let stdoutBytes = 0
|
||||
let stderrBytes = 0
|
||||
let stdoutTruncated = false
|
||||
let stderrTruncated = false
|
||||
const append = (chunks: Buffer[], chunk: Buffer, currentBytes: number): number => {
|
||||
const remaining = options.maxOutputBytes - currentBytes
|
||||
if (remaining <= 0) return currentBytes
|
||||
chunks.push(chunk.subarray(0, remaining))
|
||||
return currentBytes + Math.min(chunk.length, remaining)
|
||||
}
|
||||
child.stdout.on("data", (chunk: Buffer) => {
|
||||
const next = append(stdout, chunk, stdoutBytes)
|
||||
stdoutTruncated ||= next - stdoutBytes < chunk.length
|
||||
stdoutBytes = next
|
||||
})
|
||||
child.stderr.on("data", (chunk: Buffer) => {
|
||||
const next = append(stderr, chunk, stderrBytes)
|
||||
stderrTruncated ||= next - stderrBytes < chunk.length
|
||||
stderrBytes = next
|
||||
})
|
||||
let closed = false
|
||||
let settled = false
|
||||
let closePromiseResolve: (() => void) | undefined
|
||||
const closePromise = new Promise<void>((resolve) => {
|
||||
closePromiseResolve = resolve
|
||||
})
|
||||
const timeout = setTimeout(() => {
|
||||
void terminateChildProcess({ child, exit: closePromise, graceMs: 1_000, isExited: () => closed })
|
||||
}, options.timeoutMs)
|
||||
child.once("error", (cause) => {
|
||||
clearTimeout(timeout)
|
||||
closed = true
|
||||
closePromiseResolve?.()
|
||||
if (settled) return
|
||||
settled = true
|
||||
resume(Effect.fail(cause))
|
||||
})
|
||||
child.once("close", (code, signal) => {
|
||||
clearTimeout(timeout)
|
||||
closed = true
|
||||
closePromiseResolve?.()
|
||||
if (settled) return
|
||||
settled = true
|
||||
resume(Effect.succeed({
|
||||
exitCode: code ?? 1,
|
||||
signal,
|
||||
stdout: Buffer.concat(stdout).toString("utf8"),
|
||||
stderr: Buffer.concat(stderr).toString("utf8"),
|
||||
stdoutTruncated,
|
||||
stderrTruncated,
|
||||
}))
|
||||
})
|
||||
return Effect.promise(() => terminateChildProcess({ child, exit: closePromise, graceMs: 1_000, isExited: () => closed }))
|
||||
})
|
||||
}
|
||||
|
||||
function childEnvironment(profile: Readonly<Record<string, string>>): NodeJS.ProcessEnv {
|
||||
const inherited = Object.fromEntries(
|
||||
Object.entries(process.env).filter(([name]) => !/^BC_SECRET_[1-9][0-9]*$/.test(name)),
|
||||
)
|
||||
return { ...inherited, ...profile }
|
||||
}
|
||||
254
packages/browser-extension/cli/src/authenticated-origin.ts
Normal file
254
packages/browser-extension/cli/src/authenticated-origin.ts
Normal file
|
|
@ -0,0 +1,254 @@
|
|||
import { Effect, Match, Schema } from "effect"
|
||||
import type { Page } from "patchright-core"
|
||||
import {
|
||||
AuthenticatedJsonOutcome,
|
||||
type AuthenticatedJsonMethod,
|
||||
type AuthenticatedJsonRequest,
|
||||
} from "./relay-schema.ts"
|
||||
|
||||
const defaultTimeoutMs = 30_000
|
||||
const defaultMaxResponseBytes = 2_000_000
|
||||
|
||||
export class AuthenticatedOriginError extends Schema.TaggedError<AuthenticatedOriginError>()(
|
||||
"AuthenticatedOrigin.Error",
|
||||
{
|
||||
message: Schema.String,
|
||||
reason: Schema.Literals(["invalid-request", "page-failed"]),
|
||||
cause: Schema.optionalKey(Schema.Defect()),
|
||||
},
|
||||
) {}
|
||||
|
||||
type RequestOptions = Omit<AuthenticatedJsonRequest, "sessionId">
|
||||
|
||||
type PageRequestInput = {
|
||||
readonly origin: string
|
||||
readonly method: AuthenticatedJsonMethod
|
||||
readonly url: string
|
||||
readonly headers?: Readonly<Record<string, string>>
|
||||
readonly body?: unknown
|
||||
readonly timeoutMs: number
|
||||
readonly maxResponseBytes: number
|
||||
}
|
||||
|
||||
export const requestJson = Effect.fn("AuthenticatedOrigin.requestJson")(function* (
|
||||
page: Page,
|
||||
request: RequestOptions,
|
||||
) {
|
||||
const target = yield* Effect.try({
|
||||
try: () => {
|
||||
const origin = normalizeOrigin(request.origin)
|
||||
return {
|
||||
origin,
|
||||
requestUrl: resolveRequestUrl(origin, request.path),
|
||||
startUrl: request.startUrl === undefined ? undefined : resolveStartUrl(origin, request.startUrl),
|
||||
}
|
||||
},
|
||||
catch: (cause) => cause instanceof AuthenticatedOriginError
|
||||
? cause
|
||||
: new AuthenticatedOriginError({
|
||||
message: "Invalid authenticated origin request",
|
||||
reason: "invalid-request",
|
||||
cause,
|
||||
}),
|
||||
})
|
||||
const { origin, requestUrl, startUrl } = target
|
||||
|
||||
if (pageOrigin(page.url()) !== origin && startUrl !== undefined) {
|
||||
yield* Effect.tryPromise({
|
||||
try: () => page.goto(startUrl, { waitUntil: "domcontentloaded", timeout: request.timeoutMs ?? defaultTimeoutMs }),
|
||||
catch: (cause) => new AuthenticatedOriginError({
|
||||
message: cause instanceof Error ? cause.message : "Navigate to authenticated origin",
|
||||
reason: "page-failed",
|
||||
cause,
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
const actualOrigin = pageOrigin(page.url())
|
||||
if (actualOrigin !== origin) {
|
||||
return AuthenticatedJsonOutcome.cases.OriginMismatch.make({
|
||||
expectedOrigin: origin,
|
||||
actualOrigin,
|
||||
})
|
||||
}
|
||||
|
||||
const input: PageRequestInput = {
|
||||
origin,
|
||||
method: request.method,
|
||||
url: requestUrl,
|
||||
...(request.headers === undefined ? {} : { headers: request.headers }),
|
||||
...(request.body === undefined ? {} : { body: request.body }),
|
||||
timeoutMs: request.timeoutMs ?? defaultTimeoutMs,
|
||||
maxResponseBytes: request.maxResponseBytes ?? defaultMaxResponseBytes,
|
||||
}
|
||||
|
||||
const pageResult = yield* Effect.tryPromise({
|
||||
try: () => page.evaluate(runPageRequest, input),
|
||||
catch: (cause) => new AuthenticatedOriginError({
|
||||
message: cause instanceof Error ? cause.message : "Run authenticated page request",
|
||||
reason: "page-failed",
|
||||
cause,
|
||||
}),
|
||||
}).pipe(Effect.uninterruptible)
|
||||
|
||||
return Match.value(pageResult).pipe(
|
||||
Match.when({ kind: "OriginMismatch" }, ({ expectedOrigin, actualOrigin }) =>
|
||||
AuthenticatedJsonOutcome.cases.OriginMismatch.make({ expectedOrigin, actualOrigin })),
|
||||
Match.when({ kind: "HttpError" }, ({ status }) =>
|
||||
AuthenticatedJsonOutcome.cases.HttpError.make({ status })),
|
||||
Match.when({ kind: "ResponseTooLarge" }, ({ status, maxResponseBytes }) =>
|
||||
AuthenticatedJsonOutcome.cases.ResponseTooLarge.make({ status, maxResponseBytes })),
|
||||
Match.when({ kind: "Success" }, ({ status, value }) =>
|
||||
AuthenticatedJsonOutcome.cases.Success.make({ status, value })),
|
||||
Match.when({ kind: "InvalidJson" }, ({ status }) =>
|
||||
AuthenticatedJsonOutcome.cases.InvalidJson.make({ status })),
|
||||
Match.when({ kind: "RequestFailed" }, ({ outcome }) =>
|
||||
AuthenticatedJsonOutcome.cases.RequestFailed.make({ outcome })),
|
||||
Match.exhaustive,
|
||||
)
|
||||
})
|
||||
|
||||
export function normalizeOrigin(value: string): string {
|
||||
let url: URL
|
||||
try {
|
||||
url = new URL(value)
|
||||
} catch (cause) {
|
||||
throw new AuthenticatedOriginError({
|
||||
message: `Invalid authenticated origin: ${value}`,
|
||||
reason: "invalid-request",
|
||||
cause,
|
||||
})
|
||||
}
|
||||
if ((url.protocol !== "https:" && url.protocol !== "http:") || url.username || url.password) {
|
||||
throw new AuthenticatedOriginError({
|
||||
message: `Authenticated origin must be an HTTP(S) origin without credentials: ${value}`,
|
||||
reason: "invalid-request",
|
||||
})
|
||||
}
|
||||
if (url.pathname !== "/" || url.search || url.hash) {
|
||||
throw new AuthenticatedOriginError({
|
||||
message: `Authenticated origin must not include a path, query, or fragment: ${value}`,
|
||||
reason: "invalid-request",
|
||||
})
|
||||
}
|
||||
return url.origin
|
||||
}
|
||||
|
||||
function resolveRequestUrl(origin: string, path: string): string {
|
||||
if (!path.startsWith("/") || path.startsWith("//")) {
|
||||
throw new AuthenticatedOriginError({
|
||||
message: "Authenticated request path must start with one slash",
|
||||
reason: "invalid-request",
|
||||
})
|
||||
}
|
||||
const url = new URL(path, origin)
|
||||
if (url.origin !== origin) {
|
||||
throw new AuthenticatedOriginError({
|
||||
message: `Authenticated request escaped its declared origin: ${path}`,
|
||||
reason: "invalid-request",
|
||||
})
|
||||
}
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
export function resolveStartUrl(origin: string, value: string): string {
|
||||
const url = new URL(value, origin)
|
||||
if (url.origin !== origin) {
|
||||
throw new AuthenticatedOriginError({
|
||||
message: `Authenticated origin startUrl must stay on ${origin}`,
|
||||
reason: "invalid-request",
|
||||
})
|
||||
}
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
function pageOrigin(value: string): string {
|
||||
try {
|
||||
return new URL(value).origin
|
||||
} catch {
|
||||
return "null"
|
||||
}
|
||||
}
|
||||
|
||||
type PageRequestResult =
|
||||
| { readonly kind: "OriginMismatch"; readonly expectedOrigin: string; readonly actualOrigin: string }
|
||||
| { readonly kind: "HttpError"; readonly status: number }
|
||||
| { readonly kind: "ResponseTooLarge"; readonly status: number; readonly maxResponseBytes: number }
|
||||
| { readonly kind: "Success"; readonly status: number; readonly value: Schema.Schema.Type<typeof Schema.Json> }
|
||||
| { readonly kind: "InvalidJson"; readonly status: number }
|
||||
| { readonly kind: "RequestFailed"; readonly outcome: "not-sent" | "unknown" }
|
||||
|
||||
async function runPageRequest(input: PageRequestInput): Promise<PageRequestResult> {
|
||||
if (window.location.origin !== input.origin) {
|
||||
return {
|
||||
kind: "OriginMismatch",
|
||||
expectedOrigin: input.origin,
|
||||
actualOrigin: window.location.origin,
|
||||
}
|
||||
}
|
||||
|
||||
const controller = new AbortController()
|
||||
const timeout = window.setTimeout(() => controller.abort(), input.timeoutMs)
|
||||
let requestStarted = false
|
||||
try {
|
||||
const body = input.body === undefined ? undefined : JSON.stringify(input.body)
|
||||
const headers: Record<string, string> = {
|
||||
...(body === undefined ? {} : { "content-type": "application/json" }),
|
||||
...(input.headers ?? {}),
|
||||
}
|
||||
requestStarted = true
|
||||
const response = await window.fetch(input.url, {
|
||||
method: input.method,
|
||||
credentials: "same-origin",
|
||||
mode: "same-origin",
|
||||
redirect: "error",
|
||||
signal: controller.signal,
|
||||
...(Object.keys(headers).length > 0 ? { headers } : {}),
|
||||
...(body === undefined ? {} : { body }),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
await response.body?.cancel().catch(() => undefined)
|
||||
return { kind: "HttpError", status: response.status }
|
||||
}
|
||||
|
||||
const reader = response.body?.getReader()
|
||||
const decoder = new TextDecoder()
|
||||
let byteCount = 0
|
||||
let text = ""
|
||||
if (reader) {
|
||||
while (true) {
|
||||
const chunk = await reader.read()
|
||||
if (chunk.done) break
|
||||
byteCount += chunk.value.byteLength
|
||||
if (byteCount > input.maxResponseBytes) {
|
||||
await reader.cancel().catch(() => undefined)
|
||||
return {
|
||||
kind: "ResponseTooLarge",
|
||||
status: response.status,
|
||||
maxResponseBytes: input.maxResponseBytes,
|
||||
}
|
||||
}
|
||||
text += decoder.decode(chunk.value, { stream: true })
|
||||
}
|
||||
text += decoder.decode()
|
||||
}
|
||||
|
||||
try {
|
||||
return {
|
||||
kind: "Success",
|
||||
status: response.status,
|
||||
value: text ? JSON.parse(text) : null,
|
||||
}
|
||||
} catch {
|
||||
return { kind: "InvalidJson", status: response.status }
|
||||
}
|
||||
} catch {
|
||||
return {
|
||||
kind: "RequestFailed",
|
||||
outcome: requestStarted ? "unknown" : "not-sent",
|
||||
}
|
||||
} finally {
|
||||
window.clearTimeout(timeout)
|
||||
}
|
||||
}
|
||||
212
packages/browser-extension/cli/src/cdp-client-pool.ts
Normal file
212
packages/browser-extension/cli/src/cdp-client-pool.ts
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
import { Data } from "effect"
|
||||
import type { CdpEvent, JsonObject } from "./protocol.ts"
|
||||
import type { ChildTarget, ConnectedTarget } from "./relay-types.ts"
|
||||
|
||||
type ClientCdpSessionAlias = Data.TaggedEnum<{
|
||||
Browser: {}
|
||||
Target: { readonly tabId: number; readonly targetId: string; readonly chromeSessionId?: string }
|
||||
}>
|
||||
export const ClientCdpSessionAlias = Data.taggedEnum<ClientCdpSessionAlias>()
|
||||
|
||||
type AnnouncedTarget = {
|
||||
readonly tabId: number
|
||||
readonly targetId: string
|
||||
readonly sessionId: string
|
||||
readonly parentSessionId?: string
|
||||
}
|
||||
|
||||
type CdpClientState = {
|
||||
readonly aliases: Map<string, ClientCdpSessionAlias>
|
||||
readonly announcements: Map<string, AnnouncedTarget>
|
||||
readonly opencodeBrowserSessionId?: string
|
||||
readonly kind: "raw" | "sandbox"
|
||||
autoAttachParams?: JsonObject
|
||||
}
|
||||
|
||||
export class CdpClientPool<Client extends object> implements Iterable<Client> {
|
||||
private readonly states = new Map<Client, CdpClientState>()
|
||||
private nextAliasId = 1
|
||||
private connectionGeneration = 0
|
||||
|
||||
constructor(
|
||||
private readonly send: (client: Client, event: CdpEvent) => void,
|
||||
/** Observe every canonical session this client stops seeing, announced or silent. */
|
||||
private readonly retired: (client: Client, sessionId: string) => void,
|
||||
) {}
|
||||
|
||||
register(client: Client, opencodeBrowserSessionId?: string, kind: "raw" | "sandbox" = "raw"): void {
|
||||
if (this.states.has(client)) throw new Error("CDP client is already registered")
|
||||
this.states.set(client, {
|
||||
aliases: new Map(),
|
||||
announcements: new Map(),
|
||||
kind: opencodeBrowserSessionId ? kind : "raw",
|
||||
...(opencodeBrowserSessionId ? { opencodeBrowserSessionId } : {}),
|
||||
})
|
||||
this.connectionGeneration += 1
|
||||
}
|
||||
|
||||
unregister(client: Client): number | undefined {
|
||||
if (!this.states.delete(client) || this.states.size !== 0) return undefined
|
||||
return ++this.connectionGeneration
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this.states.size
|
||||
}
|
||||
|
||||
has(client: Client): boolean {
|
||||
return this.states.has(client)
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<Client> {
|
||||
return this.states.keys()
|
||||
}
|
||||
|
||||
sessionId(client: Client): string | undefined {
|
||||
return this.states.get(client)?.opencodeBrowserSessionId
|
||||
}
|
||||
|
||||
isSandbox(client: Client): boolean {
|
||||
return this.states.get(client)?.kind === "sandbox"
|
||||
}
|
||||
|
||||
hasSession(client: Client, sessionId: string): boolean {
|
||||
return this.states.get(client)?.announcements.has(sessionId) ?? false
|
||||
}
|
||||
|
||||
announce(client: Client, target: ConnectedTarget | ChildTarget): void {
|
||||
const state = this.requireState(client)
|
||||
const targetId = target.targetInfo.targetId
|
||||
const existing = Array.from(state.announcements.values()).find((entry) => entry.targetId === targetId)
|
||||
if (existing?.sessionId === target.sessionId) return
|
||||
if (existing) this.detachSession(client, state, existing.sessionId)
|
||||
this.detachSession(client, state, target.sessionId)
|
||||
const parent = "parentSessionId" in target ? { parentSessionId: target.parentSessionId } : {}
|
||||
state.announcements.set(target.sessionId, { tabId: target.tabId, targetId, sessionId: target.sessionId, ...parent })
|
||||
this.send(client, {
|
||||
...(parent.parentSessionId === undefined ? {} : { sessionId: parent.parentSessionId }),
|
||||
method: "Target.attachedToTarget",
|
||||
params: {
|
||||
sessionId: target.sessionId,
|
||||
targetInfo: { ...target.targetInfo, attached: true },
|
||||
waitingForDebugger: "waitingForDebugger" in target ? target.waitingForDebugger : false,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
detach(client: Client, sessionId: string): void {
|
||||
const state = this.requireState(client)
|
||||
if (state.aliases.delete(sessionId)) return
|
||||
this.detachSession(client, state, sessionId, { notify: false })
|
||||
}
|
||||
|
||||
detachTab(tabId: number, options: { readonly destroyed?: boolean } = {}): void {
|
||||
for (const [client, state] of this.states) {
|
||||
this.removeTargetAliases(state, (alias) => alias.tabId === tabId)
|
||||
for (const announced of state.announcements.values()) {
|
||||
if (announced.tabId === tabId) this.detachSession(client, state, announced.sessionId, options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
detachTarget(target: ChildTarget, options: { readonly notify?: boolean } = {}): void {
|
||||
for (const [client, state] of this.states) {
|
||||
this.removeTargetAliases(state, (alias) => alias.targetId === target.targetInfo.targetId)
|
||||
this.detachSession(client, state, target.sessionId, options)
|
||||
}
|
||||
}
|
||||
|
||||
pruneInvisible(client: Client, canSeeTab: (tabId: number) => boolean): void {
|
||||
const state = this.requireState(client)
|
||||
this.removeTargetAliases(state, (alias) => !canSeeTab(alias.tabId))
|
||||
for (const announced of state.announcements.values()) {
|
||||
if (!canSeeTab(announced.tabId)) this.detachSession(client, state, announced.sessionId)
|
||||
}
|
||||
}
|
||||
|
||||
sendToViewers(rootSessionId: string, event: CdpEvent, canSeeTab: (client: Client, tabId: number) => boolean): void {
|
||||
for (const [client, state] of this.states) {
|
||||
const announced = state.announcements.get(rootSessionId)
|
||||
if (!announced) continue
|
||||
if (!canSeeTab(client, announced.tabId)) {
|
||||
this.pruneInvisible(client, (tabId) => canSeeTab(client, tabId))
|
||||
continue
|
||||
}
|
||||
this.send(client, event)
|
||||
}
|
||||
}
|
||||
|
||||
setAutoAttachParams(client: Client, params: JsonObject | undefined): void {
|
||||
const state = this.requireState(client)
|
||||
if (params === undefined) delete state.autoAttachParams
|
||||
else state.autoAttachParams = params
|
||||
}
|
||||
|
||||
autoAttachParams(client: Client): JsonObject | undefined {
|
||||
return this.states.get(client)?.autoAttachParams
|
||||
}
|
||||
|
||||
isCurrentIdleGeneration(generation: number): boolean {
|
||||
return this.states.size === 0 && this.connectionGeneration === generation
|
||||
}
|
||||
|
||||
createBrowserAlias(client: Client): string {
|
||||
const aliasId = this.nextAlias("browser")
|
||||
this.requireState(client).aliases.set(aliasId, ClientCdpSessionAlias.Browser())
|
||||
return aliasId
|
||||
}
|
||||
|
||||
createTargetAlias(client: Client, target: ConnectedTarget | ChildTarget): string {
|
||||
const aliasId = this.nextAlias("session")
|
||||
this.requireState(client).aliases.set(aliasId, ClientCdpSessionAlias.Target({
|
||||
tabId: target.tabId,
|
||||
targetId: target.targetInfo.targetId,
|
||||
...("parentSessionId" in target ? { chromeSessionId: target.sessionId } : {}),
|
||||
}))
|
||||
return aliasId
|
||||
}
|
||||
|
||||
alias(client: Client, aliasId: string): ClientCdpSessionAlias | undefined {
|
||||
return this.states.get(client)?.aliases.get(aliasId)
|
||||
}
|
||||
|
||||
private detachSession(client: Client, state: CdpClientState, sessionId: string, options: {
|
||||
readonly notify?: boolean
|
||||
readonly destroyed?: boolean
|
||||
} = {}): void {
|
||||
const announced = state.announcements.get(sessionId)
|
||||
if (!announced) return
|
||||
state.announcements.delete(sessionId)
|
||||
this.removeTargetAliases(state, (alias) => alias.targetId === announced.targetId)
|
||||
// Descendants must disappear before their parent, including on replacement.
|
||||
for (const child of state.announcements.values()) {
|
||||
if (child.parentSessionId === sessionId) this.detachSession(client, state, child.sessionId, options)
|
||||
}
|
||||
this.retired(client, sessionId)
|
||||
if (options.notify === false) return
|
||||
if (options.destroyed && announced.parentSessionId === undefined) {
|
||||
this.send(client, { method: "Target.targetDestroyed", params: { targetId: announced.targetId } })
|
||||
}
|
||||
this.send(client, {
|
||||
...(announced.parentSessionId === undefined ? {} : { sessionId: announced.parentSessionId }),
|
||||
method: "Target.detachedFromTarget",
|
||||
params: { sessionId, targetId: announced.targetId },
|
||||
})
|
||||
}
|
||||
|
||||
private removeTargetAliases(state: CdpClientState, matches: (alias: Data.TaggedEnum.Value<ClientCdpSessionAlias, "Target">) => boolean): void {
|
||||
for (const [aliasId, alias] of state.aliases) {
|
||||
if (ClientCdpSessionAlias.$is("Target")(alias) && matches(alias)) state.aliases.delete(aliasId)
|
||||
}
|
||||
}
|
||||
|
||||
private nextAlias(kind: "browser" | "session"): string {
|
||||
return `bc-client-${kind}-${this.nextAliasId++}`
|
||||
}
|
||||
|
||||
private requireState(client: Client): CdpClientState {
|
||||
const state = this.states.get(client)
|
||||
if (!state) throw new Error("CDP client is not registered")
|
||||
return state
|
||||
}
|
||||
}
|
||||
44
packages/browser-extension/cli/src/cdp-guardrails.ts
Normal file
44
packages/browser-extension/cli/src/cdp-guardrails.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/**
|
||||
* Relay-level CDP guardrails.
|
||||
*
|
||||
* The relay refuses a small set of CDP methods that would destroy the user's
|
||||
* real browser state (OpenCode Browser drives the user's own browser, with
|
||||
* their real logins). Read-only sessions additionally refuse input-dispatching
|
||||
* methods so "go look at X" tasks cannot click or type.
|
||||
*
|
||||
* Pure module: given a method and session context, return a rejection message
|
||||
* or null. The relay turns a rejection into a normal CDP error response for
|
||||
* that command id, so the calling script fails loudly without breaking the
|
||||
* connection.
|
||||
*/
|
||||
|
||||
const alwaysBlocked = new Map<string, string>([
|
||||
["Network.clearBrowserCookies", "it would log the user out of every site in their browser"],
|
||||
["Network.clearBrowserCache", "it would clear the user's entire browser cache"],
|
||||
["Storage.clearCookies", "it would log the user out of every site in their browser"],
|
||||
["Browser.close", "it would close the user's browser"],
|
||||
])
|
||||
|
||||
const readOnlyBlockedPrefixes = ["Input."] as const
|
||||
|
||||
export type GuardContext = {
|
||||
readonly method: string
|
||||
readonly readOnly: boolean
|
||||
readonly sessionId?: string | undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human-readable rejection message when the method must be blocked,
|
||||
* or null when the command may be forwarded to the browser.
|
||||
*/
|
||||
export function guardCdpMethod(context: GuardContext): string | null {
|
||||
const reason = alwaysBlocked.get(context.method)
|
||||
if (reason) {
|
||||
return `OpenCode Browser blocked ${context.method}: ${reason}. This command is always blocked by the relay.`
|
||||
}
|
||||
if (context.readOnly && readOnlyBlockedPrefixes.some((prefix) => context.method.startsWith(prefix))) {
|
||||
const session = context.sessionId ? `Session ${context.sessionId}` : "This session"
|
||||
return `${session} is read-only: ${context.method} is blocked. Use a session created without --read-only to interact with pages.`
|
||||
}
|
||||
return null
|
||||
}
|
||||
166
packages/browser-extension/cli/src/cdp-router.ts
Normal file
166
packages/browser-extension/cli/src/cdp-router.ts
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
import { ClientCdpSessionAlias, type CdpClientPool } from "./cdp-client-pool.ts"
|
||||
import { canClientSeeTarget } from "./cdp-visibility.ts"
|
||||
import type { CdpRequest, TargetInfo } from "./protocol.ts"
|
||||
import { getString, isRestrictedTarget } from "./relay-helpers.ts"
|
||||
import type { ChildTarget, ConnectedTarget } from "./relay-types.ts"
|
||||
import { shouldExposeChildTarget, type TargetRegistry } from "./target-registry.ts"
|
||||
|
||||
const rootRoutableBrowserContextMethods = new Set([
|
||||
"Browser.grantPermissions",
|
||||
"Browser.resetPermissions",
|
||||
"Storage.getCookies",
|
||||
"Storage.setCookies",
|
||||
"Storage.clearCookies",
|
||||
])
|
||||
|
||||
export function isRootRoutableBrowserContextMethod(method: string): boolean {
|
||||
return rootRoutableBrowserContextMethods.has(method)
|
||||
}
|
||||
|
||||
export type CdpRoutedSession = {
|
||||
readonly tabId: number
|
||||
readonly rootSessionId: string
|
||||
readonly chromeSessionId?: string
|
||||
}
|
||||
|
||||
export class CdpRouter<Client extends object> {
|
||||
constructor(
|
||||
private readonly clients: CdpClientPool<Client>,
|
||||
private readonly registry: TargetRegistry,
|
||||
) {}
|
||||
|
||||
canSeeTarget(client: Client, target: ConnectedTarget): boolean {
|
||||
return this.clients.has(client) && this.canSessionSeeTarget(this.clients.sessionId(client), target)
|
||||
}
|
||||
|
||||
canSessionSeeTarget(clientSessionId: string | undefined, target: ConnectedTarget): boolean {
|
||||
return canClientSeeTarget({
|
||||
clientSessionId,
|
||||
targetOwnerSessionId: target.opencodeBrowserSessionId,
|
||||
targetOwner: target.owner,
|
||||
clientHasOwnedTarget: clientSessionId !== undefined && this.registry
|
||||
.listRootTargets()
|
||||
.some((candidate) => candidate.opencodeBrowserSessionId === clientSessionId),
|
||||
})
|
||||
}
|
||||
|
||||
canSeeTab(client: Client, tabId: number): boolean {
|
||||
const rootTarget = this.registry.tabTargets.get(tabId)
|
||||
return rootTarget ? this.canSeeTarget(client, rootTarget) : false
|
||||
}
|
||||
|
||||
preferredRoot(client: Client, browserContextId?: string): ConnectedTarget | undefined {
|
||||
const clientSessionId = this.clients.sessionId(client)
|
||||
const candidates = this.registry.listRootTargets().filter((target) => {
|
||||
if (target.crashed) return false
|
||||
if (browserContextId !== undefined && target.targetInfo.browserContextId !== browserContextId) return false
|
||||
return clientSessionId !== undefined
|
||||
? target.opencodeBrowserSessionId === clientSessionId
|
||||
: this.canSeeTarget(client, target)
|
||||
})
|
||||
if (candidates.length === 0) return undefined
|
||||
if (clientSessionId !== undefined || candidates.length === 1) return candidates[0]
|
||||
const contexts = new Set(candidates.map((target) => target.targetInfo.browserContextId))
|
||||
// CDP omits browserContextId for the singleton default context. Matching
|
||||
// absence is therefore a validated default-context identity, not a guess.
|
||||
if (contexts.size === 1) {
|
||||
return candidates[0]
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
isBrowserAlias(client: Client, sessionId: string): boolean {
|
||||
return ClientCdpSessionAlias.$is("Browser")(this.clients.alias(client, sessionId))
|
||||
}
|
||||
|
||||
visibleRoots(client: Client): ConnectedTarget[] {
|
||||
return this.registry.listRootTargets().filter((target) => this.canSeeTarget(client, target))
|
||||
}
|
||||
|
||||
rootForSession(client: Client, sessionId: string): ConnectedTarget | undefined {
|
||||
const target = this.registry.targets.get(sessionId)
|
||||
return target && this.canSeeTarget(client, target) ? target : undefined
|
||||
}
|
||||
|
||||
targetForAttach(client: Client, targetId: string): ConnectedTarget | ChildTarget | undefined {
|
||||
const root = this.registry.targetsByTargetId.get(targetId)
|
||||
if (root) return this.canSeeTarget(client, root) ? root : undefined
|
||||
const child = this.registry.childTargetsByTargetId.get(targetId)
|
||||
return child && this.canSeeTab(client, child.tabId) ? child : undefined
|
||||
}
|
||||
|
||||
targetInfo(client: Client, options: {
|
||||
readonly targetId?: string
|
||||
readonly sessionId?: string
|
||||
}): ConnectedTarget | ChildTarget | undefined {
|
||||
const alias = options.sessionId ? this.clients.alias(client, options.sessionId) : undefined
|
||||
const aliasedTargetId = ClientCdpSessionAlias.$is("Target")(alias) ? alias.targetId : undefined
|
||||
const target = (options.targetId
|
||||
? this.registry.targetsByTargetId.get(options.targetId) ?? this.registry.childTargetsByTargetId.get(options.targetId)
|
||||
: undefined) ??
|
||||
(aliasedTargetId
|
||||
? this.registry.targetsByTargetId.get(aliasedTargetId) ?? this.registry.childTargetsByTargetId.get(aliasedTargetId)
|
||||
: undefined) ??
|
||||
(options.sessionId
|
||||
? this.registry.targets.get(options.sessionId) ?? this.registry.childTargets.get(options.sessionId)
|
||||
: undefined)
|
||||
if (!target) return undefined
|
||||
return "owner" in target
|
||||
? this.canSeeTarget(client, target) ? target : undefined
|
||||
: this.canSeeTab(client, target.tabId) ? target : undefined
|
||||
}
|
||||
|
||||
session(client: Client, requestedSessionId: string): CdpRoutedSession | undefined {
|
||||
const sessionAlias = this.clients.alias(client, requestedSessionId)
|
||||
if (ClientCdpSessionAlias.$is("Browser")(sessionAlias)) return undefined
|
||||
const alias = ClientCdpSessionAlias.$is("Target")(sessionAlias) ? sessionAlias : undefined
|
||||
const target = alias
|
||||
? this.registry.targetsByTargetId.get(alias.targetId) ?? this.registry.childTargetsByTargetId.get(alias.targetId)
|
||||
: this.registry.targets.get(requestedSessionId) ?? this.registry.childTargets.get(requestedSessionId)
|
||||
if (!target || !this.canSeeTab(client, target.tabId)) return undefined
|
||||
const rootSessionId = this.registry.tabTargets.get(target.tabId)?.sessionId
|
||||
if (!rootSessionId) return undefined
|
||||
const expectedChromeSessionId = "owner" in target ? undefined : target.sessionId
|
||||
if (alias && (alias.tabId !== target.tabId || alias.chromeSessionId !== expectedChromeSessionId)) return undefined
|
||||
return {
|
||||
tabId: target.tabId,
|
||||
rootSessionId,
|
||||
...(expectedChromeSessionId ? { chromeSessionId: expectedChromeSessionId } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
resolveCommandRoute(client: Client, message: CdpRequest): CdpRoutedSession {
|
||||
const browserAlias = message.sessionId !== undefined && this.isBrowserAlias(client, message.sessionId)
|
||||
const rootRoutable = isRootRoutableBrowserContextMethod(message.method) && (!message.sessionId || browserAlias)
|
||||
const requestedBrowserContextId = getString(message.params, "browserContextId")
|
||||
const preferredRoot = rootRoutable ? this.preferredRoot(client, requestedBrowserContextId) : undefined
|
||||
const route = rootRoutable && preferredRoot
|
||||
? { tabId: preferredRoot.tabId, rootSessionId: preferredRoot.sessionId }
|
||||
: message.sessionId
|
||||
? this.session(client, message.sessionId)
|
||||
: undefined
|
||||
if (route) return route
|
||||
throw new Error(rootRoutable
|
||||
? requestedBrowserContextId !== undefined
|
||||
? `A healthy visible root target in browser context ${requestedBrowserContextId} is required for ${message.method}`
|
||||
: this.clients.sessionId(client) === undefined
|
||||
? `Exactly one visible browser context is required for ${message.method}`
|
||||
: `A healthy session-owned root target is required for ${message.method}`
|
||||
: message.sessionId
|
||||
? `Unknown CDP session ${message.sessionId} for ${message.method}`
|
||||
: `CDP sessionId is required for ${message.method}`)
|
||||
}
|
||||
|
||||
reconcileClient(client: Client): void {
|
||||
this.clients.pruneInvisible(client, (tabId) => this.canSeeTab(client, tabId))
|
||||
}
|
||||
|
||||
visibleTargetInfos(client: Client): TargetInfo[] {
|
||||
return this.registry.allTargetInfos({
|
||||
isRestrictedTarget,
|
||||
isVisibleTarget: (target) => {
|
||||
return this.canSeeTab(client, target.tabId) && ("owner" in target || shouldExposeChildTarget(target))
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
299
packages/browser-extension/cli/src/cdp-runtime.ts
Normal file
299
packages/browser-extension/cli/src/cdp-runtime.ts
Normal file
|
|
@ -0,0 +1,299 @@
|
|||
import { Deferred, Effect, Fiber, Predicate } from "effect"
|
||||
import type { CdpRoutedSession } from "./cdp-router.ts"
|
||||
import type { CdpEvent, JsonObject } from "./protocol.ts"
|
||||
import { getNumber, getObject, getString } from "./relay-helpers.ts"
|
||||
import { boundedToken, runtimeFailureKind } from "./runtime-diagnostics.ts"
|
||||
import type { TargetRegistry } from "./target-registry.ts"
|
||||
|
||||
const maxCachedContexts = 512
|
||||
|
||||
type Waiter = {
|
||||
readonly sessionId: string
|
||||
readonly ready: Deferred.Deferred<boolean>
|
||||
readonly nativeReady: (event: CdpEvent) => boolean
|
||||
/** Installed once the enable command succeeds. */
|
||||
replay?: () => boolean
|
||||
}
|
||||
|
||||
type Contexts = Map<number, CdpEvent>
|
||||
/** `contexts` is undefined once the cache overflowed and can no longer prove completeness. */
|
||||
type ContextCache = { readonly current: () => boolean; contexts: Contexts | undefined }
|
||||
type Requester = {
|
||||
readonly current: () => boolean
|
||||
readonly delivered: Map<number, string | undefined>
|
||||
frameId?: string
|
||||
}
|
||||
|
||||
const routeSessionId = (route: CdpRoutedSession) => route.chromeSessionId ?? route.rootSessionId
|
||||
const disableKey = (tabId: number, chromeSessionId?: string) => `${tabId}:${chromeSessionId ?? "root"}`
|
||||
const contextAux = (event: CdpEvent) => getObject(getObject(event.params?.context)?.auxData)
|
||||
|
||||
export class CdpRuntime<Client extends object> {
|
||||
private readonly waiters = new Set<Waiter>()
|
||||
private readonly contexts = new Map<string, ContextCache>()
|
||||
private readonly requesters = new WeakMap<Client, Map<string, Requester>>()
|
||||
private readonly disabling = new Map<string, number>()
|
||||
|
||||
constructor(private readonly options: {
|
||||
readonly registry: TargetRegistry
|
||||
readonly generation: () => number
|
||||
readonly send: (command: { readonly tabId: number; readonly sessionId?: string; readonly method: string; readonly params: JsonObject }) => Effect.Effect<JsonObject, Error>
|
||||
readonly sendEvent: (client: Client, event: CdpEvent) => void
|
||||
readonly trace?: (message: string) => void
|
||||
}) {}
|
||||
|
||||
notify(event: CdpEvent): void {
|
||||
if (event.sessionId) this.recordContextEvent(event.sessionId, event)
|
||||
if (event.method !== "Runtime.executionContextCreated" || contextAux(event)?.isDefault !== true) return
|
||||
// Replay missing sibling contexts before waking the native waiter: resolving
|
||||
// its Deferred can synchronously retire the waiter's pending replay.
|
||||
for (const waiter of this.waiters) this.settle(waiter)
|
||||
for (const waiter of this.waiters) {
|
||||
if (waiter.sessionId === event.sessionId && waiter.nativeReady(event)) Deferred.doneUnsafe(waiter.ready, Effect.succeed(true))
|
||||
}
|
||||
}
|
||||
|
||||
/** Observe the exact events actually sent to this canonical client. Aliases never enter here. */
|
||||
deliver(client: Client, event: CdpEvent): void {
|
||||
const requester = event.sessionId ? this.requesters.get(client)?.get(event.sessionId) : undefined
|
||||
const active = requester?.current() ? requester : undefined
|
||||
const context = getObject(event.params?.context)
|
||||
const id = getNumber(context, "id")
|
||||
const destroyedId = getNumber(event.params, "executionContextId")
|
||||
if (event.method === "Runtime.executionContextsCleared") active?.delivered.clear()
|
||||
if (event.method === "Runtime.executionContextDestroyed" && destroyedId !== undefined) {
|
||||
active?.delivered.delete(destroyedId)
|
||||
}
|
||||
if (event.method === "Runtime.executionContextCreated" && active?.frameId !== undefined && id !== undefined) {
|
||||
const unique = getString(context, "uniqueId")
|
||||
if (active.delivered.has(id) && active.delivered.get(id) === unique) return
|
||||
active.delivered.set(id, unique)
|
||||
}
|
||||
this.options.sendEvent(client, event)
|
||||
}
|
||||
|
||||
/** Construct at request admission; invoke with the result only after its successful response is sent. */
|
||||
frameTreeResponse(client: Client, route: CdpRoutedSession, canContinue: () => boolean): (result: unknown) => void {
|
||||
const requester = this.requester(client, route, canContinue)
|
||||
return (result) => {
|
||||
const frameId = getString(getObject(getObject(getObject(result)?.frameTree)?.frame), "id")
|
||||
if (!Predicate.isString(frameId) || !requester.current()) return
|
||||
requester.frameId = frameId
|
||||
for (const waiter of this.waiters) this.settle(waiter)
|
||||
}
|
||||
}
|
||||
|
||||
disconnect(client: Client): void {
|
||||
this.requesters.delete(client)
|
||||
}
|
||||
|
||||
detach(client: Client, sessionId: string): void {
|
||||
this.requesters.get(client)?.delete(sessionId)
|
||||
}
|
||||
|
||||
beginDisable(tabId: number, chromeSessionId?: string): () => void {
|
||||
const key = disableKey(tabId, chromeSessionId)
|
||||
this.disabling.set(key, (this.disabling.get(key) ?? 0) + 1)
|
||||
this.invalidate(tabId, chromeSessionId)
|
||||
return () => {
|
||||
const count = (this.disabling.get(key) ?? 1) - 1
|
||||
if (count === 0) this.disabling.delete(key)
|
||||
else this.disabling.set(key, count)
|
||||
}
|
||||
}
|
||||
|
||||
private recordContextEvent(sessionId: string, event: CdpEvent): void {
|
||||
if (!event.method.startsWith("Runtime.executionContext") && event.method !== "Page.frameDetached") return
|
||||
const cache = this.cacheFor(sessionId)
|
||||
if (!cache) return
|
||||
switch (event.method) {
|
||||
case "Runtime.executionContextsCleared":
|
||||
cache.contexts = new Map()
|
||||
return
|
||||
case "Runtime.executionContextDestroyed": {
|
||||
const id = getNumber(event.params, "executionContextId")
|
||||
if (id !== undefined) cache.contexts?.delete(id)
|
||||
return
|
||||
}
|
||||
case "Page.frameDetached":
|
||||
for (const [id, created] of cache.contexts ?? []) {
|
||||
if (contextAux(created)?.frameId === event.params?.frameId) cache.contexts?.delete(id)
|
||||
}
|
||||
return
|
||||
case "Runtime.executionContextCreated": {
|
||||
const id = getNumber(getObject(event.params?.context), "id")
|
||||
if (!cache.contexts || id === undefined) return
|
||||
cache.contexts.set(id, event)
|
||||
if (cache.contexts.size > maxCachedContexts) cache.contexts = undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private invalidate(tabId: number, chromeSessionId?: string): void {
|
||||
for (const [sessionId] of this.contexts) {
|
||||
const target = this.options.registry.targets.get(sessionId) ?? this.options.registry.childTargets.get(sessionId)
|
||||
if (target?.tabId === tabId && (chromeSessionId === undefined || sessionId === chromeSessionId)) this.contexts.delete(sessionId)
|
||||
}
|
||||
}
|
||||
|
||||
private cacheFor(sessionId: string): ContextCache | undefined {
|
||||
for (const [key, value] of this.contexts) if (!value.current()) this.contexts.delete(key)
|
||||
const root = this.options.registry.targets.get(sessionId)
|
||||
const child = this.options.registry.childTargets.get(sessionId)
|
||||
const parent = child ? this.options.registry.tabTargets.get(child.tabId) : undefined
|
||||
const route = root ? { tabId: root.tabId, rootSessionId: root.sessionId }
|
||||
: child && parent ? { tabId: child.tabId, rootSessionId: parent.sessionId, chromeSessionId: child.sessionId } : undefined
|
||||
if (!route) return undefined
|
||||
if (this.disabling.has(disableKey(route.tabId)) || this.disabling.has(disableKey(route.tabId, route.chromeSessionId))) return undefined
|
||||
let cache = this.contexts.get(sessionId)
|
||||
if (!cache) {
|
||||
cache = { current: this.capture(route), contexts: new Map() }
|
||||
this.contexts.set(sessionId, cache)
|
||||
}
|
||||
return cache
|
||||
}
|
||||
|
||||
private requester(client: Client, route: CdpRoutedSession, canContinue: () => boolean): Requester {
|
||||
const sessions = this.requesters.get(client) ?? new Map<string, Requester>()
|
||||
this.requesters.set(client, sessions)
|
||||
const sessionId = routeSessionId(route)
|
||||
const existing = sessions.get(sessionId)
|
||||
if (existing?.current()) return existing
|
||||
const captured = this.capture(route)
|
||||
const next: Requester = {
|
||||
current: () => captured() && canContinue() && this.requesters.get(client) === sessions && sessions.get(sessionId) === next,
|
||||
delivered: new Map(),
|
||||
}
|
||||
sessions.set(sessionId, next)
|
||||
return next
|
||||
}
|
||||
|
||||
readonly enable = Effect.fn("CdpRuntime.enable")(function* (
|
||||
this: CdpRuntime<Client>,
|
||||
route: CdpRoutedSession,
|
||||
params: JsonObject,
|
||||
canContinue: () => boolean,
|
||||
client?: Client,
|
||||
) {
|
||||
const current = this.capture(route)
|
||||
const permitted = () => current() && canContinue()
|
||||
// Raw CDP clients need not request a frame tree. Preserve their ordinary
|
||||
// native-event path; cached replay requires a frame-tree request already admitted.
|
||||
const requester = client ? this.requesters.get(client)?.get(routeSessionId(route)) : undefined
|
||||
const rootFrame = () => requester && permitted() && requester.current() ? requester.frameId : undefined
|
||||
const first = yield* this.observe(route, Effect.suspend(() => permitted()
|
||||
? this.options.send({
|
||||
tabId: route.tabId, method: "Runtime.enable", params,
|
||||
...(route.chromeSessionId === undefined ? {} : { sessionId: route.chromeSessionId }),
|
||||
})
|
||||
: Effect.fail(new Error("CDP target changed before Runtime.enable"))), client && requester ? {
|
||||
replay: () => {
|
||||
const frameId = rootFrame()
|
||||
return frameId !== undefined && this.replayCached(client, route, frameId)
|
||||
},
|
||||
nativeReady: (event) => {
|
||||
const frameId = rootFrame()
|
||||
return frameId !== undefined && contextAux(event)?.frameId === frameId
|
||||
},
|
||||
} : {})
|
||||
this.trace(route, `runtime-enable defaultContextSeen=${first.seen}`)
|
||||
if (!first.seen && permitted()) {
|
||||
// The shared debugger may acknowledge enable without replaying its context.
|
||||
// Never run the recovery cycle against a successor generation or new owner.
|
||||
const retry = yield* this.observe(route, this.reset(route, "Runtime.disable", {}, permitted).pipe(
|
||||
Effect.andThen(() => this.reset(route, "Runtime.enable", params, permitted)),
|
||||
))
|
||||
this.trace(route, `runtime-reset phase=missing-default-context defaultContextSeen=${retry.seen}`)
|
||||
}
|
||||
return first.result
|
||||
})
|
||||
|
||||
readonly disableIdle = Effect.fn("CdpRuntime.disableIdle")(function* (this: CdpRuntime<Client>, stillIdle: () => boolean) {
|
||||
const { registry } = this.options
|
||||
const routes: CdpRoutedSession[] = registry.listRootTargets().map((target) => ({ tabId: target.tabId, rootSessionId: target.sessionId }))
|
||||
for (const target of registry.childTargets.values()) {
|
||||
const root = registry.tabTargets.get(target.tabId)
|
||||
if (root) routes.push({ tabId: target.tabId, rootSessionId: root.sessionId, chromeSessionId: target.sessionId })
|
||||
}
|
||||
const targets = routes.map((route) => ({ route, current: this.capture(route) }))
|
||||
for (const { route, current } of targets) {
|
||||
if (!stillIdle()) break
|
||||
yield* this.reset(route, "Runtime.disable", {}, () => stillIdle() && current())
|
||||
}
|
||||
})
|
||||
|
||||
/** Replay a complete cache only when it already holds the requester's root-frame default context. */
|
||||
private replayCached(client: Client, route: CdpRoutedSession, frameId: string): boolean {
|
||||
const cache = this.contexts.get(routeSessionId(route))
|
||||
const contexts = cache?.current() ? cache.contexts : undefined
|
||||
if (!contexts || ![...contexts.values()].some((event) => {
|
||||
const aux = contextAux(event)
|
||||
return aux?.isDefault === true && aux.frameId === frameId
|
||||
})) return false
|
||||
for (const event of contexts.values()) this.deliver(client, event)
|
||||
this.trace(route, "runtime-replay defaultContextSeen=true")
|
||||
return true
|
||||
}
|
||||
|
||||
private settle(waiter: Waiter): void {
|
||||
if (waiter.replay?.()) Deferred.doneUnsafe(waiter.ready, Effect.succeed(true))
|
||||
}
|
||||
|
||||
private observe<A>(route: CdpRoutedSession, command: Effect.Effect<A, Error>, options: {
|
||||
readonly replay?: () => boolean
|
||||
readonly nativeReady?: (event: CdpEvent) => boolean
|
||||
} = {}): Effect.Effect<{ readonly result: A; readonly seen: boolean }, Error> {
|
||||
const runtime = this
|
||||
return Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
const waiter: Waiter = { sessionId: routeSessionId(route), ready: Deferred.makeUnsafe(), nativeReady: options.nativeReady ?? (() => true) }
|
||||
runtime.waiters.add(waiter)
|
||||
return waiter
|
||||
}),
|
||||
(waiter) => Effect.gen(function* () {
|
||||
// Start the window before sending: context events may precede the reply.
|
||||
const seen = yield* Effect.forkScoped(Deferred.await(waiter.ready).pipe(Effect.timeoutOrElse({
|
||||
duration: "3 seconds",
|
||||
orElse: () => Effect.succeed(false),
|
||||
})), { startImmediately: true })
|
||||
const result = yield* command
|
||||
if (options.replay) waiter.replay = options.replay
|
||||
runtime.settle(waiter)
|
||||
return { result, seen: yield* Fiber.join(seen) }
|
||||
}).pipe(Effect.scoped),
|
||||
(waiter) => Effect.sync(() => { runtime.waiters.delete(waiter) }),
|
||||
)
|
||||
}
|
||||
|
||||
private reset(route: CdpRoutedSession, method: "Runtime.enable" | "Runtime.disable", params: JsonObject, current: () => boolean): Effect.Effect<void> {
|
||||
return Effect.suspend(() => {
|
||||
if (!current()) return Effect.void
|
||||
this.trace(route, `runtime-reset command=${method}`)
|
||||
return this.options.send({ tabId: route.tabId, method, params, ...(route.chromeSessionId === undefined ? {} : { sessionId: route.chromeSessionId }) }).pipe(
|
||||
Effect.match({
|
||||
onFailure: (error) => this.trace(route, `runtime-reset command=${method} outcome=failed failure=${runtimeFailureKind(error)}`),
|
||||
onSuccess: () => this.trace(route, `runtime-reset command=${method} outcome=ok`),
|
||||
}),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
private capture(route: CdpRoutedSession): () => boolean {
|
||||
const { registry } = this.options
|
||||
const generation = this.options.generation()
|
||||
const root = registry.targets.get(route.rootSessionId)
|
||||
const owner = root?.opencodeBrowserSessionId
|
||||
const child = route.chromeSessionId === undefined ? undefined : registry.childTargets.get(route.chromeSessionId)
|
||||
return () => {
|
||||
const currentRoot = registry.routingRootTarget(route.tabId)
|
||||
if (!root || generation !== this.options.generation() || currentRoot?.sessionId !== root.sessionId || currentRoot.targetInfo.targetId !== root.targetInfo.targetId || currentRoot.opencodeBrowserSessionId !== owner) return false
|
||||
if (route.chromeSessionId === undefined) return true
|
||||
const currentChild = registry.childTargets.get(route.chromeSessionId)
|
||||
return child !== undefined && currentChild?.tabId === route.tabId && currentChild.targetInfo.targetId === child.targetInfo.targetId && currentChild.parentSessionId === child.parentSessionId
|
||||
}
|
||||
}
|
||||
|
||||
private trace(route: CdpRoutedSession, message: string): void {
|
||||
this.options.trace?.(`${message} tab=${route.tabId} rootSession=${boundedToken(route.rootSessionId)} chromeSession=${boundedToken(route.chromeSessionId)}`)
|
||||
}
|
||||
}
|
||||
133
packages/browser-extension/cli/src/cdp-shims.ts
Normal file
133
packages/browser-extension/cli/src/cdp-shims.ts
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
import type { WebSocket } from "ws"
|
||||
import type { CdpRequest, JsonObject, TargetInfo } from "./protocol.ts"
|
||||
import { getObject, getString, sendCdpEvent } from "./relay-helpers.ts"
|
||||
import type { ChildTarget } from "./relay-types.ts"
|
||||
import { shouldExposeChildTarget, type TargetRegistry } from "./target-registry.ts"
|
||||
import type { CdpClientPool } from "./cdp-client-pool.ts"
|
||||
|
||||
export function removeDefaultLightColorSchemeEmulation(message: CdpRequest): CdpRequest {
|
||||
if (message.method !== "Emulation.setEmulatedMedia") {
|
||||
return message
|
||||
}
|
||||
const features = Array.isArray(message.params?.features) ? message.params.features : []
|
||||
const isExplicit = features.some((feature) => {
|
||||
const object = getObject(feature)
|
||||
return object?.name === "__bc_explicit__"
|
||||
})
|
||||
if (isExplicit) {
|
||||
return {
|
||||
...message,
|
||||
params: {
|
||||
...message.params,
|
||||
features: features.filter((feature) => {
|
||||
const object = getObject(feature)
|
||||
return object?.name !== "__bc_explicit__"
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
const hasDefaultLightColorScheme = features.some((feature) => {
|
||||
const object = getObject(feature)
|
||||
return object?.name === "prefers-color-scheme" && object.value === "light"
|
||||
})
|
||||
if (!hasDefaultLightColorScheme) {
|
||||
return message
|
||||
}
|
||||
return {
|
||||
...message,
|
||||
params: {
|
||||
...message.params,
|
||||
features: features.filter((feature) => {
|
||||
const object = getObject(feature)
|
||||
return object?.name !== "prefers-color-scheme"
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function replayChildTargetsForParent<Client extends Pick<WebSocket, "send">>(options: {
|
||||
readonly socket: Client
|
||||
readonly parentSessionId: string
|
||||
readonly registry: TargetRegistry
|
||||
readonly clients: CdpClientPool<Client>
|
||||
}): void {
|
||||
for (const target of options.registry.childTargets.values()) {
|
||||
if (target.parentSessionId === options.parentSessionId && shouldExposeChildTarget(target)) {
|
||||
replayFrameEventsForChild({ socket: options.socket, registry: options.registry, target })
|
||||
options.clients.announce(options.socket, target)
|
||||
replayChildFrameNavigation({ socket: options.socket, registry: options.registry, target })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function replayFrameEventsForChild(options: { readonly socket: Pick<WebSocket, "send">; readonly registry: TargetRegistry; readonly target: ChildTarget }): void {
|
||||
if (options.target.targetInfo.type !== "iframe") {
|
||||
return
|
||||
}
|
||||
const frameEvents = options.registry.tabFrameEvents.get(options.target.tabId)?.get(options.target.targetInfo.targetId)
|
||||
if (!frameEvents) {
|
||||
return
|
||||
}
|
||||
if (frameEvents.attached) {
|
||||
sendCdpEvent(options.socket, { sessionId: options.target.parentSessionId, method: "Page.frameAttached", params: frameEvents.attached })
|
||||
}
|
||||
if (frameEvents.navigated) {
|
||||
sendCdpEvent(options.socket, { sessionId: options.target.parentSessionId, method: "Page.frameNavigated", params: frameEvents.navigated })
|
||||
}
|
||||
}
|
||||
|
||||
export function replayChildFrameNavigation(options: { readonly socket: Pick<WebSocket, "send">; readonly registry: TargetRegistry; readonly target: ChildTarget }): void {
|
||||
const navigationParams = childFrameNavigationParams({ registry: options.registry, target: options.target })
|
||||
if (!navigationParams) {
|
||||
return
|
||||
}
|
||||
// Stock Playwright does not apply Page.getFrameTree to child iframe sessions;
|
||||
// replay the current navigation on the child session so reconnects do not
|
||||
// leave OOPIF frames with an empty URL.
|
||||
sendCdpEvent(options.socket, { sessionId: options.target.sessionId, method: "Page.frameNavigated", params: navigationParams })
|
||||
}
|
||||
|
||||
function childFrameNavigationParams(options: { readonly registry: TargetRegistry; readonly target: ChildTarget }): JsonObject | undefined {
|
||||
if (options.target.targetInfo.type !== "iframe") {
|
||||
return undefined
|
||||
}
|
||||
const frameEvents = options.registry.findFrameEventsForChild(options.target)
|
||||
const navigated = frameEvents?.navigated
|
||||
const frame = getObject(navigated?.frame)
|
||||
if (navigated && frame) {
|
||||
return {
|
||||
...navigated,
|
||||
frame: {
|
||||
...frame,
|
||||
id: options.target.targetInfo.targetId,
|
||||
url: options.target.targetInfo.url || (getString(frame, "url") ?? ""),
|
||||
...(options.target.targetInfo.parentFrameId ? { parentId: options.target.targetInfo.parentFrameId } : {}),
|
||||
},
|
||||
}
|
||||
}
|
||||
if (!options.target.targetInfo.url) {
|
||||
return undefined
|
||||
}
|
||||
const gatedAPIFeatures: string[] = []
|
||||
return {
|
||||
frame: {
|
||||
id: options.target.targetInfo.targetId,
|
||||
loaderId: options.target.targetInfo.targetId,
|
||||
url: options.target.targetInfo.url,
|
||||
domainAndRegistry: "",
|
||||
securityOrigin: new URL(options.target.targetInfo.url).origin,
|
||||
mimeType: "text/html",
|
||||
adFrameStatus: { adFrameType: "none" },
|
||||
secureContextType: "Secure",
|
||||
crossOriginIsolatedContextType: "NotIsolated",
|
||||
gatedAPIFeatures,
|
||||
...(options.target.targetInfo.parentFrameId ? { parentId: options.target.targetInfo.parentFrameId } : {}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function replayTargetCreated(options: { readonly socket: Pick<WebSocket, "send">; readonly targetInfos: readonly TargetInfo[] }): void {
|
||||
for (const targetInfo of options.targetInfos) {
|
||||
sendCdpEvent(options.socket, { method: "Target.targetCreated", params: { targetInfo } })
|
||||
}
|
||||
}
|
||||
36
packages/browser-extension/cli/src/cdp-visibility.ts
Normal file
36
packages/browser-extension/cli/src/cdp-visibility.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* Per-client CDP target visibility.
|
||||
*
|
||||
* Each OpenCode Browser session's sandbox connects as its own CDP client and
|
||||
* identifies itself with a OpenCode Browser session id. Tabs created for a
|
||||
* session are owned by that session. Without scoping, every client is told
|
||||
* about every tab, so concurrently connected clients attach to and
|
||||
* double-initialize each other's pages, which makes
|
||||
* `newPage`/`setContent`/`evaluate` hang non-deterministically.
|
||||
*
|
||||
* Visibility rule:
|
||||
* - Session-owned targets are visible only to that session's clients.
|
||||
* - User toolbar-attached targets stay visible to every client, so
|
||||
* `--target-url` recovery keeps working.
|
||||
* - Relay-created targets without a OpenCode Browser session id belong to raw
|
||||
* `connectOverCDP` clients. They are visible to raw clients and to a session
|
||||
* client that does not already own a target, so explicit `--target-url`
|
||||
* adoption can still find existing attached pages. Once a session has its own
|
||||
* sandbox target, it must not attach to raw-client pages, or Playwright
|
||||
* double-initializes the page on Chrome's single debugger attachment and
|
||||
* `locator.evaluate` can wedge.
|
||||
*
|
||||
* Two simultaneous raw clients can still interfere with each other's tabs;
|
||||
* OpenCode Browser sessions are the isolated, supported path.
|
||||
*/
|
||||
export function canClientSeeTarget(options: {
|
||||
readonly clientSessionId: string | undefined
|
||||
readonly targetOwnerSessionId: string | undefined
|
||||
readonly targetOwner: "relay" | "user"
|
||||
readonly clientHasOwnedTarget: boolean
|
||||
}): boolean {
|
||||
if (options.targetOwnerSessionId === undefined) {
|
||||
return options.targetOwner === "user" || options.clientSessionId === undefined || !options.clientHasOwnedTarget
|
||||
}
|
||||
return options.clientSessionId === options.targetOwnerSessionId
|
||||
}
|
||||
35
packages/browser-extension/cli/src/child-process.ts
Normal file
35
packages/browser-extension/cli/src/child-process.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import type { ChildProcess } from "node:child_process"
|
||||
|
||||
export async function terminateChildProcess(options: {
|
||||
readonly child: ChildProcess
|
||||
readonly exit: Promise<unknown>
|
||||
readonly graceMs: number
|
||||
readonly isExited?: () => boolean
|
||||
}): Promise<void> {
|
||||
const isExited = options.isExited ?? (() => options.child.exitCode !== null || options.child.signalCode !== null)
|
||||
if (isExited()) return
|
||||
signalChildProcess(options.child, "SIGTERM")
|
||||
let timeout: ReturnType<typeof setTimeout> | undefined
|
||||
await Promise.race([
|
||||
options.exit,
|
||||
new Promise<void>((resolve) => {
|
||||
timeout = setTimeout(resolve, options.graceMs)
|
||||
}),
|
||||
])
|
||||
if (timeout) clearTimeout(timeout)
|
||||
if (isExited()) return
|
||||
signalChildProcess(options.child, "SIGKILL")
|
||||
await options.exit
|
||||
}
|
||||
|
||||
function signalChildProcess(child: ChildProcess, signal: NodeJS.Signals): void {
|
||||
if (process.platform !== "win32" && child.pid !== undefined) {
|
||||
try {
|
||||
process.kill(-child.pid, signal)
|
||||
return
|
||||
} catch {
|
||||
// The child may have exited between the state check and the signal.
|
||||
}
|
||||
}
|
||||
child.kill(signal)
|
||||
}
|
||||
1094
packages/browser-extension/cli/src/cli.ts
Normal file
1094
packages/browser-extension/cli/src/cli.ts
Normal file
File diff suppressed because it is too large
Load diff
246
packages/browser-extension/cli/src/demonstration.ts
Normal file
246
packages/browser-extension/cli/src/demonstration.ts
Normal file
|
|
@ -0,0 +1,246 @@
|
|||
import { Match, Schema } from "effect"
|
||||
import type { Frame, Page } from "patchright-core"
|
||||
|
||||
const DemonstrationStep = Schema.Union([
|
||||
Schema.Struct({
|
||||
kind: Schema.Literal("click"),
|
||||
selector: Schema.String,
|
||||
role: Schema.optionalKey(Schema.String),
|
||||
name: Schema.optionalKey(Schema.String),
|
||||
}),
|
||||
Schema.Struct({
|
||||
kind: Schema.Literal("fill"),
|
||||
selector: Schema.String,
|
||||
value: Schema.String,
|
||||
redacted: Schema.optionalKey(Schema.Boolean),
|
||||
}),
|
||||
Schema.Struct({
|
||||
kind: Schema.Literal("check"),
|
||||
selector: Schema.String,
|
||||
checked: Schema.Boolean,
|
||||
}),
|
||||
Schema.Struct({
|
||||
kind: Schema.Literal("select"),
|
||||
selector: Schema.String,
|
||||
value: Schema.String,
|
||||
}),
|
||||
Schema.Struct({
|
||||
kind: Schema.Literal("navigation"),
|
||||
url: Schema.String,
|
||||
}),
|
||||
])
|
||||
|
||||
export type DemonstrationStep = typeof DemonstrationStep.Type
|
||||
|
||||
export type DemonstrationResult = {
|
||||
readonly startedUrl: string
|
||||
readonly endedUrl: string
|
||||
readonly steps: readonly DemonstrationStep[]
|
||||
readonly code: string
|
||||
}
|
||||
|
||||
type PageRecorderState = {
|
||||
active?: { readonly id: string; readonly startedUrl: string; readonly steps: DemonstrationStep[] }
|
||||
}
|
||||
|
||||
const demonstrationBindingName = "__opencodeBrowserDemonstration__"
|
||||
const pageRecorders = new WeakMap<Page, PageRecorderState>()
|
||||
let nextRecorder = 0
|
||||
|
||||
export async function startDemonstrationRecorder(page: Page): Promise<{ readonly stop: () => Promise<DemonstrationResult> }> {
|
||||
const state = await recorderState(page)
|
||||
if (state.active) throw new Error("A human demonstration is already being recorded on this page")
|
||||
const id = `demo-${++nextRecorder}`
|
||||
const active = { id, startedUrl: page.url(), steps: [] as DemonstrationStep[] }
|
||||
state.active = active
|
||||
const pendingInstalls = new Set<Promise<void>>()
|
||||
const install = (frame: Frame): Promise<void> => {
|
||||
const pending = frame.evaluate(installDemonstrationListeners, { recorderId: id })
|
||||
.then(() => {}, () => {})
|
||||
.finally(() => pendingInstalls.delete(pending))
|
||||
pendingInstalls.add(pending)
|
||||
return pending
|
||||
}
|
||||
const onFrameNavigated = (frame: Frame) => {
|
||||
if (frame === page.mainFrame()) appendStep(active.steps, { kind: "navigation", url: frame.url() })
|
||||
void install(frame)
|
||||
}
|
||||
page.on("framenavigated", onFrameNavigated)
|
||||
await Promise.all(page.frames().map(install))
|
||||
let stopped = false
|
||||
return {
|
||||
stop: async () => {
|
||||
if (stopped) throw new Error("Human demonstration recording has already stopped")
|
||||
stopped = true
|
||||
page.off("framenavigated", onFrameNavigated)
|
||||
await Promise.all([...pendingInstalls])
|
||||
await Promise.all(page.frames().map((frame) => frame.evaluate(removeDemonstrationListeners, { recorderId: id }).catch(() => {})))
|
||||
if (state.active?.id === id) delete state.active
|
||||
const steps = [...active.steps]
|
||||
const endedUrl = page.isClosed() ? active.startedUrl : page.url()
|
||||
return {
|
||||
startedUrl: active.startedUrl,
|
||||
endedUrl,
|
||||
steps,
|
||||
code: formatDemonstrationCode({ startedUrl: active.startedUrl, steps }),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function formatDemonstrationCode(options: {
|
||||
readonly startedUrl: string
|
||||
readonly steps: readonly DemonstrationStep[]
|
||||
}): string {
|
||||
const lines = [
|
||||
`// Recorded from ${options.startedUrl}`,
|
||||
...options.steps.map((step) =>
|
||||
Match.value(step).pipe(
|
||||
Match.when({ kind: "navigation" }, ({ url }) => `// Navigated to ${url}`),
|
||||
Match.when({ kind: "click" }, ({ selector, role, name }) => {
|
||||
const description = role && name ? ` // ${role} ${JSON.stringify(name)}` : ""
|
||||
return `await page.locator(${JSON.stringify(selector)}).click()${description}`
|
||||
}),
|
||||
Match.when({ kind: "fill" }, ({ selector, value, redacted }) =>
|
||||
redacted
|
||||
? `// Fill ${JSON.stringify(selector)} from an approved secret source.`
|
||||
: `await page.locator(${JSON.stringify(selector)}).fill(${JSON.stringify(value)})`),
|
||||
Match.when({ kind: "check" }, ({ selector, checked }) =>
|
||||
`await page.locator(${JSON.stringify(selector)}).${checked ? "check" : "uncheck"}()`),
|
||||
Match.when({ kind: "select" }, ({ selector, value }) =>
|
||||
`await page.locator(${JSON.stringify(selector)}).selectOption(${JSON.stringify(value)})`),
|
||||
Match.exhaustive,
|
||||
),
|
||||
),
|
||||
"return { url: page.url(), title: await page.title() }",
|
||||
]
|
||||
return lines.join("\n")
|
||||
}
|
||||
|
||||
async function recorderState(page: Page): Promise<PageRecorderState> {
|
||||
const existing = pageRecorders.get(page)
|
||||
if (existing) return existing
|
||||
const state: PageRecorderState = {}
|
||||
pageRecorders.set(page, state)
|
||||
await page.exposeBinding(demonstrationBindingName, (_source, value: unknown) => {
|
||||
const active = state.active
|
||||
if (!active || !isDemonstrationStep(value)) return
|
||||
appendStep(active.steps, value)
|
||||
})
|
||||
return state
|
||||
}
|
||||
|
||||
function appendStep(steps: DemonstrationStep[], next: DemonstrationStep): void {
|
||||
const previous = steps.at(-1)
|
||||
if (next.kind === "fill" && previous?.kind === "fill" && previous.selector === next.selector) {
|
||||
steps[steps.length - 1] = next
|
||||
return
|
||||
}
|
||||
if (next.kind === "navigation" && previous?.kind === "navigation" && previous.url === next.url) return
|
||||
steps.push(next)
|
||||
}
|
||||
|
||||
const isDemonstrationStep = Schema.is(DemonstrationStep)
|
||||
|
||||
function installDemonstrationListeners(options: { readonly recorderId: string }): void {
|
||||
type BrowserState = { readonly id: string; readonly cleanup: () => void }
|
||||
const browserWindow = window as Window & {
|
||||
__opencodeBrowserDemonstration__?: (step: DemonstrationStep) => unknown
|
||||
__opencodeBrowserDemonstrationListeners__?: BrowserState
|
||||
}
|
||||
browserWindow.__opencodeBrowserDemonstrationListeners__?.cleanup()
|
||||
const send = (step: DemonstrationStep) => {
|
||||
const binding = browserWindow.__opencodeBrowserDemonstration__
|
||||
if (binding instanceof Function) void Promise.resolve(binding(step)).catch(() => {})
|
||||
}
|
||||
const cssPath = (element: Element): string => {
|
||||
const id = element.getAttribute("id")
|
||||
if (id) return `#${CSS.escape(id)}`
|
||||
for (const attribute of ["data-testid", "data-test", "name", "aria-label"]) {
|
||||
const value = element.getAttribute(attribute)
|
||||
if (!value) continue
|
||||
const candidate = `[${attribute}="${CSS.escape(value)}"]`
|
||||
if (document.querySelectorAll(candidate).length === 1) return candidate
|
||||
}
|
||||
const parent = element.parentElement
|
||||
const tag = element.tagName.toLowerCase()
|
||||
if (!parent) return tag
|
||||
const siblings = Array.from(parent.children).filter((candidate) => candidate.tagName === element.tagName)
|
||||
return `${cssPath(parent)} > ${tag}:nth-of-type(${siblings.indexOf(element) + 1})`
|
||||
}
|
||||
const nameFor = (element: Element): string | undefined => {
|
||||
const ariaLabel = element.getAttribute("aria-label")?.trim()
|
||||
if (ariaLabel) return ariaLabel
|
||||
const labelledBy = element.getAttribute("aria-labelledby")
|
||||
if (labelledBy) {
|
||||
const text = labelledBy.split(/\s+/).map((id) => document.getElementById(id)?.textContent ?? "").join(" ").replace(/\s+/g, " ").trim()
|
||||
if (text) return text
|
||||
}
|
||||
const text = element.textContent?.replace(/\s+/g, " ").trim()
|
||||
return text ? text.slice(0, 120) : undefined
|
||||
}
|
||||
const roleFor = (element: Element): string | undefined => {
|
||||
const explicit = element.getAttribute("role")
|
||||
if (explicit) return explicit
|
||||
if (element instanceof HTMLButtonElement) return "button"
|
||||
if (element instanceof HTMLAnchorElement) return "link"
|
||||
if (element instanceof HTMLInputElement) {
|
||||
if (element.type === "checkbox") return "checkbox"
|
||||
if (element.type === "radio") return "radio"
|
||||
return "textbox"
|
||||
}
|
||||
if (element instanceof HTMLSelectElement) return "combobox"
|
||||
if (element instanceof HTMLTextAreaElement || (element instanceof HTMLElement && element.isContentEditable)) return "textbox"
|
||||
return undefined
|
||||
}
|
||||
const onClick = (event: Event) => {
|
||||
const target = event.composedPath().find((candidate) => candidate instanceof Element) as Element | undefined
|
||||
const actionable = target?.closest("button, a[href], summary, [role='button'], [role='link'], [role='tab'], [role='menuitem']")
|
||||
if (!actionable) return
|
||||
const role = roleFor(actionable)
|
||||
const name = nameFor(actionable)
|
||||
send({ kind: "click", selector: cssPath(actionable), ...(role === undefined ? {} : { role }), ...(name === undefined ? {} : { name }) })
|
||||
}
|
||||
const onInput = (event: Event) => {
|
||||
const element = event.target
|
||||
if (element instanceof HTMLInputElement) {
|
||||
if (["checkbox", "radio", "file", "button", "submit", "reset"].includes(element.type)) return
|
||||
send({ kind: "fill", selector: cssPath(element), value: element.type === "password" ? "" : element.value, ...(element.type === "password" ? { redacted: true } : {}) })
|
||||
} else if (element instanceof HTMLTextAreaElement) {
|
||||
send({ kind: "fill", selector: cssPath(element), value: element.value })
|
||||
} else if (element instanceof HTMLElement && element.isContentEditable) {
|
||||
send({ kind: "fill", selector: cssPath(element), value: element.innerText })
|
||||
}
|
||||
}
|
||||
const onChange = (event: Event) => {
|
||||
const element = event.target
|
||||
if (element instanceof HTMLInputElement && (element.type === "checkbox" || element.type === "radio")) {
|
||||
send({ kind: "check", selector: cssPath(element), checked: element.checked })
|
||||
} else if (element instanceof HTMLSelectElement) {
|
||||
send({ kind: "select", selector: cssPath(element), value: element.value })
|
||||
} else {
|
||||
onInput(event)
|
||||
}
|
||||
}
|
||||
document.addEventListener("click", onClick, true)
|
||||
document.addEventListener("input", onInput, true)
|
||||
document.addEventListener("change", onChange, true)
|
||||
const cleanup = () => {
|
||||
document.removeEventListener("click", onClick, true)
|
||||
document.removeEventListener("input", onInput, true)
|
||||
document.removeEventListener("change", onChange, true)
|
||||
if (browserWindow.__opencodeBrowserDemonstrationListeners__?.id === options.recorderId) {
|
||||
delete browserWindow.__opencodeBrowserDemonstrationListeners__
|
||||
}
|
||||
}
|
||||
browserWindow.__opencodeBrowserDemonstrationListeners__ = { id: options.recorderId, cleanup }
|
||||
}
|
||||
|
||||
function removeDemonstrationListeners(options: { readonly recorderId: string }): void {
|
||||
const browserWindow = window as Window & {
|
||||
__opencodeBrowserDemonstrationListeners__?: { readonly id: string; readonly cleanup: () => void }
|
||||
}
|
||||
if (browserWindow.__opencodeBrowserDemonstrationListeners__?.id === options.recorderId) {
|
||||
browserWindow.__opencodeBrowserDemonstrationListeners__.cleanup()
|
||||
}
|
||||
}
|
||||
615
packages/browser-extension/cli/src/doctor.ts
Normal file
615
packages/browser-extension/cli/src/doctor.ts
Normal file
|
|
@ -0,0 +1,615 @@
|
|||
import { Effect, FileSystem, Path, Schema } from "effect"
|
||||
import * as RelayClient from "./relay-client.ts"
|
||||
import { relayBuildProblem } from "./relay-lifecycle.ts"
|
||||
import type { ExtensionStatus, RelayVersion, SessionSummary, TargetSummary } from "./relay-schema.ts"
|
||||
import { extensionProtocolVersion } from "./protocol.ts"
|
||||
import * as SessionStore from "./session-store.ts"
|
||||
import { opencodeBrowserBuildId, opencodeBrowserVersion } from "./version.ts"
|
||||
|
||||
/**
|
||||
* Read-only local install and runtime diagnostics. Pure report construction
|
||||
* over RelayClient/SessionStore/FileSystem probes; never fails, degrades to
|
||||
* warn/fail checks instead.
|
||||
*/
|
||||
|
||||
export type ProbeResult<T> =
|
||||
| { readonly ok: true; readonly value: T }
|
||||
| { readonly ok: false; readonly error: string }
|
||||
|
||||
const PackageMetadata = Schema.Struct({
|
||||
name: Schema.String,
|
||||
version: Schema.String,
|
||||
bin: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)),
|
||||
})
|
||||
|
||||
const ManifestVersion = Schema.Struct({
|
||||
version: Schema.String,
|
||||
})
|
||||
const bundledExtensionManifestPath = "extension/manifest.json"
|
||||
|
||||
type PackageInfo = {
|
||||
readonly name: string
|
||||
readonly version: string
|
||||
readonly bin: {
|
||||
readonly opencodeBrowser: string | null
|
||||
readonly opencodeBrowserMcp: string | null
|
||||
}
|
||||
}
|
||||
|
||||
type DoctorArtifact = {
|
||||
readonly path: string
|
||||
readonly exists: boolean
|
||||
readonly version?: string
|
||||
}
|
||||
|
||||
export type DoctorCheckStatus = "ok" | "warn" | "fail"
|
||||
|
||||
export type DoctorCheck = {
|
||||
readonly id: string
|
||||
readonly label: string
|
||||
readonly status: DoctorCheckStatus
|
||||
readonly message: string
|
||||
}
|
||||
|
||||
export type DoctorReport = {
|
||||
readonly status: DoctorCheckStatus
|
||||
readonly endpoint: string
|
||||
readonly cli: {
|
||||
readonly version: string
|
||||
readonly buildId: string
|
||||
}
|
||||
readonly package: {
|
||||
readonly path: string
|
||||
readonly name: string | null
|
||||
readonly version: string | null
|
||||
readonly bin: {
|
||||
readonly opencodeBrowser: boolean
|
||||
readonly opencodeBrowserMcp: boolean
|
||||
}
|
||||
readonly error: string | null
|
||||
}
|
||||
readonly relay: {
|
||||
readonly reachable: boolean
|
||||
readonly version: string | null
|
||||
readonly buildId: string | null
|
||||
readonly buildMatches: boolean | null
|
||||
readonly error: string | null
|
||||
}
|
||||
readonly extension: {
|
||||
readonly connected: boolean | null
|
||||
readonly version: string | null
|
||||
readonly expectedVersion: string | null
|
||||
readonly versionMatches: boolean | null
|
||||
readonly protocolVersion: number | null
|
||||
readonly expectedProtocolVersion: number
|
||||
readonly protocolCompatible: boolean | null
|
||||
readonly protocolLegacy: boolean | null
|
||||
readonly rejectedConnections: number | null
|
||||
readonly error: string | null
|
||||
}
|
||||
readonly targets: {
|
||||
readonly active: number | null
|
||||
readonly child: number | null
|
||||
readonly relayOwned: readonly TargetSummary[]
|
||||
readonly unhealthy: readonly TargetSummary[]
|
||||
readonly all: readonly TargetSummary[]
|
||||
readonly error: string | null
|
||||
}
|
||||
readonly sessions: {
|
||||
readonly current: string | null
|
||||
readonly staleCurrent: boolean
|
||||
readonly possibleLeaked: readonly SessionSummary[]
|
||||
readonly all: readonly SessionSummary[]
|
||||
readonly error: string | null
|
||||
}
|
||||
readonly artifacts: readonly DoctorArtifact[]
|
||||
readonly checks: readonly DoctorCheck[]
|
||||
readonly recommendations: readonly string[]
|
||||
}
|
||||
|
||||
const probe = <A, E>(effect: Effect.Effect<A, E>): Effect.Effect<ProbeResult<A>> =>
|
||||
effect.pipe(
|
||||
Effect.match({
|
||||
onFailure: (error) => ({ ok: false, error: errorMessage(error) } as const),
|
||||
onSuccess: (value) => ({ ok: true, value } as const),
|
||||
}),
|
||||
)
|
||||
|
||||
const errorMessage = (error: unknown): string => {
|
||||
if (error instanceof Error) {
|
||||
return error.message
|
||||
}
|
||||
return String(error)
|
||||
}
|
||||
|
||||
export const createDoctorReport = Effect.fn("Doctor.createReport")(function* (options: {
|
||||
readonly packageRoot: string
|
||||
}) {
|
||||
const relay = yield* RelayClient.Service
|
||||
const store = yield* SessionStore.Service
|
||||
const fs = yield* FileSystem.FileSystem
|
||||
const path = yield* Path.Path
|
||||
|
||||
const readJsonFile = <A>(relativePath: string, schema: Schema.ConstraintDecoder<A>): Effect.Effect<A, Error> =>
|
||||
fs.readFileString(path.join(options.packageRoot, relativePath)).pipe(
|
||||
Effect.mapError((cause) => new Error(`read ${relativePath}: ${cause.reason.message}`)),
|
||||
Effect.flatMap((text) =>
|
||||
Effect.try({
|
||||
try: () => JSON.parse(text) as unknown,
|
||||
catch: () => new Error(`parse ${relativePath}: invalid JSON`),
|
||||
})
|
||||
),
|
||||
Effect.flatMap((value) =>
|
||||
Schema.decodeUnknownEffect(schema)(value).pipe(
|
||||
Effect.mapError((cause) => new Error(`decode ${relativePath}: ${cause.message}`)),
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
const readPackageInfo: Effect.Effect<PackageInfo, Error> = readJsonFile("package.json", PackageMetadata).pipe(
|
||||
Effect.map((metadata) => ({
|
||||
name: metadata.name,
|
||||
version: metadata.version,
|
||||
bin: {
|
||||
opencodeBrowser: metadata.bin?.["opencode-browser"] ?? null,
|
||||
opencodeBrowserMcp: metadata.bin?.["opencode-browser-mcp"] ?? null,
|
||||
},
|
||||
})),
|
||||
)
|
||||
|
||||
const readManifestVersion = (relativePath: string): Effect.Effect<string, Error> =>
|
||||
readJsonFile(relativePath, ManifestVersion).pipe(Effect.map((manifest) => manifest.version))
|
||||
|
||||
const fileExists = (relativePath: string): Effect.Effect<boolean> =>
|
||||
fs.exists(path.join(options.packageRoot, relativePath)).pipe(
|
||||
Effect.orElseSucceed(() => false),
|
||||
)
|
||||
|
||||
const [packageResult, bundledManifestVersion, distCliExists, distMcpExists, extensionDistManifestExists, currentResult] = yield* Effect.all([
|
||||
probe(readPackageInfo),
|
||||
probe(readManifestVersion(bundledExtensionManifestPath)),
|
||||
fileExists("dist/cli.mjs"),
|
||||
fileExists("dist/cli.mjs"),
|
||||
fileExists(bundledExtensionManifestPath),
|
||||
probe(store.read),
|
||||
])
|
||||
const relayResult = yield* probe(relay.version)
|
||||
const relayBuildMatches = relayResult.ok
|
||||
? relayResult.value.buildId
|
||||
? relayBuildProblem(relayResult.value, opencodeBrowserBuildId) === undefined
|
||||
: null
|
||||
: null
|
||||
const [extensionResult, targetsResult, sessionsResult] = relayResult.ok
|
||||
? yield* Effect.all([
|
||||
probe(relay.extensionStatus),
|
||||
probe(relay.targets),
|
||||
probe(relay.sessions),
|
||||
])
|
||||
: [
|
||||
{ ok: false, error: relayResult.error } satisfies ProbeResult<ExtensionStatus>,
|
||||
{ ok: false, error: relayResult.error } satisfies ProbeResult<readonly TargetSummary[]>,
|
||||
{ ok: false, error: relayResult.error } satisfies ProbeResult<readonly SessionSummary[]>,
|
||||
]
|
||||
const expectedVersion = bundledManifestVersion.ok ? bundledManifestVersion.value : null
|
||||
const extensionVersion = extensionResult.ok ? extensionResult.value.version : null
|
||||
const extensionVersionMatches = extensionResult.ok && extensionVersion && expectedVersion ? extensionVersion === expectedVersion : null
|
||||
const current = currentResult.ok ? currentResult.value ?? null : null
|
||||
const targets = targetsResult.ok ? targetsResult.value : []
|
||||
const sessions = sessionsResult.ok ? sessionsResult.value : []
|
||||
const staleCurrent = Boolean(current && sessionsResult.ok && !sessions.some((session) => {
|
||||
return session.id === current
|
||||
}))
|
||||
const relayOwnedTargets = targets.filter((target) => {
|
||||
return target.owner === "relay"
|
||||
})
|
||||
const unhealthyTargets = targets.filter((target) => {
|
||||
return target.crashed === true || target.url.startsWith("chrome-error://")
|
||||
})
|
||||
const possibleLeakedSessions = sessions.filter((session) => {
|
||||
return session.connected && session.id !== current
|
||||
})
|
||||
const artifacts: readonly DoctorArtifact[] = [
|
||||
{ path: "dist/cli.mjs", exists: distCliExists && distMcpExists },
|
||||
{
|
||||
path: bundledExtensionManifestPath,
|
||||
exists: extensionDistManifestExists,
|
||||
...(bundledManifestVersion.ok ? { version: bundledManifestVersion.value } : {}),
|
||||
},
|
||||
]
|
||||
const checks = buildDoctorChecks({
|
||||
packageResult,
|
||||
relayResult,
|
||||
extensionResult,
|
||||
bundledManifestVersion,
|
||||
artifacts,
|
||||
currentResult,
|
||||
staleCurrent,
|
||||
relayOwnedTargets,
|
||||
unhealthyTargets,
|
||||
possibleLeakedSessions,
|
||||
targetsResult,
|
||||
sessionsResult,
|
||||
})
|
||||
const report: DoctorReport = {
|
||||
status: summarizeCheckStatus(checks),
|
||||
endpoint: relay.endpoint,
|
||||
cli: { version: opencodeBrowserVersion, buildId: opencodeBrowserBuildId },
|
||||
package: {
|
||||
path: path.join(options.packageRoot, "package.json"),
|
||||
name: packageResult.ok ? packageResult.value.name : null,
|
||||
version: packageResult.ok ? packageResult.value.version : null,
|
||||
bin: {
|
||||
opencodeBrowser: packageResult.ok ? Boolean(packageResult.value.bin.opencodeBrowser) : false,
|
||||
opencodeBrowserMcp: packageResult.ok ? Boolean(packageResult.value.bin.opencodeBrowserMcp) : false,
|
||||
},
|
||||
error: packageResult.ok ? null : packageResult.error,
|
||||
},
|
||||
relay: {
|
||||
reachable: relayResult.ok,
|
||||
version: relayResult.ok ? relayResult.value.version : null,
|
||||
buildId: relayResult.ok ? relayResult.value.buildId ?? null : null,
|
||||
buildMatches: relayBuildMatches,
|
||||
error: relayResult.ok ? null : relayResult.error,
|
||||
},
|
||||
extension: {
|
||||
connected: extensionResult.ok ? extensionResult.value.connected : null,
|
||||
version: extensionVersion,
|
||||
expectedVersion,
|
||||
versionMatches: extensionVersionMatches,
|
||||
protocolVersion: extensionResult.ok ? extensionResult.value.protocolVersion ?? null : null,
|
||||
expectedProtocolVersion: extensionProtocolVersion,
|
||||
protocolCompatible: extensionResult.ok ? extensionResult.value.protocolCompatible ?? null : null,
|
||||
protocolLegacy: extensionResult.ok ? extensionResult.value.protocolLegacy ?? null : null,
|
||||
rejectedConnections: extensionResult.ok ? extensionResult.value.rejectedConnections ?? null : null,
|
||||
error: extensionResult.ok ? null : extensionResult.error,
|
||||
},
|
||||
targets: {
|
||||
active: extensionResult.ok ? extensionResult.value.activeTargets : null,
|
||||
child: extensionResult.ok ? extensionResult.value.childTargets ?? null : null,
|
||||
relayOwned: relayOwnedTargets,
|
||||
unhealthy: unhealthyTargets,
|
||||
all: targets,
|
||||
error: targetsResult.ok ? null : targetsResult.error,
|
||||
},
|
||||
sessions: {
|
||||
current,
|
||||
staleCurrent,
|
||||
possibleLeaked: possibleLeakedSessions,
|
||||
all: sessions,
|
||||
error: sessionsResult.ok ? currentResult.ok ? null : currentResult.error : sessionsResult.error,
|
||||
},
|
||||
artifacts,
|
||||
checks,
|
||||
recommendations: buildDoctorRecommendations({
|
||||
relayResult,
|
||||
extensionResult,
|
||||
artifacts,
|
||||
staleCurrent,
|
||||
current,
|
||||
unhealthyTargets,
|
||||
}),
|
||||
}
|
||||
return report
|
||||
})
|
||||
|
||||
function buildDoctorChecks(options: {
|
||||
readonly packageResult: ProbeResult<PackageInfo>
|
||||
readonly relayResult: ProbeResult<RelayVersion>
|
||||
readonly extensionResult: ProbeResult<ExtensionStatus>
|
||||
readonly bundledManifestVersion: ProbeResult<string>
|
||||
readonly artifacts: readonly DoctorArtifact[]
|
||||
readonly currentResult: ProbeResult<string | undefined>
|
||||
readonly staleCurrent: boolean
|
||||
readonly relayOwnedTargets: readonly TargetSummary[]
|
||||
readonly unhealthyTargets: readonly TargetSummary[]
|
||||
readonly possibleLeakedSessions: readonly SessionSummary[]
|
||||
readonly targetsResult: ProbeResult<readonly TargetSummary[]>
|
||||
readonly sessionsResult: ProbeResult<readonly SessionSummary[]>
|
||||
}): readonly DoctorCheck[] {
|
||||
const packageBinChecks: readonly DoctorCheck[] = options.packageResult.ok
|
||||
? [
|
||||
{
|
||||
id: "bin-opencode-browser",
|
||||
label: "opencode-browser bin",
|
||||
status: options.packageResult.value.bin.opencodeBrowser ? "ok" : "fail",
|
||||
message: options.packageResult.value.bin.opencodeBrowser ?? "missing from package.json bin",
|
||||
},
|
||||
{
|
||||
id: "bin-opencode-browser-mcp",
|
||||
label: "opencode-browser-mcp bin",
|
||||
status: options.packageResult.value.bin.opencodeBrowserMcp ? "ok" : "warn",
|
||||
message: options.packageResult.value.bin.opencodeBrowserMcp ?? "missing from package.json bin",
|
||||
},
|
||||
]
|
||||
: []
|
||||
const artifactChecks = options.artifacts.map((artifact): DoctorCheck => {
|
||||
return {
|
||||
id: `artifact-${artifact.path}`,
|
||||
label: artifact.path,
|
||||
status: artifact.exists ? "ok" : "warn",
|
||||
message: artifact.exists ? artifact.version ? `exists (${artifact.version})` : "exists" : "missing; prepare a fresh runtime",
|
||||
}
|
||||
})
|
||||
return [
|
||||
{
|
||||
id: "package-metadata",
|
||||
label: "package metadata",
|
||||
status: options.packageResult.ok ? "ok" : "fail",
|
||||
message: options.packageResult.ok ? `${options.packageResult.value.name} ${options.packageResult.value.version}` : options.packageResult.error,
|
||||
},
|
||||
...packageBinChecks,
|
||||
{
|
||||
id: "relay-http",
|
||||
label: "relay HTTP endpoint",
|
||||
status: options.relayResult.ok ? "ok" : "fail",
|
||||
message: options.relayResult.ok ? `reachable (${options.relayResult.value.version})` : options.relayResult.error,
|
||||
},
|
||||
relayBuildCheck({ relayResult: options.relayResult, cliBuildId: opencodeBrowserBuildId }),
|
||||
{
|
||||
id: "extension-connected",
|
||||
label: "extension connection",
|
||||
status: options.extensionResult.ok && options.extensionResult.value.connected ? "ok" : "fail",
|
||||
message: options.extensionResult.ok ? options.extensionResult.value.connected ? `connected${options.extensionResult.value.version ? ` (${options.extensionResult.value.version})` : ""}` : "disconnected" : options.extensionResult.error,
|
||||
},
|
||||
extensionVersionCheck({
|
||||
extensionResult: options.extensionResult,
|
||||
bundledManifestVersion: options.bundledManifestVersion,
|
||||
}),
|
||||
extensionProtocolCheck(options.extensionResult),
|
||||
...(options.extensionResult.ok && options.extensionResult.value.rejectedConnections !== undefined ? [{
|
||||
id: "extension-connection-conflicts",
|
||||
label: "competing browser connections",
|
||||
status: options.extensionResult.value.rejectedConnections > 0 ? "warn" as const : "ok" as const,
|
||||
message: options.extensionResult.value.rejectedConnections > 0
|
||||
? `${options.extensionResult.value.rejectedConnections} connection attempt(s) rejected; the active browser connection was preserved`
|
||||
: "none observed on this connection",
|
||||
}] : []),
|
||||
{
|
||||
id: "targets-readable",
|
||||
label: "targets readable",
|
||||
status: options.targetsResult.ok ? "ok" : "fail",
|
||||
message: options.targetsResult.ok ? `${options.targetsResult.value.length} active root target(s)` : options.targetsResult.error,
|
||||
},
|
||||
unhealthyTargetsCheck({
|
||||
targetsResult: options.targetsResult,
|
||||
unhealthyTargets: options.unhealthyTargets,
|
||||
}),
|
||||
{
|
||||
id: "sessions-readable",
|
||||
label: "sessions readable",
|
||||
status: options.sessionsResult.ok ? "ok" : "fail",
|
||||
message: options.sessionsResult.ok ? `${options.sessionsResult.value.length} session(s)` : options.sessionsResult.error,
|
||||
},
|
||||
{
|
||||
id: "current-session-file",
|
||||
label: "current session file",
|
||||
status: options.currentResult.ok ? "ok" : "warn",
|
||||
message: options.currentResult.ok ? options.currentResult.value ?? "none" : options.currentResult.error,
|
||||
},
|
||||
{
|
||||
id: "current-session-stale",
|
||||
label: "current session membership",
|
||||
status: options.staleCurrent ? "warn" : "ok",
|
||||
message: options.staleCurrent ? "current session is not present in relay sessions" : "current session is valid or unset",
|
||||
},
|
||||
{
|
||||
id: "relay-owned-targets",
|
||||
label: "relay-owned active targets",
|
||||
status: "ok",
|
||||
message: options.relayOwnedTargets.length ? `${options.relayOwnedTargets.length} persistent relay-owned target(s)` : "none",
|
||||
},
|
||||
{
|
||||
id: "possible-leaked-sessions",
|
||||
label: "connected non-current sessions",
|
||||
status: "ok",
|
||||
message: options.possibleLeakedSessions.length ? `${options.possibleLeakedSessions.length} connected non-current session(s)` : "none",
|
||||
},
|
||||
...artifactChecks,
|
||||
]
|
||||
}
|
||||
|
||||
const makeDoctorCheck = (id: string, label: string) => (status: DoctorCheckStatus, message: string): DoctorCheck => ({
|
||||
id,
|
||||
label,
|
||||
status,
|
||||
message,
|
||||
})
|
||||
|
||||
export function unhealthyTargetsCheck(options: {
|
||||
readonly targetsResult: ProbeResult<readonly TargetSummary[]>
|
||||
readonly unhealthyTargets: readonly TargetSummary[]
|
||||
}): DoctorCheck {
|
||||
const check = makeDoctorCheck("unhealthy-targets", "crashed or browser-error targets")
|
||||
if (!options.targetsResult.ok) {
|
||||
return check("warn", `target health unknown: ${options.targetsResult.error}`)
|
||||
}
|
||||
return check(
|
||||
options.unhealthyTargets.length ? "warn" : "ok",
|
||||
options.unhealthyTargets.length ? `${options.unhealthyTargets.length} unhealthy target(s)` : "none",
|
||||
)
|
||||
}
|
||||
|
||||
export function relayBuildCheck(options: {
|
||||
readonly relayResult: ProbeResult<RelayVersion>
|
||||
readonly cliBuildId: string
|
||||
}): DoctorCheck {
|
||||
const check = makeDoctorCheck("relay-build", "relay build")
|
||||
if (!options.relayResult.ok) {
|
||||
return check("warn", "relay unreachable; cannot compare builds")
|
||||
}
|
||||
const relayBuildId = options.relayResult.value.buildId
|
||||
if (!relayBuildId) {
|
||||
return check("warn", "running relay does not report a build id")
|
||||
}
|
||||
const matches = relayBuildId === options.cliBuildId
|
||||
return check(
|
||||
matches ? "ok" : "warn",
|
||||
matches
|
||||
? `matches CLI build (${options.cliBuildId})`
|
||||
: `runtime ${relayBuildId} does not match CLI ${options.cliBuildId}`,
|
||||
)
|
||||
}
|
||||
|
||||
function extensionVersionCheck(options: {
|
||||
readonly extensionResult: ProbeResult<ExtensionStatus>
|
||||
readonly bundledManifestVersion: ProbeResult<string>
|
||||
}): DoctorCheck {
|
||||
const check = makeDoctorCheck("extension-version", "extension version")
|
||||
if (!options.extensionResult.ok) {
|
||||
return check("warn", options.extensionResult.error)
|
||||
}
|
||||
if (!options.extensionResult.value.connected) {
|
||||
return check("warn", "extension disconnected; cannot compare runtime version")
|
||||
}
|
||||
if (!options.bundledManifestVersion.ok) {
|
||||
return check("warn", `could not read ${bundledExtensionManifestPath}: ${options.bundledManifestVersion.error}`)
|
||||
}
|
||||
const runtimeVersion = options.extensionResult.value.version
|
||||
if (!runtimeVersion) {
|
||||
return check("warn", "extension did not report a version")
|
||||
}
|
||||
return check(
|
||||
"ok",
|
||||
runtimeVersion === options.bundledManifestVersion.value
|
||||
? `matches bundled extension (${options.bundledManifestVersion.value})`
|
||||
: `runtime ${runtimeVersion} differs from bundled ${options.bundledManifestVersion.value}; protocol compatibility determines support`,
|
||||
)
|
||||
}
|
||||
|
||||
export function extensionProtocolCheck(extensionResult: ProbeResult<ExtensionStatus>): DoctorCheck {
|
||||
const check = makeDoctorCheck("extension-protocol", "extension protocol")
|
||||
if (!extensionResult.ok) {
|
||||
return check("warn", extensionResult.error)
|
||||
}
|
||||
const protocolVersion = extensionResult.value.protocolVersion
|
||||
const protocolCompatible = extensionResult.value.protocolCompatible
|
||||
if (protocolCompatible === false) {
|
||||
return check("fail", `runtime ${protocolVersion ?? "unknown"} is incompatible with relay ${extensionProtocolVersion}`)
|
||||
}
|
||||
if (extensionResult.value.protocolLegacy === true) {
|
||||
return check("warn", `legacy extension does not report its protocol; relay infers ${protocolVersion ?? "unknown"}`)
|
||||
}
|
||||
if (protocolVersion === undefined || protocolVersion === null) {
|
||||
return check("warn", "extension protocol is unknown")
|
||||
}
|
||||
return check("ok", `runtime ${protocolVersion} is compatible with relay ${extensionProtocolVersion}`)
|
||||
}
|
||||
|
||||
function buildDoctorRecommendations(options: {
|
||||
readonly relayResult: ProbeResult<RelayVersion>
|
||||
readonly extensionResult: ProbeResult<ExtensionStatus>
|
||||
readonly artifacts: readonly DoctorArtifact[]
|
||||
readonly staleCurrent: boolean
|
||||
readonly current: string | null
|
||||
readonly unhealthyTargets: readonly TargetSummary[]
|
||||
}): readonly string[] {
|
||||
const relayRecommendations = options.relayResult.ok ? [] : [
|
||||
"Run a relay-backed command to start the detached relay automatically; use `opencode-browser serve` only for foreground debugging.",
|
||||
]
|
||||
const relayBuildRecommendations = options.relayResult.ok
|
||||
? [relayBuildProblem(options.relayResult.value)].filter((message) => message !== undefined)
|
||||
: []
|
||||
const extensionRecommendations = options.relayResult.ok && options.extensionResult.ok && !options.extensionResult.value.connected
|
||||
? options.extensionResult.value.protocolCompatible === false
|
||||
? ["Update the OpenCode Browser extension or npm package so their extension protocols are compatible."]
|
||||
: ["Run `npx opencode-browser-cli install`, and install OpenCode Browser from the Chrome Web Store (or load the unpacked extension it copies)."]
|
||||
: []
|
||||
const artifactRecommendations = options.artifacts.some((artifact) => {
|
||||
return !artifact.exists
|
||||
}) ? ["Prepare and select a fresh validated runtime with `pnpm runtime:prepare` / `pnpm runtime:select`; do not rebuild the active installation."] : []
|
||||
const connectionConflictRecommendations = options.extensionResult.ok && (options.extensionResult.value.rejectedConnections ?? 0) > 0
|
||||
? ["Keep OpenCode Browser enabled in one browser/profile. To switch, disable it in the current browser before connecting the other; new sessions do not change the active browser."]
|
||||
: []
|
||||
const staleSessionRecommendations = options.staleCurrent && options.current ? [
|
||||
`Current session ${options.current} is stale; run \`opencode-browser session new\` or \`opencode-browser session use <id>\` after the relay is running.`,
|
||||
] : []
|
||||
const unhealthyTargetRecommendations = options.unhealthyTargets.length ? [
|
||||
"A target is crashed or showing a browser error page. Run the owning session once to trigger relay-owned recovery, or reset/re-adopt a user-owned tab.",
|
||||
] : []
|
||||
return [
|
||||
...relayRecommendations,
|
||||
...relayBuildRecommendations,
|
||||
...extensionRecommendations,
|
||||
...connectionConflictRecommendations,
|
||||
...artifactRecommendations,
|
||||
...staleSessionRecommendations,
|
||||
...unhealthyTargetRecommendations,
|
||||
]
|
||||
}
|
||||
|
||||
function summarizeCheckStatus(checks: readonly DoctorCheck[]): DoctorCheckStatus {
|
||||
if (checks.some((check) => {
|
||||
return check.status === "fail"
|
||||
})) {
|
||||
return "fail"
|
||||
}
|
||||
if (checks.some((check) => {
|
||||
return check.status === "warn"
|
||||
})) {
|
||||
return "warn"
|
||||
}
|
||||
return "ok"
|
||||
}
|
||||
|
||||
export function formatDoctorReport(report: DoctorReport): string {
|
||||
const lines: string[] = [
|
||||
"OpenCode Browser doctor",
|
||||
`Status: ${report.status}`,
|
||||
`Endpoint: ${report.endpoint}`,
|
||||
`CLI: ${report.cli.version} (${report.cli.buildId})`,
|
||||
`Package: ${report.package.name && report.package.version ? `${report.package.name} ${report.package.version}` : report.package.error ?? "unknown"}`,
|
||||
`Relay: ${report.relay.reachable ? `reachable (${report.relay.version ?? "unknown"}, ${report.relay.buildId ?? "unknown build"})` : `unreachable (${report.relay.error ?? "unknown error"})`}`,
|
||||
`Extension: ${formatExtensionSummary(report)}`,
|
||||
`Targets: active=${formatNullableNumber(report.targets.active)} child=${formatNullableNumber(report.targets.child)} relay-owned=${report.targets.relayOwned.length} unhealthy=${report.targets.unhealthy.length}`,
|
||||
`Sessions: current=${report.sessions.current ?? "none"} total=${report.sessions.all.length} connected=${report.sessions.all.filter((session) => {
|
||||
return session.connected
|
||||
}).length}`,
|
||||
"",
|
||||
"Checks:",
|
||||
...report.checks.map((check) => {
|
||||
return `[${check.status}] ${check.label}: ${check.message}`
|
||||
}),
|
||||
]
|
||||
const targetLines = report.targets.relayOwned.map((target) => {
|
||||
return `- ${formatTargetSummary(target)}`
|
||||
})
|
||||
const unhealthyTargetLines = report.targets.unhealthy.map((target) => {
|
||||
return `- ${formatTargetSummary(target)}`
|
||||
})
|
||||
const sessionLines = report.sessions.possibleLeaked.map((session) => {
|
||||
return `- ${session.id} ${session.pageUrl ?? "no page yet"}`
|
||||
})
|
||||
const details = [
|
||||
...(targetLines.length ? ["", "Relay-owned targets:", ...targetLines] : []),
|
||||
...(unhealthyTargetLines.length ? ["", "Unhealthy targets:", ...unhealthyTargetLines] : []),
|
||||
...(sessionLines.length ? ["", "Connected non-current sessions:", ...sessionLines] : []),
|
||||
...(report.recommendations.length ? ["", "Next steps:", ...report.recommendations.map((item) => {
|
||||
return `- ${item}`
|
||||
})] : []),
|
||||
]
|
||||
return [...lines, ...details].join("\n")
|
||||
}
|
||||
|
||||
function formatExtensionSummary(report: DoctorReport): string {
|
||||
if (report.extension.connected === null) {
|
||||
return `unknown (${report.extension.error ?? "relay unreachable"})`
|
||||
}
|
||||
const version = report.extension.version ? ` (${report.extension.version})` : ""
|
||||
const protocol = report.extension.protocolVersion === null
|
||||
? ""
|
||||
: `, protocol ${report.extension.protocolVersion} ${report.extension.protocolCompatible === false ? "incompatible" : "compatible"}`
|
||||
const match = report.extension.versionMatches === null ? "" : report.extension.versionMatches ? ", matches bundled version" : ", differs from bundled version"
|
||||
return `${report.extension.connected ? "connected" : "disconnected"}${version}${protocol}${match}`
|
||||
}
|
||||
|
||||
function formatNullableNumber(value: number | null): string {
|
||||
return value === null ? "unknown" : String(value)
|
||||
}
|
||||
|
||||
export function formatTargetSummary(target: TargetSummary, options: { readonly includeSession?: boolean } = {}): string {
|
||||
const tab = target.tabId === undefined ? "" : ` tab=${target.tabId}`
|
||||
const session = options.includeSession && target.opencodeBrowserSessionId ? ` session=${target.opencodeBrowserSessionId}` : ""
|
||||
const owner = target.owner ? ` owner=${target.owner}` : ""
|
||||
const health = `${target.crashed ? " crashed=true" : ""}${target.protectedUi ? " protected-ui=true" : ""}`
|
||||
return `${target.type} ${target.id}${tab}${session}${owner}${health} ${target.url || "about:blank"}`
|
||||
}
|
||||
2176
packages/browser-extension/cli/src/execute.ts
Normal file
2176
packages/browser-extension/cli/src/execute.ts
Normal file
File diff suppressed because it is too large
Load diff
218
packages/browser-extension/cli/src/extension-rpc.ts
Normal file
218
packages/browser-extension/cli/src/extension-rpc.ts
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
import { Effect, Predicate } from "effect"
|
||||
import { WebSocket } from "ws"
|
||||
import {
|
||||
extensionProtocolCompatibility,
|
||||
type ExtensionCommand,
|
||||
type ExtensionProtocolCompatibility,
|
||||
type ExtensionResponse,
|
||||
type JsonObject,
|
||||
type JsonValue,
|
||||
} from "./protocol.ts"
|
||||
import type { PendingExtensionRequest } from "./relay-types.ts"
|
||||
|
||||
export type ExtensionRpcTimeouts = {
|
||||
readonly commandTimeoutMs?: number
|
||||
readonly debuggerCommandTimeoutMs?: number
|
||||
readonly livenessProbeTimeoutMs?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Request/response correlator for the single extension websocket.
|
||||
*
|
||||
* A timed-out command fails only that command. Connection teardown is reserved
|
||||
* for a failed websocket-level liveness probe, so one hung debugger command
|
||||
* (for example a dialog-blocked tab) cannot destroy every attached tab's relay
|
||||
* state.
|
||||
*/
|
||||
export class ExtensionRpc {
|
||||
private socket: WebSocket | undefined
|
||||
private ready = false
|
||||
private nextRequestId = 1
|
||||
private readonly pendingRequests = new Map<number, PendingExtensionRequest>()
|
||||
private livenessProbe: { readonly timeout: NodeJS.Timeout; readonly socket: WebSocket; readonly onPong: () => void } | undefined
|
||||
version: string | undefined
|
||||
protocolVersion: number | null | undefined
|
||||
protocolCompatible: boolean | undefined
|
||||
protocolLegacy: boolean | undefined
|
||||
|
||||
constructor(private readonly timeouts: ExtensionRpcTimeouts = {}) {}
|
||||
|
||||
get connected(): boolean {
|
||||
return this.socket?.readyState === WebSocket.OPEN && this.ready && this.protocolCompatible !== false
|
||||
}
|
||||
|
||||
get acceptsEvents(): boolean {
|
||||
return this.socket?.readyState === WebSocket.OPEN && this.protocolCompatible === true
|
||||
}
|
||||
|
||||
isCurrent(socket: WebSocket): boolean {
|
||||
return this.socket === socket
|
||||
}
|
||||
|
||||
replaceSocket(socket: WebSocket): void {
|
||||
this.rejectPending(new Error("Extension replaced"))
|
||||
this.socket?.close(4001, "Extension replaced")
|
||||
this.resetConnectionState(socket)
|
||||
}
|
||||
|
||||
markHandshake(version: string | undefined, reportedProtocolVersion: JsonValue | undefined): ExtensionProtocolCompatibility {
|
||||
const protocol = extensionProtocolCompatibility(reportedProtocolVersion)
|
||||
this.version = version
|
||||
this.protocolVersion = protocol.version
|
||||
this.protocolCompatible = protocol.compatible
|
||||
this.protocolLegacy = protocol.legacy
|
||||
return protocol
|
||||
}
|
||||
|
||||
markReady(): void {
|
||||
if (this.protocolCompatible === true) {
|
||||
this.ready = true
|
||||
}
|
||||
}
|
||||
|
||||
disconnectIfCurrent(socket: WebSocket): boolean {
|
||||
if (this.socket !== socket) {
|
||||
return false
|
||||
}
|
||||
this.resetConnectionState(undefined)
|
||||
this.rejectPending(new Error("Extension disconnected"))
|
||||
return true
|
||||
}
|
||||
|
||||
private resetConnectionState(socket: WebSocket | undefined): void {
|
||||
this.cancelLivenessProbe()
|
||||
this.socket = socket
|
||||
this.ready = false
|
||||
this.version = undefined
|
||||
this.protocolVersion = undefined
|
||||
this.protocolCompatible = undefined
|
||||
this.protocolLegacy = undefined
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.cancelLivenessProbe()
|
||||
this.socket?.close()
|
||||
}
|
||||
|
||||
rejectPending(error: Error): void {
|
||||
for (const pending of this.pendingRequests.values()) {
|
||||
pending.reject(error)
|
||||
}
|
||||
}
|
||||
|
||||
rejectDebuggerCommandsForTab(tabId: number, error: Error): void {
|
||||
for (const pending of this.pendingRequests.values()) {
|
||||
if (pending.debuggerTabId !== tabId) {
|
||||
continue
|
||||
}
|
||||
pending.reject(error)
|
||||
}
|
||||
}
|
||||
|
||||
handleResponse(response: ExtensionResponse): boolean {
|
||||
const pending = this.pendingRequests.get(response.id)
|
||||
if (!pending) {
|
||||
return false
|
||||
}
|
||||
if (response.error) {
|
||||
pending.reject(new Error(response.error))
|
||||
return true
|
||||
}
|
||||
pending.resolve(response.result ?? {})
|
||||
return true
|
||||
}
|
||||
|
||||
send(command: Omit<ExtensionCommand, "id">): Effect.Effect<JsonObject, Error> {
|
||||
return Effect.callback<JsonObject, Error>((resume) => {
|
||||
const socket = this.socket
|
||||
if (this.protocolCompatible === false) {
|
||||
resume(Effect.fail(new Error(`OpenCode Browser extension protocol ${this.protocolVersion ?? "unknown"} is incompatible`)))
|
||||
return Effect.void
|
||||
}
|
||||
if (!socket || socket.readyState !== WebSocket.OPEN || !this.acceptsEvents) {
|
||||
resume(Effect.fail(new Error("OpenCode Browser extension is not connected")))
|
||||
return Effect.void
|
||||
}
|
||||
|
||||
const id = this.nextRequestId++
|
||||
const message: ExtensionCommand = { ...command, id }
|
||||
let completed = false
|
||||
const timeoutMs = command.method === "debugger.sendCommand"
|
||||
? this.timeouts.debuggerCommandTimeoutMs ?? 60_000
|
||||
: this.timeouts.commandTimeoutMs ?? 15_000
|
||||
const finish = (effect: Effect.Effect<JsonObject, Error>) => {
|
||||
if (completed) {
|
||||
return
|
||||
}
|
||||
completed = true
|
||||
clearTimeout(timeout)
|
||||
this.pendingRequests.delete(id)
|
||||
resume(effect)
|
||||
}
|
||||
const timeout = setTimeout(() => {
|
||||
// Fail only this command. Close the socket only if the websocket
|
||||
// itself is unresponsive to a protocol-level ping.
|
||||
this.probeLiveness(socket)
|
||||
finish(Effect.fail(new Error(`Extension command timed out after ${timeoutMs}ms: ${command.method}`)))
|
||||
}, timeoutMs)
|
||||
const debuggerTabId = command.method === "debugger.sendCommand" && Predicate.isNumber(command.params?.tabId)
|
||||
? command.params.tabId
|
||||
: undefined
|
||||
this.pendingRequests.set(id, {
|
||||
resolve: (value) => {
|
||||
finish(Effect.succeed(value))
|
||||
},
|
||||
reject: (error) => {
|
||||
finish(Effect.fail(error))
|
||||
},
|
||||
...(debuggerTabId === undefined ? {} : { debuggerTabId }),
|
||||
})
|
||||
try {
|
||||
socket.send(JSON.stringify(message), (error) => {
|
||||
if (error) {
|
||||
finish(Effect.fail(new Error(`send extension command: ${command.method}`, { cause: error })))
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
finish(Effect.fail(new Error(`send extension command: ${command.method}`, { cause: error })))
|
||||
}
|
||||
return Effect.sync(() => {
|
||||
completed = true
|
||||
clearTimeout(timeout)
|
||||
this.pendingRequests.delete(id)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
probeLiveness(socket = this.socket): void {
|
||||
if (!socket || this.socket !== socket || this.livenessProbe || socket.readyState !== WebSocket.OPEN) {
|
||||
return
|
||||
}
|
||||
const probeTimeoutMs = this.timeouts.livenessProbeTimeoutMs ?? 10_000
|
||||
const onPong = () => {
|
||||
if (this.livenessProbe?.socket !== socket) return
|
||||
this.cancelLivenessProbe()
|
||||
}
|
||||
const timeout = setTimeout(() => {
|
||||
if (this.livenessProbe?.socket !== socket) return
|
||||
this.cancelLivenessProbe()
|
||||
socket.close(4002, "Extension websocket did not answer liveness probe")
|
||||
}, probeTimeoutMs)
|
||||
this.livenessProbe = { timeout, socket, onPong }
|
||||
socket.on("pong", onPong)
|
||||
try {
|
||||
socket.ping()
|
||||
} catch {
|
||||
// If ping cannot be sent the socket is already failing; let the probe
|
||||
// timeout close it.
|
||||
}
|
||||
}
|
||||
|
||||
private cancelLivenessProbe(): void {
|
||||
if (this.livenessProbe) {
|
||||
clearTimeout(this.livenessProbe.timeout)
|
||||
this.livenessProbe.socket.off("pong", this.livenessProbe.onPong)
|
||||
this.livenessProbe = undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
293
packages/browser-extension/cli/src/flight-recorder.ts
Normal file
293
packages/browser-extension/cli/src/flight-recorder.ts
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
import fs from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import { performance } from "node:perf_hooks"
|
||||
import type { JsonObject } from "./protocol.ts"
|
||||
import { getNumber, getObject, getString } from "./relay-helpers.ts"
|
||||
import type { FlightRecorderSaveResponse, FlightRecorderStatusResponse } from "./relay-schema.ts"
|
||||
import {
|
||||
cdpArtifactType,
|
||||
findByRecordingTarget,
|
||||
prepareCdpScreencastViewport,
|
||||
startFfmpegVideoEncoder,
|
||||
type RecordingTargetOptions,
|
||||
type SendDebuggerCommand,
|
||||
type StartVideoEncoder,
|
||||
} from "./recording-relay.ts"
|
||||
|
||||
const defaultRetentionMs = 60_000
|
||||
const defaultFrameRate = 60
|
||||
const maxRetentionMs = 120_000
|
||||
const maxBufferedBytes = 128 * 1024 * 1024
|
||||
const maxBufferedFrames = 7_200
|
||||
|
||||
type BufferedFrame = {
|
||||
readonly data: Buffer
|
||||
readonly receivedAt: number
|
||||
readonly surfaceWidth?: number
|
||||
}
|
||||
|
||||
type ActiveFlightRecorder = {
|
||||
readonly tabId: number
|
||||
readonly sessionId?: string
|
||||
readonly retentionMs: number
|
||||
readonly frameRate: number
|
||||
readonly startedAt: number
|
||||
readonly width: number
|
||||
readonly height: number
|
||||
readonly frames: BufferedFrame[]
|
||||
bufferedBytes: number
|
||||
sourceFrameCount: number
|
||||
droppedFrameCount: number
|
||||
savePromise?: Promise<FlightRecorderSaveReceipt>
|
||||
}
|
||||
|
||||
export type FlightRecorderTarget = RecordingTargetOptions
|
||||
|
||||
export type FlightRecorderStartOptions = {
|
||||
readonly tabId: number
|
||||
readonly sessionId?: string
|
||||
readonly retentionMs?: number
|
||||
readonly frameRate?: number
|
||||
}
|
||||
|
||||
export type FlightRecorderStatus = FlightRecorderStatusResponse
|
||||
|
||||
export type FlightRecorderSaveReceipt = FlightRecorderSaveResponse
|
||||
|
||||
export class FlightRecorderRelay {
|
||||
private readonly active = new Map<number, ActiveFlightRecorder>()
|
||||
private readonly starting = new Set<number>()
|
||||
|
||||
constructor(readonly options: {
|
||||
readonly sendDebuggerCommand: SendDebuggerCommand
|
||||
readonly isExtensionConnected: () => boolean
|
||||
readonly isTabRecording?: (tabId: number) => boolean
|
||||
readonly startVideoEncoder?: StartVideoEncoder
|
||||
readonly now?: () => number
|
||||
readonly monotonicNow?: () => number
|
||||
}) {}
|
||||
|
||||
hasActiveRecorders(): boolean {
|
||||
return this.active.size > 0 || this.starting.size > 0
|
||||
}
|
||||
|
||||
isActiveTab(tabId: number): boolean {
|
||||
return this.active.has(tabId) || this.starting.has(tabId)
|
||||
}
|
||||
|
||||
async start(options: FlightRecorderStartOptions): Promise<FlightRecorderStatus> {
|
||||
if (!this.options.isExtensionConnected()) throw new Error("OpenCode Browser extension is not connected")
|
||||
if (this.isActiveTab(options.tabId)) throw new Error("Flight recorder already active for this tab")
|
||||
if (this.options.isTabRecording?.(options.tabId)) throw new Error("Stop the active recording before starting the flight recorder")
|
||||
const retentionMs = options.retentionMs ?? defaultRetentionMs
|
||||
if (!Number.isInteger(retentionMs) || retentionMs < 1_000 || retentionMs > maxRetentionMs) {
|
||||
throw new Error("Flight recorder retentionMs must be an integer from 1000 to 120000")
|
||||
}
|
||||
const frameRate = options.frameRate ?? defaultFrameRate
|
||||
if (!Number.isInteger(frameRate) || frameRate < 1 || frameRate > 60) {
|
||||
throw new Error("Flight recorder frameRate must be an integer from 1 to 60")
|
||||
}
|
||||
this.starting.add(options.tabId)
|
||||
try {
|
||||
const { width, height } = await prepareCdpScreencastViewport(this.options.sendDebuggerCommand, options.tabId)
|
||||
const recorder: ActiveFlightRecorder = {
|
||||
tabId: options.tabId,
|
||||
...(options.sessionId ? { sessionId: options.sessionId } : {}),
|
||||
retentionMs,
|
||||
frameRate,
|
||||
startedAt: this.now(),
|
||||
width,
|
||||
height,
|
||||
frames: [],
|
||||
bufferedBytes: 0,
|
||||
sourceFrameCount: 0,
|
||||
droppedFrameCount: 0,
|
||||
}
|
||||
this.active.set(options.tabId, recorder)
|
||||
await this.options.sendDebuggerCommand({
|
||||
tabId: options.tabId,
|
||||
method: "Page.startScreencast",
|
||||
params: { format: "jpeg", quality: 90, everyNthFrame: 1 },
|
||||
})
|
||||
return this.statusFor(recorder)
|
||||
} catch (error) {
|
||||
this.active.delete(options.tabId)
|
||||
throw error
|
||||
} finally {
|
||||
this.starting.delete(options.tabId)
|
||||
}
|
||||
}
|
||||
|
||||
status(target: FlightRecorderTarget): FlightRecorderStatus {
|
||||
const recorder = this.find(target)
|
||||
return recorder ? this.statusFor(recorder) : { active: false }
|
||||
}
|
||||
|
||||
async saveLast(target: FlightRecorderTarget & { readonly outputPath: string; readonly durationMs?: number }): Promise<FlightRecorderSaveReceipt> {
|
||||
const recorder = this.find(target)
|
||||
if (!recorder) throw new Error("No active flight recorder found")
|
||||
if (recorder.savePromise) throw new Error("Flight recorder save already in progress")
|
||||
const savePromise = this.saveLastForRecorder(recorder, target)
|
||||
recorder.savePromise = savePromise
|
||||
try {
|
||||
return await savePromise
|
||||
} finally {
|
||||
if (recorder.savePromise === savePromise) delete recorder.savePromise
|
||||
}
|
||||
}
|
||||
|
||||
private async saveLastForRecorder(
|
||||
recorder: ActiveFlightRecorder,
|
||||
target: FlightRecorderTarget & { readonly outputPath: string; readonly durationMs?: number },
|
||||
): Promise<FlightRecorderSaveReceipt> {
|
||||
const artifactType = cdpArtifactType(target.outputPath)
|
||||
if (!artifactType) throw new Error("Flight recorder output path must end in .webm or .mp4")
|
||||
const requestedDuration = target.durationMs ?? Math.min(30_000, recorder.retentionMs)
|
||||
if (!Number.isInteger(requestedDuration) || requestedDuration < 1 || requestedDuration > recorder.retentionMs) {
|
||||
throw new Error(`Flight recorder durationMs must be an integer from 1 to ${recorder.retentionMs}`)
|
||||
}
|
||||
if (recorder.frames.length === 0) throw new Error("Flight recorder has not captured any frames yet")
|
||||
await assertOutputAvailable(target.outputPath)
|
||||
await assertOutputAvailable(`${target.outputPath}.json`)
|
||||
const newestAt = recorder.frames.at(-1)!.receivedAt
|
||||
const frames = recorder.frames.filter((frame) => frame.receivedAt >= newestAt - requestedDuration)
|
||||
const startAt = frames[0]!.receivedAt
|
||||
const endAt = frames.at(-1)!.receivedAt
|
||||
const durationMs = Math.max(1, endAt - startAt)
|
||||
await fs.mkdir(path.dirname(target.outputPath), { recursive: true })
|
||||
let encoder: Awaited<ReturnType<StartVideoEncoder>> | undefined
|
||||
try {
|
||||
encoder = await (this.options.startVideoEncoder ?? startFfmpegVideoEncoder)({
|
||||
outputPath: target.outputPath,
|
||||
artifactType,
|
||||
frameRate: recorder.frameRate,
|
||||
width: recorder.width,
|
||||
height: recorder.height,
|
||||
})
|
||||
for (const [index, frame] of frames.entries()) {
|
||||
const next = frames[index + 1]
|
||||
const timestampMs = frame.receivedAt - startAt
|
||||
const frameDurationMs = Math.max(1, (next?.receivedAt ?? endAt + Math.round(1_000 / recorder.frameRate)) - frame.receivedAt)
|
||||
await encoder.write(frame.data, timestampMs, frameDurationMs, frame.surfaceWidth)
|
||||
}
|
||||
await encoder.finish()
|
||||
const receipt = {
|
||||
path: target.outputPath,
|
||||
durationMs,
|
||||
frameCount: frames.length,
|
||||
sourceFrameCount: recorder.sourceFrameCount,
|
||||
droppedFrameCount: recorder.droppedFrameCount,
|
||||
}
|
||||
await fs.writeFile(`${target.outputPath}.json`, `${JSON.stringify({ ...receipt, savedAt: new Date(this.now()).toISOString(), retainedDurationMs: this.retainedDuration(recorder) }, null, 2)}\n`, "utf8")
|
||||
return receipt
|
||||
} catch (error) {
|
||||
await encoder?.cancel().catch(() => {})
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async cancel(target: FlightRecorderTarget): Promise<{ readonly cancelled: boolean }> {
|
||||
const recorder = this.find(target)
|
||||
if (!recorder) return { cancelled: false }
|
||||
await recorder.savePromise?.catch(() => {})
|
||||
this.active.delete(recorder.tabId)
|
||||
await this.options.sendDebuggerCommand({ tabId: recorder.tabId, method: "Page.stopScreencast", params: {} }).catch(() => {})
|
||||
return { cancelled: true }
|
||||
}
|
||||
|
||||
async cleanupAll(): Promise<void> {
|
||||
await Promise.all([...this.active.values()].map((recorder) => this.cancel({ tabId: recorder.tabId })))
|
||||
}
|
||||
|
||||
handleDebuggerEvent(options: { readonly tabId: number; readonly method: string; readonly params: JsonObject | undefined }): boolean {
|
||||
if (options.method !== "Page.screencastFrame") return false
|
||||
const recorder = this.active.get(options.tabId)
|
||||
if (!recorder) return false
|
||||
const frameSessionId = getNumber(options.params, "sessionId")
|
||||
if (frameSessionId !== undefined) {
|
||||
void this.options.sendDebuggerCommand({
|
||||
tabId: recorder.tabId,
|
||||
method: "Page.screencastFrameAck",
|
||||
params: { sessionId: frameSessionId },
|
||||
}).catch(() => {})
|
||||
}
|
||||
const frameData = getString(options.params, "data")
|
||||
if (frameData === undefined) return true
|
||||
recorder.sourceFrameCount += 1
|
||||
if (frameData.length > Math.ceil(maxBufferedBytes * 4 / 3) + 4) {
|
||||
recorder.droppedFrameCount += 1
|
||||
return true
|
||||
}
|
||||
const data = Buffer.from(frameData, "base64")
|
||||
if (data.byteLength > maxBufferedBytes) {
|
||||
recorder.droppedFrameCount += 1
|
||||
return true
|
||||
}
|
||||
const surfaceWidth = getNumber(getObject(options.params?.metadata), "deviceWidth")
|
||||
const frame: BufferedFrame = {
|
||||
data,
|
||||
receivedAt: this.monotonicNow(),
|
||||
...(surfaceWidth !== undefined ? { surfaceWidth } : {}),
|
||||
}
|
||||
recorder.frames.push(frame)
|
||||
recorder.bufferedBytes += data.byteLength
|
||||
this.trim(recorder, frame.receivedAt)
|
||||
return true
|
||||
}
|
||||
|
||||
private trim(recorder: ActiveFlightRecorder, now: number): void {
|
||||
while (recorder.frames.length > 0 && (
|
||||
now - recorder.frames[0]!.receivedAt > recorder.retentionMs ||
|
||||
recorder.frames.length > maxBufferedFrames ||
|
||||
recorder.bufferedBytes > maxBufferedBytes
|
||||
)) {
|
||||
const removed = recorder.frames.shift()!
|
||||
recorder.bufferedBytes -= removed.data.byteLength
|
||||
recorder.droppedFrameCount += 1
|
||||
}
|
||||
}
|
||||
|
||||
private retainedDuration(recorder: ActiveFlightRecorder): number {
|
||||
if (recorder.frames.length < 2) return 0
|
||||
return Math.max(0, recorder.frames.at(-1)!.receivedAt - recorder.frames[0]!.receivedAt)
|
||||
}
|
||||
|
||||
private statusFor(recorder: ActiveFlightRecorder): FlightRecorderStatus {
|
||||
return {
|
||||
active: true,
|
||||
tabId: recorder.tabId,
|
||||
...(recorder.sessionId ? { sessionId: recorder.sessionId } : {}),
|
||||
startedAt: recorder.startedAt,
|
||||
retentionMs: recorder.retentionMs,
|
||||
retainedDurationMs: this.retainedDuration(recorder),
|
||||
frameRate: recorder.frameRate,
|
||||
bufferedFrames: recorder.frames.length,
|
||||
bufferedBytes: recorder.bufferedBytes,
|
||||
sourceFrameCount: recorder.sourceFrameCount,
|
||||
droppedFrameCount: recorder.droppedFrameCount,
|
||||
saving: recorder.savePromise !== undefined,
|
||||
}
|
||||
}
|
||||
|
||||
private find(target: FlightRecorderTarget): ActiveFlightRecorder | undefined {
|
||||
return findByRecordingTarget(this.active, target, "Multiple flight recorders are active; provide sessionId or tabId")
|
||||
}
|
||||
|
||||
private now(): number {
|
||||
return this.options.now?.() ?? Date.now()
|
||||
}
|
||||
|
||||
private monotonicNow(): number {
|
||||
return this.options.monotonicNow?.() ?? performance.now()
|
||||
}
|
||||
}
|
||||
|
||||
async function assertOutputAvailable(filePath: string): Promise<void> {
|
||||
try {
|
||||
await fs.stat(filePath)
|
||||
} catch (error) {
|
||||
if (error instanceof Error && "code" in error && error.code === "ENOENT") return
|
||||
throw error
|
||||
}
|
||||
throw new Error(`Flight recorder output already exists: ${filePath}`)
|
||||
}
|
||||
41
packages/browser-extension/cli/src/fs-durability.ts
Normal file
41
packages/browser-extension/cli/src/fs-durability.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import { Predicate } from "effect"
|
||||
import crypto from "node:crypto"
|
||||
import fs from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
|
||||
const unsupportedDirectorySyncCodes = new Set(["EPERM", "EINVAL", "ENOTSUP"])
|
||||
|
||||
export function isNodeError(error: unknown): error is NodeJS.ErrnoException {
|
||||
return error instanceof Error && "code" in error
|
||||
}
|
||||
|
||||
export function isUnsupportedDirectorySyncError(error: unknown): boolean {
|
||||
return isNodeError(error) &&
|
||||
Predicate.isString(error.code) &&
|
||||
unsupportedDirectorySyncCodes.has(error.code)
|
||||
}
|
||||
|
||||
export async function writeJsonFileAtomically(
|
||||
filePath: string,
|
||||
value: unknown,
|
||||
options: { readonly dirMode?: number } = {},
|
||||
): Promise<void> {
|
||||
const directory = path.dirname(filePath)
|
||||
await fs.mkdir(directory, {
|
||||
recursive: true,
|
||||
...(options.dirMode === undefined ? {} : { mode: options.dirMode }),
|
||||
})
|
||||
if (options.dirMode !== undefined) {
|
||||
await fs.chmod(directory, options.dirMode)
|
||||
}
|
||||
const temporaryPath = `${filePath}.${process.pid}.${crypto.randomUUID()}.tmp`
|
||||
let renamed = false
|
||||
try {
|
||||
await fs.writeFile(temporaryPath, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 })
|
||||
await fs.rename(temporaryPath, filePath)
|
||||
renamed = true
|
||||
await fs.chmod(filePath, 0o600)
|
||||
} finally {
|
||||
if (!renamed) await fs.rm(temporaryPath, { force: true }).catch(() => {})
|
||||
}
|
||||
}
|
||||
1309
packages/browser-extension/cli/src/ghost-cursor.ts
Normal file
1309
packages/browser-extension/cli/src/ghost-cursor.ts
Normal file
File diff suppressed because it is too large
Load diff
278
packages/browser-extension/cli/src/handoff.ts
Normal file
278
packages/browser-extension/cli/src/handoff.ts
Normal file
|
|
@ -0,0 +1,278 @@
|
|||
/**
|
||||
* Human-in-the-loop handoff registry.
|
||||
*
|
||||
* A running execute script can call `await handoff("Complete the 2FA prompt")`.
|
||||
* The sandbox registers a pending handoff for its OpenCode Browser session and
|
||||
* exact tab, then blocks until the in-page completion control sends the
|
||||
* matching handoff id or the timeout elapses.
|
||||
*/
|
||||
|
||||
export type HandoffCancellationReason = "target-detached" | "target-crashed"
|
||||
|
||||
export type HandoffOutcome =
|
||||
| "resolved"
|
||||
| "timeout"
|
||||
| { readonly type: "cancelled"; readonly reason: HandoffCancellationReason }
|
||||
|
||||
type PendingHandoff = {
|
||||
readonly id: string
|
||||
readonly sessionId: string
|
||||
readonly tabId: number
|
||||
targetId: string
|
||||
targetSessionId: string
|
||||
readonly message: string
|
||||
readonly resolve: (outcome: HandoffOutcome) => void
|
||||
}
|
||||
|
||||
export type HandoffWait = {
|
||||
readonly id: string
|
||||
readonly outcome: Promise<HandoffOutcome>
|
||||
/** End the wait as a timeout if it is still pending. */
|
||||
readonly cancel: () => void
|
||||
/** Defer human completion once, keeping the deadline and target cancellation live while a start action settles. */
|
||||
readonly holdCompletion: () => () => void
|
||||
}
|
||||
|
||||
export async function awaitHandoffAction(options: {
|
||||
readonly wait: HandoffWait
|
||||
readonly present?: () => Promise<void>
|
||||
readonly start?: () => unknown | Promise<unknown>
|
||||
readonly cancelStart?: () => Promise<void>
|
||||
}): Promise<HandoffOutcome> {
|
||||
const { wait } = options
|
||||
const outcome = wait.outcome.then((value) => ({ type: "handoff-completed" as const, value }))
|
||||
if (options.present) {
|
||||
const presentation = options.present().then(
|
||||
() => ({ type: "presented" as const }),
|
||||
(error: unknown) => ({ type: "presentation-failed" as const, error }),
|
||||
)
|
||||
const first = await Promise.race([presentation, outcome])
|
||||
if (first.type === "handoff-completed") return first.value
|
||||
if (first.type === "presentation-failed") {
|
||||
wait.cancel()
|
||||
throw first.error
|
||||
}
|
||||
}
|
||||
if (!options.start) return await wait.outcome
|
||||
const releaseCompletion = wait.holdCompletion()
|
||||
const action = Promise.resolve().then(options.start).then(
|
||||
() => {
|
||||
releaseCompletion()
|
||||
return { type: "action-completed" as const }
|
||||
},
|
||||
(error: unknown) => ({ type: "action-failed" as const, error }),
|
||||
)
|
||||
const first = await Promise.race([action, outcome])
|
||||
if (first.type === "action-failed") {
|
||||
wait.cancel()
|
||||
throw first.error
|
||||
}
|
||||
if (first.type === "action-completed") return await wait.outcome
|
||||
// A human completion delivered before the hold still lets the action settle.
|
||||
if (first.value === "resolved" || !options.cancelStart) {
|
||||
const actionResult = await action
|
||||
if (actionResult.type === "action-failed") throw actionResult.error
|
||||
} else {
|
||||
await options.cancelStart()
|
||||
}
|
||||
return first.value
|
||||
}
|
||||
|
||||
export type PendingHandoffView = {
|
||||
readonly id: string
|
||||
readonly sessionId: string
|
||||
readonly tabId: number
|
||||
readonly targetId: string
|
||||
readonly targetSessionId: string
|
||||
readonly message: string
|
||||
}
|
||||
|
||||
export type ToolbarClickAction = "ignore" | "toggle"
|
||||
|
||||
export function toolbarClickAction(options: {
|
||||
readonly handoffPending: boolean
|
||||
readonly sessionExecuting: boolean
|
||||
}): ToolbarClickAction {
|
||||
return options.handoffPending || options.sessionExecuting ? "ignore" : "toggle"
|
||||
}
|
||||
|
||||
export class HandoffRegistry {
|
||||
private readonly pending = new Map<string, PendingHandoff>()
|
||||
|
||||
constructor(private readonly createId: () => string = () => crypto.randomUUID()) {}
|
||||
|
||||
/**
|
||||
* Register a pending handoff for a session. Only one handoff can be pending
|
||||
* per session (execute calls are serialized per session). Returns its unique
|
||||
* id and an outcome promise.
|
||||
*/
|
||||
wait(options: {
|
||||
readonly sessionId: string
|
||||
readonly tabId: number
|
||||
readonly targetId: string
|
||||
readonly targetSessionId: string
|
||||
readonly message: string
|
||||
readonly timeoutMs: number
|
||||
}): HandoffWait {
|
||||
const existing = this.pending.get(options.sessionId)
|
||||
if (existing) {
|
||||
existing.resolve("timeout")
|
||||
}
|
||||
const id = this.createId()
|
||||
let held = false
|
||||
let humanCompleted = false
|
||||
let finish: (outcome: HandoffOutcome) => void
|
||||
const outcome = new Promise<HandoffOutcome>((resolvePromise) => {
|
||||
let pending: PendingHandoff
|
||||
const timeout = setTimeout(() => finish("timeout"), options.timeoutMs)
|
||||
finish = (outcome) => {
|
||||
clearTimeout(timeout)
|
||||
if (this.pending.get(options.sessionId) === pending) {
|
||||
this.pending.delete(options.sessionId)
|
||||
}
|
||||
resolvePromise(outcome)
|
||||
}
|
||||
pending = {
|
||||
id,
|
||||
sessionId: options.sessionId,
|
||||
tabId: options.tabId,
|
||||
targetId: options.targetId,
|
||||
targetSessionId: options.targetSessionId,
|
||||
message: options.message,
|
||||
resolve: (outcome) => {
|
||||
if (outcome === "resolved" && held) {
|
||||
humanCompleted = true
|
||||
return
|
||||
}
|
||||
finish(outcome)
|
||||
},
|
||||
}
|
||||
this.pending.set(options.sessionId, pending)
|
||||
})
|
||||
return {
|
||||
id,
|
||||
outcome,
|
||||
cancel: () => {
|
||||
this.cancel(id)
|
||||
},
|
||||
holdCompletion: () => {
|
||||
held = true
|
||||
return () => {
|
||||
if (!held) return
|
||||
held = false
|
||||
if (humanCompleted) finish("resolved")
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** Resolve only the waiter named by its token and exact registry target. */
|
||||
complete(options: {
|
||||
readonly id: string
|
||||
readonly tabId: number
|
||||
readonly targetId: string
|
||||
readonly targetSessionId: string
|
||||
}): boolean {
|
||||
const pending = Array.from(this.pending.values()).find((candidate) => candidate.id === options.id)
|
||||
if (
|
||||
!pending ||
|
||||
pending.tabId !== options.tabId ||
|
||||
pending.targetId !== options.targetId ||
|
||||
pending.targetSessionId !== options.targetSessionId
|
||||
) {
|
||||
return false
|
||||
}
|
||||
pending.resolve("resolved")
|
||||
return true
|
||||
}
|
||||
|
||||
cancel(id: string): boolean {
|
||||
const pending = Array.from(this.pending.values()).find((candidate) => candidate.id === id)
|
||||
if (!pending) return false
|
||||
pending.resolve("timeout")
|
||||
return true
|
||||
}
|
||||
|
||||
pendingForSession(sessionId: string): PendingHandoffView | undefined {
|
||||
return this.view(this.pending.get(sessionId))
|
||||
}
|
||||
|
||||
pendingForTab(tabId: number): PendingHandoffView | undefined {
|
||||
return this.view(Array.from(this.pending.values()).find((pending) => pending.tabId === tabId))
|
||||
}
|
||||
|
||||
get pendingCount(): number {
|
||||
return this.pending.size
|
||||
}
|
||||
|
||||
rebindTarget(options: {
|
||||
readonly tabId: number
|
||||
readonly previousTargetId: string
|
||||
readonly previousTargetSessionId: string
|
||||
readonly targetId: string
|
||||
readonly targetSessionId: string
|
||||
}): boolean {
|
||||
const pending = Array.from(this.pending.values()).find((candidate) => {
|
||||
return candidate.tabId === options.tabId &&
|
||||
candidate.targetId === options.previousTargetId &&
|
||||
candidate.targetSessionId === options.previousTargetSessionId
|
||||
})
|
||||
if (!pending) return false
|
||||
pending.targetId = options.targetId
|
||||
pending.targetSessionId = options.targetSessionId
|
||||
return true
|
||||
}
|
||||
|
||||
cancelForTarget(options: {
|
||||
readonly targetId: string
|
||||
readonly targetSessionId: string
|
||||
readonly reason: HandoffCancellationReason
|
||||
}): readonly PendingHandoffView[] {
|
||||
const matching = Array.from(this.pending.values()).filter((pending) => {
|
||||
return pending.targetId === options.targetId && pending.targetSessionId === options.targetSessionId
|
||||
})
|
||||
const cancelled = matching.map((pending) => this.view(pending)).filter((pending) => pending !== undefined)
|
||||
for (const pending of matching) {
|
||||
pending.resolve({ type: "cancelled", reason: options.reason })
|
||||
}
|
||||
return cancelled
|
||||
}
|
||||
|
||||
/** Cancel every pending handoff, resolving waiters as timeouts. */
|
||||
cancelAll(): void {
|
||||
for (const pending of Array.from(this.pending.values())) {
|
||||
pending.resolve("timeout")
|
||||
}
|
||||
this.pending.clear()
|
||||
}
|
||||
|
||||
private view(pending: PendingHandoff | undefined): PendingHandoffView | undefined {
|
||||
if (!pending) {
|
||||
return undefined
|
||||
}
|
||||
return {
|
||||
id: pending.id,
|
||||
sessionId: pending.sessionId,
|
||||
tabId: pending.tabId,
|
||||
targetId: pending.targetId,
|
||||
targetSessionId: pending.targetSessionId,
|
||||
message: pending.message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveExactHandoffTarget<T extends {
|
||||
readonly tabId: number
|
||||
readonly sessionId: string
|
||||
readonly targetInfo: { readonly targetId: string }
|
||||
}>(options: {
|
||||
readonly targetId: string
|
||||
readonly targets: readonly T[]
|
||||
readonly isVisible: (target: T) => boolean
|
||||
}): T {
|
||||
const target = options.targets.find((candidate) => candidate.targetInfo.targetId === options.targetId)
|
||||
if (!target || !options.isVisible(target)) {
|
||||
throw new Error("Could not bind handoff to the selected page; its target detached or is no longer visible")
|
||||
}
|
||||
return target
|
||||
}
|
||||
669
packages/browser-extension/cli/src/http-api.ts
Normal file
669
packages/browser-extension/cli/src/http-api.ts
Normal file
|
|
@ -0,0 +1,669 @@
|
|||
import type { JsonObject } from "./protocol.ts"
|
||||
import http from "node:http"
|
||||
import { Effect, Match, Predicate, Schema } from "effect"
|
||||
import * as AuthProfile from "./auth-profile.ts"
|
||||
import { AuthenticatedOriginError } from "./authenticated-origin.ts"
|
||||
import { NetworkCaptureError } from "./network-capture.ts"
|
||||
import {
|
||||
HttpRouteError,
|
||||
formatHostForUrl,
|
||||
headerValue,
|
||||
optionalSessionId,
|
||||
readJsonBody,
|
||||
requiredSessionId,
|
||||
sendJson,
|
||||
validateBrowserFetchSite,
|
||||
validateHostHeader,
|
||||
} from "./relay-helpers.ts"
|
||||
import { selectTarget, TargetSelectionError } from "./execute.ts"
|
||||
import type { FlightRecorderRelay } from "./flight-recorder.ts"
|
||||
import {
|
||||
AuthProfileRequest,
|
||||
AuthenticatedJsonRequest,
|
||||
AuthRefreshRequest,
|
||||
AuthRunRequest,
|
||||
ExecuteRequest,
|
||||
FlightRecorderSaveRequest,
|
||||
FlightRecorderStartRequest,
|
||||
NetworkSessionRequest,
|
||||
NetworkStartRequest,
|
||||
NetworkStopRequest,
|
||||
RecordingStartRequest,
|
||||
RecordingTargetRequest,
|
||||
type RelayErrorCode,
|
||||
RelayShutdownRequest,
|
||||
SessionAdoptRequest,
|
||||
SessionIdRequest,
|
||||
SessionEnsureRequest,
|
||||
SessionNewRequest,
|
||||
type ExtensionStatus,
|
||||
type TargetSummary,
|
||||
} from "./relay-schema.ts"
|
||||
import { SessionError, type OpenCodeBrowserSessions } from "./session-manager.ts"
|
||||
import type { RecordingRelay, RecordingStartOptions, RecordingTargetOptions } from "./recording-relay.ts"
|
||||
import { TargetOwnershipError, type TargetRegistry } from "./target-registry.ts"
|
||||
import { opencodeBrowserBuildId, opencodeBrowserVersion } from "./version.ts"
|
||||
import { RelayShutdown, RelayShutdownError } from "./relay-shutdown.ts"
|
||||
|
||||
export function createHttpRequestHandler(options: {
|
||||
readonly host: string
|
||||
readonly port: number
|
||||
readonly browserId: string
|
||||
readonly relayInstance: { readonly id: string; readonly startedAt: string; readonly pid: number; readonly managed: boolean }
|
||||
readonly shutdown: RelayShutdown
|
||||
readonly extensionStatus: () => Pick<ExtensionStatus,
|
||||
"connected" | "version" | "build" | "origin" | "protocolVersion" | "protocolCompatible" | "protocolLegacy" | "rejectedConnections" | "cdpClients"
|
||||
>
|
||||
readonly recordingRelay: RecordingRelay
|
||||
readonly flightRecorder: FlightRecorderRelay
|
||||
readonly registry: TargetRegistry
|
||||
readonly sessions: OpenCodeBrowserSessions
|
||||
/** Asks the extension to close agent-opened tabs idle for `idleMinutes` (its setting when omitted). */
|
||||
readonly cleanupTabs: (idleMinutes: number | undefined) => Effect.Effect<JsonObject, Error>
|
||||
/** Reloads the extension so an unpacked install picks up files `install` just replaced. */
|
||||
readonly reloadExtension: () => Effect.Effect<JsonObject, Error>
|
||||
}): (request: http.IncomingMessage, response: http.ServerResponse) => void {
|
||||
options.sessions.setUserAttachedPageUrlsProvider(() =>
|
||||
options.registry.listRootTargets()
|
||||
.filter((target) => target.owner === "user")
|
||||
.map((target) => target.targetInfo.url || "about:blank")
|
||||
)
|
||||
return (request, response) => {
|
||||
const hostError = validateHostHeader({ hostHeader: request.headers.host, host: options.host, port: options.port })
|
||||
if (hostError) {
|
||||
sendJson(response, { error: hostError }, 403)
|
||||
return
|
||||
}
|
||||
const fetchSiteError = validateBrowserFetchSite(request)
|
||||
if (fetchSiteError) {
|
||||
sendJson(response, { error: fetchSiteError }, 403)
|
||||
return
|
||||
}
|
||||
const requestUrl = new URL(request.url ?? "/", `http://${formatHostForUrl(options.host)}:${options.port}`)
|
||||
const pathname = requestUrl.pathname.replace(/\/$/, "") || "/"
|
||||
const observational = request.method === "GET" || pathname === "/network/status" || pathname === "/auth/status"
|
||||
const run = (effect: Effect.Effect<void, Error>, settle = false): void => {
|
||||
runRequestEffect(response, observational ? effect : options.shutdown.track(settle ? Effect.uninterruptible(effect) : effect))
|
||||
}
|
||||
if (pathname === "/" || pathname === "/version") {
|
||||
sendJson(response, {
|
||||
version: opencodeBrowserVersion,
|
||||
buildId: opencodeBrowserBuildId,
|
||||
instanceId: options.relayInstance.id,
|
||||
startedAt: options.relayInstance.startedAt,
|
||||
pid: options.relayInstance.pid,
|
||||
managed: options.relayInstance.managed,
|
||||
shutdownProtocol: 2,
|
||||
})
|
||||
return
|
||||
}
|
||||
if (pathname === "/shutdown" && request.method === "POST") {
|
||||
runRequestEffect(response, Effect.gen(function* () {
|
||||
const body = yield* decodeRequest(RelayShutdownRequest, yield* readJsonBody(request), "relay shutdown")
|
||||
yield* options.shutdown.request(body)
|
||||
sendJson(response, { stopping: true })
|
||||
}))
|
||||
return
|
||||
}
|
||||
if (pathname === "/json/version") {
|
||||
const opencodeBrowserSessionId = headerValue(request.headers["opencode-browser-session-id"])
|
||||
const webSocketDebuggerUrl = new URL(`ws://${formatHostForUrl(options.host)}:${options.port}/devtools/browser/${options.browserId}`)
|
||||
if (opencodeBrowserSessionId) {
|
||||
webSocketDebuggerUrl.searchParams.set("opencodeBrowserSessionId", opencodeBrowserSessionId)
|
||||
}
|
||||
sendJson(response, {
|
||||
Browser: `OpenCode-Browser/${opencodeBrowserVersion}`,
|
||||
"Protocol-Version": "1.3",
|
||||
webSocketDebuggerUrl: webSocketDebuggerUrl.toString(),
|
||||
})
|
||||
return
|
||||
}
|
||||
if (pathname === "/extension/reload" && request.method === "POST") {
|
||||
run(Effect.gen(function* () {
|
||||
sendJson(response, yield* options.reloadExtension())
|
||||
}))
|
||||
return
|
||||
}
|
||||
if (pathname === "/tabs/cleanup" && request.method === "POST") {
|
||||
run(Effect.gen(function* () {
|
||||
const body = yield* readJsonBody(request)
|
||||
const idleMinutes = typeof body === "object" && body !== null && "idleMinutes" in body && typeof body.idleMinutes === "number"
|
||||
? Math.max(0, body.idleMinutes)
|
||||
: undefined
|
||||
sendJson(response, yield* options.cleanupTabs(idleMinutes))
|
||||
}))
|
||||
return
|
||||
}
|
||||
if (pathname === "/json/list") {
|
||||
sendJson(response, targetSummaries(options.registry))
|
||||
return
|
||||
}
|
||||
if (pathname === "/extension/status") {
|
||||
const extensionStatus = options.extensionStatus()
|
||||
sendJson(response, {
|
||||
connected: extensionStatus.connected,
|
||||
version: extensionStatus.version,
|
||||
...(extensionStatus.build === undefined ? {} : { build: extensionStatus.build }),
|
||||
...(extensionStatus.origin === undefined ? {} : { origin: extensionStatus.origin }),
|
||||
...(extensionStatus.protocolVersion === undefined ? {} : { protocolVersion: extensionStatus.protocolVersion }),
|
||||
...(extensionStatus.protocolCompatible === undefined ? {} : { protocolCompatible: extensionStatus.protocolCompatible }),
|
||||
...(extensionStatus.protocolLegacy === undefined ? {} : { protocolLegacy: extensionStatus.protocolLegacy }),
|
||||
...(extensionStatus.rejectedConnections === undefined ? {} : { rejectedConnections: extensionStatus.rejectedConnections }),
|
||||
...(extensionStatus.cdpClients === undefined ? {} : { cdpClients: extensionStatus.cdpClients }),
|
||||
activeTargets: options.registry.rootTargetCount(),
|
||||
childTargets: options.registry.childTargets.size,
|
||||
sessions: options.sessions.listSummaries(),
|
||||
targets: targetSummaries(options.registry),
|
||||
})
|
||||
return
|
||||
}
|
||||
if (pathname.startsWith("/recording/")) {
|
||||
run(handleRecordingRequest({ request, response, pathname, requestUrl, registry: options.registry, recordingRelay: options.recordingRelay }), true)
|
||||
return
|
||||
}
|
||||
if (pathname.startsWith("/flight-recorder/")) {
|
||||
run(handleFlightRecorderRequest({ request, response, pathname, requestUrl, registry: options.registry, flightRecorder: options.flightRecorder }), true)
|
||||
return
|
||||
}
|
||||
if (pathname.startsWith("/network/")) {
|
||||
run(handleNetworkRequest({ request, response, pathname, sessions: options.sessions }))
|
||||
return
|
||||
}
|
||||
if (pathname.startsWith("/auth/")) {
|
||||
run(handleAuthRequest({ request, response, pathname, sessions: options.sessions }), pathname === "/auth/run")
|
||||
return
|
||||
}
|
||||
if (pathname.startsWith("/v1/")) {
|
||||
run(handleClientRequest({
|
||||
request,
|
||||
response,
|
||||
pathname,
|
||||
sessions: options.sessions,
|
||||
}))
|
||||
return
|
||||
}
|
||||
if (pathname.startsWith("/cli/")) {
|
||||
run(handleCliRequest({
|
||||
request,
|
||||
response,
|
||||
pathname,
|
||||
sessions: options.sessions,
|
||||
registry: options.registry,
|
||||
}))
|
||||
return
|
||||
}
|
||||
response.writeHead(404)
|
||||
response.end("Not found")
|
||||
}
|
||||
}
|
||||
|
||||
function handleClientRequest(options: {
|
||||
readonly request: http.IncomingMessage
|
||||
readonly response: http.ServerResponse
|
||||
readonly pathname: string
|
||||
readonly sessions: OpenCodeBrowserSessions
|
||||
}): Effect.Effect<void, Error> {
|
||||
return Effect.gen(function* () {
|
||||
if (options.pathname === "/v1/sessions/ensure" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(SessionEnsureRequest, yield* readJsonBody(options.request), "session ensure")
|
||||
sendJson(options.response, {
|
||||
session: yield* options.sessions.ensure(request.id, {
|
||||
...(request.readOnly === undefined ? {} : { readOnly: request.readOnly }),
|
||||
}),
|
||||
})
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/v1/authenticated-origin/json" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(AuthenticatedJsonRequest, yield* readJsonBody(options.request), "authenticated origin")
|
||||
options.response.setHeader("cache-control", "no-store")
|
||||
sendJson(options.response, yield* options.sessions.authenticatedJson(request))
|
||||
return
|
||||
}
|
||||
options.response.writeHead(404)
|
||||
options.response.end("Not found")
|
||||
})
|
||||
}
|
||||
|
||||
function handleNetworkRequest(options: {
|
||||
readonly request: http.IncomingMessage
|
||||
readonly response: http.ServerResponse
|
||||
readonly pathname: string
|
||||
readonly sessions: OpenCodeBrowserSessions
|
||||
}): Effect.Effect<void, Error> {
|
||||
return Effect.gen(function* () {
|
||||
if (options.pathname === "/network/start" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(NetworkStartRequest, yield* readJsonBody(options.request), "network start")
|
||||
const { sessionId, ...captureOptions } = request
|
||||
const result = yield* options.sessions.networkStart(sessionId, captureOptions)
|
||||
sendJson(options.response, result)
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/network/status" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(NetworkSessionRequest, yield* readJsonBody(options.request), "network status")
|
||||
sendJson(options.response, yield* options.sessions.networkStatus(request.sessionId))
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/network/stop" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(NetworkStopRequest, yield* readJsonBody(options.request), "network stop")
|
||||
const { sessionId, ...stopOptions } = request
|
||||
sendJson(options.response, yield* options.sessions.networkStop(sessionId, stopOptions))
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/network/cancel" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(NetworkSessionRequest, yield* readJsonBody(options.request), "network cancel")
|
||||
sendJson(options.response, yield* options.sessions.networkCancel(request.sessionId))
|
||||
return
|
||||
}
|
||||
options.response.writeHead(404)
|
||||
options.response.end("Not found")
|
||||
})
|
||||
}
|
||||
|
||||
function handleAuthRequest(options: {
|
||||
readonly request: http.IncomingMessage
|
||||
readonly response: http.ServerResponse
|
||||
readonly pathname: string
|
||||
readonly sessions: OpenCodeBrowserSessions
|
||||
}): Effect.Effect<void, Error> {
|
||||
return Effect.gen(function* () {
|
||||
if (options.pathname === "/auth/status" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(AuthProfileRequest, yield* readJsonBody(options.request), "auth status")
|
||||
sendJson(options.response, yield* AuthProfile.status(request.name))
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/auth/refresh" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(AuthRefreshRequest, yield* readJsonBody(options.request), "auth refresh")
|
||||
const { sessionId, ...refreshOptions } = request
|
||||
sendJson(options.response, yield* options.sessions.authRefresh(sessionId, refreshOptions))
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/auth/run" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(AuthRunRequest, yield* readJsonBody(options.request), "auth run")
|
||||
sendJson(options.response, yield* AuthProfile.run(request))
|
||||
return
|
||||
}
|
||||
options.response.writeHead(404)
|
||||
options.response.end("Not found")
|
||||
})
|
||||
}
|
||||
|
||||
function runRequestEffect(response: http.ServerResponse, effect: Effect.Effect<void, Error>): void {
|
||||
const controller = new AbortController()
|
||||
const onClose = () => controller.abort()
|
||||
response.once("close", onClose)
|
||||
Effect.runPromise(effect, { signal: controller.signal }).catch((error: unknown) => {
|
||||
if (response.destroyed || response.writableEnded) return
|
||||
const routeError = relayHttpError(error)
|
||||
sendJson(response, {
|
||||
error: routeError.message,
|
||||
code: routeError.code,
|
||||
}, routeError.status)
|
||||
}).finally(() => {
|
||||
response.off("close", onClose)
|
||||
})
|
||||
}
|
||||
|
||||
function handleRecordingRequest(options: {
|
||||
readonly request: http.IncomingMessage
|
||||
readonly response: http.ServerResponse
|
||||
readonly pathname: string
|
||||
readonly requestUrl: URL
|
||||
readonly registry: TargetRegistry
|
||||
readonly recordingRelay: RecordingRelay
|
||||
}): Effect.Effect<void, Error> {
|
||||
return Effect.gen(function* () {
|
||||
if (options.pathname === "/recording/start" && options.request.method === "POST") {
|
||||
const body = yield* readJsonBody(options.request)
|
||||
const { tabId, sessionId, ...recordingOptions } = yield* decodeRequest(RecordingStartRequest, body, "recording start")
|
||||
const target = resolveAttachedRecordingTarget({ registry: options.registry, tabId, sessionId })
|
||||
const startOptions: RecordingStartOptions = {
|
||||
...recordingOptions,
|
||||
tabId: target.tabId,
|
||||
...(target.sessionId ? { sessionId: target.sessionId } : {}),
|
||||
owner: target.owner,
|
||||
}
|
||||
const result = yield* tryRecordingPromise("start recording", () => options.recordingRelay.startRecording(startOptions))
|
||||
sendJson(options.response, result, result.success ? 200 : 500)
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/recording/stop" && options.request.method === "POST") {
|
||||
const body = yield* readJsonBody(options.request)
|
||||
const request = yield* decodeRequest(RecordingTargetRequest, body, "recording stop")
|
||||
const target = recordingTargetFromValues({ registry: options.registry, tabId: request.tabId, sessionId: request.sessionId })
|
||||
const result = yield* tryRecordingPromise("stop recording", () => options.recordingRelay.stopRecording(target))
|
||||
sendJson(options.response, result, result.success ? 200 : 500)
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/recording/status" && options.request.method === "GET") {
|
||||
const target = recordingTargetFromQuery({ registry: options.registry, searchParams: options.requestUrl.searchParams })
|
||||
const result = yield* tryRecordingPromise("recording status", () => options.recordingRelay.statusRecording(target))
|
||||
sendJson(options.response, result)
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/recording/cancel" && options.request.method === "POST") {
|
||||
const body = yield* readJsonBody(options.request)
|
||||
const request = yield* decodeRequest(RecordingTargetRequest, body, "recording cancel")
|
||||
const target = recordingTargetFromValues({ registry: options.registry, tabId: request.tabId, sessionId: request.sessionId })
|
||||
const result = yield* tryRecordingPromise("cancel recording", () => options.recordingRelay.cancelRecording(target))
|
||||
sendJson(options.response, result, result.success ? 200 : 500)
|
||||
return
|
||||
}
|
||||
options.response.writeHead(404)
|
||||
options.response.end("Not found")
|
||||
})
|
||||
}
|
||||
|
||||
function handleFlightRecorderRequest(options: {
|
||||
readonly request: http.IncomingMessage
|
||||
readonly response: http.ServerResponse
|
||||
readonly pathname: string
|
||||
readonly requestUrl: URL
|
||||
readonly registry: TargetRegistry
|
||||
readonly flightRecorder: FlightRecorderRelay
|
||||
}): Effect.Effect<void, Error> {
|
||||
return Effect.gen(function* () {
|
||||
if (options.pathname === "/flight-recorder/start" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(FlightRecorderStartRequest, yield* readJsonBody(options.request), "flight recorder start")
|
||||
const target = resolveAttachedRecordingTarget({ registry: options.registry, tabId: request.tabId, sessionId: request.sessionId })
|
||||
const result = yield* tryRecordingPromise("start flight recorder", () => options.flightRecorder.start({
|
||||
tabId: target.tabId,
|
||||
...(target.sessionId ? { sessionId: target.sessionId } : {}),
|
||||
...(request.retentionMs === undefined ? {} : { retentionMs: request.retentionMs }),
|
||||
...(request.frameRate === undefined ? {} : { frameRate: request.frameRate }),
|
||||
}))
|
||||
sendJson(options.response, result)
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/flight-recorder/status" && options.request.method === "GET") {
|
||||
sendJson(options.response, options.flightRecorder.status(recordingTargetFromQuery({ registry: options.registry, searchParams: options.requestUrl.searchParams })))
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/flight-recorder/save-last" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(FlightRecorderSaveRequest, yield* readJsonBody(options.request), "flight recorder save-last")
|
||||
const target = recordingTargetFromValues({ registry: options.registry, tabId: request.tabId, sessionId: request.sessionId })
|
||||
const result = yield* tryRecordingPromise("save flight recorder", () => options.flightRecorder.saveLast({
|
||||
...target,
|
||||
outputPath: request.outputPath,
|
||||
...(request.durationMs === undefined ? {} : { durationMs: request.durationMs }),
|
||||
}))
|
||||
sendJson(options.response, result)
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/flight-recorder/cancel" && options.request.method === "POST") {
|
||||
const request = yield* decodeRequest(RecordingTargetRequest, yield* readJsonBody(options.request), "flight recorder cancel")
|
||||
const target = recordingTargetFromValues({ registry: options.registry, tabId: request.tabId, sessionId: request.sessionId })
|
||||
sendJson(options.response, yield* Effect.promise(() => options.flightRecorder.cancel(target)))
|
||||
return
|
||||
}
|
||||
options.response.writeHead(404)
|
||||
options.response.end("Not found")
|
||||
})
|
||||
}
|
||||
|
||||
function tryRecordingPromise<A>(label: string, tryFn: () => Promise<A>): Effect.Effect<A, Error> {
|
||||
return Effect.tryPromise({
|
||||
try: tryFn,
|
||||
catch: (cause) => new Error(formatCauseMessage({ label, cause }), { cause }),
|
||||
})
|
||||
}
|
||||
|
||||
function formatCauseMessage(options: { readonly label: string; readonly cause: unknown }): string {
|
||||
if (options.cause instanceof Error && options.cause.message) {
|
||||
return `${options.label}: ${options.cause.message}`
|
||||
}
|
||||
if (Predicate.isString(options.cause) && options.cause) {
|
||||
return `${options.label}: ${options.cause}`
|
||||
}
|
||||
return options.label
|
||||
}
|
||||
|
||||
function handleCliRequest(options: {
|
||||
readonly request: http.IncomingMessage
|
||||
readonly response: http.ServerResponse
|
||||
readonly pathname: string
|
||||
readonly sessions: OpenCodeBrowserSessions
|
||||
readonly registry: TargetRegistry
|
||||
}): Effect.Effect<void, Error> {
|
||||
return Effect.gen(function* () {
|
||||
if (options.pathname === "/cli/sessions" && options.request.method === "GET") {
|
||||
sendJson(options.response, { sessions: options.sessions.listSummaries() })
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/cli/session/new" && options.request.method === "POST") {
|
||||
const body = yield* readJsonBody(options.request)
|
||||
const request = yield* decodeRequest(SessionNewRequest, body, "session new")
|
||||
const session = yield* options.sessions.create(optionalSessionId(request.id), { readOnly: request.readOnly === true })
|
||||
sendJson(options.response, { session: options.sessions.summary(session.id) })
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/cli/session/delete" && options.request.method === "POST") {
|
||||
const body = yield* readJsonBody(options.request)
|
||||
const request = yield* decodeRequest(SessionIdRequest, body, "session delete")
|
||||
const id = requiredSessionId(request.id)
|
||||
const deleted = yield* options.sessions.delete(id)
|
||||
sendJson(options.response, { deleted, id })
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/cli/session/reset" && options.request.method === "POST") {
|
||||
const body = yield* readJsonBody(options.request)
|
||||
const request = yield* decodeRequest(SessionIdRequest, body, "session reset")
|
||||
const id = requiredSessionId(request.id)
|
||||
const session = yield* options.sessions.reset(id)
|
||||
if (!session) {
|
||||
sendJson(options.response, { error: `Session not found: ${id}`, code: "session-not-found" }, 404)
|
||||
return
|
||||
}
|
||||
sendJson(options.response, { session })
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/cli/session/adopt" && options.request.method === "POST") {
|
||||
const body = yield* readJsonBody(options.request)
|
||||
const request = yield* decodeRequest(SessionAdoptRequest, body, "session adopt")
|
||||
const requestedSessionId = optionalSessionId(request.sessionId)
|
||||
const allTargets = options.registry.listRootTargets()
|
||||
const byTab = request.tabId === undefined ? undefined : allTargets.find((target) => target.tabId === request.tabId)
|
||||
if (request.tabId !== undefined && !byTab)
|
||||
throw new Error(`Tab ${request.tabId} is not available to agents. Ask the user to click the OpenCode Browser toolbar button on that tab.`)
|
||||
const candidates = byTab ? [byTab] : request.targetSelection
|
||||
? allTargets
|
||||
: (() => {
|
||||
const adoptableUserTargets = allTargets.filter((target) =>
|
||||
target.owner === "user" && (!target.opencodeBrowserSessionId || target.opencodeBrowserSessionId === requestedSessionId)
|
||||
)
|
||||
return adoptableUserTargets.length > 0 ? adoptableUserTargets : allTargets
|
||||
})()
|
||||
const selectedTarget = selectTarget({
|
||||
targets: candidates,
|
||||
selection: request.targetSelection ?? {},
|
||||
getUrl: (target) => target.targetInfo.url,
|
||||
getIndex: (target) => allTargets.indexOf(target),
|
||||
})
|
||||
if (!selectedTarget) {
|
||||
throw new Error("No page matched target selection")
|
||||
}
|
||||
const adoptedTargetId = selectedTarget.targetInfo.targetId
|
||||
const { session, adoptedUrl } = yield* options.sessions.adopt({
|
||||
...(requestedSessionId ? { sessionId: requestedSessionId } : {}),
|
||||
createIfMissing: request.createIfMissing,
|
||||
targetId: adoptedTargetId,
|
||||
targetUrl: selectedTarget.targetInfo.url,
|
||||
})
|
||||
sendJson(options.response, { session, adoptedUrl, adoptedTargetId })
|
||||
return
|
||||
}
|
||||
if (options.pathname === "/cli/execute" && options.request.method === "POST") {
|
||||
const body = yield* readJsonBody(options.request)
|
||||
const request = yield* decodeRequest(ExecuteRequest, body, "execute")
|
||||
const requestedSessionId = optionalSessionId(request.sessionId)
|
||||
const { result, session } = yield* options.sessions.execute({
|
||||
...(requestedSessionId ? { sessionId: requestedSessionId } : {}),
|
||||
code: request.code,
|
||||
createIfMissing: request.createIfMissing,
|
||||
...(request.targetSelection ? { targetSelection: request.targetSelection } : {}),
|
||||
})
|
||||
const { setupFailed: _setupFailed, ...wireResult } = result
|
||||
sendJson(options.response, { ...wireResult, session })
|
||||
return
|
||||
}
|
||||
options.response.writeHead(404)
|
||||
options.response.end("Not found")
|
||||
})
|
||||
}
|
||||
|
||||
function targetSummaries(registry: TargetRegistry): TargetSummary[] {
|
||||
return registry.listRootTargets().map((target) => {
|
||||
return {
|
||||
id: target.targetInfo.targetId,
|
||||
type: target.targetInfo.type,
|
||||
title: target.targetInfo.title,
|
||||
url: target.targetInfo.url,
|
||||
tabId: target.tabId,
|
||||
sessionId: target.sessionId,
|
||||
...(target.opencodeBrowserSessionId ? { opencodeBrowserSessionId: target.opencodeBrowserSessionId } : {}),
|
||||
owner: target.owner,
|
||||
...(target.crashed ? { crashed: true } : {}),
|
||||
...(target.protectedUi ? { protectedUi: true } : {}),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function decodeRequest<A>(schema: Schema.ConstraintDecoder<A>, body: unknown, label: string): Effect.Effect<A, Error> {
|
||||
return Schema.decodeUnknownEffect(schema)(body).pipe(
|
||||
Effect.mapError((cause) => new HttpRouteError({
|
||||
message: `Invalid ${label} request: ${cause.message}`,
|
||||
status: 400,
|
||||
code: "invalid-request",
|
||||
})),
|
||||
)
|
||||
}
|
||||
|
||||
function resolveAttachedRecordingTarget(options: {
|
||||
readonly registry: TargetRegistry
|
||||
readonly tabId: unknown
|
||||
readonly sessionId: unknown
|
||||
}): { readonly tabId: number; readonly sessionId?: string; readonly owner: "relay" | "user" } {
|
||||
const tabId = optionalInteger(options.tabId, "tabId")
|
||||
if (tabId !== undefined) {
|
||||
const target = options.registry.getRootTargetByTabId(tabId)
|
||||
if (!target) {
|
||||
throw new HttpRouteError({ message: `No attached tab found for tabId ${tabId}`, status: 404, code: "target-not-found" })
|
||||
}
|
||||
return { tabId, sessionId: target.sessionId, owner: target.owner }
|
||||
}
|
||||
const sessionId = Predicate.isString(options.sessionId) && options.sessionId ? options.sessionId : undefined
|
||||
if (sessionId) {
|
||||
const target = options.registry.getRootTargetBySessionId(sessionId)
|
||||
if (!target) {
|
||||
throw new HttpRouteError({ message: `No attached tab found for sessionId ${sessionId}`, status: 404, code: "target-not-found" })
|
||||
}
|
||||
return { tabId: target.tabId, sessionId: target.sessionId, owner: target.owner }
|
||||
}
|
||||
const targets = options.registry.listRootTargets()
|
||||
if (targets.length === 0) {
|
||||
throw new HttpRouteError({ message: "No attached tab available for recording", status: 404, code: "target-not-found" })
|
||||
}
|
||||
if (targets.length > 1) {
|
||||
throw new HttpRouteError({ message: "Multiple attached tabs available; provide sessionId or tabId", status: 409, code: "target-ambiguous" })
|
||||
}
|
||||
const target = targets[0]
|
||||
if (!target) {
|
||||
throw new HttpRouteError({ message: "No attached tab available for recording", status: 404, code: "target-not-found" })
|
||||
}
|
||||
return { tabId: target.tabId, sessionId: target.sessionId, owner: target.owner }
|
||||
}
|
||||
|
||||
function recordingTargetFromValues(options: { readonly registry: TargetRegistry; readonly tabId: unknown; readonly sessionId: unknown }): RecordingTargetOptions {
|
||||
const tabId = optionalInteger(options.tabId, "tabId")
|
||||
const sessionId = Predicate.isString(options.sessionId) && options.sessionId ? options.sessionId : undefined
|
||||
const target = sessionId ? options.registry.getRootTargetBySessionId(sessionId) : undefined
|
||||
return {
|
||||
...(tabId === undefined ? {} : { tabId }),
|
||||
...(target?.sessionId ? { sessionId: target.sessionId } : sessionId ? { sessionId } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
function recordingTargetFromQuery(options: { readonly registry: TargetRegistry; readonly searchParams: URLSearchParams }): RecordingTargetOptions {
|
||||
const tabIdText = options.searchParams.get("tabId")
|
||||
return recordingTargetFromValues({
|
||||
registry: options.registry,
|
||||
tabId: tabIdText ? Number(tabIdText) : undefined,
|
||||
sessionId: options.searchParams.get("sessionId") ?? undefined,
|
||||
})
|
||||
}
|
||||
|
||||
function optionalInteger(value: unknown, field: string): number | undefined {
|
||||
if (value === undefined) {
|
||||
return undefined
|
||||
}
|
||||
if (!Predicate.isNumber(value) || !Number.isInteger(value)) {
|
||||
throw new HttpRouteError({ message: `${field} must be an integer`, status: 400, code: "invalid-request" })
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function relayHttpError(error: unknown): HttpRouteError {
|
||||
const routeError = (message: string, status: number, code: RelayErrorCode) =>
|
||||
new HttpRouteError({ message, status, code })
|
||||
if (error instanceof RelayShutdownError) {
|
||||
return routeError(error.message, 409, error.reason === "busy" ? "relay-busy" : "invalid-request")
|
||||
}
|
||||
if (error instanceof HttpRouteError) {
|
||||
return error
|
||||
}
|
||||
if (error instanceof SessionError) {
|
||||
const [status, code] = Match.value(error.reason).pipe(
|
||||
Match.when("already-exists", () => [409, "session-already-exists"] as const),
|
||||
Match.when("inactive", () => [409, "session-inactive"] as const),
|
||||
Match.when("invalid-request", () => [400, "invalid-request"] as const),
|
||||
Match.when("not-found", () => [404, "session-not-found"] as const),
|
||||
Match.when("target-owned", () => [409, "target-owned"] as const),
|
||||
Match.when("timeout", () => [409, "session-timeout"] as const),
|
||||
Match.when("setup-failed", () => [500, "setup-failed"] as const),
|
||||
Match.exhaustive,
|
||||
)
|
||||
return routeError(error.message, status, code)
|
||||
}
|
||||
if (error instanceof NetworkCaptureError) {
|
||||
const [status, code] = Match.value(error.reason).pipe(
|
||||
Match.when("invalid-options", () => [400, "invalid-request"] as const),
|
||||
Match.when("already-active", () => [409, "capture-conflict"] as const),
|
||||
Match.when("inactive", () => [409, "capture-conflict"] as const),
|
||||
Match.orElse(() => [500, "internal"] as const),
|
||||
)
|
||||
return routeError(error.message, status, code)
|
||||
}
|
||||
if (error instanceof AuthProfile.AuthProfileError) {
|
||||
const [status, code] = Match.value(error.reason).pipe(
|
||||
Match.when("invalid-name", () => [400, "invalid-request"] as const),
|
||||
Match.when("not-found", () => [404, "auth-profile-not-found"] as const),
|
||||
Match.orElse(() => [500, "internal"] as const),
|
||||
)
|
||||
return routeError(error.message, status, code)
|
||||
}
|
||||
if (error instanceof AuthenticatedOriginError) {
|
||||
return routeError(
|
||||
error.message,
|
||||
error.reason === "invalid-request" ? 400 : 500,
|
||||
error.reason === "invalid-request" ? "invalid-request" : "setup-failed",
|
||||
)
|
||||
}
|
||||
if (error instanceof TargetSelectionError) {
|
||||
const [status, code] = Match.value(error.reason).pipe(
|
||||
Match.when("invalid", () => [400, "invalid-request"] as const),
|
||||
Match.when("not-found", () => [404, "target-not-found"] as const),
|
||||
Match.when("ambiguous", () => [409, "target-ambiguous"] as const),
|
||||
Match.exhaustive,
|
||||
)
|
||||
return routeError(error.message, status, code)
|
||||
}
|
||||
if (error instanceof TargetOwnershipError) {
|
||||
const [status, code] = Match.value(error.reason).pipe(
|
||||
Match.when("not-found", () => [404, "target-not-found"] as const),
|
||||
Match.when("owned", () => [409, "target-owned"] as const),
|
||||
Match.when("generation-changed", () => [409, "target-changed"] as const),
|
||||
Match.exhaustive,
|
||||
)
|
||||
return routeError(error.message, status, code)
|
||||
}
|
||||
return routeError(error instanceof Error ? error.message : String(error), 500, "internal")
|
||||
}
|
||||
251
packages/browser-extension/cli/src/human-input.ts
Normal file
251
packages/browser-extension/cli/src/human-input.ts
Normal file
|
|
@ -0,0 +1,251 @@
|
|||
// Human-like pointer and keyboard timing for the input agents send through the relay.
|
||||
//
|
||||
// Playwright moves the mouse by teleporting: one mouseMoved event straight to the exact center of the target,
|
||||
// then a press and release a few milliseconds apart. Behavioral bot detection (reCAPTCHA v3, Cloudflare, DataDome,
|
||||
// HUMAN) scores exactly that. This module sits in the relay's CDP path and, for each Input.dispatchMouseEvent
|
||||
// that moves the pointer, first sends a curved, eased trajectory from the pointer's last position (a cubic Bezier
|
||||
// with a perpendicular bow, minimum-jerk velocity, ~60-120 Hz samples, sub-pixel tremor, and an occasional
|
||||
// overshoot and correction), with a duration that grows with distance (Fitts' law). It also holds a press for a
|
||||
// human 60-140 ms, settles briefly before pressing, and spaces consecutive key presses 30-110 ms apart. Scrolling
|
||||
// glides: each wheel event becomes an eased ~60 Hz burst, and scrolling an element into view happens in wheel
|
||||
// flicks instead of a one-frame jump.
|
||||
//
|
||||
// Off with OPENCODE_BROWSER_HUMAN_INPUT=0 (for example for fast test runs).
|
||||
import type { JsonObject } from "./protocol.ts"
|
||||
import { relayLog } from "./relay-log.ts"
|
||||
|
||||
type Point = { x: number; y: number }
|
||||
type Send = (method: string, params: JsonObject) => Promise<unknown>
|
||||
|
||||
const enabled = !/^(0|false|off|no)$/i.test(process.env.OPENCODE_BROWSER_HUMAN_INPUT ?? "")
|
||||
|
||||
type TabState = { pointer?: Point; aim?: Point & { dx: number; dy: number }; arrivedAt: number; pressedAt?: number; lastKeyAt: number; queue: Promise<void> }
|
||||
const tabs = new Map<number, TabState>()
|
||||
|
||||
function state(tabId: number): TabState {
|
||||
let value = tabs.get(tabId)
|
||||
if (!value) {
|
||||
value = { arrivedAt: 0, lastKeyAt: 0, queue: Promise.resolve() }
|
||||
tabs.set(tabId, value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
export function forgetTab(tabId: number) {
|
||||
tabs.delete(tabId)
|
||||
}
|
||||
|
||||
const sleep = (ms: number) => (ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve())
|
||||
const random = (min: number, max: number) => min + Math.random() * (max - min)
|
||||
/** Normal-ish noise (sum of uniforms). */
|
||||
const noise = (scale: number) => (Math.random() + Math.random() + Math.random() - 1.5) * scale
|
||||
|
||||
/** Minimum-jerk position profile: slow start, fast middle, slow arrival. */
|
||||
const minimumJerk = (t: number) => t * t * t * (10 - 15 * t + 6 * t * t)
|
||||
|
||||
function bezier(p0: Point, p1: Point, p2: Point, p3: Point, t: number): Point {
|
||||
const u = 1 - t
|
||||
return {
|
||||
x: u * u * u * p0.x + 3 * u * u * t * p1.x + 3 * u * t * t * p2.x + t * t * t * p3.x,
|
||||
y: u * u * u * p0.y + 3 * u * u * t * p1.y + 3 * u * t * t * p2.y + t * t * t * p3.y,
|
||||
}
|
||||
}
|
||||
|
||||
/** Points (with delays) for one human-looking movement from `from` to `to`. */
|
||||
export function trajectory(from: Point, to: Point): { point: Point; delay: number }[] {
|
||||
const dx = to.x - from.x
|
||||
const dy = to.y - from.y
|
||||
const distance = Math.hypot(dx, dy)
|
||||
if (distance < 2) return []
|
||||
// Fitts' law with a ~40px target, plus personal variation.
|
||||
const duration = Math.min(1100, (90 + 110 * Math.log2(distance / 40 + 1)) * random(0.85, 1.25))
|
||||
// Bow the path to one side, more for longer moves, never past ~18% of the distance.
|
||||
const normal = { x: -dy / distance, y: dx / distance }
|
||||
const bow = (Math.random() < 0.5 ? -1 : 1) * random(0.04, 0.18) * distance
|
||||
const c1 = { x: from.x + dx * random(0.2, 0.35) + normal.x * bow, y: from.y + dy * random(0.2, 0.35) + normal.y * bow }
|
||||
const c2 = { x: from.x + dx * random(0.65, 0.8) + normal.x * bow * 0.6, y: from.y + dy * random(0.65, 0.8) + normal.y * bow * 0.6 }
|
||||
// Long moves sometimes overshoot slightly and come back.
|
||||
const overshoot = distance > 250 && Math.random() < 0.25
|
||||
const end = overshoot ? { x: to.x + (dx / distance) * random(4, 12), y: to.y + (dy / distance) * random(4, 12) } : to
|
||||
const points: { point: Point; delay: number }[] = []
|
||||
let elapsed = 0
|
||||
while (elapsed < duration) {
|
||||
const step = random(8, 17)
|
||||
elapsed = Math.min(duration, elapsed + step)
|
||||
const s = minimumJerk(elapsed / duration)
|
||||
const p = bezier(from, c1, c2, end, s)
|
||||
// Tremor fades out as the hand settles on the target.
|
||||
const tremor = (1 - s) * 0.9
|
||||
points.push({ point: { x: p.x + noise(tremor), y: p.y + noise(tremor) }, delay: step })
|
||||
}
|
||||
if (overshoot) {
|
||||
const back = Math.max(3, Math.round(random(4, 7)))
|
||||
for (let i = 1; i <= back; i++) {
|
||||
const s = minimumJerk(i / back)
|
||||
points.push({ point: { x: end.x + (to.x - end.x) * s, y: end.y + (to.y - end.y) * s }, delay: random(10, 18) })
|
||||
}
|
||||
}
|
||||
// The caller's own event lands exactly on the target; drop a final sample that duplicates it.
|
||||
points.pop()
|
||||
return points.map(({ point, delay }) => ({ point: { x: Math.round(point.x * 10) / 10, y: Math.round(point.y * 10) / 10 }, delay }))
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs before an input or scroll command is forwarded: sends the movement or scrolling leading up to it and paces
|
||||
* it. The caller then sends the command itself; only a mouse event's x/y (a few pixels off center) and a wheel's
|
||||
* remaining delta are adjusted, so Playwright's own semantics (buttons, modifiers, frames) are untouched. `root`
|
||||
* is false for commands to a child frame, whose coordinates aren't the page's.
|
||||
*/
|
||||
export function beforeInput(tabId: number, method: string, params: JsonObject, root: boolean, send: Send): Promise<void> {
|
||||
if (!enabled || !paced.has(method) || (method === "DOM.scrollIntoViewIfNeeded" && !root)) return Promise.resolve()
|
||||
// Playwright doesn't always wait for a move before pressing, so one tab's input runs in order: the press
|
||||
// waits for the movement before it to land.
|
||||
const tab = state(tabId)
|
||||
const run = tab.queue.then(() => {
|
||||
if (method === "Input.dispatchKeyEvent") return paceKey(tab, params)
|
||||
if (method === "DOM.scrollIntoViewIfNeeded") return scrollToward(tabId, tab, params, send)
|
||||
return paceMouse(tabId, tab, params, send)
|
||||
})
|
||||
tab.queue = run.catch(() => undefined)
|
||||
return run
|
||||
}
|
||||
|
||||
const paced = new Set(["Input.dispatchKeyEvent", "Input.dispatchMouseEvent", "DOM.scrollIntoViewIfNeeded"])
|
||||
|
||||
async function paceMouse(tabId: number, tab: TabState, params: JsonObject, send: Send): Promise<void> {
|
||||
const type = params.type
|
||||
if (typeof params.x !== "number" || typeof params.y !== "number") return
|
||||
// The relay forwards this same object, so adjustments here are what Chrome receives.
|
||||
const target = params as Record<string, unknown>
|
||||
if (type === "mouseWheel") {
|
||||
// One wheel event jumps the page; a hand on a trackpad or wheel scrolls in a burst that slows to a stop.
|
||||
const frames = glide(Number(params.deltaX ?? 0), Number(params.deltaY ?? 0))
|
||||
const last = frames.pop()
|
||||
await sendFrames(frames, (frame) => send("Input.dispatchMouseEvent", { ...params, deltaX: frame.x, deltaY: frame.y }))
|
||||
if (last) {
|
||||
target.deltaX = last.x
|
||||
target.deltaY = last.y
|
||||
}
|
||||
return
|
||||
}
|
||||
// Playwright aims at the exact center of an element every time; people land a few pixels off, the same way
|
||||
// for the move, press and release on one target. The offset stays well inside any clickable element.
|
||||
if (!tab.aim || Math.hypot(tab.aim.x - params.x, tab.aim.y - params.y) >= 1)
|
||||
tab.aim = { x: params.x, y: params.y, dx: noise(2.2), dy: noise(1.6) }
|
||||
const x = Math.round((params.x + tab.aim.dx) * 10) / 10
|
||||
const y = Math.round((params.y + tab.aim.dy) * 10) / 10
|
||||
target.x = x
|
||||
target.y = y
|
||||
if (type === "mouseMoved" || ((type === "mousePressed" || type === "mouseReleased") && tab.pointer && Math.hypot(tab.pointer.x - x, tab.pointer.y - y) >= 2))
|
||||
await travel(tabId, tab, { x, y }, params, send)
|
||||
if (type === "mousePressed") {
|
||||
// People settle on a target for a moment before pressing.
|
||||
await sleep(random(45, 130) - (Date.now() - tab.arrivedAt))
|
||||
tab.pressedAt = Date.now()
|
||||
}
|
||||
if (type === "mouseReleased" && tab.pressedAt !== undefined) {
|
||||
await sleep(random(60, 140) - (Date.now() - tab.pressedAt))
|
||||
delete tab.pressedAt
|
||||
}
|
||||
tab.pointer = { x, y }
|
||||
}
|
||||
|
||||
/** Sends the curved movement from the pointer's last position to `to`, ending just short of it. */
|
||||
async function travel(tabId: number, tab: TabState, to: Point, params: JsonObject, send: Send) {
|
||||
// The first movement on a page starts from somewhere plausible rather than the corner.
|
||||
const from = tab.pointer ?? { x: to.x + random(-260, 260), y: to.y + random(-180, 180) }
|
||||
const path = trajectory(from, to)
|
||||
const started = Date.now()
|
||||
for (const { point, delay } of path) {
|
||||
// Each sample waits for the last to land so they don't arrive in bursts; the round trip counts toward the
|
||||
// gap so it doesn't slow the hand down.
|
||||
const sent = Date.now()
|
||||
await send("Input.dispatchMouseEvent", {
|
||||
type: "mouseMoved",
|
||||
x: point.x,
|
||||
y: point.y,
|
||||
...(typeof params.button === "string" && params.type === "mouseMoved" ? { button: params.button } : {}),
|
||||
...(typeof params.buttons === "number" ? { buttons: params.buttons } : {}),
|
||||
...(typeof params.modifiers === "number" ? { modifiers: params.modifiers } : {}),
|
||||
}).catch(() => undefined)
|
||||
await sleep(delay - (Date.now() - sent))
|
||||
}
|
||||
tab.arrivedAt = Date.now()
|
||||
if (path.length)
|
||||
relayLog("input.move", { tabId, type: params.type, from: `${Math.round(from.x)},${Math.round(from.y)}`, to: `${Math.round(to.x)},${Math.round(to.y)}`, points: path.length, ms: tab.arrivedAt - started })
|
||||
}
|
||||
|
||||
/**
|
||||
* Playwright scrolls an element into view before acting on it, which jumps the page in one frame. When the
|
||||
* element is off screen, scroll toward it with the wheel first, in flicks the way a person would; the command
|
||||
* itself then only fine-tunes (or does everything, when the wheel can't move whatever holds the element).
|
||||
*/
|
||||
async function scrollToward(tabId: number, tab: TabState, params: JsonObject, send: Send) {
|
||||
const node = {
|
||||
...(params.objectId === undefined ? {} : { objectId: params.objectId }),
|
||||
...(params.nodeId === undefined ? {} : { nodeId: params.nodeId }),
|
||||
...(params.backendNodeId === undefined ? {} : { backendNodeId: params.backendNodeId }),
|
||||
}
|
||||
const metrics = (await send("Page.getLayoutMetrics", {})) as { cssVisualViewport?: { clientWidth: number; clientHeight: number } }
|
||||
const viewport = metrics.cssVisualViewport
|
||||
if (!viewport) return
|
||||
const started = Date.now()
|
||||
const offset = async () => {
|
||||
const result = (await send("DOM.getContentQuads", node)) as { quads?: number[][] }
|
||||
const quad = result.quads?.[0]
|
||||
if (!quad) return undefined
|
||||
const ys = [quad[1]!, quad[3]!, quad[5]!, quad[7]!]
|
||||
if (Math.min(...ys) >= 0 && Math.max(...ys) <= viewport.clientHeight) return 0
|
||||
return (Math.min(...ys) + Math.max(...ys)) / 2 - viewport.clientHeight * random(0.35, 0.5)
|
||||
}
|
||||
const first = await offset()
|
||||
if (!first) return
|
||||
// The wheel scrolls whatever is under the pointer, so it rests over the page first.
|
||||
if (!tab.pointer) {
|
||||
const rest = { x: viewport.clientWidth * random(0.35, 0.65), y: viewport.clientHeight * random(0.35, 0.65) }
|
||||
await travel(tabId, tab, rest, { type: "mouseMoved" }, send)
|
||||
tab.pointer = rest
|
||||
}
|
||||
const pointer = tab.pointer
|
||||
let remaining = first
|
||||
let scrolled = 0
|
||||
for (let flick = 0; flick < 12 && Math.abs(remaining) > 2; flick++) {
|
||||
const amount = Math.sign(remaining) * Math.min(Math.abs(remaining), random(450, 900))
|
||||
await sendFrames(glide(0, amount), (frame) =>
|
||||
send("Input.dispatchMouseEvent", { type: "mouseWheel", x: pointer.x, y: pointer.y, deltaX: frame.x, deltaY: frame.y }),
|
||||
)
|
||||
scrolled += amount
|
||||
const next = await offset()
|
||||
// Stop when the wheel didn't move the element (the page can't scroll further, or something else holds it).
|
||||
if (next === undefined || Math.abs(next - remaining) < 1) break
|
||||
remaining = next
|
||||
if (Math.abs(remaining) > 2) await sleep(random(90, 240))
|
||||
}
|
||||
relayLog("input.scroll", { tabId, distance: Math.round(first), scrolled: Math.round(scrolled), ms: Date.now() - started })
|
||||
}
|
||||
|
||||
/** Per-frame wheel deltas for one scroll gesture: a quick start that eases out, at ~60 Hz. */
|
||||
function glide(dx: number, dy: number): Point[] {
|
||||
const distance = Math.hypot(dx, dy)
|
||||
if (distance < 40) return [{ x: dx, y: dy }]
|
||||
const frames = Math.max(4, Math.round((Math.min(650, 140 + distance * 0.45) * random(0.85, 1.15)) / 16.7))
|
||||
return Array.from({ length: frames }, (_, i) => {
|
||||
const ease = (t: number) => 1 - (1 - t) ** 3
|
||||
return { x: dx * (ease((i + 1) / frames) - ease(i / frames)), y: dy * (ease((i + 1) / frames) - ease(i / frames)) }
|
||||
})
|
||||
}
|
||||
|
||||
async function sendFrames(frames: Point[], send: (frame: Point) => Promise<unknown>) {
|
||||
for (const frame of frames) {
|
||||
const sent = Date.now()
|
||||
await send({ x: Math.round(frame.x * 100) / 100, y: Math.round(frame.y * 100) / 100 }).catch(() => undefined)
|
||||
await sleep(random(15.5, 18) - (Date.now() - sent))
|
||||
}
|
||||
}
|
||||
|
||||
async function paceKey(tab: TabState, params: JsonObject) {
|
||||
if (params.type !== "keyDown" && params.type !== "rawKeyDown") return
|
||||
const gap = random(30, 110) - (Date.now() - tab.lastKeyAt)
|
||||
if (gap > 0 && tab.lastKeyAt > 0 && Date.now() - tab.lastKeyAt < 1000) await sleep(gap)
|
||||
tab.lastKeyAt = Date.now()
|
||||
}
|
||||
3
packages/browser-extension/cli/src/index.ts
Normal file
3
packages/browser-extension/cli/src/index.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export * as AuthenticatedOrigin from "./opencode-browser-client.ts"
|
||||
export * as OpenCodeBrowserClient from "./opencode-browser-client.ts"
|
||||
export * as SecretProfile from "./secret-profile.ts"
|
||||
37
packages/browser-extension/cli/src/main.ts
Normal file
37
packages/browser-extension/cli/src/main.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env node
|
||||
// opencode-browser: one command for OpenCode Browser.
|
||||
// Setup (setup.ts): install, extension, uninstall.
|
||||
// Driving the browser (cli.ts): serve, relay, execute, session, network, secrets, recording, journal,
|
||||
// doctor, skill, mcp, status. The relay is the local server agents use; the extension connects to it.
|
||||
// `opencode-browser-mcp` (or `opencode-browser mcp`) runs the MCP server over stdio.
|
||||
import path from "node:path"
|
||||
|
||||
const setupCommands = new Set(["install", "uninstall", "extension"])
|
||||
const command = process.argv[2]
|
||||
const invokedAs = path.basename(process.argv[1] ?? "")
|
||||
|
||||
if (invokedAs === "opencode-browser-mcp") {
|
||||
const { NodeRuntime } = await import("@effect/platform-node")
|
||||
const { runMcpServer } = await import("./mcp.ts")
|
||||
runMcpServer.pipe(NodeRuntime.runMain)
|
||||
} else if (command && setupCommands.has(command)) {
|
||||
const { runSetup } = await import("./setup.ts")
|
||||
await runSetup(command, process.argv.slice(3))
|
||||
} else {
|
||||
if (!command || command === "--help" || command === "-h") {
|
||||
process.stdout.write(`SETUP
|
||||
install [--opencode <path>] Install OpenCode Browser: add the browse MCP server to opencode, copy
|
||||
the extension, and check which browsers have it (opening its Chrome
|
||||
Web Store listing when none does)
|
||||
--no-open Print the store link instead of opening it
|
||||
extension Open the unpacked extension folder (for "Load unpacked")
|
||||
uninstall Remove everything install set up
|
||||
|
||||
`)
|
||||
}
|
||||
if (command === "status") {
|
||||
const { printSetupStatus } = await import("./setup.ts")
|
||||
printSetupStatus()
|
||||
}
|
||||
await import("./cli.ts")
|
||||
}
|
||||
5
packages/browser-extension/cli/src/mcp-main.ts
Normal file
5
packages/browser-extension/cli/src/mcp-main.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#!/usr/bin/env node
|
||||
import { NodeRuntime } from "@effect/platform-node"
|
||||
import { runMcpServer } from "./mcp.ts"
|
||||
|
||||
runMcpServer.pipe(NodeRuntime.runMain)
|
||||
727
packages/browser-extension/cli/src/mcp.ts
Normal file
727
packages/browser-extension/cli/src/mcp.ts
Normal file
|
|
@ -0,0 +1,727 @@
|
|||
import { NodeStdio } from "@effect/platform-node"
|
||||
import { Config, Context, Effect, Layer, Option, Predicate } from "effect"
|
||||
import { McpProtocol, McpSchema, McpServer } from "effect/ai"
|
||||
import fs from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import { fileURLToPath } from "node:url"
|
||||
import type { JsonObject } from "./protocol.ts"
|
||||
import { getObject, getString, parseTargetSelection } from "./relay-helpers.ts"
|
||||
import * as RelayClient from "./relay-client.ts"
|
||||
import * as RelayLifecycle from "./relay-lifecycle.ts"
|
||||
import type { TargetSelection } from "./relay-schema.ts"
|
||||
import { opencodeBrowserVersion } from "./version.ts"
|
||||
|
||||
const packageRoot = path.dirname(path.dirname(fileURLToPath(import.meta.url)))
|
||||
type CurrentSession = { id: string; established: boolean }
|
||||
|
||||
type ToolSpec = {
|
||||
readonly name: string
|
||||
readonly description: string
|
||||
readonly inputSchema: JsonObject
|
||||
readonly readOnly: boolean
|
||||
readonly destructive: boolean
|
||||
readonly idempotent: boolean
|
||||
readonly handle: (input: unknown) => Effect.Effect<unknown, Error>
|
||||
}
|
||||
|
||||
type ExecuteArguments = {
|
||||
readonly code: string
|
||||
readonly session?: string | undefined
|
||||
readonly targetSelection?: TargetSelection
|
||||
}
|
||||
|
||||
type AdoptArguments = {
|
||||
readonly session?: string | undefined
|
||||
readonly targetSelection?: TargetSelection
|
||||
readonly tabId?: number
|
||||
}
|
||||
|
||||
const emptyInputSchema = objectSchema({})
|
||||
const sessionSchemaProperty = {
|
||||
type: "string",
|
||||
description: "Optional session id. Defaults to this MCP server's current session.",
|
||||
} as const
|
||||
const sessionOnlyInputSchema = objectSchema({
|
||||
session: sessionSchemaProperty,
|
||||
})
|
||||
|
||||
function makeToolSpecs(relay: RelayClient.Interface, currentSession: CurrentSession): readonly ToolSpec[] {
|
||||
const resolveSessionId = (input: unknown, field = "session") => optionalStringField(input, field) ?? currentSession.id
|
||||
const establishCurrentSession = (id: string) => {
|
||||
currentSession.id = id
|
||||
currentSession.established = true
|
||||
}
|
||||
return [
|
||||
{
|
||||
name: "execute",
|
||||
description: "Execute trusted Playwright JavaScript against the OpenCode Browser session. The result includes console logs, warnings, a bounded execution-context diagnostic when relevant, and an aftermath summary (URL movement, navigations, error counts, handoffs).",
|
||||
inputSchema: objectSchema({
|
||||
code: { type: "string", description: "JavaScript code to execute. It receives browser, context, page, state, modules, fillInput, fillInputs, snapshot(options?) for compact semantic outlines, search, explicit diffs, or automatic deltas, persistent compatible ref(id) locators, webmcp (list/call), screenshot helpers, ariaSnapshot, ghostCursor, handoff, demonstrate, network capture, and pageConsole (start/stop capturing the page's own console messages and errors, off by default because pages can detect it)." },
|
||||
session: { type: "string", description: "Optional existing OpenCode Browser session id. Explicit ids must already exist; omit this field to use the MCP server's current session, which is created when needed." },
|
||||
targetUrl: { type: "string", description: "Optional URL substring selecting an existing attached page. This does not navigate or open a URL; use page.goto() for that." },
|
||||
targetIndex: { type: "integer", minimum: 0, description: "Optional zero-based attached page index selector." },
|
||||
}, ["code"]),
|
||||
readOnly: false,
|
||||
destructive: true,
|
||||
idempotent: false,
|
||||
handle: (input) => Effect.gen(function* () {
|
||||
const args = yield* Effect.try(() => parseExecuteArguments(input))
|
||||
yield* RelayLifecycle.ensureExtensionConnected({ relay, waitForReconnect: true })
|
||||
const sessionId = args.session ?? currentSession.id
|
||||
const result = yield* relay.execute({
|
||||
sessionId,
|
||||
code: args.code,
|
||||
createIfMissing: !args.session,
|
||||
...(args.targetSelection ? { targetSelection: args.targetSelection } : {}),
|
||||
})
|
||||
const recreated = !args.session && currentSession.established && result.session.created === true
|
||||
establishCurrentSession(sessionId)
|
||||
return {
|
||||
...result,
|
||||
...(recreated ? { notice: `Recreated session '${sessionId}' — relay had no such session; page and state were reset.` } : {}),
|
||||
}
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "status",
|
||||
description: "Return relay, extension, target, and session status.",
|
||||
inputSchema: emptyInputSchema,
|
||||
readOnly: true,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: () => Effect.gen(function* () {
|
||||
const [version, status] = yield* Effect.all([relay.version, relay.extensionStatus])
|
||||
const buildProblem = RelayLifecycle.relayBuildProblem(version)
|
||||
return { endpoint: relay.endpoint, currentSession: currentSession.id, version, status, ...(buildProblem ? { buildProblem } : {}) }
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "session_new",
|
||||
description: "Create a OpenCode Browser session and make it current for this MCP server.",
|
||||
inputSchema: objectSchema({
|
||||
id: { type: "string", description: "Optional lowercase session id." },
|
||||
readOnly: { type: "boolean", description: "Create a read-only session: the relay rejects input-dispatching CDP so scripts can inspect but not click or type." },
|
||||
}),
|
||||
readOnly: false,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: (input) => Effect.gen(function* () {
|
||||
const requestedId = optionalStringField(input, "id")
|
||||
const readOnly = optionalBooleanField(input, "readOnly")
|
||||
const session = yield* relay.sessionNew(requestedId, readOnly ? { readOnly: true } : {})
|
||||
establishCurrentSession(session.id)
|
||||
return { session }
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "session_list",
|
||||
description: "List OpenCode Browser sessions.",
|
||||
inputSchema: emptyInputSchema,
|
||||
readOnly: true,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: () => relay.sessions.pipe(Effect.map((sessions) => ({ sessions }))),
|
||||
},
|
||||
{
|
||||
name: "session_current",
|
||||
description: "Return this MCP server's current OpenCode Browser session id.",
|
||||
inputSchema: emptyInputSchema,
|
||||
readOnly: true,
|
||||
destructive: false,
|
||||
idempotent: true,
|
||||
handle: () => Effect.succeed({ currentSession: currentSession.id }),
|
||||
},
|
||||
{
|
||||
name: "session_use",
|
||||
description: "Set this MCP server's current OpenCode Browser session id.",
|
||||
inputSchema: objectSchema({
|
||||
id: { type: "string", description: "Existing OpenCode Browser session id." },
|
||||
}, ["id"]),
|
||||
readOnly: false,
|
||||
destructive: false,
|
||||
idempotent: true,
|
||||
handle: (input) => Effect.gen(function* () {
|
||||
const id = yield* Effect.try(() => requiredStringField(input, "id"))
|
||||
yield* ensureSessionExists(relay, id)
|
||||
establishCurrentSession(id)
|
||||
return { currentSession: currentSession.id }
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "session_reset",
|
||||
description: "Reset a OpenCode Browser session's state and page.",
|
||||
inputSchema: objectSchema({
|
||||
id: sessionSchemaProperty,
|
||||
}),
|
||||
readOnly: false,
|
||||
destructive: true,
|
||||
idempotent: false,
|
||||
handle: (input) => Effect.gen(function* () {
|
||||
const id = resolveSessionId(input, "id")
|
||||
const session = yield* relay.sessionReset(id)
|
||||
establishCurrentSession(id)
|
||||
return { session }
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "session_delete",
|
||||
description: "Delete a OpenCode Browser session.",
|
||||
inputSchema: objectSchema({
|
||||
id: sessionSchemaProperty,
|
||||
}),
|
||||
readOnly: false,
|
||||
destructive: true,
|
||||
idempotent: true,
|
||||
handle: (input) => Effect.gen(function* () {
|
||||
const id = resolveSessionId(input, "id")
|
||||
const result = yield* relay.sessionDelete(id)
|
||||
if (currentSession.id === id) {
|
||||
currentSession.id = `mcp-${crypto.randomUUID().slice(0, 8)}`
|
||||
currentSession.established = false
|
||||
}
|
||||
return { ...result, currentSession: currentSession.id }
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "session_adopt",
|
||||
description: "Make an attached tab the OpenCode Browser session's default page for subsequent bare execute calls. Omit targetUrl and targetIndex when only one user-attached tab is available.",
|
||||
inputSchema: objectSchema({
|
||||
session: { type: "string", description: "Optional existing OpenCode Browser session id. Explicit ids must already exist; omit this field to use the MCP server's current session, which is created when needed." },
|
||||
targetUrl: { type: "string", description: "Adopt an existing attached page whose URL contains this text. Omit when only one user-attached tab is available. This does not navigate or open a URL." },
|
||||
targetIndex: { type: "integer", minimum: 0, description: "Adopt the attached page at this zero-based target index." },
|
||||
tabId: { type: "integer", description: "Adopt the user's tab with this Chrome tab id (the tab= value in status)." },
|
||||
}),
|
||||
readOnly: false,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: (input) => Effect.gen(function* () {
|
||||
const args = yield* Effect.try(() => parseAdoptArguments(input))
|
||||
const sessionId = args.session ?? currentSession.id
|
||||
const result = yield* relay.sessionAdopt({
|
||||
sessionId,
|
||||
createIfMissing: !args.session,
|
||||
...(args.targetSelection ? { targetSelection: args.targetSelection } : {}),
|
||||
...(args.tabId === undefined ? {} : { tabId: args.tabId }),
|
||||
})
|
||||
establishCurrentSession(sessionId)
|
||||
return { ...result, confirmation: `Adopted session '${result.session.id}' default page: ${result.adoptedUrl}` }
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "network_start",
|
||||
description: "Start session-scoped network capture. OpenCode Browser records normalized Playwright exchanges; HAR is only the optional export format. Bodies are embedded by default with per-body and total memory limits.",
|
||||
inputSchema: objectSchema({
|
||||
session: { type: "string", description: "Optional existing session id. Omit to use or create this MCP server's current session." },
|
||||
urlFilter: { type: "string", description: "Capture only request URLs containing this text." },
|
||||
resourceTypes: { type: "array", items: { type: "string" }, description: "Optional Playwright resource types such as fetch and xhr." },
|
||||
content: { type: "string", enum: ["embed", "omit"], description: "Request and response body mode. Defaults to embed." },
|
||||
maxBodyBytes: { type: "integer", minimum: 1, description: "Maximum bytes captured from each body. Defaults to 1000000." },
|
||||
maxTotalBodyBytes: { type: "integer", minimum: 1, description: "Maximum body bytes retained for the capture. Defaults to 25000000." },
|
||||
maxEntries: { type: "integer", minimum: 1, description: "Maximum captured requests. Defaults to 1000." },
|
||||
}),
|
||||
readOnly: false,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: (input) => Effect.gen(function* () {
|
||||
const object = requireObject(input)
|
||||
const explicitSession = optionalStringField(object, "session")
|
||||
const sessionId = explicitSession ?? currentSession.id
|
||||
if (explicitSession) {
|
||||
yield* ensureSessionExists(relay, sessionId)
|
||||
} else {
|
||||
yield* relay.sessionEnsure(sessionId)
|
||||
currentSession.established = true
|
||||
}
|
||||
yield* RelayLifecycle.ensureExtensionConnected({ relay, waitForReconnect: true })
|
||||
const content = optionalStringField(object, "content")
|
||||
if (content !== undefined && content !== "embed" && content !== "omit") {
|
||||
return yield* Effect.fail(new Error("content must be embed or omit"))
|
||||
}
|
||||
const urlFilter = optionalStringField(object, "urlFilter")
|
||||
const resourceTypes = optionalStringArrayField(object, "resourceTypes")
|
||||
const maxBodyBytes = optionalPositiveIntegerField(object, "maxBodyBytes")
|
||||
const maxTotalBodyBytes = optionalPositiveIntegerField(object, "maxTotalBodyBytes")
|
||||
const maxEntries = optionalPositiveIntegerField(object, "maxEntries")
|
||||
const result = yield* relay.networkStart({
|
||||
sessionId,
|
||||
...(urlFilter ? { urlFilter } : {}),
|
||||
...(resourceTypes && resourceTypes.length > 0 ? { resourceTypes } : {}),
|
||||
...(content ? { content } : {}),
|
||||
...(maxBodyBytes === undefined ? {} : { maxBodyBytes }),
|
||||
...(maxTotalBodyBytes === undefined ? {} : { maxTotalBodyBytes }),
|
||||
...(maxEntries === undefined ? {} : { maxEntries }),
|
||||
})
|
||||
return { session: sessionId, ...result }
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "network_status",
|
||||
description: "Return bounded metadata for a session's active network capture. Captured values are never included.",
|
||||
inputSchema: sessionOnlyInputSchema,
|
||||
readOnly: true,
|
||||
destructive: false,
|
||||
idempotent: true,
|
||||
handle: (input) => {
|
||||
const sessionId = resolveSessionId(input)
|
||||
return relay.networkStatus({ sessionId }).pipe(Effect.map((result) => ({ session: sessionId, ...result })))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "network_stop",
|
||||
description: "Stop network capture. Optionally write a credential-redacted HAR and store lossless credential values in a reusable secret profile. At least one of outputPath or secrets is required.",
|
||||
inputSchema: objectSchema({
|
||||
session: sessionSchemaProperty,
|
||||
outputPath: { type: "string", description: "Optional artifact path, resolved against the MCP process working directory. The HAR contains stable ${BC_SECRET_N} references, not captured values." },
|
||||
secrets: { type: "string", description: "Optional reusable profile name for captured credential values." },
|
||||
}),
|
||||
readOnly: false,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: (input) => Effect.gen(function* () {
|
||||
const sessionId = resolveSessionId(input)
|
||||
const outputPath = optionalStringField(input, "outputPath")
|
||||
const secrets = optionalStringField(input, "secrets")
|
||||
if (!outputPath && !secrets) {
|
||||
return yield* Effect.fail(new Error("network_stop requires outputPath, secrets, or both"))
|
||||
}
|
||||
return yield* relay.networkStop({
|
||||
sessionId,
|
||||
...(outputPath ? { outputPath: path.resolve(outputPath) } : {}),
|
||||
...(secrets ? { secrets } : {}),
|
||||
})
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "network_cancel",
|
||||
description: "Cancel a session's network capture and discard its in-memory exchanges.",
|
||||
inputSchema: sessionOnlyInputSchema,
|
||||
readOnly: false,
|
||||
destructive: true,
|
||||
idempotent: true,
|
||||
handle: (input) => relay.networkCancel({ sessionId: resolveSessionId(input) }),
|
||||
},
|
||||
{
|
||||
name: "recording_start",
|
||||
description: "Start recording the current session tab. CDP mode records video to WebM or MP4; tab-capture mode supports WebM and optional audio.",
|
||||
inputSchema: objectSchema({
|
||||
session: sessionSchemaProperty,
|
||||
outputPath: { type: "string", description: "Recording artifact path, resolved against the MCP process working directory." },
|
||||
mode: { type: "string", enum: ["auto", "tab-capture", "cdp"], description: "Recording backend. Defaults to auto." },
|
||||
audio: { type: "boolean", description: "Capture tab audio in tab-capture mode." },
|
||||
frameRate: { type: "integer", minimum: 1, maximum: 60, description: "Requested frame rate." },
|
||||
maxDurationMs: { type: "integer", minimum: 1, description: "Maximum recording duration in milliseconds." },
|
||||
}, ["outputPath"]),
|
||||
readOnly: false,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: (input) => {
|
||||
const object = requireObject(input)
|
||||
const mode = optionalStringField(object, "mode")
|
||||
if (mode !== undefined && mode !== "auto" && mode !== "tab-capture" && mode !== "cdp") {
|
||||
return Effect.fail(new Error("mode must be auto, tab-capture, or cdp"))
|
||||
}
|
||||
const frameRate = optionalPositiveIntegerField(object, "frameRate")
|
||||
if (frameRate !== undefined && frameRate > 60) return Effect.fail(new Error("frameRate must be at most 60"))
|
||||
const maxDurationMs = optionalPositiveIntegerField(object, "maxDurationMs")
|
||||
const audio = optionalBooleanField(object, "audio")
|
||||
return relay.recordingStart({
|
||||
sessionId: resolveSessionId(object),
|
||||
outputPath: path.resolve(requiredStringField(object, "outputPath")),
|
||||
...(mode ? { mode } : {}),
|
||||
...(audio === undefined ? {} : { audio }),
|
||||
...(frameRate === undefined ? {} : { frameRate }),
|
||||
...(maxDurationMs === undefined ? {} : { maxDurationMs }),
|
||||
})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "recording_stop",
|
||||
description: "Stop the active recording for a session and finalize its artifact.",
|
||||
inputSchema: sessionOnlyInputSchema,
|
||||
readOnly: false,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: (input) => relay.recordingStop({ sessionId: resolveSessionId(input) }),
|
||||
},
|
||||
{
|
||||
name: "tabs_cleanup",
|
||||
description: "Close tabs agents opened that nobody has used for a while (no agent command and not visited by the user). Tabs the user shared or pinned, the active tab, tabs playing audio, recording, or waiting for a handoff stay open. Call it when you finish a task to leave the user's browser tidy; OpenCode Browser also does this automatically after 10 idle minutes.",
|
||||
inputSchema: objectSchema({
|
||||
idleMinutes: { type: "integer", minimum: 0, description: "Close tabs idle at least this long. 0 closes every eligible agent tab. Defaults to the user's setting (10)." },
|
||||
}),
|
||||
readOnly: false,
|
||||
destructive: true,
|
||||
idempotent: true,
|
||||
handle: (input) => {
|
||||
const value = typeof input === "object" && input !== null && "idleMinutes" in input ? input.idleMinutes : undefined
|
||||
return relay.tabsCleanup(typeof value === "number" && value >= 0 ? value : undefined)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "recording_status",
|
||||
description: "Return bounded status and quality counters for a session recording.",
|
||||
inputSchema: sessionOnlyInputSchema,
|
||||
readOnly: true,
|
||||
destructive: false,
|
||||
idempotent: true,
|
||||
handle: (input) => relay.recordingStatus({ sessionId: resolveSessionId(input) }),
|
||||
},
|
||||
{
|
||||
name: "recording_cancel",
|
||||
description: "Cancel a session recording and discard its unfinished artifact.",
|
||||
inputSchema: sessionOnlyInputSchema,
|
||||
readOnly: false,
|
||||
destructive: true,
|
||||
idempotent: true,
|
||||
handle: (input) => relay.recordingCancel({ sessionId: resolveSessionId(input) }),
|
||||
},
|
||||
{
|
||||
name: "flight_recorder_start",
|
||||
description: "Start a rolling in-memory video buffer for the current session tab. Saving a clip does not stop buffering.",
|
||||
inputSchema: objectSchema({
|
||||
session: sessionSchemaProperty,
|
||||
retentionMs: { type: "integer", minimum: 1000, maximum: 120000, description: "Rolling retention window. Defaults to 60000." },
|
||||
frameRate: { type: "integer", minimum: 1, maximum: 60, description: "Saved clip frame rate. Defaults to 60." },
|
||||
}),
|
||||
readOnly: false,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: (input) => {
|
||||
const object = requireObject(input)
|
||||
const retentionMs = optionalPositiveIntegerField(object, "retentionMs")
|
||||
const frameRate = optionalPositiveIntegerField(object, "frameRate")
|
||||
return relay.flightRecorderStart({
|
||||
sessionId: resolveSessionId(object),
|
||||
...(retentionMs === undefined ? {} : { retentionMs }),
|
||||
...(frameRate === undefined ? {} : { frameRate }),
|
||||
})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "flight_recorder_status",
|
||||
description: "Return bounded rolling-buffer duration, frame, byte, and drop counters.",
|
||||
inputSchema: sessionOnlyInputSchema,
|
||||
readOnly: true,
|
||||
destructive: false,
|
||||
idempotent: true,
|
||||
handle: (input) => relay.flightRecorderStatus({ sessionId: resolveSessionId(input) }),
|
||||
},
|
||||
{
|
||||
name: "flight_recorder_save_last",
|
||||
description: "Encode and save the most recent buffered browser video without stopping the flight recorder.",
|
||||
inputSchema: objectSchema({
|
||||
session: sessionSchemaProperty,
|
||||
outputPath: { type: "string", description: "Fresh .webm or .mp4 path, resolved against the MCP process working directory." },
|
||||
durationMs: { type: "integer", minimum: 1, description: "Recent duration to save. Defaults to 30000 and cannot exceed retention." },
|
||||
}, ["outputPath"]),
|
||||
readOnly: false,
|
||||
destructive: false,
|
||||
idempotent: false,
|
||||
handle: (input) => {
|
||||
const object = requireObject(input)
|
||||
const durationMs = optionalPositiveIntegerField(object, "durationMs")
|
||||
return relay.flightRecorderSaveLast({
|
||||
sessionId: resolveSessionId(object),
|
||||
outputPath: path.resolve(requiredStringField(object, "outputPath")),
|
||||
...(durationMs === undefined ? {} : { durationMs }),
|
||||
})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "flight_recorder_cancel",
|
||||
description: "Stop and discard a session's rolling video buffer.",
|
||||
inputSchema: sessionOnlyInputSchema,
|
||||
readOnly: false,
|
||||
destructive: true,
|
||||
idempotent: true,
|
||||
handle: (input) => relay.flightRecorderCancel({ sessionId: resolveSessionId(input) }),
|
||||
},
|
||||
{
|
||||
name: "secrets_status",
|
||||
description: "Return secret profile references, sources, and expiration metadata without revealing credential values.",
|
||||
inputSchema: objectSchema({ name: { type: "string", description: "Secret profile name." } }, ["name"]),
|
||||
readOnly: true,
|
||||
destructive: false,
|
||||
idempotent: true,
|
||||
handle: (input) => relay.authStatus({ name: requiredStringField(input, "name") }),
|
||||
},
|
||||
{
|
||||
name: "secrets_refresh",
|
||||
description: "Reload a session page, observe fresh credentials, and update a secret profile while preserving stable BC_SECRET_N references.",
|
||||
inputSchema: objectSchema({
|
||||
name: { type: "string", description: "Existing secret profile name." },
|
||||
session: sessionSchemaProperty,
|
||||
urlFilter: { type: "string", description: "Observe credentials only on matching request URLs." },
|
||||
timeoutMs: { type: "integer", minimum: 1, description: "Reload timeout. Defaults to 30000." },
|
||||
}, ["name"]),
|
||||
readOnly: false,
|
||||
destructive: true,
|
||||
idempotent: false,
|
||||
handle: (input) => {
|
||||
const object = requireObject(input)
|
||||
const timeoutMs = optionalPositiveIntegerField(object, "timeoutMs")
|
||||
const urlFilter = optionalStringField(object, "urlFilter")
|
||||
return relay.authRefresh({
|
||||
sessionId: resolveSessionId(object),
|
||||
name: requiredStringField(object, "name"),
|
||||
...(urlFilter ? { urlFilter } : {}),
|
||||
...(timeoutMs === undefined ? {} : { timeoutMs }),
|
||||
})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "secrets_run",
|
||||
description: "Run a local command with a captured profile injected as BC_SECRET_N environment variables. Known values are replaced with their references in stdout and stderr.",
|
||||
inputSchema: objectSchema({
|
||||
name: { type: "string", description: "Secret profile name." },
|
||||
command: { type: "string", description: "Executable path or command name." },
|
||||
args: { type: "array", items: { type: "string" }, description: "Command arguments." },
|
||||
cwd: { type: "string", description: "Optional child working directory." },
|
||||
timeoutMs: { type: "integer", minimum: 1, description: "Child timeout. Defaults to 120000." },
|
||||
}, ["name", "command"]),
|
||||
readOnly: false,
|
||||
destructive: true,
|
||||
idempotent: false,
|
||||
handle: (input) => {
|
||||
const object = requireObject(input)
|
||||
const args = optionalStringArrayField(object, "args")
|
||||
const cwd = optionalStringField(object, "cwd")
|
||||
const timeoutMs = optionalPositiveIntegerField(object, "timeoutMs")
|
||||
return relay.authRun({
|
||||
name: requiredStringField(object, "name"),
|
||||
command: requiredStringField(object, "command"),
|
||||
...(args ? { args } : {}),
|
||||
cwd: path.resolve(cwd ?? process.cwd()),
|
||||
...(timeoutMs === undefined ? {} : { timeoutMs }),
|
||||
})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "skill",
|
||||
description: "Return the OpenCode Browser agent skill instructions.",
|
||||
inputSchema: emptyInputSchema,
|
||||
readOnly: true,
|
||||
destructive: false,
|
||||
idempotent: true,
|
||||
handle: () => Effect.tryPromise({
|
||||
try: () => fs.readFile(path.join(packageRoot, "skills", "browse", "SKILL.md"), "utf8"),
|
||||
catch: (cause) => new Error("read opencode-browser skill", { cause }),
|
||||
}),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
const registerTools = Effect.gen(function* () {
|
||||
const server = yield* McpServer.McpServer
|
||||
const relay = yield* RelayClient.Service
|
||||
const configuredSession = Option.getOrUndefined(yield* Config.option(Config.String("OPENCODE_BROWSER_SESSION")))
|
||||
const currentSession: CurrentSession = {
|
||||
id: configuredSession || `mcp-${crypto.randomUUID().slice(0, 8)}`,
|
||||
established: Boolean(configuredSession),
|
||||
}
|
||||
yield* Effect.forEach(makeToolSpecs(relay, currentSession), (spec) => {
|
||||
return server.addTool({
|
||||
tool: new McpSchema.Tool({
|
||||
name: spec.name,
|
||||
description: spec.description,
|
||||
inputSchema: spec.inputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: spec.readOnly,
|
||||
destructiveHint: spec.destructive,
|
||||
idempotentHint: spec.idempotent,
|
||||
openWorldHint: true,
|
||||
},
|
||||
}),
|
||||
annotations: Context.empty(),
|
||||
handle: (payload: unknown) => {
|
||||
const operation = mcpToolRequiresRelayCompatibility(spec.name)
|
||||
? RelayLifecycle.ensureRelay({ relay }).pipe(
|
||||
Effect.flatMap((readiness) => readiness.buildProblem
|
||||
? Effect.fail(new Error(readiness.buildProblem))
|
||||
: spec.handle(payload)),
|
||||
)
|
||||
: spec.handle(payload)
|
||||
return operation.pipe(
|
||||
Effect.match({
|
||||
onFailure: (error) => toolResult({ text: mcpErrorMessage(spec.name, error.message), isError: true }),
|
||||
onSuccess: (value) => toolResultForValue(value),
|
||||
}),
|
||||
)
|
||||
},
|
||||
})
|
||||
}, { discard: true })
|
||||
})
|
||||
|
||||
export function mcpToolRequiresRelayCompatibility(name: string): boolean {
|
||||
return !["status", "session_list", "session_current", "network_status", "recording_status", "flight_recorder_status", "secrets_status", "skill"].includes(name)
|
||||
}
|
||||
|
||||
export const mcpServerLayer = McpServer.layerStdio({
|
||||
name: "browse",
|
||||
version: opencodeBrowserVersion,
|
||||
protocols: [McpProtocol.v2025_06_18, McpProtocol.v2025_11_25, McpProtocol.v2025_03_26, McpProtocol.v2024_11_05],
|
||||
})
|
||||
|
||||
export const mcpToolsLayer = Layer.effectDiscard(registerTools)
|
||||
|
||||
export const runMcpServer: Effect.Effect<never, Error> = Layer.launch(
|
||||
mcpToolsLayer.pipe(
|
||||
Layer.provide(mcpServerLayer),
|
||||
Layer.provide(NodeStdio.layer),
|
||||
Layer.provide(RelayClient.layerFetch),
|
||||
),
|
||||
)
|
||||
|
||||
const ensureSessionExists = Effect.fnUntraced(function* (relay: RelayClient.Interface, id: string) {
|
||||
const sessions = yield* relay.sessions
|
||||
const exists = sessions.some((session) => {
|
||||
return session.id === id
|
||||
})
|
||||
if (!exists) {
|
||||
return yield* Effect.fail(new Error(`Session not found: ${id}`))
|
||||
}
|
||||
})
|
||||
|
||||
function parseExecuteArguments(input: unknown): ExecuteArguments {
|
||||
const object = requireObject(input)
|
||||
const code = requiredStringField(object, "code")
|
||||
const session = optionalStringField(object, "session")
|
||||
const targetSelection = parseMcpTargetSelection(object)
|
||||
return {
|
||||
code,
|
||||
...(session ? { session } : {}),
|
||||
...(targetSelection ? { targetSelection } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
function parseAdoptArguments(input: unknown): AdoptArguments {
|
||||
const object = requireObject(input)
|
||||
const session = optionalStringField(object, "session")
|
||||
const targetSelection = parseMcpTargetSelection(object)
|
||||
const tabId = typeof object.tabId === "number" && Number.isInteger(object.tabId) ? object.tabId : undefined
|
||||
if (tabId !== undefined && targetSelection) throw new Error("Use only one of tabId, targetUrl, or targetIndex")
|
||||
return {
|
||||
...(session ? { session } : {}),
|
||||
...(targetSelection ? { targetSelection } : {}),
|
||||
...(tabId === undefined ? {} : { tabId }),
|
||||
}
|
||||
}
|
||||
|
||||
function parseMcpTargetSelection(input: JsonObject): TargetSelection | undefined {
|
||||
const urlIncludes = optionalStringField(input, "targetUrl")
|
||||
const selection = parseTargetSelection({
|
||||
...(urlIncludes ? { urlIncludes } : {}),
|
||||
...(input.targetIndex === undefined ? {} : { index: input.targetIndex }),
|
||||
})
|
||||
return selection?.urlIncludes || selection?.index !== undefined ? selection : undefined
|
||||
}
|
||||
|
||||
function requiredStringField(input: unknown, field: string): string {
|
||||
const value = optionalStringField(input, field)
|
||||
if (!value) {
|
||||
throw new Error(`${field} is required`)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function optionalStringField(input: unknown, field: string): string | undefined {
|
||||
const value = getString(requireObject(input), field)
|
||||
return value ? value : undefined
|
||||
}
|
||||
|
||||
function optionalBooleanField(input: unknown, field: string): boolean | undefined {
|
||||
const value = requireObject(input)[field]
|
||||
return Predicate.isBoolean(value) ? value : undefined
|
||||
}
|
||||
|
||||
function optionalPositiveIntegerField(input: unknown, field: string): number | undefined {
|
||||
const value = requireObject(input)[field]
|
||||
if (value === undefined) return undefined
|
||||
if (!Predicate.isNumber(value) || !Number.isInteger(value) || value <= 0) {
|
||||
throw new Error(`${field} must be a positive integer`)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function optionalStringArrayField(input: unknown, field: string): readonly string[] | undefined {
|
||||
const value = requireObject(input)[field]
|
||||
if (value === undefined) return undefined
|
||||
if (!Array.isArray(value) || !value.every((item): item is string => Predicate.isString(item) && item.length > 0)) {
|
||||
throw new Error(`${field} must be an array of non-empty strings`)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function requireObject(input: unknown): JsonObject {
|
||||
const object = getObject(input)
|
||||
if (!object) {
|
||||
throw new Error("Expected arguments object")
|
||||
}
|
||||
return object
|
||||
}
|
||||
|
||||
function stringifyResult(value: unknown): string {
|
||||
if (Predicate.isString(value)) {
|
||||
return value
|
||||
}
|
||||
return JSON.stringify(value, null, 2)
|
||||
}
|
||||
|
||||
export function toolResultForValue(value: unknown): McpSchema.CallToolResult {
|
||||
const object = getObject(value)
|
||||
const isError = object?.isError === true
|
||||
const media = Array.isArray(object?.media)
|
||||
? object.media.flatMap((item) => {
|
||||
const image = getObject(item)
|
||||
const mimeType = getString(image, "mimeType")
|
||||
const data = getString(image, "data")
|
||||
return image?.type === "image" && mimeType !== undefined && data !== undefined
|
||||
? [{ data, mimeType }]
|
||||
: []
|
||||
})
|
||||
: []
|
||||
const errorText = isError ? getString(object, "text") : undefined
|
||||
if (media.length > 0) {
|
||||
const { media: _media, ...structuredContent } = object ?? {}
|
||||
const text = errorText ?? stringifyResult(structuredContent)
|
||||
return new McpSchema.CallToolResult({
|
||||
content: [
|
||||
McpSchema.TextContent.make({ text }),
|
||||
...media.map((image) => McpSchema.ImageContent.make({
|
||||
data: new Uint8Array(Buffer.from(image.data, "base64")),
|
||||
mimeType: image.mimeType,
|
||||
})),
|
||||
],
|
||||
structuredContent,
|
||||
isError,
|
||||
})
|
||||
}
|
||||
const text = errorText ?? stringifyResult(value)
|
||||
return toolResult({ text, ...(object ? { structuredContent: object } : {}), isError })
|
||||
}
|
||||
|
||||
export function mcpErrorMessage(tool: string, message: string): string {
|
||||
if (!message.startsWith("Session not found:")) {
|
||||
return message
|
||||
}
|
||||
return tool === "execute" || tool === "session_adopt"
|
||||
? `${message} Create it with session_new first, or omit the explicit session id to use the MCP current session.`
|
||||
: `${message} Create it with session_new first.`
|
||||
}
|
||||
|
||||
function toolResult(options: { readonly text: string; readonly structuredContent?: unknown; readonly isError: boolean }): McpSchema.CallToolResult {
|
||||
return new McpSchema.CallToolResult({
|
||||
content: [McpSchema.TextContent.make({ text: options.text })],
|
||||
structuredContent: options.structuredContent,
|
||||
isError: options.isError,
|
||||
})
|
||||
}
|
||||
|
||||
function objectSchema(properties: JsonObject, required: readonly string[] = []): JsonObject {
|
||||
return {
|
||||
type: "object",
|
||||
properties,
|
||||
required: [...required],
|
||||
additionalProperties: false,
|
||||
}
|
||||
}
|
||||
118
packages/browser-extension/cli/src/mjpeg-matroska.ts
Normal file
118
packages/browser-extension/cli/src/mjpeg-matroska.ts
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
// Minimal streaming Matroska writer for timestamped MJPEG frames.
|
||||
// References: https://www.matroska.org/technical/elements.html and RFC 8794.
|
||||
|
||||
const ebml = Buffer.from("1A45DFA3", "hex")
|
||||
const ebmlVersion = Buffer.from("4286", "hex")
|
||||
const ebmlReadVersion = Buffer.from("42F7", "hex")
|
||||
const ebmlMaxIdLength = Buffer.from("42F2", "hex")
|
||||
const ebmlMaxSizeLength = Buffer.from("42F3", "hex")
|
||||
const docType = Buffer.from("4282", "hex")
|
||||
const docTypeVersion = Buffer.from("4287", "hex")
|
||||
const docTypeReadVersion = Buffer.from("4285", "hex")
|
||||
const segment = Buffer.from("18538067", "hex")
|
||||
const info = Buffer.from("1549A966", "hex")
|
||||
const timestampScale = Buffer.from("2AD7B1", "hex")
|
||||
const muxingApp = Buffer.from("4D80", "hex")
|
||||
const writingApp = Buffer.from("5741", "hex")
|
||||
const tracks = Buffer.from("1654AE6B", "hex")
|
||||
const trackEntry = Buffer.from("AE", "hex")
|
||||
const trackNumber = Buffer.from("D7", "hex")
|
||||
const trackUid = Buffer.from("73C5", "hex")
|
||||
const trackType = Buffer.from("83", "hex")
|
||||
const flagLacing = Buffer.from("9C", "hex")
|
||||
const codecId = Buffer.from("86", "hex")
|
||||
const video = Buffer.from("E0", "hex")
|
||||
const pixelWidth = Buffer.from("B0", "hex")
|
||||
const pixelHeight = Buffer.from("BA", "hex")
|
||||
const cluster = Buffer.from("1F43B675", "hex")
|
||||
const timestamp = Buffer.from("E7", "hex")
|
||||
const blockGroup = Buffer.from("A0", "hex")
|
||||
const block = Buffer.from("A1", "hex")
|
||||
const blockDuration = Buffer.from("9B", "hex")
|
||||
const unknownSize = Buffer.from([0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
|
||||
|
||||
function variableInteger(value: number): Buffer {
|
||||
let length = 1
|
||||
while (value >= 2 ** (7 * length) - 1) length += 1
|
||||
const buffer = Buffer.alloc(length)
|
||||
let remaining = value
|
||||
for (let index = length - 1; index >= 0; index -= 1) {
|
||||
buffer[index] = remaining & 0xff
|
||||
remaining = Math.floor(remaining / 256)
|
||||
}
|
||||
buffer[0] = (buffer[0] ?? 0) | (1 << (8 - length))
|
||||
return buffer
|
||||
}
|
||||
|
||||
function unsignedInteger(value: number): Buffer {
|
||||
if (value === 0) return Buffer.from([0])
|
||||
const bytes: number[] = []
|
||||
let remaining = value
|
||||
while (remaining > 0) {
|
||||
bytes.unshift(remaining & 0xff)
|
||||
remaining = Math.floor(remaining / 256)
|
||||
}
|
||||
return Buffer.from(bytes)
|
||||
}
|
||||
|
||||
function element(id: Buffer, payload: Buffer): Buffer {
|
||||
return Buffer.concat([id, variableInteger(payload.length), payload])
|
||||
}
|
||||
|
||||
export function mjpegMatroskaHeader(width: number, height: number): Buffer {
|
||||
const ebmlHeader = element(ebml, Buffer.concat([
|
||||
element(ebmlVersion, unsignedInteger(1)),
|
||||
element(ebmlReadVersion, unsignedInteger(1)),
|
||||
element(ebmlMaxIdLength, unsignedInteger(4)),
|
||||
element(ebmlMaxSizeLength, unsignedInteger(8)),
|
||||
element(docType, Buffer.from("matroska")),
|
||||
element(docTypeVersion, unsignedInteger(4)),
|
||||
element(docTypeReadVersion, unsignedInteger(2)),
|
||||
]))
|
||||
const streamInfo = element(info, Buffer.concat([
|
||||
element(timestampScale, unsignedInteger(1_000_000)),
|
||||
element(muxingApp, Buffer.from("opencode-browser")),
|
||||
element(writingApp, Buffer.from("opencode-browser")),
|
||||
]))
|
||||
const track = element(trackEntry, Buffer.concat([
|
||||
element(trackNumber, unsignedInteger(1)),
|
||||
element(trackUid, unsignedInteger(1)),
|
||||
element(trackType, unsignedInteger(1)),
|
||||
element(flagLacing, unsignedInteger(0)),
|
||||
element(codecId, Buffer.from("V_MJPEG")),
|
||||
element(video, Buffer.concat([
|
||||
element(pixelWidth, unsignedInteger(width)),
|
||||
element(pixelHeight, unsignedInteger(height)),
|
||||
])),
|
||||
]))
|
||||
return Buffer.concat([ebmlHeader, segment, unknownSize, streamInfo, element(tracks, track)])
|
||||
}
|
||||
|
||||
export function mjpegMatroskaFrame(timestampMs: number, durationMs: number, frameLength: number): {
|
||||
readonly header: Buffer
|
||||
readonly trailer: Buffer
|
||||
} {
|
||||
const blockHeader = Buffer.concat([
|
||||
block,
|
||||
variableInteger(4 + frameLength),
|
||||
variableInteger(1),
|
||||
Buffer.from([0x00, 0x00]),
|
||||
Buffer.from([0x00]),
|
||||
])
|
||||
const clusterTimestamp = element(timestamp, unsignedInteger(timestampMs))
|
||||
const duration = element(blockDuration, unsignedInteger(Math.max(1, durationMs)))
|
||||
const groupHeader = Buffer.concat([
|
||||
blockGroup,
|
||||
variableInteger(blockHeader.length + frameLength + duration.length),
|
||||
blockHeader,
|
||||
])
|
||||
return {
|
||||
header: Buffer.concat([
|
||||
cluster,
|
||||
variableInteger(clusterTimestamp.length + groupHeader.length + frameLength + duration.length),
|
||||
clusterTimestamp,
|
||||
groupHeader,
|
||||
]),
|
||||
trailer: duration,
|
||||
}
|
||||
}
|
||||
864
packages/browser-extension/cli/src/network-capture.ts
Normal file
864
packages/browser-extension/cli/src/network-capture.ts
Normal file
|
|
@ -0,0 +1,864 @@
|
|||
import { Effect, Predicate, Schema, Semaphore } from "effect"
|
||||
import path from "node:path"
|
||||
import type { Page, Request, Response } from "patchright-core"
|
||||
import * as AuthProfile from "./auth-profile.ts"
|
||||
import { writeJsonFileAtomically } from "./fs-durability.ts"
|
||||
import { SecretCollector } from "./network-redaction.ts"
|
||||
|
||||
export type NetworkCaptureOptions = {
|
||||
readonly urlFilter?: string
|
||||
readonly resourceTypes?: readonly string[]
|
||||
readonly content?: "omit" | "embed"
|
||||
readonly maxBodyBytes?: number
|
||||
readonly maxTotalBodyBytes?: number
|
||||
readonly maxEntries?: number
|
||||
}
|
||||
|
||||
export type NetworkCaptureStopOptions = {
|
||||
readonly outputPath?: string
|
||||
readonly secrets?: string
|
||||
readonly requireObservedSecrets?: boolean
|
||||
}
|
||||
|
||||
export type NetworkCaptureStatus = {
|
||||
readonly active: boolean
|
||||
readonly startedAt?: string
|
||||
readonly entryCount: number
|
||||
readonly responseCount: number
|
||||
readonly failureCount: number
|
||||
readonly capturedBodyBytes: number
|
||||
readonly truncatedBodyCount: number
|
||||
readonly droppedEntryCount: number
|
||||
readonly urlFilter?: string
|
||||
readonly resourceTypes?: readonly string[]
|
||||
readonly content?: "omit" | "embed"
|
||||
readonly secrets?: string
|
||||
}
|
||||
|
||||
type NetworkEndpointDigest = {
|
||||
readonly method: string
|
||||
readonly url: string
|
||||
readonly status?: number
|
||||
readonly count: number
|
||||
readonly requestHeaders?: readonly string[]
|
||||
readonly requestKeys?: readonly string[]
|
||||
readonly responseKeys?: readonly string[]
|
||||
}
|
||||
|
||||
export type NetworkCaptureResult = NetworkCaptureStatus & {
|
||||
readonly active: false
|
||||
readonly stoppedAt: string
|
||||
readonly outputPath?: string
|
||||
readonly authProfile?: AuthProfile.AuthProfileSummary
|
||||
readonly updatedSecretRefs: readonly string[]
|
||||
readonly observedSecretRefs: readonly string[]
|
||||
readonly endpoints?: readonly NetworkEndpointDigest[]
|
||||
}
|
||||
|
||||
export class NetworkCaptureError extends Schema.TaggedError<NetworkCaptureError>()(
|
||||
"NetworkCapture.Error",
|
||||
{
|
||||
message: Schema.String,
|
||||
operation: Schema.String,
|
||||
reason: Schema.Literals(["already-active", "inactive", "invalid-options", "finalize-failed", "persistence-failed"]),
|
||||
cause: Schema.optionalKey(Schema.Defect()),
|
||||
},
|
||||
) {}
|
||||
|
||||
type Header = { readonly name: string; readonly value: string }
|
||||
|
||||
type CapturedBody = {
|
||||
readonly size: number
|
||||
readonly mimeType: string
|
||||
readonly text?: string
|
||||
readonly truncated: boolean
|
||||
}
|
||||
|
||||
type CapturedEntry = {
|
||||
readonly id: string
|
||||
readonly startedDateTime: string
|
||||
readonly durationMs: number
|
||||
readonly request: {
|
||||
readonly method: string
|
||||
readonly url: string
|
||||
readonly headers: readonly Header[]
|
||||
readonly resourceType: string
|
||||
readonly body?: CapturedBody
|
||||
readonly redirectedFrom?: string
|
||||
}
|
||||
readonly response?: {
|
||||
readonly status: number
|
||||
readonly statusText: string
|
||||
readonly headers: readonly Header[]
|
||||
readonly body?: CapturedBody
|
||||
}
|
||||
readonly failure?: string
|
||||
}
|
||||
|
||||
type PendingEntry = {
|
||||
readonly id: string
|
||||
readonly startedAt: number
|
||||
readonly startedDateTime: string
|
||||
readonly request: Request
|
||||
response?: Response
|
||||
failure?: string
|
||||
finalized: boolean
|
||||
epoch: number
|
||||
}
|
||||
|
||||
type FinalizeWork = {
|
||||
readonly pending: PendingEntry
|
||||
readonly generation: number
|
||||
}
|
||||
|
||||
type ActiveCapture = {
|
||||
readonly startedAt: string
|
||||
readonly options: Required<Pick<NetworkCaptureOptions, "content" | "maxBodyBytes" | "maxTotalBodyBytes" | "maxEntries">> & NetworkCaptureOptions
|
||||
readonly pending: Map<Request, PendingEntry>
|
||||
readonly entries: CapturedEntry[]
|
||||
readonly finalizeQueue: FinalizeWork[]
|
||||
readonly finalizeWaiters: Set<() => void>
|
||||
readonly liveCollector: SecretCollector
|
||||
finalizeWorkers: number
|
||||
finalizeGeneration: number
|
||||
capturedBodyBytes: number
|
||||
truncatedBodyCount: number
|
||||
droppedEntryCount: number
|
||||
stopping: boolean
|
||||
cancelled: boolean
|
||||
outputUnsafe: boolean
|
||||
}
|
||||
|
||||
const maxAllowedBodyBytes = 10_000_000
|
||||
const maxAllowedTotalBodyBytes = 100_000_000
|
||||
const maxAllowedEntries = 10_000
|
||||
const maxAllowedResourceTypes = 50
|
||||
const maxFinalizeWorkers = 4
|
||||
|
||||
export class Recorder {
|
||||
private active: ActiveCapture | undefined
|
||||
private page: Page | undefined
|
||||
private pageEpoch = 0
|
||||
private readonly transition = Semaphore.makeUnsafe(1)
|
||||
|
||||
constructor(private readonly recorderOptions: {
|
||||
readonly authProfileBaseDir?: string
|
||||
readonly outputSettleTimeoutMs?: number
|
||||
} = {}) {}
|
||||
|
||||
start(page: Page, options: NetworkCaptureOptions = {}): Effect.Effect<NetworkCaptureStatus, NetworkCaptureError> {
|
||||
const capture = this
|
||||
return this.transition.withPermit(Effect.try({
|
||||
try: () => {
|
||||
validateOptions(options)
|
||||
if (capture.active) {
|
||||
throw new NetworkCaptureError({ message: "Network capture is already active for this session", operation: "start", reason: "already-active" })
|
||||
}
|
||||
capture.active = {
|
||||
startedAt: new Date().toISOString(),
|
||||
options: {
|
||||
...options,
|
||||
content: options.content ?? "embed",
|
||||
maxBodyBytes: options.maxBodyBytes ?? 1_000_000,
|
||||
maxTotalBodyBytes: options.maxTotalBodyBytes ?? 25_000_000,
|
||||
maxEntries: options.maxEntries ?? 1_000,
|
||||
},
|
||||
pending: new Map(),
|
||||
entries: [],
|
||||
finalizeQueue: [],
|
||||
finalizeWaiters: new Set(),
|
||||
liveCollector: new SecretCollector(),
|
||||
finalizeWorkers: 0,
|
||||
finalizeGeneration: 0,
|
||||
capturedBodyBytes: 0,
|
||||
truncatedBodyCount: 0,
|
||||
droppedEntryCount: 0,
|
||||
stopping: false,
|
||||
cancelled: false,
|
||||
outputUnsafe: false,
|
||||
}
|
||||
capture.bindPage(page)
|
||||
return capture.status()
|
||||
},
|
||||
catch: (cause) => cause instanceof NetworkCaptureError
|
||||
? cause
|
||||
: new NetworkCaptureError({ message: "Invalid network capture options", operation: "start", reason: "invalid-options", cause }),
|
||||
}))
|
||||
}
|
||||
|
||||
status(): NetworkCaptureStatus {
|
||||
const active = this.active
|
||||
if (!active) {
|
||||
return {
|
||||
active: false,
|
||||
entryCount: 0,
|
||||
responseCount: 0,
|
||||
failureCount: 0,
|
||||
capturedBodyBytes: 0,
|
||||
truncatedBodyCount: 0,
|
||||
droppedEntryCount: 0,
|
||||
}
|
||||
}
|
||||
return {
|
||||
active: true,
|
||||
startedAt: active.startedAt,
|
||||
entryCount: active.entries.length + active.pending.size,
|
||||
responseCount: active.entries.filter((entry) => entry.response).length,
|
||||
failureCount: active.entries.filter((entry) => entry.failure).length,
|
||||
capturedBodyBytes: active.capturedBodyBytes,
|
||||
truncatedBodyCount: active.truncatedBodyCount,
|
||||
droppedEntryCount: active.droppedEntryCount,
|
||||
...(active.options.urlFilter ? { urlFilter: active.options.urlFilter } : {}),
|
||||
...(active.options.resourceTypes ? { resourceTypes: [...active.options.resourceTypes] } : {}),
|
||||
content: active.options.content,
|
||||
}
|
||||
}
|
||||
|
||||
stop(options: NetworkCaptureStopOptions = {}): Effect.Effect<NetworkCaptureResult, NetworkCaptureError> {
|
||||
const capture = this
|
||||
return this.transition.withPermit(Effect.gen(function* () {
|
||||
const active = capture.active
|
||||
if (!active) {
|
||||
return yield* new NetworkCaptureError({ message: "Network capture is not active for this session", operation: "stop", reason: "inactive" })
|
||||
}
|
||||
const boundPage = capture.page
|
||||
active.stopping = true
|
||||
capture.unbindPage()
|
||||
const operation = Effect.gen(function* () {
|
||||
for (const pending of [...active.pending.values()]) {
|
||||
if (!pending.finalized) {
|
||||
pending.failure ??= "Capture stopped before request completed"
|
||||
delete pending.response
|
||||
}
|
||||
capture.scheduleFinalize(pending)
|
||||
}
|
||||
const settled = yield* Effect.promise(() => settleFinalizers(active, 5_000))
|
||||
if (!settled) {
|
||||
active.droppedEntryCount += active.pending.size
|
||||
capture.discardFinalizers(active)
|
||||
}
|
||||
|
||||
const secrets = options.secrets
|
||||
const profileOptions = capture.recorderOptions.authProfileBaseDir ? { baseDir: capture.recorderOptions.authProfileBaseDir } : {}
|
||||
const finish = finishCapture(active, options, secrets, profileOptions)
|
||||
const finished = yield* (secrets
|
||||
? AuthProfile.withLock(secrets, profileOptions, finish)
|
||||
: finish
|
||||
).pipe(
|
||||
Effect.mapError((cause) => cause instanceof NetworkCaptureError
|
||||
? cause
|
||||
: new NetworkCaptureError({ message: cause.message, operation: "auth-profile", reason: "persistence-failed", cause })),
|
||||
)
|
||||
const finalStatus = capture.status()
|
||||
capture.active = undefined
|
||||
return {
|
||||
...finalStatus,
|
||||
...(secrets ? { secrets } : {}),
|
||||
truncatedBodyCount: finalStatus.truncatedBodyCount + finished.redactionOmissionCount,
|
||||
active: false as const,
|
||||
stoppedAt: new Date().toISOString(),
|
||||
...(options.outputPath ? { outputPath: path.resolve(options.outputPath) } : {}),
|
||||
...(finished.authProfile ? { authProfile: finished.authProfile } : {}),
|
||||
updatedSecretRefs: finished.updatedSecretRefs,
|
||||
observedSecretRefs: finished.observedSecretRefs,
|
||||
...(finished.endpoints.length > 0 ? { endpoints: finished.endpoints } : {}),
|
||||
}
|
||||
}).pipe(
|
||||
Effect.tapError(() => Effect.sync(() => {
|
||||
active.stopping = false
|
||||
if (capture.active === active) capture.bindPage(boundPage)
|
||||
})),
|
||||
)
|
||||
return yield* operation
|
||||
})).pipe(Effect.uninterruptible)
|
||||
}
|
||||
|
||||
cancel(): Effect.Effect<{ readonly cancelled: boolean }> {
|
||||
const capture = this
|
||||
return this.transition.withPermit(Effect.gen(function* () {
|
||||
const active = capture.active
|
||||
if (!active) return { cancelled: false }
|
||||
active.stopping = true
|
||||
active.cancelled = true
|
||||
capture.unbindPage()
|
||||
capture.discardFinalizers(active)
|
||||
yield* Effect.promise(() => settleFinalizers(active, 5_000))
|
||||
if (capture.active === active) capture.active = undefined
|
||||
return { cancelled: true }
|
||||
})).pipe(Effect.uninterruptible)
|
||||
}
|
||||
|
||||
bindPage(page: Page | undefined): void {
|
||||
const active = this.active
|
||||
if (!active || active.stopping || this.page === page) return
|
||||
if (this.page) {
|
||||
for (const pending of active.pending.values()) {
|
||||
if (pending.epoch !== this.pageEpoch || pending.finalized) continue
|
||||
pending.failure ??= "Page changed before request completed"
|
||||
delete pending.response
|
||||
this.scheduleFinalize(pending)
|
||||
}
|
||||
}
|
||||
this.unbindPage()
|
||||
if (!page || page.isClosed()) return
|
||||
this.page = page
|
||||
this.pageEpoch += 1
|
||||
page.on("request", this.onRequest)
|
||||
page.on("response", this.onResponse)
|
||||
page.on("requestfinished", this.onRequestFinished)
|
||||
page.on("requestfailed", this.onRequestFailed)
|
||||
}
|
||||
|
||||
async settleForOutput(): Promise<void> {
|
||||
const active = this.active
|
||||
if (!active) return
|
||||
active.outputUnsafe = !await settleFinalizers(active, this.recorderOptions.outputSettleTimeoutMs ?? 5_000)
|
||||
}
|
||||
|
||||
redactText(text: string): string {
|
||||
const active = this.active
|
||||
if (!active) return text
|
||||
return active.outputUnsafe ? "[REDACTED: network capture finalization pending]" : active.liveCollector.redactText(text)
|
||||
}
|
||||
|
||||
redactValue(value: unknown): unknown {
|
||||
const active = this.active
|
||||
if (!active) return value
|
||||
return active.outputUnsafe ? "[REDACTED: network capture finalization pending]" : active.liveCollector.redactValue(value)
|
||||
}
|
||||
|
||||
redactUrl(url: string): string {
|
||||
const active = this.active
|
||||
if (!active) return url
|
||||
if (active.outputUnsafe) return "[REDACTED: network capture finalization pending]"
|
||||
return active.liveCollector.redactText(active.liveCollector.protectUrl(url, "execute"))
|
||||
}
|
||||
|
||||
private readonly onRequest = (request: Request): void => {
|
||||
const active = this.active
|
||||
if (!active || !matches(request, active.options)) return
|
||||
if (active.entries.length + active.pending.size >= active.options.maxEntries) {
|
||||
active.droppedEntryCount += 1
|
||||
return
|
||||
}
|
||||
active.pending.set(request, {
|
||||
id: `request-${active.entries.length + active.pending.size + 1}`,
|
||||
startedAt: Date.now(),
|
||||
startedDateTime: new Date().toISOString(),
|
||||
request,
|
||||
finalized: false,
|
||||
epoch: this.pageEpoch,
|
||||
})
|
||||
}
|
||||
|
||||
private readonly onResponse = (response: Response): void => {
|
||||
const pending = this.active?.pending.get(response.request())
|
||||
if (pending) pending.response = response
|
||||
}
|
||||
|
||||
private readonly onRequestFinished = (request: Request): void => {
|
||||
const pending = this.active?.pending.get(request)
|
||||
if (pending) this.scheduleFinalize(pending)
|
||||
}
|
||||
|
||||
private readonly onRequestFailed = (request: Request): void => {
|
||||
const pending = this.active?.pending.get(request)
|
||||
if (!pending) return
|
||||
pending.failure = request.failure()?.errorText ?? "Request failed"
|
||||
this.scheduleFinalize(pending)
|
||||
}
|
||||
|
||||
private scheduleFinalize(pending: PendingEntry): void {
|
||||
const active = this.active
|
||||
if (!active || pending.finalized) return
|
||||
pending.finalized = true
|
||||
active.finalizeQueue.push({ pending, generation: active.finalizeGeneration })
|
||||
this.pumpFinalizers(active)
|
||||
}
|
||||
|
||||
private pumpFinalizers(active: ActiveCapture): void {
|
||||
if (active.cancelled) active.finalizeQueue.length = 0
|
||||
while (!active.cancelled && active.finalizeWorkers < maxFinalizeWorkers) {
|
||||
const work = active.finalizeQueue.shift()
|
||||
if (!work) break
|
||||
active.finalizeWorkers += 1
|
||||
void this.finalize(work.pending, active, work.generation).catch(() => {}).finally(() => {
|
||||
active.finalizeWorkers -= 1
|
||||
this.pumpFinalizers(active)
|
||||
notifyFinalizersSettled(active)
|
||||
})
|
||||
}
|
||||
notifyFinalizersSettled(active)
|
||||
}
|
||||
|
||||
private discardFinalizers(active: ActiveCapture): void {
|
||||
active.finalizeGeneration += 1
|
||||
active.finalizeQueue.length = 0
|
||||
active.pending.clear()
|
||||
notifyFinalizersSettled(active)
|
||||
}
|
||||
|
||||
private async finalize(pending: PendingEntry, active: ActiveCapture, generation: number): Promise<void> {
|
||||
const request = pending.request
|
||||
try {
|
||||
const requestHeaders = await safeHeaders(() => request.headersArray())
|
||||
const requestLimit = Math.min(active.options.maxBodyBytes, active.options.maxTotalBodyBytes - active.capturedBodyBytes)
|
||||
const requestSize = declaredBodySize(requestHeaders)
|
||||
const requestMayHaveBody = requestSize !== undefined
|
||||
? requestSize > 0
|
||||
: contentType(requestHeaders) !== undefined && !/^(GET|HEAD)$/i.test(request.method())
|
||||
const requestBody = active.options.content === "embed" && requestMayHaveBody
|
||||
? bodyCanFit(requestHeaders, requestLimit)
|
||||
? captureBuffer(request.postDataBuffer(), contentType(requestHeaders), active)
|
||||
: truncatedBody(contentType(requestHeaders), active, requestSize ?? 0)
|
||||
: undefined
|
||||
const response = pending.response
|
||||
const responseHeaders = response ? await safeHeaders(() => response.headersArray()) : []
|
||||
const shouldCaptureResponseBody = response && active.options.content === "embed"
|
||||
const hasBodyBudget = active.capturedBodyBytes < active.options.maxTotalBodyBytes
|
||||
let responseBody: CapturedBody | undefined
|
||||
if (shouldCaptureResponseBody) {
|
||||
if (hasBodyBudget) {
|
||||
const responseBuffer = bodyCanFit(responseHeaders, Math.min(active.options.maxBodyBytes, active.options.maxTotalBodyBytes - active.capturedBodyBytes))
|
||||
? await bodyWithTimeout(response, 1_000)
|
||||
: null
|
||||
responseBody = responseBuffer === null
|
||||
? truncatedBody(contentType(responseHeaders), active, declaredBodySize(responseHeaders) ?? 0)
|
||||
: captureBuffer(responseBuffer, contentType(responseHeaders), active)
|
||||
} else {
|
||||
responseBody = truncatedBody(contentType(responseHeaders), active)
|
||||
}
|
||||
}
|
||||
const redirectedFrom = request.redirectedFrom()?.url()
|
||||
this.recordEntry(active, generation, {
|
||||
id: pending.id,
|
||||
startedDateTime: pending.startedDateTime,
|
||||
durationMs: Math.max(0, Date.now() - pending.startedAt),
|
||||
request: {
|
||||
method: request.method(),
|
||||
url: request.url(),
|
||||
headers: requestHeaders,
|
||||
resourceType: request.resourceType(),
|
||||
...(requestBody ? { body: requestBody } : {}),
|
||||
...(redirectedFrom ? { redirectedFrom } : {}),
|
||||
},
|
||||
...(response ? {
|
||||
response: {
|
||||
status: response.status(),
|
||||
statusText: response.statusText(),
|
||||
headers: responseHeaders,
|
||||
...(responseBody ? { body: responseBody } : {}),
|
||||
},
|
||||
} : {}),
|
||||
...(pending.failure ? { failure: pending.failure } : {}),
|
||||
})
|
||||
} catch {
|
||||
this.recordEntry(active, generation, {
|
||||
id: pending.id,
|
||||
startedDateTime: pending.startedDateTime,
|
||||
durationMs: Math.max(0, Date.now() - pending.startedAt),
|
||||
request: {
|
||||
method: safeValue(() => request.method(), "UNKNOWN"),
|
||||
url: safeValue(() => request.url(), ""),
|
||||
headers: [],
|
||||
resourceType: safeValue(() => request.resourceType(), "unknown"),
|
||||
},
|
||||
failure: "Capture finalization failed",
|
||||
})
|
||||
} finally {
|
||||
active.pending.delete(request)
|
||||
}
|
||||
}
|
||||
|
||||
private recordEntry(active: ActiveCapture, generation: number, entry: CapturedEntry): void {
|
||||
if (active.cancelled || generation !== active.finalizeGeneration) return
|
||||
active.entries.push(entry)
|
||||
protectEntry(entry, active.liveCollector)
|
||||
}
|
||||
|
||||
private unbindPage(): void {
|
||||
const page = this.page
|
||||
this.page = undefined
|
||||
if (!page) return
|
||||
page.off("request", this.onRequest)
|
||||
page.off("response", this.onResponse)
|
||||
page.off("requestfinished", this.onRequestFinished)
|
||||
page.off("requestfailed", this.onRequestFailed)
|
||||
}
|
||||
}
|
||||
|
||||
function finishCapture(
|
||||
active: ActiveCapture,
|
||||
options: NetworkCaptureStopOptions,
|
||||
secrets: string | undefined,
|
||||
profileOptions: { readonly baseDir?: string },
|
||||
): Effect.Effect<{
|
||||
readonly authProfile?: AuthProfile.AuthProfileSummary
|
||||
readonly updatedSecretRefs: readonly string[]
|
||||
readonly observedSecretRefs: readonly string[]
|
||||
readonly redactionOmissionCount: number
|
||||
readonly endpoints: readonly NetworkEndpointDigest[]
|
||||
}, NetworkCaptureError | AuthProfile.AuthProfileError> {
|
||||
return Effect.gen(function* () {
|
||||
const existingProfile = secrets
|
||||
? yield* AuthProfile.readOptional(secrets, profileOptions)
|
||||
: undefined
|
||||
const collector = new SecretCollector(existingProfile?.slots ?? [])
|
||||
const structurallyProtected = active.entries.map((entry) => protectEntry(entry, collector))
|
||||
const protectedEntries = structurallyProtected.map((entry) => redactEntryKnownValues(entry, collector))
|
||||
if (options.requireObservedSecrets && collector.observedRefs().length === 0) {
|
||||
return yield* new NetworkCaptureError({
|
||||
message: `Auth refresh did not observe credentials for profile ${secrets ?? "unknown"}`,
|
||||
operation: "auth-profile",
|
||||
reason: "persistence-failed",
|
||||
})
|
||||
}
|
||||
const authProfile = secrets
|
||||
? yield* AuthProfile.write({ name: secrets, slots: collector.slots(), ...profileOptions })
|
||||
: undefined
|
||||
if (options.outputPath) {
|
||||
yield* writeArtifact(options.outputPath, {
|
||||
log: {
|
||||
version: "1.2",
|
||||
creator: { name: "OpenCode Browser", version: "1" },
|
||||
entries: protectedEntries.map(toHarEntry),
|
||||
},
|
||||
})
|
||||
}
|
||||
return {
|
||||
...(authProfile ? { authProfile } : {}),
|
||||
updatedSecretRefs: collector.updatedRefs(),
|
||||
observedSecretRefs: collector.observedRefs(),
|
||||
redactionOmissionCount: options.outputPath ? countRedactionOmissions(active.entries, protectedEntries) : 0,
|
||||
endpoints: summarizeEndpoints(protectedEntries),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function extractTopLevelJsonKeys(body: CapturedBody | undefined): readonly string[] | undefined {
|
||||
if (!body?.text || !body.mimeType.toLowerCase().includes("json")) return undefined
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(body.text)
|
||||
if (Predicate.isObject(parsed) && !Array.isArray(parsed)) {
|
||||
const keys = Object.keys(parsed).slice(0, 15)
|
||||
return keys.length > 0 ? keys : undefined
|
||||
}
|
||||
} catch {}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function summarizeEndpoints(entries: readonly CapturedEntry[]): readonly NetworkEndpointDigest[] {
|
||||
const byRoute = new Map<string, NetworkEndpointDigest>()
|
||||
for (const entry of entries) {
|
||||
if (["image", "stylesheet", "font", "media"].includes(entry.request.resourceType)) continue
|
||||
let routePath = entry.request.url
|
||||
try {
|
||||
const u = new URL(entry.request.url)
|
||||
routePath = `${u.origin}${u.pathname}`
|
||||
} catch {}
|
||||
const key = `${entry.request.method} ${routePath}`
|
||||
const existing = byRoute.get(key)
|
||||
const notableHeaders = entry.request.headers
|
||||
.filter((h) => {
|
||||
const lower = h.name.toLowerCase()
|
||||
if (lower === "x-client-data" || lower.startsWith("sec-ch-")) return false
|
||||
return h.value.includes("${BC_SECRET_") || lower === "authorization" || lower.startsWith("x-")
|
||||
})
|
||||
.slice(0, 8)
|
||||
.map((h) => `${h.name}: ${h.value.slice(0, 80)}`)
|
||||
const requestKeys = extractTopLevelJsonKeys(entry.request.body)
|
||||
const responseKeys = extractTopLevelJsonKeys(entry.response?.body)
|
||||
byRoute.set(key, {
|
||||
method: entry.request.method,
|
||||
url: entry.request.url.slice(0, 240),
|
||||
...(entry.response ? { status: entry.response.status } : existing?.status !== undefined ? { status: existing.status } : {}),
|
||||
count: (existing?.count ?? 0) + 1,
|
||||
...(notableHeaders.length > 0 ? { requestHeaders: notableHeaders } : existing?.requestHeaders ? { requestHeaders: existing.requestHeaders } : {}),
|
||||
...(requestKeys ? { requestKeys } : existing?.requestKeys ? { requestKeys: existing.requestKeys } : {}),
|
||||
...(responseKeys ? { responseKeys } : existing?.responseKeys ? { responseKeys: existing.responseKeys } : {}),
|
||||
})
|
||||
if (byRoute.size >= 30) break
|
||||
}
|
||||
return [...byRoute.values()]
|
||||
}
|
||||
|
||||
function countRedactionOmissions(original: readonly CapturedEntry[], protectedEntries: readonly CapturedEntry[]): number {
|
||||
let count = 0
|
||||
for (let index = 0; index < original.length; index += 1) {
|
||||
const before = original[index]
|
||||
const after = protectedEntries[index]
|
||||
if (before?.request.body?.text && !before.request.body.truncated && !after?.request.body?.text) count += 1
|
||||
if (before?.response?.body?.text && !before.response.body.truncated && !after?.response?.body?.text) count += 1
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
function matches(request: Request, options: ActiveCapture["options"]): boolean {
|
||||
if (options.urlFilter && !request.url().includes(options.urlFilter)) return false
|
||||
return !options.resourceTypes || options.resourceTypes.includes(request.resourceType())
|
||||
}
|
||||
|
||||
function validateOptions(options: NetworkCaptureOptions): void {
|
||||
if (options.content !== undefined && options.content !== "embed" && options.content !== "omit") {
|
||||
throw new NetworkCaptureError({ message: "Network content must be embed or omit", operation: "start", reason: "invalid-options" })
|
||||
}
|
||||
validateLimit("maxBodyBytes", options.maxBodyBytes, maxAllowedBodyBytes)
|
||||
validateLimit("maxTotalBodyBytes", options.maxTotalBodyBytes, maxAllowedTotalBodyBytes)
|
||||
validateLimit("maxEntries", options.maxEntries, maxAllowedEntries)
|
||||
if (options.resourceTypes && options.resourceTypes.length > maxAllowedResourceTypes) {
|
||||
throw new NetworkCaptureError({ message: `resourceTypes cannot contain more than ${maxAllowedResourceTypes} values`, operation: "start", reason: "invalid-options" })
|
||||
}
|
||||
}
|
||||
|
||||
function validateLimit(name: string, value: number | undefined, maximum: number): void {
|
||||
if (value === undefined) return
|
||||
if (!Number.isInteger(value) || value <= 0 || value > maximum) {
|
||||
throw new NetworkCaptureError({ message: `${name} must be an integer from 1 to ${maximum}`, operation: "start", reason: "invalid-options" })
|
||||
}
|
||||
}
|
||||
|
||||
function captureBuffer(buffer: Buffer | null, mimeType: string | undefined, active: ActiveCapture): CapturedBody | undefined {
|
||||
if (!buffer) return undefined
|
||||
const remaining = Math.max(0, active.options.maxTotalBodyBytes - active.capturedBodyBytes)
|
||||
const captureBytes = Math.min(buffer.length, active.options.maxBodyBytes, remaining)
|
||||
const truncated = captureBytes < buffer.length
|
||||
active.capturedBodyBytes += captureBytes
|
||||
const textual = isTextualMimeType(mimeType)
|
||||
const omitted = truncated || !textual
|
||||
if (omitted) active.truncatedBodyCount += 1
|
||||
return {
|
||||
size: buffer.length,
|
||||
mimeType: mimeType ?? "application/octet-stream",
|
||||
...(!truncated && textual ? { text: buffer.toString("utf8", 0, captureBytes) } : {}),
|
||||
truncated: omitted,
|
||||
}
|
||||
}
|
||||
|
||||
function bodyCanFit(headers: readonly Header[], limit: number): boolean {
|
||||
if (limit <= 0) return false
|
||||
const length = declaredBodySize(headers)
|
||||
const encoding = header(headers, "content-encoding")?.trim().toLowerCase()
|
||||
const supportedEncoding = !encoding || encoding === "identity" || encoding === "gzip" || encoding === "br" || encoding === "deflate" || encoding === "zstd"
|
||||
if (!supportedEncoding) return false
|
||||
if (length !== undefined) return length <= limit
|
||||
return Boolean(encoding && isTextualMimeType(contentType(headers)))
|
||||
}
|
||||
|
||||
function declaredBodySize(headers: readonly Header[]): number | undefined {
|
||||
const value = header(headers, "content-length")
|
||||
if (!value) return undefined
|
||||
const length = Number(value)
|
||||
return Number.isSafeInteger(length) && length >= 0 ? length : undefined
|
||||
}
|
||||
|
||||
function truncatedBody(mimeType: string | undefined, active: ActiveCapture, size = 0): CapturedBody {
|
||||
active.truncatedBodyCount += 1
|
||||
return { size, mimeType: mimeType ?? "application/octet-stream", truncated: true }
|
||||
}
|
||||
|
||||
function protectEntry(entry: CapturedEntry, collector: SecretCollector): CapturedEntry {
|
||||
const scope = requestScope(entry.request.method, entry.request.url)
|
||||
const requestHeaders = collector.protectHeaders(entry.request.headers, "request", scope)
|
||||
const responseHeaders = entry.response ? collector.protectHeaders(entry.response.headers, "response", scope) : undefined
|
||||
const requestBody = protectCapturedBody(entry.request.body, collector, "request", scope)
|
||||
const responseBody = protectCapturedBody(entry.response?.body, collector, "response", scope)
|
||||
return {
|
||||
...entry,
|
||||
request: {
|
||||
...entry.request,
|
||||
url: collector.protectUrl(entry.request.url, scope),
|
||||
headers: requestHeaders,
|
||||
...(requestBody ? { body: requestBody } : {}),
|
||||
...(entry.request.redirectedFrom ? { redirectedFrom: collector.protectUrl(entry.request.redirectedFrom, scope) } : {}),
|
||||
},
|
||||
...(entry.response ? {
|
||||
response: {
|
||||
...entry.response,
|
||||
headers: responseHeaders ?? [],
|
||||
...(responseBody ? { body: responseBody } : {}),
|
||||
},
|
||||
} : {}),
|
||||
}
|
||||
}
|
||||
|
||||
function protectCapturedBody(
|
||||
body: CapturedBody | undefined,
|
||||
collector: SecretCollector,
|
||||
location: "request" | "response",
|
||||
scope: string,
|
||||
): CapturedBody | undefined {
|
||||
if (!body) return undefined
|
||||
if (!body.text) {
|
||||
return { size: body.size, mimeType: body.mimeType, truncated: true }
|
||||
}
|
||||
const text = collector.protectBody(body.text, body.mimeType, location, scope)
|
||||
return text === undefined
|
||||
? { size: body.size, mimeType: body.mimeType, truncated: true }
|
||||
: { ...body, text }
|
||||
}
|
||||
|
||||
function redactEntryKnownValues(entry: CapturedEntry, collector: SecretCollector): CapturedEntry {
|
||||
return {
|
||||
...entry,
|
||||
request: {
|
||||
...entry.request,
|
||||
...(entry.request.body?.text ? { body: redactStructuredBody(entry.request.body, collector) } : {}),
|
||||
},
|
||||
...(entry.response ? {
|
||||
response: {
|
||||
...entry.response,
|
||||
...(entry.response.body?.text ? { body: redactStructuredBody(entry.response.body, collector) } : {}),
|
||||
},
|
||||
} : {}),
|
||||
}
|
||||
}
|
||||
|
||||
function redactStructuredBody(body: CapturedBody, collector: SecretCollector): CapturedBody {
|
||||
if (!body.text || !body.mimeType.toLowerCase().includes("json")) return body
|
||||
try {
|
||||
return { ...body, text: JSON.stringify(redactKnownScalars(JSON.parse(body.text), collector)) }
|
||||
} catch {
|
||||
return { size: body.size, mimeType: body.mimeType, truncated: true }
|
||||
}
|
||||
}
|
||||
|
||||
function redactKnownScalars(value: unknown, collector: SecretCollector): unknown {
|
||||
if (Array.isArray(value)) return value.map((item) => redactKnownScalars(item, collector))
|
||||
if (Predicate.isObject(value)) {
|
||||
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, redactKnownScalars(item, collector)]))
|
||||
}
|
||||
return collector.redactExactValue(value, 8)
|
||||
}
|
||||
|
||||
function toHarEntry(entry: CapturedEntry): Record<string, unknown> {
|
||||
return {
|
||||
startedDateTime: entry.startedDateTime,
|
||||
time: entry.durationMs,
|
||||
request: {
|
||||
method: entry.request.method,
|
||||
url: entry.request.url,
|
||||
httpVersion: "",
|
||||
cookies: [],
|
||||
headers: entry.request.headers,
|
||||
queryString: queryString(entry.request.url),
|
||||
headersSize: -1,
|
||||
bodySize: entry.request.body?.size ?? 0,
|
||||
...(entry.request.body?.text ? {
|
||||
postData: { mimeType: entry.request.body.mimeType, text: entry.request.body.text },
|
||||
} : {}),
|
||||
},
|
||||
response: entry.response ? {
|
||||
status: entry.response.status,
|
||||
statusText: entry.response.statusText,
|
||||
httpVersion: "",
|
||||
cookies: [],
|
||||
headers: entry.response.headers,
|
||||
content: {
|
||||
size: entry.response.body?.size ?? 0,
|
||||
mimeType: entry.response.body?.mimeType ?? contentType(entry.response.headers) ?? "application/octet-stream",
|
||||
...(entry.response.body?.text ? { text: entry.response.body.text } : {}),
|
||||
},
|
||||
redirectURL: header(entry.response.headers, "location") ?? "",
|
||||
headersSize: -1,
|
||||
bodySize: entry.response.body?.size ?? 0,
|
||||
} : { status: 0, statusText: entry.failure ?? "No response", httpVersion: "", cookies: [], headers: [], content: { size: 0, mimeType: "application/octet-stream" }, redirectURL: "", headersSize: -1, bodySize: 0 },
|
||||
cache: {},
|
||||
timings: { send: 0, wait: entry.durationMs, receive: 0 },
|
||||
_opencodeBrowser: {
|
||||
id: entry.id,
|
||||
resourceType: entry.request.resourceType,
|
||||
...(entry.request.redirectedFrom ? { redirectedFrom: entry.request.redirectedFrom } : {}),
|
||||
...(entry.failure ? { failure: entry.failure } : {}),
|
||||
requestBodyTruncated: entry.request.body?.truncated ?? false,
|
||||
responseBodyTruncated: entry.response?.body?.truncated ?? false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function writeArtifact(outputPath: string, value: unknown): Effect.Effect<void, NetworkCaptureError> {
|
||||
return Effect.tryPromise({
|
||||
try: () => writeJsonFileAtomically(path.resolve(outputPath), value),
|
||||
catch: (cause) => new NetworkCaptureError({ message: `Could not write network capture: ${outputPath}`, operation: "write", reason: "persistence-failed", cause }),
|
||||
})
|
||||
}
|
||||
|
||||
async function safeHeaders(read: () => Promise<Header[]>): Promise<Header[]> {
|
||||
return read().catch(() => [])
|
||||
}
|
||||
|
||||
async function bodyWithTimeout(response: Response, timeoutMs: number): Promise<Buffer | null> {
|
||||
let timeout: NodeJS.Timeout | undefined
|
||||
try {
|
||||
return await Promise.race([
|
||||
response.body().catch(() => null),
|
||||
new Promise<null>((resolve) => {
|
||||
timeout = setTimeout(() => resolve(null), timeoutMs)
|
||||
}),
|
||||
])
|
||||
} finally {
|
||||
if (timeout) clearTimeout(timeout)
|
||||
}
|
||||
}
|
||||
|
||||
async function settleFinalizers(active: ActiveCapture, timeoutMs: number): Promise<boolean> {
|
||||
if (active.pending.size === 0 && active.finalizeQueue.length === 0 && active.finalizeWorkers === 0) return true
|
||||
let timeout: NodeJS.Timeout | undefined
|
||||
let waiter: (() => void) | undefined
|
||||
try {
|
||||
return await Promise.race([
|
||||
new Promise<true>((resolve) => {
|
||||
waiter = () => resolve(true)
|
||||
active.finalizeWaiters.add(waiter)
|
||||
}),
|
||||
new Promise<false>((resolve) => {
|
||||
timeout = setTimeout(() => resolve(false), timeoutMs)
|
||||
}),
|
||||
])
|
||||
} finally {
|
||||
if (timeout) clearTimeout(timeout)
|
||||
if (waiter) active.finalizeWaiters.delete(waiter)
|
||||
}
|
||||
}
|
||||
|
||||
function notifyFinalizersSettled(active: ActiveCapture): void {
|
||||
if (active.pending.size > 0 || active.finalizeQueue.length > 0 || active.finalizeWorkers > 0) return
|
||||
for (const resolve of active.finalizeWaiters) resolve()
|
||||
active.finalizeWaiters.clear()
|
||||
}
|
||||
|
||||
function safeValue<A>(read: () => A, fallback: A): A {
|
||||
try {
|
||||
return read()
|
||||
} catch {
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
function requestScope(method: string, rawUrl: string): string {
|
||||
try {
|
||||
const url = new URL(rawUrl)
|
||||
return `${method.toUpperCase()} ${url.origin}${url.pathname}`
|
||||
} catch {
|
||||
return `${method.toUpperCase()} ${rawUrl.split("?", 1)[0] ?? rawUrl}`
|
||||
}
|
||||
}
|
||||
|
||||
function contentType(headers: readonly Header[]): string | undefined {
|
||||
return header(headers, "content-type")?.trim()
|
||||
}
|
||||
|
||||
function header(headers: readonly Header[], name: string): string | undefined {
|
||||
return headers.find((candidate) => candidate.name.toLowerCase() === name)?.value
|
||||
}
|
||||
|
||||
function queryString(rawUrl: string): Array<{ name: string; value: string }> {
|
||||
try {
|
||||
return Array.from(new URL(rawUrl).searchParams, ([name, value]) => ({ name, value }))
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
function isTextualMimeType(mimeType: string | undefined): boolean {
|
||||
return !mimeType || mimeType.startsWith("text/") || /json|javascript|xml|x-www-form-urlencoded|multipart\/form-data|graphql/.test(mimeType)
|
||||
}
|
||||
378
packages/browser-extension/cli/src/network-redaction.ts
Normal file
378
packages/browser-extension/cli/src/network-redaction.ts
Normal file
|
|
@ -0,0 +1,378 @@
|
|||
import { Predicate } from "effect"
|
||||
|
||||
export type CredentialSlot = {
|
||||
readonly ref: string
|
||||
value: string
|
||||
readonly sources: readonly string[]
|
||||
expiresAt?: string
|
||||
}
|
||||
|
||||
type RedactionSlot = Pick<CredentialSlot, "ref" | "value">
|
||||
type MutableCredentialSlot = Omit<CredentialSlot, "sources"> & { readonly sources: string[] }
|
||||
|
||||
const secretNamePattern = /auth(?:orization)?|cookie|credential|csrf|xsrf|token|secret|session|password|passwd|pwd|passcode|otp|(?:^|[-_.])code(?:$|[-_.])|(?:api|access|refresh)[-_.]?key|signature|(?:^|[-_.])sig(?:$|[-_.])/i
|
||||
const secretHeaders = new Set([
|
||||
"authorization",
|
||||
"proxy-authorization",
|
||||
"x-api-key",
|
||||
"api-key",
|
||||
"x-csrf-token",
|
||||
"csrf-token",
|
||||
"x-xsrf-token",
|
||||
])
|
||||
|
||||
export class SecretCollector {
|
||||
private readonly slotsByRef = new Map<string, MutableCredentialSlot>()
|
||||
private readonly refsByValue = new Map<string, string>()
|
||||
private readonly refsBySource = new Map<string, string>()
|
||||
// Retired values are redaction-only: never include them in persisted profile slots.
|
||||
private readonly retiredRefsByValue = new Map<string, string>()
|
||||
private nextRef = 1
|
||||
private updated = new Set<string>()
|
||||
private observed = new Set<string>()
|
||||
|
||||
constructor(existing: readonly CredentialSlot[] = []) {
|
||||
for (const slot of existing) {
|
||||
const copy: MutableCredentialSlot = {
|
||||
ref: slot.ref,
|
||||
value: slot.value,
|
||||
sources: [...slot.sources],
|
||||
...(slot.expiresAt ? { expiresAt: slot.expiresAt } : {}),
|
||||
}
|
||||
this.slotsByRef.set(copy.ref, copy)
|
||||
this.refsByValue.set(copy.value, copy.ref)
|
||||
for (const source of copy.sources) {
|
||||
this.refsBySource.set(source, copy.ref)
|
||||
}
|
||||
const number = /^BC_SECRET_(\d+)$/.exec(copy.ref)?.[1]
|
||||
if (number) this.nextRef = Math.max(this.nextRef, Number(number) + 1)
|
||||
}
|
||||
}
|
||||
|
||||
protectHeaders(headers: readonly { readonly name: string; readonly value: string }[], location: "request" | "response", requestScope = ""): Array<{ name: string; value: string }> {
|
||||
const occurrences = new Map<string, number>()
|
||||
return headers.map(({ name, value }) => {
|
||||
const lowerName = name.toLowerCase()
|
||||
const occurrence = occurrences.get(lowerName) ?? 0
|
||||
occurrences.set(lowerName, occurrence + 1)
|
||||
return {
|
||||
name,
|
||||
value: this.protectHeader(name, value, sourceName(requestScope, `${location}.header.${lowerName}`, occurrence)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
protectUrl(rawUrl: string, requestScope = ""): string {
|
||||
const fragmentStart = rawUrl.indexOf("#")
|
||||
const base = fragmentStart < 0 ? rawUrl : rawUrl.slice(0, fragmentStart)
|
||||
const protectQuery = (name: string, value: string, occurrence: number) => this.reference(
|
||||
value,
|
||||
sourceName(requestScope, `query.${name}`, occurrence),
|
||||
)
|
||||
let protectedUrl: string
|
||||
try {
|
||||
const url = new URL(base)
|
||||
url.search = protectSearchParams(url.searchParams, protectQuery).toString()
|
||||
protectedUrl = restoreReferencePlaceholders(url.toString())
|
||||
} catch {
|
||||
protectedUrl = protectRelativeUrl(base, protectQuery)
|
||||
}
|
||||
if (fragmentStart < 0) return protectedUrl
|
||||
const fragment = protectFragment(rawUrl.slice(fragmentStart + 1), (name, value, occurrence) => this.reference(
|
||||
value,
|
||||
sourceName(requestScope, `fragment.${name}`, occurrence),
|
||||
))
|
||||
return `${protectedUrl}#${fragment}`
|
||||
}
|
||||
|
||||
protectBody(body: string, contentType: string | undefined, location: "request" | "response", requestScope = ""): string | undefined {
|
||||
const type = contentType?.toLowerCase() ?? ""
|
||||
if (type.includes("json")) {
|
||||
try {
|
||||
return JSON.stringify(this.protectJson(JSON.parse(body), sourceName(requestScope, location)))
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
if (type.includes("application/x-www-form-urlencoded")) {
|
||||
const protectedParams = protectSearchParams(new URLSearchParams(body), (name, value, occurrence) =>
|
||||
this.reference(value, sourceName(requestScope, `${location}.form.${name}`, occurrence)))
|
||||
return restoreReferencePlaceholders(protectedParams.toString())
|
||||
}
|
||||
if (type.includes("multipart/form-data")) {
|
||||
return this.protectMultipart(body, contentType ?? "", location, requestScope)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
slots(): readonly CredentialSlot[] {
|
||||
return Array.from(this.slotsByRef.values())
|
||||
.sort((left, right) => left.ref.localeCompare(right.ref, undefined, { numeric: true }))
|
||||
.map((slot) => ({ ...slot, sources: [...slot.sources] }))
|
||||
}
|
||||
|
||||
updatedRefs(): readonly string[] {
|
||||
return Array.from(this.updated).sort()
|
||||
}
|
||||
|
||||
observedRefs(): readonly string[] {
|
||||
return Array.from(this.observed).sort()
|
||||
}
|
||||
|
||||
redactText(text: string): string {
|
||||
return redactKnownValues(text, this.redactionSlots())
|
||||
}
|
||||
|
||||
redactValue(value: unknown): unknown {
|
||||
return redactKnownValue(value, this.redactionSlots())
|
||||
}
|
||||
|
||||
redactExactValue(value: unknown, minimumLength: number): unknown {
|
||||
if (!Predicate.isString(value) && !Predicate.isNumber(value) && !Predicate.isBoolean(value)) return value
|
||||
const serialized = String(value)
|
||||
if (serialized.length < minimumLength) return value
|
||||
const ref = this.refsByValue.get(serialized) ?? this.retiredRefsByValue.get(serialized)
|
||||
return ref ? `\${${ref}}` : value
|
||||
}
|
||||
|
||||
private redactionSlots(): readonly RedactionSlot[] {
|
||||
return [...this.slotsByRef.values(), ...Array.from(this.retiredRefsByValue, ([value, ref]) => ({ ref, value }))]
|
||||
}
|
||||
|
||||
private protectHeader(name: string, value: string, source: string): string {
|
||||
if (!value) return value
|
||||
const lower = name.toLowerCase()
|
||||
if (lower === "cookie" || lower === "set-cookie") {
|
||||
const cookieOccurrences = new Map<string, number>()
|
||||
return value.split(/;\s*/).map((part, index) => {
|
||||
const separator = part.indexOf("=")
|
||||
if (separator < 1) return part
|
||||
const cookieName = part.slice(0, separator).trim()
|
||||
const cookieValue = part.slice(separator + 1)
|
||||
if (lower === "set-cookie" && index > 0 && /^(domain|path|expires|max-age|samesite)$/i.test(cookieName)) {
|
||||
return part
|
||||
}
|
||||
if (!cookieValue) return part
|
||||
const occurrence = cookieOccurrences.get(cookieName) ?? 0
|
||||
cookieOccurrences.set(cookieName, occurrence + 1)
|
||||
return `${cookieName}=${this.reference(cookieValue, sourceName("", `${source}.${cookieName}`, occurrence))}`
|
||||
}).join("; ")
|
||||
}
|
||||
if (lower === "location" || lower === "referer" || lower === "referrer") {
|
||||
return this.protectUrl(value, `${source}.url`)
|
||||
}
|
||||
if (lower === "authorization" || lower === "proxy-authorization") {
|
||||
const match = /^(\S+)\s+(.+)$/.exec(value)
|
||||
if (match) return `${match[1]} ${this.reference(match[2] ?? "", source)}`
|
||||
}
|
||||
if (secretHeaders.has(lower) || secretNamePattern.test(lower)) {
|
||||
return this.reference(value, source)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
private protectJson(value: unknown, location: string, path: readonly string[] = [], inSecret = false): unknown {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item, index) => this.protectJson(item, location, [...path, String(index)], inSecret))
|
||||
}
|
||||
if (Predicate.isObject(value)) {
|
||||
return Object.fromEntries(Object.entries(value).map(([key, item]) => [
|
||||
key,
|
||||
this.protectJson(item, location, [...path, key], inSecret || secretNamePattern.test(key)),
|
||||
]))
|
||||
}
|
||||
if (!inSecret || value === null || value === "") return value
|
||||
return this.reference(String(value), `${location}.json.${path.join(".")}`)
|
||||
}
|
||||
|
||||
private protectMultipart(body: string, contentType: string, location: "request" | "response", requestScope: string): string | undefined {
|
||||
const boundary = /boundary=(?:"([^"]+)"|([^;\s]+))/i.exec(contentType)?.slice(1).find(Boolean)
|
||||
if (!boundary) return undefined
|
||||
const delimiter = `--${boundary}`
|
||||
const parts = body.split(delimiter)
|
||||
if (parts.length < 3) return undefined
|
||||
const occurrences = new Map<string, number>()
|
||||
for (let index = 1; index < parts.length - 1; index += 1) {
|
||||
const part = parts[index]
|
||||
if (!part?.startsWith("\r\n")) return undefined
|
||||
const headerEnd = part.indexOf("\r\n\r\n")
|
||||
if (headerEnd < 0 || !part.endsWith("\r\n")) return undefined
|
||||
const headers = part.slice(2, headerEnd)
|
||||
const disposition = headers.split("\r\n").find((line) => /^content-disposition:/i.test(line))
|
||||
if (!disposition || /;\s*filename\*?=/i.test(disposition)) return undefined
|
||||
const name = /;\s*name="([^"]+)"/i.exec(disposition)?.[1]
|
||||
if (!name) return undefined
|
||||
const valueStart = headerEnd + 4
|
||||
const valueEnd = part.length - 2
|
||||
const value = part.slice(valueStart, valueEnd)
|
||||
if (!value || !secretNamePattern.test(name)) continue
|
||||
const occurrence = occurrences.get(name) ?? 0
|
||||
occurrences.set(name, occurrence + 1)
|
||||
const reference = this.reference(value, sourceName(requestScope, `${location}.multipart.${name}`, occurrence))
|
||||
parts[index] = `${part.slice(0, valueStart)}${reference}${part.slice(valueEnd)}`
|
||||
}
|
||||
return parts.join(delimiter)
|
||||
}
|
||||
|
||||
private reference(value: string, source: string): string {
|
||||
const sourceRef = this.refsBySource.get(source)
|
||||
if (sourceRef) {
|
||||
const slot = this.slotsByRef.get(sourceRef)
|
||||
if (slot) {
|
||||
if (slot.value === value) {
|
||||
this.observed.add(sourceRef)
|
||||
return `\${${sourceRef}}`
|
||||
}
|
||||
if (slot.sources.length === 1) {
|
||||
this.retiredRefsByValue.set(slot.value, slot.ref)
|
||||
if (this.refsByValue.get(slot.value) === sourceRef) this.refsByValue.delete(slot.value)
|
||||
slot.value = value
|
||||
const expiresAt = jwtExpiration(value)
|
||||
if (expiresAt) slot.expiresAt = expiresAt
|
||||
else delete slot.expiresAt
|
||||
this.refsByValue.set(value, sourceRef)
|
||||
this.updated.add(sourceRef)
|
||||
this.observed.add(sourceRef)
|
||||
return `\${${sourceRef}}`
|
||||
}
|
||||
const sourceIndex = slot.sources.indexOf(source)
|
||||
if (sourceIndex >= 0) slot.sources.splice(sourceIndex, 1)
|
||||
const replacementRef = this.refsByValue.get(value)
|
||||
if (replacementRef) {
|
||||
this.refsBySource.set(source, replacementRef)
|
||||
this.addSource(replacementRef, source)
|
||||
this.updated.add(replacementRef)
|
||||
this.observed.add(replacementRef)
|
||||
return `\${${replacementRef}}`
|
||||
}
|
||||
}
|
||||
}
|
||||
const valueRef = this.refsByValue.get(value)
|
||||
if (valueRef) {
|
||||
this.observed.add(valueRef)
|
||||
this.addSource(valueRef, source)
|
||||
return `\${${valueRef}}`
|
||||
}
|
||||
const ref = `BC_SECRET_${this.nextRef++}`
|
||||
const expiresAt = jwtExpiration(value)
|
||||
this.slotsByRef.set(ref, { ref, value, sources: [source], ...(expiresAt ? { expiresAt } : {}) })
|
||||
this.refsByValue.set(value, ref)
|
||||
this.refsBySource.set(source, ref)
|
||||
this.updated.add(ref)
|
||||
this.observed.add(ref)
|
||||
return `\${${ref}}`
|
||||
}
|
||||
|
||||
private addSource(ref: string, source: string): void {
|
||||
const slot = this.slotsByRef.get(ref)
|
||||
if (!slot || slot.sources.includes(source)) return
|
||||
slot.sources.push(source)
|
||||
this.refsBySource.set(source, ref)
|
||||
}
|
||||
}
|
||||
|
||||
function restoreReferencePlaceholders(value: string): string {
|
||||
return value.replace(/(?:%24|\$)%7B(BC_SECRET_\d+)%7D/gi, (_match, ref: string) => `\${${ref}}`)
|
||||
}
|
||||
|
||||
/** Protect the query of a fragment-free URL that `new URL` cannot parse. */
|
||||
function protectRelativeUrl(
|
||||
rawUrl: string,
|
||||
protect: (name: string, value: string, occurrence: number) => string,
|
||||
): string {
|
||||
const queryStart = rawUrl.indexOf("?")
|
||||
if (queryStart < 0) return rawUrl
|
||||
const protectedQuery = protectSearchParams(new URLSearchParams(rawUrl.slice(queryStart + 1)), protect).toString()
|
||||
return restoreReferencePlaceholders(`${rawUrl.slice(0, queryStart)}?${protectedQuery}`)
|
||||
}
|
||||
|
||||
function protectSearchParams(
|
||||
params: URLSearchParams,
|
||||
protect: (name: string, value: string, occurrence: number) => string,
|
||||
): URLSearchParams {
|
||||
const result = new URLSearchParams()
|
||||
const occurrences = new Map<string, number>()
|
||||
for (const [name, value] of params) {
|
||||
const occurrence = occurrences.get(name) ?? 0
|
||||
occurrences.set(name, occurrence + 1)
|
||||
result.append(name, value && secretNamePattern.test(name) ? protect(name, value, occurrence) : value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
function protectFragment(
|
||||
fragment: string,
|
||||
protect: (name: string, value: string, occurrence: number) => string,
|
||||
): string {
|
||||
const occurrences = new Map<string, number>()
|
||||
return fragment.split("&").map((part) => {
|
||||
const separator = part.indexOf("=")
|
||||
if (separator < 1) return part
|
||||
const entry = new URLSearchParams(part).entries().next().value
|
||||
if (!entry) return part
|
||||
const [name, value] = entry
|
||||
// Recognize parameter names, not route/anchor text containing token-like words.
|
||||
if (!/^[\w.-]+$/.test(name) || !secretNamePattern.test(name)) return part
|
||||
const occurrence = occurrences.get(name) ?? 0
|
||||
occurrences.set(name, occurrence + 1)
|
||||
return value ? `${part.slice(0, separator + 1)}${protect(name, value, occurrence)}` : part
|
||||
}).join("&")
|
||||
}
|
||||
|
||||
function sourceName(requestScope: string, location: string, occurrence = 0): string {
|
||||
const base = requestScope ? `${requestScope}.${location}` : location
|
||||
return occurrence === 0 ? base : `${base}.${occurrence}`
|
||||
}
|
||||
|
||||
export function redactKnownValues(text: string, slots: readonly RedactionSlot[]): string {
|
||||
return [...slots]
|
||||
.sort((left, right) => right.value.length - left.value.length)
|
||||
.reduce((output, slot) => replaceOutsideReferences(output, slot.value, `\${${slot.ref}}`), text)
|
||||
}
|
||||
|
||||
function redactKnownValue(value: unknown, slots: readonly RedactionSlot[]): unknown {
|
||||
if (Predicate.isString(value)) return redactKnownValues(value, slots)
|
||||
if (Predicate.isNumber(value) || Predicate.isBoolean(value)) {
|
||||
const slot = slots.find((candidate) => candidate.value === String(value))
|
||||
return slot ? `\${${slot.ref}}` : value
|
||||
}
|
||||
if (Array.isArray(value)) return value.map((item) => redactKnownValue(item, slots))
|
||||
if (!Predicate.isObject(value)) return value
|
||||
return Object.fromEntries(Object.entries(value).map(([key, item]) => [
|
||||
key,
|
||||
secretNamePattern.test(key) ? redactSecretValue(item) : redactKnownValue(item, slots),
|
||||
]))
|
||||
}
|
||||
|
||||
function redactSecretValue(value: unknown): unknown {
|
||||
if (Array.isArray(value)) return value.map(redactSecretValue)
|
||||
if (Predicate.isObject(value)) {
|
||||
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, redactSecretValue(item)]))
|
||||
}
|
||||
return value === null || value === "" ? value : "[REDACTED]"
|
||||
}
|
||||
|
||||
function replaceOutsideReferences(text: string, value: string, replacement: string): string {
|
||||
if (!value) return text
|
||||
const referencePattern = /\$\{BC_SECRET_\d+\}/g
|
||||
let output = ""
|
||||
let offset = 0
|
||||
for (const match of text.matchAll(referencePattern)) {
|
||||
const index = match.index ?? 0
|
||||
output += text.slice(offset, index).split(value).join(replacement)
|
||||
output += match[0]
|
||||
offset = index + match[0].length
|
||||
}
|
||||
return output + text.slice(offset).split(value).join(replacement)
|
||||
}
|
||||
|
||||
function jwtExpiration(value: string): string | undefined {
|
||||
const parts = value.split(".")
|
||||
if (parts.length !== 3 || !parts[1]) return undefined
|
||||
try {
|
||||
const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8")) as { exp?: unknown }
|
||||
return Predicate.isNumber(payload.exp) ? new Date(payload.exp * 1_000).toISOString() : undefined
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
442
packages/browser-extension/cli/src/opencode-browser-client.ts
Normal file
442
packages/browser-extension/cli/src/opencode-browser-client.ts
Normal file
|
|
@ -0,0 +1,442 @@
|
|||
import { fileURLToPath } from "node:url"
|
||||
import { NodeHttpClient } from "@effect/platform-node"
|
||||
import { Context, Effect, Layer, Match, Redacted, Schema } from "effect"
|
||||
import * as AuthenticatedOriginInternal from "./authenticated-origin.ts"
|
||||
import * as RelayClient from "./relay-client.ts"
|
||||
import * as RelayLifecycle from "./relay-lifecycle.ts"
|
||||
import type {
|
||||
AuthenticatedJsonMethod,
|
||||
AuthenticatedJsonOutcome,
|
||||
SessionSummary,
|
||||
} from "./relay-schema.ts"
|
||||
|
||||
export type Json = Schema.Schema.Type<typeof Schema.Json>
|
||||
|
||||
export class ClientError extends Schema.TaggedError<ClientError>()(
|
||||
"OpenCodeBrowserClient.Error",
|
||||
{
|
||||
message: Schema.String,
|
||||
reason: Schema.Literals(["connect", "session", "invalid-request"]),
|
||||
code: Schema.optionalKey(Schema.String),
|
||||
status: Schema.optionalKey(Schema.Number),
|
||||
},
|
||||
) {}
|
||||
|
||||
export class OriginMismatch extends Schema.TaggedError<OriginMismatch>()(
|
||||
"AuthenticatedOrigin.OriginMismatch",
|
||||
{
|
||||
expectedOrigin: Schema.String,
|
||||
actualOrigin: Schema.String,
|
||||
message: Schema.String,
|
||||
},
|
||||
) {}
|
||||
|
||||
export class HttpError extends Schema.TaggedError<HttpError>()(
|
||||
"AuthenticatedOrigin.HttpError",
|
||||
{
|
||||
status: Schema.Number,
|
||||
method: Schema.String,
|
||||
message: Schema.String,
|
||||
},
|
||||
) {}
|
||||
|
||||
export class RequestFailed extends Schema.TaggedError<RequestFailed>()(
|
||||
"AuthenticatedOrigin.RequestFailed",
|
||||
{
|
||||
method: Schema.String,
|
||||
message: Schema.String,
|
||||
},
|
||||
) {}
|
||||
|
||||
export class RequestOutcomeUnknown extends Schema.TaggedError<RequestOutcomeUnknown>()(
|
||||
"AuthenticatedOrigin.RequestOutcomeUnknown",
|
||||
{
|
||||
method: Schema.String,
|
||||
message: Schema.String,
|
||||
},
|
||||
) {}
|
||||
|
||||
export class InvalidResponse extends Schema.TaggedError<InvalidResponse>()(
|
||||
"AuthenticatedOrigin.InvalidResponse",
|
||||
{
|
||||
reason: Schema.Literals(["invalid-json", "too-large"]),
|
||||
status: Schema.Number,
|
||||
maxResponseBytes: Schema.optionalKey(Schema.Number),
|
||||
message: Schema.String,
|
||||
},
|
||||
) {}
|
||||
|
||||
export class ResponseDecodeFailed extends Schema.TaggedError<ResponseDecodeFailed>()(
|
||||
"AuthenticatedOrigin.ResponseDecodeFailed",
|
||||
{
|
||||
message: Schema.String,
|
||||
},
|
||||
) {}
|
||||
|
||||
export class SensitiveCaptureActive extends Schema.TaggedError<SensitiveCaptureActive>()(
|
||||
"AuthenticatedOrigin.SensitiveCaptureActive",
|
||||
{
|
||||
message: Schema.String,
|
||||
},
|
||||
) {}
|
||||
|
||||
export type Error =
|
||||
| ClientError
|
||||
| OriginMismatch
|
||||
| HttpError
|
||||
| RequestFailed
|
||||
| RequestOutcomeUnknown
|
||||
| InvalidResponse
|
||||
| ResponseDecodeFailed
|
||||
| SensitiveCaptureActive
|
||||
|
||||
export interface JsonOptions<S extends Schema.Top> {
|
||||
readonly path: `/${string}`
|
||||
readonly method?: AuthenticatedJsonMethod
|
||||
readonly headers?: Readonly<Record<string, string>>
|
||||
readonly body?: Json
|
||||
readonly response: S
|
||||
readonly sensitive?: boolean
|
||||
readonly handoffOnAuthFailure?: boolean
|
||||
readonly handoffMessage?: string
|
||||
readonly timeoutMs?: number
|
||||
readonly maxResponseBytes?: number
|
||||
}
|
||||
|
||||
export interface AuthenticatedOriginCapability {
|
||||
readonly origin: string
|
||||
readonly json: {
|
||||
<S extends Schema.Top>(
|
||||
options: JsonOptions<S> & { readonly sensitive: true },
|
||||
): Effect.Effect<Redacted.Redacted<S["Type"]>, Error, S["DecodingServices"]>
|
||||
<S extends Schema.Top>(
|
||||
options: JsonOptions<S> & { readonly sensitive?: false },
|
||||
): Effect.Effect<S["Type"], Error, S["DecodingServices"]>
|
||||
}
|
||||
}
|
||||
|
||||
export type AuthenticatedOrigin = AuthenticatedOriginCapability
|
||||
|
||||
/** Reveal a sensitive response using OpenCode Browser's Effect runtime. */
|
||||
export const reveal = <A>(value: Redacted.Redacted<A>): A => Redacted.value(value)
|
||||
|
||||
export interface AuthenticatedOriginOptions {
|
||||
readonly origin: string
|
||||
/** Explicitly navigate here when the session page is not already on `origin`. */
|
||||
readonly startUrl?: string
|
||||
/** Default headers merged into every request on this origin. */
|
||||
readonly headers?: Readonly<Record<string, string>>
|
||||
/** Present an in-page handoff prompt and retry once if the session is logged out or redirected to auth. */
|
||||
readonly handoffOnAuthFailure?: boolean
|
||||
readonly handoffMessage?: string
|
||||
}
|
||||
|
||||
export interface OriginClientOptions extends Omit<AuthenticatedOriginOptions, "origin"> {
|
||||
readonly session: string
|
||||
readonly readOnly?: boolean
|
||||
readonly endpoint?: string
|
||||
}
|
||||
|
||||
export interface OriginClient {
|
||||
readonly origin: string
|
||||
readonly session: string
|
||||
readonly get: <T = unknown>(
|
||||
path: `/${string}`,
|
||||
options?: { readonly headers?: Readonly<Record<string, string>>; readonly timeoutMs?: number },
|
||||
) => Promise<T>
|
||||
readonly post: <T = unknown>(
|
||||
path: `/${string}`,
|
||||
body?: Json,
|
||||
options?: { readonly headers?: Readonly<Record<string, string>>; readonly timeoutMs?: number },
|
||||
) => Promise<T>
|
||||
readonly json: AuthenticatedOriginCapability["json"]
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
readonly id: string
|
||||
readonly summary: SessionSummary
|
||||
readonly authenticatedOrigin: (
|
||||
options: AuthenticatedOriginOptions,
|
||||
) => Effect.Effect<AuthenticatedOriginCapability, ClientError>
|
||||
}
|
||||
|
||||
export interface EnsureSessionOptions {
|
||||
readonly id: string
|
||||
readonly readOnly?: boolean
|
||||
}
|
||||
|
||||
export interface Interface {
|
||||
readonly ensureSession: (
|
||||
options: EnsureSessionOptions,
|
||||
) => Effect.Effect<Session, ClientError>
|
||||
readonly resetSession: (id: string) => Effect.Effect<Session, ClientError>
|
||||
}
|
||||
|
||||
export interface MakeOptions {
|
||||
readonly endpoint?: string
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Interface>()(
|
||||
"opencode-browser-cli/OpenCodeBrowserClient",
|
||||
) {}
|
||||
|
||||
export const make = Effect.fn("OpenCodeBrowserClient.make")(function* (options: MakeOptions = {}) {
|
||||
const relay = yield* RelayClient.make(options).pipe(
|
||||
Effect.provide(NodeHttpClient.layerNodeHttp),
|
||||
Effect.mapError((error) => clientError("connect", error)),
|
||||
)
|
||||
const readiness = yield* RelayLifecycle.ensureRelay({
|
||||
relay,
|
||||
// The consumer may run under Bun; the relay is a Node application.
|
||||
start: RelayLifecycle.startManagedRelay(fileURLToPath(import.meta.url), "node", []),
|
||||
}).pipe(Effect.mapError((error) => clientError("connect", error)))
|
||||
if (readiness.buildProblem) {
|
||||
return yield* new ClientError({
|
||||
message: readiness.buildProblem,
|
||||
reason: "connect",
|
||||
})
|
||||
}
|
||||
yield* RelayLifecycle.ensureExtensionConnected({
|
||||
relay,
|
||||
waitForReconnect: true,
|
||||
}).pipe(Effect.mapError((error) => clientError("connect", error)))
|
||||
|
||||
const ensureSession = Effect.fn("OpenCodeBrowserClient.ensureSession")(function* (
|
||||
sessionOptions: EnsureSessionOptions,
|
||||
) {
|
||||
const summary = yield* relay.sessionEnsure(sessionOptions.id, {
|
||||
...(sessionOptions.readOnly === undefined ? {} : { readOnly: sessionOptions.readOnly }),
|
||||
}).pipe(Effect.mapError((error) => clientError("session", error)))
|
||||
return makeSession(relay, summary)
|
||||
})
|
||||
|
||||
const resetSession = Effect.fn("OpenCodeBrowserClient.resetSession")(function* (id: string) {
|
||||
const summary = yield* relay.sessionReset(id).pipe(
|
||||
Effect.mapError((error) => clientError("session", error)),
|
||||
)
|
||||
return makeSession(relay, summary)
|
||||
})
|
||||
|
||||
return Service.of({ ensureSession, resetSession })
|
||||
})
|
||||
|
||||
export const layer = (options: MakeOptions = {}): Layer.Layer<Service, ClientError> =>
|
||||
Layer.effect(Service, make(options))
|
||||
|
||||
export function origin(originUrl: string, options: OriginClientOptions): OriginClient {
|
||||
const normalizedOrigin = AuthenticatedOriginInternal.normalizeOrigin(originUrl)
|
||||
let cachedOrigin: Promise<AuthenticatedOriginCapability> | undefined
|
||||
const resolveOrigin = (): Promise<AuthenticatedOriginCapability> => {
|
||||
cachedOrigin ??= Effect.runPromise(
|
||||
Effect.gen(function* () {
|
||||
const client = yield* make(options.endpoint ? { endpoint: options.endpoint } : {})
|
||||
const liveSession = yield* client.ensureSession({
|
||||
id: options.session,
|
||||
...(options.readOnly === undefined ? {} : { readOnly: options.readOnly }),
|
||||
})
|
||||
return yield* liveSession.authenticatedOrigin({
|
||||
origin: normalizedOrigin,
|
||||
startUrl: options.startUrl ?? "/",
|
||||
...(options.headers ? { headers: options.headers } : {}),
|
||||
...(options.handoffOnAuthFailure === undefined ? {} : { handoffOnAuthFailure: options.handoffOnAuthFailure }),
|
||||
...(options.handoffMessage ? { handoffMessage: options.handoffMessage } : {}),
|
||||
})
|
||||
}),
|
||||
).catch((error) => {
|
||||
cachedOrigin = undefined
|
||||
throw error
|
||||
})
|
||||
return cachedOrigin
|
||||
}
|
||||
const json: AuthenticatedOriginCapability["json"] = ((request: JsonOptions<Schema.Top>) =>
|
||||
Effect.promise(resolveOrigin).pipe(
|
||||
Effect.flatMap((cap) => cap.json(request as JsonOptions<Schema.Top> & { readonly sensitive?: false })),
|
||||
)) as AuthenticatedOriginCapability["json"]
|
||||
|
||||
return {
|
||||
origin: normalizedOrigin,
|
||||
session: options.session,
|
||||
get: async <T = unknown>(path: `/${string}`, callOptions?: { readonly headers?: Readonly<Record<string, string>>; readonly timeoutMs?: number }) => {
|
||||
const cap = await resolveOrigin()
|
||||
return (await Effect.runPromise(cap.json({
|
||||
path,
|
||||
method: "GET",
|
||||
...(callOptions?.headers ? { headers: callOptions.headers } : {}),
|
||||
...(callOptions?.timeoutMs === undefined ? {} : { timeoutMs: callOptions.timeoutMs }),
|
||||
response: Schema.Json,
|
||||
}))) as T
|
||||
},
|
||||
post: async <T = unknown>(path: `/${string}`, body?: Json, callOptions?: { readonly headers?: Readonly<Record<string, string>>; readonly timeoutMs?: number }) => {
|
||||
const cap = await resolveOrigin()
|
||||
return (await Effect.runPromise(cap.json({
|
||||
path,
|
||||
method: "POST",
|
||||
...(body === undefined ? {} : { body }),
|
||||
...(callOptions?.headers ? { headers: callOptions.headers } : {}),
|
||||
...(callOptions?.timeoutMs === undefined ? {} : { timeoutMs: callOptions.timeoutMs }),
|
||||
response: Schema.Json,
|
||||
}))) as T
|
||||
},
|
||||
json,
|
||||
}
|
||||
}
|
||||
|
||||
function makeSession(relay: RelayClient.Interface, summary: SessionSummary): Session {
|
||||
return {
|
||||
id: summary.id,
|
||||
summary,
|
||||
authenticatedOrigin: (options) => Effect.try({
|
||||
try: () => makeAuthenticatedOrigin(relay, summary.id, options),
|
||||
catch: (cause) => cause instanceof ClientError
|
||||
? cause
|
||||
: new ClientError({
|
||||
message: cause instanceof globalThis.Error ? cause.message : "Invalid authenticated origin",
|
||||
reason: "invalid-request",
|
||||
}),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
function makeAuthenticatedOrigin(
|
||||
relay: RelayClient.Interface,
|
||||
sessionId: string,
|
||||
options: AuthenticatedOriginOptions,
|
||||
): AuthenticatedOrigin {
|
||||
const origin = AuthenticatedOriginInternal.normalizeOrigin(options.origin)
|
||||
const startUrl = options.startUrl === undefined ? undefined : AuthenticatedOriginInternal.resolveStartUrl(origin, options.startUrl)
|
||||
|
||||
function json<S extends Schema.Top>(
|
||||
request: JsonOptions<S> & { readonly sensitive: true },
|
||||
): Effect.Effect<Redacted.Redacted<S["Type"]>, Error, S["DecodingServices"]>
|
||||
function json<S extends Schema.Top>(
|
||||
request: JsonOptions<S> & { readonly sensitive?: false },
|
||||
): Effect.Effect<S["Type"], Error, S["DecodingServices"]>
|
||||
function json<S extends Schema.Top>(
|
||||
request: JsonOptions<S>,
|
||||
): Effect.Effect<S["Type"] | Redacted.Redacted<S["Type"]>, Error, S["DecodingServices"]> {
|
||||
const method = request.method ?? "GET"
|
||||
if (method === "GET" && request.body !== undefined) {
|
||||
return Effect.fail(new ClientError({
|
||||
message: "GET authenticated origin requests cannot include a body",
|
||||
reason: "invalid-request",
|
||||
}))
|
||||
}
|
||||
const mutation = method !== "GET"
|
||||
const mergedHeaders =
|
||||
options.headers || request.headers
|
||||
? { ...(options.headers ?? {}), ...(request.headers ?? {}) }
|
||||
: undefined
|
||||
const handoffOnAuthFailure = request.handoffOnAuthFailure ?? options.handoffOnAuthFailure
|
||||
const handoffMessage = request.handoffMessage ?? options.handoffMessage
|
||||
return relay.authenticatedJson({
|
||||
sessionId,
|
||||
origin,
|
||||
...(startUrl ? { startUrl } : {}),
|
||||
method,
|
||||
path: request.path,
|
||||
...(mergedHeaders ? { headers: mergedHeaders } : {}),
|
||||
...(request.body === undefined ? {} : { body: request.body }),
|
||||
...(request.sensitive === true ? { sensitive: true } : {}),
|
||||
...(handoffOnAuthFailure === undefined ? {} : { handoffOnAuthFailure }),
|
||||
...(handoffMessage === undefined ? {} : { handoffMessage }),
|
||||
...(request.timeoutMs === undefined ? {} : { timeoutMs: request.timeoutMs }),
|
||||
...(request.maxResponseBytes === undefined ? {} : { maxResponseBytes: request.maxResponseBytes }),
|
||||
}).pipe(
|
||||
Effect.mapError((error): Error => mutation && (
|
||||
error instanceof RelayClient.RelayUnreachable || error instanceof RelayClient.RelayDecodeFailed
|
||||
)
|
||||
? unknownOutcome(method)
|
||||
: clientError("session", error)),
|
||||
Effect.flatMap((outcome) => decodeOutcome(outcome, method, mutation, request)),
|
||||
)
|
||||
}
|
||||
|
||||
return { origin, json }
|
||||
}
|
||||
|
||||
function decodeOutcome<S extends Schema.Top>(
|
||||
outcome: AuthenticatedJsonOutcome,
|
||||
method: AuthenticatedJsonMethod,
|
||||
mutation: boolean,
|
||||
request: JsonOptions<S>,
|
||||
): Effect.Effect<S["Type"] | Redacted.Redacted<S["Type"]>, Error, S["DecodingServices"]> {
|
||||
return Match.valueTags(outcome, {
|
||||
Success: (outcome) => {
|
||||
if (request.sensitive === true) {
|
||||
return Schema.decodeUnknownEffect(Schema.RedactedFromValue(request.response, {
|
||||
label: "OpenCode Browser authenticated response",
|
||||
disallowEncode: true,
|
||||
}))(outcome.value).pipe(
|
||||
Effect.mapError(() => mutation
|
||||
? unknownOutcome(method)
|
||||
: new ResponseDecodeFailed({ message: "Sensitive authenticated response did not match the expected schema" })),
|
||||
)
|
||||
}
|
||||
return Schema.decodeUnknownEffect(request.response)(outcome.value).pipe(
|
||||
Effect.mapError((cause) => mutation
|
||||
? unknownOutcome(method)
|
||||
: new ResponseDecodeFailed({ message: `Authenticated response did not match the expected schema: ${cause.message}` })),
|
||||
)
|
||||
},
|
||||
OriginMismatch: (outcome) =>
|
||||
Effect.fail(new OriginMismatch({
|
||||
expectedOrigin: outcome.expectedOrigin,
|
||||
actualOrigin: outcome.actualOrigin,
|
||||
message: `Session page origin ${outcome.actualOrigin} does not match ${outcome.expectedOrigin}`,
|
||||
})),
|
||||
HttpError: (outcome) =>
|
||||
Effect.fail(new HttpError({
|
||||
status: outcome.status,
|
||||
method,
|
||||
message: `Authenticated ${method} request was rejected with HTTP ${outcome.status}`,
|
||||
})),
|
||||
RequestFailed: (outcome) =>
|
||||
Effect.fail(mutation || outcome.outcome === "unknown"
|
||||
? unknownOutcome(method)
|
||||
: new RequestFailed({ method, message: `Authenticated ${method} request failed before it was sent` })),
|
||||
InvalidJson: (outcome) =>
|
||||
Effect.fail(mutation
|
||||
? unknownOutcome(method)
|
||||
: new InvalidResponse({
|
||||
reason: "invalid-json",
|
||||
status: outcome.status,
|
||||
message: "Authenticated response was not valid JSON",
|
||||
})),
|
||||
ResponseTooLarge: (outcome) =>
|
||||
Effect.fail(mutation
|
||||
? unknownOutcome(method)
|
||||
: new InvalidResponse({
|
||||
reason: "too-large",
|
||||
status: outcome.status,
|
||||
maxResponseBytes: outcome.maxResponseBytes,
|
||||
message: `Authenticated response exceeded ${outcome.maxResponseBytes} bytes`,
|
||||
})),
|
||||
SensitiveCaptureActive: () =>
|
||||
Effect.fail(new SensitiveCaptureActive({
|
||||
message: "Sensitive authenticated requests are blocked while session network capture is active",
|
||||
})),
|
||||
})
|
||||
}
|
||||
|
||||
function unknownOutcome(method: AuthenticatedJsonMethod): RequestOutcomeUnknown {
|
||||
return new RequestOutcomeUnknown({
|
||||
method,
|
||||
message: `Authenticated ${method} request outcome is unknown; reconcile state before retrying`,
|
||||
})
|
||||
}
|
||||
|
||||
function clientError(reason: ClientError["reason"], error: unknown): ClientError {
|
||||
if (error instanceof ClientError) return error
|
||||
if (error instanceof RelayClient.RelayRejected) {
|
||||
return new ClientError({
|
||||
message: error.message,
|
||||
reason,
|
||||
status: error.status,
|
||||
...(error.code ? { code: error.code } : {}),
|
||||
})
|
||||
}
|
||||
return new ClientError({
|
||||
message: error instanceof globalThis.Error ? error.message : "OpenCode Browser request failed",
|
||||
reason,
|
||||
})
|
||||
}
|
||||
72
packages/browser-extension/cli/src/page-console.ts
Normal file
72
packages/browser-extension/cli/src/page-console.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
// Patchright never enables CDP's Runtime domain: a page can detect that domain (console arguments get inspected
|
||||
// by the debugger), and bot checks do. Without it, a page's console messages and uncaught errors never reach
|
||||
// execute. `pageConsole.start()` turns Runtime on for one page, through its own CDP session, when an agent needs
|
||||
// those logs; that page can then tell it is being debugged, so it stays off by default.
|
||||
export * as PageConsole from "./page-console.ts"
|
||||
import type { CDPSession, Page } from "patchright-core"
|
||||
import type { ExecuteLogEntry } from "./relay-schema.ts"
|
||||
|
||||
type Listener = (entry: ExecuteLogEntry) => void
|
||||
type RemoteObject = { readonly type: string; readonly value?: unknown; readonly unserializableValue?: string; readonly description?: string }
|
||||
type CallFrame = { readonly url: string; readonly lineNumber: number; readonly columnNumber: number }
|
||||
|
||||
const sessions = new WeakMap<Page, CDPSession>()
|
||||
const listeners = new WeakMap<Page, Set<Listener>>()
|
||||
|
||||
/** Starts capturing a page's console messages and uncaught errors into execute's logs. */
|
||||
export async function start(page: Page) {
|
||||
if (sessions.has(page)) return { capturing: true }
|
||||
const session = await page.context().newCDPSession(page)
|
||||
sessions.set(page, session)
|
||||
page.once("close", () => sessions.delete(page))
|
||||
// Enabling Runtime first replays what the page logged earlier; only what happens from now on is new.
|
||||
let live = false
|
||||
session.on("Runtime.consoleAPICalled", (event: { type: string; args: RemoteObject[]; stackTrace?: { callFrames: CallFrame[] } }) => {
|
||||
if (!live) return
|
||||
const frame = event.stackTrace?.callFrames[0]
|
||||
emit(page, {
|
||||
source: "page",
|
||||
type: event.type,
|
||||
text: event.args.map(describe).join(" "),
|
||||
...(frame ? { location: { url: frame.url, lineNumber: frame.lineNumber, columnNumber: frame.columnNumber } } : {}),
|
||||
})
|
||||
})
|
||||
session.on("Runtime.exceptionThrown", (event: { exceptionDetails: { text: string; exception?: RemoteObject } }) => {
|
||||
if (!live) return
|
||||
emit(page, { source: "page", type: "pageerror", text: event.exceptionDetails.exception?.description ?? event.exceptionDetails.text })
|
||||
})
|
||||
await session.send("Runtime.enable")
|
||||
live = true
|
||||
return { capturing: true }
|
||||
}
|
||||
|
||||
/** Stops capturing, so the page no longer sees a debugger. */
|
||||
export async function stop(page: Page) {
|
||||
const session = sessions.get(page)
|
||||
sessions.delete(page)
|
||||
if (!session) return { capturing: false }
|
||||
await session.send("Runtime.disable").catch(() => undefined)
|
||||
await session.detach().catch(() => undefined)
|
||||
return { capturing: false }
|
||||
}
|
||||
|
||||
export function capturing(page: Page) {
|
||||
return sessions.has(page)
|
||||
}
|
||||
|
||||
/** Receives a page's captured entries while registered; registering doesn't start a capture. */
|
||||
export function listen(page: Page, listener: Listener) {
|
||||
const set = listeners.get(page) ?? new Set<Listener>()
|
||||
listeners.set(page, set)
|
||||
set.add(listener)
|
||||
return () => set.delete(listener)
|
||||
}
|
||||
|
||||
function emit(page: Page, entry: ExecuteLogEntry) {
|
||||
for (const listener of listeners.get(page) ?? []) listener(entry)
|
||||
}
|
||||
|
||||
function describe(arg: RemoteObject) {
|
||||
if (arg.value !== undefined) return typeof arg.value === "string" ? arg.value : JSON.stringify(arg.value)
|
||||
return arg.unserializableValue ?? arg.description ?? arg.type
|
||||
}
|
||||
43
packages/browser-extension/cli/src/page-read-timeout.ts
Normal file
43
packages/browser-extension/cli/src/page-read-timeout.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import type { Page } from "patchright-core"
|
||||
|
||||
const installed = new WeakSet<object>()
|
||||
|
||||
export type PageReadOperation = "page.title" | "page.content"
|
||||
|
||||
/** A bounded read did not settle; this does not establish renderer unresponsiveness. */
|
||||
export class PageReadTimeoutError extends Error {
|
||||
constructor(
|
||||
readonly timeoutMs: number,
|
||||
readonly operation: PageReadOperation = "page.title",
|
||||
) {
|
||||
super(`${operation}() timed out after ${timeoutMs}ms: the page execution-context read did not complete; the context may be unavailable or busy.`)
|
||||
this.name = "PageReadTimeoutError"
|
||||
}
|
||||
}
|
||||
|
||||
function wrapBoundedPageRead(
|
||||
read: () => Promise<string>,
|
||||
operation: PageReadOperation,
|
||||
timeoutMs: number,
|
||||
): () => Promise<string> {
|
||||
return () => new Promise<string>((resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new PageReadTimeoutError(timeoutMs, operation)), timeoutMs)
|
||||
Promise.resolve().then(read).then(
|
||||
(value) => { clearTimeout(timer); resolve(value) },
|
||||
(error) => { clearTimeout(timer); reject(error) },
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/** Title and content are read-only, but Playwright passes kNoTimeout (timeout: 0) for their context waits. */
|
||||
export function installPageReadTimeout(
|
||||
page: Pick<Page, "title"> & Partial<Pick<Page, "content">>,
|
||||
timeoutMs = 5_000,
|
||||
): void {
|
||||
if (installed.has(page)) return
|
||||
installed.add(page)
|
||||
page.title = wrapBoundedPageRead(page.title.bind(page), "page.title", timeoutMs)
|
||||
if (page.content) {
|
||||
page.content = wrapBoundedPageRead(page.content.bind(page), "page.content", timeoutMs)
|
||||
}
|
||||
}
|
||||
59
packages/browser-extension/cli/src/patchright-tuning.ts
Normal file
59
packages/browser-extension/cli/src/patchright-tuning.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { createRequire } from "node:module"
|
||||
import { relayLog } from "./relay-log.ts"
|
||||
|
||||
type Progress = { race<T>(promise: Promise<T>): Promise<T> }
|
||||
type Selectors = {
|
||||
_hasClosedShadowRoots?: () => Promise<boolean>
|
||||
queryCount(selector: string): Promise<number>
|
||||
queryAll(selector: string): Promise<unknown[]>
|
||||
}
|
||||
type Frame = {
|
||||
selectors: Selectors
|
||||
isNonRetriableError(error: unknown): boolean
|
||||
queryCount?: (progress: Progress, selector: string) => Promise<number>
|
||||
querySelectorAll?: (progress: Progress, selector: string) => Promise<unknown[]>
|
||||
}
|
||||
type ServerPage = { mainFrame(): Frame }
|
||||
|
||||
/**
|
||||
* Patchright finds elements its own way so it can reach into closed shadow roots: it walks the DOM over CDP,
|
||||
* describing every match. Through the extension that's fine for actions, but calls that return many elements are
|
||||
* slow: on a 4,000-element page `getByRole("link").count()` took ~650 ms (Playwright: ~9 ms), and before every
|
||||
* count()/evaluateAll()/all() it also pulled the whole DOM tree (DOM.getDocument depth -1) to look for closed
|
||||
* shadow roots, ~0.2-2 s. Playwright never reached into closed shadow roots, so count() and querySelectorAll()
|
||||
* go back to Playwright's lookup (Patchright's injected script, in the isolated world, which pages can't see) and
|
||||
* the shadow-root scan is off.
|
||||
*
|
||||
* The classes aren't exported; they're reached through the first page's main frame. If a Patchright update renames
|
||||
* these internals, nothing breaks: lookups stay correct but slower, and relay.log says so.
|
||||
*/
|
||||
export function tunePatchright() {
|
||||
const { server } = createRequire(import.meta.url)("patchright-core/lib/coreBundle") as { server?: { Page?: { prototype: ServerPage } } }
|
||||
const page = server?.Page?.prototype
|
||||
const mainFrame = page?.mainFrame
|
||||
if (!page || typeof mainFrame !== "function") {
|
||||
relayLog("patchright.tuning", { applied: false, reason: "server.Page.mainFrame not found" })
|
||||
return
|
||||
}
|
||||
page.mainFrame = function () {
|
||||
const frame = mainFrame.call(this)
|
||||
page.mainFrame = mainFrame
|
||||
const frames = Object.getPrototypeOf(frame) as Frame
|
||||
const selectors = Object.getPrototypeOf(frame.selectors) as Selectors
|
||||
if (typeof selectors._hasClosedShadowRoots !== "function" || typeof frames.queryCount !== "function" || typeof frames.querySelectorAll !== "function") {
|
||||
relayLog("patchright.tuning", { applied: false, reason: "Frame or FrameSelectors internals changed" })
|
||||
return frame
|
||||
}
|
||||
selectors._hasClosedShadowRoots = async () => false
|
||||
frames.querySelectorAll = function (progress, selector) {
|
||||
return progress.race(this.selectors.queryAll(selector))
|
||||
}
|
||||
frames.queryCount = async function (progress, selector) {
|
||||
return progress.race(this.selectors.queryCount(selector)).catch((error: unknown) => {
|
||||
if (this.isNonRetriableError(error)) throw error
|
||||
return 0
|
||||
})
|
||||
}
|
||||
return frame
|
||||
}
|
||||
}
|
||||
16
packages/browser-extension/cli/src/paths.ts
Normal file
16
packages/browser-extension/cli/src/paths.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Where OpenCode Browser keeps its files: the installed runtime, the relay's log, session catalogs and
|
||||
// journals, and captured secrets. One root, so `uninstall` can remove it and logs are easy to find.
|
||||
// macOS/Linux: $XDG_DATA_HOME/opencode-browser, else ~/.local/share/opencode-browser
|
||||
// Windows: %LOCALAPPDATA%\opencode-browser
|
||||
// OPENCODE_BROWSER_HOME overrides it (isolated test relays).
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
|
||||
export function dataRoot(home = os.homedir()): string {
|
||||
const override = process.env.OPENCODE_BROWSER_HOME
|
||||
if (override) return override
|
||||
if (process.platform === "win32")
|
||||
return path.join(process.env.LOCALAPPDATA ?? path.join(home, "AppData", "Local"), "opencode-browser")
|
||||
const xdg = home === os.homedir() ? process.env.XDG_DATA_HOME : undefined
|
||||
return path.join(xdg ?? path.join(home, ".local", "share"), "opencode-browser")
|
||||
}
|
||||
109
packages/browser-extension/cli/src/protected-frames.ts
Normal file
109
packages/browser-extension/cli/src/protected-frames.ts
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
import { Predicate } from "effect"
|
||||
import type { JsonObject } from "./protocol.ts"
|
||||
import { getObject, getString, isRestrictedUrl } from "./relay-helpers.ts"
|
||||
|
||||
/**
|
||||
* How a root-session `Page.*` event about a child frame should reach CDP
|
||||
* clients once the relay knows the frame is protected.
|
||||
*
|
||||
* - `forward`: an ordinary frame event.
|
||||
* - `suppress`: the frame is protected; clients must not learn about it.
|
||||
* - `retract`: the frame just became protected after its attach was already
|
||||
* forwarded; emit a synthetic `Page.frameDetached` so clients drop it.
|
||||
* - `restore`: a protected frame navigated back to an ordinary document; emit
|
||||
* a synthetic `Page.frameAttached` before forwarding the navigation.
|
||||
*/
|
||||
export type ProtectedFrameDecision =
|
||||
| { readonly kind: "forward" }
|
||||
| { readonly kind: "suppress" }
|
||||
| { readonly kind: "retract"; readonly frameId: string }
|
||||
| { readonly kind: "restore"; readonly frameId: string; readonly parentFrameId: string }
|
||||
|
||||
const navigationIntentMethods = new Set(["Page.frameRequestedNavigation", "Page.frameScheduledNavigation", "Page.frameStartedNavigating"])
|
||||
|
||||
/**
|
||||
* Tracks child frames whose document is a restricted URL, such as a password
|
||||
* manager's `chrome-extension://` inline menu injected into the page.
|
||||
*
|
||||
* Chrome reports such a frame to the tab's root session like any other child
|
||||
* frame, but the relay can never expose its target, so a stock Playwright
|
||||
* client would keep an empty-URL phantom frame forever. While the frame exists
|
||||
* `chrome.debugger` also rejects every command for the tab; that block is
|
||||
* tracked separately by the relay and lifted when the last protected frame in
|
||||
* the tab goes away.
|
||||
*/
|
||||
export class ProtectedFrameTracker {
|
||||
private readonly framesByTab = new Map<number, Set<string>>()
|
||||
|
||||
has(tabId: number, frameId: string): boolean {
|
||||
return this.framesByTab.get(tabId)?.has(frameId) ?? false
|
||||
}
|
||||
|
||||
hasAny(tabId: number): boolean {
|
||||
return (this.framesByTab.get(tabId)?.size ?? 0) > 0
|
||||
}
|
||||
|
||||
/** Mark a frame protected. Returns true when it was not already tracked. */
|
||||
mark(tabId: number, frameId: string): boolean {
|
||||
const frames = this.framesByTab.get(tabId) ?? new Set<string>()
|
||||
if (frames.has(frameId)) return false
|
||||
frames.add(frameId)
|
||||
this.framesByTab.set(tabId, frames)
|
||||
return true
|
||||
}
|
||||
|
||||
/** Forget one frame. Returns true when it was the tab's last protected frame. */
|
||||
release(tabId: number, frameId: string): boolean {
|
||||
const frames = this.framesByTab.get(tabId)
|
||||
if (!frames?.delete(frameId)) return false
|
||||
if (frames.size > 0) return false
|
||||
this.framesByTab.delete(tabId)
|
||||
return true
|
||||
}
|
||||
|
||||
forgetTab(tabId: number): void {
|
||||
this.framesByTab.delete(tabId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Classify a root-session `Page.*` event. Only child frames are ever
|
||||
* protected; the main frame is left to root-target handling, so a frame is
|
||||
* marked only with positive evidence that it has a parent.
|
||||
*/
|
||||
observe(options: {
|
||||
readonly tabId: number
|
||||
readonly method: string
|
||||
readonly params: JsonObject | undefined
|
||||
readonly mainFrameId: string | undefined
|
||||
/** Whether a `Page.frameAttached` with a parent was seen for this frame. */
|
||||
readonly isChildFrame: (frameId: string) => boolean
|
||||
}): ProtectedFrameDecision {
|
||||
const { tabId, method, params } = options
|
||||
if (!method.startsWith("Page.") || !params) return { kind: "forward" }
|
||||
const frame = getObject(params.frame)
|
||||
const frameId = getString(params, "frameId") ?? getString(frame, "id")
|
||||
if (!frameId || frameId === options.mainFrameId) return { kind: "forward" }
|
||||
const frameUrl = getString(frame, "url")
|
||||
const parentId = getString(frame, "parentId")
|
||||
const intentUrl = navigationIntentMethods.has(method) ? getString(params, "url") : undefined
|
||||
|
||||
if (this.has(tabId, frameId)) {
|
||||
if (method === "Page.frameDetached") {
|
||||
this.release(tabId, frameId)
|
||||
return { kind: "suppress" }
|
||||
}
|
||||
if (method === "Page.frameNavigated" && frameUrl !== undefined && !isRestrictedUrl(frameUrl) && Predicate.isString(parentId)) {
|
||||
this.release(tabId, frameId)
|
||||
return { kind: "restore", frameId, parentFrameId: parentId }
|
||||
}
|
||||
return { kind: "suppress" }
|
||||
}
|
||||
|
||||
const restricted = intentUrl !== undefined
|
||||
? isRestrictedUrl(intentUrl) && options.isChildFrame(frameId)
|
||||
: method === "Page.frameNavigated" && Predicate.isString(parentId) && isRestrictedUrl(frameUrl)
|
||||
if (!restricted) return { kind: "forward" }
|
||||
this.mark(tabId, frameId)
|
||||
return { kind: "retract", frameId }
|
||||
}
|
||||
}
|
||||
193
packages/browser-extension/cli/src/protocol.ts
Normal file
193
packages/browser-extension/cli/src/protocol.ts
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
type JsonPrimitive = string | number | boolean | null
|
||||
|
||||
export type JsonValue = JsonPrimitive | JsonValue[] | { readonly [key: string]: JsonValue }
|
||||
|
||||
export type JsonObject = { readonly [key: string]: JsonValue }
|
||||
|
||||
export const extensionProtocolVersion = 2
|
||||
|
||||
/**
|
||||
* Chrome's minimum alarm period. A disconnected extension's MV3 worker sleeps
|
||||
* until this alarm fires, so relay clients must wait at least this long.
|
||||
*/
|
||||
export const extensionReconnectAlarmPeriodMs = 30_000
|
||||
const legacyExtensionProtocolVersion = 1
|
||||
|
||||
export type ExtensionProtocolCompatibility = {
|
||||
readonly version: number | null
|
||||
readonly compatible: boolean
|
||||
readonly legacy: boolean
|
||||
}
|
||||
|
||||
export function extensionProtocolCompatibility(value: JsonValue | undefined): ExtensionProtocolCompatibility {
|
||||
const legacy = value === undefined
|
||||
const valid = typeof value === "number" && Number.isSafeInteger(value) && value > 0
|
||||
const version = valid ? value : legacy ? legacyExtensionProtocolVersion : null
|
||||
return { version, compatible: version === extensionProtocolVersion, legacy }
|
||||
}
|
||||
|
||||
export type CdpRequest = {
|
||||
readonly id: number
|
||||
readonly method: string
|
||||
readonly params?: JsonObject
|
||||
readonly sessionId?: string
|
||||
}
|
||||
|
||||
export type CdpResponse = {
|
||||
readonly id: number
|
||||
readonly result?: JsonObject
|
||||
readonly error?: {
|
||||
readonly message: string
|
||||
}
|
||||
readonly sessionId?: string
|
||||
}
|
||||
|
||||
export type CdpEvent = {
|
||||
readonly method: string
|
||||
readonly params?: JsonObject
|
||||
readonly sessionId?: string
|
||||
}
|
||||
|
||||
export type TargetInfo = {
|
||||
readonly targetId: string
|
||||
readonly type: "page" | "iframe" | "worker"
|
||||
readonly title: string
|
||||
readonly url: string
|
||||
readonly attached: boolean
|
||||
readonly canAccessOpener: boolean
|
||||
readonly browserContextId?: string
|
||||
readonly openerId?: string
|
||||
readonly parentFrameId?: string
|
||||
}
|
||||
|
||||
export type PageStatus = {
|
||||
readonly state: "attached" | "running" | "waiting"
|
||||
readonly owner: "session" | "user"
|
||||
readonly sessionId?: string
|
||||
readonly readOnly?: boolean
|
||||
readonly message?: string
|
||||
readonly handoffId?: string
|
||||
}
|
||||
|
||||
export function makePageStatus(options: {
|
||||
readonly state: PageStatus["state"]
|
||||
readonly targetOwner: "relay" | "user"
|
||||
readonly sessionId?: string
|
||||
readonly readOnly?: boolean
|
||||
readonly message?: string
|
||||
readonly handoffId?: string
|
||||
}): PageStatus {
|
||||
return {
|
||||
state: options.state,
|
||||
owner: options.targetOwner === "user" ? "user" : "session",
|
||||
...(options.sessionId ? { sessionId: options.sessionId } : {}),
|
||||
...(options.sessionId && options.readOnly ? { readOnly: true } : {}),
|
||||
...(options.message ? { message: options.message } : {}),
|
||||
...(options.handoffId ? { handoffId: options.handoffId } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
const extensionCommandMethodValues = [
|
||||
"ping",
|
||||
"debugger.attach",
|
||||
"debugger.detach",
|
||||
"debugger.sendCommand",
|
||||
"tabs.create",
|
||||
"tabs.remove",
|
||||
"tabs.group",
|
||||
"tabs.ungroup",
|
||||
"action.setAttached",
|
||||
"action.setBadge",
|
||||
"pageStatus.set",
|
||||
"pageStatus.clear",
|
||||
"runtime.reload",
|
||||
"recording.start",
|
||||
"recording.stop",
|
||||
"recording.status",
|
||||
"recording.cancel",
|
||||
"tabs.cleanup",
|
||||
] as const
|
||||
|
||||
const extensionEventMethodValues = [
|
||||
"hello",
|
||||
"ready",
|
||||
"toolbar.clicked",
|
||||
"handoff.completed",
|
||||
"debugger.event",
|
||||
"debugger.attached",
|
||||
"debugger.detached",
|
||||
"tabs.removed",
|
||||
"pong",
|
||||
"log",
|
||||
"recording.cancelled",
|
||||
"pageStatus.requested",
|
||||
] as const
|
||||
|
||||
type ExtensionCommandMethod = typeof extensionCommandMethodValues[number]
|
||||
type ExtensionEventMethod = typeof extensionEventMethodValues[number]
|
||||
|
||||
export type ExtensionCommand = {
|
||||
readonly id: number
|
||||
readonly method: ExtensionCommandMethod
|
||||
readonly params?: JsonObject
|
||||
}
|
||||
|
||||
export type ExtensionResponse = {
|
||||
readonly id: number
|
||||
readonly result?: JsonObject
|
||||
readonly error?: string
|
||||
}
|
||||
|
||||
export type ExtensionEvent = {
|
||||
readonly method: ExtensionEventMethod
|
||||
readonly params?: JsonObject
|
||||
}
|
||||
|
||||
export function parseJsonObject(input: string): JsonObject {
|
||||
const parsed: unknown = JSON.parse(input)
|
||||
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
||||
throw new Error("Expected JSON object")
|
||||
}
|
||||
return parsed as JsonObject
|
||||
}
|
||||
|
||||
const extensionCommandMethods = new Set<string>(extensionCommandMethodValues)
|
||||
|
||||
const extensionEventMethods = new Set<string>(extensionEventMethodValues)
|
||||
|
||||
export function parseExtensionCommand(input: string): ExtensionCommand {
|
||||
const parsed = parseJsonObject(input)
|
||||
if (
|
||||
typeof parsed.id !== "number" ||
|
||||
typeof parsed.method !== "string" ||
|
||||
!extensionCommandMethods.has(parsed.method) ||
|
||||
(parsed.params !== undefined && !isJsonObject(parsed.params))
|
||||
) {
|
||||
throw new Error("Invalid extension command")
|
||||
}
|
||||
return parsed as ExtensionCommand
|
||||
}
|
||||
|
||||
export function isCdpRequest(input: JsonObject): input is CdpRequest {
|
||||
return typeof input.id === "number" &&
|
||||
typeof input.method === "string" &&
|
||||
(input.params === undefined || isJsonObject(input.params)) &&
|
||||
(input.sessionId === undefined || typeof input.sessionId === "string")
|
||||
}
|
||||
|
||||
export function isExtensionResponse(input: JsonObject): input is ExtensionResponse {
|
||||
return typeof input.id === "number" &&
|
||||
(input.result === undefined || isJsonObject(input.result)) &&
|
||||
(input.error === undefined || typeof input.error === "string") &&
|
||||
!(input.result !== undefined && input.error !== undefined)
|
||||
}
|
||||
|
||||
export function isExtensionEvent(input: JsonObject): input is ExtensionEvent {
|
||||
return typeof input.method === "string" &&
|
||||
extensionEventMethods.has(input.method) &&
|
||||
(input.params === undefined || isJsonObject(input.params))
|
||||
}
|
||||
|
||||
export function isJsonObject(value: unknown): value is JsonObject {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value)
|
||||
}
|
||||
62
packages/browser-extension/cli/src/recording-protocol.ts
Normal file
62
packages/browser-extension/cli/src/recording-protocol.ts
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
const magic = [0x42, 0x43, 0x52, 0x44] as const
|
||||
const version = 1
|
||||
const finalFlag = 1
|
||||
const headerLength = 20
|
||||
|
||||
export const maxRecordingFramePayloadBytes = 4 * 1024 * 1024
|
||||
|
||||
export type RecordingFrame = {
|
||||
readonly tabId: number
|
||||
readonly sequence: number
|
||||
readonly final: boolean
|
||||
readonly payload: Uint8Array
|
||||
}
|
||||
|
||||
export function encodeRecordingFrame(frame: RecordingFrame): Uint8Array {
|
||||
validateUint32(frame.tabId, "tabId", true)
|
||||
validateUint32(frame.sequence, "sequence", false)
|
||||
if (frame.payload.byteLength > maxRecordingFramePayloadBytes) {
|
||||
throw new Error(`Recording frame payload exceeds ${maxRecordingFramePayloadBytes} bytes`)
|
||||
}
|
||||
if (frame.final && frame.payload.byteLength !== 0) throw new Error("Final recording frame must have an empty payload")
|
||||
if (!frame.final && frame.payload.byteLength === 0) throw new Error("Recording data frame must have a payload")
|
||||
const encoded = new Uint8Array(headerLength + frame.payload.byteLength)
|
||||
encoded.set(magic, 0)
|
||||
const view = new DataView(encoded.buffer)
|
||||
view.setUint8(4, version)
|
||||
view.setUint8(5, frame.final ? finalFlag : 0)
|
||||
view.setUint16(6, headerLength)
|
||||
view.setUint32(8, frame.tabId)
|
||||
view.setUint32(12, frame.sequence)
|
||||
view.setUint32(16, frame.payload.byteLength)
|
||||
encoded.set(frame.payload, headerLength)
|
||||
return encoded
|
||||
}
|
||||
|
||||
export function decodeRecordingFrame(data: Uint8Array): RecordingFrame {
|
||||
if (data.byteLength < headerLength) throw new Error("Recording frame is shorter than its header")
|
||||
if (magic.some((byte, index) => data[index] !== byte)) throw new Error("Invalid recording frame magic")
|
||||
const view = new DataView(data.buffer, data.byteOffset, data.byteLength)
|
||||
if (view.getUint8(4) !== version) throw new Error(`Unsupported recording frame version: ${view.getUint8(4)}`)
|
||||
const flags = view.getUint8(5)
|
||||
if ((flags & ~finalFlag) !== 0) throw new Error(`Invalid recording frame flags: ${flags}`)
|
||||
if (view.getUint16(6) !== headerLength) throw new Error(`Invalid recording frame header length: ${view.getUint16(6)}`)
|
||||
const tabId = view.getUint32(8)
|
||||
const sequence = view.getUint32(12)
|
||||
const payloadLength = view.getUint32(16)
|
||||
if (tabId === 0) throw new Error("Recording frame tabId must be positive")
|
||||
if (payloadLength > maxRecordingFramePayloadBytes) {
|
||||
throw new Error(`Recording frame payload exceeds ${maxRecordingFramePayloadBytes} bytes`)
|
||||
}
|
||||
if (data.byteLength !== headerLength + payloadLength) throw new Error("Recording frame payload length does not match its header")
|
||||
const final = (flags & finalFlag) !== 0
|
||||
if (final && payloadLength !== 0) throw new Error("Final recording frame must have an empty payload")
|
||||
if (!final && payloadLength === 0) throw new Error("Recording data frame must have a payload")
|
||||
return { tabId, sequence, final, payload: data.subarray(headerLength) }
|
||||
}
|
||||
|
||||
function validateUint32(value: number, field: string, positive: boolean): void {
|
||||
if (!Number.isInteger(value) || value < (positive ? 1 : 0) || value > 0xffff_ffff) {
|
||||
throw new Error(`Recording frame ${field} must be ${positive ? "a positive" : "an"} uint32`)
|
||||
}
|
||||
}
|
||||
1199
packages/browser-extension/cli/src/recording-relay.ts
Normal file
1199
packages/browser-extension/cli/src/recording-relay.ts
Normal file
File diff suppressed because it is too large
Load diff
312
packages/browser-extension/cli/src/relay-client.ts
Normal file
312
packages/browser-extension/cli/src/relay-client.ts
Normal file
|
|
@ -0,0 +1,312 @@
|
|||
import { defaultPort } from "./relay-helpers.ts"
|
||||
import { NodeHttpClient } from "@effect/platform-node"
|
||||
import { Config, Context, Effect, Layer, Option, Schema } from "effect"
|
||||
import { HttpClient, HttpClientRequest, type HttpClientResponse } from "effect/http"
|
||||
import {
|
||||
type AuthProfileRequest,
|
||||
type AuthenticatedJsonRequest,
|
||||
AuthenticatedJsonOutcome,
|
||||
AuthProfileSummary,
|
||||
type AuthRefreshRequest,
|
||||
type AuthRunRequest,
|
||||
AuthRunResponse,
|
||||
ErrorEnvelope,
|
||||
type ExecuteRequest,
|
||||
ExecuteResponse,
|
||||
ExtensionStatus,
|
||||
FlightRecorderCancelResponse,
|
||||
type FlightRecorderSaveRequest,
|
||||
FlightRecorderSaveResponse,
|
||||
type FlightRecorderStartRequest,
|
||||
FlightRecorderStatusResponse,
|
||||
NetworkCancelResponse,
|
||||
type NetworkSessionRequest,
|
||||
type NetworkStartRequest,
|
||||
NetworkStatusResponse,
|
||||
type NetworkStopRequest,
|
||||
NetworkStopResponse,
|
||||
RecordingCancelResponse,
|
||||
TabsCleanupResponse,
|
||||
type RecordingStartRequest,
|
||||
RecordingStartResponse,
|
||||
RecordingStatusResponse,
|
||||
RecordingStopResponse,
|
||||
type RecordingTargetRequest,
|
||||
RelayErrorCode,
|
||||
type RelayShutdownRequest,
|
||||
RelayShutdownResponse,
|
||||
RelayVersion,
|
||||
SessionAdoptResponse,
|
||||
SessionContainer,
|
||||
SessionDeleted,
|
||||
SessionsContainer,
|
||||
TargetSummaries,
|
||||
type SessionAdoptRequest,
|
||||
type SessionSummary,
|
||||
type TargetSummary,
|
||||
} from "./relay-schema.ts"
|
||||
|
||||
/**
|
||||
* RelayClient is the single typed client for the relay HTTP API, shared by the
|
||||
* CLI and the MCP server. All responses are decoded against the shared wire
|
||||
* schemas in `src/relay-schema.ts`, and failures are tagged errors that keep
|
||||
* the relay's own error message as the top-level message.
|
||||
*/
|
||||
|
||||
export const portConfig = Config.Int("OPENCODE_BROWSER_PORT").pipe(Config.withDefault(defaultPort))
|
||||
|
||||
/**
|
||||
* Extra `chrome-extension://<id>` origins to allowlist for the extension
|
||||
* WebSocket, comma- or whitespace-separated. This supports same-host unpacked
|
||||
* installs whose path-derived id cannot match the relay's bundled path. Parsed
|
||||
* by `parseAdditionalExtensionOrigins`.
|
||||
*/
|
||||
export const extensionOriginsConfig = Config.String("OPENCODE_BROWSER_EXTENSION_ORIGINS").pipe(Config.withDefault(""))
|
||||
|
||||
export const endpointForPort = (port: number): string => `http://127.0.0.1:${port}`
|
||||
|
||||
export class RelayUnreachable extends Schema.TaggedError<RelayUnreachable>()(
|
||||
"RelayClient.RelayUnreachable",
|
||||
{
|
||||
message: Schema.String,
|
||||
endpoint: Schema.String,
|
||||
path: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {}
|
||||
|
||||
export class RelayRejected extends Schema.TaggedError<RelayRejected>()(
|
||||
"RelayClient.RelayRejected",
|
||||
{
|
||||
message: Schema.String,
|
||||
status: Schema.Number,
|
||||
path: Schema.String,
|
||||
code: Schema.optionalKey(RelayErrorCode),
|
||||
},
|
||||
) {}
|
||||
|
||||
export class RelayDecodeFailed extends Schema.TaggedError<RelayDecodeFailed>()(
|
||||
"RelayClient.RelayDecodeFailed",
|
||||
{
|
||||
message: Schema.String,
|
||||
path: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {}
|
||||
|
||||
class RelayEncodeFailed extends Schema.TaggedError<RelayEncodeFailed>()(
|
||||
"RelayClient.RelayEncodeFailed",
|
||||
{
|
||||
message: Schema.String,
|
||||
path: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {}
|
||||
|
||||
export class RelayConfigInvalid extends Schema.TaggedError<RelayConfigInvalid>()(
|
||||
"RelayClient.RelayConfigInvalid",
|
||||
{
|
||||
message: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {}
|
||||
|
||||
export type RelayClientError = RelayUnreachable | RelayRejected | RelayDecodeFailed | RelayEncodeFailed
|
||||
|
||||
export interface Interface {
|
||||
readonly endpoint: string
|
||||
readonly version: Effect.Effect<RelayVersion, RelayClientError>
|
||||
readonly shutdown: (request: RelayShutdownRequest) => Effect.Effect<RelayShutdownResponse, RelayClientError>
|
||||
readonly extensionStatus: Effect.Effect<ExtensionStatus, RelayClientError>
|
||||
readonly targets: Effect.Effect<readonly TargetSummary[], RelayClientError>
|
||||
readonly sessions: Effect.Effect<readonly SessionSummary[], RelayClientError>
|
||||
readonly sessionNew: (id?: string | undefined, options?: { readonly readOnly?: boolean }) => Effect.Effect<SessionSummary, RelayClientError>
|
||||
readonly sessionEnsure: (id: string, options?: { readonly readOnly?: boolean }) => Effect.Effect<SessionSummary, RelayClientError>
|
||||
readonly sessionReset: (id: string) => Effect.Effect<SessionSummary, RelayClientError>
|
||||
readonly sessionAdopt: (request: SessionAdoptRequest) => Effect.Effect<SessionAdoptResponse, RelayClientError>
|
||||
readonly sessionDelete: (id: string) => Effect.Effect<SessionDeleted, RelayClientError>
|
||||
readonly execute: (request: ExecuteRequest) => Effect.Effect<ExecuteResponse, RelayClientError>
|
||||
readonly authenticatedJson: (request: AuthenticatedJsonRequest) => Effect.Effect<AuthenticatedJsonOutcome, RelayClientError>
|
||||
readonly networkStart: (request: NetworkStartRequest) => Effect.Effect<NetworkStatusResponse, RelayClientError>
|
||||
readonly networkStatus: (request: NetworkSessionRequest) => Effect.Effect<NetworkStatusResponse, RelayClientError>
|
||||
readonly networkStop: (request: NetworkStopRequest) => Effect.Effect<NetworkStopResponse, RelayClientError>
|
||||
readonly networkCancel: (request: NetworkSessionRequest) => Effect.Effect<NetworkCancelResponse, RelayClientError>
|
||||
readonly authStatus: (request: AuthProfileRequest) => Effect.Effect<AuthProfileSummary, RelayClientError>
|
||||
readonly authRefresh: (request: AuthRefreshRequest) => Effect.Effect<NetworkStopResponse, RelayClientError>
|
||||
readonly authRun: (request: AuthRunRequest) => Effect.Effect<AuthRunResponse, RelayClientError>
|
||||
readonly recordingStart: (request: RecordingStartRequest) => Effect.Effect<RecordingStartResponse, RelayClientError>
|
||||
readonly recordingStop: (target: RecordingTargetRequest) => Effect.Effect<RecordingStopResponse, RelayClientError>
|
||||
readonly recordingStatus: (target: RecordingTargetRequest) => Effect.Effect<RecordingStatusResponse, RelayClientError>
|
||||
readonly recordingCancel: (target: RecordingTargetRequest) => Effect.Effect<RecordingCancelResponse, RelayClientError>
|
||||
readonly tabsCleanup: (idleMinutes?: number) => Effect.Effect<TabsCleanupResponse, RelayClientError>
|
||||
readonly flightRecorderStart: (request: FlightRecorderStartRequest) => Effect.Effect<FlightRecorderStatusResponse, RelayClientError>
|
||||
readonly flightRecorderStatus: (target: RecordingTargetRequest) => Effect.Effect<FlightRecorderStatusResponse, RelayClientError>
|
||||
readonly flightRecorderSaveLast: (request: FlightRecorderSaveRequest) => Effect.Effect<FlightRecorderSaveResponse, RelayClientError>
|
||||
readonly flightRecorderCancel: (target: RecordingTargetRequest) => Effect.Effect<FlightRecorderCancelResponse, RelayClientError>
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Interface>()("opencode-browser-cli/RelayClient") {}
|
||||
|
||||
const decodeErrorEnvelope = Schema.decodeUnknownOption(ErrorEnvelope)
|
||||
const decodeErrorMessage = Schema.decodeUnknownOption(Schema.Struct({ error: Schema.String }))
|
||||
|
||||
export const make = Effect.fn("RelayClient.make")(function* (options?: { readonly endpoint?: string }) {
|
||||
const port = yield* portConfig.pipe(
|
||||
Effect.mapError((cause) => new RelayConfigInvalid({
|
||||
message: `Invalid OPENCODE_BROWSER_PORT configuration: ${cause.message}`,
|
||||
cause,
|
||||
})),
|
||||
)
|
||||
const endpoint = options?.endpoint ?? endpointForPort(port)
|
||||
const httpClient = yield* HttpClient.HttpClient
|
||||
|
||||
const readBody = (
|
||||
response: HttpClientResponse.HttpClientResponse,
|
||||
path: string,
|
||||
): Effect.Effect<unknown, RelayDecodeFailed> =>
|
||||
response.json.pipe(
|
||||
Effect.mapError((cause) =>
|
||||
new RelayDecodeFailed({
|
||||
message: `Relay returned an unreadable response for ${path}`,
|
||||
path,
|
||||
cause,
|
||||
})
|
||||
),
|
||||
)
|
||||
|
||||
const handleResponse = <A>(
|
||||
response: HttpClientResponse.HttpClientResponse,
|
||||
path: string,
|
||||
schema: Schema.ConstraintDecoder<A>,
|
||||
): Effect.Effect<A, RelayClientError> =>
|
||||
readBody(response, path).pipe(
|
||||
Effect.flatMap((body): Effect.Effect<A, RelayClientError> => {
|
||||
if (response.status < 200 || response.status >= 300) {
|
||||
const envelope = decodeErrorEnvelope(body)
|
||||
const message = Option.isSome(envelope)
|
||||
? envelope.value.error
|
||||
: Option.getOrElse(Option.map(decodeErrorMessage(body), (value) => value.error), () => `Relay responded with HTTP ${response.status} for ${path}`)
|
||||
return Effect.fail(new RelayRejected({
|
||||
message,
|
||||
status: response.status,
|
||||
path,
|
||||
...(Option.isSome(envelope) && envelope.value.code ? { code: envelope.value.code } : {}),
|
||||
}))
|
||||
}
|
||||
return Schema.decodeUnknownEffect(schema)(body).pipe(
|
||||
Effect.mapError((cause) =>
|
||||
new RelayDecodeFailed({
|
||||
message: `Relay response for ${path} did not match the expected shape: ${cause.message}`,
|
||||
path,
|
||||
cause,
|
||||
})
|
||||
),
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
const transportError = (path: string) => (cause: unknown) =>
|
||||
new RelayUnreachable({
|
||||
message: `OpenCode Browser relay is not reachable at ${endpoint}. Relay-backed CLI commands start it automatically; use \`opencode-browser serve\` only for foreground debugging.`,
|
||||
endpoint,
|
||||
path,
|
||||
cause,
|
||||
})
|
||||
|
||||
const getJson = <A>(path: string, schema: Schema.ConstraintDecoder<A>): Effect.Effect<A, RelayClientError> =>
|
||||
httpClient.get(new URL(path, endpoint)).pipe(
|
||||
Effect.mapError(transportError(path)),
|
||||
Effect.flatMap((response) => handleResponse(response, path, schema)),
|
||||
)
|
||||
|
||||
const postJson = <A>(
|
||||
path: string,
|
||||
body: Record<string, unknown>,
|
||||
schema: Schema.ConstraintDecoder<A>,
|
||||
): Effect.Effect<A, RelayClientError> => HttpClientRequest.post(new URL(path, endpoint)).pipe(
|
||||
HttpClientRequest.bodyJson(body),
|
||||
Effect.mapError((cause) => new RelayEncodeFailed({
|
||||
message: `Could not encode relay request for ${path}`,
|
||||
path,
|
||||
cause,
|
||||
})),
|
||||
Effect.flatMap((request) => httpClient.execute(request).pipe(
|
||||
Effect.mapError(transportError(path)),
|
||||
Effect.flatMap((response) => handleResponse(response, path, schema)),
|
||||
)),
|
||||
)
|
||||
|
||||
const recordingTargetBody = (target: { readonly sessionId?: string | undefined; readonly tabId?: number | undefined }): Record<string, unknown> => ({
|
||||
...(target.sessionId ? { sessionId: target.sessionId } : {}),
|
||||
...(target.tabId === undefined ? {} : { tabId: target.tabId }),
|
||||
})
|
||||
|
||||
const recordingTargetQuery = (target: RecordingTargetRequest): string => {
|
||||
const searchParams = new URLSearchParams()
|
||||
if (target.sessionId) {
|
||||
searchParams.set("sessionId", target.sessionId)
|
||||
}
|
||||
if (target.tabId !== undefined) {
|
||||
searchParams.set("tabId", String(target.tabId))
|
||||
}
|
||||
const text = searchParams.toString()
|
||||
return text ? `?${text}` : ""
|
||||
}
|
||||
|
||||
return Service.of({
|
||||
endpoint,
|
||||
version: getJson("/version", RelayVersion),
|
||||
shutdown: (request) => postJson("/shutdown", { ...request }, RelayShutdownResponse),
|
||||
extensionStatus: getJson("/extension/status", ExtensionStatus),
|
||||
targets: getJson("/json/list", TargetSummaries),
|
||||
sessions: getJson("/cli/sessions", SessionsContainer).pipe(Effect.map((container) => container.sessions)),
|
||||
sessionNew: (id, options) =>
|
||||
postJson("/cli/session/new", {
|
||||
...(id ? { id } : {}),
|
||||
...(options?.readOnly ? { readOnly: true } : {}),
|
||||
}, SessionContainer).pipe(Effect.map((container) => container.session)),
|
||||
sessionEnsure: (id, options) =>
|
||||
postJson("/v1/sessions/ensure", {
|
||||
id,
|
||||
...(options?.readOnly === undefined ? {} : { readOnly: options.readOnly }),
|
||||
}, SessionContainer).pipe(Effect.map((container) => container.session)),
|
||||
sessionReset: (id) =>
|
||||
postJson("/cli/session/reset", { id }, SessionContainer).pipe(Effect.map((container) => container.session)),
|
||||
sessionAdopt: ({ sessionId, ...request }) =>
|
||||
postJson("/cli/session/adopt", {
|
||||
...request,
|
||||
...(sessionId ? { sessionId } : {}),
|
||||
}, SessionAdoptResponse),
|
||||
sessionDelete: (id) => postJson("/cli/session/delete", { id }, SessionDeleted),
|
||||
execute: ({ sessionId, ...request }) =>
|
||||
postJson("/cli/execute", {
|
||||
...request,
|
||||
...(sessionId ? { sessionId } : {}),
|
||||
}, ExecuteResponse),
|
||||
authenticatedJson: (request) => postJson("/v1/authenticated-origin/json", { ...request }, AuthenticatedJsonOutcome),
|
||||
networkStart: (request) => postJson("/network/start", { ...request }, NetworkStatusResponse),
|
||||
networkStatus: (request) => postJson("/network/status", { ...request }, NetworkStatusResponse),
|
||||
networkStop: (request) => postJson("/network/stop", { ...request }, NetworkStopResponse),
|
||||
networkCancel: (request) => postJson("/network/cancel", { ...request }, NetworkCancelResponse),
|
||||
authStatus: (request) => postJson("/auth/status", { ...request }, AuthProfileSummary),
|
||||
authRefresh: (request) => postJson("/auth/refresh", { ...request }, NetworkStopResponse),
|
||||
authRun: (request) => postJson("/auth/run", { ...request }, AuthRunResponse),
|
||||
recordingStart: ({ sessionId, tabId, ...request }) =>
|
||||
postJson("/recording/start", { ...recordingTargetBody({ sessionId, tabId }), ...request }, RecordingStartResponse),
|
||||
recordingStop: (target) => postJson("/recording/stop", recordingTargetBody(target), RecordingStopResponse),
|
||||
recordingStatus: (target) => getJson(`/recording/status${recordingTargetQuery(target)}`, RecordingStatusResponse),
|
||||
recordingCancel: (target) => postJson("/recording/cancel", recordingTargetBody(target), RecordingCancelResponse),
|
||||
tabsCleanup: (idleMinutes) => postJson("/tabs/cleanup", idleMinutes === undefined ? {} : { idleMinutes }, TabsCleanupResponse),
|
||||
flightRecorderStart: ({ sessionId, tabId, ...request }) =>
|
||||
postJson("/flight-recorder/start", { ...recordingTargetBody({ sessionId, tabId }), ...request }, FlightRecorderStatusResponse),
|
||||
flightRecorderStatus: (target) => getJson(`/flight-recorder/status${recordingTargetQuery(target)}`, FlightRecorderStatusResponse),
|
||||
flightRecorderSaveLast: ({ sessionId, tabId, ...request }) =>
|
||||
postJson("/flight-recorder/save-last", { ...recordingTargetBody({ sessionId, tabId }), ...request }, FlightRecorderSaveResponse),
|
||||
flightRecorderCancel: (target) => postJson("/flight-recorder/cancel", recordingTargetBody(target), FlightRecorderCancelResponse),
|
||||
})
|
||||
})
|
||||
|
||||
const layer: Layer.Layer<Service, RelayConfigInvalid, HttpClient.HttpClient> = Layer.effect(Service, make())
|
||||
|
||||
/** RelayClient backed by `node:http` (avoiding undici's 5-minute headersTimeout on long handoffs/commands), for standalone CLI/MCP wiring. */
|
||||
export const layerFetch: Layer.Layer<Service, RelayConfigInvalid> = layer.pipe(Layer.provide(NodeHttpClient.layerNodeHttp))
|
||||
483
packages/browser-extension/cli/src/relay-helpers.ts
Normal file
483
packages/browser-extension/cli/src/relay-helpers.ts
Normal file
|
|
@ -0,0 +1,483 @@
|
|||
import http from "node:http"
|
||||
import crypto from "node:crypto"
|
||||
import { Effect, Option, Predicate, Schema } from "effect"
|
||||
import { WebSocket, WebSocketServer } from "ws"
|
||||
import type { ExecuteTargetSelection } from "./execute.ts"
|
||||
import type { CdpEvent, CdpResponse, JsonObject, TargetInfo } from "./protocol.ts"
|
||||
import { parseJsonObject } from "./protocol.ts"
|
||||
import type { OpenCodeBrowserSession } from "./relay-types.ts"
|
||||
import { RelayErrorCode } from "./relay-schema.ts"
|
||||
|
||||
export const defaultHost = "127.0.0.1"
|
||||
/** Not Browser Control's 19989, so an old relay can't hold OpenCode Browser's port. Keep in step with the extension. */
|
||||
export const defaultPort = 19988
|
||||
/** OpenCode Browser from the Chrome Web Store, and the unpacked build (its ID is pinned by the manifest key). */
|
||||
export const chromeWebStoreExtensionOrigin = "chrome-extension://mfnicocicmmlkpjnaffgihfjhdgjkdjg"
|
||||
export const stableUnpackedExtensionOrigin = "chrome-extension://afeafocngkodbmaipcngoamamfmekgfo"
|
||||
|
||||
export function chromeExtensionOriginForPath(extensionPath: string, platform: NodeJS.Platform = process.platform): string {
|
||||
const normalizedPath = platform === "win32" && /^[a-z]:/.test(extensionPath)
|
||||
? extensionPath.charAt(0).toUpperCase() + extensionPath.slice(1)
|
||||
: extensionPath
|
||||
const pathBytes = platform === "win32" ? Buffer.from(normalizedPath, "utf16le") : normalizedPath
|
||||
const digest = crypto.createHash("sha256").update(pathBytes).digest()
|
||||
let extensionId = ""
|
||||
for (const byte of digest.subarray(0, 16)) {
|
||||
extensionId += String.fromCharCode(97 + (byte >> 4), 97 + (byte & 0x0f))
|
||||
}
|
||||
return `chrome-extension://${extensionId}`
|
||||
}
|
||||
|
||||
const chromeExtensionOriginPattern = /^chrome-extension:\/\/[a-p]+$/
|
||||
|
||||
/**
|
||||
* Parses the operator-supplied `OPENCODE_BROWSER_EXTENSION_ORIGINS` value into a
|
||||
* de-duplicated list of `chrome-extension://<id>` origins to allowlist in
|
||||
* addition to the built-ins. Entries are separated by commas or whitespace;
|
||||
* anything that is not a bare `chrome-extension://` origin (web origins, paths,
|
||||
* scheme-less ids) is dropped so a malformed value can never widen the origin
|
||||
* check to a non-extension origin. Used for same-host unpacked installs where
|
||||
* the path-derived id cannot match the relay's own bundled path.
|
||||
*/
|
||||
export function parseAdditionalExtensionOrigins(raw: string | undefined): string[] {
|
||||
if (!raw) {
|
||||
return []
|
||||
}
|
||||
const seen = new Set<string>()
|
||||
const origins: string[] = []
|
||||
for (const token of raw.split(/[\s,]+/)) {
|
||||
const value = token.trim()
|
||||
if (!value || !chromeExtensionOriginPattern.test(value) || seen.has(value)) {
|
||||
continue
|
||||
}
|
||||
seen.add(value)
|
||||
origins.push(value)
|
||||
}
|
||||
return origins
|
||||
}
|
||||
|
||||
const maxCliBodyBytes = 1_000_000
|
||||
|
||||
export class HttpRouteError extends Schema.TaggedError<HttpRouteError>()(
|
||||
"HttpApi.HttpRouteError",
|
||||
{ message: Schema.String, status: Schema.Number, code: RelayErrorCode },
|
||||
) {}
|
||||
|
||||
export function formatHostForUrl(host: string): string {
|
||||
if (host.includes(":") && !host.startsWith("[")) {
|
||||
return `[${host}]`
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
export function validateHostHeader(options: {
|
||||
readonly hostHeader: string | undefined
|
||||
readonly host: string
|
||||
readonly port: number
|
||||
}): string | undefined {
|
||||
const parsed = parseHostHeader(options.hostHeader)
|
||||
if (!parsed) {
|
||||
return "Invalid Host header"
|
||||
}
|
||||
if (parsed.port !== undefined && parsed.port !== options.port) {
|
||||
return "Invalid Host header port"
|
||||
}
|
||||
const allowedHosts = new Set(["localhost", "127.0.0.1", "::1", normalizeHostname(options.host)])
|
||||
if (!allowedHosts.has(parsed.hostname)) {
|
||||
return "Invalid Host header"
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export function validateBrowserFetchSite(request: http.IncomingMessage): string | undefined {
|
||||
const secFetchSite = request.headers["sec-fetch-site"]
|
||||
const value = Array.isArray(secFetchSite) ? secFetchSite[0] : secFetchSite
|
||||
if (!value || value === "same-origin" || value === "none") {
|
||||
return undefined
|
||||
}
|
||||
return "Cross-origin browser requests are not allowed"
|
||||
}
|
||||
|
||||
export function validateWebSocketOrigin(options: {
|
||||
readonly origin: string | undefined
|
||||
readonly additionalChromeExtensionOrigins?: ReadonlySet<string>
|
||||
readonly allowAnyChromeExtension?: boolean
|
||||
readonly requireChromeExtension?: boolean
|
||||
}): string | undefined {
|
||||
if (!options.origin) {
|
||||
return options.requireChromeExtension ? "Extension WebSocket origin is required" : undefined
|
||||
}
|
||||
if (
|
||||
options.origin === chromeWebStoreExtensionOrigin
|
||||
|| options.origin === stableUnpackedExtensionOrigin
|
||||
|| options.additionalChromeExtensionOrigins?.has(options.origin)
|
||||
) {
|
||||
return undefined
|
||||
}
|
||||
if (options.allowAnyChromeExtension && options.origin.startsWith("chrome-extension://")) {
|
||||
return undefined
|
||||
}
|
||||
if (options.requireChromeExtension) {
|
||||
return "Extension WebSocket origin is not allowed"
|
||||
}
|
||||
return "WebSocket origin is not allowed"
|
||||
}
|
||||
|
||||
function normalizeHostname(host: string): string {
|
||||
const value = host.trim().toLowerCase()
|
||||
if (value.startsWith("[") && value.endsWith("]")) {
|
||||
return value.slice(1, -1)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function parseHostHeader(hostHeader: string | undefined): { readonly hostname: string; readonly port?: number } | undefined {
|
||||
const value = hostHeader?.trim().toLowerCase()
|
||||
if (!value) {
|
||||
return undefined
|
||||
}
|
||||
if (value.startsWith("[")) {
|
||||
const closingBracket = value.indexOf("]")
|
||||
if (closingBracket === -1) {
|
||||
return undefined
|
||||
}
|
||||
const hostname = value.slice(1, closingBracket)
|
||||
const rest = value.slice(closingBracket + 1)
|
||||
if (!hostname) {
|
||||
return undefined
|
||||
}
|
||||
if (!rest) {
|
||||
return { hostname }
|
||||
}
|
||||
if (!rest.startsWith(":")) {
|
||||
return undefined
|
||||
}
|
||||
const port = parsePort(rest.slice(1))
|
||||
return port === undefined ? undefined : { hostname, port }
|
||||
}
|
||||
if (value === "::1") {
|
||||
return { hostname: "::1" }
|
||||
}
|
||||
const colonCount = value.split(":").length - 1
|
||||
if (colonCount > 1) {
|
||||
return undefined
|
||||
}
|
||||
if (colonCount === 0) {
|
||||
return { hostname: value }
|
||||
}
|
||||
const [hostname, portText] = value.split(":")
|
||||
if (!hostname || portText === undefined) {
|
||||
return undefined
|
||||
}
|
||||
const port = parsePort(portText)
|
||||
return port === undefined ? undefined : { hostname, port }
|
||||
}
|
||||
|
||||
function parsePort(value: string): number | undefined {
|
||||
if (!/^\d+$/.test(value)) {
|
||||
return undefined
|
||||
}
|
||||
const port = Number(value)
|
||||
if (!Number.isInteger(port) || port < 1 || port > 65_535) {
|
||||
return undefined
|
||||
}
|
||||
return port
|
||||
}
|
||||
|
||||
export function listenHttpServer(options: {
|
||||
readonly server: http.Server
|
||||
readonly host: string
|
||||
readonly port: number
|
||||
}): Effect.Effect<void, Error> {
|
||||
return Effect.callback<void, Error>((resume) => {
|
||||
const onError = (error: Error) => {
|
||||
resume(Effect.fail(error))
|
||||
}
|
||||
options.server.once("error", onError)
|
||||
options.server.listen(options.port, options.host, () => {
|
||||
options.server.off("error", onError)
|
||||
resume(Effect.void)
|
||||
})
|
||||
return Effect.sync(() => {
|
||||
options.server.off("error", onError)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function closeHttpServer(server: http.Server): Effect.Effect<void, Error> {
|
||||
if (!server.listening) {
|
||||
return Effect.void
|
||||
}
|
||||
return Effect.callback<void, Error>((resume) => {
|
||||
server.close((error?: Error) => {
|
||||
if (error) {
|
||||
resume(Effect.fail(new Error("close http server", { cause: error })))
|
||||
return
|
||||
}
|
||||
resume(Effect.void)
|
||||
})
|
||||
return Effect.void
|
||||
})
|
||||
}
|
||||
|
||||
export function closeWebSocketServer(server: WebSocketServer): Effect.Effect<void, Error> {
|
||||
return Effect.callback<void, Error>((resume) => {
|
||||
server.close((error?: Error) => {
|
||||
const nodeError = error as NodeJS.ErrnoException | undefined
|
||||
if (nodeError?.code === "ERR_SERVER_NOT_RUNNING") {
|
||||
resume(Effect.void)
|
||||
return
|
||||
}
|
||||
if (error) {
|
||||
resume(Effect.fail(new Error("close websocket server", { cause: error })))
|
||||
return
|
||||
}
|
||||
resume(Effect.void)
|
||||
})
|
||||
return Effect.void
|
||||
})
|
||||
}
|
||||
|
||||
export function logCloseError(message: string) {
|
||||
return (effect: Effect.Effect<void, Error>): Effect.Effect<void> => {
|
||||
return Effect.catch(effect, (error) => {
|
||||
return Effect.sync(() => {
|
||||
console.error(message, error)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function sendJson(response: http.ServerResponse, value: unknown, status = 200): void {
|
||||
response.writeHead(status, { "content-type": "application/json" })
|
||||
response.end(JSON.stringify(value))
|
||||
}
|
||||
|
||||
export function readJsonBody(request: http.IncomingMessage): Effect.Effect<JsonObject, Error> {
|
||||
const contentType = request.headers["content-type"]
|
||||
const contentTypeValue = Array.isArray(contentType) ? contentType[0] : contentType
|
||||
if (!contentTypeValue?.toLowerCase().includes("application/json")) {
|
||||
return Effect.fail(new HttpRouteError({ message: "Content-Type must be application/json", status: 415, code: "invalid-request" }))
|
||||
}
|
||||
return Effect.callback<JsonObject, Error>((resume) => {
|
||||
const chunks: Buffer[] = []
|
||||
let totalBytes = 0
|
||||
let completed = false
|
||||
const onData = (chunk: Buffer) => {
|
||||
if (completed) {
|
||||
return
|
||||
}
|
||||
totalBytes += chunk.byteLength
|
||||
if (totalBytes > maxCliBodyBytes) {
|
||||
completed = true
|
||||
request.destroy(new Error(`Request body exceeds ${maxCliBodyBytes} bytes`))
|
||||
resume(Effect.fail(new HttpRouteError({ message: `Request body exceeds ${maxCliBodyBytes} bytes`, status: 413, code: "invalid-request" })))
|
||||
return
|
||||
}
|
||||
chunks.push(chunk)
|
||||
}
|
||||
const onError = (error: Error) => {
|
||||
if (completed) {
|
||||
return
|
||||
}
|
||||
completed = true
|
||||
resume(Effect.fail(new Error("read request body", { cause: error })))
|
||||
}
|
||||
const onAbort = () => {
|
||||
if (completed) {
|
||||
return
|
||||
}
|
||||
completed = true
|
||||
resume(Effect.fail(new Error("request body aborted")))
|
||||
}
|
||||
const onClose = () => {
|
||||
if (completed || request.complete) {
|
||||
return
|
||||
}
|
||||
completed = true
|
||||
resume(Effect.fail(new Error("request closed before body completed")))
|
||||
}
|
||||
const onEnd = () => {
|
||||
if (completed) {
|
||||
return
|
||||
}
|
||||
completed = true
|
||||
const text = Buffer.concat(chunks).toString("utf8")
|
||||
if (!text.trim()) {
|
||||
resume(Effect.succeed({}))
|
||||
return
|
||||
}
|
||||
try {
|
||||
resume(Effect.succeed(parseJsonObject(text)))
|
||||
} catch (error) {
|
||||
resume(Effect.fail(new HttpRouteError({ message: "Invalid JSON body", status: 400, code: "invalid-request" })))
|
||||
}
|
||||
}
|
||||
request.on("data", onData)
|
||||
request.on("error", onError)
|
||||
request.on("aborted", onAbort)
|
||||
request.on("close", onClose)
|
||||
request.on("end", onEnd)
|
||||
return Effect.sync(() => {
|
||||
request.off("data", onData)
|
||||
request.off("error", onError)
|
||||
request.off("aborted", onAbort)
|
||||
request.off("close", onClose)
|
||||
request.off("end", onEnd)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const RawTargetSelection = Schema.Struct({
|
||||
urlIncludes: Schema.optionalKey(Schema.String),
|
||||
index: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
|
||||
})
|
||||
const decodeRawTargetSelection = Schema.decodeUnknownOption(RawTargetSelection)
|
||||
|
||||
const RawTargetInfo = Schema.Struct({
|
||||
targetId: Schema.String,
|
||||
type: Schema.Literals(["page", "iframe", "worker"]),
|
||||
title: Schema.optionalKey(Schema.String),
|
||||
url: Schema.String,
|
||||
canAccessOpener: Schema.optionalKey(Schema.Boolean),
|
||||
browserContextId: Schema.optionalKey(Schema.String),
|
||||
openerId: Schema.optionalKey(Schema.String),
|
||||
parentFrameId: Schema.optionalKey(Schema.String),
|
||||
})
|
||||
const decodeRawTargetInfo = Schema.decodeUnknownOption(RawTargetInfo)
|
||||
|
||||
export function optionalSessionId(value: JsonObject[string] | undefined): string | undefined {
|
||||
if (!Predicate.isString(value) || !value.trim()) {
|
||||
return undefined
|
||||
}
|
||||
const id = value.trim()
|
||||
if (!isValidSessionId(id)) {
|
||||
throw new HttpRouteError({
|
||||
message: "Session ids must use lowercase letters, numbers, and dashes, and be at most 63 characters",
|
||||
status: 400,
|
||||
code: "invalid-request",
|
||||
})
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
export function isValidSessionId(id: string): boolean {
|
||||
if (id.length < 1 || id.length > 63 || id === "." || id === "..") return false
|
||||
if (/[A-Z/\\:\0-\x1f\x7f]/.test(id)) return false
|
||||
return /^(?:[\p{Extended_Pictographic}\u200d\ufe0f]+\s*)?[a-z0-9][a-z0-9\p{Extended_Pictographic}\u200d\ufe0f\s_-]*$/u.test(id)
|
||||
}
|
||||
|
||||
export function requiredSessionId(value: JsonObject[string] | undefined): string {
|
||||
const id = optionalSessionId(value)
|
||||
if (!id) {
|
||||
throw new HttpRouteError({ message: "sessionId is required", status: 400, code: "invalid-request" })
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
export function parseTargetSelection(value: JsonObject[string] | undefined): ExecuteTargetSelection | undefined {
|
||||
if (value === undefined) {
|
||||
return undefined
|
||||
}
|
||||
if (!getObject(value)) {
|
||||
throw new HttpRouteError({ message: "targetSelection must be an object", status: 400, code: "invalid-request" })
|
||||
}
|
||||
const decoded = decodeRawTargetSelection(value)
|
||||
if (Option.isNone(decoded)) {
|
||||
throw new HttpRouteError({ message: "targetSelection.index must be a non-negative integer", status: 400, code: "invalid-request" })
|
||||
}
|
||||
const urlIncludes = decoded.value.urlIncludes ? decoded.value.urlIncludes : undefined
|
||||
const index = decoded.value.index
|
||||
if (urlIncludes && index !== undefined) {
|
||||
throw new HttpRouteError({ message: "Use only one target selector", status: 400, code: "invalid-request" })
|
||||
}
|
||||
return {
|
||||
...(urlIncludes ? { urlIncludes } : {}),
|
||||
...(index !== undefined ? { index } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
export function generateSessionId(existing: ReadonlyMap<string, OpenCodeBrowserSession>): string {
|
||||
const adjectives = ["amber", "brisk", "calm", "clever", "cosmic", "gentle", "lucky", "quiet", "rapid", "tidy"]
|
||||
const nouns = ["badger", "comet", "falcon", "otter", "panda", "raven", "sparrow", "tiger", "walrus", "wombat"]
|
||||
for (let attempt = 0; attempt < 100; attempt++) {
|
||||
const adjective = adjectives[Math.floor(Math.random() * adjectives.length)] ?? "calm"
|
||||
const noun = nouns[Math.floor(Math.random() * nouns.length)] ?? "otter"
|
||||
const suffix = String(Math.floor(Math.random() * 1000)).padStart(3, "0")
|
||||
const id = `${adjective}-${noun}-${suffix}`
|
||||
if (!existing.has(id)) {
|
||||
return id
|
||||
}
|
||||
}
|
||||
return `session-${Date.now().toString(36)}`
|
||||
}
|
||||
|
||||
export function sendCdpResponse(socket: WebSocket, response: CdpResponse): void {
|
||||
socket.send(JSON.stringify(response))
|
||||
}
|
||||
|
||||
export function sendCdpEvent(socket: Pick<WebSocket, "send">, event: CdpEvent): void {
|
||||
socket.send(JSON.stringify(event))
|
||||
}
|
||||
|
||||
export function getObject(value: unknown): JsonObject | undefined {
|
||||
return Predicate.isObject(value) ? (value as JsonObject) : undefined
|
||||
}
|
||||
|
||||
export function getString(object: JsonObject | undefined, key: string): string | undefined {
|
||||
const value = object?.[key]
|
||||
return Predicate.isString(value) ? value : undefined
|
||||
}
|
||||
|
||||
export function getNumber(object: JsonObject | undefined, key: string): number | undefined {
|
||||
const value = object?.[key]
|
||||
return Predicate.isNumber(value) ? value : undefined
|
||||
}
|
||||
|
||||
export function getIdText(object: JsonObject | undefined, key: string): string | undefined {
|
||||
const value = object?.[key]
|
||||
return Predicate.isNumber(value) || Predicate.isString(value) ? String(value) : undefined
|
||||
}
|
||||
|
||||
export function headerValue(value: string | string[] | undefined): string | undefined {
|
||||
if (!Predicate.isString(value) || !value) return undefined
|
||||
try {
|
||||
return decodeURIComponent(value)
|
||||
} catch {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
export function getTargetInfo(value: unknown): TargetInfo | undefined {
|
||||
return Option.match(decodeRawTargetInfo(value), {
|
||||
onNone: () => undefined,
|
||||
onSome: (info) => ({
|
||||
targetId: info.targetId,
|
||||
type: info.type,
|
||||
title: info.title ?? info.url,
|
||||
url: info.url,
|
||||
attached: true,
|
||||
canAccessOpener: info.canAccessOpener ?? false,
|
||||
...(info.browserContextId === undefined ? {} : { browserContextId: info.browserContextId }),
|
||||
...(info.openerId === undefined ? {} : { openerId: info.openerId }),
|
||||
...(info.parentFrameId === undefined ? {} : { parentFrameId: info.parentFrameId }),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
export function isRestrictedTarget(targetInfo: TargetInfo): boolean {
|
||||
if (targetInfo.type !== "page" && targetInfo.type !== "iframe" && targetInfo.type !== "worker") {
|
||||
return true
|
||||
}
|
||||
return isRestrictedUrl(targetInfo.url)
|
||||
}
|
||||
|
||||
const restrictedUrlPrefixes = ["chrome://", "chrome-extension://", "chrome-untrusted://", "devtools://", "edge://", "brave://"]
|
||||
|
||||
/** Browser-internal or other-extension documents that `chrome.debugger` refuses to expose. */
|
||||
export function isRestrictedUrl(url: string | undefined): boolean {
|
||||
if (!url) {
|
||||
return false
|
||||
}
|
||||
return restrictedUrlPrefixes.some((prefix) => url.startsWith(prefix))
|
||||
}
|
||||
52
packages/browser-extension/cli/src/relay-lifecycle-log.ts
Normal file
52
packages/browser-extension/cli/src/relay-lifecycle-log.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import fs from "node:fs"
|
||||
import path from "node:path"
|
||||
import { Schema } from "effect"
|
||||
import { isUnsupportedDirectorySyncError } from "./fs-durability.ts"
|
||||
import { RelayShutdownRequest } from "./relay-schema.ts"
|
||||
|
||||
const requestFields = {
|
||||
instanceId: RelayShutdownRequest.fields.instanceId,
|
||||
requestId: RelayShutdownRequest.fields.requestId,
|
||||
client: RelayShutdownRequest.fields.client,
|
||||
}
|
||||
|
||||
export const RelayLifecycleEvent = Schema.TaggedUnion({
|
||||
Requested: requestFields,
|
||||
Cancelled: requestFields,
|
||||
Stopping: requestFields,
|
||||
Closed: { instanceId: RelayShutdownRequest.fields.instanceId },
|
||||
Ready: {
|
||||
instanceId: RelayShutdownRequest.fields.instanceId,
|
||||
buildId: RelayShutdownRequest.fields.client.fields.buildId,
|
||||
restartRequestId: Schema.optionalKey(RelayShutdownRequest.fields.requestId),
|
||||
},
|
||||
})
|
||||
export type RelayLifecycleEvent = typeof RelayLifecycleEvent.Type
|
||||
|
||||
// Lifecycle transitions are rare and must be recorded before stopping the relay.
|
||||
// Synchronous bounded writes cannot finish out of order after request cancellation.
|
||||
export function appendRelayLifecycleEvent(filePath: string, event: RelayLifecycleEvent): void {
|
||||
const directory = path.dirname(filePath)
|
||||
fs.mkdirSync(directory, { recursive: true, mode: 0o700 })
|
||||
fs.chmodSync(directory, 0o700)
|
||||
const entry = `${JSON.stringify({ ...Schema.encodeSync(RelayLifecycleEvent)(event), timestamp: new Date().toISOString() })}\n`
|
||||
const file = fs.openSync(filePath, "a+", 0o600)
|
||||
try {
|
||||
fs.fchmodSync(file, 0o600)
|
||||
if (fs.fstatSync(file).size + Buffer.byteLength(entry) > 256_000) fs.ftruncateSync(file, 0)
|
||||
fs.writeFileSync(file, entry)
|
||||
fs.fsyncSync(file)
|
||||
} finally {
|
||||
fs.closeSync(file)
|
||||
}
|
||||
const parent = fs.openSync(directory, "r")
|
||||
try {
|
||||
try {
|
||||
fs.fsyncSync(parent)
|
||||
} catch (error) {
|
||||
if (!isUnsupportedDirectorySyncError(error)) throw error
|
||||
}
|
||||
} finally {
|
||||
fs.closeSync(parent)
|
||||
}
|
||||
}
|
||||
337
packages/browser-extension/cli/src/relay-lifecycle.ts
Normal file
337
packages/browser-extension/cli/src/relay-lifecycle.ts
Normal file
|
|
@ -0,0 +1,337 @@
|
|||
import { Config, Effect, Result, Schedule, Schema } from "effect"
|
||||
import { spawn } from "node:child_process"
|
||||
import crypto from "node:crypto"
|
||||
import path from "node:path"
|
||||
import process from "node:process"
|
||||
import { fileURLToPath } from "node:url"
|
||||
import { extensionReconnectAlarmPeriodMs } from "./protocol.ts"
|
||||
import * as RelayClient from "./relay-client.ts"
|
||||
import { RelayShutdownRequest, type ExtensionStatus, type RelayVersion } from "./relay-schema.ts"
|
||||
import { opencodeBrowserBuildId } from "./version.ts"
|
||||
|
||||
// From source the CLI entry is main.ts next to this file; the published bundle is a single file, so it is this file.
|
||||
const loadedCliEntrypoint = fileURLToPath(import.meta.url.endsWith(".ts") ? new URL("./main.ts", import.meta.url) : import.meta.url)
|
||||
const clientInstanceId = crypto.randomUUID()
|
||||
|
||||
export type RelayReadiness = {
|
||||
readonly version: RelayVersion
|
||||
readonly started: boolean
|
||||
readonly buildProblem?: string
|
||||
readonly waitForReconnect?: true
|
||||
}
|
||||
|
||||
export type EnsureRelayOptions = {
|
||||
readonly relay: RelayClient.Interface
|
||||
readonly start?: Effect.Effect<void, Error>
|
||||
readonly buildId?: string
|
||||
readonly retryTimes?: number
|
||||
readonly retryDelayMs?: number
|
||||
}
|
||||
|
||||
export function shouldWaitForExtensionReconnect(readiness: RelayReadiness): boolean {
|
||||
return readiness.started || readiness.waitForReconnect === true
|
||||
}
|
||||
|
||||
class RelayStillRunning extends Error {}
|
||||
|
||||
class RelayStartFailed extends Schema.TaggedError<RelayStartFailed>()(
|
||||
"RelayLifecycle.RelayStartFailed",
|
||||
{
|
||||
message: Schema.String,
|
||||
endpoint: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {}
|
||||
|
||||
export class ExtensionDisconnected extends Schema.TaggedError<ExtensionDisconnected>()(
|
||||
"RelayLifecycle.ExtensionDisconnected",
|
||||
{ message: Schema.String },
|
||||
) {}
|
||||
|
||||
export class ExtensionProtocolIncompatible extends Schema.TaggedError<ExtensionProtocolIncompatible>()(
|
||||
"RelayLifecycle.ExtensionProtocolIncompatible",
|
||||
{
|
||||
message: Schema.String,
|
||||
protocolVersion: Schema.NullOr(Schema.Number),
|
||||
},
|
||||
) {}
|
||||
|
||||
export function relayBuildProblem(version: RelayVersion, buildId = opencodeBrowserBuildId): string | undefined {
|
||||
if (!version.buildId) {
|
||||
return `Running relay does not report a build id; use \`opencode-browser relay restart\` for upgrade guidance (${buildId}).`
|
||||
}
|
||||
if (version.buildId !== buildId) {
|
||||
if (isNewerBuild(version.buildId, buildId)) {
|
||||
return `Running relay build ${version.buildId} is newer than CLI build ${buildId}; refresh or restart this CLI or MCP client. Only use \`opencode-browser relay restart\` from the current installation.`
|
||||
}
|
||||
return `Running relay build ${version.buildId} does not match CLI build ${buildId}; run \`opencode-browser relay restart\` explicitly. This leaves browser tabs open but resets in-memory JavaScript state.`
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export const ensureRelay = Effect.fn("RelayLifecycle.ensureRelay")(function* (options: EnsureRelayOptions) {
|
||||
const buildId = options.buildId ?? opencodeBrowserBuildId
|
||||
const probe = options.relay.version
|
||||
const initial = yield* Effect.result(probe)
|
||||
if (Result.isSuccess(initial)) {
|
||||
const buildProblem = relayBuildProblem(initial.success, buildId)
|
||||
return {
|
||||
version: initial.success,
|
||||
started: false,
|
||||
...(buildProblem ? { buildProblem } : {}),
|
||||
} satisfies RelayReadiness
|
||||
}
|
||||
const relayWasAbsent = isRelayUnreachable(initial.failure)
|
||||
if (!relayWasAbsent && !isRelayStarting(initial.failure)) {
|
||||
return yield* initial.failure
|
||||
}
|
||||
|
||||
if (relayWasAbsent) {
|
||||
const autoStart = yield* Config.Boolean("OPENCODE_BROWSER_AUTOSTART").pipe(
|
||||
Config.withDefault(true),
|
||||
Effect.mapError((cause) => new Error("Invalid OPENCODE_BROWSER_AUTOSTART configuration", { cause })),
|
||||
)
|
||||
if (!autoStart) {
|
||||
return yield* Effect.fail(new Error(`Automatic relay startup is disabled; no relay is available at ${options.relay.endpoint}`))
|
||||
}
|
||||
yield* options.start ?? startManagedRelay()
|
||||
}
|
||||
const version = yield* waitForRelayReady(options)
|
||||
const buildProblem = relayBuildProblem(version, buildId)
|
||||
return { version, started: relayWasAbsent, ...(buildProblem ? { buildProblem } : {}) } satisfies RelayReadiness
|
||||
})
|
||||
|
||||
function waitForRelayReady(options: EnsureRelayOptions): Effect.Effect<RelayVersion, Error | RelayClient.RelayClientError> {
|
||||
return options.relay.version.pipe(
|
||||
Effect.retry({
|
||||
times: options.retryTimes ?? 200,
|
||||
schedule: Schedule.spaced(options.retryDelayMs ?? 50),
|
||||
while: isRelayStartingOrUnreachable,
|
||||
}),
|
||||
Effect.mapError((error) => isRelayStartingOrUnreachable(error)
|
||||
? new RelayStartFailed({
|
||||
message: `OpenCode Browser relay did not start at ${options.relay.endpoint}`,
|
||||
endpoint: options.relay.endpoint,
|
||||
cause: error,
|
||||
})
|
||||
: error),
|
||||
)
|
||||
}
|
||||
|
||||
export const restartRelay = Effect.fn("RelayLifecycle.restartRelay")(function* (options: EnsureRelayOptions & {
|
||||
readonly clientKind?: RelayShutdownRequest["client"]["kind"]
|
||||
}) {
|
||||
const buildId = options.buildId ?? opencodeBrowserBuildId
|
||||
const initial = yield* Effect.result(options.relay.version)
|
||||
let original: RelayVersion | undefined
|
||||
let replacement: RelayVersion | undefined
|
||||
let restartRequestId: string | undefined
|
||||
if (Result.isSuccess(initial)) {
|
||||
original = initial.success
|
||||
} else if (isRelayStarting(initial.failure)) {
|
||||
original = yield* waitForRelayReady(options)
|
||||
} else if (!isRelayUnreachable(initial.failure)) {
|
||||
return yield* initial.failure
|
||||
}
|
||||
|
||||
if (original) {
|
||||
if (original.managed !== true || !original.instanceId) {
|
||||
return yield* Effect.fail(new Error("Cannot restart a foreground or unidentified relay. Stop it manually, then run `opencode-browser relay restart`."))
|
||||
}
|
||||
if (original.shutdownProtocol !== 2) {
|
||||
return yield* Effect.fail(new Error("This legacy relay does not support safe shutdown protocol 2. Stop it manually once, then run `opencode-browser relay restart` to upgrade; no shutdown request was sent."))
|
||||
}
|
||||
if (!Number.isFinite(Date.parse(buildId)) || !original.buildId || !Number.isFinite(Date.parse(original.buildId))) {
|
||||
return yield* Effect.fail(new Error("Cannot explicitly replace a source or unorderable relay build. Stop it manually, then launch the intended build."))
|
||||
}
|
||||
if (isNewerBuild(original.buildId, buildId)) {
|
||||
return yield* Effect.fail(new Error(`Refusing to downgrade relay build ${original.buildId} to ${buildId}; refresh or restart this CLI or MCP client.`))
|
||||
}
|
||||
|
||||
const confirmed = yield* Effect.result(options.relay.version)
|
||||
if (Result.isFailure(confirmed)) {
|
||||
if (!isRelayUnreachable(confirmed.failure)) return yield* confirmed.failure
|
||||
} else if (!isSameRelayInstance(original, confirmed.success)) {
|
||||
replacement = confirmed.success
|
||||
} else {
|
||||
if (confirmed.success.managed !== true || confirmed.success.shutdownProtocol !== 2 || confirmed.success.buildId !== original.buildId) {
|
||||
return yield* Effect.fail(new Error("Relay identity metadata changed during restart; no shutdown request was sent."))
|
||||
}
|
||||
const request = yield* RelayShutdownRequest.makeEffect({
|
||||
instanceId: original.instanceId,
|
||||
requestId: crypto.randomUUID(),
|
||||
reason: "explicit-restart",
|
||||
client: { kind: options.clientKind ?? "sdk", instanceId: clientInstanceId, buildId },
|
||||
})
|
||||
const shutdown = yield* Effect.result(options.relay.shutdown(request))
|
||||
if (Result.isSuccess(shutdown)) restartRequestId = request.requestId
|
||||
else if (!isRelayUnreachable(shutdown.failure) && !isRelayInstanceChanged(shutdown.failure)) {
|
||||
return yield* shutdown.failure
|
||||
}
|
||||
replacement = yield* waitForRelayExitOrReplacement({ ...options, version: original })
|
||||
}
|
||||
}
|
||||
|
||||
const started = replacement === undefined
|
||||
if (started) {
|
||||
yield* options.start ?? startManagedRelay(undefined, undefined, undefined, { ...(restartRequestId ? { restartRequestId } : {}) })
|
||||
replacement = yield* waitForRelayReady(options)
|
||||
}
|
||||
if (!replacement || !replacement.instanceId || replacement.managed !== true || (original && isSameRelayInstance(original, replacement))) {
|
||||
return yield* Effect.fail(new Error("Relay restart did not produce a new managed instance; no further shutdown was attempted."))
|
||||
}
|
||||
const buildProblem = relayBuildProblem(replacement, buildId)
|
||||
if (buildProblem) return yield* Effect.fail(new Error(`${buildProblem} A competing relay was left untouched.`))
|
||||
return { version: replacement, started, waitForReconnect: true } satisfies RelayReadiness
|
||||
})
|
||||
|
||||
function waitForRelayExitOrReplacement(options: {
|
||||
readonly relay: RelayClient.Interface
|
||||
readonly version: RelayVersion
|
||||
readonly retryTimes?: number
|
||||
readonly retryDelayMs?: number
|
||||
}): Effect.Effect<RelayVersion | undefined, Error | RelayClient.RelayClientError> {
|
||||
return options.relay.version.pipe(
|
||||
Effect.flatMap((version) => isSameRelayInstance(options.version, version)
|
||||
? Effect.fail(new RelayStillRunning("OpenCode Browser relay is still draining; no replacement was started"))
|
||||
: Effect.succeed(version)),
|
||||
Effect.catch((error) => isRelayUnreachable(error) ? Effect.succeed(undefined) : Effect.fail(error)),
|
||||
Effect.retry({
|
||||
times: options.retryTimes ?? 200,
|
||||
schedule: Schedule.spaced(options.retryDelayMs ?? 50),
|
||||
while: (error) => error instanceof RelayStillRunning || isRelayStarting(error),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
function isSameRelayInstance(left: RelayVersion, right: RelayVersion): boolean {
|
||||
return left.instanceId !== undefined && left.instanceId === right.instanceId
|
||||
}
|
||||
|
||||
function isNewerBuild(current: string, running: string | undefined): boolean {
|
||||
if (!running) return false
|
||||
const currentTime = Date.parse(current)
|
||||
const runningTime = Date.parse(running)
|
||||
return Number.isFinite(currentTime) && Number.isFinite(runningTime) && currentTime > runningTime
|
||||
}
|
||||
|
||||
function isRelayInstanceChanged(error: unknown): boolean {
|
||||
return error instanceof RelayClient.RelayRejected && error.status === 409 && error.code === "invalid-request"
|
||||
}
|
||||
|
||||
// Cover one full reconnect alarm period plus margin for the worker to connect.
|
||||
export const extensionReconnectWaitMs = extensionReconnectAlarmPeriodMs + 5_000
|
||||
const extensionReconnectProbeMs = 200
|
||||
|
||||
export const ensureExtensionConnected = Effect.fn("RelayLifecycle.ensureExtensionConnected")(function* (options: {
|
||||
readonly relay: RelayClient.Interface
|
||||
readonly waitForReconnect?: boolean
|
||||
readonly retryTimes?: number
|
||||
readonly retryDelayMs?: number
|
||||
/** Runs once, when the first probe finds the extension disconnected. */
|
||||
readonly onWait?: Effect.Effect<void>
|
||||
}) {
|
||||
const check = options.relay.extensionStatus.pipe(Effect.flatMap((status): Effect.Effect<
|
||||
ExtensionStatus,
|
||||
ExtensionProtocolIncompatible | ExtensionDisconnected
|
||||
> => {
|
||||
if (status.protocolCompatible === false) {
|
||||
return Effect.fail(new ExtensionProtocolIncompatible({
|
||||
message: `OpenCode Browser extension protocol ${status.protocolVersion ?? "unknown"} is incompatible with this relay.`,
|
||||
protocolVersion: status.protocolVersion ?? null,
|
||||
}))
|
||||
}
|
||||
return status.connected
|
||||
? Effect.succeed(status)
|
||||
: Effect.fail(new ExtensionDisconnected({
|
||||
message: "OpenCode Browser extension is not connected. Open the browser with OpenCode Browser installed (`npx opencode-browser-cli install`); it reconnects automatically after relay or browser startup.",
|
||||
}))
|
||||
}))
|
||||
if (!options.waitForReconnect) {
|
||||
return yield* check
|
||||
}
|
||||
let waitAnnounced = false
|
||||
return yield* check.pipe(
|
||||
Effect.tapError((error) => {
|
||||
if (waitAnnounced || !options.onWait || !(error instanceof ExtensionDisconnected)) return Effect.void
|
||||
waitAnnounced = true
|
||||
return options.onWait
|
||||
}),
|
||||
Effect.retry({
|
||||
times: options.retryTimes ?? Math.ceil(extensionReconnectWaitMs / extensionReconnectProbeMs),
|
||||
schedule: Schedule.spaced(options.retryDelayMs ?? extensionReconnectProbeMs),
|
||||
while: (error) => error instanceof ExtensionDisconnected || isRelayStartingOrUnreachable(error),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
export function stoppedRelayStatus(endpoint: string): {
|
||||
readonly endpoint: string
|
||||
readonly relay: { readonly running: false }
|
||||
readonly extension: null
|
||||
readonly sessions: readonly []
|
||||
readonly targets: readonly []
|
||||
} {
|
||||
return { endpoint, relay: { running: false }, extension: null, sessions: [], targets: [] }
|
||||
}
|
||||
|
||||
export function statusCollections(status: ExtensionStatus): {
|
||||
readonly sessions: NonNullable<ExtensionStatus["sessions"]>
|
||||
readonly targets: NonNullable<ExtensionStatus["targets"]>
|
||||
} | undefined {
|
||||
return status.sessions && status.targets ? { sessions: status.sessions, targets: status.targets } : undefined
|
||||
}
|
||||
|
||||
export function startManagedRelay(
|
||||
entrypoint = loadedCliEntrypoint,
|
||||
executable = process.execPath,
|
||||
execArgv: readonly string[] = process.execArgv,
|
||||
options: { readonly restartRequestId?: string } = {},
|
||||
): Effect.Effect<void, Error> {
|
||||
return Effect.try({
|
||||
try: () => {
|
||||
if (!entrypoint) {
|
||||
throw new Error("Cannot locate the opencode-browser CLI entrypoint")
|
||||
}
|
||||
const launch = managedRelayLaunch(entrypoint, executable, execArgv)
|
||||
const { OPENCODE_BROWSER_RESTART_REQUEST_ID: _inheritedRestartRequestId, ...env } = process.env
|
||||
const child = spawn(launch.executable, launch.args, {
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
env: { ...env, OPENCODE_BROWSER_MANAGED_RELAY: "1", ...(options.restartRequestId ? { OPENCODE_BROWSER_RESTART_REQUEST_ID: options.restartRequestId } : {}) },
|
||||
})
|
||||
child.unref()
|
||||
},
|
||||
catch: (cause) => cause instanceof Error ? cause : new Error("Failed to start OpenCode Browser relay", { cause }),
|
||||
})
|
||||
}
|
||||
|
||||
export function managedRelayLaunch(
|
||||
entrypoint = loadedCliEntrypoint,
|
||||
executable = process.execPath,
|
||||
execArgv: readonly string[] = process.execArgv,
|
||||
): { readonly executable: string; readonly args: string[] } {
|
||||
return {
|
||||
executable,
|
||||
args: [...execArgv, managedRelayEntrypoint(entrypoint), "serve"],
|
||||
}
|
||||
}
|
||||
|
||||
export function managedRelayEntrypoint(entrypoint: string): string {
|
||||
const name = path.basename(entrypoint)
|
||||
if (name === "mcp-main.ts" || name === "index.ts" || name === "opencode-browser-client.ts" || name === "cli.ts")
|
||||
return path.join(path.dirname(entrypoint), "main.ts")
|
||||
return entrypoint
|
||||
}
|
||||
|
||||
function isRelayUnreachable(error: unknown): error is RelayClient.RelayUnreachable {
|
||||
return error instanceof RelayClient.RelayUnreachable
|
||||
}
|
||||
|
||||
function isRelayStarting(error: unknown): error is RelayClient.RelayRejected {
|
||||
return error instanceof RelayClient.RelayRejected && error.code === "relay-starting"
|
||||
}
|
||||
|
||||
function isRelayStartingOrUnreachable(error: unknown): error is RelayClient.RelayRejected | RelayClient.RelayUnreachable {
|
||||
return isRelayStarting(error) || isRelayUnreachable(error)
|
||||
}
|
||||
41
packages/browser-extension/cli/src/relay-log.ts
Normal file
41
packages/browser-extension/cli/src/relay-log.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import { dataRoot } from "./paths.ts"
|
||||
import fs from "node:fs"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
|
||||
const maxLogBytes = 1_000_000
|
||||
const maxEntryCharacters = 64_000
|
||||
|
||||
export function managedRelayLogPath(home = os.homedir()): string {
|
||||
return path.join(dataRoot(home), "logs", "relay.log")
|
||||
}
|
||||
|
||||
export function appendManagedRelayProcessLog(message: string, home = os.homedir()): void {
|
||||
try {
|
||||
const logPath = managedRelayLogPath(home)
|
||||
const directory = path.dirname(logPath)
|
||||
fs.mkdirSync(directory, { recursive: true, mode: 0o700 })
|
||||
fs.chmodSync(directory, 0o700)
|
||||
const entry = `${new Date().toISOString()} ${message.slice(0, maxEntryCharacters)}\n`
|
||||
const stat = fs.statSync(logPath, { throwIfNoEntry: false })
|
||||
// Keep the previous file instead of truncating, so a rotation never drops the lines before a failure.
|
||||
if (stat && stat.size + Buffer.byteLength(entry) > maxLogBytes) fs.renameSync(logPath, `${logPath}.1`)
|
||||
fs.appendFileSync(logPath, entry, { encoding: "utf8", mode: 0o600 })
|
||||
fs.chmodSync(logPath, 0o600)
|
||||
} catch {
|
||||
// Process-fault logging must never hide or replace the original fault.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The relay's operational log: one line per lifecycle event (start, extension connects and disconnects, HTTP
|
||||
* requests with status and duration, faults), always on, in <data root>/logs/relay.log. Per-CDP-message
|
||||
* tracing stays behind OPENCODE_BROWSER_DEBUG.
|
||||
*/
|
||||
export function relayLog(event: string, fields: Record<string, unknown> = {}): void {
|
||||
const detail = Object.entries(fields)
|
||||
.filter(([, value]) => value !== undefined)
|
||||
.map(([key, value]) => `${key}=${typeof value === "string" && !/\s/.test(value) ? value : JSON.stringify(value)}`)
|
||||
.join(" ")
|
||||
appendManagedRelayProcessLog(detail ? `${event} ${detail}` : event)
|
||||
}
|
||||
569
packages/browser-extension/cli/src/relay-schema.ts
Normal file
569
packages/browser-extension/cli/src/relay-schema.ts
Normal file
|
|
@ -0,0 +1,569 @@
|
|||
import { Schema } from "effect"
|
||||
|
||||
/**
|
||||
* Shared wire contract for the relay HTTP API.
|
||||
*
|
||||
* These schemas are the single source of truth for the JSON shapes exchanged
|
||||
* between the relay's HTTP responders (`src/http-api.ts`) and its clients
|
||||
* (`src/relay-client.ts`, used by the CLI and the MCP server). Server-side
|
||||
* producers derive their types from here so the contract cannot drift.
|
||||
*/
|
||||
|
||||
export const SessionSummary = Schema.Struct({
|
||||
id: Schema.String,
|
||||
createdAt: Schema.String,
|
||||
updatedAt: Schema.String,
|
||||
connected: Schema.Boolean,
|
||||
pageUrl: Schema.NullOr(Schema.String),
|
||||
stateKeys: Schema.Array(Schema.String),
|
||||
readOnly: Schema.optionalKey(Schema.Boolean),
|
||||
})
|
||||
|
||||
export interface SessionSummary extends Schema.Schema.Type<typeof SessionSummary> {}
|
||||
|
||||
export const ExecuteSessionSummary = SessionSummary.pipe(Schema.fieldsAssign({
|
||||
created: Schema.optionalKey(Schema.Boolean),
|
||||
}))
|
||||
|
||||
export interface ExecuteSessionSummary extends Schema.Schema.Type<typeof ExecuteSessionSummary> {}
|
||||
|
||||
export const SessionContainer = Schema.Struct({
|
||||
session: SessionSummary,
|
||||
})
|
||||
|
||||
export interface SessionContainer extends Schema.Schema.Type<typeof SessionContainer> {}
|
||||
|
||||
export const SessionsContainer = Schema.Struct({
|
||||
sessions: Schema.Array(SessionSummary),
|
||||
})
|
||||
|
||||
export interface SessionsContainer extends Schema.Schema.Type<typeof SessionsContainer> {}
|
||||
|
||||
export const SessionDeleted = Schema.Struct({
|
||||
deleted: Schema.Boolean,
|
||||
id: Schema.String,
|
||||
})
|
||||
|
||||
export interface SessionDeleted extends Schema.Schema.Type<typeof SessionDeleted> {}
|
||||
|
||||
export const TargetSelection = Schema.Struct({
|
||||
urlIncludes: Schema.optionalKey(Schema.NonEmptyString),
|
||||
index: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
|
||||
}).check(Schema.makeFilter((selection) => {
|
||||
const hasUrl = selection.urlIncludes !== undefined
|
||||
const hasIndex = selection.index !== undefined
|
||||
return hasUrl !== hasIndex ? undefined : "targetSelection must contain exactly one of urlIncludes or index"
|
||||
}))
|
||||
|
||||
export interface TargetSelection extends Schema.Schema.Type<typeof TargetSelection> {}
|
||||
|
||||
export const ExecuteRequest = Schema.Struct({
|
||||
sessionId: Schema.optionalKey(Schema.String),
|
||||
code: Schema.String,
|
||||
createIfMissing: Schema.Boolean,
|
||||
targetSelection: Schema.optionalKey(TargetSelection),
|
||||
})
|
||||
|
||||
export interface ExecuteRequest extends Schema.Schema.Type<typeof ExecuteRequest> {}
|
||||
|
||||
export const SessionAdoptRequest = Schema.Struct({
|
||||
sessionId: Schema.optionalKey(Schema.String),
|
||||
createIfMissing: Schema.Boolean,
|
||||
targetSelection: Schema.optionalKey(TargetSelection),
|
||||
/** The Chrome tab id of an attached tab (the tab= value in status). */
|
||||
tabId: Schema.optionalKey(Schema.Int),
|
||||
})
|
||||
|
||||
export interface SessionAdoptRequest extends Schema.Schema.Type<typeof SessionAdoptRequest> {}
|
||||
|
||||
export const SessionNewRequest = Schema.Struct({
|
||||
id: Schema.optionalKey(Schema.String),
|
||||
readOnly: Schema.optionalKey(Schema.Boolean),
|
||||
})
|
||||
|
||||
export interface SessionNewRequest extends Schema.Schema.Type<typeof SessionNewRequest> {}
|
||||
|
||||
export const SessionEnsureRequest = Schema.Struct({
|
||||
id: Schema.NonEmptyString,
|
||||
readOnly: Schema.optionalKey(Schema.Boolean),
|
||||
})
|
||||
|
||||
export interface SessionEnsureRequest extends Schema.Schema.Type<typeof SessionEnsureRequest> {}
|
||||
|
||||
export const SessionIdRequest = Schema.Struct({
|
||||
id: Schema.String,
|
||||
})
|
||||
|
||||
export interface SessionIdRequest extends Schema.Schema.Type<typeof SessionIdRequest> {}
|
||||
|
||||
export const SessionAdoptResponse = Schema.Struct({
|
||||
session: ExecuteSessionSummary,
|
||||
adoptedUrl: Schema.String,
|
||||
adoptedTargetId: Schema.String,
|
||||
})
|
||||
|
||||
export interface SessionAdoptResponse extends Schema.Schema.Type<typeof SessionAdoptResponse> {}
|
||||
|
||||
const ExecuteLogLocation = Schema.Struct({
|
||||
url: Schema.String,
|
||||
lineNumber: Schema.Number,
|
||||
columnNumber: Schema.Number,
|
||||
})
|
||||
|
||||
interface ExecuteLogLocation extends Schema.Schema.Type<typeof ExecuteLogLocation> {}
|
||||
|
||||
export const ExecuteLogEntry = Schema.Struct({
|
||||
source: Schema.Literals(["script", "page"]),
|
||||
type: Schema.String,
|
||||
text: Schema.String,
|
||||
location: Schema.optionalKey(ExecuteLogLocation),
|
||||
repeatCount: Schema.optionalKey(Schema.Number),
|
||||
})
|
||||
|
||||
export interface ExecuteLogEntry extends Schema.Schema.Type<typeof ExecuteLogEntry> {}
|
||||
|
||||
export const ExecuteLogSummary = Schema.Struct({
|
||||
totalCount: Schema.Number,
|
||||
returnedCount: Schema.Number,
|
||||
repeatedCount: Schema.Number,
|
||||
omittedCount: Schema.Number,
|
||||
})
|
||||
|
||||
export interface ExecuteLogSummary extends Schema.Schema.Type<typeof ExecuteLogSummary> {}
|
||||
|
||||
/**
|
||||
* What changed in the browser during one execute call: URL movement, main
|
||||
* frame navigations, error counts, and human handoffs. Delivered with the
|
||||
* call that caused it so agents never fish warnings out of a later response.
|
||||
*/
|
||||
export const ExecuteAftermath = Schema.Struct({
|
||||
startUrl: Schema.NullOr(Schema.String),
|
||||
endUrl: Schema.NullOr(Schema.String),
|
||||
navigations: Schema.Array(Schema.String),
|
||||
consoleErrorCount: Schema.Number,
|
||||
pageErrorCount: Schema.Number,
|
||||
handoffs: Schema.Number,
|
||||
})
|
||||
|
||||
export interface ExecuteAftermath extends Schema.Schema.Type<typeof ExecuteAftermath> {}
|
||||
|
||||
export const ExecuteMedia = Schema.Struct({
|
||||
type: Schema.Literal("image"),
|
||||
mimeType: Schema.String,
|
||||
data: Schema.String,
|
||||
size: Schema.Number,
|
||||
})
|
||||
|
||||
export interface ExecuteMedia extends Schema.Schema.Type<typeof ExecuteMedia> {}
|
||||
|
||||
export const ExecuteResponse = Schema.Struct({
|
||||
text: Schema.String,
|
||||
value: Schema.optionalKey(Schema.Unknown),
|
||||
media: Schema.optionalKey(Schema.Array(ExecuteMedia)),
|
||||
isError: Schema.Boolean,
|
||||
logs: Schema.Array(ExecuteLogEntry),
|
||||
logSummary: Schema.optionalKey(ExecuteLogSummary),
|
||||
warnings: Schema.optionalKey(Schema.Array(Schema.String)),
|
||||
diagnostic: Schema.optionalKey(Schema.String),
|
||||
aftermath: Schema.optionalKey(ExecuteAftermath),
|
||||
session: ExecuteSessionSummary,
|
||||
})
|
||||
|
||||
export interface ExecuteResponse extends Schema.Schema.Type<typeof ExecuteResponse> {}
|
||||
|
||||
export const AuthenticatedJsonMethod = Schema.Literals(["GET", "POST", "PUT", "PATCH", "DELETE"])
|
||||
|
||||
export type AuthenticatedJsonMethod = Schema.Schema.Type<typeof AuthenticatedJsonMethod>
|
||||
|
||||
export const AuthenticatedJsonRequest = Schema.Struct({
|
||||
sessionId: Schema.NonEmptyString,
|
||||
origin: Schema.NonEmptyString,
|
||||
startUrl: Schema.optionalKey(Schema.NonEmptyString),
|
||||
method: AuthenticatedJsonMethod,
|
||||
path: Schema.NonEmptyString,
|
||||
headers: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)),
|
||||
body: Schema.optionalKey(Schema.Json),
|
||||
sensitive: Schema.optionalKey(Schema.Boolean),
|
||||
handoffOnAuthFailure: Schema.optionalKey(Schema.Boolean),
|
||||
handoffMessage: Schema.optionalKey(Schema.String),
|
||||
timeoutMs: Schema.optionalKey(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 120_000 }))),
|
||||
maxResponseBytes: Schema.optionalKey(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 10_000_000 }))),
|
||||
})
|
||||
|
||||
export interface AuthenticatedJsonRequest extends Schema.Schema.Type<typeof AuthenticatedJsonRequest> {}
|
||||
|
||||
export const AuthenticatedJsonOutcome = Schema.TaggedUnion({
|
||||
Success: {
|
||||
status: Schema.Int,
|
||||
value: Schema.Json,
|
||||
},
|
||||
OriginMismatch: {
|
||||
expectedOrigin: Schema.String,
|
||||
actualOrigin: Schema.String,
|
||||
},
|
||||
RequestFailed: {
|
||||
outcome: Schema.Literals(["not-sent", "unknown"]),
|
||||
},
|
||||
HttpError: {
|
||||
status: Schema.Int,
|
||||
},
|
||||
ResponseTooLarge: {
|
||||
status: Schema.Int,
|
||||
maxResponseBytes: Schema.Int,
|
||||
},
|
||||
InvalidJson: {
|
||||
status: Schema.Int,
|
||||
},
|
||||
SensitiveCaptureActive: {},
|
||||
})
|
||||
|
||||
export type AuthenticatedJsonOutcome = typeof AuthenticatedJsonOutcome.Type
|
||||
|
||||
export const TargetSummary = Schema.Struct({
|
||||
id: Schema.String,
|
||||
type: Schema.String,
|
||||
title: Schema.String,
|
||||
url: Schema.String,
|
||||
tabId: Schema.optionalKey(Schema.Number),
|
||||
sessionId: Schema.optionalKey(Schema.String),
|
||||
opencodeBrowserSessionId: Schema.optionalKey(Schema.String),
|
||||
owner: Schema.optionalKey(Schema.Literals(["relay", "user"])),
|
||||
crashed: Schema.optionalKey(Schema.Boolean),
|
||||
/** Chrome is rejecting debugger commands while another extension's UI is open in the tab. */
|
||||
protectedUi: Schema.optionalKey(Schema.Boolean),
|
||||
})
|
||||
|
||||
export interface TargetSummary extends Schema.Schema.Type<typeof TargetSummary> {}
|
||||
|
||||
export const TargetSummaries = Schema.Array(TargetSummary)
|
||||
|
||||
export const ExtensionStatus = Schema.Struct({
|
||||
connected: Schema.Boolean,
|
||||
version: Schema.NullOr(Schema.String),
|
||||
/** When the connected extension was built: changes when a reload or update took effect. */
|
||||
build: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
||||
/** The connected extension's origin: the store build or the unpacked one. */
|
||||
origin: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
||||
protocolVersion: Schema.optionalKey(Schema.NullOr(Schema.Number)),
|
||||
protocolCompatible: Schema.optionalKey(Schema.NullOr(Schema.Boolean)),
|
||||
protocolLegacy: Schema.optionalKey(Schema.NullOr(Schema.Boolean)),
|
||||
rejectedConnections: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))),
|
||||
activeTargets: Schema.Number,
|
||||
childTargets: Schema.optionalKey(Schema.Number),
|
||||
cdpClients: Schema.optionalKey(Schema.Number),
|
||||
sessions: Schema.optionalKey(Schema.Array(SessionSummary)),
|
||||
targets: Schema.optionalKey(TargetSummaries),
|
||||
})
|
||||
|
||||
export interface ExtensionStatus extends Schema.Schema.Type<typeof ExtensionStatus> {}
|
||||
|
||||
export const RelayVersion = Schema.Struct({
|
||||
version: Schema.String,
|
||||
buildId: Schema.optionalKey(Schema.String),
|
||||
instanceId: Schema.optionalKey(Schema.String),
|
||||
startedAt: Schema.optionalKey(Schema.String),
|
||||
pid: Schema.optionalKey(Schema.Number),
|
||||
managed: Schema.optionalKey(Schema.Boolean),
|
||||
shutdownProtocol: Schema.optionalKey(Schema.Literal(2)),
|
||||
})
|
||||
|
||||
export interface RelayVersion extends Schema.Schema.Type<typeof RelayVersion> {}
|
||||
|
||||
// Unlike $, this end assertion rejects trailing line breaks in audit identifiers.
|
||||
const RelayIdentity = Schema.String.check(Schema.isPattern(/^[A-Za-z0-9][A-Za-z0-9_-]{0,127}(?![\s\S])/))
|
||||
const RelayBuildId = Schema.String.check(Schema.isPattern(/^[A-Za-z0-9][A-Za-z0-9._:+-]{0,255}(?![\s\S])/))
|
||||
|
||||
export const RelayShutdownRequest = Schema.Struct({
|
||||
instanceId: RelayIdentity,
|
||||
requestId: RelayIdentity,
|
||||
reason: Schema.Literal("explicit-restart"),
|
||||
client: Schema.Struct({
|
||||
kind: Schema.Literals(["cli", "mcp", "sdk"]),
|
||||
instanceId: RelayIdentity,
|
||||
buildId: RelayBuildId,
|
||||
}),
|
||||
})
|
||||
|
||||
export interface RelayShutdownRequest extends Schema.Schema.Type<typeof RelayShutdownRequest> {}
|
||||
|
||||
export const RelayShutdownResponse = Schema.Struct({
|
||||
stopping: Schema.Literal(true),
|
||||
})
|
||||
|
||||
export interface RelayShutdownResponse extends Schema.Schema.Type<typeof RelayShutdownResponse> {}
|
||||
|
||||
const NetworkContentMode = Schema.Literals(["omit", "embed"])
|
||||
|
||||
export const NetworkStartRequest = Schema.Struct({
|
||||
sessionId: Schema.NonEmptyString,
|
||||
urlFilter: Schema.optionalKey(Schema.String),
|
||||
resourceTypes: Schema.optionalKey(Schema.Array(Schema.NonEmptyString).check(Schema.isMaxLength(50))),
|
||||
content: Schema.optionalKey(NetworkContentMode),
|
||||
maxBodyBytes: Schema.optionalKey(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 10_000_000 }))),
|
||||
maxTotalBodyBytes: Schema.optionalKey(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100_000_000 }))),
|
||||
maxEntries: Schema.optionalKey(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 10_000 }))),
|
||||
})
|
||||
|
||||
export interface NetworkStartRequest extends Schema.Schema.Type<typeof NetworkStartRequest> {}
|
||||
|
||||
export const NetworkSessionRequest = Schema.Struct({ sessionId: Schema.NonEmptyString })
|
||||
export interface NetworkSessionRequest extends Schema.Schema.Type<typeof NetworkSessionRequest> {}
|
||||
|
||||
export const NetworkStopRequest = NetworkSessionRequest.pipe(Schema.fieldsAssign({
|
||||
outputPath: Schema.optionalKey(Schema.String),
|
||||
secrets: Schema.optionalKey(Schema.NonEmptyString),
|
||||
}))
|
||||
export interface NetworkStopRequest extends Schema.Schema.Type<typeof NetworkStopRequest> {}
|
||||
|
||||
export const NetworkStatusResponse = Schema.Struct({
|
||||
active: Schema.Boolean,
|
||||
startedAt: Schema.optionalKey(Schema.String),
|
||||
entryCount: Schema.Number,
|
||||
responseCount: Schema.Number,
|
||||
failureCount: Schema.Number,
|
||||
capturedBodyBytes: Schema.Number,
|
||||
truncatedBodyCount: Schema.Number,
|
||||
droppedEntryCount: Schema.Number,
|
||||
urlFilter: Schema.optionalKey(Schema.String),
|
||||
resourceTypes: Schema.optionalKey(Schema.Array(Schema.String)),
|
||||
content: Schema.optionalKey(NetworkContentMode),
|
||||
secrets: Schema.optionalKey(Schema.String),
|
||||
})
|
||||
export interface NetworkStatusResponse extends Schema.Schema.Type<typeof NetworkStatusResponse> {}
|
||||
|
||||
const AuthProfileSlotSummary = Schema.Struct({
|
||||
ref: Schema.String,
|
||||
sources: Schema.Array(Schema.String),
|
||||
expiresAt: Schema.optionalKey(Schema.String),
|
||||
expired: Schema.Boolean,
|
||||
})
|
||||
|
||||
export const AuthProfileSummary = Schema.Struct({
|
||||
name: Schema.String,
|
||||
createdAt: Schema.String,
|
||||
updatedAt: Schema.String,
|
||||
slotCount: Schema.Number,
|
||||
slots: Schema.Array(AuthProfileSlotSummary),
|
||||
})
|
||||
export interface AuthProfileSummary extends Schema.Schema.Type<typeof AuthProfileSummary> {}
|
||||
|
||||
const NetworkEndpointDigest = Schema.Struct({
|
||||
method: Schema.String,
|
||||
url: Schema.String,
|
||||
status: Schema.optionalKey(Schema.Number),
|
||||
count: Schema.Number,
|
||||
requestHeaders: Schema.optionalKey(Schema.Array(Schema.String)),
|
||||
requestKeys: Schema.optionalKey(Schema.Array(Schema.String)),
|
||||
responseKeys: Schema.optionalKey(Schema.Array(Schema.String)),
|
||||
})
|
||||
|
||||
export const NetworkStopResponse = NetworkStatusResponse.pipe(Schema.fieldsAssign({
|
||||
active: Schema.Literal(false),
|
||||
stoppedAt: Schema.String,
|
||||
outputPath: Schema.optionalKey(Schema.String),
|
||||
authProfile: Schema.optionalKey(AuthProfileSummary),
|
||||
updatedSecretRefs: Schema.Array(Schema.String),
|
||||
observedSecretRefs: Schema.Array(Schema.String),
|
||||
endpoints: Schema.optionalKey(Schema.Array(NetworkEndpointDigest)),
|
||||
}))
|
||||
export interface NetworkStopResponse extends Schema.Schema.Type<typeof NetworkStopResponse> {}
|
||||
|
||||
export const NetworkCancelResponse = Schema.Struct({ cancelled: Schema.Boolean })
|
||||
export interface NetworkCancelResponse extends Schema.Schema.Type<typeof NetworkCancelResponse> {}
|
||||
|
||||
export const AuthProfileRequest = Schema.Struct({ name: Schema.NonEmptyString })
|
||||
export interface AuthProfileRequest extends Schema.Schema.Type<typeof AuthProfileRequest> {}
|
||||
|
||||
export const AuthRefreshRequest = Schema.Struct({
|
||||
sessionId: Schema.NonEmptyString,
|
||||
name: Schema.NonEmptyString,
|
||||
urlFilter: Schema.optionalKey(Schema.String),
|
||||
timeoutMs: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThan(0))),
|
||||
})
|
||||
export interface AuthRefreshRequest extends Schema.Schema.Type<typeof AuthRefreshRequest> {}
|
||||
|
||||
export const AuthRunRequest = Schema.Struct({
|
||||
name: Schema.NonEmptyString,
|
||||
command: Schema.NonEmptyString,
|
||||
args: Schema.optionalKey(Schema.Array(Schema.String)),
|
||||
cwd: Schema.optionalKey(Schema.String),
|
||||
timeoutMs: Schema.optionalKey(Schema.Int.check(Schema.isGreaterThan(0))),
|
||||
})
|
||||
export interface AuthRunRequest extends Schema.Schema.Type<typeof AuthRunRequest> {}
|
||||
|
||||
export const AuthRunResponse = Schema.Struct({
|
||||
exitCode: Schema.Number,
|
||||
signal: Schema.NullOr(Schema.String),
|
||||
stdout: Schema.String,
|
||||
stderr: Schema.String,
|
||||
stdoutTruncated: Schema.Boolean,
|
||||
stderrTruncated: Schema.Boolean,
|
||||
durationMs: Schema.Number,
|
||||
})
|
||||
export interface AuthRunResponse extends Schema.Schema.Type<typeof AuthRunResponse> {}
|
||||
|
||||
const RecordingMode = Schema.Literals(["tab-capture", "cdp"])
|
||||
|
||||
const RecordingRequestedMode = Schema.Literals(["auto", "tab-capture", "cdp"])
|
||||
|
||||
const RecordingFrameRate = Schema.Number.check(Schema.isInt(), Schema.isBetween({ minimum: 1, maximum: 60 }))
|
||||
|
||||
export const RecordingQuality = Schema.Struct({
|
||||
width: Schema.Number,
|
||||
height: Schema.Number,
|
||||
frameRate: Schema.Number,
|
||||
sourceFrameCount: Schema.Number,
|
||||
encodedSourceFrameCount: Schema.Number,
|
||||
coalescedFrameCount: Schema.Number,
|
||||
droppedFrameCount: Schema.Number,
|
||||
achievedSourceFrameRate: Schema.Number,
|
||||
achievedEncodedSourceFrameRate: Schema.Number,
|
||||
screenshotFallback: Schema.Boolean,
|
||||
sourceWidth: Schema.optionalKey(Schema.Number),
|
||||
sourceHeight: Schema.optionalKey(Schema.Number),
|
||||
})
|
||||
|
||||
export interface RecordingQuality extends Schema.Schema.Type<typeof RecordingQuality> {}
|
||||
|
||||
export const RecordingTargetRequest = Schema.Struct({
|
||||
sessionId: Schema.optionalKey(Schema.String),
|
||||
tabId: Schema.optionalKey(Schema.Number),
|
||||
})
|
||||
|
||||
export interface RecordingTargetRequest extends Schema.Schema.Type<typeof RecordingTargetRequest> {}
|
||||
|
||||
export const RecordingStartRequest = RecordingTargetRequest.pipe(Schema.fieldsAssign({
|
||||
outputPath: Schema.String,
|
||||
mode: Schema.optionalKey(RecordingRequestedMode),
|
||||
audio: Schema.optionalKey(Schema.Boolean),
|
||||
frameRate: Schema.optionalKey(RecordingFrameRate),
|
||||
videoBitsPerSecond: Schema.optionalKey(Schema.Number),
|
||||
audioBitsPerSecond: Schema.optionalKey(Schema.Number),
|
||||
maxDurationMs: Schema.optionalKey(Schema.Number),
|
||||
}))
|
||||
|
||||
export interface RecordingStartRequest extends Schema.Schema.Type<typeof RecordingStartRequest> {}
|
||||
|
||||
const RecordingArtifactType = Schema.Literals(["webm", "mp4"])
|
||||
|
||||
export const RecordingStartResponse = Schema.Struct({
|
||||
success: Schema.Boolean,
|
||||
tabId: Schema.optionalKey(Schema.Number),
|
||||
startedAt: Schema.optionalKey(Schema.Number),
|
||||
path: Schema.optionalKey(Schema.String),
|
||||
mimeType: Schema.optionalKey(Schema.String),
|
||||
mode: Schema.optionalKey(RecordingMode),
|
||||
artifactType: Schema.optionalKey(RecordingArtifactType),
|
||||
frameRate: Schema.optionalKey(Schema.Number),
|
||||
error: Schema.optionalKey(Schema.String),
|
||||
})
|
||||
|
||||
export interface RecordingStartResponse extends Schema.Schema.Type<typeof RecordingStartResponse> {}
|
||||
|
||||
export const RecordingStopResponse = Schema.Struct({
|
||||
success: Schema.Boolean,
|
||||
tabId: Schema.optionalKey(Schema.Number),
|
||||
duration: Schema.optionalKey(Schema.Number),
|
||||
path: Schema.optionalKey(Schema.String),
|
||||
size: Schema.optionalKey(Schema.Number),
|
||||
mode: Schema.optionalKey(RecordingMode),
|
||||
artifactType: Schema.optionalKey(RecordingArtifactType),
|
||||
frameCount: Schema.optionalKey(Schema.Number),
|
||||
quality: Schema.optionalKey(RecordingQuality),
|
||||
error: Schema.optionalKey(Schema.String),
|
||||
})
|
||||
|
||||
export interface RecordingStopResponse extends Schema.Schema.Type<typeof RecordingStopResponse> {}
|
||||
|
||||
export const RecordingStatusResponse = Schema.Struct({
|
||||
isRecording: Schema.Boolean,
|
||||
tabId: Schema.optionalKey(Schema.Number),
|
||||
startedAt: Schema.optionalKey(Schema.Number),
|
||||
path: Schema.optionalKey(Schema.String),
|
||||
size: Schema.optionalKey(Schema.Number),
|
||||
mode: Schema.optionalKey(RecordingMode),
|
||||
artifactType: Schema.optionalKey(RecordingArtifactType),
|
||||
frameCount: Schema.optionalKey(Schema.Number),
|
||||
quality: Schema.optionalKey(RecordingQuality),
|
||||
})
|
||||
|
||||
export interface RecordingStatusResponse extends Schema.Schema.Type<typeof RecordingStatusResponse> {}
|
||||
|
||||
export const TabsCleanupResponse = Schema.Struct({
|
||||
closed: Schema.Array(Schema.Struct({ tabId: Schema.Number, title: Schema.String, idleMinutes: Schema.Number })),
|
||||
})
|
||||
export type TabsCleanupResponse = typeof TabsCleanupResponse.Type
|
||||
|
||||
export const RecordingCancelResponse = Schema.Struct({
|
||||
success: Schema.Boolean,
|
||||
error: Schema.optionalKey(Schema.String),
|
||||
})
|
||||
|
||||
export interface RecordingCancelResponse extends Schema.Schema.Type<typeof RecordingCancelResponse> {}
|
||||
|
||||
export const FlightRecorderStartRequest = RecordingTargetRequest.pipe(Schema.fieldsAssign({
|
||||
retentionMs: Schema.optionalKey(Schema.Number.check(Schema.isInt(), Schema.isBetween({ minimum: 1_000, maximum: 120_000 }))),
|
||||
frameRate: Schema.optionalKey(RecordingFrameRate),
|
||||
}))
|
||||
export interface FlightRecorderStartRequest extends Schema.Schema.Type<typeof FlightRecorderStartRequest> {}
|
||||
|
||||
export const FlightRecorderStatusResponse = Schema.Struct({
|
||||
active: Schema.Boolean,
|
||||
tabId: Schema.optionalKey(Schema.Number),
|
||||
sessionId: Schema.optionalKey(Schema.String),
|
||||
startedAt: Schema.optionalKey(Schema.Number),
|
||||
retentionMs: Schema.optionalKey(Schema.Number),
|
||||
retainedDurationMs: Schema.optionalKey(Schema.Number),
|
||||
frameRate: Schema.optionalKey(Schema.Number),
|
||||
bufferedFrames: Schema.optionalKey(Schema.Number),
|
||||
bufferedBytes: Schema.optionalKey(Schema.Number),
|
||||
sourceFrameCount: Schema.optionalKey(Schema.Number),
|
||||
droppedFrameCount: Schema.optionalKey(Schema.Number),
|
||||
saving: Schema.optionalKey(Schema.Boolean),
|
||||
})
|
||||
export interface FlightRecorderStatusResponse extends Schema.Schema.Type<typeof FlightRecorderStatusResponse> {}
|
||||
|
||||
export const FlightRecorderSaveRequest = RecordingTargetRequest.pipe(Schema.fieldsAssign({
|
||||
outputPath: Schema.String,
|
||||
durationMs: Schema.optionalKey(Schema.Number.check(Schema.isInt(), Schema.isGreaterThan(0))),
|
||||
}))
|
||||
export interface FlightRecorderSaveRequest extends Schema.Schema.Type<typeof FlightRecorderSaveRequest> {}
|
||||
|
||||
export const FlightRecorderSaveResponse = Schema.Struct({
|
||||
path: Schema.String,
|
||||
durationMs: Schema.Number,
|
||||
frameCount: Schema.Number,
|
||||
sourceFrameCount: Schema.Number,
|
||||
droppedFrameCount: Schema.Number,
|
||||
})
|
||||
export interface FlightRecorderSaveResponse extends Schema.Schema.Type<typeof FlightRecorderSaveResponse> {}
|
||||
|
||||
export const FlightRecorderCancelResponse = Schema.Struct({ cancelled: Schema.Boolean })
|
||||
export interface FlightRecorderCancelResponse extends Schema.Schema.Type<typeof FlightRecorderCancelResponse> {}
|
||||
|
||||
export const RelayErrorCode = Schema.Literals([
|
||||
"invalid-request",
|
||||
"relay-starting",
|
||||
"relay-busy",
|
||||
"auth-profile-not-found",
|
||||
"capture-conflict",
|
||||
"session-already-exists",
|
||||
"session-inactive",
|
||||
"session-not-found",
|
||||
"session-timeout",
|
||||
"setup-failed",
|
||||
"target-ambiguous",
|
||||
"target-changed",
|
||||
"target-not-found",
|
||||
"target-owned",
|
||||
"internal",
|
||||
])
|
||||
|
||||
export type RelayErrorCode = Schema.Schema.Type<typeof RelayErrorCode>
|
||||
|
||||
export const ErrorEnvelope = Schema.Struct({
|
||||
error: Schema.String,
|
||||
code: Schema.optionalKey(RelayErrorCode),
|
||||
})
|
||||
|
||||
export interface ErrorEnvelope extends Schema.Schema.Type<typeof ErrorEnvelope> {}
|
||||
152
packages/browser-extension/cli/src/relay-shutdown.ts
Normal file
152
packages/browser-extension/cli/src/relay-shutdown.ts
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
import { Data, Effect, Exit, Latch, Match, Schema } from "effect"
|
||||
import type { RelayShutdownRequest } from "./relay-schema.ts"
|
||||
import { RelayLifecycleEvent } from "./relay-lifecycle-log.ts"
|
||||
|
||||
type State = Data.TaggedEnum<{
|
||||
Running: {}
|
||||
Draining: {}
|
||||
Stopping: {}
|
||||
Closing: {}
|
||||
}>
|
||||
const State = Data.taggedEnum<State>()
|
||||
|
||||
export class RelayShutdownError extends Schema.TaggedError<RelayShutdownError>()(
|
||||
"RelayShutdown.Error",
|
||||
{
|
||||
reason: Schema.Literals(["busy", "instance-changed", "not-managed"]),
|
||||
message: Schema.String,
|
||||
},
|
||||
) {}
|
||||
|
||||
export class RelayShutdown {
|
||||
private state: State = State.Running()
|
||||
private requests = 0
|
||||
private readonly idle = Latch.makeUnsafe(true)
|
||||
|
||||
constructor(private readonly options: {
|
||||
readonly instanceId: string
|
||||
readonly managed: boolean
|
||||
readonly drain: Effect.Effect<void, Error>
|
||||
readonly resume: () => void
|
||||
readonly busy: () => "raw-clients" | "recordings" | undefined
|
||||
readonly settle: Effect.Effect<void, Error>
|
||||
readonly quiescent: () => boolean
|
||||
readonly audit: (event: RelayLifecycleEvent) => Effect.Effect<void, Error>
|
||||
readonly stop: () => void
|
||||
readonly timeoutMs?: number
|
||||
}) {}
|
||||
|
||||
get accepting(): boolean {
|
||||
return State.$is("Running")(this.state)
|
||||
}
|
||||
|
||||
get stopping(): boolean {
|
||||
return State.$is("Stopping")(this.state) || State.$is("Closing")(this.state)
|
||||
}
|
||||
|
||||
close(): Effect.Effect<void, Error> {
|
||||
const control = this
|
||||
return Effect.gen(function* () {
|
||||
control.state = State.Closing()
|
||||
yield* Effect.all([control.options.drain, control.idle.await], { concurrency: "unbounded" })
|
||||
yield* control.idle.await
|
||||
yield* control.options.settle
|
||||
})
|
||||
}
|
||||
|
||||
track<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E | RelayShutdownError, R> {
|
||||
const control = this
|
||||
return Effect.acquireUseRelease(
|
||||
Effect.suspend(() => {
|
||||
if (!control.accepting) return Effect.fail(control.busyError())
|
||||
return Effect.sync(() => control.retain())
|
||||
}),
|
||||
() => effect,
|
||||
(release) => Effect.sync(release),
|
||||
)
|
||||
}
|
||||
|
||||
// Already-admitted sandbox work needs continuation RPCs during a drain.
|
||||
// Keep their lifetime independent of the client socket remaining connected.
|
||||
trackTransport<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> {
|
||||
return Effect.acquireUseRelease(
|
||||
Effect.sync(() => this.retain()),
|
||||
() => effect,
|
||||
(release) => Effect.sync(release),
|
||||
)
|
||||
}
|
||||
|
||||
private retain(): () => void {
|
||||
this.requests += 1
|
||||
this.idle.closeUnsafe()
|
||||
return () => {
|
||||
if (--this.requests === 0) this.idle.openUnsafe()
|
||||
}
|
||||
}
|
||||
|
||||
request(request: RelayShutdownRequest): Effect.Effect<void, Error> {
|
||||
const control = this
|
||||
return Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
||||
if (request.instanceId !== control.options.instanceId) {
|
||||
return yield* new RelayShutdownError({ reason: "instance-changed", message: "Relay shutdown does not match the active managed instance" })
|
||||
}
|
||||
if (!control.options.managed) {
|
||||
return yield* new RelayShutdownError({ reason: "not-managed", message: "A foreground relay must be stopped by its owner" })
|
||||
}
|
||||
if (!control.accepting) return yield* control.busyError()
|
||||
control.state = State.Draining()
|
||||
const fields = { instanceId: request.instanceId, requestId: request.requestId, client: request.client }
|
||||
yield* Effect.gen(function* () {
|
||||
yield* control.options.audit(RelayLifecycleEvent.cases.Requested.make(fields))
|
||||
const drain = Effect.gen(function* () {
|
||||
const busy = control.options.busy()
|
||||
if (busy) return yield* control.busyError(busy)
|
||||
yield* Effect.all([control.options.drain, control.idle.await], { concurrency: "unbounded" })
|
||||
yield* control.idle.await
|
||||
yield* control.options.settle
|
||||
const remaining = control.options.busy()
|
||||
if (remaining) return yield* control.busyError(remaining)
|
||||
})
|
||||
yield* restore(drain.pipe(Effect.timeoutOrElse({
|
||||
duration: control.options.timeoutMs ?? 10_000,
|
||||
orElse: () => control.busyError("timeout"),
|
||||
})))
|
||||
if (!State.$is("Draining")(control.state)) {
|
||||
return yield* control.busyError()
|
||||
}
|
||||
yield* control.options.audit(RelayLifecycleEvent.cases.Stopping.make(fields))
|
||||
yield* restore(Effect.void)
|
||||
if (!State.$is("Draining")(control.state)) {
|
||||
return yield* control.busyError()
|
||||
}
|
||||
if (control.requests !== 0 || !control.options.quiescent()) {
|
||||
return yield* control.busyError("changed")
|
||||
}
|
||||
const busy = control.options.busy()
|
||||
if (busy) return yield* control.busyError(busy)
|
||||
control.state = State.Stopping()
|
||||
control.options.stop()
|
||||
}).pipe(Effect.onExit((exit) => {
|
||||
if (Exit.isSuccess(exit) || !State.$is("Draining")(control.state)) return Effect.void
|
||||
control.options.resume()
|
||||
control.state = State.Running()
|
||||
return control.options.audit(RelayLifecycleEvent.cases.Cancelled.make(fields)).pipe(
|
||||
Effect.catch(() => Effect.sync(() => {
|
||||
console.error(`Relay cancellation audit failed for request ${request.requestId}`)
|
||||
})),
|
||||
)
|
||||
}))
|
||||
}))
|
||||
}
|
||||
|
||||
private busyError(reason?: "raw-clients" | "recordings" | "timeout" | "changed"): RelayShutdownError {
|
||||
const message = Match.value(reason).pipe(
|
||||
Match.when("raw-clients", () => "Relay has raw CDP clients attached; disconnect them before restarting"),
|
||||
Match.when("recordings", () => "Relay has active recordings or network captures; stop them before restarting"),
|
||||
Match.when("timeout", () => "Relay restart timed out waiting for accepted work; the relay is still running"),
|
||||
Match.when("changed", () => "Relay changed while preparing restart; it is still running, retry the restart"),
|
||||
Match.orElse(() => "Relay is draining for an explicit restart; retry after it completes"),
|
||||
)
|
||||
return new RelayShutdownError({ reason: "busy", message })
|
||||
}
|
||||
}
|
||||
94
packages/browser-extension/cli/src/relay-types.ts
Normal file
94
packages/browser-extension/cli/src/relay-types.ts
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import type { Effect, Semaphore } from "effect"
|
||||
import type { ExecuteOptions, ExecuteResult } from "./execute.ts"
|
||||
import type { NetworkCaptureOptions, NetworkCaptureResult, NetworkCaptureStatus, NetworkCaptureStopOptions } from "./network-capture.ts"
|
||||
import type { JsonObject, TargetInfo } from "./protocol.ts"
|
||||
import type { AuthenticatedJsonOutcome, AuthenticatedJsonRequest, SessionSummary } from "./relay-schema.ts"
|
||||
export type SessionTarget = {
|
||||
readonly id: string
|
||||
readonly owner: "relay" | "user"
|
||||
}
|
||||
|
||||
export type ConnectedTarget = {
|
||||
readonly tabId: number
|
||||
readonly sessionId: string
|
||||
/**
|
||||
* The OpenCode Browser session the tab was created for. Owned tabs are
|
||||
* visible only to that session's CDP clients; unowned tabs (user
|
||||
* toolbar-attached or raw-client-created) are visible to every client.
|
||||
*/
|
||||
readonly opencodeBrowserSessionId?: string
|
||||
readonly targetInfo: TargetInfo
|
||||
readonly owner: "relay" | "user"
|
||||
readonly crashed?: boolean
|
||||
/**
|
||||
* `chrome.debugger` currently rejects every command for this tab because a
|
||||
* protected frame (another extension's UI, such as a password manager's
|
||||
* inline menu) is open inside it. Cleared when a command succeeds again or
|
||||
* the last protected frame goes away.
|
||||
*/
|
||||
readonly protectedUi?: boolean
|
||||
}
|
||||
|
||||
export type ChildTarget = {
|
||||
readonly tabId: number
|
||||
readonly sessionId: string
|
||||
readonly parentSessionId: string
|
||||
readonly targetInfo: TargetInfo
|
||||
readonly waitingForDebugger: boolean
|
||||
}
|
||||
|
||||
export type StoredFrameEvents = {
|
||||
readonly frameId: string
|
||||
readonly attached?: JsonObject
|
||||
readonly navigated?: JsonObject
|
||||
}
|
||||
|
||||
export type PendingExtensionRequest = {
|
||||
readonly resolve: (value: JsonObject) => void
|
||||
readonly reject: (error: Error) => void
|
||||
readonly debuggerTabId?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* The sandbox seam used by session management. `ExecuteSandbox` is the real
|
||||
* implementation; tests can substitute a fake.
|
||||
*/
|
||||
export interface ExecuteSandboxLike {
|
||||
execute(code: string, options?: ExecuteOptions): Effect.Effect<ExecuteResult>
|
||||
authenticatedJson(request: Omit<AuthenticatedJsonRequest, "sessionId">): Effect.Effect<AuthenticatedJsonOutcome, Error>
|
||||
adoptPage(targetId: string): Effect.Effect<void, Error>
|
||||
/** Shutdown and handoff cancellation await disconnection without closing or forgetting the default tab. */
|
||||
disconnectSettled(): Effect.Effect<void, Error>
|
||||
/** Adoption rollback cleanup does not settle before started Playwright close promises settle. */
|
||||
closeSettled(): Effect.Effect<void, Error>
|
||||
networkStart(options?: NetworkCaptureOptions): Effect.Effect<NetworkCaptureStatus, Error>
|
||||
networkStatus(): NetworkCaptureStatus
|
||||
networkStop(options?: NetworkCaptureStopOptions): Effect.Effect<NetworkCaptureResult, Error>
|
||||
networkCancel(): Effect.Effect<{ readonly cancelled: boolean }>
|
||||
authRefresh(options: { readonly name: string; readonly urlFilter?: string; readonly timeoutMs?: number }): Effect.Effect<NetworkCaptureResult, Error>
|
||||
redactNetworkCaptureText(text: string): string
|
||||
markTargetCrashed(targetId: string): boolean
|
||||
markTargetProtectedUi(targetId: string, protectedUi: boolean): boolean
|
||||
markTargetDetached(targetId: string): boolean
|
||||
markTargetReplaced(previousTargetId: string, targetId: string): boolean
|
||||
restore(target: SessionTarget | undefined): void
|
||||
getStatus(): {
|
||||
readonly sessionId?: string
|
||||
readonly connected: boolean
|
||||
readonly pageUrl: string | null
|
||||
readonly stateKeys: string[]
|
||||
}
|
||||
}
|
||||
|
||||
export type OpenCodeBrowserSession = {
|
||||
readonly id: string
|
||||
readonly createdAt: string
|
||||
readonly readOnly: boolean
|
||||
readonly sandbox: ExecuteSandboxLike
|
||||
readonly executeSemaphore: Semaphore.Semaphore
|
||||
/** Durable default-target identity. Authoritative live ownership remains in TargetRegistry. */
|
||||
target?: SessionTarget
|
||||
updatedAt: string
|
||||
}
|
||||
|
||||
export type { SessionSummary }
|
||||
1627
packages/browser-extension/cli/src/relay.ts
Normal file
1627
packages/browser-extension/cli/src/relay.ts
Normal file
File diff suppressed because it is too large
Load diff
341
packages/browser-extension/cli/src/root-target-lifecycle.ts
Normal file
341
packages/browser-extension/cli/src/root-target-lifecycle.ts
Normal file
|
|
@ -0,0 +1,341 @@
|
|||
import { Effect, Exit, Latch, Schedule, Schema, Scope, Semaphore } from "effect"
|
||||
import type { CdpClientPool } from "./cdp-client-pool.ts"
|
||||
import { ghostCursorClientSource } from "./ghost-cursor.ts"
|
||||
import type { HandoffRegistry } from "./handoff.ts"
|
||||
import type { ExtensionCommand, JsonObject } from "./protocol.ts"
|
||||
import { getTargetInfo } from "./relay-helpers.ts"
|
||||
import type { ChildTarget, ConnectedTarget } from "./relay-types.ts"
|
||||
import type { OpenCodeBrowserSessions } from "./session-manager.ts"
|
||||
import { shouldExposeChildTarget, type RootTargetChange, type TargetRegistry } from "./target-registry.ts"
|
||||
|
||||
type AttachOptions = {
|
||||
readonly tabId: number
|
||||
readonly owner: "relay" | "user"
|
||||
readonly opencodeBrowserSessionId?: string
|
||||
readonly alreadyAttached?: boolean
|
||||
readonly expectedExtensionGeneration?: number
|
||||
readonly autoAttachParams?: JsonObject
|
||||
}
|
||||
|
||||
type Options = {
|
||||
readonly registry: TargetRegistry
|
||||
readonly sessions: Pick<OpenCodeBrowserSessions, "persistedTargetOwner" | "markTargetReplaced">
|
||||
readonly handoffs: Pick<HandoffRegistry, "rebindTarget">
|
||||
readonly clients: Pick<CdpClientPool<object>, "detachTab">
|
||||
readonly extension: {
|
||||
readonly generation: () => number
|
||||
readonly send: (command: Omit<ExtensionCommand, "id">) => Effect.Effect<JsonObject, Error>
|
||||
}
|
||||
readonly presentation: {
|
||||
readonly replaced: (change: Extract<RootTargetChange, { readonly kind: "replaced" }>) => void
|
||||
readonly committed: (target: ConnectedTarget) => void
|
||||
readonly announceRoot: (target: ConnectedTarget) => void
|
||||
readonly announceChild: (rootSessionId: string, target: ChildTarget) => void
|
||||
}
|
||||
readonly reportError: (message: string, error: unknown) => void
|
||||
}
|
||||
|
||||
type TabWork = { readonly semaphore: Semaphore.Semaphore; revision: number; users: number }
|
||||
type Transition = {
|
||||
readonly tabId: number
|
||||
readonly generation: number
|
||||
readonly work: TabWork
|
||||
readonly revision: number
|
||||
root: ConnectedTarget | undefined
|
||||
staged: ConnectedTarget | undefined
|
||||
}
|
||||
type Worker = {
|
||||
readonly generation: number
|
||||
readonly done: Latch.Latch
|
||||
attachIfMissing: boolean
|
||||
pending: boolean
|
||||
verificationRetries: number
|
||||
}
|
||||
|
||||
class GenerationChanged extends Schema.TaggedError<GenerationChanged>()("RootTargetLifecycle.GenerationChanged", {
|
||||
message: Schema.String,
|
||||
}) {}
|
||||
|
||||
/** Owns root setup and replacement, not target ownership or client announcement indexes. */
|
||||
export class RootTargetLifecycle {
|
||||
static readonly make = Effect.fn("RootTargetLifecycle.make")(function* (options: Options) {
|
||||
const scope = yield* Scope.make()
|
||||
const run = Effect.runForkWith(yield* Effect.context())
|
||||
return yield* Effect.acquireRelease(
|
||||
Effect.sync(() => new RootTargetLifecycle(options, scope, run)),
|
||||
(lifecycle) => lifecycle.close(),
|
||||
)
|
||||
})
|
||||
|
||||
private readonly tabs = new Map<number, TabWork>()
|
||||
private readonly workers = new Map<string, Worker>()
|
||||
private readonly idle = Latch.makeUnsafe(true)
|
||||
private active = 0
|
||||
private closing = false
|
||||
private readonly failedTabs = new Map<number, number>()
|
||||
private nextSessionId = 1
|
||||
|
||||
private constructor(
|
||||
private readonly options: Options,
|
||||
private readonly scope: Scope.Closeable,
|
||||
private readonly run: ReturnType<typeof Effect.runForkWith<never>>,
|
||||
) {}
|
||||
|
||||
isIdle(): boolean {
|
||||
return this.active === 0 && this.workers.size === 0
|
||||
}
|
||||
|
||||
/** Invalidate in-flight work before removing a tab, including a not-yet-staged attach. */
|
||||
invalidate(tabId: number): void {
|
||||
const work = this.tabs.get(tabId)
|
||||
if (work) work.revision += 1
|
||||
this.failedTabs.delete(tabId)
|
||||
}
|
||||
|
||||
readonly attach = Effect.fn("RootTargetLifecycle.attach")((options: AttachOptions) =>
|
||||
this.withPermit(options.tabId, options.expectedExtensionGeneration, (transition) => this.attachUnlocked(transition, options)))
|
||||
|
||||
readonly reconcile = Effect.fn("RootTargetLifecycle.reconcile")((tabId: number, generation?: number) =>
|
||||
this.withPermit(tabId, generation, (transition) => this.reconcileUnlocked(transition)))
|
||||
|
||||
queue(options: {
|
||||
readonly tabId: number
|
||||
readonly attachIfMissing: boolean
|
||||
readonly verificationRetries: number
|
||||
readonly errorMessage: string
|
||||
readonly generation?: number
|
||||
}): void {
|
||||
if (this.closing) return
|
||||
const lifecycle = this
|
||||
const generation = options.generation ?? this.options.extension.generation()
|
||||
if (generation !== this.options.extension.generation()) return
|
||||
for (const [tabId, failedGeneration] of this.failedTabs) {
|
||||
if (failedGeneration !== generation) this.failedTabs.delete(tabId)
|
||||
}
|
||||
const work = this.tabWork(options.tabId)
|
||||
const revision = work.revision
|
||||
const key = `${generation}:${options.tabId}:${revision}`
|
||||
const existing = this.workers.get(key)
|
||||
if (existing) {
|
||||
existing.pending = true
|
||||
existing.attachIfMissing ||= options.attachIfMissing
|
||||
existing.verificationRetries = Math.max(existing.verificationRetries, options.verificationRetries)
|
||||
return
|
||||
}
|
||||
work.users += 1
|
||||
const worker: Worker = {
|
||||
generation,
|
||||
done: Latch.makeUnsafe(),
|
||||
attachIfMissing: options.attachIfMissing,
|
||||
pending: false,
|
||||
verificationRetries: options.verificationRetries,
|
||||
}
|
||||
this.workers.set(key, worker)
|
||||
const pass = Effect.gen(function* () {
|
||||
worker.pending = false
|
||||
const mayAttach = worker.attachIfMissing
|
||||
worker.attachIfMissing = false
|
||||
yield* Effect.suspend(() => {
|
||||
if (revision !== work.revision) return new GenerationChanged({ message: "Tab detached during root reconciliation" })
|
||||
return lifecycle.withPermit(options.tabId, generation, (transition) => {
|
||||
if (lifecycle.options.registry.routingRootTarget(options.tabId)) return lifecycle.reconcileUnlocked(transition)
|
||||
return mayAttach
|
||||
? lifecycle.attachUnlocked(transition, { tabId: options.tabId, owner: "user", alreadyAttached: true }).pipe(Effect.asVoid)
|
||||
: Effect.void
|
||||
})
|
||||
}).pipe(
|
||||
Effect.tapError((error) => Effect.sync(() => lifecycle.options.reportError(options.errorMessage, error))),
|
||||
Effect.retry({
|
||||
times: 2,
|
||||
schedule: Schedule.spaced(0).pipe(Schedule.modifyDelay(({ attempt }) => Effect.succeed(100 * attempt))),
|
||||
while: (error) => !(error instanceof GenerationChanged) && !lifecycle.closing && generation === lifecycle.options.extension.generation() && revision === work.revision,
|
||||
}),
|
||||
)
|
||||
if (lifecycle.closing || generation !== lifecycle.options.extension.generation()) return null
|
||||
if (worker.verificationRetries > 0) {
|
||||
const delay = 50 * (4 - worker.verificationRetries)
|
||||
worker.verificationRetries -= 1
|
||||
return delay
|
||||
}
|
||||
return worker.pending ? 0 : null
|
||||
}).pipe(
|
||||
Effect.repeat({
|
||||
while: (delay) => delay !== null && !lifecycle.closing,
|
||||
schedule: Schedule.spaced(0).pipe(
|
||||
Schedule.setInputType<number | null>(),
|
||||
Schedule.modifyDelay(({ input }) => Effect.succeed(input ?? 0)),
|
||||
),
|
||||
}),
|
||||
Effect.onExit((exit) => Effect.sync(() => {
|
||||
if (generation === lifecycle.options.extension.generation() && revision === work.revision) {
|
||||
if (Exit.isFailure(exit)) lifecycle.failedTabs.set(options.tabId, generation)
|
||||
else lifecycle.failedTabs.delete(options.tabId)
|
||||
}
|
||||
lifecycle.workers.delete(key)
|
||||
if (--work.users === 0 && !lifecycle.options.registry.routingRootTarget(options.tabId)) lifecycle.tabs.delete(options.tabId)
|
||||
worker.done.openUnsafe()
|
||||
})),
|
||||
)
|
||||
// The scope is closed only after settlement; queued browser work is never interrupted by drain.
|
||||
this.run(pass.pipe(Effect.forkIn(this.scope)))
|
||||
}
|
||||
|
||||
/** Readiness retains failed inventory results even when a worker finished before `ready`. */
|
||||
readonly settle = Effect.fn("RootTargetLifecycle.settle")(function* (this: RootTargetLifecycle, generation?: number) {
|
||||
while (true) {
|
||||
const workers = Array.from(this.workers.values()).filter((worker) => generation === undefined || worker.generation === generation)
|
||||
yield* Effect.forEach(workers, (worker) => worker.done.await, { concurrency: "unbounded", discard: true })
|
||||
if (generation === undefined) yield* this.idle.await
|
||||
if (!Array.from(this.workers.values()).some((worker) => generation === undefined || worker.generation === generation)) break
|
||||
}
|
||||
return generation === undefined || (generation === this.options.extension.generation() && !Array.from(this.failedTabs.values()).includes(generation))
|
||||
})
|
||||
|
||||
readonly close = Effect.fn("RootTargetLifecycle.close")(function* (this: RootTargetLifecycle) {
|
||||
this.closing = true
|
||||
yield* this.settle()
|
||||
yield* Scope.close(this.scope, Exit.void)
|
||||
this.tabs.clear()
|
||||
})
|
||||
|
||||
private withPermit<A>(tabId: number, expectedGeneration: number | undefined, use: (transition: Transition) => Effect.Effect<A, Error>): Effect.Effect<A, Error> {
|
||||
const lifecycle = this
|
||||
return Effect.suspend(() => {
|
||||
if (lifecycle.closing) return Effect.fail(new Error("Relay is closing"))
|
||||
const generation = expectedGeneration ?? lifecycle.options.extension.generation()
|
||||
const work = lifecycle.tabWork(tabId)
|
||||
const revision = work.revision
|
||||
work.users += 1
|
||||
lifecycle.active += 1
|
||||
lifecycle.idle.closeUnsafe()
|
||||
return work.semaphore.withPermit(Effect.suspend(() => {
|
||||
const transition: Transition = {
|
||||
tabId, generation, work, revision,
|
||||
root: lifecycle.options.registry.tabTargets.get(tabId),
|
||||
staged: lifecycle.options.registry.stagedRootTarget(tabId),
|
||||
}
|
||||
return lifecycle.check(transition).pipe(Effect.andThen(() => use(transition)))
|
||||
})).pipe(Effect.ensuring(Effect.sync(() => {
|
||||
if (--work.users === 0 && !lifecycle.options.registry.routingRootTarget(tabId)) lifecycle.tabs.delete(tabId)
|
||||
if (--lifecycle.active === 0) lifecycle.idle.openUnsafe()
|
||||
})))
|
||||
})
|
||||
}
|
||||
|
||||
private tabWork(tabId: number): TabWork {
|
||||
const work = this.tabs.get(tabId) ?? { semaphore: Semaphore.makeUnsafe(1), revision: 0, users: 0 }
|
||||
this.tabs.set(tabId, work)
|
||||
return work
|
||||
}
|
||||
|
||||
private readonly check = Effect.fnUntraced(function* (this: RootTargetLifecycle, transition: Transition) {
|
||||
const { registry, extension } = this.options
|
||||
const root = registry.tabTargets.get(transition.tabId)
|
||||
const staged = registry.stagedRootTarget(transition.tabId)
|
||||
if (
|
||||
transition.generation !== extension.generation() || transition.revision !== transition.work.revision ||
|
||||
transition.root?.sessionId !== root?.sessionId || transition.root?.targetInfo.targetId !== root?.targetInfo.targetId ||
|
||||
transition.staged?.sessionId !== staged?.sessionId || transition.staged?.targetInfo.targetId !== staged?.targetInfo.targetId
|
||||
) return yield* new GenerationChanged({ message: "Root target generation changed during reconciliation" })
|
||||
})
|
||||
|
||||
private step<A>(transition: Transition, effect: Effect.Effect<A, Error>): Effect.Effect<A, Error> {
|
||||
return this.check(transition).pipe(
|
||||
Effect.andThen(effect),
|
||||
Effect.catch((error) => this.check(transition).pipe(Effect.andThen(Effect.fail(error)))),
|
||||
Effect.tap(() => this.check(transition)),
|
||||
)
|
||||
}
|
||||
|
||||
private command(transition: Transition, method: string, params: JsonObject = {}): Effect.Effect<JsonObject, Error> {
|
||||
return this.step(transition, this.options.extension.send({ method: "debugger.sendCommand", params: { tabId: transition.tabId, method, params } }))
|
||||
}
|
||||
|
||||
private readonly attachUnlocked = Effect.fnUntraced(function* (this: RootTargetLifecycle, transition: Transition, options: AttachOptions) {
|
||||
const { tabId } = transition
|
||||
if (!options.alreadyAttached) yield* this.step(transition, this.options.extension.send({ method: "debugger.attach", params: { tabId } }))
|
||||
yield* this.command(transition, "Page.enable")
|
||||
yield* Effect.gen({ self: this }, function* () {
|
||||
yield* this.command(transition, "Emulation.setFocusEmulationEnabled", { enabled: true })
|
||||
yield* this.command(transition, "Page.addScriptToEvaluateOnNewDocument", { source: ghostCursorClientSource })
|
||||
yield* this.command(transition, "Runtime.evaluate", { expression: ghostCursorClientSource })
|
||||
}).pipe(Effect.ignore)
|
||||
yield* this.check(transition)
|
||||
const result = yield* this.command(transition, "Target.getTargetInfo")
|
||||
const targetInfo = getTargetInfo(result.targetInfo)
|
||||
if (!targetInfo) return yield* Effect.fail(new Error("Target.getTargetInfo did not return targetInfo"))
|
||||
const restored = options.opencodeBrowserSessionId ? undefined : this.options.sessions.persistedTargetOwner(targetInfo.targetId)
|
||||
const opencodeBrowserSessionId = options.opencodeBrowserSessionId ?? restored?.sessionId
|
||||
const target = this.options.registry.stageRootTarget({
|
||||
tabId,
|
||||
sessionId: `bc-tab-${this.nextSessionId++}`,
|
||||
targetInfo,
|
||||
owner: restored?.owner ?? options.owner,
|
||||
...(opencodeBrowserSessionId ? { opencodeBrowserSessionId } : {}),
|
||||
})
|
||||
transition.staged = target
|
||||
return yield* this.finish(transition, target, options.autoAttachParams)
|
||||
})
|
||||
|
||||
private readonly reconcileUnlocked = Effect.fnUntraced(function* (this: RootTargetLifecycle, transition: Transition) {
|
||||
const { registry } = this.options
|
||||
const expected = registry.tabTargets.get(transition.tabId)
|
||||
const staged = registry.stagedRootTarget(transition.tabId)
|
||||
if (!expected && !staged) return
|
||||
const result = yield* this.command(transition, "Target.getTargetInfo").pipe(
|
||||
Effect.retry({ times: 1, schedule: Schedule.spaced(50), while: (error) => !(error instanceof GenerationChanged) }),
|
||||
)
|
||||
yield* this.check(transition)
|
||||
const targetInfo = getTargetInfo(result.targetInfo)
|
||||
if (!targetInfo) {
|
||||
return yield* Effect.fail(new Error(staged ? "Unable to verify staged root target" : "Unable to verify committed root target"))
|
||||
}
|
||||
if (staged?.targetInfo.targetId === targetInfo.targetId) {
|
||||
yield* this.finish(transition, staged)
|
||||
return
|
||||
}
|
||||
if (!staged && expected?.targetInfo.targetId === targetInfo.targetId) return
|
||||
const owner = expected ?? staged
|
||||
if (!owner) return
|
||||
yield* this.attachUnlocked(transition, {
|
||||
tabId: transition.tabId, owner: owner.owner, alreadyAttached: true,
|
||||
...(owner.opencodeBrowserSessionId ? { opencodeBrowserSessionId: owner.opencodeBrowserSessionId } : {}),
|
||||
})
|
||||
})
|
||||
|
||||
private readonly finish = Effect.fnUntraced(function* (this: RootTargetLifecycle, transition: Transition, target: ConnectedTarget, autoAttachParams?: JsonObject) {
|
||||
const { registry, handoffs, sessions, clients, presentation } = this.options
|
||||
// Stage before auto-attach: synchronous child events must route to this generation.
|
||||
yield* this.command(transition, "Target.setAutoAttach", autoAttachParams ?? { autoAttach: true, waitForDebuggerOnStart: false, flatten: true })
|
||||
const result = yield* this.command(transition, "Target.getTargetInfo")
|
||||
if (getTargetInfo(result.targetInfo)?.targetId !== target.targetInfo.targetId) {
|
||||
return yield* Effect.fail(new Error(`Root target changed while preparing ${target.targetInfo.targetId}`))
|
||||
}
|
||||
const change = registry.commitStagedRootTarget(target.tabId, target.sessionId)
|
||||
if (!change) return yield* Effect.fail(new Error(`Staged root target changed before commit: ${target.targetInfo.targetId}`))
|
||||
transition.root = change.target
|
||||
transition.staged = undefined
|
||||
if (change.kind === "replaced") {
|
||||
handoffs.rebindTarget({
|
||||
tabId: target.tabId,
|
||||
previousTargetId: change.previous.targetInfo.targetId,
|
||||
previousTargetSessionId: change.previous.sessionId,
|
||||
targetId: change.target.targetInfo.targetId,
|
||||
targetSessionId: change.target.sessionId,
|
||||
})
|
||||
sessions.markTargetReplaced(change.previous.targetInfo.targetId, change.target.targetInfo.targetId)
|
||||
clients.detachTab(target.tabId)
|
||||
presentation.replaced(change)
|
||||
}
|
||||
// No asynchronous gap between retiring old views and publishing the committed tree.
|
||||
presentation.committed(change.target)
|
||||
presentation.announceRoot(change.target)
|
||||
for (const child of registry.childTargets.values()) {
|
||||
if (child.tabId === target.tabId && child.parentSessionId === change.target.sessionId && shouldExposeChildTarget(child)) {
|
||||
presentation.announceChild(change.target.sessionId, child)
|
||||
}
|
||||
}
|
||||
yield* this.step(transition, this.options.extension.send({ method: "action.setAttached", params: { tabId: target.tabId, attached: true } }).pipe(Effect.ignore))
|
||||
return change.target
|
||||
})
|
||||
}
|
||||
112
packages/browser-extension/cli/src/runtime-diagnostics.ts
Normal file
112
packages/browser-extension/cli/src/runtime-diagnostics.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
import crypto from "node:crypto"
|
||||
import { Predicate } from "effect"
|
||||
import { PageReadTimeoutError } from "./page-read-timeout.ts"
|
||||
import { getIdText, getString } from "./relay-helpers.ts"
|
||||
import type { ExecuteAftermath } from "./relay-schema.ts"
|
||||
import type { JsonObject } from "./protocol.ts"
|
||||
|
||||
export type RuntimeFailureKind = "context-destroyed" | "context-missing" | "cross-extension-page" | "target-closed" | "timeout" | "other"
|
||||
|
||||
const maxDiagnosticTokenLength = 48
|
||||
|
||||
export function runtimeFailureKind(cause: unknown): RuntimeFailureKind {
|
||||
for (const message of errorMessages(cause)) {
|
||||
if (/cannot access a chrome-extension:\/\/ url of different extension/i.test(message)) {
|
||||
return "cross-extension-page"
|
||||
}
|
||||
if (/execution context was destroyed|context.*destroyed/i.test(message)) {
|
||||
return "context-destroyed"
|
||||
}
|
||||
if (/cannot find context with (?:specified )?id|execution context.*(?:not found|not available|does not exist)|context.*detached frame|frame (?:was|got) detached/i.test(message)) {
|
||||
return "context-missing"
|
||||
}
|
||||
if (/target page, context or browser has been closed|target closed|session closed/i.test(message)) {
|
||||
return "target-closed"
|
||||
}
|
||||
if (/timed out|timeout/i.test(message)) {
|
||||
return "timeout"
|
||||
}
|
||||
}
|
||||
return "other"
|
||||
}
|
||||
|
||||
export function executionContextFailureDiagnostic(cause: unknown, aftermath: ExecuteAftermath | undefined): string | undefined {
|
||||
const kind = runtimeFailureKind(cause)
|
||||
if (kind === "cross-extension-page") {
|
||||
return "target/cross-extension-page"
|
||||
}
|
||||
for (const error of errorCauses(cause)) {
|
||||
if (error instanceof PageReadTimeoutError) {
|
||||
// This is read-deadline evidence, not a lost context requiring page recovery.
|
||||
return `session-page/context-read-timeout; operation=${error.operation}; timeoutMs=${error.timeoutMs}`
|
||||
}
|
||||
}
|
||||
if (kind !== "context-destroyed" && kind !== "context-missing") {
|
||||
return undefined
|
||||
}
|
||||
const pageClosed = aftermath?.endUrl === null
|
||||
const urlChanged = aftermath ? aftermath.startUrl !== aftermath.endUrl : false
|
||||
const navigationCount = aftermath?.navigations.length ?? 0
|
||||
return `execution-context/${kind}; pageClosed=${pageClosed}; urlChanged=${urlChanged}; mainFrameNavigations=${navigationCount}`
|
||||
}
|
||||
|
||||
export function summarizeDiagnosticUrl(value: string | undefined): string {
|
||||
if (!value) {
|
||||
return "url=none"
|
||||
}
|
||||
const fingerprint = crypto.createHash("sha256").update(value).digest("hex").slice(0, 12)
|
||||
try {
|
||||
const parsed = new URL(value)
|
||||
const pathSegments = parsed.pathname.split("/").filter(Boolean).length
|
||||
const origin = parsed.origin === "null" ? `${parsed.protocol}//` : parsed.origin
|
||||
return `origin=${boundedToken(origin)} pathSegments=${pathSegments} query=${parsed.search ? "yes" : "no"} fragment=${parsed.hash ? "yes" : "no"} urlHash=${fingerprint}`
|
||||
} catch {
|
||||
return `url=unparseable chars=${value.length} urlHash=${fingerprint}`
|
||||
}
|
||||
}
|
||||
|
||||
export function summarizeRuntimeEvaluate(params: JsonObject | undefined): string {
|
||||
const source = getString(params, "expression") ?? getString(params, "functionDeclaration")
|
||||
const sourceLength = source?.length ?? 0
|
||||
const rawContextId = getIdText(params, "contextId") ?? getIdText(params, "executionContextId")
|
||||
const contextId = rawContextId !== undefined ? boundedToken(rawContextId) : "default"
|
||||
return [
|
||||
`sourceChars=${sourceLength}`,
|
||||
`argumentCount=${Array.isArray(params?.arguments) ? params.arguments.length : 0}`,
|
||||
`context=${contextId}`,
|
||||
`awaitPromise=${params?.awaitPromise === true}`,
|
||||
`returnByValue=${params?.returnByValue === true}`,
|
||||
`userGesture=${params?.userGesture === true}`,
|
||||
].join(" ")
|
||||
}
|
||||
|
||||
export function boundedToken(value: string | undefined): string {
|
||||
if (!value) {
|
||||
return "none"
|
||||
}
|
||||
const normalized = value.replace(/[^a-zA-Z0-9:._/-]/g, "_")
|
||||
if (normalized.length <= maxDiagnosticTokenLength) {
|
||||
return normalized
|
||||
}
|
||||
return `${normalized.slice(0, maxDiagnosticTokenLength - 9)}~${crypto.createHash("sha256").update(value).digest("hex").slice(0, 8)}`
|
||||
}
|
||||
|
||||
function errorMessages(cause: unknown): string[] {
|
||||
return errorCauses(cause).flatMap((error) => error instanceof Error ? [error.message] : Predicate.isString(error) ? [error] : [])
|
||||
}
|
||||
|
||||
function errorCauses(cause: unknown): unknown[] {
|
||||
const causes: unknown[] = []
|
||||
const seen = new Set<unknown>()
|
||||
let current = cause
|
||||
for (let depth = 0; depth < 5 && current !== undefined && current !== null && !seen.has(current); depth++) {
|
||||
seen.add(current)
|
||||
causes.push(current)
|
||||
if (current instanceof Error) {
|
||||
current = current.cause
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
return causes
|
||||
}
|
||||
104
packages/browser-extension/cli/src/screenshot-diff.ts
Normal file
104
packages/browser-extension/cli/src/screenshot-diff.ts
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
import fs from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import { Predicate } from "effect"
|
||||
import pixelmatch from "pixelmatch"
|
||||
import { PNG } from "pngjs"
|
||||
import type { Page } from "patchright-core"
|
||||
|
||||
const maxImageBytes = 32 * 1024 * 1024
|
||||
const maxImagePixels = 16 * 1024 * 1024
|
||||
const pngSignature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])
|
||||
|
||||
export type ScreenshotDiffOptions = {
|
||||
readonly baseline: string | Buffer
|
||||
readonly path?: string
|
||||
/** Per-pixel perceptual color threshold, 0..1. Not a changed-area allowance. */
|
||||
readonly threshold?: number
|
||||
readonly fullPage?: boolean
|
||||
}
|
||||
|
||||
export type ScreenshotDiffResult = {
|
||||
readonly matches: boolean
|
||||
readonly width: number
|
||||
readonly height: number
|
||||
readonly changedPixels: number
|
||||
readonly totalPixels: number
|
||||
readonly changedRatio: number
|
||||
readonly threshold: number
|
||||
readonly image?: Buffer
|
||||
readonly path?: string
|
||||
}
|
||||
|
||||
export function createScreenshotDiff(page: Pick<Page, "screenshot">): (options: ScreenshotDiffOptions) => Promise<ScreenshotDiffResult> {
|
||||
return async (options) => {
|
||||
const threshold = validateThreshold(options.threshold ?? 0.1)
|
||||
if (options.path !== undefined && (!path.isAbsolute(options.path) || path.extname(options.path).toLowerCase() !== ".png")) {
|
||||
throw new Error("screenshotDiff output path must be an absolute .png path")
|
||||
}
|
||||
const baseline = Predicate.isString(options.baseline) ? await readBaseline(options.baseline) : options.baseline
|
||||
const before = decodePng(baseline)
|
||||
const current = await page.screenshot({ type: "png", scale: "css", fullPage: options.fullPage ?? false })
|
||||
const after = decodePng(current)
|
||||
if (before.width !== after.width || before.height !== after.height) {
|
||||
throw new Error(`Screenshot dimensions differ: baseline ${before.width}×${before.height}, current ${after.width}×${after.height}. Capture both at the same CSS viewport and fullPage setting; images are never resized.`)
|
||||
}
|
||||
const diff = new PNG({ width: before.width, height: before.height })
|
||||
const changedPixels = pixelmatch(before.data, after.data, diff.data, before.width, before.height, {
|
||||
threshold,
|
||||
includeAA: true,
|
||||
})
|
||||
const image = PNG.sync.write(diff)
|
||||
const totalPixels = before.width * before.height
|
||||
// Exclusive creation also rejects symlinks/hardlinks to the baseline.
|
||||
if (options.path) await fs.writeFile(options.path, image, { flag: "wx", mode: 0o600 })
|
||||
return {
|
||||
matches: changedPixels === 0,
|
||||
width: before.width,
|
||||
height: before.height,
|
||||
changedPixels,
|
||||
totalPixels,
|
||||
changedRatio: changedPixels / totalPixels,
|
||||
threshold,
|
||||
...(options.path ? { path: options.path } : { image }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function readBaseline(filename: string): Promise<Buffer> {
|
||||
if (!path.isAbsolute(filename)) throw new Error("screenshotDiff baseline path must be absolute")
|
||||
const file = await fs.open(filename, "r")
|
||||
try {
|
||||
const stat = await file.stat()
|
||||
if (!stat.isFile() || stat.size > maxImageBytes) throw new Error("Screenshot baseline must be a PNG file no larger than 32 MiB")
|
||||
// Bound the read even if another process grows the file after stat.
|
||||
const buffer = Buffer.alloc(stat.size)
|
||||
let offset = 0
|
||||
while (offset < buffer.length) {
|
||||
const { bytesRead } = await file.read(buffer, offset, buffer.length - offset, offset)
|
||||
if (!bytesRead) break
|
||||
offset += bytesRead
|
||||
}
|
||||
return buffer.subarray(0, offset)
|
||||
} finally {
|
||||
await file.close()
|
||||
}
|
||||
}
|
||||
|
||||
function validateThreshold(threshold: number): number {
|
||||
if (!Number.isFinite(threshold) || threshold < 0 || threshold > 1) {
|
||||
throw new Error("screenshotDiff threshold must be a finite number from 0 to 1")
|
||||
}
|
||||
return threshold
|
||||
}
|
||||
|
||||
function decodePng(buffer: Buffer): PNG {
|
||||
if (!Buffer.isBuffer(buffer) || buffer.length > maxImageBytes || buffer.length < 24 || !buffer.subarray(0, 8).equals(pngSignature) || buffer.toString("ascii", 12, 16) !== "IHDR") {
|
||||
throw new Error("Screenshot must be a PNG buffer no larger than 32 MiB")
|
||||
}
|
||||
const width = buffer.readUInt32BE(16)
|
||||
const height = buffer.readUInt32BE(20)
|
||||
if (width === 0 || height === 0 || width * height > maxImagePixels) {
|
||||
throw new Error("Screenshot exceeds the 16 megapixel comparison limit")
|
||||
}
|
||||
return PNG.sync.read(buffer)
|
||||
}
|
||||
17
packages/browser-extension/cli/src/secret-profile.ts
Normal file
17
packages/browser-extension/cli/src/secret-profile.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import * as AuthProfile from "./auth-profile.ts"
|
||||
|
||||
export type Summary = AuthProfile.AuthProfileSummary
|
||||
export type RunResult = AuthProfile.AuthRunResult
|
||||
export type RunOptions = AuthProfile.AuthRunOptions
|
||||
export type StatusOptions = AuthProfile.AuthProfileOptions
|
||||
export type Error = AuthProfile.AuthProfileError
|
||||
export const Error = AuthProfile.AuthProfileError
|
||||
|
||||
/** Return profile metadata without revealing credential values. */
|
||||
export const status = AuthProfile.status
|
||||
|
||||
/**
|
||||
* Run a trusted credential-bearing worker with profile slots injected as BC_SECRET_N.
|
||||
* Known values are redacted from bounded stdout and stderr before they return.
|
||||
*/
|
||||
export const run = AuthProfile.run
|
||||
88
packages/browser-extension/cli/src/session-catalog.ts
Normal file
88
packages/browser-extension/cli/src/session-catalog.ts
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
import { dataRoot } from "./paths.ts"
|
||||
import fs from "node:fs/promises"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import crypto from "node:crypto"
|
||||
import { Schema } from "effect"
|
||||
import { isNodeError, isUnsupportedDirectorySyncError } from "./fs-durability.ts"
|
||||
import { isValidSessionId } from "./relay-helpers.ts"
|
||||
|
||||
export const PersistedSession = Schema.Struct({
|
||||
id: Schema.String,
|
||||
createdAt: Schema.String,
|
||||
updatedAt: Schema.String,
|
||||
readOnly: Schema.Boolean,
|
||||
target: Schema.optionalKey(Schema.Struct({
|
||||
id: Schema.String,
|
||||
owner: Schema.Literals(["relay", "user"]),
|
||||
})),
|
||||
})
|
||||
|
||||
export type PersistedSession = Schema.Schema.Type<typeof PersistedSession>
|
||||
|
||||
const Catalog = Schema.Struct({
|
||||
version: Schema.Literal(1),
|
||||
sessions: Schema.Array(PersistedSession),
|
||||
})
|
||||
|
||||
export function defaultSessionCatalogPath(port: number, home = os.homedir()): string {
|
||||
return path.join(dataRoot(home), "relays", String(port), "sessions.json")
|
||||
}
|
||||
|
||||
export class SessionCatalog {
|
||||
constructor(readonly filePath: string) {}
|
||||
|
||||
async load(): Promise<readonly PersistedSession[]> {
|
||||
let text: string
|
||||
try {
|
||||
text = await fs.readFile(this.filePath, "utf8")
|
||||
} catch (error) {
|
||||
if (isNodeError(error) && error.code === "ENOENT") return []
|
||||
throw new Error(`Could not read OpenCode Browser session catalog at ${this.filePath}`, { cause: error })
|
||||
}
|
||||
try {
|
||||
const sessions = Schema.decodeUnknownSync(Catalog)(JSON.parse(text)).sessions
|
||||
const invalid = sessions.find((session) => !isValidSessionId(session.id))
|
||||
if (invalid) throw new Error(`Invalid persisted session id: ${invalid.id}`)
|
||||
return sessions
|
||||
} catch (error) {
|
||||
const detail = error instanceof Error && error.message ? `: ${error.message}` : ""
|
||||
throw new Error(`Could not decode OpenCode Browser session catalog at ${this.filePath}${detail}`, { cause: error })
|
||||
}
|
||||
}
|
||||
|
||||
async save(sessions: readonly PersistedSession[]): Promise<void> {
|
||||
const directory = path.dirname(this.filePath)
|
||||
const temporaryPath = `${this.filePath}.${process.pid}.${crypto.randomUUID()}.tmp`
|
||||
const contents = `${JSON.stringify({ version: 1, sessions }, null, 2)}\n`
|
||||
let temporaryFile: fs.FileHandle | undefined
|
||||
try {
|
||||
await fs.mkdir(directory, { recursive: true, mode: 0o700 })
|
||||
await fs.chmod(directory, 0o700)
|
||||
temporaryFile = await fs.open(temporaryPath, "wx", 0o600)
|
||||
await temporaryFile.writeFile(contents, "utf8")
|
||||
await temporaryFile.sync()
|
||||
await temporaryFile.close()
|
||||
temporaryFile = undefined
|
||||
await fs.rename(temporaryPath, this.filePath)
|
||||
const directoryHandle = await fs.open(directory, "r")
|
||||
try {
|
||||
try {
|
||||
await directoryHandle.sync()
|
||||
} catch (error) {
|
||||
if (!isUnsupportedDirectorySyncError(error)) throw error
|
||||
}
|
||||
} finally {
|
||||
await directoryHandle.close()
|
||||
}
|
||||
} catch (error) {
|
||||
try {
|
||||
await temporaryFile?.close()
|
||||
} catch {}
|
||||
try {
|
||||
await fs.rm(temporaryPath, { force: true })
|
||||
} catch {}
|
||||
throw new Error(`Could not write OpenCode Browser session catalog at ${this.filePath}`, { cause: error })
|
||||
}
|
||||
}
|
||||
}
|
||||
149
packages/browser-extension/cli/src/session-journal.ts
Normal file
149
packages/browser-extension/cli/src/session-journal.ts
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
import { dataRoot } from "./paths.ts"
|
||||
import fs from "node:fs"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { Option, Schema } from "effect"
|
||||
import { isNodeError } from "./fs-durability.ts"
|
||||
|
||||
/**
|
||||
* Session journal: an auditable per-session record of what agents did to the
|
||||
* user's browser. The relay appends one JSON line per execute call to
|
||||
* `<data root>/sessions/<id>/journal.jsonl` (see paths.ts); the CLI `journal` command
|
||||
* reads the file locally and renders a human-readable timeline.
|
||||
*/
|
||||
|
||||
export const JournalEntry = Schema.Struct({
|
||||
ts: Schema.String,
|
||||
sessionId: Schema.String,
|
||||
code: Schema.String,
|
||||
isError: Schema.Boolean,
|
||||
durationMs: Schema.Number,
|
||||
resultPreview: Schema.String,
|
||||
logCount: Schema.Number,
|
||||
startUrl: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
||||
endUrl: Schema.optionalKey(Schema.NullOr(Schema.String)),
|
||||
navigations: Schema.optionalKey(Schema.Array(Schema.String)),
|
||||
warnings: Schema.optionalKey(Schema.Array(Schema.String)),
|
||||
diagnostic: Schema.optionalKey(Schema.String),
|
||||
handoffs: Schema.optionalKey(Schema.Number),
|
||||
})
|
||||
|
||||
export interface JournalEntry extends Schema.Schema.Type<typeof JournalEntry> {}
|
||||
|
||||
const decodeJournalEntry = Schema.decodeUnknownOption(JournalEntry)
|
||||
|
||||
export const defaultJournalBaseDir = (): string => {
|
||||
return path.join(dataRoot(), "sessions")
|
||||
}
|
||||
|
||||
export function journalPathForSession(baseDir: string, sessionId: string): string {
|
||||
return path.join(baseDir, sessionId, "journal.jsonl")
|
||||
}
|
||||
|
||||
const maxJournalCodeLength = 2_000
|
||||
const maxJournalPreviewLength = 400
|
||||
const maxJournalDiagnosticLength = 240
|
||||
|
||||
export function truncateForJournal(text: string, maxLength: number): string {
|
||||
if (text.length <= maxLength) {
|
||||
return text
|
||||
}
|
||||
return `${text.slice(0, maxLength)}… [truncated ${text.length - maxLength} chars]`
|
||||
}
|
||||
|
||||
export function makeJournalEntry(options: {
|
||||
readonly sessionId: string
|
||||
readonly code: string
|
||||
readonly isError: boolean
|
||||
readonly durationMs: number
|
||||
readonly resultText: string
|
||||
readonly logCount: number
|
||||
readonly startUrl?: string | null | undefined
|
||||
readonly endUrl?: string | null | undefined
|
||||
readonly navigations?: readonly string[] | undefined
|
||||
readonly warnings?: readonly string[] | undefined
|
||||
readonly diagnostic?: string | undefined
|
||||
readonly handoffs?: number | undefined
|
||||
}): JournalEntry {
|
||||
return {
|
||||
ts: new Date().toISOString(),
|
||||
sessionId: options.sessionId,
|
||||
code: truncateForJournal(options.code, maxJournalCodeLength),
|
||||
isError: options.isError,
|
||||
durationMs: options.durationMs,
|
||||
resultPreview: truncateForJournal(options.resultText, maxJournalPreviewLength),
|
||||
logCount: options.logCount,
|
||||
...(options.startUrl === undefined ? {} : { startUrl: options.startUrl }),
|
||||
...(options.endUrl === undefined ? {} : { endUrl: options.endUrl }),
|
||||
...(options.navigations && options.navigations.length > 0 ? { navigations: options.navigations } : {}),
|
||||
...(options.warnings && options.warnings.length > 0 ? { warnings: options.warnings } : {}),
|
||||
...(options.diagnostic ? { diagnostic: truncateForJournal(options.diagnostic, maxJournalDiagnosticLength) } : {}),
|
||||
...(options.handoffs ? { handoffs: options.handoffs } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a journal entry, creating the session directory if needed. Journal
|
||||
* writes are best-effort: failures are reported to the callback (or ignored)
|
||||
* and never fail the execute call they describe.
|
||||
*/
|
||||
export async function appendJournalEntry(options: {
|
||||
readonly baseDir: string
|
||||
readonly entry: JournalEntry
|
||||
}): Promise<void> {
|
||||
const filePath = journalPathForSession(options.baseDir, options.entry.sessionId)
|
||||
await fs.promises.mkdir(path.dirname(filePath), { recursive: true })
|
||||
await fs.promises.appendFile(filePath, `${JSON.stringify(options.entry)}\n`, "utf8")
|
||||
}
|
||||
|
||||
/** Read the last `limit` journal entries for a session; skips corrupt lines. */
|
||||
export async function readJournalEntries(options: {
|
||||
readonly baseDir: string
|
||||
readonly sessionId: string
|
||||
readonly limit: number
|
||||
}): Promise<JournalEntry[]> {
|
||||
const filePath = journalPathForSession(options.baseDir, options.sessionId)
|
||||
let raw: string
|
||||
try {
|
||||
raw = await fs.promises.readFile(filePath, "utf8")
|
||||
} catch (error) {
|
||||
if (isNodeError(error) && error.code === "ENOENT") {
|
||||
return []
|
||||
}
|
||||
throw error
|
||||
}
|
||||
return parseJournalLines(raw).slice(-options.limit)
|
||||
}
|
||||
|
||||
export function parseJournalLines(raw: string): JournalEntry[] {
|
||||
const entries: JournalEntry[] = []
|
||||
for (const line of raw.split("\n")) {
|
||||
const trimmed = line.trim()
|
||||
if (!trimmed) {
|
||||
continue
|
||||
}
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(trimmed)
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
const decoded = decodeJournalEntry(parsed)
|
||||
if (Option.isSome(decoded)) {
|
||||
entries.push(decoded.value)
|
||||
}
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
export function formatJournalEntry(entry: JournalEntry): string {
|
||||
const time = entry.ts.slice(11, 19) || entry.ts
|
||||
const status = entry.isError ? "ERR" : "ok "
|
||||
const codePreview = entry.code.replace(/\s+/g, " ").trim().slice(0, 80)
|
||||
const urlPart = entry.endUrl ? ` ${entry.endUrl}` : ""
|
||||
const navPart = entry.navigations && entry.navigations.length > 0 ? ` nav=${entry.navigations.length}` : ""
|
||||
const handoffPart = entry.handoffs ? ` handoffs=${entry.handoffs}` : ""
|
||||
const warningPart = entry.warnings && entry.warnings.length > 0 ? ` warnings=${entry.warnings.length}` : ""
|
||||
const diagnosticPart = entry.diagnostic ? ` diagnostic=${entry.diagnostic}` : ""
|
||||
return `${time} ${status} ${String(entry.durationMs).padStart(5)}ms${urlPart}${navPart}${handoffPart}${warningPart}${diagnosticPart} ${codePreview}`
|
||||
}
|
||||
990
packages/browser-extension/cli/src/session-manager.ts
Normal file
990
packages/browser-extension/cli/src/session-manager.ts
Normal file
|
|
@ -0,0 +1,990 @@
|
|||
import { Deferred, Effect, Exit, Fiber, Latch, Result, Schema, Semaphore } from "effect"
|
||||
import { defaultPageClosedWarning, ExecuteSandbox, hasExplicitTargetSelection, type ExecuteResult, type ExecuteTargetSelection } from "./execute.ts"
|
||||
import type { NetworkCaptureOptions, NetworkCaptureResult, NetworkCaptureStatus, NetworkCaptureStopOptions } from "./network-capture.ts"
|
||||
import { generateSessionId } from "./relay-helpers.ts"
|
||||
import type { OpenCodeBrowserSession, ExecuteSandboxLike, SessionSummary, SessionTarget } from "./relay-types.ts"
|
||||
import type { AuthenticatedJsonOutcome, AuthenticatedJsonRequest } from "./relay-schema.ts"
|
||||
import type { PersistedSession } from "./session-catalog.ts"
|
||||
import {
|
||||
MemoryTargetOwnership,
|
||||
type TargetOwnership,
|
||||
type TargetOwnershipChange,
|
||||
type TargetOwnershipReservation,
|
||||
} from "./target-registry.ts"
|
||||
|
||||
export type SessionExecuteRecord = {
|
||||
readonly sessionId: string
|
||||
readonly code: string
|
||||
readonly durationMs: number
|
||||
readonly result: ExecuteResult
|
||||
}
|
||||
|
||||
type CreatedSessionSummary = SessionSummary & { readonly created?: boolean }
|
||||
|
||||
type ExecutionResponse = { readonly result: ExecuteResult; readonly session: CreatedSessionSummary }
|
||||
|
||||
type AdoptionResult = {
|
||||
readonly adoptedUrl: string
|
||||
readonly session: CreatedSessionSummary
|
||||
readonly releasedTargetIds: readonly string[]
|
||||
}
|
||||
|
||||
export type SessionHooks = {
|
||||
/** Called when a session starts (true) or finishes (false) an execute. */
|
||||
readonly onExecuteStateChange?: (sessionId: string, executing: boolean) => void
|
||||
/** Called after each execute completes, for journaling. Failure is logged and ignored. */
|
||||
readonly onExecuteRecord?: (record: SessionExecuteRecord) => unknown | Promise<unknown>
|
||||
/** How long lifecycle commands wait for a permit or sandbox operation. */
|
||||
readonly lifecycleTimeoutMs?: number
|
||||
/** Maximum time best-effort journal I/O may retain an execute permit. */
|
||||
readonly journalTimeoutMs?: number
|
||||
/** Current user-owned attached page URLs, used for relay-side adoption hints. */
|
||||
readonly getUserAttachedPageUrls?: () => readonly string[]
|
||||
/** Reconcile relay visibility and presentation after authoritative ownership changes. */
|
||||
readonly onTargetOwnershipChange?: (change: TargetOwnershipChange) => void
|
||||
/** Close a live relay-owned target by its durable target id. */
|
||||
readonly onReleaseRelayTarget?: (targetId: string) => Effect.Effect<void, Error>
|
||||
/** Persist session identity and target continuity after durable lifecycle changes. */
|
||||
readonly onSessionsChanged?: (sessions: readonly PersistedSession[]) => unknown | Promise<unknown>
|
||||
}
|
||||
|
||||
export class SessionError extends Schema.TaggedError<SessionError>()(
|
||||
"OpenCodeBrowserSessions.SessionError",
|
||||
{
|
||||
message: Schema.String,
|
||||
reason: Schema.Literals([
|
||||
"already-exists",
|
||||
"inactive",
|
||||
"invalid-request",
|
||||
"not-found",
|
||||
"setup-failed",
|
||||
"target-owned",
|
||||
"timeout",
|
||||
]),
|
||||
sessionId: Schema.optionalKey(Schema.String),
|
||||
},
|
||||
) {}
|
||||
|
||||
const sessionError = (
|
||||
reason: SessionError["reason"],
|
||||
message: string,
|
||||
sessionId?: string,
|
||||
): SessionError => new SessionError({ message, reason, ...(sessionId ? { sessionId } : {}) })
|
||||
|
||||
export const adoptionTipForUrl = (url: string): string => {
|
||||
const selector = targetUrlHintSelector(url)
|
||||
return `Tip: an attached tab is open (${url}). Use opencode-browser session adopt --target-url '${selector}' to drive it instead of this new tab.`
|
||||
}
|
||||
|
||||
export const shouldAppendAdoptionTip = (options: {
|
||||
readonly explicitTargetSelection: boolean
|
||||
readonly sessionCreated: boolean
|
||||
readonly warnings: readonly string[]
|
||||
readonly userAttachedPageUrls: readonly string[]
|
||||
}): boolean => {
|
||||
if (options.explicitTargetSelection || options.userAttachedPageUrls.length === 0) {
|
||||
return false
|
||||
}
|
||||
return options.sessionCreated || options.warnings.includes(defaultPageClosedWarning)
|
||||
}
|
||||
|
||||
export class OpenCodeBrowserSessions {
|
||||
readonly sessions = new Map<string, OpenCodeBrowserSession>()
|
||||
private readonly createSandbox: (id: string, onDefaultTargetChange: (target: SessionTarget | undefined) => void) => ExecuteSandboxLike
|
||||
private readonly hooks: SessionHooks
|
||||
private readonly executing = new Set<string>()
|
||||
private readonly adoptSemaphore = Semaphore.makeUnsafe(1)
|
||||
private readonly targetOwnership: TargetOwnership
|
||||
private persistenceTail = Promise.resolve()
|
||||
private drainedPersistenceTail: Promise<void> | undefined
|
||||
private admission: "open" | "draining" | "closed" = "open"
|
||||
private pendingWork = 0
|
||||
private readonly pendingSessionWork = new Map<string, number>()
|
||||
private readonly identityChanges = new Map<string, Deferred.Deferred<void, Error>>()
|
||||
private readonly idle = Latch.makeUnsafe(true)
|
||||
private userAttachedPageUrlsProvider: (() => readonly string[]) | undefined
|
||||
|
||||
constructor(
|
||||
private readonly endpointUrl: string,
|
||||
createSandbox?: (id: string, onDefaultTargetChange: (target: SessionTarget | undefined) => void) => ExecuteSandboxLike,
|
||||
hooks?: SessionHooks,
|
||||
targetOwnership?: TargetOwnership,
|
||||
) {
|
||||
this.createSandbox = createSandbox ?? ((id, onDefaultTargetChange) => new ExecuteSandbox({ endpointUrl: this.endpointUrl, sessionId: id, onDefaultTargetChange }))
|
||||
this.hooks = hooks ?? {}
|
||||
this.targetOwnership = targetOwnership ?? new MemoryTargetOwnership()
|
||||
this.userAttachedPageUrlsProvider = this.hooks.getUserAttachedPageUrls
|
||||
}
|
||||
|
||||
setUserAttachedPageUrlsProvider(provider: () => readonly string[]): void {
|
||||
this.userAttachedPageUrlsProvider = provider
|
||||
}
|
||||
|
||||
listSummaries(): SessionSummary[] {
|
||||
return Array.from(this.sessions.values()).map((session) => {
|
||||
return this.sessionSummary(session)
|
||||
})
|
||||
}
|
||||
|
||||
restore(entries: readonly PersistedSession[]): void {
|
||||
this.assertAdmission()
|
||||
if (this.sessions.size > 0) throw new Error("Cannot restore sessions after session management has started")
|
||||
const ids = new Set<string>()
|
||||
const targetOwners = new Set<string>()
|
||||
for (const entry of entries) {
|
||||
if (ids.has(entry.id)) throw new Error(`Duplicate persisted session: ${entry.id}`)
|
||||
ids.add(entry.id)
|
||||
if (entry.target && targetOwners.has(entry.target.id)) {
|
||||
throw new Error(`Duplicate persisted target owner: ${entry.target.id}`)
|
||||
}
|
||||
if (entry.target) targetOwners.add(entry.target.id)
|
||||
}
|
||||
for (const entry of entries) {
|
||||
const session = this.createOpenCodeBrowserSession(entry.id, entry.readOnly, {
|
||||
createdAt: entry.createdAt,
|
||||
updatedAt: entry.updatedAt,
|
||||
})
|
||||
if (entry.target) session.target = entry.target
|
||||
session.sandbox.restore(entry.target)
|
||||
this.sessions.set(entry.id, session)
|
||||
}
|
||||
}
|
||||
|
||||
persistedTargetOwner(targetId: string): { readonly sessionId: string; readonly owner: "relay" | "user" } | undefined {
|
||||
for (const session of this.sessions.values()) {
|
||||
const target = session.target
|
||||
if (target?.id === targetId) return { sessionId: session.id, owner: target.owner }
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
private updateTarget(session: OpenCodeBrowserSession, target: SessionTarget | undefined): void {
|
||||
if (this.sessions.get(session.id) !== session) return
|
||||
if (session.target?.id === target?.id && session.target?.owner === target?.owner) return
|
||||
if (target) session.target = target
|
||||
else delete session.target
|
||||
session.updatedAt = new Date().toISOString()
|
||||
this.schedulePersistence()
|
||||
}
|
||||
|
||||
createNew(id: string | undefined, options?: { readonly readOnly?: boolean }): OpenCodeBrowserSession {
|
||||
this.assertAdmission()
|
||||
if (id !== undefined) this.assertStableIdentity(id)
|
||||
return this.createAcceptedSession(id, options)
|
||||
}
|
||||
|
||||
private createAcceptedSession(id: string | undefined, options?: { readonly readOnly?: boolean }): OpenCodeBrowserSession {
|
||||
const sessionId = id ?? generateSessionId(this.sessions)
|
||||
if (this.sessions.has(sessionId)) {
|
||||
throw sessionError("already-exists", `Session already exists: ${sessionId}`, sessionId)
|
||||
}
|
||||
const session = this.createOpenCodeBrowserSession(sessionId, options?.readOnly === true)
|
||||
this.sessions.set(sessionId, session)
|
||||
this.schedulePersistence()
|
||||
return session
|
||||
}
|
||||
|
||||
create(id: string | undefined, options?: { readonly readOnly?: boolean }): Effect.Effect<OpenCodeBrowserSession, Error> {
|
||||
return this.withAdmission(id, this.createPersistedSession(id, options).pipe(Effect.uninterruptible))
|
||||
}
|
||||
|
||||
ensure(id: string, options?: { readonly readOnly?: boolean }): Effect.Effect<SessionSummary, Error> {
|
||||
const manager = this
|
||||
return this.withAdmission(id, Effect.gen(function* () {
|
||||
const existing = manager.sessions.get(id)
|
||||
if (existing) {
|
||||
if (options?.readOnly === true && !existing.readOnly) {
|
||||
return yield* sessionError(
|
||||
"invalid-request",
|
||||
`Session ${id} already exists with write access and cannot be ensured as read-only`,
|
||||
id,
|
||||
)
|
||||
}
|
||||
return manager.sessionSummary(existing)
|
||||
}
|
||||
const session = yield* manager.createPersistedSession(id, options)
|
||||
return manager.sessionSummary(session)
|
||||
}).pipe(Effect.uninterruptible))
|
||||
}
|
||||
|
||||
private readonly createPersistedSession = Effect.fnUntraced(function* (this: OpenCodeBrowserSessions, id: string | undefined, options?: { readonly readOnly?: boolean }) {
|
||||
const manager = this
|
||||
const sessionId = id ?? generateSessionId(this.sessions)
|
||||
return yield* manager.withIdentityChange(sessionId, Effect.gen(function* () {
|
||||
const session = manager.createAcceptedSession(sessionId, options)
|
||||
yield* manager.flushPersistenceWithRollback(() => Effect.gen(function* () {
|
||||
if (manager.sessions.get(session.id) === session) manager.sessions.delete(session.id)
|
||||
yield* manager.closeOpenCodeBrowserSession(session)
|
||||
}))
|
||||
return session
|
||||
}))
|
||||
})
|
||||
|
||||
getOrCreate(id: string): { readonly session: OpenCodeBrowserSession; readonly created: boolean } {
|
||||
this.assertAdmission()
|
||||
this.assertStableIdentity(id)
|
||||
return this.getOrCreateAcceptedSession(id)
|
||||
}
|
||||
|
||||
private getOrCreateAcceptedSession(id: string): { readonly session: OpenCodeBrowserSession; readonly created: boolean } {
|
||||
const existing = this.sessions.get(id)
|
||||
if (existing) {
|
||||
return { session: existing, created: false }
|
||||
}
|
||||
return { session: this.createAcceptedSession(id), created: true }
|
||||
}
|
||||
|
||||
isReadOnly(id: string): boolean {
|
||||
return this.sessions.get(id)?.readOnly === true
|
||||
}
|
||||
|
||||
isExecuting(id: string): boolean {
|
||||
return this.executing.has(id)
|
||||
}
|
||||
|
||||
hasPendingWork(id: string): boolean {
|
||||
return this.pendingSessionWork.has(id)
|
||||
}
|
||||
|
||||
hasActiveNetworkCapture(): boolean {
|
||||
return Array.from(this.sessions.values()).some((session) => session.sandbox.networkStatus().active)
|
||||
}
|
||||
|
||||
private collectSessionIds(matches: (session: OpenCodeBrowserSession) => boolean): string[] {
|
||||
const affectedSessionIds: string[] = []
|
||||
for (const session of this.sessions.values()) {
|
||||
if (matches(session)) {
|
||||
affectedSessionIds.push(session.id)
|
||||
}
|
||||
}
|
||||
return affectedSessionIds
|
||||
}
|
||||
|
||||
markTargetCrashed(targetId: string): string[] {
|
||||
return this.collectSessionIds((session) => session.sandbox.markTargetCrashed(targetId))
|
||||
}
|
||||
|
||||
markTargetProtectedUi(targetId: string, protectedUi: boolean): string[] {
|
||||
return this.collectSessionIds((session) => session.sandbox.markTargetProtectedUi(targetId, protectedUi))
|
||||
}
|
||||
|
||||
markTargetDetached(targetId: string): string[] {
|
||||
return this.collectSessionIds((session) => {
|
||||
const detached = session.sandbox.markTargetDetached(targetId)
|
||||
if (session.target?.id === targetId && session.target.owner === "user") {
|
||||
this.notifyTargetOwnershipChange(this.targetOwnership.releaseTargetOwnership(targetId, session.id))
|
||||
}
|
||||
if (session.target?.id === targetId) {
|
||||
this.updateTarget(session, undefined)
|
||||
}
|
||||
return detached
|
||||
})
|
||||
}
|
||||
|
||||
markTargetReplaced(previousTargetId: string, targetId: string): string[] {
|
||||
return this.collectSessionIds((session) => {
|
||||
if (session.target?.id === previousTargetId) this.updateTarget(session, { ...session.target, id: targetId })
|
||||
return session.sandbox.markTargetReplaced(previousTargetId, targetId)
|
||||
})
|
||||
}
|
||||
|
||||
delete(id: string): Effect.Effect<boolean, Error> {
|
||||
const manager = this
|
||||
return this.withAdmission(id, Effect.gen(function* () {
|
||||
const session = manager.sessions.get(id)
|
||||
if (!session) {
|
||||
return false
|
||||
}
|
||||
return yield* manager.withLifecyclePermit(session, "delete", manager.withIdentityChange(id, Effect.gen(function* () {
|
||||
if (manager.sessions.get(id) !== session) {
|
||||
return false
|
||||
}
|
||||
if (session.target?.owner === "relay") yield* manager.closeRelayTarget(session.target.id).pipe(Effect.ignore)
|
||||
manager.sessions.delete(id)
|
||||
yield* manager.commitPersistence(() => Effect.sync(() => {
|
||||
manager.sessions.set(id, session)
|
||||
}))
|
||||
yield* manager.releaseSessionTargetOwnership(session)
|
||||
yield* manager.closeOpenCodeBrowserSession(session)
|
||||
return true
|
||||
})))
|
||||
}))
|
||||
}
|
||||
|
||||
reset(id: string): Effect.Effect<SessionSummary | undefined, Error> {
|
||||
const manager = this
|
||||
return this.withAdmission(id, Effect.gen(function* () {
|
||||
const existing = manager.sessions.get(id)
|
||||
if (!existing) {
|
||||
return undefined
|
||||
}
|
||||
return yield* manager.withLifecyclePermit(existing, "reset", manager.withIdentityChange(id, Effect.gen(function* () {
|
||||
if (manager.sessions.get(id) !== existing) {
|
||||
return yield* sessionError("inactive", `Session is no longer active: ${id}`, id)
|
||||
}
|
||||
if (existing.target?.owner === "relay") yield* manager.closeRelayTarget(existing.target.id).pipe(Effect.ignore)
|
||||
const session = manager.createOpenCodeBrowserSession(id, existing.readOnly)
|
||||
manager.sessions.set(id, session)
|
||||
yield* manager.commitPersistence(() => Effect.sync(() => {
|
||||
manager.sessions.set(id, existing)
|
||||
}))
|
||||
yield* manager.releaseSessionTargetOwnership(existing)
|
||||
yield* manager.closeOpenCodeBrowserSession(existing)
|
||||
return manager.sessionSummary(session)
|
||||
})))
|
||||
}))
|
||||
}
|
||||
|
||||
adoptedTargetId(id: string): string | undefined {
|
||||
const target = this.sessions.get(id)?.target
|
||||
return target?.owner === "user" ? target.id : undefined
|
||||
}
|
||||
|
||||
networkStart(id: string, options: NetworkCaptureOptions = {}): Effect.Effect<NetworkCaptureStatus, Error> {
|
||||
return this.withSessionOperation(id, "network start", (session) => session.sandbox.networkStart(options))
|
||||
}
|
||||
|
||||
networkStatus(id: string): Effect.Effect<NetworkCaptureStatus, Error> {
|
||||
const session = this.sessions.get(id)
|
||||
return session
|
||||
? Effect.succeed(session.sandbox.networkStatus())
|
||||
: Effect.fail(sessionError("not-found", `Session not found: ${id}`, id))
|
||||
}
|
||||
|
||||
networkStop(id: string, options: NetworkCaptureStopOptions = {}): Effect.Effect<NetworkCaptureResult, Error> {
|
||||
return this.withSessionOperation(id, "network stop", (session) => session.sandbox.networkStop(options))
|
||||
}
|
||||
|
||||
networkCancel(id: string): Effect.Effect<{ readonly cancelled: boolean }, Error> {
|
||||
return this.withSessionOperation(id, "network cancel", (session) => session.sandbox.networkCancel())
|
||||
}
|
||||
|
||||
authRefresh(id: string, options: { readonly name: string; readonly urlFilter?: string; readonly timeoutMs?: number }): Effect.Effect<NetworkCaptureResult, Error> {
|
||||
return this.withSessionOperation(id, "auth refresh", (session) => session.sandbox.authRefresh(options))
|
||||
}
|
||||
|
||||
private withSessionOperation<A>(
|
||||
id: string,
|
||||
operation: string,
|
||||
use: (session: OpenCodeBrowserSession) => Effect.Effect<A, Error>,
|
||||
beforePermit?: (session: OpenCodeBrowserSession) => Effect.Effect<void, Error>,
|
||||
): Effect.Effect<A, Error> {
|
||||
const manager = this
|
||||
return this.withAdmission(id, Effect.suspend(() => {
|
||||
const session = manager.sessions.get(id)
|
||||
if (!session) return Effect.fail(sessionError("not-found", `Session not found: ${id}`, id))
|
||||
return (beforePermit ? beforePermit(session) : Effect.void).pipe(
|
||||
Effect.andThen(manager.withLifecyclePermit(session, operation, Effect.gen(function* () {
|
||||
if (manager.sessions.get(id) !== session) {
|
||||
return yield* sessionError("inactive", `Session is no longer active: ${id}`, id)
|
||||
}
|
||||
return yield* use(session)
|
||||
}))),
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
authenticatedJson(
|
||||
request: AuthenticatedJsonRequest,
|
||||
): Effect.Effect<AuthenticatedJsonOutcome, Error> {
|
||||
const manager = this
|
||||
const { sessionId, ...pageRequest } = request
|
||||
return this.withSessionOperation(
|
||||
sessionId,
|
||||
"authenticated request",
|
||||
(session) => Effect.gen(function* () {
|
||||
manager.setExecuting(session.id, true)
|
||||
const result = yield* session.sandbox.authenticatedJson(pageRequest).pipe(
|
||||
Effect.ensuring(Effect.sync(() => manager.setExecuting(session.id, false))),
|
||||
)
|
||||
session.updatedAt = new Date().toISOString()
|
||||
yield* manager.commitPersistence()
|
||||
return result
|
||||
}),
|
||||
(session) => session.readOnly && request.method !== "GET"
|
||||
? Effect.fail(sessionError(
|
||||
"invalid-request",
|
||||
`Read-only session ${sessionId} cannot make ${request.method} authenticated requests`,
|
||||
sessionId,
|
||||
))
|
||||
: Effect.void,
|
||||
)
|
||||
}
|
||||
|
||||
execute(options: {
|
||||
readonly sessionId?: string
|
||||
readonly code: string
|
||||
readonly createIfMissing: boolean
|
||||
readonly targetSelection?: ExecuteTargetSelection
|
||||
}): Effect.Effect<ExecutionResponse, Error> {
|
||||
const manager = this
|
||||
return this.withAdmission(options.sessionId, Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
||||
const resolved = yield* manager.resolveWorkSession(options.sessionId, options.createIfMissing)
|
||||
const session = resolved.session
|
||||
const response = yield* Deferred.make<ExecutionResponse, Error>()
|
||||
let started = false
|
||||
let cancelled = false
|
||||
const operation = Effect.gen(function* () {
|
||||
if (cancelled) {
|
||||
return yield* sessionError("inactive", `Session execute was cancelled before starting: ${session.id}`, session.id)
|
||||
}
|
||||
if (manager.sessions.get(session.id) !== session) {
|
||||
return yield* sessionError("inactive", `Session is no longer active: ${session.id}`, session.id)
|
||||
}
|
||||
started = true
|
||||
session.updatedAt = new Date().toISOString()
|
||||
manager.setExecuting(session.id, true)
|
||||
const startedAt = Date.now()
|
||||
const result = yield* session.sandbox
|
||||
.execute(options.code, { ...(options.targetSelection ? { targetSelection: options.targetSelection } : {}) })
|
||||
.pipe(Effect.ensuring(Effect.sync(() => manager.setExecuting(session.id, false))))
|
||||
if (resolved.created && result.setupFailed) {
|
||||
return yield* sessionError("setup-failed", result.text, session.id)
|
||||
}
|
||||
const userAttachedPageUrls = manager.userAttachedPageUrlsProvider?.() ?? []
|
||||
const resultWithHint = shouldAppendAdoptionTip({
|
||||
explicitTargetSelection: hasExplicitTargetSelection(options.targetSelection),
|
||||
sessionCreated: resolved.created,
|
||||
warnings: result.warnings,
|
||||
userAttachedPageUrls,
|
||||
})
|
||||
? { ...result, warnings: [...result.warnings, adoptionTipForUrl(userAttachedPageUrls[0] ?? "about:blank")] }
|
||||
: result
|
||||
session.updatedAt = new Date().toISOString()
|
||||
yield* manager.recordExecute({
|
||||
sessionId: session.id,
|
||||
code: session.sandbox.redactNetworkCaptureText(options.code),
|
||||
durationMs: Date.now() - startedAt,
|
||||
result: resultWithHint,
|
||||
})
|
||||
yield* manager.commitPersistence()
|
||||
return { result: resultWithHint, session: manager.createdSummary(session, resolved.created) }
|
||||
})
|
||||
const worker = session.executeSemaphore.withPermit(operation.pipe(Effect.matchEffect({
|
||||
onFailure: (error) => (resolved.created
|
||||
? manager.cleanupCreatedSession(session)
|
||||
: Effect.void).pipe(Effect.andThen(Deferred.fail(response, error))),
|
||||
onSuccess: (value) => Deferred.succeed(response, value),
|
||||
})))
|
||||
const workerFiber = yield* manager.forkTracked(worker, session.id)
|
||||
return yield* restore(Deferred.await(response)).pipe(
|
||||
Effect.onInterrupt(() => Effect.suspend(() => {
|
||||
if (started) return Effect.void
|
||||
cancelled = true
|
||||
return Fiber.interrupt(workerFiber).pipe(
|
||||
Effect.asVoid,
|
||||
Effect.andThen(resolved.created
|
||||
? session.executeSemaphore.withPermit(manager.cleanupCreatedSession(session))
|
||||
: Effect.void),
|
||||
)
|
||||
})),
|
||||
)
|
||||
})))
|
||||
}
|
||||
|
||||
adopt(options: {
|
||||
readonly sessionId?: string
|
||||
readonly createIfMissing: boolean
|
||||
readonly targetId: string
|
||||
readonly targetUrl: string
|
||||
}): Effect.Effect<AdoptionResult, Error> {
|
||||
const manager = this
|
||||
return this.withAdmission(options.sessionId, Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
||||
const resolved = yield* manager.resolveWorkSession(options.sessionId, options.createIfMissing)
|
||||
const session = resolved.session
|
||||
const result = yield* Deferred.make<AdoptionResult, Error>()
|
||||
let state: "pending" | "reserved" | "committed" = "pending"
|
||||
let cancelled = false
|
||||
let reservation: TargetOwnershipReservation | undefined
|
||||
let previousTarget: SessionTarget | undefined
|
||||
let previousRelayClosed = false
|
||||
const timeoutMs = manager.hooks.lifecycleTimeoutMs ?? 10_000
|
||||
const timeoutError = sessionError("timeout", `Session adopt for ${session.id} timed out after ${timeoutMs}ms`, session.id)
|
||||
const adoptionCancelled = () => cancelled
|
||||
const cancel = Effect.sync(() => {
|
||||
if (cancelled) {
|
||||
return
|
||||
}
|
||||
cancelled = true
|
||||
if (reservation && state === "reserved") {
|
||||
manager.notifyTargetOwnershipChange(manager.targetOwnership.rollbackTargetOwnership(reservation))
|
||||
}
|
||||
})
|
||||
const operation = Effect.gen(function* () {
|
||||
if (manager.sessions.get(session.id) !== session) {
|
||||
return yield* sessionError("inactive", `Session is no longer active: ${session.id}`, session.id)
|
||||
}
|
||||
if (adoptionCancelled()) {
|
||||
return yield* timeoutError
|
||||
}
|
||||
reservation = yield* Effect.try({
|
||||
try: () => manager.targetOwnership.reserveTargetOwnership(options.targetId, session.id),
|
||||
catch: (cause) => cause instanceof Error ? cause : new Error("Reserve target ownership", { cause }),
|
||||
})
|
||||
state = "reserved"
|
||||
manager.notifyTargetOwnershipChange({ targetIds: [options.targetId], tabIds: reservation.tabId < 0 ? [] : [reservation.tabId] })
|
||||
previousTarget = session.target
|
||||
yield* session.sandbox.adoptPage(options.targetId)
|
||||
if (adoptionCancelled()) {
|
||||
return yield* timeoutError
|
||||
}
|
||||
const activeReservation = reservation
|
||||
previousTarget = session.target ?? previousTarget
|
||||
const previousTargetId = previousTarget?.id
|
||||
if (previousTarget?.owner === "relay" && previousTarget.id !== options.targetId) {
|
||||
yield* manager.closeRelayTarget(previousTarget.id)
|
||||
previousRelayClosed = true
|
||||
}
|
||||
session.target = { id: options.targetId, owner: "user" }
|
||||
session.updatedAt = new Date().toISOString()
|
||||
yield* manager.commitPersistence()
|
||||
if (adoptionCancelled()) {
|
||||
return yield* timeoutError
|
||||
}
|
||||
yield* Effect.try({
|
||||
try: () => {
|
||||
manager.notifyTargetOwnershipChange(manager.targetOwnership.commitTargetOwnership({
|
||||
reservation: activeReservation,
|
||||
...(previousTargetId ? { previousAdoptedTargetId: previousTargetId } : {}),
|
||||
}))
|
||||
state = "committed"
|
||||
},
|
||||
catch: (cause) => cause instanceof Error ? cause : new Error("Commit target ownership", { cause }),
|
||||
})
|
||||
const releasedTargetIds = previousTargetId && previousTargetId !== options.targetId ? [previousTargetId] : []
|
||||
const value = { adoptedUrl: options.targetUrl, releasedTargetIds, session: manager.createdSummary(session, resolved.created) }
|
||||
yield* Deferred.succeed(result, value)
|
||||
return value
|
||||
})
|
||||
const transaction = session.executeSemaphore.withPermit(operation.pipe(Effect.matchEffect({
|
||||
onFailure: (error) => Effect.gen(function* () {
|
||||
if (reservation && state !== "committed") {
|
||||
manager.notifyTargetOwnershipChange(manager.targetOwnership.rollbackTargetOwnership(reservation))
|
||||
}
|
||||
if (manager.sessions.get(session.id) === session && (resolved.created || state !== "pending")) {
|
||||
const cleanup = yield* Effect.result(manager.cleanupSettledAdoption(session, resolved.created, previousTarget, previousRelayClosed))
|
||||
if (Result.isFailure(cleanup)) {
|
||||
yield* Deferred.fail(result, cleanup.failure)
|
||||
return
|
||||
}
|
||||
}
|
||||
yield* Deferred.fail(result, error)
|
||||
}),
|
||||
onSuccess: () => Effect.void,
|
||||
})))
|
||||
const worker = manager.adoptSemaphore.withPermit(transaction)
|
||||
yield* manager.forkTracked(worker, session.id)
|
||||
return yield* restore(Deferred.await(result).pipe(
|
||||
Effect.timeoutOrElse({
|
||||
duration: timeoutMs,
|
||||
orElse: () => cancel.pipe(Effect.andThen(Effect.fail(timeoutError))),
|
||||
}),
|
||||
Effect.onInterrupt(() => cancel),
|
||||
))
|
||||
})))
|
||||
}
|
||||
|
||||
/** Stop admission without cancelling accepted work or disconnecting sessions. */
|
||||
beginDrain(): Effect.Effect<void, Error> {
|
||||
const manager = this
|
||||
return Effect.gen(function* () {
|
||||
if (manager.admission === "open") manager.admission = "draining"
|
||||
while (true) {
|
||||
yield* manager.idle.await
|
||||
const pending = manager.persistenceTail
|
||||
yield* manager.flushPersistence()
|
||||
// Target callbacks can append persistence while the previous tail settles.
|
||||
if (manager.pendingWork === 0 && pending === manager.persistenceTail) {
|
||||
manager.drainedPersistenceTail = pending
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** Recheck synchronously at commit: late catalog writes invalidate a completed drain. */
|
||||
isDrained(): boolean {
|
||||
return this.admission !== "open"
|
||||
&& this.pendingWork === 0
|
||||
&& this.drainedPersistenceTail === this.persistenceTail
|
||||
}
|
||||
|
||||
/** Call only after cancelling or completing the reversible drain waiter. */
|
||||
resume(): void {
|
||||
if (this.admission === "draining") this.admission = "open"
|
||||
}
|
||||
|
||||
closeAll(): Effect.Effect<void> {
|
||||
const manager = this
|
||||
return Effect.gen(function* () {
|
||||
manager.admission = "closed"
|
||||
yield* manager.beginDrain().pipe(Effect.ignore)
|
||||
yield* manager.adoptSemaphore.withPermit(Effect.gen(function* () {
|
||||
yield* Effect.forEach(Array.from(manager.sessions.values()), (session) => {
|
||||
return session.executeSemaphore.withPermit(
|
||||
manager.disconnectOpenCodeBrowserSession(session),
|
||||
)
|
||||
}, { concurrency: "unbounded", discard: true })
|
||||
yield* manager.flushPersistence().pipe(Effect.ignore)
|
||||
manager.sessions.clear()
|
||||
}))
|
||||
}).pipe(Effect.uninterruptible)
|
||||
}
|
||||
|
||||
summary(id: string): SessionSummary | undefined {
|
||||
const session = this.sessions.get(id)
|
||||
if (!session) {
|
||||
return undefined
|
||||
}
|
||||
return this.sessionSummary(session)
|
||||
}
|
||||
|
||||
private setExecuting(id: string, executing: boolean): void {
|
||||
if (executing) {
|
||||
this.executing.add(id)
|
||||
} else {
|
||||
this.executing.delete(id)
|
||||
}
|
||||
try {
|
||||
this.hooks.onExecuteStateChange?.(id, executing)
|
||||
} catch (error) {
|
||||
console.error("Session execute-state hook failed", error)
|
||||
}
|
||||
}
|
||||
|
||||
private recordExecute(record: SessionExecuteRecord): Effect.Effect<void> {
|
||||
const hook = this.hooks.onExecuteRecord
|
||||
if (!hook) return Effect.void
|
||||
return Effect.tryPromise({
|
||||
try: async () => {
|
||||
const release = this.retainWork(record.sessionId)
|
||||
try {
|
||||
return await hook(record)
|
||||
} finally {
|
||||
release()
|
||||
}
|
||||
},
|
||||
catch: (cause) => cause,
|
||||
}).pipe(
|
||||
Effect.timeoutOrElse({
|
||||
duration: this.hooks.journalTimeoutMs ?? 2_000,
|
||||
orElse: () => Effect.fail(new Error(`Session journal write timed out after ${this.hooks.journalTimeoutMs ?? 2_000}ms`)),
|
||||
}),
|
||||
Effect.catch((error) => Effect.sync(() => {
|
||||
console.error("Session execute-record hook failed", error)
|
||||
})),
|
||||
Effect.asVoid,
|
||||
)
|
||||
}
|
||||
|
||||
private createOpenCodeBrowserSession(id: string, readOnly: boolean, timestamps?: {
|
||||
readonly createdAt: string
|
||||
readonly updatedAt?: string
|
||||
}): OpenCodeBrowserSession {
|
||||
const now = new Date().toISOString()
|
||||
let session: OpenCodeBrowserSession | undefined
|
||||
session = {
|
||||
id,
|
||||
createdAt: timestamps?.createdAt ?? now,
|
||||
updatedAt: timestamps?.updatedAt ?? now,
|
||||
readOnly,
|
||||
sandbox: this.createSandbox(id, (target) => {
|
||||
if (session) this.updateTarget(session, target)
|
||||
}),
|
||||
executeSemaphore: Semaphore.makeUnsafe(1),
|
||||
}
|
||||
return session
|
||||
}
|
||||
|
||||
/** Resolve execute/adopt work before any permit; the caller owns cleanup of a created session. */
|
||||
private resolveWorkSession(id: string | undefined, createIfMissing: boolean): Effect.Effect<{ readonly session: OpenCodeBrowserSession; readonly created: boolean }, SessionError> {
|
||||
return Effect.suspend(() => {
|
||||
if (id === undefined) {
|
||||
return createIfMissing
|
||||
? Effect.succeed({ session: this.createAcceptedSession(undefined), created: true })
|
||||
: Effect.fail(sessionError("invalid-request", "sessionId is required when createIfMissing is false"))
|
||||
}
|
||||
if (createIfMissing) return Effect.succeed(this.getOrCreateAcceptedSession(id))
|
||||
const session = this.sessions.get(id)
|
||||
return session ? Effect.succeed({ session, created: false }) : Effect.fail(sessionError("not-found", `Session not found: ${id}`, id))
|
||||
})
|
||||
}
|
||||
|
||||
private createdSummary(session: OpenCodeBrowserSession, created: boolean): CreatedSessionSummary {
|
||||
return { ...this.sessionSummary(session), ...(created ? { created: true } : {}) }
|
||||
}
|
||||
|
||||
private sessionSummary(session: OpenCodeBrowserSession): SessionSummary {
|
||||
const status = session.sandbox.getStatus()
|
||||
return {
|
||||
id: session.id,
|
||||
createdAt: session.createdAt,
|
||||
updatedAt: session.updatedAt,
|
||||
connected: status.connected,
|
||||
pageUrl: status.pageUrl,
|
||||
stateKeys: status.stateKeys,
|
||||
...(session.readOnly ? { readOnly: true } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
private closeOpenCodeBrowserSession(session: OpenCodeBrowserSession): Effect.Effect<void> {
|
||||
return this.forkTracked(this.closeOpenCodeBrowserSessionSettled(session), session.id).pipe(
|
||||
Effect.flatMap((worker) => this.withLifecycleTimeout(Fiber.join(worker), `Close session ${session.id}`)),
|
||||
Effect.ignore,
|
||||
)
|
||||
}
|
||||
|
||||
private disconnectOpenCodeBrowserSession(session: OpenCodeBrowserSession): Effect.Effect<void> {
|
||||
return Effect.acquireUseRelease(
|
||||
Effect.sync(() => this.retainWork(session.id)),
|
||||
() => session.sandbox.disconnectSettled().pipe(Effect.ignore),
|
||||
(release) => Effect.sync(release),
|
||||
)
|
||||
}
|
||||
|
||||
private closeOpenCodeBrowserSessionSettled(session: OpenCodeBrowserSession): Effect.Effect<void> {
|
||||
return session.sandbox.closeSettled().pipe(Effect.ignore)
|
||||
}
|
||||
|
||||
private releaseSessionTargetOwnership(session: OpenCodeBrowserSession): Effect.Effect<void> {
|
||||
const target = session.target
|
||||
if (!target) return Effect.void
|
||||
this.notifyTargetOwnershipChange(this.targetOwnership.releaseTargetOwnership(target.id, session.id))
|
||||
delete session.target
|
||||
return Effect.void
|
||||
}
|
||||
|
||||
private closeRelayTarget(targetId: string): Effect.Effect<void, Error> {
|
||||
const close = this.hooks.onReleaseRelayTarget?.(targetId)
|
||||
return close ?? Effect.void
|
||||
}
|
||||
|
||||
private notifyTargetOwnershipChange(change: TargetOwnershipChange): void {
|
||||
if (change.targetIds.length === 0 && change.tabIds.length === 0) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
this.hooks.onTargetOwnershipChange?.(change)
|
||||
} catch (error) {
|
||||
console.error("Target ownership hook failed", error)
|
||||
}
|
||||
}
|
||||
|
||||
private persistedSession(session: OpenCodeBrowserSession): PersistedSession {
|
||||
const target = session.target
|
||||
return {
|
||||
id: session.id,
|
||||
createdAt: session.createdAt,
|
||||
updatedAt: session.updatedAt,
|
||||
readOnly: session.readOnly,
|
||||
...(target ? { target } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
private persistedSessions(): PersistedSession[] {
|
||||
return Array.from(this.sessions.values(), (session) => this.persistedSession(session))
|
||||
}
|
||||
|
||||
private schedulePersistence(sessions = this.persistedSessions()): void {
|
||||
const hook = this.hooks.onSessionsChanged
|
||||
if (!hook) return
|
||||
const release = this.retainWork()
|
||||
const previous = this.persistenceTail
|
||||
this.persistenceTail = previous.catch(() => {}).then(async () => {
|
||||
try {
|
||||
await hook(sessions)
|
||||
} finally {
|
||||
release()
|
||||
}
|
||||
})
|
||||
void this.persistenceTail.catch((error) => {
|
||||
console.error("Failed to persist OpenCode Browser sessions", error)
|
||||
})
|
||||
}
|
||||
|
||||
private flushPersistence(): Effect.Effect<void, Error> {
|
||||
return Effect.tryPromise({
|
||||
try: () => this.persistenceTail,
|
||||
catch: (cause) => cause instanceof Error ? cause : new Error("Persist OpenCode Browser sessions", { cause }),
|
||||
})
|
||||
}
|
||||
|
||||
private commitPersistence(rollback?: () => Effect.Effect<void>): Effect.Effect<void, Error> {
|
||||
this.schedulePersistence()
|
||||
return rollback ? this.flushPersistenceWithRollback(rollback) : this.flushPersistence()
|
||||
}
|
||||
|
||||
private flushPersistenceWithRollback(rollback: () => Effect.Effect<void>): Effect.Effect<void, Error> {
|
||||
return this.flushPersistence().pipe(Effect.catch((error) => Effect.gen({ self: this }, function* () {
|
||||
yield* rollback()
|
||||
this.schedulePersistence()
|
||||
yield* this.flushPersistence().pipe(Effect.ignore)
|
||||
return yield* Effect.fail(error)
|
||||
})))
|
||||
}
|
||||
|
||||
persist(): Effect.Effect<void, Error> {
|
||||
return this.flushPersistence()
|
||||
}
|
||||
|
||||
private cleanupSettledAdoption(
|
||||
session: OpenCodeBrowserSession,
|
||||
created: boolean,
|
||||
previousTarget?: SessionTarget,
|
||||
previousRelayClosed = false,
|
||||
): Effect.Effect<void, Error> {
|
||||
const manager = this
|
||||
return Effect.gen(function* () {
|
||||
const activeTarget = session.target
|
||||
yield* manager.releaseSessionTargetOwnership(session)
|
||||
if (previousTarget && previousTarget.id !== activeTarget?.id) {
|
||||
manager.notifyTargetOwnershipChange(manager.targetOwnership.releaseTargetOwnership(previousTarget.id, session.id))
|
||||
if (previousTarget.owner === "relay" && !previousRelayClosed) yield* manager.closeRelayTarget(previousTarget.id)
|
||||
}
|
||||
yield* manager.closeOpenCodeBrowserSessionSettled(session)
|
||||
if (manager.sessions.get(session.id) !== session) {
|
||||
return
|
||||
}
|
||||
if (created) {
|
||||
manager.sessions.delete(session.id)
|
||||
} else {
|
||||
const replacement = manager.createOpenCodeBrowserSession(session.id, session.readOnly, {
|
||||
createdAt: session.createdAt,
|
||||
})
|
||||
manager.sessions.set(session.id, replacement)
|
||||
}
|
||||
yield* manager.commitPersistence()
|
||||
})
|
||||
}
|
||||
|
||||
private cleanupCreatedSession(session: OpenCodeBrowserSession): Effect.Effect<void> {
|
||||
const manager = this
|
||||
return Effect.gen(function* () {
|
||||
if (manager.sessions.get(session.id) !== session) return
|
||||
manager.sessions.delete(session.id)
|
||||
yield* manager.releaseSessionTargetOwnership(session)
|
||||
yield* manager.closeOpenCodeBrowserSession(session)
|
||||
yield* manager.commitPersistence().pipe(Effect.ignore)
|
||||
})
|
||||
}
|
||||
|
||||
private assertAdmission(): void {
|
||||
if (this.admission !== "open") {
|
||||
throw sessionError("inactive", this.admission === "closed"
|
||||
? "OpenCode Browser sessions are closing"
|
||||
: "OpenCode Browser sessions are draining")
|
||||
}
|
||||
}
|
||||
|
||||
private assertStableIdentity(id: string): void {
|
||||
if (this.identityChanges.has(id)) {
|
||||
throw sessionError("inactive", `Session lifecycle change is still committing: ${id}`, id)
|
||||
}
|
||||
}
|
||||
|
||||
private awaitStableIdentity(id: string | undefined): Effect.Effect<void, Error> {
|
||||
return Effect.suspend(() => {
|
||||
const pending = id === undefined ? undefined : this.identityChanges.get(id)
|
||||
return pending ? Deferred.await(pending).pipe(Effect.andThen(this.awaitStableIdentity(id))) : Effect.void
|
||||
})
|
||||
}
|
||||
|
||||
/** Keep same-ID callers behind the entire commit/rollback, including corrective persistence. */
|
||||
private withIdentityChange<A>(id: string, effect: Effect.Effect<A, Error>): Effect.Effect<A, Error> {
|
||||
return Effect.suspend(() => {
|
||||
// Check and claim synchronously: the run loop may yield between separate effects.
|
||||
const pending = this.identityChanges.get(id)
|
||||
if (pending) return Deferred.await(pending).pipe(Effect.andThen(this.withIdentityChange(id, effect)))
|
||||
const settled = Deferred.makeUnsafe<void, Error>()
|
||||
this.identityChanges.set(id, settled)
|
||||
return effect.pipe(Effect.onExit((exit) => Effect.suspend(() => {
|
||||
if (this.identityChanges.get(id) === settled) this.identityChanges.delete(id)
|
||||
// Waiters observe the change's failure or interruption and do not proceed past it.
|
||||
return Deferred.done(settled, Exit.asVoid(exit))
|
||||
})))
|
||||
})
|
||||
}
|
||||
|
||||
private retainWork(sessionId?: string): () => void {
|
||||
this.pendingWork += 1
|
||||
if (sessionId !== undefined) this.pendingSessionWork.set(sessionId, (this.pendingSessionWork.get(sessionId) ?? 0) + 1)
|
||||
this.idle.closeUnsafe()
|
||||
return () => {
|
||||
this.pendingWork -= 1
|
||||
if (sessionId !== undefined) {
|
||||
const remaining = (this.pendingSessionWork.get(sessionId) ?? 1) - 1
|
||||
if (remaining === 0) this.pendingSessionWork.delete(sessionId)
|
||||
else this.pendingSessionWork.set(sessionId, remaining)
|
||||
}
|
||||
if (this.pendingWork === 0) this.idle.openUnsafe()
|
||||
}
|
||||
}
|
||||
|
||||
private withAdmission<A, E, R>(sessionId: string | undefined, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E | Error, R> {
|
||||
return Effect.acquireUseRelease(
|
||||
Effect.try({
|
||||
try: () => {
|
||||
this.assertAdmission()
|
||||
return this.retainWork(sessionId)
|
||||
},
|
||||
catch: (cause) => cause instanceof Error ? cause : new Error("Admit session operation", { cause }),
|
||||
}),
|
||||
() => this.awaitStableIdentity(sessionId).pipe(Effect.andThen(effect)),
|
||||
(release) => Effect.sync(release),
|
||||
)
|
||||
}
|
||||
|
||||
private forkTracked<A, E, R>(effect: Effect.Effect<A, E, R>, sessionId: string): Effect.Effect<Fiber.Fiber<A, E>, never, R> {
|
||||
return Effect.uninterruptibleMask(() => Effect.suspend(() => {
|
||||
// Transfer the lease before forking; caller interruption cannot strand it.
|
||||
const release = this.retainWork(sessionId)
|
||||
return effect.pipe(
|
||||
Effect.ensuring(Effect.sync(release)),
|
||||
Effect.forkDetach({ startImmediately: true }),
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
private withLifecyclePermit<A, E, R>(
|
||||
session: OpenCodeBrowserSession,
|
||||
operation: string,
|
||||
effect: Effect.Effect<A, E, R>,
|
||||
): Effect.Effect<A, E | Error, R> {
|
||||
return Effect.suspend(() => {
|
||||
let state: "waiting" | "started" | "timed-out" = "waiting"
|
||||
const timeout = sessionError("timeout", `Session ${operation} timed out waiting for active execute in ${session.id}`, session.id)
|
||||
// withPermit installs release atomically; racing a bare take can lose a granted permit.
|
||||
return Effect.raceFirst(
|
||||
session.executeSemaphore.withPermit(Effect.suspend((): Effect.Effect<A, E | Error, R> => {
|
||||
if (state === "timed-out") return Effect.fail(timeout)
|
||||
state = "started"
|
||||
return effect
|
||||
}).pipe(Effect.uninterruptible)),
|
||||
Effect.sleep(this.hooks.lifecycleTimeoutMs ?? 10_000).pipe(Effect.flatMap(() => {
|
||||
if (state === "started") return Effect.never
|
||||
state = "timed-out"
|
||||
return Effect.fail(timeout)
|
||||
})),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
private withLifecycleTimeout<A, E, R>(effect: Effect.Effect<A, E, R>, label: string): Effect.Effect<A, E | Error, R> {
|
||||
const timeoutMs = this.hooks.lifecycleTimeoutMs ?? 10_000
|
||||
return effect.pipe(
|
||||
Effect.timeoutOrElse({
|
||||
duration: timeoutMs,
|
||||
orElse: () => Effect.fail(sessionError("timeout", `${label} timed out after ${timeoutMs}ms`)),
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function targetUrlHintSelector(rawUrl: string): string {
|
||||
try {
|
||||
const parsed = new URL(rawUrl)
|
||||
return parsed.host || rawUrl
|
||||
} catch {
|
||||
return rawUrl
|
||||
}
|
||||
}
|
||||
129
packages/browser-extension/cli/src/session-store.ts
Normal file
129
packages/browser-extension/cli/src/session-store.ts
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
import { defaultPort } from "./relay-helpers.ts"
|
||||
import { dataRoot } from "./paths.ts"
|
||||
import { Context, Effect, FileSystem, Layer, Option, Path, Predicate, Schema } from "effect"
|
||||
import os from "node:os"
|
||||
import { endpointForPort, portConfig } from "./relay-client.ts"
|
||||
|
||||
/**
|
||||
* SessionStore persists the CLI's current OpenCode Browser session id, scoped
|
||||
* by relay endpoint so switching `OPENCODE_BROWSER_PORT` cannot silently reuse
|
||||
* a session id created against a different relay.
|
||||
*
|
||||
* File format (v2): `{ "endpoints": { "<endpoint>": { "id": "..." } } }`.
|
||||
* Legacy `{ "id": "..." }` files are read as belonging to the default
|
||||
* endpoint and migrated on the next write.
|
||||
*/
|
||||
|
||||
export class SessionStoreError extends Schema.TaggedError<SessionStoreError>()(
|
||||
"SessionStore.SessionStoreError",
|
||||
{
|
||||
message: Schema.String,
|
||||
operation: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {}
|
||||
|
||||
const StoreFile = Schema.Struct({
|
||||
id: Schema.optionalKey(Schema.String),
|
||||
endpoints: Schema.optionalKey(Schema.Record(Schema.String, Schema.Struct({
|
||||
id: Schema.String,
|
||||
}))),
|
||||
})
|
||||
|
||||
export interface Interface {
|
||||
readonly endpoint: string
|
||||
readonly filePath: string
|
||||
readonly read: Effect.Effect<string | undefined, SessionStoreError>
|
||||
readonly write: (id: string) => Effect.Effect<void, SessionStoreError>
|
||||
readonly clear: Effect.Effect<void, SessionStoreError>
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Interface>()("opencode-browser-cli/SessionStore") {}
|
||||
|
||||
const defaultFilePath = (): string => {
|
||||
return `${dataRoot()}/session.json`
|
||||
}
|
||||
|
||||
export const make = Effect.fn("SessionStore.make")(function* (options?: {
|
||||
readonly filePath?: string
|
||||
readonly endpoint?: string
|
||||
}) {
|
||||
const fs = yield* FileSystem.FileSystem
|
||||
const path = yield* Path.Path
|
||||
const filePath = options?.filePath ?? defaultFilePath()
|
||||
const port = yield* portConfig.pipe(
|
||||
Effect.mapError((cause) => new SessionStoreError({
|
||||
message: `Invalid OPENCODE_BROWSER_PORT configuration: ${cause.message}`,
|
||||
operation: "configure",
|
||||
cause,
|
||||
})),
|
||||
)
|
||||
const endpoint = options?.endpoint ?? endpointForPort(port)
|
||||
const defaultEndpoint = endpointForPort(defaultPort)
|
||||
|
||||
const storeError = (operation: string) => (cause: unknown) =>
|
||||
new SessionStoreError({
|
||||
message: `Could not ${operation} the current session file at ${filePath}`,
|
||||
operation,
|
||||
cause,
|
||||
})
|
||||
|
||||
const readStore: Effect.Effect<Schema.Schema.Type<typeof StoreFile>, SessionStoreError> = fs
|
||||
.readFileString(filePath)
|
||||
.pipe(
|
||||
Effect.map(Option.some),
|
||||
Effect.catchTag("PlatformError", (error) =>
|
||||
Predicate.isTagged(error.reason, "NotFound") ? Effect.succeed(Option.none<string>()) : Effect.fail(error)),
|
||||
Effect.mapError(storeError("read")),
|
||||
Effect.flatMap(Option.match({
|
||||
onNone: () => Effect.succeed({}),
|
||||
onSome: (text) => Effect.try({
|
||||
try: () => JSON.parse(text) as unknown,
|
||||
catch: (cause) => cause,
|
||||
}).pipe(
|
||||
Effect.flatMap(Schema.decodeUnknownEffect(StoreFile)),
|
||||
Effect.mapError(storeError("decode")),
|
||||
),
|
||||
})),
|
||||
)
|
||||
|
||||
const currentEntries = (store: Schema.Schema.Type<typeof StoreFile>): Record<string, { readonly id: string }> => {
|
||||
const entries = { ...(store.endpoints ?? {}) }
|
||||
// Migrate the legacy top-level id to the default endpoint.
|
||||
if (store.id && entries[defaultEndpoint] === undefined) {
|
||||
entries[defaultEndpoint] = { id: store.id }
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
const writeStore = (entries: Record<string, { readonly id: string }>) =>
|
||||
fs.makeDirectory(path.dirname(filePath), { recursive: true }).pipe(
|
||||
Effect.andThen(fs.writeFileString(filePath, `${JSON.stringify({ endpoints: entries }, null, 2)}\n`)),
|
||||
Effect.mapError(storeError("write")),
|
||||
)
|
||||
|
||||
const read = readStore.pipe(
|
||||
Effect.map((store) => currentEntries(store)[endpoint]?.id),
|
||||
)
|
||||
|
||||
const write = (id: string) =>
|
||||
readStore.pipe(
|
||||
Effect.flatMap((store) => {
|
||||
const entries = currentEntries(store)
|
||||
entries[endpoint] = { id }
|
||||
return writeStore(entries)
|
||||
}),
|
||||
)
|
||||
|
||||
const clear = readStore.pipe(
|
||||
Effect.flatMap((store) => {
|
||||
const entries = currentEntries(store)
|
||||
delete entries[endpoint]
|
||||
return writeStore(entries)
|
||||
}),
|
||||
)
|
||||
|
||||
return Service.of({ endpoint, filePath, read, write, clear })
|
||||
})
|
||||
|
||||
export const layer: Layer.Layer<Service, SessionStoreError, FileSystem.FileSystem | Path.Path> = Layer.effect(Service, make())
|
||||
575
packages/browser-extension/cli/src/setup.ts
Normal file
575
packages/browser-extension/cli/src/setup.ts
Normal file
|
|
@ -0,0 +1,575 @@
|
|||
// Setup for OpenCode Browser, without changing opencode itself:
|
||||
// install install the runtime, add the browse MCP server to opencode's config, copy the unpacked extension
|
||||
// (reloading a connected one), then check which browsers have the extension and open the Chrome Web
|
||||
// Store listing when none does
|
||||
// extension open the unpacked extension folder (for "Load unpacked")
|
||||
// uninstall remove everything install wrote
|
||||
// Earlier versions registered a native messaging host and wrote an opencode plugin; install and uninstall
|
||||
// remove those leftovers.
|
||||
import { spawn, spawnSync } from "node:child_process"
|
||||
import { createHash } from "node:crypto"
|
||||
import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"
|
||||
import { homedir } from "node:os"
|
||||
import path from "node:path"
|
||||
import { fileURLToPath } from "node:url"
|
||||
import { applyEdits, modify, parse } from "jsonc-parser"
|
||||
import { dataRoot } from "./paths.ts"
|
||||
import { opencodeBrowserVersion } from "./version.ts"
|
||||
|
||||
const STORE_ID = "mfnicocicmmlkpjnaffgihfjhdgjkdjg"
|
||||
/** The unpacked build; its ID is pinned by the manifest key. */
|
||||
const UNPACKED_ID = "afeafocngkodbmaipcngoamamfmekgfo"
|
||||
const STORE_URL = `https://chromewebstore.google.com/detail/${STORE_ID}`
|
||||
const PACKAGE = "opencode-browser-cli"
|
||||
const MCP_NAME = "browse"
|
||||
/** The MCP server's name before it was renamed to `browse`. */
|
||||
const LEGACY_MCP_NAME = "opencode-browser"
|
||||
const LEGACY_HOST_NAME = "ai.opencode.browser"
|
||||
const LEGACY_REGISTRY_ROOTS = ["HKCU\\Software\\Google\\Chrome", "HKCU\\Software\\Microsoft\\Edge"]
|
||||
const windows = process.platform === "win32"
|
||||
|
||||
const home = homedir()
|
||||
const root = dataRoot()
|
||||
const configRoot = process.env.XDG_CONFIG_HOME ?? path.join(home, ".config")
|
||||
const files = {
|
||||
dir: root,
|
||||
/** A pinned npm install of this package: the MCP server runs from here, not the npx cache. */
|
||||
runtime: path.join(root, "runtime"),
|
||||
settings: path.join(root, "settings.json"),
|
||||
extension: path.join(root, "extension"),
|
||||
}
|
||||
/** Files the native messaging host of earlier versions left behind. */
|
||||
const legacyFiles = [
|
||||
path.join(root, "host"),
|
||||
path.join(root, "host.bat"),
|
||||
path.join(root, `${LEGACY_HOST_NAME}.json`),
|
||||
path.join(root, "state.json"),
|
||||
path.join(root, "logs", "host.log"),
|
||||
path.join(configRoot, "opencode", "plugins", "opencode-browser.ts"),
|
||||
]
|
||||
/** This package's root: dist/cli.mjs when published, src/setup.ts from source. */
|
||||
const packageRoot = path.dirname(path.dirname(fileURLToPath(import.meta.url)))
|
||||
/** Running from a source checkout without a package to install: the MCP server runs main.ts in place with bun. */
|
||||
const fromSource = existsSync(path.join(packageRoot, "src", "main.ts")) && !process.env.OPENCODE_BROWSER_PACKAGE
|
||||
const bundledExtension = path.join(packageRoot, "extension")
|
||||
|
||||
type Settings = { opencode?: string; entry?: string; version?: string }
|
||||
|
||||
type Browser = {
|
||||
name: string
|
||||
/** The Chromium user data directory: `Local State` and one directory per profile. */
|
||||
userData: string
|
||||
/** Where earlier versions wrote the native messaging host manifest. */
|
||||
manifests: string[]
|
||||
/** How the OS names this browser as the default: bundle IDs, desktop files, or URL ProgIds. */
|
||||
defaultIDs: string[]
|
||||
/** macOS application name, for `/Applications/<app>.app` and `open -a`. */
|
||||
app?: string
|
||||
/** Linux: binary names on PATH. Windows: full executable paths. */
|
||||
executables: string[]
|
||||
}
|
||||
|
||||
function browsers(): Browser[] {
|
||||
if (process.platform === "darwin") {
|
||||
const support = (dir: string) => path.join(home, "Library/Application Support", dir)
|
||||
const browser = (name: string, app: string, bundleID: string, dirs: string[]): Browser => ({
|
||||
name,
|
||||
app,
|
||||
userData: support(dirs[0]!),
|
||||
manifests: dirs.map((dir) => path.join(support(dir), "NativeMessagingHosts")),
|
||||
defaultIDs: [bundleID],
|
||||
executables: [],
|
||||
})
|
||||
return [
|
||||
browser("Google Chrome", "Google Chrome", "com.google.Chrome", ["Google/Chrome"]),
|
||||
browser("Chrome for Testing", "Google Chrome for Testing", "com.google.chrome.for.testing", ["Google/Chrome for Testing", "Google/ChromeForTesting"]),
|
||||
browser("Google Chrome Beta", "Google Chrome Beta", "com.google.Chrome.beta", ["Google/Chrome Beta"]),
|
||||
browser("Google Chrome Canary", "Google Chrome Canary", "com.google.Chrome.canary", ["Google/Chrome Canary"]),
|
||||
browser("Chromium", "Chromium", "org.chromium.Chromium", ["Chromium"]),
|
||||
browser("Microsoft Edge", "Microsoft Edge", "com.microsoft.edgemac", ["Microsoft Edge"]),
|
||||
browser("Brave", "Brave Browser", "com.brave.Browser", ["BraveSoftware/Brave-Browser"]),
|
||||
browser("Opera", "Opera", "com.operasoftware.Opera", ["com.operasoftware.Opera"]),
|
||||
browser("Vivaldi", "Vivaldi", "com.vivaldi.Vivaldi", ["Vivaldi"]),
|
||||
browser("Helium", "Helium", "net.imput.helium", ["net.imput.helium"]),
|
||||
browser("Arc", "Arc", "company.thebrowser.Browser", ["Arc/User Data"]),
|
||||
browser("Dia", "Dia", "company.thebrowser.dia", ["Dia/User Data"]),
|
||||
browser("Comet", "Comet", "ai.perplexity.comet", ["Comet"]),
|
||||
]
|
||||
}
|
||||
if (windows) {
|
||||
const local = process.env.LOCALAPPDATA ?? path.join(home, "AppData", "Local")
|
||||
const roaming = process.env.APPDATA ?? path.join(home, "AppData", "Roaming")
|
||||
const programs = [process.env.ProgramFiles, process.env["ProgramFiles(x86)"], local].filter((dir): dir is string => !!dir)
|
||||
const installs = (relative: string) => programs.map((dir) => path.join(dir, relative))
|
||||
return [
|
||||
{ name: "Google Chrome", userData: path.join(local, "Google", "Chrome", "User Data"), manifests: [], defaultIDs: ["ChromeHTML"], executables: installs("Google\\Chrome\\Application\\chrome.exe") },
|
||||
{ name: "Microsoft Edge", userData: path.join(local, "Microsoft", "Edge", "User Data"), manifests: [], defaultIDs: ["MSEdgeHTM"], executables: installs("Microsoft\\Edge\\Application\\msedge.exe") },
|
||||
{ name: "Brave", userData: path.join(local, "BraveSoftware", "Brave-Browser", "User Data"), manifests: [], defaultIDs: ["BraveHTML"], executables: installs("BraveSoftware\\Brave-Browser\\Application\\brave.exe") },
|
||||
{ name: "Opera", userData: path.join(roaming, "Opera Software", "Opera Stable"), manifests: [], defaultIDs: ["OperaStable"], executables: installs("Programs\\Opera\\opera.exe") },
|
||||
{ name: "Vivaldi", userData: path.join(local, "Vivaldi", "User Data"), manifests: [], defaultIDs: ["VivaldiHTM"], executables: installs("Vivaldi\\Application\\vivaldi.exe") },
|
||||
]
|
||||
}
|
||||
// Chrome-family builds honor CHROME_CONFIG_HOME before XDG_CONFIG_HOME.
|
||||
const chrome = process.env.CHROME_CONFIG_HOME ?? configRoot
|
||||
const browser = (name: string, root: string, dir: string, executables: string[]): Browser => ({
|
||||
name,
|
||||
userData: path.join(root, dir),
|
||||
manifests: [path.join(root, dir, "NativeMessagingHosts")],
|
||||
defaultIDs: executables,
|
||||
executables,
|
||||
})
|
||||
return [
|
||||
browser("Google Chrome", chrome, "google-chrome", ["google-chrome", "google-chrome-stable"]),
|
||||
browser("Google Chrome Beta", chrome, "google-chrome-beta", ["google-chrome-beta"]),
|
||||
browser("Google Chrome Unstable", chrome, "google-chrome-unstable", ["google-chrome-unstable"]),
|
||||
browser("Chrome for Testing", chrome, "google-chrome-for-testing", ["google-chrome-for-testing"]),
|
||||
browser("Chromium", chrome, "chromium", ["chromium", "chromium-browser"]),
|
||||
browser("Microsoft Edge", configRoot, "microsoft-edge", ["microsoft-edge", "microsoft-edge-stable"]),
|
||||
browser("Brave", configRoot, "BraveSoftware/Brave-Browser", ["brave-browser", "brave"]),
|
||||
browser("Opera", configRoot, "opera", ["opera"]),
|
||||
browser("Vivaldi", configRoot, "vivaldi", ["vivaldi", "vivaldi-stable"]),
|
||||
browser("Helium", configRoot, "net.imput.helium", ["helium", "helium-browser"]),
|
||||
]
|
||||
}
|
||||
|
||||
/** The opencode binary (recorded for status): --opencode, $OPENCODE_BIN, or `opencode` on PATH. */
|
||||
function findOpencode(args: string[]) {
|
||||
const explicit = flag(args, "--opencode") ?? process.env.OPENCODE_BIN
|
||||
if (explicit) return path.resolve(explicit)
|
||||
return findOnPath("opencode")
|
||||
}
|
||||
|
||||
function flag(args: string[], name: string) {
|
||||
const index = args.indexOf(name)
|
||||
return index === -1 ? undefined : args[index + 1]
|
||||
}
|
||||
|
||||
function readJson<T>(file: string): T | undefined {
|
||||
try {
|
||||
return JSON.parse(readFileSync(file, "utf8")) as T
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
// ---- opencode config: the browse MCP server ---------------------------------------------------------
|
||||
|
||||
function configFile() {
|
||||
const directory = path.join(configRoot, "opencode")
|
||||
const candidates = ["opencode.json", "opencode.jsonc", ".opencode/opencode.json", ".opencode/opencode.jsonc"].map(
|
||||
(name) => path.join(directory, name),
|
||||
)
|
||||
return candidates.find((file) => existsSync(file) && statSync(file).isFile()) ?? candidates[0]!
|
||||
}
|
||||
|
||||
type Servers = Record<string, { type?: unknown; command?: unknown; environment?: unknown }>
|
||||
|
||||
/** Changes whenever the installed runtime does. */
|
||||
function fingerprint(entry: string) {
|
||||
return createHash("sha256").update(readFileOr(entry, entry)).digest("hex").slice(0, 16)
|
||||
}
|
||||
|
||||
/**
|
||||
* After an update, a relay still running the old build refuses the new CLI and MCP server; restart it from
|
||||
* the new runtime. Restarting keeps browser tabs open but ends in-flight agent work, so it's announced.
|
||||
*/
|
||||
function restartOutdatedRelay(entry: string, node: string) {
|
||||
const status = spawnSync(node, [entry, "status", "--json"], { encoding: "utf8", timeout: 15_000 })
|
||||
if (!/"stale":\s*true/.test(status.stdout ?? "")) return undefined
|
||||
const restart = spawnSync(node, [entry, "relay", "restart"], { encoding: "utf8", timeout: 60_000 })
|
||||
return restart.status === 0 ? "restarted with the new version" : `could not restart: ${(restart.stderr || restart.stdout).trim()}`
|
||||
}
|
||||
|
||||
function mcpServers() {
|
||||
return ((parse(readFileOr(configFile(), "{}")) ?? {}) as { mcp?: { servers?: Servers } }).mcp?.servers ?? {}
|
||||
}
|
||||
|
||||
/** Writes (or rewrites) the browse MCP server so it runs the installed runtime, replacing the old entry. */
|
||||
function configureMcp(entry: string) {
|
||||
const file = configFile()
|
||||
// A running MCP server keeps its old code after an update, and its relay calls then fail on the build check.
|
||||
// Stamping the runtime's fingerprint into the entry changes the config whenever the runtime changes, which
|
||||
// makes opencode restart the server with the new code.
|
||||
const server = { type: "local", command: [process.execPath, entry, "mcp"], environment: { OPENCODE_BROWSER_RUNTIME: fingerprint(entry) } }
|
||||
const renamed = removeMcp(LEGACY_MCP_NAME)
|
||||
const existing = mcpServers()[MCP_NAME]
|
||||
if (existing && JSON.stringify(existing) === JSON.stringify(server)) return `"${MCP_NAME}" is up to date`
|
||||
const text = readFileOr(file, "{}")
|
||||
const edits = modify(text, ["mcp", "servers", MCP_NAME], server, { formattingOptions: { tabSize: 2, insertSpaces: true } })
|
||||
mkdirSync(path.dirname(file), { recursive: true })
|
||||
writeFileSync(file, applyEdits(text, edits))
|
||||
if (renamed) return `"${LEGACY_MCP_NAME}" renamed to "${MCP_NAME}" in ${file}`
|
||||
return existing ? `"${MCP_NAME}" updated in ${file}` : `"${MCP_NAME}" added to ${file}`
|
||||
}
|
||||
|
||||
function removeMcp(name: string) {
|
||||
const file = configFile()
|
||||
const text = readFileOr(file, "")
|
||||
if (!text || !mcpServers()[name]) return false
|
||||
writeFileSync(file, applyEdits(text, modify(text, ["mcp", "servers", name], undefined, {})))
|
||||
return true
|
||||
}
|
||||
|
||||
/** MCP servers from the separate Browser Control product, which OpenCode Browser replaces. */
|
||||
function legacyBrowserControl() {
|
||||
return Object.entries(mcpServers())
|
||||
.filter(([name, server]) => name === "browser-control" || JSON.stringify(server.command ?? "").includes("browser-control"))
|
||||
.map(([name]) => name)
|
||||
}
|
||||
|
||||
function readFileOr(file: string, fallback: string) {
|
||||
try {
|
||||
return readFileSync(file, "utf8")
|
||||
} catch {
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes the native messaging host and opencode plugin of earlier versions; true when any was there. */
|
||||
function removeLegacyHost() {
|
||||
const leftovers = [
|
||||
...browsers().flatMap((browser) => browser.manifests.map((directory) => path.join(directory, `${LEGACY_HOST_NAME}.json`))),
|
||||
...legacyFiles,
|
||||
].filter((file) => existsSync(file))
|
||||
leftovers.forEach((file) => rmSync(file, { force: true }))
|
||||
const keys = windows
|
||||
? LEGACY_REGISTRY_ROOTS.map((root) => `${root}\\NativeMessagingHosts\\${LEGACY_HOST_NAME}`).filter(
|
||||
(key) => spawnSync("reg", ["query", key, "/ve"]).status === 0,
|
||||
)
|
||||
: []
|
||||
keys.forEach((key) => spawnSync("reg", ["delete", key, "/f"]))
|
||||
return leftovers.length + keys.length > 0
|
||||
}
|
||||
|
||||
// ---- runtime ----------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Installs this exact version into the data root, so the MCP server keeps working after the npx cache is
|
||||
* cleared, and an update is one `install` away. From source (development), runs in place.
|
||||
* OPENCODE_BROWSER_PACKAGE installs a specific spec instead, for example a local tarball.
|
||||
*/
|
||||
function installRuntime() {
|
||||
if (fromSource) return path.join(packageRoot, "src", "main.ts")
|
||||
const spec = process.env.OPENCODE_BROWSER_PACKAGE ?? `${PACKAGE}@${opencodeBrowserVersion}`
|
||||
mkdirSync(files.runtime, { recursive: true })
|
||||
if (!existsSync(path.join(files.runtime, "package.json"))) writeFileSync(path.join(files.runtime, "package.json"), "{\"private\":true}\n")
|
||||
const npm = spawnSync(windows ? "npm.cmd" : "npm", ["install", "--prefix", files.runtime, "--no-audit", "--no-fund", "--loglevel=error", spec], {
|
||||
encoding: "utf8",
|
||||
shell: windows,
|
||||
})
|
||||
if (npm.status !== 0) fail(`Could not install ${spec}: ${(npm.stderr || npm.stdout || "npm failed").trim()}`)
|
||||
return path.join(files.runtime, "node_modules", PACKAGE, "dist", "cli.mjs")
|
||||
}
|
||||
|
||||
// ---- commands -------------------------------------------------------------------------------------
|
||||
|
||||
async function install(args: string[]) {
|
||||
const opencode = findOpencode(args)
|
||||
|
||||
mkdirSync(files.dir, { recursive: true })
|
||||
const entry = installRuntime()
|
||||
log(`✓ OpenCode Browser ${opencodeBrowserVersion} installed${fromSource ? " (from source)" : ""}`)
|
||||
writeFileSync(files.settings, JSON.stringify({ ...(opencode ? { opencode } : {}), entry, version: opencodeBrowserVersion } satisfies Settings, null, 2) + "\n")
|
||||
if (removeLegacyHost()) log("✓ Removed the native messaging host of an earlier version")
|
||||
|
||||
const node = fromSource ? (findOnPath("bun") ?? process.execPath) : process.execPath
|
||||
log(opencode ? `✓ Using opencode at ${opencode}` : "! opencode not found. Install it (https://opencode.ai) to use the browse tools; they're configured for it.")
|
||||
log(`✓ MCP server ${configureMcp(entry)}`)
|
||||
const relay = restartOutdatedRelay(entry, node)
|
||||
if (relay) log(`${relay.startsWith("restarted") ? "✓" : "!"} Relay ${relay}`)
|
||||
const legacy = legacyBrowserControl()
|
||||
if (legacy.length)
|
||||
log(`! Browser Control MCP server${legacy.length === 1 ? "" : "s"} ${legacy.map((name) => `"${name}"`).join(", ")} also configured. OpenCode Browser replaces it; remove ${legacy.length === 1 ? "it" : "them"} from ${configFile()} to avoid two sets of browser tools.`)
|
||||
|
||||
const connected = await connectedExtension()
|
||||
if (existsSync(bundledExtension)) {
|
||||
rmSync(files.extension, { recursive: true, force: true })
|
||||
cpSync(bundledExtension, files.extension, { recursive: true })
|
||||
// A browser already running an older unpacked build keeps its old code until it reloads; ask it to.
|
||||
const reload = connected ? await reloadOutdatedExtension(connected) : "current"
|
||||
if (reload === "reloaded") log("✓ Reloaded OpenCode Browser in your browser")
|
||||
if (reload === "stale")
|
||||
log("! OpenCode Browser didn't reload in your browser. Reload it on the browser's extensions page to finish the update.")
|
||||
}
|
||||
log("")
|
||||
locateExtension({ allowOpen: !args.includes("--no-open"), connected: !!connected })
|
||||
}
|
||||
|
||||
/** Printed before the relay's status by `opencode-browser status`. */
|
||||
export function printSetupStatus() {
|
||||
const settings = readJson<Settings>(files.settings)
|
||||
log(settings?.version ? `Setup: installed` : "Setup: not installed. Run `npx opencode-browser-cli install`.")
|
||||
log(`Version: ${settings?.version ?? "not installed"}${settings?.entry ? ` (${settings.entry})` : ""}`)
|
||||
log(`opencode: ${settings?.opencode ?? findOpencode([]) ?? "not found"}`)
|
||||
log(mcpServers()[MCP_NAME] ? `MCP: "${MCP_NAME}" configured` : "MCP: not configured")
|
||||
log(`Logs: ${path.join(files.dir, "logs")}`)
|
||||
log("")
|
||||
}
|
||||
|
||||
function extension() {
|
||||
if (!existsSync(files.extension)) {
|
||||
if (!existsSync(bundledExtension)) fail("This package has no bundled extension.")
|
||||
mkdirSync(files.dir, { recursive: true })
|
||||
cpSync(bundledExtension, files.extension, { recursive: true })
|
||||
}
|
||||
const opener = windows ? "explorer" : process.platform === "darwin" ? "open" : "xdg-open"
|
||||
spawnSync(opener, [files.extension])
|
||||
log(`Load unpacked from: ${files.extension}`)
|
||||
}
|
||||
|
||||
function uninstall() {
|
||||
const host = removeLegacyHost()
|
||||
const mcp = [MCP_NAME, LEGACY_MCP_NAME].filter((name) => removeMcp(name))
|
||||
rmSync(files.dir, { recursive: true, force: true })
|
||||
log(`✓ Removed ${files.dir}`)
|
||||
if (host) log("✓ Removed the native messaging host of an earlier version")
|
||||
if (mcp.length) log(`✓ Removed the ${mcp.map((name) => `"${name}"`).join(" and ")} MCP server${mcp.length === 1 ? "" : "s"}`)
|
||||
log("Remove the extension from your browser to finish.")
|
||||
}
|
||||
|
||||
type ExtensionStatus = { connected?: boolean; build?: string | null; origin?: string | null }
|
||||
|
||||
const relayBase = () => `http://127.0.0.1:${Number(process.env.OPENCODE_BROWSER_PORT) || 19988}`
|
||||
|
||||
const extensionStatus = () =>
|
||||
fetch(`${relayBase()}/extension/status`, { signal: AbortSignal.timeout(3_000) })
|
||||
.then((response) => (response.ok ? (response.json() as Promise<ExtensionStatus>) : undefined))
|
||||
.catch(() => undefined)
|
||||
|
||||
/** The extension connected to the relay, if any; a relay restarted moments ago waits a second or two for it. */
|
||||
async function connectedExtension() {
|
||||
let status = await extensionStatus()
|
||||
for (let attempt = 0; status?.connected === false && attempt < 10; attempt++) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500))
|
||||
status = await extensionStatus()
|
||||
}
|
||||
return status?.connected ? status : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the connected extension when it's the unpacked build (the store updates its own) and older than the files
|
||||
* just copied, then checks the reload took effect: the build it reports after reconnecting must be the new one.
|
||||
*/
|
||||
async function reloadOutdatedExtension(before: ExtensionStatus): Promise<"reloaded" | "stale" | "current"> {
|
||||
if (before.origin !== `chrome-extension://${UNPACKED_ID}`) return "current"
|
||||
const bundled = readJson<{ build?: string }>(path.join(files.extension, "build.json"))?.build
|
||||
if (bundled && before.build === bundled) return "current"
|
||||
const sent = await fetch(`${relayBase()}/extension/reload`, {
|
||||
method: "POST",
|
||||
body: "{}",
|
||||
headers: { "content-type": "application/json" },
|
||||
signal: AbortSignal.timeout(5_000),
|
||||
}).then(
|
||||
(response) => response.ok,
|
||||
() => false,
|
||||
)
|
||||
if (!sent) return "stale"
|
||||
for (let attempt = 0; attempt < 20; attempt++) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500))
|
||||
const after = await extensionStatus()
|
||||
if (after?.connected && after.build !== before.build) return "reloaded"
|
||||
}
|
||||
return "stale"
|
||||
}
|
||||
|
||||
function findOnPath(name: string) {
|
||||
const found = spawnSync(windows ? "where" : "which", [name], { encoding: "utf8" })
|
||||
return found.status === 0 ? found.stdout.split(/\r?\n/)[0]?.trim() || undefined : undefined
|
||||
}
|
||||
|
||||
// ---- which browsers have the extension -------------------------------------------------------------
|
||||
|
||||
type Profile = { dir: string; name: string }
|
||||
type Match = { browser: Browser; profile: Profile; id: string }
|
||||
/** blocked: the OS refused to read the browser's data (macOS privacy protection), so it may have the extension. */
|
||||
type Detection = { browser: Browser; blocked: boolean; enabled: Match[]; disabled: Match[] }
|
||||
|
||||
/**
|
||||
* Reports the browsers and profiles that have OpenCode Browser. When none does (and none is connected to the relay),
|
||||
* opens its Chrome Web Store listing in the default browser, unless there is no browser to open here (SSH, no
|
||||
* display, no terminal, --no-open) or a browser couldn't be checked and may already have it.
|
||||
*/
|
||||
function locateExtension(options: { allowOpen: boolean; connected: boolean }) {
|
||||
const ready = "✓ Agents can use OpenCode Browser now. Click its toolbar button on a tab to let them use that tab too."
|
||||
// Over SSH the browsers here aren't the ones the user looks at.
|
||||
if (process.env.SSH_CONNECTION || process.env.SSH_TTY)
|
||||
return log(options.connected ? ready : `Install OpenCode Browser in your browser from the Chrome Web Store: ${STORE_URL}`)
|
||||
const detections = detectBrowsers()
|
||||
const preferred = defaultBrowser(detections.map((detection) => detection.browser))
|
||||
reportDetections(detections, preferred)
|
||||
// A connected extension is installed, even somewhere the scan doesn't look (a custom profile directory, a snap).
|
||||
if (options.connected || detections.some((detection) => detection.enabled.length))
|
||||
return log(ready)
|
||||
const off = detections.flatMap((detection) => detection.disabled)
|
||||
if (off.length)
|
||||
return log(`! OpenCode Browser is turned off in ${off.map((match) => `${match.browser.name} (${match.profile.name})`).join(", ")}. Turn it on in the browser's extensions page.`)
|
||||
const blocked = detections.filter((detection) => detection.blocked)
|
||||
const display = process.platform !== "linux" || !!(process.env.DISPLAY || process.env.WAYLAND_DISPLAY)
|
||||
const target = preferred ?? detections.find((detection) => !detection.blocked)?.browser
|
||||
const opened = options.allowOpen && display && !!process.stdout.isTTY && !blocked.length && target !== undefined
|
||||
if (opened) open(target, STORE_URL)
|
||||
log(`Install OpenCode Browser from the Chrome Web Store${opened ? ` (opened in ${target.name})` : ""}:`)
|
||||
log(` ${STORE_URL}`)
|
||||
if (blocked.length) log(` Already installed in ${blocked.map((detection) => detection.browser.name).join(", ")}? Then you're set.`)
|
||||
log(` Or load the unpacked build: npx ${PACKAGE} extension`)
|
||||
}
|
||||
|
||||
/** Every browser with user data here, and the profiles that have OpenCode Browser. */
|
||||
function detectBrowsers() {
|
||||
return browsers().flatMap((browser): Detection[] => {
|
||||
const state = readLocked(path.join(browser.userData, "Local State"))
|
||||
if (state.missing) return []
|
||||
// Leftover data of an uninstalled browser: nothing to open it with.
|
||||
if (state.blocked) return launcher(browser) ? [{ browser, blocked: true, enabled: [], disabled: [] }] : []
|
||||
const matches = listProfiles(browser, state.text).flatMap((profile) => {
|
||||
const settings = ["Secure Preferences", "Preferences"].map(
|
||||
(file) =>
|
||||
(parseJson(readLocked(path.join(browser.userData, profile.dir, file)).text) as { extensions?: { settings?: Record<string, ExtensionSetting> } } | undefined)
|
||||
?.extensions?.settings ?? {},
|
||||
)
|
||||
const id = [STORE_ID, UNPACKED_ID].find((candidate) => settings.some((entries) => entries[candidate]))
|
||||
if (!id) return []
|
||||
const setting = settings.find((entries) => entries[id])![id]!
|
||||
return [{ browser, profile, id, enabled: !disabled(setting) }]
|
||||
})
|
||||
return [
|
||||
{
|
||||
browser,
|
||||
blocked: false,
|
||||
enabled: matches.filter((match) => match.enabled),
|
||||
disabled: matches.filter((match) => !match.enabled),
|
||||
},
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
type ExtensionSetting = { disable_reasons?: unknown }
|
||||
|
||||
function disabled(setting: ExtensionSetting) {
|
||||
const reasons = setting.disable_reasons
|
||||
if (Array.isArray(reasons)) return reasons.length > 0
|
||||
return typeof reasons === "number" && reasons !== 0
|
||||
}
|
||||
|
||||
/** Profiles from `Local State` (with their display names), else the profile directories themselves. */
|
||||
function listProfiles(browser: Browser, localState: string | undefined): Profile[] {
|
||||
const cache = (parseJson(localState) as { profile?: { info_cache?: Record<string, { name?: string }> } } | undefined)?.profile?.info_cache
|
||||
if (cache && Object.keys(cache).length)
|
||||
return Object.entries(cache).map(([dir, info]) => ({ dir, name: info.name || dir }))
|
||||
const dirs = (() => {
|
||||
try {
|
||||
return readdirSync(browser.userData)
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
})()
|
||||
return dirs.filter((dir) => /^(Default|Profile \d+)$/.test(dir)).map((dir) => ({ dir, name: dir }))
|
||||
}
|
||||
|
||||
function readLocked(file: string) {
|
||||
try {
|
||||
return { text: readFileSync(file, "utf8"), missing: false, blocked: false }
|
||||
} catch (error) {
|
||||
const code = (error as NodeJS.ErrnoException).code
|
||||
return { text: undefined, missing: code === "ENOENT" || code === "ENOTDIR", blocked: code === "EPERM" || code === "EACCES" }
|
||||
}
|
||||
}
|
||||
|
||||
function parseJson(text: string | undefined): unknown {
|
||||
if (!text) return undefined
|
||||
try {
|
||||
return JSON.parse(text)
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
/** The OS default web browser among the known ones; undefined when it can't be told or isn't Chromium. */
|
||||
function defaultBrowser(candidates: Browser[]) {
|
||||
const id = defaultBrowserID()
|
||||
if (!id) return undefined
|
||||
const known = browsers().find((browser) =>
|
||||
browser.defaultIDs.some((name) => name.toLowerCase() === id.toLowerCase() || id.startsWith(`${name}.`)),
|
||||
)
|
||||
return known && (candidates.find((browser) => browser.name === known.name) ?? known)
|
||||
}
|
||||
|
||||
function defaultBrowserID() {
|
||||
if (process.platform === "darwin") {
|
||||
const plist = path.join(home, "Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist")
|
||||
const json = spawnSync("plutil", ["-convert", "json", "-o", "-", plist], { encoding: "utf8" })
|
||||
const handlers =
|
||||
(parseJson(json.stdout) as { LSHandlers?: { LSHandlerURLScheme?: string; LSHandlerRoleAll?: string }[] } | undefined)?.LSHandlers ?? []
|
||||
return ["https", "http"]
|
||||
.map((scheme) => handlers.find((handler) => handler.LSHandlerURLScheme === scheme)?.LSHandlerRoleAll)
|
||||
.find((bundle) => !!bundle)
|
||||
}
|
||||
if (windows) {
|
||||
const key = "HKCU\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\https\\UserChoice"
|
||||
const query = spawnSync("reg", ["query", key, "/v", "ProgId"], { encoding: "utf8" })
|
||||
return /ProgId\s+REG_SZ\s+(\S+)/.exec(query.stdout ?? "")?.[1]
|
||||
}
|
||||
const xdg = spawnSync("xdg-settings", ["get", "default-web-browser"], { encoding: "utf8" })
|
||||
return xdg.status === 0 ? xdg.stdout.trim().replace(/\.desktop$/, "") || undefined : undefined
|
||||
}
|
||||
|
||||
function reportDetections(detections: Detection[], preferred: Browser | undefined) {
|
||||
log(`Default browser: ${preferred?.name ?? "unknown"}`)
|
||||
detections.forEach((detection) => {
|
||||
const name = detection.browser.name
|
||||
if (detection.blocked) return log(` ${name}: couldn't check (${blockedReason()})`)
|
||||
if (detection.enabled.length) return log(` ${name}: OpenCode Browser in ${detection.enabled.map(describe).join(", ")}`)
|
||||
if (detection.disabled.length) return log(` ${name}: OpenCode Browser turned off in ${detection.disabled.map(describe).join(", ")}`)
|
||||
log(` ${name}: no OpenCode Browser`)
|
||||
})
|
||||
}
|
||||
|
||||
const describe = (match: Match) => `${match.profile.name}${match.id === UNPACKED_ID ? " (unpacked)" : ""}`
|
||||
const blockedReason = () => (process.platform === "darwin" ? "macOS privacy protection" : "permission denied")
|
||||
|
||||
/** Opens a URL in a specific browser; launching its executable forwards the URL to a running instance. */
|
||||
function open(browser: Browser, url: string) {
|
||||
const executable = launcher(browser)?.executable
|
||||
if (executable) {
|
||||
const child = spawn(executable, [url], { detached: true, stdio: "ignore" })
|
||||
child.on("error", () => openWithSystem(browser, url))
|
||||
child.unref()
|
||||
return
|
||||
}
|
||||
openWithSystem(browser, url)
|
||||
}
|
||||
|
||||
function openWithSystem(browser: Browser, url: string) {
|
||||
if (process.platform === "darwin") return void spawnSync("open", browser.app ? ["-a", browser.app, url] : [url])
|
||||
// rundll32 hands the URL to its handler without cmd parsing the & in it.
|
||||
if (windows) return void spawnSync("rundll32", ["url.dll,FileProtocolHandler", url])
|
||||
spawnSync("xdg-open", [url])
|
||||
}
|
||||
|
||||
/** The browser's executable, or (macOS) just its app bundle when the executable can't be read. */
|
||||
function launcher(browser: Browser): { executable?: string } | undefined {
|
||||
if (process.platform === "darwin") {
|
||||
const bundle = [path.join("/Applications", `${browser.app}.app`), path.join(home, "Applications", `${browser.app}.app`)].find((dir) => existsSync(dir))
|
||||
if (!bundle) return undefined
|
||||
const name = spawnSync("plutil", ["-extract", "CFBundleExecutable", "raw", "-o", "-", path.join(bundle, "Contents", "Info.plist")], { encoding: "utf8" }).stdout?.trim()
|
||||
const executable = name ? path.join(bundle, "Contents", "MacOS", name) : undefined
|
||||
return executable && existsSync(executable) ? { executable } : {}
|
||||
}
|
||||
const executable = windows ? browser.executables.find((file) => existsSync(file)) : browser.executables.map(findOnPath).find((file) => !!file)
|
||||
return executable ? { executable } : undefined
|
||||
}
|
||||
|
||||
function log(line: string) {
|
||||
process.stdout.write(line + "\n")
|
||||
}
|
||||
|
||||
function fail(message: string): never {
|
||||
process.stderr.write(message + "\n")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
export async function runSetup(command: string, args: string[]) {
|
||||
if (command === "install") return install(args)
|
||||
if (command === "extension") return extension()
|
||||
if (command === "uninstall") return uninstall()
|
||||
}
|
||||
1717
packages/browser-extension/cli/src/snapshot.ts
Normal file
1717
packages/browser-extension/cli/src/snapshot.ts
Normal file
File diff suppressed because it is too large
Load diff
489
packages/browser-extension/cli/src/target-registry.ts
Normal file
489
packages/browser-extension/cli/src/target-registry.ts
Normal file
|
|
@ -0,0 +1,489 @@
|
|||
import { Schema } from "effect"
|
||||
import { isJsonObject, type JsonObject, type TargetInfo } from "./protocol.ts"
|
||||
import type { ChildTarget, ConnectedTarget, StoredFrameEvents } from "./relay-types.ts"
|
||||
|
||||
export type TargetOwnershipReservation = {
|
||||
readonly targetId: string
|
||||
readonly targetSessionId: string
|
||||
readonly tabId: number
|
||||
readonly sessionId: string
|
||||
readonly previousOpenCodeBrowserSessionId?: string
|
||||
}
|
||||
|
||||
export type TargetOwnershipChange = {
|
||||
readonly targetIds: readonly string[]
|
||||
readonly tabIds: readonly number[]
|
||||
}
|
||||
|
||||
export interface TargetOwnership {
|
||||
reserveTargetOwnership(targetId: string, sessionId: string): TargetOwnershipReservation
|
||||
rollbackTargetOwnership(reservation: TargetOwnershipReservation): TargetOwnershipChange
|
||||
commitTargetOwnership(options: {
|
||||
readonly reservation: TargetOwnershipReservation
|
||||
readonly previousAdoptedTargetId?: string
|
||||
}): TargetOwnershipChange
|
||||
releaseTargetOwnership(targetId: string, sessionId: string): TargetOwnershipChange
|
||||
}
|
||||
|
||||
export class TargetOwnershipError extends Schema.TaggedError<TargetOwnershipError>()(
|
||||
"TargetRegistry.TargetOwnershipError",
|
||||
{
|
||||
message: Schema.String,
|
||||
reason: Schema.Literals(["not-found", "owned", "generation-changed"]),
|
||||
ownerSessionId: Schema.optionalKey(Schema.String),
|
||||
},
|
||||
) {}
|
||||
|
||||
export class MemoryTargetOwnership implements TargetOwnership {
|
||||
private readonly owners = new Map<string, string>()
|
||||
|
||||
reserveTargetOwnership(targetId: string, sessionId: string): TargetOwnershipReservation {
|
||||
const owner = this.owners.get(targetId)
|
||||
if (owner && owner !== sessionId) {
|
||||
throw targetOwnedError(owner)
|
||||
}
|
||||
this.owners.set(targetId, sessionId)
|
||||
return {
|
||||
targetId,
|
||||
targetSessionId: targetId,
|
||||
tabId: -1,
|
||||
sessionId,
|
||||
...(owner ? { previousOpenCodeBrowserSessionId: owner } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
rollbackTargetOwnership(reservation: TargetOwnershipReservation): TargetOwnershipChange {
|
||||
if (this.owners.get(reservation.targetId) !== reservation.sessionId) {
|
||||
return emptyOwnershipChange
|
||||
}
|
||||
if (reservation.previousOpenCodeBrowserSessionId) {
|
||||
this.owners.set(reservation.targetId, reservation.previousOpenCodeBrowserSessionId)
|
||||
} else {
|
||||
this.owners.delete(reservation.targetId)
|
||||
}
|
||||
return ownershipChange(reservation.targetId, reservation.tabId)
|
||||
}
|
||||
|
||||
commitTargetOwnership(options: {
|
||||
readonly reservation: TargetOwnershipReservation
|
||||
readonly previousAdoptedTargetId?: string
|
||||
}): TargetOwnershipChange {
|
||||
const changed = [options.reservation.targetId]
|
||||
if (options.previousAdoptedTargetId && options.previousAdoptedTargetId !== options.reservation.targetId) {
|
||||
if (this.owners.get(options.previousAdoptedTargetId) === options.reservation.sessionId) {
|
||||
this.owners.delete(options.previousAdoptedTargetId)
|
||||
changed.push(options.previousAdoptedTargetId)
|
||||
}
|
||||
}
|
||||
return { targetIds: changed, tabIds: [] }
|
||||
}
|
||||
|
||||
releaseTargetOwnership(targetId: string, sessionId: string): TargetOwnershipChange {
|
||||
if (this.owners.get(targetId) !== sessionId) {
|
||||
return emptyOwnershipChange
|
||||
}
|
||||
this.owners.delete(targetId)
|
||||
return { targetIds: [targetId], tabIds: [] }
|
||||
}
|
||||
}
|
||||
|
||||
export type ConnectedTargetInfoUpdate =
|
||||
| { readonly kind: "root"; readonly target: ConnectedTarget }
|
||||
| { readonly kind: "child"; readonly target: ChildTarget }
|
||||
|
||||
export type RootTargetChange =
|
||||
| { readonly kind: "added" | "updated"; readonly target: ConnectedTarget }
|
||||
| {
|
||||
readonly kind: "replaced"
|
||||
readonly target: ConnectedTarget
|
||||
readonly previous: ConnectedTarget
|
||||
readonly childSessionIds: readonly string[]
|
||||
}
|
||||
|
||||
export function shouldExposeChildTarget(target: ChildTarget): boolean {
|
||||
return target.targetInfo.type !== "page" || target.targetInfo.url !== ""
|
||||
}
|
||||
|
||||
export class TargetRegistry {
|
||||
readonly targets = new Map<string, ConnectedTarget>()
|
||||
readonly tabTargets = new Map<number, ConnectedTarget>()
|
||||
readonly targetsByTargetId = new Map<string, ConnectedTarget>()
|
||||
readonly childTargets = new Map<string, ChildTarget>()
|
||||
readonly childTargetsByTargetId = new Map<string, ChildTarget>()
|
||||
readonly tabFrameEvents = new Map<number, Map<string, StoredFrameEvents>>()
|
||||
private readonly pendingOwnershipReservations = new Map<string, TargetOwnershipReservation>()
|
||||
private readonly stagedRootTargets = new Map<number, ConnectedTarget>()
|
||||
|
||||
clear(): void {
|
||||
this.targets.clear()
|
||||
this.tabTargets.clear()
|
||||
this.targetsByTargetId.clear()
|
||||
this.childTargets.clear()
|
||||
this.childTargetsByTargetId.clear()
|
||||
this.tabFrameEvents.clear()
|
||||
this.pendingOwnershipReservations.clear()
|
||||
this.stagedRootTargets.clear()
|
||||
}
|
||||
|
||||
addRootTarget(target: ConnectedTarget, options: {
|
||||
readonly preserveChildParentSessionId?: string
|
||||
readonly preserveFrameEvents?: boolean
|
||||
} = {}): RootTargetChange {
|
||||
const existingForTab = this.tabTargets.get(target.tabId)
|
||||
const generationChanged = existingForTab !== undefined && (
|
||||
existingForTab.sessionId !== target.sessionId ||
|
||||
existingForTab.targetInfo.targetId !== target.targetInfo.targetId
|
||||
)
|
||||
const nextTarget = generationChanged ? this.inheritRootOwnership(target, existingForTab) : target
|
||||
const detached = generationChanged
|
||||
? this.detachRootTargetState(target.tabId, options)
|
||||
: undefined
|
||||
const childSessionIds = detached?.childSessionIds ?? []
|
||||
if (existingForTab && !generationChanged) {
|
||||
this.targets.delete(existingForTab.sessionId)
|
||||
this.targetsByTargetId.delete(existingForTab.targetInfo.targetId)
|
||||
}
|
||||
const existingForTargetId = this.targetsByTargetId.get(nextTarget.targetInfo.targetId)
|
||||
if (existingForTargetId) {
|
||||
this.targets.delete(existingForTargetId.sessionId)
|
||||
this.tabTargets.delete(existingForTargetId.tabId)
|
||||
}
|
||||
this.targets.set(nextTarget.sessionId, nextTarget)
|
||||
this.tabTargets.set(nextTarget.tabId, nextTarget)
|
||||
this.targetsByTargetId.set(nextTarget.targetInfo.targetId, nextTarget)
|
||||
return generationChanged
|
||||
? { kind: "replaced", target: nextTarget, previous: existingForTab, childSessionIds }
|
||||
: { kind: existingForTab ? "updated" : "added", target: nextTarget }
|
||||
}
|
||||
|
||||
stageRootTarget(target: ConnectedTarget): ConnectedTarget {
|
||||
const existing = this.tabTargets.get(target.tabId)
|
||||
const staged = existing ? this.inheritRootOwnership(target, existing) : target
|
||||
this.stagedRootTargets.set(target.tabId, staged)
|
||||
this.tabFrameEvents.delete(target.tabId)
|
||||
return staged
|
||||
}
|
||||
|
||||
private inheritRootOwnership(target: ConnectedTarget, existing: ConnectedTarget): ConnectedTarget {
|
||||
const pendingReservation = this.pendingOwnershipReservations.get(existing.targetInfo.targetId)
|
||||
const inheritedOpenCodeBrowserSessionId = pendingReservation
|
||||
? pendingReservation.previousOpenCodeBrowserSessionId
|
||||
: existing.opencodeBrowserSessionId
|
||||
const { opencodeBrowserSessionId: _incomingOwner, ...targetWithoutOwner } = target
|
||||
return {
|
||||
...targetWithoutOwner,
|
||||
owner: existing.owner,
|
||||
...(inheritedOpenCodeBrowserSessionId ? { opencodeBrowserSessionId: inheritedOpenCodeBrowserSessionId } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
stagedRootTarget(tabId: number): ConnectedTarget | undefined {
|
||||
return this.stagedRootTargets.get(tabId)
|
||||
}
|
||||
|
||||
routingRootTarget(tabId: number): ConnectedTarget | undefined {
|
||||
return this.stagedRootTargets.get(tabId) ?? this.tabTargets.get(tabId)
|
||||
}
|
||||
|
||||
commitStagedRootTarget(tabId: number, sessionId: string): RootTargetChange | undefined {
|
||||
const staged = this.stagedRootTargets.get(tabId)
|
||||
if (!staged || staged.sessionId !== sessionId) return undefined
|
||||
this.stagedRootTargets.delete(tabId)
|
||||
return this.addRootTarget(staged, {
|
||||
preserveChildParentSessionId: staged.sessionId,
|
||||
preserveFrameEvents: true,
|
||||
})
|
||||
}
|
||||
|
||||
addChildTarget(target: ChildTarget): readonly ChildTarget[] {
|
||||
const replaced: ChildTarget[] = []
|
||||
const existingForSession = this.childTargets.get(target.sessionId)
|
||||
if (existingForSession && existingForSession.targetInfo.targetId !== target.targetInfo.targetId) {
|
||||
replaced.push(existingForSession)
|
||||
this.detachChildTargetState(existingForSession.sessionId)
|
||||
}
|
||||
const existingForTargetId = this.childTargetsByTargetId.get(target.targetInfo.targetId)
|
||||
if (existingForTargetId && existingForTargetId.sessionId !== target.sessionId) {
|
||||
replaced.push(existingForTargetId)
|
||||
this.detachChildTargetState(existingForTargetId.sessionId)
|
||||
}
|
||||
this.childTargets.set(target.sessionId, target)
|
||||
this.childTargetsByTargetId.set(target.targetInfo.targetId, target)
|
||||
return replaced
|
||||
}
|
||||
|
||||
rootTargetCount(): number {
|
||||
return this.targets.size
|
||||
}
|
||||
|
||||
listRootTargets(): ConnectedTarget[] {
|
||||
return Array.from(this.targets.values())
|
||||
}
|
||||
|
||||
getRootTargetByTabId(tabId: number): ConnectedTarget | undefined {
|
||||
return this.tabTargets.get(tabId)
|
||||
}
|
||||
|
||||
getRootTargetBySessionId(sessionId: string): ConnectedTarget | undefined {
|
||||
return this.targets.get(sessionId) ?? this.listRootTargets().find((target) => {
|
||||
return target.opencodeBrowserSessionId === sessionId
|
||||
})
|
||||
}
|
||||
|
||||
reserveTargetOwnership(targetId: string, sessionId: string): TargetOwnershipReservation {
|
||||
const target = this.targetsByTargetId.get(targetId)
|
||||
if (!target) {
|
||||
throw new TargetOwnershipError({ reason: "not-found", message: `Target detached before adoption: ${targetId}` })
|
||||
}
|
||||
const owner = target.opencodeBrowserSessionId
|
||||
if (owner && owner !== sessionId) {
|
||||
throw targetOwnedError(owner)
|
||||
}
|
||||
this.addRootTarget({ ...target, opencodeBrowserSessionId: sessionId })
|
||||
const reservation = {
|
||||
targetId,
|
||||
targetSessionId: target.sessionId,
|
||||
tabId: target.tabId,
|
||||
sessionId,
|
||||
...(owner ? { previousOpenCodeBrowserSessionId: owner } : {}),
|
||||
}
|
||||
this.pendingOwnershipReservations.set(targetId, reservation)
|
||||
return reservation
|
||||
}
|
||||
|
||||
rollbackTargetOwnership(reservation: TargetOwnershipReservation): TargetOwnershipChange {
|
||||
this.pendingOwnershipReservations.delete(reservation.targetId)
|
||||
const target = this.targetsByTargetId.get(reservation.targetId)
|
||||
if (!target || target.sessionId !== reservation.targetSessionId || target.opencodeBrowserSessionId !== reservation.sessionId) {
|
||||
return emptyOwnershipChange
|
||||
}
|
||||
const { opencodeBrowserSessionId: _owner, ...unowned } = target
|
||||
this.addRootTarget(reservation.previousOpenCodeBrowserSessionId
|
||||
? { ...unowned, opencodeBrowserSessionId: reservation.previousOpenCodeBrowserSessionId }
|
||||
: unowned)
|
||||
return ownershipChange(target.targetInfo.targetId, target.tabId)
|
||||
}
|
||||
|
||||
commitTargetOwnership(options: {
|
||||
readonly reservation: TargetOwnershipReservation
|
||||
readonly previousAdoptedTargetId?: string
|
||||
}): TargetOwnershipChange {
|
||||
this.pendingOwnershipReservations.delete(options.reservation.targetId)
|
||||
const target = this.targetsByTargetId.get(options.reservation.targetId)
|
||||
if (!target || target.sessionId !== options.reservation.targetSessionId || target.opencodeBrowserSessionId !== options.reservation.sessionId) {
|
||||
throw new TargetOwnershipError({
|
||||
reason: "generation-changed",
|
||||
message: `Target detached or changed during adoption: ${options.reservation.targetId}`,
|
||||
})
|
||||
}
|
||||
const targetIds = [target.targetInfo.targetId]
|
||||
const tabIds = [target.tabId]
|
||||
if (options.previousAdoptedTargetId && options.previousAdoptedTargetId !== target.targetInfo.targetId) {
|
||||
const released = this.releaseTargetOwnership(options.previousAdoptedTargetId, options.reservation.sessionId)
|
||||
targetIds.push(...released.targetIds)
|
||||
tabIds.push(...released.tabIds)
|
||||
}
|
||||
return { targetIds: Array.from(new Set(targetIds)), tabIds: Array.from(new Set(tabIds)) }
|
||||
}
|
||||
|
||||
releaseTargetOwnership(targetId: string, sessionId: string): TargetOwnershipChange {
|
||||
const target = this.targetsByTargetId.get(targetId)
|
||||
if (!target || target.opencodeBrowserSessionId !== sessionId) {
|
||||
return emptyOwnershipChange
|
||||
}
|
||||
const { opencodeBrowserSessionId: _owner, ...released } = target
|
||||
this.addRootTarget(released)
|
||||
return ownershipChange(target.targetInfo.targetId, target.tabId)
|
||||
}
|
||||
|
||||
detachRootTargetState(tabId: number, options: {
|
||||
readonly preserveChildParentSessionId?: string
|
||||
readonly preserveFrameEvents?: boolean
|
||||
} = {}): { readonly target: ConnectedTarget; readonly childSessionIds: string[] } | undefined {
|
||||
this.stagedRootTargets.delete(tabId)
|
||||
const target = this.tabTargets.get(tabId)
|
||||
// Resolve the entire staged subtree before recursive detach mutates the indexes.
|
||||
const preservedSessionIds = new Set<string>()
|
||||
if (options.preserveChildParentSessionId !== undefined) {
|
||||
preservedSessionIds.add(options.preserveChildParentSessionId)
|
||||
for (const parentSessionId of preservedSessionIds) {
|
||||
for (const child of this.childTargets.values()) {
|
||||
if (child.tabId === tabId && child.parentSessionId === parentSessionId) {
|
||||
preservedSessionIds.add(child.sessionId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const childSessionIds = Array.from(this.childTargets.values())
|
||||
.filter((child) => child.tabId === tabId && !preservedSessionIds.has(child.sessionId))
|
||||
.map((child) => child.sessionId)
|
||||
if (target) {
|
||||
this.targets.delete(target.sessionId)
|
||||
this.tabTargets.delete(tabId)
|
||||
this.targetsByTargetId.delete(target.targetInfo.targetId)
|
||||
this.pendingOwnershipReservations.delete(target.targetInfo.targetId)
|
||||
}
|
||||
if (!options.preserveFrameEvents) this.tabFrameEvents.delete(tabId)
|
||||
for (const sessionId of childSessionIds) this.detachChildTargetState(sessionId)
|
||||
return target ? { target, childSessionIds } : undefined
|
||||
}
|
||||
|
||||
detachChildTargetState(sessionId: string): ChildTarget | undefined {
|
||||
const target = this.childTargets.get(sessionId)
|
||||
this.childTargets.delete(sessionId)
|
||||
if (target) {
|
||||
this.childTargetsByTargetId.delete(target.targetInfo.targetId)
|
||||
}
|
||||
for (const child of this.childTargets.values()) {
|
||||
if (child.parentSessionId === sessionId) this.detachChildTargetState(child.sessionId)
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
updateTargetUrl(tabId: number, url: string): void {
|
||||
const staged = this.stagedRootTargets.get(tabId)
|
||||
if (staged) {
|
||||
this.stagedRootTargets.set(tabId, { ...staged, targetInfo: { ...staged.targetInfo, title: url, url }, crashed: false })
|
||||
return
|
||||
}
|
||||
const target = this.tabTargets.get(tabId)
|
||||
if (!target) {
|
||||
return
|
||||
}
|
||||
this.addRootTarget({ ...target, targetInfo: { ...target.targetInfo, title: url, url }, crashed: false })
|
||||
}
|
||||
|
||||
/** Record or lift a protected-frame debugger block. Returns the target only when its state changed. */
|
||||
markRootTargetProtectedUi(tabId: number, protectedUi: boolean): ConnectedTarget | undefined {
|
||||
const target = this.tabTargets.get(tabId)
|
||||
if (!target || (target.protectedUi === true) === protectedUi) {
|
||||
return undefined
|
||||
}
|
||||
const updated: ConnectedTarget = { ...target, protectedUi }
|
||||
this.addRootTarget(updated)
|
||||
return updated
|
||||
}
|
||||
|
||||
markRootTargetCrashed(tabId: number): ConnectedTarget | undefined {
|
||||
const staged = this.stagedRootTargets.get(tabId)
|
||||
if (staged) {
|
||||
const crashed = { ...staged, crashed: true }
|
||||
this.stagedRootTargets.set(tabId, crashed)
|
||||
return crashed
|
||||
}
|
||||
const target = this.tabTargets.get(tabId)
|
||||
if (!target) {
|
||||
return undefined
|
||||
}
|
||||
const crashed: ConnectedTarget = { ...target, crashed: true }
|
||||
this.addRootTarget(crashed)
|
||||
return crashed
|
||||
}
|
||||
|
||||
updateConnectedTargetInfo(options: { readonly tabId: number; readonly targetInfo: TargetInfo }): ConnectedTargetInfoUpdate | undefined {
|
||||
if (this.childTargetsByTargetId.has(options.targetInfo.targetId)) {
|
||||
this.updateChildTargetInfo(options.targetInfo)
|
||||
const target = this.childTargetsByTargetId.get(options.targetInfo.targetId)
|
||||
return target ? { kind: "child", target } : undefined
|
||||
}
|
||||
const root = this.stagedRootTargets.get(options.tabId) ?? this.tabTargets.get(options.tabId)
|
||||
if (root?.targetInfo.targetId !== options.targetInfo.targetId) {
|
||||
return undefined
|
||||
}
|
||||
this.updateRootTargetInfo(options.tabId, options.targetInfo)
|
||||
const target = this.tabTargets.get(options.tabId)
|
||||
return target ? { kind: "root", target } : undefined
|
||||
}
|
||||
|
||||
updateRootTargetInfo(tabId: number, targetInfo: TargetInfo): void {
|
||||
const staged = this.stagedRootTargets.get(tabId)
|
||||
if (staged?.targetInfo.targetId === targetInfo.targetId) {
|
||||
this.stagedRootTargets.set(tabId, { ...staged, targetInfo })
|
||||
return
|
||||
}
|
||||
const target = this.tabTargets.get(tabId)
|
||||
if (!target) {
|
||||
return
|
||||
}
|
||||
const updated: ConnectedTarget = { ...target, targetInfo }
|
||||
this.targets.set(updated.sessionId, updated)
|
||||
this.tabTargets.set(tabId, updated)
|
||||
this.targetsByTargetId.delete(target.targetInfo.targetId)
|
||||
this.targetsByTargetId.set(targetInfo.targetId, updated)
|
||||
}
|
||||
|
||||
updateChildTargetInfo(targetInfo: TargetInfo): void {
|
||||
const target = this.childTargetsByTargetId.get(targetInfo.targetId)
|
||||
if (!target) {
|
||||
return
|
||||
}
|
||||
const updated: ChildTarget = { ...target, targetInfo }
|
||||
this.childTargets.set(updated.sessionId, updated)
|
||||
this.childTargetsByTargetId.delete(target.targetInfo.targetId)
|
||||
this.childTargetsByTargetId.set(targetInfo.targetId, updated)
|
||||
}
|
||||
|
||||
rememberFrameEvent(options: {
|
||||
readonly tabId: number
|
||||
readonly frameId: string
|
||||
readonly attached?: JsonObject
|
||||
readonly navigated?: JsonObject
|
||||
}): void {
|
||||
const frameEvents = this.tabFrameEvents.get(options.tabId) ?? new Map<string, StoredFrameEvents>()
|
||||
const existing = frameEvents.get(options.frameId)
|
||||
const attached = options.attached ?? existing?.attached
|
||||
const navigated = options.navigated ?? existing?.navigated
|
||||
const next: StoredFrameEvents = {
|
||||
frameId: options.frameId,
|
||||
...(attached ? { attached } : {}),
|
||||
...(navigated ? { navigated } : {}),
|
||||
}
|
||||
frameEvents.set(options.frameId, next)
|
||||
this.tabFrameEvents.set(options.tabId, frameEvents)
|
||||
}
|
||||
|
||||
allTargetInfos(options: {
|
||||
readonly isRestrictedTarget: (targetInfo: TargetInfo) => boolean
|
||||
readonly isVisibleTarget?: (target: ConnectedTarget | ChildTarget) => boolean
|
||||
}): TargetInfo[] {
|
||||
return [...this.targets.values(), ...this.childTargets.values()].flatMap((target) => {
|
||||
if (!(options.isVisibleTarget?.(target) ?? true)) return []
|
||||
if (options.isRestrictedTarget(target.targetInfo)) return []
|
||||
return [{ ...target.targetInfo, attached: true }]
|
||||
})
|
||||
}
|
||||
|
||||
findFrameEventsForChild(target: ChildTarget): StoredFrameEvents | undefined {
|
||||
const frameEvents = this.tabFrameEvents.get(target.tabId)
|
||||
if (!frameEvents) {
|
||||
return undefined
|
||||
}
|
||||
const exactMatch = frameEvents.get(target.targetInfo.targetId)
|
||||
if (exactMatch) {
|
||||
return exactMatch
|
||||
}
|
||||
return Array.from(frameEvents.values()).find((candidate) => {
|
||||
const frame = isJsonObject(candidate.navigated?.frame) ? candidate.navigated.frame : undefined
|
||||
if (!frame) {
|
||||
return false
|
||||
}
|
||||
const parentMatches = !target.targetInfo.parentFrameId || frame.parentId === target.targetInfo.parentFrameId
|
||||
const urlMatches = !target.targetInfo.url || frame.url === target.targetInfo.url
|
||||
return parentMatches && urlMatches
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const emptyOwnershipChange: TargetOwnershipChange = { targetIds: [], tabIds: [] }
|
||||
|
||||
function ownershipChange(targetId: string, tabId: number): TargetOwnershipChange {
|
||||
return { targetIds: [targetId], tabIds: tabId < 0 ? [] : [tabId] }
|
||||
}
|
||||
|
||||
function targetOwnedError(owner: string): Error {
|
||||
return new TargetOwnershipError({
|
||||
reason: "owned",
|
||||
ownerSessionId: owner,
|
||||
message: `Target is already adopted by session ${owner}. Use that session, or reset/delete it to release the tab before adopting it elsewhere.`,
|
||||
})
|
||||
}
|
||||
37
packages/browser-extension/cli/src/version.ts
Normal file
37
packages/browser-extension/cli/src/version.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import crypto from "node:crypto"
|
||||
import fs from "node:fs"
|
||||
import path from "node:path"
|
||||
import { fileURLToPath } from "node:url"
|
||||
|
||||
declare global {
|
||||
// Injected by scripts/build-cli.ts at build time.
|
||||
var __OPENCODE_BROWSER_VERSION__: string | undefined
|
||||
var __OPENCODE_BROWSER_BUILD_ID__: string | undefined
|
||||
}
|
||||
|
||||
export const opencodeBrowserVersion: string = globalThis.__OPENCODE_BROWSER_VERSION__ ?? "0.0.0-dev"
|
||||
export const opencodeBrowserBuildId: string = globalThis.__OPENCODE_BROWSER_BUILD_ID__ ?? sourceBuildId()
|
||||
|
||||
export function sourceBuildIdForFiles(files: readonly { readonly name: string; readonly content: string | Buffer }[]): string {
|
||||
const hash = crypto.createHash("sha256")
|
||||
for (const file of [...files].sort((left, right) => left.name < right.name ? -1 : left.name > right.name ? 1 : 0)) {
|
||||
hash.update(file.name)
|
||||
hash.update("\0")
|
||||
hash.update(file.content)
|
||||
hash.update("\0")
|
||||
}
|
||||
return `source-${hash.digest("hex").slice(0, 16)}`
|
||||
}
|
||||
|
||||
function sourceBuildId(): string {
|
||||
const srcDir = path.dirname(fileURLToPath(import.meta.url))
|
||||
const rootDir = path.join(srcDir, "..")
|
||||
const files = fs.readdirSync(srcDir, { withFileTypes: true })
|
||||
.filter((entry) => entry.isFile() && entry.name.endsWith(".ts"))
|
||||
.map((entry) => ({ name: `src/${entry.name}`, content: fs.readFileSync(path.join(srcDir, entry.name)) }))
|
||||
for (const name of ["package.json", "bun.lock"]) {
|
||||
const file = path.join(rootDir, name)
|
||||
if (fs.existsSync(file)) files.push({ name, content: fs.readFileSync(file) })
|
||||
}
|
||||
return sourceBuildIdForFiles(files)
|
||||
}
|
||||
210
packages/browser-extension/cli/src/webmcp.ts
Normal file
210
packages/browser-extension/cli/src/webmcp.ts
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
import type { Frame, Page } from "patchright-core"
|
||||
|
||||
const defaultFrameTimeoutMs = 5_000
|
||||
const maxTools = 100
|
||||
const maxDescriptionLength = 1_000
|
||||
const maxSchemaLength = 32_000
|
||||
|
||||
type WebMcpTool = {
|
||||
readonly name: string
|
||||
readonly title?: string
|
||||
readonly description: string
|
||||
readonly inputSchema?: unknown
|
||||
readonly annotations?: {
|
||||
readonly readOnly?: boolean
|
||||
readonly untrustedContent?: boolean
|
||||
readonly consequential?: boolean
|
||||
}
|
||||
readonly origin?: string
|
||||
readonly frame: string
|
||||
readonly frameUrl: string
|
||||
}
|
||||
|
||||
type WebMcpListing = {
|
||||
readonly available: boolean
|
||||
readonly tools: readonly WebMcpTool[]
|
||||
readonly omitted: number
|
||||
}
|
||||
|
||||
export type WebMcpHelper = {
|
||||
readonly list: () => Promise<WebMcpListing>
|
||||
readonly call: (name: string, input?: Record<string, unknown>, options?: {
|
||||
readonly frame?: string
|
||||
readonly timeout?: number
|
||||
}) => Promise<unknown>
|
||||
}
|
||||
|
||||
type CollectedTool = Omit<WebMcpTool, "frame" | "frameUrl">
|
||||
|
||||
export function createWebMcpHelper(page: Page): WebMcpHelper {
|
||||
return {
|
||||
list: async () => {
|
||||
const listing = await collectWebMcpToolsWithFrames(page, defaultFrameTimeoutMs)
|
||||
return {
|
||||
available: listing.available,
|
||||
tools: listing.tools.map(({ tool }) => tool),
|
||||
omitted: listing.omitted,
|
||||
}
|
||||
},
|
||||
call: async (name, input = {}, options = {}) => {
|
||||
if (!name.trim()) throw new Error("webmcp.call requires a non-empty tool name")
|
||||
const listing = await collectWebMcpToolsWithFrames(page, options.timeout ?? defaultFrameTimeoutMs)
|
||||
const matches = listing.tools.filter((candidate) => {
|
||||
return candidate.tool.name === name &&
|
||||
(options.frame === undefined || candidate.tool.frame === options.frame || candidate.tool.frameUrl === options.frame)
|
||||
})
|
||||
if (matches.length === 0) {
|
||||
const available = listing.tools.map(({ tool }) => tool.name)
|
||||
throw new Error(`No WebMCP tool named ${JSON.stringify(name)}${options.frame ? ` in frame ${options.frame}` : ""}.${available.length > 0 ? ` Available tools: ${[...new Set(available)].join(", ")}.` : " The page does not register any WebMCP tools."}`)
|
||||
}
|
||||
if (matches.length > 1) {
|
||||
throw new Error(`WebMCP tool ${JSON.stringify(name)} is registered in multiple frames; pass { frame }. Matching frames: ${matches.map(({ tool }) => tool.frame).join(", ")}`)
|
||||
}
|
||||
const match = matches[0]!
|
||||
return await withTimeout(
|
||||
match.frame.evaluate(callWebMcpToolInPage, { name, inputJson: JSON.stringify(input) }),
|
||||
options.timeout ?? defaultFrameTimeoutMs,
|
||||
`WebMCP tool ${JSON.stringify(name)} timed out`,
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async function collectWebMcpToolsWithFrames(page: Page, timeoutMs: number): Promise<{
|
||||
readonly available: boolean
|
||||
readonly tools: readonly { readonly frame: Frame; readonly tool: WebMcpTool }[]
|
||||
readonly omitted: number
|
||||
}> {
|
||||
const frames = page.frames()
|
||||
const urlCounts = new Map<string, number>()
|
||||
for (const frame of frames) urlCounts.set(frame.url(), (urlCounts.get(frame.url()) ?? 0) + 1)
|
||||
const results = await Promise.all(frames.map(async (frame, index) => {
|
||||
const frameUrl = frame.url()
|
||||
const frameLabel = (urlCounts.get(frameUrl) ?? 0) > 1 ? `${frameUrl} (frame ${index})` : frameUrl
|
||||
const result = await withTimeout(frame.evaluate(collectWebMcpToolsInPage), timeoutMs, "WebMCP discovery timed out")
|
||||
.catch(() => null)
|
||||
return { frame, frameUrl, frameLabel, result }
|
||||
}))
|
||||
const available = results.some(({ result }) => result !== null)
|
||||
let discovered = 0
|
||||
const collected: Array<{ readonly frame: Frame; readonly tool: WebMcpTool }> = []
|
||||
for (const { frame, frameUrl, frameLabel, result } of results) {
|
||||
if (!result) continue
|
||||
for (const raw of result) {
|
||||
discovered += 1
|
||||
if (collected.length >= maxTools) continue
|
||||
const tool = normalizeCollectedTool(raw, frameLabel, frameUrl)
|
||||
if (tool) collected.push({ frame, tool })
|
||||
}
|
||||
}
|
||||
return { available, tools: collected, omitted: Math.max(0, discovered - maxTools) }
|
||||
}
|
||||
|
||||
function collectWebMcpToolsInPage(): Promise<CollectedTool[] | null> | null {
|
||||
type PageTool = {
|
||||
readonly name?: unknown
|
||||
readonly title?: unknown
|
||||
readonly description?: unknown
|
||||
readonly inputSchema?: unknown
|
||||
readonly annotations?: Record<string, unknown>
|
||||
readonly origin?: unknown
|
||||
readonly window?: Window
|
||||
}
|
||||
type ModelContext = { readonly getTools?: () => Promise<PageTool[]> }
|
||||
const context = (document as Document & { modelContext?: ModelContext }).modelContext ??
|
||||
(navigator as Navigator & { modelContext?: ModelContext }).modelContext
|
||||
if (!context?.getTools) return null
|
||||
return Promise.resolve(context.getTools()).then((tools) => tools
|
||||
.filter((tool) => !("window" in tool) || tool.window === window)
|
||||
.map((tool) => {
|
||||
let inputSchema = tool.inputSchema
|
||||
if (typeof inputSchema === "string") {
|
||||
try {
|
||||
inputSchema = JSON.parse(inputSchema)
|
||||
} catch {
|
||||
inputSchema = undefined
|
||||
}
|
||||
}
|
||||
return {
|
||||
name: typeof tool.name === "string" ? tool.name : "",
|
||||
...(typeof tool.title === "string" ? { title: tool.title } : {}),
|
||||
description: typeof tool.description === "string" ? tool.description : "",
|
||||
...(inputSchema === undefined ? {} : { inputSchema }),
|
||||
...(tool.annotations ? {
|
||||
annotations: {
|
||||
readOnly: Boolean(tool.annotations.readOnlyHint ?? tool.annotations.readOnly),
|
||||
untrustedContent: Boolean(tool.annotations.untrustedContentHint ?? tool.annotations.untrustedContent),
|
||||
consequential: Boolean(tool.annotations.consequentialHint ?? tool.annotations.consequential),
|
||||
},
|
||||
} : {}),
|
||||
...(typeof tool.origin === "string" ? { origin: tool.origin } : {}),
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
function callWebMcpToolInPage(options: { readonly name: string; readonly inputJson: string }): Promise<unknown> {
|
||||
type PageTool = { readonly name?: unknown; readonly window?: Window }
|
||||
type ModelContext = {
|
||||
readonly getTools?: () => Promise<PageTool[]>
|
||||
readonly executeTool?: (tool: PageTool, input: string) => Promise<unknown>
|
||||
readonly invokeTool?: (name: string, input: unknown) => Promise<unknown>
|
||||
}
|
||||
const context = (document as Document & { modelContext?: ModelContext }).modelContext ??
|
||||
(navigator as Navigator & { modelContext?: ModelContext }).modelContext
|
||||
if (!context?.getTools) throw new Error("WebMCP is not available on this page")
|
||||
return Promise.resolve(context.getTools()).then(async (tools) => {
|
||||
const tool = tools.filter((candidate) => !("window" in candidate) || candidate.window === window)
|
||||
.find((candidate) => candidate.name === options.name)
|
||||
if (!tool) throw new Error(`WebMCP tool ${JSON.stringify(options.name)} is not registered in this frame`)
|
||||
const result = context.executeTool
|
||||
? await context.executeTool(tool, options.inputJson)
|
||||
: context.invokeTool
|
||||
? await context.invokeTool(options.name, JSON.parse(options.inputJson))
|
||||
: (() => { throw new Error("WebMCP tool execution is not available on this page") })()
|
||||
if (typeof result !== "string") return result ?? null
|
||||
try {
|
||||
return JSON.parse(result)
|
||||
} catch {
|
||||
return result
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeCollectedTool(raw: CollectedTool, frame: string, frameUrl: string): WebMcpTool | undefined {
|
||||
const name = typeof raw.name === "string" ? raw.name.trim() : ""
|
||||
if (!name) return undefined
|
||||
let inputSchema = raw.inputSchema
|
||||
if (inputSchema !== undefined) {
|
||||
try {
|
||||
const serialized = JSON.stringify(inputSchema)
|
||||
inputSchema = serialized.length <= maxSchemaLength ? JSON.parse(serialized) : undefined
|
||||
} catch {
|
||||
inputSchema = undefined
|
||||
}
|
||||
}
|
||||
const annotations = raw.annotations && Object.values(raw.annotations).some(Boolean) ? raw.annotations : undefined
|
||||
return {
|
||||
name,
|
||||
...(raw.title ? { title: raw.title.slice(0, 200) } : {}),
|
||||
description: raw.description.slice(0, maxDescriptionLength),
|
||||
...(inputSchema === undefined ? {} : { inputSchema }),
|
||||
...(annotations ? { annotations } : {}),
|
||||
...(raw.origin ? { origin: raw.origin } : {}),
|
||||
frame,
|
||||
frameUrl,
|
||||
}
|
||||
}
|
||||
|
||||
async function withTimeout<A>(promise: Promise<A>, timeoutMs: number, message: string): Promise<A> {
|
||||
let timer: ReturnType<typeof setTimeout> | undefined
|
||||
try {
|
||||
return await Promise.race([
|
||||
promise,
|
||||
new Promise<never>((_, reject) => {
|
||||
timer = setTimeout(() => reject(new Error(`${message} after ${timeoutMs}ms`)), timeoutMs)
|
||||
}),
|
||||
])
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer)
|
||||
}
|
||||
}
|
||||
22
packages/browser-extension/cli/tsconfig.json
Normal file
22
packages/browser-extension/cli/tsconfig.json
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"strict": true,
|
||||
"exactOptionalPropertyTypes": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"moduleDetection": "force",
|
||||
"allowImportingTsExtensions": true,
|
||||
"noEmit": true,
|
||||
"skipLibCheck": true,
|
||||
"types": [
|
||||
"node",
|
||||
"chrome"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"build.ts"
|
||||
]
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue