-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (42 loc) · 1.2 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
package main
import (
"fmt"
"os"
"strings"
openai "github.com/sashabaranov/go-openai"
"github.com/henomis/ai-shell-go/internal/pkg/completion"
"github.com/henomis/ai-shell-go/internal/pkg/shell"
)
var (
ErrorShellAI = fmt.Errorf("🤖 OOPS! ")
)
func main() {
openAIKey := os.Getenv("OPENAI_API_KEY")
if openAIKey == "" {
fmt.Printf("%s: OPEN_AI_KEY is not set. Please set the OPENAI_API_KEY environment variable to your OpenAI API key\n", ErrorShellAI)
return
}
userInput := strings.Join(os.Args[1:], " ")
openAIClient := openai.NewClient(openAIKey)
completionInstance := completion.New(openAIClient)
shellInstance := shell.New(completionInstance)
shellResponse, err := shellInstance.Suggest(userInput, "")
if err != nil {
fmt.Printf("%s: %s\n", ErrorShellAI, err)
return
}
for shellResponse.CommandAction == shell.CommandActionRevise {
shellResponse, err = shellInstance.Suggest("", shellResponse.Command)
if err != nil {
fmt.Printf("%s: %s\n", ErrorShellAI, err)
return
}
}
if shellResponse.CommandAction == shell.CommandActionExecute {
err = shellInstance.Execute(shellResponse.Command)
if err != nil {
fmt.Printf("%s: %s\n", ErrorShellAI, err)
return
}
}
}