refactor(session): decouple history tools from WebUI replay (#6017)
* refactor(session): decouple history tools from WebUI replay * test(oauth): make callback expiry independent of thread scheduling
This commit is contained in:
1 parent
ae6801d3eb
commit
4904305450
9 files changed
+273
-503
No files matched your search
@@ -1,27 +1,18 @@
|
||||
"""Read and validate persisted conversations for WebUI and session tools."""
|
||||
"""Validate WebUI session references against persisted session metadata."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from collections.abc import Mapping
|
||||
from functools import cache
|
||||
from typing import Any, TypedDict, cast
|
||||
|
||||
from nanobot.runtime_context import (
|
||||
RuntimeContextBlock,
|
||||
public_history_message,
|
||||
wrap_runtime_context_lines,
|
||||
)
|
||||
from nanobot.session.history_visibility import is_hidden_history_message
|
||||
from nanobot.session.manager import SessionManager
|
||||
from nanobot.session.session_handles import SessionHandleResolver
|
||||
from nanobot.webui.session_list_index import list_webui_sessions
|
||||
from nanobot.webui.transcript import (
|
||||
build_webui_thread_response,
|
||||
normalize_session_mentions_metadata,
|
||||
)
|
||||
|
||||
_VISIBLE_ROLES = {"user", "assistant"}
|
||||
from nanobot.webui.transcript import normalize_session_mentions_metadata
|
||||
|
||||
|
||||
class SessionMention(TypedDict):
|
||||
@@ -31,127 +22,6 @@ class SessionMention(TypedDict):
|
||||
title: str
|
||||
|
||||
|
||||
class SessionMessage(TypedDict):
|
||||
message_index: int
|
||||
role: str
|
||||
timestamp: str | int | None
|
||||
content: str
|
||||
|
||||
|
||||
class SessionMatch(TypedDict):
|
||||
session_key: str
|
||||
title: str
|
||||
updated_at: str | None
|
||||
messages: list[SessionMessage]
|
||||
|
||||
|
||||
def _message_text(message: Mapping[str, Any]) -> str:
|
||||
content = message.get("content")
|
||||
if isinstance(content, str):
|
||||
return content.strip()
|
||||
if not isinstance(content, list):
|
||||
return ""
|
||||
parts: list[str] = []
|
||||
for raw_block in cast(list[object], content):
|
||||
if not isinstance(raw_block, dict):
|
||||
continue
|
||||
block = cast(dict[object, object], raw_block)
|
||||
text = block.get("text")
|
||||
if block.get("type") == "text" and isinstance(text, str):
|
||||
parts.append(text)
|
||||
return "\n".join(parts).strip()
|
||||
|
||||
|
||||
def _visible_messages(raw_messages: object) -> list[SessionMessage]:
|
||||
if not isinstance(raw_messages, list):
|
||||
return []
|
||||
visible: list[SessionMessage] = []
|
||||
for index, raw_message in enumerate(cast(list[object], raw_messages)):
|
||||
if not isinstance(raw_message, dict):
|
||||
continue
|
||||
message = cast(dict[str, Any], raw_message)
|
||||
role = message.get("role")
|
||||
if role not in _VISIBLE_ROLES or message.get("_command") or is_hidden_history_message(message):
|
||||
continue
|
||||
public = public_history_message(message)
|
||||
text = _message_text(public)
|
||||
if not text:
|
||||
continue
|
||||
timestamp = public.get("createdAt", public.get("timestamp"))
|
||||
visible.append({
|
||||
"message_index": index,
|
||||
"role": cast(str, role),
|
||||
"timestamp": timestamp if isinstance(timestamp, (str, int)) else None,
|
||||
"content": text,
|
||||
})
|
||||
return visible
|
||||
|
||||
|
||||
def _visible_projection_events(raw_events: object) -> list[SessionMessage]:
|
||||
"""Extract searchable conversation text from the event protocol.
|
||||
|
||||
This is intentionally narrower than the WebUI projector: session tools
|
||||
expose only user and assistant answer text, never reasoning or activity.
|
||||
"""
|
||||
if not isinstance(raw_events, list):
|
||||
return []
|
||||
visible: list[SessionMessage] = []
|
||||
stream_parts: list[str] = []
|
||||
stream_timestamp: str | int | None = None
|
||||
|
||||
def append(role: str, text: object, timestamp: object) -> None:
|
||||
content = text.strip() if isinstance(text, str) else ""
|
||||
if not content:
|
||||
return
|
||||
visible.append({
|
||||
"message_index": len(visible),
|
||||
"role": role,
|
||||
"timestamp": timestamp if isinstance(timestamp, (str, int)) else None,
|
||||
"content": content,
|
||||
})
|
||||
|
||||
def flush_stream(final_text: object = None) -> None:
|
||||
nonlocal stream_timestamp
|
||||
text = final_text if isinstance(final_text, str) else "".join(stream_parts)
|
||||
append("assistant", text, stream_timestamp)
|
||||
stream_parts.clear()
|
||||
stream_timestamp = None
|
||||
|
||||
for raw_event in cast(list[object], raw_events):
|
||||
if not isinstance(raw_event, dict):
|
||||
continue
|
||||
event = cast(dict[str, Any], raw_event)
|
||||
name = event.get("event")
|
||||
timestamp = event.get("created_at_ms")
|
||||
if name == "user_message":
|
||||
flush_stream()
|
||||
append("user", event.get("text"), timestamp)
|
||||
elif name == "delta":
|
||||
text = event.get("text")
|
||||
if isinstance(text, str):
|
||||
if not stream_parts:
|
||||
stream_timestamp = timestamp if isinstance(timestamp, (str, int)) else None
|
||||
stream_parts.append(text)
|
||||
elif name == "stream_end":
|
||||
if event.get("resuming") is True and event.get("merge_next") is True:
|
||||
text = event.get("text")
|
||||
if isinstance(text, str):
|
||||
stream_parts[:] = [text]
|
||||
continue
|
||||
flush_stream(event.get("text"))
|
||||
elif name == "message":
|
||||
kind = event.get("kind")
|
||||
if kind in {"tool_hint", "progress", "reasoning"}:
|
||||
flush_stream()
|
||||
continue
|
||||
flush_stream()
|
||||
append("assistant", event.get("text"), timestamp)
|
||||
elif name in {"reasoning_delta", "reasoning_end", "file_edit", "turn_end"}:
|
||||
flush_stream()
|
||||
flush_stream()
|
||||
return visible
|
||||
|
||||
|
||||
def _text(value: object) -> str:
|
||||
return value.strip()[:160] if isinstance(value, str) else ""
|
||||
|
||||
@@ -161,12 +31,8 @@ def _session_metadata(payload: Mapping[str, Any]) -> dict[str, Any]:
|
||||
return cast(dict[str, Any], raw) if isinstance(raw, dict) else {}
|
||||
|
||||
|
||||
def _row_title(row: Mapping[str, Any]) -> str:
|
||||
return _text(row.get("title")) or _text(row.get("preview"))
|
||||
|
||||
|
||||
class WebuiSessionAccess:
|
||||
"""Own listing, validation, and history reads for session references."""
|
||||
"""Validate and normalize session references selected in the WebUI."""
|
||||
|
||||
def __init__(self, sessions: SessionManager) -> None:
|
||||
self._sessions = sessions
|
||||
@@ -182,145 +48,6 @@ class WebuiSessionAccess:
|
||||
return None
|
||||
return self._sessions.read_session_metadata(session_key)
|
||||
|
||||
def _messages(self, session_key: str, *, needle: str, limit: int) -> list[SessionMessage]:
|
||||
@cache
|
||||
def load_session_messages() -> list[dict[str, Any]] | None:
|
||||
payload = self._sessions.read_session_file(session_key)
|
||||
raw_messages = payload.get("messages") if payload is not None else None
|
||||
if not isinstance(raw_messages, list):
|
||||
return []
|
||||
return [
|
||||
cast(dict[str, Any], message)
|
||||
for message in cast(list[object], raw_messages)
|
||||
if isinstance(message, dict)
|
||||
]
|
||||
|
||||
def matching(raw_messages: object) -> list[SessionMessage]:
|
||||
return [
|
||||
message for message in _visible_messages(raw_messages)
|
||||
if not needle or needle in message["content"].casefold()
|
||||
]
|
||||
|
||||
# The WebUI replay API returns one page, even when no limit is given.
|
||||
# Session tools need the older pages too, including turns no longer in
|
||||
# the compacted model history. Keep the UI's per-page replay budgets.
|
||||
matches: list[SessionMessage] = []
|
||||
message_count = 0
|
||||
before: str | None = None
|
||||
while True:
|
||||
thread = build_webui_thread_response(
|
||||
session_key,
|
||||
session_messages_loader=load_session_messages,
|
||||
before=before,
|
||||
)
|
||||
if thread is None:
|
||||
if before is None:
|
||||
return matching(load_session_messages())[-limit:]
|
||||
break
|
||||
raw_events = thread.get("events")
|
||||
if isinstance(raw_events, list):
|
||||
page_messages = _visible_projection_events(cast(list[object], raw_events))
|
||||
message_count += len(page_messages)
|
||||
remaining = limit - len(matches)
|
||||
if remaining > 0:
|
||||
page_matches = [
|
||||
message for message in page_messages
|
||||
if not needle or needle in message["content"].casefold()
|
||||
][-remaining:]
|
||||
for message in page_matches:
|
||||
# Index relative to the conversation's end until we
|
||||
# know the total number of raw messages across pages.
|
||||
message["message_index"] -= message_count
|
||||
matches = page_matches + matches
|
||||
raw_page = thread.get("page")
|
||||
if not isinstance(raw_page, dict):
|
||||
break
|
||||
page = cast(dict[str, Any], raw_page)
|
||||
cursor = page.get("before_cursor")
|
||||
if not page.get("has_more_before") or not isinstance(cursor, str) or cursor == before:
|
||||
break
|
||||
before = cursor
|
||||
|
||||
# Global indexes still require counting older pages, but retain only
|
||||
# the requested matches, never the full conversation's raw traces.
|
||||
for message in matches:
|
||||
message["message_index"] += message_count
|
||||
return matches
|
||||
|
||||
def search(
|
||||
self,
|
||||
query: str,
|
||||
limit: int,
|
||||
*,
|
||||
exclude_session_key: str | None = None,
|
||||
) -> list[SessionMatch]:
|
||||
needle = query.casefold()
|
||||
rows: list[dict[str, Any]] = []
|
||||
for row in list_webui_sessions(self._sessions):
|
||||
key = row.get("key")
|
||||
if isinstance(key, str) and key != exclude_session_key:
|
||||
rows.append(row)
|
||||
ranked: list[tuple[int, SessionMatch]] = []
|
||||
remaining: list[dict[str, Any]] = []
|
||||
for row in rows:
|
||||
title = _row_title(row)
|
||||
folded = title.casefold()
|
||||
rank = (
|
||||
0 if folded == needle
|
||||
else 1 if folded.startswith(needle)
|
||||
else 2 if needle in folded
|
||||
else None
|
||||
)
|
||||
if rank is None:
|
||||
remaining.append(row)
|
||||
continue
|
||||
updated = row.get("updated_at")
|
||||
ranked.append((rank, {
|
||||
"session_key": cast(str, row["key"]),
|
||||
"title": title,
|
||||
"updated_at": updated if isinstance(updated, str) else None,
|
||||
"messages": [],
|
||||
}))
|
||||
|
||||
ranked.sort(key=lambda item: item[0])
|
||||
needed = max(0, limit - len(ranked))
|
||||
for row in remaining:
|
||||
if needed <= 0:
|
||||
break
|
||||
key = cast(str, row["key"])
|
||||
matches = self._messages(key, needle=needle, limit=2)
|
||||
if not matches:
|
||||
continue
|
||||
updated = row.get("updated_at")
|
||||
ranked.append((3, {
|
||||
"session_key": key,
|
||||
"title": _row_title(row),
|
||||
"updated_at": updated if isinstance(updated, str) else None,
|
||||
"messages": matches,
|
||||
}))
|
||||
needed -= 1
|
||||
return [item[1] for item in ranked[:limit]]
|
||||
|
||||
def read(
|
||||
self,
|
||||
session_key: str,
|
||||
*,
|
||||
query: str,
|
||||
limit: int,
|
||||
exclude_session_key: str | None = None,
|
||||
) -> SessionMatch | None:
|
||||
payload = self._metadata(session_key, exclude_session_key=exclude_session_key)
|
||||
if payload is None:
|
||||
return None
|
||||
messages = self._messages(session_key, needle=query.casefold(), limit=limit)
|
||||
updated = payload.get("updated_at")
|
||||
return {
|
||||
"session_key": session_key,
|
||||
"title": _text(_session_metadata(payload).get("title")),
|
||||
"updated_at": updated if isinstance(updated, str) else None,
|
||||
"messages": messages,
|
||||
}
|
||||
|
||||
def normalize_mentions(
|
||||
self,
|
||||
raw: object,
|
||||
@@ -352,6 +79,7 @@ class WebuiSessionAccess:
|
||||
seen_names.add(folded_name)
|
||||
return normalized
|
||||
|
||||
|
||||
def session_mentions_runtime_context(
|
||||
mentions: list[SessionMention],
|
||||
) -> RuntimeContextBlock | None:
|
||||
|
||||
Reference in new issue
Block a user