diff --git a/apps/agent/agent/runtime.py b/apps/agent/agent/runtime.py index 094fa63a..1d58b1d3 100644 --- a/apps/agent/agent/runtime.py +++ b/apps/agent/agent/runtime.py @@ -17,35 +17,26 @@ async def load_tools_for_turn( scene: str = SCENE_GENERAL, client_tools: list[Any] | None = None, ) -> list[Any]: - """general:builtin + memory + 全部 MCP;发布场景:builtin + 目录/拼多多 MCP + 前端 ui_ 工具。""" + """各场景均挂 memory_*;general 全量 MCP,发布场景仅目录/拼多多 + 前端 ui_。""" from agent.tools.builtin import builtin_tools from agent.tools.memory import memory_tools from infrastructure.mcp.client import load_mcp_tools scene_value = (scene or SCENE_GENERAL).strip() or SCENE_GENERAL - tools: list[Any] = [*builtin_tools()] + tools: list[Any] = [*builtin_tools(), *memory_tools(memory, actor)] + mcp_tools = await load_mcp_tools( + settings, + company_id=actor.company_id, + user_id=actor.user_id, + ) if scene_value == SCENE_ECOM_GOODS_PUBLISH: - mcp_tools = await load_mcp_tools( - settings, - company_id=actor.company_id, - user_id=actor.user_id, - ) # 发布场景:目录 + 拼多多渠道;不挂经营 BI mcp_tools = [ t for t in mcp_tools if _tool_name(t).startswith(("catalog_", "pdd_")) or _tool_name(t) == "mcp_ping" ] - tools.extend(mcp_tools) - if client_tools: - tools.extend(client_tools) - return tools - - tools.extend(memory_tools(memory, actor)) - mcp_tools = await load_mcp_tools( - settings, company_id=actor.company_id, user_id=actor.user_id - ) tools.extend(mcp_tools) if client_tools: tools.extend(client_tools) diff --git a/apps/agent/application/chat_service.py b/apps/agent/application/chat_service.py index 453a91a5..2b63e296 100644 --- a/apps/agent/application/chat_service.py +++ b/apps/agent/application/chat_service.py @@ -87,13 +87,11 @@ class ChatService: session.llm_response_chain = None await self._session_repo.update(session) - just_saved = None - memory_count = 0 - if scene == "general": - just_saved = await self._memory.maybe_capture_from_user( - actor, thread_id=thread_id, text=text - ) - memory_count = await self._memory.count_active(actor) + # 各 scene 均可按需 memory_*;显式「记住」也会 maybe_capture + just_saved = await self._memory.maybe_capture_from_user( + actor, thread_id=thread_id, text=text + ) + memory_count = await self._memory.count_active(actor) system_prompt = build_system_prompt( memory_count=memory_count, diff --git a/apps/agent/application/system_prompt.py b/apps/agent/application/system_prompt.py index f2637d6b..41b30f92 100644 --- a/apps/agent/application/system_prompt.py +++ b/apps/agent/application/system_prompt.py @@ -57,9 +57,28 @@ _PUBLISH_SYSTEM = ( "不要复述工具名、回执原文、字数统计、查了几条、竞品标题全文或改写策略,除非用户问为什么这样改。" "不要主动列 SKU 编码表,不要给没问过的下一版备选,不要让用户刷新、核对,或说「如果还是旧的再告诉我」。" "禁止向用户索要或复述数字 ID。" + "【长期记忆】跨会话用户级记忆不自动注入;标题口径、常用规格写法、用户偏好等需要时:" + "先 memory_search,必要时 memory_load;用户要求记住用 memory_save。" + "改表仍以当前表单与用户本轮指令为准,记忆只作补充,勿用旧记忆覆盖用户刚说的写法。" ) +def _append_memory_status( + system: str, + *, + memory_count: int, + memory_just_saved: str | None, +) -> str: + if memory_count > 0: + system += ( + f"\n\n当前用户约有 {memory_count} 条长期记忆," + "需要时用 memory_search / memory_load。" + ) + if memory_just_saved: + system += f"\n\n【本轮已写入长期记忆】{memory_just_saved}" + return system + + def build_system_prompt( *, memory_count: int = 0, @@ -73,16 +92,15 @@ def build_system_prompt( # 控制体积,避免拖垮上下文 if len(snap_text) > 12000: snap_text = snap_text[:12000] + "…(截断)" - return ( - f"{_PUBLISH_SYSTEM}\n\n【当前发布表单】\n{snap_text}" + system = f"{_PUBLISH_SYSTEM}\n\n【当前发布表单】\n{snap_text}" + return _append_memory_status( + system, + memory_count=memory_count, + memory_just_saved=memory_just_saved, ) - system = _BASE_SYSTEM - if memory_count > 0: - system += ( - f"\n\n当前用户约有 {memory_count} 条长期记忆," - "需要时用 memory_search / memory_load。" - ) - if memory_just_saved: - system += f"\n\n【本轮已写入长期记忆】{memory_just_saved}" - return system + return _append_memory_status( + _BASE_SYSTEM, + memory_count=memory_count, + memory_just_saved=memory_just_saved, + )