Documentation
¶
Overview ¶
Package memory provides a host-side hybrid conversation-memory service for Flow applications. It keeps recent turns in a bounded working store, retrieves scoped long-term memories, builds an LLM prompt, and promotes important or aged turns through backend interfaces.
Typical use:
working, _ := memory.NewRedisWorkingStore(redisClient, memory.RedisConfig{})
service, _ := memory.NewService(memory.Config{}, working, leannAdapter, chatAdapter)
reply, err := service.Reply(ctx, memory.Request{Scope: scope, UserMessage: "..."})
Index ¶
- type CompleteFunc
- type Completer
- type Config
- type ImportanceScoreFunc
- type ImportanceScorer
- type KeywordScorer
- type LongTermStore
- type MemoryRecorddeprecated
- type Message
- type RecallPolicy
- type Record
- type RedisConfig
- type RedisWorkingStore
- func (s *RedisWorkingStore) Append(ctx context.Context, scope Scope, turn Turn) error
- func (s *RedisWorkingStore) Delete(ctx context.Context, scope Scope, turnIDs []string) error
- func (s *RedisWorkingStore) Older(ctx context.Context, scope Scope, before time.Time, limit int) ([]Turn, error)
- func (s *RedisWorkingStore) Recent(ctx context.Context, scope Scope, limit int) ([]Turn, error)
- type Request
- type Response
- type Scope
- type SearchHit
- type Service
- type Turn
- type WorkingStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CompleteFunc ¶
CompleteFunc adapts a function to Completer.
type Config ¶
type Config struct {
SystemPrompt string
RecentTurns int
RecallTopK int
RecallMinScore float64
MaxRecallCharacters int
ImportanceThreshold float64
Keywords []string
Scorer ImportanceScorer
ShouldRecall RecallPolicy
}
Config controls prompt assembly, retrieval, and automatic memory promotion.
type ImportanceScoreFunc ¶
ImportanceScoreFunc adapts a function to ImportanceScorer.
type ImportanceScorer ¶
ImportanceScorer estimates whether a user message merits immediate promotion. The returned score must be in [0, 1].
type KeywordScorer ¶
KeywordScorer gives a configurable score when a message contains any configured retention phrase. It is a transparent baseline; production callers can inject a classifier or model-backed ImportanceScorer instead.
type LongTermStore ¶
type LongTermStore interface {
Search(ctx context.Context, scope Scope, query string, limit int) ([]SearchHit, error)
Upsert(ctx context.Context, record Record) error
}
LongTermStore is the adapter boundary for LEANN or another semantic-memory backend. Search and Upsert must honor the complete Scope; Upsert must be idempotent by record ID.
type MemoryRecord
deprecated
added in
v0.1.1
type MemoryRecord = Record
Deprecated: use Record.
type RecallPolicy ¶
RecallPolicy can skip semantic search for messages that do not need older context.
type Record ¶
type Record struct {
ID string `json:"id"`
Scope Scope `json:"scope"`
Text string `json:"text"`
Score float64 `json:"score"`
SourceTurn string `json:"source_turn"`
CreatedAt time.Time `json:"created_at"`
Reason string `json:"reason"`
}
Record is an idempotently upserted long-term memory. ID is derived from the source turn, so a retried promotion replaces rather than duplicates it.
type RedisConfig ¶
RedisConfig controls Redis key names, the maximum turns retained per scope, and optional expiration. A positive MaxTurns is always enforced.
type RedisWorkingStore ¶
type RedisWorkingStore struct {
// contains filtered or unexported fields
}
RedisWorkingStore stores completed turns in one bounded Redis list per scope.
func NewRedisWorkingStore ¶
func NewRedisWorkingStore(client redis.UniversalClient, config RedisConfig) (*RedisWorkingStore, error)
NewRedisWorkingStore creates a Redis-backed working store. It does not take ownership of the client; the caller remains responsible for closing it.
func (*RedisWorkingStore) Append ¶
Append adds a turn atomically and trims the oldest turns over the configured cap.
func (*RedisWorkingStore) Delete ¶
Delete removes only turns whose IDs are supplied. It preserves concurrent appends and turns that were not successfully promoted.
type Request ¶
type Request struct {
Scope Scope
UserMessage string
SystemPrompt string
Remember bool // explicit user/application request to retain this turn
}
Request is one incoming conversation message.
type Response ¶
type Response struct {
Assistant string
TurnID string
Recalled []SearchHit
Remembered bool
Importance float64
}
Response is the generated answer and memory actions completed for this turn.
type Scope ¶
Scope is the isolation boundary for conversation and durable memory. Backends must use both fields when reading, searching, writing, and deleting.
type SearchHit ¶
type SearchHit struct {
ID string `json:"id"`
Text string `json:"text"`
Score float64 `json:"score,omitempty"`
}
SearchHit is a relevant long-term memory returned for the current query.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service runs the hybrid memory flow around an LLM completion.
func NewService ¶
func NewService(config Config, working WorkingStore, semantic LongTermStore, model Completer) (*Service, error)
NewService validates dependencies and applies conservative defaults.
func (*Service) PromoteOlder ¶
func (s *Service) PromoteOlder(ctx context.Context, scope Scope, before time.Time, limit int) (int, error)
PromoteOlder copies at most limit turns older than before into long-term storage, then removes exactly those turns from working storage. It is safe to retry when an upsert succeeds but deletion fails because records are keyed by source turn ID. A scheduler can call this per active scope in bounded batches.
type Turn ¶
type Turn struct {
ID string `json:"id"`
User string `json:"user"`
Assistant string `json:"assistant"`
CreatedAt time.Time `json:"created_at"`
ImportanceScore float64 `json:"importance_score,omitempty"`
ExplicitRemember bool `json:"explicit_remember,omitempty"`
}
Turn is one completed user/assistant exchange stored in working memory.
type WorkingStore ¶
type WorkingStore interface {
Recent(ctx context.Context, scope Scope, limit int) ([]Turn, error)
Append(ctx context.Context, scope Scope, turn Turn) error
Older(ctx context.Context, scope Scope, before time.Time, limit int) ([]Turn, error)
Delete(ctx context.Context, scope Scope, turnIDs []string) error
}
WorkingStore holds bounded, ordered short-term conversation turns. Recent must return turns oldest-to-newest. Delete removes only the supplied IDs.