|
| 1 | +package svc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "path/filepath" |
| 9 | + "time" |
| 10 | + |
| 11 | + goragcore "github.com/DotNetAge/gorag/v2/core" |
| 12 | + goragindexer "github.com/DotNetAge/gorag/v2/indexer" |
| 13 | + "github.com/DotNetAge/mindx/pkg/rpc" |
| 14 | +) |
| 15 | + |
| 16 | +// handleKBCheckRegionHealth checks whether the Region graph for projectDir is |
| 17 | +// complete. Returns one of: |
| 18 | +// |
| 19 | +// { "health": "no_data" } — zero chunks found, nothing to repair |
| 20 | +// { "health": "healthy" } — Region node exists with proper structure |
| 21 | +// { "health": "needs_repair" } — chunks exist but Region node is missing |
| 22 | +func (d *Daemon) handleKBCheckRegionHealth(_ context.Context, params json.RawMessage) (any, error) { |
| 23 | + var p rpc.KBCheckRegionHealthParams |
| 24 | + if err := unmarshalParams(params, &p); err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + if p.ProjectDir == "" { |
| 28 | + return nil, fmt.Errorf("project_dir is required") |
| 29 | + } |
| 30 | + |
| 31 | + if d.graphIndexer == nil { |
| 32 | + return nil, fmt.Errorf("knowledge base not available") |
| 33 | + } |
| 34 | + |
| 35 | + absDir, err := filepath.Abs(p.ProjectDir) |
| 36 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("resolve project dir: %w", err) |
| 38 | + } |
| 39 | + absDir = filepath.Clean(absDir) |
| 40 | + regionID := fmt.Sprintf("%x", sha256.Sum256([]byte(absDir))) |
| 41 | + |
| 42 | + // 1. Query vectorDB for chunks with this region_id |
| 43 | + vectors, _, err := d.graphIndexer.VectorDB().ListFiltered(context.Background(), 0, 1, []goragcore.FilterCondition{ |
| 44 | + {Key: "region_id", Type: "exact", Value: regionID}, |
| 45 | + }) |
| 46 | + if err != nil { |
| 47 | + d.logger.Warn("kb.check_region_health: vectorDB query failed", "error", err) |
| 48 | + return nil, fmt.Errorf("query vectorDB: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + // No chunks at all → no data to repair |
| 52 | + if len(vectors) == 0 { |
| 53 | + return map[string]any{"health": "no_data"}, nil |
| 54 | + } |
| 55 | + |
| 56 | + // 2. Check if the project-level Region node exists in graphDB |
| 57 | + if d.graphStore == nil { |
| 58 | + return map[string]any{"health": "needs_repair"}, nil |
| 59 | + } |
| 60 | + |
| 61 | + allNodes, err := d.graphStore.ListNodes() |
| 62 | + if err != nil { |
| 63 | + d.logger.Warn("kb.check_region_health: listNodes failed", "error", err) |
| 64 | + return map[string]any{"health": "needs_repair"}, nil |
| 65 | + } |
| 66 | + |
| 67 | + projectDirPrefix := absDir |
| 68 | + hasProjectRegion := false |
| 69 | + for _, n := range allNodes { |
| 70 | + isRegion := false |
| 71 | + for _, l := range n.Labels { |
| 72 | + if l == "Region" { |
| 73 | + isRegion = true |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + if !isRegion { |
| 78 | + continue |
| 79 | + } |
| 80 | + dirV, ok := n.GetProperty("dir") |
| 81 | + if !ok { |
| 82 | + continue |
| 83 | + } |
| 84 | + dirStr, _ := dirV.InterfaceValue().(string) |
| 85 | + if dirStr == projectDirPrefix { |
| 86 | + hasProjectRegion = true |
| 87 | + break |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if !hasProjectRegion { |
| 92 | + return map[string]any{"health": "needs_repair"}, nil |
| 93 | + } |
| 94 | + |
| 95 | + return map[string]any{"health": "healthy"}, nil |
| 96 | +} |
| 97 | + |
| 98 | +// --------------------------------------------------------------------------- |
| 99 | +// kb.repair_region — generate Region chunks + graph nodes for projectDir |
| 100 | +// --------------------------------------------------------------------------- |
| 101 | + |
| 102 | +// handleKBRepairRegion calls RegionIndexer.IndexRegion followed by |
| 103 | +// GraphIndexer.AddFile to fill in missing Region-level chunks and graph nodes |
| 104 | +// for the given projectDir. |
| 105 | +func (d *Daemon) handleKBRepairRegion(ctx context.Context, params json.RawMessage) (any, error) { |
| 106 | + var p rpc.KBRepairRegionParams |
| 107 | + if err := unmarshalParams(params, &p); err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + if p.ProjectDir == "" { |
| 111 | + return nil, fmt.Errorf("project_dir is required") |
| 112 | + } |
| 113 | + |
| 114 | + if d.regionIndexer == nil { |
| 115 | + return nil, fmt.Errorf("region indexer not available") |
| 116 | + } |
| 117 | + if d.graphIndexer == nil { |
| 118 | + return nil, fmt.Errorf("knowledge base not available") |
| 119 | + } |
| 120 | + if d.graphStore == nil { |
| 121 | + return nil, fmt.Errorf("graph store not available") |
| 122 | + } |
| 123 | + |
| 124 | + absDir, err := filepath.Abs(p.ProjectDir) |
| 125 | + if err != nil { |
| 126 | + return nil, fmt.Errorf("resolve project dir: %w", err) |
| 127 | + } |
| 128 | + absDir = filepath.Clean(absDir) |
| 129 | + |
| 130 | + d.logger.Info("kb.repair_region: starting", "project_dir", absDir) |
| 131 | + |
| 132 | + // 1. Generate .README.md + Region graph nodes/edges |
| 133 | + result, riErr := d.regionIndexer.IndexRegion(ctx, absDir) |
| 134 | + if riErr != nil { |
| 135 | + d.logger.Error("kb.repair_region: IndexRegion failed", riErr, "project_dir", absDir) |
| 136 | + return nil, fmt.Errorf("index region: %w", riErr) |
| 137 | + } |
| 138 | + if result == nil || result.RegionFilePath == "" { |
| 139 | + d.logger.Info("kb.repair_region: no content to index", "project_dir", absDir) |
| 140 | + return map[string]any{ |
| 141 | + "status": "no_change", |
| 142 | + "message": "no content to index for this directory", |
| 143 | + }, nil |
| 144 | + } |
| 145 | + |
| 146 | + d.logger.Info("kb.repair_region: .README.md generated, now indexing it", |
| 147 | + "path", result.RegionFilePath) |
| 148 | + |
| 149 | + // 2. Index the generated .README.md through GraphIndexer |
| 150 | + regionID := fmt.Sprintf("%x", sha256.Sum256([]byte(absDir))) |
| 151 | + fileCtx := goragindexer.WithRegionID(ctx, regionID) |
| 152 | + |
| 153 | + indexStart := time.Now() |
| 154 | + chunks, idxErr := d.graphIndexer.AddFile(fileCtx, result.RegionFilePath) |
| 155 | + if idxErr != nil { |
| 156 | + d.logger.Error("kb.repair_region: AddFile failed", idxErr, |
| 157 | + "path", result.RegionFilePath) |
| 158 | + return nil, fmt.Errorf("index region file: %w", idxErr) |
| 159 | + } |
| 160 | + |
| 161 | + elapsed := time.Since(indexStart).Milliseconds() |
| 162 | + d.logger.Info("kb.repair_region: completed", |
| 163 | + "project_dir", absDir, |
| 164 | + "chunks", len(chunks), |
| 165 | + "elapsed_ms", elapsed) |
| 166 | + |
| 167 | + return map[string]any{ |
| 168 | + "status": "repaired", |
| 169 | + "chunks": len(chunks), |
| 170 | + "region_file": result.RegionFilePath, |
| 171 | + }, nil |
| 172 | +} |
0 commit comments