yiyan-browser-agent 1.4.5 → 1.4.7

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.
@@ -0,0 +1,178 @@
1
+ #!/usr/bin/env node
2
+ // src/calibrate.js — Auto-detect Yiyan UI selectors and update browser.js
3
+ 'use strict';
4
+
5
+ const { chromium } = require('playwright');
6
+ const path = require('path');
7
+ const fs = require('fs');
8
+ const config = require('./config');
9
+
10
+ console.log('\n🔬 Yiyan Agent — Selector Calibration Tool\n');
11
+ console.log('This tool opens Yiyan (文心一言), inspects the DOM, and prints out');
12
+ console.log('the selectors that your browser.js should use.\n');
13
+
14
+ async function calibrate() {
15
+ const context = await chromium.launchPersistentContext(config.SESSION_DIR, {
16
+ headless : false,
17
+ viewport : { width: 1280, height: 900 },
18
+ });
19
+
20
+ const pages = context.pages();
21
+ const page = pages.length > 0 ? pages[0] : await context.newPage();
22
+
23
+ console.log('→ Navigating to', config.YIYAN_URL, '...');
24
+ await page.goto(config.YIYAN_URL, { waitUntil: 'domcontentloaded', timeout: 30_000 });
25
+ await page.waitForTimeout(3_000);
26
+
27
+ console.log('→ Inspecting DOM...\n');
28
+
29
+ const report = await page.evaluate(() => {
30
+ function isVisible(el) {
31
+ const s = window.getComputedStyle(el);
32
+ return s.display !== 'none' && s.visibility !== 'hidden' && s.opacity !== '0' && el.offsetParent !== null;
33
+ }
34
+
35
+ function classify(el) {
36
+ return {
37
+ tag : el.tagName.toLowerCase(),
38
+ id : el.id || null,
39
+ classes : el.className?.slice?.(0, 120) || null,
40
+ placeholder : el.placeholder || null,
41
+ ariaLabel : el.getAttribute('aria-label') || null,
42
+ dataTestId : el.dataset?.testid || null,
43
+ role : el.getAttribute('role') || null,
44
+ visible : isVisible(el),
45
+ text : (el.innerText || '').slice(0, 40).replace(/\n/g, ' ') || null,
46
+ type : el.getAttribute('type') || null,
47
+ };
48
+ }
49
+
50
+ // ── Inputs ───────────────────────────────────────────────────────────
51
+ const inputs = Array.from(document.querySelectorAll('textarea, [contenteditable="true"]'))
52
+ .map(classify);
53
+
54
+ // ── Buttons ──────────────────────────────────────────────────────────
55
+ const buttons = Array.from(document.querySelectorAll('button, [role="button"]'))
56
+ .filter(isVisible)
57
+ .map(classify)
58
+ .slice(0, 30);
59
+
60
+ // ── All named classes ─────────────────────────────────────────────────
61
+ const classFreq = {};
62
+ document.querySelectorAll('*').forEach(el => {
63
+ (el.getAttribute('class') || '').split(/\s+/).forEach(c => {
64
+ if (c.length > 2 && c.length < 50) {
65
+ classFreq[c] = (classFreq[c] || 0) + 1;
66
+ }
67
+ });
68
+ });
69
+ const topClasses = Object.entries(classFreq)
70
+ .sort((a, b) => b[1] - a[1])
71
+ .slice(0, 80)
72
+ .map(([cls, n]) => ({ cls, n }));
73
+
74
+ // ── Suggested selectors ───────────────────────────────────────────────
75
+ const suggestedInput = (
76
+ inputs.find(i => i.placeholder?.toLowerCase().includes('message'))?.id ||
77
+ inputs.find(i => i.placeholder?.toLowerCase().includes('ask'))?.id ||
78
+ inputs.find(i => i.visible)?.id ||
79
+ null
80
+ );
81
+
82
+ const sendBtn = buttons.find(b =>
83
+ /send/i.test(b.ariaLabel || '') ||
84
+ /send/i.test(b.text || '') ||
85
+ /send/i.test(b.classes || '')
86
+ );
87
+
88
+ const stopBtn = buttons.find(b =>
89
+ /stop/i.test(b.ariaLabel || '') ||
90
+ /stop/i.test(b.text || '') ||
91
+ /stop/i.test(b.classes || '')
92
+ );
93
+
94
+ const newChatBtn = buttons.find(b =>
95
+ /new chat/i.test(b.ariaLabel || '') ||
96
+ /new chat/i.test(b.text || '') ||
97
+ /new.?chat/i.test(b.classes || '')
98
+ );
99
+
100
+ return {
101
+ url : window.location.href,
102
+ title : document.title,
103
+ inputs,
104
+ buttons,
105
+ topClasses,
106
+ suggestions: { suggestedInput, sendBtn, stopBtn, newChatBtn },
107
+ };
108
+ });
109
+
110
+ // ── Print report ──────────────────────────────────────────────────────────
111
+ const sep = '─'.repeat(60);
112
+
113
+ console.log(sep);
114
+ console.log('URL :', report.url);
115
+ console.log('Title :', report.title);
116
+ console.log(sep);
117
+
118
+ console.log('\n📥 INPUT ELEMENTS:');
119
+ if (report.inputs.length === 0) {
120
+ console.log(' (none found — are you logged in?)');
121
+ }
122
+ report.inputs.forEach((el, i) => {
123
+ console.log(` [${i}] ${JSON.stringify(el)}`);
124
+ });
125
+
126
+ console.log('\n🔘 BUTTONS (visible, first 30):');
127
+ report.buttons.forEach((el, i) => {
128
+ console.log(` [${i}] ${JSON.stringify(el)}`);
129
+ });
130
+
131
+ console.log('\n🏷️ TOP CSS CLASSES:');
132
+ report.topClasses.slice(0, 40).forEach(({ cls, n }) => {
133
+ console.log(` ${String(n).padStart(4)}x .${cls}`);
134
+ });
135
+
136
+ console.log('\n' + sep);
137
+ console.log('🎯 SUGGESTED SELECTORS (update browser.js SEL object):');
138
+ console.log(sep);
139
+
140
+ const s = report.suggestions;
141
+
142
+ if (s.suggestedInput) {
143
+ console.log(` chatInput : '#${s.suggestedInput}' (or use the id above)`);
144
+ }
145
+ if (s.sendBtn) {
146
+ const sel = s.sendBtn.ariaLabel
147
+ ? `button[aria-label="${s.sendBtn.ariaLabel}"]`
148
+ : s.sendBtn.id ? `#${s.sendBtn.id}` : `.${s.sendBtn.classes?.split(' ')[0]}`;
149
+ console.log(` sendButton : '${sel}'`);
150
+ }
151
+ if (s.stopBtn) {
152
+ const sel = s.stopBtn.ariaLabel
153
+ ? `button[aria-label="${s.stopBtn.ariaLabel}"]`
154
+ : `.${s.stopBtn.classes?.split(' ')[0]}`;
155
+ console.log(` stopButton : '${sel}'`);
156
+ }
157
+ if (s.newChatBtn) {
158
+ const sel = s.newChatBtn.ariaLabel
159
+ ? `button[aria-label="${s.newChatBtn.ariaLabel}"]`
160
+ : `.${s.newChatBtn.classes?.split(' ')[0]}`;
161
+ console.log(` newChat : '${sel}'`);
162
+ }
163
+
164
+ console.log(sep);
165
+ console.log('\n📸 Taking screenshot → /tmp/yiyan-calibrate.png');
166
+ await page.screenshot({ path: '/tmp/yiyan-calibrate.png', fullPage: false });
167
+
168
+ console.log('\n✅ Calibration complete! Update src/browser.js SEL object with the selectors above.');
169
+ console.log(' Press Ctrl+C to exit.\n');
170
+
171
+ // Keep browser open so user can inspect manually
172
+ await new Promise(() => {}); // wait forever
173
+ }
174
+
175
+ calibrate().catch(err => {
176
+ console.error('Calibration error:', err.message);
177
+ process.exit(1);
178
+ });
package/src/config.js ADDED
@@ -0,0 +1,68 @@
1
+ // src/config.js — Central configuration for Yiyan Agent
2
+ const path = require('path');
3
+ const fs = require('fs');
4
+ const os = require('os');
5
+
6
+ // ─────────────────────────────────────────────
7
+ // Default configuration
8
+ // ─────────────────────────────────────────────
9
+ const defaults = {
10
+ // Browser
11
+ YIYAN_URL : 'https://yiyan.baidu.com/',
12
+ SESSION_DIR : path.join(os.homedir(), '.yiyan-agent', 'session'),
13
+ HEADLESS : false,
14
+
15
+ // Timing (configurable for speed vs stability tradeoff)
16
+ RESPONSE_TIMEOUT : 180_000,
17
+ STABLE_DELAY : 1_000, // 1s stability check (faster, may cut off long responses)
18
+ SEND_DELAY : 100, // Reduced for faster operation
19
+
20
+ // Agent
21
+ MAX_ITERATIONS : 40,
22
+ WORKING_DIR : process.cwd(),
23
+
24
+ // Output
25
+ MAX_OUTPUT_LENGTH : 8_000,
26
+ DEBUG : false,
27
+ };
28
+
29
+ // ─────────────────────────────────────────────
30
+ // Config loading priority (highest wins):
31
+ //
32
+ // 1. ~/.yiyan-agent/config.json — global user config
33
+ // 2. ./yiyan-agent.config.json — per-project config
34
+ // ─────────────────────────────────────────────
35
+
36
+ function loadJson(filePath) {
37
+ try {
38
+ if (fs.existsSync(filePath)) {
39
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
40
+ }
41
+ } catch {
42
+ console.warn('[yiyan-agent] Could not parse config file: ' + filePath);
43
+ }
44
+ return {};
45
+ }
46
+
47
+ const globalConfigPath = path.join(os.homedir(), '.yiyan-agent', 'config.json');
48
+ const projectConfigPath = path.join(process.cwd(), 'yiyan-agent.config.json');
49
+
50
+ const config = {
51
+ ...defaults,
52
+ ...loadJson(globalConfigPath), // global overrides defaults
53
+ ...loadJson(projectConfigPath), // project overrides global
54
+ };
55
+
56
+ // Remove comment keys from JSON files
57
+ delete config._comment;
58
+
59
+ // Resolve session dir to absolute path
60
+ if (!path.isAbsolute(config.SESSION_DIR)) {
61
+ config.SESSION_DIR = path.resolve(process.cwd(), config.SESSION_DIR);
62
+ }
63
+
64
+ // Ensure required directories exist
65
+ fs.mkdirSync(config.SESSION_DIR, { recursive: true });
66
+ fs.mkdirSync(path.join(os.homedir(), '.yiyan-agent', 'logs'), { recursive: true });
67
+
68
+ module.exports = config;
package/src/index.js ADDED
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env node
2
+ // src/index.js — CLI entry point
3
+ 'use strict';
4
+
5
+ const path = require('path');
6
+ const fs = require('fs');
7
+ const config = require('./config');
8
+ const logger = require('./logger');
9
+ const YiyanAgent = require('./agent');
10
+
11
+ function parseArgs(argv) {
12
+ const args = argv.slice(2);
13
+ const opts = { task: null, interactive: false, debug: false, showBrowser: false, workingDir: null, calibrate: false, help: false };
14
+
15
+ for (let i = 0; i < args.length; i++) {
16
+ const a = args[i];
17
+ if (a === '-i' || a === '--interactive') opts.interactive = true;
18
+ else if (a === '--debug') opts.debug = true;
19
+ else if (a === '--show-browser') opts.showBrowser = true;
20
+ else if (a === '--calibrate') opts.calibrate = true;
21
+ else if (a === '-h' || a === '--help') opts.help = true;
22
+ else if (a === '-d' || a === '--dir') opts.workingDir = args[++i];
23
+ else if (a === '-t' || a === '--task') opts.task = args[++i];
24
+ else if (!a.startsWith('-')) { opts.task = args.slice(i).join(' '); break; }
25
+ }
26
+ return opts;
27
+ }
28
+
29
+ function printHelp() {
30
+ console.log(`
31
+ yiyan-agent — AI Agent via Yiyan Browser
32
+
33
+ USAGE
34
+ yiyan-agent "任务" # 执行任务,只输出JSON
35
+ yiyan-agent -i # 交互模式(用于登录)
36
+
37
+ OPTIONS
38
+ -i, --interactive 交互模式
39
+ --show-browser 显示浏览器
40
+ --debug 调试模式
41
+ -h, --help 帮助
42
+ `);
43
+ }
44
+
45
+ async function main() {
46
+ const opts = parseArgs(process.argv);
47
+
48
+ if (opts.help) { printHelp(); process.exit(0); }
49
+
50
+ if (opts.debug) config.DEBUG = true;
51
+
52
+ // Headless: 单任务默认隐藏,交互模式显示
53
+ config.HEADLESS = !(opts.interactive || opts.calibrate || opts.showBrowser);
54
+
55
+ if (opts.workingDir) {
56
+ const resolved = path.resolve(opts.workingDir);
57
+ if (!fs.existsSync(resolved)) {
58
+ if (opts.interactive) logger.error(`Dir not found: ${resolved}`);
59
+ else console.log(JSON.stringify({ question: '', answer: `Dir not found: ${resolved}`, status: 'error' }));
60
+ process.exit(1);
61
+ }
62
+ config.WORKING_DIR = resolved;
63
+ }
64
+
65
+ // 只在交互/调试模式显示日志
66
+ const silent = !opts.interactive && !opts.debug && !opts.calibrate;
67
+ if (!silent) {
68
+ logger.banner();
69
+ logger.info(`Working dir: ${config.WORKING_DIR}`);
70
+ }
71
+
72
+ const agent = new YiyanAgent();
73
+
74
+ const shutdown = async (code = 0) => {
75
+ if (!silent) logger.info('Shutting down...');
76
+ try { await agent.shutdown(); } catch {}
77
+ process.exit(code);
78
+ };
79
+
80
+ process.on('SIGINT', () => shutdown(0));
81
+ process.on('SIGTERM', () => shutdown(0));
82
+
83
+ if (opts.calibrate) {
84
+ await agent.init();
85
+ await agent.browser.dumpDebugInfo();
86
+ await agent.browser.screenshot();
87
+ await shutdown(0);
88
+ }
89
+
90
+ if (!opts.interactive && !opts.task) opts.interactive = true;
91
+
92
+ try {
93
+ await agent.init();
94
+ } catch (err) {
95
+ if (silent) console.log(JSON.stringify({ question: opts.task || '', answer: `Error: ${err.message}`, status: 'error' }));
96
+ else logger.error(`Failed: ${err.message}`);
97
+ process.exit(1);
98
+ }
99
+
100
+ try {
101
+ if (opts.interactive) {
102
+ await agent.runInteractive();
103
+ } else {
104
+ const result = await agent.run(opts.task);
105
+ // 单任务:只输出JSON
106
+ console.log(JSON.stringify(result, null, 2));
107
+ }
108
+ } catch (err) {
109
+ console.log(JSON.stringify({ question: opts.task || '', answer: `Error: ${err.message}`, status: 'error' }));
110
+ }
111
+
112
+ await shutdown(0);
113
+ }
114
+
115
+ main();
package/src/logger.js ADDED
@@ -0,0 +1,118 @@
1
+ // src/logger.js — ANSI-colored terminal output (no dependencies)
2
+ 'use strict';
3
+
4
+ const A = {
5
+ reset : '\x1b[0m',
6
+ bold : '\x1b[1m',
7
+ dim : '\x1b[2m',
8
+ red : '\x1b[31m',
9
+ green : '\x1b[32m',
10
+ yellow : '\x1b[33m',
11
+ blue : '\x1b[34m',
12
+ magenta : '\x1b[35m',
13
+ cyan : '\x1b[36m',
14
+ white : '\x1b[37m',
15
+ gray : '\x1b[90m',
16
+ lred : '\x1b[91m',
17
+ lgreen : '\x1b[92m',
18
+ lyellow : '\x1b[93m',
19
+ lblue : '\x1b[94m',
20
+ lmagenta: '\x1b[95m',
21
+ lcyan : '\x1b[96m',
22
+ };
23
+
24
+ const c = (code, text) => `${A[code]}${text}${A.reset}`;
25
+ const cb = (code, text) => `${A.bold}${A[code]}${text}${A.reset}`;
26
+
27
+ // ── Helpers ──────────────────────────────────────────────────────────────────
28
+ function truncDisplay(str, max = 400) {
29
+ if (!str) return '';
30
+ const s = String(str);
31
+ if (s.length <= max) return s;
32
+ return s.slice(0, max) + c('gray', `… (+${s.length - max} chars)`);
33
+ }
34
+
35
+ function jsonPreview(obj, max = 350) {
36
+ const s = JSON.stringify(obj, null, 2);
37
+ return truncDisplay(s, max);
38
+ }
39
+
40
+ // ── Public logger API ─────────────────────────────────────────────────────────
41
+ const logger = {
42
+ banner() {
43
+ console.log(`
44
+ ${c('cyan','╔══════════════════════════════════════════════════╗')}
45
+ ${c('cyan','║')} ${cb('lcyan','🤖 Yiyan Browser Agent (文心一言)')} ${c('cyan','║')}
46
+ ${c('cyan','║')} ${c('gray','AI Coding Agent via Browser Automation')} ${c('cyan','║')}
47
+ ${c('cyan','║')} ${c('gray','No API key needed — uses yiyan.baidu.com')} ${c('cyan','║')}
48
+ ${c('cyan','╚══════════════════════════════════════════════════╝')}
49
+ `);
50
+ },
51
+
52
+ header(msg) {
53
+ const line = '─'.repeat(50);
54
+ console.log(`\n${c('blue', line)}`);
55
+ console.log(`${c('bold','📋 ')}${cb('white', msg)}`);
56
+ console.log(`${c('blue', line)}\n`);
57
+ },
58
+
59
+ info(msg) { console.log(`${c('lblue',' ℹ ')} ${msg}`); },
60
+ success(msg) { console.log(`${c('lgreen',' ✓ ')} ${c('lgreen', msg)}`); },
61
+ warn(msg) { console.log(`${c('lyellow',' ⚠ ')} ${c('lyellow', msg)}`); },
62
+ error(msg) { console.log(`${c('lred',' ✗ ')} ${c('lred', msg)}`); },
63
+ dim(msg) { console.log(`${A.dim} ${msg}${A.reset}`); },
64
+
65
+ /** Spinner-style line (overwrites itself with \r) */
66
+ thinking(msg) {
67
+ process.stdout.write(` ${c('cyan','⟳')} ${c('gray', msg)}\r`);
68
+ },
69
+
70
+ /** Clear the current line */
71
+ clearLine() {
72
+ process.stdout.write(`\r${' '.repeat(80)}\r`);
73
+ },
74
+
75
+ // ── Tool call display ───────────────────────────────────────────────────────
76
+ toolCall(name, args) {
77
+ console.log(`\n ${cb('magenta','⚡ TOOL CALL')} ${c('cyan', `→ ${name}`)}`);
78
+ const preview = jsonPreview(args);
79
+ if (preview.trim()) {
80
+ preview.split('\n').forEach(l => console.log(` ${c('gray', l)}`));
81
+ }
82
+ },
83
+
84
+ toolResult(result, isError = false) {
85
+ const icon = isError ? c('lred',' ✗ Result:') : c('lgreen',' ✓ Result:');
86
+ const text = truncDisplay(String(result), 300);
87
+ const color = isError ? 'lred' : 'gray';
88
+ console.log(`${icon}`);
89
+ text.split('\n').slice(0, 12).forEach(l => console.log(` ${c(color, l)}`));
90
+ if (String(result).split('\n').length > 12) {
91
+ console.log(` ${c('gray',' … (truncated for display)')}`);
92
+ }
93
+ console.log('');
94
+ },
95
+
96
+ // ── Final agent output ──────────────────────────────────────────────────────
97
+ finalOutput(msg) {
98
+ const line = '━'.repeat(50);
99
+ console.log(`\n${c('lgreen', line)}`);
100
+ console.log(`${cb('lgreen','✅ TASK COMPLETE')}`);
101
+ console.log(`${c('lgreen', line)}\n`);
102
+ console.log(msg);
103
+ console.log('');
104
+ },
105
+
106
+ // ── Section separator ───────────────────────────────────────────────────────
107
+ separator(label = '') {
108
+ const pad = label ? ` ${label} ` : '';
109
+ console.log(`\n${c('gray', '·'.repeat(20) + pad + '·'.repeat(20))}\n`);
110
+ },
111
+
112
+ // ── Iteration marker ────────────────────────────────────────────────────────
113
+ iteration(n, max) {
114
+ console.log(`\n${c('gray',' ┄')} ${c('dim',`Step ${n}/${max}`)} ${c('gray','┄')}`);
115
+ },
116
+ };
117
+
118
+ module.exports = logger;