dsh-mobile-plugin 0.5.34 → 0.5.35
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/lib/impl.js +0 -4
- package/lib/mobile/approval-facts.js +0 -13
- package/lib/mobile/permissions.js +5 -19
- package/lib/mobile/session-send.js +0 -1
- package/lib/routes/mobile-api.js +1 -39
- package/package.json +1 -2
- package/assets/pets/README.md +0 -9
- package/assets/pets/deepy-whale/attention.gif +0 -0
- package/assets/pets/deepy-whale/done.gif +0 -0
- package/assets/pets/deepy-whale/error.gif +0 -0
- package/assets/pets/deepy-whale/idle.gif +0 -0
- package/assets/pets/deepy-whale/pack.json +0 -54
- package/assets/pets/deepy-whale/sleeping.gif +0 -0
- package/assets/pets/deepy-whale/working-3.gif +0 -0
- package/assets/pets/deepy-whale/working.gif +0 -0
- package/knowledge/apps/alipay.md +0 -30
- package/knowledge/apps/android-settings.md +0 -30
- package/knowledge/apps/baijiahao.md +0 -30
- package/knowledge/apps/bilibili.md +0 -31
- package/knowledge/apps/boss-zhipin.md +0 -32
- package/knowledge/apps/dedao.md +0 -33
- package/knowledge/apps/douyin.md +0 -32
- package/knowledge/apps/feishu.md +0 -35
- package/knowledge/apps/gmail.md +0 -33
- package/knowledge/apps/jike.md +0 -31
- package/knowledge/apps/kuaishou.md +0 -34
- package/knowledge/apps/linkedin.md +0 -33
- package/knowledge/apps/qqmail.md +0 -30
- package/knowledge/apps/taobao.md +0 -33
- package/knowledge/apps/telegram.md +0 -27
- package/knowledge/apps/toutiao.md +0 -32
- package/knowledge/apps/uber-eats.md +0 -26
- package/knowledge/apps/uber.md +0 -27
- package/knowledge/apps/wechat.md +0 -33
- package/knowledge/apps/weibo.md +0 -31
- package/knowledge/apps/whatsapp.md +0 -27
- package/knowledge/apps/x.md +0 -32
- package/knowledge/apps/xiaohongshu.md +0 -30
- package/knowledge/apps/zhihu.md +0 -31
- package/knowledge/sites/bilibili.com/SITE.md +0 -50
- package/knowledge/sites/bilibili.com/references/view-fields.md +0 -17
- package/knowledge/sites/github.com/SITE.md +0 -51
- package/knowledge/sites/github.com/references/repo-fields.md +0 -13
- package/knowledge/sites/x.com/SITE.md +0 -47
- package/knowledge/sites/xiaohongshu.com/SITE.md +0 -48
- package/knowledge/sites/xiaohongshu.com/references/note-fields.md +0 -18
- package/knowledge/sites/youtube.com/SITE.md +0 -49
- package/knowledge/sites/youtube.com/references/innertube-fields.md +0 -14
- package/knowledge/sites/zhihu.com/SITE.md +0 -47
- package/knowledge/sites/zhihu.com/references/api-fields.md +0 -21
- package/lib/mobile/pet-tools.js +0 -261
- package/lib/mobile/pets.js +0 -755
- package/lib/mobile/phone/access.js +0 -140
- package/lib/mobile/phone/binding.js +0 -247
- package/lib/mobile/phone/confirm.js +0 -190
- package/lib/mobile/phone/evidence.js +0 -228
- package/lib/mobile/phone/gate.js +0 -290
- package/lib/mobile/phone/grounding.js +0 -314
- package/lib/mobile/phone/guard.js +0 -465
- package/lib/mobile/phone/guarded.js +0 -86
- package/lib/mobile/phone/index.js +0 -114
- package/lib/mobile/phone/irreversible.js +0 -226
- package/lib/mobile/phone/knowledge-tools.js +0 -665
- package/lib/mobile/phone/learn.js +0 -329
- package/lib/mobile/phone/pacing.js +0 -173
- package/lib/mobile/phone/redact.js +0 -237
- package/lib/mobile/phone/risk.js +0 -95
- package/lib/mobile/phone/route-extract.js +0 -460
- package/lib/mobile/phone/route-hint.js +0 -164
- package/lib/mobile/phone/routes-memory.js +0 -133
- package/lib/mobile/phone/routes.js +0 -287
- package/lib/mobile/phone/service.js +0 -520
- package/lib/mobile/phone/site-commands.js +0 -271
- package/lib/mobile/phone/skill-recall.js +0 -246
- package/lib/mobile/phone/skill-routes.js +0 -106
- package/lib/mobile/phone/step-summary.js +0 -201
- package/lib/mobile/phone/stop-summary.js +0 -235
- package/lib/mobile/phone/tool-groups.js +0 -125
- package/lib/mobile/phone/tools.js +0 -2590
- package/lib/mobile/phone/untrusted.js +0 -65
- package/lib/mobile/phone-knowledge.js +0 -317
- package/lib/mobile/route-store.js +0 -266
|
@@ -1,460 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 把一次真跑成功的 `phone_steps` 轨迹,抽成一份**结构化路线**(也就是「教一次就学会」里
|
|
5
|
-
* 「学」的那一半)。纯函数:不连手机、不发动作、不碰磁盘,进得去出得来。
|
|
6
|
-
*
|
|
7
|
-
* ## 为什么另起一份,不复用 `routes-memory`
|
|
8
|
-
*
|
|
9
|
-
* `routes-memory` 存的是「这个 App 上次是怎么做的」的**摘要**,而且文件头写明了刻意不给可执行
|
|
10
|
-
* 步骤 —— 界面会变,照抄旧路线比没有更危险。那一条对「人读的参考」仍然成立,所以那份不动。
|
|
11
|
-
* 这里要做的是另一件事:让**机器**能照着重放,所以必须留下每个目标的可重认身份(文字、role、
|
|
12
|
-
* 资源 id、视口相对位置),而不是把步骤压成一句人话。
|
|
13
|
-
*
|
|
14
|
-
* 两者互不替代:`route-hint` 继续负责「大概怎么做过」,这份负责「精确怎么再走一遍」。
|
|
15
|
-
*
|
|
16
|
-
* ## 存什么、不存什么(这条是隐私面,不是风格)
|
|
17
|
-
*
|
|
18
|
-
* - **不存 ref**:`e7` 只对当时那一屏有效,换一屏就失效;存下来等于存了一把随时对不上号的钥匙。
|
|
19
|
-
* - **不存绝对像素**:只存视口百分比中心,换台尺寸不同的手机仍然指同一个控件。
|
|
20
|
-
* - **不存业务值**:用户输入的正文、被点的人名/单号一律换成 `<参数>` 占位符,值本身不落盘。
|
|
21
|
-
* - **界面锚保留字面量**:「发送」「搜索」「+」这类按钮文字是定位骨架,换成占位符这条路线就废了。
|
|
22
|
-
*
|
|
23
|
-
* 换句话说:**会变的那部分不存,不变的那部分才存**。
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
const redactor = require('./redact.js');
|
|
27
|
-
const irreversible = require('./irreversible.js');
|
|
28
|
-
|
|
29
|
-
/** 路线格式版本。字段含义变才抬,加可选字段不抬。 */
|
|
30
|
-
const ROUTE_VERSION = 1;
|
|
31
|
-
|
|
32
|
-
/** 一条路线最多几步。与 `phone_steps` 单批上限一致:再多就不是「一次教」了。 */
|
|
33
|
-
const MAX_STEPS = 20;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 能重放的动作,是 `service.js` 的 ACTIONS 的一个子集。
|
|
37
|
-
*
|
|
38
|
-
* 刻意只有能「再走一遍」的那些:`batch` 不进来(那是容器)、`screen.screenshot` / `extract` /
|
|
39
|
-
* `collect` 这类只读动作也不进来 —— 重放靠的是现读屏,把当时的观察固化成步骤没有意义。
|
|
40
|
-
*/
|
|
41
|
-
const REPLAYABLE_ACTIONS = new Set([
|
|
42
|
-
'tap', 'type', 'swipe', 'key', 'app.open', 'app.close', 'wait', 'gesture',
|
|
43
|
-
]);
|
|
44
|
-
|
|
45
|
-
/** 动作名 → 给人看的一句话动词。 */
|
|
46
|
-
const VERBS = {
|
|
47
|
-
tap: '点击',
|
|
48
|
-
type: '输入',
|
|
49
|
-
swipe: '滑动',
|
|
50
|
-
key: '按键',
|
|
51
|
-
'app.open': '打开',
|
|
52
|
-
'app.close': '关闭',
|
|
53
|
-
wait: '等待',
|
|
54
|
-
gesture: '手势',
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 要脱敏的字面量:**只有用户亲手输入的正文**(`args.text`)。
|
|
59
|
-
*
|
|
60
|
-
* 刻意不含 `args.target`:那是「这一步在点什么」,绝大多数是界面上的按钮文字(发送、搜索、
|
|
61
|
-
* 微信),换掉就等于把定位锚毁了。人名这类可能被点到的内容确实有隐私担忧,但它本来就活在
|
|
62
|
-
* 界面上、也已经被 `routes-memory` 原样记着;把整张元素表都当 PII 处理会让路线一条都学不成,
|
|
63
|
-
* 那不是隐私保护,那是功能报废。真正该挡的凭据类由统一脱敏器的模式负责。
|
|
64
|
-
*/
|
|
65
|
-
function sensitiveArgOf(step) {
|
|
66
|
-
if (!step || typeof step !== 'object') return [];
|
|
67
|
-
const args = step.args && typeof step.args === 'object' ? step.args : {};
|
|
68
|
-
return typeof args.text === 'string' && args.text.trim() ? [args.text] : [];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** 元素上的文字:与 `tools.js` 的 `elementText` 同一套字段优先级。 */
|
|
72
|
-
function elementText(el) {
|
|
73
|
-
if (!el || typeof el !== 'object') return '';
|
|
74
|
-
for (const key of ['text', 'name', 'label', 'description', 'contentDescription', 'hint']) {
|
|
75
|
-
const value = el[key];
|
|
76
|
-
if (typeof value === 'string' && value.trim()) return value;
|
|
77
|
-
}
|
|
78
|
-
return '';
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** 元素的视口中心(百分比)。读不出来返回 null —— 没有位置就不硬编一个。 */
|
|
82
|
-
function centerOf(el) {
|
|
83
|
-
const bounds = el && Array.isArray(el.bounds) ? el.bounds : null;
|
|
84
|
-
if (!bounds || bounds.length < 4) return null;
|
|
85
|
-
const nums = bounds.slice(0, 4).map((n) => Number(n));
|
|
86
|
-
if (!nums.every((n) => Number.isFinite(n))) return null;
|
|
87
|
-
return { x: (nums[0] + nums[2]) / 2, y: (nums[1] + nums[3]) / 2 };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** 按 ref 在这一屏里找元素;找不到返回 null(说明这一屏已经换过了)。 */
|
|
91
|
-
function elementByRef(elements, ref) {
|
|
92
|
-
const wanted = typeof ref === 'string' ? ref.trim() : '';
|
|
93
|
-
if (!wanted) return null;
|
|
94
|
-
const list = Array.isArray(elements) ? elements : [];
|
|
95
|
-
return list.find((el) => el && String(el.ref) === wanted) || null;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** 把文字压成一行、去掉多余空白。 */
|
|
99
|
-
function tidy(text) {
|
|
100
|
-
return String(text == null ? '' : text).replace(/\s+/g, ' ').trim();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 从当屏元素表里给这一步取一个「重放时认得回来」的定位骨架。
|
|
105
|
-
*
|
|
106
|
-
* 优先取元素自己的文字;元素没有文字(只有 ref 的图标按钮)时退到这一屏的 app + 相对位置。
|
|
107
|
-
* 三条特征按「越稳越靠前」排:资源 id(改文案不改控件)→ role → 文字 → 相对位置。
|
|
108
|
-
*/
|
|
109
|
-
function locatorOf(step, elements) {
|
|
110
|
-
const args = (step && step.args) || {};
|
|
111
|
-
const ref = typeof args.ref === 'string' ? args.ref.trim() : '';
|
|
112
|
-
const el = ref ? elementByRef(elements, ref) : null;
|
|
113
|
-
const loc = {};
|
|
114
|
-
if (el) {
|
|
115
|
-
const text = tidy(elementText(el));
|
|
116
|
-
if (text) loc.text = text.slice(0, 120);
|
|
117
|
-
if (typeof el.role === 'string' && el.role.trim()) loc.role = el.role.trim().slice(0, 60);
|
|
118
|
-
const rid = el.resourceId || el.id;
|
|
119
|
-
if (typeof rid === 'string' && rid.trim()) loc.resourceId = rid.trim().slice(0, 160);
|
|
120
|
-
const center = centerOf(el);
|
|
121
|
-
if (center) loc.relPos = { x: round1(center.x), y: round1(center.y) };
|
|
122
|
-
}
|
|
123
|
-
// 按坐标点的那一下:没有元素可认,只能留下当时那个位置。它仍然比什么都不留有用 ——
|
|
124
|
-
// 重放时按位置找可点元素,找不到就如实停,不瞎点。
|
|
125
|
-
if (!el && Number.isFinite(args.x) && Number.isFinite(args.y)) {
|
|
126
|
-
loc.relPos = { x: round1(args.x), y: round1(args.y) };
|
|
127
|
-
}
|
|
128
|
-
// 兜底锚:模型调 tap 时给的 `target`(「这一步在点什么」)。手机侧批量默认只回最后那一屏,
|
|
129
|
-
// 拿不到每步的元素表,所以这条文字往往是唯一能认回来的东西 —— 有它就用,没有才判不可重放。
|
|
130
|
-
if (!loc.text) {
|
|
131
|
-
const target = tidy(args.target);
|
|
132
|
-
// 被脱敏成 `<参数>` 的目标文字认不回任何控件 —— 那种等于没有锚,不能拿它当定位依据。
|
|
133
|
-
if (target && target !== '<参数>') loc.text = target.slice(0, 120);
|
|
134
|
-
}
|
|
135
|
-
return Object.keys(loc).length > 0 ? loc : null;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function round1(n) {
|
|
139
|
-
return Math.round(Number(n) * 10) / 10;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/** 一条路线去重用的键:同一 App + 同一意图 + 同样的动作形状,算同一条。 */
|
|
143
|
-
function routeKey(route) {
|
|
144
|
-
const pkg = route && route.app ? String(route.app.package || '') : '';
|
|
145
|
-
const shape = route && route.intent && route.intent.shape ? route.intent.shape : {};
|
|
146
|
-
const first = shape.firstAction || '';
|
|
147
|
-
const count = Number(shape.stepCount) || 0;
|
|
148
|
-
const words = Array.isArray(route && route.intent && route.intent.tokens)
|
|
149
|
-
? route.intent.tokens.slice().sort().join(',')
|
|
150
|
-
: '';
|
|
151
|
-
return `${pkg}|${first}|${count}|${words}`;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** 意图分词:中文按二字滑窗,英文/数字按整词。停用词去掉,重复的去重。 */
|
|
155
|
-
const STOP_WORDS = new Set([
|
|
156
|
-
'帮我', '给我', '一下', '这个', '那个', '然后', '就是', '可以', '能不', '不能',
|
|
157
|
-
'the', 'and', 'for', 'with', 'please', 'you', 'can', 'would', 'this', 'that',
|
|
158
|
-
]);
|
|
159
|
-
|
|
160
|
-
function tokenize(text) {
|
|
161
|
-
const source = tidy(text).toLowerCase();
|
|
162
|
-
if (!source) return [];
|
|
163
|
-
const out = new Set();
|
|
164
|
-
for (const word of source.match(/[a-z0-9][a-z0-9_]+/g) || []) out.add(word);
|
|
165
|
-
const cjk = source.replace(/[^\u4e00-\u9fff]/g, '');
|
|
166
|
-
for (let i = 0; i + 2 <= cjk.length; i += 1) out.add(cjk.slice(i, i + 2));
|
|
167
|
-
return [...out].filter((t) => t.length >= 2 && !STOP_WORDS.has(t)).slice(0, 40);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* 抽一条路线。
|
|
172
|
-
*
|
|
173
|
-
* @param {object} input
|
|
174
|
-
* @param {string} input.packageName 这条轨迹发生在哪个 App(首屏或批量结果的包名)
|
|
175
|
-
* @param {Array} input.steps 真跑过的步骤,每步 `{action, args, until?}`
|
|
176
|
-
* @param {Array<Array>} [input.screens] 与 steps 对齐的「这一步动手之前那一屏」的元素表;
|
|
177
|
-
* 缺一格就用上一格,缺全部则这一步没有定位骨架(会被判不可重放)
|
|
178
|
-
* @param {string} [input.intentText] 触发这次操作用户说的话
|
|
179
|
-
* @param {string} [input.title] 首屏/页面标题
|
|
180
|
-
* @param {Array<{action,ok}>} [input.results] 每步的执行结果,用来判「是不是真的都做成了」
|
|
181
|
-
* @returns {{ok:true, route:object} | {ok:false, problem:string}}
|
|
182
|
-
*/
|
|
183
|
-
function extractRoute(input = {}) {
|
|
184
|
-
const packageName = tidy(input.packageName);
|
|
185
|
-
if (!packageName) return { ok: false, problem: 'no_package' };
|
|
186
|
-
|
|
187
|
-
const steps = Array.isArray(input.steps) ? input.steps : [];
|
|
188
|
-
if (steps.length === 0) return { ok: false, problem: 'no_steps' };
|
|
189
|
-
if (steps.length > MAX_STEPS) return { ok: false, problem: 'too_many_steps' };
|
|
190
|
-
|
|
191
|
-
// 每一步必须真的成功。有一条失败或没执行,这条轨迹就不是「做成的那一次」。
|
|
192
|
-
const results = Array.isArray(input.results) ? input.results : [];
|
|
193
|
-
if (results.length > 0) {
|
|
194
|
-
for (const row of results) {
|
|
195
|
-
if (!row || row.ok !== true) return { ok: false, problem: 'not_all_steps_ok' };
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// 动作必须在可重放子集里。批容器、只读动作、系统写动作一律拒绝:它们不是「再走一遍」的一部分。
|
|
200
|
-
for (const step of steps) {
|
|
201
|
-
const action = step && typeof step.action === 'string' ? step.action.trim() : '';
|
|
202
|
-
if (!REPLAYABLE_ACTIONS.has(action)) return { ok: false, problem: `unreplayable_action:${action || '(空)'}` };
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// 收集本次要脱敏的字面量。注意只用于「用户内容」字段,界面锚不脱敏。
|
|
206
|
-
const literals = [];
|
|
207
|
-
for (const step of steps) literals.push(...sensitiveArgOf(step));
|
|
208
|
-
|
|
209
|
-
const screens = Array.isArray(input.screens) ? input.screens : [];
|
|
210
|
-
const outSteps = [];
|
|
211
|
-
const params = [];
|
|
212
|
-
let lastScreen = null;
|
|
213
|
-
|
|
214
|
-
for (let i = 0; i < steps.length; i += 1) {
|
|
215
|
-
const step = steps[i];
|
|
216
|
-
const action = step.action.trim();
|
|
217
|
-
const args = step.args && typeof step.args === 'object' ? step.args : {};
|
|
218
|
-
const screen = Array.isArray(screens[i]) ? screens[i] : lastScreen;
|
|
219
|
-
if (Array.isArray(screens[i])) lastScreen = screens[i];
|
|
220
|
-
|
|
221
|
-
const out = { i, action };
|
|
222
|
-
const outArgs = {};
|
|
223
|
-
|
|
224
|
-
if (action === 'tap') {
|
|
225
|
-
const target = tidy(args.target);
|
|
226
|
-
// 目标文字作为「这一步在点哪儿」的人读说明;真正的定位靠 locator。
|
|
227
|
-
if (target) outArgs.target = redactText(target, literals, params, 'target');
|
|
228
|
-
} else if (action === 'type') {
|
|
229
|
-
const text = typeof args.text === 'string' ? args.text : '';
|
|
230
|
-
outArgs.text = redactText(text, literals, params, 'text');
|
|
231
|
-
if (args.submit === true) outArgs.submit = true;
|
|
232
|
-
} else if (action === 'swipe') {
|
|
233
|
-
if (typeof args.direction === 'string') outArgs.direction = args.direction;
|
|
234
|
-
if (Number.isFinite(args.amount)) outArgs.amount = args.amount;
|
|
235
|
-
} else if (action === 'key') {
|
|
236
|
-
if (typeof args.key === 'string') outArgs.key = args.key;
|
|
237
|
-
} else if (action === 'app.open' || action === 'app.close') {
|
|
238
|
-
outArgs.packageName = tidy(args.packageName) || packageName;
|
|
239
|
-
} else if (action === 'wait') {
|
|
240
|
-
if (Number.isFinite(args.ms)) outArgs.ms = args.ms;
|
|
241
|
-
} else if (action === 'gesture') {
|
|
242
|
-
if (Array.isArray(args.strokes)) outArgs.strokes = args.strokes;
|
|
243
|
-
}
|
|
244
|
-
out.args = outArgs;
|
|
245
|
-
|
|
246
|
-
const locator = locatorOf(step, screen);
|
|
247
|
-
// 定位锚上如果正好是用户自己的内容(被点的是人名、单号……),把那一截换成占位符而不是删掉:
|
|
248
|
-
// 删掉这一步就再也认不回来,而「给<收件人>发消息」这种正是最该学的那一类。换成占位符之后
|
|
249
|
-
// 它仍是一个可用锚,只是重放时要由调用方把值补进来。
|
|
250
|
-
if (locator && typeof locator.text === 'string' && locator.text) {
|
|
251
|
-
locator.text = redactText(locator.text, literals, params, 'anchor');
|
|
252
|
-
}
|
|
253
|
-
if (locator && Object.keys(locator).length > 0) out.locator = locator;
|
|
254
|
-
if (step.until && typeof step.until === 'object') {
|
|
255
|
-
const until = {};
|
|
256
|
-
for (const key of ['appears', 'gone', 'app']) {
|
|
257
|
-
if (typeof step.until[key] === 'string' && step.until[key].trim()) {
|
|
258
|
-
until[key] = redactText(tidy(step.until[key]), literals, params, key);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
if (step.until.stable === true) until.stable = true;
|
|
262
|
-
if (Number.isFinite(step.until.timeoutMs)) until.timeoutMs = step.until.timeoutMs;
|
|
263
|
-
if (Object.keys(until).length > 0) out.until = until;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// 这一步要不要在重放时**当场**再问一次:付款/下单那类永远要;存档里绝不记「已确认」。
|
|
267
|
-
const judged = judgeStep(step);
|
|
268
|
-
if (judged && judged.kind === 'payment') out.mustConfirmLive = true;
|
|
269
|
-
|
|
270
|
-
// 「认不回目标」只对**必须指名道姓那个控件**的动作有意义。`tap` 永远要;`type` 只有在
|
|
271
|
-
// 指定了 ref(往某个具体的框里写)时才要 —— 不指定 ref 的 `type` 是「往当前焦点框里打」,
|
|
272
|
-
// 它本来就跟着前一步的焦点走,没有锚是对的。`app.open` / `key` / `swipe` / `gesture` /
|
|
273
|
-
// `wait` 这类动的是「动作本身」,不需要元素锚。
|
|
274
|
-
const needsLocator = out.action === 'tap' || (out.action === 'type' && typeof args.ref === 'string' && args.ref.trim());
|
|
275
|
-
if (needsLocator && (!out.locator || (!out.locator.text && !out.locator.role && !out.locator.resourceId))) {
|
|
276
|
-
out.positionOnly = true;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
outSteps.push(out);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
const intentText = tidy(input.intentText);
|
|
283
|
-
const title = tidy(input.title);
|
|
284
|
-
const actions = outSteps.map((s) => s.action);
|
|
285
|
-
const route = {
|
|
286
|
-
version: ROUTE_VERSION,
|
|
287
|
-
app: { package: packageName },
|
|
288
|
-
intent: {
|
|
289
|
-
raw: redactText(intentText, literals, params, 'intent'),
|
|
290
|
-
tokens: tokenize(intentText),
|
|
291
|
-
shape: {
|
|
292
|
-
firstAction: actions[0] || '',
|
|
293
|
-
stepCount: actions.length,
|
|
294
|
-
actionBigrams: bigrams(actions),
|
|
295
|
-
},
|
|
296
|
-
},
|
|
297
|
-
outcome: 'done',
|
|
298
|
-
learnedAt: new Date().toISOString(),
|
|
299
|
-
steps: outSteps,
|
|
300
|
-
};
|
|
301
|
-
if (title) route.intent.title = title.slice(0, 200);
|
|
302
|
-
if (params.length > 0) route.params = dedupeParams(params);
|
|
303
|
-
|
|
304
|
-
// 有一条只能靠位置认的步,就整条判「仅供人看」:与其存一条注定在第二台手机上点错的消息,
|
|
305
|
-
// 不如不存。这与 `route-hint` 的取向一致,只是门槛落在「能不能重认目标」上。
|
|
306
|
-
if (outSteps.some((s) => s.positionOnly)) return { ok: false, problem: 'position_only' };
|
|
307
|
-
|
|
308
|
-
const problem = validateRoute(route);
|
|
309
|
-
if (problem) return { ok: false, problem };
|
|
310
|
-
return { ok: true, route };
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/** 动作二元组:用来在「说法不同、做法一样」时兜底认亲。 */
|
|
314
|
-
function bigrams(actions) {
|
|
315
|
-
const out = [];
|
|
316
|
-
for (let i = 0; i + 1 < actions.length; i += 1) out.push([actions[i], actions[i + 1]]);
|
|
317
|
-
return out.slice(0, 12);
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/** 判这一步重放时要不要现问。判定沿用不可逆模块,抽的时候只作参考标记。 */
|
|
321
|
-
function judgeStep(step) {
|
|
322
|
-
try {
|
|
323
|
-
const args = (step && step.args) || {};
|
|
324
|
-
const screenText = typeof args.target === 'string' ? args.target : '';
|
|
325
|
-
return irreversible.judge({ action: step.action, args, screenText });
|
|
326
|
-
} catch {
|
|
327
|
-
return null;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* 把一个用户内容字段脱敏:命中就换 `<参数>`,并登记一个占位符。
|
|
333
|
-
*
|
|
334
|
-
* 只替换**本次真正出现过**的字面量与凭据类模式,不做「见数字就换」那种粗暴处理 ——
|
|
335
|
-
* 界面锚(按钮上的文字)必须原样留下,否则重放认不到目标。
|
|
336
|
-
*/
|
|
337
|
-
function redactText(text, literals, params, slot) {
|
|
338
|
-
const source = String(text == null ? '' : text);
|
|
339
|
-
if (!source.trim()) return source;
|
|
340
|
-
let out = source;
|
|
341
|
-
let changed = false;
|
|
342
|
-
// 先按本次输入过的原文精确替换(长的先换,避免短的把长的切碎)。
|
|
343
|
-
const sorted = [...new Set(literals.map((s) => tidy(s)).filter((s) => s.length >= 2))]
|
|
344
|
-
.sort((a, b) => b.length - a.length);
|
|
345
|
-
for (const lit of sorted) {
|
|
346
|
-
if (!out.includes(lit)) continue;
|
|
347
|
-
out = out.split(lit).join('<参数>');
|
|
348
|
-
changed = true;
|
|
349
|
-
}
|
|
350
|
-
// 再交给统一脱敏模块挡凭据类(卡号、验证码、长数字串)。
|
|
351
|
-
try {
|
|
352
|
-
const masked = redactor.redact(out);
|
|
353
|
-
if (masked && masked !== out) { out = masked; changed = true; }
|
|
354
|
-
} catch { /* 脱敏器读坏了不阻断抽取,下面的兜底照样挡长数字 */ }
|
|
355
|
-
if (changed && out.includes('<参数>')) {
|
|
356
|
-
params.push({ name: slot, placeholder: '<参数>', kind: 'text', required: true });
|
|
357
|
-
}
|
|
358
|
-
return out;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
function dedupeParams(params) {
|
|
362
|
-
const seen = new Set();
|
|
363
|
-
const out = [];
|
|
364
|
-
for (const p of params) {
|
|
365
|
-
const key = `${p.name}|${p.placeholder}`;
|
|
366
|
-
if (seen.has(key)) continue;
|
|
367
|
-
seen.add(key);
|
|
368
|
-
out.push(p);
|
|
369
|
-
}
|
|
370
|
-
return out;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* 结构化校验:形状不对就整条不要。
|
|
375
|
-
*
|
|
376
|
-
* 这里刻意**不校验业务值**(金额、单号)—— 那些已经在抽取时换掉了,再校验一遍是白费。
|
|
377
|
-
*/
|
|
378
|
-
function validateRoute(route) {
|
|
379
|
-
if (!route || typeof route !== 'object') return 'bad_route';
|
|
380
|
-
if (route.version !== ROUTE_VERSION) return 'bad_version';
|
|
381
|
-
if (!route.app || typeof route.app.package !== 'string' || route.app.package.length < 3) return 'bad_app';
|
|
382
|
-
if (!route.intent || typeof route.intent.raw !== 'string') return 'bad_intent';
|
|
383
|
-
if (!route.intent.shape || !Number.isInteger(route.intent.shape.stepCount)) return 'bad_shape';
|
|
384
|
-
if (!Array.isArray(route.steps) || route.steps.length === 0) return 'bad_steps';
|
|
385
|
-
if (route.steps.length > MAX_STEPS) return 'too_many_steps';
|
|
386
|
-
for (const step of route.steps) {
|
|
387
|
-
if (!step || !Number.isInteger(step.i) || !REPLAYABLE_ACTIONS.has(step.action)) return 'bad_step';
|
|
388
|
-
if (!step.args || typeof step.args !== 'object') return 'bad_step_args';
|
|
389
|
-
}
|
|
390
|
-
return null;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* 按「App + 意图」从候选路线里挑一条最像的。纯读,不执行。
|
|
395
|
-
*
|
|
396
|
-
* 打分口径:App 必须沾边(不沾边直接出局,避免把别的 App 的步骤套上来),意图词重叠为主,
|
|
397
|
-
* 动作形状只做 tie-break。同分取最近学的那条 —— 新近成功的路线更贴近当前版本的界面。
|
|
398
|
-
*/
|
|
399
|
-
function rankForIntent({ packageName, intentText, candidates } = {}) {
|
|
400
|
-
const pkg = tidy(packageName).toLowerCase();
|
|
401
|
-
const tokens = tokenize(intentText);
|
|
402
|
-
const list = Array.isArray(candidates) ? candidates : [];
|
|
403
|
-
const scored = [];
|
|
404
|
-
for (const entry of list) {
|
|
405
|
-
const route = entry && entry.route ? entry.route : entry;
|
|
406
|
-
if (!route || !route.app) continue;
|
|
407
|
-
const candidatePkg = tidy(route.app.package).toLowerCase();
|
|
408
|
-
let appMatch = 0;
|
|
409
|
-
if (pkg && candidatePkg === pkg) appMatch = 1;
|
|
410
|
-
else if (pkg && candidatePkg && (candidatePkg.includes(pkg) || pkg.includes(candidatePkg))) appMatch = 0.6;
|
|
411
|
-
if (appMatch === 0) continue;
|
|
412
|
-
|
|
413
|
-
const hay = new Set([
|
|
414
|
-
...(Array.isArray(route.intent.tokens) ? route.intent.tokens : []),
|
|
415
|
-
...tokenize(route.intent.raw || ''),
|
|
416
|
-
]);
|
|
417
|
-
const shared = tokens.filter((t) => hay.has(t)).length;
|
|
418
|
-
const intentSim = tokens.length > 0 ? shared / tokens.length : 0;
|
|
419
|
-
|
|
420
|
-
const shape = route.intent.shape || {};
|
|
421
|
-
const shapeSim = shape.firstAction && shape.firstAction === (route.steps[0] && route.steps[0].action) ? 0.6 : 0;
|
|
422
|
-
const score = 0.5 * appMatch + 0.35 * intentSim + 0.15 * shapeSim;
|
|
423
|
-
|
|
424
|
-
scored.push({ route, score, appMatch, intentSim });
|
|
425
|
-
}
|
|
426
|
-
scored.sort((a, b) => {
|
|
427
|
-
if (b.score !== a.score) return b.score - a.score;
|
|
428
|
-
return String(b.route.learnedAt || '').localeCompare(String(a.route.learnedAt || ''));
|
|
429
|
-
});
|
|
430
|
-
return scored;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/** 一条路线压成给人看的一段:用来在工具结果里说明「学的是什么」。 */
|
|
434
|
-
function describeRoute(route) {
|
|
435
|
-
if (!route || !Array.isArray(route.steps)) return '';
|
|
436
|
-
const parts = route.steps.map((step) => {
|
|
437
|
-
const verb = VERBS[step.action] || step.action;
|
|
438
|
-
const args = step.args || {};
|
|
439
|
-
const anchor = step.locator && step.locator.text
|
|
440
|
-
? `「${step.locator.text}」`
|
|
441
|
-
: (args.packageName ? args.packageName : '');
|
|
442
|
-
const value = typeof args.text === 'string' && args.text !== '<参数>' && step.action === 'type'
|
|
443
|
-
? ` ${args.text}`
|
|
444
|
-
: '';
|
|
445
|
-
return anchor ? `${verb}${anchor}${value}` : `${verb}${value}`;
|
|
446
|
-
});
|
|
447
|
-
return parts.join(' → ');
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
module.exports = {
|
|
451
|
-
ROUTE_VERSION,
|
|
452
|
-
MAX_STEPS,
|
|
453
|
-
REPLAYABLE_ACTIONS,
|
|
454
|
-
tokenize,
|
|
455
|
-
routeKey,
|
|
456
|
-
extractRoute,
|
|
457
|
-
validateRoute,
|
|
458
|
-
rankForIntent,
|
|
459
|
-
describeRoute,
|
|
460
|
-
};
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* `phone_route_hint`:只读地翻出「这个 App 以前成功过的路线」。
|
|
5
|
-
*
|
|
6
|
-
* 用法是打开一个 App 之后问一句「以前有没有做成过、怎么做的」。**只给参考,不自动执行**:
|
|
7
|
-
* 这里既不连手机、也不发任何动作,模型看过之后还得自己按当前屏幕重新判断。之所以只给摘要不给
|
|
8
|
-
* 原始步骤,是因为界面会变,照抄旧路线比没有更危险 —— 摘要让模型看懂大意,具体每一步仍要现读屏。
|
|
9
|
-
*
|
|
10
|
-
* 注册形状照 `phone/tools.js` 里的那套:走 `ctx.inject(['tools'], …)` 再 `defineTool`,
|
|
11
|
-
* 不能直接读 `ctx.tools`(宿主里那是个影子对象,`register` 一调就抛,被容错吞掉后一条工具都交不到
|
|
12
|
-
* 模型手上)。缺 tools 注入的 profile 静默跳过,别把整块装配掀翻。
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
const routesMemory = require('./routes-memory.js');
|
|
16
|
-
|
|
17
|
-
/** 一次最多回几条路线。 */
|
|
18
|
-
const MAX_ROUTES = 5;
|
|
19
|
-
/** 摘要里单段文字最长多少字。 */
|
|
20
|
-
const MAX_TEXT = 60;
|
|
21
|
-
|
|
22
|
-
/** 动作名换成一句人话动词。 */
|
|
23
|
-
const ACTION_VERBS = {
|
|
24
|
-
tap: '点击',
|
|
25
|
-
type: '输入',
|
|
26
|
-
key: '按键',
|
|
27
|
-
swipe: '滑动',
|
|
28
|
-
gesture: '手势',
|
|
29
|
-
'screen.wait': '等待',
|
|
30
|
-
'screen.read': '读屏',
|
|
31
|
-
'screen.screenshot': '截图',
|
|
32
|
-
'app.open': '打开',
|
|
33
|
-
'app.close': '关闭',
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
function clip(text) {
|
|
37
|
-
const value = typeof text === 'string' ? text.trim() : '';
|
|
38
|
-
if (!value) return '';
|
|
39
|
-
return value.length > MAX_TEXT ? `${value.slice(0, MAX_TEXT)}…` : value;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 把一步压成一句摘要。只取最有辨识度的一个参数,不要把小箭头变成一段代码。
|
|
44
|
-
*/
|
|
45
|
-
function summarizeStep(step) {
|
|
46
|
-
if (!step || typeof step !== 'object') return '';
|
|
47
|
-
const action = typeof step.action === 'string' ? step.action.trim() : '';
|
|
48
|
-
if (!action) return '';
|
|
49
|
-
const args = step.args && typeof step.args === 'object' ? step.args : {};
|
|
50
|
-
const verb = ACTION_VERBS[action] || action;
|
|
51
|
-
|
|
52
|
-
let object = '';
|
|
53
|
-
if (action === 'type') {
|
|
54
|
-
const text = clip(args.text);
|
|
55
|
-
if (text) object = `「${text}」`;
|
|
56
|
-
} else if (action === 'tap') {
|
|
57
|
-
const target = clip(args.target);
|
|
58
|
-
if (target) object = `「${target}」`;
|
|
59
|
-
else if (typeof args.ref === 'string' && args.ref.trim()) object = args.ref.trim();
|
|
60
|
-
} else if (action === 'key') {
|
|
61
|
-
object = typeof args.key === 'string' ? args.key : '';
|
|
62
|
-
} else if (action === 'app.open') {
|
|
63
|
-
object = clip(args.packageName);
|
|
64
|
-
} else if (action === 'swipe') {
|
|
65
|
-
object = typeof args.direction === 'string' ? args.direction : '';
|
|
66
|
-
} else {
|
|
67
|
-
object = clip(args.target) || clip(args.text) || clip(args.packageName) || (typeof args.ref === 'string' ? args.ref.trim() : '');
|
|
68
|
-
}
|
|
69
|
-
return object ? `${verb}${object}` : verb;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* 把一条路线压成可读的摘要对象:步骤数、时间、整条路线的一句话。
|
|
74
|
-
*/
|
|
75
|
-
function summarizeRoute(route) {
|
|
76
|
-
const steps = route && Array.isArray(route.steps) ? route.steps : [];
|
|
77
|
-
const parts = steps.map(summarizeStep).filter(Boolean);
|
|
78
|
-
return {
|
|
79
|
-
at: route && Number.isFinite(route.at) ? route.at : 0,
|
|
80
|
-
stepCount: steps.length,
|
|
81
|
-
summary: parts.join(' → '),
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** 取这个 App 最近成功的几条,压成摘要。没有记录回空数组。 */
|
|
86
|
-
function hintFor(packageName) {
|
|
87
|
-
const routes = routesMemory.list(packageName);
|
|
88
|
-
// 最近的先给:新近成功的路线更贴近现在这个版本的界面。
|
|
89
|
-
return routes
|
|
90
|
-
.slice()
|
|
91
|
-
.sort((a, b) => b.at - a.at)
|
|
92
|
-
.slice(0, MAX_ROUTES)
|
|
93
|
-
.map(summarizeRoute);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* 注册 `phone_route_hint`。与 `installPhoneTools` 同一口径:拿不到 tools 注入就静静不装。
|
|
98
|
-
*/
|
|
99
|
-
function installRouteHint(ctx) {
|
|
100
|
-
if (!ctx || typeof ctx.inject !== 'function') return;
|
|
101
|
-
let defineTool;
|
|
102
|
-
try {
|
|
103
|
-
({ defineTool } = require('@deepseek-ai/dsh-tools'));
|
|
104
|
-
} catch {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
ctx.inject(['tools'], (scoped) => {
|
|
109
|
-
if (!scoped || !scoped.tools || typeof scoped.tools.register !== 'function') return;
|
|
110
|
-
scoped.tools.register(defineTool({
|
|
111
|
-
name: 'phone_route_hint',
|
|
112
|
-
description: 'Look up routes this app has succeeded with before, as short step summaries. This is reference only — nothing is executed. After opening an app, use it to recall a past successful path, then re-read the screen and decide each step yourself; the UI may have changed.',
|
|
113
|
-
parameters: {
|
|
114
|
-
packageName: {
|
|
115
|
-
type: 'string',
|
|
116
|
-
required: true,
|
|
117
|
-
description: 'Android package name of the app, e.g. "com.tencent.mm".',
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
output: {
|
|
121
|
-
schema: {
|
|
122
|
-
type: 'object',
|
|
123
|
-
additionalProperties: false,
|
|
124
|
-
properties: {
|
|
125
|
-
ok: { type: 'boolean' },
|
|
126
|
-
packageName: { type: 'string' },
|
|
127
|
-
count: { type: 'number' },
|
|
128
|
-
// 一条路线是「步骤数 + 摘要」,形状固定,但为了紧凑允许附加字段。
|
|
129
|
-
routes: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
130
|
-
note: { type: 'string' },
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
render: (_args, value) => {
|
|
134
|
-
const count = Array.isArray(value.routes) ? value.routes.length : 0;
|
|
135
|
-
const lines = [`这个 App 以前成功过 ${count} 条路线(仅供参考,不会自动执行):`];
|
|
136
|
-
for (const route of value.routes || []) lines.push(`- ${route.summary}`);
|
|
137
|
-
if (!count) lines.push('- 还没有记录。');
|
|
138
|
-
return [{ type: 'text', text: lines.join('\n') }];
|
|
139
|
-
},
|
|
140
|
-
},
|
|
141
|
-
execute: async (args) => {
|
|
142
|
-
const pkg = typeof args.packageName === 'string' ? args.packageName.trim() : '';
|
|
143
|
-
if (!pkg) throw new Error('要给一个包名(例如 com.tencent.mm)才能查历史路线。');
|
|
144
|
-
const routes = hintFor(pkg);
|
|
145
|
-
const value = {
|
|
146
|
-
ok: true,
|
|
147
|
-
packageName: pkg,
|
|
148
|
-
count: routes.length,
|
|
149
|
-
routes,
|
|
150
|
-
};
|
|
151
|
-
if (routes.length === 0) value.note = '这个 App 还没有成功过的路线记录。';
|
|
152
|
-
return value;
|
|
153
|
-
},
|
|
154
|
-
}));
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
module.exports = {
|
|
159
|
-
installRouteHint,
|
|
160
|
-
summarizeStep,
|
|
161
|
-
summarizeRoute,
|
|
162
|
-
hintFor,
|
|
163
|
-
MAX_ROUTES,
|
|
164
|
-
};
|