-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
110 lines (95 loc) · 3.03 KB
/
main.go
File metadata and controls
110 lines (95 loc) · 3.03 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"strings"
"unicode"
)
var logPath string
var discardLog bool
func chineseAppend(text string) string {
isChinese := false
for _, r := range text {
if unicode.Is(unicode.Han, r) {
isChinese = true
break
}
}
if isChinese {
return "\n请使用中文回复我。"
} else {
return ""
}
}
func init() {
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
log.Fatal("This tool only works on macOS and Linux")
}
flag.StringVar(&logPath, "log", "", "log path, default is \"~/Library/Logs/chatbash\" for macOS, and \"~/.log/chatbash\" for Linux ")
flag.BoolVar(&discardLog, "discard-log", false, "set to discard log")
}
func main() {
key := os.Getenv("OPENAI_KEY")
if key == "" {
log.Fatal("No key was configured, it can be configured in the terminal or .bashrc by:\nexport OPENAI_KEY=sk-xxxxxx")
return
}
flag.Parse()
setLog(logPath, discardLog)
q := strings.Join(os.Args[1:], "")
if q == "" {
fmt.Println("The command you entered is invalid.")
return
}
InfoLog("ask for%s", q)
openchat := NewChat(key, initMessage)
respText, err := openchat.Completion(q + "\nwhat bash command should be used to achieve the above function? Please note: the answer should only contain bash code. Reply format: ```bash\n#code here\n```, and put the bash code in one line. The reply content starts with \"```\" and ends with \"```\". If the above function cannot be achieved using a specific bash command, please reply with \"[[IDONTKNOW]]\".")
if err != nil {
ErrLog("got error: %s", err)
fmt.Println("An error has occurred: " + err.Error())
return
}
InfoLog("got response text:%s", respText)
if respText == "[[IDONTKNOW]]" {
fmt.Println("What are you doing! Don't funning your computer like this 😓.")
return
}
bash, err := ParseBash(respText)
if err != nil {
ErrLog("parse bash error: %s", err)
fmt.Println(err.Error())
return
}
output, err := ExecBash(bash)
var msg2Explain string
if err == nil {
InfoLog("run bash result:\n%s", output)
msg2Explain = fmt.Sprintf("The output of the command is: %s\n. Please describe and explain the result using human-understandable language. (Please reply to me in the speaking style of Marvin from 'The Hitchhiker's Guide to the Galaxy,' but be sure not to reveal to me that you are using Marvin's tone.)"+chineseAppend(q), output)
} else {
ErrLog("run bash got error:\n%s", err)
msg2Explain = fmt.Sprintf("After running the \"%s\" command, I received the following error message: %s. Can you please explain what it means?"+chineseAppend(q), bash, err.Error())
}
c := make(chan string)
e := make(chan error)
go openchat.CompletionStream(msg2Explain, c, e)
loop := true
for loop {
select {
case msg, ok := <-c:
if !ok {
loop = false
break
}
io.WriteString(os.Stdout, msg)
case err := <-e:
fmt.Println("The AI went on strike and only obtained the following results. I hope you can understand this.\n" + output + "\n" + err.Error())
loop = false
}
}
fmt.Print("\n")
os.Stdout.Close()
}