agent-browser 0.0.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.
Files changed (54) hide show
  1. package/.prettierrc +7 -0
  2. package/LICENSE +201 -0
  3. package/README.md +274 -1
  4. package/bin/agent-browser +2 -0
  5. package/dist/actions.d.ts +7 -0
  6. package/dist/actions.d.ts.map +1 -0
  7. package/dist/actions.js +1138 -0
  8. package/dist/actions.js.map +1 -0
  9. package/dist/browser.d.ts +232 -0
  10. package/dist/browser.d.ts.map +1 -0
  11. package/dist/browser.js +477 -0
  12. package/dist/browser.js.map +1 -0
  13. package/dist/browser.test.d.ts +2 -0
  14. package/dist/browser.test.d.ts.map +1 -0
  15. package/dist/browser.test.js +136 -0
  16. package/dist/browser.test.js.map +1 -0
  17. package/dist/client.d.ts +17 -0
  18. package/dist/client.d.ts.map +1 -0
  19. package/dist/client.js +133 -0
  20. package/dist/client.js.map +1 -0
  21. package/dist/daemon.d.ts +29 -0
  22. package/dist/daemon.d.ts.map +1 -0
  23. package/dist/daemon.js +165 -0
  24. package/dist/daemon.js.map +1 -0
  25. package/dist/index.d.ts +3 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +1158 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/protocol.d.ts +26 -0
  30. package/dist/protocol.d.ts.map +1 -0
  31. package/dist/protocol.js +717 -0
  32. package/dist/protocol.js.map +1 -0
  33. package/dist/protocol.test.d.ts +2 -0
  34. package/dist/protocol.test.d.ts.map +1 -0
  35. package/dist/protocol.test.js +176 -0
  36. package/dist/protocol.test.js.map +1 -0
  37. package/dist/types.d.ts +604 -0
  38. package/dist/types.d.ts.map +1 -0
  39. package/dist/types.js +2 -0
  40. package/dist/types.js.map +1 -0
  41. package/package.json +39 -8
  42. package/scripts/postinstall.js +17 -0
  43. package/src/actions.ts +1658 -0
  44. package/src/browser.test.ts +157 -0
  45. package/src/browser.ts +586 -0
  46. package/src/client.ts +150 -0
  47. package/src/daemon.ts +187 -0
  48. package/src/index.ts +1180 -0
  49. package/src/protocol.test.ts +216 -0
  50. package/src/protocol.ts +848 -0
  51. package/src/types.ts +913 -0
  52. package/tsconfig.json +19 -0
  53. package/vitest.config.ts +9 -0
  54. package/index.js +0 -2
package/src/index.ts ADDED
@@ -0,0 +1,1180 @@
1
+ #!/usr/bin/env node
2
+ import * as fs from 'fs';
3
+ import * as os from 'os';
4
+ import * as path from 'path';
5
+ import { execSync, spawnSync } from 'child_process';
6
+ import { send, setDebug, setSession, getSession } from './client.js';
7
+ import type { Response } from './types.js';
8
+
9
+ // ============================================================================
10
+ // System Dependencies Installation
11
+ // ============================================================================
12
+
13
+ // Common dependencies needed for Playwright browsers on Linux
14
+ const LINUX_DEPS = {
15
+ // Shared libraries for Chromium/Firefox/WebKit
16
+ apt: [
17
+ 'libxcb-shm0',
18
+ 'libx11-xcb1',
19
+ 'libx11-6',
20
+ 'libxcb1',
21
+ 'libxext6',
22
+ 'libxrandr2',
23
+ 'libxcomposite1',
24
+ 'libxcursor1',
25
+ 'libxdamage1',
26
+ 'libxfixes3',
27
+ 'libxi6',
28
+ 'libgtk-3-0',
29
+ 'libpangocairo-1.0-0',
30
+ 'libpango-1.0-0',
31
+ 'libatk1.0-0',
32
+ 'libcairo-gobject2',
33
+ 'libcairo2',
34
+ 'libgdk-pixbuf-2.0-0',
35
+ 'libxrender1',
36
+ 'libasound2',
37
+ 'libfreetype6',
38
+ 'libfontconfig1',
39
+ 'libdbus-1-3',
40
+ 'libnss3',
41
+ 'libnspr4',
42
+ 'libatk-bridge2.0-0',
43
+ 'libdrm2',
44
+ 'libxkbcommon0',
45
+ 'libatspi2.0-0',
46
+ 'libcups2',
47
+ 'libxshmfence1',
48
+ 'libgbm1',
49
+ ],
50
+ dnf: [
51
+ 'libxcb',
52
+ 'libX11-xcb',
53
+ 'libX11',
54
+ 'libXext',
55
+ 'libXrandr',
56
+ 'libXcomposite',
57
+ 'libXcursor',
58
+ 'libXdamage',
59
+ 'libXfixes',
60
+ 'libXi',
61
+ 'gtk3',
62
+ 'pango',
63
+ 'atk',
64
+ 'cairo-gobject',
65
+ 'cairo',
66
+ 'gdk-pixbuf2',
67
+ 'libXrender',
68
+ 'alsa-lib',
69
+ 'freetype',
70
+ 'fontconfig',
71
+ 'dbus-libs',
72
+ 'nss',
73
+ 'nspr',
74
+ 'at-spi2-atk',
75
+ 'libdrm',
76
+ 'libxkbcommon',
77
+ 'at-spi2-core',
78
+ 'cups-libs',
79
+ 'libxshmfence',
80
+ 'mesa-libgbm',
81
+ 'libwayland-client',
82
+ 'libwayland-server',
83
+ ],
84
+ yum: [
85
+ 'libxcb',
86
+ 'libX11-xcb',
87
+ 'libX11',
88
+ 'libXext',
89
+ 'libXrandr',
90
+ 'libXcomposite',
91
+ 'libXcursor',
92
+ 'libXdamage',
93
+ 'libXfixes',
94
+ 'libXi',
95
+ 'gtk3',
96
+ 'pango',
97
+ 'atk',
98
+ 'cairo-gobject',
99
+ 'cairo',
100
+ 'gdk-pixbuf2',
101
+ 'libXrender',
102
+ 'alsa-lib',
103
+ 'freetype',
104
+ 'fontconfig',
105
+ 'dbus-libs',
106
+ 'nss',
107
+ 'nspr',
108
+ 'at-spi2-atk',
109
+ 'libdrm',
110
+ 'libxkbcommon',
111
+ 'at-spi2-core',
112
+ 'cups-libs',
113
+ 'libxshmfence',
114
+ 'mesa-libgbm',
115
+ ],
116
+ };
117
+
118
+ function detectPackageManager(): 'apt' | 'dnf' | 'yum' | null {
119
+ const managers = ['apt-get', 'dnf', 'yum'] as const;
120
+ for (const mgr of managers) {
121
+ try {
122
+ execSync(`which ${mgr}`, { stdio: 'ignore' });
123
+ return mgr === 'apt-get' ? 'apt' : mgr;
124
+ } catch {
125
+ // Not found, try next
126
+ }
127
+ }
128
+ return null;
129
+ }
130
+
131
+ function installSystemDeps(): void {
132
+ if (os.platform() !== 'linux') {
133
+ console.log('System dependency installation is only needed on Linux');
134
+ return;
135
+ }
136
+
137
+ const pkgMgr = detectPackageManager();
138
+ if (!pkgMgr) {
139
+ throw new Error('No supported package manager found (apt-get, dnf, or yum)');
140
+ }
141
+
142
+ const deps = LINUX_DEPS[pkgMgr];
143
+ if (!deps || deps.length === 0) {
144
+ throw new Error(`No dependencies defined for package manager: ${pkgMgr}`);
145
+ }
146
+
147
+ console.log(`Detected package manager: ${pkgMgr}`);
148
+ console.log(`Installing ${deps.length} dependencies...`);
149
+
150
+ let cmd: string;
151
+ switch (pkgMgr) {
152
+ case 'apt':
153
+ cmd = `apt-get update && apt-get install -y ${deps.join(' ')}`;
154
+ break;
155
+ case 'dnf':
156
+ cmd = `dnf install -y ${deps.join(' ')}`;
157
+ break;
158
+ case 'yum':
159
+ cmd = `yum install -y ${deps.join(' ')}`;
160
+ break;
161
+ }
162
+
163
+ // Run with sudo if not root
164
+ const isRoot = process.getuid?.() === 0;
165
+ if (!isRoot) {
166
+ cmd = `sudo ${cmd}`;
167
+ }
168
+
169
+ execSync(cmd, { stdio: 'inherit' });
170
+ }
171
+
172
+ // ============================================================================
173
+ // Utilities
174
+ // ============================================================================
175
+
176
+ function listSessions(): string[] {
177
+ const tmpDir = os.tmpdir();
178
+ try {
179
+ const files = fs.readdirSync(tmpDir);
180
+ const sessions: string[] = [];
181
+ for (const file of files) {
182
+ const match = file.match(/^agent-browser-(.+)\.pid$/);
183
+ if (match) {
184
+ const pidFile = path.join(tmpDir, file);
185
+ try {
186
+ const pid = parseInt(fs.readFileSync(pidFile, 'utf8').trim(), 10);
187
+ process.kill(pid, 0);
188
+ sessions.push(match[1]);
189
+ } catch {
190
+ /* Process not running */
191
+ }
192
+ }
193
+ }
194
+ return sessions;
195
+ } catch {
196
+ return [];
197
+ }
198
+ }
199
+
200
+ const colors = {
201
+ reset: '\x1b[0m',
202
+ bold: '\x1b[1m',
203
+ dim: '\x1b[2m',
204
+ red: '\x1b[31m',
205
+ green: '\x1b[32m',
206
+ yellow: '\x1b[33m',
207
+ cyan: '\x1b[36m',
208
+ };
209
+
210
+ const c = (color: keyof typeof colors, text: string) => `${colors[color]}${text}${colors.reset}`;
211
+
212
+ function genId(): string {
213
+ return Math.random().toString(36).slice(2, 10);
214
+ }
215
+
216
+ function err(msg: string): never {
217
+ console.error(c('red', 'Error:'), msg);
218
+ process.exit(1);
219
+ }
220
+
221
+ // ============================================================================
222
+ // Help
223
+ // ============================================================================
224
+
225
+ function printHelp(): void {
226
+ console.log(`
227
+ ${c('bold', 'agent-browser')} - headless browser automation for AI agents
228
+
229
+ ${c('yellow', 'Usage:')} agent-browser <command> [options]
230
+
231
+ ${c('yellow', 'Core Commands:')}
232
+ ${c('cyan', 'open')} <url> Navigate to URL
233
+ ${c('cyan', 'click')} <sel> Click element
234
+ ${c('cyan', 'type')} <sel> <text> Type into element
235
+ ${c('cyan', 'fill')} <sel> <text> Clear and fill
236
+ ${c('cyan', 'press')} <key> Press key (Enter, Tab, Control+a)
237
+ ${c('cyan', 'hover')} <sel> Hover element
238
+ ${c('cyan', 'select')} <sel> <val> Select dropdown option
239
+ ${c('cyan', 'scroll')} <dir> [px] Scroll (up/down/left/right)
240
+ ${c('cyan', 'wait')} <sel|ms> Wait for element or time
241
+ ${c('cyan', 'screenshot')} [path] Take screenshot
242
+ ${c('cyan', 'snapshot')} Accessibility tree (for AI)
243
+ ${c('cyan', 'eval')} <js> Run JavaScript
244
+ ${c('cyan', 'close')} Close browser
245
+
246
+ ${c('yellow', 'Get Info:')} agent-browser get <what> [selector]
247
+ text, html, value, attr, title, url, count, box
248
+
249
+ ${c('yellow', 'Check State:')} agent-browser is <what> <selector>
250
+ visible, enabled, checked
251
+
252
+ ${c('yellow', 'Find Elements:')} agent-browser find <locator> <action> [value]
253
+ role, text, label, placeholder, alt, title, testid, first, last, nth
254
+
255
+ ${c('yellow', 'Mouse:')} agent-browser mouse <action> [args]
256
+ move <x> <y>, down, up, wheel <dy>
257
+
258
+ ${c('yellow', 'Storage:')}
259
+ ${c('cyan', 'cookies')} [get|set|clear] Manage cookies
260
+ ${c('cyan', 'storage')} <local|session> Manage web storage
261
+
262
+ ${c('yellow', 'Browser:')} agent-browser set <setting> [value]
263
+ viewport, device, geo, offline, headers, credentials
264
+
265
+ ${c('yellow', 'Network:')} agent-browser network <action>
266
+ route, unroute, requests
267
+
268
+ ${c('yellow', 'Tabs:')}
269
+ ${c('cyan', 'tab')} [new|list|close|<n>] Manage tabs
270
+
271
+ ${c('yellow', 'Debug:')}
272
+ ${c('cyan', 'trace')} start|stop <path> Record trace
273
+ ${c('cyan', 'console')} View console logs
274
+ ${c('cyan', 'errors')} View page errors
275
+
276
+ ${c('yellow', 'Setup:')}
277
+ ${c('cyan', 'install')} Install browser binaries
278
+ ${c('cyan', 'install')} --with-deps Also install system dependencies (Linux)
279
+
280
+ ${c('yellow', 'Options:')}
281
+ --session <name> Isolated session (or AGENT_BROWSER_SESSION env)
282
+ --json JSON output
283
+ --full, -f Full page screenshot
284
+ --headed Show browser window (not headless)
285
+ --debug Debug output
286
+
287
+ ${c('yellow', 'Examples:')}
288
+ agent-browser open example.com
289
+ agent-browser click "#submit"
290
+ agent-browser fill "#email" "test@example.com"
291
+ agent-browser get text "h1"
292
+ agent-browser is visible ".modal"
293
+ agent-browser find role button click --name Submit
294
+ agent-browser wait 2000
295
+ agent-browser wait --load networkidle
296
+ `);
297
+ }
298
+
299
+ // ============================================================================
300
+ // Response Printing
301
+ // ============================================================================
302
+
303
+ function printResponse(response: Response, jsonMode: boolean): void {
304
+ if (jsonMode) {
305
+ console.log(JSON.stringify(response));
306
+ return;
307
+ }
308
+
309
+ if (!response.success) {
310
+ console.error(c('red', '✗ Error:'), response.error);
311
+ process.exit(1);
312
+ }
313
+
314
+ const data = response.data as Record<string, unknown>;
315
+
316
+ if (data.url && data.title) {
317
+ console.log(c('green', '✓'), c('bold', data.title as string));
318
+ console.log(c('dim', ` ${data.url}`));
319
+ } else if (data.text !== undefined) {
320
+ console.log(data.text ?? c('dim', 'null'));
321
+ } else if (data.html !== undefined) {
322
+ console.log(data.html);
323
+ } else if (data.value !== undefined) {
324
+ console.log(data.value ?? c('dim', 'null'));
325
+ } else if (data.result !== undefined) {
326
+ const result = data.result;
327
+ console.log(typeof result === 'object' ? JSON.stringify(result, null, 2) : result);
328
+ } else if (data.snapshot) {
329
+ console.log(data.snapshot);
330
+ } else if (data.visible !== undefined) {
331
+ console.log(data.visible ? c('green', 'true') : c('red', 'false'));
332
+ } else if (data.enabled !== undefined) {
333
+ console.log(data.enabled ? c('green', 'true') : c('red', 'false'));
334
+ } else if (data.checked !== undefined) {
335
+ console.log(data.checked ? c('green', 'true') : c('red', 'false'));
336
+ } else if (data.count !== undefined) {
337
+ console.log(data.count);
338
+ } else if (data.box) {
339
+ const box = data.box as { x: number; y: number; width: number; height: number };
340
+ console.log(`x:${box.x} y:${box.y} w:${box.width} h:${box.height}`);
341
+ } else if (data.url) {
342
+ console.log(data.url);
343
+ } else if (data.title) {
344
+ console.log(data.title);
345
+ } else if (data.base64) {
346
+ console.log(c('green', '✓'), 'Screenshot captured');
347
+ } else if (data.path) {
348
+ console.log(c('green', '✓'), `Saved: ${data.path}`);
349
+ } else if (data.cookies) {
350
+ const cookies = data.cookies as Array<{ name: string; value: string }>;
351
+ if (cookies.length === 0) console.log(c('dim', 'No cookies'));
352
+ else cookies.forEach((ck) => console.log(`${c('cyan', ck.name)}: ${ck.value}`));
353
+ } else if (data.tabs) {
354
+ const tabs = data.tabs as Array<{ index: number; url: string; title: string; active: boolean }>;
355
+ tabs.forEach((t) => {
356
+ const marker = t.active ? c('green', '→') : ' ';
357
+ console.log(`${marker} [${t.index}] ${t.title || c('dim', '(untitled)')}`);
358
+ if (t.url) console.log(c('dim', ` ${t.url}`));
359
+ });
360
+ } else if (data.index !== undefined && data.total !== undefined) {
361
+ console.log(c('green', '✓'), `Tab ${data.index} (${data.total} total)`);
362
+ } else if (data.messages) {
363
+ const msgs = data.messages as Array<{ type: string; text: string }>;
364
+ if (msgs.length === 0) console.log(c('dim', 'No messages'));
365
+ else
366
+ msgs.forEach((m) => {
367
+ const col = m.type === 'error' ? 'red' : m.type === 'warning' ? 'yellow' : 'dim';
368
+ console.log(`${c(col, `[${m.type}]`)} ${m.text}`);
369
+ });
370
+ } else if (data.errors) {
371
+ const errs = data.errors as Array<{ message: string }>;
372
+ if (errs.length === 0) console.log(c('dim', 'No errors'));
373
+ else errs.forEach((e) => console.log(c('red', '✗'), e.message));
374
+ } else if (data.requests) {
375
+ const reqs = data.requests as Array<{ method: string; url: string }>;
376
+ if (reqs.length === 0) console.log(c('dim', 'No requests'));
377
+ else reqs.forEach((r) => console.log(`${c('cyan', r.method)} ${r.url}`));
378
+ } else if (data.moved) {
379
+ console.log(c('green', '✓'), `Moved to (${data.x}, ${data.y})`);
380
+ } else if (data.body !== undefined && data.status !== undefined) {
381
+ // Response body
382
+ console.log(c('green', '✓'), `${data.status} ${data.url}`);
383
+ console.log(typeof data.body === 'object' ? JSON.stringify(data.body, null, 2) : data.body);
384
+ } else if (data.filename) {
385
+ // Download
386
+ console.log(c('green', '✓'), `Downloaded: ${data.filename}`);
387
+ console.log(c('dim', ` Path: ${data.path}`));
388
+ } else if (data.inserted) {
389
+ console.log(c('green', '✓'), 'Text inserted');
390
+ } else if (data.key) {
391
+ console.log(c('green', '✓'), `Key ${data.down ? 'down' : 'up'}: ${data.key}`);
392
+ } else if (data.note) {
393
+ console.log(c('yellow', '⚠'), data.note);
394
+ } else if (data.closed === true) {
395
+ console.log(c('green', '✓'), 'Browser closed');
396
+ } else if (data.launched) {
397
+ console.log(c('green', '✓'), 'Browser launched');
398
+ } else if (data.state) {
399
+ console.log(c('green', '✓'), `Load state: ${data.state}`);
400
+ } else if (
401
+ Object.keys(data).some((k) =>
402
+ [
403
+ 'clicked',
404
+ 'typed',
405
+ 'filled',
406
+ 'pressed',
407
+ 'hovered',
408
+ 'scrolled',
409
+ 'selected',
410
+ 'waited',
411
+ 'checked',
412
+ 'unchecked',
413
+ 'focused',
414
+ 'set',
415
+ 'cleared',
416
+ 'started',
417
+ 'down',
418
+ 'up',
419
+ ].includes(k)
420
+ )
421
+ ) {
422
+ console.log(c('green', '✓'), 'Done');
423
+ } else {
424
+ console.log(c('green', '✓'), JSON.stringify(data));
425
+ }
426
+ }
427
+
428
+ // ============================================================================
429
+ // Command Handlers
430
+ // ============================================================================
431
+
432
+ async function handleGet(args: string[], id: string): Promise<Record<string, unknown>> {
433
+ const what = args[0];
434
+ const selector = args[1];
435
+
436
+ switch (what) {
437
+ case 'text':
438
+ if (!selector) err('Selector required: agent-browser get text <selector>');
439
+ return { id, action: 'gettext', selector };
440
+ case 'html':
441
+ if (!selector) err('Selector required: agent-browser get html <selector>');
442
+ return { id, action: 'innerhtml', selector };
443
+ case 'value':
444
+ if (!selector) err('Selector required: agent-browser get value <selector>');
445
+ return { id, action: 'inputvalue', selector };
446
+ case 'attr':
447
+ if (!selector || !args[2]) err('Usage: agent-browser get attr <selector> <attribute>');
448
+ return { id, action: 'getattribute', selector, attribute: args[2] };
449
+ case 'title':
450
+ return { id, action: 'title' };
451
+ case 'url':
452
+ return { id, action: 'url' };
453
+ case 'count':
454
+ if (!selector) err('Selector required: agent-browser get count <selector>');
455
+ return { id, action: 'count', selector };
456
+ case 'box':
457
+ if (!selector) err('Selector required: agent-browser get box <selector>');
458
+ return { id, action: 'boundingbox', selector };
459
+ default:
460
+ err(`Unknown: agent-browser get ${what}. Options: text, html, value, attr, title, url, count, box`);
461
+ }
462
+ }
463
+
464
+ async function handleIs(args: string[], id: string): Promise<Record<string, unknown>> {
465
+ const what = args[0];
466
+ const selector = args[1];
467
+
468
+ if (!selector) err(`Selector required: agent-browser is ${what} <selector>`);
469
+
470
+ switch (what) {
471
+ case 'visible':
472
+ return { id, action: 'isvisible', selector };
473
+ case 'enabled':
474
+ return { id, action: 'isenabled', selector };
475
+ case 'checked':
476
+ return { id, action: 'ischecked', selector };
477
+ default:
478
+ err(`Unknown: agent-browser is ${what}. Options: visible, enabled, checked`);
479
+ }
480
+ }
481
+
482
+ async function handleFind(
483
+ args: string[],
484
+ id: string,
485
+ flags: Flags
486
+ ): Promise<Record<string, unknown>> {
487
+ const locator = args[0];
488
+ const value = args[1];
489
+ const subaction = args[2] || 'click';
490
+ const fillValue = args[3];
491
+
492
+ if (!value) err(`Value required: agent-browser find ${locator} <value> <action>`);
493
+
494
+ const exact = flags.exact;
495
+ const name = flags.name;
496
+
497
+ switch (locator) {
498
+ case 'role':
499
+ return { id, action: 'getbyrole', role: value, subaction, value: fillValue, name, exact };
500
+ case 'text':
501
+ return { id, action: 'getbytext', text: value, subaction, exact };
502
+ case 'label':
503
+ return { id, action: 'getbylabel', label: value, subaction, value: fillValue, exact };
504
+ case 'placeholder':
505
+ return {
506
+ id,
507
+ action: 'getbyplaceholder',
508
+ placeholder: value,
509
+ subaction,
510
+ value: fillValue,
511
+ exact,
512
+ };
513
+ case 'alt':
514
+ return { id, action: 'getbyalttext', text: value, subaction, exact };
515
+ case 'title':
516
+ return { id, action: 'getbytitle', text: value, subaction, exact };
517
+ case 'testid':
518
+ return { id, action: 'getbytestid', testId: value, subaction, value: fillValue };
519
+ case 'first':
520
+ return { id, action: 'nth', selector: value, index: 0, subaction, value: fillValue };
521
+ case 'last':
522
+ return { id, action: 'nth', selector: value, index: -1, subaction, value: fillValue };
523
+ case 'nth': {
524
+ const idx = parseInt(value, 10);
525
+ const sel = args[2];
526
+ const act = args[3] || 'click';
527
+ const val = args[4];
528
+ if (isNaN(idx) || !sel) err('Usage: agent-browser find nth <index> <selector> <action>');
529
+ return { id, action: 'nth', selector: sel, index: idx, subaction: act, value: val };
530
+ }
531
+ default:
532
+ err(
533
+ `Unknown locator: ${locator}. Options: role, text, label, placeholder, alt, title, testid, first, last, nth`
534
+ );
535
+ }
536
+ }
537
+
538
+ async function handleMouse(args: string[], id: string): Promise<Record<string, unknown>> {
539
+ const action = args[0];
540
+
541
+ switch (action) {
542
+ case 'move': {
543
+ const x = parseInt(args[1], 10);
544
+ const y = parseInt(args[2], 10);
545
+ if (isNaN(x) || isNaN(y)) err('Usage: agent-browser mouse move <x> <y>');
546
+ return { id, action: 'mousemove', x, y };
547
+ }
548
+ case 'down':
549
+ return { id, action: 'mousedown', button: args[1] || 'left' };
550
+ case 'up':
551
+ return { id, action: 'mouseup', button: args[1] || 'left' };
552
+ case 'wheel': {
553
+ const dy = parseInt(args[1], 10) || 100;
554
+ const dx = parseInt(args[2], 10) || 0;
555
+ return { id, action: 'wheel', deltaY: dy, deltaX: dx };
556
+ }
557
+ default:
558
+ err(`Unknown: agent-browser mouse ${action}. Options: move, down, up, wheel`);
559
+ }
560
+ }
561
+
562
+ async function handleSet(args: string[], id: string): Promise<Record<string, unknown>> {
563
+ const setting = args[0];
564
+
565
+ switch (setting) {
566
+ case 'viewport': {
567
+ const w = parseInt(args[1], 10);
568
+ const h = parseInt(args[2], 10);
569
+ if (isNaN(w) || isNaN(h)) err('Usage: agent-browser set viewport <width> <height>');
570
+ return { id, action: 'viewport', width: w, height: h };
571
+ }
572
+ case 'device':
573
+ if (!args[1]) err('Usage: agent-browser set device <name>');
574
+ return { id, action: 'device', device: args[1] };
575
+ case 'geo':
576
+ case 'geolocation': {
577
+ const lat = parseFloat(args[1]);
578
+ const lng = parseFloat(args[2]);
579
+ if (isNaN(lat) || isNaN(lng)) err('Usage: agent-browser set geo <lat> <lng>');
580
+ return { id, action: 'geolocation', latitude: lat, longitude: lng };
581
+ }
582
+ case 'offline':
583
+ return { id, action: 'offline', offline: args[1] !== 'off' && args[1] !== 'false' };
584
+ case 'headers':
585
+ if (!args[1]) err('Usage: agent-browser set headers <json>');
586
+ try {
587
+ return { id, action: 'headers', headers: JSON.parse(args[1]) };
588
+ } catch {
589
+ err('Invalid JSON for headers');
590
+ }
591
+ break;
592
+ case 'credentials':
593
+ case 'auth':
594
+ if (!args[1] || !args[2]) err('Usage: agent-browser set credentials <user> <pass>');
595
+ return { id, action: 'credentials', username: args[1], password: args[2] };
596
+ case 'media': {
597
+ const colorScheme = args.includes('dark')
598
+ ? 'dark'
599
+ : args.includes('light')
600
+ ? 'light'
601
+ : undefined;
602
+ const media = args.includes('print')
603
+ ? 'print'
604
+ : args.includes('screen')
605
+ ? 'screen'
606
+ : undefined;
607
+ return { id, action: 'emulatemedia', colorScheme, media };
608
+ }
609
+ default:
610
+ err(
611
+ `Unknown: agent-browser set ${setting}. Options: viewport, device, geo, offline, headers, credentials, media`
612
+ );
613
+ }
614
+ return {};
615
+ }
616
+
617
+ async function handleNetwork(
618
+ args: string[],
619
+ id: string,
620
+ allArgs: string[]
621
+ ): Promise<Record<string, unknown>> {
622
+ const action = args[0];
623
+
624
+ switch (action) {
625
+ case 'route': {
626
+ const url = args[1];
627
+ if (!url) err('Usage: agent-browser network route <url> [--abort|--body <json>]');
628
+ const abort = allArgs.includes('--abort');
629
+ const bodyIdx = allArgs.indexOf('--body');
630
+ const body = bodyIdx !== -1 ? allArgs[bodyIdx + 1] : undefined;
631
+ return {
632
+ id,
633
+ action: 'route',
634
+ url,
635
+ abort,
636
+ response: body ? { body, contentType: 'application/json' } : undefined,
637
+ };
638
+ }
639
+ case 'unroute':
640
+ return { id, action: 'unroute', url: args[1] };
641
+ case 'requests': {
642
+ const clear = allArgs.includes('--clear');
643
+ const filterIdx = allArgs.indexOf('--filter');
644
+ const filter = filterIdx !== -1 ? allArgs[filterIdx + 1] : undefined;
645
+ return { id, action: 'requests', clear, filter };
646
+ }
647
+ default:
648
+ err(`Unknown: agent-browser network ${action}. Options: route, unroute, requests`);
649
+ }
650
+ return {};
651
+ }
652
+
653
+ async function handleStorage(args: string[], id: string): Promise<Record<string, unknown>> {
654
+ const type = args[0] as 'local' | 'session';
655
+ const sub = args[1];
656
+
657
+ if (type !== 'local' && type !== 'session') {
658
+ err('Usage: agent-browser storage <local|session> [get|set|clear] [key] [value]');
659
+ }
660
+
661
+ if (sub === 'set') {
662
+ if (!args[2] || !args[3]) err(`Usage: agent-browser storage ${type} set <key> <value>`);
663
+ return { id, action: 'storage_set', type, key: args[2], value: args[3] };
664
+ } else if (sub === 'clear') {
665
+ return { id, action: 'storage_clear', type };
666
+ } else {
667
+ // get (default)
668
+ return { id, action: 'storage_get', type, key: sub };
669
+ }
670
+ }
671
+
672
+ async function handleCookies(args: string[], id: string): Promise<Record<string, unknown>> {
673
+ const sub = args[0];
674
+
675
+ if (sub === 'set') {
676
+ if (!args[1]) err('Usage: agent-browser cookies set <json>');
677
+ try {
678
+ return { id, action: 'cookies_set', cookies: JSON.parse(args[1]) };
679
+ } catch {
680
+ err('Invalid JSON for cookies');
681
+ }
682
+ } else if (sub === 'clear') {
683
+ return { id, action: 'cookies_clear' };
684
+ } else {
685
+ return { id, action: 'cookies_get' };
686
+ }
687
+ return {};
688
+ }
689
+
690
+ async function handleTab(args: string[], id: string): Promise<Record<string, unknown>> {
691
+ const sub = args[0];
692
+
693
+ if (sub === 'new') {
694
+ return { id, action: 'tab_new' };
695
+ } else if (sub === 'list' || sub === 'ls' || !sub) {
696
+ return { id, action: 'tab_list' };
697
+ } else if (sub === 'close') {
698
+ const idx = args[1] !== undefined ? parseInt(args[1], 10) : undefined;
699
+ return { id, action: 'tab_close', index: idx };
700
+ } else {
701
+ const idx = parseInt(sub, 10);
702
+ if (isNaN(idx)) err(`Unknown: agent-browser tab ${sub}. Options: new, list, close, <index>`);
703
+ return { id, action: 'tab_switch', index: idx };
704
+ }
705
+ }
706
+
707
+ async function handleTrace(args: string[], id: string): Promise<Record<string, unknown>> {
708
+ const sub = args[0];
709
+
710
+ if (sub === 'start') {
711
+ return { id, action: 'trace_start', screenshots: true, snapshots: true };
712
+ } else if (sub === 'stop') {
713
+ if (!args[1]) err('Usage: agent-browser trace stop <path>');
714
+ return { id, action: 'trace_stop', path: args[1] };
715
+ } else {
716
+ err('Usage: agent-browser trace start|stop');
717
+ }
718
+ return {};
719
+ }
720
+
721
+ async function handleState(args: string[], id: string): Promise<Record<string, unknown>> {
722
+ const sub = args[0];
723
+ const path = args[1];
724
+
725
+ if (sub === 'save') {
726
+ if (!path) err('Usage: agent-browser state save <path>');
727
+ return { id, action: 'state_save', path };
728
+ } else if (sub === 'load') {
729
+ if (!path) err('Usage: agent-browser state load <path>');
730
+ return { id, action: 'state_load', path };
731
+ } else {
732
+ err('Usage: agent-browser state save|load <path>');
733
+ }
734
+ return {};
735
+ }
736
+
737
+ // ============================================================================
738
+ // Flags Parser
739
+ // ============================================================================
740
+
741
+ interface Flags {
742
+ json: boolean;
743
+ full: boolean;
744
+ text: boolean;
745
+ debug: boolean;
746
+ headed: boolean;
747
+ session: string;
748
+ selector?: string;
749
+ name?: string;
750
+ exact: boolean;
751
+ url?: string;
752
+ load?: string;
753
+ fn?: string;
754
+ }
755
+
756
+ function parseFlags(args: string[]): { flags: Flags; cleanArgs: string[] } {
757
+ const flags: Flags = {
758
+ json: false,
759
+ full: false,
760
+ text: false,
761
+ debug: false,
762
+ headed: false,
763
+ session: process.env.AGENT_BROWSER_SESSION || 'default',
764
+ exact: false,
765
+ };
766
+
767
+ const cleanArgs: string[] = [];
768
+ let i = 0;
769
+
770
+ while (i < args.length) {
771
+ const arg = args[i];
772
+
773
+ if (arg === '--json') {
774
+ flags.json = true;
775
+ } else if (arg === '--full' || arg === '-f') {
776
+ flags.full = true;
777
+ } else if (arg === '--text' || arg === '-t') {
778
+ flags.text = true;
779
+ } else if (arg === '--debug') {
780
+ flags.debug = true;
781
+ } else if (arg === '--headed' || arg === '--head') {
782
+ flags.headed = true;
783
+ } else if (arg === '--exact') {
784
+ flags.exact = true;
785
+ } else if (arg === '--session' && args[i + 1]) {
786
+ flags.session = args[++i];
787
+ } else if ((arg === '--selector' || arg === '-s') && args[i + 1]) {
788
+ flags.selector = args[++i];
789
+ } else if ((arg === '--name' || arg === '-n') && args[i + 1]) {
790
+ flags.name = args[++i];
791
+ } else if (arg === '--url' && args[i + 1]) {
792
+ flags.url = args[++i];
793
+ } else if (arg === '--load' && args[i + 1]) {
794
+ flags.load = args[++i];
795
+ } else if ((arg === '--fn' || arg === '--function') && args[i + 1]) {
796
+ flags.fn = args[++i];
797
+ } else if (!arg.startsWith('-')) {
798
+ cleanArgs.push(arg);
799
+ }
800
+ i++;
801
+ }
802
+
803
+ return { flags, cleanArgs };
804
+ }
805
+
806
+ // ============================================================================
807
+ // Main
808
+ // ============================================================================
809
+
810
+ async function main(): Promise<void> {
811
+ const rawArgs = process.argv.slice(2);
812
+ const { flags, cleanArgs } = parseFlags(rawArgs);
813
+
814
+ if (flags.debug) setDebug(true);
815
+ setSession(flags.session);
816
+
817
+ if (cleanArgs.length === 0 || rawArgs.includes('--help') || rawArgs.includes('-h')) {
818
+ printHelp();
819
+ process.exit(0);
820
+ }
821
+
822
+ const command = cleanArgs[0];
823
+ const args = cleanArgs.slice(1);
824
+ const id = genId();
825
+
826
+ let cmd: Record<string, unknown>;
827
+
828
+ switch (command) {
829
+ // === Core Commands ===
830
+ case 'open':
831
+ case 'goto':
832
+ case 'navigate': {
833
+ if (!args[0]) err('URL required');
834
+ const url = args[0].startsWith('http') ? args[0] : `https://${args[0]}`;
835
+ // If --headed, launch with headless=false first
836
+ if (flags.headed) {
837
+ await send({ id: genId(), action: 'launch', headless: false });
838
+ }
839
+ cmd = { id, action: 'navigate', url };
840
+ break;
841
+ }
842
+
843
+ case 'click':
844
+ if (!args[0]) err('Selector required');
845
+ cmd = { id, action: 'click', selector: args[0] };
846
+ break;
847
+
848
+ case 'dblclick':
849
+ if (!args[0]) err('Selector required');
850
+ cmd = { id, action: 'dblclick', selector: args[0] };
851
+ break;
852
+
853
+ case 'type':
854
+ if (!args[0] || !args[1]) err('Usage: agent-browser type <selector> <text>');
855
+ cmd = { id, action: 'type', selector: args[0], text: args.slice(1).join(' ') };
856
+ break;
857
+
858
+ case 'fill':
859
+ if (!args[0] || !args[1]) err('Usage: agent-browser fill <selector> <text>');
860
+ cmd = { id, action: 'fill', selector: args[0], value: args.slice(1).join(' ') };
861
+ break;
862
+
863
+ case 'press':
864
+ case 'key':
865
+ if (!args[0]) err('Key required');
866
+ cmd = { id, action: 'press', key: args[0] };
867
+ break;
868
+
869
+ case 'keydown':
870
+ if (!args[0]) err('Key required');
871
+ cmd = { id, action: 'keydown', key: args[0] };
872
+ break;
873
+
874
+ case 'keyup':
875
+ if (!args[0]) err('Key required');
876
+ cmd = { id, action: 'keyup', key: args[0] };
877
+ break;
878
+
879
+ case 'hover':
880
+ if (!args[0]) err('Selector required');
881
+ cmd = { id, action: 'hover', selector: args[0] };
882
+ break;
883
+
884
+ case 'focus':
885
+ if (!args[0]) err('Selector required');
886
+ cmd = { id, action: 'focus', selector: args[0] };
887
+ break;
888
+
889
+ case 'check':
890
+ if (!args[0]) err('Selector required');
891
+ cmd = { id, action: 'check', selector: args[0] };
892
+ break;
893
+
894
+ case 'uncheck':
895
+ if (!args[0]) err('Selector required');
896
+ cmd = { id, action: 'uncheck', selector: args[0] };
897
+ break;
898
+
899
+ case 'select':
900
+ if (!args[0] || !args[1]) err('Usage: agent-browser select <selector> <value>');
901
+ cmd = { id, action: 'select', selector: args[0], value: args[1] };
902
+ break;
903
+
904
+ case 'drag':
905
+ if (!args[0] || !args[1]) err('Usage: agent-browser drag <source> <target>');
906
+ cmd = { id, action: 'drag', source: args[0], target: args[1] };
907
+ break;
908
+
909
+ case 'upload':
910
+ if (!args[0] || !args[1]) err('Usage: agent-browser upload <selector> <files...>');
911
+ cmd = { id, action: 'upload', selector: args[0], files: args.slice(1) };
912
+ break;
913
+
914
+ case 'scroll': {
915
+ const dir = args[0] || 'down';
916
+ const amount = parseInt(args[1], 10) || 300;
917
+ cmd = { id, action: 'scroll', direction: dir, amount, selector: flags.selector };
918
+ break;
919
+ }
920
+
921
+ case 'wait': {
922
+ const target = args[0];
923
+ // Check for flags
924
+ if (flags.fn) {
925
+ cmd = { id, action: 'waitforfunction', expression: flags.fn };
926
+ } else if (flags.url) {
927
+ cmd = { id, action: 'waitforurl', url: flags.url };
928
+ } else if (flags.load) {
929
+ cmd = { id, action: 'waitforloadstate', state: flags.load };
930
+ } else if (flags.text) {
931
+ if (!target) err('Text required with --text flag');
932
+ cmd = { id, action: 'wait', text: target };
933
+ } else if (target && /^\d+$/.test(target)) {
934
+ cmd = { id, action: 'wait', timeout: parseInt(target, 10) };
935
+ } else if (target) {
936
+ cmd = { id, action: 'wait', selector: target };
937
+ } else {
938
+ err('Usage: agent-browser wait <selector|ms|--text|--url|--load|--fn>');
939
+ }
940
+ break;
941
+ }
942
+
943
+ case 'screenshot': {
944
+ const path = args[0];
945
+ cmd = { id, action: 'screenshot', path, fullPage: flags.full, selector: flags.selector };
946
+ break;
947
+ }
948
+
949
+ case 'pdf':
950
+ if (!args[0]) err('Path required');
951
+ cmd = { id, action: 'pdf', path: args[0] };
952
+ break;
953
+
954
+ case 'snapshot':
955
+ cmd = { id, action: 'snapshot' };
956
+ break;
957
+
958
+ case 'eval':
959
+ if (!args[0]) err('Script required');
960
+ cmd = { id, action: 'evaluate', script: args.join(' ') };
961
+ break;
962
+
963
+ case 'close':
964
+ case 'quit':
965
+ case 'exit':
966
+ cmd = { id, action: 'close' };
967
+ break;
968
+
969
+ // === Navigation ===
970
+ case 'back':
971
+ cmd = { id, action: 'back' };
972
+ break;
973
+
974
+ case 'forward':
975
+ cmd = { id, action: 'forward' };
976
+ break;
977
+
978
+ case 'reload':
979
+ cmd = { id, action: 'reload' };
980
+ break;
981
+
982
+ // === Grouped Commands ===
983
+ case 'get':
984
+ cmd = await handleGet(args, id);
985
+ break;
986
+
987
+ case 'is':
988
+ cmd = await handleIs(args, id);
989
+ break;
990
+
991
+ case 'find':
992
+ cmd = await handleFind(args, id, flags);
993
+ break;
994
+
995
+ case 'mouse':
996
+ cmd = await handleMouse(args, id);
997
+ break;
998
+
999
+ case 'set':
1000
+ cmd = await handleSet(args, id);
1001
+ break;
1002
+
1003
+ case 'network':
1004
+ cmd = await handleNetwork(args, id, rawArgs);
1005
+ break;
1006
+
1007
+ case 'storage':
1008
+ cmd = await handleStorage(args, id);
1009
+ break;
1010
+
1011
+ case 'cookies':
1012
+ cmd = await handleCookies(args, id);
1013
+ break;
1014
+
1015
+ case 'tab':
1016
+ cmd = await handleTab(args, id);
1017
+ break;
1018
+
1019
+ case 'window':
1020
+ if (args[0] === 'new') {
1021
+ cmd = { id, action: 'window_new' };
1022
+ } else {
1023
+ err('Usage: agent-browser window new');
1024
+ }
1025
+ break;
1026
+
1027
+ case 'frame':
1028
+ if (!args[0]) err('Selector required');
1029
+ if (args[0] === 'main') {
1030
+ cmd = { id, action: 'mainframe' };
1031
+ } else {
1032
+ cmd = { id, action: 'frame', selector: args[0] };
1033
+ }
1034
+ break;
1035
+
1036
+ case 'dialog':
1037
+ if (args[0] === 'accept') {
1038
+ cmd = { id, action: 'dialog', response: 'accept', promptText: args[1] };
1039
+ } else if (args[0] === 'dismiss') {
1040
+ cmd = { id, action: 'dialog', response: 'dismiss' };
1041
+ } else {
1042
+ err('Usage: agent-browser dialog accept|dismiss');
1043
+ }
1044
+ break;
1045
+
1046
+ case 'trace':
1047
+ cmd = await handleTrace(args, id);
1048
+ break;
1049
+
1050
+ case 'state':
1051
+ cmd = await handleState(args, id);
1052
+ break;
1053
+
1054
+ case 'console':
1055
+ cmd = { id, action: 'console', clear: rawArgs.includes('--clear') };
1056
+ break;
1057
+
1058
+ case 'errors':
1059
+ cmd = { id, action: 'errors', clear: rawArgs.includes('--clear') };
1060
+ break;
1061
+
1062
+ case 'highlight':
1063
+ if (!args[0]) err('Selector required');
1064
+ cmd = { id, action: 'highlight', selector: args[0] };
1065
+ break;
1066
+
1067
+ case 'scrollintoview':
1068
+ case 'scrollinto':
1069
+ if (!args[0]) err('Selector required');
1070
+ cmd = { id, action: 'scrollintoview', selector: args[0] };
1071
+ break;
1072
+
1073
+ case 'initscript':
1074
+ if (!args[0]) err('Script required');
1075
+ cmd = { id, action: 'addinitscript', script: args.join(' ') };
1076
+ break;
1077
+
1078
+ case 'inserttext':
1079
+ case 'insert':
1080
+ if (!args[0]) err('Text required');
1081
+ cmd = { id, action: 'inserttext', text: args.join(' ') };
1082
+ break;
1083
+
1084
+ case 'multiselect':
1085
+ if (!args[0] || args.length < 2)
1086
+ err('Usage: agent-browser multiselect <selector> <value1> [value2...]');
1087
+ cmd = { id, action: 'multiselect', selector: args[0], values: args.slice(1) };
1088
+ break;
1089
+
1090
+ case 'download':
1091
+ cmd = { id, action: 'waitfordownload', path: args[0] };
1092
+ break;
1093
+
1094
+ case 'response':
1095
+ if (!args[0]) err('URL pattern required');
1096
+ cmd = { id, action: 'responsebody', url: args[0] };
1097
+ break;
1098
+
1099
+ case 'session':
1100
+ if (args[0] === 'list' || args[0] === 'ls') {
1101
+ const sessions = listSessions();
1102
+ const current = getSession();
1103
+ if (sessions.length === 0) {
1104
+ console.log(c('dim', 'No active sessions'));
1105
+ } else {
1106
+ sessions.forEach((s) => {
1107
+ const marker = s === current ? c('green', '→') : ' ';
1108
+ console.log(`${marker} ${c('cyan', s)}`);
1109
+ });
1110
+ }
1111
+ process.exit(0);
1112
+ } else {
1113
+ console.log(c('cyan', getSession()));
1114
+ process.exit(0);
1115
+ }
1116
+
1117
+ case 'install': {
1118
+ const withDeps = rawArgs.includes('--with-deps') || rawArgs.includes('-d');
1119
+
1120
+ // Install system dependencies first if requested
1121
+ if (withDeps) {
1122
+ console.log(c('cyan', 'Installing system dependencies...'));
1123
+ try {
1124
+ installSystemDeps();
1125
+ console.log(c('green', '✓'), 'System dependencies installed');
1126
+ } catch (error) {
1127
+ const msg = error instanceof Error ? error.message : String(error);
1128
+ console.error(c('red', '✗'), 'Failed to install system dependencies:', msg);
1129
+ process.exit(1);
1130
+ }
1131
+ }
1132
+
1133
+ // Install browsers
1134
+ console.log(c('cyan', 'Installing Playwright browsers...'));
1135
+ try {
1136
+ execSync('npx playwright install', { stdio: 'inherit' });
1137
+ console.log(c('green', '✓'), 'Browsers installed successfully');
1138
+ process.exit(0);
1139
+ } catch (error) {
1140
+ console.error(c('red', '✗'), 'Failed to install browsers');
1141
+ process.exit(1);
1142
+ }
1143
+ }
1144
+
1145
+ // === Legacy aliases for backwards compatibility ===
1146
+ case 'url':
1147
+ cmd = { id, action: 'url' };
1148
+ break;
1149
+ case 'title':
1150
+ cmd = { id, action: 'title' };
1151
+ break;
1152
+ case 'gettext':
1153
+ cmd = { id, action: 'gettext', selector: args[0] };
1154
+ break;
1155
+ case 'extract':
1156
+ cmd = { id, action: 'content', selector: args[0] };
1157
+ break;
1158
+
1159
+ default:
1160
+ console.error(c('red', 'Unknown command:'), command);
1161
+ console.error(c('dim', 'Run: agent-browser --help'));
1162
+ process.exit(1);
1163
+ }
1164
+
1165
+ try {
1166
+ const response = await send(cmd);
1167
+ printResponse(response, flags.json);
1168
+ process.exit(0);
1169
+ } catch (error) {
1170
+ const message = error instanceof Error ? error.message : String(error);
1171
+ if (flags.json) {
1172
+ console.log(JSON.stringify({ id, success: false, error: message }));
1173
+ } else {
1174
+ console.error(c('red', '✗ Error:'), message);
1175
+ }
1176
+ process.exit(1);
1177
+ }
1178
+ }
1179
+
1180
+ main();