6565mobile_tool = create_mobile_use_tool(
6666 system_prompt=system_prompt,
6767 timeout_seconds=300, # 任务超时时间:5分钟
68- poll_interval_seconds =3 # 状态查询间隔:3秒
68+ step_interval_seconds =3 # 状态查询间隔:3秒
6969)
7070
7171# 2. 复用配置执行多个任务(异步调用)
@@ -91,7 +91,6 @@ async def main():
9191from queue import Queue
9292from threading import Lock
9393
94-
9594from veadk .utils .logger import get_logger
9695from veadk .utils .volcengine_sign import ve_request
9796
@@ -145,17 +144,6 @@ class ResponseMetadata:
145144 Region : str # 地域
146145
147146
148- @dataclass
149- class CreateAgentRunConfigResult :
150- ConfigId : int # 配置ID(整数类型)
151-
152-
153- @dataclass
154- class CreateAgentRunConfigResponse :
155- ResponseMetadata : ResponseMetadata # 公共元数据
156- Result : CreateAgentRunConfigResult # 该接口专属结果
157-
158-
159147@dataclass
160148class RunAgentTaskResult :
161149 RunId : str # 运行ID(字符串类型)
@@ -275,55 +263,22 @@ def get_available_count(self) -> int:
275263 return self .available_pods .qsize ()
276264
277265
278- def _create_agent_config (system_prompt : str ) -> CreateAgentRunConfigResponse :
279- try :
280- agent_config = ve_request (
281- request_body = {
282- "MaxStep" : 100 ,
283- "OutputSchema" : "{}" ,
284- "RetryLimit" : 3 ,
285- "SystemPrompt" : system_prompt ,
286- "TosBucket" : tos_bucket ,
287- "TosEndpoint" : tos_endpoint ,
288- "TosRegion" : tos_region ,
289- },
290- action = "CreateAgentRunConfig" ,
291- ak = ak ,
292- sk = sk ,
293- service = service_name ,
294- version = version ,
295- region = region ,
296- content_type = "application/json" ,
297- host = host ,
298- )
299- except Exception as e :
300- raise MobileUseToolError (f"CreateAgentRunConfig 调用失败: { e } " ) from e
301-
302- create_agent_config_response = _dict_to_dataclass (
303- agent_config , CreateAgentRunConfigResponse
304- )
305- if (
306- not getattr (create_agent_config_response , "Result" , None )
307- or create_agent_config_response .Result is None
308- or create_agent_config_response .Result .ConfigId is None
309- ):
310- raise MobileUseToolError (f"CreateAgentRunConfig 返回无效结果: { agent_config } " )
311- logger .debug (f"创建 Agent 配置成功:{ create_agent_config_response } " )
312- return create_agent_config_response
313-
314-
315- def _run_agent_task (user_prompt : str , config_id : int , pid : str ) -> RunAgentTaskResponse :
266+ def _run_agent_task (system_prompt : str , user_prompt : str , pid : str , max_step : int , step_interval : int ,
267+ timeout : int ) -> RunAgentTaskResponse :
316268 try :
317269 run_task = ve_request (
318270 request_body = {
319271 "RunName" : "test-run" ,
320272 "PodId" : pid ,
321273 "ProductId" : product_id ,
274+ "SystemPrompt" : system_prompt ,
322275 "UserPrompt" : user_prompt ,
323- "AgentRunConfigId" : config_id ,
324276 "EndpointId" : tos_endpoint ,
277+ "MaxStep" : max_step ,
278+ "StepInterval" : step_interval ,
279+ "Timeout" : timeout
325280 },
326- action = "RunAgentTask " ,
281+ action = "RunAgentTaskOneStep " ,
327282 ak = ak ,
328283 sk = sk ,
329284 service = service_name ,
@@ -337,9 +292,9 @@ def _run_agent_task(user_prompt: str, config_id: int, pid: str) -> RunAgentTaskR
337292
338293 run_task_response = _dict_to_dataclass (run_task , RunAgentTaskResponse )
339294 if (
340- not getattr (run_task_response , "Result" , None )
341- or not run_task_response .Result
342- or not run_task_response .Result .RunId
295+ not getattr (run_task_response , "Result" , None )
296+ or not run_task_response .Result
297+ or not run_task_response .Result .RunId
343298 ):
344299 raise MobileUseToolError (f"RunAgentTask 返回无效结果: { run_task } " )
345300 logger .debug (f"启动 Agent 运行成功:{ run_task_response } " )
@@ -352,7 +307,6 @@ def _get_task_result(task_id: str) -> GetAgentResultResponse:
352307 request_body = {},
353308 query = {
354309 "RunId" : task_id ,
355- # "IsDetail": False
356310 },
357311 action = "GetAgentResult" ,
358312 ak = ak ,
@@ -420,9 +374,10 @@ def _cancel_task(task_id: str) -> None:
420374
421375
422376def create_mobile_use_tool (
423- system_prompt : str ,
424- timeout_seconds : int = 600 ,
425- poll_interval_seconds : int = 5 ,
377+ system_prompt : str ,
378+ timeout_seconds : int = 900 ,
379+ max_step : int = 100 ,
380+ step_interval_seconds : int = 1 ,
426381):
427382 """
428383 闭包外层函数:初始化虚拟手机工具的固定配置(系统提示、超时/轮询参数)
@@ -433,20 +388,18 @@ def create_mobile_use_tool(
433388 系统级指令,定义Agent的角色、行为规范、约束条件和安全边界。
434389 示例:
435390 * "You are a mobile testing agent. Follow least-privilege principles and avoid unauthorized access."
391+ max_step (int): 每个agent最大执行步骤数
436392 timeout_seconds (int):
437393 最大等待时间(秒),超时未完成则抛出异常。默认:600。
438- poll_interval_seconds (int):
439- 状态查询间隔(秒)。默认:5 。
394+ step_interval_seconds (int):
395+ 状态查询间隔(秒)。默认:1 。
440396
441397 Returns:
442398 Callable[[str], str]: 内层工具函数,接收 user_prompt 执行具体任务并返回结果
443399 """
444400 # 初始化pod池
445401 _require_env_vars ()
446402 pod_pool = PodPool (pod_ids )
447- # 创建Agent配置
448- agent_config = _create_agent_config (system_prompt )
449- config_id = agent_config .Result .ConfigId
450403
451404 async def mobile_use_tool (user_prompts : List [str ]) -> list [None ]:
452405 """
@@ -496,7 +449,7 @@ async def run():
496449
497450 # 2. 执行任务
498451 logger .info (f"任务{ index } 分配到pod: { pod_id } ,开始执行: { prompt } " )
499- task_response = _run_agent_task (prompt , config_id , pod_id )
452+ task_response = _run_agent_task (system_prompt , prompt , pod_id , max_step , step_interval_seconds , timeout_seconds )
500453 task_id = task_response .Result .RunId
501454
502455 # 3. 轮询任务结果
@@ -526,7 +479,7 @@ async def run():
526479 logger .debug (
527480 f"任务{ index } ,thread_id={ task_response .Result .ThreadId } , run_id={ task_id } .当前步骤: { last_step ['Action' ]} ,状态: { '成功' if last_step ['StepResult' ]['IsSuccess' ] else '失败' } "
528481 )
529- await asyncio .sleep (poll_interval_seconds )
482+ await asyncio .sleep (5 )
530483
531484 except Exception as e :
532485 # 任务执行异常
0 commit comments