@a2hmarket/a2hmarket 0.6.3 → 0.6.5

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/install.mjs +96 -74
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a2hmarket/a2hmarket",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "index.ts",
@@ -308,8 +308,31 @@ async function runUpdate() {
308
308
  process.exit(0);
309
309
  }
310
310
 
311
- // 3. Update plugin
311
+ // 3. Backup credentials before uninstall
312
312
  logStep(3, "更新插件");
313
+ let savedCreds = null;
314
+ // Try openclaw.json plugin config first
315
+ try {
316
+ const cfg = JSON.parse(readFileSync(join(OPENCLAW_DIR, "openclaw.json"), "utf-8"));
317
+ const entry = cfg?.plugins?.entries?.a2hmarket;
318
+ if (entry?.agentId && entry?.agentKey) {
319
+ savedCreds = entry;
320
+ log(` ${CHECK} 凭证已备份 (openclaw.json)`);
321
+ }
322
+ } catch {}
323
+ // Fallback to credentials file
324
+ if (!savedCreds) {
325
+ try {
326
+ if (existsSync(CREDS_FILE)) {
327
+ savedCreds = JSON.parse(readFileSync(CREDS_FILE, "utf-8"));
328
+ log(` ${CHECK} 凭证已备份 (credentials.json)`);
329
+ }
330
+ } catch {}
331
+ }
332
+ if (!savedCreds) {
333
+ log(` ${WARN} 未找到凭证备份,更新后可能需要重新安装`);
334
+ }
335
+
313
336
  try {
314
337
  log(` 卸载旧版本...`);
315
338
  execSync('echo y | openclaw plugins uninstall a2hmarket 2>&1', { encoding: "utf-8", stdio: "pipe" });
@@ -321,6 +344,38 @@ async function runUpdate() {
321
344
  process.exit(1);
322
345
  }
323
346
 
347
+ // Restore credentials to both locations
348
+ if (savedCreds) {
349
+ // Restore credentials file
350
+ mkdirSync(DATA_DIR, { recursive: true });
351
+ const fileData = {
352
+ agent_id: savedCreds.agentId ?? savedCreds.agent_id,
353
+ agent_key: savedCreds.agentKey ?? savedCreds.agent_key,
354
+ api_url: savedCreds.apiUrl ?? savedCreds.api_url ?? "https://api.a2hmarket.ai",
355
+ mqtt_url: savedCreds.mqttUrl ?? savedCreds.mqtt_url ?? "mqtts://post-cn-e4k4o78q702.mqtt.aliyuncs.com:8883",
356
+ };
357
+ if (savedCreds.notify) fileData.notify = savedCreds.notify;
358
+ writeFileSync(CREDS_FILE, JSON.stringify(fileData, null, 2) + "\n");
359
+
360
+ // Restore openclaw.json plugin entry
361
+ try {
362
+ const configPath = join(OPENCLAW_DIR, "openclaw.json");
363
+ const cfg = JSON.parse(readFileSync(configPath, "utf-8"));
364
+ if (!cfg.plugins) cfg.plugins = {};
365
+ if (!cfg.plugins.entries) cfg.plugins.entries = {};
366
+ cfg.plugins.entries.a2hmarket = {
367
+ enabled: true,
368
+ agentId: fileData.agent_id,
369
+ agentKey: fileData.agent_key,
370
+ apiUrl: fileData.api_url,
371
+ mqttUrl: fileData.mqtt_url,
372
+ ...(fileData.notify ? { notify: fileData.notify } : {}),
373
+ };
374
+ writeFileSync(configPath, JSON.stringify(cfg, null, 2) + "\n");
375
+ } catch {}
376
+ log(` ${CHECK} 凭证已恢复`);
377
+ }
378
+
324
379
  // 4. Re-link openclaw module
325
380
  log(` 配置模块依赖...`);
326
381
  try {
@@ -468,32 +523,11 @@ async function main() {
468
523
  log(`\n${BOLD}A2H Market — OpenClaw Plugin${RESET}\n`);
469
524
  log(` 安装: npx -y ${NPM_SPEC} install`);
470
525
  log(` 快速: npx -y ${NPM_SPEC} install --agent ag_xxx:key`);
471
- log(` 非交互: npx -y ${NPM_SPEC} install --agent ag_xxx:key --notify feishu:ou_xxx`);
472
526
  log(` 更新: npx -y ${NPM_SPEC} update`);
473
527
  log(` 卸载: npx -y ${NPM_SPEC} uninstall\n`);
474
- log(` 选项:`);
475
- log(` --agent <id:key> 跳过浏览器授权,直接使用凭证`);
476
- log(` --notify <channel:id> 指定通知渠道(如 feishu:ou_xxx, discord:123456)`);
477
- log(` --yes 跳过所有确认提示\n`);
478
528
  process.exit(0);
479
529
  }
480
530
 
481
- // Parse global flags
482
- const autoYes = args.includes("--yes") || args.includes("-y");
483
-
484
- const notifyFlag = args.find((a) => a.startsWith("--notify"));
485
- const notifyValue = notifyFlag
486
- ? args[args.indexOf(notifyFlag) + 1] ?? notifyFlag.split("=")[1]
487
- : null;
488
- let presetNotify = null;
489
- if (notifyValue && notifyValue.includes(":")) {
490
- const colonIdx = notifyValue.indexOf(":");
491
- presetNotify = {
492
- channel: notifyValue.slice(0, colonIdx),
493
- target: notifyValue.slice(colonIdx + 1),
494
- };
495
- }
496
-
497
531
  log(`\n${BOLD}🏪 A2H Market — OpenClaw Plugin Installer${RESET}\n`);
498
532
 
499
533
  // ── Step 1: Check OpenClaw ─────────────────────────────────────
@@ -528,14 +562,9 @@ async function main() {
528
562
  const existingId = existing.agent_id ?? existing.agentId ?? "";
529
563
  if (existingId) {
530
564
  log(` 已有凭证: ${CYAN}${existingId}${RESET}`);
531
- let reuse = "Y";
532
- if (!autoYes) {
533
- const prompt = createPrompt();
534
- reuse = await prompt.ask("使用现有凭证? (Y/n)", "Y");
535
- prompt.close();
536
- } else {
537
- log(` ${DIM}--yes: 自动使用现有凭证${RESET}`);
538
- }
565
+ const prompt = createPrompt();
566
+ const reuse = await prompt.ask("使用现有凭证? (Y/n)", "Y");
567
+ prompt.close();
539
568
  if (reuse.toLowerCase() !== "n") {
540
569
  agentId = existingId;
541
570
  agentKey = existing.agent_key ?? existing.agentKey ?? "";
@@ -689,59 +718,52 @@ async function main() {
689
718
  mqtt_url: mqttUrl,
690
719
  };
691
720
 
692
- // Configure notification channel
693
- if (presetNotify) {
694
- // --notify flag: skip interactive selection
695
- credsData.notify = presetNotify;
696
- log(` ${CHECK} 通知渠道已配置: ${presetNotify.channel} → ${presetNotify.target}`);
697
- } else {
698
- // Interactive channel selection
699
- const channels = detectChannels();
700
- if (channels.length > 0) {
701
- log(` 检测到 ${channels.length} 个可用渠道:`);
702
- channels.forEach((ch, i) => {
703
- log(` ${CYAN}${i + 1}${RESET}. ${ch.name}`);
704
- });
721
+ // Detect available channels and let user choose
722
+ const channels = detectChannels();
723
+ if (channels.length > 0) {
724
+ log(` 检测到 ${channels.length} 个可用渠道:`);
725
+ channels.forEach((ch, i) => {
726
+ log(` ${CYAN}${i + 1}${RESET}. ${ch.name}`);
727
+ });
705
728
 
706
- const prompt2 = createPrompt();
707
- const choice = await prompt2.ask(
708
- `选择通知渠道 (1-${channels.length},回车跳过)`,
709
- "",
710
- );
711
-
712
- if (choice) {
713
- const idx = parseInt(choice, 10) - 1;
714
- if (idx >= 0 && idx < channels.length) {
715
- const chosen = channels[idx];
716
- let target = "";
717
-
718
- if (chosen.name === "feishu") {
719
- target = detectFeishuTarget() || "";
720
- if (target) {
721
- log(` 检测到飞书用户: ${CYAN}${target}${RESET}`);
722
- } else {
723
- target = await prompt2.ask("输入飞书 open_id (ou_xxx) 或 chat_id (oc_xxx)", "");
724
- }
725
- } else if (chosen.name === "discord") {
726
- target = await prompt2.ask("输入 Discord 频道 ID", "");
727
- } else {
728
- target = await prompt2.ask(`输入 ${chosen.name} 目标 ID`, "");
729
- }
729
+ const prompt2 = createPrompt();
730
+ const choice = await prompt2.ask(
731
+ `选择通知渠道 (1-${channels.length},回车跳过)`,
732
+ "",
733
+ );
734
+
735
+ if (choice) {
736
+ const idx = parseInt(choice, 10) - 1;
737
+ if (idx >= 0 && idx < channels.length) {
738
+ const chosen = channels[idx];
739
+ let target = "";
730
740
 
741
+ if (chosen.name === "feishu") {
742
+ target = detectFeishuTarget() || "";
731
743
  if (target) {
732
- credsData.notify = { channel: chosen.name, target };
733
- log(` ${CHECK} 通知渠道已配置: ${chosen.name} → ${target}`);
744
+ log(` 检测到飞书用户: ${CYAN}${target}${RESET}`);
734
745
  } else {
735
- log(` ${WARN} 未输入目标 ID,跳过通知配置`);
746
+ target = await prompt2.ask("输入飞书 open_id (ou_xxx) 或 chat_id (oc_xxx)", "");
736
747
  }
748
+ } else if (chosen.name === "discord") {
749
+ target = await prompt2.ask("输入 Discord 频道 ID", "");
750
+ } else {
751
+ target = await prompt2.ask(`输入 ${chosen.name} 目标 ID`, "");
752
+ }
753
+
754
+ if (target) {
755
+ credsData.notify = { channel: chosen.name, target };
756
+ log(` ${CHECK} 通知渠道已配置: ${chosen.name} → ${target}`);
757
+ } else {
758
+ log(` ${WARN} 未输入目标 ID,跳过通知配置`);
737
759
  }
738
- } else {
739
- log(` ${DIM}跳过通知配置${RESET}`);
740
760
  }
741
- prompt2.close();
742
761
  } else {
743
- log(` ${DIM}未检测到可用渠道,跳过通知配置${RESET}`);
762
+ log(` ${DIM}跳过通知配置${RESET}`);
744
763
  }
764
+ prompt2.close();
765
+ } else {
766
+ log(` ${DIM}未检测到可用渠道,跳过通知配置${RESET}`);
745
767
  }
746
768
 
747
769
  // Save credentials file (fallback for dev mode)