Refactor code around the curl call
This makes passing input data and setting flags more robust.
This commit is contained in:
parent
0105a07695
commit
11865f92cf
1 changed files with 22 additions and 24 deletions
34
kagi.el
34
kagi.el
|
@ -77,22 +77,17 @@ https://kagi.com/settings?p=api"
|
||||||
:type 'boolean
|
:type 'boolean
|
||||||
:group 'kagi)
|
:group 'kagi)
|
||||||
|
|
||||||
(defun kagi--build-curl-command (prompt)
|
(defun kagi--curl-flags ()
|
||||||
"Build a `curl' command to call the Kagi FastGPT API.
|
"Collect flags for a `curl' command to call the Kagi FastGPT API."
|
||||||
|
(let ((token (cond ((functionp kagi-api-token) (funcall kagi-api-token))
|
||||||
PROMPT is used to fill in the POST part of the request."
|
|
||||||
(let* ((input-obj `((query . ,prompt)))
|
|
||||||
(data (json-encode input-obj))
|
|
||||||
(token (cond ((functionp kagi-api-token) (funcall kagi-api-token))
|
|
||||||
((stringp kagi-api-token) kagi-api-token)
|
((stringp kagi-api-token) kagi-api-token)
|
||||||
(t (error "No API token configured in variable kagi-api-token"))))
|
(t (error "No API token configured in variable kagi-api-token")))))
|
||||||
(curl-flags `("--silent"
|
`("--silent"
|
||||||
,(format "--header \"Authorization: Bot %s\"" token)
|
"--header" ,(format "Authorization: Bot %s" token)
|
||||||
"--header \"Content-Type: application/json\""
|
"--header" "Content-Type: application/json"
|
||||||
,(format "--data '%s'" data))))
|
"--data" "@-"
|
||||||
(format "curl %s %s"
|
,kagi-api-fastgpt-url
|
||||||
(string-join curl-flags " ")
|
)))
|
||||||
kagi-api-fastgpt-url)))
|
|
||||||
|
|
||||||
(defun kagi--html-bold-to-face (string)
|
(defun kagi--html-bold-to-face (string)
|
||||||
"Convert HTML tags inside STRING to faces.
|
"Convert HTML tags inside STRING to faces.
|
||||||
|
@ -148,15 +143,18 @@ PROMPT is used to fill in the POST part of the request."
|
||||||
`kagi--canned-response', no remote call will be made. This is useful
|
`kagi--canned-response', no remote call will be made. This is useful
|
||||||
for development purposes as API calls require a charge your API
|
for development purposes as API calls require a charge your API
|
||||||
credit at Kagi."
|
credit at Kagi."
|
||||||
(let ((command (kagi--build-curl-command prompt)))
|
|
||||||
(if kagi-debug
|
(if kagi-debug
|
||||||
kagi--canned-response
|
kagi--canned-response
|
||||||
|
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(let ((return (call-process-shell-command command nil t)))
|
(insert (json-encode `((query . ,prompt))))
|
||||||
|
(let* ((call-process-flags '(nil nil "curl" t t nil))
|
||||||
|
(curl-flags (kagi--curl-flags))
|
||||||
|
(all-flags (append call-process-flags curl-flags))
|
||||||
|
(return (apply #'call-process-region all-flags)))
|
||||||
(if (eql return 0)
|
(if (eql return 0)
|
||||||
(buffer-string)
|
(buffer-string)
|
||||||
(error "Call to FastGPT API returned with status %s" return)))))))
|
(error "Call to FastGPT API returned with status %s" return))))))
|
||||||
|
|
||||||
(defun kagi--process-prompt (prompt)
|
(defun kagi--process-prompt (prompt)
|
||||||
"Submit a PROMPT to FastGPT and process the API response.
|
"Submit a PROMPT to FastGPT and process the API response.
|
||||||
|
|
Loading…
Reference in a new issue