diff --git a/ai-hub/app/api/routes/nodes.py b/ai-hub/app/api/routes/nodes.py index 04a8029..da107fb 100644 --- a/ai-hub/app/api/routes/nodes.py +++ b/ai-hub/app/api/routes/nodes.py @@ -376,6 +376,127 @@ # M4: Config YAML Download (admin only) # ================================================================== + 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. + +## 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 + +### 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 + ``` + +### Windows +1. Open the folder in File Explorer. +2. Double-click `run.bat`. + +The scripts will automatically set up a Python virtual environment, install dependencies, and start the node. + +## 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. +""" + + RUN_SH_CONTENT = """#!/bin/bash +# Cortex Agent Node — Seamless Runner +set -e + +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." + exit 1 +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 you are on Ubuntu/Debian, try: sudo apt install python3-venv" + exit 1 + } +fi + +# Activate venv +# shellcheck source=/dev/null +source "$VENV/bin/activate" + +# 3. Dependency Installation +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 (may require sudo)..." + # --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 +""" + + 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+. + 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( "/admin/{node_id}/config.yaml", response_model=schemas.NodeConfigYamlResponse, @@ -437,6 +558,18 @@ # 3. Add the generated config YAML as 'agent_config.yaml' zip_file.writestr("agent_config.yaml", config_yaml) + + # 4. Add README and run.sh / run.bat + zip_file.writestr("README.md", README_CONTENT) + + # Create run.sh with execute permissions (external_attr) + run_sh_info = zipfile.ZipInfo("run.sh") + run_sh_info.external_attr = 0o100755 << 16 # -rwxr-xr-x + run_sh_info.compress_type = zipfile.ZIP_DEFLATED + zip_file.writestr(run_sh_info, RUN_SH_CONTENT) + + # Create run.bat + zip_file.writestr("run.bat", RUN_BAT_CONTENT) zip_buffer.seek(0) return StreamingResponse(