OnAgentStart Agent 启动时加载记忆
(ctx context.Context, agentID string)
| 87 | |
| 88 | // OnAgentStart Agent 启动时加载记忆 |
| 89 | func (m *AgentMemoryMiddleware) OnAgentStart(ctx context.Context, agentID string) error { |
| 90 | // 仅在首次加载 |
| 91 | if m.memoryLoaded { |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | amLog.Debug(ctx, "loading agent memory", map[string]any{"path": AGENT_MEMORY_FILE_PATH}) |
| 96 | |
| 97 | // 从 backend 读取 agent.md |
| 98 | content, err := m.backend.Read(ctx, AGENT_MEMORY_FILE_PATH, 0, 0) |
| 99 | if err != nil { |
| 100 | // 文件不存在时,记录警告但不返回错误 |
| 101 | amLog.Warn(ctx, "failed to load agent memory, using empty", map[string]any{"error": err.Error()}) |
| 102 | m.memoryContent = "" |
| 103 | } else { |
| 104 | m.memoryContent = content |
| 105 | amLog.Info(ctx, "agent memory loaded", map[string]any{"chars": len(content)}) |
| 106 | } |
| 107 | |
| 108 | m.memoryLoaded = true |
| 109 | return nil |
| 110 | } |
| 111 | |
| 112 | // WrapModelCall 包装模型调用,注入记忆到 system prompt |
| 113 | func (m *AgentMemoryMiddleware) WrapModelCall(ctx context.Context, req *ModelRequest, handler ModelCallHandler) (*ModelResponse, error) { |