Files
ailine/backend/app/deprecated/finalize.py
root 22fdb625a4
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m36s
feat: 完成极简 LangGraph 架构迁移,添加 Baosi API 支持
主要变更:
- 迁移到极简 LangGraph 标准架构(START → init_state → 记忆 → Agent ⇄ Tools → finalize → END)
- 添加 Baosi API 支持,配置 ops4.7 模型
- 保留本地模型作为默认首选,Baosi 作为备选
- 新架构使用 LangGraph 原生 ToolNode 和 bind_tools
- 移除旧的混合路由、JSON 解析等复杂逻辑
- 把旧代码移到 deprecated/ 目录
- 添加新的 Agent 节点和 Tools 模块
- 添加测试脚本验证新架构
- 所有测试通过 ✓
2026-05-07 00:48:17 +08:00

60 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
完成事件节点模块
负责发送完成事件包含token使用情况和耗时信息
"""
from typing import Any, Dict
# 本地模块
from ...main_graph.state import MainGraphState
from ...utils.logging import log_state_change
from backend.app.logger import info, warning
from langchain_core.runnables.config import RunnableConfig
async def finalize_node(state: MainGraphState, config: RunnableConfig) -> Dict[str, Any]:
"""
完成事件节点 - 发送完成事件包含token使用情况和耗时信息
Args:
state: 当前对话状态
config: 运行时配置
Returns:
更新后的状态(包含 final_result
"""
log_state_change("finalize", state, "进入")
# 确保 final_result 被传递出去
result = {
"final_result": state.final_result,
"success": state.success,
"current_phase": "done"
}
try:
# 获取流式写入器并发送完成事件
from backend.app.main_graph.config import get_stream_writer
writer = get_stream_writer()
# 只在 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,
"final_result": state.final_result
}
})
info("🏁 [完成事件] 已发送完成事件")
except Exception as e:
warning(f"⚠️ [完成事件] 发送完成事件失败 (非致命): {e}")
except Exception as e:
warning(f"⚠️ [完成事件] 处理失败 (非致命): {e}")
log_state_change("finalize", state, "离开")
return result