83 lines
2.4 KiB
JavaScript
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();
|
||
|
|
}
|