NewAgentMemoryMiddleware 创建中间件
(config *AgentMemoryMiddlewareConfig)
| 46 | |
| 47 | // NewAgentMemoryMiddleware 创建中间件 |
| 48 | func NewAgentMemoryMiddleware(config *AgentMemoryMiddlewareConfig) (*AgentMemoryMiddleware, error) { |
| 49 | if config == nil { |
| 50 | return nil, errors.New("agent memory not configured properly") |
| 51 | } |
| 52 | |
| 53 | if config.Backend == nil { |
| 54 | return nil, errors.New("backend is required") |
| 55 | } |
| 56 | |
| 57 | if config.MemoryPath == "" { |
| 58 | config.MemoryPath = "/memories/" |
| 59 | } |
| 60 | |
| 61 | if config.SystemPromptTemplate == "" { |
| 62 | config.SystemPromptTemplate = "<agent_memory>\n%s\n</agent_memory>" |
| 63 | } |
| 64 | |
| 65 | m := &AgentMemoryMiddleware{ |
| 66 | BaseMiddleware: NewBaseMiddleware("agent_memory", 5), // 高优先级,早期执行 |
| 67 | backend: config.Backend, |
| 68 | memoryPath: config.MemoryPath, |
| 69 | systemPromptTemplate: config.SystemPromptTemplate, |
| 70 | baseNamespace: config.BaseNamespace, |
| 71 | memoryLoaded: false, |
| 72 | memoryContent: "", |
| 73 | } |
| 74 | |
| 75 | // 创建基于 backend 的高级记忆工具 (搜索 / 写入等) |
| 76 | m.memoryTools = m.createMemoryTools() |
| 77 | |
| 78 | amLog.Info(context.Background(), "initialized", map[string]any{"memory_path": config.MemoryPath}) |
| 79 | return m, nil |
| 80 | } |
| 81 | |
| 82 | // Tools 返回由 AgentMemoryMiddleware 注入的长期记忆相关工具 |
| 83 | // 这些工具专门针对 memoryPath 下的记忆文件,提供搜索与写入能力 |