gsuuon / model.nvim

Neovim plugin for interacting with LLM's and building editor integrated prompts.
MIT License
293 stars 21 forks source link

Add option to insert streamed responses using :undojoin #18

Closed MattSPalmer closed 9 months ago

MattSPalmer commented 9 months ago

Issue: Currently, the plugin does not properly support the undo command when working with streamed responses.

Expected Behavior: After receiving a streamed response, I should be able to hit u a single time to remove the entire response

Actual Behavior: After receiving a streamed response, I have to hit u once per streamed "chunk" to remove the entire response.

Suggestion: Use :undojoin to join all of the streamed chunks into a single undoable action.

Thanks for a great plugin!

gsuuon commented 9 months ago

Hi, thanks for the suggestion! I didn't know about :undojoin. I'll take a look, though my first guess is that it may have issues since users can do other things while the response completes. These may also get bundled into the undo and I definitely don't want to mess up people's undo history.

The currently supported way to delete a response is to use :LlmDelete with your cursor over the response. I think the most straightforward solution may just be to map u to try an :LlmDelete before a normal undo. I'll have to play around with this a bit.

My current setup is to map <C-g>c to my main :Llm prompt, <C-g>d to :LlmDelete and <C-g>s to :LlmSelect. This lets me easily generate, delete, or reformat (<C-g>s=) the last response.

MattSPalmer commented 9 months ago

Hello,

Thanks for your reply. I tested it and it does work the way you're describing.

I understand the concern about users making edits while waiting for the response to complete, and not wanting to mess up their undo history. But I'd argue the current behavior is just as disruptive to undo history.

I am always biased towards keeping editor behavior as close to vanilla vim as possible. To that end, I appreciate the suggestion to use :LlmDelete with the cursor over the response, but I think it goes against what people expect from the undo command: that its behavior is not contingent on the cursor position.

Would you be open to a configuration option that lets users opt into the proposed behavior? To be honest I'm rarely if ever going to be editing the buffer while a response streams in, and if my changes did get swept up in an undo operation, I'd just redo, yank changed text, undo again, paste. (I actually do this a lot already 😅).

EDIT: To clarify, I like the streaming feature and would love to keep using it! Otherwise I'd be happy just using on_finish to write the completed response in.

gsuuon commented 9 months ago

That's a good point, undo tied to cursor position would indeed feel weird. Yeah sure, a config option sounds like a good workaround - I can just add the caveat that you'll possibly lose work if you do anything while a stream is ongoing and then undo. Would you mind editing the original issue to track that?