Function: gptel-curl--stream-cleanup

gptel-curl--stream-cleanup is a natively compiled function defined in gptel-request.el.

Signature

(gptel-curl--stream-cleanup PROCESS STATUS)

Documentation

Process sentinel for gptel curl requests.

PROCESS and _STATUS are process parameters.

Source Code

;; Defined in /nix/store/q3r7g81pvghw7a523kjk2k2zih3pfk8z-emacs-packages-deps/share/emacs/site-lisp/elpa/gptel-20261002.545/gptel-request.el
;; TODO: Separate user-messaging from this function
(defun gptel-curl--stream-cleanup (process _status)
  "Process sentinel for gptel curl requests.

PROCESS and _STATUS are process parameters."
  (let ((proc-buf (process-buffer process))
        (exit-status (process-exit-status process)))
    (let* ((fsm (car (alist-get process gptel--request-alist)))
           (info (gptel-fsm-info fsm))
           (http-status (plist-get info :http-status)))
      (when gptel-log-level (gptel-curl--log-response proc-buf info)) ;logging
      (cond
       ;; Curl exited with a non-zero status: connection-level failure
       ((not (zerop exit-status))
        (plist-put info :error
                   (format "Curl failed with exit code %d. See Curl manpage for details."
                           exit-status))
        (plist-put info :status "Curl failure")
        ;; MAYBE: This transition should happen in the process filter, but it's
        ;; not clear how to reliably detect Curl failure there.
        (gptel--fsm-transition fsm)     ;Curl failed, WAIT -> TYPE
        (with-demoted-errors "gptel callback error: %S"
          (funcall (plist-get info :callback) nil info)))
       ;; Finish handling a successful streaming response
       ((member http-status '("200" "100"))
        (with-demoted-errors "gptel callback error: %S"
          (funcall (plist-get info :callback) t info)))
       ;; Capture error message from HTTP error response
       (t
        (with-current-buffer proc-buf
          (goto-char (point-max))
          (if (not (search-backward (plist-get info :uuid) nil t))
              (plist-put info :error "Could not parse Curl response")
            (backward-char)
            (pcase-let* ((`(,_ . ,header-size) (read (current-buffer)))
                         (response (progn (goto-char header-size)
                                          (condition-case nil (gptel--json-read)
                                            (error 'json-read-error))))
                         (error-data (gptel--parse-response-error response)))
              (cond
               (error-data
                (plist-put info :error error-data))
               ((eq response 'json-read-error)
                (plist-put info :error "Malformed JSON in response."))
               (t (plist-put info :error "Could not parse HTTP response."))))))
        (with-demoted-errors "gptel callback error: %S"
          (funcall (plist-get info :callback) nil info))))
      (gptel--fsm-transition fsm))      ; Move to next state
    (setf (alist-get process gptel--request-alist nil 'remove) nil)
    (kill-buffer proc-buf)))