@wecom/wecom-openclaw-plugin 2026.4.3-beta.0 → 2026.4.7-beta.1

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.
@@ -15,6 +15,8 @@ import { getWeComWebSocket } from "../state-manager.js";
15
15
  import { MCP_GET_CONFIG_CMD, MCP_CONFIG_FETCH_TIMEOUT_MS } from "../const.js";
16
16
  import { withTimeout } from "../timeout.js";
17
17
  import { PLUGIN_VERSION } from "../version.js";
18
+ import { getWeComRuntime } from "../runtime.js";
19
+ import { resolveDefaultWeComAccountId, listWeComAccountIds, resolveWeComAccountMulti, } from "../accounts.js";
18
20
  // ============================================================================
19
21
  // 内部状态
20
22
  // ============================================================================
@@ -77,16 +79,75 @@ const inflightInitRequests = new Map();
77
79
  // ============================================================================
78
80
  // MCP 配置拉取与缓存
79
81
  // ============================================================================
82
+ /**
83
+ * 判断账户是否为长连接机器人(通过 botId + secret 配置)
84
+ */
85
+ function isWebSocketBot(cfg, accountId) {
86
+ const resolved = resolveWeComAccountMulti({ cfg, accountId });
87
+ return Boolean(resolved.botId?.trim() && resolved.secret?.trim());
88
+ }
89
+ /**
90
+ * 解析用于 MCP 调用的账户 ID
91
+ *
92
+ * 优先级:
93
+ * 1. 默认账户(如果是长连接机器人)
94
+ * 2. 第一个长连接机器人账户
95
+ * 3. 无可用账户时返回 null
96
+ */
97
+ function resolveMcpAccountId(cfg) {
98
+ const defaultAccountId = resolveDefaultWeComAccountId(cfg);
99
+ // 优先使用默认账户
100
+ if (isWebSocketBot(cfg, defaultAccountId)) {
101
+ return defaultAccountId;
102
+ }
103
+ // 默认账户非长连接,查找第一个长连接账户
104
+ const allIds = listWeComAccountIds(cfg);
105
+ for (const id of allIds) {
106
+ if (id !== defaultAccountId && isWebSocketBot(cfg, id)) {
107
+ console.log(`${LOG_TAG} 默认账户 "${defaultAccountId}" 非长连接模式,使用账户 "${id}" 获取 MCP 配置`);
108
+ return id;
109
+ }
110
+ }
111
+ return null;
112
+ }
113
+ /**
114
+ * 根据配置解析当前用于 MCP 的账户 ID
115
+ *
116
+ * 通过 PluginRuntime 读取全局配置,按以下优先级选择账户:
117
+ * 1. 默认账户(如果配置了 botId + secret)
118
+ * 2. 第一个配置了 botId + secret 的长连接账户
119
+ * 3. 无可用长连接账户时回退到默认账户(会导致后续 WSClient 获取失败)
120
+ */
121
+ function resolveCurrentAccountId() {
122
+ try {
123
+ const core = getWeComRuntime();
124
+ const cfg = core.config.loadConfig();
125
+ const mcpAccountId = resolveMcpAccountId(cfg);
126
+ if (mcpAccountId) {
127
+ return mcpAccountId;
128
+ }
129
+ // 无长连接账户,给出友好提示
130
+ console.warn(`${LOG_TAG} 未找到长连接模式的机器人账户(需配置 botId + secret),MCP 功能不可用`);
131
+ return resolveDefaultWeComAccountId(cfg);
132
+ }
133
+ catch {
134
+ // runtime 未初始化时回退到 "default"
135
+ return DEFAULT_ACCOUNT_ID;
136
+ }
137
+ }
80
138
  /**
81
139
  * 通过 WSClient 拉取指定 category 的 MCP 完整配置
82
140
  *
141
+ * 自动根据 defaultAccount 配置获取对应账户的 WSClient,无需手动传入 accountId。
142
+ *
83
143
  * @param category - MCP 品类名称,如 doc、contact
84
144
  * @returns 完整的 response.body 配置对象(至少包含 url 字段)
85
145
  */
86
146
  async function fetchMcpConfig(category) {
87
- const wsClient = getWeComWebSocket(DEFAULT_ACCOUNT_ID);
147
+ const accountId = resolveCurrentAccountId();
148
+ const wsClient = getWeComWebSocket(accountId);
88
149
  if (!wsClient) {
89
- throw new Error("WSClient 未连接,无法拉取 MCP 配置");
150
+ throw new Error(`WSClient 未连接 (accountId="${accountId}"),无法拉取 MCP 配置`);
90
151
  }
91
152
  const reqId = generateReqId("mcp_config");
92
153
  const response = await withTimeout(wsClient.reply({ headers: { req_id: reqId } }, { biz_type: category, plugin_version: PLUGIN_VERSION }, MCP_GET_CONFIG_CMD), MCP_CONFIG_FETCH_TIMEOUT_MS, `MCP config fetch for "${category}" timed out after ${MCP_CONFIG_FETCH_TIMEOUT_MS}ms`);
@@ -99,7 +160,7 @@ async function fetchMcpConfig(category) {
99
160
  if (!body?.url) {
100
161
  throw new Error(`MCP 配置响应缺少 url 字段 (category="${category}")`);
101
162
  }
102
- console.log(`${LOG_TAG} 配置拉取成功 (category="${category}")`);
163
+ console.log(`${LOG_TAG} 配置拉取成功 (accountId="${accountId}", category="${category}")`);
103
164
  return body;
104
165
  }
105
166
  /**
@@ -353,7 +353,7 @@ async function routeAndDispatchMessage(params) {
353
353
  }
354
354
  },
355
355
  deliver: async (payload, info) => {
356
- runtime.log?.(`[openclaw -> plugin] kind=${info.kind}, text=${payload.text ?? ''}, mediaUrl=${payload.mediaUrl ?? ''}, mediaUrls=${JSON.stringify(payload.mediaUrls ?? [])}`);
356
+ runtime.log?.(`[openclaw -> plugin] kind=${info.kind}, text=${JSON.stringify(payload.text ?? '')}, mediaUrl=${payload.mediaUrl ?? ''}, mediaUrls=${JSON.stringify(payload.mediaUrls ?? [])}`);
357
357
  // 累积文本
358
358
  if (payload.text) {
359
359
  state.accumulatedText += `${payload.text || ''}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wecom/wecom-openclaw-plugin",
3
- "version": "2026.4.3-beta.0",
3
+ "version": "2026.4.7-beta.1",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -61,7 +61,7 @@
61
61
  }
62
62
  },
63
63
  "dependencies": {
64
- "@wecom/aibot-node-sdk": "1.0.6-beta.2",
64
+ "@wecom/aibot-node-sdk": "^1.0.6",
65
65
  "fast-xml-parser": "^5.5.9",
66
66
  "file-type": "^21.3.0",
67
67
  "undici": "^7.24.6",