(ctx context.Context, input map[string]any, tc *tools.ToolContext)
| 216 | } |
| 217 | |
| 218 | func (t *MemoryWriteTool) Execute(ctx context.Context, input map[string]any, tc *tools.ToolContext) (any, error) { |
| 219 | file, _ := input["file"].(string) |
| 220 | content, _ := input["content"].(string) |
| 221 | mode, _ := input["mode"].(string) |
| 222 | title, _ := input["title"].(string) |
| 223 | rawNamespace, _ := input["namespace"].(string) |
| 224 | |
| 225 | if mode == "" { |
| 226 | mode = "append" |
| 227 | } |
| 228 | |
| 229 | // 组合基础命名空间 + 调用命名空间 |
| 230 | // 规则: |
| 231 | // - 如果 namespace 以 "/" 开头, 视为全局命名空间, 不叠加 baseNamespace |
| 232 | // - 否则在 baseNamespace 下叠加 |
| 233 | ns := strings.TrimSpace(rawNamespace) |
| 234 | if after, found := strings.CutPrefix(ns, "/"); found { |
| 235 | ns = after |
| 236 | } else if t.baseNamespace != "" { |
| 237 | if ns != "" { |
| 238 | ns = filepath.ToSlash(filepath.Join(t.baseNamespace, ns)) |
| 239 | } else { |
| 240 | ns = t.baseNamespace |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // 将命名空间与 file 组合成相对 MemoryPath 的完整路径 |
| 245 | combinedFile := strings.TrimSpace(file) |
| 246 | if ns != "" { |
| 247 | combinedFile = filepath.ToSlash(filepath.Join(ns, combinedFile)) |
| 248 | } |
| 249 | |
| 250 | switch mode { |
| 251 | case "append": |
| 252 | path, err := t.manager.AppendNote(ctx, combinedFile, title, content) |
| 253 | if err != nil { |
| 254 | return map[string]any{ |
| 255 | "ok": false, |
| 256 | "error": fmt.Sprintf("append note failed: %v", err), |
| 257 | }, nil |
| 258 | } |
| 259 | |
| 260 | return map[string]any{ |
| 261 | "ok": true, |
| 262 | "mode": "append", |
| 263 | "path": path, |
| 264 | "namespace": ns, |
| 265 | "memory_path": t.manager.MemoryPath(), |
| 266 | "message": "Note appended to memory file successfully.", |
| 267 | }, nil |
| 268 | |
| 269 | case "overwrite": |
| 270 | path, err := t.manager.OverwriteWithNote(ctx, combinedFile, title, content) |
| 271 | if err != nil { |
| 272 | return map[string]any{ |
| 273 | "ok": false, |
| 274 | "error": fmt.Sprintf("overwrite note failed: %v", err), |
| 275 | }, nil |
nothing calls this directly
no test coverage detected