Documentation
¶
Index ¶
- Constants
- Variables
- func MustRegisterPlugin(plugin *Plugin)
- func RegisterPlugin(plugin *Plugin) error
- type AsyncMemorizer
- type CleanupConfig
- type ConversationMessage
- type HookEvent
- type HookHandler
- type HookableProvider
- type MemorizeNormalizer
- type MemorizeRequest
- type MemoryConfig
- type MemoryMiddleware
- func (m *MemoryMiddleware) AfterModelRewriteState(ctx context.Context, ...) (context.Context, *adk.TypedChatModelAgentState[*schema.AgenticMessage], error)
- func (m *MemoryMiddleware) BeforeAgent(ctx context.Context, runCtx *adk.ChatModelAgentContext) (context.Context, *adk.ChatModelAgentContext, error)
- func (m *MemoryMiddleware) BeforeModelRewriteState(ctx context.Context, ...) (context.Context, *adk.TypedChatModelAgentState[*schema.AgenticMessage], error)
- type MemoryMiddlewareConfig
- type MemoryProvider
- type MemoryRetrieval
- type MemoryStorage
- type MissingIdentityPolicy
- type Plugin
- type ProviderFactory
- type Registry
- type RetrieveRequest
- type RetrieveResult
- type SearchableProvider
- type SessionSummary
- type SummaryTriggerConfig
- type SummaryTriggerStrategy
- type TaskQueueStats
- type UserMemory
- type UserMemoryAnalyzerParam
- type UserMemoryEventSearcher
Constants ¶
const ( RetrievalLastN = builtin.RetrievalLastN RetrievalFirstN = builtin.RetrievalFirstN RetrievalSemantic = builtin.RetrievalSemantic UserMemoryOpUpdate = builtin.UserMemoryOpUpdate UserMemoryOpNoop = builtin.UserMemoryOpNoop TriggerAlways = builtin.TriggerAlways TriggerByMessages = builtin.TriggerByMessages TriggerByTime = builtin.TriggerByTime TriggerSmart = builtin.TriggerSmart )
Variables ¶
var ( NewMemoryManager = builtin.NewMemoryManager DefaultMemoryConfig = builtin.DefaultMemoryConfig )
Functions ¶
func MustRegisterPlugin ¶ added in v0.2.0
func MustRegisterPlugin(plugin *Plugin)
MustRegisterPlugin registers a plugin with the global registry, panicking on error.
func RegisterPlugin ¶ added in v0.2.0
RegisterPlugin registers a plugin with the global registry.
Types ¶
type AsyncMemorizer ¶ added in v0.3.3
type AsyncMemorizer interface {
// EnqueueMemorize accepts a memorize request for asynchronous execution.
// It must be safe to reject the request after the provider is closed
// (returning an error instead of panicking or blocking).
EnqueueMemorize(ctx context.Context, req *MemorizeRequest) error
}
AsyncMemorizer is an optional extension of MemoryProvider for providers that own a managed async write path (e.g. builtin.MemoryManager's worker pool with Flush/Shutdown lifecycle). When implemented, MemoryMiddleware delegates memorization to EnqueueMemorize instead of spawning an untracked goroutine, so pending writes are drained on shutdown.
type CleanupConfig ¶ added in v0.2.0
type CleanupConfig = builtin.CleanupConfig
type ConversationMessage ¶
type ConversationMessage = builtin.ConversationMessage
type HookEvent ¶ added in v0.2.0
type HookEvent string
HookEvent represents a lifecycle event in the memory provider.
const ( // HookBeforeRetrieve fires before the Retrieve operation. HookBeforeRetrieve HookEvent = "before_retrieve" // HookAfterRetrieve fires after the Retrieve operation. HookAfterRetrieve HookEvent = "after_retrieve" // HookBeforeMemorize fires before the Memorize operation. HookBeforeMemorize HookEvent = "before_memorize" // HookAfterMemorize fires after the Memorize operation. HookAfterMemorize HookEvent = "after_memorize" )
type HookHandler ¶ added in v0.2.0
HookHandler is a callback function invoked when a lifecycle event fires.
type HookableProvider ¶ added in v0.2.0
type HookableProvider interface {
MemoryProvider
// RegisterHook registers a handler for the given lifecycle event.
RegisterHook(event HookEvent, handler HookHandler)
}
HookableProvider is an optional extension of MemoryProvider for providers that support lifecycle hooks.
type MemorizeNormalizer ¶ added in v0.3.3
type MemorizeNormalizer func(ctx context.Context, req *MemorizeRequest) (*MemorizeRequest, error)
MemorizeNormalizer 在消息写入记忆前对请求做清洗/裁剪(如移除图片索引、 发言人标记、runtime context 注入内容)。
约定:
- 入参请求视为不可变,返回新的请求对象,不得原地修改;
- 返回 nil, nil 表示丢弃本次写入;
- 返回错误表示清洗失败,MemoryMiddleware 会记录日志并跳过本次写入。
type MemorizeRequest ¶ added in v0.2.0
type MemorizeRequest struct {
// UserID identifies the user whose conversation to store.
UserID string
// SessionID identifies the conversation session.
SessionID string
// Messages are the conversation turn(s) to store.
Messages []*schema.AgenticMessage
}
MemorizeRequest is the input for the Memorize operation.
type MemoryConfig ¶
type MemoryConfig = builtin.MemoryConfig
type MemoryMiddleware ¶ added in v0.2.0
type MemoryMiddleware struct {
*adk.TypedBaseChatModelAgentMiddleware[*schema.AgenticMessage]
// contains filtered or unexported fields
}
MemoryMiddleware implements adk.ChatModelAgentMiddleware. It delegates to a MemoryProvider for retrieval and memorization.
func NewMemoryMiddleware ¶ added in v0.2.0
func NewMemoryMiddleware(provider MemoryProvider) *MemoryMiddleware
NewMemoryMiddleware creates a MemoryMiddleware with a MemoryProvider. 行为与旧版本兼容:身份缺失时记录一条日志后跳过(MissingIdentityLog)。
func NewMemoryMiddlewareWithConfig ¶ added in v0.3.3
func NewMemoryMiddlewareWithConfig(provider MemoryProvider, config MemoryMiddlewareConfig) *MemoryMiddleware
NewMemoryMiddlewareWithConfig creates a MemoryMiddleware with explicit config.
func (*MemoryMiddleware) AfterModelRewriteState ¶ added in v0.2.0
func (m *MemoryMiddleware) AfterModelRewriteState(ctx context.Context, state *adk.TypedChatModelAgentState[*schema.AgenticMessage], mc *adk.TypedModelContext[*schema.AgenticMessage]) (context.Context, *adk.TypedChatModelAgentState[*schema.AgenticMessage], error)
AfterModelRewriteState stores assistant response after a model call.
func (*MemoryMiddleware) BeforeAgent ¶ added in v0.2.0
func (m *MemoryMiddleware) BeforeAgent(ctx context.Context, runCtx *adk.ChatModelAgentContext) (context.Context, *adk.ChatModelAgentContext, error)
BeforeAgent is called before the agent runs.
func (*MemoryMiddleware) BeforeModelRewriteState ¶ added in v0.2.0
func (m *MemoryMiddleware) BeforeModelRewriteState(ctx context.Context, state *adk.TypedChatModelAgentState[*schema.AgenticMessage], mc *adk.TypedModelContext[*schema.AgenticMessage]) (context.Context, *adk.TypedChatModelAgentState[*schema.AgenticMessage], error)
BeforeModelRewriteState injects memory context before a model call.
type MemoryMiddlewareConfig ¶ added in v0.3.3
type MemoryMiddlewareConfig struct {
// UserIDKey 从会话中读取用户身份的键,默认 "userID",
// 找不到时回退读取 "actorID"。
UserIDKey string
// SessionIDKey 从会话中读取会话 ID 的键,默认 "sessionID"。
SessionIDKey string
// IdentityResolver 覆盖默认的身份解析逻辑
//(例如从 actorID/conversationID 组合出记忆归属),设置后 UserIDKey/SessionIDKey 被忽略。
IdentityResolver func(ctx context.Context) (userID, sessionID string, ok bool)
// MissingIdentity 决定身份缺失时的行为,默认 MissingIdentityLog。
MissingIdentity MissingIdentityPolicy
// Normalizers 在写入前依次作用于 MemorizeRequest(深拷贝后执行)。
Normalizers []MemorizeNormalizer
// MemorizeTimeout 仅在 provider 未实现 AsyncMemorizer 时,
// 用于兼容路径的后台写入超时,默认 2 分钟。
MemorizeTimeout time.Duration
}
MemoryMiddlewareConfig 配置 MemoryMiddleware 的身份解析与写入行为。 零值可用:所有字段都有与旧行为兼容的默认值。
type MemoryProvider ¶ added in v0.2.0
type MemoryProvider interface {
// Retrieve fetches relevant memory context before a model call.
Retrieve(ctx context.Context, req *RetrieveRequest) (*RetrieveResult, error)
// Memorize persists a conversation turn after a model call.
Memorize(ctx context.Context, req *MemorizeRequest) error
// Close releases any resources held by the provider.
Close() error
}
MemoryProvider defines the interface that all memory backends must implement. It provides the core operations for retrieving relevant context before a model call and persisting conversation turns after a model call.
type MemoryRetrieval ¶
type MemoryRetrieval = builtin.MemoryRetrieval
type MemoryStorage ¶
type MemoryStorage = builtin.MemoryStorage
type MissingIdentityPolicy ¶ added in v0.3.3
type MissingIdentityPolicy int
MissingIdentityPolicy 决定 MemoryMiddleware 在会话缺少身份信息时的行为。
const ( // MissingIdentityLog 记录日志后跳过。作为零值默认值, // 避免现有业务在身份缺失时无声失效。 MissingIdentityLog MissingIdentityPolicy = iota // MissingIdentitySkip 静默跳过记忆读写。 MissingIdentitySkip // MissingIdentityError 向上层返回错误,中断本轮请求。 MissingIdentityError )
type Plugin ¶ added in v0.2.0
type Plugin struct {
// ID is the unique identifier for the plugin (e.g. "builtin", "memu", "mem0").
ID string
// Factory creates a new MemoryProvider instance for this plugin.
Factory ProviderFactory
}
Plugin represents a memory backend plugin that can be registered with the registry.
type ProviderFactory ¶ added in v0.2.0
type ProviderFactory func(config any) (MemoryProvider, error)
ProviderFactory creates a new MemoryProvider instance from the given config.
type Registry ¶ added in v0.2.0
type Registry struct {
// contains filtered or unexported fields
}
Registry manages registered memory backend plugins.
func GlobalRegistry ¶ added in v0.2.0
func GlobalRegistry() *Registry
GlobalRegistry returns the global registry instance.
func NewRegistry ¶ added in v0.2.0
func NewRegistry() *Registry
NewRegistry creates a new empty Registry.
func (*Registry) CreateProvider ¶ added in v0.2.0
func (r *Registry) CreateProvider(pluginID string, config any) (MemoryProvider, error)
CreateProvider creates a new MemoryProvider using the plugin identified by pluginID.
func (*Registry) Get ¶ added in v0.2.0
Get returns the plugin with the given ID, or false if not found.
func (*Registry) ListPlugins ¶ added in v0.2.0
ListPlugins returns the IDs of all registered plugins.
func (*Registry) MustRegister ¶ added in v0.2.0
MustRegister registers a plugin and panics if registration fails.
type RetrieveRequest ¶ added in v0.2.0
type RetrieveRequest struct {
// UserID identifies the user whose memory to search.
UserID string
// SessionID identifies the current conversation session.
SessionID string
// Messages are the current conversation messages used as context for retrieval.
Messages []*schema.AgenticMessage
// Limit is the maximum number of memory items to return.
// A value of 0 means the provider should use its own default.
Limit int
}
RetrieveRequest is the input for the Retrieve operation.
type RetrieveResult ¶ added in v0.2.0
type RetrieveResult struct {
// ContextMessages are appended to the current user message as runtime context.
// Providers should use this for user/session-specific retrieved data so the
// base system prompt and conversation history remain cacheable.
ContextMessages []*schema.AgenticMessage
// SystemMessages are legacy context messages.
// Deprecated: use ContextMessages for dynamic memory or retrieval results.
SystemMessages []*schema.AgenticMessage
// HistoryMessages are injected as conversation history.
HistoryMessages []*schema.AgenticMessage
// Warnings records degradations during retrieval (sub-query failures,
// budget truncation, etc.) so callers can surface them in logs/metrics
// instead of silently losing context.
Warnings []string
// Metadata holds provider-specific data.
Metadata map[string]any
}
RetrieveResult is the output of the Retrieve operation.
type SearchableProvider ¶ added in v0.2.6
type SearchableProvider interface {
MemoryProvider
SearchMessages(ctx context.Context, q *search.SearchQuery) ([]*search.SearchHit, error)
}
type SessionSummary ¶
type SessionSummary = builtin.SessionSummary
type SummaryTriggerConfig ¶
type SummaryTriggerConfig = builtin.SummaryTriggerConfig
type SummaryTriggerStrategy ¶
type SummaryTriggerStrategy = builtin.SummaryTriggerStrategy
type TaskQueueStats ¶ added in v0.0.10
type TaskQueueStats = builtin.TaskQueueStats
type UserMemory ¶
type UserMemory = builtin.UserMemory
type UserMemoryAnalyzerParam ¶
type UserMemoryAnalyzerParam = builtin.UserMemoryAnalyzerParam
type UserMemoryEventSearcher ¶ added in v0.3.0
type UserMemoryEventSearcher interface {
SearchUserMemoryEvents(ctx context.Context, query *memoryevent.Query) ([]*memoryevent.Event, error)
ListRecentUserMemoryEvents(ctx context.Context, userID string, limit int) ([]*memoryevent.Event, error)
}
UserMemoryEventSearcher 让 search_user_memory 工具可以独立于具体 provider 实现, 任何能够检索用户事件级记忆的 provider 都可以实现该接口并被工具识别。
参数与返回值使用中性的 memoryevent.Event / memoryevent.Query; memory/builtin 通过 type alias 保持后向兼容。
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package memoryevent defines provider-neutral types for the user-memory event search interface used by aggo's search_user_memory tool.
|
Package memoryevent defines provider-neutral types for the user-memory event search interface used by aggo's search_user_memory tool. |