opencode/packages/browser-extension/cli/build.ts
Ryan Vogel 982d17cdb8 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.
2026-10-18 18:16:01 -04:00

57 lines
2.6 KiB
TypeScript

// 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})`)