feat(webui): refine attachment galleries and file cards (#6457)
* feat(webui): refine attachment galleries and file cards * fix(webui): align attachment tests and Escape handling
This commit is contained in:
1 parent
44a7ef53ce
commit
eed7b47753
19 files changed
+660
-255
No files matched your search
@@ -149,3 +149,35 @@ async def test_mixed_media_replay_keeps_uploaded_cache_and_borrowed_freshness(tm
|
||||
assert bytes(served.body) == expected
|
||||
assert served.headers["Cache-Control"] == cache
|
||||
assert sorted(path.name for path in media.iterdir()) == ["borrowed.png", "upload.png"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("role", ["assistant", "user"])
|
||||
def test_replayed_media_keeps_missing_files_in_order_without_download_urls(tmp_path, monkeypatch, role):
|
||||
media = tmp_path / "media"
|
||||
media.mkdir()
|
||||
first = media / "first.json"
|
||||
missing = media / "missing.txt"
|
||||
last = media / "last.pdf"
|
||||
first.write_bytes(b'{"synthetic":true}')
|
||||
last.write_bytes(b"synthetic pdf")
|
||||
paths = [str(path) for path in (first, missing, last)]
|
||||
gateway = WebUIMediaGateway(workspace_path=tmp_path, logger=Mock(), media_dir=lambda _: media)
|
||||
|
||||
def project():
|
||||
if role == "assistant":
|
||||
return gateway.augment_transcript_media(paths)
|
||||
return gateway.augment_transcript_user_media(paths, [], session_key="websocket:files")
|
||||
|
||||
with monkeypatch.context() as patch:
|
||||
patch.setattr(type(first), "read_bytes", lambda *_: pytest.fail("projection read file contents"))
|
||||
attachments = project()
|
||||
assert [item["name"] for item in attachments] == ["first.json", "missing.txt", "last.pdf"]
|
||||
assert attachments[1] == {"kind": "file", "name": "missing.txt", "url": ""}
|
||||
sig, payload = attachments[0]["url"].split("/")[-2:]
|
||||
assert bytes(gateway.serve_signed_media(sig, payload).body) == b'{"synthetic":true}'
|
||||
assert attachments[2]["url"]
|
||||
|
||||
last.unlink()
|
||||
refreshed = project()
|
||||
assert [item["name"] for item in refreshed] == [item["name"] for item in attachments]
|
||||
assert refreshed[2]["url"] == ""
|
||||
@@ -184,17 +184,16 @@ def signed_media_attachments(
|
||||
*,
|
||||
sign_path: SignedMediaPath,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Map persisted media paths to WebUI attachment dicts with fresh signed URLs."""
|
||||
"""Keep every attachment in order, signing only files available at projection time."""
|
||||
out: list[dict[str, Any]] = []
|
||||
for pstr in paths:
|
||||
path = Path(pstr)
|
||||
att = sign_path(path)
|
||||
if att is None:
|
||||
continue
|
||||
url = att.get("url")
|
||||
if not url:
|
||||
continue
|
||||
name = att.get("name") or path.name
|
||||
try:
|
||||
att = sign_path(path) if path.is_file() else None
|
||||
except OSError:
|
||||
att = None
|
||||
name = (att.get("name") if att else None) or path.name
|
||||
url = (att.get("url") if att else None) or ""
|
||||
out.append({"kind": media_attachment_kind(name), "url": url, "name": name})
|
||||
return out
|
||||
|
||||
|
||||
@@ -96,9 +96,14 @@ class WebUIMediaGateway:
|
||||
media: list[dict[str, Any]] = []
|
||||
for raw in paths:
|
||||
path = Path(raw)
|
||||
if raw in local:
|
||||
url = self.local_attachment_url(raw, session_key=session_key) if path.is_file() else ""
|
||||
else:
|
||||
url = sign_media_path(path, secret=self.secret, media_dir=self._media_dir) or ""
|
||||
try:
|
||||
if not path.is_file():
|
||||
url = ""
|
||||
elif raw in local:
|
||||
url = self.local_attachment_url(raw, session_key=session_key)
|
||||
else:
|
||||
url = sign_media_path(path, secret=self.secret, media_dir=self._media_dir) or ""
|
||||
except OSError:
|
||||
url = ""
|
||||
media.append({"kind": media_attachment_kind(path.name), "name": path.name, "url": url})
|
||||
return media
|
||||
Reference in new issue
Block a user