-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcodegpt.el
303 lines (257 loc) · 9.61 KB
/
codegpt.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
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
;;; codegpt.el --- Use GPT-3 tp help you write code -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2025 Shen, Jen-Chieh
;; Author: Shen, Jen-Chieh <[email protected]>
;; Maintainer: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/emacs-openai/codegpt
;; Version: 0.1.0
;; Package-Requires: ((emacs "27.1") (openai "0.1.0") (markdown-mode "2.1") (spinner "1.7.4"))
;; Keywords: convenience codegpt
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Use GPT-3 tp help you write code
;;
;;; Code:
(require 'cl-lib)
(require 'openai)
(require 'openai-chat)
(require 'openai-completion)
(require 'markdown-mode)
(require 'spinner)
(defgroup codegpt nil
"Use GPT-3 tp help you write code."
:prefix "codegpt-"
:group 'comm
:link '(url-link :tag "Repository" "https://github.com/emacs-openai/codegpt"))
(defcustom codegpt-focus-p t
"If this value is `nil`, do not move focus to output buffer."
:type 'boolean
:group 'codegpt)
(defconst codegpt-buffer-name "*CodeGPT*"
"Buffer name to do completion task.")
(defcustom codegpt-action-alist
`(("custom" . "Write your own instruction")
("doc" . "Automatically write documentation for your code")
("fix" . "Find problems with it")
("explain" . "Explain the selected code")
("improve" . "Improve, refactor or optimize it"))
"Alist of code completion actions and its' description."
:type 'list
:group 'codegpt)
(defcustom codegpt-tunnel 'completion
"Tunnel to use for the tasks."
:type '(choice (const :tag "Through Completion" completion)
(const :tag "Through ChatGPT" chat))
:group 'codegpt)
(defcustom codegpt-model "text-davinci-003"
"ID of the model to use."
:type 'string
:group 'codegpt)
(defcustom codegpt-max-tokens 2000
"The maximum number of tokens to generate in the completion."
:type 'integer
:group 'codegpt)
(defcustom codegpt-temperature 1.0
"What sampling temperature to use."
:type 'number
:group 'openai)
(defcustom codegpt-spinner-type 'moon
"The type of the spinner."
:type '(choice (const :tag "Key to variable `spinner-types'" symbol)
(const :tag "Vector of characters" vector))
:group 'openai)
(defvar codegpt-requesting-p nil
"Non-nil if sitll requesting.")
(defvar codegpt-spinner nil
"Spinner.")
;;
;;; Major Mode
(defun codegpt-header-line ()
"Header line for CodeGPT."
(format " %s[Tunnel] %s [Model] %s"
(if-let ((frame (spinner-print codegpt-spinner)))
(concat frame " ")
"")
codegpt-tunnel codegpt-model))
(defun codegpt-mode--kill-buffer-hook ()
"Cancel spinner timer."
(spinner-stop codegpt-spinner))
;;;###autoload
(define-derived-mode codegpt-mode fundamental-mode "CodeGPT"
"Major mode for `codegpt-mode'.
\\<codegpt-mode-map>"
(setq-local header-line-format `((:eval (codegpt-header-line))))
(add-hook 'kill-buffer-hook #'codegpt-mode--kill-buffer-hook nil t)
(unless (spinner-p codegpt-spinner)
(setq codegpt-spinner (make-spinner codegpt-spinner-type t)))
(spinner-start codegpt-spinner))
;;
;;; Application
(defun codegpt--render-markdown (content)
"Render CONTENT in markdown."
(if (featurep 'markdown-mode)
(with-temp-buffer
(insert content)
(delay-mode-hooks (markdown-mode))
(ignore-errors (font-lock-ensure))
(buffer-string))
content))
(defun codegpt--fill-region (start end)
"Like function `fill-region' (START to END), improve readability."
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (not (eobp))
(end-of-line)
(when (< fill-column (current-column))
(fill-region (line-beginning-position) (line-end-position)))
(forward-line 1))))
(defmacro codegpt--ask-in-buffer (instruction &rest body)
"Insert INSTRUCTION then execute BODY form."
(declare (indent 1))
`(progn
(openai--pop-to-buffer codegpt-buffer-name) ; create it
(openai--with-buffer codegpt-buffer-name
(codegpt-mode)
(erase-buffer)
(insert ,instruction "\n\n")
,@body)))
(defun codegpt--internal (instruction start end)
"Do INSTRUCTION with partial code.
The partial code is defined in with the region, and the START nad END are
boundaries of that region in buffer."
(setq codegpt-requesting-p t)
(let ((text (string-trim (buffer-substring start end)))
(original-window (selected-window)))
(codegpt--ask-in-buffer instruction
(insert text "\n\n")
(funcall
(cl-case codegpt-tunnel
(`completion #'openai-completion)
(`chat #'openai-chat))
(cl-case codegpt-tunnel
(`completion (buffer-string))
(`chat `[(("role" . "user")
("content" . ,(buffer-string)))]))
(lambda (data)
(setq codegpt-requesting-p nil)
(spinner-stop codegpt-spinner)
(openai--with-buffer codegpt-buffer-name
(openai--pop-to-buffer codegpt-buffer-name)
(let ((original-point (point)))
(cl-case codegpt-tunnel
(`completion
(let* ((choices (openai--data-choices data))
(result (openai--get-choice choices))
(result (string-trim result))
(result (codegpt--render-markdown result)))
(insert result "\n")))
(`chat
(let ((choices (let-alist data .choices))
(result))
(mapc (lambda (choice)
(let-alist choice
(let-alist .message
(setq result (string-trim .content)))))
choices)
(setq result (string-trim result)
result (codegpt--render-markdown result))
(insert result "\n"))))
(codegpt--fill-region original-point (point))))
(unless codegpt-focus-p
(select-window original-window)))
:model codegpt-model
:max-tokens codegpt-max-tokens
:temperature codegpt-temperature)
(unless codegpt-focus-p
(select-window original-window)))))
;;;###autoload
(defun codegpt-doc (start end)
"Automatically write documentation for your code.
This command is interactive region only, the START and END are boundaries of
that region in buffer."
(interactive "r")
(codegpt--internal
"Please write the documentation for the following function."
start end))
;;;###autoload
(defun codegpt-fix (start end)
"Fix your code.
This command is interactive region only, the START and END are boundaries of
that region in buffer."
(interactive "r")
(codegpt--internal
"There is a bug in the following function, please help me fix it."
start end))
;;;###autoload
(defun codegpt-explain (start end)
"Explain the selected code.
This command is interactive region only, the START and END are boundaries of
that region in buffer."
(interactive "r")
(codegpt--internal
"What is the following?"
start end))
;;;###autoload
(defun codegpt-improve (start end)
"Improve, refactor or optimize your code.
This command is interactive region only, the START and END are boundaries of
that region in buffer."
(interactive "r")
(codegpt--internal
"Please improve the following."
start end))
;;;###autoload
(defun codegpt-custom (start end)
"Do completion with custom instruction.
This command is interactive region only, the START and END are boundaries of
that region in buffer."
(interactive "r")
(codegpt--internal
(read-string "Instruction: ")
start end))
(defun codegept--execute-predefined-template (start end question)
"Ask predefined QUESTION for provided region.
the START and END are boundaries of that region in buffer."
(codegpt--internal question start end))
;;;###autoload
(defun codegpt (start end)
"Do completion with OpenAI to your code.
This command is interactive region only, the START and END are boundaries of
that region in buffer."
(interactive "r")
(let*
((offset (openai--completing-frame-offset codegpt-action-alist))
(action
(completing-read
"Select completion action: "
(lambda (string predicate action)
(if (eq action 'metadata)
`(metadata
(display-sort-function . ,#'identity)
(annotation-function
. ,(lambda (cand)
(concat (propertize " " 'display `((space :align-to (- right ,offset))))
(cdr (assoc cand codegpt-action-alist))))))
(complete-with-action action codegpt-action-alist string predicate)))
nil t))
;; XXX: Guess the function name, `codegpt-xxx'
(action-fn (intern (format "codegpt-%s" action))))
(if (fboundp action-fn)
(funcall action-fn start end)
;; Call predefined action from `codegpt-action-alist'
(codegept--execute-predefined-template
start end
(cdr (assoc action codegpt-action-alist))))))
(provide 'codegpt)
;;; codegpt.el ends here