| 1 | """Strip LLM-only inject prefixes from user-visible session content. |
| 2 | |
| 3 | Gate / story-confirm notes are prepended onto the inbound user text so the |
| 4 | current Agent turn can follow them. They must not be persisted or shown in |
| 5 | the chat thread / sidebar preview. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | AGENT_INJECT_STARTS = ( |
| 11 | "REFERENCE_IMAGE_GATE ", |
| 12 | "STORY_DIRECTION_CONFIRM ", |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | def is_agent_inject_user_text(content: str) -> bool: |
| 17 | text = (content or "").lstrip() |
| 18 | return any(text.startswith(prefix) for prefix in AGENT_INJECT_STARTS) |
| 19 | |
| 20 | |
| 21 | def visible_user_content(content: str) -> str: |
| 22 | """Return the user-facing remainder after an inject prefix, else *content*.""" |
| 23 | text = content if isinstance(content, str) else "" |
| 24 | stripped = text.strip() |
| 25 | if not is_agent_inject_user_text(stripped): |
| 26 | return text |
| 27 | parts = stripped.split("\n\n", 1) |
| 28 | if len(parts) == 2 and parts[1].strip(): |
| 29 | return parts[1].strip() |
| 30 | lines = [line.strip() for line in stripped.splitlines() if line.strip()] |
| 31 | if len(lines) >= 2: |
| 32 | return lines[-1] |
| 33 | return "" |
| 34 |