MCPcopy Create free account
hub / github.com/astercloud/aster / AddRelation

Method AddRelation

pkg/knowledge/manager.go:556–600  ·  view source on GitHub ↗

AddRelation 添加知识关系

(ctx context.Context, fromID, toID string, relationType RelationType, weight float64, label string)

Source from the content-addressed store, hash-verified

554
555// AddRelation 添加知识关系
556func (m *manager) AddRelation(ctx context.Context, fromID, toID string, relationType RelationType, weight float64, label string) error {
557 m.mu.Lock()
558 defer m.mu.Unlock()
559
560 // 获取知识项
561 fromItem, err := m.getItemFromMemory(ctx, fromID)
562 if err != nil {
563 return fmt.Errorf("knowledge: from item not found: %w", err)
564 }
565
566 // 验证目标项目存在
567 _, err = m.getItemFromMemory(ctx, toID)
568 if err != nil {
569 return fmt.Errorf("knowledge: to item not found: %w", err)
570 }
571
572 // 添加关系
573 relation := KnowledgeRelation{
574 Type: relationType,
575 TargetID: toID,
576 Weight: weight,
577 Label: label,
578 }
579
580 // 检查关系是否已存在
581 for _, existing := range fromItem.Relations {
582 if existing.Type == relationType && existing.TargetID == toID {
583 return errors.New("knowledge: relation already exists")
584 }
585 }
586
587 fromItem.Relations = append(fromItem.Relations, relation)
588
589 // 保存更新
590 if err := m.saveItemToMemory(ctx, fromItem); err != nil {
591 return fmt.Errorf("knowledge: failed to save updated relations: %w", err)
592 }
593
594 // 记录审计
595 if m.config.EnableAudit {
596 m.audit("add_relation", "", fromID, fmt.Sprintf("Added relation to %s: %s", toID, relationType))
597 }
598
599 return nil
600}
601
602// RemoveRelation 移除知识关系
603func (m *manager) RemoveRelation(ctx context.Context, fromID, toID string, relationType RelationType) error {

Callers

nothing calls this directly

Calls 3

getItemFromMemoryMethod · 0.95
saveItemToMemoryMethod · 0.95
auditMethod · 0.95

Tested by

no test coverage detected