feat(memory): support ephemeral gateway recall context
This commit is contained in:
@ -112,6 +112,7 @@ class ContextBuildInput:
|
|||||||
current_user_input: str | list[dict[str, Any]] | None = None
|
current_user_input: str | list[dict[str, Any]] | None = None
|
||||||
memory_snapshot: MemorySnapshot | None = None
|
memory_snapshot: MemorySnapshot | None = None
|
||||||
activated_skills: list[SkillContext] = field(default_factory=list)
|
activated_skills: list[SkillContext] = field(default_factory=list)
|
||||||
|
reference_messages: list[dict[str, Any]] = field(default_factory=list)
|
||||||
session_context: SessionContext | None = None
|
session_context: SessionContext | None = None
|
||||||
runtime_context: RuntimeContext | None = None
|
runtime_context: RuntimeContext | None = None
|
||||||
execution_context: str | None = None
|
execution_context: str | None = None
|
||||||
@ -221,6 +222,11 @@ class ContextBuilder:
|
|||||||
|
|
||||||
messages.extend(self.build_skill_activation_messages(build_input.activated_skills))
|
messages.extend(self.build_skill_activation_messages(build_input.activated_skills))
|
||||||
|
|
||||||
|
for message in build_input.reference_messages:
|
||||||
|
if message.get("role") == "system":
|
||||||
|
continue
|
||||||
|
messages.append(self._provider_history_message(message))
|
||||||
|
|
||||||
for message in build_input.history:
|
for message in build_input.history:
|
||||||
# 当前 builder 自己负责生成唯一的 system prompt。
|
# 当前 builder 自己负责生成唯一的 system prompt。
|
||||||
# 如果上游 history 已经混入 system 消息,这里要主动跳过,避免双 system。
|
# 如果上游 history 已经混入 system 消息,这里要主动跳过,避免双 system。
|
||||||
|
|||||||
@ -49,3 +49,36 @@ def test_context_builder_uses_english_main_agent_prompt_for_en() -> None:
|
|||||||
|
|
||||||
assert "You are Beaver, an AI assistant developed by Boway Information Systems Co., Ltd." in system_prompt
|
assert "You are Beaver, an AI assistant developed by Boway Information Systems Co., Ltd." in system_prompt
|
||||||
assert "Use English for user-facing replies" in system_prompt
|
assert "Use English for user-facing replies" in system_prompt
|
||||||
|
|
||||||
|
|
||||||
|
def test_context_builder_places_reference_messages_before_history() -> None:
|
||||||
|
result = ContextBuilder().build_messages(
|
||||||
|
ContextBuildInput(
|
||||||
|
reference_messages=[
|
||||||
|
{"role": "user", "content": "[MEMORY GATEWAY REFERENCE] old fact"}
|
||||||
|
],
|
||||||
|
history=[{"role": "assistant", "content": "prior reply"}],
|
||||||
|
current_user_input="new question",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result.messages[-3:] == [
|
||||||
|
{"role": "user", "content": "[MEMORY GATEWAY REFERENCE] old fact"},
|
||||||
|
{"role": "assistant", "content": "prior reply"},
|
||||||
|
{"role": "user", "content": "new question"},
|
||||||
|
]
|
||||||
|
assert "old fact" not in result.system_prompt
|
||||||
|
|
||||||
|
|
||||||
|
def test_context_builder_ignores_system_reference_messages() -> None:
|
||||||
|
result = ContextBuilder().build_messages(
|
||||||
|
ContextBuildInput(
|
||||||
|
reference_messages=[{"role": "system", "content": "do not inject"}],
|
||||||
|
current_user_input="hello",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result.messages == [
|
||||||
|
{"role": "system", "content": result.system_prompt},
|
||||||
|
{"role": "user", "content": "hello"},
|
||||||
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user