From da422a82e678073b23cfb5661c1c07e4db05210f Mon Sep 17 00:00:00 2001 From: john Date: Sun, 12 Jul 2026 22:44:18 +0800 Subject: [PATCH] fix(wechat): restore session routing exports after merge Co-authored-by: Cursor --- wechat-mp.mjs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/wechat-mp.mjs b/wechat-mp.mjs index ebb686c..30f93ed 100644 --- a/wechat-mp.mjs +++ b/wechat-mp.mjs @@ -21,6 +21,7 @@ import { } from './wechat/handlers/sync-replies.mjs'; import { classifyWechatIntent } from './wechat/intent/classifier.mjs'; import { isPageGenerateText, isTopicResetText } from './wechat/intent/patterns.mjs'; +import { isPageDataIntent } from './chat-skills.mjs'; import { resolvePageGenerateOutcome } from './wechat/handlers/page-generate.mjs'; import { buildWechatAgentPrompt } from './wechat/prompts/chat-general.mjs'; import { @@ -973,6 +974,14 @@ function isTopicResetIntent(text) { return isTopicResetText(text); } +export function shouldForceNewWechatAgentSession(wechatIntent, resetCandidate) { + return ( + wechatIntent?.kind === 'session.reset' || + isTopicResetIntent(resetCandidate) || + isPageDataIntent(resetCandidate) + ); +} + export function isRecoverableWechatAgentSessionError(message) { const normalized = String(message ?? '').trim(); if (!normalized) return false; @@ -1907,7 +1916,11 @@ export function createWechatMpService({ const wechatIntent = classifyWechatIntent(intent); const resetCandidate = intent.msgType === 'text' || intent.msgType === 'voice' ? intent.agentText : ''; - const forceNew = wechatIntent.kind === 'session.reset' || isTopicResetIntent(resetCandidate); + // Page Data delivery owns persistent files, datasets and two publication + // policies. Reusing a conversational route here can make a new request + // inspect/retry unrelated historical pages from that session. + const isPageDataRequest = isPageDataIntent(resetCandidate); + const forceNew = shouldForceNewWechatAgentSession(wechatIntent, resetCandidate); let route = await ensureWechatAgentSession({ userId: user.userId, openid: inbound.fromUserName, @@ -2066,6 +2079,24 @@ export function createWechatMpService({ return { sessionId }; } catch (err) { const message = err instanceof Error ? err.message : String(err); + // A Page Data request must fail closed. Retrying a poisoned completion in + // another session while the finish guard is also active can turn one + // request into repairs against historical pages. Drop only this user's + // route so the next explicit request starts cleanly. + if (isPageDataRequest) { + await userAuth.clearWechatAgentRoute(config.appId, inbound.fromUserName).catch((clearErr) => { + logger.warn?.('WeChat MP page data route clear failed:', clearErr); + }); + if (!err?.wechatUserNotified) { + const text = buildPageDataCollectFailureText(); + try { + await sendCustomerServiceText(inbound.fromUserName, text, user); + } catch (sendErr) { + logger.error?.('WeChat MP page data failure notice failed:', sendErr); + } + } + throw markWechatUserNotified(err instanceof Error ? err : new Error(message)); + } const mayBeStaleSession = sessionId && isRecoverableWechatAgentSessionError(message); if (mayBeStaleSession) { route = await ensureWechatAgentSession({