Skip to content

Commit efc4de2

Browse files
committed
gptel: Make async query payloads inspectable
* gptel.el (gptel--inspect-query): If there are async prompt augmentors/transformers in the mix, the fsm passed to `gptel--inspect-query' by gptel-request will not yet be ready for consumption. Poll until payload construction is complete. This is an interim solution while I figure out how to modify the call chain to make this expensive timer unnecessary.
1 parent 079d9cd commit efc4de2

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

gptel.el

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,38 +2625,48 @@ REQUEST-FSM is the state of the request, as returned by
26252625
`gptel-request'. If FORMAT is the symbol json, show the encoded
26262626
JSON query instead of the Lisp structure gptel uses."
26272627
(unless request-fsm (setq request-fsm gptel--fsm-last))
2628-
(with-current-buffer (get-buffer-create "*gptel-query*")
2629-
(let* ((standard-output (current-buffer))
2630-
(inhibit-read-only t)
2631-
(request-data
2632-
(plist-get (gptel-fsm-info request-fsm) :data)))
2633-
(buffer-disable-undo)
2634-
(erase-buffer)
2635-
(if (eq format 'json)
2636-
(progn (fundamental-mode)
2637-
(insert (gptel--json-encode request-data))
2638-
(json-pretty-print-buffer))
2639-
(lisp-data-mode)
2640-
(prin1 request-data)
2641-
(pp-buffer))
2642-
(setq-local gptel--fsm-last request-fsm)
2643-
(goto-char (point-min))
2644-
(view-mode 1)
2645-
(setq buffer-undo-list nil)
2646-
(use-local-map
2647-
(make-composed-keymap
2648-
(define-keymap
2649-
"C-c C-c" #'gptel--continue-query
2650-
"C-c C-k" #'quit-window)
2651-
(current-local-map)))
2652-
(unless header-line-format
2653-
(setq header-line-format
2654-
(substitute-command-keys
2655-
(concat
2656-
"Edit request: \\[read-only-mode],"
2657-
" Send request: \\[gptel--continue-query],"
2658-
" Quit: \\[quit-window]"))))
2659-
(display-buffer (current-buffer) gptel-display-buffer-action))))
2628+
(if (bufferp (plist-get (gptel-fsm-info request-fsm) :data))
2629+
(letrec ((dry-run-poll
2630+
(run-with-timer
2631+
0 1 (lambda (fsm form)
2632+
(unless (bufferp (plist-get (gptel-fsm-info fsm) :data))
2633+
(cancel-timer dry-run-poll)
2634+
(gptel--inspect-query fsm form)))
2635+
request-fsm format))))
2636+
(with-current-buffer (plist-get (gptel-fsm-info request-fsm) :buffer)
2637+
(gptel--update-status " Ready" 'success))
2638+
(with-current-buffer (get-buffer-create "*gptel-query*")
2639+
(let* ((standard-output (current-buffer))
2640+
(inhibit-read-only t)
2641+
(request-data
2642+
(plist-get (gptel-fsm-info request-fsm) :data)))
2643+
(buffer-disable-undo)
2644+
(erase-buffer)
2645+
(if (eq format 'json)
2646+
(progn (fundamental-mode)
2647+
(insert (gptel--json-encode request-data))
2648+
(json-pretty-print-buffer))
2649+
(lisp-data-mode)
2650+
(prin1 request-data)
2651+
(pp-buffer))
2652+
(setq-local gptel--fsm-last request-fsm)
2653+
(goto-char (point-min))
2654+
(view-mode 1)
2655+
(setq buffer-undo-list nil)
2656+
(use-local-map
2657+
(make-composed-keymap
2658+
(define-keymap
2659+
"C-c C-c" #'gptel--continue-query
2660+
"C-c C-k" #'quit-window)
2661+
(current-local-map)))
2662+
(unless header-line-format
2663+
(setq header-line-format
2664+
(substitute-command-keys
2665+
(concat
2666+
"Edit request: \\[read-only-mode],"
2667+
" Send request: \\[gptel--continue-query],"
2668+
" Quit: \\[quit-window]"))))
2669+
(display-buffer (current-buffer) gptel-display-buffer-action)))))
26602670

26612671
(defun gptel--continue-query ()
26622672
"Continue sending the gptel query displayed in this buffer.

0 commit comments

Comments
 (0)