NewWorkingMemoryMiddleware 创建 Working Memory 中间件
(config *WorkingMemoryMiddlewareConfig)
| 42 | |
| 43 | // NewWorkingMemoryMiddleware 创建 Working Memory 中间件 |
| 44 | func NewWorkingMemoryMiddleware(config *WorkingMemoryMiddlewareConfig) (*WorkingMemoryMiddleware, error) { |
| 45 | if config == nil { |
| 46 | return nil, errors.New("working memory not configured properly") |
| 47 | } |
| 48 | |
| 49 | if config.Backend == nil { |
| 50 | return nil, errors.New("backend is required") |
| 51 | } |
| 52 | |
| 53 | // 创建 Working Memory 管理器 |
| 54 | manager, err := memory.NewWorkingMemoryManager(&memory.WorkingMemoryConfig{ |
| 55 | Backend: config.Backend, |
| 56 | BasePath: config.BasePath, |
| 57 | Scope: config.Scope, |
| 58 | Schema: config.Schema, |
| 59 | Template: config.Template, |
| 60 | DefaultTTL: config.TTL, |
| 61 | }) |
| 62 | if err != nil { |
| 63 | return nil, fmt.Errorf("create working memory manager: %w", err) |
| 64 | } |
| 65 | |
| 66 | systemPromptTemplate := config.SystemPromptTemplate |
| 67 | if systemPromptTemplate == "" { |
| 68 | systemPromptTemplate = "<working_memory>\n%s\n</working_memory>" |
| 69 | } |
| 70 | |
| 71 | m := &WorkingMemoryMiddleware{ |
| 72 | BaseMiddleware: NewBaseMiddleware("working_memory", 6), // 比 agent_memory 稍低优先级 |
| 73 | manager: manager, |
| 74 | systemPromptTemplate: systemPromptTemplate, |
| 75 | experimental: config.Experimental, |
| 76 | } |
| 77 | |
| 78 | // 创建 Working Memory 工具 |
| 79 | m.workingMemoryTools = m.createWorkingMemoryTools() |
| 80 | |
| 81 | wmLog.Info(context.Background(), "initialized", map[string]any{"scope": config.Scope, "base_path": config.BasePath}) |
| 82 | return m, nil |
| 83 | } |
| 84 | |
| 85 | // Tools 返回 Working Memory 相关工具 |
| 86 | func (m *WorkingMemoryMiddleware) Tools() []tools.Tool { |
no test coverage detected