yiyan-browser-agent 1.7.8 → 1.8.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/browser.js +36 -33
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yiyan-browser-agent",
3
- "version": "1.7.8",
3
+ "version": "1.8.0",
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/browser.js CHANGED
@@ -297,26 +297,49 @@ class YiyanBrowser {
297
297
  if (!appeared) logger.warn('Response may have been delayed — continuing to wait...');
298
298
 
299
299
  // Phase 2: wait for completion
300
- // 条件: dialogCardBottom 出现 + 连续两次内容稳定
300
+ // 条件: dialogCardBottom 出现 + answer稳定 + 思考区域稳定
301
301
  let lastText = '';
302
+ let lastThinkingText = '';
302
303
  let stableCount = 0;
303
- let lastCheckTime = Date.now(); // 在 Phase 2 开始时重新设置
304
+ let thinkingStableCount = 0;
305
+ let lastCheckTime = Date.now();
304
306
  let dotCount = 0;
305
307
  let hasCompletionMarker = false;
306
308
 
307
309
  while (Date.now() - start < timeout) {
308
310
  const text = await this._extractLastMessage();
309
311
 
310
- // ── 稳定性检测:内容不变则计数 ──
312
+ // ── 思考区域内容检测 ──
313
+ const thinkingInfo = await this.page.evaluate(() => {
314
+ const thinkingEl = document.querySelector('.container__SPpahQHm, [class*="container__SPpah"]');
315
+ if (!thinkingEl) return { text: '', exists: false };
316
+
317
+ const s = window.getComputedStyle(thinkingEl);
318
+ const isVisible = s.display !== 'none' && s.visibility !== 'hidden';
319
+ const text = isVisible ? (thinkingEl.innerText || '') : '';
320
+
321
+ return { text, exists: true };
322
+ });
323
+
324
+ // ── 思考区域稳定性检测 ──
325
+ if (thinkingInfo.text === lastThinkingText && thinkingInfo.text.length > 5) {
326
+ if (Date.now() - lastCheckTime >= stableDelay) {
327
+ thinkingStableCount++;
328
+ logger.dim(`Thinking stable: ${thinkingStableCount}/2 (${thinkingInfo.text.length} chars)`);
329
+ }
330
+ } else if (thinkingInfo.text !== lastThinkingText) {
331
+ lastThinkingText = thinkingInfo.text;
332
+ thinkingStableCount = 0;
333
+ lastCheckTime = Date.now();
334
+ }
335
+
336
+ // ── answer 稳定性检测 ──
311
337
  if (text === lastText && text.length > 50) {
312
- // 内容没变化,检查是否达到稳定时间
313
338
  if (Date.now() - lastCheckTime >= stableDelay) {
314
339
  stableCount++;
315
- lastCheckTime = Date.now();
316
- logger.dim(`Stable count: ${stableCount}/2 (${text.length} chars)`);
340
+ logger.dim(`Answer stable: ${stableCount}/2 (${text.length} chars)`);
317
341
  }
318
- } else {
319
- // 内容变化,重置
342
+ } else if (text !== lastText) {
320
343
  lastText = text;
321
344
  stableCount = 0;
322
345
  lastCheckTime = Date.now();
@@ -344,30 +367,9 @@ class YiyanBrowser {
344
367
  hasCompletionMarker = true;
345
368
  }
346
369
 
347
- // ── 完成判断:标记出现 + 连续两次稳定 + 思考区域已消失 ──
348
- if (hasCompletionMarker && stableCount >= 2) {
349
- // 检查思考区域是否存在且有内容
350
- const thinkingStatus = await this.page.evaluate(() => {
351
- const thinkingEl = document.querySelector('.container__SPpahQHm, [class*="container__SPpah"]');
352
- if (!thinkingEl) return { exists: false, hasContent: false };
353
-
354
- const s = window.getComputedStyle(thinkingEl);
355
- const isVisible = s.display !== 'none' && s.visibility !== 'hidden';
356
- const text = thinkingEl.innerText || '';
357
- const hasContent = text.length > 5;
358
-
359
- return { exists: true, isVisible, hasContent };
360
- });
361
-
362
- // 思考区域存在且可见且有内容 → 还在思考,继续等待
363
- if (thinkingStatus.exists && thinkingStatus.isVisible && thinkingStatus.hasContent) {
364
- stableCount = 0;
365
- lastCheckTime = Date.now();
366
- logger.dim('Thinking in progress, continue waiting...');
367
- continue;
368
- }
369
-
370
- // 思考区域已消失/不可见/无内容 → 检查 _isGenerating
370
+ // ── 完成判断:标记出现 + answer稳定 + 思考区域稳定 ──
371
+ if (hasCompletionMarker && stableCount >= 2 && thinkingStableCount >= 2) {
372
+ // 思考区域和答案都已稳定,检查 _isGenerating 作为最终确认
371
373
  if (!await this._isGenerating()) {
372
374
  await this.page.waitForTimeout(500);
373
375
  logger.clearLine();
@@ -376,6 +378,7 @@ class YiyanBrowser {
376
378
  }
377
379
  // _isGenerating 说还在生成,重置计数继续等待
378
380
  stableCount = 0;
381
+ thinkingStableCount = 0;
379
382
  lastCheckTime = Date.now();
380
383
  }
381
384
 
@@ -472,7 +475,7 @@ class YiyanBrowser {
472
475
  return result.trim();
473
476
  }
474
477
 
475
- // ── 仅从 answer_text_id 提取内容,不使用任何 fallback ──
478
+ // ── answer_text_id 提取内容 ──
476
479
  const answerEl = document.querySelector('#answer_text_id');
477
480
  if (answerEl) {
478
481
  return getFullText(answerEl);