Documentation
¶
Overview ¶
Package memory provides long-term agent memory with file-based storage, vector search, and hybrid retrieval.
Index ¶
- Constants
- type Chunk
- type FileStore
- type FileVectorStore
- func (s *FileVectorStore) Close() error
- func (s *FileVectorStore) Count() int
- func (s *FileVectorStore) DeleteBySource(_ context.Context, sourceFile string) error
- func (s *FileVectorStore) Index(_ context.Context, chunks []IndexedChunk) error
- func (s *FileVectorStore) Search(_ context.Context, queryVector []float32, k int) ([]SearchResult, error)
- type HybridSearcher
- type IndexedChunk
- type Logger
- type Manager
- func (m *Manager) AppendDailyLog(ctx context.Context, observation string) error
- func (m *Manager) Close() error
- func (m *Manager) GetFile(path string) (string, error)
- func (m *Manager) IndexAll(ctx context.Context) error
- func (m *Manager) IndexFile(ctx context.Context, path string) error
- func (m *Manager) Search(ctx context.Context, query string) ([]SearchResult, error)
- type ManagerConfig
- type SearchConfig
- type SearchResult
- type VectorStore
Constants ¶
const ( DefaultChunkChars = 1600 DefaultOverlapChars = 320 )
Default chunking parameters (~400 tokens at ~4 chars/token).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chunk ¶
type Chunk struct {
ID string `json:"id"`
Source string `json:"source"` // relative file path
Content string `json:"content"`
LineStart int `json:"line_start"`
LineEnd int `json:"line_end"`
CreatedAt time.Time `json:"created_at"`
}
Chunk is a segment of a memory file for indexing and search.
type FileStore ¶
type FileStore struct {
// contains filtered or unexported fields
}
FileStore manages the on-disk memory directory (.forge/memory).
func NewFileStore ¶
NewFileStore creates a FileStore rooted at dir, creating it if needed.
func (*FileStore) AppendDaily ¶
AppendDaily appends an entry to today's daily log (YYYY-MM-DD.md).
func (*FileStore) EnsureMemoryMD ¶
EnsureMemoryMD creates a template MEMORY.md if one doesn't exist.
type FileVectorStore ¶
type FileVectorStore struct {
// contains filtered or unexported fields
}
FileVectorStore is a JSON file-backed VectorStore. All data is loaded into memory; suitable for corpora under ~10K chunks.
func NewFileVectorStore ¶
func NewFileVectorStore(dir string) (*FileVectorStore, error)
NewFileVectorStore opens or creates a file-based vector store in dir.
func (*FileVectorStore) Close ¶
func (s *FileVectorStore) Close() error
Close flushes dirty data to disk.
func (*FileVectorStore) Count ¶
func (s *FileVectorStore) Count() int
Count returns the number of indexed chunks.
func (*FileVectorStore) DeleteBySource ¶
func (s *FileVectorStore) DeleteBySource(_ context.Context, sourceFile string) error
DeleteBySource removes all chunks from a given source file.
func (*FileVectorStore) Index ¶
func (s *FileVectorStore) Index(_ context.Context, chunks []IndexedChunk) error
Index adds or updates indexed chunks. Thread-safe.
func (*FileVectorStore) Search ¶
func (s *FileVectorStore) Search(_ context.Context, queryVector []float32, k int) ([]SearchResult, error)
Search performs a linear scan with cosine similarity. Thread-safe.
type HybridSearcher ¶
type HybridSearcher struct {
// contains filtered or unexported fields
}
HybridSearcher combines vector similarity, keyword overlap, and temporal decay for memory retrieval.
func NewHybridSearcher ¶
func NewHybridSearcher(store VectorStore, embedder llm.Embedder, config SearchConfig) *HybridSearcher
NewHybridSearcher creates a new hybrid searcher.
func (*HybridSearcher) Search ¶
func (h *HybridSearcher) Search(ctx context.Context, query string) ([]SearchResult, error)
Search performs hybrid search: vector + keyword + temporal decay. If no embedder is available, falls back to keyword-only search over all chunks.
type IndexedChunk ¶
IndexedChunk is a Chunk with its embedding vector.
type Logger ¶
type Logger interface {
Info(msg string, fields map[string]any)
Warn(msg string, fields map[string]any)
Error(msg string, fields map[string]any)
Debug(msg string, fields map[string]any)
}
Logger is the logging interface used by the memory package.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager orchestrates long-term memory: file storage, indexing, and search.
func NewManager ¶
func NewManager(cfg ManagerConfig) (*Manager, error)
NewManager creates a new memory Manager.
func (*Manager) AppendDailyLog ¶
AppendDailyLog appends an observation to today's daily log and indexes it.
type ManagerConfig ¶
type ManagerConfig struct {
MemoryDir string // root directory for memory files
Embedder llm.Embedder // nil = keyword-only mode
Logger Logger
SearchConfig SearchConfig
}
ManagerConfig configures a Manager.
type SearchConfig ¶
type SearchConfig struct {
VectorWeight float64 // weight for vector similarity (default: 0.7)
KeywordWeight float64 // weight for keyword overlap (default: 0.3)
DecayHalfLife time.Duration // temporal decay half-life (default: 7 days)
DecayEnabled bool // whether to apply temporal decay (default: true)
TopK int // max results to return (default: 10)
}
SearchConfig configures the hybrid search engine.
func DefaultSearchConfig ¶
func DefaultSearchConfig() SearchConfig
DefaultSearchConfig returns a SearchConfig with sensible defaults.
type SearchResult ¶
SearchResult is a chunk with its similarity score.
type VectorStore ¶
type VectorStore interface {
// Index adds or updates chunks with their embedding vectors.
Index(ctx context.Context, chunks []IndexedChunk) error
// Search returns the top-k most similar chunks to the query vector.
Search(ctx context.Context, queryVector []float32, k int) ([]SearchResult, error)
// DeleteBySource removes all chunks from a given source file.
DeleteBySource(ctx context.Context, sourceFile string) error
// Count returns the total number of indexed chunks.
Count() int
// Close flushes any pending writes and releases resources.
Close() error
}
VectorStore is the pluggable interface for vector storage backends. FileVectorStore is the initial implementation; swap to Qdrant/Pinecone later.