memory

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package memory provides long-term agent memory with file-based storage, vector search, and hybrid retrieval.

Index

Constants

View Source
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.

func ChunkText

func ChunkText(text, source string, chunkSize, overlap int) []Chunk

ChunkText splits text from a source file into overlapping chunks. chunkSize and overlap are in characters. Use 0 for defaults.

type FileStore

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

FileStore manages the on-disk memory directory (.forge/memory).

func NewFileStore

func NewFileStore(dir string) (*FileStore, error)

NewFileStore creates a FileStore rooted at dir, creating it if needed.

func (*FileStore) AppendDaily

func (fs *FileStore) AppendDaily(entry string) error

AppendDaily appends an entry to today's daily log (YYYY-MM-DD.md).

func (*FileStore) Dir

func (fs *FileStore) Dir() string

Dir returns the root directory of the file store.

func (*FileStore) EnsureMemoryMD

func (fs *FileStore) EnsureMemoryMD() error

EnsureMemoryMD creates a template MEMORY.md if one doesn't exist.

func (*FileStore) ListFiles

func (fs *FileStore) ListFiles() ([]string, error)

ListFiles returns all .md files in the memory directory (relative paths).

func (*FileStore) ReadFile

func (fs *FileStore) ReadFile(relPath string) (string, error)

ReadFile reads a file relative to the memory directory. Returns an error if the path escapes the memory directory.

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

type IndexedChunk struct {
	Chunk  Chunk     `json:"chunk"`
	Vector []float32 `json:"vector"`
}

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

func (m *Manager) AppendDailyLog(ctx context.Context, observation string) error

AppendDailyLog appends an observation to today's daily log and indexes it.

func (*Manager) Close

func (m *Manager) Close() error

Close flushes the vector store to disk.

func (*Manager) GetFile

func (m *Manager) GetFile(path string) (string, error)

GetFile retrieves a memory file by relative path.

func (*Manager) IndexAll

func (m *Manager) IndexAll(ctx context.Context) error

IndexAll indexes all memory files (MEMORY.md + daily logs).

func (*Manager) IndexFile

func (m *Manager) IndexFile(ctx context.Context, path string) error

IndexFile indexes a single memory file by relative path.

func (*Manager) Search

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

Search queries long-term memory with hybrid search.

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

type SearchResult struct {
	Chunk Chunk   `json:"chunk"`
	Score float64 `json:"score"`
}

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.

Jump to

Keyboard shortcuts

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