-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_cmd.go
460 lines (372 loc) · 10.3 KB
/
index_cmd.go
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package main
import (
"context"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"go.lsp.dev/protocol"
"go.lsp.dev/uri"
)
func init() {
flags := IndexCmd.PersistentFlags()
flags.StringP("file", "f", "", "specify a specific file to index for debugging/information purposes")
flags.BoolP("embed", "e", false, "specify whether or not to generate embeddings for symbols")
}
var IndexCmd = &cobra.Command{
Use: "index",
Short: "Create or update the sage index for a directory",
RunE: func(cmd *cobra.Command, args []string) error {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
config, err := getConfigForWd()
if err != nil {
return err
}
flags := cmd.Flags()
shouldEmbed, err := flags.GetBool("embed")
if err != nil {
return err
}
indexFileOptions := []IndexFileOption{}
if shouldEmbed {
indexFileOptions = append(indexFileOptions, IncludeEmbeddings)
}
numFiles := 0
lsps := map[string]*ChildLanguageServer{}
params := &protocol.InitializeParams{
ProcessID: int32(os.Getpid()),
Capabilities: protocol.ClientCapabilities{},
WorkspaceFolders: []protocol.WorkspaceFolder{
{
URI: string(uri.File(wd)),
Name: "Main workspace",
},
},
}
for languageName, config := range config.Languages {
ls, err := startLsp(config.LanguageServer, nil, params)
if err != nil {
panic(err)
}
fmt.Println("Setting config...")
err = ls.Server.DidChangeConfiguration(context.TODO(), &protocol.DidChangeConfigurationParams{
Settings: map[string]any{},
})
if err != nil {
return err
}
fmt.Println("Config set!")
defer func() {
err = ls.Exit(ls.Context)
if err != nil {
panic(err)
}
}()
lsps[languageName] = ls
}
llm, err := NewLLMClient()
if err != nil {
return err
}
db, err := openDB(wd)
if err != nil {
panic(err)
}
defer db.Close()
insertCh := make(chan *SymbolInfo, 1000)
defer close(insertCh)
filePath, err := flags.GetString("file")
if err != nil {
return err
}
if filePath != "" {
content, err := os.ReadFile(filePath)
if err != nil {
return err
}
relativePath, err := filepath.Rel(wd, filePath)
if err != nil {
return err
}
name, langConfig := config.GetLanguageConfigForFile(relativePath)
if langConfig == nil {
return fmt.Errorf("No language configuration in '%s' matching path '%s'", config.Name(), relativePath)
}
ls := lsps[name]
syms, err := indexFile(wd, filePath, content, config, ls, llm, indexFileOptions...)
if err != nil {
return err
}
bs, _ := json.MarshalIndent(map[string]any{
"symbols": syms,
}, "", "\t")
fmt.Println(string(bs))
return nil
}
numSymbols := 0
resetCounter := 0
var timedOutFiles []string
indexFunc := func(path string) error {
configName, languageConfig := config.GetLanguageConfigForFile(path)
if languageConfig == nil {
return nil
}
ls := lsps[configName]
log.Debug().Str("path", path).Msg("Inspecting file")
content, err := os.ReadFile(path)
if err != nil {
return err
}
hash := md5.Sum(content)
hashStr := fmt.Sprintf("%x", hash)
_, ok, err := db.GetFile(path, hashStr)
if err != nil {
return fmt.Errorf("Error getting file: %w", err)
}
if ok {
// We've already indexed this exact file
log.Info().Str("path", path).Str("hash", hashStr).Msg("Skipping indexed file")
return nil
}
log.Debug().Str("path", path).Str("hash", hashStr).Msg("Indexing file")
tx, err := db.Begin()
if err != nil {
return err
}
defer func() {
err := tx.Commit()
if err != nil {
panic(err)
}
}()
// Clear out any old info for this file
err = tx.DeleteFileInfoByPath(path)
if err != nil {
return fmt.Errorf("Error deleting file: %w", err)
}
fileId, err := tx.InsertFile(path, hashStr)
if err != nil {
return fmt.Errorf("Error inserting file: %w", err)
}
start := time.Now()
syms, err := indexFile(wd, path, content, config, ls, llm, indexFileOptions...)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
timedOutFiles = append(timedOutFiles, path)
} else {
return err
}
}
numSyms := len(syms)
for _, sym := range syms {
start := sym.Location.Range.Start
end := sym.Location.Range.End
_, err := tx.InsertSymbol(fileId,
float64(sym.Kind), sym.Name, sym.RelativePath,
int(start.Line), int(start.Character),
int(end.Line), int(end.Character), sym.Embedding,
)
if err != nil {
return err
}
}
numFiles++
numSymbols += numSyms
resetCounter += numSyms
if resetCounter > 200000 {
resetCounter = 0
ls.Close()
ls, err = startLsp(languageConfig.LanguageServer, nil, params)
if err != nil {
panic(err)
}
fmt.Println("Setting config...")
err = ls.Server.DidChangeConfiguration(context.TODO(), &protocol.DidChangeConfigurationParams{
Settings: map[string]any{},
})
if err != nil {
return err
}
fmt.Println("Config set!")
lsps[configName] = ls
}
fmt.Println(fmt.Sprintf("%d (total %d)", numSyms, numSymbols), "symbols in", time.Since(start))
return nil
}
walkFunc := func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
shortPath, err := filepath.Rel(wd, path)
if err != nil {
return err
}
// This will be a no-op if there are no excludes
for i, exc := range config.compiledExcludes {
if exc.Match(shortPath) {
log.Debug().Str("path", shortPath).Str("pattern", config.Exclude[i]).Msg("Skipping file because of match with excludes")
return nil
}
}
return indexFunc(path)
}
if len(config.Include) > 0 {
// We should walk the includes if they're directories, and parse them if they're strings
for _, include := range config.Include {
matches, err := filepath.Glob(include)
if err != nil {
return err
}
for _, match := range matches {
info, err := os.Stat(match)
if err != nil {
return err
}
absolute, err := filepath.Abs(filepath.Join(wd, match))
if err != nil {
return err
}
if info.IsDir() {
err = filepath.WalkDir(absolute, walkFunc)
if err != nil {
return err
}
} else {
err = indexFunc(absolute)
if err != nil {
return err
}
}
}
}
} else {
// We just walk everything, ignoring excludes if they exist
err = filepath.WalkDir(wd, walkFunc)
}
if len(timedOutFiles) > 0 {
fmt.Println("Timeouts recorded when indexing the following paths:")
for _, path := range timedOutFiles {
fmt.Println("-", path)
}
}
return err
},
}
func getRangeFromFile(text string, locationRange protocol.Range) string {
lines := strings.Split(text, "\n")
var snippetLines []string
start := locationRange.Start
end := locationRange.End
snippetLines = append(snippetLines, lines[start.Line][start.Character:])
for i := start.Line + 1; i < end.Line; i++ {
snippetLines = append(snippetLines, lines[i])
}
snippetLines = append(snippetLines, lines[end.Line][:end.Character])
return strings.Join(snippetLines, "\n")
}
type IndexFileOption int
const (
IncludeEmbeddings IndexFileOption = iota
)
func indexFile(wd, path string, content []byte, config *SagePathConfig, ls *ChildLanguageServer, llm *LLMClient, options ...IndexFileOption) ([]*SymbolInfo, error) {
var shouldEmbed bool
for _, opt := range options {
switch opt {
case IncludeEmbeddings:
shouldEmbed = true
default:
panic("Unhandled indexing option: " + fmt.Sprint(opt))
}
}
ctx, cancel := context.WithTimeout(ls.Context, time.Minute)
defer cancel()
// TODO: make this a config-driven option
// model := "deepseek-coder-v2:16b"
pathUri := uri.File(path)
textDoc := protocol.TextDocumentItem{
URI: pathUri,
LanguageID: protocol.PythonLanguage,
Version: 1,
Text: string(content),
}
relativePath, err := filepath.Rel(wd, path)
if err != nil {
return nil, err
}
err = ls.DidOpen(ctx, &protocol.DidOpenTextDocumentParams{
TextDocument: textDoc,
})
if err != nil {
return nil, err
}
syms, err := ls.DocumentSymbol(ctx, &protocol.DocumentSymbolParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: pathUri,
},
})
if err != nil {
return nil, err
}
err = ls.DidClose(ctx, &protocol.DidCloseTextDocumentParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: textDoc.URI,
},
})
if err != nil {
return nil, err
}
var result []*SymbolInfo
for _, sym := range syms {
info := &protocol.SymbolInformation{}
bs, _ := json.Marshal(sym)
err = json.Unmarshal(bs, info)
if err != nil {
return nil, err
}
if info.ContainerName != "" && info.Kind >= protocol.SymbolKindVariable {
continue
}
calculatedInfo := &SymbolInfo{
SymbolInformation: *info,
RelativePath: relativePath,
}
// fmt.Println("Indexing", info.Kind, info.Name)
if shouldEmbed {
symbolText := getRangeFromFile(string(content), info.Location.Range)
prompt := "\n============= INSTRUCTIONS: ================\nDescribe the following code. Be concise, but descriptive. Use domain-specific terms like function names, class names, etc. Make sure you cover as many edge cases and interesting codepaths/features as possible. Above this message is more code from the same file as this symbol. Your response will be indexed for full-text search. DO NOT reproduce the code with comments. Simply write a short but descriptive paragraph about the code:\n"
prompt += "File: " + path + "\n"
var explanation string
// if len(strings.Split(symbolText, "\n")) > 2 {
// explanation, err = llm.GenerateCompletion(context.TODO(), *config.Models.ExplainCode, textDoc.Text+prompt+symbolText)
// if err != nil {
// return nil, err
// }
// }
prompt = explanation + "\n" + symbolText
// fmt.Fprintln(os.Stderr, prompt)
models, err := config.Models.Get()
if err != nil {
return nil, err
}
embedding, err := llm.GetEmbedding(context.TODO(), *models.Embedding, prompt)
if err != nil {
return nil, err
}
calculatedInfo.Embedding = embedding
} else {
calculatedInfo.Embedding = make([]float64, 768)
}
result = append(result, calculatedInfo)
}
return result, nil
}