memory

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const LegacyModelName = "all-MiniLM-L6-v2"

Variables

This section is empty.

Functions

func Categorize

func Categorize(content string) string

func ExpandQueryAdvanced added in v0.2.0

func ExpandQueryAdvanced(query string) string

ExpandQueryAdvanced takes a raw user query and produces an FTS5-compatible query string. It handles:

  • Quoted phrases: "exact phrase" → preserved as FTS5 phrase
  • NEAR operator: word1 NEAR/5 word2 → preserved
  • Standalone words: prefix expansion (word*) + synonym expansion
  • Accent normalization before FTS5 generation

func ExpandQueryForFTS

func ExpandQueryForFTS(keywords []string) string

func ExtractKeywords

func ExtractKeywords(query string) []string

func Migrate

func Migrate(db *sql.DB) error

func NormalizeAccents added in v0.2.0

func NormalizeAccents(s string) string

func SplitOnPunctuation

func SplitOnPunctuation(text string) []string

func TruncateAtBoundary

func TruncateAtBoundary(s string, maxBytes int) string

func VectorNorm

func VectorNorm(v []float32) float64

Types

type DeleteScopeOptions added in v0.2.0

type DeleteScopeOptions struct {
	ProjectID string
	Category  string
	Source    string
	Hard      bool
}

type EmbeddingCache

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

func NewEmbeddingCache

func NewEmbeddingCache(db *sql.DB, logger *slog.Logger) *EmbeddingCache

func (*EmbeddingCache) Get

func (c *EmbeddingCache) Get(ctx context.Context, text, model string) ([]float32, bool)

func (*EmbeddingCache) MigrateFromLegacy added in v0.2.0

func (c *EmbeddingCache) MigrateFromLegacy(currentModel string) int64

func (*EmbeddingCache) Put

func (c *EmbeddingCache) Put(ctx context.Context, text, model string, vec []float32, quantize bool) error

type EmbeddingProvider

type EmbeddingProvider interface {
	Embed(ctx context.Context, texts []string) ([][]float32, error)
	Dimensions() int
	Name() string
	Model() string
	Close() error
}

type EntityDetector added in v0.2.0

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

func NewEntityDetector added in v0.2.0

func NewEntityDetector(db *sql.DB, cfg EntityDetectorConfig, logger *slog.Logger) *EntityDetector

func (*EntityDetector) Detect added in v0.2.0

func (d *EntityDetector) Detect(text string) []string

func (*EntityDetector) Refresh added in v0.2.0

func (d *EntityDetector) Refresh(ctx context.Context) error

type EntityDetectorConfig added in v0.2.0

type EntityDetectorConfig struct {
	CacheTTL    time.Duration
	MaxTokens   int
	MinTokenLen int
}

func DefaultEntityDetectorConfig added in v0.2.0

func DefaultEntityDetectorConfig() EntityDetectorConfig

type FastTokenizer added in v0.2.0

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

FastTokenizer parses HuggingFace tokenizer.json and produces ONNX-compatible input_ids matching Python transformers output.

func NewFastTokenizer added in v0.2.0

func NewFastTokenizer(tokenizerPath string, maxLen int) (*FastTokenizer, error)

NewFastTokenizer loads a HuggingFace tokenizer.json and builds a FastTokenizer.

func (*FastTokenizer) Tokenize added in v0.2.0

func (ft *FastTokenizer) Tokenize(text string) (inputIDs, attentionMask, tokenTypeIDs []int64)

type HybridSearchConfig

type HybridSearchConfig struct {
	VectorWeight              float64
	BM25Weight                float64
	MaxResults                int
	MinScore                  float64
	MMREnabled                bool
	MMRLambda                 float64
	TemporalDecayEnabled      bool
	TemporalDecayHalfLifeDays int
}

func DefaultHybridSearchConfig

func DefaultHybridSearchConfig() HybridSearchConfig

type HybridSearcher

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

func NewHybridSearcher

func NewHybridSearcher(store Store, embedder EmbeddingProvider, cache *EmbeddingCache, vectorCache *VectorCache, cfg HybridSearchConfig, entityDetector *EntityDetector, topicChangeDetector *TopicChangeDetector, logger *slog.Logger) *HybridSearcher

func (*HybridSearcher) Search

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

type ImportRecord added in v0.2.0

type ImportRecord struct {
	ID               string
	Source           string
	Path             string
	MemoriesImported int
	EntitiesImported int
	Status           string
	StartedAt        *time.Time
	FinishedAt       *time.Time
	Error            string
}

type ListOptions

type ListOptions struct {
	Limit     int
	Offset    int
	Category  string
	ProjectID string
}

type Memory

type Memory struct {
	ID           string     `json:"id"`
	ProjectID    *string    `json:"project_id,omitempty"`
	Category     string     `json:"category"`
	Content      string     `json:"content"`
	ContentHash  string     `json:"content_hash,omitempty"`
	Keywords     []string   `json:"keywords,omitempty"`
	Embedding    []float32  `json:"embedding,omitempty"`
	Source       string     `json:"source"`
	SourceID     *string    `json:"source_id,omitempty"`
	CreatedAt    time.Time  `json:"created_at"`
	UpdatedAt    time.Time  `json:"updated_at"`
	DeletedAt    *time.Time `json:"deleted_at,omitempty"`
	AccessCount  int        `json:"access_count"`
	LastAccessed *time.Time `json:"last_accessed,omitempty"`
	Metadata     any        `json:"metadata,omitempty"`
}

type MemoryIndexer added in v0.2.0

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

MemoryIndexer watches directories and incrementally indexes markdown/jsonl files. It uses polling (os.Stat) instead of filesystem events to avoid external dependencies.

func NewMemoryIndexer added in v0.2.0

func NewMemoryIndexer(svc *Service, paths []string, logger *slog.Logger) *MemoryIndexer

func (*MemoryIndexer) IndexNow added in v0.2.0

func (idx *MemoryIndexer) IndexNow(path string) error

IndexNow triggers immediate indexing of a specific file or directory.

func (*MemoryIndexer) SetInterval added in v0.2.0

func (idx *MemoryIndexer) SetInterval(d time.Duration)

SetInterval changes the polling interval. Must be called before Start.

func (*MemoryIndexer) Start added in v0.2.0

func (idx *MemoryIndexer) Start()

Start performs initial indexing of all watched paths, then starts a background goroutine that polls for changes.

func (*MemoryIndexer) Stop added in v0.2.0

func (idx *MemoryIndexer) Stop()

Stop terminates the background polling goroutine.

type MemoryMetadata added in v0.2.0

type MemoryMetadata struct {
	Source        string   `json:"source,omitempty"`         // "user", "auto_capture", "dream", "import", "precompact"
	SessionID     string   `json:"session_id,omitempty"`     // Source session that created this memory
	Consolidated  []string `json:"consolidated,omitempty"`   // IDs of memories merged into this one
	DreamVersion  string   `json:"dream_version,omitempty"`  // Dream run ID that processed this
	CaptureReason string   `json:"capture_reason,omitempty"` // Why this was captured
	QualityScore  float64  `json:"quality_score,omitempty"`  // Auto-capture quality score
}

MemoryMetadata provides structured metadata for memories. Stored as JSON in the Memory.Metadata field (which is `any` for backward compat).

func ParseMetadata added in v0.2.0

func ParseMetadata(v any) MemoryMetadata

ParseMetadata parses an `any` (from Memory.Metadata) into MemoryMetadata. Returns zero-value MemoryMetadata if input is nil or unparseable.

func (MemoryMetadata) MarshalJSON added in v0.2.0

func (m MemoryMetadata) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler using an alias to avoid infinite recursion.

func (MemoryMetadata) ToAny added in v0.2.0

func (m MemoryMetadata) ToAny() any

ToAny converts MemoryMetadata to the `any` type expected by Memory.Metadata.

type ONNXEmbedder

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

func NewONNXEmbedder

func NewONNXEmbedder(modelDir string, logger *slog.Logger) (*ONNXEmbedder, error)

func (*ONNXEmbedder) Close

func (e *ONNXEmbedder) Close() error

func (*ONNXEmbedder) Dimensions

func (e *ONNXEmbedder) Dimensions() int

func (*ONNXEmbedder) Embed

func (e *ONNXEmbedder) Embed(_ context.Context, texts []string) ([][]float32, error)

func (*ONNXEmbedder) Model

func (e *ONNXEmbedder) Model() string

func (*ONNXEmbedder) Name

func (e *ONNXEmbedder) Name() string

type ONNXPaths

type ONNXPaths struct {
	RuntimeLib    string
	ModelFile     string
	VocabFile     string
	TokenizerFile string
}

type QuantizedEmbedding

type QuantizedEmbedding struct {
	Data   []uint8
	Scale  float32
	MinVal float32
	Dims   int
}

func QuantizeFloat32

func QuantizeFloat32(vec []float32) QuantizedEmbedding

func (QuantizedEmbedding) CosineSimilarity

func (q QuantizedEmbedding) CosineSimilarity(query []float32, queryNorm float64) float64

func (QuantizedEmbedding) Dequantize

func (q QuantizedEmbedding) Dequantize() []float32

func (QuantizedEmbedding) DotProduct

func (q QuantizedEmbedding) DotProduct(query []float32) float64

func (QuantizedEmbedding) MarshalBinary

func (q QuantizedEmbedding) MarshalBinary() ([]byte, error)

func (*QuantizedEmbedding) UnmarshalBinary

func (q *QuantizedEmbedding) UnmarshalBinary(data []byte) error

type SQLiteStore

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

func NewSQLiteStore

func NewSQLiteStore(dbPath string, logger *slog.Logger) (*SQLiteStore, error)

func (*SQLiteStore) BackfillContentHash added in v0.2.0

func (s *SQLiteStore) BackfillContentHash(ctx context.Context) (int, error)

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

func (*SQLiteStore) CountWithoutEmbedding added in v0.2.0

func (s *SQLiteStore) CountWithoutEmbedding(ctx context.Context) (int, error)

func (*SQLiteStore) CreateImport added in v0.2.0

func (s *SQLiteStore) CreateImport(id, source, path string) error

func (*SQLiteStore) DB

func (s *SQLiteStore) DB() *sql.DB

func (*SQLiteStore) Delete

func (s *SQLiteStore) Delete(ctx context.Context, id string) error

func (*SQLiteStore) DeleteByScope added in v0.2.0

func (s *SQLiteStore) DeleteByScope(ctx context.Context, opts DeleteScopeOptions) (int, error)

func (*SQLiteStore) FindByContentHash added in v0.2.0

func (s *SQLiteStore) FindByContentHash(ctx context.Context, hash string, projectID *string) (*Memory, error)

func (*SQLiteStore) Get

func (s *SQLiteStore) Get(ctx context.Context, id string) (*Memory, error)

func (*SQLiteStore) GetLastImport added in v0.2.0

func (s *SQLiteStore) GetLastImport(source string) (*ImportRecord, error)

func (*SQLiteStore) List

func (s *SQLiteStore) List(ctx context.Context, opts ListOptions) ([]Memory, error)

func (*SQLiteStore) ListWithoutEmbedding added in v0.2.0

func (s *SQLiteStore) ListWithoutEmbedding(ctx context.Context, limit int) ([]Memory, error)

func (*SQLiteStore) Save

func (s *SQLiteStore) Save(ctx context.Context, m Memory) error

func (*SQLiteStore) Search

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

func (*SQLiteStore) SoftDelete added in v0.2.0

func (s *SQLiteStore) SoftDelete(ctx context.Context, id string) error

func (*SQLiteStore) Stats

func (s *SQLiteStore) Stats(ctx context.Context) (*StoreStats, error)

func (*SQLiteStore) Update added in v0.2.0

func (s *SQLiteStore) Update(ctx context.Context, id string, content string, category string) error

func (*SQLiteStore) UpdateEmbedding added in v0.2.0

func (s *SQLiteStore) UpdateEmbedding(ctx context.Context, id string, embedding []float32) error

func (*SQLiteStore) UpdateImport added in v0.2.0

func (s *SQLiteStore) UpdateImport(id, status string, memoriesImported int, errMsg string) error

func (*SQLiteStore) VectorCache added in v0.2.0

func (s *SQLiteStore) VectorCache() *VectorCache

type Sanitizer

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

func NewSanitizer

func NewSanitizer(enabled bool) *Sanitizer

func (*Sanitizer) Sanitize

func (s *Sanitizer) Sanitize(text string) string

type SaveOptions added in v0.2.0

type SaveOptions struct {
	Content   string
	Category  string
	Source    string
	SourceID  *string
	CWD       string
	SkipEmbed bool
}

type SearchOptions

type SearchOptions struct {
	MaxResults     int
	Category       string
	ProjectID      string
	BoostProjectID string // Project to boost in results (separate from filter)
	Since          *time.Time
}

type SearchResult

type SearchResult struct {
	Memory Memory
	Score  float64
}

type Service

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

func NewService

func NewService(cfg *config.Config, logger *slog.Logger) (*Service, error)

func (*Service) BackfillContentHash added in v0.2.0

func (s *Service) BackfillContentHash(ctx context.Context) (int, error)

func (*Service) BackfillEmbeddings added in v0.2.0

func (s *Service) BackfillEmbeddings(ctx context.Context, batchSize int) (int, error)

func (*Service) Close

func (s *Service) Close()

func (*Service) Forget

func (s *Service) Forget(ctx context.Context, id string) error

func (*Service) ForgetByScope added in v0.2.0

func (s *Service) ForgetByScope(ctx context.Context, projectID, category, source string, hard bool) (int, error)

func (*Service) Get

func (s *Service) Get(ctx context.Context, id string) (*Memory, error)

func (*Service) List

func (s *Service) List(ctx context.Context, opts ListOptions) ([]Memory, error)

func (*Service) ResolveProject added in v0.2.0

func (s *Service) ResolveProject(cwd string) string

ResolveProject returns the project ID for a given working directory, or empty string if none.

func (*Service) Save

func (s *Service) Save(ctx context.Context, content, category, source string, cwd string) (*Memory, error)

func (*Service) SaveMemory added in v0.1.1

func (s *Service) SaveMemory(ctx context.Context, content, category, source string, cwd string) error

func (*Service) SaveRaw added in v0.1.6

func (s *Service) SaveRaw(ctx context.Context, content, category, source string, cwd string) error

func (*Service) SaveRawNoEmbed added in v0.2.0

func (s *Service) SaveRawNoEmbed(ctx context.Context, content, category, source string, cwd string) error

func (*Service) SaveWithOptions added in v0.2.0

func (s *Service) SaveWithOptions(ctx context.Context, opts SaveOptions) (*Memory, error)

func (*Service) Search

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

func (*Service) SetKGExtractor added in v0.2.0

func (s *Service) SetKGExtractor(extractor *kg.PatternExtractor)

func (*Service) SoftForget added in v0.2.0

func (s *Service) SoftForget(ctx context.Context, id string) error

func (*Service) Stats

func (s *Service) Stats(ctx context.Context) (*StoreStats, error)

func (*Service) StoreDB

func (s *Service) StoreDB() *sql.DB

func (*Service) Update added in v0.2.0

func (s *Service) Update(ctx context.Context, id, content, category string) (*Memory, error)

func (*Service) VectorCache added in v0.2.0

func (s *Service) VectorCache() *VectorCache

type Store

type Store interface {
	Save(ctx context.Context, m Memory) error
	Get(ctx context.Context, id string) (*Memory, error)
	Update(ctx context.Context, id, content, category string) error
	Search(ctx context.Context, query string, opts SearchOptions) ([]SearchResult, error)
	Delete(ctx context.Context, id string) error
	SoftDelete(ctx context.Context, id string) error
	DeleteByScope(ctx context.Context, opts DeleteScopeOptions) (int, error)
	List(ctx context.Context, opts ListOptions) ([]Memory, error)
	Stats(ctx context.Context) (*StoreStats, error)
	UpdateEmbedding(ctx context.Context, id string, embedding []float32) error
	ListWithoutEmbedding(ctx context.Context, limit int) ([]Memory, error)
	FindByContentHash(ctx context.Context, hash string, projectID *string) (*Memory, error)
	BackfillContentHash(ctx context.Context) (int, error)
	DB() *sql.DB
	VectorCache() *VectorCache
	Close() error
}

type StoreStats

type StoreStats struct {
	TotalMemories int            `json:"total_memories"`
	ByCategory    map[string]int `json:"by_category"`
	ByProject     map[string]int `json:"by_project"`
}

type Tokenizer added in v0.2.0

type Tokenizer interface {
	Tokenize(text string) (inputIDs, attentionMask, tokenTypeIDs []int64)
}

Tokenizer converts text into ONNX-compatible token sequences.

type TopicChangeDetector added in v0.2.0

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

func NewTopicChangeDetector added in v0.2.0

func NewTopicChangeDetector(embedder EmbeddingProvider, entityDetector *EntityDetector) *TopicChangeDetector

func (*TopicChangeDetector) Check added in v0.2.0

func (d *TopicChangeDetector) Check(ctx context.Context, currentQuery string) (changed bool, err error)

func (*TopicChangeDetector) Reset added in v0.2.0

func (d *TopicChangeDetector) Reset()

type VectorCache added in v0.2.0

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

VectorCache is a thread-safe in-memory cache of memory embeddings keyed by memory ID.

func NewVectorCache added in v0.2.0

func NewVectorCache(logger *slog.Logger) *VectorCache

func (*VectorCache) All added in v0.2.0

func (c *VectorCache) All() map[string][]float32

func (*VectorCache) Get added in v0.2.0

func (c *VectorCache) Get(id string) ([]float32, bool)

func (*VectorCache) Len added in v0.2.0

func (c *VectorCache) Len() int

func (*VectorCache) Load added in v0.2.0

func (c *VectorCache) Load(db *sql.DB) error

func (*VectorCache) Put added in v0.2.0

func (c *VectorCache) Put(id string, embedding []float32)

func (*VectorCache) Remove added in v0.2.0

func (c *VectorCache) Remove(id string)

type WordPieceTokenizer

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

func NewWordPieceTokenizer

func NewWordPieceTokenizer(vocabPath string, maxLen int) (*WordPieceTokenizer, error)

func (*WordPieceTokenizer) Tokenize

func (t *WordPieceTokenizer) Tokenize(text string) (inputIDs, attentionMask, tokenTypeIDs []int64)

Jump to

Keyboard shortcuts

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