model

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package model defines GORM models for the memory system. All timestamps use time.Time and will be stored as DATETIME in SQLite.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateID

func GenerateID() string

GenerateID generates a ULID (Universally Unique Lexicographically Sortable Identifier). Uses github.com/oklog/ulid for proper implementation.

Types

type DeletedItem

type DeletedItem struct {
	ID               string    `gorm:"column:id;type:text;primaryKey"`
	OriginalDataJSON string    `gorm:"column:original_data_json;type:text;not null"` // Full backup including FTS fields
	DeletedAt        time.Time `gorm:"column:deleted_at;type:datetime;not null;index:idx_del_deleted"`
	PurgeAfter       time.Time `gorm:"column:purge_after;type:datetime;not null;index:idx_del_purge"`
	DeletedBy        *string   `gorm:"column:deleted_by;type:text"`
	Reason           *string   `gorm:"column:reason;type:text"`
}

DeletedItem stores soft-deleted memory items for recovery. Items are physically purged after purge_after date.

func (DeletedItem) TableName

func (DeletedItem) TableName() string

TableName specifies the table name for DeletedItem

type DialogExtraction

type DialogExtraction struct {
	ID         string `gorm:"column:id;type:text;primaryKey"`
	DialogText string `gorm:"column:dialog_text;type:text;not null"`
	// DialogHash is indexed for recent-window cache lookup.
	// It is intentionally NOT globally unique because idempotency is a time-window policy (48h), not a forever constraint.
	DialogHash            string           `gorm:"column:dialog_hash;type:text;not null;index:idx_dialog_hash"`
	ConfigRef             string           `gorm:"column:config_ref;type:text"`
	ExtractedMemoriesJSON string           `gorm:"column:extracted_memories_json;type:text"` // Array of ExtractedMemory
	TotalTokens           *int             `gorm:"column:total_tokens;type:integer"`
	ProcessingTimeMs      *int             `gorm:"column:processing_time_ms;type:integer"`
	Status                ExtractionStatus `gorm:"column:status;type:text;not null;index:idx_ext_status"`
	ErrorMessage          *string          `gorm:"column:error_message;type:text"`
	CreatedAt             time.Time        `gorm:"column:created_at;type:datetime;not null;index:idx_ext_created"`
	CompletedAt           *time.Time       `gorm:"column:completed_at;type:datetime"`
}

DialogExtraction records the extraction process and results

func (DialogExtraction) TableName

func (DialogExtraction) TableName() string

TableName specifies the table name for DialogExtraction

type EventType

type EventType string

EventType defines the type of memory event

const (
	EventTypeCreate           EventType = "create"
	EventTypeUpdate           EventType = "update"
	EventTypeRead             EventType = "read"
	EventTypeExpire           EventType = "expire"
	EventTypeDelete           EventType = "delete"
	EventTypeSummarize        EventType = "summarize"
	EventTypeRestore          EventType = "restore"
	EventTypeConflictDetected EventType = "conflict_detected"
)

type ExtractionPrompt

type ExtractionPrompt struct {
	ID           string
	SystemPrompt string
	JSONSchema   string
}

ExtractionPrompt stores runtime prompt configuration for memory extraction. This config is passed in code and is NOT persisted by this library.

type ExtractionStatus

type ExtractionStatus string

ExtractionStatus defines the status of dialog extraction

const (
	ExtractionStatusPending    ExtractionStatus = "pending"
	ExtractionStatusProcessing ExtractionStatus = "processing"
	ExtractionStatusCompleted  ExtractionStatus = "completed"
	ExtractionStatusFailed     ExtractionStatus = "failed"
)

type FTSMemory

type FTSMemory struct {
	ItemID   string `gorm:"column:item_id"`
	Title    string `gorm:"column:title"`
	Content  string `gorm:"column:content"`
	Summary  string `gorm:"column:summary"`
	TagsText string `gorm:"column:tags_text"`
}

FTSMemory represents the FTS5 virtual table for full-text search. The table and triggers are created by store.Migrate after AutoMigrate.

func (FTSMemory) IsVirtualTable

func (FTSMemory) IsVirtualTable() bool

IsVirtualTable indicates this is an FTS5 virtual table

func (FTSMemory) TableName

func (FTSMemory) TableName() string

TableName specifies the virtual table name

type ItemStatus

type ItemStatus string

ItemStatus defines the status of a memory item

const (
	ItemStatusActive   ItemStatus = "active"
	ItemStatusExpired  ItemStatus = "expired"
	ItemStatusArchived ItemStatus = "archived"
	ItemStatusDeleted  ItemStatus = "deleted"
)

type LLMConfig

type LLMConfig struct {
	Provider       LLMProvider
	APIKey         string // Plaintext - caller handles encryption/decryption if needed
	BaseURL        *string
	Model          string
	MaxTokens      int
	Temperature    float64
	TimeoutSeconds int
}

LLMConfig stores runtime LLM provider configuration. This config is passed in code and is NOT persisted by this library.

type LLMProvider

type LLMProvider string

LLMProvider defines the supported LLM providers

const (
	LLMProviderOpenAI LLMProvider = "openai"
	LLMProviderClaude LLMProvider = "anthropic"
	LLMProviderOllama LLMProvider = "ollama"
	LLMProviderCustom LLMProvider = "custom"
)

type LinkType

type LinkType string

LinkType defines the type of relationship between memory items

const (
	LinkTypeSupports    LinkType = "supports"
	LinkTypeContradicts LinkType = "contradicts"
	LinkTypeDerivedFrom LinkType = "derived_from"
	LinkTypeRelatedTo   LinkType = "related_to"
	LinkTypeSupersedes  LinkType = "supersedes"
)

type MemoryEvent

type MemoryEvent struct {
	ID          string    `gorm:"column:id;type:text;primaryKey"`
	ItemID      *string   `gorm:"column:item_id;type:text;index:idx_event_item"`
	EventType   EventType `gorm:"column:event_type;type:text;not null;index:idx_event_type"`
	Actor       *string   `gorm:"column:actor;type:text;index:idx_event_actor"` // agent_id/user_id/system
	TraceID     *string   `gorm:"column:trace_id;type:text;index:idx_event_trace"`
	RequestID   *string   `gorm:"column:request_id;type:text;index:idx_event_request"`
	PayloadJSON *string   `gorm:"column:payload_json;type:text"`
	CreatedAt   time.Time `gorm:"column:created_at;type:datetime;not null;index:idx_event_created"`
}

MemoryEvent stores audit trail for all memory operations. Supports full traceability with actor, trace_id, and request_id.

func (MemoryEvent) TableName

func (MemoryEvent) TableName() string

TableName specifies the table name for MemoryEvent

type MemoryItem

type MemoryItem struct {
	ID            string        `gorm:"column:id;type:text;primaryKey"`
	Namespace     string        `` /* 127-byte string literal not displayed */
	NamespaceType NamespaceType `gorm:"column:namespace_type;type:text;not null;index:idx_mem_ns_type;index:idx_mem_type_created"`
	Title         string        `gorm:"column:title;type:text"`
	Content       string        `gorm:"column:content;type:text;not null"`
	Summary       string        `gorm:"column:summary;type:text"`
	TagsJSON      string        `gorm:"column:tags_json;type:text"` // JSON array stored as string
	SourceType    SourceType    `gorm:"column:source_type;type:text"`
	SourceRef     string        `gorm:"column:source_ref;type:text"`
	Importance    int           `gorm:"column:importance;type:integer;default:0"`
	Confidence    float64       `gorm:"column:confidence;type:real;default:1.0"`
	Status        ItemStatus    `gorm:"column:status;type:text;default:'active';index:idx_mem_status;index:idx_mem_recall_filter"`
	ExpiresAt     *time.Time    `gorm:"column:expires_at;type:datetime;index:idx_mem_expires;index:idx_mem_recall_filter"`
	CreatedAt     time.Time     `gorm:"column:created_at;type:datetime;not null;index:idx_mem_created;index:idx_mem_type_created"`
	UpdatedAt     time.Time     `gorm:"column:updated_at;type:datetime;not null"`
	LastAccessAt  *time.Time    `gorm:"column:last_access_at;type:datetime;index:idx_mem_last_access"`
	AccessCount   int           `gorm:"column:access_count;type:integer;default:0;index:idx_mem_access"`
	Version       int           `gorm:"column:version;type:integer;default:1"` // Optimistic locking
	DedupeKey     *string       `gorm:"column:dedupe_key;type:text;uniqueIndex:idx_namespace_dedupe"`
	EmbeddingRef  *string       `gorm:"column:embedding_ref;type:text"` // Reserved for future vector support

	// TokenizedText stores jiebago tokenized content for Chinese FTS search.
	// Format: space-separated tokens (e.g., "北京 清华大学 毕业")
	TokenizedText string `gorm:"column:tokenized_text;type:text"`
}

MemoryItem represents a single memory entry in the system. This is the core entity for storing agent memories.

func (MemoryItem) TableName

func (MemoryItem) TableName() string

TableName specifies the table name for MemoryItem

type MemoryLink struct {
	ID         string    `gorm:"column:id;type:text;primaryKey"`
	FromID     string    `gorm:"column:from_id;type:text;not null;index:idx_link_from"`
	ToID       string    `gorm:"column:to_id;type:text;not null;index:idx_link_to"`
	LinkType   LinkType  `gorm:"column:link_type;type:text;not null;index:idx_link_type"`
	Weight     float64   `gorm:"column:weight;type:real;default:1.0"`
	CreatedAt  time.Time `gorm:"column:created_at;type:datetime;not null"`
	ReasonJSON *string   `gorm:"column:reason_json;type:text"` // JSON with link reason/confidence details
}

MemoryLink represents relationships between memory items. Supports various link types including contradiction detection.

func (MemoryLink) TableName

func (MemoryLink) TableName() string

TableName specifies the table name for MemoryLink

type MemoryMerge

type MemoryMerge struct {
	ID             string    `gorm:"column:id;type:text;primaryKey"`
	TargetID       string    `gorm:"column:target_id;type:text;not null;index:idx_merge_target"` // The memory being updated
	SourceContent  string    `gorm:"column:source_content;type:text"`                            // Content being merged in
	SourceTitle    string    `gorm:"column:source_title;type:text"`
	MergedContent  string    `gorm:"column:merged_content;type:text"` // Result after merge
	MergedTitle    string    `gorm:"column:merged_title;type:text"`
	SourceDialogID string    `gorm:"column:source_dialog_id;type:text;index:idx_merge_dialog"` // Original extraction that triggered merge
	MergeType      string    `gorm:"column:merge_type;type:text;default:'auto'"`               // 'auto' (LLM) or 'manual'
	Reason         string    `gorm:"column:reason;type:text"`                                  // Why this merge happened
	CreatedAt      time.Time `gorm:"column:created_at;type:datetime;not null;index:idx_merge_created"`
}

MemoryMerge tracks merge operations for memory consolidation

func (MemoryMerge) TableName

func (MemoryMerge) TableName() string

TableName specifies the table name for MemoryMerge

type NamespacePolicy

type NamespacePolicy struct {
	Namespace                 string    `gorm:"column:namespace;type:text;primaryKey"`
	TTLSeconds                *int      `gorm:"column:ttl_seconds;type:integer"` // nil means never expires
	TTLPolicy                 TTLPolicy `gorm:"column:ttl_policy;type:text;default:'fixed'"`
	SlidingTTLThreshold       int       `gorm:"column:sliding_ttl_threshold;type:integer;default:3"`
	SummaryEnabled            bool      `gorm:"column:summary_enabled;type:boolean;default:1"`
	SummaryItemTokenThreshold int       `gorm:"column:summary_item_token_threshold;type:integer;default:500"`
	RankWeightsJSON           string    `gorm:"column:rank_weights_json;type:text;default:'{\"fts\":0.55,\"recency\":0.20,\"importance\":0.15,\"confidence\":0.10}'"`
	DefaultTopK               int       `gorm:"column:default_top_k;type:integer;default:10"`
	CreatedAt                 time.Time `gorm:"column:created_at;type:datetime;not null"`
	UpdatedAt                 time.Time `gorm:"column:updated_at;type:datetime;not null"`
}

NamespacePolicy stores configuration policies for namespaces. Supports exact match and prefix match (e.g., "task/projA/*").

func (NamespacePolicy) TableName

func (NamespacePolicy) TableName() string

TableName specifies the table name for NamespacePolicy

type NamespaceSummary

type NamespaceSummary struct {
	ID          string     `gorm:"column:id;type:text;primaryKey"`
	Namespace   string     `gorm:"column:namespace;type:text;uniqueIndex;not null"`
	Summary     string     `gorm:"column:summary;type:text;not null"`
	ItemCount   int        `gorm:"column:item_count;type:integer;not null"`
	WindowStart *time.Time `gorm:"column:window_start;type:datetime"`
	WindowEnd   *time.Time `gorm:"column:window_end;type:datetime"`
	UpdatedAt   time.Time  `gorm:"column:updated_at;type:datetime;not null"`
}

NamespaceSummary stores aggregated summaries for namespaces. Used for quick overview of namespace contents without loading all items.

func (NamespaceSummary) TableName

func (NamespaceSummary) TableName() string

TableName specifies the table name for NamespaceSummary

type NamespaceType

type NamespaceType string

NamespaceType defines the type of namespace Simplified to 4 categories for better usability

const (
	// Transient: Short-term conversation context, expires quickly
	NamespaceTypeTransient NamespaceType = "transient"

	// Profile: User preferences, personal info, long-term stable
	NamespaceTypeProfile NamespaceType = "profile"

	// Action: Tasks, todos, actionable items
	NamespaceTypeAction NamespaceType = "action"

	// Knowledge: Facts, skills, procedures, learned information
	NamespaceTypeKnowledge NamespaceType = "knowledge"
)

type SourceType

type SourceType string

SourceType defines the source of a memory item

const (
	SourceTypeUser   SourceType = "user"
	SourceTypeAgent  SourceType = "agent"
	SourceTypeImport SourceType = "import"
	SourceTypeSystem SourceType = "system"
)

type TTLPolicy

type TTLPolicy string

TTLPolicy defines the TTL policy type

const (
	TTLPolicyFixed   TTLPolicy = "fixed"
	TTLPolicySliding TTLPolicy = "sliding"
	TTLPolicyManual  TTLPolicy = "manual"
)

Jump to

Keyboard shortcuts

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