From f752330c9562b7a1bbdce15c05106a577daa2392 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Sat, 13 Dec 2025 01:14:38 -0500 Subject: telegram: add conversation context to group pre-filter Pre-filter now sees last 5 messages so it can detect when user is continuing a conversation with Ava, even without explicit mention. - Fetch recent messages before shouldEngageInGroup - Update classifier prompt to understand Ava context - Handle follow-up messages to bot's previous responses --- Omni/Agent/Telegram.hs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/Omni/Agent/Telegram.hs b/Omni/Agent/Telegram.hs index 27d7413..ee6784b 100644 --- a/Omni/Agent/Telegram.hs +++ b/Omni/Agent/Telegram.hs @@ -437,7 +437,21 @@ handleAuthorizedMessage tgConfig provider engineCfg msg uid userName chatId = do if Types.isGroupChat msg then do putText "Checking if should engage (group chat)..." - shouldEngageInGroup (Types.tgOpenRouterApiKey tgConfig) userMessage + recentMsgs <- Memory.getRecentMessages uid chatId 5 + let recentContext = + if null recentMsgs + then "" + else + Text.unlines + [ "[Recent conversation for context]", + Text.unlines + [ (if Memory.cmRole m == Memory.UserRole then "User: " else "Ava: ") <> Memory.cmContent m + | m <- reverse recentMsgs + ], + "", + "[New message to classify]" + ] + shouldEngageInGroup (Types.tgOpenRouterApiKey tgConfig) (recentContext <> userMessage) else pure True if not shouldEngage @@ -597,20 +611,23 @@ shouldEngageInGroup openRouterKey messageText = do [ Provider.Message Provider.System ( Text.unlines - [ "You are a classifier that decides if an AI assistant should respond to a message in a group chat.", + [ "You are a classifier that decides if an AI assistant named 'Ava' should respond to a message in a group chat.", + "You may be given recent conversation context to help decide.", "Respond with ONLY 'yes' or 'no' (lowercase, nothing else).", "", "Say 'yes' if:", - "- The message is a direct question the assistant could answer", + "- The message is a direct question Ava could answer", "- The message contains a factual error worth correcting", - "- The message mentions the bot or asks for help", + "- The message mentions Ava or asks for help", "- The message shares a link or document to analyze", + "- The message is a follow-up to a conversation Ava was just participating in", + "- The user is clearly talking to Ava based on context (e.g. Ava just responded)", "", "Say 'no' if:", - "- It's casual banter or chit-chat between people", - "- It's a greeting or farewell", - "- It's an inside joke or personal conversation", - "- It doesn't require or benefit from bot input" + "- It's casual banter or chit-chat between people (not involving Ava)", + "- It's a greeting or farewell not directed at Ava", + "- It's an inside joke or personal conversation between humans", + "- It doesn't require or benefit from Ava's input" ] ) Nothing -- cgit v1.2.3