{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}

module Data.Ollama.Delete
  ( -- * Delete downloaded Models
    deleteModel
  ) where

import Control.Monad (when)
import Data.Aeson
import Data.Ollama.Common.Utils qualified as CU
import Data.Text (Text)
import Data.Text qualified as T
import Network.HTTP.Client
import Network.HTTP.Types.Status (status404)

-- TODO: Add Options parameter
-- TODO: Add Context parameter
newtype DeleteModelReq = DeleteModelReq {DeleteModelReq -> Text
name :: Text}
  deriving newtype (Int -> DeleteModelReq -> ShowS
[DeleteModelReq] -> ShowS
DeleteModelReq -> String
(Int -> DeleteModelReq -> ShowS)
-> (DeleteModelReq -> String)
-> ([DeleteModelReq] -> ShowS)
-> Show DeleteModelReq
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DeleteModelReq -> ShowS
showsPrec :: Int -> DeleteModelReq -> ShowS
$cshow :: DeleteModelReq -> String
show :: DeleteModelReq -> String
$cshowList :: [DeleteModelReq] -> ShowS
showList :: [DeleteModelReq] -> ShowS
Show, DeleteModelReq -> DeleteModelReq -> Bool
(DeleteModelReq -> DeleteModelReq -> Bool)
-> (DeleteModelReq -> DeleteModelReq -> Bool) -> Eq DeleteModelReq
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DeleteModelReq -> DeleteModelReq -> Bool
== :: DeleteModelReq -> DeleteModelReq -> Bool
$c/= :: DeleteModelReq -> DeleteModelReq -> Bool
/= :: DeleteModelReq -> DeleteModelReq -> Bool
Eq, [DeleteModelReq] -> Value
[DeleteModelReq] -> Encoding
DeleteModelReq -> Bool
DeleteModelReq -> Value
DeleteModelReq -> Encoding
(DeleteModelReq -> Value)
-> (DeleteModelReq -> Encoding)
-> ([DeleteModelReq] -> Value)
-> ([DeleteModelReq] -> Encoding)
-> (DeleteModelReq -> Bool)
-> ToJSON DeleteModelReq
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: DeleteModelReq -> Value
toJSON :: DeleteModelReq -> Value
$ctoEncoding :: DeleteModelReq -> Encoding
toEncoding :: DeleteModelReq -> Encoding
$ctoJSONList :: [DeleteModelReq] -> Value
toJSONList :: [DeleteModelReq] -> Value
$ctoEncodingList :: [DeleteModelReq] -> Encoding
toEncodingList :: [DeleteModelReq] -> Encoding
$comitField :: DeleteModelReq -> Bool
omitField :: DeleteModelReq -> Bool
ToJSON)

-- | Delete a model
deleteModel ::
  -- | Model name
  Text ->
  IO ()
deleteModel :: Text -> IO ()
deleteModel Text
modelName =
  do
    let url :: Text
url = OllamaClient -> Text
CU.host OllamaClient
CU.defaultOllama
    Manager
manager <- ManagerSettings -> IO Manager
newManager ManagerSettings
defaultManagerSettings
    Request
initialRequest <- String -> IO Request
forall (m :: * -> *). MonadThrow m => String -> m Request
parseRequest (String -> IO Request) -> String -> IO Request
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack (Text
url Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/api/delete")
    let reqBody :: DeleteModelReq
reqBody =
          DeleteModelReq {$sel:name:DeleteModelReq :: Text
name = Text
modelName}
        request :: Request
request =
          Request
initialRequest
            { method = "DELETE"
            , requestBody = RequestBodyLBS $ encode reqBody
            }
    Response ByteString
response <- Request -> Manager -> IO (Response ByteString)
httpLbs Request
request Manager
manager
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when
      (Response ByteString -> Status
forall body. Response body -> Status
responseStatus Response ByteString
response Status -> Status -> Bool
forall a. Eq a => a -> a -> Bool
== Status
status404)
      (String -> IO ()
putStrLn String
"Model does not exist")