- Build of the Chat Client using Vue.js
- Accompanying css - Views to serve the Chat Client - first test version of the TRACIE_SELECTION_SPECIALIST - ESS Implemented.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import uuid
|
||||
from flask import Blueprint, render_template, request, session, current_app, jsonify, abort
|
||||
from flask import Blueprint, render_template, request, session, current_app, jsonify, Response, stream_with_context
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from common.extensions import db
|
||||
@@ -8,6 +8,7 @@ from common.models.interaction import SpecialistMagicLink, Specialist, ChatSessi
|
||||
from common.services.interaction.specialist_services import SpecialistServices
|
||||
from common.utils.database import Database
|
||||
from common.utils.chat_utils import get_default_chat_customisation
|
||||
from common.utils.execution_progress import ExecutionProgressTracker
|
||||
|
||||
chat_bp = Blueprint('chat_bp', __name__, url_prefix='/chat')
|
||||
|
||||
@@ -82,6 +83,7 @@ def chat(magic_link_code):
|
||||
session['specialist'] = specialist.to_dict()
|
||||
session['magic_link'] = specialist_ml.to_dict()
|
||||
session['tenant_make'] = tenant_make.to_dict()
|
||||
session['chat_session_id'] = SpecialistServices.start_session()
|
||||
|
||||
# Get customisation options with defaults
|
||||
customisation = get_default_chat_customisation(tenant_make.chat_customisation_options)
|
||||
@@ -89,11 +91,20 @@ def chat(magic_link_code):
|
||||
# Start a new chat session
|
||||
session['chat_session_id'] = SpecialistServices.start_session()
|
||||
|
||||
# Define settings for the client
|
||||
settings = {
|
||||
"max_message_length": 2000,
|
||||
"auto_scroll": True
|
||||
}
|
||||
|
||||
return render_template('chat.html',
|
||||
tenant=tenant,
|
||||
tenant_make=tenant_make,
|
||||
specialist=specialist,
|
||||
customisation=customisation)
|
||||
customisation=customisation,
|
||||
messages=[customisation['welcome_message']],
|
||||
settings=settings
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error in chat view: {str(e)}", exc_info=True)
|
||||
@@ -111,10 +122,10 @@ def send_message():
|
||||
if not message:
|
||||
return jsonify({'error': 'No message provided'}), 400
|
||||
|
||||
tenant_id = session.get('tenant_id')
|
||||
specialist_id = session.get('specialist_id')
|
||||
tenant_id = session['tenant']['id']
|
||||
specialist_id = session['specialist']['id']
|
||||
chat_session_id = session.get('chat_session_id')
|
||||
specialist_args = session.get('specialist_args', {})
|
||||
specialist_args = session['magic_link'].get('specialist_args', {})
|
||||
|
||||
if not all([tenant_id, specialist_id, chat_session_id]):
|
||||
return jsonify({'error': 'Session expired or invalid'}), 400
|
||||
@@ -123,7 +134,11 @@ def send_message():
|
||||
Database(tenant_id).switch_schema()
|
||||
|
||||
# Add user message to specialist arguments
|
||||
specialist_args['user_message'] = message
|
||||
specialist_args['question'] = message
|
||||
|
||||
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
|
||||
result = SpecialistServices.execute_specialist(
|
||||
@@ -134,12 +149,16 @@ def send_message():
|
||||
user_timezone=data.get('timezone', 'UTC')
|
||||
)
|
||||
|
||||
current_app.logger.debug(f"Specialist execution result: {result}")
|
||||
|
||||
# Store the task ID for polling
|
||||
session['current_task_id'] = result['task_id']
|
||||
|
||||
return jsonify({
|
||||
'status': 'processing',
|
||||
'task_id': result['task_id']
|
||||
'task_id': result['task_id'],
|
||||
'content': 'Verwerking gestart...',
|
||||
'type': 'text'
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
@@ -192,3 +211,34 @@ def check_status():
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error checking status: {str(e)}", exc_info=True)
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@chat_bp.route('/api/task_progress/<task_id>')
|
||||
def task_progress_stream(task_id):
|
||||
"""
|
||||
Server-Sent Events endpoint voor realtime voortgangsupdates
|
||||
"""
|
||||
current_app.logger.debug(f"Streaming updates for task ID: {task_id}")
|
||||
try:
|
||||
tracker = ExecutionProgressTracker()
|
||||
|
||||
def generate():
|
||||
try:
|
||||
for update in tracker.get_updates(task_id):
|
||||
current_app.logger.debug(f"Progress update: {update}")
|
||||
yield update
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Progress stream error: {str(e)}")
|
||||
yield f"data: {{'error': '{str(e)}'}}\n\n"
|
||||
|
||||
return Response(
|
||||
stream_with_context(generate()),
|
||||
mimetype='text/event-stream',
|
||||
headers={
|
||||
'Cache-Control': 'no-cache',
|
||||
'X-Accel-Buffering': 'no'
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Failed to start progress stream: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
Reference in New Issue
Block a user