ai icon indicating copy to clipboard operation
ai copied to clipboard

Need ability to load previous messages after refresh (useAssistant)

Open hasan-aa opened this issue 1 year ago • 3 comments
trafficstars

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:

  1. submitMessage method expects the input to be populated.
  2. submitMessage method 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.

hasan-aa avatar Dec 28 '23 09:12 hasan-aa

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.

hasan-aa avatar Dec 28 '23 11:12 hasan-aa

Is there any other way to achieve this? This method seems unstable.

clkefe avatar Jan 15 '24 02:01 clkefe

Could we simply call the listMessages endpoint to initialize the messages array, and when the threadId changes ? Happy to open a PR

overmode avatar Jan 26 '24 06:01 overmode