Documentation
¶
Overview ¶
Package store persists chat messages and chat metadata for agentic flows, with in-memory and Redis backends.
Every MessageStore method derives its keys from the chatmodel.ChatContext on the supplied context via GetTenantAndChatID, which yields "orgID:userID" (or just userID when no org is set) as the tenant plus the chat ID. One store instance therefore serves all tenants safely.
memstore := store.NewMemoryStore()
// or: store.NewRedisStore(redisClient, "myapp")
agent := assistants.NewAssistant[Answer](fac, sysPrompt,
assistants.WithMessageStore(memstore))
How an assistant uses a store ¶
On each run the assistant reads MessageStore.Messages and inserts the result after the system prompt, then appends everything the run produced — the user turn, tool calls, tool responses and the final answer — with a single MessageStore.Add. Use assistants.WithSkipMessageHistory to read without writing, and assistants.WithSkipToolHistory to omit the verbose tool-call pairs, which usually dominate token usage on later turns.
Add must be atomic across its whole variadic set: a partial write can leave a tool call without its tool response, which providers reject.
Chat metadata and housekeeping ¶
MessageStore.UpdateChat merges a title, metadata and tags into the stored ChatInfo; empty arguments leave the existing values untouched. MessageStoreManager adds tenant enumeration and age-based cleanup.
PopulateMemoryStore snapshots the current chat into a fresh in-memory store, which is useful for a speculative run that must not touch durable history.
See Documentation/memory.md for the Redis key layout and history-sizing strategies.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetTenantAndChatID ¶ added in v0.19.141
GetTenantAndChatID derives the store keys from the chatmodel.ChatContext on ctx. The tenant is "orgID:userID", or just the user ID when no org is set. Returns chatmodel.ErrInvalidChatContext when ctx carries no chat context.
Types ¶
type ChatInfo ¶ added in v0.1.7
type ChatInfo struct {
UserID string `json:"user_id"`
ChatID string `json:"chat_id"`
Title string `json:"title"`
Messages []llms.Message `json:"messages"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Metadata map[string]any `json:"metadata"`
Tags []string `json:"tags"`
}
ChatInfo is the stored metadata for one chat, optionally including its messages.
type MessageStore ¶
type MessageStore interface {
// Messages returns the messages for a tenant and chat ID from context.
Messages(ctx context.Context) []llms.Message
// Add adds one or more messages to the chat history for a tenant and chat ID from context.
// Multiple messages are added atomically for better performance and consistency.
Add(ctx context.Context, msgs ...llms.Message) error
// Reset resets the chat history for a tenant and chat ID from context.
Reset(ctx context.Context) error
// UpdateChat creates or updates a chat with the title, and metadata for a tenant and chat ID from context.
// If title is empty, it will not be updated.
// If metadata is nil, it will not be updated, otherwise merged with the existing metadata.
// If tags are empty, it will not be updated, otherwise merged with the existing tags.
UpdateChat(ctx context.Context, title string, metadata map[string]any, tags []string) (*ChatInfo, error)
// ListChatIDs returns a list of chat IDs for a tenant and chat ID from context.
ListChatIDs(ctx context.Context) ([]string, error)
// GetChatInfo returns the chat information for a tenant and chat ID from context.
GetChatInfo(ctx context.Context, id string, withMessages bool) (*ChatInfo, error)
}
MessageStore is an interface for storing and retrieving chat messages. The supplied context must have ChatContext with tenantID and chatID, created by NewChatContext.
func NewMemoryStore ¶
func NewMemoryStore() MessageStore
NewMemoryStore returns a process-local MessageStore, locked per tenant.
func NewRedisStore ¶ added in v0.5.21
func NewRedisStore(client *redis.Client, prefix string) MessageStore
NewRedisStore returns a Redis-backed MessageStore. All keys are namespaced under prefix; see the key layout documented at the top of this file.
func PopulateMemoryStore ¶ added in v0.3.12
func PopulateMemoryStore(ctx context.Context, store MessageStore) (MessageStore, error)
PopulateMemoryStore returns a new in-memory store seeded with the current chat's messages from store, which may be nil. Use it for a speculative run that must not modify durable history.
type MessageStoreManager ¶ added in v0.7.39
type MessageStoreManager interface {
ListTenants(ctx context.Context) ([]string, error)
Cleanup(ctx context.Context, tenantID string, olderThan time.Duration) (uint32, error)
}
MessageStoreManager provides housekeeping across tenants, for retention jobs.
func NewMemoryStoreManager ¶ added in v0.7.39
func NewMemoryStoreManager(store MessageStore) MessageStoreManager
NewMemoryStoreManager returns a manager for a store created by NewMemoryStore.
func NewRedisStoreManager ¶ added in v0.7.39
func NewRedisStoreManager(client *redis.Client, prefix string) MessageStoreManager
NewRedisStoreManager returns a manager over the same keyspace as NewRedisStore.