dsh-browser-cdp 1.9.51 → 1.11.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.
@@ -30,7 +30,7 @@ import { STATE_DIR, TASK_SPACE_FILE } from "./paths.mjs";
30
30
  * Each heredoc is a fresh Node process, so state lives in a file, not memory.
31
31
  */
32
32
 
33
- const EMPTY = { spaces: [], selectedId: null, nextId: 1, closedSpaces: [] };
33
+ const EMPTY = { spaces: [], selectedId: null, nextId: 1, closedSpaces: [], orphans: {} };
34
34
 
35
35
  /**
36
36
  * How many idle-closed spaces to remember.
@@ -40,6 +40,49 @@ const EMPTY = { spaces: [], selectedId: null, nextId: 1, closedSpaces: [] };
40
40
  */
41
41
  const REMEMBERED_CLOSURES = 20;
42
42
 
43
+ /**
44
+ * How long a page target must stay unclaimed before it is treated as an orphan.
45
+ */
46
+ const ORPHAN_GRACE_MS = 120000;
47
+
48
+ /**
49
+ * Which live page targets belong to no space.
50
+ *
51
+ * [local patch, see runtime/PATCHES.md] Issue #67 (reconcile half).
52
+ *
53
+ * Two-phase deliberately: a target seen orphaned once is remembered in
54
+ * `seenAt`, and only closed once it has STAYED orphaned past a grace period.
55
+ * The grace covers the window between a tool creating a tab and registering it
56
+ * in a space, and the window where a person opened a tab by hand to take a look
57
+ * at something. Blank and chrome:// targets are never candidates — a tab is
58
+ * about:blank for a moment on every navigation.
59
+ *
60
+ * @param {Map<string, {url?: string}>} live
61
+ * @param {Array<{targetIds?: string[]}>} spaces
62
+ * @param {Record<string, number>} seenAt
63
+ * @param {number} now
64
+ * @param {number} graceMs
65
+ * @returns {{orphans: string[], next: Record<string, number>}}
66
+ */
67
+ export function selectOrphans(live, spaces, seenAt, now, graceMs) {
68
+ const claimed = new Set();
69
+ for (const space of spaces) {
70
+ for (const id of space.targetIds || []) claimed.add(id);
71
+ }
72
+ const orphans = [];
73
+ const next = {};
74
+ for (const [targetId, target] of live) {
75
+ if (claimed.has(targetId)) continue;
76
+ const url = target && typeof target.url === "string" ? target.url : "";
77
+ if (url === "" || url === "about:blank") continue;
78
+ if (url.startsWith("chrome://") || url.startsWith("devtools://")) continue;
79
+ const firstSeen = typeof seenAt[targetId] === "number" ? seenAt[targetId] : now;
80
+ next[targetId] = firstSeen;
81
+ if (now - firstSeen >= graceMs) orphans.push(targetId);
82
+ }
83
+ return { orphans, next };
84
+ }
85
+
43
86
  export function createTaskSpacesApi(cdp) {
44
87
  /**
45
88
  * Which space *this process* is working in.
@@ -293,6 +336,40 @@ export function createTaskSpacesApi(cdp) {
293
336
  if (await pruneAbandoned(state, live)) changed = true;
294
337
  if (await pruneIdle(state)) changed = true;
295
338
 
339
+ // [local patch, see runtime/PATCHES.md] Issue #67 (reconcile half): a page
340
+ // target claimed by no space — the residue of a crash-restore or a tab a
341
+ // human opened by hand — never leaves on its own, because pruneIdle /
342
+ // pruneAbandoned / --prune-spaces only walk state.spaces.
343
+ const orphanNow = Date.now();
344
+ const orphanPlan = selectOrphans(
345
+ live,
346
+ state.spaces,
347
+ state.orphans || {},
348
+ orphanNow,
349
+ ORPHAN_GRACE_MS,
350
+ );
351
+ if (orphanPlan.orphans.length > 0) {
352
+ const urls = orphanPlan.orphans.map((id) => live.get(id)?.url).filter(Boolean);
353
+ for (const id of orphanPlan.orphans) {
354
+ await cdp.call("Target.closeTarget", { targetId: id }).catch(() => {});
355
+ }
356
+ const closures = state.closedSpaces || [];
357
+ closures.unshift({
358
+ name: "(orphan tab)",
359
+ urls,
360
+ closedAt: orphanNow,
361
+ idleMinutes: 0,
362
+ });
363
+ state.closedSpaces = closures.slice(0, REMEMBERED_CLOSURES);
364
+ changed = true;
365
+ }
366
+ const nextOrphans = { ...orphanPlan.next };
367
+ for (const id of orphanPlan.orphans) delete nextOrphans[id];
368
+ if (JSON.stringify(nextOrphans) !== JSON.stringify(state.orphans || {})) {
369
+ state.orphans = nextOrphans;
370
+ changed = true;
371
+ }
372
+
296
373
  const surviving = state.spaces.filter((space) => space.targetIds.length > 0);
297
374
  if (surviving.length !== state.spaces.length) {
298
375
  // Losing its last tab is how a space ends when the user closes tabs by
@@ -0,0 +1,68 @@
1
+ ---
2
+ name: bcdp-browser
3
+ description: Drive a visible agent browser (ego / Chrome / Edge / Tabbit / Lightpanda) with bcdp_* tools — navigate, click, fill forms, screenshot, read page data, manage tabs, verify results. Use whenever a task needs a real browser, a logged-in page, or visual confirmation.
4
+ whenToUse: The user asks to open or inspect a page, fill a form, click through a flow, screenshot or verify a web UI, or read data that only exists behind a login.
5
+ ---
6
+
7
+ # bcdp-browser
8
+
9
+ ## 两个入口,别搞混
10
+
11
+ | 入口 | 给你什么 | 什么时候调 |
12
+ |---|---|---|
13
+ | `bcdp_help`(常驻工具) | **运行态**:激活的后端、当前 space、已解锁批次、单工具提示(`topic`) | 每个浏览器任务的第一步;拿不准参数时再调一次 |
14
+ | 本 skill | **流程**:循环、铁律、配方、降级表 | 已加载就够,不要反复读 |
15
+
16
+ 只看到 `bcdp_help` 一个 `bcdp_*` 工具是正常现象:其余工具按批次解锁,调一次 `bcdp_help` 就会铺开下一批。**不要把"工具不在列表里"当成坏了。**
17
+
18
+ ## 一次循环
19
+
20
+ 1. `bcdp_help` —— 看后端是哪条通道、space 叫什么、有没有坑
21
+ 2. `bcdp_navigate {url}` —— 等 DOM 就绪
22
+ 3. `bcdp_snapshot` —— 拿到带 `[ref=N, loc=...]` 的语义树
23
+ 4. `bcdp_click` / `bcdp_fill` / `bcdp_check` / `bcdp_select` —— 用 `loc=`
24
+ 5. `bcdp_snapshot` 或 `bcdp_page_info` —— 验证页面真的变了,而不是假设它变了
25
+ 6. 需要交给人:`bcdp_space {op:'handoff'}`;拿回来:`op:'takeover'`
26
+
27
+ ## 选择器:永远优先 `loc=`
28
+
29
+ `bcdp_snapshot` 返回两种标记:
30
+
31
+ - `loc=...` —— **跨调用稳定**,用它
32
+ - `@N` / `ref=N` —— 每次快照重新编号,只在本次快照内有效
33
+
34
+ `bcdp_click` 也接受坐标(`x`、`y`),只在确实拿不到选择器时用。
35
+
36
+ ## 三条铁律
37
+
38
+ 1. **一次调用只做一件事。** 导航和提交不要塞进同一次调用,否则失败后说不清哪一步挂了。
39
+ 2. **等状态用 `bcdp_wait`**(`op=selector|url|response|load|function`),不要在 `bcdp_js` 里写 sleep。
40
+ 3. **看到 `actionSent=true` 就停。** 先 `bcdp_snapshot` 核实页面到底变没变,禁止原样重试——盲跑第二遍会把一次提交变成两次。
41
+
42
+ ## 六条禁令
43
+
44
+ - 不在 `retry=never` 的失败后重试:`BACKEND_UNSUPPORTED` / `INVALID_ARGS` / `CAPTCHA` / `USER_CONTROLLING` 都是终点,换路或交人。
45
+ - 不复用上一轮快照的 `ref=` / `@N`——它们只在本次快照内有效,跨调用一律用 `loc=`。
46
+ - 不在 lightpanda 后端尝试登录 / CAPTCHA:没有窗口,必须换后端。
47
+ - 不对 `ownership=user` 的标签页做破坏性动作(关闭 / 提交 / 删除 / 清空),要动先问。
48
+ - 不猜密码、不暴力重试、不绕过登录与人机验证。
49
+ - 不把整页截图当默认观察手段——先 `bcdp_snapshot` 拿语义树,只在需要视觉证据时再截图。
50
+
51
+ ## 后端差异(会静默降级,必须看)
52
+
53
+ `bcdp_help` 会报 `backend:`,各通道能力不同:
54
+
55
+ | 后端 | 缺口 |
56
+ |---|---|
57
+ | `tabbit-cli` | 无标签枚举 / 窗口关闭、无 overlay 点选、无下载事件;`bcdp_script` 里没有 `taskSpaces` / `cdp()` / `fetch.*`,`verbatim` 被拒;`snapshot` 上限 20000 字符 |
58
+ | `lightpanda` | 无头无窗口:登录、CAPTCHA 没有可操作的地方,必须换后端 |
59
+ | `ego-cli` | 无直接 CDP:`bcdp_cdp` 不可用 |
60
+ | CDP 端点 / 内置运行时 | 全能力 |
61
+
62
+ 撞到"工具报不支持"时**不要重试**,读 `references/recovery.md` 换路。
63
+
64
+ ## 按需读(不要预加载这三册)
65
+
66
+ - `references/recipes.md` —— 表单、多 tab、上传下载、iframe、分页取数
67
+ - `references/recovery.md` —— 断连、后端切换、CAPTCHA、前台抢占、ref 失效
68
+ - `references/eval.md` —— JEV 判定链(实验性,默认别碰)
@@ -0,0 +1,23 @@
1
+ # JEV 判定链(实验性)
2
+
3
+ **默认不要用。** 这条链是实验性质,只在明确要求"按视觉判定网页状态"时才启用。
4
+
5
+ ## 前提
6
+
7
+ - 必须配了 JEV 或 Laya 的 Key,否则这 5 个工具不注入;即使注入,调用也只会回"未配置";
8
+ - 五个工具是一套流水:`bcdp_jev_status` → `bcdp_jev_ask` / `bcdp_jev_frame` → `bcdp_jev_run` → `bcdp_jev_attempt`。
9
+
10
+ ## 什么时候值得用
11
+
12
+ - 需要用"看图判断"来确认状态(不是读 DOM 文本能得出的结论);
13
+ - 需要一次判定产出可复用的证据(帧 + 结论 + 尝试记录)。
14
+
15
+ ## 什么时候不要用
16
+
17
+ - 只是想确认"页面变了没" → 用 `bcdp_snapshot` / `bcdp_page_info`;
18
+ - 只是想截个图给人看 → 用 `bcdp_screenshot`;
19
+ - 没配 Key → 直接跳过,不要反复试探。
20
+
21
+ ## 失败处理
22
+
23
+ 判定链失败时**不要自动重试整条链**:先 `bcdp_jev_status` 看链路可用性,确认是"未配置"还是"判定无结论"。判定无结论时把事实(帧、目标、失败原因)回给用户,不要替用户下结论。
@@ -0,0 +1,59 @@
1
+ # 配方
2
+
3
+ 按需读。每个配方都是"一次调用只做一件事"的排布,不要合并。
4
+
5
+ ## 填一份表单
6
+
7
+ ```
8
+ 1. bcdp_navigate {url, wait:true}
9
+ 2. bcdp_snapshot → 找候选字段的 loc=
10
+ 3. bcdp_fill {selector:'loc=...', text:'...'} (逐个字段,一次一个)
11
+ 4. bcdp_check {selector:'loc=...', checked:true} / bcdp_select {selector, value}
12
+ 5. bcdp_click {selector:'loc=...'} → 点提交(单独一次)
13
+ 6. bcdp_wait {op:'url', pattern:'success'} 或 {op:'response', pattern:'/api/submit'}
14
+ 7. bcdp_snapshot 或 bcdp_page_info → 确认真的成功了
15
+ ```
16
+
17
+ 要点:提交前确认每个字段都写进去了(回读一次成对字段),提交后**必须有一步验证**,不要凭 click 返回成功就报告完成。
18
+
19
+ ## 多标签页
20
+
21
+ ```
22
+ bcdp_tab {op:'list'} → 看现有标签
23
+ bcdp_tab {op:'new', url:'...'} → 打开(会复用同 URL 的标签)
24
+ bcdp_tab {op:'current'} → 确认 agent 当前在哪个页
25
+ bcdp_tab {op:'switch', target:'...'} → target 可传 targetId 或 URL 子串
26
+ bcdp_tab {op:'close', target:'...'} → 不会关闭最后一个标签
27
+ ```
28
+
29
+ 注意:`tabbit-cli` 后端**没有标签枚举和窗口关闭**,`op=list/close` 会失败。这种活要换 CDP 端点或 ego 线。
30
+
31
+ ## 上传 / 下载
32
+
33
+ - 上传:`bcdp_upload` 指定本地文件路径,不要用 `bcdp_js` 伪造 file input(伪造的往往被前端校验拦掉)。
34
+ - 下载:`bcdp_download`。`tabbit-cli` 后端**收不到下载事件**,需要真下载就换后端。
35
+ - 先确认触发动作(点击)与上传/下载的顺序,别把两者塞进同一次调用。
36
+
37
+ ## iframe 内部
38
+
39
+ `bcdp_snapshot` 不保证覆盖跨域 iframe。优先:
40
+
41
+ 1. 先 `bcdp_page_info` 看页面结构;
42
+ 2. 覆盖不到就用 `bcdp_script`,在脚本里用 `page.frames()` 定位(**仅 ego 线可用**;tabbit 线没有 `taskSpaces` / `cdp()` / `fetch.*`,`page.frames()` 之外的 SDK 面也不全)。
43
+
44
+ ## 分页取数
45
+
46
+ ```
47
+ 1. bcdp_js {expression:'...'} → 抓当前页条目(只返回结构化数组 + 计数)
48
+ 2. bcdp_click / bcdp_wait → 翻页,一次一步
49
+ 3. 重复,直到 计数不再增长 或 出现结束标记
50
+ ```
51
+
52
+ 要点:
53
+ - 每次只返回**本轮新增**的条目,不要每轮把累计结果整体回传(上下文会被自己撑爆)。
54
+ - 报"共 N 条"之前,必须有一次"最后一页为空/总数对得上"的验证。没验证就说"已翻到第 k 页,累计 N 条,未确认是否到底"。
55
+
56
+ ## 视觉确认
57
+
58
+ - `bcdp_screenshot {selector:'loc=...'}` 只截局部,默认别截整页;
59
+ - 需要判断"长得对不对"时,截完直接看图,不要用 `bcdp_js` 读计算样式去猜。
@@ -0,0 +1,63 @@
1
+ # 恢复
2
+
3
+ ## 工具突然全没了
4
+
5
+ 正常。`lazy` 模式下闲置回收会把注入面收回成只剩 `bcdp_help`。再调一次 `bcdp_help` 就重新铺开——这不是插件坏了,也不要改设置。
6
+
7
+ ## 连不上浏览器
8
+
9
+ ```
10
+ bcdp_status {refresh:true} → 重新探测并刷新状态
11
+ bcdp_doctor → 看连接列表、激活项、解析到的路径
12
+ ```
13
+
14
+ `bcdp_doctor` 首行会写当前注入面(`mode=lazy browse=open ... live N / sealed M`)。看不到工具时先读这一行,别猜。
15
+
16
+ ## 后端不对
17
+
18
+ 设置面板里的连接列表是**有序**的,**被激活的那一项驱动每一次 `bcdp_*` 调用**。顺序即优先级。
19
+
20
+ - 切了后端之后,先 `bcdp_status` 确认 `backend:` 再干活;
21
+ - 激活 `tabbit-cli` 时插件会硬拒内置运行时启动(防止"以为在 Tabbit 里做、实际驱动了另一个浏览器");
22
+ - 同机不要同时用另一个 Tabbit 驱动(会抢同一个任务槽)。
23
+
24
+ ## 人机验证 / 登录墙
25
+
26
+ ```
27
+ bcdp_captcha → 检测并提示用户去窗口里手动完成
28
+ bcdp_wait_for_human {reason:'...'} → 显式让权,等用户做完再继续
29
+ ```
30
+
31
+ 铁律:不猜密码、不绕验证、不重复提交。用户那边完成后,`bcdp_space {op:'takeover'}` 拿回控制权。
32
+
33
+ ## Windows 上前台反复被抢
34
+
35
+ 在 Windows 上频繁 `bcdp_tab {op:'switch'}` / `op:'new'` 可能把浏览器窗口反复提到最前,打断用户。
36
+
37
+ 对策:把同一轮操作聚在一次调用里完成,减少切 tab 次数;需要长时间后台跑时先跟用户说一声。
38
+
39
+ ## 选择器失效
40
+
41
+ - `@N` / `ref=N` 每次快照重新编号,**跨调用不可靠**;
42
+ - 重新 `bcdp_snapshot` 拿新的,或者从一开始就用 `loc=...`;
43
+ - 页面用 shadow DOM 时,`loc=` 也可能取不到,改用 `bcdp_script` 里 `page.locator()` 的深度穿透写法。
44
+
45
+ ## 超时之后
46
+
47
+ 1. **先核实状态**(`bcdp_snapshot` 看页面当前长什么样);
48
+ 2. 只有当确认"没生效"才重试;有副作用的操作(提交、支付、删除)尤其不能盲跑;
49
+ 3. 需要更长预算时,`bcdp_script` 有 `timeoutMs`;`tabbit-cli` 的 CLI 级 `--timeout-ms` 被夹在 60000–180000,写小了会被抬上去。
50
+
51
+ ## tabbit 通道专属
52
+
53
+ - `request-id` 是幂等的:同一个 id 重放返回原回执,不会重跑;
54
+ - 任务被 finish 后重建,会重置该任务的配额与 `globalThis` 状态;
55
+ - 没有 `Browser.*` / `Target.*` 域:标签枚举、窗口关闭、Overlay 点选、下载事件都不可用;
56
+ - 不支持 `verbatim`:`bcdp_script` 在 tabbit 线会拒绝该参数。
57
+
58
+ ## 空间与并发
59
+
60
+ - 每个会话默认用**自己的**空间(`dsh-agent-<会话 id 前 8 位>`),并发会话互不干扰;
61
+ - **不要手动建 `#4`/`#5` 这类编号空间**;
62
+ - 确实要跨会话共享一个槽时才显式传 `space:'default'`;
63
+ - 登录态在磁盘 Profile 里跨重启保留(除非开了 `isolateSpaces` 内存沙盒)。