"""HTTP adapters for chat; business logic lives in services.""" from flask import Blueprint, Response, request, stream_with_context from .. import business_service, chat_service bp = Blueprint('chat', __name__) @bp.route('/stream_chat', methods=['GET', 'POST']) def stream_chat(): if request.method == 'POST': data = request.get_json(silent=True) or {} msg = (data.get('msg') or '').strip() conv_id = data.get('conv_id') try: conv_id = int(conv_id) if conv_id else None except (TypeError, ValueError): conv_id = None else: msg = (request.args.get('msg') or '').strip() conv_id = request.args.get('conv_id', type=int) frames, headers = chat_service.create_chat_stream(msg, conv_id) body = frames if isinstance(frames, str) else stream_with_context(frames) return Response(body, mimetype='text/event-stream', headers=headers) @bp.route('/api/feedback', methods=['POST']) def feedback(): return business_service.feedback(request.get_json(silent=True) or {})