$(html_escape "$1")
$2
| 检查项 | 状态 | 说明 |
|---|---|---|
| %s | %s | %s |
装好之后回到终端,重新执行 ./local/start.sh(或双击 start.command)。
这个页面是本地静态文件,不会自动刷新,也不会重试。
brew install node@20,或到 nodejs.org 下载 macOS 安装包';;
*) NODE_HINT='用 nvm 安装(nvm install 20),或发行版包管理器装 nodejs 20+';;
esac
# ---------- 自检页(纯静态,无 node 也能打开) ----------
html_escape() {
printf '%s' "$1" | sed -e 's/&/\&/g' -e 's/\</g' -e 's/>/\>/g'
}
open_target() {
if command -v open >/dev/null 2>&1; then open "$1" >/dev/null 2>&1 || true
elif command -v xdg-open >/dev/null 2>&1; then xdg-open "$1" >/dev/null 2>&1 || true
fi
}
setup_head() {
# $1 标题 $2 建议(可含 HTML)
cat > "$RUNTIME/setup.html" <$2
| 检查项 | 状态 | 说明 |
|---|---|---|
| %s | %s | %s |
装好之后回到终端,重新执行 ./local/start.sh(或双击 start.command)。
这个页面是本地静态文件,不会自动刷新,也不会重试。
npm。'
return 1
fi
say '首次运行:安装 Node 依赖(需要联网,通常 1~3 分钟)'
for d in $NEED; do
say " npm install → ${d#$ROOT/}"
if [ -n "$NPM_REGISTRY" ]; then
( cd "$d" && npm install --no-audit --no-fund --omit=dev --registry "$NPM_REGISTRY" ) || return 1
else
( cd "$d" && npm install --no-audit --no-fund --omit=dev ) || return 1
fi
done
return 0
}
# ---------- 子进程 ----------
MCP_PIDS=''
# 四个服务对接参数的方式不一样,别混用:
# image_task / video_task / web_search → 认 --http --host --port
# mcp_asr → 只认 PORT 环境变量(它的 argv 不解析)
start_mcp_flags() {
dir=$1; mport=$2; tag=$3; shift 3
( cd "$ROOT/mcp/$dir" && exec node "$@" --host "$HOST_ADDR" --port "$mport" >>"$RUNTIME/mcp-$tag.log" 2>&1 ) &
MCP_PIDS="$MCP_PIDS $!"
}
start_mcp_env() {
dir=$1; mport=$2; tag=$3; shift 3
( cd "$ROOT/mcp/$dir" && exec env PORT="$mport" node "$@" >>"$RUNTIME/mcp-$tag.log" 2>&1 ) &
MCP_PIDS="$MCP_PIDS $!"
}
CORE_PID=''
STOPPING=0
kill_tree() {
pid=$1
[ -n "$pid" ] || return 0
if command -v pgrep >/dev/null 2>&1; then
for c in $(pgrep -P "$pid" 2>/dev/null); do kill_tree "$c"; done
fi
kill "$pid" 2>/dev/null || true
}
do_cleanup() {
for p in $CORE_PID $MCP_PIDS; do kill_tree "$p"; done
CORE_PID=''
MCP_PIDS=''
}
on_signal() { STOPPING=1; }
trap 'on_signal' INT TERM HUP
trap 'do_cleanup' EXIT
health() {
if command -v curl >/dev/null 2>&1; then
curl -fsS --max-time 2 "http://$HOST_ADDR:$PORT/api/health" >/dev/null 2>&1
else
node -e 'fetch("http://127.0.0.1:"+process.argv[1]+"/api/health").then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))' "$PORT" >/dev/null 2>&1
fi
}
port_open() {
node -e 'const s=require("net").connect(Number(process.argv[1]),"127.0.0.1");s.setTimeout(800);s.on("connect",()=>{s.destroy();process.exit(0)});s.on("error",()=>process.exit(1));s.on("timeout",()=>{s.destroy();process.exit(1)})' "$1" >/dev/null 2>&1
}
# MCP 起不来是静默降级(core 只会发现不到工具),所以启动后主动点名报告。
check_mcp() {
if ! port_open "$1"; then
warn "MCP $2 没起来(端口 $1)——日志:local/.runtime/mcp-$2.log"
fi
}
start_core() {
( cd "$ROOT/app" && exec env \
PORT="$PORT" \
HOST="$HOST_ADDR" \
AGENT_CONFIG="$CONFIG" \
AGENT_FILE_SCOPE=workspace \
ASR_URL="$ASR_URL" \
PYTHONPATH="$ROOT/skills/deps" \
node server/server.js >>"$LOG" 2>&1 ) &
CORE_PID=$!
}
open_browser() {
url="http://$HOST_ADDR:$PORT"
if command -v open >/dev/null 2>&1; then open "$url" >/dev/null 2>&1 || true
elif command -v xdg-open >/dev/null 2>&1; then xdg-open "$url" >/dev/null 2>&1 || true
fi
}
# ============ 主流程 ============
if [ "$NODE_OK" != 1 ]; then
setup_head '启动前自检未通过' "没有找到可用的 Node.js 20+,服务起不来。按下面提示装好后,重新执行 ./local/start.sh。"
setup_rows
setup_close
exit 1
fi
if [ "$PORT_FREE" != 1 ]; then
setup_head '端口被占用' "端口 $PORT 上已经有程序在监听。关掉它,或者换个端口启动:PORT=8091 ./local/start.sh(记得把设置页里的访问地址一起改)。"
setup_rows
setup_close
exit 1
fi
if [ ! -f "$CONFIG" ]; then
say '首次运行:需要选一个 AI 的工作目录'
WS=$(choose_workspace)
generate_config "$WS"
else
say "使用已有配置:$CONFIG"
fi
if ! ensure_deps; then
setup_head "${SETUP_TITLE:-依赖安装失败}" "${SETUP_HINT:-安装 app/ 的 Node 依赖时出错了,多半是网络问题。}"
setup_rows
setup_close
printf '\n最近日志:\n'
tail -n 20 "$LOG" 2>/dev/null || true
exit 1
fi
link_skills
# 语音:ffmpeg 在就启用(ASR_URL 由 core 直接读 env,不是配置),不在就不给入口。
if [ "$FF_OK" = 1 ]; then ASR_URL="http://$HOST_ADDR:$MCP_ASR_PORT"; else ASR_URL=''; fi
say '启动 MCP 服务…'
start_mcp_flags web_search "$MCP_WEB_PORT" web server.js --http
start_mcp_flags image_task "$MCP_IMAGE_PORT" image src/mcp-server.js --http
start_mcp_flags video_task "$MCP_VIDEO_PORT" video src/mcp-server.js --http
start_mcp_env asr "$MCP_ASR_PORT" asr src/server.js
say "启动 agent-core($HOST_ADDR:$PORT)…"
FIRST=1
FAILS=0
while :; do
start_core
i=0
while [ "$i" -lt 60 ]; do
health && break
kill -0 "$CORE_PID" 2>/dev/null || break
sleep 1
i=$((i + 1))
done
if health; then
if [ "$FIRST" = 1 ]; then
FIRST=0
open_browser
printf '\n'
say "已启动:http://$HOST_ADDR:$PORT"
sleep 1
check_mcp "$MCP_WEB_PORT" web
check_mcp "$MCP_IMAGE_PORT" image
check_mcp "$MCP_VIDEO_PORT" video
check_mcp "$MCP_ASR_PORT" asr
if [ "$FF_OK" = 1 ]; then
say '语音:可用(ffmpeg 已就绪;还需在设置页给 asr 填上游地址与模型)'
else
say '语音:不可用(未安装 ffmpeg,麦克风入口会自动隐藏)'
fi
if [ "$PY_OK" = 1 ]; then
say '技能:已启用(python3 就绪)'
else
say '技能:已关闭(未安装 python3;装好后重跑本脚本即可)'
fi
printf '\n首次使用:左下角「用户区」→ 设置 → 模型池,添加平台和 API Key,然后点「重启」。\n'
printf '关闭:在本终端按 Ctrl-C(会一起停掉子进程)。\n\n'
fi
else
warn 'agent-core 没能起来,最近日志:'
tail -n 30 "$LOG" 2>/dev/null || true
kill_tree "$CORE_PID"
CORE_PID=''
exit 1
fi
wait "$CORE_PID" 2>/dev/null
code=$?
[ "$STOPPING" = 1 ] && break
# 设置页的「重启」就是 exit 0;非 0 = 崩溃。连续崩 5 次就停下来别刷屏(典型是配置写坏了)。
if [ "$code" = 0 ]; then FAILS=0; else FAILS=$((FAILS + 1)); fi
if [ "$FAILS" -ge 5 ]; then
warn "agent-core 连续 $FAILS 次异常退出,不再自动重启。最近日志:"
tail -n 30 "$LOG" 2>/dev/null || true
exit 1
fi
warn 'agent-core 已退出,1 秒后自动拉起(设置页点「重启」走的也是这条路)'
sleep 1
done
say '已停止。'