conversationstore

package
v0.1.57 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package conversationstore provides persistence for the OpenAI-compatible Conversations lifecycle endpoints.

Index

Constants

View Source
const (
	// DefaultMemoryStoreTTL bounds in-memory conversation retention by age.
	// It mirrors the OpenAI Conversations retention window (~30 days).
	DefaultMemoryStoreTTL = 30 * 24 * time.Hour
	// DefaultMemoryStoreMaxEntries bounds in-memory conversation retention by count.
	DefaultMemoryStoreMaxEntries = 10000
	// DefaultMemoryStoreMaxBytes bounds in-memory conversation retention by
	// total serialized size. Conversations grow per turn without bound, so
	// entry counts alone do not bound memory.
	DefaultMemoryStoreMaxBytes = 64 << 20
	// DefaultMemoryStoreCleanupInterval limits full expired-entry sweeps.
	DefaultMemoryStoreCleanupInterval = time.Minute
)
View Source
const (
	// DefaultPersistentStoreTTL bounds stored conversation retention in
	// persistent backends, matching the in-memory default; expired rows are
	// swept hourly.
	DefaultPersistentStoreTTL = 30 * 24 * time.Hour

	// CleanupInterval is how often persistent stores sweep expired conversations.
	CleanupInterval = 1 * time.Hour
)

Variables

View Source
var ErrNotFound = errors.New("conversation not found")

ErrNotFound indicates a requested conversation was not found.

Functions

This section is empty.

Types

type MemoryStore

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

MemoryStore keeps conversation snapshots in process memory. Data survives across requests but not process restarts.

func NewMemoryStore

func NewMemoryStore(options ...MemoryStoreOption) *MemoryStore

NewMemoryStore creates an empty in-memory conversation store. Retention is bounded by default; options can adjust or disable the bounds.

func (*MemoryStore) AppendItems

func (s *MemoryStore) AppendItems(_ context.Context, id string, items []json.RawMessage) error

AppendItems atomically appends items to an existing conversation snapshot.

func (*MemoryStore) Close

func (s *MemoryStore) Close() error

Close releases resources (no-op for memory store).

func (*MemoryStore) Create

func (s *MemoryStore) Create(_ context.Context, conversation *StoredConversation) error

Create stores a new conversation snapshot.

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(_ context.Context, id string) error

Delete removes one conversation snapshot by id.

func (*MemoryStore) Get

Get retrieves one conversation snapshot by id.

func (*MemoryStore) Update

func (s *MemoryStore) Update(_ context.Context, conversation *StoredConversation) error

Update replaces an existing conversation snapshot.

type MemoryStoreOption

type MemoryStoreOption func(*MemoryStore)

MemoryStoreOption configures bounded in-memory conversation retention.

func WithMaxBytes

func WithMaxBytes(maxBytes int64) MemoryStoreOption

WithMaxBytes caps the total serialized size of stored conversations with FIFO eviction. Non-positive values disable the cap.

func WithMaxEntries

func WithMaxEntries(maxEntries int) MemoryStoreOption

WithMaxEntries caps stored conversations with FIFO eviction. Non-positive values disable the cap.

func WithTTL

func WithTTL(ttl time.Duration) MemoryStoreOption

WithTTL expires stored conversations after ttl. Non-positive values disable TTL.

type MongoDBStore

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

MongoDBStore persists conversation snapshots in MongoDB.

func NewMongoDBStore

func NewMongoDBStore(database *mongo.Database) (*MongoDBStore, error)

NewMongoDBStore creates collection indexes if needed and starts the hourly expired-snapshot sweep.

func (*MongoDBStore) AppendItems

func (s *MongoDBStore) AppendItems(ctx context.Context, id string, items []json.RawMessage) error

AppendItems atomically appends items to an existing, unexpired conversation via $push, so two concurrently completing turns cannot overwrite each other's exchange.

func (*MongoDBStore) Close

func (s *MongoDBStore) Close() error

Close stops the cleanup loop; client lifecycle is managed by the storage layer.

func (*MongoDBStore) Create

func (s *MongoDBStore) Create(ctx context.Context, conversation *StoredConversation) error

Create stores a new conversation snapshot. An existing snapshot with the same id is only replaced when it has already expired.

func (*MongoDBStore) Delete

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

Delete removes one unexpired conversation snapshot by id.

func (*MongoDBStore) DeleteExpired

func (s *MongoDBStore) DeleteExpired(ctx context.Context) error

DeleteExpired removes all expired conversation snapshots.

func (*MongoDBStore) Get

Get retrieves one conversation snapshot by id.

func (*MongoDBStore) Update

func (s *MongoDBStore) Update(ctx context.Context, conversation *StoredConversation) error

Update replaces an existing, unexpired conversation snapshot including its items. Zero StoredAt or ExpiresAt values preserve the stored retention fields.

type PostgreSQLStore

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

PostgreSQLStore persists conversation snapshots in PostgreSQL.

func NewPostgreSQLStore

func NewPostgreSQLStore(ctx context.Context, pool *pgxpool.Pool) (*PostgreSQLStore, error)

NewPostgreSQLStore creates the conversation_snapshots table if needed and starts the hourly expired-snapshot sweep.

func (*PostgreSQLStore) AppendItems

func (s *PostgreSQLStore) AppendItems(ctx context.Context, id string, items []json.RawMessage) error

AppendItems atomically appends items to an existing, unexpired conversation using jsonb array concatenation, so two concurrently completing turns cannot overwrite each other's exchange.

func (*PostgreSQLStore) Close

func (s *PostgreSQLStore) Close() error

Close stops the cleanup loop; pool lifecycle is managed by the storage layer.

func (*PostgreSQLStore) Create

func (s *PostgreSQLStore) Create(ctx context.Context, conversation *StoredConversation) error

Create stores a new conversation snapshot. An existing snapshot with the same id is only replaced when it has already expired.

func (*PostgreSQLStore) Delete

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

Delete removes one unexpired conversation snapshot by id.

func (*PostgreSQLStore) DeleteExpired

func (s *PostgreSQLStore) DeleteExpired(ctx context.Context) error

DeleteExpired removes all expired conversation snapshots.

func (*PostgreSQLStore) Get

Get retrieves one conversation snapshot by id.

func (*PostgreSQLStore) Update

func (s *PostgreSQLStore) Update(ctx context.Context, conversation *StoredConversation) error

Update replaces an existing, unexpired conversation snapshot including its items. Zero StoredAt or ExpiresAt values preserve the stored retention columns.

type Result

type Result struct {
	Store   Store
	Storage storage.Storage
}

Result holds the initialized conversation store and optional owned storage.

func New

func New(ctx context.Context, cfg *config.Config) (*Result, error)

New creates a conversation store from app configuration.

func NewWithSharedStorage

func NewWithSharedStorage(ctx context.Context, shared storage.Storage) (*Result, error)

NewWithSharedStorage creates a conversation store using a shared storage connection.

func (*Result) Close

func (r *Result) Close() error

Close releases resources held by the conversation store.

type SQLiteStore

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

SQLiteStore persists conversation snapshots in SQLite.

func NewSQLiteStore

func NewSQLiteStore(db *sql.DB) (*SQLiteStore, error)

NewSQLiteStore creates the conversation_snapshots table if needed and starts the hourly expired-snapshot sweep.

func (*SQLiteStore) AppendItems

func (s *SQLiteStore) AppendItems(ctx context.Context, id string, items []json.RawMessage) error

AppendItems atomically appends items to an existing, unexpired conversation. The append happens in a single UPDATE via chained json_insert '$[#]' paths, so two concurrently completing turns cannot overwrite each other's exchange.

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close stops the cleanup loop; DB lifecycle is managed by the storage layer.

func (*SQLiteStore) Create

func (s *SQLiteStore) Create(ctx context.Context, conversation *StoredConversation) error

Create stores a new conversation snapshot. An existing snapshot with the same id is only replaced when it has already expired.

func (*SQLiteStore) Delete

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

Delete removes one unexpired conversation snapshot by id.

func (*SQLiteStore) DeleteExpired

func (s *SQLiteStore) DeleteExpired(ctx context.Context) error

DeleteExpired removes all expired conversation snapshots.

func (*SQLiteStore) Get

Get retrieves one conversation snapshot by id.

func (*SQLiteStore) Update

func (s *SQLiteStore) Update(ctx context.Context, conversation *StoredConversation) error

Update replaces an existing, unexpired conversation snapshot including its items. Zero StoredAt or ExpiresAt values preserve the stored retention columns.

type Store

type Store interface {
	Create(ctx context.Context, conversation *StoredConversation) error
	Get(ctx context.Context, id string) (*StoredConversation, error)
	Update(ctx context.Context, conversation *StoredConversation) error
	// AppendItems atomically appends items to an existing conversation, so two
	// concurrently completing turns cannot overwrite each other's exchange the
	// way a Get-then-Update would.
	AppendItems(ctx context.Context, id string, items []json.RawMessage) error
	Delete(ctx context.Context, id string) error
	Close() error
}

Store defines persistence operations for the Conversations lifecycle API.

type StoredConversation

type StoredConversation struct {
	Conversation *core.Conversation `json:"conversation"`
	Items        []json.RawMessage  `json:"items,omitempty"`
	UserPath     string             `json:"user_path,omitempty"`
	RequestID    string             `json:"request_id,omitempty"`
	StoredAt     time.Time          `json:"stored_at"`
	ExpiresAt    time.Time          `json:"expires_at"`
}

StoredConversation keeps the public conversation snapshot separate from gateway-only metadata (initial items, owning user path, request id).

Jump to

Keyboard shortcuts

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