responsestore

package
v0.1.94 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package responsestore provides persistence for OpenAI-compatible Responses lifecycle endpoints.

Index

Constants

View Source
const (
	// DefaultMemoryStoreTTL bounds in-memory response retention by age.
	DefaultMemoryStoreTTL = 24 * time.Hour
	// DefaultMemoryStoreMaxEntries bounds in-memory response retention by count.
	DefaultMemoryStoreMaxEntries = 10000
	// DefaultMemoryStoreMaxBytes bounds in-memory response retention by total
	// serialized size. Agentic clients resend whole conversations as input, 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 response retention in persistent
	// backends. Matches OpenAI's documented 30-day retention for stored
	// responses; expired rows are swept hourly.
	DefaultPersistentStoreTTL = 30 * 24 * time.Hour

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

Variables

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

ErrNotFound indicates a requested response was not found.

Functions

This section is empty.

Types

type DetachedSnapshot added in v0.1.78

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

DetachedSnapshot is a snapshot captured as its serialized form. Detaching serializes exactly once, so a caller that hands the snapshot to another goroutine pays no second marshal when the store can persist the bytes directly, and no copy of the source can race later mutations. Explicit retention values from the source are kept alongside the bytes because serialized writes track retention outside the serialized data.

func Detach added in v0.1.78

func Detach(src *StoredResponse) (*DetachedSnapshot, error)

Detach normalizes and serializes a snapshot for a later Persist. The result shares no memory with src, so src may be mutated freely afterwards.

func (*DetachedSnapshot) ID added in v0.1.78

func (d *DetachedSnapshot) ID() string

ID returns the response id the snapshot persists under.

func (*DetachedSnapshot) Persist added in v0.1.78

func (d *DetachedSnapshot) Persist(ctx context.Context, store Store) error

Persist upserts the snapshot: Create first, falling back to Update when the id already holds a live row. Stores that support serialized writes receive the detached bytes directly; other stores decode the snapshot once and take the regular Create/Update path.

type MemoryStore

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

MemoryStore keeps response 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 response store. By default retention is bounded; pass WithUnboundedRetention to opt out.

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, response *StoredResponse) error

Create stores a new response snapshot.

func (*MemoryStore) Delete

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

Delete removes one response snapshot by id.

func (*MemoryStore) Get

Get retrieves one response snapshot by id.

func (*MemoryStore) Update

func (s *MemoryStore) Update(_ context.Context, response *StoredResponse) error

Update replaces an existing response snapshot.

type MemoryStoreOption

type MemoryStoreOption func(*MemoryStore)

MemoryStoreOption configures bounded in-memory response retention.

func WithMaxBytes

func WithMaxBytes(maxBytes int64) MemoryStoreOption

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

func WithMaxEntries

func WithMaxEntries(maxEntries int) MemoryStoreOption

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

func WithTTL

func WithTTL(ttl time.Duration) MemoryStoreOption

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

func WithUnboundedRetention

func WithUnboundedRetention() MemoryStoreOption

WithUnboundedRetention disables default in-memory retention bounds.

type MongoDBStore

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

MongoDBStore persists response 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) 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, response *StoredResponse) error

Create stores a new response 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 response snapshot by id.

func (*MongoDBStore) DeleteExpired

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

DeleteExpired removes all expired response snapshots.

func (*MongoDBStore) Get

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

Get retrieves one response snapshot by id.

func (*MongoDBStore) Update

func (s *MongoDBStore) Update(ctx context.Context, response *StoredResponse) error

Update replaces an existing, unexpired response snapshot. Zero StoredAt or ExpiresAt values preserve the stored retention fields.

type Result

type Result struct {
	Store Store
}

Result holds the initialized response store.

func New

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

New creates a response store on the shared storage connection.

func (*Result) Close

func (r *Result) Close() error

Close releases resources held by the response store.

type SQLStore added in v0.1.60

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

SQLStore persists response snapshots in a SQL database.

func NewSQLStore added in v0.1.60

func NewSQLStore(ctx context.Context, db sqlx.DB) (*SQLStore, error)

NewSQLStore creates the response_snapshots table if needed and starts the hourly expired-snapshot sweep.

func (*SQLStore) Close added in v0.1.60

func (s *SQLStore) Close() error

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

func (*SQLStore) Create added in v0.1.60

func (s *SQLStore) Create(ctx context.Context, response *StoredResponse) error

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

func (*SQLStore) Delete added in v0.1.60

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

Delete removes one unexpired response snapshot by id.

func (*SQLStore) DeleteExpired added in v0.1.60

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

DeleteExpired removes all expired response snapshots.

func (*SQLStore) Get added in v0.1.60

func (s *SQLStore) Get(ctx context.Context, id string) (*StoredResponse, error)

Get retrieves one response snapshot by id.

func (*SQLStore) Update added in v0.1.60

func (s *SQLStore) Update(ctx context.Context, response *StoredResponse) error

Update replaces an existing, unexpired response snapshot. Zero StoredAt or ExpiresAt values preserve the stored retention columns.

type Store

type Store interface {
	Create(ctx context.Context, response *StoredResponse) error
	Get(ctx context.Context, id string) (*StoredResponse, error)
	Update(ctx context.Context, response *StoredResponse) error
	Delete(ctx context.Context, id string) error
	Close() error
}

Store defines persistence operations for Responses lifecycle APIs.

type StoredResponse

type StoredResponse struct {
	Response           *core.ResponsesResponse `json:"response"`
	InputItems         []json.RawMessage       `json:"input_items,omitempty"`
	Provider           string                  `json:"provider,omitempty"`
	ProviderName       string                  `json:"provider_name,omitempty"`
	ProviderResponseID string                  `json:"provider_response_id,omitempty"`
	RequestID          string                  `json:"request_id,omitempty"`
	UserPath           string                  `json:"user_path,omitempty"`
	WorkflowVersionID  string                  `json:"workflow_version_id,omitempty"`
	StoredAt           time.Time               `json:"stored_at"`
	ExpiresAt          time.Time               `json:"expires_at"`
}

StoredResponse keeps the public response snapshot separate from gateway-only routing and input item metadata.

Jump to

Keyboard shortcuts

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