Hi all,
When using Agents in Retool Embed, the current chat history is lost after a page reload. Is there a way to retrieve past chat history so users can view previous conversations and continue from where they left off? Ideally, Iād like users to be able to see a list of their previous chats and select one to resume.
Any tips would be appreciated!
i store every message in a table. I also have a 2nd table to hold "conversation" data.
Conversation Table:
- conversation_id (string)
- just a suggestion, I concatenate `'user_id' + '-' + 'conversation_count'
- so for a user with
user_id = 792069
who creates a new conversation, i'd use '792069-1'
as the conversation_id so I can shortcut certain searches later on
- title (string)
- created_by (int)
- created_on (Date)
- updated_on (Date)
- this willl help you later on so you can use polling to notify anybody else in the conversation when there's a new message
- joined_users (JSON - Array of ints for user_id's)
- this is so you can get not just the conversations somebody has created but also the conversations of others they've joined
Message Table:
- message_id (string)
- again, just a suggestion but I use
conversation_id + '-' + message_count
- so using the above example the first message_id would be
792069-1-1
- conversation_id (string - refers to table above)
- content (str)
- created_by (int)
- created_on (Date)
- update_message_id (string - refers to another row in this table, if the user edits their message we want to store the original and the edit while only displaying the newest update... or keep following update_message_id until it's null as that will be the last update. so if this column has a value we know we need to display the content from this updated message id)
- we don't need
updated_on
in this table since if there is an update, we can just check the created_on
date of the message with this id
- type (Enum['user', 'ai'])
- you could instead create a user_id specifically for the ai and use that anytime you process a response. in fact, this would let you have different AIs for different things or people and later on sort messages by the ai so you can monitor, analyze and start the foundations of a dataset you could use to fine-tune your model.... plus you save like 4 bytes per row by not having this. idk why i'm keeping it here lol.
now that you have the database design, you need to hook it up to the frontend so that when the user sends a message it adds a row to the Message Table and if they click a button it adds a row to the Conversation Table. after that, you just have to make some queries so that u can get all the conversations created by current_user's user_id, all the conversations a user_id has joined, all the messages for for a given conversation_id and then i think you should be good to go?
2 Likes
Thanks @bobthebear this is the approach I'd expect in terms of building my own conversation history. Thank you for the details on the two tables. I'd probably also store what agent or LLM that was used each convo.
I guess I was hoping there was something more native within Retool's AI agents that I didn't know about.
1 Like