feat(status): add web search provider usage to /status command

This commit is contained in:
whs
2026-04-06 13:37:55 +08:00
committed by Xubin Ren
parent b2e751f21b
commit bc0ff7f214
5 changed files with 515 additions and 3 deletions
+16
View File
@@ -60,6 +60,21 @@ async def cmd_status(ctx: CommandContext) -> OutboundMessage:
pass
if ctx_est <= 0:
ctx_est = loop._last_usage.get("prompt_tokens", 0)
# Fetch web search provider usage (best-effort, never blocks the response)
search_usage_text: str | None = None
try:
from nanobot.agent.tools.search_usage import fetch_search_usage
web_cfg = getattr(getattr(loop, "config", None), "tools", None)
web_cfg = getattr(web_cfg, "web", None) if web_cfg else None
search_cfg = getattr(web_cfg, "search", None) if web_cfg else None
if search_cfg is not None:
provider = getattr(search_cfg, "provider", "duckduckgo")
api_key = getattr(search_cfg, "api_key", "") or None
usage = await fetch_search_usage(provider=provider, api_key=api_key)
search_usage_text = usage.format()
except Exception:
pass # Never let usage fetch break /status
return OutboundMessage(
channel=ctx.msg.channel,
chat_id=ctx.msg.chat_id,
@@ -69,6 +84,7 @@ async def cmd_status(ctx: CommandContext) -> OutboundMessage:
context_window_tokens=loop.context_window_tokens,
session_msg_count=len(session.get_history(max_messages=0)),
context_tokens_estimate=ctx_est,
search_usage_text=search_usage_text,
),
metadata={**dict(ctx.msg.metadata or {}), "render_as": "text"},
)