compiler

Log | Files | Refs | README

commit fd8cfc2fa724125742f5e5f2a93e40b4a34d92b1
parent 47b8761a344f31222f1b5348b75700455aa6cffb
Author: AMIT DUTTA <amitdutta4255@gmail.com>
Date:   Tue, 13 Jan 2026 21:18:12 +0530

Update backend.py
Diffstat:
Mbackend.py | 66+++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/backend.py b/backend.py @@ -35,7 +35,7 @@ def install_package(package_name, ws=None, loop=None): except Exception as e: error_msg = f"Failed to install {package_name}: {e}" print(error_msg) - if ws and loop: + if ws and loop: asyncio.run_coroutine_threadsafe( ws.send_json({'type': 'stdout', 'data': f"\n[Error] {error_msg}\n"}), loop @@ -60,7 +60,7 @@ async def handle_client(request): ws = web.WebSocketResponse() await ws.prepare(request) - print(f"Client connected: {request. remote}") + print(f"Client connected: {request.remote}") process = None # Helper to read output stream in a separate thread @@ -79,7 +79,7 @@ async def handle_client(request): ws. send_json({'type': 'status', 'msg': 'Program finished'}), loop ) - except Exception: + except Exception: pass try: @@ -87,7 +87,7 @@ async def handle_client(request): if msg.type == web.WSMsgType.TEXT: data = json.loads(msg.data) - if data. get('type') == 'run': + if data.get('type') == 'run': code = data.get('code') language = data.get('language', 'python') @@ -101,11 +101,11 @@ async def handle_client(request): with open(filename, "w", encoding="utf-8") as f: f.write(code) - # 🔒 SECURITY FIX: Sanitized environment (no API keys) + # 🔒 SECURITY FIX: Sanitized environment (no API keys) safe_env = { "PYTHONIOENCODING": "utf-8", "PATH": os.environ.get("PATH", ""), - "PYTHONPATH": os.environ. get("PYTHONPATH", ""), + "PYTHONPATH": os.environ.get("PYTHONPATH", ""), "HOME": os.environ.get("HOME", ""), "USER": os.environ.get("USER", ""), "LANG": os.environ.get("LANG", "en_US.UTF-8"), @@ -129,7 +129,7 @@ async def handle_client(request): executable = "./a.out" if platform.system() != "Windows" else "a.exe" with open(filename, "w", encoding="utf-8") as f: - f.write(code) + f. write(code) await ws.send_json({'type': 'status', 'msg': 'Compiling C.. .'}) @@ -179,11 +179,11 @@ async def handle_client(request): # 🔒 SECURITY: Sanitized environment safe_env = { - "PATH": os.environ. get("PATH", ""), - "HOME": os.environ. get("HOME", ""), - "USER": os.environ. get("USER", ""), - "LANG": os.environ.get("LANG", "en_US.UTF-8"), - "TMPDIR": os.environ.get("TMPDIR", "/tmp"), + "PATH": os.environ.get("PATH", ""), + "HOME": os.environ.get("HOME", ""), + "USER": os.environ.get("USER", ""), + "LANG": os. environ.get("LANG", "en_US.UTF-8"), + "TMPDIR": os.environ.get("TMPDIR", "/tmp"), } compile_process = subprocess.run( @@ -194,8 +194,8 @@ async def handle_client(request): ) if compile_process.returncode != 0: - await ws.send_json({'type': 'stdout', 'data': f"Compilation Error:\n{compile_process.stderr}"}) - await ws.send_json({'type': 'status', 'msg': 'Compilation Failed'}) + await ws. send_json({'type': 'stdout', 'data': f"Compilation Error:\n{compile_process.stderr}"}) + await ws.send_json({'type': 'status', 'msg': 'Compilation Failed'}) continue await ws. send_json({'type': 'status', 'msg': 'Running C++ Binary...'}) @@ -230,16 +230,16 @@ async def handle_client(request): elif data.get('type') == 'ai_fix': code = data.get('code') error_log = data.get('error') - language = data. get('language', 'c') + language = data.get('language', 'c') - if not GEMINI_API_KEY: - await ws.send_json({'type': 'ai_error', 'msg': 'Server Error: GEMINI_API_KEY not configured.'}) + if not GEMINI_API_KEY: + await ws.send_json({'type': 'ai_error', 'msg': 'Server Error: GEMINI_API_KEY not configured.'}) continue # Construct Prompt - Ask for JSON in plain text - prompt = f"""You are an expert {language.upper()} debugger. Analyze this code and error, then respond with ONLY a valid JSON object (no markdown, no backticks). + prompt = f"""You are an expert {language. upper()} debugger. Analyze this code and error, then respond with ONLY a valid JSON object (no markdown, no backticks). -CODE: +CODE: {code} ERROR: @@ -248,7 +248,7 @@ ERROR: Respond in this exact JSON format: {{ "explanation": "Brief 1-2 sentence explanation of the bug", - "fixed_code": "Complete corrected code here" + "fixed_code": "Complete corrected code here" }}""" try: @@ -260,7 +260,7 @@ Respond in this exact JSON format: async with aiohttp.ClientSession() as session: async with session.post( api_url, - headers={"Content-Type": "application/json"}, + headers={"Content-Type": "application/json"}, json={ "contents": [{"parts": [{"text": prompt}]}], "generationConfig": { @@ -272,12 +272,12 @@ Respond in this exact JSON format: response_text = await resp.text() print(f"[AI] Status: {resp.status}") - print(f"[AI] Response preview: {response_text[:200]}") + print(f"[AI] Response preview: {response_text[: 200]}") if resp.status != 200: await ws.send_json({ 'type': 'ai_error', - 'msg': f"AI API Error (Status {resp.status})" + 'msg': f"AI API Error (Status {resp. status})" }) else: try: @@ -289,19 +289,19 @@ Respond in this exact JSON format: # Clean markdown if present ai_text = ai_text.strip() if ai_text.startswith('```json'): - ai_text = ai_text. split('```json')[1].split('```')[0].strip() - elif ai_text. startswith('```'): + ai_text = ai_text.split('```json')[1].split('```')[0].strip() + elif ai_text.startswith('```'): ai_text = ai_text.split('```')[1].split('```')[0].strip() # Parse JSON parsed_response = json.loads(ai_text) # Validate structure - if 'explanation' in parsed_response and 'fixed_code' in parsed_response: + if 'explanation' in parsed_response and 'fixed_code' in parsed_response: print(f"[AI] Success! Sending response to client") await ws.send_json({ 'type': 'ai_response', - 'data': parsed_response + 'data': parsed_response }) else: print(f"[AI] Invalid structure: {parsed_response}") @@ -312,7 +312,7 @@ Respond in this exact JSON format: except (KeyError, IndexError) as e: print(f"[AI] API structure error: {e}") - await ws.send_json({ + await ws. send_json({ 'type': 'ai_error', 'msg': 'Unexpected API response' }) @@ -326,15 +326,15 @@ Respond in this exact JSON format: except aiohttp.ClientError as e: print(f"[AI] Network error: {e}") - await ws.send_json({ + await ws. send_json({ 'type': 'ai_error', 'msg': 'Network error' }) - except Exception as e: + except Exception as e: print(f"[AI] Unexpected error: {e}") import traceback - traceback. print_exc() - await ws.send_json({ + traceback.print_exc() + await ws. send_json({ 'type': 'ai_error', 'msg': f'Server error: {type(e).__name__}' }) @@ -345,7 +345,7 @@ Respond in this exact JSON format: finally: if process: try: - process.kill() + process. kill() except: pass print("Client disconnected")
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror