All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m37s
主要改动: 1. 移除无用字段 system_prompt 2. 重新组织 state,明确标注「持久化字段」和「临时字段」 3. 完善 init_state_node,重置所有临时字段 4. 解决数据残留隐患,确保每轮对话开始时状态干净
149 lines
4.5 KiB
Python
149 lines
4.5 KiB
Python
"""
|
|
主图状态定义 - React 模式增强版
|
|
Main Graph State Definition - React Mode Enhanced
|
|
|
|
字段分类说明:
|
|
- 持久化字段:跨轮次保留,不重置
|
|
- 临时字段:每轮对话开始时重置
|
|
"""
|
|
|
|
from enum import Enum, auto
|
|
from typing import Optional, Dict, Any, Annotated, Sequence, TypedDict, List
|
|
from dataclasses import dataclass, field
|
|
from langgraph.graph import add_messages
|
|
from langchain_core.messages import BaseMessage
|
|
|
|
|
|
# ========== 枚举类型 ==========
|
|
class CurrentAction(Enum):
|
|
"""主图当前操作类型"""
|
|
NONE = auto()
|
|
GENERAL_CHAT = auto()
|
|
NEWS_ANALYSIS = auto()
|
|
DICTIONARY = auto()
|
|
CONTACT = auto()
|
|
|
|
|
|
class ErrorSeverity(Enum):
|
|
"""错误严重程度"""
|
|
INFO = auto() # 信息级别,继续执行
|
|
WARNING = auto() # 警告级别,可以重试
|
|
ERROR = auto() # 错误级别,需要处理
|
|
FATAL = auto() # 致命错误,终止执行
|
|
|
|
|
|
@dataclass
|
|
class ErrorRecord:
|
|
"""错误记录"""
|
|
error_type: str
|
|
error_message: str
|
|
severity: ErrorSeverity = ErrorSeverity.ERROR
|
|
source: str = "" # 来源:哪个节点/子图/工具
|
|
timestamp: str = ""
|
|
retry_count: int = 0 # 已重试次数
|
|
max_retries: int = 3 # 最大重试次数
|
|
context: Dict[str, Any] = field(default_factory=dict) # 错误上下文
|
|
|
|
|
|
@dataclass
|
|
class ReactReasoningState:
|
|
"""React 推理状态"""
|
|
last_reasoning: Optional[Dict[str, Any]] = None
|
|
reasoning_result: Optional[Any] = None # 实际类型是 ReasoningResult
|
|
|
|
|
|
@dataclass
|
|
class HybridRouterState:
|
|
"""混合路由状态"""
|
|
decision: Optional[Any] = None # 实际类型是 HybridRouterResult
|
|
start_time: Optional[str] = None
|
|
|
|
|
|
@dataclass
|
|
class FastPathState:
|
|
"""快速路径状态"""
|
|
chitchat_success: bool = False
|
|
rag_success: bool = False
|
|
tool_success: bool = False
|
|
failed: bool = False
|
|
fail_reason: str = ""
|
|
|
|
|
|
@dataclass
|
|
class MainGraphState:
|
|
"""
|
|
主图状态定义
|
|
|
|
字段分类:
|
|
- 持久化字段:跨轮次保留,不重置
|
|
- 临时字段:每轮对话开始时重置
|
|
"""
|
|
|
|
# ==================================================
|
|
# 持久化字段(每轮保留)
|
|
# ==================================================
|
|
|
|
messages: Annotated[Sequence[BaseMessage], add_messages] = field(default_factory=list)
|
|
turns_since_last_summary: int = 0 # 距离上次总结的轮数
|
|
user_id: str = ""
|
|
|
|
# ==================================================
|
|
# 临时字段(每轮重置)
|
|
# ==================================================
|
|
|
|
# 主图控制字段
|
|
user_query: str = ""
|
|
current_action: CurrentAction = CurrentAction.NONE
|
|
current_model: str = "" # 本次请求使用的模型
|
|
intent_confidence: float = 0.0
|
|
|
|
# React 推理专用字段
|
|
reasoning_step: int = 0
|
|
max_steps: int = 10 # 避免过长循环
|
|
last_action: str = ""
|
|
reasoning_history: List[Dict[str, Any]] = field(default_factory=list)
|
|
|
|
# RAG 相关字段
|
|
rag_context: str = ""
|
|
rag_retrieved: bool = False
|
|
rag_docs: List[Dict[str, Any]] = field(default_factory=list)
|
|
rag_confidence: float = 0.0 # RAG 检索置信度 (0.0-1.0)
|
|
rag_attempts: int = 0 # RAG 检索次数统计
|
|
|
|
# 联网搜索相关字段
|
|
web_search_results: List[str] = field(default_factory=list)
|
|
|
|
# 错误处理字段
|
|
errors: List[ErrorRecord] = field(default_factory=list)
|
|
current_error: Optional[ErrorRecord] = None
|
|
retry_action: Optional[str] = None
|
|
error_message: str = ""
|
|
|
|
# 子图结果字段
|
|
news_result: Optional[Dict[str, Any]] = None
|
|
dictionary_result: Optional[Dict[str, Any]] = None
|
|
contact_result: Optional[Dict[str, Any]] = None
|
|
|
|
# 执行状态
|
|
current_phase: str = "init"
|
|
final_result: str = ""
|
|
success: bool = False
|
|
|
|
# 元数据
|
|
start_time: Optional[str] = None
|
|
end_time: Optional[str] = None
|
|
|
|
# 结构化状态
|
|
react_reasoning: ReactReasoningState = field(default_factory=ReactReasoningState)
|
|
hybrid_router: HybridRouterState = field(default_factory=HybridRouterState)
|
|
fast_path: FastPathState = field(default_factory=FastPathState)
|
|
|
|
# 统计字段(用于反馈)
|
|
llm_calls: int = 0
|
|
last_token_usage: Dict[str, Any] = field(default_factory=dict)
|
|
last_elapsed_time: float = 0.0
|
|
memory_context: str = "" # 记忆检索结果
|
|
|
|
# 向后兼容(保留但不推荐使用)
|
|
debug_info: Dict[str, Any] = field(default_factory=dict)
|