@autotest/agent 0.0.6 → 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 +7 -0
- package/dist/api/agent_api.d.ts +2 -2
- package/dist/autotest-cli.js +1 -397
- package/dist/common/config/config.yaml +4 -0
- package/dist/common/config/manager.d.ts +4 -4
- package/dist/common/config/types.d.ts +5 -0
- package/dist/common/dfx/index.d.ts +1 -1
- package/dist/core/agent/single_agent/agent.d.ts +1 -0
- package/dist/core/device/device_protocol.d.ts +1 -1
- package/dist/core/knowledge/knowledge_manager.d.ts +3 -3
- package/dist/core/knowledge/vectordb.d.ts +5 -5
- package/dist/core/mcp/harmony_hdc_adapter.d.ts +1 -0
- package/dist/core/mcp/mcp_tools.d.ts +1 -0
- package/dist/core/mcp/verify_tool.d.ts +12 -0
- package/dist/core/prompt/templates/single/single_v2.0.md +15 -0
- package/dist/data_types/device.d.ts +1 -1
- package/dist/data_types/index.d.ts +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -163
- package/dist/tools/batch_runner.js +1 -745
- package/dist/tools/case_runner/runner.js +1 -410
- package/dist/tools/config/config_tool.js +1 -339
- 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 -185
- 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 -487
- 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 -82
- package/dist/core/agent/layered_agent/decision.js +0 -518
- 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 -801
- package/dist/core/agent/single_agent/hook_manager.js +0 -59
- package/dist/core/agent/single_agent/hooks.js +0 -288
- package/dist/core/agent/single_agent/image_utils.js +0 -158
- package/dist/core/agent/single_agent/scroll_search_agent.js +0 -405
- package/dist/core/agent/single_agent/subagent_manager.js +0 -72
- 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 -798
- 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 -473
- 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 -529
- 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,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.Parser = exports.runAsyncFunc = exports.sleepSync = exports.sleep = exports.asyncWaitIfNotExceed = exports.waitIfNotExceed = exports.getMessage = exports.getMessages = exports.MCP_SYSTEM_PROMPT_ALL = exports.MCP_SYSTEM_PROMPT_BASIC = exports.MCP_SYSTEM_PROMPT = exports.SYSTEM_PROMPT = exports.SimpleReporter = exports.ActionHandler = exports.MaiUiAgent = exports.GLMAgent = exports.GeneralAgent = exports.createAgent = exports.AgentFactory = exports.BaseAgent = void 0;
|
|
37
|
-
var base_agent_1 = require("./base_agent");
|
|
38
|
-
Object.defineProperty(exports, "BaseAgent", { enumerable: true, get: function () { return base_agent_1.BaseAgent; } });
|
|
39
|
-
var agent_factory_1 = require("./agent_factory");
|
|
40
|
-
Object.defineProperty(exports, "AgentFactory", { enumerable: true, get: function () { return agent_factory_1.AgentFactory; } });
|
|
41
|
-
Object.defineProperty(exports, "createAgent", { enumerable: true, get: function () { return agent_factory_1.createAgent; } });
|
|
42
|
-
var general_agent_1 = require("./general_agent");
|
|
43
|
-
Object.defineProperty(exports, "GeneralAgent", { enumerable: true, get: function () { return general_agent_1.GeneralAgent; } });
|
|
44
|
-
var glm_agent_1 = require("./glm_agent");
|
|
45
|
-
Object.defineProperty(exports, "GLMAgent", { enumerable: true, get: function () { return glm_agent_1.GLMAgent; } });
|
|
46
|
-
var mai_ui_agent_1 = require("./mai_ui_agent");
|
|
47
|
-
Object.defineProperty(exports, "MaiUiAgent", { enumerable: true, get: function () { return mai_ui_agent_1.MaiUiAgent; } });
|
|
48
|
-
var action_handler_1 = require("./action_handler");
|
|
49
|
-
Object.defineProperty(exports, "ActionHandler", { enumerable: true, get: function () { return action_handler_1.ActionHandler; } });
|
|
50
|
-
var simple_reporter_1 = require("./simple_reporter");
|
|
51
|
-
Object.defineProperty(exports, "SimpleReporter", { enumerable: true, get: function () { return simple_reporter_1.SimpleReporter; } });
|
|
52
|
-
var prompts_1 = require("./prompts");
|
|
53
|
-
Object.defineProperty(exports, "SYSTEM_PROMPT", { enumerable: true, get: function () { return prompts_1.SYSTEM_PROMPT; } });
|
|
54
|
-
var mcp_prompts_1 = require("./mcp_prompts");
|
|
55
|
-
Object.defineProperty(exports, "MCP_SYSTEM_PROMPT", { enumerable: true, get: function () { return mcp_prompts_1.MCP_SYSTEM_PROMPT; } });
|
|
56
|
-
Object.defineProperty(exports, "MCP_SYSTEM_PROMPT_BASIC", { enumerable: true, get: function () { return mcp_prompts_1.MCP_SYSTEM_PROMPT_BASIC; } });
|
|
57
|
-
Object.defineProperty(exports, "MCP_SYSTEM_PROMPT_ALL", { enumerable: true, get: function () { return mcp_prompts_1.MCP_SYSTEM_PROMPT_ALL; } });
|
|
58
|
-
var i18n_1 = require("./i18n");
|
|
59
|
-
Object.defineProperty(exports, "getMessages", { enumerable: true, get: function () { return i18n_1.getMessages; } });
|
|
60
|
-
Object.defineProperty(exports, "getMessage", { enumerable: true, get: function () { return i18n_1.getMessage; } });
|
|
61
|
-
var utils_1 = require("./utils");
|
|
62
|
-
Object.defineProperty(exports, "waitIfNotExceed", { enumerable: true, get: function () { return utils_1.waitIfNotExceed; } });
|
|
63
|
-
Object.defineProperty(exports, "asyncWaitIfNotExceed", { enumerable: true, get: function () { return utils_1.asyncWaitIfNotExceed; } });
|
|
64
|
-
Object.defineProperty(exports, "sleep", { enumerable: true, get: function () { return utils_1.sleep; } });
|
|
65
|
-
Object.defineProperty(exports, "sleepSync", { enumerable: true, get: function () { return utils_1.sleepSync; } });
|
|
66
|
-
Object.defineProperty(exports, "runAsyncFunc", { enumerable: true, get: function () { return utils_1.runAsyncFunc; } });
|
|
67
|
-
exports.Parser = __importStar(require("./parser"));
|
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MaiUiAgent = void 0;
|
|
4
|
-
const types_1 = require("../../../../reporter/types");
|
|
5
|
-
const logger_1 = require("../../../../common/logger");
|
|
6
|
-
const model_1 = require("../../../../common/model");
|
|
7
|
-
const cache_transport_1 = require("../../../../common/llm_cache/cache_transport");
|
|
8
|
-
const message_builder_1 = require("../../utils/message_builder");
|
|
9
|
-
const base_agent_1 = require("./base_agent");
|
|
10
|
-
const action_handler_1 = require("./action_handler");
|
|
11
|
-
const parser_1 = require("./parser");
|
|
12
|
-
const prompts_1 = require("./prompts");
|
|
13
|
-
const mcp_prompts_1 = require("./mcp_prompts");
|
|
14
|
-
const simple_reporter_1 = require("./simple_reporter");
|
|
15
|
-
const i18n_1 = require("./i18n");
|
|
16
|
-
const utils_1 = require("./utils");
|
|
17
|
-
function mkEvent(eventType, content, opts) {
|
|
18
|
-
return {
|
|
19
|
-
eventType,
|
|
20
|
-
timestamp: new Date().toISOString(),
|
|
21
|
-
content,
|
|
22
|
-
data: opts?.data ?? {},
|
|
23
|
-
screenshotPath: opts?.screenshotPath ?? null,
|
|
24
|
-
stepNumber: opts?.stepNumber ?? null,
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
function getSystemPrompt(lang = 'en', mode) {
|
|
28
|
-
if (mode === 'mcp') {
|
|
29
|
-
return mcp_prompts_1.MCP_SYSTEM_PROMPT;
|
|
30
|
-
}
|
|
31
|
-
return prompts_1.SYSTEM_PROMPT;
|
|
32
|
-
}
|
|
33
|
-
class MaiUiAgent extends base_agent_1.BaseAgent {
|
|
34
|
-
llmCaller;
|
|
35
|
-
actionHandler;
|
|
36
|
-
parser;
|
|
37
|
-
_context = [];
|
|
38
|
-
_screenshot = [];
|
|
39
|
-
_stepCount = 0;
|
|
40
|
-
_isRunning = false;
|
|
41
|
-
constructor(opts) {
|
|
42
|
-
const reportGenerator = opts.reportGenerator ?? new simple_reporter_1.SimpleReporter();
|
|
43
|
-
super({ ...opts, reportGenerator });
|
|
44
|
-
this.llmCaller = opts.llmCaller ?? new model_1.LLMCaller(new cache_transport_1.CacheTransport(this.agentConfig.enableCache));
|
|
45
|
-
this.actionHandler = new action_handler_1.ActionHandler(this.device, opts.confirmationCallback, opts.takeoverCallback);
|
|
46
|
-
this.parser = new parser_1.MaiUiParser();
|
|
47
|
-
}
|
|
48
|
-
async run(task) {
|
|
49
|
-
this.resetExecutionState();
|
|
50
|
-
try {
|
|
51
|
-
let result = await this.executeStep(task, true);
|
|
52
|
-
if (result.finished) {
|
|
53
|
-
return result.message ?? 'Task completed';
|
|
54
|
-
}
|
|
55
|
-
if (result.success) {
|
|
56
|
-
return 'step success';
|
|
57
|
-
}
|
|
58
|
-
while (this.shouldContinue()) {
|
|
59
|
-
result = await this.executeStep(undefined, false);
|
|
60
|
-
if (result.finished) {
|
|
61
|
-
return result.message ?? 'Task completed';
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return 'Max steps reached';
|
|
65
|
-
}
|
|
66
|
-
catch (e) {
|
|
67
|
-
logger_1.logger.error(`${e instanceof Error ? e.message : String(e)}`);
|
|
68
|
-
return `Error: ${e instanceof Error ? e.message : String(e)}`;
|
|
69
|
-
}
|
|
70
|
-
finally {
|
|
71
|
-
this._isRunning = false;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
resetExecutionState() {
|
|
75
|
-
this._context = [];
|
|
76
|
-
this._stepCount = 0;
|
|
77
|
-
this._isRunning = true;
|
|
78
|
-
}
|
|
79
|
-
shouldContinue() {
|
|
80
|
-
return this._stepCount < this.agentConfig.maxSteps && this._isRunning;
|
|
81
|
-
}
|
|
82
|
-
step(task) {
|
|
83
|
-
const isFirst = this._context.length === 0;
|
|
84
|
-
if (isFirst && !task) {
|
|
85
|
-
throw new Error('Task is required for the first step');
|
|
86
|
-
}
|
|
87
|
-
return this.executeStep(task, isFirst);
|
|
88
|
-
}
|
|
89
|
-
reset() {
|
|
90
|
-
this._context = [];
|
|
91
|
-
this._screenshot = [];
|
|
92
|
-
this._stepCount = 0;
|
|
93
|
-
this._isRunning = false;
|
|
94
|
-
}
|
|
95
|
-
abort() {
|
|
96
|
-
this._isRunning = false;
|
|
97
|
-
logger_1.logger.info('Agent aborted by user');
|
|
98
|
-
}
|
|
99
|
-
parseOutput(content) {
|
|
100
|
-
if (this.agentConfig.jsonOutput) {
|
|
101
|
-
return JSON.stringify({ tag: 'Thinking', message: content });
|
|
102
|
-
}
|
|
103
|
-
return content;
|
|
104
|
-
}
|
|
105
|
-
async getToolsListForLlm() {
|
|
106
|
-
const mcpTools = await this.device.listTools();
|
|
107
|
-
const openaiTools = mcpTools.map((t) => ({
|
|
108
|
-
type: 'function',
|
|
109
|
-
function: {
|
|
110
|
-
name: t.name,
|
|
111
|
-
description: t.description,
|
|
112
|
-
parameters: t.inputSchema,
|
|
113
|
-
},
|
|
114
|
-
}));
|
|
115
|
-
return openaiTools.map((item) => JSON.stringify(item)).join('\n');
|
|
116
|
-
}
|
|
117
|
-
async captureStepState() {
|
|
118
|
-
const screenshot = await this.device.getScreenshot(undefined, true);
|
|
119
|
-
this._screenshot.push(screenshot.screenshotPath ?? '');
|
|
120
|
-
const currentApp = await this.device.getCurrentApp();
|
|
121
|
-
return { screenshot, currentApp };
|
|
122
|
-
}
|
|
123
|
-
prepareFirstStepMessages(userPrompt, currentApp, screenshot, mcpToolStr) {
|
|
124
|
-
let systemPrompt = this.agentConfig.systemPrompt;
|
|
125
|
-
if (!systemPrompt) {
|
|
126
|
-
systemPrompt = getSystemPrompt(this.agentConfig.lang, this.agentConfig.mode);
|
|
127
|
-
}
|
|
128
|
-
systemPrompt += mcpToolStr;
|
|
129
|
-
this._context.push(message_builder_1.MessageBuilder.createSystemMessage(systemPrompt));
|
|
130
|
-
const screenInfo = message_builder_1.MessageBuilder.buildScreenInfo(currentApp);
|
|
131
|
-
const textContent = `${userPrompt ?? ''}\n\n${screenInfo}`;
|
|
132
|
-
this._context.push(message_builder_1.MessageBuilder.createUserMessage(textContent, screenshot.base64Data));
|
|
133
|
-
}
|
|
134
|
-
prepareContinuationMessages(userPrompt, currentApp, screenshot) {
|
|
135
|
-
const screenInfo = message_builder_1.MessageBuilder.buildScreenInfo(currentApp);
|
|
136
|
-
let textContent;
|
|
137
|
-
if (userPrompt) {
|
|
138
|
-
textContent = `${userPrompt}\n\n** Screen Info **\n\n${screenInfo}`;
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
textContent = `** Screen Info **\n\n${screenInfo}`;
|
|
142
|
-
}
|
|
143
|
-
this._context.push(message_builder_1.MessageBuilder.createUserMessage(textContent, screenshot.base64Data));
|
|
144
|
-
}
|
|
145
|
-
async executeLlmRequest() {
|
|
146
|
-
const msgs = this.agentConfig.lang ? (0, i18n_1.getMessages)(this.agentConfig.lang) : null;
|
|
147
|
-
if (this.agentConfig.verbose && msgs) {
|
|
148
|
-
logger_1.logger.info(`${msgs.thinking}:`);
|
|
149
|
-
}
|
|
150
|
-
const rawContent = await this.streamRequest(this._context);
|
|
151
|
-
if (this.agentConfig.streamLog) {
|
|
152
|
-
process.stdout.write('\n');
|
|
153
|
-
}
|
|
154
|
-
if (!rawContent) {
|
|
155
|
-
logger_1.logger.info('No content output');
|
|
156
|
-
}
|
|
157
|
-
return rawContent;
|
|
158
|
-
}
|
|
159
|
-
parseAction(llmOutput) {
|
|
160
|
-
let funcName = 'finish';
|
|
161
|
-
let funcArgs = { message: 'No tool call triggered' };
|
|
162
|
-
try {
|
|
163
|
-
const parsed = this.parser.parse(llmOutput, 0, 0);
|
|
164
|
-
funcName = parsed.name;
|
|
165
|
-
funcArgs = parsed.arguments;
|
|
166
|
-
const action = { action: funcName };
|
|
167
|
-
Object.assign(action, funcArgs);
|
|
168
|
-
if (funcName === 'answer' ||
|
|
169
|
-
funcName === 'ask_user' ||
|
|
170
|
-
funcName === 'terminate' ||
|
|
171
|
-
funcName.includes('finish')) {
|
|
172
|
-
action._metadata = 'finish';
|
|
173
|
-
}
|
|
174
|
-
return { action, funcName, funcArgs };
|
|
175
|
-
}
|
|
176
|
-
catch (e) {
|
|
177
|
-
if (this.agentConfig.verbose) {
|
|
178
|
-
logger_1.logger.warn(`Failed to parse action: ${e instanceof Error ? e.message : String(e)}, treating as finish`);
|
|
179
|
-
}
|
|
180
|
-
return { action: { _metadata: 'finish', message: llmOutput }, funcName, funcArgs };
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
logThinkingAndAction(llmOutput, funcName, funcArgs) {
|
|
184
|
-
const msgs = (0, i18n_1.getMessages)(this.agentConfig.lang);
|
|
185
|
-
if (this.agentConfig.verbose) {
|
|
186
|
-
logger_1.logger.info(`${msgs.action}: ${funcName} ${JSON.stringify(funcArgs)}`);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
reportThinkingAndAction(thinking, action, screenshot) {
|
|
190
|
-
if (!this.reportGenerator) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
this.reportGenerator.addEvent(mkEvent(types_1.EventType.GLM_THINKING, thinking, {
|
|
194
|
-
stepNumber: this._stepCount,
|
|
195
|
-
screenshotPath: screenshot.screenshotPath,
|
|
196
|
-
}));
|
|
197
|
-
const funcName = action.action ?? '';
|
|
198
|
-
action.coor_mode = 'relative';
|
|
199
|
-
const eventData = {
|
|
200
|
-
action,
|
|
201
|
-
screen_width: screenshot.width,
|
|
202
|
-
screen_height: screenshot.height,
|
|
203
|
-
};
|
|
204
|
-
const args = action.args;
|
|
205
|
-
if (args) {
|
|
206
|
-
Object.assign(eventData, args);
|
|
207
|
-
}
|
|
208
|
-
this.reportGenerator.addEvent(mkEvent(types_1.EventType.GLM_ACTION, `Action: ${funcName}`, {
|
|
209
|
-
data: eventData,
|
|
210
|
-
screenshotPath: screenshot.screenshotPath,
|
|
211
|
-
stepNumber: this._stepCount,
|
|
212
|
-
}));
|
|
213
|
-
const lastIndex = this._context.length - 1;
|
|
214
|
-
if (lastIndex >= 0) {
|
|
215
|
-
this._context[lastIndex] = message_builder_1.MessageBuilder.removeImagesFromMessage(this._context[lastIndex]);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
async executeAction(action, funcName, funcArgs) {
|
|
219
|
-
if (action._metadata === 'finish') {
|
|
220
|
-
return { success: true, shouldFinish: true, message: funcArgs.message };
|
|
221
|
-
}
|
|
222
|
-
try {
|
|
223
|
-
const result = await this.device.callTool(funcName, funcArgs);
|
|
224
|
-
if (result.output) {
|
|
225
|
-
this._context.push({ role: 'tool', content: result.output });
|
|
226
|
-
}
|
|
227
|
-
return { success: result.success, shouldFinish: false, message: result.output };
|
|
228
|
-
}
|
|
229
|
-
catch (e) {
|
|
230
|
-
logger_1.logger.error(`${e instanceof Error ? e.message : String(e)}`);
|
|
231
|
-
return { success: false, shouldFinish: true, message: e instanceof Error ? e.message : String(e) };
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
async reportStepEnd(result) {
|
|
235
|
-
if (!this.reportGenerator) {
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
const postScreenshot = await this.device.getScreenshot(undefined, true);
|
|
239
|
-
this.reportGenerator.addEvent(mkEvent(types_1.EventType.GLM_STEP_END, `Step ${this._stepCount} completed`, {
|
|
240
|
-
data: { result: result.message, success: result.success },
|
|
241
|
-
screenshotPath: postScreenshot.screenshotPath,
|
|
242
|
-
stepNumber: this._stepCount,
|
|
243
|
-
}));
|
|
244
|
-
}
|
|
245
|
-
appendAssistantResponse(llmOutput) {
|
|
246
|
-
this._context.push(message_builder_1.MessageBuilder.createAssistantMessage(llmOutput));
|
|
247
|
-
}
|
|
248
|
-
isActionFinished(action, result) {
|
|
249
|
-
return action._metadata === 'finish' || result.shouldFinish;
|
|
250
|
-
}
|
|
251
|
-
reportStepStart() {
|
|
252
|
-
if (!this.reportGenerator) {
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
this.reportGenerator.addEvent(mkEvent(types_1.EventType.GLM_STEP_START, `Step ${this._stepCount} started`, { stepNumber: this._stepCount }));
|
|
256
|
-
}
|
|
257
|
-
async executeStep(userPrompt, isFirst = false) {
|
|
258
|
-
this._stepCount += 1;
|
|
259
|
-
const mcpToolStr = await this.getToolsListForLlm();
|
|
260
|
-
this.reportStepStart();
|
|
261
|
-
const { screenshot, currentApp } = await this.captureStepState();
|
|
262
|
-
if (isFirst) {
|
|
263
|
-
this.prepareFirstStepMessages(userPrompt, currentApp, screenshot, mcpToolStr);
|
|
264
|
-
}
|
|
265
|
-
else {
|
|
266
|
-
this.prepareContinuationMessages(userPrompt, currentApp, screenshot);
|
|
267
|
-
}
|
|
268
|
-
const llmOutput = await this.executeLlmRequest();
|
|
269
|
-
const { action, funcName, funcArgs } = this.parseAction(llmOutput);
|
|
270
|
-
this.logThinkingAndAction(llmOutput, funcName, funcArgs);
|
|
271
|
-
this.reportThinkingAndAction(llmOutput, action, screenshot);
|
|
272
|
-
const result = await this.executeAction(action, funcName, funcArgs);
|
|
273
|
-
await this.reportStepEnd(result);
|
|
274
|
-
const actionEndTimestamp = Date.now() / 1000;
|
|
275
|
-
this.appendAssistantResponse(llmOutput);
|
|
276
|
-
const finished = this.isActionFinished(action, result);
|
|
277
|
-
if (finished) {
|
|
278
|
-
logger_1.logger.info('Task finished, screenshot cached for planner');
|
|
279
|
-
}
|
|
280
|
-
await (0, utils_1.asyncWaitIfNotExceed)(this.agentConfig.actionInterval, actionEndTimestamp);
|
|
281
|
-
return this.makeStepResult(result.success, finished, action, llmOutput, result.message ?? action.message);
|
|
282
|
-
}
|
|
283
|
-
async streamRequest(messages) {
|
|
284
|
-
const llmResult = await this.llmCaller.callWithMessages({
|
|
285
|
-
messages: (0, message_builder_1.toModelMessages)(messages),
|
|
286
|
-
modelConfig: this.modelConfig,
|
|
287
|
-
});
|
|
288
|
-
if (llmResult.tokenUsage && this.reportGenerator) {
|
|
289
|
-
this.reportGenerator.addTokenUsage(llmResult.tokenUsage, 'Executor(MaiUi)', this._stepCount);
|
|
290
|
-
}
|
|
291
|
-
const rawContent = llmResult.text;
|
|
292
|
-
if (this.agentConfig.streamLog && rawContent) {
|
|
293
|
-
const lines = rawContent.split('\n');
|
|
294
|
-
for (const line of lines) {
|
|
295
|
-
process.stdout.write(this.parseOutput(line.replace(/^```|```$/g, '')) + '\n');
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return rawContent;
|
|
299
|
-
}
|
|
300
|
-
get context() {
|
|
301
|
-
return this._context.map((m) => m);
|
|
302
|
-
}
|
|
303
|
-
get stepCount() {
|
|
304
|
-
return this._stepCount;
|
|
305
|
-
}
|
|
306
|
-
get screenshot() {
|
|
307
|
-
return [...this._screenshot];
|
|
308
|
-
}
|
|
309
|
-
get isRunning() {
|
|
310
|
-
return this._isRunning;
|
|
311
|
-
}
|
|
312
|
-
async cleanUp() {
|
|
313
|
-
await this.device.cleanUp();
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
exports.MaiUiAgent = MaiUiAgent;
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MCP_SYSTEM_PROMPT = exports.MCP_SYSTEM_PROMPT_ALL = exports.MCP_SYSTEM_PROMPT_BASIC = void 0;
|
|
4
|
-
const WEEKDAY_NAMES = [
|
|
5
|
-
'Monday',
|
|
6
|
-
'Tuesday',
|
|
7
|
-
'Wednesday',
|
|
8
|
-
'Thursday',
|
|
9
|
-
'Friday',
|
|
10
|
-
'Saturday',
|
|
11
|
-
'Sunday',
|
|
12
|
-
];
|
|
13
|
-
function getFormattedDate() {
|
|
14
|
-
const today = new Date();
|
|
15
|
-
const weekday = WEEKDAY_NAMES[today.getDay()];
|
|
16
|
-
return `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')} ${weekday}`;
|
|
17
|
-
}
|
|
18
|
-
exports.MCP_SYSTEM_PROMPT_BASIC = `You are a GUI operation agent. You will receive a task description, an action history, a screen screenshot, and a set of tools used for device operations. You need to decide the next action to take to complete the task based on the current interface state. Please operate based on the current interface and the given task, do not speculate about user intent.
|
|
19
|
-
|
|
20
|
-
# Output Format Requirements
|
|
21
|
-
- Must include a thinking section that first summarizes the history of operations, especially repeatedly retried operations, the decision reason for the current operation step, and the operation description.
|
|
22
|
-
- The thinking section should summarize in one sentence.
|
|
23
|
-
|
|
24
|
-
# Notes
|
|
25
|
-
## Decide based on screenshot and history
|
|
26
|
-
- Please analyze the current interface content based on the provided screenshot and user instruction, and select the most appropriate operation from the tools. Your judgment must be based on visual recognition of the controls on the interface.
|
|
27
|
-
## Retry principle
|
|
28
|
-
- Please pay attention to the operation history; the same operation sequence should be retried at most 2 times. If the operation does not achieve the expected result, terminate execution directly and report the failure reason.
|
|
29
|
-
## Execution conditions
|
|
30
|
-
- Please confirm whether the execution conditions are met before execution. If the execution conditions are not met, terminate execution directly and report the failure reason.
|
|
31
|
-
## Target description
|
|
32
|
-
- Please confirm the operations required by the target description in the task list, and only complete the operations specified in the target description.
|
|
33
|
-
## Expected result
|
|
34
|
-
- Please confirm the expected result before execution. If the expected result has been achieved, report completion directly.
|
|
35
|
-
## Swipe description
|
|
36
|
-
- Swipe from left to right -> right
|
|
37
|
-
- Swipe from top to bottom -> down
|
|
38
|
-
- Swipe from bottom to top -> up
|
|
39
|
-
- Swipe from right to left -> left
|
|
40
|
-
## Coordinate description
|
|
41
|
-
- The coordinate format is (x, y), the coordinate range is (0.000, 1.000), do not exceed the coordinate range.
|
|
42
|
-
|
|
43
|
-
## Supported MCP Tools
|
|
44
|
-
`;
|
|
45
|
-
exports.MCP_SYSTEM_PROMPT_ALL = `You are the execution-layer Agent, responsible for strictly executing the operation instructions of the superior (decision layer).
|
|
46
|
-
|
|
47
|
-
## Core Positioning (Highest Priority)
|
|
48
|
-
|
|
49
|
-
You are the execution layer, not the decision layer.
|
|
50
|
-
- Your responsibility: execute the instructions of the superior, do not judge whether the instruction is meaningful.
|
|
51
|
-
- Your superior (Planner): responsible for decisions, judging whether the task is complete.
|
|
52
|
-
- Your role: tool executor, not task decision-maker.
|
|
53
|
-
|
|
54
|
-
---
|
|
55
|
-
|
|
56
|
-
## Mandatory Execution Rules (Must Follow)
|
|
57
|
-
|
|
58
|
-
Must execute:
|
|
59
|
-
- Receive an instruction from the superior then must execute, do not judge whether it is meaningful.
|
|
60
|
-
- The superior asks to swipe then must swipe, do not judge whether there is content.
|
|
61
|
-
- If the superior specifies a swipe area, must perform the swipe in that area, do not change the area on your own.
|
|
62
|
-
- The superior asks to click then must click, do not judge whether it is correct.
|
|
63
|
-
- System operations such as entering the notification center, control center, recent tasks, and rotating the screen should prioritize using tools that directly match the task description.
|
|
64
|
-
|
|
65
|
-
Forbidden autonomous behaviors:
|
|
66
|
-
- Forbidden to autonomously terminate (task completion is judged by the superior).
|
|
67
|
-
- Forbidden to autonomously go back (going back is decided by the superior).
|
|
68
|
-
- Forbidden to autonomously explore (exploration is planned by the superior).
|
|
69
|
-
- Forbidden to judge whether an operation is meaningful.
|
|
70
|
-
- Forbidden to think about whether you should go back to the previous level.
|
|
71
|
-
- Forbidden to think about whether the task is complete.
|
|
72
|
-
|
|
73
|
-
## Output Format
|
|
74
|
-
Each operation must contain two parts:
|
|
75
|
-
1. Thinking process: explain your analysis and decision in detail inside thinking tags.
|
|
76
|
-
2. Action instruction: return a JSON function call inside a json code fence.
|
|
77
|
-
|
|
78
|
-
# Format Requirements
|
|
79
|
-
Example:
|
|
80
|
-
|
|
81
|
-
${'<thinking>\n' +
|
|
82
|
-
'**Observation**: The current screen shows the Meituan home page, with address, search bar, scan icon, menu bar, image list, etc.\n' +
|
|
83
|
-
'**Analysis**: I need to click the top search box and enter "Bawang Chaji".\n' +
|
|
84
|
-
'**Usability check**: The page elements are available, without any occlusion.\n' +
|
|
85
|
-
'**Decision**: The search box is located in the upper center of the screen, the coordinate is about [0.5, 0.1].\n' +
|
|
86
|
-
'**Summary**: Next operation: click the search box.\n' +
|
|
87
|
-
'</thinking>\n' +
|
|
88
|
-
'```json\n' +
|
|
89
|
-
'{"name": "tap", "arguments": {"x": 0.5, "y": 0.1}}\n' +
|
|
90
|
-
'```'}
|
|
91
|
-
|
|
92
|
-
# Notes
|
|
93
|
-
## Decide based on screenshot and history
|
|
94
|
-
- Please analyze the current interface content based on the provided screenshot and user instruction, and select the most appropriate operation from the tools. Your judgment must be based on visual recognition of the controls on the interface.
|
|
95
|
-
## Retry principle
|
|
96
|
-
- Please pay attention to the operation history; the same operation sequence should be retried at most 2 times. If the operation does not achieve the expected result, terminate execution directly and report the failure reason.
|
|
97
|
-
## Execution conditions
|
|
98
|
-
- Please confirm whether the execution conditions are met before execution. If the execution conditions are not met, terminate execution directly and report the failure reason.
|
|
99
|
-
### Target availability check (Critical)
|
|
100
|
-
Core principle: visible does not mean available. Before clicking the target element, please check whether there is a popup on the page and whether the element is occluded by other UI elements.
|
|
101
|
-
- Rule: if the target is occluded or there is a popup on the page, it is forbidden to click the target directly.
|
|
102
|
-
- Processing flow:
|
|
103
|
-
1. Handle the occluding object first: click the back key, X button, allow/deny button of the popup, or swipe to close the popup.
|
|
104
|
-
2. Wait for it to take effect: after handling the occluding object, wait 0.5 seconds to confirm the target is displayed.
|
|
105
|
-
3. Then execute the original operation: after confirming the target is visible, execute the original click operation.
|
|
106
|
-
## Target description
|
|
107
|
-
- Please confirm the operations required by the target description in the task list, and only complete the operations specified in the target description.
|
|
108
|
-
## Expected result
|
|
109
|
-
- Please confirm the expected result before execution. If the expected result has been achieved, report completion directly.
|
|
110
|
-
## Swipe description
|
|
111
|
-
- Swipe from left to right -> right, the Y coordinate should remain unchanged.
|
|
112
|
-
- Swipe from top to bottom -> down, the X coordinate should remain unchanged.
|
|
113
|
-
- Swipe from bottom to top -> up, the X coordinate should remain unchanged.
|
|
114
|
-
- Swipe from right to left -> left, the Y coordinate should remain unchanged.
|
|
115
|
-
## Coordinate description
|
|
116
|
-
- The coordinate format is (x, y), the coordinate range is (0.000, 1.000), do not exceed the coordinate range.
|
|
117
|
-
## Operation guide
|
|
118
|
-
- Special characters such as quotes and newlines in text input must be escaped.
|
|
119
|
-
- Only return one action at a time, do not try to batch operations.
|
|
120
|
-
- Carefully observe the position of UI elements in the screenshot and estimate coordinates accurately.
|
|
121
|
-
|
|
122
|
-
### Thinking process suggestions
|
|
123
|
-
The thinking section should include:
|
|
124
|
-
1. Observation: observe the screen content and state based on the current screenshot, and identify the element distribution.
|
|
125
|
-
2. Analysis: identify the position and characteristics of the target element only from the current image.
|
|
126
|
-
3. Usability check: does the target element exist? Is it covered by a popup, floating window, or occlusion layer?
|
|
127
|
-
4. Localization: determine the target area and coordinate position according to the superior instruction.
|
|
128
|
-
- Understand the superior instruction: which area/element is specified by the superior instruction.
|
|
129
|
-
- Locate the target area: find the area/element specified by the superior in the screenshot.
|
|
130
|
-
- Locate within that area: the start and end points should be within that area.
|
|
131
|
-
- Swipe localization examples:
|
|
132
|
-
- Instruction "swipe the top tab bar" -> locate the top tab bar -> swipe within the tab bar area.
|
|
133
|
-
- Instruction "swipe the page to load more" -> locate the content area -> swipe within the content area.
|
|
134
|
-
- Instruction "swipe this card" -> locate the card -> swipe within the card.
|
|
135
|
-
5. Execution: return the execution instruction.
|
|
136
|
-
|
|
137
|
-
### Common mistakes to avoid
|
|
138
|
-
- Do not type directly without clicking the input box first.
|
|
139
|
-
- Do not use coordinates outside the [0.000, 1.000] range.
|
|
140
|
-
- Do not omit the thinking tags.
|
|
141
|
-
- Do not use comments or extra fields in the JSON.
|
|
142
|
-
- Do not continuously perform multiple identical invalid operations.
|
|
143
|
-
- Do not click elements occluded by a popup or system dialog. If you must click the target, close the occluding object first.
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
## Mandatory Principles Reiteration (Must Strictly Follow)
|
|
147
|
-
|
|
148
|
-
- Strict execution: receive an instruction from the superior then must execute, do not judge the meaning.
|
|
149
|
-
- No autonomy: forbidden to autonomously terminate, forbidden to autonomously go back, forbidden to autonomously explore.
|
|
150
|
-
- Accurate coordinates: carefully observe the position of UI elements in the screenshot and estimate coordinates accurately.
|
|
151
|
-
- Format specification: must include complete thinking tags, and the JSON format is correct.
|
|
152
|
-
|
|
153
|
-
Current date: ${getFormattedDate()}`;
|
|
154
|
-
exports.MCP_SYSTEM_PROMPT = exports.MCP_SYSTEM_PROMPT_ALL;
|