@glittering0717/ai-browser 2.5.0 → 2.5.1

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/README.md CHANGED
@@ -15,7 +15,7 @@
15
15
  <img alt="MIT" src="https://img.shields.io/badge/license-MIT-blue.svg">
16
16
  <img alt="Electron" src="https://img.shields.io/badge/built%20with-Electron%2033-9cf">
17
17
  <img alt="Platforms" src="https://img.shields.io/badge/platforms-macOS%20%E2%80%A2%20Linux-blueviolet">
18
- <img alt="Tests" src="https://img.shields.io/badge/tests-1309%20unit%20%2B%20831%20e2e-2ea043">
18
+ <img alt="Tests" src="https://img.shields.io/badge/tests-1324%20unit%20%2B%20831%20e2e-2ea043">
19
19
  <img alt="MCP" src="https://img.shields.io/badge/spec-MCP%20(stdio)-f5b23b">
20
20
  </p>
21
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glittering0717/ai-browser",
3
- "version": "2.5.0",
3
+ "version": "2.5.1",
4
4
  "description": "AI-native browser — renders pixels for humans, structured semantic-tree API for agents. No screenshots, no OCR, no coordinate guessing.",
5
5
  "main": "src/main/index.js",
6
6
  "bin": {
package/src/main/index.js CHANGED
@@ -451,13 +451,19 @@ if (!gpuDisabled && gpuPref !== 'on') {
451
451
  }
452
452
  if (gpuDisabled) app.disableHardwareAcceleration();
453
453
 
454
- // Disable Chromium sandbox — required when launched from restricted
455
- // environments (e.g. TRAE IDE shell) where the sandbox helper cannot
456
- // acquire the privileges it needs, causing "sandbox initialization failed:
457
- // Operation not permitted" and immediate app exit. Safe here because we
458
- // already run with contextIsolation:true + nodeIntegration:false, so
459
- // renderer code has no Node access regardless of OS-level sandbox.
460
- app.commandLine.appendSwitch('no-sandbox');
454
+ // Chromium 的 OS 级沙箱**默认关闭**:受限环境(例如 IDE 的集成终端 / TRAE 的
455
+ // shell)里 sandbox helper 拿不到它需要的权限,表现为 "sandbox initialization
456
+ // failed: Operation not permitted" 后立刻退出 —— 那是一个"装上就打不开"的
457
+ // 故障,比沙箱带来的纵深防御更急。
458
+ //
459
+ // 但它不再是**无法打开**的:`AI_BROWSER_SANDBOX=1` 就不加这个开关,让
460
+ // Chromium 用自己的沙箱。前提是启动环境允许 helper 工作(否则启动即退),
461
+ // 所以默认仍然关着 —— 这是权衡,不是"忘了"。
462
+ //
463
+ // 注:渲染器侧无论如何都没有 Node(contextIsolation:true + nodeIntegration:false),
464
+ // 这一位管的是"渲染进程被攻破之后还能不能碰到系统"。
465
+ const wantOsSandbox = process.env.AI_BROWSER_SANDBOX === '1';
466
+ if (!wantOsSandbox) app.commandLine.appendSwitch('no-sandbox');
461
467
 
462
468
  // Keep renderers fully active even when the window is occluded (e.g. an IDE
463
469
  // fullscreen window sits on top of the browser). By default Chromium
@@ -1424,10 +1430,26 @@ async function persistSessionCookies() {
1424
1430
  // 每 10 个停 100ms,让每一批都有机会落盘。
1425
1431
  if (i % 10 === 9) await new Promise((r) => setTimeout(r, 100));
1426
1432
  }
1427
- // 最后一组的 grouped commit 也要时间,等 2 秒再让 cleanupAndQuit 继续。
1433
+ // 最后强制落盘。`cookies.set()` resolve 只代表"已经排进 store":Chromium 的
1434
+ // cookie store 是 grouped commit,提交永远追不上(实测 86 个 set 全部"成功"
1435
+ // 但磁盘只剩 2 个,2026-10-19)。此前这里靠"固定睡 2 秒"碰运气,退出因此
1436
+ // 白等 2s 而且不保证成功;`flushStore()` 正是为此存在的原语:强制写盘并
1437
+ // 等它完成。老版本 Electron 没有这个 API 时退回原来的等待。
1428
1438
  if (n > 0) {
1429
- await new Promise((r) => setTimeout(r, 2000));
1430
- console.log(`[index] persisted ${n} session cookies before quit`);
1439
+ let flushed = false;
1440
+ try {
1441
+ if (ses.cookies && typeof ses.cookies.flushStore === 'function') {
1442
+ await ses.cookies.flushStore();
1443
+ flushed = true;
1444
+ }
1445
+ } catch (e) {
1446
+ console.error('[index] cookie flushStore failed:', e && e.message);
1447
+ }
1448
+ if (!flushed) await new Promise((r) => setTimeout(r, 2000));
1449
+ // 留一点余量给紧随其后的进程退出路径(不参与正确性,只是别让写盘和
1450
+ // 销毁撞在同一毫秒)。有了 flushStore 之后这 2s 不再需要。
1451
+ await new Promise((r) => setTimeout(r, 300));
1452
+ console.log(`[index] persisted ${n} session cookies before quit (flushStore=${flushed})`);
1431
1453
  }
1432
1454
  } catch (e) {
1433
1455
  console.error('[index] persistSessionCookies failed:', e && e.message);
@@ -207,6 +207,10 @@ function keySpec(name, extraModifiers) {
207
207
  class PageManager {
208
208
  constructor(browserWindow) {
209
209
  this.window = browserWindow;
210
+ // preload 的路径**必须在这里有默认值**:index.js 也会赋值(同一个路径),
211
+ // 但此前只有它赋值 —— 换个入口(单测、脚本、未来的新窗口)就 `undefined`,
212
+ // 而 `preload: undefined` 不报任何错,只表现为"页面读不到树、全超时"。
213
+ this._preloadPath = path.join(__dirname, '../preload/bridge.cjs');
210
214
  this.tabs = new Map(); // tabId -> BrowserView
211
215
  this.activeTab = null;
212
216
  // 启动时那一个标签页。它存在的唯一用途是判断"人眼前这一页是不是还没人动过":
@@ -3828,6 +3832,20 @@ return {ok:true,name:file.name,size:file.size,via:via,host_sel:hostSel,clicked:c
3828
3832
  * agent 侧由 ws_server 显式传 false(见 shared/tab_route.cjs 的说明)。
3829
3833
  */
3830
3834
  newTab(url = null, opts = {}) {
3835
+ // 缺 preload 的症状是"每一条读树都超时",而没有一条错误信息指向它。
3836
+ // 这里做一次性自检并落盘:宁可多一条 error_log,也不要让人从超时反推。
3837
+ if (this._preloadReported !== true) {
3838
+ let ok = false;
3839
+ try { ok = !!this._preloadPath && fs.existsSync(this._preloadPath); } catch (_e) { ok = false; }
3840
+ if (!ok) {
3841
+ this._preloadReported = true;
3842
+ console.error('[page_manager] preload missing:', this._preloadPath);
3843
+ recordError('preload_missing', {
3844
+ path: String(this._preloadPath),
3845
+ hint: 'Views will load but no bridge is injected — every get_tree/evaluate/act will time out. Check that src/preload/bridge.cjs is packaged.',
3846
+ });
3847
+ }
3848
+ }
3831
3849
  const tabId = ++this.tabCounter;
3832
3850
  const self = this;
3833
3851
  const view = new BrowserView({
@@ -311,6 +311,32 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null,
311
311
  //
312
312
  // `mode`('diff')只在 AX 读法里有意义。此前默认读法会**静默忽略**
313
313
  // 它,调用方拿到的还是整棵树却以为拿到了增量 —— 宁可报错说清楚。
314
+ // 枚举必须先校验。`subset` 只认 'interactive'|'full',此前传
315
+ // 'all'(wrapper_hint 就一直这么建议)或任意拼错都会**静默返回全量**
316
+ // ——调用方以为省了 token,实际一句没省,而且没有任何信号。这和
317
+ // "参数根本不存在"是同一类失败,必须报错。
318
+ const SUBSETS = ['interactive', 'full'];
319
+ if (params.subset !== undefined && params.subset !== null && SUBSETS.indexOf(params.subset) < 0) {
320
+ send({
321
+ jsonrpc: '2.0', id,
322
+ error: {
323
+ code: -32602,
324
+ message: `get_tree: unknown subset ${JSON.stringify(params.subset)} — use one of: ${SUBSETS.join(' / ')}`,
325
+ },
326
+ });
327
+ break;
328
+ }
329
+ const MODES = ['diff', 'full'];
330
+ if (params.mode !== undefined && params.mode !== null && MODES.indexOf(params.mode) < 0) {
331
+ send({
332
+ jsonrpc: '2.0', id,
333
+ error: {
334
+ code: -32602,
335
+ message: `get_tree: unknown mode ${JSON.stringify(params.mode)} — use one of: ${MODES.join(' / ')}`,
336
+ },
337
+ });
338
+ break;
339
+ }
314
340
  if (params.mode && !params.ax) {
315
341
  send({
316
342
  jsonrpc: '2.0', id,
@@ -801,6 +827,30 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null,
801
827
  });
802
828
  break;
803
829
  }
830
+ // 必填参数也要先校验:`button_enabled` 不传 target、`text_*` 不传 text
831
+ // 时,旧代码会在轮询里拿一个恒不成立的条件跑满 timeout,最后回
832
+ // `satisfied:false`。调用方读到的是"条件没出现",而真相是"你少传了一个
833
+ // 参数"——这两种情况要付出的代价完全不同(一个该重试,一个该改代码)。
834
+ const NEEDS = {
835
+ button_enabled: 'target',
836
+ text_contains: 'text',
837
+ url_contains: 'text',
838
+ };
839
+ const needKey = NEEDS[params.condition];
840
+ if (needKey) {
841
+ const v = params[needKey];
842
+ const missing = v === undefined || v === null || (typeof v === 'string' && v === '');
843
+ if (missing) {
844
+ send({
845
+ jsonrpc: '2.0', id,
846
+ error: {
847
+ code: -32602,
848
+ message: `wait: condition '${params.condition}' requires ${needKey} (got ${JSON.stringify(v)})`,
849
+ },
850
+ });
851
+ break;
852
+ }
853
+ }
804
854
  // timeout_ms 必须钳制:调用方写 600000 会让这条 RPC 和一次 500ms 一轮的
805
855
  // 树抽取循环一直跑着,而 agent 那边通常早就超时放弃了。
806
856
  const timeoutMs = Math.max(100, Math.min(120000, Number(params.timeout_ms) || 10000));
@@ -2,4 +2,4 @@
2
2
  // Kept in JS (not imported from package.json) to avoid Node import-attribute
3
3
  // (JSON module) compatibility issues inside Electron. package.json "version"
4
4
  // is manually synced to this value; tests assert they match.
5
- export const VERSION = '2.5.0';
5
+ export const VERSION = '2.5.1';