forked from karthink/gptel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gptel-transient.el
696 lines (636 loc) · 27.4 KB
/
gptel-transient.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
;;; gptel-transient.el --- Transient menu for GPTel -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Karthik Chikmagalur
;; Author: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
;; Keywords: convenience
;; SPDX-License-Identifier: GPL-3.0-or-later
;; 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:
;;
;;; Code:
(require 'cl-lib)
(require 'gptel)
(require 'transient)
(declare-function ediff-regions-internal "ediff")
(declare-function ediff-make-cloned-buffer "ediff-utils")
;; * Helper functions
(defun gptel--refactor-or-rewrite ()
"Rewrite should be refactored into refactor.
Or is it the other way around?"
(if (derived-mode-p 'prog-mode)
"Refactor" "Rewrite"))
(defvar-local gptel--rewrite-message nil)
(defun gptel--rewrite-message ()
"Set a generic refactor/rewrite message for the buffer."
(if (derived-mode-p 'prog-mode)
(format "You are a %s programmer. Refactor the following code. Generate only code, no explanation."
(substring (symbol-name major-mode) nil -5))
(format "You are a prose editor. Rewrite the following text to be more professional.")))
(defvar gptel--crowdsourced-prompts-url
"https://github.com/f/awesome-chatgpt-prompts/raw/main/prompts.csv"
"URL for crowdsourced ChatGPT system prompts.")
(defvar gptel--crowdsourced-prompts
(make-hash-table :test #'equal)
"Crowdsourced system prompts for ChatGPT.")
(defun gptel--crowdsourced-prompts ()
"Acquire and read crowdsourced system prompts for ChatGPT.
These are stored in the variable `gptel--crowdsourced-prompts',
which see."
(when (hash-table-p gptel--crowdsourced-prompts)
(when (hash-table-empty-p gptel--crowdsourced-prompts)
(unless gptel-crowdsourced-prompts-file
(run-at-time 0 nil #'gptel-system-prompt)
(user-error "No crowdsourced prompts available"))
(unless (and (file-exists-p gptel-crowdsourced-prompts-file)
(time-less-p
(time-subtract (current-time) (days-to-time 14))
(file-attribute-modification-time
(file-attributes gptel-crowdsourced-prompts-file))))
(when (y-or-n-p
(concat
"Fetch crowdsourced system prompts from "
(propertize "https://github.com/f/awesome-chatgpt-prompts" 'face 'link)
"?"))
;; Fetch file
(message "Fetching prompts...")
(if (url-copy-file gptel--crowdsourced-prompts-url
gptel-crowdsourced-prompts-file
'ok-if-already-exists)
(message "Fetching prompts... done.")
(message "Could not retrieve new prompts."))))
(if (not (file-readable-p gptel-crowdsourced-prompts-file))
(progn (message "No crowdsourced prompts available")
(call-interactively #'gptel-system-prompt))
(with-temp-buffer
(insert-file-contents gptel-crowdsourced-prompts-file)
(goto-char (point-min))
(forward-line 1)
(while (not (eobp))
(when-let ((act (read (current-buffer))))
(forward-char)
(save-excursion
(while (re-search-forward "\"\"" (line-end-position) t)
(replace-match "\\\\\"")))
(when-let ((prompt (read (current-buffer))))
(puthash act prompt gptel--crowdsourced-prompts)))
(forward-line 1)))))
gptel--crowdsourced-prompts))
;; * Transient Prefixes
(define-obsolete-function-alias 'gptel-send-menu 'gptel-menu "0.3.2")
;; BUG: The `:incompatible' spec doesn't work if there's a `:description' below it.
;;;###autoload (autoload 'gptel-menu "gptel-transient" nil t)
(transient-define-prefix gptel-menu ()
"Change parameters of prompt to send ChatGPT."
;; :incompatible '(("-m" "-n" "-k" "-e"))
[:description
(lambda () (format "Directive: %s"
(truncate-string-to-width
gptel--system-message (max (- (window-width) 14) 20) nil nil t)))
("h" "Set directives for chat" gptel-system-prompt :transient t)]
[["Session Parameters"
(gptel--infix-provider)
;; (gptel--infix-model)
(gptel--infix-max-tokens)
(gptel--infix-num-messages-to-send)
(gptel--infix-temperature)]
["Prompt:"
("p" "From minibuffer instead" "p")
("y" "From kill-ring instead" "y")
("i" "Replace/Delete prompt" "i")
"Response to:"
("m" "Minibuffer instead" "m")
("g" "gptel session" "g"
:class transient-option
:prompt "Existing or new gptel session: "
:reader
(lambda (prompt _ _history)
(read-buffer
prompt (generate-new-buffer-name
(concat "*" (gptel-backend-name gptel-backend) "*"))
nil (lambda (buf-name)
(if (consp buf-name) (setq buf-name (car buf-name)))
(let ((buf (get-buffer buf-name)))
(and (buffer-local-value 'gptel-mode buf)
(not (eq (current-buffer) buf))))))))
("b" "Any buffer" "b"
:class transient-option
:prompt "Output to buffer: "
:reader
(lambda (prompt _ _history)
(read-buffer prompt (buffer-name (other-buffer)) nil)))
("k" "Kill-ring" "k")]
[:description gptel--refactor-or-rewrite
:if use-region-p
("r"
;;FIXME: Transient complains if I use `gptel--refactor-or-rewrite' here. It
;;reads this function as a suffix instead of a function that returns the
;;description.
(lambda () (if (derived-mode-p 'prog-mode)
"Refactor" "Rewrite"))
gptel-rewrite-menu)]
["Send" (gptel--suffix-send)]]
(interactive)
(gptel--sanitize-model)
(transient-setup 'gptel-menu))
;; ** Prefix for setting the system prompt.
(defun gptel-system-prompt--setup (_)
"Set up suffixes for system prompt."
(transient-parse-suffixes
'gptel-system-prompt
(cl-loop for (type . prompt) in gptel-directives
with taken
with width = (window-width)
for name = (symbol-name type)
for key =
(let ((idx 0) pos)
(while (or (not pos) (member pos taken))
(setq pos (substring name idx (1+ idx)))
(cl-incf idx))
(push pos taken)
pos)
;; The explicit declaration ":transient transient--do-return" here
;; appears to be required for Transient v0.5 and up. Without it, these
;; are treated as suffixes when invoking `gptel-system-prompt' directly,
;; and infixes when going through `gptel-menu'.
;; TODO: Raise an issue with Transient.
collect (list (key-description key)
(concat (capitalize name) " "
(propertize " " 'display '(space :align-to 20))
(propertize
(concat
"("
(string-replace
"\n" " "
(truncate-string-to-width prompt (- width 30) nil nil t))
")")
'face 'shadow))
`(lambda () (interactive)
(message "Directive: %s" ,prompt)
(setq gptel--system-message ,prompt))
:transient 'transient--do-return)
into prompt-suffixes
finally return
(nconc
(list (list 'gptel--suffix-system-message))
prompt-suffixes
(list (list "SPC" "Pick crowdsourced prompt"
'gptel--read-crowdsourced-prompt
;; NOTE: Quitting the completing read when picking a
;; crowdsourced prompt will cause the transient to exit
;; instead of returning to the system prompt menu.
:transient 'transient--do-exit))))))
(transient-define-prefix gptel-system-prompt ()
"Change the system prompt to send ChatGPT.
The \"system\" prompt establishes directives for the chat
session. Some examples of system prompts are:
You are a helpful assistant. Answer as concisely as possible.
Reply only with shell commands and no prose.
You are a poet. Reply only in verse.
Customize `gptel-directives' for task-specific prompts."
[:description
(lambda () (format "Current directive: %s"
(truncate-string-to-width
gptel--system-message 100 nil nil t)))
:class transient-column
:setup-children gptel-system-prompt--setup
:pad-keys t])
;; ** Prefix for rewriting/refactoring
(transient-define-prefix gptel-rewrite-menu ()
"Rewrite or refactor text region using ChatGPT."
[:description
(lambda ()
(format "Directive: %s"
(truncate-string-to-width
(or gptel--rewrite-message (gptel--rewrite-message))
(max (- (window-width) 14) 20) nil nil t)))
(gptel--infix-rewrite-prompt)]
[[:description "Diff Options"
("-w" "Wordwise diff" "-w")]
[:description
(lambda () (if (derived-mode-p 'prog-mode)
"Refactor" "Rewrite"))
(gptel--suffix-rewrite)
(gptel--suffix-rewrite-and-replace)
(gptel--suffix-rewrite-and-ediff)]]
(interactive)
(unless gptel--rewrite-message
(setq gptel--rewrite-message (gptel--rewrite-message)))
(transient-setup 'gptel-rewrite-menu))
;; * Transient Infixes
;; ** Infixes for model parameters
(defun gptel--transient-read-variable (prompt initial-input history)
"Read value from minibuffer and interpret the result as a Lisp object.
PROMPT, INITIAL-INPUT and HISTORY are as in the Transient reader
documention."
(ignore-errors
(read-from-minibuffer prompt initial-input read-expression-map t history)))
(transient-define-infix gptel--infix-num-messages-to-send ()
"Number of recent messages to send with each exchange.
By default, the full conversation history is sent with every new
prompt. This retains the full context of the conversation, but
can be expensive in token size. Set how many recent messages to
include."
:description "Number of past messages to send"
:class 'transient-lisp-variable
:variable 'gptel--num-messages-to-send
:key "-n"
:prompt "Number of past messages to include for context (leave empty for all): "
:reader 'gptel--transient-read-variable)
(transient-define-infix gptel--infix-max-tokens ()
"Max tokens per response.
This is roughly the number of words in the response. 100-300 is a
reasonable range for short answers, 400 or more for longer
responses.
If left unset, ChatGPT will target about 40% of the total token
count of the conversation so far in each message, so messages
will get progressively longer!"
:description "Response length (tokens)"
:class 'transient-lisp-variable
:variable 'gptel-max-tokens
:key "-c"
:prompt "Response length in tokens (leave empty: default, 80-200: short, 200-500: long): "
:reader 'gptel--transient-read-variable)
(defclass gptel-provider-variable (transient-lisp-variable)
((model :initarg :model)
(model-value :initarg :model-value)
(always-read :initform t)
(set-value :initarg :set-value :initform #'set))
"Class used for gptel-backends.")
(cl-defmethod transient-format-value ((obj gptel-provider-variable))
(propertize (concat (gptel-backend-name (oref obj value)) ":"
(buffer-local-value (oref obj model) transient--original-buffer))
'face 'transient-value))
(cl-defmethod transient-infix-set ((obj gptel-provider-variable) value)
(pcase-let ((`(,backend-value ,model-value) value))
(funcall (oref obj set-value)
(oref obj variable)
(oset obj value backend-value))
(funcall (oref obj set-value)
(oref obj model)
(oset obj model-value model-value))))
(transient-define-infix gptel--infix-provider ()
"AI Provider for Chat."
:description "GPT Model: "
:class 'gptel-provider-variable
:prompt "Model provider: "
:variable 'gptel-backend
:model 'gptel-model
:key "-m"
:reader (lambda (prompt &rest _)
(let* ((backend-name
(if (<= (length gptel--known-backends) 1)
(caar gptel--known-backends)
(completing-read
prompt
(mapcar #'car gptel--known-backends))))
(backend (alist-get backend-name gptel--known-backends
nil nil #'equal))
(backend-models (gptel-backend-models backend))
(model-name (if (= (length backend-models) 1)
(car backend-models)
(completing-read
"Model: " backend-models))))
(list backend model-name))))
(transient-define-infix gptel--infix-model ()
"AI Model for Chat."
:description "GPT Model: "
:class 'transient-lisp-variable
:variable 'gptel-model
:key "-m"
:choices '("gpt-3.5-turbo" "gpt-3.5-turbo-16k" "gpt-4" "gpt-4-1106-preview")
:reader (lambda (prompt &rest _)
(completing-read
prompt
'("gpt-3.5-turbo" "gpt-3.5-turbo-16k" "gpt-4" "gpt-4-1106-preview"))))
(transient-define-infix gptel--infix-temperature ()
"Temperature of request."
:description "Randomness (0 - 2.0)"
:class 'transient-lisp-variable
:variable 'gptel-temperature
:key "-t"
:prompt "Set temperature (0.0-2.0, leave empty for default): "
:reader 'gptel--transient-read-variable)
;; ** Infix for the refactor/rewrite system message
(transient-define-infix gptel--infix-rewrite-prompt ()
"Chat directive (system message) to use for rewriting or refactoring."
:description (lambda () (if (derived-mode-p 'prog-mode)
"Set directives for refactor"
"Set directives for rewrite"))
:format "%k %d"
:class 'transient-lisp-variable
:variable 'gptel--rewrite-message
:key "h"
:prompt "Set directive for rewrite: "
:reader (lambda (prompt _ history)
(read-string
prompt (gptel--rewrite-message) history)))
;; * Transient Suffixes
;; ** Suffix to send prompt
(transient-define-suffix gptel--suffix-send (args)
"Send ARGS."
:key "RET"
:description "Send prompt"
(interactive (list (transient-args transient-current-command)))
(let ((stream gptel-stream)
(in-place (and (member "i" args) t))
(output-to-other-buffer-p)
(backend gptel-backend)
(model gptel-model)
(backend-name (gptel-backend-name gptel-backend))
(buffer) (position)
(callback) (gptel-buffer-name)
;; Input redirection: grab prompt from elsewhere?
(prompt
(cond
((member "p" args)
(read-string
(format "Ask %s: " (gptel-backend-name gptel-backend))
(apply #'buffer-substring-no-properties
(if (use-region-p)
(list (region-beginning) (region-end))
(list (line-beginning-position) (line-end-position))))))
((member "y" args)
(unless (car-safe kill-ring)
(user-error "`kill-ring' is empty! Nothing to send"))
(if current-prefix-arg
(read-from-kill-ring "Prompt from kill-ring: ")
(current-kill 0))))))
;; Output redirection: Send response elsewhere?
(cond
((member "m" args)
(setq stream nil)
(setq callback
(lambda (resp info)
(if resp
(message "%s response: %s" backend-name resp)
(message "%s response error: %s" backend-name (plist-get info :status))))))
((member "k" args)
(setq stream nil)
(setq callback
(lambda (resp info)
(if (not resp)
(message "%s response error: %s" backend-name (plist-get info :status))
(kill-new resp)
(message "%s response: \"%s\" copied to kill-ring."
backend-name
(truncate-string-to-width resp 30))))))
((setq gptel-buffer-name
(cl-some (lambda (s) (and (string-prefix-p "g" s)
(substring s 1)))
args))
(setq output-to-other-buffer-p t)
(let ((reduced-prompt ;For inserting into the gptel buffer as
;context, not the prompt used for the
;request itself
(or prompt
(if (use-region-p)
(buffer-substring-no-properties (region-beginning)
(region-end))
(buffer-substring-no-properties
(save-excursion
(text-property-search-backward
'gptel 'response
(when (get-char-property (max (point-min) (1- (point)))
'gptel)
t))
(point))
(gptel--at-word-end (point)))))))
(cond
((buffer-live-p (get-buffer gptel-buffer-name))
;; Insert into existing gptel session
(progn
(setq buffer (get-buffer gptel-buffer-name))
(with-current-buffer buffer
(goto-char (point-max))
(unless (or buffer-read-only
(get-char-property (point) 'read-only))
(insert reduced-prompt))
(setq position (point))
(when gptel-mode
(gptel--update-status " Waiting..." 'warning)))))
;; Insert into new gptel session
(t (setq buffer
(gptel gptel-buffer-name
(condition-case nil
(gptel--get-api-key)
((error user-error)
(setq gptel-api-key
(read-passwd
(format "%s API key: "
(gptel-backend-name
gptel-backend))))))
reduced-prompt))
;; Set backend and model in new session from current buffer
(with-current-buffer buffer
(setq gptel-backend backend)
(setq gptel-model model)
(gptel--update-status " Waiting..." 'warning)
(setq position (point)))))))
((setq gptel-buffer-name
(cl-some (lambda (s) (and (string-prefix-p "b" s)
(substring s 1)))
args))
(setq output-to-other-buffer-p t)
(setq buffer (get-buffer-create gptel-buffer-name))
(with-current-buffer buffer (setq position (point)))))
;; Create prompt, unless doing input-redirection above
(unless prompt
(setq prompt (gptel--create-prompt (gptel--at-word-end (point)))))
(when in-place
;; Kill the latest prompt
(let ((beg
(if (use-region-p)
(region-beginning)
(save-excursion
(text-property-search-backward
'gptel 'response
(when (get-char-property (max (point-min) (1- (point)))
'gptel)
t))
(point))))
(end (if (use-region-p) (region-end) (point))))
(kill-region beg end)))
(gptel-request
prompt
:buffer (or buffer (current-buffer))
:position position
:in-place (and in-place (not output-to-other-buffer-p))
:stream stream
:callback callback)
(when output-to-other-buffer-p
(message (concat "Prompt sent to buffer: "
(propertize gptel-buffer-name 'face 'help-key-binding)))
(display-buffer
buffer '((display-buffer-reuse-window
display-buffer-pop-up-window)
(reusable-frames . visible))))))
;; ** Set system message
(defun gptel--read-crowdsourced-prompt ()
"Pick a crowdsourced system prompt for gptel.
This uses the prompts in the variable
`gptel--crowdsourced-prompts', which see."
(interactive)
(if (not (hash-table-empty-p (gptel--crowdsourced-prompts)))
(let ((choice
(completing-read
"Pick and edit prompt: "
(lambda (str pred action)
(if (eq action 'metadata)
`(metadata
(affixation-function .
(lambda (cands)
(mapcar
(lambda (c)
(list c ""
(concat (propertize " " 'display '(space :align-to 22))
" " (propertize (gethash c gptel--crowdsourced-prompts)
'face 'completions-annotations))))
cands))))
(complete-with-action action gptel--crowdsourced-prompts str pred)))
nil t)))
(when-let ((prompt (gethash choice gptel--crowdsourced-prompts)))
(setq gptel--system-message prompt)
(call-interactively #'gptel--suffix-system-message)))
(message "No prompts available.")))
(transient-define-suffix gptel--suffix-system-message ()
"Set directives sent to ChatGPT."
:transient 'transient--do-exit
:description "Set custom directives"
:key "h"
(interactive)
(let ((orig-buf (current-buffer))
(msg-start (make-marker)))
(with-current-buffer (get-buffer-create "*gptel-system*")
(let ((inhibit-read-only t))
(erase-buffer)
(text-mode)
(setq header-line-format
(concat
"Edit your system message below and press "
(propertize "C-c C-c" 'face 'help-key-binding)
" when ready, or "
(propertize "C-c C-k" 'face 'help-key-binding)
" to abort."))
(insert
"# Example: You are a helpful assistant. Answer as concisely as possible.\n"
"# Example: Reply only with shell commands and no prose.\n"
"# Example: You are a poet. Reply only in verse.\n\n")
(add-text-properties
(point-min) (1- (point))
(list 'read-only t 'face 'font-lock-comment-face))
;; TODO: make-separator-line requires Emacs 28.1+.
;; (insert (propertize (make-separator-line) 'rear-nonsticky t))
(set-marker msg-start (point))
(save-excursion
(insert (buffer-local-value 'gptel--system-message orig-buf))
(push-mark nil 'nomsg))
(activate-mark))
(display-buffer (current-buffer)
`((display-buffer-below-selected)
(body-function . ,#'select-window)
(window-height . ,#'fit-window-to-buffer)))
(let ((quit-to-menu
(lambda ()
(interactive)
(local-unset-key (kbd "C-c C-c"))
(local-unset-key (kbd "C-c C-k"))
(quit-window)
(display-buffer
orig-buf
`((display-buffer-reuse-window
display-buffer-use-some-window)
(body-function . ,#'select-window)))
(call-interactively #'gptel-menu))))
(local-set-key (kbd "C-c C-c")
(lambda ()
(interactive)
(let ((system-message
(buffer-substring msg-start (point-max))))
(with-current-buffer orig-buf
(setq gptel--system-message system-message)))
(funcall quit-to-menu)))
(local-set-key (kbd "C-c C-k") quit-to-menu)))))
;; ** Suffixes for rewriting/refactoring
(transient-define-suffix gptel--suffix-rewrite ()
"Rewrite or refactor region contents."
:key "r"
:description #'gptel--refactor-or-rewrite
(interactive)
(let* ((prompt (buffer-substring-no-properties
(region-beginning) (region-end)))
(stream gptel-stream)
(gptel--system-message gptel--rewrite-message))
(gptel-request prompt :stream stream)))
(transient-define-suffix gptel--suffix-rewrite-and-replace ()
"Refactor region contents and replace it."
:key "R"
:description (lambda () (concat (gptel--refactor-or-rewrite) " in place"))
(interactive)
(let* ((prompt (buffer-substring-no-properties
(region-beginning) (region-end)))
(stream gptel-stream)
(gptel--system-message gptel--rewrite-message))
(kill-region (region-beginning) (region-end))
(message "Original text saved to kill-ring.")
(gptel-request prompt :stream stream :in-place t)))
(transient-define-suffix gptel--suffix-rewrite-and-ediff (args)
"Refactoring or rewrite region contents and run Ediff."
:key "E"
:description (lambda () (concat (gptel--refactor-or-rewrite) " and Ediff"))
(interactive (list (transient-args transient-current-command)))
(letrec ((prompt (buffer-substring-no-properties
(region-beginning) (region-end)))
(gptel--system-message gptel--rewrite-message)
;; TODO: Technically we should save the window config at the time
;; `ediff-setup-hook' runs, but this will do for now.
(cwc (current-window-configuration))
(gptel--ediff-restore
(lambda ()
(when (window-configuration-p cwc)
(set-window-configuration cwc))
(remove-hook 'ediff-quit-hook gptel--ediff-restore))))
(message "Waiting for response... ")
(gptel-request
prompt
:context (cons (region-beginning) (region-end))
:callback
(lambda (response info)
(if (not response)
(message "ChatGPT response error: %s" (plist-get info :status))
(let* ((gptel-buffer (plist-get info :buffer))
(gptel-bounds (plist-get info :context))
(buffer-mode
(buffer-local-value 'major-mode gptel-buffer)))
(pcase-let ((`(,new-buf ,new-beg ,new-end)
(with-current-buffer (get-buffer-create "*gptel-rewrite-Region.B-*")
(let ((inhibit-read-only t))
(erase-buffer)
(funcall buffer-mode)
(insert response)
(goto-char (point-min))
(list (current-buffer) (point-min) (point-max))))))
(require 'ediff)
(add-hook 'ediff-quit-hook gptel--ediff-restore)
(apply
#'ediff-regions-internal
(get-buffer (ediff-make-cloned-buffer gptel-buffer "-Region.A-"))
(car gptel-bounds) (cdr gptel-bounds)
new-buf new-beg new-end
nil
(if (transient-arg-value "-w" args)
(list 'ediff-regions-wordwise 'word-wise nil)
(list 'ediff-regions-linewise nil nil))))))))))
(provide 'gptel-transient)
;;; gptel-transient.el ends here
;; Local Variables:
;; outline-regexp: "^;; \\*+"
;; eval: (outline-minor-mode 1)
;; End: