feat(webui): visualize context usage by logical round (#5649)

* feat(webui): visualize per-request context reuse

* refactor(webui): report usage by logical round

* fix(webui): align context chart with logical rounds

* refactor(webui): derive turn usage from logical rounds

* fix(webui): polish context usage details
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-04 15:13:17 +08:00
1 parent 8fb7bc34ec
commit 6b82d642fc
35 files changed
+994 -274

No files matched your search

+28 -7
View File
@@ -74,6 +74,20 @@ _SESSION_MENTION_NAME_RE = re.compile(r"^[\w-]+$")
_SESSION_HANDLE_ID_RE = re.compile(r"^handle_[0-9a-f]{32}$")
def _sanitize_turn_usage(value: object) -> dict[str, int] | None:
if not isinstance(value, dict):
return None
data = cast(dict[object, object], value)
return {
key: item
for key, item in data.items()
if isinstance(key, str)
and isinstance(item, int)
and not isinstance(item, bool)
and item >= 0
}
def rewrite_local_markdown_images(
text: str,
*,
@@ -1916,6 +1930,7 @@ def replay_transcript_to_ui_messages(
*,
latency_ms: int | None = None,
usage: dict[str, int] | None = None,
round_usages: list[dict[str, int]] | None = None,
context_window_tokens: int | None = None,
) -> None:
for i in range(len(messages) - 1, -1, -1):
@@ -1925,6 +1940,8 @@ def replay_transcript_to_ui_messages(
completion["latencyMs"] = latency_ms
if usage:
completion["usage"] = usage
if round_usages:
completion["roundUsages"] = round_usages
if context_window_tokens is not None:
completion["contextWindowTokens"] = context_window_tokens
messages[i] = {
@@ -2428,19 +2445,23 @@ def replay_transcript_to_ui_messages(
messages[i] = {**m, "isStreaming": False}
lat = rec.get("latency_ms")
usage = rec.get("usage")
sanitized_usage = (
{
key: value
for key, value in cast(dict[object, object], usage).items()
if isinstance(key, str) and type(value) is int and value >= 0
}
if isinstance(usage, dict)
sanitized_usage = _sanitize_turn_usage(usage)
raw_round_usages = rec.get("round_usages")
sanitized_round_usages = (
[
sanitized
for item in cast(list[object], raw_round_usages)
if (sanitized := _sanitize_turn_usage(item))
is not None
]
if isinstance(raw_round_usages, list)
else None
)
context_window = rec.get("context_window_tokens")
stamp_completion(
latency_ms=int(lat) if isinstance(lat, (int, float)) and lat >= 0 else None,
usage=sanitized_usage,
round_usages=sanitized_round_usages,
context_window_tokens=(
int(context_window)
if isinstance(context_window, (int, float)) and context_window >= 0