yoursdvniel commited on
Commit
ea9369b
·
verified ·
1 Parent(s): 7d57c38

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +29 -12
main.py CHANGED
@@ -436,25 +436,42 @@ def get_agent_briefing(project_id):
436
  @app.route('/api/ai/get-agent-url', methods=['GET'])
437
  def get_agent_url():
438
  uid = verify_token(request.headers.get('Authorization'))
439
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
 
 
440
  user_data = db_ref.child(f'users/{uid}').get()
441
  if not user_data or user_data.get('credits', 0) < 3:
442
  return jsonify({'error': 'Insufficient credits to start a call. Minimum 3 required.'}), 402
 
443
  try:
444
- agent_id = os.environ.get("ELEVENLABS_AGENT_ID")
445
- if not agent_id: raise ValueError("ELEVENLABS_AGENT_ID is not configured on the server.")
446
- url, headers = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={agent_id}", {"xi-api-key": ELEVENLABS_API_KEY}
447
- response = requests.get(url, headers=headers)
448
- response.raise_for_status()
449
- logger.info(f"Successfully generated ElevenLabs signed URL for user {uid}.")
450
- return jsonify(response.json()), 200
451
- except requests.exceptions.RequestException as e:
452
- logger.error(f"ElevenLabs API error for user {uid}: {e}")
453
- return jsonify({'error': 'Failed to connect to the conversation service.'}), 502
 
 
 
 
 
 
 
 
 
 
 
 
 
454
  except Exception as e:
455
- logger.error(f"Error in get_agent_url for user {uid}: {e}")
456
  return jsonify({'error': 'An internal server error occurred.'}), 500
457
 
 
458
  @app.route('/api/projects/<string:project_id>/sessions/end', methods=['POST'])
459
  def end_session_and_analyze(project_id): # <-- CORRECTED: Added project_id parameter
460
  uid = verify_token(request.headers.get('Authorization'))
 
436
  @app.route('/api/ai/get-agent-url', methods=['GET'])
437
  def get_agent_url():
438
  uid = verify_token(request.headers.get('Authorization'))
439
+ if not uid:
440
+ return jsonify({'error': 'Unauthorized'}), 401
441
+
442
  user_data = db_ref.child(f'users/{uid}').get()
443
  if not user_data or user_data.get('credits', 0) < 3:
444
  return jsonify({'error': 'Insufficient credits to start a call. Minimum 3 required.'}), 402
445
+
446
  try:
447
+ url = "https://api.elevenlabs.io/v1/convai/conversation/get-signed-url"
448
+ headers = {"xi-api-key": ELEVENLABS_API_KEY}
449
+ params = {"agent_id": ELEVENLABS_AGENT_ID}
450
+
451
+ resp = requests.get(url, headers=headers, params=params, timeout=15)
452
+ if not resp.ok:
453
+ logger.error(
454
+ "ElevenLabs signed-URL failed: status=%s body=%s",
455
+ resp.status_code, resp.text[:500]
456
+ )
457
+ # Surface the real reason to the client (no mystery 502)
458
+ return jsonify({
459
+ 'error': 'ElevenLabs rejected the request',
460
+ 'status': resp.status_code,
461
+ 'detail': resp.text
462
+ }), resp.status_code
463
+
464
+ logger.info("Successfully generated ElevenLabs signed URL for user %s.", uid)
465
+ return jsonify(resp.json()), 200
466
+
467
+ except requests.RequestException as e:
468
+ logger.error("Network error talking to ElevenLabs: %s", e)
469
+ return jsonify({'error': 'Failed to connect to ElevenLabs', 'detail': str(e)}), 502
470
  except Exception as e:
471
+ logger.error("Error in get_agent_url for user %s: %s", uid, e)
472
  return jsonify({'error': 'An internal server error occurred.'}), 500
473
 
474
+
475
  @app.route('/api/projects/<string:project_id>/sessions/end', methods=['POST'])
476
  def end_session_and_analyze(project_id): # <-- CORRECTED: Added project_id parameter
477
  uid = verify_token(request.headers.get('Authorization'))