Skip to content

Commit

Permalink
gptel: Handle status HTTP 100
Browse files Browse the repository at this point in the history
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 karthink#194.
  • Loading branch information
karthink committed Mar 7, 2024
1 parent a32f4ef commit 199595b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 6 additions & 4 deletions gptel-curl.el
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -315,15 +315,16 @@ 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))))

(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)
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion gptel.el
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 199595b

Please sign in to comment.