-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
158 lines (133 loc) · 4.22 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"pluja.dev/maestro/db"
"pluja.dev/maestro/llm"
"pluja.dev/maestro/utils"
)
type Config struct {
Four bool
Three bool
Dev bool
ExecFlag bool
OllamaModel string
OAIToken string
OllamaURL string
OllamaDefaultModel string
Query string
EnableFolderContext bool
}
func main() {
cfg := parseFlags()
db.Init()
defer db.Badger.Close() // Simplified database closing logic
if err := handleConfigSettings(&cfg); err != nil {
exitWithError(err)
}
if cfg.Query == "" {
fmt.Println("Usage: maestro [flags] <query>")
os.Exit(1)
}
if err := processQuery(&cfg); err != nil {
exitWithError(err)
}
}
func parseFlags() Config {
var cfg Config
maestroFlags := flag.NewFlagSet("maestro", flag.ExitOnError)
maestroFlags.BoolVar(&cfg.Four, "4", false, "Use OpenAI GPT-4")
maestroFlags.BoolVar(&cfg.Three, "3", false, "Use OpenAI GPT-3")
maestroFlags.BoolVar(&cfg.Dev, "dev", false, "Enable development mode")
maestroFlags.BoolVar(&cfg.ExecFlag, "e", false, "Run the command instead of printing it")
maestroFlags.BoolVar(&cfg.EnableFolderContext, "ctx", false, "Enable the folder context (files and folders list)")
maestroFlags.StringVar(&cfg.OllamaModel, "m", "dolphin-mistral:latest", "Model to use")
maestroFlags.StringVar(&cfg.OAIToken, "set-openai-token", "", "Set OpenAI API token")
maestroFlags.StringVar(&cfg.OllamaURL, "set-ollama-url", "", "Set the ollama server URL")
maestroFlags.StringVar(&cfg.OllamaDefaultModel, "set-ollama-model", "", "Set the default ollama model")
maestroFlags.Parse(os.Args[1:])
cfg.Query = strings.Join(maestroFlags.Args(), " ")
return cfg
}
func handleConfigSettings(cfg *Config) error {
if cfg.OAIToken != "" {
return setAndExit("oai-token", cfg.OAIToken, "OpenAI API token set.")
}
if cfg.OllamaURL != "" {
endpoint := utils.SanitizeEndpoint(cfg.OllamaURL)
return setAndExit("ollama-url", endpoint, "Ollama URL set.")
}
if cfg.OllamaDefaultModel != "" {
return setAndExit("ollama-model", cfg.OllamaDefaultModel, "Ollama default model set to "+cfg.OllamaDefaultModel)
}
return nil
}
func setAndExit(key, value, message string) error {
if err := db.Badger.Set(key, value); err != nil {
return err
}
fmt.Println(utils.ColorGreen + message + utils.ColorReset)
os.Exit(0)
return nil // This return is never reached but required by the compiler
}
func processQuery(cfg *Config) error {
prompt := "**TASK: " + cfg.Query + "?**\n"
context, err := utils.GetContext(cfg.EnableFolderContext)
if err != nil {
return err
}
prompt += "```CONTEXT: " + context + "```"
ai, err := prepareAI(cfg)
if err != nil {
return err
}
response, err := ai.Ask(prompt)
if err != nil {
return err
}
displayResponse(response, cfg)
return nil
}
func prepareAI(cfg *Config) (llm.Llm, error) {
var ai llm.Llm
ai.Oai = cfg.Four || cfg.Three
ai.Openai.Gpt4 = cfg.Four
ollamaEndpoint, err := db.Badger.Get("ollama-url")
if err != nil {
return ai, fmt.Errorf("ollama URL not set. Please run `maestro -set-ollama-url <url>` first")
}
ollamaModel, err := db.Badger.Get("ollama-model")
if err != nil || cfg.OllamaModel != "dolphin-mistral:latest" {
ollamaModel = cfg.OllamaModel
}
ai.Ollama.Endpoint = ollamaEndpoint
ai.Ollama.Model = ollamaModel
return ai, nil
}
func displayResponse(response llm.Response, cfg *Config) {
for _, command := range response.Commands {
fmt.Println(utils.ColorComment + "# " + command.Comment + utils.ColorReset)
fmt.Println("$ " + utils.ColorGreen + command.Command + utils.ColorReset)
}
if cfg.ExecFlag {
executeCommands(response)
}
}
func executeCommands(response llm.Response) {
for _, command := range response.Commands {
fmt.Println(utils.ColorYellow + "\n🔥 " + command.Command + utils.ColorReset)
confirmation, _ := utils.GetUserConfirmation(fmt.Sprint(utils.ColorBlue + "🔥🏃 Run?" + utils.ColorReset))
if confirmation {
utils.RunCommand(command.Command)
} else {
fmt.Println(utils.ColorBlue + "[X] Command execution cancelled." + utils.ColorReset)
os.Exit(0)
}
}
}
func exitWithError(err error) {
fmt.Printf("%sError: %s%s\n", utils.ColorRed, err.Error(), utils.ColorReset)
os.Exit(1)
}