Spaces:
Sleeping
Sleeping
Commit
·
65abc8a
1
Parent(s):
5dc0941
Update api_routes_v2.py
Browse files- api_routes_v2.py +96 -28
api_routes_v2.py
CHANGED
|
@@ -639,7 +639,8 @@ 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(chat_id: str, role: str, content: str, file_metadata: Optional[Dict[str, Any]] = None
|
|
|
|
| 643 |
"""
|
| 644 |
V3 RULE: Append message to S3 conversation.
|
| 645 |
MongoDB metadata updated via _save_conversation_to_s3.
|
|
@@ -648,14 +649,30 @@ def _add_and_mirror_message(chat_id: str, role: str, content: str, file_metadata
|
|
| 648 |
# 1. Load existing
|
| 649 |
current_messages = _load_conversation_from_s3(chat_id)
|
| 650 |
|
| 651 |
-
# 2.
|
| 652 |
new_msg = {
|
| 653 |
"role": role,
|
| 654 |
"content": content if isinstance(content, str) else json.dumps(content, ensure_ascii=False),
|
| 655 |
"timestamp": datetime.utcnow().isoformat() + "Z"
|
| 656 |
}
|
| 657 |
|
| 658 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
if file_metadata:
|
| 660 |
new_msg["file_data"] = {
|
| 661 |
"has_file": True,
|
|
@@ -682,7 +699,8 @@ def _assistant_response_payload(
|
|
| 682 |
state: str,
|
| 683 |
output: Optional[Dict[str, Any]] = None,
|
| 684 |
final_output: Optional[Dict[str, Any]] = None,
|
| 685 |
-
exception: Optional[str] = None
|
|
|
|
| 686 |
) -> ChatResponse:
|
| 687 |
"""
|
| 688 |
Create ChatResponse payload with all required fields.
|
|
@@ -691,15 +709,39 @@ def _assistant_response_payload(
|
|
| 691 |
from services.schemas import generate_message_id
|
| 692 |
message_id = generate_message_id()
|
| 693 |
|
| 694 |
-
#
|
| 695 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 696 |
|
| 697 |
# Get file metadata from session
|
| 698 |
session = session_manager.get_session(chat_id) or {}
|
| 699 |
chat_name = session.get("chat_name")
|
| 700 |
file_metadata = session.get("file_metadata", {})
|
| 701 |
|
| 702 |
-
|
|
|
|
| 703 |
message_id=message_id,
|
| 704 |
assistant_response=friendly_response,
|
| 705 |
output=output or {},
|
|
@@ -715,6 +757,8 @@ def _assistant_response_payload(
|
|
| 715 |
fileName=file_metadata.get("file_name"),
|
| 716 |
fileUrl=file_metadata.get("file_url")
|
| 717 |
)
|
|
|
|
|
|
|
| 718 |
|
| 719 |
def parse_s3_uri(uri: str) -> Tuple[str, str]:
|
| 720 |
"""
|
|
@@ -1413,6 +1457,26 @@ async def chat_unified(
|
|
| 1413 |
print(f"Warning: Could not get output_id/download_url: {e}")
|
| 1414 |
if not final_output:
|
| 1415 |
final_output = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1416 |
else:
|
| 1417 |
# Pipeline failed or partially completed
|
| 1418 |
error_msg = result.get("error", "Pipeline execution incomplete")
|
|
@@ -1442,31 +1506,26 @@ async def chat_unified(
|
|
| 1442 |
final_output = {"text": f"Pipeline execution {pipeline_status} with {completed_steps}/{total_steps} steps completed"}
|
| 1443 |
api_type = "pipeline_failed" if pipeline_status == "failed" else "pipeline_partial"
|
| 1444 |
exception_msg = error_msg
|
| 1445 |
-
|
| 1446 |
-
|
| 1447 |
-
if is_success:
|
| 1448 |
-
api_data = {
|
| 1449 |
-
"type": api_type,
|
| 1450 |
-
"result": api_result,
|
| 1451 |
-
"pipeline": api_pipeline
|
| 1452 |
-
}
|
| 1453 |
-
else:
|
| 1454 |
api_data = {
|
| 1455 |
"type": api_type,
|
| 1456 |
"result": result,
|
| 1457 |
"pipeline": proposed
|
| 1458 |
}
|
| 1459 |
-
|
| 1460 |
-
|
| 1461 |
-
|
| 1462 |
-
|
| 1463 |
-
|
| 1464 |
-
|
| 1465 |
-
|
| 1466 |
-
|
| 1467 |
-
|
| 1468 |
-
|
| 1469 |
-
|
|
|
|
|
|
|
| 1470 |
except Exception as e:
|
| 1471 |
session_manager.update_session(chat_id, {"state": "initial"})
|
| 1472 |
pipeline_id = proposed.get("pipeline_id")
|
|
@@ -1501,13 +1560,22 @@ async def chat_unified(
|
|
| 1501 |
}
|
| 1502 |
if failed_component:
|
| 1503 |
api_data["failed_component"] = failed_component
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1504 |
return _assistant_response_payload(
|
| 1505 |
chat_id=chat_id,
|
| 1506 |
friendly_response=friendly,
|
| 1507 |
intent={"intent": "pipeline_execute"},
|
| 1508 |
api_data=api_data,
|
| 1509 |
state="initial",
|
| 1510 |
-
exception=str(e)
|
|
|
|
| 1511 |
)
|
| 1512 |
finally:
|
| 1513 |
try:
|
|
|
|
| 639 |
metadata["pages_processed"] = pages_processed
|
| 640 |
return {"summary": summary, "metadata": metadata}
|
| 641 |
|
| 642 |
+
def _add_and_mirror_message(chat_id: str, role: str, content: str, file_metadata: Optional[Dict[str, Any]] = None,
|
| 643 |
+
pipeline_result: Optional[Dict[str, Any]] = None):
|
| 644 |
"""
|
| 645 |
V3 RULE: Append message to S3 conversation.
|
| 646 |
MongoDB metadata updated via _save_conversation_to_s3.
|
|
|
|
| 649 |
# 1. Load existing
|
| 650 |
current_messages = _load_conversation_from_s3(chat_id)
|
| 651 |
|
| 652 |
+
# 2. Create message
|
| 653 |
new_msg = {
|
| 654 |
"role": role,
|
| 655 |
"content": content if isinstance(content, str) else json.dumps(content, ensure_ascii=False),
|
| 656 |
"timestamp": datetime.utcnow().isoformat() + "Z"
|
| 657 |
}
|
| 658 |
|
| 659 |
+
# Add result for pipeline completion messages
|
| 660 |
+
if role == "assistant" and pipeline_result and "pipeline completed" in content.lower():
|
| 661 |
+
extracted_text = pipeline_result.get("execution_results", {}).get("text", "")
|
| 662 |
+
if extracted_text:
|
| 663 |
+
new_msg["result"] = {
|
| 664 |
+
"text": extracted_text,
|
| 665 |
+
"pipeline_id": pipeline_result.get("pipeline_id"),
|
| 666 |
+
"status": pipeline_result.get("status", "completed")
|
| 667 |
+
}
|
| 668 |
+
elif pipeline_result.get("error"):
|
| 669 |
+
new_msg["result"] = {
|
| 670 |
+
"error": pipeline_result.get("error"),
|
| 671 |
+
"pipeline_id": pipeline_result.get("pipeline_id"),
|
| 672 |
+
"status": "failed"
|
| 673 |
+
}
|
| 674 |
+
|
| 675 |
+
# Add file metadata if provided
|
| 676 |
if file_metadata:
|
| 677 |
new_msg["file_data"] = {
|
| 678 |
"has_file": True,
|
|
|
|
| 699 |
state: str,
|
| 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: Add pipeline results
|
| 704 |
) -> ChatResponse:
|
| 705 |
"""
|
| 706 |
Create ChatResponse payload with all required fields.
|
|
|
|
| 709 |
from services.schemas import generate_message_id
|
| 710 |
message_id = generate_message_id()
|
| 711 |
|
| 712 |
+
# Create the base message
|
| 713 |
+
base_message = {
|
| 714 |
+
"role": "assistant",
|
| 715 |
+
"content": friendly_response,
|
| 716 |
+
"timestamp": datetime.utcnow().isoformat() + "Z",
|
| 717 |
+
"file_data": {"has_file": False}
|
| 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
|
| 739 |
session = session_manager.get_session(chat_id) or {}
|
| 740 |
chat_name = session.get("chat_name")
|
| 741 |
file_metadata = session.get("file_metadata", {})
|
| 742 |
|
| 743 |
+
# Create the ChatResponse
|
| 744 |
+
response = ChatResponse(
|
| 745 |
message_id=message_id,
|
| 746 |
assistant_response=friendly_response,
|
| 747 |
output=output or {},
|
|
|
|
| 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 |
"""
|
|
|
|
| 1457 |
print(f"Warning: Could not get output_id/download_url: {e}")
|
| 1458 |
if not final_output:
|
| 1459 |
final_output = None
|
| 1460 |
+
|
| 1461 |
+
# Build api_response data for success
|
| 1462 |
+
api_data = {
|
| 1463 |
+
"type": api_type,
|
| 1464 |
+
"result": api_result,
|
| 1465 |
+
"pipeline": api_pipeline
|
| 1466 |
+
}
|
| 1467 |
+
|
| 1468 |
+
# Return response with pipeline result included
|
| 1469 |
+
return _assistant_response_payload(
|
| 1470 |
+
chat_id=chat_id,
|
| 1471 |
+
friendly_response=friendly,
|
| 1472 |
+
intent={"intent": "pipeline_execute"},
|
| 1473 |
+
api_data=api_data,
|
| 1474 |
+
state="initial",
|
| 1475 |
+
output=output,
|
| 1476 |
+
final_output=final_output,
|
| 1477 |
+
exception=exception_msg,
|
| 1478 |
+
pipeline_result=result # NEW: Pass the pipeline results
|
| 1479 |
+
)
|
| 1480 |
else:
|
| 1481 |
# Pipeline failed or partially completed
|
| 1482 |
error_msg = result.get("error", "Pipeline execution incomplete")
|
|
|
|
| 1506 |
final_output = {"text": f"Pipeline execution {pipeline_status} with {completed_steps}/{total_steps} steps completed"}
|
| 1507 |
api_type = "pipeline_failed" if pipeline_status == "failed" else "pipeline_partial"
|
| 1508 |
exception_msg = error_msg
|
| 1509 |
+
|
| 1510 |
+
# Build api_response data for failure
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1511 |
api_data = {
|
| 1512 |
"type": api_type,
|
| 1513 |
"result": result,
|
| 1514 |
"pipeline": proposed
|
| 1515 |
}
|
| 1516 |
+
|
| 1517 |
+
# Return response with pipeline result included (even for failures)
|
| 1518 |
+
return _assistant_response_payload(
|
| 1519 |
+
chat_id=chat_id,
|
| 1520 |
+
friendly_response=friendly,
|
| 1521 |
+
intent={"intent": "pipeline_execute"},
|
| 1522 |
+
api_data=api_data,
|
| 1523 |
+
state="initial",
|
| 1524 |
+
output=output,
|
| 1525 |
+
final_output=final_output,
|
| 1526 |
+
exception=exception_msg,
|
| 1527 |
+
pipeline_result=result # NEW: Pass results even for failures
|
| 1528 |
+
)
|
| 1529 |
except Exception as e:
|
| 1530 |
session_manager.update_session(chat_id, {"state": "initial"})
|
| 1531 |
pipeline_id = proposed.get("pipeline_id")
|
|
|
|
| 1560 |
}
|
| 1561 |
if failed_component:
|
| 1562 |
api_data["failed_component"] = failed_component
|
| 1563 |
+
|
| 1564 |
+
# Create error result for the response
|
| 1565 |
+
error_result = {
|
| 1566 |
+
"error": str(e),
|
| 1567 |
+
"pipeline_id": pipeline_id,
|
| 1568 |
+
"status": "failed"
|
| 1569 |
+
}
|
| 1570 |
+
|
| 1571 |
return _assistant_response_payload(
|
| 1572 |
chat_id=chat_id,
|
| 1573 |
friendly_response=friendly,
|
| 1574 |
intent={"intent": "pipeline_execute"},
|
| 1575 |
api_data=api_data,
|
| 1576 |
state="initial",
|
| 1577 |
+
exception=str(e),
|
| 1578 |
+
pipeline_result=error_result # NEW: Include error in pipeline_result
|
| 1579 |
)
|
| 1580 |
finally:
|
| 1581 |
try:
|