store

package
v0.21.167 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func GetTenantAndChatID(ctx context.Context) (string, string, error)

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.

func (*ChatInfo) Clone added in v0.18.137

func (c *ChatInfo) Clone() *ChatInfo

Clone returns a copy of the chat metadata WITHOUT its messages, for returning chat summaries without copying a long transcript.

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.

Jump to

Keyboard shortcuts

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