diff --git a/backend/chainlit/auth.py b/backend/chainlit/auth.py index 85abe2f8475..f45bb53b498 100644 --- a/backend/chainlit/auth.py +++ b/backend/chainlit/auth.py @@ -53,9 +53,8 @@ def create_jwt(data: User) -> str: to_encode: Dict[str, Any] = data.to_dict() to_encode.update( { - "exp": datetime.utcnow() + timedelta( - seconds=config.project.user_session_timeout - ), + "exp": datetime.utcnow() + + timedelta(seconds=config.project.user_session_timeout), } ) encoded_jwt = jwt.encode(to_encode, get_jwt_secret(), algorithm="HS256") diff --git a/backend/chainlit/chat_context.py b/backend/chainlit/chat_context.py index 5f7215ba56b..81bf66b3d28 100644 --- a/backend/chainlit/chat_context.py +++ b/backend/chainlit/chat_context.py @@ -25,10 +25,10 @@ def add(self, message: "Message"): if context.session.id not in chat_contexts: chat_contexts[context.session.id] = [] - + if message not in chat_contexts[context.session.id]: chat_contexts[context.session.id].append(message) - + return message def remove(self, message: "Message") -> bool: diff --git a/backend/chainlit/data/acl.py b/backend/chainlit/data/acl.py index abc38649bd9..60e45ac97f3 100644 --- a/backend/chainlit/data/acl.py +++ b/backend/chainlit/data/acl.py @@ -9,7 +9,7 @@ async def is_thread_author(username: str, thread_id: str): raise HTTPException(status_code=400, detail="Data layer not initialized") thread_author = await data_layer.get_thread_author(thread_id) - + if not thread_author: raise HTTPException(status_code=404, detail="Thread not found") diff --git a/backend/chainlit/emitter.py b/backend/chainlit/emitter.py index 0219ad112be..2ef1bf2d9d5 100644 --- a/backend/chainlit/emitter.py +++ b/backend/chainlit/emitter.py @@ -53,15 +53,15 @@ async def resume_thread(self, thread_dict: ThreadDict): async def send_element(self, element_dict: ElementDict): """Stub method to send an element to the UI.""" pass - + async def update_audio_connection(self, state: Literal["on", "off"]): """Audio connection signaling.""" pass - + async def send_audio_chunk(self, chunk: OutputAudioChunk): """Stub method to send an audio chunk to the UI.""" pass - + async def send_audio_interrupt(self): """Stub method to interrupt the current audio response.""" pass @@ -178,7 +178,7 @@ async def update_audio_connection(self, state: Literal["on", "off"]): async def send_audio_chunk(self, chunk: OutputAudioChunk): """Send an audio chunk to the UI.""" await self.emit("audio_chunk", chunk) - + async def send_audio_interrupt(self): """Method to interrupt the current audio response.""" await self.emit("audio_interrupt", {}) diff --git a/backend/chainlit/haystack/callbacks.py b/backend/chainlit/haystack/callbacks.py index 92536366acb..bed15d8afd5 100644 --- a/backend/chainlit/haystack/callbacks.py +++ b/backend/chainlit/haystack/callbacks.py @@ -132,7 +132,7 @@ def on_tool_finish( tool_result: str, tool_name: Optional[str] = None, tool_input: Optional[str] = None, - **kwargs: Any + **kwargs: Any, ) -> None: # Tool finished, send step with tool_result tool_step = self.stack.pop() diff --git a/backend/chainlit/llama_index/callbacks.py b/backend/chainlit/llama_index/callbacks.py index a52b4fa0815..6ca0081cfe5 100644 --- a/backend/chainlit/llama_index/callbacks.py +++ b/backend/chainlit/llama_index/callbacks.py @@ -144,16 +144,15 @@ def on_event_end( context_var.get().loop.create_task(step.update()) elif event_type == CBEventType.LLM: - formatted_messages = payload.get( - EventPayload.MESSAGES - ) # type: Optional[List[ChatMessage]] + formatted_messages = payload.get(EventPayload.MESSAGES) # type: Optional[List[ChatMessage]] formatted_prompt = payload.get(EventPayload.PROMPT) response = payload.get(EventPayload.RESPONSE) if formatted_messages: messages = [ GenerationMessage( - role=m.role.value, content=m.content or "" # type: ignore + role=m.role.value, # type: ignore + content=m.content or "", ) for m in formatted_messages ] diff --git a/backend/chainlit/server.py b/backend/chainlit/server.py index d4a67759253..1edd68da8f7 100644 --- a/backend/chainlit/server.py +++ b/backend/chainlit/server.py @@ -897,7 +897,7 @@ async def get_file( detail="Unauthorized", ) - #TODO: Causes 401 error. See https://github.com/Chainlit/chainlit/issues/1472 + # TODO: Causes 401 error. See https://github.com/Chainlit/chainlit/issues/1472 # if current_user: # if not session.user or session.user.identifier != current_user.identifier: # raise HTTPException( diff --git a/backend/chainlit/session.py b/backend/chainlit/session.py index d456a1e419b..95a34cf8add 100644 --- a/backend/chainlit/session.py +++ b/backend/chainlit/session.py @@ -113,9 +113,10 @@ async def persist_file( if path: # Copy the file from the given path - async with aiofiles.open(path, "rb") as src, aiofiles.open( - file_path, "wb" - ) as dst: + async with ( + aiofiles.open(path, "rb") as src, + aiofiles.open(file_path, "wb") as dst, + ): await dst.write(await src.read()) elif content: # Write the provided content to the file diff --git a/backend/chainlit/socket.py b/backend/chainlit/socket.py index 4cfc42fa9a3..93e08852d63 100644 --- a/backend/chainlit/socket.py +++ b/backend/chainlit/socket.py @@ -320,10 +320,10 @@ async def audio_start(sid): context = init_ws_context(session) if config.code.on_audio_start: - connected = bool(await config.code.on_audio_start()) - connection_state = "on" if connected else "off" - await context.emitter.update_audio_connection(connection_state) - + connected = bool(await config.code.on_audio_start()) + connection_state = "on" if connected else "off" + await context.emitter.update_audio_connection(connection_state) + @sio.on("audio_chunk") async def audio_chunk(sid, payload: InputAudioChunkPayload): @@ -350,7 +350,7 @@ async def audio_end(sid): if config.code.on_audio_end: await config.code.on_audio_end() - + except asyncio.CancelledError: pass except Exception as e: diff --git a/backend/chainlit/types.py b/backend/chainlit/types.py index b094716f933..26c63c09e3f 100644 --- a/backend/chainlit/types.py +++ b/backend/chainlit/types.py @@ -168,11 +168,13 @@ class InputAudioChunk: elapsedTime: float data: bytes + class OutputAudioChunk(TypedDict): track: str mimeType: str data: bytes + @dataclass class AskFileResponse: id: str diff --git a/cypress/e2e/elements/main.py b/cypress/e2e/elements/main.py index 15f9def9176..d7a412231dd 100644 --- a/cypress/e2e/elements/main.py +++ b/cypress/e2e/elements/main.py @@ -3,13 +3,11 @@ @cl.step(type="tool") async def gen_img(): - return cl.Image(path="./cat.jpeg", name="image1", display="inline") @cl.on_chat_start async def start(): - img = await gen_img() # Element should not be inlined or referenced