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

Activation of gptel-mode via org-mode-hook slightly misbehaves #440

Open
nordlow opened this issue Oct 28, 2024 · 15 comments
Open

Activation of gptel-mode via org-mode-hook slightly misbehaves #440

nordlow opened this issue Oct 28, 2024 · 15 comments
Labels
question Further information is requested

Comments

@nordlow
Copy link

nordlow commented Oct 28, 2024

I want to have gptel be activated automatically in org-mode and markdown-mode buffers when they are opened. I currently am using gptel-setup-hook:nordlow in

(defun gptel-setup-hook:nordlow ()
  (gptel-mode 1))

(use-package gptel						;https://github.com/karthink/gptel
  :ensure t :defer t
  :custom
  (gptel-use-curl nil)
  (gptel-stream t)
  (gptel-default-mode 'org-mode)
  (gptel-model 'claude-3-sonnet-20240229) ;See `gptel--anthropic-models'.
  :hook ((markdown-mode . gptel-setup-hook:nordlow)
		 (org-mode . gptel-setup-hook:nordlow))
  :config
  (add-hook 'gptel-post-response-functions
			'gptel-post-response-adjust:nordlow)
  (require 'gptel-gemini nil t)
  (when (require 'gptel-anthropic nil t)
	(setq gptel-backend (gptel-make-anthropic "Claude"
						  :stream nil	;TODO: experiment with true
						  :key anthropic-api-token)))
  (global-set-key [(control c) (return)] 'gptel-send))

.

But it slightly misbehaves:

  • Upon opening the screen flashes like it doesn't do when I interactively run gptel-mode after org-mode has completed loading
  • Some of the existing org-mode properties such as :GPTEL_SYSTEM: are either duplicated or replaced

Is the problem that the org-mode-hook is run too early?

Thanks in advance.

@nordlow nordlow added the enhancement New feature or request label Oct 28, 2024
@nordlow nordlow changed the title Best way to automatically activate gptel-mode upon opening of files in org-mode Activation of gptel-mode via org-mode-hook slightly misbehaves Oct 28, 2024
@karthink karthink added question Further information is requested and removed enhancement New feature or request labels Oct 28, 2024
@karthink
Copy link
Owner

  • Upon opening the screen flashes like it doesn't do when I interactively run gptel-mode after org-mode has completed loading
  • Some of the existing org-mode properties such as :GPTEL_SYSTEM: are either duplicated or replaced

To be clear, these issues don't occur when you run M-x gptel-mode manually after opening the Org buffer?


I have a couple unrelated comments for you:

  1. If you're using gptel-modein all Org buffers, the header line might be annoying. You can get a more discreet display of gptel's status by setting gptel-use-header-line to nil.

  2. I see you have gptel-use-curl set to nil. Is there some reason you don't use Curl? gptel works much better with Curl. (In particular, streaming responses don't work without Curl.)

@nordlow
Copy link
Author

nordlow commented Oct 28, 2024

I managed to work around the issue with flashing for now by using

(defun gptel-setup-hook:nordlow ()
  "Setup `gptel-mode' in `org-mode' or `markdown-mode' buffers.
Avoids screen flash on activation via use of `run-with-timer'.
See: https://github.com/karthink/gptel/issues/440."
  (run-with-timer 0.1 nil (lambda ()
                            (when (derived-mode-p 'org-mode 'markdown-mode)
                              (gptel-mode 1)))))
                              
(dolist (hook '(org-mode-hook markdown-mode-hook))
	(add-hook hook 'gptel-setup-hook:nordlow))

.

@nordlow
Copy link
Author

nordlow commented Oct 28, 2024

I see you have gptel-use-curl set to nil. Is there some reason you don't use Curl? gptel works much better with Curl. (In particular, streaming responses don't work without Curl.)

Ahh, no, no reason at all. Thanks.

Will revert that change and see if things improve.

Is it preferred to remove that configuration complete or set .

In what way does gptel work better with curl, btw?

@karthink
Copy link
Owner

I managed to work around the issue with flashing for now by using

(defun gptel-setup-hook:nordlow ()
  "Setup `gptel-mode' in `org-mode' or `markdown-mode' buffers.
Avoids screen flash on activation via use of `run-with-timer'.
See: https://github.com/karthink/gptel/issues/440."
  (run-with-timer 0.1 nil (lambda ()
                            (when (derived-mode-p 'org-mode 'markdown-mode)
                              (gptel-mode 1)))))
                              
(dolist (hook '(org-mode-hook markdown-mode-hook))
	(add-hook hook 'gptel-setup-hook:nordlow))

That's cool, but it sounds like there might be a bug here, so I'm hoping to address it in gptel. I can't reproduce the screen flash or the duplicate entry -- in particular, turning on gptel-mode should not modify the buffer text in any way.

Is there anything else in your org-mode-hook that might be interfering with turning on gptel-mode?

@nordlow
Copy link
Author

nordlow commented Oct 28, 2024

To be clear, these issues don't occur when you run M-x gptel-mode manually after opening the Org buffer?

Exactly.

@nordlow
Copy link
Author

nordlow commented Oct 28, 2024

Is there anything else in your org-mode-hook that might be interfering with turning on gptel-mode?

My current org-mode-hook looks like

(#f(compiled-function () #<bytecode -0x663ec7d713d82c2>)
   auto-indent-turn-on-org-indent org-add-electric-pairs
   flyspell-mode-setup:nordlow
   #f(compiled-function () #<bytecode 0x7b4eae3155c62>) org-mode-setup:nordlow
   org-adjust-faces:nordlow
   #f(compiled-function () #<bytecode -0x13d257f8976a5ac9>)
   #f(compiled-function () #<bytecode -0x13d257fb4a4dcac9>)
   org-babel-result-hide-spec org-babel-hide-all-hashes completion-preview-mode
   gptel-setup-hook:nordlow)

. I have a bunch of adjustments in it, yes. The bytecode hooks are from standard org-mode.

@karthink
Copy link
Owner

In what way does gptel work better with curl, btw?

It supports streaming responses, it's better tested, and the processing runs a little faster (at least on Linux).

The url-retrieve method is mostly intended as a fallback for when Curl is not available.

Is it preferred to remove that configuration complete or set .

gptel-use-curl defaults to true if Curl is available, so you can just remove it.

@nordlow
Copy link
Author

nordlow commented Oct 28, 2024

Ok, thanks.

I'm also using

(defun gptel-post-response-adjust:nordlow (_begin _end)
  ;; break long lines
  ;; (fill-paragraph nil (cons begin end))
  ;; (fill-region begin end)
  (unless visual-line-mode
	(visual-line-mode 1)))

, btw. So altogether I currently have

(defun gptel-post-response-adjust:nordlow (_begin _end)
  ;; break long lines
  ;; (fill-paragraph nil (cons begin end))
  ;; (fill-region begin end)
  (unless visual-line-mode
	(visual-line-mode 1)))

(defun gptel-setup-hook:nordlow ()
  "Setup `gptel-mode' in `org-mode' or `markdown-mode' buffers.
Avoids screen flash on activation via use of `run-with-timer'.
See: https://github.com/karthink/gptel/issues/440."
  (run-with-timer 0.1 nil (lambda ()
                            (when (derived-mode-p 'org-mode 'markdown-mode)
                              (gptel-mode 1)))))

(use-package gptel						;https://github.com/karthink/gptel
  :ensure t :defer t
  :custom
  (gptel-default-mode 'org-mode)
  (gptel-model 'claude-3-sonnet-20240229) ;See `gptel--anthropic-models'.
  :config
  (dolist (hook '(org-mode-hook markdown-mode-hook))
	(add-hook hook 'gptel-setup-hook:nordlow))
  (add-hook 'gptel-post-response-functions
			'gptel-post-response-adjust:nordlow)
  (require 'gptel-gemini nil t)
  (when (require 'gptel-anthropic nil t)
	(setq gptel-backend (gptel-make-anthropic "Claude"
						  :stream nil	;TODO: experiment with true
						  :key anthropic-api-token)))
  (global-set-key [(control c) (return)] 'gptel-send))

@karthink
Copy link
Owner

karthink commented Oct 28, 2024

Ok, thanks.

I'm also using

(defun gptel-post-response-adjust:nordlow (_begin _end)
  ;; break long lines
  ;; (fill-paragraph nil (cons begin end))
  ;; (fill-region begin end)
  (unless visual-line-mode
	(visual-line-mode 1)))

, btw.

This shouldn't be an issue. (BTW if you were using fill-region or fill-paragraph before but stopped because gptel threw an error, this has been fixed now, you should be able to use fill-* to format the response without issues.)

@nordlow
Copy link
Author

nordlow commented Oct 28, 2024

  ;; (fill-paragraph nil (cons begin end))
  ;; (fill-region begin end)

Ok, thanks. The reason why I chose visual-line-mode over fill- was that fill- modifed the org-mode code blocks in a bad way making them syntactically incorrect. Do you have a solution to that?

Are you using something lke

(defun gptel-post-response-adjust:nordlow (begin end)
  (fill-paragraph nil (cons begin end))
)

or do you use visual-line-mode?

@karthink
Copy link
Owner

or do you use visual-line-mode?

I don't modify or post-process the response in any way.

..making them syntactically incorrect. Do you have a solution to that?

No, but you can write one. For example:

(defun gptel-post-response-adjust:nordflow (beg end)
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (org-element-map (org-element-parse-buffer)
          '(paragraph)
        (lambda (node)
          (goto-char (org-element-begin node))
          (org-fill-paragraph))))))

This will only fill paragraphs, leaving all blocks and other Org elements alone.

@karthink
Copy link
Owner

Is there anything else in your org-mode-hook that might be interfering with turning on gptel-mode?

My current org-mode-hook looks like

(#f(compiled-function () #<bytecode -0x663ec7d713d82c2>)
   auto-indent-turn-on-org-indent org-add-electric-pairs
   flyspell-mode-setup:nordlow
   #f(compiled-function () #<bytecode 0x7b4eae3155c62>) org-mode-setup:nordlow
   org-adjust-faces:nordlow
   #f(compiled-function () #<bytecode -0x13d257f8976a5ac9>)
   #f(compiled-function () #<bytecode -0x13d257fb4a4dcac9>)
   org-babel-result-hide-spec org-babel-hide-all-hashes completion-preview-mode
   gptel-setup-hook:nordlow)

. I have a bunch of adjustments in it, yes. The bytecode hooks are from standard org-mode.

Doesn't seem like there's anything that should be causing trouble with gptel. Hmm.

@karthink
Copy link
Owner

I tried to reproduce this error (duplicate GPTEL_SYSTEM property, flashing screen) again but couldn't.

Could you try to reproduce it in stock Emacs, with emacs -q? You can install the gptel package and try to work with an Org file.

@karthink
Copy link
Owner

Did you have a chance to try this with emacs -q?

@karthink
Copy link
Owner

karthink commented Jan 2, 2025

Is the original issue -- with org-mode-hook -- still present?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants