memory

package
v0.0.0-...-12e3ab2 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package memory provides persistent conversation storage and fact extraction for AI assistants.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClaudeExtractor

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

ClaudeExtractor implements Extractor using Claude CLI

func NewClaudeExtractor

func NewClaudeExtractor(
	claudePath string,
	workingDir string,
	timeout time.Duration,
	maxConversations int,
) (*ClaudeExtractor, error)

NewClaudeExtractor creates a new Claude-based extractor

func (*ClaudeExtractor) Extract

func (e *ClaudeExtractor) Extract(
	ctx context.Context,
	conversations []Conversation,
) ([]Fact, error)

Extract analyzes conversations and extracts facts

type Context

type Context struct {
	Facts         []Fact
	Conversations []Conversation
}

Context represents memory context to inject into prompts

type Conversation

type Conversation struct {
	ID          int64
	SessionID   string
	Timestamp   time.Time
	Query       string
	Response    string
	Metadata    map[string]string
	ExtractedAt *time.Time // Tracks when facts were extracted to prevent duplicates
}

Conversation represents a stored conversation in the database

type ConversationTurn

type ConversationTurn struct {
	SessionID string
	Timestamp time.Time
	Query     string
	Response  string
}

ConversationTurn represents a single Q&A exchange

type Extractor

type Extractor interface {
	Extract(ctx context.Context, conversations []Conversation) ([]Fact, error)
}

Extractor abstracts fact extraction (swappable LLM)

type Fact

type Fact struct {
	ID         int64
	Category   FactCategory
	Text       string
	Confidence float64 // 0.0-1.0
	SourceIDs  []int64 // conversation IDs
	CreatedAt  time.Time
	UpdatedAt  time.Time
}

Fact represents an extracted piece of user information

type FactCategory

type FactCategory string

FactCategory classifies different types of extracted facts

const (
	CategoryPreference FactCategory = "preference" // coffee, temperature
	CategoryHAPattern  FactCategory = "ha_pattern" // automation behaviors
	CategoryKnowledge  FactCategory = "knowledge"  // general facts
	CategorySchedule   FactCategory = "schedule"   // time-based
)

type Filter

type Filter struct {
	SessionID         string
	Since             time.Time
	NotExtractedSince time.Time // Load conversations not extracted since this time
	Categories        []FactCategory
	Limit             int
}

Filter configures database queries

type MemoryService

type MemoryService interface {
	// Remember stores a conversation turn
	Remember(ctx context.Context, turn ConversationTurn) error

	// Recall retrieves relevant context for a query
	Recall(ctx context.Context, query string, opts RecallOptions) (Context, error)

	// ExtractFacts extracts facts from recent conversations
	ExtractFacts(ctx context.Context) error

	// StartBackgroundExtraction starts periodic fact extraction
	StartBackgroundExtraction(interval time.Duration)

	// Close cleans up resources
	Close() error
}

MemoryService is the main interface for memory operations

type RecallOptions

type RecallOptions struct {
	IncludeFacts         bool
	IncludeConversations bool
	FactLimit            int
	ConversationLimit    int
}

RecallOptions configures what to retrieve

type Retriever

type Retriever interface {
	GetRelevantFacts(ctx context.Context, query string, limit int) ([]Fact, error)
}

Retriever abstracts context assembly

type SQLiteStorage

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

SQLiteStorage implements Storage interface using SQLite

func NewSQLiteStorage

func NewSQLiteStorage(dbPath string) (*SQLiteStorage, error)

NewSQLiteStorage creates a new SQLite storage instance

func (*SQLiteStorage) Close

func (s *SQLiteStorage) Close() error

Close closes the database connection

func (*SQLiteStorage) LoadConversations

func (s *SQLiteStorage) LoadConversations(
	ctx context.Context,
	filter Filter,
) ([]Conversation, error)

LoadConversations retrieves conversations matching filter

func (*SQLiteStorage) LoadFacts

func (s *SQLiteStorage) LoadFacts(ctx context.Context, filter Filter) ([]Fact, error)

LoadFacts retrieves facts matching filter

func (*SQLiteStorage) MarkExtracted

func (s *SQLiteStorage) MarkExtracted(
	ctx context.Context,
	convIDs []int64,
	extractedAt time.Time,
) error

MarkExtracted marks conversations as having been processed for fact extraction

func (*SQLiteStorage) SaveConversation

func (s *SQLiteStorage) SaveConversation(ctx context.Context, conv Conversation) error

SaveConversation stores a conversation

func (*SQLiteStorage) SaveFact

func (s *SQLiteStorage) SaveFact(ctx context.Context, fact Fact) error

SaveFact stores a fact

type Service

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

Service implements MemoryService interface

func NewService

func NewService(storage Storage, extractor Extractor, retriever Retriever) *Service

NewService creates a new memory service

func (*Service) Close

func (s *Service) Close() error

Close stops background tasks and closes storage

func (*Service) ExtractFacts

func (s *Service) ExtractFacts(ctx context.Context) error

ExtractFacts extracts facts from recent conversations

func (*Service) Recall

func (s *Service) Recall(ctx context.Context, query string, opts RecallOptions) (Context, error)

Recall retrieves relevant context for a query

func (*Service) Remember

func (s *Service) Remember(ctx context.Context, turn ConversationTurn) error

Remember stores a conversation turn

func (*Service) StartBackgroundExtraction

func (s *Service) StartBackgroundExtraction(interval time.Duration)

StartBackgroundExtraction starts periodic fact extraction

type SimpleRetriever

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

SimpleRetriever implements Retriever using keyword matching

func NewSimpleRetriever

func NewSimpleRetriever(storage Storage) *SimpleRetriever

NewSimpleRetriever creates a new retriever

func (*SimpleRetriever) GetRelevantFacts

func (r *SimpleRetriever) GetRelevantFacts(
	ctx context.Context,
	query string,
	limit int,
) ([]Fact, error)

GetRelevantFacts retrieves facts relevant to the query

type Storage

type Storage interface {
	SaveConversation(ctx context.Context, conv Conversation) error
	LoadConversations(ctx context.Context, filter Filter) ([]Conversation, error)
	MarkExtracted(ctx context.Context, convIDs []int64, extractedAt time.Time) error
	SaveFact(ctx context.Context, fact Fact) error
	LoadFacts(ctx context.Context, filter Filter) ([]Fact, error)
	Close() error
}

Storage abstracts the persistence layer

Jump to

Keyboard shortcuts

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