refactor: merge user-role branches in _save_turn

This commit is contained in:
Re-bin
2026-02-27 10:07:22 +00:00
parent 6bdb590028
commit bc558d0592

View File

@@ -444,24 +444,19 @@ class AgentLoop:
from datetime import datetime from datetime import datetime
for m in messages[skip:]: for m in messages[skip:]:
entry = {k: v for k, v in m.items() if k != "reasoning_content"} entry = {k: v for k, v in m.items() if k != "reasoning_content"}
if entry.get("role") == "tool" and isinstance(entry.get("content"), str): role, content = entry.get("role"), entry.get("content")
content = entry["content"] if role == "tool" and isinstance(content, str) and len(content) > self._TOOL_RESULT_MAX_CHARS:
if len(content) > self._TOOL_RESULT_MAX_CHARS: entry["content"] = content[:self._TOOL_RESULT_MAX_CHARS] + "\n... (truncated)"
entry["content"] = content[:self._TOOL_RESULT_MAX_CHARS] + "\n... (truncated)" elif role == "user":
if ( if isinstance(content, str) and content.startswith(ContextBuilder._RUNTIME_CONTEXT_TAG):
entry.get("role") == "user" continue
and isinstance(entry.get("content"), str) if isinstance(content, list):
and entry["content"].startswith(ContextBuilder._RUNTIME_CONTEXT_TAG) entry["content"] = [
): {"type": "text", "text": "[image]"} if (
continue c.get("type") == "image_url"
if entry.get("role") == "user" and isinstance(entry.get("content"), list): and c.get("image_url", {}).get("url", "").startswith("data:image/")
entry["content"] = [ ) else c for c in content
{"type": "text", "text": "[image]"} if ( ]
c.get("type") == "image_url"
and c.get("image_url", {}).get("url", "").startswith("data:image/")
) else c
for c in entry["content"]
]
entry.setdefault("timestamp", datetime.now().isoformat()) entry.setdefault("timestamp", datetime.now().isoformat())
session.messages.append(entry) session.messages.append(entry)
session.updated_at = datetime.now() session.updated_at = datetime.now()