Files
u-claw/portable/lib/maintain.sh
zheng b076815171
Some checks failed
Tests / test (push) Has been cancelled
feat: 海外化改造(新加坡市场)—— 阶段 0-3
按 U-Claw-海外化改造方案.md 与 范围决策记录.md 实施。这是 fork,不回上游:
海外版删掉的正是上游的中国市场默认值。

阶段 0 地基
- 下载源全部改国际:脚本/CI 61 处 + lockfile 880 条 npmmirror URL 归零
  (lockfile 那 880 条是 npm 的 resolved 字段,脚本层参数化根本绕不过它)
- 移除 install.ps1 里三个第三方 GitHub 加速代理,bundle 改直连 + SHA256 校验
  (原来只检查"文件大于 1MB"就解压运行)
- 技能内容与分发分离:skills/manifest.json 单一来源,install.sh 1170→658 行、
  install.ps1 721→546 行,两者技能内容归零
  实测原来是三份不一致:skills-cn 完整、install.sh 约 40%、install.ps1 约 17%,
  且 7 个通用技能只有 U 盘版有 —— 一键安装的用户一个能用的技能都没有
- Node 版本三种(v22.14/16/22.1)统一,新建 NODE_VERSION 单一来源
- Config 页三份合一。portable/Config.html 用根相对路径调 API 却只从 file:// 打开,
  保存功能已静默失效两个月;现缩为 120 行重定向壳
- 测试接入 CI(此前 node --test 无人运行,所有断言形同虚设)

阶段 1 双语可用
- 浏览器侧 i18n:JSON 为源、生成经典 script(file:// 下 fetch 本地 JSON 被拦)
  语言跟盘走不跟机器走:启动器写 data/.openclaw/locale.js
- 8 处硬编码 lang="zh-CN" 归零,data-i18n 覆盖 213 处,词条 en/zh 各 279 条
- B3 单框 Key:12 张模型卡 → 一个输入框,前缀识别 provider,
  服务端 /api/test-key 发 1-token 请求实测,错误映射成人话
  Key 填错到得知:从"直到对话失败"降到 ≤1 秒
- 区域格式 SG:DD/MM/YYYY、12 小时、S$、Asia/Singapore
  (ICU 在 en-SG 下把 SGD 渲染成裸 $,与美元无法区分,故自行拼 S$)
- README 内容分叉而非翻译,§1.3 证据清单逐条清零

阶段 2 降门槛
- 启动逻辑上移 lib/start.mjs:Windows-Start.bat 220→28 行、
  Mac-Start.command 235→33 行
  修掉 Mac 侧两个 bug:控制台端口硬编码 18788(回落时打开死页)、
  微信插件从未在 Mac 上安装
- U 盘根目录 23 → 3 个可点文件,其余进 advanced/
- 首启向导:语言 → 用途(7 角色,manifest 驱动)→ 密钥,答过不再问
- 三档界面,Simple 档隐藏一切技术名词
- 自动自愈:启动失败先自查自修,修不好导出脱敏诊断包
  (Doctor 从"用户要知道去点的工具"变成后台机制)

阶段 3 技能库
- 19 个英文技能,planned 归零。sg-weather / sg-transport 的端点均实测过
- SkillHub 从 56 张手写第三方卡片改为 manifest 生成:703→125 行,中文归零

其他
- origin.json 收拢所有运行时地址,tests/origin.test.mjs 保证迁移不会漏
- portable/ 下用户可见中文归零(由断言保证)
- 82 项测试

未验证(本机无 Windows / 无 pwsh):
- install.ps1、setup.ps1 约 210 行改动从未经 PowerShell 解析器
- 完整启动路径仅在假 node + 假 openclaw 上冒烟
- 8 个 .bat 的盘根推导仅静态断言
详见 U盘实测清单.md

受阻:
- 隐藏黑窗口 —— 需代码签名证书(.vbs 已被 Windows 弃用,替代方案都要签名)
- 场景卡 —— OpenClaw 上游 Dashboard 无预填 prompt 接口
- 官网 36 条 —— 上游 2026-04-14 拆到私有仓库,无权限
2026-08-17 18:37:49 +08:00

470 lines
16 KiB
Bash

#!/bin/bash
# ============================================================
# U-Claw Maintenance Functions (shared by Mac menus)
# Requires caller to set: $UCLAW_DIR, $DATA_DIR, $STATE_DIR,
# $CONFIG_PATH, $NODE_BIN, $NPM_BIN, $CORE_DIR,
# color vars ($RED $GREEN $YELLOW $CYAN $WHITE $NC $BOLD $DIM),
# and run_oc() function.
# ============================================================
LOG_DIR="$DATA_DIR/logs"
BACKUP_DIR="$DATA_DIR/backups"
DEFAULT_CONFIG="$UCLAW_DIR/default-config.json"
# Detect install mode: installed (~/.uclaw), usb (/Volumes or /media), portable (other)
detect_install_mode() {
if echo "$UCLAW_DIR" | grep -q "/.uclaw"; then
echo "installed"
elif echo "$UCLAW_DIR" | grep -qE "/Volumes/|/media/|/mnt/"; then
echo "usb"
else
echo "portable"
fi
}
# ── [9] Kill residual gateway processes ──────────────────────
do_kill_gateway() {
echo ""
echo -e " ${CYAN}${BOLD}--- Stop leftover processes ---${NC}"
echo ""
local FOUND=0
# Method 1: Check ports 18789-18799
for PORT in $(seq 18789 18799); do
local PIDS=""
if command -v lsof >/dev/null 2>&1; then
PIDS=$(lsof -ti :$PORT 2>/dev/null)
elif command -v ss >/dev/null 2>&1; then
PIDS=$(ss -tlnp 2>/dev/null | grep ":$PORT " | sed -n 's/.*pid=\([0-9]*\).*/\1/p')
fi
if [ -n "$PIDS" ]; then
for PID in $PIDS; do
local CMD=$(ps -p "$PID" -o comm= 2>/dev/null || echo "unknown")
echo -e " ${YELLOW}Port $PORT: PID $PID ($CMD)${NC}"
FOUND=1
done
fi
done
# Method 2: Check for openclaw.mjs processes
local OC_PIDS=$(pgrep -f "openclaw.mjs gateway" 2>/dev/null)
if [ -n "$OC_PIDS" ]; then
for PID in $OC_PIDS; do
echo -e " ${YELLOW}OpenClaw gateway: PID $PID${NC}"
FOUND=1
done
fi
if [ "$FOUND" = "0" ]; then
echo -e " ${GREEN}Nothing left running${NC}"
return
fi
echo ""
read -p " Stop these? (y/N): " CONFIRM
if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; then
echo -e " ${YELLOW}Cancelled${NC}"
return
fi
# Kill port processes
for PORT in $(seq 18789 18799); do
if command -v lsof >/dev/null 2>&1; then
lsof -ti :$PORT 2>/dev/null | xargs kill 2>/dev/null
elif command -v ss >/dev/null 2>&1; then
ss -tlnp 2>/dev/null | grep ":$PORT " | sed -n 's/.*pid=\([0-9]*\).*/\1/p' | xargs kill 2>/dev/null
fi
done
# Kill openclaw.mjs processes
pgrep -f "openclaw.mjs gateway" 2>/dev/null | xargs kill 2>/dev/null
sleep 1
echo -e " ${GREEN}Processes stopped${NC}"
}
# ── [10] View/export/clean logs ──────────────────────────────
do_logs() {
echo ""
echo -e " ${CYAN}${BOLD}--- Logs ---${NC}"
echo ""
mkdir -p "$LOG_DIR"
local LOG_FILE="$LOG_DIR/gateway.log"
echo -e " ${GREEN}[a]${NC} Show the last 50 lines"
echo -e " ${GREEN}[b]${NC} Copy the log to the desktop"
echo -e " ${GREEN}[c]${NC} Delete logs older than 7 days"
echo ""
read -p " Choose (a-c): " -n 1 LOG_CHOICE
echo ""
echo ""
case $LOG_CHOICE in
a)
if [ -f "$LOG_FILE" ]; then
echo -e " ${DIM}── $LOG_FILE ──${NC}"
echo ""
tail -50 "$LOG_FILE"
else
echo -e " ${YELLOW}No log file yet: $LOG_FILE${NC}"
echo " One is created the first time U-Claw runs."
fi
;;
b)
if [ ! -f "$LOG_FILE" ]; then
echo -e " ${YELLOW}No log to copy${NC}"
return
fi
local TS=$(date +%Y%m%d_%H%M%S)
local EXPORT_DIR="$HOME/Desktop"
[ ! -d "$EXPORT_DIR" ] && EXPORT_DIR="$HOME"
local EXPORT_FILE="$EXPORT_DIR/uclaw-logs-$TS.txt"
cp "$LOG_FILE" "$EXPORT_FILE"
echo -e " ${GREEN}Copied to: $EXPORT_FILE${NC}"
echo " Size: $(du -sh "$EXPORT_FILE" | cut -f1)"
;;
c)
local COUNT=$(find "$LOG_DIR" -name "*.log" -mtime +7 2>/dev/null | wc -l | tr -d ' ')
if [ "$COUNT" = "0" ]; then
echo -e " ${GREEN}No old logs to delete${NC}"
return
fi
echo -e " Found ${YELLOW}$COUNT${NC} log files older than 7 days"
read -p " Delete them? (y/N): " CONFIRM
if [ "$CONFIRM" = "y" ] || [ "$CONFIRM" = "Y" ]; then
find "$LOG_DIR" -name "*.log" -mtime +7 -delete 2>/dev/null
echo -e " ${GREEN}Old logs deleted${NC}"
else
echo -e " ${YELLOW}Cancelled${NC}"
fi
;;
*)
echo -e " ${RED}Not a valid choice${NC}"
;;
esac
}
# ── [11] Factory reset ───────────────────────────────────────
do_factory_reset() {
echo ""
echo -e " ${RED}${BOLD}--- Reset to factory settings ---${NC}"
echo ""
echo -e " ${RED}This deletes every setting and everything the AI remembers.${NC}"
echo ""
echo " It will:"
echo " 1. Back up your settings and memory first"
echo " 2. Delete openclaw.json"
echo " 3. Delete memory/"
echo " 4. Put the defaults back"
echo ""
echo -e " ${YELLOW}Type RESET to confirm (case-sensitive):${NC}"
read -p " > " CONFIRM
if [ "$CONFIRM" != "RESET" ]; then
echo -e " ${YELLOW}Cancelled${NC}"
return
fi
# Step 1: Auto backup
echo ""
echo -e " ${CYAN}[1/4] Backing up…${NC}"
local TS=$(date +%Y%m%d_%H%M%S)
local BK="$BACKUP_DIR/pre-reset_$TS"
mkdir -p "$BK"
[ -f "$CONFIG_PATH" ] && cp "$CONFIG_PATH" "$BK/" 2>/dev/null
[ -d "$DATA_DIR/memory" ] && cp -R "$DATA_DIR/memory" "$BK/" 2>/dev/null
echo -e " ${GREEN}Backup saved to $BK${NC}"
# Step 2: Delete config
echo -e " ${CYAN}[2/4] Removing settings…${NC}"
rm -f "$CONFIG_PATH" 2>/dev/null
rm -f "$DATA_DIR/config.json" 2>/dev/null
# Step 3: Delete memory
echo -e " ${CYAN}[3/4] Removing memory…${NC}"
rm -rf "$DATA_DIR/memory" 2>/dev/null
mkdir -p "$DATA_DIR/memory"
# Step 4: Restore default config
echo -e " ${CYAN}[4/4] Restoring defaults…${NC}"
mkdir -p "$STATE_DIR"
if [ -f "$DEFAULT_CONFIG" ]; then
cp "$DEFAULT_CONFIG" "$CONFIG_PATH"
else
cat > "$CONFIG_PATH" << 'CFGEOF'
{
"gateway": {
"mode": "local",
"auth": { "token": "uclaw" }
}
}
CFGEOF
fi
echo ""
echo -e " ${GREEN}Reset complete.${NC}"
echo -e " Your old settings are at $BK"
echo -e " Run the setup wizard again to add a model."
}
# ── [12] Uninstall ───────────────────────────────────────────
do_uninstall() {
echo ""
echo -e " ${RED}${BOLD}--- Uninstall U-Claw ---${NC}"
echo ""
local MODE=$(detect_install_mode)
case $MODE in
installed)
echo " Found: installed copy at ~/.uclaw/"
echo ""
echo " These folders will be deleted:"
echo -e " ${YELLOW}$HOME/.uclaw/${NC}"
if [ -d "$HOME/.uclaw" ]; then
echo " Size: $(du -sh "$HOME/.uclaw" 2>/dev/null | cut -f1)"
fi
echo ""
echo -e " ${RED}Type UNINSTALL to confirm:${NC}"
read -p " > " CONFIRM
if [ "$CONFIRM" != "UNINSTALL" ]; then
echo -e " ${YELLOW}Cancelled${NC}"
return
fi
rm -rf "$HOME/.uclaw"
echo -e " ${GREEN}Uninstalled.${NC}"
;;
usb)
echo " Found: running from a USB drive"
echo ""
echo " There is nothing to uninstall. Just:"
echo " 1. Close every U-Claw window"
echo " 2. Eject the drive"
echo " 3. If you want nothing left on this computer, delete ~/.uclaw/"
echo ""
if [ -d "$HOME/.uclaw" ]; then
echo -e " ${YELLOW}Found data on this computer: ~/.uclaw/${NC}"
echo " Size: $(du -sh "$HOME/.uclaw" 2>/dev/null | cut -f1)"
read -p " Delete it? (y/N): " DEL
if [ "$DEL" = "y" ] || [ "$DEL" = "Y" ]; then
rm -rf "$HOME/.uclaw"
echo -e " ${GREEN}Deleted${NC}"
fi
fi
;;
portable)
echo " Found: portable copy"
echo ""
echo " There is nothing to uninstall — just delete the folder:"
echo -e " ${YELLOW}$UCLAW_DIR${NC}"
echo ""
if [ -d "$HOME/.uclaw" ]; then
echo -e " ${YELLOW}Found data on this computer: ~/.uclaw/${NC}"
echo " Size: $(du -sh "$HOME/.uclaw" 2>/dev/null | cut -f1)"
read -p " Delete it? (y/N): " DEL
if [ "$DEL" = "y" ] || [ "$DEL" = "Y" ]; then
rm -rf "$HOME/.uclaw"
echo -e " ${GREEN}Deleted${NC}"
fi
fi
echo ""
echo " Electron desktop app:"
echo " macOS: drag U-Claw.app from Applications to the Trash"
;;
esac
}
# ── [13] Check for updates (P1) ─────────────────────────────
do_update() {
echo ""
echo -e " ${CYAN}${BOLD}--- Check for updates ---${NC}"
echo ""
if [ ! -f "$NODE_BIN" ]; then
echo -e " ${RED}Node.js is missing, so updates cannot be checked${NC}"
return
fi
echo -e " ${DIM}Looking up the latest version…${NC}"
# Get current version
local CUR_VER=""
if [ -f "$CORE_DIR/node_modules/openclaw/package.json" ]; then
CUR_VER=$("$NODE_BIN" -e "console.log(require('$CORE_DIR/node_modules/openclaw/package.json').version)" 2>/dev/null)
fi
if [ -z "$CUR_VER" ]; then
echo -e " ${RED}Cannot read the installed version — OpenClaw may not be installed${NC}"
return
fi
# Get latest version from the npm registry
local LATEST_VER=$(curl -s --connect-timeout 10 "https://registry.npmjs.org/openclaw/latest" 2>/dev/null | "$NODE_BIN" -e "let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>{try{console.log(JSON.parse(d).version)}catch(e){console.log('error')}})" 2>/dev/null)
if [ -z "$LATEST_VER" ] || [ "$LATEST_VER" = "error" ]; then
echo -e " ${YELLOW}Could not reach the registry. Check your network.${NC}"
echo " Installed: $CUR_VER"
return
fi
echo " Installed: $CUR_VER"
echo " Latest: $LATEST_VER"
echo ""
if [ "$CUR_VER" = "$LATEST_VER" ]; then
echo -e " ${GREEN}Already up to date.${NC}"
return
fi
echo -e " ${YELLOW}A newer version is available.${NC}"
read -p " Update now? (y/N): " DO_UPDATE
if [ "$DO_UPDATE" != "y" ] && [ "$DO_UPDATE" != "Y" ]; then
echo -e " ${YELLOW}Cancelled${NC}"
return
fi
echo ""
echo -e " ${CYAN}Updating…${NC}"
cd "$CORE_DIR"
"$NPM_BIN" install openclaw@latest --registry=https://registry.npmjs.org 2>&1
local NEW_VER=$("$NODE_BIN" -e "console.log(require('./node_modules/openclaw/package.json').version)" 2>/dev/null)
echo ""
echo -e " ${GREEN}Updated.${NC} $CUR_VER$NEW_VER"
}
# ── [14] Disk cleanup (P1) ───────────────────────────────────
do_cleanup() {
echo ""
echo -e " ${CYAN}${BOLD}--- Free up space ---${NC}"
echo ""
# Show directory sizes
echo -e " ${WHITE}Space used:${NC}"
[ -d "$CORE_DIR/node_modules" ] && echo " node_modules: $(du -sh "$CORE_DIR/node_modules" 2>/dev/null | cut -f1)"
[ -d "$DATA_DIR/memory" ] && echo " memory: $(du -sh "$DATA_DIR/memory" 2>/dev/null | cut -f1)"
[ -d "$BACKUP_DIR" ] && echo " backups: $(du -sh "$BACKUP_DIR" 2>/dev/null | cut -f1)"
[ -d "$LOG_DIR" ] && echo " logs: $(du -sh "$LOG_DIR" 2>/dev/null | cut -f1)"
echo " Total: $(du -sh "$UCLAW_DIR" 2>/dev/null | cut -f1)"
echo ""
local CLEANED=0
# Clean old backups (keep latest 3)
if [ -d "$BACKUP_DIR" ]; then
local BK_COUNT=$(ls -d "$BACKUP_DIR"/*/ 2>/dev/null | wc -l | tr -d ' ')
if [ "$BK_COUNT" -gt 3 ]; then
local OLD_COUNT=$((BK_COUNT - 3))
echo -e " ${YELLOW}Found $BK_COUNT backups. Keeping the newest 3, deleting $OLD_COUNT.${NC}"
read -p " Go ahead? (y/N): " DEL_BK
if [ "$DEL_BK" = "y" ] || [ "$DEL_BK" = "Y" ]; then
ls -dt "$BACKUP_DIR"/*/ 2>/dev/null | tail -n "$OLD_COUNT" | while read -r DIR; do
rm -rf "$DIR"
done
echo -e " ${GREEN}Old backups deleted${NC}"
CLEANED=1
fi
else
echo " Backups: ${BK_COUNT} — nothing to clear"
fi
fi
# Clean old logs (>7 days)
if [ -d "$LOG_DIR" ]; then
local OLD_LOGS=$(find "$LOG_DIR" -name "*.log" -mtime +7 2>/dev/null | wc -l | tr -d ' ')
if [ "$OLD_LOGS" -gt 0 ]; then
echo -e " ${YELLOW}Found $OLD_LOGS log files older than 7 days${NC}"
read -p " Delete them? (y/N): " DEL_LOG
if [ "$DEL_LOG" = "y" ] || [ "$DEL_LOG" = "Y" ]; then
find "$LOG_DIR" -name "*.log" -mtime +7 -delete 2>/dev/null
echo -e " ${GREEN}Old logs deleted${NC}"
CLEANED=1
fi
else
echo " Logs: nothing to clear"
fi
fi
# Clean npm cache
echo ""
read -p " Clear the npm cache? (y/N): " DEL_CACHE
if [ "$DEL_CACHE" = "y" ] || [ "$DEL_CACHE" = "Y" ]; then
"$NPM_BIN" cache clean --force 2>/dev/null
echo -e " ${GREEN}npm cache cleared${NC}"
CLEANED=1
fi
if [ "$CLEANED" = "0" ]; then
echo ""
echo -e " ${GREEN}Nothing to clear${NC}"
else
echo ""
echo " Now using: $(du -sh "$UCLAW_DIR" 2>/dev/null | cut -f1)"
fi
}
# ── [15] Plugin management (P1) ─────────────────────────────
do_plugins() {
echo ""
echo -e " ${CYAN}${BOLD}--- Plugins ---${NC}"
echo ""
echo -e " ${GREEN}[a]${NC} List installed plugins"
echo -e " ${GREEN}[b]${NC} Install a plugin"
echo -e " ${GREEN}[c]${NC} Remove a plugin"
echo ""
read -p " Choose (a-c): " -n 1 PLG_CHOICE
echo ""
echo ""
case $PLG_CHOICE in
a)
echo -e " ${WHITE}Installed plugins:${NC}"
echo ""
run_oc plugins list 2>&1 || echo -e " ${YELLOW}Could not list plugins${NC}"
;;
b)
echo " Commonly used:"
echo " @icesword760/openclaw-wechat WeChat"
echo " @nicepkg/openclaw-plugin-qq QQ (community build)"
echo ""
read -p " Plugin name (blank to cancel): " PLG_NAME
if [ -z "$PLG_NAME" ]; then
echo -e " ${YELLOW}Cancelled${NC}"
return
fi
echo ""
echo -e " ${CYAN}Installing $PLG_NAME${NC}"
run_oc plugins install "$PLG_NAME" 2>&1
echo ""
echo -e " ${GREEN}Installed${NC}"
;;
c)
echo -e " ${WHITE}Installed plugins:${NC}"
echo ""
run_oc plugins list 2>&1 || true
echo ""
read -p " Plugin to remove (blank to cancel): " PLG_NAME
if [ -z "$PLG_NAME" ]; then
echo -e " ${YELLOW}Cancelled${NC}"
return
fi
echo ""
echo -e " ${CYAN}Removing $PLG_NAME${NC}"
run_oc plugins remove "$PLG_NAME" 2>&1
echo ""
echo -e " ${GREEN}Removed${NC}"
;;
*)
echo -e " ${RED}Not a valid choice${NC}"
;;
esac
}