@@ -13,7 +13,10 @@ import Control.Monad.Except
13
13
import Control.Monad.IO.Class (liftIO )
14
14
import Data.Foldable
15
15
import GHC.Conc (threadDelay )
16
+ import Network.HTTP.Client qualified as HTTP
17
+ import Network.HTTP.Client.TLS qualified as HTTP.TLS
16
18
import Network.Matrix.Client
19
+ import OpenAI.Client (makeOpenAIClient )
17
20
import Options.Applicative qualified as Opt
18
21
import OptionsParser
19
22
import System.Environment.XDG.BaseDir (getUserCacheDir )
@@ -23,17 +26,17 @@ main :: IO ()
23
26
main = do
24
27
command <- Opt. execParser parserInfo
25
28
xdgCache <- getUserCacheDir " cofree-bot"
26
-
29
+ httpManager <- HTTP. newManager HTTP.TLS. tlsManagerSettings
27
30
case command of
28
- LoginCmd cred -> do
31
+ LoginCmd cred openAIKey -> do
29
32
session <- login cred
30
- matrixMain session xdgCache
31
- TokenCmd TokenCredentials {.. } -> do
33
+ matrixMain session xdgCache httpManager openAIKey
34
+ TokenCmd TokenCredentials {.. } openAIKey -> do
32
35
session <- createSession (getMatrixServer matrixServer) matrixToken
33
- matrixMain session xdgCache
34
- CLI -> cliMain xdgCache
36
+ matrixMain session xdgCache httpManager openAIKey
37
+ CLI openAIKey -> cliMain xdgCache httpManager openAIKey
35
38
36
- bot process =
39
+ bot process manager ( OpenAIKey aiKey) =
37
40
let calcBot =
38
41
embedTextBot $
39
42
simplifySessionBot printCalcOutput statementP $
@@ -43,29 +46,31 @@ bot process =
43
46
coinFlipBot' = embedTextBot $ simplifyCoinFlipBot coinFlipBot
44
47
ghciBot' = embedTextBot $ ghciBot process
45
48
magic8BallBot' = embedTextBot $ simplifyMagic8BallBot magic8BallBot
49
+ openAIBot' = openAIBot $ makeOpenAIClient aiKey manager 2
46
50
in calcBot
47
51
/.\ coinFlipBot'
48
52
/.\ helloBot
49
53
/.\ ghciBot'
50
54
/.\ magic8BallBot'
51
55
/.\ updogMatrixBot
52
56
/.\ embedTextBot jitsiBot
57
+ /.\ embedTextBot openAIBot'
53
58
54
- cliMain :: FilePath -> IO ()
55
- cliMain xdgCache = withProcessWait_ ghciConfig $ \ process -> do
59
+ cliMain :: FilePath -> HTTP. Manager -> OpenAIKey -> IO ()
60
+ cliMain xdgCache manager openAIKey = withProcessWait_ ghciConfig $ \ process -> do
56
61
void $ threadDelay 1e6
57
62
void $ hGetOutput (getStdout process)
58
63
state <- readState xdgCache
59
- fixedBot <- flip (fixBotPersistent xdgCache) (fold state) $ simplifyMatrixBot $ bot process
64
+ fixedBot <- flip (fixBotPersistent xdgCache) (fold state) $ simplifyMatrixBot $ bot process manager openAIKey
60
65
void $ loop $ annihilate repl fixedBot
61
66
62
67
unsafeCrashInIO :: Show e => ExceptT e IO a -> IO a
63
68
unsafeCrashInIO = runExceptT >=> either (fail . show ) pure
64
69
65
- matrixMain :: ClientSession -> FilePath -> IO ()
66
- matrixMain session xdgCache = withProcessWait_ ghciConfig $ \ process -> do
70
+ matrixMain :: ClientSession -> FilePath -> HTTP. Manager -> OpenAIKey -> IO ()
71
+ matrixMain session xdgCache manager openAIKey = withProcessWait_ ghciConfig $ \ process -> do
67
72
void $ threadDelay 1e6
68
73
void $ hGetOutput (getStdout process)
69
74
state <- readState xdgCache
70
- fixedBot <- flip (fixBotPersistent xdgCache) (fold state) $ hoistBot liftIO $ bot process
75
+ fixedBot <- flip (fixBotPersistent xdgCache) (fold state) $ hoistBot liftIO $ bot process manager openAIKey
71
76
unsafeCrashInIO $ loop $ annihilate (matrix session xdgCache) $ batch fixedBot
0 commit comments