karthink / gptel

A simple LLM client for Emacs
GNU General Public License v3.0
1.03k stars 111 forks source link

gptel asks user to select a buffer if many are open #311

Closed algal closed 1 month ago

algal commented 1 month ago

This commit modifies the behavior of the gptel function so that, in the case where the function is called interactively, and multiple gptel sessions already exist (as judged by there being multiple buffers with the minor mode gptel-mode enabled), then the function prompts the user to select one of those buffers.

This replaces the previous behavior in this case, which was just to switch to the buffer named backend-name if it already existed, or else to create a new buffer with that name.

So, after this comit, the gptel function behaves as follows:

karthink commented 1 month ago

@algal This looks good. After trying it for a bit, I think we can simplify the behavior of M-x gptel:

You also don't need to use seq-filter to find the candidates then, and the completing-read will be replaced by

(read-buffer "Create or choose gptel buffer: "
             backend-name nil                         ; DEFAULT and REQUIRE-MATCH
             (lambda (b)                              ; PREDICATE
               (buffer-local-value 'gptel-mode
                                   (get-buffer (or (car-safe b) b)))))

The prefix-arg behavior will then be unnecessary, and we can use it to bring up a transient menu where the user can choose the gptel buffer name, backend, system message and other model parameters to use in the new session. This is a feature that's been asked for a couple of times, and I can add it after this PR.

What do you think?

algal commented 1 month ago

@karthink Yes, I think the behavior you describe is better than what I proposed in my original PR. My new commits above implement what you describe, based on your code snippet, and this feels like a better user experience. (Of course, please feel free to just to implement as you like if you want to keep your history clean.)

I also like the consistency of the second idea -- of using the prefix arg for gptel to bring up roughly the same transient menu which is now available when the user invokes gptel-send.

I think this would be especially handy for selecting a system directive early, since usually you know what kind of assistance you require at buffer-creation time, even if you might want to experiment with different models at query-submission time.

I suppose it would have to be only roughly the same transient menu, since it looks like some of the options there now would not make sense when creating a buffer.

karthink commented 1 month ago

Looks good! Will merge after you fix the version change.

I suppose it would have to be only roughly the same transient menu, since it looks like some of the options there now would not make sense when creating a buffer.

Yeah, I can't reuse the menu, I'll have to reuse some of the pieces to make a new one.

karthink commented 1 month ago

Looks good, merging. Thanks for the PR!