agent-browser 0.3.1 → 0.3.3
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/bin/agent-browser +13 -2
- package/bin/agent-browser-darwin-arm64 +0 -0
- package/bin/agent-browser-darwin-x64 +0 -0
- package/bin/agent-browser-linux-arm64 +0 -0
- package/bin/agent-browser-linux-x64 +0 -0
- package/package.json +6 -1
- package/.prettierrc +0 -7
- package/AGENTS.md +0 -26
- package/benchmark/benchmark.ts +0 -521
- package/benchmark/run.ts +0 -322
- package/cli/Cargo.lock +0 -114
- package/cli/Cargo.toml +0 -17
- package/cli/src/main.rs +0 -332
- package/docker/Dockerfile.build +0 -31
- package/docker/docker-compose.yml +0 -68
- package/src/actions.ts +0 -1670
- package/src/browser.test.ts +0 -157
- package/src/browser.ts +0 -686
- package/src/cli-light.ts +0 -457
- package/src/client.ts +0 -150
- package/src/daemon.ts +0 -187
- package/src/index.ts +0 -1185
- package/src/protocol.test.ts +0 -216
- package/src/protocol.ts +0 -852
- package/src/snapshot.ts +0 -380
- package/src/types.ts +0 -913
- package/tsconfig.json +0 -28
- package/vitest.config.ts +0 -9
package/src/cli-light.ts
DELETED
|
@@ -1,457 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Lightweight CLI client for agent-browser
|
|
4
|
-
*
|
|
5
|
-
* This file contains ONLY the client logic (no Playwright imports).
|
|
6
|
-
* It can be compiled with Bun for fast startup times.
|
|
7
|
-
*
|
|
8
|
-
* The actual browser automation runs in a separate daemon process.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import * as net from 'net';
|
|
12
|
-
import * as fs from 'fs';
|
|
13
|
-
import * as os from 'os';
|
|
14
|
-
import * as path from 'path';
|
|
15
|
-
import { spawn } from 'child_process';
|
|
16
|
-
|
|
17
|
-
// ============================================================================
|
|
18
|
-
// Configuration
|
|
19
|
-
// ============================================================================
|
|
20
|
-
|
|
21
|
-
const SESSION = process.env.AGENT_BROWSER_SESSION || 'default';
|
|
22
|
-
const SOCKET_PATH = path.join(os.tmpdir(), `agent-browser-${SESSION}.sock`);
|
|
23
|
-
const PID_FILE = path.join(os.tmpdir(), `agent-browser-${SESSION}.pid`);
|
|
24
|
-
|
|
25
|
-
// ============================================================================
|
|
26
|
-
// Daemon Management
|
|
27
|
-
// ============================================================================
|
|
28
|
-
|
|
29
|
-
function isDaemonRunning(): boolean {
|
|
30
|
-
if (!fs.existsSync(PID_FILE)) return false;
|
|
31
|
-
try {
|
|
32
|
-
const pid = parseInt(fs.readFileSync(PID_FILE, 'utf8').trim(), 10);
|
|
33
|
-
process.kill(pid, 0);
|
|
34
|
-
return true;
|
|
35
|
-
} catch {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async function ensureDaemon(): Promise<void> {
|
|
41
|
-
if (isDaemonRunning() && fs.existsSync(SOCKET_PATH)) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Find the daemon script - look relative to this script
|
|
46
|
-
const scriptDir = path.dirname(process.argv[1]);
|
|
47
|
-
let daemonPath = path.join(scriptDir, 'daemon.js');
|
|
48
|
-
|
|
49
|
-
// Fallback paths
|
|
50
|
-
if (!fs.existsSync(daemonPath)) {
|
|
51
|
-
daemonPath = path.join(scriptDir, '../dist/daemon.js');
|
|
52
|
-
}
|
|
53
|
-
if (!fs.existsSync(daemonPath)) {
|
|
54
|
-
daemonPath = path.join(process.cwd(), 'dist/daemon.js');
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!fs.existsSync(daemonPath)) {
|
|
58
|
-
throw new Error(`Daemon not found. Looked in: ${daemonPath}`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const child = spawn('node', [daemonPath], {
|
|
62
|
-
detached: true,
|
|
63
|
-
stdio: 'ignore',
|
|
64
|
-
env: { ...process.env, AGENT_BROWSER_DAEMON: '1', AGENT_BROWSER_SESSION: SESSION },
|
|
65
|
-
});
|
|
66
|
-
child.unref();
|
|
67
|
-
|
|
68
|
-
// Wait for socket
|
|
69
|
-
for (let i = 0; i < 50; i++) {
|
|
70
|
-
if (fs.existsSync(SOCKET_PATH)) return;
|
|
71
|
-
await new Promise(r => setTimeout(r, 100));
|
|
72
|
-
}
|
|
73
|
-
throw new Error('Failed to start daemon');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ============================================================================
|
|
77
|
-
// Command Execution
|
|
78
|
-
// ============================================================================
|
|
79
|
-
|
|
80
|
-
interface Response {
|
|
81
|
-
id: string;
|
|
82
|
-
success: boolean;
|
|
83
|
-
data?: unknown;
|
|
84
|
-
error?: string;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function sendCommand(cmd: Record<string, unknown>): Promise<Response> {
|
|
88
|
-
return new Promise((resolve, reject) => {
|
|
89
|
-
let buffer = '';
|
|
90
|
-
let resolved = false;
|
|
91
|
-
const socket = net.createConnection(SOCKET_PATH);
|
|
92
|
-
|
|
93
|
-
const cleanup = () => {
|
|
94
|
-
socket.removeAllListeners();
|
|
95
|
-
socket.destroy();
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
socket.on('connect', () => {
|
|
99
|
-
socket.write(JSON.stringify(cmd) + '\n');
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
socket.on('data', (data) => {
|
|
103
|
-
buffer += data.toString();
|
|
104
|
-
const idx = buffer.indexOf('\n');
|
|
105
|
-
if (idx !== -1 && !resolved) {
|
|
106
|
-
resolved = true;
|
|
107
|
-
try {
|
|
108
|
-
const response = JSON.parse(buffer.substring(0, idx)) as Response;
|
|
109
|
-
cleanup();
|
|
110
|
-
resolve(response);
|
|
111
|
-
} catch {
|
|
112
|
-
cleanup();
|
|
113
|
-
reject(new Error('Invalid JSON response'));
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
socket.on('error', (err) => {
|
|
119
|
-
if (!resolved) {
|
|
120
|
-
resolved = true;
|
|
121
|
-
cleanup();
|
|
122
|
-
reject(err);
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
socket.on('close', () => {
|
|
127
|
-
if (!resolved && buffer.trim()) {
|
|
128
|
-
resolved = true;
|
|
129
|
-
try {
|
|
130
|
-
resolve(JSON.parse(buffer.trim()) as Response);
|
|
131
|
-
} catch {
|
|
132
|
-
reject(new Error('Connection closed'));
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
setTimeout(() => {
|
|
138
|
-
if (!resolved) {
|
|
139
|
-
resolved = true;
|
|
140
|
-
cleanup();
|
|
141
|
-
reject(new Error('Timeout'));
|
|
142
|
-
}
|
|
143
|
-
}, 30000);
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// ============================================================================
|
|
148
|
-
// Command Parsing
|
|
149
|
-
// ============================================================================
|
|
150
|
-
|
|
151
|
-
function parseCommand(parts: string[]): Record<string, unknown> | null {
|
|
152
|
-
if (parts.length === 0) return null;
|
|
153
|
-
|
|
154
|
-
const command = parts[0];
|
|
155
|
-
const rest = parts.slice(1);
|
|
156
|
-
const id = Math.random().toString(36).slice(2, 10);
|
|
157
|
-
|
|
158
|
-
switch (command) {
|
|
159
|
-
case 'open':
|
|
160
|
-
case 'goto':
|
|
161
|
-
case 'navigate':
|
|
162
|
-
return { id, action: 'navigate', url: rest[0]?.startsWith('http') ? rest[0] : `https://${rest[0]}` };
|
|
163
|
-
|
|
164
|
-
case 'click':
|
|
165
|
-
return { id, action: 'click', selector: rest[0] };
|
|
166
|
-
|
|
167
|
-
case 'fill':
|
|
168
|
-
return { id, action: 'fill', selector: rest[0], value: rest.slice(1).join(' ') };
|
|
169
|
-
|
|
170
|
-
case 'type':
|
|
171
|
-
return { id, action: 'type', selector: rest[0], text: rest.slice(1).join(' ') };
|
|
172
|
-
|
|
173
|
-
case 'hover':
|
|
174
|
-
return { id, action: 'hover', selector: rest[0] };
|
|
175
|
-
|
|
176
|
-
case 'snapshot': {
|
|
177
|
-
const opts: Record<string, unknown> = { id, action: 'snapshot' };
|
|
178
|
-
// Parse snapshot options from rest args
|
|
179
|
-
for (let i = 0; i < rest.length; i++) {
|
|
180
|
-
const arg = rest[i];
|
|
181
|
-
if (arg === '-i' || arg === '--interactive') {
|
|
182
|
-
opts.interactive = true;
|
|
183
|
-
} else if (arg === '-c' || arg === '--compact') {
|
|
184
|
-
opts.compact = true;
|
|
185
|
-
} else if (arg === '--depth' || arg === '-d') {
|
|
186
|
-
opts.maxDepth = parseInt(rest[++i], 10);
|
|
187
|
-
} else if (arg === '--selector' || arg === '-s') {
|
|
188
|
-
opts.selector = rest[++i];
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return opts;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
case 'screenshot':
|
|
195
|
-
return { id, action: 'screenshot', path: rest[0] };
|
|
196
|
-
|
|
197
|
-
case 'close':
|
|
198
|
-
case 'quit':
|
|
199
|
-
return { id, action: 'close' };
|
|
200
|
-
|
|
201
|
-
case 'get':
|
|
202
|
-
if (rest[0] === 'text') return { id, action: 'gettext', selector: rest[1] };
|
|
203
|
-
if (rest[0] === 'url') return { id, action: 'url' };
|
|
204
|
-
if (rest[0] === 'title') return { id, action: 'title' };
|
|
205
|
-
return null;
|
|
206
|
-
|
|
207
|
-
case 'press':
|
|
208
|
-
return { id, action: 'press', key: rest[0] };
|
|
209
|
-
|
|
210
|
-
case 'wait':
|
|
211
|
-
if (/^\d+$/.test(rest[0])) {
|
|
212
|
-
return { id, action: 'wait', timeout: parseInt(rest[0], 10) };
|
|
213
|
-
}
|
|
214
|
-
return { id, action: 'wait', selector: rest[0] };
|
|
215
|
-
|
|
216
|
-
case 'back':
|
|
217
|
-
return { id, action: 'back' };
|
|
218
|
-
|
|
219
|
-
case 'forward':
|
|
220
|
-
return { id, action: 'forward' };
|
|
221
|
-
|
|
222
|
-
case 'reload':
|
|
223
|
-
return { id, action: 'reload' };
|
|
224
|
-
|
|
225
|
-
case 'eval':
|
|
226
|
-
return { id, action: 'evaluate', script: rest.join(' ') };
|
|
227
|
-
|
|
228
|
-
default:
|
|
229
|
-
return null;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
function parseBatchCommands(args: string[]): Record<string, unknown>[] {
|
|
234
|
-
const commands: Record<string, unknown>[] = [];
|
|
235
|
-
|
|
236
|
-
// Each argument after 'batch' is a command string
|
|
237
|
-
for (const arg of args) {
|
|
238
|
-
// Split the command string into parts
|
|
239
|
-
const parts = arg.match(/(?:[^\s"]+|"[^"]*")+/g) || [];
|
|
240
|
-
const cleanParts = parts.map(p => p.replace(/^"|"$/g, ''));
|
|
241
|
-
|
|
242
|
-
const cmd = parseCommand(cleanParts);
|
|
243
|
-
if (cmd) {
|
|
244
|
-
commands.push(cmd);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return commands;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// ============================================================================
|
|
252
|
-
// Output Formatting
|
|
253
|
-
// ============================================================================
|
|
254
|
-
|
|
255
|
-
function formatResponse(response: Response): string {
|
|
256
|
-
if (!response.success) {
|
|
257
|
-
return `\x1b[31m✗ Error:\x1b[0m ${response.error}`;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
const data = response.data as Record<string, unknown>;
|
|
261
|
-
|
|
262
|
-
if (data?.url && data?.title) {
|
|
263
|
-
return `\x1b[32m✓\x1b[0m \x1b[1m${data.title}\x1b[0m\n\x1b[2m ${data.url}\x1b[0m`;
|
|
264
|
-
} else if (data?.snapshot) {
|
|
265
|
-
return String(data.snapshot);
|
|
266
|
-
} else if (data?.text !== undefined) {
|
|
267
|
-
return String(data.text);
|
|
268
|
-
} else if (data?.url) {
|
|
269
|
-
return String(data.url);
|
|
270
|
-
} else if (data?.title) {
|
|
271
|
-
return String(data.title);
|
|
272
|
-
} else if (data?.result !== undefined) {
|
|
273
|
-
return typeof data.result === 'object' ? JSON.stringify(data.result, null, 2) : String(data.result);
|
|
274
|
-
} else if (data?.closed) {
|
|
275
|
-
return '\x1b[32m✓\x1b[0m Browser closed';
|
|
276
|
-
} else {
|
|
277
|
-
return '\x1b[32m✓\x1b[0m Done';
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function printResponse(response: Response, json: boolean): void {
|
|
282
|
-
if (json) {
|
|
283
|
-
console.log(JSON.stringify(response));
|
|
284
|
-
} else {
|
|
285
|
-
console.log(formatResponse(response));
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// ============================================================================
|
|
290
|
-
// Main
|
|
291
|
-
// ============================================================================
|
|
292
|
-
|
|
293
|
-
const HELP = `
|
|
294
|
-
agent-browser - fast browser automation CLI
|
|
295
|
-
|
|
296
|
-
Usage:
|
|
297
|
-
agent-browser <command> [args] [--json]
|
|
298
|
-
agent-browser batch <cmd1> <cmd2> ... [--json]
|
|
299
|
-
|
|
300
|
-
Commands:
|
|
301
|
-
open <url> Navigate to URL
|
|
302
|
-
click <sel> Click element (use @ref from snapshot)
|
|
303
|
-
fill <sel> <text> Fill input
|
|
304
|
-
type <sel> <text> Type text
|
|
305
|
-
hover <sel> Hover element
|
|
306
|
-
snapshot [options] Get accessibility tree with refs
|
|
307
|
-
screenshot [path] Take screenshot
|
|
308
|
-
get text <sel> Get text content
|
|
309
|
-
get url Get current URL
|
|
310
|
-
get title Get page title
|
|
311
|
-
press <key> Press keyboard key
|
|
312
|
-
wait <ms|sel> Wait for time or element
|
|
313
|
-
eval <js> Evaluate JavaScript
|
|
314
|
-
close Close browser
|
|
315
|
-
|
|
316
|
-
Snapshot Options:
|
|
317
|
-
-i, --interactive Only show interactive elements (buttons, links, inputs)
|
|
318
|
-
-c, --compact Remove empty structural elements
|
|
319
|
-
-d, --depth <n> Limit tree depth (e.g., --depth 3)
|
|
320
|
-
-s, --selector <sel> Scope snapshot to CSS selector
|
|
321
|
-
|
|
322
|
-
Batch Mode:
|
|
323
|
-
batch <cmd1> <cmd2> ... Execute multiple commands in sequence
|
|
324
|
-
Each command is a quoted string
|
|
325
|
-
|
|
326
|
-
Options:
|
|
327
|
-
--json Output JSON (for AI agents)
|
|
328
|
-
|
|
329
|
-
Examples:
|
|
330
|
-
agent-browser open example.com
|
|
331
|
-
agent-browser snapshot
|
|
332
|
-
agent-browser click @e2
|
|
333
|
-
agent-browser fill @e3 "hello"
|
|
334
|
-
|
|
335
|
-
# Batch mode - execute multiple commands efficiently
|
|
336
|
-
agent-browser batch "open example.com" "snapshot" "click a"
|
|
337
|
-
agent-browser batch "open google.com" "snapshot" "get title" --json
|
|
338
|
-
`;
|
|
339
|
-
|
|
340
|
-
async function runBatch(commands: Record<string, unknown>[], json: boolean): Promise<void> {
|
|
341
|
-
const results: Response[] = [];
|
|
342
|
-
let hasError = false;
|
|
343
|
-
|
|
344
|
-
for (const cmd of commands) {
|
|
345
|
-
try {
|
|
346
|
-
const response = await sendCommand(cmd);
|
|
347
|
-
results.push(response);
|
|
348
|
-
|
|
349
|
-
if (!json) {
|
|
350
|
-
// Print each result as we go for non-JSON mode
|
|
351
|
-
console.log(`\x1b[36m[${cmd.action}]\x1b[0m`);
|
|
352
|
-
console.log(formatResponse(response));
|
|
353
|
-
console.log();
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
if (!response.success) {
|
|
357
|
-
hasError = true;
|
|
358
|
-
break; // Stop on first error
|
|
359
|
-
}
|
|
360
|
-
} catch (err) {
|
|
361
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
362
|
-
const errorResponse: Response = {
|
|
363
|
-
id: String(cmd.id),
|
|
364
|
-
success: false,
|
|
365
|
-
error: message,
|
|
366
|
-
};
|
|
367
|
-
results.push(errorResponse);
|
|
368
|
-
hasError = true;
|
|
369
|
-
|
|
370
|
-
if (!json) {
|
|
371
|
-
console.log(`\x1b[36m[${cmd.action}]\x1b[0m`);
|
|
372
|
-
console.log(`\x1b[31m✗ Error:\x1b[0m ${message}`);
|
|
373
|
-
}
|
|
374
|
-
break;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
if (json) {
|
|
379
|
-
console.log(JSON.stringify({
|
|
380
|
-
success: !hasError,
|
|
381
|
-
results,
|
|
382
|
-
completed: results.length,
|
|
383
|
-
total: commands.length,
|
|
384
|
-
}));
|
|
385
|
-
} else {
|
|
386
|
-
console.log(`\x1b[2m─────────────────────────────────────\x1b[0m`);
|
|
387
|
-
console.log(`Completed ${results.length}/${commands.length} commands`);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
process.exit(hasError ? 1 : 0);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
async function main(): Promise<void> {
|
|
394
|
-
const args = process.argv.slice(2);
|
|
395
|
-
const json = args.includes('--json');
|
|
396
|
-
const cleanArgs = args.filter(a => !a.startsWith('--'));
|
|
397
|
-
|
|
398
|
-
if (cleanArgs.length === 0 || args.includes('--help') || args.includes('-h')) {
|
|
399
|
-
console.log(HELP);
|
|
400
|
-
process.exit(0);
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
// Check for batch mode
|
|
404
|
-
if (cleanArgs[0] === 'batch') {
|
|
405
|
-
const batchArgs = cleanArgs.slice(1);
|
|
406
|
-
if (batchArgs.length === 0) {
|
|
407
|
-
console.error('\x1b[31mBatch mode requires at least one command\x1b[0m');
|
|
408
|
-
console.log('\nExample: agent-browser batch "open example.com" "snapshot"');
|
|
409
|
-
process.exit(1);
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
const commands = parseBatchCommands(batchArgs);
|
|
413
|
-
if (commands.length === 0) {
|
|
414
|
-
console.error('\x1b[31mNo valid commands found\x1b[0m');
|
|
415
|
-
process.exit(1);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
try {
|
|
419
|
-
await ensureDaemon();
|
|
420
|
-
await runBatch(commands, json);
|
|
421
|
-
} catch (err) {
|
|
422
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
423
|
-
if (json) {
|
|
424
|
-
console.log(JSON.stringify({ success: false, error: message }));
|
|
425
|
-
} else {
|
|
426
|
-
console.error('\x1b[31m✗ Error:\x1b[0m', message);
|
|
427
|
-
}
|
|
428
|
-
process.exit(1);
|
|
429
|
-
}
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
// Single command mode
|
|
434
|
-
const cmd = parseCommand(cleanArgs);
|
|
435
|
-
|
|
436
|
-
if (!cmd) {
|
|
437
|
-
console.error('\x1b[31mUnknown command:\x1b[0m', cleanArgs[0]);
|
|
438
|
-
process.exit(1);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
try {
|
|
442
|
-
await ensureDaemon();
|
|
443
|
-
const response = await sendCommand(cmd);
|
|
444
|
-
printResponse(response, json);
|
|
445
|
-
process.exit(response.success ? 0 : 1);
|
|
446
|
-
} catch (err) {
|
|
447
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
448
|
-
if (json) {
|
|
449
|
-
console.log(JSON.stringify({ success: false, error: message }));
|
|
450
|
-
} else {
|
|
451
|
-
console.error('\x1b[31m✗ Error:\x1b[0m', message);
|
|
452
|
-
}
|
|
453
|
-
process.exit(1);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
main();
|
package/src/client.ts
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import * as net from 'net';
|
|
2
|
-
import { spawn } from 'child_process';
|
|
3
|
-
import { fileURLToPath } from 'url';
|
|
4
|
-
import * as path from 'path';
|
|
5
|
-
import * as fs from 'fs';
|
|
6
|
-
import { getSocketPath, isDaemonRunning, setSession, getSession } from './daemon.js';
|
|
7
|
-
import type { Response } from './types.js';
|
|
8
|
-
|
|
9
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
10
|
-
|
|
11
|
-
let DEBUG = false;
|
|
12
|
-
|
|
13
|
-
export function setDebug(enabled: boolean): void {
|
|
14
|
-
DEBUG = enabled;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export { setSession, getSession };
|
|
18
|
-
|
|
19
|
-
function debug(...args: unknown[]): void {
|
|
20
|
-
if (DEBUG) {
|
|
21
|
-
console.error('[debug]', ...args);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Wait for socket to exist
|
|
27
|
-
*/
|
|
28
|
-
async function waitForSocket(maxAttempts = 30): Promise<boolean> {
|
|
29
|
-
const socketPath = getSocketPath();
|
|
30
|
-
debug('Waiting for socket at', socketPath);
|
|
31
|
-
for (let i = 0; i < maxAttempts; i++) {
|
|
32
|
-
if (fs.existsSync(socketPath)) {
|
|
33
|
-
debug('Socket found after', i * 100, 'ms');
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
await new Promise((r) => setTimeout(r, 100));
|
|
37
|
-
}
|
|
38
|
-
debug('Socket not found after', maxAttempts * 100, 'ms');
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Ensure daemon is running, start if not
|
|
44
|
-
*/
|
|
45
|
-
export async function ensureDaemon(): Promise<void> {
|
|
46
|
-
const session = getSession();
|
|
47
|
-
debug(`Checking if daemon is running for session "${session}"...`);
|
|
48
|
-
if (isDaemonRunning()) {
|
|
49
|
-
debug('Daemon already running');
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
debug('Starting daemon...');
|
|
54
|
-
const daemonPath = path.join(__dirname, 'daemon.js');
|
|
55
|
-
const child = spawn(process.execPath, [daemonPath], {
|
|
56
|
-
detached: true,
|
|
57
|
-
stdio: 'ignore',
|
|
58
|
-
env: { ...process.env, AGENT_BROWSER_DAEMON: '1', AGENT_BROWSER_SESSION: session },
|
|
59
|
-
});
|
|
60
|
-
child.unref();
|
|
61
|
-
|
|
62
|
-
// Wait for socket to be created
|
|
63
|
-
const ready = await waitForSocket();
|
|
64
|
-
if (!ready) {
|
|
65
|
-
throw new Error('Failed to start daemon');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
debug(`Daemon started for session "${session}"`);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Send a command to the daemon
|
|
73
|
-
*/
|
|
74
|
-
export async function sendCommand(command: Record<string, unknown>): Promise<Response> {
|
|
75
|
-
const socketPath = getSocketPath();
|
|
76
|
-
debug('Sending command:', JSON.stringify(command));
|
|
77
|
-
|
|
78
|
-
return new Promise((resolve, reject) => {
|
|
79
|
-
let resolved = false;
|
|
80
|
-
let buffer = '';
|
|
81
|
-
const startTime = Date.now();
|
|
82
|
-
|
|
83
|
-
const socket = net.createConnection(socketPath);
|
|
84
|
-
|
|
85
|
-
socket.on('connect', () => {
|
|
86
|
-
debug('Connected to daemon, sending command...');
|
|
87
|
-
socket.write(JSON.stringify(command) + '\n');
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
socket.on('data', (data) => {
|
|
91
|
-
buffer += data.toString();
|
|
92
|
-
debug('Received data:', buffer.length, 'bytes');
|
|
93
|
-
|
|
94
|
-
// Try to parse complete JSON from buffer
|
|
95
|
-
const newlineIdx = buffer.indexOf('\n');
|
|
96
|
-
if (newlineIdx !== -1) {
|
|
97
|
-
const jsonStr = buffer.substring(0, newlineIdx);
|
|
98
|
-
try {
|
|
99
|
-
const response = JSON.parse(jsonStr) as Response;
|
|
100
|
-
debug('Response received in', Date.now() - startTime, 'ms');
|
|
101
|
-
resolved = true;
|
|
102
|
-
socket.end();
|
|
103
|
-
resolve(response);
|
|
104
|
-
} catch (e) {
|
|
105
|
-
debug('JSON parse error:', e);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
socket.on('error', (err) => {
|
|
111
|
-
debug('Socket error:', err.message);
|
|
112
|
-
if (!resolved) {
|
|
113
|
-
reject(new Error(`Connection error: ${err.message}`));
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
socket.on('close', () => {
|
|
118
|
-
debug('Socket closed, resolved:', resolved, 'buffer:', buffer.length);
|
|
119
|
-
if (!resolved && buffer.trim()) {
|
|
120
|
-
try {
|
|
121
|
-
const response = JSON.parse(buffer.trim()) as Response;
|
|
122
|
-
resolve(response);
|
|
123
|
-
} catch {
|
|
124
|
-
reject(new Error('Invalid response from daemon'));
|
|
125
|
-
}
|
|
126
|
-
} else if (!resolved) {
|
|
127
|
-
reject(new Error('Connection closed without response'));
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
// Timeout after 15 seconds (allows for 10s Playwright timeout + overhead)
|
|
132
|
-
setTimeout(() => {
|
|
133
|
-
if (!resolved) {
|
|
134
|
-
debug('Command timeout after 15s');
|
|
135
|
-
socket.destroy();
|
|
136
|
-
reject(new Error('Command timeout'));
|
|
137
|
-
}
|
|
138
|
-
}, 15000);
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Send a command, ensuring daemon is running first
|
|
144
|
-
*/
|
|
145
|
-
export async function send(command: Record<string, unknown>): Promise<Response> {
|
|
146
|
-
const startTime = Date.now();
|
|
147
|
-
await ensureDaemon();
|
|
148
|
-
debug('ensureDaemon took', Date.now() - startTime, 'ms');
|
|
149
|
-
return sendCommand(command);
|
|
150
|
-
}
|