@xopcai/xopc 0.0.464 → 0.0.465

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 (119) hide show
  1. package/dist/browser-ext/manifest.json +1 -1
  2. package/dist/extensions/feishu/src/adapters/cli-login.js +23 -49
  3. package/dist/extensions/feishu/src/adapters/gateway-setup.d.ts +3 -0
  4. package/dist/extensions/feishu/src/adapters/gateway-setup.js +180 -128
  5. package/dist/extensions/feishu/src/adapters/onboard-cli.d.ts +1 -1
  6. package/dist/extensions/feishu/src/adapters/onboard-cli.js +27 -15
  7. package/dist/extensions/feishu/src/auth/app-registration.d.ts +1 -0
  8. package/dist/extensions/feishu/src/auth/app-registration.js +12 -9
  9. package/dist/extensions/feishu/src/outbound/outbound-adapter.js +40 -16
  10. package/dist/extensions/feishu/src/plugin.d.ts +2 -0
  11. package/dist/extensions/feishu/src/plugin.js +37 -23
  12. package/dist/extensions/feishu/src/status/status-adapter.d.ts +1 -1
  13. package/dist/extensions/feishu/src/status/status-adapter.js +8 -5
  14. package/dist/extensions/feishu/src/streaming/streaming-adapter.js +97 -69
  15. package/dist/extensions/feishu/src/transport/client/bot-info.d.ts +10 -0
  16. package/dist/extensions/feishu/src/transport/client/bot-info.js +34 -0
  17. package/dist/extensions/feishu/src/transport/client/client.d.ts +7 -1
  18. package/dist/extensions/feishu/src/transport/client/client.js +6 -5
  19. package/dist/extensions/feishu/src/transport/client/install-sdk.d.ts +2 -0
  20. package/dist/extensions/feishu/src/transport/client/install-sdk.js +77 -0
  21. package/dist/extensions/feishu/src/transport/client/lark-sdk.d.ts +2 -0
  22. package/dist/extensions/feishu/src/transport/client/lark-sdk.js +27 -3
  23. package/dist/extensions/feishu/src/transport/client/sdk-catalog.d.ts +1 -0
  24. package/dist/extensions/feishu/src/transport/client/sdk-catalog.js +317 -0
  25. package/dist/extensions/feishu/src/transport/reliability/inbound-pipeline.d.ts +1 -1
  26. package/dist/extensions/feishu/src/transport/reliability/inbound-pipeline.js +13 -5
  27. package/dist/extensions/feishu/src/transport/reliability/message-ingress.d.ts +21 -0
  28. package/dist/extensions/feishu/src/transport/reliability/message-ingress.js +313 -0
  29. package/dist/extensions/feishu/src/transport/socket-mode/monitor.d.ts +1 -0
  30. package/dist/extensions/feishu/src/transport/socket-mode/monitor.js +56 -171
  31. package/dist/extensions/feishu/src/transport/webhook/monitor.d.ts +1 -0
  32. package/dist/extensions/feishu/src/transport/webhook/monitor.js +10 -102
  33. package/dist/extensions/telegram/src/account-manager.d.ts +1 -0
  34. package/dist/extensions/telegram/src/account-manager.js +3 -0
  35. package/dist/extensions/telegram/src/adapters/debounce-keys.js +7 -2
  36. package/dist/extensions/telegram/src/adapters/gateway-setup.js +113 -63
  37. package/dist/extensions/telegram/src/adapters/streaming-adapter.js +8 -4
  38. package/dist/extensions/telegram/src/doctor.js +1 -1
  39. package/dist/extensions/telegram/src/inbound-coalescer.d.ts +8 -6
  40. package/dist/extensions/telegram/src/inbound-coalescer.js +47 -88
  41. package/dist/extensions/telegram/src/inbound-processor.js +5 -1
  42. package/dist/extensions/telegram/src/outbound-sender.js +16 -23
  43. package/dist/extensions/telegram/src/plugin.js +80 -57
  44. package/dist/extensions/telegram/src/polling-session.d.ts +3 -6
  45. package/dist/extensions/telegram/src/polling-session.js +107 -94
  46. package/dist/extensions/telegram/src/rich-message.d.ts +4 -0
  47. package/dist/extensions/telegram/src/rich-message.js +23 -0
  48. package/dist/extensions/telegram/src/timeout-abort.d.ts +1 -1
  49. package/dist/extensions/telegram/src/timeout-abort.js +9 -3
  50. package/dist/extensions/telegram/src/update-offset-store.d.ts +1 -0
  51. package/dist/extensions/telegram/src/update-offset-store.js +6 -1
  52. package/dist/extensions/telegram/xopc.extension.json +1 -1
  53. package/dist/extensions/weixin/src/api/api.d.ts +1 -0
  54. package/dist/extensions/weixin/src/api/api.js +12 -6
  55. package/dist/extensions/weixin/src/api/types.d.ts +5 -2
  56. package/dist/extensions/weixin/src/auth/login-qr.d.ts +7 -1
  57. package/dist/extensions/weixin/src/auth/login-qr.js +48 -13
  58. package/dist/extensions/weixin/src/cdn/pic-decrypt.d.ts +2 -2
  59. package/dist/extensions/weixin/src/cdn/pic-decrypt.js +37 -25
  60. package/dist/extensions/weixin/src/cli/gateway-qr-login.d.ts +8 -0
  61. package/dist/extensions/weixin/src/cli/gateway-qr-login.js +45 -6
  62. package/dist/extensions/weixin/src/cli/qr-login.d.ts +1 -1
  63. package/dist/extensions/weixin/src/cli/qr-login.js +9 -3
  64. package/dist/extensions/weixin/src/media/media-download.d.ts +1 -0
  65. package/dist/extensions/weixin/src/media/media-download.js +4 -5
  66. package/dist/extensions/weixin/src/messaging/process-message.d.ts +2 -0
  67. package/dist/extensions/weixin/src/messaging/process-message.js +90 -65
  68. package/dist/extensions/weixin/src/messaging/quote-cache.d.ts +10 -0
  69. package/dist/extensions/weixin/src/messaging/quote-cache.js +46 -0
  70. package/dist/extensions/weixin/src/monitor/monitor.d.ts +3 -3
  71. package/dist/extensions/weixin/src/monitor/monitor.js +108 -104
  72. package/dist/extensions/weixin/src/plugin.d.ts +5 -1
  73. package/dist/extensions/weixin/src/plugin.js +138 -42
  74. package/dist/gateway/static/root/assets/{capabilities-page-rTjkjo71.js → capabilities-page-U_hQx5ou.js} +2 -2
  75. package/dist/gateway/static/root/assets/channels-BUzno8Pi.js +1 -0
  76. package/dist/gateway/static/root/assets/extension-settings-page-BvUtZGLT.js +1 -0
  77. package/dist/gateway/static/root/assets/index-Cdo4_zon.js +11 -0
  78. package/dist/gateway/static/root/assets/index-DYTktubl.css +1 -0
  79. package/dist/gateway/static/root/assets/{route-preload-5iwjWERk.js → route-preload-ClsAFFQc.js} +2 -2
  80. package/dist/gateway/static/root/assets/schema-form-BxBDgLrg.js +1 -0
  81. package/dist/gateway/static/root/assets/{settings-page-DNiRlM5x.js → settings-page-M6XwzIyu.js} +1 -1
  82. package/dist/gateway/static/root/assets/understanding-refresh-store-CM6bUcaJ.js +1 -0
  83. package/dist/gateway/static/root/assets/user-model-page-CBcGMhrR.js +2 -0
  84. package/dist/gateway/static/root/index.html +4 -4
  85. package/dist/package.js +1 -1
  86. package/dist/src/agent/inbound/inbound-loop.js +14 -0
  87. package/dist/src/agent/messaging/outbound-coordinator.js +1 -1
  88. package/dist/src/channels/connection-probe.d.ts +23 -0
  89. package/dist/src/channels/connection-probe.js +98 -0
  90. package/dist/src/channels/manager.d.ts +2 -0
  91. package/dist/src/channels/manager.js +5 -0
  92. package/dist/src/channels/message-journal.d.ts +25 -0
  93. package/dist/src/channels/message-journal.js +79 -0
  94. package/dist/src/channels/outbound-sender.d.ts +8 -0
  95. package/dist/src/channels/outbound-sender.js +60 -1
  96. package/dist/src/channels/plugins/types.adapters.d.ts +4 -0
  97. package/dist/src/channels/verified-inbound.js +20 -14
  98. package/dist/src/gateway/hono/routes/channels.js +23 -10
  99. package/dist/src/gateway/hono/routes/home.js +1 -1
  100. package/dist/src/gateway/hono/routes/user-model.js +48 -20
  101. package/dist/src/gateway/service.js +5 -5
  102. package/dist/src/home-intelligence/host.js +5 -5
  103. package/dist/src/infra/bus/index.d.ts +1 -1
  104. package/dist/src/infra/bus/queue.d.ts +16 -3
  105. package/dist/src/infra/bus/queue.js +74 -3
  106. package/dist/src/infra/debounce.d.ts +1 -0
  107. package/dist/src/infra/debounce.js +40 -35
  108. package/dist/src/knowledge-memory/index.js +2 -2
  109. package/dist/src/knowledge-memory/repository.d.ts +9 -0
  110. package/dist/src/knowledge-memory/repository.js +12 -2
  111. package/dist/src/runtime-tools/types.d.ts +1 -1
  112. package/package.json +5 -6
  113. package/dist/gateway/static/root/assets/channels-DvB2fwPI.js +0 -1
  114. package/dist/gateway/static/root/assets/extension-settings-page-DKAJE4h9.js +0 -1
  115. package/dist/gateway/static/root/assets/index-BGA-G-Bp.css +0 -1
  116. package/dist/gateway/static/root/assets/index-pmlYYCCU.js +0 -11
  117. package/dist/gateway/static/root/assets/schema-form-C9TYGa0_.js +0 -1
  118. package/dist/gateway/static/root/assets/understanding-refresh-store-Cf0DUO_C.js +0 -1
  119. package/dist/gateway/static/root/assets/user-model-page-CdB9qH57.js +0 -2
@@ -1,8 +1,10 @@
1
+ import { persistInboundAttachments } from "../../../../src/channels/attachments/inbound-persist.js";
1
2
  import { resolveWeixinRootDir } from "../storage/state-dir.js";
2
3
  import { logger } from "../util/logger.js";
3
4
  import { resolveWeixinPairingPath } from "../../../../src/channels/pairing/paths.js";
4
5
  import { issuePairingChallenge } from "../../../../src/channels/pairing/pairing-challenge.js";
5
6
  import { evaluateAccess } from "../../../../src/channels/security.js";
7
+ import { handleChannelConnectionProbe } from "../../../../src/channels/connection-probe.js";
6
8
  import { MessageItemType } from "../api/types.js";
7
9
  import { isMediaItem, setContextToken, weixinMessageToMsgContext } from "./inbound.js";
8
10
  import { readFrameworkAllowFromList } from "../auth/pairing.js";
@@ -10,10 +12,11 @@ import { downloadMediaFromItem } from "../media/media-download.js";
10
12
  import { isDebugMode } from "./debug-mode.js";
11
13
  import { sendMessageWeixin } from "./send.js";
12
14
  import { handleSlashCommand } from "./slash-commands.js";
15
+ import { getWeixinQuotedMessage, rememberWeixinQuotedMessage } from "./quote-cache.js";
13
16
  import { generateWeixinConversationIdWithRouting } from "../routing-integration.js";
14
17
  import path from "node:path";
15
18
  import { randomBytes } from "node:crypto";
16
- import { mkdir, readFile, writeFile } from "node:fs/promises";
19
+ import { mkdir, readFile, unlink, writeFile } from "node:fs/promises";
17
20
  //#region extensions/weixin/src/messaging/process-message.ts
18
21
  function extractTextBody(itemList) {
19
22
  if (!itemList?.length) return "";
@@ -25,7 +28,7 @@ function mergeAllowFrom(account, accountId) {
25
28
  return [...new Set([...(account.allowFrom ?? []).map(String), ...store])];
26
29
  }
27
30
  async function saveMediaBuffer(buffer, contentType, _subdir, maxBytes, originalFilename) {
28
- const cap = maxBytes ?? 100 * 1024 * 1024;
31
+ const cap = Math.min(maxBytes ?? Infinity, 5 * 1024 * 1024);
29
32
  if (buffer.length > cap) throw new Error(`weixin media exceeds max bytes (${cap})`);
30
33
  const dir = path.join(resolveWeixinRootDir(), "media", "inbound-temp");
31
34
  await mkdir(dir, { recursive: true });
@@ -40,17 +43,22 @@ async function processWeixinInboundMessage(full, deps) {
40
43
  const senderId = full.from_user_id ?? "";
41
44
  if (full.context_token?.trim() && senderId) setContextToken(deps.accountId, senderId, full.context_token, { sendToUserId: senderId });
42
45
  const textBody = extractTextBody(full.item_list);
43
- if (textBody.startsWith("/")) {
44
- if ((await handleSlashCommand(textBody, {
45
- to: full.from_user_id ?? "",
46
- contextToken: full.context_token,
47
- baseUrl: deps.baseUrl,
48
- token: deps.token,
49
- accountId: deps.accountId,
50
- log: () => {},
51
- errLog: () => {}
52
- }, receivedAt, full.create_time_ms)).handled) return;
53
- }
46
+ if (senderId && full.context_token && await handleChannelConnectionProbe({
47
+ channel: "weixin",
48
+ accountId: deps.accountId,
49
+ senderId,
50
+ text: textBody,
51
+ reply: (text) => sendMessageWeixin({
52
+ to: senderId,
53
+ text,
54
+ opts: {
55
+ baseUrl: deps.baseUrl,
56
+ token: deps.token,
57
+ contextToken: full.context_token,
58
+ routeTag: deps.routeTag
59
+ }
60
+ })
61
+ })) return;
54
62
  const mergedAllow = mergeAllowFrom(deps.account, deps.accountId);
55
63
  const access = evaluateAccess({
56
64
  context: {
@@ -94,63 +102,80 @@ async function processWeixinInboundMessage(full, deps) {
94
102
  else logger.info(`weixin: dropped message from=${senderId} dmPolicy=${deps.account.dmPolicy ?? "open"} reason=${access.reason ?? "not allowed"}`);
95
103
  return;
96
104
  }
97
- const mediaOpts = {};
98
- const hasDownloadableMedia = (m) => Boolean(m?.encrypt_query_param || m?.full_url);
99
- const mainMediaItem = full.item_list?.find((i) => i.type === MessageItemType.IMAGE && hasDownloadableMedia(i.image_item?.media)) ?? full.item_list?.find((i) => i.type === MessageItemType.VIDEO && hasDownloadableMedia(i.video_item?.media)) ?? full.item_list?.find((i) => i.type === MessageItemType.FILE && hasDownloadableMedia(i.file_item?.media)) ?? full.item_list?.find((i) => i.type === MessageItemType.VOICE && hasDownloadableMedia(i.voice_item?.media) && !i.voice_item?.text);
100
- const refMediaItem = !mainMediaItem ? full.item_list?.find((i) => i.type === MessageItemType.TEXT && i.ref_msg?.message_item && isMediaItem(i.ref_msg.message_item))?.ref_msg?.message_item : void 0;
101
- const mediaItem = mainMediaItem ?? refMediaItem;
102
- if (mediaItem) {
103
- const label = refMediaItem ? "ref" : "inbound";
104
- const downloaded = await downloadMediaFromItem(mediaItem, {
105
- cdnBaseUrl: deps.cdnBaseUrl,
106
- saveMedia: saveMediaBuffer,
107
- log: (m) => logger.debug(m),
108
- errLog: (m) => logger.warn(m),
109
- label
110
- });
111
- Object.assign(mediaOpts, downloaded);
105
+ if (textBody.startsWith("/")) {
106
+ if ((await handleSlashCommand(textBody, {
107
+ to: full.from_user_id ?? "",
108
+ contextToken: full.context_token,
109
+ baseUrl: deps.baseUrl,
110
+ token: deps.token,
111
+ accountId: deps.accountId,
112
+ log: () => {},
113
+ errLog: () => {}
114
+ }, receivedAt, full.create_time_ms)).handled) return;
115
+ }
116
+ const currentAttachments = [];
117
+ const quotedAttachments = [];
118
+ const incoming = (full.item_list ?? []).filter((item) => isMediaItem(item) && !item.voice_item?.text);
119
+ const quoted = (full.item_list ?? []).flatMap((item) => {
120
+ const ref = item.ref_msg?.message_item;
121
+ return ref && isMediaItem(ref) && !ref.voice_item?.text ? [ref] : [];
122
+ });
123
+ let totalBytes = 0;
124
+ for (const [index, item] of [...incoming, ...quoted].slice(0, 8).entries()) {
125
+ deps.abortSignal?.throwIfAborted();
126
+ let savedPath;
127
+ try {
128
+ const media = await downloadMediaFromItem(item, {
129
+ cdnBaseUrl: deps.cdnBaseUrl,
130
+ saveMedia: saveMediaBuffer,
131
+ log: (m) => logger.debug(m),
132
+ errLog: (m) => logger.warn(m),
133
+ label: index < incoming.length ? "inbound" : "ref",
134
+ signal: deps.abortSignal
135
+ });
136
+ savedPath = media.decryptedPicPath ?? media.decryptedVoicePath ?? media.decryptedFilePath ?? media.decryptedVideoPath;
137
+ if (!savedPath) continue;
138
+ const type = media.decryptedPicPath ? "image" : media.decryptedVoicePath ? "audio" : "file";
139
+ const mimeType = media.decryptedPicPath ? "image/jpeg" : media.decryptedVoicePath ? media.voiceMediaType ?? "audio/wav" : media.decryptedVideoPath ? "video/mp4" : media.fileMediaType ?? "application/octet-stream";
140
+ const buffer = await readFile(savedPath);
141
+ if (totalBytes + buffer.length > 20 * 1024 * 1024) break;
142
+ totalBytes += buffer.length;
143
+ (index < incoming.length ? currentAttachments : quotedAttachments).push({
144
+ type,
145
+ mimeType,
146
+ data: buffer.toString("base64"),
147
+ name: path.basename(savedPath)
148
+ });
149
+ } catch (err) {
150
+ logger.warn({
151
+ accountId: deps.accountId,
152
+ errorType: err instanceof Error ? err.name : typeof err
153
+ }, "Weixin attachment could not be prepared");
154
+ } finally {
155
+ if (savedPath) await unlink(savedPath).catch(() => {});
156
+ }
157
+ }
158
+ const ctx = weixinMessageToMsgContext(full, deps.accountId);
159
+ let body = ctx.Body?.trim() ?? "";
160
+ const currentRefs = await persistInboundAttachments(currentAttachments);
161
+ const stableMessageId = full.message_id == null ? void 0 : String(full.message_id);
162
+ if (stableMessageId && deps.delivery) rememberWeixinQuotedMessage(deps.delivery.scope, senderId, stableMessageId, textBody || body, currentRefs);
163
+ if (deps.delivery) for (const item of full.item_list ?? []) {
164
+ const ref = item.ref_msg;
165
+ if (!ref?.svr_id || ref.message_item || ref.svr_id === stableMessageId) continue;
166
+ const cached = await getWeixinQuotedMessage(deps.delivery.scope, senderId, ref.svr_id);
167
+ if (!cached) continue;
168
+ if (!ref.title && cached.text) body = `[引用: ${cached.text}]\n${body}`;
169
+ quotedAttachments.push(...cached.attachments);
112
170
  }
113
- const ctx = weixinMessageToMsgContext(full, deps.accountId, mediaOpts);
114
- const body = ctx.Body?.trim() ?? "";
171
+ const attachments = [...currentRefs ?? [], ...quotedAttachments];
115
172
  const conversationId = generateWeixinConversationIdWithRouting({
116
173
  accountId: deps.accountId,
117
174
  senderId
118
175
  }, deps.config);
119
- const attachments = [];
120
- if (mediaOpts.decryptedPicPath) {
121
- const buf = await readFile(mediaOpts.decryptedPicPath);
122
- attachments.push({
123
- type: "image",
124
- mimeType: "image/jpeg",
125
- data: buf.toString("base64"),
126
- name: path.basename(mediaOpts.decryptedPicPath)
127
- });
128
- } else if (mediaOpts.decryptedVoicePath) {
129
- const buf = await readFile(mediaOpts.decryptedVoicePath);
130
- attachments.push({
131
- type: "audio",
132
- mimeType: mediaOpts.voiceMediaType ?? "audio/wav",
133
- data: buf.toString("base64"),
134
- name: path.basename(mediaOpts.decryptedVoicePath)
135
- });
136
- } else if (mediaOpts.decryptedFilePath) {
137
- const buf = await readFile(mediaOpts.decryptedFilePath);
138
- attachments.push({
139
- type: "file",
140
- mimeType: mediaOpts.fileMediaType ?? "application/octet-stream",
141
- data: buf.toString("base64"),
142
- name: path.basename(mediaOpts.decryptedFilePath)
143
- });
144
- } else if (mediaOpts.decryptedVideoPath) {
145
- const buf = await readFile(mediaOpts.decryptedVideoPath);
146
- attachments.push({
147
- type: "file",
148
- mimeType: "video/mp4",
149
- data: buf.toString("base64"),
150
- name: path.basename(mediaOpts.decryptedVideoPath)
151
- });
152
- }
153
176
  if (isDebugMode(deps.accountId)) logger.debug(`weixin debug inbound from=${senderId} bodyLen=${body.length} attachments=${attachments.length}`);
177
+ deps.abortSignal?.throwIfAborted();
178
+ if (!body && !attachments.length) return;
154
179
  await deps.bus.publishInbound({
155
180
  channel: "weixin",
156
181
  sender_id: senderId,
@@ -165,7 +190,7 @@ async function processWeixinInboundMessage(full, deps) {
165
190
  contextToken: ctx.context_token
166
191
  },
167
192
  attachments: attachments.length ? attachments : void 0
168
- });
193
+ }, deps.delivery);
169
194
  }
170
195
  //#endregion
171
196
  export { processWeixinInboundMessage };
@@ -0,0 +1,10 @@
1
+ import type { MediaRef } from '@xopcai/xopc/media/types.js';
2
+ type QuotedMessage = {
3
+ id: string;
4
+ text: string;
5
+ attachments: MediaRef[];
6
+ timestamp: number;
7
+ };
8
+ export declare function rememberWeixinQuotedMessage(scope: string, sender: string, id: string, text: string, attachments?: MediaRef[]): void;
9
+ export declare function getWeixinQuotedMessage(scope: string, sender: string, id: string): Promise<QuotedMessage | undefined>;
10
+ export {};
@@ -0,0 +1,46 @@
1
+ import { DurableState } from "../../../../src/storage/sqlite/durable-state.js";
2
+ import { resolveMediaReference } from "../../../../src/media/media-reference.js";
3
+ import { logger } from "../util/logger.js";
4
+ //#region extensions/weixin/src/messaging/quote-cache.ts
5
+ const cache = new DurableState("weixin-quoted-messages");
6
+ const RETENTION_MS = 10080 * 6e4;
7
+ const MAX_MESSAGES_PER_CHAT = 64;
8
+ function chatKey(scope, sender) {
9
+ return JSON.stringify([scope, sender]);
10
+ }
11
+ function rememberWeixinQuotedMessage(scope, sender, id, text, attachments) {
12
+ try {
13
+ const now = Date.now();
14
+ cache.update(chatKey(scope, sender), (entries) => ({
15
+ value: [...(entries ?? []).filter((entry) => entry.id !== id && now - entry.timestamp <= RETENTION_MS).slice(-(MAX_MESSAGES_PER_CHAT - 1)), {
16
+ id,
17
+ text: text.slice(0, 4e3),
18
+ attachments: (attachments ?? []).slice(0, 8),
19
+ timestamp: now
20
+ }],
21
+ result: void 0
22
+ }));
23
+ } catch {
24
+ logger.debug("Weixin quote cache could not store the message");
25
+ }
26
+ }
27
+ async function getWeixinQuotedMessage(scope, sender, id) {
28
+ try {
29
+ const now = Date.now();
30
+ const entry = cache.get(chatKey(scope, sender))?.find((row) => row.id === id && now - row.timestamp <= RETENTION_MS);
31
+ if (!entry) return;
32
+ const attachments = [];
33
+ for (const attachment of entry.attachments) try {
34
+ await resolveMediaReference(attachment.uri);
35
+ attachments.push(attachment);
36
+ } catch {}
37
+ return {
38
+ ...entry,
39
+ attachments
40
+ };
41
+ } catch {
42
+ return;
43
+ }
44
+ }
45
+ //#endregion
46
+ export { getWeixinQuotedMessage, rememberWeixinQuotedMessage };
@@ -1,14 +1,14 @@
1
1
  import type { Config } from '@xopcai/xopc/config/schema.js';
2
2
  import type { MessageBus } from '@xopcai/xopc/infra/bus/index.js';
3
3
  import type { ResolvedWeixinAccount } from '../auth/accounts.js';
4
+ export type WeixinMonitorState = 'connecting' | 'polling' | 'reconnect-required';
4
5
  export type MonitorWeixinOpts = {
5
6
  account: ResolvedWeixinAccount;
6
7
  config: Config;
7
8
  bus: MessageBus;
8
9
  abortSignal?: AbortSignal;
9
10
  longPollTimeoutMs?: number;
11
+ onState?: (state: WeixinMonitorState) => void;
10
12
  };
11
- /**
12
- * Long-poll loop: getUpdates → process inbound.
13
- */
13
+ /** Persist each fetched batch before using the next platform cursor. */
14
14
  export declare function monitorWeixinProvider(opts: MonitorWeixinOpts): Promise<void>;
@@ -1,131 +1,135 @@
1
- import { loadGetUpdatesBuf, saveGetUpdatesBuf } from "../storage/sync-buf.js";
1
+ import { loadGetUpdatesBuf } from "../storage/sync-buf.js";
2
2
  import { logger } from "../util/logger.js";
3
- import { redactBody } from "../util/redact.js";
3
+ import { ChannelMessageJournal } from "../../../../src/channels/message-journal.js";
4
+ import { MessageItemType } from "../api/types.js";
4
5
  import { getUpdates } from "../api/api.js";
5
6
  import { WeixinConfigManager } from "../api/config-cache.js";
6
7
  import { getRemainingPauseMs, pauseSession } from "../api/session-guard.js";
7
8
  import { processWeixinInboundMessage } from "../messaging/process-message.js";
8
9
  import { setWeixinTypingTicket } from "../messaging/typing-ticket-store.js";
10
+ import { createHash } from "node:crypto";
11
+ import { setTimeout } from "node:timers/promises";
9
12
  //#region extensions/weixin/src/monitor/monitor.ts
10
13
  const DEFAULT_LONG_POLL_TIMEOUT_MS = 35e3;
11
- const MAX_CONSECUTIVE_FAILURES = 3;
12
- const BACKOFF_DELAY_MS = 3e4;
13
- const RETRY_DELAY_MS = 2e3;
14
- /**
15
- * Long-poll loop: getUpdates → process inbound.
16
- */
14
+ const MAX_LONG_POLL_TIMEOUT_MS = 9e4;
15
+ function inboundId(message) {
16
+ const messageId = String(message.message_id ?? "");
17
+ const sequence = String(message.seq ?? "");
18
+ const identity = (messageId !== "0" ? messageId : "") || message.client_id || (sequence !== "0" ? sequence : "") || createHash("sha256").update(JSON.stringify(message)).digest("hex");
19
+ return JSON.stringify([message.from_user_id, String(identity)]);
20
+ }
21
+ /** Persist each fetched batch before using the next platform cursor. */
17
22
  async function monitorWeixinProvider(opts) {
18
23
  const { account, config, bus, abortSignal, longPollTimeoutMs } = opts;
19
- const accountId = account.accountId;
20
- const baseUrl = account.baseUrl;
21
- const cdnBaseUrl = account.cdnBaseUrl;
22
- const token = account.token;
23
- const routeTag = account.routeTag;
24
- const aLog = logger;
25
- const previousGetUpdatesBuf = loadGetUpdatesBuf(accountId);
26
- let getUpdatesBuf = previousGetUpdatesBuf ?? "";
27
- if (previousGetUpdatesBuf) aLog.debug(`Using previous get_updates_buf (${getUpdatesBuf.length} bytes)`);
28
- else aLog.info(`No previous get_updates_buf found, starting fresh`);
29
- const configLog = (msg) => {
30
- aLog.debug(msg);
31
- };
24
+ const { accountId, baseUrl, cdnBaseUrl, token, routeTag } = account;
25
+ const scope = JSON.stringify([accountId, baseUrl]);
26
+ const journal = new ChannelMessageJournal("weixin", scope);
27
+ const interrupted = journal.recoverInterrupted();
28
+ if (interrupted) logger.warn({
29
+ accountId,
30
+ interrupted
31
+ }, "Weixin messages interrupted during dispatch");
32
+ bus.restoreDurableInbound?.("weixin", scope);
33
+ let cursor = journal.cursor() ?? loadGetUpdatesBuf(accountId) ?? "";
32
34
  const configManager = new WeixinConfigManager({
33
35
  baseUrl,
34
36
  token,
35
37
  routeTag
36
- }, configLog);
37
- let nextTimeoutMs = longPollTimeoutMs ?? DEFAULT_LONG_POLL_TIMEOUT_MS;
38
+ }, (msg) => logger.debug(msg));
39
+ let timeoutMs = longPollTimeoutMs ?? DEFAULT_LONG_POLL_TIMEOUT_MS;
38
40
  let consecutiveFailures = 0;
39
- let lastHealthCheck = Date.now();
40
- aLog.info(`Weixin monitor started: baseUrl=${baseUrl} account=${accountId} timeoutMs=${nextTimeoutMs}`);
41
- while (!abortSignal?.aborted) try {
42
- aLog.debug(`getUpdates: get_updates_buf=${getUpdatesBuf.substring(0, 50)}..., timeoutMs=${nextTimeoutMs}`);
43
- const resp = await getUpdates({
44
- baseUrl,
45
- token,
46
- get_updates_buf: getUpdatesBuf,
47
- timeoutMs: nextTimeoutMs,
48
- routeTag,
49
- abortSignal
50
- });
51
- aLog.debug(`getUpdates response: ret=${resp.ret}, msgs=${resp.msgs?.length ?? 0}, get_updates_buf_length=${resp.get_updates_buf?.length ?? 0}`);
52
- if (resp.longpolling_timeout_ms != null && resp.longpolling_timeout_ms > 0) {
53
- nextTimeoutMs = resp.longpolling_timeout_ms;
54
- aLog.debug(`Updated next poll timeout: ${nextTimeoutMs}ms`);
55
- }
56
- if (resp.ret !== void 0 && resp.ret !== 0 || resp.errcode !== void 0 && resp.errcode !== 0) {
57
- if (resp.errcode === -14 || resp.ret === -14) {
58
- pauseSession(accountId);
59
- const pauseMs = getRemainingPauseMs(accountId);
60
- aLog.error(`getUpdates: session expired (errcode=${resp.errcode} ret=${resp.ret}), pausing all requests for ${Math.ceil(pauseMs / 6e4)} min`);
61
- consecutiveFailures = 0;
62
- await sleep(pauseMs, abortSignal);
63
- continue;
41
+ opts.onState?.("connecting");
42
+ while (!abortSignal?.aborted) {
43
+ const pending = journal.pending();
44
+ if (pending.length) {
45
+ const senders = /* @__PURE__ */ new Map();
46
+ for (const entry of pending) {
47
+ const sender = entry.payload.from_user_id ?? "";
48
+ const entries = senders.get(sender) ?? [];
49
+ entries.push(entry);
50
+ senders.set(sender, entries);
64
51
  }
65
- consecutiveFailures += 1;
66
- aLog.error(`getUpdates failed: ret=${resp.ret} errcode=${resp.errcode} errmsg=${resp.errmsg ?? ""} response=${redactBody(JSON.stringify(resp))}`);
67
- if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
68
- aLog.error(`getUpdates: ${MAX_CONSECUTIVE_FAILURES} consecutive failures, backing off 30s`);
69
- consecutiveFailures = 0;
70
- await sleep(BACKOFF_DELAY_MS, abortSignal);
71
- } else await sleep(RETRY_DELAY_MS, abortSignal);
52
+ const groups = [...senders.values()];
53
+ for (let index = 0; index < groups.length; index += 4) await Promise.all(groups.slice(index, index + 4).map(async (entries) => {
54
+ for (const { id, payload } of entries) {
55
+ const text = payload.item_list?.find((item) => item.type === MessageItemType.TEXT)?.text_item?.text?.trim() ?? "";
56
+ const replayable = !text.startsWith("/") && !/^xopc_[a-f0-9]{32}$/.test(text);
57
+ if (abortSignal?.aborted || !journal.begin(id, replayable)) return;
58
+ try {
59
+ const senderId = payload.from_user_id;
60
+ if (senderId) configManager.getForUser(senderId, payload.context_token).then((cached) => {
61
+ if (!abortSignal?.aborted) setWeixinTypingTicket(accountId, senderId, cached.typingTicket);
62
+ }).catch(() => {});
63
+ await processWeixinInboundMessage(payload, {
64
+ account,
65
+ accountId,
66
+ config,
67
+ bus,
68
+ baseUrl,
69
+ cdnBaseUrl,
70
+ token,
71
+ routeTag,
72
+ delivery: {
73
+ scope,
74
+ id
75
+ },
76
+ abortSignal
77
+ });
78
+ journal.finish(id, "completed");
79
+ } catch (err) {
80
+ if (abortSignal?.aborted && replayable) return;
81
+ journal.finish(id, "interrupted");
82
+ logger.warn({
83
+ accountId,
84
+ errorType: err instanceof Error ? err.name : typeof err
85
+ }, "Weixin message dispatch interrupted");
86
+ }
87
+ }
88
+ }));
72
89
  continue;
73
90
  }
74
- consecutiveFailures = 0;
75
- if (resp.get_updates_buf != null && resp.get_updates_buf !== "") {
76
- saveGetUpdatesBuf(accountId, resp.get_updates_buf);
77
- getUpdatesBuf = resp.get_updates_buf;
78
- aLog.debug(`Saved new get_updates_buf (${getUpdatesBuf.length} bytes)`);
79
- }
80
- const list = resp.msgs ?? [];
81
- for (const full of list) {
82
- aLog.info(`inbound message: from=${full.from_user_id} types=${full.item_list?.map((i) => i.type).join(",") ?? "none"}`);
83
- const fromUserId = full.from_user_id ?? "";
84
- setWeixinTypingTicket(accountId, fromUserId, (await configManager.getForUser(fromUserId, full.context_token)).typingTicket);
85
- await processWeixinInboundMessage(full, {
86
- accountId,
87
- account,
88
- config,
89
- bus,
91
+ try {
92
+ const response = await getUpdates({
90
93
  baseUrl,
91
- cdnBaseUrl,
92
94
  token,
93
- routeTag
95
+ get_updates_buf: cursor,
96
+ timeoutMs,
97
+ routeTag,
98
+ abortSignal
94
99
  });
95
- }
96
- } catch (err) {
97
- if (abortSignal?.aborted) {
98
- aLog.info(`Weixin monitor stopped (aborted)`);
99
- return;
100
- }
101
- consecutiveFailures += 1;
102
- const e = err;
103
- const cause = e && typeof e === "object" ? e.cause : void 0;
104
- const causeText = cause ? ` cause=${cause.name ?? ""}${cause.code ? `(${cause.code})` : ""}:${cause.message ?? String(cause)}` : "";
105
- aLog.error(`getUpdates error: ${String(err)}${causeText}, stack=${err.stack ?? ""}`);
106
- if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
107
- aLog.error(`getUpdates: ${MAX_CONSECUTIVE_FAILURES} consecutive failures, backing off 30s`);
100
+ if (abortSignal?.aborted) return;
101
+ if (response.ret === -14 || response.errcode === -14) {
102
+ opts.onState?.("reconnect-required");
103
+ pauseSession(accountId);
104
+ await setTimeout(getRemainingPauseMs(accountId), void 0, { signal: abortSignal });
105
+ continue;
106
+ }
107
+ if ((response.ret ?? 0) !== 0 || (response.errcode ?? 0) !== 0) throw new Error(`Weixin poll rejected (ret=${response.ret ?? 0}, errcode=${response.errcode ?? 0})`);
108
+ const next = response.get_updates_buf || cursor;
109
+ const messages = response.msgs ?? [];
110
+ if (next !== cursor || messages.length) {
111
+ journal.accept(messages.map((payload) => ({
112
+ id: inboundId(payload),
113
+ payload
114
+ })), next);
115
+ cursor = next;
116
+ }
117
+ if (!response.timedOut) opts.onState?.("polling");
108
118
  consecutiveFailures = 0;
109
- await sleep(3e4, abortSignal);
110
- } else await sleep(2e3, abortSignal);
111
- const HEALTH_CHECK_INTERVAL_MS = 12e4;
112
- const now = Date.now();
113
- if (now - lastHealthCheck > HEALTH_CHECK_INTERVAL_MS) {
114
- aLog.debug(`Health check: resetting get_updates_buf to force fresh poll`);
115
- getUpdatesBuf = "";
119
+ if (response.longpolling_timeout_ms && response.longpolling_timeout_ms > 0) timeoutMs = Math.min(response.longpolling_timeout_ms, MAX_LONG_POLL_TIMEOUT_MS);
120
+ } catch (err) {
121
+ if (abortSignal?.aborted) return;
122
+ opts.onState?.("connecting");
123
+ consecutiveFailures += 1;
124
+ const retryMs = Math.min(3e4, 2e3 * 2 ** Math.min(4, consecutiveFailures - 1));
125
+ if (consecutiveFailures === 1) logger.warn({
126
+ accountId,
127
+ retryMs,
128
+ errorType: err instanceof Error ? err.name : typeof err
129
+ }, "Weixin polling failed; retrying");
130
+ await setTimeout(retryMs, void 0, { signal: abortSignal });
116
131
  }
117
- lastHealthCheck = now;
118
132
  }
119
- aLog.info(`Weixin monitor ended`);
120
- }
121
- function sleep(ms, signal) {
122
- return new Promise((resolve, reject) => {
123
- const t = setTimeout(resolve, ms);
124
- signal?.addEventListener("abort", () => {
125
- clearTimeout(t);
126
- reject(Object.assign(/* @__PURE__ */ new Error("aborted"), { name: "AbortError" }));
127
- }, { once: true });
128
- });
129
133
  }
130
134
  //#endregion
131
135
  export { monitorWeixinProvider };
@@ -5,6 +5,7 @@ import './pairing-config-resolver.js';
5
5
  import type { Config } from '@xopcai/xopc/config/schema.js';
6
6
  import type { MessageBus } from '@xopcai/xopc/infra/bus/index.js';
7
7
  import type { ChannelCapabilities, ChannelPlugin, ChannelPluginDefaults, ChannelPluginInitOptions, ChannelPluginReloadMeta, ChannelPluginStartOptions, ChannelSecurityContext, ChannelStreamingAdapter } from '@xopcai/xopc/channels/plugin-types.js';
8
+ import { type WeixinMonitorState } from './monitor/monitor.js';
8
9
  import type { ChannelCliLoginAdapter, ChannelCronDeliveryAdapter, ChannelRuntimeActionAdapter } from '@xopcai/xopc/channels/plugins/types.adapters.js';
9
10
  import { type ResolvedWeixinAccount } from './auth/accounts.js';
10
11
  import { weixinTextChunker } from './outbound-send.js';
@@ -41,6 +42,9 @@ export declare class WeixinChannelPlugin implements ChannelPlugin<ResolvedWeixin
41
42
  private bus;
42
43
  private cfg;
43
44
  private abortControllers;
45
+ private monitorTasks;
46
+ private setupSessions;
47
+ private monitorStates;
44
48
  config: {
45
49
  listAccountIds: (cfg: Config) => string[];
46
50
  resolveAccount: (cfg: Config, accountId?: string | null) => ResolvedWeixinAccount;
@@ -51,7 +55,7 @@ export declare class WeixinChannelPlugin implements ChannelPlugin<ResolvedWeixin
51
55
  name: string;
52
56
  enabled: boolean;
53
57
  configured: boolean;
54
- status: any;
58
+ status: WeixinMonitorState;
55
59
  };
56
60
  };
57
61
  security: {