Skip to content

The agent that grows with you

fix(codex): send image attachments natively in app-server turn/start (#51053)

teknium1b056e1f36e7e
diff --git a/agent/transports/codex_app_server_session.py b/agent/transports/codex_app_server_session.py
index 90ce312c39d..604402c3a90 100644
--- a/agent/transports/codex_app_server_session.py
+++ b/agent/transports/codex_app_server_session.py
@@ -101,22 +101,58 @@ def _notification_belongs_to_turn(note: dict, *, thread_id: Optional[str], turn_
)
-def _coerce_turn_input_text(user_input: Any) -> str:
- """Collapse rich content parts into app-server text (``turn/start`` is text-only; images become a marker)."""
+_TEXT_PART_TYPES = frozenset({"text", "input_text"})
+_IMAGE_PART_TYPES = frozenset({"image", "image_url", "input_image"})
+_IMAGE_URL_SCHEMES = ("data:", "http://", "https://")
+
+
+def _image_part_to_turn_input(item: dict) -> Optional[dict]:
+ """Map one Hermes image part onto the app-server ``UserInput`` shape.
+
+ ``turn/start`` accepts ``{type: image, url}`` (data:/http URLs) and ``{type: localImage, path}``
+ natively (protocol schema ``v2/UserInput``), so nothing here is flattened into a text marker.
+ """
+ ref = item.get("image_url") or item.get("url") or item.get("path") or item.get("image")
+ if isinstance(ref, dict):
+ ref = ref.get("url") or ref.get("path")
+ ref = (ref or "").strip() if isinstance(ref, str) else ""
+ if not ref:
+ return None
+ if ref.startswith(_IMAGE_URL_SCHEMES):
+ return {"type": "image", "url": ref}
+ if ref.startswith("file://"):
+ ref = ref[len("file://"):]
+ return {"type": "localImage", "path": ref}
+
+
+def _build_turn_input(user_input: Any) -> tuple[list[dict], str]:
+ """Build the ``turn/start`` ``input`` list plus the text the wire will echo back.
+
+ Text parts stay text; image parts ride natively (#51053 — a text marker in their place left the
+ model blind to the attachment). Returns ``(input_items, submitted_text)``.
+ """
if isinstance(user_input, str):
- return user_input
+ return [{"type": "text", "text": user_input}], user_input
if not isinstance(user_input, list):
- return "" if user_input is None else str(user_input)
- parts: list[str] = []
+ text = "" if user_input is None else str(user_input)
+ return [{"type": "text", "text": text}], text
+ texts: list[str] = []
+ images: list[dict] = []
for item in user_input:
if not isinstance(item, dict):
if item.strip() if isinstance(item, str) else item is not None:
- parts.append(str(item))
- elif item.get("type") in {"text", "input_text"}:
- parts.append(str(item.get("text") or item.get("content") or ""))
- elif item.get("type") in {"image", "image_url", "input_image"}:
- parts.append("[image attached]")
- return "\n\n".join(p for p in parts if p).strip() or "What do you see in this image?"
+ texts.append(str(item))
+ elif item.get("type") in _TEXT_PART_TYPES:
+ texts.append(str(item.get("text") or item.get("content") or ""))
+ elif item.get("type") in _IMAGE_PART_TYPES:
+ mapped = _image_part_to_turn_input(item)
+ if mapped is not None:
+ images.append(mapped)
+ text = "\n\n".join(t for t in texts if t).strip()
+ if not text and images:
+ text = "What do you see in this image?"
+ items: list[dict] = [{"type": "text", "text": text}] if text or not images else []
+ return items + images, text
# Strong credential-failure signals: trusted whether they appear in the primary
@@ -363,10 +399,10 @@ class CodexAppServerSession:
if self._interrupt_event.is_set():
result.interrupted = True
else:
- result.submitted_user_text = _coerce_turn_input_text(user_input)
+ input_items, result.submitted_user_text = _build_turn_input(user_input)
ts = self._request_for(
result, "turn/start",
- {"threadId": self._thread_id, "input": [{"type": "text", "text": result.submitted_user_text}]},
+ {"threadId": self._thread_id, "input": input_items},
"turn/start",
)
if ts is not None:
diff --git a/tests/agent/transports/test_codex_app_server_session.py b/tests/agent/transports/test_codex_app_server_session.py
index a70182a1150..a34cc4a2478 100644
--- a/tests/agent/transports/test_codex_app_server_session.py
+++ b/tests/agent/transports/test_codex_app_server_session.py
@@ -21,7 +21,7 @@ from agent.transports.codex_app_server_session import (
CodexAppServerSession,
_ServerRequestRouting,
_approval_choice_to_codex_decision,
- _coerce_turn_input_text,
+ _build_turn_input,
)
@@ -144,12 +144,25 @@ class TestApprovalChoiceMapping:
class TestTurnInputCoercion:
- def test_list_content_keeps_text_and_marks_images(self):
- text = _coerce_turn_input_text([
+ def test_image_parts_ride_natively_in_turn_start(self):
+ """#51053: image attachments must reach the model as app-server image inputs, not a text marker."""
+ items, text = _build_turn_input([
{"type": "text", "text": "caption"},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,abc"}},
+ {"type": "image_url", "image_url": {"url": "/tmp/shot.png"}},
])
- assert text == "caption\n\n[image attached]"
+ assert items == [
+ {"type": "text", "text": "caption"},
+ {"type": "image", "url": "data:image/png;base64,abc"},
+ {"type": "localImage", "path": "/tmp/shot.png"},
+ ]
+ assert text == "caption"
+
+ def test_image_only_turn_gets_default_prompt_and_plain_text_is_unchanged(self):
+ items, text = _build_turn_input([{"type": "image_url", "image_url": {"url": "https://x/a.png"}}])
+ assert items == [{"type": "text", "text": "What do you see in this image?"}, {"type": "image", "url": "https://x/a.png"}]
+ assert text == "What do you see in this image?"
+ assert _build_turn_input("hi") == ([{"type": "text", "text": "hi"}], "hi")
# ---- lifecycle ----
diff --git a/website/docs/user-guide/features/codex-app-server-runtime.md b/website/docs/user-guide/features/codex-app-server-runtime.md
index 47cd2769d1a..46c2fbbc241 100644
--- a/website/docs/user-guide/features/codex-app-server-runtime.md
+++ b/website/docs/user-guide/features/codex-app-server-runtime.md
@@ -114,6 +114,7 @@ The kanban tools are gated by `HERMES_KANBAN_TASK` env var the dispatcher sets
| `web_search`, `web_extract` | yes | yes (via MCP callback) |
| Browser automation (Camofox/Browserbase) | yes | yes (via MCP callback) |
| `vision_analyze`, `image_generate` | yes | yes (via MCP callback) |
+| Image attachments in the user turn (screenshots, pasted images, `/image`) | yes (native multimodal) | yes — sent natively as app-server image inputs (data/http URLs) or local-image paths, never flattened to a text marker |
| `skill_view`, `skills_list` | yes | yes (via MCP callback) |
| `text_to_speech` | yes | yes (via MCP callback) |
| Codex `shell` (terminal/read/write/search/find/run) | — | yes (Codex built-in) |