diff --git a/gptel-transient.el b/gptel-transient.el index 971711c..119c5ad 100644 --- a/gptel-transient.el +++ b/gptel-transient.el @@ -174,7 +174,20 @@ which see." :if gptel--at-response-history-p :transient t) ("E" "Ediff previous" gptel--ediff - :if gptel--at-response-history-p)]] + :if gptel--at-response-history-p)] + ["Inspect" + ("I" "Query as Lisp" + (lambda () + "Inspect the query that will be sent as a lisp object." + (interactive) + (gptel--sanitize-model) + (gptel--inspect-query))) + ("J" "Query as JSON" + (lambda () + "Inspect the query that will be sent as a JSON object." + (interactive) + (gptel--sanitize-model) + (gptel--inspect-query 'json)))]] (interactive) (gptel--sanitize-model) (transient-setup 'gptel-menu)) diff --git a/gptel.el b/gptel.el index a75d905..bdf89d1 100644 --- a/gptel.el +++ b/gptel.el @@ -955,6 +955,31 @@ waiting for the response." (gptel-request nil :stream gptel-stream) (gptel--update-status " Waiting..." 'warning))) +(declare-function json-pretty-print-buffer "json") +(defun gptel--inspect-query (&optional arg) + "Show the full LLM query to be sent in a new buffer. + +This functions as a dry run of `gptel-send'. If prefix ARG is +the symbol json, show the encoded JSON query instead of the lisp +structure gptel uses." + (let* ((request-data + (gptel-request nil :stream gptel-stream :dry-run t))) + (with-current-buffer (get-buffer-create "*gptel-query*") + (let ((standard-output (current-buffer)) + (inhibit-read-only t)) + (buffer-disable-undo) + (erase-buffer) + (if (eq arg 'json) + (progn (fundamental-mode) + (insert (gptel--json-encode request-data)) + (json-pretty-print-buffer)) + (lisp-data-mode) + (prin1 request-data) + (pp-buffer)) + (goto-char (point-min)) + (view-mode 1) + (display-buffer (current-buffer) gptel-display-buffer-action))))) + (defun gptel--insert-response (response info) "Insert the LLM RESPONSE into the gptel buffer.