Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
7.24k stars 955 forks source link

Extraheaders within the socket connection not being updated. #1507

Open deepakjyadav01 opened 2 weeks ago

deepakjyadav01 commented 2 weeks ago

Describe the bug I'm using a custom frontend repo and chainlit/react-client lib to establish a WebSocket connection with Chainlit. The issue is that the X-Chainlit-Thread-Id header doesn’t update as expected when I try to set a old thread ID dynamically through idToResume. The header only updates correctly if the page is refreshed. Without a refresh, the header retains an old value or fails to set the new thread ID, resulting in a new connection without the intended idToResume value in the header.

According to my understanding : IdToResume hook sets the X-Chainlit-Thread-Id header and setIdtoResume from chatInteract can be used to set IdToResume. Accordinglt whenever IdToResume changes sessionstate recoil state(if you are using one) should get updated right? but it only happens when i reload/refresh the page.

To Reproduce Steps to reproduce the behavior:

  1. Open the custom frontend and initiate a WebSocket connection with a specific idToResume set in the headers.
  2. Click on an item or perform an action that updates idToResume and should trigger a new WebSocket connection with the updated X-Chainlit-Thread-Id.
  3. Observe that the header does not update as expected and either has an empty X-Chainlit-Thread-Id or retains the old value.
  4. Refresh the page and note that the header is then correctly updated with the intended idToResume.
  5. Expected behavior
  6. The X-Chainlit-Thread-Id header should update dynamically whenever idToResume is changed, without requiring a page refresh to set the new thread ID properly in the WebSocket connection.

I am looking for guidance on the best way to dynamically set X-Chainlit-Thread-Id without refreshing the page each time idToResume is updated. Is there a recommended way to ensure the WebSocket connection uses the updated idToResume header immediately upon reconnecting? I tried using clear() and disconnect but still facing difficulties in trying to get the header updated when reconnecting to the old chat using Idtoresume hook, though the same functionality works when i reload the page.