Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m36s
主要变更: - 迁移到极简 LangGraph 标准架构(START → init_state → 记忆 → Agent ⇄ Tools → finalize → END) - 添加 Baosi API 支持,配置 ops4.7 模型 - 保留本地模型作为默认首选,Baosi 作为备选 - 新架构使用 LangGraph 原生 ToolNode 和 bind_tools - 移除旧的混合路由、JSON 解析等复杂逻辑 - 把旧代码移到 deprecated/ 目录 - 添加新的 Agent 节点和 Tools 模块 - 添加测试脚本验证新架构 - 所有测试通过 ✓
41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from typing import Any, Dict
|
|
from langchain_core.runnables.config import RunnableConfig
|
|
from ..state import AgentState
|
|
from ...memory.mem0_client import Mem0Client
|
|
from backend.app.logger import info
|
|
|
|
|
|
# 全局变量,在 GraphBuilder 中注入
|
|
_mem0_client: Mem0Client = None
|
|
|
|
|
|
def set_mem0_client(client: Mem0Client):
|
|
global _mem0_client
|
|
_mem0_client = client
|
|
|
|
|
|
async def memory_trigger_node(state: AgentState, config: RunnableConfig) -> Dict[str, Any]:
|
|
"""检测用户消息中的记忆指令,若命中则主动调用 Mem0 存储"""
|
|
if _mem0_client is None:
|
|
return {}
|
|
|
|
messages = state.messages
|
|
if not messages:
|
|
return {}
|
|
|
|
last_msg = messages[-1]
|
|
content = last_msg.content if hasattr(last_msg, 'content') else str(last_msg)
|
|
|
|
# 触发词(可自行扩展)
|
|
trigger_words = ["记住", "记下", "保存", "备忘", "记录下", "别忘了"]
|
|
if any(word in content for word in trigger_words):
|
|
user_id = config.get("metadata", {}).get("user_id", "default_user")
|
|
# 确保 Mem0 已初始化
|
|
if not _mem0_client._initialized:
|
|
await _mem0_client.initialize()
|
|
# 将用户消息作为事实来源提交给 Mem0
|
|
info(f"📌 检测到记忆指令,已主动触发 Mem0 存储")
|
|
mem0_messages = [{"role": "user", "content": content}]
|
|
await _mem0_client.add_memories(mem0_messages, user_id=user_id)
|
|
|
|
return {} # 不修改状态 |