-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcopilot-chat-request.el
177 lines (156 loc) · 8.12 KB
/
copilot-chat-request.el
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
;;; copilot-chat --- copilot-chat-request.el --- copilot chat request backend -*- indent-tabs-mode: nil; lexical-binding:t -*-
;; Copyright (C) 2024 copilot-chat maintainers
;; The MIT License (MIT)
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;; Request backend for copilot-chat
;;; Code:
(require 'json)
(require 'request)
(require 'copilot-chat-common)
(cl-defun copilot-chat--request-token-cb (&key response
&key data
&allow-other-keys)
"Manage token reception for github auth.
Argument DATA is whatever PARSER function returns, or nil.
Argument RESPONSE is request-response object."
(unless (= (request-response-status-code response) 200)
(error "Http error"))
(let ((token (alist-get 'access_token data))
(token-dir (file-name-directory (expand-file-name copilot-chat-github-token-file))))
(setf (copilot-chat-github-token copilot-chat--instance) token)
(when (not (file-directory-p token-dir))
(make-directory token-dir t))
(with-temp-file copilot-chat-github-token-file
(insert token))))
(cl-defun copilot-chat--request-code-cb (&key response
&key data
&allow-other-keys)
"Manage user code reception for github buth.
Argument RESPONSE is request-response object.
Argument DATA is whatever PARSER function returns, or nil."
(unless (= (request-response-status-code response) 200)
(error "Http error"))
(let ((device-code (alist-get 'device_code data))
(user-code (alist-get 'user_code data))
(verification-uri (alist-get 'verification_uri data)))
(gui-set-selection 'CLIPBOARD user-code)
(read-from-minibuffer
(format "Your one-time code %s is copied. \
Press ENTER to open GitHub in your browser. \
If your browser does not open automatically, browse to %s."
user-code verification-uri))
(browse-url verification-uri)
(read-from-minibuffer "Press ENTER after authorizing.")
(request "https://github.com/login/oauth/access_token"
:type "POST"
:headers `(("content-type" . "application/json")
("accept" . "application/json")
("editor-plugin-version" . "CopilotChat.nvim/2.0.0")
("editor-version" . "Neovim/0.10.0")
("user-agent" . "CopilotChat.nvim/2.0.0"))
:data (format "{\"client_id\":\"Iv1.b507a08c87ecfe98\",\"device_code\":\"%s\",\"grant_type\":\"urn:ietf:params:oauth:grant-type:device_code\"}" device-code)
:parser 'json-read
:sync t
:complete #'copilot-chat--request-token-cb)))
(defun copilot-chat--request-login()
"Manage github login."
(request "https://github.com/login/device/code"
:type "POST"
:data "{\"client_id\":\"Iv1.b507a08c87ecfe98\",\"scope\":\"read:user\"}"
:sync t
:headers `(("content-type" . "application/json")
("accept" . "application/json")
("editor-plugin-version" . "CopilotChat.nvim/2.0.0")
("user-agent" . "CopilotChat.nvim/2.0.0")
("editor-version" . "Neovim/0.10.0"))
:parser 'json-read
:complete #'copilot-chat--request-code-cb))
(cl-defun copilot-chat--request-renew-token-cb(&key response
&key data
&allow-other-keys)
"Renew token callback.
Argument RESPONSE is request-response object.
Argument DATA is whatever PARSER function returns, or nil."
(unless (= (request-response-status-code response) 200)
(error "Authentication error"))
(setf (copilot-chat-token copilot-chat--instance) data)
;; save token in copilot-chat-token-cache file after creating
;; folders if needed
(let ((cache-dir (file-name-directory (expand-file-name copilot-chat-token-cache))))
(when (not (file-directory-p cache-dir))
(make-directory cache-dir t))
(with-temp-file copilot-chat-token-cache
(insert (json-encode data)))))
(defun copilot-chat--request-renew-token()
"Renew session token."
(request "https://api.github.com/copilot_internal/v2/token"
:type "GET"
:headers `(("authorization" . ,(concat "token " (copilot-chat-github-token copilot-chat--instance)))
("accept" . "application/json")
("editor-version" . "Neovim/0.10.0")
("editor-plugin-version" . "CopilotChat.nvim/2.0.0")
("user-agent" . "CopilotChat.nvim/2.0.0"))
:parser 'json-read
:sync t
:complete #'copilot-chat--request-renew-token-cb))
(defun copilot-chat--request-ask-parser ()
"Parser for copilot chat answer."
(let ((content ""))
(while (re-search-forward "^data: " nil t)
(let* ((line (buffer-substring-no-properties (point) (line-end-position)))
(json-string (and (not (string= "[DONE]" line)) line))
(json-obj (and json-string (json-parse-string json-string :object-type 'alist)))
(choices (and json-obj (alist-get 'choices json-obj)))
(delta (and (> (length choices) 0) (alist-get 'delta (aref choices 0))))
(token (and delta (alist-get 'content delta))))
(when (and token (not (eq token :null)))
(setq content (concat content token)))))
content))
(defun copilot-chat--request-ask (prompt callback out-of-context)
"Ask a question to Copilot using request backend.
Argument PROMPT is the prompt to send to copilot.
Argument CALLBACK is the function to call with copilot answer as argument.
Argument OUT-OF-CONTEXT is a boolean to indicate if the prompt is out of context."
(request "https://api.githubcopilot.com/chat/completions"
:type "POST"
:headers `(("openai-intent" . "conversation-panel")
("content-type" . "application/json")
("user-agent" . "CopilotChat.nvim/2.0.0")
("editor-plugin-version" . "CopilotChat.nvim/2.0.0")
("authorization" . ,(concat "Bearer "
(alist-get 'token (copilot-chat-token copilot-chat--instance))))
("x-request-id" . ,(copilot-chat--uuid))
("vscode-sessionid" . ,(copilot-chat-sessionid copilot-chat--instance))
("vscode-machineid" . ,(copilot-chat-machineid copilot-chat--instance))
("copilot-integration-id" . "vscode-chat")
("openai-organization" . "github-copilot")
("editor-version" . "Neovim/0.10.0"))
:data (copilot-chat--create-req prompt out-of-context)
:parser #'copilot-chat--request-ask-parser
:complete (cl-function
(lambda (&key response
&key data
&allow-other-keys)
(unless (= (request-response-status-code response) 200)
(error "Authentication error"))
(funcall callback data)
(unless out-of-context
(setf (copilot-chat-history copilot-chat--instance) (cons (list prompt "assistant") (copilot-chat-history copilot-chat--instance))))
(funcall callback copilot-chat--magic)))))
(provide 'copilot-chat-request)
;;; copilot-chat-request.el ends here