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

+5 -3
View File
@@ -161,7 +161,7 @@ def settings_payload(
restart_required_sections: list[str] | None = None,
apply_state: dict[str, Any] | None = None,
config_path: Path | None = None,
include_usage: bool = True,
include_usage: bool = False,
) -> dict[str, Any]:
config = _load_settings_config(config_path)
payload: dict[str, Any] = {
@@ -190,8 +190,10 @@ def settings_payload(
)
def settings_usage_payload(*, config_path: Path | None = None) -> dict[str, Any]:
return system.settings_usage_payload(_load_settings_config(config_path))
def settings_usage_payload(
*, config_path: Path | None = None, details: bool = False,
) -> dict[str, Any]:
return system.settings_usage_payload(_load_settings_config(config_path), details=details)
def update_agent_settings(
+21 -3
View File
@@ -21,6 +21,7 @@ from nanobot.webui import settings_contracts as contracts
from nanobot.webui import settings_models as model_domain
from nanobot.webui import settings_system as system_domain
from nanobot.webui.cli_apps_api import cli_apps_action, cli_apps_payload
from nanobot.webui.http_utils import http_json_response
from nanobot.webui.http_utils import http_response as _http_response
from nanobot.webui.http_utils import is_local_browser_request as _is_local_browser_request
from nanobot.webui.mcp_oauth_api import McpOAuthManager
@@ -312,7 +313,7 @@ class WebUISettingsRouter:
if route == ("root", "settings"):
return await asyncio.to_thread(self._handle_settings)
if route == ("root", "usage"):
return await asyncio.to_thread(self._handle_settings_usage)
return await asyncio.to_thread(self._handle_settings_usage, request)
domain, action = route
if action.startswith("update-") and self._runtime_surface == "native":
@@ -500,8 +501,25 @@ class WebUISettingsRouter:
)
)
def _handle_settings_usage(self) -> Response:
return self._json_response(self.settings.read(settings_usage_payload))
def _handle_settings_usage(self, request: WsRequest) -> Response:
details = contracts.query_first(self._parse_query(request.path), "details")
if details not in (None, "true", "false"):
return self._error_response(400, "details must be true or false")
payload = self.settings.read(settings_usage_payload, details=details == "true")
revision = payload.get("revision")
headers = [("Cache-Control", "private, no-cache")]
if revision:
etag = f'W/"{revision}"'
headers.append(("ETag", etag))
if request.headers.get("If-None-Match") == etag:
return _http_response(
b"", status=304, extra_headers=[*headers, ("Vary", "Accept-Encoding")],
)
return http_json_response(
payload,
accept_encoding=request.headers.get("Accept-Encoding", ""),
extra_headers=headers,
)
def _model_operations(self) -> model_domain.ModelSettingsOperations:
return model_domain.ModelSettingsOperations(
+6 -3
View File
@@ -185,9 +185,12 @@ def system_settings_payload(
return payload
def settings_usage_payload(config: Config) -> dict[str, Any]:
"""Return the lightweight token usage slice for Overview refreshes."""
return llm_usage_payload(timezone_name=config.agents.defaults.timezone)
def settings_usage_payload(config: Config, *, details: bool = False) -> dict[str, Any]:
"""Return only the 30-day usage projection currently displayed."""
return llm_usage_payload(
timezone_name=config.agents.defaults.timezone,
view="models" if details else "overview",
)
def update_agent_system_settings(config: Config, query: QueryParams) -> tuple[bool, bool]: