""" Web interface for the code generation system. Simple Flask app with UI for prompt input and JSON output visualization. """ from flask import Flask, render_template, request, jsonify from flask_cors import CORS import json import os import sys from datetime import datetime # Add src to path sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src')) from pipeline import Pipeline from runtime_simulator import validate_config_executable app = Flask(__name__) CORS(app) # Initialize pipeline pipeline = Pipeline(use_llm=False) # Use rule-based for now, can enable LLM class GenerationRequest: """Track generation requests.""" def __init__(self, prompt: str): self.prompt = prompt self.timestamp = datetime.now().isoformat() self.config = None self.execution_log = None self.executable_report = None self.errors = [] # Store recent requests for demo recent_requests = [] @app.route("/") def index(): """Main page.""" return render_template("index.html") @app.route("/api/generate", methods=["POST"]) def generate(): """Generate config from prompt.""" try: data = request.json prompt = data.get("prompt", "").strip() if not prompt: return jsonify({"error": "Prompt is required"}), 400 if len(prompt) > 2000: return jsonify({"error": "Prompt is too long (max 2000 chars)"}), 400 # Create request tracker req = GenerationRequest(prompt) # Generate config, exec_log = pipeline.generate(prompt) req.config = config req.execution_log = exec_log # Check executability is_executable, exec_report = validate_config_executable(config) req.executable_report = exec_report # Store request recent_requests.append(req) if len(recent_requests) > 20: recent_requests.pop(0) return jsonify({ "success": True, "config": config, "execution_log": exec_log, "executable_report": exec_report, "is_executable": is_executable, }) except Exception as e: return jsonify({ "success": False, "error": str(e) }), 500 @app.route("/api/validate", methods=["POST"]) def validate(): """Validate a config.""" try: data = request.json config = data.get("config", {}) # Validate is_executable, report = validate_config_executable(config) return jsonify({ "success": True, "is_executable": is_executable, "report": report, }) except Exception as e: return jsonify({ "success": False, "error": str(e) }), 500 @app.route("/api/recent", methods=["GET"]) def get_recent(): """Get recent requests.""" recent = [] for req in recent_requests[-10:]: recent.append({ "timestamp": req.timestamp, "prompt": req.prompt[:100], "success": req.config is not None, "executable": req.executable_report.get("is_executable", False) if req.executable_report else False, }) return jsonify({"recent": recent}) @app.route("/api/example", methods=["GET"]) def get_example(): """Get an example generation.""" example_prompt = "Build a CRM with login, contacts, dashboard, role-based access, and premium plan with payments." config, exec_log = pipeline.generate(example_prompt) is_executable, exec_report = validate_config_executable(config) return jsonify({ "prompt": example_prompt, "config": config, "executable": is_executable, }) @app.route("/api/health", methods=["GET"]) def health(): """Health check.""" return jsonify({ "status": "healthy", "timestamp": datetime.now().isoformat(), "total_requests": len(recent_requests), }) # Error handlers @app.errorhandler(404) def not_found(e): return jsonify({"error": "Not found"}), 404 @app.errorhandler(500) def internal_error(e): return jsonify({"error": "Internal server error"}), 500 if __name__ == "__main__": port = int(os.environ.get("PORT", 8080)) debug_mode = os.environ.get("DEBUG", "false").lower() == "true" app.run(host="0.0.0.0", port=port, debug=debug_mode)