@different-ai/opencode-browser 2.1.0 → 3.0.0

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/src/plugin.ts DELETED
@@ -1,672 +0,0 @@
1
- /**
2
- * OpenCode Browser Plugin
3
- *
4
- * OpenCode Plugin (this) <--WebSocket:19222--> Chrome Extension
5
- *
6
- * Notes
7
- * - Uses a lock file so only one OpenCode session owns the browser.
8
- * - Supports a *soft takeover* (SIGUSR1) so we don't have to kill OpenCode.
9
- */
10
-
11
- import type { Plugin } from "@opencode-ai/plugin";
12
- import { tool } from "@opencode-ai/plugin";
13
- import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "fs";
14
- import { homedir } from "os";
15
- import { join } from "path";
16
-
17
- const WS_PORT = 19222;
18
- const BASE_DIR = join(homedir(), ".opencode-browser");
19
- const LOCK_FILE = join(BASE_DIR, "lock.json");
20
- const SCREENSHOTS_DIR = join(BASE_DIR, "screenshots");
21
-
22
- // If a session hasn't used the browser in this long, allow soft takeover by default.
23
- const LOCK_TTL_MS = 2 * 60 * 60 * 1000; // 2 hours
24
-
25
- mkdirSync(BASE_DIR, { recursive: true });
26
- mkdirSync(SCREENSHOTS_DIR, { recursive: true });
27
-
28
- // Session state
29
- const sessionId = Math.random().toString(36).slice(2);
30
- const pid = process.pid;
31
- let ws: WebSocket | null = null;
32
- let isConnected = false;
33
- let server: ReturnType<typeof Bun.serve> | null = null;
34
- let pendingRequests = new Map<number, { resolve: (v: any) => void; reject: (e: Error) => void }>();
35
- let requestId = 0;
36
-
37
- interface LockInfo {
38
- pid: number;
39
- sessionId: string;
40
- startedAt: string;
41
- lastUsedAt: string;
42
- cwd: string;
43
- }
44
-
45
- function nowIso(): string {
46
- return new Date().toISOString();
47
- }
48
-
49
- function readLock(): LockInfo | null {
50
- try {
51
- if (!existsSync(LOCK_FILE)) return null;
52
- return JSON.parse(readFileSync(LOCK_FILE, "utf-8"));
53
- } catch {
54
- return null;
55
- }
56
- }
57
-
58
- function writeLock(): void {
59
- writeFileSync(
60
- LOCK_FILE,
61
- JSON.stringify(
62
- {
63
- pid,
64
- sessionId,
65
- startedAt: nowIso(),
66
- lastUsedAt: nowIso(),
67
- cwd: process.cwd(),
68
- } satisfies LockInfo,
69
- null,
70
- 2
71
- ) + "\n"
72
- );
73
- }
74
-
75
- function touchLock(): void {
76
- const lock = readLock();
77
- if (!lock) return;
78
- if (lock.sessionId !== sessionId) return;
79
-
80
- try {
81
- writeFileSync(
82
- LOCK_FILE,
83
- JSON.stringify(
84
- {
85
- ...lock,
86
- lastUsedAt: nowIso(),
87
- } satisfies LockInfo,
88
- null,
89
- 2
90
- ) + "\n"
91
- );
92
- } catch {
93
- // Ignore
94
- }
95
- }
96
-
97
- function releaseLock(): void {
98
- try {
99
- const lock = readLock();
100
- if (lock && lock.sessionId === sessionId) {
101
- unlinkSync(LOCK_FILE);
102
- }
103
- } catch {
104
- // Ignore
105
- }
106
- }
107
-
108
- function isProcessAlive(targetPid: number): boolean {
109
- try {
110
- process.kill(targetPid, 0);
111
- return true;
112
- } catch {
113
- return false;
114
- }
115
- }
116
-
117
- function lockAgeMs(lock: LockInfo): number {
118
- const ts = lock.lastUsedAt || lock.startedAt;
119
- const n = Date.parse(ts);
120
- if (Number.isNaN(n)) return Number.POSITIVE_INFINITY;
121
- return Date.now() - n;
122
- }
123
-
124
- function isLockExpired(lock: LockInfo): boolean {
125
- return lockAgeMs(lock) > LOCK_TTL_MS;
126
- }
127
-
128
- function isPortFree(port: number): boolean {
129
- try {
130
- // If we can connect, something is already listening.
131
- const testSocket = Bun.connect({ port, timeout: 300 });
132
- testSocket.end();
133
- return false;
134
- } catch (e) {
135
- if ((e as any).code === "ECONNREFUSED") return true;
136
- return false;
137
- }
138
- }
139
-
140
- function stopBrowserServer(): void {
141
- try {
142
- (ws as any)?.close?.();
143
- } catch {
144
- // Ignore
145
- }
146
- ws = null;
147
- isConnected = false;
148
-
149
- try {
150
- server?.stop();
151
- } catch {
152
- // Ignore
153
- }
154
- server = null;
155
- }
156
-
157
- function startServer(): boolean {
158
- if (server) return true;
159
- if (!isPortFree(WS_PORT)) return false;
160
-
161
- try {
162
- server = Bun.serve({
163
- port: WS_PORT,
164
- fetch(req, server) {
165
- if (server.upgrade(req)) return;
166
- return new Response("OpenCode Browser Plugin", { status: 200 });
167
- },
168
- websocket: {
169
- open(wsClient) {
170
- console.error(`[browser-plugin] Chrome extension connected`);
171
- ws = wsClient as unknown as WebSocket;
172
- isConnected = true;
173
- },
174
- close() {
175
- console.error(`[browser-plugin] Chrome extension disconnected`);
176
- ws = null;
177
- isConnected = false;
178
- },
179
- message(_wsClient, data) {
180
- try {
181
- const message = JSON.parse(data.toString());
182
- handleMessage(message);
183
- } catch (e) {
184
- console.error(`[browser-plugin] Parse error:`, e);
185
- }
186
- },
187
- },
188
- });
189
-
190
- console.error(`[browser-plugin] WebSocket server listening on port ${WS_PORT}`);
191
- return true;
192
- } catch (e) {
193
- console.error(`[browser-plugin] Failed to start server:`, e);
194
- return false;
195
- }
196
- }
197
-
198
- function sleep(ms: number): Promise<void> {
199
- return new Promise((resolve) => setTimeout(resolve, ms));
200
- }
201
-
202
- async function waitForExtensionConnection(timeoutMs: number): Promise<boolean> {
203
- const start = Date.now();
204
- while (Date.now() - start < timeoutMs) {
205
- if (isConnected) return true;
206
- await sleep(100);
207
- }
208
- return isConnected;
209
- }
210
-
211
- async function requestSessionRelease(targetPid: number, opts?: { timeoutMs?: number }): Promise<{ success: boolean; error?: string }> {
212
- const timeoutMs = opts?.timeoutMs ?? 3000;
213
-
214
- try {
215
- // SIGUSR1 is treated as "release browser lock + stop server".
216
- // This does NOT terminate OpenCode.
217
- process.kill(targetPid, "SIGUSR1");
218
- } catch (e) {
219
- return { success: false, error: e instanceof Error ? e.message : String(e) };
220
- }
221
-
222
- const start = Date.now();
223
- while (Date.now() - start < timeoutMs) {
224
- const lock = readLock();
225
- const lockCleared = !lock || lock.pid !== targetPid;
226
- const portCleared = isPortFree(WS_PORT);
227
-
228
- if (lockCleared && portCleared) return { success: true };
229
- await sleep(100);
230
- }
231
-
232
- return {
233
- success: false,
234
- error: `Timed out waiting for PID ${targetPid} to release browser`,
235
- };
236
- }
237
-
238
- async function forceKillSession(targetPid: number): Promise<{ success: boolean; error?: string }> {
239
- try {
240
- process.kill(targetPid, "SIGTERM");
241
- let attempts = 0;
242
- while (isProcessAlive(targetPid) && attempts < 20) {
243
- await sleep(100);
244
- attempts++;
245
- }
246
- if (isProcessAlive(targetPid)) {
247
- process.kill(targetPid, "SIGKILL");
248
- }
249
- return { success: true };
250
- } catch (e) {
251
- return { success: false, error: e instanceof Error ? e.message : String(e) };
252
- }
253
- }
254
-
255
- function handleMessage(message: { type: string; id?: number; result?: any; error?: any }): void {
256
- if (message.type === "tool_response" && message.id !== undefined) {
257
- const pending = pendingRequests.get(message.id);
258
- if (!pending) return;
259
-
260
- pendingRequests.delete(message.id);
261
- if (message.error) {
262
- pending.reject(new Error(message.error.content || String(message.error)));
263
- } else {
264
- pending.resolve(message.result?.content);
265
- }
266
- }
267
- }
268
-
269
- function sendToChrome(message: any): boolean {
270
- if (ws && isConnected) {
271
- (ws as any).send(JSON.stringify(message));
272
- return true;
273
- }
274
- return false;
275
- }
276
-
277
- async function performTakeover(): Promise<string> {
278
- const lock = readLock();
279
-
280
- if (!lock) {
281
- writeLock();
282
- } else if (lock.sessionId === sessionId) {
283
- // Already ours.
284
- } else if (!isProcessAlive(lock.pid)) {
285
- // Dead PID -> stale.
286
- console.error(`[browser-plugin] Cleaning stale lock from dead PID ${lock.pid}`);
287
- writeLock();
288
- } else {
289
- const ageMinutes = Math.round(lockAgeMs(lock) / 60000);
290
- console.error(
291
- `[browser-plugin] Requesting release from PID ${lock.pid} (last used ${ageMinutes}m ago)...`
292
- );
293
- const released = await requestSessionRelease(lock.pid, { timeoutMs: 4000 });
294
- if (!released.success) {
295
- throw new Error(
296
- `Failed to takeover without killing OpenCode: ${released.error}. ` +
297
- `Try again, or use browser_force_kill_session as last resort.`
298
- );
299
- }
300
- console.error(`[browser-plugin] Previous session released gracefully.`);
301
- writeLock();
302
- }
303
-
304
- touchLock();
305
-
306
- if (!server) {
307
- if (!startServer()) {
308
- throw new Error("Failed to start WebSocket server after takeover.");
309
- }
310
- }
311
-
312
- const ok = await waitForExtensionConnection(3000);
313
- if (!ok) {
314
- throw new Error("Took over lock but Chrome extension did not connect.");
315
- }
316
-
317
- return "Browser now connected to this session.";
318
- }
319
-
320
- async function ensureLockAndServer(): Promise<void> {
321
- const existingLock = readLock();
322
-
323
- if (!existingLock) {
324
- writeLock();
325
- } else if (existingLock.sessionId === sessionId) {
326
- // Already ours.
327
- } else if (!isProcessAlive(existingLock.pid)) {
328
- // Stale lock (dead PID).
329
- console.error(`[browser-plugin] Cleaning stale lock from dead PID ${existingLock.pid}`);
330
- writeLock();
331
- } else {
332
- // Another session holds the lock - attempt automatic soft takeover
333
- const ageMinutes = Math.round(lockAgeMs(existingLock) / 60000);
334
- console.error(
335
- `[browser-plugin] Browser locked by PID ${existingLock.pid} (last used ${ageMinutes}m ago). Attempting auto-takeover...`
336
- );
337
-
338
- const released = await requestSessionRelease(existingLock.pid, { timeoutMs: 4000 });
339
- if (released.success) {
340
- console.error(`[browser-plugin] Auto-takeover succeeded. Previous session released gracefully.`);
341
- writeLock();
342
- } else {
343
- // Soft takeover failed - provide helpful error
344
- const expired = isLockExpired(existingLock);
345
- const why = expired ? "expired" : "active";
346
- throw new Error(
347
- `Browser locked by another session (PID ${existingLock.pid}, ${why}). ` +
348
- `Auto-takeover failed: ${released.error}. ` +
349
- `Use browser_force_kill_session as last resort, or browser_status for details.`
350
- );
351
- }
352
- }
353
-
354
- touchLock();
355
-
356
- if (!server) {
357
- if (!startServer()) {
358
- throw new Error("Failed to start WebSocket server. Port may be in use.");
359
- }
360
- }
361
-
362
- if (!isConnected) {
363
- const ok = await waitForExtensionConnection(3000);
364
- if (!ok) {
365
- throw new Error(
366
- "Chrome extension not connected. Make sure Chrome is running with the OpenCode Browser extension enabled."
367
- );
368
- }
369
- }
370
- }
371
-
372
- async function executeCommand(toolName: string, args: Record<string, any>): Promise<any> {
373
- await ensureLockAndServer();
374
-
375
- const id = ++requestId;
376
- touchLock();
377
-
378
- return new Promise((resolve, reject) => {
379
- pendingRequests.set(id, { resolve, reject });
380
-
381
- sendToChrome({
382
- type: "tool_request",
383
- id,
384
- tool: toolName,
385
- args,
386
- });
387
-
388
- setTimeout(() => {
389
- if (!pendingRequests.has(id)) return;
390
- pendingRequests.delete(id);
391
- reject(new Error("Tool execution timed out after 60 seconds"));
392
- }, 60000);
393
- });
394
- }
395
-
396
- // ============================================================================
397
- // Cleanup / Signals
398
- // ============================================================================
399
-
400
- // Soft release: do NOT exit the OpenCode process.
401
- process.on("SIGUSR1", () => {
402
- console.error(`[browser-plugin] SIGUSR1: releasing lock + stopping server`);
403
- releaseLock();
404
- stopBrowserServer();
405
- });
406
-
407
- process.on("SIGTERM", () => {
408
- releaseLock();
409
- stopBrowserServer();
410
- process.exit(0);
411
- });
412
-
413
- process.on("SIGINT", () => {
414
- releaseLock();
415
- stopBrowserServer();
416
- process.exit(0);
417
- });
418
-
419
- process.on("exit", () => {
420
- releaseLock();
421
- });
422
-
423
- // ============================================================================
424
- // Plugin Export
425
- // ============================================================================
426
-
427
- export const BrowserPlugin: Plugin = async (_ctx) => {
428
- console.error(`[browser-plugin] Initializing (session ${sessionId})`);
429
-
430
- return {
431
- tool: {
432
- browser_status: tool({
433
- description:
434
- "Check if browser is available or locked by another session. Returns connection status and lock info.",
435
- args: {},
436
- async execute() {
437
- const lock = readLock();
438
-
439
- if (!lock) {
440
- return "Browser available (no active session)";
441
- }
442
-
443
- if (lock.sessionId === sessionId) {
444
- return (
445
- `Browser connected (this session)\n` +
446
- `PID: ${pid}\n` +
447
- `Started: ${lock.startedAt}\n` +
448
- `Last used: ${lock.lastUsedAt}\n` +
449
- `Extension: ${isConnected ? "connected" : "not connected"}`
450
- );
451
- }
452
-
453
- const alive = isProcessAlive(lock.pid);
454
- const ageMinutes = Math.round(lockAgeMs(lock) / 60000);
455
- const expired = isLockExpired(lock);
456
-
457
- if (!alive) {
458
- return `Browser available (stale lock from dead PID ${lock.pid} will be auto-cleaned on next command)`;
459
- }
460
-
461
- return (
462
- `Browser locked by another session\n` +
463
- `PID: ${lock.pid}\n` +
464
- `Session: ${lock.sessionId}\n` +
465
- `Started: ${lock.startedAt}\n` +
466
- `Last used: ${lock.lastUsedAt} (~${ageMinutes}m ago)${expired ? " [expired]" : ""}\n` +
467
- `Working directory: ${lock.cwd}\n\n` +
468
- `Use browser_takeover to request release (no kill), or browser_force_kill_session as last resort.`
469
- );
470
- },
471
- }),
472
-
473
- browser_release: tool({
474
- description: "Release browser lock and stop the server for this session.",
475
- args: {},
476
- async execute() {
477
- const lock = readLock();
478
- if (lock && lock.sessionId !== sessionId) {
479
- throw new Error("This session does not own the browser lock.");
480
- }
481
-
482
- releaseLock();
483
- stopBrowserServer();
484
- return "Released browser lock for this session.";
485
- },
486
- }),
487
-
488
- browser_takeover: tool({
489
- description:
490
- "Request the session holding the browser lock to release it (no process kill), then take over.",
491
- args: {},
492
- async execute() {
493
- return await performTakeover();
494
- },
495
- }),
496
-
497
- browser_kill_session: tool({
498
- description:
499
- "(Deprecated name) Soft takeover without killing OpenCode. Prefer browser_takeover.",
500
- args: {},
501
- async execute() {
502
- // Keep backward compatibility: old callers use this.
503
- return await performTakeover();
504
- },
505
- }),
506
-
507
- browser_force_kill_session: tool({
508
- description: "Force kill the session holding the browser lock (last resort).",
509
- args: {},
510
- async execute() {
511
- const lock = readLock();
512
-
513
- if (!lock) {
514
- writeLock();
515
- return "No active session. Browser now connected to this session.";
516
- }
517
-
518
- if (lock.sessionId === sessionId) {
519
- return "This session already owns the browser.";
520
- }
521
-
522
- if (!isProcessAlive(lock.pid)) {
523
- writeLock();
524
- return `Cleaned stale lock (PID ${lock.pid} was dead). Browser now connected to this session.`;
525
- }
526
-
527
- const result = await forceKillSession(lock.pid);
528
- if (!result.success) {
529
- throw new Error(`Failed to force kill session: ${result.error}`);
530
- }
531
-
532
- // Best-effort cleanup; then take lock.
533
- try {
534
- unlinkSync(LOCK_FILE);
535
- } catch {
536
- // Ignore
537
- }
538
-
539
- writeLock();
540
-
541
- if (!server) {
542
- if (!startServer()) {
543
- throw new Error("Failed to start WebSocket server after force kill.");
544
- }
545
- }
546
-
547
- const ok = await waitForExtensionConnection(3000);
548
- if (!ok) {
549
- throw new Error("Force-killed lock holder but Chrome extension did not connect.");
550
- }
551
-
552
- return `Force-killed session ${lock.sessionId} (PID ${lock.pid}). Browser now connected to this session.`;
553
- },
554
- }),
555
-
556
- browser_navigate: tool({
557
- description: "Navigate to a URL in browser",
558
- args: {
559
- url: tool.schema.string({ description: "The URL to navigate to" }),
560
- tabId: tool.schema.optional(tool.schema.number({ description: "Optional tab ID" })),
561
- },
562
- async execute(args) {
563
- return await executeCommand("navigate", args);
564
- },
565
- }),
566
-
567
- browser_click: tool({
568
- description: "Click an element on page using a CSS selector",
569
- args: {
570
- selector: tool.schema.string({ description: "CSS selector for element to click" }),
571
- tabId: tool.schema.optional(tool.schema.number({ description: "Optional tab ID" })),
572
- },
573
- async execute(args) {
574
- return await executeCommand("click", args);
575
- },
576
- }),
577
-
578
- browser_type: tool({
579
- description: "Type text into an input element",
580
- args: {
581
- selector: tool.schema.string({ description: "CSS selector for input element" }),
582
- text: tool.schema.string({ description: "Text to type" }),
583
- clear: tool.schema.optional(tool.schema.boolean({ description: "Clear field before typing" })),
584
- tabId: tool.schema.optional(tool.schema.number({ description: "Optional tab ID" })),
585
- },
586
- async execute(args) {
587
- return await executeCommand("type", args);
588
- },
589
- }),
590
-
591
- browser_screenshot: tool({
592
- description: "Take a screenshot of the current page. Saves to ~/.opencode-browser/screenshots/",
593
- args: {
594
- tabId: tool.schema.optional(tool.schema.number({ description: "Optional tab ID" })),
595
- name: tool.schema.optional(
596
- tool.schema.string({ description: "Optional name for screenshot file (without extension)" })
597
- ),
598
- },
599
- async execute(args: { tabId?: number; name?: string }) {
600
- const result = await executeCommand("screenshot", args);
601
-
602
- if (result && typeof result === "string" && result.startsWith("data:image")) {
603
- const base64Data = result.replace(/^data:image\/\w+;base64,/, "");
604
- const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
605
- const filename = args.name ? `${args.name}.png` : `screenshot-${timestamp}.png`;
606
- const filepath = join(SCREENSHOTS_DIR, filename);
607
-
608
- writeFileSync(filepath, Buffer.from(base64Data, "base64"));
609
- return `Screenshot saved: ${filepath}`;
610
- }
611
-
612
- return result;
613
- },
614
- }),
615
-
616
- browser_snapshot: tool({
617
- description:
618
- "Get an accessibility tree snapshot of the page. Returns interactive elements with selectors for clicking.",
619
- args: {
620
- tabId: tool.schema.optional(tool.schema.number({ description: "Optional tab ID" })),
621
- },
622
- async execute(args) {
623
- return await executeCommand("snapshot", args);
624
- },
625
- }),
626
-
627
- browser_get_tabs: tool({
628
- description: "List all open browser tabs",
629
- args: {},
630
- async execute() {
631
- return await executeCommand("get_tabs", {});
632
- },
633
- }),
634
-
635
- browser_scroll: tool({
636
- description: "Scroll the page or scroll an element into view",
637
- args: {
638
- selector: tool.schema.optional(tool.schema.string({ description: "CSS selector to scroll into view" })),
639
- x: tool.schema.optional(tool.schema.number({ description: "Horizontal scroll amount in pixels" })),
640
- y: tool.schema.optional(tool.schema.number({ description: "Vertical scroll amount in pixels" })),
641
- tabId: tool.schema.optional(tool.schema.number({ description: "Optional tab ID" })),
642
- },
643
- async execute(args) {
644
- return await executeCommand("scroll", args);
645
- },
646
- }),
647
-
648
- browser_wait: tool({
649
- description: "Wait for a specified duration",
650
- args: {
651
- ms: tool.schema.optional(tool.schema.number({ description: "Milliseconds to wait (default: 1000)" })),
652
- },
653
- async execute(args) {
654
- return await executeCommand("wait", args);
655
- },
656
- }),
657
-
658
- browser_execute: tool({
659
- description: "Execute JavaScript code in the page context and return the result",
660
- args: {
661
- code: tool.schema.string({ description: "JavaScript code to execute" }),
662
- tabId: tool.schema.optional(tool.schema.number({ description: "Optional tab ID" })),
663
- },
664
- async execute(args) {
665
- return await executeCommand("execute_script", args);
666
- },
667
- }),
668
- },
669
- };
670
- };
671
-
672
- export default BrowserPlugin;