Get 获取 Working Memory 内容 根据配置的 scope 自动选择读取路径: - thread scope: /working_memory/threads/ .json - resource scope: /working_memory/resources/ .json
(ctx context.Context, threadID, resourceID string)
| 107 | // - thread scope: /working_memory/threads/<threadID>.json |
| 108 | // - resource scope: /working_memory/resources/<resourceID>.json |
| 109 | func (wm *WorkingMemoryManager) Get(ctx context.Context, threadID, resourceID string) (string, error) { |
| 110 | if threadID == "" && resourceID == "" { |
| 111 | return "", errors.New("threadID and resourceID cannot both be empty") |
| 112 | } |
| 113 | |
| 114 | path := wm.resolvePath(threadID, resourceID) |
| 115 | |
| 116 | content, err := wm.backend.Read(ctx, path, 0, 0) |
| 117 | if err != nil { |
| 118 | // 文件不存在时返回空字符串,不报错 |
| 119 | if strings.Contains(strings.ToLower(err.Error()), "not found") { |
| 120 | return "", nil |
| 121 | } |
| 122 | return "", fmt.Errorf("read working memory: %w", err) |
| 123 | } |
| 124 | |
| 125 | // 解析 JSON |
| 126 | var data WorkingMemoryData |
| 127 | if err := json.Unmarshal([]byte(content), &data); err != nil { |
| 128 | return "", fmt.Errorf("parse working memory: %w", err) |
| 129 | } |
| 130 | |
| 131 | // 检查是否过期 |
| 132 | if data.Meta.ExpiresAt != nil && time.Now().After(*data.Meta.ExpiresAt) { |
| 133 | return "", nil // 已过期,返回空 |
| 134 | } |
| 135 | |
| 136 | return data.Content, nil |
| 137 | } |
| 138 | |
| 139 | // Update 更新 Working Memory 内容 |
| 140 | // content: 新的内容(Markdown 或 JSON 字符串) |