#!/bin/bash # OpenCortex: The Unified Conductor set -e PORT=9105 HOST=${1:-localhost} RED='\033[0;31m'; GREEN='\033[0;32m'; BLUE='\033[0;34m'; YELLOW='\033[0;33m'; NC='\033[0m' command_exists() { command -v "$1" >/dev/null 2>&1; } # --- 1. BOOTSTRAP (Clone) --- if [ ! -d ".git" ] && [[ ! "$(pwd)" =~ "opencortex" ]]; then echo -e "${BLUE}=== OpenCortex: Zero-to-One Bootstrapper ===${NC}" TARGET_DIR="opencortex" if [ ! -d "$TARGET_DIR" ]; then echo -e "Cloning repository..." git clone http://10.10.10.201:3001/amr/opencortex.git "$TARGET_DIR" fi cd "$TARGET_DIR" git submodule update --init --recursive exec ./opencortex.sh "$@" fi # --- 2. SETUP (Deps & Tangle) --- setup_system() { echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}" # Dependencies if ! command_exists sbcl; then echo -e "Installing dependencies..." sudo apt-get update && sudo apt-get install -y sbcl emacs git curl socat || true fi # Quicklisp if [ ! -d "$HOME/quicklisp" ]; then echo -e "Installing Quicklisp..." curl -O https://beta.quicklisp.org/quicklisp.lisp sbcl --non-interactive --load quicklisp.lisp --eval "(quicklisp-quickstart:install)" --eval "(ql-util:without-prompting (ql:add-to-init-file))" rm quicklisp.lisp fi # Tangle if [ ! -f "src/package.lisp" ]; then echo -e "Tangling brain from literate source..." mkdir -p src for f in literate/*.org; do emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true done fi # .env if [ ! -f .env ]; then cp .env.example .env sed -i "s/MEMEX_USER=.*/MEMEX_USER=\"User\"/g" .env sed -i "s/MEMEX_ASSISTANT=.*/MEMEX_ASSISTANT=\"OpenCortex\"/g" .env sed -i "s|SKILLS_DIR=.*|SKILLS_DIR=\"$(pwd)/skills\"|g" .env fi # PATH installation mkdir -p "$HOME/.local/bin" ln -sf "$(pwd)/opencortex.sh" "$HOME/.local/bin/opencortex" echo -e "${GREEN}✓ Setup complete. 'opencortex' command ready in PATH.${NC}" } if [ ! -f "src/package.lisp" ] || [ ! -f .env ]; then setup_system fi # --- 3. BOOT (The Brain) --- if [[ "$1" == "--boot" ]]; then echo -e "${BLUE}Starting OpenCortex Brain...${NC}" if [ -f .env ]; then while IFS='=' read -r key value || [ -n "$key" ]; do if [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then value=$(echo "$value" | sed 's/^"//;s/"$//') export "$key=$value" fi done < .env fi exec sbcl --non-interactive \ --eval "(load \"~/quicklisp/setup.lisp\")" \ --eval "(push \"$(cd "$(dirname "$0")" && pwd)/\" asdf:*central-registry*)" \ --eval "(ql:quickload :opencortex)" \ --eval "(opencortex:main)" fi # --- 4. INTERACT (The Client) --- if command_exists socat && socat - TCP:$HOST:$PORT,connect-timeout=1 2>/dev/null; then socat READLINE,history=$HOME/.org_agent_history TCP:$HOST:$PORT exit 0 fi echo -e "${YELLOW}Brain is offline. Awakening...${NC}" ./opencortex.sh --boot > brain.log 2>&1 & sleep 15 exec ./opencortex.sh "$@"