refactor(webui): remove legacy message projection (#5823)
* refactor(webui): defer oversized replay traces as events * fix(webui): type deferred trace index set * refactor(webui): remove legacy message projection * fix(webui): type event protocol boundaries * test(webui): assert canonical thread events
This commit is contained in:
1 parent
756c8bb7ed
commit
0983762fe5
23 files changed
+943
-2839
No files matched your search
@@ -87,6 +87,71 @@ def _visible_messages(raw_messages: object) -> list[SessionMessage]:
|
||||
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 ""
|
||||
|
||||
@@ -152,13 +217,16 @@ class WebuiSessionAccess:
|
||||
if before is None:
|
||||
return matching(load_session_messages())[-limit:]
|
||||
break
|
||||
raw_messages = thread.get("messages")
|
||||
if isinstance(raw_messages, list):
|
||||
page_messages = cast(list[object], raw_messages)
|
||||
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 = matching(page_messages)[-remaining:]
|
||||
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.
|
||||
|
||||
+180
-1265
File diff suppressed because it is too large.
Load diff
@@ -1001,9 +1001,6 @@ class GatewayHTTPHandler:
|
||||
if direction is not None and direction not in {"latest"}:
|
||||
return _http_error(400, "invalid direction")
|
||||
before = _query_first(query, "before")
|
||||
projection = _query_first(query, "projection")
|
||||
if projection is not None and projection not in {"events"}:
|
||||
return _http_error(400, "invalid projection")
|
||||
from nanobot.session.webui_turns import (
|
||||
websocket_turn_id,
|
||||
websocket_turn_transcript_persistence_failed,
|
||||
@@ -1031,7 +1028,6 @@ class GatewayHTTPHandler:
|
||||
"direction": direction,
|
||||
"gateway_instance": self.tokens.instance_id,
|
||||
"limit": limit,
|
||||
"projection": projection,
|
||||
"session_updated_at": (
|
||||
session_metadata.get("updated_at") if session_metadata is not None else None
|
||||
),
|
||||
@@ -1066,7 +1062,6 @@ class GatewayHTTPHandler:
|
||||
limit=limit,
|
||||
direction=direction,
|
||||
before=before,
|
||||
projection=projection or "messages",
|
||||
stats=diagnostics.transcript if diagnostics is not None else None,
|
||||
)
|
||||
if diagnostics is not None:
|
||||
|
||||
Reference in new issue
Block a user