ylib-wecom-openclaw-plugin 2026.4.29

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 (180) hide show
  1. package/README.md +596 -0
  2. package/dist/index.d.ts +10 -0
  3. package/dist/index.js +99 -0
  4. package/dist/src/accounts.d.ts +57 -0
  5. package/dist/src/accounts.js +247 -0
  6. package/dist/src/agent/api-client.d.ts +95 -0
  7. package/dist/src/agent/api-client.js +425 -0
  8. package/dist/src/agent/handler.d.ts +64 -0
  9. package/dist/src/agent/handler.js +731 -0
  10. package/dist/src/agent/index.d.ts +5 -0
  11. package/dist/src/agent/index.js +21 -0
  12. package/dist/src/agent/webhook.d.ts +25 -0
  13. package/dist/src/agent/webhook.js +294 -0
  14. package/dist/src/agent/xml.d.ts +21 -0
  15. package/dist/src/agent/xml.js +43 -0
  16. package/dist/src/channel.d.ts +5 -0
  17. package/dist/src/channel.js +815 -0
  18. package/dist/src/chat-queue.d.ts +31 -0
  19. package/dist/src/chat-queue.js +53 -0
  20. package/dist/src/config-schema.d.ts +587 -0
  21. package/dist/src/config-schema.js +146 -0
  22. package/dist/src/const.d.ts +128 -0
  23. package/dist/src/const.js +168 -0
  24. package/dist/src/dm-policy.d.ts +29 -0
  25. package/dist/src/dm-policy.js +146 -0
  26. package/dist/src/dynamic-agent.d.ts +37 -0
  27. package/dist/src/dynamic-agent.js +67 -0
  28. package/dist/src/dynamic-routing.d.ts +65 -0
  29. package/dist/src/dynamic-routing.js +62 -0
  30. package/dist/src/endpoint-dispatch.d.ts +54 -0
  31. package/dist/src/endpoint-dispatch.js +967 -0
  32. package/dist/src/endpoint-event-adapter.d.ts +15 -0
  33. package/dist/src/endpoint-event-adapter.js +427 -0
  34. package/dist/src/group-policy.d.ts +30 -0
  35. package/dist/src/group-policy.js +126 -0
  36. package/dist/src/http.d.ts +27 -0
  37. package/dist/src/http.js +168 -0
  38. package/dist/src/im-runtime-telemetry.d.ts +25 -0
  39. package/dist/src/im-runtime-telemetry.js +68 -0
  40. package/dist/src/interface.d.ts +192 -0
  41. package/dist/src/interface.js +5 -0
  42. package/dist/src/markdown-chunk.d.ts +1 -0
  43. package/dist/src/markdown-chunk.js +396 -0
  44. package/dist/src/mcp/index.d.ts +6 -0
  45. package/dist/src/mcp/index.js +28 -0
  46. package/dist/src/mcp/interceptors/biz-error.d.ts +11 -0
  47. package/dist/src/mcp/interceptors/biz-error.js +73 -0
  48. package/dist/src/mcp/interceptors/doc-auth-error.d.ts +10 -0
  49. package/dist/src/mcp/interceptors/doc-auth-error.js +235 -0
  50. package/dist/src/mcp/interceptors/index.d.ts +35 -0
  51. package/dist/src/mcp/interceptors/index.js +143 -0
  52. package/dist/src/mcp/interceptors/msg-media.d.ts +11 -0
  53. package/dist/src/mcp/interceptors/msg-media.js +201 -0
  54. package/dist/src/mcp/interceptors/smartpage-create.d.ts +30 -0
  55. package/dist/src/mcp/interceptors/smartpage-create.js +252 -0
  56. package/dist/src/mcp/interceptors/smartpage-export.d.ts +17 -0
  57. package/dist/src/mcp/interceptors/smartpage-export.js +135 -0
  58. package/dist/src/mcp/interceptors/smartsheet-upload.d.ts +22 -0
  59. package/dist/src/mcp/interceptors/smartsheet-upload.js +388 -0
  60. package/dist/src/mcp/interceptors/types.d.ts +64 -0
  61. package/dist/src/mcp/interceptors/types.js +8 -0
  62. package/dist/src/mcp/schema.d.ts +11 -0
  63. package/dist/src/mcp/schema.js +115 -0
  64. package/dist/src/mcp/tool.d.ts +63 -0
  65. package/dist/src/mcp/tool.js +318 -0
  66. package/dist/src/mcp/transport.d.ts +94 -0
  67. package/dist/src/mcp/transport.js +702 -0
  68. package/dist/src/media-handler.d.ts +55 -0
  69. package/dist/src/media-handler.js +306 -0
  70. package/dist/src/media-uploader.d.ts +142 -0
  71. package/dist/src/media-uploader.js +446 -0
  72. package/dist/src/message-parser.d.ts +104 -0
  73. package/dist/src/message-parser.js +232 -0
  74. package/dist/src/message-sender.d.ts +54 -0
  75. package/dist/src/message-sender.js +210 -0
  76. package/dist/src/monitor.d.ts +69 -0
  77. package/dist/src/monitor.js +1846 -0
  78. package/dist/src/onboarding.d.ts +8 -0
  79. package/dist/src/onboarding.js +248 -0
  80. package/dist/src/openclaw-compat.d.ts +148 -0
  81. package/dist/src/openclaw-compat.js +839 -0
  82. package/dist/src/proactive-markdown-send.d.ts +14 -0
  83. package/dist/src/proactive-markdown-send.js +205 -0
  84. package/dist/src/reqid-store.d.ts +23 -0
  85. package/dist/src/reqid-store.js +136 -0
  86. package/dist/src/runtime.d.ts +2 -0
  87. package/dist/src/runtime.js +7 -0
  88. package/dist/src/shared/command-auth.d.ts +23 -0
  89. package/dist/src/shared/command-auth.js +112 -0
  90. package/dist/src/shared/xml-parser.d.ts +46 -0
  91. package/dist/src/shared/xml-parser.js +228 -0
  92. package/dist/src/state-dir-resolve.d.ts +2 -0
  93. package/dist/src/state-dir-resolve.js +33 -0
  94. package/dist/src/state-manager.d.ts +115 -0
  95. package/dist/src/state-manager.js +413 -0
  96. package/dist/src/target.d.ts +35 -0
  97. package/dist/src/target.js +71 -0
  98. package/dist/src/template-card-manager.d.ts +55 -0
  99. package/dist/src/template-card-manager.js +316 -0
  100. package/dist/src/template-card-parser.d.ts +37 -0
  101. package/dist/src/template-card-parser.js +672 -0
  102. package/dist/src/timeout.d.ts +20 -0
  103. package/dist/src/timeout.js +57 -0
  104. package/dist/src/types/account.d.ts +29 -0
  105. package/dist/src/types/account.js +5 -0
  106. package/dist/src/types/config.d.ts +98 -0
  107. package/dist/src/types/config.js +8 -0
  108. package/dist/src/types/constants.d.ts +42 -0
  109. package/dist/src/types/constants.js +45 -0
  110. package/dist/src/types/index.d.ts +7 -0
  111. package/dist/src/types/index.js +17 -0
  112. package/dist/src/types/message.d.ts +238 -0
  113. package/dist/src/types/message.js +6 -0
  114. package/dist/src/utils.d.ts +148 -0
  115. package/dist/src/utils.js +92 -0
  116. package/dist/src/version.d.ts +2 -0
  117. package/dist/src/version.js +28 -0
  118. package/dist/src/webhook/command-auth.d.ts +47 -0
  119. package/dist/src/webhook/command-auth.js +137 -0
  120. package/dist/src/webhook/gateway.d.ts +36 -0
  121. package/dist/src/webhook/gateway.js +297 -0
  122. package/dist/src/webhook/handler.d.ts +19 -0
  123. package/dist/src/webhook/handler.js +481 -0
  124. package/dist/src/webhook/helpers.d.ts +157 -0
  125. package/dist/src/webhook/helpers.js +936 -0
  126. package/dist/src/webhook/http.d.ts +27 -0
  127. package/dist/src/webhook/http.js +168 -0
  128. package/dist/src/webhook/index.d.ts +11 -0
  129. package/dist/src/webhook/index.js +43 -0
  130. package/dist/src/webhook/media.d.ts +30 -0
  131. package/dist/src/webhook/media.js +152 -0
  132. package/dist/src/webhook/monitor.d.ts +59 -0
  133. package/dist/src/webhook/monitor.js +1672 -0
  134. package/dist/src/webhook/state.d.ts +220 -0
  135. package/dist/src/webhook/state.js +568 -0
  136. package/dist/src/webhook/target.d.ts +41 -0
  137. package/dist/src/webhook/target.js +165 -0
  138. package/dist/src/webhook/types.d.ts +348 -0
  139. package/dist/src/webhook/types.js +36 -0
  140. package/dist/src/webhook/video-frame.d.ts +13 -0
  141. package/dist/src/webhook/video-frame.js +108 -0
  142. package/openclaw.plugin.json +19 -0
  143. package/package.json +96 -0
  144. package/schema.json +534 -0
  145. package/scripts/generate-schema.mjs +33 -0
  146. package/skills/wecom-contact/SKILL.md +162 -0
  147. package/skills/wecom-doc/SKILL.md +162 -0
  148. package/skills/wecom-doc/references/create-doc.md +56 -0
  149. package/skills/wecom-doc/references/edit-doc-content.md +68 -0
  150. package/skills/wecom-doc/references/get-doc-content.md +88 -0
  151. package/skills/wecom-doc/references/smartpage-create.md +125 -0
  152. package/skills/wecom-doc/references/smartpage-export.md +160 -0
  153. package/skills/wecom-meeting/SKILL.md +441 -0
  154. package/skills/wecom-meeting/references/example-full.md +30 -0
  155. package/skills/wecom-meeting/references/example-reminder.md +46 -0
  156. package/skills/wecom-meeting/references/example-security.md +22 -0
  157. package/skills/wecom-meeting/references/response-get-meeting-info.md +148 -0
  158. package/skills/wecom-msg/SKILL.md +157 -0
  159. package/skills/wecom-msg/references/api-get-messages.md +93 -0
  160. package/skills/wecom-msg/references/api-get-msg-chat-list.md +58 -0
  161. package/skills/wecom-msg/references/api-get-msg-media.md +44 -0
  162. package/skills/wecom-msg/references/api-send-message.md +39 -0
  163. package/skills/wecom-preflight/SKILL.md +141 -0
  164. package/skills/wecom-schedule/SKILL.md +161 -0
  165. package/skills/wecom-schedule/references/api-check-availability.md +56 -0
  166. package/skills/wecom-schedule/references/api-create-schedule.md +38 -0
  167. package/skills/wecom-schedule/references/api-get-schedule-detail.md +81 -0
  168. package/skills/wecom-schedule/references/api-update-schedule.md +32 -0
  169. package/skills/wecom-schedule/references/ref-reminders.md +24 -0
  170. package/skills/wecom-send-media/SKILL.md +68 -0
  171. package/skills/wecom-send-template-card/SKILL.md +157 -0
  172. package/skills/wecom-send-template-card/references/api-template-card-types.md +358 -0
  173. package/skills/wecom-smartsheet/SKILL.md +164 -0
  174. package/skills/wecom-smartsheet/references/smartsheet-cell-value-formats.md +163 -0
  175. package/skills/wecom-smartsheet/references/smartsheet-field-types.md +44 -0
  176. package/skills/wecom-smartsheet/references/smartsheet-get-records.md +96 -0
  177. package/skills/wecom-smartsheet/references/webhook-examples.md +185 -0
  178. package/skills/wecom-smartsheet/references/webhook-fallback.md +184 -0
  179. package/skills/wecom-todo/SKILL.md +392 -0
  180. package/skills/wecom-todo/examples/workflows.md +163 -0
@@ -0,0 +1,568 @@
1
+ "use strict";
2
+ /**
3
+ * Webhook 模式状态管理
4
+ *
5
+ * 从 @mocrane/wecom monitor/state.ts 完整迁移。
6
+ * 包含 StreamStore(流状态存储)、ActiveReplyStore(主动回复地址存储)、MonitorState(全局容器)。
7
+ */
8
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
10
+ return new (P || (P = Promise))(function (resolve, reject) {
11
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
14
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
15
+ });
16
+ };
17
+ var __generator = (this && this.__generator) || function (thisArg, body) {
18
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
19
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
20
+ function verb(n) { return function (v) { return step([n, v]); }; }
21
+ function step(op) {
22
+ if (f) throw new TypeError("Generator is already executing.");
23
+ while (_) try {
24
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
25
+ if (y = 0, t) op = [op[0] & 2, t.value];
26
+ switch (op[0]) {
27
+ case 0: case 1: t = op; break;
28
+ case 4: _.label++; return { value: op[1], done: false };
29
+ case 5: _.label++; y = op[1]; op = [0]; continue;
30
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
31
+ default:
32
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
33
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
34
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
35
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
36
+ if (t[2]) _.ops.pop();
37
+ _.trys.pop(); continue;
38
+ }
39
+ op = body.call(thisArg, _);
40
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
41
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
42
+ }
43
+ };
44
+ var __importDefault = (this && this.__importDefault) || function (mod) {
45
+ return (mod && mod.__esModule) ? mod : { "default": mod };
46
+ };
47
+ exports.__esModule = true;
48
+ exports.monitorState = exports.WebhookMonitorState = exports.ActiveReplyStore = exports.StreamStore = exports.LIMITS = void 0;
49
+ var node_crypto_1 = __importDefault(require("node:crypto"));
50
+ // ============================================================================
51
+ // 常量
52
+ // ============================================================================
53
+ exports.LIMITS = {
54
+ STREAM_TTL_MS: 10 * 60 * 1000,
55
+ ACTIVE_REPLY_TTL_MS: 60 * 60 * 1000,
56
+ DEFAULT_DEBOUNCE_MS: 500,
57
+ STREAM_MAX_BYTES: 20480,
58
+ REQUEST_TIMEOUT_MS: 15000
59
+ };
60
+ // ============================================================================
61
+ // StreamStore
62
+ // ============================================================================
63
+ /**
64
+ * **StreamStore (流状态会话存储)**
65
+ *
66
+ * 管理企业微信回调的流式会话状态、消息去重和防抖聚合逻辑。
67
+ * 负责维护 msgid 到 streamId 的映射,以及临时缓存待处理的 Pending 消息。
68
+ */
69
+ var StreamStore = /** @class */ (function () {
70
+ function StreamStore() {
71
+ this.streams = new Map();
72
+ this.msgidToStreamId = new Map();
73
+ this.pendingInbounds = new Map();
74
+ this.conversationState = new Map();
75
+ this.streamIdToBatchKey = new Map();
76
+ this.batchStreamIdToAckStreamIds = new Map();
77
+ }
78
+ /**
79
+ * **setFlushHandler (设置防抖刷新回调)**
80
+ *
81
+ * 当防抖计时器结束时调用的处理函数。通常用于触发 Agent 进行消息处理。
82
+ * @param handler 回调函数,接收聚合后的 PendingInbound 对象
83
+ */
84
+ StreamStore.prototype.setFlushHandler = function (handler) {
85
+ this.onFlush = handler;
86
+ };
87
+ /**
88
+ * **createStream (创建流会话)**
89
+ *
90
+ * 初始化一个新的流式会话状态。
91
+ * @param params.msgid (可选) 企业微信消息 ID,用于后续去重映射
92
+ * @returns 生成的 streamId (Hex 字符串)
93
+ */
94
+ StreamStore.prototype.createStream = function (params) {
95
+ var streamId = node_crypto_1["default"].randomBytes(16).toString("hex");
96
+ if (params.msgid) {
97
+ this.msgidToStreamId.set(String(params.msgid), streamId);
98
+ }
99
+ this.streams.set(streamId, {
100
+ streamId: streamId,
101
+ msgid: params.msgid,
102
+ conversationKey: params.conversationKey,
103
+ batchKey: params.batchKey,
104
+ createdAt: Date.now(),
105
+ updatedAt: Date.now(),
106
+ started: false,
107
+ finished: false,
108
+ content: ""
109
+ });
110
+ if (params.batchKey) {
111
+ this.streamIdToBatchKey.set(streamId, params.batchKey);
112
+ }
113
+ return streamId;
114
+ };
115
+ /**
116
+ * **getStream (获取流状态)**
117
+ *
118
+ * 根据 streamId 获取当前的会话状态。
119
+ * @param streamId 流会话 ID
120
+ */
121
+ StreamStore.prototype.getStream = function (streamId) {
122
+ return this.streams.get(streamId);
123
+ };
124
+ /**
125
+ * **getStreamByMsgId (通过 msgid 查找流 ID)**
126
+ *
127
+ * 用于消息去重:检查该 msgid 是否已经关联由正在进行或已完成的流会话。
128
+ * @param msgid 企业微信消息 ID
129
+ */
130
+ StreamStore.prototype.getStreamByMsgId = function (msgid) {
131
+ return this.msgidToStreamId.get(String(msgid));
132
+ };
133
+ /** 手动设置 msgid → streamId 映射 */
134
+ StreamStore.prototype.setStreamIdForMsgId = function (msgid, streamId) {
135
+ var key = String(msgid).trim();
136
+ var value = String(streamId).trim();
137
+ if (!key || !value)
138
+ return;
139
+ this.msgidToStreamId.set(key, value);
140
+ };
141
+ /**
142
+ * 将“回执流”(ack stream) 关联到某个“批次流”(batch stream)。
143
+ * 用于:当用户连发多条消息被合并排队时,让后续消息的 stream 最终也能更新为可理解的提示,而不是永久停留在“已合并排队…”。
144
+ */
145
+ StreamStore.prototype.addAckStreamForBatch = function (params) {
146
+ var _a;
147
+ var batchStreamId = params.batchStreamId.trim();
148
+ var ackStreamId = params.ackStreamId.trim();
149
+ if (!batchStreamId || !ackStreamId)
150
+ return;
151
+ var list = (_a = this.batchStreamIdToAckStreamIds.get(batchStreamId)) !== null && _a !== void 0 ? _a : [];
152
+ list.push(ackStreamId);
153
+ this.batchStreamIdToAckStreamIds.set(batchStreamId, list);
154
+ };
155
+ /**
156
+ * 取出并清空某个批次流关联的所有回执流。
157
+ */
158
+ StreamStore.prototype.drainAckStreamsForBatch = function (batchStreamId) {
159
+ var _a;
160
+ var key = batchStreamId.trim();
161
+ if (!key)
162
+ return [];
163
+ var list = (_a = this.batchStreamIdToAckStreamIds.get(key)) !== null && _a !== void 0 ? _a : [];
164
+ this.batchStreamIdToAckStreamIds["delete"](key);
165
+ return list;
166
+ };
167
+ /**
168
+ * **updateStream (更新流状态)**
169
+ *
170
+ * 原子更新流状态,并自动刷新 updatedAt 时间戳。
171
+ * @param streamId 流会话 ID
172
+ * @param mutator 状态修改函数
173
+ */
174
+ StreamStore.prototype.updateStream = function (streamId, mutator) {
175
+ var state = this.streams.get(streamId);
176
+ if (state) {
177
+ mutator(state);
178
+ state.updatedAt = Date.now();
179
+ }
180
+ };
181
+ /**
182
+ * **markStarted (标记流开始)**
183
+ *
184
+ * 标记该流会话已经开始处理(通常在 Agent 启动后调用)。
185
+ */
186
+ StreamStore.prototype.markStarted = function (streamId) {
187
+ this.updateStream(streamId, function (s) { s.started = true; });
188
+ };
189
+ /**
190
+ * **markFinished (标记流结束)**
191
+ *
192
+ * 标记该流会话已完成,不再接收内容更新。
193
+ */
194
+ StreamStore.prototype.markFinished = function (streamId) {
195
+ this.updateStream(streamId, function (s) { s.finished = true; });
196
+ };
197
+ /**
198
+ * **addPendingMessage (添加待处理消息 / 防抖聚合)**
199
+ *
200
+ * 将收到的消息加入待处理队列。如果相同 pendingKey 已存在,则是防抖聚合;否则创建新条目。
201
+ * 会自动设置或重置防抖定时器。
202
+ *
203
+ * @param params 消息参数
204
+ * @returns { streamId, isNew } isNew=true 表示这是新的一组消息,需初始化 ActiveReply
205
+ */
206
+ StreamStore.prototype.addPendingMessage = function (params) {
207
+ var _this = this;
208
+ var conversationKey = params.conversationKey, target = params.target, msg = params.msg, msgContent = params.msgContent, nonce = params.nonce, timestamp = params.timestamp, debounceMs = params.debounceMs;
209
+ var effectiveDebounceMs = debounceMs !== null && debounceMs !== void 0 ? debounceMs : exports.LIMITS.DEFAULT_DEBOUNCE_MS;
210
+ var state = this.conversationState.get(conversationKey);
211
+ if (!state) {
212
+ // 第一批次(active)
213
+ var batchKey_1 = conversationKey;
214
+ var streamId_1 = this.createStream({ msgid: msg.msgid, conversationKey: conversationKey, batchKey: batchKey_1 });
215
+ var pending_1 = {
216
+ streamId: streamId_1,
217
+ conversationKey: conversationKey,
218
+ batchKey: batchKey_1,
219
+ target: target,
220
+ msg: msg,
221
+ contents: [msgContent],
222
+ msgids: msg.msgid ? [msg.msgid] : [],
223
+ nonce: nonce,
224
+ timestamp: timestamp,
225
+ createdAt: Date.now(),
226
+ timeout: setTimeout(function () {
227
+ _this.requestFlush(batchKey_1);
228
+ }, effectiveDebounceMs)
229
+ };
230
+ this.pendingInbounds.set(batchKey_1, pending_1);
231
+ this.conversationState.set(conversationKey, { activeBatchKey: batchKey_1, queue: [], nextSeq: 1 });
232
+ return { streamId: streamId_1, status: "active_new" };
233
+ }
234
+ // 合并规则(排队语义):
235
+ // - 初始批次(batchKey===conversationKey)不接收合并:避免 1/2 都刷出同一份最终答案。
236
+ // - 如果 active 批次是“排队批次”(batchKey!=conversationKey)且还没开始处理(started=false),
237
+ // 则允许把后续消息合并进该 active 批次(典型:1 很快结束,2 变 active 但还没开始跑,3 合并到 2)。
238
+ var activeBatchKey = state.activeBatchKey;
239
+ var activeIsInitial = activeBatchKey === conversationKey;
240
+ var activePending = this.pendingInbounds.get(activeBatchKey);
241
+ if (activePending && !activeIsInitial) {
242
+ var activeStream = this.streams.get(activePending.streamId);
243
+ var activeStarted = Boolean(activeStream === null || activeStream === void 0 ? void 0 : activeStream.started);
244
+ if (!activeStarted) {
245
+ activePending.contents.push(msgContent);
246
+ if (msg.msgid) {
247
+ activePending.msgids.push(msg.msgid);
248
+ // 注意:不把该 msgid 映射到 active streamId(避免该消息最终也刷出同一份完整答案)
249
+ }
250
+ if (activePending.timeout)
251
+ clearTimeout(activePending.timeout);
252
+ activePending.timeout = setTimeout(function () {
253
+ _this.requestFlush(activeBatchKey);
254
+ }, effectiveDebounceMs);
255
+ return { streamId: activePending.streamId, status: "active_merged" };
256
+ }
257
+ }
258
+ // active 批次已经开始处理;后续消息进入队列批次(queued),并允许在队列批次内做防抖聚合。
259
+ var queuedBatchKey = state.queue[0];
260
+ if (queuedBatchKey) {
261
+ var existingQueued = this.pendingInbounds.get(queuedBatchKey);
262
+ if (existingQueued) {
263
+ existingQueued.contents.push(msgContent);
264
+ if (msg.msgid) {
265
+ existingQueued.msgids.push(msg.msgid);
266
+ // 注意:不把该 msgid 映射到 queued streamId(避免该消息最终也刷出同一份完整答案)
267
+ }
268
+ if (existingQueued.timeout)
269
+ clearTimeout(existingQueued.timeout);
270
+ existingQueued.timeout = setTimeout(function () {
271
+ _this.requestFlush(queuedBatchKey);
272
+ }, effectiveDebounceMs);
273
+ return { streamId: existingQueued.streamId, status: "queued_merged" };
274
+ }
275
+ }
276
+ // 创建新的 queued 批次(会话只保留 1 个“下一批次”,后续消息继续合并到该批次)
277
+ var seq = state.nextSeq++;
278
+ var batchKey = conversationKey + "#q" + seq;
279
+ state.queue = [batchKey];
280
+ var streamId = this.createStream({ msgid: msg.msgid, conversationKey: conversationKey, batchKey: batchKey });
281
+ var pending = {
282
+ streamId: streamId,
283
+ conversationKey: conversationKey,
284
+ batchKey: batchKey,
285
+ target: target,
286
+ msg: msg,
287
+ contents: [msgContent],
288
+ msgids: msg.msgid ? [msg.msgid] : [],
289
+ nonce: nonce,
290
+ timestamp: timestamp,
291
+ createdAt: Date.now(),
292
+ timeout: setTimeout(function () {
293
+ _this.requestFlush(batchKey);
294
+ }, effectiveDebounceMs)
295
+ };
296
+ this.pendingInbounds.set(batchKey, pending);
297
+ this.conversationState.set(conversationKey, state);
298
+ return { streamId: streamId, status: "queued_new" };
299
+ };
300
+ /**
301
+ * 请求刷新:如果该批次当前为 active,则立即 flush;否则标记 ready,等待前序批次完成后再 flush。
302
+ */
303
+ StreamStore.prototype.requestFlush = function (batchKey) {
304
+ var pending = this.pendingInbounds.get(batchKey);
305
+ if (!pending)
306
+ return;
307
+ var state = this.conversationState.get(pending.conversationKey);
308
+ var isActive = (state === null || state === void 0 ? void 0 : state.activeBatchKey) === batchKey;
309
+ if (!isActive) {
310
+ if (pending.timeout) {
311
+ clearTimeout(pending.timeout);
312
+ pending.timeout = null;
313
+ }
314
+ pending.readyToFlush = true;
315
+ return;
316
+ }
317
+ this.flushPending(batchKey);
318
+ };
319
+ /**
320
+ * **flushPending (触发消息处理)**
321
+ *
322
+ * 内部方法:防抖时间结束后,将聚合的消息一次性推送给 flushHandler。
323
+ */
324
+ StreamStore.prototype.flushPending = function (pendingKey) {
325
+ var pending = this.pendingInbounds.get(pendingKey);
326
+ if (!pending)
327
+ return;
328
+ this.pendingInbounds["delete"](pendingKey);
329
+ if (pending.timeout) {
330
+ clearTimeout(pending.timeout);
331
+ pending.timeout = null;
332
+ }
333
+ pending.readyToFlush = false;
334
+ if (this.onFlush) {
335
+ this.onFlush(pending);
336
+ }
337
+ };
338
+ /**
339
+ * 在一个 stream 完成后推进会话队列:将 queued 批次提升为 active,并在需要时触发 flush。
340
+ */
341
+ StreamStore.prototype.onStreamFinished = function (streamId) {
342
+ var batchKey = this.streamIdToBatchKey.get(streamId);
343
+ var state = batchKey ? this.streams.get(streamId) : undefined;
344
+ var conversationKey = state === null || state === void 0 ? void 0 : state.conversationKey;
345
+ if (!batchKey || !conversationKey)
346
+ return;
347
+ var conv = this.conversationState.get(conversationKey);
348
+ if (!conv)
349
+ return;
350
+ if (conv.activeBatchKey !== batchKey)
351
+ return;
352
+ var next = conv.queue.shift();
353
+ if (!next) {
354
+ // 队列为空:会话已空闲。删除状态,避免后续消息被误判为“排队但永远不触发”。
355
+ this.conversationState["delete"](conversationKey);
356
+ return;
357
+ }
358
+ conv.activeBatchKey = next;
359
+ this.conversationState.set(conversationKey, conv);
360
+ var pending = this.pendingInbounds.get(next);
361
+ if (!pending)
362
+ return;
363
+ if (pending.readyToFlush) {
364
+ this.flushPending(next);
365
+ }
366
+ // 否则等待该批次自己的 debounce timer 到期后 requestFlush(next) 执行
367
+ };
368
+ /**
369
+ * **prune (清理过期状态)**
370
+ *
371
+ * 清理过期的流会话、msgid 映射以及残留的 Pending 消息。
372
+ * @param now 当前时间戳 (毫秒)
373
+ */
374
+ StreamStore.prototype.prune = function (now) {
375
+ if (now === void 0) { now = Date.now(); }
376
+ var streamCutoff = now - exports.LIMITS.STREAM_TTL_MS;
377
+ // 清理过期的流会话
378
+ for (var _i = 0, _a = this.streams.entries(); _i < _a.length; _i++) {
379
+ var _b = _a[_i], id = _b[0], state = _b[1];
380
+ if (state.updatedAt < streamCutoff) {
381
+ this.streams["delete"](id);
382
+ if (state.msgid) {
383
+ // 如果 msgid 映射仍指向该 stream,则一并移除
384
+ if (this.msgidToStreamId.get(state.msgid) === id) {
385
+ this.msgidToStreamId["delete"](state.msgid);
386
+ }
387
+ }
388
+ }
389
+ }
390
+ // 清理悬空的 msgid 映射 (Double check)
391
+ for (var _c = 0, _d = this.msgidToStreamId.entries(); _c < _d.length; _c++) {
392
+ var _e = _d[_c], msgid = _e[0], id = _e[1];
393
+ if (!this.streams.has(id)) {
394
+ this.msgidToStreamId["delete"](msgid);
395
+ }
396
+ }
397
+ // 清理超时的 Pending 消息 (通常由 timeout 清理,此处作为兜底)
398
+ for (var _f = 0, _g = this.pendingInbounds.entries(); _f < _g.length; _f++) {
399
+ var _h = _g[_f], key = _h[0], pending_2 = _h[1];
400
+ if (now - pending_2.createdAt > exports.LIMITS.STREAM_TTL_MS) {
401
+ if (pending_2.timeout)
402
+ clearTimeout(pending_2.timeout);
403
+ this.pendingInbounds["delete"](key);
404
+ }
405
+ }
406
+ // 清理 conversationState:active 已不存在且队列为空的会话
407
+ for (var _j = 0, _k = this.conversationState.entries(); _j < _k.length; _j++) {
408
+ var _l = _k[_j], convKey = _l[0], conv = _l[1];
409
+ var activeExists = this.pendingInbounds.has(conv.activeBatchKey) || Array.from(this.streamIdToBatchKey.values()).includes(conv.activeBatchKey);
410
+ var hasQueue = conv.queue.length > 0;
411
+ if (!activeExists && !hasQueue) {
412
+ this.conversationState["delete"](convKey);
413
+ }
414
+ }
415
+ };
416
+ return StreamStore;
417
+ }());
418
+ exports.StreamStore = StreamStore;
419
+ /**
420
+ * **ActiveReplyStore (主动回复地址存储)**
421
+ *
422
+ * 管理企业微信回调中的 `response_url` (用于被动回复转主动推送) 和 `proxyUrl`。
423
+ * 支持 'once' (一次性) 或 'multi' (多次) 使用策略。
424
+ */
425
+ var ActiveReplyStore = /** @class */ (function () {
426
+ /**
427
+ * @param policy 使用策略: "once" (默认,销毁式) 或 "multi"
428
+ */
429
+ function ActiveReplyStore(policy) {
430
+ if (policy === void 0) { policy = "once"; }
431
+ this.policy = policy;
432
+ this.activeReplies = new Map();
433
+ }
434
+ /**
435
+ * **store (存储回复地址)**
436
+ *
437
+ * 关联 streamId 与 response_url。
438
+ */
439
+ ActiveReplyStore.prototype.store = function (streamId, responseUrl, proxyUrl) {
440
+ var url = responseUrl === null || responseUrl === void 0 ? void 0 : responseUrl.trim();
441
+ if (!url)
442
+ return;
443
+ this.activeReplies.set(streamId, { response_url: url, proxyUrl: proxyUrl, createdAt: Date.now() });
444
+ };
445
+ /**
446
+ * **getUrl (获取回复地址)**
447
+ *
448
+ * 获取指定 streamId 关联的 response_url。
449
+ */
450
+ ActiveReplyStore.prototype.getUrl = function (streamId) {
451
+ var _a;
452
+ return (_a = this.activeReplies.get(streamId)) === null || _a === void 0 ? void 0 : _a.response_url;
453
+ };
454
+ /**
455
+ * 获取关联的代理 URL
456
+ */
457
+ ActiveReplyStore.prototype.getProxyUrl = function (streamId) {
458
+ var _a;
459
+ return (_a = this.activeReplies.get(streamId)) === null || _a === void 0 ? void 0 : _a.proxyUrl;
460
+ };
461
+ /**
462
+ * **use (消耗回复地址)**
463
+ *
464
+ * 使用存储的 response_url 执行操作。
465
+ * - 如果策略是 "once",第二次调用会抛错。
466
+ * - 自动更新使用时间 (usedAt)。
467
+ *
468
+ * @param streamId 流会话 ID
469
+ * @param fn 执行函数,接收 { responseUrl, proxyUrl }
470
+ */
471
+ ActiveReplyStore.prototype.use = function (streamId, fn) {
472
+ return __awaiter(this, void 0, void 0, function () {
473
+ var state, err_1;
474
+ return __generator(this, function (_a) {
475
+ switch (_a.label) {
476
+ case 0:
477
+ state = this.activeReplies.get(streamId);
478
+ if (!(state === null || state === void 0 ? void 0 : state.response_url)) {
479
+ return [2 /*return*/]; // 无 URL 可用,安全跳过
480
+ }
481
+ if (this.policy === "once" && state.usedAt) {
482
+ throw new Error("response_url already used for stream " + streamId + " (Policy: once)");
483
+ }
484
+ _a.label = 1;
485
+ case 1:
486
+ _a.trys.push([1, 3, , 4]);
487
+ return [4 /*yield*/, fn({ responseUrl: state.response_url, proxyUrl: state.proxyUrl })];
488
+ case 2:
489
+ _a.sent();
490
+ state.usedAt = Date.now();
491
+ return [3 /*break*/, 4];
492
+ case 3:
493
+ err_1 = _a.sent();
494
+ state.lastError = err_1 instanceof Error ? err_1.message : String(err_1);
495
+ throw err_1;
496
+ case 4: return [2 /*return*/];
497
+ }
498
+ });
499
+ });
500
+ };
501
+ /**
502
+ * **prune (清理过期地址)**
503
+ *
504
+ * 清理超过 TTL 的 active reply 记录。
505
+ */
506
+ ActiveReplyStore.prototype.prune = function (now) {
507
+ if (now === void 0) { now = Date.now(); }
508
+ var cutoff = now - exports.LIMITS.ACTIVE_REPLY_TTL_MS;
509
+ for (var _i = 0, _a = this.activeReplies.entries(); _i < _a.length; _i++) {
510
+ var _b = _a[_i], id = _b[0], state = _b[1];
511
+ if (state.createdAt < cutoff) {
512
+ this.activeReplies["delete"](id);
513
+ }
514
+ }
515
+ };
516
+ return ActiveReplyStore;
517
+ }());
518
+ exports.ActiveReplyStore = ActiveReplyStore;
519
+ /**
520
+ * **MonitorState (全局监控状态容器)**
521
+ *
522
+ * 模块单例,统一管理 StreamStore 和 ActiveReplyStore 实例。
523
+ * 提供生命周期方法 (startPruning / stopPruning) 以自动清理过期数据。
524
+ */
525
+ var WebhookMonitorState = /** @class */ (function () {
526
+ function WebhookMonitorState() {
527
+ /** 主要的流状态存储 */
528
+ this.streamStore = new StreamStore();
529
+ /** 主动回复地址存储 */
530
+ this.activeReplyStore = new ActiveReplyStore("multi");
531
+ }
532
+ /**
533
+ * **startPruning (启动自动清理)**
534
+ *
535
+ * 启动定时器,定期清理过期的流和回复地址。应在插件有活跃 Target 时调用。
536
+ * @param intervalMs 清理间隔 (默认 60s)
537
+ */
538
+ WebhookMonitorState.prototype.startPruning = function (intervalMs) {
539
+ var _this = this;
540
+ if (intervalMs === void 0) { intervalMs = 60000; }
541
+ if (this.pruneInterval)
542
+ return;
543
+ this.pruneInterval = setInterval(function () {
544
+ var now = Date.now();
545
+ _this.streamStore.prune(now);
546
+ _this.activeReplyStore.prune(now);
547
+ }, intervalMs);
548
+ };
549
+ /**
550
+ * **stopPruning (停止自动清理)**
551
+ *
552
+ * 停止定时器。应在插件无活跃 Target 时调用以释放资源。
553
+ */
554
+ WebhookMonitorState.prototype.stopPruning = function () {
555
+ if (this.pruneInterval) {
556
+ clearInterval(this.pruneInterval);
557
+ this.pruneInterval = undefined;
558
+ }
559
+ };
560
+ return WebhookMonitorState;
561
+ }());
562
+ exports.WebhookMonitorState = WebhookMonitorState;
563
+ /**
564
+ * **monitorState (全局单例)**
565
+ *
566
+ * 导出全局唯一的 MonitorState 实例,供整个应用共享状态。
567
+ */
568
+ exports.monitorState = new WebhookMonitorState();
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Webhook Target 管理
3
+ *
4
+ * 从 @mocrane/wecom monitor.ts 部分迁移(仅 Webhook Target 相关)。
5
+ * 维护全局已注册 Target 列表,提供注册/注销/查询功能。
6
+ *
7
+ * Target 按路径索引:Map<path, WecomWebhookTarget[]>
8
+ * 同一路径可能注册多个账号(老路径兼容模式),通过签名验证匹配到正确账号。
9
+ */
10
+ import type { WecomWebhookTarget } from "./types.js";
11
+ /**
12
+ * 注册 Webhook Target(多条路径)
13
+ *
14
+ * 为每条路径分别注册 Target,返回一个注销函数(一次性注销所有路径)。
15
+ */
16
+ export declare function registerWecomWebhookTarget(target: WecomWebhookTarget, paths: string[]): () => void;
17
+ /**
18
+ * 获取全局 Target 注册表
19
+ *
20
+ * 返回完整的 Map<path, Target[]>,供 handler.ts 路由匹配使用。
21
+ */
22
+ export declare function getWebhookTargetsMap(): ReadonlyMap<string, WecomWebhookTarget[]>;
23
+ /**
24
+ * 获取所有已注册的 Webhook Target(扁平列表)
25
+ *
26
+ * 用于无法精确匹配路径时的逐一签名验证场景。
27
+ */
28
+ export declare function getRegisteredTargets(): WecomWebhookTarget[];
29
+ /**
30
+ * 判断是否有活跃 Target
31
+ */
32
+ export declare function hasActiveTargets(): boolean;
33
+ /**
34
+ * 从 URL 中解析 accountId(多账号路径)
35
+ *
36
+ * 支持路径格式:
37
+ * - /plugins/wecom/bot/{accountId}
38
+ * - /wecom/bot/{accountId}
39
+ * - /wecom/{accountId}
40
+ */
41
+ export declare function parseWebhookPath(url: string): string | undefined;