How to load previous X messages from current session (stored in Postgres DB) into the chat widget on page refresh/load?

Hi there. I’ve successfully built a workflow, embedded the chat widget onto my website, and everything is working beautifully.

I’m using Postres memory to store every message by the user and bot into a database table, and that’s working, as well.

I’m now trying to load the last 20 messages from the user’s current website session upon page refresh or load (new page), and I can get the data to show up in the console (in the correct format), but I can NOT get the messages to load into the widget itself. I’ve been going ‘round in circles for hours with chatGPT trying all kinds of things, but to no avail.

Any help would be greatly appreciated! :slight_smile:

1 Like

Hi @Jason_Sugar welcome to the :n8n: community!

Since you’re storing the messages, you’ll just need to edit the chat node trigger, enable the Load Previous Session option, choose From Memory, and link it:

Then, pass the Key of the session you want. This way, it should load previous chats in the UI.

1 Like

Thanks so much for your reply, mohamed! I believe everything is working properly within the workflow itself, and with the database table. All testing from within n8n works just fine.

I’m even able to get those database records (past messages) into the web page.

What I’m NOT able to do yet is insert them into the widget itself. I seem to be struggling with how/when the page retrieves the records and renders the widget… I have a version of my embed script/code that can inject the last 20 records into the console…so I know I can retrieve the data into the web page. But I’ve not been able to successfull insert those messages into the widget (i.e. preload the widget on page load).

Oh, and I’m using a webhook as the trigger for the workflow, not “When chat message…” Is that a problem?

There is an option in the chat trigger node to Load Previous Session which i think is what you’re looking for..

Are you suggesting that I maybe go back to using the chat trigger instead of a web hook to kick off the workflow? That's how I initially developed the workflow, and was testing it, but then at some point I was directed (by chatGPT ;)) to use a web hook as a trigger...

yes try using the chat trigger node and configure the Load Previous Session option

1 Like

OK, thanks a alot. I will go back to trying that.

Hey Mohamed, thanks again for your help. Turns out that the solution was as simple as you suggested!

I had wandered down some rabbit hole trying to use webhooks, etc…and manually writing and retrieving records to the DB, and then using all kinds of complicated script code in the webpage to try to retrieve past messages and insert them into the widget manually.

But doing what you suggested worked like a charm and was super simple to implement! So thanks again! :slight_smile:

1 Like

I have another related question that maybe you could help me with? I’ve instructed the agent to output JSON in the following structure so I can store and use various fields to be able to send them to myself in an email:

When you respond, ALWAYS return in this JSON schema:
{
“output”: “<string: the clean text reply you want displayed in the chat window>”,
“name”: “<string, empty if unknown>”,
“email”: “<string, empty if unknown>”,
“topic”: “<string, empty if unknown>”,
“availability”: “<string, empty if unknown>”,
“intent”: “<string: one of ‘collectInfo’, ‘sendTicket’, or ‘none’>”,
}

This is working great within the workflow, but it means that the agent is also storing past messages in the database in a format that doesn’t work well when retrieving past messages for a webpage refresh:

“content”: “{\n \“output\”: \“I understand that you might be testing things out. If you have any questions about Bitcoin or would like assistance with scheduling a consultation, just let me know!\”,\n \“name\”: \”\“,\n \“email\”: \”\“,\n \“topic\”: \”\“,\n \“availability\”: \”\“,\n \“intent\”: \“none\”\n}”,

Do you know of a way that I can:

a) keep data structured like this in the workflow so I can work with the individual fields, but

b) store just the plain text of each message in the database so that when history is retrieved, the messages are formatted in plain text?

this is an example of how the history is displayed when I refresh the page

Glad it worked @Jason_Sugar


Regarding your other question, I’m not quite sure I understand, Are you saying the messages in the database aren’t exactly the same as the ones in the chat, or that the difference is just in the style?

By the way, if you don’t want to store the reply schema in the database at all, you can remove this part entirely from the prompt and let the agent return plain text, and after the agent node, you can add another node: Information Extractor, and in this node, you can specify the schema you want and handle it there.

hope the idea is clear..

Hey thanks, I’ll give that a try!

(And to answer your question, the correct data is being stored in the database, just not in the format I want to re-present it on page refresh.)

The other thing I was thinking of trying was maybe a manual retrieval of history/memory and then try to format it how I would want to display in the chat. But I’ll give your idea a try first!