feat: 完善词典子图,添加API调用和前端格式化工具
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m5s

- 完善词典子图:添加生词本功能
- 创建API调用工具:dictionary_api
- 添加前端格式化展示工具:result_formatter.py
- 创建通讯录和资讯子图的基本结构
- 更新主图状态结构,添加MainGraphState
- 添加subgraph_builder.py用于子图集成
This commit is contained in:
2026-04-25 18:29:23 +08:00
parent 03ba825645
commit a14744f18b
17 changed files with 2057 additions and 18 deletions

View File

@@ -1,25 +1,75 @@
"""
LangGraph 状态定义模块
包含 MessagesState 和 GraphContext
主图状态定义 - 扩展版
Main Graph State Definition - Extended
"""
import operator
from typing import Annotated
from typing_extensions import TypedDict
from dataclasses import dataclass
from langchain_core.messages import AnyMessage
from enum import Enum, auto
from typing import Optional, Dict, Any, Annotated, Sequence, TypedDict
from dataclasses import dataclass, field
from langgraph.graph import add_messages
from langchain_core.messages import BaseMessage
# ========== 兼容旧代码的类型 ==========
class MessagesState(TypedDict):
"""对话状态类型定义"""
messages: Annotated[list[AnyMessage], operator.add]
"""旧的MessagesState类型保留兼容性"""
messages: Annotated[Sequence[BaseMessage], add_messages]
class GraphContext(TypedDict):
"""旧的GraphContext类型保留兼容性"""
llm_calls: int
memory_context: str
last_token_usage: dict # 本次调用的 token 使用详情
last_elapsed_time: float # 本次调用耗时(秒)
turns_since_last_summary: int # 距离上次生成摘要的轮数
system_prompt: str
# ========== 新的类型 ==========
class CurrentAction(Enum):
"""主图当前操作类型"""
NONE = auto()
GENERAL_CHAT = auto()
NEWS_ANALYSIS = auto()
DICTIONARY = auto()
CONTACT = auto()
@dataclass
class GraphContext:
"""图执行上下文"""
user_id: str
# 可扩展更多上下文信息
class MainGraphState:
"""
主图状态 - 兼容旧代码 + 新增子图功能
包含:
1. 旧代码的MessagesState兼容性字段
2. 主图控制字段
3. 子图结果占位
4. 用户信息
"""
# ========== 兼容性字段保留旧的MessagesState ==========
messages: Annotated[Sequence[BaseMessage], add_messages] = field(default_factory=list)
llm_calls: int = 0
memory_context: str = ""
system_prompt: str = ""
# ========== 主图控制字段 ==========
user_query: str = "" # 用户当前查询
current_action: CurrentAction = CurrentAction.NONE # 当前操作
intent_confidence: float = 0.0 # 意图识别置信度
# ========== 子图结果占位 ==========
news_result: Optional[Dict[str, Any]] = None # 资讯子图结果
dictionary_result: Optional[Dict[str, Any]] = None # 词典子图结果
contact_result: Optional[Dict[str, Any]] = None # 通讯录子图结果
# ========== 用户信息 ==========
user_id: str = ""
# ========== 执行状态 ==========
current_phase: str = "init"
error_message: str = ""
final_result: str = ""
success: bool = False
# ========== 元数据 ==========
start_time: Optional[str] = None
end_time: Optional[str] = None
debug_info: Dict[str, Any] = field(default_factory=dict)