module System.Agents.Runtime.Trace where

import Data.Text (Text)

import System.Agents.Base
import qualified System.Agents.LLMs.OpenAI as OpenAI
import qualified System.Agents.ToolPortal as ToolPortal
import qualified System.Agents.Tools.BashToolbox as BashToolbox
import qualified System.Agents.Tools.DeveloperToolbox as DeveloperToolbox
import qualified System.Agents.Tools.LuaToolbox as LuaToolbox
import qualified System.Agents.Tools.Skills.Toolbox as SkillsToolbox
import qualified System.Agents.Tools.SqliteToolbox as SqliteToolbox
import qualified System.Agents.Tools.SystemToolbox as SystemToolbox
import qualified System.Agents.Tools.Trace as Tools

-------------------------------------------------------------------------------
data Trace
    = AgentTrace_Loading !AgentSlug !AgentId !BashToolbox.Trace
    | AgentTrace_Conversation !AgentSlug !AgentId !ConversationId !ConversationTrace
    | BuiltinToolboxTrace !Text !SqliteToolbox.Trace
    | BuiltinToolboxInitError !Text !String
    | LuaToolboxInitError !Text !String
    | SystemToolboxTrace !Text !SystemToolbox.Trace
    | DeveloperToolboxTrace !Text !DeveloperToolbox.Trace
    | LuaToolboxTrace !Text !LuaToolbox.Trace
    | SkillsToolboxTrace !Text !SkillsToolbox.Trace
    | SkillsToolboxInitError !Text !String
    | ToolTrace !Text !Tools.ToolTrace
    | ToolPortalTrace !Text !ToolPortal.Trace
    deriving (Show)

traceAgentSlug :: Trace -> AgentSlug
traceAgentSlug (AgentTrace_Loading aSlug _ _) = aSlug
traceAgentSlug (AgentTrace_Conversation aSlug _ _ _) = aSlug
traceAgentSlug (BuiltinToolboxTrace _ _) = "builtin"
traceAgentSlug (BuiltinToolboxInitError tName _) = tName
traceAgentSlug (LuaToolboxInitError tName _) = tName
traceAgentSlug (SystemToolboxTrace tName _) = tName
traceAgentSlug (DeveloperToolboxTrace tName _) = tName
traceAgentSlug (LuaToolboxTrace tName _) = tName
traceAgentSlug (SkillsToolboxTrace tName _) = tName
traceAgentSlug (SkillsToolboxInitError tName _) = tName
traceAgentSlug (ToolTrace tName _) = tName
traceAgentSlug (ToolPortalTrace tName _) = tName

traceAgentId :: Trace -> AgentId
traceAgentId (AgentTrace_Loading _ aId _) = aId
traceAgentId (AgentTrace_Conversation _ aId _ _) = aId
traceAgentId (BuiltinToolboxTrace _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")
traceAgentId (BuiltinToolboxInitError _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")
traceAgentId (LuaToolboxInitError _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")
traceAgentId (SystemToolboxTrace _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")
traceAgentId (DeveloperToolboxTrace _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")
traceAgentId (LuaToolboxTrace _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")
traceAgentId (SkillsToolboxTrace _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")
traceAgentId (SkillsToolboxInitError _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")
traceAgentId (ToolTrace _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")
traceAgentId (ToolPortalTrace _ _) = AgentId (read "00000000-0000-0000-0000-000000000000")

traceConversationId :: Trace -> Maybe ConversationId
traceConversationId (AgentTrace_Loading _ _ _) = Nothing
traceConversationId (AgentTrace_Conversation _ _ cId _) = Just cId
traceConversationId (BuiltinToolboxTrace _ _) = Nothing
traceConversationId (BuiltinToolboxInitError _ _) = Nothing
traceConversationId (LuaToolboxInitError _ _) = Nothing
traceConversationId (SystemToolboxTrace _ _) = Nothing
traceConversationId (DeveloperToolboxTrace _ _) = Nothing
traceConversationId (LuaToolboxTrace _ _) = Nothing
traceConversationId (SkillsToolboxTrace _ _) = Nothing
traceConversationId (SkillsToolboxInitError _ _) = Nothing
traceConversationId (ToolTrace _ _) = Nothing
traceConversationId (ToolPortalTrace _ _) = Nothing

data ConversationTrace
    = NewConversation
    | WaitingForPrompt
    | LLMTrace !StepId !OpenAI.Trace
    | RunToolTrace !StepId !Trace
    | ChildrenTrace !Trace
    deriving (Show)
