yiyan-browser-agent 1.0.16 → 1.0.18

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.18",
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;
package/src/index.js CHANGED
@@ -150,23 +150,25 @@ async function main() {
150
150
  const agent = new YiyanAgent({ saveLog: opts.saveLog });
151
151
 
152
152
  // ── Graceful shutdown handler ──────────────────────────────────────────────
153
- const shutdown = async (code = 0) => {
154
- logger.info('\nShutting down...');
155
- try { await agent.shutdown(); } catch {}
153
+ const shutdown = async (code = 0, closeBrowser = true) => {
154
+ if (closeBrowser) {
155
+ logger.info('\nShutting down...');
156
+ try { await agent.shutdown(); } catch {}
157
+ }
156
158
  process.exit(code);
157
159
  };
158
160
 
159
- process.on('SIGINT', () => shutdown(0));
160
- process.on('SIGTERM', () => shutdown(0));
161
+ process.on('SIGINT', () => shutdown(0, true));
162
+ process.on('SIGTERM', () => shutdown(0, true));
161
163
  process.on('uncaughtException', async err => {
162
164
  logger.error(`Uncaught error: ${err.message}`);
163
165
  if (config.DEBUG) console.error(err.stack);
164
- await shutdown(1);
166
+ await shutdown(1, true); // Error: close browser
165
167
  });
166
168
  process.on('unhandledRejection', async reason => {
167
169
  logger.error(`Unhandled rejection: ${reason}`);
168
170
  if (config.DEBUG) console.error(reason);
169
- await shutdown(1);
171
+ await shutdown(1, true); // Error: close browser
170
172
  });
171
173
 
172
174
  // ── Calibrate mode ─────────────────────────────────────────────────────────
@@ -198,9 +200,12 @@ async function main() {
198
200
  try {
199
201
  if (opts.interactive) {
200
202
  await agent.runInteractive();
203
+ await shutdown(0, true); // Interactive mode exit: close browser
201
204
  } else {
202
205
  const result = await agent.run(opts.task);
203
206
  console.log(JSON.stringify(result, null, 2));
207
+ // Single task mode: keep browser open if success, close on error
208
+ await shutdown(0, result.status === 'error');
204
209
  }
205
210
  } catch (err) {
206
211
  console.log(JSON.stringify({
@@ -209,10 +214,8 @@ async function main() {
209
214
  duration: 0,
210
215
  status: 'error'
211
216
  }, null, 2));
212
- await shutdown(1);
217
+ await shutdown(1, true); // Error: close browser
213
218
  }
214
-
215
- await shutdown(0);
216
219
  }
217
220
 
218
221
  main();