@xiaoqiong0v0/opencode-browser-tool 1.13.0 → 1.14.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
@@ -226,7 +226,7 @@ bt_cli({ args: "inject disable --id r1" })
226
226
  - **`inject list` 只给结构**:`id / order / enabled / match / items / last`(ok、failed + 最近错误首行),明细只显示每个 item 的 `name` + 字节数,**绝不回显脚本内容**
227
227
  - **与 `evaluate` 的区别**:`evaluate` 是**一次性**在当前页面执行并返回结果;`inject` 是**持久规则**,命中 URL 后每次加载都执行,结果只写回规则(不回模型),需用 `inject list` 查看
228
228
 
229
- ### 命令参考(52 条,与 `COMMANDS` 一致)
229
+ ### 命令参考(55 条,与 `COMMANDS` 一致)
230
230
 
231
231
  > 唯一真源:`src/index.ts:521-757` 的 `COMMANDS`(本表与之逐条对应,条数、flag 名、可选性均以代码为准)。说明取自 `src/i18n/en.ts` 的 `cli.cmd.*`(`src/i18n/en.ts:49-100`,中文对照见 `src/i18n/zh.ts`)。参数列中**不带 `[]` 者为必填**;⏱ = 支持 `--timeout ms`。完整命令/参数/help 以 `bt_cli({ args: "help" })` 的实际输出为准。
232
232
 
@@ -238,7 +238,10 @@ bt_cli({ args: "inject disable --id r1" })
238
238
  | `clear` ⏱ | `--selector` 必填 | 清空输入框 |
239
239
  | `select` ⏱ | `--selector --value` 必填 | 选择下拉框选项 |
240
240
  | `hover` ⏱ | `--selector` 必填 | 悬停到元素 |
241
- | `drag` ⏱ | `--sourceSelector --targetSelector` 必填 | 拖拽元素到目标 |
241
+ | `drag` ⏱ | `[--sourceSelector S \| --sourcePoint "x,y"] [--targetSelector T \| --targetPoint "x,y"] [--pointer mouse\|touch]` 或 `--actions '<json array>' [--pointer mouse\|touch]` | 拖拽(默认 `--pointer mouse`,`touch` 走触摸序列):非批次取源/目标**可视中心**(元素矩形 ∩ 可滚动祖先 clientRect ∩ 视口),不可见直接报错且不发鼠标事件;批次支持 press/move/scroll/wait/release(详见「拖拽(drag)」) |
242
+ | `touch` ⏱ | `--actions '<json array>'` 必填 | 多点触摸动作集:`down/move/up/wait`,最多 5 指,点可用 `x/y` 或 `selector`,支持 `radius`/`force` |
243
+ | `tap` ⏱ | `[--selector S \| --x X --y Y] [--pointer touch\|mouse]` | 单指点击;默认触摸,`--pointer mouse` 走鼠标点击 |
244
+ | `swipe` ⏱ | `--from "x,y" --to "x,y" [--steps N] [--duration ms]` | 单指滑动;`--steps` 默认 8,`--duration` 为总时长(毫秒) |
242
245
  | `press_key` ⏱ | `--key` 必填,`[--selector]` | 按下键盘按键;`--selector` 指定先聚焦的元素 |
243
246
  | `type` ⏱ | `--text <文本\|@file:<路径>>` 必填,`[--selector] [--enter]` | **向当前焦点插入文本(不清空已有内容)**;`--selector` 先聚焦该元素,`--enter` 插入后回车;`--text` 支持 `@file:`(与 `evaluate` 同一套 containment)。xterm/终端/contenteditable 用它,表单替换用 `fill` |
244
247
  | `upload_file` ⏱ | `--selector --filePath` 必填 | 上传文件到输入框 |
@@ -339,6 +342,43 @@ bt_cli({ args: "inject disable --id r1" })
339
342
  - Linux(含 WSLg)截图路径已实测通过(见 `docs/implementation.md`);**真实 Linux 桌面(GNOME/KDE)仍待复验**
340
343
  - 折下懒加载图(`loading=lazy`)经 cache-bust 实测:滚动前 `complete=false`,自动滚动后加载完成、截图内容非空白
341
344
 
345
+ ### 拖拽(drag)
346
+
347
+ **非批次**(选择器或视口坐标,source/target 各二选一):
348
+
349
+ ```text
350
+ drag --sourceSelector "#card" --targetSelector ".column"
351
+ drag --sourceSelector "#card" --targetPoint "800,400"
352
+ drag --sourcePoint "120,220" --targetSelector ".column"
353
+ ```
354
+
355
+ - 抓取点 = **元素矩形 ∩ 所有可滚动祖先的 clientRect ∩ 视口**的交集中心;**不再自动滚动**。源或目标交集为空/宽高 ≤ 0 → `drag: source/target center not visible (<selector>)`,**不发任何鼠标事件**
356
+ - `sourcePoint` 必须落在视口内(`0 <= x < innerWidth && 0 <= y < innerHeight`);`targetPoint` 允许越界(拖出窗口/贴边)
357
+ - 执行序列:`move(源) → press → N 步插值 move → release(目标)`
358
+
359
+ **动作批次**(`--actions`,单次 HTTP 调用内顺序执行,无跨命令状态):
360
+
361
+ ```text
362
+ drag --actions '[{"press":{"selector":"#card"}},{"scroll":{"selector":".list","dy":300}},{"wait":{"ms":50}},{"move":{"selector":".item-5","steps":8}},{"release":{}}]'
363
+ ```
364
+
365
+ - 动作:`move{selector|to,steps?,delay?}`(`steps` 默认 8)、`press{selector|to}`、`scroll{selector?,dx?,dy?}`、`wait{ms}`、`release{selector|to}`
366
+ - 状态机:**press 前不能 move/scroll/release**;release 后不能再有动作;press 时已按下 → 报错。`release` 可省略,执行器 **try/finally 自动补释放**(中途失败也会先释放按键)
367
+ - 每个 `selector` 同样取可视中心;不可见/找不到即报错并保证释放。`press` 坐标越界直接报错且不发事件;`move`/`release` 坐标允许越界
368
+ - `scroll`:带 `selector` 时把该容器 `scrollLeft/scrollTop` 加上 `dx/dy`,并在滚动后于当前坐标重新派发 `dragOver`(不再额外注入 wheel,避免双倍滚动);不带 `selector` 时仍注入 wheel。两种路径都在滚动后重新 `dragOver`
369
+
370
+ **限制**:仅 Windows;iframe 内元素不支持;目标不在 DOM 的虚拟列表不会自动滚动查找。
371
+
372
+ > ⚠️ **行为变更(v1.14.0)**:旧调用若源/目标不同屏(可视交集为空),现在会明确报 `not visible`;此前会静默取矩形中心导致落点偏移,不会报错。
373
+
374
+ ### 触摸输入(touch / tap / swipe)
375
+
376
+ - 首次触摸命令会启用触摸仿真(`navigator.maxTouchPoints = 5`),之后保持启用;页面导航/重建后下次触摸会重新启用;不影响鼠标命令。
377
+ - `touch --actions '<json>'`:点集动作 `down/move/up/wait`。`down`/`move` 的点可用 `{"x","y"}` 或 `{"selector","align":"center"}`(取可视中心);`radius` 默认 1、`force` 默认 1.0。`move` 必须携带当前全部按下点,最多 5 指;`id` 按按下顺序分配并跨步骤保持。**批次必须以 `up` 收尾**(缺 `up` 直接报错,不会遗留按下状态)。长按 = `down → wait → up`;长按拖拽 = `down → wait → move → up`。
378
+ - `tap [--selector|--x --y] [--pointer touch|mouse]`:默认触摸(`touchStart → touchEnd`),`--pointer mouse` 改为鼠标点击。
379
+ - `swipe --from "x,y" --to "x,y" [--steps 8] [--duration ms]`:单指移动序列;`duration` 为总时长,按 steps 均分。
380
+ - `drag --pointer touch`:复用 drag 的坐标/可见性/批次/scroll 语义,底层为 `touchStart → touchMove×N → touchEnd`,不启用鼠标拖拽拦截;默认 `--pointer mouse` 行为不变。
381
+ - 限制:非 Windows 明确报未实现;触摸仅透传 radius/force 数值,不模拟真实压力/面积;超过 5 指报错。**触摸通道不会触发 HTML5 DnD**(原生拖拽走鼠标拦截通道;`drag --pointer touch` 是触摸序列,仅供监听 touch 事件的页面使用)。
342
382
  ## 设备预设 / 尺寸语义
343
383
 
344
384
  > ⚠️ **行为变更(v1.8.0)**:`resize` 与 `set_device` 的宽高一律指**页面内容区(视口)**尺寸,不再是 OS 窗口尺寸。窗口高 = 内容区高 + 工具栏 72px。**旧脚本若按窗口尺寸理解,高度会偏 72px**。API 返回同时给出 `content` 与 `window` 两组尺寸便于核对。
package/dist/client.js CHANGED
@@ -827,6 +827,9 @@ export const service = {
827
827
  pressKey: cmd("press-key"),
828
828
  typeText: cmd("type"),
829
829
  drag: cmd("drag"),
830
+ touch: cmd("touch"),
831
+ tap: cmd("tap"),
832
+ swipe: cmd("swipe"),
830
833
  uploadFile: cmd("upload-file"),
831
834
  screenshot: cmd("screenshot"),
832
835
  evaluate: cmd("evaluate"),
package/dist/i18n/en.js CHANGED
@@ -40,6 +40,23 @@ const en = {
40
40
  "cli.err.type_file_too_large": "type: @file exceeds the per-file limit of {kb} KiB: {path}",
41
41
  "cli.err.type_file_total_too_large": "type: @file exceeds the total limit of {kb} KiB: {path}",
42
42
  "cli.err.type_read_failed": "type: cannot read @file: {path}: {msg}",
43
+ "cli.err.drag_actions_conflict": "drag: --actions cannot be combined with sourceSelector/sourcePoint/targetSelector/targetPoint",
44
+ "cli.err.drag_source_required": "drag: pass --sourceSelector or --sourcePoint (exactly one)",
45
+ "cli.err.drag_target_required": "drag: pass --targetSelector or --targetPoint (exactly one)",
46
+ "cli.err.drag_source_conflict": "drag: --sourceSelector and --sourcePoint are mutually exclusive",
47
+ "cli.err.drag_target_conflict": "drag: --targetSelector and --targetPoint are mutually exclusive",
48
+ "cli.err.touch_actions_invalid": "touch: --actions must be a valid JSON action array (down/move/up/wait)",
49
+ "cli.err.touch_pointer_invalid": "--pointer must be mouse or touch",
50
+ "cli.err.touch_missing_up": "touch: action batch must end with up",
51
+ "cli.err.tap_selector_conflict": "tap: --selector and --x/--y are mutually exclusive",
52
+ "cli.err.tap_target_required": "tap: pass --selector or --x/--y",
53
+ "cli.err.tap_xy_pair": "tap: --x and --y must be provided together",
54
+ "cli.err.tap_xy_invalid": "tap: --x and --y must be numbers",
55
+ "cli.err.tap_pointer": "tap: --pointer must be touch or mouse",
56
+ "cli.err.swipe_from_invalid": "swipe: --from must be \"x,y\"",
57
+ "cli.err.swipe_to_invalid": "swipe: --to must be \"x,y\"",
58
+ "cli.err.swipe_steps_invalid": "swipe: --steps must be an integer 1-1000",
59
+ "cli.err.swipe_duration_invalid": "swipe: --duration must be a non-negative integer (ms)",
43
60
  "cli.failed": "Command {cmd} failed: {msg}",
44
61
  "cli.cmd.navigate": "Navigate browser to a URL",
45
62
  "cli.cmd.click": "Click an element; optional --timeout ms (100-60000) caps this call's wait budget, timeout errors explicitly",
@@ -47,7 +64,10 @@ const en = {
47
64
  "cli.cmd.clear": "Clear an input field; optional --timeout ms (100-60000) caps this call's wait budget, timeout errors explicitly",
48
65
  "cli.cmd.select": "Select a dropdown option; optional --timeout ms (100-60000) caps this call's wait budget, timeout errors explicitly",
49
66
  "cli.cmd.hover": "Hover over an element; optional --timeout ms (100-60000) caps this call's wait budget, timeout errors explicitly",
50
- "cli.cmd.drag": "Drag element to a target; optional --timeout ms (100-60000) caps this call's wait budget, timeout errors explicitly",
67
+ "cli.cmd.drag": "Drag using selectors or viewport points (exactly one per side), using each element's visible center; or run a press/move/scroll/wait/release action batch in one call via --actions. --pointer defaults to mouse; use touch for touch events. Optional --timeout ms (100-60000) caps each step's wait budget, timeout errors explicitly",
68
+ "cli.cmd.touch": "Multi-point touch actions: --actions takes down/move/up/wait (up to 5 points, optional radius/force, x/y or selector)",
69
+ "cli.cmd.tap": "Single tap: --selector or --x/--y; defaults to touch, use --pointer mouse for a mouse click. Optional --timeout ms",
70
+ "cli.cmd.swipe": "Single-finger swipe: --from \"x,y\" --to \"x,y\", --steps defaults to 8, --duration is total ms. Optional --timeout ms",
51
71
  "cli.cmd.press_key": "Press a keyboard key; optional --timeout ms (100-60000) caps this call's wait budget, timeout errors explicitly",
52
72
  "cli.cmd.type": "Insert text at the current focus WITHOUT clearing existing content (use this for xterm/terminal/contenteditable; use fill for form replacement). --selector focuses that element first; --enter presses Enter after inserting; --text accepts @file:<path> (workspace dir or plugin tmp only, content never enters the model context); optional --timeout ms (100-60000) caps this call's wait budget",
53
73
  "cli.cmd.upload_file": "Upload a file to an input; optional --timeout ms (100-60000) caps this call's wait budget, timeout errors explicitly",
@@ -109,7 +129,10 @@ const en = {
109
129
  "cli.out.clear": "Cleared",
110
130
  "cli.out.select": "Selected",
111
131
  "cli.out.hover": "Hovered",
112
- "cli.out.drag": "Dragged",
132
+ "cli.out.drag": "drag ok (steps={steps}, {ms}ms)",
133
+ "cli.out.touch": "touch ok (steps={steps}, {ms}ms)",
134
+ "cli.out.tap": "tapped ({pointer}, steps={steps})",
135
+ "cli.out.swipe": "swipe ok (steps={steps}, {ms}ms)",
113
136
  "cli.out.press_key": "Pressed: {key}",
114
137
  "cli.out.type": "typed {n} char(s) into {target}",
115
138
  "cli.out.type.focused": "focused element",
package/dist/i18n/zh.js CHANGED
@@ -40,6 +40,23 @@ const zh = {
40
40
  "cli.err.type_file_too_large": "type: @file 超过单文件上限 {kb} KiB: {path}",
41
41
  "cli.err.type_file_total_too_large": "type: @file 合计超过上限 {kb} KiB: {path}",
42
42
  "cli.err.type_read_failed": "type: 无法读取 @file: {path}: {msg}",
43
+ "cli.err.drag_actions_conflict": "drag: --actions 不能与 sourceSelector/sourcePoint/targetSelector/targetPoint 同时使用",
44
+ "cli.err.drag_source_required": "drag: 必须给 --sourceSelector 或 --sourcePoint(二选一)",
45
+ "cli.err.drag_target_required": "drag: 必须给 --targetSelector 或 --targetPoint(二选一)",
46
+ "cli.err.drag_source_conflict": "drag: --sourceSelector 与 --sourcePoint 只能给一个",
47
+ "cli.err.drag_target_conflict": "drag: --targetSelector 与 --targetPoint 只能给一个",
48
+ "cli.err.touch_actions_invalid": "touch: --actions 必须是合法的 JSON 动作数组(down/move/up/wait)",
49
+ "cli.err.touch_pointer_invalid": "--pointer 只能是 mouse 或 touch",
50
+ "cli.err.touch_missing_up": "touch: 动作批次必须以 up 收尾",
51
+ "cli.err.tap_selector_conflict": "tap: --selector 与 --x/--y 只能给一种",
52
+ "cli.err.tap_target_required": "tap: 必须给 --selector 或 --x/--y",
53
+ "cli.err.tap_xy_pair": "tap: --x 与 --y 必须同时提供",
54
+ "cli.err.tap_xy_invalid": "tap: --x 与 --y 必须是数字",
55
+ "cli.err.tap_pointer": "tap: --pointer 只能是 touch 或 mouse",
56
+ "cli.err.swipe_from_invalid": "swipe: --from 必须是 \"x,y\" 格式",
57
+ "cli.err.swipe_to_invalid": "swipe: --to 必须是 \"x,y\" 格式",
58
+ "cli.err.swipe_steps_invalid": "swipe: --steps 必须是 1~1000 的整数",
59
+ "cli.err.swipe_duration_invalid": "swipe: --duration 必须是非负整数(毫秒)",
43
60
  "cli.failed": "命令 {cmd} 失败: {msg}",
44
61
  "cli.cmd.navigate": "导航浏览器到 URL",
45
62
  "cli.cmd.click": "点击页面元素;可选 --timeout ms(100~60000)限定本次等待上限,超时明确报错",
@@ -47,7 +64,10 @@ const zh = {
47
64
  "cli.cmd.clear": "清空输入框;可选 --timeout ms(100~60000)限定本次等待上限,超时明确报错",
48
65
  "cli.cmd.select": "选择下拉框选项;可选 --timeout ms(100~60000)限定本次等待上限,超时明确报错",
49
66
  "cli.cmd.hover": "悬停到元素;可选 --timeout ms(100~60000)限定本次等待上限,超时明确报错",
50
- "cli.cmd.drag": "拖拽元素到目标;可选 --timeout ms(100~60000)限定本次等待上限,超时明确报错",
67
+ "cli.cmd.drag": "拖拽:选择器或视口坐标(source/target 各二选一),取元素可视中心;或用 --actions 在单次调用内执行 press/move/scroll/wait/release 动作批次。--pointer 默认 mouse,可改 touch(底层触摸序列)。可选 --timeout ms(100~60000)限定每步等待上限,超时明确报错",
68
+ "cli.cmd.touch": "多点触摸动作集:--actions 传 down/move/up/wait 数组(最多 5 指,可带 radius/force,点可用 x/y 或 selector)",
69
+ "cli.cmd.tap": "单指点击:--selector 或 --x/--y;默认触摸(touch),可加 --pointer mouse 走鼠标点击。可选 --timeout ms",
70
+ "cli.cmd.swipe": "单指滑动:--from \"x,y\" --to \"x,y\",--steps 默认 8,--duration 为总时长(毫秒)。可选 --timeout ms",
51
71
  "cli.cmd.press_key": "按下键盘按键;可选 --timeout ms(100~60000)限定本次等待上限,超时明确报错",
52
72
  "cli.cmd.type": "向当前焦点插入文本(不清空已有内容;xterm/终端/contenteditable 用这个,表单替换用 fill)。--selector 先聚焦该元素;--enter 插入后再回车;--text 支持 @file:<路径>(仅限工作区目录或插件临时目录,内容不进模型上下文);可选 --timeout ms(100~60000)限定本次等待上限",
53
73
  "cli.cmd.upload_file": "上传文件到输入框;可选 --timeout ms(100~60000)限定本次等待上限,超时明确报错",
@@ -109,7 +129,10 @@ const zh = {
109
129
  "cli.out.clear": "已清空",
110
130
  "cli.out.select": "已选择",
111
131
  "cli.out.hover": "已悬停",
112
- "cli.out.drag": "已拖拽",
132
+ "cli.out.drag": "drag ok(steps={steps}, 耗时 {ms}ms)",
133
+ "cli.out.touch": "touch ok(steps={steps}, 耗时 {ms}ms)",
134
+ "cli.out.tap": "tapped({pointer}, steps={steps})",
135
+ "cli.out.swipe": "swipe ok(steps={steps}, 耗时 {ms}ms)",
113
136
  "cli.out.press_key": "已按键: {key}",
114
137
  "cli.out.type": "已输入 {n} 个字符到 {target}",
115
138
  "cli.out.type.focused": "当前焦点元素",
package/dist/index.js CHANGED
@@ -10,11 +10,14 @@ import { configureService, setAppDirectory, getAppDirectory, setServiceReadyHand
10
10
  import { startNotifyServer } from "./notify-server.js";
11
11
  import { startDownload, getBinaryStatus, describeBinaryStatus } from "./binary.js";
12
12
  import { sanitizeForModel } from "./util/sanitize.js";
13
+ import { fileResolutionMode } from "./util/file-resolver.js";
13
14
  import { EVAL_ITEM_MAX_BYTES, EVAL_TOTAL_MAX_BYTES, classifyEvalArgs, runEvalItems, } from "./util/eval-items.js";
14
15
  import { INJECT_RULE_MAX_BYTES, buildInjectItems, formatInjectAdd, formatInjectList, formatInjectTest, injectAddIssue, injectRuleLimitIssue, normalizeInjectAction, } from "./util/inject-rules.js";
15
16
  import { formatConsoleItems, formatNetworkItems, renderLogText, } from "./util/log-format.js";
16
17
  import { TYPE_TEXT_MAX_BYTES, resolveTypeText, typeTextIssue, formatTypeOutput, } from "./util/type-text.js";
17
18
  import { formatDownloads, buildDownloadsPayload, buildDownloadDirPayload, downloadsLimitIssue, } from "./util/downloads.js";
19
+ import { dragArgIssue } from "./util/drag.js";
20
+ import { parsePointer, parseTouchActions, swipeIssue, tapIssue } from "./util/touch.js";
18
21
  import { tool } from "@opencode-ai/plugin";
19
22
  import { log } from "./logger.js";
20
23
  import { createSessionIDCache, extractSessionIDFromEvent } from "./session-id.js";
@@ -505,7 +508,69 @@ const COMMANDS = {
505
508
  clear: { usage: "--selector [--timeout ms]", descKey: "cli.cmd.clear", args: [{ flag: "selector" }, { flag: "timeout" }], run: async (a) => { const tn = timeoutNote(a); const r = await service.clear(a); return _t("cli.out.clear") + degradedNote(r) + tn; } },
506
509
  select: { usage: "--selector --value [--timeout ms]", descKey: "cli.cmd.select", args: [{ flag: "selector" }, { flag: "value" }, { flag: "timeout" }], run: async (a) => { const tn = timeoutNote(a); const r = await service.select(a); return _t("cli.out.select") + degradedNote(r) + tn; } },
507
510
  hover: { usage: "--selector [--timeout ms]", descKey: "cli.cmd.hover", args: [{ flag: "selector" }, { flag: "timeout" }], run: async (a) => { const tn = timeoutNote(a); const r = await service.hover(a); return _t("cli.out.hover") + degradedNote(r) + tn; } },
508
- drag: { usage: "--sourceSelector --targetSelector [--timeout ms]", descKey: "cli.cmd.drag", args: [{ flag: "sourceSelector" }, { flag: "targetSelector" }, { flag: "timeout" }], run: async (a) => { const tn = timeoutNote(a); await service.drag(a); return _t("cli.out.drag") + tn; } },
511
+ // drag:非批次用选择器或坐标(source/target 各二选一);或 --actions 单次调用内执行动作批次
512
+ drag: {
513
+ usage: "[--sourceSelector S | --sourcePoint \"x,y\"] [--targetSelector T | --targetPoint \"x,y\"] [--pointer mouse|touch] [--timeout ms] | [--actions '<json array>'] [--pointer mouse|touch] [--timeout ms]",
514
+ descKey: "cli.cmd.drag",
515
+ args: [
516
+ { flag: "sourceSelector" }, { flag: "sourcePoint" },
517
+ { flag: "targetSelector" }, { flag: "targetPoint" },
518
+ { flag: "actions" }, { flag: "pointer" }, { flag: "timeout" },
519
+ ],
520
+ run: async (a) => {
521
+ const tn = timeoutNote(a);
522
+ if (!parsePointer(a.pointer, "mouse").ok)
523
+ return _t("cli.err.touch_pointer_invalid");
524
+ const issue = dragArgIssue(a);
525
+ if (issue)
526
+ return _t(`cli.err.drag_${issue}`);
527
+ const r = await service.drag(a);
528
+ return _t("cli.out.drag", { steps: r?.steps ?? 0, ms: r?.elapsedMs ?? 0 }) + tn;
529
+ },
530
+ },
531
+ // touch:多点动作集(单次调用内顺序执行)
532
+ touch: {
533
+ usage: "--actions '<json array>' [--timeout ms]",
534
+ descKey: "cli.cmd.touch",
535
+ args: [{ flag: "actions" }, { flag: "timeout" }],
536
+ run: async (a) => {
537
+ const tn = timeoutNote(a);
538
+ const parsed = parseTouchActions(a.actions);
539
+ if (!parsed.ok) {
540
+ return _t(parsed.reason === "missing_up" ? "cli.err.touch_missing_up" : "cli.err.touch_actions_invalid");
541
+ }
542
+ const r = await service.touch(a);
543
+ return _t("cli.out.touch", { steps: r?.steps ?? 0, ms: r?.elapsedMs ?? 0 }) + tn;
544
+ },
545
+ },
546
+ // tap:单指点击(默认 touch;--pointer mouse 可走鼠标)
547
+ tap: {
548
+ usage: "[--selector S | --x X --y Y] [--pointer touch|mouse] [--timeout ms]",
549
+ descKey: "cli.cmd.tap",
550
+ args: [{ flag: "selector" }, { flag: "x" }, { flag: "y" }, { flag: "pointer" }, { flag: "timeout" }],
551
+ run: async (a) => {
552
+ const tn = timeoutNote(a);
553
+ const issue = tapIssue(a);
554
+ if (issue)
555
+ return _t(`cli.err.tap_${issue}`);
556
+ const r = await service.tap(a);
557
+ return _t("cli.out.tap", { pointer: r?.pointer || "touch", steps: r?.steps ?? 0 }) + tn;
558
+ },
559
+ },
560
+ // swipe:单指滑动
561
+ swipe: {
562
+ usage: "--from \"x,y\" --to \"x,y\" [--steps N] [--duration ms] [--timeout ms]",
563
+ descKey: "cli.cmd.swipe",
564
+ args: [{ flag: "from" }, { flag: "to" }, { flag: "steps" }, { flag: "duration" }, { flag: "timeout" }],
565
+ run: async (a) => {
566
+ const tn = timeoutNote(a);
567
+ const issue = swipeIssue(a);
568
+ if (issue)
569
+ return _t(`cli.err.swipe_${issue}`);
570
+ const r = await service.swipe(a);
571
+ return _t("cli.out.swipe", { steps: r?.steps ?? 0, ms: r?.elapsedMs ?? 0 }) + tn;
572
+ },
573
+ },
509
574
  press_key: { usage: "--key [--selector] [--timeout ms]", descKey: "cli.cmd.press_key", args: [{ flag: "key" }, { flag: "selector" }, { flag: "timeout" }], run: async (a) => { const tn = timeoutNote(a); await service.pressKey(a); return _t("cli.out.press_key", { key: a.key }) + tn; } },
510
575
  // type:向当前焦点插入文本(不清空);--text 支持 @file:<path>,--enter 插入后回车
511
576
  type: { usage: "--text <text|@file:path> [--selector] [--enter] [--timeout ms]", descKey: "cli.cmd.type", args: [{ flag: "text" }, { flag: "selector" }, { flag: "enter", type: "boolean" }, { flag: "timeout" }], run: async (a) => runType(a) },
@@ -980,19 +1045,25 @@ export const opencodeBrowserTool = async ({ client, worktree, directory }) => {
980
1045
  for (const key of Object.keys(params)) {
981
1046
  const v = params[key];
982
1047
  if (typeof v === "string" && v.startsWith("@file:")) {
983
- // `type` 的 `--text` 自带 evaluate 同款解析(允许工作区目录 + 插件 tmp,且不删文件),
984
- // 故跳过这里的「仅插件 tmp」通用层,避免两套规则叠加(通用层还会删掉全局 tmp 文件)。
985
- if (cmdName === "type" && key === "text")
1048
+ // 三类模式:own=命令自己解析(跳过本层)、workspace=允许工作区+插件 tmp、
1049
+ // tmp=只允许插件 tmp(默认,读完删全局 tmp 临时文件)。
1050
+ // 缺 workspace 会让 `evaluate --script @file:<工作区>/x.js` 永远被拒(1.14.0 缺陷)。
1051
+ const mode = fileResolutionMode(cmdName, key);
1052
+ if (mode === "own")
986
1053
  continue;
1054
+ const workspaceDir = getAppDirectory();
1055
+ const allowedDirs = mode === "workspace" && workspaceDir ? [...allowedTmpDirs, workspaceDir] : allowedTmpDirs;
987
1056
  const p = v.slice(6);
988
- const abs = resolve(p);
989
- if (!allowedTmpDirs.some((d) => isInsideDir(abs, d))) {
990
- return _t("cli.err.file_outside_tmp", { path: p });
1057
+ const abs = workspaceDir && mode === "workspace" ? resolve(workspaceDir, p) : resolve(p);
1058
+ if (!allowedDirs.some((d) => isInsideDir(abs, d))) {
1059
+ return _t(mode === "workspace" ? "cli.err.eval_file_outside" : "cli.err.file_outside_tmp", {
1060
+ path: p,
1061
+ });
991
1062
  }
992
1063
  try {
993
1064
  params[key] = readFileSync(abs, "utf-8").replace(/\r?\n$/, "");
994
1065
  // 用完即删(仅限 lookup 生成的全局 tmp 临时密码文件;会话 tmp 内的截图等保留)
995
- if (isInsideDir(abs, globalTmpDir)) {
1066
+ if (mode !== "workspace" && isInsideDir(abs, globalTmpDir)) {
996
1067
  unlinkSync(abs);
997
1068
  }
998
1069
  }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * `drag` 命令参数的插件侧最小校验。
3
+ *
4
+ * 真正的解析/执行在 Rust `/api/drag`(服务端仍是最终口径);这里只做互斥检查,
5
+ * 让模型在插件侧就能拿到本地化错误,而不是先发一次 HTTP。
6
+ */
7
+ /** 字段是否为非空字符串(空白视为未提供) */
8
+ function present(a, key) {
9
+ const v = a[key];
10
+ return typeof v === "string" && v.trim() !== "";
11
+ }
12
+ /**
13
+ * 校验 drag 参数组合。
14
+ *
15
+ * 规则:
16
+ * - `actions` 与 selection/point 四参互斥;
17
+ * - 非批次时 source 侧、target 侧各自必须且只能给一种(选择器或坐标)。
18
+ *
19
+ * @param a 已解析的命令参数
20
+ * @returns 问题原因;合法返回 null
21
+ */
22
+ export function dragArgIssue(a) {
23
+ // 与 Rust 同口径:只要出现 actions 键就按批次处理(非法类型交给服务端报错)
24
+ const actions = a.actions !== undefined;
25
+ const sides = ["sourceSelector", "sourcePoint", "targetSelector", "targetPoint"];
26
+ if (actions) {
27
+ return sides.some((k) => present(a, k)) ? "actions_conflict" : null;
28
+ }
29
+ const sourceCount = ["sourceSelector", "sourcePoint"].filter((k) => present(a, k)).length;
30
+ if (sourceCount === 0)
31
+ return "source_required";
32
+ if (sourceCount > 1)
33
+ return "source_conflict";
34
+ const targetCount = ["targetSelector", "targetPoint"].filter((k) => present(a, k)).length;
35
+ if (targetCount === 0)
36
+ return "target_required";
37
+ if (targetCount > 1)
38
+ return "target_conflict";
39
+ return null;
40
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * `@file:` 通用层的解析模式判定(纯逻辑,便于单测)。
3
+ *
4
+ * 背景:`bt_cli` 的通用层对所有字符串参数先做一遍 `@file:` 处理。它原本只允许
5
+ * **插件 tmp**内的文件(读完还会删掉全局 tmp 里的文件),这对大多数命令是对的;
6
+ * 但有两类命令自带更宽的语义,需要区别对待:
7
+ *
8
+ * - `type --text`:命令自身解析(`resolveTypeText`),通用层必须整体跳过;
9
+ * - `evaluate --script`:脚本文件应当允许放在**工作区**(用户最常见用法),
10
+ * 但语义仍是"读出内容当脚本",所以不能跳过,要按 workspace 模式读且**不删文件**。
11
+ *
12
+ * 1.14.0 及更早:`evaluate --script @file:<工作区>/x.js` 会被 tmp-only 规则直接拒掉
13
+ * (报 file_outside_tmp),工作区里的脚本永远跑不起来。
14
+ */
15
+ /** 命令名 → 参数键 → 模式(未列出的一律 `tmp`)。 */
16
+ const MODES = Object.freeze({
17
+ type: { text: "own" },
18
+ evaluate: { script: "workspace" },
19
+ });
20
+ /**
21
+ * 判断通用层该如何处理某命令的某个 `@file:` 参数。
22
+ *
23
+ * @param cmd 命令名(如 `evaluate` / `type`)
24
+ * @param key 参数键(如 `script` / `text`)
25
+ * @returns 解析模式(`own` / `workspace` / `tmp`)
26
+ */
27
+ export function fileResolutionMode(cmd, key) {
28
+ const perCmd = MODES[cmd];
29
+ const mode = perCmd ? perCmd[key] : undefined;
30
+ return mode ?? "tmp";
31
+ }
@@ -0,0 +1,181 @@
1
+ /**
2
+ * 触摸命令参数校验(插件侧,Rust 端仍为最终口径)。
3
+ *
4
+ * 只做结构/互斥校验,避免明显非法参数先发一次 HTTP;selector 的真实 CSS 语法由页面解析。
5
+ */
6
+ /** 解析 `--pointer`(缺省由命令决定) */
7
+ export function parsePointer(v, fallback) {
8
+ if (v === undefined || v === null)
9
+ return { ok: true, pointer: fallback };
10
+ if (typeof v === "string") {
11
+ const s = v.trim().toLowerCase();
12
+ if (s === "")
13
+ return { ok: true, pointer: fallback };
14
+ if (s === "mouse" || s === "touch")
15
+ return { ok: true, pointer: s };
16
+ }
17
+ return { ok: false };
18
+ }
19
+ /** 解析 `"x,y"` */
20
+ export function parsePointText(v) {
21
+ if (typeof v !== "string")
22
+ return { ok: false };
23
+ const parts = v.split(",");
24
+ if (parts.length !== 2)
25
+ return { ok: false };
26
+ const x = Number(parts[0].trim());
27
+ const y = Number(parts[1].trim());
28
+ if (!Number.isFinite(x) || !Number.isFinite(y))
29
+ return { ok: false };
30
+ return { ok: true, x, y };
31
+ }
32
+ /** 校验 `touch --actions` 的 JSON(不解析 selector CSS) */
33
+ export function parseTouchActions(text) {
34
+ if (typeof text !== "string")
35
+ return { ok: false, reason: "json" };
36
+ let value;
37
+ try {
38
+ value = JSON.parse(text);
39
+ }
40
+ catch {
41
+ return { ok: false, reason: "json" };
42
+ }
43
+ if (!Array.isArray(value))
44
+ return { ok: false, reason: "not_array" };
45
+ if (value.length === 0)
46
+ return { ok: false, reason: "empty" };
47
+ let active = 0;
48
+ for (const item of value) {
49
+ if (!isTouchAction(item))
50
+ return { ok: false, reason: "shape" };
51
+ const a = item;
52
+ if ("down" in a) {
53
+ if (active > 0)
54
+ return { ok: false, reason: "shape" };
55
+ active = a.down.length;
56
+ }
57
+ else if ("move" in a) {
58
+ if (active === 0 || a.move.length !== active)
59
+ return { ok: false, reason: "shape" };
60
+ }
61
+ else if ("up" in a) {
62
+ if (active === 0)
63
+ return { ok: false, reason: "shape" };
64
+ active = 0;
65
+ }
66
+ }
67
+ // 批次必须以 up 收尾,避免跨命令遗留按下
68
+ const last = value[value.length - 1];
69
+ if (!isObj(last) || !("up" in last))
70
+ return { ok: false, reason: "missing_up" };
71
+ return { ok: true, actions: value };
72
+ }
73
+ /** 校验 tap 目标/pointer */
74
+ export function tapIssue(a) {
75
+ if (!parsePointer(a.pointer, "touch").ok)
76
+ return "pointer";
77
+ const hasSel = typeof a.selector === "string" && a.selector.trim() !== "";
78
+ const hasX = present(a.x);
79
+ const hasY = present(a.y);
80
+ if (hasSel && (hasX || hasY))
81
+ return "selector_conflict";
82
+ if (!hasSel && !hasX && !hasY)
83
+ return "target_required";
84
+ if (hasX !== hasY)
85
+ return "xy_pair";
86
+ if (hasX && hasY) {
87
+ const x = Number(a.x);
88
+ const y = Number(a.y);
89
+ if (!Number.isFinite(x) || !Number.isFinite(y))
90
+ return "xy_invalid";
91
+ }
92
+ return null;
93
+ }
94
+ /** 非空值(字符串 trim 后为空视为未提供) */
95
+ function present(v) {
96
+ return v !== undefined && v !== null && !(typeof v === "string" && v.trim() === "");
97
+ }
98
+ /** 校验 swipe 参数 */
99
+ export function swipeIssue(a) {
100
+ if (!parsePointText(a.from).ok)
101
+ return "from_invalid";
102
+ if (!parsePointText(a.to).ok)
103
+ return "to_invalid";
104
+ if (a.steps !== undefined && a.steps !== null && a.steps !== "") {
105
+ const n = Number(a.steps);
106
+ if (!Number.isInteger(n) || n < 1 || n > 1000)
107
+ return "steps_invalid";
108
+ }
109
+ if (a.duration !== undefined && a.duration !== null && a.duration !== "") {
110
+ const n = Number(a.duration);
111
+ if (!Number.isInteger(n) || n < 0)
112
+ return "duration_invalid";
113
+ }
114
+ return null;
115
+ }
116
+ /** 单个 action 合法结构 */
117
+ function isTouchAction(v) {
118
+ if (!isObj(v))
119
+ return false;
120
+ const keys = Object.keys(v);
121
+ if (keys.includes("move")) {
122
+ if (keys.some((k) => k !== "move" && k !== "steps"))
123
+ return false;
124
+ if (!isPointArray(v.move))
125
+ return false;
126
+ if (v.steps !== undefined) {
127
+ const n = Number(v.steps);
128
+ if (!Number.isInteger(n) || n < 1 || n > 1000)
129
+ return false;
130
+ }
131
+ return true;
132
+ }
133
+ if (keys.length !== 1)
134
+ return false;
135
+ if ("down" in v)
136
+ return isPointArray(v.down);
137
+ if ("up" in v)
138
+ return Array.isArray(v.up) && v.up.length === 0;
139
+ if ("wait" in v) {
140
+ if (!isObj(v.wait))
141
+ return false;
142
+ const ms = Number(v.wait.ms);
143
+ return Number.isInteger(ms) && ms >= 0;
144
+ }
145
+ return false;
146
+ }
147
+ /** 点数组 1..=5 */
148
+ function isPointArray(v) {
149
+ if (!Array.isArray(v) || v.length === 0 || v.length > 5)
150
+ return false;
151
+ return v.every(isPoint);
152
+ }
153
+ /** 点:{x,y} 或 {selector,align?},可选 radius/force */
154
+ function isPoint(v) {
155
+ if (!isObj(v))
156
+ return false;
157
+ if (v.selector !== undefined) {
158
+ if (typeof v.selector !== "string" || v.selector.trim() === "")
159
+ return false;
160
+ if (v.x !== undefined || v.y !== undefined)
161
+ return false;
162
+ if (v.align !== undefined && v.align !== "center")
163
+ return false;
164
+ }
165
+ else {
166
+ if (typeof v.x !== "number" || typeof v.y !== "number")
167
+ return false;
168
+ if (!Number.isFinite(v.x) || !Number.isFinite(v.y))
169
+ return false;
170
+ }
171
+ for (const key of ["radius", "force"]) {
172
+ if (v[key] !== undefined && (typeof v[key] !== "number" || !Number.isFinite(v[key]))) {
173
+ return false;
174
+ }
175
+ }
176
+ return true;
177
+ }
178
+ /** 普通对象 */
179
+ function isObj(v) {
180
+ return typeof v === "object" && v !== null && !Array.isArray(v);
181
+ }
package/package.json CHANGED
@@ -1,45 +1,45 @@
1
- {
2
- "name": "@xiaoqiong0v0/opencode-browser-tool",
3
- "version": "1.13.0",
4
- "description": "opencode 浏览器工具插件:WebView2 外壳渲染 + 零注入视觉元素选取面板",
5
- "main": "dist/index.js",
6
- "scripts": {
7
- "build": "tsc",
8
- "dev": "tsc --watch",
9
- "test": "npm run build && node --test \"tests/unit/*.test.mjs\"",
10
- "build:frontend": "cd frontend && npm install && npm run build"
11
- },
12
- "keywords": [
13
- "opencode",
14
- "browser",
15
- "webview2",
16
- "plugin"
17
- ],
18
- "author": "",
19
- "license": "ISC",
20
- "type": "module",
21
- "files": [
22
- "dist"
23
- ],
24
- "dependencies": {
25
- "@xiaoqiong0v0/opencode-plugin-logger": "^1.0.5",
26
- "string-argv": "^0.3.2"
27
- },
28
- "peerDependencies": {
29
- "@opencode-ai/plugin": "^1.18.4"
30
- },
31
- "devDependencies": {
32
- "@types/node": "^26.1.1",
33
- "typescript": "^7.0.2"
34
- },
35
- "publishConfig": {
36
- "access": "public"
37
- },
38
- "repository": {
39
- "type": "git",
40
- "url": "git+https://github.com/xiaoqiong0v0/opencode-browser-tool.git"
41
- },
42
- "volta": {
43
- "node": "24.16.0"
44
- }
45
- }
1
+ {
2
+ "name": "@xiaoqiong0v0/opencode-browser-tool",
3
+ "version": "1.14.1",
4
+ "description": "opencode 浏览器工具插件:WebView2 外壳渲染 + 零注入视觉元素选取面板",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc",
8
+ "dev": "tsc --watch",
9
+ "test": "npm run build && node --test \"tests/unit/*.test.mjs\"",
10
+ "build:frontend": "cd frontend && npm install && npm run build"
11
+ },
12
+ "keywords": [
13
+ "opencode",
14
+ "browser",
15
+ "webview2",
16
+ "plugin"
17
+ ],
18
+ "author": "",
19
+ "license": "ISC",
20
+ "type": "module",
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "dependencies": {
25
+ "@xiaoqiong0v0/opencode-plugin-logger": "^1.0.5",
26
+ "string-argv": "^0.3.2"
27
+ },
28
+ "peerDependencies": {
29
+ "@opencode-ai/plugin": "^1.18.4"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^26.1.1",
33
+ "typescript": "^7.0.2"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/xiaoqiong0v0/opencode-browser-tool.git"
41
+ },
42
+ "volta": {
43
+ "node": "24.16.0"
44
+ }
45
+ }