packages feed

baikai-mcp-0.1.0.0: src/Baikai/Mcp/Content.hs

-- | Content blocks shared by tool results and prompt messages.
module Baikai.Mcp.Content
  ( ContentBlock (..),
    decodeContentBlock,
  )
where

import Data.Aeson (Value (..))
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Aeson.Types (Object)
import Data.Text (Text)
import GHC.Generics (Generic)

-- | One content block, decoded where this package models it and otherwise
-- preserved.
data ContentBlock
  = TextBlock !Text
  | -- | Base64 data, as sent, and its MIME type.
    ImageBlock !Text !Text
  | AudioBlock !Text !Text
  | -- | A link to a resource; never fetched here.
    ResourceLinkBlock !Object
  | EmbeddedResourceBlock !Object
  | -- | A block type this package does not model.
    OtherBlock !Value
  deriving stock (Eq, Show, Generic)

decodeContentBlock :: Value -> ContentBlock
decodeContentBlock v = case v of
  Object o -> case (KeyMap.lookup "type" o, KeyMap.lookup "text" o, KeyMap.lookup "data" o, KeyMap.lookup "mimeType" o) of
    (Just (String "text"), Just (String t), _, _) -> TextBlock t
    (Just (String "image"), _, Just (String d), Just (String m)) -> ImageBlock d m
    (Just (String "audio"), _, Just (String d), Just (String m)) -> AudioBlock d m
    (Just (String "resource_link"), _, _, _) -> ResourceLinkBlock o
    (Just (String "resource"), _, _, _) -> EmbeddedResourceBlock o
    _ -> OtherBlock v
  _ -> OtherBlock v