Update src/database.py
Browse files- src/database.py +64 -42
src/database.py
CHANGED
|
@@ -79,18 +79,32 @@ class SupabaseClient:
|
|
| 79 |
|
| 80 |
if "evidence" in message:
|
| 81 |
# Convert evidence to string for storage
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
def get_conversation_history(self, consultation_id):
|
| 96 |
"""Retrieve full conversation history for a given consultation ID."""
|
|
@@ -99,38 +113,47 @@ class SupabaseClient:
|
|
| 99 |
"order": "timestamp.asc"
|
| 100 |
}
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
message_dict = {
|
| 117 |
-
"role": msg["role"],
|
| 118 |
-
"content": msg["content"]
|
| 119 |
-
}
|
| 120 |
|
| 121 |
-
|
| 122 |
-
message_dict["explanation"] = msg["explanation"]
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
history
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
def delete_conversation(self, consultation_id):
|
| 136 |
"""Delete a completed conversation from the database."""
|
|
@@ -168,5 +191,4 @@ class SupabaseClient:
|
|
| 168 |
# Initialize database client
|
| 169 |
def get_db_client():
|
| 170 |
"""Get database client instance."""
|
| 171 |
-
return SupabaseClient()
|
| 172 |
-
|
|
|
|
| 79 |
|
| 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",
|
| 93 |
+
data=message_data
|
| 94 |
+
)
|
| 95 |
+
|
| 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):
|
| 110 |
"""Retrieve full conversation history for a given consultation ID."""
|
|
|
|
| 113 |
"order": "timestamp.asc"
|
| 114 |
}
|
| 115 |
|
| 116 |
+
try:
|
| 117 |
+
response = self._make_request(
|
| 118 |
+
"GET",
|
| 119 |
+
"/rest/v1/messages",
|
| 120 |
+
params=params
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
if response.status_code != 200:
|
| 124 |
+
print(f"Failed to retrieve conversation history: Status {response.status_code}, Response: {response.text}")
|
| 125 |
+
return []
|
| 126 |
+
|
| 127 |
+
# Convert database format back to application format
|
| 128 |
+
messages = response.json()
|
| 129 |
+
print(f"Retrieved {len(messages)} messages from database for consultation {consultation_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 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):
|
| 159 |
"""Delete a completed conversation from the database."""
|
|
|
|
| 191 |
# Initialize database client
|
| 192 |
def get_db_client():
|
| 193 |
"""Get database client instance."""
|
| 194 |
+
return SupabaseClient()
|
|
|