feat(webui): show the full token-usage picture in settings (#6638)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-12 18:13:06 +08:00
1 parent b49218b6a3
commit e6b23c7356
42 files changed
+1116 -342

No files matched your search

+15 -5
View File
@@ -5,6 +5,7 @@ import json
import time
from datetime import date
from types import SimpleNamespace
from unittest.mock import patch
import httpx
import pytest
@@ -1753,7 +1754,7 @@ def test_update_transcription_settings_validates_language(
update_transcription_settings({"language": ["en-US"]})
def test_settings_payload_includes_token_usage_summary(
def test_settings_payload_includes_token_usage_only_when_requested(
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
@@ -1774,7 +1775,12 @@ def test_settings_payload_includes_token_usage_summary(
)
)
payload = settings_payload()
store = get_llm_usage_store()
with patch.object(store, "usage_payload", wraps=store.usage_payload) as aggregate:
assert "usage" not in settings_payload()
aggregate.assert_not_called()
payload = settings_payload(include_usage=True)
aggregate.assert_called_once()
assert payload["usage"]["total_tokens_30d"] == 15
assert payload["usage"]["total_tokens"] == 15
@@ -1808,9 +1814,13 @@ def test_settings_usage_payload_returns_lightweight_token_usage(
payload = settings_usage_payload()
assert payload["total_tokens"] == 22
assert payload["requests_30d"] == 1
assert "agent" not in payload
assert sum(day["total_tokens"] for day in payload["days"]) == 22
assert sum(day["requests"] for day in payload["days"]) == 1
assert set(payload) == {"days", "range_start", "range_end", "revision"}
details = settings_usage_payload(details=True)
assert details["days"] == payload["days"]
assert details["providers_30d"][0]["total_tokens"] == 22
assert details["model_days_30d"][0]["total_tokens"] == 22
def test_update_network_safety_settings_writes_local_service_flag(
+34
View File
@@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
import gzip
import json
import threading
from collections.abc import Awaitable, Callable, Mapping
@@ -1181,3 +1182,36 @@ async def test_mcp_proxy_mutation_persists_reloads_and_keeps_oauth_credentials(
reload.assert_awaited_once()
finally:
await gateway.http.settings_routes.close()
@pytest.mark.asyncio
async def test_usage_query_supports_lazy_details_and_conditional_compressed_responses(monkeypatch):
calls = []
payload = {"days": [{"date": "2026-06-03", "total_tokens": 12}] * 100, "revision": "overview"}
def usage_payload(**kwargs):
calls.append(kwargs["details"])
return {**payload, "revision": "models" if kwargs["details"] else "overview"}
monkeypatch.setattr("nanobot.webui.settings_routes.settings_usage_payload", usage_payload)
router = _router()
path = "/api/settings/usage"
request = SimpleNamespace(path=path, headers=Headers({"Accept-Encoding": "gzip"}))
first = await router.dispatch(None, request, path)
assert first.status_code == 200
assert first.headers["Content-Encoding"] == "gzip"
assert json.loads(gzip.decompress(first.body)) == payload
assert first.headers["ETag"] == 'W/"overview"'
request.headers["If-None-Match"] = first.headers["ETag"]
unchanged = await router.dispatch(None, request, path)
assert unchanged.status_code == 304
assert unchanged.body == b""
request.path = f"{path}?details=true"
detailed = await router.dispatch(None, request, path)
assert detailed.status_code == 200
assert detailed.headers["ETag"] == 'W/"models"'
assert calls == [False, False, True]
request.path = f"{path}?details=invalid"
assert (await router.dispatch(None, request, path)).status_code == 400
unauthorized = _router(authorized=False)
assert (await unauthorized.dispatch(None, request, path)).status_code == 401