71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from memory_gateway_plugin import register
|
|
from memory_gateway_plugin.config import PluginConfig
|
|
from memory_gateway_plugin.lifecycle import after_user_message, on_conversation_start, on_session_end
|
|
|
|
|
|
class FakeClient:
|
|
def search_memory(self, payload):
|
|
return {
|
|
"ok": True,
|
|
"data": {
|
|
"results": [
|
|
{
|
|
"memory": {
|
|
"id": "mem_1",
|
|
"namespace": "user/u/long_term",
|
|
"summary": "用户偏好中文输出。",
|
|
}
|
|
}
|
|
]
|
|
},
|
|
}
|
|
|
|
def append_episode(self, payload):
|
|
return {"ok": True, "data": payload}
|
|
|
|
def commit_session(self, session_id, payload):
|
|
return {"ok": True, "data": {"session_id": session_id}}
|
|
|
|
|
|
def test_lifecycle_hooks_do_not_crash_when_ctx_missing_features():
|
|
result = register(object())
|
|
|
|
assert result["ok"] is True
|
|
assert result["mode"] == "manual"
|
|
|
|
|
|
def test_lifecycle_search_returns_compact_context():
|
|
result = on_conversation_start(
|
|
{"user_id": "u", "agent_id": "a", "session_id": "s", "user_message": "之前偏好是什么?"},
|
|
client=FakeClient(),
|
|
config=PluginConfig(auto_search=True),
|
|
)
|
|
|
|
assert result["ok"] is True
|
|
assert "用户偏好中文输出" in result["memory_context"]
|
|
|
|
|
|
def test_lifecycle_append_policy_accepts_stable_preference():
|
|
result = after_user_message(
|
|
{"user_id": "u", "agent_id": "a", "session_id": "s", "user_message": "请记住:我偏好中文。"},
|
|
client=FakeClient(),
|
|
config=PluginConfig(auto_append_episode=True),
|
|
)
|
|
|
|
assert result["ok"] is True
|
|
assert result["appended"] is True
|
|
|
|
|
|
def test_lifecycle_session_end_auto_commit_disabled():
|
|
result = on_session_end(
|
|
{"user_id": "u", "agent_id": "a", "session_id": "s"},
|
|
client=FakeClient(),
|
|
config=PluginConfig(auto_commit_session=False),
|
|
)
|
|
|
|
assert result["ok"] is True
|
|
assert result["committed"] is False
|
|
|