chainlit
chainlit copied to clipboard
Messages sent during on_chat_resume disappear after on_chat_resume
trafficstars
Describe the bug The chat resume feature seems to have an issue with resuming the thread correctly, most notably messages sent during chat resume disappear immediately afterwards.
When a function decorated using @chainlit.on_chat_resume is called, the following happens:
- The
welcome viewis shown (as if the user started a new chat) - (optional)
2.a. A message is sent using chainlit.Message().send()
2.b. The the
chat view(displaying previous messages) is loaded 2.c. The new message appears inchat view2.d. Afteron_chat_resumereturns, the new message disappears immediately - The chat view is (re)loaded displaying previous messages (not including a message sent during step 2)
We are guessing the issue is, that context.emitter.resume_thread is called after on_chat_resume instead of before, and will post a merge request fixing this issue shortly.
To Reproduce Steps to reproduce the behavior:
- Enable authentication and data persistence (we used the example
header_auth_callbackand the official data layer as shown in chainlit docs). - Run an application that sends a message
msgduringon_chat_resume(example code down below). - Start a new chat & send a message
prev_msg. - Start another new chat.
- Switch back to the first chat, triggering
on_chat_resume. Thewelcome viewis shown. Whenmsgis sent, thechat viewis loaded and previous messages (includingprev_msg) andmsgare displayed. Whenon_chat_resumereturns,msgdisappears.
Example code for reproduction:
import chainlit as cl
from chainlit.types import ThreadDict
from starlette.datastructures import Headers
@cl.on_message
async def on_message(message: cl.Message) -> None:
await cl.Message("in cl_on_message").send()
@cl.on_chat_resume
async def on_chat_resume(thread: ThreadDict) -> None:
# on_chat_resume, 'welcome view' is shown (as if user started a new chat) instead of 'chat view' with previous messages
await cl.sleep(2)
# sending a new message triggers loading the 'chat view'
# the new message itself appears in chat immediately
await cl.Message("in on_chat_resume").send()
# showcase the new message indeed appears in chat
await cl.sleep(2)
# new message disappears
return
@cl.on_chat_start
async def on_chat_start():
app_user = cl.user_session.get("user")
await cl.Message(f"Hello {app_user.identifier}").send()
@cl.header_auth_callback
async def header_auth_callback(headers: Headers) -> cl.User:
return cl.User(
identifier="admin", metadata={"role": "admin", "provider": "header"}
)
if __name__ == "__main__":
cl.run_chainlit(__file__)
Expected behavior
- 'chat view' is loaded immediately when a chat is resumed.
- a message sent during
on_chat_resume('msg' in the reproduction example) does not disappear afteron_chat_resumereturned.
Desktop (please complete the following information):
- OS: Ubuntu running inside WSL on Windows
- Browser: Firefox running on Windows
- Version: 144.0