@@ -90,9 +90,9 @@ def generate(
9090 if result is None :
9191 raise RuntimeError ("LLM session spawn failed or timed out" )
9292
93- # 检查结果
93+ # 等待文件落盘(agent 的 browser/exec 操作可能异步完成)
9494 elapsed = int ((time .time () - start ) * 1000 )
95- check = self ._check_result (output_path , width , height )
95+ check = self ._wait_for_file (output_path , width , height , max_wait = 120 )
9696
9797 if check ["ok" ]:
9898 logger .info ("llm_browser[%s]: success, file=%s size=%d" ,
@@ -242,6 +242,41 @@ def _spawn_session(self, task: str) -> dict | None:
242242 # 超时但图片可能已经下载了
243243 return {"status" : "timeout" , "session_id" : session_id }
244244
245+ def _wait_for_file (self , output_path : Path , width : int , height : int , max_wait : int = 120 ) -> dict :
246+ """等待文件出现并通过检查。
247+
248+ Agent turn 返回后,浏览器/exec 操作可能仍在异步执行(DALL-E 生成 + curl 下载)。
249+ 轮询等待文件出现,最多等 max_wait 秒。
250+ """
251+ import time as _time
252+
253+ # 先立即检查一次
254+ check = self ._check_result (output_path , width , height )
255+ if check ["ok" ]:
256+ return check
257+
258+ logger .info ("llm_browser: file not ready yet, polling up to %ds for %s" , max_wait , output_path .name )
259+ poll_interval = 5 # 每 5 秒检查一次
260+ waited = 0
261+ while waited < max_wait :
262+ _time .sleep (poll_interval )
263+ waited += poll_interval
264+ check = self ._check_result (output_path , width , height )
265+ if check ["ok" ]:
266+ logger .info ("llm_browser: file appeared after %ds polling for %s" , waited , output_path .name )
267+ return check
268+ # 如果文件存在但太小,可能还在下载中
269+ if output_path .exists ():
270+ size = output_path .stat ().st_size
271+ if size > 0 :
272+ logger .info ("llm_browser: file exists but %d bytes (waiting for complete download)" , size )
273+
274+ # 最终检查
275+ final = self ._check_result (output_path , width , height )
276+ if not final ["ok" ]:
277+ logger .warning ("llm_browser: file still not ready after %ds for %s: %s" , max_wait , output_path .name , final .get ("reason" ))
278+ return final
279+
245280 def _check_result (self , output_path : Path , width : int , height : int ) -> dict :
246281 """检查生成结果
247282
0 commit comments