Documentation
¶
Overview ¶
Package store provides pluggable state management for gains workflows and agents.
The package offers two main types:
- Store: A generic key-value store with map[string]any semantics
- MessageStore: A specialized store for conversation history
All types support pluggable persistence through the Adapter interface, with a default in-memory implementation provided via MemoryAdapter.
Basic Usage ¶
Create a store with the default in-memory adapter:
s := store.New(nil)
s.Set("name", "Alice")
s.Set("count", 42)
name := s.GetString("name") // "Alice"
count := s.GetInt("count") // 42
Message Store ¶
Use MessageStore for conversation history:
history := store.NewMessageStore(nil)
history.Append(gains.Message{Role: gains.RoleUser, Content: "Hello"})
msgs := history.Messages() // Get all messages
Persistence ¶
Persist state by calling Sync, reload with Reload:
s := store.New(myAdapter)
s.Set("key", "value")
// Persist to adapter
if err := s.Sync(ctx); err != nil {
log.Fatal(err)
}
// Later, reload from adapter
if err := s.Reload(ctx); err != nil {
log.Fatal(err)
}
Custom Adapters ¶
Implement the Adapter interface for custom persistence:
type RedisAdapter struct { ... }
func (r *RedisAdapter) Get(ctx context.Context, key string) (json.RawMessage, bool, error) { ... }
func (r *RedisAdapter) Set(ctx context.Context, key string, value json.RawMessage) error { ... }
// ... implement remaining methods
s := store.New(&RedisAdapter{})
Index ¶
- Variables
- type Adapter
- type MemoryAdapter
- func (m *MemoryAdapter) Clear(_ context.Context) error
- func (m *MemoryAdapter) Delete(_ context.Context, key string) error
- func (m *MemoryAdapter) Get(_ context.Context, key string) (json.RawMessage, bool, error)
- func (m *MemoryAdapter) Has(_ context.Context, key string) (bool, error)
- func (m *MemoryAdapter) Keys(_ context.Context) ([]string, error)
- func (m *MemoryAdapter) Len(_ context.Context) (int, error)
- func (m *MemoryAdapter) Load(_ context.Context) (map[string]json.RawMessage, error)
- func (m *MemoryAdapter) Save(_ context.Context, data map[string]json.RawMessage) error
- func (m *MemoryAdapter) Set(_ context.Context, key string, value json.RawMessage) error
- type MessageStore
- func (m *MessageStore) Adapter() Adapter
- func (m *MessageStore) Append(msgs ...ai.Message)
- func (m *MessageStore) Clear()
- func (m *MessageStore) Clone() *MessageStore
- func (m *MessageStore) Last(n int) []ai.Message
- func (m *MessageStore) Len() int
- func (m *MessageStore) Messages() []ai.Message
- func (m *MessageStore) Reload(ctx context.Context, key string) error
- func (m *MessageStore) Sync(ctx context.Context, key string) error
- type SerializationError
- type Store
- func (s *Store) Adapter() Adapter
- func (s *Store) Clone() *Store
- func (s *Store) Data() map[string]any
- func (s *Store) Delete(key string)
- func (s *Store) Get(key string) (any, bool)
- func (s *Store) GetBool(key string) bool
- func (s *Store) GetFloat(key string) float64
- func (s *Store) GetInt(key string) int
- func (s *Store) GetString(key string) string
- func (s *Store) Has(key string) bool
- func (s *Store) Keys() []string
- func (s *Store) Len() int
- func (s *Store) Merge(other *Store)
- func (s *Store) Reload(ctx context.Context) error
- func (s *Store) Set(key string, value any)
- func (s *Store) Sync(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrKeyNotFound indicates the requested key does not exist. ErrKeyNotFound = errors.New("store: key not found") // ErrAdapterClosed indicates the adapter has been closed. ErrAdapterClosed = errors.New("store: adapter closed") )
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter interface {
// Get retrieves a value by key. Returns nil, false, nil if not found.
Get(ctx context.Context, key string) (json.RawMessage, bool, error)
// Set stores a value by key.
Set(ctx context.Context, key string, value json.RawMessage) error
// Delete removes a key. No error if key doesn't exist.
Delete(ctx context.Context, key string) error
// Has returns true if the key exists.
Has(ctx context.Context, key string) (bool, error)
// Keys returns all keys.
Keys(ctx context.Context) ([]string, error)
// Len returns the number of stored keys.
Len(ctx context.Context) (int, error)
// Clear removes all data.
Clear(ctx context.Context) error
// Load retrieves all data as a map.
Load(ctx context.Context) (map[string]json.RawMessage, error)
// Save stores all data from a map, replacing existing data.
Save(ctx context.Context, data map[string]json.RawMessage) error
}
Adapter defines the interface for persistence backends. Implementations must be thread-safe.
type MemoryAdapter ¶
type MemoryAdapter struct {
// contains filtered or unexported fields
}
MemoryAdapter provides thread-safe in-memory storage.
func NewMemoryAdapter ¶
func NewMemoryAdapter() *MemoryAdapter
NewMemoryAdapter creates a new in-memory adapter.
func (*MemoryAdapter) Clear ¶
func (m *MemoryAdapter) Clear(_ context.Context) error
Clear removes all data.
func (*MemoryAdapter) Delete ¶
func (m *MemoryAdapter) Delete(_ context.Context, key string) error
Delete removes a key.
func (*MemoryAdapter) Get ¶
func (m *MemoryAdapter) Get(_ context.Context, key string) (json.RawMessage, bool, error)
Get retrieves a value by key.
func (*MemoryAdapter) Keys ¶
func (m *MemoryAdapter) Keys(_ context.Context) ([]string, error)
Keys returns all keys.
func (*MemoryAdapter) Len ¶
func (m *MemoryAdapter) Len(_ context.Context) (int, error)
Len returns the number of stored keys.
func (*MemoryAdapter) Load ¶
func (m *MemoryAdapter) Load(_ context.Context) (map[string]json.RawMessage, error)
Load retrieves all data as a map.
func (*MemoryAdapter) Save ¶
func (m *MemoryAdapter) Save(_ context.Context, data map[string]json.RawMessage) error
Save stores all data from a map, replacing existing data.
func (*MemoryAdapter) Set ¶
func (m *MemoryAdapter) Set(_ context.Context, key string, value json.RawMessage) error
Set stores a value by key.
type MessageStore ¶
type MessageStore struct {
// contains filtered or unexported fields
}
MessageStore manages conversation history with persistence support.
func NewMessageStore ¶
func NewMessageStore(adapter Adapter) *MessageStore
NewMessageStore creates a new MessageStore with the given adapter. If adapter is nil, a default in-memory adapter is used.
func NewMessageStoreFrom ¶
func NewMessageStoreFrom(messages []ai.Message, adapter Adapter) *MessageStore
NewMessageStoreFrom creates a MessageStore initialized with existing messages.
func (*MessageStore) Adapter ¶
func (m *MessageStore) Adapter() Adapter
Adapter returns the underlying adapter.
func (*MessageStore) Append ¶
func (m *MessageStore) Append(msgs ...ai.Message)
Append adds messages to the store.
func (*MessageStore) Clone ¶
func (m *MessageStore) Clone() *MessageStore
Clone creates a deep copy of the MessageStore.
func (*MessageStore) Last ¶
func (m *MessageStore) Last(n int) []ai.Message
Last returns the last n messages. If n > Len(), returns all messages.
func (*MessageStore) Messages ¶
func (m *MessageStore) Messages() []ai.Message
Messages returns a copy of all messages.
type SerializationError ¶
SerializationError wraps JSON marshaling/unmarshaling errors with context.
func (*SerializationError) Error ¶
func (e *SerializationError) Error() string
func (*SerializationError) Unwrap ¶
func (e *SerializationError) Unwrap() error
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store provides thread-safe key-value state management with pluggable persistence.
func New ¶
New creates a new Store with the given adapter. If adapter is nil, a default in-memory adapter is used.
func (*Store) GetFloat ¶
GetFloat retrieves a float64 value. Returns 0.0 if not found or wrong type.
func (*Store) GetInt ¶
GetInt retrieves an int value. Returns 0 if not found or wrong type. Handles float64 from JSON unmarshaling.
func (*Store) GetString ¶
GetString retrieves a string value. Returns empty string if not found or wrong type.