jeffdapaz / VisualChatGPTStudio

Add chatGPT functionalities directly on Visual Studio
https://marketplace.visualstudio.com/items?itemName=jefferson-pires.VisualChatGPTStudio
MIT License
186 stars 47 forks source link

Buffer the response of the request for better integration in the text editor #14

Closed djack42 closed 1 year ago

djack42 commented 1 year ago

Hi, In BaseChatGPTCommand.RequestAsync(string selectedText), you should buffer the result of the request to the API in the handler and then produce the modifications in the doc once await ChatGPT.RequestAsync(OptionsGeneral, command, ResultHandler) returned. that way, the changes in the doc could easily be undo or redo as one operation instead of a lot of small ones.

Also, you could also prefix the plain text response with the doc's default single line comment to produce a better integration (command Explain, etc.).

jeffdapaz commented 1 year ago

Hi,

About writing the answer at once, personally I don't like this approach due to the longer delay in getting a response. This way, for example, it will be necessary to wait for the response to be complete to display it in the editor. Other people may think this way as well.

But I believe the best approach is to give the user a choice. A new parameter could be created in the options where you can select between "single response" or "continuous response", something like that.

Or better yet, use an alternate command. In the same way that now the user can hold the SHIFT key to send the response to the tool window, if the user selects the command holding the CTRL key, the response would be written in a single pass.

What do you think?

About the prefix, you are right. This will be a good improvement.

jeffdapaz commented 1 year ago

Comment prefix for the "Explain" and "Find Bugs" already added.

Will be on next release.

djack42 commented 1 year ago

Instead of buffering the response and keep the continuous writing, there is a solution to stilla have a better integration : using ITextUndoHistory to create a transaction wrapping any changes to the ITextBuffer and have a clean undo / redo history.
You can even name the transaction according to the chatGPT command executed for better visibility in the history.

You could wrap the line await ChatGPT.RequestAsync(OptionsGeneral, command, ResultHandler); with an ITextUndoTransaction :

ITextUndoHistory undoHistory = textBuffer.GetUndoHistory();
using (ITextUndoTransaction undoTransaction = undoHistory.CreateTransaction("Your transaction name")) {
    // Perform your modifications to the text buffer here
    // For example:
    textBuffer.Insert(position, "text to insert");

    // Complete the transaction
    undoTransaction.Complete();
}
jeffdapaz commented 1 year ago

Excellent solution!

I'll test it and eventually it may already be in the next release.

jeffdapaz commented 1 year ago

I tried to apply this solution that you suggested but I had no luck.

The "docview.TextBuffer" does not have the GetUndoHistory implementation.

I tried other similar approaches but also without success.

PureKrome commented 1 year ago

+1 to having the answer as one history item. Reason - if I don't like the answer given, I can just undo once.

As to the "delay", maybe have some visual indicator - maybe in the Visual chatGPT window, somewhere?

jeffdapaz commented 1 year ago

The extension already provides feedback while waiting for the response, so this is not a problem, but the longer wait time for me would be, I believe it would make it less responsive.

However as I said in the first post I could add an option where the user could choose the "writing mode" of their choice.

PureKrome commented 1 year ago

I could add an option where the user could choose the "writing mode" of their choice.

that would be lovely!

jeffdapaz commented 1 year ago

Ok, I'll implement it as soon as possible and make it available in the next release.

jeffdapaz commented 1 year ago

I already published a new release (1.7.4) that contains the new option parameter "Single Response" for this purpose.

PureKrome commented 1 year ago

Thanks @jeffdapaz !