优化:重构 state,分离持久化和临时字段,完善 init_state_node
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m37s
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m37s
主要改动: 1. 移除无用字段 system_prompt 2. 重新组织 state,明确标注「持久化字段」和「临时字段」 3. 完善 init_state_node,重置所有临时字段 4. 解决数据残留隐患,确保每轮对话开始时状态干净
This commit is contained in:
@@ -11,21 +11,97 @@
|
||||
from datetime import datetime
|
||||
|
||||
from backend.app.core.intent import get_route_by_reasoning, ReasoningAction
|
||||
from ...main_graph.state import MainGraphState
|
||||
from ...main_graph.state import (
|
||||
MainGraphState,
|
||||
CurrentAction,
|
||||
ReactReasoningState,
|
||||
HybridRouterState,
|
||||
FastPathState
|
||||
)
|
||||
from backend.app.logger import info
|
||||
|
||||
|
||||
# ========== 初始化状态节点 ==========
|
||||
def init_state_node(state: MainGraphState) -> MainGraphState:
|
||||
"""初始化状态节点:在流程开始时设置初始值"""
|
||||
"""
|
||||
初始化状态节点:在流程开始时设置初始值
|
||||
|
||||
重置策略:
|
||||
- 持久化字段(如 messages、turns_since_last_summary)不重置
|
||||
- 临时字段(如 rag_context、final_result)重置为初始值
|
||||
"""
|
||||
# 持久化字段保留原样
|
||||
# - messages
|
||||
# - turns_since_last_summary
|
||||
# - user_id
|
||||
|
||||
# ========== 重置临时字段 ==========
|
||||
|
||||
# 主图控制字段
|
||||
state.user_query = ""
|
||||
state.current_action = CurrentAction.NONE
|
||||
state.current_model = ""
|
||||
state.intent_confidence = 0.0
|
||||
|
||||
# React 推理专用字段
|
||||
state.reasoning_step = 0
|
||||
state.last_action = ""
|
||||
state.reasoning_history = []
|
||||
|
||||
# RAG 相关字段
|
||||
state.rag_context = ""
|
||||
state.rag_retrieved = False
|
||||
state.rag_docs = []
|
||||
state.rag_confidence = 0.0
|
||||
state.rag_attempts = 0
|
||||
|
||||
# 联网搜索相关字段
|
||||
state.web_search_results = []
|
||||
|
||||
# 错误处理字段
|
||||
state.errors = []
|
||||
state.current_error = None
|
||||
state.retry_action = None
|
||||
state.error_message = ""
|
||||
|
||||
# 子图结果字段
|
||||
state.news_result = None
|
||||
state.dictionary_result = None
|
||||
state.contact_result = None
|
||||
|
||||
# 执行状态
|
||||
state.current_phase = "initializing"
|
||||
state.final_result = ""
|
||||
state.success = False
|
||||
|
||||
# 元数据
|
||||
state.start_time = None
|
||||
state.end_time = None
|
||||
|
||||
# 结构化状态
|
||||
state.react_reasoning = ReactReasoningState()
|
||||
state.hybrid_router = HybridRouterState()
|
||||
state.fast_path = FastPathState()
|
||||
|
||||
# 统计字段
|
||||
state.llm_calls = 0
|
||||
state.last_token_usage = {}
|
||||
state.last_elapsed_time = 0.0
|
||||
state.memory_context = ""
|
||||
|
||||
# 向后兼容字段
|
||||
state.debug_info = {}
|
||||
|
||||
# 设置初始值
|
||||
state.current_phase = "initializing"
|
||||
state.reasoning_step = 0
|
||||
state.start_time = datetime.now().isoformat()
|
||||
|
||||
|
||||
# 从 messages 中提取 user_query(如果没有的话)
|
||||
if not state.user_query and state.messages:
|
||||
last_msg = state.messages[-1]
|
||||
state.user_query = getattr(last_msg, "content", str(last_msg))
|
||||
|
||||
|
||||
return state
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user