From 5a8da1b14287b4b823a2d2ab88d46b2c289e2713 Mon Sep 17 00:00:00 2001 From: wangkai04 Date: Mon, 8 Jun 2026 12:32:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20capture=5Fscreenshot=5Fjpeg=20hel?= =?UTF-8?q?per=20=E6=B7=BB=E5=8A=A0=20JPEG=20=E6=88=AA=E5=9B=BE=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JPEG 格式 + quality 参数可将截图体积缩小 2-5x, 避免 LLM API 输入长度超限(如 202745 chars 上限)。 支持 max_dim 等比缩放 + quality 调节(默认 80)。 Co-Authored-By: Claude Opus 4.6 --- agent-workspace/agent_helpers.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/agent-workspace/agent_helpers.py b/agent-workspace/agent_helpers.py index 2d493c17..fbf156e2 100644 --- a/agent-workspace/agent_helpers.py +++ b/agent-workspace/agent_helpers.py @@ -5,3 +5,28 @@ repo's default agent-workspace exists. """ +import base64 +from PIL import Image + + +def capture_screenshot_jpeg(path=None, full=False, max_dim=None, quality=80): + """Save a JPEG of the current viewport. Much smaller than PNG — avoids + LLM API input-length limits on 2× displays. + + quality: 0–100 (default 80). 80 is visually clean and ~5–10× smaller than PNG. + max_dim: proportional downscale if either side exceeds this (e.g. 1800). + """ + # Lazy import to avoid circular dep: helpers loads us at module init. + from browser_harness.helpers import cdp + from browser_harness._ipc import _TMP + + path = path or str(_TMP / "shot.jpg") + r = cdp("Page.captureScreenshot", format="jpeg", quality=quality, + captureBeyondViewport=full) + open(path, "wb").write(base64.b64decode(r["data"])) + if max_dim: + img = Image.open(path) + if max(img.size) > max_dim: + img.thumbnail((max_dim, max_dim)) + img.save(path, format="JPEG", quality=quality) + return path \ No newline at end of file