-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (87 loc) · 2.53 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
package main
import (
"flag"
"fmt"
"os"
"strings"
openai "github.com/sashabaranov/go-openai"
)
func main() {
flavFlag := flag.String("f", "senior", "flavor: To make the generated code match the existing code-base, pick a flavor from [ junior | senior | bigbrain ]")
langFlag := flag.String("l", "", "language: The (programming) language the source is in. If no language is provided, a guess is made.")
flag.CommandLine.Usage = func() {
stderr("Usage:")
stderr(fmt.Sprintf("\t%s [flags] FILE", os.Args[0]))
stderr("\nAvailable flags:")
}
flag.Parse()
token, ok := os.LookupEnv("OPENAI_KEY")
if !ok || token == "" {
fatal("No OpenAI API key provided. Please set it via the environment variable OPENAI_KEY")
}
if len(flag.Args()) != 1 {
picard()
flag.CommandLine.Usage()
flag.PrintDefaults()
os.Exit(1)
}
fileName := flag.Arg(0)
buf, err := os.ReadFile(fileName)
if err != nil {
fatal(err)
}
file := string(buf)
lang := *langFlag
if lang == "" {
lang = "a programming language you need to determine from the given file before following other instructions."
}
outFile := os.Stdout
theMegaBrain := &megaBrain{
client: openai.NewClient(token),
flavor: flavorType(*flavFlag),
}
var output string
output, err = theMegaBrain.thinkRealHard(theMegaBrain.engineerPrompt(basePromptPass1, lang, file))
if err != nil {
fatal(err)
}
output, err = theMegaBrain.thinkRealHard(theMegaBrain.engineerPrompt(basePromptPass2, lang, output))
if err != nil {
fatal(err)
}
// i know how to use regex. go away
output = strings.ReplaceAll(output, "/* "+begin+" */", "")
output = strings.ReplaceAll(output, "/* "+end+" */", "")
output = strings.ReplaceAll(output, "// "+begin, "")
output = strings.ReplaceAll(output, "// "+end, "")
output = strings.ReplaceAll(output, "/*"+begin+"*/", "")
output = strings.ReplaceAll(output, "/*"+end+"*/", "")
output = strings.ReplaceAll(output, "//"+begin, "")
output = strings.ReplaceAll(output, "//"+end, "")
_, _ = outFile.WriteString(output + "\n")
}
func picard() {
stderr(strings.Join([]string{
"",
" .-'---`-.",
",' `.",
"| \\",
"| \\",
"\\ _ \\",
",\\ _ ,'-,/-)\\",
"( * \\ \\,' ,' ,'-)",
" `._,) -',-')",
" \\/ ''/",
" ) / /",
" / ,'-'",
"", "The computer's attempt to execute the designated task has regrettably failed.", "",
}, "\n"))
}
func fatal(err any) {
picard()
stderr(fmt.Sprintf("%v", err))
os.Exit(1)
}
func stderr(s string) {
_, _ = os.Stderr.WriteString(s + "\n")
}