AnyBot/Makefile

214 lines
11 KiB
Makefile
Raw Normal View History

# AnyBot — build and install from source. make help lists everything.
#
# git clone https://code.30cm.net/daniel.w/AnyBot.git && cd AnyBot
# make deps everything the build needs (system packages, Rust) — once
# make install build and install: ~/Applications/AnyBot.app on a Mac, ~/.local/bin on Linux
# make run open it without installing
#
# The one-step version of all of this is ./install.sh (README.md).
# Rust installed by rustup lives in ~/.cargo/bin, which a fresh shell may not have on PATH yet.
export PATH := $(HOME)/.cargo/bin:$(PATH)
CARGO ?= cargo
UNAME := $(shell uname -s)
# The version, as the program shows it: v<number in Cargo.toml>-<commit>, with "-dirty" when what
# goes into the program has uncommitted changes (the same paths as INPUTS in build.rs). Outside git
# it is just v<number>. ANYBOT_GIT_COMMIT is handed to cargo, so the program built here and the
# dist file names say the same thing (build.rs takes it instead of asking git again).
VERSION_NUMBER := $(shell sed -n 's/^version *= *"\(.*\)"/\1/p' Cargo.toml | head -n 1)
GIT_COMMIT := $(shell git rev-parse --short=7 HEAD 2>/dev/null)$(shell git status --porcelain --untracked-files=no -- src box fonts icons mobile Cargo.toml Cargo.lock build.rs 2>/dev/null | grep -q . && echo -dirty)
export ANYBOT_GIT_COMMIT ?= $(GIT_COMMIT)
VERSION ?= v$(VERSION_NUMBER)$(if $(ANYBOT_GIT_COMMIT),-$(ANYBOT_GIT_COMMIT))
TARGET := $(or $(CARGO_TARGET_DIR),target)
APPDIR ?= $(HOME)/Applications
BIN := $(TARGET)/release/anybot
RELAY := $(if $(CARGO_TARGET_DIR),$(CARGO_TARGET_DIR),relay/target)/release/anybot-relay
# make update PULL=0: install this checkout as it is, without pulling the newest first.
PULL ?= 1
.PHONY: all build app relay run serve setup relay-run test deps doctor install uninstall update update-app update-hands update-server update-box server-logs hands server dist clean help check-rust lint-sh sign-setup
all: build
help:
@echo "AnyBot $(VERSION)"
@echo ""
@echo " make 建置 AnyBot 和帳號伺服器(正式版)"
@echo " make run 直接打開 AnyBot(不安裝)"
@echo " make install 安裝:Mac → $(APPDIR)/AnyBot.app;Linux → ~/.local/bin/anybot 和選單"
@echo " make uninstall 移除安裝的程式(你的 Bot 和聊天不會刪)"
@echo ""
@echo " make update 更新這台裝的 AnyBot(伺服器、App、手腳,自己認)"
@echo " make update-app 只更新桌面 App(有背景服務也一起重新啟動)"
@echo " make update-hands 只更新 Linux 手腳(換新程式、重新啟動背景服務)"
@echo " make update-server 只更新伺服器(帳號伺服器 + 大腦,Docker)"
@echo " 加 PULL=0:不拉新版,裝現在這份原始碼"
@echo " make update-box 請這台的 AnyBot(大腦)現在更新「Bot 的電腦」=設定 › 電腦 › 更新"
@echo " make migrate 把帳號伺服器搬到大腦那台(體感提速的根治法;逐步引導、可逆)"
@echo " (先存快照、檔案和登入保留;用它帶的版本和它設定的 Docker 主機)"
@echo " 要新版的電腦:先 make update(伺服器 make update-server),它會自己換新"
@echo ""
@echo " make deps 裝好建置需要的東西:系統套件和 Rust(Linux 會問 sudo 密碼)"
@echo " make doctor 檢查這台缺什麼"
@echo " make test 跑測試"
@echo " make lint-sh 檢查 shell 腳本(語法、中文標點黏變數)"
@echo " make lint 跟 CI 一樣的 clippy 檢查(有警告就失敗)"
@echo ""
@echo " make serve 不開視窗執行(伺服器、一直開著的電腦)"
@echo " make setup 把這台接到你的 AnyBot(終端機裡的引導畫面)"
@echo " make hands Linux:裝好虛擬螢幕等,再接成大腦的手腳"
@echo " make relay-run 在這台跑帳號伺服器(埠 8080,資料在 ./relay-data)"
@echo " make server 在伺服器上架好帳號伺服器 + 大腦 + HTTPS(Docker);再跑一次可以改設定"
@echo " make server-logs 看伺服器上大腦的紀錄"
@echo " 伺服器的設定只在 deploy/server/.env;改了之後 make update-server"
@echo ""
@echo " make dist 打包發行檔到 dist/(Mac:.app 的 zip;Linux:三個 tar.gz)"
@echo " make clean 清掉建置結果"
build: app relay
check-rust:
@command -v $(CARGO) >/dev/null 2>&1 || { echo; echo " ✗ 找不到 Rust(cargo)。先執行:make deps"; echo; exit 1; }
@set -- $$(rustc --version | sed -n 's/^rustc \([0-9][0-9]*\)\.\([0-9][0-9]*\).*/\1 \2/p'); { [ "$${1:-0}" -gt 1 ] || { [ "$${1:-0}" -eq 1 ] && [ "$${2:-0}" -ge 95 ]; }; } || { echo; echo " ✗ Rust 太舊($$(rustc --version)),要 1.95 以上。執行:rustup update stable(或 make deps)"; echo; exit 1; }
app: check-rust
$(CARGO) build --release --bin anybot
relay: check-rust
cd relay && $(CARGO) build --release
# 把帳號伺服器(relay)搬到大腦那台:逐步引導,可逆,舊伺服器不停不刪。
migrate:
sh deploy/migrate-relay.sh
run: app
$(BIN)
serve: app
$(BIN) --serve
setup: app
$(BIN) setup
relay-run: relay
$(RELAY) --port 8080 --data relay-data
test: check-rust
$(CARGO) test --bin anybot
cd relay && $(CARGO) test
lint-sh:
@sh deploy/ci/lint-sh.sh
# The same clippy check CI runs: no warnings allowed (clippy.toml has AnyBot's own rules).
lint: check-rust
$(CARGO) clippy --bin anybot --all-targets -- -D warnings
deps:
sh ./install.sh deps
doctor:
@echo "AnyBot $(VERSION) · $(UNAME) $$(uname -m)"
@command -v git >/dev/null && echo " ✓ git" || echo " ✗ git:make deps"
@command -v cc >/dev/null && echo " ✓ C 編譯器" || echo " ✗ C 編譯器:make deps"
@command -v $(CARGO) >/dev/null && echo " ✓ $$(rustc --version)" || echo " ✗ Rust:make deps"
ifeq ($(UNAME),Linux)
@pkg-config --exists alsa 2>/dev/null && echo " ✓ ALSA(libasound2-dev)" || echo " ✗ ALSA 開發套件:make deps"
@command -v docker >/dev/null && echo " ✓ $$(docker --version)" || echo " · 沒有 Docker(只有伺服器和 Bot 自己的電腦需要;make server 會自動裝)"
endif
ifeq ($(UNAME),Darwin)
@xcode-select -p >/dev/null 2>&1 && echo " ✓ Xcode Command Line Tools" || echo " ✗ Xcode Command Line Tools:make deps"
endif
@test -x $(BIN) && echo " ✓ 已建置 $(BIN)" || echo " · 還沒建置:make"
Bots do a planned sequence in one step; the box's screen settles by what it shows; no login is ever the only copy - computer_use_batch: up to 8 planned actions run in one docker exec (steps chained with && and a done-marker each), the first failure stops the rest, one picture comes back at the end. Typed text rides inside the script quoted (one stdin for the chain). The prompt tells the model when to use it (a form whose fields are all visible) and when not to (step 2 needs step 1's screen). - The box waits for the screen to stop changing, not the clock: two matching small probes (~60 ms each) mean settled — ~160 ms on a still screen where every step paid a fixed 500 ms, and a page mid-load is waited for (up to ~0.9 s) instead of captured mid-change. The picture is JPEG, and the a11y read runs in parallel with the capture (look ~307 ms; two steps with pictures 1102 ms). (SETTLE_SH is a group command: dash needs ';' after it — found by box_screen_is_exact.) - The home volume (every bot's logins) is never one command from loss: snapshots keep generations (last 7 days, then one per week; the old single snapshot migrates by file date), Reset and Restore save the running box first and refuse to wipe if saving fails, a box built on an empty volume re-fills itself from the newest snapshot, and a sleeping holder container keeps the volume attached so `docker volume prune` can't take it. make doctor shows volume and snapshot state; the 電腦 card shows the box's average step times and says resets save first. - box_batch_in_one_round_trip (ignored): three typed steps, ASCII and Chinese, land in one trip. On the real box after upgrading v3→v5: box_update_keeps_files (files survive, snapshot 2 代), box_numbers_are_exact, box_screen_is_exact, box_end_to_end, box_traffic_via_mac pass; 155 unit tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 01:47:37 +00:00
@command -v docker >/dev/null 2>&1 && { docker volume inspect anybot-box-home >/dev/null 2>&1 && echo " ✓ Bot 的電腦的資料碟 anybot-box-home(檔案和登入都在裡面)" || echo " · 沒有 anybot-box-home 資料碟(Bot 的電腦還沒建立過,或被清除過——有快照會在下次建立時自動還原)"; } || true
@d="$${HOME}/Library/Application Support/AnyBot/box/snapshots"; [ -d "$$d" ] || d="$${HOME}/.local/share/AnyBot/box/snapshots"; [ -d "$$d" ] && echo " ✓ 快照 $$(ls "$$d" 2>/dev/null | grep -c '^snapshot-') 份(留最近 7 天+之前每週一份;重設前也會先存)" || echo " · 還沒有快照(Bot 的電腦每天自動存,重設前也會先存)"
install: app
ifeq ($(UNAME),Darwin)
KEEP_APP=1 sh deploy/mac/bundle.sh $(VERSION)
mkdir -p "$(APPDIR)"
rm -rf "$(APPDIR)/AnyBot.app"
mv dist/AnyBot.app "$(APPDIR)/AnyBot.app"
mkdir -p "$(HOME)/.local/bin"
ln -sfn "$(APPDIR)/AnyBot.app/Contents/MacOS/AnyBot" "$(HOME)/.local/bin/anybot"
@echo "已安裝 $(APPDIR)/AnyBot.app(第一次打開:按右鍵 › 打開)"
else ifneq ($(findstring MINGW,$(UNAME)),)
powershell.exe -NoProfile -ExecutionPolicy Bypass -File install.ps1 app
else ifeq ($(OS),Windows_NT)
powershell.exe -NoProfile -ExecutionPolicy Bypass -File install.ps1 app
else
sh deploy/linux/install.sh
endif
uninstall:
ifeq ($(UNAME),Darwin)
rm -rf "$(APPDIR)/AnyBot.app"
else
rm -f "$(HOME)/.local/bin/anybot" "$(HOME)/.local/share/applications/anybot.desktop"
rm -f "$(HOME)/.local/share/icons/hicolor/scalable/apps/anybot.svg" "$(HOME)/.local/share/icons/hicolor/256x256/apps/anybot.png" "$(HOME)/.local/share/icons/hicolor/128x128/apps/anybot.png"
endif
@echo "已移除程式。你的 Bot、聊天和設定還在(要全部刪掉請看 docs/install.md)。"
hands: app
sh deploy/linux/hands.sh --deps
# Pull, rebuild, swap the installed program, restart what runs in the background (deploy/update.sh).
update:
@PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh auto
update-app:
@PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh app
update-hands:
@PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh hands
update-server:
@PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh server
# The box (Bot 的電腦): ask the AnyBot installed here (the brain) to update its computer now —
# the same as 設定 › 電腦 › 更新: a snapshot first, queued while a bot is using it, files and
# logins kept. `anybot --update-box` asks the AnyBot running on its data folder, or with none
# running does it itself and waits. It is always that AnyBot's own box version, on the Docker
# host it is set to use (Lima, ssh://, the server's). This target used to build box/ from this
# checkout beside it with plain `docker`: another version than the brain's (which put its own
# back within a minute), and often another Docker than the brain's. A newer box comes with a
# newer AnyBot: make update / make update-server, and it updates the box by itself.
update-box:
@if [ -f deploy/server/.env ]; then \
s=$$(sed -n "s/^DOCKER_SOCKET=['\"]\{0,1\}\([^'\"]*\)['\"]\{0,1\}\$$/\1/p" deploy/server/.env | tail -n 1); \
傻瓜安裝:伺服器 sh AnyBot/install.sh server 再 make update-server 就好(自動裝 Docker、自己選網址——Tailscale 用 http、區網用自己的憑證、有 Tailscale 憑證用它的名字——自己產生邀請碼和所有金鑰,中間不問問題,沒有終端機也跑得完);不用先開帳號:大腦管理員(compose 的 brains,anybot --brains)看到有人用邀請碼註冊,就替那個帳號開一個雲端大腦 anybot-brain-帳號(自己的資料卷、帳號伺服器發的登入,伺服器不存密碼),停了叫醒、重開機回來;第一個註冊的是擁有者,只有它的大腦有伺服器上的 Docker(Bot 的電腦),其他人的大腦拿掉 NET_RAW、限記憶體、CPU、程序數,最多 20 個;更新後換成新版時新的先在舊的旁邊建好,Docker 拒絕的話舊的照常在跑,舊的確定停了才換、換不成放回舊的,管理員中途被停掉下次接回來,一直失敗的帳號隔 1、2、4…分鐘最多半小時再試。電腦登入有雲端大腦的伺服器時,先等它上線(最多約一分半)再當它的螢幕和手腳,不把自己當大腦。帳號伺服器只相信 Caddy(帶暗號)說的請求來源,大腦自己寫 X-Forwarded-For 繞不過註冊、登入的次數限制;舊的 Caddyfile 更新時自動補上那一行(舊版 mawk 也行),Caddy 檢查過才換上。 順手修:Anthropic 回「錯誤 400:Invalid signature in thinking block」時這一輪不再停下來:舊的思考內容對不上就請它丟掉(thinking-binding-controls)或拿掉重送,聊天紀錄裡也一起丟掉,之後不再撞到;手機帳號登入的一個偶發失敗的測試。 功能地圖新增 server 區(models 和地圖的 README 跟著改),FEATURES、README.md、docs/multi-device.md 也改了;計畫在 docs/plan-2026-11-install.md(S8 App 信任伺服器的憑證、S9 伺服器上裝手腳還沒做)。 已驗證: cargo test(902)、relay cargo test(29)、bash -n;在 nobledesktop 上的隔離測試機(Ubuntu 24.04、全新安裝):一般使用者 install.sh server+make update-server 不給任何設定就跑完、兩個帳號註冊後各自有雲端大腦且在線、只有擁有者的大腦連得到 Docker、偽造 X-Forwarded-For 被擋、ANYBOT_BRAINS_SKIP 停掉指定帳號、重開機後全部回來、舊版伺服器(有自己的大腦)更新後保留原本的大腦並補上 Caddyfile、程式更新只換一次、只改 .env 不換、Docker 拒絕建立時舊的照常在跑、這次的更新(大腦管理員就緒、amy 換成新版在線、沒有殘留);Debian 10 的 mawk 1.3.3 補 Caddyfile;Anthropic 真的帳號:舊思考內容對不上時這一輪照常回答、丟掉的有回報;七輪獨立審查,最後兩輪沒有 P0/P1。 未驗證: 公開網址(有網域、DNS)那條路(要真的網域);Mac App 用帳號登入後等雲端大腦再當螢幕只跑了測試(ignored 的實機測試要真的伺服器);更新中途按 Ctrl-C 把管理員開回來只讀過程式沒實際按。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-23 00:09:02 +00:00
u=$$(sed -n "s/^ANYBOT_USER=['\"]\{0,1\}\([^'\"]*\)['\"]\{0,1\}\$$/\1/p" deploy/server/.env | tail -n 1); \
if [ -n "$$u" ]; then \
cd deploy/server && env $${s:+DOCKER_HOST=unix://$$s} docker compose exec -T anybot anybot --update-box; \
else \
ids=$$(env $${s:+DOCKER_HOST=unix://$$s} docker ps -q --filter label=anybot.brain.docker=1); \
[ -n "$$ids" ] || { echo " 沒有雲端大腦有 Bot 的電腦(只有擁有者的有)"; exit 0; }; \
for c in $$ids; do env $${s:+DOCKER_HOST=unix://$$s} docker exec $$c anybot --update-box || exit 1; done; \
fi; \
else \
b="$(HOME)/.local/bin/anybot"; [ -x "$$b" ] || b="$(APPDIR)/AnyBot.app/Contents/MacOS/AnyBot"; \
[ -x "$$b" ] || { echo " ✗ 這台還沒裝 AnyBot(make install),沒有 Bot 的電腦可以更新"; exit 1; }; \
"$$b" --update-box; \
fi || { echo " ✗ 沒有更新。上面說不認得「--update-box」的話,裝好的 AnyBot 是舊版:先 make update(伺服器 make update-server)"; exit 1; }
# The author, once: sign every commit so installs can check where an update came from.
sign-setup:
@sh deploy/sign-setup.sh
server-logs:
@bash deploy/server/setup.sh --logs
server:
sh ./install.sh server
dist: build
ifeq ($(UNAME),Darwin)
sh deploy/mac/bundle.sh $(VERSION)
else
sh deploy/ci/package-linux.sh $(VERSION)
endif
clean:
$(CARGO) clean
cd relay && $(CARGO) clean
rm -rf dist