import subprocess import sys import json import re import time from pathlib import Path def get_base_dir(): if getattr(sys, "frozen", False): return Path(sys.executable).parent return Path(__file__).resolve().parent.parent BASE_DIR = get_base_dir() PROJECTS_DIR = Path.home() / "Desktop" / "JarvisProjects" MAX_FIX_ATTEMPTS = 5 from core.llm_client import call_llm_text as _llm def _strip_fences(text: str) -> str: text = text.strip() text = re.sub(r"^```[a-zA-Z]*\r?\n?", "", text) text = re.sub(r"\r?\n?```\s*$", "", text) return text.strip() def _is_rate_limit(error: Exception) -> bool: msg = str(error).lower() return "429" in msg or "quota" in msg or "resource_exhausted" in msg def _parse_traceback(output: str, project_files: list[str]) -> tuple[str | None, int | None]: pattern = re.compile(r'File ["\']([^"\']+\.py)["\'],\s+line\s+(\d+)', re.IGNORECASE) matches = pattern.findall(output) for raw_path, line_str in reversed(matches): raw_name = Path(raw_path).name for pf in project_files: if Path(pf).name == raw_name or pf == raw_path or raw_path.endswith(pf): return pf, int(line_str) return None, None def _classify_error(output: str) -> str: low = output.lower() if any(x in low for x in ("no module named", "modulenotfounderror", "importerror")): return "dependency_error" if "syntaxerror" in low or "invalid syntax" in low: return "syntax_error" if "cannot import" in low or "importerror" in low: return "import_error" if any(x in low for x in ( "traceback", "exception", "error:", "nameerror", "typeerror", "attributeerror", "valueerror", "keyerror", "indexerror", "zerodivisionerror", "filenotfounderror", "permissionerror", )): return "runtime_error" return "none" def _has_error(output: str, run_command: str) -> bool: low = output.lower() if "timed out" in low: return False if not output.strip(): return False error_type = _classify_error(output) return error_type != "none" class RateLimitError(Exception): pass def _plan_project(description: str, language: str) -> dict: system = ( "You are a senior software architect. " "Return ONLY valid JSON β€” no markdown, no explanation." ) prompt = f"""Create a minimal, complete file plan for this project. Language: {language} Description: {description} Return ONLY valid JSON: {{ "project_name": "snake_case_name", "entry_point": "main.py", "files": [ {{ "path": "main.py", "description": "Entry point", "imports": ["utils.helpers"] }}, {{ "path": "utils/helpers.py", "description": "Helpers", "imports": [] }} ], "run_command": "python main.py", "dependencies": ["requests"] }} Rules: dependency order, relative paths, entry point last, stdlib NOT in dependencies. JSON:""" try: raw = _strip_fences(_llm(prompt, system=system)) return json.loads(raw) except json.JSONDecodeError as e: raise ValueError(f"Planner returned invalid JSON: {e}") except Exception as e: raise def _write_file( file_info: dict, project_description: str, all_files: list[dict], language: str, project_dir: Path, already_written: dict[str, str], ) -> str: file_path = file_info["path"] file_desc = file_info.get("description", "") file_imports = file_info.get("imports", []) file_list = "\n".join( f" [{i+1}] {f['path']}: {f.get('description', '')}" for i, f in enumerate(all_files) ) dep_ctx = "" for dep_dotted in file_imports: dep_path = dep_dotted.replace(".", "/") + ".py" if dep_path in already_written: dep_ctx += f"\n\n--- {dep_path} ---\n{already_written[dep_path][:2000]}" system = ( f"You are a senior {language} developer. " "Output ONLY raw code β€” no explanation, no markdown, no backticks." ) prompt = ( f"Project goal: {project_description}\n\n" f"Files (dependency order):\n{file_list}\n" f"{dep_ctx}\n\n" f"Write complete code for: {file_path}\n" f"Purpose: {file_desc}\n" f"{'Imports from: ' + ', '.join(file_imports) if file_imports else 'No project-internal imports.'}\n\n" f"Rules: COMPLETE & RUNNABLE code, no placeholders, match import paths exactly.\n\n" f"Code for {file_path}:" ) try: code = _strip_fences(_llm(prompt, system=system)) full_path = project_dir / file_path full_path.parent.mkdir(parents=True, exist_ok=True) full_path.write_text(code, encoding="utf-8") print(f"[DevAgent] βœ… Written: {file_path} ({len(code)} chars)") return code except Exception as e: raise def _install_dependencies(dependencies: list[str], project_dir: Path) -> str: if not dependencies: return "No external dependencies." to_install = [] for dep in dependencies: pkg_name = re.split(r"[>= bool: vscode_candidates = [ "code", rf"C:\Users\{Path.home().name}\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd", r"C:\Program Files\Microsoft VS Code\bin\code.cmd", ] for cmd in vscode_candidates: try: subprocess.Popen( [cmd, str(project_dir)], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) time.sleep(1.5) print(f"[DevAgent] πŸ’» VSCode opened: {project_dir}") return True except Exception: continue return False def _run_project(run_command: str, project_dir: Path, timeout: int = 30) -> str: print(f"[DevAgent] πŸš€ Running: {run_command}") try: parts = run_command.split() if parts[0].lower() == "python": parts[0] = sys.executable result = subprocess.run( parts, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=timeout, cwd=str(project_dir) ) stdout = result.stdout.strip() stderr = result.stderr.strip() combined_parts = [] if stdout: combined_parts.append(f"STDOUT:\n{stdout}") if stderr: combined_parts.append(f"STDERR:\n{stderr}") return "\n\n".join(combined_parts) if combined_parts else "Ran with no output." except subprocess.TimeoutExpired: return f"Timed out after {timeout}s β€” long-running app (server/GUI) is likely working." except FileNotFoundError as e: return f"Command not found: {e}" except Exception as e: return f"Run error: {e}" def _try_auto_install(error_output: str, project_dir: Path) -> bool: """ModuleNotFoundError varsa eksik paketi otomatik kurmaya Γ§alışır.""" pattern = re.compile( r"No module named ['\"]([a-zA-Z0-9_\-\.]+)['\"]", re.IGNORECASE ) match = pattern.search(error_output) if not match: return False pkg = match.group(1).replace("_", "-").split(".")[0] print(f"[DevAgent] πŸ”§ Auto-installing missing package: {pkg}") try: result = subprocess.run( [sys.executable, "-m", "pip", "install", pkg], capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=60, cwd=str(project_dir) ) return result.returncode == 0 except Exception: return False def _fix_files( error_output: str, project_description: str, all_files: list[dict], file_codes: dict[str, str], language: str, project_dir: Path, entry_point: str, ) -> dict[str, str]: error_file, error_line = _parse_traceback(error_output, list(file_codes.keys())) error_type = _classify_error(error_output) files_to_fix: list[str] = [] if error_file: files_to_fix.append(error_file) if error_type == "import_error": for fi in all_files: if error_file.replace("/", ".").replace(".py", "") in fi.get("imports", []): p = fi["path"] if p not in files_to_fix: files_to_fix.append(p) else: files_to_fix.append(entry_point) updated_codes: dict[str, str] = {} system = f"You are an expert {language} debugger. Return ONLY the complete fixed code β€” no explanation, no markdown, no backticks." for fix_path in files_to_fix: current_code = file_codes.get(fix_path, "") other_ctx = "" for fp, code in file_codes.items(): if fp != fix_path and code: other_ctx += f"\n--- {fp} ---\n{code[:1500]}\n" line_hint = ( f"\nError near line {error_line} in this file." if error_line and fix_path == error_file else "" ) prompt = ( f"Project goal: {project_description}\n\n" f"All files:\n" + "\n".join(f" - {f['path']}: {f.get('description', '')}" for f in all_files) + f"\n\nOther files (context):\n{other_ctx[:3500]}\n\n" f"File to fix: {fix_path}{line_hint}\n" f"Error type: {error_type}\n\n" f"Error output:\n{error_output[:2500]}\n\n" f"Current code:\n{current_code}\n\n" f"Fixed code for {fix_path}:" ) try: fixed = _strip_fences(_llm(prompt, system=system)) full_path = project_dir / fix_path full_path.parent.mkdir(parents=True, exist_ok=True) full_path.write_text(fixed, encoding="utf-8") updated_codes[fix_path] = fixed print(f"[DevAgent] πŸ”§ Fixed: {fix_path}") except Exception as e: print(f"[DevAgent] ⚠️ Could not fix {fix_path}: {e}") return updated_codes def _build_project( description: str, language: str, project_name: str, timeout: int, speak=None, player=None, ) -> str: def log(msg: str): print(f"[DevAgent] {msg}") if player: player.write_log(f"[DevAgent] {msg}") log("Planning project structure...") try: plan = _plan_project(description, language) except RateLimitError: msg = "Rate limit reached, sir. Please try again in a moment." if speak: speak(msg) return msg except ValueError as e: msg = f"Planning failed: {e}" if speak: speak(msg) return msg proj_name = project_name or plan.get("project_name", "jarvis_project") proj_name = re.sub(r"[^\w\-]", "_", proj_name) project_dir = PROJECTS_DIR / proj_name project_dir.mkdir(parents=True, exist_ok=True) files = plan.get("files", []) entry_point = plan.get("entry_point", "main.py") run_command = plan.get("run_command", f"python {entry_point}") dependencies = plan.get("dependencies", []) log(f"Project: {proj_name} | Files: {len(files)} | Entry: {entry_point}") def _dep_sort_key(fi: dict) -> int: return len(fi.get("imports", [])) sorted_files = sorted(files, key=_dep_sort_key) file_codes: dict[str, str] = {} for file_info in sorted_files: file_path = file_info.get("path", "") if not file_path: continue log(f"Writing {file_path}...") for attempt in range(2): try: code = _write_file( file_info=file_info, project_description=description, all_files=files, language=language, project_dir=project_dir, already_written=file_codes, ) file_codes[file_path] = code time.sleep(0.4) break except RateLimitError: if attempt == 0: log("Rate limit β€” waiting 20s...") time.sleep(20) else: log(f"Rate limit retry failed for {file_path}, skipping.") except Exception as e: log(f"Failed to write {file_path}: {e}") break if not file_codes: msg = "I could not write any project files, sir." if speak: speak(msg) return msg if dependencies: install_result = _install_dependencies(dependencies, project_dir) log(install_result) _open_vscode(project_dir) last_output = "" auto_installs = 0 for attempt in range(1, MAX_FIX_ATTEMPTS + 1): log(f"Running project (attempt {attempt}/{MAX_FIX_ATTEMPTS})...") last_output = _run_project(run_command, project_dir, timeout) log(f"Output preview: {last_output[:150]}") if not _has_error(last_output, run_command): msg = ( f"Project '{proj_name}' is working, sir. " f"Built in {attempt} attempt{'s' if attempt > 1 else ''}. " f"Saved to: {project_dir}" ) if speak: speak(msg) return f"{msg}\n\nOutput:\n{last_output}" if attempt == MAX_FIX_ATTEMPTS: break error_type = _classify_error(last_output) if error_type == "dependency_error" and auto_installs < 3: installed = _try_auto_install(last_output, project_dir) if installed: auto_installs += 1 log("Missing dependency installed, retrying...") time.sleep(1) continue log(f"Fixing errors (type: {error_type})...") try: updated = _fix_files( error_output=last_output, project_description=description, all_files=files, file_codes=file_codes, language=language, project_dir=project_dir, entry_point=entry_point, ) file_codes.update(updated) time.sleep(1) except RateLimitError: msg = "Rate limit reached during fix. Project saved, check it manually in VSCode." if speak: speak(msg) return msg except Exception as e: log(f"Fix step failed: {e}") msg = ( f"I couldn't fully fix '{proj_name}' after {MAX_FIX_ATTEMPTS} attempts, sir. " f"Project is saved at {project_dir} β€” open it in VSCode and check manually." ) if speak: speak(msg) return f"{msg}\n\nLast error:\n{last_output[:600]}" def dev_agent( parameters: dict, response=None, player=None, session_memory=None, speak=None, ) -> str: p = parameters or {} description = p.get("description", "").strip() language = p.get("language", "python").strip() project_name = p.get("project_name", "").strip() timeout = int(p.get("timeout", 30)) if not description: return "Please describe the project you want me to build, sir." return _build_project( description = description, language = language, project_name = project_name, timeout = timeout, speak = speak, player = player, )