diff --git a/backend/app/utils/logging.py b/backend/app/utils/logging.py index c3137fc..7802895 100644 --- a/backend/app/utils/logging.py +++ b/backend/app/utils/logging.py @@ -5,37 +5,27 @@ LangGraph 节点日志工具模块 from app.config import ENABLE_GRAPH_TRACE from app.logger import debug, info +from app.main_graph.state import MainGraphState -def log_state_change(node_name: str, state, prefix: str = "进入"): +def log_state_change(node_name: str, state: MainGraphState, prefix: str = "进入"): """ 记录状态变化日志 - + Args: node_name: 节点名称 - state: 当前状态(兼容 dict 和 dataclass) + state: 当前状态 prefix: 日志前缀("进入" 或 "离开") """ from app.logger import info - - # 获取 messages - messages = [] - if isinstance(state, dict): - messages = state.get("messages", []) - else: - messages = getattr(state, "messages", []) - + + messages = state.messages msg_count = len(messages) last_msg = messages[-1] if messages else None last_info = "" if last_msg: - # 兼容 dict 和对象两种格式 - if isinstance(last_msg, dict): - content_preview = str(last_msg.get("content", ""))[:10].replace("\n", " ") - msg_type = last_msg.get("type", "unknown") - else: - content_preview = str(last_msg.content)[:10].replace("\n", " ") - msg_type = getattr(last_msg, 'type', 'unknown') + content_preview = str(last_msg.content)[:10].replace("\n", " ") + msg_type = getattr(last_msg, 'type', 'unknown') last_info = f"{msg_type.upper()}: {content_preview}" info(f"🔄 [{node_name}] {prefix} | 消息数:{msg_count} | 最后一条:{last_info}")