dsh-cua-linux 0.0.0-stage → 0.1.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 +85 -0
- package/LICENSE +21 -0
- package/README.md +88 -2
- package/README.zh.md +78 -0
- package/assets/icon-180.png +0 -0
- package/assets/screenshot-1-cdp-bing.png +0 -0
- package/assets/screenshot-2-occluded.png +0 -0
- package/assets/screenshot-3-x11-wechat.png +0 -0
- package/cordis.patch.yml +6 -0
- package/index.js +474 -0
- package/lib/cdp.js +307 -0
- package/lib/png.js +95 -0
- package/lib/probe.js +175 -0
- package/lib/verify.js +162 -0
- package/lib/x11.js +306 -0
- package/package.json +62 -4
- package/screenshots.json +5 -0
package/index.js
ADDED
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-cua-linux —— 给 DSH 补上 Linux(X11 / XWayland)的 computer-use 后端。
|
|
3
|
+
*
|
|
4
|
+
* 通道优先级:CDP(浏览器,见 M2) > X11 像素。
|
|
5
|
+
*
|
|
6
|
+
* 工具:
|
|
7
|
+
* cua_linux_probe 只读探测:这台机器能怎么操作
|
|
8
|
+
* cua_linux_windows 枚举可见窗口(含 app / 标题 / 屏幕坐标 / 尺寸)
|
|
9
|
+
* cua_linux_screenshot 截图(整屏或单窗口)写 PNG 到磁盘,返回路径
|
|
10
|
+
* cua_linux_click 点击(窗口本地坐标,默认先置顶)
|
|
11
|
+
* cua_linux_key 组合键 / 特殊键
|
|
12
|
+
* cua_linux_type 输入 ASCII 文本(非 ASCII 明确报错,不静默失败)
|
|
13
|
+
* cua_linux_scroll 滚轮
|
|
14
|
+
*
|
|
15
|
+
* 已知边界:
|
|
16
|
+
* - 只对 X11 / XWayland 客户端有效;原生 Wayland 窗口收不到 XTEST 事件。
|
|
17
|
+
* - 中文无法用 XTEST 注入(XTEST 只能发 keycode),需要 CDP 或剪贴板,M2 补。
|
|
18
|
+
* - 目标窗口必须先置顶,否则命中会落到上层窗口(工具默认自动置顶)。
|
|
19
|
+
*/
|
|
20
|
+
export const name = 'dsh-cua-linux'
|
|
21
|
+
|
|
22
|
+
export const inject = ['tools']
|
|
23
|
+
|
|
24
|
+
// 宿主提供则用官方 defineTool 校验参数/输出;否则退化为身份函数,便于本地单测。
|
|
25
|
+
let defineTool = (def) => def
|
|
26
|
+
try {
|
|
27
|
+
const mod = await import('@deepseek-ai/dsh-tools')
|
|
28
|
+
if (mod && typeof mod.defineTool === 'function') defineTool = mod.defineTool
|
|
29
|
+
} catch {
|
|
30
|
+
// 非 DSH 环境:走兜底
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const OUT = {
|
|
34
|
+
schema: {
|
|
35
|
+
type: 'object',
|
|
36
|
+
additionalProperties: false,
|
|
37
|
+
properties: {
|
|
38
|
+
ok: { type: 'boolean', required: true },
|
|
39
|
+
result: { type: 'string', required: true },
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
render: (_args, value) => [{ type: 'text', text: value.result }],
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const ok = (result) => ({ ok: true, result })
|
|
46
|
+
const fail = (result) => ({ ok: false, result })
|
|
47
|
+
|
|
48
|
+
/** 统一包一层:把异常变成 ok:false,而不是把栈抛给模型。 */
|
|
49
|
+
const guard = (fn) => async (args) => {
|
|
50
|
+
try {
|
|
51
|
+
return await fn(args || {})
|
|
52
|
+
} catch (err) {
|
|
53
|
+
return fail(`✗ ${err && err.message ? err.message : String(err)}`)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function fmtWindow(w) {
|
|
58
|
+
return `0x${w.wid.toString(16)} ${(w.app || '?').padEnd(16)} ${String(w.title || '(无标题)').slice(0, 40).padEnd(40)} ${w.w}x${w.h}+${w.x}+${w.y}`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function apply(ctx) {
|
|
62
|
+
if (!ctx || !ctx.tools || typeof ctx.tools.register !== 'function') {
|
|
63
|
+
throw new Error('dsh-cua-linux: 需要 ctx.tools.register(宿主未注入 tools 服务)')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const x11 = () => import('./lib/x11.js')
|
|
67
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* X11 动作闭环:拍前图 → 执行 → 等界面响应 → 拍后图 → 比像素。
|
|
71
|
+
* wid 为空时比较整个屏幕(用于不知道焦点在哪的操作,比如 cua_linux_type)。
|
|
72
|
+
*/
|
|
73
|
+
/**
|
|
74
|
+
* 动作闭环(区域版):
|
|
75
|
+
* 1) 动作前连拍两张 → 判断该区域本身是否在动(光标闪烁/动画/视频)
|
|
76
|
+
* 2) 动作后再拍一张 → 先比动作点附近,再比整窗(影响可能在别处)
|
|
77
|
+
* 只有区域稳定时,前后比对才有意义。
|
|
78
|
+
*/
|
|
79
|
+
async function actVerifiedVisual({ wid = null, point = null, radius = 90, label = null, settleMs = 280 }, action) {
|
|
80
|
+
const x = await x11()
|
|
81
|
+
const v = await import('./lib/verify.js')
|
|
82
|
+
const shoot = () => x.captureRaw(wid)
|
|
83
|
+
|
|
84
|
+
// 采三帧:光标闪烁周期约 530ms,单次 130ms 间隔会漏掉整个闪烁
|
|
85
|
+
const b1 = await shoot()
|
|
86
|
+
await sleep(250)
|
|
87
|
+
const b2 = await shoot()
|
|
88
|
+
await sleep(250)
|
|
89
|
+
const b3 = await shoot()
|
|
90
|
+
|
|
91
|
+
const width = b2.width
|
|
92
|
+
const box = point
|
|
93
|
+
? { x: point.x - radius, y: point.y - radius, w: radius * 2, h: radius * 2 }
|
|
94
|
+
: null
|
|
95
|
+
|
|
96
|
+
// 噪声 = 动作前两帧之间的自发变化(光标闪烁/动画)
|
|
97
|
+
const noise = Math.max(
|
|
98
|
+
(v.diffRGBA(b1.rgba, b2.rgba, 8, { width, box }) || {}).ratio || 0,
|
|
99
|
+
(v.diffRGBA(b2.rgba, b3.rgba, 8, { width, box }) || {}).ratio || 0,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
await action(x)
|
|
103
|
+
await sleep(settleMs)
|
|
104
|
+
const after = await shoot()
|
|
105
|
+
|
|
106
|
+
const regionDiff = box
|
|
107
|
+
? v.diffRGBA(b2.rgba, after.rgba, 8, { width, box })
|
|
108
|
+
: v.diffRGBA(b2.rgba, after.rgba)
|
|
109
|
+
const windowDiff = box ? v.diffRGBA(b2.rgba, after.rgba) : null
|
|
110
|
+
|
|
111
|
+
return v.judgeTwoTier({
|
|
112
|
+
noise,
|
|
113
|
+
signal: regionDiff,
|
|
114
|
+
window: windowDiff,
|
|
115
|
+
areaLabel: box ? 'Area around the action point' : label || (wid ? 'Window' : 'Whole screen'),
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 把判定结果渲染成一行,失败时用 ⚠ 而不是静默的 ✓。 */
|
|
120
|
+
// verified: true=✓ / false=⚠ / null=?(无法判定)。三种状态必须能区分开,
|
|
121
|
+
// 把 null 画成 ✓ 等于撒谎。
|
|
122
|
+
const line = (j) => `${j.verified === true ? '✓' : j.verified === false ? '⚠' : '?'} ${j.text}`
|
|
123
|
+
|
|
124
|
+
ctx.tools.register(defineTool({
|
|
125
|
+
name: 'cua_linux_probe',
|
|
126
|
+
description:
|
|
127
|
+
'探测本机 Linux 桌面当前可用的 computer-use 通道:会话类型(X11/Wayland)、' +
|
|
128
|
+
'X11 socket 与 XTEST 扩展是否可用、cua-driver 是否存在、uinput 是否可用、' +
|
|
129
|
+
'以及本机是否已有 Chromium 调试端口(用于 CDP 直控)。' +
|
|
130
|
+
'只读取环境信息,不点击、不输入、不修改任何状态。',
|
|
131
|
+
parameters: {
|
|
132
|
+
cdpPorts: {
|
|
133
|
+
type: 'string',
|
|
134
|
+
description: '可选:要探测的 Chromium 调试端口,逗号分隔,默认 "9222,9333"。',
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
output: OUT,
|
|
138
|
+
execute: guard(async (args) => {
|
|
139
|
+
const { probe } = await import('./lib/probe.js')
|
|
140
|
+
const ports = String(args.cdpPorts || '9222,9333')
|
|
141
|
+
.split(',')
|
|
142
|
+
.map((s) => Number(s.trim()))
|
|
143
|
+
.filter((n) => Number.isInteger(n) && n > 0 && n < 65536)
|
|
144
|
+
const report = await probe({ cdpPorts: ports })
|
|
145
|
+
return ok(report.text)
|
|
146
|
+
}),
|
|
147
|
+
}))
|
|
148
|
+
|
|
149
|
+
ctx.tools.register(defineTool({
|
|
150
|
+
name: 'cua_linux_windows',
|
|
151
|
+
description:
|
|
152
|
+
'列出当前可见的 X11 窗口:窗口 id、程序名(WM_CLASS)、标题、屏幕绝对坐标、尺寸,' +
|
|
153
|
+
'按叠放顺序自上而下。用于先看清桌面再决定操作哪个窗口。只读。',
|
|
154
|
+
parameters: {
|
|
155
|
+
minArea: {
|
|
156
|
+
type: 'integer',
|
|
157
|
+
description: '可选:最小面积(像素²),默认 4,用于过滤 1x1 的辅助窗口。',
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
output: OUT,
|
|
161
|
+
execute: guard(async (args) => {
|
|
162
|
+
const x = await x11()
|
|
163
|
+
const wins = await x.listWindows({ minArea: args.minArea || 4 })
|
|
164
|
+
if (!wins.length) return ok('(没有可见窗口)')
|
|
165
|
+
const lines = wins.map((w, i) => `${i + 1}. ${fmtWindow(w)}`)
|
|
166
|
+
return ok(`${wins.length} visible window(s), top to bottom:\n${lines.join('\n')}`)
|
|
167
|
+
}),
|
|
168
|
+
}))
|
|
169
|
+
|
|
170
|
+
ctx.tools.register(defineTool({
|
|
171
|
+
name: 'cua_linux_screenshot',
|
|
172
|
+
description:
|
|
173
|
+
'截图并写入 PNG 文件,返回绝对路径与尺寸。不传 wid 抓全屏;传 wid 抓该窗口(内部抓 root 再裁,' +
|
|
174
|
+
'避免 compositor 下对子窗口取图拿到黑图)。文件写在系统临时目录,可用路径直接查看。',
|
|
175
|
+
parameters: {
|
|
176
|
+
wid: { type: 'integer', description: '可选:窗口 id(cua_linux_windows 输出,十六进制转十进制)。' },
|
|
177
|
+
path: { type: 'string', description: '可选:输出路径,默认写到系统临时目录。' },
|
|
178
|
+
},
|
|
179
|
+
output: OUT,
|
|
180
|
+
execute: guard(async (args) => {
|
|
181
|
+
const fs = await import('node:fs')
|
|
182
|
+
const os = await import('node:os')
|
|
183
|
+
const path = await import('node:path')
|
|
184
|
+
const x = await x11()
|
|
185
|
+
const shot = await x.screenshot(args.wid || null)
|
|
186
|
+
const out = args.path || path.join(os.tmpdir(), `cua-linux-${Date.now()}.png`)
|
|
187
|
+
fs.writeFileSync(out, shot.png)
|
|
188
|
+
const kb = (shot.png.length / 1024).toFixed(0)
|
|
189
|
+
return ok(`Screenshot ${shot.width}x${shot.height} (${kb}KB)\nPath: ${out}`)
|
|
190
|
+
}),
|
|
191
|
+
}))
|
|
192
|
+
|
|
193
|
+
ctx.tools.register(defineTool({
|
|
194
|
+
name: 'cua_linux_click',
|
|
195
|
+
description:
|
|
196
|
+
'在窗口本地像素坐标处点击。默认先把窗口置顶(activate=true)——X11 下指针命中永远是最上层窗口,' +
|
|
197
|
+
'不置顶的点击会静默落到别的窗口。坐标以窗口左上角为原点。',
|
|
198
|
+
parameters: {
|
|
199
|
+
wid: { type: 'integer', required: true, description: '目标窗口 id。' },
|
|
200
|
+
x: { type: 'integer', required: true, description: '窗口本地 x(像素)。' },
|
|
201
|
+
y: { type: 'integer', required: true, description: '窗口本地 y(像素)。' },
|
|
202
|
+
button: { type: 'integer', description: '1=左键(默认)2=中键 3=右键。' },
|
|
203
|
+
count: { type: 'integer', description: '点击次数,默认 1(2 即双击)。' },
|
|
204
|
+
activate: { type: 'boolean', description: '是否先置顶窗口,默认 true。' },
|
|
205
|
+
},
|
|
206
|
+
output: OUT,
|
|
207
|
+
execute: guard(async (args) => {
|
|
208
|
+
const x = await x11()
|
|
209
|
+
// 先校验坐标:越界是确定性失败,不该靠"看画面有没有变"来发现
|
|
210
|
+
const size = await x.windowSize(args.wid)
|
|
211
|
+
if (args.x < 0 || args.y < 0 || args.x >= size.width || args.y >= size.height) {
|
|
212
|
+
return fail(`✗ Out of bounds: window 0x${Number(args.wid).toString(16)} is ${size.width}x${size.height}, got (${args.x},${args.y})`)
|
|
213
|
+
}
|
|
214
|
+
if (args.activate !== false) await x.ensureOnTop(args.wid)
|
|
215
|
+
const j = await actVerifiedVisual({ wid: args.wid, point: { x: args.x, y: args.y } }, (X) =>
|
|
216
|
+
X.click(args.wid, args.x, args.y, { button: args.button || 1, count: args.count || 1 }))
|
|
217
|
+
return ok(`Clicked 0x${Number(args.wid).toString(16)} at (${args.x},${args.y}) button=${args.button || 1} count=${args.count || 1}\nVerify: ${line(j)}`)
|
|
218
|
+
}),
|
|
219
|
+
}))
|
|
220
|
+
|
|
221
|
+
ctx.tools.register(defineTool({
|
|
222
|
+
name: 'cua_linux_key',
|
|
223
|
+
description:
|
|
224
|
+
'按组合键或特殊键,例如 "ctrl+t" / "shift+tab" / "Return" / "Escape" / "F5"。' +
|
|
225
|
+
'发给当前焦点窗口,因此通常需要先 cua_linux_click 把焦点点进去。',
|
|
226
|
+
parameters: {
|
|
227
|
+
combo: { type: 'string', required: true, description: '组合键,用 + 连接,如 "ctrl+shift+t"。' },
|
|
228
|
+
},
|
|
229
|
+
output: OUT,
|
|
230
|
+
execute: guard(async (args) => {
|
|
231
|
+
const j = await actVerifiedVisual({}, (X) => X.pressKey(args.combo))
|
|
232
|
+
return ok(`Sent key: ${args.combo}\nVerify: ${line(j)}`)
|
|
233
|
+
}),
|
|
234
|
+
}))
|
|
235
|
+
|
|
236
|
+
ctx.tools.register(defineTool({
|
|
237
|
+
name: 'cua_linux_type',
|
|
238
|
+
description:
|
|
239
|
+
'逐字输入 ASCII 文本(字母、数字、常用符号)。非 ASCII 字符(中文、emoji)会明确报错——' +
|
|
240
|
+
'XTEST 只能发 keycode,产生不了输入法的 commit 事件,中文请走 CDP 通道。' +
|
|
241
|
+
'注意:目标输入框若开着中文输入法,字母会进入候选串(composition)而不是直接上屏。',
|
|
242
|
+
parameters: {
|
|
243
|
+
text: { type: 'string', required: true, description: '要输入的 ASCII 文本。' },
|
|
244
|
+
},
|
|
245
|
+
output: OUT,
|
|
246
|
+
execute: guard(async (args) => {
|
|
247
|
+
const text = String(args.text ?? '')
|
|
248
|
+
const bad = [...text].find((c) => c.charCodeAt(0) > 0x7e)
|
|
249
|
+
if (bad !== undefined) {
|
|
250
|
+
return fail(`✗ Non-ASCII character ${JSON.stringify(bad)}: XTEST cannot inject it — use the CDP channel (browsers) or the clipboard`)
|
|
251
|
+
}
|
|
252
|
+
const j = await actVerifiedVisual({}, (X) => X.typeAscii(text))
|
|
253
|
+
return ok(`Typed ${text.length} character(s): ${text}\nVerify: ${line(j)}`)
|
|
254
|
+
}),
|
|
255
|
+
}))
|
|
256
|
+
|
|
257
|
+
ctx.tools.register(defineTool({
|
|
258
|
+
name: 'cua_linux_scroll',
|
|
259
|
+
description: '在窗口本地坐标处滚动滚轮。axis=v 垂直(默认),h 水平;dir=1 向下/向右,-1 向上/向左。',
|
|
260
|
+
parameters: {
|
|
261
|
+
wid: { type: 'integer', required: true, description: '目标窗口 id。' },
|
|
262
|
+
x: { type: 'integer', required: true, description: '窗口本地 x。' },
|
|
263
|
+
y: { type: 'integer', required: true, description: '窗口本地 y。' },
|
|
264
|
+
notches: { type: 'integer', description: '滚动格数,默认 3。' },
|
|
265
|
+
axis: { type: 'string', description: '"v"(默认)或 "h"。' },
|
|
266
|
+
dir: { type: 'integer', description: '1 向下/向右(默认),-1 反向。' },
|
|
267
|
+
activate: { type: 'boolean', description: '是否先置顶,默认 true。' },
|
|
268
|
+
},
|
|
269
|
+
output: OUT,
|
|
270
|
+
execute: guard(async (args) => {
|
|
271
|
+
const x = await x11()
|
|
272
|
+
const size = await x.windowSize(args.wid)
|
|
273
|
+
if (args.x < 0 || args.y < 0 || args.x >= size.width || args.y >= size.height) {
|
|
274
|
+
return fail(`✗ Out of bounds: window is ${size.width}x${size.height}, got (${args.x},${args.y})`)
|
|
275
|
+
}
|
|
276
|
+
if (args.activate !== false) await x.ensureOnTop(args.wid)
|
|
277
|
+
const j = await actVerifiedVisual({ wid: args.wid, point: { x: args.x, y: args.y }, radius: 220 }, (X) =>
|
|
278
|
+
X.scroll(args.wid, args.x, args.y, {
|
|
279
|
+
notches: args.notches || 3,
|
|
280
|
+
axis: args.axis === 'h' ? 'h' : 'v',
|
|
281
|
+
dir: args.dir === -1 ? -1 : 1,
|
|
282
|
+
}))
|
|
283
|
+
return ok(`Scrolled 0x${Number(args.wid).toString(16)} at (${args.x},${args.y}) by ${args.notches || 3} notch(es)\nVerify: ${line(j)}`)
|
|
284
|
+
}),
|
|
285
|
+
}))
|
|
286
|
+
// ================= M2:Chromium CDP 通道 =================
|
|
287
|
+
// 浏览器场景不要用像素:selector 定位、insertText 免输入法、视口坐标与窗口是否可见无关。
|
|
288
|
+
|
|
289
|
+
const cdp = () => import('./lib/cdp.js')
|
|
290
|
+
|
|
291
|
+
/** 每次调用按需建连(~10ms),不跨调用持有连接,避免失效 WS。 */
|
|
292
|
+
async function withTab(args, fn) {
|
|
293
|
+
const m = await cdp()
|
|
294
|
+
const port = Number(args.port) || 9222
|
|
295
|
+
const c = new m.Cdp(port)
|
|
296
|
+
try {
|
|
297
|
+
const tab = await c.attach(args.tab || null)
|
|
298
|
+
return await fn(c, tab)
|
|
299
|
+
} finally {
|
|
300
|
+
c.close()
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const PORT_PARAM = { type: 'integer', description: '可选:Chromium 调试端口,默认 9222。' }
|
|
305
|
+
const TAB_PARAM = { type: 'string', description: '可选:按 URL 子串选 tab,缺省取第一个。' }
|
|
306
|
+
|
|
307
|
+
ctx.tools.register(defineTool({
|
|
308
|
+
name: 'cua_browser_launch',
|
|
309
|
+
description:
|
|
310
|
+
'启动一个带 DevTools 调试端口的 Chromium(独立 profile,不影响用户正在用的浏览器),' +
|
|
311
|
+
'等待端口就绪后返回。之后可用 cua_browser_* 系列工具直控。' +
|
|
312
|
+
'注意:已运行的 Chrome 无法事后附加调试端口,必须这样才能接管。',
|
|
313
|
+
parameters: {
|
|
314
|
+
port: { type: 'integer', description: '调试端口,默认 9222。' },
|
|
315
|
+
url: { type: 'string', description: '启动后打开的地址,默认 about:blank。' },
|
|
316
|
+
headless: { type: 'boolean', description: '是否无头,默认 false。' },
|
|
317
|
+
},
|
|
318
|
+
output: OUT,
|
|
319
|
+
execute: guard(async (args) => {
|
|
320
|
+
const m = await cdp()
|
|
321
|
+
const port = args.port || 9222
|
|
322
|
+
if (await m.hasDebugPort(port)) {
|
|
323
|
+
return ok(`Port ${port} already exposes a DevTools endpoint; reusing it (nothing was launched).`)
|
|
324
|
+
}
|
|
325
|
+
const proc = await m.launch({ port, url: args.url || 'about:blank', headless: !!args.headless })
|
|
326
|
+
await m.waitReady(port, 25000)
|
|
327
|
+
return ok(`Launched Chromium: pid=${proc.pid} port=${port}\nURL: ${args.url || 'about:blank'}`)
|
|
328
|
+
}),
|
|
329
|
+
}))
|
|
330
|
+
|
|
331
|
+
ctx.tools.register(defineTool({
|
|
332
|
+
name: 'cua_browser_pages',
|
|
333
|
+
description: '列出调试端口上的所有页面(标题 + URL),用于选择目标 tab。只读。',
|
|
334
|
+
parameters: { port: PORT_PARAM },
|
|
335
|
+
output: OUT,
|
|
336
|
+
execute: guard(async (args) => {
|
|
337
|
+
const m = await cdp()
|
|
338
|
+
const port = args.port || 9222
|
|
339
|
+
const c = new m.Cdp(port)
|
|
340
|
+
const tabs = await c.listTabs()
|
|
341
|
+
if (!tabs.length) return ok('(没有页面)')
|
|
342
|
+
return ok(tabs.map((t, i) => `${i + 1}. ${t.title || '(无标题)'} — ${t.url}`).join('\n'))
|
|
343
|
+
}),
|
|
344
|
+
}))
|
|
345
|
+
|
|
346
|
+
ctx.tools.register(defineTool({
|
|
347
|
+
name: 'cua_browser_goto',
|
|
348
|
+
description: '在指定 tab 内导航到 URL,并等到 document.readyState 为 complete。',
|
|
349
|
+
parameters: {
|
|
350
|
+
url: { type: 'string', required: true, description: '目标地址。' },
|
|
351
|
+
port: PORT_PARAM, tab: TAB_PARAM,
|
|
352
|
+
},
|
|
353
|
+
output: OUT,
|
|
354
|
+
execute: guard(async (args) => {
|
|
355
|
+
const r = await withTab(args, async (c) => {
|
|
356
|
+
await c.navigate(args.url)
|
|
357
|
+
await c.settle({ timeoutMs: 12000 })
|
|
358
|
+
return c.evaluate('({ url: location.href, title: document.title })')
|
|
359
|
+
})
|
|
360
|
+
// 闭环:实际落地的 URL 可能与请求的不同(重定向 / 登录墙 / 错误页)
|
|
361
|
+
const landed = r?.url || ''
|
|
362
|
+
const host = (() => { try { return new URL(args.url).host } catch { return '' } })()
|
|
363
|
+
const okHost = host ? landed.includes(host) : true
|
|
364
|
+
return ok(`Navigated to ${args.url}\nLanded on: ${landed}${okHost ? '' : ' ⚠ host differs from the target'}\nTitle: ${r?.title || '(empty)'}`)
|
|
365
|
+
}),
|
|
366
|
+
}))
|
|
367
|
+
|
|
368
|
+
ctx.tools.register(defineTool({
|
|
369
|
+
name: 'cua_browser_fill',
|
|
370
|
+
description:
|
|
371
|
+
'按 CSS selector 聚焦输入框并填入文本。走 Input.insertText,中文/emoji 直接写入,' +
|
|
372
|
+
'不经过输入法、不占用剪贴板,也不会出现候选串。',
|
|
373
|
+
parameters: {
|
|
374
|
+
selector: { type: 'string', required: true, description: 'CSS selector,如 "#sb_form_q"。' },
|
|
375
|
+
text: { type: 'string', required: true, description: '要填入的文本(任意 Unicode)。' },
|
|
376
|
+
port: PORT_PARAM, tab: TAB_PARAM,
|
|
377
|
+
},
|
|
378
|
+
output: OUT,
|
|
379
|
+
execute: guard(async (args) => {
|
|
380
|
+
const val = await withTab(args, async (c) => {
|
|
381
|
+
await c.fill(args.selector, args.text)
|
|
382
|
+
return c.evaluate(
|
|
383
|
+
`(() => { const e = document.querySelector(${JSON.stringify(args.selector)});
|
|
384
|
+
return e ? e.value : null; })()`)
|
|
385
|
+
})
|
|
386
|
+
// 闭环:读回值必须与写入一致,否则就是没进框(比如元素是只读的或被遮挡层接管)
|
|
387
|
+
if (val === null) return fail(`✗ Element not found: ${args.selector}; nothing was written`)
|
|
388
|
+
if (val !== String(args.text)) {
|
|
389
|
+
return fail(`✗ Write did not stick: expected ${JSON.stringify(args.text)}, read back ${JSON.stringify(val)}`)
|
|
390
|
+
}
|
|
391
|
+
return ok(`Filled ${JSON.stringify(args.text)}\nVerify: ✓ read-back matches`)
|
|
392
|
+
}),
|
|
393
|
+
}))
|
|
394
|
+
|
|
395
|
+
ctx.tools.register(defineTool({
|
|
396
|
+
name: 'cua_browser_click',
|
|
397
|
+
description:
|
|
398
|
+
'按 CSS selector 点击元素。发的是真实鼠标事件(dispatchMouseEvent + 元素中心点),' +
|
|
399
|
+
'会触发 pointerdown/mousedown/up,比 el.click() 更接近人手;坐标是视口坐标。',
|
|
400
|
+
parameters: {
|
|
401
|
+
selector: { type: 'string', required: true, description: 'CSS selector。' },
|
|
402
|
+
port: PORT_PARAM, tab: TAB_PARAM,
|
|
403
|
+
},
|
|
404
|
+
output: OUT,
|
|
405
|
+
execute: guard(async (args) => {
|
|
406
|
+
const v = await import('./lib/verify.js')
|
|
407
|
+
const j = await withTab(args, async (c) => {
|
|
408
|
+
await c.watchBegin()
|
|
409
|
+
await c.click(args.selector)
|
|
410
|
+
await sleep(350)
|
|
411
|
+
return v.judgeDom(await c.watchRead())
|
|
412
|
+
})
|
|
413
|
+
return ok(`Clicked ${args.selector}\nVerify: ${line(j)}`)
|
|
414
|
+
}),
|
|
415
|
+
}))
|
|
416
|
+
|
|
417
|
+
ctx.tools.register(defineTool({
|
|
418
|
+
name: 'cua_browser_key',
|
|
419
|
+
description: '向当前页面发送按键:Enter / Tab / Escape / Backspace / ArrowUp / ArrowDown。',
|
|
420
|
+
parameters: {
|
|
421
|
+
key: { type: 'string', required: true, description: '按键名,如 "Enter"。' },
|
|
422
|
+
port: PORT_PARAM, tab: TAB_PARAM,
|
|
423
|
+
},
|
|
424
|
+
output: OUT,
|
|
425
|
+
execute: guard(async (args) => {
|
|
426
|
+
const v = await import('./lib/verify.js')
|
|
427
|
+
const j = await withTab(args, async (c) => {
|
|
428
|
+
await c.watchBegin()
|
|
429
|
+
await c.press(args.key)
|
|
430
|
+
await sleep(400)
|
|
431
|
+
return v.judgeDom(await c.watchRead())
|
|
432
|
+
})
|
|
433
|
+
return ok(`Sent key: ${args.key}\nVerify: ${line(j)}`)
|
|
434
|
+
}),
|
|
435
|
+
}))
|
|
436
|
+
|
|
437
|
+
ctx.tools.register(defineTool({
|
|
438
|
+
name: 'cua_browser_read',
|
|
439
|
+
description: '读取页面文本:传 selector 取该元素 innerText,缺省取整个 body。',
|
|
440
|
+
parameters: {
|
|
441
|
+
selector: { type: 'string', description: '可选:CSS selector,默认 body。' },
|
|
442
|
+
maxChars: { type: 'integer', description: '可选:最多返回多少字符,默认 4000。' },
|
|
443
|
+
port: PORT_PARAM, tab: TAB_PARAM,
|
|
444
|
+
},
|
|
445
|
+
output: OUT,
|
|
446
|
+
execute: guard(async (args) => {
|
|
447
|
+
const text = await withTab(args, (c) => c.text(args.selector || 'body'))
|
|
448
|
+
const max = args.maxChars || 4000
|
|
449
|
+
const clipped = text.length > max ? `${text.slice(0, max)}\n…(已截断,共 ${text.length} 字符)` : text
|
|
450
|
+
return ok(clipped || '(空)')
|
|
451
|
+
}),
|
|
452
|
+
}))
|
|
453
|
+
|
|
454
|
+
ctx.tools.register(defineTool({
|
|
455
|
+
name: 'cua_browser_screenshot',
|
|
456
|
+
description:
|
|
457
|
+
'页面截图写 PNG(走渲染管线,与窗口是否可见、是否被遮挡无关),返回路径。',
|
|
458
|
+
parameters: {
|
|
459
|
+
path: { type: 'string', description: '可选:输出路径,默认写到系统临时目录。' },
|
|
460
|
+
port: PORT_PARAM, tab: TAB_PARAM,
|
|
461
|
+
},
|
|
462
|
+
output: OUT,
|
|
463
|
+
execute: guard(async (args) => {
|
|
464
|
+
const fs = await import('node:fs')
|
|
465
|
+
const os = await import('node:os')
|
|
466
|
+
const path = await import('node:path')
|
|
467
|
+
const png = await withTab(args, (c) => c.screenshot())
|
|
468
|
+
const out = args.path || path.join(os.tmpdir(), `cua-browser-${Date.now()}.png`)
|
|
469
|
+
fs.writeFileSync(out, png)
|
|
470
|
+
return ok(`Screenshot (${(png.length / 1024).toFixed(0)}KB)\nPath: ${out}`)
|
|
471
|
+
}),
|
|
472
|
+
}))
|
|
473
|
+
|
|
474
|
+
}
|