diff --git a/ai-hub/app/api/routes/nodes.py b/ai-hub/app/api/routes/nodes.py index e15b53f..8db2891 100644 --- a/ai-hub/app/api/routes/nodes.py +++ b/ai-hub/app/api/routes/nodes.py @@ -434,124 +434,69 @@ README_CONTENT = """# Cortex Agent Node -This bundle contains the Cortex Agent Node, a modular software that connects to your Cortex Hub to execute tasks like browser automation, file management, and terminal control. +This bundle contains the Cortex Agent Node, a modular software that connects to your Cortex Hub. -## Prerequisites - -- **Python 3.10+**: Ensure you have Python installed. -- **Internet Access**: The node needs to reach your Hub at the endpoint specified in `agent_config.yaml`. - -## Quick Start +## Running the Node ### Linux & macOS -1. Open your terminal. -2. Navigate to this directory. -3. Make the runner script executable: - ```bash - chmod +x run.sh - ``` -4. Run the node: - ```bash - ./run.sh - ``` - -*Note for Mac users:* You can also double-click the `run_mac.command` file to start the node directly. +1. Open your terminal in this directory. +2. Make the runner script executable: `chmod +x run.sh` +3. Run: `./run.sh` ### Windows -1. Open the folder in File Explorer. -2. Double-click `run.bat`. +1. Double-click `run.bat`. -The scripts will automatically set up a Python virtual environment, install dependencies, and start the node. +The scripts will automatically detect if the node is provided as a binary executable or source code, and handle the environment setup accordingly. ## Configuration -The `agent_config.yaml` file contains the connection details and security tokens for your node. It has been pre-configured for you. Do not share this file. - -## Troubleshooting - -- **Permissions**: If you encounter permission errors, ensure you are running in a directory where you have write access. -- **Port 50051**: Ensure your network/firewall allows outgoing connections to the Hub's gRPC port. +The `agent_config.yaml` file has been pre-configured with your node's identity and security tokens. Do not share this file. """ RUN_SH_CONTENT = """#!/bin/bash # Cortex Agent Node — Seamless Runner set -e -# Ensure we are in the script's directory (crucial for double-clicking and manual runs) +# Ensure we are in the script's directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" cd "$SCRIPT_DIR" || { echo "❌ Error: Could not change directory to $SCRIPT_DIR"; exit 1; } echo "🚀 Starting Cortex Agent Node..." -# 1. Environment Check -if ! command -v python3 &> /dev/null; then - echo "❌ Error: python3 is not installed. Please install Python 3.10+ and try again." - # Mac users often need to be pointed to python.org or brew - if [[ "$OSTYPE" == "darwin"* ]]; then - echo "💡 Tip: Visit https://www.python.org/downloads/macos/ or run 'brew install python'" - fi - sleep 5 - exit 1 +# 1. Future Binary Check +if [ -f "./agent-node" ]; then + echo "[*] Binary executable detected. Launching..." + chmod +x ./agent-node + ./agent-node + exit $? fi -# 2. Virtual Environment Setup -VENV=".venv" -if [ ! -d "$VENV" ]; then - echo "[*] Creating virtual environment..." - python3 -m venv "$VENV" || { - echo "❌ Error: Failed to create virtual environment." - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - echo "💡 Tip: Try: sudo apt install python3-venv" - fi - sleep 5 +# 2. Source Code Fallback +if [ -d "./agent_node" ]; then + echo "[*] Source code detected. Setting up Python environment..." + if ! command -v python3 &> /dev/null; then + echo "❌ Error: python3 not found. Please install Python 3.10+." exit 1 - } -fi - -# Activate venv -# shellcheck source=/dev/null -if [ -f "$VENV/bin/activate" ]; then - source "$VENV/bin/activate" -elif [ -f "$VENV/Scripts/activate" ]; then - # Fallback for some windows environments running bash - source "$VENV/Scripts/activate" -else - echo "❌ Error: Could not find activation script in $VENV" - exit 1 -fi - -# 3. Dependency Installation -if [ ! -f "requirements.txt" ]; then - echo "❌ Error: requirements.txt not found in $(pwd)" - echo "Contents of $(pwd):" - ls -la - exit 1 -fi - -echo "[*] Ensuring dependencies (pip, gRPC, etc.) are installed..." -pip install --upgrade pip --quiet -pip install -r requirements.txt --quiet - -# 4. Playwright Setup (for Browser Skills) -if grep -q "playwright" requirements.txt; then - if [ ! -f "$VENV/.playwright-installed" ]; then - echo "[*] Installing browser engines and system dependencies..." - # --with-deps ensures chromium actually runs on Linux (installs missing .so files) - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - python3 -m playwright install --with-deps chromium - else - python3 -m playwright install chromium - fi - touch "$VENV/.playwright-installed" fi -fi -# 5. Start the Node -echo "✅ Environment ready. Booting node..." -python3 -m agent_node.main -if [ $? -ne 0 ]; then - echo "⚠️ Node exited with error. Check the logs above." - sleep 5 + VENV=".venv" + if [ ! -d "$VENV" ]; then + echo "[*] Creating virtual environment..." + python3 -m venv "$VENV" + fi + source "$VENV/bin/activate" + + if [ -f "requirements.txt" ]; then + echo "[*] Syncing dependencies..." + pip install --upgrade pip --quiet + pip install -r requirements.txt --quiet + fi + + echo "✅ Environment ready. Booting node..." + python3 -m agent_node.main +else + echo "❌ Error: No executable ('agent-node') or source code ('agent_node/') found in this bundle." + exit 1 fi """ @@ -559,34 +504,38 @@ RUN_BAT_CONTENT = """@echo off echo 🚀 Starting Cortex Agent Node... -python --version >nul 2>&1 -if %errorlevel% neq 0 ( - echo ❌ Error: python is not installed or not in PATH. Please install Python 3.10+. + +if exist agent-node.exe ( + echo [*] Binary executable detected. Launching... + agent-node.exe + exit /b %errorlevel% +) + +if exist agent_node ( + echo [*] Source code detected. Checking environment... + python --version >nul 2>&1 + if %errorlevel% neq 0 ( + echo ❌ Error: python not found. Please install Python 3.10+. + pause + exit /b 1 + ) + if not exist .venv ( + echo [*] Creating virtual environment... + python -m venv .venv + ) + call .venv\\Scripts\\activate + if exist requirements.txt ( + echo [*] Syncing dependencies... + pip install --upgrade pip --quiet + pip install -r requirements.txt --quiet + ) + echo ✅ Environment ready. Booting node... + python -m agent_node.main +) else ( + echo ❌ Error: No executable ('agent-node.exe') or source code ('agent_node/') found. pause exit /b 1 ) -if not exist .venv ( - echo [*] Creating virtual environment... - python -m venv .venv -) -call .venv\\Scripts\\activate -echo [*] Ensuring dependencies are installed... -pip install --upgrade pip --quiet -pip install -r requirements.txt --quiet -findstr "playwright" requirements.txt >nul -if %errorlevel% equ 0 ( - if not exist .venv\\.playwright-installed ( - echo [*] Installing browser engines... - python -m playwright install chromium - echo done > .venv\\.playwright-installed - ) -) -echo ✅ Environment ready. Booting node... -python -m agent_node.main -if %errorlevel% neq 0 ( - echo [!] Node exited with error level %errorlevel% - pause -) """ @router.get( @@ -624,18 +573,27 @@ # Create ZIP in memory zip_buffer = io.BytesIO() with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file: - # 1. Add Agent Node source files from /app/agent-node - source_dir = "/app/agent-node" - if os.path.exists(source_dir): - for root, dirs, files in os.walk(source_dir): - # Exclude unwanted directories - dirs[:] = [d for d in dirs if not d.startswith("sync-node-") and d != "__pycache__" and d != ".git"] + # 1. Add Agent Node files (binary or supporting scripts) + # Try production mount first, then local fallback + source_dirs = ["/app/agent-node-source", "/app/agent-node"] + found_dir = None + for sd in source_dirs: + if os.path.exists(sd): + found_dir = sd + break + + if found_dir: + for root, dirs, files in os.walk(found_dir): + # Exclude source code folders and sensitive/build metadata + dirs[:] = [d for d in dirs if d not in ["agent_node", "shared_core", "protos", "__pycache__", ".git", ".venv"]] for file in files: - if file == ".env" or file == "agent_config.yaml": continue + # Security/Cleanliness Exclusion: Never include raw source, requirements, or local envs + if file.endswith(".py") or file == "requirements.txt" or file == ".env" or file == "agent_config.yaml": + continue file_path = os.path.join(root, file) - rel_path = os.path.relpath(file_path, source_dir) + rel_path = os.path.relpath(file_path, found_dir) zip_file.write(file_path, rel_path) # 2. Add skills from /app/skills