-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
220 lines (187 loc) · 5.6 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"encoding/json"
"flag"
"fmt"
"golang.design/x/clipboard"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
inf "github.com/fzdwx/infinite"
"github.com/fzdwx/infinite/components/input/text"
"github.com/fzdwx/infinite/theme"
)
const (
GREEN = "\033[32m"
PURPLE = "\033[;35m"
)
var (
commandInterpreter string
token string
proxy string
copyToClipboardByDefault bool
h bool
)
func main() {
flag.StringVar(&commandInterpreter, "c", "bash", "command interpreter")
flag.StringVar(&token, "t", "", "openAI KEY")
flag.StringVar(&proxy, "p", "", "proxy address")
flag.BoolVar(©ToClipboardByDefault, "C", false, "copy to clipboard by default")
flag.BoolVar(&h, "h", false, "help")
flag.Usage = usage
flag.Parse()
if h {
flag.Usage()
return
}
i := inf.NewText(
text.WithPrompt("Enter your question?"),
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
)
question, _ := i.Display()
if len(question) == 0 {
fmt.Print("No question, exit.\n")
return
}
chatCompletionMessage := ChatCompletionMessage{
Model: "gpt-3.5-turbo",
Messages: []ChatMessage{
{
Role: "user",
Content: "你充当 Linux 终端。我输入问题,您回复应该使用什么命令。\\n我希望您只在唯一的代码块内回复终端代码,而不是其他任何内容。不要写解释。除非我指示您这样做,否则不要键入命令。在我建议您的时候, 请在上一命令的基础上进行改进。\\n我的第一个命令是 列出当前文件夹路径",
},
{
Role: "user",
Content: question,
},
},
}
chatCompletionResponse := completionMessages(chatCompletionMessage)
ask(chatCompletionResponse, chatCompletionMessage, chatCompletionResponse.Choices[0].Message.Content)
}
func usage() {
fmt.Print("Example: -c zsh -t openAI_APIKey -p http://127.0.0.1:7890 -C\n\n")
flag.PrintDefaults()
}
func ask(chatCompletionResponse ChatCompletionResponse, chatCompletionMessage ChatCompletionMessage, command string) {
fmt.Println(GREEN, command)
if copyToClipboardByDefault {
writeClipboard(command)
}
i := inf.NewText(
text.WithPrompt("Do you want to execute this command?"),
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
text.WithDefaultValue("Y/n/s(suggestion)/e(explain)/c(Copy to Clipboard)"),
)
whether, _ := i.Display()
if strings.EqualFold(whether, "y") {
executeCommand(command)
} else if strings.EqualFold(whether, "c") {
writeClipboard(command)
} else if strings.EqualFold(whether, "e") {
chatCompletionMessage.Messages = append(chatCompletionMessage.Messages, ChatMessage{
Role: "user",
Content: "解释" + command,
})
chatCompletionResponse = completionMessages(chatCompletionMessage)
fmt.Println(PURPLE, "\nExplain: ", chatCompletionResponse.Choices[0].Message.Content)
ask(chatCompletionResponse, chatCompletionMessage, command)
} else if strings.EqualFold(whether, "s") {
chatCompletionMessage.Messages = append(chatCompletionMessage.Messages, ChatMessage{
Role: "user",
Content: "建议" + askSuggestion(),
})
chatCompletionResponse = completionMessages(chatCompletionMessage)
ask(chatCompletionResponse, chatCompletionMessage, chatCompletionResponse.Choices[0].Message.Content)
}
}
func writeClipboard(command string) {
err := clipboard.Init()
if err != nil {
panic(err)
}
clipboard.Write(clipboard.FmtText, []byte(command))
}
func askSuggestion() string {
i := inf.NewText(
text.WithPrompt("Enter your suggestion:"),
text.WithPromptStyle(theme.DefaultTheme.PromptStyle),
)
suggestion, _ := i.Display()
if len(suggestion) == 0 {
askSuggestion()
}
return suggestion
}
func executeCommand(command string) {
cmd := exec.Command(commandInterpreter, "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fmt.Println("\nExecuting command:", command)
err := cmd.Run()
if err != nil {
fmt.Printf("cmd.Run: %s failed: %s\n", err, err)
}
}
func completionMessages(message ChatCompletionMessage) ChatCompletionResponse {
body, _ := json.Marshal(message)
req, _ := http.NewRequest(
"POST",
"https://api.openai.com/v1/chat/completions",
strings.NewReader(string(body)))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+token)
var clt http.Client
if proxy != "" {
proxyUrl, err := url.Parse(proxy)
if err != nil {
panic(err)
}
clt = http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
} else {
clt = http.Client{}
}
resp, err := clt.Do(req)
if err != nil {
fmt.Println(err)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, _ = io.ReadAll(resp.Body)
var chatCompletionResponse ChatCompletionResponse
_ = json.Unmarshal(body, &chatCompletionResponse)
if len(chatCompletionResponse.Choices) == 0 {
panic("No response, retry later.")
}
return chatCompletionResponse
}
type ChatCompletionMessage struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
}
type ChatMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type ChatCompletionResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Choices []struct {
Index int `json:"index"`
Message struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}