[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/gptel 4d4b61af94 259/273: gptel-transient: More robust dry
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/gptel 4d4b61af94 259/273: gptel-transient: More robust dry-run commands |
Date: |
Wed, 1 May 2024 10:02:49 -0400 (EDT) |
branch: elpa/gptel
commit 4d4b61af94153f26c879c27768e0b80c95eebf58
Author: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
gptel-transient: More robust dry-run commands
* gptel.el (gptel--inspect-query): `gptel--inspect-query` now
takes data to display as an argument. Reduce its function to
displaying a buffer with the data.
* gptel-transient.el (gptel-menu, gptel--suffix-send): Fold
dry-run the option into `gptel--suffix-send` and call it with a
dry-run flag instead of using an alternate pathway for dry-runs.
The "Inspect query" suffixes of `gptel-menu` now perform actual
dry-runs, avoiding issues like #276.
---
gptel-transient.el | 103 ++++++++++++++++++++++++++---------------------------
gptel.el | 38 ++++++++++----------
2 files changed, 69 insertions(+), 72 deletions(-)
diff --git a/gptel-transient.el b/gptel-transient.el
index ee7f381e5e..08d1cc3e6a 100644
--- a/gptel-transient.el
+++ b/gptel-transient.el
@@ -336,24 +336,19 @@ Also format its value in the Transient menu."
(lambda ()
"Inspect the query that will be sent as a lisp object."
(interactive)
- (let* ((extra (gptel--get-directive
- (transient-args
- transient-current-command)))
- (gptel--system-message
- (concat gptel--system-message extra)))
- (gptel--sanitize-model)
- (gptel--inspect-query))))
+ (gptel--sanitize-model)
+ (gptel--inspect-query
+ (gptel--suffix-send
+ (cons "I" (transient-args transient-current-command))))))
("J" "Inspect query (JSON)"
(lambda ()
"Inspect the query that will be sent as a JSON object."
(interactive)
- (let* ((extra (gptel--get-directive
- (transient-args
- transient-current-command)))
- (gptel--system-message
- (concat gptel--system-message extra)))
- (gptel--sanitize-model)
- (gptel--inspect-query 'json))))]]
+ (gptel--sanitize-model)
+ (gptel--inspect-query
+ (gptel--suffix-send
+ (cons "I" (transient-args transient-current-command)))
+ 'json)))]]
(interactive)
(gptel--sanitize-model)
(transient-setup 'gptel-menu))
@@ -594,6 +589,7 @@ Or in an extended conversation:
(buffer) (position)
(callback) (gptel-buffer-name)
(system-extra (gptel--get-directive args))
+ (dry-run (and (member "I" args) t))
;; Input redirection: grab prompt from elsewhere?
(prompt
(cond
@@ -689,44 +685,47 @@ Or in an extended conversation:
(setq buffer (get-buffer-create gptel-buffer-name))
(with-current-buffer buffer (setq position (point)))))
- (gptel-request
- prompt
- :buffer (or buffer (current-buffer))
- :position position
- :in-place (and in-place (not output-to-other-buffer-p))
- :stream stream
- :system (concat gptel--system-message system-extra)
- :callback callback)
-
- ;; NOTE: Possible future race condition here if Emacs ever drops the GIL.
- ;; The HTTP request callback might modify the buffer before the in-place
- ;; text is killed below.
- (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))))
- (unless output-to-other-buffer-p
- ;; store the killed text in gptel-history
- (gptel--attach-response-history
- (list (buffer-substring-no-properties beg end))))
- (kill-region beg end)))
-
- (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))))))
+ (prog1 (gptel-request prompt
+ :buffer (or buffer (current-buffer))
+ :position position
+ :in-place (and in-place (not output-to-other-buffer-p))
+ :stream stream
+ :system (concat gptel--system-message system-extra)
+ :callback callback
+ :dry-run dry-run)
+
+ ;; NOTE: Possible future race condition here if Emacs ever drops the GIL.
+ ;; The HTTP request callback might modify the buffer before the in-place
+ ;; text is killed below.
+ (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))))
+ (unless output-to-other-buffer-p
+ ;; store the killed text in gptel-history
+ (gptel--attach-response-history
+ (list (buffer-substring-no-properties beg end))))
+ (kill-region beg end)))
+
+ (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)))))))
+
+;; Allow calling from elisp
+(put 'gptel--suffix-send 'interactive-only nil)
;; ** Suffix to regenerate response
diff --git a/gptel.el b/gptel.el
index 160601dd73..fc98d9a525 100644
--- a/gptel.el
+++ b/gptel.el
@@ -931,29 +931,27 @@ waiting for the response."
(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.
+(defun gptel--inspect-query (request-data &optional arg)
+ "Show REQUEST-DATA, the full LLM query to be sent, in a buffer.
-This functions as a dry run of `gptel-send'. If prefix ARG is
+This functions as a dry run of `gptel-send'. If 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)))))
+ (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.
- [nongnu] elpa/gptel 7e6b106516 233/273: gptel-transient: Change menu display, (continued)
- [nongnu] elpa/gptel 7e6b106516 233/273: gptel-transient: Change menu display, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 26326c302e 249/273: gptel-anthropic: Parsing fix, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 22f7043c32 242/273: gptel: Fix gptel-end-of-response, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 94b13e78ec 239/273: gptel-transient: enable additional directive by default, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel f24ec164cd 251/273: gptel: Adjust doc-string, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 5d74ec4de0 254/273: gptel: Set system message correctly in gptel-request, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 53ee34653e 252/273: gptel-openai: Typo in gptel--json-read, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel f2fd2b13b0 257/273: gptel-org: Move response transform code for Org, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel e994a443d3 262/273: README: add OpenRouter instructions (#282), ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 7b6e3c5900 260/273: gptel: Release v0.8.5, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 4d4b61af94 259/273: gptel-transient: More robust dry-run commands,
ELPA Syncer <=
- [nongnu] elpa/gptel 70889ad95c 263/273: gptel-gemini: Add Gemini 1.5 (#284), ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 4273f067e8 271/273: gptel-org: Improve stream converter, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel c319966997 272/273: gptel-org: Further improve stream converter, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 040baad910 034/273: gptel: Remove aio dependency, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 1828dd3fa4 050/273: gptel: Set "waiting" state after sending the prompt, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel c9795fe9e8 060/273: gptel: org support for streaming WIP, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 644fc1de2f 118/273: gptel-transient: Handle empty input when setting temperature, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 3c01477c37 129/273: gptel: api-key shenanigans, ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel 644e341244 141/273: Add multiline prefixes & AI response prefixes (#142), ELPA Syncer, 2024/05/01
- [nongnu] elpa/gptel a202911009 148/273: gptel: Add post-stream hook, scroll commands, ELPA Syncer, 2024/05/01