Extend FastGPT function to insert result at point
Split the interactive and non-interactive functionality in separate functions. This allows the interactive command to be extended with an insert parameter.
This commit is contained in:
parent
f61c3c7d0f
commit
bf599591f0
1 changed files with 12 additions and 15 deletions
27
kagi.el
27
kagi.el
|
@ -348,10 +348,8 @@ list of conses."
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(insert (substring-no-properties summary))))
|
(insert (substring-no-properties summary))))
|
||||||
|
|
||||||
(defun kagi--process-prompt (prompt)
|
(defun kagi-fastgpt (prompt)
|
||||||
"Submit a PROMPT to FastGPT and process the API response.
|
"Submit a PROMPT to FastGPT and return a formatted response string."
|
||||||
|
|
||||||
Returns a formatted string to be displayed by the shell."
|
|
||||||
(let* ((response (kagi--call-fastgpt prompt))
|
(let* ((response (kagi--call-fastgpt prompt))
|
||||||
(parsed-response (json-parse-string response))
|
(parsed-response (json-parse-string response))
|
||||||
(output (kagi--gethash parsed-response "data" "output"))
|
(output (kagi--gethash parsed-response "data" "output"))
|
||||||
|
@ -374,7 +372,7 @@ Returns a formatted string to be displayed by the shell."
|
||||||
:execute-command
|
:execute-command
|
||||||
(lambda (command _history callback error-callback)
|
(lambda (command _history callback error-callback)
|
||||||
(condition-case err
|
(condition-case err
|
||||||
(funcall callback (kagi--process-prompt command) nil)
|
(funcall callback (kagi-fastgpt-prompt command) nil)
|
||||||
(json-parse-error (funcall error-callback
|
(json-parse-error (funcall error-callback
|
||||||
(format "Could not parse the server response %s" (cdr err))))
|
(format "Could not parse the server response %s" (cdr err))))
|
||||||
(error (funcall error-callback (format "An error occurred during the request %s" (cdr err)))))))
|
(error (funcall error-callback (format "An error occurred during the request %s" (cdr err)))))))
|
||||||
|
@ -389,18 +387,17 @@ Returns a formatted string to be displayed by the shell."
|
||||||
(shell-maker-start kagi-fastgpt--config))
|
(shell-maker-start kagi-fastgpt--config))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun kagi-fastgpt-prompt (prompt &optional interactive)
|
(defun kagi-fastgpt-prompt (prompt &optional insert)
|
||||||
"Feed the given PROMPT to FastGPT and return a response.
|
"Feed the given PROMPT to FastGPT.
|
||||||
|
|
||||||
When INTERACTIVE is nil, the response is returned as a string.
|
If INSERT is non-nil, the response is inserted at point.
|
||||||
|
Otherwise, show the result in a separate buffer."
|
||||||
When INTERACTIVE is non-nil, the function was called
|
|
||||||
interactively and the result is displayed in a new buffer."
|
|
||||||
(interactive "sfastgpt> \np")
|
(interactive "sfastgpt> \np")
|
||||||
(let ((result (kagi--process-prompt prompt)))
|
(let ((result (kagi-fastgpt prompt)))
|
||||||
(if interactive
|
(if insert
|
||||||
(kagi--fastgpt-display-result result)
|
(save-excursion
|
||||||
result)))
|
(insert result))
|
||||||
|
(kagi--fastgpt-display-result result))))
|
||||||
|
|
||||||
;;; Summarizer
|
;;; Summarizer
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue