"""SQL Whisperer routes.""" from flask import Blueprint, render_template, request, jsonify from .whisperer import whisper bp = Blueprint("sql_whisperer", __name__, template_folder="templates") _DIALECTS = {"PostgreSQL", "MySQL", "SQLite", "SQL Server", "BigQuery"} @bp.route("/") def index(): return render_template("sql_whisperer/index.html") @bp.route("/api/whisper", methods=["POST"]) def api_whisper(): body = request.get_json(silent=True) or {} question = (body.get("question") or "").strip() schema = (body.get("schema") or "").strip() dialect = body.get("dialect", "PostgreSQL") if not question: return jsonify({"error": "question is required"}), 400 if dialect not in _DIALECTS: dialect = "PostgreSQL" try: result = whisper(question, schema, dialect) except Exception as e: return jsonify({"error": "AI failed to convert query — please try again"}), 502 if not result or not isinstance(result, dict): return jsonify({"error": "AI failed to convert query — please try again"}), 502 return jsonify(result)