refactor: inline base64 image stripping in _save_turn
This commit is contained in:
@@ -463,15 +463,20 @@ class AgentLoop:
|
||||
def _save_turn(self, session: Session, messages: list[dict], skip: int) -> None:
|
||||
"""Save new-turn messages into session, truncating large tool results."""
|
||||
from datetime import datetime
|
||||
from nanobot.utils import helpers
|
||||
for m in messages[skip:]:
|
||||
entry = {k: v for k, v in m.items() if k != "reasoning_content"}
|
||||
if entry.get("role") == "tool" and isinstance(entry.get("content"), str):
|
||||
content = entry["content"]
|
||||
if len(content) > self._TOOL_RESULT_MAX_CHARS:
|
||||
entry["content"] = content[: self._TOOL_RESULT_MAX_CHARS] + "\n... (truncated)"
|
||||
if entry.get("role") == "user":
|
||||
entry["content"] = helpers.strip_base64_images(entry["content"])
|
||||
entry["content"] = content[:self._TOOL_RESULT_MAX_CHARS] + "\n... (truncated)"
|
||||
if entry.get("role") == "user" and isinstance(entry.get("content"), list):
|
||||
entry["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())
|
||||
session.messages.append(entry)
|
||||
session.updated_at = datetime.now()
|
||||
|
||||
Reference in New Issue
Block a user