Files
lassistanoque/backend/internal/domain/llm.go
T
fabien 51bbb45b32 compaction de contexte, filtre/pagination/suppression des conversations
- compaction automatique du contexte LLM quand la taille atteint le seuil
  (contexte max - taille de réponse max), par résumé de l'historique ancien
- filtre textuel (?q=) et pagination (20/page) de l'historique des conversations
- correctif ORM : offset de pagination en base 0
- suppression d'une conversation : messages associés supprimés, session en
  cours annulée, popup de confirmation (ConfirmDialog)
- titres de conversation nettoyés du markdown
- styles des listes markdown, variante danger du bouton
- go.mod/go.sum : dépendances go-rod et stealth manquantes du commit précédent
2026-10-05 23:46:12 +02:00

163 lines
4.4 KiB
Go

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"`
FreshTitle bool `db:"fresh_title" json:"freshTitle"`
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
ContextMaxTokens int // taille de contexte du modèle ; 0 = défaut
MaxResponseTokens int // réserve de réponse ; 0 = défaut
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
Generate(
ctx context.Context,
provider *Provider,
modelID string,
messages []*Message,
) (string, error)
}
type ChatRepository interface {
ListChats(userID string, teamID string, page int, query string) ([]*Chat, error)
ListChatsWithUnfreshTitles(teamID string) ([]*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)
}