storage

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound        = errors.New("not found")
	ErrAlreadyExists   = errors.New("already exists")
	ErrVersionConflict = errors.New("version conflict")
)

Common errors returned by storage backends.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// Conversation memory operations
	ConversationStorage

	// Knowledge store operations
	KnowledgeStorage

	// Workflow context operations
	ContextStorage

	// Entity memory operations
	EntityStorage

	// Hybrid search operations (optional)
	HybridSearch

	// Lifecycle operations
	Lifecycle

	// Garbage collection operations
	GarbageCollection
}

Backend defines the interface for all storage operations. Implementations must be safe for concurrent use.

type ChunkExport

type ChunkExport struct {
	*types.Chunk
	Embedding []float32 `json:"embedding,omitempty"`
}

ChunkExport includes chunk data with embedding.

type ChunkSearchOpts

type ChunkSearchOpts struct {
	TopK         int
	MinScore     float64
	CollectionID *string           // Optional: limit to a specific collection
	Filters      map[string]string // Metadata filters
}

ChunkSearchOpts configures chunk search behavior.

type ContextHistoryExport

type ContextHistoryExport struct {
	Namespace string  `json:"namespace"`
	Key       string  `json:"key"`
	RunID     *string `json:"run_id,omitempty"`
	*types.ContextHistoryEntry
}

ContextHistoryExport includes full context history with namespace and key.

type ContextStorage

type ContextStorage interface {
	// GetContext retrieves a context entry by key.
	// runID is optional: nil means persistent (cross-run) context.
	GetContext(ctx context.Context, namespace, key string, runID *string) (*types.ContextEntry, error)

	// SetContext stores a context entry, incrementing the version.
	// If expectedVersion is provided, the operation fails if the current version doesn't match.
	SetContext(ctx context.Context, entry *types.ContextEntry, expectedVersion *int64) error

	// ListContextKeys returns all keys in a namespace, optionally filtered by prefix.
	ListContextKeys(ctx context.Context, namespace string, prefix *string, runID *string, cursor string, limit int) ([]string, string, error)

	// DeleteContext removes a context entry.
	DeleteContext(ctx context.Context, namespace, key string, runID *string) error

	// GetContextHistory returns the version history for a context key.
	GetContextHistory(ctx context.Context, namespace, key string, runID *string, cursor string, limit int) ([]*types.ContextHistoryEntry, string, error)

	// CleanupExpiredContext removes entries past their TTL expiration.
	// Returns the number of entries deleted.
	CleanupExpiredContext(ctx context.Context) (int64, error)

	// CleanupRunContext removes all context entries for a specific run.
	CleanupRunContext(ctx context.Context, namespace, runID string) error
}

ContextStorage defines operations for workflow context.

type ConversationStorage

type ConversationStorage interface {
	// AppendMessage adds a message to a thread, creating the thread if it doesn't exist.
	AppendMessage(ctx context.Context, msg *types.Message) error

	// GetMessages retrieves messages from a thread, ordered by creation time.
	// limit specifies the maximum number of messages to return (most recent first).
	GetMessages(ctx context.Context, namespace, threadID string, limit int, cursor string) ([]*types.Message, string, error)

	// SearchMessages performs semantic search across messages in a namespace.
	SearchMessages(ctx context.Context, namespace string, embedding []float32, opts MessageSearchOpts) ([]*types.MessageResult, error)

	// ListThreads returns all threads in a namespace.
	ListThreads(ctx context.Context, namespace string, cursor string, limit int) ([]*types.Thread, string, error)

	// GetThread retrieves a single thread by ID.
	GetThread(ctx context.Context, namespace, threadID string) (*types.Thread, error)

	// UpdateThread updates a thread's metadata (title, summary).
	UpdateThread(ctx context.Context, thread *types.Thread) error

	// DeleteThread removes a thread and all its messages.
	DeleteThread(ctx context.Context, namespace, threadID string) error

	// StoreMessageEmbedding stores the embedding for a message.
	StoreMessageEmbedding(ctx context.Context, messageID string, embedding []float32) error

	// MarkMessagesSummarized marks messages as having been included in a summary.
	MarkMessagesSummarized(ctx context.Context, namespace, threadID string, beforeTime int64) error
}

ConversationStorage defines operations for conversation memory.

type DocumentExport

type DocumentExport struct {
	*types.Document
}

DocumentExport includes document metadata.

type EntityAliasExport

type EntityAliasExport struct {
	Namespace string `json:"namespace"`
	Alias     string `json:"alias"`
	EntityID  string `json:"entity_id"`
}

EntityAliasExport represents an entity alias for export.

type EntityListOpts

type EntityListOpts struct {
	EntityType *types.EntityType
	SortBy     types.EntitySortBy
	Limit      int
	Cursor     string
}

EntityListOpts configures entity list behavior.

type EntitySearchOpts

type EntitySearchOpts struct {
	TopK       int
	MinScore   float64
	EntityType *types.EntityType
}

EntitySearchOpts configures entity search behavior.

type EntityStorage

type EntityStorage interface {
	// UpsertEntity creates or updates an entity.
	UpsertEntity(ctx context.Context, entity *types.Entity) error

	// GetEntityByID retrieves an entity by its ID.
	GetEntityByID(ctx context.Context, namespace, entityID string) (*types.Entity, error)

	// GetEntityByName retrieves an entity by its canonical name (case-insensitive).
	GetEntityByName(ctx context.Context, namespace, name string) (*types.Entity, error)

	// ResolveAlias looks up an entity by an alias.
	ResolveAlias(ctx context.Context, namespace, alias string) (*types.Entity, error)

	// SearchEntities performs semantic search across entity summaries.
	SearchEntities(ctx context.Context, namespace string, embedding []float32, opts EntitySearchOpts) ([]*types.EntityResult, error)

	// ListEntities returns entities in a namespace with optional filtering.
	ListEntities(ctx context.Context, namespace string, opts EntityListOpts) ([]*types.Entity, string, error)

	// DeleteEntity removes an entity and all its mentions/relationships.
	DeleteEntity(ctx context.Context, namespace, entityID string) error

	// MergeEntities combines two entities, moving all data from source to target.
	// The source entity is deleted after merging.
	MergeEntities(ctx context.Context, namespace, sourceID, targetID string) error

	// InsertMention records a mention of an entity in source content.
	InsertMention(ctx context.Context, mention *types.EntityMention) error

	// GetMentions retrieves recent mentions of an entity.
	GetMentions(ctx context.Context, entityID string, limit int) ([]*types.EntityMention, error)

	// UpsertRelationship creates or updates a relationship between entities.
	UpsertRelationship(ctx context.Context, rel *types.EntityRelationship) error

	// GetRelationships retrieves relationships for an entity.
	GetRelationships(ctx context.Context, namespace, entityID string, opts RelationshipOpts) ([]*types.EntityRelationship, error)

	// RegisterAlias adds an alias for an entity.
	RegisterAlias(ctx context.Context, namespace, alias, entityID string) error

	// StoreEntityEmbedding stores the embedding for an entity's summary.
	StoreEntityEmbedding(ctx context.Context, entityID string, embedding []float32) error

	// EnqueueExtraction adds an item to the entity extraction queue.
	EnqueueExtraction(ctx context.Context, item *types.ExtractionQueueItem) error

	// DequeueExtraction retrieves pending items from the extraction queue.
	// Items are marked as "processing" to prevent duplicate processing.
	DequeueExtraction(ctx context.Context, batchSize int) ([]*types.ExtractionQueueItem, error)

	// CompleteExtraction marks an extraction queue item as completed or failed.
	CompleteExtraction(ctx context.Context, itemID int64, status string) error

	// GetExtractionQueueStats returns statistics about the extraction queue.
	GetExtractionQueueStats(ctx context.Context) (*ExtractionQueueStats, error)
}

EntityStorage defines operations for entity memory.

type ExportData

type ExportData struct {
	Version    int    `json:"version"`
	Namespace  string `json:"namespace"`
	ExportedAt int64  `json:"exported_at"`

	// Conversation data
	Threads  []*ThreadExport  `json:"threads,omitempty"`
	Messages []*MessageExport `json:"messages,omitempty"`

	// Knowledge data
	Collections []*types.Collection `json:"collections,omitempty"`
	Documents   []*DocumentExport   `json:"documents,omitempty"`
	Chunks      []*ChunkExport      `json:"chunks,omitempty"`

	// Context data
	ContextEntries []*types.ContextEntry   `json:"context_entries,omitempty"`
	ContextHistory []*ContextHistoryExport `json:"context_history,omitempty"`

	// Entity data
	Entities      []*types.Entity             `json:"entities,omitempty"`
	EntityAliases []*EntityAliasExport        `json:"entity_aliases,omitempty"`
	Mentions      []*types.EntityMention      `json:"entity_mentions,omitempty"`
	Relationships []*types.EntityRelationship `json:"entity_relationships,omitempty"`
}

ExportData represents all data for a namespace that can be exported/imported.

type ExportOptions

type ExportOptions struct {
	IncludeConversations bool
	IncludeKnowledge     bool
	IncludeContext       bool
	IncludeEntities      bool
	IncludeEmbeddings    bool
}

ExportOptions configures what data to export.

func DefaultExportOptions

func DefaultExportOptions() ExportOptions

DefaultExportOptions returns options that export everything.

type ExtractionQueueStats

type ExtractionQueueStats struct {
	PendingCount    int64
	ProcessingCount int64
	FailedCount     int64
	DeadLetterCount int64
}

ExtractionQueueStats holds statistics about the extraction queue.

type GarbageCollection

type GarbageCollection interface {
	// DeleteOldConversations removes threads and their messages older than the specified duration.
	// Returns the number of threads deleted.
	DeleteOldConversations(ctx context.Context, olderThan time.Duration) (int64, error)

	// PruneStaleEntities removes entities that haven't been mentioned recently
	// and have fewer mentions than the threshold.
	// Returns the number of entities deleted.
	PruneStaleEntities(ctx context.Context, staleDuration time.Duration, minMentions int) (int64, error)

	// DeleteOrphanedChunks removes chunks whose parent documents no longer exist.
	// Returns the number of chunks deleted.
	DeleteOrphanedChunks(ctx context.Context) (int64, error)

	// CleanupContextHistory removes context version history entries older than the specified duration.
	// Returns the number of history entries deleted.
	CleanupContextHistory(ctx context.Context, olderThan time.Duration) (int64, error)

	// CleanupOldRunContext removes run-scoped context entries older than the specified duration.
	// Returns the number of entries deleted.
	CleanupOldRunContext(ctx context.Context, olderThan time.Duration) (int64, error)
}

GarbageCollection defines operations for data lifecycle management.

type HybridChunkSearchOpts

type HybridChunkSearchOpts struct {
	TopK         int
	MinScore     float64
	CollectionID *string           // Optional: limit to a specific collection
	Filters      map[string]string // Metadata filters
	Alpha        float64           // Weight for vector vs text (0=pure text, 1=pure vector, 0.5=equal)
	RRFConstant  float64           // RRF constant k (default: 60)
}

HybridChunkSearchOpts configures hybrid chunk search behavior.

type HybridEntitySearchOpts

type HybridEntitySearchOpts struct {
	TopK        int
	MinScore    float64
	EntityType  *types.EntityType
	Alpha       float64 // Weight for vector vs text (0=pure text, 1=pure vector, 0.5=equal)
	RRFConstant float64 // RRF constant k (default: 60)
}

HybridEntitySearchOpts configures hybrid entity search behavior.

type HybridSearch

type HybridSearch interface {
	// HybridSearchMessages performs combined vector and full-text search on messages.
	HybridSearchMessages(ctx context.Context, namespace string, query string, embedding []float32, opts HybridSearchOpts) ([]*types.MessageResult, error)

	// HybridSearchChunks performs combined vector and full-text search on chunks.
	HybridSearchChunks(ctx context.Context, namespace string, query string, embedding []float32, opts HybridChunkSearchOpts) ([]*types.ChunkResult, error)

	// HybridSearchEntities performs combined vector and full-text search on entities.
	HybridSearchEntities(ctx context.Context, namespace string, query string, embedding []float32, opts HybridEntitySearchOpts) ([]*types.EntityResult, error)
}

HybridSearch defines operations for combined vector + full-text search. Uses Reciprocal Rank Fusion (RRF) to combine results from both search methods.

type HybridSearchOpts

type HybridSearchOpts struct {
	TopK        int
	MinScore    float64
	ThreadID    *string // Optional: limit to a specific thread
	Alpha       float64 // Weight for vector vs text (0=pure text, 1=pure vector, 0.5=equal)
	RRFConstant float64 // RRF constant k (default: 60)
}

HybridSearchOpts configures hybrid message search behavior.

type KnowledgeStorage

type KnowledgeStorage interface {
	// InsertDocument adds a new document to the store.
	InsertDocument(ctx context.Context, doc *types.Document) error

	// InsertChunks adds chunks for a document. Embeddings should already be populated.
	InsertChunks(ctx context.Context, chunks []*types.Chunk) error

	// SearchChunks performs semantic search across chunks in a namespace.
	SearchChunks(ctx context.Context, namespace string, embedding []float32, opts ChunkSearchOpts) ([]*types.ChunkResult, error)

	// GetDocument retrieves a document by ID.
	GetDocument(ctx context.Context, namespace, docID string) (*types.Document, error)

	// DeleteDocument removes a document and all its chunks.
	DeleteDocument(ctx context.Context, namespace, docID string) error

	// GetAdjacentChunks retrieves chunks adjacent to a given chunk for context.
	// window specifies how many chunks before and after to retrieve.
	GetAdjacentChunks(ctx context.Context, chunkID string, window int) ([]*types.Chunk, error)

	// ListCollections returns all collections in a namespace.
	ListCollections(ctx context.Context, namespace string, cursor string, limit int) ([]*types.Collection, string, error)

	// GetCollection retrieves a collection by ID.
	GetCollection(ctx context.Context, namespace, collectionID string) (*types.Collection, error)

	// CreateCollection creates a new collection.
	CreateCollection(ctx context.Context, col *types.Collection) error

	// DeleteCollection removes a collection and all its documents.
	DeleteCollection(ctx context.Context, namespace, collectionID string) error

	// CollectionStats returns statistics for a collection.
	CollectionStats(ctx context.Context, namespace, collectionID string) (*types.CollectionStats, error)
}

KnowledgeStorage defines operations for the knowledge store.

type Lifecycle

type Lifecycle interface {
	// Migrate runs any necessary schema migrations.
	Migrate(ctx context.Context) error

	// Close releases any resources held by the backend.
	Close() error

	// Health checks the health of the storage backend.
	Health(ctx context.Context) error
}

Lifecycle defines operations for backend lifecycle management.

type MessageExport

type MessageExport struct {
	*types.Message
	Embedding []float32 `json:"embedding,omitempty"`
}

MessageExport includes message data with embedding.

type MessageSearchOpts

type MessageSearchOpts struct {
	TopK     int
	MinScore float64
	ThreadID *string // Optional: limit to a specific thread
}

MessageSearchOpts configures message search behavior.

type RelationshipOpts

type RelationshipOpts struct {
	RelationType *string
	Direction    types.RelationshipDirection
}

RelationshipOpts configures relationship queries.

type ThreadExport

type ThreadExport struct {
	*types.Thread
}

ThreadExport includes thread data with embedding.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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