@agent-wechat/wechat 0.4.1 → 0.5.0
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.
- package/dist/index.js +67 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5554,6 +5554,8 @@ async function startWeChatMonitor(opts) {
|
|
|
5554
5554
|
const { account, abortSignal, setStatus, log } = opts;
|
|
5555
5555
|
const client = new WeChatClient({ baseUrl: account.serverUrl, token: account.token });
|
|
5556
5556
|
const lastSeenId = /* @__PURE__ */ new Map();
|
|
5557
|
+
const groupHistory = /* @__PURE__ */ new Map();
|
|
5558
|
+
const GROUP_HISTORY_LIMIT = 50;
|
|
5557
5559
|
let lastAuthCheck = 0;
|
|
5558
5560
|
let prevStatus = void 0;
|
|
5559
5561
|
setStatus({
|
|
@@ -5642,7 +5644,10 @@ async function startWeChatMonitor(opts) {
|
|
|
5642
5644
|
lastSeenId,
|
|
5643
5645
|
account,
|
|
5644
5646
|
cfg,
|
|
5645
|
-
log
|
|
5647
|
+
log,
|
|
5648
|
+
void 0,
|
|
5649
|
+
groupHistory,
|
|
5650
|
+
GROUP_HISTORY_LIMIT
|
|
5646
5651
|
);
|
|
5647
5652
|
}
|
|
5648
5653
|
}
|
|
@@ -5657,7 +5662,7 @@ async function startWeChatMonitor(opts) {
|
|
|
5657
5662
|
log?.info?.(
|
|
5658
5663
|
`[wechat:${account.accountId}] Catch-up: ${chatId} lastMsgLocalId=${chat.lastMsgLocalId} > lastSeenId=${prevSeen}`
|
|
5659
5664
|
);
|
|
5660
|
-
await processUnreadChat(client, chat, lastSeenId, account, cfg, log, true);
|
|
5665
|
+
await processUnreadChat(client, chat, lastSeenId, account, cfg, log, true, groupHistory, GROUP_HISTORY_LIMIT);
|
|
5661
5666
|
}
|
|
5662
5667
|
} catch (err) {
|
|
5663
5668
|
log?.error?.(
|
|
@@ -5789,7 +5794,7 @@ function buildSegments(processed) {
|
|
|
5789
5794
|
}
|
|
5790
5795
|
return segments;
|
|
5791
5796
|
}
|
|
5792
|
-
async function dispatchSegment(segment, client, chatId, chat, liveAccount, cfg, log, remainingSegments) {
|
|
5797
|
+
async function dispatchSegment(segment, client, chatId, chat, liveAccount, cfg, log, remainingSegments, groupHistory) {
|
|
5793
5798
|
const core = getWeChatRuntime();
|
|
5794
5799
|
const lastMsg = segment[segment.length - 1];
|
|
5795
5800
|
const { isGroup, senderId, senderName, timestamp, rawBody, msg } = lastMsg;
|
|
@@ -6000,13 +6005,29 @@ async function dispatchSegment(segment, client, chatId, chat, liveAccount, cfg,
|
|
|
6000
6005
|
direction: "inbound",
|
|
6001
6006
|
at: timestamp
|
|
6002
6007
|
});
|
|
6008
|
+
if (isGroup && groupHistory) {
|
|
6009
|
+
groupHistory.set(chatId, []);
|
|
6010
|
+
}
|
|
6003
6011
|
} catch (err) {
|
|
6004
6012
|
log?.error?.(
|
|
6005
6013
|
`[wechat:${liveAccount.accountId}] Failed to dispatch segment (last msg ${msg.localId}): ${err}`
|
|
6006
6014
|
);
|
|
6007
6015
|
}
|
|
6008
6016
|
}
|
|
6009
|
-
|
|
6017
|
+
function bufferGroupHistory(groupHistory, chatId, pm, limit) {
|
|
6018
|
+
const history = groupHistory.get(chatId) ?? [];
|
|
6019
|
+
history.push(pm);
|
|
6020
|
+
while (history.length > limit) {
|
|
6021
|
+
history.shift();
|
|
6022
|
+
}
|
|
6023
|
+
groupHistory.delete(chatId);
|
|
6024
|
+
groupHistory.set(chatId, history);
|
|
6025
|
+
if (groupHistory.size > 1e3) {
|
|
6026
|
+
const first = groupHistory.keys().next().value;
|
|
6027
|
+
if (first) groupHistory.delete(first);
|
|
6028
|
+
}
|
|
6029
|
+
}
|
|
6030
|
+
async function processUnreadChat(client, chat, lastSeenId, account, cfg, log, skipOpen, groupHistory, groupHistoryLimit) {
|
|
6010
6031
|
const liveAccount = resolveWeChatAccount(cfg, account.accountId) ?? account;
|
|
6011
6032
|
const chatId = chat.username ?? chat.id;
|
|
6012
6033
|
if (!skipOpen) {
|
|
@@ -6071,6 +6092,47 @@ async function processUnreadChat(client, chat, lastSeenId, account, cfg, log, sk
|
|
|
6071
6092
|
processed.push(pm);
|
|
6072
6093
|
}
|
|
6073
6094
|
}
|
|
6095
|
+
const isGroup = chatId.includes("@chatroom");
|
|
6096
|
+
if (isGroup && groupHistory) {
|
|
6097
|
+
const wechatCfg = cfg?.channels?.wechat;
|
|
6098
|
+
const groupEntry = wechatCfg?.groups?.[chatId];
|
|
6099
|
+
const defaultEntry = wechatCfg?.groups?.["*"];
|
|
6100
|
+
const requireMention = groupEntry?.requireMention ?? defaultEntry?.requireMention ?? true;
|
|
6101
|
+
if (requireMention) {
|
|
6102
|
+
const hasMention = processed.some((pm) => pm.isMentioned);
|
|
6103
|
+
if (!hasMention) {
|
|
6104
|
+
const limit = groupHistoryLimit ?? 50;
|
|
6105
|
+
for (const pm of processed) {
|
|
6106
|
+
bufferGroupHistory(groupHistory, chatId, pm, limit);
|
|
6107
|
+
}
|
|
6108
|
+
log?.info?.(`[wechat:${liveAccount.accountId}] Buffered ${processed.length} msg(s) for group history in ${chatId}`);
|
|
6109
|
+
const maxId2 = Math.max(...newMessages.map((m) => m.localId));
|
|
6110
|
+
lastSeenId.set(chatId, maxId2);
|
|
6111
|
+
return;
|
|
6112
|
+
}
|
|
6113
|
+
const buffered = groupHistory.get(chatId) ?? [];
|
|
6114
|
+
if (buffered.length > 0) {
|
|
6115
|
+
for (const pm of buffered) {
|
|
6116
|
+
pm.isMentioned = true;
|
|
6117
|
+
}
|
|
6118
|
+
processed.unshift(...buffered);
|
|
6119
|
+
groupHistory.set(chatId, []);
|
|
6120
|
+
log?.info?.(`[wechat:${liveAccount.accountId}] Injected ${buffered.length} buffered msg(s) as history in ${chatId}`);
|
|
6121
|
+
}
|
|
6122
|
+
let latestMediaIdx = -1;
|
|
6123
|
+
for (let i = processed.length - 1; i >= 0; i--) {
|
|
6124
|
+
if (processed[i].mediaPath) {
|
|
6125
|
+
latestMediaIdx = i;
|
|
6126
|
+
break;
|
|
6127
|
+
}
|
|
6128
|
+
}
|
|
6129
|
+
for (let i = 0; i < processed.length; i++) {
|
|
6130
|
+
if (processed[i].mediaPath && i !== latestMediaIdx) {
|
|
6131
|
+
processed[i] = { ...processed[i], mediaPath: void 0, mediaMime: void 0, hasMedia: false };
|
|
6132
|
+
}
|
|
6133
|
+
}
|
|
6134
|
+
}
|
|
6135
|
+
}
|
|
6074
6136
|
if (processed.length > 0) {
|
|
6075
6137
|
const segments = buildSegments(processed);
|
|
6076
6138
|
log?.info?.(
|
|
@@ -6078,7 +6140,7 @@ async function processUnreadChat(client, chat, lastSeenId, account, cfg, log, sk
|
|
|
6078
6140
|
);
|
|
6079
6141
|
for (let i = 0; i < segments.length; i++) {
|
|
6080
6142
|
const remaining = segments.length - i - 1;
|
|
6081
|
-
await dispatchSegment(segments[i], client, chatId, chat, liveAccount, cfg, log, remaining);
|
|
6143
|
+
await dispatchSegment(segments[i], client, chatId, chat, liveAccount, cfg, log, remaining, groupHistory);
|
|
6082
6144
|
}
|
|
6083
6145
|
}
|
|
6084
6146
|
const maxId = Math.max(...newMessages.map((m) => m.localId));
|