@mocrane/wecom 2026.2.5

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 (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -0
  3. package/clawdbot.plugin.json +10 -0
  4. package/index.ts +28 -0
  5. package/openclaw.plugin.json +10 -0
  6. package/package.json +81 -0
  7. package/src/accounts.ts +72 -0
  8. package/src/agent/api-client.ts +336 -0
  9. package/src/agent/handler.ts +566 -0
  10. package/src/agent/index.ts +12 -0
  11. package/src/channel.ts +259 -0
  12. package/src/config/accounts.ts +99 -0
  13. package/src/config/index.ts +12 -0
  14. package/src/config/media.ts +14 -0
  15. package/src/config/network.ts +16 -0
  16. package/src/config/schema.ts +104 -0
  17. package/src/config-schema.ts +41 -0
  18. package/src/crypto/aes.ts +108 -0
  19. package/src/crypto/index.ts +24 -0
  20. package/src/crypto/signature.ts +43 -0
  21. package/src/crypto/xml.ts +49 -0
  22. package/src/crypto.test.ts +32 -0
  23. package/src/crypto.ts +176 -0
  24. package/src/http.ts +102 -0
  25. package/src/media.test.ts +55 -0
  26. package/src/media.ts +55 -0
  27. package/src/monitor/state.queue.test.ts +185 -0
  28. package/src/monitor/state.ts +514 -0
  29. package/src/monitor/types.ts +136 -0
  30. package/src/monitor.active.test.ts +239 -0
  31. package/src/monitor.integration.test.ts +207 -0
  32. package/src/monitor.ts +1802 -0
  33. package/src/monitor.webhook.test.ts +311 -0
  34. package/src/onboarding.ts +472 -0
  35. package/src/outbound.test.ts +143 -0
  36. package/src/outbound.ts +200 -0
  37. package/src/runtime.ts +14 -0
  38. package/src/shared/command-auth.ts +101 -0
  39. package/src/shared/index.ts +5 -0
  40. package/src/shared/xml-parser.test.ts +30 -0
  41. package/src/shared/xml-parser.ts +183 -0
  42. package/src/target.ts +80 -0
  43. package/src/types/account.ts +76 -0
  44. package/src/types/config.ts +88 -0
  45. package/src/types/constants.ts +42 -0
  46. package/src/types/global.d.ts +9 -0
  47. package/src/types/index.ts +38 -0
  48. package/src/types/message.ts +185 -0
  49. package/src/types.ts +159 -0
@@ -0,0 +1,514 @@
1
+ import crypto from "node:crypto";
2
+ import type { StreamState, PendingInbound, ActiveReplyState, WecomWebhookTarget } from "./types.js";
3
+ import type { WecomInboundMessage } from "../types.js";
4
+
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
+ };
13
+
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
+ 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
+ }
507
+ }
508
+
509
+ /**
510
+ * **monitorState (全局单例)**
511
+ *
512
+ * 导出全局唯一的 MonitorState 实例,供整个应用共享状态。
513
+ */
514
+ export const monitorState = new MonitorState();
@@ -0,0 +1,136 @@
1
+
2
+ import type { OpenClawConfig, PluginRuntime } from "openclaw/plugin-sdk";
3
+ import type { ResolvedBotAccount } from "../types/index.js";
4
+ import type { WecomInboundMessage } from "../types.js";
5
+
6
+ /**
7
+ * **WecomRuntimeEnv (运行时环境)**
8
+ *
9
+ * 包含基础的日志和错误报告接口,用于解耦对 PluginRuntime 的直接依赖。
10
+ */
11
+ export type WecomRuntimeEnv = {
12
+ log?: (message: string) => void;
13
+ error?: (message: string) => void;
14
+ };
15
+
16
+ /**
17
+ * **WecomWebhookTarget (Webhook 目标上下文)**
18
+ *
19
+ * 描述一个注册的 Bot 接收端点。包含处理该端点所需的所有上下文信息。
20
+ *
21
+ * @property account 解析后的 Bot 账号信息 (Token, AESKey 等)
22
+ * @property config 插件全局配置
23
+ * @property runtime 运行时环境 (日志)
24
+ * @property core OpenClaw 插件核心运行时
25
+ * @property path 该 Target 注册的 Webhook 路径
26
+ * @property statusSink 用于上报最后收发消息时间的回调
27
+ */
28
+ export type WecomWebhookTarget = {
29
+ account: ResolvedBotAccount;
30
+ config: OpenClawConfig;
31
+ runtime: WecomRuntimeEnv;
32
+ core: PluginRuntime;
33
+ path: string;
34
+ /** 反馈最后接收/发送时间 */
35
+ statusSink?: (patch: { lastInboundAt?: number; lastOutboundAt?: number }) => void;
36
+ };
37
+
38
+ /**
39
+ * **StreamState (流式会话状态)**
40
+ *
41
+ * 记录一个流式请求的生命周期状态。
42
+ *
43
+ * @property streamId 唯一会话 ID
44
+ * @property msgid 关联的企业微信消息 ID (用于去重)
45
+ * @property createdAt 创建时间
46
+ * @property updatedAt 最后更新时间 (用于 Prune)
47
+ * @property started 是否已开始处理 (Agent 已介入)
48
+ * @property finished 是否已完成 (Agent 输出完毕或出错)
49
+ * @property error 错误信息 (如有)
50
+ * @property content 已积累的响应内容 (用于长轮询返回)
51
+ * @property images 过程中生成的图片 (Base64 + MD5)
52
+ */
53
+ export type StreamState = {
54
+ streamId: string;
55
+ msgid?: string;
56
+ /** 会话键(同一人同一会话,用于队列/批次) */
57
+ conversationKey?: string;
58
+ /** 批次键(conversationKey + 批次序号) */
59
+ batchKey?: string;
60
+ /** 触发者 userid(用于 Agent 私信兜底) */
61
+ userId?: string;
62
+ /** 会话类型(用于群聊兜底逻辑) */
63
+ chatType?: "group" | "direct";
64
+ /** 群聊 chatid(用于日志/提示,不用于 Agent 发群) */
65
+ chatId?: string;
66
+ /** 智能机器人 aibotid(用于 taskKey 生成与日志) */
67
+ aibotid?: string;
68
+ /** Bot 回调幂等键(用于最终交付幂等) */
69
+ taskKey?: string;
70
+ createdAt: number;
71
+ updatedAt: number;
72
+ started: boolean;
73
+ finished: boolean;
74
+ error?: string;
75
+ content: string;
76
+ images?: { base64: string; md5: string }[];
77
+ /** 兜底模式(仅作为内部状态,不暴露给企微) */
78
+ fallbackMode?: "media" | "timeout" | "error";
79
+ /** 群内兜底提示是否已发送(用于防重复刷屏) */
80
+ fallbackPromptSentAt?: number;
81
+ /** Agent 私信最终交付是否已完成(用于防重复发送) */
82
+ finalDeliveredAt?: number;
83
+ /** 用于私信兜底的完整内容(不受 STREAM_MAX_BYTES 限制,但仍需上限保护) */
84
+ dmContent?: string;
85
+ /** 已通过 Agent 私信发送过的媒体标识(防重复发送附件) */
86
+ agentMediaKeys?: string[];
87
+ };
88
+
89
+ /**
90
+ * **PendingInbound (待处理/防抖消息)**
91
+ *
92
+ * 暂存在队列中的消息,等待防抖计时器结束进行聚合。
93
+ *
94
+ * @property streamId 预分配的流 ID
95
+ * @property target 目标 Webhook 上下文
96
+ * @property msg 原始消息对象 (如果聚合,通常指第一条)
97
+ * @property contents 聚合的消息内容列表
98
+ * @property media 附带的媒体文件 (如果有)
99
+ * @property msgids 聚合的所有消息 ID (用于去重)
100
+ * @property timeout 防抖定时器句柄
101
+ */
102
+ export type PendingInbound = {
103
+ streamId: string;
104
+ conversationKey: string;
105
+ batchKey: string;
106
+ target: WecomWebhookTarget;
107
+ msg: WecomInboundMessage;
108
+ contents: string[];
109
+ media?: { buffer: Buffer; contentType: string; filename: string };
110
+ msgids: string[];
111
+ nonce: string;
112
+ timestamp: string;
113
+ timeout: ReturnType<typeof setTimeout> | null;
114
+ /** 已到达防抖截止时间,但因前序批次仍在处理中而暂存 */
115
+ readyToFlush?: boolean;
116
+ createdAt: number;
117
+ };
118
+
119
+ /**
120
+ * **ActiveReplyState (主动回复地址状态)**
121
+ *
122
+ * 存储企业微信回调中提供的 `response_url`,用于后续将流式响应转为主动推送(template_card)等。
123
+ *
124
+ * @property response_url 企业微信提供的回调回复 URL
125
+ * @property proxyUrl 如果配置了代理,存储代理地址
126
+ * @property createdAt 创建时间
127
+ * @property usedAt 使用时间 (仅当 policy="once" 时有意义)
128
+ * @property lastError 最后一次发送失败的错误信息
129
+ */
130
+ export type ActiveReplyState = {
131
+ response_url: string;
132
+ proxyUrl?: string;
133
+ createdAt: number;
134
+ usedAt?: number;
135
+ lastError?: string;
136
+ };