Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
better logging and timeout handling
Alishahryar1a0fae1a7f218
diff --git a/.gitignore b/.gitignoreindex 0058b85e..dd2e5477 100644--- a/.gitignore+++ b/.gitignore@@ -33,6 +33,7 @@ ENV/# Logs*.log+*.jsonl# IDE specific files.idea/diff --git a/providers/logging_utils.py b/providers/logging_utils.pynew file mode 100644index 00000000..0b987ccd--- /dev/null+++ b/providers/logging_utils.py@@ -0,0 +1,174 @@+"""Logging utilities for compact, traceable request logging.++Provides fingerprinting and summary functions to reduce log file sizes+while maintaining full traceability through request IDs and content hashes.+"""++import hashlib+import json+import logging+import os+from datetime import datetime+from logging.handlers import RotatingFileHandler+from typing import Any, Dict, List, Optional, Union++logger = logging.getLogger(__name__)++# Separate debug file handler for full payloads+_debug_handler: Optional[RotatingFileHandler] = None+++def _get_debug_handler() -> RotatingFileHandler:+ """Get or create the rotating debug file handler."""+ global _debug_handler+ if _debug_handler is None:+ _debug_handler = RotatingFileHandler(+ "server_debug.jsonl",+ maxBytes=10 * 1024 * 1024, # 10MB+ backupCount=3,+ encoding="utf-8",+ )+ _debug_handler.setLevel(logging.DEBUG)+ return _debug_handler+++def generate_request_fingerprint(messages: List[Any]) -> str:+ """Generate unique short hash for message content.++ Creates a SHA256 hash of all message content, returning an 8-char prefix+ that's sufficient for correlation without full content logging.+ """+ content_parts = []+ for msg in messages:+ if hasattr(msg, "content"):+ content = msg.content+ if isinstance(content, str):+ content_parts.append(content)+ elif isinstance(content, list):+ for block in content:+ if hasattr(block, "text"):+ content_parts.append(block.text)+ elif hasattr(block, "type"):+ content_parts.append(f"<{block.type}>")+ elif hasattr(msg, "role"):+ content_parts.append(msg.role)++ combined = "|".join(content_parts)+ hash_digest = hashlib.sha256(combined.encode("utf-8")).hexdigest()+ return f"fp_{hash_digest[:8]}"+++def get_last_user_message_preview(messages: List[Any], max_len: int = 100) -> str:+ """Extract a preview of the last user message."""+ for msg in reversed(messages):+ if hasattr(msg, "role") and msg.role == "user":+ content = msg.content+ if isinstance(content, str):+ preview = content.replace("\n", " ").replace("\r", "")+ return preview[:max_len] + "..." if len(preview) > max_len else preview+ elif isinstance(content, list):+ text_parts = []+ for block in content:+ if hasattr(block, "text"):+ text_parts.append(block.text)+ if text_parts:+ preview = " ".join(text_parts).replace("\n", " ")+ return (+ preview[:max_len] + "..." if len(preview) > max_len else preview+ )+ return "(no user message)"+++def get_tool_names(tools: Optional[List[Any]], max_count: int = 5) -> List[str]:+ """Extract tool names from tool list, limiting to max_count."""+ if not tools:+ return []+ names = []+ for tool in tools[:max_count]:+ if hasattr(tool, "name"):+ names.append(tool.name)+ elif isinstance(tool, dict) and "name" in tool:+ names.append(tool["name"])+ if len(tools) > max_count:+ names.append(f"+{len(tools) - max_count} more")+ return names+++def build_request_summary(request_data: Any) -> Dict[str, Any]:+ """Build compact metadata dict for logging.++ Returns a dictionary with key metrics about the request without+ including the full content.+ """+ messages = getattr(request_data, "messages", [])+ tools = getattr(request_data, "tools", None)+ system = getattr(request_data, "system", None)+ thinking = getattr(request_data, "thinking", None)++ # Count message types+ user_count = sum(1 for m in messages if getattr(m, "role", None) == "user")+ assistant_count = sum(+ 1 for m in messages if getattr(m, "role", None) == "assistant"+ )++ return {+ "fingerprint": generate_request_fingerprint(messages),+ "model": getattr(request_data, "model", "unknown"),+ "message_count": len(messages),+ "user_msgs": user_count,+ "assistant_msgs": assistant_count,+ "user_preview": get_last_user_message_preview(messages),+ "tool_count": len(tools) if tools else 0,+ "tool_names": get_tool_names(tools),+ "has_thinking": bool(thinking and getattr(thinking, "enabled", False)),+ "has_system": bool(system),+ "max_tokens": getattr(request_data, "max_tokens", 0),+ }+++def log_full_payload(request_id: str, payload: Dict[str, Any]) -> None:+ """Write full payload to separate debug file for forensic analysis.++ Only writes if LOG_FULL_PAYLOADS env var is set to 'true'.+ """+ if os.getenv("LOG_FULL_PAYLOADS", "false").lower() != "true":+ return++ try:+ handler = _get_debug_handler()+ record = {+ "timestamp": datetime.utcnow().isoformat(),+ "request_id": request_id,+ "payload": payload,+ }+ handler.stream.write(json.dumps(record, default=str) + "\n")+ handler.stream.flush()+ except Exception as e:+ logger.warning(f"Failed to write debug payload: {e}")+++def log_request_compact(+ logger_instance: logging.Logger,+ request_id: str,+ request_data: Any,+ prefix: str = "API_REQUEST",+) -> None:+ """Log a compact request summary with fingerprint for correlation.++ This is the main entry point for logging requests. It logs a single-line+ JSON summary to the main log and optionally writes full payload to debug file.+ """+ summary = build_request_summary(request_data)+ summary["request_id"] = request_id++ logger_instance.info(f"{prefix}: {json.dumps(summary)}")++ # Optionally write full payload to debug file+ if os.getenv("LOG_FULL_PAYLOADS", "false").lower() == "true":+ try:+ payload = (+ request_data.model_dump() if hasattr(request_data, "model_dump") else {}+ )+ log_full_payload(request_id, payload)+ except Exception as e:+ logger.debug(f"Could not dump request data: {e}")diff --git a/providers/nvidia_nim.py b/providers/nvidia_nim.pyindex 03ca9bb6..71171fa0 100644--- a/providers/nvidia_nim.py+++ b/providers/nvidia_nim.py@@ -7,6 +7,7 @@ import uuidfrom typing import Dict, Any, AsyncIteratorimport httpx+from httpx import TimeoutException, ReadTimeout, ConnectTimeoutfrom .base import BaseProvider, ProviderConfigfrom .utils import (@@ -112,8 +113,10 @@ class NvidiaNimProvider(BaseProvider):await self._rate_limiter.acquire()body = self._build_request_body(request, stream=True)- # Log the converted body for context verification- logger.debug(f"STREAM REQUEST BODY: {json.dumps(body, default=str)}")+ # Log compact request summary+ logger.info(+ f"NIM_STREAM: model={body.get('model')} msgs={len(body.get('messages', []))} tools={len(body.get('tools', []))}"+ )headers = {"Authorization": f"Bearer {self._api_key}",@@ -202,8 +205,15 @@ class NvidiaNimProvider(BaseProvider):for event in self._process_tool_call(tc, sse):yield event+ except TimeoutException as e:+ timeout_type = "connect" if isinstance(e, ConnectTimeout) else "read"+ logger.error(f"NIM_TIMEOUT: type={timeout_type} model={body.get('model')}")+ error_occurred = True+ error_message = (+ f"⏱️ API Timeout ({timeout_type}): Request exceeded time limit"+ )except Exception as e:- logger.error(f"Streaming error: {e}")+ logger.error(f"NIM_ERROR: {type(e).__name__}: {e}")error_occurred = Trueerror_message = str(e)@@ -372,17 +382,28 @@ class NvidiaNimProvider(BaseProvider):await self._rate_limiter.acquire()body = self._build_request_body(request, stream=False)- # Log the converted body for context verification- logger.debug(f"COMPLETE REQUEST BODY: {json.dumps(body, default=str)}")+ # Log compact request summary+ logger.info(+ f"NIM_COMPLETE: model={body.get('model')} msgs={len(body.get('messages', []))} tools={len(body.get('tools', []))}"+ )headers = {"Authorization": f"Bearer {self._api_key}","Content-Type": "application/json",}- response = await self._client.post(- f"{self._base_url}/chat/completions", headers=headers, json=body- )+ try:+ response = await self._client.post(+ f"{self._base_url}/chat/completions", headers=headers, json=body+ )+ except TimeoutException as e:+ timeout_type = "connect" if isinstance(e, ConnectTimeout) else "read"+ logger.error(f"NIM_TIMEOUT: type={timeout_type} model={body.get('model')}")+ raise APIError(+ f"API Timeout ({timeout_type}): Request exceeded time limit",+ status_code=504,+ )+if response.status_code != 200:raise self._map_error(response_status=response.status_code, error_text=response.textdiff --git a/server.py b/server.pyindex 8a2af8de..f3cc3f7b 100644--- a/server.py+++ b/server.py@@ -27,6 +27,7 @@ from fastapi.responses import StreamingResponse, JSONResponseimport tiktokenfrom providers.claude_cli import CLIParserfrom providers.cli_session_manager import CLISessionManager+from providers.logging_utils import log_request_compact# Optional: telethon for the bottry:@@ -591,7 +592,10 @@ def register_bot_handlers(client: "TelegramClient"):elif parsed["type"] == "error":error_msg = parsed.get("message", "Unknown error")- message_parts.append(("error", f"**CLI Error:** {error_msg}"))+ if "timeout" in error_msg.lower():+ message_parts.append(("error", f"⏱️ {error_msg}"))+ else:+ message_parts.append(("error", f"**CLI Error:** {error_msg}"))await update_bot_ui("❌ **Error**", force=True)except asyncio.CancelledError:@@ -933,47 +937,7 @@ def get_token_count(messages, system=None, tools=None) -> int:return max(1, total_tokens)-def log_request_details(request_data: MessagesRequest):- """Log detailed request content for debugging."""-- def sanitize(text: str, max_len: int = 200) -> str:- """Escape newlines and truncate for single-line logging."""- text = text.replace("\n", "\\n").replace("\r", "\\r")- return text[:max_len] + "..." if len(text) > max_len else text-- for i, msg in enumerate(request_data.messages):- role = msg.role- if isinstance(msg.content, str):- logger.debug(f" [{i}] {role}: {sanitize(msg.content)}")- elif isinstance(msg.content, list):- text_acc = []- for block in msg.content:- block_type = getattr(block, "type", None)- if block_type == "text":- text_acc.append(getattr(block, "text", ""))- else:- if text_acc:- logger.debug(- f" [{i}] {role}/text: {sanitize(''.join(text_acc))}"- )- text_acc = []- if block_type == "tool_use":- name = getattr(block, "name", "unknown")- inp = getattr(block, "input", {})- logger.debug(- f" [{i}] {role}/tool_use: {name}({sanitize(json.dumps(inp), 500)})"- )- elif block_type == "tool_result":- content = getattr(block, "content", "")- tool_use_id = getattr(block, "tool_use_id", "unknown")- logger.debug(- f" [{i}] {role}/tool_result[{tool_use_id}]: {sanitize(str(content))}"- )- elif block_type == "thinking":- thinking = getattr(block, "thinking", "")- logger.debug(f" [{i}] {role}/thinking: {sanitize(thinking)}")- if text_acc:- logger.debug(f" [{i}] {role}/text: {sanitize(''.join(text_acc))}")+# log_request_details removed - now using log_request_compact from logging_utils@app.post("/v1/messages")@@ -996,10 +960,10 @@ async def create_message(usage=Usage(input_tokens=100, output_tokens=5),)- logger.info(- f"Request: model={request_data.model}, messages={len(request_data.messages)}, stream={request_data.stream}"- )- log_request_details(request_data)+ import uuid as uuid_mod++ request_id = f"req_{uuid_mod.uuid4().hex[:12]}"+ log_request_compact(logger, request_id, request_data)if request_data.stream:input_tokens = get_token_count(diff --git a/wrapper/cli_session.py b/wrapper/cli_session.pyindex 6c4eea9f..3b66d3b2 100644--- a/wrapper/cli_session.py+++ b/wrapper/cli_session.py@@ -55,8 +55,11 @@ class CLISession:for d in self.allowed_dirs:cmd.extend(["--add-dir", os.path.normpath(d)])+ prompt_preview = prompt[:100].replace("\n", " ") + (+ "..." if len(prompt) > 100 else ""+ )logger.info(- f"Launching Claude CLI in {normalized_workspace} with API {self.api_url}"+ f'CLI_TASK: workspace={normalized_workspace} prompt_len={len(prompt)} preview="{prompt_preview}"')self.process = await asyncio.create_subprocess_exec(@@ -85,7 +88,7 @@ class CLISession:if newline_pos == -1:breakline = buffer[:newline_pos]- buffer = buffer[newline_pos + 1:]+ buffer = buffer[newline_pos + 1 :]line_str = line.decode("utf-8", errors="replace").strip()if line_str:async for event in self._handle_line_gen(line_str):