Update src/database.py
Browse files- src/database.py +23 -51
src/database.py
CHANGED
|
@@ -80,19 +80,13 @@ class SupabaseClient:
|
|
| 80 |
if "evidence" in message:
|
| 81 |
# Convert evidence to string for storage
|
| 82 |
try:
|
| 83 |
-
|
| 84 |
-
message_data["evidence"] = json.dumps(message.get("evidence"), default=str)
|
| 85 |
except Exception as e:
|
| 86 |
-
# If JSON serialization fails,
|
| 87 |
message_data["evidence"] = "[]"
|
| 88 |
print(f"Failed to serialize evidence: {str(e)}")
|
| 89 |
-
# Print the evidence structure for debugging
|
| 90 |
-
print(f"Evidence structure causing error: {type(message.get('evidence'))}")
|
| 91 |
|
| 92 |
try:
|
| 93 |
-
# Log the operation
|
| 94 |
-
print(f"Saving message for consultation {consultation_id}, role: {message.get('role')}")
|
| 95 |
-
|
| 96 |
response = self._make_request(
|
| 97 |
"POST",
|
| 98 |
"/rest/v1/messages",
|
|
@@ -102,18 +96,14 @@ class SupabaseClient:
|
|
| 102 |
if response.status_code not in (200, 201):
|
| 103 |
print(f"Failed to save message: Status {response.status_code}, Response: {response.text}")
|
| 104 |
# Attempt to log the message that failed to save
|
| 105 |
-
print(f"Message data: {json.dumps(message_data
|
| 106 |
return None
|
| 107 |
|
| 108 |
-
print(f"Successfully saved message to database")
|
| 109 |
return response.json()
|
| 110 |
except Exception as e:
|
| 111 |
print(f"Exception saving message to database: {str(e)}")
|
| 112 |
# Attempt to log the message that failed to save
|
| 113 |
-
|
| 114 |
-
print(f"Message data: {json.dumps(message_data, default=str)}")
|
| 115 |
-
except:
|
| 116 |
-
print("Could not serialize message data for logging")
|
| 117 |
return None
|
| 118 |
|
| 119 |
def get_conversation_history(self, consultation_id):
|
|
@@ -124,7 +114,6 @@ class SupabaseClient:
|
|
| 124 |
}
|
| 125 |
|
| 126 |
try:
|
| 127 |
-
print(f"Retrieving conversation history for consultation {consultation_id}")
|
| 128 |
response = self._make_request(
|
| 129 |
"GET",
|
| 130 |
"/rest/v1/messages",
|
|
@@ -142,46 +131,28 @@ class SupabaseClient:
|
|
| 142 |
history = []
|
| 143 |
|
| 144 |
for msg in messages:
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
history.append(message_dict)
|
| 164 |
-
except Exception as e:
|
| 165 |
-
print(f"Error processing message from database: {str(e)}")
|
| 166 |
-
# Try to include partial message info for debugging
|
| 167 |
-
print(f"Problematic message keys: {msg.keys() if isinstance(msg, dict) else 'Not a dict'}")
|
| 168 |
|
| 169 |
-
print(f"Successfully processed {len(history)} messages for history")
|
| 170 |
return history
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
print(f"Exception retrieving conversation history: {str(e)}")
|
| 174 |
-
# Try to reconnect or handle database connection issues
|
| 175 |
-
try:
|
| 176 |
-
# Simple test to check if connection is still valid
|
| 177 |
-
test_response = self._make_request("GET", "/rest/v1/health")
|
| 178 |
-
if test_response.status_code != 200:
|
| 179 |
-
print("Database connection may be down, returned status: {test_response.status_code}")
|
| 180 |
-
else:
|
| 181 |
-
print("Database connection appears to be working despite retrieval error")
|
| 182 |
-
except Exception as conn_error:
|
| 183 |
-
print(f"Failed to connect to database: {str(conn_error)}")
|
| 184 |
-
|
| 185 |
return []
|
| 186 |
|
| 187 |
def delete_conversation(self, consultation_id):
|
|
@@ -220,4 +191,5 @@ class SupabaseClient:
|
|
| 220 |
# Initialize database client
|
| 221 |
def get_db_client():
|
| 222 |
"""Get database client instance."""
|
| 223 |
-
return SupabaseClient()
|
|
|
|
|
|
| 80 |
if "evidence" in message:
|
| 81 |
# Convert evidence to string for storage
|
| 82 |
try:
|
| 83 |
+
message_data["evidence"] = json.dumps(message.get("evidence"))
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
+
# If JSON serialization fails, store as empty array
|
| 86 |
message_data["evidence"] = "[]"
|
| 87 |
print(f"Failed to serialize evidence: {str(e)}")
|
|
|
|
|
|
|
| 88 |
|
| 89 |
try:
|
|
|
|
|
|
|
|
|
|
| 90 |
response = self._make_request(
|
| 91 |
"POST",
|
| 92 |
"/rest/v1/messages",
|
|
|
|
| 96 |
if response.status_code not in (200, 201):
|
| 97 |
print(f"Failed to save message: Status {response.status_code}, Response: {response.text}")
|
| 98 |
# Attempt to log the message that failed to save
|
| 99 |
+
print(f"Message data: {json.dumps(message_data)}")
|
| 100 |
return None
|
| 101 |
|
|
|
|
| 102 |
return response.json()
|
| 103 |
except Exception as e:
|
| 104 |
print(f"Exception saving message to database: {str(e)}")
|
| 105 |
# Attempt to log the message that failed to save
|
| 106 |
+
print(f"Message data: {json.dumps(message_data)}")
|
|
|
|
|
|
|
|
|
|
| 107 |
return None
|
| 108 |
|
| 109 |
def get_conversation_history(self, consultation_id):
|
|
|
|
| 114 |
}
|
| 115 |
|
| 116 |
try:
|
|
|
|
| 117 |
response = self._make_request(
|
| 118 |
"GET",
|
| 119 |
"/rest/v1/messages",
|
|
|
|
| 131 |
history = []
|
| 132 |
|
| 133 |
for msg in messages:
|
| 134 |
+
message_dict = {
|
| 135 |
+
"role": msg["role"],
|
| 136 |
+
"content": msg["content"]
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
if msg.get("explanation"):
|
| 140 |
+
message_dict["explanation"] = msg["explanation"]
|
| 141 |
+
|
| 142 |
+
if msg.get("evidence"):
|
| 143 |
+
# Parse evidence JSON string back to object
|
| 144 |
+
try:
|
| 145 |
+
message_dict["evidence"] = json.loads(msg["evidence"])
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f"Failed to parse evidence JSON: {str(e)}")
|
| 148 |
+
message_dict["evidence"] = []
|
| 149 |
+
|
| 150 |
+
history.append(message_dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
|
|
|
| 152 |
return history
|
| 153 |
|
| 154 |
except Exception as e:
|
| 155 |
print(f"Exception retrieving conversation history: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
return []
|
| 157 |
|
| 158 |
def delete_conversation(self, consultation_id):
|
|
|
|
| 191 |
# Initialize database client
|
| 192 |
def get_db_client():
|
| 193 |
"""Get database client instance."""
|
| 194 |
+
return SupabaseClient()
|
| 195 |
+
|