Skip to content

The agent that grows with you

refactor(gateway): one helper for prefixing per-turn notes onto model input

Brooklyn Nicholson2c94e3fb63b3
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index 4b58252b34d..c0bda0f8d35 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -9513,6 +9513,24 @@ def _start_notification_poller(sid: str, session: dict) -> threading.Event:
return stop
+def _prepend_note(run_message: Any, note: str) -> Any:
+ """Prefix a per-turn note onto the MODEL INPUT, leaving the prompt alone.
+
+ Everything the model needs to know about the turn but the user did not
+ type — an interrupted reply, reactions, the surface they typed into —
+ arrives this way. persist_user_message keeps the clean prompt, so no
+ scaffolding reaches the transcript, and annotating the NEW turn never
+ rewrites an already-sent message, so the cached prefix survives.
+ """
+ if not note:
+ return run_message
+ if isinstance(run_message, str):
+ return f"{note}\n\n{run_message}"
+ if isinstance(run_message, list):
+ return [{"type": "text", "text": note}, *run_message]
+ return run_message
+
+
def _run_prompt_submit(
rid,
sid: str,
@@ -9765,21 +9783,10 @@ def _run_prompt_submit(
from tools.tts_streaming import SPEECH_INTERRUPTED_NOTE, take_speech_interrupted
if take_speech_interrupted():
- if isinstance(run_message, str):
- run_message = f"{SPEECH_INTERRUPTED_NOTE}\n\n{run_message}"
- elif isinstance(run_message, list):
- run_message = [{"type": "text", "text": SPEECH_INTERRUPTED_NOTE}, *run_message]
-
- # Reactions the user added since the last turn ride the MODEL INPUT
- # only (same enrichment channel as the speech-interrupted note);
- # persist_user_message below stays the clean prompt, so no
- # scaffolding reaches the transcript. Cache-safe: annotating the
- # NEW turn never rewrites an already-sent message.
- if reaction_notes := _pending_reaction_notes(session):
- if isinstance(run_message, str):
- run_message = f"{reaction_notes}\n\n{run_message}"
- elif isinstance(run_message, list):
- run_message = [{"type": "text", "text": reaction_notes}, *run_message]
+ run_message = _prepend_note(run_message, SPEECH_INTERRUPTED_NOTE)
+
+ # Reactions the user added since the last turn.
+ run_message = _prepend_note(run_message, _pending_reaction_notes(session))
def _stream(delta):
with session["history_lock"]: