Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1062,8 +1062,8 @@ def test_agent():
|
|
| 1062 |
@app.route('/api/projects/<project_id>/log-call-usage', methods=['POST'])
|
| 1063 |
def log_call_usage(project_id):
|
| 1064 |
"""
|
| 1065 |
-
|
| 1066 |
-
|
| 1067 |
"""
|
| 1068 |
logger.info(f"[LOGGING] Received usage log for project: {project_id}")
|
| 1069 |
|
|
@@ -1073,32 +1073,45 @@ def log_call_usage(project_id):
|
|
| 1073 |
|
| 1074 |
data = request.get_json()
|
| 1075 |
duration_seconds = data.get("durationSeconds")
|
|
|
|
| 1076 |
|
| 1077 |
-
if duration_seconds is None
|
| 1078 |
return jsonify({'error': 'Invalid duration provided.'}), 400
|
| 1079 |
|
| 1080 |
-
#
|
| 1081 |
minutes = math.ceil(duration_seconds / 60)
|
| 1082 |
cost = minutes * 3
|
| 1083 |
-
|
| 1084 |
-
logger.info(f"[LOGGING] User '{uid}' call duration: {duration_seconds:.2f}s, rounded to {minutes} minute(s). Cost: {cost} credits.")
|
| 1085 |
|
| 1086 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1087 |
user_ref = db_ref.child(f'users/{uid}')
|
| 1088 |
user_data = user_ref.get()
|
| 1089 |
|
| 1090 |
if user_data is None:
|
| 1091 |
-
logger.error(f"[LOGGING] User with UID '{uid}' not found in the database.")
|
| 1092 |
return jsonify({'error': 'User not found.'}), 404
|
| 1093 |
|
| 1094 |
current_credits = user_data.get('credits', 0)
|
| 1095 |
-
|
| 1096 |
new_credits = max(0, current_credits - cost)
|
| 1097 |
-
|
| 1098 |
user_ref.update({'credits': new_credits})
|
| 1099 |
|
| 1100 |
-
logger.info(f"[LOGGING]
|
| 1101 |
-
|
| 1102 |
return jsonify({
|
| 1103 |
"status": "success",
|
| 1104 |
"creditsDeducted": cost,
|
|
@@ -1110,6 +1123,7 @@ def log_call_usage(project_id):
|
|
| 1110 |
return jsonify({'error': 'A server error occurred while updating credits.'}), 500
|
| 1111 |
|
| 1112 |
|
|
|
|
| 1113 |
# -----------------------------------------------------------------------------
|
| 1114 |
# 7. MAIN EXECUTION
|
| 1115 |
# -----------------------------------------------------------------------------
|
|
|
|
| 1062 |
@app.route('/api/projects/<project_id>/log-call-usage', methods=['POST'])
|
| 1063 |
def log_call_usage(project_id):
|
| 1064 |
"""
|
| 1065 |
+
✅ MODIFIED: Now accepts and stores the full conversation transcript
|
| 1066 |
+
in addition to calculating credit cost.
|
| 1067 |
"""
|
| 1068 |
logger.info(f"[LOGGING] Received usage log for project: {project_id}")
|
| 1069 |
|
|
|
|
| 1073 |
|
| 1074 |
data = request.get_json()
|
| 1075 |
duration_seconds = data.get("durationSeconds")
|
| 1076 |
+
transcript = data.get("transcript") # Get the new transcript field
|
| 1077 |
|
| 1078 |
+
if duration_seconds is None:
|
| 1079 |
return jsonify({'error': 'Invalid duration provided.'}), 400
|
| 1080 |
|
| 1081 |
+
# --- Credit Calculation ---
|
| 1082 |
minutes = math.ceil(duration_seconds / 60)
|
| 1083 |
cost = minutes * 3
|
| 1084 |
+
logger.info(f"[LOGGING] User '{uid}' call duration: {duration_seconds:.2f}s, Cost: {cost} credits.")
|
|
|
|
| 1085 |
|
| 1086 |
try:
|
| 1087 |
+
# --- Transcript Storage ---
|
| 1088 |
+
if transcript and isinstance(transcript, str) and len(transcript) > 10:
|
| 1089 |
+
transcript_id = f"{project_id}_{int(time.time())}"
|
| 1090 |
+
transcript_ref = db_ref.child(f'transcripts/{uid}/{transcript_id}')
|
| 1091 |
+
transcript_data = {
|
| 1092 |
+
"transcript": transcript,
|
| 1093 |
+
"projectId": project_id,
|
| 1094 |
+
"userId": uid,
|
| 1095 |
+
"durationSeconds": duration_seconds,
|
| 1096 |
+
"createdAt": datetime.utcnow().isoformat()
|
| 1097 |
+
}
|
| 1098 |
+
transcript_ref.set(transcript_data)
|
| 1099 |
+
logger.info(f"[LOGGING] Successfully stored transcript {transcript_id} for user '{uid}'.")
|
| 1100 |
+
else:
|
| 1101 |
+
logger.warning(f"[LOGGING] No valid transcript provided for user '{uid}' on project {project_id}.")
|
| 1102 |
+
|
| 1103 |
+
# --- Credit Deduction ---
|
| 1104 |
user_ref = db_ref.child(f'users/{uid}')
|
| 1105 |
user_data = user_ref.get()
|
| 1106 |
|
| 1107 |
if user_data is None:
|
|
|
|
| 1108 |
return jsonify({'error': 'User not found.'}), 404
|
| 1109 |
|
| 1110 |
current_credits = user_data.get('credits', 0)
|
|
|
|
| 1111 |
new_credits = max(0, current_credits - cost)
|
|
|
|
| 1112 |
user_ref.update({'credits': new_credits})
|
| 1113 |
|
| 1114 |
+
logger.info(f"[LOGGING] Updated credits for user '{uid}'. New balance: {new_credits}")
|
|
|
|
| 1115 |
return jsonify({
|
| 1116 |
"status": "success",
|
| 1117 |
"creditsDeducted": cost,
|
|
|
|
| 1123 |
return jsonify({'error': 'A server error occurred while updating credits.'}), 500
|
| 1124 |
|
| 1125 |
|
| 1126 |
+
|
| 1127 |
# -----------------------------------------------------------------------------
|
| 1128 |
# 7. MAIN EXECUTION
|
| 1129 |
# -----------------------------------------------------------------------------
|