feat: 增强日志功能并添加rich依赖

- 添加rich库依赖以改进日志显示
- 在各模块添加详细调试日志,包括消息处理、命令执行和工具调用过程
- 使用RichHandler美化日志输出并抑制第三方库的噪音日志
- 在关键路径添加日志记录,便于问题排查
This commit is contained in:
Yuyao Huang (Sam)
2026-03-28 07:57:24 +08:00
parent 0eb29f2dcc
commit b67e2dd2db
5 changed files with 47 additions and 8 deletions
+24 -3
View File
@@ -83,22 +83,36 @@ class OrchestrationAgent:
async def run(self, user_id: str, text: str) -> str:
"""Process a user message and return the agent's reply."""
active_conv = self._active_conv[user_id]
logger.debug(
"[mailboy] run | user=%s active_conv=%s msg=%r",
user_id, active_conv, text[:120],
)
messages: List[BaseMessage] = (
[SystemMessage(content=self._build_system_prompt(user_id))]
+ self._history[user_id]
+ [HumanMessage(content=text)]
)
logger.debug("[mailboy] history_len=%d", len(self._history[user_id]))
reply = ""
try:
for _ in range(MAX_ITERATIONS):
for iteration in range(MAX_ITERATIONS):
logger.debug("[mailboy] LLM call iteration=%d", iteration)
ai_msg: AIMessage = await self._llm_with_tools.ainvoke(messages)
messages.append(ai_msg)
if not ai_msg.tool_calls:
reply = ai_msg.content or ""
logger.debug("[mailboy] final reply (no tool calls): %r", reply[:200])
break
logger.debug(
"[mailboy] tool_calls=%s",
[(tc["name"], tc["args"]) for tc in ai_msg.tool_calls],
)
for tc in ai_msg.tool_calls:
tool_name = tc["name"]
tool_args = tc["args"]
@@ -107,11 +121,15 @@ class OrchestrationAgent:
tool_obj = _TOOL_MAP.get(tool_name)
if tool_obj is None:
result = f"Unknown tool: {tool_name}"
logger.warning("[mailboy] unknown tool: %s", tool_name)
else:
logger.debug("[mailboy] calling tool %s args=%s", tool_name, tool_args)
try:
result = await tool_obj.arun(tool_args)
except Exception as exc:
result = f"Tool error: {exc}"
logger.error("[mailboy] tool %s error: %s", tool_name, exc)
logger.debug("[mailboy] tool %s result: %r", tool_name, str(result)[:300])
# If a session was just created, record it as the active session
if tool_name == "create_conversation":
@@ -120,7 +138,7 @@ class OrchestrationAgent:
if "conv_id" in data:
self._active_conv[user_id] = data["conv_id"]
logger.info(
"Active session for %s set to %s",
"[mailboy] active session for %s set to %s",
user_id, data["conv_id"],
)
except Exception:
@@ -131,11 +149,14 @@ class OrchestrationAgent:
)
else:
reply = "[Max iterations reached]"
logger.warning("[mailboy] max iterations reached for user=%s", user_id)
except Exception as exc:
logger.exception("Agent error for user %s", user_id)
logger.exception("[mailboy] agent error for user=%s", user_id)
reply = f"[Error] {exc}"
logger.info("[mailboy] user=%s reply=%r", user_id, reply[:200])
# Update history
self._history[user_id].append(HumanMessage(content=text))
self._history[user_id].append(AIMessage(content=reply))