-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatsh.go
423 lines (337 loc) · 9.12 KB
/
chatsh.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package chatsh
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"github.com/atotto/clipboard"
"github.com/google/uuid"
)
var (
chatCmd = flag.NewFlagSet("chat", flag.ExitOnError)
setupCmd = flag.NewFlagSet("setup", flag.ExitOnError)
)
var subcommands = map[string]*flag.FlagSet{
chatCmd.Name(): chatCmd,
setupCmd.Name(): setupCmd,
}
type Message struct {
Role string `json:"role,omitempty"`
Content string `json:"content"`
}
type ContentParser struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
type CopilotChatPayload struct {
Messages interface{} `json:"messages"`
Model string `json:"model"`
Temperature float64 `json:"temperature"`
TopP float64 `json:"top_p"`
N int64 `json:"n"`
Stream bool `json:"stream"`
}
type CopilotSession struct{
chat string
githubToken string
copilotToken string
chatUrl string
input io.Reader
output io.Writer
chatHistory []Message
chatPrompt Message
chatFile string
}
type copilotOption func(client *CopilotSession) error
func NewInputWithInputFromArgs (args []string) copilotOption{
return func (c *CopilotSession) error {
if len(args) < 1{
return nil
}
c.chat = args[0]
return nil
}
}
func NewCopilotSession(opts ...copilotOption) (*CopilotSession, error){
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
filePath := filepath.Join(homeDir, ".config", ".chatsh")
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
var cache Cache
err = json.NewDecoder(f).Decode(&cache)
if err != nil {
return nil, err
}
c := &CopilotSession{
githubToken: cache.GithubToken,
chat :"",
input: os.Stdin,
output: os.Stdout,
chatHistory: []Message{},
chatUrl: "https://api.githubcopilot.com/chat/completions",
chatFile: "",
chatPrompt: Message{
Role: "system",
Content: "\nYou are ChatGPT, a large language model trained by OpenAI.\nKnowledge cutoff: 2021-09\nCurrent model: gpt-4\n",
},
}
for _, opt := range opts{
err := opt(c)
if err != nil {
return nil, err
}
}
return c, nil
}
func (c *CopilotSession) Authenticate() (error){
url := "https://api.github.com/copilot_internal/v2/token"
authenticateHeader := map[string]string{
"authorization": fmt.Sprintf("token %s", c.githubToken),
"editor-version": "vscode/1.80.1",
"editor-plugin-version": "copilot-chat/0.4.1",
"user-agent": "GitHubCopilotChat/0.4.1",
}
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
for k,v := range authenticateHeader {
req.Header.Set(k,v)
}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
var results struct{
Token string `json:"token"`
}
data, err := io.ReadAll(res.Body)
if err != nil {
return err
}
err = json.Unmarshal(data, &results)
if err != nil {
return err
}
c.copilotToken = results.Token
return nil
}
func (c *CopilotSession) Save() error {
f, err := os.Create(c.chatFile)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(c.chatHistory)
}
func (c *CopilotSession) Chat() error{
err := c.Authenticate()
if err != nil {
return err
}
headers := c.CreateHeader()
messagePayload := []Message{}
messagePayload = append(messagePayload, c.chatPrompt)
for _, chatHis := range c.chatHistory{
messagePayload= append(messagePayload, chatHis)
}
if c.chat == ""{
scanner := bufio.NewScanner(c.input)
text := ""
for scanner.Scan() {
text += scanner.Text()
}
messagePayload = append(messagePayload, Message{
Role: "user",
Content: text,
})
} else {
messagePayload = append(messagePayload, Message{
Role: "user",
Content: c.chat,
})
}
jsonBody := &CopilotChatPayload{
Messages: messagePayload,
Model: "gpt-4",
Temperature: 0.1,
TopP: 1,
N: 1,
Stream: false,
}
jsonData, err := json.Marshal(jsonBody)
if err != nil {
return err
}
req, _ := http.NewRequest("POST", c.chatUrl, bytes.NewReader(jsonData))
for k, v := range headers {
req.Header.Set(k, v)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var content ContentParser
err = json.Unmarshal(data, &content)
if err != nil {
return err
}
if c.chatFile != ""{
c.chatHistory = messagePayload[1:]
c.chatHistory = append(c.chatHistory, Message{
Role: "assistant",
Content: content.Choices[0].Message.Content,
})
err = c.Save()
}
if err != nil {
return err
}
fmt.Println(content.Choices[0].Message.Content)
return nil
}
func (c *CopilotSession) SetChatFile(chatFile string) error {
// i need to open file and populete the chatHistory :D
f, err := os.Open(chatFile)
c.chatFile = chatFile
if errors.Is(err, fs.ErrNotExist){
return nil
}
if err != nil {
return err
}
defer f.Close()
err = json.NewDecoder(f).Decode(&c.chatHistory)
if err != nil {
return err
}
return nil
}
func (c *CopilotSession) SetPrompt(prompt string) error {
c.chatPrompt = Message{
Role: "system",
Content: prompt,
}
return nil
}
func (c *CopilotSession) CreateHeader ()map[string]string{
return map[string]string{
"Authorization": "Bearer " + c.copilotToken,
"X-Request-Id": uuid.NewString(),
"Editor-Version": "vscode/1.83.1",
"Editor-Plugin-Version": "copilot-chat/0.8.0",
"Openai-Organization": "github-copilot",
"Openai-Intent": "conversation-panel",
"Content-Type": "application/json; charset=utf-8",
"User-Agent": "GitHubCopilotChat/0.8.0",
"Accept": "*/*",
"Accept-Encoding": "gzip,deflate,br",
"Connection": "close",
}
}
func MainAuthenticate() int {
fmt.Println("get github token")
err := githubAuthenticate()
if err != nil {
fmt.Fprintln(os.Stdout, err)
return 0
}
fmt.Println("Authenticate Success")
return 1
}
func MainChat(flag *flag.FlagSet) int {
chatFile := flag.String("chat-file", "", "add previous chat context specified by chat-file")
prompt := flag.String("prompt", "", "custom prompt")
clipBoardContext := flag.Bool("clipboard-context", false, "add clipboardas another context")
flag.Usage = func(){
fmt.Printf("Usage: %s chat [-chat-file] [-prompt] [-clipboard-context] [query]\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse(os.Args[2:])
copilot, err := NewCopilotSession(NewInputWithInputFromArgs(flag.Args()))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
if *prompt != ""{
err = copilot.SetPrompt(*prompt)
}
if *chatFile != ""{
err = copilot.SetChatFile(*chatFile)
}
if *clipBoardContext {
text, err := clipboard.ReadAll()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
copilot.chatHistory = append(copilot.chatHistory, Message{
Role: "user",
Content:text,
})
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
err = copilot.Chat()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
return 0
}
func Main() int {
usage := `Usage: chatsh [COMMAND]
Examples:
chatsh chat "How to install Git on Windows"
Chat with Copilot. This command will prompt text to the Copilot API.
chatsh chat -chat-file ./test.json "Rewrite everything with Go"
Same as above, but with previous chat context specified by ./test.json.
chatsh chat -h
chatsh setup
Setup Copilot for this CLI.
Available Commands:
setup Setup OAuth
chat Chat with Copilot`
if len(os.Args) < 2 {
fmt.Println(usage)
return 1
}
command := subcommands[os.Args[1]]
if command == nil {
fmt.Println("subcommand doesnt exist")
return 1
}
switch command.Name() {
case "setup":
return MainAuthenticate()
case "chat":
return MainChat(command)
}
fmt.Fprintf(os.Stderr, usage)
return 1
}