Documentation
¶
Overview ¶
Package statestore provides conversation state persistence and management.
Index ¶
- Variables
- type ConversationState
- type ListOptions
- type MemoryStore
- func (s *MemoryStore) Delete(ctx context.Context, id string) error
- func (s *MemoryStore) Fork(ctx context.Context, sourceID, newID string) error
- func (s *MemoryStore) List(ctx context.Context, opts ListOptions) ([]string, error)
- func (s *MemoryStore) Load(ctx context.Context, id string) (*ConversationState, error)
- func (s *MemoryStore) Save(ctx context.Context, state *ConversationState) error
- type RedisOption
- type RedisStore
- func (s *RedisStore) Delete(ctx context.Context, id string) error
- func (s *RedisStore) Fork(ctx context.Context, sourceID, newID string) error
- func (s *RedisStore) List(ctx context.Context, opts ListOptions) ([]string, error)
- func (s *RedisStore) Load(ctx context.Context, id string) (*ConversationState, error)
- func (s *RedisStore) Save(ctx context.Context, state *ConversationState) error
- type Store
- type Summary
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidID = errors.New("invalid conversation ID")
ErrInvalidID is returned when an invalid conversation ID is provided.
var ErrInvalidState = errors.New("invalid conversation state")
ErrInvalidState is returned when a conversation state is invalid.
var ErrNotFound = errors.New("conversation not found")
ErrNotFound is returned when a conversation doesn't exist in the store.
Functions ¶
This section is empty.
Types ¶
type ConversationState ¶
type ConversationState struct {
ID string // Unique conversation identifier
UserID string // User who owns this conversation
Messages []types.Message // Message history (using unified types.Message)
SystemPrompt string // System prompt for this conversation
Summaries []Summary // Compressed summaries of old turns
TokenCount int // Total tokens in messages
LastAccessedAt time.Time // Last time conversation was accessed
Metadata map[string]interface{} // Arbitrary metadata (e.g., extracted context)
}
ConversationState represents stored conversation state in the state store. This is the primary data structure for persisting and loading conversation history.
type ListOptions ¶
type ListOptions struct {
// UserID filters conversations by the user who owns them.
// If empty, all conversations are returned (subject to pagination).
UserID string
// Limit is the maximum number of conversation IDs to return.
// If 0, a default limit (e.g., 100) should be applied.
Limit int
// Offset is the number of conversations to skip (for pagination).
Offset int
// SortBy specifies the field to sort by (e.g., "created_at", "updated_at").
// If empty, implementation-specific default sorting is used.
SortBy string
// SortOrder specifies sort direction: "asc" or "desc".
// If empty, defaults to "desc" (newest first).
SortOrder string
}
ListOptions provides filtering and pagination options for listing conversations.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore provides an in-memory implementation of the Store interface. It is thread-safe and suitable for development, testing, and single-instance deployments. For distributed systems, use RedisStore or a database-backed implementation.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore creates a new in-memory state store.
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(ctx context.Context, id string) error
Delete removes a conversation state by ID.
func (*MemoryStore) Fork ¶ added in v1.1.6
func (s *MemoryStore) Fork(ctx context.Context, sourceID, newID string) error
Fork creates a copy of an existing conversation state with a new ID.
func (*MemoryStore) List ¶
func (s *MemoryStore) List(ctx context.Context, opts ListOptions) ([]string, error)
List returns conversation IDs matching the given criteria.
func (*MemoryStore) Load ¶
func (s *MemoryStore) Load(ctx context.Context, id string) (*ConversationState, error)
Load retrieves a conversation state by ID. Returns a deep copy to prevent external mutations.
func (*MemoryStore) Save ¶
func (s *MemoryStore) Save(ctx context.Context, state *ConversationState) error
Save persists a conversation state. If it already exists, it will be updated.
type RedisOption ¶
type RedisOption func(*RedisStore)
RedisOption configures a RedisStore.
func WithPrefix ¶
func WithPrefix(prefix string) RedisOption
WithPrefix sets the key prefix for Redis keys. Default is "promptkit".
func WithTTL ¶
func WithTTL(ttl time.Duration) RedisOption
WithTTL sets the time-to-live for conversation states. After this duration, conversations will be automatically deleted. Default is 24 hours. Set to 0 for no expiration.
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore provides a Redis-backed implementation of the Store interface. It uses JSON serialization for state storage and supports automatic TTL-based cleanup. This implementation is suitable for distributed systems and production deployments.
func NewRedisStore ¶
func NewRedisStore(client *redis.Client, opts ...RedisOption) *RedisStore
NewRedisStore creates a new Redis-backed state store.
Example:
store := NewRedisStore(
redis.NewClient(&redis.Options{Addr: "localhost:6379"}),
WithTTL(24 * time.Hour),
WithPrefix("myapp"),
)
func (*RedisStore) Delete ¶
func (s *RedisStore) Delete(ctx context.Context, id string) error
Delete removes a conversation state from Redis.
func (*RedisStore) Fork ¶ added in v1.1.6
func (s *RedisStore) Fork(ctx context.Context, sourceID, newID string) error
Fork creates a copy of an existing conversation state with a new ID.
func (*RedisStore) List ¶
func (s *RedisStore) List(ctx context.Context, opts ListOptions) ([]string, error)
List returns conversation IDs matching the given criteria.
func (*RedisStore) Load ¶
func (s *RedisStore) Load(ctx context.Context, id string) (*ConversationState, error)
Load retrieves a conversation state by ID from Redis.
func (*RedisStore) Save ¶
func (s *RedisStore) Save(ctx context.Context, state *ConversationState) error
Save persists a conversation state to Redis with TTL.
type Store ¶
type Store interface {
// Load retrieves conversation state by ID
Load(ctx context.Context, id string) (*ConversationState, error)
// Save persists conversation state
Save(ctx context.Context, state *ConversationState) error
// Fork creates a copy of an existing conversation state with a new ID
// The original conversation is left unchanged. Returns ErrNotFound if sourceID doesn't exist.
Fork(ctx context.Context, sourceID, newID string) error
}
Store defines the interface for persistent conversation state storage.
type Summary ¶
type Summary struct {
StartTurn int // First turn included in this summary
EndTurn int // Last turn included in this summary
Content string // Summarized content
TokenCount int // Token count of the summary
CreatedAt time.Time // When this summary was created
}
Summary represents a compressed version of conversation turns. Used to maintain context while reducing token count for older conversations.