修复状态兼容性问题:让旧节点同时支持 dict 和 dataclass
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 6m39s
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 6m39s
This commit is contained in:
@@ -11,33 +11,42 @@ from app.memory.mem0_client import Mem0Client
|
||||
from app.utils.logging import log_state_change
|
||||
from app.logger import debug, info, error, warning
|
||||
|
||||
|
||||
def _get_attr(state, attr_name, default=None):
|
||||
"""通用方法:兼容 dict 和 dataclass 两种状态格式"""
|
||||
if isinstance(state, dict):
|
||||
return state.get(attr_name, default)
|
||||
else:
|
||||
return getattr(state, attr_name, default)
|
||||
|
||||
|
||||
def create_summarize_node(mem0_client: Mem0Client):
|
||||
"""
|
||||
工厂函数:创建记忆存储节点
|
||||
|
||||
Args:
|
||||
mem0_client: Mem0 客户端实例
|
||||
|
||||
|
||||
Returns:
|
||||
异步节点函数
|
||||
"""
|
||||
|
||||
from langchain_core.runnables.config import RunnableConfig
|
||||
|
||||
async def summarize_conversation(state: MessagesState, config: RunnableConfig) -> Dict[str, Any]:
|
||||
async def summarize_conversation(state, config: RunnableConfig) -> Dict[str, Any]:
|
||||
"""
|
||||
记忆存储节点 - 使用 Mem0
|
||||
|
||||
Args:
|
||||
state: 当前对话状态
|
||||
state: 当前对话状态(兼容 dict 和 dataclass)
|
||||
config: 运行时配置
|
||||
|
||||
|
||||
Returns:
|
||||
重置计数器的状态更新
|
||||
"""
|
||||
log_state_change("summarize", state, "进入")
|
||||
|
||||
messages = state["messages"]
|
||||
messages = _get_attr(state, "messages", [])
|
||||
if len(messages) < 4:
|
||||
debug("📝 [记忆添加] 对话过短,跳过")
|
||||
return {"turns_since_last_summary": 0}
|
||||
|
||||
Reference in New Issue
Block a user