Function: gptel-curl--sentinel

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

Signature

(gptel-curl--sentinel 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
(defun gptel-curl--sentinel (process _status)
  "Process sentinel for gptel curl requests.

PROCESS and _STATUS are process parameters."
  (let ((proc-buf (process-buffer process)))
    (when-let* (((eq (process-status process) 'exit))
                (fsm (car (alist-get process gptel--request-alist)))
                (proc-info (gptel-fsm-info fsm))
                (proc-callback (plist-get proc-info :callback)))
      (when gptel-log-level (gptel-curl--log-response proc-buf proc-info)) ;logging
      (let ((exit-status (process-exit-status process)))
        (if (zerop exit-status)
            (pcase-let ((`(,response ,http-status ,http-msg ,error)
                         (with-current-buffer proc-buf
                           (gptel-curl--parse-response proc-info))))
              (plist-put proc-info :http-status http-status)
              (plist-put proc-info :status http-msg)
              (gptel--fsm-transition fsm) ;WAIT -> TYPE
              (when error (plist-put proc-info :error error))
              ;; Look for a reasoning block
              (if (and (stringp response) (string-match-p "^\\s-*<think>" response))
                  (when-let* ((idx (string-search "</think>" response)))
                    (with-demoted-errors "gptel callback error: %S"
                      (funcall proc-callback
                               (cons 'reasoning (substring response nil (+ idx 8)))
                               proc-info))
                    (setq response
                          (string-trim-left (substring response (+ idx 8)))))
                (when-let* ((reasoning (plist-get proc-info :reasoning))
                            ((stringp reasoning)))
                  (funcall proc-callback (cons 'reasoning reasoning) proc-info)))
              ;; Call callback with response text
              (when (or response (not (member http-status '("200" "100"))))
                (with-demoted-errors "gptel callback error: %S"
                  (funcall proc-callback response proc-info))))
          ;; Curl exited with a non-zero status: connection-level failure
          (plist-put proc-info :error
                     (format "Curl failed with exit code %d. See Curl manpage for details."
                             exit-status))
          (plist-put proc-info :status "Curl failure")
          (gptel--fsm-transition fsm)   ;WAIT -> TYPE
          (with-demoted-errors "gptel callback error: %S"
            (funcall proc-callback nil proc-info))))
      (gptel--fsm-transition fsm))      ;TYPE -> next
    (setf (alist-get process gptel--request-alist nil 'remove) nil)
    (kill-buffer proc-buf)))