sync: 同步总仓库 luoguang/monkey-ai weiyuan@bfdfb339 的 web/ 改动
同步基准:总仓库 a2ca13cb(= 本仓库初始导入 d8f3aee2) 范围:a2ca13cb..bfdfb339 中触及 web/ 的提交(不含 web/.claude/settings.local.json) - 8490ac02 feat(aliyun): integrate Wan 3 and Qwen Image models - 2aff08ec perf(analytics): 运营看板概览改读汇总表,首屏从 19s 降到 0.4s - dc69335e feat(experience): 体验中心图像/视频创作升级(能力 schema、估价、新创作台) - b059c657 fix(org): 下线 /org/create 自助加入企业页与 POST /api/org/join - 4fcf47e6 fix(i18n): 修复中/英/韩/繁体多语言错乱,补齐日/越/泰译文 - e4c301d5 fix: merge enterprise task record date filters - c9369b95 feat: expose enterprise image and video task records - a410fee5 feat(claude): 无断点请求自动注入 cache_control 断点(方案 2) - 521b3376 feat(claude): 缓存命中率优化——上游粘性路由 + 缓存时长强制改写 - 73b615dd feat(pricing): 企业成员不论登录身份均按所属企业等级计价与扣费 - f73f55c3 feat(client): 新增 WeyAi 版本说明管理与公开读取接口 - 8cdf1994 fix(topup): reset amount when switching from PayPal - 93a4ac34 feat(baidu): configure Seedance liveness header by channel - cb1818c5 fix(weyai): 按评审修正内容清理竞态、默认适用端与中文退回(M21) - 04214d83 feat(weyai): WeyAi 内容管理与客户端内容接口(M21 第二部分) - e3c4a57c feat(weyai): 设置页新增 WeyAi 页签与内容专用 COS 桶(M21 第一部分) - b5ee8e70 fix(auth): 会话角色/状态以 DB 为准,修复提升管理员后管理接口报权限不足 - e4618fc2 fix: 修复企业看板日志聚合死循环导致 CPU 打满,并降低看板查询开销 - 584aad52 style(web): 个人中心侧边栏按 prettier 换行 - b8d87064 fix: 小程序教程隐藏充值后保留原步骤编号,企业充值判断直接用 isMiniProgramWebView - eee7f5b2 fix: 小程序里企业首页不落到充值页,隐藏钱包入口与教程充值步骤 - 8b254d49 fix: 小程序里隐藏企业充值入口与页面 - a67661b4 fix(web): 小程序里个人中心默认页跳过充值和发票,避免来回跳转 - cff6c543 feat(web): WeyAi 小程序里打开个人中心时不出现充值和开票 - c6c99eb6 feat(billing): Claude 全部缓存写入统一按 1h 倍率计费 - 0699d1bf fix(web): 小程序 web-view 里注册页同样只显示站内方式 - 04853331 feat(web): WeyAi 小程序 web-view 里登录页只显示站内登录方式
This commit is contained in:
@@ -0,0 +1,372 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
// 创作台统一错误映射(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: '企业额度不足' };
|
||||
};
|
||||
Reference in New Issue
Block a user