Skip to content

Commit

Permalink
move ws logic in libs/components (Chainlit#445)
Browse files Browse the repository at this point in the history
* move ws logic in libs/components

* fix build

* fix build

* fix lint
  • Loading branch information
willydouhard authored Oct 4, 2023
1 parent c917e5c commit 37166fe
Show file tree
Hide file tree
Showing 46 changed files with 866 additions and 729 deletions.
13 changes: 13 additions & 0 deletions backend/chainlit/emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ async def enable_file_upload(self, spec):
"""Stub method to enable uploading file in the UI."""
pass

async def init_conversation(self, msg_dict: MessageDict):
"""Signal the UI that a new conversation (with a user message) exists"""
pass

async def process_user_message(self, message_dict) -> None:
"""Stub method to process user message."""
pass
Expand Down Expand Up @@ -153,6 +157,11 @@ def enable_file_upload(self, spec: FileSpec):

return self.emit("enable_file_upload", spec.to_dict())

def init_conversation(self, message: MessageDict):
"""Signal the UI that a new conversation (with a user message) exists"""

return self.emit("init_conversation", message)

async def process_user_message(self, message_dict: MessageDict):
# Temporary UUID generated by the frontend should use v4
assert uuid.UUID(message_dict["id"]).version == 4
Expand All @@ -167,6 +176,10 @@ async def process_user_message(self, message_dict: MessageDict):
ui_message_update["newId"] = message_dict["id"]
await self.update_message(ui_message_update)

if not self.session.has_user_message:
self.session.has_user_message = True
await self.init_conversation(message_dict)

message = Message.from_dict(message_dict)
self.session.root_message = message

Expand Down
1 change: 1 addition & 0 deletions backend/chainlit/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
self.user = user
self.token = token
self.root_message = root_message
self.has_user_message = False
self.user_env = user_env or {}

self.id = id
Expand Down
6 changes: 5 additions & 1 deletion backend/chainlit/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from chainlit.action import Action
from chainlit.auth import get_current_user, require_login
from chainlit.client.base import MessageDict
from chainlit.client.cloud import MessageDict, chainlit_client
from chainlit.config import config
from chainlit.context import init_ws_context
from chainlit.logger import logger
Expand Down Expand Up @@ -132,6 +132,10 @@ async def disconnect(sid):
"""Call the on_chat_end function provided by the developer."""
await config.code.on_chat_end()

if chainlit_client and session and not session.has_user_message:
if session.conversation_id:
await chainlit_client.delete_conversation(session.conversation_id)

async def disconnect_on_timeout(sid):
await asyncio.sleep(config.project.session_timeout)
if session := WebsocketSession.get(sid):
Expand Down
4 changes: 2 additions & 2 deletions cypress/e2e/streaming/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ async def main():
msg = cl.Message(content="")
for token in token_list:
await msg.stream_token(token)
await cl.sleep(1)
await cl.sleep(0.1)

await msg.send()

msg = cl.Message(content="")
for seq in sequence_list:
await msg.stream_token(token=seq, is_sequence=True)
await cl.sleep(1)
await cl.sleep(0.1)

await msg.send()
13 changes: 4 additions & 9 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
"@mui/material": "^5.14.10",
"@mui/lab": "^5.0.0-alpha.122",
"formik": "^2.4.3",
"lodash.clonedeep": "^4.5.0",
"lodash.debounce": "^4.0.8",
"lodash.mapvalues": "^4.6.0",
"lodash.throttle": "^4.1.1",
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.1",
Expand All @@ -41,16 +38,14 @@
"@chainlit/components": "workspace:^"
},
"devDependencies": {
"@types/lodash.clonedeep": "^4.5.7",
"@types/lodash.debounce": "^4.0.7",
"@types/lodash.mapvalues": "^4.6.7",
"@types/lodash.throttle": "^4.1.7",
"@types/lodash": "^4.14.199",
"@types/node": "^20.5.7",
"@types/react": "^18.2.0",
"@types/react-window-infinite-loader": "^1.0.6",
"@types/uuid": "^9.0.3",
"@vitejs/plugin-react-swc": "^3.3.2",
"vite": "^4.4.9",
"vite-tsconfig-paths": "^4.2.0"
"vite-tsconfig-paths": "^4.2.0",
"typescript": "^5.2.2"
}
}
Loading

0 comments on commit 37166fe

Please sign in to comment.