test-wuying-agentbay-sdk 0.13.1-beta.20251223203147 → 0.13.1-beta.20251224091333
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/dist/index.cjs +350 -295
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +70 -119
- package/dist/index.d.ts +70 -119
- package/dist/index.mjs +350 -295
- package/dist/index.mjs.map +1 -1
- package/docs/api/common-features/advanced/agent.md +5 -3
- package/docs/api/common-features/advanced/mobile-use-agent.md +161 -0
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -3886,41 +3886,35 @@ init_esm_shims();
|
|
|
3886
3886
|
|
|
3887
3887
|
// src/agent/agent.ts
|
|
3888
3888
|
init_esm_shims();
|
|
3889
|
-
var
|
|
3890
|
-
/**
|
|
3891
|
-
* Initialize an Computer Agent object.
|
|
3892
|
-
*
|
|
3893
|
-
* @param session - The Session instance that this Agent belongs to.
|
|
3894
|
-
*/
|
|
3889
|
+
var _BaseTaskAgent = class _BaseTaskAgent {
|
|
3895
3890
|
constructor(session) {
|
|
3896
3891
|
this.session = session;
|
|
3897
3892
|
}
|
|
3893
|
+
/**
|
|
3894
|
+
* Get the full MCP tool name based on prefix and action.
|
|
3895
|
+
*/
|
|
3896
|
+
getToolName(action) {
|
|
3897
|
+
const toolMap = {
|
|
3898
|
+
execute: "execute_task",
|
|
3899
|
+
get_status: "get_task_status",
|
|
3900
|
+
terminate: "terminate_task"
|
|
3901
|
+
};
|
|
3902
|
+
const baseName = toolMap[action] || action;
|
|
3903
|
+
if (this.toolPrefix) {
|
|
3904
|
+
return `${this.toolPrefix}_${baseName}`;
|
|
3905
|
+
}
|
|
3906
|
+
return baseName;
|
|
3907
|
+
}
|
|
3898
3908
|
/**
|
|
3899
3909
|
* Execute a specific task described in human language.
|
|
3900
|
-
*
|
|
3901
|
-
* @param task - Task description in human language.
|
|
3902
|
-
* @param maxTryTimes - Maximum number of retry attempts.
|
|
3903
|
-
* @returns ExecutionResult containing success status, task output, and error
|
|
3904
|
-
* message if any.
|
|
3905
|
-
*
|
|
3906
|
-
* @example
|
|
3907
|
-
* ```typescript
|
|
3908
|
-
* const agentBay = new AgentBay({ apiKey: 'your_api_key' });
|
|
3909
|
-
* const result = await agentBay.create({ imageId: 'windows_latest' });
|
|
3910
|
-
* if (result.success) {
|
|
3911
|
-
* const taskResult = await result.session.agent.computer.executeTask(
|
|
3912
|
-
* 'Open notepad',
|
|
3913
|
-
* 10
|
|
3914
|
-
* );
|
|
3915
|
-
* console.log(`Task status: ${taskResult.taskStatus}`);
|
|
3916
|
-
* await result.session.delete();
|
|
3917
|
-
* }
|
|
3918
|
-
* ```
|
|
3919
3910
|
*/
|
|
3920
3911
|
async executeTask(task, maxTryTimes) {
|
|
3921
3912
|
try {
|
|
3922
3913
|
const args = { task, max_try_times: maxTryTimes };
|
|
3923
|
-
const result = await this.session.callMcpTool(
|
|
3914
|
+
const result = await this.session.callMcpTool(
|
|
3915
|
+
this.getToolName("execute"),
|
|
3916
|
+
args
|
|
3917
|
+
);
|
|
3924
3918
|
if (!result.success) {
|
|
3925
3919
|
return {
|
|
3926
3920
|
requestId: result.requestId,
|
|
@@ -3944,7 +3938,7 @@ var _ComputerUseAgent = class _ComputerUseAgent {
|
|
|
3944
3938
|
taskResult: "Invalid execution response."
|
|
3945
3939
|
};
|
|
3946
3940
|
}
|
|
3947
|
-
const taskId = content.task_id;
|
|
3941
|
+
const taskId = content.taskId || content.task_id;
|
|
3948
3942
|
if (!taskId) {
|
|
3949
3943
|
return {
|
|
3950
3944
|
requestId: result.requestId,
|
|
@@ -3969,13 +3963,13 @@ var _ComputerUseAgent = class _ComputerUseAgent {
|
|
|
3969
3963
|
};
|
|
3970
3964
|
}
|
|
3971
3965
|
switch (query.taskStatus) {
|
|
3972
|
-
case "
|
|
3966
|
+
case "completed":
|
|
3973
3967
|
return {
|
|
3974
3968
|
requestId: query.requestId,
|
|
3975
3969
|
success: true,
|
|
3976
3970
|
errorMessage: "",
|
|
3977
3971
|
taskId,
|
|
3978
|
-
taskStatus: "
|
|
3972
|
+
taskStatus: "completed",
|
|
3979
3973
|
taskResult: query.taskProduct
|
|
3980
3974
|
};
|
|
3981
3975
|
case "failed":
|
|
@@ -3987,6 +3981,15 @@ var _ComputerUseAgent = class _ComputerUseAgent {
|
|
|
3987
3981
|
taskStatus: "failed",
|
|
3988
3982
|
taskResult: ""
|
|
3989
3983
|
};
|
|
3984
|
+
case "cancelled":
|
|
3985
|
+
return {
|
|
3986
|
+
requestId: query.requestId,
|
|
3987
|
+
success: false,
|
|
3988
|
+
errorMessage: query.errorMessage || "Task was cancelled.",
|
|
3989
|
+
taskId,
|
|
3990
|
+
taskStatus: "cancelled",
|
|
3991
|
+
taskResult: ""
|
|
3992
|
+
};
|
|
3990
3993
|
case "unsupported":
|
|
3991
3994
|
return {
|
|
3992
3995
|
requestId: query.requestId,
|
|
@@ -4022,32 +4025,20 @@ var _ComputerUseAgent = class _ComputerUseAgent {
|
|
|
4022
4025
|
}
|
|
4023
4026
|
/**
|
|
4024
4027
|
* Get the status of the task with the given task ID.
|
|
4025
|
-
*
|
|
4026
|
-
* @param taskId - Task ID
|
|
4027
|
-
* @returns QueryResult containing the task status
|
|
4028
|
-
*
|
|
4029
|
-
* @example
|
|
4030
|
-
* ```typescript
|
|
4031
|
-
* const agentBay = new AgentBay({ apiKey: 'your_api_key' });
|
|
4032
|
-
* const result = await agentBay.create({ imageId: 'windows_latest' });
|
|
4033
|
-
* if (result.success) {
|
|
4034
|
-
* const taskResult = await result.session.agent.computer.executeTask('Open
|
|
4035
|
-
* calculator', 10); const statusResult = await
|
|
4036
|
-
* result.session.agent.computer.getTaskStatus(taskResult.taskId);
|
|
4037
|
-
* console.log(`Status:
|
|
4038
|
-
* ${JSON.parse(statusResult.output).status}`); await result.session.delete();
|
|
4039
|
-
* }
|
|
4040
|
-
* ```
|
|
4041
4028
|
*/
|
|
4042
4029
|
async getTaskStatus(taskId) {
|
|
4043
4030
|
try {
|
|
4044
4031
|
const args = { task_id: taskId };
|
|
4045
|
-
const result = await this.session.callMcpTool(
|
|
4032
|
+
const result = await this.session.callMcpTool(
|
|
4033
|
+
this.getToolName("get_status"),
|
|
4034
|
+
args
|
|
4035
|
+
);
|
|
4046
4036
|
if (!result.success) {
|
|
4047
4037
|
return {
|
|
4048
4038
|
requestId: result.requestId,
|
|
4049
4039
|
success: false,
|
|
4050
4040
|
errorMessage: result.errorMessage,
|
|
4041
|
+
taskId,
|
|
4051
4042
|
taskAction: "",
|
|
4052
4043
|
taskProduct: "",
|
|
4053
4044
|
taskStatus: "failed"
|
|
@@ -4056,19 +4047,27 @@ var _ComputerUseAgent = class _ComputerUseAgent {
|
|
|
4056
4047
|
let queryResult;
|
|
4057
4048
|
try {
|
|
4058
4049
|
queryResult = JSON.parse(result.data);
|
|
4050
|
+
const contentTaskId = queryResult.taskId || queryResult.task_id || taskId;
|
|
4051
|
+
const taskProduct = queryResult.result || queryResult.product || "";
|
|
4052
|
+
const stream = Array.isArray(queryResult.stream) ? queryResult.stream : void 0;
|
|
4053
|
+
const error = queryResult.error || void 0;
|
|
4059
4054
|
return {
|
|
4060
4055
|
requestId: result.requestId,
|
|
4061
4056
|
success: true,
|
|
4062
4057
|
errorMessage: "",
|
|
4058
|
+
taskId: contentTaskId,
|
|
4063
4059
|
taskAction: queryResult.action || "",
|
|
4064
|
-
taskProduct
|
|
4065
|
-
taskStatus: queryResult.status || "
|
|
4060
|
+
taskProduct,
|
|
4061
|
+
taskStatus: queryResult.status || "completed",
|
|
4062
|
+
stream,
|
|
4063
|
+
error
|
|
4066
4064
|
};
|
|
4067
4065
|
} catch (error) {
|
|
4068
4066
|
return {
|
|
4069
4067
|
requestId: result.requestId,
|
|
4070
4068
|
success: false,
|
|
4071
4069
|
errorMessage: `Failed to get task status: ${error}`,
|
|
4070
|
+
taskId,
|
|
4072
4071
|
taskAction: "",
|
|
4073
4072
|
taskProduct: "",
|
|
4074
4073
|
taskStatus: "failed"
|
|
@@ -4079,6 +4078,7 @@ var _ComputerUseAgent = class _ComputerUseAgent {
|
|
|
4079
4078
|
requestId: "",
|
|
4080
4079
|
success: false,
|
|
4081
4080
|
errorMessage: `Failed to get task status: ${error}`,
|
|
4081
|
+
taskId,
|
|
4082
4082
|
taskAction: "",
|
|
4083
4083
|
taskProduct: "",
|
|
4084
4084
|
taskStatus: "failed"
|
|
@@ -4087,33 +4087,15 @@ var _ComputerUseAgent = class _ComputerUseAgent {
|
|
|
4087
4087
|
}
|
|
4088
4088
|
/**
|
|
4089
4089
|
* Terminate a task with a specified task ID.
|
|
4090
|
-
*
|
|
4091
|
-
* @param taskId - The ID of the running task.
|
|
4092
|
-
* @returns ExecutionResult containing success status, task output, and
|
|
4093
|
-
* error message if any.
|
|
4094
|
-
*
|
|
4095
|
-
* @example
|
|
4096
|
-
* ```typescript
|
|
4097
|
-
* const agentBay = new AgentBay({ apiKey: 'your_api_key' });
|
|
4098
|
-
* const result = await agentBay.create({ imageId: 'windows_latest' });
|
|
4099
|
-
* if (result.success) {
|
|
4100
|
-
* const taskResult = await result.session.agent.computer.executeTask(
|
|
4101
|
-
* 'Open notepad',
|
|
4102
|
-
* 5
|
|
4103
|
-
* );
|
|
4104
|
-
* const terminateResult = await result.session.agent.computer.terminateTask(
|
|
4105
|
-
* taskResult.taskId
|
|
4106
|
-
* );
|
|
4107
|
-
* console.log(`Terminated: ${terminateResult.taskStatus}`);
|
|
4108
|
-
* await result.session.delete();
|
|
4109
|
-
* }
|
|
4110
|
-
* ```
|
|
4111
4090
|
*/
|
|
4112
4091
|
async terminateTask(taskId) {
|
|
4113
4092
|
logDebug("Terminating task");
|
|
4114
4093
|
try {
|
|
4115
4094
|
const args = { task_id: taskId };
|
|
4116
|
-
const result = await this.session.callMcpTool(
|
|
4095
|
+
const result = await this.session.callMcpTool(
|
|
4096
|
+
this.getToolName("terminate"),
|
|
4097
|
+
args
|
|
4098
|
+
);
|
|
4117
4099
|
let content;
|
|
4118
4100
|
try {
|
|
4119
4101
|
content = JSON.parse(result.data);
|
|
@@ -4127,8 +4109,8 @@ var _ComputerUseAgent = class _ComputerUseAgent {
|
|
|
4127
4109
|
taskResult: ""
|
|
4128
4110
|
};
|
|
4129
4111
|
}
|
|
4130
|
-
const terminatedTaskId = content.task_id || taskId;
|
|
4131
|
-
const status = content.status || "
|
|
4112
|
+
const terminatedTaskId = content.taskId || content.task_id || taskId;
|
|
4113
|
+
const status = content.status || "cancelling";
|
|
4132
4114
|
if (result.success) {
|
|
4133
4115
|
return {
|
|
4134
4116
|
requestId: result.requestId,
|
|
@@ -4159,17 +4141,20 @@ var _ComputerUseAgent = class _ComputerUseAgent {
|
|
|
4159
4141
|
}
|
|
4160
4142
|
}
|
|
4161
4143
|
};
|
|
4144
|
+
__name(_BaseTaskAgent, "BaseTaskAgent");
|
|
4145
|
+
var BaseTaskAgent = _BaseTaskAgent;
|
|
4146
|
+
var _ComputerUseAgent = class _ComputerUseAgent extends (BaseTaskAgent.default || BaseTaskAgent) {
|
|
4147
|
+
constructor() {
|
|
4148
|
+
super(...arguments);
|
|
4149
|
+
this.toolPrefix = "flux";
|
|
4150
|
+
}
|
|
4151
|
+
};
|
|
4162
4152
|
__name(_ComputerUseAgent, "ComputerUseAgent");
|
|
4163
4153
|
var ComputerUseAgent = _ComputerUseAgent;
|
|
4164
|
-
var _BrowserUseAgent = class _BrowserUseAgent {
|
|
4165
|
-
|
|
4166
|
-
|
|
4167
|
-
|
|
4168
|
-
*
|
|
4169
|
-
* @param session - The Session instance that this Agent belongs to.
|
|
4170
|
-
*/
|
|
4171
|
-
constructor(session) {
|
|
4172
|
-
this.session = session;
|
|
4154
|
+
var _BrowserUseAgent = class _BrowserUseAgent extends (BaseTaskAgent.default || BaseTaskAgent) {
|
|
4155
|
+
constructor() {
|
|
4156
|
+
super(...arguments);
|
|
4157
|
+
this.toolPrefix = "browser_use";
|
|
4173
4158
|
}
|
|
4174
4159
|
/**
|
|
4175
4160
|
* Initialize the browser agent with specific options.
|
|
@@ -4214,276 +4199,345 @@ var _BrowserUseAgent = class _BrowserUseAgent {
|
|
|
4214
4199
|
};
|
|
4215
4200
|
}
|
|
4216
4201
|
}
|
|
4202
|
+
};
|
|
4203
|
+
__name(_BrowserUseAgent, "BrowserUseAgent");
|
|
4204
|
+
var BrowserUseAgent = _BrowserUseAgent;
|
|
4205
|
+
var _MobileUseAgent = class _MobileUseAgent extends (BaseTaskAgent.default || BaseTaskAgent) {
|
|
4206
|
+
constructor() {
|
|
4207
|
+
super(...arguments);
|
|
4208
|
+
this.toolPrefix = "";
|
|
4209
|
+
}
|
|
4217
4210
|
/**
|
|
4218
|
-
* Execute a
|
|
4211
|
+
* Execute a task in human language without waiting for completion
|
|
4212
|
+
* (non-blocking). This is a fire-and-return interface that immediately
|
|
4213
|
+
* provides a task ID. Call getTaskStatus to check the task status.
|
|
4219
4214
|
*
|
|
4220
4215
|
* @param task - Task description in human language.
|
|
4221
|
-
* @param
|
|
4222
|
-
*
|
|
4223
|
-
*
|
|
4216
|
+
* @param maxSteps - Maximum number of steps (clicks/swipes/etc.) allowed.
|
|
4217
|
+
* Used to prevent infinite loops or excessive resource
|
|
4218
|
+
* consumption. Default is 50.
|
|
4219
|
+
* @param maxStepRetries - Maximum retry times for MCP tool call failures
|
|
4220
|
+
* at SDK level. Used to retry when callMcpTool fails
|
|
4221
|
+
* (e.g., network errors, timeouts). Default is 3.
|
|
4222
|
+
* @returns ExecutionResult containing success status, task ID, task status,
|
|
4223
|
+
* and error message if any.
|
|
4224
4224
|
*
|
|
4225
4225
|
* @example
|
|
4226
4226
|
* ```typescript
|
|
4227
4227
|
* const agentBay = new AgentBay({ apiKey: 'your_api_key' });
|
|
4228
|
-
* const result = await agentBay.create({ imageId: '
|
|
4228
|
+
* const result = await agentBay.create({ imageId: 'mobile_latest' });
|
|
4229
4229
|
* if (result.success) {
|
|
4230
|
-
* const
|
|
4231
|
-
* '
|
|
4232
|
-
* 10
|
|
4230
|
+
* const execResult = await result.session.agent.mobile.executeTask(
|
|
4231
|
+
* 'Open WeChat app', 100, 5
|
|
4233
4232
|
* );
|
|
4234
|
-
* console.log(`Task
|
|
4233
|
+
* console.log(`Task ID: ${execResult.taskId}`);
|
|
4235
4234
|
* await result.session.delete();
|
|
4236
4235
|
* }
|
|
4237
4236
|
* ```
|
|
4238
4237
|
*/
|
|
4239
|
-
async executeTask(task,
|
|
4240
|
-
|
|
4241
|
-
|
|
4242
|
-
|
|
4243
|
-
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
errorMessage: result.errorMessage,
|
|
4248
|
-
taskStatus: "failed",
|
|
4249
|
-
taskId: "",
|
|
4250
|
-
taskResult: "Task Failed"
|
|
4251
|
-
};
|
|
4252
|
-
}
|
|
4253
|
-
let content;
|
|
4238
|
+
async executeTask(task, maxSteps = 50, maxStepRetries = 3) {
|
|
4239
|
+
const args = {
|
|
4240
|
+
task,
|
|
4241
|
+
max_steps: maxSteps
|
|
4242
|
+
};
|
|
4243
|
+
let lastError;
|
|
4244
|
+
let lastRequestId = "";
|
|
4245
|
+
for (let attempt = 0; attempt < maxStepRetries; attempt++) {
|
|
4254
4246
|
try {
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
4258
|
-
|
|
4259
|
-
|
|
4260
|
-
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
|
|
4265
|
-
}
|
|
4266
|
-
const taskId = content.task_id;
|
|
4267
|
-
if (!taskId) {
|
|
4268
|
-
return {
|
|
4269
|
-
requestId: result.requestId,
|
|
4270
|
-
success: false,
|
|
4271
|
-
errorMessage: "Task ID not found in response",
|
|
4272
|
-
taskStatus: "failed",
|
|
4273
|
-
taskId: "",
|
|
4274
|
-
taskResult: "Invalid task ID."
|
|
4275
|
-
};
|
|
4276
|
-
}
|
|
4277
|
-
let triedTime = 0;
|
|
4278
|
-
while (triedTime < maxTryTimes) {
|
|
4279
|
-
const query = await this.getTaskStatus(taskId);
|
|
4280
|
-
if (!query.success) {
|
|
4281
|
-
return {
|
|
4282
|
-
requestId: query.requestId,
|
|
4283
|
-
success: false,
|
|
4284
|
-
errorMessage: query.errorMessage,
|
|
4285
|
-
taskStatus: "failed",
|
|
4286
|
-
taskId,
|
|
4287
|
-
taskResult: ""
|
|
4288
|
-
};
|
|
4289
|
-
}
|
|
4290
|
-
switch (query.taskStatus) {
|
|
4291
|
-
case "finished":
|
|
4247
|
+
const result = await this.session.callMcpTool(
|
|
4248
|
+
this.getToolName("execute"),
|
|
4249
|
+
args
|
|
4250
|
+
);
|
|
4251
|
+
lastRequestId = result.requestId;
|
|
4252
|
+
if (result.success) {
|
|
4253
|
+
let content;
|
|
4254
|
+
try {
|
|
4255
|
+
content = JSON.parse(result.data);
|
|
4256
|
+
} catch (err) {
|
|
4292
4257
|
return {
|
|
4293
|
-
requestId:
|
|
4294
|
-
success:
|
|
4295
|
-
errorMessage:
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
taskResult:
|
|
4258
|
+
requestId: result.requestId,
|
|
4259
|
+
success: false,
|
|
4260
|
+
errorMessage: `Failed to parse response: ${err}`,
|
|
4261
|
+
taskStatus: "failed",
|
|
4262
|
+
taskId: "",
|
|
4263
|
+
taskResult: "Invalid execution response."
|
|
4299
4264
|
};
|
|
4300
|
-
|
|
4265
|
+
}
|
|
4266
|
+
const taskId = content.taskId || content.task_id;
|
|
4267
|
+
if (!taskId) {
|
|
4301
4268
|
return {
|
|
4302
|
-
requestId:
|
|
4269
|
+
requestId: result.requestId,
|
|
4303
4270
|
success: false,
|
|
4304
|
-
errorMessage:
|
|
4305
|
-
taskId,
|
|
4271
|
+
errorMessage: "Task ID not found in response",
|
|
4306
4272
|
taskStatus: "failed",
|
|
4307
|
-
|
|
4273
|
+
taskId: "",
|
|
4274
|
+
taskResult: "Invalid task ID."
|
|
4308
4275
|
};
|
|
4309
|
-
|
|
4276
|
+
}
|
|
4277
|
+
return {
|
|
4278
|
+
requestId: result.requestId,
|
|
4279
|
+
success: true,
|
|
4280
|
+
errorMessage: "",
|
|
4281
|
+
taskId,
|
|
4282
|
+
taskStatus: "running",
|
|
4283
|
+
taskResult: ""
|
|
4284
|
+
};
|
|
4285
|
+
} else {
|
|
4286
|
+
lastError = result.errorMessage || "Failed to execute task";
|
|
4287
|
+
if (attempt < maxStepRetries - 1) {
|
|
4288
|
+
logDebug(
|
|
4289
|
+
`Attempt ${attempt + 1}/${maxStepRetries} failed, retrying...`
|
|
4290
|
+
);
|
|
4291
|
+
await new Promise((resolve2) => setTimeout(resolve2, 1e3));
|
|
4292
|
+
continue;
|
|
4293
|
+
} else {
|
|
4310
4294
|
return {
|
|
4311
|
-
requestId:
|
|
4295
|
+
requestId: result.requestId,
|
|
4312
4296
|
success: false,
|
|
4313
|
-
errorMessage:
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
taskResult: ""
|
|
4297
|
+
errorMessage: lastError,
|
|
4298
|
+
taskStatus: "failed",
|
|
4299
|
+
taskId: "",
|
|
4300
|
+
taskResult: "Task Failed"
|
|
4317
4301
|
};
|
|
4302
|
+
}
|
|
4303
|
+
}
|
|
4304
|
+
} catch (error) {
|
|
4305
|
+
lastError = `Failed to execute: ${error}`;
|
|
4306
|
+
if (attempt < maxStepRetries - 1) {
|
|
4307
|
+
logDebug(
|
|
4308
|
+
`Attempt ${attempt + 1}/${maxStepRetries} raised exception, retrying...`
|
|
4309
|
+
);
|
|
4310
|
+
await new Promise((resolve2) => setTimeout(resolve2, 1e3));
|
|
4311
|
+
continue;
|
|
4312
|
+
} else {
|
|
4313
|
+
return {
|
|
4314
|
+
requestId: lastRequestId,
|
|
4315
|
+
success: false,
|
|
4316
|
+
errorMessage: lastError,
|
|
4317
|
+
taskStatus: "failed",
|
|
4318
|
+
taskId: "",
|
|
4319
|
+
taskResult: "Task Failed"
|
|
4320
|
+
};
|
|
4318
4321
|
}
|
|
4319
|
-
logDebug(`Task ${taskId} is still running, please wait for a while.`);
|
|
4320
|
-
await new Promise((resolve2) => setTimeout(resolve2, 3e3));
|
|
4321
|
-
triedTime++;
|
|
4322
4322
|
}
|
|
4323
|
-
return {
|
|
4324
|
-
requestId: result.requestId,
|
|
4325
|
-
success: false,
|
|
4326
|
-
errorMessage: "Task execution timed out",
|
|
4327
|
-
taskStatus: "timeout",
|
|
4328
|
-
taskId,
|
|
4329
|
-
taskResult: "Task Timed Out"
|
|
4330
|
-
};
|
|
4331
|
-
} catch (error) {
|
|
4332
|
-
return {
|
|
4333
|
-
requestId: "",
|
|
4334
|
-
success: false,
|
|
4335
|
-
errorMessage: `Failed to execute: ${error}`,
|
|
4336
|
-
taskStatus: "failed",
|
|
4337
|
-
taskId: "",
|
|
4338
|
-
taskResult: "Task Failed"
|
|
4339
|
-
};
|
|
4340
4323
|
}
|
|
4324
|
+
return {
|
|
4325
|
+
requestId: lastRequestId,
|
|
4326
|
+
success: false,
|
|
4327
|
+
errorMessage: `Failed after ${maxStepRetries} attempts: ${lastError || "Unknown error"}`,
|
|
4328
|
+
taskStatus: "failed",
|
|
4329
|
+
taskId: "",
|
|
4330
|
+
taskResult: "Task Failed"
|
|
4331
|
+
};
|
|
4341
4332
|
}
|
|
4342
4333
|
/**
|
|
4343
|
-
*
|
|
4334
|
+
* Execute a specific task described in human language synchronously.
|
|
4335
|
+
* This is a synchronous interface that blocks until the task is completed or
|
|
4336
|
+
* an error occurs, or timeout happens. The default polling interval is
|
|
4337
|
+
* 3 seconds.
|
|
4344
4338
|
*
|
|
4345
|
-
* @param
|
|
4346
|
-
* @
|
|
4339
|
+
* @param task - Task description in human language.
|
|
4340
|
+
* @param maxSteps - Maximum number of steps (clicks/swipes/etc.) allowed.
|
|
4341
|
+
* Used to prevent infinite loops or excessive resource
|
|
4342
|
+
* consumption. Default is 50.
|
|
4343
|
+
* @param maxStepRetries - Maximum retry times for MCP tool call failures
|
|
4344
|
+
* at SDK level. Used to retry when callMcpTool fails
|
|
4345
|
+
* (e.g., network errors, timeouts). Default is 3.
|
|
4346
|
+
* @param maxTryTimes - Maximum number of polling attempts (each 3 seconds).
|
|
4347
|
+
* Used to control how long to wait for task completion.
|
|
4348
|
+
* Default is 300 (about 15 minutes).
|
|
4349
|
+
* @returns ExecutionResult containing success status, task ID, task status,
|
|
4350
|
+
* and error message if any.
|
|
4347
4351
|
*
|
|
4348
4352
|
* @example
|
|
4349
4353
|
* ```typescript
|
|
4350
4354
|
* const agentBay = new AgentBay({ apiKey: 'your_api_key' });
|
|
4351
|
-
* const result = await agentBay.create({ imageId: '
|
|
4355
|
+
* const result = await agentBay.create({ imageId: 'mobile_latest' });
|
|
4352
4356
|
* if (result.success) {
|
|
4353
|
-
* const
|
|
4354
|
-
* '
|
|
4355
|
-
* 10
|
|
4357
|
+
* const execResult = await result.session.agent.mobile.executeTaskAndWait(
|
|
4358
|
+
* 'Open WeChat app', 100, 3, 200
|
|
4356
4359
|
* );
|
|
4357
|
-
*
|
|
4358
|
-
* taskResult.taskId
|
|
4359
|
-
* );
|
|
4360
|
-
* console.log(`Status: ${statusResult.taskStatus}`);
|
|
4360
|
+
* console.log(`Task result: ${execResult.taskResult}`);
|
|
4361
4361
|
* await result.session.delete();
|
|
4362
4362
|
* }
|
|
4363
4363
|
* ```
|
|
4364
4364
|
*/
|
|
4365
|
-
async
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
taskAction: "",
|
|
4375
|
-
taskProduct: "",
|
|
4376
|
-
taskStatus: "failed"
|
|
4377
|
-
};
|
|
4378
|
-
}
|
|
4379
|
-
let queryResult;
|
|
4365
|
+
async executeTaskAndWait(task, maxSteps = 50, maxStepRetries = 3, maxTryTimes = 300) {
|
|
4366
|
+
const args = {
|
|
4367
|
+
task,
|
|
4368
|
+
max_steps: maxSteps
|
|
4369
|
+
};
|
|
4370
|
+
let taskId;
|
|
4371
|
+
let lastError;
|
|
4372
|
+
let lastRequestId = "";
|
|
4373
|
+
for (let attempt = 0; attempt < maxStepRetries; attempt++) {
|
|
4380
4374
|
try {
|
|
4381
|
-
|
|
4382
|
-
|
|
4383
|
-
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4389
|
-
|
|
4375
|
+
const result = await this.session.callMcpTool(
|
|
4376
|
+
this.getToolName("execute"),
|
|
4377
|
+
args
|
|
4378
|
+
);
|
|
4379
|
+
lastRequestId = result.requestId;
|
|
4380
|
+
if (result.success) {
|
|
4381
|
+
let content;
|
|
4382
|
+
try {
|
|
4383
|
+
content = JSON.parse(result.data);
|
|
4384
|
+
} catch (err) {
|
|
4385
|
+
return {
|
|
4386
|
+
requestId: result.requestId,
|
|
4387
|
+
success: false,
|
|
4388
|
+
errorMessage: `Failed to parse response: ${err}`,
|
|
4389
|
+
taskStatus: "failed",
|
|
4390
|
+
taskId: "",
|
|
4391
|
+
taskResult: "Invalid execution response."
|
|
4392
|
+
};
|
|
4393
|
+
}
|
|
4394
|
+
taskId = content.taskId || content.task_id;
|
|
4395
|
+
if (!taskId) {
|
|
4396
|
+
return {
|
|
4397
|
+
requestId: result.requestId,
|
|
4398
|
+
success: false,
|
|
4399
|
+
errorMessage: "Task ID not found in response",
|
|
4400
|
+
taskStatus: "failed",
|
|
4401
|
+
taskId: "",
|
|
4402
|
+
taskResult: "Invalid task ID."
|
|
4403
|
+
};
|
|
4404
|
+
}
|
|
4405
|
+
break;
|
|
4406
|
+
} else {
|
|
4407
|
+
lastError = result.errorMessage || "Failed to execute task";
|
|
4408
|
+
if (attempt < maxStepRetries - 1) {
|
|
4409
|
+
logDebug(
|
|
4410
|
+
`Attempt ${attempt + 1}/${maxStepRetries} failed, retrying...`
|
|
4411
|
+
);
|
|
4412
|
+
await new Promise((resolve2) => setTimeout(resolve2, 1e3));
|
|
4413
|
+
continue;
|
|
4414
|
+
} else {
|
|
4415
|
+
return {
|
|
4416
|
+
requestId: result.requestId,
|
|
4417
|
+
success: false,
|
|
4418
|
+
errorMessage: lastError,
|
|
4419
|
+
taskStatus: "failed",
|
|
4420
|
+
taskId: "",
|
|
4421
|
+
taskResult: "Task Failed"
|
|
4422
|
+
};
|
|
4423
|
+
}
|
|
4424
|
+
}
|
|
4390
4425
|
} catch (error) {
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
}
|
|
4426
|
+
lastError = `Failed to execute: ${error}`;
|
|
4427
|
+
if (attempt < maxStepRetries - 1) {
|
|
4428
|
+
logDebug(
|
|
4429
|
+
`Attempt ${attempt + 1}/${maxStepRetries} raised exception, retrying...`
|
|
4430
|
+
);
|
|
4431
|
+
await new Promise((resolve2) => setTimeout(resolve2, 1e3));
|
|
4432
|
+
continue;
|
|
4433
|
+
} else {
|
|
4434
|
+
return {
|
|
4435
|
+
requestId: lastRequestId,
|
|
4436
|
+
success: false,
|
|
4437
|
+
errorMessage: lastError,
|
|
4438
|
+
taskStatus: "failed",
|
|
4439
|
+
taskId: "",
|
|
4440
|
+
taskResult: "Task Failed"
|
|
4441
|
+
};
|
|
4442
|
+
}
|
|
4399
4443
|
}
|
|
4400
|
-
}
|
|
4444
|
+
}
|
|
4445
|
+
if (!taskId) {
|
|
4401
4446
|
return {
|
|
4402
|
-
requestId:
|
|
4447
|
+
requestId: lastRequestId,
|
|
4403
4448
|
success: false,
|
|
4404
|
-
errorMessage: `Failed to get
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4449
|
+
errorMessage: `Failed to get task_id after ${maxStepRetries} attempts: ${lastError || "Unknown error"}`,
|
|
4450
|
+
taskStatus: "failed",
|
|
4451
|
+
taskId: "",
|
|
4452
|
+
taskResult: "Task Failed"
|
|
4408
4453
|
};
|
|
4409
4454
|
}
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
* @returns ExecutionResult containing success status, task output, and
|
|
4416
|
-
* error message if any.
|
|
4417
|
-
*
|
|
4418
|
-
* @example
|
|
4419
|
-
* ```typescript
|
|
4420
|
-
* const agentBay = new AgentBay({ apiKey: 'your_api_key' });
|
|
4421
|
-
* const result = await agentBay.create({ imageId: 'linux_latest' });
|
|
4422
|
-
* if (result.success) {
|
|
4423
|
-
* const taskResult = await result.session.agent.browser.executeTask(
|
|
4424
|
-
* 'Navigate to baidu and query the weather of Shanghai',
|
|
4425
|
-
* 10
|
|
4426
|
-
* );
|
|
4427
|
-
* const terminateResult = await result.session.agent.browser.terminateTask(
|
|
4428
|
-
* taskResult.taskId
|
|
4429
|
-
* );
|
|
4430
|
-
* console.log(`Terminated: ${terminateResult.taskStatus}`);
|
|
4431
|
-
* await result.session.delete();
|
|
4432
|
-
* }
|
|
4433
|
-
* ```
|
|
4434
|
-
*/
|
|
4435
|
-
async terminateTask(taskId) {
|
|
4436
|
-
logDebug("Terminating task");
|
|
4437
|
-
try {
|
|
4438
|
-
const args = { task_id: taskId };
|
|
4439
|
-
const result = await this.session.callMcpTool("browser_use_terminate_task", args);
|
|
4440
|
-
let content;
|
|
4441
|
-
try {
|
|
4442
|
-
content = JSON.parse(result.data);
|
|
4443
|
-
} catch (err) {
|
|
4455
|
+
let triedTime = 0;
|
|
4456
|
+
const processedTimestamps = /* @__PURE__ */ new Set();
|
|
4457
|
+
while (triedTime < maxTryTimes) {
|
|
4458
|
+
const query = await this.getTaskStatus(taskId);
|
|
4459
|
+
if (!query.success) {
|
|
4444
4460
|
return {
|
|
4445
|
-
requestId:
|
|
4461
|
+
requestId: query.requestId,
|
|
4446
4462
|
success: false,
|
|
4447
|
-
errorMessage:
|
|
4448
|
-
taskId,
|
|
4463
|
+
errorMessage: query.errorMessage,
|
|
4449
4464
|
taskStatus: "failed",
|
|
4465
|
+
taskId,
|
|
4450
4466
|
taskResult: ""
|
|
4451
4467
|
};
|
|
4452
4468
|
}
|
|
4453
|
-
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4462
|
-
|
|
4463
|
-
|
|
4469
|
+
if (query.stream) {
|
|
4470
|
+
for (const streamItem of query.stream) {
|
|
4471
|
+
const timestamp = streamItem.timestamp_ms;
|
|
4472
|
+
if (timestamp !== void 0 && !processedTimestamps.has(timestamp)) {
|
|
4473
|
+
processedTimestamps.add(timestamp);
|
|
4474
|
+
const content = streamItem.content || "";
|
|
4475
|
+
const reasoning = streamItem.reasoning || "";
|
|
4476
|
+
if (content) {
|
|
4477
|
+
process.stdout.write(content);
|
|
4478
|
+
}
|
|
4479
|
+
if (reasoning) {
|
|
4480
|
+
}
|
|
4481
|
+
}
|
|
4482
|
+
}
|
|
4464
4483
|
}
|
|
4465
|
-
|
|
4466
|
-
|
|
4467
|
-
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4484
|
+
if (query.error) {
|
|
4485
|
+
}
|
|
4486
|
+
switch (query.taskStatus) {
|
|
4487
|
+
case "completed":
|
|
4488
|
+
return {
|
|
4489
|
+
requestId: query.requestId,
|
|
4490
|
+
success: true,
|
|
4491
|
+
errorMessage: "",
|
|
4492
|
+
taskId,
|
|
4493
|
+
taskStatus: "completed",
|
|
4494
|
+
taskResult: query.taskProduct
|
|
4495
|
+
};
|
|
4496
|
+
case "failed":
|
|
4497
|
+
return {
|
|
4498
|
+
requestId: query.requestId,
|
|
4499
|
+
success: false,
|
|
4500
|
+
errorMessage: query.errorMessage || "Failed to execute task.",
|
|
4501
|
+
taskId,
|
|
4502
|
+
taskStatus: "failed",
|
|
4503
|
+
taskResult: ""
|
|
4504
|
+
};
|
|
4505
|
+
case "cancelled":
|
|
4506
|
+
return {
|
|
4507
|
+
requestId: query.requestId,
|
|
4508
|
+
success: false,
|
|
4509
|
+
errorMessage: query.errorMessage || "Task was cancelled.",
|
|
4510
|
+
taskId,
|
|
4511
|
+
taskStatus: "cancelled",
|
|
4512
|
+
taskResult: ""
|
|
4513
|
+
};
|
|
4514
|
+
case "unsupported":
|
|
4515
|
+
return {
|
|
4516
|
+
requestId: query.requestId,
|
|
4517
|
+
success: false,
|
|
4518
|
+
errorMessage: query.errorMessage || "Unsupported task.",
|
|
4519
|
+
taskId,
|
|
4520
|
+
taskStatus: "unsupported",
|
|
4521
|
+
taskResult: ""
|
|
4522
|
+
};
|
|
4523
|
+
}
|
|
4524
|
+
logDebug(`\u23F3 Task ${taskId} running \u{1F680}: ${query.taskAction}.`);
|
|
4525
|
+
await new Promise((resolve2) => setTimeout(resolve2, 3e3));
|
|
4526
|
+
triedTime++;
|
|
4482
4527
|
}
|
|
4528
|
+
logDebug("\u26A0\uFE0F task execution timeout!");
|
|
4529
|
+
return {
|
|
4530
|
+
requestId: lastRequestId,
|
|
4531
|
+
success: false,
|
|
4532
|
+
errorMessage: "Task timeout.",
|
|
4533
|
+
taskStatus: "failed",
|
|
4534
|
+
taskId,
|
|
4535
|
+
taskResult: "Task timeout."
|
|
4536
|
+
};
|
|
4483
4537
|
}
|
|
4484
4538
|
};
|
|
4485
|
-
__name(
|
|
4486
|
-
var
|
|
4539
|
+
__name(_MobileUseAgent, "MobileUseAgent");
|
|
4540
|
+
var MobileUseAgent = _MobileUseAgent;
|
|
4487
4541
|
var _Agent = class _Agent {
|
|
4488
4542
|
/**
|
|
4489
4543
|
* Initialize an Agent object.
|
|
@@ -4493,6 +4547,7 @@ var _Agent = class _Agent {
|
|
|
4493
4547
|
constructor(session) {
|
|
4494
4548
|
this.computer = new ComputerUseAgent(session);
|
|
4495
4549
|
this.browser = new BrowserUseAgent(session);
|
|
4550
|
+
this.mobile = new MobileUseAgent(session);
|
|
4496
4551
|
}
|
|
4497
4552
|
};
|
|
4498
4553
|
__name(_Agent, "Agent");
|