Skip to content

Commit

Permalink
Merge pull request #910 from sambarza/55-document-websocket-notificat…
Browse files Browse the repository at this point in the history
…ion-types

Updated docstring close #cheshire-cat-ai/docs#55
  • Loading branch information
pieroit authored Sep 14, 2024
2 parents 8e55a4b + 2e3336a commit c8680b5
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion core/cat/looking_glass/stray_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def send_ws_message(self, content: str, msg_type: MSG_TYPES = "notification"):
"""Send a message via websocket.
This method is useful for sending a message via websocket directly without passing through the LLM
In case there is no connection the message is skipped and a warning is logged
Parameters
----------
Expand Down Expand Up @@ -116,6 +117,14 @@ def send_ws_message(self, content: str, msg_type: MSG_TYPES = "notification"):
self.__send_ws_json({"type": msg_type, "content": content})

def send_chat_message(self, message: Union[str, CatMessage], save=False):
"""Sends a chat message to the user using the active WebSocket connection.
In case there is no connection the message is skipped and a warning is logged
Args:
message (Union[str, CatMessage]): message to send
save (bool, optional): Save the message in the conversation history. Defaults to False.
"""
if self.__ws is None:
log.warning(f"No websocket connection is open for user {self.user_id}")
return
Expand All @@ -132,9 +141,23 @@ def send_chat_message(self, message: Union[str, CatMessage], save=False):
self.__send_ws_json(message.model_dump())

def send_notification(self, content: str):
"""Sends a notification message to the user using the active WebSocket connection.
In case there is no connection the message is skipped and a warning is logged
Args:
content (str): message to send
"""
self.send_ws_message(content=content, msg_type="notification")

def send_error(self, error: Union[str, Exception]):
"""Sends an error message to the user using the active WebSocket connection.
In case there is no connection the message is skipped and a warning is logged
Args:
error (Union[str, Exception]): message to send
"""
if self.__ws is None:
log.warning(f"No websocket connection is open for user {self.user_id}")
return
Expand Down Expand Up @@ -318,7 +341,6 @@ def llm(self, prompt: str, stream: bool = False) -> str:

return output


async def __call__(self, message_dict):
"""Call the Cat instance.
Expand Down

0 comments on commit c8680b5

Please sign in to comment.