emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/gptel a32f4effe5 215/273: gptel-curl: Handle empty respons


From: ELPA Syncer
Subject: [nongnu] elpa/gptel a32f4effe5 215/273: gptel-curl: Handle empty responses correctly
Date: Wed, 1 May 2024 10:02:25 -0400 (EDT)

branch: elpa/gptel
commit a32f4effe5bb659d986cb376a48400db07820202
Author: Karthik Chikmagalur <karthikchikmagalur@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmagalur@gmail.com>

    gptel-curl: Handle empty responses correctly
    
    gptel-curl.el (gptel-curl--stream-cleanup): The LLM response can
    be empty with HTTP status 200, for example when the API responds
    with an error description instead.  Handle this case gracefully.
    When `gptel-mode` is enabled, also inform the user that the
    response was empty.  Fix #234.
    
    gptel.el (gptel-post-response-functions): Documentation.  Explain
    the arguments passed to each hook function in this hook when the
    response fails or is empty.
---
 gptel-curl.el | 27 +++++++++++++--------------
 gptel.el      |  4 +++-
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/gptel-curl.el b/gptel-curl.el
index bb7fc39a49..4c4215a831 100644
--- a/gptel-curl.el
+++ b/gptel-curl.el
@@ -199,20 +199,17 @@ PROCESS and _STATUS are process parameters."
            (tracking-marker (plist-get info :tracking-marker))
            (start-marker (plist-get info :position))
            (http-status (plist-get info :http-status))
-           (http-msg (plist-get info :status))
-           response-beg response-end)
+           (http-msg (plist-get info :status)))
       (when gptel-log-level (gptel-curl--log-response proc-buf info)) ;logging
-      (if (equal http-status "200")
-          (progn
-            ;; Finish handling response
-            (with-current-buffer (marker-buffer start-marker)
-              (setq response-beg (marker-position start-marker)
-                    response-end (marker-position tracking-marker))
-              (pulse-momentary-highlight-region response-beg tracking-marker)
-              (when gptel-mode (save-excursion (goto-char tracking-marker)
-                                               (insert "\n\n" 
(gptel-prompt-prefix-string)))))
-            (with-current-buffer gptel-buffer
-              (when gptel-mode (gptel--update-status  " Ready" 'success))))
+      (if (equal http-status "200")                                   ;Finish 
handling response
+          (with-current-buffer gptel-buffer
+            (if (not tracking-marker)   ;Empty response
+                (when gptel-mode (gptel--update-status " Empty response" 
'success))
+              (pulse-momentary-highlight-region start-marker tracking-marker)
+              (when gptel-mode
+                (save-excursion (goto-char tracking-marker)
+                                (insert "\n\n" (gptel-prompt-prefix-string)))
+                (gptel--update-status  " Ready" 'success))))
         ;; Or Capture error message
         (with-current-buffer proc-buf
           (goto-char (point-max))
@@ -240,7 +237,9 @@ PROCESS and _STATUS are process parameters."
             (gptel--update-status
              (format " Response Error: %s" http-msg) 'error))))
       (with-current-buffer gptel-buffer
-        (run-hook-with-args 'gptel-post-response-functions response-beg 
response-end)))
+        (run-hook-with-args 'gptel-post-response-functions
+                            (marker-position start-marker)
+                            (marker-position (or tracking-marker 
start-marker)))))
     (setf (alist-get process gptel-curl--process-alist nil 'remove) nil)
     (kill-buffer proc-buf)))
 
diff --git a/gptel.el b/gptel.el
index 2b9cce7990..2cc360e9b1 100644
--- a/gptel.el
+++ b/gptel.el
@@ -250,7 +250,9 @@ to the LLM, and after the full response has been inserted.  
Each
 function is called with two arguments: the response beginning and
 end positions.
 
-Note: this hook runs even if the request fails."
+Note: this hook runs even if the request fails.  In this case the
+response beginning and end positions are both the cursor position
+at the time of the request."
   :group 'gptel
   :type 'hook)
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]