Files
memind/wechat-cursor-page-timeout-takeover.mjs
T
john 2c3c1f7a52
Memind CI / Test, build, and release guards (push) Failing after 2m37s
feat(wechat): hand off page.generate timeouts to Cursor after 20 minutes
Wire WECHAT_AGENT_REPLY_TIMEOUT into the same Cursor takeover path used by
agent runs, raise the default WeChat reply timeout to 20 minutes, and fall
back to env-based tool-gateway detection when runtime exports are missing.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-11 15:31:19 +08:00

83 lines
2.4 KiB
JavaScript

import crypto from 'node:crypto';
import {
cursorExecutorEnabled,
resolvePreferredCodeExecutor,
} from './cursor-agent-launch.mjs';
import {
buildPageTimeoutTakeoverRowFromDisplayText,
resolveCursorPageTimeoutTakeover,
} from './cursor-page-timeout-takeover.mjs';
import { executeCursorTakeoverRun } from './wechat-cursor-agent-run.mjs';
function resolveToolGatewayStatus(agentRunGateway, env = process.env) {
const fromGateway = agentRunGateway?.getToolGatewayStatus?.()
?? agentRunGateway?.getQueueStatus?.()?.toolGateway
?? null;
if (fromGateway?.enabled) return fromGateway;
if (!cursorExecutorEnabled(env) || resolvePreferredCodeExecutor(env) !== 'cursor') {
return null;
}
return { enabled: true, executors: ['cursor'] };
}
export function buildWechatPageTimeoutTakeoverRow(intent) {
return buildPageTimeoutTakeoverRowFromDisplayText(
intent?.displayText ?? intent?.agentText ?? '',
);
}
export async function attemptWechatCursorPageTimeoutTakeover({
error = null,
intent = null,
wechatIntent = null,
userId,
sessionId = null,
requestId,
agentRunGateway = null,
toolGatewayStatus = null,
timeoutMs = 20 * 60 * 1000,
logger = console,
env = process.env,
} = {}) {
if (String(error?.code ?? '') !== 'WECHAT_AGENT_REPLY_TIMEOUT') return null;
if (wechatIntent?.kind !== 'page.generate') return null;
if (!agentRunGateway?.createRun || !agentRunGateway?.dispatchRun) return null;
const takeover = resolveCursorPageTimeoutTakeover({
row: buildWechatPageTimeoutTakeoverRow(intent),
error,
toolGatewayStatus:
toolGatewayStatus
?? resolveToolGatewayStatus(agentRunGateway, env),
env,
});
if (!takeover) return null;
const takeoverRequestId = `${String(requestId ?? '').trim() || cryptoRandomId()}-cursor-takeover`;
logger.info?.('[wechat-mp] cursor page timeout takeover starting', {
userId,
sessionId,
requestId: takeoverRequestId,
});
try {
return await executeCursorTakeoverRun({
agentRunGateway,
userId,
sessionId,
requestId: takeoverRequestId,
userMessage: takeover.userMessage,
runOptions: takeover.runOptions,
timeoutMs,
logger,
});
} catch (takeoverErr) {
logger.warn?.('[wechat-mp] cursor page timeout takeover failed:', takeoverErr);
return null;
}
}
function cryptoRandomId() {
return crypto.randomUUID();
}