From 726e043a3a0ef930837db6668ef3f40ac882f62a Mon Sep 17 00:00:00 2001 From: oliver Date: Wed, 29 Apr 2026 20:31:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=AE=80=20attachment=5Flocal=5Furl?= =?UTF-8?q?=20=E9=BB=98=E8=AE=A4=E8=BF=94=E5=9B=9E=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=EF=BC=8C=E9=99=8D=E4=BD=8E=E8=B7=AF=E5=BE=84=E5=99=AA=E9=9F=B3?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 默认仅返回 attachment_id 与 file_url 等核心信息,并将 exists/local_path/preferred_url 调整为 verbose 模式输出,补齐对应测试。 Made-with: Cursor --- .../tools/public/attachment_local_url_tool.py | 17 ++++++++++++----- tests/test_attachment_local_url_public_tool.py | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/runtime/tools/public/attachment_local_url_tool.py b/runtime/tools/public/attachment_local_url_tool.py index b2de9fa9..17c82a62 100644 --- a/runtime/tools/public/attachment_local_url_tool.py +++ b/runtime/tools/public/attachment_local_url_tool.py @@ -13,6 +13,7 @@ _ATTACHMENT_ID_RE = re.compile(r"^[a-f0-9]{64}$") def attachment_local_url_tool() -> ToolSpec: def _handler(args: dict[str, Any]) -> dict[str, Any]: attachment_id = str(args.get("attachment_id") or "").strip().lower() + verbose = bool(args.get("verbose")) if not attachment_id: return {"ok": False, "error": "attachment_id_required"} if not _ATTACHMENT_ID_RE.fullmatch(attachment_id): @@ -24,18 +25,20 @@ def attachment_local_url_tool() -> ToolSpec: local_path_text = str(local_path) if local_path else "" file_url = Path(local_path).resolve().as_uri() if local_path else "" - return { + out = { "ok": True, "attachment_id": attachment_id, - "exists": bool(meta or local_path), - # For direct rendering in desktop/electron contexts, prefer file URL first. - "preferred_url": file_url or local_path_text, "mime": str(getattr(meta, "mime", "") or ""), "name": str(getattr(meta, "name", "") or ""), "bytes": int(getattr(meta, "bytes", 0) or 0), - "local_path": local_path_text, "file_url": file_url, } + if verbose: + # Keep debug/context fields opt-in to reduce response noise. + out["exists"] = bool(meta or local_path) + out["local_path"] = local_path_text + out["preferred_url"] = file_url or local_path_text + return out return ToolSpec( name="attachment_local_url", @@ -44,6 +47,10 @@ def attachment_local_url_tool() -> ToolSpec: "type": "object", "properties": { "attachment_id": {"type": "string", "description": "Stored attachment id."}, + "verbose": { + "type": "boolean", + "description": "When true, include extra debug fields (exists/local_path/preferred_url).", + }, }, "required": ["attachment_id"], "additionalProperties": False, diff --git a/tests/test_attachment_local_url_public_tool.py b/tests/test_attachment_local_url_public_tool.py index 512d1235..bc9a5e2e 100644 --- a/tests/test_attachment_local_url_public_tool.py +++ b/tests/test_attachment_local_url_public_tool.py @@ -36,11 +36,22 @@ def test_attachment_local_url_builds_absolute_url(monkeypatch) -> None: aid = "a" * 64 out = spec.handler({"attachment_id": aid}) assert out.get("ok") is True + assert str(out.get("file_url") or "").startswith("file:///") + assert out.get("mime") == "image/png" + assert "exists" not in out + assert "local_path" not in out + assert "preferred_url" not in out + + +def test_attachment_local_url_verbose_includes_debug_fields(monkeypatch) -> None: + monkeypatch.setattr("oclaw.runtime.tools.public.attachment_local_url_tool.AttachmentAssetStore", _FakeStore) + spec = attachment_local_url_tool() + aid = "a" * 64 + out = spec.handler({"attachment_id": aid, "verbose": True}) + assert out.get("ok") is True assert out.get("exists") is True assert str(out.get("local_path") or "").endswith("cat.png") - assert str(out.get("file_url") or "").startswith("file:///") assert out.get("preferred_url") == out.get("file_url") - assert out.get("mime") == "image/png" def test_attachment_local_url_is_visible_in_public_registry() -> None: