mongodb

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package mongodb provides adapter types that bridge the concrete MongoDB store implementations to the agent-layer interfaces defined in agent/interfaces.go.

These adapters allow server.go to pass MongoDB stores to the agent builder without the agent package importing this package directly.

Package mongodb implements MongoDB-backed persistence stores for agent data.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = persistence.ErrNotFound

ErrNotFound re-exports persistence.ErrNotFound for callers that need to check it.

Functions

func IsNotFound

func IsNotFound(err error) bool

IsNotFound checks if an error is a not-found error.

Types

type BranchDocument

type BranchDocument struct {
	ID          string    `bson:"id"          json:"id"`
	Name        string    `bson:"name"        json:"name"`
	Description string    `bson:"description" json:"description,omitempty"`
	ParentIndex int       `bson:"parent_index" json:"parent_index"`
	IsActive    bool      `bson:"is_active"   json:"is_active"`
	CreatedAt   time.Time `bson:"created_at"  json:"created_at"`
}

BranchDocument stores conversation branch metadata.

type ConversationDocument

type ConversationDocument struct {
	ID        string            `bson:"_id"        json:"id"`
	AgentID   string            `bson:"agent_id"   json:"agent_id"`
	TenantID  string            `bson:"tenant_id"  json:"tenant_id"`
	UserID    string            `bson:"user_id"    json:"user_id"`
	Title     string            `bson:"title"      json:"title,omitempty"`
	Messages  []MessageDocument `bson:"messages"   json:"messages"`
	Branches  []BranchDocument  `bson:"branches"   json:"branches,omitempty"`
	Metadata  map[string]any    `bson:"metadata"   json:"metadata,omitempty"`
	CreatedAt time.Time         `bson:"created_at" json:"created_at"`
	UpdatedAt time.Time         `bson:"updated_at" json:"updated_at"`
}

ConversationDocument is the MongoDB document for a single conversation.

type ConversationFilter

type ConversationFilter struct {
	AgentID   string     `json:"agent_id,omitempty"`
	TenantID  string     `json:"tenant_id,omitempty"`
	UserID    string     `json:"user_id,omitempty"`
	StartTime *time.Time `json:"start_time,omitempty"`
	EndTime   *time.Time `json:"end_time,omitempty"`
	Limit     int        `json:"limit,omitempty"`
	Offset    int        `json:"offset,omitempty"`
}

ConversationFilter defines query parameters for listing conversations.

type ConversationStore

type ConversationStore interface {
	// Create inserts a new conversation.
	Create(ctx context.Context, doc *ConversationDocument) error
	// GetByID retrieves a conversation by ID.
	GetByID(ctx context.Context, id string) (*ConversationDocument, error)
	// AppendMessage adds a message to an existing conversation.
	AppendMessage(ctx context.Context, conversationID string, msg MessageDocument) error
	// GetMessages returns a paginated slice of messages from a conversation.
	GetMessages(ctx context.Context, conversationID string, limit, offset int) ([]MessageDocument, error)
	// List returns conversations matching the filter.
	List(ctx context.Context, filter ConversationFilter) ([]*ConversationDocument, error)
	// Delete removes a conversation by ID.
	Delete(ctx context.Context, id string) error
	// UpdateBranches replaces the branches array for a conversation.
	UpdateBranches(ctx context.Context, conversationID string, branches []BranchDocument) error
}

ConversationStore defines operations for conversation persistence.

type ConversationStoreAdapter

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

ConversationStoreAdapter wraps MongoConversationStore to satisfy agent.ConversationStoreProvider.

func NewConversationStoreAdapter

func NewConversationStoreAdapter(store *MongoConversationStore) *ConversationStoreAdapter

NewConversationStoreAdapter creates a ConversationStoreAdapter.

func (*ConversationStoreAdapter) AppendMessages

func (a *ConversationStoreAdapter) AppendMessages(ctx context.Context, conversationID string, msgs []agent.ConversationMessage) error

AppendMessages adds messages to an existing conversation.

func (*ConversationStoreAdapter) Create

Create inserts a new conversation document.

func (*ConversationStoreAdapter) GetByID

GetByID retrieves a conversation by ID.

func (*ConversationStoreAdapter) Underlying

Underlying returns the underlying MongoConversationStore for direct access.

type MessageDocument

type MessageDocument struct {
	Role       string               `bson:"role"         json:"role"`
	Content    string               `bson:"content"      json:"content,omitempty"`
	Name       string               `bson:"name"         json:"name,omitempty"`
	ToolCalls  []types.ToolCall     `bson:"tool_calls"   json:"tool_calls,omitempty"`
	ToolCallID string               `bson:"tool_call_id" json:"tool_call_id,omitempty"`
	Images     []types.ImageContent `bson:"images"      json:"images,omitempty"`
	Metadata   any                  `bson:"metadata"     json:"metadata,omitempty"`
	Timestamp  time.Time            `bson:"timestamp"    json:"timestamp,omitempty"`
}

MessageDocument maps to types.Message for BSON storage.

type MongoAuditBackend

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

MongoAuditBackend implements tools.AuditBackend backed by MongoDB.

func NewAuditBackend

func NewAuditBackend(ctx context.Context, client *mongoclient.Client) (*MongoAuditBackend, error)

NewAuditBackend creates a MongoAuditBackend and ensures indexes.

func (*MongoAuditBackend) Close

func (b *MongoAuditBackend) Close() error

Close is a no-op; the MongoDB client lifecycle is managed by pkg/mongodb.Client.

func (*MongoAuditBackend) Query

func (b *MongoAuditBackend) Query(ctx context.Context, filter *tools.AuditFilter) ([]*tools.AuditEntry, error)

Query retrieves audit entries matching the filter.

func (*MongoAuditBackend) Write

func (b *MongoAuditBackend) Write(ctx context.Context, entry *tools.AuditEntry) error

Write inserts an audit entry into MongoDB.

type MongoConversationStore

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

MongoConversationStore implements ConversationStore backed by MongoDB.

func NewConversationStore

func NewConversationStore(ctx context.Context, client *mongoclient.Client) (*MongoConversationStore, error)

NewConversationStore creates a MongoConversationStore and ensures indexes.

func (*MongoConversationStore) AppendMessage

func (s *MongoConversationStore) AppendMessage(ctx context.Context, conversationID string, msg MessageDocument) error

func (*MongoConversationStore) Create

func (*MongoConversationStore) Delete

func (s *MongoConversationStore) Delete(ctx context.Context, id string) error

func (*MongoConversationStore) GetByID

func (*MongoConversationStore) GetMessages

func (s *MongoConversationStore) GetMessages(ctx context.Context, conversationID string, limit, offset int) ([]MessageDocument, error)

GetMessages uses $slice projection to paginate the messages array.

func (*MongoConversationStore) List

func (*MongoConversationStore) UpdateBranches

func (s *MongoConversationStore) UpdateBranches(ctx context.Context, conversationID string, branches []BranchDocument) error

type MongoEpisodicStore

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

MongoEpisodicStore implements memory.EpisodicStore backed by MongoDB.

func NewEpisodicStore

func NewEpisodicStore(ctx context.Context, client *mongoclient.Client) (*MongoEpisodicStore, error)

NewEpisodicStore creates a MongoEpisodicStore and ensures indexes.

func (*MongoEpisodicStore) GetTimeline

func (s *MongoEpisodicStore) GetTimeline(ctx context.Context, agentID string, start, end time.Time) ([]memory.EpisodicEvent, error)

func (*MongoEpisodicStore) QueryEvents

func (*MongoEpisodicStore) RecordEvent

func (s *MongoEpisodicStore) RecordEvent(ctx context.Context, event *memory.EpisodicEvent) error

type MongoExperimentStore

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

MongoExperimentStore implements evaluation.ExperimentStore backed by MongoDB.

func NewExperimentStore

func NewExperimentStore(ctx context.Context, client *mongoclient.Client) (*MongoExperimentStore, error)

NewExperimentStore creates a MongoExperimentStore and ensures indexes.

func (*MongoExperimentStore) DeleteExperiment

func (s *MongoExperimentStore) DeleteExperiment(ctx context.Context, id string) error

func (*MongoExperimentStore) GetAssignment

func (s *MongoExperimentStore) GetAssignment(ctx context.Context, experimentID, userID string) (string, error)

func (*MongoExperimentStore) GetResults

func (s *MongoExperimentStore) GetResults(ctx context.Context, experimentID string) (map[string][]*evaluation.EvalResult, error)

func (*MongoExperimentStore) ListExperiments

func (s *MongoExperimentStore) ListExperiments(ctx context.Context) ([]*evaluation.Experiment, error)

func (*MongoExperimentStore) LoadExperiment

func (s *MongoExperimentStore) LoadExperiment(ctx context.Context, id string) (*evaluation.Experiment, error)

func (*MongoExperimentStore) RecordAssignment

func (s *MongoExperimentStore) RecordAssignment(ctx context.Context, experimentID, userID, variantID string) error

func (*MongoExperimentStore) RecordResult

func (s *MongoExperimentStore) RecordResult(ctx context.Context, experimentID, variantID string, result *evaluation.EvalResult) error

func (*MongoExperimentStore) SaveExperiment

func (s *MongoExperimentStore) SaveExperiment(ctx context.Context, exp *evaluation.Experiment) error

type MongoKnowledgeGraph

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

MongoKnowledgeGraph implements memory.KnowledgeGraph backed by MongoDB.

func NewKnowledgeGraph

func NewKnowledgeGraph(ctx context.Context, client *mongoclient.Client) (*MongoKnowledgeGraph, error)

NewKnowledgeGraph creates a MongoKnowledgeGraph and ensures indexes.

func (*MongoKnowledgeGraph) AddEntity

func (g *MongoKnowledgeGraph) AddEntity(ctx context.Context, entity *memory.Entity) error

func (*MongoKnowledgeGraph) AddRelation

func (g *MongoKnowledgeGraph) AddRelation(ctx context.Context, relation *memory.Relation) error

func (*MongoKnowledgeGraph) FindPath

func (g *MongoKnowledgeGraph) FindPath(ctx context.Context, fromID, toID string, maxDepth int) ([][]string, error)

FindPath finds paths between two entities using BFS up to maxDepth. It queries the relations collection iteratively, treating the graph as bidirectional (matching the in-memory implementation behavior).

func (*MongoKnowledgeGraph) QueryEntity

func (g *MongoKnowledgeGraph) QueryEntity(ctx context.Context, id string) (*memory.Entity, error)

func (*MongoKnowledgeGraph) QueryRelations

func (g *MongoKnowledgeGraph) QueryRelations(ctx context.Context, entityID string, relationType string) ([]memory.Relation, error)

type MongoMemoryStore

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

MongoMemoryStore implements memory.MemoryStore backed by MongoDB.

func NewMemoryStore

func NewMemoryStore(ctx context.Context, client *mongoclient.Client) (*MongoMemoryStore, error)

NewMemoryStore creates a MongoMemoryStore and ensures indexes.

func (*MongoMemoryStore) Clear

func (s *MongoMemoryStore) Clear(ctx context.Context) error

func (*MongoMemoryStore) Delete

func (s *MongoMemoryStore) Delete(ctx context.Context, key string) error

func (*MongoMemoryStore) List

func (s *MongoMemoryStore) List(ctx context.Context, pattern string, limit int) ([]any, error)

func (*MongoMemoryStore) Load

func (s *MongoMemoryStore) Load(ctx context.Context, key string) (any, error)

func (*MongoMemoryStore) Save

func (s *MongoMemoryStore) Save(ctx context.Context, key string, value any, ttl time.Duration) error

type MongoPromptStore

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

MongoPromptStore implements PromptStore backed by MongoDB.

func NewPromptStore

func NewPromptStore(ctx context.Context, client *mongoclient.Client) (*MongoPromptStore, error)

NewPromptStore creates a MongoPromptStore and ensures indexes.

func (*MongoPromptStore) Create

func (s *MongoPromptStore) Create(ctx context.Context, doc *PromptDocument) error

func (*MongoPromptStore) Delete

func (s *MongoPromptStore) Delete(ctx context.Context, id string) error

func (*MongoPromptStore) GetActive

func (s *MongoPromptStore) GetActive(ctx context.Context, agentType, name, tenantID string) (*PromptDocument, error)

func (*MongoPromptStore) GetByID

func (s *MongoPromptStore) GetByID(ctx context.Context, id string) (*PromptDocument, error)

func (*MongoPromptStore) ListByAgentType

func (s *MongoPromptStore) ListByAgentType(ctx context.Context, agentType, tenantID string, limit, offset int) ([]*PromptDocument, error)

func (*MongoPromptStore) SetActive

func (s *MongoPromptStore) SetActive(ctx context.Context, id string) error

func (*MongoPromptStore) Update

func (s *MongoPromptStore) Update(ctx context.Context, doc *PromptDocument) error

type MongoRegistryStore

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

MongoRegistryStore implements discovery.RegistryStore backed by MongoDB.

func NewRegistryStore

func NewRegistryStore(ctx context.Context, client *mongoclient.Client) (*MongoRegistryStore, error)

NewRegistryStore creates a MongoRegistryStore and ensures indexes. The _id index is created automatically by MongoDB; no additional indexes needed.

func (*MongoRegistryStore) Delete

func (s *MongoRegistryStore) Delete(ctx context.Context, id string) error

func (*MongoRegistryStore) Load

func (*MongoRegistryStore) LoadAll

func (*MongoRegistryStore) Save

type MongoRunStore

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

MongoRunStore implements RunStore backed by MongoDB.

func NewRunStore

func NewRunStore(ctx context.Context, client *mongoclient.Client) (*MongoRunStore, error)

NewRunStore creates a MongoRunStore and ensures indexes.

func (*MongoRunStore) GetByID

func (s *MongoRunStore) GetByID(ctx context.Context, id string) (*RunDocument, error)

func (*MongoRunStore) List

func (s *MongoRunStore) List(ctx context.Context, filter RunFilter) ([]*RunDocument, error)

func (*MongoRunStore) RecordRun

func (s *MongoRunStore) RecordRun(ctx context.Context, doc *RunDocument) error

func (*MongoRunStore) Stats

func (s *MongoRunStore) Stats(ctx context.Context, agentID, tenantID string) (*RunStats, error)

func (*MongoRunStore) UpdateStatus

func (s *MongoRunStore) UpdateStatus(ctx context.Context, id, status string, output *RunOutput, errMsg string) error

type PromptDocument

type PromptDocument struct {
	ID        string    `bson:"_id"        json:"id"`
	AgentType string    `bson:"agent_type" json:"agent_type"`
	Name      string    `bson:"name"       json:"name"`
	Version   string    `bson:"version"    json:"version"`
	TenantID  string    `bson:"tenant_id"  json:"tenant_id"`
	IsActive  bool      `bson:"is_active"  json:"is_active"`
	CreatedAt time.Time `bson:"created_at" json:"created_at"`
	UpdatedAt time.Time `bson:"updated_at" json:"updated_at"`

	// Embedded PromptBundle fields.
	System      agent.SystemPrompt `bson:"system"      json:"system"`
	Tools       []llm.ToolSchema   `bson:"tools"       json:"tools,omitempty"`
	Examples    []agent.Example    `bson:"examples"    json:"examples,omitempty"`
	Constraints []string           `bson:"constraints" json:"constraints,omitempty"`
}

PromptDocument is the MongoDB document schema for a PromptBundle.

type PromptStore

type PromptStore interface {
	// Create inserts a new prompt document.
	Create(ctx context.Context, doc *PromptDocument) error
	// GetByID retrieves a prompt by its ID.
	GetByID(ctx context.Context, id string) (*PromptDocument, error)
	// Update replaces an existing prompt document.
	Update(ctx context.Context, doc *PromptDocument) error
	// Delete removes a prompt by ID.
	Delete(ctx context.Context, id string) error
	// ListByAgentType returns prompts matching the given agent type and tenant.
	ListByAgentType(ctx context.Context, agentType, tenantID string, limit, offset int) ([]*PromptDocument, error)
	// GetActive returns the active prompt for a given agent type, name, and tenant.
	GetActive(ctx context.Context, agentType, name, tenantID string) (*PromptDocument, error)
	// SetActive marks a specific version as active and deactivates others.
	SetActive(ctx context.Context, id string) error
}

PromptStore defines CRUD operations for agent prompt bundles.

type PromptStoreAdapter

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

PromptStoreAdapter wraps MongoPromptStore to satisfy agent.PromptStoreProvider.

func NewPromptStoreAdapter

func NewPromptStoreAdapter(store *MongoPromptStore) *PromptStoreAdapter

NewPromptStoreAdapter creates a PromptStoreAdapter.

func (*PromptStoreAdapter) GetActive

func (a *PromptStoreAdapter) GetActive(ctx context.Context, agentType, name, tenantID string) (agent.PromptDocument, error)

GetActive returns the active prompt for the given agent type, name, and tenant.

func (*PromptStoreAdapter) Underlying

func (a *PromptStoreAdapter) Underlying() *MongoPromptStore

Underlying returns the underlying MongoPromptStore for direct access.

type RunDocument

type RunDocument struct {
	ID        string         `bson:"_id"         json:"id"`
	AgentID   string         `bson:"agent_id"    json:"agent_id"`
	TenantID  string         `bson:"tenant_id"   json:"tenant_id"`
	TraceID   string         `bson:"trace_id"    json:"trace_id,omitempty"`
	Status    string         `bson:"status"      json:"status"`
	Input     RunInput       `bson:"input"       json:"input"`
	Output    *RunOutput     `bson:"output"      json:"output,omitempty"`
	Error     string         `bson:"error"       json:"error,omitempty"`
	StartTime time.Time      `bson:"start_time"  json:"start_time"`
	EndTime   *time.Time     `bson:"end_time"    json:"end_time,omitempty"`
	Duration  time.Duration  `bson:"duration"    json:"duration,omitempty"`
	Metadata  map[string]any `bson:"metadata"    json:"metadata,omitempty"`
}

RunDocument is the MongoDB document for an agent execution run.

type RunFilter

type RunFilter struct {
	AgentID   string     `json:"agent_id,omitempty"`
	TenantID  string     `json:"tenant_id,omitempty"`
	Status    string     `json:"status,omitempty"`
	StartTime *time.Time `json:"start_time,omitempty"`
	EndTime   *time.Time `json:"end_time,omitempty"`
	Limit     int        `json:"limit,omitempty"`
	Offset    int        `json:"offset,omitempty"`
}

RunFilter defines query parameters for listing runs.

type RunInput

type RunInput struct {
	Content   string            `bson:"content"    json:"content"`
	Variables map[string]string `bson:"variables"  json:"variables,omitempty"`
	Context   map[string]any    `bson:"context"    json:"context,omitempty"`
}

RunInput mirrors agent.Input for BSON storage.

type RunOutput

type RunOutput struct {
	Content      string         `bson:"content"       json:"content"`
	TokensUsed   int            `bson:"tokens_used"   json:"tokens_used,omitempty"`
	Cost         float64        `bson:"cost"          json:"cost,omitempty"`
	FinishReason string         `bson:"finish_reason" json:"finish_reason,omitempty"`
	Metadata     map[string]any `bson:"metadata"      json:"metadata,omitempty"`
}

RunOutput mirrors agent.Output for BSON storage.

type RunStats

type RunStats struct {
	TotalRuns   int64   `bson:"total_runs"   json:"total_runs"`
	Completed   int64   `bson:"completed"    json:"completed"`
	Failed      int64   `bson:"failed"       json:"failed"`
	TotalTokens int64   `bson:"total_tokens" json:"total_tokens"`
	TotalCost   float64 `bson:"total_cost"   json:"total_cost"`
}

RunStats holds aggregated statistics for agent runs.

type RunStore

type RunStore interface {
	// RecordRun inserts a new run document.
	RecordRun(ctx context.Context, doc *RunDocument) error
	// GetByID retrieves a run by ID.
	GetByID(ctx context.Context, id string) (*RunDocument, error)
	// UpdateStatus updates the status, output, error, and timing of a run.
	UpdateStatus(ctx context.Context, id, status string, output *RunOutput, errMsg string) error
	// List returns runs matching the filter.
	List(ctx context.Context, filter RunFilter) ([]*RunDocument, error)
	// Stats returns aggregated statistics for runs matching the filter.
	Stats(ctx context.Context, agentID, tenantID string) (*RunStats, error)
}

RunStore defines operations for agent run persistence.

type RunStoreAdapter

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

RunStoreAdapter wraps MongoRunStore to satisfy agent.RunStoreProvider.

func NewRunStoreAdapter

func NewRunStoreAdapter(store *MongoRunStore) *RunStoreAdapter

NewRunStoreAdapter creates a RunStoreAdapter.

func (*RunStoreAdapter) RecordRun

func (a *RunStoreAdapter) RecordRun(ctx context.Context, doc *agent.RunDoc) error

RecordRun inserts a new run document.

func (*RunStoreAdapter) Underlying

func (a *RunStoreAdapter) Underlying() *MongoRunStore

Underlying returns the underlying MongoRunStore for direct access.

func (*RunStoreAdapter) UpdateStatus

func (a *RunStoreAdapter) UpdateStatus(ctx context.Context, id, status string, output *agent.RunOutputDoc, errMsg string) error

UpdateStatus updates the status and output of a run.

Jump to

Keyboard shortcuts

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