// 创作台统一错误映射(WP B-7,方案 §4.4「失败可理解」):图像 / 视频的提交错误与任务失败原因 // → 中文文案(i18n key)+ 下一步动作。纯函数、无 React / i18n 依赖,可直接 node --test。 // 旧视频页仍使用 helpers/videoTask.normalizeExperienceVideoError(只取 code / message),两者互不影响。 /** 下一步动作:界面据此渲染按钮(去充值 / 重试 / 修改提示词 / 重新上传)。 */ export const ERROR_ACTIONS = Object.freeze({ TOPUP: 'topup', RETRY: 'retry', EDIT_PROMPT: 'edit_prompt', REPLACE_IMAGE: 'replace_image', WAIT: 'wait', CONTACT_ADMIN: 'contact_admin', NONE: '', }); /** 动作按钮文案(i18n key);无按钮的动作为 ''。 */ export const ERROR_ACTION_LABELS = Object.freeze({ topup: '去充值', retry: '重试', edit_prompt: '修改提示词', replace_image: '重新上传', }); const MODERATION_CODES = new Set([ 'sensitive_words_detected', 'prompt_blocked', 'content_guard_error', 'content_policy_violation', 'content_moderation', 'moderation_blocked', 'data_inspection_failed', ]); const BALANCE_CODES = new Set([ 'insufficient_user_quota', 'insufficient_quota', 'quota_not_enough', 'pre_consume_token_quota_failed', ]); const TIMEOUT_CODES = new Set([ 'fallback_total_timeout', 'channel:response_time_exceeded', 'upstream_timeout', ]); const BUSY_CODES = new Set([ 'channel_not_enabled', 'get_channel_failed', 'channel:no_available_key', 'video_task_poller_unavailable', 'fallback_exhausted', 'task_busy', ]); const VALIDATION_CODES = new Set([ 'invalid_request', 'invalid_size', 'invalid_image', 'invalid_parameter', 'unsupported_parameter', 'unsupported_model', 'bad_request_body', 'too_many_ids', ]); const RE_EXPIRED = /原图已失效|image_result_expired|reference (image )?expired/i; const RE_BUDGET = /预算(已达上限|已停用|不足)|企业额度不足/; const RE_BALANCE = /余额不足|额度不足|insufficient[_ ](user[_ ])?(quota|balance)|quota (is )?not enough|quota exceeded/i; const RE_MODERATION = /审核|敏感|违规|不合规|安全策略|内容安全|content[ _-]?(policy|filter|moderation|safety)|moderation|sensitive|safety (system|check|filter)|nsfw|inappropriate|data[_ ]inspection/i; const RE_TIMEOUT = /timed? ?out|timeout|超时|deadline exceeded|context deadline/i; const RE_BUSY = /no available (channel|key)|无可用渠道|渠道.{0,6}不可用|service unavailable|overloaded|繁忙|rate limit|too many requests/i; const str = (v) => (typeof v === 'string' ? v : v == null ? '' : String(v)); /** * 从 axios 错误 / 接口响应体 / { code, message } / 失败原因字符串中取出 code、message、status、type。 */ export const extractCreationError = (input) => { if (typeof input === 'string') { return { code: '', message: input, status: 0, type: '' }; } const err = input || {}; const body = err.response?.data || err.data || err; const nested = body && typeof body.error === 'object' ? body.error : null; return { code: str(body?.code || nested?.code || err.code).trim(), message: str( body?.message || nested?.message || body?.description || err.failReason || err.message, ).trim(), status: Number(err.response?.status || err.status) || 0, type: str(body?.type || nested?.type).trim(), }; }; /** * 内容审核由哪个输入触发(后端在 message 中写明时): * { key: 'prompt' } | { key: 'first_frame' } | { key: 'last_frame' } * | { key: 'reference_images', index?: number(从 0 开始) } | { key: 'image' } | null */ export const moderationInputOf = (message) => { const m = str(message); if (!m) return null; const bracket = m.match(/reference[_ ]?images?\s*\[\s*(\d+)\s*\]/i); if (bracket) return { key: 'reference_images', index: Number(bracket[1]) }; const numbered = m.match( /(?:参考图|图|reference[_ ]?image|image)\s*#?\s*(\d{1,2})(?![\dx×])/i, ); if (numbered && Number(numbered[1]) > 0) { return { key: 'reference_images', index: Number(numbered[1]) - 1 }; } if (/首帧|first[_ ]?(frame|image)/i.test(m)) return { key: 'first_frame' }; if (/尾帧|last[_ ]?(frame|image)/i.test(m)) return { key: 'last_frame' }; if (/参考图|reference[_ ]?image/i.test(m)) { return { key: 'reference_images' }; } if (/提示词|prompt|text input|input text/i.test(m)) return { key: 'prompt' }; if (/图片|图像|image|photo|picture/i.test(m)) return { key: 'image' }; return null; }; const INPUT_LABELS = { first_frame: '首帧', last_frame: '尾帧', reference_images: '参考图', }; const moderationResult = (base, message) => { const input = moderationInputOf(message); const out = { ...base, category: 'moderation', titleKey: '内容审核未通过', input, }; if (!input) { return { ...out, detailKey: '提示词或图片触发了内容安全策略,请修改后重试', action: ERROR_ACTIONS.EDIT_PROMPT, }; } if (input.key === 'prompt') { return { ...out, detailKey: '提示词触发了内容安全策略,请修改后重试', action: ERROR_ACTIONS.EDIT_PROMPT, }; } if (input.key === 'image') { return { ...out, detailKey: '上传的图片触发了内容安全策略,请更换图片', action: ERROR_ACTIONS.REPLACE_IMAGE, }; } return { ...out, detailKey: '{{input}}触发了内容安全策略,请更换图片', detailParams: { inputKey: INPUT_LABELS[input.key], inputIndex: input.key === 'reference_images' && Number.isInteger(input.index) ? input.index + 1 : 0, }, action: ERROR_ACTIONS.REPLACE_IMAGE, }; }; /** * 统一错误分类。 * @param {any} input axios 错误、接口响应体、{ code, message, status } 或失败原因字符串 * @param {{ running?: number }} [extra] too_many_running_tasks 时进行中的任务数 * @returns {{ * category: 'concurrency'|'image_expired'|'budget'|'balance'|'moderation'|'timeout' * |'busy'|'model_unavailable'|'validation'|'unknown', * code: string, raw: string, action: string, * titleKey: string, titleParams?: object, passthrough?: boolean, * detailKey?: string, detailParams?: object, input?: object|null, * }} * passthrough=true 时标题直接使用 raw(后端校验文案,已是中文)。 */ export const classifyCreationError = (input, extra = {}) => { const { code, message, status, type } = extractCreationError(input); const raw = message; const lower = code.toLowerCase(); const base = { code, raw, action: ERROR_ACTIONS.NONE }; if (lower === 'too_many_running_tasks' || (!code && status === 429)) { const running = Number(extra.running) || 0; return running > 0 ? { ...base, category: 'concurrency', titleKey: '已有 {{n}} 个任务进行中,完成后可继续', titleParams: { n: running }, action: ERROR_ACTIONS.WAIT, } : { ...base, category: 'concurrency', titleKey: '进行中的任务过多,请稍后再试', action: ERROR_ACTIONS.WAIT, }; } if (RE_EXPIRED.test(raw) || lower === 'image_result_expired') { return { ...base, category: 'image_expired', titleKey: '引用的图片已失效,请重新上传', action: ERROR_ACTIONS.REPLACE_IMAGE, }; } if (RE_BUDGET.test(raw)) { return { ...base, category: 'budget', titleKey: raw, passthrough: true, detailKey: '请联系企业管理员调整预算', action: ERROR_ACTIONS.CONTACT_ADMIN, }; } if (BALANCE_CODES.has(lower) || RE_BALANCE.test(raw)) { return { ...base, category: 'balance', titleKey: '余额不足,请充值后再试', action: ERROR_ACTIONS.TOPUP, }; } if ( MODERATION_CODES.has(lower) || type === 'content_guard_error' || RE_MODERATION.test(raw) ) { return moderationResult(base, raw); } if (TIMEOUT_CODES.has(lower) || status === 504 || RE_TIMEOUT.test(raw)) { return { ...base, category: 'timeout', titleKey: '上游响应超时,请稍后重试', action: ERROR_ACTIONS.RETRY, }; } if (lower === 'model_not_found') { return { ...base, category: 'model_unavailable', titleKey: '模型暂不可用,请切换其他模型', }; } if ( BUSY_CODES.has(lower) || status === 502 || status === 503 || RE_BUSY.test(raw) ) { return { ...base, category: 'busy', titleKey: '服务繁忙,请稍后重试', action: ERROR_ACTIONS.RETRY, }; } if ((VALIDATION_CODES.has(lower) || status === 400) && raw) { return { ...base, category: 'validation', titleKey: raw, passthrough: true, }; } return { ...base, category: 'unknown', titleKey: '生成失败', action: ERROR_ACTIONS.RETRY, }; }; /** * 翻译分类结果:{ title, detail, actionLabel }。 * detail 在没有映射说明时回退为原始信息(与标题相同时省略)。 */ export const describeCreationError = (t, info) => { const tr = typeof t === 'function' ? t : (k) => k; if (!info) return { title: '', detail: '', actionLabel: '' }; const title = info.passthrough ? info.raw : tr(info.titleKey, info.titleParams); let detail = ''; if (info.detailKey) { const p = info.detailParams; if (p && p.inputKey) { const label = tr(p.inputKey); detail = tr(info.detailKey, { input: p.inputIndex ? `${label} ${p.inputIndex}` : label, }); } else { detail = tr(info.detailKey, p); } } else if (info.category === 'unknown' && info.raw && info.raw !== title) { detail = info.raw; } const labelKey = ERROR_ACTION_LABELS[info.action] || ''; return { title, detail, actionLabel: labelKey ? tr(labelKey) : '' }; }; // --------------------------------------------------------------------------- // 估价 dry-run 的预算不足(quote.balance_enough=false 时的 budget_source,见 // service/org_quota_dryrun.go):个人钱包 → 去充值;企业 / 令牌 → 说明是哪一级预算不足。 // --------------------------------------------------------------------------- const PERSONAL_SOURCES = new Set(['user', 'membership', 'gift', 'unknown', '']); const ORG_BUDGET_LABELS = { department: '部门预算不足', project: '项目预算不足', member: '成员预算不足', token: '令牌预算不足', org: '企业预算不足', }; /** * @returns {null | { personal: boolean, source: string, labelKey: string }} */ export const budgetShortfallOf = (quote) => { if (!quote || quote.balance_enough !== false) return null; const source = str(quote.budget_source).trim(); if (PERSONAL_SOURCES.has(source)) { return { personal: true, source, labelKey: '余额不足' }; } if (source.startsWith('org_budget:')) { const subject = source.slice('org_budget:'.length); return { personal: false, source, labelKey: ORG_BUDGET_LABELS[subject] || '企业预算不足', }; } if (source === 'token') { return { personal: false, source, labelKey: '令牌额度不足' }; } // org / org_member / org_gift:企业额度 return { personal: false, source, labelKey: '企业额度不足' }; };