@gloablehive/ipad-wechat-plugin 2.1.0 → 2.2.0

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 (3) hide show
  1. package/dist/index.js +26 -3
  2. package/index.ts +30 -3
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -24,12 +24,15 @@ import { ipadWeChatPlugin, handleInboundMessage, getCacheManagerReady, getCacheM
24
24
  import { getIPadClient } from "./src/client-pool.js";
25
25
  /** Read OpenClaw main config from disk so standalone server has full cfg */
26
26
  let _cfgCache = null;
27
+ let _cfgCacheTs = 0;
28
+ const CFG_CACHE_TTL_MS = 30_000; // re-read config every 30s to pick up binding changes
27
29
  function loadOpenClawConfig() {
28
- if (_cfgCache)
30
+ if (_cfgCache && (Date.now() - _cfgCacheTs) < CFG_CACHE_TTL_MS)
29
31
  return _cfgCache;
30
32
  try {
31
33
  const p = join(homedir(), ".openclaw", "openclaw.json");
32
34
  _cfgCache = JSON.parse(readFileSync(p, "utf-8"));
35
+ _cfgCacheTs = Date.now();
33
36
  return _cfgCache;
34
37
  }
35
38
  catch (err) {
@@ -37,6 +40,24 @@ function loadOpenClawConfig() {
37
40
  return null;
38
41
  }
39
42
  }
43
+ /**
44
+ * Resolve agentId from config bindings for a given channel/accountId.
45
+ * Falls back to "main" if no binding matches.
46
+ */
47
+ function resolveAgentFromBindings(cfg, channel, accountId) {
48
+ const bindings = cfg?.bindings ?? [];
49
+ // Try specific match first (channel + accountId)
50
+ if (accountId) {
51
+ const specific = bindings.find((b) => b.match?.channel === channel && b.match?.accountId === accountId);
52
+ if (specific?.agentId)
53
+ return specific.agentId;
54
+ }
55
+ // Then try channel-only match
56
+ const channelOnly = bindings.find((b) => b.match?.channel === channel && !b.match?.accountId);
57
+ if (channelOnly?.agentId)
58
+ return channelOnly.agentId;
59
+ return "main";
60
+ }
40
61
  // ── Gateway WebSocket dispatch ──
41
62
  const GATEWAY_PORT = parseInt(process.env.OPENCLAW_GATEWAY_PORT || "18789", 10);
42
63
  let _gwReqId = 0;
@@ -446,10 +467,12 @@ export default defineChannelPluginEntry({
446
467
  const isChatroom = !!msg.roomId;
447
468
  const peerId = isChatroom ? msg.roomId : (msg.fromUser || msg.toUser || "");
448
469
  const peerKind = isChatroom ? "group" : "direct";
449
- let sessionKey = `agent:main:ipad-wechat:${peerKind}:${peerId}`;
470
+ const resolvedAgent = resolveAgentFromBindings(cfg, "ipad-wechat");
471
+ console.log(`[iPad WeChat] Resolved agent: ${resolvedAgent} (from bindings)`);
472
+ let sessionKey = `agent:${resolvedAgent}:ipad-wechat:${peerKind}:${peerId}`;
450
473
  try {
451
474
  const route = buildChannelOutboundSessionRoute({
452
- cfg, agentId: "main", channel: "ipad-wechat",
475
+ cfg, agentId: resolvedAgent, channel: "ipad-wechat",
453
476
  peer: { kind: peerKind, id: peerId },
454
477
  chatType: peerKind,
455
478
  from: msg.toUser || "", to: msg.fromUser || "",
package/index.ts CHANGED
@@ -27,11 +27,14 @@ import type { WebhookPayload } from "./src/client.js";
27
27
 
28
28
  /** Read OpenClaw main config from disk so standalone server has full cfg */
29
29
  let _cfgCache: any = null;
30
+ let _cfgCacheTs = 0;
31
+ const CFG_CACHE_TTL_MS = 30_000; // re-read config every 30s to pick up binding changes
30
32
  function loadOpenClawConfig(): any {
31
- if (_cfgCache) return _cfgCache;
33
+ if (_cfgCache && (Date.now() - _cfgCacheTs) < CFG_CACHE_TTL_MS) return _cfgCache;
32
34
  try {
33
35
  const p = join(homedir(), ".openclaw", "openclaw.json");
34
36
  _cfgCache = JSON.parse(readFileSync(p, "utf-8"));
37
+ _cfgCacheTs = Date.now();
35
38
  return _cfgCache;
36
39
  } catch (err) {
37
40
  console.error("[iPad WeChat] Failed to load openclaw.json:", err);
@@ -39,6 +42,27 @@ function loadOpenClawConfig(): any {
39
42
  }
40
43
  }
41
44
 
45
+ /**
46
+ * Resolve agentId from config bindings for a given channel/accountId.
47
+ * Falls back to "main" if no binding matches.
48
+ */
49
+ function resolveAgentFromBindings(cfg: any, channel: string, accountId?: string): string {
50
+ const bindings: any[] = cfg?.bindings ?? [];
51
+ // Try specific match first (channel + accountId)
52
+ if (accountId) {
53
+ const specific = bindings.find(
54
+ (b: any) => b.match?.channel === channel && b.match?.accountId === accountId
55
+ );
56
+ if (specific?.agentId) return specific.agentId;
57
+ }
58
+ // Then try channel-only match
59
+ const channelOnly = bindings.find(
60
+ (b: any) => b.match?.channel === channel && !b.match?.accountId
61
+ );
62
+ if (channelOnly?.agentId) return channelOnly.agentId;
63
+ return "main";
64
+ }
65
+
42
66
  // ── Gateway WebSocket dispatch ──
43
67
  const GATEWAY_PORT = parseInt(process.env.OPENCLAW_GATEWAY_PORT || "18789", 10);
44
68
  let _gwReqId = 0;
@@ -463,10 +487,13 @@ export default defineChannelPluginEntry({
463
487
  const peerId = isChatroom ? (msg as any).roomId : (msg.fromUser || msg.toUser || "");
464
488
  const peerKind = isChatroom ? "group" : "direct";
465
489
 
466
- let sessionKey = `agent:main:ipad-wechat:${peerKind}:${peerId}`;
490
+ const resolvedAgent = resolveAgentFromBindings(cfg, "ipad-wechat");
491
+ console.log(`[iPad WeChat] Resolved agent: ${resolvedAgent} (from bindings)`);
492
+
493
+ let sessionKey = `agent:${resolvedAgent}:ipad-wechat:${peerKind}:${peerId}`;
467
494
  try {
468
495
  const route = buildChannelOutboundSessionRoute({
469
- cfg, agentId: "main", channel: "ipad-wechat",
496
+ cfg, agentId: resolvedAgent, channel: "ipad-wechat",
470
497
  peer: { kind: peerKind as any, id: peerId },
471
498
  chatType: peerKind as any,
472
499
  from: msg.toUser || "", to: msg.fromUser || "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gloablehive/ipad-wechat-plugin",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "type": "module",
5
5
  "description": "OpenClaw channel plugin for iPad WeChat protocol - enables sending/receiving WeChat messages through JuHeBot API",
6
6
  "main": "index.ts",