feat(agent): add persistent runtime context providers

This commit is contained in:
chengyongru
2026-07-12 00:35:17 +08:00
committed by Xubin Ren
parent 7f8c3453e1
commit f75d3519db
27 changed files with 789 additions and 371 deletions
+21 -2
View File
@@ -15,6 +15,10 @@ from typing import Any
from loguru import logger
from nanobot.config.paths import get_legacy_sessions_dir
from nanobot.runtime_context import (
RUNTIME_CONTEXT_HISTORY_META,
public_history_message,
)
from nanobot.utils.helpers import (
ensure_dir,
estimate_message_tokens,
@@ -93,6 +97,7 @@ def _text_preview(content: Any) -> str:
def _message_preview_text(message: dict[str, Any]) -> str:
"""Session list preview text; subagent inject blobs are shortened for display."""
message = public_history_message(message)
content: Any = message.get("content")
if message.get("injected_event") == "subagent_result" and isinstance(content, str):
content = scrub_subagent_announce_body(content)
@@ -153,6 +158,7 @@ class Session:
*,
max_tokens: int = 0,
extend_to_user: bool = False,
include_runtime_context: bool = True,
) -> list[dict[str, Any]]:
"""Return unconsolidated messages for LLM input.
@@ -187,6 +193,12 @@ class Session:
for message in sliced:
if message.get("_command"):
continue
has_persisted_runtime_context = isinstance(
message.get(RUNTIME_CONTEXT_HISTORY_META),
dict,
)
if not include_runtime_context:
message = public_history_message(message)
content = message.get("content", "")
role = message.get("role")
if role == "assistant" and isinstance(content, str):
@@ -203,7 +215,13 @@ class Session:
)
content = f"{content}\n{breadcrumbs}" if content else breadcrumbs
cli_apps = message.get("cli_apps")
if role == "user" and isinstance(cli_apps, list) and cli_apps and isinstance(content, str):
if (
not has_persisted_runtime_context
and role == "user"
and isinstance(cli_apps, list)
and cli_apps
and isinstance(content, str)
):
cli_lines: list[str] = []
for item in cli_apps[:8]:
if not isinstance(item, dict):
@@ -221,7 +239,8 @@ class Session:
content = f"{content}\n{breadcrumbs}" if content else breadcrumbs
mcp_presets = message.get("mcp_presets")
if (
role == "user"
not has_persisted_runtime_context
and role == "user"
and isinstance(mcp_presets, list)
and mcp_presets
and isinstance(content, str)