- Min of meer werkende chat client new stule
This commit is contained in:
@@ -139,31 +139,57 @@ def chat(magic_link_code):
|
||||
current_app.logger.error(f"Error in chat view: {str(e)}", exc_info=True)
|
||||
return render_template('error.html', message="An error occurred while setting up the chat.")
|
||||
|
||||
|
||||
@chat_bp.route('/api/send_message', methods=['POST'])
|
||||
def send_message():
|
||||
"""
|
||||
API endpoint to send a message to the specialist
|
||||
"""
|
||||
current_app.logger.debug(f"Sending message to specialist: {session.get('specialist', {}).get('id', 'UNKNOWN')}\n"
|
||||
f"with data: {request.json} \n"
|
||||
f"BEFORE TRY")
|
||||
try:
|
||||
current_app.logger.debug(
|
||||
f"Sending message to specialist: {session.get('specialist', {}).get('id', 'UNKNOWN')}\n"
|
||||
f"with data: {request.json} \n"
|
||||
f"AFTER TRY")
|
||||
|
||||
# Voeg meer debug logging toe om elk stap te traceren
|
||||
current_app.logger.debug("Step 1: Getting request data")
|
||||
data = request.json
|
||||
message = data.get('message', '')
|
||||
form_values = data.get('form_values', {})
|
||||
|
||||
current_app.logger.debug(f"Step 2: Parsed message='{message}', form_values={form_values}")
|
||||
|
||||
# Controleer of er ofwel een bericht of formuliergegevens zijn
|
||||
if not message and not form_values:
|
||||
current_app.logger.debug("Step 3: No message or form data - returning error")
|
||||
return jsonify({'error': 'No message or form data provided'}), 400
|
||||
|
||||
tenant_id = session['tenant']['id']
|
||||
specialist_id = session['specialist']['id']
|
||||
current_app.logger.debug("Step 4: Getting session data")
|
||||
# Veiliger session toegang met fallbacks
|
||||
tenant_data = session.get('tenant', {})
|
||||
specialist_data = session.get('specialist', {})
|
||||
|
||||
tenant_id = tenant_data.get('id')
|
||||
specialist_id = specialist_data.get('id')
|
||||
chat_session_id = session.get('chat_session_id')
|
||||
specialist_args = session['magic_link'].get('specialist_args', {})
|
||||
specialist_args = session.get('magic_link', {}).get('specialist_args', {})
|
||||
|
||||
current_app.logger.debug(
|
||||
f"Step 5: Session data - tenant_id={tenant_id}, specialist_id={specialist_id}, chat_session_id={chat_session_id}")
|
||||
|
||||
if not all([tenant_id, specialist_id, chat_session_id]):
|
||||
current_app.logger.error(
|
||||
f"Missing session data: tenant_id={tenant_id}, specialist_id={specialist_id}, chat_session_id={chat_session_id}")
|
||||
return jsonify({'error': 'Session expired or invalid'}), 400
|
||||
|
||||
current_app.logger.debug("Step 6: Switching to tenant schema")
|
||||
# Switch to tenant schema
|
||||
Database(tenant_id).switch_schema()
|
||||
|
||||
current_app.logger.debug("Step 7: Preparing specialist arguments")
|
||||
# Add user message to specialist arguments
|
||||
if message:
|
||||
specialist_args['question'] = message
|
||||
@@ -177,11 +203,13 @@ def send_message():
|
||||
if user_language:
|
||||
specialist_args['language'] = user_language
|
||||
|
||||
current_app.logger.debug(f"Step 8: About to execute specialist with args: {specialist_args}")
|
||||
current_app.logger.debug(f"Sending message to specialist: {specialist_id} for tenant {tenant_id}\n"
|
||||
f" with args: {specialist_args}\n"
|
||||
f"with session ID: {chat_session_id}")
|
||||
|
||||
# Execute specialist
|
||||
current_app.logger.debug("Step 9: Calling SpecialistServices.execute_specialist")
|
||||
result = SpecialistServices.execute_specialist(
|
||||
tenant_id=tenant_id,
|
||||
specialist_id=specialist_id,
|
||||
@@ -190,17 +218,22 @@ def send_message():
|
||||
user_timezone=data.get('timezone', 'UTC')
|
||||
)
|
||||
|
||||
current_app.logger.debug(f"Specialist execution result: {result}")
|
||||
current_app.logger.debug(f"Step 10: Specialist execution result: {result}")
|
||||
|
||||
# Store the task ID for polling
|
||||
current_app.logger.debug("Step 11: Storing task ID in session")
|
||||
session['current_task_id'] = result['task_id']
|
||||
|
||||
return jsonify({
|
||||
current_app.logger.debug("Step 12: Preparing response")
|
||||
response_data = {
|
||||
'status': 'processing',
|
||||
'task_id': result['task_id'],
|
||||
'content': 'Verwerking gestart...',
|
||||
'type': 'text'
|
||||
})
|
||||
}
|
||||
|
||||
current_app.logger.debug(f"Step 13: Returning response: {response_data}")
|
||||
return jsonify(response_data)
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error sending message: {str(e)}", exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user