@mocrane/wecom 2026.3.14 → 2026.3.19

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 (41) hide show
  1. package/index.ts +37 -0
  2. package/package.json +4 -3
  3. package/skills/wecom-contact-lookup/SKILL.md +162 -0
  4. package/skills/wecom-doc-manager/SKILL.md +64 -0
  5. package/skills/wecom-doc-manager/references/api-create-doc.md +56 -0
  6. package/skills/wecom-doc-manager/references/api-edit-doc-content.md +68 -0
  7. package/skills/wecom-doc-manager/references/api-export-document.md +88 -0
  8. package/skills/wecom-edit-todo/SKILL.md +249 -0
  9. package/skills/wecom-get-todo-detail/SKILL.md +143 -0
  10. package/skills/wecom-get-todo-list/SKILL.md +127 -0
  11. package/skills/wecom-meeting-create/SKILL.md +158 -0
  12. package/skills/wecom-meeting-create/references/example-full.md +30 -0
  13. package/skills/wecom-meeting-create/references/example-reminder.md +46 -0
  14. package/skills/wecom-meeting-create/references/example-security.md +22 -0
  15. package/skills/wecom-meeting-manage/SKILL.md +136 -0
  16. package/skills/wecom-meeting-query/SKILL.md +330 -0
  17. package/skills/wecom-preflight/SKILL.md +141 -0
  18. package/skills/wecom-schedule/SKILL.md +159 -0
  19. package/skills/wecom-schedule/references/api-check-availability.md +56 -0
  20. package/skills/wecom-schedule/references/api-create-schedule.md +38 -0
  21. package/skills/wecom-schedule/references/api-get-schedule-detail.md +81 -0
  22. package/skills/wecom-schedule/references/api-update-schedule.md +30 -0
  23. package/skills/wecom-schedule/references/ref-reminders.md +24 -0
  24. package/skills/wecom-smartsheet-data/SKILL.md +71 -0
  25. package/skills/wecom-smartsheet-data/references/api-get-records.md +61 -0
  26. package/skills/wecom-smartsheet-data/references/cell-value-formats.md +120 -0
  27. package/skills/wecom-smartsheet-schema/SKILL.md +92 -0
  28. package/skills/wecom-smartsheet-schema/references/field-types.md +43 -0
  29. package/src/agent/handler.ts +105 -14
  30. package/src/mcp/index.ts +7 -0
  31. package/src/mcp/schema.ts +108 -0
  32. package/src/mcp/tool.ts +226 -0
  33. package/src/mcp/transport.ts +561 -0
  34. package/src/media/const.ts +24 -0
  35. package/src/media/index.ts +15 -0
  36. package/src/media/uploader.ts +240 -0
  37. package/src/monitor.ts +293 -12
  38. package/src/outbound.ts +116 -46
  39. package/src/types/index.ts +1 -0
  40. package/src/types/message.ts +10 -1
  41. package/src/ws-adapter.ts +4 -0
@@ -0,0 +1,561 @@
1
+ /**
2
+ * MCP Streamable HTTP 传输层模块
3
+ *
4
+ * 负责:
5
+ * - MCP JSON-RPC over HTTP 通信(发送请求、解析响应)
6
+ * - Streamable HTTP session 生命周期管理(initialize 握手 → Mcp-Session-Id 维护 → 失效重建)
7
+ * - 自动检测无状态 Server:如果 initialize 响应未返回 Mcp-Session-Id,
8
+ * 则标记为无状态模式,后续请求跳过握手和 session 管理
9
+ * - SSE 流式响应解析
10
+ * - MCP 配置运行时缓存(通过 WSClient 拉取 URL 并缓存在内存中)
11
+ */
12
+
13
+ import { readFileSync } from "node:fs";
14
+ import { dirname, resolve } from "node:path";
15
+ import { fileURLToPath } from "node:url";
16
+ import { generateReqId } from "@wecom/aibot-node-sdk";
17
+ import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk";
18
+ import { getWsClient } from "../ws-adapter.js";
19
+ import { withTimeout } from "../timeout.js";
20
+
21
+ // ============================================================================
22
+ // 常量
23
+ // ============================================================================
24
+
25
+ /** 获取 MCP 配置的 WebSocket 命令 */
26
+ const MCP_GET_CONFIG_CMD = "aibot_get_mcp_config";
27
+
28
+ /** MCP 配置拉取超时时间(毫秒) */
29
+ const MCP_CONFIG_FETCH_TIMEOUT_MS = 15_000;
30
+
31
+ /** 从 package.json 读取插件版本号 */
32
+ const getPluginVersion = (): string => {
33
+ try {
34
+ const currentDir = dirname(fileURLToPath(import.meta.url));
35
+ const pkgPath = resolve(currentDir, "..", "..", "package.json");
36
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf-8")) as { version?: string };
37
+ return pkg.version ?? "";
38
+ } catch {
39
+ return "";
40
+ }
41
+ };
42
+
43
+ const PLUGIN_VERSION = getPluginVersion();
44
+
45
+ // ============================================================================
46
+ // 类型定义
47
+ // ============================================================================
48
+
49
+ /** MCP JSON-RPC 请求体 */
50
+ interface JsonRpcRequest {
51
+ jsonrpc: "2.0";
52
+ id?: string;
53
+ method: string;
54
+ params?: Record<string, unknown>;
55
+ }
56
+
57
+ /** MCP JSON-RPC 响应体 */
58
+ interface JsonRpcResponse {
59
+ jsonrpc: "2.0";
60
+ id: number | string;
61
+ result?: unknown;
62
+ error?: {
63
+ code: number;
64
+ message: string;
65
+ data?: unknown;
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Streamable HTTP 会话信息
71
+ *
72
+ * 每个 MCP Server category 维护一个独立的会话,包含:
73
+ * - sessionId: 服务端通过 Mcp-Session-Id 响应头返回的会话标识
74
+ * - initialized: 是否已完成 initialize 握手
75
+ * - stateless: 服务端未返回 Mcp-Session-Id 时标记为无状态模式,后续请求跳过 session 管理
76
+ */
77
+ interface McpSession {
78
+ sessionId: string | null;
79
+ initialized: boolean;
80
+ stateless: boolean;
81
+ }
82
+
83
+ // ============================================================================
84
+ // 内部状态
85
+ // ============================================================================
86
+
87
+ /** HTTP 请求超时时间(毫秒) */
88
+ const HTTP_REQUEST_TIMEOUT_MS = 30_000;
89
+
90
+ /** 日志前缀 */
91
+ const LOG_TAG = "[mcp]";
92
+
93
+ /**
94
+ * MCP JSON-RPC 错误
95
+ *
96
+ * 携带服务端返回的 JSON-RPC error.code,
97
+ * 用于上层按错误码进行差异化处理(如特定错误码触发缓存清理)。
98
+ */
99
+ export class McpRpcError extends Error {
100
+ constructor(
101
+ public readonly code: number,
102
+ message: string,
103
+ public readonly data?: unknown,
104
+ ) {
105
+ super(message);
106
+ this.name = "McpRpcError";
107
+ }
108
+ }
109
+
110
+ /**
111
+ * MCP HTTP 错误
112
+ *
113
+ * 携带 HTTP 状态码,用于精确判断 session 失效(404)等场景,
114
+ * 避免通过字符串匹配 "404" 导致的误判。
115
+ */
116
+ export class McpHttpError extends Error {
117
+ constructor(
118
+ public readonly statusCode: number,
119
+ message: string,
120
+ ) {
121
+ super(message);
122
+ this.name = "McpHttpError";
123
+ }
124
+ }
125
+
126
+ /**
127
+ * 需要清理缓存的 JSON-RPC 错误码集合
128
+ *
129
+ * 当 MCP Server 返回以下错误码时,说明服务端状态已发生变化(如配置变更、
130
+ * 服务重启等),需要清理对应 category 的全部缓存,确保下次请求重新
131
+ * 拉取配置并重建会话。
132
+ *
133
+ * - -32001: 服务不可用(Server Unavailable)
134
+ * - -32002: 配置已变更(Config Changed)
135
+ * - -32003: 认证失败(Auth Failed)
136
+ */
137
+ const CACHE_CLEAR_ERROR_CODES = new Set([-32001, -32002, -32003]);
138
+
139
+ /** MCP 配置缓存:category → response.body(完整配置) */
140
+ const mcpConfigCache = new Map<string, Record<string, unknown>>();
141
+
142
+ /** Streamable HTTP 会话缓存:category → session */
143
+ const mcpSessionCache = new Map<string, McpSession>();
144
+
145
+ /** 已确认为无状态的 MCP Server 品类集合(跳过后续握手) */
146
+ const statelessCategories = new Set<string>();
147
+
148
+ /** 正在进行中的 initialize 请求(防止并发重复初始化),key 为 category */
149
+ const inflightInitRequests = new Map<string, Promise<McpSession>>();
150
+
151
+ // ============================================================================
152
+ // MCP 配置拉取与缓存
153
+ // ============================================================================
154
+
155
+ /**
156
+ * 通过 WSClient 拉取指定 category 的 MCP 完整配置
157
+ *
158
+ * @param category - MCP 品类名称,如 doc、contact
159
+ * @returns 完整的 response.body 配置对象(至少包含 url 字段)
160
+ */
161
+ async function fetchMcpConfig(category: string): Promise<Record<string, unknown>> {
162
+ const wsClient = getWsClient(DEFAULT_ACCOUNT_ID);
163
+ if (!wsClient) {
164
+ throw new Error("WSClient 未连接,无法拉取 MCP 配置");
165
+ }
166
+
167
+ const reqId = generateReqId("mcp_config");
168
+
169
+ const response = await withTimeout(
170
+ wsClient.reply(
171
+ { headers: { req_id: reqId } },
172
+ { biz_type: category, plugin_version: PLUGIN_VERSION },
173
+ MCP_GET_CONFIG_CMD,
174
+ ),
175
+ MCP_CONFIG_FETCH_TIMEOUT_MS,
176
+ `MCP config fetch for "${category}" timed out after ${MCP_CONFIG_FETCH_TIMEOUT_MS}ms`,
177
+ );
178
+
179
+ if (response.errcode !== undefined && response.errcode !== 0) {
180
+ const errMsg = `MCP 配置请求失败: errcode=${response.errcode}, errmsg=${response.errmsg ?? "unknown"}`;
181
+ console.error(`${LOG_TAG} ${errMsg}`);
182
+ throw new Error(errMsg);
183
+ }
184
+
185
+ const body = response.body as { url?: string } | undefined;
186
+ if (!body?.url) {
187
+ throw new Error(
188
+ `MCP 配置响应缺少 url 字段 (category="${category}")`,
189
+ );
190
+ }
191
+
192
+ console.log(`${LOG_TAG} 配置拉取成功 (category="${category}")`);
193
+ return body as Record<string, unknown>;
194
+ }
195
+
196
+ /**
197
+ * 获取指定品类的 MCP Server URL
198
+ *
199
+ * 优先从内存缓存中读取,未命中时通过 WSClient 拉取并缓存。
200
+ *
201
+ * @param category - MCP 品类名称
202
+ * @returns MCP Server URL
203
+ */
204
+ async function getMcpUrl(category: string): Promise<string> {
205
+ // 查内存缓存
206
+ const cached = mcpConfigCache.get(category);
207
+ if (cached) return cached.url as string;
208
+
209
+ // 缓存未命中,通过 WSClient 拉取
210
+ const body = await fetchMcpConfig(category);
211
+
212
+ // 写入缓存
213
+ mcpConfigCache.set(category, body);
214
+
215
+ console.log(`${LOG_TAG} getMcpUrl ${category}: ${body.url}`);
216
+
217
+ return body.url as string;
218
+ }
219
+
220
+ // ============================================================================
221
+ // HTTP 底层通信
222
+ // ============================================================================
223
+
224
+ /**
225
+ * 发送原始 HTTP 请求到 MCP Server(底层方法)
226
+ *
227
+ * 自动携带 Mcp-Session-Id 请求头(如果有),
228
+ * 并从响应头中更新 sessionId。
229
+ */
230
+ async function sendRawJsonRpc(
231
+ url: string,
232
+ session: McpSession,
233
+ body: JsonRpcRequest,
234
+ ): Promise<{ response: Response; rpcResult: unknown; newSessionId: string | null }> {
235
+ const controller = new AbortController();
236
+ const timeoutId = setTimeout(() => controller.abort(), HTTP_REQUEST_TIMEOUT_MS);
237
+
238
+ const headers: Record<string, string> = {
239
+ "Content-Type": "application/json",
240
+ Accept: "application/json, text/event-stream",
241
+ };
242
+ // Streamable HTTP:携带会话 ID
243
+ if (session.sessionId) {
244
+ headers["Mcp-Session-Id"] = session.sessionId;
245
+ }
246
+
247
+ let response: Response;
248
+ try {
249
+ response = await fetch(url, {
250
+ method: "POST",
251
+ headers,
252
+ body: JSON.stringify(body),
253
+ signal: controller.signal,
254
+ });
255
+ } catch (err) {
256
+ if (err instanceof DOMException && err.name === "AbortError") {
257
+ throw new Error(`MCP 请求超时 (${HTTP_REQUEST_TIMEOUT_MS}ms)`);
258
+ }
259
+ throw new Error(`MCP 网络请求失败: ${err instanceof Error ? err.message : String(err)}`);
260
+ } finally {
261
+ clearTimeout(timeoutId);
262
+ }
263
+
264
+ // 从响应头提取新的 sessionId(不直接修改入参,由调用方决定如何更新)
265
+ const newSessionId = response.headers.get("mcp-session-id");
266
+
267
+ if (!response.ok) {
268
+ throw new McpHttpError(
269
+ response.status,
270
+ `MCP HTTP 请求失败: ${response.status} ${response.statusText}`,
271
+ );
272
+ }
273
+
274
+ // Streamable HTTP:notification 响应可能无响应体(204 或 content-length: 0)
275
+ const contentLength = response.headers.get("content-length");
276
+ if (response.status === 204 || contentLength === "0") {
277
+ return { response, rpcResult: undefined, newSessionId };
278
+ }
279
+
280
+ const contentType = response.headers.get("content-type") ?? "";
281
+
282
+ // 处理 SSE 流式响应
283
+ if (contentType.includes("text/event-stream")) {
284
+ return { response, rpcResult: await parseSseResponse(response), newSessionId };
285
+ }
286
+
287
+ // 普通 JSON 响应 — 先读取文本,防止空内容导致 JSON.parse 报错
288
+ const text = await response.text();
289
+ if (!text.trim()) {
290
+ return { response, rpcResult: undefined, newSessionId };
291
+ }
292
+
293
+ const rpc = JSON.parse(text) as JsonRpcResponse;
294
+ if (rpc.error) {
295
+ throw new McpRpcError(
296
+ rpc.error.code,
297
+ `MCP 调用错误 [${rpc.error.code}]: ${rpc.error.message}`,
298
+ rpc.error.data,
299
+ );
300
+ }
301
+ return { response, rpcResult: rpc.result, newSessionId };
302
+ }
303
+
304
+ // ============================================================================
305
+ // Session 管理
306
+ // ============================================================================
307
+
308
+ /**
309
+ * 对指定 URL 执行 Streamable HTTP 的 initialize 握手
310
+ *
311
+ * 发送 initialize → 接收 serverInfo → 发送 initialized 通知。
312
+ * 如果服务端未返回 Mcp-Session-Id,则标记为无状态模式,后续请求跳过 session 管理。
313
+ */
314
+ async function initializeSession(url: string, category: string): Promise<McpSession> {
315
+ const session: McpSession = { sessionId: null, initialized: false, stateless: false };
316
+
317
+ console.log(`${LOG_TAG} 开始 initialize 握手 (category="${category}")`);
318
+
319
+ // 1. 发送 initialize 请求
320
+ const initBody: JsonRpcRequest = {
321
+ jsonrpc: "2.0",
322
+ id: generateReqId("mcp_init"),
323
+ method: "initialize",
324
+ params: {
325
+ protocolVersion: "2025-03-26",
326
+ capabilities: {},
327
+ clientInfo: { name: "wecom_mcp", version: "1.0.0" },
328
+ },
329
+ };
330
+
331
+ const { newSessionId: initSessionId } = await sendRawJsonRpc(url, session, initBody);
332
+
333
+ // 用返回的 newSessionId 更新 session(不再依赖副作用修改)
334
+ if (initSessionId) {
335
+ session.sessionId = initSessionId;
336
+ }
337
+
338
+ // 检查服务端是否返回了 Mcp-Session-Id
339
+ // 如果没有返回,说明该 Server 是无状态实现,无需维护 session
340
+ if (!session.sessionId) {
341
+ session.stateless = true;
342
+ session.initialized = true;
343
+ statelessCategories.add(category);
344
+ mcpSessionCache.set(category, session);
345
+ console.log(`${LOG_TAG} 无状态 Server 确认 (category="${category}")`);
346
+ return session;
347
+ }
348
+
349
+ // 2. 发送 initialized 通知(JSON-RPC notification 不带 id 字段)
350
+ const notifyBody: JsonRpcRequest = {
351
+ jsonrpc: "2.0",
352
+ method: "notifications/initialized",
353
+ };
354
+ // initialized 通知不需要等待响应,但 Streamable HTTP 要求通过 POST 发送
355
+ const { newSessionId: notifySessionId } = await sendRawJsonRpc(url, session, notifyBody);
356
+
357
+ // 如果 initialized 通知的响应也携带了 sessionId,以最新的为准
358
+ if (notifySessionId) {
359
+ session.sessionId = notifySessionId;
360
+ }
361
+
362
+ session.initialized = true;
363
+ mcpSessionCache.set(category, session);
364
+ console.log(`${LOG_TAG} 有状态 Session 建立成功 (category="${category}", sessionId="${session.sessionId}")`);
365
+ return session;
366
+ }
367
+
368
+ /**
369
+ * 获取或创建指定 URL 的 MCP 会话
370
+ *
371
+ * - 已确认无状态的 category:直接返回空 session,跳过握手
372
+ * - 已有可用有状态会话:直接返回缓存
373
+ * - 其他情况:执行 initialize 握手,并发请求会被合并
374
+ */
375
+ async function getOrCreateSession(url: string, category: string): Promise<McpSession> {
376
+ // 已确认为无状态的 Server,直接返回空 session 跳过握手
377
+ if (statelessCategories.has(category)) {
378
+ const cached = mcpSessionCache.get(category);
379
+ if (cached) return cached;
380
+ // 首次发现被清除(理论上不会走到这里),重新走握手探测
381
+ }
382
+
383
+ const cached = mcpSessionCache.get(category);
384
+ if (cached?.initialized) return cached;
385
+
386
+ // 防止并发重复初始化
387
+ const inflight = inflightInitRequests.get(category);
388
+ if (inflight) return inflight;
389
+
390
+ const promise = initializeSession(url, category).finally(() => {
391
+ inflightInitRequests.delete(category);
392
+ });
393
+ inflightInitRequests.set(category, promise);
394
+ return promise;
395
+ }
396
+
397
+ // ============================================================================
398
+ // SSE 解析
399
+ // ============================================================================
400
+
401
+ /**
402
+ * 解析 SSE 流式响应,提取最终的 JSON-RPC result
403
+ *
404
+ * 按照 SSE 规范,同一事件中的多个 `data:` 行会用换行符拼接。
405
+ * 空行分隔不同事件,取最后一个完整事件的数据。
406
+ */
407
+ async function parseSseResponse(response: Response): Promise<unknown> {
408
+ const text = await response.text();
409
+ const lines = text.split("\n");
410
+
411
+ // 按 SSE 规范解析:空行分隔事件,同一事件内的 data 行用换行拼接
412
+ let currentDataParts: string[] = [];
413
+ let lastEventData = "";
414
+
415
+ for (const line of lines) {
416
+ if (line.startsWith("data: ")) {
417
+ currentDataParts.push(line.slice(6));
418
+ } else if (line.startsWith("data:")) {
419
+ // data: 后无空格时,值为空字符串
420
+ currentDataParts.push(line.slice(5));
421
+ } else if (line.trim() === "" && currentDataParts.length > 0) {
422
+ // 空行表示事件结束,拼接所有 data 行
423
+ lastEventData = currentDataParts.join("\n").trim();
424
+ currentDataParts = [];
425
+ }
426
+ }
427
+
428
+ // 处理最后一个未以空行结尾的事件
429
+ if (currentDataParts.length > 0) {
430
+ lastEventData = currentDataParts.join("\n").trim();
431
+ }
432
+
433
+ if (!lastEventData) {
434
+ throw new Error("SSE 响应中未包含有效数据");
435
+ }
436
+
437
+ try {
438
+ const rpc = JSON.parse(lastEventData) as JsonRpcResponse;
439
+ if (rpc.error) {
440
+ throw new McpRpcError(
441
+ rpc.error.code,
442
+ `MCP 调用错误 [${rpc.error.code}]: ${rpc.error.message}`,
443
+ rpc.error.data,
444
+ );
445
+ }
446
+ return rpc.result;
447
+ } catch (err) {
448
+ if (err instanceof SyntaxError) {
449
+ throw new Error(`SSE 响应解析失败: ${lastEventData.slice(0, 200)}`);
450
+ }
451
+ throw err;
452
+ }
453
+ }
454
+
455
+ // ============================================================================
456
+ // 公共 API
457
+ // ============================================================================
458
+
459
+ /**
460
+ * 清理指定品类的所有 MCP 缓存(配置、会话、无状态标记)
461
+ *
462
+ * 当 MCP Server 返回特定错误码时调用,确保下次请求重新拉取配置并重建会话。
463
+ *
464
+ * @param category - MCP 品类名称
465
+ */
466
+ export function clearCategoryCache(category: string): void {
467
+ console.log(`${LOG_TAG} 清理缓存 (category="${category}")`);
468
+ mcpConfigCache.delete(category);
469
+ mcpSessionCache.delete(category);
470
+ statelessCategories.delete(category);
471
+ inflightInitRequests.delete(category);
472
+ }
473
+
474
+ /** tools/list 返回的工具描述 */
475
+ export interface McpToolInfo {
476
+ name: string;
477
+ description?: string;
478
+ inputSchema?: Record<string, unknown>;
479
+ }
480
+
481
+ /**
482
+ * 发送 JSON-RPC 请求到 MCP Server(Streamable HTTP 协议)
483
+ *
484
+ * 自动管理 session 生命周期:
485
+ * - 无状态 Server:跳过 session 管理,直接发送请求
486
+ * - 有状态 Server:首次调用先执行 initialize 握手,session 失效(404)时自动重建并重试
487
+ *
488
+ * @param category - MCP 品类名称
489
+ * @param method - JSON-RPC 方法名
490
+ * @param params - JSON-RPC 参数
491
+ * @returns JSON-RPC result
492
+ */
493
+ export async function sendJsonRpc(
494
+ category: string,
495
+ method: string,
496
+ params?: Record<string, unknown>,
497
+ ): Promise<unknown> {
498
+ const url = await getMcpUrl(category);
499
+
500
+ const body: JsonRpcRequest = {
501
+ jsonrpc: "2.0",
502
+ id: generateReqId("mcp_rpc"),
503
+ method,
504
+ ...(params !== undefined ? { params } : {}),
505
+ };
506
+
507
+ let session = await getOrCreateSession(url, category);
508
+
509
+ try {
510
+ const { rpcResult, newSessionId } = await sendRawJsonRpc(url, session, body);
511
+ // 用最新的 sessionId 更新 session
512
+ if (newSessionId) {
513
+ session.sessionId = newSessionId;
514
+ }
515
+ return rpcResult;
516
+ } catch (err) {
517
+ // 特定 JSON-RPC 错误码触发缓存清理(统一在传输层处理,上层无需关心)
518
+ if (err instanceof McpRpcError && CACHE_CLEAR_ERROR_CODES.has(err.code)) {
519
+ clearCategoryCache(category);
520
+ }
521
+
522
+ // 无状态 Server 不存在 session 失效问题,直接抛出错误
523
+ if (session.stateless) throw err;
524
+
525
+ // 有状态 Server:session 失效时服务端返回 404,需要重新初始化并重试一次
526
+ // 使用 McpHttpError.statusCode 精确匹配,避免字符串匹配 "404" 导致误判
527
+ if (err instanceof McpHttpError && err.statusCode === 404) {
528
+ console.log(`${LOG_TAG} Session 失效 (category="${category}"),开始重建...`);
529
+ mcpSessionCache.delete(category);
530
+
531
+ // 使用 rebuildSession 合并并发的 session 重建请求,避免竞态条件
532
+ session = await rebuildSession(url, category);
533
+ const { rpcResult, newSessionId } = await sendRawJsonRpc(url, session, body);
534
+ if (newSessionId) {
535
+ session.sessionId = newSessionId;
536
+ }
537
+ return rpcResult;
538
+ }
539
+
540
+ // 其他错误记录日志后抛出
541
+ console.error(`${LOG_TAG} RPC 请求失败 (category="${category}", method="${method}"): ${err instanceof Error ? err.message : String(err)}`);
542
+ throw err;
543
+ }
544
+ }
545
+
546
+ /**
547
+ * 合并并发的 session 重建请求
548
+ *
549
+ * 与 getOrCreateSession 类似,使用 inflightInitRequests 防止
550
+ * 多个并发请求同时遇到 404 时重复执行 initialize 握手。
551
+ */
552
+ async function rebuildSession(url: string, category: string): Promise<McpSession> {
553
+ const inflight = inflightInitRequests.get(category);
554
+ if (inflight) return inflight;
555
+
556
+ const promise = initializeSession(url, category).finally(() => {
557
+ inflightInitRequests.delete(category);
558
+ });
559
+ inflightInitRequests.set(category, promise);
560
+ return promise;
561
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * WeCom 媒体文件大小限制常量
3
+ *
4
+ * 对标企业微信智能机器人临时素材上传限制。
5
+ * 超出限制时,uploader 会按规则降级(如图片→文件)或拒绝。
6
+ */
7
+
8
+ /** 图片最大字节数 (10 MB),超出则降级为 file 类型 */
9
+ export const IMAGE_MAX_BYTES = 10 * 1024 * 1024;
10
+
11
+ /** 视频最大字节数 (10 MB),超出则降级为 file 类型 */
12
+ export const VIDEO_MAX_BYTES = 10 * 1024 * 1024;
13
+
14
+ /** 语音最大字节数 (2 MB),超出则降级为 file 类型 */
15
+ export const VOICE_MAX_BYTES = 2 * 1024 * 1024;
16
+
17
+ /** 文件最大字节数 (20 MB),超出则拒绝发送 */
18
+ export const FILE_MAX_BYTES = 20 * 1024 * 1024;
19
+
20
+ /** 绝对大小上限,等于 FILE_MAX_BYTES */
21
+ export const ABSOLUTE_MAX_BYTES = FILE_MAX_BYTES;
22
+
23
+ /** 语音类型支持的 MIME 集合(企微仅支持 AMR 格式语音消息) */
24
+ export const VOICE_SUPPORTED_MIMES = new Set(["audio/amr"]);
@@ -0,0 +1,15 @@
1
+ /**
2
+ * 媒体上传模块公共导出
3
+ */
4
+ export { uploadAndSendMediaBuffer } from "./uploader.js";
5
+ export type { UploadAndSendMediaBufferOptions, UploadAndSendMediaResult } from "./uploader.js";
6
+ export { detectWeComMediaType, applyFileSizeLimits } from "./uploader.js";
7
+ export type { FileSizeCheckResult } from "./uploader.js";
8
+ export {
9
+ IMAGE_MAX_BYTES,
10
+ VIDEO_MAX_BYTES,
11
+ VOICE_MAX_BYTES,
12
+ FILE_MAX_BYTES,
13
+ ABSOLUTE_MAX_BYTES,
14
+ VOICE_SUPPORTED_MIMES,
15
+ } from "./const.js";