-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
232 lines (204 loc) · 5.04 KB
/
Copy pathnode.go
File metadata and controls
232 lines (204 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package contexting
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
type Node struct {
FullPath string `json:"full_path"`
Type string `json:"type"`
Synonyms []string `json:"synonyms,omitempty"`
Symbols []string `json:"symbols,omitempty"`
ModTime int64 `json:"mod_time,omitempty"`
Children map[string]*Node `json:"children,omitempty"`
}
const MaxFileCount = 10000 // Safety limit to prevent crashes on large repos
type IndexStats struct {
TotalNodes int `json:"total_nodes"`
TotalFiles int `json:"total_files"`
TotalDirs int `json:"total_dirs"`
SynonymNodes int `json:"synonym_nodes"`
CollectedNames int `json:"collected_names"`
}
func BuildTree(rootPath string, ignored map[string]bool) (*Node, error) {
absRoot, err := filepath.Abs(rootPath)
if err != nil {
return nil, fmt.Errorf("resolve root path: %w", err)
}
root := &Node{
FullPath: absRoot,
Type: "directory",
Children: make(map[string]*Node),
}
fileCount := 0
err = filepath.WalkDir(absRoot, func(path string, d os.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
rel, err := filepath.Rel(absRoot, path)
if err != nil {
return err
}
if rel == "." {
if !d.IsDir() {
return fmt.Errorf("project root must be a directory: %s", absRoot)
}
return nil
}
if d.Type()&os.ModeSymlink != 0 {
return nil
}
if shouldIgnorePath(rel, d.Name(), ignored) {
if d.IsDir() {
return filepath.SkipDir
}
return nil
}
parent := root
parentRel := filepath.Dir(rel)
if parentRel != "." {
parts := strings.Split(parentRel, string(os.PathSeparator))
for _, part := range parts {
next, ok := parent.Children[part]
if !ok {
return fmt.Errorf("missing parent node for %q", path)
}
parent = next
}
}
nodeType := "file"
if d.IsDir() {
nodeType = "directory"
} else {
fileCount++
if fileCount > MaxFileCount {
return fmt.Errorf("project exceeds %d files; add ignore patterns before indexing", MaxFileCount)
}
if fileCount == MaxFileCount/2 {
LogWarnf("Large repository detected (%d files). Consider adding more ignore patterns.", fileCount)
}
}
name := d.Name()
parent.Children[name] = &Node{
FullPath: path,
Type: nodeType,
Children: make(map[string]*Node),
}
return nil
})
if err != nil {
return nil, err
}
return root, nil
}
func pathSuffix(fullPath string) string {
dir := filepath.Dir(fullPath)
base := filepath.Base(fullPath)
parent := filepath.Base(dir)
// For root-level files, parent is the project directory name (still useful context).
// For deeply nested files, parentDir/basename is usually unique enough.
return parent + "/" + base
}
// llmSynonymKey returns the key used for LLM synonym generation and lookup.
// Files use parentDir/basename (pathSuffix); directories use just basename.
// Must match CollectNamesForLLM's key logic.
func llmSynonymKey(node *Node) string {
if node.Type == "file" {
return pathSuffix(node.FullPath)
}
return filepath.Base(node.FullPath)
}
func CollectNamesForLLM(tree *Node) []string {
if tree == nil {
return nil
}
seen := make(map[string]struct{})
var names []string
walkTree(tree, func(node *Node) {
if node == tree {
return
}
name := llmSynonymKey(node)
if _, ok := seen[name]; ok {
return
}
seen[name] = struct{}{}
names = append(names, name)
})
sort.Strings(names)
return names
}
func AssignSynonymsToTree(tree *Node, synonyms SynonymResponse, maxPerNode int) {
if tree == nil || len(synonyms) == 0 {
walkTree(tree, func(node *Node) {
if node == tree {
return
}
name := llmSynonymKey(node)
node.Synonyms = sanitizeSynonyms(lexicalSynonyms(name), maxPerNode)
})
return
}
walkTree(tree, func(node *Node) {
if node == tree {
return
}
name := llmSynonymKey(node)
combined := make([]string, 0, maxPerNode+4)
if syns, ok := synonyms[name]; ok {
combined = append(combined, syns...)
}
combined = append(combined, lexicalSynonyms(name)...)
node.Synonyms = sanitizeSynonyms(combined, maxPerNode)
})
}
func ComputeStats(tree *Node) IndexStats {
stats := IndexStats{}
if tree == nil {
return stats
}
walkTree(tree, func(node *Node) {
stats.TotalNodes++
if node.Type == "directory" {
stats.TotalDirs++
} else {
stats.TotalFiles++
}
if len(node.Synonyms) > 0 {
stats.SynonymNodes++
}
})
return stats
}
func walkTree(node *Node, fn func(*Node)) {
if node == nil {
return
}
fn(node)
keys := make([]string, 0, len(node.Children))
for name := range node.Children {
keys = append(keys, name)
}
sort.Strings(keys)
for _, key := range keys {
walkTree(node.Children[key], fn)
}
}
func dedupeStrings(values []string) []string {
seen := make(map[string]struct{}, len(values))
out := make([]string, 0, len(values))
for _, value := range values {
normalized := strings.TrimSpace(value)
if normalized == "" {
continue
}
if _, ok := seen[normalized]; ok {
continue
}
seen[normalized] = struct{}{}
out = append(out, normalized)
}
return out
}