package domain import ( "context" "encoding/json" "time" ) type Chat struct { ID string `db:"id" json:"id"` TeamID string `db:"team_id" json:"teamId"` StartDatetime *time.Time `db:"start_datetime" json:"startDatetime"` EndDatetime *time.Time `db:"end_datetime" json:"endDatetime"` AgentID string `db:"agent_id" json:"agentId"` UserID string `db:"user_id" json:"userId"` TaskID string `db:"task_id" json:"taskId,omitempty"` Title string `db:"title" json:"title"` DateCreated time.Time `db:"_date_created" json:"_date_created"` DateUpdated *time.Time `db:"_date_updated" json:"_date_updated"` VersionId string `db:"_version" json:"_version"` } type Message struct { ID string `db:"id" json:"id"` TeamID string `db:"team_id" json:"teamId"` ChatID string `db:"chat_id" json:"chatId"` Role string `db:"role" json:"role"` Content string `db:"content" json:"content"` ToolCallID string `db:"tool_call_id" json:"toolCallId"` ToolCalls []ToolCall `db:"-" json:"toolcalls"` ToolCallsJson string `db:"tool_calls" json:"-"` DateCreated time.Time `db:"_date_created" json:"_date_created"` DateUpdated *time.Time `db:"_date_updated" json:"_date_updated"` VersionId string `db:"_version" json:"_version"` } func (msg *Message) ToolCall2Json() error { if msg.ToolCalls != nil { toolCallJson, err := json.Marshal(&msg.ToolCalls) if err != nil { return err } msg.ToolCallsJson = string(toolCallJson) } return nil } func (msg *Message) Json2toolCall() error { if msg.ToolCallsJson != "" { err := json.Unmarshal([]byte(msg.ToolCallsJson), &msg.ToolCalls) if err != nil { return err } } return nil } type ChatWithMessages struct { Chat *Chat `json:"chat"` Messages []*Message `json:"messages"` } type ChatParams struct { ProviderID string ModelID string OnChunk StreamCallback } type ToolParam struct { JsonName string `json:"name"` Type string `json:"type"` Description string `json:"description"` Mandatory bool `json:"mandatory"` } type ITool interface { Name() string Description() string Params() []ToolParam Definition(ctx context.Context) *ToolDefinition Execute(ctx context.Context, input []byte, configuration string) ([]byte, error) } type ToolDefinition struct { Type string Function *ToolFunction } type ToolFunction struct { Name string Description string Parameters map[string]any } type ToolCall struct { ID string Type string Function ToolCallFunction Extra map[string]map[string]any } type ToolCallFunction struct { Name string Arguments string } type Chunk struct { Done bool `json:"done"` ToolName string `json:"toolName,omitempty"` Role string `json:"role,omitempty"` Content string `json:"content,omitempty"` Reasoning bool `json:"reasoning,omitempty"` } type StreamCallback func(chunk *Chunk) type Role string const ( RoleSystem Role = "system" RoleUser Role = "user" RoleAssistant Role = "assistant" RoleTool Role = "tool" ) type LLMParams struct { Tools []*ToolDefinition OnChunk StreamCallback ReasoningEffort string } type LLMEngine interface { ListProviderTypes() []Item ListModelsFromProvider(ctx context.Context, provider *Provider) ([]string, error) Stream( ctx context.Context, provider *Provider, modelID string, params *LLMParams, messages []*Message, ) *Message } type ChatRepository interface { ListChats(userID string, teamID string, page int) ([]*Chat, error) GetChat(userID string, teamID string, id string) (*Chat, error) CreateChat(userID string, chat *Chat) (*Chat, error) UpdateChat(userID string, chat *Chat) (*Chat, error) DeleteChat(userID string, teamID string, id string) error GetChatMessages(userID string, teamID string, id string, withToolCallResponses bool) ([]*Message, error) CreateChatMessage(userID string, message *Message) (*Message, error) }