@yanhaidao/wecom 2.3.3 → 2.3.9

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 (111) hide show
  1. package/.github/workflows/release.yml +69 -1
  2. package/README.md +213 -337
  3. package/assets/03.bot.page.png +0 -0
  4. package/changelog/v2.3.4.md +20 -0
  5. package/changelog/v2.3.9.md +22 -0
  6. package/compat-single-account.md +32 -2
  7. package/index.test.ts +34 -0
  8. package/index.ts +15 -7
  9. package/package.json +8 -7
  10. package/src/agent/api-client.upload.test.ts +1 -2
  11. package/src/agent/handler.ts +82 -9
  12. package/src/agent/index.ts +1 -1
  13. package/src/app/account-runtime.ts +245 -0
  14. package/src/app/bootstrap.ts +29 -0
  15. package/src/app/index.ts +31 -0
  16. package/src/capability/agent/delivery-service.ts +79 -0
  17. package/src/capability/agent/fallback-policy.ts +13 -0
  18. package/src/capability/agent/index.ts +3 -0
  19. package/src/capability/agent/ingress-service.ts +38 -0
  20. package/src/capability/bot/dispatch-config.ts +47 -0
  21. package/src/capability/bot/fallback-delivery.ts +178 -0
  22. package/src/capability/bot/index.ts +1 -0
  23. package/src/capability/bot/local-path-delivery.ts +215 -0
  24. package/src/capability/bot/service.ts +56 -0
  25. package/src/capability/bot/stream-delivery.ts +379 -0
  26. package/src/capability/bot/stream-finalizer.ts +120 -0
  27. package/src/capability/bot/stream-orchestrator.ts +352 -0
  28. package/src/capability/bot/types.ts +8 -0
  29. package/src/capability/index.ts +2 -0
  30. package/src/channel.lifecycle.test.ts +9 -6
  31. package/src/channel.meta.test.ts +12 -0
  32. package/src/channel.ts +48 -21
  33. package/src/config/accounts.ts +223 -283
  34. package/src/config/derived-paths.test.ts +111 -0
  35. package/src/config/derived-paths.ts +41 -0
  36. package/src/config/index.ts +10 -12
  37. package/src/config/runtime-config.ts +46 -0
  38. package/src/config/schema.ts +59 -102
  39. package/src/domain/models.ts +7 -0
  40. package/src/domain/policies.ts +36 -0
  41. package/src/dynamic-agent.ts +6 -0
  42. package/src/gateway-monitor.ts +43 -93
  43. package/src/http.ts +23 -2
  44. package/src/monitor/limits.ts +7 -0
  45. package/src/monitor/state.ts +28 -508
  46. package/src/monitor.active.test.ts +3 -3
  47. package/src/monitor.integration.test.ts +0 -1
  48. package/src/monitor.ts +64 -2603
  49. package/src/monitor.webhook.test.ts +127 -42
  50. package/src/observability/audit-log.ts +48 -0
  51. package/src/observability/legacy-operational-event-store.ts +36 -0
  52. package/src/observability/raw-envelope-log.ts +28 -0
  53. package/src/observability/status-registry.ts +13 -0
  54. package/src/observability/transport-session-view.ts +14 -0
  55. package/src/onboarding.test.ts +219 -0
  56. package/src/onboarding.ts +88 -71
  57. package/src/outbound.test.ts +5 -5
  58. package/src/outbound.ts +18 -66
  59. package/src/runtime/dispatcher.ts +52 -0
  60. package/src/runtime/index.ts +4 -0
  61. package/src/runtime/outbound-intent.ts +4 -0
  62. package/src/runtime/reply-orchestrator.test.ts +38 -0
  63. package/src/runtime/reply-orchestrator.ts +55 -0
  64. package/src/runtime/routing-bridge.ts +19 -0
  65. package/src/runtime/session-manager.ts +76 -0
  66. package/src/runtime.ts +7 -14
  67. package/src/shared/command-auth.ts +1 -17
  68. package/src/shared/media-service.ts +36 -0
  69. package/src/shared/media-types.ts +5 -0
  70. package/src/store/active-reply-store.ts +42 -0
  71. package/src/store/interfaces.ts +11 -0
  72. package/src/store/memory-store.ts +43 -0
  73. package/src/store/stream-batch-store.ts +350 -0
  74. package/src/target.ts +28 -0
  75. package/src/transport/agent-api/client.ts +44 -0
  76. package/src/transport/agent-api/core.ts +367 -0
  77. package/src/transport/agent-api/delivery.ts +41 -0
  78. package/src/transport/agent-api/media-upload.ts +11 -0
  79. package/src/transport/agent-api/reply.ts +39 -0
  80. package/src/transport/agent-callback/http-handler.ts +47 -0
  81. package/src/transport/agent-callback/inbound.ts +5 -0
  82. package/src/transport/agent-callback/reply.ts +13 -0
  83. package/src/transport/agent-callback/request-handler.ts +244 -0
  84. package/src/transport/agent-callback/session.ts +23 -0
  85. package/src/transport/bot-webhook/active-reply.ts +36 -0
  86. package/src/transport/bot-webhook/http-handler.ts +48 -0
  87. package/src/transport/bot-webhook/inbound-normalizer.ts +371 -0
  88. package/src/transport/bot-webhook/inbound.ts +5 -0
  89. package/src/transport/bot-webhook/message-shape.ts +89 -0
  90. package/src/transport/bot-webhook/protocol.ts +148 -0
  91. package/src/transport/bot-webhook/reply.ts +15 -0
  92. package/src/transport/bot-webhook/request-handler.ts +394 -0
  93. package/src/transport/bot-webhook/session.ts +23 -0
  94. package/src/transport/bot-ws/inbound.ts +109 -0
  95. package/src/transport/bot-ws/reply.ts +48 -0
  96. package/src/transport/bot-ws/sdk-adapter.ts +180 -0
  97. package/src/transport/bot-ws/session.ts +28 -0
  98. package/src/transport/http/common.ts +109 -0
  99. package/src/transport/http/registry.ts +92 -0
  100. package/src/transport/http/request-handler.ts +84 -0
  101. package/src/transport/index.ts +14 -0
  102. package/src/types/account.ts +56 -91
  103. package/src/types/config.ts +59 -112
  104. package/src/types/constants.ts +20 -35
  105. package/src/types/events.ts +21 -0
  106. package/src/types/index.ts +14 -38
  107. package/src/types/legacy-stream.ts +50 -0
  108. package/src/types/runtime-context.ts +28 -0
  109. package/src/types/runtime.ts +161 -0
  110. package/src/agent/api-client.ts +0 -383
  111. package/src/monitor/types.ts +0 -136
@@ -1,514 +1,34 @@
1
- import crypto from "node:crypto";
2
- import type { StreamState, PendingInbound, ActiveReplyState, WecomWebhookTarget } from "./types.js";
3
- import type { WecomBotInboundMessage as WecomInboundMessage } from "../types/index.js";
1
+ import { LegacyOperationalEventStore, type MonitorOperationalEvent } from "../observability/legacy-operational-event-store.js";
2
+ import { ActiveReplyStore } from "../store/active-reply-store.js";
3
+ import { StreamStore } from "../store/stream-batch-store.js";
4
+ import { LIMITS } from "./limits.js";
4
5
 
5
- // Constants
6
- export const LIMITS = {
7
- STREAM_TTL_MS: 10 * 60 * 1000,
8
- ACTIVE_REPLY_TTL_MS: 60 * 60 * 1000,
9
- DEFAULT_DEBOUNCE_MS: 500,
10
- STREAM_MAX_BYTES: 20_480,
11
- REQUEST_TIMEOUT_MS: 15_000
12
- };
6
+ export { LIMITS, StreamStore, ActiveReplyStore };
7
+ export type { MonitorOperationalEvent };
13
8
 
14
- /**
15
- * **StreamStore (流状态会话存储)**
16
- *
17
- * 管理企业微信回调的流式会话状态、消息去重和防抖聚合逻辑。
18
- * 负责维护 msgid 到 streamId 的映射,以及临时缓存待处理的 Pending 消息。
19
- */
20
- export class StreamStore {
21
- private streams = new Map<string, StreamState>();
22
- private msgidToStreamId = new Map<string, string>();
23
- private pendingInbounds = new Map<string, PendingInbound>();
24
- private conversationState = new Map<string, { activeBatchKey: string; queue: string[]; nextSeq: number }>();
25
- private streamIdToBatchKey = new Map<string, string>();
26
- private batchStreamIdToAckStreamIds = new Map<string, string[]>();
27
- private onFlush?: (pending: PendingInbound) => void;
28
-
29
- /**
30
- * **setFlushHandler (设置防抖刷新回调)**
31
- *
32
- * 当防抖计时器结束时调用的处理函数。通常用于触发 Agent 进行消息处理。
33
- * @param handler 回调函数,接收聚合后的 PendingInbound 对象
34
- */
35
- public setFlushHandler(handler: (pending: PendingInbound) => void) {
36
- this.onFlush = handler;
37
- }
38
-
39
- /**
40
- * **createStream (创建流会话)**
41
- *
42
- * 初始化一个新的流式会话状态。
43
- * @param params.msgid (可选) 企业微信消息 ID,用于后续去重映射
44
- * @returns 生成的 streamId (Hex 字符串)
45
- */
46
- createStream(params: { msgid?: string; conversationKey?: string; batchKey?: string }): string {
47
- const streamId = crypto.randomBytes(16).toString("hex");
48
-
49
- if (params.msgid) {
50
- this.msgidToStreamId.set(String(params.msgid), streamId);
51
- }
52
-
53
- this.streams.set(streamId, {
54
- streamId,
55
- msgid: params.msgid,
56
- conversationKey: params.conversationKey,
57
- batchKey: params.batchKey,
58
- createdAt: Date.now(),
59
- updatedAt: Date.now(),
60
- started: false,
61
- finished: false,
62
- content: ""
63
- });
64
-
65
- if (params.batchKey) {
66
- this.streamIdToBatchKey.set(streamId, params.batchKey);
67
- }
68
-
69
- return streamId;
70
- }
71
-
72
- /**
73
- * **getStream (获取流状态)**
74
- *
75
- * 根据 streamId 获取当前的会话状态。
76
- * @param streamId 流会话 ID
77
- */
78
- getStream(streamId: string): StreamState | undefined {
79
- return this.streams.get(streamId);
80
- }
81
-
82
- /**
83
- * **getStreamByMsgId (通过 msgid 查找流 ID)**
84
- *
85
- * 用于消息去重:检查该 msgid 是否已经关联由正在进行或已完成的流会话。
86
- * @param msgid 企业微信消息 ID
87
- */
88
- getStreamByMsgId(msgid: string): string | undefined {
89
- return this.msgidToStreamId.get(String(msgid));
90
- }
91
-
92
- setStreamIdForMsgId(msgid: string, streamId: string): void {
93
- const key = String(msgid).trim();
94
- const value = String(streamId).trim();
95
- if (!key || !value) return;
96
- this.msgidToStreamId.set(key, value);
97
- }
98
-
99
- /**
100
- * 将“回执流”(ack stream) 关联到某个“批次流”(batch stream)。
101
- * 用于:当用户连发多条消息被合并排队时,让后续消息的 stream 最终也能更新为可理解的提示,而不是永久停留在“已合并排队…”。
102
- */
103
- addAckStreamForBatch(params: { batchStreamId: string; ackStreamId: string }): void {
104
- const batchStreamId = params.batchStreamId.trim();
105
- const ackStreamId = params.ackStreamId.trim();
106
- if (!batchStreamId || !ackStreamId) return;
107
- const list = this.batchStreamIdToAckStreamIds.get(batchStreamId) ?? [];
108
- list.push(ackStreamId);
109
- this.batchStreamIdToAckStreamIds.set(batchStreamId, list);
110
- }
111
-
112
- /**
113
- * 取出并清空某个批次流关联的所有回执流。
114
- */
115
- drainAckStreamsForBatch(batchStreamId: string): string[] {
116
- const key = batchStreamId.trim();
117
- if (!key) return [];
118
- const list = this.batchStreamIdToAckStreamIds.get(key) ?? [];
119
- this.batchStreamIdToAckStreamIds.delete(key);
120
- return list;
121
- }
122
-
123
- /**
124
- * **updateStream (更新流状态)**
125
- *
126
- * 原子更新流状态,并自动刷新 updatedAt 时间戳。
127
- * @param streamId 流会话 ID
128
- * @param mutator 状态修改函数
129
- */
130
- updateStream(streamId: string, mutator: (state: StreamState) => void): void {
131
- const state = this.streams.get(streamId);
132
- if (state) {
133
- mutator(state);
134
- state.updatedAt = Date.now();
135
- }
136
- }
137
-
138
- /**
139
- * **markStarted (标记流开始)**
140
- *
141
- * 标记该流会话已经开始处理(通常在 Agent 启动后调用)。
142
- */
143
- markStarted(streamId: string): void {
144
- this.updateStream(streamId, (s) => { s.started = true; });
145
- }
146
-
147
- /**
148
- * **markFinished (标记流结束)**
149
- *
150
- * 标记该流会话已完成,不再接收内容更新。
151
- */
152
- markFinished(streamId: string): void {
153
- this.updateStream(streamId, (s) => { s.finished = true; });
154
- }
155
-
156
- /**
157
- * **addPendingMessage (添加待处理消息 / 防抖聚合)**
158
- *
159
- * 将收到的消息加入待处理队列。如果相同 pendingKey 已存在,则是防抖聚合;否则创建新条目。
160
- * 会自动设置或重置防抖定时器。
161
- *
162
- * @param params 消息参数
163
- * @returns { streamId, isNew } isNew=true 表示这是新的一组消息,需初始化 ActiveReply
164
- */
165
- addPendingMessage(params: {
166
- conversationKey: string;
167
- target: WecomWebhookTarget;
168
- msg: WecomInboundMessage;
169
- msgContent: string;
170
- nonce: string;
171
- timestamp: string;
172
- debounceMs?: number;
173
- }): { streamId: string; status: "active_new" | "active_merged" | "queued_new" | "queued_merged" } {
174
- const { conversationKey, target, msg, msgContent, nonce, timestamp, debounceMs } = params;
175
- const effectiveDebounceMs = debounceMs ?? LIMITS.DEFAULT_DEBOUNCE_MS;
176
-
177
- const state = this.conversationState.get(conversationKey);
178
- if (!state) {
179
- // 第一批次(active)
180
- const batchKey = conversationKey;
181
- const streamId = this.createStream({ msgid: msg.msgid, conversationKey, batchKey });
182
- const pending: PendingInbound = {
183
- streamId,
184
- conversationKey,
185
- batchKey,
186
- target,
187
- msg,
188
- contents: [msgContent],
189
- msgids: msg.msgid ? [msg.msgid] : [],
190
- nonce,
191
- timestamp,
192
- createdAt: Date.now(),
193
- timeout: setTimeout(() => {
194
- this.requestFlush(batchKey);
195
- }, effectiveDebounceMs)
196
- };
197
- this.pendingInbounds.set(batchKey, pending);
198
- this.conversationState.set(conversationKey, { activeBatchKey: batchKey, queue: [], nextSeq: 1 });
199
- return { streamId, status: "active_new" };
200
- }
201
-
202
- // 合并规则(排队语义):
203
- // - 初始批次(batchKey===conversationKey)不接收合并:避免 1/2 都刷出同一份最终答案。
204
- // - 如果 active 批次是“排队批次”(batchKey!=conversationKey)且还没开始处理(started=false),
205
- // 则允许把后续消息合并进该 active 批次(典型:1 很快结束,2 变 active 但还没开始跑,3 合并到 2)。
206
- const activeBatchKey = state.activeBatchKey;
207
- const activeIsInitial = activeBatchKey === conversationKey;
208
- const activePending = this.pendingInbounds.get(activeBatchKey);
209
- if (activePending && !activeIsInitial) {
210
- const activeStream = this.streams.get(activePending.streamId);
211
- const activeStarted = Boolean(activeStream?.started);
212
- if (!activeStarted) {
213
- activePending.contents.push(msgContent);
214
- if (msg.msgid) {
215
- activePending.msgids.push(msg.msgid);
216
- // 注意:不把该 msgid 映射到 active streamId(避免该消息最终也刷出同一份完整答案)
217
- }
218
- if (activePending.timeout) clearTimeout(activePending.timeout);
219
- activePending.timeout = setTimeout(() => {
220
- this.requestFlush(activeBatchKey);
221
- }, effectiveDebounceMs);
222
- return { streamId: activePending.streamId, status: "active_merged" };
223
- }
224
- }
225
-
226
- // active 批次已经开始处理;后续消息进入队列批次(queued),并允许在队列批次内做防抖聚合。
227
- const queuedBatchKey = state.queue[0];
228
- if (queuedBatchKey) {
229
- const existingQueued = this.pendingInbounds.get(queuedBatchKey);
230
- if (existingQueued) {
231
- existingQueued.contents.push(msgContent);
232
- if (msg.msgid) {
233
- existingQueued.msgids.push(msg.msgid);
234
- // 注意:不把该 msgid 映射到 queued streamId(避免该消息最终也刷出同一份完整答案)
235
- }
236
- if (existingQueued.timeout) clearTimeout(existingQueued.timeout);
237
-
238
- existingQueued.timeout = setTimeout(() => {
239
- this.requestFlush(queuedBatchKey);
240
- }, effectiveDebounceMs);
241
- return { streamId: existingQueued.streamId, status: "queued_merged" };
242
- }
243
- }
244
-
245
- // 创建新的 queued 批次(会话只保留 1 个“下一批次”,后续消息继续合并到该批次)
246
- const seq = state.nextSeq++;
247
- const batchKey = `${conversationKey}#q${seq}`;
248
- state.queue = [batchKey];
249
- const streamId = this.createStream({ msgid: msg.msgid, conversationKey, batchKey });
250
- const pending: PendingInbound = {
251
- streamId,
252
- conversationKey,
253
- batchKey,
254
- target,
255
- msg,
256
- contents: [msgContent],
257
- msgids: msg.msgid ? [msg.msgid] : [],
258
- nonce,
259
- timestamp,
260
- createdAt: Date.now(),
261
- timeout: setTimeout(() => {
262
- this.requestFlush(batchKey);
263
- }, effectiveDebounceMs)
264
- };
265
- this.pendingInbounds.set(batchKey, pending);
266
- this.conversationState.set(conversationKey, state);
267
- return { streamId, status: "queued_new" };
268
- }
269
-
270
- /**
271
- * 请求刷新:如果该批次当前为 active,则立即 flush;否则标记 ready,等待前序批次完成后再 flush。
272
- */
273
- private requestFlush(batchKey: string): void {
274
- const pending = this.pendingInbounds.get(batchKey);
275
- if (!pending) return;
276
-
277
- const state = this.conversationState.get(pending.conversationKey);
278
- const isActive = state?.activeBatchKey === batchKey;
279
- if (!isActive) {
280
- if (pending.timeout) {
281
- clearTimeout(pending.timeout);
282
- pending.timeout = null;
283
- }
284
- pending.readyToFlush = true;
285
- return;
286
- }
287
- this.flushPending(batchKey);
288
- }
289
-
290
- /**
291
- * **flushPending (触发消息处理)**
292
- *
293
- * 内部方法:防抖时间结束后,将聚合的消息一次性推送给 flushHandler。
294
- */
295
- private flushPending(pendingKey: string): void {
296
- const pending = this.pendingInbounds.get(pendingKey);
297
- if (!pending) return;
298
-
299
- this.pendingInbounds.delete(pendingKey);
300
- if (pending.timeout) {
301
- clearTimeout(pending.timeout);
302
- pending.timeout = null;
303
- }
304
- pending.readyToFlush = false;
305
-
306
- if (this.onFlush) {
307
- this.onFlush(pending);
308
- }
309
- }
310
-
311
- /**
312
- * 在一个 stream 完成后推进会话队列:将 queued 批次提升为 active,并在需要时触发 flush。
313
- */
314
- onStreamFinished(streamId: string): void {
315
- const batchKey = this.streamIdToBatchKey.get(streamId);
316
- const state = batchKey ? this.streams.get(streamId) : undefined;
317
- const conversationKey = state?.conversationKey;
318
- if (!batchKey || !conversationKey) return;
319
-
320
- const conv = this.conversationState.get(conversationKey);
321
- if (!conv) return;
322
- if (conv.activeBatchKey !== batchKey) return;
323
-
324
- const next = conv.queue.shift();
325
- if (!next) {
326
- // 队列为空:会话已空闲。删除状态,避免后续消息被误判为“排队但永远不触发”。
327
- this.conversationState.delete(conversationKey);
328
- return;
329
- }
330
- conv.activeBatchKey = next;
331
- this.conversationState.set(conversationKey, conv);
332
-
333
- const pending = this.pendingInbounds.get(next);
334
- if (!pending) return;
335
- if (pending.readyToFlush) {
336
- this.flushPending(next);
337
- }
338
- // 否则等待该批次自己的 debounce timer 到期后 requestFlush(next) 执行
339
- }
340
-
341
- /**
342
- * **prune (清理过期状态)**
343
- *
344
- * 清理过期的流会话、msgid 映射以及残留的 Pending 消息。
345
- * @param now 当前时间戳 (毫秒)
346
- */
347
- prune(now: number = Date.now()): void {
348
- const streamCutoff = now - LIMITS.STREAM_TTL_MS;
349
-
350
- // 清理过期的流会话
351
- for (const [id, state] of this.streams.entries()) {
352
- if (state.updatedAt < streamCutoff) {
353
- this.streams.delete(id);
354
- if (state.msgid) {
355
- // 如果 msgid 映射仍指向该 stream,则一并移除
356
- if (this.msgidToStreamId.get(state.msgid) === id) {
357
- this.msgidToStreamId.delete(state.msgid);
358
- }
359
- }
360
- }
361
- }
362
-
363
- // 清理悬空的 msgid 映射 (Double check)
364
- for (const [msgid, id] of this.msgidToStreamId.entries()) {
365
- if (!this.streams.has(id)) {
366
- this.msgidToStreamId.delete(msgid);
367
- }
368
- }
369
-
370
- // 清理超时的 Pending 消息 (通常由 timeout 清理,此处作为兜底)
371
- for (const [key, pending] of this.pendingInbounds.entries()) {
372
- if (now - pending.createdAt > LIMITS.STREAM_TTL_MS) {
373
- if (pending.timeout) clearTimeout(pending.timeout);
374
- this.pendingInbounds.delete(key);
375
- }
376
- }
377
-
378
- // 清理 conversationState:active 已不存在且队列为空的会话
379
- for (const [convKey, conv] of this.conversationState.entries()) {
380
- const activeExists = this.pendingInbounds.has(conv.activeBatchKey) || Array.from(this.streamIdToBatchKey.values()).includes(conv.activeBatchKey);
381
- const hasQueue = conv.queue.length > 0;
382
- if (!activeExists && !hasQueue) {
383
- this.conversationState.delete(convKey);
384
- }
385
- }
386
- }
387
- }
388
-
389
- /**
390
- * **ActiveReplyStore (主动回复地址存储)**
391
- *
392
- * 管理企业微信回调中的 `response_url` (用于被动回复转主动推送) 和 `proxyUrl`。
393
- * 支持 'once' (一次性) 或 'multi' (多次) 使用策略。
394
- */
395
- export class ActiveReplyStore {
396
- private activeReplies = new Map<string, ActiveReplyState>();
397
-
398
- /**
399
- * @param policy 使用策略: "once" (默认,销毁式) 或 "multi"
400
- */
401
- constructor(private policy: "once" | "multi" = "once") { }
402
-
403
- /**
404
- * **store (存储回复地址)**
405
- *
406
- * 关联 streamId 与 response_url。
407
- */
408
- store(streamId: string, responseUrl?: string, proxyUrl?: string): void {
409
- const url = responseUrl?.trim();
410
- if (!url) return;
411
- this.activeReplies.set(streamId, { response_url: url, proxyUrl, createdAt: Date.now() });
412
- }
413
-
414
- /**
415
- * **getUrl (获取回复地址)**
416
- *
417
- * 获取指定 streamId 关联的 response_url。
418
- */
419
- getUrl(streamId: string): string | undefined {
420
- return this.activeReplies.get(streamId)?.response_url;
421
- }
422
-
423
- /**
424
- * **use (消耗回复地址)**
425
- *
426
- * 使用存储的 response_url 执行操作。
427
- * - 如果策略是 "once",第二次调用会抛错。
428
- * - 自动更新使用时间 (usedAt)。
429
- *
430
- * @param streamId 流会话 ID
431
- * @param fn 执行函数,接收 { responseUrl, proxyUrl }
432
- */
433
- async use(streamId: string, fn: (params: { responseUrl: string; proxyUrl?: string }) => Promise<void>): Promise<void> {
434
- const state = this.activeReplies.get(streamId);
435
- if (!state?.response_url) {
436
- return; // 无 URL 可用,安全跳过
437
- }
438
-
439
- if (this.policy === "once" && state.usedAt) {
440
- throw new Error(`response_url already used for stream ${streamId} (Policy: once)`);
441
- }
442
-
443
- try {
444
- await fn({ responseUrl: state.response_url, proxyUrl: state.proxyUrl });
445
- state.usedAt = Date.now();
446
- } catch (err: unknown) {
447
- state.lastError = err instanceof Error ? err.message : String(err);
448
- throw err;
449
- }
450
- }
451
-
452
- /**
453
- * **prune (清理过期地址)**
454
- *
455
- * 清理超过 TTL 的 active reply 记录。
456
- */
457
- prune(now: number = Date.now()): void {
458
- const cutoff = now - LIMITS.ACTIVE_REPLY_TTL_MS;
459
- for (const [id, state] of this.activeReplies.entries()) {
460
- if (state.createdAt < cutoff) {
461
- this.activeReplies.delete(id);
462
- }
463
- }
464
- }
465
- }
466
-
467
- /**
468
- * **MonitorState (全局监控状态容器)**
469
- *
470
- * 模块单例,统一管理 StreamStore 和 ActiveReplyStore 实例。
471
- * 提供生命周期方法 (startPruning / stopPruning) 以自动清理过期数据。
472
- */
473
9
  class MonitorState {
474
- /** 主要的流状态存储 */
475
- public readonly streamStore = new StreamStore();
476
- /** 主动回复地址存储 */
477
- public readonly activeReplyStore = new ActiveReplyStore("multi");
478
-
479
- private pruneInterval?: NodeJS.Timeout;
480
-
481
- /**
482
- * **startPruning (启动自动清理)**
483
- *
484
- * 启动定时器,定期清理过期的流和回复地址。应在插件有活跃 Target 时调用。
485
- * @param intervalMs 清理间隔 (默认 60s)
486
- */
487
- public startPruning(intervalMs: number = 60_000): void {
488
- if (this.pruneInterval) return;
489
- this.pruneInterval = setInterval(() => {
490
- const now = Date.now();
491
- this.streamStore.prune(now);
492
- this.activeReplyStore.prune(now);
493
- }, intervalMs);
494
- }
495
-
496
- /**
497
- * **stopPruning (停止自动清理)**
498
- *
499
- * 停止定时器。应在插件无活跃 Target 时调用以释放资源。
500
- */
501
- public stopPruning(): void {
502
- if (this.pruneInterval) {
503
- clearInterval(this.pruneInterval);
504
- this.pruneInterval = undefined;
505
- }
506
- }
10
+ public readonly streamStore = new StreamStore();
11
+ public readonly activeReplyStore = new ActiveReplyStore("multi");
12
+ public readonly operationalEvents = new LegacyOperationalEventStore();
13
+
14
+ private pruneInterval?: NodeJS.Timeout;
15
+
16
+ public startPruning(intervalMs: number = 60_000): void {
17
+ if (this.pruneInterval) return;
18
+ this.pruneInterval = setInterval(() => {
19
+ const now = Date.now();
20
+ this.streamStore.prune(now);
21
+ this.activeReplyStore.prune(now);
22
+ this.operationalEvents.prune(now);
23
+ }, intervalMs);
24
+ }
25
+
26
+ public stopPruning(): void {
27
+ if (this.pruneInterval) {
28
+ clearInterval(this.pruneInterval);
29
+ this.pruneInterval = undefined;
30
+ }
31
+ }
507
32
  }
508
33
 
509
- /**
510
- * **monitorState (全局单例)**
511
- *
512
- * 导出全局唯一的 MonitorState 实例,供整个应用共享状态。
513
- */
514
34
  export const monitorState = new MonitorState();
@@ -2,7 +2,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
2
  import { sendActiveMessage, handleWecomWebhookRequest, registerWecomWebhookTarget } from "./monitor.js";
3
3
  import * as cryptoHelpers from "./crypto.js";
4
4
  import * as runtime from "./runtime.js";
5
- import * as agentApi from "./agent/api-client.js";
5
+ import * as agentApi from "./transport/agent-api/core.js";
6
6
  import { IncomingMessage, ServerResponse } from "node:http";
7
7
  import { Socket } from "node:net";
8
8
  import * as crypto from "node:crypto";
@@ -17,7 +17,7 @@ vi.mock("undici", () => ({
17
17
  ProxyAgent: class ProxyAgent { },
18
18
  }));
19
19
 
20
- vi.mock("./agent/api-client.js", () => ({
20
+ vi.mock("./transport/agent-api/core.js", () => ({
21
21
  sendText: vi.fn(),
22
22
  sendMedia: vi.fn(),
23
23
  uploadMedia: vi.fn(),
@@ -123,7 +123,7 @@ describe("Monitor Active Features", () => {
123
123
  vi.spyOn(runtime, "getWecomRuntime").mockReturnValue(mockCore);
124
124
 
125
125
  unregisterTarget = registerWecomWebhookTarget({
126
- account: { accountId: "default", enabled: true, configured: true, token: "T", encodingAESKey: validKey, receiveId: "R", config: {} as any },
126
+ account: { accountId: "default", configured: true, token: "T", encodingAESKey: validKey, receiveId: "R", config: {} as any },
127
127
  config: {
128
128
  channels: {
129
129
  wecom: {
@@ -98,7 +98,6 @@ describe("Monitor Integration: Inbound Image", () => {
98
98
  account: {
99
99
  accountId: "test-acc",
100
100
  name: "Test",
101
- enabled: true,
102
101
  configured: true,
103
102
  token,
104
103
  encodingAESKey,