-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
151 lines (131 loc) · 3.25 KB
/
main.go
File metadata and controls
151 lines (131 loc) · 3.25 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
package main
import (
"fmt"
"strings"
"os"
// "os/exec"
"democli/start/utils"
"github.com/atotto/clipboard"
"github.com/charmbracelet/huh"
"github.com/joho/godotenv"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
var (
Model string
Prompt string
AvailableModels []string
ModelsMapped map[string]interface{}
modelOptions []huh.Option[string]
GROQ_API_KEY string
)
func loadHuhModelOption(API_KEY string) {
AvailableModels = utils.GetAvailableModels(API_KEY)
// huh options
for _, model := range AvailableModels {
modelName := cases.Title(language.English, cases.NoLower).String(strings.Join(strings.Split(model, "-"), " "))
modelOptions = append(modelOptions, huh.NewOption(modelName, model))
}
}
func main() {
// if api not provided during build time
if GROQ_API_KEY == "" {
// load the env
err := godotenv.Load("~/$HOME/Desktop/projects/gocli/.env")
if err != nil {
fmt.Println("Error loading .env file", err)
return
}
GROQ_API_KEY = os.Getenv("GROQ_API_KEY")
}
// check if the user wats to chat or commit
var purpose string = "commit"
purForm := huh.NewSelect[string]().
Title("Whats the mood?").
Options(
huh.NewOption("generate commit msg!", "commit"),
huh.NewOption("anything else?", "chat"),
).
Value(&purpose)
err := purForm.Run()
if err != nil {
fmt.Println(err)
return
}
// fetch the available models
loadHuhModelOption(GROQ_API_KEY)
selectModelForm := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Select a Model: ").
Options(
modelOptions...,
).
Value(&Model),
),
)
runErr := selectModelForm.Run()
if runErr != nil {
fmt.Println(runErr)
return
}
analyzer := NewDiffAnalyzer()
// its only for the chat purpose
var user_query string
if purpose == "chat" {
inputForm := huh.NewInput().
Title("Something different? ").
Placeholder("Tell me a joke!").
Value(&user_query)
runErr := inputForm.Run()
if runErr != nil {
fmt.Println(runErr)
return
}
var llm_context string
context, err := SearchDirectory(".")
if err != nil {
fmt.Println(err)
}
for t := 0; t < len(context); t++ {
llm_context = llm_context + context[t].FilePath + "\n"
llm_context = llm_context + "Imports: " + strings.Join(context[t].Imports, ", ") + "\n"
llm_context = llm_context + "Functions: " + strings.Join(context[t].Functions, ", ") + "\n"
llm_context = llm_context + "Variables: " + strings.Join(context[t].Variables, ", ") + "\n"
}
Prompt = "Context: " + llm_context + "\n\nUser Query: " + user_query
};if purpose == "commit" {
cmd, err := utils.RunCommand("git", "diff")
if err != nil {
fmt.Println(err)
return
}
Prompt, err = analyzer.analyzeGitDiff(cmd)
if err != nil {
fmt.Println(err)
return
}
}
// fmt.Println(Prompt)
if Prompt != "" {
res := utils.ModelCall(Model, Prompt, "", GROQ_API_KEY)
if res == "" {
fmt.Println("Model Calling went wrong")
return
}
if res[0] == '"' {
res = res[1 : len(res)-1]
}
fmt.Println(res)
// copy to clipboard
// if purpose == "commit" {
clipErr := clipboard.WriteAll(res)
if clipErr != nil {
fmt.Println("failed to copy ", clipErr)
return
}
// }
} else {
fmt.Println("yare yare! Prompt is empty!!!")
}
}