@celljs/ta-bots 4.15.0 → 4.15.2

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/host/index.mjs +35 -4
  2. package/package.json +3 -3
package/host/index.mjs CHANGED
@@ -61,8 +61,16 @@ class BotStore {
61
61
  async secret(id) {
62
62
  return (await this.allSecrets())[id];
63
63
  }
64
- async tokens() {
65
- return Object.values(await this.allSecrets()).flatMap((secret) => secret.kind === "weixin" ? [secret.token] : []);
64
+ async tokens(bots) {
65
+ const secrets = await this.allSecrets();
66
+ return bots.flatMap((bot) => {
67
+ const secret = secrets[bot.id];
68
+ return secret?.kind === "weixin" ? [secret.token] : [];
69
+ });
70
+ }
71
+ async orphans(bots) {
72
+ const known = new Set(bots.map((bot) => bot.id));
73
+ return Object.entries(await this.allSecrets()).filter(([id]) => !known.has(id));
66
74
  }
67
75
  async setSecret(id, secret) {
68
76
  const secrets = { ...await this.allSecrets() };
@@ -899,6 +907,8 @@ var TaActionUtil;
899
907
  }
900
908
  TaActionUtil2.inside = inside;
901
909
  })(TaActionUtil || (TaActionUtil = {}));
910
+ // ../ta-protocol/esm/common/usage-protocol.js
911
+ var TaUsageServer = Symbol("TaUsageServer");
902
912
  // ../ta-protocol/esm/common/workspace-protocol.js
903
913
  var TaWorkspaceServer = Symbol("TaWorkspaceServer");
904
914
  var TaTerminalServer = Symbol("TaTerminalServer");
@@ -2268,6 +2278,7 @@ class WeixinChannel {
2268
2278
  texts.push(item.text_item.text);
2269
2279
  } else if (item.type === WeixinItemType.VOICE && item.voice_item?.text) {
2270
2280
  texts.push(item.voice_item.text);
2281
+ continue;
2271
2282
  }
2272
2283
  const file = await this.media(item).catch((error) => {
2273
2284
  texts.push(`[${WeixinChannel.label(item.type)}: ${errorText(error)}]`);
@@ -2454,6 +2465,7 @@ class BotService {
2454
2465
  }
2455
2466
  async start() {
2456
2467
  this.bots = await this.store.bots();
2468
+ await this.recover();
2457
2469
  this.status = {};
2458
2470
  for (const bot of this.bots) {
2459
2471
  await this.connect(bot);
@@ -2479,7 +2491,7 @@ class BotService {
2479
2491
  }
2480
2492
  async startWeixinLogin(target) {
2481
2493
  const where = BotTargetUtil.parse(target);
2482
- return this.logins.start(await this.store.tokens(), (signIn) => this.signedIn(signIn, where));
2494
+ return this.logins.start(await this.store.tokens(this.bots), (signIn) => this.signedIn(signIn, where));
2483
2495
  }
2484
2496
  async startWecomLogin(target) {
2485
2497
  const where = BotTargetUtil.parse(target);
@@ -2509,7 +2521,7 @@ class BotService {
2509
2521
  id,
2510
2522
  kind: "wecom",
2511
2523
  name: typeof name === "string" && name.trim() || before?.name || BotServerTexts.defaultName("wecom", this.names()),
2512
- account: botId.length > 10 ? `${botId.slice(0, 6)}…${botId.slice(-4)}` : botId,
2524
+ account: BotService.wecomAccount(botId),
2513
2525
  target: target ?? before?.target,
2514
2526
  createdAt: before?.createdAt ?? Date.now()
2515
2527
  };
@@ -2608,6 +2620,22 @@ class BotService {
2608
2620
  return chat ? [{ botId: bot.id, chat }] : [];
2609
2621
  });
2610
2622
  }
2623
+ async recover() {
2624
+ const lost = await this.store.orphans(this.bots);
2625
+ if (!lost.length) {
2626
+ return;
2627
+ }
2628
+ for (const [id, secret] of lost) {
2629
+ this.bots.push({
2630
+ id,
2631
+ kind: secret.kind,
2632
+ name: BotServerTexts.defaultName(secret.kind, this.names()),
2633
+ account: secret.kind === "weixin" ? BotService.short(secret.userId ?? id.slice("weixin:".length)) : BotService.wecomAccount(secret.botId),
2634
+ createdAt: Date.now()
2635
+ });
2636
+ }
2637
+ await this.store.saveBots(this.bots);
2638
+ }
2611
2639
  async signedIn(signIn, target) {
2612
2640
  const id = `weixin:${signIn.botId}`;
2613
2641
  const secrets = await Promise.all(this.bots.map(async (bot2) => [bot2, await this.store.secret(bot2.id)]));
@@ -2781,6 +2809,9 @@ ${origin.reply ?? ""}`;
2781
2809
  return !seen.has(key) && seen.add(key) !== undefined;
2782
2810
  });
2783
2811
  }
2812
+ static wecomAccount(botId) {
2813
+ return botId.length > 10 ? `${botId.slice(0, 6)}…${botId.slice(-4)}` : botId;
2814
+ }
2784
2815
  static short(account) {
2785
2816
  const name = account.split("@")[0];
2786
2817
  return name.length > 10 ? `${name.slice(0, 4)}…${name.slice(-4)}` : name;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-bots",
3
- "version": "4.15.0",
3
+ "version": "4.15.2",
4
4
  "description": "A pi package for Ta: bots of WeChat (ClawBot) and WeCom connected to the double, an agent or a conversation, messages and files both ways",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -27,8 +27,8 @@
27
27
  "watch": "tsc -b -w --preserveWatchOutput"
28
28
  },
29
29
  "devDependencies": {
30
- "@celljs/ta-protocol": "4.15.0",
31
- "@celljs/ta-web": "4.15.0",
30
+ "@celljs/ta-protocol": "4.15.2",
31
+ "@celljs/ta-web": "4.15.2",
32
32
  "@types/react": "^19.3.0"
33
33
  },
34
34
  "publishConfig": {