Skip to content

HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.

feat(media): an imported picture lands on the canvas, in the same call

richard-epsilla902c2c2146fb
diff --git a/gateway/app.py b/gateway/app.py
index c63ce5be..84ac0f9a 100644
--- a/gateway/app.py
+++ b/gateway/app.py
@@ -10292,15 +10292,44 @@ async def post_media_import(hid: str, eid: str, sid: str, request: Request) -> d
if not kind:
raise uhp_error(415, "not_media",
"That file is not a picture, a clip or a sound this can read.")
+ label = (request.headers.get("x-media-label") or "")[:200]
try:
rec = await _media_store(sid, kind, data, {
- "model": "", "capability": "import", "job_id": "",
- "prompt": (request.headers.get("x-media-label") or "")[:200]})
+ "model": "", "capability": "import", "job_id": "", "prompt": label})
except media_plane.MediaError as e:
raise uhp_error(400, "import_refused", str(e)) from e
+
+ # ON THE CANVAS, IN THE SAME CALL. Media that is only in the store is invisible: the agent's
+ # one view of a video is describe_canvas, and it said so itself when a reference was imported
+ # and not placed — "it exists as media but was never put on the board". Doing it here also
+ # removes a race the browser could not win, since the agent reads the canvas within seconds
+ # of the session existing. `?place=0` for a caller that wants the bytes and nothing else.
+ element_id = ""
+ if str(request.query_params.get("place") or "1") not in ("0", "false", "no"):
+ med = rec["media_id"]
+
+ def mutate(scene: dict) -> None:
+ els = scene.get("elements") or []
+ x, y = media_plane.next_free([e for e in els if not e.get("isDeleted")])
+ el = media_plane.media_element(
+ rec["kind"], x=x, y=y, w=media_plane.TILE_W, h=media_plane.TILE_H,
+ media_id=med, status="ready", cap="import", label=label,
+ seconds=float(rec.get("seconds") or 0),
+ width=int(rec.get("width") or 0), height=int(rec.get("height") or 0),
+ media_url=_media_url(hid, str(entry.get("id") or eid), sid, med))
+ if rec["kind"] == "image":
+ scene.setdefault("files", {})[med] = media_plane.file_entry(
+ med, str(rec.get("mime") or "image/png"),
+ _media_url(hid, str(entry.get("id") or eid), sid, med))
+ els.append(el)
+ scene["elements"] = els
+ return el["id"]
+
+ _rev, element_id = await _media_scene_touch(sid, mutate)
+
return {"media_id": rec["media_id"], "kind": rec["kind"], "mime": rec["mime"],
"bytes": rec["bytes"], "width": rec["width"], "height": rec["height"],
- "seconds": rec["seconds"]}
+ "seconds": rec["seconds"], "element_id": element_id or None}
@app.get("/v1/harnesses/{hid}/servers/{eid}/sessions/{sid}/media/{med}")
diff --git a/gateway/tests/test_media_mcp.py b/gateway/tests/test_media_mcp.py
index 38e48ba1..0182f948 100644
--- a/gateway/tests/test_media_mcp.py
+++ b/gateway/tests/test_media_mcp.py
@@ -3207,6 +3207,17 @@ def test_a_picture_can_be_imported_and_then_animated_from(api, client, harness,
assert rec["kind"] == "image" and rec["media_id"].startswith("med_"), rec
assert rec["width"] and rec["height"], rec
+ # AND IT IS ON THE CANVAS. Media that is only in the store is invisible to the agent, whose
+ # one view of a video is describe_canvas — it said exactly that when a reference was imported
+ # and not placed. `?place=0` is the way to ask for the bytes alone.
+ assert rec["element_id"], rec
+ scene = asyncio.run(app._media_scene_read(session))
+ el = next((e for e in scene["elements"] if e.get("id") == rec["element_id"]), None)
+ assert el and (el.get("customData") or {}).get("media", {}).get("mediaId") == rec["media_id"]
+ assert scene["files"].get(rec["media_id"]), "an image needs a file entry to be drawn"
+ quiet = api.post(f"{base}?place=0", content=_still("jpg"))
+ assert quiet.status_code == 200 and quiet.json()["element_id"] is None, quiet.text
+
# It reads back through the ordinary media route, byte for byte.
got = api.get(f"{base}/{rec['media_id']}")
assert got.status_code == 200 and got.content == png