diff --git a/api/gateway_chat.py b/api/gateway_chat.py index 13d4d7fc..89f850e2 100644 --- a/api/gateway_chat.py +++ b/api/gateway_chat.py @@ -247,15 +247,30 @@ def _run_gateway_chat_streaming( cfg = get_config() try: from api.streaming import ( - _WEBUI_PROGRESS_PROMPT, _load_webui_prefill_context, _prefill_messages_with_webui_context, _public_prefill_context_status, + _webui_ephemeral_system_prompt, ) prefill_context = _load_webui_prefill_context(cfg) + # #3324: the WebUI session/delivery context (connected platforms, + # home channels, delivery hints, session framing) is now carried in + # the ephemeral system prompt rather than a prefill `user` message. + # The gateway-backed path must build the SAME system prompt so that + # context is not silently dropped on Gateway-routed WebUI chats. + _gateway_system_prompt = _webui_ephemeral_system_prompt( + None, + surface_context={ + "source": "webui", + "session_id": session_id, + "profile": getattr(s, "profile", None), + "workspace": s.workspace if s is not None else str(workspace), + }, + config_data=cfg, + ) prefill_messages = [ - {"role": "system", "content": _WEBUI_PROGRESS_PROMPT}, + {"role": "system", "content": _gateway_system_prompt}, *_prefill_messages_with_webui_context(prefill_context, cfg), ] put_gateway_event("context_status", { diff --git a/tests/test_webui_gateway_chat_backend.py b/tests/test_webui_gateway_chat_backend.py index 9a87bbc1..e26053a6 100644 --- a/tests/test_webui_gateway_chat_backend.py +++ b/tests/test_webui_gateway_chat_backend.py @@ -285,15 +285,23 @@ def test_gateway_chat_worker_translates_sse_and_persists_session(tmp_path, monke assert captured["headers"]["X-hermes-session-key"] == f"webui:{s.session_id}" assert '"stream": true' in captured["body"] payload = json.loads(captured["body"]) - assert [m["content"] for m in payload["messages"]] == [ - streaming._WEBUI_PROGRESS_PROMPT, + # #3324: the gateway path's first system message is now the full WebUI + # ephemeral system prompt (progress prompt + session/delivery context), + # NOT the bare _WEBUI_PROGRESS_PROMPT — otherwise the delivery/session + # context is silently dropped on Gateway-routed WebUI chats. + system_msg = payload["messages"][0] + assert system_msg["role"] == "system" + assert "Final visible assistant replies" in system_msg["content"] + assert "Need script" in system_msg["content"] + # The moved session/delivery context must be present in the system prompt. + assert "Connected Platforms:" in system_msg["content"] + assert "Delivery options for scheduled tasks:" in system_msg["content"] + # The recall prefill + the actual user turn follow the system message. + assert [m["content"] for m in payload["messages"][1:]] == [ "prefill", "webui session context", "Say hello", ] - assert payload["messages"][0]["role"] == "system" - assert "Final visible assistant replies" in payload["messages"][0]["content"] - assert "Need script" in payload["messages"][0]["content"] events = [] while not subscriber.empty(): events.append(subscriber.get_nowait())