1
0
Fork 0

Add ability to return stubbed responses

When developing / debugging, it's a waste of API credit to make actual
calls. Flip the kagi-stubbed-responses flag to t to enable dummy responses.
This commit is contained in:
Bram Schoenmakers 2024-01-11 21:41:38 +01:00
parent 0d1d402ab7
commit 4fd1372b42
2 changed files with 40 additions and 2 deletions

View file

@ -102,6 +102,7 @@ The token can be supplied directly as a string, but you could write a lambda to
|----------------------------------+---------------------------------------------------------|
| kagi-api-token | The Kagi API token. |
| kagi-fastgpt-api-url | The Kagi FastGPT API entry point. |
| kagi-stubbed-responses | Whether the package should return a stubbed response. |
| kagi-summarizer-api-url | The Kagi Summarizer API entry point. |
| kagi-summarizer-cache | Determines whether the Summarizer should cache results. |
| kagi-summarizer-default-language | Default target language of the summary. |

41
kagi.el
View file

@ -63,6 +63,14 @@ https://kagi.com/settings?p=api"
:type '(choice string function)
:group 'kagi)
(defcustom kagi-stubbed-responses nil
"Whether the package should return a stubbed response.
To be used for testing purposes, such that no credits are spent
on dummy data."
:type 'boolean
:group 'kagi)
(defcustom kagi-fastgpt-api-url "https://kagi.com/api/v0/fastgpt"
"The Kagi FastGPT API entry point."
:type '(choice string function)
@ -206,6 +214,35 @@ https://help.kagi.com/kagi/api/fastgpt.html for more information."
"--header" "Content-Type: application/json"
"--data" "@-")))
(defvar kagi--fastgpt-stubbed-response
"{\"data\":{\"output\":\"a<b>b</b>c\"}}"
"Stubbed response for the Kagi FastGPT endpoint.")
(defvar kagi--summarizer-stubbed-response
"{\"data\":{\"output\":\"a```b```c\"}}"
"Stubbed response for the Kagi Summarizer endpoint.")
(defun kagi--call-process-region (&rest args)
"`call-process-region' wrapper.
Calls `call-process-region' with ARGS, unless
`kagi-stubbed-responses' is non-nil.
In that case, this function will not do an actual API call but
return some dummy data."
(if (not kagi-stubbed-responses)
(apply #'call-process-region args)
(let* ((url (car (last args)))
(response (cond
((string= url kagi-fastgpt-api-url)
kagi--fastgpt-stubbed-response)
((string= url kagi-summarizer-api-url)
kagi--summarizer-stubbed-response)
(t ""))))
(erase-buffer)
(insert response)
0)))
(defun kagi--call-fastgpt (prompt)
"Submit the given PROMPT to the FastGPT API.
@ -219,7 +256,7 @@ interpretation."
(all-flags (append call-process-flags
curl-flags
(list kagi-fastgpt-api-url)))
(return (apply #'call-process-region all-flags)))
(return (apply #'kagi--call-process-region all-flags)))
(if (zerop return)
(buffer-string)
(error "Call to FastGPT API returned with status %s" return)))))
@ -239,7 +276,7 @@ interpretation."
(all-flags (append call-process-flags
curl-flags
(list kagi-summarizer-api-url)))
(return (apply #'call-process-region all-flags)))
(return (apply #'kagi--call-process-region all-flags)))
(if (zerop return)
(buffer-string)
(error "Call to Summarizer API returned with status %s" return)))))