feat(tui): unify session history and context

This commit is contained in:
Xubin Ren committed 2026-08-17 20:56:10 +08:00
1 parent b3c3a82075
commit 6301c0ab57
24 files changed
+880 -84

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
"""Read-only projection of the session material available to the agent."""
from __future__ import annotations
from typing import Any, cast
from nanobot.session.manager import Session
from nanobot.utils.helpers import estimate_message_tokens, truncate_text
_SUMMARY_PREVIEW_CHARS = 4_000
def session_context_payload(session: Session) -> dict[str, Any]:
"""Return an explainable view of session replay without building a model prompt.
The final prompt also contains workspace instructions, memory, skills, and a
model-specific token budget. This projection deliberately reports only the
session-owned part: archived summary plus the replayable raw suffix.
"""
replay = session.get_history(max_messages=0, include_runtime_context=False)
raw_summary = session.metadata.get("_last_summary")
summary = ""
summary_preview = ""
summary_at: str | None = None
if isinstance(raw_summary, dict):
summary_data = cast(dict[str, object], raw_summary)
text = summary_data.get("text")
last_active = summary_data.get("last_active")
if isinstance(text, str):
summary = text.strip()
summary_preview = truncate_text(summary, _SUMMARY_PREVIEW_CHARS)
if isinstance(last_active, str):
summary_at = last_active
replay_tokens = sum(estimate_message_tokens(message) for message in replay)
summary_tokens = (
estimate_message_tokens({"role": "system", "content": summary}) if summary else 0
)
return {
"schema_version": 1,
"session_key": session.key,
"total_messages": len(session.messages),
"archived_messages": min(session.last_consolidated, len(session.messages)),
"replay_messages": len(replay),
"estimated_replay_tokens": replay_tokens,
"estimated_summary_tokens": summary_tokens,
"estimated_session_tokens": replay_tokens + summary_tokens,
"archived_summary": summary_preview or None,
"archived_summary_at": summary_at,
}
+23
View File
@@ -97,6 +97,7 @@ from nanobot.webui.session_automations import (
session_automation_jobs,
session_automations_payload,
)
from nanobot.webui.session_context import session_context_payload
from nanobot.webui.session_list_index import (
WEBUI_SESSION_INDEX_INTERNAL_FIELDS,
indexed_workspace_scope,
@@ -678,6 +679,10 @@ class GatewayHTTPHandler:
if m:
return self._handle_webui_thread_get(request, m.group(1))
m = re.match(r"^/api/sessions/([^/]+)/context$", got)
if m:
return await self._handle_session_context_get(request, m.group(1))
m = re.match(r"^/api/sessions/([^/]+)/file-preview$", got)
if m:
return self._handle_file_preview(request, m.group(1))
@@ -692,6 +697,24 @@ class GatewayHTTPHandler:
return None
async def _handle_session_context_get(self, request: WsRequest, key: str) -> Response:
if not self.check_api_token(request):
return _http_error(401, "Unauthorized")
decoded_key = _decode_api_key(key)
if decoded_key is None:
return _http_error(400, "invalid session key")
if not _is_websocket_channel_session_key(decoded_key):
return _http_error(404, "session not found")
if self.session_manager is None:
return _http_error(503, "session manager unavailable")
session = await asyncio.to_thread(
self.session_manager.read_session_snapshot,
decoded_key,
)
if session is None:
return _http_error(404, "session not found")
return _http_json_response(session_context_payload(session))
async def _handle_sessions_list(self, request: WsRequest) -> Response:
if not self.check_api_token(request):
return _http_error(401, "Unauthorized")