-- | The `Tool` type
module OpenAI.V1.Tool
    ( -- * Types
      Tool(..)
    , RankingOptions(..)
    , FileSearch(..)
    , Function(..)
    , ToolChoice(..)
    , CodeInterpreterContainer(..)
      -- * Constants
    , toolChoiceNoneText
    , toolChoiceAutoText
    , toolChoiceRequiredText
      -- * Helpers
    , codeInterpreter
    , codeInterpreterAuto
    , codeInterpreterWithFiles
    , toolToResponsesValue
    , parseResponsesToolValue
    , toolChoiceToResponsesValue
    , parseResponsesToolChoiceValue
    , flattenToolValue
    , unflattenToolValue
    ) where

import Data.Aeson ((.:), (.:?), (.=))
import Data.Aeson.Types (Parser)
import OpenAI.Prelude
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Key as Key
import qualified Data.Aeson.KeyMap as KeyMap
import qualified Data.HashSet as HashSet
import Data.HashSet (HashSet)
import Data.List (partition)
import qualified Data.Vector as V

-- | Tool choice string constants
toolChoiceNoneText, toolChoiceAutoText, toolChoiceRequiredText :: Text
toolChoiceNoneText :: Text
toolChoiceNoneText = Text
"none"
toolChoiceAutoText :: Text
toolChoiceAutoText = Text
"auto"
toolChoiceRequiredText :: Text
toolChoiceRequiredText = Text
"required"

-- | The ranking options for the file search
data RankingOptions = RankingOptions
    { RankingOptions -> Maybe Text
ranker :: Maybe Text
    , RankingOptions -> Double
score_threshold :: Double
    } deriving stock ((forall x. RankingOptions -> Rep RankingOptions x)
-> (forall x. Rep RankingOptions x -> RankingOptions)
-> Generic RankingOptions
forall x. Rep RankingOptions x -> RankingOptions
forall x. RankingOptions -> Rep RankingOptions x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. RankingOptions -> Rep RankingOptions x
from :: forall x. RankingOptions -> Rep RankingOptions x
$cto :: forall x. Rep RankingOptions x -> RankingOptions
to :: forall x. Rep RankingOptions x -> RankingOptions
Generic, Int -> RankingOptions -> ShowS
[RankingOptions] -> ShowS
RankingOptions -> String
(Int -> RankingOptions -> ShowS)
-> (RankingOptions -> String)
-> ([RankingOptions] -> ShowS)
-> Show RankingOptions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RankingOptions -> ShowS
showsPrec :: Int -> RankingOptions -> ShowS
$cshow :: RankingOptions -> String
show :: RankingOptions -> String
$cshowList :: [RankingOptions] -> ShowS
showList :: [RankingOptions] -> ShowS
Show)
      deriving anyclass (Maybe RankingOptions
Value -> Parser [RankingOptions]
Value -> Parser RankingOptions
(Value -> Parser RankingOptions)
-> (Value -> Parser [RankingOptions])
-> Maybe RankingOptions
-> FromJSON RankingOptions
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser RankingOptions
parseJSON :: Value -> Parser RankingOptions
$cparseJSONList :: Value -> Parser [RankingOptions]
parseJSONList :: Value -> Parser [RankingOptions]
$comittedField :: Maybe RankingOptions
omittedField :: Maybe RankingOptions
FromJSON, [RankingOptions] -> Value
[RankingOptions] -> Encoding
RankingOptions -> Bool
RankingOptions -> Value
RankingOptions -> Encoding
(RankingOptions -> Value)
-> (RankingOptions -> Encoding)
-> ([RankingOptions] -> Value)
-> ([RankingOptions] -> Encoding)
-> (RankingOptions -> Bool)
-> ToJSON RankingOptions
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: RankingOptions -> Value
toJSON :: RankingOptions -> Value
$ctoEncoding :: RankingOptions -> Encoding
toEncoding :: RankingOptions -> Encoding
$ctoJSONList :: [RankingOptions] -> Value
toJSONList :: [RankingOptions] -> Value
$ctoEncodingList :: [RankingOptions] -> Encoding
toEncodingList :: [RankingOptions] -> Encoding
$comitField :: RankingOptions -> Bool
omitField :: RankingOptions -> Bool
ToJSON)

-- | Overrides for the file search tool
data FileSearch = FileSearch
    { FileSearch -> Maybe Natural
max_num_results :: Maybe Natural
    , FileSearch -> Maybe RankingOptions
ranking_options :: Maybe RankingOptions
    } deriving stock ((forall x. FileSearch -> Rep FileSearch x)
-> (forall x. Rep FileSearch x -> FileSearch) -> Generic FileSearch
forall x. Rep FileSearch x -> FileSearch
forall x. FileSearch -> Rep FileSearch x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FileSearch -> Rep FileSearch x
from :: forall x. FileSearch -> Rep FileSearch x
$cto :: forall x. Rep FileSearch x -> FileSearch
to :: forall x. Rep FileSearch x -> FileSearch
Generic, Int -> FileSearch -> ShowS
[FileSearch] -> ShowS
FileSearch -> String
(Int -> FileSearch -> ShowS)
-> (FileSearch -> String)
-> ([FileSearch] -> ShowS)
-> Show FileSearch
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FileSearch -> ShowS
showsPrec :: Int -> FileSearch -> ShowS
$cshow :: FileSearch -> String
show :: FileSearch -> String
$cshowList :: [FileSearch] -> ShowS
showList :: [FileSearch] -> ShowS
Show)
      deriving anyclass (Maybe FileSearch
Value -> Parser [FileSearch]
Value -> Parser FileSearch
(Value -> Parser FileSearch)
-> (Value -> Parser [FileSearch])
-> Maybe FileSearch
-> FromJSON FileSearch
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser FileSearch
parseJSON :: Value -> Parser FileSearch
$cparseJSONList :: Value -> Parser [FileSearch]
parseJSONList :: Value -> Parser [FileSearch]
$comittedField :: Maybe FileSearch
omittedField :: Maybe FileSearch
FromJSON, [FileSearch] -> Value
[FileSearch] -> Encoding
FileSearch -> Bool
FileSearch -> Value
FileSearch -> Encoding
(FileSearch -> Value)
-> (FileSearch -> Encoding)
-> ([FileSearch] -> Value)
-> ([FileSearch] -> Encoding)
-> (FileSearch -> Bool)
-> ToJSON FileSearch
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: FileSearch -> Value
toJSON :: FileSearch -> Value
$ctoEncoding :: FileSearch -> Encoding
toEncoding :: FileSearch -> Encoding
$ctoJSONList :: [FileSearch] -> Value
toJSONList :: [FileSearch] -> Value
$ctoEncodingList :: [FileSearch] -> Encoding
toEncodingList :: [FileSearch] -> Encoding
$comitField :: FileSearch -> Bool
omitField :: FileSearch -> Bool
ToJSON)

-- | The Function tool
data Function = Function
    { Function -> Maybe Text
description :: Maybe Text
    , Function -> Text
name :: Text
    , Function -> Maybe Value
parameters :: Maybe Value
    , Function -> Maybe Bool
strict :: Maybe Bool
    } deriving stock ((forall x. Function -> Rep Function x)
-> (forall x. Rep Function x -> Function) -> Generic Function
forall x. Rep Function x -> Function
forall x. Function -> Rep Function x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Function -> Rep Function x
from :: forall x. Function -> Rep Function x
$cto :: forall x. Rep Function x -> Function
to :: forall x. Rep Function x -> Function
Generic, Int -> Function -> ShowS
[Function] -> ShowS
Function -> String
(Int -> Function -> ShowS)
-> (Function -> String) -> ([Function] -> ShowS) -> Show Function
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Function -> ShowS
showsPrec :: Int -> Function -> ShowS
$cshow :: Function -> String
show :: Function -> String
$cshowList :: [Function] -> ShowS
showList :: [Function] -> ShowS
Show)
      deriving anyclass (Maybe Function
Value -> Parser [Function]
Value -> Parser Function
(Value -> Parser Function)
-> (Value -> Parser [Function])
-> Maybe Function
-> FromJSON Function
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser Function
parseJSON :: Value -> Parser Function
$cparseJSONList :: Value -> Parser [Function]
parseJSONList :: Value -> Parser [Function]
$comittedField :: Maybe Function
omittedField :: Maybe Function
FromJSON, [Function] -> Value
[Function] -> Encoding
Function -> Bool
Function -> Value
Function -> Encoding
(Function -> Value)
-> (Function -> Encoding)
-> ([Function] -> Value)
-> ([Function] -> Encoding)
-> (Function -> Bool)
-> ToJSON Function
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Function -> Value
toJSON :: Function -> Value
$ctoEncoding :: Function -> Encoding
toEncoding :: Function -> Encoding
$ctoJSONList :: [Function] -> Value
toJSONList :: [Function] -> Value
$ctoEncodingList :: [Function] -> Encoding
toEncodingList :: [Function] -> Encoding
$comitField :: Function -> Bool
omitField :: Function -> Bool
ToJSON)

-- | A tool enabled on the assistant
data Tool
    = Tool_Code_Interpreter{ Tool -> Maybe CodeInterpreterContainer
container :: Maybe CodeInterpreterContainer }
    | Tool_File_Search{ Tool -> FileSearch
file_search :: FileSearch }
    | Tool_Function{ Tool -> Function
function :: Function }
    | Tool_Web_Search
    deriving stock ((forall x. Tool -> Rep Tool x)
-> (forall x. Rep Tool x -> Tool) -> Generic Tool
forall x. Rep Tool x -> Tool
forall x. Tool -> Rep Tool x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Tool -> Rep Tool x
from :: forall x. Tool -> Rep Tool x
$cto :: forall x. Rep Tool x -> Tool
to :: forall x. Rep Tool x -> Tool
Generic, Int -> Tool -> ShowS
[Tool] -> ShowS
Tool -> String
(Int -> Tool -> ShowS)
-> (Tool -> String) -> ([Tool] -> ShowS) -> Show Tool
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Tool -> ShowS
showsPrec :: Int -> Tool -> ShowS
$cshow :: Tool -> String
show :: Tool -> String
$cshowList :: [Tool] -> ShowS
showList :: [Tool] -> ShowS
Show)

toolOptions :: Options
toolOptions :: Options
toolOptions = Options
aesonOptions
    { sumEncoding =
        TaggedObject{ tagFieldName = "type", contentsFieldName = "" }

    , tagSingleConstructors = True

    , constructorTagModifier = stripPrefix "Tool_"
    }

instance FromJSON Tool where
    parseJSON :: Value -> Parser Tool
parseJSON = Options -> Value -> Parser Tool
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
toolOptions

instance ToJSON Tool where
    toJSON :: Tool -> Value
toJSON = Options -> Tool -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
toolOptions

toolToResponsesValue :: Tool -> Value
toolToResponsesValue :: Tool -> Value
toolToResponsesValue = Value -> Value
flattenToolValue (Value -> Value) -> (Tool -> Value) -> Tool -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tool -> Value
forall a. ToJSON a => a -> Value
toJSON

parseResponsesToolValue :: Value -> Parser Tool
parseResponsesToolValue :: Value -> Parser Tool
parseResponsesToolValue = Value -> Parser Tool
forall a. FromJSON a => Value -> Parser a
parseJSON (Value -> Parser Tool) -> (Value -> Value) -> Value -> Parser Tool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Value
unflattenToolValue

toolChoiceToResponsesValue :: ToolChoice -> Value
toolChoiceToResponsesValue :: ToolChoice -> Value
toolChoiceToResponsesValue ToolChoice
ToolChoiceNone = Text -> Value
String Text
toolChoiceNoneText
toolChoiceToResponsesValue ToolChoice
ToolChoiceAuto = Text -> Value
String Text
toolChoiceAutoText
toolChoiceToResponsesValue ToolChoice
ToolChoiceRequired = Text -> Value
String Text
toolChoiceRequiredText
toolChoiceToResponsesValue (ToolChoiceTool Tool
tool) = Tool -> Value
toolToResponsesValue Tool
tool

parseResponsesToolChoiceValue :: Value -> Parser ToolChoice
parseResponsesToolChoiceValue :: Value -> Parser ToolChoice
parseResponsesToolChoiceValue (String Text
s)
    | Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
toolChoiceNoneText = ToolChoice -> Parser ToolChoice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ToolChoice
ToolChoiceNone
    | Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
toolChoiceAutoText = ToolChoice -> Parser ToolChoice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ToolChoice
ToolChoiceAuto
    | Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
toolChoiceRequiredText = ToolChoice -> Parser ToolChoice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ToolChoice
ToolChoiceRequired
parseResponsesToolChoiceValue Value
other = Tool -> ToolChoice
ToolChoiceTool (Tool -> ToolChoice) -> Parser Tool -> Parser ToolChoice
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Tool
parseResponsesToolValue Value
other

keyFunction, keyType :: Key.Key
keyFunction :: Key
keyFunction = Text -> Key
Key.fromText Text
"function"
keyType :: Key
keyType = Text -> Key
Key.fromText Text
"type"

functionFieldKeys :: HashSet Key.Key
functionFieldKeys :: HashSet Key
functionFieldKeys = [Key] -> HashSet Key
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList ([Key] -> HashSet Key) -> [Key] -> HashSet Key
forall a b. (a -> b) -> a -> b
$ Text -> Key
Key.fromText (Text -> Key) -> [Text] -> [Key]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text
Item [Text]
"description", Text
Item [Text]
"name", Text
Item [Text]
"parameters", Text
Item [Text]
"strict"]

isFunctionField :: Key.Key -> Bool
isFunctionField :: Key -> Bool
isFunctionField = (Key -> HashSet Key -> Bool
forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
`HashSet.member` HashSet Key
functionFieldKeys)

partitionFunctionFields
    :: KeyMap.KeyMap Value
    -> (KeyMap.KeyMap Value, KeyMap.KeyMap Value)
partitionFunctionFields :: KeyMap Value -> (KeyMap Value, KeyMap Value)
partitionFunctionFields KeyMap Value
obj =
    let ([Pair]
fnPairs, [Pair]
restPairs) = (Pair -> Bool) -> [Pair] -> ([Pair], [Pair])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (Key -> Bool
isFunctionField (Key -> Bool) -> (Pair -> Key) -> Pair -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pair -> Key
forall a b. (a, b) -> a
fst) (KeyMap Value -> [Pair]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList KeyMap Value
obj)
    in ([Pair] -> KeyMap Value
forall v. [(Key, v)] -> KeyMap v
KeyMap.fromList [Pair]
fnPairs, [Pair] -> KeyMap Value
forall v. [(Key, v)] -> KeyMap v
KeyMap.fromList [Pair]
restPairs)

flattenToolValue :: Value -> Value
flattenToolValue :: Value -> Value
flattenToolValue value :: Value
value@(Aeson.Object KeyMap Value
o) =
    Value -> (Value -> Value) -> Maybe Value -> Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Value
value Value -> Value
flattenFunction (Key -> KeyMap Value -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
keyFunction KeyMap Value
o)
  where
    flattenFunction :: Value -> Value
flattenFunction (Aeson.Object KeyMap Value
fnFields) =
        KeyMap Value -> Value
Aeson.Object (Key -> KeyMap Value -> KeyMap Value
forall v. Key -> KeyMap v -> KeyMap v
KeyMap.delete Key
keyFunction KeyMap Value
o KeyMap Value -> KeyMap Value -> KeyMap Value
forall a. Semigroup a => a -> a -> a
<> KeyMap Value
fnFields)
    flattenFunction Value
_ = Value
value
flattenToolValue Value
value = Value
value

unflattenToolValue :: Value -> Value
unflattenToolValue :: Value -> Value
unflattenToolValue value :: Value
value@(Aeson.Object KeyMap Value
o)
    | Key -> KeyMap Value -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
keyType KeyMap Value
o Maybe Value -> Maybe Value -> Bool
forall a. Eq a => a -> a -> Bool
== Value -> Maybe Value
forall a. a -> Maybe a
Just (Text -> Value
String Text
"function")
    , Bool -> Bool
not (Key -> KeyMap Value -> Bool
forall a. Key -> KeyMap a -> Bool
KeyMap.member Key
keyFunction KeyMap Value
o) =
        let (KeyMap Value
fnFields, KeyMap Value
rest) = KeyMap Value -> (KeyMap Value, KeyMap Value)
partitionFunctionFields KeyMap Value
o
        in KeyMap Value -> Value
Aeson.Object (Key -> Value -> KeyMap Value -> KeyMap Value
forall v. Key -> v -> KeyMap v -> KeyMap v
KeyMap.insert Key
keyFunction (KeyMap Value -> Value
Aeson.Object KeyMap Value
fnFields) KeyMap Value
rest)
    | Bool
otherwise = Value
value
unflattenToolValue Value
value = Value
value

-- | Controls which (if any) tool is called by the model
data ToolChoice
    = ToolChoiceNone
    | ToolChoiceAuto
    | ToolChoiceRequired
    | ToolChoiceTool Tool
    deriving stock ((forall x. ToolChoice -> Rep ToolChoice x)
-> (forall x. Rep ToolChoice x -> ToolChoice) -> Generic ToolChoice
forall x. Rep ToolChoice x -> ToolChoice
forall x. ToolChoice -> Rep ToolChoice x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ToolChoice -> Rep ToolChoice x
from :: forall x. ToolChoice -> Rep ToolChoice x
$cto :: forall x. Rep ToolChoice x -> ToolChoice
to :: forall x. Rep ToolChoice x -> ToolChoice
Generic, Int -> ToolChoice -> ShowS
[ToolChoice] -> ShowS
ToolChoice -> String
(Int -> ToolChoice -> ShowS)
-> (ToolChoice -> String)
-> ([ToolChoice] -> ShowS)
-> Show ToolChoice
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ToolChoice -> ShowS
showsPrec :: Int -> ToolChoice -> ShowS
$cshow :: ToolChoice -> String
show :: ToolChoice -> String
$cshowList :: [ToolChoice] -> ShowS
showList :: [ToolChoice] -> ShowS
Show)

instance FromJSON ToolChoice where
    parseJSON :: Value -> Parser ToolChoice
parseJSON Value
"none" = ToolChoice -> Parser ToolChoice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ToolChoice
ToolChoiceNone
    parseJSON Value
"auto" = ToolChoice -> Parser ToolChoice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ToolChoice
ToolChoiceAuto
    parseJSON Value
"required" = ToolChoice -> Parser ToolChoice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ToolChoice
ToolChoiceRequired
    parseJSON Value
other = (Tool -> ToolChoice) -> Parser Tool -> Parser ToolChoice
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Tool -> ToolChoice
ToolChoiceTool (Value -> Parser Tool
forall a. FromJSON a => Value -> Parser a
parseJSON Value
other)

instance ToJSON ToolChoice where
    toJSON :: ToolChoice -> Value
toJSON ToolChoice
ToolChoiceNone = Value
"none"
    toJSON ToolChoice
ToolChoiceAuto = Value
"auto"
    toJSON ToolChoice
ToolChoiceRequired = Value
"required"
    toJSON (ToolChoiceTool Tool
tool) = Tool -> Value
forall a. ToJSON a => a -> Value
toJSON Tool
tool

-- | Code Interpreter container reference
data CodeInterpreterContainer
    = CodeInterpreterContainer_Auto{ CodeInterpreterContainer -> Maybe (Vector Text)
file_ids :: Maybe (Vector Text) }
    | CodeInterpreterContainer_ID{ CodeInterpreterContainer -> Text
container_id :: Text }
    deriving stock ((forall x.
 CodeInterpreterContainer -> Rep CodeInterpreterContainer x)
-> (forall x.
    Rep CodeInterpreterContainer x -> CodeInterpreterContainer)
-> Generic CodeInterpreterContainer
forall x.
Rep CodeInterpreterContainer x -> CodeInterpreterContainer
forall x.
CodeInterpreterContainer -> Rep CodeInterpreterContainer x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x.
CodeInterpreterContainer -> Rep CodeInterpreterContainer x
from :: forall x.
CodeInterpreterContainer -> Rep CodeInterpreterContainer x
$cto :: forall x.
Rep CodeInterpreterContainer x -> CodeInterpreterContainer
to :: forall x.
Rep CodeInterpreterContainer x -> CodeInterpreterContainer
Generic, Int -> CodeInterpreterContainer -> ShowS
[CodeInterpreterContainer] -> ShowS
CodeInterpreterContainer -> String
(Int -> CodeInterpreterContainer -> ShowS)
-> (CodeInterpreterContainer -> String)
-> ([CodeInterpreterContainer] -> ShowS)
-> Show CodeInterpreterContainer
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CodeInterpreterContainer -> ShowS
showsPrec :: Int -> CodeInterpreterContainer -> ShowS
$cshow :: CodeInterpreterContainer -> String
show :: CodeInterpreterContainer -> String
$cshowList :: [CodeInterpreterContainer] -> ShowS
showList :: [CodeInterpreterContainer] -> ShowS
Show)

instance ToJSON CodeInterpreterContainer where
    toJSON :: CodeInterpreterContainer -> Value
toJSON (CodeInterpreterContainer_ID Text
container_id) = Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
container_id
    toJSON (CodeInterpreterContainer_Auto Maybe (Vector Text)
file_ids) =
        [Pair] -> Value
Aeson.object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ Key
"type" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"auto" Pair -> [Pair] -> [Pair]
forall a. a -> [a] -> [a]
:
                      [Pair] -> (Vector Text -> [Pair]) -> Maybe (Vector Text) -> [Pair]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Vector Text
ids -> [Key
"file_ids" Key -> Vector Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Vector Text
ids]) Maybe (Vector Text)
file_ids

instance FromJSON CodeInterpreterContainer where
    parseJSON :: Value -> Parser CodeInterpreterContainer
parseJSON (String Text
s) = CodeInterpreterContainer -> Parser CodeInterpreterContainer
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CodeInterpreterContainer -> Parser CodeInterpreterContainer)
-> CodeInterpreterContainer -> Parser CodeInterpreterContainer
forall a b. (a -> b) -> a -> b
$ Text -> CodeInterpreterContainer
CodeInterpreterContainer_ID Text
s
    parseJSON (Object KeyMap Value
o) = do
        Text
t <- KeyMap Value
o KeyMap Value -> Key -> Parser Text
forall a. FromJSON a => KeyMap Value -> Key -> Parser a
.: Key
"type"
        case (Text
t :: Text) of
            Text
"auto" -> Maybe (Vector Text) -> CodeInterpreterContainer
CodeInterpreterContainer_Auto (Maybe (Vector Text) -> CodeInterpreterContainer)
-> Parser (Maybe (Vector Text)) -> Parser CodeInterpreterContainer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> KeyMap Value
o KeyMap Value -> Key -> Parser (Maybe (Vector Text))
forall a. FromJSON a => KeyMap Value -> Key -> Parser (Maybe a)
.:? Key
"file_ids"
            Text
_ -> String -> Parser CodeInterpreterContainer
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown code interpreter container object"
    parseJSON Value
_ = String -> Parser CodeInterpreterContainer
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid code interpreter container value"

-- | Convenience: Code Interpreter without an explicit container (for Assistants API)
codeInterpreter :: Tool
codeInterpreter :: Tool
codeInterpreter =
    Tool_Code_Interpreter { container :: Maybe CodeInterpreterContainer
container = Maybe CodeInterpreterContainer
forall a. Maybe a
Nothing }

-- | Convenience: Code Interpreter with automatic new container and no files
codeInterpreterAuto :: Tool
codeInterpreterAuto :: Tool
codeInterpreterAuto =
    Tool_Code_Interpreter
        { container :: Maybe CodeInterpreterContainer
container = CodeInterpreterContainer -> Maybe CodeInterpreterContainer
forall a. a -> Maybe a
Just CodeInterpreterContainer_Auto{ file_ids :: Maybe (Vector Text)
file_ids = Maybe (Vector Text)
forall a. Maybe a
Nothing }
        }

-- | Convenience: Code Interpreter with automatic new container seeded with files
codeInterpreterWithFiles :: [Text] -> Tool
codeInterpreterWithFiles :: [Text] -> Tool
codeInterpreterWithFiles [Text]
xs =
    let mFiles :: Maybe (Vector Text)
mFiles = case [Text]
xs of
            [] -> Maybe (Vector Text)
forall a. Maybe a
Nothing
            [Text]
_  -> Vector Text -> Maybe (Vector Text)
forall a. a -> Maybe a
Just ([Text] -> Vector Text
forall a. [a] -> Vector a
V.fromList [Text]
xs)
    in Tool_Code_Interpreter { container :: Maybe CodeInterpreterContainer
container = CodeInterpreterContainer -> Maybe CodeInterpreterContainer
forall a. a -> Maybe a
Just CodeInterpreterContainer_Auto{ file_ids :: Maybe (Vector Text)
file_ids = Maybe (Vector Text)
mFiles } }