fix(session): replay user images from stored attachments (#6720)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-19 15:53:57 +08:00
1 parent 488b657ee1
commit 3025a01cf0
29 files changed
+456 -281

No files matched your search

+10 -12
View File
@@ -19,7 +19,7 @@ from nanobot.runtime_context import (
from nanobot.security.workspace_access import WorkspaceScopeResolver
from nanobot.session.manager import Session
from nanobot.session.summary import SessionSummary
from nanobot.utils.helpers import detect_image_mime, image_placeholder_text, load_bundled_template
from nanobot.utils.helpers import detect_image_mime, load_bundled_template
from nanobot.utils.prompt_templates import render_template
@@ -354,28 +354,26 @@ class ContextBuilder:
image_paths: list[str] | None,
) -> str | list[dict[str, Any]]:
"""Build user message content from prefiltered image paths."""
if not image_paths:
return text
media_blocks: list[dict[str, Any]] = []
for path in image_paths:
for path in image_paths or ():
p = Path(path)
if not p.is_file():
try:
if not p.is_file():
continue
raw = p.read_bytes()
except OSError:
continue
raw = p.read_bytes()
# Re-detect from the bytes used for the request: the file may have
# changed since attachment routing, and the data URL needs its MIME.
mime = detect_image_mime(raw) or mimetypes.guess_type(path)[0]
if not mime or not mime.startswith("image/"):
continue
b64 = base64.b64encode(raw).decode()
media_blocks.append({"type": "text", "text": image_placeholder_text(str(p))})
media_blocks.append({
"type": "image_url",
"image_url": {"url": f"data:{mime};base64,{b64}"},
"_meta": {"path": str(p)},
})
if not media_blocks:
return text
return media_blocks + [{"type": "text", "text": text}]
if text:
media_blocks.append({"type": "text", "text": text})
return media_blocks
+34 -26
View File
@@ -86,6 +86,7 @@ from nanobot.security.workspace_access import (
from nanobot.session import turn_continuation
from nanobot.session.automation_turns import automation_history_overrides
from nanobot.session.history_visibility import HIDDEN_HISTORY_META
from nanobot.session.image_attachments import persist_user_image_blocks
from nanobot.session.manager import SESSION_CACHE_MAX_SIZE, Session, SessionManager, SessionPolicy
from nanobot.session.message_metadata import input_history_metadata, persisted_message
from nanobot.session.model_selection import (
@@ -110,7 +111,7 @@ from nanobot.session.summary import (
SessionSummaryCheckpoint,
)
from nanobot.triggers.local_turns import LocalTriggerTurnCoordinator
from nanobot.utils.cancellation import task_is_cancelling
from nanobot.utils.cancellation import shield_and_drain, task_is_cancelling
from nanobot.utils.document import reference_non_image_attachments
from nanobot.utils.helpers import image_placeholder_text
from nanobot.utils.llm_runtime import LLMRuntime
@@ -748,10 +749,12 @@ class AgentLoop:
if text_override is not None:
text = text_override
extra.update(automation_extra)
text, runtime_context_meta = append_runtime_context(
text,
runtime_context_blocks or (),
)
user_content = await self.runner.prepare_prompt(
self.context.build_user_content, text, image_paths=media_paths,
) if media_paths else text
if isinstance(user_content, list) and session.policy.persist:
user_content = await shield_and_drain(asyncio.to_thread(persist_user_image_blocks, user_content))
content, runtime_context_meta = append_runtime_context(user_content, runtime_context_blocks or ())
if runtime_context_meta is not None:
extra[RUNTIME_CONTEXT_HISTORY_META] = runtime_context_meta
if INBOUND_META_MODEL_OPTIONS in msg.metadata:
@@ -762,7 +765,7 @@ class AgentLoop:
session.metadata.update(SessionModelPreset(
selection.base_preset, ModelOptions.parse(msg.metadata[INBOUND_META_MODEL_OPTIONS]),
).metadata_update())
session.add_message("user", text, **extra)
session.add_message("user", content, **extra)
self._mark_pending_user_turn(session)
followup_id = msg.metadata.get(PENDING_FOLLOWUP_ID_KEY)
if isinstance(followup_id, str) and followup_id:
@@ -776,7 +779,7 @@ class AgentLoop:
return TranscriptInput(
history=ctx.history,
current_message=ctx.msg.content,
media=ctx.msg.media if ctx.kind is TurnKind.USER and ctx.msg.media else None,
media=ctx.msg.media if ctx.kind is TurnKind.USER else None,
session_summary=ctx.pending_summary,
runtime_context_blocks=ctx.runtime_context_blocks,
input_metadata=(
@@ -1058,11 +1061,15 @@ class AgentLoop:
if image_paths:
content, image_paths = reference_non_image_attachments(content, image_paths)
image_paths = image_paths or None
user_content = self.context.build_user_content(content, image_paths=image_paths)
user_content = await self.runner.prepare_prompt(
self.context.build_user_content, content, image_paths=image_paths,
) if image_paths else content
row: dict[str, Any] = {
"role": "user", "content": user_content,
"_meta": input_history_metadata(message),
}
if image_paths:
row["_meta"]["media"] = image_paths
if HIDDEN_HISTORY_META in message.metadata:
row[HIDDEN_HISTORY_META] = message.metadata[HIDDEN_HISTORY_META]
metadata_value = cast(object, message.metadata)
@@ -1117,8 +1124,10 @@ class AgentLoop:
async def _checkpoint_turn(self, session: Session, payload: AgentCheckpoint) -> None:
public_payload: dict[str, Any] = dict(payload)
preceding = Session(key=session.key)
self._save_turn(preceding, payload["preceding_messages"], skip=0)
preceding = Session(key=session.key, policy=session.policy)
await shield_and_drain(asyncio.to_thread(
self._save_turn, preceding, payload["preceding_messages"], skip=0,
))
public_payload["preceding_messages"] = preceding.messages
public_payload["consumed_followup_ids"] = sorted(preceding.consumed_followup_ids)
public_payload.pop("provider_state", None)
@@ -2185,9 +2194,9 @@ class AgentLoop:
stored_state,
runtime.model,
):
current_provider_message = self.context.build_current_message(
ctx.msg.content,
media=ctx.msg.media if ctx.kind is TurnKind.USER and ctx.msg.media else None,
current_provider_message = await self.runner.prepare_prompt(
self.context.build_current_message, ctx.msg.content,
media=ctx.msg.media if ctx.kind is TurnKind.USER else None,
runtime_context_blocks=ctx.runtime_context_blocks,
include_memory=session.policy.persist,
has_cron=(ctx.tools if ctx.tools is not None else self.tools).has("cron"),
@@ -2312,12 +2321,13 @@ class AgentLoop:
turn_continuation.record_iteration_limit(
session.metadata, ctx.stop_reason,
)
self._save_turn(
session, ctx.all_messages, ctx.save_skip,
await shield_and_drain(asyncio.to_thread(
self._save_turn, session, ctx.all_messages, ctx.save_skip,
turn_latency_ms=ctx.turn_latency_ms,
summary_checkpoint=None if ctx.ephemeral else ctx.summary_checkpoint,
input_persisted_early=ctx.input_persisted_early,
)
store_images=ctx.commit_session,
))
if (
not ctx.ephemeral
and ctx.provider_compaction_applied
@@ -2364,7 +2374,7 @@ class AgentLoop:
if ctx.ephemeral and ctx.outbound is not None:
ctx.outbound.metadata["_stop_reason"] = ctx.stop_reason
def _sanitize_persisted_blocks(
def _redact_persisted_tool_images(
self,
content: list[object],
) -> list[object]:
@@ -2422,6 +2432,7 @@ class AgentLoop:
turn_latency_ms: int | None = None,
summary_checkpoint: SessionSummaryCheckpoint | None = None,
input_persisted_early: bool = False,
store_images: bool = True,
) -> None:
declared_tool_call_ids = {
str(tc["id"])
@@ -2484,7 +2495,7 @@ class AgentLoop:
fulfilled_tool_call_ids.add(tool_call_id_str)
# Preserve model-visible text for replay; redact only inline images.
if isinstance(content, list):
filtered = self._sanitize_persisted_blocks(
filtered = self._redact_persisted_tool_images(
cast(list[object], content),
)
if not filtered:
@@ -2493,14 +2504,11 @@ class AgentLoop:
{"type": "text", "text": "[tool result omitted during persistence]"}
]
entry["content"] = filtered
elif role == "user":
if isinstance(content, list):
filtered = self._sanitize_persisted_blocks(
cast(list[object], content),
)
if not filtered:
continue
entry["content"] = filtered
elif role == "user" and isinstance(content, list):
if not content:
continue
if store_images and session.policy.persist:
entry["content"] = persist_user_image_blocks(cast(list[object], content))
entry.setdefault("timestamp", datetime.now().isoformat())
session.messages.append(entry)
if role == "user" and isinstance(followup_id, str) and followup_id:
+6 -19
View File
@@ -25,12 +25,12 @@ from nanobot.providers.base import LLMResponse, ProviderConversationState
from nanobot.providers.conversation_state import ProviderConversationStateController
from nanobot.runtime_context import public_history_messages
from nanobot.session.manager import Session, SessionManager
from nanobot.session.message_content import history_message_text
from nanobot.session.summary import is_summary_checkpoint, session_summary_from_metadata
from nanobot.utils.gitstore import MEMORY_CONTENT_PATHS, MEMORY_WIKI_PATH, GitStore
from nanobot.utils.helpers import (
atomic_write_lines,
build_assistant_message,
content_with_media_breadcrumbs,
ensure_dir,
estimate_prompt_tokens_chain,
strip_think,
@@ -615,11 +615,7 @@ class MemoryStore:
def _format_messages(messages: list[dict[str, Any]]) -> str:
lines: list[str] = []
for message in messages:
content = content_with_media_breadcrumbs(
message.get("role"),
message.get("content", ""),
message.get("media"),
)
content = history_message_text(message)
if not content:
continue
tools_used = message.get("tools_used")
@@ -1002,11 +998,11 @@ class MemoryArchiver:
messages=list(session.messages[:archive_end]),
last_consolidated=session.last_archived,
)
history = prefix.get_history(max_tokens=input_token_budget)
history = prefix.get_text_history(max_tokens=input_token_budget)
archive_history = Session(
key=session.key,
messages=messages,
).get_history()
).get_text_history()
if not archive_history or history[-len(archive_history):] != archive_history:
logger.debug(
"Memory archive cannot replay the full chunk for {}; raw-dumping",
@@ -1137,15 +1133,6 @@ class Consolidator:
persist=persist,
)
@staticmethod
def _full_replay_history(
session: Session,
) -> list[dict[str, Any]]:
"""Return all messages that can reach the next model prompt."""
if not session.messages:
return []
return session.get_history()
def estimate_session_prompt_tokens(
self,
session: Session,
@@ -1153,7 +1140,7 @@ class Consolidator:
runtime: LLMRuntime,
) -> tuple[int, str]:
"""Estimate prompt size from the full replayable session history."""
history = self._full_replay_history(session)
history = session.get_text_history()
channel = session.key.split(":", 1)[0] if ":" in session.key else None
summary = session_summary_from_metadata(
session.metadata,
@@ -1265,7 +1252,7 @@ class Consolidator:
"Idle-session compact for {}: archived={}, visible={}, retained={}, summary={}",
session_key,
len(messages_to_archive),
len(session.get_history()),
len(session.get_text_history()),
len(session.messages),
bool(summary),
)