yiyan-browser-agent 1.0.25 → 1.0.27
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 +35 -27
- package/src/page-agent.js +10 -3
package/package.json
CHANGED
package/src/browser-manager.js
CHANGED
|
@@ -20,37 +20,45 @@ class BrowserManager {
|
|
|
20
20
|
// Ensure directories exist
|
|
21
21
|
fs.mkdirSync(SESSION_DIR, { recursive: true });
|
|
22
22
|
|
|
23
|
-
// Try connect existing browser
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
// Try connect existing browser (with retries for concurrent launches)
|
|
24
|
+
for (let retry = 0; retry < 3; retry++) {
|
|
25
|
+
try {
|
|
26
|
+
if (fs.existsSync(CDP_PORT_FILE)) {
|
|
27
|
+
const endpointInfo = JSON.parse(fs.readFileSync(CDP_PORT_FILE, 'utf8'));
|
|
28
|
+
const wsEndpoint = endpointInfo.wsEndpoint;
|
|
29
|
+
|
|
30
|
+
if (!wsEndpoint) {
|
|
31
|
+
throw new Error('No wsEndpoint in file');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
logger.info('Connecting to existing browser...');
|
|
35
|
+
const browser = await chromium.connect({ wsEndpoint, timeout: 10000 });
|
|
36
|
+
|
|
37
|
+
// Create NEW context and page (tab) for this task
|
|
38
|
+
const context = await browser.newContext({
|
|
39
|
+
viewport: { width: 1280, height: 900 },
|
|
40
|
+
userAgent: 'Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36',
|
|
41
|
+
});
|
|
42
|
+
const page = await context.newPage();
|
|
43
|
+
await page.goto(config.YIYAN_URL, { waitUntil: 'networkidle', timeout: 20000 });
|
|
44
|
+
await page.waitForTimeout(1500);
|
|
45
|
+
|
|
46
|
+
logger.success('Connected! New tab opened.');
|
|
47
|
+
return { browser, context, page, isNew: false };
|
|
48
|
+
}
|
|
49
|
+
} catch (err) {
|
|
50
|
+
// If connection failed, wait and retry (another process might be starting)
|
|
51
|
+
if (retry < 2) {
|
|
52
|
+
logger.warn(`Connection attempt ${retry + 1} failed, waiting 3 seconds...`);
|
|
53
|
+
await new Promise(r => setTimeout(r, 3000));
|
|
54
|
+
} else {
|
|
55
|
+
logger.warn('Connection failed after retries, launching new browser...');
|
|
56
|
+
try { fs.unlinkSync(CDP_PORT_FILE); } catch {}
|
|
31
57
|
}
|
|
32
|
-
|
|
33
|
-
logger.info('Connecting to existing browser...');
|
|
34
|
-
const browser = await chromium.connect({ wsEndpoint, timeout: 5000 });
|
|
35
|
-
|
|
36
|
-
// Create NEW context and page (tab) for this task
|
|
37
|
-
const context = await browser.newContext({
|
|
38
|
-
viewport: { width: 1280, height: 900 },
|
|
39
|
-
userAgent: 'Mozilla/5.0 AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36',
|
|
40
|
-
});
|
|
41
|
-
const page = await context.newPage();
|
|
42
|
-
await page.goto(config.YIYAN_URL, { waitUntil: 'domcontentloaded', timeout: 15000 });
|
|
43
|
-
await page.waitForTimeout(500);
|
|
44
|
-
|
|
45
|
-
logger.success('Connected! New tab opened.');
|
|
46
|
-
return { browser, context, page, isNew: false };
|
|
47
58
|
}
|
|
48
|
-
} catch (err) {
|
|
49
|
-
logger.warn('Connection failed, launching new browser...');
|
|
50
|
-
try { fs.unlinkSync(CDP_PORT_FILE); } catch {}
|
|
51
59
|
}
|
|
52
60
|
|
|
53
|
-
// Launch new browser
|
|
61
|
+
// Launch new browser
|
|
54
62
|
return await this._launchNew();
|
|
55
63
|
}
|
|
56
64
|
|
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
|