yiyan-browser-agent 1.0.26 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yiyan-browser-agent",
3
- "version": "1.0.26",
3
+ "version": "1.0.27",
4
4
  "description": "AI coding agent powered by Yiyan (文心一言) via browser automation — no API key needed",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -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
- try {
25
- if (fs.existsSync(CDP_PORT_FILE)) {
26
- const endpointInfo = JSON.parse(fs.readFileSync(CDP_PORT_FILE, 'utf8'));
27
- const wsEndpoint = endpointInfo.wsEndpoint;
28
-
29
- if (!wsEndpoint) {
30
- throw new Error('No wsEndpoint in file');
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: 'networkidle', timeout: 20000 });
43
- await page.waitForTimeout(1500); // Extra wait for UI to render
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 with CDP port
61
+ // Launch new browser
54
62
  return await this._launchNew();
55
63
  }
56
64