@glittering0717/ai-browser 2.3.0 → 2.4.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.
@@ -49,6 +49,12 @@ export function buildCanvasHookSource(opts = {}) {
49
49
  var scheduled = false;
50
50
  var lastSig = null;
51
51
  var lastEntry = null;
52
+ // 帧指纹(见 push 里的注释)。n = 记录到的绘制调用总数,fp = 自上次清屏以来的
53
+ // 绘制序列哈希,frames = 见过的清屏次数。三个数一起构成"画布上现在是什么"的
54
+ // 廉价摘要 —— 用它回答"变了没有",不用它回答"画的是什么"。
55
+ // last = 最近一个**完整帧**的内容哈希(帧边界时提交),hasLast 区分"提交过 0"(空帧)
56
+ // 与"还没提交过"。为什么要它,见 push 里的帧边界注释。
57
+ var FP = { n: 0, fp: 0, frames: 0, last: 0, hasLast: 0, dirty: 0 };
52
58
 
53
59
  // ---- 小工具 ----------------------------------------------------------
54
60
 
@@ -176,6 +182,72 @@ export function buildCanvasHookSource(opts = {}) {
176
182
  var sig;
177
183
  try { sig = JSON.stringify(rec); } catch (e) { return; }
178
184
  if (!sig) return;
185
+ // ---- 帧指纹 ----------------------------------------------------------
186
+ // 用途是**变化检测**,不是重建场景:一个纯 canvas 应用(Excalidraw / 白板 /
187
+ // 数据网格)的写入在 DOM 上常常零字节变化,于是 page_probe 与 ui.diff 会
188
+ // 回答 unchanged —— 对"我真的画了一条线"这种事实给出假阴性,而这正是本工具
189
+ // 唯一的赌注。
190
+ //
191
+ // 只累积**会改变像素**的记录。meta(getContext / resize / transfer)与
192
+ // state(save / restore / translate)改的是上下文状态,不是像素;让它们进去,
193
+ // 等于"一个像素都没画也算变更" —— 实测 ECharts / 广告位 / 1x1 反爬 canvas
194
+ // 都是"每次 render 调一次 getContext 但可能什么都不画",那是纯粹的假阳性源。
195
+ //
196
+ // 语义:fp = "自上次帧边界以来,绘制序列的哈希"。帧边界 = 整屏清屏或整屏铺底。
197
+ // 于是**同一段绘制序列得到同一个 fp**,多画一个形状就得到不同的 fp。
198
+ //
199
+ // 两条设计边界,都是踩过之后定下来的:
200
+ //
201
+ // 1. **不要把 n / frames 拼进被比较的值。** 它们单调递增,于是"内容逐字节
202
+ // 相同的重绘"(时钟、光标、动画循环、canvas 视频)也会让比较结果变化,
203
+ // 判定退化成"永远为真" —— 恰好把 page_probe 存在的意义(抓"点了没反应")
204
+ // 反过来废掉。接入层曾经写成 fp:n:frames 的拼接串,就是这么坏的:
205
+ // hook 里的 fp 有单测钉住是对的,产品用的是它的包装,两边各自都"对",
206
+ // 合起来是错的。
207
+ //
208
+ // 2. **不要把"已完成的帧"缓存起来只暴露上一帧。** 试过,退回了:静态页面上
209
+ // 一次写入只产生**一帧**,而且那一帧要等到下次清屏才算"完成" —— 于是
210
+ // 用户刚画的那个矩形永远看不见,正好是本改动要修的那个缺陷。
211
+ //
212
+ // 代价是采样可能落在帧中间,读到一个"前缀哈希"。这个方向的错误是**安全的**:
213
+ // 同前缀 → 迄今画出的像素确实一样;不同前缀 → 报"变了"(假阳性),而假阳性
214
+ // 由 page_probe 的空闲标定兜住(见 page_probe.js 的 canvasIdle)。反过来
215
+ // 若为了消掉假阳性而做缓存/延迟提交,代价是引入假阴性,而假阴性没有任何兜底。
216
+ try {
217
+ FP.n++;
218
+ var fk = rec.k;
219
+ if (fk !== "meta" && fk !== "state") {
220
+ if (fk === "clear" || rec.fb === 1) {
221
+ // 提交上一帧(归零前)—— 这是"完整帧"的唯一来源。
222
+ //
223
+ // 为什么需要它:上面那个 fp 是"自上次清屏以来的前缀哈希",采样落在帧中间
224
+ // 就读到半成品。对**一直在重绘**的页面(动画、直播、协作者光标),这意味着
225
+ // 两次采样可能因为相位不同而报"变了",于是"这一手到底生效没有"永远判不了
226
+ // (只能老实回答"不可判")。而完整帧的哈希与采样相位无关:稳定重绘的页面
227
+ // 每帧都得到同一个 last,内容真变了立刻不同。
228
+ //
229
+ // 代价与边界:last 只在**下一个**帧边界才更新,所以静态页面(画完就停)
230
+ // 的 last 会滞后 —— 那种页面必须继续用 fp,不能拿 last 判。谁用哪个由
231
+ // page_probe 按"这一页在不在画"决定,这里只负责如实提供两个值。
232
+ // 只有"这一帧真的画过东西"才提交(dirty)。为什么不用"这是第几次清屏":
233
+ // 真实渲染器常常 clearRect + 整屏铺底连发两条记录(两条都是帧边界),
234
+ // 按次数提交会在**开帧时就**把"空的"提交上去 —— 实测静态页只画一帧时
235
+ // last 变成 0,看起来像空画布(CF-13 抓到的就是这个)。
236
+ //
237
+ // 一帧什么都没画就不提交:此刻"画布变空了"由 fp=0 表达,不需要 last。
238
+ FP.frames++;
239
+ if (FP.dirty) {
240
+ FP.last = FP.fp;
241
+ FP.hasLast = 1;
242
+ }
243
+ FP.fp = 0;
244
+ FP.dirty = 0;
245
+ } else {
246
+ FP.fp = ((FP.fp * 33) ^ hash32(sig)) >>> 0;
247
+ FP.dirty = 1;
248
+ }
249
+ }
250
+ } catch (e) {}
179
251
  if (mode !== "trace" && lastEntry && lastSig === sig) {
180
252
  lastEntry.n += 1;
181
253
  pendingBytes += sig.length;
@@ -289,10 +361,20 @@ export function buildCanvasHookSource(opts = {}) {
289
361
  b: rectBounds(m, x, y, w, h), st: styleOf(ctx)
290
362
  };
291
363
  // 全量 clear 等价于场景重置 → 新 epoch。
292
- if (method === "clearRect" && st.w > 0 && st.h > 0 &&
293
- x <= 0 && y <= 0 && (x + w) >= st.w && (y + h) >= st.h) {
364
+ var full = st.w > 0 && st.h > 0 &&
365
+ x <= 0 && y <= 0 && (x + w) >= st.w - 0.5 && (y + h) >= st.h - 0.5;
366
+ if (method === "clearRect" && full) {
294
367
  st.epoch += 1;
295
368
  rec.e = st.epoch;
369
+ } else if (method === "fillRect" && full) {
370
+ // 整屏填充 = 一帧的开始(fb = frame boundary)。
371
+ //
372
+ // 为什么需要这条:帧指纹在 clear 上归零,但**很多渲染器从不清屏**,直接用
373
+ // 背景色铺满整屏(本项目的测试夹具就是这样写的)。没有这条,fp 会从页面
374
+ // 打开起一直累积,变成"这个会话画过什么"的日志哈希 —— 它确实会变,但它
375
+ // 回答不了"现在这一帧和刚才那一帧是不是同一个"。有了这条,"整屏铺底 +
376
+ // 画内容"的渲染器也能得到逐帧可比的指纹。
377
+ rec.fb = 1;
296
378
  }
297
379
  push(rec);
298
380
  }
@@ -641,6 +723,32 @@ export function buildCanvasHookSource(opts = {}) {
641
723
  Object.defineProperty(G, KEY, { value: bridge, configurable: true, enumerable: false, writable: false });
642
724
  } catch (e) { try { G[KEY] = bridge; } catch (e2) {} }
643
725
 
726
+ // 固定名字的只读指纹口,给 page_probe / DEFAULT_SNAPSHOT_JS 用。
727
+ // 必须与 bridge 的 KEY 分开:KEY 是动态的、只有主进程知道,而这两处探针
728
+ // 是**页面内**执行的脚本,只能靠一个约定名字找到它。
729
+ // 不可枚举,避免被页面自己的 for-in / Object.keys(window) 扫到;
730
+ // recording=false 时返回 null(不是 0)—— "没在录"和"录了但什么都没画"
731
+ // 是两件事,用 0 表示前者会让调用方以为画布是空的。
732
+ //
733
+ // configurable:false:这一条是给**被观测页面**设的门槛。属性可配置时,页面
734
+ // 在 hook 之后可以用一次 defineProperty 把它换成返回常量的函数,于是所有
735
+ // canvas 写入都被判成"没变化" —— 验证能力被被验证方单方面关掉。
736
+ // 代价是同一页面里重复注入会抛错,但 hook 本身有 __aiCanvasHook 幂等短路,
737
+ // 不会走到第二次定义。
738
+ var fpApi = function () {
739
+ if (!recording) return null;
740
+ // last 用 null 表示"还没有完整帧"(而不是 0)—— 0 是一个合法的空帧哈希,
741
+ // 两者混在一起会让"画布刚被清空"和"从没画过"长得一样。
742
+ return { n: FP.n, fp: FP.fp, frames: FP.frames, last: FP.hasLast ? FP.last : null, on: 1 };
743
+ };
744
+ try {
745
+ Object.defineProperty(G, "__aiCanvasFp", {
746
+ value: fpApi, configurable: false, enumerable: false, writable: false,
747
+ });
748
+ } catch (e) {
749
+ try { G.__aiCanvasFp = fpApi; } catch (e2) {}
750
+ }
751
+
644
752
  try {
645
753
  if (typeof G.addEventListener === "function") {
646
754
  G.addEventListener("pagehide", flush);
@@ -232,8 +232,20 @@ export async function probeCapabilities(pageManager, opts = {}) {
232
232
  * canvas sites and would bury the actual API list.
233
233
  */
234
234
  async function collectEndpoints(pageManager, tid, limit) {
235
+ // 参数名是 `tab`,不是 `tabId`。
236
+ //
237
+ // 这里曾经写成 `{ tabId: tid, limit }`,而 NetworkMonitor.list 读的是
238
+ // `filter.tab` —— 于是**过滤条件静默失效**,返回的是所有标签页的请求。
239
+ // 实测(2026-10-29):在一条刚打开 excalidraw.com 的空白 profile 上,
240
+ // ui.capabilities 的 endpoints 里出现了 `/ztbox`、`/sugrec`(百度的接口),
241
+ // 而这个实例当时只有这一个页面。agent 会照着这份清单去 fetch 一个与本页
242
+ // 无关的接口,然后得到一份"看起来有数据"的错误结论。
243
+ //
244
+ // 同类错误在本项目已出现三次(`ui.evaluate` 的 js/expr、`read` 的
245
+ // uris/uri、这里的 tab/tabId):**同一个概念在两层用了不同名字,而调用方
246
+ // 不会报错,只会拿到别人的数据**。钉住它的是 CAP-EP-01..03。
235
247
  const res = await pageManager.networkMonitor.list
236
- ? await pageManager.networkMonitor.list({ tabId: tid, limit })
248
+ ? await pageManager.networkMonitor.list({ tab: tid, limit })
237
249
  : null;
238
250
  const reqs = (res && (res.requests || res.items || res)) || [];
239
251
  const seen = new Set();
@@ -491,7 +503,20 @@ const rich = flat.length >= 60 && roleSet.size >= 4 && actionable >= 10;
491
503
  preferred: false,
492
504
  why: `${totalCanvas} canvas element(s) (${topCanvas} top-level, ${frameCanvas} in frames) but only ${flat.length} semantic elements (${actionable} actionable) — do NOT keep trying coordinate gestures to hit canvas-drawn items; go find the app's data model instead`,
493
505
  evidence: { canvas_top: topCanvas, canvas_in_frames: frameCanvas, elements: flat.length, actionable },
494
- how: 'ui.canvas list/read can still extract text drawn via fillText (no OCR), but that is a fallback, not a primary path',
506
+ // 这句曾经承诺 "ui.canvas list/read can still extract text drawn via fillText"。
507
+ // 实测(2026-10-29,Excalidraw):2D canvas 也可以**一次 fillText 都没有** ——
508
+ // 整帧 clearRect + Path2D + 离屏合成,于是 texts[] 是空的。照着旧承诺去
509
+ // "读文本"的 agent 会得到空数组,然后判定"这个画布是空的",而画布上其实
510
+ // 画满了图元。承诺要么给条件,要么不给。
511
+ how: 'ui.canvas_read lists draw calls; texts[] holds only strings drawn with fillText. '
512
+ + 'Full-frame repaint renderers (Excalidraw-class) log only clearRect/fillRect, so an '
513
+ + 'EMPTY texts[] does NOT mean an empty canvas. Canvas is write-verifiable (ui.act '
514
+ + 'canvas_changed / canvas_idle / canvas_repeat, ui.diff canvas); to READ content go to '
515
+ + 'the L1 route or the app\'s own export. Read canvas_changed carefully: false means '
516
+ + '"proven unchanged", null means "this page cannot be judged" (it repaints different '
517
+ + 'content every frame, or the hook has no whole frame) — never treat null as false. '
518
+ + 'canvas_repeat:true means the page repaints identical content, so the verdict is '
519
+ + 'still trustworthy',
495
520
  });
496
521
  }
497
522
 
package/src/main/index.js CHANGED
@@ -12,6 +12,7 @@ import { parseWatchdogTargets, isWatchdogEnabled } from '../shared/watchdog.js';
12
12
  import { wrapSetActive } from '../shared/tab_hooks.js';
13
13
  import { initErrorLog, recordError, markShutdown, getLogPath } from './error_log.js';
14
14
  import errorBanner from '../shared/error_banner.cjs';
15
+ import { classifyShowReason, describeShowUrl } from '../shared/window_show.js';
15
16
 
16
17
  const __filename = fileURLToPath(import.meta.url);
17
18
  const __dirname = path.dirname(__filename);
@@ -390,19 +391,94 @@ if (gotLock) {
390
391
  //
391
392
  // app.dock.setIcon 是 Electron 提供的运行时入口,不影响签名与打包。
392
393
  // Windows/Linux 走 BrowserWindow 的 icon 选项(见 createWindow)。
393
- function appIconPath() {
394
- const p = path.join(__dirname, '..', '..', 'build', 'icon.png');
395
- try { return fs.existsSync(p) ? p : null; } catch (_) { return null; }
394
+ //
395
+ // ⚠️ 这里必须用 build/icon-dock.png,不能用 build/icon.png。
396
+ // macOS 26/27 给 bundle 里的 icns 套了一层统一图标网格(内容缩到约 86.7%),
397
+ // 而 dock.setIcon 的裸图**不套**这层网格 —— 直接塞满出血的 icon.png,Dock
398
+ // 里就会比旁边所有图标大一圈。而打包版走的是 Info.plist 的 icns,正好是
399
+ // 缩过的 86.7%。于是"从 /Applications 启动"和"npm start"画出来两个尺寸,
400
+ // 用户看到的就是图标一会大一会小。icon-dock.png 由 render_icon.cjs 预先按
401
+ // 86.7% 缩好,两条路径才落到同一个尺寸(契约见 tests/test_icon_geometry.js)。
402
+ function appIconPath(kind = 'window') {
403
+ const base = path.join(__dirname, '..', '..', 'build');
404
+ const pick = (f) => {
405
+ const p = path.join(base, f);
406
+ try { return fs.existsSync(p) ? p : null; } catch (_) { return null; }
407
+ };
408
+ if (kind === 'dock') {
409
+ // 先要按网格缩过的;退而求其次用满出血那张(会偏大,但总比 Electron 火箭强),
410
+ // 并把 fallback 记进 appIconState —— 让"为什么偏大"能一眼看出来。
411
+ const dock = pick('icon-dock.png');
412
+ if (dock) return { file: dock, fallback: false };
413
+ const plain = pick('icon.png');
414
+ return plain ? { file: plain, fallback: true } : null;
415
+ }
416
+ return { file: pick('icon.png'), fallback: false };
396
417
  }
397
418
 
419
+ // 把"实际设进去的图有多大"量出来报给外部(ui.server_info.icon)。
420
+ // 只要这个数字偏离系统网格,"图标大小不对"就能被直接判定,而不是靠肉眼比。
421
+ function measureIcon(file) {
422
+ try {
423
+ const img = nativeImage.createFromPath(file);
424
+ if (img.isEmpty()) return null;
425
+ // 不缩放:缩放会让边缘抗锯齿多出一两像素,量出来的占比偏高(实测 256px 时
426
+ // 86.7% → 87.5%)。这里要的是"这张图本身"的数字,1024² 全扫一遍也就几毫秒。
427
+ const { width, height } = img.getSize();
428
+ const bmp = img.toBitmap(); // BGRA,alpha 在第 4 字节
429
+ let minX = width, minY = height, maxX = -1, maxY = -1;
430
+ for (let y = 0; y < height; y++) {
431
+ for (let x = 0; x < width; x++) {
432
+ if (bmp[(y * width + x) * 4 + 3] > 2) {
433
+ if (x < minX) minX = x;
434
+ if (x > maxX) maxX = x;
435
+ if (y < minY) minY = y;
436
+ if (y > maxY) maxY = y;
437
+ }
438
+ }
439
+ }
440
+ if (maxX < 0) return { content_ratio: 0 };
441
+ return {
442
+ pixels: `${width}x${height}`,
443
+ content_ratio: +((maxX - minX + 1) / width).toFixed(3),
444
+ };
445
+ } catch (e) {
446
+ return { error: e.message };
447
+ }
448
+ }
449
+
450
+ let appIconState = null;
451
+
398
452
  function applyAppIcon() {
399
- if (process.platform !== 'darwin') return false; // 其他平台由窗口 icon 选项负责
400
- const icon = appIconPath();
401
- if (!icon) return false; // 装的是精简包(没带 build/)→ 静默跳过,不值得启动失败
453
+ // 打包版:Info.plist 里已经是自定义 icns,系统会按图标网格渲染 —— 不要再动它。
454
+ // 反过来,源码启动时进程本体是 Electron.app,Dock 跟的是可执行文件,不设就是火箭。
455
+ if (process.platform !== 'darwin') {
456
+ appIconState = { source: 'none', reason: 'non-darwin' };
457
+ return false;
458
+ }
459
+ if (app.isPackaged) {
460
+ appIconState = { source: 'bundle', reason: 'packaged: Info.plist icns' };
461
+ return false;
462
+ }
463
+ const icon = appIconPath('dock');
464
+ if (!icon) { // 装的是精简包(没带 build/)→ 静默跳过,不值得启动失败
465
+ appIconState = { source: 'none', reason: 'build/icon-dock.png missing' };
466
+ return false;
467
+ }
402
468
  try {
403
- if (app.dock && typeof app.dock.setIcon === 'function') { app.dock.setIcon(icon); return true; }
469
+ if (app.dock && typeof app.dock.setIcon === 'function') {
470
+ app.dock.setIcon(icon.file);
471
+ appIconState = {
472
+ source: 'dock.setIcon',
473
+ file: path.basename(icon.file),
474
+ ...(icon.fallback ? { fallback: true, note: 'icon-dock.png 缺失,退回满出血图(会比系统图标大)' } : {}),
475
+ ...(measureIcon(icon.file) || {}),
476
+ };
477
+ return true;
478
+ }
404
479
  } catch (e) {
405
480
  console.error('[icon] dock.setIcon failed:', e && e.message);
481
+ appIconState = { source: 'error', error: e.message };
406
482
  }
407
483
  return false;
408
484
  }
@@ -416,7 +492,7 @@ function createWindow() {
416
492
  title: APP_DISPLAY_NAME,
417
493
  // Windows/Linux 的任务栏图标来自这里(macOS 走 app.dock.setIcon,
418
494
  // 见 applyAppIcon:Dock 跟的是进程可执行文件,不是窗口选项)。
419
- icon: appIconPath() || undefined,
495
+ icon: (appIconPath('window') || {}).file || undefined,
420
496
  // 先不显示:如何显示由 showInitialWindow() 按焦点策略决定 —— 窗口必须真实可见
421
497
  // (产品承诺人可随时介入),但被 agent 拉起时不该把用户正在做的事打断。
422
498
  show: false,
@@ -770,14 +846,26 @@ function showInitialWindow() {
770
846
  // "窗口在这一刻显示了、而页面是空的"这个事实本身也必须留下 —— 那正是要抓的
771
847
  // 白屏形态。若等 Promise 成功才记,renderer 起不来时记录永远是 null,
772
848
  // 观测口在最需要它的时候失灵。
849
+ // 那一刻 webContents 上到底是哪一页 —— 这决定了 show_reason 能不能说实话。
850
+ //
851
+ // 2026-10-24 起新标签页先加载 loading.html 占位页(把白屏换成"正在打开 <url>"),
852
+ // 而占位页**自己也会发 dom-ready**。于是"等到 dom-ready 才显示"这条承诺被它
853
+ // 架空了:记录里写着 dom-ready + waited_for_dom:true,而此刻页面是我们的占位页,
854
+ // 目标文档一个字节都没到(实测:外网不通时 body 恒为 578 字符)。
855
+ // 口径交给 shared/window_show.js 折算,那里有完整的来由与反例。
856
+ let curUrl = '';
857
+ try { curUrl = (wc && !wc.isDestroyed() && wc.getURL) ? String(wc.getURL() || '') : ''; } catch (_e) { curUrl = ''; }
858
+ const cls = classifyShowReason(reason, curUrl);
773
859
  const rec = {
774
860
  tab: pageManager.activeTab,
775
861
  body_len: null,
776
862
  ready_state: null,
777
- // 'dom-ready' | 'fail-load' | 'renderer-ready' | 'timeout'
778
- show_reason: reason || 'unknown',
779
- // 真话:只有 3s 兜底(renderer 迟迟没到)才是 false。
780
- waited_for_dom: reason !== 'timeout',
863
+ // 'dom-ready' | 'renderer-ready' | 'fail-load' | 'timeout' | 'placeholder'
864
+ show_reason: cls.show_reason,
865
+ // 真话:只有**目标文档**就绪才算等到了。占位页就绪不算(那是我们自己的页面)。
866
+ waited_for_dom: cls.waited_for_dom,
867
+ // 当时挂在 webContents 上的地址(占位页 URL 里带着目标地址,最有用的一半)。
868
+ show_url: describeShowUrl(curUrl),
781
869
  renderer_ready: false,
782
870
  };
783
871
  pageManager.windowShownAt = rec;
@@ -1053,6 +1141,9 @@ function normalizeUrlInput(input) {
1053
1141
  app.whenReady().then(() => {
1054
1142
  applyAppIcon();
1055
1143
  createWindow();
1144
+ // 图标这条链的"实际状态"挂到 pageManager 上,由 ui.server_info 报出去 ——
1145
+ // "图标大小不对"否则只能靠肉眼比;有了 content_ratio 就能判定(期望 ≈0.867)。
1146
+ if (pageManager) pageManager.appIcon = appIconState;
1056
1147
 
1057
1148
  // === 自检钩子:AI_BROWSER_SELFTEST=crash ===
1058
1149
  // 主动制造一次主进程未捕获异常,用来验证"崩溃时的日志与横幅"这条链路真的通。
@@ -211,8 +211,14 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
211
211
  // 这是"多 agent / 无头式运行"的默认语义,见 shared/tab_route.cjs。
212
212
  const params = { url: args.url || null };
213
213
  if (args.show !== undefined) params.show = args.show === true;
214
- const result = await wsClient.call('ui.new_tab', params);
215
- return { content: [{ type: 'text', text: JSON.stringify(result) }] };
214
+ try {
215
+ const result = await wsClient.call('ui.new_tab', params);
216
+ return { content: [{ type: 'text', text: JSON.stringify(result) }] };
217
+ } catch (e) {
218
+ // group-full 这类执行期错误必须回到 agent 为止,以 isError 工具结果
219
+ // 呈现(tool-result-form),而不是逃成 JSON-RPC protocol error。
220
+ return { content: [{ type: 'text', text: `Error: ${e.message}` }], isError: true };
221
+ }
216
222
  }
217
223
  if (op === 'close') {
218
224
  const result = await wsClient.call('ui.close_tab', { tab: args.tab });
@@ -220,8 +226,14 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
220
226
  }
221
227
  if (op === 'switch') {
222
228
  // 只换目标、不抢视野:agent 说"切到这个标签页"通常只是想说
223
- // "接下来都在这页上干活",不该把人的窗口内容换掉。
224
- const result = await wsClient.call('ui.set_active_tab', { tab: args.tab, show: false });
229
+ // "接下来都在这页上干活",不该把人的窗口内容换掉。但 agent 自己主动
230
+ // 要看(比如交代"请在浏览器里看这个")时,可以传 show:true —— 这正是
231
+ // tasks.md 里 "switch tab show:true" 的用法:同时把这一页顶到用户眼前
232
+ // 并设为默认目标。
233
+ const result = await wsClient.call('ui.set_active_tab', {
234
+ tab: args.tab,
235
+ show: args.show === true,
236
+ });
225
237
  return { content: [{ type: 'text', text: JSON.stringify(result) }] };
226
238
  }
227
239
  if (op === 'window') {
@@ -17,7 +17,7 @@ export const MCP_TOOLS = [
17
17
  type: 'object',
18
18
  properties: {
19
19
  url: { type: 'string', description: 'URL to navigate to.' },
20
- tab: { type: 'integer', description: 'Tab id (default: active tab).' },
20
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' },
21
21
  wait_until: { type: 'string', enum: ['commit', 'domcontentloaded', 'load'], description: 'How far to wait. commit = navigation committed (fastest, content may not be painted); domcontentloaded = DOM ready (default, safe for SPAs); load = also subresources (can hang forever on pages that never fire load).' }
22
22
  },
23
23
  required: ['url']
@@ -35,7 +35,7 @@ export const MCP_TOOLS = [
35
35
  ax: { type: 'boolean', description: 'Use the AX read layer (links carry url).', default: false },
36
36
  subset: { type: 'string', enum: ['interactive', 'full'], description: 'Prune layout-only branches (token saver).' },
37
37
  mode: { type: 'string', enum: ['diff', 'full'], description: 'Return only new/hidden interactive nodes since last read.' },
38
- tab: { type: 'integer', description: 'Tab id (default: active tab).' }
38
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' }
39
39
  }
40
40
  }
41
41
  },
@@ -117,7 +117,7 @@ export const MCP_TOOLS = [
117
117
  button: { type: 'string', enum: ['left', 'middle', 'right'], description: 'Canvas apps pan on middle/right drag.' }
118
118
  }
119
119
  },
120
- tab: { type: 'integer', description: 'Tab id (default: active tab).' }
120
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' }
121
121
  },
122
122
  required: ['action']
123
123
  }
@@ -131,7 +131,7 @@ export const MCP_TOOLS = [
131
131
  type: 'object',
132
132
  properties: {
133
133
  js: { type: 'string', description: 'Page-context JS expression (not statements). Max 5000 chars. Promises are awaited.' },
134
- tab: { type: 'integer', description: 'Tab id (default: active tab).' },
134
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' },
135
135
  frame_ref: { type: 'string', description: 'Run inside this cross-process frame (an axf-... id from the tree). Pass it explicitly when touching iframe content: building the selector by string concatenation defeats the automatic route and the top-frame run returns null.' }
136
136
  },
137
137
  required: ['js']
@@ -148,7 +148,7 @@ export const MCP_TOOLS = [
148
148
  direction: { type: 'string', enum: ['up', 'down'], default: 'down' },
149
149
  amount: { type: 'integer', default: 500, description: 'Pixels (ignored if target set).' },
150
150
  target: { type: 'string', description: 'data-ai-id to scroll into view.' },
151
- tab: { type: 'integer', description: 'Tab id (default: active tab).' }
151
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' }
152
152
  }
153
153
  }
154
154
  },
@@ -164,7 +164,7 @@ export const MCP_TOOLS = [
164
164
  target: { type: 'string', description: 'Button label (button_enabled).' },
165
165
  text: { type: 'string', description: 'Text/URL fragment (text_contains / url_contains).' },
166
166
  timeout_ms: { type: 'integer', default: 10000 },
167
- tab: { type: 'integer', description: 'Tab id (default: active tab).' }
167
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' }
168
168
  },
169
169
  required: ['condition']
170
170
  }
@@ -177,7 +177,7 @@ export const MCP_TOOLS = [
177
177
  inputSchema: {
178
178
  type: 'object',
179
179
  properties: {
180
- tab: { type: 'integer', description: 'Tab id (default: active tab).' }
180
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' }
181
181
  }
182
182
  }
183
183
  },
@@ -189,7 +189,7 @@ export const MCP_TOOLS = [
189
189
  inputSchema: {
190
190
  type: 'object',
191
191
  properties: {
192
- tab: { type: 'integer', description: 'Tab id (default: active tab).' },
192
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' },
193
193
  format: { type: 'string', enum: ['png', 'jpeg', 'webp'], description: 'Image format. Default png. jpeg at quality 85 is ~5x smaller — prefer it for long pages.' },
194
194
  quality: { type: 'number', description: '0-100, jpeg/webp only (ignored for png). Default 85 for jpeg.' },
195
195
  fullPage: { type: 'boolean', description: 'Capture the full scrollable page, not just the viewport. Cannot be combined with clip.' },
@@ -210,7 +210,7 @@ export const MCP_TOOLS = [
210
210
  {
211
211
  name: 'browse_tabs',
212
212
  title: 'Tabs',
213
- description: 'Tabs: list/new/close/switch/window. Yours are grouped and capped (list: count/limit). Full → close one. switch = default target. new never steals the view unless show:true. window = hide/show.',
213
+ description: 'Tabs: list/new/close/switch/window. Yours are grouped and capped. switch = default; add show:true. new never steals the view unless show:true. window = hide/show.',
214
214
  // 合并前是 4 个工具(list / new / close / set_active),每个只承载一个动词。
215
215
  // 合并后 destructiveHint 只能是 false —— 否则 op=list 也会被标成破坏性操作;
216
216
  // close 的破坏性写在 description 里。这是合并的唯一语义代价,接受。
@@ -218,10 +218,10 @@ export const MCP_TOOLS = [
218
218
  inputSchema: {
219
219
  type: 'object',
220
220
  properties: {
221
- op: { type: 'string', enum: ['list', 'new', 'close', 'switch', 'window'], description: 'list = every tab; switch = set default; window = hide/show.' },
222
- tab: { type: 'integer', description: 'close / switch: the target tab id.' },
221
+ op: { type: 'string', enum: ['list', 'new', 'close', 'switch', 'window'], description: 'list = every tab; new = open; close = close; switch = default; window = hide/show.' },
222
+ tab: { type: 'integer', description: 'close / switch: the target tab id; omit → session default.' },
223
223
  url: { type: 'string', description: 'new: open at this URL (default about:blank).' },
224
- show: { type: 'boolean', description: 'new: bring it to the front (default: background).' },
224
+ show: { type: 'boolean', description: 'new: bring it to the front (default: background); switch: same (default: false).' },
225
225
  visible: { type: 'boolean', description: 'window: true = show, false = hide.' }
226
226
  },
227
227
  required: ['op']
@@ -281,7 +281,7 @@ export const MCP_TOOLS = [
281
281
  capture_bodies: { type: 'string', enum: ['none', 'api', 'all'] }
282
282
  }
283
283
  },
284
- tab: { type: 'integer', description: 'Tab id (default: active tab).' }
284
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' }
285
285
  },
286
286
  required: ['operation']
287
287
  }
@@ -295,7 +295,7 @@ export const MCP_TOOLS = [
295
295
  type: 'object',
296
296
  properties: {
297
297
  operation: { type: 'string', enum: ['list', 'read', 'configure', 'capture'], description: 'Operation.' },
298
- tab: { type: 'integer', description: 'Tab id (default: active tab).' },
298
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' },
299
299
  canvas_id: { type: 'string', description: 'read/capture: id from list.' },
300
300
  view: { type: 'string', enum: ['summary', 'calls'], description: 'read: summary (default) or raw draw calls.' },
301
301
  since_seq: { type: 'integer', description: 'read: cursor from pagination.next_seq.' },
@@ -320,7 +320,7 @@ export const MCP_TOOLS = [
320
320
  snapshot: { type: 'string', description: 'diff: the id returned by snapshot.' },
321
321
  rearm: { type: 'boolean', description: 'diff: make the current state the new baseline, so you can loop "assert no change" over many steps.' },
322
322
  forget: { type: 'boolean', description: 'diff: drop the stored baseline when done.' },
323
- tab: { type: 'integer', description: 'Tab id (default: active tab).' }
323
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' }
324
324
  },
325
325
  required: ['operation']
326
326
  }
@@ -335,7 +335,7 @@ export const MCP_TOOLS = [
335
335
  properties: {
336
336
  include_network: { type: 'boolean', description: 'Include observed JSON-ish endpoints (default true).' },
337
337
  network_limit: { type: 'integer', description: 'How many captured requests to scan.' },
338
- tab: { type: 'integer', description: 'Tab id (default: active tab).' }
338
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' }
339
339
  }
340
340
  }
341
341
  },
@@ -347,7 +347,7 @@ export const MCP_TOOLS = [
347
347
  inputSchema: {
348
348
  type: 'object',
349
349
  properties: {
350
- tab: { type: 'integer', description: 'Tab id (default: active tab).' }
350
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' }
351
351
  }
352
352
  }
353
353
  },
@@ -359,7 +359,7 @@ export const MCP_TOOLS = [
359
359
  inputSchema: {
360
360
  type: 'object',
361
361
  properties: {
362
- tab: { type: 'integer', description: 'Tab ID (defaults to the active tab).' },
362
+ tab: { type: 'integer', description: 'Tab id; omit → session default.' },
363
363
  },
364
364
  },
365
365
  },