Documentation
¶
Overview ¶
Package memory provides a hierarchical long-term memory system for gleann.
Memory is organized into three tiers:
- Short-term (in-memory): Active conversation notes, ephemeral context
- Medium-term (BBolt): Daily summaries, conversation digests
- Long-term (BBolt): Persistent facts, user preferences, archived knowledge
Inspired by Letta's memory block architecture, adapted for gleann's RAG pipeline.
Package memory — Sleep-time compute engine.
Inspired by Letta's sleep-time agents, this subsystem runs a background goroutine that periodically reflects on recent conversations and autonomously updates memory blocks. It extracts key facts, prunes contradictions, and promotes important information — without blocking the interactive flow.
Configuration (env vars):
GLEANN_SLEEPTIME_ENABLED — "1" or "true" to enable (default: disabled) GLEANN_SLEEPTIME_INTERVAL — how often to run, Go duration (default: 30m) GLEANN_SLEEPTIME_MAX_CONVS — max recent conversations per cycle (default: 5)
Index ¶
- func DefaultStorePath() string
- type Block
- type ContextWindow
- type Manager
- func (m *Manager) AddNote(tier Tier, label, content string, tags ...string) (*Block, error)
- func (m *Manager) AddScopedNote(scope string, tier Tier, label, content string, tags ...string) (*Block, error)
- func (m *Manager) BuildContext() (*ContextWindow, error)
- func (m *Manager) BuildScopedContext(scope string) (*ContextWindow, error)
- func (m *Manager) Clear(tier Tier) (int, error)
- func (m *Manager) ClearAll() (int, error)
- func (m *Manager) Close() error
- func (m *Manager) EndSession() error
- func (m *Manager) Forget(idOrQuery string) (int, error)
- func (m *Manager) List(tier Tier) ([]Block, error)
- func (m *Manager) ListScoped(scope string, tier Tier) ([]Block, error)
- func (m *Manager) Remember(content string, tags ...string) (*Block, error)
- func (m *Manager) RunMaintenance() error
- func (m *Manager) Search(query string) ([]Block, error)
- func (m *Manager) SearchScoped(scope, query string) ([]Block, error)
- func (m *Manager) Stats() (*Stats, error)
- func (m *Manager) Store() *Store
- type SleepTimeConfig
- type SleepTimeEngine
- type Stats
- type Store
- func (s *Store) Add(block *Block) error
- func (s *Store) BuildContext() (*ContextWindow, error)
- func (s *Store) ClearAll() (int, error)
- func (s *Store) ClearTier(tier Tier) (int, error)
- func (s *Store) Close() error
- func (s *Store) Delete(id string) error
- func (s *Store) DeleteOlderThan(tier Tier, d time.Duration) (int, error)
- func (s *Store) DeleteSummariesOlderThan(d time.Duration) (int, error)
- func (s *Store) Get(id string) (*Block, error)
- func (s *Store) List(tier Tier) ([]Block, error)
- func (s *Store) ListSummaries() ([]Summary, error)
- func (s *Store) Path() string
- func (s *Store) Promote(id string, target Tier) error
- func (s *Store) PruneExpired() (int, error)
- func (s *Store) SaveSummary(summary *Summary) error
- func (s *Store) Search(query string) ([]Block, error)
- func (s *Store) Stats() (*Stats, error)
- type Summarizer
- type SummarizerConfig
- type Summary
- type Tier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultStorePath ¶
func DefaultStorePath() string
DefaultStorePath returns the default memory database path.
Types ¶
type Block ¶
type Block struct {
ID string `json:"id"` // Unique identifier (UUID-like or content hash)
Tier Tier `json:"tier"` // Storage tier
Label string `json:"label"` // Semantic label (e.g. "user_preference", "conversation_summary")
Content string `json:"content"` // The memory content
Source string `json:"source"` // Origin: "user", "auto_summary", "system", "sleep_time"
Tags []string `json:"tags"` // Searchable tags
Metadata map[string]string `json:"metadata"` // Arbitrary key-value metadata
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ExpiresAt *time.Time `json:"expires_at"` // nil = never expires
// Character limit for this block's content. 0 = unlimited.
// When content exceeds this limit, it is truncated or compressed.
CharLimit int `json:"char_limit,omitempty"`
// Scope isolates the block to a specific context (e.g. conversation ID,
// session ID, or a named group). Empty string means global (visible everywhere).
Scope string `json:"scope,omitempty"`
}
Block represents a single memory entry.
func (*Block) ExceedsLimit ¶
ExceedsLimit returns true if the block's content exceeds its character limit. Returns false when CharLimit is 0 (unlimited).
func (*Block) TruncateToLimit ¶
func (b *Block) TruncateToLimit()
TruncateToLimit trims content to the character limit if exceeded. Appends "... [truncated]" when trimming occurs.
type ContextWindow ¶
type ContextWindow struct {
ShortTerm []Block `json:"short_term,omitempty"`
MediumTerm []Block `json:"medium_term,omitempty"`
LongTerm []Block `json:"long_term,omitempty"`
Summaries []Summary `json:"summaries,omitempty"`
}
ContextWindow represents compiled memory for LLM injection.
func (*ContextWindow) Render ¶
func (cw *ContextWindow) Render() string
Render compiles the context window into a formatted string for LLM consumption.
type Manager ¶
type Manager struct {
// Configuration.
ShortTermTTL time.Duration // Default TTL for short-term blocks (0 = session only)
MediumTermMaxAge time.Duration // Auto-archive medium-term blocks older than this
AutoPromote bool // Auto-promote short-term blocks on session end
AutoCleanup bool // Auto-cleanup expired blocks
// Character limit defaults (Letta-inspired).
DefaultCharLimit int // Default char limit for new blocks (0 = unlimited)
EnforceCharLimit bool // Whether to truncate blocks that exceed their limit on Add
// contains filtered or unexported fields
}
Manager provides a high-level interface to the memory system with automatic lifecycle management (promotion, cleanup, expiration).
func DefaultManager ¶
DefaultManager opens the default store and returns a manager.
func NewManager ¶
NewManager creates a new memory manager wrapping the given store.
func (*Manager) AddScopedNote ¶
func (m *Manager) AddScopedNote(scope string, tier Tier, label, content string, tags ...string) (*Block, error)
AddScopedNote adds a note scoped to a specific context (e.g. conversation).
func (*Manager) BuildContext ¶
func (m *Manager) BuildContext() (*ContextWindow, error)
BuildContext compiles memory into a ContextWindow for LLM injection.
func (*Manager) BuildScopedContext ¶
func (m *Manager) BuildScopedContext(scope string) (*ContextWindow, error)
BuildScopedContext compiles memory visible to a specific scope. Includes both global blocks (scope="") and blocks matching the given scope.
func (*Manager) Clear ¶
Clear removes all blocks from the specified tier. This is the /clear command equivalent.
func (*Manager) EndSession ¶
EndSession handles session end: promotes short-term notes and cleans up.
func (*Manager) Forget ¶
Forget removes a memory block by ID or content match. This is the /forget command equivalent.
func (*Manager) ListScoped ¶
ListScoped lists blocks for a tier within a specific scope plus global blocks.
func (*Manager) Remember ¶
Remember adds important information to long-term memory. This is the /remember command equivalent.
func (*Manager) RunMaintenance ¶
RunMaintenance performs periodic maintenance: - Prune expired blocks - Archive old medium-term blocks to long-term - Clean up old summaries
func (*Manager) SearchScoped ¶
SearchScoped searches within a specific scope plus global blocks.
type SleepTimeConfig ¶
type SleepTimeConfig struct {
Enabled bool
Interval time.Duration
MaxConvs int // Max recent conversations to process per cycle.
ConvStore *conversations.Store
SummarizerCfg SummarizerConfig
}
SleepTimeConfig holds configuration for the sleep-time compute engine.
func DefaultSleepTimeConfig ¶
func DefaultSleepTimeConfig() SleepTimeConfig
DefaultSleepTimeConfig returns the default configuration, reading env vars.
type SleepTimeEngine ¶
type SleepTimeEngine struct {
// contains filtered or unexported fields
}
SleepTimeEngine runs background reflection on conversations.
func NewSleepTimeEngine ¶
func NewSleepTimeEngine(mgr *Manager, cfg SleepTimeConfig) *SleepTimeEngine
NewSleepTimeEngine creates a new sleep-time engine.
func (*SleepTimeEngine) RunOnce ¶
func (e *SleepTimeEngine) RunOnce(ctx context.Context) error
RunOnce executes a single sleep-time cycle synchronously. Useful for testing.
func (*SleepTimeEngine) Start ¶
func (e *SleepTimeEngine) Start(stopCh <-chan struct{})
Start launches the background goroutine. It stops when stopCh is closed.
type Stats ¶
type Stats struct {
ShortTermCount int `json:"short_term_count"`
MediumTermCount int `json:"medium_term_count"`
LongTermCount int `json:"long_term_count"`
TotalCount int `json:"total_count"`
DiskSizeBytes int64 `json:"disk_size_bytes"`
}
Stats holds memory system statistics.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store provides persistent memory storage backed by BBolt. Short-term blocks are kept in-memory with optional BBolt persistence. Medium-term and long-term blocks are always persisted.
func DefaultStore ¶
DefaultStore opens the default memory store.
func (*Store) Add ¶
Add stores a new memory block. Short-term blocks are kept in-memory; medium and long-term blocks are persisted to BBolt.
func (*Store) BuildContext ¶
func (s *Store) BuildContext() (*ContextWindow, error)
BuildContext compiles all active memory into a ContextWindow for LLM injection.
func (*Store) DeleteOlderThan ¶
DeleteOlderThan removes blocks older than the given duration from the specified tier.
func (*Store) DeleteSummariesOlderThan ¶
DeleteSummariesOlderThan removes summaries older than the given duration.
func (*Store) List ¶
List returns all blocks for a given tier, sorted by creation time (newest first). If tier is empty, returns all blocks across all tiers.
func (*Store) ListSummaries ¶
ListSummaries returns all summaries, newest first.
func (*Store) PruneExpired ¶
PruneExpired removes all expired blocks from all tiers.
func (*Store) SaveSummary ¶
SaveSummary stores a conversation summary.
type Summarizer ¶
type Summarizer struct {
// contains filtered or unexported fields
}
Summarizer generates conversation summaries for memory storage.
func NewSummarizer ¶
func NewSummarizer(cfg SummarizerConfig) *Summarizer
NewSummarizer creates a new summarizer with the given configuration.
func (*Summarizer) ExtractMemories ¶
func (s *Summarizer) ExtractMemories(ctx context.Context, conv *conversations.Conversation) ([]Block, error)
ExtractMemories analyzes a conversation and extracts important facts that should be stored as long-term memories.
func (*Summarizer) SummarizeConversation ¶
func (s *Summarizer) SummarizeConversation(ctx context.Context, conv *conversations.Conversation) (*Summary, error)
SummarizeConversation generates a concise summary of a conversation. The summary captures key topics, decisions, and important facts.
type SummarizerConfig ¶
type SummarizerConfig struct {
Provider string // "ollama", "openai", "anthropic"
Model string
BaseURL string
APIKey string
}
SummarizerConfig holds configuration for the auto-summarization LLM calls.
type Summary ¶
type Summary struct {
ConversationID string `json:"conversation_id"`
Title string `json:"title"`
Content string `json:"content"` // The summary text
MessageCount int `json:"message_count"`
IndexNames []string `json:"index_names"` // Which indexes were used
Model string `json:"model"` // LLM model used
CreatedAt time.Time `json:"created_at"`
}
Summary represents a conversation summary stored in medium-term memory.