Files
hermes-webui/tests/test_webui_surface_context.py
T
nesquena-hermes 1630fb8d95 fix(streaming): inject WebUI delivery context via system prompt, not prefill user message (#3324, @aether-agent)
Consecutive user turns (session-context prefill + actual message) made models
with strict chat templates (Mistral, Gemma via llama.cpp) reject the request
with a Jinja 500. Move the platform/delivery context (connected platforms, home
channels, scheduled-task delivery hints) from _webui_session_context_message
(a prefill user message) into _webui_delivery_context_prompt, appended to the
ephemeral system prompt. Session framing stays in _webui_surface_context_prompt.
Context is preserved — just role-alternation-safe. Closes #3276.

Co-authored-by: aether-agent <aether-agent@users.noreply.github.com>
2026-06-02 19:36:25 +00:00

109 lines
4.0 KiB
Python

from api.streaming import _webui_ephemeral_system_prompt, _prefill_messages_with_webui_context
def test_webui_ephemeral_prompt_includes_browser_surface_context():
prompt = _webui_ephemeral_system_prompt(
"Use a concise tone.",
surface_context={
"source": "webui",
"session_id": "session-123",
"profile": "default",
"workspace": "/tmp/example-workspace",
},
)
assert "Use a concise tone." in prompt
assert "WebUI session context" in prompt
assert "Source: webui" in prompt
assert "Session ID: session-123" in prompt
assert "Profile: default" in prompt
assert "Workspace: /tmp/example-workspace" in prompt
assert "not the same live transcript as Telegram" in prompt
assert "Do not copy or dump this browser transcript" in prompt
assert "Write to external notes or durable memory only" in prompt
assert "otherwise leave notes unchanged" in prompt
assert "what note/section changed" in prompt
assert "explicit captures" in prompt
assert "durable user preferences" in prompt
assert "Do not include terse planning fragments" in prompt
assert "Final visible assistant replies" in prompt
assert "user-facing English" not in prompt
assert "in the user's language" in prompt
assert "Need script" in prompt
assert "Need inspect email" in prompt
assert "clear user-facing progress" in prompt
def test_webui_ephemeral_prompt_skips_empty_surface_fields():
prompt = _webui_ephemeral_system_prompt(
None,
surface_context={
"source": "webui",
"session_id": "",
"profile": None,
"workspace": " ",
},
)
assert "WebUI session context" in prompt
assert "Source: webui" in prompt
assert "Session ID:" not in prompt
assert "Profile:" not in prompt
assert "Workspace:" not in prompt
def test_ephemeral_prompt_avoids_platform_info_when_no_config():
"""Without config_data, the delivery context falls back to defaults."""
prompt = _webui_ephemeral_system_prompt(
"Be concise.",
surface_context={"source": "webui"},
)
# Core platform headings should still appear (fallback data).
assert "Connected Platforms:" in prompt
assert "Delivery options for scheduled tasks:" in prompt
# But home channels are only present when the config has them.
assert "Home Channels" not in prompt
def test_prefill_no_longer_adds_session_context_user_message():
"""_prefill_messages_with_webui_context must NOT append a user message.
Strict chat templates (Mistral, Gemma) require user/assistant alternation.
Adding a 'user' session context message creates two consecutive user turns.
"""
prefill = {"messages": [{"role": "system", "content": "recall note"}]}
result = _prefill_messages_with_webui_context(prefill)
assert len(result) == 1
assert result[0]["role"] == "system"
assert "Connected Platforms" not in result[0].get("content", "")
def test_prefill_preserves_empty_and_none_messages():
"""Edge cases: empty prefill stays empty, missing key returns empty."""
assert _prefill_messages_with_webui_context({"messages": []}) == []
assert _prefill_messages_with_webui_context({}) == []
assert _prefill_messages_with_webui_context({"messages": None}) == []
def test_delivery_context_includes_home_channels_when_configured():
"""When config_data has platforms with a home_channel, the prompt includes it."""
config = {
"platforms": {
"telegram": {
"enabled": True,
"home_channel": {"name": "General"},
},
},
}
prompt = _webui_ephemeral_system_prompt(
None,
surface_context={"source": "webui"},
config_data=config,
)
assert "Connected Platforms:" in prompt
assert "Home Channels (default destinations):" in prompt
assert "telegram: General" in prompt
assert "telegram" in prompt and "Home channel" in prompt