Add explain, debug and optimise llm options, also make it runtime specific

This commit is contained in:
Peter Stockings
2025-11-19 22:28:00 +11:00
parent 7e5df46997
commit dfcbd9263e
2 changed files with 249 additions and 258 deletions

View File

@@ -1,96 +1,69 @@
import os
import json
import requests
from flask import Blueprint, jsonify, request
from flask import Blueprint, request, jsonify
from flask_login import login_required
llm = Blueprint('llm', __name__)
def _generate_script_from_natural_language(natural_query):
"""Generates a Javascript function from natural language using Gemini REST API."""
gemni_model = os.environ.get("GEMINI_MODEL", "gemini-1.5-flash")
def _call_llm(action, natural_query, code, runtime, logs):
api_key = os.environ.get("GEMINI_API_KEY")
model = os.environ.get("GEMINI_MODEL", "gemini-2.0-flash")
if not api_key:
return None, "GEMINI_API_KEY environment variable not set."
return None, "GEMINI_API_KEY not set."
api_url = f"https://generativelanguage.googleapis.com/v1beta/models/{gemni_model}:generateContent?key={api_key}"
api_url = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={api_key}"
headers = {'Content-Type': 'application/json'}
try:
prompt = f"""
You are an expert Javascript developer. Your task is to write a complete, production-ready Javascript async function based on the user's request.
system_instruction = ""
user_prompt = ""
if action == "generate":
system_instruction = f"""
You are an expert {runtime} developer. Your task is to write a complete, production-ready {runtime} async function based on the user's request.
**Function Signature:**
Your function MUST have the following signature: `async (req, environment) => {{ ... }}`
- `req`: An object containing details about the incoming HTTP request (e.g., `req.method`, `req.headers`, `req.body`, `req.query`).
- `environment`: A mutable JSON object that persists across executions. You can read and write to it to maintain state.
Your function MUST have the following signature: `async (req, environment) => {{ ... }}` (for Node/Deno) or `def main(req, environment):` (for Python).
- `req`: An object containing details about the incoming HTTP request.
- `environment`: A mutable JSON object that persists across executions.
**Environment & Constraints:**
- The function will be executed in a simple, sandboxed Javascript environment.
- **CRITICAL**: ALL helper functions or variables MUST be defined *inside* the main `async` function. Do not define anything in the global scope.
- **DO NOT** use `require()`, `import`, or any other module loading system.
- **DO NOT** access the file system (`fs` module) or make network requests.
- You have access to a `console.log()` function for logging.
**Response Helpers:**
You must use one of the following functions to return a response:
- `HtmlResponse(body)`: Returns an HTML response.
- `JsonResponse(body)`: Returns a JSON response.
- `TextResponse(body)`: Returns a plain text response.
- `RedirectResponse(url)`: Redirects the user.
**Complex Example (Tic-Tac-Toe):**
```javascript
async (req, environment) => {{
// Helper function defined INSIDE the main function
function checkWinner(board) {{
const winConditions = [
[0, 1, 2], [3, 4, 5], [6, 7, 8],
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];
for (const condition of winConditions) {{
const [a, b, c] = condition;
if (board[a] && board[a] === board[b] && board[a] === board[c]) {{
return board[a];
}}
}}
return null;
}}
if (!environment.board) {{
environment.board = Array(9).fill("");
environment.turn = "X";
environment.winner = null;
}}
if (req.method === "POST" && req.json && req.json.move !== undefined) {{
const move = parseInt(req.json.move);
if (environment.board[move] === "" && !environment.winner) {{
environment.board[move] = environment.turn;
environment.turn = environment.turn === "X" ? "O" : "X";
environment.winner = checkWinner(environment.board);
}}
}}
const boardHTML = environment.board.map((cell, index) => `<button hx-post="/f/${{req.path.split('/')[2]}}/${{req.path.split('/')[3]}}" hx-target="body" hx-swap="innerHTML" name="move" value="${{index}}">${{cell || '&nbsp;'}}</button>`).join("");
const message = environment.winner ? `Player ${{environment.winner}} wins!` : `Turn: ${{environment.turn}}`;
return HtmlResponse(`
<html>
<head><title>Tic-Tac-Toe</title><script src="https://unpkg.com/htmx.org@1.9.9"></script></head>
<body><h1>${{message}}</h1><div>${{boardHTML}}</div></body>
</html>
`);
}}
```
**User's request:** "{natural_query}"
Return ONLY the complete Javascript function code, without any explanation, comments, or surrounding text/markdown.
**Constraints:**
- The function will be executed in a sandboxed environment.
- Define ALL helper functions inside the main function.
- DO NOT use external module loading (require/import) unless standard lib. DO NOT IMPORT!
- Return ONLY the code.
"""
user_prompt = f"Write a function that: {natural_query}"
elif action == "explain":
system_instruction = f"You are an expert {runtime} developer. Explain what the following code does in simple, concise terms suitable for a developer."
user_prompt = f"Code:\n```\n{code}\n```"
elif action == "optimize":
system_instruction = f"""
You are an expert {runtime} developer. Analyze the following code and provide an optimized version.
Focus on performance, readability, and best practices.
Return ONLY the optimized code, without markdown formatting or explanation.
"""
user_prompt = f"Code:\n```\n{code}\n```"
elif action == "debug":
system_instruction = f"""
You are an expert {runtime} developer. Analyze the following code and error logs.
Explain the error and provide a fixed version of the code.
Format your response as:
**Explanation:** [Brief explanation of the bug]
**Fix:**
```
[Fixed Code]
```
"""
user_prompt = f"Code:\n```\n{code}\n```\n\nLogs:\n{logs}"
try:
payload = json.dumps({
"contents": [{"parts": [{"text": prompt}]}]
"contents": [{"parts": [{"text": system_instruction + "\n\n" + user_prompt}]}]
})
response = requests.post(api_url, headers=headers, data=payload)
@@ -107,23 +80,20 @@ Return ONLY the complete Javascript function code, without any explanation, comm
if not parts:
return None, "No parts found in API response content."
generated_script = parts[0].get('text', '').strip()
generated_text = parts[0].get('text', '').strip()
# More robustly extract from markdown
if generated_script.startswith("```javascript"):
generated_script = generated_script[12:]
if generated_script.endswith("```"):
generated_script = generated_script[:-3]
generated_script = generated_script.strip()
# Clean up code blocks for generate/optimize actions
if action in ["generate", "optimize"]:
if generated_text.startswith("```"):
# Find first newline
first_newline = generated_text.find("\n")
if first_newline != -1:
generated_text = generated_text[first_newline+1:]
if generated_text.endswith("```"):
generated_text = generated_text[:-3]
generated_text = generated_text.strip()
# Remove any leading non-code characters
if not generated_script.startswith('async ('):
async_start = generated_script.find('async (')
if async_start != -1:
generated_script = generated_script[async_start:]
return generated_script.strip(), None
return generated_text, None
except requests.exceptions.RequestException as e:
return None, f"Error communicating with API: {e}"
@@ -134,16 +104,25 @@ Return ONLY the complete Javascript function code, without any explanation, comm
@login_required
def generate_script():
try:
natural_query = request.json.get('natural_query')
if not natural_query:
return jsonify({"error": "natural_query is required"}), 400
data = request.json
action = data.get('action', 'generate') # Default to generate for backward compatibility
natural_query = data.get('natural_query')
code = data.get('code')
runtime = data.get('runtime', 'node')
logs = data.get('logs')
script_content, error = _generate_script_from_natural_language(natural_query)
if action == 'generate' and not natural_query:
return jsonify({"error": "natural_query is required for generation"}), 400
if action in ['explain', 'optimize', 'debug'] and not code:
return jsonify({"error": "code is required for this action"}), 400
result, error = _call_llm(action, natural_query, code, runtime, logs)
if error:
return jsonify({"error": error}), 500
return jsonify({"script_content": script_content})
return jsonify({"result": result})
except Exception as e:
return jsonify({'error': str(e)}), 500