mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-11-22 16:39:56 +08:00
fix(LongTermMemory): support memory tools in Agent processors (#2843)
* fix(LongTermMemory): support memory tools in Agent processors * docs(LongTermMemory): clarify tool and Pipeline lifecycle access * test(LongTermMemory): isolate persistent Chroma clients across fixtures
This commit is contained in:
@@ -468,6 +468,7 @@ class LongTermMemoryEngine(KnowledgeEngine):
|
||||
batch_texts.append(content)
|
||||
batch_ids.append(mid)
|
||||
batch_metas.append({
|
||||
"episode_id": mid,
|
||||
"content": content,
|
||||
"tags": ",".join(tags) if isinstance(tags, list) else str(tags),
|
||||
"importance": str(importance),
|
||||
|
||||
@@ -5,7 +5,6 @@ from typing import Any
|
||||
|
||||
from langbot_plugin.api.definition.components.tool.tool import Tool
|
||||
from langbot_plugin.api.entities.builtin.provider import session as provider_session
|
||||
from langbot_plugin.api.proxies.query_based_api import QueryBasedAPIProxy
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -18,12 +17,7 @@ class Forget(Tool):
|
||||
query_id: int,
|
||||
) -> str:
|
||||
store = self.plugin.memory_store
|
||||
api = QueryBasedAPIProxy(
|
||||
query_id=query_id,
|
||||
plugin_runtime_handler=self.plugin.plugin_runtime_handler,
|
||||
)
|
||||
bot_uuid = await api.get_bot_uuid()
|
||||
query_vars = await api.get_query_vars()
|
||||
bot_uuid = session.bot_uuid or ""
|
||||
session_key, user_key, kb_id, _, config = await store.resolve_user_context(
|
||||
session, bot_uuid
|
||||
)
|
||||
@@ -48,6 +42,8 @@ class Forget(Tool):
|
||||
episode_id=episode_id,
|
||||
user_key=user_key,
|
||||
)
|
||||
if not deleted:
|
||||
return "No matching memory found in the current scope."
|
||||
await store.append_audit_entry(
|
||||
scope_key=session_key,
|
||||
user_key=user_key,
|
||||
@@ -55,8 +51,7 @@ class Forget(Tool):
|
||||
target_type="episode",
|
||||
target_id=str(episode_id),
|
||||
summary=f"Deleted episode {episode_id}",
|
||||
sender_id=str(query_vars.get("sender_id", "") or ""),
|
||||
sender_name=str(query_vars.get("sender_name", "") or ""),
|
||||
sender_id=str(session.sender_id or ""),
|
||||
query_id=query_id,
|
||||
metadata={"kb_id": kb_id, "deleted": deleted},
|
||||
)
|
||||
|
||||
@@ -6,7 +6,6 @@ from typing import Any
|
||||
|
||||
from langbot_plugin.api.definition.components.tool.tool import Tool
|
||||
from langbot_plugin.api.entities.builtin.provider import session as provider_session
|
||||
from langbot_plugin.api.proxies.query_based_api import QueryBasedAPIProxy
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -26,27 +25,19 @@ class RecallMemory(Tool):
|
||||
query_id: int,
|
||||
) -> str:
|
||||
store = self.plugin.memory_store
|
||||
api = QueryBasedAPIProxy(
|
||||
query_id=query_id,
|
||||
plugin_runtime_handler=self.plugin.plugin_runtime_handler,
|
||||
)
|
||||
logger.info(
|
||||
"[LongTermMemory] recall_memory called: query_id=%s params_keys=%s",
|
||||
query_id,
|
||||
sorted(params.keys()),
|
||||
)
|
||||
bot_uuid = await api.get_bot_uuid()
|
||||
_, user_key, kb_id, _isolation, config = await store.resolve_user_context(
|
||||
bot_uuid = session.bot_uuid or ""
|
||||
session_key, user_key, kb_id, _isolation, config = await store.resolve_user_context(
|
||||
session, bot_uuid
|
||||
)
|
||||
|
||||
if not kb_id:
|
||||
return "Error: no memory knowledge base configured. Create one first."
|
||||
|
||||
pipeline_kbs = await api.list_pipeline_knowledge_bases()
|
||||
if not any(kb.get("uuid") == kb_id for kb in pipeline_kbs):
|
||||
return "Error: memory knowledge base is not configured for the current pipeline."
|
||||
|
||||
embedding_model_uuid = config.get("embedding_model_uuid", "")
|
||||
if not embedding_model_uuid:
|
||||
return "Error: no embedding model configured in knowledge base."
|
||||
@@ -140,7 +131,9 @@ class RecallMemory(Tool):
|
||||
except ValueError as exc:
|
||||
return f"Error: {exc}"
|
||||
|
||||
if not episodes:
|
||||
session_profile = await store.load_session_profile(session_key)
|
||||
speaker_profile = await store.load_speaker_profile(session_key, str(session.sender_id or ""))
|
||||
if not episodes and not any(store.has_profile_data(profile) for profile in (session_profile, speaker_profile)):
|
||||
logger.info(
|
||||
"[LongTermMemory] recall_memory no results: query_id=%s kb_id=%s",
|
||||
query_id,
|
||||
@@ -154,4 +147,8 @@ class RecallMemory(Tool):
|
||||
kb_id,
|
||||
len(episodes),
|
||||
)
|
||||
return json.dumps(episodes, ensure_ascii=False)
|
||||
return json.dumps({
|
||||
"session_profile": store.format_profile_text(session_profile),
|
||||
"speaker_profile": store.format_profile_text(speaker_profile),
|
||||
"episodes": episodes,
|
||||
}, ensure_ascii=False)
|
||||
|
||||
@@ -6,8 +6,8 @@ metadata:
|
||||
en_US: RecallMemory
|
||||
zh_Hans: 主动回忆
|
||||
description:
|
||||
en_US: Search long-term episodic memories with controlled filters such as speaker and time range.
|
||||
zh_Hans: 使用受控过滤条件(如说话人、时间范围)主动搜索长期情景记忆。
|
||||
en_US: Read current session and speaker profiles, and search episodic memories with controlled filters.
|
||||
zh_Hans: 读取当前会话和发言人的档案,并使用受控过滤条件搜索情景记忆。
|
||||
|
||||
spec:
|
||||
parameters:
|
||||
@@ -49,7 +49,9 @@ spec:
|
||||
- query
|
||||
|
||||
llm_prompt: |
|
||||
Search long-term episodic memories when the default automatic memory recall is not sufficient.
|
||||
Read long-term memory for the current conversation and speaker.
|
||||
Returns session_profile and speaker_profile text together with matching episodes.
|
||||
Independent Agent processors can use this tool without a Pipeline or automatic memory injection.
|
||||
|
||||
Use this tool when you need to actively inspect memory with a specific retrieval intent, such as:
|
||||
- recalling what a specific person said
|
||||
|
||||
@@ -5,7 +5,6 @@ from typing import Any
|
||||
|
||||
from langbot_plugin.api.definition.components.tool.tool import Tool
|
||||
from langbot_plugin.api.entities.builtin.provider import session as provider_session
|
||||
from langbot_plugin.api.proxies.query_based_api import QueryBasedAPIProxy
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -25,17 +24,12 @@ class Remember(Tool):
|
||||
query_id: int,
|
||||
) -> str:
|
||||
store = self.plugin.memory_store
|
||||
api = QueryBasedAPIProxy(
|
||||
query_id=query_id,
|
||||
plugin_runtime_handler=self.plugin.plugin_runtime_handler,
|
||||
)
|
||||
logger.info(
|
||||
"[LongTermMemory] remember called: query_id=%s params_keys=%s",
|
||||
query_id,
|
||||
sorted(params.keys()),
|
||||
)
|
||||
bot_uuid = await api.get_bot_uuid()
|
||||
query_vars = await api.get_query_vars()
|
||||
bot_uuid = session.bot_uuid or ""
|
||||
session_key, user_key, kb_id, _, config = await store.resolve_user_context(
|
||||
session, bot_uuid
|
||||
)
|
||||
@@ -43,10 +37,6 @@ class Remember(Tool):
|
||||
if not kb_id:
|
||||
return "Error: no memory knowledge base configured. Create one first."
|
||||
|
||||
pipeline_kbs = await api.list_pipeline_knowledge_bases()
|
||||
if not any(kb.get("uuid") == kb_id for kb in pipeline_kbs):
|
||||
return "Error: memory knowledge base is not configured for the current pipeline."
|
||||
|
||||
embedding_model_uuid = config.get("embedding_model_uuid", "")
|
||||
if not embedding_model_uuid:
|
||||
return "Error: no embedding model configured in knowledge base."
|
||||
@@ -57,8 +47,7 @@ class Remember(Tool):
|
||||
|
||||
tags = params.get("tags", [])
|
||||
importance = params.get("importance", 2)
|
||||
sender_id = str(query_vars.get("sender_id", "") or "")
|
||||
sender_name = str(query_vars.get("sender_name", "") or "")
|
||||
sender_id = str(session.sender_id or "")
|
||||
logger.info(
|
||||
"[LongTermMemory] remember storing episode: query_id=%s kb_id=%s user_key=%s sender_id=%s importance=%s tags=%s content_len=%s",
|
||||
query_id,
|
||||
@@ -78,7 +67,6 @@ class Remember(Tool):
|
||||
tags=tags,
|
||||
importance=importance,
|
||||
sender_id=sender_id,
|
||||
sender_name=sender_name,
|
||||
bot_uuid=bot_uuid,
|
||||
)
|
||||
|
||||
@@ -97,7 +85,6 @@ class Remember(Tool):
|
||||
target_id=episode["id"],
|
||||
summary=self._preview_text(str(content)),
|
||||
sender_id=sender_id,
|
||||
sender_name=sender_name,
|
||||
query_id=query_id,
|
||||
metadata={
|
||||
"kb_id": kb_id,
|
||||
|
||||
@@ -5,7 +5,6 @@ from typing import Any
|
||||
|
||||
from langbot_plugin.api.definition.components.tool.tool import Tool
|
||||
from langbot_plugin.api.entities.builtin.provider import session as provider_session
|
||||
from langbot_plugin.api.proxies.query_based_api import QueryBasedAPIProxy
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -94,14 +93,8 @@ class UpdateProfile(Tool):
|
||||
if not isinstance(previous_value, str):
|
||||
return "Error: previous_value must be a string."
|
||||
|
||||
api = QueryBasedAPIProxy(
|
||||
query_id=query_id,
|
||||
plugin_runtime_handler=self.plugin.plugin_runtime_handler,
|
||||
)
|
||||
bot_uuid = await api.get_bot_uuid()
|
||||
query_vars = await api.get_query_vars()
|
||||
sender_id = str(query_vars.get("sender_id", "") or "")
|
||||
sender_name = str(query_vars.get("sender_name", "") or "")
|
||||
bot_uuid = session.bot_uuid or ""
|
||||
sender_id = str(session.sender_id or "")
|
||||
|
||||
session_key, user_key, kb_id, _isolation, _ = await store.resolve_user_context(
|
||||
session, bot_uuid
|
||||
@@ -109,10 +102,6 @@ class UpdateProfile(Tool):
|
||||
if not kb_id:
|
||||
return "Error: no memory knowledge base configured. Create one first."
|
||||
|
||||
pipeline_kbs = await api.list_pipeline_knowledge_bases()
|
||||
if not any(kb.get("uuid") == kb_id for kb in pipeline_kbs):
|
||||
return "Error: memory knowledge base is not configured for the current pipeline."
|
||||
|
||||
target_scope = self._infer_scope(field, scope)
|
||||
logger.info(
|
||||
"[LongTermMemory] update_profile resolved scope: query_id=%s target_scope=%s session_key=%s sender_id=%s value_len=%s",
|
||||
@@ -165,7 +154,6 @@ class UpdateProfile(Tool):
|
||||
f"{f' ({fact_key})' if fact_key else ''}: {self._preview_text(str(value))}"
|
||||
),
|
||||
sender_id=sender_id,
|
||||
sender_name=sender_name,
|
||||
query_id=query_id,
|
||||
metadata={
|
||||
"field": field,
|
||||
|
||||
Reference in New Issue
Block a user