Skip to content

Commit f550c82

Browse files
committed
wip
1 parent 65fc83a commit f550c82

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

chatgpt.el

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
;;; Code:
3333

34-
(require 'comint)
34+
(require 'cl-lib)
3535

3636
(require 'openai)
3737
(require 'ht)
@@ -42,20 +42,47 @@
4242
:group 'comm
4343
:link '(url-link :tag "Repository" "https://github.com/emacs-openai/chatgpt"))
4444

45+
(defcustom chatgpt-max-tokens 4000
46+
"The maximum number of tokens to generate in the completion."
47+
:type 'integer
48+
:group 'chatgpt)
49+
50+
(defcustom chatgpt-temperature 1.0
51+
"What sampling temperature to use."
52+
:type 'number
53+
:group 'chatgpt)
54+
4555
(defconst chatgpt-buffer-name-format "*ChatGPT: <%s>*"
46-
"Name of the buffer to use for the `chatgpt' comint instance.")
56+
"Name of the buffer to use for the `chatgpt' instance.")
4757

4858
(defvar chatgpt-instances (ht-create)
4959
"List of instances, each pair is consist of (index . buffer).")
5060

61+
(defvar-local chatgpt-instance nil
62+
"Instance data for each buffer.")
63+
64+
(defvar-local chatgpt-chat-history nil
65+
"The chat history use to send request.")
66+
67+
(defvar-local chatgpt-requesting-p nil
68+
"Non-nil when requesting; waiting for the response.")
69+
5170
;;
5271
;;; Util
5372

5473
(defun chatgpt--pop-to-buffer (buffer-or-name)
55-
""
74+
"Wrapper to function `pop-to-buffer'.
75+
76+
Display buffer from BUFFER-OR-NAME."
5677
(pop-to-buffer buffer-or-name `((display-buffer-in-direction)
5778
(dedicated . t))))
5879

80+
(defun chatgpt--get-user ()
81+
"Return the current user."
82+
(if (string-empty-p openai-user)
83+
"user" ; this is free?
84+
openai-user))
85+
5986
;;
6087
;;; Core
6188

@@ -90,12 +117,43 @@
90117
(setq target (ht-size chatgpt-instances))) ; Create a new one!
91118
target))
92119

120+
(defun chatgpt--display-message (instance message)
121+
""
122+
(with-current-buffer (get-buffer (cdr instance))
123+
(let ((inhibit-read-only t))
124+
125+
)))
126+
127+
(defun chatgpt-type-response ()
128+
""
129+
(interactive)
130+
(let ((response (read-string "Type response: "))
131+
(instance chatgpt-instance))
132+
(setq chatgpt-requesting-p t)
133+
(openai-chat response
134+
(lambda (data)
135+
(let ((message ; TODO: ..
136+
))
137+
(chatgpt--display-response instance message)))
138+
:max-tokens chatgpt-max-tokens
139+
:temperature chatgpt-temperature
140+
:user (chatgpt--get-user))))
141+
142+
;;
143+
;;; Entry
144+
145+
(defvar chatgpt-mode-map
146+
(let ((map (make-sparse-keymap)))
147+
(define-key map (kbd "RET") #'chatgpt-type-response)
148+
map)
149+
"Keymap for `chatgpt-mode'.")
150+
93151
;;;###autoload
94152
(define-derived-mode chatgpt-mode fundamental-mode "ChatGPT"
95153
"Major mode for `chatgpt-mode'.
96154
97155
\\<chatgpt-mode-map>"
98-
)
156+
(setq-local buffer-read-only t))
99157

100158
;;;###autoload
101159
(defun chatgpt-new ()
@@ -107,6 +165,7 @@
107165
(user-error "Internal Error: creating instance that already exists"))
108166
(ht-set chatgpt-instances new-index (get-buffer-create new-buffer-name))
109167
(with-current-buffer new-buffer-name
168+
(setq chatgpt-instance (cons new-index (current-buffer)))
110169
(chatgpt-mode 1))
111170
(chatgpt--pop-to-buffer new-buffer-name)))
112171

0 commit comments

Comments
 (0)