Add files via upload
This commit is contained in:
1 parent
fb46b5e6a1
commit
067f5289b2
26 files changed
+4189
-3033
No files matched your search
+130
-76
@@ -12,10 +12,26 @@ def get_base_dir():
|
||||
return Path(__file__).resolve().parent.parent
|
||||
|
||||
BASE_DIR = get_base_dir()
|
||||
API_CONFIG_PATH = BASE_DIR / "config" / "api_keys.json"
|
||||
DESKTOP = Path.home() / "Desktop"
|
||||
MAX_BUILD_ATTEMPTS = 3
|
||||
GEMINI_MODEL = "gemini-2.5-flash"
|
||||
|
||||
from core.llm_client import call_llm_text as _llm
|
||||
|
||||
def _get_api_key() -> str:
|
||||
with open(API_CONFIG_PATH, "r", encoding="utf-8") as f:
|
||||
return json.load(f)["gemini_api_key"]
|
||||
|
||||
|
||||
def _get_gemini(model: str = GEMINI_MODEL):
|
||||
from google import genai
|
||||
_c = genai.Client(api_key=_get_api_key())
|
||||
|
||||
class _W:
|
||||
def generate_content(self, contents):
|
||||
return _c.models.generate_content(model=model, contents=contents)
|
||||
|
||||
return _W()
|
||||
|
||||
|
||||
def _clean_code(text: str) -> str:
|
||||
@@ -134,28 +150,47 @@ def _detect_intent(description: str, file_path: str, code: str) -> str:
|
||||
return "write"
|
||||
|
||||
def _write(description: str, language: str, output_path: str, player=None) -> tuple[str, Path]:
|
||||
lang = language or "python"
|
||||
system = f"You are an expert {lang} developer. Output ONLY raw code, no markdown, no backticks, no explanations."
|
||||
prompt = (
|
||||
f"Write clean, working, well-commented {lang} code.\n"
|
||||
f"Handle errors and edge cases. Use modern best practices.\n\n"
|
||||
f"Description: {description}\n\nCode:"
|
||||
)
|
||||
code = _clean_code(_llm(prompt, system=system))
|
||||
path = _resolve_save_path(output_path, lang)
|
||||
lang = language or "python"
|
||||
model = _get_gemini()
|
||||
|
||||
prompt = f"""You are an expert {lang} developer.
|
||||
Write clean, working, well-commented {lang} code for the description below.
|
||||
|
||||
Rules:
|
||||
- Output ONLY the code. No explanation, no markdown, no backticks.
|
||||
- Add helpful inline comments.
|
||||
- Handle errors and edge cases properly.
|
||||
- Use modern best practices.
|
||||
|
||||
Description: {description}
|
||||
|
||||
Code:"""
|
||||
|
||||
response = model.generate_content(prompt)
|
||||
code = _clean_code(response.text)
|
||||
path = _resolve_save_path(output_path, lang)
|
||||
_save_file(path, code)
|
||||
return code, path
|
||||
|
||||
|
||||
def _fix_code(code: str, error_output: str, description: str) -> str:
|
||||
system = "You are an expert debugger. Return ONLY the corrected code — no explanation, no markdown, no backticks."
|
||||
prompt = (
|
||||
f"Fix the code below. It failed with this error.\n\n"
|
||||
f"Original goal: {description}\n\n"
|
||||
f"Error:\n{error_output[:2000]}\n\n"
|
||||
f"Broken code:\n{code}\n\nFixed code:"
|
||||
)
|
||||
return _clean_code(_llm(prompt, system=system))
|
||||
model = _get_gemini()
|
||||
prompt = f"""You are an expert debugger.
|
||||
The code below failed with the following error. Fix it.
|
||||
Return ONLY the corrected code — no explanation, no markdown, no backticks.
|
||||
|
||||
Original goal: {description}
|
||||
|
||||
Error:
|
||||
{error_output[:2000]}
|
||||
|
||||
Broken code:
|
||||
{code}
|
||||
|
||||
Fixed code:"""
|
||||
|
||||
response = model.generate_content(prompt)
|
||||
return _clean_code(response.text)
|
||||
|
||||
|
||||
def _run_file(path: Path, args: list, timeout: int) -> str:
|
||||
@@ -273,14 +308,21 @@ def _edit_action(file_path, instruction, player) -> str:
|
||||
if player:
|
||||
player.write_log("[Code] Editing file...")
|
||||
|
||||
system = "You are an expert code editor. Return ONLY the complete updated code — no explanation, no markdown, no backticks."
|
||||
prompt = (
|
||||
f"Apply the following change to the code below.\n\n"
|
||||
f"Change: {instruction}\n\n"
|
||||
f"Original code:\n{content}\n\nUpdated code:"
|
||||
)
|
||||
model = _get_gemini()
|
||||
prompt = f"""You are an expert code editor.
|
||||
Apply the following change to the code below.
|
||||
Return ONLY the complete updated code — no explanation, no markdown, no backticks.
|
||||
|
||||
Change: {instruction}
|
||||
|
||||
Original code:
|
||||
{content}
|
||||
|
||||
Updated code:"""
|
||||
|
||||
try:
|
||||
edited = _clean_code(_llm(prompt, system=system))
|
||||
response = model.generate_content(prompt)
|
||||
edited = _clean_code(response.text)
|
||||
except Exception as e:
|
||||
return f"Could not edit code: {e}"
|
||||
|
||||
@@ -300,13 +342,19 @@ def _explain_action(file_path, code, player) -> str:
|
||||
if player:
|
||||
player.write_log("[Code] Analyzing code...")
|
||||
|
||||
system = "You are an expert programmer. Explain code concisely in 3-6 sentences."
|
||||
prompt = (
|
||||
f"Explain what this code does — what it does, how it works, important details.\n\n"
|
||||
f"Code:\n{code[:4000]}\n\nExplanation:"
|
||||
)
|
||||
model = _get_gemini()
|
||||
prompt = f"""Explain what this code does in simple, clear language.
|
||||
Focus on: what it does, how it works, and any important details.
|
||||
Be concise — 3 to 6 sentences maximum.
|
||||
|
||||
Code:
|
||||
{code[:4000]}
|
||||
|
||||
Explanation:"""
|
||||
|
||||
try:
|
||||
return _llm(prompt, system=system)
|
||||
response = model.generate_content(prompt)
|
||||
return response.text.strip()
|
||||
except Exception as e:
|
||||
return f"Could not explain code: {e}"
|
||||
|
||||
@@ -334,15 +382,26 @@ def _optimize_action(file_path, code, language, output_path, player) -> str:
|
||||
if player:
|
||||
player.write_log("[Code] Optimizing code...")
|
||||
|
||||
lang = language or "python"
|
||||
system = f"You are an expert {lang} developer. Return ONLY the optimized code — no explanation, no markdown, no backticks."
|
||||
prompt = (
|
||||
f"Optimize this {lang} code for performance, readability, and best practices. "
|
||||
f"Remove dead code and unnecessary complexity.\n\n"
|
||||
f"Original code:\n{code[:6000]}\n\nOptimized code:"
|
||||
)
|
||||
lang = language or "python"
|
||||
model = _get_gemini()
|
||||
|
||||
prompt = f"""You are an expert {lang} developer and code reviewer.
|
||||
Optimize the following code for:
|
||||
1. Performance — eliminate unnecessary operations, use efficient data structures
|
||||
2. Readability — clear variable names, proper formatting, logical structure
|
||||
3. Best practices — modern {lang} patterns, error handling, type hints if applicable
|
||||
4. Remove dead code, redundant comments, and unnecessary complexity
|
||||
|
||||
Return ONLY the optimized code — no explanation, no markdown, no backticks.
|
||||
|
||||
Original code:
|
||||
{code[:6000]}
|
||||
|
||||
Optimized code:"""
|
||||
|
||||
try:
|
||||
optimized = _clean_code(_llm(prompt, system=system))
|
||||
response = model.generate_content(prompt)
|
||||
optimized = _clean_code(response.text)
|
||||
except Exception as e:
|
||||
return f"Could not optimize code: {e}"
|
||||
|
||||
@@ -387,51 +446,44 @@ def _screen_debug_action(description, file_path, player, speak=None) -> str:
|
||||
print(f"[Code] ⚠️ Could not read file: {err}")
|
||||
|
||||
try:
|
||||
import base64, json as _json, requests as _req
|
||||
from pathlib import Path as _Path
|
||||
from google import genai
|
||||
from google.genai import types
|
||||
|
||||
cfg = {}
|
||||
cfg_file = _Path(__file__).resolve().parent.parent / "config" / "api_keys.json"
|
||||
try:
|
||||
cfg = _json.loads(cfg_file.read_text(encoding="utf-8"))
|
||||
except Exception:
|
||||
pass
|
||||
client = genai.Client(api_key=_get_api_key())
|
||||
|
||||
ollama_url = cfg.get("llm_url", "http://localhost:11434").rstrip("/")
|
||||
vision_model = cfg.get("vision_model") or cfg.get("llm_model", "llava")
|
||||
image_bytes = screenshot_path.read_bytes()
|
||||
image_base64 = _image_to_base64(screenshot_path)
|
||||
|
||||
image_bytes = screenshot_path.read_bytes()
|
||||
b64 = base64.b64encode(image_bytes).decode("ascii")
|
||||
user_question = description or "What error or problem do you see on the screen? How can it be fixed?"
|
||||
context = ""
|
||||
|
||||
context = ""
|
||||
if file_content:
|
||||
context = f"\n\nRelated file content:\n```\n{file_content[:4000]}\n```"
|
||||
context = f"\n\nAdditionally, here is the related file content:\n```\n{file_content[:4000]}\n```"
|
||||
|
||||
analysis_prompt = (
|
||||
f"You are an expert programmer/debugger analyzing a screenshot.\n\n"
|
||||
f"User question: {user_question}{context}\n\n"
|
||||
f"Identify errors, explain the cause, and provide a fix. "
|
||||
f"If you see code, show the corrected version."
|
||||
analysis_prompt = f"""You are an expert programmer and debugger analyzing a screenshot.
|
||||
|
||||
User's question: {user_question}{context}
|
||||
|
||||
Please:
|
||||
1. Identify any errors, exceptions, or problems visible on the screen
|
||||
2. Explain what is causing the problem in simple terms
|
||||
3. Provide a concrete fix or solution
|
||||
4. If there's code visible, show the corrected version
|
||||
|
||||
Be specific and actionable. If you see an error message, quote it exactly."""
|
||||
|
||||
contents = [
|
||||
types.Part.from_bytes(data=image_bytes, mime_type="image/png"),
|
||||
analysis_prompt,
|
||||
]
|
||||
|
||||
response = client.models.generate_content(
|
||||
model="gemini-2.5-flash",
|
||||
contents=contents,
|
||||
)
|
||||
|
||||
resp = _req.post(
|
||||
f"{ollama_url}/api/chat",
|
||||
json={
|
||||
"model": vision_model,
|
||||
"stream": False,
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": analysis_prompt,
|
||||
"images": [b64],
|
||||
}
|
||||
],
|
||||
},
|
||||
timeout=60,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
analysis = (resp.json().get("message", {}).get("content") or "").strip()
|
||||
print("[Code] ✅ Screen analysis complete")
|
||||
analysis = response.text.strip()
|
||||
print(f"[Code] ✅ Screen analysis complete")
|
||||
|
||||
try:
|
||||
screenshot_path.unlink()
|
||||
@@ -439,6 +491,7 @@ def _screen_debug_action(description, file_path, player, speak=None) -> str:
|
||||
pass
|
||||
|
||||
if file_path and file_content:
|
||||
|
||||
code_match = re.search(r"```[a-zA-Z]*\n(.*?)```", analysis, re.DOTALL)
|
||||
if code_match:
|
||||
fixed_code = code_match.group(1).strip()
|
||||
@@ -450,6 +503,7 @@ def _screen_debug_action(description, file_path, player, speak=None) -> str:
|
||||
return analysis
|
||||
|
||||
except Exception as e:
|
||||
|
||||
try:
|
||||
screenshot_path.unlink()
|
||||
except Exception:
|
||||
|
||||
Reference in new issue
Block a user