@ryantest/openclaw-qqbot 1.6.7-beta.2 → 1.6.7-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -162,7 +162,7 @@ function checkUpgradeCompatibility(): UpgradeCompatResult {
162
162
  // 3. 检查 Node.js 版本
163
163
  const nodeVer = process.version.replace(/^v/, "");
164
164
  if (compareSemver(nodeVer, req.minNodeVersion) < 0) {
165
- errors.push(`❌ Node.js 版本过低:当前 **v${nodeVer}**,热更新要求最低 **v${req.minNodeVersion}**`);
165
+ errors.push(`❌ NoVBNde.js 版本过低:当前 **v${nodeVer}**,热更新要求最低 **v${req.minNodeVersion}**`);
166
166
  }
167
167
 
168
168
  // 4. 检查系统架构(arm 等特殊架构提示)
@@ -746,14 +746,22 @@ function cleanupTempScript(): void {
746
746
  *
747
747
  * 安全机制:脚本会被复制到临时目录再执行,避免升级过程中插件目录被操作导致脚本自身丢失。
748
748
  */
749
- function fireHotUpgrade(targetVersion?: string): HotUpgradeStartResult {
750
- // 优先从远端下载升级脚本,避免使用本地可能过时的版本
751
- const scriptPath = downloadRemoteUpgradeScript() || (() => {
752
- const local = getUpgradeScriptPath();
753
- if (!local) return null;
754
- console.log(`[qqbot] fireHotUpgrade: remote download failed, falling back to local script: ${local}`);
755
- return copyScriptToTemp(local) || local;
756
- })();
749
+ function fireHotUpgrade(targetVersion?: string, pkg?: string, useLocal?: boolean): HotUpgradeStartResult {
750
+ // --local: 直接使用本地脚本,跳过远端下载
751
+ // 默认: 优先从远端下载升级脚本,避免使用本地可能过时的版本
752
+ const scriptPath = useLocal
753
+ ? (() => {
754
+ const local = getUpgradeScriptPath();
755
+ if (!local) return null;
756
+ console.log(`[qqbot] fireHotUpgrade: --local specified, using local script: ${local}`);
757
+ return copyScriptToTemp(local) || local;
758
+ })()
759
+ : downloadRemoteUpgradeScript() || (() => {
760
+ const local = getUpgradeScriptPath();
761
+ if (!local) return null;
762
+ console.log(`[qqbot] fireHotUpgrade: remote download failed, falling back to local script: ${local}`);
763
+ return copyScriptToTemp(local) || local;
764
+ })();
757
765
  if (!scriptPath) return { ok: false, reason: "no-script" };
758
766
 
759
767
  const cli = findCli();
@@ -772,27 +780,128 @@ function fireHotUpgrade(targetVersion?: string): HotUpgradeStartResult {
772
780
  "-File", scriptPath,
773
781
  "-NoRestart",
774
782
  ...(targetVersion ? ["-Version", targetVersion] : []),
783
+ ...(pkg ? ["-Pkg", pkg] : []),
775
784
  ];
776
785
  } else {
777
786
  // Mac / Linux: bash 执行 .sh
778
787
  const bash = findBash();
779
788
  if (!bash) return { ok: false, reason: "no-bash" };
780
789
  shell = bash;
781
- shellArgs = [scriptPath, "--no-restart", ...(targetVersion ? ["--version", targetVersion] : [])];
790
+ shellArgs = [scriptPath, "--no-restart", ...(targetVersion ? ["--version", targetVersion] : []), ...(pkg ? ["--pkg", pkg] : [])];
782
791
  }
783
792
 
784
- console.log(`[qqbot] fireHotUpgrade: shell=${shell}, script=${scriptPath}, cli=${cli}, target=${targetVersion || "latest"}`);
793
+ console.log(`[qqbot] fireHotUpgrade: shell=${shell}, script=${scriptPath}, cli=${cli}, target=${targetVersion || "latest"}, pkg=${pkg || "default"}`);
794
+
795
+ // ── 兼容 openclaw 3.23+ 配置严格校验 ──
796
+ // openclaw plugins install/update 启动时会校验整个配置文件,
797
+ // 如果 channels.qqbot 已存在但 qqbot 插件尚未加载,校验会报 "unknown channel id: qqbot"。
798
+ //
799
+ // ⚠️ 关键:绝不能直接修改真实的 openclaw.json!
800
+ // gateway 的 config file watcher 会检测到变更并触发 SIGUSR1 重启,
801
+ // 导致当前进程被杀、execFile 回调(restoreConfigAndCleanup)永远不会执行,
802
+ // channels.qqbot 配置就此丢失。
803
+ //
804
+ // 策略:创建临时配置副本(不含 channels.qqbot),通过 OPENCLAW_CONFIG_PATH
805
+ // 环境变量传递给子进程,真实配置文件不受影响。
806
+ // shell 脚本(upgrade-via-npm.sh)内部也有同样的临时配置机制作为双保险。
807
+ const homeDir = getHomeDir();
808
+ const realConfigPath = path.join(homeDir, ".openclaw", "openclaw.json");
809
+ let tempConfigPath: string | null = null;
810
+ const childEnv: NodeJS.ProcessEnv = { ...process.env };
811
+
812
+ try {
813
+ if (fs.existsSync(realConfigPath)) {
814
+ const cfg = JSON.parse(fs.readFileSync(realConfigPath, "utf8"));
815
+ const needsTempConfig =
816
+ !!(cfg.channels?.qqbot) ||
817
+ !!(cfg.plugins?.entries?.["openclaw-qqbot"]);
818
+
819
+ if (needsTempConfig) {
820
+ // 创建临时配置副本(移除 channels.qqbot 和 plugins.entries.openclaw-qqbot)
821
+ const cleanCfg = JSON.parse(JSON.stringify(cfg)); // deep clone
822
+ if (cleanCfg.channels?.qqbot) {
823
+ delete cleanCfg.channels.qqbot;
824
+ if (Object.keys(cleanCfg.channels).length === 0) delete cleanCfg.channels;
825
+ }
826
+ if (cleanCfg.plugins?.entries?.["openclaw-qqbot"]) {
827
+ delete cleanCfg.plugins.entries["openclaw-qqbot"];
828
+ if (cleanCfg.plugins.entries && Object.keys(cleanCfg.plugins.entries).length === 0) delete cleanCfg.plugins.entries;
829
+ }
830
+
831
+ const tmpDir = path.join(homeDir, ".openclaw", ".qqbot-upgrade-tmp");
832
+ fs.mkdirSync(tmpDir, { recursive: true });
833
+ tempConfigPath = path.join(tmpDir, "openclaw-tmp.json");
834
+ fs.writeFileSync(tempConfigPath, JSON.stringify(cleanCfg, null, 4) + "\n");
835
+ childEnv.OPENCLAW_CONFIG_PATH = tempConfigPath;
836
+ console.log(`[qqbot] fireHotUpgrade: created temp config without channels.qqbot (OPENCLAW_CONFIG_PATH=${tempConfigPath}), real config untouched`);
837
+ }
838
+ }
839
+ } catch (e: any) {
840
+ console.warn(`[qqbot] fireHotUpgrade: failed to create temp config: ${e.message}, proceeding with original`);
841
+ tempConfigPath = null;
842
+ }
843
+
844
+ /**
845
+ * 将 openclaw plugins install 写入临时配置的 installs/entries 记录同步回真实配置,
846
+ * 然后清理临时文件。
847
+ *
848
+ * 注意:真实配置中的 channels.qqbot 从未被移除,无需恢复。
849
+ */
850
+ function syncTempConfigAndCleanup(): void {
851
+ try {
852
+ if (tempConfigPath && fs.existsSync(tempConfigPath) && fs.existsSync(realConfigPath)) {
853
+ const tmp = JSON.parse(fs.readFileSync(tempConfigPath, "utf8"));
854
+ const real = JSON.parse(fs.readFileSync(realConfigPath, "utf8"));
855
+ let changed = false;
856
+
857
+ // 同步 plugins.installs(openclaw plugins install 会写入安装记录)
858
+ if (tmp.plugins?.installs) {
859
+ if (!real.plugins) real.plugins = {};
860
+ real.plugins.installs = { ...(real.plugins.installs || {}), ...tmp.plugins.installs };
861
+ changed = true;
862
+ }
863
+ // 同步 plugins.entries(openclaw plugins install 会写入 entries)
864
+ // 注意:不同步 openclaw-qqbot 自身的 entry,因为插件通过 auto-discover 加载,
865
+ // 显式写入 entries 会导致 "duplicate plugin id" 警告刷屏。
866
+ if (tmp.plugins?.entries) {
867
+ if (!real.plugins) real.plugins = {};
868
+ if (!real.plugins.entries) real.plugins.entries = {};
869
+ for (const [k, v] of Object.entries(tmp.plugins.entries)) {
870
+ if (k === "openclaw-qqbot") continue; // 跳过自身,避免 duplicate
871
+ if (!real.plugins.entries[k]) {
872
+ real.plugins.entries[k] = v;
873
+ changed = true;
874
+ }
875
+ }
876
+ }
877
+
878
+ if (changed) {
879
+ fs.writeFileSync(realConfigPath, JSON.stringify(real, null, 4) + "\n");
880
+ console.log("[qqbot] fireHotUpgrade: synced install/entries records from temp config to real config");
881
+ }
882
+ }
883
+ } catch (e: any) {
884
+ console.warn(`[qqbot] fireHotUpgrade: failed to sync temp config: ${e.message}`);
885
+ }
886
+ // 清理临时文件
887
+ try { if (tempConfigPath) fs.unlinkSync(tempConfigPath); } catch { /* ignore */ }
888
+ }
785
889
 
786
890
  // 异步执行升级脚本
891
+ // 必须显式设置 cwd 为一个确定存在的目录(如 homeDir),
892
+ // 否则子进程继承 gateway 的 cwd,如果该目录在升级过程中被删除/移动,
893
+ // openclaw CLI 启动时 process.cwd() 会报 ENOENT: uv_cwd 错误。
787
894
  execFile(shell, shellArgs, {
788
895
  timeout: 120_000,
789
- env: { ...process.env },
896
+ cwd: homeDir,
897
+ env: childEnv,
790
898
  ...(isWindows() ? { windowsHide: true } : {}),
791
899
  }, (error, stdout, _stderr) => {
792
900
  if (error) {
793
901
  console.error(`[qqbot] fireHotUpgrade: script failed: ${error.message}`);
794
902
  if (stdout) console.error(`[qqbot] fireHotUpgrade: stdout: ${stdout.slice(0, 2000)}`);
795
903
  if (_stderr) console.error(`[qqbot] fireHotUpgrade: stderr: ${_stderr.slice(0, 2000)}`);
904
+ syncTempConfigAndCleanup();
796
905
  cleanupTempScript();
797
906
  _upgrading = false;
798
907
  return;
@@ -805,6 +914,7 @@ function fireHotUpgrade(targetVersion?: string): HotUpgradeStartResult {
805
914
  const newVersion = versionMatch?.[1];
806
915
  if (newVersion === "unknown") {
807
916
  console.error(`[qqbot] fireHotUpgrade: script output QQBOT_NEW_VERSION=unknown, aborting restart`);
917
+ syncTempConfigAndCleanup();
808
918
  cleanupTempScript();
809
919
  _upgrading = false;
810
920
  return;
@@ -812,7 +922,8 @@ function fireHotUpgrade(targetVersion?: string): HotUpgradeStartResult {
812
922
 
813
923
  console.log(`[qqbot] fireHotUpgrade: new version=${newVersion || "(not detected)"}, triggering restart...`);
814
924
 
815
- // 脚本执行成功,清理临时脚本副本
925
+ // 脚本执行成功,同步临时配置中的 install 记录并清理
926
+ syncTempConfigAndCleanup();
816
927
  cleanupTempScript();
817
928
 
818
929
  // 文件替换成功,在 restart 之前把 source 从 path 切换为 npm,
@@ -855,17 +966,176 @@ function fireHotUpgrade(targetVersion?: string): HotUpgradeStartResult {
855
966
  execCliAsync(cli, ["gateway", "restart"], { timeout: 30_000 }, () => {});
856
967
  }
857
968
  } else {
858
- // Mac/Linux: 直接 restart(框架通常以 daemon 模式运行)
859
- execCliAsync(cli, ["gateway", "restart"], { timeout: 30_000 }, (restartErr) => {
860
- if (restartErr) {
861
- console.error(`[qqbot] fireHotUpgrade: restart failed: ${restartErr.message}, trying stop+start fallback`);
862
- execCliAsync(cli, ["gateway", "stop"], { timeout: 10_000 }, () => {
863
- setTimeout(() => {
864
- execCliAsync(cli, ["gateway", "start"], { timeout: 30_000 }, () => {});
865
- }, 1000);
866
- });
969
+ // Mac/Linux: 使用 detached shell 脚本执行 stop+start
970
+ //
971
+ // 兼容 openclaw 2026.3.24+ 配置严格校验:
972
+ // gateway restart openclaw 先校验配置(loadConfig)再加载插件。
973
+ // 如果 channels.qqbot 存在但 qqbot channel type 尚未注册,校验会失败。
974
+ // 解决:stop 后临时移除 channels.qqbot → start(插件加载、qqbot type 注册)→ 恢复。
975
+ const cliInvoke = cli.endsWith(".mjs")
976
+ ? `"${process.execPath}" "${cli}"`
977
+ : `"${cli}"`;
978
+ const homeDir = getHomeDir();
979
+ const configPath = path.join(homeDir, ".openclaw", "openclaw.json");
980
+ const qqbotChannelBackup = path.join(homeDir, ".openclaw", ".qqbot-channel-backup.json");
981
+ const restartScript = path.join(homeDir, ".openclaw", ".qqbot-restart.sh");
982
+
983
+ // 先保存 channels.qqbot 到临时文件(在当前进程中,JSON 处理更安全)
984
+ let hasChannel = false;
985
+ try {
986
+ const cfgRaw = fs.readFileSync(configPath, "utf8");
987
+ const cfg = JSON.parse(cfgRaw);
988
+ const qqbot = cfg?.channels?.qqbot;
989
+ if (qqbot) {
990
+ fs.writeFileSync(qqbotChannelBackup, JSON.stringify(qqbot, null, 2), "utf8");
991
+ hasChannel = true;
867
992
  }
868
- });
993
+ } catch {
994
+ // 配置文件不存在或 JSON 解析失败,不做处理
995
+ }
996
+
997
+ const shContent = `#!/bin/bash
998
+ # 注意:不使用 set -e,因为 gateway start 失败时仍需恢复 channels.qqbot
999
+ CLI="${cliInvoke}"
1000
+ CONFIG="${configPath}"
1001
+ BACKUP="${qqbotChannelBackup}"
1002
+
1003
+ # ── 兼容 openclaw 3.23+ 配置严格校验 ──
1004
+ # 所有 openclaw CLI 命令(包括 gateway stop/start)启动时都会 loadConfig 校验配置,
1005
+ # 如果 channels.qqbot 存在但 qqbot 插件尚未加载,校验会报 "unknown channel id: qqbot"。
1006
+ #
1007
+ # 策略:
1008
+ # 1. gateway stop:使用 OPENCLAW_CONFIG_PATH 临时配置(不含 channels.qqbot)
1009
+ # 2. gateway start:先尝试直接启动(真实配置),如果 CLI 校验失败,
1010
+ # 则临时修改真实配置(此时 gateway 已停止,无 config watcher),启动后恢复。
1011
+ # 这样 gateway 进程读取的是完整配置(含 channels.qqbot)。
1012
+
1013
+ # 为 gateway stop 创建临时配置
1014
+ TEMP_RESTART_CONFIG=""
1015
+ if [ -f "$BACKUP" ]; then
1016
+ TEMP_RESTART_CONFIG="\$(mktemp)"
1017
+ node -e "
1018
+ const fs = require('fs');
1019
+ const cfg = JSON.parse(fs.readFileSync(process.argv[1], 'utf8'));
1020
+ if (cfg.channels && cfg.channels.qqbot) {
1021
+ delete cfg.channels.qqbot;
1022
+ if (Object.keys(cfg.channels).length === 0) delete cfg.channels;
1023
+ }
1024
+ if (cfg.plugins && cfg.plugins.entries && cfg.plugins.entries['openclaw-qqbot']) {
1025
+ delete cfg.plugins.entries['openclaw-qqbot'];
1026
+ if (Object.keys(cfg.plugins.entries).length === 0) delete cfg.plugins.entries;
1027
+ }
1028
+ fs.writeFileSync(process.argv[2], JSON.stringify(cfg, null, 4) + '\\n');
1029
+ " "$CONFIG" "$TEMP_RESTART_CONFIG" 2>/dev/null
1030
+ if [ \$? -ne 0 ] || [ ! -s "$TEMP_RESTART_CONFIG" ]; then
1031
+ echo "[qqbot-upgrade] WARNING: failed to create temp config"
1032
+ TEMP_RESTART_CONFIG=""
1033
+ fi
1034
+ fi
1035
+
1036
+ echo "[qqbot-upgrade] Stopping gateway..."
1037
+ if [ -n "$TEMP_RESTART_CONFIG" ]; then
1038
+ OPENCLAW_CONFIG_PATH="$TEMP_RESTART_CONFIG" $CLI gateway stop 2>/dev/null || true
1039
+ else
1040
+ $CLI gateway stop 2>/dev/null || true
1041
+ fi
1042
+ sleep 2
1043
+
1044
+ # 清理临时配置(不再需要)
1045
+ if [ -n "$TEMP_RESTART_CONFIG" ] && [ -f "$TEMP_RESTART_CONFIG" ]; then
1046
+ rm -f "$TEMP_RESTART_CONFIG"
1047
+ fi
1048
+
1049
+ echo "[qqbot-upgrade] Starting gateway..."
1050
+ START_OK=false
1051
+
1052
+ # 先尝试直接启动(使用真实配置,含 channels.qqbot)
1053
+ # 如果 openclaw 版本不做严格校验,或者插件已注册,这会直接成功
1054
+ if $CLI gateway start 2>/dev/null; then
1055
+ START_OK=true
1056
+ echo "[qqbot-upgrade] Gateway started successfully (direct start)"
1057
+ elif [ -f "$BACKUP" ]; then
1058
+ # 直接启动失败(可能是 channels.qqbot 校验失败),
1059
+ # 临时修改真实配置(此时 gateway 已停止,无 config watcher,安全)
1060
+ echo "[qqbot-upgrade] Direct start failed, temporarily removing channels.qqbot from real config..."
1061
+ node -e "
1062
+ const fs = require('fs');
1063
+ const cfg = JSON.parse(fs.readFileSync(process.argv[1], 'utf8'));
1064
+ let changed = false;
1065
+ if (cfg.channels && cfg.channels.qqbot) {
1066
+ delete cfg.channels.qqbot;
1067
+ if (Object.keys(cfg.channels).length === 0) delete cfg.channels;
1068
+ changed = true;
1069
+ }
1070
+ if (cfg.plugins && cfg.plugins.entries && cfg.plugins.entries['openclaw-qqbot']) {
1071
+ delete cfg.plugins.entries['openclaw-qqbot'];
1072
+ if (Object.keys(cfg.plugins.entries).length === 0) delete cfg.plugins.entries;
1073
+ changed = true;
1074
+ }
1075
+ if (changed) {
1076
+ fs.writeFileSync(process.argv[1], JSON.stringify(cfg, null, 4) + '\\n');
1077
+ }
1078
+ " "$CONFIG" 2>/dev/null
1079
+
1080
+ if $CLI gateway start 2>/dev/null; then
1081
+ START_OK=true
1082
+ echo "[qqbot-upgrade] Gateway started successfully (after config fix)"
1083
+ else
1084
+ echo "[qqbot-upgrade] WARNING: gateway start still failed after config fix"
1085
+ fi
1086
+
1087
+ # 等待 gateway 进程启动并加载插件(插件注册 qqbot channel type)
1088
+ echo "[qqbot-upgrade] Waiting for plugin to load (8s)..."
1089
+ sleep 8
1090
+
1091
+ # 恢复 channels.qqbot 到真实配置
1092
+ # gateway 的 config file watcher 会检测到变更并热加载
1093
+ echo "[qqbot-upgrade] Restoring channels.qqbot to real config..."
1094
+ node -e "
1095
+ const fs = require('fs');
1096
+ const fs = require('fs');
1097
+ const cfg = JSON.parse(fs.readFileSync(process.argv[1], 'utf8'));
1098
+ const qqbot = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
1099
+ if (!cfg.channels) cfg.channels = {};
1100
+ cfg.channels.qqbot = qqbot;
1101
+ // 注意:不写入 plugins.entries.openclaw-qqbot,
1102
+ // 插件通过 auto-discover 加载,显式 entry 会导致 duplicate plugin id 警告。
1103
+ fs.writeFileSync(process.argv[1], JSON.stringify(cfg, null, 4) + '\\n');
1104
+ " "$CONFIG" "$BACKUP" 2>/dev/null
1105
+ rm -f "$BACKUP"
1106
+ echo "[qqbot-upgrade] channels.qqbot restored"
1107
+ else
1108
+ echo "[qqbot-upgrade] WARNING: gateway start failed, no backup to restore"
1109
+ fi
1110
+
1111
+ # 直接启动成功的情况下,清理备份文件
1112
+ if [ "$START_OK" = "true" ] && [ -f "$BACKUP" ]; then
1113
+ rm -f "$BACKUP"
1114
+ fi
1115
+
1116
+ # 如果 start 失败,尝试再次启动
1117
+ if [ "$START_OK" != "true" ]; then
1118
+ echo "[qqbot-upgrade] Retrying gateway start..."
1119
+ sleep 2
1120
+ $CLI gateway start 2>/dev/null || echo "[qqbot-upgrade] WARNING: retry also failed"
1121
+ fi
1122
+
1123
+ # 清理自身
1124
+ rm -f "$0"
1125
+ echo "[qqbot-upgrade] Done."
1126
+ `;
1127
+ try {
1128
+ fs.writeFileSync(restartScript, shContent, { mode: 0o755 });
1129
+ const child = spawn("bash", [restartScript], {
1130
+ detached: true,
1131
+ stdio: "ignore",
1132
+ });
1133
+ child.unref();
1134
+ console.log(`[qqbot] fireHotUpgrade: launched detached restart script (pid=${child.pid}), hasChannel=${hasChannel}`);
1135
+ } catch (shErr: any) {
1136
+ console.error(`[qqbot] fireHotUpgrade: failed to launch restart script: ${shErr.message}, falling back to direct restart`);
1137
+ execCliAsync(cli, ["gateway", "restart"], { timeout: 30_000 }, () => {});
1138
+ }
869
1139
  }
870
1140
  });
871
1141
 
@@ -894,7 +1164,9 @@ registerCommand({
894
1164
  `/bot-upgrade 检查是否有新版本`,
895
1165
  `/bot-upgrade --latest 确认升级到最新版本(需 upgradeMode=hot-reload)`,
896
1166
  `/bot-upgrade --version X 升级到指定版本(需 upgradeMode=hot-reload)`,
1167
+ `/bot-upgrade --pkg scope/name 指定 npm 包(如 ryantest/openclaw-qqbot)`,
897
1168
  `/bot-upgrade --force 强制重新安装当前版本(需 upgradeMode=hot-reload)`,
1169
+ `/bot-upgrade --local 使用本地升级脚本(跳过远端下载)`,
898
1170
  ].join("\n"),
899
1171
  handler: async (ctx) => {
900
1172
  const url = ctx.accountConfig?.upgradeUrl || DEFAULT_UPGRADE_URL;
@@ -949,7 +1221,9 @@ registerCommand({
949
1221
 
950
1222
  let isForce = false;
951
1223
  let isLatest = false;
1224
+ let isLocal = false;
952
1225
  let versionArg: string | undefined;
1226
+ let pkgArg: string | undefined;
953
1227
  const tokens = args ? args.split(/\s+/).filter(Boolean) : [];
954
1228
  for (let i = 0; i < tokens.length; i += 1) {
955
1229
  const t = tokens[i]!;
@@ -961,6 +1235,27 @@ registerCommand({
961
1235
  isLatest = true;
962
1236
  continue;
963
1237
  }
1238
+ if (t === "--local") {
1239
+ isLocal = true;
1240
+ continue;
1241
+ }
1242
+ if (t === "--pkg") {
1243
+ const next = tokens[i + 1];
1244
+ if (!next || next.startsWith("--")) {
1245
+ return `❌ 参数错误:--pkg 需要包名\n\n示例:/bot-upgrade --pkg ryantest/openclaw-qqbot`;
1246
+ }
1247
+ pkgArg = next;
1248
+ i += 1;
1249
+ continue;
1250
+ }
1251
+ if (t.startsWith("--pkg=")) {
1252
+ const v = t.slice("--pkg=".length).trim();
1253
+ if (!v) {
1254
+ return `❌ 参数错误:--pkg 需要包名\n\n示例:/bot-upgrade --pkg ryantest/openclaw-qqbot`;
1255
+ }
1256
+ pkgArg = v;
1257
+ continue;
1258
+ }
964
1259
  if (t === "--version") {
965
1260
  const next = tokens[i + 1];
966
1261
  if (!next || next.startsWith("--")) {
@@ -1022,9 +1317,17 @@ registerCommand({
1022
1317
  ].join("\n");
1023
1318
  }
1024
1319
 
1320
+ // 解析 npm 包名:--pkg 参数 > 配置项 upgradePkg > 默认
1321
+ // 支持 "scope/name"(自动补 @)和 "@scope/name" 两种格式
1322
+ let upgradePkg = pkgArg || ctx.accountConfig?.upgradePkg;
1323
+ if (upgradePkg) {
1324
+ upgradePkg = upgradePkg.trim();
1325
+ if (!upgradePkg.startsWith("@")) upgradePkg = `@${upgradePkg}`;
1326
+ }
1327
+
1025
1328
  // ── --version 指定版本:先校验版本号是否存在 ──
1026
1329
  if (versionArg) {
1027
- const exists = await checkVersionExists(versionArg);
1330
+ const exists = await checkVersionExists(versionArg, upgradePkg);
1028
1331
  if (!exists) {
1029
1332
  return `❌ 版本 ${versionArg} 不存在,请检查版本号`;
1030
1333
  }
@@ -1077,7 +1380,7 @@ registerCommand({
1077
1380
  preUpgradeCredentialBackup(ctx.accountId, ctx.appId);
1078
1381
 
1079
1382
  // 异步执行升级
1080
- const startResult = fireHotUpgrade(targetVersion);
1383
+ const startResult = fireHotUpgrade(targetVersion, upgradePkg, isLocal);
1081
1384
  if (!startResult.ok) {
1082
1385
  _upgrading = false;
1083
1386
  if (startResult.reason === "no-script") {
package/src/types.ts CHANGED
@@ -101,6 +101,13 @@ export interface QQBotAccountConfig {
101
101
  * - "hot-reload":检测到新版本时直接执行 npm 升级脚本进行热更新(默认)
102
102
  */
103
103
  upgradeMode?: "doc" | "hot-reload";
104
+ /**
105
+ * /bot-upgrade 热更新时使用的 npm 包名
106
+ * 支持 "scope/name"(自动补 @)或 "@scope/name" 格式
107
+ * 默认: "@tencent-connect/openclaw-qqbot"
108
+ * 示例: "ryantest/openclaw-qqbot"
109
+ */
110
+ upgradePkg?: string;
104
111
  /**
105
112
  * 出站消息合并回复(debounce)配置
106
113
  * 当短时间内收到多次 deliver 时,将文本合并为一条消息发送,避免消息轰炸
@@ -122,9 +122,12 @@ export async function getUpdateInfo(): Promise<UpdateInfo> {
122
122
  /**
123
123
  * 检查指定版本是否存在于 npm registry
124
124
  * 用于 /bot-upgrade --version 的前置校验
125
+ * @param version 要检查的版本号
126
+ * @param pkgName 可选的包名(如 "@ryantest/openclaw-qqbot"),默认使用内置包名
125
127
  */
126
- export async function checkVersionExists(version: string): Promise<boolean> {
127
- for (const baseUrl of REGISTRIES) {
128
+ export async function checkVersionExists(version: string, pkgName?: string): Promise<boolean> {
129
+ const registries = pkgName ? buildRegistries(pkgName) : REGISTRIES;
130
+ for (const baseUrl of registries) {
128
131
  try {
129
132
  const url = `${baseUrl}/${version}`;
130
133
  const json = await fetchJson(url, 10_000);
@@ -136,6 +139,15 @@ export async function checkVersionExists(version: string): Promise<boolean> {
136
139
  return false;
137
140
  }
138
141
 
142
+ /** 根据自定义包名构建 registry URL 列表 */
143
+ function buildRegistries(pkgName: string): string[] {
144
+ const encoded = encodeURIComponent(pkgName);
145
+ return [
146
+ `https://registry.npmjs.org/${encoded}`,
147
+ `https://registry.npmmirror.com/${encoded}`,
148
+ ];
149
+ }
150
+
139
151
  function compareVersions(a: string, b: string): number {
140
152
  const parse = (v: string) => {
141
153
  const clean = v.replace(/^v/, "");