OvidijusParsiunas / deep-chat

Fully customizable AI chatbot component for your website
https://deepchat.dev
MIT License
1.26k stars 170 forks source link

Event during streaming and simulation updates #128

Closed devpulse01 closed 4 months ago

devpulse01 commented 4 months ago

Hi,

I'm facing a scenario where I need to react to every update within the messages list, including individual character changes, during streaming and simulation processes.

As it stands, I can handle new messages through the onNewMessage event for standard scenarios. However, if I want to have the same behavior as within the UI for stream and simulation, I guess I would need to reimplement the existing logic on my own and manage updates manually within the onNewMessage event handler.

Would it be possible to introduce an event that is specifically triggered for each update during streaming and simulation?

Thank you!

OvidijusParsiunas commented 4 months ago

Hey @devpulse01. Would the responseInterceptor be able to do what you need?

devpulse01 commented 4 months ago

Hi Ovidijus,

Thank you for your quick response.

The onResponse is triggered only once per API response. My challenge arises specifically during the streaming and simulation processes, where updates (including individual character changes within messages) occur multiple times within a single response.

So I'm looking for a mechanism that allows me to react to each of these incremental updates as they happen. This would enable to synchronize custom UI elements or perform other actions in real-time with the stream/simulation updates managed internally by deep-chat.

I hope my request is clear :)

OvidijusParsiunas commented 4 months ago

I see, so it is specifically for tracking the stream simulation character updates? If this is the case, then I would unfortunately have to say that I currently have to restrain for adding such an event to our API as it is a very precise edge case that is quite uncommon. Having said that, could I find out more about what you trying to achieve with this, and if there is a bigger demand for it in the future, I will reconsider it.

I don't want to leave you empty handed and instead offer two directions you can take:

function populateMessages(characters, characterIndex = 0) { const character = characters[characterIndex]; if (character) { // execute the code that you need here setTimeout(() => { populateMessages(characters, characterIndex + 1); }, 10); // change to a timeout that you configured Deep Chat with } }


- Another option is to ofcourse clone/fork the repo and augment the [simulation code]((https://github.com/OvidijusParsiunas/deep-chat/blob/bf8bda213ce06f8d21c36d3f2f4fde3ed967b046/component/src/utils/HTTP/stream.ts#L88)) for your case. It is actually quite simple to do and you can find the setup instructions [here](https://www.npmjs.com/package/deep-chat#construction_worker-to-create-your-own-deep-chat-component).

Let me know if you have any further questions. Thanks!
devpulse01 commented 4 months ago

Hey Ovidijus, Thank you for your response. I'll attempt to implement it on my own, following your code example. Best!

OvidijusParsiunas commented 4 months ago

I will be closing this issue since the original problem now has a solution, nevertheless feel free to comment below if anything comes up or create a new issue for anything else. Thanks!