From e4fc0db07d09cd4eab3cb5bfc367085c8d2dc308 Mon Sep 17 00:00:00 2001 From: csh28 Date: Mon, 11 May 2026 20:29:27 +0800 Subject: [PATCH] chore: improve agent error logging --- agent/logging_handler.py | 7 ++++++- routes/chat.py | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/agent/logging_handler.py b/agent/logging_handler.py index c3e21e7..60aa886 100644 --- a/agent/logging_handler.py +++ b/agent/logging_handler.py @@ -1,6 +1,7 @@ """Logging callback handler module.""" import logging +import traceback from typing import Any, Optional, Dict, List from langchain_core.callbacks import BaseCallbackHandler from langchain_core.messages import BaseMessage @@ -80,4 +81,8 @@ class LoggingCallbackHandler(BaseCallbackHandler): self, error: Exception, **kwargs: Any ) -> None: """Called when a tool invocation raises an error.""" - self.logger.error(f"❌ Tool Error: {error}") + self.logger.error( + "❌ Tool Error: %s\n%s", + repr(error), + "".join(traceback.format_exception(type(error), error, error.__traceback__)), + ) diff --git a/routes/chat.py b/routes/chat.py index 275f6d0..fd5dc50 100644 --- a/routes/chat.py +++ b/routes/chat.py @@ -3,6 +3,7 @@ import os import asyncio import shutil import time +import traceback from typing import Union, Optional, Any, List, Dict from fastapi import APIRouter, HTTPException, Header, Body from fastapi.responses import StreamingResponse @@ -185,9 +186,11 @@ async def enhanced_generate_stream_response( await output_queue.put(("agent_done", None)) except Exception as e: - logger.error(f"Error in agent task: {e}") + logger.error(f"Error in agent task: {e}\n{traceback.format_exc()}") # Send error information to the client - await output_queue.put(("agent", f'data: {{"error": "{str(e)}"}}\n\n')) + await output_queue.put( + ("agent", f"data: {json.dumps({'error': str(e)}, ensure_ascii=False)}\n\n") + ) # Send completion signal to ensure the output controller exits normally await output_queue.put(("agent_done", None))