2026-02-03 13:31:05 +08:00
|
|
|
"""Middleware for memory mechanism."""
|
|
|
|
|
|
2026-03-05 11:14:34 +08:00
|
|
|
import re
|
2026-02-03 13:41:04 +08:00
|
|
|
from typing import Any, override
|
2026-02-03 13:31:05 +08:00
|
|
|
|
|
|
|
|
from langchain.agents import AgentState
|
|
|
|
|
from langchain.agents.middleware import AgentMiddleware
|
|
|
|
|
from langgraph.runtime import Runtime
|
|
|
|
|
|
|
|
|
|
from src.agents.memory.queue import get_memory_queue
|
|
|
|
|
from src.config.memory_config import get_memory_config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MemoryMiddlewareState(AgentState):
|
|
|
|
|
"""Compatible with the `ThreadState` schema."""
|
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2026-02-03 13:41:04 +08:00
|
|
|
def _filter_messages_for_memory(messages: list[Any]) -> list[Any]:
|
|
|
|
|
"""Filter messages to keep only user inputs and final assistant responses.
|
|
|
|
|
|
|
|
|
|
This filters out:
|
|
|
|
|
- Tool messages (intermediate tool call results)
|
|
|
|
|
- AI messages with tool_calls (intermediate steps, not final responses)
|
2026-03-05 11:14:34 +08:00
|
|
|
- The <uploaded_files> block injected by UploadsMiddleware into human messages
|
|
|
|
|
(file paths are session-scoped and must not persist in long-term memory).
|
|
|
|
|
The user's actual question is preserved; only turns whose content is entirely
|
|
|
|
|
the upload block (nothing remains after stripping) are dropped along with
|
|
|
|
|
their paired assistant response.
|
2026-02-03 13:41:04 +08:00
|
|
|
|
|
|
|
|
Only keeps:
|
2026-03-05 11:14:34 +08:00
|
|
|
- Human messages (with the ephemeral upload block removed)
|
|
|
|
|
- AI messages without tool_calls (final assistant responses), unless the
|
|
|
|
|
paired human turn was upload-only and had no real user text.
|
2026-02-03 13:41:04 +08:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
messages: List of all conversation messages.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Filtered list containing only user inputs and final assistant responses.
|
|
|
|
|
"""
|
feat: add IM channels for Feishu, Slack, and Telegram (#1010)
* feat: add IM channels system for Feishu, Slack, and Telegram integration
Bridge external messaging platforms to DeerFlow via LangGraph Server with
async message bus, thread management, and per-channel configuration.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: address review comments on IM channels system
Fix topic_id handling in store remove/list_entries and manager commands,
correct Telegram reply threading, remove unused imports/variables, update
docstrings and docs to match implementation, and prevent config mutation.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* update skill creator
* fix im reply text
* fix comments
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 15:21:18 +08:00
|
|
|
_UPLOAD_BLOCK_RE = re.compile(r"<uploaded_files>[\s\S]*?</uploaded_files>\n*", re.IGNORECASE)
|
2026-03-05 11:14:34 +08:00
|
|
|
|
2026-02-03 13:41:04 +08:00
|
|
|
filtered = []
|
2026-03-05 11:14:34 +08:00
|
|
|
skip_next_ai = False
|
2026-02-03 13:41:04 +08:00
|
|
|
for msg in messages:
|
|
|
|
|
msg_type = getattr(msg, "type", None)
|
|
|
|
|
|
|
|
|
|
if msg_type == "human":
|
2026-03-05 11:14:34 +08:00
|
|
|
content = getattr(msg, "content", "")
|
|
|
|
|
if isinstance(content, list):
|
feat: add IM channels for Feishu, Slack, and Telegram (#1010)
* feat: add IM channels system for Feishu, Slack, and Telegram integration
Bridge external messaging platforms to DeerFlow via LangGraph Server with
async message bus, thread management, and per-channel configuration.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: address review comments on IM channels system
Fix topic_id handling in store remove/list_entries and manager commands,
correct Telegram reply threading, remove unused imports/variables, update
docstrings and docs to match implementation, and prevent config mutation.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* update skill creator
* fix im reply text
* fix comments
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 15:21:18 +08:00
|
|
|
content = " ".join(p.get("text", "") for p in content if isinstance(p, dict))
|
2026-03-05 11:14:34 +08:00
|
|
|
content_str = str(content)
|
|
|
|
|
if "<uploaded_files>" in content_str:
|
|
|
|
|
# Strip the ephemeral upload block; keep the user's real question.
|
|
|
|
|
stripped = _UPLOAD_BLOCK_RE.sub("", content_str).strip()
|
|
|
|
|
if not stripped:
|
|
|
|
|
# Nothing left — the entire turn was upload bookkeeping;
|
|
|
|
|
# skip it and the paired assistant response.
|
|
|
|
|
skip_next_ai = True
|
|
|
|
|
continue
|
|
|
|
|
# Rebuild the message with cleaned content so the user's question
|
|
|
|
|
# is still available for memory summarisation.
|
|
|
|
|
from copy import copy
|
|
|
|
|
|
|
|
|
|
clean_msg = copy(msg)
|
|
|
|
|
clean_msg.content = stripped
|
|
|
|
|
filtered.append(clean_msg)
|
|
|
|
|
skip_next_ai = False
|
|
|
|
|
else:
|
|
|
|
|
filtered.append(msg)
|
|
|
|
|
skip_next_ai = False
|
2026-02-03 13:41:04 +08:00
|
|
|
elif msg_type == "ai":
|
|
|
|
|
tool_calls = getattr(msg, "tool_calls", None)
|
|
|
|
|
if not tool_calls:
|
2026-03-05 11:14:34 +08:00
|
|
|
if skip_next_ai:
|
|
|
|
|
skip_next_ai = False
|
|
|
|
|
continue
|
2026-02-03 13:41:04 +08:00
|
|
|
filtered.append(msg)
|
|
|
|
|
# Skip tool messages and AI messages with tool_calls
|
|
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
|
|
|
|
|
|
|
2026-02-03 13:31:05 +08:00
|
|
|
class MemoryMiddleware(AgentMiddleware[MemoryMiddlewareState]):
|
|
|
|
|
"""Middleware that queues conversation for memory update after agent execution.
|
|
|
|
|
|
|
|
|
|
This middleware:
|
|
|
|
|
1. After each agent execution, queues the conversation for memory update
|
2026-02-03 13:41:04 +08:00
|
|
|
2. Only includes user inputs and final assistant responses (ignores tool calls)
|
|
|
|
|
3. The queue uses debouncing to batch multiple updates together
|
|
|
|
|
4. Memory is updated asynchronously via LLM summarization
|
2026-02-03 13:31:05 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
state_schema = MemoryMiddlewareState
|
|
|
|
|
|
2026-03-03 21:32:01 +08:00
|
|
|
def __init__(self, agent_name: str | None = None):
|
|
|
|
|
"""Initialize the MemoryMiddleware.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
agent_name: If provided, memory is stored per-agent. If None, uses global memory.
|
|
|
|
|
"""
|
|
|
|
|
super().__init__()
|
|
|
|
|
self._agent_name = agent_name
|
|
|
|
|
|
2026-02-03 13:31:05 +08:00
|
|
|
@override
|
|
|
|
|
def after_agent(self, state: MemoryMiddlewareState, runtime: Runtime) -> dict | None:
|
|
|
|
|
"""Queue conversation for memory update after agent completes.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
state: The current agent state.
|
|
|
|
|
runtime: The runtime context.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
None (no state changes needed from this middleware).
|
|
|
|
|
"""
|
|
|
|
|
config = get_memory_config()
|
|
|
|
|
if not config.enabled:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
# Get thread ID from runtime context
|
|
|
|
|
thread_id = runtime.context.get("thread_id")
|
|
|
|
|
if not thread_id:
|
|
|
|
|
print("MemoryMiddleware: No thread_id in context, skipping memory update")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
# Get messages from state
|
|
|
|
|
messages = state.get("messages", [])
|
|
|
|
|
if not messages:
|
|
|
|
|
print("MemoryMiddleware: No messages in state, skipping memory update")
|
|
|
|
|
return None
|
|
|
|
|
|
2026-02-03 13:41:04 +08:00
|
|
|
# Filter to only keep user inputs and final assistant responses
|
|
|
|
|
filtered_messages = _filter_messages_for_memory(messages)
|
|
|
|
|
|
2026-02-03 13:31:05 +08:00
|
|
|
# Only queue if there's meaningful conversation
|
|
|
|
|
# At minimum need one user message and one assistant response
|
2026-02-03 13:41:04 +08:00
|
|
|
user_messages = [m for m in filtered_messages if getattr(m, "type", None) == "human"]
|
|
|
|
|
assistant_messages = [m for m in filtered_messages if getattr(m, "type", None) == "ai"]
|
2026-02-03 13:31:05 +08:00
|
|
|
|
|
|
|
|
if not user_messages or not assistant_messages:
|
|
|
|
|
return None
|
|
|
|
|
|
2026-02-03 13:41:04 +08:00
|
|
|
# Queue the filtered conversation for memory update
|
2026-02-03 13:31:05 +08:00
|
|
|
queue = get_memory_queue()
|
2026-03-03 21:32:01 +08:00
|
|
|
queue.add(thread_id=thread_id, messages=filtered_messages, agent_name=self._agent_name)
|
2026-02-03 13:31:05 +08:00
|
|
|
|
|
|
|
|
return None
|