Encode a lambda-provided API key as utf-8

If the user provides the OpenAPI key via a function, as is the case by
default if the user puts credentials in an auth-sources resource like
.authinfo or .authinfo.gpg, then it is necessary to encode the
function's returned value into utf-8 before passing it onward to build
the HTTP request. This commit ensures that happen.

Why is this necessary, given that the API key contains only
alphanumeric characters and therefore should be byte-for-byte the same
in utf-8 as in us-acii? I don't know. It may because emacs's
url-http.el library concats many strings together, and they all need
to be identically encoded before they can be combined correctly.

Whatever the reason, this fix works and allows you to send prompts
which include Unicode characters that require multibyte encodings in UTF-8
This commit is contained in:
algal 2023-03-30 17:34:11 -07:00 committed by karthink
parent 1c07a94e18
commit a500c76a68

View file

@ -202,7 +202,9 @@ By default, \"openai.com\" is used as HOST and \"apikey\" as USER."
:host (or host "openai.com")
:user (or user "apikey")))
:secret)))
(if (functionp secret) (funcall secret) secret)
(if (functionp secret)
(encode-coding-string (funcall secret) 'utf-8)
secret)
(user-error "No `gptel-api-key' found in the auth source")))
(defun gptel--api-key ()