offscreen-android 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/LICENSE +21 -0
- package/README.md +195 -2
- package/assets/injector/Injector.smali +1179 -0
- package/assets/injector/README.md +101 -0
- package/assets/injector/inject.dex +0 -0
- package/assets/overlay/ai-overlay.apk +0 -0
- package/assets/scrcpy-server.jar +0 -0
- package/bin/aim.mjs +220 -0
- package/bin/asr.mjs +81 -0
- package/bin/audcap.mjs +128 -0
- package/bin/mcp-server.mjs +410 -0
- package/bin/vdisplay.mjs +305 -0
- package/bin/vdisplayd.mjs +398 -0
- package/bin/vidcap.mjs +82 -0
- package/bin/vidgrab.mjs +172 -0
- package/package.json +55 -4
- package/src/framesrv.js +348 -0
- package/src/human.js +302 -0
- package/src/index.js +161 -0
- package/src/inject.js +415 -0
- package/src/lease.js +314 -0
- package/src/occupancy.js +214 -0
- package/src/ocr.js +209 -0
- package/src/ocr_helper.py +125 -0
- package/src/pool.js +195 -0
- package/src/sampling.js +163 -0
- package/src/stream.js +571 -0
- package/src/target.js +29 -0
- package/src/timeline.js +324 -0
- package/src/tool-spec.js +873 -0
- package/src/tools.js +2034 -0
- package/src/vd.js +2354 -0
- package/src/video.js +751 -0
- package/src/vision.js +193 -0
- package/src/vision_helper.py +334 -0
package/src/human.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 拟人化输入:用 `input motionevent` 编排接近真人的触摸事件。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不用 `input tap`(2026-11-12 实测):
|
|
5
|
+
* tap 只产生 DOWN+UP 两个事件,没有 MOVE,压力恒定,时长固定。
|
|
6
|
+
* 风控靶场能直接判定「click-without-mousedown」「零轨迹」。
|
|
7
|
+
*
|
|
8
|
+
* 为什么用批量(同样实测):
|
|
9
|
+
* 单个事件分次调 adb,每次 55-199ms(adb 往返 + JVM 启动);
|
|
10
|
+
* 一条 adb shell 里用 `;` 串起来 **4 个事件只要 105ms,平均 26ms/个**。
|
|
11
|
+
* 26ms/事件 正好落进真人点击的采样节奏里,所以能编排出看起来自然的序列。
|
|
12
|
+
*
|
|
13
|
+
* 真人的特征(本模块据此建模):
|
|
14
|
+
* · 落点不会每次一样 → 截断高斯,σ = 目标尺寸的 15~25%
|
|
15
|
+
* · 按下时间不会一样 → 对数正态,中位 ~90ms
|
|
16
|
+
* · 手指是移动的不是瞬移的 → 最小加加速度轨迹 + 横向抖动 + 偶尔 overshoot
|
|
17
|
+
* · 压力先升后稳 → --axis PRESSURE 沿轨迹变化
|
|
18
|
+
* · 动作之间有"思考"时间 → 对数正态,中位 1.2~2.5s
|
|
19
|
+
*
|
|
20
|
+
* 做不到的(如实记录,不要吹):
|
|
21
|
+
* · `isTrusted` 永远是 false —— 这是内核层标记,只有真实手指或 uinput 设备才是 true
|
|
22
|
+
* · `sendevent` 写真实触摸屏被 SELinux 禁止(实测 Permission denied)
|
|
23
|
+
* · `/dev/uinput` 需要 root(实测写入失败)
|
|
24
|
+
* 所以「和真人无法区分」做不到,只能做到「行为分布像人」。
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** 高斯随机数(Box-Muller)。 */
|
|
28
|
+
function gauss(mean = 0, sd = 1) {
|
|
29
|
+
let u = 0; let v = 0;
|
|
30
|
+
while (u === 0) u = Math.random();
|
|
31
|
+
while (v === 0) v = Math.random();
|
|
32
|
+
return mean + sd * Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 对数正态:真人的反应时间/点击间隔是这种分布,不是均匀分布。 */
|
|
36
|
+
function lognorm(median, sigma = 0.5) {
|
|
37
|
+
return Math.exp(Math.log(median) + sigma * gauss(0, 1));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 截断到区间内。 */
|
|
41
|
+
function clamp(v, lo, hi) {
|
|
42
|
+
return Math.min(hi, Math.max(lo, v));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 最小加加速度(minimum jerk)轨迹 —— 人手运动的经典模型。
|
|
47
|
+
*
|
|
48
|
+
* 和纯贝塞尔的关键区别:它同时约束了位置和速度剖面,
|
|
49
|
+
* 起步慢、中段快、末端减速,而贝塞尔只保证曲线形状好看。
|
|
50
|
+
*
|
|
51
|
+
* @param {number} n 采样点数
|
|
52
|
+
* @returns {number[]} 每个采样点的归一化进度 0..1
|
|
53
|
+
*/
|
|
54
|
+
function minJerk(n) {
|
|
55
|
+
const out = [];
|
|
56
|
+
for (let i = 1; i <= n; i += 1) {
|
|
57
|
+
const t = i / n;
|
|
58
|
+
out.push(10 * t ** 3 - 15 * t ** 4 + 6 * t ** 5);
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 生成一次点击的完整事件序列(不含时序 sleep,时序由调用方插入)。
|
|
65
|
+
*
|
|
66
|
+
* @param {number} x 目标中心 x
|
|
67
|
+
* @param {number} y 目标中心 y
|
|
68
|
+
* @param {object} o
|
|
69
|
+
* @param {number} [o.size=100] 目标大致尺寸(像素),决定落点散布
|
|
70
|
+
* @param {number} [o.jitterPx] 手抖幅度,默认按 size 的 2%
|
|
71
|
+
* @param {number} [o.moves] MOVE 事件数量,默认随机 1~3
|
|
72
|
+
* @returns {{events: Array<{action: string, x: number, y: number, pressure: number}>, holdMs: number}}
|
|
73
|
+
*/
|
|
74
|
+
export function buildTap(x, y, o = {}) {
|
|
75
|
+
const size = o.size || 100;
|
|
76
|
+
// 落点散布:真人不会每次点正中,σ 取目标的 15~25%
|
|
77
|
+
const sigma = size * (0.15 + Math.random() * 0.1);
|
|
78
|
+
let cx = clamp(x + gauss(0, sigma / 2), x - size / 2, x + size / 2);
|
|
79
|
+
let cy = clamp(y + gauss(0, sigma / 2), y - size / 2, y + size / 2);
|
|
80
|
+
|
|
81
|
+
// 系统偏差:右手用户整体偏右下(有研究支持,差 2~5px 量级)
|
|
82
|
+
if (Math.random() < 0.7) {
|
|
83
|
+
cx += 1 + Math.random() * 3;
|
|
84
|
+
cy += 1 + Math.random() * 3;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const moves = o.moves ?? (1 + Math.floor(Math.random() * 3));
|
|
88
|
+
const jitterPx = o.jitterPx ?? Math.max(1, size * 0.02);
|
|
89
|
+
const events = [];
|
|
90
|
+
|
|
91
|
+
// DOWN —— 真人按下瞬间压力就起来了,但不是满值
|
|
92
|
+
let px = cx;
|
|
93
|
+
let py = cy;
|
|
94
|
+
events.push({ action: 'DOWN', x: Math.round(px), y: Math.round(py), pressure: 0.25 + Math.random() * 0.2 });
|
|
95
|
+
|
|
96
|
+
// 按下期间的微移:手指不是完全静止的
|
|
97
|
+
for (let i = 0; i < moves; i += 1) {
|
|
98
|
+
px = clamp(px + gauss(0, jitterPx), cx - size / 3, cx + size / 3);
|
|
99
|
+
py = clamp(py + gauss(0, jitterPx), cy - size / 3, cy + size / 3);
|
|
100
|
+
// 压力先升后稳
|
|
101
|
+
const p = 0.45 + (i / Math.max(1, moves)) * 0.3 + Math.random() * 0.1;
|
|
102
|
+
events.push({ action: 'MOVE', x: Math.round(px), y: Math.round(py), pressure: clamp(p, 0.3, 0.95) });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// UP —— 抬手压力回落
|
|
106
|
+
events.push({ action: 'UP', x: Math.round(px), y: Math.round(py), pressure: 0.1 + Math.random() * 0.15 });
|
|
107
|
+
|
|
108
|
+
// 按下总时长:真人 60~140ms,用对数正态
|
|
109
|
+
const holdMs = clamp(lognorm(95, 0.35), 55, 180);
|
|
110
|
+
return { events, holdMs };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 生成一次滑动的完整事件序列。
|
|
115
|
+
*
|
|
116
|
+
* @param {number} x1 起点 x
|
|
117
|
+
* @param {number} y1 起点 y
|
|
118
|
+
* @param {number} x2 终点 x
|
|
119
|
+
* @param {number} y2 终点 y
|
|
120
|
+
* @param {object} o
|
|
121
|
+
* @param {number} [o.points=40] 采样点数量
|
|
122
|
+
* @param {number} [o.overshoot=0.3] 产生 overshoot 的概率
|
|
123
|
+
* @returns {{events: Array, durationMs: number}}
|
|
124
|
+
*/
|
|
125
|
+
export function buildSwipe(x1, y1, x2, y2, o = {}) {
|
|
126
|
+
const points = o.points ?? (30 + Math.floor(Math.random() * 50));
|
|
127
|
+
const curve = minJerk(points);
|
|
128
|
+
|
|
129
|
+
// 轨迹整体加一个横向弯曲(真人不会走直线),弯曲方向随机。
|
|
130
|
+
//
|
|
131
|
+
// **幅度必须跟路径长度成比例**(2026-11-12 实测):原来写死 gauss(0, 12) 像素,
|
|
132
|
+
// 对 60px 的短移动够用,对 200px 的移动就几乎是一条直线 ——
|
|
133
|
+
// 靶场算出来的直线度会在 0.92~0.99 之间飘,一半的点击直接判失败。
|
|
134
|
+
// 改成路径长度的 12%~30%,直线度稳定落在 0.7~0.85。
|
|
135
|
+
const pathLen = Math.hypot(x2 - x1, y2 - y1) || 1;
|
|
136
|
+
const bendMag = pathLen * (0.20 + Math.random() * 0.16);
|
|
137
|
+
const bend = (Math.random() < 0.5 ? -1 : 1) * bendMag;
|
|
138
|
+
const dx = x2 - x1;
|
|
139
|
+
const dy = y2 - y1;
|
|
140
|
+
const len = Math.hypot(dx, dy) || 1;
|
|
141
|
+
const nx = -dy / len; // 法向量
|
|
142
|
+
const ny = dx / len;
|
|
143
|
+
|
|
144
|
+
const events = [];
|
|
145
|
+
let ox = 0;
|
|
146
|
+
let oy = 0;
|
|
147
|
+
|
|
148
|
+
// overshoot:真人经常冲过头一点再往回拉
|
|
149
|
+
const willOvershoot = Math.random() < (o.overshoot ?? 0.3);
|
|
150
|
+
if (willOvershoot) {
|
|
151
|
+
const over = 5 + Math.random() * 10;
|
|
152
|
+
ox = (dx / len) * over;
|
|
153
|
+
oy = (dy / len) * over;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
events.push({
|
|
157
|
+
action: 'DOWN',
|
|
158
|
+
x: Math.round(x1 + gauss(0, 3)),
|
|
159
|
+
y: Math.round(y1 + gauss(0, 3)),
|
|
160
|
+
pressure: 0.3 + Math.random() * 0.2,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
for (let i = 0; i < points; i += 1) {
|
|
164
|
+
const t = curve[i];
|
|
165
|
+
// 横向弯曲在中间最大
|
|
166
|
+
const bow = Math.sin(Math.PI * t) * bend;
|
|
167
|
+
const jx = gauss(0, 1.5);
|
|
168
|
+
const jy = gauss(0, 1.5);
|
|
169
|
+
const px = x1 + dx * t + nx * bow + ox * (t > 0.85 ? (1 - t) / 0.15 : 1) + jx;
|
|
170
|
+
const py = y1 + dy * t + ny * bow + oy * (t > 0.85 ? (1 - t) / 0.15 : 1) + jy;
|
|
171
|
+
events.push({
|
|
172
|
+
action: 'MOVE',
|
|
173
|
+
x: Math.round(px),
|
|
174
|
+
y: Math.round(py),
|
|
175
|
+
pressure: clamp(0.4 + Math.sin(Math.PI * t) * 0.35 + gauss(0, 0.05), 0.25, 0.95),
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
events.push({
|
|
180
|
+
action: 'UP',
|
|
181
|
+
x: Math.round(x2 + gauss(0, 2)),
|
|
182
|
+
y: Math.round(y2 + gauss(0, 2)),
|
|
183
|
+
pressure: 0.1 + Math.random() * 0.15,
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// 真人滑动时长与距离相关:大致 200~800ms
|
|
187
|
+
const durationMs = clamp(lognorm(320 + len * 0.35, 0.3), 180, 1200);
|
|
188
|
+
return { events, durationMs };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 把事件序列编成一条 adb shell 命令。
|
|
193
|
+
*
|
|
194
|
+
* 用 `;` 串在**同一次 adb shell 调用**里 —— 实测 4 个事件 105ms,
|
|
195
|
+
* 比逐个调 adb(每次 55~199ms)快一个数量级,而且这样事件间隔才可能像真人。
|
|
196
|
+
*/
|
|
197
|
+
export function toShellCommand(events, opts = {}) {
|
|
198
|
+
const displayArg = opts.displayId != null ? `-d ${Number(opts.displayId)} ` : '';
|
|
199
|
+
return events.map((e) => {
|
|
200
|
+
const parts = [`input ${displayArg}motionevent ${e.action} ${e.x} ${e.y}`];
|
|
201
|
+
if (e.pressure != null && e.pressure > 0) {
|
|
202
|
+
parts[0] += ` --axis PRESSURE,${e.pressure.toFixed(2)}`;
|
|
203
|
+
}
|
|
204
|
+
return parts[0];
|
|
205
|
+
}).join('; ');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** 一段随机"思考"时间(毫秒),真人操作前会有停顿。 */
|
|
209
|
+
export function thinkDelay(median = 1600) {
|
|
210
|
+
/*
|
|
211
|
+
* **下限必须跟着 median 走,不能写死 300。**
|
|
212
|
+
*
|
|
213
|
+
* 原来写的是 clamp(lognorm(median, 0.5), 300, 9000):
|
|
214
|
+
* 调用方传 thinkDelay(200) 时 median=200 < 下限 300,于是**所有采样都被压成恰好 300ms**,
|
|
215
|
+
* 实测约 73% 的「按下前停顿」变成同一个数字 —— 这本身就是一个新的机器尖峰,
|
|
216
|
+
* 比不加抖动还糟:真人的停顿是连续分布的,没有任何单点会占七成。
|
|
217
|
+
*
|
|
218
|
+
* 改成下限 = median × 0.3(跟对数正态的形态匹配),上限仍然封顶 9 秒防卡死。
|
|
219
|
+
*/
|
|
220
|
+
const lo = Math.max(20, Math.round(median * 0.3));
|
|
221
|
+
return Math.round(clamp(lognorm(median, 0.5), lo, 9000));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 鼠标移动之间的延迟 —— **双峰分布**,不是均匀也不是单一对数正态。
|
|
226
|
+
*
|
|
227
|
+
* 为什么要双峰(2026-11-12 实测):浏览器把 mousemove 对齐到渲染帧(60fps = 16.7ms),
|
|
228
|
+
* 所以实际测到的间隔全是 16.7 的整数倍:`[100, 83, 117, 66, 100, 67, ...]`。
|
|
229
|
+
* 如果延迟都挤在 55ms 附近,量化后只剩 4-7 帧可选,变异系数被压在 0.20 左右,
|
|
230
|
+
* 风控直接判「太规律像机器」(阈值 > 0.25)。
|
|
231
|
+
*
|
|
232
|
+
* 真人不会匀速移动:有一段快速逼近,也会在中途停顿一下再修正。
|
|
233
|
+
* 所以这里用双峰——大部分是快速移动,小部分是明显停顿:
|
|
234
|
+
* 65% 概率:20~45ms(连续移动)
|
|
235
|
+
* 35% 概率:120~320ms(停顿/犹豫)
|
|
236
|
+
*
|
|
237
|
+
* 实测效果:dtCv 从 0.20 提到 0.65~0.73,靶场该项从失败变通过。
|
|
238
|
+
*/
|
|
239
|
+
export function mouseDelay() {
|
|
240
|
+
return Math.random() < 0.65
|
|
241
|
+
? 20 + Math.random() * 25
|
|
242
|
+
: 120 + Math.random() * 200;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 打字节奏:按键之间的间隔。
|
|
247
|
+
*
|
|
248
|
+
* 真人打字的间隔不是均匀的 —— 有连续快速输入,也会停下来想下一个字。
|
|
249
|
+
* 和 mouseDelay 用同样的双峰思路:多数是相邻键的快速衔接,少数是思考停顿。
|
|
250
|
+
*/
|
|
251
|
+
export function keyFlightDelay() {
|
|
252
|
+
// 用对数正态,参数和严苛检测器里的"真人参考分布"对齐(中位 132ms,σ 0.55)。
|
|
253
|
+
// 为什么不能继续用双峰:严苛模式用 KS 检验比对分布形状,双峰的形状和
|
|
254
|
+
// 对数正态差得远,实测 KS 距离 0.73(阈值 0.7)直接判失败。
|
|
255
|
+
// 但真人确实会中途停顿,所以保留 12% 的长尾(这一部分本来就在对数正态的尾部里,
|
|
256
|
+
// 只是拉得更长一点,用来模拟"想一下下一个词")。
|
|
257
|
+
const z = Math.sqrt(-2 * Math.log(Math.max(1e-9, Math.random())))
|
|
258
|
+
* Math.cos(2 * Math.PI * Math.random());
|
|
259
|
+
let d = Math.exp(Math.log(132) + 0.55 * z);
|
|
260
|
+
if (Math.random() < 0.12) d *= 2.2 + Math.random() * 1.6; // 偶尔想一下
|
|
261
|
+
return d;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* 按住一个键的时长(dwell)。
|
|
266
|
+
*
|
|
267
|
+
* **必须双峰**,和 mouseDelay 同一个原因:按键事件被对齐到渲染帧(16.7ms),
|
|
268
|
+
* 如果 dwell 全挤在 55~140ms,量化后只剩五六个格子,变异系数被压在 0.19,
|
|
269
|
+
* 风控判「太一致像机器」。实测:单峰 0.190(失败),双峰 0.45+(通过)。
|
|
270
|
+
*
|
|
271
|
+
* 真人按住时长本来就不均匀:多数是轻点,偶尔停顿久一点才开始松手。
|
|
272
|
+
*/
|
|
273
|
+
export function keyDwellDelay() {
|
|
274
|
+
return Math.random() < 0.68
|
|
275
|
+
? 42 + Math.random() * 68 // 42~110ms:正常轻按
|
|
276
|
+
: 150 + Math.random() * 190; // 150~340ms:按久一点
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* 滚轮/触控板滑动的减速曲线。
|
|
281
|
+
*
|
|
282
|
+
* 真人滑动不会一次跳到底:手指/滚轮先快后慢,最后再补一两下小的。
|
|
283
|
+
* 返回一组归一化的增量比例(和为 1),按这个比例切分总距离。
|
|
284
|
+
*
|
|
285
|
+
* @param {number} steps 分段数
|
|
286
|
+
*/
|
|
287
|
+
export function scrollSteps(steps = 5) {
|
|
288
|
+
const n = Math.max(2, Math.min(12, steps + Math.floor(Math.random() * 3) - 1));
|
|
289
|
+
const w = [];
|
|
290
|
+
for (let i = 0; i < n; i += 1) {
|
|
291
|
+
// 前段权重大,尾段变小,模拟"冲出去—减速—微调"
|
|
292
|
+
w.push(Math.pow(1 - i / n, 1.8) + Math.random() * 0.35);
|
|
293
|
+
}
|
|
294
|
+
const sum = w.reduce((a, b) => a + b, 0);
|
|
295
|
+
return w.map((x) => x / sum);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** 滚动两步之间的间隔:早期连续,收尾时会停一下。 */
|
|
299
|
+
export function scrollDelay(idx, total) {
|
|
300
|
+
const tail = idx / Math.max(1, total - 1);
|
|
301
|
+
return (28 + Math.random() * 55) * (1 + tail * 1.6);
|
|
302
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* index.js — 插件入口,只做注册。
|
|
3
|
+
*
|
|
4
|
+
* 六个工具:screen_modes / vdisplay_start / vdisplay_launch / vdisplay_input /
|
|
5
|
+
* vdisplay_shot / screen_shot_main / vdisplay_status / vdisplay_stop
|
|
6
|
+
*
|
|
7
|
+
* defineTool 要五级回退:插件装在 profile 的 node_modules 下,那里没有 dsh-tools,
|
|
8
|
+
* 引擎自带的那份在 /opt/dsh/node_modules。只写裸包名会让 apply 直接失败。
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
inputSpec, launchSpec, mainShotSpec, modesSpec, riskCheckSpec, shotSpec, startSpec, statusSpec,
|
|
12
|
+
stopSpec, userStateSpec,
|
|
13
|
+
streamStartSpec, streamStopSpec, textSpec, appsSpec,
|
|
14
|
+
poolSpec, newScreenSpec, useScreenSpec, closeScreenSpec, checkAppSpec,
|
|
15
|
+
claimAppSpec, releaseAppSpec, leasesSpec,
|
|
16
|
+
// 感知层(2026-11-16):常驻解码、实时帧、等稳定、自动 ROI、黑屏探针
|
|
17
|
+
liveSpec, liveShotSpec, waitStableSpec, roiSpec, probeSecureSpec,
|
|
18
|
+
// 按文字找/点(2026-11-19):消掉模型自己算坐标的 点空率
|
|
19
|
+
findTextSpec, tapTextSpec, keepAliveSpec,
|
|
20
|
+
findImageSpec, tapImageSpec, fingerprintSpec, screenDiffSpec, probeScreenSpec,
|
|
21
|
+
findColorSpec, tapColorSpec, chooseMethodSpec,
|
|
22
|
+
waitForSpec,
|
|
23
|
+
} from './tool-spec.js';
|
|
24
|
+
import * as T from './tools.js';
|
|
25
|
+
|
|
26
|
+
export const name = 'dsh-go-plugin-screen';
|
|
27
|
+
export const inject = ['tools'];
|
|
28
|
+
|
|
29
|
+
async function loadDefineTool() {
|
|
30
|
+
const tryImport = async (spec) => {
|
|
31
|
+
const mod = await import(spec);
|
|
32
|
+
return mod?.defineTool ? mod.defineTool : null;
|
|
33
|
+
};
|
|
34
|
+
try { const d = await tryImport('@deepseek-ai/dsh-tools'); if (d) return d; } catch {}
|
|
35
|
+
const override = process.env.DSH_TOOLS_PATH;
|
|
36
|
+
if (override) {
|
|
37
|
+
try {
|
|
38
|
+
const { pathToFileURL } = await import('node:url');
|
|
39
|
+
const d = await tryImport(pathToFileURL(override).href);
|
|
40
|
+
if (d) return d;
|
|
41
|
+
} catch {}
|
|
42
|
+
}
|
|
43
|
+
const { existsSync } = await import('node:fs');
|
|
44
|
+
const { dirname, join } = await import('node:path');
|
|
45
|
+
const { pathToFileURL } = await import('node:url');
|
|
46
|
+
for (const root of [process.env.DSH_ROOT, '/opt/dsh', '/usr/local/lib/dsh', '/app'].filter(Boolean)) {
|
|
47
|
+
const entry = `${root}/node_modules/@deepseek-ai/dsh-tools/lib/index.js`;
|
|
48
|
+
if (!existsSync(entry)) continue;
|
|
49
|
+
try { const d = await tryImport(pathToFileURL(entry).href); if (d) return d; } catch {}
|
|
50
|
+
}
|
|
51
|
+
for (const seed of [process.argv?.[1], process.cwd()].filter(Boolean)) {
|
|
52
|
+
let dir = dirname(seed);
|
|
53
|
+
for (let i = 0; i < 8; i += 1) {
|
|
54
|
+
const entry = join(dir, 'node_modules/@deepseek-ai/dsh-tools/lib/index.js');
|
|
55
|
+
if (existsSync(entry)) {
|
|
56
|
+
try { const d = await tryImport(pathToFileURL(entry).href); if (d) return d; } catch {}
|
|
57
|
+
}
|
|
58
|
+
const up = dirname(dir);
|
|
59
|
+
if (up === dir) break;
|
|
60
|
+
dir = up;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
throw new Error('cannot resolve @deepseek-ai/dsh-tools');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function apply(ctx) {
|
|
67
|
+
// 启动时修一次假状态(见 stream.js 的 reconcileStreamState)
|
|
68
|
+
import('./stream.js').then(m => m.reconcileStreamState?.()).catch(() => {});
|
|
69
|
+
// **启动空闲看门狗**:AI 不调工具就自动放掉虚拟屏和推流(省电)。
|
|
70
|
+
// 实测不关的话 framesrv 持续 10.3% CPU —— 悬浮窗连着 MJPEG,
|
|
71
|
+
// framesrv 就以为「有人在看」,一直编码。
|
|
72
|
+
try { T.startIdleWatchdog(); } catch { /* ignore */ }
|
|
73
|
+
// Cordis 规定:没通过 ctx.inject 声明依赖之前,**碰 ctx 的任何属性都会抛**
|
|
74
|
+
// ("cannot get property X without inject")。所以所有 ctx 访问都放进 inject 回调里。
|
|
75
|
+
// 这个坑让三个插件在真实引擎里全部加载失败过,工具一个都没出现。
|
|
76
|
+
const disposers = [];
|
|
77
|
+
try {
|
|
78
|
+
ctx.inject(['tools'], (c) => {
|
|
79
|
+
loadDefineTool()
|
|
80
|
+
.then((defineTool) => {
|
|
81
|
+
const defs = [
|
|
82
|
+
[modesSpec, () => T.modes()],
|
|
83
|
+
[startSpec, (a) => T.start(a)],
|
|
84
|
+
[launchSpec, (a) => T.launch(a)],
|
|
85
|
+
[inputSpec, (a) => T.input(a)],
|
|
86
|
+
[shotSpec, (a) => T.shot(a)],
|
|
87
|
+
[textSpec, (a) => T.text(a)],
|
|
88
|
+
[appsSpec, () => T.apps()],
|
|
89
|
+
[poolSpec, () => T.poolInfo()],
|
|
90
|
+
[newScreenSpec, (a) => T.newScreen(a)],
|
|
91
|
+
[useScreenSpec, (a) => T.useScreen(a)],
|
|
92
|
+
[closeScreenSpec, (a) => T.closeScreen(a)],
|
|
93
|
+
[checkAppSpec, (a) => T.checkApp(a)],
|
|
94
|
+
[claimAppSpec, (a) => T.claimApp(a)],
|
|
95
|
+
[releaseAppSpec, (a) => T.releaseApp(a)],
|
|
96
|
+
[leasesSpec, () => T.leases()],
|
|
97
|
+
[mainShotSpec, (a) => T.mainShot(a)],
|
|
98
|
+
[statusSpec, () => T.status()],
|
|
99
|
+
[stopSpec, (a) => T.stop(a)], // 以前写成 () => T.stop(),把 args 丢了,forceStopApps 永远无效
|
|
100
|
+
[streamStartSpec, (a) => T.streamStart(a)],
|
|
101
|
+
[streamStopSpec, () => T.streamStop()],
|
|
102
|
+
// 感知层(2026-11-16):常驻解码、实时帧、等稳定、自动 ROI、黑屏探针
|
|
103
|
+
[liveSpec, (a) => T.live(a)],
|
|
104
|
+
[liveShotSpec, (a) => T.liveShot(a)],
|
|
105
|
+
[waitStableSpec, (a) => T.waitStable(a)],
|
|
106
|
+
[roiSpec, (a) => T.roi(a)],
|
|
107
|
+
[probeSecureSpec, (a) => T.probeSecure(a)],
|
|
108
|
+
// 安全闸门(2026-11-19 对抗审查发现):这两个能力一直缺
|
|
109
|
+
[userStateSpec, () => T.userState()],
|
|
110
|
+
[riskCheckSpec, (a) => T.riskCheck(a?.pkg)],
|
|
111
|
+
// 按文字找/点(2026-11-19):消掉模型自己算坐标的 点空率
|
|
112
|
+
[findTextSpec, (a) => T.findText(a)],
|
|
113
|
+
[tapTextSpec, (a) => T.tapText(a)],
|
|
114
|
+
// 后台屏生命周期(2026-11-19):让 AI 按任务预估空闲超时
|
|
115
|
+
[keepAliveSpec, (a) => T.keepAlive(a)],
|
|
116
|
+
// 视觉方法(2026-11-19):模板/指纹/差分/探测,全部基于常驻 OpenCV
|
|
117
|
+
[findImageSpec, (a) => T.findImage(a)],
|
|
118
|
+
[tapImageSpec, (a) => T.tapImage(a)],
|
|
119
|
+
[fingerprintSpec, (a) => T.fingerprint(a)],
|
|
120
|
+
[screenDiffSpec, (a) => T.screenDiff(a)],
|
|
121
|
+
[probeScreenSpec, (a) => T.probeScreen(a)],
|
|
122
|
+
// 颜色检测 + 方法自动选择(2026-11-19)
|
|
123
|
+
[findColorSpec, (a) => T.findColor(a)],
|
|
124
|
+
[tapColorSpec, (a) => T.tapColor(a)],
|
|
125
|
+
[chooseMethodSpec, (a) => T.chooseMethod(a)],
|
|
126
|
+
// 条件等待(2026-11-19):替代固定 sleep,每步省 1-2 秒
|
|
127
|
+
[waitForSpec, (a) => T.waitFor(a)],
|
|
128
|
+
];
|
|
129
|
+
for (const [spec, exec] of defs) {
|
|
130
|
+
try {
|
|
131
|
+
// **每次工具调用都刷新空闲计时**,看门狗据此判断「AI 还在干活」。
|
|
132
|
+
const wrapped = async (a) => {
|
|
133
|
+
try { T.touchActivity(); } catch { /* ignore */ }
|
|
134
|
+
return exec(a);
|
|
135
|
+
};
|
|
136
|
+
disposers.push(c.tools.register(defineTool({ ...spec(), execute: wrapped })));
|
|
137
|
+
} catch (e) {
|
|
138
|
+
console.error('[dsh-go-plugin-screen] register failed:', e?.message || e);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
.catch((e) => console.error('[dsh-go-plugin-screen] apply failed, plugin disabled:', e?.message || e));
|
|
143
|
+
});
|
|
144
|
+
} catch (e) {
|
|
145
|
+
console.error('[dsh-go-plugin-screen] inject failed:', e?.message || e);
|
|
146
|
+
}
|
|
147
|
+
return async () => {
|
|
148
|
+
for (const d of disposers) { try { d?.(); } catch { /* 单个工具 dispose 失败不影响其他 */ } }
|
|
149
|
+
// **卸载时把常驻子进程关掉。** 不关的话 OCR 的 python3 会变成孤儿,
|
|
150
|
+
// 带着 15MB 模型一直占内存,重载一次多一个(对抗审查第三轮 S3)。
|
|
151
|
+
try { const ocr = await import('./ocr.js'); ocr.shutdown(); } catch { /* 没加载过就算了 */ }
|
|
152
|
+
// **视觉助手同样要关。** OpenCV 常驻进程占 80-150MB,不关就是孤儿
|
|
153
|
+
// (和 OCR 同款泄漏,视觉功能是后加的,当时漏了 —— 对抗审查 S4)。
|
|
154
|
+
try { const vis = await import('./vision.js'); vis.shutdown(); } catch { /* 没加载过就算了 */ }
|
|
155
|
+
try {
|
|
156
|
+
const vd = await import('./vd.js');
|
|
157
|
+
const st = await vd.status().catch(() => null);
|
|
158
|
+
if (st?.running) await vd.stop({ forceStopApps: false }).catch(() => {});
|
|
159
|
+
} catch { /* 清理失败不阻塞卸载 */ }
|
|
160
|
+
};
|
|
161
|
+
}
|