From 670667567ea3116606e158bc28d8bd95ce335b2f Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 29 Nov 2023 20:31:07 +0100 Subject: [PATCH] Handle key upload errors and refine tool flow responses - Introduce error handling for the keys upload process, logging failures to assist with troubleshooting. - Improve exception handling in the OpenAI class by returning a more informative response based on the exception arguments if available. - Replace a return statement in the Newroom tool with an exception raise to standardize tool action termination and provide clearer flow control. Resolves issue with silent key upload failures. Refines response and control flow for better clarity and debugging. --- src/gptbot/callbacks/roommember.py | 7 +++++-- src/gptbot/classes/openai.py | 4 ++-- src/gptbot/tools/newroom.py | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/gptbot/callbacks/roommember.py b/src/gptbot/callbacks/roommember.py index 3016f03..8702516 100644 --- a/src/gptbot/callbacks/roommember.py +++ b/src/gptbot/callbacks/roommember.py @@ -1,7 +1,10 @@ -from nio import RoomMemberEvent, MatrixRoom +from nio import RoomMemberEvent, MatrixRoom, KeysUploadError async def roommember_callback(room: MatrixRoom, event: RoomMemberEvent, bot): - await bot.matrix_client.keys_upload() + try: + await bot.matrix_client.keys_upload() + except KeysUploadError as e: + bot.logger.log(f"Failed to upload keys: {e.message}") if event.membership == "leave": bot.logger.log(f"User {event.state_key} left room {room.room_id} - am I alone now?") diff --git a/src/gptbot/classes/openai.py b/src/gptbot/classes/openai.py index f4088fc..d9a1c99 100644 --- a/src/gptbot/classes/openai.py +++ b/src/gptbot/classes/openai.py @@ -294,8 +294,8 @@ class OpenAI: "tool_call_id": tool_call.id, "content": str(tool_response) }) - except StopProcessing: - return False, 0 + except StopProcessing as e: + return (e.args[0] if e.args else False), 0 except Handover: return await self.generate_chat_response(original_messages, user, room, allow_override=False, use_tools=False) diff --git a/src/gptbot/tools/newroom.py b/src/gptbot/tools/newroom.py index c874aea..6f37eba 100644 --- a/src/gptbot/tools/newroom.py +++ b/src/gptbot/tools/newroom.py @@ -1,4 +1,4 @@ -from .base import BaseTool +from .base import BaseTool, StopProcessing from nio import RoomCreateError, RoomInviteError @@ -54,4 +54,4 @@ class Newroom(BaseTool): await self.bot.matrix_client.room_put_state( new_room.room_id, "m.room.power_levels", {"users": {self.user: 100, self.bot.matrix_client.user_id: 100}}) - return "Created new Matrix room with ID " + new_room.room_id + " and invited user." \ No newline at end of file + raise StopProcessing("Created new Matrix room with ID " + new_room.room_id + " and invited user.") \ No newline at end of file