Persistent Chat Session

I have created the attached


simple chat workflow. The chat trigger is set to load previous sessions. The memory node is Session ID = Define below and Key = CurrentSession. When the workflow loads, it does not load previous sessions nor does it load the “CurrentSession”. The sesssion id is always a new id and there is no chat history. How do I get it to retain the chat history on load?

Hi there, may i know what version of n8n you are using, and if it’s an outdated one to update to the latest stable version.

Hello. It’s the web version.

I have the same issue - it seems like the n8n chat library always creates a new session ID even if you have loadPreviousSession: true in the createChat config.

I can see the sessionID in the localSession of the browser but its not sending it for some reason

1 Like

I have the same problem. Any solution?

1 Like

Hi, I’ve been having trouble with this as well, and I was able to HACK together a ‘solution’ but I’m pretty sure (hope) it wasn’t meant to work like this. Here’s that ‘working’ example: https://jsfiddle.net/cqeuwp2m/1/

correction: in that example, I mentioned setting the chat node to ‘manual’. The right move is leaving it set to ‘off’. otherwise the chat node will grind a bit trying to loadPreviousSession, against a sessionId with no history, and pass an undefined chatInput to the agent node.

So

  • in the chat node, leave Load Previous Session off

  • in the html config, set up a valid uuid, and pass it in the metadata as n8nchatui.sessionKey

  • later in the html config, pass loadPreviousSession: true,

  • after the chat node, add a set node with pass-thru chatInput, action, and metadata, and a redefined sessionId based on the uuid packed in metadata

    {
    “action”: “{{ $json.action }}”,
    “chatInput”: “{{ $json.chatInput }}”,
    “metadata”: {{ $json.metadata }},
    “sessionId”: “{{ $json.metadata.sessionId }}”
    }

You’ll also probably want to sanitize the chatInput to prevent issues with messages that would break the json, so it becomes
{
“sessionId”: “{{ typeof $json.metadata === ‘undefined’ ? $json.sessionId : $json.metadata.sessionId }}”,
“action”: “{{ $json.action }}”,
“chatInput”: {{ JSON.stringify($json.chatInput) }},
“metadata”: {{ typeof $json.metadata !== ‘undefined’ ? $json.metadata : {} }}
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.