Spaces:
Sleeping
Sleeping
Commit
·
fc7cdd2
1
Parent(s):
65abc8a
Update api_routes_v2.py
Browse files- api_routes_v2.py +73 -48
api_routes_v2.py
CHANGED
|
@@ -639,12 +639,16 @@ def _build_api_result_summary(result: Dict[str, Any], session: Dict[str, Any]) -
|
|
| 639 |
metadata["pages_processed"] = pages_processed
|
| 640 |
return {"summary": summary, "metadata": metadata}
|
| 641 |
|
| 642 |
-
def _add_and_mirror_message(
|
| 643 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
"""
|
| 645 |
V3 RULE: Append message to S3 conversation.
|
| 646 |
-
|
| 647 |
-
Updates last_activity for all messages (user and assistant interactions).
|
| 648 |
"""
|
| 649 |
# 1. Load existing
|
| 650 |
current_messages = _load_conversation_from_s3(chat_id)
|
|
@@ -656,21 +660,63 @@ def _add_and_mirror_message(chat_id: str, role: str, content: str, file_metadata
|
|
| 656 |
"timestamp": datetime.utcnow().isoformat() + "Z"
|
| 657 |
}
|
| 658 |
|
| 659 |
-
# Add
|
| 660 |
-
if role == "assistant" and pipeline_result
|
| 661 |
-
|
| 662 |
-
if
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
"
|
| 673 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
# Add file metadata if provided
|
| 676 |
if file_metadata:
|
|
@@ -700,7 +746,7 @@ def _assistant_response_payload(
|
|
| 700 |
output: Optional[Dict[str, Any]] = None,
|
| 701 |
final_output: Optional[Dict[str, Any]] = None,
|
| 702 |
exception: Optional[str] = None,
|
| 703 |
-
pipeline_result: Optional[Dict[str, Any]] = None # NEW
|
| 704 |
) -> ChatResponse:
|
| 705 |
"""
|
| 706 |
Create ChatResponse payload with all required fields.
|
|
@@ -709,30 +755,12 @@ def _assistant_response_payload(
|
|
| 709 |
from services.schemas import generate_message_id
|
| 710 |
message_id = generate_message_id()
|
| 711 |
|
| 712 |
-
#
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
"
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
}
|
| 719 |
-
|
| 720 |
-
# NEW: Add pipeline results if available
|
| 721 |
-
if pipeline_result:
|
| 722 |
-
# Extract text from pipeline results
|
| 723 |
-
extracted_text = pipeline_result.get("execution_results", {}).get("text", "")
|
| 724 |
-
if extracted_text:
|
| 725 |
-
base_message["result"] = {
|
| 726 |
-
"text": extracted_text,
|
| 727 |
-
"pipeline_id": pipeline_result.get("pipeline_id"),
|
| 728 |
-
"status": pipeline_result.get("status", "completed")
|
| 729 |
-
}
|
| 730 |
-
|
| 731 |
-
# Persist assistant message to S3 (with results if available)
|
| 732 |
-
_save_conversation_to_s3(
|
| 733 |
-
chat_id,
|
| 734 |
-
_load_conversation_from_s3(chat_id) + [base_message],
|
| 735 |
-
update_activity=True
|
| 736 |
)
|
| 737 |
|
| 738 |
# Get file metadata from session
|
|
@@ -740,8 +768,7 @@ def _assistant_response_payload(
|
|
| 740 |
chat_name = session.get("chat_name")
|
| 741 |
file_metadata = session.get("file_metadata", {})
|
| 742 |
|
| 743 |
-
|
| 744 |
-
response = ChatResponse(
|
| 745 |
message_id=message_id,
|
| 746 |
assistant_response=friendly_response,
|
| 747 |
output=output or {},
|
|
@@ -757,8 +784,6 @@ def _assistant_response_payload(
|
|
| 757 |
fileName=file_metadata.get("file_name"),
|
| 758 |
fileUrl=file_metadata.get("file_url")
|
| 759 |
)
|
| 760 |
-
|
| 761 |
-
return response
|
| 762 |
|
| 763 |
def parse_s3_uri(uri: str) -> Tuple[str, str]:
|
| 764 |
"""
|
|
|
|
| 639 |
metadata["pages_processed"] = pages_processed
|
| 640 |
return {"summary": summary, "metadata": metadata}
|
| 641 |
|
| 642 |
+
def _add_and_mirror_message(
|
| 643 |
+
chat_id: str,
|
| 644 |
+
role: str,
|
| 645 |
+
content: str,
|
| 646 |
+
file_metadata: Optional[Dict[str, Any]] = None,
|
| 647 |
+
pipeline_result: Optional[Dict[str, Any]] = None # NEW: Add pipeline_result parameter
|
| 648 |
+
):
|
| 649 |
"""
|
| 650 |
V3 RULE: Append message to S3 conversation.
|
| 651 |
+
Now includes pipeline results in assistant messages.
|
|
|
|
| 652 |
"""
|
| 653 |
# 1. Load existing
|
| 654 |
current_messages = _load_conversation_from_s3(chat_id)
|
|
|
|
| 660 |
"timestamp": datetime.utcnow().isoformat() + "Z"
|
| 661 |
}
|
| 662 |
|
| 663 |
+
# NEW: Add pipeline results if available (for assistant messages)
|
| 664 |
+
if role == "assistant" and pipeline_result:
|
| 665 |
+
# Check if this is a pipeline completion message
|
| 666 |
+
if "pipeline completed" in content.lower() or "pipeline failed" in content.lower():
|
| 667 |
+
# Extract the text/result from pipeline
|
| 668 |
+
execution_results = pipeline_result.get("execution_results", {})
|
| 669 |
+
|
| 670 |
+
# Try to extract the main result text
|
| 671 |
+
result_text = ""
|
| 672 |
+
|
| 673 |
+
# Check for image descriptions
|
| 674 |
+
if execution_results.get("image_descriptions"):
|
| 675 |
+
image_desc = execution_results["image_descriptions"]
|
| 676 |
+
if image_desc.get("result") and isinstance(image_desc["result"], list) and len(image_desc["result"]) > 0:
|
| 677 |
+
page_result = image_desc["result"][0]
|
| 678 |
+
# Use Gemini description if available
|
| 679 |
+
if page_result.get("gemini", {}).get("description"):
|
| 680 |
+
result_text = page_result["gemini"]["description"]
|
| 681 |
+
elif page_result.get("mistral", {}).get("description"):
|
| 682 |
+
result_text = page_result["mistral"]["description"]
|
| 683 |
+
|
| 684 |
+
# Check for extracted text (for text extraction pipelines)
|
| 685 |
+
elif execution_results.get("text"):
|
| 686 |
+
result_text = execution_results["text"]
|
| 687 |
+
|
| 688 |
+
# Check for component results
|
| 689 |
+
elif execution_results.get("components_executed"):
|
| 690 |
+
for comp in execution_results["components_executed"]:
|
| 691 |
+
comp_result = comp.get("result", {})
|
| 692 |
+
if comp_result.get("text"):
|
| 693 |
+
result_text = comp_result["text"]
|
| 694 |
+
break
|
| 695 |
+
elif comp_result.get("image_descriptions"):
|
| 696 |
+
image_desc = comp_result["image_descriptions"]
|
| 697 |
+
if image_desc.get("result") and isinstance(image_desc["result"], list) and len(image_desc["result"]) > 0:
|
| 698 |
+
page_result = image_desc["result"][0]
|
| 699 |
+
if page_result.get("gemini", {}).get("description"):
|
| 700 |
+
result_text = page_result["gemini"]["description"]
|
| 701 |
+
break
|
| 702 |
+
elif page_result.get("mistral", {}).get("description"):
|
| 703 |
+
result_text = page_result["mistral"]["description"]
|
| 704 |
+
break
|
| 705 |
+
|
| 706 |
+
# Add result to message if we found something
|
| 707 |
+
if result_text:
|
| 708 |
+
new_msg["result"] = {
|
| 709 |
+
"text": result_text,
|
| 710 |
+
"pipeline_id": pipeline_result.get("pipeline_id"),
|
| 711 |
+
"status": pipeline_result.get("status", "completed")
|
| 712 |
+
}
|
| 713 |
+
elif pipeline_result.get("error"):
|
| 714 |
+
# For error cases
|
| 715 |
+
new_msg["result"] = {
|
| 716 |
+
"error": pipeline_result.get("error"),
|
| 717 |
+
"pipeline_id": pipeline_result.get("pipeline_id"),
|
| 718 |
+
"status": "failed"
|
| 719 |
+
}
|
| 720 |
|
| 721 |
# Add file metadata if provided
|
| 722 |
if file_metadata:
|
|
|
|
| 746 |
output: Optional[Dict[str, Any]] = None,
|
| 747 |
final_output: Optional[Dict[str, Any]] = None,
|
| 748 |
exception: Optional[str] = None,
|
| 749 |
+
pipeline_result: Optional[Dict[str, Any]] = None # NEW parameter
|
| 750 |
) -> ChatResponse:
|
| 751 |
"""
|
| 752 |
Create ChatResponse payload with all required fields.
|
|
|
|
| 755 |
from services.schemas import generate_message_id
|
| 756 |
message_id = generate_message_id()
|
| 757 |
|
| 758 |
+
# Persist assistant message to S3 WITH pipeline results
|
| 759 |
+
_add_and_mirror_message(
|
| 760 |
+
chat_id=chat_id,
|
| 761 |
+
role="assistant",
|
| 762 |
+
content=friendly_response,
|
| 763 |
+
pipeline_result=pipeline_result # NEW: Pass pipeline results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 764 |
)
|
| 765 |
|
| 766 |
# Get file metadata from session
|
|
|
|
| 768 |
chat_name = session.get("chat_name")
|
| 769 |
file_metadata = session.get("file_metadata", {})
|
| 770 |
|
| 771 |
+
return ChatResponse(
|
|
|
|
| 772 |
message_id=message_id,
|
| 773 |
assistant_response=friendly_response,
|
| 774 |
output=output or {},
|
|
|
|
| 784 |
fileName=file_metadata.get("file_name"),
|
| 785 |
fileUrl=file_metadata.get("file_url")
|
| 786 |
)
|
|
|
|
|
|
|
| 787 |
|
| 788 |
def parse_s3_uri(uri: str) -> Tuple[str, str]:
|
| 789 |
"""
|