diff options
Diffstat (limited to 'Omni/Agent/Telegram.hs')
| -rw-r--r-- | Omni/Agent/Telegram.hs | 77 |
1 files changed, 76 insertions, 1 deletions
diff --git a/Omni/Agent/Telegram.hs b/Omni/Agent/Telegram.hs index 34cf0d1..a61c2d0 100644 --- a/Omni/Agent/Telegram.hs +++ b/Omni/Agent/Telegram.hs @@ -92,6 +92,7 @@ import qualified Omni.Agent.Tools.Email as Email import qualified Omni.Agent.Tools.Hledger as Hledger import qualified Omni.Agent.Tools.Http as Http import qualified Omni.Agent.Tools.Notes as Notes +import qualified Omni.Agent.Tools.Outreach as Outreach import qualified Omni.Agent.Tools.Pdf as Pdf import qualified Omni.Agent.Tools.Python as Python import qualified Omni.Agent.Tools.Todos as Todos @@ -566,6 +567,22 @@ handleAuthorizedMessage :: handleAuthorizedMessage tgConfig provider engineCfg msg uid userName chatId = do Reminders.recordUserChat uid chatId + let msgText = Types.tmText msg + threadId = Types.tmThreadId msg + cmdHandled <- handleOutreachCommand tgConfig chatId threadId msgText + when cmdHandled (pure ()) + unless cmdHandled <| handleAuthorizedMessageContinued tgConfig provider engineCfg msg uid userName chatId + +handleAuthorizedMessageContinued :: + Types.TelegramConfig -> + Provider.Provider -> + Engine.EngineConfig -> + Types.TelegramMessage -> + Text -> + Text -> + Int -> + IO () +handleAuthorizedMessageContinued tgConfig provider engineCfg msg uid userName chatId = do pdfContent <- case Types.tmDocument msg of Just doc | Types.isPdf doc -> do putText <| "Processing PDF: " <> fromMaybe "(unnamed)" (Types.tdFileName doc) @@ -963,7 +980,8 @@ processEngagedMessage tgConfig provider engineCfg msg uid userName chatId userMe else [] pythonTools = [Python.pythonExecTool] httpTools = Http.allHttpTools - tools = memoryTools <> searchTools <> webReaderTools <> pdfTools <> notesTools <> calendarTools <> todoTools <> messageTools <> hledgerTools <> emailTools <> pythonTools <> httpTools + outreachTools = Outreach.allOutreachTools + tools = memoryTools <> searchTools <> webReaderTools <> pdfTools <> notesTools <> calendarTools <> todoTools <> messageTools <> hledgerTools <> emailTools <> pythonTools <> httpTools <> outreachTools let agentCfg = Engine.defaultAgentConfig @@ -1261,3 +1279,60 @@ loadAllowedUserIds = do Just idsStr -> do let ids = mapMaybe (readMaybe <. Text.unpack <. Text.strip) (Text.splitOn "," (Text.pack idsStr)) pure ids + +handleOutreachCommand :: Types.TelegramConfig -> Int -> Maybe Int -> Text -> IO Bool +handleOutreachCommand _tgConfig chatId mThreadId cmd + | "/review" `Text.isPrefixOf` cmd = do + pending <- Outreach.listDrafts Outreach.Pending + case pending of + [] -> do + _ <- Messages.enqueueImmediate Nothing chatId mThreadId "no pending outreach drafts" (Just "system") Nothing + pure True + (draft : _) -> do + let msg = formatDraftForReview draft + _ <- Messages.enqueueImmediate Nothing chatId mThreadId msg (Just "system") Nothing + pure True + | "/approve " `Text.isPrefixOf` cmd = do + let draftId = Text.strip (Text.drop 9 cmd) + result <- Outreach.approveDraft draftId + case result of + Left err -> do + _ <- Messages.enqueueImmediate Nothing chatId mThreadId ("error: " <> err) (Just "system") Nothing + pure True + Right draft -> do + _ <- Messages.enqueueImmediate Nothing chatId mThreadId ("approved: " <> Outreach.draftId draft) (Just "system") Nothing + pure True + | "/reject " `Text.isPrefixOf` cmd = do + let rest = Text.strip (Text.drop 8 cmd) + (draftId, reason) = case Text.breakOn " " rest of + (did, r) -> (did, if Text.null r then Nothing else Just (Text.strip r)) + result <- Outreach.rejectDraft draftId reason + case result of + Left err -> do + _ <- Messages.enqueueImmediate Nothing chatId mThreadId ("error: " <> err) (Just "system") Nothing + pure True + Right draft -> do + let reasonMsg = maybe "" (" reason: " <>) (Outreach.draftRejectReason draft) + _ <- Messages.enqueueImmediate Nothing chatId mThreadId ("rejected: " <> Outreach.draftId draft <> reasonMsg) (Just "system") Nothing + pure True + | "/queue" `Text.isPrefixOf` cmd = do + count <- Outreach.getPendingCount + _ <- Messages.enqueueImmediate Nothing chatId mThreadId (tshow count <> " pending outreach drafts") (Just "system") Nothing + pure True + | otherwise = pure False + +formatDraftForReview :: Outreach.OutreachDraft -> Text +formatDraftForReview draft = + Text.unlines + [ "*outreach draft*", + "", + "*id:* `" <> Outreach.draftId draft <> "`", + "*type:* " <> tshow (Outreach.draftType draft), + "*to:* " <> Outreach.draftRecipient draft, + maybe "" (\s -> "*subject:* " <> s <> "\n") (Outreach.draftSubject draft), + "*context:* " <> Outreach.draftContext draft, + "", + Outreach.draftBody draft, + "", + "reply `/approve " <> Outreach.draftId draft <> "` or `/reject " <> Outreach.draftId draft <> " [reason]`" + ] |
