yiyan-browser-agent 1.0.24 → 1.0.26
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/browser-manager.js +9 -7
- package/src/index.js +9 -8
- package/src/page-agent.js +10 -3
package/package.json
CHANGED
package/src/browser-manager.js
CHANGED
|
@@ -39,8 +39,8 @@ class BrowserManager {
|
|
|
39
39
|
userAgent: 'Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36',
|
|
40
40
|
});
|
|
41
41
|
const page = await context.newPage();
|
|
42
|
-
await page.goto(config.YIYAN_URL, { waitUntil: '
|
|
43
|
-
await page.waitForTimeout(
|
|
42
|
+
await page.goto(config.YIYAN_URL, { waitUntil: 'networkidle', timeout: 20000 });
|
|
43
|
+
await page.waitForTimeout(1500); // Extra wait for UI to render
|
|
44
44
|
|
|
45
45
|
logger.success('Connected! New tab opened.');
|
|
46
46
|
return { browser, context, page, isNew: false };
|
|
@@ -61,7 +61,6 @@ class BrowserManager {
|
|
|
61
61
|
logger.info('Launching new browser server...');
|
|
62
62
|
|
|
63
63
|
// Use launchServer to create independent browser process
|
|
64
|
-
// Browser will NOT close when Node.js process exits
|
|
65
64
|
const browserServer = await chromium.launchServer({
|
|
66
65
|
headless: false,
|
|
67
66
|
args: [
|
|
@@ -73,14 +72,14 @@ class BrowserManager {
|
|
|
73
72
|
|
|
74
73
|
// Get the WebSocket endpoint
|
|
75
74
|
const wsEndpoint = browserServer.wsEndpoint();
|
|
76
|
-
|
|
75
|
+
logger.dim('WebSocket endpoint: ' + wsEndpoint);
|
|
77
76
|
|
|
78
|
-
// Save endpoint info
|
|
77
|
+
// Save endpoint info IMMEDIATELY before any other operations
|
|
79
78
|
fs.writeFileSync(CDP_PORT_FILE, JSON.stringify({
|
|
80
79
|
wsEndpoint: wsEndpoint,
|
|
81
|
-
port: port,
|
|
82
80
|
launchedAt: Date.now()
|
|
83
81
|
}));
|
|
82
|
+
logger.dim('Endpoint saved to: ' + CDP_PORT_FILE);
|
|
84
83
|
|
|
85
84
|
logger.success('Browser server started!');
|
|
86
85
|
|
|
@@ -98,7 +97,10 @@ class BrowserManager {
|
|
|
98
97
|
await page.waitForTimeout(800);
|
|
99
98
|
|
|
100
99
|
logger.success('Tab ready.');
|
|
101
|
-
|
|
100
|
+
|
|
101
|
+
// Return without browserServer - let it run independently
|
|
102
|
+
// Do NOT close browserServer on process exit
|
|
103
|
+
return { browser, context, page, isNew: true };
|
|
102
104
|
}
|
|
103
105
|
|
|
104
106
|
/**
|
package/src/index.js
CHANGED
|
@@ -151,24 +151,25 @@ async function main() {
|
|
|
151
151
|
const agent = new YiyanAgent({ saveLog: opts.saveLog });
|
|
152
152
|
|
|
153
153
|
// ── Graceful shutdown handler ──────────────────────────────────────────────
|
|
154
|
-
// Only
|
|
155
|
-
const shutdown = async (code = 0) => {
|
|
156
|
-
|
|
157
|
-
|
|
154
|
+
// Only delete endpoint file on Ctrl+C (explicit exit)
|
|
155
|
+
const shutdown = async (code = 0, keepBrowser = true) => {
|
|
156
|
+
if (!keepBrowser) {
|
|
157
|
+
try { BrowserManager.forceClose(); } catch {}
|
|
158
|
+
}
|
|
158
159
|
process.exit(code);
|
|
159
160
|
};
|
|
160
161
|
|
|
161
|
-
process.on('SIGINT', () => shutdown(0));
|
|
162
|
-
process.on('SIGTERM', () => shutdown(0));
|
|
162
|
+
process.on('SIGINT', () => shutdown(0, false)); // Ctrl+C: close browser
|
|
163
|
+
process.on('SIGTERM', () => shutdown(0, false)); // Kill: close browser
|
|
163
164
|
process.on('uncaughtException', async err => {
|
|
164
165
|
logger.error(`Uncaught error: ${err.message}`);
|
|
165
166
|
if (config.DEBUG) console.error(err.stack);
|
|
166
|
-
await shutdown(1);
|
|
167
|
+
await shutdown(1, false); // Error: close browser
|
|
167
168
|
});
|
|
168
169
|
process.on('unhandledRejection', async reason => {
|
|
169
170
|
logger.error(`Unhandled rejection: ${reason}`);
|
|
170
171
|
if (config.DEBUG) console.error(reason);
|
|
171
|
-
await shutdown(1);
|
|
172
|
+
await shutdown(1, false); // Error: close browser
|
|
172
173
|
});
|
|
173
174
|
|
|
174
175
|
// ── Calibrate mode ─────────────────────────────────────────────────────────
|
package/src/page-agent.js
CHANGED
|
@@ -27,17 +27,24 @@ class PageAgent {
|
|
|
27
27
|
* Send message to Yiyan
|
|
28
28
|
*/
|
|
29
29
|
async sendMessage(text) {
|
|
30
|
+
// Wait for page to be fully loaded
|
|
31
|
+
await this.page.waitForLoadState('networkidle', { timeout: 10000 }).catch(() => {});
|
|
32
|
+
await this.page.waitForTimeout(1000);
|
|
33
|
+
|
|
30
34
|
// Find input
|
|
31
35
|
let inputEl = null;
|
|
32
36
|
for (const sel of SEL.chatInput) {
|
|
33
37
|
try {
|
|
34
|
-
inputEl = await this.page.waitForSelector(sel, { timeout:
|
|
35
|
-
if (inputEl)
|
|
38
|
+
inputEl = await this.page.waitForSelector(sel, { timeout: 8000, state: 'visible' });
|
|
39
|
+
if (inputEl) {
|
|
40
|
+
logger.dim('Found input: ' + sel);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
36
43
|
} catch {}
|
|
37
44
|
}
|
|
38
45
|
|
|
39
46
|
if (!inputEl) {
|
|
40
|
-
throw new Error('Cannot find input box');
|
|
47
|
+
throw new Error('Cannot find input box. Make sure Yiyan page is loaded.');
|
|
41
48
|
}
|
|
42
49
|
|
|
43
50
|
// Focus and clear
|