1
0
Fork 0

Refactored and merged some common functionality

This commit is contained in:
Bram Schoenmakers 2024-02-09 08:40:41 +01:00
parent d95e5ebfbe
commit a8a5786ace
Signed by: bram
GPG key ID: 0CCD19DFDC63258F

51
kagi.el
View file

@ -415,17 +415,29 @@ list of conses."
(shell-maker-start kagi-fastgpt--config)) (shell-maker-start kagi-fastgpt--config))
;;;###autoload ;;;###autoload
(defun kagi-fastgpt-prompt (prompt &optional insert) (defun kagi-fastgpt-prompt (prompt &optional insert interactive-p)
"Feed the given PROMPT to FastGPT. "Feed the given PROMPT to FastGPT.
If INSERT is non-nil, the response is inserted at point. If INSERT is non-nil, the response is inserted at point (if the
Otherwise, show the result in a separate buffer." buffer is writable).
(interactive "sfastgpt> \nP")
(let ((result (kagi-fastgpt prompt))) If INTERACTIVE-P is non-nil, the result is presented either in the minibuffer for single line outputs, or shown in a separate buffer.
(if (and insert (not buffer-read-only))
If INTERACTIVE-P is nil, the result is returned as a
string (suitable for invocations from Emacs Lisp)."
(interactive (list (read-string "fastgpt> ")
current-prefix-arg
t))
(let* ((result (string-trim (kagi-fastgpt prompt)))
(result-lines (length (string-lines result))))
(cond ((and insert (not buffer-read-only))
(save-excursion (save-excursion
(insert result)) (insert result)))
(kagi--fastgpt-display-result result)))) ((and interactive-p (eql result-lines 1))
(message result))
((and interactive-p (> result-lines 1))
(kagi--fastgpt-display-result result))
((not interactive-p) result))))
(defun kagi--read-language (prompt) (defun kagi--read-language (prompt)
"Read a language from the minibuffer interactively. "Read a language from the minibuffer interactively.
@ -439,6 +451,7 @@ PROMPT is passed to the corresponding parameters of
kagi--language-history kagi--language-history
"English")) "English"))
;; TODO rename
(defun kagi--get-buffer-or-text () (defun kagi--get-buffer-or-text ()
"Return the text to operate on. "Return the text to operate on.
@ -474,19 +487,16 @@ result is short, otherwise it is displayed in a new buffer."
(when (equal current-prefix-arg '(4)) (when (equal current-prefix-arg '(4))
(kagi--read-language (format-prompt "Source language" nil))) (kagi--read-language (format-prompt "Source language" nil)))
t)) t))
(let* ((prompt (format "Translate the following text %sto %s, return the translation in the target language only: (kagi-fastgpt-prompt
(format "Translate the following text %sto %s, return the translation in the target language only:
%s" %s"
(if source-language (if source-language
(format "from %s " source-language) (format "from %s " source-language)
"") "")
target-language target-language
text)) text)
(result (string-trim (kagi-fastgpt prompt))) nil t))
(result-lines (length (string-lines result))))
(cond ((and interactive-p (eql result-lines 1)) (message result))
((and interactive-p (> result-lines 1)) (kagi--fastgpt-display-result result))
(t result))))
;;;###autoload ;;;###autoload
(defun kagi-proofread (text &optional interactive-p) (defun kagi-proofread (text &optional interactive-p)
@ -499,18 +509,15 @@ nil), the function should return the string 'OK' when there are
no issues." no issues."
(interactive (interactive
(list (kagi--get-buffer-or-text) t)) (list (kagi--get-buffer-or-text) t))
(let* ((prompt (format "Proofread the following text. %s (kagi-fastgpt-prompt
(format "Proofread the following text. %s
%s" %s"
(if interactive-p (if interactive-p
"" ""
"Say OK if there are no issues.") "Say OK if there are no issues.")
text)) text)
(result (string-trim (kagi-fastgpt prompt))) nil t))
(result-lines (length (string-lines result))))
(cond ((and (eql result-lines 1)) (message result))
((and (> result-lines 1)) (kagi--fastgpt-display-result result))
(t result))))
;;; Summarizer ;;; Summarizer