Spaces:
Sleeping
Sleeping
Commit
·
9becbae
1
Parent(s):
2fc4e76
Update api_routes_v2.py
Browse files- api_routes_v2.py +143 -0
api_routes_v2.py
CHANGED
|
@@ -634,6 +634,149 @@ def upload_stream_to_s3(chat_id: str, file: UploadFile) -> str:
|
|
| 634 |
return s3_uri
|
| 635 |
|
| 636 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
# ========================
|
| 638 |
# UNIFIED CHAT (non-streaming)
|
| 639 |
# ========================
|
|
|
|
| 634 |
return s3_uri
|
| 635 |
|
| 636 |
|
| 637 |
+
# ========================
|
| 638 |
+
# GET ALL SESSIONS ENDPOINT
|
| 639 |
+
# ========================
|
| 640 |
+
|
| 641 |
+
@router.get("/sessions", response_model=Dict[str, Any])
|
| 642 |
+
async def get_all_sessions(
|
| 643 |
+
limit: int = 100,
|
| 644 |
+
skip: int = 0,
|
| 645 |
+
include_stats: bool = False
|
| 646 |
+
):
|
| 647 |
+
"""
|
| 648 |
+
Get all session IDs from the database with optional pagination
|
| 649 |
+
|
| 650 |
+
Args:
|
| 651 |
+
limit: Maximum number of sessions to return (default: 100)
|
| 652 |
+
skip: Number of sessions to skip (for pagination)
|
| 653 |
+
include_stats: If True, include session statistics
|
| 654 |
+
|
| 655 |
+
Returns:
|
| 656 |
+
List of sessions with basic info or full stats
|
| 657 |
+
"""
|
| 658 |
+
try:
|
| 659 |
+
all_session_ids = session_manager.get_all_session_ids()
|
| 660 |
+
|
| 661 |
+
if not all_session_ids:
|
| 662 |
+
return {"sessions": [], "pagination": {"total": 0, "returned": 0}}
|
| 663 |
+
|
| 664 |
+
# Apply pagination
|
| 665 |
+
total_sessions = len(all_session_ids)
|
| 666 |
+
paginated_ids = all_session_ids[skip:skip + limit]
|
| 667 |
+
|
| 668 |
+
if not include_stats:
|
| 669 |
+
# Return just session IDs with pagination info
|
| 670 |
+
sessions_basic = [
|
| 671 |
+
{
|
| 672 |
+
"session_id": sid,
|
| 673 |
+
"created_at": None,
|
| 674 |
+
"last_activity": None
|
| 675 |
+
}
|
| 676 |
+
for sid in paginated_ids
|
| 677 |
+
]
|
| 678 |
+
|
| 679 |
+
return {
|
| 680 |
+
"sessions": sessions_basic,
|
| 681 |
+
"pagination": {
|
| 682 |
+
"total": total_sessions,
|
| 683 |
+
"returned": len(sessions_basic),
|
| 684 |
+
"limit": limit,
|
| 685 |
+
"skip": skip,
|
| 686 |
+
"has_more": total_sessions > (skip + limit)
|
| 687 |
+
}
|
| 688 |
+
}
|
| 689 |
+
|
| 690 |
+
# Include detailed statistics for each session
|
| 691 |
+
sessions_with_stats = []
|
| 692 |
+
for session_id in paginated_ids:
|
| 693 |
+
session = session_manager.get_session(session_id)
|
| 694 |
+
if session:
|
| 695 |
+
# Format datetime objects for JSON serialization
|
| 696 |
+
created_at = session.get("created_at")
|
| 697 |
+
last_activity = session.get("last_activity")
|
| 698 |
+
|
| 699 |
+
if isinstance(created_at, datetime):
|
| 700 |
+
created_at = created_at.isoformat()
|
| 701 |
+
if isinstance(last_activity, datetime):
|
| 702 |
+
last_activity = last_activity.isoformat()
|
| 703 |
+
|
| 704 |
+
sessions_with_stats.append({
|
| 705 |
+
"session_id": session_id,
|
| 706 |
+
"user_id": session.get("user_id"),
|
| 707 |
+
"created_at": created_at,
|
| 708 |
+
"last_activity": last_activity,
|
| 709 |
+
"state": session.get("state", "unknown"),
|
| 710 |
+
"current_file": session.get("current_file"),
|
| 711 |
+
"stats": session.get("stats", {}),
|
| 712 |
+
"total_messages": len(session.get("conversation_history", [])),
|
| 713 |
+
"pipeline_executions_count": len(session.get("pipeline_executions", []))
|
| 714 |
+
})
|
| 715 |
+
|
| 716 |
+
return {
|
| 717 |
+
"sessions": sessions_with_stats,
|
| 718 |
+
"pagination": {
|
| 719 |
+
"total": total_sessions,
|
| 720 |
+
"returned": len(sessions_with_stats),
|
| 721 |
+
"limit": limit,
|
| 722 |
+
"skip": skip,
|
| 723 |
+
"has_more": total_sessions > (skip + limit)
|
| 724 |
+
}
|
| 725 |
+
}
|
| 726 |
+
|
| 727 |
+
except Exception as e:
|
| 728 |
+
raise HTTPException(
|
| 729 |
+
status_code=500,
|
| 730 |
+
detail=f"Error retrieving sessions: {str(e)}"
|
| 731 |
+
)
|
| 732 |
+
|
| 733 |
+
|
| 734 |
+
# ========================
|
| 735 |
+
# GET SESSION HISTORY ENDPOINT (with session_id in response)
|
| 736 |
+
# ========================
|
| 737 |
+
|
| 738 |
+
@router.get("/sessions/{session_id}/history", response_model=Dict[str, Any])
|
| 739 |
+
async def get_session_history(
|
| 740 |
+
session_id: str,
|
| 741 |
+
limit: int = 50
|
| 742 |
+
):
|
| 743 |
+
"""
|
| 744 |
+
Get conversation history for a session
|
| 745 |
+
|
| 746 |
+
Args:
|
| 747 |
+
session_id: Session identifier
|
| 748 |
+
limit: Maximum number of messages to return (default: 50)
|
| 749 |
+
|
| 750 |
+
Returns:
|
| 751 |
+
Session history including session ID
|
| 752 |
+
"""
|
| 753 |
+
try:
|
| 754 |
+
# Get the history using session manager
|
| 755 |
+
history = session_manager.get_session_history(session_id, limit)
|
| 756 |
+
|
| 757 |
+
# Format datetime objects to ISO strings for JSON serialization
|
| 758 |
+
formatted_history = []
|
| 759 |
+
for msg in history:
|
| 760 |
+
msg_copy = msg.copy()
|
| 761 |
+
if "timestamp" in msg_copy and isinstance(msg_copy["timestamp"], datetime):
|
| 762 |
+
msg_copy["timestamp"] = msg_copy["timestamp"].isoformat()
|
| 763 |
+
formatted_history.append(msg_copy)
|
| 764 |
+
|
| 765 |
+
# Return with session_id included
|
| 766 |
+
return {
|
| 767 |
+
"session_id": session_id,
|
| 768 |
+
"history": formatted_history,
|
| 769 |
+
"count": len(formatted_history),
|
| 770 |
+
"limit": limit
|
| 771 |
+
}
|
| 772 |
+
|
| 773 |
+
except Exception as e:
|
| 774 |
+
raise HTTPException(
|
| 775 |
+
status_code=500,
|
| 776 |
+
detail=f"Error retrieving session history: {str(e)}"
|
| 777 |
+
)
|
| 778 |
+
|
| 779 |
+
|
| 780 |
# ========================
|
| 781 |
# UNIFIED CHAT (non-streaming)
|
| 782 |
# ========================
|