Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket Fix Proposal #936

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions core/cat/auth/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ async def get_user_stray(self, user: AuthUserInfo, connection: WebSocket) -> Str

if user.id in strays.keys():
stray = strays[user.id]
# Close previus ws connection
if stray._StrayCat__ws:
await stray._StrayCat__ws.close()
await stray.close_connection()

# Set new ws connection
stray._StrayCat__ws = connection
stray.reset_connection(connection)
log.info(
f"New websocket connection for user '{user.id}', the old one has been closed."
)
Expand Down
23 changes: 22 additions & 1 deletion core/cat/looking_glass/stray_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from cat.convo.messages import CatMessage, UserMessage, MessageWhy, Role, EmbedderModelInteraction
from cat.agents import AgentOutput
from cat import utils
from websockets.exceptions import ConnectionClosedOK

MSG_TYPES = Literal["notification", "chat", "error", "chat_token"]

Expand Down Expand Up @@ -484,7 +485,13 @@ def run(self, user_message_json, return_message=False):
if return_message:
return {"error": str(e)}
else:
self.send_error(e)
try:
self.send_error(e)
except ConnectionClosedOK as ex:
log.warning(ex)
if self.__ws:
del self.__ws
self.__ws = None

def classify(
self, sentence: str, labels: List[str] | Dict[str, List[str]]
Expand Down Expand Up @@ -599,6 +606,20 @@ def langchainfy_chat_history(self, latest_n: int = 5) -> List[BaseMessage]:

return langchain_chat_history

async def close_connection(self):
if self.__ws:
try:
await self.__ws.close()
except RuntimeError as ex:
log.warning(ex)
if self.__ws:
del self.__ws
self.__ws = None

def reset_connection(self, connection):
"""Reset the connection to the API service."""
self.__ws = connection

@property
def user_id(self):
return self.__user_id
Expand Down
Loading