From 160552b45e96db60a69f80eaf10b2fa6bebed408 Mon Sep 17 00:00:00 2001 From: Stringadmin Date: Tue, 2 Jun 2026 15:20:23 +0800 Subject: [PATCH 1/4] fix(ecommerce): 502 bug - vision model upgrade + MIME normalization + fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Upgrade VISION_MODEL to qwen3.7-plus (latest, confirmed working with image_url) - Add VISION_FALLBACK_MODEL = qwen-vl-plus for retry on "image format" errors - Normalize upload MIME types: unsupported formats (HEIC/AVIF) fall back to image/png to prevent server saving as .bin which DashScope can't read - Server-side: add image/avif, image/heic, image/heif to MIME_EXTENSIONS Root cause: DashScope returned "image format is illegal" when uploaded images had unrecognized MIME types → saved as .bin → DashScope couldn't decode. Co-Authored-By: Claude Opus 4.7 --- src/api/adVideoPlanClient.ts | 60 ++++++++++++++---------- src/features/ecommerce/EcommercePage.tsx | 4 +- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/api/adVideoPlanClient.ts b/src/api/adVideoPlanClient.ts index ae700ea..fc29983 100644 --- a/src/api/adVideoPlanClient.ts +++ b/src/api/adVideoPlanClient.ts @@ -1,7 +1,8 @@ import { buildApiUrl, buildAuthHeaders } from "./serverConnection"; const TEXT_MODEL = "qwen-max"; -const VISION_MODEL = "qwen3.6-plus"; +const VISION_MODEL = "qwen3.7-plus"; +const VISION_FALLBACK_MODEL = "qwen-vl-plus"; export interface AdVideoUserConfig { platform: string; @@ -149,30 +150,39 @@ async function visionChat( ...imageUrls.map((url) => ({ type: "image_url", image_url: { url } })), { type: "text", text }, ]; - const timeoutSignal = AbortSignal.timeout(60000); - const combinedSignal = signal - ? AbortSignal.any([signal, timeoutSignal]) - : timeoutSignal; - const res = await fetch(buildApiUrl("ai/chat"), { - method: "POST", - headers: buildAuthHeaders(), - body: JSON.stringify({ - model: VISION_MODEL, - messages: [ - { role: "system", content: systemPrompt }, - { role: "user", content }, - ], - stream: false, - temperature: 0.3, - }), - signal: combinedSignal, - }); - if (!res.ok) throw new Error(`图片理解调用失败 (${res.status})`); - const payload = await res.json(); - const out: string = - payload?.content ?? payload?.choices?.[0]?.message?.content ?? payload?.text ?? ""; - if (!out) throw new Error("图片理解未返回有效内容"); - return out; + const messages = [ + { role: "system", content: systemPrompt }, + { role: "user", content }, + ]; + + for (const model of [VISION_MODEL, VISION_FALLBACK_MODEL]) { + const timeoutSignal = AbortSignal.timeout(60000); + const combinedSignal = signal + ? AbortSignal.any([signal, timeoutSignal]) + : timeoutSignal; + try { + const res = await fetch(buildApiUrl("ai/chat"), { + method: "POST", + headers: buildAuthHeaders(), + body: JSON.stringify({ model, messages, stream: false, temperature: 0.3 }), + signal: combinedSignal, + }); + if (!res.ok) { + const errBody = await res.text().catch(() => ""); + if (model === VISION_MODEL && errBody.includes("image format")) continue; + throw new Error(`图片理解调用失败 (${res.status})`); + } + const payload = await res.json(); + const out: string = + payload?.content ?? payload?.choices?.[0]?.message?.content ?? payload?.text ?? ""; + if (!out) throw new Error("图片理解未返回有效内容"); + return out; + } catch (err) { + if (model === VISION_MODEL && err instanceof Error && err.message?.includes("图片理解调用失败")) continue; + throw err; + } + } + throw new Error("图片理解调用失败,所有模型均不可用"); } const IMAGE_UNDERSTANDING_PROMPT = `你是电商产品图片分析专家。请分析用户提供的产品图片,识别产品主体、外观、颜色、材质、形状、尺寸感、品牌标识、关键部件、可视化卖点和适合展示的镜头角度。请用简洁的中文段落描述,不要编造图片中看不到的信息。`; diff --git a/src/features/ecommerce/EcommercePage.tsx b/src/features/ecommerce/EcommercePage.tsx index 1f561ba..9774b8a 100644 --- a/src/features/ecommerce/EcommercePage.tsx +++ b/src/features/ecommerce/EcommercePage.tsx @@ -1321,18 +1321,20 @@ function ProductClonePage(_props: ProductClonePageProps = {}) { }); const uploadProductImages = async (): Promise => { + const SUPPORTED_IMAGE_TYPES = new Set(["image/jpeg", "image/png", "image/webp", "image/gif"]); const urls: string[] = []; for (const item of productImages) { try { const resp = await fetch(item.src); const blob = await resp.blob(); + const mimeType = SUPPORTED_IMAGE_TYPES.has(blob.type) ? blob.type : "image/png"; const dataUrl = await new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => resolve(String(reader.result)); reader.onerror = () => reject(reader.error); reader.readAsDataURL(blob); }); - const { url } = await aiGenerationClient.uploadAsset({ dataUrl, name: item.name, mimeType: blob.type }); + const { url } = await aiGenerationClient.uploadAsset({ dataUrl, name: item.name, mimeType }); urls.push(url); } catch { // skip images that fail to upload -- 2.52.0 From 44c748b0dc7fc2e18589de2f90486ea40e6660e5 Mon Sep 17 00:00:00 2001 From: Stringadmin Date: Tue, 2 Jun 2026 16:03:50 +0800 Subject: [PATCH 2/4] feat(ecommerce): use FormData binary upload instead of base64 dataUrl - Add uploadAssetBinary method to aiGenerationClient (FormData + busboy) - Replace base64 dataUrl upload in uploadProductImages with direct blob upload via /oss/upload-binary multipart endpoint - This eliminates the DATA_URL_PATTERN regex parsing bug that produced 44-byte corrupt files on OSS, causing DashScope "image format illegal" errors Co-Authored-By: Claude Opus 4.7 --- src/api/aiGenerationClient.ts | 16 ++++++++++++++++ src/features/ecommerce/EcommercePage.tsx | 13 ++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/api/aiGenerationClient.ts b/src/api/aiGenerationClient.ts index f580b5a..032ef9f 100644 --- a/src/api/aiGenerationClient.ts +++ b/src/api/aiGenerationClient.ts @@ -403,6 +403,22 @@ export const aiGenerationClient = { return readJsonResponse<{ url: string; ossKey?: string }>(res, "Asset upload response failed"); }, + async uploadAssetBinary(blob: Blob, options?: { name?: string; mimeType?: string; scope?: string }): Promise<{ url: string; signedUrl?: string; ossKey?: string }> { + const form = new FormData(); + form.append("file", blob, options?.name || "upload.png"); + if (options?.scope) form.append("scope", options.scope); + if (options?.mimeType) form.append("mimeType", options.mimeType); + const res = await fetch(buildApiUrl("oss/upload-binary"), { + method: "POST", + headers: { ...buildAuthHeaders() }, + body: form, + }); + if (!res.ok) { + await throwResponseError(res, "Binary asset upload failed"); + } + return readJsonResponse<{ url: string; ossKey?: string }>(res, "Binary asset upload response failed"); + }, + async uploadAssetByUrl(input: UploadAssetByUrlInput): Promise<{ url: string; signedUrl?: string; ossKey?: string }> { const res = await fetch(buildApiUrl("oss/upload-by-url"), { method: "POST", diff --git a/src/features/ecommerce/EcommercePage.tsx b/src/features/ecommerce/EcommercePage.tsx index 9774b8a..3c500ba 100644 --- a/src/features/ecommerce/EcommercePage.tsx +++ b/src/features/ecommerce/EcommercePage.tsx @@ -1326,15 +1326,10 @@ function ProductClonePage(_props: ProductClonePageProps = {}) { for (const item of productImages) { try { const resp = await fetch(item.src); - const blob = await resp.blob(); - const mimeType = SUPPORTED_IMAGE_TYPES.has(blob.type) ? blob.type : "image/png"; - const dataUrl = await new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.onload = () => resolve(String(reader.result)); - reader.onerror = () => reject(reader.error); - reader.readAsDataURL(blob); - }); - const { url } = await aiGenerationClient.uploadAsset({ dataUrl, name: item.name, mimeType }); + const rawBlob = await resp.blob(); + const mimeType = SUPPORTED_IMAGE_TYPES.has(rawBlob.type) ? rawBlob.type : "image/png"; + const blob = rawBlob.type === mimeType ? rawBlob : new Blob([rawBlob], { type: mimeType }); + const { url } = await aiGenerationClient.uploadAssetBinary(blob, { name: item.name, mimeType, scope: "ecommerce-product" }); urls.push(url); } catch { // skip images that fail to upload -- 2.52.0 From 9504f8ee872dc9b01333338868d3bcde399f5f41 Mon Sep 17 00:00:00 2001 From: Stringadmin Date: Tue, 2 Jun 2026 16:16:09 +0800 Subject: [PATCH 3/4] fix(ecommerce): replace base64 upload with binary blob in video service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit runVideoPlan was passing blob URLs as "dataUrl" to uploadAssetWithProgress, which sent them to /api/oss/upload (base64 path). Blob URLs don't match DATA_URL_PATTERN regex, causing corrupt 44-byte files on OSS. Now uses uploadAssetBinary (FormData multipart) via /api/oss/upload-binary, fetching blob → uploading binary directly, same as EcommercePage path. Co-Authored-By: Claude Opus 4.7 --- .../ecommerce/ecommerceVideoService.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/features/ecommerce/ecommerceVideoService.ts b/src/features/ecommerce/ecommerceVideoService.ts index f1f7639..5f92e86 100644 --- a/src/features/ecommerce/ecommerceVideoService.ts +++ b/src/features/ecommerce/ecommerceVideoService.ts @@ -9,7 +9,6 @@ import { type AdVideoUserConfig, } from "../../api/adVideoPlanClient"; import { aiGenerationClient } from "../../api/aiGenerationClient"; -import { uploadAssetWithProgress } from "../../api/uploadWithProgress"; import { waitForTask } from "../../api/taskSubscription"; import { resolveVideoRequestModel } from "../../utils/resolveVideoModel"; import type { @@ -34,12 +33,18 @@ export async function runVideoPlan( onStepStart("upload"); const imageUrls: string[] = []; - for (const dataUrl of imageDataUrls) { - const result = await uploadAssetWithProgress( - { dataUrl, scope: "ecommerce-product", mimeType: "image/png" }, - { signal }, - ); - imageUrls.push(result.url); + const SUPPORTED_IMAGE_TYPES = new Set(["image/jpeg", "image/png", "image/webp", "image/gif"]); + for (const srcUrl of imageDataUrls) { + try { + const resp = await fetch(srcUrl); + const rawBlob = await resp.blob(); + const mimeType = SUPPORTED_IMAGE_TYPES.has(rawBlob.type) ? rawBlob.type : "image/png"; + const blob = rawBlob.type === mimeType ? rawBlob : new Blob([rawBlob], { type: mimeType }); + const result = await aiGenerationClient.uploadAssetBinary(blob, { mimeType, scope: "ecommerce-product" }); + imageUrls.push(result.url); + } catch { + // skip images that fail to upload + } } onStepDone("upload"); -- 2.52.0 From 94c1453c9bc4670d5351f687999f7f8f0c288bb0 Mon Sep 17 00:00:00 2001 From: Stringadmin Date: Tue, 2 Jun 2026 16:58:59 +0800 Subject: [PATCH 4/4] fix: upload-binary Content-Type fix, 429/timeout retry, 120s timeout - Remove Content-Type: application/json from uploadAssetBinary FormData request - Add retryOnTransient for 429 + timeout + signal timed out errors - Increase AI chat timeout from 60s to 120s per call - Apply retry logic to both chat() and visionChat() --- src/api/adVideoPlanClient.ts | 113 ++++++++++++++++++++++------------ src/api/aiGenerationClient.ts | 4 +- 2 files changed, 75 insertions(+), 42 deletions(-) diff --git a/src/api/adVideoPlanClient.ts b/src/api/adVideoPlanClient.ts index fc29983..2bae189 100644 --- a/src/api/adVideoPlanClient.ts +++ b/src/api/adVideoPlanClient.ts @@ -108,36 +108,63 @@ interface ChatMessage { content: string; } +const MAX_RETRIES = 3; +const RETRY_BASE_MS = 2000; +const CHAT_TIMEOUT_MS = 120_000; // 2 minutes per AI call + +function isTransientError(err: unknown): boolean { + if (!(err instanceof Error)) return false; + const msg = err.message.toLowerCase(); + return /\b429\b/.test(msg) || msg.includes("signal timed out") || msg.includes("aborted") || msg.includes("timeout"); +} + +async function retryOnTransient(fn: () => Promise, signal?: AbortSignal): Promise { + for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) { + try { + return await fn(); + } catch (err) { + if (signal?.aborted) throw err; + if (attempt === MAX_RETRIES) throw err; + if (!isTransientError(err)) throw err; + const delay = RETRY_BASE_MS * 2 ** attempt + Math.random() * 1000; + await new Promise((r) => setTimeout(r, delay)); + } + } + throw new Error("unreachable"); +} + async function chat( systemPrompt: string, userContent: string, options?: { model?: string; signal?: AbortSignal }, ): Promise { - const messages: ChatMessage[] = [ - { role: "system", content: systemPrompt }, - { role: "user", content: userContent }, - ]; - const timeoutSignal = AbortSignal.timeout(60000); - const combinedSignal = options?.signal - ? AbortSignal.any([options.signal, timeoutSignal]) - : timeoutSignal; - const res = await fetch(buildApiUrl("ai/chat"), { - method: "POST", - headers: buildAuthHeaders(), - body: JSON.stringify({ - model: options?.model ?? TEXT_MODEL, - messages, - stream: false, - temperature: 0.4, - }), - signal: combinedSignal, - }); - if (!res.ok) throw new Error(`AI 调用失败 (${res.status})`); - const payload = await res.json(); - const content: string = - payload?.content ?? payload?.choices?.[0]?.message?.content ?? payload?.text ?? ""; - if (!content) throw new Error("模型未返回有效内容"); - return content; + return retryOnTransient(async () => { + const messages: ChatMessage[] = [ + { role: "system", content: systemPrompt }, + { role: "user", content: userContent }, + ]; + const timeoutSignal = AbortSignal.timeout(CHAT_TIMEOUT_MS); + const combinedSignal = options?.signal + ? AbortSignal.any([options.signal, timeoutSignal]) + : timeoutSignal; + const res = await fetch(buildApiUrl("ai/chat"), { + method: "POST", + headers: buildAuthHeaders(), + body: JSON.stringify({ + model: options?.model ?? TEXT_MODEL, + messages, + stream: false, + temperature: 0.4, + }), + signal: combinedSignal, + }); + if (!res.ok) throw new Error(`AI 调用失败 (${res.status})`); + const payload = await res.json(); + const content: string = + payload?.content ?? payload?.choices?.[0]?.message?.content ?? payload?.text ?? ""; + if (!content) throw new Error("模型未返回有效内容"); + return content; + }, options?.signal); } async function visionChat( @@ -156,28 +183,32 @@ async function visionChat( ]; for (const model of [VISION_MODEL, VISION_FALLBACK_MODEL]) { - const timeoutSignal = AbortSignal.timeout(60000); + const timeoutSignal = AbortSignal.timeout(CHAT_TIMEOUT_MS); const combinedSignal = signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal; try { - const res = await fetch(buildApiUrl("ai/chat"), { - method: "POST", - headers: buildAuthHeaders(), - body: JSON.stringify({ model, messages, stream: false, temperature: 0.3 }), - signal: combinedSignal, - }); - if (!res.ok) { - const errBody = await res.text().catch(() => ""); - if (model === VISION_MODEL && errBody.includes("image format")) continue; - throw new Error(`图片理解调用失败 (${res.status})`); - } - const payload = await res.json(); - const out: string = - payload?.content ?? payload?.choices?.[0]?.message?.content ?? payload?.text ?? ""; - if (!out) throw new Error("图片理解未返回有效内容"); + const out = await retryOnTransient(async () => { + const res = await fetch(buildApiUrl("ai/chat"), { + method: "POST", + headers: buildAuthHeaders(), + body: JSON.stringify({ model, messages, stream: false, temperature: 0.3 }), + signal: combinedSignal, + }); + if (!res.ok) { + const errBody = await res.text().catch(() => ""); + if (model === VISION_MODEL && errBody.includes("image format")) throw new Error("IMAGE_FORMAT_FALLBACK"); + throw new Error(`图片理解调用失败 (${res.status})`); + } + const payload = await res.json(); + const result: string = + payload?.content ?? payload?.choices?.[0]?.message?.content ?? payload?.text ?? ""; + if (!result) throw new Error("图片理解未返回有效内容"); + return result; + }, signal); return out; } catch (err) { + if (err instanceof Error && err.message === "IMAGE_FORMAT_FALLBACK") continue; if (model === VISION_MODEL && err instanceof Error && err.message?.includes("图片理解调用失败")) continue; throw err; } diff --git a/src/api/aiGenerationClient.ts b/src/api/aiGenerationClient.ts index 032ef9f..1335847 100644 --- a/src/api/aiGenerationClient.ts +++ b/src/api/aiGenerationClient.ts @@ -408,9 +408,11 @@ export const aiGenerationClient = { form.append("file", blob, options?.name || "upload.png"); if (options?.scope) form.append("scope", options.scope); if (options?.mimeType) form.append("mimeType", options.mimeType); + // Exclude Content-Type so browser auto-sets multipart/form-data with boundary + const { "Content-Type": _ct, ...authHeaders } = buildAuthHeaders(); const res = await fetch(buildApiUrl("oss/upload-binary"), { method: "POST", - headers: { ...buildAuthHeaders() }, + headers: authHeaders, body: form, }); if (!res.ok) { -- 2.52.0