Skip to content

The agent that grows with you

refactor(tui_gateway): insights.get guard, undo count parse, quick-command output join, Counter role tally

Teknium8b25a8ae096b
diff --git a/tui_gateway/methods_slash.py b/tui_gateway/methods_slash.py
index fca69349f49..41b0af0eab2 100644
--- a/tui_gateway/methods_slash.py
+++ b/tui_gateway/methods_slash.py
@@ -87,11 +87,9 @@ def _live_session_messages(session: dict) -> Optional[list]:
profile's state.db, and through the launch handle this read comes back empty."""
with _session_db(session) as db:
if db is not None and session.get("session_key"):
- try:
+ with contextlib.suppress(Exception):
return db.get_messages_as_conversation(
session["session_key"], include_ancestors=True, include_row_ids=True)
- except Exception:
- pass
return None
@@ -129,6 +127,7 @@ def _format_live_prompt_output(sid: str, session: dict, arg: str) -> str:
def _format_live_context_output(sid: str, session: dict, arg: str) -> str:
+ from collections import Counter
try:
messages = _history_to_messages(_live_session_messages(session) or [])
except Exception:
@@ -139,10 +138,7 @@ def _format_live_context_output(sid: str, session: dict, arg: str) -> str:
usage = _session_usage_snapshot(session)
mirror = _metadata_mirror(session)
lines = [f"Conversation: {len(messages)} messages" if messages else "Conversation is empty (no messages yet)."]
- roles: dict[str, int] = {}
- for msg in messages:
- role = str(msg.get("role") or "unknown")
- roles[role] = roles.get(role, 0) + 1
+ roles = Counter(str(msg.get("role") or "unknown") for msg in messages)
lines.append(
f" user: {roles.get('user', 0)}, assistant: {roles.get('assistant', 0)}, "
f"tool: {roles.get('tool', 0)}, system: {roles.get('system', 0)}")
diff --git a/tui_gateway/methods_tools.py b/tui_gateway/methods_tools.py
index a1b9eaf88c3..26cb19433ba 100644
--- a/tui_gateway/methods_tools.py
+++ b/tui_gateway/methods_tools.py
@@ -496,7 +496,7 @@ def _dispatch_quick(rid, params, session, name, arg):
from tools.environments.local import build_subprocess_env
sanitized_env = build_subprocess_env()
r = subprocess.run(qc.get("command", ""), shell=True, env=sanitized_env, **_capture_run_kwargs(30))
- output = ((r.stdout or "") + ("\n" if r.stdout and r.stderr else "") + (r.stderr or "")).strip()[:4000]
+ output = "\n".join(p for p in (r.stdout or "", r.stderr or "") if p).strip()[:4000]
if output:
from agent.redact import redact_sensitive_text
output = redact_sensitive_text(output)
@@ -800,14 +800,11 @@ def _cmd_undo(rid, params, session, name, arg):
session_key = session.get("session_key", "")
if not session_key:
return _err(rid, 4001, "no session key for undo")
- n = 1
arg_str = (arg or "").strip()
- if arg_str:
- try:
- n = int(arg_str.split()[0])
- except (ValueError, IndexError):
- return _err(rid, 4004, f"undo: invalid count {arg_str!r} — use /undo or /undo N")
- n = max(n, 1)
+ try:
+ n = max(int(arg_str.split()[0]), 1) if arg_str else 1
+ except (ValueError, IndexError):
+ return _err(rid, 4004, f"undo: invalid count {arg_str!r} — use /undo or /undo N")
from agent.message_content import flatten_message_text
with session["history_lock"]:
if busy := _busy_error(rid, session, "undo"):
@@ -971,17 +968,15 @@ def _(rid, params: dict) -> dict:
@method("insights.get")
+@_guarded(5017)
def _(rid, params: dict) -> dict:
days = params.get("days", 30)
db = _get_db()
if db is None:
return _db_unavailable_error(rid, code=5017)
- try:
- cutoff = time.time() - days * 86400
- rows = [s for s in db.list_sessions_rich(limit=500, compact_rows=True) if (s.get("started_at") or 0) >= cutoff]
- return _ok(rid, {"days": days, "sessions": len(rows), "messages": sum(s.get("message_count", 0) for s in rows)})
- except Exception as e:
- return _err(rid, 5017, str(e))
+ cutoff = time.time() - days * 86400
+ rows = [s for s in db.list_sessions_rich(limit=500, compact_rows=True) if (s.get("started_at") or 0) >= cutoff]
+ return _ok(rid, {"days": days, "sessions": len(rows), "messages": sum(s.get("message_count", 0) for s in rows)})
@method("rollback.list")