dsh-chrome-use 0.9.4 → 0.10.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,28 @@
3
3
  版本号遵循语义化版本。格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/)。
4
4
  扩展版本和 Harness 插件版本始终一起发布,数字保持一致。
5
5
 
6
+ ## [0.10.0] - 2026-11-13
7
+
8
+ ### 新增
9
+
10
+ - chrome_doctor:页面健康检查。不传 tabId 时报告连接、端口、扩展版本、插件版本和部署允许的工具;传 tabId 时再报告页面 URL、文档就绪状态、弹窗、验证码/人机校验、旧 ref、已执行动作数、控制台错误和未完成请求。不能确定为什么点不动时先查它。
11
+ - chrome_wait_response:等待某次网络响应(urlContains、method、status 组合),可读取响应体,替代「反复轮询 chrome_network」。
12
+ - chrome_assert:确认页面满足某个条件(元素可见/消失、选择器、文字、网址、predicate、networkIdle),默认立即返回,可短暂轮询;不满足时返回 ASSERT_FAILED,避免下一步靠猜。
13
+ - chrome_content:按 selector 读取一块页面正文,输出 markdown、纯文本、HTML 或 JSON 大纲,maxChars 限制长度。
14
+ - chrome_scrape:用 `name: selector` / `name: selector@attr` 字段表静态抽取结构化数据;给容器 selector 时逐容器重复。
15
+ - 同源 iframe 与 open shadow DOM 支持:快照/查找会给子框架元素标 frame=true,点击和填写的坐标会自动换算到顶层视口;content、scrape、wait、assert 的选择器能穿透 open shadow DOM。
16
+ - 快照和 chrome_doctor 增加 challenge 字段,识别 Cloudflare、reCAPTCHA、hCaptcha、Turnstile 等验证码/人机校验,提示交给用户处理。
17
+ - 动作历史:chrome_history 不传 action 时列出本会话在该标签页执行过的工具、目标、是否可能已发送输入和失败码;不记录输入文字、表达式、predicate 和文件路径。
18
+
19
+ ### 变更
20
+
21
+ - 所有失败都带结构化字段:错误消息第二行为 `Failure: {"code","actionSent","retry","hint"}`。actionSent 说明输入是否可能已经生效(null 表示不确定),retry 给出下一步建议(snapshot/claim/other-tab/wait/inspect/user/fix-args/fix-page/none),hint 是一句可执行的下一步说明。扩展、bridge 和工具结果三层都会保留这组字段。
22
+ - 部署可以用 DSH_CHROME_USE_ALLOWED_ACTIONS 限制可用工具(逗号分隔短名,如 snapshot,tabs,click);设置后未列出的工具返回 ACTION_NOT_ALLOWED;诊断类工具(chrome_setup/status/pair/doctor/report)始终可用,chrome_status 和 chrome_doctor 显示允许清单。
23
+
24
+ ### 文档
25
+
26
+ - 运行期 skill 指南补充新工具、结构化失败字段、同源 iframe/shadow DOM 说明和动作开关。
27
+
6
28
  ## [0.9.4] - 2026-11-13
7
29
 
8
30
  ### 变更
package/README.md CHANGED
@@ -68,6 +68,8 @@ DSH_CHROME_USE_PORT=18793 DSH_CHROME_USE_NAME=Codex dsh
68
68
 
69
69
  端口 18792 留给 `scripts/fixture.mjs` 测试页。自动选择会跳过它。
70
70
 
71
+ 需要收窄权限时,用 `DSH_CHROME_USE_ALLOWED_ACTIONS` 限定这个部署能用的工具,逗号分隔短名,例如 `DSH_CHROME_USE_ALLOWED_ACTIONS=snapshot,tabs,click`。设置后没列出的工具会返回 `ACTION_NOT_ALLOWED`(fail-closed,写错名字等于禁用);`chrome_setup`、`chrome_status`、`chrome_pair`、`chrome_doctor`、`chrome_report` 这几个诊断工具始终可用。不设置则全部可用。`chrome_status` 和 `chrome_doctor` 会显示实际允许的清单。
72
+
71
73
  自动模式共用同一个 `port.json`。同时运行多个 Harness 时,建议各自显式设置 `DSH_CHROME_USE_PORT`;否则端口可能随启动顺序变化,导致旧令牌失效、需要重新配对。
72
74
 
73
75
  多个 Harness 可以同时操作不同的标签页,同一个标签页仍只归一个 Harness。另一个 Harness 抢先占用时返回 `TAB_OWNED_BY_ANOTHER_HARNESS`,不会排队等待。原来的单 Harness 安装不需要改配置。
@@ -154,6 +156,8 @@ Port 18792 is reserved for the `scripts/fixture.mjs` test page; automatic select
154
156
 
155
157
  Automatic mode shares one `port.json`. When running several harnesses, set `DSH_CHROME_USE_PORT` explicitly for each; otherwise the port a harness gets can depend on startup order, which invalidates the old token and forces a new pairing.
156
158
 
159
+ To narrow the surface, set `DSH_CHROME_USE_ALLOWED_ACTIONS` to a comma-separated list of short tool names, for example `DSH_CHROME_USE_ALLOWED_ACTIONS=snapshot,tabs,click`. Tools outside the list fail with `ACTION_NOT_ALLOWED` (fail-closed: a typo disables the tool); the diagnostics `chrome_setup`, `chrome_status`, `chrome_pair`, `chrome_doctor` and `chrome_report` always stay available. Unset means every tool is available. `chrome_status` and `chrome_doctor` report the effective list.
160
+
157
161
  Several harnesses can work on different tabs at the same time; a tab still belongs to only one harness. When another harness got there first you get `TAB_OWNED_BY_ANOTHER_HARNESS`, and there is no queueing. Existing single-harness setups need no configuration change.
158
162
 
159
163
  Harnesses can also leave each other messages in the background: `chrome_send_message` broadcasts or targets a specific port, `chrome_messages` reads them, and the receiver sees an unread hint in the result of its next tool call. This is an asynchronous message board: it does not interrupt a running operation and is not shown in the extension popup. Details and limits: [Messages between harnesses](docs/agent-messaging.md).
@@ -21,7 +21,7 @@
21
21
 
22
22
  ## 读取结构和验证按钮
23
23
 
24
- `chrome_snapshot` 默认返回页面的可访问性结构,包括链接、按钮等可操作元素,最多 500 个条目,结果中的 `truncated` 表示是否截断。读文章正文用 `format: "text"` 或 `format: "markdown"`,这两种模式不返回 ref,也不会让旧 ref 失效。元素太多时用 `chrome_find` 按名称和角色查找。快照不返回完整 HTML,也不返回输入框的值。
24
+ `chrome_snapshot` 默认返回页面的可访问性结构,包括链接、按钮等可操作元素,最多 500 个条目,结果中的 `truncated` 表示是否截断。读文章正文用 `format: "text"` 或 `format: "markdown"`,这两种模式不返回 ref,也不会让旧 ref 失效;也可以改用 `chrome_content` 按 selector 取一块区域,或 `chrome_scrape` 批量抽字段。元素太多时用 `chrome_find` 按名称和角色查找。快照不返回完整 HTML,也不返回输入框的值。同源子框架里的元素会带 `frame: true`;`challenge` 字段非空表示页面出现验证码/人机校验,需要用户处理。点不动或页面看起来不对时,先 `chrome_doctor` 看页面状态和未完成请求。
25
25
 
26
26
  要检查目标区域,可用 `chrome_evaluate` 执行下面的表达式。先根据快照确认选择器,再替换示例中的 `main`。
27
27
 
@@ -2,6 +2,7 @@ import { BrowserController } from './controller.js';
2
2
  import { TabLeases } from './leases.js';
3
3
  import { PORT, MAX_MESSAGE, PARALLEL_TABS, MESSAGE_UNREAD_MAX, validateArgs } from './protocol.js';
4
4
  import { classifyPairingCode } from './pairing-code.js';
5
+ import { classifyFailure } from './failures.js';
5
6
 
6
7
  // One Chrome extension can drive several harnesses at once. Each connection
7
8
  // keeps its own socket, pairing token, command map and cleanup generation. The
@@ -410,10 +411,20 @@ function connectPort(port, options = {}) {
410
411
  return ready;
411
412
  }
412
413
 
414
+ // Every failure keeps the plain CODE: message for older plugins and adds the
415
+ // structured parts (code, actionSent, retry, hint) for newer ones.
416
+ function failResult(action, message, error) {
417
+ const text = String(error?.message ?? error).slice(0, 2000);
418
+ // The controller already classified failures that crossed the page boundary
419
+ // (it knows the action); keep that object instead of re-deriving it from text.
420
+ const failure = error?.failure && typeof error.failure.code === 'string' ? error.failure : classifyFailure(text, action);
421
+ return { error: text, failure };
422
+ }
423
+
413
424
  function sendResult(client, message, response) {
414
425
  if (!isCurrent(client) || !client.connected || client.socket?.readyState !== WebSocket.OPEN) return;
415
426
  const body = JSON.stringify({ type: 'result', id: message.id, ...response });
416
- client.socket.send(body.length <= MAX_MESSAGE ? body : JSON.stringify({ type: 'result', id: message.id, error: 'RESULT_TOO_LARGE' }));
427
+ client.socket.send(body.length <= MAX_MESSAGE ? body : JSON.stringify({ type: 'result', id: message.id, error: 'RESULT_TOO_LARGE', failure: classifyFailure('RESULT_TOO_LARGE', message.action) }));
417
428
  }
418
429
 
419
430
  // Only the count and the senders reach the bridge; message text stays in
@@ -517,7 +528,7 @@ async function handleCommand(client, message) {
517
528
  throw new Error('Invalid command deadline');
518
529
  }
519
530
  if (client.tasks.has(id)) {
520
- sendResult(client, message, { error: 'DUPLICATE_COMMAND_ID' });
531
+ sendResult(client, message, failResult(message.action, message, 'DUPLICATE_COMMAND_ID: this command id is already running'));
521
532
  return;
522
533
  }
523
534
  const action = message.action;
@@ -525,7 +536,7 @@ async function handleCommand(client, message) {
525
536
  try {
526
537
  args = validateArgs(`chrome_${action}`, message.args);
527
538
  } catch (error) {
528
- sendResult(client, message, { error: String(error.message).slice(0, 2000) });
539
+ sendResult(client, message, failResult(action, message, error));
529
540
  return;
530
541
  }
531
542
  // Inbox tools only touch extension storage: they need no tab, input lock,
@@ -535,35 +546,35 @@ async function handleCommand(client, message) {
535
546
  if (action === 'send_message') await sendAgentMessage(client, message, args);
536
547
  else await readAgentMessages(client, message, args);
537
548
  } catch (error) {
538
- sendResult(client, message, { error: String(error.message).slice(0, 2000) });
549
+ sendResult(client, message, failResult(action, message, error));
539
550
  }
540
551
  return;
541
552
  }
542
553
  const tabId = Number.isSafeInteger(args.tabId) ? args.tabId : undefined;
543
554
  const lease = tabId === undefined ? undefined : leases.get(tabId);
544
555
  if (lease?.state === 'quarantined') {
545
- sendResult(client, message, { error: 'TAB_LEASE_QUARANTINED: this tab did not finish native cleanup; reload the extension to clear it' });
556
+ sendResult(client, message, failResult(action, message, 'TAB_LEASE_QUARANTINED: this tab did not finish native cleanup; reload the extension to clear it'));
546
557
  return;
547
558
  }
548
559
  if (lease?.state === 'closing') {
549
- sendResult(client, message, { error: 'TAB_LEASE_CLOSING: this tab is being cleaned up; retry after cleanup settles' });
560
+ sendResult(client, message, failResult(action, message, 'TAB_LEASE_CLOSING: this tab is being cleaned up; retry after cleanup settles'));
550
561
  return;
551
562
  }
552
563
  if (tabId !== undefined && busyTabs.has(tabId)) {
553
- sendResult(client, message, { error: 'TAB_BUSY: another command is already running on this tab' });
564
+ sendResult(client, message, failResult(action, message, 'TAB_BUSY: another command is already running on this tab'));
554
565
  return;
555
566
  }
556
567
  if (action === 'claim' && tabId !== undefined) {
557
568
  try { leases.acquire(tabId, client); }
558
- catch (error) { sendResult(client, message, { error: String(error.message).slice(0, 2000) }); return; }
569
+ catch (error) { sendResult(client, message, failResult(action, message, error)); return; }
559
570
  } else if (action === 'release' || action === 'close') {
560
571
  const owner = tabId === undefined ? undefined : leases.owner(tabId);
561
- if (!owner) { sendResult(client, message, { error: 'TAB_NOT_LEASED: claim the tab first' }); return; }
562
- if (owner !== client) { sendResult(client, message, { error: 'TAB_OWNED_BY_ANOTHER_HARNESS: another Harness has claimed this tab; use another tab or release it there' }); return; }
572
+ if (!owner) { sendResult(client, message, failResult(action, message, 'TAB_NOT_LEASED: claim the tab first')); return; }
573
+ if (owner !== client) { sendResult(client, message, failResult(action, message, 'TAB_OWNED_BY_ANOTHER_HARNESS: another Harness has claimed this tab; use another tab or release it there')); return; }
563
574
  } else if (tabId !== undefined) {
564
575
  const owner = leases.owner(tabId);
565
- if (!owner) { sendResult(client, message, { error: 'TAB_NOT_LEASED: claim the tab first' }); return; }
566
- if (owner !== client) { sendResult(client, message, { error: 'TAB_OWNED_BY_ANOTHER_HARNESS: another Harness has claimed this tab; use another tab or release it there' }); return; }
576
+ if (!owner) { sendResult(client, message, failResult(action, message, 'TAB_NOT_LEASED: claim the tab first')); return; }
577
+ if (owner !== client) { sendResult(client, message, failResult(action, message, 'TAB_OWNED_BY_ANOTHER_HARNESS: another Harness has claimed this tab; use another tab or release it there')); return; }
567
578
  }
568
579
 
569
580
  const cancellation = new AbortController();
@@ -588,7 +599,7 @@ async function handleCommand(client, message) {
588
599
  : undefined;
589
600
  response = { result: await controller.execute(action, args, cancellation.signal, options) };
590
601
  } catch (error) {
591
- response = { error: String(error.message).slice(0, 2000) };
602
+ response = failResult(action, message, error);
592
603
  } finally {
593
604
  clearTimeout(deadline);
594
605
  try {