#!/bin/bash SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" ENV_FILE="${PROJECT_DIR}/.env.development" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' CYAN='\033[0;36m' NC='\033[0m' FAILURES=() check() { if [ $? -eq 0 ]; then echo -e " ${GREEN}✅${NC} $1"; else echo -e " ${RED}❌${NC} $1"; FAILURES+=("$1"); fi } echo -e "${CYAN}====================================${NC}" echo -e "${CYAN} Momentry Core - Startup Sequence${NC}" echo -e "${CYAN}====================================${NC}" echo "" LOG_DIR="/Users/accusys/momentry/logs" # ── 1. PostgreSQL ── echo -e "${YELLOW}[1/7] PostgreSQL${NC}" PG_DATA="/Users/accusys/pgsql/data" PG_BIN="/Users/accusys/pgsql/18.3/bin" if $PG_BIN/pg_isready -q 2>/dev/null; then echo -e " ${GREEN}✅${NC} already running" else $PG_BIN/pg_ctl -D "$PG_DATA" -l "$LOG_DIR/pg.log" start 2>/dev/null sleep 2 $PG_BIN/pg_isready -q 2>/dev/null; check "started" fi # ── 2. Redis ── echo -e "${YELLOW}[2/7] Redis${NC}" if redis-cli ping 2>/dev/null | grep -q PONG; then echo -e " ${GREEN}✅${NC} already running" else brew services start redis 2>/dev/null || redis-server --daemonize yes 2>/dev/null sleep 2 redis-cli ping 2>/dev/null | grep -q PONG; check "started" fi # ── 3. Qdrant ── echo -e "${YELLOW}[3/7] Qdrant${NC}" QDRANT_BIN="${PROJECT_DIR}/services/qdrant/target/release/qdrant" QDRANT_STORAGE="/Users/accusys/momentry/qdrant_storage" if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 http://localhost:6333/healthz 2>/dev/null | grep -q 200; then echo -e " ${GREEN}✅${NC} already running" else mkdir -p "$QDRANT_STORAGE" nohup "$QDRANT_BIN" > "$LOG_DIR/qdrant.log" 2>&1 & for i in $(seq 1 15); do sleep 2 if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 2 http://localhost:6333/healthz 2>/dev/null | grep -q 200; then break fi done curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 http://localhost:6333/healthz 2>/dev/null | grep -q 200; check "started" fi # ── 4. Qdrant Collection ── echo -e "${YELLOW}[4/7] Qdrant Collection${NC}" source "$ENV_FILE" 2>/dev/null || true COLLECTION="${QDRANT_COLLECTION:-momentry_dev_rule1_v2}" EXISTS=$(curl -s "http://localhost:6333/collections/$COLLECTION" 2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('result',{}).get('status','not_found'))" 2>/dev/null) if [ "$EXISTS" = "not_found" ]; then curl -s -X PUT "http://localhost:6333/collections/$COLLECTION" \ -H "Content-Type: application/json" \ -d '{"vectors":{"size":768,"distance":"Cosine"}}' > /dev/null 2>&1 sleep 1 fi curl -s "http://localhost:6333/collections/$COLLECTION" 2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); s=d.get('result',{}).get('status','not_found'); assert s in ('green','ok'), f'unexpected status: {s}'" 2>/dev/null check "collection '$COLLECTION' ready" # ── 5. LLM (Gemma4 / llama.cpp) ── echo -e "${YELLOW}[5/7] LLM Server (Gemma4)${NC}" if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:8082/health 2>/dev/null | grep -q 200; then echo -e " ${GREEN}✅${NC} already running" else LLM_BIN="/Users/accusys/llama/bin/llama-server" LLM_MODEL="/Users/accusys/models/google_gemma-4-26B-A4B-it-Q5_K_M.gguf" nohup "$LLM_BIN" -m "$LLM_MODEL" --host 0.0.0.0 --port 8082 -ngl 99 -c 16384 --temp 0.1 --mlock --reasoning off > "$LOG_DIR/llama_server.log" 2>&1 & echo -e " ${YELLOW}⏳ loading model (~30s)...${NC}" for i in $(seq 1 30); do sleep 2 if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 2 http://localhost:8082/health 2>/dev/null | grep -q 200; then break fi done curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 http://localhost:8082/health 2>/dev/null | grep -q 200; check "started" fi # ── 6. Embedding Server ── echo -e "${YELLOW}[6/7] EmbeddingGemma${NC}" if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:11436/health 2>/dev/null | grep -q 200; then echo -e " ${GREEN}✅${NC} already running" else VENV_PYTHON="${PROJECT_DIR}/venv/bin/python" EMBED_SCRIPT="${PROJECT_DIR}/scripts/embeddinggemma_server.py" if [ ! -f "$VENV_PYTHON" ]; then VENV_PYTHON="/opt/homebrew/bin/python3.11" pip install flask 2>/dev/null || true fi nohup "$VENV_PYTHON" "$EMBED_SCRIPT" --port 11436 > "$LOG_DIR/embed.log" 2>&1 & sleep 5 curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:11436/health 2>/dev/null | grep -q 200; check "started" fi # ── 7. Playground Server ── echo -e "${YELLOW}[7/7] Playground API Server${NC}" if curl -s -o /dev/null -w "%{http_code}" -H "X-API-Key: muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69" --connect-timeout 5 http://127.0.0.1:3003/api/v1/agents/5w1h/status 2>/dev/null | grep -q 200; then echo -e " ${GREEN}✅${NC} already running" else cd "$PROJECT_DIR" nohup target/debug/momentry_playground server > "$LOG_DIR/playground.log" 2>&1 & sleep 4 curl -s -o /dev/null -w "%{http_code}" -H "X-API-Key: muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69" --connect-timeout 5 http://127.0.0.1:3003/api/v1/agents/5w1h/status 2>/dev/null | grep -q 200; check "started" fi echo "" if [ ${#FAILURES[@]} -eq 0 ]; then echo -e "${GREEN}====================================${NC}" echo -e "${GREEN} All services running${NC}" echo -e "${GREEN}====================================${NC}" else echo -e "${RED}====================================${NC}" echo -e "${RED} ${#FAILURES[@]} service(s) failed to start${NC}" echo -e "${RED}====================================${NC}" for f in "${FAILURES[@]}"; do echo -e " ${RED}❌${NC} $f"; done fi echo "" echo " Playground: http://127.0.0.1:3003" echo " LLM: http://127.0.0.1:8082" echo " Embedding: http://127.0.0.1:11436" echo " Qdrant: http://localhost:6333" echo " PostgreSQL: localhost:5432" echo " Redis: localhost:6379" echo ""