memory

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MemoryKindUser      = "user"
	MemoryKindFeedback  = "feedback"
	MemoryKindProject   = "project"
	MemoryKindReference = "reference"
)

Memory kinds extracted from conversations. Mirrors proto MemoryType.

Variables

This section is empty.

Functions

func FormatMemoryBlock

func FormatMemoryBlock(snapshot string, entryType EntryType) string

FormatMemoryBlock formats a snapshot as a labelled block for the system prompt.

func LoadMemorySection

func LoadMemorySection(store *MemStore, sessionID, userID, agentID string) string

LoadMemorySection returns the formatted memory block for the scopes named in the request context map (session_id / user_id / agent_id).

Types

type AgentMemory

type AgentMemory struct {
	Ulid        string `gorm:"column:ulid;primaryKey;type:varchar(128)" json:"ulid"`
	CreatedAt   int64  `gorm:"column:created_at;type:bigint" json:"created_at"`
	UpdatedAt   int64  `gorm:"column:updated_at;type:bigint" json:"updated_at"`
	DeletedAt   int64  `gorm:"column:deleted_at;type:bigint;index" json:"deleted_at"`
	AgentId     string `gorm:"column:agent_id;type:varchar(128);index" json:"agent_id"`
	UserId      string `gorm:"column:user_id;type:varchar(128);index" json:"user_id"`
	SessionId   string `gorm:"column:session_id;type:varchar(128);index" json:"session_id"`
	EntryType   string `gorm:"column:entry_type;type:varchar(20);index" json:"entry_type"`
	MemoryType  string `gorm:"column:memory_type;type:varchar(50)" json:"memory_type"`
	MemoryKey   string `gorm:"column:memory_key;type:varchar(255);index" json:"memory_key"`
	Name        string `gorm:"column:name;type:varchar(255)" json:"name"`
	Description string `gorm:"column:description;type:varchar(500)" json:"description"`
	Content     string `gorm:"column:content;type:text" json:"content"`
	Keywords    string `gorm:"column:keywords;type:text" json:"keywords"`
	Importance  int    `gorm:"column:importance;type:int;default:1" json:"importance"`
	Source      string `gorm:"column:source;type:varchar(50)" json:"source"`
	SourceMsgId string `gorm:"column:source_msg_id;type:varchar(128)" json:"source_msg_id"`
	ExpiresAt   int64  `gorm:"column:expires_at;type:bigint" json:"expires_at"`
}

AgentMemory is the gorm persistence object for a stored memory. The schema mirrors XiaoQinglong agent-frame's agent_memory table.

func (*AgentMemory) BeforeCreate

func (po *AgentMemory) BeforeCreate(tx *gorm.DB) error

BeforeCreate assigns a ULID-like id and timestamps.

func (AgentMemory) TableName

func (AgentMemory) TableName() string

TableName sets the gorm table name.

type BackgroundReviewer

type BackgroundReviewer struct {
	// contains filtered or unexported fields
}

BackgroundReviewer mines and persists memories after the main response has been produced, so it never competes with the primary task. It mirrors the XiaoQinglong _spawn_background_review pattern.

func NewBackgroundReviewer

func NewBackgroundReviewer(cfg ReviewConfig, store *MemStore) *BackgroundReviewer

NewBackgroundReviewer creates a reviewer bound to a store.

func (*BackgroundReviewer) ReviewIfNeeded

func (r *BackgroundReviewer) ReviewIfNeeded(model eino.ModelConfig, sessionID, userID, agentID, userInput, assistantOutput string)

ReviewIfNeeded launches an async review when the exchange looks worth remembering. It returns immediately.

type EntryType

type EntryType string

EntryType is the memory scope (session / user / agent).

const (
	EntryTypeSession EntryType = "session"
	EntryTypeUser    EntryType = "user"
	EntryTypeAgent   EntryType = "agent"
)

type ExtractedMemory

type ExtractedMemory struct {
	Name        string
	Description string
	Type        string // user | feedback | project | reference
	Content     string
	Importance  int
}

ExtractedMemory is a memory candidate produced by the extractor.

type Extractor

type Extractor struct {
	// contains filtered or unexported fields
}

Extractor extracts structured memories from a conversation turn using an OpenAI-compatible chat completions endpoint.

func NewExtractor

func NewExtractor(model eino.ModelConfig) *Extractor

NewExtractor creates an Extractor from a model config.

func (*Extractor) Extract

func (e *Extractor) Extract(ctx context.Context, userInput, assistantOutput string) ([]ExtractedMemory, error)

Extract returns memories mined from the given user/assistant exchange.

type MemStore

type MemStore struct {
	// contains filtered or unexported fields
}

MemStore persists memories in PostgreSQL and keeps per-scope snapshots in memory for fast system-prompt injection. It is a DB-backed port of the XiaoQinglong runner MemStore.

func NewMemStore

func NewMemStore(db *gorm.DB) *MemStore

NewMemStore creates a DB-backed memory store.

func (*MemStore) Add

func (s *MemStore) Add(ctx context.Context, entry MemoryEntry) error

Add stores a new memory entry after a security scan and duplicate check.

func (*MemStore) AutoMigrate

func (s *MemStore) AutoMigrate() error

AutoMigrate creates/updates the memory tables.

func (*MemStore) GetAgentSnapshot

func (s *MemStore) GetAgentSnapshot(agentID string) string

GetAgentSnapshot returns an agent's frozen snapshot.

func (*MemStore) GetAll

func (s *MemStore) GetAll(ctx context.Context, entryType EntryType, id string) ([]MemoryEntry, error)

GetAll returns all memories for a scope.

func (*MemStore) GetSessionSnapshot

func (s *MemStore) GetSessionSnapshot(sessionID string) string

GetSessionSnapshot returns a session's frozen snapshot.

func (*MemStore) GetUserSnapshot

func (s *MemStore) GetUserSnapshot(userID string) string

GetUserSnapshot returns a user's frozen snapshot.

func (*MemStore) InitializeAgent

func (s *MemStore) InitializeAgent(ctx context.Context, agentID string) error

InitializeAgent loads an agent's memories and freezes its snapshot.

func (*MemStore) InitializeAll

func (s *MemStore) InitializeAll(ctx context.Context, sessionID, userID, agentID string) error

InitializeAll initializes any non-empty scopes.

func (*MemStore) InitializeSession

func (s *MemStore) InitializeSession(ctx context.Context, sessionID string) error

InitializeSession loads a session's memories and freezes its snapshot.

func (*MemStore) InitializeUser

func (s *MemStore) InitializeUser(ctx context.Context, userID string) error

InitializeUser loads a user's memories and freezes its snapshot.

func (*MemStore) Remove

func (s *MemStore) Remove(ctx context.Context, entryType EntryType, id, key string) error

Remove deletes a memory identified by scope + key.

func (*MemStore) Replace

func (s *MemStore) Replace(ctx context.Context, entry MemoryEntry) error

Replace updates an existing memory identified by scope + key.

func (*MemStore) SaveExtracted

func (s *MemStore) SaveExtracted(ctx context.Context, sessionID, userID, agentID string, mems []ExtractedMemory) error

SaveExtracted persists extracted memories, routing them to the right scope. User-typed memories persist to the user scope (cross-session); the rest go to the session scope. Failures on individual entries are ignored so the batch makes best-effort progress.

func (*MemStore) Search

func (s *MemStore) Search(ctx context.Context, entryType EntryType, id, keyword string) ([]MemoryEntry, error)

Search returns memories whose content/key contains keyword.

type MemoryContext

type MemoryContext struct {
	SessionID   string
	UserID      string
	AgentID     string
	SessionSnap string
	UserSnap    string
	AgentSnap   string
}

MemoryContext carries the frozen snapshots for a request's scopes.

func NewMemoryContext

func NewMemoryContext(sessionID, userID, agentID string, store *MemStore) *MemoryContext

NewMemoryContext builds a MemoryContext from the store's frozen snapshots.

func (*MemoryContext) ToPromptBlock

func (m *MemoryContext) ToPromptBlock() string

ToPromptBlock renders all non-empty scope snapshots as a single block.

type MemoryEntry

type MemoryEntry struct {
	Type        EntryType
	ID          string // session_id / user_id / agent_id
	Key         string
	Name        string
	Description string
	MemoryKind  string // user | feedback | project | reference
	Content     string
	Importance  int
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

MemoryEntry is an in-memory view of a stored memory used to build snapshots.

type MemoryIndex

type MemoryIndex struct {
	Ulid       string `gorm:"column:ulid;primaryKey;type:varchar(128)" json:"ulid"`
	MemoryID   string `gorm:"column:memory_id;type:varchar(128);index" json:"memory_id"`
	HookLine   string `gorm:"column:hook_line;type:varchar(500)" json:"hook_line"`
	MemoryType string `gorm:"column:memory_type;type:varchar(50);index" json:"memory_type"`
	AgentId    string `gorm:"column:agent_id;type:varchar(128);index" json:"agent_id"`
	UserId     string `gorm:"column:user_id;type:varchar(128);index" json:"user_id"`
	CreatedAt  int64  `gorm:"column:created_at;type:bigint" json:"created_at"`
	UpdatedAt  int64  `gorm:"column:updated_at;type:bigint" json:"updated_at"`
}

MemoryIndex is a lightweight retrieval index row, replacing the MEMORY.md text index used by the file-based runner.

func (*MemoryIndex) BeforeCreate

func (po *MemoryIndex) BeforeCreate(tx *gorm.DB) error

BeforeCreate assigns an id and timestamps.

func (MemoryIndex) TableName

func (MemoryIndex) TableName() string

TableName sets the gorm table name.

type ReviewConfig

type ReviewConfig struct {
	Enabled   bool
	MaxMemory int
}

ReviewConfig configures the background reviewer.

Jump to

Keyboard shortcuts

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