An image Read on a worker (the Orus Mac) showed "Not previewable", or a false "No longer available" for a Mac /private/tmp/claude-501/ path read as an expired lab scratchpad. The transcript already holds the bitmap the model saw, so the hub serves it (GET /api/conversation-image), and the card: - previews the lab's own file when it serves one, else that copy (a cleared scratchpad falls through to it too); worker paths never map onto the lab; - header: Open in a new tab, Copy path, and Original ↗ while the run's worker is online (the hub fetches the path the call read from the worker's new GET /media; a lab file outside the served roots links host-file/raw). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
"""
|
|
``/media`` — an image a run here read, handed to the hub at full size.
|
|
|
|
The hub shows an image ``Read`` from the copy in the transcript (the bitmap
|
|
the model saw, often downscaled); its *Original* link asks this machine for
|
|
the file itself (backend ``conversations/toolimage.py``):
|
|
|
|
GET /media?path=/Users/…/shot.png → the bytes, with their image type
|
|
|
|
The hub only asks for a path a tool call of a conversation it shows read, and
|
|
this side only serves raster images — never a text file, a key, an SVG — up to
|
|
``WORKER_MEDIA_MAX_BYTES``. Refused on the host sidecar: the lab serves its own
|
|
files.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import pathlib
|
|
from typing import Callable
|
|
|
|
from fastapi import APIRouter, Header, HTTPException, Query
|
|
from fastapi.responses import FileResponse
|
|
|
|
from config import IS_WORKER
|
|
|
|
MAX_BYTES = int(os.environ.get("WORKER_MEDIA_MAX_BYTES", 50 * 1024 * 1024))
|
|
TYPES = {".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg",
|
|
".gif": "image/gif", ".webp": "image/webp"}
|
|
|
|
|
|
def resolve(raw: str) -> tuple[pathlib.Path, str] | None:
|
|
"""``(file, media type)`` for an absolute path to an image, else None.
|
|
The link's target decides: a ``shot.png`` pointing at a key isn't one."""
|
|
p = pathlib.Path(os.path.expanduser(raw or ""))
|
|
if not p.is_absolute():
|
|
return None
|
|
try:
|
|
real = p.resolve(strict=True)
|
|
st = real.stat()
|
|
except (OSError, RuntimeError):
|
|
return None
|
|
media = TYPES.get(real.suffix.lower())
|
|
if not media or not real.is_file() or st.st_size > MAX_BYTES:
|
|
return None
|
|
return real, media
|
|
|
|
|
|
def make_router(auth: Callable[[str | None], None]) -> APIRouter:
|
|
router = APIRouter()
|
|
|
|
@router.get("/media")
|
|
def get_media(path: str = Query(...),
|
|
authorization: str | None = Header(default=None)) -> FileResponse:
|
|
auth(authorization)
|
|
if not IS_WORKER:
|
|
raise HTTPException(409, "the hub serves the lab's files itself")
|
|
hit = resolve(path)
|
|
if hit is None:
|
|
raise HTTPException(404, "no image there")
|
|
real, media = hit
|
|
return FileResponse(real, media_type=media,
|
|
headers={"X-Content-Type-Options": "nosniff"})
|
|
|
|
return router
|