fix: tighten WeChat session rotation and defer snapshot refresh
Use session snapshot message counts alongside WeChat route counts so long-lived dedicated sessions rotate before Goose history bloats, and refresh snapshots asynchronously so customer replies are not blocked. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+30
-8
@@ -1683,10 +1683,12 @@ export function createWechatMpService({
|
||||
config.sessionIdleRotateMs > 0 &&
|
||||
routeUpdatedAt > 0 &&
|
||||
now - routeUpdatedAt > config.sessionIdleRotateMs;
|
||||
const routeMessageCount =
|
||||
config.sessionMessageRotateCount > 0 &&
|
||||
typeof userAuth.countWechatAgentSessionMessages === 'function'
|
||||
? await userAuth
|
||||
let routeMessageCount = 0;
|
||||
if (config.sessionMessageRotateCount > 0) {
|
||||
const counts = [];
|
||||
if (typeof userAuth.countWechatAgentSessionMessages === 'function') {
|
||||
counts.push(
|
||||
userAuth
|
||||
.countWechatAgentSessionMessages({
|
||||
appId: config.appId,
|
||||
openid,
|
||||
@@ -1695,8 +1697,24 @@ export function createWechatMpService({
|
||||
.catch((err) => {
|
||||
logger.warn?.('WeChat MP route message count lookup failed:', err);
|
||||
return 0;
|
||||
})
|
||||
: 0;
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (typeof userAuth.getWechatAgentSessionSnapshotMessageCount === 'function') {
|
||||
counts.push(
|
||||
userAuth
|
||||
.getWechatAgentSessionSnapshotMessageCount(existingRoute.agentSessionId)
|
||||
.catch((err) => {
|
||||
logger.warn?.('WeChat MP snapshot message count lookup failed:', err);
|
||||
return 0;
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (counts.length > 0) {
|
||||
const resolved = await Promise.all(counts);
|
||||
routeMessageCount = Math.max(0, ...resolved.map((value) => Number(value) || 0));
|
||||
}
|
||||
}
|
||||
const routeIsTooLong =
|
||||
config.sessionMessageRotateCount > 0 &&
|
||||
Number(routeMessageCount ?? 0) >= config.sessionMessageRotateCount;
|
||||
@@ -1807,6 +1825,10 @@ export function createWechatMpService({
|
||||
}
|
||||
};
|
||||
|
||||
const scheduleWechatSessionSnapshotRefresh = (sessionId, userId) => {
|
||||
void refreshWechatSessionSnapshot(sessionId, userId);
|
||||
};
|
||||
|
||||
const rememberWechatUserContext = async (sessionId, user, { forceBootstrap = false } = {}) => {
|
||||
const addressName = resolveWechatAddressName(user);
|
||||
if (!addressName) return;
|
||||
@@ -1984,7 +2006,7 @@ export function createWechatMpService({
|
||||
publicBaseUrl: config.publicBaseUrl,
|
||||
artifacts: verifiedArtifacts.length > 0 ? verifiedArtifacts : confirmedArtifacts,
|
||||
});
|
||||
await refreshWechatSessionSnapshot(sessionId, user.userId);
|
||||
scheduleWechatSessionSnapshotRefresh(sessionId, user.userId);
|
||||
await sendCustomerServiceText(inbound.fromUserName, await guardScheduleReply(finalizedReply), user, {
|
||||
verifiedHtmlUrls: (verifiedArtifacts.length > 0 ? verifiedArtifacts : confirmedArtifacts).map(
|
||||
(artifact) => artifact.url,
|
||||
@@ -2053,7 +2075,7 @@ export function createWechatMpService({
|
||||
publicBaseUrl: config.publicBaseUrl,
|
||||
artifacts: verifiedArtifacts.length > 0 ? verifiedArtifacts : confirmedArtifacts,
|
||||
});
|
||||
await refreshWechatSessionSnapshot(sessionId, user.userId);
|
||||
scheduleWechatSessionSnapshotRefresh(sessionId, user.userId);
|
||||
await sendCustomerServiceText(inbound.fromUserName, await guardScheduleReply(finalizedReply), user, {
|
||||
verifiedHtmlUrls: (verifiedArtifacts.length > 0 ? verifiedArtifacts : confirmedArtifacts).map(
|
||||
(artifact) => artifact.url,
|
||||
|
||||
Reference in New Issue
Block a user