Spaces:
Sleeping
Sleeping
Commit
·
52b54a6
1
Parent(s):
b8b4625
Update api_routes_v2.py
Browse files- api_routes_v2.py +28 -23
api_routes_v2.py
CHANGED
|
@@ -670,13 +670,14 @@ def download_to_temp_file(file_ref: Optional[str]) -> Tuple[Optional[str], Calla
|
|
| 670 |
# Already a local path
|
| 671 |
return file_ref, noop
|
| 672 |
|
| 673 |
-
def upload_stream_to_s3(chat_id: str, file: UploadFile) -> str:
|
| 674 |
"""
|
| 675 |
Stream an UploadFile directly to S3, return s3:// URI.
|
| 676 |
Supports optional SSE via env S3_SSE and S3_KMS_KEY_ID.
|
| 677 |
CHANGE:
|
| 678 |
- Generate a presigned GET URL with max expiry (7 days) once at upload time.
|
| 679 |
- Store presigned_url and presigned_expires_at in session.file_metadata (do not regenerate later).
|
|
|
|
| 680 |
"""
|
| 681 |
key = f"{S3_PREFIX}/{chat_id}/{file.filename}"
|
| 682 |
config = TransferConfig(multipart_threshold=8 * 1024 * 1024, max_concurrency=4)
|
|
@@ -725,25 +726,27 @@ def upload_stream_to_s3(chat_id: str, file: UploadFile) -> str:
|
|
| 725 |
}
|
| 726 |
})
|
| 727 |
|
| 728 |
-
#
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
"
|
| 735 |
-
"
|
| 736 |
-
|
|
|
|
|
|
|
|
|
|
| 737 |
}
|
| 738 |
-
}
|
| 739 |
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
|
| 748 |
return s3_uri
|
| 749 |
|
|
@@ -757,10 +760,10 @@ async def get_all_sessions(
|
|
| 757 |
skip: int = 0,
|
| 758 |
include_stats: bool = False
|
| 759 |
):
|
| 760 |
-
"""
|
| 761 |
-
Get all session IDs from the database with optional pagination
|
| 762 |
-
CHANGE: When include_stats=True, include 'chat_name' (added field only).
|
| 763 |
-
|
| 764 |
try:
|
| 765 |
all_session_ids = session_manager.get_all_session_ids()
|
| 766 |
|
|
@@ -1067,7 +1070,9 @@ async def chat_unified(
|
|
| 1067 |
# If a file is included in the form, upload to S3 and attach it
|
| 1068 |
file_info = None
|
| 1069 |
if file is not None:
|
| 1070 |
-
|
|
|
|
|
|
|
| 1071 |
meta = (session_manager.get_session(chat_id) or {}).get("file_metadata", {}) or {}
|
| 1072 |
file_info = {
|
| 1073 |
"bucket": S3_BUCKET,
|
|
|
|
| 670 |
# Already a local path
|
| 671 |
return file_ref, noop
|
| 672 |
|
| 673 |
+
def upload_stream_to_s3(chat_id: str, file: UploadFile, create_message: bool = True) -> str:
|
| 674 |
"""
|
| 675 |
Stream an UploadFile directly to S3, return s3:// URI.
|
| 676 |
Supports optional SSE via env S3_SSE and S3_KMS_KEY_ID.
|
| 677 |
CHANGE:
|
| 678 |
- Generate a presigned GET URL with max expiry (7 days) once at upload time.
|
| 679 |
- Store presigned_url and presigned_expires_at in session.file_metadata (do not regenerate later).
|
| 680 |
+
- create_message: If True, creates "Uploaded file" message. Set to False when file is sent with user message.
|
| 681 |
"""
|
| 682 |
key = f"{S3_PREFIX}/{chat_id}/{file.filename}"
|
| 683 |
config = TransferConfig(multipart_threshold=8 * 1024 * 1024, max_concurrency=4)
|
|
|
|
| 726 |
}
|
| 727 |
})
|
| 728 |
|
| 729 |
+
# Only create file upload message if requested (not when file is sent with user message)
|
| 730 |
+
if create_message:
|
| 731 |
+
# Create a user message with file metadata (instead of system message)
|
| 732 |
+
file_message = {
|
| 733 |
+
"role": "user",
|
| 734 |
+
"content": f"Uploaded file: {file.filename}",
|
| 735 |
+
"timestamp": datetime.utcnow().isoformat() + "Z",
|
| 736 |
+
"file_data": {
|
| 737 |
+
"has_file": True,
|
| 738 |
+
"file_name": file.filename,
|
| 739 |
+
"file_url": presigned["presigned_url"] # Use presigned URL for user access
|
| 740 |
+
}
|
| 741 |
}
|
|
|
|
| 742 |
|
| 743 |
+
# V3 RULE: Append to S3 conversation history
|
| 744 |
+
# 1. Load existing
|
| 745 |
+
current_messages = _load_conversation_from_s3(chat_id)
|
| 746 |
+
# 2. Append
|
| 747 |
+
current_messages.append(file_message)
|
| 748 |
+
# 3. Save to S3 (and update MongoDB metadata)
|
| 749 |
+
_save_conversation_to_s3(chat_id, current_messages)
|
| 750 |
|
| 751 |
return s3_uri
|
| 752 |
|
|
|
|
| 760 |
skip: int = 0,
|
| 761 |
include_stats: bool = False
|
| 762 |
):
|
| 763 |
+
# """
|
| 764 |
+
# Get all session IDs from the database with optional pagination
|
| 765 |
+
# CHANGE: When include_stats=True, include 'chat_name' (added field only)."""
|
| 766 |
+
|
| 767 |
try:
|
| 768 |
all_session_ids = session_manager.get_all_session_ids()
|
| 769 |
|
|
|
|
| 1070 |
# If a file is included in the form, upload to S3 and attach it
|
| 1071 |
file_info = None
|
| 1072 |
if file is not None:
|
| 1073 |
+
# Don't create automatic "Uploaded file" message if user is also sending text
|
| 1074 |
+
has_user_message = message and str(message).strip() != ""
|
| 1075 |
+
s3_uri = upload_stream_to_s3(chat_id, file, create_message=not has_user_message)
|
| 1076 |
meta = (session_manager.get_session(chat_id) or {}).get("file_metadata", {}) or {}
|
| 1077 |
file_info = {
|
| 1078 |
"bucket": S3_BUCKET,
|