Function: gptel-curl-get-response
gptel-curl-get-response is a natively compiled function defined in
gptel-request.el.
Signature
(gptel-curl-get-response FSM)
Documentation
Fetch response to prompt in state FSM from the LLM using Curl.
FSM is the state machine driving this request.
FSM is the state machine driving this request. Its INFO slot
contains the data required for setting up the request. INFO is a
plist with the following keys, among others:
- :data (the data being sent)
- :buffer (the gptel buffer)
- :position (marker at which to insert the response).
- :callback (optional, the request callback)
Call CALLBACK with the response and INFO afterwards. If omitted the response is inserted into the current buffer after point.
Source Code
;; Defined in /nix/store/q3r7g81pvghw7a523kjk2k2zih3pfk8z-emacs-packages-deps/share/emacs/site-lisp/elpa/gptel-20261002.545/gptel-request.el
;;;###autoload
(defun gptel-curl-get-response (fsm)
"Fetch response to prompt in state FSM from the LLM using Curl.
FSM is the state machine driving this request.
FSM is the state machine driving this request. Its INFO slot
contains the data required for setting up the request. INFO is a
plist with the following keys, among others:
- :data (the data being sent)
- :buffer (the gptel buffer)
- :position (marker at which to insert the response).
- :callback (optional, the request callback)
Call CALLBACK with the response and INFO afterwards. If omitted
the response is inserted into the current buffer after point."
(let* ((uuid (md5 (format "%s%s%s%s" (random)
(emacs-pid) (user-full-name) (recent-keys))))
(info (gptel-fsm-info fsm))
(backend (plist-get info :backend))
(stream (plist-get info :stream))
(command (nconc (list (gptel--curl-path))
(gptel-curl--get-config-args info)
(list "--config" "-")))
(process (make-process
:name "gptel-curl"
:buffer (gptel--temp-buffer " *gptel-curl*")
:command command
:connection-type 'pipe)))
(when (eq gptel-log-level 'debug)
(gptel--log (format "%s" command) "request command"))
(with-current-buffer (process-buffer process)
(cond
((eq (gptel-backend-coding-system backend) 'binary)
;; set-buffer-file-coding-system is not needed since we don't save this buffer
(set-buffer-multibyte nil)
(set-process-coding-system process 'binary 'binary))
(t
;; Don't try to convert cr-lf to cr on Windows so that curl's "header size
;; in bytes" stays correct. Explicitly set utf-8 for non-win systems too,
;; for cases when buffer coding system is not set to utf-8.
(set-process-coding-system process 'utf-8-unix 'utf-8-unix)))
(set-process-query-on-exit-flag process nil)
(process-send-string process (gptel-curl--get-config info uuid))
(process-send-eof process)
(if (plist-get info :uuid) ;not the first run, set only the uuid
(plist-put info :uuid uuid)
(setf (gptel-fsm-info fsm) ;fist run, set all process parameters
(nconc (list :uuid uuid
:transformer
(when (with-current-buffer (plist-get info :buffer)
(and (derived-mode-p 'org-mode)
gptel-org-convert-response))
(gptel--stream-convert-markdown->org
(plist-get info :position))))
(unless (plist-get info :callback)
(list :callback (if stream
#'gptel-curl--stream-insert-response
#'gptel--insert-response)))
info)))
(if stream
(progn (set-process-sentinel process #'gptel-curl--stream-cleanup)
(set-process-filter process #'gptel-curl--stream-filter))
(set-process-sentinel process #'gptel-curl--sentinel))
(setf (alist-get process gptel--request-alist)
(cons fsm
#'(lambda ()
;; Clean up Curl process
(set-process-sentinel process #'ignore)
(delete-process process)
(kill-buffer (process-buffer process))))))))