opencode-browser-kit 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/dist/plugin.js +12400 -18
- package/package.json +2 -2
- package/src/plugin.ts +70 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-browser-kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Headless browser + visual element picker for OpenCode. Tools: browser_navigate, browser_snapshop, browser_click, browser_type, browser_screenshot, browser_close and browser_inspect (opens a visible browser so you click an element and type what to change).",
|
|
5
5
|
"main": "dist/plugin.js",
|
|
6
6
|
"type": "module",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"README.md"
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
|
-
"build": "bun build src/plugin.ts --outdir dist --target node --external
|
|
13
|
+
"build": "bun build src/plugin.ts --outdir dist --target node --external playwright",
|
|
14
14
|
"prepublishOnly": "bun run build"
|
|
15
15
|
},
|
|
16
16
|
"keywords": [
|
package/src/plugin.ts
CHANGED
|
@@ -1,18 +1,83 @@
|
|
|
1
1
|
import { type Plugin, tool } from "@opencode-ai/plugin"
|
|
2
2
|
import type { Browser, BrowserContext, Page } from "playwright"
|
|
3
|
-
import { chromium } from "playwright"
|
|
4
3
|
import fs from "fs"
|
|
5
4
|
import os from "os"
|
|
6
5
|
import path from "path"
|
|
6
|
+
import { spawn } from "node:child_process"
|
|
7
|
+
import { createRequire } from "node:module"
|
|
8
|
+
import { fileURLToPath } from "url"
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
type PlaywrightModule = typeof import("playwright")
|
|
11
|
+
|
|
12
|
+
function pluginRoot(): string {
|
|
13
|
+
// dist/plugin.js -> package root; src/plugin.ts -> package root too
|
|
14
|
+
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
15
|
+
return path.basename(here) === "dist" ? path.join(here, "..") : here
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let playwrightModule: PlaywrightModule | null = null
|
|
19
|
+
|
|
20
|
+
function hasPlaywrightOnDisk(): boolean {
|
|
21
|
+
const root = pluginRoot()
|
|
22
|
+
return (
|
|
23
|
+
fs.existsSync(path.join(root, "node_modules", "playwright", "package.json")) ||
|
|
24
|
+
fs.existsSync(path.join(root, "..", "node_modules", "playwright", "package.json"))
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function selfHealInstall(): Promise<void> {
|
|
29
|
+
const root = pluginRoot()
|
|
30
|
+
const cacheDir = path.join(os.tmpdir(), "opencode-bun-cache")
|
|
31
|
+
fs.mkdirSync(cacheDir, { recursive: true })
|
|
32
|
+
const envPatch = { BUN_INSTALL_CACHE_DIR: cacheDir, CI: "1" } as Record<string, string>
|
|
33
|
+
const install = (command: string, ms: number) =>
|
|
34
|
+
new Promise<boolean>((resolve) => {
|
|
35
|
+
const child = spawn(command, { cwd: root, shell: true, stdio: "ignore", env: { ...process.env, ...envPatch } })
|
|
36
|
+
let done = false
|
|
37
|
+
const finish = (ok: boolean) => {
|
|
38
|
+
if (done) return
|
|
39
|
+
done = true
|
|
40
|
+
clearTimeout(timer)
|
|
41
|
+
resolve(ok)
|
|
42
|
+
}
|
|
43
|
+
const timer = setTimeout(() => {
|
|
44
|
+
try { child.kill("SIGKILL") } catch {}
|
|
45
|
+
finish(false)
|
|
46
|
+
}, ms)
|
|
47
|
+
child.on("exit", (code) => finish(code === 0))
|
|
48
|
+
child.on("error", () => finish(false))
|
|
49
|
+
})
|
|
50
|
+
if (!hasPlaywrightOnDisk()) await install(`bun add playwright@^1.64.0`, 45000)
|
|
51
|
+
if (!hasPlaywrightOnDisk())
|
|
52
|
+
await install(`npm install playwright@^1.64.0 --no-audit --no-fund --loglevel=error`, 120000)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function loadPlaywright(): Promise<PlaywrightModule> {
|
|
56
|
+
if (playwrightModule) return playwrightModule
|
|
57
|
+
// IMPORTANT: never `import("playwright")` speculatively. If the package is
|
|
58
|
+
// missing, bun's runtime auto-install kicks in and can hang for minutes on
|
|
59
|
+
// slow package caches; and after a fresh install, bun's async ESM loader
|
|
60
|
+
// can stall on the new files. require() via createRequire is instant and
|
|
61
|
+
// deterministic, resolved from this plugin's own directory.
|
|
62
|
+
if (!hasPlaywrightOnDisk()) await selfHealInstall()
|
|
63
|
+
if (!hasPlaywrightOnDisk()) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
"opencode-browser-kit could not install playwright. Run manually in the plugin's folder: `bun add playwright && bunx playwright install chromium` (or use system Chrome/Edge).",
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
playwrightModule = createRequire(import.meta.url)("playwright") as PlaywrightModule
|
|
69
|
+
return playwrightModule
|
|
70
|
+
}
|
|
10
71
|
|
|
11
72
|
let browser: Browser | null = null
|
|
12
73
|
let context: BrowserContext | null = null
|
|
13
74
|
let page: Page | null = null
|
|
14
75
|
|
|
76
|
+
const SCREENSHOT_DIR = path.join(os.tmpdir(), "opencode-browser-screenshots")
|
|
77
|
+
const VIEWPORT = { width: 1280, height: 800 }
|
|
78
|
+
|
|
15
79
|
async function launchBrowser(headless = true): Promise<Browser> {
|
|
80
|
+
const { chromium } = await loadPlaywright()
|
|
16
81
|
const attempts: { channel?: string }[] = [
|
|
17
82
|
{},
|
|
18
83
|
{ channel: "chrome" },
|
|
@@ -78,7 +143,7 @@ function formatSnap(file: string): string {
|
|
|
78
143
|
return `\n\nScreenshot saved: ${file}\n(Use the read tool on that path to view it.)`
|
|
79
144
|
}
|
|
80
145
|
|
|
81
|
-
|
|
146
|
+
const PICKER_JS = `(function () {
|
|
82
147
|
if (window.__ocPickerActive) return
|
|
83
148
|
window.__ocPickerActive = true
|
|
84
149
|
window.__ocPicked = undefined
|
|
@@ -426,6 +491,7 @@ export const BrowserPlugin: Plugin = async (ctx) => {
|
|
|
426
491
|
async execute(args) {
|
|
427
492
|
let inspectBrowser: Browser | null = null
|
|
428
493
|
try {
|
|
494
|
+
const { chromium } = await loadPlaywright()
|
|
429
495
|
inspectBrowser = await launchBrowser(false)
|
|
430
496
|
const pageCtx = await inspectBrowser.newContext({
|
|
431
497
|
viewport: VIEWPORT,
|