fix: harden agent qwen vision fallback stability
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-02-21 05:33:14 +08:00
parent acd37e8c57
commit 8cd3fa2954

View File

@ -238,10 +238,34 @@ const ANALYZER_SYSTEM_PROMPT = `你是一个友好的AI图片编辑助手。用
"needsClarification": false
}`;
function getEnvInt(name: string, fallback: number, min: number, max: number): number {
const raw = process.env[name];
const parsed = raw ? Number.parseInt(raw, 10) : Number.NaN;
if (!Number.isFinite(parsed)) return fallback;
return Math.max(min, Math.min(max, parsed));
}
const ANALYZER_MAX_IMAGE_COUNT = 2;
const ANALYZER_IMAGE_MAX_SIDE = 500;
const ANALYZER_TIMEOUT_MS = 120000;
const ANALYZER_IMAGE_FETCH_TIMEOUT_MS = ANALYZER_TIMEOUT_MS;
const ANALYZER_IMAGE_FETCH_TIMEOUT_MS = getEnvInt(
"AGENT_IMAGE_FETCH_TIMEOUT_MS",
15000,
3000,
60000,
);
const ANALYZER_QWEN_FALLBACK_TIMEOUT_MS = getEnvInt(
"AGENT_QWEN_FALLBACK_TIMEOUT_MS",
35000,
10000,
90000,
);
const ANALYZER_QWEN_FALLBACK_RETRIES = getEnvInt(
"AGENT_QWEN_FALLBACK_RETRIES",
0,
0,
1,
);
const ZHIPU_VISION_MODEL_CANDIDATES = [
"glm-4.6v-flash",
"glm-4.6v",
@ -375,12 +399,13 @@ async function getQwen3VlFallbackImageCandidates(imageUrls?: string[]): Promise<
const firstImageUrl = (imageUrls || []).find((url) => isHttpUrl(url));
if (!firstImageUrl) return [];
const candidates: string[] = [firstImageUrl];
const candidates: string[] = [];
const inlineDataUrl = await toAnalyzerDataUrl(firstImageUrl);
if (inlineDataUrl) {
candidates.push(inlineDataUrl);
}
return candidates;
candidates.push(firstImageUrl);
return Array.from(new Set(candidates));
}
async function analyzeWithHardcodedQwen3VlFallback(params: {
@ -434,8 +459,8 @@ async function analyzeWithHardcodedQwen3VlFallback(params: {
prompt,
temperature: 0.1,
maxTokens: 360,
timeoutMs: ANALYZER_TIMEOUT_MS,
retries: 1,
timeoutMs: ANALYZER_QWEN_FALLBACK_TIMEOUT_MS,
retries: ANALYZER_QWEN_FALLBACK_RETRIES,
});
const parsed = parseAnalysisResultFromModelText(
fallbackText,
@ -486,8 +511,8 @@ async function summarizeWithHardcodedQwen3VlFallback(params: {
prompt,
temperature: 0.1,
maxTokens: 140,
timeoutMs: ANALYZER_TIMEOUT_MS,
retries: 1,
timeoutMs: ANALYZER_QWEN_FALLBACK_TIMEOUT_MS,
retries: ANALYZER_QWEN_FALLBACK_RETRIES,
});
const normalized = normalizeVisualSummaryText(text);
if (normalized && !isWeakVisualSummary(normalized)) {