#!/usr/bin/env bash set -euo pipefail ROOT="$(cd "$(dirname "$0")" && pwd)" PORT="${GOOSED_PORT:-18006}" LOG="${ROOT}/goosed.local.log" PID_FILE="${ROOT}/.goosed.pid" if [[ -f "${ROOT}/port.conf" ]]; then # shellcheck disable=SC1091 source "${ROOT}/port.conf" PORT="${GOOSED_PORT:-${PORT}}" fi if [[ -f "${ROOT}/.env.local" ]]; then set -a # shellcheck disable=SC1091 source "${ROOT}/.env.local" set +a fi export GOOSE_PORT="${PORT}" export GOOSE_HOST="${GOOSE_HOST:-127.0.0.1}" export GOOSE_SERVER__SECRET_KEY="${GOOSE_SERVER__SECRET_KEY:-local-dev-secret}" kill_port() { if command -v lsof >/dev/null 2>&1; then local pids pids="$(lsof -ti TCP:"${PORT}" -s TCP:LISTEN 2>/dev/null || true)" if [[ -n "${pids}" ]]; then echo "==> 停止占用 :${PORT} 的进程: ${pids}" kill ${pids} 2>/dev/null || true sleep 1 kill -9 ${pids} 2>/dev/null || true fi fi } ensure_goosed() { if [[ -x "${ROOT}/target/release/goosed" ]]; then echo "${ROOT}/target/release/goosed" return fi if [[ -x "${ROOT}/target/debug/goosed" ]]; then echo "${ROOT}/target/debug/goosed" return fi if ! command -v cargo >/dev/null 2>&1; then echo "错误: 未找到 cargo。请先安装 Rust: https://rustup.rs/" >&2 exit 1 fi echo "==> 编译 goosed(首次较慢)..." >&2 (cd "${ROOT}" && cargo build -p goose-server --bin goosed) echo "${ROOT}/target/debug/goosed" } kill_port if [[ -f "${PID_FILE}" ]]; then old_pid="$(<"${PID_FILE}")" if kill -0 "${old_pid}" 2>/dev/null; then echo "==> 停止旧 goosed PID ${old_pid}" kill "${old_pid}" 2>/dev/null || true sleep 1 fi rm -f "${PID_FILE}" fi GOOSED_BIN="$(ensure_goosed)" echo "==> 启动 goosed @ https://${GOOSE_HOST}:${PORT}" nohup env \ GOOSE_PORT="${PORT}" \ GOOSE_HOST="${GOOSE_HOST}" \ GOOSE_SERVER__SECRET_KEY="${GOOSE_SERVER__SECRET_KEY}" \ GOOSE_CODING_ROUTER="${GOOSE_CODING_ROUTER:-}" \ GOOSE_AIDER_BIN="${GOOSE_AIDER_BIN:-}" \ "${GOOSED_BIN}" agent >>"${LOG}" 2>&1 & echo $! >"${PID_FILE}" disown started=false for _ in 1 2 3 4 5 6 8 10 12 15; do sleep 1 if ! kill -0 "$(<"${PID_FILE}")" 2>/dev/null; then break fi if curl -sk --connect-timeout 2 --max-time 3 "https://${GOOSE_HOST}:${PORT}/status" 2>/dev/null | grep -q '^ok$'; then started=true break fi done if [[ "${started}" == true ]]; then echo "✅ goosed 已启动" echo " API: https://${GOOSE_HOST}:${PORT}" echo " 日志: ${LOG}" echo "" echo "启动桌面 UI(连接外部 goosed):" echo " cd ui/desktop && GOOSE_EXTERNAL_BACKEND=true GOOSE_HOST=${GOOSE_HOST} GOOSE_PORT=${PORT} GOOSE_SERVER__SECRET_KEY=${GOOSE_SERVER__SECRET_KEY} pnpm run start-gui" else echo "❌ goosed 启动失败,查看日志: ${LOG}" >&2 tail -n 40 "${LOG}" >&2 || true exit 1 fi