deerflow2/backend/tests/test_thread_memory_prompt.py
2026-05-18 16:03:53 +08:00

29 lines
1.3 KiB
Python

from langchain_core.messages import HumanMessage
from deerflow.agents.memory.thread_prompt import build_thread_memory_prompt, format_thread_memory_for_injection
def test_thread_memory_injection_keeps_profile_and_preferences_under_small_budget(monkeypatch):
monkeypatch.setattr("deerflow.agents.memory.thread_prompt._count_tokens", lambda text, encoding_name="cl100k_base": len(text))
memory = {
"profile": {"name": "Alice", "role": "Engineer", "expertise": ["Python", "React"], "language": "en-US", "context": "Building APIs"},
"preferences": {"tone": "technical", "verbosity": "concise", "codeStyle": "typed-first", "other": "tests first"},
"facts": [
{"content": "Fact one that might be trimmed", "category": "context", "confidence": 0.9},
{"content": "Fact two that might be trimmed", "category": "context", "confidence": 0.8},
],
}
result = format_thread_memory_for_injection(memory, max_tokens=140)
assert "Profile:" in result
assert "Preferences:" in result
def test_build_thread_memory_prompt_does_not_raise_format_key_error():
prompt = build_thread_memory_prompt(
{"profile": {}, "preferences": {}, "facts": []},
[HumanMessage(content="My name is Alice.")],
)
assert "Current per-thread memory" in prompt
assert '"profile"' in prompt