Skip to content

Commit ea66ff7

Browse files
committed
refactor(memory): move _build_query method to base class and simplify logic
- Remove MAX_QUERY_CHARS constant from adbpg and reme_light managers - Delete old _build_query implementation with character limiting logic - Add new simplified _build_query in base_memory_manager returning latest user message only - Update tests to verify the new query building behavior with user messages - Remove redundant query building implementations from child classes
1 parent 29b2a9b commit ea66ff7

4 files changed

Lines changed: 46 additions & 36 deletions

File tree

src/qwenpaw/agents/memory/adbpg_memory_manager.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from ...exceptions import ConfigurationException as ConfigurationError
2828

2929
logger = logging.getLogger(__name__)
30-
MAX_QUERY_CHARS = 50
3130

3231

3332
@memory_registry.register("adbpg")
@@ -337,23 +336,6 @@ async def memory_search(
337336
# Private helpers
338337
# ------------------------------------------------------------------
339338

340-
@staticmethod
341-
def _build_query(messages: list[Msg]) -> str:
342-
parts = []
343-
total = 0
344-
for msg in reversed(messages):
345-
if msg.role not in {"user", "assistant"}:
346-
continue
347-
text = (msg.get_text_content() or "").strip()
348-
if not text:
349-
continue
350-
remaining = MAX_QUERY_CHARS - total - (1 if parts else 0)
351-
if remaining <= 0:
352-
break
353-
parts.insert(0, text[-remaining:])
354-
total += min(len(text), remaining) + (1 if len(parts) > 1 else 0)
355-
return " ".join(parts).strip()
356-
357339
@staticmethod
358340
def _tool_chunk_text(chunk: ToolChunk) -> str:
359341
parts = []

src/qwenpaw/agents/memory/base_memory_manager.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ async def auto_memory_search(
287287
"""
288288
return None
289289

290+
@staticmethod
291+
def _build_query(messages: list[Msg]) -> str:
292+
for msg in reversed(messages):
293+
if msg.role != "user":
294+
continue
295+
text = (msg.get_text_content() or "").strip()
296+
if text:
297+
return text
298+
return ""
299+
290300
async def auto_memory(
291301
self,
292302
all_messages: list[Msg],

src/qwenpaw/agents/memory/reme_light_memory_manager.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
logger = logging.getLogger(__name__)
3030

31-
MAX_QUERY_CHARS = 50
3231
NO_MEMORY_RESULTS = "(no memory results)"
3332
INBOX_RESULT_JOB_NAMES = {"auto_memory", "auto_dream", "auto_resource"}
3433
INBOX_RESULT_HOOK_KEY = "qwenpaw_memory_result_hook"
@@ -471,20 +470,3 @@ async def dream(self, **kwargs: Any) -> None:
471470
)
472471
if response is not None and not response.success:
473472
raise RuntimeError(str(response.answer))
474-
475-
@staticmethod
476-
def _build_query(messages: list[Msg]) -> str:
477-
parts = []
478-
total = 0
479-
for msg in reversed(messages):
480-
if msg.role not in {"user", "assistant"}:
481-
continue
482-
text = (msg.get_text_content() or "").strip()
483-
if not text:
484-
continue
485-
remaining = MAX_QUERY_CHARS - total - (1 if parts else 0)
486-
if remaining <= 0:
487-
break
488-
parts.insert(0, text[-remaining:])
489-
total += min(len(text), remaining) + (1 if len(parts) > 1 else 0)
490-
return " ".join(parts).strip()

tests/unit/agents/memory/test_base_memory_manager.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,42 @@ async def test_worker_task_created(self, manager):
193193
class TestAutoMemorySearchSanitization:
194194
"""P1: auto_memory input should exclude auto-search blocks only."""
195195

196+
def test_build_query_uses_latest_user_message_only(self, manager):
197+
messages = [
198+
Msg(
199+
name="user",
200+
role="user",
201+
content=[TextBlock(text="NVDA 股价")],
202+
),
203+
Msg(
204+
name="assistant",
205+
role="assistant",
206+
content=[
207+
TextBlock(
208+
text=("达股价查询:NVDA $195.93 (+0.56%)," "市值 $4.746T"),
209+
),
210+
],
211+
),
212+
Msg(
213+
name="user",
214+
role="user",
215+
content=[TextBlock(text="台积电股价")],
216+
),
217+
]
218+
219+
assert manager._build_query(messages) == "台积电股价"
220+
221+
def test_build_query_returns_empty_without_user_text(self, manager):
222+
messages = [
223+
Msg(
224+
name="assistant",
225+
role="assistant",
226+
content=[TextBlock(text="memory noise")],
227+
),
228+
]
229+
230+
assert manager._build_query(messages) == ""
231+
196232
def test_builds_mock_assistant_msg_with_configured_estimated_usage(
197233
self,
198234
manager,

0 commit comments

Comments
 (0)