@autotest/agent 0.0.5 → 0.0.7
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 +10 -0
- package/dist/api/agent_api.d.ts +2 -2
- package/dist/autotest-cli.js +1 -397
- package/dist/common/config/config.yaml +5 -0
- package/dist/common/config/manager.d.ts +7 -7
- package/dist/common/config/types.d.ts +33 -1
- package/dist/common/dfx/index.d.ts +1 -1
- package/dist/common/image_helper.d.ts +2 -2
- package/dist/common/llm_cache/cache_transport.d.ts +1 -1
- package/dist/common/llm_cache/prompt_cache.d.ts +2 -2
- package/dist/common/utils.d.ts +5 -5
- package/dist/core/agent/layered_agent/agent_registry.d.ts +3 -1
- package/dist/core/agent/layered_agent/execute_agent/mcp_prompts.d.ts +1 -1
- package/dist/core/agent/layered_agent/execute_agent/prompts.d.ts +1 -1
- package/dist/core/agent/report_analyzer_agent/prompt.d.ts +1 -1
- package/dist/core/agent/report_analyzer_agent/tools.d.ts +1 -1
- package/dist/core/agent/rewrite_agent/agent.d.ts +5 -5
- package/dist/core/agent/rewrite_agent/guardrails.d.ts +1 -1
- package/dist/core/agent/rewrite_agent/mapping.d.ts +1 -1
- package/dist/core/agent/rewrite_agent/parse.d.ts +1 -1
- package/dist/core/agent/rewrite_agent/types.d.ts +1 -1
- package/dist/core/agent/single_agent/agent.d.ts +2 -2
- package/dist/core/agent/single_agent/hook_manager.d.ts +1 -2
- package/dist/core/agent/single_agent/image_utils.d.ts +1 -2
- package/dist/core/agent/single_agent/scroll_search_agent.d.ts +5 -5
- package/dist/core/agent/single_agent/subagent_manager.d.ts +1 -2
- package/dist/core/agent/single_agent/tool_recorder_hook.d.ts +3 -3
- package/dist/core/device/device_protocol.d.ts +1 -1
- package/dist/core/device/hdc/device.d.ts +1 -1
- package/dist/core/device/index.d.ts +2 -2
- package/dist/core/knowledge/knowledge_manager.d.ts +1 -1
- package/dist/core/knowledge/vectordb.d.ts +1 -1
- package/dist/core/mcp/harmony_hdc_adapter.d.ts +1 -0
- package/dist/core/mcp/mcp_tools.d.ts +11 -2
- package/dist/core/mcp/verify_tool.d.ts +12 -0
- package/dist/core/memory/_utils.d.ts +1 -1
- package/dist/core/memory/index.d.ts +1 -1
- package/dist/core/memory/memory_manager.d.ts +1 -1
- package/dist/core/memory/replan_manager.d.ts +1 -1
- package/dist/core/memory/tool_player.d.ts +5 -5
- package/dist/core/memory/tool_player_popup.d.ts +1 -1
- package/dist/core/memory/tool_player_replan.d.ts +1 -1
- package/dist/core/memory/tool_player_xpath.d.ts +3 -3
- package/dist/core/prompt/prompt_loader.d.ts +1 -1
- package/dist/core/prompt/templates/single/single_v2.0.md +15 -0
- package/dist/core/skill/skill_manager.d.ts +2 -2
- package/dist/data_types/device.d.ts +1 -1
- package/dist/data_types/index.d.ts +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +1 -164
- package/dist/loop/cli.d.ts +4 -4
- package/dist/loop/orchestrator.d.ts +6 -6
- package/dist/loop/result_writer.d.ts +1 -1
- package/dist/loop/types.d.ts +1 -1
- package/dist/tools/batch_runner.js +1 -745
- package/dist/tools/case_runner/report.d.ts +1 -1
- package/dist/tools/case_runner/runner.d.ts +3 -3
- package/dist/tools/case_runner/runner.js +1 -410
- package/dist/tools/config/config_tool.d.ts +6 -6
- package/dist/tools/config/config_tool.js +1 -339
- package/dist/tools/prompt_compare/compare_runner.d.ts +2 -2
- package/dist/tools/prompt_compare/compare_runner.js +1 -360
- package/package.json +2 -2
- package/dist/api/agent_api.js +0 -550
- package/dist/api/autotest_agent.js +0 -205
- package/dist/api/types.js +0 -11
- package/dist/common/case_manager.js +0 -91
- package/dist/common/collect_case.js +0 -128
- package/dist/common/common_errors.js +0 -66
- package/dist/common/config/config_validator.js +0 -28
- package/dist/common/config/manager.js +0 -257
- package/dist/common/config/output.js +0 -59
- package/dist/common/config/types.js +0 -184
- package/dist/common/dfx/client_factory.js +0 -62
- package/dist/common/dfx/context.js +0 -23
- package/dist/common/dfx/index.js +0 -15
- package/dist/common/dfx/recorder.js +0 -191
- package/dist/common/dfx/recording_fetch.js +0 -228
- package/dist/common/dfx/source.js +0 -13
- package/dist/common/image_cacher.js +0 -36
- package/dist/common/image_helper.js +0 -128
- package/dist/common/image_similarity.js +0 -171
- package/dist/common/llm_cache/cache_transport.js +0 -102
- package/dist/common/llm_cache/prompt_cache.js +0 -113
- package/dist/common/logger.js +0 -158
- package/dist/common/model/custom_model.js +0 -525
- package/dist/common/model/index.js +0 -5
- package/dist/common/path_utils.js +0 -88
- package/dist/common/result_parser.js +0 -49
- package/dist/common/usage/token_usage.js +0 -117
- package/dist/common/utils.js +0 -125
- package/dist/core/agent/layered_agent/ability_helper.js +0 -125
- package/dist/core/agent/layered_agent/agent.js +0 -37
- package/dist/core/agent/layered_agent/agent_registry.js +0 -75
- package/dist/core/agent/layered_agent/decision.js +0 -516
- package/dist/core/agent/layered_agent/execute_agent/action_handler.js +0 -155
- package/dist/core/agent/layered_agent/execute_agent/agent_factory.js +0 -41
- package/dist/core/agent/layered_agent/execute_agent/base_agent.js +0 -112
- package/dist/core/agent/layered_agent/execute_agent/general_agent.js +0 -207
- package/dist/core/agent/layered_agent/execute_agent/glm_agent.js +0 -241
- package/dist/core/agent/layered_agent/execute_agent/i18n.js +0 -34
- package/dist/core/agent/layered_agent/execute_agent/index.js +0 -67
- package/dist/core/agent/layered_agent/execute_agent/mai_ui_agent.js +0 -316
- package/dist/core/agent/layered_agent/execute_agent/mcp_prompts.js +0 -154
- package/dist/core/agent/layered_agent/execute_agent/parser/general_parser.js +0 -243
- package/dist/core/agent/layered_agent/execute_agent/parser/glm_parser.js +0 -139
- package/dist/core/agent/layered_agent/execute_agent/parser/index.js +0 -9
- package/dist/core/agent/layered_agent/execute_agent/parser/mai_ui_parser.js +0 -94
- package/dist/core/agent/layered_agent/execute_agent/prompts.js +0 -109
- package/dist/core/agent/layered_agent/execute_agent/simple_reporter.js +0 -33
- package/dist/core/agent/layered_agent/execute_agent/utils.js +0 -52
- package/dist/core/agent/report_analyzer_agent/agent.js +0 -565
- package/dist/core/agent/report_analyzer_agent/config.js +0 -22
- package/dist/core/agent/report_analyzer_agent/data_loader.js +0 -255
- package/dist/core/agent/report_analyzer_agent/guardrails.js +0 -99
- package/dist/core/agent/report_analyzer_agent/hooks.js +0 -31
- package/dist/core/agent/report_analyzer_agent/index.js +0 -52
- package/dist/core/agent/report_analyzer_agent/key_steps_builder.js +0 -42
- package/dist/core/agent/report_analyzer_agent/mapping.js +0 -133
- package/dist/core/agent/report_analyzer_agent/parse.js +0 -375
- package/dist/core/agent/report_analyzer_agent/prompt.js +0 -300
- package/dist/core/agent/report_analyzer_agent/report_writer.js +0 -558
- package/dist/core/agent/report_analyzer_agent/tools.js +0 -150
- package/dist/core/agent/report_analyzer_agent/types.js +0 -274
- package/dist/core/agent/rewrite_agent/agent.js +0 -209
- package/dist/core/agent/rewrite_agent/guardrails.js +0 -95
- package/dist/core/agent/rewrite_agent/mapping.js +0 -56
- package/dist/core/agent/rewrite_agent/parse.js +0 -121
- package/dist/core/agent/rewrite_agent/types.js +0 -37
- package/dist/core/agent/single_agent/agent.js +0 -798
- package/dist/core/agent/single_agent/hook_manager.js +0 -60
- package/dist/core/agent/single_agent/hooks.js +0 -288
- package/dist/core/agent/single_agent/image_utils.js +0 -159
- package/dist/core/agent/single_agent/scroll_search_agent.js +0 -376
- package/dist/core/agent/single_agent/subagent_manager.js +0 -73
- package/dist/core/agent/single_agent/tool_recorder_hook.js +0 -42
- package/dist/core/agent/types.js +0 -2
- package/dist/core/agent/utils/message_builder.js +0 -138
- package/dist/core/device/device_protocol.js +0 -2
- package/dist/core/device/hdc/apps.js +0 -437
- package/dist/core/device/hdc/device.js +0 -219
- package/dist/core/device/hdc/index.js +0 -31
- package/dist/core/device/hdc/input.js +0 -74
- package/dist/core/device/hdc/screenshot.js +0 -171
- package/dist/core/device/index.js +0 -70
- package/dist/core/device/types.js +0 -2
- package/dist/core/knowledge/knowledge_manager.js +0 -70
- package/dist/core/knowledge/vectordb.js +0 -283
- package/dist/core/mcp/adapter_factory.js +0 -99
- package/dist/core/mcp/android_adapter.js +0 -286
- package/dist/core/mcp/base_adapter.js +0 -86
- package/dist/core/mcp/harmony_hdc_adapter.js +0 -782
- package/dist/core/mcp/ios_adapter.js +0 -286
- package/dist/core/mcp/local_tools.js +0 -41
- package/dist/core/mcp/mcp_tools.js +0 -410
- package/dist/core/mcp/types.js +0 -6
- package/dist/core/memory/_utils.js +0 -140
- package/dist/core/memory/compressed_session.js +0 -182
- package/dist/core/memory/context_compressor.js +0 -176
- package/dist/core/memory/index.js +0 -22
- package/dist/core/memory/memory_manager.js +0 -28
- package/dist/core/memory/reflector.js +0 -95
- package/dist/core/memory/replan_manager.js +0 -12
- package/dist/core/memory/session_factory.js +0 -29
- package/dist/core/memory/tool_player.js +0 -225
- package/dist/core/memory/tool_player_popup.js +0 -276
- package/dist/core/memory/tool_player_replan.js +0 -226
- package/dist/core/memory/tool_player_xpath.js +0 -139
- package/dist/core/memory/tool_recorder.js +0 -126
- package/dist/core/preprocessor/task_splitter.js +0 -112
- package/dist/core/prompt/prompt_loader.js +0 -315
- package/dist/core/skill/skill_manager.js +0 -302
- package/dist/data_types/action.js +0 -2
- package/dist/data_types/agent.js +0 -2
- package/dist/data_types/case.js +0 -57
- package/dist/data_types/device.js +0 -2
- package/dist/data_types/index.js +0 -11
- package/dist/data_types/report.js +0 -6
- package/dist/data_types/screen.js +0 -23
- package/dist/data_types/task.js +0 -2
- package/dist/loop/cli.js +0 -127
- package/dist/loop/orchestrator.js +0 -314
- package/dist/loop/result_writer.js +0 -260
- package/dist/loop/types.js +0 -82
- package/dist/mcp-server/server.js +0 -341
- package/dist/mcp-server/types.js +0 -224
- package/dist/reporter/batch_summary.js +0 -197
- package/dist/reporter/generator.js +0 -286
- package/dist/reporter/html_template/index.js +0 -524
- package/dist/reporter/json_report.js +0 -6
- package/dist/reporter/types.js +0 -45
- package/dist/tools/case_runner/report.js +0 -264
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ToolPlayer = exports.ACTION_MAP = exports.DEFAULT_POPUP_CONFIG = void 0;
|
|
4
|
-
const logger_1 = require("../../common/logger");
|
|
5
|
-
const tool_recorder_1 = require("./tool_recorder");
|
|
6
|
-
const custom_model_1 = require("../../common/model/custom_model");
|
|
7
|
-
exports.DEFAULT_POPUP_CONFIG = {
|
|
8
|
-
enabled: true,
|
|
9
|
-
maxRetryAttempts: 1,
|
|
10
|
-
skipPopupSteps: true,
|
|
11
|
-
};
|
|
12
|
-
/** Map recorded tool names to device action display names (aligned with python ACTION_MAP). */
|
|
13
|
-
exports.ACTION_MAP = {
|
|
14
|
-
click: 'Tap',
|
|
15
|
-
double_click: 'Double Tap',
|
|
16
|
-
long_click: 'Long Press',
|
|
17
|
-
drag: 'Drag',
|
|
18
|
-
swipe: 'Swipe',
|
|
19
|
-
pinch_in: 'Pinch In',
|
|
20
|
-
pinch_out: 'Pinch Out',
|
|
21
|
-
input_text: 'Type',
|
|
22
|
-
clear_text: 'Clear Text',
|
|
23
|
-
};
|
|
24
|
-
class ToolPlayer {
|
|
25
|
-
_recording;
|
|
26
|
-
_device;
|
|
27
|
-
_modelConfig;
|
|
28
|
-
_maxRetries;
|
|
29
|
-
_popupConfig;
|
|
30
|
-
_replanCallback;
|
|
31
|
-
_currentIndex = 0;
|
|
32
|
-
_stepCount = 0;
|
|
33
|
-
_replanTriggered = false;
|
|
34
|
-
_lastPlaybackResults = null;
|
|
35
|
-
_popupStats = { detected: 0, closed: 0, skipped: 0, retrySuccess: 0 };
|
|
36
|
-
constructor(opts) {
|
|
37
|
-
this._recording = opts.recording ?? null;
|
|
38
|
-
this._device = opts.device;
|
|
39
|
-
this._modelConfig = opts.modelConfig;
|
|
40
|
-
this._maxRetries = opts.maxRetries ?? 3;
|
|
41
|
-
this._popupConfig = opts.popupConfig ?? exports.DEFAULT_POPUP_CONFIG;
|
|
42
|
-
this._replanCallback = opts.replanCallback;
|
|
43
|
-
}
|
|
44
|
-
/** Load a recording from a file. Returns true on success. */
|
|
45
|
-
loadRecord(filePath) {
|
|
46
|
-
const session = tool_recorder_1.ToolRecorder.loadFromFile(filePath);
|
|
47
|
-
if (session) {
|
|
48
|
-
this._recording = session;
|
|
49
|
-
logger_1.logger.info(`[ToolPlayer] Loaded ${session.records.length} records from ${filePath}`);
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
logger_1.logger.warn(`[ToolPlayer] Failed to load recording from ${filePath}`);
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
/** Load a recording by task name (md5 hash lookup). */
|
|
56
|
-
loadByTask(task, memoryDir) {
|
|
57
|
-
const session = tool_recorder_1.ToolRecorder.findRecording(task, memoryDir);
|
|
58
|
-
if (session) {
|
|
59
|
-
this._recording = session;
|
|
60
|
-
logger_1.logger.info(`[ToolPlayer] Loaded ${session.records.length} records for task "${task}"`);
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
logger_1.logger.warn(`[ToolPlayer] No recording found for task "${task}"`);
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
/** Get the loaded session. */
|
|
67
|
-
getSession() {
|
|
68
|
-
return this._recording;
|
|
69
|
-
}
|
|
70
|
-
getTotalSteps() {
|
|
71
|
-
return this._recording?.records.length ?? 0;
|
|
72
|
-
}
|
|
73
|
-
getCurrentStep() {
|
|
74
|
-
return this._currentIndex;
|
|
75
|
-
}
|
|
76
|
-
lastPlaybackResults() {
|
|
77
|
-
return this._lastPlaybackResults;
|
|
78
|
-
}
|
|
79
|
-
/** Play back the recorded tool calls in order. */
|
|
80
|
-
async play() {
|
|
81
|
-
if (!this._recording) {
|
|
82
|
-
return { totalSteps: 0, replayedSteps: 0, successCount: 0, failureCount: 0, result: 'fail', summary: 'No recording loaded' };
|
|
83
|
-
}
|
|
84
|
-
const records = this._recording.records;
|
|
85
|
-
let replayed = 0;
|
|
86
|
-
let success = 0;
|
|
87
|
-
let failure = 0;
|
|
88
|
-
const details = [];
|
|
89
|
-
for (let i = 0; i < records.length; i++) {
|
|
90
|
-
this._currentIndex = i;
|
|
91
|
-
this._stepCount++;
|
|
92
|
-
const record = records[i];
|
|
93
|
-
let attemptSuccess = false;
|
|
94
|
-
let lastError;
|
|
95
|
-
for (let attempt = 0; attempt <= this._maxRetries; attempt++) {
|
|
96
|
-
try {
|
|
97
|
-
const result = await this._device.callTool(record.toolName, record.arguments);
|
|
98
|
-
const resultStr = typeof result === 'string' ? result : JSON.stringify(result);
|
|
99
|
-
// Popup detection (aligned with python PopupMixin — basic version)
|
|
100
|
-
if (this._popupConfig.enabled && this._detectPopup(resultStr)) {
|
|
101
|
-
this._popupStats.detected++;
|
|
102
|
-
const closed = await this._tryClosePopup();
|
|
103
|
-
if (closed) {
|
|
104
|
-
this._popupStats.closed++;
|
|
105
|
-
if (attempt > 0)
|
|
106
|
-
this._popupStats.retrySuccess++;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
this._popupStats.skipped++;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (resultStr.includes('failed') || resultStr.includes('error')) {
|
|
113
|
-
lastError = resultStr.slice(0, 200);
|
|
114
|
-
if (attempt < this._maxRetries)
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
attemptSuccess = true;
|
|
119
|
-
}
|
|
120
|
-
break;
|
|
121
|
-
}
|
|
122
|
-
catch (e) {
|
|
123
|
-
lastError = e instanceof Error ? e.message : String(e);
|
|
124
|
-
if (attempt < this._maxRetries)
|
|
125
|
-
continue;
|
|
126
|
-
logger_1.logger.warning(`[ToolPlayer] Step ${i + 1} ${record.toolName} failed: ${lastError}`);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
replayed++;
|
|
130
|
-
if (attemptSuccess)
|
|
131
|
-
success++;
|
|
132
|
-
else {
|
|
133
|
-
failure++;
|
|
134
|
-
details.push({ step: i + 1, tool: record.toolName, success: false, error: lastError });
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
// Replan trigger (aligned with python ReplanMixin — basic version)
|
|
138
|
-
if (failure > 0 && this._replanCallback && !this._replanTriggered) {
|
|
139
|
-
this._replanTriggered = true;
|
|
140
|
-
logger_1.logger.info('[ToolPlayer] Triggering replan due to playback failures');
|
|
141
|
-
try {
|
|
142
|
-
await this._replanCallback(`Playback failed at ${failure}/${replayed} steps. Please replan.`);
|
|
143
|
-
}
|
|
144
|
-
catch (e) {
|
|
145
|
-
logger_1.logger.warning(`[ToolPlayer] Replan callback failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
const result = failure === 0 ? 'pass' : success > failure ? 'unknown' : 'fail';
|
|
149
|
-
const summary = `Replayed ${replayed}/${records.length} steps: ${success} success, ${failure} fail`;
|
|
150
|
-
logger_1.logger.info(`[ToolPlayer] ${summary} (popups: ${JSON.stringify(this._popupStats)})`);
|
|
151
|
-
this._lastPlaybackResults = details.map((d) => ({ ...d }));
|
|
152
|
-
return {
|
|
153
|
-
totalSteps: records.length,
|
|
154
|
-
replayedSteps: replayed,
|
|
155
|
-
successCount: success,
|
|
156
|
-
failureCount: failure,
|
|
157
|
-
result: result,
|
|
158
|
-
summary,
|
|
159
|
-
details,
|
|
160
|
-
popupStats: { ...this._popupStats },
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
/** Basic popup detection: check result text for popup-related keywords. */
|
|
164
|
-
_detectPopup(resultStr) {
|
|
165
|
-
return /popup|dialog|modal|permission|allow|deny|close.*button|dismiss/i.test(resultStr);
|
|
166
|
-
}
|
|
167
|
-
/** Basic popup close: try pressing back/home to dismiss. */
|
|
168
|
-
async _tryClosePopup() {
|
|
169
|
-
try {
|
|
170
|
-
await this._device.callTool('go_back', {});
|
|
171
|
-
return true;
|
|
172
|
-
}
|
|
173
|
-
catch {
|
|
174
|
-
return false;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
/** Judge the playback result with a detailed LLM prompt (aligned with python judge_result). */
|
|
178
|
-
async judgeResult(playbackResult) {
|
|
179
|
-
if (!this._modelConfig)
|
|
180
|
-
return playbackResult.summary;
|
|
181
|
-
const taskInfo = this._recording?.task ?? '(unknown task)';
|
|
182
|
-
const resultsText = playbackResult.details
|
|
183
|
-
? playbackResult.details.map((d) => `Step ${d.step}: ${d.tool} — ${d.success ? 'PASS' : `FAIL (${d.error ?? 'unknown'})`}`).join('\n')
|
|
184
|
-
: playbackResult.summary;
|
|
185
|
-
const prompt = this._buildJudgePrompt(taskInfo, resultsText, playbackResult);
|
|
186
|
-
try {
|
|
187
|
-
const llmCaller = new custom_model_1.LLMCaller();
|
|
188
|
-
const result = await llmCaller.call({
|
|
189
|
-
systemPrompt: 'You are a test result judge. Analyze the playback results and determine if the test passed or failed.',
|
|
190
|
-
userPrompt: prompt,
|
|
191
|
-
modelConfig: this._modelConfig,
|
|
192
|
-
});
|
|
193
|
-
return result.text;
|
|
194
|
-
}
|
|
195
|
-
catch {
|
|
196
|
-
return playbackResult.summary;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/** Build the detailed judge prompt (aligned with python _build_judge_prompt). */
|
|
200
|
-
_buildJudgePrompt(taskInfo, resultsText, playbackResult) {
|
|
201
|
-
return `## Task
|
|
202
|
-
${taskInfo}
|
|
203
|
-
|
|
204
|
-
## Playback Results
|
|
205
|
-
${resultsText}
|
|
206
|
-
|
|
207
|
-
## Summary
|
|
208
|
-
Total steps: ${playbackResult.totalSteps}
|
|
209
|
-
Replayed: ${playbackResult.replayedSteps}
|
|
210
|
-
Success: ${playbackResult.successCount}
|
|
211
|
-
Failure: ${playbackResult.failureCount}
|
|
212
|
-
Popup stats: ${JSON.stringify(playbackResult.popupStats ?? {})}
|
|
213
|
-
|
|
214
|
-
## Instructions
|
|
215
|
-
Based on the playback results above, determine whether the test passed or failed.
|
|
216
|
-
|
|
217
|
-
Consider:
|
|
218
|
-
1. Did all critical steps succeed?
|
|
219
|
-
2. Were any failures due to popups or transient issues (not real test failures)?
|
|
220
|
-
3. Was the overall task intent achieved despite minor failures?
|
|
221
|
-
|
|
222
|
-
Output your judgment as either "PASS" or "FAIL" followed by a brief explanation.`;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
exports.ToolPlayer = ToolPlayer;
|
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Popup handling and LLM screenshot analysis mixin for ToolPlayer.
|
|
4
|
-
*
|
|
5
|
-
* Ported from python core/memory/tool_player_popup.py.
|
|
6
|
-
* Detects unexpected popups during playback via LLM screenshot analysis,
|
|
7
|
-
* closes them, and retries the failed step. Also handles skipping popup
|
|
8
|
-
* steps that are no longer relevant.
|
|
9
|
-
*
|
|
10
|
-
* Uses the Node.js LLMCaller instead of direct OpenAI client.
|
|
11
|
-
*/
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.PopupMixin = void 0;
|
|
14
|
-
const logger_1 = require("../../common/logger");
|
|
15
|
-
const custom_model_1 = require("../../common/model/custom_model");
|
|
16
|
-
class PopupMixin {
|
|
17
|
-
host;
|
|
18
|
-
constructor(host) {
|
|
19
|
-
this.host = host;
|
|
20
|
-
}
|
|
21
|
-
async analyzeScreenshotWithLlm(prompt) {
|
|
22
|
-
try {
|
|
23
|
-
const screenshot = this.host.device.getScreenshot ? await this.host.device.getScreenshot() : null;
|
|
24
|
-
if (!screenshot?.base64Data) {
|
|
25
|
-
logger_1.logger.warning('[ToolPlayer] No screenshot available for LLM analysis');
|
|
26
|
-
return '';
|
|
27
|
-
}
|
|
28
|
-
if (!this.host.modelConfig) {
|
|
29
|
-
logger_1.logger.warning('[ToolPlayer] No model config for LLM analysis');
|
|
30
|
-
return '';
|
|
31
|
-
}
|
|
32
|
-
const llmCaller = new custom_model_1.LLMCaller();
|
|
33
|
-
const result = await llmCaller.call({
|
|
34
|
-
systemPrompt: 'You are a screen analysis assistant. Respond only with JSON.',
|
|
35
|
-
userPrompt: prompt,
|
|
36
|
-
modelConfig: this.host.modelConfig,
|
|
37
|
-
images: [{ base64Data: screenshot.base64Data, mimeType: 'image/jpeg' }],
|
|
38
|
-
});
|
|
39
|
-
logger_1.logger.info(`[ToolPlayer] LLM analysis result: ${result.text.slice(0, 200)}`);
|
|
40
|
-
return result.text;
|
|
41
|
-
}
|
|
42
|
-
catch (e) {
|
|
43
|
-
logger_1.logger.error(`[ToolPlayer] LLM screenshot analysis failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
44
|
-
return '';
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
async retryFailedStep(failedStepIndex) {
|
|
48
|
-
try {
|
|
49
|
-
if (!this.host.currentSession || failedStepIndex >= this.host.currentSession.recordings.length) {
|
|
50
|
-
logger_1.logger.error(`[ToolPlayer] Invalid failed_step_index: ${failedStepIndex}`);
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
const originalRecording = this.host.currentSession.recordings[failedStepIndex];
|
|
54
|
-
const toolName = String(originalRecording['tool_name'] ?? originalRecording['toolName'] ?? '');
|
|
55
|
-
const arguments_ = (originalRecording['arguments'] ?? {});
|
|
56
|
-
const clickCache = originalRecording['click_cache'];
|
|
57
|
-
logger_1.logger.info(`[ToolPlayer] Retrying step ${failedStepIndex}: ${toolName}(${JSON.stringify(arguments_)})`);
|
|
58
|
-
if (!this.host.executeToolAsync) {
|
|
59
|
-
logger_1.logger.error('[ToolPlayer] executeToolAsync not available');
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
const retryResult = await this.host.executeToolAsync(toolName, arguments_, clickCache);
|
|
63
|
-
let isSuccess = true;
|
|
64
|
-
if (toolName === 'execute') {
|
|
65
|
-
try {
|
|
66
|
-
const resultData = retryResult.startsWith('{') ? JSON.parse(retryResult) : {};
|
|
67
|
-
if (resultData['replan_required'])
|
|
68
|
-
isSuccess = false;
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
if (['无法', '未找到', '不存在', '失败'].some((kw) => retryResult.includes(kw))) {
|
|
72
|
-
isSuccess = false;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
const playbackRecord = {
|
|
77
|
-
order: originalRecording['order'] ?? failedStepIndex,
|
|
78
|
-
tool_name: toolName,
|
|
79
|
-
arguments: arguments_,
|
|
80
|
-
result: retryResult,
|
|
81
|
-
timestamp: originalRecording['timestamp'] ?? '',
|
|
82
|
-
click_cache: clickCache,
|
|
83
|
-
is_played: true,
|
|
84
|
-
is_retry: true,
|
|
85
|
-
};
|
|
86
|
-
if (failedStepIndex < this.host.currentRecordings.length) {
|
|
87
|
-
this.host.currentRecordings[failedStepIndex] = playbackRecord;
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
this.host.currentRecordings.push(playbackRecord);
|
|
91
|
-
}
|
|
92
|
-
logger_1.logger.info(`[ToolPlayer] Retry step ${failedStepIndex}: ${isSuccess ? 'SUCCESS' : 'FAILED'}`);
|
|
93
|
-
return isSuccess;
|
|
94
|
-
}
|
|
95
|
-
catch (e) {
|
|
96
|
-
logger_1.logger.error(`[ToolPlayer] Retry failed step ${failedStepIndex} error: ${e instanceof Error ? e.message : String(e)}`);
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
async isPopupStillExists() {
|
|
101
|
-
try {
|
|
102
|
-
const screenshot = this.host.device.getScreenshot ? await this.host.device.getScreenshot() : null;
|
|
103
|
-
const imgWidth = screenshot?.width ?? 1080;
|
|
104
|
-
const imgHeight = screenshot?.height ?? 1920;
|
|
105
|
-
const prompt = `请仔细分析当前屏幕截图,判断是否存在弹窗(popup/dialog/alert/对话框/弹窗/系统弹窗/广告弹窗)。
|
|
106
|
-
|
|
107
|
-
请按以下JSON格式回答(只返回JSON,不要包含其他文字):
|
|
108
|
-
如果存在弹窗,请找出处理该弹窗需要点击的按钮的中心坐标(用0-1的归一化坐标表示,0=最左边/最上边,1=最右边/最下边):
|
|
109
|
-
{
|
|
110
|
-
"has_popup": true,
|
|
111
|
-
"popup_type": "弹窗类型(如:隐私协议确认、系统权限弹窗、广告弹窗、更新提示、确认对话框等)",
|
|
112
|
-
"action": "需要点击的按钮文本(如:同意、关闭、确定、取消、允许等)",
|
|
113
|
-
"close_x": 按钮中心的X坐标(0-1范围),
|
|
114
|
-
"close_y": 按钮中心的Y坐标(0-1范围)
|
|
115
|
-
}
|
|
116
|
-
如果不存在弹窗:
|
|
117
|
-
{
|
|
118
|
-
"has_popup": false
|
|
119
|
-
}`;
|
|
120
|
-
const responseText = await this.analyzeScreenshotWithLlm(prompt);
|
|
121
|
-
if (!responseText)
|
|
122
|
-
return null;
|
|
123
|
-
const jsonStart = responseText.indexOf('{');
|
|
124
|
-
const jsonEnd = responseText.lastIndexOf('}') + 1;
|
|
125
|
-
if (jsonStart < 0 || jsonEnd <= jsonStart) {
|
|
126
|
-
logger_1.logger.warning('[ToolPlayer] Failed to parse popup detection response');
|
|
127
|
-
return null;
|
|
128
|
-
}
|
|
129
|
-
const result = JSON.parse(responseText.slice(jsonStart, jsonEnd));
|
|
130
|
-
if (result['has_popup']) {
|
|
131
|
-
const closeX = Number(result['close_x'] ?? 0);
|
|
132
|
-
const closeY = Number(result['close_y'] ?? 0);
|
|
133
|
-
const deviceX = Math.max(0, Math.min(Math.round(closeX * imgWidth), imgWidth - 1));
|
|
134
|
-
const deviceY = Math.max(0, Math.min(Math.round(closeY * imgHeight), imgHeight - 1));
|
|
135
|
-
const action = String(result['action'] ?? '关闭');
|
|
136
|
-
logger_1.logger.warning(`[ToolPlayer] Popup detected: type=${result['popup_type']}, action=${action}, ` +
|
|
137
|
-
`normalized (${closeX}, ${closeY}) -> device (${deviceX}, ${deviceY}), device=${imgWidth}x${imgHeight}`);
|
|
138
|
-
this.host.popupStats.detected++;
|
|
139
|
-
return {
|
|
140
|
-
popupType: String(result['popup_type'] ?? 'unknown'),
|
|
141
|
-
action,
|
|
142
|
-
deviceX,
|
|
143
|
-
deviceY,
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
logger_1.logger.info('[ToolPlayer] No popup detected on screen');
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
catch (e) {
|
|
150
|
-
logger_1.logger.warning(`[ToolPlayer] isPopupStillExists failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
async handleUnexpectedPopup(failedStepIndex) {
|
|
155
|
-
if (!this.host.popupConfig.enabled) {
|
|
156
|
-
logger_1.logger.info('[ToolPlayer] Popup handling disabled, skipping');
|
|
157
|
-
return false;
|
|
158
|
-
}
|
|
159
|
-
const maxAttempts = this.host.popupConfig.maxRetryAttempts;
|
|
160
|
-
logger_1.logger.warning(`[ToolPlayer] Checking for unexpected popup (step ${failedStepIndex}, max_attempts=${maxAttempts})`);
|
|
161
|
-
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
162
|
-
const popupInfo = await this.isPopupStillExists();
|
|
163
|
-
if (!popupInfo) {
|
|
164
|
-
logger_1.logger.info('[ToolPlayer] No popup detected, cannot handle');
|
|
165
|
-
return false;
|
|
166
|
-
}
|
|
167
|
-
logger_1.logger.warning(`[ToolPlayer] Attempt ${attempt + 1}/${maxAttempts}: Handling popup - ` +
|
|
168
|
-
`type=${popupInfo.popupType}, action=${popupInfo.action}, device at (${popupInfo.deviceX}, ${popupInfo.deviceY})`);
|
|
169
|
-
try {
|
|
170
|
-
if (popupInfo.deviceX > 0 || popupInfo.deviceY > 0) {
|
|
171
|
-
await this.host.device.callTool('click', { x: popupInfo.deviceX, y: popupInfo.deviceY });
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
logger_1.logger.warning('[ToolPlayer] No close coordinates from LLM, falling back to go_back');
|
|
175
|
-
await this.host.device.callTool('go_back', {});
|
|
176
|
-
}
|
|
177
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
178
|
-
const verifyResult = await this.isPopupStillExists();
|
|
179
|
-
if (!verifyResult) {
|
|
180
|
-
logger_1.logger.warning(`[ToolPlayer] Popup closed successfully on attempt ${attempt + 1}`);
|
|
181
|
-
this.host.popupStats.closed++;
|
|
182
|
-
const retrySuccess = await this.retryFailedStep(failedStepIndex);
|
|
183
|
-
if (retrySuccess) {
|
|
184
|
-
this.host.popupStats.retrySuccess++;
|
|
185
|
-
logger_1.logger.warning('[ToolPlayer] Retry succeeded after popup handling');
|
|
186
|
-
return true;
|
|
187
|
-
}
|
|
188
|
-
this.host.popupStats.retryFailed++;
|
|
189
|
-
logger_1.logger.warning('[ToolPlayer] Retry failed after popup handling');
|
|
190
|
-
return false;
|
|
191
|
-
}
|
|
192
|
-
logger_1.logger.warning(`[ToolPlayer] Popup still exists after attempt ${attempt + 1}, retrying...`);
|
|
193
|
-
}
|
|
194
|
-
catch (e) {
|
|
195
|
-
logger_1.logger.error(`[ToolPlayer] Failed to close popup on attempt ${attempt + 1}: ${e instanceof Error ? e.message : String(e)}`);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
logger_1.logger.warning(`[ToolPlayer] Failed to handle popup after ${maxAttempts} attempts`);
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
async tryResolveStepFailure(failedStepIndex, recording, arguments_) {
|
|
202
|
-
if (!this.host.popupConfig.enabled)
|
|
203
|
-
return 'failed';
|
|
204
|
-
if (this.host.popupConfig.skipPopupSteps) {
|
|
205
|
-
const analysis = await this.analyzeStepAndScreen(arguments_);
|
|
206
|
-
if (analysis.isPopupOperation && (!analysis.hasPopup || (analysis.deviceX === 0 && analysis.deviceY === 0))) {
|
|
207
|
-
await this.skipPopupStep(recording, failedStepIndex);
|
|
208
|
-
return 'skip';
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
const handled = await this.handleUnexpectedPopup(failedStepIndex);
|
|
212
|
-
if (handled)
|
|
213
|
-
return 'retry_success';
|
|
214
|
-
return 'failed';
|
|
215
|
-
}
|
|
216
|
-
async analyzeStepAndScreen(arguments_) {
|
|
217
|
-
const empty = { isPopupOperation: false, hasPopup: false, deviceX: 0, deviceY: 0, popupType: '' };
|
|
218
|
-
try {
|
|
219
|
-
const messageText = arguments_['message'] ?? String(arguments_);
|
|
220
|
-
const screenshot = this.host.device.getScreenshot ? await this.host.device.getScreenshot() : null;
|
|
221
|
-
const imgWidth = screenshot?.width ?? 1080;
|
|
222
|
-
const imgHeight = screenshot?.height ?? 1920;
|
|
223
|
-
const prompt = `请分析以下操作步骤描述和当前屏幕截图,回答两个问题。
|
|
224
|
-
|
|
225
|
-
问题1:该操作步骤是否与"关闭弹窗/对话框/弹窗广告/系统弹窗/权限申请弹窗"相关?
|
|
226
|
-
操作描述:${messageText}
|
|
227
|
-
|
|
228
|
-
问题2:当前屏幕截图是否存在弹窗(对话框、弹窗广告、系统权限请求、提示框等)?
|
|
229
|
-
|
|
230
|
-
请按以下JSON格式回答(只返回JSON,不要包含其他文字):
|
|
231
|
-
{
|
|
232
|
-
"is_popup_operation": true或false,
|
|
233
|
-
"has_popup": true或false,
|
|
234
|
-
"popup_type": "如果存在弹窗,填写弹窗类型;否则填空字符串",
|
|
235
|
-
"close_x": 如果存在弹窗,填写处理该弹窗需要点击的按钮的X坐标(0-1归一化);否则填0,
|
|
236
|
-
"close_y": 如果存在弹窗,填写处理该弹窗需要点击的按钮的Y坐标(0-1归一化);否则填0
|
|
237
|
-
}`;
|
|
238
|
-
const responseText = await this.analyzeScreenshotWithLlm(prompt);
|
|
239
|
-
if (!responseText)
|
|
240
|
-
return empty;
|
|
241
|
-
const jsonStart = responseText.indexOf('{');
|
|
242
|
-
const jsonEnd = responseText.lastIndexOf('}') + 1;
|
|
243
|
-
if (jsonStart < 0 || jsonEnd <= jsonStart)
|
|
244
|
-
return empty;
|
|
245
|
-
const result = JSON.parse(responseText.slice(jsonStart, jsonEnd));
|
|
246
|
-
const closeX = Number(result['close_x'] ?? 0);
|
|
247
|
-
const closeY = Number(result['close_y'] ?? 0);
|
|
248
|
-
const deviceX = closeX ? Math.max(0, Math.min(Math.round(closeX * imgWidth), imgWidth - 1)) : 0;
|
|
249
|
-
const deviceY = closeY ? Math.max(0, Math.min(Math.round(closeY * imgHeight), imgHeight - 1)) : 0;
|
|
250
|
-
logger_1.logger.info(`[ToolPlayer] Step analysis: is_popup=${result['is_popup_operation']}, ` +
|
|
251
|
-
`has_popup=${result['has_popup']}, popup_type=${result['popup_type']}, device=(${deviceX}, ${deviceY})`);
|
|
252
|
-
return {
|
|
253
|
-
isPopupOperation: Boolean(result['is_popup_operation']),
|
|
254
|
-
hasPopup: Boolean(result['has_popup']),
|
|
255
|
-
deviceX,
|
|
256
|
-
deviceY,
|
|
257
|
-
popupType: String(result['popup_type'] ?? ''),
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
catch (e) {
|
|
261
|
-
logger_1.logger.warning(`[ToolPlayer] analyzeStepAndScreen failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
262
|
-
return empty;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
async skipPopupStep(recording, stepIndex) {
|
|
266
|
-
const args = (recording['arguments'] ?? {});
|
|
267
|
-
const messageText = args['message'] ?? String(recording['arguments'] ?? '');
|
|
268
|
-
logger_1.logger.info(`[ToolPlayer] Skipping popup step ${stepIndex}: LLM confirmed popup no longer exists (message: ${messageText.slice(0, 80)})`);
|
|
269
|
-
this.host.popupStats.skipped++;
|
|
270
|
-
recording['is_played'] = true;
|
|
271
|
-
recording['is_skipped'] = true;
|
|
272
|
-
recording['result'] = '[SKIPPED] Popup no longer exists, step skipped';
|
|
273
|
-
return true;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
exports.PopupMixin = PopupMixin;
|