Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof Of Concept Repl Bots via nix-shell #32

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
20 changes: 11 additions & 9 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ import OptionsParser
import System.Environment.XDG.BaseDir ( getUserCacheDir )
import System.Process.Typed

--main :: IO ()
--main = withProcessWait_ ghciConfig $ \process -> do
-- runSimpleBot (simplifySessionBot (T.intercalate "\n" . printCalcOutput) programP $ sessionize mempty $ calculatorBot) mempty
-- runSimpleBot (ghciBot process) mempty
main :: IO ()
main = cliMain

-- let ghciBot' = ghciBot process
-- calcBot = simplifySessionBot (T.intercalate "\n" . printCalcOutput) programP $ sessionize mempty $ calculatorBot
-- runSimpleBot (rmap (\(x :& y) -> x <> y ) $ ghciBot' /\ calcBot) (mempty)
cliMain :: IO ()
cliMain = withProcessWait_ pythonConfig $ \pythonProcess -> withProcessWait_ nodeConfig $ \process -> do
solomon-b marked this conversation as resolved.
Show resolved Hide resolved
runTextBot (rmap (\(x :& y) -> x <> y ) $ nodeBot process /\ pythonBot pythonProcess) mempty
--runSimpleBot (simplifySessionBot (T.intercalate "\n" . printCalcOutput) programP $ sessionize mempty $ calculatorBot) mempty
--let ghciBot' = ghciBot process
-- calcBot = simplifySessionBot (T.intercalate "\n" . printCalcOutput) programP $ sessionize mempty $ calculatorBot
--runTextBot (rmap (\(x :& y) -> x <> y ) $ ghciBot' /\ calcBot) (mempty)

main :: IO ()
main = withProcessWait_ ghciConfig $ \process -> do
matrixMain :: IO ()
matrixMain = withProcessWait_ ghciConfig $ \process -> do
void $ threadDelay 1e6
void $ hGetOutput (getStdout process)
command <- Opt.execParser parserInfo
Expand Down
6 changes: 5 additions & 1 deletion cofree-bot.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ library
CofreeBot.Bot.Behaviors.Calculator
CofreeBot.Bot.Behaviors.Calculator.Language
CofreeBot.Bot.Behaviors.CoinFlip
CofreeBot.Bot.Behaviors.GHCI
CofreeBot.Bot.Behaviors.Hello
CofreeBot.Bot.Behaviors.Repl
CofreeBot.Bot.Behaviors.Repl.GHCI
CofreeBot.Bot.Behaviors.Repl.Node
CofreeBot.Bot.Behaviors.Repl.Python
CofreeBot.Bot.Behaviors.Repl.Util
CofreeBot.Bot.Context
CofreeBot.Utils

Expand Down
4 changes: 2 additions & 2 deletions src/CofreeBot/Bot/Behaviors.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module CofreeBot.Bot.Behaviors
( module Calculator
, module CoinFlip
, module GHCI
, module Hello
, module Repl
) where

import CofreeBot.Bot.Behaviors.Calculator
as Calculator
import CofreeBot.Bot.Behaviors.CoinFlip
as CoinFlip
import CofreeBot.Bot.Behaviors.GHCI as GHCI
import CofreeBot.Bot.Behaviors.Hello as Hello
import CofreeBot.Bot.Behaviors.Repl as Repl
12 changes: 12 additions & 0 deletions src/CofreeBot/Bot/Behaviors/Repl.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module CofreeBot.Bot.Behaviors.Repl
( module Util
, module GHCI
, module Node
, module Python
) where

import CofreeBot.Bot.Behaviors.Repl.Util as Util
import CofreeBot.Bot.Behaviors.Repl.GHCI as GHCI
import CofreeBot.Bot.Behaviors.Repl.Node as Node
import CofreeBot.Bot.Behaviors.Repl.Python as Python

25 changes: 25 additions & 0 deletions src/CofreeBot/Bot/Behaviors/Repl/GHCI.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{-# LANGUAGE NumDecimals #-}
module CofreeBot.Bot.Behaviors.Repl.GHCI
( ghciBot
, ghciConfig
) where

import CofreeBot.Bot
import CofreeBot.Bot.Behaviors.Repl.Util
import CofreeBot.Utils
import Data.Profunctor
import System.IO
import System.Process.Typed

ghciBot' :: Process Handle Handle () -> ReplBot
ghciBot' = replBot "ghci: "

ghciBot :: Process Handle Handle () -> ReplBot
ghciBot p =
dimap (distinguish (/= "ghci: :q")) indistinct
$ pureStatelessBot (const $ ["I'm Sorry Dave"])
\/ ghciBot' p

ghciConfig :: ProcessConfig Handle Handle ()
ghciConfig = replConfig "docker run -i --rm haskell 2>&1"

14 changes: 14 additions & 0 deletions src/CofreeBot/Bot/Behaviors/Repl/Node.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module CofreeBot.Bot.Behaviors.Repl.Node
( nodeBot
, nodeConfig
) where

import CofreeBot.Bot.Behaviors.Repl.Util
import System.IO
import System.Process.Typed

nodeBot :: Process Handle Handle () -> ReplBot
nodeBot = replBot "node: "

nodeConfig :: ProcessConfig Handle Handle ()
nodeConfig = replConfig "nix-shell -p nodejs --run 'node -i' 2>&1"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i need to get this command running in a docker container.

14 changes: 14 additions & 0 deletions src/CofreeBot/Bot/Behaviors/Repl/Python.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module CofreeBot.Bot.Behaviors.Repl.Python
( pythonBot
, pythonConfig
) where

import CofreeBot.Bot.Behaviors.Repl.Util
import System.IO
import System.Process.Typed

pythonBot :: Process Handle Handle () -> ReplBot
pythonBot = replBot "python: "

pythonConfig :: ProcessConfig Handle Handle ()
pythonConfig = replConfig "nix-shell -p python3 --run 'python -iq' 2>&1"
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
{-# LANGUAGE NumDecimals #-}
module CofreeBot.Bot.Behaviors.GHCI
( ghciBot
, ghciConfig
, hGetOutput
) where
module CofreeBot.Bot.Behaviors.Repl.Util where

import CofreeBot.Bot
import CofreeBot.Utils
import Control.Monad
import Control.Monad.Loops ( whileM )
import Data.Attoparsec.Text as A
import Data.Profunctor
import qualified Data.Text as T
import GHC.Conc ( threadDelay )
import System.IO
import System.Process.Typed

type GhciBot = Bot IO () T.Text [T.Text]
type ReplBot = Bot IO () T.Text [T.Text]

hGetOutput :: Handle -> IO String
hGetOutput handle = whileM (hReady handle) (hGetChar handle)

ghciBot' :: Process Handle Handle () -> GhciBot
ghciBot' p =
mapMaybeBot (either (const Nothing) Just . parseOnly ghciInputParser)
replBot :: T.Text -> Process Handle Handle () -> ReplBot
replBot prompt p =
mapMaybeBot (either (const Nothing) Just . parseOnly (replInputParser prompt))
$ Bot
$ \i s -> do
hPutStrLn (getStdin p) $ T.unpack i
Expand All @@ -32,18 +26,11 @@ ghciBot' p =
o <- hGetOutput (getStdout p)
pure $ BotAction (pure $ T.pack o) s

ghciBot :: Process Handle Handle () -> GhciBot
ghciBot p =
dimap (distinguish (/= "ghci: :q")) indistinct
$ pureStatelessBot (const $ ["I'm Sorry Dave"])
\/ ghciBot' p
replConfig :: String -> ProcessConfig Handle Handle ()
replConfig = setStdin createPipe . setStdout createPipe . shell

ghciConfig :: ProcessConfig Handle Handle ()
ghciConfig = setStdin createPipe $ setStdout createPipe $ shell
"docker run -i --rm haskell 2>&1"

ghciInputParser :: Parser T.Text
ghciInputParser = do
void $ "ghci: "
replInputParser :: T.Text -> Parser T.Text
replInputParser prompt = do
void $ string prompt
T.pack <$> many1 anyChar