summaryrefslogtreecommitdiff
path: root/Omni/Agent
diff options
context:
space:
mode:
authorBen Sima <ben@bensima.com>2025-12-14 23:14:08 -0500
committerBen Sima <ben@bensima.com>2025-12-14 23:14:08 -0500
commita5030192113e43a80a95e4d48c40704546c31695 (patch)
tree7d96ee9cc8cf7982e0f958e902c94937d882d72b /Omni/Agent
parentf6bbf86e7e8e76c41b8163ce0b1996ee474fc560 (diff)
Fix message splitting to not use LLM
The haiku-based splitting was modifying message content. Replace with deterministic paragraph-based splitting that preserves the original text exactly.
Diffstat (limited to 'Omni/Agent')
-rw-r--r--Omni/Agent/Telegram.hs56
1 files changed, 19 insertions, 37 deletions
diff --git a/Omni/Agent/Telegram.hs b/Omni/Agent/Telegram.hs
index a61c2d0..ed25a14 100644
--- a/Omni/Agent/Telegram.hs
+++ b/Omni/Agent/Telegram.hs
@@ -1078,43 +1078,25 @@ checkAndSummarize openRouterKey uid chatId = do
putText "Conversation summarized and archived (gemini)"
splitMessageForChat :: Text -> Text -> IO [Text]
-splitMessageForChat openRouterKey message = do
- if Text.length message < 200
- then pure [message]
- else do
- let haiku = Provider.defaultOpenRouter openRouterKey "anthropic/claude-haiku-4.5"
- result <-
- Provider.chat
- haiku
- []
- [ Provider.Message
- Provider.System
- ( Text.unlines
- [ "Split this message into separate chat messages that feel natural in a messaging app.",
- "Each part should be logically independent - a complete thought.",
- "Separate parts with exactly '---' on its own line.",
- "Keep the original text, just add separators. Don't add any commentary.",
- "If the message is already short/simple, return it unchanged (no separators).",
- "Aim for 2-4 parts maximum. Don't over-split.",
- "",
- "Good splits: between topics, after questions, between a statement and follow-up",
- "Bad splits: mid-sentence, between closely related points"
- ]
- )
- Nothing
- Nothing,
- Provider.Message Provider.User message Nothing Nothing
- ]
- case result of
- Left err -> do
- putText <| "Message split failed: " <> err
- pure [message]
- Right msg -> do
- let parts = map Text.strip (Text.splitOn "---" (Provider.msgContent msg))
- validParts = filter (not <. Text.null) parts
- if null validParts
- then pure [message]
- else pure validParts
+splitMessageForChat _openRouterKey message = do
+ let parts = splitOnParagraphs message
+ pure parts
+
+splitOnParagraphs :: Text -> [Text]
+splitOnParagraphs message
+ | Text.length message < 300 = [message]
+ | otherwise =
+ let paragraphs = filter (not <. Text.null) (map Text.strip (Text.splitOn "\n\n" message))
+ in if length paragraphs <= 1
+ then [message]
+ else mergeTooShort paragraphs
+
+mergeTooShort :: [Text] -> [Text]
+mergeTooShort [] = []
+mergeTooShort [x] = [x]
+mergeTooShort (x : y : rest)
+ | Text.length x < 100 = mergeTooShort ((x <> "\n\n" <> y) : rest)
+ | otherwise = x : mergeTooShort (y : rest)
enqueueMultipart :: Maybe Text -> Int -> Maybe Int -> [Text] -> Maybe Text -> IO ()
enqueueMultipart _ _ _ [] _ = pure ()