yiyan-browser-agent 1.4.9 → 1.4.11
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/package.json +1 -1
- package/src/client.js +16 -4
- package/src/index.js +22 -18
- package/src/server.js +14 -7
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -19,11 +19,23 @@ class AgentClient {
|
|
|
19
19
|
if (fs.existsSync(LOCK_FILE)) {
|
|
20
20
|
try {
|
|
21
21
|
const lock = JSON.parse(fs.readFileSync(LOCK_FILE, 'utf8'));
|
|
22
|
-
//
|
|
22
|
+
// 检查进程是否活着(Windows 兼容)
|
|
23
23
|
try {
|
|
24
|
-
process.
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
if (process.platform === 'win32') {
|
|
25
|
+
const result = require('child_process').execSync(
|
|
26
|
+
`tasklist /FI "PID eq ${lock.pid}" /NH`,
|
|
27
|
+
{ encoding: 'utf8', timeout: 2000 }
|
|
28
|
+
);
|
|
29
|
+
if (result.includes(lock.pid.toString())) {
|
|
30
|
+
this.port = lock.port || DEFAULT_PORT;
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
} else {
|
|
35
|
+
process.kill(lock.pid, 0);
|
|
36
|
+
this.port = lock.port || DEFAULT_PORT;
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
27
39
|
} catch {
|
|
28
40
|
return false;
|
|
29
41
|
}
|
package/src/index.js
CHANGED
|
@@ -96,6 +96,28 @@ async function main() {
|
|
|
96
96
|
|
|
97
97
|
if (!opts.interactive && !opts.task) opts.interactive = true;
|
|
98
98
|
|
|
99
|
+
// 单任务模式:先检查服务器,避免不必要的浏览器启动
|
|
100
|
+
if (!opts.interactive) {
|
|
101
|
+
const client = new AgentClient();
|
|
102
|
+
|
|
103
|
+
if (client.isServerAvailable()) {
|
|
104
|
+
// 有服务器运行,直接转发任务,不启动浏览器
|
|
105
|
+
if (!silent) {
|
|
106
|
+
logger.banner();
|
|
107
|
+
logger.info(`Working dir: ${config.WORKING_DIR}`);
|
|
108
|
+
logger.info(`Found running server on port ${client.port}, forwarding task...`);
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const result = await client.sendTask(opts.task);
|
|
112
|
+
console.log(JSON.stringify(result, null, 2));
|
|
113
|
+
process.exit(0);
|
|
114
|
+
} catch (err) {
|
|
115
|
+
if (!silent) logger.warn(`Server connection failed: ${err.message}, starting local browser...`);
|
|
116
|
+
// fallback 到本地执行,继续下面的流程
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
99
121
|
try {
|
|
100
122
|
await agent.init();
|
|
101
123
|
} catch (err) {
|
|
@@ -108,24 +130,6 @@ async function main() {
|
|
|
108
130
|
if (opts.interactive) {
|
|
109
131
|
await agent.runInteractive();
|
|
110
132
|
} else {
|
|
111
|
-
// 单任务模式:先检查是否有服务器运行
|
|
112
|
-
const client = new AgentClient();
|
|
113
|
-
|
|
114
|
-
if (client.isServerAvailable()) {
|
|
115
|
-
// 有服务器运行,转发任务
|
|
116
|
-
logger.info(`Found running server, forwarding task...`);
|
|
117
|
-
try {
|
|
118
|
-
const result = await client.sendTask(opts.task);
|
|
119
|
-
console.log(JSON.stringify(result, null, 2));
|
|
120
|
-
// 直接退出,不关闭浏览器(服务器管理)
|
|
121
|
-
process.exit(0);
|
|
122
|
-
} catch (err) {
|
|
123
|
-
// 转发失败,fallback 到本地执行
|
|
124
|
-
logger.warn(`Server connection failed: ${err.message}, running locally...`);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// 无服务器或转发失败,本地执行
|
|
129
133
|
const result = await agent.run(opts.task);
|
|
130
134
|
console.log(JSON.stringify(result, null, 2));
|
|
131
135
|
}
|
package/src/server.js
CHANGED
|
@@ -28,10 +28,8 @@ class AgentServer {
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
await new Promise((resolve, reject) => {
|
|
31
|
-
this.server.
|
|
32
|
-
|
|
33
|
-
else resolve();
|
|
34
|
-
});
|
|
31
|
+
this.server.on('error', reject);
|
|
32
|
+
this.server.listen(this.port, resolve);
|
|
35
33
|
});
|
|
36
34
|
|
|
37
35
|
// 写锁文件
|
|
@@ -54,10 +52,19 @@ class AgentServer {
|
|
|
54
52
|
if (fs.existsSync(LOCK_FILE)) {
|
|
55
53
|
try {
|
|
56
54
|
const lock = JSON.parse(fs.readFileSync(LOCK_FILE, 'utf8'));
|
|
57
|
-
//
|
|
55
|
+
// 检查进程是否还活着(Windows 兼容)
|
|
58
56
|
try {
|
|
59
|
-
process.kill(
|
|
60
|
-
|
|
57
|
+
// Windows: tasklist 检查, Unix: process.kill(0)
|
|
58
|
+
if (process.platform === 'win32') {
|
|
59
|
+
const result = require('child_process').execSync(
|
|
60
|
+
`tasklist /FI "PID eq ${lock.pid}" /NH`,
|
|
61
|
+
{ encoding: 'utf8', timeout: 2000 }
|
|
62
|
+
);
|
|
63
|
+
return result.includes(lock.pid.toString());
|
|
64
|
+
} else {
|
|
65
|
+
process.kill(lock.pid, 0);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
61
68
|
} catch {
|
|
62
69
|
// 进程已死,清理锁文件
|
|
63
70
|
this._removeLockFile();
|