ai
ai copied to clipboard
Need ability to load previous messages after refresh (useAssistant)
Feature Description
I'm trying to create a chat bot using useAssistant hook. I think it'd be useful if we could load the previous messages on the given thread. If we pass the threadId the AI is able to refer to the previous messages but the user doesn't see them on the UI.
Use Case
When a user tries to interact with the assistant (chat bot) after the first time, typically they'd like to see the old chat messages and continue from there. We can pass the threadId to do this, but user can not see old messages like AI does.
Additional context
I could implement this using the requestOptions parameter to send a special message to the server to return only old messages. But this is not possible for 2 reasons:
submitMessagemethod expects the input to be populated.submitMessagemethod always adds the input to the message list.
Adding a new method like submitCommand that bypasses the message handling would be useful for this scenario and other possible custom actions we'll need.
I was using version 2.2.27. It seems that data message support was added on version 2.2.28. 🤦♂️
I was able to implement the feature by wrapping old messages inside data messages and sending it to the client. Client code has to check for these special data messages and display them as regular messages.
Still, submitting data messages (without an input) from the client is not straightforward. I had to set the input to undefined to bypass the check like this:
submitMessage(undefined, { data: { action: 'loadPreviousMessages' } });
Also I had to filter out empty messages from the ui as these data messages still added to the messages array.
Is there any other way to achieve this? This method seems unstable.
Could we simply call the listMessages endpoint to initialize the messages array, and when the threadId changes ? Happy to open a PR