-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (121 loc) · 3.29 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
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/slog"
"os"
"runtime/debug"
"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() {
logger := slog.Default()
startTime := time.Now()
defer func() {
elapsed := time.Since(startTime)
logger.Info("finished execution", "elapsed", elapsed)
}()
config, err := parseConfig()
if err != nil {
logger.Error("fatal error", slog.Any("err", err))
os.Exit(1)
}
if config.TokenCountPath != "" {
ignorer, err := ignore.LoadIgnoreFiles(".")
if err != nil {
logger.Error("fatal error", slog.Any("err", err))
os.Exit(1)
}
if ignorer == nil {
logger.Error("git ignorer was nil")
os.Exit(1)
}
if err := tokentree.PrintTokenTree(os.DirFS("."), ignorer); err != nil {
slog.Error("fatal error", slog.Any("err", err))
os.Exit(1)
}
return
}
executor, err := agent.InitExecutor(logger, 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,
})
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)
}
}
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 input string
// Read from stdin or file if provided
if inputPath != "" && inputPath != "-" {
// Read from file
content, err := os.ReadFile(inputPath)
if err != nil {
return "", fmt.Errorf("error opening input file %s: %w", inputPath, err)
}
input = string(content)
} else if inputPath == "-" {
// Read from stdin
content, err := io.ReadAll(os.Stdin)
if err != nil {
return "", err
}
input = string(content)
}
// If we have a prompt from command line arguments, append it to any existing input
if cliopts.Opts.Prompt != "" {
if input != "" {
input = input + "\n\n" + cliopts.Opts.Prompt
} else {
input = cliopts.Opts.Prompt
}
}
if input == "" {
return "", fmt.Errorf("no input provided. Please provide input via stdin, input file, or as a command line argument")
}
return input, nil
}