Skip to content

The agent that grows with you

fix(cli): a native image turn (vision model) reaches the gateway as staged attachments

Teknium3e926f9bc21d
diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.py
index 268a838fc11..67b0faaf321 100644
--- a/hermes_cli/gateway_agent.py
+++ b/hermes_cli/gateway_agent.py
@@ -378,8 +378,17 @@ class GatewayAgent:
# -- the turn ------------------------------------------------------------------------------
def _submit_params(self, user_message, persist_user_message, turn_author):
+ attachments = None
+ if isinstance(user_message, list):
+ # Main's native image turn: OpenAI content parts built from local files. The gateway takes
+ # text plus staged attachments and routes the images itself (native or described).
+ user_message, attachments = self._stage_content_parts(user_message)
+ if isinstance(persist_user_message, list):
+ persist_user_message = user_message
text = persist_user_message if isinstance(persist_user_message, str) else user_message
params = {"session_id": self.session_id, "submission_id": uuid.uuid4().hex, "text": text}
+ if attachments:
+ params["attachments"] = attachments
if isinstance(user_message, str) and user_message != text:
params["api_text"] = user_message
if turn_author:
@@ -393,6 +402,25 @@ class GatewayAgent:
params["finite"] = True
return params
+ def _stage_content_parts(self, parts):
+ """``[{type: text}, {type: image_url, image_url: {url: data:...}}]`` -> (text, attachments): each
+ image is staged on the owner with ``image.attach_bytes`` (bytes read here, locally, by main's
+ ``build_native_content_parts``); main's ``[Image attached at: <local path>]`` hints are dropped
+ because the gateway names its own staged copies."""
+ texts, attachments = [], []
+ for part in parts:
+ if not isinstance(part, dict):
+ continue
+ if part.get("type") == "text":
+ lines = str(part.get("text") or "").splitlines()
+ texts.append("\n".join(ln for ln in lines if not ln.startswith("[Image attached")).strip())
+ elif part.get("type") == "image_url":
+ url = (part.get("image_url") or {}).get("url") if isinstance(part.get("image_url"), dict) else None
+ if isinstance(url, str) and url.startswith("data:image/"):
+ staged = self.connection.call("image.attach_bytes", session_id=self.session_id, content_base64=url)
+ attachments.append({"path": staged["path"], "mime": staged["mime"]})
+ return "\n\n".join(t for t in texts if t), attachments
+
def run_conversation(self, user_message, conversation_history=None, stream_callback=None,
task_id=None, persist_user_message=None, turn_author=None, **_kw):
history = list(conversation_history or [])
diff --git a/tests/hermes_cli/test_gateway_agent.py b/tests/hermes_cli/test_gateway_agent.py
index 4a38016271e..afc46a3384f 100644
--- a/tests/hermes_cli/test_gateway_agent.py
+++ b/tests/hermes_cli/test_gateway_agent.py
@@ -517,3 +517,33 @@ def test_first_turn_on_a_rebuilt_gateway_agent_keeps_the_exact_figure(make_agent
agent.run_conversation("who are you", conversation_history=[{"role": "user", "content": "a"},
{"role": "assistant", "content": "b"}])
assert agent._turn_base_usage_anchor["base_count"] == 3 # anchored after the ask: no estimated delta
+
+
+def test_native_image_parts_are_staged_and_submitted_as_attachments(make_agent):
+ """Main hands run_conversation OpenAI content parts for a vision model (images read locally);
+ prompt.submit takes text + attachments, so each image is staged with image.attach_bytes first."""
+ from agent.image_routing import build_native_content_parts
+ import base64
+ import tempfile
+ from pathlib import Path
+ png = base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNg+M8AAAICAQB7CYF4AAAAAElFTkSuQmCC")
+ with tempfile.TemporaryDirectory(dir=Path(__file__).parent) as tmp:
+ path = Path(tmp) / "pixel.png"
+ path.write_bytes(png)
+ parts, skipped = build_native_content_parts("what is this?", [str(path)])
+ assert not skipped
+ agent, client = make_agent([frame("message.complete", {"text": "a pixel", "outcome": "completed"})])
+ original = client.rpc
+
+ async def rpc(method, *, _timeout=None, **params):
+ if method == "image.attach_bytes":
+ client.calls.append((method, params))
+ return {"attached": True, "path": "/owner/cache/images/upload_1.png", "mime": "image/png"}
+ return await original(method, _timeout=_timeout, **params)
+ client.rpc = rpc
+ agent.run_conversation(parts, persist_user_message=None)
+ staged = [p for m, p in client.calls if m == "image.attach_bytes"]
+ assert len(staged) == 1 and staged[0]["content_base64"].startswith("data:image/png;base64,")
+ submit = dict(client.calls)["prompt.submit"]
+ assert submit["text"] == "what is this?"
+ assert submit["attachments"] == [{"path": "/owner/cache/images/upload_1.png", "mime": "image/png"}]