|
31 | 31 |
|
32 | 32 | ;;; Code: |
33 | 33 |
|
34 | | -(require 'comint) |
| 34 | +(require 'cl-lib) |
35 | 35 |
|
36 | 36 | (require 'openai) |
37 | 37 | (require 'ht) |
|
42 | 42 | :group 'comm |
43 | 43 | :link '(url-link :tag "Repository" "https://github.com/emacs-openai/chatgpt")) |
44 | 44 |
|
| 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 | + |
45 | 55 | (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.") |
47 | 57 |
|
48 | 58 | (defvar chatgpt-instances (ht-create) |
49 | 59 | "List of instances, each pair is consist of (index . buffer).") |
50 | 60 |
|
| 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 | + |
51 | 70 | ;; |
52 | 71 | ;;; Util |
53 | 72 |
|
54 | 73 | (defun chatgpt--pop-to-buffer (buffer-or-name) |
55 | | - "" |
| 74 | + "Wrapper to function `pop-to-buffer'. |
| 75 | +
|
| 76 | +Display buffer from BUFFER-OR-NAME." |
56 | 77 | (pop-to-buffer buffer-or-name `((display-buffer-in-direction) |
57 | 78 | (dedicated . t)))) |
58 | 79 |
|
| 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 | + |
59 | 86 | ;; |
60 | 87 | ;;; Core |
61 | 88 |
|
|
90 | 117 | (setq target (ht-size chatgpt-instances))) ; Create a new one! |
91 | 118 | target)) |
92 | 119 |
|
| 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 | + |
93 | 151 | ;;;###autoload |
94 | 152 | (define-derived-mode chatgpt-mode fundamental-mode "ChatGPT" |
95 | 153 | "Major mode for `chatgpt-mode'. |
96 | 154 |
|
97 | 155 | \\<chatgpt-mode-map>" |
98 | | - ) |
| 156 | + (setq-local buffer-read-only t)) |
99 | 157 |
|
100 | 158 | ;;;###autoload |
101 | 159 | (defun chatgpt-new () |
|
107 | 165 | (user-error "Internal Error: creating instance that already exists")) |
108 | 166 | (ht-set chatgpt-instances new-index (get-buffer-create new-buffer-name)) |
109 | 167 | (with-current-buffer new-buffer-name |
| 168 | + (setq chatgpt-instance (cons new-index (current-buffer))) |
110 | 169 | (chatgpt-mode 1)) |
111 | 170 | (chatgpt--pop-to-buffer new-buffer-name))) |
112 | 171 |
|
|
0 commit comments