Skip to content

Conversation

@kracejic
Copy link
Contributor

This changes behavior of AIChat to be asynchronous...

As written in #39, I am only changing the AIChat functionality, I do not think this would be too helpful to have it also elsewhere.

I took approach of moving the request call into a thread on the chat.py level. And then I added an async timer in vimscript that is trying to pickup any data coming out from the thread and append them at the end of the buffer.

The main tradeoff here is that in the call to provider.request(messages) you can't access anything from vim anymore as it is not in the main thread.

That means that I have to move the apikey and orgid acquisition from the request to the init part, which is IMHO fine. And the print_debug is also touching vim variables, so I will have to deal somehow with that as well.

Other than that, I have added a "<<< thinking ..." line with changing number of dots when the request is happening in the background so user knows that something is happening.

Also in case of exception in the provider.request(messages), I just dump that into chat as <<< Error getting response segment.

I am creating the pull request to gather some feedback, if you do not have problems with the approach I have taken, I would give it one more a week at least of usage by myself to see if I run into some problems.

kracejic added 6 commits May 17, 2025 22:27
added new print_debug_threaded function that will work in threads. It
expects that `update_debug_variables` will be called before to update
python global variables based on vim configuration.

We can't access vim variables from the thread.
@madox2
Copy link
Owner

madox2 commented May 20, 2025

Hi, thank you for your work! It is going to be a big thing and I think it needs some more testing and tuning. It might also require to change provider contract a little bit, so it might break some things. I wonder if it would be possible to keep the current functionality and add an option that would enable async chat. This way it would be much safer to roll out

@kracejic
Copy link
Contributor Author

Good point.

I think I could relatively easily add new vim variable that could toggle between old/new behavior.
I would check for its value would be in run_ai_chat() whether to go with old sync path or new async one.

This way, even other providers that expect to run in the same thread will still work fine with the sync path.

You could then roll it out gradually, first default to sync, then switch default to async, but keep the option to switch back to sync.

@kracejic
Copy link
Contributor Author

Done, you can take a look if you like the approach, I would still like to keep testing for a couple of days.

@madox2
Copy link
Owner

madox2 commented May 26, 2025

One thing I noticed while testing, undo history is cluttered in the async chat and that also breaks :AIRedo functionality.
In the sync chat it is cleaned up by calling undojoin, I am not sure if it is possible from a separate thread.

I have also pushed some small changes, hope I didn't break anything.

@kracejic
Copy link
Contributor Author

Busy with other things, but hopefully will will get to this soon again.

@madox2 madox2 merged commit f6069eb into madox2:main Jul 21, 2025
1 check passed
@madox2 madox2 mentioned this pull request Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants