-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinternal.go
More file actions
347 lines (317 loc) · 9.7 KB
/
internal.go
File metadata and controls
347 lines (317 loc) · 9.7 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"sync/atomic"
)
// analyze is called from investigate where the path is inspected and the resultsChan is written to
func analyze(ext, filePath string) {
defer maxFileSemaphore.Release() // maxFileSemaphore prevents excessive files from being opened
defer wg.Done() // keep the main thread running while this file is being processed
if strings.HasSuffix(filePath, ".DS_Store") ||
strings.HasSuffix(filePath, ".exe") ||
strings.HasSuffix(filePath, "-amd64") ||
strings.HasSuffix(filePath, "-arm64") ||
strings.HasSuffix(filePath, "aarch64") {
return
}
type tFileInfo struct {
Name string `json:"name"`
Size int64 `json:"size"`
Mode os.FileMode `json:"mode"`
}
info, err := os.Stat(filePath)
if err != nil {
errs = append(errs, err)
return
}
fileInfo := &tFileInfo{
Name: filepath.Base(filePath),
Size: info.Size(),
Mode: info.Mode(),
}
infoJson, err := json.MarshalIndent(fileInfo, "", " ")
if err != nil {
errs = append(errs, err)
return
}
var sb bytes.Buffer // capture what we write to file in a bytes buffer
sb.WriteString("## " + filepath.Base(filePath) + "\n\n")
sb.WriteString("The `os.Stat` for the " + filePath + " is: \n\n")
sb.WriteString("```json\n")
sb.WriteString(string(infoJson) + "\n")
sb.WriteString("```\n\n")
sb.WriteString("Source Code:\n\n")
sb.WriteString("```" + ext + "\n")
content, err := os.ReadFile(filePath) // open the file and get its contents
if err != nil {
errs = append(errs, fmt.Errorf("Error reading file %s: %v\n", filePath, err))
return
}
if _, writeErr := sb.Write(content); writeErr != nil {
errs = append(errs, fmt.Errorf("Error writing file %s: %v\n", filePath, err))
return
}
content = []byte{} // clear memory after its written
sb.WriteString("\n```\n\n") // close out the file footer
seen.Add(filePath)
resultsChan <- Result{
Path: filePath,
Contents: sb.Bytes(),
Size: int64(sb.Len()),
}
}
// done is responsible for printing the results to STDOUT when the summarize program is finished
func done() {
// Print completion message
if *figs.Bool(kJson) {
r := M{
Message: fmt.Sprintf("Summary generated: %s\n",
filepath.Join(*figs.String(kOutputDir), *figs.String(kFilename)),
),
}
jb, err := json.MarshalIndent(r, "", " ")
if err != nil {
terminate(os.Stderr, "Error marshalling results: %v\n", err)
} else {
fmt.Println(string(jb))
}
} else {
fmt.Printf("Summary generated: %s\n",
filepath.Join(*figs.String(kOutputDir), *figs.String(kFilename)),
)
}
}
// iterate is a func that gets passed directly into the data.Range(iterate) that runs investigate concurrently with the
// wg and throttler enabled
func iterate(e, p any) bool {
ext, ok := e.(string)
if !ok {
return true // continue
}
thisData, ok := p.(mapData)
if !ok {
return true // continue
}
paths := slices.Clone(thisData.Paths)
throttler.Acquire() // throttler is used to protect the runtime from excessive use
wg.Add(1) // wg is used to prevent the runtime from exiting early
go investigate(&thisData, &toUpdate, ext, paths)
return true
}
// investigate is called from iterate where it takes an extension and a slice of paths to analyze each path
func investigate(innerData *mapData, toUpdate *[]mapData, ext string, paths []string) {
defer throttler.Release() // when we're done, release the throttler
defer wg.Done() // then tell the sync.WaitGroup that we are done
paths = simplify(paths)
innerData.Paths = paths
*toUpdate = append(*toUpdate, *innerData)
// process each file in the ext list (one ext per throttle slot in the semaphore)
for _, filePath := range paths {
if seen.Exists(filePath) {
continue
}
maxFileSemaphore.Acquire()
wg.Add(1)
go analyze(ext, filePath)
}
}
// populate is responsible for loading new paths into the *sync.Map called data
func populate(ext, path string) {
todo := make([]mapData, 0)
if data == nil {
panic("data is nil")
}
// populate the -inc list in data
data.Range(func(e any, p any) bool {
key, ok := e.(string)
if !ok {
return true // continue
}
value, ok := p.(mapData)
if !ok {
return true
}
if strings.EqualFold(key, ext) {
value.Ext = key
}
value.Paths = append(value.Paths, path)
todo = append(todo, value)
return true
})
for _, value := range todo {
data.Store(value.Ext, value)
}
}
// receive will accept Result from resultsChan and write to the summary file. If `-chat` is enabled, the StartChat will
// get called. Once the chat session is completed, the contents of the chat log is injected into the summary file.
func receive() {
if writerWG == nil {
panic("writer wg is nil")
}
defer writerWG.Done()
// Create output file
srcDir := *figs.String(kSourceDir)
outputFileName := filepath.Join(*figs.String(kOutputDir), *figs.String(kFilename))
var buf bytes.Buffer
buf.WriteString("# Project Summary - " + filepath.Base(*figs.String(kFilename)) + "\n")
buf.WriteString("Generated by " + projectName + " " + Version() + "\n\n")
buf.WriteString("AI Instructions are the user requests that you analyze their project workspace ")
buf.WriteString("as provided here by filename followed by the contents. You are to answer their ")
buf.WriteString("question using the source code provided as the basis of your responses. You are to ")
buf.WriteString("completely modify each individual file as per-the request and provide the completely ")
buf.WriteString("updated form of the file. Do not abbreviate the file, and if the file is excessive in ")
buf.WriteString("length, then print the entire contents in your response with your updates to the ")
buf.WriteString("specific components while retaining all existing functionality and maintaining comments ")
buf.WriteString("within the code. \n\n")
buf.WriteString("### Workspace\n\n")
abs, err := filepath.Abs(srcDir)
if err == nil {
buf.WriteString("`" + abs + "`\n\n")
} else {
buf.WriteString("`" + srcDir + "`\n\n")
}
renderMu := &sync.Mutex{}
renderedPaths := make(map[string]int64)
totalSize := int64(buf.Len())
for in := range resultsChan {
if _, exists := renderedPaths[in.Path]; exists {
continue
}
runningSize := atomic.AddInt64(&totalSize, in.Size)
if runningSize >= *figs.Int64(kMaxOutputSize) {
continue
}
renderMu.Lock()
renderedPaths[in.Path] = in.Size
buf.Write(in.Contents)
renderMu.Unlock()
}
if *figs.Bool(kChat) {
StartChat(&buf)
path := latestChatLog()
contents, err := os.ReadFile(path)
if err == nil {
old := buf.String()
buf.Reset()
buf.WriteString("## Chat Log \n\n")
body := string(contents)
body = strings.ReplaceAll(body, "You: ", "\n### ")
buf.WriteString(body)
buf.WriteString("\n\n")
buf.WriteString("## Summary \n\n")
buf.WriteString(old)
}
}
render(&buf, outputFileName)
}
// render will take the summary and either write it to a file, STDOUT or present an error to STDERR
func render(buf *bytes.Buffer, outputFileName string) {
shouldPrint := *figs.Bool(kPrint)
canWrite := *figs.Bool(kWrite)
showJson := *figs.Bool(kJson)
wrote := false
if *figs.Bool(kCompress) {
compressed, err := compress(bytes.Clone(buf.Bytes()))
capture("compressing bytes buffer", err)
buf.Reset()
buf.Write(compressed)
outputFileName += ".gz"
}
if !shouldPrint && !canWrite {
capture("saving output file during write", os.WriteFile(outputFileName, buf.Bytes(), 0644))
wrote = true
}
if canWrite && !wrote {
capture("saving output file during write", os.WriteFile(outputFileName, buf.Bytes(), 0644))
wrote = true
}
if shouldPrint {
if showJson {
r := Final{
Path: outputFileName,
Size: int64(buf.Len()),
Contents: buf.String(),
}
jb, err := json.MarshalIndent(r, "", " ")
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
fmt.Println(string(jb))
} else {
fmt.Println(buf.String())
}
os.Exit(0)
}
}
// summarize walks through a filepath recursively and matches paths that get stored inside the
// data *sync.Map for the extension.
func summarize(path string, info fs.FileInfo, err error) error {
if err != nil {
return err // return the error received
}
if !info.IsDir() {
// get the filename
filename := filepath.Base(path)
if *figs.Bool(kDotFiles) {
if strings.HasPrefix(filename, ".") {
return nil // skip without error
}
}
// check the -avoid list
for _, avoidThis := range lSkipContains {
a := strings.Contains(filename, avoidThis) || strings.Contains(path, avoidThis)
b := strings.HasPrefix(filename, avoidThis) || strings.HasPrefix(path, avoidThis)
c := strings.HasSuffix(filename, avoidThis) || strings.HasSuffix(path, avoidThis)
if a || b || c {
if isDebug {
fmt.Printf("ignoring %s in %s\n", filename, path)
}
return nil // skip without error
}
parts, err := filepath.Glob(path)
if err != nil {
errs = append(errs, err)
continue
}
for i := 0; i < len(parts); i++ {
part := parts[i]
if strings.EqualFold(part, string(os.PathSeparator)) {
continue
}
if strings.Contains(part, avoidThis) || strings.HasPrefix(part, avoidThis) || strings.HasSuffix(part, avoidThis) {
if isDebug {
fmt.Printf("skipping file %q\n", part)
}
return nil
}
}
}
// get the extension
ext := filepath.Ext(path)
ext = strings.ToLower(ext)
ext = strings.TrimPrefix(ext, ".")
if isDebug {
fmt.Printf("ext: %s\n", ext)
}
// check the -exc list
for _, excludeThis := range lExcludeExt {
if strings.EqualFold(excludeThis, ext) {
if isDebug {
fmt.Printf("ignoring %s\n", path)
}
return nil // skip without error
}
}
populate(ext, path)
}
// continue to the next file
return nil
}