Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cancelling queries #31

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions ellama.el
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@

(defvar-local ellama--change-group nil)

(defvar-local ellama--current-request nil)

(defconst ellama--code-prefix
(rx (minimal-match
(zero-or-more anything) (literal "```") (zero-or-more anything) line-end)))
Expand Down Expand Up @@ -174,6 +176,15 @@
;; If ellama-enable-keymap is nil, remove the key bindings
(define-key global-map (kbd ellama-keymap-prefix) nil))))

(defun ellama-cancel ()
"Cancel the current ellama request."
(interactive)
(when ellama--current-request
(llm-cancel-request ellama--current-request)

Check warning on line 183 in ellama.el

View workflow job for this annotation

GitHub Actions / lint-emacs-29

the function ‘llm-cancel-request’ is not known to be defined.

Check warning on line 183 in ellama.el

View workflow job for this annotation

GitHub Actions / lint-emacs-28

the function ‘llm-cancel-request’ is not known to be defined.
(cancel-change-group ellama--change-group)
(spinner-stop)
(setq ellama--current-request nil)))

(defun ellama-stream (prompt &rest args)
"Query ellama for PROMPT.
ARGS contains keys for fine control.
Expand Down Expand Up @@ -229,24 +240,28 @@
(activate-change-group ellama--change-group)
(set-marker start point)
(set-marker end point)
(set-marker-insertion-type start nil)
(set-marker-insertion-type end t)
(spinner-start ellama-spinner-type)
(llm-chat-streaming ellama-provider
ellama--chat-prompt
insert-text
(lambda (text)
(funcall insert-text text)
(with-current-buffer buffer
(undo-amalgamate-change-group ellama--change-group)
(accept-change-group ellama--change-group)
(spinner-stop)
(funcall donecb text)))
(lambda (_ msg)
(with-current-buffer buffer
(cancel-change-group ellama--change-group)
(spinner-stop)
(funcall errcb msg))))))))
(set-marker-insertion-type start nil)
(set-marker-insertion-type end t)
(spinner-start ellama-spinner-type)
(ellama-cancel)
(setq ellama--current-request
(llm-chat-streaming ellama-provider
ellama--chat-prompt
insert-text
(lambda (text)
(funcall insert-text text)
(with-current-buffer buffer
(setq ellama--current-request nil)
(undo-amalgamate-change-group ellama--change-group)
(accept-change-group ellama--change-group)
(spinner-stop)
(funcall donecb text)))
(lambda (_ msg)
(with-current-buffer buffer
(setq ellama--current-request nil)
(cancel-change-group ellama--change-group)
(spinner-stop)
(funcall errcb msg)))))))))

;;;###autoload
(defun ellama-chat (prompt)
Expand Down
Loading