FindAndReplace 在现有内容中查找并替换(实验性功能,对标 Mastra) searchString: 要查找的字符串 newContent: 替换后的新内容 如果 searchString 为空,则追加到末尾
(ctx context.Context, threadID, resourceID, searchString, newContent string)
| 210 | // newContent: 替换后的新内容 |
| 211 | // 如果 searchString 为空,则追加到末尾 |
| 212 | func (wm *WorkingMemoryManager) FindAndReplace(ctx context.Context, threadID, resourceID, searchString, newContent string) error { |
| 213 | if threadID == "" && resourceID == "" { |
| 214 | return errors.New("threadID and resourceID cannot both be empty") |
| 215 | } |
| 216 | |
| 217 | // 读取现有内容 |
| 218 | existing, err := wm.Get(ctx, threadID, resourceID) |
| 219 | if err != nil { |
| 220 | return fmt.Errorf("read existing content: %w", err) |
| 221 | } |
| 222 | |
| 223 | var updated string |
| 224 | if searchString == "" || existing == "" { |
| 225 | // 追加模式 |
| 226 | if existing == "" { |
| 227 | updated = newContent |
| 228 | } else { |
| 229 | updated = existing + "\n\n" + newContent |
| 230 | } |
| 231 | } else { |
| 232 | // 查找替换模式 |
| 233 | if !strings.Contains(existing, searchString) { |
| 234 | return errors.New("search string not found in working memory") |
| 235 | } |
| 236 | updated = strings.Replace(existing, searchString, newContent, 1) |
| 237 | } |
| 238 | |
| 239 | return wm.Update(ctx, threadID, resourceID, updated) |
| 240 | } |
| 241 | |
| 242 | // Delete 删除 Working Memory |
| 243 | func (wm *WorkingMemoryManager) Delete(ctx context.Context, threadID, resourceID string) error { |