rairo commited on
Commit
19f67b5
·
verified ·
1 Parent(s): 11e0e23

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +18 -33
main.py CHANGED
@@ -976,15 +976,11 @@ def initiate_call(project_id):
976
  logger.error("[INITIATE] ELEVENLABS_API_KEY is not set on the server.")
977
  return jsonify({'error': 'Server configuration error.'}), 500
978
 
979
- headers = {
980
- "xi-api-key": ELEVENLABS_API_KEY
981
- }
982
 
983
  try:
984
- # ✅ FIX: This is the only correct endpoint for this operation.
985
- # It uses GET, not POST, and includes the agent_id as a query parameter.
986
- url = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={AGENT_ID}"
987
-
988
  response = requests.get(url, headers=headers, timeout=15)
989
  response.raise_for_status() # This will raise an error for 4xx/5xx responses
990
 
@@ -1009,42 +1005,28 @@ def test_agent():
1009
  Fixed debug endpoint that tests the CORRECT conversation endpoint.
1010
  """
1011
  if not ELEVENLABS_API_KEY:
1012
- return jsonify({'error': 'API key not set'}), 500
1013
 
1014
- headers = {
1015
- "xi-api-key": ELEVENLABS_API_KEY
1016
- }
1017
 
1018
  try:
1019
- results = {'agent_id': AGENT_ID, 'tests': {}}
1020
-
1021
- # Test 1: Check agent exists (this works per your debug)
1022
  agent_url = f"https://api.elevenlabs.io/v1/convai/agents/{AGENT_ID}"
1023
- agent_response = requests.get(agent_url, headers=headers, timeout=10)
1024
  results['tests']['agent_check'] = {
1025
- 'status': agent_response.status_code,
1026
- 'exists': agent_response.status_code == 200
1027
  }
1028
 
1029
- # FIX: Test 2 now checks the CORRECT endpoint that we use in initiate_call.
1030
  conv_url = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={AGENT_ID}"
1031
- conv_response = requests.get(conv_url, headers=headers, timeout=10)
1032
  results['tests']['get_signed_url_check'] = {
1033
- 'status': conv_response.status_code,
1034
- 'response_contains_url': 'signed_url' in conv_response.json() if conv_response.status_code == 200 else False
1035
  }
1036
 
1037
- # Test 3: List available agents to confirm access (this is fine)
1038
- list_url = "https://api.elevenlabs.io/v1/convai/agents"
1039
- list_response = requests.get(list_url, headers=headers, timeout=10)
1040
- if list_response.status_code == 200:
1041
- agents = list_response.json().get('agents', [])
1042
- agent_ids = [a.get('agent_id') for a in agents]
1043
- results['tests']['agent_list'] = {
1044
- 'total_agents': len(agents),
1045
- 'your_agent_found': AGENT_ID in agent_ids
1046
- }
1047
-
1048
  return jsonify(results)
1049
 
1050
  except Exception as e:
@@ -1053,7 +1035,8 @@ def test_agent():
1053
  @app.route('/api/projects/<project_id>/log-call-usage', methods=['POST'])
1054
  def log_call_usage(project_id):
1055
  """
1056
- No changes needed here - your original was fine
 
1057
  """
1058
  logger.info(f"[LOGGING] Received usage log for project: {project_id}")
1059
 
@@ -1067,6 +1050,7 @@ def log_call_usage(project_id):
1067
  if duration_seconds is None or not isinstance(duration_seconds, (int, float)):
1068
  return jsonify({'error': 'Invalid duration provided.'}), 400
1069
 
 
1070
  minutes = math.ceil(duration_seconds / 60)
1071
  cost = minutes * 3
1072
 
@@ -1081,6 +1065,7 @@ def log_call_usage(project_id):
1081
  return jsonify({'error': 'User not found.'}), 404
1082
 
1083
  current_credits = user_data.get('credits', 0)
 
1084
  new_credits = max(0, current_credits - cost)
1085
 
1086
  user_ref.update({'credits': new_credits})
 
976
  logger.error("[INITIATE] ELEVENLABS_API_KEY is not set on the server.")
977
  return jsonify({'error': 'Server configuration error.'}), 500
978
 
979
+ # This is the correct URL as per the official ElevenLabs documentation and our debug results.
980
+ url = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={AGENT_ID}"
981
+ headers = {"xi-api-key": ELEVENLABS_API_KEY}
982
 
983
  try:
 
 
 
 
984
  response = requests.get(url, headers=headers, timeout=15)
985
  response.raise_for_status() # This will raise an error for 4xx/5xx responses
986
 
 
1005
  Fixed debug endpoint that tests the CORRECT conversation endpoint.
1006
  """
1007
  if not ELEVENLABS_API_KEY:
1008
+ return jsonify({'error': 'API key not set on server'}), 500
1009
 
1010
+ headers = {"xi-api-key": ELEVENLABS_API_KEY}
1011
+ results = {'agent_id': AGENT_ID, 'tests': {}}
 
1012
 
1013
  try:
1014
+ # Test 1: Check if the agent can be found by its ID.
 
 
1015
  agent_url = f"https://api.elevenlabs.io/v1/convai/agents/{AGENT_ID}"
1016
+ agent_resp = requests.get(agent_url, headers=headers, timeout=10)
1017
  results['tests']['agent_check'] = {
1018
+ 'status': agent_resp.status_code,
1019
+ 'exists': agent_resp.ok
1020
  }
1021
 
1022
+ # Test 2: Check if we can get a signed URL for this agent. This is the most important test.
1023
  conv_url = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={AGENT_ID}"
1024
+ conv_resp = requests.get(conv_url, headers=headers, timeout=10)
1025
  results['tests']['get_signed_url_check'] = {
1026
+ 'status': conv_resp.status_code,
1027
+ 'url_received': 'signed_url' in conv_resp.json() if conv_resp.ok else False
1028
  }
1029
 
 
 
 
 
 
 
 
 
 
 
 
1030
  return jsonify(results)
1031
 
1032
  except Exception as e:
 
1035
  @app.route('/api/projects/<project_id>/log-call-usage', methods=['POST'])
1036
  def log_call_usage(project_id):
1037
  """
1038
+ Calculates and deducts credits from a user's account in Firebase
1039
+ after a call is completed.
1040
  """
1041
  logger.info(f"[LOGGING] Received usage log for project: {project_id}")
1042
 
 
1050
  if duration_seconds is None or not isinstance(duration_seconds, (int, float)):
1051
  return jsonify({'error': 'Invalid duration provided.'}), 400
1052
 
1053
+ # Calculate credit cost (3 credits per minute, always rounded up)
1054
  minutes = math.ceil(duration_seconds / 60)
1055
  cost = minutes * 3
1056
 
 
1065
  return jsonify({'error': 'User not found.'}), 404
1066
 
1067
  current_credits = user_data.get('credits', 0)
1068
+
1069
  new_credits = max(0, current_credits - cost)
1070
 
1071
  user_ref.update({'credits': new_credits})