彻底重构状态系统:整合所有旧状态到 MainGraphState,修复所有节点
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m35s

This commit is contained in:
2026-05-01 23:20:31 +08:00
parent 9a58eb8e6d
commit 9386b9fa7a
7 changed files with 155 additions and 193 deletions

View File

@@ -4,32 +4,23 @@
"""
from typing import Any, Dict
from app.main_graph.config import get_stream_writer
# 本地模块
from app.main_graph.state import MessagesState
from app.main_graph.state import MainGraphState
from app.utils.logging import log_state_change
from app.logger import info, error
from app.logger import info, warning
from langchain_core.runnables.config import RunnableConfig
def _get_attr(state, attr_name, default=None):
"""通用方法:兼容 dict 和 dataclass 两种状态格式"""
if isinstance(state, dict):
return state.get(attr_name, default)
else:
return getattr(state, attr_name, default)
async def finalize_node(state, config: RunnableConfig) -> Dict[str, Any]:
async def finalize_node(state: MainGraphState, config: RunnableConfig) -> Dict[str, Any]:
"""
完成事件节点 - 发送完成事件包含token使用情况和耗时信息
Args:
state: 当前对话状态(兼容 dict 和 dataclass
state: 当前对话状态
config: 运行时配置
Returns:
空字典(完成节点,无状态更新)
"""
@@ -37,18 +28,25 @@ async def finalize_node(state, config: RunnableConfig) -> Dict[str, Any]:
try:
# 获取流式写入器并发送完成事件
from app.main_graph.config import get_stream_writer
writer = get_stream_writer()
writer({
"type": "custom",
"data": {
"type": "done",
"token_usage": _get_attr(state, "last_token_usage", {}),
"elapsed_time": _get_attr(state, "last_elapsed_time", 0.0)
}
})
info("🏁 [完成事件] 已发送完成事件包含token使用情况和耗时信息")
# 只在 writer 存在且不是 noop 时才发送
if writer and hasattr(writer, '__call__'):
try:
writer({
"type": "custom",
"data": {
"type": "done",
"token_usage": state.last_token_usage,
"elapsed_time": state.last_elapsed_time
}
})
info("🏁 [完成事件] 已发送完成事件包含token使用情况和耗时信息")
except Exception as e:
warning(f"⚠️ [完成事件] 发送完成事件失败 (非致命): {e}")
except Exception as e:
error(f" [完成事件] 发送完成事件时发生异常: {e}")
warning(f"⚠️ [完成事件] 处理失败 (非致命): {e}")
log_state_change("finalize", state, "离开")
return {}