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/lib/verify.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* verify.js —— 动作生效的判定原语。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:注入类工具最大的问题是「静默成功」——点没点中、输没输进去、
|
|
5
|
+
* 页面跳没跳,接口都返回 success。模型看不到界面变化,就会在错误的状态上继续操作,
|
|
6
|
+
* 错误还会累积。所以每个动作至少要留下一条可复核的痕迹。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 逐像素比较两张 RGBA,返回变化比例。threshold 用来吃掉抗锯齿/光标噪声。
|
|
11
|
+
* opts.width + opts.box 可只比一块区域(在整帧缓冲上切片,不需要额外抓图)。
|
|
12
|
+
* @param {Buffer} a
|
|
13
|
+
* @param {Buffer} b
|
|
14
|
+
* @param {number} threshold
|
|
15
|
+
* @param {{width?:number, box?:{x:number,y:number,w:number,h:number}}} opts
|
|
16
|
+
*/
|
|
17
|
+
export function diffRGBA(a, b, threshold = 8, opts = {}) {
|
|
18
|
+
if (!a || !b) return null
|
|
19
|
+
if (a.length !== b.length) return { changed: Infinity, ratio: 1, incomparable: true }
|
|
20
|
+
|
|
21
|
+
const { width, box } = opts
|
|
22
|
+
const fullFrame = !box || !width
|
|
23
|
+
let x0 = 0, y0 = 0, x1 = 0, y1 = 0
|
|
24
|
+
if (fullFrame) {
|
|
25
|
+
x1 = a.length / 4
|
|
26
|
+
} else {
|
|
27
|
+
const fh = a.length / (width * 4)
|
|
28
|
+
x0 = Math.max(0, Math.floor(box.x))
|
|
29
|
+
y0 = Math.max(0, Math.floor(box.y))
|
|
30
|
+
x1 = Math.min(width, Math.ceil(box.x + box.w))
|
|
31
|
+
y1 = Math.min(fh, Math.ceil(box.y + box.h))
|
|
32
|
+
if (x1 <= x0 || y1 <= y0) return { changed: 0, total: 0, ratio: 0, empty: true }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let changed = 0
|
|
36
|
+
let total = 0
|
|
37
|
+
if (fullFrame) {
|
|
38
|
+
for (let i = 0; i < a.length; i += 4) {
|
|
39
|
+
total++
|
|
40
|
+
if (Math.abs(a[i] - b[i]) > threshold ||
|
|
41
|
+
Math.abs(a[i + 1] - b[i + 1]) > threshold ||
|
|
42
|
+
Math.abs(a[i + 2] - b[i + 2]) > threshold) changed++
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
for (let y = y0; y < y1; y++) {
|
|
46
|
+
for (let x = x0; x < x1; x++) {
|
|
47
|
+
const i = (y * width + x) * 4
|
|
48
|
+
total++
|
|
49
|
+
if (Math.abs(a[i] - b[i]) > threshold ||
|
|
50
|
+
Math.abs(a[i + 1] - b[i + 1]) > threshold ||
|
|
51
|
+
Math.abs(a[i + 2] - b[i + 2]) > threshold) changed++
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { changed, total, ratio: total ? changed / total : 0 }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 把变化比例翻译成一句人话 + 一个判断。
|
|
60
|
+
* @param {{ratio:number, changed:number}} d
|
|
61
|
+
* @param {{minRatio?:number, label?:string}} opts
|
|
62
|
+
*/
|
|
63
|
+
export function judgeVisual(d, { minRatio = 0.0005, label = 'Screen' } = {}) {
|
|
64
|
+
if (!d) return { verified: null, text: `${label}: no baseline to compare` }
|
|
65
|
+
if (d.incomparable) return { verified: null, text: `${label}: frame sizes differ, cannot compare` }
|
|
66
|
+
const pct = (d.ratio * 100).toFixed(2)
|
|
67
|
+
if (d.ratio >= minRatio) {
|
|
68
|
+
return { verified: true, text: `${label} changed (${d.changed} px / ${pct}%)` }
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
verified: false,
|
|
72
|
+
// 措辞很重要:动作**已经发出**,只是没观察到变化。写成"失败"会诱导模型重试,
|
|
73
|
+
// 在已聚焦/重复点击这类场景下重试反而危险。
|
|
74
|
+
text: `${label} did not change (${d.changed} px / ${pct}%) — the action was dispatched but no feedback was observed; it may be a repeat action, a non-interactive target, or an operation that does not repaint`,
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 判定一个区域在**动作之前**是否本身就在变(光标闪烁、加载动画、视频)。
|
|
80
|
+
* 不稳的区域上做前后比对没有意义,必须如实说"无法判定",而不是随便给个 ✓/⚠。
|
|
81
|
+
*/
|
|
82
|
+
export function judgeStability(d, { noiseRatio = 0.0008 } = {}) {
|
|
83
|
+
if (!d) return { stable: null, text: 'Region stability: no baseline' }
|
|
84
|
+
if (d.incomparable) return { stable: null, text: 'Region stability: frame sizes differ' }
|
|
85
|
+
if (d.ratio >= noiseRatio) {
|
|
86
|
+
return {
|
|
87
|
+
stable: false,
|
|
88
|
+
text: `this region already changes on its own (${(d.ratio * 100).toFixed(2)}%) — blinking caret, animation or video, so pixels cannot decide it`,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return { stable: true, text: 'region stable' }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 两级判定(信噪比思路):
|
|
96
|
+
* noise = 动作前该区域自身的变化率(光标闪烁/动画)
|
|
97
|
+
* signal = 动作后相对"动作前最后一帧"的变化率
|
|
98
|
+
*
|
|
99
|
+
* 判定顺序:
|
|
100
|
+
* 1. signal 显著高于 noise(>5 倍且超过绝对阈值)→ 动作确实改变了画面
|
|
101
|
+
* 2. noise 本身就大 → 无法判定(不猜)
|
|
102
|
+
* 3. 区域没变但整窗变了 → 影响在别处,仍算生效
|
|
103
|
+
* 4. 都没有 → 警告"已发出但无反馈"
|
|
104
|
+
*/
|
|
105
|
+
export function judgeTwoTier({ noise = 0, signal, window: winDiff, areaLabel = 'Area around the action point' }) {
|
|
106
|
+
const sig = signal || {}
|
|
107
|
+
const sigRatio = sig.ratio || 0
|
|
108
|
+
if (sigRatio > 0.0005 && sigRatio > noise * 5) {
|
|
109
|
+
return { verified: true, text: `${areaLabel} changed (${sig.changed || 0} px / ${(sigRatio * 100).toFixed(2)}%)` }
|
|
110
|
+
}
|
|
111
|
+
// 分档:噪声很小时(光标闪烁约 0.1~0.3%)按"无变化"处理更有用;
|
|
112
|
+
// 只有大面积动画(>0.5%,转圈/视频/滚动)才真的无法逐点比对。
|
|
113
|
+
if (noise > 0.005) {
|
|
114
|
+
return {
|
|
115
|
+
verified: null,
|
|
116
|
+
text: `Undecidable: the region changes on its own (${(noise * 100).toFixed(2)}%), while the action changed only ${(sigRatio * 100).toFixed(2)}%`,
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (winDiff && winDiff.ratio !== undefined && winDiff.ratio > 0.0005) {
|
|
120
|
+
return {
|
|
121
|
+
verified: true,
|
|
122
|
+
text: `${areaLabel} itself did not change, but the rest of the window changed ${(winDiff.ratio * 100).toFixed(2)}% — the effect landed elsewhere (dialog, list refresh, ...)`,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
verified: false,
|
|
127
|
+
text: `nothing changed around the action point or elsewhere — the event was dispatched but no feedback was observed; it may be a repeat action, a non-interactive target, or an operation that does not repaint`,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** DOM 侧:装一个 MutationObserver 计数器,动作后读突变数与 URL 变化。 */
|
|
132
|
+
export const WATCH_BEGIN = `(() => {
|
|
133
|
+
window.__cuaMut = 0;
|
|
134
|
+
window.__cuaUrl = location.href;
|
|
135
|
+
if (!window.__cuaMo) {
|
|
136
|
+
window.__cuaMo = new MutationObserver((ms) => { window.__cuaMut += ms.length });
|
|
137
|
+
window.__cuaMo.observe(document.documentElement, { childList: true, subtree: true, attributes: true });
|
|
138
|
+
}
|
|
139
|
+
return true;
|
|
140
|
+
})()`
|
|
141
|
+
|
|
142
|
+
export const WATCH_READ = `(() => ({
|
|
143
|
+
mutations: window.__cuaMut || 0,
|
|
144
|
+
urlChanged: (window.__cuaUrl || '') !== location.href,
|
|
145
|
+
url: location.href,
|
|
146
|
+
}))()`
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 把 DOM 侧观测结果翻译成判定。
|
|
150
|
+
* 注意:0 突变不代表失败——有些操作(聚焦、选中)不产生 DOM 突变。
|
|
151
|
+
*/
|
|
152
|
+
export function judgeDom(w, { minMutations = 1 } = {}) {
|
|
153
|
+
if (!w) return { verified: null, text: 'Page: no observation available' }
|
|
154
|
+
if (w.urlChanged) return { verified: true, text: `page navigated → ${w.url}` }
|
|
155
|
+
if (w.mutations >= minMutations) {
|
|
156
|
+
return { verified: true, text: `DOM changed in ${w.mutations} place(s)` }
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
verified: false,
|
|
160
|
+
text: `no DOM change (0 mutations, URL unchanged) — the target may not be interactive, or this operation produces no DOM change`,
|
|
161
|
+
}
|
|
162
|
+
}
|
package/lib/x11.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x11.js —— X11 后端(纯 JS,经 x11 包)。
|
|
3
|
+
*
|
|
4
|
+
* 设计要点(都是这次踩出来的):
|
|
5
|
+
* 1. 坐标统一:对外一律「窗口本地像素」,进出接口时换算成 root 坐标。
|
|
6
|
+
* 2. 动作前必须 ensureOnTop:X11 指针命中永远是最上层窗口,窗口被挡时点击会
|
|
7
|
+
* 静默打到别的窗口上,而且 XRaiseWindow 在 mutter 下无效,必须发 EWMH。
|
|
8
|
+
* 3. 截图抓 root 再裁:对子窗口直接 GetImage 在 compositor 下常拿到黑图。
|
|
9
|
+
* 4. 键盘走 GetKeyboardMapping 建 keysym→(keycode, shift) 表,不写死键码。
|
|
10
|
+
*/
|
|
11
|
+
import x11 from 'x11'
|
|
12
|
+
import { encodePNG, xImageToRGBA } from './png.js'
|
|
13
|
+
|
|
14
|
+
const call = (X, method, ...args) =>
|
|
15
|
+
new Promise((res, rej) => X[method](...args, (e, r) => (e ? rej(e) : res(r))))
|
|
16
|
+
|
|
17
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
|
|
18
|
+
|
|
19
|
+
const KEYSYMS = {
|
|
20
|
+
Return: 0xff0d, Tab: 0xff09, Escape: 0xff1b, BackSpace: 0xff08, Delete: 0xffff,
|
|
21
|
+
Left: 0xff51, Up: 0xff52, Right: 0xff53, Down: 0xff54,
|
|
22
|
+
Home: 0xff50, End: 0xff57, Prior: 0xff55, Next: 0xff56,
|
|
23
|
+
shift: 0xffe1, control: 0xffe3, alt: 0xffe9, super: 0xffeb,
|
|
24
|
+
}
|
|
25
|
+
for (let i = 1; i <= 12; i++) KEYSYMS[`F${i}`] = 0xffbe + i - 1
|
|
26
|
+
|
|
27
|
+
const MOD_ALIASES = {
|
|
28
|
+
ctrl: 'control', control: 'control', shift: 'shift', alt: 'alt', super: 'super', meta: 'super',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let state = null
|
|
32
|
+
|
|
33
|
+
/** 建立(并复用)到 X server 的连接。 */
|
|
34
|
+
export async function connect() {
|
|
35
|
+
if (state) return state
|
|
36
|
+
const display = await new Promise((res, rej) => {
|
|
37
|
+
x11.createClient((err, d) => (err ? rej(err) : res(d)))
|
|
38
|
+
})
|
|
39
|
+
const X = display.client
|
|
40
|
+
const root = display.screen[0].root
|
|
41
|
+
// require(name, cb) —— 不传 cb 会在扩展加载完成时抛 'callback is not a function'
|
|
42
|
+
const xtest = await new Promise((res, rej) =>
|
|
43
|
+
X.require('xtest', (e, ext) => (e ? rej(e) : res(ext))))
|
|
44
|
+
|
|
45
|
+
const atoms = {
|
|
46
|
+
netWmName: await call(X, 'InternAtom', false, '_NET_WM_NAME'),
|
|
47
|
+
utf8: await call(X, 'InternAtom', false, 'UTF8_STRING'),
|
|
48
|
+
netActiveWindow: await call(X, 'InternAtom', false, '_NET_ACTIVE_WINDOW'),
|
|
49
|
+
netWmPid: await call(X, 'InternAtom', false, '_NET_WM_PID'),
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
state = {
|
|
53
|
+
display, X, root, xtest, atoms,
|
|
54
|
+
screen: { width: display.screen[0].pixel_width, height: display.screen[0].pixel_height },
|
|
55
|
+
keymap: null,
|
|
56
|
+
}
|
|
57
|
+
return state
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function close() {
|
|
61
|
+
if (state) {
|
|
62
|
+
try { state.X.terminate?.() } catch { /* ignore */ }
|
|
63
|
+
state = null
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function getPropData(wid, atom, maxLen = 4096) {
|
|
68
|
+
const { X } = await connect()
|
|
69
|
+
const p = await call(X, 'GetProperty', false, wid, atom, 0, 0, maxLen)
|
|
70
|
+
return p?.data && p.data.length ? p.data : null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function getTitle(wid) {
|
|
74
|
+
const { atoms } = await connect()
|
|
75
|
+
const utf8 = await getPropData(wid, atoms.netWmName)
|
|
76
|
+
if (utf8) return utf8.toString('utf8')
|
|
77
|
+
const legacy = await getPropData(wid, (await connect()).X.atoms.WM_NAME)
|
|
78
|
+
return legacy ? legacy.toString('latin1').replace(/\0+$/, '') : ''
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function getApp(wid) {
|
|
82
|
+
const { X } = await connect()
|
|
83
|
+
const data = await getPropData(wid, X.atoms.WM_CLASS)
|
|
84
|
+
if (!data) return ''
|
|
85
|
+
const parts = data.toString('latin1').split('\0').filter(Boolean)
|
|
86
|
+
return parts[1] || parts[0] || ''
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function getPid(wid) {
|
|
90
|
+
const { atoms } = await connect()
|
|
91
|
+
const data = await getPropData(wid, atoms.netWmPid, 16)
|
|
92
|
+
return data && data.length >= 4 ? data.readUInt32LE(0) : null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 枚举可见窗口。
|
|
97
|
+
* @returns {Promise<Array<{wid:number,app:string,title:string,x:number,y:number,w:number,h:number,pid:number|null}>>}
|
|
98
|
+
*/
|
|
99
|
+
export async function listWindows({ minArea = 4, onlyMapped = true } = {}) {
|
|
100
|
+
const { X, root } = await connect()
|
|
101
|
+
const tree = await call(X, 'QueryTree', root)
|
|
102
|
+
|
|
103
|
+
const rows = await Promise.all(tree.children.map(async (wid) => {
|
|
104
|
+
try {
|
|
105
|
+
const attrs = await call(X, 'GetWindowAttributes', wid)
|
|
106
|
+
if (onlyMapped && attrs.mapState !== 2) return null
|
|
107
|
+
const g = await call(X, 'GetGeometry', wid)
|
|
108
|
+
if (g.width * g.height < minArea) return null
|
|
109
|
+
const tc = await call(X, 'TranslateCoordinates', wid, root, 0, 0)
|
|
110
|
+
const [app, title, pid] = await Promise.all([getApp(wid), getTitle(wid), getPid(wid)])
|
|
111
|
+
return { wid, app, title, pid, x: tc.destX, y: tc.destY, w: g.width, h: g.height }
|
|
112
|
+
} catch {
|
|
113
|
+
return null
|
|
114
|
+
}
|
|
115
|
+
}))
|
|
116
|
+
|
|
117
|
+
// QueryTree 是自下而上,反过来 = 自顶向下(越靠前越上层)
|
|
118
|
+
return rows.filter(Boolean).reverse()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** 把窗口提到最前并抢焦点(EWMH _NET_ACTIVE_WINDOW,mutter 只认这个)。 */
|
|
122
|
+
export async function ensureOnTop(wid, settleMs = 180) {
|
|
123
|
+
const { X, root, atoms } = await connect()
|
|
124
|
+
const ev = Buffer.alloc(32)
|
|
125
|
+
ev[0] = 33 // ClientMessage
|
|
126
|
+
ev[1] = 32 // format = 32
|
|
127
|
+
ev.writeUInt32LE(wid >>> 0, 4)
|
|
128
|
+
ev.writeUInt32LE(atoms.netActiveWindow >>> 0, 8)
|
|
129
|
+
ev.writeUInt32LE(2, 12) // source indication: 2 = pager(绕过防焦点窃取)
|
|
130
|
+
ev.writeUInt32LE(0, 16) // timestamp: CurrentTime
|
|
131
|
+
ev.writeUInt32LE(0, 20)
|
|
132
|
+
ev.writeUInt32LE(0, 24)
|
|
133
|
+
ev.writeUInt32LE(0, 28)
|
|
134
|
+
// SubstructureRedirectMask | SubstructureNotifyMask
|
|
135
|
+
await call(X, 'SendEvent', root, false, 0x00180000, ev)
|
|
136
|
+
await sleep(settleMs)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 窗口本地坐标 -> root 坐标。 */
|
|
140
|
+
export async function toRoot(wid, x, y) {
|
|
141
|
+
const { X, root } = await connect()
|
|
142
|
+
const tc = await call(X, 'TranslateCoordinates', wid, root, Math.round(x), Math.round(y))
|
|
143
|
+
return { x: tc.destX, y: tc.destY }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function fakeInput(type, detail, x = 0, y = 0) {
|
|
147
|
+
// ⚠️ 第 4 个参数是 root window,不是「窗口 id 可有可无」——传 0 时按键会被
|
|
148
|
+
// 服务器静默丢弃(按钮/移动恰好还能用,所以很容易误判成"注入器是好的")。
|
|
149
|
+
const { xtest, X, root } = await connect()
|
|
150
|
+
xtest.FakeInput(type, detail, 0, root, Math.round(x), Math.round(y))
|
|
151
|
+
X.Flush?.()
|
|
152
|
+
await sleep(18)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 把指针移到窗口本地坐标。 */
|
|
156
|
+
export async function movePointer(wid, x, y) {
|
|
157
|
+
const p = await toRoot(wid, x, y)
|
|
158
|
+
await fakeInput(6, 0, p.x, p.y) // 6 = MotionNotify
|
|
159
|
+
return p
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 点击。button: 1 左 / 2 中 / 3 右。
|
|
164
|
+
* 依赖:调用方应先 ensureOnTop,否则命中的可能是别的窗口。
|
|
165
|
+
*/
|
|
166
|
+
export async function click(wid, x, y, { button = 1, count = 1 } = {}) {
|
|
167
|
+
const p = await movePointer(wid, x, y)
|
|
168
|
+
for (let i = 0; i < count; i++) {
|
|
169
|
+
await fakeInput(4, button, p.x, p.y) // ButtonPress
|
|
170
|
+
await fakeInput(5, button, p.x, p.y) // ButtonRelease
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** 滚轮:vertical -> 4/5,horizontal -> 6/7。dir=1 向下/向右。 */
|
|
175
|
+
export async function scroll(wid, x, y, { notches = 3, axis = 'v', dir = 1 } = {}) {
|
|
176
|
+
const p = await movePointer(wid, x, y)
|
|
177
|
+
const btn = axis === 'h' ? (dir > 0 ? 7 : 6) : (dir > 0 ? 5 : 4)
|
|
178
|
+
for (let i = 0; i < Math.abs(notches); i++) {
|
|
179
|
+
await fakeInput(4, btn, p.x, p.y)
|
|
180
|
+
await fakeInput(5, btn, p.x, p.y)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function ensureKeymap() {
|
|
185
|
+
const { X, keymap } = await connect()
|
|
186
|
+
if (keymap) return keymap
|
|
187
|
+
const rows = await call(X, 'GetKeyboardMapping', 8, 248)
|
|
188
|
+
const map = new Map()
|
|
189
|
+
rows.forEach((row, i) => {
|
|
190
|
+
const keycode = 8 + i
|
|
191
|
+
row.forEach((ks, level) => {
|
|
192
|
+
if (ks && !map.has(ks)) map.set(ks, { keycode, shift: level % 2 === 1 })
|
|
193
|
+
})
|
|
194
|
+
})
|
|
195
|
+
state.keymap = map
|
|
196
|
+
return map
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function keysymOf(name) {
|
|
200
|
+
if (Object.prototype.hasOwnProperty.call(KEYSYMS, name)) return KEYSYMS[name]
|
|
201
|
+
if (name.length === 1) {
|
|
202
|
+
const o = name.charCodeAt(0)
|
|
203
|
+
if (o >= 0x20 && o <= 0x7e) return o
|
|
204
|
+
throw new Error(`非 ASCII 字符 ${name} 无法用 XTEST 注入,请走剪贴板/CDP`)
|
|
205
|
+
}
|
|
206
|
+
throw new Error(`未知按键: ${name}`)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** 按组合键,如 "ctrl+shift+t" / "Return" / "a"。 */
|
|
210
|
+
export async function pressKey(combo) {
|
|
211
|
+
const parts = String(combo).split('+').map((s) => s.trim()).filter(Boolean)
|
|
212
|
+
const mods = parts.slice(0, -1).map((m) => MOD_ALIASES[m.toLowerCase()] || m.toLowerCase())
|
|
213
|
+
const base = parts[parts.length - 1]
|
|
214
|
+
const map = await ensureKeymap()
|
|
215
|
+
const modCodes = mods.map((m) => {
|
|
216
|
+
const hit = map.get(KEYSYMS[m])
|
|
217
|
+
if (!hit) throw new Error(`不支持的修饰键: ${m}`)
|
|
218
|
+
return hit.keycode
|
|
219
|
+
})
|
|
220
|
+
for (const c of modCodes) await fakeInput(2, c)
|
|
221
|
+
await tapKeysymNoMods(keysymOf(base))
|
|
222
|
+
for (const c of modCodes.slice().reverse()) await fakeInput(3, c)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** 只按主键(Shift 由 keymap 自动处理)。 */
|
|
226
|
+
async function tapKeysymNoMods(ks) {
|
|
227
|
+
const map = await ensureKeymap()
|
|
228
|
+
const hit = map.get(ks)
|
|
229
|
+
if (!hit) throw new Error(`当前键盘布局里没有 keysym 0x${ks.toString(16)}`)
|
|
230
|
+
if (hit.shift) {
|
|
231
|
+
const shiftCode = map.get(KEYSYMS.shift)?.keycode
|
|
232
|
+
await fakeInput(2, shiftCode)
|
|
233
|
+
await fakeInput(2, hit.keycode)
|
|
234
|
+
await fakeInput(3, hit.keycode)
|
|
235
|
+
await fakeInput(3, shiftCode)
|
|
236
|
+
} else {
|
|
237
|
+
await fakeInput(2, hit.keycode)
|
|
238
|
+
await fakeInput(3, hit.keycode)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** 逐字输入 ASCII(中文会明确报错,不静默失败)。 */
|
|
243
|
+
export async function typeAscii(text) {
|
|
244
|
+
for (const ch of String(text)) {
|
|
245
|
+
if (ch === ' ') await tapKeysymNoMods(0x20)
|
|
246
|
+
else await tapKeysymNoMods(keysymOf(ch))
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 抓原始像素(RGBA),用于动作前后的差异比较——不做 PNG 编码,快得多。
|
|
252
|
+
* @returns {Promise<{width:number,height:number,rgba:Buffer}>}
|
|
253
|
+
*/
|
|
254
|
+
export async function captureRaw(win = null) {
|
|
255
|
+
const { X, root, screen } = await connect()
|
|
256
|
+
let x = 0, y = 0, w = screen.width, h = screen.height
|
|
257
|
+
if (win) {
|
|
258
|
+
const g = await call(X, 'GetGeometry', win)
|
|
259
|
+
const tc = await call(X, 'TranslateCoordinates', win, root, 0, 0)
|
|
260
|
+
x = Math.max(0, tc.destX)
|
|
261
|
+
y = Math.max(0, tc.destY)
|
|
262
|
+
w = Math.min(g.width, screen.width - x)
|
|
263
|
+
h = Math.min(g.height, screen.height - y)
|
|
264
|
+
if (w <= 0 || h <= 0) throw new Error('窗口不在屏幕可见区域')
|
|
265
|
+
}
|
|
266
|
+
const img = await call(X, 'GetImage', 2, root, x, y, w, h, 0xffffffff)
|
|
267
|
+
return { width: w, height: h, rgba: xImageToRGBA(img.data, w, h) }
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* 截图。win 为空则抓全屏。
|
|
272
|
+
* @returns {Promise<{width:number,height:number,png:Buffer}>}
|
|
273
|
+
*/
|
|
274
|
+
export async function screenshot(win = null) {
|
|
275
|
+
const raw = await captureRaw(win)
|
|
276
|
+
return { width: raw.width, height: raw.height, png: encodePNG(raw.width, raw.height, raw.rgba) }
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* 只抓一块区域(比全窗便宜,且 diff 时不需要再切片)。
|
|
281
|
+
* box 是「窗口本地坐标」;越界部分会被裁掉。
|
|
282
|
+
* @returns {Promise<{width:number,height:number,rgba:Buffer,box:{x:number,y:number,w:number,h:number}}>}
|
|
283
|
+
*/
|
|
284
|
+
export async function captureBox(win, box) {
|
|
285
|
+
const { X, root, screen } = await connect()
|
|
286
|
+
let ox = 0, oy = 0, ww = screen.width, wh = screen.height
|
|
287
|
+
if (win) {
|
|
288
|
+
const g = await call(X, 'GetGeometry', win)
|
|
289
|
+
const tc = await call(X, 'TranslateCoordinates', win, root, 0, 0)
|
|
290
|
+
ox = Math.max(0, tc.destX); oy = Math.max(0, tc.destY)
|
|
291
|
+
ww = Math.min(g.width, screen.width - ox); wh = Math.min(g.height, screen.height - oy)
|
|
292
|
+
}
|
|
293
|
+
const x = Math.max(0, Math.min(ww - 1, Math.round(box.x)))
|
|
294
|
+
const y = Math.max(0, Math.min(wh - 1, Math.round(box.y)))
|
|
295
|
+
const w = Math.max(1, Math.min(ww - x, Math.round(box.w)))
|
|
296
|
+
const h = Math.max(1, Math.min(wh - y, Math.round(box.h)))
|
|
297
|
+
const img = await call(X, 'GetImage', 2, root, ox + x, oy + y, w, h, 0xffffffff)
|
|
298
|
+
return { width: w, height: h, rgba: xImageToRGBA(img.data, w, h), box: { x, y, w, h } }
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** 窗口尺寸,用于动作前校验坐标是否越界。 */
|
|
302
|
+
export async function windowSize(wid) {
|
|
303
|
+
const { X } = await connect()
|
|
304
|
+
const g = await call(X, 'GetGeometry', wid)
|
|
305
|
+
return { width: g.width, height: g.height }
|
|
306
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-cua-linux",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Linux (X11/XWayland) computer-use backend for DeepSeek Harness: window enumeration, screenshots, XTEST input injection, and a CDP channel for Chromium.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"dsh",
|
|
10
|
+
"dsh-plugin",
|
|
11
|
+
"computer-use",
|
|
12
|
+
"cua",
|
|
13
|
+
"linux",
|
|
14
|
+
"x11",
|
|
15
|
+
"wayland",
|
|
16
|
+
"xdotool",
|
|
17
|
+
"cdp",
|
|
18
|
+
"screenshot"
|
|
19
|
+
],
|
|
20
|
+
"files": [
|
|
21
|
+
"index.js",
|
|
22
|
+
"lib/",
|
|
23
|
+
"cordis.patch.yml",
|
|
24
|
+
"README.md",
|
|
25
|
+
"README.zh.md",
|
|
26
|
+
"LICENSE",
|
|
27
|
+
"assets/",
|
|
28
|
+
"screenshots.json",
|
|
29
|
+
"CHANGELOG.md"
|
|
30
|
+
],
|
|
31
|
+
"dsh": {
|
|
32
|
+
"bundle": {
|
|
33
|
+
"patch": "./cordis.patch.yml"
|
|
34
|
+
},
|
|
35
|
+
"compatibility": {
|
|
36
|
+
"node": ">=22.18.0"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@deepseek-ai/dsh-tools": ">=0.1.7-rc.1 <0.2.0-0 || >=0.2.0-rc.1 <0.3.0-0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"@deepseek-ai/dsh-tools": {
|
|
44
|
+
"optional": true
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/ethanrise/dsh-cua-linux.git"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"test": "node --test tests/*.test.mjs",
|
|
53
|
+
"smoke:x11": "node scripts/smoke-x11.mjs",
|
|
54
|
+
"smoke:cdp": "node scripts/smoke-cdp.mjs",
|
|
55
|
+
"check": "node --check index.js && node --check lib/*.js"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"x11": "^4.4.0"
|
|
59
|
+
},
|
|
60
|
+
"homepage": "https://github.com/ethanrise/dsh-cua-linux",
|
|
61
|
+
"bugs": {
|
|
62
|
+
"url": "https://github.com/ethanrise/dsh-cua-linux/issues"
|
|
63
|
+
}
|
|
64
|
+
}
|