Compare commits
287 Commits
codex/canv
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f670158409 | |||
| 0d16c27178 | |||
| d79b0b8e49 | |||
| 171e5d6a49 | |||
| c0054cfa7c | |||
| 4fb829a417 | |||
| 6ff8a0c43d | |||
| dfe39a8d20 | |||
| 7fe5ec24a8 | |||
| e64bbfef9c | |||
| a99a63a7bb | |||
| 2b23476d51 | |||
| 4a1e521556 | |||
| 8b2ec847e7 | |||
| a13a6cc180 | |||
| 53b376c739 | |||
| 75997efe2e | |||
| ee1c5bf971 | |||
| 23cf98bf61 | |||
| 18cdbc99c2 | |||
| d96336f568 | |||
| e48b8fd10b | |||
| 3e08783b37 | |||
| a02c203781 | |||
| fc97232b6f | |||
| e3f17cbb5d | |||
| 020637f13d | |||
| 121ca3014b | |||
| 83091c0457 | |||
| 399aa1d761 | |||
| 7094231a55 | |||
| 902ad6fc9c | |||
| bf4dfe0fcb | |||
| 582667dd05 | |||
| 4cfadcea08 | |||
| a7fdff59fc | |||
| 58b4695d17 | |||
| 2123e6208c | |||
| 60c8109d09 | |||
| 9687561b9c | |||
| 82218217ec | |||
| 2d65834dbb | |||
| 0a808a2b83 | |||
| 90492972b3 | |||
| 4d739927f7 | |||
| 903686db36 | |||
| 40fdb41b32 | |||
| f980cfb036 | |||
| 2d0238aca6 | |||
| 5701e95bb4 | |||
| 36ad7900b6 | |||
| c655bbd81e | |||
| dff2e22ffd | |||
| 995820138a | |||
| 4ac5062080 | |||
| ba24efc8e7 | |||
| 1a682343c7 | |||
| f8c97ba7e4 | |||
| 17075e38bd | |||
| ed25603be2 | |||
| 7464108d2f | |||
| 86fd3353f4 | |||
| af54ec2611 | |||
| 9139b33208 | |||
| 2b5455d046 | |||
| 89a9416815 | |||
| 168de1db3c | |||
| 90fe4f2933 | |||
| dc7a1ae0a4 | |||
| a6f79998bd | |||
| d0653c2d51 | |||
| f058012bb8 | |||
| 8aa9ebd061 | |||
| d32258e13b | |||
| 6caf1b5cce | |||
| 78b111130b | |||
| 8fca3b25c5 | |||
| b8799f4804 | |||
| d1f9590d05 | |||
| 74053490cc | |||
| 61b6eba4b4 | |||
| bbecafc826 | |||
| ed3b1b5e8a | |||
| 8f56b5a567 | |||
| fd8d5404c6 | |||
| 09db16f74c | |||
| 70621f4261 | |||
| 5a95e7da49 | |||
| 8cf113b554 | |||
| 5fc3f1bb11 | |||
| 1342bf384c | |||
| 759ee6af58 | |||
| 54eb35e21c | |||
| f157161ad9 | |||
| eb9ee0ce1e | |||
| 2c30c9d7f9 | |||
| 8428886a1d | |||
| 24cf77c9c2 | |||
| 9cbc32bb5d | |||
| 7ddaeb12a0 | |||
| 5f24743bdb | |||
| 03a5613d80 | |||
| 6112892b85 | |||
| 8c50c1ce72 | |||
| b3c4f2763a | |||
| 97d32fd2d3 | |||
| 4dd6885f1c | |||
| 7b776f96c1 | |||
| 17d5f6fec9 | |||
| b3549f25d7 | |||
| 74d9368744 | |||
| e8e8edcefe | |||
| 57e94b797c | |||
| ca6b27624c | |||
| fa3210a367 | |||
| e335821ca8 | |||
| c2e8ff6eac | |||
| d26bec9864 | |||
| 24a3c930c2 | |||
| ebb0d1514b | |||
| 50ffc8d648 | |||
| ff6381d02e | |||
| feaa18ed3b | |||
| 2939ff8a1f | |||
| 5bffeaf94d | |||
| e221027b74 | |||
| 445d756a19 | |||
| 05c3d90708 | |||
| cea4cd01f5 | |||
| ea0351bb2c | |||
| 37406d7d6e | |||
| 0ce9a95a9c | |||
| 8bf8e349ca | |||
| 8039bea86c | |||
| aea18a7fc6 | |||
| e9921cae98 | |||
| 1f5528331d | |||
| 535ea1aee4 | |||
| 8cbc8b1ab8 | |||
| ae9354f747 | |||
| c68bf9a824 | |||
| 21b47751d6 | |||
| c219f5b10b | |||
| 672c1368e0 | |||
| c60585aeca | |||
| 2ee8bd4dea | |||
| 85ff680b65 | |||
| 751cffae05 | |||
| 6e4350fe2b | |||
| 3f52e24076 | |||
| 0b74e71a33 | |||
| 4f8c1a5d5d | |||
| 33662210dd | |||
| 71413c173e | |||
| dbe96db34d | |||
| dfaeb9f09d | |||
| 46abb8259a | |||
| 3b3b5bc78a | |||
| db63f38b9c | |||
| 955ad91ae8 | |||
| 36ac3def8f | |||
| 1d40053c7a | |||
| 07451f5558 | |||
| 13684505a6 | |||
| 65b235e747 | |||
| d1ee6d53d5 | |||
| a484942fb9 | |||
| 084f538e77 | |||
| 5250aed7b7 | |||
| cbec13b285 | |||
| 5fda42495a | |||
| 73eb276e7f | |||
| 8c7ca174bc | |||
| 8fd0dba59f | |||
| ce94241234 | |||
| 198deca47b | |||
| 8908b0d39a | |||
| 34ba1919c4 | |||
| 2fdc4299e0 | |||
| ac2369b657 | |||
| 6642c39198 | |||
| ac7928fe2d | |||
| b06fdbb38b | |||
| 61e2bbe0e4 | |||
| 70f954382a | |||
| 00cef71be7 | |||
| 525e105876 | |||
| 5371ae2617 | |||
| af8189908d | |||
| 6053271352 | |||
| 6b7b24a86e | |||
| c431a574c8 | |||
| 1daf08a964 | |||
| 348cf4cf5d | |||
| a43a51adfc | |||
| 6ba5d9606d | |||
| 02b9575a47 | |||
| 349735601c | |||
| e837788d7a | |||
|
|
dfab95a654 | ||
| c35f5fc5a8 | |||
|
|
e6c6e19e5a | ||
| bb9d02a053 | |||
|
|
d9b06cf0b3 | ||
| 086c58fcc3 | |||
|
|
5a343b6392 | ||
| e6f07ed2ce | |||
|
|
db46983725 | ||
| ae4605d479 | |||
|
|
36efdfae53 | ||
| 10efb4c1e7 | |||
|
|
9da598967a | ||
| 91edc4019e | |||
|
|
7439792d40 | ||
| be642948ec | |||
|
|
9155682238 | ||
|
|
03b8a0deb0 | ||
|
|
439fd56fe1 | ||
|
|
99db635e0a | ||
|
|
14a7668274 | ||
|
|
01485340b5 | ||
|
|
8784bb783e | ||
|
|
82c71b8cd8 | ||
|
|
90c17a61fb | ||
|
|
404d783fa5 | ||
|
|
1705f0ba43 | ||
|
|
b689f8f770 | ||
|
|
7541262f28 | ||
|
|
75e6a28309 | ||
|
|
7925545980 | ||
|
|
59994062c9 | ||
|
|
fb10747828 | ||
|
|
d2d0a1725c | ||
|
|
f38488b546 | ||
|
|
36761e37a4 | ||
|
|
32e8c9cbd6 | ||
|
|
2a2a32c3bd | ||
|
|
d4a6b54078 | ||
|
|
d8aa51185e | ||
|
|
73f9844022 | ||
|
|
1803727d4b | ||
|
|
8848d00107 | ||
|
|
623859f846 | ||
|
|
f55d21968f | ||
|
|
ad25b4e17f | ||
|
|
5dbdc3df46 | ||
|
|
cfb071d0df | ||
|
|
fce14356d9 | ||
|
|
ac2e355a7a | ||
|
|
a8c6a5a026 | ||
|
|
1965ee9cc0 | ||
|
|
0d44ac12c8 | ||
|
|
c7f320074a | ||
|
|
62311d187f | ||
|
|
a4bf68734c | ||
|
|
145f699823 | ||
|
|
8ee12a5b13 | ||
|
|
0a3eef1f4d | ||
|
|
0fd14f18bb | ||
|
|
ac9103906e | ||
|
|
320d52d26f | ||
|
|
2bd2910fae | ||
|
|
54ce53f263 | ||
|
|
f0b6c2765f | ||
|
|
d3b8162faf | ||
|
|
ac54ade6b7 | ||
|
|
1aa61570db | ||
|
|
3504141be5 | ||
|
|
79d10cf1ac | ||
|
|
3a9e392a7f | ||
|
|
07101946ff | ||
|
|
8111ce7708 | ||
|
|
a03ac48f3a | ||
|
|
1dd4487cf0 | ||
|
|
6319a3ceef | ||
|
|
4734d5cb76 | ||
|
|
d33db04ecc | ||
|
|
ab7240b3d4 | ||
|
|
55ad5b21f0 | ||
|
|
562b9a3f1d | ||
|
|
30cfd62669 | ||
|
|
255e9082da | ||
|
|
53e565045a | ||
|
|
a2b65b426c | ||
|
|
d1789f5f93 | ||
|
|
726d66bd19 | ||
|
|
74aa67ce44 |
@ -40,6 +40,11 @@ EMAIL_VERIFICATION_SECRET=
|
|||||||
# Optional scheduled job protection.
|
# Optional scheduled job protection.
|
||||||
CRON_SECRET=
|
CRON_SECRET=
|
||||||
|
|
||||||
|
# Canvas feedback: server-side project credentials for the shared notification center.
|
||||||
|
YYD_NOTIFICATION_CENTER_URL=https://sc.jiansun.vip/notification-center
|
||||||
|
YYD_NOTIFICATION_CENTER_PROJECT_CODE=
|
||||||
|
YYD_NOTIFICATION_CENTER_PROJECT_SECRET=
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
# Database
|
# Database
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|||||||
20
.gitattributes
vendored
Normal file
20
.gitattributes
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Keep source checkouts consistent across Windows and Linux.
|
||||||
|
.gitattributes text eol=lf
|
||||||
|
.gitignore text eol=lf
|
||||||
|
*.ts text eol=lf
|
||||||
|
*.tsx text eol=lf
|
||||||
|
*.js text eol=lf
|
||||||
|
*.jsx text eol=lf
|
||||||
|
*.mjs text eol=lf
|
||||||
|
*.cjs text eol=lf
|
||||||
|
*.json text eol=lf
|
||||||
|
*.jsonc text eol=lf
|
||||||
|
*.yaml text eol=lf
|
||||||
|
*.yml text eol=lf
|
||||||
|
*.css text eol=lf
|
||||||
|
*.html text eol=lf
|
||||||
|
*.md text eol=lf
|
||||||
|
*.sh text eol=lf
|
||||||
|
*.py text eol=lf
|
||||||
|
*.bat text eol=crlf
|
||||||
|
*.cmd text eol=crlf
|
||||||
10
.gitignore
vendored
10
.gitignore
vendored
@ -13,6 +13,7 @@ coverage
|
|||||||
.next/
|
.next/
|
||||||
.next-build/
|
.next-build/
|
||||||
.next-previous/
|
.next-previous/
|
||||||
|
/.next-*/
|
||||||
out/
|
out/
|
||||||
build
|
build
|
||||||
|
|
||||||
@ -67,6 +68,11 @@ migration-backup-complete/
|
|||||||
migration-*.log
|
migration-*.log
|
||||||
migration-*.txt
|
migration-*.txt
|
||||||
|
|
||||||
|
# Local release backups, model credentials and migration scratch data.
|
||||||
|
/.model-config-backups/
|
||||||
|
/backups/
|
||||||
|
/tmp/
|
||||||
|
|
||||||
# 本地抓包/竞品参考产物,不能提交
|
# 本地抓包/竞品参考产物,不能提交
|
||||||
artifacts/liblib-*/
|
artifacts/liblib-*/
|
||||||
analysis/qiaodou-extract/
|
analysis/qiaodou-extract/
|
||||||
@ -128,3 +134,7 @@ services/video-upscale/vendor/__pycache__/
|
|||||||
|
|
||||||
/services/audio-processing/dist/
|
/services/audio-processing/dist/
|
||||||
/services/audio-processing/__pycache__/
|
/services/audio-processing/__pycache__/
|
||||||
|
|
||||||
|
# Python release-tool and test bytecode.
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|||||||
@ -2377,8 +2377,10 @@ export function LocalAiImagePipelineSections({
|
|||||||
items={[
|
items={[
|
||||||
{
|
{
|
||||||
label:
|
label:
|
||||||
extendConfig.provider === "vip98k"
|
extendConfig.provider === "fal"
|
||||||
? "VIP98K 服务商"
|
? "fal · FLUX 2 Pro 扩图"
|
||||||
|
: extendConfig.provider === "vip98k"
|
||||||
|
? `98K · ${extendConfig.modelId || "image-expansion"}`
|
||||||
: "阿里云服务商",
|
: "阿里云服务商",
|
||||||
tone: "enabled",
|
tone: "enabled",
|
||||||
},
|
},
|
||||||
@ -2391,9 +2393,13 @@ export function LocalAiImagePipelineSections({
|
|||||||
}
|
}
|
||||||
title="图像拓展API配置"
|
title="图像拓展API配置"
|
||||||
>
|
>
|
||||||
{extendConfig.provider === "vip98k" ? (
|
{extendConfig.provider === "fal" ? (
|
||||||
<div className="rounded-lg border bg-muted/40 p-4 text-sm">
|
<div className="rounded-lg border bg-muted/40 p-4 text-sm">
|
||||||
使用 VIP98K 的 image-expansion 模型。API Key 从“AI 模型设置”中的
|
使用 FLUX 2 Pro Outpaint 扩展图片。密钥从已启用的 fal 官方服务商读取。
|
||||||
|
</div>
|
||||||
|
) : extendConfig.provider === "vip98k" ? (
|
||||||
|
<div className="rounded-lg border bg-muted/40 p-4 text-sm">
|
||||||
|
使用 98K 的 {extendConfig.modelId || "image-expansion"} 模型。API Key 从“AI 模型设置”中的
|
||||||
VIP98K 服务商读取,仅在这里维护扩展比例。
|
VIP98K 服务商读取,仅在这里维护扩展比例。
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
@ -274,7 +274,9 @@ export type EnhanceConfig = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type ExtendConfig = {
|
export type ExtendConfig = {
|
||||||
provider: "vip98k" | "dashscope";
|
provider: "vip98k" | "dashscope" | "fal";
|
||||||
|
modelId: string;
|
||||||
|
routeOptionId: string;
|
||||||
xScale: string;
|
xScale: string;
|
||||||
yScale: string;
|
yScale: string;
|
||||||
};
|
};
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -23,6 +23,21 @@ import { AntigravityBackground } from "@/components/antigravity-background";
|
|||||||
import { createClientLogger } from "@/lib/observability/client-logger";
|
import { createClientLogger } from "@/lib/observability/client-logger";
|
||||||
|
|
||||||
const logger = createClientLogger("auth-login");
|
const logger = createClientLogger("auth-login");
|
||||||
|
|
||||||
|
function getSafeLoginCallbackUrl() {
|
||||||
|
const { origin, search } = window.location;
|
||||||
|
const callbackUrl = new URLSearchParams(search).get("callbackUrl");
|
||||||
|
if (!callbackUrl?.startsWith("/")) return "/";
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { origin: targetOrigin, pathname, search, hash } = new URL(callbackUrl, origin);
|
||||||
|
if (targetOrigin !== origin || pathname.startsWith("//")) return "/";
|
||||||
|
return pathname + search + hash;
|
||||||
|
} catch {
|
||||||
|
return "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
const [email, setEmail] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
const [isSuccessful, setIsSuccessful] = useState(false);
|
const [isSuccessful, setIsSuccessful] = useState(false);
|
||||||
@ -70,7 +85,7 @@ export default function Page() {
|
|||||||
.then((data) => {
|
.then((data) => {
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
if (data?.valid === true && data?.userType !== "guest") {
|
if (data?.valid === true && data?.userType !== "guest") {
|
||||||
window.location.replace("/");
|
window.location.replace(getSafeLoginCallbackUrl());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setIsCheckingExistingSession(false);
|
setIsCheckingExistingSession(false);
|
||||||
@ -90,6 +105,7 @@ export default function Page() {
|
|||||||
const completeDingTalkLogin = useCallback(async (token: string) => {
|
const completeDingTalkLogin = useCallback(async (token: string) => {
|
||||||
logger.debug("[DingTalk] Completing login with token", { hasToken: Boolean(token) });
|
logger.debug("[DingTalk] Completing login with token", { hasToken: Boolean(token) });
|
||||||
setIsHandlingToken(true);
|
setIsHandlingToken(true);
|
||||||
|
const callbackUrl = getSafeLoginCallbackUrl();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 获取 CSRF token
|
// 获取 CSRF token
|
||||||
@ -102,11 +118,12 @@ export default function Page() {
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
"X-Auth-Return-Redirect": "1",
|
||||||
},
|
},
|
||||||
body: new URLSearchParams({
|
body: new URLSearchParams({
|
||||||
csrfToken,
|
csrfToken,
|
||||||
token,
|
token,
|
||||||
callbackUrl: "/",
|
callbackUrl,
|
||||||
}),
|
}),
|
||||||
credentials: "include", // 重要:确保 cookie 被发送和接收
|
credentials: "include", // 重要:确保 cookie 被发送和接收
|
||||||
});
|
});
|
||||||
@ -134,12 +151,12 @@ export default function Page() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (session && session.user) {
|
if (session && session.user) {
|
||||||
logger.debug("[DingTalk] Session validated, redirecting to home...");
|
logger.debug("[DingTalk] Session validated, redirecting...");
|
||||||
setIsSuccessful(true);
|
setIsSuccessful(true);
|
||||||
// 使用 replace 而不是 href,避免在历史记录中留下登录页
|
// 使用 replace 而不是 href,避免在历史记录中留下登录页
|
||||||
// 增加短暂延迟确保 cookie 完全同步
|
// 增加短暂延迟确保 cookie 完全同步
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.replace("/");
|
window.location.replace(callbackUrl);
|
||||||
}, 200);
|
}, 200);
|
||||||
} else {
|
} else {
|
||||||
logger.error("[DingTalk] Session not created after login");
|
logger.error("[DingTalk] Session not created after login");
|
||||||
@ -174,7 +191,7 @@ export default function Page() {
|
|||||||
// 使用 replace 而不是 href,避免在历史记录中留下登录页
|
// 使用 replace 而不是 href,避免在历史记录中留下登录页
|
||||||
// 增加短暂延迟确保 cookie 完全同步
|
// 增加短暂延迟确保 cookie 完全同步
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.replace(result.url || "/");
|
window.location.replace(callbackUrl);
|
||||||
}, 200);
|
}, 200);
|
||||||
} else {
|
} else {
|
||||||
logger.error("[DingTalk] Session not created");
|
logger.error("[DingTalk] Session not created");
|
||||||
@ -298,7 +315,7 @@ export default function Page() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await signIn("google", { callbackUrl: "/" });
|
await signIn("google", { redirectTo: getSafeLoginCallbackUrl() });
|
||||||
};
|
};
|
||||||
|
|
||||||
// 检查 URL 中是否有钉钉 Token(页面加载时自动登录)
|
// 检查 URL 中是否有钉钉 Token(页面加载时自动登录)
|
||||||
@ -365,13 +382,14 @@ export default function Page() {
|
|||||||
description: "提交的信息校验失败",
|
description: "提交的信息校验失败",
|
||||||
});
|
});
|
||||||
} else if (state.status === "success") {
|
} else if (state.status === "success") {
|
||||||
toast({ type: "success", description: "登录成功,正在进入首页" });
|
const callbackUrl = getSafeLoginCallbackUrl();
|
||||||
|
toast({ type: "success", description: "登录成功,正在跳转" });
|
||||||
setIsSuccessful(true);
|
setIsSuccessful(true);
|
||||||
loginRedirectTimerRef.current = window.setTimeout(() => {
|
loginRedirectTimerRef.current = window.setTimeout(() => {
|
||||||
window.location.replace("/");
|
window.location.replace(callbackUrl);
|
||||||
}, 120);
|
}, 120);
|
||||||
loginRedirectFallbackTimerRef.current = window.setTimeout(() => {
|
loginRedirectFallbackTimerRef.current = window.setTimeout(() => {
|
||||||
window.location.assign("/");
|
window.location.assign(callbackUrl);
|
||||||
}, 1600);
|
}, 1600);
|
||||||
}
|
}
|
||||||
}, [state.status]);
|
}, [state.status]);
|
||||||
@ -393,23 +411,6 @@ export default function Page() {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const getSafeLoginCallbackUrl = () => {
|
|
||||||
if (typeof window === "undefined") return "/";
|
|
||||||
|
|
||||||
const callbackUrl = new URLSearchParams(window.location.search).get(
|
|
||||||
"callbackUrl",
|
|
||||||
);
|
|
||||||
if (
|
|
||||||
callbackUrl &&
|
|
||||||
callbackUrl.startsWith("/") &&
|
|
||||||
!callbackUrl.startsWith("//")
|
|
||||||
) {
|
|
||||||
return callbackUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return "/";
|
|
||||||
};
|
|
||||||
|
|
||||||
const buildDingTalkLoginUrl = (orgName?: string) => {
|
const buildDingTalkLoginUrl = (orgName?: string) => {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (orgName) params.set("org", orgName);
|
if (orgName) params.set("org", orgName);
|
||||||
|
|||||||
@ -4,7 +4,8 @@ import { fetchImageBufferFromUrl } from "@/lib/ai/rmbg/service";
|
|||||||
import { getLanguageModelById } from "@/lib/ai/dynamic-provider";
|
import { getLanguageModelById } from "@/lib/ai/dynamic-provider";
|
||||||
import {
|
import {
|
||||||
DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY,
|
DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY,
|
||||||
normalizeDetailReplacementImageModelConfig,
|
findConfiguredDetailReplacementImageModel,
|
||||||
|
isDetailReplacementImageModel,
|
||||||
} from "@/lib/ai/detail-replacement-image-model-config";
|
} from "@/lib/ai/detail-replacement-image-model-config";
|
||||||
import {
|
import {
|
||||||
compositeReplacementWithReferenceLocks,
|
compositeReplacementWithReferenceLocks,
|
||||||
@ -29,7 +30,6 @@ import {
|
|||||||
} from "@/lib/db/queries";
|
} from "@/lib/db/queries";
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
import { bufferToArrayBuffer } from "./persistence";
|
import { bufferToArrayBuffer } from "./persistence";
|
||||||
import { isVideoProviderType } from "./video-models";
|
|
||||||
import type { AgentPresetMessageImagePart } from "./agent-scene-workflow";
|
import type { AgentPresetMessageImagePart } from "./agent-scene-workflow";
|
||||||
import type { PostRequestBody } from "../schema";
|
import type { PostRequestBody } from "../schema";
|
||||||
|
|
||||||
@ -623,7 +623,7 @@ async function resolveReplacementProductIdentityProfile(params: {
|
|||||||
system:
|
system:
|
||||||
"你是高精度商品结构分析器。多张图片属于同一商品;请跨视角还原唯一结构身份,并明确每个视角最清楚的构件。",
|
"你是高精度商品结构分析器。多张图片属于同一商品;请跨视角还原唯一结构身份,并明确每个视角最清楚的构件。",
|
||||||
temperature: params.planningRuntime.kind === "local_moonshot" ? 1 : 0.1,
|
temperature: params.planningRuntime.kind === "local_moonshot" ? 1 : 0.1,
|
||||||
maxTokens: 1800,
|
maxOutputTokens: 1800,
|
||||||
abortSignal: AbortSignal.timeout(
|
abortSignal: AbortSignal.timeout(
|
||||||
params.planningRuntime.kind === "local_moonshot" ? 90_000 : 60_000,
|
params.planningRuntime.kind === "local_moonshot" ? 90_000 : 60_000,
|
||||||
),
|
),
|
||||||
@ -2161,22 +2161,10 @@ export async function resolveDirectReplacementModelRef(params: {
|
|||||||
models?: Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>;
|
models?: Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>;
|
||||||
}) {
|
}) {
|
||||||
const models = params.models || (await getEnabledAiModelsWithProvider(false));
|
const models = params.models || (await getEnabledAiModelsWithProvider(false));
|
||||||
const imageModels = models.filter(
|
|
||||||
({ model, provider }) =>
|
|
||||||
model.supportsImageGen &&
|
|
||||||
!model.isSubModel &&
|
|
||||||
!isVideoProviderType(provider?.type, model),
|
|
||||||
);
|
|
||||||
const configuredModelSetting = await getSystemSetting({
|
const configuredModelSetting = await getSystemSetting({
|
||||||
key: DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY,
|
key: DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY,
|
||||||
}).catch(() => null);
|
}).catch(() => null);
|
||||||
const configuredModelId = normalizeDetailReplacementImageModelConfig(
|
const configuredModel = findConfiguredDetailReplacementImageModel(models, configuredModelSetting?.value);
|
||||||
configuredModelSetting?.value,
|
|
||||||
).modelId;
|
|
||||||
const configuredModel = imageModels.find(
|
|
||||||
({ model }) =>
|
|
||||||
model.id === configuredModelId || model.modelId === configuredModelId,
|
|
||||||
);
|
|
||||||
if (configuredModel) {
|
if (configuredModel) {
|
||||||
return configuredModel.model.id;
|
return configuredModel.model.id;
|
||||||
}
|
}
|
||||||
@ -2190,6 +2178,7 @@ export async function resolveDirectReplacementModelRef(params: {
|
|||||||
return routedModel;
|
return routedModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const imageModels = models.filter(isDetailReplacementImageModel);
|
||||||
const preferred =
|
const preferred =
|
||||||
imageModels.find(({ model }) => model.isDefault) ||
|
imageModels.find(({ model }) => model.isDefault) ||
|
||||||
imageModels.find(({ provider }) => String(provider?.type || "").includes("image")) ||
|
imageModels.find(({ provider }) => String(provider?.type || "").includes("image")) ||
|
||||||
@ -2387,7 +2376,7 @@ export async function analyzeReplacementReferenceImage(params: {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
temperature: 0,
|
temperature: 0,
|
||||||
maxTokens: 160,
|
maxOutputTokens: 160,
|
||||||
abortSignal: AbortSignal.timeout(45_000),
|
abortSignal: AbortSignal.timeout(45_000),
|
||||||
} as any);
|
} as any);
|
||||||
|
|
||||||
@ -2690,7 +2679,7 @@ export async function analyzeReplacementExecutionPlan(params: {
|
|||||||
system:
|
system:
|
||||||
"你是高精度商品详情图场景规划器。你只分析当前参考图本身,负责理解这一屏的场景、机位、主体位置和人物需求,不要臆测或依赖其他输入图片。",
|
"你是高精度商品详情图场景规划器。你只分析当前参考图本身,负责理解这一屏的场景、机位、主体位置和人物需求,不要臆测或依赖其他输入图片。",
|
||||||
temperature: params.planningRuntime.kind === "local_moonshot" ? 1 : 0.1,
|
temperature: params.planningRuntime.kind === "local_moonshot" ? 1 : 0.1,
|
||||||
maxTokens: 1600,
|
maxOutputTokens: 1600,
|
||||||
abortSignal: AbortSignal.timeout(replacementScenePlanningTimeoutMs),
|
abortSignal: AbortSignal.timeout(replacementScenePlanningTimeoutMs),
|
||||||
} as any);
|
} as any);
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import { modelLacksReferenceImageInputSupport } from "@/lib/ai/reference-image-c
|
|||||||
import type { ChatMessage } from "@/lib/types";
|
import type { ChatMessage } from "@/lib/types";
|
||||||
import { convertMessagesWithImages } from "./image-media";
|
import { convertMessagesWithImages } from "./image-media";
|
||||||
import { isProviderBalanceError } from "@/lib/request-error";
|
import { isProviderBalanceError } from "@/lib/request-error";
|
||||||
import { isImageFallbackModelBalanceCoolingDown } from "@/lib/ai/runtime/image-fallback-balance-cooldown";
|
import { isImageModelBalanceBlocked } from "@/lib/ai/runtime/image-balance";
|
||||||
|
|
||||||
const chatApiLogger = createLogger("chat.imageFailover");
|
const chatApiLogger = createLogger("chat.imageFailover");
|
||||||
|
|
||||||
@ -264,6 +264,29 @@ export function chatMessagesHaveImageInput(messages: ChatMessage[]) {
|
|||||||
return countChatMessageImageInputs(messages) > 0;
|
return countChatMessageImageInputs(messages) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function canUseImageFallback(
|
||||||
|
candidate: { model: any; provider: any },
|
||||||
|
imageParams: { imageSize?: unknown; aspectRatio?: unknown; imageCount?: number },
|
||||||
|
messages: ChatMessage[],
|
||||||
|
balanceBlockedProviders: ReadonlySet<string>,
|
||||||
|
) {
|
||||||
|
if (balanceBlockedProviders.has(candidate.provider?.id)) return false;
|
||||||
|
const config = candidate.model?.imageGenConfig;
|
||||||
|
const size = String(imageParams.imageSize || "").toUpperCase();
|
||||||
|
const ratio = String(imageParams.aspectRatio || "");
|
||||||
|
if (size && Array.isArray(config?.supportedImageSizes) &&
|
||||||
|
!config.supportedImageSizes.includes(size)) return false;
|
||||||
|
if (ratio && ratio !== "auto" && Array.isArray(config?.supportedAspectRatios) &&
|
||||||
|
!config.supportedAspectRatios.includes(ratio)) return false;
|
||||||
|
if (imageParams.imageCount && typeof config?.maxImages === "number" &&
|
||||||
|
imageParams.imageCount > config.maxImages) return false;
|
||||||
|
const referenceCount = countChatMessageImageInputs(messages);
|
||||||
|
if (referenceCount && modelLacksReferenceImageInputSupport(candidate.model, candidate.provider)) return false;
|
||||||
|
if (referenceCount && typeof config?.maxReferenceImages === "number" &&
|
||||||
|
referenceCount > config.maxReferenceImages) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
export function getImageFailoverConvertProvider(providerType?: string | null) {
|
export function getImageFailoverConvertProvider(providerType?: string | null) {
|
||||||
switch (providerType) {
|
switch (providerType) {
|
||||||
case "bfl-image":
|
case "bfl-image":
|
||||||
@ -326,7 +349,7 @@ export function buildImageFallbackCandidates(
|
|||||||
if (!sameProvider && !IMAGE_FAILOVER_PROVIDER_ALLOWLIST.has(providerType)) {
|
if (!sameProvider && !IMAGE_FAILOVER_PROVIDER_ALLOWLIST.has(providerType)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isImageFallbackModelCoolingDown(item.model.id) || isImageFallbackModelBalanceCoolingDown(item.model)) {
|
if (isImageFallbackModelCoolingDown(item.model.id) || isImageModelBalanceBlocked(item.model)) {
|
||||||
const cooldown = imageFailoverModelCooldown.get(item.model.id);
|
const cooldown = imageFailoverModelCooldown.get(item.model.id);
|
||||||
chatApiLogger.info(`[Chat API] ⏭ Skip configured image fallback on cooldown: ${item.model.displayName} (${providerType}) - ${cooldown?.reason || "provider balance cooldown"}`);
|
chatApiLogger.info(`[Chat API] ⏭ Skip configured image fallback on cooldown: ${item.model.displayName} (${providerType}) - ${cooldown?.reason || "provider balance cooldown"}`);
|
||||||
return false;
|
return false;
|
||||||
@ -488,7 +511,7 @@ export function getImageCrossProviderCandidates(
|
|||||||
}
|
}
|
||||||
if (m.model.id === options.currentModelId) return false;
|
if (m.model.id === options.currentModelId) return false;
|
||||||
if (options.currentProviderId && m.provider.id === options.currentProviderId) return false;
|
if (options.currentProviderId && m.provider.id === options.currentProviderId) return false;
|
||||||
if (isImageFallbackModelCoolingDown(m.model.id) || isImageFallbackModelBalanceCoolingDown(m.model)) {
|
if (isImageFallbackModelCoolingDown(m.model.id) || isImageModelBalanceBlocked(m.model)) {
|
||||||
const cooldown = imageFailoverModelCooldown.get(m.model.id);
|
const cooldown = imageFailoverModelCooldown.get(m.model.id);
|
||||||
chatApiLogger.info(`[Chat API] ⏭ Skip image failover model on cooldown: ${m.model.displayName} (${m.provider.type}) - ${cooldown?.reason || "provider balance cooldown"}`);
|
chatApiLogger.info(`[Chat API] ⏭ Skip image failover model on cooldown: ${m.model.displayName} (${m.provider.type}) - ${cooldown?.reason || "provider balance cooldown"}`);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
56
app/(chat)/api/chat/_lib/image-generation-failure.ts
Normal file
56
app/(chat)/api/chat/_lib/image-generation-failure.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import { saveMessages } from "@/lib/db/queries";
|
||||||
|
import type { ImageGenerationStatusStreamPayload } from "@/lib/canvas/image-generation-status";
|
||||||
|
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
||||||
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
|
import { safeWriteStreamChunk, type StreamWriter, writeAssistantTextChunk, writeStructuredDataChunk } from "./stream";
|
||||||
|
|
||||||
|
const logger = createLogger("chat.imageGenerationFailure");
|
||||||
|
|
||||||
|
export async function reportImageGenerationFailure(params: {
|
||||||
|
writer: StreamWriter;
|
||||||
|
chatId: string;
|
||||||
|
userMessageId: string;
|
||||||
|
messageId: string;
|
||||||
|
error: unknown;
|
||||||
|
label: string;
|
||||||
|
}) {
|
||||||
|
const failure = getRequestFailureDetails(params.error, "图片生成失败");
|
||||||
|
logger.warn("Image request failed", {
|
||||||
|
requestId: params.messageId,
|
||||||
|
userMessageId: params.userMessageId,
|
||||||
|
providerRequestId: (params.error as { providerRequestId?: string } | null)?.providerRequestId,
|
||||||
|
code: failure.code,
|
||||||
|
});
|
||||||
|
const status: ImageGenerationStatusStreamPayload = {
|
||||||
|
phase: "failed",
|
||||||
|
userMessageId: params.userMessageId,
|
||||||
|
requestId: params.messageId,
|
||||||
|
failure,
|
||||||
|
};
|
||||||
|
const text = formatRequestFailure(failure, params.messageId);
|
||||||
|
const parts = [
|
||||||
|
{ type: "text", text },
|
||||||
|
{ type: "data-imageGenerationStatus", data: status },
|
||||||
|
];
|
||||||
|
writeStructuredDataChunk(params.writer, "data-imageGenerationStatus", status, `${params.label}:status`);
|
||||||
|
safeWriteStreamChunk(params.writer, {
|
||||||
|
type: "data-appendMessage",
|
||||||
|
data: JSON.stringify({ id: params.messageId, role: "assistant", parts }),
|
||||||
|
}, params.label);
|
||||||
|
writeAssistantTextChunk(params.writer, "<!-- IMAGE_GENERATION_END -->", `${params.label}:end`);
|
||||||
|
// History carries the same terminal state when the browser loses the stream.
|
||||||
|
await saveMessages({ messages: [{
|
||||||
|
id: params.messageId,
|
||||||
|
chatId: params.chatId,
|
||||||
|
role: "assistant",
|
||||||
|
parts,
|
||||||
|
attachments: [],
|
||||||
|
createdAt: new Date(),
|
||||||
|
videoStorageStatus: null,
|
||||||
|
temporaryVideoUrl: null,
|
||||||
|
permanentVideoUrl: null,
|
||||||
|
archivedAt: null,
|
||||||
|
videoMetadata: null,
|
||||||
|
}] });
|
||||||
|
return text;
|
||||||
|
}
|
||||||
@ -208,6 +208,7 @@ export function writeImageGenerationStatusChunk(
|
|||||||
writer: StreamWriter,
|
writer: StreamWriter,
|
||||||
params: {
|
params: {
|
||||||
phase: "queued" | "processing" | "finalizing" | "failed" | "completed";
|
phase: "queued" | "processing" | "finalizing" | "failed" | "completed";
|
||||||
|
userMessageId?: string;
|
||||||
providerType?: string | null;
|
providerType?: string | null;
|
||||||
modelName?: string | null;
|
modelName?: string | null;
|
||||||
imageCount?: number | null;
|
imageCount?: number | null;
|
||||||
@ -223,6 +224,7 @@ export function writeImageGenerationStatusChunk(
|
|||||||
"data-imageGenerationStatus",
|
"data-imageGenerationStatus",
|
||||||
{
|
{
|
||||||
phase: params.phase,
|
phase: params.phase,
|
||||||
|
userMessageId: params.userMessageId,
|
||||||
providerType: normalizeImageLoadingProviderType(params.providerType),
|
providerType: normalizeImageLoadingProviderType(params.providerType),
|
||||||
modelName: String(params.modelName || "").trim() || undefined,
|
modelName: String(params.modelName || "").trim() || undefined,
|
||||||
imageCount:
|
imageCount:
|
||||||
|
|||||||
@ -1,8 +1,11 @@
|
|||||||
|
import { isExplicitDestructiveIntent } from "@/lib/ai/canvas-agent-runtime/capability-access";
|
||||||
|
import { inferVideoTaskRequirements } from "@/lib/ai/canvas-agent-runtime/video-protocol";
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
|
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
||||||
import { geolocation } from "@vercel/functions";
|
import { geolocation } from "@vercel/functions";
|
||||||
import {
|
import {
|
||||||
convertToModelMessages,
|
convertToModelMessages,
|
||||||
createUIMessageStream,
|
createUIMessageStream as createAiUIMessageStream,
|
||||||
generateText,
|
generateText,
|
||||||
smoothStream,
|
smoothStream,
|
||||||
stepCountIs,
|
stepCountIs,
|
||||||
@ -194,6 +197,7 @@ import {
|
|||||||
} from "@/lib/ai/runtime/agent-media-markdown";
|
} from "@/lib/ai/runtime/agent-media-markdown";
|
||||||
import { extractImageUrlsFromText } from "@/lib/image-url";
|
import { extractImageUrlsFromText } from "@/lib/image-url";
|
||||||
import { saveGeneratedImages } from "@/lib/ai/runtime/generated-image-persistence";
|
import { saveGeneratedImages } from "@/lib/ai/runtime/generated-image-persistence";
|
||||||
|
import { withAuthenticationFailover } from "@/lib/ai/runtime/authentication-failover";
|
||||||
import {
|
import {
|
||||||
recordAiModelHealthFailure,
|
recordAiModelHealthFailure,
|
||||||
recordAiModelHealthSuccess,
|
recordAiModelHealthSuccess,
|
||||||
@ -333,6 +337,8 @@ import {
|
|||||||
} from "./_lib/image-media";
|
} from "./_lib/image-media";
|
||||||
import {
|
import {
|
||||||
buildImageFallbackCandidates,
|
buildImageFallbackCandidates,
|
||||||
|
canUseImageFallback,
|
||||||
|
countChatMessageImageInputs,
|
||||||
generateImageWithFailoverCandidate,
|
generateImageWithFailoverCandidate,
|
||||||
getImageCrossProviderCandidates,
|
getImageCrossProviderCandidates,
|
||||||
IMAGE_FAILOVER_PROVIDER_ALLOWLIST,
|
IMAGE_FAILOVER_PROVIDER_ALLOWLIST,
|
||||||
@ -341,6 +347,8 @@ import {
|
|||||||
setImageFallbackModelCooldown,
|
setImageFallbackModelCooldown,
|
||||||
shouldCooldownImageFallbackModel,
|
shouldCooldownImageFallbackModel,
|
||||||
} from "./_lib/image-failover";
|
} from "./_lib/image-failover";
|
||||||
|
import { isImageModelBalanceBlocked, resolveConfiguredImageBalanceFallback } from "@/lib/ai/runtime/image-balance";
|
||||||
|
import { isProviderBalanceError } from "@/lib/request-error";
|
||||||
import {
|
import {
|
||||||
buildAgentSceneWorkflowPrompt,
|
buildAgentSceneWorkflowPrompt,
|
||||||
buildAgentSlotPrompt,
|
buildAgentSlotPrompt,
|
||||||
@ -419,7 +427,11 @@ import {
|
|||||||
type VideoModeType,
|
type VideoModeType,
|
||||||
} from "./_lib/video-models";
|
} from "./_lib/video-models";
|
||||||
|
|
||||||
|
import { reportImageGenerationFailure } from "./_lib/image-generation-failure";
|
||||||
|
|
||||||
const chatApiLogger = createLogger("chat.route");
|
const chatApiLogger = createLogger("chat.route");
|
||||||
|
const createUIMessageStream: typeof createAiUIMessageStream = (options) =>
|
||||||
|
createAiUIMessageStream({ ...options, execute: (writer) => trackRuntimeWork(() => options.execute(writer)) });
|
||||||
|
|
||||||
const INTERNAL_IMAGE_GENERATION_FAILURE_PATTERN =
|
const INTERNAL_IMAGE_GENERATION_FAILURE_PATTERN =
|
||||||
/生成图片失败|图片生成失败|未找到图片数据|未解析到图片|模型未返回生成图片|图片数据不可用|未返回可用图片/i;
|
/生成图片失败|图片生成失败|未找到图片数据|未解析到图片|模型未返回生成图片|图片数据不可用|未返回可用图片/i;
|
||||||
@ -437,16 +449,6 @@ function assertGeneratedImageMarkdownHasImage(markdown: string, modelLabel: stri
|
|||||||
throw new Error(`${modelLabel} 未返回可用图片:${reason.slice(0, 160)}`);
|
throw new Error(`${modelLabel} 未返回可用图片:${reason.slice(0, 160)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPublicImageGenerationErrorText(error: unknown) {
|
|
||||||
const message = error instanceof Error ? error.message : String(error || "");
|
|
||||||
const publicMessage = getUserFacingImageGenerationErrorMessage(error);
|
|
||||||
if (!message || (publicMessage === message && INTERNAL_IMAGE_GENERATION_FAILURE_PATTERN.test(message))) {
|
|
||||||
return "图片生成失败,请稍后重试";
|
|
||||||
}
|
|
||||||
return publicMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const AGENT_REPLACEMENT_PARALLEL_WORKER_COUNT = Math.max(
|
const AGENT_REPLACEMENT_PARALLEL_WORKER_COUNT = Math.max(
|
||||||
1,
|
1,
|
||||||
Math.min(8, Number(process.env.AGENT_REPLACEMENT_PARALLEL_WORKER_COUNT || 6) || 6),
|
Math.min(8, Number(process.env.AGENT_REPLACEMENT_PARALLEL_WORKER_COUNT || 6) || 6),
|
||||||
@ -904,6 +906,15 @@ export async function POST(request: Request) {
|
|||||||
const json = await request.json();
|
const json = await request.json();
|
||||||
chatApiLogger.info("[Chat API] Received request body:", JSON.stringify(json, null, 2));
|
chatApiLogger.info("[Chat API] Received request body:", JSON.stringify(json, null, 2));
|
||||||
requestBody = postRequestBodySchema.parse(json);
|
requestBody = postRequestBodySchema.parse(json);
|
||||||
|
// Agent/Auto Agent has one execution owner: the isolated native ZCode entry.
|
||||||
|
// This also upgrades existing browser tabs without routing Claw here.
|
||||||
|
if (requestBody.composerMode === "agent" && requestBody.contextPayload?.source === "canvas" && requestBody.contextPayload.projectId) {
|
||||||
|
const { POST: nativeAgentPost } = await import("@/app/api/canvas/agent-chat/route");
|
||||||
|
return nativeAgentPost(new Request(new URL("/api/canvas/agent-chat", request.url), {
|
||||||
|
method: "POST", headers: request.headers, body: JSON.stringify(json),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
chatApiLogger.error("[Chat API] Schema validation failed:", error);
|
chatApiLogger.error("[Chat API] Schema validation failed:", error);
|
||||||
return new ChatSDKError("bad_request:api").toResponse();
|
return new ChatSDKError("bad_request:api").toResponse();
|
||||||
@ -1198,6 +1209,14 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shouldRunAgentConversation && preferredAgentModelId && !shouldKeepSelectedModel) {
|
||||||
|
const preferred = await getAiModelById({ id: preferredAgentModelId }).catch(() => null);
|
||||||
|
if (!preferred?.isEnabled || preferred.healthStatus === "auto_disabled") {
|
||||||
|
preferredAgentModelId = undefined;
|
||||||
|
configuredAgentConversationModelId = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldRunAgentConversation && !preferredAgentModelId) {
|
if (shouldRunAgentConversation && !preferredAgentModelId) {
|
||||||
const configuredAgentModel = await getAgentAiModelWithProvider().catch((agentModelError) => {
|
const configuredAgentModel = await getAgentAiModelWithProvider().catch((agentModelError) => {
|
||||||
chatApiLogger.warn("[Chat API] Failed to load configured agent model:", agentModelError);
|
chatApiLogger.warn("[Chat API] Failed to load configured agent model:", agentModelError);
|
||||||
@ -1318,6 +1337,7 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const allowAgentVideoGeneration =
|
const allowAgentVideoGeneration =
|
||||||
|
(contextPayload?.source === "canvas" && inferVideoTaskRequirements(requestTextContent).expectsVideo) ||
|
||||||
Boolean(requestBody.videoParams) ||
|
Boolean(requestBody.videoParams) ||
|
||||||
isVideoProviderType(requestedModelProviderType, requestedModelConfig || undefined);
|
isVideoProviderType(requestedModelProviderType, requestedModelConfig || undefined);
|
||||||
let smartAutoWorkflowDecision:
|
let smartAutoWorkflowDecision:
|
||||||
@ -5661,6 +5681,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
modelName: resolvedModelRef,
|
modelName: resolvedModelRef,
|
||||||
imageCount: replacementReferences.length,
|
imageCount: replacementReferences.length,
|
||||||
@ -6003,6 +6024,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase:
|
phase:
|
||||||
queueStatus.status === "running"
|
queueStatus.status === "running"
|
||||||
? "processing"
|
? "processing"
|
||||||
@ -6158,6 +6180,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: successCount > 0 ? "completed" : "failed",
|
phase: successCount > 0 ? "completed" : "failed",
|
||||||
modelName: resolvedModelDisplayName,
|
modelName: resolvedModelDisplayName,
|
||||||
imageCount: successCount,
|
imageCount: successCount,
|
||||||
@ -6390,6 +6413,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
modelName:
|
modelName:
|
||||||
selectedAgentPreset.defaultModel?.displayName ||
|
selectedAgentPreset.defaultModel?.displayName ||
|
||||||
@ -6941,6 +6965,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase:
|
phase:
|
||||||
queueStatus.status === "running"
|
queueStatus.status === "running"
|
||||||
? "processing"
|
? "processing"
|
||||||
@ -7266,6 +7291,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase:
|
phase:
|
||||||
queueStatus.status === "running"
|
queueStatus.status === "running"
|
||||||
? "processing"
|
? "processing"
|
||||||
@ -7494,6 +7520,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase:
|
phase:
|
||||||
queueStatus.status === "running"
|
queueStatus.status === "running"
|
||||||
? "processing"
|
? "processing"
|
||||||
@ -7812,6 +7839,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: successCount > 0 ? "completed" : "failed",
|
phase: successCount > 0 ? "completed" : "failed",
|
||||||
modelName:
|
modelName:
|
||||||
selectedAgentPreset.defaultModel?.displayName ||
|
selectedAgentPreset.defaultModel?.displayName ||
|
||||||
@ -8021,6 +8049,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: requestedModelProviderType,
|
providerType: requestedModelProviderType,
|
||||||
modelName: finalModelId,
|
modelName: finalModelId,
|
||||||
@ -8059,6 +8088,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
...directImageStatusPayload,
|
...directImageStatusPayload,
|
||||||
},
|
},
|
||||||
@ -8151,6 +8181,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: requestedModelProviderType,
|
providerType: requestedModelProviderType,
|
||||||
modelName: directImageResult.model,
|
modelName: directImageResult.model,
|
||||||
@ -8171,42 +8202,16 @@ export async function POST(request: Request) {
|
|||||||
);
|
);
|
||||||
} catch (directToolError) {
|
} catch (directToolError) {
|
||||||
chatApiLogger.error("[Chat API] Direct agent image generation failed:", directToolError);
|
chatApiLogger.error("[Chat API] Direct agent image generation failed:", directToolError);
|
||||||
const errorMessage = getDirectAgentToolErrorMessage(
|
const errorMessage = await reportImageGenerationFailure({
|
||||||
"generate_image",
|
writer, chatId: id, userMessageId: message.id,
|
||||||
directToolError,
|
messageId: assistantMessageId, error: directToolError,
|
||||||
);
|
label: "agent:generate_image:error",
|
||||||
await saveAssistantTextOnlyMessage({
|
|
||||||
chatId: id,
|
|
||||||
messageId: assistantMessageId,
|
|
||||||
text: errorMessage,
|
|
||||||
});
|
});
|
||||||
writeAssistantTextChunk(
|
|
||||||
writer,
|
|
||||||
errorMessage,
|
|
||||||
"agent:generate_image:error",
|
|
||||||
assistantMessageId,
|
|
||||||
);
|
|
||||||
if (contextPayload?.source === "canvas") {
|
|
||||||
writeImageGenerationStatusChunk(
|
|
||||||
writer,
|
|
||||||
{
|
|
||||||
phase: "failed",
|
|
||||||
providerType: requestedModelProviderType,
|
|
||||||
modelName: finalModelId,
|
|
||||||
},
|
|
||||||
"agent:generate_image:status-failed",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
writeAgentProgressChunk(
|
writeAgentProgressChunk(
|
||||||
writer,
|
writer,
|
||||||
buildDirectAgentImageGenerateFailedProgress(errorMessage),
|
buildDirectAgentImageGenerateFailedProgress(errorMessage),
|
||||||
"agent:generate_image:progress-failed",
|
"agent:generate_image:progress-failed",
|
||||||
);
|
);
|
||||||
writeAssistantTextChunk(
|
|
||||||
writer,
|
|
||||||
"<!-- IMAGE_GENERATION_END -->",
|
|
||||||
"agent:generate_image:end-error",
|
|
||||||
);
|
|
||||||
} finally {
|
} finally {
|
||||||
stopDirectImageKeepalive();
|
stopDirectImageKeepalive();
|
||||||
if (stopDirectImageStatus) {
|
if (stopDirectImageStatus) {
|
||||||
@ -8748,6 +8753,19 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (/^[0-9a-f-]{36}$/i.test(finalModelId)) {
|
||||||
|
const requested = await getAiModelById({ id: finalModelId });
|
||||||
|
if (requested?.supportsImageGen && isImageModelBalanceBlocked(requested)) {
|
||||||
|
const replacement = resolveConfiguredImageBalanceFallback(requested, await getEnabledAiModelsWithProvider(), {
|
||||||
|
imageSize: effectiveImageGenerationParams.imageSize,
|
||||||
|
aspectRatio: effectiveImageGenerationParams.aspectRatio,
|
||||||
|
referenceImageCount: countChatMessageImageInputs(uiMessagesForModel.filter(item => item.role === "user").slice(-1)),
|
||||||
|
});
|
||||||
|
if (!replacement) return new ChatSDKError("bad_request:api", "当前生图线路余额不足,已暂停新请求;请切换可用模型,已有任务和图片会保留。").toResponse();
|
||||||
|
finalModelId = replacement.model.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 检查是否是 UUID 格式(数据库中的模型 ID)
|
// 检查是否是 UUID 格式(数据库中的模型 ID)
|
||||||
const isUUID =
|
const isUUID =
|
||||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
|
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(
|
||||||
@ -8865,7 +8883,7 @@ export async function POST(request: Request) {
|
|||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join("\n\n");
|
.join("\n\n");
|
||||||
|
|
||||||
const activeRuntimeModelDescriptor = await resolveAgentModelSlotDescriptor(
|
let activeRuntimeModelDescriptor = await resolveAgentModelSlotDescriptor(
|
||||||
resolvedRuntimeModelLookupId,
|
resolvedRuntimeModelLookupId,
|
||||||
).catch(() => null);
|
).catch(() => null);
|
||||||
if (shouldRunAgentConversation && activeRuntimeModelDescriptor) {
|
if (shouldRunAgentConversation && activeRuntimeModelDescriptor) {
|
||||||
@ -9155,6 +9173,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: (languageModel as any).provider,
|
providerType: (languageModel as any).provider,
|
||||||
modelName: currentModelConfig?.displayName || actualModelId,
|
modelName: currentModelConfig?.displayName || actualModelId,
|
||||||
@ -9198,8 +9217,11 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
// 🆕 尝试备用模型(支持跨服务商切换)
|
// 🆕 尝试备用模型(支持跨服务商切换)
|
||||||
let fallbackSuccess = false;
|
let fallbackSuccess = false;
|
||||||
|
const balanceBlockedProviders = new Set<string>();
|
||||||
|
if (isProviderBalanceError(primaryError) && currentModelConfig?.providerId) balanceBlockedProviders.add(currentModelConfig.providerId);
|
||||||
const triedFallbackModelIds = new Set<string>();
|
const triedFallbackModelIds = new Set<string>();
|
||||||
for (const candidate of fallbackCandidates) {
|
for (const candidate of fallbackCandidates) {
|
||||||
|
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, buildMessagesForGemini(), balanceBlockedProviders)) continue;
|
||||||
try {
|
try {
|
||||||
triedFallbackModelIds.add(String(candidate.model.id || ""));
|
triedFallbackModelIds.add(String(candidate.model.id || ""));
|
||||||
chatApiLogger.info(`[Chat API] 🔄 Trying fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] 🔄 Trying fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
@ -9221,6 +9243,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info(`[Chat API] ✅ Fallback model ${fallbackResult.displayName} succeeded!`);
|
chatApiLogger.info(`[Chat API] ✅ Fallback model ${fallbackResult.displayName} succeeded!`);
|
||||||
break;
|
break;
|
||||||
} catch (fallbackError) {
|
} catch (fallbackError) {
|
||||||
|
if (isProviderBalanceError(fallbackError)) balanceBlockedProviders.add(candidate.provider.id);
|
||||||
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
||||||
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
||||||
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
||||||
@ -9242,6 +9265,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.warn("[Chat API] Record Gemini fallback health failure failed:", healthError);
|
chatApiLogger.warn("[Chat API] Record Gemini fallback health failure failed:", healthError);
|
||||||
});
|
});
|
||||||
chatApiLogger.error(`[Chat API] ❌ Fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ Fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
|
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -9273,6 +9297,7 @@ export async function POST(request: Request) {
|
|||||||
}).filter((candidate) => !triedFallbackModelIds.has(String(candidate.model.id || "")));
|
}).filter((candidate) => !triedFallbackModelIds.has(String(candidate.model.id || "")));
|
||||||
|
|
||||||
for (const candidate of crossProviderCandidates) {
|
for (const candidate of crossProviderCandidates) {
|
||||||
|
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, buildMessagesForGemini(), balanceBlockedProviders)) continue;
|
||||||
try {
|
try {
|
||||||
triedFallbackModelIds.add(String(candidate.model.id || ""));
|
triedFallbackModelIds.add(String(candidate.model.id || ""));
|
||||||
chatApiLogger.info(`[Chat API] 🔁 Gemini cross-provider failover: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] 🔁 Gemini cross-provider failover: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
@ -9295,6 +9320,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info(`[Chat API] ✅ Gemini cross-provider fallback model ${fallbackResult.displayName} succeeded!`);
|
chatApiLogger.info(`[Chat API] ✅ Gemini cross-provider fallback model ${fallbackResult.displayName} succeeded!`);
|
||||||
break;
|
break;
|
||||||
} catch (fallbackError) {
|
} catch (fallbackError) {
|
||||||
|
if (isProviderBalanceError(fallbackError)) balanceBlockedProviders.add(candidate.provider.id);
|
||||||
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
||||||
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
||||||
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
||||||
@ -9316,6 +9342,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.warn("[Chat API] Record Gemini cross-provider health failure failed:", healthError);
|
chatApiLogger.warn("[Chat API] Record Gemini cross-provider health failure failed:", healthError);
|
||||||
});
|
});
|
||||||
chatApiLogger.error(`[Chat API] ❌ Gemini cross-provider fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ Gemini cross-provider fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
|
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -9383,6 +9410,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: (languageModel as any).provider,
|
providerType: (languageModel as any).provider,
|
||||||
modelName: usedModelDisplayName,
|
modelName: usedModelDisplayName,
|
||||||
@ -9447,6 +9475,14 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
chatApiLogger.error("[Chat API] Gemini generation error:", error);
|
chatApiLogger.error("[Chat API] Gemini generation error:", error);
|
||||||
|
let finalError = error;
|
||||||
|
if (!shouldFailoverAiModelError(error)) {
|
||||||
|
await reportImageGenerationFailure({
|
||||||
|
writer, chatId: id, userMessageId: message.id,
|
||||||
|
messageId: generateUUID(), error, label: "gemini:error",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
chatApiLogger.info("[Chat API] Attempting failover to Volcano Vision...");
|
chatApiLogger.info("[Chat API] Attempting failover to Volcano Vision...");
|
||||||
|
|
||||||
// 故障转移:尝试使用火山引擎模型
|
// 故障转移:尝试使用火山引擎模型
|
||||||
@ -9524,6 +9560,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: "volcano-vision",
|
providerType: "volcano-vision",
|
||||||
modelName: volcanoModel.model.displayName || volcanoModel.model.modelId,
|
modelName: volcanoModel.model.displayName || volcanoModel.model.modelId,
|
||||||
@ -9589,46 +9626,14 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info("[Chat API] No Volcano Vision model available for failover");
|
chatApiLogger.info("[Chat API] No Volcano Vision model available for failover");
|
||||||
} catch (failoverError) {
|
} catch (failoverError) {
|
||||||
chatApiLogger.error("[Chat API] Failover to Volcano Vision also failed:", failoverError);
|
chatApiLogger.error("[Chat API] Failover to Volcano Vision also failed:", failoverError);
|
||||||
|
finalError = failoverError;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果故障转移也失败,返回错误消息
|
// 如果故障转移也失败,返回错误消息
|
||||||
const errorMessageId = generateUUID();
|
await reportImageGenerationFailure({
|
||||||
writeAssistantTextChunk(
|
writer, chatId: id, userMessageId: message.id,
|
||||||
writer,
|
messageId: generateUUID(), error: finalError, label: "gemini:error",
|
||||||
"图片生成失败,请稍后重试",
|
|
||||||
"gemini:error",
|
|
||||||
errorMessageId,
|
|
||||||
);
|
|
||||||
|
|
||||||
// 🆕 图片生成结束标记(失败)
|
|
||||||
writeAssistantTextChunk(
|
|
||||||
writer,
|
|
||||||
"<!-- IMAGE_GENERATION_END -->",
|
|
||||||
"gemini:image-end-failed",
|
|
||||||
);
|
|
||||||
|
|
||||||
// 尝试保存错误消息
|
|
||||||
try {
|
|
||||||
await saveMessages({
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
id: errorMessageId,
|
|
||||||
chatId: id,
|
|
||||||
role: "assistant",
|
|
||||||
parts: [{ type: "text", text: "图片生成失败,请稍后重试" }],
|
|
||||||
attachments: [],
|
|
||||||
createdAt: new Date(),
|
|
||||||
videoStorageStatus: null,
|
|
||||||
temporaryVideoUrl: null,
|
|
||||||
permanentVideoUrl: null,
|
|
||||||
archivedAt: null,
|
|
||||||
videoMetadata: null,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
} catch (saveError) {
|
|
||||||
chatApiLogger.error("[Chat API] Failed to save error message:", saveError);
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
stopKeepalive?.();
|
stopKeepalive?.();
|
||||||
}
|
}
|
||||||
@ -9826,6 +9831,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "queued",
|
phase: "queued",
|
||||||
providerType: "fal-image",
|
providerType: "fal-image",
|
||||||
modelName: modelDisplayName,
|
modelName: modelDisplayName,
|
||||||
@ -9932,6 +9938,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: (languageModel as any).provider,
|
providerType: (languageModel as any).provider,
|
||||||
modelName: alibabaModelConfig?.displayName || actualModelId,
|
modelName: alibabaModelConfig?.displayName || actualModelId,
|
||||||
@ -9981,7 +9988,10 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
let fallbackSuccess = false;
|
let fallbackSuccess = false;
|
||||||
const triedFallbackModelIds = new Set<string>();
|
const triedFallbackModelIds = new Set<string>();
|
||||||
|
const balanceBlockedProviders = new Set<string>();
|
||||||
|
if (isProviderBalanceError(primaryError) && currentModelEntry?.provider.id) balanceBlockedProviders.add(currentModelEntry.provider.id);
|
||||||
for (const candidate of fallbackCandidates) {
|
for (const candidate of fallbackCandidates) {
|
||||||
|
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForAlibaba, balanceBlockedProviders)) continue;
|
||||||
try {
|
try {
|
||||||
triedFallbackModelIds.add(String(candidate.model.id || ""));
|
triedFallbackModelIds.add(String(candidate.model.id || ""));
|
||||||
chatApiLogger.info(`[Chat API] 🔄 Alibaba fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] 🔄 Alibaba fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
@ -10004,6 +10014,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info(`[Chat API] ✅ Alibaba fallback model ${fallbackResult.displayName} succeeded!`);
|
chatApiLogger.info(`[Chat API] ✅ Alibaba fallback model ${fallbackResult.displayName} succeeded!`);
|
||||||
break;
|
break;
|
||||||
} catch (fallbackError) {
|
} catch (fallbackError) {
|
||||||
|
if (isProviderBalanceError(fallbackError)) balanceBlockedProviders.add(candidate.provider.id);
|
||||||
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
||||||
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
||||||
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
||||||
@ -10025,6 +10036,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.warn("[Chat API] Record image fallback health failure failed:", healthError);
|
chatApiLogger.warn("[Chat API] Record image fallback health failure failed:", healthError);
|
||||||
});
|
});
|
||||||
chatApiLogger.error(`[Chat API] ❌ Alibaba fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ Alibaba fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
|
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -10052,9 +10064,10 @@ export async function POST(request: Request) {
|
|||||||
currentModelKey: actualModelId,
|
currentModelKey: actualModelId,
|
||||||
currentProviderId: alibabaModelConfig?.providerId,
|
currentProviderId: alibabaModelConfig?.providerId,
|
||||||
providerPriority,
|
providerPriority,
|
||||||
}).filter((candidate) => !triedFallbackModelIds.has(String(candidate.model.id || "")));
|
}).filter((candidate) => !triedFallbackModelIds.has(String(candidate.model.id || "")) && !balanceBlockedProviders.has(candidate.provider.id));
|
||||||
|
|
||||||
for (const candidate of crossProviderCandidates) {
|
for (const candidate of crossProviderCandidates) {
|
||||||
|
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForAlibaba, balanceBlockedProviders)) continue;
|
||||||
try {
|
try {
|
||||||
triedFallbackModelIds.add(String(candidate.model.id || ""));
|
triedFallbackModelIds.add(String(candidate.model.id || ""));
|
||||||
chatApiLogger.info(`[Chat API] 🔁 Cross-provider failover attempt: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] 🔁 Cross-provider failover attempt: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
@ -10136,6 +10149,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info(`[Chat API] ✅ Cross-provider failover succeeded: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] ✅ Cross-provider failover succeeded: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
break;
|
break;
|
||||||
} catch (crossError) {
|
} catch (crossError) {
|
||||||
|
if (isProviderBalanceError(crossError)) balanceBlockedProviders.add(candidate.provider.id);
|
||||||
const crossErrorMessage = normalizeFailoverErrorMessage(crossError);
|
const crossErrorMessage = normalizeFailoverErrorMessage(crossError);
|
||||||
if (shouldCooldownImageFallbackModel(crossErrorMessage)) {
|
if (shouldCooldownImageFallbackModel(crossErrorMessage)) {
|
||||||
setImageFallbackModelCooldown(candidate.model.id, crossErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, crossErrorMessage);
|
||||||
@ -10157,6 +10171,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.warn("[Chat API] Record image cross-provider health failure failed:", healthError);
|
chatApiLogger.warn("[Chat API] Record image cross-provider health failure failed:", healthError);
|
||||||
});
|
});
|
||||||
chatApiLogger.error(`[Chat API] ❌ Cross-provider failover failed: ${candidate.model.displayName} (${candidate.provider.type})`, crossError);
|
chatApiLogger.error(`[Chat API] ❌ Cross-provider failover failed: ${candidate.model.displayName} (${candidate.provider.type})`, crossError);
|
||||||
|
if (!shouldFailoverAiModelError(crossError)) throw crossError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -10224,6 +10239,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: (languageModel as any).provider,
|
providerType: (languageModel as any).provider,
|
||||||
modelName: usedModelDisplayName,
|
modelName: usedModelDisplayName,
|
||||||
@ -10288,43 +10304,10 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.error("[Chat API] Alibaba generation error:", error);
|
chatApiLogger.error("[Chat API] Alibaba generation error:", error);
|
||||||
|
|
||||||
// 返回错误消息
|
// 返回错误消息
|
||||||
const errorMessageId = generateUUID();
|
await reportImageGenerationFailure({
|
||||||
const errorText = getPublicImageGenerationErrorText(error);
|
writer, chatId: id, userMessageId: message.id,
|
||||||
writeAssistantTextChunk(
|
messageId: generateUUID(), error, label: "alibaba:error",
|
||||||
writer,
|
|
||||||
errorText,
|
|
||||||
"alibaba:error",
|
|
||||||
errorMessageId,
|
|
||||||
);
|
|
||||||
|
|
||||||
// 🆕 图片生成结束标记(失败)
|
|
||||||
writeAssistantTextChunk(
|
|
||||||
writer,
|
|
||||||
"<!-- IMAGE_GENERATION_END -->",
|
|
||||||
"alibaba:image-end-failed",
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await saveMessages({
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
id: errorMessageId,
|
|
||||||
chatId: id,
|
|
||||||
role: "assistant",
|
|
||||||
parts: [{ type: "text", text: errorText }],
|
|
||||||
attachments: [],
|
|
||||||
createdAt: new Date(),
|
|
||||||
videoStorageStatus: null,
|
|
||||||
temporaryVideoUrl: null,
|
|
||||||
permanentVideoUrl: null,
|
|
||||||
archivedAt: null,
|
|
||||||
videoMetadata: null,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
} catch (saveError) {
|
|
||||||
chatApiLogger.error("[Chat API] Failed to save error message:", saveError);
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
stopKeepalive?.();
|
stopKeepalive?.();
|
||||||
}
|
}
|
||||||
@ -10453,6 +10436,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: "volcano-vision",
|
providerType: "volcano-vision",
|
||||||
modelName: volcanoModelConfig?.displayName || actualModelId,
|
modelName: volcanoModelConfig?.displayName || actualModelId,
|
||||||
@ -10471,7 +10455,10 @@ export async function POST(request: Request) {
|
|||||||
throw primaryError;
|
throw primaryError;
|
||||||
}
|
}
|
||||||
let fallbackSuccess = false;
|
let fallbackSuccess = false;
|
||||||
|
const balanceBlockedProviders = new Set<string>();
|
||||||
|
if (isProviderBalanceError(primaryError) && volcanoModelConfig?.providerId) balanceBlockedProviders.add(volcanoModelConfig.providerId);
|
||||||
for (const candidate of fallbackCandidates) {
|
for (const candidate of fallbackCandidates) {
|
||||||
|
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForVolcano, balanceBlockedProviders)) continue;
|
||||||
try {
|
try {
|
||||||
chatApiLogger.info(`[Chat API] 🔄 Volcano fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] 🔄 Volcano fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
const fallbackResult = await generateImageWithFailoverCandidate({
|
const fallbackResult = await generateImageWithFailoverCandidate({
|
||||||
@ -10493,11 +10480,13 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info(`[Chat API] ✅ Volcano fallback model ${fallbackResult.displayName} succeeded!`);
|
chatApiLogger.info(`[Chat API] ✅ Volcano fallback model ${fallbackResult.displayName} succeeded!`);
|
||||||
break;
|
break;
|
||||||
} catch (fallbackError) {
|
} catch (fallbackError) {
|
||||||
|
if (isProviderBalanceError(fallbackError)) balanceBlockedProviders.add(candidate.provider.id);
|
||||||
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
||||||
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
||||||
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
||||||
}
|
}
|
||||||
chatApiLogger.error(`[Chat API] ❌ Volcano fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ Volcano fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
|
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!fallbackSuccess) {
|
if (!fallbackSuccess) {
|
||||||
@ -10527,6 +10516,7 @@ export async function POST(request: Request) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
for (const candidate of crossProviderCandidates) {
|
for (const candidate of crossProviderCandidates) {
|
||||||
|
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForVolcano, balanceBlockedProviders)) continue;
|
||||||
try {
|
try {
|
||||||
chatApiLogger.info(`[Chat API] 🔁 Volcano cross-provider failover attempt: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] 🔁 Volcano cross-provider failover attempt: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
if (
|
if (
|
||||||
@ -10607,6 +10597,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info(`[Chat API] ✅ Volcano cross-provider failover succeeded: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] ✅ Volcano cross-provider failover succeeded: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
break;
|
break;
|
||||||
} catch (crossError) {
|
} catch (crossError) {
|
||||||
|
if (isProviderBalanceError(crossError)) balanceBlockedProviders.add(candidate.provider.id);
|
||||||
const crossErrorMessage = normalizeFailoverErrorMessage(crossError);
|
const crossErrorMessage = normalizeFailoverErrorMessage(crossError);
|
||||||
if (shouldCooldownImageFallbackModel(crossErrorMessage)) {
|
if (shouldCooldownImageFallbackModel(crossErrorMessage)) {
|
||||||
setImageFallbackModelCooldown(candidate.model.id, crossErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, crossErrorMessage);
|
||||||
@ -10664,6 +10655,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: "volcano-vision",
|
providerType: "volcano-vision",
|
||||||
modelName: usedModelDisplayName,
|
modelName: usedModelDisplayName,
|
||||||
@ -10728,43 +10720,10 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
chatApiLogger.error("[Chat API] Volcano Vision generation error:", error);
|
chatApiLogger.error("[Chat API] Volcano Vision generation error:", error);
|
||||||
const errorMessageId = generateUUID();
|
await reportImageGenerationFailure({
|
||||||
writeAssistantTextChunk(
|
writer, chatId: id, userMessageId: message.id,
|
||||||
writer,
|
messageId: generateUUID(), error, label: "volcano:error",
|
||||||
"图片生成失败,请稍后重试",
|
|
||||||
"volcano:error",
|
|
||||||
errorMessageId,
|
|
||||||
);
|
|
||||||
|
|
||||||
// 🆕 图片生成结束标记(失败)
|
|
||||||
writeAssistantTextChunk(
|
|
||||||
writer,
|
|
||||||
"<!-- IMAGE_GENERATION_END -->",
|
|
||||||
"volcano:image-end-failed",
|
|
||||||
);
|
|
||||||
|
|
||||||
// 尝试保存错误消息
|
|
||||||
try {
|
|
||||||
await saveMessages({
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
id: errorMessageId,
|
|
||||||
chatId: id,
|
|
||||||
role: "assistant",
|
|
||||||
parts: [{ type: "text", text: "图片生成失败,请稍后重试" }],
|
|
||||||
attachments: [], // 必须提供 attachments 字段
|
|
||||||
createdAt: new Date(),
|
|
||||||
videoStorageStatus: null,
|
|
||||||
temporaryVideoUrl: null,
|
|
||||||
permanentVideoUrl: null,
|
|
||||||
archivedAt: null,
|
|
||||||
videoMetadata: null,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
} catch (saveError) {
|
|
||||||
chatApiLogger.error("[Chat API] Failed to save error message:", saveError);
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
stopKeepalive?.();
|
stopKeepalive?.();
|
||||||
}
|
}
|
||||||
@ -11222,7 +11181,7 @@ export async function POST(request: Request) {
|
|||||||
requestHeaders: request.headers,
|
requestHeaders: request.headers,
|
||||||
requestUrl: request.url,
|
requestUrl: request.url,
|
||||||
defaultModelId: finalModelId,
|
defaultModelId: finalModelId,
|
||||||
destructiveConfirmed: isCanvasOperationConfirmText(requestTextContent),
|
authorizeDestructive: (nodeIds) => isExplicitDestructiveIntent(requestTextContent) && nodeIds.length > 0 && nodeIds.every((nodeId) => selectedCanvasImageNodeIds.includes(nodeId)),
|
||||||
callbacks: {
|
callbacks: {
|
||||||
onClientCommand: (command, capability) => {
|
onClientCommand: (command, capability) => {
|
||||||
writeCanvasCapabilityCommandChunk(
|
writeCanvasCapabilityCommandChunk(
|
||||||
@ -11337,6 +11296,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
dataStream as unknown as StreamWriter,
|
dataStream as unknown as StreamWriter,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: currentModelConfig?.providerId ? resolvedProviderType || providerType : providerType,
|
providerType: currentModelConfig?.providerId ? resolvedProviderType || providerType : providerType,
|
||||||
modelName: currentModelConfig?.displayName || actualModelId,
|
modelName: currentModelConfig?.displayName || actualModelId,
|
||||||
@ -11381,7 +11341,10 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let fallbackSuccess = false;
|
let fallbackSuccess = false;
|
||||||
|
const balanceBlockedProviders = new Set<string>();
|
||||||
|
if (isProviderBalanceError(primaryError) && currentModelConfig?.providerId) balanceBlockedProviders.add(currentModelConfig.providerId);
|
||||||
for (const candidate of fallbackCandidates) {
|
for (const candidate of fallbackCandidates) {
|
||||||
|
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForCrossProvider, balanceBlockedProviders)) continue;
|
||||||
try {
|
try {
|
||||||
chatApiLogger.info(`[Chat API] 🔄 OpenAI-compatible fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] 🔄 OpenAI-compatible fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
const shouldUseOpenAICompatFallback =
|
const shouldUseOpenAICompatFallback =
|
||||||
@ -11423,11 +11386,13 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info(`[Chat API] ✅ OpenAI-compatible fallback model ${candidate.model.displayName} succeeded!`);
|
chatApiLogger.info(`[Chat API] ✅ OpenAI-compatible fallback model ${candidate.model.displayName} succeeded!`);
|
||||||
break;
|
break;
|
||||||
} catch (fallbackError) {
|
} catch (fallbackError) {
|
||||||
|
if (isProviderBalanceError(fallbackError)) balanceBlockedProviders.add(candidate.provider.id);
|
||||||
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
|
||||||
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
|
||||||
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
||||||
}
|
}
|
||||||
chatApiLogger.error(`[Chat API] ❌ OpenAI-compatible fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ OpenAI-compatible fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
|
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -11458,6 +11423,7 @@ export async function POST(request: Request) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
for (const candidate of crossProviderCandidates) {
|
for (const candidate of crossProviderCandidates) {
|
||||||
|
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForCrossProvider, balanceBlockedProviders)) continue;
|
||||||
try {
|
try {
|
||||||
chatApiLogger.info(`[Chat API] 🔁 OpenAI-compatible cross-provider failover: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] 🔁 OpenAI-compatible cross-provider failover: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
if (
|
if (
|
||||||
@ -11541,6 +11507,7 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info(`[Chat API] ✅ OpenAI-compatible cross-provider failover succeeded: ${candidate.model.displayName} (${candidate.provider.type})`);
|
chatApiLogger.info(`[Chat API] ✅ OpenAI-compatible cross-provider failover succeeded: ${candidate.model.displayName} (${candidate.provider.type})`);
|
||||||
break;
|
break;
|
||||||
} catch (crossError) {
|
} catch (crossError) {
|
||||||
|
if (isProviderBalanceError(crossError)) balanceBlockedProviders.add(candidate.provider.id);
|
||||||
const crossErrorMessage = normalizeFailoverErrorMessage(crossError);
|
const crossErrorMessage = normalizeFailoverErrorMessage(crossError);
|
||||||
if (shouldCooldownImageFallbackModel(crossErrorMessage)) {
|
if (shouldCooldownImageFallbackModel(crossErrorMessage)) {
|
||||||
setImageFallbackModelCooldown(candidate.model.id, crossErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, crossErrorMessage);
|
||||||
@ -11599,6 +11566,7 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
dataStream as unknown as StreamWriter,
|
dataStream as unknown as StreamWriter,
|
||||||
{
|
{
|
||||||
|
userMessageId: message.id,
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: resolvedProviderType || providerType,
|
providerType: resolvedProviderType || providerType,
|
||||||
modelName: usedModelDisplayName,
|
modelName: usedModelDisplayName,
|
||||||
@ -11658,54 +11626,10 @@ export async function POST(request: Request) {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
chatApiLogger.error("[Chat API] OpenAI-compatible image model generation error:", error);
|
chatApiLogger.error("[Chat API] OpenAI-compatible image model generation error:", error);
|
||||||
|
|
||||||
const errorMessageId = generateUUID();
|
await reportImageGenerationFailure({
|
||||||
const errorText = getPublicImageGenerationErrorText(error);
|
writer: dataStream as unknown as StreamWriter,
|
||||||
|
chatId: id, userMessageId: message.id,
|
||||||
dataStream.write({
|
messageId: generateUUID(), error, label: "openai-compat:error",
|
||||||
type: "data-appendMessage",
|
|
||||||
data: JSON.stringify({
|
|
||||||
id: errorMessageId,
|
|
||||||
role: "assistant",
|
|
||||||
parts: [{ type: "text", text: errorText }],
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
dataStream.write({
|
|
||||||
type: "data-appendMessage",
|
|
||||||
data: JSON.stringify({
|
|
||||||
id: generateUUID(),
|
|
||||||
role: "assistant",
|
|
||||||
parts: [{ type: "text", text: "<!-- IMAGE_GENERATION_END -->" }],
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
if (contextPayload?.source === "canvas") {
|
|
||||||
writeImageGenerationStatusChunk(
|
|
||||||
dataStream as unknown as StreamWriter,
|
|
||||||
{
|
|
||||||
phase: "failed",
|
|
||||||
providerType: resolvedProviderType || providerType,
|
|
||||||
modelName: currentModelConfig?.displayName || actualModelId,
|
|
||||||
imageCount: effectiveImageParams?.imageCount || 1,
|
|
||||||
},
|
|
||||||
"openai-compat:image-status-failed",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await saveMessages({
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
id: errorMessageId,
|
|
||||||
role: "assistant",
|
|
||||||
parts: [{ type: "text", text: errorText }],
|
|
||||||
createdAt: new Date(),
|
|
||||||
attachments: [],
|
|
||||||
chatId: id,
|
|
||||||
videoStorageStatus: null,
|
|
||||||
temporaryVideoUrl: null,
|
|
||||||
permanentVideoUrl: null,
|
|
||||||
archivedAt: null,
|
|
||||||
videoMetadata: null,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -12251,8 +12175,70 @@ export async function POST(request: Request) {
|
|||||||
const shouldDisableSmoothStream =
|
const shouldDisableSmoothStream =
|
||||||
resolvedModelSupportsImageGen || isVolcanoVideoModel;
|
resolvedModelSupportsImageGen || isVolcanoVideoModel;
|
||||||
|
|
||||||
|
let activeConversationModel = currentModelConfig;
|
||||||
|
let activeConversationProvider = currentProviderConfig;
|
||||||
|
const recordedAuthFailures = new Set<string>();
|
||||||
|
const recordConversationAuthFailure = async (error: unknown) => {
|
||||||
|
const key = activeConversationModel?.id;
|
||||||
|
if (!key || recordedAuthFailures.has(key)) return;
|
||||||
|
recordedAuthFailures.add(key);
|
||||||
|
recordProviderHealthFailure({
|
||||||
|
providerKey: activeConversationProvider?.id,
|
||||||
|
providerType: activeConversationProvider?.type,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
await recordAiModelHealthFailure({
|
||||||
|
model: activeConversationModel,
|
||||||
|
provider: activeConversationProvider,
|
||||||
|
error,
|
||||||
|
context: "canvas_agent_conversation_auth",
|
||||||
|
}).catch((healthError) => chatApiLogger.warn("[Chat API] Could not persist model authentication failure:", healthError));
|
||||||
|
};
|
||||||
|
const needsVisionFallback = convertedMessages.some((item: any) =>
|
||||||
|
Array.isArray(item.content) && item.content.some((part: any) =>
|
||||||
|
part.type === "image" || (part.type === "file" && String(part.mediaType || "").startsWith("image/"))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
const streamModel = shouldRunAgentConversation && !shouldKeepSelectedModel &&
|
||||||
|
currentModelConfig?.excludeFromFailover !== true &&
|
||||||
|
!resolvedModelSupportsImageGen && !isVolcanoVideoModel
|
||||||
|
? withAuthenticationFailover({
|
||||||
|
primary: languageModel as any,
|
||||||
|
onAuthenticationFailure: recordConversationAuthFailure,
|
||||||
|
resolveFallback: async () => {
|
||||||
|
const enabled = await getEnabledAiModelsWithProvider(false);
|
||||||
|
const preferredIds = [currentModelConfig?.fallbackModelId1, currentModelConfig?.fallbackModelId2];
|
||||||
|
const candidates = enabled.filter(({ model, provider }) =>
|
||||||
|
model.id !== activeConversationModel?.id && model.isEnabled && provider.isEnabled &&
|
||||||
|
!model.excludeFromFailover && !model.isSubModel && model.healthStatus !== "auto_disabled" &&
|
||||||
|
!isImageGenerationModelType(provider.type, model) && !isVideoProviderType(provider.type, model) &&
|
||||||
|
(!needsVisionFallback || model.supportsVision) &&
|
||||||
|
(isAiModelAgentAuto(model) || preferredIds.includes(model.id)) &&
|
||||||
|
!shouldAvoidProvider(getProviderHealthSnapshot(provider.id))
|
||||||
|
).sort((a, b) => {
|
||||||
|
const rank = (id: string, isDefault: boolean) => {
|
||||||
|
const index = preferredIds.indexOf(id);
|
||||||
|
return index >= 0 ? index : isDefault ? 2 : 3;
|
||||||
|
};
|
||||||
|
return rank(a.model.id, a.model.isDefault) - rank(b.model.id, b.model.isDefault);
|
||||||
|
});
|
||||||
|
for (const candidate of candidates) {
|
||||||
|
const fallback = await getLanguageModelById(candidate.model.id);
|
||||||
|
if (!fallback || typeof fallback === "string") continue;
|
||||||
|
activeConversationModel = candidate.model;
|
||||||
|
activeConversationProvider = candidate.provider;
|
||||||
|
finalModelId = candidate.model.id;
|
||||||
|
actualModelId = candidate.model.modelId;
|
||||||
|
activeRuntimeModelDescriptor = await resolveAgentModelSlotDescriptor(candidate.model.id).catch(() => null);
|
||||||
|
return fallback as any;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
})
|
||||||
|
: languageModel;
|
||||||
const result = streamText({
|
const result = streamText({
|
||||||
model: languageModel,
|
model: streamModel,
|
||||||
|
maxRetries: shouldRunAgentConversation ? 0 : undefined,
|
||||||
system: effectiveConversationSystemPrompt,
|
system: effectiveConversationSystemPrompt,
|
||||||
messages: convertedMessages,
|
messages: convertedMessages,
|
||||||
stopWhen: stepCountIs(shouldUseCanvasCapabilityToolLoop ? 8 : 5),
|
stopWhen: stepCountIs(shouldUseCanvasCapabilityToolLoop ? 8 : 5),
|
||||||
|
|||||||
@ -10,6 +10,10 @@ import {
|
|||||||
} from "@/lib/db/queries";
|
} from "@/lib/db/queries";
|
||||||
import { getAiModelFrontendAvailability } from "@/lib/ai/model-frontend-availability";
|
import { getAiModelFrontendAvailability } from "@/lib/ai/model-frontend-availability";
|
||||||
import { buildVideoToolCapability } from "@/lib/ai/video-tool-capability";
|
import { buildVideoToolCapability } from "@/lib/ai/video-tool-capability";
|
||||||
|
import {
|
||||||
|
DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY,
|
||||||
|
findConfiguredDetailReplacementImageModel,
|
||||||
|
} from "@/lib/ai/detail-replacement-image-model-config";
|
||||||
import {
|
import {
|
||||||
isImageGenerationModelDescriptor,
|
isImageGenerationModelDescriptor,
|
||||||
isVideoModelDescriptor,
|
isVideoModelDescriptor,
|
||||||
@ -157,10 +161,17 @@ export async function GET(request: Request) {
|
|||||||
: await getEnabledAiModelsWithProvider(false, true);
|
: await getEnabledAiModelsWithProvider(false, true);
|
||||||
const agentModelWithProvider = await getAgentAiModelWithProvider();
|
const agentModelWithProvider = await getAgentAiModelWithProvider();
|
||||||
const defaultModel = await getDefaultAiModel();
|
const defaultModel = await getDefaultAiModel();
|
||||||
const [siteSettings, agentWorkflowUiConfigSetting] = await Promise.all([
|
const [siteSettings, agentWorkflowUiConfigSetting, detailReplacementModelSetting] = await Promise.all([
|
||||||
getSiteSettings(),
|
getSiteSettings(),
|
||||||
getSystemSetting({ key: AGENT_WORKFLOW_UI_CONFIG_KEY }).catch(() => null),
|
getSystemSetting({ key: AGENT_WORKFLOW_UI_CONFIG_KEY }).catch(() => null),
|
||||||
|
getSystemSetting({ key: DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY }).catch(() => null),
|
||||||
]);
|
]);
|
||||||
|
const detailReplacementModel = findConfiguredDetailReplacementImageModel(
|
||||||
|
includeUnavailable ? modelsWithProvider.filter(({ model, provider }) =>
|
||||||
|
provider.isEnabled && model.isEnabled && getAiModelFrontendAvailability(model).selectable !== false
|
||||||
|
) : modelsWithProvider,
|
||||||
|
detailReplacementModelSetting?.value,
|
||||||
|
);
|
||||||
type ModelWithProvider = (typeof modelsWithProvider)[number];
|
type ModelWithProvider = (typeof modelsWithProvider)[number];
|
||||||
const serializeModel = ({ model, provider }: ModelWithProvider) => ({
|
const serializeModel = ({ model, provider }: ModelWithProvider) => ({
|
||||||
id: model.id,
|
id: model.id,
|
||||||
@ -309,6 +320,7 @@ export async function GET(request: Request) {
|
|||||||
: null,
|
: null,
|
||||||
agentWorkflowDefaultModelId: agentWorkflowDefaultModel?.model.id || null,
|
agentWorkflowDefaultModelId: agentWorkflowDefaultModel?.model.id || null,
|
||||||
agentWorkflowConfiguredModels,
|
agentWorkflowConfiguredModels,
|
||||||
|
detailReplacementModel: detailReplacementModel ? serializeModel(detailReplacementModel) : null,
|
||||||
agentModel: agentModelWithProvider
|
agentModel: agentModelWithProvider
|
||||||
? {
|
? {
|
||||||
id: agentModelWithProvider.model.id,
|
id: agentModelWithProvider.model.id,
|
||||||
|
|||||||
@ -8,6 +8,8 @@ import { ArrowLeft, Download, ChevronLeft, ChevronRight, ChevronsLeft, ChevronsR
|
|||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import { formatBeijingTime } from "@/lib/utils";
|
import { formatBeijingTime } from "@/lib/utils";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
||||||
|
|
||||||
type FeaturedImage = {
|
type FeaturedImage = {
|
||||||
id: string;
|
id: string;
|
||||||
@ -74,18 +76,11 @@ export default function FeaturedPage() {
|
|||||||
|
|
||||||
const handleDownload = async (url: string, prompt: string) => {
|
const handleDownload = async (url: string, prompt: string) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url);
|
await downloadOriginalImage({ url, name: "featured-image" });
|
||||||
const blob = await response.blob();
|
toast.success("原图已开始下载");
|
||||||
const blobUrl = window.URL.createObjectURL(blob);
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = blobUrl;
|
|
||||||
link.download = `featured-${Date.now()}.png`;
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
window.URL.revokeObjectURL(blobUrl);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Download failed:", error);
|
console.error("Download failed:", error);
|
||||||
|
toast.error(getCanvasImageDownloadErrorMessage(error));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
@ -9,9 +10,10 @@ import { Input } from "@/components/ui/input";
|
|||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { requestLongTaskResult } from "@/lib/client/long-task";
|
import { requestLongTaskResult } from "@/lib/client/long-task";
|
||||||
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
import { buildImageProxyUrl, getImageDownloadExtension } from "@/lib/image-delivery";
|
||||||
import { inferOriginalImageUrl } from "@/lib/image-url";
|
import { inferOriginalImageUrl } from "@/lib/image-url";
|
||||||
import { formatBeijingTime } from "@/lib/utils";
|
import { formatBeijingTime } from "@/lib/utils";
|
||||||
|
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
||||||
|
|
||||||
type GalleryImage = {
|
type GalleryImage = {
|
||||||
id: string;
|
id: string;
|
||||||
@ -32,7 +34,7 @@ type Pagination = {
|
|||||||
totalPages: number;
|
totalPages: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
function buildGalleryDownloadFilename(url: string, alt: string) {
|
function buildGalleryDownloadFilename(url: string, alt: string, contentType: string) {
|
||||||
const rawUrl = String(url || "").trim();
|
const rawUrl = String(url || "").trim();
|
||||||
const rawAlt = String(alt || "").trim();
|
const rawAlt = String(alt || "").trim();
|
||||||
const safeBase =
|
const safeBase =
|
||||||
@ -58,7 +60,7 @@ function buildGalleryDownloadFilename(url: string, alt: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${safeBase}-${Date.now()}.${extension}`;
|
return `${safeBase}.${getImageDownloadExtension(contentType) || extension}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function GalleryPage() {
|
export default function GalleryPage() {
|
||||||
@ -103,7 +105,7 @@ export default function GalleryPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleDownload = async (url: string, alt: string) => {
|
const handleDownload = async (url: string, alt: string) => {
|
||||||
const originalUrl = inferOriginalImageUrl(String(url || "").trim());
|
const originalUrl = String(url || "").trim();
|
||||||
if (!originalUrl) {
|
if (!originalUrl) {
|
||||||
toast.error("图片地址无效");
|
toast.error("图片地址无效");
|
||||||
return;
|
return;
|
||||||
@ -112,32 +114,17 @@ export default function GalleryPage() {
|
|||||||
try {
|
try {
|
||||||
toast.info("正在准备下载...");
|
toast.info("正在准备下载...");
|
||||||
|
|
||||||
const response = await fetch(
|
// Validate the supplied URL before any legacy thumbnail inference can
|
||||||
buildImageProxyUrl(originalUrl, { delivery: "proxy" })
|
// remove signed storage transformation parameters.
|
||||||
);
|
buildImageProxyUrl(originalUrl, { delivery: "original" });
|
||||||
if (!response.ok) {
|
const parsed = new URL(originalUrl, window.location.origin);
|
||||||
throw new Error(`download failed: ${response.status}`);
|
const source = parsed.pathname === "/api/image-proxy" ? originalUrl : inferOriginalImageUrl(originalUrl);
|
||||||
}
|
|
||||||
const blob = await response.blob();
|
await downloadOriginalImage({ url: source, name: buildGalleryDownloadFilename(originalUrl, alt, "") });
|
||||||
const blobUrl = window.URL.createObjectURL(blob);
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = blobUrl;
|
|
||||||
link.download = buildGalleryDownloadFilename(originalUrl, alt);
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
window.setTimeout(() => window.URL.revokeObjectURL(blobUrl), 1000);
|
|
||||||
toast.success("图片已开始下载");
|
toast.success("图片已开始下载");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Download failed:", error);
|
console.error("Download failed:", error);
|
||||||
const fallbackLink = document.createElement("a");
|
toast.error(getCanvasImageDownloadErrorMessage(error));
|
||||||
fallbackLink.href = originalUrl;
|
|
||||||
fallbackLink.target = "_blank";
|
|
||||||
fallbackLink.rel = "noopener noreferrer";
|
|
||||||
document.body.appendChild(fallbackLink);
|
|
||||||
fallbackLink.click();
|
|
||||||
document.body.removeChild(fallbackLink);
|
|
||||||
toast.error("下载失败,已为你打开原图,可直接另存为");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -317,7 +304,12 @@ export default function GalleryPage() {
|
|||||||
<>
|
<>
|
||||||
<div className="flex-1 overflow-y-auto">
|
<div className="flex-1 overflow-y-auto">
|
||||||
<div className="flex flex-wrap justify-center gap-6 px-4">
|
<div className="flex flex-wrap justify-center gap-6 px-4">
|
||||||
{images.map((img) => (
|
{images.map((img) => isRetiredStorageUrl(img.url) ? (
|
||||||
|
<div key={img.id} className="flex h-[270px] w-[200px] shrink-0 flex-col justify-center rounded-lg border bg-muted p-4 text-center text-sm text-muted-foreground">
|
||||||
|
<p>{RETIRED_STORAGE_MESSAGE}</p>
|
||||||
|
<p className="mt-3 line-clamp-3 text-xs">{img.alt}</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
<div
|
<div
|
||||||
key={img.id}
|
key={img.id}
|
||||||
className="group relative cursor-pointer rounded-lg border bg-muted overflow-hidden transition-all hover:shadow-lg hover:scale-105"
|
className="group relative cursor-pointer rounded-lg border bg-muted overflow-hidden transition-all hover:shadow-lg hover:scale-105"
|
||||||
@ -326,7 +318,7 @@ export default function GalleryPage() {
|
|||||||
>
|
>
|
||||||
<div className="relative w-full h-full">
|
<div className="relative w-full h-full">
|
||||||
<Image
|
<Image
|
||||||
src={img.thumbnailUrl || img.url}
|
src={isRetiredStorageUrl(img.thumbnailUrl) ? img.url : img.thumbnailUrl || img.url}
|
||||||
alt={img.alt}
|
alt={img.alt}
|
||||||
width={200}
|
width={200}
|
||||||
height={270}
|
height={270}
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { requireAdmin } from "@/lib/middleware/admin";
|
import { requireAdmin } from "@/lib/middleware/admin";
|
||||||
import { ChatSDKError } from "@/lib/errors";
|
import { ChatSDKError } from "@/lib/errors";
|
||||||
|
import { db } from "@/lib/db";
|
||||||
|
import { aiModelHealthEvent } from "@/lib/db/schema";
|
||||||
|
import { and, desc, eq, sql } from "drizzle-orm";
|
||||||
import {
|
import {
|
||||||
listAiModelHealthEvents,
|
|
||||||
restoreAiModelHealth,
|
restoreAiModelHealth,
|
||||||
} from "@/lib/ai/runtime/model-health";
|
} from "@/lib/ai/runtime/model-health";
|
||||||
|
|
||||||
@ -15,14 +17,30 @@ export async function GET(request: Request) {
|
|||||||
await requireAdmin();
|
await requireAdmin();
|
||||||
|
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const modelId = searchParams.get("modelId") || undefined;
|
const modelId = searchParams.get("modelId");
|
||||||
const limit = Number(searchParams.get("limit") || 80);
|
const providerId = searchParams.get("providerId");
|
||||||
const events = await listAiModelHealthEvents({
|
const rawLimit = Number(searchParams.get("limit") || 20);
|
||||||
modelId,
|
const limit = Number.isFinite(rawLimit) ? Math.max(1, Math.min(200, Math.floor(rawLimit))) : 20;
|
||||||
limit: Number.isFinite(limit) ? limit : 80,
|
const rawPage = Number(searchParams.get("page") || 1);
|
||||||
});
|
const page = Number.isFinite(rawPage) ? Math.max(1, Math.floor(rawPage)) : 1;
|
||||||
|
const filters = [];
|
||||||
return Response.json({ events });
|
if (modelId) filters.push(eq(aiModelHealthEvent.modelId, modelId));
|
||||||
|
if (providerId) filters.push(eq(aiModelHealthEvent.providerId, providerId));
|
||||||
|
if (searchParams.get("eventType")) filters.push(eq(aiModelHealthEvent.eventType, searchParams.get("eventType")!));
|
||||||
|
const start = searchParams.get("startDate");
|
||||||
|
const end = searchParams.get("endDate");
|
||||||
|
if (start && /^\d{4}-\d{2}-\d{2}$/.test(start)) filters.push(sql`${aiModelHealthEvent.createdAt}>=${start}::date`);
|
||||||
|
if (end && /^\d{4}-\d{2}-\d{2}$/.test(end)) filters.push(sql`${aiModelHealthEvent.createdAt}<${end}::date+interval '1 day'`);
|
||||||
|
const events = await db.select({
|
||||||
|
id: aiModelHealthEvent.id, modelId: aiModelHealthEvent.modelId, modelDisplayName: aiModelHealthEvent.modelDisplayName,
|
||||||
|
modelRuntimeId: aiModelHealthEvent.modelRuntimeId, providerName: aiModelHealthEvent.providerName,
|
||||||
|
providerId: aiModelHealthEvent.providerId, eventType: aiModelHealthEvent.eventType,
|
||||||
|
errorMessage: aiModelHealthEvent.errorMessage, elapsedMs: aiModelHealthEvent.elapsedMs,
|
||||||
|
consecutiveFailures: aiModelHealthEvent.consecutiveFailures, autoDisabled: aiModelHealthEvent.autoDisabled,
|
||||||
|
counted: aiModelHealthEvent.counted, createdAt: aiModelHealthEvent.createdAt,
|
||||||
|
}).from(aiModelHealthEvent).where(and(...filters)).orderBy(desc(aiModelHealthEvent.createdAt), desc(aiModelHealthEvent.id))
|
||||||
|
.limit(limit + 1).offset((page - 1) * limit);
|
||||||
|
return Response.json({ events: events.slice(0, limit), hasMore: events.length > limit, page });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof ChatSDKError) {
|
if (error instanceof ChatSDKError) {
|
||||||
return error.toResponse();
|
return error.toResponse();
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { db } from "@/lib/db";
|
||||||
|
import { aiModel, aiProvider } from "@/lib/db/schema";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
import { requireAdmin } from "@/lib/middleware/admin";
|
import { requireAdmin } from "@/lib/middleware/admin";
|
||||||
import {
|
import {
|
||||||
getAllAiModels,
|
|
||||||
getAiModelById,
|
getAiModelById,
|
||||||
getAiModelsByProviderId,
|
|
||||||
createAiModel,
|
createAiModel,
|
||||||
updateAiModel,
|
updateAiModel,
|
||||||
deleteAiModel,
|
deleteAiModel,
|
||||||
@ -154,8 +155,14 @@ export async function GET(request: Request) {
|
|||||||
const id = searchParams.get("id");
|
const id = searchParams.get("id");
|
||||||
const providerId = searchParams.get("providerId");
|
const providerId = searchParams.get("providerId");
|
||||||
|
|
||||||
|
if (searchParams.get("view") === "options") {
|
||||||
|
const models = await db.select({ id: aiModel.id, modelId: aiModel.modelId, displayName: aiModel.displayName,
|
||||||
|
providerName: aiProvider.name }).from(aiModel).leftJoin(aiProvider, eq(aiModel.providerId, aiProvider.id));
|
||||||
|
return Response.json({ models: sortModelsForDisplay(models) });
|
||||||
|
}
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
const model = await getAiModelById({ id });
|
const model = await db.query.aiModel.findFirst({ where: eq(aiModel.id, id) });
|
||||||
if (!model) {
|
if (!model) {
|
||||||
return new ChatSDKError("bad_request:api", "Model not found").toResponse();
|
return new ChatSDKError("bad_request:api", "Model not found").toResponse();
|
||||||
}
|
}
|
||||||
@ -163,12 +170,19 @@ export async function GET(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (providerId) {
|
if (providerId) {
|
||||||
const models = await getAiModelsByProviderId({ providerId });
|
const models = await db.select().from(aiModel).where(eq(aiModel.providerId, providerId));
|
||||||
return Response.json({ models: sortModelsForDisplay(models.map(normalizeModel)) });
|
return Response.json({ models: sortModelsForDisplay(models.map(normalizeModel)) });
|
||||||
}
|
}
|
||||||
|
|
||||||
const models = await getAllAiModels();
|
const models = await db.select().from(aiModel);
|
||||||
return Response.json({ models: sortModelsForDisplay(models.map(normalizeModel)) });
|
const normalized = models.map(normalizeModel);
|
||||||
|
const output = searchParams.get("view") === "summary" ? normalized.map(model => {
|
||||||
|
const { capabilities, defaultParams, imageGenConfig, description, ...summary } = model;
|
||||||
|
const fields = ["kind", "mediaType", "modelType", "outputType", "supportedModes"];
|
||||||
|
return { ...summary, healthLastError: summary.healthLastError?.slice(0, 1200),
|
||||||
|
capabilities: Object.fromEntries(fields.filter(key => capabilities?.[key] !== undefined).map(key => [key, capabilities[key]])) };
|
||||||
|
}) : normalized;
|
||||||
|
return Response.json({ models: sortModelsForDisplay(output) });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof ChatSDKError) {
|
if (error instanceof ChatSDKError) {
|
||||||
return error.toResponse();
|
return error.toResponse();
|
||||||
|
|||||||
@ -1,237 +1,31 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { chat, generatedImage, message, user } from "@/lib/db/schema";
|
import { generatedImage, user } from "@/lib/db/schema";
|
||||||
import { eq, desc, sql, and, gte, lte, like, or, isNotNull } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import { requireAdmin } from "@/lib/middleware/admin";
|
||||||
function extractVideoUrlFromParts(parts: unknown): string | null {
|
import { ChatSDKError } from "@/lib/errors";
|
||||||
const text = Array.isArray(parts)
|
import { mediaRecordsQuery, parseMediaQuery } from "@/lib/admin/media-records";
|
||||||
? parts
|
import { resolveArchivedImageUrls } from "@/lib/storage/image-archive-db";
|
||||||
.map((part) => (part && typeof part === "object" && "text" in part ? String((part as any).text || "") : ""))
|
|
||||||
.join("\n")
|
|
||||||
: typeof parts === "string"
|
|
||||||
? parts
|
|
||||||
: "";
|
|
||||||
|
|
||||||
if (!text) return null;
|
|
||||||
|
|
||||||
const htmlMatch = text.match(/<video[^>]+src="([^"]+)"/i);
|
|
||||||
if (htmlMatch?.[1]) return htmlMatch[1];
|
|
||||||
|
|
||||||
const markdownMatch = text.match(/\[[^\]]*]\((https?:\/\/[^)\s]+\.mp4(?:\?[^)\s]*)?)\)/i);
|
|
||||||
if (markdownMatch?.[1]) return markdownMatch[1];
|
|
||||||
|
|
||||||
const plainMatch = text.match(/(https?:\/\/[^\s"'<>]+\.mp4(?:\?[^\s"'<>]+)?)/i);
|
|
||||||
return plainMatch?.[1] || null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const session = await auth();
|
const session = await requireAdmin();
|
||||||
|
const operator = await db.query.user.findFirst({ where: eq(user.id, session.user.id) });
|
||||||
if (!session?.user?.id) {
|
if (operator?.role !== "admin") return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
const query = parseMediaQuery(request.nextUrl.searchParams);
|
||||||
|
if (query.startDate && query.endDate && query.startDate > query.endDate) {
|
||||||
|
return NextResponse.json({ error: "开始日期不能晚于结束日期" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
const [result] = await db.execute(mediaRecordsQuery(query));
|
||||||
// 检查是否为管理员
|
const images = await resolveArchivedImageUrls(result.records);
|
||||||
const currentUser = await db.query.user.findFirst({
|
const total = Number(result.total);
|
||||||
where: eq(user.id, session.user.id),
|
return NextResponse.json({ images, pagination: { page: query.page, pageSize: query.pageSize, total,
|
||||||
});
|
totalPages: Math.ceil(total / query.pageSize), totalImages: Number(result.totalImages), totalVideos: Number(result.totalVideos) } });
|
||||||
|
|
||||||
if (currentUser?.role !== "admin") {
|
|
||||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取查询参数
|
|
||||||
const searchParams = request.nextUrl.searchParams;
|
|
||||||
const page = Number.parseInt(searchParams.get("page") || "1");
|
|
||||||
const pageSize = Number.parseInt(searchParams.get("pageSize") || "200");
|
|
||||||
const offset = (page - 1) * pageSize;
|
|
||||||
const search = searchParams.get("search") || "";
|
|
||||||
const startDate = searchParams.get("startDate");
|
|
||||||
const endDate = searchParams.get("endDate");
|
|
||||||
|
|
||||||
// 构建查询条件
|
|
||||||
const imageConditions = [];
|
|
||||||
const videoConditions = [
|
|
||||||
eq(message.role, "assistant"),
|
|
||||||
or(
|
|
||||||
isNotNull(message.temporaryVideoUrl),
|
|
||||||
isNotNull(message.permanentVideoUrl),
|
|
||||||
sql`${message.parts}::text ILIKE '%<video%'`,
|
|
||||||
sql`${message.parts}::text ILIKE '%.mp4%'`
|
|
||||||
),
|
|
||||||
];
|
|
||||||
if (search) {
|
|
||||||
// 搜索用户名或邮箱
|
|
||||||
const matchingUsers = await db
|
|
||||||
.select({ id: user.id })
|
|
||||||
.from(user)
|
|
||||||
.where(or(
|
|
||||||
like(user.name, `%${search}%`),
|
|
||||||
like(user.email, `%${search}%`)
|
|
||||||
));
|
|
||||||
|
|
||||||
if (matchingUsers.length > 0) {
|
|
||||||
const matchedUserIds = matchingUsers.map((u) => u.id);
|
|
||||||
imageConditions.push(
|
|
||||||
sql`${generatedImage.userId} IN (${sql.join(matchedUserIds.map((id) => sql`${id}`), sql`, `)})`
|
|
||||||
);
|
|
||||||
videoConditions.push(
|
|
||||||
sql`${chat.userId} IN (${sql.join(matchedUserIds.map((id) => sql`${id}`), sql`, `)})`
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// 没有匹配的用户,返回空结果
|
|
||||||
return NextResponse.json({
|
|
||||||
images: [],
|
|
||||||
pagination: { page, pageSize, total: 0, totalPages: 0 }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (startDate) {
|
|
||||||
const start = new Date(startDate);
|
|
||||||
imageConditions.push(gte(generatedImage.createdAt, start));
|
|
||||||
videoConditions.push(gte(message.createdAt, start));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (endDate) {
|
|
||||||
const end = new Date(endDate);
|
|
||||||
end.setHours(23, 59, 59, 999);
|
|
||||||
imageConditions.push(lte(generatedImage.createdAt, end));
|
|
||||||
videoConditions.push(lte(message.createdAt, end));
|
|
||||||
}
|
|
||||||
|
|
||||||
const imageWhereClause = imageConditions.length > 0 ? and(...imageConditions) : undefined;
|
|
||||||
const videoWhereClause = videoConditions.length > 0 ? and(...videoConditions) : undefined;
|
|
||||||
|
|
||||||
// 查询图片总数
|
|
||||||
const imageCountResult = await db
|
|
||||||
.select({ count: sql<number>`count(*)::int` })
|
|
||||||
.from(generatedImage)
|
|
||||||
.where(imageWhereClause);
|
|
||||||
|
|
||||||
// 查询视频总数
|
|
||||||
const videoCountResult = await db
|
|
||||||
.select({ count: sql<number>`count(*)::int` })
|
|
||||||
.from(message)
|
|
||||||
.innerJoin(chat, eq(message.chatId, chat.id))
|
|
||||||
.where(videoWhereClause);
|
|
||||||
|
|
||||||
const totalImages = imageCountResult[0]?.count || 0;
|
|
||||||
const totalVideos = videoCountResult[0]?.count || 0;
|
|
||||||
const total = totalImages + totalVideos;
|
|
||||||
const totalPages = Math.ceil(total / pageSize);
|
|
||||||
|
|
||||||
const fetchLimit = Math.max(page * pageSize, pageSize);
|
|
||||||
|
|
||||||
// 查询图片列表(包含用户信息)
|
|
||||||
const imageRows = await db
|
|
||||||
.select({
|
|
||||||
id: generatedImage.id,
|
|
||||||
userId: generatedImage.userId,
|
|
||||||
userName: user.name,
|
|
||||||
userEmail: user.email,
|
|
||||||
userDepartment: user.department,
|
|
||||||
thumbnailUrl: generatedImage.thumbnailUrl,
|
|
||||||
originalUrl: generatedImage.originalUrl,
|
|
||||||
imageSize: generatedImage.imageSize,
|
|
||||||
prompt: generatedImage.prompt,
|
|
||||||
modelId: generatedImage.modelId,
|
|
||||||
isFeatured: generatedImage.isFeatured,
|
|
||||||
featuredCategory: generatedImage.featuredCategory,
|
|
||||||
featuredAt: generatedImage.featuredAt,
|
|
||||||
createdAt: generatedImage.createdAt,
|
|
||||||
chatId: generatedImage.chatId,
|
|
||||||
recordType: sql<"image">`'image'`,
|
|
||||||
videoUrl: sql<string | null>`null`,
|
|
||||||
})
|
|
||||||
.from(generatedImage)
|
|
||||||
.leftJoin(user, eq(generatedImage.userId, user.id))
|
|
||||||
.where(imageWhereClause)
|
|
||||||
.orderBy(desc(generatedImage.createdAt))
|
|
||||||
.limit(fetchLimit);
|
|
||||||
|
|
||||||
// 查询视频列表(来自消息表)
|
|
||||||
const videoRows = await db
|
|
||||||
.select({
|
|
||||||
id: message.id,
|
|
||||||
userId: chat.userId,
|
|
||||||
userName: user.name,
|
|
||||||
userEmail: user.email,
|
|
||||||
userDepartment: user.department,
|
|
||||||
temporaryVideoUrl: message.temporaryVideoUrl,
|
|
||||||
permanentVideoUrl: message.permanentVideoUrl,
|
|
||||||
parts: message.parts,
|
|
||||||
videoMetadata: message.videoMetadata,
|
|
||||||
createdAt: message.createdAt,
|
|
||||||
chatId: message.chatId,
|
|
||||||
})
|
|
||||||
.from(message)
|
|
||||||
.innerJoin(chat, eq(message.chatId, chat.id))
|
|
||||||
.leftJoin(user, eq(chat.userId, user.id))
|
|
||||||
.where(videoWhereClause)
|
|
||||||
.orderBy(desc(message.createdAt))
|
|
||||||
.limit(fetchLimit);
|
|
||||||
|
|
||||||
const normalizedVideos = videoRows
|
|
||||||
.map((item) => {
|
|
||||||
const metadata = (item.videoMetadata as Record<string, any> | null) || {};
|
|
||||||
const videoUrl =
|
|
||||||
item.permanentVideoUrl ||
|
|
||||||
item.temporaryVideoUrl ||
|
|
||||||
extractVideoUrlFromParts(item.parts);
|
|
||||||
|
|
||||||
if (!videoUrl) return null;
|
|
||||||
|
|
||||||
const resolution =
|
|
||||||
typeof metadata.resolution === "string" && metadata.resolution.trim()
|
|
||||||
? metadata.resolution.trim()
|
|
||||||
: null;
|
|
||||||
const prompt =
|
|
||||||
typeof metadata.prompt === "string" && metadata.prompt.trim()
|
|
||||||
? metadata.prompt.trim()
|
|
||||||
: null;
|
|
||||||
const modelId =
|
|
||||||
typeof metadata.model === "string" && metadata.model.trim()
|
|
||||||
? metadata.model.trim()
|
|
||||||
: null;
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: `video-${item.id}`,
|
|
||||||
sourceMessageId: item.id,
|
|
||||||
userId: item.userId,
|
|
||||||
userName: item.userName,
|
|
||||||
userEmail: item.userEmail,
|
|
||||||
userDepartment: item.userDepartment,
|
|
||||||
thumbnailUrl: videoUrl,
|
|
||||||
originalUrl: videoUrl,
|
|
||||||
videoUrl,
|
|
||||||
imageSize: resolution,
|
|
||||||
prompt,
|
|
||||||
modelId,
|
|
||||||
isFeatured: false,
|
|
||||||
featuredCategory: null,
|
|
||||||
featuredAt: null,
|
|
||||||
createdAt: item.createdAt,
|
|
||||||
chatId: item.chatId,
|
|
||||||
recordType: "video" as const,
|
|
||||||
};
|
|
||||||
})
|
|
||||||
.filter((item): item is NonNullable<typeof item> => Boolean(item));
|
|
||||||
|
|
||||||
const merged = [...imageRows, ...normalizedVideos].sort(
|
|
||||||
(a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
|
|
||||||
);
|
|
||||||
const images = merged.slice(offset, offset + pageSize);
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
images,
|
|
||||||
pagination: { page, pageSize, total, totalPages, totalImages, totalVideos }
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[Admin Images API] Error:", error);
|
if (error instanceof ChatSDKError) return error.toResponse();
|
||||||
return NextResponse.json({ error: "Failed to fetch images" }, { status: 500 });
|
console.error("[Admin Images API] GET failed", error);
|
||||||
|
return NextResponse.json({ error: "记录加载失败,请重试" }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
31
app/api/admin/media-tasks/route.ts
Normal file
31
app/api/admin/media-tasks/route.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { db } from "@/lib/db";
|
||||||
|
import { sql } from "drizzle-orm";
|
||||||
|
import { requireAdmin } from "@/lib/middleware/admin";
|
||||||
|
import { ChatSDKError } from "@/lib/errors";
|
||||||
|
import { parseMediaQuery } from "@/lib/admin/media-records";
|
||||||
|
|
||||||
|
export async function GET(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
await requireAdmin();
|
||||||
|
const q = parseMediaQuery(request.nextUrl.searchParams);
|
||||||
|
const status = request.nextUrl.searchParams.get("status") || "all";
|
||||||
|
const filters = [sql`true`];
|
||||||
|
if (q.search)
|
||||||
|
filters.push(sql`(u.name ilike ${`%${q.search}%`} or u.email ilike ${`%${q.search}%`})`);
|
||||||
|
if (["pending", "running", "failed", "succeeded"].includes(status))
|
||||||
|
filters.push(sql`t.status=${status}`);
|
||||||
|
if (q.startDate) filters.push(sql`t."createdAt">=${q.startDate}::date - interval '8 hours'`);
|
||||||
|
if (q.endDate) filters.push(sql`t."createdAt"<${q.endDate}::date+interval '16 hours'`);
|
||||||
|
const [result] = await db.execute(sql`with filtered as not materialized (
|
||||||
|
select t.id,u.name as "userName",t.status,t."createdAt"+interval '8 hours' as "createdAt",t.error,coalesce(t.request->>'modelId',t.request->>'model') as model,
|
||||||
|
extract(epoch from (t."completedAt"-t."startedAt"))*1000 as "elapsedMs"
|
||||||
|
from "CanvasImageTask" t left join "User" u on u.id=t."userId" where ${sql.join(filters, sql` and `)}
|
||||||
|
) select (select count(*)::int from filtered) as total,
|
||||||
|
coalesce((select jsonb_agg(p) from (select * from filtered order by "createdAt" desc,id desc limit ${q.pageSize} offset ${(q.page - 1) * q.pageSize}) p),'[]'::jsonb) as tasks`);
|
||||||
|
return NextResponse.json(result);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof ChatSDKError) return error.toResponse();
|
||||||
|
return NextResponse.json({ error: "任务记录加载失败,请重试" }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -106,6 +106,22 @@ export async function POST(request: Request) {
|
|||||||
? catalogPresets.filter((preset) => existingModelIds.has(preset.modelId))
|
? catalogPresets.filter((preset) => existingModelIds.has(preset.modelId))
|
||||||
: catalogPresets;
|
: catalogPresets;
|
||||||
|
|
||||||
|
if (body?.dryRun === true) {
|
||||||
|
const catalogIds = new Set(catalogPresets.map(preset => preset.modelId));
|
||||||
|
const fields = ["displayName", "description", "supportsVision", "supportsImageGen", "isHidden", "sortOrder", "capabilities", "defaultParams", "imageGenConfig"] as const;
|
||||||
|
const added = presets.filter(preset => !existingModelIds.has(preset.modelId)).map(preset => ({ modelId: preset.modelId, displayName: preset.displayName }));
|
||||||
|
const updated = presets.flatMap(preset => {
|
||||||
|
const existing = existingModels.find(model => model.modelId === preset.modelId);
|
||||||
|
if (!existing) return [];
|
||||||
|
const changedFields: string[] = fields.filter(field => preset[field] !== undefined && JSON.stringify(existing[field]) !== JSON.stringify(preset[field]));
|
||||||
|
if (existing.avatarUrl) changedFields.push("avatarUrl");
|
||||||
|
if (requestedModelIds !== null && !existing.isEnabled) changedFields.push("isEnabled");
|
||||||
|
return changedFields.length ? [{ modelId: preset.modelId, displayName: existing.displayName, changedFields }] : [];
|
||||||
|
});
|
||||||
|
const disabled = requestedModelIds === null ? existingModels.filter(model => !catalogIds.has(model.modelId) && (model.isEnabled || !model.isHidden)).map(model => ({ modelId: model.modelId, displayName: model.displayName })) : [];
|
||||||
|
return NextResponse.json({ preview: { added, updated, disabled }, total: catalogPresets.length });
|
||||||
|
}
|
||||||
|
|
||||||
const addedModels: AiModel[] = [];
|
const addedModels: AiModel[] = [];
|
||||||
const updatedModels: AiModel[] = [];
|
const updatedModels: AiModel[] = [];
|
||||||
const disabledModels: AiModel[] = [];
|
const disabledModels: AiModel[] = [];
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { desc, eq } from "drizzle-orm";
|
import { and, desc, eq } from "drizzle-orm";
|
||||||
|
import { z } from "zod";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { canvasProject } from "@/lib/db/schema";
|
import { canvasProject } from "@/lib/db/schema";
|
||||||
@ -56,14 +57,20 @@ export async function POST(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const body = await request.json().catch(() => ({}));
|
const body = await request.json().catch(() => ({}));
|
||||||
|
const requestedId = body.id === undefined ? undefined : z.string().uuid().safeParse(body.id);
|
||||||
|
if (requestedId && !requestedId.success) {
|
||||||
|
return NextResponse.json({ success: false, error: "项目编号无效" }, { status: 400 });
|
||||||
|
}
|
||||||
|
const id = requestedId?.data;
|
||||||
const title = String(body.title || "").trim() || "未命名生图项目";
|
const title = String(body.title || "").trim() || "未命名生图项目";
|
||||||
const description = String(body.description || "").trim() || null;
|
const description = String(body.description || "").trim() || null;
|
||||||
const thumbnail = String(body.thumbnail || "").trim() || null;
|
const thumbnail = String(body.thumbnail || "").trim() || null;
|
||||||
const projectState = normalizeAgentGenerationProjectPersistedState(body.projectState);
|
const projectState = normalizeAgentGenerationProjectPersistedState(body.projectState);
|
||||||
|
|
||||||
const [project] = await db
|
const [insertedProject] = await db
|
||||||
.insert(canvasProject)
|
.insert(canvasProject)
|
||||||
.values({
|
.values({
|
||||||
|
id,
|
||||||
userId: session.user.id,
|
userId: session.user.id,
|
||||||
title,
|
title,
|
||||||
description,
|
description,
|
||||||
@ -78,8 +85,22 @@ export async function POST(request: NextRequest) {
|
|||||||
agentProjectState: projectState,
|
agentProjectState: projectState,
|
||||||
} as any,
|
} as any,
|
||||||
})
|
})
|
||||||
|
.onConflictDoNothing({ target: canvasProject.id })
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
|
const project = insertedProject || (id ? (await db
|
||||||
|
.select()
|
||||||
|
.from(canvasProject)
|
||||||
|
.where(and(
|
||||||
|
eq(canvasProject.id, id),
|
||||||
|
eq(canvasProject.userId, session.user.id),
|
||||||
|
eq(canvasProject.workspaceType, "agent")
|
||||||
|
))
|
||||||
|
.limit(1))[0] : undefined);
|
||||||
|
if (!project) {
|
||||||
|
return NextResponse.json({ success: false, error: "项目编号不可用,请重试" }, { status: 409 });
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json({ success: true, data: mapAgentProjectRow(project) });
|
return NextResponse.json({ success: true, data: mapAgentProjectRow(project) });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[AgentProjects API] 创建项目失败:", error);
|
console.error("[AgentProjects API] 创建项目失败:", error);
|
||||||
|
|||||||
@ -450,7 +450,7 @@ async function analyzeFunctionalShowcaseImage(params: {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
temperature: 0,
|
temperature: 0,
|
||||||
maxTokens: 120,
|
maxOutputTokens: 120,
|
||||||
abortSignal: AbortSignal.timeout(25_000),
|
abortSignal: AbortSignal.timeout(25_000),
|
||||||
} as any);
|
} as any);
|
||||||
|
|
||||||
|
|||||||
20
app/api/agent-workflow/template-prompts/route.ts
Normal file
20
app/api/agent-workflow/template-prompts/route.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import { resolveAgentWorkflowComposerSelections } from "@/lib/agents/workflow-composer";
|
||||||
|
import { AGENT_WORKFLOW_UI_CONFIG_KEY, normalizeAgentWorkflowUiConfig } from "@/lib/agents/workflow-ui-config";
|
||||||
|
import { getSystemSetting } from "@/lib/db/queries";
|
||||||
|
|
||||||
|
const selectionsSchema = z.record(z.string().max(160), z.string().max(8192))
|
||||||
|
.refine((selections) => Object.keys(selections).length <= 100);
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) return Response.json({ error: "请先登录" }, { status: 401 });
|
||||||
|
|
||||||
|
const selections = selectionsSchema.safeParse(await request.json().catch(() => null));
|
||||||
|
if (!selections.success) return Response.json({ error: "风格模板选择无效" }, { status: 400 });
|
||||||
|
|
||||||
|
const setting = await getSystemSetting({ key: AGENT_WORKFLOW_UI_CONFIG_KEY });
|
||||||
|
const config = normalizeAgentWorkflowUiConfig(setting?.value);
|
||||||
|
return Response.json({ selections: resolveAgentWorkflowComposerSelections({ config, selections: selections.data }) });
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ import { auth } from "@/app/(auth)/auth";
|
|||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { canvasImageTask, canvasProject, message } from "@/lib/db/schema";
|
import { canvasImageTask, canvasProject, message } from "@/lib/db/schema";
|
||||||
import { extractAgentProjectStateFromCanvasData, isAgentProjectRow } from "@/lib/agents/agent-project-compat";
|
import { extractAgentProjectStateFromCanvasData, isAgentProjectRow } from "@/lib/agents/agent-project-compat";
|
||||||
|
import { MAX_DETAIL_REPLACEMENT_REFERENCES } from "@/lib/agents/mode-workspace-state";
|
||||||
import {
|
import {
|
||||||
createCanvasImageTask,
|
createCanvasImageTask,
|
||||||
enqueueCanvasImageTask,
|
enqueueCanvasImageTask,
|
||||||
@ -146,6 +147,14 @@ export async function POST(request: Request) {
|
|||||||
.map((item) => [String(item.id), String(item.imageUrl)]),
|
.map((item) => [String(item.id), String(item.imageUrl)]),
|
||||||
);
|
);
|
||||||
const requested = Array.isArray(body.references) ? body.references : [];
|
const requested = Array.isArray(body.references) ? body.references : [];
|
||||||
|
if (requested.length === 0) {
|
||||||
|
return NextResponse.json({ error: "请先选择详情参考图" }, { status: 400 });
|
||||||
|
}
|
||||||
|
if (requested.length > MAX_DETAIL_REPLACEMENT_REFERENCES) {
|
||||||
|
return NextResponse.json({
|
||||||
|
error: `本次选中了 ${requested.length} 张详情参考图,一次最多替换 ${MAX_DETAIL_REPLACEMENT_REFERENCES} 张,请减少本次选择后重试`,
|
||||||
|
}, { status: 400 });
|
||||||
|
}
|
||||||
const references: DetailReplacementBatchReference[] = requested.map((raw: unknown) => {
|
const references: DetailReplacementBatchReference[] = requested.map((raw: unknown) => {
|
||||||
const item = raw && typeof raw === "object" ? raw as Record<string, unknown> : {};
|
const item = raw && typeof raw === "object" ? raw as Record<string, unknown> : {};
|
||||||
return {
|
return {
|
||||||
@ -160,7 +169,6 @@ export async function POST(request: Request) {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
if (
|
if (
|
||||||
references.length === 0 || references.length > 40 ||
|
|
||||||
new Set(references.map((item) => item.id)).size !== references.length ||
|
new Set(references.map((item) => item.id)).size !== references.length ||
|
||||||
references.some((item) => !item.id || knownReferences.get(item.id) !== item.imageUrl)
|
references.some((item) => !item.id || knownReferences.get(item.id) !== item.imageUrl)
|
||||||
) {
|
) {
|
||||||
@ -187,6 +195,12 @@ export async function POST(request: Request) {
|
|||||||
if (!model || model === "auto" || !models.some((entry) => entry.model.id === model)) {
|
if (!model || model === "auto" || !models.some((entry) => entry.model.id === model)) {
|
||||||
return NextResponse.json({ error: "详情替换模型不可用,请检查模型配置" }, { status: 400 });
|
return NextResponse.json({ error: "详情替换模型不可用,请检查模型配置" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
if (typeof body.expectedModelId === "string" && body.expectedModelId !== model) {
|
||||||
|
return NextResponse.json({
|
||||||
|
code: "DETAIL_REPLACEMENT_MODEL_CHANGED",
|
||||||
|
error: "详情替换模型已更新,请确认页面显示的模型后重新提交",
|
||||||
|
}, { status: 409 });
|
||||||
|
}
|
||||||
const quota = await checkUserImageQuota(session.user.id);
|
const quota = await checkUserImageQuota(session.user.id);
|
||||||
if (!quota.allowed) {
|
if (!quota.allowed) {
|
||||||
return NextResponse.json({ error: "本月图片生成配额已用完" }, { status: 429 });
|
return NextResponse.json({ error: "本月图片生成配额已用完" }, { status: 429 });
|
||||||
|
|||||||
8
app/api/app-version/route.ts
Normal file
8
app/api/app-version/route.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
export function GET() {
|
||||||
|
return NextResponse.json({ version: process.env.NEXT_PUBLIC_APP_RELEASE || "development" }, {
|
||||||
|
headers: { "Cache-Control": "no-store, max-age=0" },
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { buildThreeAssetUrl } from "@/lib/canvas/three-asset-url";
|
import { buildThreeAssetUrl } from "@/lib/canvas/three-asset-url";
|
||||||
|
|
||||||
@ -12,7 +13,6 @@ const ALLOWED_MODEL_HOST_DOMAINS = [
|
|||||||
"tripo3d.ai",
|
"tripo3d.ai",
|
||||||
"tripo3d.com",
|
"tripo3d.com",
|
||||||
"worldlabs.ai",
|
"worldlabs.ai",
|
||||||
"s3.1080p.hk",
|
|
||||||
"img.yydd.ai",
|
"img.yydd.ai",
|
||||||
"scqny.jiansun.vip",
|
"scqny.jiansun.vip",
|
||||||
"blob.vercel-storage.com",
|
"blob.vercel-storage.com",
|
||||||
@ -25,7 +25,6 @@ const ALLOWED_MODEL_HOST_DOMAINS = [
|
|||||||
|
|
||||||
const ALLOWED_HTTP_MODEL_HOST_DOMAINS = [
|
const ALLOWED_HTTP_MODEL_HOST_DOMAINS = [
|
||||||
"bbyy.ai",
|
"bbyy.ai",
|
||||||
"s3.1080p.hk",
|
|
||||||
"img.yydd.ai",
|
"img.yydd.ai",
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -165,6 +164,9 @@ async function handleRequest(request: Request, method: "GET" | "HEAD") {
|
|||||||
|
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const rawUrl = String(searchParams.get("url") || "").trim();
|
const rawUrl = String(searchParams.get("url") || "").trim();
|
||||||
|
if (isRetiredStorageUrl(rawUrl)) {
|
||||||
|
return NextResponse.json({ success: false, error: RETIRED_STORAGE_MESSAGE, code: "storage_retired" }, { status: 410 });
|
||||||
|
}
|
||||||
if (!rawUrl) {
|
if (!rawUrl) {
|
||||||
return NextResponse.json({ success: false, error: "缺少 url 参数" }, { status: 400 });
|
return NextResponse.json({ success: false, error: "缺少 url 参数" }, { status: 400 });
|
||||||
}
|
}
|
||||||
@ -180,6 +182,18 @@ async function handleRequest(request: Request, method: "GET" | "HEAD") {
|
|||||||
return NextResponse.json({ success: false, error: "仅支持 http/https 模型地址" }, { status: 400 });
|
return NextResponse.json({ success: false, error: "仅支持 http/https 模型地址" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (normalizeLegacyAliyunStorageUrl(targetUrl.toString())) {
|
||||||
|
try {
|
||||||
|
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
||||||
|
targetUrl = new URL(await resolveLegacyAliyunStorageUrl(targetUrl.toString()));
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Error && error.message.includes("HTTP 410")) {
|
||||||
|
return NextResponse.json({ success: false, error: RETIRED_STORAGE_MESSAGE, code: "storage_retired" }, { status: 410 });
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!isAllowedModelHost(targetUrl.hostname)) {
|
if (!isAllowedModelHost(targetUrl.hostname)) {
|
||||||
return NextResponse.json({ success: false, error: "不允许的模型地址域名" }, { status: 403 });
|
return NextResponse.json({ success: false, error: "不允许的模型地址域名" }, { status: 403 });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,12 +2,14 @@ import { NextResponse } from "next/server";
|
|||||||
import { ZodError } from "zod";
|
import { ZodError } from "zod";
|
||||||
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import { RmbgServiceError } from "@/lib/ai/rmbg/service";
|
||||||
import { canvasCapabilityClientCommandSchema } from "@/lib/canvas/capabilities/client-command";
|
import { canvasCapabilityClientCommandSchema } from "@/lib/canvas/capabilities/client-command";
|
||||||
import { CanvasCapabilityRuntimeError } from "@/lib/canvas/capabilities/context";
|
import { CanvasCapabilityRuntimeError } from "@/lib/canvas/capabilities/context";
|
||||||
import { getCanvasCapabilityManifest } from "@/lib/canvas/capabilities/registry";
|
import { getCanvasCapabilityManifest } from "@/lib/canvas/capabilities/registry";
|
||||||
import { executeCanvasCapabilityRuntime } from "@/lib/canvas/capabilities/runtime";
|
import { executeCanvasCapabilityRuntime } from "@/lib/canvas/capabilities/runtime";
|
||||||
import { getCanvasCapabilityTask } from "@/lib/canvas/capabilities/task-store";
|
import { getCanvasCapabilityTask } from "@/lib/canvas/capabilities/task-store";
|
||||||
import { CanvasAgentMutationError } from "@/lib/canvas/agent-mutations";
|
import { CanvasAgentMutationError } from "@/lib/canvas/agent-mutations";
|
||||||
|
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export const maxDuration = 600;
|
export const maxDuration = 600;
|
||||||
@ -39,7 +41,8 @@ export async function GET(
|
|||||||
{ status: 404 }
|
{ status: 404 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return NextResponse.json({ success: true, task });
|
const failure = task.status === "failed" ? getRequestFailureDetails(task.error) : undefined;
|
||||||
|
return NextResponse.json({ success: true, task: { ...task, failure, error: failure ? formatRequestFailure(failure, task.requestId) : task.error } });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(
|
export async function POST(
|
||||||
@ -51,9 +54,11 @@ export async function POST(
|
|||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let requestId: string | undefined;
|
||||||
try {
|
try {
|
||||||
const { id } = await params;
|
const { id } = await params;
|
||||||
const command = canvasCapabilityClientCommandSchema.parse(await request.json());
|
const command = canvasCapabilityClientCommandSchema.parse(await request.json());
|
||||||
|
requestId = command.requestId;
|
||||||
const result = await executeCanvasCapabilityRuntime({
|
const result = await executeCanvasCapabilityRuntime({
|
||||||
command,
|
command,
|
||||||
projectId: id,
|
projectId: id,
|
||||||
@ -67,8 +72,9 @@ export async function POST(
|
|||||||
error instanceof CanvasCapabilityRuntimeError ||
|
error instanceof CanvasCapabilityRuntimeError ||
|
||||||
error instanceof CanvasAgentMutationError
|
error instanceof CanvasAgentMutationError
|
||||||
) {
|
) {
|
||||||
|
const failure = getRequestFailureDetails(error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, error: error.message, code: error.code },
|
{ success: false, error: formatRequestFailure(failure, requestId), code: error.code, failure, requestId },
|
||||||
{ status: error.status }
|
{ status: error.status }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -82,10 +88,10 @@ export async function POST(
|
|||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const message = error instanceof Error ? error.message : "执行画布能力失败";
|
const failure = getRequestFailureDetails(error, "执行画布能力失败");
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, error: message },
|
{ success: false, error: formatRequestFailure(failure, requestId), failure, requestId },
|
||||||
{ status: 500 }
|
{ status: error instanceof RmbgServiceError ? error.status : 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
app/api/canvas/[id]/feedback/route.ts
Normal file
12
app/api/canvas/[id]/feedback/route.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { feedbackGet, feedbackPost } from "@/lib/canvas/feedback-route";
|
||||||
|
|
||||||
|
export const runtime = "nodejs";
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||||
|
return feedbackPost(request, (await params).id);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||||
|
return feedbackGet(request, (await params).id);
|
||||||
|
}
|
||||||
@ -207,7 +207,11 @@ export async function PUT(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.returning();
|
.returning();
|
||||||
return NextResponse.json({ success: true, data: saved });
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
data: saved,
|
||||||
|
previousCanvasRevision: existingProject?.updatedAt.toISOString() ?? null,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("更新/创建画布失败:", error);
|
console.error("更新/创建画布失败:", error);
|
||||||
|
|||||||
40
app/api/canvas/agent-capabilities/route.ts
Normal file
40
app/api/canvas/agent-capabilities/route.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import { PROJECT_AGENT_OPERATIONS, projectOperationAuthorized } from "@/lib/ai/canvas-agent-runtime/project-operation-catalog";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { and, eq } from "drizzle-orm";
|
||||||
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import { db } from "@/lib/db";
|
||||||
|
import { canvasProject } from "@/lib/db/schema";
|
||||||
|
import { getSystemSetting } from "@/lib/db/queries";
|
||||||
|
import {
|
||||||
|
normalizeVideoCanvasFunctionConfig,
|
||||||
|
VIDEO_CANVAS_FUNCTION_CONFIG_KEY,
|
||||||
|
} from "@/lib/video-canvas/config";
|
||||||
|
import { getCanvasAgentAccessManifest } from "@/lib/ai/canvas-agent-runtime/capability-access";
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
|
const parsed = z.string().uuid().safeParse(new URL(request.url).searchParams.get("projectId"));
|
||||||
|
if (!parsed.success) return NextResponse.json({ error: "画布编号无效" }, { status: 400 });
|
||||||
|
const [project] = await db
|
||||||
|
.select({ id: canvasProject.id, workspace: canvasProject.workspaceType })
|
||||||
|
.from(canvasProject)
|
||||||
|
.where(and(eq(canvasProject.id, parsed.data), eq(canvasProject.userId, session.user.id)))
|
||||||
|
.limit(1);
|
||||||
|
if (!project) return NextResponse.json({ error: "画布不存在" }, { status: 404 });
|
||||||
|
const config = normalizeVideoCanvasFunctionConfig(
|
||||||
|
(await getSystemSetting({ key: VIDEO_CANVAS_FUNCTION_CONFIG_KEY }))?.value,
|
||||||
|
);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
workspace: project.workspace,
|
||||||
|
projectServices: PROJECT_AGENT_OPERATIONS.map(item => ({ ...item, authorized: projectOperationAuthorized(item) })),
|
||||||
|
utilityTools: ["getWeather", "createDocument", "updateDocument", "requestSuggestions", "loadProjectSkill"],
|
||||||
|
capabilities: getCanvasAgentAccessManifest(config).filter(
|
||||||
|
(item) => item.workspace === (project.workspace === "video" ? "video" : "canvas"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{ headers: { "Cache-Control": "private, no-store" } },
|
||||||
|
);
|
||||||
|
}
|
||||||
492
app/api/canvas/agent-chat/route.ts
Normal file
492
app/api/canvas/agent-chat/route.ts
Normal file
@ -0,0 +1,492 @@
|
|||||||
|
import { agentModelCandidates } from "@/lib/ai/runtime/agent-model-failover";
|
||||||
|
import { agentModelCircuitStore } from "@/lib/ai/runtime/agent-model-circuit-store";
|
||||||
|
import { createAgentVisionExecutor } from "@/lib/ai/runtime/agent-vision-failover";
|
||||||
|
import { canvasAgentProgress } from "@/lib/ai/canvas-agent-runtime/progress";
|
||||||
|
import { getStyleProImageModelEntries } from "@/lib/style-pro/model-options";
|
||||||
|
import { registerProjectAgentTools } from "@/lib/ai/canvas-agent-runtime/project-tools";
|
||||||
|
import { isZcodeWorkspaceAuthorized } from "@/lib/ai/canvas-agent-runtime/authorization";
|
||||||
|
import {
|
||||||
|
createUIMessageStream,
|
||||||
|
createUIMessageStreamResponse,
|
||||||
|
generateText,
|
||||||
|
} from "ai";
|
||||||
|
import { after, NextResponse } from "next/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { db } from "@/lib/db";
|
||||||
|
import { siteSettings, canvasProject } from "@/lib/db/schema";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import { postRequestBodySchema } from "@/app/(chat)/api/chat/schema";
|
||||||
|
import { entitlementsByUserType } from "@/lib/ai/entitlements";
|
||||||
|
import { getLanguageModelById } from "@/lib/ai/dynamic-provider";
|
||||||
|
import {
|
||||||
|
getAgentAiModelWithProvider,
|
||||||
|
getAccessibleAgentPresetById,
|
||||||
|
getEnabledAiModelsWithProvider,
|
||||||
|
getChatById,
|
||||||
|
getMessagesByChatId,
|
||||||
|
getMessageCountByUserId,
|
||||||
|
getSiteSettings,
|
||||||
|
saveChat,
|
||||||
|
saveMessages,
|
||||||
|
} from "@/lib/db/queries";
|
||||||
|
import { runZcodeCanvasTask } from "@/lib/ai/zcode-agent/task";
|
||||||
|
import { canvasAgentReplyText } from "@/lib/ai/zcode-agent/assistant-output";
|
||||||
|
import { createZcodeModelGateway } from "@/lib/ai/zcode-agent/model-gateway";
|
||||||
|
import { newAgentRunSnapshot, publicAgentError } from "@/lib/ai/canvas-agent-runtime/protocol";
|
||||||
|
import { shouldUseCanvasAgentRuntime } from "@/lib/ai/canvas-agent-runtime/routing";
|
||||||
|
import {
|
||||||
|
acquireAgentRun,
|
||||||
|
agentRunView,
|
||||||
|
AgentRunError,
|
||||||
|
checkpointAgentRun,
|
||||||
|
consumeAgentRunInputs,
|
||||||
|
heartbeatAgentRun,
|
||||||
|
} from "@/lib/ai/canvas-agent-runtime/store";
|
||||||
|
import { readOwnedCanvas } from "@/lib/ai/canvas-agent-runtime/canvas";
|
||||||
|
import { buildCanvasRuntimeTools } from "@/lib/ai/canvas-agent-runtime/tools";
|
||||||
|
import { canvasRuntimeSystemPrompt } from "@/lib/ai/canvas-agent-runtime/prompt";
|
||||||
|
import { verifyCanvasAgentResults } from "@/lib/ai/canvas-agent-runtime/review";
|
||||||
|
import { inferCanvasTaskRequirements } from "@/lib/ai/canvas-agent-runtime/requirements";
|
||||||
|
import {
|
||||||
|
resolveRuntimeConversationSources,
|
||||||
|
persistRuntimeConversationResults,
|
||||||
|
} from "@/lib/ai/canvas-agent-runtime/conversation";
|
||||||
|
import { getCanvasAgentFeatureFlagsFromSystemSettings } from "@/lib/canvas/agent-feature-settings";
|
||||||
|
import { canApplyCanvasAgentMutations } from "@/lib/canvas/agent-feature-flags";
|
||||||
|
|
||||||
|
import {
|
||||||
|
videoAgentSettingsSchema,
|
||||||
|
inferVideoTaskRequirements,
|
||||||
|
videoNodeUrl,
|
||||||
|
videoRuntimeSystemPrompt,
|
||||||
|
} from "@/lib/ai/canvas-agent-runtime/video-protocol";
|
||||||
|
import { readOwnedVideoCanvas } from "@/lib/ai/canvas-agent-runtime/video-store";
|
||||||
|
import {
|
||||||
|
buildVideoRuntimeTools,
|
||||||
|
createVideoRuntimePorts,
|
||||||
|
} from "@/lib/ai/canvas-agent-runtime/video-tools";
|
||||||
|
import { probeVideoMetadata } from "@/lib/ai/canvas-agent-runtime/video-probe";
|
||||||
|
import { verifyVideoAgentResults } from "@/lib/ai/canvas-agent-runtime/video-review";
|
||||||
|
|
||||||
|
export const runtime = "nodejs";
|
||||||
|
export const maxDuration = 1200;
|
||||||
|
const requestSchema = postRequestBodySchema.extend({
|
||||||
|
resumeRunId: z.string().uuid().optional(),
|
||||||
|
selectedImageModel: z.string().min(1).max(180).optional(),
|
||||||
|
workspaceVariant: z.enum(["canvas", "video"]).default("canvas"),
|
||||||
|
videoAgentSettings: videoAgentSettingsSchema.optional(),
|
||||||
|
});
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
|
try {
|
||||||
|
const body = requestSchema.parse(await request.json());
|
||||||
|
if (!shouldUseCanvasAgentRuntime(body))
|
||||||
|
throw new AgentRunError("此请求不属于画布 Agent 任务", 400);
|
||||||
|
const projectId = body.contextPayload!.projectId!;
|
||||||
|
const [ownedProject] = await db.select({ userId: canvasProject.userId, workspaceType: canvasProject.workspaceType })
|
||||||
|
.from(canvasProject).where(eq(canvasProject.id, projectId)).limit(1);
|
||||||
|
if (!ownedProject || ownedProject.userId !== session.user.id) throw new AgentRunError("画布不存在", 404);
|
||||||
|
if (!isZcodeWorkspaceAuthorized(ownedProject.workspaceType)) throw new AgentRunError("视频画布暂未授权给 ZCode Agent,请使用手工功能。", 403);
|
||||||
|
const isVideo = ownedProject.workspaceType === "video";
|
||||||
|
const preset = body.agentPresetId ? await getAccessibleAgentPresetById({ id: body.agentPresetId, userId: session.user.id }) : null;
|
||||||
|
if (preset && String(preset.type) === "video") throw new AgentRunError("视频技能暂未授权给 ZCode Agent", 403);
|
||||||
|
if (body.agentPresetId && (!preset || preset.status !== "active")) throw new AgentRunError("此 Agent 预设不可用", 403);
|
||||||
|
const objective = body.message.parts
|
||||||
|
.filter((part) => part.type === "text")
|
||||||
|
.map((part) => part.text)
|
||||||
|
.join("\n")
|
||||||
|
.trim();
|
||||||
|
if (!objective) throw new AgentRunError("请描述需要完成的创作任务", 400);
|
||||||
|
const settings = await getSiteSettings();
|
||||||
|
if (
|
||||||
|
!settings.agentEnabled ||
|
||||||
|
!canApplyCanvasAgentMutations(await getCanvasAgentFeatureFlagsFromSystemSettings())
|
||||||
|
) {
|
||||||
|
throw new AgentRunError("管理员尚未开启画布 Agent 操作", 403);
|
||||||
|
}
|
||||||
|
const [usageSettings] = await db.select().from(siteSettings).limit(1);
|
||||||
|
if (usageSettings?.usageTimeEnabled) {
|
||||||
|
const time = new Intl.DateTimeFormat("en-GB", {
|
||||||
|
timeZone: "Asia/Shanghai",
|
||||||
|
hour: "2-digit",
|
||||||
|
minute: "2-digit",
|
||||||
|
hourCycle: "h23",
|
||||||
|
}).format(new Date());
|
||||||
|
if (
|
||||||
|
time < (usageSettings.usageTimeStart || "08:00") ||
|
||||||
|
time > (usageSettings.usageTimeEnd || "20:00")
|
||||||
|
)
|
||||||
|
throw new AgentRunError(usageSettings.usageTimeMessage || "当前不在系统使用时间段内", 403);
|
||||||
|
}
|
||||||
|
const count = await getMessageCountByUserId({ id: session.user.id, differenceInHours: 24 });
|
||||||
|
if (count > entitlementsByUserType[session.user.type].maxMessagesPerDay)
|
||||||
|
throw new AgentRunError("今日对话次数已达到限额", 429);
|
||||||
|
const imageCanvas = isVideo ? null : await readOwnedCanvas(session.user.id, projectId);
|
||||||
|
if (imageCanvas?.project.workspaceType === "video")
|
||||||
|
throw new AgentRunError("请使用视频画布 Agent 入口", 400);
|
||||||
|
const videoCanvas = isVideo ? await readOwnedVideoCanvas(session.user.id, projectId) : null;
|
||||||
|
const nodes = imageCanvas?.nodes || [];
|
||||||
|
const availableIds = isVideo
|
||||||
|
? videoCanvas!.nodes.map((node) => node.id)
|
||||||
|
: nodes.map((node) => node.id);
|
||||||
|
const selectedNodeIds = body.contextPayload?.selectedNodeIds || [];
|
||||||
|
if (selectedNodeIds.some((id) => !availableIds.includes(id))) {
|
||||||
|
throw new AgentRunError("选中图片尚未保存到画布,请等待保存完成后重试。");
|
||||||
|
}
|
||||||
|
const existingChat = await getChatById({ id: body.id });
|
||||||
|
if (existingChat && existingChat.userId !== session.user.id)
|
||||||
|
throw new AgentRunError("对话不存在", 404);
|
||||||
|
if (!existingChat)
|
||||||
|
await saveChat({
|
||||||
|
id: body.id,
|
||||||
|
userId: session.user.id,
|
||||||
|
title: objective.slice(0, 80),
|
||||||
|
visibility: "private",
|
||||||
|
});
|
||||||
|
const conversation = isVideo
|
||||||
|
? {
|
||||||
|
sourceNodeIds: selectedNodeIds,
|
||||||
|
targetReason: "video-selected-nodes",
|
||||||
|
previousContext: null,
|
||||||
|
}
|
||||||
|
: await resolveRuntimeConversationSources({
|
||||||
|
userId: session.user.id,
|
||||||
|
chatId: body.id,
|
||||||
|
projectId,
|
||||||
|
text: objective,
|
||||||
|
parts: body.message.parts,
|
||||||
|
nodes,
|
||||||
|
selectedNodeIds,
|
||||||
|
});
|
||||||
|
const candidates = agentModelCandidates(await getAgentAiModelWithProvider(), await getEnabledAiModelsWithProvider());
|
||||||
|
const configured = candidates[0];
|
||||||
|
if (!configured) throw new AgentRunError("请先在后台配置可用的多模态 Agent 对话模型", 503);
|
||||||
|
let model = await getLanguageModelById(configured.model.id);
|
||||||
|
for (const candidate of candidates.slice(1)) {
|
||||||
|
if (model) break;
|
||||||
|
model = await getLanguageModelById(candidate.model.id);
|
||||||
|
}
|
||||||
|
if (!model) throw new AgentRunError("Agent 模型暂不可用", 503);
|
||||||
|
const visionModel = model;
|
||||||
|
const executeVisionModel = createAgentVisionExecutor(candidates, agentModelCircuitStore, getLanguageModelById);
|
||||||
|
// Validate the user's image choice independently of the Agent planning model.
|
||||||
|
if (body.selectedImageModel) {
|
||||||
|
const images = await getStyleProImageModelEntries();
|
||||||
|
if (!images.some(({ model }) => model.id === body.selectedImageModel && model.healthStatus !== "auto_disabled"))
|
||||||
|
throw new AgentRunError("你选择的生图模型当前不可用,请重新选择;不会自动换模型生成。", 400);
|
||||||
|
}
|
||||||
|
const assistantMessageId = crypto.randomUUID();
|
||||||
|
const previousVideoMessages =
|
||||||
|
existingChat
|
||||||
|
? (await getMessagesByChatId({ id: body.id })).slice(-6).map((message) => ({
|
||||||
|
role: message.role as "user" | "assistant",
|
||||||
|
content: (message.parts as Array<{ type: string; text?: string }>)
|
||||||
|
.filter((part) => part.type === "text")
|
||||||
|
.map((part) => part.text || "")
|
||||||
|
.join("\n")
|
||||||
|
.slice(0, 4000),
|
||||||
|
}))
|
||||||
|
: [];
|
||||||
|
const initialSnapshot = newAgentRunSnapshot(objective, [
|
||||||
|
...previousVideoMessages,
|
||||||
|
{ role: "user", content: objective },
|
||||||
|
]);
|
||||||
|
initialSnapshot.workspace = isVideo ? "video" : "canvas";
|
||||||
|
if (isVideo) {
|
||||||
|
initialSnapshot.videoRequirements = inferVideoTaskRequirements(objective);
|
||||||
|
initialSnapshot.videoSettings = videoAgentSettingsSchema.parse({
|
||||||
|
...body.videoAgentSettings,
|
||||||
|
...(initialSnapshot.videoRequirements.duration
|
||||||
|
? { duration: initialSnapshot.videoRequirements.duration }
|
||||||
|
: {}),
|
||||||
|
...(initialSnapshot.videoRequirements.ratio
|
||||||
|
? { ratio: initialSnapshot.videoRequirements.ratio }
|
||||||
|
: {}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
initialSnapshot.sourceNodeIds = conversation.sourceNodeIds;
|
||||||
|
initialSnapshot.requirements = {
|
||||||
|
...inferCanvasTaskRequirements(objective, initialSnapshot.sourceNodeIds.length),
|
||||||
|
sourceCount: initialSnapshot.sourceNodeIds.length,
|
||||||
|
};
|
||||||
|
const run = await acquireAgentRun({
|
||||||
|
id: body.message.id,
|
||||||
|
userId: session.user.id,
|
||||||
|
projectId,
|
||||||
|
chatId: body.id,
|
||||||
|
assistantMessageId,
|
||||||
|
resumeRunId: body.resumeRunId,
|
||||||
|
snapshot: initialSnapshot,
|
||||||
|
});
|
||||||
|
const snapshot = run.snapshot;
|
||||||
|
snapshot.imagePreferences = {
|
||||||
|
...snapshot.imagePreferences,
|
||||||
|
// Each submitted turn captures the current controls, including switching back to Auto.
|
||||||
|
modelId: body.selectedImageModel,
|
||||||
|
parameters: body.imageParams || snapshot.imagePreferences?.parameters,
|
||||||
|
};
|
||||||
|
const taskSourceNodeIds = snapshot.sourceNodeIds ?? selectedNodeIds;
|
||||||
|
const context = {
|
||||||
|
recentConversation: previousVideoMessages,
|
||||||
|
preset: preset ? { name: preset.name, type: preset.type, promptTemplate: preset.promptTemplate, defaultModelId: preset.defaultModelId, defaultParams: preset.defaultParams, slots: preset.slots.map(slot => ({ title: slot.title, promptTemplate: slot.promptTemplate, defaultParams: slot.defaultParams })) } : null,
|
||||||
|
videoSettings: snapshot.videoSettings,
|
||||||
|
selectedNodeIds: taskSourceNodeIds,
|
||||||
|
nodes: nodes
|
||||||
|
.filter((node) => taskSourceNodeIds.includes(node.id))
|
||||||
|
.map((node) => ({
|
||||||
|
id: node.id,
|
||||||
|
type: node.type,
|
||||||
|
position: node.position,
|
||||||
|
size: node.size,
|
||||||
|
title: node.data.title,
|
||||||
|
})),
|
||||||
|
skillInstructions: body.hiddenPrompt || undefined,
|
||||||
|
imagePreferences: snapshot.imagePreferences,
|
||||||
|
targetReason: conversation.targetReason,
|
||||||
|
previousConversationContext: conversation.previousContext,
|
||||||
|
};
|
||||||
|
await saveMessages({
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
id: body.message.id,
|
||||||
|
chatId: body.id,
|
||||||
|
role: "user",
|
||||||
|
parts: body.message.parts,
|
||||||
|
attachments: [],
|
||||||
|
createdAt: new Date(),
|
||||||
|
videoStorageStatus: null,
|
||||||
|
temporaryVideoUrl: null,
|
||||||
|
permanentVideoUrl: null,
|
||||||
|
archivedAt: null,
|
||||||
|
videoMetadata: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
let archiveVideoResults: (() => Promise<void>) | undefined;
|
||||||
|
const stream = createUIMessageStream({
|
||||||
|
execute: async ({ writer }) => {
|
||||||
|
const emit = (type: string, data: unknown) => writer.write({ type, data, ...(type === "data-canvasAgentRun" ? { transient: true } : {}) } as never);
|
||||||
|
let cancelled = false;
|
||||||
|
let leaseLost = false;
|
||||||
|
const activeModelController = new AbortController();
|
||||||
|
const withModel: typeof executeVisionModel = (execute, signal) => executeVisionModel(execute,
|
||||||
|
signal ? AbortSignal.any([signal, activeModelController.signal]) : activeModelController.signal);
|
||||||
|
const heartbeat = setInterval(() => {
|
||||||
|
void heartbeatAgentRun(run)
|
||||||
|
.then((current) => {
|
||||||
|
if (!current) leaseLost = true;
|
||||||
|
if (current?.cancelRequested) cancelled = true;
|
||||||
|
if (cancelled || leaseLost) activeModelController?.abort();
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
leaseLost = true;
|
||||||
|
activeModelController?.abort();
|
||||||
|
});
|
||||||
|
emit("data-keepalive", { label: "canvas-agent-runtime", ts: Date.now() });
|
||||||
|
}, 10_000);
|
||||||
|
const textId = crypto.randomUUID();
|
||||||
|
writer.write({ type: "start", messageId: assistantMessageId });
|
||||||
|
writer.write({ type: "text-start", id: textId });
|
||||||
|
let streamedText = "";
|
||||||
|
const emitAssistantText = (delta: string) => {
|
||||||
|
streamedText += delta;
|
||||||
|
writer.write({ type: "text-delta", id: textId, delta });
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
const videoPorts = isVideo
|
||||||
|
? await createVideoRuntimePorts({
|
||||||
|
userId: session.user.id,
|
||||||
|
chatId: body.id,
|
||||||
|
projectId,
|
||||||
|
runId: run.id,
|
||||||
|
assistantMessageId,
|
||||||
|
request,
|
||||||
|
snapshot,
|
||||||
|
emit,
|
||||||
|
})
|
||||||
|
: null;
|
||||||
|
archiveVideoResults = videoPorts?.archiveResults;
|
||||||
|
const tools = isVideo
|
||||||
|
? buildVideoRuntimeTools({
|
||||||
|
runId: run.id,
|
||||||
|
projectId,
|
||||||
|
selectedNodeIds,
|
||||||
|
snapshot,
|
||||||
|
ports: videoPorts!,
|
||||||
|
})
|
||||||
|
: await buildCanvasRuntimeTools({
|
||||||
|
userId: session.user.id,
|
||||||
|
chatId: body.id,
|
||||||
|
projectId,
|
||||||
|
runId: run.id,
|
||||||
|
assistantMessageId,
|
||||||
|
selectedNodeIds: taskSourceNodeIds,
|
||||||
|
request,
|
||||||
|
model: visionModel || model,
|
||||||
|
supportsVision: Boolean(visionModel),
|
||||||
|
withModel,
|
||||||
|
imageSize: snapshot.imagePreferences?.parameters?.imageSize,
|
||||||
|
imagePreferences: snapshot.imagePreferences,
|
||||||
|
priorCalls: snapshot.calls,
|
||||||
|
preferredModelId: snapshot.imagePreferences?.modelId || (preset?.type === "image" ? preset.defaultModelId || undefined : undefined),
|
||||||
|
operationSelectedNodeIds: selectedNodeIds,
|
||||||
|
objective: [snapshot.objective, ...snapshot.inputs.map((input) => input.text)].join(
|
||||||
|
"\n",
|
||||||
|
),
|
||||||
|
emit,
|
||||||
|
});
|
||||||
|
await registerProjectAgentTools({ session, projectId, request, objective: snapshot.objective,
|
||||||
|
definitions: tools.definitions, executable: tools.executable, emit,
|
||||||
|
imagePreferences: snapshot.imagePreferences });
|
||||||
|
const gateway = await createZcodeModelGateway(configured, undefined, candidates);
|
||||||
|
const status = await runZcodeCanvasTask({
|
||||||
|
runId: run.id, snapshot, definitions: tools.definitions,
|
||||||
|
instructions: (isVideo ? videoRuntimeSystemPrompt : canvasRuntimeSystemPrompt)(snapshot.objective, snapshot.summary, snapshot.inputs.map(input => input.text), context),
|
||||||
|
context, gateway, signal: activeModelController.signal,
|
||||||
|
onAssistantText: emitAssistantText,
|
||||||
|
tools: tools.executable,
|
||||||
|
checkpoint: async (state, stateStatus) => {
|
||||||
|
if (leaseLost) throw new AgentRunError("任务执行已中断");
|
||||||
|
const saved = await checkpointAgentRun(run, state, stateStatus);
|
||||||
|
cancelled ||= saved.cancelRequested;
|
||||||
|
emit("data-canvasAgentRun", agentRunView(saved));
|
||||||
|
},
|
||||||
|
consumeInputs: async (state) => {
|
||||||
|
if (leaseLost) throw new AgentRunError("任务执行已中断");
|
||||||
|
const control = await consumeAgentRunInputs(run, state);
|
||||||
|
cancelled ||= control.cancelled;
|
||||||
|
return { cancelled };
|
||||||
|
},
|
||||||
|
isCancelled: async () => {
|
||||||
|
const current = await heartbeatAgentRun(run);
|
||||||
|
if (!current) throw new AgentRunError("任务执行已中断");
|
||||||
|
cancelled ||= current.cancelRequested;
|
||||||
|
return cancelled;
|
||||||
|
},
|
||||||
|
verify: async (state) =>
|
||||||
|
isVideo
|
||||||
|
? verifyVideoAgentResults({
|
||||||
|
snapshot: state,
|
||||||
|
nodes: (await readOwnedVideoCanvas(session.user.id, projectId)).nodes,
|
||||||
|
inspect: async (node, requirements) => {
|
||||||
|
const metadata = await probeVideoMetadata(videoNodeUrl(node));
|
||||||
|
const report = await videoPorts!.analyze({
|
||||||
|
mode: "video-analysis",
|
||||||
|
prompt: `请基于真实抽帧画面检查主体、场景、镜头连贯性,说明不确定内容。用户要求:${requirements}`,
|
||||||
|
imageUrls: [],
|
||||||
|
videoUrls: [videoNodeUrl(node)],
|
||||||
|
});
|
||||||
|
const verdict = await generateText({
|
||||||
|
model,
|
||||||
|
system:
|
||||||
|
'你是视频验收员。依据实际抽帧分析判断是否满足用户要求;不能从文件名或提示词判断。若报告无法读取真实帧,必须不通过。抽帧只能检查画面,不能证明音频、精确时长或完整运动,涉及这些要求无法确认时说明需要人工检查。仅返回 JSON {"passed":boolean,"issues":string[]}。',
|
||||||
|
prompt: JSON.stringify({ requirements, report }),
|
||||||
|
maxOutputTokens: 1200,
|
||||||
|
maxRetries: 0,
|
||||||
|
abortSignal: AbortSignal.timeout(60_000),
|
||||||
|
});
|
||||||
|
state.inputTokens += verdict.usage.inputTokens || 0;
|
||||||
|
state.outputTokens += verdict.usage.outputTokens || 0;
|
||||||
|
const match = verdict.text.match(/\{[\s\S]*\}/);
|
||||||
|
if (!match) throw new Error("视频检查未返回有效结果");
|
||||||
|
return {
|
||||||
|
...z
|
||||||
|
.object({ passed: z.boolean(), issues: z.array(z.string()).max(12) })
|
||||||
|
.parse(JSON.parse(match[0])),
|
||||||
|
...metadata,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
})
|
||||||
|
: verifyCanvasAgentResults({
|
||||||
|
snapshot: state,
|
||||||
|
userId: session.user.id,
|
||||||
|
projectId,
|
||||||
|
model: visionModel || model,
|
||||||
|
supportsVision: Boolean(visionModel),
|
||||||
|
withModel,
|
||||||
|
signal: activeModelController.signal,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const finalText = canvasAgentReplyText(snapshot);
|
||||||
|
const progress = canvasAgentProgress(agentRunView({ ...run, snapshot, status }));
|
||||||
|
writer.write({ type: "data-canvasAgentProgress", id: run.id, data: progress });
|
||||||
|
if (!isVideo)
|
||||||
|
await persistRuntimeConversationResults({
|
||||||
|
userId: session.user.id,
|
||||||
|
chatId: body.id,
|
||||||
|
projectId,
|
||||||
|
assistantMessageId,
|
||||||
|
snapshot,
|
||||||
|
}).catch((error) => {
|
||||||
|
console.warn("[Canvas Agent] Conversation memory update failed", {
|
||||||
|
runId: run.id,
|
||||||
|
error: publicAgentError(error),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
await saveMessages({
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
id: assistantMessageId,
|
||||||
|
chatId: body.id,
|
||||||
|
role: "assistant",
|
||||||
|
parts: [{ type: "text", text: finalText }, { type: "data-canvasAgentProgress", data: progress }],
|
||||||
|
attachments: [],
|
||||||
|
createdAt: new Date(),
|
||||||
|
videoStorageStatus: null,
|
||||||
|
temporaryVideoUrl: null,
|
||||||
|
permanentVideoUrl: null,
|
||||||
|
archivedAt: null,
|
||||||
|
videoMetadata: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
emitAssistantText(finalText.startsWith(streamedText) ? finalText.slice(streamedText.length) : `\n\n${snapshot.message}`);
|
||||||
|
} catch (error) {
|
||||||
|
const statusText = publicAgentError(error);
|
||||||
|
const text = canvasAgentReplyText({ ...snapshot, message: statusText });
|
||||||
|
await checkpointAgentRun(run, { ...snapshot, message: statusText }, "needs_review").catch(
|
||||||
|
() => undefined,
|
||||||
|
);
|
||||||
|
const progress = canvasAgentProgress(agentRunView({ ...run, snapshot: { ...snapshot, message: statusText }, status: "needs_review" }));
|
||||||
|
writer.write({ type: "data-canvasAgentProgress", id: run.id, data: progress });
|
||||||
|
await saveMessages({ messages: [{ id: assistantMessageId, chatId: body.id, role: "assistant",
|
||||||
|
parts: [{ type: "text", text }, { type: "data-canvasAgentProgress", data: progress }],
|
||||||
|
attachments: [], createdAt: new Date(), videoStorageStatus: null, temporaryVideoUrl: null,
|
||||||
|
permanentVideoUrl: null, archivedAt: null, videoMetadata: null }] }).catch(() => undefined);
|
||||||
|
emitAssistantText(text.startsWith(streamedText) ? text.slice(streamedText.length) : `\n\n${statusText}`);
|
||||||
|
} finally {
|
||||||
|
clearInterval(heartbeat);
|
||||||
|
writer.write({ type: "text-end", id: textId });
|
||||||
|
writer.write({ type: "finish" });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// Browser disconnection does not cancel paid image work. Keep consuming on the server.
|
||||||
|
const [client, background] = stream.tee();
|
||||||
|
after(async () => {
|
||||||
|
const reader = background.getReader();
|
||||||
|
try {
|
||||||
|
while (!(await reader.read()).done) {
|
||||||
|
/* Drain the server branch. */
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
reader.releaseLock();
|
||||||
|
await archiveVideoResults?.();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return createUIMessageStreamResponse({
|
||||||
|
stream: client,
|
||||||
|
headers: { "Cache-Control": "no-store", "X-Accel-Buffering": "no" },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: publicAgentError(error), message: publicAgentError(error) },
|
||||||
|
{
|
||||||
|
status:
|
||||||
|
error instanceof AgentRunError ? error.status : error instanceof z.ZodError ? 400 : 503,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
65
app/api/canvas/agent-runs/route.ts
Normal file
65
app/api/canvas/agent-runs/route.ts
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import {
|
||||||
|
AgentRunError,
|
||||||
|
agentRunView,
|
||||||
|
assertAgentRunOwnership,
|
||||||
|
controlAgentRun,
|
||||||
|
getAgentRun,
|
||||||
|
} from "@/lib/ai/canvas-agent-runtime/store";
|
||||||
|
import { publicAgentError } from "@/lib/ai/canvas-agent-runtime/protocol";
|
||||||
|
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
const scope = z.object({
|
||||||
|
chatId: z.string().uuid(),
|
||||||
|
projectId: z.string().uuid(),
|
||||||
|
runId: z.string().uuid().optional(),
|
||||||
|
});
|
||||||
|
const control = scope
|
||||||
|
.extend({
|
||||||
|
runId: z.string().uuid(),
|
||||||
|
action: z.enum(["steer", "cancel"]),
|
||||||
|
inputId: z.string().uuid().optional(),
|
||||||
|
text: z.string().trim().min(1).max(2000).optional(),
|
||||||
|
})
|
||||||
|
.refine(
|
||||||
|
(input) => input.action !== "steer" || Boolean(input.inputId && input.text),
|
||||||
|
"补充要求缺少内容或请求号",
|
||||||
|
);
|
||||||
|
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
|
try {
|
||||||
|
const input = scope.parse(Object.fromEntries(new URL(request.url).searchParams));
|
||||||
|
await assertAgentRunOwnership(session.user.id, input.projectId, input.chatId);
|
||||||
|
const run = await getAgentRun(session.user.id, input.chatId, input.projectId, input.runId);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ run: run ? agentRunView(run) : null },
|
||||||
|
{ headers: { "Cache-Control": "private, no-store" } },
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
return failure(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
|
try {
|
||||||
|
const input = control.parse(await request.json());
|
||||||
|
await assertAgentRunOwnership(session.user.id, input.projectId, input.chatId);
|
||||||
|
return NextResponse.json({ run: await controlAgentRun({ ...input, userId: session.user.id }) });
|
||||||
|
} catch (error) {
|
||||||
|
return failure(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function failure(error: unknown) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: publicAgentError(error) },
|
||||||
|
{
|
||||||
|
status:
|
||||||
|
error instanceof AgentRunError ? error.status : error instanceof z.ZodError ? 400 : 503,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -9,10 +9,10 @@ export async function GET() {
|
|||||||
const models = await listCanvasAudioModels();
|
const models = await listCanvasAudioModels();
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
models: models.map(({ model }) => ({
|
models: models.map(({ model, provider }) => ({
|
||||||
id: model.id,
|
id: model.id,
|
||||||
modelId: model.modelId,
|
modelId: model.modelId,
|
||||||
name: model.displayName,
|
name: `${model.displayName} · ${provider.type === "vip98k" ? "98K" : "阿里云"}`,
|
||||||
...audioContract(model.modelId),
|
...audioContract(model.modelId),
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
|
|||||||
71
app/api/canvas/batch-instruction/route.ts
Normal file
71
app/api/canvas/batch-instruction/route.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import { createCanvasImageTask, enqueueCanvasImageTask } from "@/lib/canvas/image-task-queue";
|
||||||
|
import { getSystemSetting } from "@/lib/db/queries";
|
||||||
|
import type { CanvasGenerateImageRequest } from "@/lib/canvas/generate-image";
|
||||||
|
|
||||||
|
const imageUrl = z.string().trim().max(8192).refine(value => {
|
||||||
|
try {
|
||||||
|
const url = new URL(value);
|
||||||
|
return (url.protocol === "https:" || url.protocol === "http:") &&
|
||||||
|
!url.username && !url.password;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const submission = z.object({
|
||||||
|
taskId: z.string().uuid(),
|
||||||
|
image: imageUrl,
|
||||||
|
reference: imageUrl.optional(),
|
||||||
|
instruction: z.string().trim().min(1).max(3000),
|
||||||
|
square: z.boolean(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
|
const parsed = submission.safeParse(await request.json().catch(() => null));
|
||||||
|
if (!parsed.success) return NextResponse.json({ error: "请提供原图和 1 至 3000 字的编辑指令" }, { status: 400 });
|
||||||
|
|
||||||
|
const { taskId, image, reference, instruction, square } = parsed.data;
|
||||||
|
if (reference === image) return NextResponse.json({ error: "共用参考图和待编辑原图相同" }, { status: 400 });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const setting = await getSystemSetting({ key: "scene_fusion_config" });
|
||||||
|
if ((setting?.value as { enabled?: boolean } | null)?.enabled === false) {
|
||||||
|
return NextResponse.json({ error: "批量处理功能已被管理员禁用" }, { status: 403 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const referenceImages = reference ? [image, reference] : [image];
|
||||||
|
const imageRequest: CanvasGenerateImageRequest = {
|
||||||
|
prompt: [
|
||||||
|
"编辑第一张图。保留第一张图的完整构图、原始宽高比和所有未要求修改的内容。",
|
||||||
|
reference && "第二张图只提供指令指定的人脸、商品或其他特征。以第一张图为底图,保持第一张图中其余人物、姿势、衣着、商品、背景和光线,除非指令明确要求更改。",
|
||||||
|
"保持原图整幅画面的四边,避免裁切或拉伸。只输出一张编辑后的图片。",
|
||||||
|
"编辑指令:" + instruction,
|
||||||
|
].filter(Boolean).join("\n"),
|
||||||
|
displayPrompt: instruction,
|
||||||
|
aspectRatio: "auto",
|
||||||
|
imageCount: 1,
|
||||||
|
referenceImages,
|
||||||
|
requiredReferenceImageCount: referenceImages.length,
|
||||||
|
agentImageMode: "auto",
|
||||||
|
promptExtend: false,
|
||||||
|
padBatchInstructionToSquare: square || undefined,
|
||||||
|
batchInstructionWithReference: Boolean(reference) || undefined,
|
||||||
|
debugLabel: "batch-instruction-edit",
|
||||||
|
};
|
||||||
|
const task = await createCanvasImageTask({
|
||||||
|
userId: session.user.id,
|
||||||
|
request: { ...imageRequest },
|
||||||
|
taskId,
|
||||||
|
});
|
||||||
|
if (task.status === "pending") await enqueueCanvasImageTask(task.id);
|
||||||
|
return NextResponse.json({ taskId: task.id }, { status: 202 });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[BatchInstruction] Task submission failed", { taskId, error });
|
||||||
|
return NextResponse.json({ error: "暂时无法确认是否提交成功,请查询原任务" }, { status: 503 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,100 +1,15 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import {
|
import { executeBoxCutoutPOST } from "@/lib/canvas/operations/box-cutout";
|
||||||
BoxCutoutServiceError,
|
|
||||||
executeBoxCutoutJob,
|
|
||||||
} from "@/lib/ai/box-cutout/service";
|
|
||||||
import { RmbgServiceError } from "@/lib/ai/rmbg/service";
|
|
||||||
import { getPointsSystemConfig } from "@/lib/db/queries";
|
|
||||||
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
|
||||||
import { getImageToolCreditCost } from "@/lib/points/config";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 60;
|
export const maxDuration = 60;
|
||||||
|
|
||||||
type BoxCutoutRequest = {
|
export async function POST(request: Request) {
|
||||||
imageUrl?: string;
|
|
||||||
cropRect?: {
|
|
||||||
x?: number;
|
|
||||||
y?: number;
|
|
||||||
width?: number;
|
|
||||||
height?: number;
|
|
||||||
};
|
|
||||||
paddingRatio?: number;
|
|
||||||
prompt?: string;
|
|
||||||
chatId?: string | null;
|
|
||||||
messageId?: string | null;
|
|
||||||
skipGallerySave?: boolean;
|
|
||||||
sourceNodeId?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
|
||||||
try {
|
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return NextResponse.json({ success: false, error: "未授权,请先登录" }, { status: 401 });
|
return NextResponse.json({ success: false, error: "未授权,请先登录" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
return executeBoxCutoutPOST(request, session.user.id);
|
||||||
const body = (await request.json()) as BoxCutoutRequest;
|
|
||||||
const rawImageUrl = String(body.imageUrl || "").trim();
|
|
||||||
if (!rawImageUrl) {
|
|
||||||
return NextResponse.json({ success: false, error: "缺少 imageUrl 参数" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const resolvedImageUrl = new URL(rawImageUrl, request.url).toString();
|
|
||||||
const pointsConfig = await getPointsSystemConfig();
|
|
||||||
const pointsCharge = await prepareFeaturePointsCharge({
|
|
||||||
userId: session.user.id,
|
|
||||||
unitCost: getImageToolCreditCost(pointsConfig, "box-cutout"),
|
|
||||||
reason: "image_box_cutout",
|
|
||||||
modelId: "box-cutout",
|
|
||||||
note: "框选抠图消耗积分",
|
|
||||||
pointsConfig,
|
|
||||||
});
|
|
||||||
if (!pointsCharge.ok) {
|
|
||||||
return NextResponse.json(pointsCharge.payload, { status: pointsCharge.status });
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await executeBoxCutoutJob({
|
|
||||||
userId: session.user.id,
|
|
||||||
imageUrl: resolvedImageUrl,
|
|
||||||
cropRect: body.cropRect || {},
|
|
||||||
paddingRatio: body.paddingRatio,
|
|
||||||
prompt: body.prompt || "框选抠图",
|
|
||||||
chatId: body.chatId || null,
|
|
||||||
messageId: body.messageId || null,
|
|
||||||
skipGallerySave: body.skipGallerySave === true,
|
|
||||||
sourceNodeId: body.sourceNodeId,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (pointsCharge.chargeable) {
|
|
||||||
try {
|
|
||||||
await pointsCharge.consume();
|
|
||||||
} catch (pointsError) {
|
|
||||||
console.error("[Points] Failed to consume box cutout points:", pointsError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json(result);
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof BoxCutoutServiceError || error instanceof RmbgServiceError) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: error.detail || error.message,
|
|
||||||
},
|
|
||||||
{ status: error.status },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.error("[BoxCutout] 处理失败:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
success: false,
|
|
||||||
error: error instanceof Error ? error.message : "框选抠图失败",
|
|
||||||
},
|
|
||||||
{ status: 500 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1804,7 +1804,7 @@ async function runDualDirectorPlanPreflight(params: {
|
|||||||
? "You are both an art director and a marketing director for high-conversion e-commerce detail pages. Output JSON only."
|
? "You are both an art director and a marketing director for high-conversion e-commerce detail pages. Output JSON only."
|
||||||
: "你同时扮演高转化电商详情页的平面设计总监与营销总监。仅输出JSON。",
|
: "你同时扮演高转化电商详情页的平面设计总监与营销总监。仅输出JSON。",
|
||||||
temperature: params.isLocalMoonshot ? 1 : 0.1,
|
temperature: params.isLocalMoonshot ? 1 : 0.1,
|
||||||
maxTokens: 2000,
|
maxOutputTokens: 2000,
|
||||||
...(params.kimiOptions || {}),
|
...(params.kimiOptions || {}),
|
||||||
}, params.timeoutMs || DETAIL_PLAN_PREFLIGHT_TIMEOUT_MS);
|
}, params.timeoutMs || DETAIL_PLAN_PREFLIGHT_TIMEOUT_MS);
|
||||||
|
|
||||||
@ -2040,7 +2040,7 @@ export async function POST(request: Request) {
|
|||||||
kimiDetailProfile: plannerKimiOptions.headers
|
kimiDetailProfile: plannerKimiOptions.headers
|
||||||
? {
|
? {
|
||||||
thinking: plannerKimiOptions.headers["x-detail-agent-kimi-thinking"],
|
thinking: plannerKimiOptions.headers["x-detail-agent-kimi-thinking"],
|
||||||
maxTokens: plannerKimiOptions.maxTokens,
|
maxOutputTokens: plannerKimiOptions.maxOutputTokens,
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
});
|
});
|
||||||
@ -2177,7 +2177,7 @@ export async function POST(request: Request) {
|
|||||||
`{"summary":"简述","globalStyle":{"designLanguage":"一句话风格方向","typography":{"fontFamily":"字体家族","title":"主标题规范","subtitle":"副标题规范","body":"正文规范"},"layout":{"grid":"栅格","safeArea":"安全区","rhythm":"留白节奏"},"palette":"配色规范"},"screens":[{"index":1,"screenType":"B","objective":"本屏目标","title":"首屏示例:甩甩杆,自拍不手抖(6-20字)","subtitle":"一句副标题","copy":"正文文案","layoutNote":"排版说明","useModel":false,"modelReason":"是否需要人物及原因","bundleShowcase":false,"bundleReason":"是否为搭配展示页及原因","fabe":{"b":"利益","f":"特征","a":"优势","e":"证据"},"visualPrompt":"用于生图的完整提示词,必须包含790x1300与统一字体排版"}],"dslV2Screens":[{"version":"detail-ui-dsl/v2","screenIndex":1,"layoutVariant":"hero-left-panel","region":{"x":0.06,"y":0.05,"width":0.88,"height":0.46},"nodes":[{"kind":"panel","id":"hero-panel","variant":"glass","tone":"light","direction":"column","gap":18,"children":[{"kind":"badge","id":"screen-badge","icon":"sparkle","text":"核心升级","tone":"primary"},{"kind":"heading","id":"hero-title","text":"护腰椅,久坐不腰酸","level":1,"emphasis":"hero"},{"kind":"body","id":"hero-subtitle","text":"在家里加班,也能护腰/舒适","emphasis":"strong"}]}]}]}`,
|
`{"summary":"简述","globalStyle":{"designLanguage":"一句话风格方向","typography":{"fontFamily":"字体家族","title":"主标题规范","subtitle":"副标题规范","body":"正文规范"},"layout":{"grid":"栅格","safeArea":"安全区","rhythm":"留白节奏"},"palette":"配色规范"},"screens":[{"index":1,"screenType":"B","objective":"本屏目标","title":"首屏示例:甩甩杆,自拍不手抖(6-20字)","subtitle":"一句副标题","copy":"正文文案","layoutNote":"排版说明","useModel":false,"modelReason":"是否需要人物及原因","bundleShowcase":false,"bundleReason":"是否为搭配展示页及原因","fabe":{"b":"利益","f":"特征","a":"优势","e":"证据"},"visualPrompt":"用于生图的完整提示词,必须包含790x1300与统一字体排版"}],"dslV2Screens":[{"version":"detail-ui-dsl/v2","screenIndex":1,"layoutVariant":"hero-left-panel","region":{"x":0.06,"y":0.05,"width":0.88,"height":0.46},"nodes":[{"kind":"panel","id":"hero-panel","variant":"glass","tone":"light","direction":"column","gap":18,"children":[{"kind":"badge","id":"screen-badge","icon":"sparkle","text":"核心升级","tone":"primary"},{"kind":"heading","id":"hero-title","text":"护腰椅,久坐不腰酸","level":1,"emphasis":"hero"},{"kind":"body","id":"hero-subtitle","text":"在家里加班,也能护腰/舒适","emphasis":"strong"}]}]}]}`,
|
||||||
temperature: 0.25,
|
temperature: 0.25,
|
||||||
...(localDetailAgentResolved ? { temperature: 1 } : {}),
|
...(localDetailAgentResolved ? { temperature: 1 } : {}),
|
||||||
maxTokens: 1800,
|
maxOutputTokens: 1800,
|
||||||
...plannerKimiOptions,
|
...plannerKimiOptions,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -323,7 +323,7 @@ export async function POST(request: Request) {
|
|||||||
kimiDetailProfile: reviewKimiOptions.headers
|
kimiDetailProfile: reviewKimiOptions.headers
|
||||||
? {
|
? {
|
||||||
thinking: reviewKimiOptions.headers["x-detail-agent-kimi-thinking"],
|
thinking: reviewKimiOptions.headers["x-detail-agent-kimi-thinking"],
|
||||||
maxTokens: reviewKimiOptions.maxTokens,
|
maxOutputTokens: reviewKimiOptions.maxOutputTokens,
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
});
|
});
|
||||||
@ -438,7 +438,7 @@ export async function POST(request: Request) {
|
|||||||
? "You are both an Art Director and a Marketing Director for high-conversion e-commerce detail pages. Evaluate with strict professional standards and output JSON only."
|
? "You are both an Art Director and a Marketing Director for high-conversion e-commerce detail pages. Evaluate with strict professional standards and output JSON only."
|
||||||
: "你同时是高转化电商详情页的平面设计总监与营销总监。按严格专业标准评估,仅输出JSON。",
|
: "你同时是高转化电商详情页的平面设计总监与营销总监。按严格专业标准评估,仅输出JSON。",
|
||||||
temperature: localDetailAgentResolved ? 1 : 0.1,
|
temperature: localDetailAgentResolved ? 1 : 0.1,
|
||||||
maxTokens: 800,
|
maxOutputTokens: 800,
|
||||||
...reviewKimiOptions,
|
...reviewKimiOptions,
|
||||||
} as any);
|
} as any);
|
||||||
text = String(result?.text || "");
|
text = String(result?.text || "");
|
||||||
|
|||||||
@ -1,7 +1,13 @@
|
|||||||
import { resolveCanvasCdnDownload } from "@/lib/storage/canvas-cdn-download";
|
import { resolveCanvasCdnDownload } from "@/lib/storage/canvas-cdn-download";
|
||||||
import { prepareImageArchiveDownload } from "@/lib/storage/image-download-preparation";
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
|
import { scheduleImageDownloadPreparation } from "@/lib/storage/image-download-preparation";
|
||||||
|
import { randomUUID } from "node:crypto";
|
||||||
|
import { formatDownloadFailure, getDownloadFailure, type DownloadFailure } from "@/lib/canvas/download-error";
|
||||||
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import { after, NextRequest, NextResponse } from "next/server";
|
import { after, NextRequest, NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import { getDownloadPreparationQueue, type DownloadPreparationInput } from "@/lib/canvas/download-preparation";
|
||||||
|
import { extractImageProxySourceUrl, inferOriginalImageUrl } from "@/lib/image-url";
|
||||||
import {
|
import {
|
||||||
buildAttachmentContentDisposition,
|
buildAttachmentContentDisposition,
|
||||||
fetchCanvasDownloadAsset,
|
fetchCanvasDownloadAsset,
|
||||||
@ -10,6 +16,51 @@ import {
|
|||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 120;
|
export const maxDuration = 120;
|
||||||
|
const logger = createLogger("canvas.download");
|
||||||
|
|
||||||
|
function failed(failure: DownloadFailure, requestId: string) {
|
||||||
|
logger.warn("request_failed", { requestId, code: failure.code, status: failure.status });
|
||||||
|
return NextResponse.json({ success: false, error: formatDownloadFailure(failure, requestId), failure, requestId }, {
|
||||||
|
status: failure.status,
|
||||||
|
headers: { "Cache-Control": "no-store", ...(failure.status === 429 ? { "Retry-After": "3" } : {}) },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function preparing(requestId: string, converting = false) {
|
||||||
|
return NextResponse.json({ delivery: "preparing", retryAfter: 2, requestId,
|
||||||
|
phase: converting ? "converting" : "preparing",
|
||||||
|
message: converting ? "正在转换为兼容 PNG 并保存,完成后自动下载" : "正在准备原图,完成后将自动开始下载" }, {
|
||||||
|
status: 202, headers: { "Retry-After": "2", "Cache-Control": "no-store" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function prepareDownload(input: DownloadPreparationInput, signal: AbortSignal) {
|
||||||
|
// A proxy candidate for the same original must not repeat the same slow
|
||||||
|
// upstream request through our own server (and create another HTTP timeout).
|
||||||
|
const sources = [...new Set(input.urls.map(url => {
|
||||||
|
const source = extractImageProxySourceUrl(url) || url;
|
||||||
|
return inferOriginalImageUrl(source) || source;
|
||||||
|
}))];
|
||||||
|
let lastError: unknown;
|
||||||
|
for (let attempt = 0; attempt < 2; attempt++) {
|
||||||
|
signal.throwIfAborted();
|
||||||
|
for (const url of sources) {
|
||||||
|
signal.throwIfAborted();
|
||||||
|
const result = await fetchCanvasDownloadAsset({
|
||||||
|
file: { url, name: "original" }, index: 0,
|
||||||
|
requestOrigin: input.requestOrigin, cookie: input.cookie,
|
||||||
|
includeSequencePrefix: false, timeoutMs: 60_000, retryCount: 1, signal,
|
||||||
|
});
|
||||||
|
if (result.ok) return { buffer: result.bytes, contentType: result.contentType };
|
||||||
|
lastError = result.error;
|
||||||
|
}
|
||||||
|
// Each pass resolves the latest archive URL inside fetchCanvasDownloadAsset.
|
||||||
|
// No AI/model request is involved and a completed source fetch is shared.
|
||||||
|
if (attempt < 1 && getDownloadFailure(lastError).retryable) await new Promise(resolve => setTimeout(resolve, 1_000));
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
throw new Error(String(lastError || "Original image sources unavailable"));
|
||||||
|
}
|
||||||
|
|
||||||
function getRequestOrigin(request: NextRequest): string {
|
function getRequestOrigin(request: NextRequest): string {
|
||||||
const forwardedHost = (request.headers.get("x-forwarded-host") || "").split(",")[0]?.trim();
|
const forwardedHost = (request.headers.get("x-forwarded-host") || "").split(",")[0]?.trim();
|
||||||
@ -48,29 +99,63 @@ function resolveDownloadUrls(body: Record<string, unknown>, origin: string): str
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
|
const requestId = randomUUID();
|
||||||
try {
|
try {
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
return failed(getDownloadFailure("未登录"), requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = (await request.json().catch(() => ({}))) as Record<string, unknown>;
|
const body = (await request.json().catch(() => ({}))) as Record<string, unknown>;
|
||||||
const requestOrigin = getRequestOrigin(request);
|
const requestOrigin = getRequestOrigin(request);
|
||||||
const urls = resolveDownloadUrls(body, requestOrigin);
|
const candidates = resolveDownloadUrls(body, requestOrigin);
|
||||||
|
if (candidates.length && candidates.every(isRetiredStorageUrl)) {
|
||||||
|
return failed(getDownloadFailure(RETIRED_STORAGE_MESSAGE), requestId);
|
||||||
|
}
|
||||||
|
const urls = candidates.filter(url => !isRetiredStorageUrl(url));
|
||||||
|
const requestedUrls = [...urls];
|
||||||
if (urls.length === 0) {
|
if (urls.length === 0) {
|
||||||
return NextResponse.json({ success: false, error: "图片地址无效" }, { status: 400 });
|
return failed(getDownloadFailure("图片地址无效"), requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
const name = sanitizeCanvasDownloadFileName(String(body.name || ""), "canvas-image");
|
const name = sanitizeCanvasDownloadFileName(String(body.name || ""), "canvas-image");
|
||||||
if (body.preferDirect === true) {
|
if (body.preferDirect === true) {
|
||||||
const { task, direct } = await resolveCanvasCdnDownload(urls, `${name}.png`).catch(() => ({ task: null, direct: null }));
|
const { task, direct, canPrepareDirect } = await resolveCanvasCdnDownload(urls, `${name}.png`);
|
||||||
if (direct) return NextResponse.json(direct, { headers: { "Cache-Control": "no-store" } });
|
if (direct) return NextResponse.json(direct, { headers: { "Cache-Control": "no-store" } });
|
||||||
if (task?.targetUrl) {
|
if (task && canPrepareDirect) {
|
||||||
if (!urls.includes(task.targetUrl)) urls.unshift(task.targetUrl);
|
const asset = (task.metadata as { downloadAsset?: { status?: string; failure?: DownloadFailure; retryAt?: string } } | null)?.downloadAsset;
|
||||||
after(() => prepareImageArchiveDownload(task.id));
|
if (asset?.status === "abandoned" || (asset?.status === "failed" && Date.parse(asset.retryAt || "") > Date.now())) {
|
||||||
|
return failed(asset.failure || getDownloadFailure(null), task.id);
|
||||||
}
|
}
|
||||||
|
scheduleImageDownloadPreparation(task.id);
|
||||||
|
return preparing(task.id, true);
|
||||||
|
}
|
||||||
|
if (task && !task.targetUrl) {
|
||||||
|
const { originalImagePreparing, prepareOriginalImageArchive } = await import("@/lib/storage/original-image-download");
|
||||||
|
const pending = await prepareOriginalImageArchive(task.sourceUrl);
|
||||||
|
return NextResponse.json(originalImagePreparing(pending), { status: 202,
|
||||||
|
headers: { "Retry-After": "3", "Cache-Control": "no-store" } });
|
||||||
|
}
|
||||||
|
throw new Error("下载文件暂时无法从存储桶直接下载");
|
||||||
}
|
}
|
||||||
const cookie = request.headers.get("cookie") || "";
|
const cookie = request.headers.get("cookie") || "";
|
||||||
|
if (body.preparation === "async") {
|
||||||
|
const result = await getDownloadPreparationQueue(prepareDownload, after).request({
|
||||||
|
userId: session.user.id, urls: requestedUrls, requestOrigin, cookie,
|
||||||
|
});
|
||||||
|
if (result.status === "preparing") {
|
||||||
|
return preparing(result.requestId);
|
||||||
|
}
|
||||||
|
if (result.status === "failed") {
|
||||||
|
return failed(result.failure, result.requestId);
|
||||||
|
}
|
||||||
|
return new NextResponse(new Uint8Array(result.asset.buffer), { headers: {
|
||||||
|
"Content-Type": result.asset.contentType,
|
||||||
|
"Content-Length": String(result.asset.buffer.length),
|
||||||
|
"Content-Disposition": buildAttachmentContentDisposition(`${name}.png`),
|
||||||
|
"Cache-Control": "no-store",
|
||||||
|
} });
|
||||||
|
}
|
||||||
let result: Awaited<ReturnType<typeof fetchCanvasDownloadAsset>> | null = null;
|
let result: Awaited<ReturnType<typeof fetchCanvasDownloadAsset>> | null = null;
|
||||||
const failures: string[] = [];
|
const failures: string[] = [];
|
||||||
|
|
||||||
@ -83,17 +168,14 @@ export async function POST(request: NextRequest) {
|
|||||||
includeSequencePrefix: false,
|
includeSequencePrefix: false,
|
||||||
});
|
});
|
||||||
if (result.ok) break;
|
if (result.ok) break;
|
||||||
failures.push(`${url}: ${result.error}`);
|
failures.push(result.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result?.ok) {
|
if (!result?.ok) {
|
||||||
if (failures.length > 0) {
|
if (failures.length > 0) {
|
||||||
console.warn("[CanvasDownloadImage] All candidates failed:", failures);
|
logger.warn("all_sources_failed", { requestId, failures });
|
||||||
}
|
}
|
||||||
return NextResponse.json(
|
return failed(getDownloadFailure(result?.error), requestId);
|
||||||
{ success: false, error: result?.error || "图片源文件暂时不可访问" },
|
|
||||||
{ status: 502 }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new NextResponse(result.bytes, {
|
return new NextResponse(result.bytes, {
|
||||||
@ -106,7 +188,7 @@ export async function POST(request: NextRequest) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[CanvasDownloadImage] Failed:", error);
|
logger.error("download_failed", error, { requestId });
|
||||||
return NextResponse.json({ success: false, error: "图片下载失败" }, { status: 500 });
|
return failed(getDownloadFailure(error), requestId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,55 +1,23 @@
|
|||||||
import { writeDownloadZip, readDownloadZip, removeDownloadZip } from "@/lib/canvas/download-zip-stream";
|
import { readDownloadZip } from "@/lib/canvas/download-zip-stream";
|
||||||
|
import { claimZipTask, loadZipTask, saveZipTask, zipTaskPath, assertZipDiskSpace, cleanupZipTasks, assertZipTaskCapacity, type ZipTask } from "@/lib/canvas/download-zip-task-store";
|
||||||
|
import { parseDownloadByteRange } from "@/lib/canvas/download-byte-range";
|
||||||
|
import { mkdir, stat, rm } from "node:fs/promises";
|
||||||
|
import { join } from "node:path";
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { after, NextRequest, NextResponse } from "next/server";
|
||||||
import JSZip from "jszip";
|
import { prepareCanvasZip } from "@/lib/canvas/download-zip-prepare";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { fetchCanvasDownloadAsset } from "@/lib/canvas/photoshop-safe-download";
|
import { fetchCanvasDownloadAsset } from "@/lib/canvas/photoshop-safe-download";
|
||||||
|
import { publishCanvasZip } from "@/lib/storage/canvas-zip-delivery";
|
||||||
|
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 300;
|
export const maxDuration = 300;
|
||||||
|
type DownloadFileItem = { url: string; name?: string };
|
||||||
type DownloadFileItem = {
|
|
||||||
url: string;
|
|
||||||
name?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type ZipTaskStatus = "pending" | "running" | "completed" | "failed";
|
|
||||||
|
|
||||||
type ZipTask = {
|
|
||||||
id: string;
|
|
||||||
userId: string;
|
|
||||||
folderName: string;
|
|
||||||
files: DownloadFileItem[];
|
|
||||||
totalCount: number;
|
|
||||||
processedCount: number;
|
|
||||||
successCount: number;
|
|
||||||
failedCount: number;
|
|
||||||
compressPercent: number;
|
|
||||||
status: ZipTaskStatus;
|
|
||||||
stage: string;
|
|
||||||
error?: string;
|
|
||||||
zipFile?: { path: string; bytes: number };
|
|
||||||
contentDisposition?: string;
|
|
||||||
createdAt: number;
|
|
||||||
updatedAt: number;
|
|
||||||
expiresAt: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
const MAX_CONCURRENCY = 8;
|
|
||||||
const MAX_DOWNLOAD_FILES = 200;
|
const MAX_DOWNLOAD_FILES = 200;
|
||||||
const TASK_TTL_MS = 30 * 60 * 1000;
|
const TASK_TTL_MS = 24 * 60 * 60_000;
|
||||||
|
const PREPARATION_TTL_MS = 6 * 60 * 60_000;
|
||||||
const globalTaskStore = globalThis as typeof globalThis & {
|
const running = (globalThis as typeof globalThis & { __canvasZipRunning?: Set<string> });
|
||||||
__canvasDownloadZipTasks?: Map<string, ZipTask>;
|
const active = running.__canvasZipRunning ??= new Set<string>();
|
||||||
};
|
|
||||||
|
|
||||||
function getTaskStore() {
|
|
||||||
if (!globalTaskStore.__canvasDownloadZipTasks) {
|
|
||||||
globalTaskStore.__canvasDownloadZipTasks = new Map<string, ZipTask>();
|
|
||||||
}
|
|
||||||
return globalTaskStore.__canvasDownloadZipTasks;
|
|
||||||
}
|
|
||||||
|
|
||||||
function sanitizeFolderName(value: string): string {
|
function sanitizeFolderName(value: string): string {
|
||||||
const cleaned = String(value || "")
|
const cleaned = String(value || "")
|
||||||
.replace(/[\\/:*?"<>|]+/g, "-")
|
.replace(/[\\/:*?"<>|]+/g, "-")
|
||||||
@ -57,17 +25,7 @@ function sanitizeFolderName(value: string): string {
|
|||||||
.replace(/-+/g, "-")
|
.replace(/-+/g, "-")
|
||||||
.replace(/^-+|-+$/g, "")
|
.replace(/^-+|-+$/g, "")
|
||||||
.trim();
|
.trim();
|
||||||
return cleaned || `canvas-images-${Date.now()}`;
|
return cleaned && !/^\.+$/.test(cleaned) ? cleaned.slice(0, 100) : `canvas-images-${Date.now()}`;
|
||||||
}
|
|
||||||
|
|
||||||
function buildContentDisposition(folderName: string): string {
|
|
||||||
const encodedName = encodeURIComponent(`${folderName}.zip`);
|
|
||||||
const asciiName =
|
|
||||||
folderName
|
|
||||||
.replace(/[^\x20-\x7E]+/g, "")
|
|
||||||
.replace(/"/g, "")
|
|
||||||
.trim() || "canvas-images";
|
|
||||||
return `attachment; filename="${asciiName}.zip"; filename*=UTF-8''${encodedName}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRequestOrigin(request: NextRequest): string {
|
function getRequestOrigin(request: NextRequest): string {
|
||||||
@ -111,252 +69,154 @@ function toDownloadItems(input: unknown, origin: string): DownloadFileItem[] {
|
|||||||
.slice(0, MAX_DOWNLOAD_FILES);
|
.slice(0, MAX_DOWNLOAD_FILES);
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanupExpiredTasks() {
|
|
||||||
const now = Date.now();
|
|
||||||
const store = getTaskStore();
|
|
||||||
for (const [taskId, task] of store.entries()) {
|
|
||||||
if (task.expiresAt <= now) {
|
|
||||||
store.delete(taskId);
|
|
||||||
if (task.zipFile) void removeDownloadZip(task.zipFile.path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTaskProgress(task: ZipTask) {
|
|
||||||
if (task.status === "completed") {
|
|
||||||
return 100;
|
|
||||||
}
|
|
||||||
if (task.status === "failed") {
|
|
||||||
return Math.max(
|
|
||||||
0,
|
|
||||||
Math.min(100, Math.round((task.processedCount / Math.max(1, task.totalCount)) * 85))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (task.processedCount >= task.totalCount && task.totalCount > 0) {
|
|
||||||
return Math.min(99, 85 + Math.round((task.compressPercent / 100) * 14));
|
|
||||||
}
|
|
||||||
return Math.max(
|
|
||||||
4,
|
|
||||||
Math.min(85, Math.round((task.processedCount / Math.max(1, task.totalCount)) * 85))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function serializeTask(task: ZipTask) {
|
function serializeTask(task: ZipTask) {
|
||||||
const downloadUrl =
|
return { id: task.id, folderName: task.folderName, status: task.status, stage: task.stage,
|
||||||
task.status === "completed"
|
totalCount: task.totalCount, processedCount: task.processedCount, successCount: task.successCount,
|
||||||
? `/api/canvas/download-zip-task?taskId=${encodeURIComponent(task.id)}&download=1`
|
failedCount: task.failedCount, progress: task.status === "completed" ? 100 : Math.min(99,
|
||||||
: "";
|
Math.round(task.processedCount / Math.max(1, task.totalCount) * 85 + task.compressPercent * .14)),
|
||||||
return {
|
error: task.error || "", expiresAt: task.expiresAt,
|
||||||
id: task.id,
|
downloadUrl: task.status === "completed" ? `/api/canvas/download-zip-task?taskId=${task.id}&download=1` : "" };
|
||||||
folderName: task.folderName,
|
|
||||||
status: task.status,
|
|
||||||
stage: task.stage,
|
|
||||||
totalCount: task.totalCount,
|
|
||||||
processedCount: task.processedCount,
|
|
||||||
successCount: task.successCount,
|
|
||||||
failedCount: task.failedCount,
|
|
||||||
progress: getTaskProgress(task),
|
|
||||||
error: task.error || "",
|
|
||||||
downloadUrl,
|
|
||||||
createdAt: task.createdAt,
|
|
||||||
updatedAt: task.updatedAt,
|
|
||||||
expiresAt: task.expiresAt,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
async function processTask(id: string, userId: string, requestOrigin: string, cookie: string) {
|
||||||
async function processZipTask(params: {
|
if (active.has(id) || active.size >= 2) return;
|
||||||
task: ZipTask;
|
active.add(id);
|
||||||
requestOrigin: string;
|
let release: (() => Promise<void>) | null = null;
|
||||||
cookie: string;
|
let task: ZipTask | null = null;
|
||||||
}) {
|
|
||||||
const { task, requestOrigin, cookie } = params;
|
|
||||||
const store = getTaskStore();
|
|
||||||
try {
|
try {
|
||||||
task.status = "running";
|
release = await claimZipTask(id);
|
||||||
task.stage = "正在读取源图";
|
if (!release) return;
|
||||||
task.updatedAt = Date.now();
|
task = await loadZipTask(id, userId);
|
||||||
|
if (!task || task.status === "completed" || task.status === "failed") return;
|
||||||
const zip = new JSZip();
|
if (task.remote) {
|
||||||
const folder = zip.folder(task.folderName) || zip;
|
const { updateRemoteCanvasZip } = await import("@/lib/canvas/remote-zip-client");
|
||||||
const results: Array<
|
await updateRemoteCanvasZip(task, requestOrigin);
|
||||||
| Awaited<ReturnType<typeof fetchCanvasDownloadAsset>>
|
return;
|
||||||
| null
|
|
||||||
> = new Array(task.files.length).fill(null);
|
|
||||||
let cursor = 0;
|
|
||||||
const workerCount = Math.min(MAX_CONCURRENCY, task.files.length);
|
|
||||||
|
|
||||||
await Promise.all(
|
|
||||||
Array.from({ length: workerCount }, async () => {
|
|
||||||
while (true) {
|
|
||||||
const index = cursor;
|
|
||||||
cursor += 1;
|
|
||||||
if (index >= task.files.length) break;
|
|
||||||
|
|
||||||
const file = task.files[index]!;
|
|
||||||
results[index] = await fetchCanvasDownloadAsset({
|
|
||||||
file,
|
|
||||||
index,
|
|
||||||
requestOrigin,
|
|
||||||
cookie,
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = results[index];
|
|
||||||
task.processedCount += 1;
|
|
||||||
if (result?.ok) {
|
|
||||||
task.successCount += 1;
|
|
||||||
} else {
|
|
||||||
task.failedCount += 1;
|
|
||||||
}
|
}
|
||||||
task.stage = `正在处理 ${task.processedCount}/${task.totalCount} 张`;
|
await assertZipDiskSpace();
|
||||||
task.updatedAt = Date.now();
|
const existingZip = task.zipFile && task.contentDisposition && await stat(task.zipFile.path).catch(() => null);
|
||||||
}
|
const reuseZip = existingZip && existingZip.size === task.zipFile!.bytes;
|
||||||
})
|
task.status = "running"; task.stage = reuseZip ? "正在准备加速下载" : "正在准备原图"; task.updatedAt = Date.now();
|
||||||
|
await saveZipTask(task);
|
||||||
|
if (!reuseZip) {
|
||||||
|
await prepareCanvasZip(task, (params) =>
|
||||||
|
fetchCanvasDownloadAsset({ ...params, requestOrigin, cookie })
|
||||||
);
|
);
|
||||||
|
|
||||||
const failures: Array<{ index: number; url: string; name?: string; error: string }> = [];
|
|
||||||
for (const [index, item] of results.entries()) {
|
|
||||||
if (!item || !item.ok) {
|
|
||||||
failures.push({
|
|
||||||
index: index + 1,
|
|
||||||
url: item?.ok === false ? item.url : task.files[index]?.url || "",
|
|
||||||
name: item?.ok === false ? item.name : task.files[index]?.name,
|
|
||||||
error: item?.ok === false ? item.error : "未知错误",
|
|
||||||
});
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
folder.file(item.filename, item.bytes);
|
task.stage = "正在准备下载链接"; await saveZipTask(task);
|
||||||
|
const cdn = await publishCanvasZip({ id: task.id, ...task.zipFile!, disposition: task.contentDisposition!,
|
||||||
|
onProgress: async bytes => {
|
||||||
|
task!.stage = `正在传送至七牛 ${Math.round(bytes / task!.zipFile!.bytes * 100)}%`;
|
||||||
|
task!.updatedAt = Date.now(); await saveZipTask(task!);
|
||||||
|
} });
|
||||||
|
if (cdn) {
|
||||||
|
task.cdnObject = { storageConfigId: cdn.storageConfigId, key: cdn.key };
|
||||||
|
if (!cdn.url) throw new Error(cdn.error || `ZIP 已打包,但加速下载未就绪,请再次点击下载继续传送原 ZIP。下载编号:${task.id}`);
|
||||||
|
task.cdnUrl = cdn.url;
|
||||||
}
|
}
|
||||||
|
task.status = "completed"; task.stage = task.failedCount ? `已打包 ${task.successCount} 张,${task.failedCount} 张未能读取` : "打包完成";
|
||||||
if (failures.length > 0) {
|
task.compressPercent = 100; task.updatedAt = Date.now(); task.expiresAt = Date.now() + TASK_TTL_MS;
|
||||||
folder.file(
|
await saveZipTask(task);
|
||||||
"_download-failures.txt",
|
// The complete ZIP is enough for retries; release staged duplicate bytes.
|
||||||
[
|
await Promise.all(task.files.filter(f => f.prepared).map(f => rm(join(zipTaskPath(id), f.prepared!), { force: true }).catch(() => undefined)));
|
||||||
"以下图片暂时无法下载。可稍后重试,或复制链接检查源图是否仍可访问。",
|
console.info("[CanvasDownloadZipTask] Complete", { taskId: id, count: task.successCount, failed: task.failedCount,
|
||||||
"",
|
bytes: task.zipFile!.bytes, delivery: task.cdnUrl ? "cdn" : "local" });
|
||||||
...failures.map((item) => {
|
|
||||||
const label = item.name ? `${item.index}. ${item.name}` : `${item.index}. image`;
|
|
||||||
return `${label}\nURL: ${item.url}\n原因: ${item.error}`;
|
|
||||||
}),
|
|
||||||
].join("\n\n")
|
|
||||||
);
|
|
||||||
console.warn("[CanvasDownloadZipTask] Partial download failures:", failures);
|
|
||||||
}
|
|
||||||
|
|
||||||
task.stage = "正在生成 ZIP 文件";
|
|
||||||
task.updatedAt = Date.now();
|
|
||||||
task.zipFile = await writeDownloadZip(zip, task.id, percent => {
|
|
||||||
task.compressPercent = Math.max(0, Math.min(100, percent || 0));
|
|
||||||
task.updatedAt = Date.now();
|
|
||||||
});
|
|
||||||
task.contentDisposition = buildContentDisposition(task.folderName);
|
|
||||||
task.status = "completed";
|
|
||||||
task.stage = "打包完成";
|
|
||||||
task.compressPercent = 100;
|
|
||||||
task.updatedAt = Date.now();
|
|
||||||
task.expiresAt = Date.now() + TASK_TTL_MS;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[CanvasDownloadZipTask] Failed:", error);
|
if (task) {
|
||||||
task.status = "failed";
|
task.status = "failed"; task.stage = task.zipFile ? "ZIP 已打包,加速下载未就绪" : "打包未完成";
|
||||||
task.stage = "打包失败";
|
task.error = error instanceof Error ? error.message : "打包失败,请稍后重试";
|
||||||
task.error = error instanceof Error ? error.message : "打包下载失败";
|
task.updatedAt = Date.now(); task.expiresAt = Date.now() + TASK_TTL_MS; await saveZipTask(task).catch(() => undefined);
|
||||||
task.updatedAt = Date.now();
|
|
||||||
task.expiresAt = Date.now() + TASK_TTL_MS;
|
|
||||||
} finally {
|
|
||||||
store.set(task.id, task);
|
|
||||||
}
|
}
|
||||||
|
console.error("[CanvasDownloadZipTask] Preparation failed", { taskId: id, message: task?.error });
|
||||||
|
} finally { await release?.().catch(() => undefined); active.delete(id); }
|
||||||
|
}
|
||||||
|
function schedule(task: ZipTask, request: NextRequest) {
|
||||||
|
const run = () => trackRuntimeWork(() => processTask(task.id, task.userId, getRequestOrigin(request), request.headers.get("cookie") || ""));
|
||||||
|
after(run);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
|
||||||
cleanupExpiredTasks();
|
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
try {
|
||||||
}
|
const body = await request.json();
|
||||||
|
let id = typeof body.taskId === "string" ? body.taskId : randomUUID();
|
||||||
const body = (await request.json().catch(() => ({}))) as Record<string, unknown>;
|
let task = await loadZipTask(id, session.user.id);
|
||||||
const requestOrigin = getRequestOrigin(request);
|
if (!task) {
|
||||||
const files = toDownloadItems(body.files, requestOrigin);
|
await assertZipTaskCapacity(session.user.id);
|
||||||
const folderName = sanitizeFolderName(String(body.folderName || ""));
|
if (await stat(zipTaskPath(id)).catch(() => null)) id = randomUUID();
|
||||||
if (files.length === 0) {
|
const files = toDownloadItems(body.files, getRequestOrigin(request));
|
||||||
return NextResponse.json({ success: false, error: "没有可下载的图片" }, { status: 400 });
|
if (!files.length || !Array.isArray(body.files) || body.files.length > MAX_DOWNLOAD_FILES || files.length !== body.files.length)
|
||||||
}
|
return NextResponse.json({ error: "请选择 1 到 200 张可下载图片" }, { status: 400 });
|
||||||
|
// Exclusive creation prevents an arbitrary taskId from overwriting another owner's manifest.
|
||||||
const task: ZipTask = {
|
await mkdir(zipTaskPath(id), { recursive: false, mode: 0o700 }).catch(async error => {
|
||||||
id: randomUUID(),
|
if (error.code !== "ENOENT") throw error;
|
||||||
userId: session.user.id,
|
await mkdir((await import("@/lib/canvas/download-zip-stream")).downloadZipDirectory, { recursive: true, mode: 0o700 });
|
||||||
folderName,
|
await mkdir(zipTaskPath(id), { mode: 0o700 });
|
||||||
files,
|
|
||||||
totalCount: files.length,
|
|
||||||
processedCount: 0,
|
|
||||||
successCount: 0,
|
|
||||||
failedCount: 0,
|
|
||||||
compressPercent: 0,
|
|
||||||
status: "pending",
|
|
||||||
stage: "等待服务端开始打包",
|
|
||||||
createdAt: Date.now(),
|
|
||||||
updatedAt: Date.now(),
|
|
||||||
expiresAt: Date.now() + TASK_TTL_MS,
|
|
||||||
};
|
|
||||||
getTaskStore().set(task.id, task);
|
|
||||||
|
|
||||||
void processZipTask({
|
|
||||||
task,
|
|
||||||
requestOrigin,
|
|
||||||
cookie: request.headers.get("cookie") || "",
|
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
task: serializeTask(task),
|
|
||||||
});
|
});
|
||||||
|
const now = Date.now();
|
||||||
|
const remote = (body.scope === "style-detail" || body.scope === "batch-instruction") &&
|
||||||
|
process.env.CANVAS_ZIP_WORKER_URL && process.env.CANVAS_ZIP_WORKER_TOKEN
|
||||||
|
? { attempt: 0 } : undefined;
|
||||||
|
task = { id, userId: session.user.id, folderName: sanitizeFolderName(String(body.folderName || "")), files, remote,
|
||||||
|
totalCount: files.length, processedCount: 0, successCount: 0, failedCount: 0, compressPercent: 0,
|
||||||
|
status: "pending", stage: "等待后台准备", createdAt: now, updatedAt: now, expiresAt: now + PREPARATION_TTL_MS };
|
||||||
|
await saveZipTask(task);
|
||||||
|
} else if ((task.status === "failed" || task.status === "completed" && task.cdnObject && !task.cdnUrl) && body.retry === true) {
|
||||||
|
task.status = "pending"; task.error = undefined;
|
||||||
|
await assertZipTaskCapacity(session.user.id);
|
||||||
|
if (task.remote) {
|
||||||
|
task.remote.attempt++;
|
||||||
|
delete task.remote.sources;
|
||||||
|
}
|
||||||
|
if (!task.zipFile) {
|
||||||
|
for (const file of task.files) delete file.error;
|
||||||
|
task.processedCount = task.successCount = task.files.filter(file => file.prepared).length;
|
||||||
|
task.failedCount = 0;
|
||||||
|
}
|
||||||
|
task.updatedAt = Date.now(); task.expiresAt = Date.now() + PREPARATION_TTL_MS;
|
||||||
|
await saveZipTask(task);
|
||||||
|
}
|
||||||
|
if (task.status === "pending" || task.status === "running") schedule(task, request);
|
||||||
|
after(() => cleanupZipTasks(async object => {
|
||||||
|
const { removeCanvasZipObject } = await import("@/lib/storage/canvas-zip-delivery");
|
||||||
|
await removeCanvasZipObject(object);
|
||||||
|
}).catch(() => { console.warn("[CanvasDownloadZipTask] Expired file cleanup deferred"); }));
|
||||||
|
return NextResponse.json({ success: true, task: serializeTask(task) }, { status: 202, headers: { "Cache-Control": "no-store" } });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[CanvasDownloadZipTask] Create failed:", error);
|
console.warn("[CanvasDownloadZipTask] Create rejected", { message: (error as Error).message });
|
||||||
return NextResponse.json({ success: false, error: "创建打包任务失败" }, { status: 500 });
|
return NextResponse.json({ error: "无法创建打包任务,请稍后重试" }, { status: 400 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
try {
|
|
||||||
cleanupExpiredTasks();
|
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
try {
|
||||||
}
|
const id = request.nextUrl.searchParams.get("taskId") || "";
|
||||||
|
const task = await loadZipTask(id, session.user.id);
|
||||||
const taskId = String(request.nextUrl.searchParams.get("taskId") || "").trim();
|
if (!task) return NextResponse.json({ error: "打包任务不存在或已过期,请重新打包" }, { status: 404 });
|
||||||
if (!taskId) {
|
|
||||||
return NextResponse.json({ success: false, error: "缺少任务 ID" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const task = getTaskStore().get(taskId);
|
|
||||||
if (!task || task.userId !== session.user.id) {
|
|
||||||
return NextResponse.json({ success: false, error: "打包任务不存在或已过期" }, { status: 404 });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.nextUrl.searchParams.get("download") === "1") {
|
if (request.nextUrl.searchParams.get("download") === "1") {
|
||||||
if (task.status !== "completed" || !task.zipFile) {
|
if (task.remote && task.status === "completed" && task.cdnUrl)
|
||||||
return NextResponse.json({ success: false, error: "ZIP 文件尚未准备好" }, { status: 409 });
|
return NextResponse.redirect(task.cdnUrl, 307);
|
||||||
|
if (task.status !== "completed" || !task.zipFile) return NextResponse.json({ error: "ZIP 尚未准备好" }, { status: 409 });
|
||||||
|
if (task.cdnUrl && request.nextUrl.searchParams.get("local") !== "1")
|
||||||
|
return NextResponse.redirect(task.cdnUrl, 307);
|
||||||
|
const info = await stat(task.zipFile.path).catch(() => null);
|
||||||
|
if (!info || info.size !== task.zipFile.bytes) return NextResponse.json({ error: "下载文件已过期,请重新打包" }, { status: 410 });
|
||||||
|
const etag = `"${task.id}-${info.size}"`;
|
||||||
|
const headers: Record<string, string> = { "Content-Type": "application/zip", "Content-Disposition": task.contentDisposition!,
|
||||||
|
"Accept-Ranges": "bytes", "ETag": etag, "Cache-Control": "private, no-store",
|
||||||
|
"X-Download-Success-Count": String(task.successCount), "X-Download-Failed-Count": String(task.failedCount) };
|
||||||
|
const ifRange = request.headers.get("if-range");
|
||||||
|
let range;
|
||||||
|
try { range = parseDownloadByteRange(!ifRange || ifRange === etag ? request.headers.get("range") : null, info.size); }
|
||||||
|
catch { return new NextResponse(null, { status: 416, headers: { ...headers, "Content-Range": `bytes */${info.size}` } }); }
|
||||||
|
headers["Content-Length"] = String(range ? range.end - range.start + 1 : info.size);
|
||||||
|
if (range) headers["Content-Range"] = `bytes ${range.start}-${range.end}/${info.size}`;
|
||||||
|
return new NextResponse(readDownloadZip(task.zipFile.path, range || undefined), { status: range ? 206 : 200, headers });
|
||||||
}
|
}
|
||||||
return new NextResponse(readDownloadZip(task.zipFile.path), {
|
if (task.status === "pending" || task.status === "running") schedule(task, request);
|
||||||
status: 200,
|
return NextResponse.json({ success: true, task: serializeTask(task) }, { headers: { "Cache-Control": "no-store" } });
|
||||||
headers: {
|
} catch {
|
||||||
"Content-Type": "application/zip",
|
return NextResponse.json({ error: "查询打包任务失败,请稍后重试" }, { status: 500 });
|
||||||
"Content-Disposition": task.contentDisposition || buildContentDisposition(task.folderName),
|
|
||||||
"Content-Length": String(task.zipFile.bytes),
|
|
||||||
"Cache-Control": "no-store",
|
|
||||||
"X-Download-Success-Count": String(task.successCount),
|
|
||||||
"X-Download-Failed-Count": String(task.failedCount),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
task: serializeTask(task),
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[CanvasDownloadZipTask] Query failed:", error);
|
|
||||||
return NextResponse.json({ success: false, error: "查询打包任务失败" }, { status: 500 });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import { executeCanvasImageResponse } from "@/lib/canvas/generate-image-response
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { getCanvasImageTaskForUser } from "@/lib/canvas/image-task-queue";
|
import { getCanvasImageTaskForUser } from "@/lib/canvas/image-task-queue";
|
||||||
|
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
||||||
|
|
||||||
export const maxDuration = 1200;
|
export const maxDuration = 1200;
|
||||||
|
|
||||||
@ -23,13 +24,15 @@ export async function GET(request: Request) {
|
|||||||
return NextResponse.json({ success: false, error: "任务不存在" }, { status: 404 });
|
return NextResponse.json({ success: false, error: "任务不存在" }, { status: 404 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const failure = task.status === "failed" ? getRequestFailureDetails(task.error) : undefined;
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
task: {
|
task: {
|
||||||
id: task.id,
|
id: task.id,
|
||||||
status: task.status,
|
status: task.status,
|
||||||
attempts: task.attempts,
|
attempts: task.attempts,
|
||||||
error: task.error || null,
|
error: failure ? formatRequestFailure(failure, task.id) : null,
|
||||||
|
failure,
|
||||||
result: task.result || null,
|
result: task.result || null,
|
||||||
createdAt: task.createdAt,
|
createdAt: task.createdAt,
|
||||||
startedAt: task.startedAt,
|
startedAt: task.startedAt,
|
||||||
|
|||||||
@ -976,7 +976,8 @@ export async function POST(request: NextRequest) {
|
|||||||
const annotatedFilename = `canvas-local-edit-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.png`;
|
const annotatedFilename = `canvas-local-edit-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.png`;
|
||||||
const annotatedUpload = await uploadFile(
|
const annotatedUpload = await uploadFile(
|
||||||
annotatedFilename,
|
annotatedFilename,
|
||||||
toArrayBuffer(annotatedBuffer)
|
toArrayBuffer(annotatedBuffer),
|
||||||
|
{ preferQiniuForm: true }
|
||||||
);
|
);
|
||||||
const prompt = buildLocalEditPrompt(regions);
|
const prompt = buildLocalEditPrompt(regions);
|
||||||
const manualRegionReferenceImageUrls = regions.flatMap((region) =>
|
const manualRegionReferenceImageUrls = regions.flatMap((region) =>
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { and, eq, inArray } from "drizzle-orm";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { callQwen3Vl } from "@/lib/ai/qwen3-vl";
|
import { callQwen3Vl } from "@/lib/ai/qwen3-vl";
|
||||||
|
import { executeConfiguredVip98kTask, getVip98kRuntimeProviders } from "@/lib/ai/vip98k-runtime";
|
||||||
|
import { db } from "@/lib/db";
|
||||||
|
import { aiModel } from "@/lib/db/schema";
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
|
|
||||||
@ -90,6 +94,39 @@ function fallbackItemsFromRaw(raw: string): OcrItem[] {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function detectTextWithVip98k(imageUrl: string): Promise<OcrItem[] | null> {
|
||||||
|
const providers = await getVip98kRuntimeProviders();
|
||||||
|
if (!providers.length) return null;
|
||||||
|
|
||||||
|
const model = await db.query.aiModel.findFirst({
|
||||||
|
columns: { providerId: true },
|
||||||
|
where: and(
|
||||||
|
inArray(aiModel.providerId, providers.map(({ id }) => id)),
|
||||||
|
eq(aiModel.modelId, "ocr"),
|
||||||
|
eq(aiModel.isEnabled, true),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
if (!model) return null;
|
||||||
|
|
||||||
|
const result = await executeConfiguredVip98kTask({
|
||||||
|
modelId: "ocr",
|
||||||
|
kind: "image-tool",
|
||||||
|
imageUrls: [imageUrl],
|
||||||
|
preferredProviderId: model.providerId,
|
||||||
|
});
|
||||||
|
const textUrl = result.outputUrls[0];
|
||||||
|
if (!textUrl) throw new Error("98K 文字识别未返回文本文件");
|
||||||
|
|
||||||
|
const response = await fetch(textUrl, { signal: AbortSignal.timeout(15_000) });
|
||||||
|
const type = response.headers.get("content-type") || "";
|
||||||
|
if (!response.ok || !/^(?:text\/plain|application\/octet-stream)(?:;|$)/i.test(type)) {
|
||||||
|
await response.body?.cancel();
|
||||||
|
throw new Error(`无法读取 98K 文字识别结果(HTTP ${response.status})`);
|
||||||
|
}
|
||||||
|
return (await response.text()).split(/\r\n?|\n/).map((line) => line.trim())
|
||||||
|
.filter(Boolean).slice(0, 50).map((text, index) => ({ id: String(index + 1), text }));
|
||||||
|
}
|
||||||
|
|
||||||
function shouldUseDataUrlFallback(error: unknown) {
|
function shouldUseDataUrlFallback(error: unknown) {
|
||||||
const message =
|
const message =
|
||||||
error instanceof Error ? error.message.toLowerCase() : String(error || "").toLowerCase();
|
error instanceof Error ? error.message.toLowerCase() : String(error || "").toLowerCase();
|
||||||
@ -147,6 +184,11 @@ export async function POST(request: Request) {
|
|||||||
return NextResponse.json({ success: false, error: "仅支持 URL 图片,不允许 base64 传输" }, { status: 400 });
|
return NextResponse.json({ success: false, error: "仅支持 URL 图片,不允许 base64 传输" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const detected = await detectTextWithVip98k(imageUrl);
|
||||||
|
if (detected !== null) {
|
||||||
|
return NextResponse.json({ success: true, data: { items: detected, model: "ocr" } });
|
||||||
|
}
|
||||||
|
|
||||||
logger.debug("model_selected", { model: "qwen3-vl" });
|
logger.debug("model_selected", { model: "qwen3-vl" });
|
||||||
const prompt = [
|
const prompt = [
|
||||||
"你是图片文字识别助手。",
|
"你是图片文字识别助手。",
|
||||||
@ -191,15 +233,9 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const parsedJson = parseJsonText(text || "");
|
const parsedJson = parseJsonText(text || "");
|
||||||
let items: OcrItem[] = [];
|
const items = Array.isArray(parsedJson?.items)
|
||||||
|
? sanitizeItems(parsedJson.items)
|
||||||
if (parsedJson && Array.isArray(parsedJson.items)) {
|
: fallbackItemsFromRaw(text);
|
||||||
items = sanitizeItems(parsedJson.items);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (items.length === 0 && text) {
|
|
||||||
items = fallbackItemsFromRaw(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
@ -213,7 +249,7 @@ export async function POST(request: Request) {
|
|||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
success: false,
|
success: false,
|
||||||
error: error instanceof Error ? error.message : "文字识别失败",
|
error: "图片文字识别暂不可用,请稍后重试",
|
||||||
},
|
},
|
||||||
{ status: 500 },
|
{ status: 500 },
|
||||||
);
|
);
|
||||||
|
|||||||
78
app/api/canvas/video-agent/route.ts
Normal file
78
app/api/canvas/video-agent/route.ts
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import { getChatById, getMessagesByChatId } from "@/lib/db/queries";
|
||||||
|
import {
|
||||||
|
readOwnedVideoCanvas,
|
||||||
|
saveVideoAgentCanvas,
|
||||||
|
} from "@/lib/ai/canvas-agent-runtime/video-store";
|
||||||
|
import { AgentRunError } from "@/lib/ai/canvas-agent-runtime/store";
|
||||||
|
import { publicAgentError } from "@/lib/ai/canvas-agent-runtime/protocol";
|
||||||
|
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
|
try {
|
||||||
|
const projectId = z.string().uuid().parse(new URL(request.url).searchParams.get("projectId"));
|
||||||
|
await readOwnedVideoCanvas(session.user.id, projectId);
|
||||||
|
// One stable conversation per video workspace survives browser reloads.
|
||||||
|
const chat = await getChatById({ id: projectId });
|
||||||
|
if (chat && chat.userId !== session.user.id) throw new AgentRunError("对话不存在", 404);
|
||||||
|
const messages = chat ? (await getMessagesByChatId({ id: projectId })).slice(-40) : [];
|
||||||
|
return NextResponse.json(
|
||||||
|
{ messages: messages.map(({ id, role, parts }) => ({ id, role, parts })) },
|
||||||
|
{ headers: { "Cache-Control": "private, no-store" } },
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: publicAgentError(error) },
|
||||||
|
{
|
||||||
|
status:
|
||||||
|
error instanceof AgentRunError ? error.status : error instanceof z.ZodError ? 400 : 503,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const saveSchema = z.object({
|
||||||
|
projectId: z.string().uuid(),
|
||||||
|
baseRevision: z.string().datetime(),
|
||||||
|
canvasData: z.object({
|
||||||
|
nodes: z.array(z.record(z.unknown())).max(2000),
|
||||||
|
edges: z
|
||||||
|
.array(
|
||||||
|
z.object({
|
||||||
|
id: z.string(),
|
||||||
|
source: z.string(),
|
||||||
|
target: z.string(),
|
||||||
|
type: z.string().optional(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.max(4000),
|
||||||
|
selectedNodeIds: z.array(z.string()).max(20),
|
||||||
|
viewport: z.object({
|
||||||
|
x: z.number().finite(),
|
||||||
|
y: z.number().finite(),
|
||||||
|
zoom: z.number().positive(),
|
||||||
|
}),
|
||||||
|
tool: z.literal("select"),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||||
|
try {
|
||||||
|
const body = saveSchema.parse(await request.json());
|
||||||
|
await saveVideoAgentCanvas(session.user.id, body.projectId, body.baseRevision, body.canvasData);
|
||||||
|
return NextResponse.json({ success: true });
|
||||||
|
} catch (error) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: publicAgentError(error) },
|
||||||
|
{
|
||||||
|
status:
|
||||||
|
error instanceof AgentRunError ? error.status : error instanceof z.ZodError ? 400 : 503,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,8 @@
|
|||||||
import { NextRequest } from "next/server";
|
import { NextRequest } from "next/server";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { generatedImage, user, aiModel } from "@/lib/db/schema";
|
import { generatedImage, user, aiModel } from "@/lib/db/schema";
|
||||||
import { eq, desc, sql } from "drizzle-orm";
|
import { and, eq, desc, sql } from "drizzle-orm";
|
||||||
|
import { isRetiredStorageUrl, RETIRED_STORAGE_URL_PATTERN } from "@/lib/storage/retired-storage";
|
||||||
import { cachedPublicJson } from "@/lib/public-api/performance";
|
import { cachedPublicJson } from "@/lib/public-api/performance";
|
||||||
|
|
||||||
const FEATURED_TIMEOUT_MS = 1800;
|
const FEATURED_TIMEOUT_MS = 1800;
|
||||||
@ -57,13 +58,17 @@ export async function GET(request: NextRequest) {
|
|||||||
userName: user.name,
|
userName: user.name,
|
||||||
userEmail: user.email,
|
userEmail: user.email,
|
||||||
userDepartment: user.department,
|
userDepartment: user.department,
|
||||||
modelName: aiModel.displayName,
|
modelName: sql<string | null>`(select ${aiModel.displayName} from ${aiModel}
|
||||||
|
where ${aiModel.modelId} = ${generatedImage.modelId}
|
||||||
|
order by ${aiModel.isEnabled} desc, ${aiModel.createdAt} desc, ${aiModel.id} asc limit 1)`,
|
||||||
totalCount: sql<number>`count(*) over()::int`,
|
totalCount: sql<number>`count(*) over()::int`,
|
||||||
})
|
})
|
||||||
.from(generatedImage)
|
.from(generatedImage)
|
||||||
.leftJoin(user, eq(generatedImage.userId, user.id))
|
.leftJoin(user, eq(generatedImage.userId, user.id))
|
||||||
.leftJoin(aiModel, eq(generatedImage.modelId, aiModel.modelId))
|
.where(and(
|
||||||
.where(eq(generatedImage.isFeatured, true))
|
eq(generatedImage.isFeatured, true),
|
||||||
|
sql`${generatedImage.originalUrl} !~* ${RETIRED_STORAGE_URL_PATTERN}`,
|
||||||
|
))
|
||||||
.orderBy(desc(generatedImage.featuredAt))
|
.orderBy(desc(generatedImage.featuredAt))
|
||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
.offset(offset);
|
.offset(offset);
|
||||||
@ -74,7 +79,7 @@ export async function GET(request: NextRequest) {
|
|||||||
return {
|
return {
|
||||||
images: featuredImages.map((img) => ({
|
images: featuredImages.map((img) => ({
|
||||||
id: img.id,
|
id: img.id,
|
||||||
thumbnailUrl: img.thumbnailUrl,
|
thumbnailUrl: isRetiredStorageUrl(img.thumbnailUrl) ? img.originalUrl : img.thumbnailUrl,
|
||||||
originalUrl: img.originalUrl,
|
originalUrl: img.originalUrl,
|
||||||
imageSize: img.imageSize,
|
imageSize: img.imageSize,
|
||||||
prompt: img.prompt,
|
prompt: img.prompt,
|
||||||
|
|||||||
7
app/api/feedback/route.ts
Normal file
7
app/api/feedback/route.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { feedbackGet, feedbackPost } from "@/lib/canvas/feedback-route";
|
||||||
|
|
||||||
|
export const runtime = "nodejs";
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
export const POST = (request: Request) => feedbackPost(request, null);
|
||||||
|
export const GET = (request: Request) => feedbackGet(request, null);
|
||||||
@ -1,7 +1,12 @@
|
|||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
|
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { formatDownloadFailure, getDownloadFailure } from "@/lib/canvas/download-error";
|
||||||
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
|
import { buildImageProxyUrl, isAlibabaModelImageUrl } from "@/lib/image-delivery";
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
|
const logger = createLogger("image.download");
|
||||||
|
|
||||||
const DEFAULT_QUALITY = 82;
|
const DEFAULT_QUALITY = 82;
|
||||||
const MAX_WIDTH = 4096;
|
const MAX_WIDTH = 4096;
|
||||||
@ -9,6 +14,7 @@ const MIN_WIDTH = 64;
|
|||||||
const MIN_QUALITY = 40;
|
const MIN_QUALITY = 40;
|
||||||
const MAX_QUALITY = 100;
|
const MAX_QUALITY = 100;
|
||||||
const FETCH_TIMEOUT_MS = 15000;
|
const FETCH_TIMEOUT_MS = 15000;
|
||||||
|
const SMALL_PREVIEW_FETCH_TIMEOUT_MS = 45000;
|
||||||
const DEFAULT_MEMORY_CACHE_MAX_BYTES = 64 * 1024 * 1024;
|
const DEFAULT_MEMORY_CACHE_MAX_BYTES = 64 * 1024 * 1024;
|
||||||
const DEFAULT_MEMORY_CACHE_MAX_ITEMS = 256;
|
const DEFAULT_MEMORY_CACHE_MAX_ITEMS = 256;
|
||||||
|
|
||||||
@ -230,6 +236,7 @@ function resolveDirectImageTarget(
|
|||||||
requestedQuality: number | null,
|
requestedQuality: number | null,
|
||||||
requestedFormat: string | null
|
requestedFormat: string | null
|
||||||
): URL | null {
|
): URL | null {
|
||||||
|
if (isAlibabaModelImageUrl(target.toString())) return null;
|
||||||
if (!isBrowserImageRequest(request)) {
|
if (!isBrowserImageRequest(request)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -258,7 +265,19 @@ function redirectToDirectImage(target: URL): NextResponse {
|
|||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const { searchParams } = request.nextUrl;
|
const { searchParams } = request.nextUrl;
|
||||||
|
const requestId = crypto.randomUUID();
|
||||||
|
const downloadFailure = (error: unknown) => {
|
||||||
|
const failure = getDownloadFailure(error);
|
||||||
|
logger.warn("original_download_failed", error, { requestId, code: failure.code, status: failure.status });
|
||||||
|
return NextResponse.json({ error: formatDownloadFailure(failure, requestId), failure, requestId }, {
|
||||||
|
status: failure.status, headers: { "Cache-Control": "no-store" },
|
||||||
|
});
|
||||||
|
};
|
||||||
const rawUrl = (searchParams.get("url") || "").trim();
|
const rawUrl = (searchParams.get("url") || "").trim();
|
||||||
|
if (isRetiredStorageUrl(rawUrl)) {
|
||||||
|
if (searchParams.get("original") === "1") return downloadFailure(RETIRED_STORAGE_MESSAGE);
|
||||||
|
return NextResponse.json({ error: RETIRED_STORAGE_MESSAGE, code: "storage_retired" }, { status: 410 });
|
||||||
|
}
|
||||||
if (!rawUrl) {
|
if (!rawUrl) {
|
||||||
return NextResponse.json({ error: "Missing url parameter" }, { status: 400 });
|
return NextResponse.json({ error: "Missing url parameter" }, { status: 400 });
|
||||||
}
|
}
|
||||||
@ -274,6 +293,24 @@ export async function GET(request: NextRequest) {
|
|||||||
return NextResponse.json({ error: "Only http/https is supported" }, { status: 400 });
|
return NextResponse.json({ error: "Only http/https is supported" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (normalizeLegacyAliyunStorageUrl(target.toString())) {
|
||||||
|
try {
|
||||||
|
if (searchParams.get("original") === "1") {
|
||||||
|
const { buildImageProxyUrl } = await import("@/lib/image-delivery");
|
||||||
|
buildImageProxyUrl(target.toString(), { delivery: "original" });
|
||||||
|
}
|
||||||
|
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
||||||
|
target = new URL(await resolveLegacyAliyunStorageUrl(target.toString()));
|
||||||
|
} catch (error) {
|
||||||
|
if (searchParams.get("original") === "1") return downloadFailure(error);
|
||||||
|
const failure = getDownloadFailure(error);
|
||||||
|
return NextResponse.json({ error: formatDownloadFailure(failure, requestId), failure, requestId,
|
||||||
|
code: failure.code === "STORAGE_RETIRED" ? "storage_retired" : failure.code }, {
|
||||||
|
status: failure.status, headers: { "Cache-Control": "no-store" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isBlockedHost(target.hostname)) {
|
if (isBlockedHost(target.hostname)) {
|
||||||
return NextResponse.json({ error: "Blocked target host" }, { status: 403 });
|
return NextResponse.json({ error: "Blocked target host" }, { status: 403 });
|
||||||
}
|
}
|
||||||
@ -285,8 +322,60 @@ export async function GET(request: NextRequest) {
|
|||||||
const directQuality = requestedQuality ? clamp(requestedQuality, MIN_QUALITY, MAX_QUALITY) : null;
|
const directQuality = requestedQuality ? clamp(requestedQuality, MIN_QUALITY, MAX_QUALITY) : null;
|
||||||
const requestedFormat = searchParams.get("f");
|
const requestedFormat = searchParams.get("f");
|
||||||
const forceProxy = searchParams.get("proxy") === "1";
|
const forceProxy = searchParams.get("proxy") === "1";
|
||||||
|
const original = searchParams.get("original") === "1";
|
||||||
|
|
||||||
if (!forceProxy) {
|
if (original && (requestedWidth || requestedQuality || requestedFormat)) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Original images cannot be transformed" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!original && !forceProxy && isBrowserImageRequest(request)) {
|
||||||
|
const direct = buildImageProxyUrl(target.toString(), {
|
||||||
|
width: width || undefined, quality: directQuality || undefined,
|
||||||
|
format: ["webp", "avif", "jpeg", "png"].includes(requestedFormat || "") ? requestedFormat as OutputFormat : undefined,
|
||||||
|
});
|
||||||
|
if (process.env.NEXT_PUBLIC_CANVAS_CDN_ORIGIN && /^https:\/\//.test(direct) &&
|
||||||
|
new URL(direct).origin === new URL(process.env.NEXT_PUBLIC_CANVAS_CDN_ORIGIN).origin) {
|
||||||
|
return redirectToDirectImage(new URL(direct));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!original && (isAlibabaModelImageUrl(target.toString()) || (!forceProxy && isBrowserImageRequest(request)))) {
|
||||||
|
try {
|
||||||
|
const { getArchivedImageUrlMappings } = await import("@/lib/storage/image-archive-db");
|
||||||
|
const mappings = await getArchivedImageUrlMappings([target.toString()]);
|
||||||
|
const archived = mappings[target.toString()];
|
||||||
|
if (archived) {
|
||||||
|
const preview = buildImageProxyUrl(archived, {
|
||||||
|
width: width || undefined,
|
||||||
|
quality,
|
||||||
|
format: ["webp", "avif", "jpeg", "png"].includes(requestedFormat || "") ? requestedFormat as OutputFormat : undefined,
|
||||||
|
});
|
||||||
|
if (/^https?:\/\//i.test(preview)) return redirectToDirectImage(new URL(preview));
|
||||||
|
target = new URL(archived);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn("preview_archive_lookup_failed", error, { requestId });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (original && searchParams.get("download") === "resolve") {
|
||||||
|
try {
|
||||||
|
const { buildImageProxyUrl } = await import("@/lib/image-delivery");
|
||||||
|
buildImageProxyUrl(target.toString(), { delivery: "original" });
|
||||||
|
const { resolveOriginalCdnDownload } = await import("@/lib/storage/original-image-download");
|
||||||
|
const direct = await resolveOriginalCdnDownload(target.toString(), searchParams.get("name") || "", request.signal);
|
||||||
|
if (!direct) throw new Error("下载文件暂时无法从存储桶直接下载");
|
||||||
|
return NextResponse.json(direct, { status: direct.delivery === "preparing" ? 202 : 200,
|
||||||
|
headers: { "Cache-Control": "no-store", ...(direct.delivery === "preparing" ? { "Retry-After": "3" } : {}) } });
|
||||||
|
} catch (error) {
|
||||||
|
return downloadFailure(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!forceProxy && !original) {
|
||||||
const directImageTarget = resolveDirectImageTarget(
|
const directImageTarget = resolveDirectImageTarget(
|
||||||
request,
|
request,
|
||||||
target,
|
target,
|
||||||
@ -299,10 +388,12 @@ export async function GET(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheKey = `${target.toString()}|${width || "auto"}|${quality}|${requestedFormat || "auto"}`;
|
const cacheKey = original
|
||||||
|
? `${target.toString()}|original-v1`
|
||||||
|
: `${target.toString()}|${width || "auto"}|${quality}|${requestedFormat || "auto"}`;
|
||||||
const etag = buildEtag(cacheKey);
|
const etag = buildEtag(cacheKey);
|
||||||
const ifNoneMatch = request.headers.get("if-none-match");
|
const ifNoneMatch = request.headers.get("if-none-match");
|
||||||
if (ifNoneMatch && ifNoneMatch === etag) {
|
if (!original && ifNoneMatch && ifNoneMatch === etag) {
|
||||||
return new NextResponse(null, {
|
return new NextResponse(null, {
|
||||||
status: 304,
|
status: 304,
|
||||||
headers: {
|
headers: {
|
||||||
@ -312,7 +403,7 @@ export async function GET(request: NextRequest) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const cached = getCachedImageResponse(cacheKey);
|
const cached = original ? null : getCachedImageResponse(cacheKey);
|
||||||
if (cached) {
|
if (cached) {
|
||||||
return buildImageResponse(cached.body, {
|
return buildImageResponse(cached.body, {
|
||||||
contentType: cached.contentType,
|
contentType: cached.contentType,
|
||||||
@ -322,32 +413,82 @@ export async function GET(request: NextRequest) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fetchSignal = AbortSignal.any([
|
||||||
|
request.signal,
|
||||||
|
AbortSignal.timeout(original ? 60_000 : isAlibabaModelImageUrl(target.toString()) || width && width <= 256 ? SMALL_PREVIEW_FETCH_TIMEOUT_MS : FETCH_TIMEOUT_MS),
|
||||||
|
]);
|
||||||
|
const upstreamFailure = (error: unknown) => {
|
||||||
|
if (request.signal.aborted) return new NextResponse(null, { status: 499 });
|
||||||
|
if (original) return downloadFailure(error);
|
||||||
|
const timedOut = (error instanceof Error && error.name === "TimeoutError") || fetchSignal.reason?.name === "TimeoutError";
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: timedOut ? "读取源图片超时,请重试" : "源图片传输中断,请重试" },
|
||||||
|
{ status: timedOut ? 504 : 502 },
|
||||||
|
);
|
||||||
|
};
|
||||||
|
let contentType: string;
|
||||||
|
let originalBuffer: Buffer;
|
||||||
|
if (!original && isAlibabaModelImageUrl(target.toString())) {
|
||||||
|
try {
|
||||||
|
const { fetchImageBytes } = await import("@/lib/storage/image-byte-cache");
|
||||||
|
const data = await fetchImageBytes(target.toString(), { timeoutMs: SMALL_PREVIEW_FETCH_TIMEOUT_MS });
|
||||||
|
originalBuffer = data.buffer;
|
||||||
|
contentType = data.contentType;
|
||||||
|
if (request.signal.aborted) return new NextResponse(null, { status: 499 });
|
||||||
|
if (!contentType.toLowerCase().startsWith("image/")) {
|
||||||
|
return NextResponse.json({ error: "Upstream is not an image" }, { status: 400 });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return upstreamFailure(error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
let upstreamResponse: Response;
|
let upstreamResponse: Response;
|
||||||
try {
|
try {
|
||||||
upstreamResponse = await fetch(target.toString(), {
|
upstreamResponse = await fetch(target.toString(), {
|
||||||
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
signal: fetchSignal,
|
||||||
headers: {
|
headers: {
|
||||||
Accept: "image/avif,image/webp,image/apng,image/*,*/*;q=0.8",
|
Accept: original ? "*/*" : "image/avif,image/webp,image/apng,image/*,*/*;q=0.8",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[ImageProxy] Upstream fetch failed:", error);
|
return upstreamFailure(error);
|
||||||
return NextResponse.json({ error: "Failed to fetch upstream image" }, { status: 502 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!upstreamResponse.ok) {
|
if (!upstreamResponse.ok) {
|
||||||
|
if (original) return downloadFailure(`HTTP ${upstreamResponse.status}`);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: `Upstream responded with ${upstreamResponse.status}` },
|
{ error: `Upstream responded with ${upstreamResponse.status}` },
|
||||||
{ status: 502 }
|
{ status: 502 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
contentType = upstreamResponse.headers.get("content-type") || "application/octet-stream";
|
||||||
const contentType = upstreamResponse.headers.get("content-type") || "application/octet-stream";
|
|
||||||
if (!contentType.toLowerCase().startsWith("image/")) {
|
if (!contentType.toLowerCase().startsWith("image/")) {
|
||||||
|
if (original) return downloadFailure("Source is not an image");
|
||||||
return NextResponse.json({ error: "Upstream is not an image" }, { status: 400 });
|
return NextResponse.json({ error: "Upstream is not an image" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
if (original) {
|
||||||
const originalBuffer = Buffer.from(await upstreamResponse.arrayBuffer());
|
if (upstreamResponse.status !== 200 || !upstreamResponse.body) {
|
||||||
|
await upstreamResponse.body?.cancel();
|
||||||
|
return downloadFailure("源图片传输中断,请重试");
|
||||||
|
}
|
||||||
|
const headers = new Headers({
|
||||||
|
"Content-Type": contentType,
|
||||||
|
"Content-Disposition": "attachment",
|
||||||
|
"Cache-Control": "private, no-store",
|
||||||
|
"X-Content-Type-Options": "nosniff",
|
||||||
|
"Content-Security-Policy": "default-src 'none'; sandbox",
|
||||||
|
});
|
||||||
|
const length = upstreamResponse.headers.get("content-length");
|
||||||
|
const encoding = upstreamResponse.headers.get("content-encoding");
|
||||||
|
if (length && (!encoding || encoding.toLowerCase() === "identity")) headers.set("Content-Length", length);
|
||||||
|
return new NextResponse(upstreamResponse.body, { headers });
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
originalBuffer = Buffer.from(await upstreamResponse.arrayBuffer());
|
||||||
|
} catch (error) {
|
||||||
|
return upstreamFailure(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
const isSvg = contentType.toLowerCase().includes("image/svg");
|
const isSvg = contentType.toLowerCase().includes("image/svg");
|
||||||
const isGif = contentType.toLowerCase().includes("image/gif");
|
const isGif = contentType.toLowerCase().includes("image/gif");
|
||||||
|
|
||||||
|
|||||||
@ -114,7 +114,7 @@ export async function POST(request: NextRequest) {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
temperature: 0.2,
|
temperature: 0.2,
|
||||||
maxTokens: 1200,
|
maxOutputTokens: 1200,
|
||||||
} as any);
|
} as any);
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import { preprocessProductImageWithRmbg } from "@/lib/scene-fusion/preprocess-product";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
@ -69,7 +70,6 @@ const SCENE_FUSION_REQUEST_STAGGER_MS = 350;
|
|||||||
const SCENE_FUSION_AGENT_TIMEOUT_MS = 20000;
|
const SCENE_FUSION_AGENT_TIMEOUT_MS = 20000;
|
||||||
const SCENE_FUSION_AGENT_MAX_WORKERS = 3;
|
const SCENE_FUSION_AGENT_MAX_WORKERS = 3;
|
||||||
const SCENE_FUSION_PROCESS_MAX_WORKERS = 4;
|
const SCENE_FUSION_PROCESS_MAX_WORKERS = 4;
|
||||||
const SCENE_FUSION_PRE_RMBG_TIMEOUT_MS = 60_000;
|
|
||||||
const SCENE_FUSION_ECHO_HASH_TIMEOUT_MS = 20_000;
|
const SCENE_FUSION_ECHO_HASH_TIMEOUT_MS = 20_000;
|
||||||
const SCENE_FUSION_ECHO_HASH_THRESHOLD = 8;
|
const SCENE_FUSION_ECHO_HASH_THRESHOLD = 8;
|
||||||
const SCENE_FUSION_REVIEW_TIMEOUT_MS = 25_000;
|
const SCENE_FUSION_REVIEW_TIMEOUT_MS = 25_000;
|
||||||
@ -236,41 +236,6 @@ function isSceneFusionReviewRejectError(message: string): boolean {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildPreRmbgCandidateUrls(request: Request): string[] {
|
|
||||||
const urls: string[] = [];
|
|
||||||
const requestUrl = new URL(request.url);
|
|
||||||
const requestHost = requestUrl.hostname.toLowerCase();
|
|
||||||
const requestProtocol = requestUrl.protocol.replace(":", "");
|
|
||||||
if (!(requestProtocol === "https" && (requestHost === "localhost" || requestHost === "127.0.0.1"))) {
|
|
||||||
urls.push(new URL("/api/rmbg", request.url).toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
const host = request.headers.get("host")?.trim();
|
|
||||||
const protoHint = request.headers
|
|
||||||
.get("x-forwarded-proto")
|
|
||||||
?.split(",")[0]
|
|
||||||
?.trim()
|
|
||||||
.toLowerCase();
|
|
||||||
const protocol = protoHint === "https" || protoHint === "http" ? protoHint : requestUrl.protocol.replace(":", "");
|
|
||||||
|
|
||||||
if (host) {
|
|
||||||
const normalizedHost = host.split(":")[0]?.toLowerCase() || "";
|
|
||||||
const isLocalHost = normalizedHost === "localhost" || normalizedHost === "127.0.0.1";
|
|
||||||
if (!(protocol === "https" && isLocalHost)) {
|
|
||||||
urls.push(`${protocol}://${host}/api/rmbg`);
|
|
||||||
}
|
|
||||||
if (isLocalHost) {
|
|
||||||
urls.push(`http://${host}/api/rmbg`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const localPort = requestUrl.port || process.env.PORT || "3125";
|
|
||||||
urls.push(`http://127.0.0.1:${localPort}/api/rmbg`);
|
|
||||||
urls.push(`http://localhost:${localPort}/api/rmbg`);
|
|
||||||
|
|
||||||
return Array.from(new Set(urls));
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeUrlForCompare(input: string): string {
|
function normalizeUrlForCompare(input: string): string {
|
||||||
try {
|
try {
|
||||||
const parsed = new URL(input);
|
const parsed = new URL(input);
|
||||||
@ -988,102 +953,6 @@ async function getSceneFusionConfig() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function preprocessProductImageWithRmbg(
|
|
||||||
request: Request,
|
|
||||||
requestId: string,
|
|
||||||
productImageUrl: string,
|
|
||||||
preRmbgEnabled: boolean
|
|
||||||
): Promise<{
|
|
||||||
productImageUrl: string;
|
|
||||||
usedPreRmbg: boolean;
|
|
||||||
provider?: string;
|
|
||||||
reason?: string;
|
|
||||||
}> {
|
|
||||||
if (!preRmbgEnabled) {
|
|
||||||
return {
|
|
||||||
productImageUrl,
|
|
||||||
usedPreRmbg: false,
|
|
||||||
reason: "disabled_by_config",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const cookie = request.headers.get("cookie");
|
|
||||||
if (!cookie) {
|
|
||||||
return {
|
|
||||||
productImageUrl,
|
|
||||||
usedPreRmbg: false,
|
|
||||||
reason: "missing_cookie",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const candidateUrls = buildPreRmbgCandidateUrls(request);
|
|
||||||
let lastReason = "no_candidate_url";
|
|
||||||
|
|
||||||
logger.info("pre_rmbg_started", {
|
|
||||||
requestId,
|
|
||||||
candidateCount: candidateUrls.length,
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const rmbgUrl of candidateUrls) {
|
|
||||||
const controller = new AbortController();
|
|
||||||
const timeoutId = setTimeout(() => controller.abort(), SCENE_FUSION_PRE_RMBG_TIMEOUT_MS);
|
|
||||||
try {
|
|
||||||
const response = await fetch(rmbgUrl, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
cookie,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
imageUrl: productImageUrl,
|
|
||||||
prompt: "场景裂变预处理-抠图",
|
|
||||||
skipGallerySave: true,
|
|
||||||
}),
|
|
||||||
signal: controller.signal,
|
|
||||||
});
|
|
||||||
|
|
||||||
const payload = await response.json().catch(() => null) as Record<string, unknown> | null;
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
lastReason = `request_failed_${response.status}`;
|
|
||||||
logger.warn("pre_rmbg_candidate_failed", {
|
|
||||||
requestId,
|
|
||||||
status: response.status,
|
|
||||||
detail: payload?.detail || payload?.error || "unknown",
|
|
||||||
});
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const cleanedUrl = String(payload?.original_url || payload?.thumbnail_url || "").trim();
|
|
||||||
if (!cleanedUrl || cleanedUrl === productImageUrl) {
|
|
||||||
lastReason = cleanedUrl ? "same_as_source" : "empty_result";
|
|
||||||
logger.warn("pre_rmbg_candidate_invalid", {
|
|
||||||
requestId,
|
|
||||||
reason: lastReason,
|
|
||||||
});
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
productImageUrl: cleanedUrl,
|
|
||||||
usedPreRmbg: true,
|
|
||||||
provider: String(payload?.provider || "unknown"),
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
lastReason = error instanceof Error ? error.message : "exception";
|
|
||||||
logger.warn("pre_rmbg_candidate_exception", error, { requestId });
|
|
||||||
} finally {
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
productImageUrl,
|
|
||||||
usedPreRmbg: false,
|
|
||||||
reason: lastReason,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建缩略图
|
* 创建缩略图
|
||||||
*/
|
*/
|
||||||
@ -2436,12 +2305,13 @@ export async function POST(request: Request) {
|
|||||||
productImageUrl = await uploadBase64ToS3(productImage, "scene-fusion-product");
|
productImageUrl = await uploadBase64ToS3(productImage, "scene-fusion-product");
|
||||||
}
|
}
|
||||||
const shouldForcePreRmbg = resolvedSceneMode === "random-bg";
|
const shouldForcePreRmbg = resolvedSceneMode === "random-bg";
|
||||||
const preprocessedProduct = await preprocessProductImageWithRmbg(
|
const preprocessedProduct = await preprocessProductImageWithRmbg({
|
||||||
request,
|
request,
|
||||||
requestId,
|
requestId,
|
||||||
|
userId: session.user.id,
|
||||||
productImageUrl,
|
productImageUrl,
|
||||||
shouldForcePreRmbg ? true : config.preRmbgEnabled
|
preRmbgEnabled: shouldForcePreRmbg ? true : config.preRmbgEnabled,
|
||||||
);
|
});
|
||||||
const fusionProductImageUrl = preprocessedProduct.productImageUrl;
|
const fusionProductImageUrl = preprocessedProduct.productImageUrl;
|
||||||
logger.info("pre_rmbg_resolved", {
|
logger.info("pre_rmbg_resolved", {
|
||||||
requestId,
|
requestId,
|
||||||
|
|||||||
@ -1,15 +1,18 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { resolveSiteBranding } from "@/lib/site-branding";
|
||||||
import { createHash } from "node:crypto";
|
import { createHash } from "node:crypto";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import {
|
import {
|
||||||
|
aiModel,
|
||||||
aiProvider,
|
aiProvider,
|
||||||
siteSettings,
|
siteSettings,
|
||||||
systemSettingHistory,
|
systemSettingHistory,
|
||||||
systemSettingSnapshot,
|
systemSettingSnapshot,
|
||||||
systemSettings,
|
systemSettings,
|
||||||
} from "@/lib/db/schema";
|
} from "@/lib/db/schema";
|
||||||
import { and, eq, inArray } from "drizzle-orm";
|
import { and, eq, inArray, sql } from "drizzle-orm";
|
||||||
|
import { omitAgentWorkflowPrompts } from "@/lib/agents/workflow-loading";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { isAgentWorkflowAdminType, isSiteAdminType } from "@/lib/middleware/admin";
|
import { isAgentWorkflowAdminType, isSiteAdminType } from "@/lib/middleware/admin";
|
||||||
import {
|
import {
|
||||||
@ -152,7 +155,7 @@ import {
|
|||||||
} from "@/lib/ai-wash/config";
|
} from "@/lib/ai-wash/config";
|
||||||
import { STYLE_PRO_CONFIG_KEY, normalizeStyleProConfig } from "@/lib/style-pro/config";
|
import { STYLE_PRO_CONFIG_KEY, normalizeStyleProConfig } from "@/lib/style-pro/config";
|
||||||
import { withTimeout } from "@/lib/public-api/performance";
|
import { withTimeout } from "@/lib/public-api/performance";
|
||||||
import type { AgentCategoryTreeConfig } from "@/types/agent";
|
import type { AgentCategoryTreeConfig, AgentWorkflowUiConfig } from "@/types/agent";
|
||||||
|
|
||||||
// Historically stored as "home_surprise_categories" but used for the featured gallery categories.
|
// Historically stored as "home_surprise_categories" but used for the featured gallery categories.
|
||||||
const HOME_SURPRISE_CATEGORIES_KEY = "home_surprise_categories";
|
const HOME_SURPRISE_CATEGORIES_KEY = "home_surprise_categories";
|
||||||
@ -552,6 +555,9 @@ function normalizeAgentCategoryTreeConfig(value: unknown): AgentCategoryTreeConf
|
|||||||
type PublicSiteSettingsScope =
|
type PublicSiteSettingsScope =
|
||||||
| "agent-brand-model"
|
| "agent-brand-model"
|
||||||
| "agent-canvas"
|
| "agent-canvas"
|
||||||
|
| "agent-canvas-base"
|
||||||
|
| "agent-workflow-templates"
|
||||||
|
| "agent-entry"
|
||||||
| "agent-presets"
|
| "agent-presets"
|
||||||
| "auth"
|
| "auth"
|
||||||
| "branding"
|
| "branding"
|
||||||
@ -563,6 +569,9 @@ type PublicSiteSettingsScope =
|
|||||||
const PUBLIC_SITE_SETTINGS_SCOPES = new Set<string>([
|
const PUBLIC_SITE_SETTINGS_SCOPES = new Set<string>([
|
||||||
"agent-brand-model",
|
"agent-brand-model",
|
||||||
"agent-canvas",
|
"agent-canvas",
|
||||||
|
"agent-canvas-base",
|
||||||
|
"agent-workflow-templates",
|
||||||
|
"agent-entry",
|
||||||
"agent-presets",
|
"agent-presets",
|
||||||
"auth",
|
"auth",
|
||||||
"branding",
|
"branding",
|
||||||
@ -606,22 +615,60 @@ const HOME_SITE_SETTINGS_FALLBACK: Record<string, unknown> = {
|
|||||||
lovartHomeSidebarConfig: normalizeLovartHomeSidebarConfig(undefined),
|
lovartHomeSidebarConfig: normalizeLovartHomeSidebarConfig(undefined),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const AGENT_CANVAS_SETTING_KEYS = [
|
||||||
|
REALTIME_CANVAS_CONFIG_KEY,
|
||||||
|
REALTIME_CANVAS_MODE_ICON_CONFIG_KEY,
|
||||||
|
CANVAS_TOOLBAR_CONFIG_SETTING_KEY,
|
||||||
|
LAYER_SPLIT_CONFIG_KEY,
|
||||||
|
ANGLE_EDIT_CONFIG_KEY,
|
||||||
|
CANVAS_AGENT_MODE_ENABLED_KEY,
|
||||||
|
...CANVAS_AGENT_FEATURE_SETTING_KEYS,
|
||||||
|
VIDEO_CANVAS_ENABLED_KEY,
|
||||||
|
VIDEO_CANVAS_SIDEBAR_ENABLED_KEY,
|
||||||
|
ENHANCE_PRO_KREA_SETTING_KEY,
|
||||||
|
AI_WASH_CONFIG_KEY,
|
||||||
|
STYLE_PRO_CONFIG_KEY,
|
||||||
|
AGENT_WORKFLOW_UI_CONFIG_KEY,
|
||||||
|
AGENT_WORKFLOW_SHOWCASE_CONFIG_KEY,
|
||||||
|
];
|
||||||
|
|
||||||
|
async function getAgentCanvasSettingsEtag(scope: string) {
|
||||||
|
const [settings, rows, providers] = await Promise.all([
|
||||||
|
db.select({ id: siteSettings.id, updatedAt: siteSettings.updatedAt })
|
||||||
|
.from(siteSettings).limit(1),
|
||||||
|
db.select({ key: systemSettings.key, updatedAt: systemSettings.updatedAt })
|
||||||
|
.from(systemSettings)
|
||||||
|
.where(inArray(systemSettings.key, AGENT_CANVAS_SETTING_KEYS)),
|
||||||
|
db.select({ id: aiProvider.id, updatedAt: aiProvider.updatedAt, enabled: aiProvider.isEnabled })
|
||||||
|
.from(aiProvider)
|
||||||
|
.where(inArray(aiProvider.type, ["tripo-3d", "worldlabs-3d"])),
|
||||||
|
]);
|
||||||
|
const versions = [
|
||||||
|
...settings.map((row) => `${row.id}:${row.updatedAt.getTime()}`),
|
||||||
|
...rows.map((row) => `${row.key}:${row.updatedAt.getTime()}`),
|
||||||
|
...providers.map((row) => `${row.id}:${row.updatedAt.getTime()}:${row.enabled}`),
|
||||||
|
].sort();
|
||||||
|
return `W/"${scope}/${versions.join(",")}"`;
|
||||||
|
}
|
||||||
|
|
||||||
function createScopedSiteSettingsResponse(
|
function createScopedSiteSettingsResponse(
|
||||||
scope: PublicSiteSettingsScope,
|
scope: PublicSiteSettingsScope,
|
||||||
data: Record<string, unknown>,
|
data: Record<string, unknown>,
|
||||||
cacheStatus: "HIT" | "MISS" | "STALE" | "FALLBACK" = "MISS",
|
cacheStatus: "HIT" | "MISS" | "STALE" | "FALLBACK" = "MISS",
|
||||||
cacheKey: string = scope,
|
cacheKey: string = scope,
|
||||||
options: { fallback?: boolean; startedAt?: number } = {}
|
options: { fallback?: boolean; startedAt?: number; etag?: string } = {}
|
||||||
) {
|
) {
|
||||||
if (cacheStatus === "MISS") {
|
if (cacheStatus === "MISS") {
|
||||||
setScopedSiteSettingsCache(
|
setScopedSiteSettingsCache(
|
||||||
cacheKey,
|
cacheKey,
|
||||||
data,
|
data,
|
||||||
PUBLIC_SITE_SETTINGS_CACHE_TTL_MS
|
PUBLIC_SITE_SETTINGS_CACHE_TTL_MS,
|
||||||
|
options.etag
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = NextResponse.json(data);
|
const response = NextResponse.json(data);
|
||||||
|
if (options.etag) response.headers.set("ETag", options.etag);
|
||||||
// Lightweight public settings change rarely; keep them browser-local briefly
|
// Lightweight public settings change rarely; keep them browser-local briefly
|
||||||
// so page opens do not repeatedly wait on the large full settings endpoint.
|
// so page opens do not repeatedly wait on the large full settings endpoint.
|
||||||
response.headers.set(
|
response.headers.set(
|
||||||
@ -641,7 +688,7 @@ function createScopedSiteSettingsResponse(
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readScopedSiteSettings(keys: string[]) {
|
async function readScopedSiteSettings(keys: string[], omitWorkflowSteps = false) {
|
||||||
const [settings, settingRows] = await Promise.all([
|
const [settings, settingRows] = await Promise.all([
|
||||||
db
|
db
|
||||||
.select()
|
.select()
|
||||||
@ -649,8 +696,14 @@ async function readScopedSiteSettings(keys: string[]) {
|
|||||||
.limit(1)
|
.limit(1)
|
||||||
.then((rows) => rows[0]),
|
.then((rows) => rows[0]),
|
||||||
keys.length > 0
|
keys.length > 0
|
||||||
? db
|
? (omitWorkflowSteps
|
||||||
.select()
|
? db.select({
|
||||||
|
key: systemSettings.key,
|
||||||
|
value: sql<unknown>`case when ${systemSettings.key} = ${AGENT_WORKFLOW_UI_CONFIG_KEY}
|
||||||
|
and jsonb_typeof(${systemSettings.value}) = 'object'
|
||||||
|
then ${systemSettings.value} - 'steps' else ${systemSettings.value} end`,
|
||||||
|
})
|
||||||
|
: db.select())
|
||||||
.from(systemSettings)
|
.from(systemSettings)
|
||||||
.where(inArray(systemSettings.key, keys))
|
.where(inArray(systemSettings.key, keys))
|
||||||
: Promise.resolve([]),
|
: Promise.resolve([]),
|
||||||
@ -663,12 +716,12 @@ async function readScopedSiteSettings(keys: string[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createBrandingSettings(settings: Record<string, unknown> | undefined) {
|
function createBrandingSettings(settings: Record<string, unknown> | undefined) {
|
||||||
return {
|
return resolveSiteBranding({
|
||||||
siteName: String(settings?.siteName || "𝗕𝘂𝗬𝗶"),
|
siteName: String(settings?.siteName || "𝗕𝘂𝗬𝗶"),
|
||||||
siteLogo: settings?.siteLogo || null,
|
siteLogo: String(settings?.siteLogo || ""),
|
||||||
siteLogoDark: settings?.siteLogoDark || null,
|
siteLogoDark: String(settings?.siteLogoDark || ""),
|
||||||
favicon: settings?.favicon || null,
|
favicon: String(settings?.favicon || ""),
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getScopedPublicSiteSettings(
|
async function getScopedPublicSiteSettings(
|
||||||
@ -676,6 +729,7 @@ async function getScopedPublicSiteSettings(
|
|||||||
options: {
|
options: {
|
||||||
brandName?: string;
|
brandName?: string;
|
||||||
cacheKey?: string;
|
cacheKey?: string;
|
||||||
|
etag?: string;
|
||||||
} = {}
|
} = {}
|
||||||
) {
|
) {
|
||||||
if (scope === "auth") {
|
if (scope === "auth") {
|
||||||
@ -737,23 +791,17 @@ async function getScopedPublicSiteSettings(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scope === "agent-canvas") {
|
if (scope === "agent-workflow-templates") {
|
||||||
const { settings, valueByKey } = await readScopedSiteSettings([
|
const [setting] = await db.select({ value: systemSettings.value })
|
||||||
REALTIME_CANVAS_CONFIG_KEY,
|
.from(systemSettings).where(eq(systemSettings.key, AGENT_WORKFLOW_UI_CONFIG_KEY)).limit(1);
|
||||||
REALTIME_CANVAS_MODE_ICON_CONFIG_KEY,
|
const config = normalizeAgentWorkflowUiConfig(setting?.value);
|
||||||
CANVAS_TOOLBAR_CONFIG_SETTING_KEY,
|
return createScopedSiteSettingsResponse(scope, {
|
||||||
LAYER_SPLIT_CONFIG_KEY,
|
steps: omitAgentWorkflowPrompts(config.steps),
|
||||||
ANGLE_EDIT_CONFIG_KEY,
|
}, "MISS", options.cacheKey || scope, { etag: options.etag });
|
||||||
CANVAS_AGENT_MODE_ENABLED_KEY,
|
}
|
||||||
...CANVAS_AGENT_FEATURE_SETTING_KEYS,
|
|
||||||
VIDEO_CANVAS_ENABLED_KEY,
|
if (scope === "agent-canvas" || scope === "agent-canvas-base" || scope === "agent-entry") {
|
||||||
VIDEO_CANVAS_SIDEBAR_ENABLED_KEY,
|
const { settings, valueByKey } = await readScopedSiteSettings(AGENT_CANVAS_SETTING_KEYS, scope !== "agent-canvas");
|
||||||
ENHANCE_PRO_KREA_SETTING_KEY,
|
|
||||||
AI_WASH_CONFIG_KEY,
|
|
||||||
STYLE_PRO_CONFIG_KEY,
|
|
||||||
AGENT_WORKFLOW_UI_CONFIG_KEY,
|
|
||||||
AGENT_WORKFLOW_SHOWCASE_CONFIG_KEY,
|
|
||||||
]);
|
|
||||||
const canvasFeatureAvailability = await readCanvasFeatureAvailability(
|
const canvasFeatureAvailability = await readCanvasFeatureAvailability(
|
||||||
valueByKey
|
valueByKey
|
||||||
);
|
);
|
||||||
@ -761,6 +809,8 @@ async function getScopedPublicSiteSettings(
|
|||||||
const videoCanvasSidebarEnabled = normalizeVideoCanvasSidebarEnabled(
|
const videoCanvasSidebarEnabled = normalizeVideoCanvasSidebarEnabled(
|
||||||
valueByKey.get(VIDEO_CANVAS_SIDEBAR_ENABLED_KEY)
|
valueByKey.get(VIDEO_CANVAS_SIDEBAR_ENABLED_KEY)
|
||||||
);
|
);
|
||||||
|
const rawUiConfig = valueByKey.get(AGENT_WORKFLOW_UI_CONFIG_KEY) as
|
||||||
|
Partial<AgentWorkflowUiConfig> | undefined;
|
||||||
return createScopedSiteSettingsResponse(scope, {
|
return createScopedSiteSettingsResponse(scope, {
|
||||||
...createBrandingSettings(typedSettings),
|
...createBrandingSettings(typedSettings),
|
||||||
agentEnabled: typedSettings?.agentEnabled ?? true,
|
agentEnabled: typedSettings?.agentEnabled ?? true,
|
||||||
@ -796,12 +846,20 @@ async function getScopedPublicSiteSettings(
|
|||||||
tripo3dEnabled: canvasFeatureAvailability.tripo3dEnabled,
|
tripo3dEnabled: canvasFeatureAvailability.tripo3dEnabled,
|
||||||
worldlabs3dEnabled: canvasFeatureAvailability.worldlabs3dEnabled,
|
worldlabs3dEnabled: canvasFeatureAvailability.worldlabs3dEnabled,
|
||||||
agentWorkflowUiConfig: normalizeAgentWorkflowUiConfig(
|
agentWorkflowUiConfig: normalizeAgentWorkflowUiConfig(
|
||||||
valueByKey.get(AGENT_WORKFLOW_UI_CONFIG_KEY)
|
scope === "agent-entry"
|
||||||
|
? {
|
||||||
|
steps: [],
|
||||||
|
pageFeatureSettings: rawUiConfig?.pageFeatureSettings,
|
||||||
|
businessModeSettings: rawUiConfig?.businessModeSettings,
|
||||||
|
}
|
||||||
|
: scope === "agent-canvas-base"
|
||||||
|
? { ...rawUiConfig, steps: [] }
|
||||||
|
: rawUiConfig
|
||||||
),
|
),
|
||||||
agentWorkflowShowcaseConfig: normalizeAgentWorkflowShowcaseConfig(
|
agentWorkflowShowcaseConfig: normalizeAgentWorkflowShowcaseConfig(
|
||||||
valueByKey.get(AGENT_WORKFLOW_SHOWCASE_CONFIG_KEY)
|
valueByKey.get(AGENT_WORKFLOW_SHOWCASE_CONFIG_KEY)
|
||||||
),
|
),
|
||||||
});
|
}, "MISS", options.cacheKey || scope, { etag: options.etag });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scope === "branding") {
|
if (scope === "branding") {
|
||||||
@ -1055,13 +1113,24 @@ export async function GET(request: NextRequest) {
|
|||||||
const cacheKey = brandName.trim()
|
const cacheKey = brandName.trim()
|
||||||
? `${scopedScope}:${brandName.trim().toLowerCase()}`
|
? `${scopedScope}:${brandName.trim().toLowerCase()}`
|
||||||
: scopedScope;
|
: scopedScope;
|
||||||
|
const etag = scopedScope === "agent-canvas" || scopedScope === "agent-entry" ||
|
||||||
|
scopedScope === "agent-canvas-base" || scopedScope === "agent-workflow-templates"
|
||||||
|
? await getAgentCanvasSettingsEtag(scopedScope)
|
||||||
|
: undefined;
|
||||||
|
if (etag && request.headers.get("If-None-Match") === etag) {
|
||||||
|
return new NextResponse(null, {
|
||||||
|
status: 304,
|
||||||
|
headers: { ETag: etag, "Cache-Control": "private, no-cache" },
|
||||||
|
});
|
||||||
|
}
|
||||||
const cached = getScopedSiteSettingsCache(cacheKey);
|
const cached = getScopedSiteSettingsCache(cacheKey);
|
||||||
if (cached && cached.expiresAt > Date.now()) {
|
if (cached && cached.expiresAt > Date.now() && (!etag || cached.etag === etag)) {
|
||||||
return createScopedSiteSettingsResponse(
|
return createScopedSiteSettingsResponse(
|
||||||
scopedScope,
|
scopedScope,
|
||||||
cached.data,
|
cached.data,
|
||||||
"HIT",
|
"HIT",
|
||||||
cacheKey
|
cacheKey,
|
||||||
|
{ etag }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (cached && scopedScope !== "home") {
|
if (cached && scopedScope !== "home") {
|
||||||
@ -1072,6 +1141,7 @@ export async function GET(request: NextRequest) {
|
|||||||
const responsePromise = getScopedPublicSiteSettings(scopedScope, {
|
const responsePromise = getScopedPublicSiteSettings(scopedScope, {
|
||||||
brandName,
|
brandName,
|
||||||
cacheKey,
|
cacheKey,
|
||||||
|
etag,
|
||||||
});
|
});
|
||||||
return scopedScope === "home"
|
return scopedScope === "home"
|
||||||
? await withTimeout(responsePromise, 1600, "site-settings:home")
|
? await withTimeout(responsePromise, 1600, "site-settings:home")
|
||||||
@ -1541,7 +1611,7 @@ export async function GET(request: NextRequest) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const response = NextResponse.json({
|
const response = NextResponse.json({
|
||||||
...baseSettings,
|
...resolveSiteBranding(baseSettings),
|
||||||
lovartPromoText:
|
lovartPromoText:
|
||||||
baseSettings.lovartPromoText ||
|
baseSettings.lovartPromoText ||
|
||||||
"现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
"现在升级,立享 Kling 3.0 与 Nano Banana Pro,最高 5 折优惠",
|
||||||
@ -2143,6 +2213,14 @@ export async function PUT(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const data = updateSchema.parse(body);
|
const data = updateSchema.parse(body);
|
||||||
|
if (data.canvasAutoModeDefaultModelId) {
|
||||||
|
const [available] = await db.select({ id: aiModel.id }).from(aiModel)
|
||||||
|
.innerJoin(aiProvider, eq(aiProvider.id, aiModel.providerId))
|
||||||
|
.where(and(eq(aiModel.id, data.canvasAutoModeDefaultModelId),
|
||||||
|
eq(aiModel.isEnabled, true), eq(aiProvider.isEnabled, true),
|
||||||
|
eq(aiModel.isSubModel, false), eq(aiModel.supportsImageGen, true))).limit(1);
|
||||||
|
if (!available) return NextResponse.json({ error: "默认生图模型或其上游已停用,请选择可用的生图模型,或改为系统智能选择。" }, { status: 400 });
|
||||||
|
}
|
||||||
const {
|
const {
|
||||||
surpriseCategories,
|
surpriseCategories,
|
||||||
aiDetailGenerationEnabled,
|
aiDetailGenerationEnabled,
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import { eq } from "drizzle-orm";
|
|||||||
import { uploadFile } from "@/lib/storage/service";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
import {
|
import {
|
||||||
createMediaAssetRelation,
|
createMediaAssetRelation,
|
||||||
|
getDefaultStorageConfig,
|
||||||
getPointsSystemConfig,
|
getPointsSystemConfig,
|
||||||
getMediaAssetsByUserAndUrls,
|
getMediaAssetsByUserAndUrls,
|
||||||
upsertMediaAsset,
|
upsertMediaAsset,
|
||||||
@ -21,6 +22,27 @@ export const maxDuration = 600;
|
|||||||
const WATERMARK_SYNC_WAIT_TIMEOUT_MS = 20_000;
|
const WATERMARK_SYNC_WAIT_TIMEOUT_MS = 20_000;
|
||||||
const WATERMARK_POLL_INTERVAL_MS = 1200;
|
const WATERMARK_POLL_INTERVAL_MS = 1200;
|
||||||
|
|
||||||
|
async function resolveStoredQiniuImageUrl(source: string): Promise<string | null> {
|
||||||
|
const config = await getDefaultStorageConfig().catch(() => null);
|
||||||
|
if (config?.type !== "qiniu-kodo" || !config.isEnabled || !config.publicUrl) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const base = new URL(config.publicUrl);
|
||||||
|
const url = new URL(source);
|
||||||
|
const prefix = base.pathname.replace(/\/$/, "") + "/";
|
||||||
|
if (base.protocol !== "https:" || url.origin !== base.origin ||
|
||||||
|
url.username || url.password || url.search || url.hash ||
|
||||||
|
!url.pathname.startsWith(prefix)) return null;
|
||||||
|
|
||||||
|
const objectKey = url.pathname.slice(prefix.length);
|
||||||
|
return /^uploads\/(?:direct\/[0-9a-f-]{36}\/)?[\w.-]+$/i.test(objectKey)
|
||||||
|
? url.toString()
|
||||||
|
: null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 获取去水印API配置
|
// 获取去水印API配置
|
||||||
async function getWatermarkRemoveConfig() {
|
async function getWatermarkRemoveConfig() {
|
||||||
try {
|
try {
|
||||||
@ -131,13 +153,16 @@ async function persistWatermarkResult(params: {
|
|||||||
processing_time?: number;
|
processing_time?: number;
|
||||||
}> {
|
}> {
|
||||||
try {
|
try {
|
||||||
|
let resultUrl = await resolveStoredQiniuImageUrl(params.processedImageUrl);
|
||||||
|
if (!resultUrl) {
|
||||||
const imageResponse = await fetch(params.processedImageUrl);
|
const imageResponse = await fetch(params.processedImageUrl);
|
||||||
if (!imageResponse.ok) {
|
if (!imageResponse.ok) {
|
||||||
throw new Error("下载处理后的图片失败");
|
throw new Error("下载处理后的图片失败");
|
||||||
}
|
}
|
||||||
const imageBuffer = await imageResponse.arrayBuffer();
|
const imageBuffer = await imageResponse.arrayBuffer();
|
||||||
const filename = `watermark-removed-${Date.now()}.png`;
|
const filename = `watermark-removed-${Date.now()}.png`;
|
||||||
const uploadResult = await uploadFile(filename, imageBuffer);
|
resultUrl = (await uploadFile(filename, imageBuffer)).url;
|
||||||
|
}
|
||||||
|
|
||||||
let saved = false;
|
let saved = false;
|
||||||
let saveError: unknown = null;
|
let saveError: unknown = null;
|
||||||
@ -150,8 +175,8 @@ async function persistWatermarkResult(params: {
|
|||||||
try {
|
try {
|
||||||
([savedImage] = await db.insert(generatedImage).values({
|
([savedImage] = await db.insert(generatedImage).values({
|
||||||
userId: params.userId,
|
userId: params.userId,
|
||||||
thumbnailUrl: uploadResult.url,
|
thumbnailUrl: resultUrl,
|
||||||
originalUrl: uploadResult.url,
|
originalUrl: resultUrl,
|
||||||
imageSize: "2K",
|
imageSize: "2K",
|
||||||
prompt: "去水印处理",
|
prompt: "去水印处理",
|
||||||
modelId: "watermark-remove",
|
modelId: "watermark-remove",
|
||||||
@ -184,9 +209,9 @@ async function persistWatermarkResult(params: {
|
|||||||
mediaType: "image",
|
mediaType: "image",
|
||||||
sourceType: "edited",
|
sourceType: "edited",
|
||||||
status: "ready",
|
status: "ready",
|
||||||
originalUrl: uploadResult.url,
|
originalUrl: resultUrl,
|
||||||
previewUrl: uploadResult.url,
|
previewUrl: resultUrl,
|
||||||
thumbnailUrl: uploadResult.url,
|
thumbnailUrl: resultUrl,
|
||||||
displayName: "去水印处理",
|
displayName: "去水印处理",
|
||||||
prompt: "去水印处理",
|
prompt: "去水印处理",
|
||||||
modelId: "watermark-remove",
|
modelId: "watermark-remove",
|
||||||
@ -217,8 +242,8 @@ async function persistWatermarkResult(params: {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
original_url: uploadResult.url,
|
original_url: resultUrl,
|
||||||
thumbnail_url: uploadResult.url,
|
thumbnail_url: resultUrl,
|
||||||
processing_time:
|
processing_time:
|
||||||
typeof params.startedAt === "number" ? (Date.now() - params.startedAt) / 1000 : undefined,
|
typeof params.startedAt === "number" ? (Date.now() - params.startedAt) / 1000 : undefined,
|
||||||
};
|
};
|
||||||
@ -362,7 +387,7 @@ export async function POST(request: NextRequest) {
|
|||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
const { image_url } = body;
|
const { image_url } = body;
|
||||||
|
|
||||||
if (!image_url) {
|
if (typeof image_url !== "string" || !image_url) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "请提供图片URL" },
|
{ error: "请提供图片URL" },
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
@ -413,8 +438,11 @@ export async function POST(request: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果是 HTTP/HTTPS URL,下载后重新上传确保公网可访问
|
// 七牛上的公开图片可以直接交给去水印服务读取。
|
||||||
else if (image_url.startsWith("http://") || image_url.startsWith("https://")) {
|
else if (image_url.startsWith("http://") || image_url.startsWith("https://")) {
|
||||||
|
const storedImageUrl = await resolveStoredQiniuImageUrl(image_url);
|
||||||
|
publicImageUrl = storedImageUrl || image_url;
|
||||||
|
if (!storedImageUrl) {
|
||||||
try {
|
try {
|
||||||
console.log("[WatermarkRemove] 下载原图并上传到S3...");
|
console.log("[WatermarkRemove] 下载原图并上传到S3...");
|
||||||
const imageResponse = await fetch(image_url);
|
const imageResponse = await fetch(image_url);
|
||||||
@ -423,14 +451,11 @@ export async function POST(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
const imageBuffer = await imageResponse.arrayBuffer();
|
const imageBuffer = await imageResponse.arrayBuffer();
|
||||||
const filename = `watermark-input-${Date.now()}.jpg`;
|
const filename = `watermark-input-${Date.now()}.jpg`;
|
||||||
const uploadResult = await uploadFile(filename, imageBuffer);
|
publicImageUrl = (await uploadFile(filename, imageBuffer)).url;
|
||||||
publicImageUrl = uploadResult.url;
|
|
||||||
console.log("[WatermarkRemove] 图片已上传到S3:", publicImageUrl);
|
console.log("[WatermarkRemove] 图片已上传到S3:", publicImageUrl);
|
||||||
} catch (downloadError) {
|
} catch (downloadError) {
|
||||||
console.error("[WatermarkRemove] 下载/上传原图失败:", downloadError);
|
console.error("[WatermarkRemove] 下载/上传原图失败,使用原始URL:", downloadError);
|
||||||
// 如果下载失败,尝试使用原始URL
|
}
|
||||||
console.log("[WatermarkRemove] 尝试使用原始URL");
|
|
||||||
publicImageUrl = image_url;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -48,8 +48,13 @@ export default function CanvasStyleProPage() {
|
|||||||
const canvasData = result.data.canvasData;
|
const canvasData = result.data.canvasData;
|
||||||
const latestState = useCanvasStore.getState();
|
const latestState = useCanvasStore.getState();
|
||||||
setProject(result.data.id, result.data.title);
|
setProject(result.data.id, result.data.title);
|
||||||
if (latestState.nodes.length === 0 && latestState.edges.length === 0) {
|
if (
|
||||||
|
latestState.projectId !== id ||
|
||||||
|
(latestState.nodes.length === 0 && latestState.edges.length === 0)
|
||||||
|
) {
|
||||||
loadCanvas(canvasData);
|
loadCanvas(canvasData);
|
||||||
|
const revision = String(result.data.updatedAt || "");
|
||||||
|
useCanvasStore.getState().setCanvasRevision(revision, revision);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
116
app/feedback/[id]/page.tsx
Normal file
116
app/feedback/[id]/page.tsx
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
import { notFound, redirect } from "next/navigation";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { auth } from "@/app/(auth)/auth";
|
||||||
|
import { feedbackReceipt, findCanvasFeedback } from "@/lib/canvas/feedback-store";
|
||||||
|
import { formatFeedbackTime } from "@/lib/canvas/feedback-contract";
|
||||||
|
|
||||||
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
export default async function CanvasFeedbackPage({ params }: { params: Promise<{ id: string }> }) {
|
||||||
|
const { id } = await params;
|
||||||
|
if (!z.string().uuid().safeParse(id).success) notFound();
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id || session.user.type === "guest") {
|
||||||
|
const query = new URLSearchParams({ callbackUrl: `/feedback/${id}` });
|
||||||
|
redirect(`/login?${query}`);
|
||||||
|
}
|
||||||
|
const feedback = await findCanvasFeedback(id);
|
||||||
|
if (!feedback || (feedback.userId !== session.user.id && session.user.type !== "admin"))
|
||||||
|
notFound();
|
||||||
|
const { reporter, project, request, client, userAgent } = feedback.snapshot;
|
||||||
|
const fields = [
|
||||||
|
["反馈编号", feedback.id],
|
||||||
|
["反馈人", `${reporter.name} · ${reporter.email} · ${reporter.department || "未登记部门"}`],
|
||||||
|
["用户编号", reporter.id],
|
||||||
|
["反馈时间", formatFeedbackTime(feedback.createdAt.toISOString())],
|
||||||
|
...(project.id ? [
|
||||||
|
["画布", `${project.title}(${project.id})`],
|
||||||
|
["对话编号", client.chatId || "未打开对话"],
|
||||||
|
["请求号", request?.requestId || "尚未获取"],
|
||||||
|
["任务或消息编号", request?.id || "未关联"],
|
||||||
|
["请求时间", request?.createdAt ? formatFeedbackTime(request.createdAt) : "未记录"],
|
||||||
|
["状态与模型", `${request?.status || "页面问题"} / ${request?.model || "未记录"}`],
|
||||||
|
[
|
||||||
|
"处理耗时",
|
||||||
|
request?.durationMs != null
|
||||||
|
? `${(request.durationMs / 1000).toFixed(1)} 秒`
|
||||||
|
: "未记录或尚未结束",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"资料来源",
|
||||||
|
request?.evidence === "browser"
|
||||||
|
? "浏览器当前对话,服务端尚无对应消息记录"
|
||||||
|
: "服务端记录及浏览器当前页面",
|
||||||
|
],
|
||||||
|
] : [["页面", project.title]]),
|
||||||
|
["页面版本", client.pageVersion],
|
||||||
|
["浏览器", userAgent],
|
||||||
|
[
|
||||||
|
"窗口与网络",
|
||||||
|
`${client.viewport.width} × ${client.viewport.height} · ${client.online ? "在线" : "离线"}`,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
return (
|
||||||
|
<main className="mx-auto max-w-3xl space-y-6 px-6 py-8 text-foreground">
|
||||||
|
<h1 className="text-2xl font-semibold">问题反馈</h1>
|
||||||
|
<p className="text-sm text-muted-foreground">{feedbackReceipt(feedback).message}</p>
|
||||||
|
<dl className="space-y-3">
|
||||||
|
{fields.map(([label, value]) => (
|
||||||
|
<div key={label} className="grid gap-1 text-sm sm:grid-cols-[8rem_1fr]">
|
||||||
|
<dt className="text-muted-foreground">{label}</dt>
|
||||||
|
<dd className="whitespace-pre-wrap break-words">{value}</dd>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</dl>
|
||||||
|
{[
|
||||||
|
["用户说明", feedback.description || "未补充说明"],
|
||||||
|
["请求内容", request?.prompt || "未关联具体请求"],
|
||||||
|
["错误信息", request?.error || client.errors.join("\n") || "未记录明确错误"],
|
||||||
|
].map(([label, value]) => (
|
||||||
|
<section key={label} className="space-y-2">
|
||||||
|
<h2 className="text-base font-medium">{label}</h2>
|
||||||
|
<p className="whitespace-pre-wrap break-words rounded-lg bg-muted p-4 text-sm">{value}</p>
|
||||||
|
</section>
|
||||||
|
))}
|
||||||
|
{request?.parameters && Object.keys(request.parameters).length > 0 && (
|
||||||
|
<section className="space-y-2">
|
||||||
|
<h2 className="text-base font-medium">请求参数</h2>
|
||||||
|
<pre className="overflow-auto whitespace-pre-wrap break-all rounded-lg bg-muted p-4 text-sm">
|
||||||
|
{JSON.stringify(request.parameters, null, 2)}
|
||||||
|
</pre>
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
|
{project.id && client.nodes.length > 0 && (
|
||||||
|
<details className="text-sm">
|
||||||
|
<summary className="cursor-pointer">相关画布节点</summary>
|
||||||
|
<pre className="mt-3 overflow-auto whitespace-pre-wrap break-all">
|
||||||
|
{JSON.stringify(client.nodes, null, 2)}
|
||||||
|
</pre>
|
||||||
|
</details>
|
||||||
|
)}
|
||||||
|
{project.id && (
|
||||||
|
<>
|
||||||
|
<details className="text-sm">
|
||||||
|
<summary className="cursor-pointer">当前对话上下文</summary>
|
||||||
|
<div className="mt-4 space-y-4">
|
||||||
|
{client.messages.map((item) => (
|
||||||
|
<div key={item.id} className="space-y-1">
|
||||||
|
<p className="text-muted-foreground">
|
||||||
|
{item.role === "user" ? "用户" : "助手"} · {item.id}
|
||||||
|
</p>
|
||||||
|
<p className="whitespace-pre-wrap break-words">{item.text}</p>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
<a
|
||||||
|
className="inline-block text-sm underline underline-offset-4"
|
||||||
|
href={`/canvas/${project.id}`}
|
||||||
|
>
|
||||||
|
打开相关画布
|
||||||
|
</a>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -642,6 +642,34 @@
|
|||||||
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-composer-mode="image"] .lovart-composer-footer > div:last-child { flex: 1 1 0; min-width: 0; gap: 0; }
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-composer-mode="image"] .lovart-composer-footer > div:last-child { flex: 1 1 0; min-width: 0; gap: 0; }
|
||||||
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-composer-mode="image"] .lovart-composer-send { min-width: 32px; width: auto; height: 32px; padding: 0 8px; gap: 2px; font-size: 13px; font-weight: 400; }
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-composer-mode="image"] .lovart-composer-send { min-width: 32px; width: auto; height: 32px; padding: 0 8px; gap: 2px; font-size: 13px; font-weight: 400; }
|
||||||
|
|
||||||
|
/* Adapt to the resizable chat panel, independently of the browser width. */
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] { container: canvas-composer / inline-size; }
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] .lovart-composer-footer { gap: 4px; }
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] .lovart-composer-footer > div:first-child { flex: 0 0 auto; overflow: visible; padding-right: 0; }
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] .lovart-composer-footer > div:last-child { flex: 0 1 auto; min-width: 0; }
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] .lovart-composer-mode-selector { width: auto; max-width: none; }
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] .lovart-image-params { width: auto; min-width: 0; }
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] .lovart-image-params-label { overflow: visible; text-overflow: clip; white-space: nowrap; }
|
||||||
|
.lovart-image-params-icon { display: none; }
|
||||||
|
@container canvas-composer (max-width: 480px) {
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] .lovart-composer-mode-selector { width: 32px; min-width: 32px; padding: 0; }
|
||||||
|
.lovart-composer-mode-selector .lovart-composer-mode-label,
|
||||||
|
.lovart-composer-mode-selector [data-reference-icon="chevron-down"] { display: none; }
|
||||||
|
.lovart-composer-mode-selector > span > :first-child { margin-right: 0; }
|
||||||
|
}
|
||||||
|
@container canvas-composer (max-width: 360px) {
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] .lovart-model-selector { flex: 0 0 32px; width: 32px; padding: 0; justify-content: center; }
|
||||||
|
.lovart-model-selector > span:not([data-reference-icon]),
|
||||||
|
.lovart-model-selector > [data-reference-icon="chevron-down"] { display: none; }
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] .lovart-model-selector > [data-reference-icon]:first-child { margin-right: 0; }
|
||||||
|
}
|
||||||
|
@container canvas-composer (max-width: 480px) {
|
||||||
|
.lovart-canvas-surface [data-lovart-chat="true"] [data-canvas-responsive-composer="true"] .lovart-image-params { width: 32px; min-width: 32px; padding: 0; justify-content: center; }
|
||||||
|
.lovart-image-params .lovart-image-params-icon { display: block; }
|
||||||
|
.lovart-image-params .lovart-image-params-label,
|
||||||
|
.lovart-image-params > [data-reference-icon="chevron-down"] { display: none; }
|
||||||
|
}
|
||||||
|
|
||||||
/* The existing image toolbar is scaled to 80%; these dimensions render at
|
/* The existing image toolbar is scaled to 80%; these dimensions render at
|
||||||
the reference's 44px bar, 32px buttons, and 20px icons. */
|
the reference's 44px bar, 32px buttons, and 20px icons. */
|
||||||
.lovart-image-action-bar {
|
.lovart-image-action-bar {
|
||||||
|
|||||||
@ -3,6 +3,9 @@ import { Toaster } from "sonner";
|
|||||||
import { ThemeProvider } from "@/components/theme-provider";
|
import { ThemeProvider } from "@/components/theme-provider";
|
||||||
import { FaviconUpdater } from "@/components/favicon-updater";
|
import { FaviconUpdater } from "@/components/favicon-updater";
|
||||||
import { ImageDeliveryOptimizer } from "@/components/image-delivery-optimizer";
|
import { ImageDeliveryOptimizer } from "@/components/image-delivery-optimizer";
|
||||||
|
import { PageFeedback } from "@/components/page-feedback";
|
||||||
|
import { AppUpdateNotice } from "@/components/app-update-notice";
|
||||||
|
import { PAGE_DOWNLOAD_ACTIVE_ATTRIBUTE } from "@/lib/client/page-refresh";
|
||||||
import { getSiteSettings } from "@/lib/db/queries";
|
import { getSiteSettings } from "@/lib/db/queries";
|
||||||
|
|
||||||
import "./globals.css";
|
import "./globals.css";
|
||||||
@ -94,17 +97,32 @@ const CHUNK_ERROR_RECOVERY_SCRIPT = `\
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function safeRemoveSessionStorage(key) {
|
// Keep one attempt per route for the browser session. A cooldown alone can
|
||||||
try {
|
// turn a recurring editor exception into an endless reload cycle.
|
||||||
sessionStorage.removeItem(key);
|
var RECOVERY_KEY = 'chunk-reload-attempted-v3:' + window.location.pathname;
|
||||||
return true;
|
var lastReloadAt = Number(safeGetSessionStorage(RECOVERY_KEY) || '0') || 0;
|
||||||
} catch (e) {
|
var recoveryNoticeShown = false;
|
||||||
return false;
|
|
||||||
}
|
function isFlowBoardEditing() {
|
||||||
|
var path = window.location.pathname;
|
||||||
|
return path === '/flow-board' || path.indexOf('/flow-board/') === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var RECOVERY_KEY = 'chunk-reload-attempted-v2';
|
function showManualRecoveryNotice() {
|
||||||
var lastReloadAt = Number(safeGetSessionStorage(RECOVERY_KEY) || '0') || 0;
|
if (recoveryNoticeShown) return;
|
||||||
|
recoveryNoticeShown = true;
|
||||||
|
function show() {
|
||||||
|
if (!document.body || !isFlowBoardEditing()) return;
|
||||||
|
var notice = document.createElement('div');
|
||||||
|
notice.setAttribute('role', 'alert');
|
||||||
|
notice.setAttribute('data-runtime-recovery-notice', 'true');
|
||||||
|
notice.style.cssText = 'position:fixed;bottom:24px;left:50%;transform:translateX(-50%);z-index:2147483647;max-width:calc(100vw - 32px);padding:12px 16px;border:1px solid var(--border,#e4e4e7);border-radius:12px;background:var(--background,#fff);color:var(--foreground,#27272a);box-shadow:0 4px 20px #0002;font:14px/1.5 system-ui;';
|
||||||
|
notice.textContent = '部分页面资源加载失败,请先保存白板后手动刷新页面。';
|
||||||
|
document.body.appendChild(notice);
|
||||||
|
}
|
||||||
|
if (document.body) show();
|
||||||
|
else window.addEventListener('DOMContentLoaded', show, { once: true });
|
||||||
|
}
|
||||||
|
|
||||||
function shouldAutoRecover(message) {
|
function shouldAutoRecover(message) {
|
||||||
if (!message) return false;
|
if (!message) return false;
|
||||||
@ -117,20 +135,30 @@ const CHUNK_ERROR_RECOVERY_SCRIPT = `\
|
|||||||
normalized.indexOf('error loading dynamically imported module') !== -1 ||
|
normalized.indexOf('error loading dynamically imported module') !== -1 ||
|
||||||
normalized.indexOf('importing a module script failed') !== -1 ||
|
normalized.indexOf('importing a module script failed') !== -1 ||
|
||||||
normalized.indexOf('module script load failed') !== -1 ||
|
normalized.indexOf('module script load failed') !== -1 ||
|
||||||
normalized.indexOf('/_next/static/chunks/') !== -1 ||
|
normalized.indexOf('failed to find server action') !== -1
|
||||||
normalized.indexOf('failed to find server action') !== -1 ||
|
|
||||||
normalized.indexOf('server components render') !== -1 ||
|
|
||||||
normalized.indexOf('application error: a client-side exception') !== -1
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function triggerAutoRecover(reason) {
|
function triggerAutoRecover(reason) {
|
||||||
|
if (isFlowBoardEditing()) {
|
||||||
|
showManualRecoveryNotice();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (lastReloadAt || document.documentElement.hasAttribute('${PAGE_DOWNLOAD_ACTIVE_ATTRIBUTE}')) return;
|
||||||
var now = Date.now();
|
var now = Date.now();
|
||||||
if (lastReloadAt && now - lastReloadAt < 30000) return;
|
// If storage is blocked, a reload cannot remember its attempt and must not run.
|
||||||
|
if (!safeSetSessionStorage(RECOVERY_KEY, String(now))) return;
|
||||||
lastReloadAt = now;
|
lastReloadAt = now;
|
||||||
console.warn('[RuntimeRecover] 检测到前端运行时异常,自动刷新页面...', reason || '');
|
console.warn('[RuntimeRecover] 检测到页面资源加载失败,尝试恢复一次...', reason || '');
|
||||||
safeSetSessionStorage(RECOVERY_KEY, String(now));
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
if (isFlowBoardEditing()) {
|
||||||
|
showManualRecoveryNotice();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (document.documentElement.hasAttribute('${PAGE_DOWNLOAD_ACTIVE_ATTRIBUTE}')) {
|
||||||
|
if (safeSetSessionStorage(RECOVERY_KEY, '0')) lastReloadAt = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}, 800);
|
}, 800);
|
||||||
}
|
}
|
||||||
@ -138,7 +166,6 @@ const CHUNK_ERROR_RECOVERY_SCRIPT = `\
|
|||||||
window.addEventListener('error', function(event) {
|
window.addEventListener('error', function(event) {
|
||||||
var message = '';
|
var message = '';
|
||||||
if (event && event.message) message += event.message;
|
if (event && event.message) message += event.message;
|
||||||
if (event && event.filename) message += ' ' + event.filename;
|
|
||||||
if (event && event.error && event.error.message) {
|
if (event && event.error && event.error.message) {
|
||||||
message += ' ' + event.error.message;
|
message += ' ' + event.error.message;
|
||||||
}
|
}
|
||||||
@ -157,12 +184,6 @@ const CHUNK_ERROR_RECOVERY_SCRIPT = `\
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 页面稳定一段时间后清除标记,避免旧资源异常无限刷新。
|
|
||||||
window.addEventListener('load', function() {
|
|
||||||
setTimeout(function() {
|
|
||||||
safeRemoveSessionStorage(RECOVERY_KEY);
|
|
||||||
}, 120000);
|
|
||||||
});
|
|
||||||
})();`;
|
})();`;
|
||||||
|
|
||||||
export default function RootLayout({
|
export default function RootLayout({
|
||||||
@ -201,7 +222,11 @@ export default function RootLayout({
|
|||||||
enableSystem
|
enableSystem
|
||||||
>
|
>
|
||||||
<Toaster position="top-center" />
|
<Toaster position="top-center" />
|
||||||
<SessionProvider>{children}</SessionProvider>
|
<AppUpdateNotice />
|
||||||
|
<SessionProvider>
|
||||||
|
{children}
|
||||||
|
<PageFeedback />
|
||||||
|
</SessionProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
309
auto-deploy.sh
309
auto-deploy.sh
@ -1,303 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
# 自动部署脚本 - 包含密码
|
printf '%s\n' 'Legacy deployment entry point disabled: it targets an obsolete host/runtime.' >&2
|
||||||
# 使用方法: ./auto-deploy.sh
|
printf '%s\n' 'Use: python scripts/production48.py status' >&2
|
||||||
|
printf '%s\n' 'Then follow docs/production-48.md for build, stage, activate and rollback.' >&2
|
||||||
set -e
|
exit 2
|
||||||
|
|
||||||
# 服务器配置
|
|
||||||
SERVER="156.225.27.52"
|
|
||||||
PORT="5204"
|
|
||||||
USER="root"
|
|
||||||
PASSWORD="${DEPLOY_SSH_PASSWORD:?set DEPLOY_SSH_PASSWORD}"
|
|
||||||
REMOTE_DIR="/var/www/ai-chatbot"
|
|
||||||
|
|
||||||
# 颜色输出
|
|
||||||
RED='\033[0;31m'
|
|
||||||
GREEN='\033[0;32m'
|
|
||||||
YELLOW='\033[1;33m'
|
|
||||||
NC='\033[0m'
|
|
||||||
|
|
||||||
echo -e "${GREEN}开始自动部署到服务器...${NC}"
|
|
||||||
|
|
||||||
# 检查是否安装了 sshpass
|
|
||||||
if ! command -v sshpass &> /dev/null; then
|
|
||||||
echo -e "${YELLOW}安装 sshpass...${NC}"
|
|
||||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
||||||
# macOS
|
|
||||||
if ! command -v brew &> /dev/null; then
|
|
||||||
echo -e "${RED}请先安装 Homebrew: /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"${NC}"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
brew install hudochenkov/sshpass/sshpass
|
|
||||||
else
|
|
||||||
# Linux
|
|
||||||
sudo apt-get update && sudo apt-get install -y sshpass
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# SSH 连接函数
|
|
||||||
ssh_cmd() {
|
|
||||||
sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -p $PORT $USER@$SERVER "$1"
|
|
||||||
}
|
|
||||||
|
|
||||||
scp_cmd() {
|
|
||||||
sshpass -p "$PASSWORD" scp -o StrictHostKeyChecking=no -P $PORT "$1" $USER@$SERVER:"$2"
|
|
||||||
}
|
|
||||||
|
|
||||||
scp_dir() {
|
|
||||||
sshpass -p "$PASSWORD" scp -o StrictHostKeyChecking=no -P $PORT -r "$1" $USER@$SERVER:"$2"
|
|
||||||
}
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 1: 检查服务器连接...${NC}"
|
|
||||||
ssh_cmd "echo '连接成功' && uname -a"
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 2: 检查并安装必要软件...${NC}"
|
|
||||||
ssh_cmd "
|
|
||||||
# 检查 Node.js
|
|
||||||
if ! command -v node &> /dev/null; then
|
|
||||||
echo '安装 Node.js...'
|
|
||||||
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
|
|
||||||
export NVM_DIR=\"\$HOME/.nvm\"
|
|
||||||
[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"
|
|
||||||
nvm install 20
|
|
||||||
nvm use 20
|
|
||||||
nvm alias default 20
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 检查 pnpm
|
|
||||||
if ! command -v pnpm &> /dev/null; then
|
|
||||||
echo '安装 pnpm...'
|
|
||||||
npm install -g pnpm
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 检查 PM2
|
|
||||||
if ! command -v pm2 &> /dev/null; then
|
|
||||||
echo '安装 PM2...'
|
|
||||||
npm install -g pm2
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 检查 PostgreSQL
|
|
||||||
if ! command -v psql &> /dev/null; then
|
|
||||||
echo '安装 PostgreSQL...'
|
|
||||||
apt-get update -y
|
|
||||||
apt-get install -y postgresql postgresql-contrib
|
|
||||||
systemctl start postgresql
|
|
||||||
systemctl enable postgresql
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 检查 Nginx
|
|
||||||
if ! command -v nginx &> /dev/null; then
|
|
||||||
echo '安装 Nginx...'
|
|
||||||
apt-get install -y nginx
|
|
||||||
systemctl start nginx
|
|
||||||
systemctl enable nginx
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 创建项目目录
|
|
||||||
mkdir -p $REMOTE_DIR
|
|
||||||
mkdir -p /var/log/pm2
|
|
||||||
mkdir -p /root/backups/ai-chatbot
|
|
||||||
"
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 3: 配置数据库...${NC}"
|
|
||||||
ssh_cmd "
|
|
||||||
# 检查并创建/更新数据库用户
|
|
||||||
echo '配置数据库用户...'
|
|
||||||
sudo -u postgres psql << 'PSQL'
|
|
||||||
-- 删除已存在的用户(如果存在)
|
|
||||||
DROP USER IF EXISTS chatbot_user;
|
|
||||||
|
|
||||||
-- 创建新用户
|
|
||||||
CREATE USER chatbot_user WITH PASSWORD 'ChatBot@2024!Secure';
|
|
||||||
|
|
||||||
-- 检查数据库是否存在
|
|
||||||
SELECT 1 FROM pg_database WHERE datname='ai_chatbot'\\gexec
|
|
||||||
|
|
||||||
-- 如果不存在则创建
|
|
||||||
DO \\$\\$
|
|
||||||
BEGIN
|
|
||||||
IF NOT EXISTS (SELECT FROM pg_database WHERE datname = 'ai_chatbot') THEN
|
|
||||||
CREATE DATABASE ai_chatbot;
|
|
||||||
END IF;
|
|
||||||
END
|
|
||||||
\\$\\$;
|
|
||||||
|
|
||||||
-- 授予权限
|
|
||||||
GRANT ALL PRIVILEGES ON DATABASE ai_chatbot TO chatbot_user;
|
|
||||||
|
|
||||||
-- 连接到数据库并授予schema权限
|
|
||||||
\\c ai_chatbot
|
|
||||||
GRANT ALL ON SCHEMA public TO chatbot_user;
|
|
||||||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO chatbot_user;
|
|
||||||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO chatbot_user;
|
|
||||||
|
|
||||||
\\q
|
|
||||||
PSQL
|
|
||||||
echo '数据库配置完成'
|
|
||||||
"
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 4: 上传项目文件...${NC}"
|
|
||||||
# 创建临时部署包
|
|
||||||
echo "准备部署文件..."
|
|
||||||
tar --exclude='node_modules' \
|
|
||||||
--exclude='.next' \
|
|
||||||
--exclude='.git' \
|
|
||||||
--exclude='.env.local' \
|
|
||||||
--exclude='*.log' \
|
|
||||||
--exclude='deploy.tar.gz' \
|
|
||||||
-czf /tmp/deploy.tar.gz .
|
|
||||||
|
|
||||||
# 上传部署包
|
|
||||||
scp_cmd "/tmp/deploy.tar.gz" "/tmp/"
|
|
||||||
|
|
||||||
# 在服务器上解压
|
|
||||||
ssh_cmd "
|
|
||||||
cd $REMOTE_DIR
|
|
||||||
tar -xzf /tmp/deploy.tar.gz
|
|
||||||
rm /tmp/deploy.tar.gz
|
|
||||||
|
|
||||||
if [ -f \"$REMOTE_DIR/scripts/security/install-security-stack.sh\" ]; then
|
|
||||||
echo '安装 security-stack...'
|
|
||||||
bash \"$REMOTE_DIR/scripts/security/install-security-stack.sh\" || echo 'security-stack 安装失败,继续部署'
|
|
||||||
elif [ -f \"$REMOTE_DIR/scripts/security/install-perf-guard.sh\" ]; then
|
|
||||||
echo '安装 perf-guard...'
|
|
||||||
bash \"$REMOTE_DIR/scripts/security/install-perf-guard.sh\" || echo 'perf-guard 安装失败,继续部署'
|
|
||||||
fi
|
|
||||||
"
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 5: 配置环境变量...${NC}"
|
|
||||||
ssh_cmd "
|
|
||||||
cd $REMOTE_DIR
|
|
||||||
|
|
||||||
# 生成 AUTH_SECRET
|
|
||||||
AUTH_SECRET=\$(openssl rand -base64 32)
|
|
||||||
|
|
||||||
# 创建 .env.local 文件
|
|
||||||
cat > .env.local << EOF
|
|
||||||
POSTGRES_URL=postgresql://chatbot_user:ChatBot@2024!Secure@localhost:5432/ai_chatbot
|
|
||||||
AUTH_SECRET=\$AUTH_SECRET
|
|
||||||
AUTH_URL=http://$SERVER
|
|
||||||
NODE_ENV=production
|
|
||||||
EOF
|
|
||||||
|
|
||||||
echo '环境变量文件已创建'
|
|
||||||
"
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 6: 安装依赖和构建...${NC}"
|
|
||||||
ssh_cmd "
|
|
||||||
cd $REMOTE_DIR
|
|
||||||
export NVM_DIR=\"\$HOME/.nvm\"
|
|
||||||
[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"
|
|
||||||
|
|
||||||
echo '安装依赖...'
|
|
||||||
pnpm install --production=false
|
|
||||||
|
|
||||||
echo '运行数据库迁移...'
|
|
||||||
pnpm db:migrate || echo '迁移可能已存在,继续...'
|
|
||||||
|
|
||||||
echo '构建项目...'
|
|
||||||
pnpm build
|
|
||||||
"
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 7: 配置 PM2...${NC}"
|
|
||||||
# 上传 PM2 配置文件
|
|
||||||
scp_cmd "ecosystem.config.js" "$REMOTE_DIR/"
|
|
||||||
|
|
||||||
ssh_cmd "
|
|
||||||
cd $REMOTE_DIR
|
|
||||||
export NVM_DIR=\"\$HOME/.nvm\"
|
|
||||||
[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"
|
|
||||||
|
|
||||||
# 停止旧进程(如果存在)
|
|
||||||
pm2 delete ai-chatbot 2>/dev/null || true
|
|
||||||
|
|
||||||
# 启动应用
|
|
||||||
pm2 start ecosystem.config.js
|
|
||||||
pm2 save
|
|
||||||
|
|
||||||
# 设置开机自启
|
|
||||||
pm2 startup systemd -u root --hp /root 2>/dev/null || echo 'PM2 startup 已配置'
|
|
||||||
"
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 8: 配置 Nginx...${NC}"
|
|
||||||
ssh_cmd "
|
|
||||||
# 创建 Nginx 配置
|
|
||||||
cat > /etc/nginx/sites-available/ai-chatbot << 'NGINX'
|
|
||||||
server {
|
|
||||||
listen 80;
|
|
||||||
server_name $SERVER;
|
|
||||||
|
|
||||||
location / {
|
|
||||||
proxy_pass http://localhost:3000;
|
|
||||||
proxy_http_version 1.1;
|
|
||||||
proxy_set_header Upgrade \$http_upgrade;
|
|
||||||
proxy_set_header Connection 'upgrade';
|
|
||||||
proxy_set_header Host \$host;
|
|
||||||
proxy_set_header X-Real-IP \$remote_addr;
|
|
||||||
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
|
||||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
|
||||||
proxy_cache_bypass \$http_upgrade;
|
|
||||||
proxy_read_timeout 300s;
|
|
||||||
proxy_connect_timeout 75s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
NGINX
|
|
||||||
|
|
||||||
# 启用站点
|
|
||||||
ln -sf /etc/nginx/sites-available/ai-chatbot /etc/nginx/sites-enabled/
|
|
||||||
|
|
||||||
# 删除默认配置(如果存在)
|
|
||||||
rm -f /etc/nginx/sites-enabled/default
|
|
||||||
|
|
||||||
# 测试配置
|
|
||||||
nginx -t
|
|
||||||
|
|
||||||
# 重启 Nginx
|
|
||||||
systemctl restart nginx
|
|
||||||
"
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 9: 配置防火墙...${NC}"
|
|
||||||
ssh_cmd "
|
|
||||||
ufw --force enable
|
|
||||||
ufw allow 80/tcp
|
|
||||||
ufw allow 443/tcp
|
|
||||||
ufw allow 5204/tcp
|
|
||||||
ufw allow 3000/tcp
|
|
||||||
"
|
|
||||||
|
|
||||||
echo -e "${GREEN}步骤 10: 检查部署状态...${NC}"
|
|
||||||
ssh_cmd "
|
|
||||||
echo '=== PM2 状态 ==='
|
|
||||||
pm2 status
|
|
||||||
|
|
||||||
echo ''
|
|
||||||
echo '=== 应用日志(最后20行)==='
|
|
||||||
pm2 logs ai-chatbot --lines 20 --nostream
|
|
||||||
|
|
||||||
echo ''
|
|
||||||
echo '=== Nginx 状态 ==='
|
|
||||||
systemctl status nginx --no-pager -l
|
|
||||||
|
|
||||||
echo ''
|
|
||||||
echo '=== 端口监听 ==='
|
|
||||||
netstat -tulpn | grep -E ':(80|3000|5432)' || ss -tulpn | grep -E ':(80|3000|5432)'
|
|
||||||
"
|
|
||||||
|
|
||||||
# 清理本地临时文件
|
|
||||||
rm -f /tmp/deploy.tar.gz
|
|
||||||
|
|
||||||
echo -e "${GREEN}========================================${NC}"
|
|
||||||
echo -e "${GREEN}部署完成!${NC}"
|
|
||||||
echo -e "${GREEN}========================================${NC}"
|
|
||||||
echo -e "${YELLOW}访问地址: http://$SERVER${NC}"
|
|
||||||
echo -e "${YELLOW}管理员设置: http://$SERVER/admin/settings${NC}"
|
|
||||||
echo -e "${YELLOW}查看日志: ssh -p $PORT $USER@$SERVER 'pm2 logs ai-chatbot'${NC}"
|
|
||||||
echo -e "${YELLOW}查看状态: ssh -p $PORT $USER@$SERVER 'pm2 status'${NC}"
|
|
||||||
echo ""
|
|
||||||
echo -e "${YELLOW}数据库信息:${NC}"
|
|
||||||
echo -e " 数据库名: ai_chatbot"
|
|
||||||
echo -e " 用户名: chatbot_user"
|
|
||||||
echo -e " 密码: ChatBot@2024!Secure"
|
|
||||||
echo ""
|
|
||||||
echo -e "${YELLOW}注意: 请尽快修改数据库密码和服务器密码!${NC}"
|
|
||||||
|
|||||||
30
components/admin/admin-surface.tsx
Normal file
30
components/admin/admin-surface.tsx
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
"use client";
|
||||||
|
import { useEffect, useRef, type ReactNode } from "react";
|
||||||
|
|
||||||
|
export function AdminSurface({ children }: { children: ReactNode }) {
|
||||||
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
|
useEffect(() => {
|
||||||
|
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
|
||||||
|
let context: { revert: () => void } | undefined;
|
||||||
|
let disposed = false;
|
||||||
|
import("gsap").then(({ gsap }) => {
|
||||||
|
if (disposed) return;
|
||||||
|
context = gsap.context(() => {
|
||||||
|
gsap.fromTo(
|
||||||
|
ref.current,
|
||||||
|
{ y: 6, opacity: 0.85 },
|
||||||
|
{ y: 0, opacity: 1, duration: 0.25, ease: "power3.out", clearProps: "transform,opacity" },
|
||||||
|
);
|
||||||
|
}, ref);
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
disposed = true;
|
||||||
|
context?.revert();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
return (
|
||||||
|
<div ref={ref} className="space-y-6">
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
603
components/admin/media-records-panel.tsx
Normal file
603
components/admin/media-records-panel.tsx
Normal file
@ -0,0 +1,603 @@
|
|||||||
|
"use client";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import useSWR from "swr";
|
||||||
|
import {
|
||||||
|
RefreshCw,
|
||||||
|
Search,
|
||||||
|
ChevronLeft,
|
||||||
|
ChevronRight,
|
||||||
|
ExternalLink,
|
||||||
|
Download,
|
||||||
|
Star,
|
||||||
|
} from "lucide-react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { MediaThumbnail } from "./media-thumbnail";
|
||||||
|
import { AdminSurface } from "./admin-surface";
|
||||||
|
import { formatRecordTime } from "@/lib/admin/record-time";
|
||||||
|
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
|
|
||||||
|
export type MediaRecord = {
|
||||||
|
id: string;
|
||||||
|
recordType: "image" | "video";
|
||||||
|
userName: string | null;
|
||||||
|
userEmail: string | null;
|
||||||
|
userDepartment: string | null;
|
||||||
|
createdAt: string;
|
||||||
|
modelId: string | null;
|
||||||
|
chatId: string | null;
|
||||||
|
thumbnailUrl: string;
|
||||||
|
originalUrl: string;
|
||||||
|
videoUrl?: string | null;
|
||||||
|
prompt: string | null;
|
||||||
|
imageSize: string | null;
|
||||||
|
isFeatured: boolean;
|
||||||
|
featuredCategory: string | null;
|
||||||
|
featuredAt?: string | null;
|
||||||
|
};
|
||||||
|
type MediaResponse = {
|
||||||
|
images: MediaRecord[];
|
||||||
|
pagination: {
|
||||||
|
total: number;
|
||||||
|
totalPages: number;
|
||||||
|
totalImages: number;
|
||||||
|
totalVideos: number;
|
||||||
|
page: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
export async function adminFetcher(url: string) {
|
||||||
|
const response = await fetch(url);
|
||||||
|
const data = await response.json();
|
||||||
|
if (!response.ok) throw new Error(data.error || data.message || "加载失败,请重试");
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
const selectClass =
|
||||||
|
"h-10 rounded-md border bg-background px-3 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring";
|
||||||
|
function recentDate() {
|
||||||
|
const d = new Date();
|
||||||
|
d.setDate(d.getDate() - 6);
|
||||||
|
return new Intl.DateTimeFormat("en-CA", { timeZone: "Asia/Shanghai" }).format(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function MediaRecordsPanel({ categories }: { categories: string[] }) {
|
||||||
|
const [view, setView] = useState("assets");
|
||||||
|
const [search, setSearch] = useState("");
|
||||||
|
const [filters, setFilters] = useState({
|
||||||
|
search: "",
|
||||||
|
type: "all",
|
||||||
|
startDate: recentDate(),
|
||||||
|
endDate: "",
|
||||||
|
model: "",
|
||||||
|
featured: "false",
|
||||||
|
status: "all",
|
||||||
|
});
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const [selected, setSelected] = useState<MediaRecord | null>(null);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [originalFailed, setOriginalFailed] = useState(false);
|
||||||
|
const update = (changes: Partial<typeof filters>) => {
|
||||||
|
setFilters((f) => ({ ...f, ...changes }));
|
||||||
|
setPage(1);
|
||||||
|
};
|
||||||
|
useEffect(() => {
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
setFilters((f) => (f.search === search ? f : { ...f, search }));
|
||||||
|
setPage(1);
|
||||||
|
}, 350);
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}, [search]);
|
||||||
|
const { data: options } = useSWR<{
|
||||||
|
models: Array<{ id: string; modelId: string; displayName: string; providerName: string }>;
|
||||||
|
}>("/api/admin/ai-models?view=options", adminFetcher, { revalidateOnFocus: false });
|
||||||
|
const modelNames = new Map<string, string>();
|
||||||
|
for (const model of options?.models || [])
|
||||||
|
if (!modelNames.has(model.modelId)) modelNames.set(model.modelId, model.displayName);
|
||||||
|
const recordModelNames = new Map(modelNames);
|
||||||
|
for (const model of options?.models || []) recordModelNames.set(model.id, model.displayName);
|
||||||
|
const openDetails = (item: MediaRecord) => {
|
||||||
|
setSelected(item);
|
||||||
|
setOriginalFailed(false);
|
||||||
|
};
|
||||||
|
const query = new URLSearchParams({ ...filters, page: String(page), pageSize: "40" });
|
||||||
|
const { data, error, isLoading, isValidating, mutate } = useSWR<MediaResponse>(
|
||||||
|
view === "assets" ? `/api/admin/images?${query}` : null,
|
||||||
|
adminFetcher,
|
||||||
|
{ keepPreviousData: true, revalidateOnFocus: false },
|
||||||
|
);
|
||||||
|
const tasks = useSWR<{
|
||||||
|
tasks: Array<{
|
||||||
|
id: string;
|
||||||
|
userName: string;
|
||||||
|
status: string;
|
||||||
|
createdAt: string;
|
||||||
|
error: string | null;
|
||||||
|
model: string | null;
|
||||||
|
elapsedMs: number | null;
|
||||||
|
}>;
|
||||||
|
total: number;
|
||||||
|
}>(view === "tasks" ? `/api/admin/media-tasks?${query}` : null, adminFetcher, {
|
||||||
|
keepPreviousData: true,
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
});
|
||||||
|
const busy = view === "assets" ? isValidating : tasks.isValidating;
|
||||||
|
const failure = view === "assets" ? error : tasks.error;
|
||||||
|
const loading = view === "assets" ? isLoading : tasks.isLoading;
|
||||||
|
const total = view === "assets" ? data?.pagination.total : tasks.data?.total;
|
||||||
|
const pages = Math.max(1, Math.ceil((total || 0) / 40));
|
||||||
|
async function save(changes: Partial<MediaRecord>) {
|
||||||
|
if (!selected || saving) return;
|
||||||
|
setSaving(true);
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/admin/images", {
|
||||||
|
method: "PATCH",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ imageId: selected.id, ...changes }),
|
||||||
|
});
|
||||||
|
if (!response.ok) throw new Error("保存失败,请重试");
|
||||||
|
setSelected({ ...selected, ...changes });
|
||||||
|
toast.success("已保存");
|
||||||
|
await mutate();
|
||||||
|
} catch (e) {
|
||||||
|
toast.error(e instanceof Error ? e.message : "保存失败");
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<AdminSurface>
|
||||||
|
<div className="flex flex-wrap items-start justify-between gap-4">
|
||||||
|
<div>
|
||||||
|
<h2 className="text-2xl font-semibold tracking-tight">生成记录</h2>
|
||||||
|
<p className="mt-2 text-sm text-muted-foreground">
|
||||||
|
查看图片与视频产物,追踪画布任务。时间统一按北京时间展示。
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
disabled={busy}
|
||||||
|
onClick={() => (view === "assets" ? mutate() : tasks.mutate())}
|
||||||
|
>
|
||||||
|
<RefreshCw className={`mr-2 size-4 ${busy ? "motion-safe:animate-spin" : ""}`} />
|
||||||
|
刷新
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2" role="tablist" aria-label="记录视图">
|
||||||
|
{[
|
||||||
|
["assets", "产物记录"],
|
||||||
|
["tasks", "画布任务"],
|
||||||
|
].map(([key, label]) => (
|
||||||
|
<Button
|
||||||
|
key={key}
|
||||||
|
role="tab"
|
||||||
|
aria-selected={view === key}
|
||||||
|
variant={view === key ? "default" : "ghost"}
|
||||||
|
onClick={() => {
|
||||||
|
setView(key);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-wrap items-end gap-3 rounded-xl border bg-background p-4">
|
||||||
|
<label className="min-w-48 flex-1 text-xs font-medium">
|
||||||
|
用户
|
||||||
|
<div className="relative mt-2">
|
||||||
|
<Search className="absolute left-3 top-3 size-4 text-muted-foreground" />
|
||||||
|
<Input
|
||||||
|
className="h-10 pl-9"
|
||||||
|
placeholder="搜索姓名或邮箱"
|
||||||
|
value={search}
|
||||||
|
onChange={(e) => setSearch(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
{view === "assets" ? (
|
||||||
|
<label className="text-xs font-medium">
|
||||||
|
类型
|
||||||
|
<select
|
||||||
|
aria-label="产物类型"
|
||||||
|
className={`${selectClass} mt-2 block`}
|
||||||
|
value={filters.type}
|
||||||
|
onChange={(e) => update({ type: e.target.value })}
|
||||||
|
>
|
||||||
|
<option value="all">图片与视频</option>
|
||||||
|
<option value="image">只看图片</option>
|
||||||
|
<option value="video">只看视频</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
) : (
|
||||||
|
<label className="text-xs font-medium">
|
||||||
|
任务状态
|
||||||
|
<select
|
||||||
|
aria-label="任务状态"
|
||||||
|
className={`${selectClass} mt-2 block`}
|
||||||
|
value={filters.status}
|
||||||
|
onChange={(e) => update({ status: e.target.value })}
|
||||||
|
>
|
||||||
|
<option value="all">全部状态</option>
|
||||||
|
<option value="failed">失败</option>
|
||||||
|
<option value="running">运行中</option>
|
||||||
|
<option value="pending">排队中</option>
|
||||||
|
<option value="succeeded">成功</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
<label className="text-xs font-medium">
|
||||||
|
开始日期
|
||||||
|
<Input
|
||||||
|
aria-label="开始日期"
|
||||||
|
className="mt-2 h-10 w-40"
|
||||||
|
type="date"
|
||||||
|
value={filters.startDate}
|
||||||
|
onChange={(e) => update({ startDate: e.target.value })}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label className="text-xs font-medium">
|
||||||
|
结束日期
|
||||||
|
<Input
|
||||||
|
aria-label="结束日期"
|
||||||
|
className="mt-2 h-10 w-40"
|
||||||
|
type="date"
|
||||||
|
value={filters.endDate}
|
||||||
|
onChange={(e) => update({ endDate: e.target.value })}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
onClick={() => {
|
||||||
|
setSearch("");
|
||||||
|
update({
|
||||||
|
search: "",
|
||||||
|
startDate: "",
|
||||||
|
endDate: "",
|
||||||
|
model: "",
|
||||||
|
featured: "false",
|
||||||
|
type: "all",
|
||||||
|
status: "all",
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
全部时间 / 重置
|
||||||
|
</Button>
|
||||||
|
{view === "assets" && (
|
||||||
|
<div className="flex w-full flex-wrap items-center gap-3">
|
||||||
|
<select
|
||||||
|
aria-label="记录模型"
|
||||||
|
className={`${selectClass} max-w-full sm:max-w-96`}
|
||||||
|
value={filters.model}
|
||||||
|
onChange={(e) => update({ model: e.target.value })}
|
||||||
|
>
|
||||||
|
<option value="">全部模型</option>
|
||||||
|
{Array.from(modelNames, ([id, name]) => (
|
||||||
|
<option key={id} value={id}>
|
||||||
|
{name} · {id}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<label className="flex items-center gap-2 text-sm">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={filters.featured === "true"}
|
||||||
|
onChange={(e) => update({ featured: String(e.target.checked) })}
|
||||||
|
/>
|
||||||
|
只看精选
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
aria-live="polite"
|
||||||
|
className="flex items-center justify-between text-sm text-muted-foreground"
|
||||||
|
>
|
||||||
|
<span>
|
||||||
|
{total === undefined ? "正在读取记录…" : `共 ${total.toLocaleString()} 条记录`}
|
||||||
|
{view === "assets" && data
|
||||||
|
? ` · 图片 ${data.pagination.totalImages.toLocaleString()} · 视频 ${data.pagination.totalVideos.toLocaleString()}`
|
||||||
|
: ""}
|
||||||
|
</span>
|
||||||
|
{busy && !loading && <span>正在更新…</span>}
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center justify-between gap-3 text-sm text-muted-foreground">
|
||||||
|
<span>
|
||||||
|
第 {page} / {pages} 页 · 每页 40 条
|
||||||
|
</span>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
disabled={busy || page === 1}
|
||||||
|
onClick={() => setPage((p) => p - 1)}
|
||||||
|
>
|
||||||
|
上一页
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
disabled={busy || page >= pages}
|
||||||
|
onClick={() => setPage((p) => p + 1)}
|
||||||
|
>
|
||||||
|
下一页
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{failure ? (
|
||||||
|
<div role="alert" className="rounded-xl border border-destructive/30 p-6 text-sm">
|
||||||
|
<p>{failure.message}</p>
|
||||||
|
<Button
|
||||||
|
className="mt-3"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => (view === "assets" ? mutate() : tasks.mutate())}
|
||||||
|
>
|
||||||
|
重试加载
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
) : loading ? (
|
||||||
|
<div role="status" className="space-y-3 rounded-xl border p-6">
|
||||||
|
{[0, 1, 2, 3].map((x) => (
|
||||||
|
<div key={x} className="h-16 rounded-lg bg-muted motion-safe:animate-pulse" />
|
||||||
|
))}
|
||||||
|
<span className="sr-only">正在加载记录</span>
|
||||||
|
</div>
|
||||||
|
) : total === 0 ? (
|
||||||
|
<div className="rounded-xl border border-dashed p-12 text-center text-sm text-muted-foreground">
|
||||||
|
没有符合条件的记录,请调整筛选条件。
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="overflow-x-auto rounded-xl border bg-background" aria-busy={busy}>
|
||||||
|
{view === "assets" ? (
|
||||||
|
<table className="w-full min-w-[720px] text-left text-sm">
|
||||||
|
<thead className="bg-muted/50 text-xs text-muted-foreground">
|
||||||
|
<tr>
|
||||||
|
{["产物", "用户", "生成时间", "模型 / 尺寸", "精选", ""].map((v, i) => (
|
||||||
|
<th key={i} className="px-4 py-3 font-medium">
|
||||||
|
{v}
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y">
|
||||||
|
{data?.images.map((item) => (
|
||||||
|
<tr key={item.id} className="hover:bg-muted/30">
|
||||||
|
<td className="p-4">
|
||||||
|
<div>
|
||||||
|
<MediaThumbnail
|
||||||
|
url={item.thumbnailUrl}
|
||||||
|
video={item.recordType === "video"}
|
||||||
|
onOpen={() => openDetails(item)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-3">
|
||||||
|
<div className="font-medium">{item.userName || item.userEmail}</div>
|
||||||
|
<div className="mt-1 text-xs text-muted-foreground">
|
||||||
|
{item.userDepartment || "未设置部门"}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td className="whitespace-nowrap px-4 py-3 tabular-nums">
|
||||||
|
{formatRecordTime(item.createdAt)}
|
||||||
|
</td>
|
||||||
|
<td className="max-w-64 px-4 py-3">
|
||||||
|
<div className="truncate" title={item.modelId || ""}>
|
||||||
|
{recordModelNames.get(item.modelId || "") || item.modelId || "未记录模型"}
|
||||||
|
</div>
|
||||||
|
<div className="mt-1 text-xs text-muted-foreground">
|
||||||
|
{item.recordType === "video" ? "视频" : "图片"} ·{" "}
|
||||||
|
{item.imageSize || "未知尺寸"}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-3">
|
||||||
|
{item.isFeatured ? (
|
||||||
|
<span className="inline-flex items-center gap-1 text-amber-700 dark:text-amber-300">
|
||||||
|
<Star className="size-3" />
|
||||||
|
{item.featuredCategory || "精选"}
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
"—"
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-3">
|
||||||
|
<Button variant="ghost" size="sm" onClick={() => openDetails(item)}>
|
||||||
|
详情
|
||||||
|
</Button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
) : (
|
||||||
|
<table className="w-full min-w-[720px] text-left text-sm">
|
||||||
|
<thead className="bg-muted/50">
|
||||||
|
<tr>
|
||||||
|
{["用户 / 时间", "状态", "模型", "耗时", "失败原因 / 任务编号"].map((x) => (
|
||||||
|
<th key={x} className="px-4 py-3 font-medium">
|
||||||
|
{x}
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y">
|
||||||
|
{tasks.data?.tasks.map((task) => (
|
||||||
|
<tr key={task.id}>
|
||||||
|
<td className="px-4 py-4">
|
||||||
|
{task.userName}
|
||||||
|
<div className="mt-1 text-xs text-muted-foreground">
|
||||||
|
{formatRecordTime(task.createdAt)}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-4">
|
||||||
|
{{ failed: "失败", running: "运行中", succeeded: "成功", pending: "排队中" }[
|
||||||
|
task.status
|
||||||
|
] || task.status}
|
||||||
|
</td>
|
||||||
|
<td className="max-w-48 break-words px-4 py-4">{task.model || "自动选择"}</td>
|
||||||
|
<td className="px-4 py-4 tabular-nums">
|
||||||
|
{task.elapsedMs === null ? "—" : `${Math.round(task.elapsedMs / 1000)} 秒`}
|
||||||
|
</td>
|
||||||
|
<td className="max-w-xl break-words px-4 py-4">
|
||||||
|
<p className={task.error ? "text-destructive" : "text-muted-foreground"}>
|
||||||
|
{task.error || "—"}
|
||||||
|
</p>
|
||||||
|
<code className="mt-2 block text-xs text-muted-foreground">{task.id}</code>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||||
|
<span className="text-sm text-muted-foreground">
|
||||||
|
每页 40 条 · 第 {page} / {pages} 页
|
||||||
|
</span>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Button variant="outline" disabled={busy || page === 1} onClick={() => setPage(1)}>
|
||||||
|
首页
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
aria-label="上一页"
|
||||||
|
variant="outline"
|
||||||
|
disabled={busy || page === 1}
|
||||||
|
onClick={() => setPage((p) => p - 1)}
|
||||||
|
>
|
||||||
|
<ChevronLeft className="size-4" />
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
aria-label="下一页"
|
||||||
|
variant="outline"
|
||||||
|
disabled={busy || page >= pages}
|
||||||
|
onClick={() => setPage((p) => p + 1)}
|
||||||
|
>
|
||||||
|
<ChevronRight className="size-4" />
|
||||||
|
</Button>
|
||||||
|
<Button variant="outline" disabled={busy || page >= pages} onClick={() => setPage(pages)}>
|
||||||
|
末页
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Dialog
|
||||||
|
open={!!selected}
|
||||||
|
onOpenChange={(open) => {
|
||||||
|
if (!open) setSelected(null);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<DialogContent className="max-h-[90vh] max-w-4xl overflow-y-auto bg-background">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>{selected?.recordType === "video" ? "视频详情" : "图片详情"}</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
{selected && (
|
||||||
|
<div className="space-y-6">
|
||||||
|
<div className="flex min-h-40 justify-center rounded-xl bg-muted">
|
||||||
|
{selected.recordType === "video" ? (
|
||||||
|
<video
|
||||||
|
src={selected.videoUrl || selected.originalUrl}
|
||||||
|
controls
|
||||||
|
preload="none"
|
||||||
|
className="max-h-[45vh] w-full"
|
||||||
|
/>
|
||||||
|
) : originalFailed ? (
|
||||||
|
<div role="alert" className="p-8 text-sm">
|
||||||
|
原图无法加载,源文件可能已过期或存储不可达。
|
||||||
|
<Button
|
||||||
|
className="ml-3"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => setOriginalFailed(false)}
|
||||||
|
>
|
||||||
|
重试
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<img
|
||||||
|
src={selected.originalUrl}
|
||||||
|
alt="生成原图"
|
||||||
|
className="max-h-[45vh] object-contain"
|
||||||
|
onError={() => setOriginalFailed(true)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<dl className="grid gap-4 text-sm sm:grid-cols-2">
|
||||||
|
{[
|
||||||
|
["用户", selected.userName || selected.userEmail],
|
||||||
|
["部门", selected.userDepartment],
|
||||||
|
["生成时间", formatRecordTime(selected.createdAt)],
|
||||||
|
["尺寸", selected.imageSize],
|
||||||
|
["模型", recordModelNames.get(selected.modelId || "") || selected.modelId],
|
||||||
|
["精选时间", selected.featuredAt ? formatRecordTime(selected.featuredAt) : "—"],
|
||||||
|
].map(([k, v]) => (
|
||||||
|
<div key={k}>
|
||||||
|
<dt className="text-xs text-muted-foreground">{k}</dt>
|
||||||
|
<dd className="mt-1 break-words">{v || "未记录"}</dd>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</dl>
|
||||||
|
<div>
|
||||||
|
<h3 className="text-sm font-medium">提示词</h3>
|
||||||
|
<p className="mt-2 whitespace-pre-wrap break-words text-sm text-muted-foreground">
|
||||||
|
{selected.prompt || "未记录提示词"}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-wrap gap-3">
|
||||||
|
<Button variant="outline" asChild>
|
||||||
|
<a href={selected.originalUrl} target="_blank" rel="noreferrer">
|
||||||
|
<ExternalLink className="mr-2 size-4" />
|
||||||
|
打开原文件
|
||||||
|
</a>
|
||||||
|
</Button>
|
||||||
|
{selected.recordType === "image" && (
|
||||||
|
<Button variant="outline" asChild>
|
||||||
|
<a
|
||||||
|
href={buildImageProxyUrl(selected.originalUrl, { delivery: "original" })}
|
||||||
|
download
|
||||||
|
>
|
||||||
|
<Download className="mr-2 size-4" />
|
||||||
|
下载原图
|
||||||
|
</a>
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{selected.recordType === "image" && (
|
||||||
|
<div className="flex flex-wrap items-center gap-3 rounded-xl border p-4">
|
||||||
|
<select
|
||||||
|
aria-label="精选分类"
|
||||||
|
className={selectClass}
|
||||||
|
value={selected.featuredCategory || ""}
|
||||||
|
onChange={(e) =>
|
||||||
|
setSelected({ ...selected, featuredCategory: e.target.value || null })
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="">未分类</option>
|
||||||
|
{[...new Set([...categories, selected.featuredCategory].filter(Boolean))].map(
|
||||||
|
(c) => (
|
||||||
|
<option key={c} value={c!}>
|
||||||
|
{c}
|
||||||
|
</option>
|
||||||
|
),
|
||||||
|
)}
|
||||||
|
</select>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
disabled={saving}
|
||||||
|
onClick={() => save({ featuredCategory: selected.featuredCategory })}
|
||||||
|
>
|
||||||
|
保存分类
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
disabled={saving}
|
||||||
|
onClick={() => save({ isFeatured: !selected.isFeatured })}
|
||||||
|
>
|
||||||
|
<Star className="mr-2 size-4" />
|
||||||
|
{selected.isFeatured ? "取消精选" : "设为精选"}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
</AdminSurface>
|
||||||
|
);
|
||||||
|
}
|
||||||
150
components/admin/media-thumbnail.tsx
Normal file
150
components/admin/media-thumbnail.tsx
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import { ImageOff, Video, RotateCw } from "lucide-react";
|
||||||
|
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
|
import { isRetiredStorageUrl } from "@/lib/storage/retired-storage";
|
||||||
|
|
||||||
|
import { queuePreview } from "@/lib/admin/preview-queue";
|
||||||
|
|
||||||
|
export function MediaThumbnail({
|
||||||
|
url,
|
||||||
|
video = false,
|
||||||
|
onOpen,
|
||||||
|
}: {
|
||||||
|
url: string;
|
||||||
|
video?: boolean;
|
||||||
|
onOpen?: () => void;
|
||||||
|
}) {
|
||||||
|
const container = useRef<HTMLDivElement>(null);
|
||||||
|
const [state, setState] = useState<"waiting" | "loading" | "ready" | "error">("waiting");
|
||||||
|
const [attempt, setAttempt] = useState(0);
|
||||||
|
const [source, setSource] = useState("");
|
||||||
|
const [failure, setFailure] = useState("");
|
||||||
|
const retired = isRetiredStorageUrl(url);
|
||||||
|
useEffect(() => {
|
||||||
|
setState("waiting");
|
||||||
|
setSource("");
|
||||||
|
setFailure("");
|
||||||
|
if (video || retired || !url || !container.current) return;
|
||||||
|
let dispose: (() => void) | undefined;
|
||||||
|
let abort = () => {};
|
||||||
|
let objectUrl = "";
|
||||||
|
let alive = true;
|
||||||
|
const observer = new IntersectionObserver(
|
||||||
|
([entry]) => {
|
||||||
|
if (!entry.isIntersecting || dispose) return;
|
||||||
|
observer.disconnect();
|
||||||
|
dispose = queuePreview((release) => {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeout = setTimeout(() => controller.abort(), 48000);
|
||||||
|
setState("loading");
|
||||||
|
// Fetch is abortable, so leaving a page also stops in-flight previews.
|
||||||
|
const parsed = new URL(url, window.location.origin);
|
||||||
|
const target =
|
||||||
|
parsed.hostname === "scqnycdn.jiansun.vip" && !parsed.search
|
||||||
|
? `${url}?imageView2/2/w/192/q/72/format/webp`
|
||||||
|
: buildImageProxyUrl(url, {
|
||||||
|
width: 192,
|
||||||
|
quality: 72,
|
||||||
|
format: "webp",
|
||||||
|
delivery: "proxy",
|
||||||
|
});
|
||||||
|
fetch(target, { signal: controller.signal })
|
||||||
|
.then(async (response) => {
|
||||||
|
if (!response.ok) {
|
||||||
|
const detail = await response.json().catch(() => null);
|
||||||
|
throw new Error(`${detail?.error || "预览请求失败"}(HTTP ${response.status})`);
|
||||||
|
}
|
||||||
|
const blob = await response.blob();
|
||||||
|
if (!blob.type.startsWith("image/")) throw new Error("Not an image");
|
||||||
|
if (alive) {
|
||||||
|
objectUrl = URL.createObjectURL(blob);
|
||||||
|
setSource(objectUrl);
|
||||||
|
setState("ready");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
if (alive) {
|
||||||
|
setFailure(
|
||||||
|
error.name === "AbortError" ? "预览加载超时" : error.message || "预览加载失败",
|
||||||
|
);
|
||||||
|
setState("error");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
release();
|
||||||
|
});
|
||||||
|
abort = () => {
|
||||||
|
controller.abort();
|
||||||
|
clearTimeout(timeout);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{ rootMargin: "80px" },
|
||||||
|
);
|
||||||
|
observer.observe(container.current);
|
||||||
|
return () => {
|
||||||
|
alive = false;
|
||||||
|
observer.disconnect();
|
||||||
|
abort();
|
||||||
|
dispose?.();
|
||||||
|
if (objectUrl) URL.revokeObjectURL(objectUrl);
|
||||||
|
};
|
||||||
|
}, [url, video, retired, attempt]);
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
ref={container}
|
||||||
|
className="relative flex h-16 w-20 shrink-0 items-center justify-center overflow-hidden rounded-lg bg-muted"
|
||||||
|
data-preview-state={state}
|
||||||
|
>
|
||||||
|
{video ? (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
aria-label="查看视频详情"
|
||||||
|
className="flex h-full w-full items-center justify-center"
|
||||||
|
onClick={onOpen}
|
||||||
|
>
|
||||||
|
<Video className="size-5 text-muted-foreground" aria-label="视频,点击查看后播放" />
|
||||||
|
</button>
|
||||||
|
) : retired ? (
|
||||||
|
<span className="text-center text-xs text-muted-foreground">
|
||||||
|
旧存储
|
||||||
|
<br />
|
||||||
|
已停用
|
||||||
|
</span>
|
||||||
|
) : source && state === "ready" ? (
|
||||||
|
<button type="button" aria-label="查看图片详情" className="h-full w-full" onClick={onOpen}>
|
||||||
|
<img
|
||||||
|
src={source}
|
||||||
|
alt="生成结果预览"
|
||||||
|
className="h-full w-full object-contain"
|
||||||
|
onError={() => {
|
||||||
|
setFailure("图片内容无法解码");
|
||||||
|
setState("error");
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
) : state === "error" ? (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="flex h-full w-full flex-col items-center justify-center gap-1 text-xs text-muted-foreground hover:text-foreground"
|
||||||
|
title={`${failure},点击重试`}
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
setAttempt((x) => x + 1);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ImageOff className="size-4" />
|
||||||
|
加载失败
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<RotateCw
|
||||||
|
className={`size-4 text-muted-foreground ${state === "loading" ? "motion-safe:animate-spin" : ""}`}
|
||||||
|
aria-label={state === "loading" ? "正在加载预览" : "等待进入可见区域"}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
191
components/admin/model-health-panel.tsx
Normal file
191
components/admin/model-health-panel.tsx
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
"use client";
|
||||||
|
import { useState } from "react";
|
||||||
|
import useSWR from "swr";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { adminFetcher } from "./media-records-panel";
|
||||||
|
import { formatRecordTime } from "@/lib/admin/record-time";
|
||||||
|
type Event = {
|
||||||
|
id: string;
|
||||||
|
modelDisplayName: string;
|
||||||
|
modelRuntimeId: string;
|
||||||
|
providerName: string;
|
||||||
|
eventType: string;
|
||||||
|
createdAt: string;
|
||||||
|
errorMessage: string;
|
||||||
|
elapsedMs: number | null;
|
||||||
|
consecutiveFailures: number;
|
||||||
|
autoDisabled: boolean;
|
||||||
|
};
|
||||||
|
export function ModelHealthPanel({
|
||||||
|
providers,
|
||||||
|
models,
|
||||||
|
initialModelId = "",
|
||||||
|
}: {
|
||||||
|
initialModelId?: string;
|
||||||
|
providers: Array<{ id: string; name: string }>;
|
||||||
|
models: Array<{ id: string; displayName: string; providerId: string }>;
|
||||||
|
}) {
|
||||||
|
const [filters, setFilters] = useState({
|
||||||
|
providerId: "",
|
||||||
|
modelId: initialModelId,
|
||||||
|
eventType: "",
|
||||||
|
startDate: "",
|
||||||
|
endDate: "",
|
||||||
|
});
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const query = new URLSearchParams({ ...filters, page: String(page), limit: "20" });
|
||||||
|
const { data, error, isLoading, isValidating, mutate } = useSWR<{
|
||||||
|
events: Event[];
|
||||||
|
hasMore: boolean;
|
||||||
|
}>(`/api/admin/ai-model-health?${query}`, adminFetcher, {
|
||||||
|
keepPreviousData: true,
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
});
|
||||||
|
const update = (change: Partial<typeof filters>) => {
|
||||||
|
setFilters((f) => ({ ...f, ...change }));
|
||||||
|
setPage(1);
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div>
|
||||||
|
<h3 className="text-lg font-semibold">运行健康</h3>
|
||||||
|
<p className="mt-2 text-sm text-muted-foreground">
|
||||||
|
按上游和模型追踪故障,查看完整原因与发生时间。
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-wrap gap-3">
|
||||||
|
{[
|
||||||
|
["providerId", "全部上游", providers.map((p) => [p.id, p.name])],
|
||||||
|
[
|
||||||
|
"modelId",
|
||||||
|
"全部模型",
|
||||||
|
models
|
||||||
|
.filter((m) => !filters.providerId || m.providerId === filters.providerId)
|
||||||
|
.map((m) => [m.id, m.displayName]),
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"eventType",
|
||||||
|
"全部错误",
|
||||||
|
[
|
||||||
|
["quota", "额度 / 余额"],
|
||||||
|
["timeout", "超时"],
|
||||||
|
["unavailable", "不可用"],
|
||||||
|
["invalid_model", "模型不可用"],
|
||||||
|
["download_error", "结果下载失败"],
|
||||||
|
["error", "其他错误"],
|
||||||
|
["success", "成功"],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
].map(([key, label, options]) => (
|
||||||
|
<select
|
||||||
|
key={key as string}
|
||||||
|
aria-label={label as string}
|
||||||
|
className="h-10 max-w-64 rounded-md border bg-background px-3 text-sm"
|
||||||
|
value={filters[key as keyof typeof filters]}
|
||||||
|
onChange={(e) =>
|
||||||
|
update({
|
||||||
|
[key as string]: e.target.value,
|
||||||
|
...(key === "providerId" ? { modelId: "" } : {}),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<option value="">{label as string}</option>
|
||||||
|
{(options as string[][]).map(([value, text]) => (
|
||||||
|
<option key={value} value={value}>
|
||||||
|
{text}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
))}
|
||||||
|
<Input
|
||||||
|
aria-label="故障开始日期"
|
||||||
|
className="h-10 w-40"
|
||||||
|
type="date"
|
||||||
|
value={filters.startDate}
|
||||||
|
onChange={(e) => update({ startDate: e.target.value })}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
aria-label="故障结束日期"
|
||||||
|
className="h-10 w-40"
|
||||||
|
type="date"
|
||||||
|
value={filters.endDate}
|
||||||
|
onChange={(e) => update({ endDate: e.target.value })}
|
||||||
|
/>
|
||||||
|
<Button variant="outline" disabled={isValidating} onClick={() => mutate()}>
|
||||||
|
刷新日志
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
{error ? (
|
||||||
|
<p role="alert" className="p-6 text-sm text-destructive">
|
||||||
|
日志加载失败,请点击刷新重试。
|
||||||
|
</p>
|
||||||
|
) : isLoading ? (
|
||||||
|
<p role="status" className="p-6 text-sm">
|
||||||
|
正在加载故障日志…
|
||||||
|
</p>
|
||||||
|
) : (
|
||||||
|
<div className="divide-y rounded-xl border">
|
||||||
|
{data?.events.map((event) => (
|
||||||
|
<div key={event.id} className="p-4">
|
||||||
|
<div className="flex flex-wrap items-center gap-3 text-sm">
|
||||||
|
<strong className="font-semibold">
|
||||||
|
{event.modelDisplayName || event.modelRuntimeId || "已删除模型"}
|
||||||
|
</strong>
|
||||||
|
<span className="text-muted-foreground">{event.providerName || "未知上游"}</span>
|
||||||
|
<span className="rounded bg-muted px-2 py-1 text-xs">
|
||||||
|
{{
|
||||||
|
quota: "额度 / 余额",
|
||||||
|
timeout: "超时",
|
||||||
|
unavailable: "不可用",
|
||||||
|
error: "服务错误",
|
||||||
|
invalid_model: "模型不可用",
|
||||||
|
download_error: "结果下载失败",
|
||||||
|
success: "成功",
|
||||||
|
}[event.eventType] || event.eventType}
|
||||||
|
</span>
|
||||||
|
<time className="ml-auto text-xs tabular-nums text-muted-foreground">
|
||||||
|
{formatRecordTime(event.createdAt)}
|
||||||
|
</time>
|
||||||
|
</div>
|
||||||
|
<p className="mt-3 whitespace-pre-wrap break-words text-sm">
|
||||||
|
{event.errorMessage || "未记录详细原因"}
|
||||||
|
</p>
|
||||||
|
<div className="mt-2 flex gap-4 text-xs text-muted-foreground">
|
||||||
|
<span>连续失败 {event.consecutiveFailures} 次</span>
|
||||||
|
{event.elapsedMs !== null && (
|
||||||
|
<span>耗时 {Math.round(event.elapsedMs / 1000)} 秒</span>
|
||||||
|
)}
|
||||||
|
{event.autoDisabled && <span>已自动停用</span>}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
{!data?.events.length && (
|
||||||
|
<p className="p-12 text-center text-sm text-muted-foreground">该范围内没有故障记录。</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="text-sm text-muted-foreground">
|
||||||
|
第 {page} 页{isValidating && " · 正在更新…"}
|
||||||
|
</span>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
disabled={page === 1 || isValidating}
|
||||||
|
onClick={() => setPage((p) => p - 1)}
|
||||||
|
>
|
||||||
|
上一页
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
disabled={!data?.hasMore || isValidating}
|
||||||
|
onClick={() => setPage((p) => p + 1)}
|
||||||
|
>
|
||||||
|
下一页
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
326
components/admin/model-management-list.tsx
Normal file
326
components/admin/model-management-list.tsx
Normal file
@ -0,0 +1,326 @@
|
|||||||
|
"use client";
|
||||||
|
import { useMemo, useState } from "react";
|
||||||
|
import { Search, RefreshCw, ChevronLeft, ChevronRight } from "lucide-react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import type { AiModel } from "@/lib/db/schema";
|
||||||
|
import {
|
||||||
|
is3DModelDescriptor,
|
||||||
|
isVideoModelDescriptor,
|
||||||
|
isImageGenerationModelDescriptor,
|
||||||
|
} from "@/lib/ai/model-classification";
|
||||||
|
|
||||||
|
type Model = AiModel & { isAgentModel?: boolean; isMultimodal?: boolean };
|
||||||
|
type Provider = { id: string; name: string; type: string; isEnabled: boolean };
|
||||||
|
const selectClass = "h-10 rounded-md border bg-background px-3 text-sm";
|
||||||
|
export function ModelManagementList({
|
||||||
|
models,
|
||||||
|
providers,
|
||||||
|
error,
|
||||||
|
loading,
|
||||||
|
onEdit,
|
||||||
|
onDelete,
|
||||||
|
onRestore,
|
||||||
|
onRefresh,
|
||||||
|
onHealth,
|
||||||
|
}: {
|
||||||
|
models: Model[];
|
||||||
|
providers: Provider[];
|
||||||
|
error?: Error;
|
||||||
|
loading: boolean;
|
||||||
|
onEdit: (model: Model) => Promise<void>;
|
||||||
|
onDelete: (id: string) => void;
|
||||||
|
onRestore: (model: Model) => void;
|
||||||
|
onRefresh: () => void;
|
||||||
|
onHealth?: (id: string) => void;
|
||||||
|
}) {
|
||||||
|
const [search, setSearch] = useState("");
|
||||||
|
const [provider, setProvider] = useState("");
|
||||||
|
const [status, setStatus] = useState("enabled");
|
||||||
|
const [kind, setKind] = useState("");
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const [opening, setOpening] = useState<string | null>(null);
|
||||||
|
const rows = useMemo(
|
||||||
|
() =>
|
||||||
|
models
|
||||||
|
.map((model) => {
|
||||||
|
const upstream = providers.find((p) => p.id === model.providerId);
|
||||||
|
const descriptor = { ...model, providerType: upstream?.type };
|
||||||
|
const type = is3DModelDescriptor(descriptor)
|
||||||
|
? "3d"
|
||||||
|
: isVideoModelDescriptor(descriptor)
|
||||||
|
? "video"
|
||||||
|
: isImageGenerationModelDescriptor(descriptor)
|
||||||
|
? "image"
|
||||||
|
: "chat";
|
||||||
|
return { model, upstream, type };
|
||||||
|
})
|
||||||
|
.filter(({ model, upstream, type }) => {
|
||||||
|
const text =
|
||||||
|
`${model.displayName} ${model.modelId} ${upstream?.name || ""}`.toLowerCase();
|
||||||
|
return (
|
||||||
|
text.includes(search.trim().toLowerCase()) &&
|
||||||
|
(!provider || model.providerId === provider) &&
|
||||||
|
(!kind || kind === type) &&
|
||||||
|
(status === "all" ||
|
||||||
|
(status === "enabled" && model.isEnabled) ||
|
||||||
|
(status === "disabled" && !model.isEnabled) ||
|
||||||
|
(status === "unhealthy" && model.healthStatus !== "healthy") ||
|
||||||
|
(status === "visible" && !model.isHidden))
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
[models, providers, search, provider, status, kind],
|
||||||
|
);
|
||||||
|
const pages = Math.max(1, Math.ceil(rows.length / 25));
|
||||||
|
const currentPage = Math.min(page, pages);
|
||||||
|
const change = (fn: () => void) => {
|
||||||
|
fn();
|
||||||
|
setPage(1);
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="flex flex-wrap gap-3">
|
||||||
|
<div className="relative min-w-56 flex-1">
|
||||||
|
<Search className="absolute left-3 top-3 size-4 text-muted-foreground" />
|
||||||
|
<Input
|
||||||
|
className="h-10 pl-9"
|
||||||
|
aria-label="搜索模型"
|
||||||
|
placeholder="搜索模型名称、ID 或上游"
|
||||||
|
value={search}
|
||||||
|
onChange={(e) => change(() => setSearch(e.target.value))}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<select
|
||||||
|
aria-label="模型上游"
|
||||||
|
className={selectClass}
|
||||||
|
value={provider}
|
||||||
|
onChange={(e) => change(() => setProvider(e.target.value))}
|
||||||
|
>
|
||||||
|
<option value="">全部上游</option>
|
||||||
|
{providers.map((p) => (
|
||||||
|
<option key={p.id} value={p.id}>
|
||||||
|
{p.name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<select
|
||||||
|
aria-label="模型类型"
|
||||||
|
className={selectClass}
|
||||||
|
value={kind}
|
||||||
|
onChange={(e) => change(() => setKind(e.target.value))}
|
||||||
|
>
|
||||||
|
<option value="">全部类型</option>
|
||||||
|
<option value="image">生图</option>
|
||||||
|
<option value="video">视频</option>
|
||||||
|
<option value="chat">对话</option>
|
||||||
|
<option value="3d">3D / CAD</option>
|
||||||
|
</select>
|
||||||
|
<select
|
||||||
|
aria-label="模型状态"
|
||||||
|
className={selectClass}
|
||||||
|
value={status}
|
||||||
|
onChange={(e) => change(() => setStatus(e.target.value))}
|
||||||
|
>
|
||||||
|
<option value="enabled">已启用</option>
|
||||||
|
<option value="all">全部模型</option>
|
||||||
|
<option value="disabled">已停用</option>
|
||||||
|
<option value="unhealthy">运行异常</option>
|
||||||
|
<option value="visible">前台可见</option>
|
||||||
|
</select>
|
||||||
|
<Button variant="outline" onClick={onRefresh} aria-label="刷新模型">
|
||||||
|
<RefreshCw className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{loading
|
||||||
|
? "正在加载模型…"
|
||||||
|
: `筛选结果 ${rows.length} 个 · 已启用 ${models.filter((m) => m.isEnabled).length} 个 · 全部 ${models.length} 个`}
|
||||||
|
</p>
|
||||||
|
<div className="flex items-center justify-end gap-2 text-sm text-muted-foreground">
|
||||||
|
<span>
|
||||||
|
第 {currentPage} / {pages} 页
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
disabled={currentPage === 1}
|
||||||
|
onClick={() => setPage(currentPage - 1)}
|
||||||
|
>
|
||||||
|
上一页
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
disabled={currentPage === pages}
|
||||||
|
onClick={() => setPage(currentPage + 1)}
|
||||||
|
>
|
||||||
|
下一页
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
{error ? (
|
||||||
|
<div role="alert" className="rounded-xl border p-6 text-sm">
|
||||||
|
模型加载失败。
|
||||||
|
<Button variant="outline" onClick={onRefresh}>
|
||||||
|
重试
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
) : loading ? (
|
||||||
|
<div role="status" className="h-48 rounded-xl bg-muted motion-safe:animate-pulse" />
|
||||||
|
) : (
|
||||||
|
<div className="overflow-x-auto rounded-xl border">
|
||||||
|
<table className="w-full min-w-[850px] text-left text-sm">
|
||||||
|
<thead className="bg-muted/50 text-xs text-muted-foreground">
|
||||||
|
<tr>
|
||||||
|
{["模型", "上游 / 类型", "启用", "前台展示", "运行健康", "操作"].map((x) => (
|
||||||
|
<th key={x} className="px-4 py-3 font-medium">
|
||||||
|
{x}
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y">
|
||||||
|
{rows
|
||||||
|
.slice((currentPage - 1) * 25, currentPage * 25)
|
||||||
|
.map(({ model, upstream, type }) => (
|
||||||
|
<tr key={model.id} className="hover:bg-muted/30">
|
||||||
|
<td className="max-w-64 px-4 py-4">
|
||||||
|
<div className="font-medium">{model.displayName}</div>
|
||||||
|
<div
|
||||||
|
className="mt-1 truncate text-xs text-muted-foreground"
|
||||||
|
title={model.modelId}
|
||||||
|
>
|
||||||
|
{model.modelId}
|
||||||
|
</div>
|
||||||
|
<div className="mt-2 flex flex-wrap gap-2 text-xs text-muted-foreground">
|
||||||
|
{model.isDefault && <span>默认模型</span>}
|
||||||
|
{model.isDefaultTextEditModel && <span>文字编辑默认</span>}
|
||||||
|
{model.isAgentModel && <span>Agent</span>}
|
||||||
|
{model.isMultimodal && <span>多模态</span>}
|
||||||
|
{model.isSubModel && <span>子模型</span>}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-4">
|
||||||
|
{upstream?.name || "未知上游"}
|
||||||
|
<div className="mt-1 text-xs text-muted-foreground">
|
||||||
|
{{ image: "生图", video: "视频", chat: "对话", "3d": "3D / CAD" }[type]}
|
||||||
|
{upstream && !upstream.isEnabled ? " · 上游已停用" : ""}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td className="whitespace-nowrap px-4 py-4">
|
||||||
|
<span
|
||||||
|
className={`rounded-md px-2 py-1 text-xs ${model.isEnabled ? "bg-emerald-100 text-emerald-800 dark:bg-emerald-950 dark:text-emerald-200" : "bg-muted text-muted-foreground"}`}
|
||||||
|
>
|
||||||
|
{model.isEnabled ? "已启用" : "已停用"}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-4">
|
||||||
|
{model.isHidden || model.isSubModel ? "隐藏" : "可见"}
|
||||||
|
</td>
|
||||||
|
<td className="max-w-72 px-4 py-4">
|
||||||
|
<span
|
||||||
|
className={
|
||||||
|
model.healthStatus !== "healthy"
|
||||||
|
? "text-amber-700 dark:text-amber-300"
|
||||||
|
: "text-muted-foreground"
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{model.healthStatus === "auto_disabled"
|
||||||
|
? "自动停用"
|
||||||
|
: model.healthStatus === "degraded"
|
||||||
|
? "观察中"
|
||||||
|
: "正常"}
|
||||||
|
</span>
|
||||||
|
{model.healthConsecutiveFailures > 0 && (
|
||||||
|
<span className="ml-2 text-xs text-muted-foreground">
|
||||||
|
连续失败 {model.healthConsecutiveFailures} 次
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{model.healthLastError && (
|
||||||
|
<details className="mt-2 text-xs">
|
||||||
|
<summary className="cursor-pointer text-muted-foreground">
|
||||||
|
查看最近原因
|
||||||
|
</summary>
|
||||||
|
<p className="mt-2 whitespace-pre-wrap break-words">
|
||||||
|
{model.healthLastError}
|
||||||
|
</p>
|
||||||
|
</details>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="mt-2 block text-xs underline underline-offset-4"
|
||||||
|
onClick={() => onHealth?.(model.id)}
|
||||||
|
>
|
||||||
|
故障记录
|
||||||
|
</button>
|
||||||
|
<div className="mt-2 text-xs text-muted-foreground">
|
||||||
|
{model.excludeFromHealthBan && "不自动封禁 "}
|
||||||
|
{model.excludeFromFailover && "不参与迁移"}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-4">
|
||||||
|
<div className="flex gap-1">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
disabled={opening !== null}
|
||||||
|
onClick={async () => {
|
||||||
|
setOpening(model.id);
|
||||||
|
try {
|
||||||
|
await onEdit(model);
|
||||||
|
} finally {
|
||||||
|
setOpening(null);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{opening === model.id ? "加载中…" : "编辑"}
|
||||||
|
</Button>
|
||||||
|
{model.healthStatus === "auto_disabled" && (
|
||||||
|
<Button variant="outline" size="sm" onClick={() => onRestore(model)}>
|
||||||
|
恢复
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
className="text-destructive"
|
||||||
|
onClick={() => onDelete(model.id)}
|
||||||
|
>
|
||||||
|
删除
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
{rows.length === 0 && (
|
||||||
|
<p className="p-12 text-center text-sm text-muted-foreground">没有符合条件的模型。</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="flex items-center justify-between text-sm text-muted-foreground">
|
||||||
|
<span>
|
||||||
|
每页 25 个 · 第 {currentPage} / {pages} 页
|
||||||
|
</span>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Button
|
||||||
|
aria-label="上一页模型"
|
||||||
|
variant="outline"
|
||||||
|
disabled={currentPage === 1}
|
||||||
|
onClick={() => setPage(currentPage - 1)}
|
||||||
|
>
|
||||||
|
<ChevronLeft className="size-4" />
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
aria-label="下一页模型"
|
||||||
|
variant="outline"
|
||||||
|
disabled={currentPage === pages}
|
||||||
|
onClick={() => setPage(currentPage + 1)}
|
||||||
|
>
|
||||||
|
<ChevronRight className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
||||||
|
|
||||||
import { useChat } from "@ai-sdk/react";
|
import { useChat } from "@ai-sdk/react";
|
||||||
import { DefaultChatTransport } from "ai";
|
import { DefaultChatTransport } from "ai";
|
||||||
@ -575,31 +576,11 @@ export function AgentGenerationWorkspace({
|
|||||||
toast.error("图片地址无效");
|
toast.error("图片地址无效");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filename = buildDownloadFilename(raw);
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(raw);
|
await downloadOriginalImage({ url: raw, name: buildDownloadFilename(raw) });
|
||||||
if (!response.ok) {
|
toast.success("原图已开始下载");
|
||||||
throw new Error(`download failed: ${response.status}`);
|
} catch (error) {
|
||||||
}
|
toast.error(getCanvasImageDownloadErrorMessage(error));
|
||||||
const blob = await response.blob();
|
|
||||||
const blobUrl = window.URL.createObjectURL(blob);
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = blobUrl;
|
|
||||||
link.download = filename;
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
window.URL.revokeObjectURL(blobUrl);
|
|
||||||
} catch {
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = raw;
|
|
||||||
link.target = "_blank";
|
|
||||||
link.rel = "noopener noreferrer";
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
toast.message("已打开原图,可右键另存为");
|
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,10 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
||||||
|
|
||||||
import { useChat } from "@ai-sdk/react";
|
import { useChat } from "@ai-sdk/react";
|
||||||
|
import { BEFORE_PAGE_REFRESH, type PageRefreshRequest } from "@/lib/client/page-refresh";
|
||||||
import { appendGenerationMessage, mergeGenerationHistory, parseAppendedGenerationMessage, recoverReplacementRun } from "@/lib/agents/replacement-run-recovery";
|
import { appendGenerationMessage, mergeGenerationHistory, parseAppendedGenerationMessage, recoverReplacementRun } from "@/lib/agents/replacement-run-recovery";
|
||||||
|
import { getSceneRunResult } from "@/lib/agents/scene-run-recovery";
|
||||||
import type { DetailReplacementBatchCard } from "@/lib/agents/detail-replacement-batch";
|
import type { DetailReplacementBatchCard } from "@/lib/agents/detail-replacement-batch";
|
||||||
import { DefaultChatTransport } from "ai";
|
import { DefaultChatTransport } from "ai";
|
||||||
import {
|
import {
|
||||||
@ -156,6 +159,7 @@ import type {
|
|||||||
} from "@/types/agent";
|
} from "@/types/agent";
|
||||||
import {
|
import {
|
||||||
createEmptyAgentModeWorkspaceState,
|
createEmptyAgentModeWorkspaceState,
|
||||||
|
MAX_DETAIL_REPLACEMENT_REFERENCES,
|
||||||
} from "@/lib/agents/mode-workspace-state";
|
} from "@/lib/agents/mode-workspace-state";
|
||||||
import {
|
import {
|
||||||
createEmptyAgentGenerationProjectWorkspaceState,
|
createEmptyAgentGenerationProjectWorkspaceState,
|
||||||
@ -173,6 +177,7 @@ export interface AgentGenerationWorkspaceDarkV2Props {
|
|||||||
workflowFeatureSettings?: AgentWorkflowPageFeatureSettings | null;
|
workflowFeatureSettings?: AgentWorkflowPageFeatureSettings | null;
|
||||||
workflowPromptOptimizationEnabled?: boolean;
|
workflowPromptOptimizationEnabled?: boolean;
|
||||||
getLatestWorkflowContext?: () => AgentWorkflowComposerContext | null;
|
getLatestWorkflowContext?: () => AgentWorkflowComposerContext | null;
|
||||||
|
getWorkflowPromptStatus?: () => "ready" | "loading" | "error";
|
||||||
onBack?: () => void;
|
onBack?: () => void;
|
||||||
brandStorePanel?: ReactNode;
|
brandStorePanel?: ReactNode;
|
||||||
materialSelectionPanel?: ReactNode;
|
materialSelectionPanel?: ReactNode;
|
||||||
@ -209,6 +214,7 @@ export interface AgentGenerationWorkspaceDarkV2Props {
|
|||||||
interface AgentWorkflowModelOption {
|
interface AgentWorkflowModelOption {
|
||||||
id: string;
|
id: string;
|
||||||
displayName: string;
|
displayName: string;
|
||||||
|
providerName?: string;
|
||||||
providerType: string;
|
providerType: string;
|
||||||
supportsImageGen?: boolean;
|
supportsImageGen?: boolean;
|
||||||
isMultimodal?: boolean;
|
isMultimodal?: boolean;
|
||||||
@ -2639,7 +2645,9 @@ function getStoredAgentPendingGenerations(): LocalGenerationPendingState[] {
|
|||||||
? record.requestedCardIds
|
? record.requestedCardIds
|
||||||
.map((entry) => String(entry || "").trim())
|
.map((entry) => String(entry || "").trim())
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.slice(0, MAX_AGENT_MODE_REFERENCE_COUNT)
|
.slice(0, record.modeType === "detail_replacement"
|
||||||
|
? MAX_DETAIL_REPLACEMENT_REFERENCES
|
||||||
|
: MAX_AGENT_MODE_REFERENCE_COUNT)
|
||||||
: null,
|
: null,
|
||||||
} satisfies LocalGenerationPendingState;
|
} satisfies LocalGenerationPendingState;
|
||||||
});
|
});
|
||||||
@ -3082,6 +3090,7 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
workflowFeatureSettings = null,
|
workflowFeatureSettings = null,
|
||||||
workflowPromptOptimizationEnabled = true,
|
workflowPromptOptimizationEnabled = true,
|
||||||
getLatestWorkflowContext,
|
getLatestWorkflowContext,
|
||||||
|
getWorkflowPromptStatus,
|
||||||
onBack,
|
onBack,
|
||||||
brandStorePanel,
|
brandStorePanel,
|
||||||
materialSelectionPanel,
|
materialSelectionPanel,
|
||||||
@ -3379,12 +3388,13 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const { data: modelsData } = useSWR<{
|
const { data: modelsData, error: modelsError, mutate: refreshModels } = useSWR<{
|
||||||
models: AgentWorkflowModelOption[];
|
models: AgentWorkflowModelOption[];
|
||||||
defaultModelId?: string | null;
|
defaultModelId?: string | null;
|
||||||
agentWorkflowConfiguredDefaultModelId?: string | null;
|
agentWorkflowConfiguredDefaultModelId?: string | null;
|
||||||
agentWorkflowDefaultModelId?: string | null;
|
agentWorkflowDefaultModelId?: string | null;
|
||||||
agentWorkflowConfiguredModels?: AgentWorkflowModelOption[];
|
agentWorkflowConfiguredModels?: AgentWorkflowModelOption[];
|
||||||
|
detailReplacementModel?: AgentWorkflowModelOption | null;
|
||||||
agentModel?: any;
|
agentModel?: any;
|
||||||
}>("/api/models", fetcher);
|
}>("/api/models", fetcher);
|
||||||
|
|
||||||
@ -3430,17 +3440,6 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
const activeBusinessModeDefaultModelId = !requiresPresetSelection
|
const activeBusinessModeDefaultModelId = !requiresPresetSelection
|
||||||
? String(activeBusinessMode?.defaultModelId || "").trim() || null
|
? String(activeBusinessMode?.defaultModelId || "").trim() || null
|
||||||
: null;
|
: null;
|
||||||
const lockedWorkflowModelId =
|
|
||||||
activeBusinessModeDefaultModelId ||
|
|
||||||
selectedAgentPreset?.defaultModelId ||
|
|
||||||
modelsData?.agentWorkflowConfiguredDefaultModelId ||
|
|
||||||
modelsData?.agentWorkflowDefaultModelId ||
|
|
||||||
null;
|
|
||||||
const isWorkflowModelLocked = Boolean(lockedWorkflowModelId);
|
|
||||||
const effectiveDefaultModelId =
|
|
||||||
lockedWorkflowModelId ||
|
|
||||||
modelsData?.defaultModelId ||
|
|
||||||
null;
|
|
||||||
const presetDrivenResultModeType: AgentGenerationResultModeType =
|
const presetDrivenResultModeType: AgentGenerationResultModeType =
|
||||||
selectedAgentPreset?.type === "image" && presetModeConfig.modeType === "detail_replacement"
|
selectedAgentPreset?.type === "image" && presetModeConfig.modeType === "detail_replacement"
|
||||||
? detailReplacementMethod === "ai_detail_generate"
|
? detailReplacementMethod === "ai_detail_generate"
|
||||||
@ -3456,6 +3455,19 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
const currentResultModeType: AgentGenerationResultModeType = requiresPresetSelection
|
const currentResultModeType: AgentGenerationResultModeType = requiresPresetSelection
|
||||||
? presetDrivenResultModeType
|
? presetDrivenResultModeType
|
||||||
: businessModeResultModeType;
|
: businessModeResultModeType;
|
||||||
|
const detailReplacementModel = currentResultModeType === "detail_replacement"
|
||||||
|
? modelsData?.detailReplacementModel
|
||||||
|
: null;
|
||||||
|
const isDetailReplacementModelLoading = currentResultModeType === "detail_replacement" && !modelsData;
|
||||||
|
const lockedWorkflowModelId =
|
||||||
|
detailReplacementModel?.id ||
|
||||||
|
activeBusinessModeDefaultModelId ||
|
||||||
|
selectedAgentPreset?.defaultModelId ||
|
||||||
|
modelsData?.agentWorkflowConfiguredDefaultModelId ||
|
||||||
|
modelsData?.agentWorkflowDefaultModelId ||
|
||||||
|
null;
|
||||||
|
const isWorkflowModelLocked = Boolean(lockedWorkflowModelId);
|
||||||
|
const effectiveDefaultModelId = lockedWorkflowModelId || modelsData?.defaultModelId || null;
|
||||||
const isSceneGenerationPreset = currentResultModeType === "scene_generation";
|
const isSceneGenerationPreset = currentResultModeType === "scene_generation";
|
||||||
const isDetailReplacementPreset =
|
const isDetailReplacementPreset =
|
||||||
currentResultModeType === "detail_replacement" ||
|
currentResultModeType === "detail_replacement" ||
|
||||||
@ -4107,12 +4119,15 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
);
|
);
|
||||||
const executableModelOptions = useMemo(() => {
|
const executableModelOptions = useMemo(() => {
|
||||||
const seen = new Set<string>();
|
const seen = new Set<string>();
|
||||||
return [...selectableModels, ...configuredWorkflowModels].filter((model) => {
|
const workflowModels = detailReplacementModel
|
||||||
|
? [detailReplacementModel, ...configuredWorkflowModels]
|
||||||
|
: configuredWorkflowModels;
|
||||||
|
return [...selectableModels, ...workflowModels].filter((model) => {
|
||||||
if (seen.has(model.id)) return false;
|
if (seen.has(model.id)) return false;
|
||||||
seen.add(model.id);
|
seen.add(model.id);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}, [configuredWorkflowModels, selectableModels]);
|
}, [configuredWorkflowModels, detailReplacementModel, selectableModels]);
|
||||||
const executableModelIdSet = useMemo(
|
const executableModelIdSet = useMemo(
|
||||||
() => new Set(executableModelOptions.map((model) => model.id)),
|
() => new Set(executableModelOptions.map((model) => model.id)),
|
||||||
[executableModelOptions]
|
[executableModelOptions]
|
||||||
@ -4131,8 +4146,9 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
selectableModels[0],
|
selectableModels[0],
|
||||||
[executableModelOptions, lockedWorkflowModel, selectableModels, selectedModelId]
|
[executableModelOptions, lockedWorkflowModel, selectableModels, selectedModelId]
|
||||||
);
|
);
|
||||||
const lockedWorkflowModelLabel =
|
const lockedWorkflowModelLabel = detailReplacementModel?.providerName
|
||||||
lockedWorkflowModel?.displayName ||
|
? `${detailReplacementModel.displayName} 「${detailReplacementModel.providerName}」`
|
||||||
|
: lockedWorkflowModel?.displayName ||
|
||||||
selectedAgentPreset?.defaultModel?.displayName ||
|
selectedAgentPreset?.defaultModel?.displayName ||
|
||||||
(lockedWorkflowModelId ? `配置模型不可用(${lockedWorkflowModelId})` : "未配置");
|
(lockedWorkflowModelId ? `配置模型不可用(${lockedWorkflowModelId})` : "未配置");
|
||||||
const [activeToolStep, setActiveToolStep] = useState<ToolStepKey>("settings");
|
const [activeToolStep, setActiveToolStep] = useState<ToolStepKey>("settings");
|
||||||
@ -4169,6 +4185,7 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
? "agent-anchor-materials"
|
? "agent-anchor-materials"
|
||||||
: "agent-anchor-generate";
|
: "agent-anchor-generate";
|
||||||
|
|
||||||
|
const [finishedSceneRequestId, setFinishedSceneRequestId] = useState<string | null>(null);
|
||||||
const {
|
const {
|
||||||
messages,
|
messages,
|
||||||
setMessages,
|
setMessages,
|
||||||
@ -4189,7 +4206,11 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
void refreshHistory();
|
void refreshHistory();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onFinish() { void refreshHistory(); },
|
onFinish({ messages: finishedMessages }) {
|
||||||
|
const lastUser = finishedMessages.filter((message) => message.role === "user").at(-1);
|
||||||
|
setFinishedSceneRequestId(lastUser?.id || null);
|
||||||
|
void refreshHistory();
|
||||||
|
},
|
||||||
experimental_throttle: 100,
|
experimental_throttle: 100,
|
||||||
generateId: generateUUID,
|
generateId: generateUUID,
|
||||||
transport: new DefaultChatTransport({
|
transport: new DefaultChatTransport({
|
||||||
@ -4872,6 +4893,14 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
return recoverReplacementRun({ messages, ...pendingGeneration });
|
return recoverReplacementRun({ messages, ...pendingGeneration });
|
||||||
}, [messages, pendingGeneration]);
|
}, [messages, pendingGeneration]);
|
||||||
const transportIsGenerating = status === "streaming" || status === "submitted";
|
const transportIsGenerating = status === "streaming" || status === "submitted";
|
||||||
|
useEffect(() => {
|
||||||
|
const beforeRefresh = (event: Event) => {
|
||||||
|
if (transportIsGenerating || pendingGeneration)
|
||||||
|
(event as CustomEvent<PageRefreshRequest>).detail.block("当前生图任务仍在处理中,请等待结果返回后再刷新。原任务无需重新提交。");
|
||||||
|
};
|
||||||
|
window.addEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
||||||
|
return () => window.removeEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
||||||
|
}, [transportIsGenerating, pendingGeneration]);
|
||||||
const pendingGenerationProgressCount = useMemo(() => {
|
const pendingGenerationProgressCount = useMemo(() => {
|
||||||
if (!pendingGeneration) {
|
if (!pendingGeneration) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -5482,6 +5511,17 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
setActiveResultModeType(currentResultModeType);
|
setActiveResultModeType(currentResultModeType);
|
||||||
}, [currentResultModeType]);
|
}, [currentResultModeType]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!pendingGeneration?.requestMessageId || pendingGeneration.modeType !== "scene_generation" || transportIsGenerating) return;
|
||||||
|
const result = getSceneRunResult(messages, pendingGeneration.requestMessageId);
|
||||||
|
const persisted = getSceneRunResult(historyMessages, pendingGeneration.requestMessageId);
|
||||||
|
if (!result || (finishedSceneRequestId !== pendingGeneration.requestMessageId && !persisted?.count)) return;
|
||||||
|
setPendingGeneration(null);
|
||||||
|
if (result.count < pendingGeneration.expectedCount) {
|
||||||
|
toast.warning(`本次生成已结束,收到 ${result.count}/${pendingGeneration.expectedCount} 张图片。缺少的图片未继续生成,可调整张数后重新提交。`);
|
||||||
|
}
|
||||||
|
}, [pendingGeneration, transportIsGenerating, finishedSceneRequestId, messages, historyMessages]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!pendingGeneration || pendingGeneration.backendTaskId || !replacementRunRecovery.done) return;
|
if (!pendingGeneration || pendingGeneration.backendTaskId || !replacementRunRecovery.done) return;
|
||||||
// The backend has settled every requested card; an open SSE connection is no longer work.
|
// The backend has settled every requested card; an open SSE connection is no longer work.
|
||||||
@ -5852,6 +5892,7 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
cache: "no-store",
|
cache: "no-store",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
scope: params.scope === "detail" ? "style-detail" : undefined,
|
||||||
folderName: params.zipName,
|
folderName: params.zipName,
|
||||||
files: normalizedUrls.map((imageUrl) => ({
|
files: normalizedUrls.map((imageUrl) => ({
|
||||||
url: imageUrl,
|
url: imageUrl,
|
||||||
@ -6287,10 +6328,24 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
toast.error(`请先查询并选择${detailReferenceSourceLabel}参考图`);
|
toast.error(`请先查询并选择${detailReferenceSourceLabel}参考图`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (isDetailReplacementModelLoading) {
|
||||||
|
if (modelsError) {
|
||||||
|
toast.error("详情替换模型加载失败,正在重新读取");
|
||||||
|
void refreshModels().catch(() => {});
|
||||||
|
} else {
|
||||||
|
toast.info("正在读取详情替换模型,请稍候");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (isSkuReplacementPreset && skuReplacementReferenceCards.length === 0) {
|
if (isSkuReplacementPreset && skuReplacementReferenceCards.length === 0) {
|
||||||
toast.error(`请先查询并选择${skuReferenceSourceLabel}参考图`);
|
toast.error(`请先查询并选择${skuReferenceSourceLabel}参考图`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const promptStatus = getWorkflowPromptStatus?.();
|
||||||
|
if (promptStatus === "loading" || promptStatus === "error") {
|
||||||
|
toast.error(promptStatus === "error" ? "请先在风格模板区重试加载提示词" : "正在加载所选模板的提示词,请稍候");
|
||||||
|
return;
|
||||||
|
}
|
||||||
const resolvedWorkflowContext = getLatestWorkflowContext?.() ?? workflowContext;
|
const resolvedWorkflowContext = getLatestWorkflowContext?.() ?? workflowContext;
|
||||||
if (
|
if (
|
||||||
isDetailReplacementPreset &&
|
isDetailReplacementPreset &&
|
||||||
@ -6349,6 +6404,10 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
}
|
}
|
||||||
if (replacementModeType === "detail_replacement") {
|
if (replacementModeType === "detail_replacement") {
|
||||||
if (detailBatchSubmittingRef.current) return;
|
if (detailBatchSubmittingRef.current) return;
|
||||||
|
if (replacementExecutionCards.length > MAX_DETAIL_REPLACEMENT_REFERENCES) {
|
||||||
|
toast.error(`本次选中了 ${replacementExecutionCards.length} 张详情参考图,一次最多替换 ${MAX_DETAIL_REPLACEMENT_REFERENCES} 张,请减少本次选择后重试`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!isValidChatUuid(projectId)) {
|
if (!isValidChatUuid(projectId)) {
|
||||||
toast.error("当前风格化项目尚未保存,请先保存项目后再执行详情替换");
|
toast.error("当前风格化项目尚未保存,请先保存项目后再执行详情替换");
|
||||||
return;
|
return;
|
||||||
@ -6369,13 +6428,14 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
const saved = await onPersistProject?.();
|
const saved = await onPersistProject?.();
|
||||||
if (saved === false) throw new Error("项目尚未保存,请重试");
|
if (saved === false) throw new Error("项目尚未保存,请重试");
|
||||||
const requestId = generateUUID();
|
const requestId = generateUUID();
|
||||||
const response = await fetchWithErrorHandlers("/api/agents/detail-replacement/batches", {
|
const response = await fetch("/api/agents/detail-replacement/batches", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
projectId,
|
projectId,
|
||||||
requestId,
|
requestId,
|
||||||
model: resolvedModelId,
|
model: resolvedModelId,
|
||||||
|
expectedModelId: resolvedModelId === "auto" ? undefined : resolvedModelId,
|
||||||
imageSize: imageParams.imageSize,
|
imageSize: imageParams.imageSize,
|
||||||
replacementStrategy: detailReplacementStrategy,
|
replacementStrategy: detailReplacementStrategy,
|
||||||
prompt: detailReplacementStrategy === "product_only"
|
prompt: detailReplacementStrategy === "product_only"
|
||||||
@ -6399,6 +6459,7 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
});
|
});
|
||||||
const payload = await response.json().catch(() => ({}));
|
const payload = await response.json().catch(() => ({}));
|
||||||
if (!response.ok || !payload?.task?.id) {
|
if (!response.ok || !payload?.task?.id) {
|
||||||
|
if (payload?.code === "DETAIL_REPLACEMENT_MODEL_CHANGED") void refreshModels().catch(() => {});
|
||||||
throw new Error(payload?.error || "详情替换任务提交失败");
|
throw new Error(payload?.error || "详情替换任务提交失败");
|
||||||
}
|
}
|
||||||
setPendingGeneration({
|
setPendingGeneration({
|
||||||
@ -6586,6 +6647,8 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
input,
|
input,
|
||||||
isDetailAiGenerationMethod,
|
isDetailAiGenerationMethod,
|
||||||
isDetailReplacementPreset,
|
isDetailReplacementPreset,
|
||||||
|
isDetailReplacementModelLoading,
|
||||||
|
modelsError,
|
||||||
isSelectedBusinessModeUnavailable,
|
isSelectedBusinessModeUnavailable,
|
||||||
isSkuReplacementPreset,
|
isSkuReplacementPreset,
|
||||||
effectiveDefaultModelId,
|
effectiveDefaultModelId,
|
||||||
@ -6602,6 +6665,7 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
skuResultCards,
|
skuResultCards,
|
||||||
userMessageCount,
|
userMessageCount,
|
||||||
getLatestWorkflowContext,
|
getLatestWorkflowContext,
|
||||||
|
getWorkflowPromptStatus,
|
||||||
isSceneGenerationPreset,
|
isSceneGenerationPreset,
|
||||||
workflowContext,
|
workflowContext,
|
||||||
sceneMessageAttachments,
|
sceneMessageAttachments,
|
||||||
@ -6612,6 +6676,7 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
onPersistProject,
|
onPersistProject,
|
||||||
projectId,
|
projectId,
|
||||||
refreshDetailBatch,
|
refreshDetailBatch,
|
||||||
|
refreshModels,
|
||||||
]);
|
]);
|
||||||
const handleStopGeneration = useCallback(() => {
|
const handleStopGeneration = useCallback(() => {
|
||||||
detailGenerationCancelRef.current = true;
|
detailGenerationCancelRef.current = true;
|
||||||
@ -6776,31 +6841,11 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
toast.error("图片地址无效");
|
toast.error("图片地址无效");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filename = buildDownloadFilename(raw);
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(raw);
|
await downloadOriginalImage({ url: raw, name: buildDownloadFilename(raw) });
|
||||||
if (!response.ok) {
|
toast.success("原图已开始下载");
|
||||||
throw new Error(`download failed: ${response.status}`);
|
} catch (error) {
|
||||||
}
|
toast.error(getCanvasImageDownloadErrorMessage(error));
|
||||||
const blob = await response.blob();
|
|
||||||
const blobUrl = window.URL.createObjectURL(blob);
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = blobUrl;
|
|
||||||
link.download = filename;
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
window.URL.revokeObjectURL(blobUrl);
|
|
||||||
} catch {
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = raw;
|
|
||||||
link.target = "_blank";
|
|
||||||
link.rel = "noopener noreferrer";
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
toast.message("已打开原图,可右键另存为");
|
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
const uploadBase64ResultToStorage = useCallback(async (base64Url: string) => {
|
const uploadBase64ResultToStorage = useCallback(async (base64Url: string) => {
|
||||||
@ -7795,6 +7840,11 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
toast.error(detailGenerationValidationIssues[0] || "请先补齐 AI详情生成参数");
|
toast.error(detailGenerationValidationIssues[0] || "请先补齐 AI详情生成参数");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const promptStatus = getWorkflowPromptStatus?.();
|
||||||
|
if (promptStatus === "loading" || promptStatus === "error") {
|
||||||
|
toast.error(promptStatus === "error" ? "请先在风格模板区重试加载提示词" : "正在加载所选模板的提示词,请稍候");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const productAttachments = detailGenerationProductSlotIndexes.map((index) =>
|
const productAttachments = detailGenerationProductSlotIndexes.map((index) =>
|
||||||
detailGenerationSlots[index]
|
detailGenerationSlots[index]
|
||||||
@ -9399,9 +9449,11 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
</Select>
|
</Select>
|
||||||
) : (
|
) : (
|
||||||
<div className="rounded-[24px] border border-white/10 bg-black/30 px-4 py-3 text-sm text-white/70">
|
<div className="rounded-[24px] border border-white/10 bg-black/30 px-4 py-3 text-sm text-white/70">
|
||||||
{isWorkflowModelLocked ? "后台已锁定生图模型:" : "当前生图模式使用默认模型:"}
|
{isDetailReplacementModelLoading
|
||||||
|
? modelsError ? "详情替换模型加载失败,点击批量替换可重试" : "正在读取详情替换模型"
|
||||||
|
: isWorkflowModelLocked ? "后台已锁定生图模型:" : "当前生图模式使用默认模型:"}
|
||||||
<span className="ml-2 font-medium text-white">
|
<span className="ml-2 font-medium text-white">
|
||||||
{isWorkflowModelLocked
|
{isDetailReplacementModelLoading ? null : isWorkflowModelLocked
|
||||||
? lockedWorkflowModelLabel
|
? lockedWorkflowModelLabel
|
||||||
: currentModel?.displayName || "自动选择"}
|
: currentModel?.displayName || "自动选择"}
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
||||||
|
|
||||||
import { useChat } from "@ai-sdk/react";
|
import { useChat } from "@ai-sdk/react";
|
||||||
import { DefaultChatTransport } from "ai";
|
import { DefaultChatTransport } from "ai";
|
||||||
@ -725,30 +726,11 @@ export function AgentGenerationWorkspaceNew({
|
|||||||
toast.error("图片地址无效");
|
toast.error("图片地址无效");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const filename = buildDownloadFilename(raw);
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(raw);
|
await downloadOriginalImage({ url: raw, name: buildDownloadFilename(raw) });
|
||||||
if (!response.ok) {
|
toast.success("原图已开始下载");
|
||||||
throw new Error(`download failed: ${response.status}`);
|
} catch (error) {
|
||||||
}
|
toast.error(getCanvasImageDownloadErrorMessage(error));
|
||||||
const blob = await response.blob();
|
|
||||||
const blobUrl = window.URL.createObjectURL(blob);
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = blobUrl;
|
|
||||||
link.download = filename;
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
window.URL.revokeObjectURL(blobUrl);
|
|
||||||
} catch {
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = raw;
|
|
||||||
link.target = "_blank";
|
|
||||||
link.rel = "noopener noreferrer";
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
toast.message("已打开原图,可右键另存为");
|
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
import { Check, ImageIcon, Loader2, UserRound } from "lucide-react";
|
import { Check, ImageIcon, Loader2, UserRound } from "lucide-react";
|
||||||
|
import { AgentPreviewImage } from "@/components/agents/AgentPreviewImage";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { cn, fetcher } from "@/lib/utils";
|
import { cn, fetcher } from "@/lib/utils";
|
||||||
import { normalizeAgentWorkflowBrandModelConfig } from "@/lib/agents/workflow-brand-model-config";
|
import { normalizeAgentWorkflowBrandModelConfig } from "@/lib/agents/workflow-brand-model-config";
|
||||||
@ -229,12 +230,11 @@ export function AgentWorkflowBrandModelPicker({
|
|||||||
isDark ? "bg-white/[0.04]" : "bg-muted/40"
|
isDark ? "bg-white/[0.04]" : "bg-muted/40"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<img
|
<AgentPreviewImage
|
||||||
src={image.imageUrl}
|
src={image.imageUrl}
|
||||||
alt={image.displayName || `${normalizedBrand} 模特图`}
|
alt={image.displayName || `${normalizedBrand} 模特图`}
|
||||||
className="h-full w-full object-cover"
|
className="h-full w-full object-cover"
|
||||||
loading="lazy"
|
previewWidth={360}
|
||||||
decoding="async"
|
|
||||||
/>
|
/>
|
||||||
<div className="absolute inset-x-0 top-0 flex items-center justify-between px-2 py-2">
|
<div className="absolute inset-x-0 top-0 flex items-center justify-between px-2 py-2">
|
||||||
<span className="rounded-full bg-black/60 px-2 py-1 text-[11px] text-white">
|
<span className="rounded-full bg-black/60 px-2 py-1 text-[11px] text-white">
|
||||||
|
|||||||
@ -112,6 +112,8 @@ const PREVIEW_DIMENSION_HANDLE_RADIUS = 3.2;
|
|||||||
const PREVIEW_DIMENSION_HANDLE_STROKE_WIDTH = 1.2;
|
const PREVIEW_DIMENSION_HANDLE_STROKE_WIDTH = 1.2;
|
||||||
const PREVIEW_DIMENSION_TEXT_HALO_WIDTH = 2.2;
|
const PREVIEW_DIMENSION_TEXT_HALO_WIDTH = 2.2;
|
||||||
const PREVIEW_DIMENSION_TEXT_OUTLINE_WIDTH = 0.85;
|
const PREVIEW_DIMENSION_TEXT_OUTLINE_WIDTH = 0.85;
|
||||||
|
const DIMENSION_FONT_FAMILY =
|
||||||
|
'"PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif';
|
||||||
const SIZE_INFO_LINE_REGEX = /尺寸信息[::]\s*([^\n]+)/i;
|
const SIZE_INFO_LINE_REGEX = /尺寸信息[::]\s*([^\n]+)/i;
|
||||||
const NUMBER_REGEX = /\d+(?:\.\d+)?/g;
|
const NUMBER_REGEX = /\d+(?:\.\d+)?/g;
|
||||||
const HEX_COLOR_REGEX = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
|
const HEX_COLOR_REGEX = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
|
||||||
@ -568,87 +570,54 @@ function drawAnnotationToCanvas(params: {
|
|||||||
context: CanvasRenderingContext2D;
|
context: CanvasRenderingContext2D;
|
||||||
annotation: DimensionAnnotation;
|
annotation: DimensionAnnotation;
|
||||||
imageSize: ImageSize;
|
imageSize: ImageSize;
|
||||||
|
previewRect: PreviewRect;
|
||||||
annotations: DimensionAnnotation[];
|
annotations: DimensionAnnotation[];
|
||||||
autoDimensionValues?: AutoDimensionValues | null;
|
autoDimensionValues?: AutoDimensionValues | null;
|
||||||
}) {
|
}) {
|
||||||
const { context, annotation, imageSize, annotations, autoDimensionValues } =
|
const { context, annotation } = params;
|
||||||
params;
|
|
||||||
const annotationColor = getAnnotationColor(annotation);
|
const annotationColor = getAnnotationColor(annotation);
|
||||||
const label = getDimensionLabel({
|
const geometry = getPreviewAnnotationGeometry(params);
|
||||||
annotation,
|
|
||||||
annotations,
|
|
||||||
autoDimensionValues,
|
|
||||||
});
|
|
||||||
const vector = getAnnotationVector(
|
|
||||||
annotation.x1,
|
|
||||||
annotation.y1,
|
|
||||||
annotation.x2,
|
|
||||||
annotation.y2
|
|
||||||
);
|
|
||||||
const rawAngleRadians = Math.atan2(vector.dy, vector.dx);
|
|
||||||
const angleRadians =
|
|
||||||
rawAngleRadians > Math.PI / 2
|
|
||||||
? rawAngleRadians - Math.PI
|
|
||||||
: rawAngleRadians < -Math.PI / 2
|
|
||||||
? rawAngleRadians + Math.PI
|
|
||||||
: rawAngleRadians;
|
|
||||||
const strokeWidth = clamp(
|
|
||||||
Math.min(imageSize.width, imageSize.height) * 0.001_35,
|
|
||||||
1,
|
|
||||||
2.4
|
|
||||||
);
|
|
||||||
const capLength = strokeWidth * 5.3;
|
|
||||||
const fontSize = clamp(
|
|
||||||
Math.round(Math.min(imageSize.width, imageSize.height) * 0.0176),
|
|
||||||
11,
|
|
||||||
27
|
|
||||||
);
|
|
||||||
const labelOffset = fontSize * 0.95;
|
|
||||||
const centerX = (annotation.x1 + annotation.x2) / 2 + vector.nx * labelOffset;
|
|
||||||
const centerY = (annotation.y1 + annotation.y2) / 2 + vector.ny * labelOffset;
|
|
||||||
const textOutlineColor = getAnnotationTextOutlineColor(annotation);
|
const textOutlineColor = getAnnotationTextOutlineColor(annotation);
|
||||||
const textOutlineWidth = annotation.glow
|
|
||||||
? Math.max(1.5, fontSize * 0.18)
|
|
||||||
: Math.max(0.85, fontSize * 0.08);
|
|
||||||
|
|
||||||
context.save();
|
context.save();
|
||||||
context.strokeStyle = annotationColor;
|
context.strokeStyle = annotationColor;
|
||||||
context.lineWidth = strokeWidth;
|
context.lineWidth = PREVIEW_DIMENSION_STROKE_WIDTH;
|
||||||
context.lineCap = "round";
|
context.lineCap = "round";
|
||||||
context.lineJoin = "round";
|
context.lineJoin = "round";
|
||||||
context.shadowColor = annotation.glow
|
context.shadowColor = annotation.glow
|
||||||
? colorWithAlpha(annotationColor, 0.52)
|
? colorWithAlpha(annotationColor, 0.42)
|
||||||
: "transparent";
|
: "transparent";
|
||||||
context.shadowBlur = annotation.glow ? Math.max(8, fontSize * 0.7) : 0;
|
context.shadowBlur = annotation.glow ? 4 : 0;
|
||||||
|
|
||||||
context.beginPath();
|
context.beginPath();
|
||||||
context.moveTo(annotation.x1, annotation.y1);
|
context.moveTo(geometry.start.x, geometry.start.y);
|
||||||
context.lineTo(annotation.x2, annotation.y2);
|
context.lineTo(geometry.end.x, geometry.end.y);
|
||||||
context.stroke();
|
context.stroke();
|
||||||
|
|
||||||
|
context.lineWidth = PREVIEW_DIMENSION_CAP_STROKE_WIDTH;
|
||||||
drawDimensionCap({
|
drawDimensionCap({
|
||||||
context,
|
context,
|
||||||
point: { x: annotation.x1, y: annotation.y1 },
|
point: geometry.start,
|
||||||
size: capLength,
|
size: PREVIEW_DIMENSION_TICK_LENGTH,
|
||||||
vector,
|
vector: geometry.vector,
|
||||||
});
|
});
|
||||||
drawDimensionCap({
|
drawDimensionCap({
|
||||||
context,
|
context,
|
||||||
point: { x: annotation.x2, y: annotation.y2 },
|
point: geometry.end,
|
||||||
size: capLength,
|
size: PREVIEW_DIMENSION_TICK_LENGTH,
|
||||||
vector,
|
vector: geometry.vector,
|
||||||
});
|
});
|
||||||
|
|
||||||
context.translate(centerX, centerY);
|
context.translate(geometry.labelCenterX, geometry.labelCenterY);
|
||||||
context.rotate(angleRadians);
|
context.rotate((geometry.angleDegrees * Math.PI) / 180);
|
||||||
context.font = `500 ${fontSize}px "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif`;
|
context.font = `500 ${PREVIEW_DIMENSION_FONT_SIZE}px ${DIMENSION_FONT_FAMILY}`;
|
||||||
context.textAlign = "center";
|
context.textAlign = "center";
|
||||||
context.textBaseline = "middle";
|
context.textBaseline = "middle";
|
||||||
context.lineWidth = textOutlineWidth;
|
context.lineWidth = getAnnotationTextOutlineWidth(annotation);
|
||||||
context.strokeStyle = textOutlineColor;
|
context.strokeStyle = textOutlineColor;
|
||||||
context.strokeText(label, 0, 0);
|
context.strokeText(geometry.label, 0, 0.5);
|
||||||
context.fillStyle = annotationColor;
|
context.fillStyle = annotationColor;
|
||||||
context.fillText(label, 0, 0);
|
context.fillText(geometry.label, 0, 0.5);
|
||||||
context.restore();
|
context.restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1067,6 +1036,10 @@ export function AgentPsdTemplateExportPreview({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!imageSize || !previewRect) {
|
||||||
|
throw new Error("请等待预览图片加载完成后再下载");
|
||||||
|
}
|
||||||
|
|
||||||
const { image, dispose } = await loadImageFromCandidates([
|
const { image, dispose } = await loadImageFromCandidates([
|
||||||
buildImageProxyUrl(imageUrl, { delivery: "proxy" }),
|
buildImageProxyUrl(imageUrl, { delivery: "proxy" }),
|
||||||
imageUrl,
|
imageUrl,
|
||||||
@ -1082,18 +1055,25 @@ export function AgentPsdTemplateExportPreview({
|
|||||||
}
|
}
|
||||||
|
|
||||||
context.drawImage(image, 0, 0, canvas.width, canvas.height);
|
context.drawImage(image, 0, 0, canvas.width, canvas.height);
|
||||||
|
// Draw in the same coordinates as the SVG, then map its image area to
|
||||||
|
// the original bitmap. The preview can use a smaller image variant.
|
||||||
|
context.save();
|
||||||
|
context.scale(
|
||||||
|
canvas.width / previewRect.width,
|
||||||
|
canvas.height / previewRect.height
|
||||||
|
);
|
||||||
|
context.translate(-previewRect.left, -previewRect.top);
|
||||||
for (const annotation of annotations) {
|
for (const annotation of annotations) {
|
||||||
drawAnnotationToCanvas({
|
drawAnnotationToCanvas({
|
||||||
annotation,
|
annotation,
|
||||||
annotations,
|
annotations,
|
||||||
autoDimensionValues,
|
autoDimensionValues,
|
||||||
context,
|
context,
|
||||||
imageSize: {
|
imageSize,
|
||||||
width: canvas.width,
|
previewRect,
|
||||||
height: canvas.height,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
context.restore();
|
||||||
|
|
||||||
const blob = await new Promise<Blob>((resolve, reject) => {
|
const blob = await new Promise<Blob>((resolve, reject) => {
|
||||||
canvas.toBlob(
|
canvas.toBlob(
|
||||||
@ -1125,6 +1105,8 @@ export function AgentPsdTemplateExportPreview({
|
|||||||
downloadName,
|
downloadName,
|
||||||
enableDimensionAnnotations,
|
enableDimensionAnnotations,
|
||||||
imageUrl,
|
imageUrl,
|
||||||
|
imageSize,
|
||||||
|
previewRect,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -1305,6 +1287,7 @@ export function AgentPsdTemplateExportPreview({
|
|||||||
<text
|
<text
|
||||||
dominantBaseline="middle"
|
dominantBaseline="middle"
|
||||||
fill={annotationColor}
|
fill={annotationColor}
|
||||||
|
fontFamily={DIMENSION_FONT_FAMILY}
|
||||||
fontSize={PREVIEW_DIMENSION_FONT_SIZE}
|
fontSize={PREVIEW_DIMENSION_FONT_SIZE}
|
||||||
fontWeight={500}
|
fontWeight={500}
|
||||||
paintOrder="stroke"
|
paintOrder="stroke"
|
||||||
|
|||||||
77
components/app-update-notice.tsx
Normal file
77
components/app-update-notice.tsx
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect, useState, useSyncExternalStore } from "react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { AlertDialog, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription, AlertDialogFooter } from "@/components/ui/alert-dialog";
|
||||||
|
import { isPageDownloadActive, preparePageRefresh, subscribeToPageDownloadActivity } from "@/lib/client/page-refresh";
|
||||||
|
|
||||||
|
export function AppUpdateNotice() {
|
||||||
|
const [availableVersion, setAvailableVersion] = useState("");
|
||||||
|
const [refreshing, setRefreshing] = useState(false);
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
const downloading = useSyncExternalStore(subscribeToPageDownloadActivity, isPageDownloadActive, () => false);
|
||||||
|
useEffect(() => {
|
||||||
|
let disposed = false;
|
||||||
|
let controller: AbortController | undefined;
|
||||||
|
let checking = false;
|
||||||
|
const check = async () => {
|
||||||
|
if (disposed || checking || document.visibilityState !== "visible") return;
|
||||||
|
checking = true;
|
||||||
|
controller = new AbortController();
|
||||||
|
const current = controller;
|
||||||
|
const timeout = window.setTimeout(() => current.abort(), 8000);
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/app-version", { cache: "no-store", signal: current.signal });
|
||||||
|
if (!response.ok) return;
|
||||||
|
const data = await response.json();
|
||||||
|
if (!disposed && !current.signal.aborted && typeof data.version === "string" && data.version)
|
||||||
|
setAvailableVersion(data.version !== (process.env.NEXT_PUBLIC_APP_RELEASE || "development") ? data.version : "");
|
||||||
|
} catch {
|
||||||
|
// A failed version read leaves the current workspace available.
|
||||||
|
} finally {
|
||||||
|
window.clearTimeout(timeout);
|
||||||
|
checking = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
void check();
|
||||||
|
const interval = window.setInterval(check, 60_000);
|
||||||
|
window.addEventListener("focus", check);
|
||||||
|
window.addEventListener("online", check);
|
||||||
|
document.addEventListener("visibilitychange", check);
|
||||||
|
return () => {
|
||||||
|
disposed = true;
|
||||||
|
controller?.abort();
|
||||||
|
clearInterval(interval);
|
||||||
|
window.removeEventListener("focus", check);
|
||||||
|
window.removeEventListener("online", check);
|
||||||
|
document.removeEventListener("visibilitychange", check);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const refresh = async () => {
|
||||||
|
if (refreshing) return;
|
||||||
|
setRefreshing(true);
|
||||||
|
setError("");
|
||||||
|
try {
|
||||||
|
await preparePageRefresh();
|
||||||
|
window.location.reload();
|
||||||
|
setRefreshing(false);
|
||||||
|
} catch (cause) {
|
||||||
|
setError(cause instanceof Error ? cause.message : "当前内容同步失败,请保留此页并重试。");
|
||||||
|
setRefreshing(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return <AlertDialog open={Boolean(availableVersion) && !downloading}>
|
||||||
|
<AlertDialogContent className="z-[10001]" overlayClassName="z-[10000]" onEscapeKeyDown={event => event.preventDefault()}>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>平台已更新</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription>需要刷新页面后继续使用。点击后会先同步当前画布并保留输入;正在处理的任务完成后再刷新。</AlertDialogDescription>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
{error ? <p role="alert" className="text-sm text-destructive">{error}</p> : null}
|
||||||
|
<AlertDialogFooter>
|
||||||
|
{error ? <Button variant="outline" disabled={refreshing} onClick={() => { setAvailableVersion(""); setError(""); }}>返回处理</Button> : null}
|
||||||
|
<Button disabled={refreshing} onClick={() => void refresh()}>{refreshing ? "正在同步内容…" : "同步并刷新"}</Button>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>;
|
||||||
|
}
|
||||||
@ -72,13 +72,13 @@ export function AudioGenerationComposer({
|
|||||||
setError("");
|
setError("");
|
||||||
setSending(true);
|
setSending(true);
|
||||||
try {
|
try {
|
||||||
if (!model) throw Error("当前模式没有已启用的官方模型");
|
if (!model) throw Error("当前模式没有已启用的模型");
|
||||||
const params = audioRequestSchema.parse({
|
const params = audioRequestSchema.parse({
|
||||||
...draft,
|
...draft,
|
||||||
text: [draft.text, connectedText].filter(Boolean).join("\n\n"),
|
text: [draft.text, connectedText].filter(Boolean).join("\n\n"),
|
||||||
modelRef: model.id,
|
modelRef: model.id,
|
||||||
voice,
|
voice,
|
||||||
format,
|
format: format || "wav",
|
||||||
...(!contract?.controls ? { rate: 1, pitch: 1, volume: 50 } : {}),
|
...(!contract?.controls ? { rate: 1, pitch: 1, volume: 50 } : {}),
|
||||||
...(!contract?.instructions ? { instructions: "" } : {}),
|
...(!contract?.instructions ? { instructions: "" } : {}),
|
||||||
});
|
});
|
||||||
@ -96,9 +96,9 @@ export function AudioGenerationComposer({
|
|||||||
<section
|
<section
|
||||||
className="fixed bottom-8 left-1/2 z-[100] max-h-[75vh] w-[min(620px,94vw)] -translate-x-1/2 overflow-y-auto rounded-2xl border border-white/10 bg-[#222] p-5 text-white shadow-xl"
|
className="fixed bottom-8 left-1/2 z-[100] max-h-[75vh] w-[min(620px,94vw)] -translate-x-1/2 overflow-y-auto rounded-2xl border border-white/10 bg-[#222] p-5 text-white shadow-xl"
|
||||||
data-canvas-overlay="true"
|
data-canvas-overlay="true"
|
||||||
aria-label="阿里云音频生成"
|
aria-label="语音与音乐"
|
||||||
>
|
>
|
||||||
<h3 className="font-semibold">语音与音乐 · 阿里云官方</h3>
|
<h3 className="font-semibold">语音与音乐</h3>
|
||||||
<fieldset disabled={busy || sending} className="mt-3 space-y-3 disabled:opacity-60">
|
<fieldset disabled={busy || sending} className="mt-3 space-y-3 disabled:opacity-60">
|
||||||
{connectedText && <p className="mb-3 whitespace-pre-wrap rounded bg-white/5 p-2 text-xs" aria-label="已连接文本">已连接文本:{connectedText}</p>}
|
{connectedText && <p className="mb-3 whitespace-pre-wrap rounded bg-white/5 p-2 text-xs" aria-label="已连接文本">已连接文本:{connectedText}</p>}
|
||||||
<div className="grid grid-cols-2 gap-3">
|
<div className="grid grid-cols-2 gap-3">
|
||||||
@ -112,6 +112,9 @@ export function AudioGenerationComposer({
|
|||||||
>
|
>
|
||||||
<option value="tts">文字配音</option>
|
<option value="tts">文字配音</option>
|
||||||
<option value="music">音乐 / 歌曲</option>
|
<option value="music">音乐 / 歌曲</option>
|
||||||
|
<option value="asr">语音转文字</option>
|
||||||
|
<option value="voiceclone">声音克隆配音</option>
|
||||||
|
<option value="voicedesign">声音设计配音</option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
<label>
|
<label>
|
||||||
@ -134,20 +137,40 @@ export function AudioGenerationComposer({
|
|||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
{["asr", "voiceclone"].includes(draft.mode) && (
|
||||||
<label className="block">
|
<label className="block">
|
||||||
{draft.mode === "tts" ? "配音文本(最多 600 字)" : "音乐描述"}
|
{draft.mode === "asr" ? "待识别音频" : "参考声音"}(MP3 / WAV,最多 20 MiB)
|
||||||
|
<input type="file" accept="audio/mpeg,audio/wav,.mp3,.wav" aria-label="上传参考音频" className={input}
|
||||||
|
onChange={async event => {
|
||||||
|
const file = event.target.files?.[0]; event.target.value = ""; if (!file) return;
|
||||||
|
if (file.size > 20 * 1024 * 1024) { setError("参考音频需小于 20 MiB"); return; }
|
||||||
|
setSending(true); setError("");
|
||||||
|
try {
|
||||||
|
const form = new FormData(); form.append("file", file);
|
||||||
|
const response = await fetch("/api/canvas/media/upload", { method: "POST", body: form, signal: AbortSignal.timeout(120000) });
|
||||||
|
const result = await response.json(); const url = result.url || result.data?.url;
|
||||||
|
if (!response.ok || !url) throw Error(result.error || "参考音频上传失败");
|
||||||
|
patch({ referenceAudioUrl: String(url), referenceAudioName: file.name });
|
||||||
|
} catch (error) { setError(error instanceof Error ? error.message : "上传失败"); }
|
||||||
|
finally { setSending(false); }
|
||||||
|
}} />
|
||||||
|
{draft.referenceAudioUrl && <span className="mt-1 block text-sm text-white/80">已上传:{draft.referenceAudioName || "参考音频"}</span>}
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
{draft.mode !== "asr" && <label className="block">
|
||||||
|
{draft.mode !== "music" ? "配音文本(最多 600 字)" : `音乐描述(最多 ${contract?.descriptionLimit || 2000} 字)`}
|
||||||
<textarea
|
<textarea
|
||||||
aria-label="音频内容"
|
aria-label="音频内容"
|
||||||
className={input}
|
className={input}
|
||||||
rows={4}
|
rows={4}
|
||||||
maxLength={draft.mode === "tts" ? 600 : 2000}
|
maxLength={draft.mode !== "music" ? 600 : contract?.descriptionLimit || 2000}
|
||||||
value={draft.text}
|
value={draft.text}
|
||||||
onChange={(e) => patch({ text: e.target.value })}
|
onChange={(e) => patch({ text: e.target.value })}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>}
|
||||||
{draft.mode === "tts" ? (
|
{!["music", "asr"].includes(draft.mode) ? (
|
||||||
<>
|
<>
|
||||||
<div className="grid grid-cols-2 gap-3">
|
{!!contract?.voices.length && <div className="grid grid-cols-2 gap-3">
|
||||||
<label>
|
<label>
|
||||||
音色
|
音色
|
||||||
<select
|
<select
|
||||||
@ -163,7 +186,7 @@ export function AudioGenerationComposer({
|
|||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
<label>
|
{contract?.supportsLanguageControl !== false && <label>
|
||||||
语言
|
语言
|
||||||
<select
|
<select
|
||||||
aria-label="配音语言"
|
aria-label="配音语言"
|
||||||
@ -175,8 +198,8 @@ export function AudioGenerationComposer({
|
|||||||
<option value="English">英文</option>
|
<option value="English">英文</option>
|
||||||
<option value="Auto">自动 / 中英混合</option>
|
<option value="Auto">自动 / 中英混合</option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>}
|
||||||
</div>
|
</div>}
|
||||||
{contract?.instructions ? (
|
{contract?.instructions ? (
|
||||||
<label className="block">
|
<label className="block">
|
||||||
声音风格指令
|
声音风格指令
|
||||||
@ -210,7 +233,7 @@ export function AudioGenerationComposer({
|
|||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
</>
|
</>
|
||||||
) : (
|
) : draft.mode === "music" ? (
|
||||||
<>
|
<>
|
||||||
<label className="flex gap-2">
|
<label className="flex gap-2">
|
||||||
<input
|
<input
|
||||||
@ -233,7 +256,7 @@ export function AudioGenerationComposer({
|
|||||||
onChange={(e) => patch({ lyrics: e.target.value })}
|
onChange={(e) => patch({ lyrics: e.target.value })}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
<label>
|
{contract?.supportsGender !== false && (!contract?.genderRequiresLyrics || draft.lyrics?.trim()) && <label>
|
||||||
演唱声音
|
演唱声音
|
||||||
<select
|
<select
|
||||||
className={input}
|
className={input}
|
||||||
@ -244,18 +267,18 @@ export function AudioGenerationComposer({
|
|||||||
<option value="female">女声</option>
|
<option value="female">女声</option>
|
||||||
<option value="male">男声</option>
|
<option value="male">男声</option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>}
|
||||||
</>
|
</>
|
||||||
) : null}
|
) : null}
|
||||||
{!options.length ? (
|
{!options.length ? (
|
||||||
<p className="text-sm text-amber-200">
|
<p className="text-sm text-amber-200">
|
||||||
Fun-Music 需要阿里云邀测权限,开通并启用模型后即可生成。
|
当前没有可用的音乐模型,请在模型设置中启用。
|
||||||
</p>
|
</p>
|
||||||
) : null}
|
) : null}
|
||||||
</>
|
</>
|
||||||
)}
|
) : null}
|
||||||
<div className="flex items-end gap-3">
|
<div className="flex items-end gap-3">
|
||||||
<label>
|
{draft.mode !== "asr" && <label>
|
||||||
格式
|
格式
|
||||||
<select
|
<select
|
||||||
aria-label="音频格式"
|
aria-label="音频格式"
|
||||||
@ -269,14 +292,14 @@ export function AudioGenerationComposer({
|
|||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
disabled={!model || loading}
|
disabled={!model || loading}
|
||||||
className="rounded-lg bg-white px-4 py-2 text-black disabled:opacity-40"
|
className="rounded-lg bg-white px-4 py-2 text-black disabled:opacity-40"
|
||||||
onClick={() => void run()}
|
onClick={() => void run()}
|
||||||
>
|
>
|
||||||
{busy || sending ? "处理中…" : "生成音频"}
|
{busy || sending ? "处理中…" : draft.mode === "asr" ? "识别文字" : "生成音频"}
|
||||||
</button>
|
</button>
|
||||||
<label className="cursor-pointer rounded-lg border border-white/20 px-3 py-2">
|
<label className="cursor-pointer rounded-lg border border-white/20 px-3 py-2">
|
||||||
上传音频
|
上传音频
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
383
components/canvas/CanvasFeedbackDialog.tsx
Normal file
383
components/canvas/CanvasFeedbackDialog.tsx
Normal file
@ -0,0 +1,383 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Bug } from "lucide-react";
|
||||||
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from "@/components/ui/dialog";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
import { captureFeedbackChat } from "@/lib/canvas/feedback-client";
|
||||||
|
import { useCanvasStore } from "@/store/canvas";
|
||||||
|
import {
|
||||||
|
feedbackCommandSchema,
|
||||||
|
formatFeedbackTime,
|
||||||
|
type FeedbackClient,
|
||||||
|
type FeedbackContext,
|
||||||
|
type FeedbackReceipt,
|
||||||
|
} from "@/lib/canvas/feedback-contract";
|
||||||
|
|
||||||
|
type Submission = Extract<ReturnType<typeof feedbackCommandSchema.parse>, { action: "submit" }>;
|
||||||
|
const terminal = (value: string) => ["sent", "unread", "read", "failed"].includes(value);
|
||||||
|
const fieldClass =
|
||||||
|
"w-full rounded-lg border border-input bg-background px-3 py-2 text-sm text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring";
|
||||||
|
|
||||||
|
export function CanvasFeedbackDialog({
|
||||||
|
projectId,
|
||||||
|
pagePath,
|
||||||
|
floating = false,
|
||||||
|
}: {
|
||||||
|
projectId?: string;
|
||||||
|
pagePath?: string;
|
||||||
|
floating?: boolean;
|
||||||
|
}) {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const [context, setContext] = useState<FeedbackContext | null>(null);
|
||||||
|
const [selection, setSelection] = useState<string | null>(null);
|
||||||
|
const [description, setDescription] = useState("");
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [sending, setSending] = useState(false);
|
||||||
|
const [sealed, setSealed] = useState(false);
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
const [receipt, setReceipt] = useState<FeedbackReceipt | null>(null);
|
||||||
|
const captured = useRef<FeedbackClient | null>(null);
|
||||||
|
const submission = useRef<Submission | null>(null);
|
||||||
|
const errors = useRef<string[]>([]);
|
||||||
|
const submitLock = useRef(false);
|
||||||
|
const endpoint = projectId ? "/api/canvas/" + projectId + "/feedback" : "/api/feedback";
|
||||||
|
const storageKey = projectId ? "canvas-feedback:" + projectId : "page-feedback:" + pagePath;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const rememberError = (event: ErrorEvent) => {
|
||||||
|
errors.current = [
|
||||||
|
...errors.current,
|
||||||
|
String(event.message || "页面脚本错误").slice(0, 2000),
|
||||||
|
].slice(-5);
|
||||||
|
};
|
||||||
|
window.addEventListener("error", rememberError);
|
||||||
|
return () => window.removeEventListener("error", rememberError);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open || receipt) return;
|
||||||
|
const controller = new AbortController();
|
||||||
|
try {
|
||||||
|
const raw = sessionStorage.getItem(storageKey);
|
||||||
|
if (raw && !submission.current) {
|
||||||
|
const command = feedbackCommandSchema.parse(JSON.parse(raw));
|
||||||
|
if (command.action === "submit") {
|
||||||
|
submission.current = command;
|
||||||
|
captured.current = command.client;
|
||||||
|
setSealed(true);
|
||||||
|
setDescription(command.description);
|
||||||
|
setSelection(command.selection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
/* An unavailable local draft does not prevent a new feedback report. */
|
||||||
|
}
|
||||||
|
if (submission.current) {
|
||||||
|
void fetch(`${endpoint}?feedbackId=${submission.current.id}`, {
|
||||||
|
cache: "no-store",
|
||||||
|
signal: controller.signal,
|
||||||
|
})
|
||||||
|
.then(async (response) => {
|
||||||
|
const data = await response.json();
|
||||||
|
if (response.ok) setReceipt(data.feedback);
|
||||||
|
else if (response.status !== 404)
|
||||||
|
setError(data.error || "暂时无法确认反馈状态,请重试提交同一条反馈。");
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
if (!controller.signal.aborted) setError("暂时无法确认反馈状态,可重试提交同一条反馈。");
|
||||||
|
});
|
||||||
|
return () => controller.abort();
|
||||||
|
}
|
||||||
|
const canvas = useCanvasStore.getState();
|
||||||
|
const selectedIds = new Set(canvas.selectedNodeIds);
|
||||||
|
const nodes = projectId ? [...canvas.nodes]
|
||||||
|
.sort((a, b) => Number(selectedIds.has(b.id)) - Number(selectedIds.has(a.id)))
|
||||||
|
.slice(0, 20) : [];
|
||||||
|
const client: FeedbackClient = {
|
||||||
|
...(projectId ? captureFeedbackChat(projectId) : { chatId: null, messages: [] }),
|
||||||
|
pagePath,
|
||||||
|
capturedAt: new Date().toISOString(),
|
||||||
|
pageVersion: process.env.NEXT_PUBLIC_APP_RELEASE || "development",
|
||||||
|
viewport: { width: window.innerWidth, height: window.innerHeight },
|
||||||
|
online: navigator.onLine,
|
||||||
|
errors: [...errors.current],
|
||||||
|
nodes: nodes.map((node) => {
|
||||||
|
const data = node.data as unknown as Record<string, unknown>;
|
||||||
|
const metadata = data.metadata as Record<string, unknown> | undefined;
|
||||||
|
return {
|
||||||
|
id: node.id.slice(0, 180),
|
||||||
|
type: node.type.slice(0, 64),
|
||||||
|
title: String(data.title || "").slice(0, 256),
|
||||||
|
status: String(data.status || "").slice(0, 64),
|
||||||
|
taskId: String(metadata?.taskId || "").slice(0, 180),
|
||||||
|
width: node.size?.width,
|
||||||
|
height: node.size?.height,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
captured.current = client;
|
||||||
|
setLoading(true);
|
||||||
|
setError("");
|
||||||
|
void fetch(endpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ action: "context", client }),
|
||||||
|
signal: controller.signal,
|
||||||
|
})
|
||||||
|
.then(async (response) => {
|
||||||
|
const data = await response.json();
|
||||||
|
if (!response.ok) throw new Error(data.error || "排查资料暂时加载失败,请重新打开反馈。");
|
||||||
|
setContext(data.context);
|
||||||
|
setSelection(data.context.requests[0]?.key || null);
|
||||||
|
})
|
||||||
|
.catch((failure) => {
|
||||||
|
if (!controller.signal.aborted)
|
||||||
|
setError(failure.message || "排查资料暂时加载失败,请重新打开反馈。");
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (!controller.signal.aborted) setLoading(false);
|
||||||
|
});
|
||||||
|
return () => controller.abort();
|
||||||
|
}, [open, endpoint, projectId, pagePath, receipt, storageKey]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open || !receipt || terminal(receipt.status)) return;
|
||||||
|
const controller = new AbortController();
|
||||||
|
let timer: ReturnType<typeof setTimeout>;
|
||||||
|
const poll = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${endpoint}?feedbackId=${receipt.id}`, {
|
||||||
|
cache: "no-store",
|
||||||
|
signal: controller.signal,
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
if (response.ok && !controller.signal.aborted) {
|
||||||
|
setReceipt(data.feedback);
|
||||||
|
setError("");
|
||||||
|
} else if (!controller.signal.aborted)
|
||||||
|
setError(data.error || "暂时无法刷新通知状态,已保存的反馈会继续处理。");
|
||||||
|
} catch {
|
||||||
|
if (!controller.signal.aborted)
|
||||||
|
setError("网络暂时不可用,已保存的反馈会继续处理,请勿重复提交。");
|
||||||
|
}
|
||||||
|
if (!controller.signal.aborted) timer = setTimeout(poll, 4000);
|
||||||
|
};
|
||||||
|
timer = setTimeout(poll, 1500);
|
||||||
|
return () => {
|
||||||
|
controller.abort();
|
||||||
|
clearTimeout(timer);
|
||||||
|
};
|
||||||
|
}, [open, receipt?.id, receipt?.status, endpoint]);
|
||||||
|
|
||||||
|
const send = async () => {
|
||||||
|
if (submitLock.current || receipt) return;
|
||||||
|
if (!submission.current) {
|
||||||
|
if (!captured.current || !context || (!projectId && !description.trim())) return;
|
||||||
|
submission.current = {
|
||||||
|
action: "submit",
|
||||||
|
id: crypto.randomUUID(),
|
||||||
|
description,
|
||||||
|
selection,
|
||||||
|
client: captured.current,
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
sessionStorage.setItem(storageKey, JSON.stringify(submission.current));
|
||||||
|
} catch {
|
||||||
|
/* In-memory retries still retain this report's id. */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
submitLock.current = true;
|
||||||
|
setSending(true);
|
||||||
|
setSealed(true);
|
||||||
|
setError("");
|
||||||
|
try {
|
||||||
|
const response = await fetch(endpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(submission.current),
|
||||||
|
signal: AbortSignal.timeout(20000),
|
||||||
|
});
|
||||||
|
const data = await response.json();
|
||||||
|
if (!response.ok) {
|
||||||
|
if (response.status === 400 || response.status === 409 || response.status === 413) {
|
||||||
|
submission.current = null;
|
||||||
|
sessionStorage.removeItem(storageKey);
|
||||||
|
setSealed(false);
|
||||||
|
}
|
||||||
|
throw new Error(data.error || "暂时无法确认反馈是否保存,请重试提交同一条反馈。");
|
||||||
|
}
|
||||||
|
setReceipt(data.feedback);
|
||||||
|
} catch (failure) {
|
||||||
|
setError(
|
||||||
|
failure instanceof Error && failure.name !== "TimeoutError"
|
||||||
|
? failure.message
|
||||||
|
: "提交连接超时。请重试提交同一条反馈,系统会核对原编号,避免重复通知。",
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
submitLock.current = false;
|
||||||
|
setSending(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const startAnother = () => {
|
||||||
|
submission.current = null;
|
||||||
|
captured.current = null;
|
||||||
|
sessionStorage.removeItem(storageKey);
|
||||||
|
setContext(null);
|
||||||
|
setDescription("");
|
||||||
|
setSelection(null);
|
||||||
|
setError("");
|
||||||
|
setSealed(false);
|
||||||
|
setReceipt(null);
|
||||||
|
};
|
||||||
|
const selected = context?.requests.find((item) => item.key === selection);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
|
<DialogTrigger asChild>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={cn(
|
||||||
|
"inline-flex shrink-0 items-center gap-2 rounded-lg px-3 py-2 text-sm text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring",
|
||||||
|
floating ? "fixed bottom-20 right-4 z-40 min-h-10 border bg-background shadow-sm" : "ml-2",
|
||||||
|
)}
|
||||||
|
aria-label="问题反馈"
|
||||||
|
>
|
||||||
|
<Bug className="size-4 text-red-600 dark:text-red-400" aria-hidden="true" />
|
||||||
|
<span className="hidden sm:inline">问题反馈</span>
|
||||||
|
</button>
|
||||||
|
</DialogTrigger>
|
||||||
|
<DialogContent
|
||||||
|
className="max-h-[85dvh] overflow-y-auto sm:max-w-xl"
|
||||||
|
aria-busy={loading || sending}
|
||||||
|
>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>问题反馈</DialogTitle>
|
||||||
|
<DialogDescription className="text-pretty">
|
||||||
|
{projectId
|
||||||
|
? "自动附上当前画布、用户和相关请求资料。你可以补充问题现象,发送给魔礼红。"
|
||||||
|
: "自动附上当前页面和浏览器信息。请说明遇到的问题,发送给魔礼红。"}
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
{receipt ? (
|
||||||
|
<div className="space-y-3 py-2" role="status" aria-live="polite">
|
||||||
|
<p className="text-sm">{receipt.message}</p>
|
||||||
|
<p className="break-all text-sm text-muted-foreground">反馈编号:{receipt.id}</p>
|
||||||
|
<a
|
||||||
|
href={`/feedback/${receipt.id}`}
|
||||||
|
target="_blank"
|
||||||
|
rel="noreferrer"
|
||||||
|
className="inline-block text-sm underline underline-offset-4"
|
||||||
|
>
|
||||||
|
查看已保存的完整反馈
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-4">
|
||||||
|
{loading ? (
|
||||||
|
<p role="status" className="py-4 text-sm text-muted-foreground">
|
||||||
|
正在整理排查资料…
|
||||||
|
</p>
|
||||||
|
) : context ? (
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{context.reporter.name} · {context.project.title}
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
{projectId && (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label htmlFor="canvas-feedback-request" className="text-sm font-medium">
|
||||||
|
关联请求
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
id="canvas-feedback-request"
|
||||||
|
className={fieldClass}
|
||||||
|
value={selection || ""}
|
||||||
|
disabled={sealed || loading}
|
||||||
|
onChange={(event) => setSelection(event.target.value || null)}
|
||||||
|
>
|
||||||
|
<option value="">页面或画布问题,不关联具体请求</option>
|
||||||
|
{context?.requests.map((item) => (
|
||||||
|
<option key={item.key} value={item.key}>
|
||||||
|
{item.createdAt ? formatFeedbackTime(item.createdAt) : "时间未记录"} ·{" "}
|
||||||
|
{item.prompt.slice(0, 45)}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
{selected && (
|
||||||
|
<div className="space-y-2 rounded-lg bg-muted p-3 text-sm">
|
||||||
|
<p>
|
||||||
|
请求号:
|
||||||
|
<span className="break-all">
|
||||||
|
{selected.requestId || "尚未获取,将附上消息编号"}
|
||||||
|
</span>
|
||||||
|
</p>
|
||||||
|
<p className="max-h-28 overflow-y-auto whitespace-pre-wrap break-words text-muted-foreground">
|
||||||
|
{selected.prompt}
|
||||||
|
</p>
|
||||||
|
{selected.error && (
|
||||||
|
<p className="whitespace-pre-wrap break-words">{selected.error}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label htmlFor="canvas-feedback-description" className="text-sm font-medium">
|
||||||
|
{projectId ? "补充说明" : "问题说明"}{" "}
|
||||||
|
{projectId && <span className="font-normal text-muted-foreground">(选填)</span>}
|
||||||
|
</label>
|
||||||
|
<textarea
|
||||||
|
id="canvas-feedback-description"
|
||||||
|
rows={4}
|
||||||
|
maxLength={2000}
|
||||||
|
className={`${fieldClass} resize-y`}
|
||||||
|
value={description}
|
||||||
|
disabled={sealed}
|
||||||
|
onChange={(event) => setDescription(event.target.value)}
|
||||||
|
placeholder="例如:点击预览后一直加载,等待约 30 秒仍无法查看。"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{projectId && (
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
将附上相关请求内容、任务状态、页面版本及已记录的错误;本次反馈不会重新执行任务。
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{error && (
|
||||||
|
<p role="alert" className="whitespace-pre-wrap text-sm text-destructive">
|
||||||
|
{error}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
<DialogFooter className="gap-2 sm:space-x-0">
|
||||||
|
<Button type="button" variant="outline" onClick={() => setOpen(false)}>
|
||||||
|
{receipt ? "关闭" : "取消"}
|
||||||
|
</Button>
|
||||||
|
{receipt ? (
|
||||||
|
<Button type="button" variant="outline" onClick={startAnother}>
|
||||||
|
反馈另一个问题
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
onClick={() => void send()}
|
||||||
|
disabled={sending || loading || (!submission.current && (!context || (!projectId && !description.trim())))}
|
||||||
|
>
|
||||||
|
{sending ? "正在发送…" : sealed ? "重试提交同一反馈" : "发送给魔礼红"}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -19,6 +19,7 @@ const PROJECT_LIST_PAGE_SIZE = 24;
|
|||||||
interface CanvasFileListPanelProps {
|
interface CanvasFileListPanelProps {
|
||||||
currentId: string;
|
currentId: string;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
|
onNavigate?: (url: string) => void;
|
||||||
workspaceType?: CanvasWorkspaceType;
|
workspaceType?: CanvasWorkspaceType;
|
||||||
workspaceLabel?: string;
|
workspaceLabel?: string;
|
||||||
createUrl?: string;
|
createUrl?: string;
|
||||||
@ -28,6 +29,7 @@ interface CanvasFileListPanelProps {
|
|||||||
export function CanvasFileListPanel({
|
export function CanvasFileListPanel({
|
||||||
currentId,
|
currentId,
|
||||||
onClose,
|
onClose,
|
||||||
|
onNavigate,
|
||||||
workspaceType = "canvas",
|
workspaceType = "canvas",
|
||||||
workspaceLabel = "画布",
|
workspaceLabel = "画布",
|
||||||
createUrl = "/canvas?new=1",
|
createUrl = "/canvas?new=1",
|
||||||
@ -88,6 +90,10 @@ export function CanvasFileListPanel({
|
|||||||
|
|
||||||
// 创建新画布
|
// 创建新画布
|
||||||
const handleCreateNew = async () => {
|
const handleCreateNew = async () => {
|
||||||
|
if (onNavigate) {
|
||||||
|
onNavigate(createUrl);
|
||||||
|
return;
|
||||||
|
}
|
||||||
router.push(createUrl);
|
router.push(createUrl);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -100,6 +106,10 @@ export function CanvasFileListPanel({
|
|||||||
}
|
}
|
||||||
const targetUrl = buildProjectUrl(id);
|
const targetUrl = buildProjectUrl(id);
|
||||||
onClose(); // 关闭文件列表面板
|
onClose(); // 关闭文件列表面板
|
||||||
|
if (onNavigate) {
|
||||||
|
onNavigate(targetUrl);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// 强制整页跳转,避免仅更新 URL 不刷新
|
// 强制整页跳转,避免仅更新 URL 不刷新
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.assign(targetUrl);
|
window.location.assign(targetUrl);
|
||||||
|
|||||||
@ -28,6 +28,7 @@ export function CanvasImageDownloadProgressDialog({
|
|||||||
: null;
|
: null;
|
||||||
const loadedLabel = formatBytes(progress.loadedBytes);
|
const loadedLabel = formatBytes(progress.loadedBytes);
|
||||||
const totalLabel = formatBytes(progress.totalBytes);
|
const totalLabel = formatBytes(progress.totalBytes);
|
||||||
|
const converting = progress.phase === "converting";
|
||||||
|
|
||||||
return createPortal(
|
return createPortal(
|
||||||
<div
|
<div
|
||||||
@ -41,12 +42,12 @@ export function CanvasImageDownloadProgressDialog({
|
|||||||
{progress.phase === "saving" ? (
|
{progress.phase === "saving" ? (
|
||||||
<Download className="h-4 w-4" />
|
<Download className="h-4 w-4" />
|
||||||
) : (
|
) : (
|
||||||
<Loader2 className="h-4 w-4 animate-spin" />
|
<Loader2 className="h-4 w-4 motion-safe:animate-spin" />
|
||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
<div>
|
<div>
|
||||||
<div className="text-[14px] font-semibold">下载原图</div>
|
<div className="text-[14px] font-semibold">下载原图</div>
|
||||||
<div className="mt-0.5 text-[12px] text-white/58">{progress.message}</div>
|
<div className="mt-0.5 text-[12px] text-white/58" role="status">{progress.message}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{onCancel ? (
|
{onCancel ? (
|
||||||
@ -63,14 +64,19 @@ export function CanvasImageDownloadProgressDialog({
|
|||||||
|
|
||||||
<div className="mt-4">
|
<div className="mt-4">
|
||||||
<div className="mb-2 flex items-center justify-between text-[12px] text-white/55">
|
<div className="mb-2 flex items-center justify-between text-[12px] text-white/55">
|
||||||
<span>{loadedLabel && totalLabel ? `${loadedLabel} / ${totalLabel}` : "正在处理文件"}</span>
|
<span>{loadedLabel && totalLabel ? `${loadedLabel} / ${totalLabel}` : converting ? "格式转换中" : "正在处理文件"}</span>
|
||||||
<span>{percent !== null ? `${percent}%` : "..."}</span>
|
<span>{percent !== null ? `${percent}%` : "..."}</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="relative h-2 overflow-hidden rounded-full bg-white/10">
|
<div
|
||||||
|
className="relative h-2 overflow-hidden rounded-full bg-white/10"
|
||||||
|
role="progressbar"
|
||||||
|
aria-label={converting ? "图片格式转换" : "图片下载"}
|
||||||
|
aria-valuenow={percent ?? undefined}
|
||||||
|
>
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"absolute inset-y-0 left-0 rounded-full bg-gradient-to-r from-cyan-300 via-sky-400 to-emerald-300",
|
"absolute inset-y-0 left-0 rounded-full bg-gradient-to-r from-cyan-300 via-sky-400 to-emerald-300",
|
||||||
percent === null && "w-1/3 animate-[download-indeterminate_1.15s_ease-in-out_infinite]"
|
percent === null && "w-1/3 motion-safe:animate-[download-indeterminate_1.15s_ease-in-out_infinite]"
|
||||||
)}
|
)}
|
||||||
style={percent !== null ? { width: `${percent}%` } : undefined}
|
style={percent !== null ? { width: `${percent}%` } : undefined}
|
||||||
/>
|
/>
|
||||||
|
|||||||
26
components/canvas/CanvasRemoteOperationContext.tsx
Normal file
26
components/canvas/CanvasRemoteOperationContext.tsx
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { createContext, useContext } from "react";
|
||||||
|
import type { CanvasState } from "@/types/canvas";
|
||||||
|
|
||||||
|
export type CanvasRemoteOperationResult = {
|
||||||
|
projectId: string;
|
||||||
|
canvasData: CanvasState;
|
||||||
|
updatedAt: string;
|
||||||
|
};
|
||||||
|
export type CanvasRemoteOperation = (() => Promise<void>) | (() => Promise<CanvasRemoteOperationResult | undefined>);
|
||||||
|
|
||||||
|
export const CanvasRemoteOperationContext = createContext<
|
||||||
|
{
|
||||||
|
run: (operation: CanvasRemoteOperation) => Promise<void>;
|
||||||
|
applySnapshot: (result: CanvasRemoteOperationResult) => boolean;
|
||||||
|
} | null
|
||||||
|
>(null);
|
||||||
|
|
||||||
|
export function useCanvasRemoteOperation() {
|
||||||
|
return useContext(CanvasRemoteOperationContext)?.run || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useCanvasRemoteSnapshot() {
|
||||||
|
return useContext(CanvasRemoteOperationContext)?.applySnapshot || null;
|
||||||
|
}
|
||||||
@ -1,16 +1,14 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { ReferenceIcon } from "@/components/lovart/reference-icon";
|
import { ReferenceIcon } from "@/components/lovart/reference-icon";
|
||||||
|
import { CanvasFeedbackDialog } from "./CanvasFeedbackDialog";
|
||||||
import { Loader2, Save } from "lucide-react";
|
|
||||||
|
|
||||||
type CanvasSurfaceHeaderProps = {
|
type CanvasSurfaceHeaderProps = {
|
||||||
isSaving: boolean;
|
|
||||||
onAddImage: () => void;
|
onAddImage: () => void;
|
||||||
onHome: () => void;
|
onHome: () => void;
|
||||||
onOpenFiles: () => void;
|
onOpenFiles: () => void;
|
||||||
onSave: () => void;
|
|
||||||
projectTitle: string;
|
projectTitle: string;
|
||||||
|
projectId: string;
|
||||||
rightInset: number;
|
rightInset: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -18,12 +16,11 @@ const iconButtonClass =
|
|||||||
"inline-flex h-[28px] w-[28px] items-center justify-center rounded-[8px] p-0 text-[#858585] transition-colors hover:bg-black/5 focus-visible:outline-2 focus-visible:outline-offset-2 dark:hover:bg-white/10";
|
"inline-flex h-[28px] w-[28px] items-center justify-center rounded-[8px] p-0 text-[#858585] transition-colors hover:bg-black/5 focus-visible:outline-2 focus-visible:outline-offset-2 dark:hover:bg-white/10";
|
||||||
|
|
||||||
export function CanvasSurfaceHeader({
|
export function CanvasSurfaceHeader({
|
||||||
isSaving,
|
|
||||||
onAddImage,
|
onAddImage,
|
||||||
onHome,
|
onHome,
|
||||||
onOpenFiles,
|
onOpenFiles,
|
||||||
onSave,
|
|
||||||
projectTitle,
|
projectTitle,
|
||||||
|
projectId,
|
||||||
rightInset,
|
rightInset,
|
||||||
}: CanvasSurfaceHeaderProps) {
|
}: CanvasSurfaceHeaderProps) {
|
||||||
return (
|
return (
|
||||||
@ -77,19 +74,7 @@ export function CanvasSurfaceHeader({
|
|||||||
<ReferenceIcon name="plus" size={16} />
|
<ReferenceIcon name="plus" size={16} />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<button
|
<CanvasFeedbackDialog projectId={projectId} />
|
||||||
className="inline-flex h-7 shrink-0 items-center gap-1.5 rounded-[8px] border border-[#e5e5e5] px-3 text-[12px] text-[#333333] transition-colors hover:bg-black/5 disabled:opacity-50 dark:border-white/10 dark:text-white"
|
|
||||||
disabled={isSaving}
|
|
||||||
onClick={onSave}
|
|
||||||
type="button"
|
|
||||||
>
|
|
||||||
{isSaving ? (
|
|
||||||
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
|
||||||
) : (
|
|
||||||
<Save className="h-3.5 w-3.5" />
|
|
||||||
)}
|
|
||||||
保存
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,6 +14,7 @@ import { WorkflowNode } from "./nodes/WorkflowNode";
|
|||||||
import { CanvasToolbarIcon } from "./CanvasToolbarIcon";
|
import { CanvasToolbarIcon } from "./CanvasToolbarIcon";
|
||||||
import { CanvasBlankContextMenu } from "./CanvasBlankContextMenu";
|
import { CanvasBlankContextMenu } from "./CanvasBlankContextMenu";
|
||||||
import { getCanvasFitViewport } from "@/lib/canvas/viewport-fit";
|
import { getCanvasFitViewport } from "@/lib/canvas/viewport-fit";
|
||||||
|
import { downloadCanvasSelectionZip } from "@/lib/client/canvas-zip-download";
|
||||||
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
|
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
@ -1914,10 +1915,10 @@ export function CanvasWorkspace({
|
|||||||
const rawImageUrl = String(nodeData?.src || "").trim();
|
const rawImageUrl = String(nodeData?.src || "").trim();
|
||||||
if (!rawImageUrl) return null;
|
if (!rawImageUrl) return null;
|
||||||
let imageUrl = inferOriginalImageUrl(rawImageUrl) || rawImageUrl;
|
let imageUrl = inferOriginalImageUrl(rawImageUrl) || rawImageUrl;
|
||||||
if (browserOrigin && rawImageUrl.startsWith("/")) {
|
if (browserOrigin && rawImageUrl.startsWith("//")) {
|
||||||
imageUrl = `${browserOrigin}${rawImageUrl}`;
|
|
||||||
} else if (browserOrigin && rawImageUrl.startsWith("//")) {
|
|
||||||
imageUrl = `${window.location.protocol}${rawImageUrl}`;
|
imageUrl = `${window.location.protocol}${rawImageUrl}`;
|
||||||
|
} else if (browserOrigin && rawImageUrl.startsWith("/")) {
|
||||||
|
imageUrl = `${browserOrigin}${rawImageUrl}`;
|
||||||
}
|
}
|
||||||
const name =
|
const name =
|
||||||
String(nodeData?.title || "").trim() ||
|
String(nodeData?.title || "").trim() ||
|
||||||
@ -1933,32 +1934,18 @@ export function CanvasWorkspace({
|
|||||||
}
|
}
|
||||||
|
|
||||||
setIsDownloadSubmitting(true);
|
setIsDownloadSubmitting(true);
|
||||||
|
const toastId = toast.loading("正在准备所选图片...");
|
||||||
try {
|
try {
|
||||||
const form = document.createElement("form");
|
const result = await downloadCanvasSelectionZip({ folderName, files,
|
||||||
form.method = "POST";
|
selectionKey: `${useCanvasStore.getState().projectId}:${selectedImageNodes.map(node => node.id).sort().join(",")}`,
|
||||||
form.action = "/api/canvas/download-zip";
|
onProgress: task => toast.loading(`${task.stage}(${task.progress}%)`, { id: toastId }),
|
||||||
form.style.display = "none";
|
|
||||||
|
|
||||||
const payloadInput = document.createElement("input");
|
|
||||||
payloadInput.type = "hidden";
|
|
||||||
payloadInput.name = "payload";
|
|
||||||
payloadInput.value = JSON.stringify({
|
|
||||||
folderName,
|
|
||||||
files,
|
|
||||||
});
|
});
|
||||||
form.appendChild(payloadInput);
|
if (result.failedCount) toast.warning(`已开始下载 ${result.successCount} 张图片,${result.failedCount} 张未能读取;ZIP 内附失败清单`, { id: toastId, duration: 10000 });
|
||||||
|
else toast.success(`ZIP 已准备完成,正在下载 ${result.successCount} 张图片`, { id: toastId });
|
||||||
document.body.appendChild(form);
|
} catch (error) {
|
||||||
form.submit();
|
toast.error(error instanceof Error ? error.message : "打包下载失败,请稍后重试", { id: toastId });
|
||||||
form.remove();
|
|
||||||
|
|
||||||
toast.success("已开始打包下载,请留意浏览器下载栏");
|
|
||||||
} catch (_error) {
|
|
||||||
toast.error("下载失败,请稍后重试");
|
|
||||||
} finally {
|
} finally {
|
||||||
window.setTimeout(() => {
|
|
||||||
setIsDownloadSubmitting(false);
|
setIsDownloadSubmitting(false);
|
||||||
}, 800);
|
|
||||||
}
|
}
|
||||||
}, [isDownloadSubmitting, selectedImageNodes]);
|
}, [isDownloadSubmitting, selectedImageNodes]);
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
|
||||||
import { useCanvasGroupRuns } from "@/hooks/use-canvas-group-runs";
|
import { useCanvasGroupRuns } from "@/hooks/use-canvas-group-runs";
|
||||||
import { hasRecoverableCanvasTask } from "@/lib/video-canvas/task-recovery";
|
import { hasRecoverableCanvasTask } from "@/lib/video-canvas/task-recovery";
|
||||||
|
import { restoreTextResourceContent } from "@/lib/video-canvas/text-resource";
|
||||||
import { GroupStoryboardPreview } from "./GroupStoryboardPreview";
|
import { GroupStoryboardPreview } from "./GroupStoryboardPreview";
|
||||||
import { CanvasGroupToolbar } from "./CanvasGroupToolbar";
|
import { CanvasGroupToolbar } from "./CanvasGroupToolbar";
|
||||||
import { isCanvasGroup, groupMembers, makeCanvasGroup, ungroupCanvasNodes, moveCanvasSelection, layoutCanvasGroup, migrateLegacyGroups, exportCanvasGroup, instantiateCanvasGroup, groupBounds, assignGroupMembers } from "@/lib/video-canvas/groups";
|
import { isCanvasGroup, groupMembers, makeCanvasGroup, ungroupCanvasNodes, moveCanvasSelection, layoutCanvasGroup, migrateLegacyGroups, exportCanvasGroup, instantiateCanvasGroup, groupBounds, assignGroupMembers } from "@/lib/video-canvas/groups";
|
||||||
@ -104,6 +106,11 @@ import {
|
|||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
import { ZCODE_VIDEO_CANVAS_AUTHORIZED } from "@/lib/ai/canvas-agent-runtime/authorization";
|
||||||
|
import { CanvasAgentUiListener } from "./chat-panel/canvas-agent-ui-listener";
|
||||||
|
import type { CanvasAgentUiCommand } from "@/lib/ai/canvas-agent-runtime/ui-command";
|
||||||
|
import { VideoCanvasAgentPanel } from "./chat-panel/video-canvas-agent-panel";
|
||||||
|
import { mergeVideoAgentResults } from "@/lib/ai/canvas-agent-runtime/video-protocol";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
import {
|
import {
|
||||||
getGenerationCreditCostFromImageGenConfig,
|
getGenerationCreditCostFromImageGenConfig,
|
||||||
@ -816,28 +823,28 @@ const STARTER_CARDS = [
|
|||||||
title: "故事脚本生成",
|
title: "故事脚本生成",
|
||||||
subtitle: "脚本到分镜",
|
subtitle: "脚本到分镜",
|
||||||
coverUrl:
|
coverUrl:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-story-script-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-story-script-cover.png",
|
||||||
steps: ["故事脚本", "多模态文本模型Pro", "可灵 3.0", "预览"],
|
steps: ["故事脚本", "多模态文本模型Pro", "可灵 3.0", "预览"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "角色三视图",
|
title: "角色三视图",
|
||||||
subtitle: "角色资产沉淀",
|
subtitle: "角色资产沉淀",
|
||||||
coverUrl:
|
coverUrl:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-character-three-view-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-character-three-view-cover.png",
|
||||||
steps: ["角色描述", "角色三视图", "全能图片模型V2", "保存"],
|
steps: ["角色描述", "角色三视图", "全能图片模型V2", "保存"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "全能参考生视频",
|
title: "全能参考生视频",
|
||||||
subtitle: "参考图到成片",
|
subtitle: "参考图到成片",
|
||||||
coverUrl:
|
coverUrl:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-first-frame-video-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-first-frame-video-cover.png",
|
||||||
steps: ["上传图片", "首帧图", "可灵 3.0", "预览"],
|
steps: ["上传图片", "首帧图", "可灵 3.0", "预览"],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "音频生视频",
|
title: "音频生视频",
|
||||||
subtitle: "音频驱动画面",
|
subtitle: "音频驱动画面",
|
||||||
coverUrl:
|
coverUrl:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-audio-to-video-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-audio-to-video-cover.png",
|
||||||
icon: "music",
|
icon: "music",
|
||||||
steps: ["上传音频", "音频主视频", "Wan 2.6", "预览"],
|
steps: ["上传音频", "音频主视频", "Wan 2.6", "预览"],
|
||||||
},
|
},
|
||||||
@ -860,7 +867,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
badge: "预设",
|
badge: "预设",
|
||||||
category: "周星驰经典名场面",
|
category: "周星驰经典名场面",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-first-frame-video-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-first-frame-video-cover.png",
|
||||||
description: "左弧滑行,镜头从人物左侧弧线推进,带轻微喜剧节奏和夸张表情停顿。",
|
description: "左弧滑行,镜头从人物左侧弧线推进,带轻微喜剧节奏和夸张表情停顿。",
|
||||||
duration: "5s",
|
duration: "5s",
|
||||||
id: "left-arc-slide",
|
id: "left-arc-slide",
|
||||||
@ -875,7 +882,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
badge: "商用",
|
badge: "商用",
|
||||||
category: "电商展示",
|
category: "电商展示",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-character-three-view-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-character-three-view-cover.png",
|
||||||
description: "电商手机弹出效果,产品从画面中心弹出并旋转定格,适合主图视频开场。",
|
description: "电商手机弹出效果,产品从画面中心弹出并旋转定格,适合主图视频开场。",
|
||||||
duration: "5s",
|
duration: "5s",
|
||||||
id: "phone-pop-product",
|
id: "phone-pop-product",
|
||||||
@ -890,7 +897,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
badge: "商用",
|
badge: "商用",
|
||||||
category: "电商展示",
|
category: "电商展示",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-story-script-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-story-script-cover.png",
|
||||||
description: "咖啡杯出场,杯身从桌面滑入,蒸汽和柔光形成温暖的品牌氛围。",
|
description: "咖啡杯出场,杯身从桌面滑入,蒸汽和柔光形成温暖的品牌氛围。",
|
||||||
duration: "5s",
|
duration: "5s",
|
||||||
id: "coffee-cup-entry",
|
id: "coffee-cup-entry",
|
||||||
@ -905,7 +912,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
badge: "热门",
|
badge: "热门",
|
||||||
category: "镜头运动",
|
category: "镜头运动",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-audio-to-video-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-audio-to-video-cover.png",
|
||||||
description: "360旋转展示,围绕主体做一圈稳定环绕,适合产品、人物和空间展示。",
|
description: "360旋转展示,围绕主体做一圈稳定环绕,适合产品、人物和空间展示。",
|
||||||
duration: "6s",
|
duration: "6s",
|
||||||
id: "orbit-360-showcase",
|
id: "orbit-360-showcase",
|
||||||
@ -919,7 +926,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
author: "工业镜头",
|
author: "工业镜头",
|
||||||
category: "镜头运动",
|
category: "镜头运动",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/character-image.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/character-image.png",
|
||||||
description: "机械臂视角,以第一视角穿过机械臂与产品线,增强科技感和空间纵深。",
|
description: "机械臂视角,以第一视角穿过机械臂与产品线,增强科技感和空间纵深。",
|
||||||
duration: "5s",
|
duration: "5s",
|
||||||
id: "robot-arm-pov",
|
id: "robot-arm-pov",
|
||||||
@ -934,7 +941,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
badge: "NEW",
|
badge: "NEW",
|
||||||
category: "人物动画",
|
category: "人物动画",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/character-three-view.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/character-three-view.png",
|
||||||
description: "Live 2D,让单张角色图产生眨眼、微笑和轻微身体摆动,适合口播开场。",
|
description: "Live 2D,让单张角色图产生眨眼、微笑和轻微身体摆动,适合口播开场。",
|
||||||
duration: "4s",
|
duration: "4s",
|
||||||
id: "live-2d-character",
|
id: "live-2d-character",
|
||||||
@ -948,7 +955,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
author: "电影运镜",
|
author: "电影运镜",
|
||||||
category: "镜头运动",
|
category: "镜头运动",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-first-frame-video-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-first-frame-video-cover.png",
|
||||||
description: "瞳孔拉近,镜头快速推向眼部并在反光里展示下一场景,适合悬疑转场。",
|
description: "瞳孔拉近,镜头快速推向眼部并在反光里展示下一场景,适合悬疑转场。",
|
||||||
duration: "5s",
|
duration: "5s",
|
||||||
id: "pupil-zoom",
|
id: "pupil-zoom",
|
||||||
@ -963,7 +970,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
badge: "热门",
|
badge: "热门",
|
||||||
category: "人物动画",
|
category: "人物动画",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-story-script-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-story-script-cover.png",
|
||||||
description: "飞鸟解体,主体由细碎羽片散开再重组,适合视觉冲击强的短视频片头。",
|
description: "飞鸟解体,主体由细碎羽片散开再重组,适合视觉冲击强的短视频片头。",
|
||||||
duration: "6s",
|
duration: "6s",
|
||||||
id: "bird-dissolve",
|
id: "bird-dissolve",
|
||||||
@ -978,7 +985,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
badge: "商用",
|
badge: "商用",
|
||||||
category: "电商展示",
|
category: "电商展示",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-character-three-view-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-character-three-view-cover.png",
|
||||||
description: "商品震撼登场,产品带冲击波与局部高光进入画面,适合活动主推款。",
|
description: "商品震撼登场,产品带冲击波与局部高光进入画面,适合活动主推款。",
|
||||||
duration: "5s",
|
duration: "5s",
|
||||||
id: "product-hero-entry",
|
id: "product-hero-entry",
|
||||||
@ -992,7 +999,7 @@ const TOOLBOX_TEMPLATE_PRESETS: ToolboxTemplatePreset[] = [
|
|||||||
author: "旅拍模板",
|
author: "旅拍模板",
|
||||||
category: "镜头运动",
|
category: "镜头运动",
|
||||||
cover:
|
cover:
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-audio-to-video-cover.png",
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-audio-to-video-cover.png",
|
||||||
description: "旅拍转场,前景遮挡切换到新地点,带自然手持感和轻微速度变化。",
|
description: "旅拍转场,前景遮挡切换到新地点,带自然手持感和轻微速度变化。",
|
||||||
duration: "5s",
|
duration: "5s",
|
||||||
id: "travel-whip-transition",
|
id: "travel-whip-transition",
|
||||||
@ -1155,9 +1162,9 @@ const NINE_GRID_CELL_VISUALS: GridCellVisual[] = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
const TARGET_AVATAR =
|
const TARGET_AVATAR =
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/target-avatar.jpg";
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/target-avatar.jpg";
|
||||||
const PUBLISH_MODAL_BG_URL =
|
const PUBLISH_MODAL_BG_URL =
|
||||||
"https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/publish-modal-bg.png";
|
"https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/publish-modal-bg.png";
|
||||||
const STORY_SCRIPT_PROMPT = "根据我上传的剧本生成一个完整的故事脚本";
|
const STORY_SCRIPT_PROMPT = "根据我上传的剧本生成一个完整的故事脚本";
|
||||||
const AUDIO_TO_VIDEO_PROMPT =
|
const AUDIO_TO_VIDEO_PROMPT =
|
||||||
"根据上传的音频生成对应场景画面,镜头语言、节奏、音乐匹配情绪变化,电影级质感。";
|
"根据上传的音频生成对应场景画面,镜头语言、节奏、音乐匹配情绪变化,电影级质感。";
|
||||||
@ -4139,7 +4146,7 @@ function normalizeTextResourceNode(node: CloneNode, fallbackIndex: number): Clon
|
|||||||
draftValues: {
|
draftValues: {
|
||||||
...(node.draftValues || {}),
|
...(node.draftValues || {}),
|
||||||
action: "TEXT_RESOURCE",
|
action: "TEXT_RESOURCE",
|
||||||
content: Array.isArray(node.draftValues?.content) ? node.draftValues?.content : [],
|
content: restoreTextResourceContent(node.draftValues),
|
||||||
nodeType: "text",
|
nodeType: "text",
|
||||||
scriptRole: "resource",
|
scriptRole: "resource",
|
||||||
viewMode: undefined,
|
viewMode: undefined,
|
||||||
@ -4568,6 +4575,8 @@ export function LiblibVideoCanvasReplica({
|
|||||||
);
|
);
|
||||||
const [nodes, setNodes] = useState<CloneNode[]>([]);
|
const [nodes, setNodes] = useState<CloneNode[]>([]);
|
||||||
const activeGenerationSubmissions = useRef(new Set<string>());
|
const activeGenerationSubmissions = useRef(new Set<string>());
|
||||||
|
const [isVideoAgentOpen, setIsVideoAgentOpen] = useState(false);
|
||||||
|
const receivedAgentNodeIds = useRef(new Set<string>());
|
||||||
const [edges, setEdges] = useState<CloneEdge[]>([]);
|
const [edges, setEdges] = useState<CloneEdge[]>([]);
|
||||||
const [selectedNodeId, setSelectedNodeId] = useState<string | null>(null);
|
const [selectedNodeId, setSelectedNodeId] = useState<string | null>(null);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -4760,6 +4769,7 @@ export function LiblibVideoCanvasReplica({
|
|||||||
setNodes(migrateLegacyGroups(restored));
|
setNodes(migrateLegacyGroups(restored));
|
||||||
setMultiSelection([]);
|
setMultiSelection([]);
|
||||||
setGroupTemplates((project.canvasData as any)?.groupTemplates||[]);
|
setGroupTemplates((project.canvasData as any)?.groupTemplates||[]);
|
||||||
|
receivedAgentNodeIds.current = new Set(restored.map((node) => node.id));
|
||||||
setEdges(restoreEdges(project.canvasData));
|
setEdges(restoreEdges(project.canvasData));
|
||||||
const storedViewport = data?.viewport;
|
const storedViewport = data?.viewport;
|
||||||
setViewport(storedViewport && [storedViewport.x, storedViewport.y, storedViewport.zoom].every(Number.isFinite)
|
setViewport(storedViewport && [storedViewport.x, storedViewport.y, storedViewport.zoom].every(Number.isFinite)
|
||||||
@ -4865,6 +4875,66 @@ export function LiblibVideoCanvasReplica({
|
|||||||
applyMergedSnapshot,
|
applyMergedSnapshot,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const refreshVideoAgentResults = useCallback(async (projectId: string, resultNodeIds: string[]) => {
|
||||||
|
if (projectId !== canvasId) return;
|
||||||
|
const result = await canvasService.getProject(canvasId);
|
||||||
|
if (!result.success || !result.data) return;
|
||||||
|
const incoming = restoreNodes(result.data.canvasData).filter((node) => resultNodeIds.includes(node.id));
|
||||||
|
const unseen = incoming.filter((node) => !receivedAgentNodeIds.current.has(node.id));
|
||||||
|
for (const node of incoming) receivedAgentNodeIds.current.add(node.id);
|
||||||
|
setNodes((current) => mergeVideoAgentResults(current.map((node) => {
|
||||||
|
const remote = incoming.find((item) => item.id === node.id);
|
||||||
|
return remote && remote.draftValues?.temporaryVideoUrl === node.draftValues?.mediaUrl && remote.draftValues?.mediaUrl !== node.draftValues?.mediaUrl ? { ...node, draftValues: { ...node.draftValues, mediaUrl: remote.draftValues?.mediaUrl, url: remote.draftValues?.url } } : node;
|
||||||
|
}), unseen));
|
||||||
|
setEdges((current) => mergeVideoAgentResults(current, restoreEdges(result.data!.canvasData).filter((edge) => unseen.some((node) => node.id === edge.target))));
|
||||||
|
}, [canvasId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handler = async (event: Event) => {
|
||||||
|
const change = (event as CustomEvent<{ projectId: string; createdNodeIds?: string[]; updatedNodeIds?: string[]; deletedNodeIds?: string[]; action?: string; nodeIds?: string[]; viewportChanged?: boolean }>).detail;
|
||||||
|
if (change?.projectId !== canvasId) return;
|
||||||
|
const response = await canvasService.getProject(canvasId);
|
||||||
|
if (!response.success || !response.data) return;
|
||||||
|
const remote = restoreNodes(response.data.canvasData);
|
||||||
|
const changed = new Set([...(change.createdNodeIds || []), ...(change.updatedNodeIds || [])]);
|
||||||
|
const deleted = new Set(change.deletedNodeIds || []);
|
||||||
|
setNodes((current) => mergeVideoAgentResults(current.filter((node) => !deleted.has(node.id)).map((node) => changed.has(node.id) ? remote.find((item) => item.id === node.id) || node : node), remote.filter((node) => changed.has(node.id))));
|
||||||
|
setEdges((current) => {
|
||||||
|
const kept = current.filter((edge) => !deleted.has(edge.source) && !deleted.has(edge.target) && !(change.action === "disconnect" && edge.source === change.nodeIds?.[0] && edge.target === change.nodeIds?.[1]));
|
||||||
|
return change.action === "connect" ? mergeVideoAgentResults(kept, restoreEdges(response.data!.canvasData).filter((edge) => edge.source === change.nodeIds?.[0] && edge.target === change.nodeIds?.[1])) : kept;
|
||||||
|
});
|
||||||
|
if (change.viewportChanged && response.data.canvasData?.viewport) setViewport(response.data.canvasData.viewport);
|
||||||
|
};
|
||||||
|
window.addEventListener("video-canvas-agent-mutation", handler);
|
||||||
|
return () => window.removeEventListener("video-canvas-agent-mutation", handler);
|
||||||
|
}, [canvasId]);
|
||||||
|
|
||||||
|
const saveBeforeVideoAgent = useCallback(async () => {
|
||||||
|
const statusResponse = await fetch(`/api/canvas/agent-runs?${new URLSearchParams({ chatId: canvasId, projectId: canvasId })}`, { cache: "no-store" });
|
||||||
|
if (statusResponse.ok && (await statusResponse.json()).run?.status === "running") throw new Error("当前视频任务还在执行,请使用补充要求。");
|
||||||
|
const remote = await canvasService.getProject(canvasId);
|
||||||
|
if (!remote.success || !remote.data) throw new Error("无法读取视频画布,请稍后重试");
|
||||||
|
const remoteNodes = restoreNodes(remote.data.canvasData);
|
||||||
|
const unseen = remoteNodes.filter((node) => node.draftValues?.canvasAgentRunId && !receivedAgentNodeIds.current.has(node.id));
|
||||||
|
const syncedNodes = nodes.map((node) => {
|
||||||
|
const archived = remoteNodes.find((item) => item.id === node.id && item.draftValues?.temporaryVideoUrl === node.draftValues?.mediaUrl);
|
||||||
|
return archived ? { ...node, draftValues: { ...node.draftValues, mediaUrl: archived.draftValues?.mediaUrl, url: archived.draftValues?.url, temporaryVideoUrl: archived.draftValues?.temporaryVideoUrl } } : node;
|
||||||
|
});
|
||||||
|
const mergedNodes = mergeVideoAgentResults(syncedNodes, unseen);
|
||||||
|
const mergedEdges = mergeVideoAgentResults(edges, restoreEdges(remote.data.canvasData).filter((edge) => unseen.some((node) => node.id === edge.target)));
|
||||||
|
const response = await fetch("/api/canvas/video-agent", {
|
||||||
|
method: "POST", headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ projectId: canvasId, baseRevision: remote.data.updatedAt, canvasData: {
|
||||||
|
nodes: serializeNodes(mergedNodes), edges: serializeEdges(mergedEdges),
|
||||||
|
selectedNodeIds: selectedNodeId ? [selectedNodeId] : [], viewport, tool: "select",
|
||||||
|
} }),
|
||||||
|
});
|
||||||
|
const result = await response.json();
|
||||||
|
if (!response.ok) throw new Error(result.error || "画布保存失败,请稍后重试");
|
||||||
|
if (result.success) { setNodes(mergedNodes); setEdges(mergedEdges); for (const node of unseen) receivedAgentNodeIds.current.add(node.id); }
|
||||||
|
if (!result.success) throw new Error(result.error || "画布保存失败,请稍后重试");
|
||||||
|
}, [canvasId, nodes, edges, selectedNodeId, viewport]);
|
||||||
|
|
||||||
const commitTitle = useCallback(() => {
|
const commitTitle = useCallback(() => {
|
||||||
const nextTitle = draftTitle.trim() || "未命名";
|
const nextTitle = draftTitle.trim() || "未命名";
|
||||||
setProjectTitle(nextTitle);
|
setProjectTitle(nextTitle);
|
||||||
@ -10898,6 +10968,50 @@ export function LiblibVideoCanvasReplica({
|
|||||||
};
|
};
|
||||||
}, [lineConnectState, nodes]);
|
}, [lineConnectState, nodes]);
|
||||||
|
|
||||||
|
const handleAgentVideoUiCommand = async (command: CanvasAgentUiCommand) => {
|
||||||
|
const target = nodes.find((node) => node.id === (command.nodeIds[0] || selectedNodeId));
|
||||||
|
if (command.nodeIds.some((id) => !nodes.some((node) => node.id === id))) throw new Error("目标节点不属于当前视频画布");
|
||||||
|
if (target) { setSelectedNodeId(target.id); setSelectedEdgeId(null); }
|
||||||
|
if (command.featureId.startsWith("video.function.")) {
|
||||||
|
const key = command.featureId.slice("video.function.".length);
|
||||||
|
const tool = configuredTools.find((item) => item.toolKey === key);
|
||||||
|
if (!tool) throw new Error("此视频功能已关闭或尚未配置");
|
||||||
|
if ((key === "grid-split" || key === "image-crop") && target) { await runSelectedImageToolAction(target.id, "crop"); return; }
|
||||||
|
if (key === "angle-edit" && target) { setAngleEditorNodeId(target.id); return; }
|
||||||
|
const created = createToolNode(tool, target ? target.x + target.width + 72 : 360, target?.y || 300);
|
||||||
|
addNodes([created], command.nodeIds.map((id) => ({ id: makeId("edge"), source: id, target: created.id })));
|
||||||
|
setInspectorNodeId(created.id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const action = command.featureId.replace("video.ui.", "");
|
||||||
|
const panels: Record<string, Exclude<ActivePanel, null>> = { assets: "assets", roles: "roleLibrary", styles: "styleLibrary", effects: "effectLibrary", history: "history", workflows: "workflow", share: "share", publish: "publish", skills: "skills", help: "help" };
|
||||||
|
if (panels[action]) { setActivePanel(panels[action]); return; }
|
||||||
|
if (action.startsWith("upload-")) { addTargetModule(action === "upload-video" ? "video" : action === "upload-audio" ? "audio" : "image"); toast.message("已添加上传模块,请选择本机文件"); return; }
|
||||||
|
if (action === "save") { if (!(await persistence.saveAndConfirm())) throw new Error("画布保存尚未确认,请稍后重试"); return; }
|
||||||
|
if (action === "export") {
|
||||||
|
const url = URL.createObjectURL(new Blob([JSON.stringify({ title: projectTitle, workspaceType: "video", canvasData: { nodes: serializeNodes(nodes), edges: serializeEdges(edges), viewport } }, null, 2)], { type: "application/json" }));
|
||||||
|
const link = document.createElement("a"); link.href = url; link.download = `${projectTitle}.json`; link.click(); setTimeout(() => URL.revokeObjectURL(url), 1000); return;
|
||||||
|
}
|
||||||
|
if (action === "import") {
|
||||||
|
const picker = document.createElement("input"); picker.type = "file"; picker.accept = ".json,application/json";
|
||||||
|
picker.onchange = async () => { try { const file = picker.files?.[0]; if (!file) return; const data = JSON.parse(await file.text()); const imported = restoreNodes(data.canvasData || data); if (!imported.length) throw new Error("文件中没有可用的视频画布节点"); if (!window.confirm("确认将文件里的节点添加到当前画布?")) return; const ids = new Map(imported.map((node) => [node.id, makeId("import")])); addNodes(imported.map((node) => ({ ...node, id: ids.get(node.id)! })), restoreEdges(data.canvasData || data).filter((edge) => ids.has(edge.source) && ids.has(edge.target)).map((edge) => ({ id: makeId("edge"), source: ids.get(edge.source)!, target: ids.get(edge.target)! }))); } catch (error) { toast.error(error instanceof Error ? error.message : "导入失败"); } }; picker.click(); return;
|
||||||
|
}
|
||||||
|
if (!target) throw new Error("请先选中具体素材节点");
|
||||||
|
if (action === "download") { downloadMediaNode(target); return; }
|
||||||
|
if (action === "preview") { setPreviewNode(target); return; }
|
||||||
|
if (action === "local-edit") { await runSelectedImageToolAction(target.id, "localEdit"); return; }
|
||||||
|
if (action === "annotate") { await runSelectedImageToolAction(target.id, "annotate"); return; }
|
||||||
|
if (action === "director") { setDirectorConsoleNodeId(target.id); return; }
|
||||||
|
if (action === "storyboard") { openStoryScriptWorkflow(target.id); return; }
|
||||||
|
if (action === "batch-storyboard" || action === "batch-video") { openStoryScriptWorkflow(target.id); toast.message("请检查分镜及费用后在工作台提交批量任务"); return; }
|
||||||
|
if (action === "video-hd") { setInspectorNodeId(target.id); toast.message("当前视频 AI 高清尚缺真实后端,不能将原视频冒充高清结果"); return; }
|
||||||
|
if (action === "video-clip") { setActiveVideoToolbarMode("clip"); return; }
|
||||||
|
if (action === "video-crop") { setActiveVideoToolbarMode("crop"); toast.message("当前裁剪导出后端尚未接入,不能当作已完成的视频裁剪"); return; }
|
||||||
|
if (action === "subtitle-erase") { setActiveVideoToolbarMode("subtitleErase"); toast.message("当前去字幕尚未接入真实处理后端"); return; }
|
||||||
|
if (["audio-split", "voice-split", "background-split", "frame-first", "frame-last", "frame-current"].includes(action)) { setPreviewNode(target); toast.message("已授权入口;当前页面此功能仍为模拟流程,Agent 不会生成占位素材或伪报完成"); return; }
|
||||||
|
throw new Error("此入口需要在相应的视频工作区打开");
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="video-canvas-workspace fixed inset-0 h-dvh w-full overflow-clip bg-[#151515] text-white"
|
className="video-canvas-workspace fixed inset-0 h-dvh w-full overflow-clip bg-[#151515] text-white"
|
||||||
@ -10940,6 +11054,7 @@ export function LiblibVideoCanvasReplica({
|
|||||||
void addUploadedFilesAtPoint(files, toCanvasPoint(event.clientX, event.clientY));
|
void addUploadedFilesAtPoint(files, toCanvasPoint(event.clientX, event.clientY));
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
<CanvasAgentUiListener projectId={canvasId} workspace="video" onVideoAction={handleAgentVideoUiCommand} />
|
||||||
<div className="pointer-events-none absolute inset-0" style={{backgroundImage:"radial-gradient(circle,rgba(255,255,255,0.14) 0.7px,transparent 0.7px)",backgroundPosition:"0.5px 0.5px",backgroundSize:"16px 16px"}} />
|
<div className="pointer-events-none absolute inset-0" style={{backgroundImage:"radial-gradient(circle,rgba(255,255,255,0.14) 0.7px,transparent 0.7px)",backgroundPosition:"0.5px 0.5px",backgroundSize:"16px 16px"}} />
|
||||||
{fileDragActive && (
|
{fileDragActive && (
|
||||||
<div className="pointer-events-none absolute inset-3 z-[1800] flex items-center justify-center rounded-2xl border-2 border-dashed border-cyan-300 bg-cyan-300/5" role="status" aria-live="polite">
|
<div className="pointer-events-none absolute inset-3 z-[1800] flex items-center justify-center rounded-2xl border-2 border-dashed border-cyan-300 bg-cyan-300/5" role="status" aria-live="polite">
|
||||||
@ -11828,6 +11943,29 @@ export function LiblibVideoCanvasReplica({
|
|||||||
onResolve={persistence.resolveConflict}
|
onResolve={persistence.resolveConflict}
|
||||||
onDownload={persistence.downloadDraft}
|
onDownload={persistence.downloadDraft}
|
||||||
/>
|
/>
|
||||||
|
{ZCODE_VIDEO_CANVAS_AUTHORIZED && (isVideoAgentOpen ? (
|
||||||
|
<VideoCanvasAgentPanel
|
||||||
|
key={canvasId}
|
||||||
|
projectId={canvasId}
|
||||||
|
selectedNodeIds={selectedNodeId ? [selectedNodeId] : []}
|
||||||
|
videoModels={canvasModelOptions.filter((model) => isVideoModelDescriptor(model))}
|
||||||
|
imageModels={canvasModelOptions.filter((model) => isImageGenerationModelDescriptor(model))}
|
||||||
|
beforeSend={saveBeforeVideoAgent}
|
||||||
|
onResults={(projectId, ids) => void refreshVideoAgentResults(projectId, ids)}
|
||||||
|
onClose={() => setIsVideoAgentOpen(false)}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
data-canvas-overlay="true"
|
||||||
|
onClick={() => { setInspectorNodeId(null); setIsVideoAgentOpen(true); }}
|
||||||
|
className="absolute right-4 top-[76px] z-40 flex items-center gap-2 rounded-xl border border-white/10 bg-[#252525] px-4 py-3 text-sm text-white shadow-lg hover:bg-[#303030]"
|
||||||
|
aria-label="打开视频创作 Agent"
|
||||||
|
>
|
||||||
|
<FileText className="h-4 w-4" />视频 Agent
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
|
||||||
<TopBar
|
<TopBar
|
||||||
canvasId={canvasId}
|
canvasId={canvasId}
|
||||||
canvasViewMode={canvasViewMode}
|
canvasViewMode={canvasViewMode}
|
||||||
@ -14968,7 +15106,7 @@ function DirectorNodeComposer({
|
|||||||
if(file.size>20*1024*1024){setError("参考图需小于 20 MiB");return;}
|
if(file.size>20*1024*1024){setError("参考图需小于 20 MiB");return;}
|
||||||
setBusy(true);setError("");try{
|
setBusy(true);setError("");try{
|
||||||
const form=new FormData();form.append("file",file);form.append("purpose","director-studio");
|
const form=new FormData();form.append("file",file);form.append("purpose","director-studio");
|
||||||
const response=await fetch("/api/files/upload",{method:"POST",body:form,signal:AbortSignal.timeout(120000)});
|
const response=await fetchUploadWithRetry(form,{signal:AbortSignal.timeout(120000)});
|
||||||
const result=await response.json();const url=result.url||result.data?.url;
|
const result=await response.json();const url=result.url||result.data?.url;
|
||||||
if(!response.ok||!url)throw new Error(result.error||"图片上传失败");
|
if(!response.ok||!url)throw new Error(result.error||"图片上传失败");
|
||||||
onDraft({directorReferenceUrl:String(url)});
|
onDraft({directorReferenceUrl:String(url)});
|
||||||
@ -15489,7 +15627,8 @@ function VideoTargetModuleComposer({
|
|||||||
const ratio = supported.ratios.length && !supported.ratios.includes(normalizeVideoRatioValue(current.ratio)) ? supported.ratios[0] : current.ratio;
|
const ratio = supported.ratios.length && !supported.ratios.includes(normalizeVideoRatioValue(current.ratio)) ? supported.ratios[0] : current.ratio;
|
||||||
const executableResolutions = supported.resolutions.filter(value => ["480p", "720p", "1080p"].includes(value.toLowerCase()));
|
const executableResolutions = supported.resolutions.filter(value => ["480p", "720p", "1080p"].includes(value.toLowerCase()));
|
||||||
const resolution = executableResolutions.length && !executableResolutions.includes(normalizeVideoResolutionValue(current.resolution)) ? executableResolutions[0].toUpperCase() : current.resolution;
|
const resolution = executableResolutions.length && !executableResolutions.includes(normalizeVideoResolutionValue(current.resolution)) ? executableResolutions[0].toUpperCase() : current.resolution;
|
||||||
const duration = supported.durations.length && !supported.durations.includes(normalizeVideoDurationValue(current.duration)) ? `${supported.durations[0]}s` : current.duration;
|
const durations = getCanvasVideoParameters(target, activeMode, resolution || "720P").durations;
|
||||||
|
const duration = durations.length && !durations.includes(normalizeVideoDurationValue(current.duration)) ? `${durations[0]}s` : current.duration;
|
||||||
const audio = supported.supportsAudio ? current.audio : "关闭";
|
const audio = supported.supportsAudio ? current.audio : "关闭";
|
||||||
return ratio === current.ratio && resolution === current.resolution && duration === current.duration && audio === current.audio ? current : {...current, ratio, resolution, duration, audio};
|
return ratio === current.ratio && resolution === current.resolution && duration === current.duration && audio === current.audio ? current : {...current, ratio, resolution, duration, audio};
|
||||||
});
|
});
|
||||||
@ -17792,7 +17931,7 @@ function VideoParameterEditor({
|
|||||||
const candidates = filterCanvasModels(modelOptions, audioRequired ? "audio-video" : "video", mode);
|
const candidates = filterCanvasModels(modelOptions, audioRequired ? "audio-video" : "video", mode);
|
||||||
const model = candidates.find(item => item.id === settings.model || item.modelId === settings.model) ||
|
const model = candidates.find(item => item.id === settings.model || item.modelId === settings.model) ||
|
||||||
(!normalizeModelRef(settings.model) || settings.model === "auto" ? candidates[0] : undefined);
|
(!normalizeModelRef(settings.model) || settings.model === "auto" ? candidates[0] : undefined);
|
||||||
const options = getCanvasVideoParameters(model, mode);
|
const options = getCanvasVideoParameters(model, mode, settings.resolution || "720P");
|
||||||
const executableResolutions = options.resolutions.filter(value => ["480p", "720p", "1080p"].includes(value.toLowerCase())).map(value => value.toUpperCase());
|
const executableResolutions = options.resolutions.filter(value => ["480p", "720p", "1080p"].includes(value.toLowerCase())).map(value => value.toUpperCase());
|
||||||
const durationIndex = Math.max(0, options.durations.indexOf(normalizeVideoDurationValue(settings.duration)));
|
const durationIndex = Math.max(0, options.durations.indexOf(normalizeVideoDurationValue(settings.duration)));
|
||||||
return (
|
return (
|
||||||
@ -20702,8 +20841,18 @@ function PresetMediaCanvasNode({
|
|||||||
</div>
|
</div>
|
||||||
) : mediaType === "audio" ? (
|
) : mediaType === "audio" ? (
|
||||||
<div className="relative h-[calc(100%-32px)]">
|
<div className="relative h-[calc(100%-32px)]">
|
||||||
<OfficialAudioCard durationSeconds={getDraftNumber(node, "durationSeconds", 3)} mediaUrl={mediaUrl} selected={selected} />
|
{isGenerating || isGenerationFailed ? (
|
||||||
{!generated ? (
|
<GenerationPendingPreview
|
||||||
|
errorMessage={getDraftString(node, "generationError")}
|
||||||
|
onCancel={onCancelGeneration}
|
||||||
|
progress={getDraftNumber(node, "generationProgress", 0)}
|
||||||
|
status={generationStatus}
|
||||||
|
statusLabel={getDraftString(node, "pendingLabel") || "生成中"}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<OfficialAudioCard durationSeconds={getDraftNumber(node, "durationSeconds", 0)} mediaUrl={mediaUrl} selected={selected} />
|
||||||
|
)}
|
||||||
|
{!generated && !isGenerating && !isGenerationFailed ? (
|
||||||
<button
|
<button
|
||||||
aria-label={mediaUrl ? "替换音频" : "上传音频"}
|
aria-label={mediaUrl ? "替换音频" : "上传音频"}
|
||||||
className="absolute right-3 top-3 flex h-9 w-9 items-center justify-center rounded-[8px] bg-black/56 text-white/90"
|
className="absolute right-3 top-3 flex h-9 w-9 items-center justify-center rounded-[8px] bg-black/56 text-white/90"
|
||||||
@ -21249,12 +21398,14 @@ function GenerationPendingPreview({
|
|||||||
previewUrl,
|
previewUrl,
|
||||||
progress,
|
progress,
|
||||||
status,
|
status,
|
||||||
|
statusLabel,
|
||||||
}: {
|
}: {
|
||||||
errorMessage?: string;
|
errorMessage?: string;
|
||||||
onCancel: () => void;
|
onCancel: () => void;
|
||||||
previewUrl?: string;
|
previewUrl?: string;
|
||||||
progress: number;
|
progress: number;
|
||||||
status: string;
|
status: string;
|
||||||
|
statusLabel?: string;
|
||||||
}) {
|
}) {
|
||||||
const displayProgress = Math.max(2, Math.min(99, Math.round(progress)));
|
const displayProgress = Math.max(2, Math.min(99, Math.round(progress)));
|
||||||
const failed = status === "failed";
|
const failed = status === "failed";
|
||||||
@ -21300,7 +21451,7 @@ function GenerationPendingPreview({
|
|||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<span>生成中 {displayProgress}%...</span>
|
<span>{statusLabel || `生成中 ${displayProgress}%...`}</span>
|
||||||
<button
|
<button
|
||||||
className="font-medium text-white/42 transition hover:text-white/80"
|
className="font-medium text-white/42 transition hover:text-white/80"
|
||||||
onClick={(event) => {
|
onClick={(event) => {
|
||||||
@ -21328,11 +21479,7 @@ function PureTextResourceCanvasNode({
|
|||||||
onTextResourceChange: (content: string) => void;
|
onTextResourceChange: (content: string) => void;
|
||||||
selected: boolean;
|
selected: boolean;
|
||||||
}) {
|
}) {
|
||||||
const contentValues = node.draftValues?.content;
|
const textContent = restoreTextResourceContent(node.draftValues)[0] || "";
|
||||||
const textContent =
|
|
||||||
Array.isArray(contentValues) && typeof contentValues[0] === "string"
|
|
||||||
? contentValues[0]
|
|
||||||
: "";
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full w-full">
|
<div className="h-full w-full">
|
||||||
@ -21698,8 +21845,8 @@ const STYLE_MARKET_CATEGORIES = [
|
|||||||
"全部",
|
"全部",
|
||||||
];
|
];
|
||||||
|
|
||||||
const STYLE_MARKET_THUMBNAIL_URL = "https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-character-three-view-cover.png";
|
const STYLE_MARKET_THUMBNAIL_URL = "https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-character-three-view-cover.png";
|
||||||
const EFFECT_MARKET_THUMBNAIL_URL = "https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/video-canvas-demo/starter-first-frame-video-cover.png";
|
const EFFECT_MARKET_THUMBNAIL_URL = "https://scqnycdn.jiansun.vip/legacy-oss/demo-sparkai/video-canvas-demo/starter-first-frame-video-cover.png";
|
||||||
|
|
||||||
const STYLE_MARKET_CARDS: AssetMarketCard[] = [
|
const STYLE_MARKET_CARDS: AssetMarketCard[] = [
|
||||||
["复古马卡龙", "算力潮汐", 177],
|
["复古马卡龙", "算力潮汐", 177],
|
||||||
|
|||||||
47
components/canvas/chat-panel/canvas-agent-progress.tsx
Normal file
47
components/canvas/chat-panel/canvas-agent-progress.tsx
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
"use client";
|
||||||
|
import { CircleAlert, Check, ChevronDown, Loader2, RotateCcw, Square } from "lucide-react";
|
||||||
|
import type { AgentRunView } from "@/lib/ai/canvas-agent-runtime/protocol";
|
||||||
|
import { canvasAgentProgressLabel, type CanvasAgentProgress } from "@/lib/ai/canvas-agent-runtime/progress";
|
||||||
|
|
||||||
|
export function CanvasAgentProgressMessage(props: {
|
||||||
|
progress: CanvasAgentProgress; run?: AgentRunView | null; busy?: boolean;
|
||||||
|
stopping?: boolean; sending?: boolean; onCancel?: () => void; onResume?: () => void;
|
||||||
|
}) {
|
||||||
|
const { progress, run } = props;
|
||||||
|
const running = progress.status === "running";
|
||||||
|
const label = progress.status === "completed" ? "处理完成"
|
||||||
|
: props.stopping && running ? "正在停止后续步骤,已完成的结果会保留"
|
||||||
|
: canvasAgentProgressLabel(progress.message || "正在处理你的任务");
|
||||||
|
return (
|
||||||
|
<div className="my-2 w-full min-w-0 text-xs text-zinc-500 dark:text-zinc-400" data-testid="canvas-agent-progress" data-agent-message-id={run?.assistantMessageId}>
|
||||||
|
<div className="flex items-start gap-2">
|
||||||
|
<details className="min-w-0 flex-1" open={running}>
|
||||||
|
<summary className="flex cursor-pointer list-none items-center gap-2 py-1 [&::-webkit-details-marker]:hidden">
|
||||||
|
{running ? <Loader2 className="h-3.5 w-3.5 shrink-0 animate-spin" /> : progress.status === "completed" ? <Check className="h-3.5 w-3.5 shrink-0" /> : <CircleAlert className="h-3.5 w-3.5 shrink-0" />}
|
||||||
|
<span className="min-w-0 flex-1" role="status" aria-live="polite">{label}</span>
|
||||||
|
<ChevronDown className="h-3 w-3 shrink-0" />
|
||||||
|
</summary>
|
||||||
|
{progress.activities?.length ? (
|
||||||
|
<ol className="ml-1.5 mt-1 space-y-1.5 border-l border-zinc-200 pl-4 dark:border-zinc-800" aria-label="任务执行步骤">
|
||||||
|
{progress.activities.map(activity => (
|
||||||
|
<li key={activity.id} className="flex items-center gap-2">
|
||||||
|
{activity.status === "started" ? <Loader2 className="h-3 w-3 shrink-0 animate-spin" /> : <span className="h-1 w-1 shrink-0 rounded-full bg-current" />}
|
||||||
|
<span>{canvasAgentProgressLabel(activity.title)}</span>
|
||||||
|
<span className="text-[11px] opacity-75">{activity.status === "succeeded" ? "已完成" : activity.status === "failed" ? "未完成" : activity.status === "queued" ? "待执行" : "进行中"}</span>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ol>
|
||||||
|
) : null}
|
||||||
|
{run?.inputs.slice(-2).map(input => (
|
||||||
|
<p key={input.id} className="mt-2 whitespace-pre-wrap break-words">{input.applied ? "已应用补充要求" : "已收到补充要求"}:{input.text}</p>
|
||||||
|
))}
|
||||||
|
</details>
|
||||||
|
{running && props.onCancel ? (
|
||||||
|
<button type="button" onClick={props.onCancel} disabled={props.sending || props.stopping} className="rounded-md p-1.5 hover:bg-zinc-100 disabled:opacity-40 dark:hover:bg-white/10" title="停止后续步骤" aria-label="停止后续步骤"><Square className="h-3.5 w-3.5" /></button>
|
||||||
|
) : run?.resumable && props.onResume ? (
|
||||||
|
<button type="button" onClick={props.onResume} disabled={props.busy} className="inline-flex shrink-0 items-center gap-1 rounded-md px-2 py-1 hover:bg-zinc-100 disabled:opacity-40 dark:hover:bg-white/10"><RotateCcw className="h-3.5 w-3.5" />继续任务</button>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
13
components/canvas/chat-panel/canvas-agent-run-panel.tsx
Normal file
13
components/canvas/chat-panel/canvas-agent-run-panel.tsx
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
"use client";
|
||||||
|
// Compatibility wrapper for the paused video entry. Image chat observes the run
|
||||||
|
// once and renders its activity in the corresponding assistant message.
|
||||||
|
import { CanvasAgentProgressMessage } from "./canvas-agent-progress";
|
||||||
|
import { useCanvasAgentRun, type CanvasAgentRunOptions } from "./use-canvas-agent-run";
|
||||||
|
import { canvasAgentProgress } from "@/lib/ai/canvas-agent-runtime/progress";
|
||||||
|
export function CanvasAgentRunPanel(props: CanvasAgentRunOptions & { onResume: (runId: string) => void }) {
|
||||||
|
const task = useCanvasAgentRun(props);
|
||||||
|
if (!task.run) return null;
|
||||||
|
return <CanvasAgentProgressMessage progress={canvasAgentProgress(task.run)} run={task.run} busy={props.busy}
|
||||||
|
stopping={task.stopPending} sending={task.sending} onCancel={() => void task.control("cancel")}
|
||||||
|
onResume={() => props.onResume(task.run!.id)} />;
|
||||||
|
}
|
||||||
@ -152,16 +152,7 @@ export function shouldRetryCanvasViewOperationAfterRefresh(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ImageGenerationStatusStreamPayload = {
|
export type { ImageGenerationStatusStreamPayload } from "@/lib/canvas/image-generation-status";
|
||||||
phase?: "queued" | "processing" | "finalizing" | "failed" | "completed";
|
|
||||||
providerType?: string;
|
|
||||||
modelName?: string;
|
|
||||||
imageCount?: number;
|
|
||||||
queuePosition?: number;
|
|
||||||
sameModelRunningCount?: number;
|
|
||||||
concurrencyLimit?: number;
|
|
||||||
estimatedWaitMs?: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type AgentTargetLockRefPreview = {
|
export type AgentTargetLockRefPreview = {
|
||||||
url: string;
|
url: string;
|
||||||
|
|||||||
78
components/canvas/chat-panel/canvas-agent-ui-listener.tsx
Normal file
78
components/canvas/chat-panel/canvas-agent-ui-listener.tsx
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
"use client";
|
||||||
|
import { useEffect, useRef } from "react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { getCanvasAgentAccessManifest } from "@/lib/ai/canvas-agent-runtime/capability-access";
|
||||||
|
import {
|
||||||
|
canvasAgentUiCommandSchema,
|
||||||
|
CANVAS_AGENT_UI_COMMAND_EVENT,
|
||||||
|
type CanvasAgentUiCommand,
|
||||||
|
} from "@/lib/ai/canvas-agent-runtime/ui-command";
|
||||||
|
import { useCanvasStore } from "@/store/canvas";
|
||||||
|
|
||||||
|
export function CanvasAgentUiListener(props: {
|
||||||
|
projectId: string;
|
||||||
|
workspace: "canvas" | "video";
|
||||||
|
onVideoAction?: (command: CanvasAgentUiCommand) => void | Promise<void>;
|
||||||
|
}) {
|
||||||
|
const latest = useRef(props);
|
||||||
|
latest.current = props;
|
||||||
|
const seen = useRef(new Set<string>());
|
||||||
|
useEffect(() => {
|
||||||
|
const handler = async (event: Event) => {
|
||||||
|
const parsed = canvasAgentUiCommandSchema.safeParse((event as CustomEvent).detail);
|
||||||
|
if (!parsed.success) return;
|
||||||
|
const command = parsed.data,
|
||||||
|
current = latest.current;
|
||||||
|
if (
|
||||||
|
command.projectId !== current.projectId ||
|
||||||
|
command.workspace !== current.workspace ||
|
||||||
|
seen.current.has(command.requestId)
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
seen.current.add(command.requestId);
|
||||||
|
try {
|
||||||
|
if (current.workspace === "video") {
|
||||||
|
await current.onVideoAction?.(command);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const feature = getCanvasAgentAccessManifest().find(
|
||||||
|
(item) => item.id === command.featureId && item.workspace === "canvas",
|
||||||
|
);
|
||||||
|
if (!feature || !feature.id.startsWith("canvas.ui.")) throw new Error("这个界面入口不存在");
|
||||||
|
const key = feature.id.slice("canvas.ui.".length);
|
||||||
|
if (/chatSend|chatStartGeneration/.test(key)) {
|
||||||
|
toast.message("已定位对话提交入口,请由你确认并发送;Agent 不会伪造下一条用户消息");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const state = useCanvasStore.getState();
|
||||||
|
if (state.projectId !== current.projectId) return;
|
||||||
|
if (command.nodeIds.length)
|
||||||
|
state.selectNodes(
|
||||||
|
command.nodeIds.filter((id) => state.nodes.some((node) => node.id === id)),
|
||||||
|
);
|
||||||
|
await new Promise<void>((resolve) =>
|
||||||
|
requestAnimationFrame(() => requestAnimationFrame(() => resolve())),
|
||||||
|
);
|
||||||
|
const element = Array.from(
|
||||||
|
document.querySelectorAll<HTMLElement>("[data-canvas-feature-key]"),
|
||||||
|
).find((item) => item.dataset.canvasFeatureKey === key && item.getClientRects().length);
|
||||||
|
const button = element?.closest<HTMLElement>("button,[role=button],a");
|
||||||
|
if (!button || button.hasAttribute("disabled"))
|
||||||
|
throw new Error(
|
||||||
|
"此入口当前未显示或不可用;请切换相应工作区。工具授权不会恢复被隐藏的按钮。",
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
/Delete|Generate|Confirm|Cancel|SaveText|realtimeSend/.test(key) &&
|
||||||
|
!window.confirm(`Agent 请求操作「${feature.title}」。确认在当前画布执行?`)
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
button.click();
|
||||||
|
} catch (error) {
|
||||||
|
toast.error(error instanceof Error ? error.message : "无法打开画布入口");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener(CANVAS_AGENT_UI_COMMAND_EVENT, handler);
|
||||||
|
return () => window.removeEventListener(CANVAS_AGENT_UI_COMMAND_EVENT, handler);
|
||||||
|
}, []);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
@ -448,7 +448,7 @@ function extractChatMediaUrls(text: string, kind: ChatMediaKind): string[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const markdownLinkPattern = /\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi;
|
const markdownLinkPattern = /(?<!!)\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi;
|
||||||
while ((match = markdownLinkPattern.exec(text)) !== null) {
|
while ((match = markdownLinkPattern.exec(text)) !== null) {
|
||||||
const label = String(match[1] || "");
|
const label = String(match[1] || "");
|
||||||
const hintedKind = /音频|语音|声音|audio|mp3/i.test(label)
|
const hintedKind = /音频|语音|声音|audio|mp3/i.test(label)
|
||||||
@ -491,7 +491,7 @@ export function stripMediaMarkup(text: string): string {
|
|||||||
.replace(/<audio[\s\S]*?<\/audio>/gi, "")
|
.replace(/<audio[\s\S]*?<\/audio>/gi, "")
|
||||||
.replace(/<audio\b[^>]*\/?\s*>/gi, "")
|
.replace(/<audio\b[^>]*\/?\s*>/gi, "")
|
||||||
.replace(/<a[\s\S]*?<\/a>/gi, "")
|
.replace(/<a[\s\S]*?<\/a>/gi, "")
|
||||||
.replace(/\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi, (match, label, url) => {
|
.replace(/(?<!!)\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi, (match, label, url) => {
|
||||||
const hintedMedia = /音频|语音|声音|audio|mp3|视频|影片|播放|video/i.test(
|
const hintedMedia = /音频|语音|声音|audio|mp3|视频|影片|播放|video/i.test(
|
||||||
String(label || "")
|
String(label || "")
|
||||||
);
|
);
|
||||||
|
|||||||
70
components/canvas/chat-panel/canvas-image-result-sync.ts
Normal file
70
components/canvas/chat-panel/canvas-image-result-sync.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import { normalizeImageIdentityUrl } from "./canvas-chat-media";
|
||||||
|
import type { ArchivedImageUrlMap } from "@/lib/storage/image-archive-urls";
|
||||||
|
|
||||||
|
type ImageResultNode = {
|
||||||
|
type: string;
|
||||||
|
data?: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
function imageUrls(node: ImageResultNode): string[] {
|
||||||
|
if (node.type !== "image") return [];
|
||||||
|
const data = node.data as { src?: string; metadata?: { sourceImageKey?: string } } | undefined;
|
||||||
|
return [data?.src, data?.metadata?.sourceImageKey].filter(
|
||||||
|
(url): url is string => typeof url === "string" && Boolean(url.trim()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getCanvasImageResultUrlKeys(
|
||||||
|
nodes: readonly ImageResultNode[],
|
||||||
|
aliases: ArchivedImageUrlMap = {},
|
||||||
|
) {
|
||||||
|
const keys = new Set<string>();
|
||||||
|
for (const node of nodes) {
|
||||||
|
for (const url of imageUrls(node)) {
|
||||||
|
keys.add(normalizeImageIdentityUrl(url));
|
||||||
|
if (aliases[url]) keys.add(normalizeImageIdentityUrl(aliases[url]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
keys.delete("");
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A history refresh may already contain bucket URLs while this tab still shows
|
||||||
|
// provider previews. Resolve aliases only for an existing result of this message;
|
||||||
|
// a new generation never waits for storage before appearing on the canvas.
|
||||||
|
export async function resolveCanvasImageResultAliases(params: {
|
||||||
|
nodes: readonly ImageResultNode[];
|
||||||
|
messageId?: string;
|
||||||
|
imageUrls: string[];
|
||||||
|
resolveAliases: (urls: string[]) => Promise<ArchivedImageUrlMap>;
|
||||||
|
}): Promise<ArchivedImageUrlMap> {
|
||||||
|
const keys = getCanvasImageResultUrlKeys(params.nodes);
|
||||||
|
if (!params.messageId || params.imageUrls.every((url) => keys.has(normalizeImageIdentityUrl(url)))) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
const urls = [...new Set(params.nodes.flatMap((node) => {
|
||||||
|
const data = node.data as { metadata?: { sourceMessageId?: string } } | undefined;
|
||||||
|
return data?.metadata?.sourceMessageId === params.messageId ? imageUrls(node) : [];
|
||||||
|
}))].filter((url) => /^https?:\/\//i.test(url));
|
||||||
|
if (!urls.length) return {};
|
||||||
|
const aliases: ArchivedImageUrlMap = {};
|
||||||
|
for (let index = 0; index < urls.length; index += 100) {
|
||||||
|
Object.assign(aliases, await params.resolveAliases(urls.slice(index, index + 100)));
|
||||||
|
}
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call synchronously immediately before changing the store, after all image
|
||||||
|
// loading/network awaits. Both stream and history writers share this check.
|
||||||
|
export function commitCanvasImageResultIfMissing<T>(params: {
|
||||||
|
getNodes: () => readonly ImageResultNode[];
|
||||||
|
url: string;
|
||||||
|
aliases: ArchivedImageUrlMap;
|
||||||
|
commit: () => T;
|
||||||
|
}): T | undefined {
|
||||||
|
const key = normalizeImageIdentityUrl(params.url);
|
||||||
|
if (!key || getCanvasImageResultUrlKeys(params.getNodes(), params.aliases).has(key)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return params.commit();
|
||||||
|
}
|
||||||
64
components/canvas/chat-panel/use-canvas-agent-run.ts
Normal file
64
components/canvas/chat-panel/use-canvas-agent-run.ts
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
"use client";
|
||||||
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
import useSWR from "swr";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import type { AgentRunView } from "@/lib/ai/canvas-agent-runtime/protocol";
|
||||||
|
export type CanvasAgentRunOptions = {
|
||||||
|
enabled: boolean; chatId: string; projectId: string; busy: boolean;
|
||||||
|
onResults: (projectId: string, nodeIds: string[]) => void;
|
||||||
|
onRunChange?: (run: AgentRunView | null) => void;
|
||||||
|
};
|
||||||
|
export function useCanvasAgentRun(props: CanvasAgentRunOptions) {
|
||||||
|
const [sending, setSending] = useState(false);
|
||||||
|
const [stopping, setStopping] = useState(false);
|
||||||
|
const sendingRef = useRef(false);
|
||||||
|
const scope = `${props.chatId}:${props.projectId}`;
|
||||||
|
const latest = useRef({ props, scope }); latest.current = { props, scope };
|
||||||
|
const resultSignature = useRef("");
|
||||||
|
const key = props.enabled ? `/api/canvas/agent-runs?${new URLSearchParams({ chatId: props.chatId, projectId: props.projectId })}` : null;
|
||||||
|
const { data, mutate } = useSWR<{ run: AgentRunView | null }>(key, async (url: string) => {
|
||||||
|
const response = await fetch(url, { cache: "no-store" });
|
||||||
|
if (!response.ok) return { run: null };
|
||||||
|
return response.json();
|
||||||
|
}, { refreshInterval: latest => latest?.run?.status === "running" || props.busy ? 2000 : 0,
|
||||||
|
revalidateOnFocus: true, shouldRetryOnError: false });
|
||||||
|
const run = props.enabled && data?.run?.chatId === props.chatId && data.run.projectId === props.projectId ? data.run : null;
|
||||||
|
useEffect(() => { latest.current.props.onRunChange?.(run); }, [run]);
|
||||||
|
useEffect(() => { setSending(false); sendingRef.current = false; setStopping(false); resultSignature.current = ""; }, [scope]);
|
||||||
|
useEffect(() => {
|
||||||
|
if (!props.enabled) return;
|
||||||
|
const handler = (event: Event) => {
|
||||||
|
const update = (event as CustomEvent<AgentRunView>).detail;
|
||||||
|
if (update?.chatId === props.chatId && update?.projectId === props.projectId) void mutate({ run: update }, false);
|
||||||
|
};
|
||||||
|
window.addEventListener("canvas-agent-run:update", handler);
|
||||||
|
return () => window.removeEventListener("canvas-agent-run:update", handler);
|
||||||
|
}, [props.enabled, props.chatId, props.projectId, mutate]);
|
||||||
|
useEffect(() => {
|
||||||
|
if (!run?.resultNodeIds.length) return;
|
||||||
|
const signature = `${run.id}:${run.resultNodeIds.join(",")}:${run.status}`;
|
||||||
|
if (signature === resultSignature.current) return;
|
||||||
|
resultSignature.current = signature;
|
||||||
|
latest.current.props.onResults(run.projectId, run.resultNodeIds);
|
||||||
|
}, [run]);
|
||||||
|
const control = useCallback(async (action: "steer" | "cancel", text?: string) => {
|
||||||
|
if (!run || sendingRef.current) return false;
|
||||||
|
sendingRef.current = true;
|
||||||
|
const startedScope = scope;
|
||||||
|
setSending(true);
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/canvas/agent-runs", { method: "POST", headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ action, chatId: props.chatId, projectId: props.projectId, runId: run.id,
|
||||||
|
...(action === "steer" ? { inputId: crypto.randomUUID(), text: text?.trim() } : {}) }) });
|
||||||
|
const result = await response.json();
|
||||||
|
if (!response.ok) throw new Error(result.error || "操作失败,请重试");
|
||||||
|
if (startedScope !== latest.current.scope) return false;
|
||||||
|
await mutate(result, false);
|
||||||
|
if (action === "cancel") setStopping(true);
|
||||||
|
toast.message(action === "steer" ? "补充要求已收到,将在下一步应用" : "正在停止后续步骤,已完成的结果会保留");
|
||||||
|
return true;
|
||||||
|
} catch (error) { toast.error(error instanceof Error ? error.message : "操作失败"); return false; }
|
||||||
|
finally { if (startedScope === latest.current.scope) { sendingRef.current = false; setSending(false); } }
|
||||||
|
}, [run, sending, scope, props.chatId, props.projectId, mutate]);
|
||||||
|
return { run, sending, stopPending: stopping || Boolean(run?.cancelRequested), control };
|
||||||
|
}
|
||||||
343
components/canvas/chat-panel/video-canvas-agent-panel.tsx
Normal file
343
components/canvas/chat-panel/video-canvas-agent-panel.tsx
Normal file
@ -0,0 +1,343 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useChat } from "@ai-sdk/react";
|
||||||
|
import { DefaultChatTransport, type UIMessage } from "ai";
|
||||||
|
import { ArrowUp, MessageSquare, X } from "lucide-react";
|
||||||
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { CanvasAgentRunPanel } from "./canvas-agent-run-panel";
|
||||||
|
import { BEFORE_PAGE_REFRESH, type PageRefreshRequest } from "@/lib/client/page-refresh";
|
||||||
|
import { readCanvasComposerDraft, writeCanvasComposerDraft, clearCanvasComposerDraft } from "@/lib/client/canvas-draft";
|
||||||
|
import {
|
||||||
|
videoAgentSettingsSchema,
|
||||||
|
type VideoAgentSettings,
|
||||||
|
} from "@/lib/ai/canvas-agent-runtime/video-protocol";
|
||||||
|
|
||||||
|
type ModelOption = { id: string; displayName: string };
|
||||||
|
export function VideoCanvasAgentPanel(props: {
|
||||||
|
projectId: string;
|
||||||
|
selectedNodeIds: string[];
|
||||||
|
videoModels: ModelOption[];
|
||||||
|
imageModels: ModelOption[];
|
||||||
|
beforeSend: () => Promise<void>;
|
||||||
|
onResults: (projectId: string, nodeIds: string[]) => void;
|
||||||
|
onClose: () => void;
|
||||||
|
}) {
|
||||||
|
const [input, setInput] = useState("");
|
||||||
|
const [settings, setSettings] = useState<VideoAgentSettings>(() =>
|
||||||
|
videoAgentSettingsSchema.parse({}),
|
||||||
|
);
|
||||||
|
const [preparing, setPreparing] = useState(false);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [backgroundRunning, setBackgroundRunning] = useState(false);
|
||||||
|
const latest = useRef({ props, settings });
|
||||||
|
latest.current = { props, settings };
|
||||||
|
const resumeRef = useRef<string | undefined>(undefined);
|
||||||
|
const mounted = useRef(true);
|
||||||
|
const resultIds = useRef<string[]>([]);
|
||||||
|
const restoredRunId = useRef("");
|
||||||
|
const { messages, setMessages, sendMessage, status } = useChat({
|
||||||
|
id: `video-agent-${props.projectId}`,
|
||||||
|
generateId: () => crypto.randomUUID(),
|
||||||
|
transport: new DefaultChatTransport({
|
||||||
|
api: "/api/canvas/agent-chat",
|
||||||
|
prepareSendMessagesRequest: ({ messages }) => {
|
||||||
|
const current = latest.current;
|
||||||
|
const resumeRunId = resumeRef.current;
|
||||||
|
resumeRef.current = undefined;
|
||||||
|
return {
|
||||||
|
body: {
|
||||||
|
id: current.props.projectId,
|
||||||
|
message: messages[messages.length - 1],
|
||||||
|
selectedChatModel: "agent",
|
||||||
|
selectedVisibilityType: "private",
|
||||||
|
composerMode: "agent",
|
||||||
|
workspaceVariant: "video",
|
||||||
|
videoAgentSettings: current.settings,
|
||||||
|
resumeRunId,
|
||||||
|
contextPayload: {
|
||||||
|
source: "canvas",
|
||||||
|
projectId: current.props.projectId,
|
||||||
|
selectedNodeIds: current.props.selectedNodeIds,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
onData: (part) => {
|
||||||
|
if (part.type === "data-videoCanvasMutation") window.dispatchEvent(new CustomEvent("video-canvas-agent-mutation", { detail: part.data }));
|
||||||
|
if (part.type === "data-canvasAgentUiCommand") window.dispatchEvent(new CustomEvent("canvas-agent-ui-command", { detail: part.data }));
|
||||||
|
if (part.type === "data-canvasAgentRun")
|
||||||
|
window.dispatchEvent(new CustomEvent("canvas-agent-run:update", { detail: part.data }));
|
||||||
|
},
|
||||||
|
onError: (error) => toast.error(error.message || "视频任务启动失败,请重试"),
|
||||||
|
});
|
||||||
|
useEffect(() => {
|
||||||
|
mounted.current = true;
|
||||||
|
const controller = new AbortController();
|
||||||
|
void fetch(`/api/canvas/video-agent?projectId=${props.projectId}`, {
|
||||||
|
cache: "no-store",
|
||||||
|
signal: controller.signal,
|
||||||
|
})
|
||||||
|
.then(async (response) => {
|
||||||
|
const data = await response.json();
|
||||||
|
if (!response.ok) throw new Error(data.error || "读取视频对话失败");
|
||||||
|
if (!controller.signal.aborted) setMessages(data.messages as UIMessage[]);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
if (!controller.signal.aborted) toast.error(error.message);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (!controller.signal.aborted) setLoading(false);
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
mounted.current = false;
|
||||||
|
controller.abort();
|
||||||
|
};
|
||||||
|
}, [props.projectId, setMessages]);
|
||||||
|
const busy = backgroundRunning || preparing || status === "submitted" || status === "streaming";
|
||||||
|
useEffect(() => {
|
||||||
|
const draft = readCanvasComposerDraft(`video-agent-${props.projectId}`);
|
||||||
|
if (draft) setInput(draft.text);
|
||||||
|
}, [props.projectId]);
|
||||||
|
useEffect(() => {
|
||||||
|
const beforeRefresh = (event: Event) => {
|
||||||
|
const request = (event as CustomEvent<PageRefreshRequest>).detail;
|
||||||
|
if (busy) request.block("当前视频任务仍在处理中,请等待结果返回后再刷新。原任务无需重新提交。");
|
||||||
|
request.save(async () => {
|
||||||
|
if (input.trim() && !writeCanvasComposerDraft(`video-agent-${props.projectId}`, { text: input, attachments: [] }))
|
||||||
|
throw new Error("浏览器无法保存当前输入,请保留此页并检查浏览器存储空间后重试。");
|
||||||
|
});
|
||||||
|
};
|
||||||
|
window.addEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
||||||
|
return () => window.removeEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
||||||
|
}, [busy, input, props.projectId]);
|
||||||
|
const submit = useCallback(
|
||||||
|
async (resumeRunId?: string) => {
|
||||||
|
if (busy || loading) return;
|
||||||
|
const text =
|
||||||
|
input.trim() ||
|
||||||
|
(resumeRunId ? "请继续尚未完成的视频创作步骤,保留已完成的脚本、关键帧和视频。" : "");
|
||||||
|
if (!text) return;
|
||||||
|
setPreparing(true);
|
||||||
|
try {
|
||||||
|
await latest.current.props.beforeSend();
|
||||||
|
if (!mounted.current) return;
|
||||||
|
clearCanvasComposerDraft(`video-agent-${props.projectId}`);
|
||||||
|
resumeRef.current = resumeRunId;
|
||||||
|
setInput("");
|
||||||
|
await sendMessage({ text });
|
||||||
|
} catch (error) {
|
||||||
|
toast.error(error instanceof Error ? error.message : "视频任务启动失败");
|
||||||
|
} finally {
|
||||||
|
resumeRef.current = undefined;
|
||||||
|
if (mounted.current) setPreparing(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[busy, loading, input, sendMessage],
|
||||||
|
);
|
||||||
|
const onRunChange = useCallback(
|
||||||
|
(run: import("@/lib/ai/canvas-agent-runtime/protocol").AgentRunView | null) => {
|
||||||
|
resultIds.current = run?.resultNodeIds || [];
|
||||||
|
if (run?.videoSettings && restoredRunId.current !== run.id) {
|
||||||
|
restoredRunId.current = run.id;
|
||||||
|
setSettings(run.videoSettings);
|
||||||
|
}
|
||||||
|
setBackgroundRunning(run?.status === "running");
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
const onResults = useCallback(
|
||||||
|
(projectId: string, ids: string[]) => latest.current.props.onResults(projectId, ids),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
useEffect(() => {
|
||||||
|
const timer = setInterval(() => {
|
||||||
|
if (resultIds.current.length)
|
||||||
|
latest.current.props.onResults(latest.current.props.projectId, resultIds.current);
|
||||||
|
}, 10_000);
|
||||||
|
return () => clearInterval(timer);
|
||||||
|
}, []);
|
||||||
|
return (
|
||||||
|
<aside
|
||||||
|
className="dark absolute bottom-3 right-3 top-[72px] z-40 flex w-[390px] max-w-[calc(100vw-24px)] flex-col overflow-hidden rounded-2xl border border-white/10 bg-[#202020] text-zinc-100 shadow-2xl"
|
||||||
|
data-canvas-overlay="true"
|
||||||
|
data-testid="video-agent-panel"
|
||||||
|
aria-label="视频创作 Agent"
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-2 border-b border-white/10 px-4 py-3">
|
||||||
|
<MessageSquare className="h-4 w-4" />
|
||||||
|
<span className="flex-1 font-medium">视频创作 Agent</span>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={props.onClose}
|
||||||
|
aria-label="收起视频 Agent"
|
||||||
|
className="rounded-lg p-1 hover:bg-white/10"
|
||||||
|
>
|
||||||
|
<X className="h-4 w-4" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div className="flex-1 space-y-4 overflow-y-auto px-4 py-4" aria-live="polite">
|
||||||
|
{loading ? (
|
||||||
|
<p className="text-sm text-zinc-400">正在读取对话…</p>
|
||||||
|
) : messages.length === 0 ? (
|
||||||
|
<p className="text-sm leading-7 text-zinc-400">
|
||||||
|
选择画布素材,告诉我你想制作的视频。我可以分析参考、编写分镜、生成关键帧并继续生成视频。
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
{messages.map((message) => (
|
||||||
|
<div
|
||||||
|
key={message.id}
|
||||||
|
className={
|
||||||
|
message.role === "user"
|
||||||
|
? "ml-5 whitespace-pre-wrap rounded-2xl bg-white/10 px-3 py-2 text-sm leading-6"
|
||||||
|
: "whitespace-pre-wrap text-sm leading-6 text-zinc-300"
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{message.parts
|
||||||
|
.filter((part) => part.type === "text")
|
||||||
|
.map((part, i) => (
|
||||||
|
<span key={`${message.id}-${i}`}>{part.type === "text" ? part.text : ""}</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<div className="border-t border-white/10 p-3">
|
||||||
|
<CanvasAgentRunPanel
|
||||||
|
enabled={!loading}
|
||||||
|
chatId={props.projectId}
|
||||||
|
projectId={props.projectId}
|
||||||
|
busy={busy}
|
||||||
|
onResume={(id) => void submit(id)}
|
||||||
|
onResults={onResults}
|
||||||
|
onRunChange={onRunChange}
|
||||||
|
/>
|
||||||
|
<div className="mb-2 grid grid-cols-2 gap-2 text-xs">
|
||||||
|
<select
|
||||||
|
aria-label="视频模型"
|
||||||
|
disabled={busy}
|
||||||
|
value={settings.modelRef || ""}
|
||||||
|
onChange={(e) => setSettings({ ...settings, modelRef: e.target.value || undefined })}
|
||||||
|
className="min-w-0 rounded-lg border border-white/10 bg-zinc-800 p-2"
|
||||||
|
>
|
||||||
|
<option value="">默认视频模型</option>
|
||||||
|
{props.videoModels.map((m) => (
|
||||||
|
<option key={m.id} value={m.id}>
|
||||||
|
{m.displayName}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<select
|
||||||
|
aria-label="关键帧模型"
|
||||||
|
disabled={busy}
|
||||||
|
value={settings.imageModelRef || ""}
|
||||||
|
onChange={(e) =>
|
||||||
|
setSettings({ ...settings, imageModelRef: e.target.value || undefined })
|
||||||
|
}
|
||||||
|
className="min-w-0 rounded-lg border border-white/10 bg-zinc-800 p-2"
|
||||||
|
>
|
||||||
|
<option value="">默认关键帧模型</option>
|
||||||
|
{props.imageModels.map((m) => (
|
||||||
|
<option key={m.id} value={m.id}>
|
||||||
|
{m.displayName}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<select
|
||||||
|
aria-label="视频模式"
|
||||||
|
disabled={busy}
|
||||||
|
value={settings.mode}
|
||||||
|
onChange={(e) =>
|
||||||
|
setSettings({ ...settings, mode: e.target.value as VideoAgentSettings["mode"] })
|
||||||
|
}
|
||||||
|
className="rounded-lg border border-white/10 bg-zinc-800 p-2"
|
||||||
|
>
|
||||||
|
{[
|
||||||
|
["auto", "按参考图匹配模式"],
|
||||||
|
["text-to-video", "文生视频"],
|
||||||
|
["first-frame", "首帧视频"],
|
||||||
|
["first-last-frame", "首尾帧视频"],
|
||||||
|
["reference-images", "参考图视频"],
|
||||||
|
].map(([value, label]) => (
|
||||||
|
<option key={value} value={value}>
|
||||||
|
{label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<div className="flex items-center gap-1 rounded-lg border border-white/10 bg-zinc-800 px-2">
|
||||||
|
<input
|
||||||
|
aria-label="视频时长(秒)"
|
||||||
|
type="number"
|
||||||
|
min={1}
|
||||||
|
max={120}
|
||||||
|
disabled={busy}
|
||||||
|
value={settings.duration}
|
||||||
|
onChange={(e) => setSettings({ ...settings, duration: Number(e.target.value) })}
|
||||||
|
className="w-12 min-w-0 bg-transparent py-2"
|
||||||
|
/>
|
||||||
|
<span>秒</span>
|
||||||
|
<select
|
||||||
|
aria-label="视频比例"
|
||||||
|
disabled={busy}
|
||||||
|
value={settings.ratio}
|
||||||
|
onChange={(e) =>
|
||||||
|
setSettings({ ...settings, ratio: e.target.value as VideoAgentSettings["ratio"] })
|
||||||
|
}
|
||||||
|
className="min-w-0 flex-1 bg-zinc-800"
|
||||||
|
>
|
||||||
|
{["16:9", "9:16", "1:1", "4:3", "3:4", "21:9"].map((v) => (
|
||||||
|
<option key={v}>{v}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<select
|
||||||
|
aria-label="视频分辨率"
|
||||||
|
disabled={busy}
|
||||||
|
value={settings.resolution}
|
||||||
|
onChange={(e) =>
|
||||||
|
setSettings({
|
||||||
|
...settings,
|
||||||
|
resolution: e.target.value as VideoAgentSettings["resolution"],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
className="rounded-lg border border-white/10 bg-zinc-800 p-2"
|
||||||
|
>
|
||||||
|
{["480p", "720p", "1080p"].map((v) => (
|
||||||
|
<option key={v}>{v}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<form
|
||||||
|
onSubmit={(event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
void submit();
|
||||||
|
}}
|
||||||
|
className="rounded-2xl border border-white/10 bg-[#151515] p-3"
|
||||||
|
>
|
||||||
|
<textarea
|
||||||
|
aria-label="视频创作要求"
|
||||||
|
value={input}
|
||||||
|
onChange={(event) => { clearCanvasComposerDraft(`video-agent-${props.projectId}`); setInput(event.target.value); }}
|
||||||
|
maxLength={2000}
|
||||||
|
rows={3}
|
||||||
|
placeholder="例如:先分析选中素材,写分镜,再生成一条5秒商品展示视频"
|
||||||
|
className="w-full resize-none bg-transparent text-sm outline-none placeholder:text-zinc-500"
|
||||||
|
/>
|
||||||
|
<div className="mt-2 flex items-center justify-between">
|
||||||
|
<span className="text-xs text-zinc-500">
|
||||||
|
已选择 {props.selectedNodeIds.length} 个素材
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
aria-label="开始视频任务"
|
||||||
|
disabled={busy || loading || !input.trim()}
|
||||||
|
className="rounded-full bg-zinc-100 p-2 text-zinc-900 disabled:opacity-30"
|
||||||
|
>
|
||||||
|
<ArrowUp className="h-4 w-4" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { readDirectorScene, type DirectorScene } from "@/lib/video-canvas/director-scene";
|
import { readDirectorScene, type DirectorScene } from "@/lib/video-canvas/director-scene";
|
||||||
@ -64,9 +65,7 @@ export function DirectorReconstructionPanel({
|
|||||||
const form = new FormData();
|
const form = new FormData();
|
||||||
form.append("file", file);
|
form.append("file", file);
|
||||||
form.append("purpose", "director-studio");
|
form.append("purpose", "director-studio");
|
||||||
const response = await fetch("/api/files/upload", {
|
const response = await fetchUploadWithRetry(form, {
|
||||||
method: "POST",
|
|
||||||
body: form,
|
|
||||||
signal: AbortSignal.timeout(120000),
|
signal: AbortSignal.timeout(120000),
|
||||||
});
|
});
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
|
||||||
import {
|
import {
|
||||||
commitDirectorScene,
|
commitDirectorScene,
|
||||||
resolveDirectorCamera,
|
resolveDirectorCamera,
|
||||||
@ -43,14 +44,13 @@ async function upload(file: File) {
|
|||||||
"purpose",
|
"purpose",
|
||||||
file.type.startsWith("video/") ? "canvas-video-process" : "director-studio",
|
file.type.startsWith("video/") ? "canvas-video-process" : "director-studio",
|
||||||
);
|
);
|
||||||
const response = await fetch(
|
const response = /\.glb$/i.test(file.name)
|
||||||
/\.glb$/i.test(file.name) ? "/api/canvas/director/upload-model" : "/api/files/upload",
|
? await fetch("/api/canvas/director/upload-model", {
|
||||||
{
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: form,
|
body: form,
|
||||||
signal: AbortSignal.timeout(120000),
|
signal: AbortSignal.timeout(120000),
|
||||||
},
|
})
|
||||||
);
|
: await fetchUploadWithRetry(form, { signal: AbortSignal.timeout(120000) });
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
const url = result.url || result.data?.url;
|
const url = result.url || result.data?.url;
|
||||||
if (!response.ok || !url) throw new Error(result.error || "上传失败");
|
if (!response.ok || !url) throw new Error(result.error || "上传失败");
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { CanvasPreviewImage } from "./image-node/canvas-preview-image";
|
||||||
|
import { canvasImagePreviewWidth } from "@/lib/canvas/image-preview-width";
|
||||||
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
@ -59,6 +62,7 @@ import { DetailUiDomRenderer } from "@/components/canvas/DetailUiDomRenderer";
|
|||||||
import { ReferenceImageToolbar } from "@/components/canvas/ReferenceImageToolbar";
|
import { ReferenceImageToolbar } from "@/components/canvas/ReferenceImageToolbar";
|
||||||
import { CanvasToolbarIcon } from "@/components/canvas/CanvasToolbarIcon";
|
import { CanvasToolbarIcon } from "@/components/canvas/CanvasToolbarIcon";
|
||||||
import { CanvasImageDownloadProgressDialog } from "@/components/canvas/CanvasImageDownloadProgressDialog";
|
import { CanvasImageDownloadProgressDialog } from "@/components/canvas/CanvasImageDownloadProgressDialog";
|
||||||
|
import { ProgressiveImagePreview, type LoadedImagePreview } from "@/components/progressive-image-preview";
|
||||||
import {
|
import {
|
||||||
areImageAdjustmentsEqual,
|
areImageAdjustmentsEqual,
|
||||||
buildImageAdjustmentFilterStyle,
|
buildImageAdjustmentFilterStyle,
|
||||||
@ -174,7 +178,7 @@ import {
|
|||||||
} from "@/components/ui/tooltip";
|
} from "@/components/ui/tooltip";
|
||||||
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
|
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
|
||||||
import {
|
import {
|
||||||
downloadCanvasImageAsPng,
|
downloadOriginalImage,
|
||||||
getCanvasImageDownloadErrorMessage,
|
getCanvasImageDownloadErrorMessage,
|
||||||
type CanvasImageDownloadProgress,
|
type CanvasImageDownloadProgress,
|
||||||
} from "@/lib/client/canvas-image-download";
|
} from "@/lib/client/canvas-image-download";
|
||||||
@ -185,7 +189,7 @@ import {
|
|||||||
normalizeDetailUiSubjectAnalysis,
|
normalizeDetailUiSubjectAnalysis,
|
||||||
} from "@/lib/detail-ui-subject-analysis";
|
} from "@/lib/detail-ui-subject-analysis";
|
||||||
import type { DetailUiScreenOverlaySnapshot } from "@/lib/detail-ui-surface-adapter";
|
import type { DetailUiScreenOverlaySnapshot } from "@/lib/detail-ui-surface-adapter";
|
||||||
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
import { buildImageFallbackUrl, buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
import {
|
import {
|
||||||
DEFAULT_PUBLIC_THREED_MODEL_ALIAS_CONFIG,
|
DEFAULT_PUBLIC_THREED_MODEL_ALIAS_CONFIG,
|
||||||
getDefaultThreeDModelAliasId,
|
getDefaultThreeDModelAliasId,
|
||||||
@ -231,6 +235,8 @@ import {
|
|||||||
import { normalizeOptionalHexColor } from "@/lib/canvas/text-edit-colors";
|
import { normalizeOptionalHexColor } from "@/lib/canvas/text-edit-colors";
|
||||||
import { isCanvasImageUploadPending } from "@/lib/canvas/local-upload-state";
|
import { isCanvasImageUploadPending } from "@/lib/canvas/local-upload-state";
|
||||||
import { createCanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
import { createCanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
||||||
|
import { executeCanvasToolbarCapability } from "@/lib/client/canvas-toolbar-capability";
|
||||||
|
import { useCanvasRemoteOperation } from "@/components/canvas/CanvasRemoteOperationContext";
|
||||||
import { getCanvasCapabilityByClientAction } from "@/lib/canvas/capabilities/registry";
|
import { getCanvasCapabilityByClientAction } from "@/lib/canvas/capabilities/registry";
|
||||||
import type { CanvasCapabilityClientAction } from "@/lib/canvas/capabilities/types";
|
import type { CanvasCapabilityClientAction } from "@/lib/canvas/capabilities/types";
|
||||||
import { cn, fetcher } from "@/lib/utils";
|
import { cn, fetcher } from "@/lib/utils";
|
||||||
@ -1657,6 +1663,10 @@ export function ImageNode({
|
|||||||
}, [publicModelAliasConfig.worldModels]);
|
}, [publicModelAliasConfig.worldModels]);
|
||||||
const [isHovered, setIsHovered] = useState(false);
|
const [isHovered, setIsHovered] = useState(false);
|
||||||
const [isProcessing, setIsProcessing] = useState(false);
|
const [isProcessing, setIsProcessing] = useState(false);
|
||||||
|
const [toolbarFailure, setToolbarFailure] = useState<{
|
||||||
|
imageUrl: string;
|
||||||
|
message: string;
|
||||||
|
} | null>(null);
|
||||||
const [isCreatingDetachedResultNode, setIsCreatingDetachedResultNode] =
|
const [isCreatingDetachedResultNode, setIsCreatingDetachedResultNode] =
|
||||||
useState(false);
|
useState(false);
|
||||||
const [imageAdjustPanelOpen, setImageAdjustPanelOpen] = useState(false);
|
const [imageAdjustPanelOpen, setImageAdjustPanelOpen] = useState(false);
|
||||||
@ -1708,6 +1718,7 @@ export function ImageNode({
|
|||||||
const [isUploading, setIsUploading] = useState(false);
|
const [isUploading, setIsUploading] = useState(false);
|
||||||
const [annotatorOpen, setAnnotatorOpen] = useState(false);
|
const [annotatorOpen, setAnnotatorOpen] = useState(false);
|
||||||
const [fullscreenPreviewOpen, setFullscreenPreviewOpen] = useState(false);
|
const [fullscreenPreviewOpen, setFullscreenPreviewOpen] = useState(false);
|
||||||
|
const [fullscreenPreviewImage, setFullscreenPreviewImage] = useState<LoadedImagePreview | null>(null);
|
||||||
const [downloadProgress, setDownloadProgress] =
|
const [downloadProgress, setDownloadProgress] =
|
||||||
useState<CanvasImageDownloadProgress | null>(null);
|
useState<CanvasImageDownloadProgress | null>(null);
|
||||||
const downloadAbortControllerRef = useRef<AbortController | null>(null);
|
const downloadAbortControllerRef = useRef<AbortController | null>(null);
|
||||||
@ -2077,7 +2088,7 @@ export function ImageNode({
|
|||||||
const selectNode = useCanvasStore((state) => state.selectNode);
|
const selectNode = useCanvasStore((state) => state.selectNode);
|
||||||
const projectId = useCanvasStore((state) => state.projectId);
|
const projectId = useCanvasStore((state) => state.projectId);
|
||||||
const getCanvasState = useCanvasStore((state) => state.getCanvasState);
|
const getCanvasState = useCanvasStore((state) => state.getCanvasState);
|
||||||
const loadCanvas = useCanvasStore((state) => state.loadCanvas);
|
const runCanvasRemoteOperation = useCanvasRemoteOperation();
|
||||||
const addPendingChatAttachment = useCanvasStore(
|
const addPendingChatAttachment = useCanvasStore(
|
||||||
(state) => state.addPendingChatAttachment
|
(state) => state.addPendingChatAttachment
|
||||||
);
|
);
|
||||||
@ -2475,14 +2486,11 @@ export function ImageNode({
|
|||||||
[compareButtonVisualSize]
|
[compareButtonVisualSize]
|
||||||
);
|
);
|
||||||
const shouldUseHighFidelityDisplayImage =
|
const shouldUseHighFidelityDisplayImage =
|
||||||
isSelected ||
|
|
||||||
isHovered ||
|
|
||||||
isCompareMode ||
|
isCompareMode ||
|
||||||
boxCutoutMode ||
|
boxCutoutMode ||
|
||||||
localEditMode ||
|
localEditMode ||
|
||||||
cropPanelOpen ||
|
cropPanelOpen ||
|
||||||
imageAdjustPanelOpen ||
|
imageAdjustPanelOpen ||
|
||||||
fullscreenPreviewOpen ||
|
|
||||||
isThisNodePicking;
|
isThisNodePicking;
|
||||||
const displayImageWidth = useMemo(() => {
|
const displayImageWidth = useMemo(() => {
|
||||||
const devicePixelRatio =
|
const devicePixelRatio =
|
||||||
@ -2497,13 +2505,13 @@ export function ImageNode({
|
|||||||
const densityMultiplier = shouldUseHighFidelityDisplayImage ? 1.35 : 1.05;
|
const densityMultiplier = shouldUseHighFidelityDisplayImage ? 1.35 : 1.05;
|
||||||
const minWidth = shouldUseHighFidelityDisplayImage ? 960 : 640;
|
const minWidth = shouldUseHighFidelityDisplayImage ? 960 : 640;
|
||||||
const maxWidth = shouldUseHighFidelityDisplayImage ? 4096 : 2600;
|
const maxWidth = shouldUseHighFidelityDisplayImage ? 4096 : 2600;
|
||||||
return Math.max(
|
return canvasImagePreviewWidth(Math.max(
|
||||||
minWidth,
|
minWidth,
|
||||||
Math.min(
|
Math.min(
|
||||||
maxWidth,
|
maxWidth,
|
||||||
Math.round((node.size.width || 320) * zoomMultiplier * devicePixelRatio * densityMultiplier)
|
Math.round((node.size.width || 320) * zoomMultiplier * devicePixelRatio * densityMultiplier)
|
||||||
)
|
)
|
||||||
);
|
), maxWidth);
|
||||||
}, [node.size.width, shouldUseHighFidelityDisplayImage, viewportZoom]);
|
}, [node.size.width, shouldUseHighFidelityDisplayImage, viewportZoom]);
|
||||||
const displayImageQuality = shouldUseHighFidelityDisplayImage ? 92 : 88;
|
const displayImageQuality = shouldUseHighFidelityDisplayImage ? 92 : 88;
|
||||||
const displayImageSrc = useMemo(
|
const displayImageSrc = useMemo(
|
||||||
@ -2515,15 +2523,6 @@ export function ImageNode({
|
|||||||
}),
|
}),
|
||||||
[data.src, displayImageQuality, displayImageWidth]
|
[data.src, displayImageQuality, displayImageWidth]
|
||||||
);
|
);
|
||||||
const fullscreenPreviewSrc = useMemo(() => {
|
|
||||||
const originalUrl = resolveOriginalImageUrl(data.src || "");
|
|
||||||
return buildImageProxyUrl(originalUrl || data.src || "", {
|
|
||||||
delivery: "proxy",
|
|
||||||
width: 4096,
|
|
||||||
quality: 92,
|
|
||||||
format: "webp",
|
|
||||||
});
|
|
||||||
}, [data.src]);
|
|
||||||
const displayCompareSrc = useMemo(
|
const displayCompareSrc = useMemo(
|
||||||
() =>
|
() =>
|
||||||
buildImageProxyUrl(compareSource, {
|
buildImageProxyUrl(compareSource, {
|
||||||
@ -4678,106 +4677,47 @@ export function ImageNode({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsCreatingDetachedResultNode(true);
|
if (!projectId || !runCanvasRemoteOperation) {
|
||||||
|
toast.error("画布还在加载,请稍后再试");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsCreatingDetachedResultNode(false);
|
||||||
|
setToolbarFailure(null);
|
||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
const actionName = "框选抠图";
|
const actionName = "框选抠图";
|
||||||
const newNodeId = addNode({
|
|
||||||
type: "image",
|
|
||||||
position: {
|
|
||||||
x: node.position.x + node.size.width + 20,
|
|
||||||
y: node.position.y,
|
|
||||||
},
|
|
||||||
size: { width: node.size.width, height: node.size.height },
|
|
||||||
data: {
|
|
||||||
src: "",
|
|
||||||
status: "generating",
|
|
||||||
prompt: `${actionName}处理中...`,
|
|
||||||
title: `${displayTitle}-${actionName}`,
|
|
||||||
originalSrc: data.src,
|
|
||||||
metadata: {
|
|
||||||
cropRect: boxCutoutSelection,
|
|
||||||
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
connections: [node.id],
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await imageGenerationService.boxCutout({
|
const command = createCanvasCapabilityClientCommand({
|
||||||
imageUrl: data.src,
|
capabilityId: "image.box_cutout",
|
||||||
cropRect: boxCutoutSelection,
|
nodeIds: [node.id],
|
||||||
|
projectId,
|
||||||
|
source: "toolbar",
|
||||||
|
args: {
|
||||||
|
...boxCutoutSelection,
|
||||||
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
||||||
sourceNodeId: node.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result.success && result.data?.url) {
|
|
||||||
updateNode(newNodeId, {
|
|
||||||
data: {
|
|
||||||
src: result.data.url,
|
|
||||||
status: "completed",
|
|
||||||
prompt: `${actionName}结果`,
|
|
||||||
title: `${displayTitle}-${actionName}`,
|
|
||||||
originalSrc: data.src,
|
|
||||||
metadata: {
|
|
||||||
cropRect: boxCutoutSelection,
|
|
||||||
cropRectApplied: result.data.cropRectApplied,
|
|
||||||
paddedCropRectApplied: result.data.paddedCropRectApplied,
|
|
||||||
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
|
||||||
editMode: "box-cutout",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
await runCanvasRemoteOperation(() => executeCanvasToolbarCapability(command));
|
||||||
toast.success(`${actionName}完成`);
|
toast.success(`${actionName}完成`);
|
||||||
} else {
|
|
||||||
updateNode(newNodeId, {
|
|
||||||
data: {
|
|
||||||
src: "",
|
|
||||||
status: "failed",
|
|
||||||
prompt: `${actionName}失败`,
|
|
||||||
title: `${displayTitle}-${actionName}-失败`,
|
|
||||||
originalSrc: data.src,
|
|
||||||
metadata: {
|
|
||||||
cropRect: boxCutoutSelection,
|
|
||||||
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
toast.error(result.error || `${actionName}失败`);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("框选抠图失败:", error);
|
setToolbarFailure({
|
||||||
updateNode(newNodeId, {
|
imageUrl: data.src,
|
||||||
data: {
|
message: error instanceof Error ? error.message : `${actionName}失败`,
|
||||||
src: "",
|
|
||||||
status: "failed",
|
|
||||||
prompt: `${actionName}失败`,
|
|
||||||
title: `${displayTitle}-${actionName}-失败`,
|
|
||||||
originalSrc: data.src,
|
|
||||||
metadata: {
|
|
||||||
cropRect: boxCutoutSelection,
|
|
||||||
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
toast.error(`${actionName}失败,请重试`);
|
|
||||||
} finally {
|
} finally {
|
||||||
setIsCreatingDetachedResultNode(false);
|
setIsCreatingDetachedResultNode(false);
|
||||||
setIsProcessing(false);
|
setIsProcessing(false);
|
||||||
resetBoxCutoutState();
|
resetBoxCutoutState();
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
addNode,
|
|
||||||
boxCutoutSelection,
|
boxCutoutSelection,
|
||||||
data.src,
|
data.src,
|
||||||
displayTitle,
|
|
||||||
isProcessing,
|
isProcessing,
|
||||||
node.id,
|
node.id,
|
||||||
node.position.x,
|
projectId,
|
||||||
node.position.y,
|
|
||||||
node.size.height,
|
|
||||||
node.size.width,
|
|
||||||
resetBoxCutoutState,
|
resetBoxCutoutState,
|
||||||
updateNode,
|
runCanvasRemoteOperation,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const animateAddToChat = useCallback(async (previewUrl?: string) => {
|
const animateAddToChat = useCallback(async (previewUrl?: string) => {
|
||||||
@ -6707,7 +6647,7 @@ export function ImageNode({
|
|||||||
const executeSharedToolbarCapability = async (
|
const executeSharedToolbarCapability = async (
|
||||||
action: "rmbg" | "watermark" | "enhance" | "extend" | "layer-split"
|
action: "rmbg" | "watermark" | "enhance" | "extend" | "layer-split"
|
||||||
) => {
|
) => {
|
||||||
if (!projectId) {
|
if (!projectId || !runCanvasRemoteOperation) {
|
||||||
toast.error("画布还在加载,请稍后再试");
|
toast.error("画布还在加载,请稍后再试");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -6718,6 +6658,7 @@ export function ImageNode({
|
|||||||
}
|
}
|
||||||
|
|
||||||
setIsCreatingDetachedResultNode(false);
|
setIsCreatingDetachedResultNode(false);
|
||||||
|
setToolbarFailure(null);
|
||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
try {
|
try {
|
||||||
const command = createCanvasCapabilityClientCommand({
|
const command = createCanvasCapabilityClientCommand({
|
||||||
@ -6726,26 +6667,13 @@ export function ImageNode({
|
|||||||
projectId,
|
projectId,
|
||||||
source: "toolbar",
|
source: "toolbar",
|
||||||
});
|
});
|
||||||
const response = await fetch(`/api/canvas/${projectId}/capabilities`, {
|
await runCanvasRemoteOperation(() => executeCanvasToolbarCapability(command));
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify(command),
|
|
||||||
});
|
|
||||||
const payload = await response.json().catch(() => ({}));
|
|
||||||
if (!response.ok || payload?.success !== true) {
|
|
||||||
throw new Error(payload?.error || `${capability.title}失败`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const projectResult = await canvasService.getProject(projectId);
|
|
||||||
if (!projectResult.success || !projectResult.data?.canvasData) {
|
|
||||||
throw new Error(projectResult.error || "结果已生成,但画布同步失败");
|
|
||||||
}
|
|
||||||
loadCanvas(projectResult.data.canvasData);
|
|
||||||
toast.success(`${capability.title}完成`);
|
toast.success(`${capability.title}完成`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message =
|
setToolbarFailure({
|
||||||
error instanceof Error ? error.message : `${capability.title}失败`;
|
imageUrl: data.src,
|
||||||
toast.error(message);
|
message: error instanceof Error ? error.message : `${capability.title}失败`,
|
||||||
|
});
|
||||||
} finally {
|
} finally {
|
||||||
setIsCreatingDetachedResultNode(false);
|
setIsCreatingDetachedResultNode(false);
|
||||||
setIsProcessing(false);
|
setIsProcessing(false);
|
||||||
@ -8304,7 +8232,6 @@ export function ImageNode({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 下载图片 - 服务端统一转为 Photoshop 兼容 PNG,避免后缀和真实格式错配。
|
|
||||||
const isDownloadingOriginalImage = Boolean(downloadProgress);
|
const isDownloadingOriginalImage = Boolean(downloadProgress);
|
||||||
const handleDownload = async () => {
|
const handleDownload = async () => {
|
||||||
if (!data.src) return;
|
if (!data.src) return;
|
||||||
@ -8318,24 +8245,16 @@ export function ImageNode({
|
|||||||
setDownloadProgress({
|
setDownloadProgress({
|
||||||
phase: "preparing",
|
phase: "preparing",
|
||||||
message: "正在准备原图...",
|
message: "正在准备原图...",
|
||||||
percent: 5,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await downloadCanvasImageAsPng({
|
await downloadOriginalImage({
|
||||||
url: data.src,
|
url: resolveOriginalImageUrl(data.src) || data.src,
|
||||||
name: displayTitle,
|
name: displayTitle,
|
||||||
candidates: [
|
|
||||||
displayImageSrc,
|
|
||||||
fullscreenPreviewSrc,
|
|
||||||
buildImageProxyUrl(resolveOriginalImageUrl(data.src) || data.src, {
|
|
||||||
delivery: "proxy",
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
onProgress: setDownloadProgress,
|
onProgress: setDownloadProgress,
|
||||||
});
|
});
|
||||||
toast.success("原图已开始下载");
|
toast.success("已开始下载");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (controller.signal.aborted) {
|
if (controller.signal.aborted) {
|
||||||
toast.message("已取消下载");
|
toast.message("已取消下载");
|
||||||
@ -8439,7 +8358,11 @@ export function ImageNode({
|
|||||||
{/* 图片内容 - 使用 object-contain 保持原始比例 */}
|
{/* 图片内容 - 使用 object-contain 保持原始比例 */}
|
||||||
{data.src ? (
|
{data.src ? (
|
||||||
<div className="relative h-full w-full">
|
<div className="relative h-full w-full">
|
||||||
{!shouldRequestDisplayImage ? (
|
{isRetiredStorageUrl(data.src) ? (
|
||||||
|
<div role="status" className="flex h-full w-full items-center justify-center bg-muted p-4 text-center text-sm text-muted-foreground">
|
||||||
|
{RETIRED_STORAGE_MESSAGE}
|
||||||
|
</div>
|
||||||
|
) : !shouldRequestDisplayImage ? (
|
||||||
<ImageNodeDeferredLoadState />
|
<ImageNodeDeferredLoadState />
|
||||||
) : isCompareMode && hasCompareSource ? (
|
) : isCompareMode && hasCompareSource ? (
|
||||||
<div
|
<div
|
||||||
@ -8466,15 +8389,16 @@ export function ImageNode({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="absolute inset-0 bg-slate-50 dark:bg-zinc-900" />
|
<div className="absolute inset-0 bg-slate-50 dark:bg-zinc-900" />
|
||||||
<img
|
<CanvasPreviewImage
|
||||||
|
sourceKey={data.src}
|
||||||
alt={displayTitle}
|
alt={displayTitle}
|
||||||
className="absolute inset-0 h-full w-full object-cover"
|
className="absolute inset-0 h-full w-full object-cover"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
draggable={false}
|
draggable={false}
|
||||||
loading="lazy"
|
loading="eager"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== data.src) {
|
if (event.currentTarget.src !== buildImageFallbackUrl(data.src)) {
|
||||||
event.currentTarget.src = data.src;
|
event.currentTarget.src = buildImageFallbackUrl(data.src);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onLoad={handleDisplayImageLoad}
|
onLoad={handleDisplayImageLoad}
|
||||||
@ -8495,15 +8419,16 @@ export function ImageNode({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="absolute inset-0 bg-slate-50 dark:bg-zinc-900" />
|
<div className="absolute inset-0 bg-slate-50 dark:bg-zinc-900" />
|
||||||
<img
|
<CanvasPreviewImage
|
||||||
|
sourceKey={compareSource}
|
||||||
alt={`${displayTitle}-原图`}
|
alt={`${displayTitle}-原图`}
|
||||||
className="absolute inset-0 h-full w-full object-cover"
|
className="absolute inset-0 h-full w-full object-cover"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
draggable={false}
|
draggable={false}
|
||||||
loading="lazy"
|
loading="eager"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== compareSource) {
|
if (event.currentTarget.src !== buildImageFallbackUrl(compareSource)) {
|
||||||
event.currentTarget.src = compareSource;
|
event.currentTarget.src = buildImageFallbackUrl(compareSource);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
src={displayCompareSrc}
|
src={displayCompareSrc}
|
||||||
@ -8545,7 +8470,8 @@ export function ImageNode({
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<img
|
<CanvasPreviewImage
|
||||||
|
sourceKey={data.src}
|
||||||
alt={displayTitle}
|
alt={displayTitle}
|
||||||
className={cn(
|
className={cn(
|
||||||
"h-full w-full bg-slate-50 object-contain dark:bg-zinc-900",
|
"h-full w-full bg-slate-50 object-contain dark:bg-zinc-900",
|
||||||
@ -8557,7 +8483,7 @@ export function ImageNode({
|
|||||||
)}
|
)}
|
||||||
decoding="async"
|
decoding="async"
|
||||||
draggable={false}
|
draggable={false}
|
||||||
loading="lazy"
|
loading="eager"
|
||||||
onClick={
|
onClick={
|
||||||
isThisNodePicking && !boxCutoutMode
|
isThisNodePicking && !boxCutoutMode
|
||||||
? handlePickerClick
|
? handlePickerClick
|
||||||
@ -8566,12 +8492,17 @@ export function ImageNode({
|
|||||||
onDoubleClick={(e) => {
|
onDoubleClick={(e) => {
|
||||||
if (!isThisNodePicking && !boxCutoutMode && !localEditMode) {
|
if (!isThisNodePicking && !boxCutoutMode && !localEditMode) {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
setFullscreenPreviewImage({
|
||||||
|
src: e.currentTarget.currentSrc || e.currentTarget.src,
|
||||||
|
width: e.currentTarget.naturalWidth,
|
||||||
|
height: e.currentTarget.naturalHeight,
|
||||||
|
});
|
||||||
setFullscreenPreviewOpen(true);
|
setFullscreenPreviewOpen(true);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== data.src) {
|
if (event.currentTarget.src !== buildImageFallbackUrl(data.src)) {
|
||||||
event.currentTarget.src = data.src;
|
event.currentTarget.src = buildImageFallbackUrl(data.src);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onLoad={handleDisplayImageLoad}
|
onLoad={handleDisplayImageLoad}
|
||||||
@ -9482,6 +9413,8 @@ export function ImageNode({
|
|||||||
) : (
|
) : (
|
||||||
<ImageNodeEmptyState
|
<ImageNodeEmptyState
|
||||||
detailProgressLabel={detailProgressLabel}
|
detailProgressLabel={detailProgressLabel}
|
||||||
|
failure={data.metadata?.generationFailure}
|
||||||
|
requestId={data.metadata?.generationRequestId}
|
||||||
fileInputRef={fileInputRef}
|
fileInputRef={fileInputRef}
|
||||||
generationProgress={generationProgress}
|
generationProgress={generationProgress}
|
||||||
onImageUpload={handleImageUpload}
|
onImageUpload={handleImageUpload}
|
||||||
@ -9502,6 +9435,29 @@ export function ImageNode({
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{!isProcessing && toolbarFailure && toolbarFailure.imageUrl === data.src && (
|
||||||
|
<div
|
||||||
|
className="absolute inset-x-2 bottom-2 z-20 flex max-h-[70%] items-start gap-2 overflow-auto rounded-lg border border-red-200 bg-white px-3 py-2 text-xs text-red-800 shadow-sm dark:border-red-900 dark:bg-zinc-900 dark:text-red-200"
|
||||||
|
data-canvas-no-drag="true"
|
||||||
|
onPointerDown={(event) => event.stopPropagation()}
|
||||||
|
>
|
||||||
|
<p className="min-w-0 flex-1 select-text whitespace-pre-wrap break-words" role="alert">
|
||||||
|
{toolbarFailure.message}
|
||||||
|
</p>
|
||||||
|
<button
|
||||||
|
aria-label="关闭任务提示"
|
||||||
|
className="shrink-0 rounded p-1 hover:bg-red-100 dark:hover:bg-red-950"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
setToolbarFailure(null);
|
||||||
|
}}
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
<X className="h-3 w-3" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* 工具栏使用 Portal 渲染到 body,避免被裁剪 */}
|
{/* 工具栏使用 Portal 渲染到 body,避免被裁剪 */}
|
||||||
|
|
||||||
{/* 悬浮时的更多操作按钮 (右上角) */}
|
{/* 悬浮时的更多操作按钮 (右上角) */}
|
||||||
@ -11971,6 +11927,7 @@ export function ImageNode({
|
|||||||
{/* 双击放大预览弹窗 */}
|
{/* 双击放大预览弹窗 */}
|
||||||
{fullscreenPreviewOpen &&
|
{fullscreenPreviewOpen &&
|
||||||
data.src &&
|
data.src &&
|
||||||
|
fullscreenPreviewImage &&
|
||||||
createPortal(
|
createPortal(
|
||||||
<div
|
<div
|
||||||
className="fixed inset-0 z-[9999] flex cursor-zoom-out items-center justify-center bg-black/90"
|
className="fixed inset-0 z-[9999] flex cursor-zoom-out items-center justify-center bg-black/90"
|
||||||
@ -11978,7 +11935,7 @@ export function ImageNode({
|
|||||||
>
|
>
|
||||||
{/* 关闭按钮 */}
|
{/* 关闭按钮 */}
|
||||||
<button
|
<button
|
||||||
className="absolute top-4 right-4 rounded-full bg-slate-950 p-2 transition-colors hover:bg-black"
|
className="absolute top-4 right-4 z-10 rounded-full bg-slate-950 p-2 transition-colors hover:bg-black"
|
||||||
onClick={() => setFullscreenPreviewOpen(false)}
|
onClick={() => setFullscreenPreviewOpen(false)}
|
||||||
>
|
>
|
||||||
<svg
|
<svg
|
||||||
@ -11996,7 +11953,7 @@ export function ImageNode({
|
|||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
className="absolute top-4 right-16 inline-flex items-center gap-1.5 rounded-full bg-slate-950 px-3 py-2 text-sm text-white transition-colors hover:bg-black disabled:cursor-not-allowed disabled:opacity-60"
|
className="absolute top-4 right-16 z-10 inline-flex items-center gap-1.5 rounded-full bg-slate-950 px-3 py-2 text-sm text-white transition-colors hover:bg-black disabled:cursor-not-allowed disabled:opacity-60"
|
||||||
disabled={isDownloadingOriginalImage}
|
disabled={isDownloadingOriginalImage}
|
||||||
onClick={(event) => {
|
onClick={(event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@ -12015,13 +11972,11 @@ export function ImageNode({
|
|||||||
</button>
|
</button>
|
||||||
|
|
||||||
{/* 图片 */}
|
{/* 图片 */}
|
||||||
<img
|
<ProgressiveImagePreview
|
||||||
alt={data.prompt || "图片预览"}
|
alt={data.prompt || "图片预览"}
|
||||||
className="max-h-[95vh] max-w-[95vw] object-contain"
|
className="max-h-[95vh] max-w-[95vw] object-contain"
|
||||||
decoding="async"
|
src={resolveOriginalImageUrl(data.src) || data.src}
|
||||||
draggable={false}
|
preview={fullscreenPreviewImage}
|
||||||
onClick={(e) => e.stopPropagation()}
|
|
||||||
src={fullscreenPreviewSrc}
|
|
||||||
style={activeImageAdjustmentsStyle}
|
style={activeImageAdjustmentsStyle}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
32
components/canvas/nodes/image-node/canvas-preview-image.tsx
Normal file
32
components/canvas/nodes/image-node/canvas-preview-image.tsx
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { forwardRef, type ImgHTMLAttributes, useState } from "react";
|
||||||
|
|
||||||
|
type Props = Omit<ImgHTMLAttributes<HTMLImageElement>, "src"> & { src: string; sourceKey: string };
|
||||||
|
|
||||||
|
export const CanvasPreviewImage = forwardRef<HTMLImageElement, Props>(function CanvasPreviewImage(
|
||||||
|
{ src, sourceKey, onLoad, style, ...props }, ref,
|
||||||
|
) {
|
||||||
|
const [displayed, setDisplayed] = useState({ sourceKey, src });
|
||||||
|
const visibleSrc = displayed.sourceKey === sourceKey ? displayed.src : src;
|
||||||
|
const sources = visibleSrc === src ? [src] : [visibleSrc, src];
|
||||||
|
return <>{sources.map(candidate => {
|
||||||
|
const visible = candidate === visibleSrc;
|
||||||
|
return <img
|
||||||
|
{...props}
|
||||||
|
key={candidate}
|
||||||
|
ref={visible ? ref : undefined}
|
||||||
|
src={candidate}
|
||||||
|
alt={visible ? props.alt : ""}
|
||||||
|
aria-hidden={visible ? props["aria-hidden"] : true}
|
||||||
|
style={visible ? style : { ...style, position: "absolute", inset: 0, opacity: 0, pointerEvents: "none" }}
|
||||||
|
onLoad={event => {
|
||||||
|
const image = event.currentTarget;
|
||||||
|
onLoad?.(event);
|
||||||
|
void image.decode().then(() => {
|
||||||
|
if (image.isConnected && candidate === src) setDisplayed({ sourceKey, src: candidate });
|
||||||
|
}).catch(() => undefined);
|
||||||
|
}}
|
||||||
|
/>;
|
||||||
|
})}</>;
|
||||||
|
});
|
||||||
@ -3,11 +3,14 @@
|
|||||||
import { AlertCircle, ImagePlus, Loader2 } from "lucide-react";
|
import { AlertCircle, ImagePlus, Loader2 } from "lucide-react";
|
||||||
import type React from "react";
|
import type React from "react";
|
||||||
import { GenerationDotPlaceholder } from "@/components/generation-dot-placeholder";
|
import { GenerationDotPlaceholder } from "@/components/generation-dot-placeholder";
|
||||||
|
import type { RequestFailureDetails } from "@/lib/request-error";
|
||||||
|
|
||||||
interface ImageNodeEmptyStateProps {
|
interface ImageNodeEmptyStateProps {
|
||||||
detailProgressLabel?: string;
|
detailProgressLabel?: string;
|
||||||
fileInputRef: React.Ref<HTMLInputElement>;
|
fileInputRef: React.Ref<HTMLInputElement>;
|
||||||
generationProgress?: number | null;
|
generationProgress?: number | null;
|
||||||
|
failure?: RequestFailureDetails;
|
||||||
|
requestId?: string;
|
||||||
onImageUpload: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
onImageUpload: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
||||||
onSelectFile: (event: React.MouseEvent<HTMLButtonElement>) => void;
|
onSelectFile: (event: React.MouseEvent<HTMLButtonElement>) => void;
|
||||||
status?: string;
|
status?: string;
|
||||||
@ -52,6 +55,8 @@ export function ImageNodeEmptyState({
|
|||||||
detailProgressLabel,
|
detailProgressLabel,
|
||||||
fileInputRef,
|
fileInputRef,
|
||||||
generationProgress,
|
generationProgress,
|
||||||
|
failure,
|
||||||
|
requestId,
|
||||||
onImageUpload,
|
onImageUpload,
|
||||||
onSelectFile,
|
onSelectFile,
|
||||||
status,
|
status,
|
||||||
@ -89,7 +94,7 @@ export function ImageNodeEmptyState({
|
|||||||
if (status === "failed") {
|
if (status === "failed") {
|
||||||
const failedTitle = detailProgressLabel || "生成失败";
|
const failedTitle = detailProgressLabel || "生成失败";
|
||||||
return (
|
return (
|
||||||
<div className="flex h-full w-full select-none flex-col items-center justify-center bg-[#fff7f8] px-6 text-center dark:bg-red-950/40">
|
<div role="alert" className="flex h-full w-full flex-col items-center justify-center overflow-auto bg-[#fff7f8] px-6 text-center dark:bg-red-950/40">
|
||||||
<span className="mb-3 flex h-12 w-12 items-center justify-center rounded-[18px] border border-[#f2d7dd] bg-white text-[#d24f63] shadow-[0_0_0_1px_rgba(242,215,221,0.22)]">
|
<span className="mb-3 flex h-12 w-12 items-center justify-center rounded-[18px] border border-[#f2d7dd] bg-white text-[#d24f63] shadow-[0_0_0_1px_rgba(242,215,221,0.22)]">
|
||||||
<AlertCircle className="h-5 w-5" />
|
<AlertCircle className="h-5 w-5" />
|
||||||
</span>
|
</span>
|
||||||
@ -97,8 +102,9 @@ export function ImageNodeEmptyState({
|
|||||||
{failedTitle}
|
{failedTitle}
|
||||||
</span>
|
</span>
|
||||||
<span className="mt-1 text-xs text-[#b77480]">
|
<span className="mt-1 text-xs text-[#b77480]">
|
||||||
{failedTitle.includes("上传") ? "请重新选择文件" : "请重新上传或再试一次"}
|
{failure?.recommendation || (failedTitle.includes("上传") ? "请重新选择文件" : "请查看对话中的错误原因和处理建议")}
|
||||||
</span>
|
</span>
|
||||||
|
{requestId ? <span className="mt-2 select-text break-all text-xs text-[#b77480]">请求编号:{requestId}</span> : null}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,10 @@ import {
|
|||||||
splitOpenClawChartBlocks,
|
splitOpenClawChartBlocks,
|
||||||
} from "@/components/elements/openclaw-chart-card";
|
} from "@/components/elements/openclaw-chart-card";
|
||||||
import { ResponsePdfDownloadButton } from "@/components/elements/response-pdf-download";
|
import { ResponsePdfDownloadButton } from "@/components/elements/response-pdf-download";
|
||||||
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
import { buildImageFallbackUrl, buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
|
import { splitMarkdownImages } from "@/lib/canvas/markdown-image-segments";
|
||||||
|
import { stableStreamingMarkdown } from "@/lib/canvas/markdown-image-streaming";
|
||||||
|
import { getGatewayErrorPageMessage } from "@/lib/request-error";
|
||||||
import {
|
import {
|
||||||
Collapsible,
|
Collapsible,
|
||||||
CollapsibleContent,
|
CollapsibleContent,
|
||||||
@ -26,6 +29,7 @@ import { cn } from "@/lib/utils";
|
|||||||
|
|
||||||
type ResponseProps = ComponentProps<typeof Streamdown> & {
|
type ResponseProps = ComponentProps<typeof Streamdown> & {
|
||||||
collapsibleImageAnalysis?: boolean;
|
collapsibleImageAnalysis?: boolean;
|
||||||
|
isMarkdownStreaming?: boolean;
|
||||||
enablePdfDownload?: boolean;
|
enablePdfDownload?: boolean;
|
||||||
pdfFileName?: string;
|
pdfFileName?: string;
|
||||||
pdfSiteLogo?: string | null;
|
pdfSiteLogo?: string | null;
|
||||||
@ -362,43 +366,6 @@ function normalizeMarkdownContent(value: string) {
|
|||||||
.trim();
|
.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
function splitMarkdownImages(content: string) {
|
|
||||||
const segments: Array<
|
|
||||||
| { type: "text"; value: string }
|
|
||||||
| { type: "image"; alt?: string; src: string }
|
|
||||||
> = [];
|
|
||||||
|
|
||||||
const imageRegex = /!\[([^\]]*)\]\((https?:\/\/[^\s)]+)\)/g;
|
|
||||||
let lastIndex = 0;
|
|
||||||
let match: RegExpExecArray | null;
|
|
||||||
|
|
||||||
while ((match = imageRegex.exec(content)) !== null) {
|
|
||||||
if (match.index > lastIndex) {
|
|
||||||
segments.push({
|
|
||||||
type: "text",
|
|
||||||
value: content.slice(lastIndex, match.index),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
segments.push({
|
|
||||||
type: "image",
|
|
||||||
alt: match[1],
|
|
||||||
src: match[2],
|
|
||||||
});
|
|
||||||
|
|
||||||
lastIndex = imageRegex.lastIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lastIndex < content.length) {
|
|
||||||
segments.push({
|
|
||||||
type: "text",
|
|
||||||
value: content.slice(lastIndex),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return segments;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isLikelyImageUrl(value?: string | null) {
|
function isLikelyImageUrl(value?: string | null) {
|
||||||
return IMAGE_URL_REGEX.test(String(value || "").trim());
|
return IMAGE_URL_REGEX.test(String(value || "").trim());
|
||||||
}
|
}
|
||||||
@ -475,8 +442,8 @@ function MarkdownTableImagePreview({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== src) {
|
if (event.currentTarget.src !== buildImageFallbackUrl(src)) {
|
||||||
event.currentTarget.src = src;
|
event.currentTarget.src = buildImageFallbackUrl(src);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -506,8 +473,8 @@ function MarkdownTableImagePreview({
|
|||||||
decoding="async"
|
decoding="async"
|
||||||
onClick={(event) => event.stopPropagation()}
|
onClick={(event) => event.stopPropagation()}
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== originalSrc) {
|
if (event.currentTarget.src !== buildImageFallbackUrl(originalSrc)) {
|
||||||
event.currentTarget.src = originalSrc;
|
event.currentTarget.src = buildImageFallbackUrl(originalSrc);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -797,6 +764,7 @@ function ResponseReportTableDownloadButton({
|
|||||||
export const Response = ({
|
export const Response = ({
|
||||||
className,
|
className,
|
||||||
collapsibleImageAnalysis = false,
|
collapsibleImageAnalysis = false,
|
||||||
|
isMarkdownStreaming = false,
|
||||||
enablePdfDownload = false,
|
enablePdfDownload = false,
|
||||||
pdfFileName,
|
pdfFileName,
|
||||||
pdfSiteLogo,
|
pdfSiteLogo,
|
||||||
@ -1061,7 +1029,10 @@ export const Response = ({
|
|||||||
|
|
||||||
const rawText =
|
const rawText =
|
||||||
typeof props.children === "string"
|
typeof props.children === "string"
|
||||||
? normalizeMarkdownContent(props.children)
|
? normalizeMarkdownContent(stableStreamingMarkdown(
|
||||||
|
getGatewayErrorPageMessage(props.children) || props.children,
|
||||||
|
isMarkdownStreaming
|
||||||
|
))
|
||||||
: null;
|
: null;
|
||||||
const shouldShowPdfDownload =
|
const shouldShowPdfDownload =
|
||||||
Boolean(enablePdfDownload && rawText && hasDownloadableReportContent(rawText));
|
Boolean(enablePdfDownload && rawText && hasDownloadableReportContent(rawText));
|
||||||
@ -1091,7 +1062,7 @@ export const Response = ({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (rawText) {
|
if (rawText !== null) {
|
||||||
const analysisSection = collapsibleImageAnalysis
|
const analysisSection = collapsibleImageAnalysis
|
||||||
? extractImageAnalysisSection(rawText)
|
? extractImageAnalysisSection(rawText)
|
||||||
: null;
|
: null;
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useMemo, useState } from "react";
|
|
||||||
import { BloubAvatar } from "@/components/bloub-avatar";
|
import { BloubAvatar } from "@/components/bloub-avatar";
|
||||||
import type { StateId } from "@/lib/bloub-official/states";
|
import type { StateId } from "@/lib/bloub-official/states";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
@ -26,193 +25,17 @@ interface GenerationDotPlaceholderProps {
|
|||||||
const PHASE_TITLES: Record<GenerationPhase, string> = {
|
const PHASE_TITLES: Record<GenerationPhase, string> = {
|
||||||
idle: "正在创建图片",
|
idle: "正在创建图片",
|
||||||
queued: "等待开始生成",
|
queued: "等待开始生成",
|
||||||
processing: "生成初稿中",
|
processing: "等待模型返回图片",
|
||||||
finalizing: "正在润饰细节",
|
finalizing: "正在保存生成结果",
|
||||||
failed: "生成失败",
|
failed: "生成失败",
|
||||||
completed: "生成完成",
|
completed: "生成完成",
|
||||||
};
|
};
|
||||||
|
|
||||||
const GENERATION_STATUS_TITLES = [
|
|
||||||
"正在理解画面关系",
|
|
||||||
"正在读取参考图片",
|
|
||||||
"正在分析主体轮廓",
|
|
||||||
"正在识别关键元素",
|
|
||||||
"正在整理构图线索",
|
|
||||||
"正在确认画面比例",
|
|
||||||
"正在匹配视觉风格",
|
|
||||||
"正在提取材质细节",
|
|
||||||
"正在梳理光影方向",
|
|
||||||
"正在构建初始画面",
|
|
||||||
"正在生成第一版构图",
|
|
||||||
"正在安排主体位置",
|
|
||||||
"正在校准画面重心",
|
|
||||||
"正在补全背景层次",
|
|
||||||
"正在保持主体一致",
|
|
||||||
"正在优化边缘结构",
|
|
||||||
"正在调整透视关系",
|
|
||||||
"正在细化空间层次",
|
|
||||||
"正在增强画面清晰度",
|
|
||||||
"正在统一色彩倾向",
|
|
||||||
"正在检查参考一致性",
|
|
||||||
"正在处理局部细节",
|
|
||||||
"正在补充纹理信息",
|
|
||||||
"正在修正不自然区域",
|
|
||||||
"正在平衡明暗对比",
|
|
||||||
"正在优化产品质感",
|
|
||||||
"正在调整阴影落点",
|
|
||||||
"正在整理背景干扰",
|
|
||||||
"正在增强真实感",
|
|
||||||
"正在重绘细小结构",
|
|
||||||
"正在合成高清结果",
|
|
||||||
"正在检查主体完整性",
|
|
||||||
"正在优化画面边缘",
|
|
||||||
"正在处理反光细节",
|
|
||||||
"正在提升材质表现",
|
|
||||||
"正在微调构图节奏",
|
|
||||||
"正在清理多余噪点",
|
|
||||||
"正在强化视觉焦点",
|
|
||||||
"正在校正颜色偏差",
|
|
||||||
"正在保留参考特征",
|
|
||||||
"正在生成高分辨率版本",
|
|
||||||
"正在压实画面层次",
|
|
||||||
"正在修饰局部瑕疵",
|
|
||||||
"正在检查文字与图形",
|
|
||||||
"正在稳定画面结构",
|
|
||||||
"正在优化细节密度",
|
|
||||||
"正在完善背景过渡",
|
|
||||||
"正在增强主体质感",
|
|
||||||
"正在统一整体光感",
|
|
||||||
"正在检查比例关系",
|
|
||||||
"正在修正边缘锯齿",
|
|
||||||
"正在提升清晰观感",
|
|
||||||
"正在处理最后细节",
|
|
||||||
"正在准备预览结果",
|
|
||||||
"正在同步生成进度",
|
|
||||||
"正在确认输出尺寸",
|
|
||||||
"正在整理结果文件",
|
|
||||||
"正在校验图片可用性",
|
|
||||||
"正在生成最终预览",
|
|
||||||
"正在完成高清渲染",
|
|
||||||
"正在保存生成结果",
|
|
||||||
"正在写入画布节点",
|
|
||||||
"正在同步到对话框",
|
|
||||||
"正在检查结果完整性",
|
|
||||||
"正在收尾生成任务",
|
|
||||||
"正在等待服务商返回",
|
|
||||||
"正在保持连接状态",
|
|
||||||
"正在确认生成结果",
|
|
||||||
"正在拉取图片地址",
|
|
||||||
"正在准备展示图片",
|
|
||||||
"正在完成最后处理",
|
|
||||||
"正在刷新画布内容",
|
|
||||||
"正在整理图片信息",
|
|
||||||
"正在完成本次生成",
|
|
||||||
"正在即将展示结果",
|
|
||||||
];
|
|
||||||
|
|
||||||
const LONG_WAIT_TITLE_THRESHOLD_MS = 5 * 60 * 1000;
|
|
||||||
const LONG_WAIT_STATUS_TITLES = [
|
|
||||||
"服务繁忙,排队处理中",
|
|
||||||
"高峰期生成较慢,请耐心等候",
|
|
||||||
"仍在保持连接,等待模型返回",
|
|
||||||
"结果返回后会自动展示",
|
|
||||||
];
|
|
||||||
const STATUS_TITLE_DURATIONS_MS = GENERATION_STATUS_TITLES.map((_, index) => {
|
|
||||||
const rhythm = [3600, 5200, 4100, 6100, 3300, 4700, 5700, 3900, 6800, 4400];
|
|
||||||
return rhythm[index % rhythm.length];
|
|
||||||
});
|
|
||||||
const TYPEWRITER_START_DELAY_MS = 120;
|
|
||||||
const TYPEWRITER_BASE_STEP_MS = 46;
|
|
||||||
|
|
||||||
function clampProgress(value: number) {
|
function clampProgress(value: number) {
|
||||||
if (!Number.isFinite(value)) return 0;
|
if (!Number.isFinite(value)) return 0;
|
||||||
return Math.max(0, Math.min(100, Math.round(value)));
|
return Math.max(0, Math.min(100, Math.round(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
function estimateProgress(phase: GenerationPhase, elapsedMs: number) {
|
|
||||||
if (phase === "completed" || phase === "failed") return 100;
|
|
||||||
if (phase === "finalizing") return 96;
|
|
||||||
if (phase === "queued") {
|
|
||||||
return clampProgress(6 + Math.min(12, elapsedMs / 3500));
|
|
||||||
}
|
|
||||||
if (phase === "processing") {
|
|
||||||
const eased = 1 - Math.exp(-elapsedMs / 85_000);
|
|
||||||
return clampProgress(18 + eased * 78);
|
|
||||||
}
|
|
||||||
return clampProgress(Math.min(8, elapsedMs / 1000));
|
|
||||||
}
|
|
||||||
|
|
||||||
function getStatusTitleState(elapsedMs: number, initialTitle?: string) {
|
|
||||||
const titles = initialTitle
|
|
||||||
? [
|
|
||||||
initialTitle,
|
|
||||||
...GENERATION_STATUS_TITLES.filter((item) => item !== initialTitle),
|
|
||||||
]
|
|
||||||
: GENERATION_STATUS_TITLES;
|
|
||||||
const durations = initialTitle
|
|
||||||
? [4200, ...STATUS_TITLE_DURATIONS_MS]
|
|
||||||
: STATUS_TITLE_DURATIONS_MS;
|
|
||||||
|
|
||||||
let cursor = Math.max(0, elapsedMs);
|
|
||||||
for (let index = 0; index < titles.length; index += 1) {
|
|
||||||
const duration = durations[index] || 4600;
|
|
||||||
if (cursor < duration) {
|
|
||||||
return {
|
|
||||||
elapsedInTitle: cursor,
|
|
||||||
index,
|
|
||||||
title:
|
|
||||||
titles[index] ||
|
|
||||||
titles[titles.length - 1] ||
|
|
||||||
PHASE_TITLES.processing,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
cursor -= duration;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
elapsedInTitle: durations[durations.length - 1] || 4600,
|
|
||||||
index: titles.length - 1,
|
|
||||||
title: titles[titles.length - 1] || PHASE_TITLES.processing,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLongWaitTitleState(elapsedMs: number, initialTitle?: string) {
|
|
||||||
const titles = initialTitle
|
|
||||||
? [
|
|
||||||
initialTitle,
|
|
||||||
...LONG_WAIT_STATUS_TITLES.filter((item) => item !== initialTitle),
|
|
||||||
]
|
|
||||||
: LONG_WAIT_STATUS_TITLES;
|
|
||||||
const elapsedAfterThreshold = Math.max(
|
|
||||||
0,
|
|
||||||
elapsedMs - LONG_WAIT_TITLE_THRESHOLD_MS
|
|
||||||
);
|
|
||||||
const duration = 7600;
|
|
||||||
const index = Math.floor(elapsedAfterThreshold / duration) % titles.length;
|
|
||||||
|
|
||||||
return {
|
|
||||||
elapsedInTitle: elapsedAfterThreshold % duration,
|
|
||||||
index,
|
|
||||||
title: titles[index] || titles[0] || PHASE_TITLES.processing,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTypewriterText(text: string, elapsedInTitle: number, index: number) {
|
|
||||||
const chars = Array.from(text);
|
|
||||||
if (chars.length === 0) return "";
|
|
||||||
const step = TYPEWRITER_BASE_STEP_MS + (index % 5) * 8;
|
|
||||||
const visibleCount = Math.max(
|
|
||||||
1,
|
|
||||||
Math.min(
|
|
||||||
chars.length,
|
|
||||||
Math.floor(
|
|
||||||
Math.max(0, elapsedInTitle - TYPEWRITER_START_DELAY_MS) / step
|
|
||||||
) + 1
|
|
||||||
)
|
|
||||||
);
|
|
||||||
return chars.slice(0, visibleCount).join("");
|
|
||||||
}
|
|
||||||
|
|
||||||
function getBloubState(phase: GenerationPhase): StateId {
|
function getBloubState(phase: GenerationPhase): StateId {
|
||||||
if (phase === "failed") return "alert";
|
if (phase === "failed") return "alert";
|
||||||
if (phase === "completed") return "wink";
|
if (phase === "completed") return "wink";
|
||||||
@ -228,39 +51,10 @@ export function GenerationDotPlaceholder({
|
|||||||
showProgress = true,
|
showProgress = true,
|
||||||
title,
|
title,
|
||||||
}: GenerationDotPlaceholderProps) {
|
}: GenerationDotPlaceholderProps) {
|
||||||
const [elapsedMs, setElapsedMs] = useState(0);
|
const resolvedProgress = typeof progress === "number" && Number.isFinite(progress)
|
||||||
|
? clampProgress(progress) : null;
|
||||||
useEffect(() => {
|
const resolvedTitle = title || PHASE_TITLES[phase];
|
||||||
const startedAt = Date.now();
|
|
||||||
const timer = window.setInterval(() => {
|
|
||||||
setElapsedMs(Date.now() - startedAt);
|
|
||||||
}, 90);
|
|
||||||
return () => window.clearInterval(timer);
|
|
||||||
}, [phase]);
|
|
||||||
|
|
||||||
const resolvedProgress = clampProgress(
|
|
||||||
typeof progress === "number" ? progress : estimateProgress(phase, elapsedMs)
|
|
||||||
);
|
|
||||||
const isLongWaitTitle =
|
|
||||||
(phase === "processing" || phase === "finalizing") &&
|
|
||||||
elapsedMs >= LONG_WAIT_TITLE_THRESHOLD_MS;
|
|
||||||
const statusTitleState = isLongWaitTitle
|
|
||||||
? getLongWaitTitleState(elapsedMs, title)
|
|
||||||
: getStatusTitleState(elapsedMs, title);
|
|
||||||
const rotatingTitle = getTypewriterText(
|
|
||||||
statusTitleState.title,
|
|
||||||
statusTitleState.elapsedInTitle,
|
|
||||||
statusTitleState.index
|
|
||||||
);
|
|
||||||
const resolvedTitle =
|
|
||||||
phase === "processing" || phase === "finalizing"
|
|
||||||
? rotatingTitle
|
|
||||||
: title || PHASE_TITLES[phase];
|
|
||||||
const playSequence = phase === "processing" || phase === "finalizing";
|
const playSequence = phase === "processing" || phase === "finalizing";
|
||||||
const progressStyle = useMemo(
|
|
||||||
() => ({ width: `${resolvedProgress}%` }),
|
|
||||||
[resolvedProgress]
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@ -281,10 +75,6 @@ export function GenerationDotPlaceholder({
|
|||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{resolvedTitle}
|
{resolvedTitle}
|
||||||
<span
|
|
||||||
aria-hidden="true"
|
|
||||||
className="ml-1 inline-block h-[1em] w-[0.08em] translate-y-[0.15em] animate-[generationCursorBlink_1s_steps(1,end)_infinite] bg-current"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex min-h-0 flex-1 items-center justify-center py-3">
|
<div className="flex min-h-0 flex-1 items-center justify-center py-3">
|
||||||
@ -297,13 +87,13 @@ export function GenerationDotPlaceholder({
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{showProgress ? (
|
{showProgress && phase !== "completed" ? (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<div className="flex items-center justify-between text-[12px] font-medium text-[#7d838b] dark:text-zinc-400">
|
<div className="flex items-center justify-between text-[12px] font-medium text-[#7d838b] dark:text-zinc-400">
|
||||||
<span>{phase === "failed" ? "已停止" : "生成进度"}</span>
|
<span>{phase === "failed" ? "已停止" : resolvedProgress === null ? "等待结果,返回后自动展示" : "生成进度"}</span>
|
||||||
<span>{resolvedProgress}%</span>
|
{resolvedProgress !== null ? <span>{resolvedProgress}%</span> : null}
|
||||||
</div>
|
</div>
|
||||||
<div className="h-1.5 overflow-hidden rounded-full bg-white/80 shadow-[inset_0_0_0_1px_rgba(0,0,0,0.035)] dark:bg-white/10 dark:shadow-none">
|
{resolvedProgress !== null ? <div role="progressbar" aria-valuenow={resolvedProgress} aria-valuemin={0} aria-valuemax={100} className="h-1.5 overflow-hidden rounded-full bg-white/80 shadow-[inset_0_0_0_1px_rgba(0,0,0,0.035)] dark:bg-white/10 dark:shadow-none">
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"h-full rounded-full transition-[width] duration-700 ease-out",
|
"h-full rounded-full transition-[width] duration-700 ease-out",
|
||||||
@ -311,25 +101,12 @@ export function GenerationDotPlaceholder({
|
|||||||
? "bg-[#c7ccd3] dark:bg-zinc-600"
|
? "bg-[#c7ccd3] dark:bg-zinc-600"
|
||||||
: "bg-[#0a0a0c] dark:bg-[#3ecf8e]"
|
: "bg-[#0a0a0c] dark:bg-[#3ecf8e]"
|
||||||
)}
|
)}
|
||||||
style={progressStyle}
|
style={{ width: `${resolvedProgress}%` }}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div> : null}
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style jsx>{`
|
|
||||||
@keyframes generationCursorBlink {
|
|
||||||
0%,
|
|
||||||
48% {
|
|
||||||
opacity: 1;
|
|
||||||
}
|
|
||||||
49%,
|
|
||||||
100% {
|
|
||||||
opacity: 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`}</style>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
68
components/home/lovart/home-image.tsx
Normal file
68
components/home/lovart/home-image.tsx
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState, type ReactNode } from "react";
|
||||||
|
import { isRetiredStorageUrl } from "@/lib/storage/retired-storage";
|
||||||
|
|
||||||
|
type HomeImageProps = {
|
||||||
|
sources: Array<string | null | undefined>;
|
||||||
|
alt: string;
|
||||||
|
className?: string;
|
||||||
|
imageClassName?: string;
|
||||||
|
fallback?: ReactNode;
|
||||||
|
loading?: "lazy" | "eager";
|
||||||
|
onUnavailable?: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
function imageCandidates(source: string) {
|
||||||
|
// These two domains are documented aliases for the same gongxiangpan store.
|
||||||
|
// Do not rewrite other buckets, providers, signed URLs or user-supplied hosts.
|
||||||
|
try {
|
||||||
|
const url = new URL(source);
|
||||||
|
if (url.origin === "https://s3.yydd.ai" &&
|
||||||
|
url.pathname.startsWith("/gongxiangpan/") && !url.search && !url.hash) {
|
||||||
|
return [`https://s3.jiansun.vip${url.pathname}`, source];
|
||||||
|
}
|
||||||
|
} catch { /* Relative site assets need no host rewrite. */ }
|
||||||
|
return [source];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Keep failed image elements out of view, trying each distinct source once. */
|
||||||
|
export function HomeImage(props: HomeImageProps) {
|
||||||
|
const sources = [...new Set(props.sources
|
||||||
|
.filter((url): url is string => Boolean(url?.trim()) && !isRetiredStorageUrl(url))
|
||||||
|
.flatMap(imageCandidates))];
|
||||||
|
return <HomeImageAttempt key={JSON.stringify(sources)} {...props} sources={sources} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function HomeImageAttempt({
|
||||||
|
sources, alt, className, imageClassName, fallback, loading = "lazy", onUnavailable,
|
||||||
|
}: Omit<HomeImageProps, "sources"> & { sources: string[] }) {
|
||||||
|
const [index, setIndex] = useState(0);
|
||||||
|
const [loaded, setLoaded] = useState(false);
|
||||||
|
const source = sources[index];
|
||||||
|
return (
|
||||||
|
<span className={`relative block overflow-hidden ${className || ""}`}>
|
||||||
|
{!loaded && (
|
||||||
|
<span className={fallback ? "block" : "block aspect-[4/5] rounded-[12px] bg-[#e8e8e8] dark:bg-[#1b1c20]"}>
|
||||||
|
{fallback}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{source ? (
|
||||||
|
<img
|
||||||
|
key={source}
|
||||||
|
src={source}
|
||||||
|
alt={alt}
|
||||||
|
loading={loading}
|
||||||
|
decoding="async"
|
||||||
|
className={`${imageClassName || ""} ${loaded ? "" : "absolute inset-0 opacity-0"}`}
|
||||||
|
onLoad={() => setLoaded(true)}
|
||||||
|
onError={() => {
|
||||||
|
setLoaded(false);
|
||||||
|
setIndex(index + 1);
|
||||||
|
if (index + 1 >= sources.length) onUnavailable?.();
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
import { ReferenceIcon } from "@/components/lovart/reference-icon";
|
import { ReferenceIcon } from "@/components/lovart/reference-icon";
|
||||||
import { Gem, Heart, Loader2, PlayCircle } from "lucide-react";
|
import { Gem, Heart, Loader2, PlayCircle } from "lucide-react";
|
||||||
import type { Ref } from "react";
|
import { useState, type Ref } from "react";
|
||||||
|
import { HomeImage } from "./home-image";
|
||||||
import type { FeaturedImage } from "./types";
|
import type { FeaturedImage } from "./types";
|
||||||
|
|
||||||
type LovartFeaturedGalleryProps = {
|
type LovartFeaturedGalleryProps = {
|
||||||
@ -30,6 +31,10 @@ function getFeaturedPreviewUrl(item: FeaturedImage) {
|
|||||||
return `${source}?imageView2/2/w/900/q/78`;
|
return `${source}?imageView2/2/w/900/q/78`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function imageIdentity(item: FeaturedImage) {
|
||||||
|
return JSON.stringify([item.id, item.thumbnailUrl, item.originalUrl]);
|
||||||
|
}
|
||||||
|
|
||||||
export function LovartFeaturedGallery({
|
export function LovartFeaturedGallery({
|
||||||
onOpenProjects,
|
onOpenProjects,
|
||||||
onOpenMaterials,
|
onOpenMaterials,
|
||||||
@ -42,6 +47,13 @@ export function LovartFeaturedGallery({
|
|||||||
isLoadingMoreFeatured,
|
isLoadingMoreFeatured,
|
||||||
setActiveCategory,
|
setActiveCategory,
|
||||||
}: LovartFeaturedGalleryProps) {
|
}: LovartFeaturedGalleryProps) {
|
||||||
|
const [unavailable, setUnavailable] = useState<Set<string>>(() => new Set());
|
||||||
|
const [retryVersion, setRetryVersion] = useState(0);
|
||||||
|
const uniqueItems = [...new Map(featuredItems.map((item) => [item.id, item])).values()];
|
||||||
|
const visibleItems = uniqueItems.filter((item) =>
|
||||||
|
Boolean(item.thumbnailUrl || item.originalUrl),
|
||||||
|
);
|
||||||
|
const unavailableCount = uniqueItems.filter(item => unavailable.has(imageIdentity(item))).length;
|
||||||
return (
|
return (
|
||||||
<section
|
<section
|
||||||
ref={featuredSectionRef}
|
ref={featuredSectionRef}
|
||||||
@ -88,44 +100,48 @@ export function LovartFeaturedGallery({
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{featuredItems.length === 0 ? (
|
{unavailableCount > 0 && (
|
||||||
|
<div role="status" className="mt-4 flex items-center gap-3 text-xs text-black/50 dark:text-white/50">
|
||||||
|
<span>{unavailableCount} 张灵感图片加载失败,请重试。</span>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="shrink-0 underline underline-offset-4 hover:text-black dark:hover:text-white"
|
||||||
|
onClick={() => {
|
||||||
|
setUnavailable(new Set());
|
||||||
|
setRetryVersion((version) => version + 1);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
重试加载
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{visibleItems.length === 0 ? (
|
||||||
<div className="mt-[22px] rounded-[10px] border border-dashed border-black/15 bg-[#f7f7f7] p-10 text-center text-[14px] text-black/35 dark:border-white/12 dark:bg-[#141416] dark:text-white/35">
|
<div className="mt-[22px] rounded-[10px] border border-dashed border-black/15 bg-[#f7f7f7] p-10 text-center text-[14px] text-black/35 dark:border-white/12 dark:bg-[#141416] dark:text-white/35">
|
||||||
{isLoadingFeatured
|
{isLoadingFeatured
|
||||||
? "灵感内容加载中..."
|
? "灵感内容加载中..."
|
||||||
: "当前没有可展示的灵感数据。"}
|
: unavailableCount > 0 ? "图片暂时无法加载,请稍后重试。" : "当前没有可展示的灵感数据。"}
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="mt-6 columns-1 gap-4 sm:columns-2 lg:columns-4">
|
<div className="mt-6 columns-1 gap-4 sm:columns-2 lg:columns-4">
|
||||||
{featuredItems.map((item, index) => (
|
{visibleItems.map((item) => (
|
||||||
<button
|
<button
|
||||||
key={item.id}
|
key={`${imageIdentity(item)}:${retryVersion}`}
|
||||||
type="button"
|
type="button"
|
||||||
|
disabled={unavailable.has(imageIdentity(item))}
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
window.open(item.originalUrl || item.thumbnailUrl, "_blank")
|
window.open(item.originalUrl || item.thumbnailUrl, "_blank")
|
||||||
}
|
}
|
||||||
className="group mb-[24px] block w-full cursor-pointer break-inside-avoid text-left"
|
className="group mb-[24px] block w-full cursor-pointer break-inside-avoid text-left"
|
||||||
>
|
>
|
||||||
<img
|
{unavailable.has(imageIdentity(item)) ? (
|
||||||
src={getFeaturedPreviewUrl(item)}
|
<div className="flex aspect-[4/5] items-center justify-center rounded-[12px] bg-muted p-4 text-sm text-muted-foreground">图片加载失败</div>
|
||||||
|
) : <HomeImage
|
||||||
|
sources={[getFeaturedPreviewUrl(item), item.thumbnailUrl, item.originalUrl]}
|
||||||
alt={item.prompt || "灵感图"}
|
alt={item.prompt || "灵感图"}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
onUnavailable={() => setUnavailable((previous) => new Set(previous).add(imageIdentity(item)))}
|
||||||
onError={(event) => {
|
imageClassName="block h-auto w-full rounded-[12px] bg-[#e8e8e8] object-cover transition-transform duration-150 ease-out will-change-transform group-hover:scale-[1.008] dark:bg-[#1b1c20]"
|
||||||
const image = event.currentTarget;
|
/>}
|
||||||
const fallbackUrl = item.originalUrl || item.thumbnailUrl;
|
|
||||||
|
|
||||||
if (
|
|
||||||
!fallbackUrl ||
|
|
||||||
image.dataset.fallbackApplied === "true"
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
image.dataset.fallbackApplied = "true";
|
|
||||||
image.src = fallbackUrl;
|
|
||||||
}}
|
|
||||||
className="block h-auto w-full rounded-[12px] bg-[#e8e8e8] object-cover transition-transform duration-150 ease-out will-change-transform group-hover:scale-[1.008] dark:bg-[#1b1c20]"
|
|
||||||
/>
|
|
||||||
<div className="mt-[8px] flex h-[22px] items-center justify-between text-[11px] font-normal leading-none text-[#b8b8b8] dark:text-white/45">
|
<div className="mt-[8px] flex h-[22px] items-center justify-between text-[11px] font-normal leading-none text-[#b8b8b8] dark:text-white/45">
|
||||||
<div className="flex min-w-0 items-center gap-[6px] pr-2">
|
<div className="flex min-w-0 items-center gap-[6px] pr-2">
|
||||||
<span className="flex h-[16px] w-[16px] shrink-0 items-center justify-center overflow-hidden rounded-full bg-[#d8d8d8] text-[8px] font-medium text-white">
|
<span className="flex h-[16px] w-[16px] shrink-0 items-center justify-center overflow-hidden rounded-full bg-[#d8d8d8] text-[8px] font-medium text-white">
|
||||||
@ -138,11 +154,11 @@ export function LovartFeaturedGallery({
|
|||||||
<div className="inline-flex shrink-0 items-center gap-[7px] text-[11px] leading-none">
|
<div className="inline-flex shrink-0 items-center gap-[7px] text-[11px] leading-none">
|
||||||
<span className="inline-flex items-center gap-[4px]">
|
<span className="inline-flex items-center gap-[4px]">
|
||||||
<PlayCircle className="h-[11px] w-[11px]" strokeWidth={2} />
|
<PlayCircle className="h-[11px] w-[11px]" strokeWidth={2} />
|
||||||
{(1200 + index * 37).toLocaleString()}
|
{(1200 + featuredItems.indexOf(item) * 37).toLocaleString()}
|
||||||
</span>
|
</span>
|
||||||
<span className="inline-flex items-center gap-[4px]">
|
<span className="inline-flex items-center gap-[4px]">
|
||||||
<Heart className="h-[11px] w-[11px]" strokeWidth={2} />
|
<Heart className="h-[11px] w-[11px]" strokeWidth={2} />
|
||||||
{(20 + (index % 9) * 11).toLocaleString()}
|
{(20 + (featuredItems.indexOf(item) % 9) * 11).toLocaleString()}
|
||||||
</span>
|
</span>
|
||||||
<Gem className="h-[11px] w-[11px]" strokeWidth={2} />
|
<Gem className="h-[11px] w-[11px]" strokeWidth={2} />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import {
|
|||||||
} from "@/components/ui/tooltip";
|
} from "@/components/ui/tooltip";
|
||||||
import type { LovartHomeSidebarItemKey } from "@/lib/home/lovart-sidebar-config";
|
import type { LovartHomeSidebarItemKey } from "@/lib/home/lovart-sidebar-config";
|
||||||
import type { NavLink, TutorialVideo } from "./types";
|
import type { NavLink, TutorialVideo } from "./types";
|
||||||
|
import { HomeImage } from "./home-image";
|
||||||
|
|
||||||
type SidebarItem = {
|
type SidebarItem = {
|
||||||
iconUrl?: string | null;
|
iconUrl?: string | null;
|
||||||
@ -183,17 +184,18 @@ export function LovartSidebar({
|
|||||||
onClick={onHome}
|
onClick={onHome}
|
||||||
title={siteName}
|
title={siteName}
|
||||||
>
|
>
|
||||||
{siteLogo ? (
|
<HomeImage
|
||||||
<img
|
sources={[siteLogo]}
|
||||||
src={siteLogo}
|
|
||||||
alt={siteName}
|
alt={siteName}
|
||||||
|
loading="eager"
|
||||||
className="h-7 w-7 rounded-full object-cover"
|
className="h-7 w-7 rounded-full object-cover"
|
||||||
/>
|
imageClassName="h-7 w-7 rounded-full object-cover"
|
||||||
) : (
|
fallback={
|
||||||
<span className="flex h-7 w-7 items-center justify-center rounded-full bg-black text-[12px] font-semibold text-white dark:bg-white dark:text-black">
|
<span aria-hidden="true" className="flex h-7 w-7 items-center justify-center rounded-full bg-black text-[12px] font-semibold text-white dark:bg-white dark:text-black">
|
||||||
{siteName.slice(0, 1).toUpperCase()}
|
{siteName.slice(0, 1).toUpperCase()}
|
||||||
</span>
|
</span>
|
||||||
)}
|
}
|
||||||
|
/>
|
||||||
</button>
|
</button>
|
||||||
</TooltipTrigger>
|
</TooltipTrigger>
|
||||||
<TooltipContent side="right">{siteName}</TooltipContent>
|
<TooltipContent side="right">{siteName}</TooltipContent>
|
||||||
|
|||||||
@ -918,9 +918,9 @@ export function ImageAnnotator({
|
|||||||
|
|
||||||
setIsRemovingBg(true);
|
setIsRemovingBg(true);
|
||||||
try {
|
try {
|
||||||
// 使用原图URL获取原图数据,而不是canvas的预览图
|
const input = /^https?:\/\//i.test(originalImageUrl)
|
||||||
const base64Data = await imageUrlToBase64(originalImageUrl);
|
? { imageUrl: originalImageUrl }
|
||||||
logger.debug("rmbg_source_prepared", { base64Length: base64Data.length });
|
: { image_base64: await imageUrlToBase64(originalImageUrl) };
|
||||||
|
|
||||||
// 调用抠图API
|
// 调用抠图API
|
||||||
const response = await fetch("/api/rmbg", {
|
const response = await fetch("/api/rmbg", {
|
||||||
@ -929,7 +929,7 @@ export function ImageAnnotator({
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
image_base64: base64Data,
|
...input,
|
||||||
prompt: "画图标注-抠图处理", // 可选:添加提示词
|
prompt: "画图标注-抠图处理", // 可选:添加提示词
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
@ -41,45 +41,11 @@ const PHASE_HINT: Record<NonNullable<ImageGenerationLoadingProps["phase"]>, stri
|
|||||||
idle: "任务已创建,正在初始化生成参数。",
|
idle: "任务已创建,正在初始化生成参数。",
|
||||||
queued: "任务已进入队列,请稍候。",
|
queued: "任务已进入队列,请稍候。",
|
||||||
processing: "模型正在生成图片,请稍候。",
|
processing: "模型正在生成图片,请稍候。",
|
||||||
finalizing: "图片已生成完成,正在同步到对话。",
|
finalizing: "图片已返回,正在保存并同步到对话。",
|
||||||
failed: "生成失败,请重试。",
|
failed: "本次处理已停止,请查看对话中的原因和建议。",
|
||||||
};
|
};
|
||||||
|
|
||||||
const LONG_PROCESSING_HINTS = [
|
|
||||||
"正在读取参考图与文字需求。",
|
|
||||||
"正在分析主体、场景和构图关系。",
|
|
||||||
"正在提取画面中的关键视觉特征。",
|
|
||||||
"正在确认图片比例和输出尺寸。",
|
|
||||||
"正在匹配当前模型的最佳生成参数。",
|
|
||||||
"正在把参考图信息提交给模型。",
|
|
||||||
"正在等待模型完成第一阶段推理。",
|
|
||||||
"正在生成初始画面结构。",
|
|
||||||
"正在保持主体特征和参考一致。",
|
|
||||||
"正在补全画面中的空间层次。",
|
|
||||||
"正在调整光影方向和明暗关系。",
|
|
||||||
"正在优化主体边缘与背景融合。",
|
|
||||||
"正在细化材质、纹理和局部细节。",
|
|
||||||
"正在减少画面噪点和不自然区域。",
|
|
||||||
"正在提升清晰度与整体质感。",
|
|
||||||
"正在检查生成结果的完整性。",
|
|
||||||
"正在处理高分辨率输出。",
|
|
||||||
"正在等待服务商返回最终图片。",
|
|
||||||
"正在保存图片并同步到对话。",
|
|
||||||
"正在准备把结果显示到画布。",
|
|
||||||
];
|
|
||||||
const LONG_WAIT_SECONDS = 5 * 60;
|
const LONG_WAIT_SECONDS = 5 * 60;
|
||||||
const LONG_WAIT_HINT =
|
|
||||||
"服务繁忙,模型可能正在排队或自动尝试备用通道,请耐心等候,结果返回后会自动同步到画布。";
|
|
||||||
|
|
||||||
function getLongProcessingHint(phase: NonNullable<ImageGenerationLoadingProps["phase"]>, elapsedSeconds: number) {
|
|
||||||
if (phase !== "processing" && phase !== "finalizing") return PHASE_HINT[phase];
|
|
||||||
if (elapsedSeconds >= LONG_WAIT_SECONDS) return LONG_WAIT_HINT;
|
|
||||||
const index = Math.min(
|
|
||||||
LONG_PROCESSING_HINTS.length - 1,
|
|
||||||
Math.floor(Math.max(0, elapsedSeconds) / 15)
|
|
||||||
);
|
|
||||||
return LONG_PROCESSING_HINTS[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
function formatImageQueueWaitLabel(estimatedWaitMs?: number) {
|
function formatImageQueueWaitLabel(estimatedWaitMs?: number) {
|
||||||
const numeric = Number(estimatedWaitMs || 0);
|
const numeric = Number(estimatedWaitMs || 0);
|
||||||
@ -138,16 +104,8 @@ export const ImageGenerationLoading = memo(
|
|||||||
const isLongWait =
|
const isLongWait =
|
||||||
resolvedElapsedSeconds >= LONG_WAIT_SECONDS &&
|
resolvedElapsedSeconds >= LONG_WAIT_SECONDS &&
|
||||||
(phase === "processing" || phase === "finalizing");
|
(phase === "processing" || phase === "finalizing");
|
||||||
const estimateRangeLabel = isLongWait
|
const elapsedLabel = resolvedElapsedSeconds > 0 ? `已等待 ${resolvedElapsedSeconds}s` : "";
|
||||||
? "单次最长约10分钟"
|
const phaseHint = PHASE_HINT[phase];
|
||||||
: providerType === "grsai-gpt-image"
|
|
||||||
? "2~5分钟"
|
|
||||||
: "60~120秒";
|
|
||||||
const elapsedLabel =
|
|
||||||
resolvedElapsedSeconds > 0
|
|
||||||
? `已等待 ${resolvedElapsedSeconds}s`
|
|
||||||
: `预计 ${estimateRangeLabel}`;
|
|
||||||
const phaseHint = getLongProcessingHint(phase, resolvedElapsedSeconds);
|
|
||||||
const queueLine =
|
const queueLine =
|
||||||
phase === "queued" && Number.isFinite(queuePosition)
|
phase === "queued" && Number.isFinite(queuePosition)
|
||||||
? Math.max(0, Number(queuePosition || 0)) > 0
|
? Math.max(0, Number(queuePosition || 0)) > 0
|
||||||
@ -189,12 +147,12 @@ export const ImageGenerationLoading = memo(
|
|||||||
}
|
}
|
||||||
compact={count > 1}
|
compact={count > 1}
|
||||||
phase={phase}
|
phase={phase}
|
||||||
title={isLongWait ? "服务繁忙,排队处理中" : undefined}
|
title={isLongWait && phase === "processing" ? "模型尚未返回结果,请勿重复提交" : undefined}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="space-y-0.5 text-[#9aa3b2]">
|
<div className="space-y-0.5 text-[#9aa3b2]">
|
||||||
<div className="text-[13px] font-medium">
|
<div className="text-[13px] font-medium">
|
||||||
使用 {resolvedModelName} 生成图片,预计 {estimateRangeLabel}
|
使用 {resolvedModelName} 生成图片
|
||||||
</div>
|
</div>
|
||||||
{queueLine ? <div className="text-[13px]">{queueLine}</div> : null}
|
{queueLine ? <div className="text-[13px]">{queueLine}</div> : null}
|
||||||
{queueDetailLine ? <div className="text-[13px]">{queueDetailLine}</div> : null}
|
{queueDetailLine ? <div className="text-[13px]">{queueDetailLine}</div> : null}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import { useEffect, useState } from "react";
|
|||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { ChevronDown, ChevronUp, Link2, Scan, SlidersHorizontal, Sparkles } from "lucide-react";
|
import { ChevronDown, ChevronUp, Link2, Scan, SlidersHorizontal, Sparkles } from "lucide-react";
|
||||||
import { getGrsaiGptImageOutputSize } from "@/lib/ai/grsai-gpt-image-sizes";
|
import { getGrsaiGptImageOutputSize } from "@/lib/ai/grsai-gpt-image-sizes";
|
||||||
import { resolveVip98kImageTaskSize } from "@/lib/ai/vip98k-task-invocation";
|
import { getVip98kQualityOptions, resolveVip98kImageTaskSize, type Vip98kTaskInvocation } from "@/lib/ai/vip98k-task-invocation";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Switch } from "@/components/ui/switch";
|
import { Switch } from "@/components/ui/switch";
|
||||||
@ -207,7 +207,7 @@ export function ImageParamsBar({
|
|||||||
normalizeImageSizeValue(size, "")
|
normalizeImageSizeValue(size, "")
|
||||||
)
|
)
|
||||||
: []
|
: []
|
||||||
);
|
).filter((size) => providerType !== "alibaba-image" || size === "1K" || size === "2K");
|
||||||
|
|
||||||
const buildImageSizeOptions = () => {
|
const buildImageSizeOptions = () => {
|
||||||
if (imageGenConfig?.supportsSize === false) {
|
if (imageGenConfig?.supportsSize === false) {
|
||||||
@ -220,6 +220,7 @@ export function ImageParamsBar({
|
|||||||
|
|
||||||
return DEFAULT_IMAGE_SIZE_OPTIONS.filter((option) => {
|
return DEFAULT_IMAGE_SIZE_OPTIONS.filter((option) => {
|
||||||
if (option.disabled) return false;
|
if (option.disabled) return false;
|
||||||
|
if (providerType === "alibaba-image" && option.value === "4K") return false;
|
||||||
if (!imageGenConfig) return true;
|
if (!imageGenConfig) return true;
|
||||||
if (option.value === "1K") return imageGenConfig.supports1K !== false;
|
if (option.value === "1K") return imageGenConfig.supports1K !== false;
|
||||||
if (option.value === "2K") return imageGenConfig.supports2K !== false;
|
if (option.value === "2K") return imageGenConfig.supports2K !== false;
|
||||||
@ -267,6 +268,17 @@ export function ImageParamsBar({
|
|||||||
availableAspectRatioOptions[0]?.value ||
|
availableAspectRatioOptions[0]?.value ||
|
||||||
"1:1";
|
"1:1";
|
||||||
|
|
||||||
|
const declaredQualityOptions = providerType === "vip98k"
|
||||||
|
? getVip98kQualityOptions((imageGenConfig?.vip98k?.invocation as Vip98kTaskInvocation | undefined)?.inputSchema)
|
||||||
|
: undefined;
|
||||||
|
const qualityOptions = ([
|
||||||
|
{ label: "自动", value: "auto" },
|
||||||
|
{ label: "高", value: "high" },
|
||||||
|
{ label: "中", value: "medium" },
|
||||||
|
{ label: "低", value: "low" },
|
||||||
|
] as const).filter(option => declaredQualityOptions === undefined ||
|
||||||
|
(declaredQualityOptions.length ? declaredQualityOptions.includes(option.value) : option.value === "auto"));
|
||||||
|
|
||||||
const clampParamsByProvider = (raw: ImageGenerationParams): ImageGenerationParams => {
|
const clampParamsByProvider = (raw: ImageGenerationParams): ImageGenerationParams => {
|
||||||
const options = getImageCountOptions();
|
const options = getImageCountOptions();
|
||||||
const fallbackCount = options[0] ?? 1;
|
const fallbackCount = options[0] ?? 1;
|
||||||
@ -285,6 +297,7 @@ export function ImageParamsBar({
|
|||||||
aspectRatio,
|
aspectRatio,
|
||||||
imageSize,
|
imageSize,
|
||||||
imageCount,
|
imageCount,
|
||||||
|
quality: qualityOptions.some(option => option.value === raw.quality) ? raw.quality : qualityOptions[0]?.value || "auto",
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -321,12 +334,6 @@ export function ImageParamsBar({
|
|||||||
...buildBaseParams(initialParams),
|
...buildBaseParams(initialParams),
|
||||||
});
|
});
|
||||||
const [minimalParamsOpen, setMinimalParamsOpen] = useState(false);
|
const [minimalParamsOpen, setMinimalParamsOpen] = useState(false);
|
||||||
const qualityOptions = [
|
|
||||||
{ label: "自动", value: "auto" },
|
|
||||||
{ label: "高", value: "high" },
|
|
||||||
{ label: "中", value: "medium" },
|
|
||||||
{ label: "低", value: "low" },
|
|
||||||
] as const;
|
|
||||||
const qualityLabel =
|
const qualityLabel =
|
||||||
qualityOptions.find((option) => option.value === params.quality)?.label || "中";
|
qualityOptions.find((option) => option.value === params.quality)?.label || "中";
|
||||||
|
|
||||||
@ -500,7 +507,8 @@ export function ImageParamsBar({
|
|||||||
title="生成参数"
|
title="生成参数"
|
||||||
aria-label={`比例:${params.aspectRatio},分辨率:${params.imageSize}`}
|
aria-label={`比例:${params.aspectRatio},分辨率:${params.imageSize}`}
|
||||||
>
|
>
|
||||||
<span className={referencePresentation ? "min-w-0 truncate text-[13px]" : undefined}>{qualityLabel} · {selectedAspectLabel} · {currentImageSize} · {selectedImageCount} {referencePresentation ? "张" : "img"}</span>
|
{referencePresentation ? <SlidersHorizontal className="lovart-image-params-icon h-4 w-4 shrink-0" aria-hidden="true" /> : null}
|
||||||
|
<span className={referencePresentation ? "lovart-image-params-label min-w-0 truncate text-[13px]" : undefined}>{qualityLabel} · {selectedAspectLabel} · {currentImageSize} · {selectedImageCount} {referencePresentation ? "张" : "img"}</span>
|
||||||
{referencePresentation ? <ReferenceIcon name="chevron-down" size={18} className={minimalParamsOpen ? "rotate-180" : undefined} /> : minimalParamsOpen ? (
|
{referencePresentation ? <ReferenceIcon name="chevron-down" size={18} className={minimalParamsOpen ? "rotate-180" : undefined} /> : minimalParamsOpen ? (
|
||||||
<ChevronUp className="ml-1 h-3 w-3" />
|
<ChevronUp className="ml-1 h-3 w-3" />
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
|
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { X, Download, Edit, Target, Paintbrush, Sparkles, Scissors, Expand, Maximize2, Move3D, Droplet } from "lucide-react";
|
import { X, Download, Edit, Target, Paintbrush, Sparkles, Scissors, Expand, Maximize2, Move3D, Droplet } from "lucide-react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
|
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
||||||
|
|
||||||
const ImageAnnotator = dynamic(
|
const ImageAnnotator = dynamic(
|
||||||
() => import("./image-annotator").then((module) => module.ImageAnnotator),
|
() => import("./image-annotator").then((module) => module.ImageAnnotator),
|
||||||
@ -22,25 +24,6 @@ const AngleEditDialog = dynamic(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
function triggerBrowserDownload(
|
|
||||||
url: string,
|
|
||||||
filename?: string,
|
|
||||||
options?: { openInNewTab?: boolean }
|
|
||||||
) {
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = url;
|
|
||||||
if (filename) {
|
|
||||||
link.download = filename;
|
|
||||||
}
|
|
||||||
if (options?.openInNewTab) {
|
|
||||||
link.target = "_blank";
|
|
||||||
}
|
|
||||||
link.rel = "noopener noreferrer";
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
document.body.removeChild(link);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function ImagePreview({
|
export function ImagePreview({
|
||||||
src,
|
src,
|
||||||
alt,
|
alt,
|
||||||
@ -237,34 +220,12 @@ export function ImagePreview({
|
|||||||
toast.error("下载失败,请重试");
|
toast.error("下载失败,请重试");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const filename = `generated-image-${Date.now()}.jpg`;
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
await downloadOriginalImage({ url: safeOriginalUrl });
|
||||||
buildImageProxyUrl(safeOriginalUrl, { delivery: "proxy" })
|
toast.success("原图已开始下载");
|
||||||
);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`下载失败(${response.status})`);
|
|
||||||
}
|
|
||||||
const blob = await response.blob();
|
|
||||||
const url = window.URL.createObjectURL(blob);
|
|
||||||
triggerBrowserDownload(url, filename);
|
|
||||||
window.URL.revokeObjectURL(url);
|
|
||||||
toast.success("下载成功");
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("下载失败:", error);
|
console.error("下载失败:", error);
|
||||||
try {
|
toast.error(getCanvasImageDownloadErrorMessage(error));
|
||||||
triggerBrowserDownload(safeOriginalUrl, filename);
|
|
||||||
toast.message("代理下载失败,已切换为原图直链下载");
|
|
||||||
} catch (fallbackError) {
|
|
||||||
console.error("原图直链下载失败:", fallbackError);
|
|
||||||
try {
|
|
||||||
triggerBrowserDownload(safeOriginalUrl, undefined, { openInNewTab: true });
|
|
||||||
toast.message("已打开原图,可直接另存为");
|
|
||||||
} catch (openError) {
|
|
||||||
console.error("打开原图失败:", openError);
|
|
||||||
toast.error("下载失败,请重试");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -316,6 +277,10 @@ export function ImagePreview({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (isRetiredStorageUrl(src) || isRetiredStorageUrl(original)) {
|
||||||
|
return <div role="status" className="my-2 rounded-lg border bg-muted p-4 text-sm text-muted-foreground">{RETIRED_STORAGE_MESSAGE}</div>;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{/* 缩略图(显示压缩后的缩略图)*/}
|
{/* 缩略图(显示压缩后的缩略图)*/}
|
||||||
@ -342,9 +307,18 @@ export function ImagePreview({
|
|||||||
}}
|
}}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
|
onLoad={(event) => {
|
||||||
|
event.currentTarget.style.aspectRatio = "";
|
||||||
|
event.currentTarget.parentElement?.style.removeProperty("width");
|
||||||
|
event.currentTarget.alt = "Generated Image";
|
||||||
|
}}
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== (thumbnail || original)) {
|
if (!normalizeLegacyAliyunStorageUrl(thumbnail || original) && event.currentTarget.src !== (thumbnail || original)) {
|
||||||
event.currentTarget.src = thumbnail || original;
|
event.currentTarget.src = thumbnail || original;
|
||||||
|
} else if (!compactGrid) {
|
||||||
|
event.currentTarget.style.aspectRatio = "1";
|
||||||
|
event.currentTarget.parentElement?.style.setProperty("width", "100%");
|
||||||
|
event.currentTarget.alt = "预览暂不可用,可尝试下载原图";
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onClick={() => setIsOpen(true)}
|
onClick={() => setIsOpen(true)}
|
||||||
@ -636,7 +610,7 @@ export function ImagePreview({
|
|||||||
onClick={(e: React.MouseEvent) => e.stopPropagation()}
|
onClick={(e: React.MouseEvent) => e.stopPropagation()}
|
||||||
>
|
>
|
||||||
<img
|
<img
|
||||||
src={original}
|
src={normalizeLegacyAliyunStorageUrl(original) ? buildImageProxyUrl(original) : original}
|
||||||
alt="Generated Image - Original"
|
alt="Generated Image - Original"
|
||||||
className={`max-h-[90vh] max-w-full rounded-lg object-contain ${
|
className={`max-h-[90vh] max-w-full rounded-lg object-contain ${
|
||||||
isAnnotationMode ? "cursor-crosshair" : ""
|
isAnnotationMode ? "cursor-crosshair" : ""
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import type { Vote } from "@/lib/db/schema";
|
|||||||
import type { ChatMessage } from "@/lib/types";
|
import type { ChatMessage } from "@/lib/types";
|
||||||
import { cn, sanitizeText } from "@/lib/utils";
|
import { cn, sanitizeText } from "@/lib/utils";
|
||||||
import { extractImageUrlsFromText } from "@/lib/image-url";
|
import { extractImageUrlsFromText } from "@/lib/image-url";
|
||||||
|
import { getGatewayErrorPageMessage } from "@/lib/request-error";
|
||||||
import { useDataStream } from "./data-stream-provider";
|
import { useDataStream } from "./data-stream-provider";
|
||||||
import { DocumentToolResult } from "./document";
|
import { DocumentToolResult } from "./document";
|
||||||
import { DocumentPreview } from "./document-preview";
|
import { DocumentPreview } from "./document-preview";
|
||||||
@ -521,7 +522,7 @@ const PurePreviewMessage = ({
|
|||||||
const displayModelName = extractedModelName || inferredFallbackModelName;
|
const displayModelName = extractedModelName || inferredFallbackModelName;
|
||||||
const filteredText =
|
const filteredText =
|
||||||
message.role === "assistant"
|
message.role === "assistant"
|
||||||
? appendImageDoneSummary(contentWithoutBadge)
|
? getGatewayErrorPageMessage(contentWithoutBadge) || appendImageDoneSummary(contentWithoutBadge)
|
||||||
: contentWithoutBadge;
|
: contentWithoutBadge;
|
||||||
const useAssistantTypewriter =
|
const useAssistantTypewriter =
|
||||||
message.role === "assistant" &&
|
message.role === "assistant" &&
|
||||||
|
|||||||
21
components/page-feedback.tsx
Normal file
21
components/page-feedback.tsx
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import dynamic from "next/dynamic";
|
||||||
|
import { usePathname } from "next/navigation";
|
||||||
|
import { useSession } from "next-auth/react";
|
||||||
|
|
||||||
|
const FeedbackDialog = dynamic(
|
||||||
|
() => import("@/components/canvas/CanvasFeedbackDialog").then((module) => module.CanvasFeedbackDialog),
|
||||||
|
{ ssr: false },
|
||||||
|
);
|
||||||
|
|
||||||
|
export function PageFeedback() {
|
||||||
|
const pathname = usePathname();
|
||||||
|
const { data: session } = useSession();
|
||||||
|
|
||||||
|
if (!session?.user?.id || session.user.type === "guest" || !pathname) return null;
|
||||||
|
if (/^\/canvas\/[0-9a-f-]{36}\/?$/i.test(pathname) || /^\/(?:feedback|login|register)(?:\/|$)/.test(pathname)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return <FeedbackDialog key={pathname} pagePath={pathname} floating />;
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user