memory

package
v0.101.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 1 Imported by: 0

README

AI Memory Engineering (ai/memory)

memory 包定义了 AI Agent 的记忆扩展接口,旨在实现更高级的记忆管理机制。

架构设计

classDiagram
    class Generator {
        <<interface>>
        +GenerateAsync(ctx, req)
        +GenerateSync(ctx, req) error
        +Shutdown(ctx) error
    }
    class NoOpGenerator {
        +GenerateAsync()
        +GenerateSync()
    }
    class simple.Generator {
        +GenerateAsync()
        +GenerateSync()
        +generateSummary()
        +embedder.Embedding()
    }

    Generator <|.. NoOpGenerator : default implementation
    Generator <|.. simple.Generator : production implementation

接口定义

Generator 接口

定义了记忆生成的标准行为:

  • 异步生成 (GenerateAsync): 用于生产环境,不阻塞主流程
  • 同步生成 (GenerateSync): 用于测试或调试
  • 关闭 (Shutdown): 等待所有待处理任务完成
MemoryRequest
type MemoryRequest struct {
    BlockID   int64
    UserID    int32
    AgentType string
    UserInput string
    Outcome   string // Assistant's response
    Metadata  map[string]any
}

实现版本

NoOpGenerator (默认)

空实现。在未配置外部记忆服务时使用,确保系统稳定性。

simple.Generator (开发/测试)

基于 LLM 的基础记忆生成实现:

  1. 摘要生成: 使用 LLM 将用户输入和助手回复压缩为简洁摘要
  2. 向量化: 使用 Embedding 模型生成向量表示
  3. 持久化: 存储到 episodic_memoryepisodic_memory_embedding
配置
type Config struct {
    Enabled          bool
    SummaryMaxTokens int
    MaxConcurrency  int
    Timeout         time.Duration
}
局限性

警告: 此实现不适合生产环境:

  • 重要性评分固定为 0.5,无动态评估
  • 无遗忘/衰减机制
  • 无记忆合并或去重
  • 无安全保护防止记忆污染
  • 仅支持单维度(向量)检索
生产建议

如需生产级记忆功能,考虑:

记忆工程概念

该包的设计参考了人类记忆机制:

  1. Importance Scoring: 基于相关性、频率和新近度对信息打分
  2. Forgetting: 模拟生物遗忘机制
  3. Consolidation: 将短期记忆合并为长期记忆

目录结构

ai/memory/
├── interface.go           # Generator 接口和 NoOpGenerator
├── simple/
│   └── generator.go       # 基于 LLM 的记忆生成实现
└── (future)
    ├── mem0/             # Mem0 集成
    └── letta/           # Letta 集成

Documentation

Overview

Package memory provides the memory extension point for AI agents.

Memory engineering is a specialized domain involving:

  • Importance scoring (relevance + frequency + recency)
  • Forgetting mechanisms (biologically-inspired decay)
  • Memory consolidation (merging, deduplication, compression)
  • Security (memory poisoning detection)
  • Multi-dimensional retrieval (semantic + temporal + causal)

This package defines the extension interface. Implementations can range from simple (for development/testing) to sophisticated (Mem0, Letta integration).

Reference: docs/architecture/context-engineering.md Phase 3

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator added in v0.100.0

type Generator interface {
	// GenerateAsync starts asynchronous memory generation.
	// This method returns immediately and processes the memory in a goroutine.
	// Implementations SHOULD be non-blocking and handle their own error logging.
	GenerateAsync(ctx context.Context, req MemoryRequest)

	// GenerateSync generates memory synchronously (for testing).
	// This method blocks until memory generation completes or fails.
	GenerateSync(ctx context.Context, req MemoryRequest) error

	// Shutdown waits for all pending memory generation tasks to complete.
	// This should be called during graceful service shutdown.
	Shutdown(ctx context.Context) error
}

Generator defines the memory generation extension point. Implementations can be:

  • NoOpGenerator: No-op implementation (default, production-safe)
  • SimpleGenerator: Basic implementation for dev/test (not production-ready)
  • ExternalGenerator: Integration with Mem0, Letta, or custom services

type MemoryRequest added in v0.100.0

type MemoryRequest struct {
	BlockID   int64
	UserID    int32
	AgentType string
	UserInput string
	Outcome   string // Assistant's response
	Metadata  map[string]any
}

MemoryRequest contains the data needed to generate a memory.

type NoOpGenerator added in v0.100.0

type NoOpGenerator struct{}

NoOpGenerator is a no-op implementation of Generator. It is the default implementation, safe for production use. Use this when memory generation is disabled or not yet configured.

func NewNoOpGenerator added in v0.100.0

func NewNoOpGenerator() *NoOpGenerator

NewNoOpGenerator creates a new no-op memory generator.

func (*NoOpGenerator) GenerateAsync added in v0.100.0

func (n *NoOpGenerator) GenerateAsync(_ context.Context, _ MemoryRequest)

func (*NoOpGenerator) GenerateSync added in v0.100.0

func (n *NoOpGenerator) GenerateSync(_ context.Context, _ MemoryRequest) error

func (*NoOpGenerator) Shutdown added in v0.100.0

func (n *NoOpGenerator) Shutdown(_ context.Context) error

Directories

Path Synopsis
Package simple provides a basic memory generator implementation.
Package simple provides a basic memory generator implementation.

Jump to

Keyboard shortcuts

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