models

package
v0.7.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package models holds the plain Go structs that gorm/gen generates type-safe repositories from. One file per model; shared fields live in Base. The SQL migrations (internal/pkg/db/migrations, run by the DB connection layer) are the source of truth for the schema — these struct tags are only gorm decoration, not a second schema definition. Never hand-edit the generated repositories; change a model or migration and re-run `make generate`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base struct {
	ID        uuid.UUID
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt
}

Base is embedded by every model: a UUID id (primary key by gorm convention) plus timestamps and the soft-delete column.

func (*Base) BeforeCreate

func (b *Base) BeforeCreate(*gorm.DB) error

BeforeCreate mints a UUID when the id is unset, so inserts don't send the zero UUID (which collides on the primary key). Tag-free — the DB column keeps its gen_random_uuid() default as a fallback for non-gorm writers.

type Chat

type Chat struct {
	Base

	UserID  uuid.UUID
	Title   string
	ModelID string

	Temperature      *float64
	TopP             *float64
	ReasoningEffort  string
	MaxOutputTokens  *int
	MaxHistoryTokens *int

	// DisabledMCPServerIDs is the per-chat set of MCP server IDs whose tools
	// are withheld from this chat's turns. Empty (the default) means every
	// globally-enabled server is available. Explicit column tag: gorm's
	// default namer would mangle the MCP/IDs acronyms. See core/chats filter.
	//nolint:lll // struct tag + generic type won't fit 80 cols; not splittable
	DisabledMCPServerIDs datatypes.JSONSlice[uuid.UUID] `gorm:"column:disabled_mcp_server_ids"`
}

Chat is one conversation, owned by a user. ModelID names the model profile (from chatz.yaml) the chat runs against. The generation settings are per-chat overrides: a nil pointer / empty ReasoningEffort means "unset" — the provider default applies (temperature/top_p/reasoning) or no cap is imposed (max_output/max_history tokens).

func (Chat) TableName

func (Chat) TableName() string

TableName pins the table name.

type LLMUsage

type LLMUsage struct {
	Base

	Service            string
	Stage              string
	Model              string
	ReasoningEffort    string
	PromptTokens       int64
	CachedPromptTokens int64
	CompletionTokens   int64
	ReasoningTokens    int64
	TotalTokens        int64
	DurationMs         int64
	UserID             *uuid.UUID
	ChatID             *uuid.UUID
	MessageID          *uuid.UUID
	RequestID          string
	ErrorMessage       string
}

LLMUsage is one row per upstream LLM call, written best-effort by the usage decorator. Reasoning + cached tokens are broken out from totals so cost dashboards can attribute spend to the owning chat / message.

func (LLMUsage) TableName

func (LLMUsage) TableName() string

TableName pins the table name.

type MCPServer

type MCPServer struct {
	Base

	Name       string
	Transport  MCPTransport
	Command    string
	Args       datatypes.JSON
	URL        string
	HeadersEnc []byte
	EnvEnc     []byte
	Enabled    bool
	CreatedBy  *uuid.UUID
}

MCPServer is an admin-added MCP server (config-file servers are held in memory, not here). Transport is stdio | http. For stdio, Command + Args spawn the process (EnvEnc holds any env-var secrets, AEAD-encrypted); for http, URL + HeadersEnc reach the endpoint. Both *Enc columns are sealed at rest — secrets never land in plaintext.

func (MCPServer) TableName

func (MCPServer) TableName() string

TableName pins the table name.

type MCPSource

type MCPSource = string

MCPSource records whether a server came from the config file (read-only in the UI) or was added by an admin through the UI (fully editable).

const (
	MCPSourceConfig MCPSource = "config"
	MCPSourceDB     MCPSource = "db"
)

type MCPToolExecution

type MCPToolExecution struct {
	Base

	MessageID  uuid.UUID
	Server     string
	Tool       string
	Params     datatypes.JSON
	Result     string
	IsError    bool
	DurationMs int64
}

MCPToolExecution records one MCP tool call made while answering a message: which server + tool, the params, the result, and whether it errored. Keeps the tool timeline for a reloaded chat and feeds tool-latency metrics.

func (MCPToolExecution) TableName

func (MCPToolExecution) TableName() string

TableName pins the table name.

type MCPTransport

type MCPTransport = string

MCPTransport is how an MCP server is reached.

const (
	MCPTransportStdio MCPTransport = "stdio"
	MCPTransportHTTP  MCPTransport = "http"
)

type Message

type Message struct {
	Base

	Position          int64     `gorm:"default:(-)"`
	TurnID            uuid.UUID `gorm:"default:(-)"`
	TurnComplete      *bool     `gorm:"default:(-)"`
	ModelID           string
	ChatID            uuid.UUID
	Role              MessageRole
	Content           string
	Reasoning         string
	ProviderReasoning datatypes.JSON
	ToolCalls         datatypes.JSON
	ToolCallID        string
	IsError           bool

	// IsInjection marks a row a tool injected into the model's context rather
	// than something the user or the model said. Injections are per-run
	// scaffolding, so only one is ever stored: the trailing one a run ended on,
	// kept so an interrupted turn is not silently missing the instruction that
	// was pending when it stopped. The flag is what keeps it OUT of a rebuilt
	// history — a row that exists as a record, not as conversation.
	IsInjection bool

	UISpec       datatypes.JSON
	InputTokens  int64
	OutputTokens int64
}

Message is one turn item in a chat. Role is user | assistant | tool, plus system for a stored injection (see IsInjection). Assistant rows may carry a Reasoning trace, streamed ToolCalls, and a rendered UISpec; tool rows carry the result keyed by ToolCallID plus whether it was an error (IsError). The set reconstructs the timeline.

func (Message) TableName

func (Message) TableName() string

TableName pins the table name.

type MessageRole

type MessageRole = string

MessageRole is the author of a chat message. Finite domain enum; alias form (no methods) so it marshals to/from a plain string at the DB + JSON boundary.

const (
	MessageRoleUser      MessageRole = "user"
	MessageRoleAssistant MessageRole = "assistant"
	MessageRoleTool      MessageRole = "tool"

	// MessageRoleSystem is only ever stored for a tool's message injection
	// that a run ended on. Ordinary system prompts are built per request and
	// never persisted, so a stored system row always has IsInjection set.
	MessageRoleSystem MessageRole = "system"
)

type Session

type Session struct {
	Base

	UserID    uuid.UUID
	TokenHash string
	ExpiresAt time.Time
}

Session is a server-side login session. Only the hash of the opaque token is stored; the raw token lives in the client's HttpOnly cookie.

func (Session) TableName

func (Session) TableName() string

TableName pins the table name.

type User

type User struct {
	Base

	Username     string
	PasswordHash *string
	IsAdmin      bool
}

User is an account. The first user created becomes the admin; further users are admin-provisioned (no self-registration). PasswordHash is null-able so a passwordless single-user install can exist without one.

func (User) TableName

func (User) TableName() string

TableName pins the table name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL