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,403 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
DRAFT_STORAGE_KEY,
|
||||
buildImageRequest,
|
||||
clampSizeSide,
|
||||
composeParams,
|
||||
formFromImageRequest,
|
||||
formFromLegacyTask,
|
||||
imageBlockReason,
|
||||
imageSlotsOf,
|
||||
loadDraft,
|
||||
modeOptionsOf,
|
||||
normalizeSize,
|
||||
pickModeForModel,
|
||||
quoteRequestOf,
|
||||
referenceMaxOf,
|
||||
requestParamsOf,
|
||||
saveDraft,
|
||||
} from './imageCreation.js';
|
||||
|
||||
const sizeOpt = (w, h, label) => ({
|
||||
value: `${w}x${h}`,
|
||||
label,
|
||||
width: w,
|
||||
height: h,
|
||||
});
|
||||
|
||||
// 允许自定义尺寸、可编辑(最多 3 张参考图)的模型
|
||||
const gemini = {
|
||||
model: 'gemini-image',
|
||||
modes: ['text2image', 'edit'],
|
||||
params: [
|
||||
{
|
||||
key: 'size',
|
||||
type: 'size',
|
||||
options: [sizeOpt(1024, 1024, '1:1'), sizeOpt(1280, 720, '16:9')],
|
||||
default: '1024x1024',
|
||||
custom_size: { min: 256, max: 2048, step: 64 },
|
||||
placement: 'dock',
|
||||
},
|
||||
{
|
||||
key: 'n',
|
||||
type: 'segmented',
|
||||
options: [1, 2, 3, 4],
|
||||
default: 1,
|
||||
placement: 'dock',
|
||||
},
|
||||
{ key: 'seed', type: 'seed', placement: 'advanced' },
|
||||
{ key: 'response_format', type: 'select', options: ['url'] },
|
||||
],
|
||||
inputs: { reference_images: { label: '参考图', modes: ['edit'], max: 3 } },
|
||||
};
|
||||
|
||||
// 只接受固定尺寸、只能文生图的模型
|
||||
const gptImage = {
|
||||
model: 'gpt-image-1',
|
||||
modes: ['text2image'],
|
||||
params: [
|
||||
{
|
||||
key: 'size',
|
||||
type: 'size',
|
||||
options: [
|
||||
sizeOpt(1024, 1024, '1:1'),
|
||||
sizeOpt(1536, 1024, '3:2'),
|
||||
sizeOpt(1024, 1536, '2:3'),
|
||||
],
|
||||
default: '1024x1024',
|
||||
placement: 'dock',
|
||||
},
|
||||
{
|
||||
key: 'quality',
|
||||
type: 'chips',
|
||||
options: ['auto', 'high', 'low'],
|
||||
default: 'auto',
|
||||
placement: 'dock',
|
||||
},
|
||||
{ key: 'watermark', type: 'switch', default: false, placement: 'advanced' },
|
||||
],
|
||||
inputs: {},
|
||||
};
|
||||
|
||||
const ready = (ref, extra = {}) => ({
|
||||
id: ref,
|
||||
status: 'ready',
|
||||
ref,
|
||||
...extra,
|
||||
});
|
||||
|
||||
test('requestParamsOf 过滤请求不认识的参数并补齐取值显示名', () => {
|
||||
const keys = requestParamsOf(gemini).map((p) => p.key);
|
||||
assert.deepEqual(keys, ['size', 'n', 'seed']);
|
||||
const quality = requestParamsOf(gptImage).find((p) => p.key === 'quality');
|
||||
assert.deepEqual(quality.options[0], { value: 'auto', label: '自动' });
|
||||
});
|
||||
|
||||
test('modeOptionsOf / referenceMaxOf', () => {
|
||||
assert.deepEqual(
|
||||
modeOptionsOf(gemini).map((o) => o.mode),
|
||||
['text2image', 'edit'],
|
||||
);
|
||||
assert.equal(referenceMaxOf(gemini, 'edit'), 3);
|
||||
assert.equal(referenceMaxOf(gemini, 'text2image'), 0);
|
||||
assert.equal(referenceMaxOf(gptImage, 'edit'), 0);
|
||||
});
|
||||
|
||||
test('clampSizeSide 按步长吸附并夹到范围', () => {
|
||||
const r = { min: 256, max: 2048, step: 64 };
|
||||
assert.equal(clampSizeSide(1000, r), 1024);
|
||||
assert.equal(clampSizeSide(10, r), 256);
|
||||
assert.equal(clampSizeSide(5000, r), 2048);
|
||||
assert.equal(clampSizeSide('abc', r), 256);
|
||||
});
|
||||
|
||||
test('normalizeSize:预设 / 自定义 / 最近比例', () => {
|
||||
const g = gemini.params[0];
|
||||
assert.deepEqual(normalizeSize(g, '1280X720'), {
|
||||
value: '1280x720',
|
||||
changed: false,
|
||||
});
|
||||
assert.deepEqual(normalizeSize(g, '1000x3000'), {
|
||||
value: '1024x2048',
|
||||
changed: true,
|
||||
});
|
||||
assert.deepEqual(normalizeSize(g, undefined), {
|
||||
value: '1024x1024',
|
||||
changed: false,
|
||||
});
|
||||
const p = gptImage.params[0];
|
||||
// 不允许自定义:16:9 吸附到最接近的横版 3:2
|
||||
assert.deepEqual(normalizeSize(p, '1280x720'), {
|
||||
value: '1536x1024',
|
||||
changed: true,
|
||||
});
|
||||
assert.deepEqual(normalizeSize(p, 'bogus'), {
|
||||
value: '1024x1024',
|
||||
changed: true,
|
||||
});
|
||||
});
|
||||
|
||||
test('composeParams:会话值 > 记忆 > 默认,非法值吸附并给出调整项', () => {
|
||||
const { values, adjustments } = composeParams(gptImage, {
|
||||
sessionValues: { size: '1280x720', n: 4, quality: 'medium' },
|
||||
memory: { watermark: true },
|
||||
});
|
||||
assert.deepEqual(values, {
|
||||
size: '1536x1024',
|
||||
quality: 'auto',
|
||||
watermark: true,
|
||||
});
|
||||
assert.deepEqual(
|
||||
adjustments.map((a) => a.key),
|
||||
['size', 'quality'],
|
||||
);
|
||||
const g = composeParams(gemini, { sessionValues: { n: 9 } });
|
||||
assert.equal(g.values.n, 4);
|
||||
assert.equal(g.values.size, '1024x1024');
|
||||
});
|
||||
|
||||
test('imageSlotsOf:超出上限 / 当前模式不使用的参考图置灰保留', () => {
|
||||
const refs = [ready('a'), ready('b'), ready('c'), ready('d')];
|
||||
const slots = imageSlotsOf(gemini, 'edit', refs);
|
||||
assert.deepEqual(
|
||||
slots.map((s) => s.active),
|
||||
[true, true, true, false],
|
||||
);
|
||||
const one = imageSlotsOf(gemini, 'edit', [ready('a')]);
|
||||
assert.equal(one.length, 2);
|
||||
assert.equal(one[1].slot, null);
|
||||
const t2i = imageSlotsOf(gptImage, 'text2image', [ready('a')]);
|
||||
assert.deepEqual(
|
||||
t2i.map((s) => s.active),
|
||||
[false],
|
||||
);
|
||||
});
|
||||
|
||||
test('imageBlockReason', () => {
|
||||
assert.equal(
|
||||
imageBlockReason(gemini, 'edit', []).key,
|
||||
'请上传参考图片后才能生成',
|
||||
);
|
||||
assert.equal(
|
||||
imageBlockReason(gemini, 'edit', [{ id: 'x', status: 'uploading' }]).key,
|
||||
'图片上传中,请稍候',
|
||||
);
|
||||
assert.equal(imageBlockReason(gemini, 'edit', [ready('a')]), null);
|
||||
assert.equal(
|
||||
imageBlockReason(gptImage, 'edit', []).key,
|
||||
'当前模型不支持该生成模式,请切换模式',
|
||||
);
|
||||
assert.equal(imageBlockReason(gptImage, 'text2image', [ready('a')]), null);
|
||||
});
|
||||
|
||||
test('pickModeForModel', () => {
|
||||
assert.equal(pickModeForModel(gptImage, 'edit', [ready('a')]), 'text2image');
|
||||
assert.equal(pickModeForModel(gemini, 'edit', []), 'edit');
|
||||
assert.equal(pickModeForModel(gemini, '', [ready('a')]), 'edit');
|
||||
});
|
||||
|
||||
test('buildImageRequest:文生图只带 schema 参数 + source', () => {
|
||||
const payload = buildImageRequest(gptImage, {
|
||||
mode: 'text2image',
|
||||
prompt: 'cat',
|
||||
params: { size: '1024x1536', quality: 'high', n: 3, negative_prompt: 'x' },
|
||||
references: [ready('task://1')],
|
||||
});
|
||||
assert.deepEqual(payload, {
|
||||
model: 'gpt-image-1',
|
||||
prompt: 'cat',
|
||||
source: 'creation_studio',
|
||||
size: '1024x1536',
|
||||
quality: 'high',
|
||||
watermark: false,
|
||||
});
|
||||
});
|
||||
|
||||
test('buildImageRequest:编辑模式按上限提交参考图,单张为字符串', () => {
|
||||
const base = {
|
||||
mode: 'edit',
|
||||
prompt: 'p',
|
||||
params: { size: '700x700', n: 2, seed: '42' },
|
||||
};
|
||||
const single = buildImageRequest(gemini, {
|
||||
...base,
|
||||
references: [ready('cos://playground/1/a.png')],
|
||||
});
|
||||
assert.equal(single.image, 'cos://playground/1/a.png');
|
||||
assert.equal(single.size, '704x704');
|
||||
assert.equal(single.n, 2);
|
||||
assert.equal(single.seed, 42);
|
||||
|
||||
const multi = buildImageRequest(gemini, {
|
||||
...base,
|
||||
params: {},
|
||||
references: [
|
||||
ready('task://t1'),
|
||||
{ id: 'u', status: 'uploading' },
|
||||
ready('data:image/png;base64,AAA'),
|
||||
ready('task://t4'),
|
||||
],
|
||||
});
|
||||
assert.deepEqual(multi.image, ['task://t1', 'data:image/png;base64,AAA']);
|
||||
assert.equal(multi.n, 1);
|
||||
assert.equal(multi.seed, undefined);
|
||||
});
|
||||
|
||||
test('quoteRequestOf', () => {
|
||||
const payload = buildImageRequest(gemini, {
|
||||
mode: 'edit',
|
||||
prompt: 'p',
|
||||
params: { n: 3 },
|
||||
references: [ready('task://t1')],
|
||||
});
|
||||
assert.deepEqual(quoteRequestOf(gemini, payload), {
|
||||
kind: 'image',
|
||||
items: [
|
||||
{
|
||||
model: 'gemini-image',
|
||||
mode: 'edit',
|
||||
params: { size: '1024x1024' },
|
||||
n: 3,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('formFromImageRequest:快照回填,失效参考图计数', () => {
|
||||
const filled = formFromImageRequest(
|
||||
{
|
||||
model: 'gemini-image',
|
||||
mode: 'edit',
|
||||
prompt: 'hello',
|
||||
size: '1280x720',
|
||||
n: 2,
|
||||
seed: 7,
|
||||
negative_prompt: 'blur',
|
||||
watermark: false,
|
||||
reference_images: ['https://cos/a.png', ''],
|
||||
reference_image_refs: ['task://abc#ref0', ''],
|
||||
},
|
||||
{ newId: () => 'id' },
|
||||
);
|
||||
assert.equal(filled.mode, 'edit');
|
||||
assert.deepEqual(filled.params, {
|
||||
size: '1280x720',
|
||||
n: 2,
|
||||
seed: 7,
|
||||
negative_prompt: 'blur',
|
||||
watermark: false,
|
||||
});
|
||||
assert.equal(filled.references.length, 1);
|
||||
assert.equal(filled.references[0].ref, 'task://abc#ref0');
|
||||
assert.equal(filled.references[0].previewUrl, 'https://cos/a.png');
|
||||
assert.equal(filled.missingReferences, 1);
|
||||
});
|
||||
|
||||
test('formFromLegacyTask:旧任务只复用内联原图', () => {
|
||||
const legacy = formFromLegacyTask({
|
||||
model: 'wanx-v1',
|
||||
mode: 'image-to-image',
|
||||
prompt: 'p',
|
||||
width: 1280,
|
||||
height: 720,
|
||||
n: 2,
|
||||
negativePrompt: 'neg',
|
||||
sourceImageUrl: 'https://cos/src.png',
|
||||
taskData: { size: '1280x720' },
|
||||
});
|
||||
assert.equal(legacy.mode, 'edit');
|
||||
assert.deepEqual(legacy.params, {
|
||||
size: '1280x720',
|
||||
n: 2,
|
||||
negative_prompt: 'neg',
|
||||
});
|
||||
assert.equal(legacy.references.length, 0);
|
||||
assert.equal(legacy.missingReferences, 1);
|
||||
});
|
||||
|
||||
const memoryStorage = (initial = {}) => {
|
||||
const data = { ...initial };
|
||||
return {
|
||||
data,
|
||||
getItem: (k) => (k in data ? data[k] : null),
|
||||
setItem: (k, v) => {
|
||||
data[k] = String(v);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
test('草稿:v3 读写往返,v1 迁移模型与参数', () => {
|
||||
const storage = memoryStorage();
|
||||
const form = {
|
||||
mode: 'edit',
|
||||
preferredMode: 'edit',
|
||||
prompt: 'p',
|
||||
modelName: 'gemini-image',
|
||||
params: { size: '1024x1024' },
|
||||
paramMemory: {},
|
||||
references: [
|
||||
ready('task://t1', { previewUrl: 'https://x/a.png', source: 'task' }),
|
||||
{ id: 'u', status: 'uploading' },
|
||||
],
|
||||
};
|
||||
assert.equal(saveDraft(storage, form), true);
|
||||
const back = loadDraft(storage);
|
||||
assert.equal(back.modelName, 'gemini-image');
|
||||
assert.equal(back.references.length, 1);
|
||||
assert.equal(back.references[0].previewUrl, 'https://x/a.png');
|
||||
|
||||
const legacy = memoryStorage({
|
||||
'expCenter.imageGen.lastParams.v1': JSON.stringify({
|
||||
mode: 'text-to-image',
|
||||
modelName: 'gpt-image-1',
|
||||
size: '1024x1536',
|
||||
quality: 'high',
|
||||
n: 1,
|
||||
}),
|
||||
});
|
||||
const migrated = loadDraft(legacy);
|
||||
assert.equal(migrated.modelName, 'gpt-image-1');
|
||||
assert.deepEqual(migrated.params, {
|
||||
size: '1024x1536',
|
||||
quality: 'high',
|
||||
n: 1,
|
||||
});
|
||||
assert.equal(DRAFT_STORAGE_KEY in legacy.data, false);
|
||||
});
|
||||
|
||||
test('草稿:配额不足时不存内联图片', () => {
|
||||
let calls = 0;
|
||||
const storage = {
|
||||
getItem: () => null,
|
||||
setItem: (k, v) => {
|
||||
calls += 1;
|
||||
if (String(v).includes('data:')) throw new Error('quota');
|
||||
},
|
||||
};
|
||||
const ok = saveDraft(storage, {
|
||||
mode: 'edit',
|
||||
references: [ready('data:image/png;base64,AAA')],
|
||||
});
|
||||
assert.equal(ok, true);
|
||||
assert.equal(calls, 2);
|
||||
});
|
||||
Reference in New Issue
Block a user