packages feed

A-gent-0.11.0.11: src/Internal/LLM.hs

{-# OPTIONS_GHC -Wall -Werror #-}

{-# LANGUAGE NoGeneralizedNewtypeDeriving #-}
{-# LANGUAGE Safe                         #-}

--------------------------------------------------------------------------------

-- |
-- Copyright  : (c) 2026 SPISE MISU ApS
-- License    : SSPL-1.0 OR AGPL-3.0-only
-- Maintainer : SPISE MISU <mail+hackage@spisemisu.com>
-- Stability  : experimental

--------------------------------------------------------------------------------

module Internal.LLM
  ( Exit (..)
  , Root (..)
  , Filter (..)
  , Mode (..)
  , View (..)
  , Chit (..)
  , Chat
  , History (..)
  , Mask (..)
  , File (..)
  , FileLine
  , FilePaths (..)
  , Template (..)
  , modes
  , tpl2xml
  )
where

--------------------------------------------------------------------------------

newtype Exit = Exit Bool

newtype Root = Root FilePath

newtype Filter = Filter String

data Mode
  = Auto
  | Chat
  | Code
  | Docs
  | Echo
  | Plan
  | Test
  deriving (Bounded, Enum, Eq, Read, Show)

data View
  = Chits
  | Chats

data Chit =
  Chit
    { prev :: ![String]
    , next :: ![String]
    }

type Chat = [String]

data History =
  History
    { view :: ![View]
    , chit :: !Chit
    , chat :: !Chat
    }

--------------------------------------------------------------------------------

newtype Mask = Mask [String]

--------------------------------------------------------------------------------

type Markdown = String

type FileLine = String

newtype File = File (FilePath, [FileLine])

newtype FilePaths = FilePaths { filePaths :: [FilePath] }

data Template =
  Template
    { purpose      :: !String
    , instructions :: ![String]
    , examples     :: ![Markdown]
    , files        :: ![File]
    }

--------------------------------------------------------------------------------

modes :: [Mode]
modes =
 [ minBound .. maxBound ]

tpl2xml
  :: Template
  -> String
tpl2xml tpl =
  ( if eis && ees && efs then
      pur
    else
      "<purpose>" ++ pur ++ "</purpose>"
  ) ++
  ( if eis then
      []
    else
      "<instructions>" ++
      tis ++
      "</instructions>"
  ) ++
  ( if ees then
      []
    else
      "<examples>" ++
      tes ++
      "</examples>"
  ) ++
  ( if efs then
      []
    else
      "<files>" ++
      concatMap aux tfs ++
      "</files>"
  )
  where
    pur =
      purpose tpl ++
      ( if eis then
          []
        else
          ". Follow the specified `instructions`"
      ) ++
      ( if ees then
          []
        else
          ". Output MUST match provided `examples`"
      ) ++
      ( if efs then
          []
        else
          ". List of provided `files` for context"
      )
    eis = null tis
    ees = null tes
    efs = null tfs
    tis =
      concatMap (\ x -> "<instruction>" ++ x ++ "</instruction>")
      $ instructions tpl
    tes =
      concatMap (\ x -> "<example>" ++ x ++ "</example>")
      $ examples tpl
    tfs = zip [0 :: Int ..] $ files tpl
    aux (i, File (src, ls)) =
      "<file index=\"" ++ show i ++ "\">" ++
      "<source>" ++ src ++ "</source>" ++
      "<content>" ++ unlines ls ++ "</content>" ++
      "</file>"