yiyan-browser-agent 1.0.16 → 1.0.17

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.16",
3
+ "version": "1.0.17",
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": {
package/src/agent.js CHANGED
@@ -6,7 +6,7 @@ const path = require('path');
6
6
  const { execSync } = require('child_process');
7
7
  const config = require('./config');
8
8
  const logger = require('./logger');
9
- const YiyanBrowser = require('./browser');
9
+ const BrowserManager = require('./browser-manager');
10
10
  const { executeTool } = require('./tools');
11
11
  const { parseResponse,
12
12
  formatToolResult } = require('./parser');
@@ -18,7 +18,7 @@ const { ConversationManager } = require('./prompt');
18
18
 
19
19
  class YiyanAgent {
20
20
  constructor(options = {}) {
21
- this.browser = new YiyanBrowser();
21
+ this.browser = null; // Will be obtained from BrowserManager
22
22
  this.conversation = new ConversationManager();
23
23
  this.options = options;
24
24
  this._running = false;
@@ -26,15 +26,15 @@ class YiyanAgent {
26
26
 
27
27
  // ── Public API ──────────────────────────────────────────────────────────────
28
28
 
29
- /** Boot the browser and load Yiyan */
29
+ /** Boot the browser (reuse if available, restart if error) */
30
30
  async init() {
31
- await this.browser.launch();
32
- await this.browser.newChat();
31
+ this.browser = await BrowserManager.getInstance();
32
+ await BrowserManager.newChat();
33
33
  }
34
34
 
35
- /** Shut down cleanly */
35
+ /** Shut down cleanly (only on explicit exit, not after each task) */
36
36
  async shutdown() {
37
- await this.browser.close();
37
+ await BrowserManager.close();
38
38
  }
39
39
 
40
40
  /**
@@ -48,7 +48,13 @@ class YiyanAgent {
48
48
 
49
49
  // ── Send task directly ───────────────────────────────────
50
50
  logger.info('Sending task to Yiyan...');
51
- await this.browser.sendMessage(task);
51
+ try {
52
+ await this.browser.sendMessage(task);
53
+ } catch (err) {
54
+ // Input error - mark for restart
55
+ BrowserManager.markError();
56
+ throw err;
57
+ }
52
58
 
53
59
  // ── Agent loop ──────────────────────────────────────────────────────
54
60
  let finalAnswer = '';
@@ -114,6 +120,14 @@ class YiyanAgent {
114
120
  finalAnswer = `Reached maximum iterations (${maxIter}).`;
115
121
  status = 'incomplete';
116
122
  }
123
+
124
+ // Mark browser status for reuse
125
+ if (status === 'success') {
126
+ BrowserManager.markSuccess(); // Keep browser open for next task
127
+ } else {
128
+ BrowserManager.markError(); // Restart browser next time
129
+ }
130
+
117
131
  this._running = false;
118
132
 
119
133
  // Return JSON
@@ -0,0 +1,85 @@
1
+ // src/browser-manager.js — Singleton browser instance manager for reuse
2
+ 'use strict';
3
+
4
+ const YiyanBrowser = require('./browser');
5
+ const logger = require('./logger');
6
+
7
+ // Global singleton instance
8
+ let _instance = null;
9
+ let _initialized = false;
10
+ let _hasError = false;
11
+
12
+ class BrowserManager {
13
+ /**
14
+ * Get or create browser instance (singleton)
15
+ * If previous run had error, will restart the browser
16
+ */
17
+ static async getInstance() {
18
+ // If there was an error, restart the browser
19
+ if (_hasError && _instance) {
20
+ logger.info('Previous error detected, restarting browser...');
21
+ try {
22
+ await _instance.close();
23
+ } catch {}
24
+ _instance = null;
25
+ _initialized = false;
26
+ _hasError = false;
27
+ }
28
+
29
+ // Create new instance if needed
30
+ if (!_instance) {
31
+ _instance = new YiyanBrowser();
32
+ await _instance.launch();
33
+ _initialized = true;
34
+ logger.success('Browser initialized (will be reused)');
35
+ }
36
+
37
+ return _instance;
38
+ }
39
+
40
+ /**
41
+ * Mark that an error occurred (browser should be restarted next time)
42
+ */
43
+ static markError() {
44
+ _hasError = true;
45
+ }
46
+
47
+ /**
48
+ * Mark that everything is working fine (keep browser open)
49
+ */
50
+ static markSuccess() {
51
+ _hasError = false;
52
+ }
53
+
54
+ /**
55
+ * Check if browser is currently initialized
56
+ */
57
+ static isInitialized() {
58
+ return _initialized && _instance && !_instance._closed;
59
+ }
60
+
61
+ /**
62
+ * Force close the browser (for shutdown)
63
+ */
64
+ static async close() {
65
+ if (_instance) {
66
+ try {
67
+ await _instance.close();
68
+ } catch {}
69
+ _instance = null;
70
+ _initialized = false;
71
+ _hasError = false;
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Reset for new chat session
77
+ */
78
+ static async newChat() {
79
+ if (_instance) {
80
+ await _instance.newChat();
81
+ }
82
+ }
83
+ }
84
+
85
+ module.exports = BrowserManager;