OvidijusParsiunas / deep-chat

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

Dynamically add responses for long-running tasks #192

Open nileshtrivedi opened 1 month ago

nileshtrivedi commented 1 month ago

There are use-cases where agent can be asked to perform a task which can take time much longer than typical web request timeout limit. For example: "Research and prepare a report", "Confirm with James if this task is ready to be picked up?"

Is there a way for a custom backend to push a message into a visible chat thread? If the user were to reload the page, initialMessages could be updated, but this is not ideal.

Perhaps this can be achieved simply by an automatic periodic refresh of the conversation using setInterval ?

buzhou9 commented 1 month ago

Perhaps this can be done by manually using the "deepChatInstance. addMessage" method after receiving the message to add the content to the current conversation process

nileshtrivedi commented 1 month ago

@buzhou9 Yeah, just got to know about _addMessage. That might work too.

OvidijusParsiunas commented 1 month ago

@buzhou9 is doing an amazing job with the responses, I have been very short on free-time so thankyou so much!!!!

@nileshtrivedi if @buzhou9's suggestion for using _addMessage works for you then I am very happy.

I have a few other alternative suggestions that you can also use:

  1. You can use the request handler to control the request logic and return the response exactly when you need to.

  2. You can make the request using your code and respond in the responseInterceptor. It is a slightly more complicated approach to above, but it is still something that works:

    • Set demo to true
    • Use the requestInterceptor to listen for when a user sends a message. When that is triggered - make the request in your own code.
    • Change the responseInterceptor code to be something as follows, where you store the resolve argument in your own state, and trigger it (using the Response format) when you have the eventual response from your server:
      async (response) => {
      return new Promise((resolve) => {
      // save the resolve argument in your app's state and call it when ready
      });
      }

If the user has opened up a fresh new page or reloaded it and you still want to add a message after it, I recommend immediately triggering the submitUserMessage method with their request text to display the loading message and prevent the user from creating another request, then handling the response using any of the above suggestions.


Huge shoutout to @buzhou9 for helping out!!!!!!