| 420 | } |
| 421 | |
| 422 | func (t *LogicMemoryUpdateTool) record(ctx context.Context, namespace, key string, input map[string]any) (string, error) { |
| 423 | // 获取必要字段 |
| 424 | memType, _ := input["type"].(string) |
| 425 | if memType == "" { |
| 426 | memType = "preference" |
| 427 | } |
| 428 | |
| 429 | scopeStr, _ := input["scope"].(string) |
| 430 | if scopeStr == "" { |
| 431 | scopeStr = "user" |
| 432 | } |
| 433 | scope := logic.MemoryScope(scopeStr) |
| 434 | |
| 435 | description, _ := input["description"].(string) |
| 436 | if description == "" { |
| 437 | return "", errors.New("description is required for recording memories") |
| 438 | } |
| 439 | |
| 440 | value := input["value"] |
| 441 | if value == nil { |
| 442 | value = map[string]any{} |
| 443 | } |
| 444 | |
| 445 | confidence := 0.7 |
| 446 | if conf, ok := input["confidence"].(float64); ok { |
| 447 | confidence = conf |
| 448 | } |
| 449 | |
| 450 | category, _ := input["category"].(string) |
| 451 | |
| 452 | // 创建 Memory |
| 453 | mem := &logic.LogicMemory{ |
| 454 | Namespace: namespace, |
| 455 | Scope: scope, |
| 456 | Type: memType, |
| 457 | Category: category, |
| 458 | Key: key, |
| 459 | Value: value, |
| 460 | Description: description, |
| 461 | Provenance: &memory.MemoryProvenance{ |
| 462 | SourceType: memory.SourceAgent, |
| 463 | Confidence: confidence, |
| 464 | }, |
| 465 | } |
| 466 | |
| 467 | // 记录 Memory |
| 468 | if err := t.manager.RecordMemory(ctx, mem); err != nil { |
| 469 | return "", fmt.Errorf("failed to record memory: %w", err) |
| 470 | } |
| 471 | |
| 472 | return fmt.Sprintf("Successfully recorded memory: **%s** (%s)\n- Type: %s\n- Scope: %s\n- Description: %s\n- Confidence: %.0f%%", |
| 473 | key, namespace, memType, scope, description, confidence*100), nil |
| 474 | } |
| 475 | |
| 476 | func (t *LogicMemoryUpdateTool) delete(ctx context.Context, namespace, key string) (string, error) { |
| 477 | if err := t.manager.DeleteMemory(ctx, namespace, key); err != nil { |