| """ |
| Project Jarvis — Evolutionary Cortex |
| Contains the meta-tools that allow JARVIS to rewrite himself. |
| """ |
|
|
| import os |
| import logging |
| import importlib.util |
| from pathlib import Path |
|
|
| from app.services.tools import dynamic |
|
|
| logger = logging.getLogger("friday.evolution") |
|
|
| USER_TOOLS_DIR = Path("/Users/paritosh/Desktop/Jarvis/backend/app/services/tools/user_tools") |
| USER_TOOLS_DIR.mkdir(parents=True, exist_ok=True) |
|
|
| def develop_new_tool_tool(tool_name: str, code: str, description: str) -> str: |
| """ |
| Autonomously develops and registers a new Python tool. |
| Args: |
| tool_name: The slug name of the tool (e.g. 'extract_pdf_tables') |
| code: Full Python function code. The function name MUST match 'tool_name' + '_tool'. |
| description: The LLM-facing description of what the tool does. |
| """ |
| import subprocess |
| import sys |
| |
| try: |
| |
| |
| import re |
| imports = re.findall(r"^(?:import|from) (\w+)", code, re.MULTILINE) |
| standard_libs = ["os", "sys", "time", "math", "random", "json", "re", "subprocess", "threading"] |
| for lib in imports: |
| if lib not in standard_libs: |
| logger.info(f"Evolution: Dependency Detected: {lib}. Installing...") |
| subprocess.run([sys.executable, "-m", "pip", "install", lib], check=True) |
| |
| filename = f"{tool_name}.py" |
| filepath = USER_TOOLS_DIR / filename |
| |
| |
| with open(filepath, "w") as f: |
| f.write(code) |
| |
| |
| spec = importlib.util.spec_from_file_location(tool_name, str(filepath)) |
| module = importlib.util.module_from_spec(spec) |
| spec.loader.exec_module(module) |
| |
| |
| func_name = f"{tool_name}_tool" |
| if not hasattr(module, func_name): |
| return f"Failure: The code did not contain a function named '{func_name}'." |
| |
| |
| real_func = getattr(module, func_name) |
| |
| |
| |
| logger.info(f"Evolution: Validating '{tool_name}' logic...") |
| try: |
| |
| |
| |
| |
| |
| |
| import inspect |
| sig = inspect.signature(real_func) |
| logger.info(f"Evolution: Tool '{tool_name}' verified with signature {sig}") |
| except Exception as ve: |
| return f"Validation Failure: The developed tool has an invalid signature: {ve}" |
| |
| |
| dynamic.register_dynamic_tool(tool_name, { |
| "func": real_func, |
| "description": description |
| }) |
| |
| logger.info(f"Evolution: Sovereign tool '{tool_name}' is now live and validated.") |
| return f"Success: Tool '{tool_name}' has been developed, dependency-checked, and registered in the neural cortex, Paritosh Sir." |
|
|
| except Exception as e: |
| logger.error(f"Evolution Failed for {tool_name}: {e}") |
| return f"Failure in evolution: {e}" |
|
|
| def self_repair_tool(target: str) -> str: |
| """ |
| Triggers an autonomous diagnostic and repair cycle for a specific system component. |
| Args: |
| target: The component to repair (e.g., 'ffmpeg', 'pip:package_name', 'ollama', 'ports'). |
| """ |
| from app.core.sentinel import sentinel |
| logger.info(f"Evolution: Starting self-repair for target: {target}") |
| |
| if target == "diagnostic": |
| |
| sentinel._check_all() |
| return "Full system diagnostic complete. Sentinel is orchestrating repairs for all detected issues, Sir." |
| |
| |
| result = sentinel.run_self_repair(target) |
| return f"Self-repair execution result for '{target}': {result}" |
|
|