branch: elpa/gptel
commit 0467b3c45309819593ef71709b16590596678239
Author: Felipe Ochoa <fel...@incquery.com>
Commit: karthink <karthikchikmaga...@gmail.com>

    gptel: Allow Curl processes to use binary coding system
    
    This is so the bedrock implementation can accept responses using
    vnd.amazon.event-stream.
    
    * gptel-curl.el (gptel-curl-get-response): Allow
    `process-coding-system' to be binary.
    * gptel-openai.el (gptel-backend): Add a coding-system field to
    the gptel-backend slot.
---
 gptel-curl.el   | 15 +++++++++++----
 gptel-openai.el |  3 ++-
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/gptel-curl.el b/gptel-curl.el
index d887c9d8a3..a421fff6b6 100644
--- a/gptel-curl.el
+++ b/gptel-curl.el
@@ -134,18 +134,25 @@ the response is inserted into the current buffer after 
point."
                              (random) (emacs-pid) (user-full-name)
                              (recent-keys))))
          (info (gptel-fsm-info fsm))
+         (backend (plist-get info :backend))
          (args (gptel-curl--get-args info token))
          (stream (plist-get info :stream))
          (process (apply #'start-process "gptel-curl"
                          (gptel--temp-buffer " *gptel-curl*") 
(gptel--curl-path) args)))
-    ;; 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)
     (when (eq gptel-log-level 'debug)
       (gptel--log (mapconcat #'shell-quote-argument (cons (gptel--curl-path) 
args) " \\\n")
                   "request Curl command" 'no-json))
     (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)
       (if (plist-get info :token)       ;not the first run, set only the token
           (plist-put info :token token)
diff --git a/gptel-openai.el b/gptel-openai.el
index 69f3ce7cd0..5f4b214aeb 100644
--- a/gptel-openai.el
+++ b/gptel-openai.el
@@ -151,7 +151,8 @@ Throw an error if there is no match."
                    (:copier gptel--copy-backend))
   name host header protocol stream
   endpoint key models url request-params
-  curl-args)
+  curl-args
+  (coding-system nil :documentation "Can be set to `binary' if the backend 
expects non UTF-8 output."))
 
 ;;; OpenAI (ChatGPT)
 (cl-defstruct (gptel-openai (:constructor gptel--make-openai)

Reply via email to