From 199595b0c84e54719d12d940896d9b8704bef1fd Mon Sep 17 00:00:00 2001 From: Karthik Chikmagalur Date: Thu, 7 Mar 2024 10:40:35 -0800 Subject: [PATCH] gptel: Handle status HTTP 100 gptel.el (gptel--url-parse-response): Handle HTTP 100 followed by 200. Note: this fix is brittle, it will break if 100 is followed by an error code. gptel-curl.el (gptel-curl--stream-filter, gptel-curl--parse-stream): Ditto. Address #194. --- gptel-curl.el | 10 ++++++---- gptel.el | 4 +++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/gptel-curl.el b/gptel-curl.el index 4c4215a..764835f 100644 --- a/gptel-curl.el +++ b/gptel-curl.el @@ -201,7 +201,7 @@ PROCESS and _STATUS are process parameters." (http-status (plist-get info :http-status)) (http-msg (plist-get info :status))) (when gptel-log-level (gptel-curl--log-response proc-buf info)) ;logging - (if (equal http-status "200") ;Finish handling response + (if (member http-status '("200" "100")) ;Finish handling response (with-current-buffer gptel-buffer (if (not tracking-marker) ;Empty response (when gptel-mode (gptel--update-status " Empty response" 'success)) @@ -315,7 +315,7 @@ See `gptel--url-get-response' for details." display-buffer-pop-up-window) (reusable-frames . visible)))) ;; Run pre-response hook - (when (and (equal (plist-get proc-info :http-status) "200") + (when (and (member (plist-get proc-info :http-status) '("200" "100")) gptel-pre-response-hook) (with-current-buffer (marker-buffer (plist-get proc-info :position)) (run-hooks 'gptel-pre-response-hook)))) @@ -323,7 +323,8 @@ See `gptel--url-get-response' for details." (when-let ((http-msg (plist-get proc-info :status)) (http-status (plist-get proc-info :http-status))) ;; Find data chunk(s) and run callback - (when-let (((equal http-status "200")) + ;; FIXME Handle the case where HTTP 100 is followed by HTTP (not 200) BUG #194 + (when-let (((member http-status '("200" "100"))) (response (funcall (plist-get proc-info :parser) nil proc-info)) ((not (equal response "")))) (funcall (or (plist-get proc-info :callback) @@ -385,7 +386,8 @@ PROC-INFO is a plist with contextual information." (json-read) (json-readtable-error 'json-read-error))))) (cond - ((equal http-status "200") + ;; FIXME Handle the case where HTTP 100 is followed by HTTP (not 200) BUG #194 + ((member http-status '("200" "100")) (list (string-trim (funcall parser nil response proc-info)) http-msg)) diff --git a/gptel.el b/gptel.el index 2cc360e..cbd5094 100644 --- a/gptel.el +++ b/gptel.el @@ -1183,7 +1183,9 @@ See `gptel-curl--get-response' for its contents.") (json-read-from-string json-str) (json-readtable-error 'json-read-error)))))) (cond - ((or (= url-http-response-status 200) (string-match-p "200 OK" http-msg)) + ;; FIXME Handle the case where HTTP 100 is followed by HTTP (not 200) BUG #194 + ((or (memq url-http-response-status '(200 100)) + (string-match-p "\\(?:1\\|2\\)00 OK" http-msg)) (list (string-trim (gptel--parse-response backend response '(:buffer response-buffer))) http-msg))