fix: log viewer

This commit is contained in:
Krrish Dholakia 2026-02-23 10:05:59 -08:00
parent a693fb05de
commit 7d845cc40a

View file

@ -493,17 +493,6 @@ def _usage_log_entry_from_row(
if hasattr(sl.startTime, "isoformat")
else str(sl.startTime)
)
# #region agent log
try:
_log_path = "/Users/krrishdholakia/Documents/litellm/.cursor/debug-d1cae6.log"
import json as _json
_msg_t = getattr(sl, "messages", None)
_resp_t = getattr(sl, "response", None)
with open(_log_path, "a") as _f:
_f.write(_json.dumps({"sessionId": "d1cae6", "hypothesisId": "H2", "location": "usage_endpoints.py:_usage_log_entry_from_row", "message": "sl.messages and sl.response types", "data": {"messages_type": type(_msg_t).__name__ if _msg_t is not None else "None", "response_type": type(_resp_t).__name__ if _resp_t is not None else "None", "messages_repr": repr(_msg_t)[:120] if _msg_t is not None else None, "response_repr": repr(_resp_t)[:120] if _resp_t is not None else None}, "timestamp": __import__("time").time() * 1000}) + "\n")
except Exception:
pass
# #endregion
return UsageLogEntry(
id=r.request_id,
timestamp=ts,
@ -511,7 +500,7 @@ def _usage_log_entry_from_row(
score=score_val,
latency_ms=latency_val,
model=sl.model,
input_snippet=_snippet(sl.messages),
input_snippet=_input_snippet_for_log(sl),
output_snippet=_snippet(sl.response),
reason=reason_val,
)
@ -534,18 +523,35 @@ def _snippet(text: Any, max_len: int = 200) -> Optional[str]:
else:
s = str(text)
result = (s[:max_len] + "...") if len(s) > max_len else s
# #region agent log
try:
_log_path = "/Users/krrishdholakia/Documents/litellm/.cursor/debug-d1cae6.log"
import json as _json
with open(_log_path, "a") as _f:
_f.write(_json.dumps({"sessionId": "d1cae6", "hypothesisId": "H1", "location": "usage_endpoints.py:_snippet", "message": "snippet input and result", "data": {"text_type": type(text).__name__, "text_repr": repr(text)[:150], "result": result, "result_is_empty_brace": result == "{}"}, "timestamp": __import__("time").time() * 1000}) + "\n")
except Exception:
pass
# #endregion
if result == "{}":
return None
return result
def _input_snippet_for_log(sl: Any) -> Optional[str]:
"""Snippet for request input: prefer messages, fall back to proxy_server_request (same as drawer)."""
out = _snippet(sl.messages)
if out:
return out
psr = getattr(sl, "proxy_server_request", None)
if not psr:
return None
if isinstance(psr, str):
try:
psr = json.loads(psr)
except Exception:
return _snippet(psr)
if isinstance(psr, dict):
msgs = psr.get("messages")
if msgs is None and isinstance(psr.get("body"), dict):
msgs = psr["body"].get("messages")
out = _snippet(msgs)
if out:
return out
return _snippet(psr)
return _snippet(psr)
@router.get(
"/guardrails/usage/logs",
tags=["Guardrails"],