In-memory state store implementation for Forge AI SDK. Perfect for testing, development, and single-instance deployments.
- ✅ Pure Go implementation (no external dependencies)
- ✅ Thread-safe with RWMutex
- ✅ Optional TTL support with automatic cleanup
- ✅ Session management per agent
- ✅ Zero configuration required
- ✅ Observability (logging & metrics)
- ✅ Perfect for unit tests and local development
go get github.com/xraph/ai-sdk/integrations/statestores/memorypackage main
import (
"context"
"log"
"github.com/xraph/ai-sdk/integrations/statestores/memory"
sdk "github.com/xraph/ai-sdk"
)
func main() {
ctx := context.Background()
// Create memory state store
store := memory.NewMemoryStateStore(memory.Config{})
// Save agent state
state := &sdk.AgentState{
AgentID: "agent-1",
SessionID: "session-1",
Context: map[string]any{
"user": "john",
"step": 1,
},
}
if err := store.Save(ctx, state); err != nil {
log.Fatal(err)
}
// Load agent state
loadedState, err := store.Load(ctx, "agent-1", "session-1")
if err != nil {
log.Fatal(err)
}
log.Printf("Loaded state for agent %s\n", loadedState.AgentID)
// List all sessions for an agent
sessions, err := store.List(ctx, "agent-1")
if err != nil {
log.Fatal(err)
}
log.Printf("Agent has %d sessions\n", len(sessions))
}import "time"
// Create store with 1-hour TTL
store := memory.NewMemoryStateStore(memory.Config{
TTL: 1 * time.Hour,
})
// States will automatically expire after 1 hour
_ = store.Save(ctx, state)
// After TTL, Load() will return an error
time.Sleep(61 * time.Minute)
_, err := store.Load(ctx, "agent-1", "session-1")
// err != nil (state expired)func TestMyFeature(t *testing.T) {
store := memory.NewMemoryStateStore(memory.Config{})
defer store.Clear() // Clean up after test
// Run tests...
// Check state count
if store.Count() != expectedCount {
t.Errorf("unexpected state count: %d", store.Count())
}
}type Config struct {
TTL time.Duration // Optional: TTL for states (0 = no expiration)
Logger logger.Logger // Optional: Logger for debugging
Metrics metrics.Metrics // Optional: Metrics for monitoring
}Saves an agent state to memory.
func (m *MemoryStateStore) Save(ctx context.Context, state *sdk.AgentState) errorLoads an agent state from memory.
func (m *MemoryStateStore) Load(ctx context.Context, agentID, sessionID string) (*sdk.AgentState, error)Deletes an agent state.
func (m *MemoryStateStore) Delete(ctx context.Context, agentID, sessionID string) errorLists all session IDs for an agent.
func (m *MemoryStateStore) List(ctx context.Context, agentID string) ([]string, error)Clears all states (useful for testing).
func (m *MemoryStateStore) Clear()Returns the total number of states.
func (m *MemoryStateStore) Count() intgo test ./...
go test -race ./... # Test for race conditions
go test -bench=. ./... # Run benchmarksNOT RECOMMENDED for production distributed systems:
- ❌ State is lost on restart (no persistence)
- ❌ No replication or high availability
- ❌ Limited to single instance memory
- ❌ No cross-process sharing
Suitable for:
- ✅ Unit and integration tests
- ✅ Local development
- ✅ Single-instance applications
- ✅ Prototyping and demos
For production, use:
- Redis StateStore - Distributed, persistent
- PostgreSQL StateStore - Relational, ACID
- DynamoDB StateStore - Serverless, scalable
MIT License - see LICENSE