gptel: Improve logging

* gptel.el (gptel--url-get-response): Log request headers as JSON objects.

* gptel-curl.el (gptel-curl--get-args, gptel-curl--get-response):
Log request headers as JSON objects, Curl command as a shell
command you can copy and paste into the terminal.
This commit is contained in:
Karthik Chikmagalur 2024-03-13 03:17:11 -07:00
parent f58ad9435c
commit e18ceb1f84
2 changed files with 10 additions and 4 deletions

View file

@ -64,7 +64,10 @@ PROMPTS is the data to send, TOKEN is a unique identifier."
(funcall header) header)))))
(when gptel-log-level
(when (eq gptel-log-level 'debug)
(gptel--log (gptel--json-encode headers) "request headers"))
(gptel--log (gptel--json-encode
(mapcar (lambda (pair) (cons (intern (car pair)) (cdr pair)))
headers))
"request headers"))
(gptel--log data "request body"))
(append
gptel-curl--common-args
@ -110,8 +113,8 @@ the response is inserted into the current buffer after point."
(process (apply #'start-process "gptel-curl"
(generate-new-buffer "*gptel-curl*") "curl" args)))
(when (eq gptel-log-level 'debug)
(gptel--log (gptel--json-encode (cons "curl" args))
"request Curl command"))
(gptel--log (mapconcat #'shell-quote-argument (cons "curl" args) " \\\n")
"request Curl command" 'no-json))
(with-current-buffer (process-buffer process)
(set-process-query-on-exit-flag process nil)
(setf (alist-get process gptel-curl--process-alist)

View file

@ -1135,7 +1135,10 @@ the response is inserted into the current buffer after point."
'utf-8)))
(when gptel-log-level ;logging
(when (eq gptel-log-level 'debug)
(gptel--log (gptel--json-encode url-request-extra-headers) "request headers"))
(gptel--log (gptel--json-encode
(mapcar (lambda (pair) (cons (intern (car pair)) (cdr pair)))
url-request-extra-headers))
"request headers"))
(gptel--log url-request-data "request body"))
(url-retrieve (let ((backend-url (gptel-backend-url gptel-backend)))
(if (functionp backend-url)