rairo commited on
Commit
11e0e23
·
verified ·
1 Parent(s): 84378a4

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +31 -37
main.py CHANGED
@@ -963,7 +963,8 @@ ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
963
  @app.route('/api/projects/<project_id>/initiate-call', methods=['POST'])
964
  def initiate_call(project_id):
965
  """
966
- Fixed version - handles the API endpoint issues shown in your debug
 
967
  """
968
  logger.info(f"[INITIATE] Received request for project: {project_id}")
969
 
@@ -975,49 +976,43 @@ def initiate_call(project_id):
975
  logger.error("[INITIATE] ELEVENLABS_API_KEY is not set on the server.")
976
  return jsonify({'error': 'Server configuration error.'}), 500
977
 
978
- # ✅ FIX: Use the correct API endpoint structure
979
- # The debug shows your agent exists, but the signed URL endpoint is different
980
  headers = {
981
- "xi-api-key": ELEVENLABS_API_KEY,
982
- "Content-Type": "application/json"
983
  }
984
 
985
  try:
986
- # Method 1: Try the newer endpoint format
987
- url = "https://api.elevenlabs.io/v1/convai/conversation"
988
- payload = {"agent_id": AGENT_ID}
989
 
990
- response = requests.post(url, headers=headers, json=payload, timeout=15)
991
-
992
- if response.status_code == 200:
993
- data = response.json()
994
- conversation_id = data.get("conversation_id")
995
- if conversation_id:
996
- # Return the WebSocket URL format
997
- ws_url = f"wss://api.elevenlabs.io/v1/convai/conversation/{conversation_id}"
998
- logger.info("[INITIATE] Successfully created conversation session.")
999
- return jsonify({"signed_url": ws_url}), 200
1000
-
1001
- # Method 2: Try direct agent connection (fallback)
1002
- logger.info("[INITIATE] Conversation creation failed, trying direct agent approach")
1003
-
1004
- # For public agents, return agent ID directly - SDK will handle it
1005
- return jsonify({"agent_id": AGENT_ID}), 200
1006
 
1007
  except requests.exceptions.RequestException as e:
1008
  logger.error(f"[INITIATE] Error calling ElevenLabs API: {e}")
1009
- # Return agent ID as fallback
1010
- return jsonify({"agent_id": AGENT_ID}), 200
1011
 
1012
  @app.route('/api/debug/test-agent', methods=['GET'])
1013
  def test_agent():
1014
- """Fixed debug endpoint"""
 
 
1015
  if not ELEVENLABS_API_KEY:
1016
  return jsonify({'error': 'API key not set'}), 500
1017
 
1018
  headers = {
1019
- "xi-api-key": ELEVENLABS_API_KEY,
1020
- "Content-Type": "application/json"
1021
  }
1022
 
1023
  try:
@@ -1031,15 +1026,15 @@ def test_agent():
1031
  'exists': agent_response.status_code == 200
1032
  }
1033
 
1034
- # Test 2: Try conversation creation
1035
- conv_url = "https://api.elevenlabs.io/v1/convai/conversation"
1036
- conv_response = requests.post(conv_url, headers=headers, json={"agent_id": AGENT_ID}, timeout=10)
1037
- results['tests']['conversation_create'] = {
1038
  'status': conv_response.status_code,
1039
- 'response': conv_response.json() if conv_response.status_code == 200 else conv_response.text[:200]
1040
  }
1041
 
1042
- # Test 3: List available agents to confirm access
1043
  list_url = "https://api.elevenlabs.io/v1/convai/agents"
1044
  list_response = requests.get(list_url, headers=headers, timeout=10)
1045
  if list_response.status_code == 200:
@@ -1047,8 +1042,7 @@ def test_agent():
1047
  agent_ids = [a.get('agent_id') for a in agents]
1048
  results['tests']['agent_list'] = {
1049
  'total_agents': len(agents),
1050
- 'your_agent_found': AGENT_ID in agent_ids,
1051
- 'first_few_agents': agent_ids[:3]
1052
  }
1053
 
1054
  return jsonify(results)
 
963
  @app.route('/api/projects/<project_id>/initiate-call', methods=['POST'])
964
  def initiate_call(project_id):
965
  """
966
+ This is the definitive, correct version. It uses the official 'get-signed-url'
967
+ endpoint, which is the only one guaranteed to work for authenticated agents.
968
  """
969
  logger.info(f"[INITIATE] Received request for project: {project_id}")
970
 
 
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
+
991
+ data = response.json()
992
+ signed_url = data.get("signed_url")
993
+
994
+ if not signed_url:
995
+ logger.error("[INITIATE] ElevenLabs response missing 'signed_url'.")
996
+ return jsonify({'error': 'Failed to retrieve session URL from provider.'}), 502
997
+
998
+ logger.info("[INITIATE] Successfully retrieved signed URL.")
999
+ # The React SDK expects a JSON object with the key "signed_url".
1000
+ return jsonify({"signed_url": signed_url}), 200
 
 
 
1001
 
1002
  except requests.exceptions.RequestException as e:
1003
  logger.error(f"[INITIATE] Error calling ElevenLabs API: {e}")
1004
+ return jsonify({'error': 'Could not connect to AI service provider.'}), 504
 
1005
 
1006
  @app.route('/api/debug/test-agent', methods=['GET'])
1007
  def test_agent():
1008
+ """
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:
 
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:
 
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)