#!/usr/bin/env bash set -e RELAY_URL="https://agentifystudio.base44.app/functions/deviceRelay" INSTALL_DIR="$HOME/.agentify" WORKER_SCRIPT="$INSTALL_DIR/worker.sh" CONFIG_FILE="$INSTALL_DIR/config.json" mkdir -p "$INSTALL_DIR" echo "==================================================" echo " Agentify Desktop Companion — macOS Installer" echo "==================================================" EMBEDDED_TOKEN="__EMBEDDED_DEVICE_TOKEN__" if [ -n "$EMBEDDED_TOKEN" ] && [ "$EMBEDDED_TOKEN" != "__EMBEDDED_DEVICE_TOKEN__" ]; then DEVICE_TOKEN="$EMBEDDED_TOKEN" elif [ -n "$1" ]; then DEVICE_TOKEN="$1" elif [ -f "$CONFIG_FILE" ]; then DEVICE_TOKEN=$(grep -o '"device_token"[[:space:]]*:[[:space:]]*"[^"]*"' "$CONFIG_FILE" | head -n1 | cut -d'"' -f4) fi if [ -z "$DEVICE_TOKEN" ]; then printf "Paste your Device Token from Agentify -> Devices: " read -r DEVICE_TOKEN < /dev/tty fi if [ -z "$DEVICE_TOKEN" ]; then echo "Error: Device token is required." exit 1 fi cat > "$CONFIG_FILE" < "$WORKER_SCRIPT" <<'WORKER' #!/usr/bin/env bash CONFIG_FILE="$HOME/.agentify/config.json" RELAY_URL="https://agentifystudio.base44.app/functions/deviceRelay" TOKEN=$(grep -o '"device_token"[[:space:]]*:[[:space:]]*"[^"]*"' "$CONFIG_FILE" | head -n1 | cut -d'"' -f4) echo "Agentify macOS worker started. Polling relay..." while true; do OS_VER="macOS $(sw_vers -productVersion 2>/dev/null || echo '')" CORES=$(sysctl -n hw.ncpu 2>/dev/null || echo 4) MEM_BYTES=$(sysctl -n hw.memsize 2>/dev/null || echo 8589934592) MEM_GB=$((MEM_BYTES / 1073741824)) BATT=$(pmset -g batt 2>/dev/null | grep -Eo '[0-9]+%' | head -n1 | tr -d '%' || echo -1) [ -z "$BATT" ] && BATT=-1 PAYLOAD=$(cat </dev/null || echo '{"commands":[]}') python3 - "$RESP" "$TOKEN" "$RELAY_URL" <<'PY' 2>/dev/null import sys, json, subprocess, os, base64, urllib.request resp_raw, token, relay_url = sys.argv[1], sys.argv[2], sys.argv[3] try: data = json.loads(resp_raw) except Exception: sys.exit(0) for cmd in data.get("commands", []): cid = cmd.get("id") kind = cmd.get("kind") args = cmd.get("args") or {} res_str, err_str = None, None try: if kind == "shell": c = args.get("cmd") or args.get("command") or "" p = subprocess.run(["/bin/zsh", "-c", c], capture_output=True, text=True, timeout=25) if p.returncode == 0: res_str = p.stdout or "ok" else: err_str = (p.stderr or p.stdout or f"exit {p.returncode}") elif kind == "applescript": s = args.get("script") or "" p = subprocess.run(["osascript", "-e", s], capture_output=True, text=True, timeout=25) if p.returncode == 0: res_str = p.stdout or "ok" else: err_str = p.stderr or "AppleScript error" elif kind == "file_read": with open(os.path.expanduser(args.get("path", "")), "r") as f: res_str = f.read() elif kind == "file_write": with open(os.path.expanduser(args.get("path", "")), "w") as f: f.write(args.get("content", "")) res_str = "ok" elif kind == "file_list": p = os.path.expanduser(args.get("path", ".")) res_str = " ".join(os.listdir(p)) elif kind == "screen_capture": jpg = "/tmp/agentify_screen.jpg" subprocess.run(["screencapture", "-x", "-t", "jpg", jpg], check=True) with open(jpg, "rb") as f: b64 = base64.b64encode(f.read()).decode() res_str = f"data:image/jpeg;base64,{b64}" else: res_str = f"Executed {kind}" except Exception as e: err_str = str(e) body = json.dumps({"action": "complete", "device_token": token, "command_id": cid, "result": res_str, "error": err_str}).encode() req = urllib.request.Request(relay_url, data=body, headers={"Content-Type": "application/json"}, method="POST") try: urllib.request.urlopen(req, timeout=15) except Exception: pass PY sleep 2 done WORKER chmod +x "$WORKER_SCRIPT" echo "" echo "Installed Agentify worker to $WORKER_SCRIPT" echo "Starting Agentify worker in background..." nohup "$WORKER_SCRIPT" > "$INSTALL_DIR/worker.log" 2>&1 & echo "Agentify Desktop Companion is now online! (Logs: $INSTALL_DIR/worker.log)"