feat: 实现 React 模式循环推理,带超时重试和结构化错误处理
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m15s

- 更新 intent.py 为 React 模式推理器
- 新增 react_nodes.py: React 模式节点
- 新增 retry_utils.py: 超时和重试工具
- 更新 state.py: 支持循环步数和错误记录
- 重写 subgraph_builder.py: 完整 React 循环流程
- 结构化错误输出,符合 Agent 执行循环最佳实践
- 限制最大推理步数 ≤40,防止无限循环
- RAG 检索带重试和超时保护
- 子图错误可传递给主图处理
This commit is contained in:
2026-04-26 11:14:04 +08:00
parent e6337eb0fc
commit e3adb45454
7 changed files with 1304 additions and 493 deletions

View File

@@ -1,10 +1,10 @@
"""
主图状态定义 - 扩展
Main Graph State Definition - Extended
主图状态定义 - React 模式增强
Main Graph State Definition - React Mode Enhanced
"""
from enum import Enum, auto
from typing import Optional, Dict, Any, Annotated, Sequence, TypedDict
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
@@ -33,16 +33,38 @@ class CurrentAction(Enum):
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 MainGraphState:
"""
主图状态 - 兼容旧代码 + 新增子图功能
主图状态 - React 循环推理版本
包含:
1. 旧代码的MessagesState兼容性字段
2. 主图控制字段
3. 子图结果占位
4. 用户信息
2. React 推理控制字段
3. 循环和错误处理
4. 子图结果占位
5. 用户信息
"""
# ========== 兼容性字段保留旧的MessagesState ==========
messages: Annotated[Sequence[BaseMessage], add_messages] = field(default_factory=list)
@@ -55,6 +77,22 @@ class MainGraphState:
current_action: CurrentAction = CurrentAction.NONE # 当前操作
intent_confidence: float = 0.0 # 意图识别置信度
# ========== React 推理专用字段 ==========
reasoning_step: int = 0 # 当前推理步数
max_steps: int = 40 # 最大推理步数≤40
last_action: str = "" # 上一步动作
reasoning_history: List[Dict[str, Any]] = field(default_factory=list) # 推理历史
# ========== RAG 相关字段 ==========
rag_context: str = "" # RAG 检索到的上下文
rag_retrieved: bool = False # 是否已经检索过
rag_docs: List[Dict[str, Any]] = field(default_factory=list) # 检索到的文档
# ========== 错误处理字段 ==========
errors: List[ErrorRecord] = field(default_factory=list) # 错误列表
current_error: Optional[ErrorRecord] = None # 当前错误
retry_action: Optional[str] = None # 重试动作
# ========== 子图结果占位 ==========
news_result: Optional[Dict[str, Any]] = None # 资讯子图结果
dictionary_result: Optional[Dict[str, Any]] = None # 词典子图结果