Skip to content

Commit fe27eb9

Browse files
committed
fix(image-gen): 等待文件落盘 — 解决 agent 异步下载导致 file not found
根因:openclaw agent CLI 返回后,浏览器/exec 操作可能仍在异步执行。 agent 发出 curl 下载指令后 CLI 就返回了,但文件实际要几秒到几十秒后才落盘。 _check_result 立刻检查 → file not found → 标记失败。 证据: - cover: agent 01:11 开始 → 01:13 CLI 返回 → file not found 但 cover.jpg 在 01:20 出现(2.4MB PNG) - img_001: 同样模式,CLI 返回时文件不在,几十秒后出现 修复: - 新增 _wait_for_file() 方法:CLI 返回后轮询等待文件,每 5s 检查一次 - 最多等 120 秒(2 分钟),足够 DALL-E 生成 + curl 下载 - 如果文件存在但太小(正在下载),继续等待 - 超时后才最终判定失败
1 parent 7624d26 commit fe27eb9

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

scripts/image_engines/llm_browser_engine.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)