Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(chat): word the suggested follow-up as the user's own request (#442)

Rufusb0bad78d709d
diff --git a/packages/core/openexecutive/api/routes/chat.py b/packages/core/openexecutive/api/routes/chat.py
index d700a962..52c3a839 100644
--- a/packages/core/openexecutive/api/routes/chat.py
+++ b/packages/core/openexecutive/api/routes/chat.py
@@ -2039,7 +2039,11 @@ async def _generate_prompts_via_llm(
subtitle_raw = data.get("subtitle")
if not isinstance(prompts_raw, list) or not isinstance(subtitle_raw, str):
return None
- prompts = [str(p).strip() for p in prompts_raw if isinstance(p, str) and p.strip()]
+ prompts = [
+ as_user_request(str(p).strip())
+ for p in prompts_raw
+ if isinstance(p, str) and p.strip()
+ ]
subtitle = subtitle_raw.strip()
if len(prompts) < 4 or not subtitle:
return None
@@ -2064,6 +2068,13 @@ _FOLLOWUP_SYSTEM = (
"or 'that'. Never repeat a question the user already asked. Never "
"suggest looping in, messaging, or following up with the user "
"themselves.\n\n"
+ "The suggestion is what the USER types, never the executive's own "
+ "words. When the last reply ends with an offer ('Should I draft a "
+ "follow-up to Andrew?', 'Want me to book it?'), the user accepts it "
+ "as a plain request: 'Draft a short follow-up to Andrew asking where "
+ "things stand.' Never copy the executive's offer, and never start "
+ "with 'Should I', 'Shall I', 'Want me to', 'Would you like me to' "
+ "or 'I can'.\n\n"
"Output ONLY a JSON object with exactly one key — no prose, no "
"markdown, no fences:\n"
' "suggestion": one sentence, 4-14 words, no greeting, no '
@@ -2100,7 +2111,96 @@ async def _generate_followup_via_llm(transcript: str) -> str | None:
# A runaway or empty answer is worse than no suggestion in the composer.
if not suggestion or len(suggestion.split()) > _FOLLOWUP_MAX_WORDS:
return None
- return suggestion
+ return as_user_request(suggestion, last_executive_reply=_last_executive_reply(transcript))
+
+
+def _last_executive_reply(transcript: str) -> str:
+ marker = "EXECUTIVE: "
+ i = transcript.rfind(marker)
+ return transcript[i + len(marker) :] if i >= 0 else ""
+
+
+# Openers that can only be the executive offering to do something. The
+# model sometimes copies the reply's closing offer into the composer as-is.
+_OFFER_OPENERS = re.compile(
+ r"^(?:(?:do you )?want me to|would you like me to|shall i)\s+",
+ re.IGNORECASE,
+)
+# "Should I …?" is also how a user asks for advice ("Should I raise
+# prices?"), so it is only an offer when the executive's reply made it.
+_SHOULD_I = re.compile(r"^should i\s+", re.IGNORECASE)
+# Executive-voice pronouns flip to the user's own once the offer is theirs:
+# "Want me to remind you?" -> "Remind me."
+_PRONOUN_FLIPS = {
+ "you": "me",
+ "your": "my",
+ "yours": "mine",
+ "yourself": "myself",
+ "you're": "I'm",
+ "you've": "I've",
+ "you'll": "I'll",
+ "you'd": "I'd",
+}
+_PRONOUN_RE = re.compile(
+ r"\b(" + "|".join(sorted(map(re.escape, _PRONOUN_FLIPS), key=len, reverse=True)) + r")\b",
+ re.IGNORECASE,
+)
+
+
+# "you" opening a clause is a subject: "check if you have time" needs "I",
+# not "me".
+_SUBJECT_YOU_RE = re.compile(
+ r"\b(if|when|whether|that|so|until|before|after|while)\s+you\b(?!')",
+ re.IGNORECASE,
+)
+# ...and so is "you" before a verb: "tell Sam you asked" -> "I asked". Not
+# after a verb that takes "you" as its object, where the next word is a bare
+# infinitive: "help you plan the offsite" -> "help me plan the offsite".
+_YOU_BEFORE_VERB_RE = re.compile(
+ r"\b(?:(\w+)\s+)?you(?=\s+(?:have|had|want|wanted|need|needed|asked|said|"
+ r"mentioned|can|could|will|would|should|might|plan|planned|think|thought)\b)",
+ re.IGNORECASE,
+)
+_OBJECT_YOU_VERBS = frozenset(
+ {"help", "let", "make", "have", "see", "watch", "hear", "bid", "get", "remind", "ask", "tell"}
+)
+
+
+def _subject_you(m: re.Match[str]) -> str:
+ before = m.group(1)
+ if before and before.lower() in _OBJECT_YOU_VERBS:
+ return m.group(0)
+ return (before + " " if before else "") + "I"
+
+
+def _norm(text: str) -> str:
+ return re.sub(r"[^a-z0-9 ]", "", " ".join(text.lower().split()))
+
+
+def as_user_request(text: str, *, last_executive_reply: str = "") -> str:
+ """Turn an executive-voice offer into the user's own request.
+
+ "Should I draft a short follow-up to Andrew?" (copied from the reply)
+ becomes "Draft a short follow-up to Andrew." Anything that does not open
+ with an offer is returned unchanged.
+ """
+ rest: str | None = None
+ m = _OFFER_OPENERS.match(text)
+ if m:
+ rest = text[m.end() :]
+ else:
+ m = _SHOULD_I.match(text)
+ if m and _norm(text.rstrip("?")) in _norm(last_executive_reply):
+ rest = text[m.end() :]
+ if not rest:
+ return text
+ rest = _SUBJECT_YOU_RE.sub(lambda p: p.group(1) + " I", rest)
+ rest = _YOU_BEFORE_VERB_RE.sub(_subject_you, rest)
+ rest = _PRONOUN_RE.sub(lambda p: _PRONOUN_FLIPS[p.group(1).lower()], rest)
+ rest = rest.rstrip(" ?.!")
+ if not rest:
+ return text
+ return rest[0].upper() + rest[1:] + "."
@router.get("/chat/suggested-prompts")
diff --git a/packages/core/tests/unit/test_session_followup.py b/packages/core/tests/unit/test_session_followup.py
index dc8bc6d8..2eb6df6f 100644
--- a/packages/core/tests/unit/test_session_followup.py
+++ b/packages/core/tests/unit/test_session_followup.py
@@ -214,3 +214,52 @@ async def test_generator_parses_fenced_json(monkeypatch: pytest.MonkeyPatch) ->
async def test_generator_rejects_bad_output(monkeypatch: pytest.MonkeyPatch, raw: str) -> None:
_patch_provider(monkeypatch, raw)
assert await chat_route._generate_followup_via_llm("t") is None
+
+
+_ANDREW_REPLY = (
+ "USER: has andrew from northwind reached out today?\n\n"
+ "EXECUTIVE: No. Nothing from Andrew has reached your inbox in the last two days. "
+ "Should I draft a short follow-up to Andrew asking where things stand?"
+)
+
+
+async def test_generator_turns_copied_offer_into_users_request(monkeypatch: pytest.MonkeyPatch) -> None:
+ _patch_provider(
+ monkeypatch,
+ '{"suggestion": "Should I draft a short follow-up to Andrew asking where things stand?"}',
+ )
+ assert (
+ await chat_route._generate_followup_via_llm(_ANDREW_REPLY)
+ == "Draft a short follow-up to Andrew asking where things stand."
+ )
+
+
+ ("offer", "expected"),
+ [
+ ("Want me to book 30 minutes with Priya on Thursday?", "Book 30 minutes with Priya on Thursday."),
+ ("Do you want me to remind you tomorrow?", "Remind me tomorrow."),
+ ("Would you like me to send your notes to the board?", "Send my notes to the board."),
+ ("Shall I check if you have time Friday?", "Check if I have time Friday."),
+ ("Want me to tell Sam you're out next week?", "Tell Sam I'm out next week."),
+ ("Want me to tell Sam you asked about the budget?", "Tell Sam I asked about the budget."),
+ ("Want me to help you plan the offsite?", "Help me plan the offsite."),
+ ("Want me to let you have the draft first?", "Let me have the draft first."),
+ ],
+)
+def test_offers_become_requests(offer: str, expected: str) -> None:
+ assert chat_route.as_user_request(offer) == expected
+
+
+ "text",
+ [
+ "Draft the customer email announcing the Pro increase.",
+ "What's driving the churn in the mid-market segment?",
+ # The user's own question, not an echo of an offer: advice, kept as is.
+ "Should I raise prices on the Pro tier before renewals?",
+ ],
+)
+def test_user_voice_is_left_alone(text: str) -> None:
+ assert chat_route.as_user_request(text, last_executive_reply="Churn rose 4% last quarter.") == text