@@ -119,7 +119,6 @@ async def main():
119119
120120
121121class MobileUseToolError (Exception ):
122-
123122 def __init__ (self , msg : str ):
124123 self .msg = msg
125124 logger .error (f"mobile use tool execute error :{ msg } " )
@@ -128,43 +127,45 @@ def __init__(self, msg: str):
128127def _require_env_vars () -> None :
129128 missing = [name for name in REQUIRED_ENV_VARS if not os .getenv (name )]
130129 if missing :
131- raise MobileUseToolError (f"Missing required environment variables: { ', ' .join (missing )} " )
130+ raise MobileUseToolError (
131+ f"Missing required environment variables: { ', ' .join (missing )} "
132+ )
132133
133134
134135@dataclass
135136class ResponseMetadata :
136- RequestId : str
137- Action : str
138- Version : str
139- Service : str
140- Region : str
137+ RequestId : str
138+ Action : str
139+ Version : str
140+ Service : str
141+ Region : str
141142
142143
143144@dataclass
144145class RunAgentTaskResult :
145- RunId : str
146- RunName : str
147- ThreadId : str
146+ RunId : str
147+ RunName : str
148+ ThreadId : str
148149
149150
150151@dataclass
151152class RunAgentTaskResponse :
152- ResponseMetadata : ResponseMetadata
153- Result : RunAgentTaskResult
153+ ResponseMetadata : ResponseMetadata
154+ Result : RunAgentTaskResult
154155
155156
156157@dataclass
157158class GetAgentResultResult :
158- IsSuccess : int
159- Content : str
159+ IsSuccess : int
160+ Content : str
160161 StructOutput : str
161162 ScreenShots : List [str ]
162163
163164
164165@dataclass
165166class GetAgentResultResponse :
166- ResponseMetadata : ResponseMetadata
167- Result : GetAgentResultResult
167+ ResponseMetadata : ResponseMetadata
168+ Result : GetAgentResultResult
168169
169170
170171@dataclass
@@ -212,7 +213,6 @@ def _dict_to_dataclass(data: dict, cls: Type[T]) -> T:
212213
213214
214215class PodPool :
215-
216216 def __init__ (self , pod_ids : List [str ]):
217217 self .pod_ids = pod_ids
218218 self .available_pods = Queue ()
@@ -241,7 +241,9 @@ def release_pod(self, pid: str) -> None:
241241 if pid in self .task_map :
242242 del self .task_map [pid ]
243243 self .available_pods .put (pid )
244- logger .debug (f"Released pod: { pid } , available pods: { self .available_pods .qsize ()} " )
244+ logger .debug (
245+ f"Released pod: { pid } , available pods: { self .available_pods .qsize ()} "
246+ )
245247
246248 def get_pod_status (self , pid : str ) -> str :
247249 with self .pod_lock :
@@ -251,8 +253,14 @@ def get_available_count(self) -> int:
251253 return self .available_pods .qsize ()
252254
253255
254- def _run_agent_task (system_prompt : str , user_prompt : str , pid : str , max_step : int , step_interval : int ,
255- timeout : int ) -> RunAgentTaskResponse :
256+ def _run_agent_task (
257+ system_prompt : str ,
258+ user_prompt : str ,
259+ pid : str ,
260+ max_step : int ,
261+ step_interval : int ,
262+ timeout : int ,
263+ ) -> RunAgentTaskResponse :
256264 try :
257265 run_task = ve_request (
258266 request_body = {
@@ -263,7 +271,7 @@ def _run_agent_task(system_prompt: str, user_prompt: str, pid: str, max_step: in
263271 "UserPrompt" : user_prompt ,
264272 "MaxStep" : max_step ,
265273 "StepInterval" : step_interval ,
266- "Timeout" : timeout
274+ "Timeout" : timeout ,
267275 },
268276 action = "RunAgentTaskOneStep" ,
269277 ak = ak ,
@@ -279,9 +287,9 @@ def _run_agent_task(system_prompt: str, user_prompt: str, pid: str, max_step: in
279287
280288 run_task_response = _dict_to_dataclass (run_task , RunAgentTaskResponse )
281289 if (
282- not getattr (run_task_response , "Result" , None )
283- or not run_task_response .Result
284- or not run_task_response .Result .RunId
290+ not getattr (run_task_response , "Result" , None )
291+ or not run_task_response .Result
292+ or not run_task_response .Result .RunId
285293 ):
286294 raise MobileUseToolError (f"RunAgentTask returned invalid result: { run_task } " )
287295 logger .debug (f"Agent run started: { run_task_response } " )
@@ -310,7 +318,9 @@ def _get_task_result(task_id: str) -> GetAgentResultResponse:
310318
311319 result = _dict_to_dataclass (task_result , GetAgentResultResponse )
312320 if not getattr (result , "Result" , None ):
313- raise MobileUseToolError (f"GetAgentResult returned invalid result: { task_result } " )
321+ raise MobileUseToolError (
322+ f"GetAgentResult returned invalid result: { task_result } "
323+ )
314324 logger .debug (f"Fetched agent result: { result } " )
315325 return result
316326
@@ -331,11 +341,15 @@ def _get_current_step(task_id: str) -> ListAgentRunCurrentResponse:
331341 method = "GET" ,
332342 )
333343 except Exception as e :
334- raise MobileUseToolError (f"ListAgentRunCurrentStep invocation failed: { e } " ) from e
344+ raise MobileUseToolError (
345+ f"ListAgentRunCurrentStep invocation failed: { e } "
346+ ) from e
335347
336348 result = _dict_to_dataclass (current_step , ListAgentRunCurrentResponse )
337349 if not getattr (result , "Result" , None ):
338- raise MobileUseToolError (f"ListAgentRunCurrentStep returned invalid result: { current_step } " )
350+ raise MobileUseToolError (
351+ f"ListAgentRunCurrentStep returned invalid result: { current_step } "
352+ )
339353 logger .debug (f"Fetched agent current step: { result } " )
340354 return result
341355
@@ -361,10 +375,10 @@ def _cancel_task(task_id: str) -> None:
361375
362376
363377def create_mobile_use_tool (
364- system_prompt : str ,
365- timeout_seconds : int = 900 ,
366- max_step : int = 100 ,
367- step_interval_seconds : int = 1 ,
378+ system_prompt : str ,
379+ timeout_seconds : int = 900 ,
380+ max_step : int = 100 ,
381+ step_interval_seconds : int = 1 ,
368382):
369383 """
370384 Outer closure: initialize fixed configuration for the virtual mobile tool
@@ -432,10 +446,19 @@ async def run():
432446 logger .debug (
433447 f"Task { index } waiting for pod, available pods: { pod_pool .get_available_count ()} "
434448 )
435- await asyncio .sleep (1 )
436-
437- logger .info (f"Task { index } assigned to pod: { pod_id } , starting: { prompt } " )
438- task_response = _run_agent_task (system_prompt , prompt , pod_id , max_step , step_interval_seconds , timeout_seconds )
449+ await asyncio .sleep (1 )
450+
451+ logger .info (
452+ f"Task { index } assigned to pod: { pod_id } , starting: { prompt } "
453+ )
454+ task_response = _run_agent_task (
455+ system_prompt ,
456+ prompt ,
457+ pod_id ,
458+ max_step ,
459+ step_interval_seconds ,
460+ timeout_seconds ,
461+ )
439462 task_id = task_response .Result .RunId
440463 pod_pool .task_map [pod_id ] = task_id
441464
0 commit comments