@autotest/agent 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -75
- package/dist/api/agent_api.d.ts +2 -6
- package/dist/api/agent_api.js +6 -11
- package/dist/autotest-cli.js +9 -2
- package/dist/common/config/config.yaml +134 -0
- package/dist/common/model/custom_model.js +63 -5
- package/dist/common/path_utils.d.ts +7 -3
- package/dist/common/path_utils.js +12 -5
- package/dist/common/result_parser.js +1 -1
- package/dist/core/agent/layered_agent/decision.js +61 -23
- package/dist/core/agent/layered_agent/execute_agent/general_agent.js +1 -1
- package/dist/core/agent/layered_agent/execute_agent/mcp_prompts.d.ts +1 -1
- package/dist/core/agent/layered_agent/execute_agent/mcp_prompts.js +5 -5
- package/dist/core/agent/layered_agent/execute_agent/parser/general_parser.js +1 -1
- package/dist/core/agent/layered_agent/execute_agent/parser/glm_parser.js +1 -1
- package/dist/core/agent/layered_agent/execute_agent/parser/mai_ui_parser.js +1 -1
- package/dist/core/agent/layered_agent/execute_agent/prompts.d.ts +1 -1
- package/dist/core/agent/layered_agent/execute_agent/prompts.js +5 -5
- package/dist/core/mcp/mcp_tools.d.ts +19 -2
- package/dist/core/mcp/mcp_tools.js +40 -12
- package/dist/core/memory/_utils.d.ts +18 -0
- package/dist/core/memory/_utils.js +140 -0
- package/dist/core/memory/replan_manager.d.ts +9 -0
- package/dist/core/memory/replan_manager.js +12 -0
- package/dist/core/memory/tool_player_popup.d.ts +66 -0
- package/dist/core/memory/tool_player_popup.js +276 -0
- package/dist/core/memory/tool_player_replan.d.ts +49 -0
- package/dist/core/memory/tool_player_replan.js +226 -0
- package/dist/core/memory/tool_player_xpath.d.ts +52 -0
- package/dist/core/memory/tool_player_xpath.js +139 -0
- package/dist/core/prompt/prompt_loader.js +3 -1
- package/dist/core/prompt/templates/debug/hypium/hypium_v2.0_optimize_scroll_search.md +310 -0
- package/dist/core/prompt/templates/debug/hypium/hypium_v2.0_relative_1000.md +284 -0
- package/dist/core/prompt/templates/debug/planner/planner_v3.0.md +140 -0
- package/dist/core/prompt/templates/debug/single/single_v2.0_optimize_scroll_search.md +272 -0
- package/dist/core/prompt/templates/debug/single/single_v2.0_relative_1000.md +277 -0
- package/dist/core/prompt/templates/single/single_v2.0_0_1000.md +315 -0
- package/dist/core/skill/skill_manager.js +4 -1
- package/dist/core/skill/skills/cancel-all-follows/SKILL.md +23 -0
- package/dist/core/skill/skills/create-local-image/SKILL.md +12 -0
- package/dist/core/skill/skills/create-local-video/SKILL.md +12 -0
- package/dist/core/skill/skills/dismiss-popups/SKILL.md +39 -0
- package/dist/core/skill/skills/douyin_test_skills.md +16 -0
- package/dist/data_types/case.d.ts +32 -0
- package/dist/data_types/case.js +57 -0
- package/dist/data_types/index.d.ts +2 -0
- package/dist/data_types/index.js +6 -1
- package/dist/data_types/screen.d.ts +12 -0
- package/dist/data_types/screen.js +23 -0
- package/dist/mcp-server/types.js +72 -50
- package/dist/reporter/generator.d.ts +0 -14
- package/dist/reporter/generator.js +18 -504
- package/dist/reporter/html_template/index.d.ts +56 -0
- package/dist/reporter/html_template/index.js +524 -0
- package/dist/reporter/json_report.d.ts +1 -0
- package/dist/reporter/json_report.js +6 -0
- package/dist/reporter/types.d.ts +1 -0
- package/dist/reporter/types.js +1 -0
- package/dist/tools/batch_runner.d.ts +3 -1
- package/dist/tools/batch_runner.js +21 -6
- package/dist/tools/case_runner/report.d.ts +9 -0
- package/dist/tools/case_runner/report.js +264 -0
- package/dist/tools/case_runner/runner.d.ts +57 -0
- package/dist/tools/case_runner/runner.js +410 -0
- package/dist/tools/config/config_tool.d.ts +13 -0
- package/dist/tools/config/config_tool.js +339 -0
- package/dist/tools/prompt_compare/compare_runner.d.ts +13 -0
- package/dist/tools/prompt_compare/compare_runner.js +360 -0
- package/package.json +7 -3
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replan mixin for ToolPlayer.
|
|
3
|
+
*
|
|
4
|
+
* Ported from python core/memory/tool_player_replan.py.
|
|
5
|
+
* Contains the replan prompt builder and the triggerReplan method.
|
|
6
|
+
* Delegates to a replanCallback callable so the host application owns the
|
|
7
|
+
* replanning lifecycle.
|
|
8
|
+
*/
|
|
9
|
+
interface RecordingRecord {
|
|
10
|
+
toolName?: string;
|
|
11
|
+
tool_name?: string;
|
|
12
|
+
arguments?: Record<string, unknown>;
|
|
13
|
+
result?: unknown;
|
|
14
|
+
}
|
|
15
|
+
interface FailureInfo {
|
|
16
|
+
failureReason?: string;
|
|
17
|
+
failure_reason?: string;
|
|
18
|
+
failedStep?: string;
|
|
19
|
+
failed_step?: string;
|
|
20
|
+
suggestion?: string;
|
|
21
|
+
failedStepIndex?: number;
|
|
22
|
+
failed_step_index?: number;
|
|
23
|
+
originalTask?: string;
|
|
24
|
+
original_task?: string;
|
|
25
|
+
}
|
|
26
|
+
interface ReplanHost {
|
|
27
|
+
replanTriggered: boolean;
|
|
28
|
+
replanCallback?: (prompt: string) => Promise<string>;
|
|
29
|
+
currentSession: {
|
|
30
|
+
task: string;
|
|
31
|
+
recordings: RecordingRecord[];
|
|
32
|
+
} | null;
|
|
33
|
+
currentRecordings: RecordingRecord[];
|
|
34
|
+
lastPlaybackResults: Array<Record<string, unknown>> | null;
|
|
35
|
+
config?: {
|
|
36
|
+
taskTimeout?: number;
|
|
37
|
+
task_timeout?: number;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export declare class ReplanMixin {
|
|
41
|
+
protected host: ReplanHost;
|
|
42
|
+
constructor(host: ReplanHost);
|
|
43
|
+
buildReplanPrompt(originalTask: string, fullHistory: RecordingRecord[], failureInfo: FailureInfo): string;
|
|
44
|
+
static formatHistoryForReplan(fullHistory: RecordingRecord[]): string;
|
|
45
|
+
static extractActionDescription(messageText: string): string;
|
|
46
|
+
static parseResultSummary(result: unknown): [string, boolean];
|
|
47
|
+
triggerReplan(failureInfo: FailureInfo, remainingTimeout?: number): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Replan mixin for ToolPlayer.
|
|
4
|
+
*
|
|
5
|
+
* Ported from python core/memory/tool_player_replan.py.
|
|
6
|
+
* Contains the replan prompt builder and the triggerReplan method.
|
|
7
|
+
* Delegates to a replanCallback callable so the host application owns the
|
|
8
|
+
* replanning lifecycle.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.ReplanMixin = void 0;
|
|
12
|
+
const logger_1 = require("../../common/logger");
|
|
13
|
+
class ReplanMixin {
|
|
14
|
+
host;
|
|
15
|
+
constructor(host) {
|
|
16
|
+
this.host = host;
|
|
17
|
+
}
|
|
18
|
+
buildReplanPrompt(originalTask, fullHistory, failureInfo) {
|
|
19
|
+
const failureReason = failureInfo.failureReason ?? failureInfo.failure_reason ?? '未知原因';
|
|
20
|
+
const failedStep = failureInfo.failedStep ?? failureInfo.failed_step ?? '未知步骤';
|
|
21
|
+
const suggestion = failureInfo.suggestion ?? '';
|
|
22
|
+
const historyText = ReplanMixin.formatHistoryForReplan(fullHistory);
|
|
23
|
+
return `## 任务执行失败,需要重新规划
|
|
24
|
+
|
|
25
|
+
### 原始任务
|
|
26
|
+
${originalTask}
|
|
27
|
+
|
|
28
|
+
### 已回放的步骤
|
|
29
|
+
${historyText}
|
|
30
|
+
|
|
31
|
+
### 当前失败信息
|
|
32
|
+
- **失败原因**: ${failureReason}
|
|
33
|
+
- **失败步骤**: ${failedStep}
|
|
34
|
+
|
|
35
|
+
### 重新规划要求
|
|
36
|
+
请分析失败原因和执行历史,重新规划执行方案:
|
|
37
|
+
1. 查看完整执行历史,理解之前的成功步骤和当前失败
|
|
38
|
+
2. 检查失败原因,判断是否需要更换操作方式
|
|
39
|
+
3. 如果目标选项不存在,尝试寻找替代方案
|
|
40
|
+
4. 如果操作方式不对,调整操作策略
|
|
41
|
+
5. 输出新的执行指令
|
|
42
|
+
|
|
43
|
+
${suggestion ? suggestion + '\n' : ''}
|
|
44
|
+
请根据以上分析,给出新的执行方案。
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
47
|
+
static formatHistoryForReplan(fullHistory) {
|
|
48
|
+
if (!fullHistory || fullHistory.length === 0)
|
|
49
|
+
return '无执行历史';
|
|
50
|
+
const lines = [];
|
|
51
|
+
for (let i = 0; i < fullHistory.length; i++) {
|
|
52
|
+
const rec = fullHistory[i];
|
|
53
|
+
const toolName = rec.toolName ?? rec.tool_name ?? 'unknown';
|
|
54
|
+
const args = rec.arguments ?? {};
|
|
55
|
+
const result = rec.result ?? '';
|
|
56
|
+
const messageRaw = args['message'];
|
|
57
|
+
let messageText;
|
|
58
|
+
if (typeof messageRaw === 'string') {
|
|
59
|
+
messageText = messageRaw;
|
|
60
|
+
}
|
|
61
|
+
else if (messageRaw !== null && messageRaw !== undefined) {
|
|
62
|
+
messageText = String(messageRaw);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
messageText = '';
|
|
66
|
+
}
|
|
67
|
+
if (typeof messageText === 'string' && messageText.trim().startsWith('{')) {
|
|
68
|
+
messageText = ReplanMixin.extractActionDescription(messageText);
|
|
69
|
+
}
|
|
70
|
+
const [resultText, isSuccess] = ReplanMixin.parseResultSummary(result);
|
|
71
|
+
let stepSummary;
|
|
72
|
+
if (toolName === 'execute') {
|
|
73
|
+
stepSummary = `执行:${messageText || '操作'}`;
|
|
74
|
+
}
|
|
75
|
+
else if (toolName === 'verify') {
|
|
76
|
+
const desc = args['description'] ?? '检查';
|
|
77
|
+
stepSummary = `验证:${desc}`;
|
|
78
|
+
if (resultText && resultText.length > 5) {
|
|
79
|
+
stepSummary += ` - ${resultText.slice(0, 40)}`;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
stepSummary = toolName;
|
|
84
|
+
if (resultText && resultText.length > 5) {
|
|
85
|
+
stepSummary += ` - ${resultText.slice(0, 40)}`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const statusIcon = isSuccess ? '✅' : '❌';
|
|
89
|
+
lines.push(` ${i + 1}. ${statusIcon} ${stepSummary}`);
|
|
90
|
+
}
|
|
91
|
+
return lines.join('\n');
|
|
92
|
+
}
|
|
93
|
+
static extractActionDescription(messageText) {
|
|
94
|
+
try {
|
|
95
|
+
const msgData = JSON.parse(messageText);
|
|
96
|
+
const action = msgData['action'] ?? '操作';
|
|
97
|
+
const log = msgData['log'];
|
|
98
|
+
if (log)
|
|
99
|
+
return log;
|
|
100
|
+
const actionDescriptions = {
|
|
101
|
+
click: '点击', double_click: '双击', long_click: '长按',
|
|
102
|
+
swipe: '滑动', input: '输入',
|
|
103
|
+
};
|
|
104
|
+
return actionDescriptions[action] ?? action;
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return messageText;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
static parseResultSummary(result) {
|
|
111
|
+
if (result === null || result === undefined)
|
|
112
|
+
return ['无结果', true];
|
|
113
|
+
if (typeof result === 'boolean')
|
|
114
|
+
return [result ? '成功' : '失败', result];
|
|
115
|
+
if (typeof result === 'object' && !Array.isArray(result)) {
|
|
116
|
+
const obj = result;
|
|
117
|
+
let isSuccess = true;
|
|
118
|
+
if ('success' in obj)
|
|
119
|
+
isSuccess = Boolean(obj['success']);
|
|
120
|
+
let resultText = '';
|
|
121
|
+
if ('result' in obj)
|
|
122
|
+
resultText = String(obj['result']);
|
|
123
|
+
else if ('message' in obj)
|
|
124
|
+
resultText = String(obj['message']);
|
|
125
|
+
else if ('error' in obj) {
|
|
126
|
+
resultText = String(obj['error']);
|
|
127
|
+
isSuccess = false;
|
|
128
|
+
}
|
|
129
|
+
else
|
|
130
|
+
resultText = String(result);
|
|
131
|
+
return [resultText, isSuccess];
|
|
132
|
+
}
|
|
133
|
+
if (Array.isArray(result)) {
|
|
134
|
+
if (result.length === 0)
|
|
135
|
+
return ['空列表', true];
|
|
136
|
+
if (result.length === 1)
|
|
137
|
+
return ReplanMixin.parseResultSummary(result[0]);
|
|
138
|
+
return [`列表(${result.length}项)`, true];
|
|
139
|
+
}
|
|
140
|
+
if (typeof result === 'string') {
|
|
141
|
+
let resultText = result;
|
|
142
|
+
let isSuccess = true;
|
|
143
|
+
if (resultText.startsWith('{') || resultText.startsWith('[')) {
|
|
144
|
+
try {
|
|
145
|
+
const parsed = JSON.parse(resultText);
|
|
146
|
+
return ReplanMixin.parseResultSummary(parsed);
|
|
147
|
+
}
|
|
148
|
+
catch { /* ignore */ }
|
|
149
|
+
}
|
|
150
|
+
if (resultText.includes('[FAILURE]') || resultText.includes('[FAIL]') || resultText.includes('[ERROR]')) {
|
|
151
|
+
isSuccess = false;
|
|
152
|
+
}
|
|
153
|
+
else if (resultText.includes('[SUCCESS]') || resultText.includes('[OK]') || resultText.includes('[DONE]')) {
|
|
154
|
+
isSuccess = true;
|
|
155
|
+
}
|
|
156
|
+
else if (resultText.toLowerCase().startsWith('error')) {
|
|
157
|
+
isSuccess = false;
|
|
158
|
+
}
|
|
159
|
+
if (resultText.includes('[SUCCESS]')) {
|
|
160
|
+
const match = resultText.match(/\[SUCCESS\]\s*([\s\S]+)/);
|
|
161
|
+
if (match)
|
|
162
|
+
resultText = match[1].trim().split('\n')[0].slice(0, 60);
|
|
163
|
+
}
|
|
164
|
+
else if (resultText.includes('[FAILURE]')) {
|
|
165
|
+
const match = resultText.match(/\[FAILURE\]\s*([\s\S]+)/);
|
|
166
|
+
if (match)
|
|
167
|
+
resultText = match[1].trim().split('\n')[0].slice(0, 60);
|
|
168
|
+
}
|
|
169
|
+
return [resultText, isSuccess];
|
|
170
|
+
}
|
|
171
|
+
return [String(result), true];
|
|
172
|
+
}
|
|
173
|
+
async triggerReplan(failureInfo, remainingTimeout) {
|
|
174
|
+
if (this.host.replanTriggered) {
|
|
175
|
+
logger_1.logger.warning('[ToolPlayer] Replan already triggered, skipping duplicate call');
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
this.host.replanTriggered = true;
|
|
179
|
+
if (!this.host.replanCallback) {
|
|
180
|
+
logger_1.logger.warning('[ToolPlayer] No replanCallback available, cannot replan');
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const originalTask = this.host.currentSession?.task ?? failureInfo.originalTask ?? failureInfo.original_task ?? '未知任务';
|
|
184
|
+
const failedStepIndex = failureInfo.failedStepIndex ?? failureInfo.failed_step_index ?? 0;
|
|
185
|
+
const fullHistory = this.host.currentRecordings.slice(0, failedStepIndex);
|
|
186
|
+
const replanPrompt = this.buildReplanPrompt(originalTask, fullHistory, failureInfo);
|
|
187
|
+
logger_1.logger.warning(`[ToolPlayer] Triggering replan with prompt:\n${replanPrompt}`);
|
|
188
|
+
const timeout = remainingTimeout ?? this.host.config?.taskTimeout ?? this.host.config?.task_timeout ?? 1800;
|
|
189
|
+
logger_1.logger.info(`[ToolPlayer] Replan timeout: ${timeout}s`);
|
|
190
|
+
try {
|
|
191
|
+
const replanPromise = this.host.replanCallback(replanPrompt);
|
|
192
|
+
const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout * 1000));
|
|
193
|
+
const replanResult = await Promise.race([replanPromise, timeoutPromise]);
|
|
194
|
+
const resultStr = replanResult ? String(replanResult) : '';
|
|
195
|
+
logger_1.logger.info(`[ToolPlayer] Replan completed: ${resultStr}`);
|
|
196
|
+
if (!this.host.lastPlaybackResults)
|
|
197
|
+
this.host.lastPlaybackResults = [];
|
|
198
|
+
this.host.lastPlaybackResults.push({
|
|
199
|
+
order: -1,
|
|
200
|
+
tool_name: 'replan',
|
|
201
|
+
arguments: { prompt: replanPrompt },
|
|
202
|
+
original_result: null,
|
|
203
|
+
playback_result: resultStr,
|
|
204
|
+
success: true,
|
|
205
|
+
is_replan: true,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
catch (e) {
|
|
209
|
+
const errMsg = e instanceof Error ? e.message : String(e);
|
|
210
|
+
logger_1.logger.error(`[ToolPlayer] Replan failed: ${errMsg}`);
|
|
211
|
+
if (!this.host.lastPlaybackResults)
|
|
212
|
+
this.host.lastPlaybackResults = [];
|
|
213
|
+
this.host.lastPlaybackResults.push({
|
|
214
|
+
order: -1,
|
|
215
|
+
tool_name: 'replan',
|
|
216
|
+
arguments: { prompt: replanPrompt },
|
|
217
|
+
original_result: null,
|
|
218
|
+
playback_result: errMsg.includes('timeout') ? `Timeout after ${timeout}s` : errMsg,
|
|
219
|
+
success: false,
|
|
220
|
+
error: errMsg,
|
|
221
|
+
is_replan: true,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
exports.ReplanMixin = ReplanMixin;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* XPath-based component finding and tool execution mixin for ToolPlayer.
|
|
3
|
+
*
|
|
4
|
+
* Ported from python core/memory/tool_player_xpath.py.
|
|
5
|
+
*
|
|
6
|
+
* NOTE: The Python version depends on `hypium` (a Python-only HarmonyOS UI
|
|
7
|
+
* test framework that provides XPath-based component finding via UiDriver)
|
|
8
|
+
* and the Python `agents` SDK (ToolContext/Usage). Neither exists in Node.js.
|
|
9
|
+
*
|
|
10
|
+
* The framework-agnostic helpers (coordinate parsing, normalization, bounds
|
|
11
|
+
* center calculation) are ported faithfully. The hypium-dependent methods
|
|
12
|
+
* (tryXpathExecution, findBestComponentByXpath, getDriver, getHypiumBy)
|
|
13
|
+
* are structural stubs that log warnings and return null, signaling the
|
|
14
|
+
* caller to fall back to tool-registry execution.
|
|
15
|
+
*/
|
|
16
|
+
interface XpathHost {
|
|
17
|
+
device: {
|
|
18
|
+
callTool: (name: string, args: Record<string, unknown>) => Promise<unknown>;
|
|
19
|
+
getScreenshot?: () => Promise<{
|
|
20
|
+
base64Data?: string;
|
|
21
|
+
width?: number;
|
|
22
|
+
height?: number;
|
|
23
|
+
screenshotPath?: string;
|
|
24
|
+
}>;
|
|
25
|
+
};
|
|
26
|
+
reportGenerator?: unknown;
|
|
27
|
+
executorAgent?: unknown;
|
|
28
|
+
config?: Record<string, unknown>;
|
|
29
|
+
currentIndex: number;
|
|
30
|
+
currentRecordings: Array<Record<string, unknown>>;
|
|
31
|
+
}
|
|
32
|
+
export declare const ACTION_MAP: Record<string, string>;
|
|
33
|
+
export declare class XpathMixin {
|
|
34
|
+
protected host: XpathHost;
|
|
35
|
+
constructor(host: XpathHost);
|
|
36
|
+
static parseBoundsCenter(bounds: string | null | undefined): [number, number] | null;
|
|
37
|
+
static normalizeCachedPoint(x: unknown, y: unknown): [number, number] | null;
|
|
38
|
+
static getDriver(): unknown;
|
|
39
|
+
static getHypiumBy(): unknown;
|
|
40
|
+
static getToolRegistry(): Record<string, unknown>;
|
|
41
|
+
tryXpathExecution(_toolName: string, _arguments: Record<string, unknown>, clickCache: Record<string, unknown>, _screenshot: unknown, _toolStartTime: Date): Promise<string | null>;
|
|
42
|
+
tryTextAction(clickCache: Record<string, unknown>, arguments_: Record<string, unknown>, screenshot: {
|
|
43
|
+
screenshotPath?: string;
|
|
44
|
+
}, toolStartTime: Date): Promise<string | null>;
|
|
45
|
+
executeViaToolRegistry(toolName: string, arguments_: Record<string, unknown>, screenshot: {
|
|
46
|
+
screenshotPath?: string;
|
|
47
|
+
}, toolStartTime: Date): Promise<string>;
|
|
48
|
+
reportAction(action: Record<string, unknown>, screenshot: {
|
|
49
|
+
screenshotPath?: string;
|
|
50
|
+
}, toolStartTime: Date): void;
|
|
51
|
+
}
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* XPath-based component finding and tool execution mixin for ToolPlayer.
|
|
4
|
+
*
|
|
5
|
+
* Ported from python core/memory/tool_player_xpath.py.
|
|
6
|
+
*
|
|
7
|
+
* NOTE: The Python version depends on `hypium` (a Python-only HarmonyOS UI
|
|
8
|
+
* test framework that provides XPath-based component finding via UiDriver)
|
|
9
|
+
* and the Python `agents` SDK (ToolContext/Usage). Neither exists in Node.js.
|
|
10
|
+
*
|
|
11
|
+
* The framework-agnostic helpers (coordinate parsing, normalization, bounds
|
|
12
|
+
* center calculation) are ported faithfully. The hypium-dependent methods
|
|
13
|
+
* (tryXpathExecution, findBestComponentByXpath, getDriver, getHypiumBy)
|
|
14
|
+
* are structural stubs that log warnings and return null, signaling the
|
|
15
|
+
* caller to fall back to tool-registry execution.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.XpathMixin = exports.ACTION_MAP = void 0;
|
|
19
|
+
const logger_1 = require("../../common/logger");
|
|
20
|
+
const _utils_1 = require("./_utils");
|
|
21
|
+
exports.ACTION_MAP = {
|
|
22
|
+
click: 'Tap',
|
|
23
|
+
double_click: 'Double Tap',
|
|
24
|
+
long_click: 'Long Press',
|
|
25
|
+
drag: 'Drag',
|
|
26
|
+
swipe: 'Swipe',
|
|
27
|
+
pinch_in: 'Pinch In',
|
|
28
|
+
pinch_out: 'Pinch Out',
|
|
29
|
+
input_text: 'Type',
|
|
30
|
+
clear_text: 'Clear Text',
|
|
31
|
+
};
|
|
32
|
+
class XpathMixin {
|
|
33
|
+
host;
|
|
34
|
+
constructor(host) {
|
|
35
|
+
this.host = host;
|
|
36
|
+
}
|
|
37
|
+
static parseBoundsCenter(bounds) {
|
|
38
|
+
if (!bounds)
|
|
39
|
+
return null;
|
|
40
|
+
const match = String(bounds).match(/\[(\d+),(\d+)\]\[(\d+),(\d+)\]/);
|
|
41
|
+
if (!match)
|
|
42
|
+
return null;
|
|
43
|
+
const [, x1, y1, x2, y2] = match.map(Number);
|
|
44
|
+
return [Math.floor((x1 + x2) / 2), Math.floor((y1 + y2) / 2)];
|
|
45
|
+
}
|
|
46
|
+
static normalizeCachedPoint(x, y) {
|
|
47
|
+
if (x === null || x === undefined || y === null || y === undefined)
|
|
48
|
+
return null;
|
|
49
|
+
try {
|
|
50
|
+
return [parseInt(String(x), 10), parseInt(String(y), 10)];
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
static getDriver() {
|
|
57
|
+
logger_1.logger.warning('[ToolPlayer] getDriver: hypium UiDriver not available in Node.js, returning null');
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
static getHypiumBy() {
|
|
61
|
+
logger_1.logger.warning('[ToolPlayer] getHypiumBy: hypium BY not available in Node.js, returning null');
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
static getToolRegistry() {
|
|
65
|
+
try {
|
|
66
|
+
return {};
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async tryXpathExecution(_toolName, _arguments, clickCache, _screenshot, _toolStartTime) {
|
|
73
|
+
const xpath = clickCache['xpath'];
|
|
74
|
+
if (xpath) {
|
|
75
|
+
logger_1.logger.info(`[ToolPlayer] Cached xpath available: ${xpath}, but hypium not available in Node.js — falling back to execute`);
|
|
76
|
+
}
|
|
77
|
+
logger_1.logger.warning('[ToolPlayer] tryXpathExecution: hypium-based XPath finding not available in Node.js');
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
async tryTextAction(clickCache, arguments_, screenshot, toolStartTime) {
|
|
81
|
+
const textAction = clickCache['action'];
|
|
82
|
+
const text = clickCache['text'] ?? '';
|
|
83
|
+
if (textAction === 'input_text' && text) {
|
|
84
|
+
try {
|
|
85
|
+
await this.host.device.callTool('input_text', { text });
|
|
86
|
+
const result = JSON.stringify({ action: 'input_text', text, message: `Input text: ${text.slice(0, 50)}...` });
|
|
87
|
+
this.reportAction({ _metadata: 'do', action: exports.ACTION_MAP['input_text'] ?? 'Type', log: arguments_['message'] ?? '', element: [] }, screenshot, toolStartTime);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return '{"error": "input_text failed"}';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (textAction === 'clear_text') {
|
|
95
|
+
try {
|
|
96
|
+
await this.host.device.callTool('clear_text', {});
|
|
97
|
+
const result = JSON.stringify({ action: 'clear_text', message: 'Cleared text' });
|
|
98
|
+
this.reportAction({ _metadata: 'do', action: exports.ACTION_MAP['clear_text'] ?? 'Clear Text', log: arguments_['message'] ?? '', element: [] }, screenshot, toolStartTime);
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return '{"error": "clear_text failed"}';
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
async executeViaToolRegistry(toolName, arguments_, screenshot, toolStartTime) {
|
|
108
|
+
try {
|
|
109
|
+
const result = await this.host.device.callTool(toolName, arguments_);
|
|
110
|
+
const resultStr = result ? (typeof result === 'string' ? result : JSON.stringify(result)) : 'OK';
|
|
111
|
+
this.reportAction({ _metadata: 'do', action: toolName, log: `执行工具${toolName}`, element: [] }, screenshot, toolStartTime);
|
|
112
|
+
return resultStr;
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
logger_1.logger.error(`[ToolPlayer] ${toolName} failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
116
|
+
return `{"error": "${e instanceof Error ? e.message : String(e)}"}`;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
reportAction(action, screenshot, toolStartTime) {
|
|
120
|
+
try {
|
|
121
|
+
const toolEndTime = new Date();
|
|
122
|
+
const timeCalcStart = new _utils_1.TimeCalculate(toolStartTime, toolStartTime);
|
|
123
|
+
const reportGen = this.host.reportGenerator;
|
|
124
|
+
if (reportGen && typeof reportGen['execute_agent_do_start'] === 'function') {
|
|
125
|
+
reportGen['execute_agent_do_start']({
|
|
126
|
+
timeCalculate: timeCalcStart,
|
|
127
|
+
thinking: '',
|
|
128
|
+
screenshot,
|
|
129
|
+
action,
|
|
130
|
+
stepCount: this.host.currentIndex + 1,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
logger_1.logger.warning(`[ToolPlayer] reportAction failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
exports.XpathMixin = XpathMixin;
|
|
@@ -63,7 +63,9 @@ exports.DEFAULT_VERSIONS = {
|
|
|
63
63
|
[PromptCategory.FAST_ACTION]: 'v1.0',
|
|
64
64
|
[PromptCategory.EXECUTOR]: 'v1.0',
|
|
65
65
|
};
|
|
66
|
-
const TEMPLATES_DIR = path.join(
|
|
66
|
+
const TEMPLATES_DIR = fs.existsSync(path.join(__dirname, 'templates'))
|
|
67
|
+
? path.join(__dirname, 'templates')
|
|
68
|
+
: path.join((0, path_utils_1.findDistRoot)(), 'core', 'prompt', 'templates');
|
|
67
69
|
const _cache = new Map();
|
|
68
70
|
function clearCache() {
|
|
69
71
|
_cache.clear();
|