memory

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 21 Imported by: 0

README

Vector Memory System

This package implements a vector-based memory system for goclaw with semantic search capabilities.

Architecture

Components
  1. Types (types.go) - Core data structures and interfaces
  2. Embeddings (embeddings.go) - Embedding provider implementations
  3. Store (store.go) - SQLite-based persistent storage
  4. Search (search.go) - High-level memory management API
  5. Vector (vector.go) - Vector math utilities

Features

  • Vector Embeddings: Support for OpenAI, Gemini, and extensible providers
  • Semantic Search: Cosine similarity search with configurable thresholds
  • Hybrid Search: Combine vector similarity with full-text search (FTS5)
  • Memory Types: Long-term, session, and daily notes
  • Caching: In-memory LRU cache for frequently accessed memories
  • Batch Operations: Efficient bulk embedding and storage

Usage

Basic Setup
import (
    "github.com/smallnest/goclaw/memory"
)

// Create embedding provider
provider, err := memory.NewOpenAIProvider(memory.DefaultOpenAIConfig(apiKey))
if err != nil {
    log.Fatal(err)
}

// Create store
store, err := memory.NewSQLiteStore(memory.DefaultStoreConfig(
    "/path/to/memory.db",
    provider,
))
if err != nil {
    log.Fatal(err)
}
defer store.Close()

// Create manager
manager, err := memory.NewMemoryManager(memory.DefaultManagerConfig(store, provider))
if err != nil {
    log.Fatal(err)
}
defer manager.Close()
Adding Memories
// Add a single memory
ve, err := manager.AddMemory(ctx, "User prefers dark mode",
    memory.MemorySourceLongTerm,
    memory.MemoryTypePreference,
    memory.MemoryMetadata{
        Tags: []string{"preference", "ui"},
        Importance: 0.8,
    })
if err != nil {
    log.Fatal(err)
}

// Add multiple memories
items := []memory.MemoryItem{
    {
        Text: "User works at TechCorp",
        Source: memory.MemorySourceLongTerm,
        Type: memory.MemoryTypeFact,
    },
    {
        Text: "Meeting scheduled for tomorrow",
        Source: memory.MemorySourceDaily,
        Type: memory.MemoryTypeContext,
    },
}
err = manager.AddMemoryBatch(ctx, items)
Searching
// Semantic search
results, err := manager.Search(ctx, "user preferences",
    memory.SearchOptions{
        Limit:    10,
        MinScore: 0.7,
        Hybrid:   true,
    })
if err != nil {
    log.Fatal(err)
}

for _, result := range results {
    fmt.Printf("Score: %.2f, Text: %s\n", result.Score, result.Text)
}

// Search by tag
preferenceMemories, err := manager.SearchByTag(ctx, "preference")

// Search by source
longTerm, err := manager.SearchBySource(ctx, memory.MemorySourceLongTerm)

Configuration

Store Options
config := memory.StoreConfig{
    DBPath:              "/path/to/memory.db",
    Provider:            provider,
    EnableVectorSearch:  true,   // Requires sqlite-vec
    EnableFTS:           true,   // Requires FTS5
    VectorExtensionPath: "",     // Auto-detect if empty
}
Search Options
opts := memory.SearchOptions{
    Limit:        10,              // Max results
    MinScore:     0.7,             // Minimum similarity (0-1)
    Sources:      []MemorySource{MemorySourceLongTerm},
    Types:        []MemoryType{MemoryTypeFact},
    Hybrid:       true,            // Enable hybrid search
    VectorWeight: 0.7,             // Weight for vector similarity
    TextWeight:   0.3,             // Weight for keyword match
}

Dependencies

  • github.com/glebarez/sqlite - Pure Go SQLite driver
  • github.com/google/uuid - UUID generation

Optional Extensions

  • sqlite-vec: Vector similarity search extension

  • FTS5: Full-text search (built into SQLite)

Integration with Agent Context

To integrate with the agent context builder:

import "github.com/smallnest/goclaw/memory"

// In agent/context.go
type ContextBuilder struct {
    // ... existing fields
    memoryManager *memory.MemoryManager
}

func (b *ContextBuilder) buildSystemPromptWithSkills(...) string {
    // ... existing code

    // Add relevant memories
    if b.memoryManager != nil {
        results, err := b.memoryManager.Search(ctx,
            "user preferences and context",
            memory.SearchOptions{
                Limit:    5,
                MinScore: 0.7,
            })
        if err == nil && len(results) > 0 {
            parts = append(parts, "## Relevant Memories\n\n")
            for _, r := range results {
                parts = append(parts, fmt.Sprintf("- %s\n", r.Text))
            }
        }
    }

    return fmt.Sprintf("%s\n\n", joinNonEmpty(parts, "\n\n---\n\n"))
}

Performance Considerations

  • Batch Size: OpenAI supports up to 2048 texts per batch
  • Caching: Enable for frequently accessed memories
  • Vector Extension: Optional but recommended for large datasets
  • FTS5: Faster than substring matching for text search

Future Enhancements

  1. Additional embedding providers (Voyage, local models)
  2. Automatic memory file indexing
  3. Session transcript integration
  4. Memory importance scoring
  5. Automatic memory pruning
  6. Hierarchical memory organization

Documentation

Index

Constants

View Source
const DayMs = 24 * 60 * 60 * 1000

DayMs is the number of milliseconds in a day

Variables

View Source
var DatedMemoryPathRE = regexp.MustCompile(`^(?:.*/)?memory/(\d{4})-(\d{2})-(\d{2})\.md$`)

DatedMemoryPathRE matches daily memory files like memory/YYYY-MM-DD.md

Functions

func Add

func Add(a, b []float32) ([]float32, error)

Add adds two vectors

func BM25RankToScore added in v0.3.1

func BM25RankToScore(rank float64) float64

BM25RankToScore converts BM25 rank to similarity score (0-1) Higher rank = lower score, using 1/(1+rank) formula

func BuildFTSQuery added in v0.3.1

func BuildFTSQuery(raw string) string

BuildFTSQuery builds an FTS query from raw text Extracts tokens and creates a quoted AND query

func BuildRelativePath added in v0.3.1

func BuildRelativePath(filePath, workspaceDir string) string

BuildRelativePath makes file path relative to workspace if possible

func ChunkText

func ChunkText(text string, maxTokens int) []string

ChunkText splits text into chunks suitable for embedding

func ComputeHash

func ComputeHash(vec []float32) uint64

ComputeHash computes a simple hash of a vector for caching purposes

func CosineSimilarity

func CosineSimilarity(a, b []float32) (float64, error)

CosineSimilarity computes the cosine similarity between two vectors

func DotProduct

func DotProduct(a, b []float32) (float64, error)

DotProduct computes the dot product of two vectors

func EuclideanDistance

func EuclideanDistance(a, b []float32) (float64, error)

EuclideanDistance computes the Euclidean distance between two vectors

func ExtractKeywords added in v0.3.1

func ExtractKeywords(query string) []string

ExtractKeywords extracts important keywords from query for expansion

func FormatCitationForDisplay added in v0.3.1

func FormatCitationForDisplay(ve *VectorEmbedding, mode CitationsMode, isGroupChat bool) string

FormatCitationForDisplay formats citation for display in responses Returns empty string if citations should be hidden

func GetCitationLineRange added in v0.3.1

func GetCitationLineRange(ve *VectorEmbedding) string

GetCitationLineRange returns the line range for citation display

func Magnitude

func Magnitude(vec []float32) (float64, error)

Magnitude computes the magnitude (length) of a vector

func Mean

func Mean(vectors [][]float32) ([]float32, error)

Mean computes the mean vector of multiple vectors

func Multiply

func Multiply(vec []float32, scalar float64) ([]float32, error)

Multiply multiplies a vector by a scalar

func Normalize

func Normalize(vec []float32) ([]float32, error)

Normalize normalizes a vector to unit length

func Subtract

func Subtract(a, b []float32) ([]float32, error)

Subtract subtracts vector b from vector a

Types

type BuiltinSearchManager

type BuiltinSearchManager struct {
	// contains filtered or unexported fields
}

BuiltinSearchManager builtin 后端实现

func (*BuiltinSearchManager) Add

func (m *BuiltinSearchManager) Add(ctx context.Context, text string, source MemorySource, memType MemoryType, metadata MemoryMetadata) error

Add 添加记忆

func (*BuiltinSearchManager) Close

func (m *BuiltinSearchManager) Close() error

Close 关闭管理器

func (*BuiltinSearchManager) GetStatus

func (m *BuiltinSearchManager) GetStatus() map[string]interface{}

GetStatus 获取状态

func (*BuiltinSearchManager) Search

func (m *BuiltinSearchManager) Search(ctx context.Context, query string, opts SearchOptions) ([]*SearchResult, error)

Search 执行搜索

type CitationsMode added in v0.3.1

type CitationsMode string

CitationsMode controls citation display behavior

const (
	// CitationsModeAuto shows citations in direct chats, hides in groups
	CitationsModeAuto CitationsMode = "auto"
	// CitationsModeOn always shows citations
	CitationsModeOn CitationsMode = "on"
	// CitationsModeOff never shows citations (agent still receives paths)
	CitationsModeOff CitationsMode = "off"
)

type EmbeddingProvider

type EmbeddingProvider interface {
	// Embed generates a single embedding
	Embed(text string) ([]float32, error)
	// EmbedBatch generates multiple embeddings in one call
	EmbedBatch(texts []string) ([][]float32, error)
	// Dimension returns the dimension of embeddings
	Dimension() int
	// MaxBatchSize returns the maximum batch size
	MaxBatchSize() int
}

EmbeddingProvider defines the interface for generating embeddings

type GeminiProvider

type GeminiProvider struct {
	// contains filtered or unexported fields
}

GeminiProvider implements EmbeddingProvider using Google's Gemini API This is a placeholder for future implementation

func NewGeminiProvider

func NewGeminiProvider(apiKey, model string) (*GeminiProvider, error)

NewGeminiProvider creates a new Gemini embedding provider

func (*GeminiProvider) Dimension

func (p *GeminiProvider) Dimension() int

Dimension returns the dimension of embeddings

func (*GeminiProvider) Embed

func (p *GeminiProvider) Embed(text string) ([]float32, error)

Embed generates a single embedding

func (*GeminiProvider) EmbedBatch

func (p *GeminiProvider) EmbedBatch(texts []string) ([][]float32, error)

EmbedBatch generates multiple embeddings in one call

func (*GeminiProvider) MaxBatchSize

func (p *GeminiProvider) MaxBatchSize() int

MaxBatchSize returns the maximum batch size

type HybridKeywordResult added in v0.3.1

type HybridKeywordResult struct {
	ID        string
	Path      string
	StartLine int
	EndLine   int
	Source    string
	Snippet   string
	TextScore float64
}

HybridKeywordResult represents a keyword/FTS search result

type HybridVectorResult added in v0.3.1

type HybridVectorResult struct {
	ID          string
	Path        string
	StartLine   int
	EndLine     int
	Source      string
	Snippet     string
	VectorScore float64
}

HybridVectorResult represents a vector search result

type LRUCache added in v0.3.1

type LRUCache struct {
	// contains filtered or unexported fields
}

LRUCache is a thread-safe LRU (Least Recently Used) cache

func NewLRUCache added in v0.3.1

func NewLRUCache(maxSize int) *LRUCache

NewLRUCache creates a new LRU cache with the specified maximum size

func (*LRUCache) Clear added in v0.3.1

func (c *LRUCache) Clear()

Clear removes all items from the cache

func (*LRUCache) Delete added in v0.3.1

func (c *LRUCache) Delete(key string)

Delete removes a key from the cache

func (*LRUCache) Get added in v0.3.1

func (c *LRUCache) Get(key string) (*VectorEmbedding, bool)

Get retrieves a value from the cache, moving it to the front (most recently used)

func (*LRUCache) Keys added in v0.3.1

func (c *LRUCache) Keys() []string

Keys returns all keys in the cache (ordered from most to least recent)

func (*LRUCache) Len added in v0.3.1

func (c *LRUCache) Len() int

Len returns the number of items in the cache

func (*LRUCache) Peek added in v0.3.1

func (c *LRUCache) Peek(key string) (*VectorEmbedding, bool)

Peek retrieves a value without updating its LRU position

func (*LRUCache) Put added in v0.3.1

func (c *LRUCache) Put(key string, value *VectorEmbedding)

Put adds a value to the cache, evicting the least recently used item if necessary

func (*LRUCache) SetOnEvict added in v0.3.1

func (c *LRUCache) SetOnEvict(fn func(key string, value *VectorEmbedding))

SetOnEvict sets a callback function to be called when items are evicted

type MMRConfig added in v0.3.1

type MMRConfig struct {
	// Enabled enables MMR re-ranking
	Enabled bool `json:"enabled"`
	// Lambda balances relevance vs diversity: 0=max diversity, 1=max relevance
	Lambda float64 `json:"lambda"`
}

MMRConfig configures Maximal Marginal Relevance re-ranking

func DefaultMMRConfig added in v0.3.1

func DefaultMMRConfig() MMRConfig

DefaultMMRConfig returns default MMR configuration

type ManagerConfig

type ManagerConfig struct {
	Store        Store
	Provider     EmbeddingProvider
	CacheMaxSize int
}

ManagerConfig configures the memory manager

func DefaultManagerConfig

func DefaultManagerConfig(store Store, provider EmbeddingProvider) ManagerConfig

DefaultManagerConfig returns default manager configuration

type MemoryItem

type MemoryItem struct {
	Text     string
	Source   MemorySource
	Type     MemoryType
	Metadata MemoryMetadata
}

MemoryItem represents a memory to be added

type MemoryManager

type MemoryManager struct {
	// contains filtered or unexported fields
}

MemoryManager manages memory storage and retrieval with LRU caching

func NewMemoryManager

func NewMemoryManager(config ManagerConfig) (*MemoryManager, error)

NewMemoryManager creates a new memory manager with LRU cache

func (*MemoryManager) AddMemory

func (m *MemoryManager) AddMemory(ctx context.Context, text string, source MemorySource, memType MemoryType, metadata MemoryMetadata) (*VectorEmbedding, error)

AddMemory adds a new memory with automatic embedding generation

func (*MemoryManager) AddMemoryBatch

func (m *MemoryManager) AddMemoryBatch(ctx context.Context, items []MemoryItem) error

AddMemoryBatch adds multiple memories with automatic embedding generation

func (*MemoryManager) ClearCache

func (m *MemoryManager) ClearCache()

ClearCache clears the in-memory cache

func (*MemoryManager) Close

func (m *MemoryManager) Close() error

Close closes the memory manager

func (*MemoryManager) Delete

func (m *MemoryManager) Delete(ctx context.Context, id string) error

Delete removes a memory

func (*MemoryManager) Get

Get retrieves a memory by ID

func (*MemoryManager) GetStats

func (m *MemoryManager) GetStats(ctx context.Context) (*MemoryStats, error)

GetStats returns statistics about the memory store

func (*MemoryManager) List

func (m *MemoryManager) List(ctx context.Context, filter func(*VectorEmbedding) bool) ([]*VectorEmbedding, error)

List lists all memories with optional filtering

func (*MemoryManager) Search

func (m *MemoryManager) Search(ctx context.Context, query string, opts SearchOptions) ([]*SearchResult, error)

Search searches for similar memories with advanced ranking options

func (*MemoryManager) SearchBySource

func (m *MemoryManager) SearchBySource(ctx context.Context, source MemorySource) ([]*VectorEmbedding, error)

SearchBySource searches memories by source

func (*MemoryManager) SearchByTag

func (m *MemoryManager) SearchByTag(ctx context.Context, tag string) ([]*VectorEmbedding, error)

SearchByTag searches memories by tag

func (*MemoryManager) SearchByText

func (m *MemoryManager) SearchByText(ctx context.Context, query string) ([]*VectorEmbedding, error)

SearchByText searches memories by text content (simple substring match)

func (*MemoryManager) SearchByType

func (m *MemoryManager) SearchByType(ctx context.Context, memType MemoryType) ([]*VectorEmbedding, error)

SearchByType searches memories by type

func (*MemoryManager) Update

func (m *MemoryManager) Update(ctx context.Context, ve *VectorEmbedding) error

Update updates an existing memory

type MemoryMetadata

type MemoryMetadata struct {
	// FilePath is the source file path (if from file)
	FilePath string `json:"file_path,omitempty"`
	// LineNumber is the line in the source file
	LineNumber int `json:"line_number,omitempty"`
	// EndLineNumber is the end line for multi-line snippets
	EndLineNumber int `json:"end_line_number,omitempty"`
	// SessionKey is the session identifier (if from session)
	SessionKey string `json:"session_key,omitempty"`
	// Tags are user-defined tags
	Tags []string `json:"tags,omitempty"`
	// Importance is a user-assigned importance score (0-1)
	Importance float64 `json:"importance,omitempty"`
	// AccessCount tracks how often this memory was accessed
	AccessCount int `json:"access_count,omitempty"`
	// LastAccessed is when this memory was last retrieved
	LastAccessed time.Time `json:"last_accessed,omitempty"`
	// Timestamp is the original timestamp for session/daily memories
	Timestamp *time.Time `json:"timestamp,omitempty"`
}

MemoryMetadata contains additional information about a memory

type MemorySearchManager

type MemorySearchManager interface {
	// Search 执行语义搜索
	Search(ctx context.Context, query string, opts SearchOptions) ([]*SearchResult, error)
	// Add 添加记忆(仅 builtin 支持)
	Add(ctx context.Context, text string, source MemorySource, memType MemoryType, metadata MemoryMetadata) error
	// GetStatus 获取状态
	GetStatus() map[string]interface{}
	// Close 关闭
	Close() error
}

MemorySearchManager 统一的记忆搜索接口

func GetBuiltinSearchManager

func GetBuiltinSearchManager(cfg config.MemoryConfig, workspace string) (MemorySearchManager, error)

GetBuiltinSearchManager 获取 builtin 搜索管理器

func GetMemorySearchManager

func GetMemorySearchManager(cfg config.MemoryConfig, workspace string) (MemorySearchManager, error)

GetMemorySearchManager 根据配置创建搜索管理器

func NewBuiltinSearchManager

func NewBuiltinSearchManager(cfg config.MemoryConfig, workspace string) (MemorySearchManager, error)

NewBuiltinSearchManager 创建 builtin 搜索管理器

func NewQMDSearchManager

func NewQMDSearchManager(qmdCfg config.QMDConfig, workspace string) (MemorySearchManager, error)

NewQMDSearchManager 创建 QMD 搜索管理器

type MemorySource

type MemorySource string

MemorySource represents where a memory entry originates

const (
	// MemorySourceLongTerm is from MEMORY.md and related files
	MemorySourceLongTerm MemorySource = "longterm"
	// MemorySourceSession is from conversation history
	MemorySourceSession MemorySource = "session"
	// MemorySourceDaily is from daily notes (YYYY-MM-DD.md)
	MemorySourceDaily MemorySource = "daily"
)

type MemoryStats

type MemoryStats struct {
	TotalCount int `json:"total_count"`
	CacheSize  int `json:"cache_size"`
}

MemoryStats contains statistics about the memory store

type MemoryType

type MemoryType string

MemoryType represents the type of memory content

const (
	// MemoryTypeFact is a factual piece of information
	MemoryTypeFact MemoryType = "fact"
	// MemoryTypePreference is user preference or setting
	MemoryTypePreference MemoryType = "preference"
	// MemoryTypeContext is situational context
	MemoryTypeContext MemoryType = "context"
	// MemoryTypeConversation is conversation summary
	MemoryTypeConversation MemoryType = "conversation"
)

type OpenAIConfig

type OpenAIConfig struct {
	APIKey     string
	BaseURL    string
	Model      string
	Timeout    time.Duration
	MaxRetries int
}

OpenAIConfig configures the OpenAI embedding provider

func DefaultOpenAIConfig

func DefaultOpenAIConfig(apiKey string) OpenAIConfig

DefaultOpenAIConfig returns default OpenAI configuration

type OpenAIProvider

type OpenAIProvider struct {
	// contains filtered or unexported fields
}

OpenAIProvider implements EmbeddingProvider using OpenAI's API

func NewOpenAIProvider

func NewOpenAIProvider(config OpenAIConfig) (*OpenAIProvider, error)

NewOpenAIProvider creates a new OpenAI embedding provider

func (*OpenAIProvider) Dimension

func (p *OpenAIProvider) Dimension() int

Dimension returns the dimension of embeddings

func (*OpenAIProvider) Embed

func (p *OpenAIProvider) Embed(text string) ([]float32, error)

Embed generates a single embedding

func (*OpenAIProvider) EmbedBatch

func (p *OpenAIProvider) EmbedBatch(texts []string) ([][]float32, error)

EmbedBatch generates multiple embeddings in one call

func (*OpenAIProvider) MaxBatchSize

func (p *OpenAIProvider) MaxBatchSize() int

MaxBatchSize returns the maximum batch size

type QMDSearchManager

type QMDSearchManager struct {
	// contains filtered or unexported fields
}

QMDSearchManager QMD 后端实现

func (*QMDSearchManager) Add

func (m *QMDSearchManager) Add(ctx context.Context, text string, source MemorySource, memType MemoryType, metadata MemoryMetadata) error

Add 添加记忆(QMD 不支持)

func (*QMDSearchManager) Close

func (m *QMDSearchManager) Close() error

Close 关闭管理器

func (*QMDSearchManager) GetStatus

func (m *QMDSearchManager) GetStatus() map[string]interface{}

GetStatus 获取状态

func (*QMDSearchManager) Search

func (m *QMDSearchManager) Search(ctx context.Context, query string, opts SearchOptions) ([]*SearchResult, error)

Search 执行搜索

type SQLiteStore

type SQLiteStore struct {
	// contains filtered or unexported fields
}

SQLiteStore implements the Store interface using SQLite

func NewSQLiteStore

func NewSQLiteStore(config StoreConfig) (*SQLiteStore, error)

NewSQLiteStore creates a new SQLite-based memory store

func (*SQLiteStore) Add

func (s *SQLiteStore) Add(embedding *VectorEmbedding) error

Add adds a memory to the store

func (*SQLiteStore) AddBatch

func (s *SQLiteStore) AddBatch(embeddings []*VectorEmbedding) error

AddBatch adds multiple memories in one transaction

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close closes the store

func (*SQLiteStore) Delete

func (s *SQLiteStore) Delete(id string) error

Delete removes a memory by ID

func (*SQLiteStore) Get

func (s *SQLiteStore) Get(id string) (*VectorEmbedding, error)

Get retrieves a memory by ID

func (*SQLiteStore) List

func (s *SQLiteStore) List(filter func(*VectorEmbedding) bool) ([]*VectorEmbedding, error)

List lists all memories with optional filtering

func (*SQLiteStore) Search

func (s *SQLiteStore) Search(query []float32, opts SearchOptions) ([]*SearchResult, error)

Search performs vector similarity search

func (*SQLiteStore) Update

func (s *SQLiteStore) Update(embedding *VectorEmbedding) error

Update updates an existing memory

type SearchOptions

type SearchOptions struct {
	// Limit is the maximum number of results to return
	Limit int `json:"limit"`
	// MinScore is the minimum similarity score (0-1)
	MinScore float64 `json:"min_score"`
	// Sources filters which memory sources to search
	Sources []MemorySource `json:"sources,omitempty"`
	// Types filters which memory types to search
	Types []MemoryType `json:"types,omitempty"`
	// Hybrid enables hybrid vector+keyword search
	Hybrid bool `json:"hybrid"`
	// VectorWeight is the weight for vector similarity in hybrid search (0-1)
	VectorWeight float64 `json:"vector_weight"`
	// TextWeight is the weight for keyword match in hybrid search (0-1)
	TextWeight float64 `json:"text_weight"`
	// MMR enables Maximal Marginal Relevance re-ranking for diversity
	MMR *MMRConfig `json:"mmr,omitempty"`
	// TemporalDecay enables time-aware scoring
	TemporalDecay *TemporalDecayConfig `json:"temporal_decay,omitempty"`
	// IncludeCitations adds citation strings to results
	IncludeCitations bool `json:"include_citations,omitempty"`
}

SearchOptions configures memory search behavior

func DefaultSearchOptions

func DefaultSearchOptions() SearchOptions

DefaultSearchOptions returns sensible default search options

type SearchResult

type SearchResult struct {
	VectorEmbedding
	Score       float64 `json:"score"`
	MatchedText string  `json:"matched_text"`
	Highlight   string  `json:"highlight,omitempty"`
	// VectorScore is the pure vector similarity score (before hybrid/decay)
	VectorScore float64 `json:"vector_score,omitempty"`
	// TextScore is the keyword match score (for hybrid search)
	TextScore float64 `json:"text_score,omitempty"`
	// Citation is the formatted citation string (e.g., "memory/2024-02-15.md#L10-L20")
	Citation string `json:"citation,omitempty"`
	// AgeInDays is the age of the memory in days (for temporal decay)
	AgeInDays float64 `json:"age_in_days,omitempty"`
}

SearchResult represents a memory search result with relevance score

func ApplyMMR added in v0.3.1

func ApplyMMR(results []*SearchResult, config MMRConfig) []*SearchResult

ApplyMMR re-ranks search results using MMR configuration

func ApplyTemporalDecay added in v0.3.1

func ApplyTemporalDecay(results []*SearchResult, config TemporalDecayConfig) []*SearchResult

ApplyTemporalDecay applies temporal decay to search results

func DecorateResultsWithCitations added in v0.3.1

func DecorateResultsWithCitations(results []*SearchResult, mode CitationsMode, isGroupChat bool) []*SearchResult

DecorateResultsWithCitations decorates search results with citations based on mode

func MergeHybridResults added in v0.3.1

func MergeHybridResults(vector []HybridVectorResult, keyword []HybridKeywordResult, vectorWeight, textWeight float64) []*SearchResult

MergeHybridResults merges vector and keyword search results Applies weighted scoring: score = vectorWeight * vectorScore + textWeight * textScore

func NormalizeScores added in v0.3.1

func NormalizeScores(results []*SearchResult) []*SearchResult

NormalizeScores normalizes scores to [0, 1] range

type Store

type Store interface {
	// Add adds a memory to the store
	Add(embedding *VectorEmbedding) error
	// AddBatch adds multiple memories in one transaction
	AddBatch(embeddings []*VectorEmbedding) error
	// Search performs similarity search
	Search(query []float32, opts SearchOptions) ([]*SearchResult, error)
	// Get retrieves a memory by ID
	Get(id string) (*VectorEmbedding, error)
	// Delete removes a memory by ID
	Delete(id string) error
	// Update updates an existing memory
	Update(embedding *VectorEmbedding) error
	// List lists all memories with optional filtering
	List(filter func(*VectorEmbedding) bool) ([]*VectorEmbedding, error)
	// Close closes the store
	Close() error
}

Store defines the interface for memory storage

type StoreConfig

type StoreConfig struct {
	// DBPath is the path to the SQLite database file
	DBPath string
	// Provider is the embedding provider to use
	Provider EmbeddingProvider
	// EnableVectorSearch enables vector similarity search (requires sqlite-vec)
	EnableVectorSearch bool
	// EnableFTS enables full-text search
	EnableFTS bool
	// VectorExtensionPath is the path to the sqlite-vec extension
	VectorExtensionPath string
}

StoreConfig configures the SQLite memory store

func DefaultStoreConfig

func DefaultStoreConfig(dbPath string, provider EmbeddingProvider) StoreConfig

DefaultStoreConfig returns default store configuration

type TemporalDecayConfig added in v0.3.1

type TemporalDecayConfig struct {
	// Enabled enables temporal decay
	Enabled bool `json:"enabled"`
	// HalfLifeDays is the half-life for exponential decay (default: 30 days)
	HalfLifeDays float64 `json:"half_life_days"`
}

TemporalDecayConfig configures time-aware scoring decay

func DefaultTemporalDecayConfig added in v0.3.1

func DefaultTemporalDecayConfig() TemporalDecayConfig

DefaultTemporalDecayConfig returns default temporal decay configuration

type VectorEmbedding

type VectorEmbedding struct {
	ID        string         `json:"id"`
	Vector    []float32      `json:"vector"`
	Dimension int            `json:"dimension"`
	Text      string         `json:"text"`
	Source    MemorySource   `json:"source"`
	Type      MemoryType     `json:"type"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	Metadata  MemoryMetadata `json:"metadata"`
}

VectorEmbedding represents a vector embedding with metadata

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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