-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (174 loc) · 4.93 KB
/
main.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
package main
import (
_ "embed"
"fmt"
"github.com/spachava753/cpe/internal/agent"
"github.com/spachava753/cpe/internal/cliopts"
"github.com/spachava753/cpe/internal/ignore"
"github.com/spachava753/cpe/internal/tokentree"
"io"
"log"
"log/slog"
"os"
"runtime/debug"
"strings"
"time"
)
// getVersion returns the version of the application from build info
func getVersion() string {
if info, ok := debug.ReadBuildInfo(); ok {
return info.Main.Version
}
return "(unknown version)"
}
func main() {
startTime := time.Now()
log.SetFlags(0)
log.SetOutput(os.Stderr)
defer func() {
elapsed := time.Since(startTime)
log.Printf("finished execution, elapsed: %s", elapsed)
}()
config, err := parseConfig()
if err != nil {
log.Fatalf("fatal error: %s", err)
}
ignorer, err := ignore.LoadIgnoreFiles(".")
if err != nil {
log.Fatalf("fatal error: %s", err)
}
if ignorer == nil {
log.Fatal("git ignorer was nil")
}
if config.TokenCountPath != "" {
if err := tokentree.PrintTokenTree(os.DirFS("."), ignorer); err != nil {
log.Fatalf("fatal error: %s", err)
}
return
}
if config.Overview {
result, err := agent.ExecuteFilesOverviewTool(ignorer)
if err != nil {
log.Fatalf("fatal error: %s", err)
}
if _, writeErr := fmt.Fprint(os.Stdout, result.Content); writeErr != nil {
log.Fatalf("fatal error: %s", writeErr)
}
return
}
if config.RelatedFiles != "" {
// Split the comma-separated list of files
inputFiles := strings.Split(config.RelatedFiles, ",")
// Trim whitespace from each file path
for i := range inputFiles {
inputFiles[i] = strings.TrimSpace(inputFiles[i])
}
result, err := agent.ExecuteGetRelatedFilesTool(inputFiles, ignorer)
if err != nil {
log.Fatalf("fatal error: %s", err)
}
if _, writeErr := fmt.Fprint(os.Stdout, result.Content); writeErr != nil {
log.Fatalf("fatal error: %s", writeErr)
}
return
}
if config.ListFiles {
files, err := agent.ListTextFiles(ignorer)
if err != nil {
log.Fatalf("fatal error: %s", err)
}
for _, file := range files {
if _, writeErr := fmt.Fprintf(os.Stdout, "File: %s\nContent:\n%s\n\n", file.Path, file.Content); writeErr != nil {
log.Fatalf("fatal error: %s", writeErr)
}
}
return
}
executor, err := agent.InitExecutor(log.Default(), agent.ModelOptions{
Model: config.Model,
CustomURL: config.CustomURL,
MaxTokens: config.MaxTokens,
Temperature: config.Temperature,
TopP: config.TopP,
TopK: config.TopK,
FrequencyPenalty: config.FrequencyPenalty,
PresencePenalty: config.PresencePenalty,
NumberOfResponses: config.NumberOfResponses,
Input: config.Input,
Version: config.Version,
Continue: config.Continue,
})
if err != nil {
slog.Error("fatal error", slog.Any("err", err))
os.Exit(1)
}
input, err := readInput(config.Input)
if err != nil {
slog.Error("fatal error", slog.Any("err", err))
os.Exit(1)
}
if err := executor.Execute(input); err != nil {
slog.Error("fatal error", slog.Any("err", err))
os.Exit(1)
}
// Save messages to file
f, err := os.Create(".cpeconvo")
if err != nil {
slog.Error("failed to create conversation file", slog.Any("err", err))
os.Exit(1)
}
defer f.Close()
if err := executor.SaveMessages(f); err != nil {
slog.Error("failed to save messages", slog.Any("err", err))
os.Exit(1)
}
}
func parseConfig() (cliopts.Options, error) {
cliopts.ParseFlags()
if cliopts.Opts.Version {
fmt.Printf("cpe version %s\n", getVersion())
os.Exit(0)
}
if cliopts.Opts.Model != "" && cliopts.Opts.Model != agent.DefaultModel {
_, ok := agent.ModelConfigs[cliopts.Opts.Model]
if !ok && cliopts.Opts.CustomURL == "" {
return cliopts.Options{}, fmt.Errorf("unknown model '%s' requires -custom-url flag", cliopts.Opts.Model)
}
}
return cliopts.Opts, nil
}
func readInput(inputPath string) (string, error) {
var inputs []string
// Check if there is any input from stdin by checking if stdin is a pipe or redirection
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
// Stdin has data available
content, err := io.ReadAll(os.Stdin)
if err != nil {
return "", fmt.Errorf("error reading from stdin: %w", err)
}
if len(content) > 0 {
inputs = append(inputs, string(content))
}
}
// Check if there is input from the -input flag
if inputPath != "" {
content, err := os.ReadFile(inputPath)
if err != nil {
return "", fmt.Errorf("error opening input file %s: %w", inputPath, err)
}
if len(content) > 0 {
inputs = append(inputs, string(content))
}
}
// Check if there is input from command line arguments
if cliopts.Opts.Prompt != "" {
inputs = append(inputs, cliopts.Opts.Prompt)
}
// Combine all inputs with double newlines
input := strings.Join(inputs, "\n\n")
if input == "" {
return "", fmt.Errorf("no input provided. Please provide input via stdin, input file, or as a command line argument")
}
return input, nil
}