Spaces:
Running
Running
Girish Jeswani commited on
Commit ·
e0687fa
1
Parent(s): ff0136e
bug fix
Browse files
multi_llm_chatbot_backend/app/api/routes/chat_sessions.py
CHANGED
|
@@ -91,6 +91,34 @@ async def get_user_chat_sessions(
|
|
| 91 |
detail="Could not fetch chat sessions"
|
| 92 |
)
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
@router.get("/chat-sessions/{session_id}")
|
| 95 |
async def get_chat_session(
|
| 96 |
session_id: str,
|
|
@@ -223,29 +251,7 @@ async def save_message_to_session(
|
|
| 223 |
detail="Could not save message"
|
| 224 |
)
|
| 225 |
|
| 226 |
-
|
| 227 |
-
async def get_chat_sessions_count(
|
| 228 |
-
current_user: User = Depends(get_current_active_user)
|
| 229 |
-
):
|
| 230 |
-
"""Get count of user's chat sessions"""
|
| 231 |
-
try:
|
| 232 |
-
db = get_database()
|
| 233 |
-
user_object_id = ObjectId(str(current_user.id))
|
| 234 |
-
|
| 235 |
-
count = await db.chat_sessions.count_documents({
|
| 236 |
-
"user_id": user_object_id,
|
| 237 |
-
"is_active": {"$ne": False},
|
| 238 |
-
"deleted_at": {"$exists": False}
|
| 239 |
-
})
|
| 240 |
-
|
| 241 |
-
return {"count": count}
|
| 242 |
-
|
| 243 |
-
except Exception as e:
|
| 244 |
-
logger.error(f"Error counting chat sessions for user {current_user.id}: {e}")
|
| 245 |
-
raise HTTPException(
|
| 246 |
-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 247 |
-
detail="Failed to count chat sessions"
|
| 248 |
-
)
|
| 249 |
|
| 250 |
@router.delete("/chat-sessions/{session_id}")
|
| 251 |
async def delete_chat_session(
|
|
|
|
| 91 |
detail="Could not fetch chat sessions"
|
| 92 |
)
|
| 93 |
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@router.get("/chat-sessions/count")
|
| 97 |
+
async def get_chat_sessions_count(
|
| 98 |
+
current_user: User = Depends(get_current_active_user)
|
| 99 |
+
):
|
| 100 |
+
"""Get count of user's chat sessions"""
|
| 101 |
+
try:
|
| 102 |
+
db = get_database()
|
| 103 |
+
user_object_id = ObjectId(str(current_user.id))
|
| 104 |
+
|
| 105 |
+
count = await db.chat_sessions.count_documents({
|
| 106 |
+
"user_id": user_object_id,
|
| 107 |
+
"is_active": {"$ne": False},
|
| 108 |
+
"deleted_at": {"$exists": False}
|
| 109 |
+
})
|
| 110 |
+
|
| 111 |
+
return {"count": count}
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
logger.error(f"Error counting chat sessions for user {current_user.id}: {e}")
|
| 115 |
+
raise HTTPException(
|
| 116 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 117 |
+
detail="Failed to count chat sessions"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
@router.get("/chat-sessions/{session_id}")
|
| 123 |
async def get_chat_session(
|
| 124 |
session_id: str,
|
|
|
|
| 251 |
detail="Could not save message"
|
| 252 |
)
|
| 253 |
|
| 254 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
@router.delete("/chat-sessions/{session_id}")
|
| 257 |
async def delete_chat_session(
|
multi_llm_chatbot_backend/app/core/canvas_database.py
CHANGED
|
@@ -23,11 +23,11 @@ async def setup_canvas_collections(db: AsyncIOMotorDatabase):
|
|
| 23 |
if isinstance(index, list):
|
| 24 |
# Compound index
|
| 25 |
await collection.create_index(index)
|
| 26 |
-
logger.info(f"Created compound index: {index}")
|
| 27 |
else:
|
| 28 |
# Simple index
|
| 29 |
await collection.create_index(index)
|
| 30 |
-
logger.info(f"Created index: {index}")
|
| 31 |
|
| 32 |
# Ensure TTL index for old canvases (optional cleanup after 2 years)
|
| 33 |
await collection.create_index(
|
|
@@ -72,5 +72,4 @@ async def cleanup_old_canvas_data(db: AsyncIOMotorDatabase):
|
|
| 72 |
logger.info("No orphaned canvas records found")
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
logger.error(f"Error during canvas cleanup: {e}")
|
| 76 |
-
|
|
|
|
| 23 |
if isinstance(index, list):
|
| 24 |
# Compound index
|
| 25 |
await collection.create_index(index)
|
| 26 |
+
logger.info(f"Created compound index: {str(index)}")
|
| 27 |
else:
|
| 28 |
# Simple index
|
| 29 |
await collection.create_index(index)
|
| 30 |
+
logger.info(f"Created index: {str(index)}")
|
| 31 |
|
| 32 |
# Ensure TTL index for old canvases (optional cleanup after 2 years)
|
| 33 |
await collection.create_index(
|
|
|
|
| 72 |
logger.info("No orphaned canvas records found")
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
+
logger.error(f"Error during canvas cleanup: {e}")
|
|
|
multi_llm_chatbot_backend/app/core/database.py
CHANGED
|
@@ -36,7 +36,7 @@ async def connect_to_mongo():
|
|
| 36 |
await create_indexes()
|
| 37 |
try:
|
| 38 |
from app.core.canvas_database import setup_canvas_collections
|
| 39 |
-
await setup_canvas_collections(db) # Pass db directly
|
| 40 |
logger.info("Canvas database initialization completed")
|
| 41 |
except Exception as canvas_error:
|
| 42 |
logger.error(f"Canvas database initialization failed: {canvas_error}")
|
|
|
|
| 36 |
await create_indexes()
|
| 37 |
try:
|
| 38 |
from app.core.canvas_database import setup_canvas_collections
|
| 39 |
+
await setup_canvas_collections(db.database) # Pass db directly
|
| 40 |
logger.info("Canvas database initialization completed")
|
| 41 |
except Exception as canvas_error:
|
| 42 |
logger.error(f"Canvas database initialization failed: {canvas_error}")
|
phd-advisor-frontend/src/pages/CanvasPage.js
CHANGED
|
@@ -95,18 +95,26 @@ const CanvasPage = ({ user, authToken, onNavigateToChat, onSignOut }) => {
|
|
| 95 |
const [isProcessingFirstTime, setIsProcessingFirstTime] = useState(false);
|
| 96 |
|
| 97 |
useEffect(() => {
|
|
|
|
|
|
|
| 98 |
const initializeCanvas = async () => {
|
| 99 |
await fetchCanvas();
|
| 100 |
await fetchStats();
|
| 101 |
await triggerAutoUpdate();
|
| 102 |
|
| 103 |
-
// After initial load, check if we need to populate empty canvas
|
| 104 |
setTimeout(() => {
|
| 105 |
checkForEmptyCanvasWithChats();
|
| 106 |
-
}, 2000);
|
| 107 |
};
|
| 108 |
|
| 109 |
initializeCanvas();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
}, []);
|
| 111 |
|
| 112 |
const fetchCanvas = async () => {
|
|
@@ -161,11 +169,9 @@ const CanvasPage = ({ user, authToken, onNavigateToChat, onSignOut }) => {
|
|
| 161 |
// Check if user has chats but empty canvas
|
| 162 |
const checkForEmptyCanvasWithChats = async () => {
|
| 163 |
try {
|
| 164 |
-
// Check if canvas is empty (no insights)
|
| 165 |
const isEmpty = !canvasData || canvasData.total_insights === 0;
|
| 166 |
|
| 167 |
if (isEmpty) {
|
| 168 |
-
// Check if user has any chat sessions
|
| 169 |
const response = await fetch(`${process.env.REACT_APP_API_URL}/api/chat-sessions/count`, {
|
| 170 |
headers: {
|
| 171 |
'Authorization': `Bearer ${authToken}`,
|
|
@@ -175,16 +181,20 @@ const CanvasPage = ({ user, authToken, onNavigateToChat, onSignOut }) => {
|
|
| 175 |
|
| 176 |
if (response.ok) {
|
| 177 |
const { count } = await response.json();
|
| 178 |
-
|
| 179 |
if (count > 0) {
|
| 180 |
-
// User has chats but empty canvas - trigger full refresh
|
| 181 |
console.log(`User has ${count} chats but empty canvas. Triggering full refresh.`);
|
| 182 |
await handleFullRefresh();
|
| 183 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
}
|
| 185 |
}
|
| 186 |
} catch (error) {
|
| 187 |
console.error('Error checking for empty canvas with chats:', error);
|
|
|
|
|
|
|
| 188 |
}
|
| 189 |
};
|
| 190 |
|
|
|
|
| 95 |
const [isProcessingFirstTime, setIsProcessingFirstTime] = useState(false);
|
| 96 |
|
| 97 |
useEffect(() => {
|
| 98 |
+
let pollInterval = null;
|
| 99 |
+
|
| 100 |
const initializeCanvas = async () => {
|
| 101 |
await fetchCanvas();
|
| 102 |
await fetchStats();
|
| 103 |
await triggerAutoUpdate();
|
| 104 |
|
|
|
|
| 105 |
setTimeout(() => {
|
| 106 |
checkForEmptyCanvasWithChats();
|
| 107 |
+
}, 2000);
|
| 108 |
};
|
| 109 |
|
| 110 |
initializeCanvas();
|
| 111 |
+
|
| 112 |
+
// Cleanup on unmount
|
| 113 |
+
return () => {
|
| 114 |
+
if (pollInterval) {
|
| 115 |
+
clearInterval(pollInterval);
|
| 116 |
+
}
|
| 117 |
+
};
|
| 118 |
}, []);
|
| 119 |
|
| 120 |
const fetchCanvas = async () => {
|
|
|
|
| 169 |
// Check if user has chats but empty canvas
|
| 170 |
const checkForEmptyCanvasWithChats = async () => {
|
| 171 |
try {
|
|
|
|
| 172 |
const isEmpty = !canvasData || canvasData.total_insights === 0;
|
| 173 |
|
| 174 |
if (isEmpty) {
|
|
|
|
| 175 |
const response = await fetch(`${process.env.REACT_APP_API_URL}/api/chat-sessions/count`, {
|
| 176 |
headers: {
|
| 177 |
'Authorization': `Bearer ${authToken}`,
|
|
|
|
| 181 |
|
| 182 |
if (response.ok) {
|
| 183 |
const { count } = await response.json();
|
|
|
|
| 184 |
if (count > 0) {
|
|
|
|
| 185 |
console.log(`User has ${count} chats but empty canvas. Triggering full refresh.`);
|
| 186 |
await handleFullRefresh();
|
| 187 |
}
|
| 188 |
+
} else {
|
| 189 |
+
console.error('Failed to fetch chat sessions count:', response.status);
|
| 190 |
+
// Don't trigger refresh if count fails
|
| 191 |
+
return;
|
| 192 |
}
|
| 193 |
}
|
| 194 |
} catch (error) {
|
| 195 |
console.error('Error checking for empty canvas with chats:', error);
|
| 196 |
+
// Stop the polling if there's an error
|
| 197 |
+
return;
|
| 198 |
}
|
| 199 |
};
|
| 200 |
|