Compare commits

..

No commits in common. "main" and "codex/canvas-operation-sync-20261106" have entirely different histories.

181 changed files with 1403 additions and 9390 deletions

View File

@ -40,11 +40,6 @@ EMAIL_VERIFICATION_SECRET=
# Optional scheduled job protection.
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
# -----------------------------------------------------------------------------

View File

@ -2377,10 +2377,8 @@ export function LocalAiImagePipelineSections({
items={[
{
label:
extendConfig.provider === "fal"
? "fal · FLUX 2 Pro 扩图"
: extendConfig.provider === "vip98k"
? `98K · ${extendConfig.modelId || "image-expansion"}`
extendConfig.provider === "vip98k"
? "VIP98K 服务商"
: "阿里云服务商",
tone: "enabled",
},
@ -2393,13 +2391,9 @@ export function LocalAiImagePipelineSections({
}
title="图像拓展API配置"
>
{extendConfig.provider === "fal" ? (
{extendConfig.provider === "vip98k" ? (
<div className="rounded-lg border bg-muted/40 p-4 text-sm">
使用 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 的 image-expansion 模型。API Key 从“AI 模型设置”中的
VIP98K 服务商读取,仅在这里维护扩展比例。
</div>
) : (

View File

@ -274,9 +274,7 @@ export type EnhanceConfig = {
};
export type ExtendConfig = {
provider: "vip98k" | "dashscope" | "fal";
modelId: string;
routeOptionId: string;
provider: "vip98k" | "dashscope";
xScale: string;
yScale: string;
};

View File

@ -2576,9 +2576,7 @@ export function AdminSettingsClient() {
// 图像拓展API配置状态
const [extendConfig, setExtendConfig] = useState({
provider: "dashscope" as "vip98k" | "dashscope" | "fal",
modelId: "",
routeOptionId: "",
provider: "dashscope" as "vip98k" | "dashscope",
xScale: "1.5", // X轴放大倍数
yScale: "1.5", // Y轴放大倍数
});
@ -2893,16 +2891,12 @@ export function AdminSettingsClient() {
useEffect(() => {
if (extendConfigData?.setting?.value) {
const config = extendConfigData.setting.value as {
provider?: "vip98k" | "dashscope" | "fal";
modelId?: string;
routeOptionId?: string;
provider?: "vip98k" | "dashscope";
xScale?: number;
yScale?: number;
};
setExtendConfig({
provider: config.provider === "vip98k" || config.provider === "fal" ? config.provider : "dashscope",
modelId: config.modelId || "",
routeOptionId: config.routeOptionId || "",
provider: config.provider === "vip98k" ? "vip98k" : "dashscope",
xScale: config.xScale ? String(config.xScale) : "1.5",
yScale: config.yScale ? String(config.yScale) : "1.5",
});
@ -5511,8 +5505,6 @@ export function AdminSettingsClient() {
key: "extend_api_config",
value: {
provider: extendConfig.provider,
modelId: extendConfig.modelId,
routeOptionId: extendConfig.routeOptionId,
xScale,
yScale,
},
@ -13517,8 +13509,7 @@ export function AdminSettingsClient() {
系统智能选择(根据意图路由)
</SelectItem>
{modelsData?.models
?.filter((m: any) => m.supportsImageGen && m.isEnabled && !m.isSubModel &&
providersData?.providers?.some((provider: any) => provider.id === m.providerId && provider.isEnabled))
?.filter((m: any) => m.supportsImageGen)
.map((model: any) => (
<SelectItem key={model.id} value={model.id}>
{model.displayName}

View File

@ -23,21 +23,6 @@ import { AntigravityBackground } from "@/components/antigravity-background";
import { createClientLogger } from "@/lib/observability/client-logger";
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() {
const [email, setEmail] = useState("");
const [isSuccessful, setIsSuccessful] = useState(false);
@ -85,7 +70,7 @@ export default function Page() {
.then((data) => {
if (cancelled) return;
if (data?.valid === true && data?.userType !== "guest") {
window.location.replace(getSafeLoginCallbackUrl());
window.location.replace("/");
return;
}
setIsCheckingExistingSession(false);
@ -105,7 +90,6 @@ export default function Page() {
const completeDingTalkLogin = useCallback(async (token: string) => {
logger.debug("[DingTalk] Completing login with token", { hasToken: Boolean(token) });
setIsHandlingToken(true);
const callbackUrl = getSafeLoginCallbackUrl();
try {
// 获取 CSRF token
@ -118,12 +102,11 @@ export default function Page() {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"X-Auth-Return-Redirect": "1",
},
body: new URLSearchParams({
csrfToken,
token,
callbackUrl,
callbackUrl: "/",
}),
credentials: "include", // 重要:确保 cookie 被发送和接收
});
@ -151,12 +134,12 @@ export default function Page() {
});
if (session && session.user) {
logger.debug("[DingTalk] Session validated, redirecting...");
logger.debug("[DingTalk] Session validated, redirecting to home...");
setIsSuccessful(true);
// 使用 replace 而不是 href,避免在历史记录中留下登录页
// 增加短暂延迟确保 cookie 完全同步
setTimeout(() => {
window.location.replace(callbackUrl);
window.location.replace("/");
}, 200);
} else {
logger.error("[DingTalk] Session not created after login");
@ -191,7 +174,7 @@ export default function Page() {
// 使用 replace 而不是 href,避免在历史记录中留下登录页
// 增加短暂延迟确保 cookie 完全同步
setTimeout(() => {
window.location.replace(callbackUrl);
window.location.replace(result.url || "/");
}, 200);
} else {
logger.error("[DingTalk] Session not created");
@ -315,7 +298,7 @@ export default function Page() {
return;
}
await signIn("google", { redirectTo: getSafeLoginCallbackUrl() });
await signIn("google", { callbackUrl: "/" });
};
// 检查 URL 中是否有钉钉 Token(页面加载时自动登录)
@ -382,14 +365,13 @@ export default function Page() {
description: "提交的信息校验失败",
});
} else if (state.status === "success") {
const callbackUrl = getSafeLoginCallbackUrl();
toast({ type: "success", description: "登录成功,正在跳转" });
toast({ type: "success", description: "登录成功,正在进入首页" });
setIsSuccessful(true);
loginRedirectTimerRef.current = window.setTimeout(() => {
window.location.replace(callbackUrl);
window.location.replace("/");
}, 120);
loginRedirectFallbackTimerRef.current = window.setTimeout(() => {
window.location.assign(callbackUrl);
window.location.assign("/");
}, 1600);
}
}, [state.status]);
@ -411,6 +393,23 @@ 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 params = new URLSearchParams();
if (orgName) params.set("org", orgName);

View File

@ -4,8 +4,7 @@ import { fetchImageBufferFromUrl } from "@/lib/ai/rmbg/service";
import { getLanguageModelById } from "@/lib/ai/dynamic-provider";
import {
DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY,
findConfiguredDetailReplacementImageModel,
isDetailReplacementImageModel,
normalizeDetailReplacementImageModelConfig,
} from "@/lib/ai/detail-replacement-image-model-config";
import {
compositeReplacementWithReferenceLocks,
@ -30,6 +29,7 @@ import {
} from "@/lib/db/queries";
import { uploadFile } from "@/lib/storage/service";
import { bufferToArrayBuffer } from "./persistence";
import { isVideoProviderType } from "./video-models";
import type { AgentPresetMessageImagePart } from "./agent-scene-workflow";
import type { PostRequestBody } from "../schema";
@ -623,7 +623,7 @@ async function resolveReplacementProductIdentityProfile(params: {
system:
"你是高精度商品结构分析器。多张图片属于同一商品;请跨视角还原唯一结构身份,并明确每个视角最清楚的构件。",
temperature: params.planningRuntime.kind === "local_moonshot" ? 1 : 0.1,
maxOutputTokens: 1800,
maxTokens: 1800,
abortSignal: AbortSignal.timeout(
params.planningRuntime.kind === "local_moonshot" ? 90_000 : 60_000,
),
@ -2161,10 +2161,22 @@ export async function resolveDirectReplacementModelRef(params: {
models?: Awaited<ReturnType<typeof getEnabledAiModelsWithProvider>>;
}) {
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({
key: DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY,
}).catch(() => null);
const configuredModel = findConfiguredDetailReplacementImageModel(models, configuredModelSetting?.value);
const configuredModelId = normalizeDetailReplacementImageModelConfig(
configuredModelSetting?.value,
).modelId;
const configuredModel = imageModels.find(
({ model }) =>
model.id === configuredModelId || model.modelId === configuredModelId,
);
if (configuredModel) {
return configuredModel.model.id;
}
@ -2178,7 +2190,6 @@ export async function resolveDirectReplacementModelRef(params: {
return routedModel;
}
const imageModels = models.filter(isDetailReplacementImageModel);
const preferred =
imageModels.find(({ model }) => model.isDefault) ||
imageModels.find(({ provider }) => String(provider?.type || "").includes("image")) ||
@ -2376,7 +2387,7 @@ export async function analyzeReplacementReferenceImage(params: {
},
],
temperature: 0,
maxOutputTokens: 160,
maxTokens: 160,
abortSignal: AbortSignal.timeout(45_000),
} as any);
@ -2679,7 +2690,7 @@ export async function analyzeReplacementExecutionPlan(params: {
system:
"你是高精度商品详情图场景规划器。你只分析当前参考图本身,负责理解这一屏的场景、机位、主体位置和人物需求,不要臆测或依赖其他输入图片。",
temperature: params.planningRuntime.kind === "local_moonshot" ? 1 : 0.1,
maxOutputTokens: 1600,
maxTokens: 1600,
abortSignal: AbortSignal.timeout(replacementScenePlanningTimeoutMs),
} as any);

View File

@ -264,29 +264,6 @@ export function chatMessagesHaveImageInput(messages: ChatMessage[]) {
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) {
switch (providerType) {
case "bfl-image":

View File

@ -1,11 +1,8 @@
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;
@ -15,12 +12,6 @@ export async function reportImageGenerationFailure(params: {
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,

View File

@ -337,7 +337,6 @@ import {
} from "./_lib/image-media";
import {
buildImageFallbackCandidates,
canUseImageFallback,
countChatMessageImageInputs,
generateImageWithFailoverCandidate,
getImageCrossProviderCandidates,
@ -9217,11 +9216,8 @@ export async function POST(request: Request) {
// 🆕 尝试备用模型(支持跨服务商切换)
let fallbackSuccess = false;
const balanceBlockedProviders = new Set<string>();
if (isProviderBalanceError(primaryError) && currentModelConfig?.providerId) balanceBlockedProviders.add(currentModelConfig.providerId);
const triedFallbackModelIds = new Set<string>();
for (const candidate of fallbackCandidates) {
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, buildMessagesForGemini(), balanceBlockedProviders)) continue;
try {
triedFallbackModelIds.add(String(candidate.model.id || ""));
chatApiLogger.info(`[Chat API] 🔄 Trying fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
@ -9243,7 +9239,6 @@ export async function POST(request: Request) {
chatApiLogger.info(`[Chat API] ✅ Fallback model ${fallbackResult.displayName} succeeded!`);
break;
} catch (fallbackError) {
if (isProviderBalanceError(fallbackError)) balanceBlockedProviders.add(candidate.provider.id);
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
@ -9297,7 +9292,6 @@ export async function POST(request: Request) {
}).filter((candidate) => !triedFallbackModelIds.has(String(candidate.model.id || "")));
for (const candidate of crossProviderCandidates) {
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, buildMessagesForGemini(), balanceBlockedProviders)) continue;
try {
triedFallbackModelIds.add(String(candidate.model.id || ""));
chatApiLogger.info(`[Chat API] 🔁 Gemini cross-provider failover: ${candidate.model.displayName} (${candidate.provider.type})`);
@ -9320,7 +9314,6 @@ export async function POST(request: Request) {
chatApiLogger.info(`[Chat API] ✅ Gemini cross-provider fallback model ${fallbackResult.displayName} succeeded!`);
break;
} catch (fallbackError) {
if (isProviderBalanceError(fallbackError)) balanceBlockedProviders.add(candidate.provider.id);
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
@ -9991,7 +9984,7 @@ export async function POST(request: Request) {
const balanceBlockedProviders = new Set<string>();
if (isProviderBalanceError(primaryError) && currentModelEntry?.provider.id) balanceBlockedProviders.add(currentModelEntry.provider.id);
for (const candidate of fallbackCandidates) {
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForAlibaba, balanceBlockedProviders)) continue;
if (balanceBlockedProviders.has(candidate.provider.id)) continue;
try {
triedFallbackModelIds.add(String(candidate.model.id || ""));
chatApiLogger.info(`[Chat API] 🔄 Alibaba fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
@ -10067,7 +10060,7 @@ export async function POST(request: Request) {
}).filter((candidate) => !triedFallbackModelIds.has(String(candidate.model.id || "")) && !balanceBlockedProviders.has(candidate.provider.id));
for (const candidate of crossProviderCandidates) {
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForAlibaba, balanceBlockedProviders)) continue;
if (balanceBlockedProviders.has(candidate.provider.id)) continue;
try {
triedFallbackModelIds.add(String(candidate.model.id || ""));
chatApiLogger.info(`[Chat API] 🔁 Cross-provider failover attempt: ${candidate.model.displayName} (${candidate.provider.type})`);
@ -10455,10 +10448,7 @@ export async function POST(request: Request) {
throw primaryError;
}
let fallbackSuccess = false;
const balanceBlockedProviders = new Set<string>();
if (isProviderBalanceError(primaryError) && volcanoModelConfig?.providerId) balanceBlockedProviders.add(volcanoModelConfig.providerId);
for (const candidate of fallbackCandidates) {
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForVolcano, balanceBlockedProviders)) continue;
try {
chatApiLogger.info(`[Chat API] 🔄 Volcano fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
const fallbackResult = await generateImageWithFailoverCandidate({
@ -10480,7 +10470,6 @@ export async function POST(request: Request) {
chatApiLogger.info(`[Chat API] ✅ Volcano fallback model ${fallbackResult.displayName} succeeded!`);
break;
} catch (fallbackError) {
if (isProviderBalanceError(fallbackError)) balanceBlockedProviders.add(candidate.provider.id);
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
@ -10516,7 +10505,6 @@ export async function POST(request: Request) {
});
for (const candidate of crossProviderCandidates) {
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForVolcano, balanceBlockedProviders)) continue;
try {
chatApiLogger.info(`[Chat API] 🔁 Volcano cross-provider failover attempt: ${candidate.model.displayName} (${candidate.provider.type})`);
if (
@ -10597,7 +10585,6 @@ export async function POST(request: Request) {
chatApiLogger.info(`[Chat API] ✅ Volcano cross-provider failover succeeded: ${candidate.model.displayName} (${candidate.provider.type})`);
break;
} catch (crossError) {
if (isProviderBalanceError(crossError)) balanceBlockedProviders.add(candidate.provider.id);
const crossErrorMessage = normalizeFailoverErrorMessage(crossError);
if (shouldCooldownImageFallbackModel(crossErrorMessage)) {
setImageFallbackModelCooldown(candidate.model.id, crossErrorMessage);
@ -11341,10 +11328,7 @@ export async function POST(request: Request) {
}
let fallbackSuccess = false;
const balanceBlockedProviders = new Set<string>();
if (isProviderBalanceError(primaryError) && currentModelConfig?.providerId) balanceBlockedProviders.add(currentModelConfig.providerId);
for (const candidate of fallbackCandidates) {
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForCrossProvider, balanceBlockedProviders)) continue;
try {
chatApiLogger.info(`[Chat API] 🔄 OpenAI-compatible fallback model: ${candidate.model.displayName} (${candidate.provider.type})`);
const shouldUseOpenAICompatFallback =
@ -11386,7 +11370,6 @@ export async function POST(request: Request) {
chatApiLogger.info(`[Chat API] ✅ OpenAI-compatible fallback model ${candidate.model.displayName} succeeded!`);
break;
} catch (fallbackError) {
if (isProviderBalanceError(fallbackError)) balanceBlockedProviders.add(candidate.provider.id);
const fallbackErrorMessage = normalizeFailoverErrorMessage(fallbackError);
if (shouldCooldownImageFallbackModel(fallbackErrorMessage)) {
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
@ -11423,7 +11406,6 @@ export async function POST(request: Request) {
});
for (const candidate of crossProviderCandidates) {
if (!canUseImageFallback(candidate, effectiveImageGenerationParams, messagesForCrossProvider, balanceBlockedProviders)) continue;
try {
chatApiLogger.info(`[Chat API] 🔁 OpenAI-compatible cross-provider failover: ${candidate.model.displayName} (${candidate.provider.type})`);
if (
@ -11507,7 +11489,6 @@ export async function POST(request: Request) {
chatApiLogger.info(`[Chat API] ✅ OpenAI-compatible cross-provider failover succeeded: ${candidate.model.displayName} (${candidate.provider.type})`);
break;
} catch (crossError) {
if (isProviderBalanceError(crossError)) balanceBlockedProviders.add(candidate.provider.id);
const crossErrorMessage = normalizeFailoverErrorMessage(crossError);
if (shouldCooldownImageFallbackModel(crossErrorMessage)) {
setImageFallbackModelCooldown(candidate.model.id, crossErrorMessage);

View File

@ -10,10 +10,6 @@ import {
} from "@/lib/db/queries";
import { getAiModelFrontendAvailability } from "@/lib/ai/model-frontend-availability";
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 {
isImageGenerationModelDescriptor,
isVideoModelDescriptor,
@ -161,17 +157,10 @@ export async function GET(request: Request) {
: await getEnabledAiModelsWithProvider(false, true);
const agentModelWithProvider = await getAgentAiModelWithProvider();
const defaultModel = await getDefaultAiModel();
const [siteSettings, agentWorkflowUiConfigSetting, detailReplacementModelSetting] = await Promise.all([
const [siteSettings, agentWorkflowUiConfigSetting] = await Promise.all([
getSiteSettings(),
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];
const serializeModel = ({ model, provider }: ModelWithProvider) => ({
id: model.id,
@ -320,7 +309,6 @@ export async function GET(request: Request) {
: null,
agentWorkflowDefaultModelId: agentWorkflowDefaultModel?.model.id || null,
agentWorkflowConfiguredModels,
detailReplacementModel: detailReplacementModel ? serializeModel(detailReplacementModel) : null,
agentModel: agentModelWithProvider
? {
id: agentModelWithProvider.model.id,

View File

@ -450,7 +450,7 @@ async function analyzeFunctionalShowcaseImage(params: {
},
],
temperature: 0,
maxOutputTokens: 120,
maxTokens: 120,
abortSignal: AbortSignal.timeout(25_000),
} as any);

View File

@ -1,20 +0,0 @@
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 }) });
}

View File

@ -4,7 +4,6 @@ import { auth } from "@/app/(auth)/auth";
import { db } from "@/lib/db";
import { canvasImageTask, canvasProject, message } from "@/lib/db/schema";
import { extractAgentProjectStateFromCanvasData, isAgentProjectRow } from "@/lib/agents/agent-project-compat";
import { MAX_DETAIL_REPLACEMENT_REFERENCES } from "@/lib/agents/mode-workspace-state";
import {
createCanvasImageTask,
enqueueCanvasImageTask,
@ -147,14 +146,6 @@ export async function POST(request: Request) {
.map((item) => [String(item.id), String(item.imageUrl)]),
);
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 item = raw && typeof raw === "object" ? raw as Record<string, unknown> : {};
return {
@ -169,6 +160,7 @@ export async function POST(request: Request) {
};
});
if (
references.length === 0 || references.length > 40 ||
new Set(references.map((item) => item.id)).size !== references.length ||
references.some((item) => !item.id || knownReferences.get(item.id) !== item.imageUrl)
) {
@ -195,12 +187,6 @@ export async function POST(request: Request) {
if (!model || model === "auto" || !models.some((entry) => entry.model.id === model)) {
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);
if (!quota.allowed) {
return NextResponse.json({ error: "本月图片生成配额已用完" }, { status: 429 });

View File

@ -42,7 +42,7 @@ export async function GET(
);
}
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 } });
return NextResponse.json({ success: true, task: { ...task, failure, error: failure ? formatRequestFailure(failure, task.id) : task.error } });
}
export async function POST(

View File

@ -1,12 +0,0 @@
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);
}

View File

@ -329,7 +329,6 @@ export async function POST(request: Request) {
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(

View File

@ -9,10 +9,10 @@ export async function GET() {
const models = await listCanvasAudioModels();
return NextResponse.json(
{
models: models.map(({ model, provider }) => ({
models: models.map(({ model }) => ({
id: model.id,
modelId: model.modelId,
name: `${model.displayName} · ${provider.type === "vip98k" ? "98K" : "阿里云"}`,
name: model.displayName,
...audioContract(model.modelId),
})),
},

View File

@ -1,71 +0,0 @@
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 });
}
}

View File

@ -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."
: "你同时扮演高转化电商详情页的平面设计总监与营销总监。仅输出JSON。",
temperature: params.isLocalMoonshot ? 1 : 0.1,
maxOutputTokens: 2000,
maxTokens: 2000,
...(params.kimiOptions || {}),
}, params.timeoutMs || DETAIL_PLAN_PREFLIGHT_TIMEOUT_MS);
@ -2040,7 +2040,7 @@ export async function POST(request: Request) {
kimiDetailProfile: plannerKimiOptions.headers
? {
thinking: plannerKimiOptions.headers["x-detail-agent-kimi-thinking"],
maxOutputTokens: plannerKimiOptions.maxOutputTokens,
maxTokens: plannerKimiOptions.maxTokens,
}
: 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"}]}]}]}`,
temperature: 0.25,
...(localDetailAgentResolved ? { temperature: 1 } : {}),
maxOutputTokens: 1800,
maxTokens: 1800,
...plannerKimiOptions,
});

View File

@ -323,7 +323,7 @@ export async function POST(request: Request) {
kimiDetailProfile: reviewKimiOptions.headers
? {
thinking: reviewKimiOptions.headers["x-detail-agent-kimi-thinking"],
maxOutputTokens: reviewKimiOptions.maxOutputTokens,
maxTokens: reviewKimiOptions.maxTokens,
}
: 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."
: "你同时是高转化电商详情页的平面设计总监与营销总监。按严格专业标准评估,仅输出JSON。",
temperature: localDetailAgentResolved ? 1 : 0.1,
maxOutputTokens: 800,
maxTokens: 800,
...reviewKimiOptions,
} as any);
text = String(result?.text || "");

View File

@ -26,10 +26,9 @@ function failed(failure: DownloadFailure, requestId: string) {
});
}
function preparing(requestId: string, converting = false) {
function preparing(requestId: string) {
return NextResponse.json({ delivery: "preparing", retryAfter: 2, requestId,
phase: converting ? "converting" : "preparing",
message: converting ? "正在转换为兼容 PNG 并保存,完成后自动下载" : "正在准备原图,完成后将自动开始下载" }, {
message: "正在准备原图,完成后将自动开始下载" }, {
status: 202, headers: { "Retry-After": "2", "Cache-Control": "no-store" },
});
}
@ -120,23 +119,23 @@ export async function POST(request: NextRequest) {
const name = sanitizeCanvasDownloadFileName(String(body.name || ""), "canvas-image");
if (body.preferDirect === true) {
const { task, direct, canPrepareDirect } = await resolveCanvasCdnDownload(urls, `${name}.png`);
const { task, direct, canPrepareDirect } = await resolveCanvasCdnDownload(urls, `${name}.png`).catch((error) => {
logger.warn("cdn_lookup_failed", error, { requestId });
return { task: null, direct: null, canPrepareDirect: false };
});
if (direct) return NextResponse.json(direct, { headers: { "Cache-Control": "no-store" } });
if (task && canPrepareDirect) {
if (task && canPrepareDirect && body.preparation === "async") {
const asset = (task.metadata as { downloadAsset?: { status?: string; failure?: DownloadFailure; retryAt?: string } } | null)?.downloadAsset;
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);
return preparing(task.id);
}
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" } });
if (task?.targetUrl) {
if (!urls.includes(task.targetUrl)) urls.unshift(task.targetUrl);
scheduleImageDownloadPreparation(task.id);
}
throw new Error("下载文件暂时无法从存储桶直接下载");
}
const cookie = request.headers.get("cookie") || "";
if (body.preparation === "async") {

View File

@ -1,11 +1,12 @@
import { readDownloadZip } from "@/lib/canvas/download-zip-stream";
import { writeDownloadZip, 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 { createReadStream } from "node:fs";
import { mkdir, stat, writeFile, rm } from "node:fs/promises";
import { join } from "node:path";
import { randomUUID } from "node:crypto";
import { after, NextRequest, NextResponse } from "next/server";
import { prepareCanvasZip } from "@/lib/canvas/download-zip-prepare";
import JSZip from "jszip";
import { auth } from "@/app/(auth)/auth";
import { fetchCanvasDownloadAsset } from "@/lib/canvas/photoshop-safe-download";
import { publishCanvasZip } from "@/lib/storage/canvas-zip-delivery";
@ -28,6 +29,16 @@ function sanitizeFolderName(value: string): string {
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 {
const forwardedHost = (request.headers.get("x-forwarded-host") || "")
.split(",")[0]
@ -88,20 +99,50 @@ async function processTask(id: string, userId: string, requestOrigin: string, co
if (!release) return;
task = await loadZipTask(id, userId);
if (!task || task.status === "completed" || task.status === "failed") return;
if (task.remote) {
const { updateRemoteCanvasZip } = await import("@/lib/canvas/remote-zip-client");
await updateRemoteCanvasZip(task, requestOrigin);
return;
}
await assertZipDiskSpace();
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 })
);
delete task.zipFile; delete task.contentDisposition; delete task.cdnUrl;
// Stage each image on disk, not all PNG buffers in the web process heap.
// A restart resumes from these files using the current owner's session.
let cursor = 0;
let writes = Promise.resolve();
const persist = () => { const current = task!; writes = writes.then(() => saveZipTask(current)); return writes; };
const results = await Promise.allSettled(Array.from({ length: Math.min(2, task.files.length) }, async () => {
while (cursor < task!.files.length) {
const index = cursor++; const file = task!.files[index];
const path = join(zipTaskPath(id), `${index}.image`);
if (file.prepared && file.filename && (await stat(path).catch(() => null))?.size) continue;
delete file.prepared; delete file.filename;
const result = await fetchCanvasDownloadAsset({ file, index, requestOrigin, cookie,
signal: AbortSignal.timeout(120_000) }).catch(() => ({ ok: false as const, error: "原图读取超时或源地址暂时不可用" }));
if (result.ok) {
await assertZipDiskSpace(result.bytes.length);
await writeFile(path, result.bytes, { mode: 0o600 });
file.prepared = `${index}.image`; file.filename = result.filename; delete file.error;
} else file.error = result.error;
task!.processedCount = task!.files.filter(f => f.prepared || f.error).length;
task!.successCount = task!.files.filter(f => f.prepared).length;
task!.failedCount = task!.files.filter(f => f.error).length;
task!.stage = `正在准备原图 ${task!.processedCount}/${task!.totalCount}`;
task!.updatedAt = Date.now(); await persist();
}
}));
const rejected = results.find(result => result.status === "rejected");
if (rejected?.status === "rejected") throw rejected.reason;
if (!task.successCount) throw new Error("所选原图暂时都无法读取,请稍后重试;画布图片不会被删除");
const zip = new JSZip(); const folder = zip.folder(task.folderName) || zip;
for (const file of task.files) if (file.prepared && file.filename)
folder.file(file.filename, createReadStream(join(zipTaskPath(id), file.prepared))
.once("close", () => { void rm(join(zipTaskPath(id), file.prepared!), { force: true }).catch(() => undefined); }));
if (task.failedCount) folder.file("_download-failures.txt", task.files.filter(f => f.error)
.map(f => `${f.name || "图片"}:${f.error}`).join("\n"));
task.stage = "正在生成完整 ZIP 文件"; await saveZipTask(task);
task.zipFile = await writeDownloadZip(zip, task.id, percent => { task!.compressPercent = percent; });
task.contentDisposition = buildContentDisposition(task.folderName);
}
task.stage = "正在准备下载链接"; await saveZipTask(task);
const cdn = await publishCanvasZip({ id: task.id, ...task.zipFile!, disposition: task.contentDisposition!,
@ -154,20 +195,13 @@ export async function POST(request: NextRequest) {
await mkdir(zipTaskPath(id), { mode: 0o700 });
});
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,
task = { id, userId: session.user.id, folderName: sanitizeFolderName(String(body.folderName || "")), files,
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;
@ -177,10 +211,7 @@ export async function POST(request: NextRequest) {
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"); }));
after(() => cleanupZipTasks().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) {
console.warn("[CanvasDownloadZipTask] Create rejected", { message: (error as Error).message });
@ -195,8 +226,6 @@ export async function GET(request: NextRequest) {
const task = await loadZipTask(id, session.user.id);
if (!task) return NextResponse.json({ error: "打包任务不存在或已过期,请重新打包" }, { status: 404 });
if (request.nextUrl.searchParams.get("download") === "1") {
if (task.remote && task.status === "completed" && task.cdnUrl)
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);

View File

@ -976,8 +976,7 @@ export async function POST(request: NextRequest) {
const annotatedFilename = `canvas-local-edit-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.png`;
const annotatedUpload = await uploadFile(
annotatedFilename,
toArrayBuffer(annotatedBuffer),
{ preferQiniuForm: true }
toArrayBuffer(annotatedBuffer)
);
const prompt = buildLocalEditPrompt(regions);
const manualRegionReferenceImageUrls = regions.flatMap((region) =>

View File

@ -1,11 +1,7 @@
import { NextResponse } from "next/server";
import { z } from "zod";
import { and, eq, inArray } from "drizzle-orm";
import { auth } from "@/app/(auth)/auth";
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 sharp from "sharp";
@ -94,39 +90,6 @@ 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) {
const message =
error instanceof Error ? error.message.toLowerCase() : String(error || "").toLowerCase();
@ -184,11 +147,6 @@ export async function POST(request: Request) {
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" });
const prompt = [
"你是图片文字识别助手。",
@ -233,9 +191,15 @@ export async function POST(request: Request) {
}
const parsedJson = parseJsonText(text || "");
const items = Array.isArray(parsedJson?.items)
? sanitizeItems(parsedJson.items)
: fallbackItemsFromRaw(text);
let items: OcrItem[] = [];
if (parsedJson && Array.isArray(parsedJson.items)) {
items = sanitizeItems(parsedJson.items);
}
if (items.length === 0 && text) {
items = fallbackItemsFromRaw(text);
}
return NextResponse.json({
success: true,
@ -249,7 +213,7 @@ export async function POST(request: Request) {
return NextResponse.json(
{
success: false,
error: "图片文字识别暂不可用,请稍后重试",
error: error instanceof Error ? error.message : "文字识别失败",
},
{ status: 500 },
);

View File

@ -1,7 +0,0 @@
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);

View File

@ -133,13 +133,15 @@ function buildImageResponse(
etag: string;
vary?: string;
cacheStatus?: "HIT" | "MISS";
cacheControl?: string;
attachment?: boolean;
}
) {
const headers: Record<string, string> = {
"Content-Type": params.contentType,
"Content-Length": String(body.length),
ETag: params.etag,
"Cache-Control": "public, max-age=31536000, immutable, stale-while-revalidate=86400",
"Cache-Control": params.cacheControl || "public, max-age=31536000, immutable, stale-while-revalidate=86400",
};
if (params.vary) {
headers.Vary = params.vary;
@ -147,6 +149,11 @@ function buildImageResponse(
if (params.cacheStatus) {
headers["X-Image-Proxy-Cache"] = params.cacheStatus;
}
if (params.attachment) {
headers["Content-Disposition"] = "attachment";
headers["X-Content-Type-Options"] = "nosniff";
headers["Content-Security-Policy"] = "default-src 'none'; sandbox";
}
return new NextResponse(body, {
status: 200,
headers,
@ -331,18 +338,7 @@ export async function GET(request: NextRequest) {
);
}
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)))) {
if (!original && isAlibabaModelImageUrl(target.toString())) {
try {
const { getArchivedImageUrlMappings } = await import("@/lib/storage/image-archive-db");
const mappings = await getArchivedImageUrlMappings([target.toString()]);
@ -367,9 +363,7 @@ export async function GET(request: NextRequest) {
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" } : {}) } });
return NextResponse.json(direct || { delivery: "proxy" }, { headers: { "Cache-Control": "no-store" } });
} catch (error) {
return downloadFailure(error);
}
@ -466,29 +460,20 @@ export async function GET(request: NextRequest) {
if (original) return downloadFailure("Source is not an image");
return NextResponse.json({ error: "Upstream is not an image" }, { status: 400 });
}
if (original) {
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);
}
}
if (original) {
return buildImageResponse(originalBuffer, {
contentType,
etag: `"${crypto.createHash("sha256").update(originalBuffer).digest("hex")}"`,
cacheControl: "private, no-store",
attachment: true,
});
}
const isSvg = contentType.toLowerCase().includes("image/svg");
const isGif = contentType.toLowerCase().includes("image/gif");

View File

@ -114,7 +114,7 @@ export async function POST(request: NextRequest) {
},
],
temperature: 0.2,
maxOutputTokens: 1200,
maxTokens: 1200,
} as any);
return NextResponse.json({

View File

@ -4,15 +4,13 @@ import { createHash } from "node:crypto";
import { z } from "zod";
import { db } from "@/lib/db";
import {
aiModel,
aiProvider,
siteSettings,
systemSettingHistory,
systemSettingSnapshot,
systemSettings,
} from "@/lib/db/schema";
import { and, eq, inArray, sql } from "drizzle-orm";
import { omitAgentWorkflowPrompts } from "@/lib/agents/workflow-loading";
import { and, eq, inArray } from "drizzle-orm";
import { auth } from "@/app/(auth)/auth";
import { isAgentWorkflowAdminType, isSiteAdminType } from "@/lib/middleware/admin";
import {
@ -555,8 +553,6 @@ function normalizeAgentCategoryTreeConfig(value: unknown): AgentCategoryTreeConf
type PublicSiteSettingsScope =
| "agent-brand-model"
| "agent-canvas"
| "agent-canvas-base"
| "agent-workflow-templates"
| "agent-entry"
| "agent-presets"
| "auth"
@ -569,8 +565,6 @@ type PublicSiteSettingsScope =
const PUBLIC_SITE_SETTINGS_SCOPES = new Set<string>([
"agent-brand-model",
"agent-canvas",
"agent-canvas-base",
"agent-workflow-templates",
"agent-entry",
"agent-presets",
"auth",
@ -688,7 +682,7 @@ function createScopedSiteSettingsResponse(
return response;
}
async function readScopedSiteSettings(keys: string[], omitWorkflowSteps = false) {
async function readScopedSiteSettings(keys: string[]) {
const [settings, settingRows] = await Promise.all([
db
.select()
@ -696,14 +690,8 @@ async function readScopedSiteSettings(keys: string[], omitWorkflowSteps = false)
.limit(1)
.then((rows) => rows[0]),
keys.length > 0
? (omitWorkflowSteps
? 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())
? db
.select()
.from(systemSettings)
.where(inArray(systemSettings.key, keys))
: Promise.resolve([]),
@ -791,17 +779,8 @@ async function getScopedPublicSiteSettings(
});
}
if (scope === "agent-workflow-templates") {
const [setting] = await db.select({ value: systemSettings.value })
.from(systemSettings).where(eq(systemSettings.key, AGENT_WORKFLOW_UI_CONFIG_KEY)).limit(1);
const config = normalizeAgentWorkflowUiConfig(setting?.value);
return createScopedSiteSettingsResponse(scope, {
steps: omitAgentWorkflowPrompts(config.steps),
}, "MISS", options.cacheKey || scope, { etag: options.etag });
}
if (scope === "agent-canvas" || scope === "agent-canvas-base" || scope === "agent-entry") {
const { settings, valueByKey } = await readScopedSiteSettings(AGENT_CANVAS_SETTING_KEYS, scope !== "agent-canvas");
if (scope === "agent-canvas" || scope === "agent-entry") {
const { settings, valueByKey } = await readScopedSiteSettings(AGENT_CANVAS_SETTING_KEYS);
const canvasFeatureAvailability = await readCanvasFeatureAvailability(
valueByKey
);
@ -852,9 +831,7 @@ async function getScopedPublicSiteSettings(
pageFeatureSettings: rawUiConfig?.pageFeatureSettings,
businessModeSettings: rawUiConfig?.businessModeSettings,
}
: scope === "agent-canvas-base"
? { ...rawUiConfig, steps: [] }
: rawUiConfig
: rawUiConfig
),
agentWorkflowShowcaseConfig: normalizeAgentWorkflowShowcaseConfig(
valueByKey.get(AGENT_WORKFLOW_SHOWCASE_CONFIG_KEY)
@ -1113,8 +1090,7 @@ export async function GET(request: NextRequest) {
const cacheKey = brandName.trim()
? `${scopedScope}:${brandName.trim().toLowerCase()}`
: scopedScope;
const etag = scopedScope === "agent-canvas" || scopedScope === "agent-entry" ||
scopedScope === "agent-canvas-base" || scopedScope === "agent-workflow-templates"
const etag = scopedScope === "agent-canvas" || scopedScope === "agent-entry"
? await getAgentCanvasSettingsEtag(scopedScope)
: undefined;
if (etag && request.headers.get("If-None-Match") === etag) {
@ -2213,14 +2189,6 @@ export async function PUT(request: NextRequest) {
}
}
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 {
surpriseCategories,
aiDetailGenerationEnabled,

View File

@ -6,7 +6,6 @@ import { eq } from "drizzle-orm";
import { uploadFile } from "@/lib/storage/service";
import {
createMediaAssetRelation,
getDefaultStorageConfig,
getPointsSystemConfig,
getMediaAssetsByUserAndUrls,
upsertMediaAsset,
@ -22,27 +21,6 @@ export const maxDuration = 600;
const WATERMARK_SYNC_WAIT_TIMEOUT_MS = 20_000;
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配置
async function getWatermarkRemoveConfig() {
try {
@ -153,16 +131,13 @@ async function persistWatermarkResult(params: {
processing_time?: number;
}> {
try {
let resultUrl = await resolveStoredQiniuImageUrl(params.processedImageUrl);
if (!resultUrl) {
const imageResponse = await fetch(params.processedImageUrl);
if (!imageResponse.ok) {
throw new Error("下载处理后的图片失败");
}
const imageBuffer = await imageResponse.arrayBuffer();
const filename = `watermark-removed-${Date.now()}.png`;
resultUrl = (await uploadFile(filename, imageBuffer)).url;
const imageResponse = await fetch(params.processedImageUrl);
if (!imageResponse.ok) {
throw new Error("下载处理后的图片失败");
}
const imageBuffer = await imageResponse.arrayBuffer();
const filename = `watermark-removed-${Date.now()}.png`;
const uploadResult = await uploadFile(filename, imageBuffer);
let saved = false;
let saveError: unknown = null;
@ -175,8 +150,8 @@ async function persistWatermarkResult(params: {
try {
([savedImage] = await db.insert(generatedImage).values({
userId: params.userId,
thumbnailUrl: resultUrl,
originalUrl: resultUrl,
thumbnailUrl: uploadResult.url,
originalUrl: uploadResult.url,
imageSize: "2K",
prompt: "去水印处理",
modelId: "watermark-remove",
@ -209,9 +184,9 @@ async function persistWatermarkResult(params: {
mediaType: "image",
sourceType: "edited",
status: "ready",
originalUrl: resultUrl,
previewUrl: resultUrl,
thumbnailUrl: resultUrl,
originalUrl: uploadResult.url,
previewUrl: uploadResult.url,
thumbnailUrl: uploadResult.url,
displayName: "去水印处理",
prompt: "去水印处理",
modelId: "watermark-remove",
@ -242,8 +217,8 @@ async function persistWatermarkResult(params: {
return {
success: true,
original_url: resultUrl,
thumbnail_url: resultUrl,
original_url: uploadResult.url,
thumbnail_url: uploadResult.url,
processing_time:
typeof params.startedAt === "number" ? (Date.now() - params.startedAt) / 1000 : undefined,
};
@ -387,7 +362,7 @@ export async function POST(request: NextRequest) {
const body = await request.json();
const { image_url } = body;
if (typeof image_url !== "string" || !image_url) {
if (!image_url) {
return NextResponse.json(
{ error: "请提供图片URL" },
{ status: 400 }
@ -438,24 +413,24 @@ export async function POST(request: NextRequest) {
);
}
}
// 七牛上的公开图片可以直接交给去水印服务读取。
// 如果是 HTTP/HTTPS URL,下载后重新上传确保公网可访问
else if (image_url.startsWith("http://") || image_url.startsWith("https://")) {
const storedImageUrl = await resolveStoredQiniuImageUrl(image_url);
publicImageUrl = storedImageUrl || image_url;
if (!storedImageUrl) {
try {
console.log("[WatermarkRemove] 下载原图并上传到S3...");
const imageResponse = await fetch(image_url);
if (!imageResponse.ok) {
throw new Error("下载原图失败");
}
const imageBuffer = await imageResponse.arrayBuffer();
const filename = `watermark-input-${Date.now()}.jpg`;
publicImageUrl = (await uploadFile(filename, imageBuffer)).url;
console.log("[WatermarkRemove] 图片已上传到S3:", publicImageUrl);
} catch (downloadError) {
console.error("[WatermarkRemove] 下载/上传原图失败,使用原始URL:", downloadError);
try {
console.log("[WatermarkRemove] 下载原图并上传到S3...");
const imageResponse = await fetch(image_url);
if (!imageResponse.ok) {
throw new Error("下载原图失败");
}
const imageBuffer = await imageResponse.arrayBuffer();
const filename = `watermark-input-${Date.now()}.jpg`;
const uploadResult = await uploadFile(filename, imageBuffer);
publicImageUrl = uploadResult.url;
console.log("[WatermarkRemove] 图片已上传到S3:", publicImageUrl);
} catch (downloadError) {
console.error("[WatermarkRemove] 下载/上传原图失败:", downloadError);
// 如果下载失败,尝试使用原始URL
console.log("[WatermarkRemove] 尝试使用原始URL");
publicImageUrl = image_url;
}
} else {
return NextResponse.json(

View File

@ -5,7 +5,6 @@ import { readCanvasDraft, writeCanvasDraft, clearCanvasDraft } from "@/lib/clien
import { mergeCanvasSnapshots } from "@/lib/video-canvas/merge-snapshot";
import { BEFORE_PAGE_REFRESH, type PageRefreshRequest } from "@/lib/client/page-refresh";
import { CanvasRemoteOperationContext, type CanvasRemoteOperation, type CanvasRemoteOperationResult } from "@/components/canvas/CanvasRemoteOperationContext";
import equal from "fast-deep-equal";
import {
AlertCircle,
AlertTriangle,
@ -172,7 +171,6 @@ import {
type VideoCanvasSymbolMarkerId,
} from "@/lib/canvas/video-canvas-markers";
import {
isCanvasImageUploadPending,
LOCAL_UPLOAD_PENDING_METADATA_KEY,
LOCAL_UPLOAD_PREVIEW_METADATA_KEY,
} from "@/lib/canvas/local-upload-state";
@ -233,8 +231,7 @@ import {
type WorkflowSceneRecord,
} from "@/lib/video-workflow/runtime";
import { agentProjectService } from "@/services/agent-projects";
import { getWorkflowSelectionsRequest } from "@/lib/agents/workflow-loading";
import { fetchCanvasSiteSettings, fetchWorkflowTemplatePrompts } from "@/lib/client/canvas-site-settings";
import { fetchCanvasSiteSettings } from "@/lib/client/canvas-site-settings";
import {
canvasService,
imageGenerationService,
@ -274,17 +271,6 @@ import type {
const CANVAS_SITE_SETTINGS_REFRESH_INTERVAL_MS = 15000;
const CANVAS_SURFACE_BACKGROUND = "#f5f5f5";
function WorkflowLoadState({ name, error, retrying, onRetry }: { name: string; error?: Error; retrying?: boolean; onRetry: () => void }) {
const failed = Boolean(error && !retrying);
const timedOut = error?.name === "AbortError" || error?.name === "TimeoutError";
return (
<div className="flex items-center gap-3 rounded-xl border border-white/10 px-4 py-3 text-sm text-white/65" role={failed ? "alert" : "status"}>
<span>{failed ? `${name}${timedOut ? "加载超时" : "加载失败"}` : `正在加载${name}…`}</span>
{failed && <Button type="button" size="sm" variant="outline" onClick={onRetry}>重试</Button>}
</div>
);
}
function CanvasLazyPanelLoader({
label,
className,
@ -2188,13 +2174,13 @@ export default function CanvasEditorLegacyPage({
);
const canvasSiteSettingsKey = requestedAgentMode
? hasActiveAgentProject
? "/api/site-settings?scope=agent-canvas-base"
? "/api/site-settings?scope=agent-canvas"
: "/api/site-settings?scope=agent-entry"
: "/api/site-settings?scope=canvas";
const [hasResolvedCanvasSiteSettings, setHasResolvedCanvasSiteSettings] =
useState(false);
const { data: siteSettingsData, error: siteSettingsError, isValidating: siteSettingsLoading, mutate: reloadSiteSettings } = useSWR<{
const { data: siteSettingsData } = useSWR<{
siteName?: string;
siteLogo?: string | null;
siteLogoDark?: string | null;
@ -2225,7 +2211,6 @@ export default function CanvasEditorLegacyPage({
? 0
: CANVAS_SITE_SETTINGS_REFRESH_INTERVAL_MS,
errorRetryCount: 1,
shouldRetryOnError: !requestedAgentMode || !hasActiveAgentProject,
dedupingInterval: 30_000,
revalidateOnFocus: true,
revalidateOnReconnect: true,
@ -2292,34 +2277,9 @@ export default function CanvasEditorLegacyPage({
!requestedAgentMode ||
!agentProjectId ||
loadedAgentProjectId === agentProjectId;
const [workflowTemplateMarker, setWorkflowTemplateMarker] = useState<HTMLDivElement | null>(null);
const [workflowTemplatesRequested, setWorkflowTemplatesRequested] = useState(false);
const hasWorkflowBase = Boolean(siteSettingsData?.agentWorkflowUiConfig);
useEffect(() => {
if (!isAgentMode || !hasActiveAgentProject || !hasWorkflowBase || workflowTemplatesRequested) return;
const element = workflowTemplateMarker;
if (!element) return;
const observer = new IntersectionObserver(([entry]) => {
if (entry?.isIntersecting) setWorkflowTemplatesRequested(true);
});
observer.observe(element);
return () => observer.disconnect();
}, [hasActiveAgentProject, hasWorkflowBase, isAgentMode, workflowTemplateMarker, workflowTemplatesRequested]);
const { data: workflowTemplates, error: workflowTemplatesError, isValidating: workflowTemplatesLoading, mutate: reloadWorkflowTemplates } = useSWR<{
steps: AgentWorkflowUiConfig["steps"];
}>(
isAgentMode && hasActiveAgentProject && hasWorkflowBase && workflowTemplatesRequested
? "/api/site-settings?scope=agent-workflow-templates"
: null,
fetchCanvasSiteSettings,
{ shouldRetryOnError: false }
);
const resolvedWorkflowUiConfig = useMemo(
() => {
const base = siteSettingsData?.agentWorkflowUiConfig || defaultAgentWorkflowUiConfig;
return requestedAgentMode ? { ...base, steps: workflowTemplates?.steps ?? [] } : base;
},
[requestedAgentMode, siteSettingsData?.agentWorkflowUiConfig, workflowTemplates?.steps]
() => siteSettingsData?.agentWorkflowUiConfig || defaultAgentWorkflowUiConfig,
[siteSettingsData?.agentWorkflowUiConfig]
);
const workflowPageFeatureSettings = useMemo(
() => getAgentWorkflowPageFeatureSettings(resolvedWorkflowUiConfig),
@ -2368,7 +2328,7 @@ export default function CanvasEditorLegacyPage({
});
const sanitizedWorkflowSelections = useMemo(
() =>
requestedAgentMode && !workflowTemplates
requestedAgentMode && !siteSettingsData?.agentWorkflowUiConfig
? workflowSelections
: sanitizeAgentWorkflowSelections({
config: resolvedWorkflowUiConfig || defaultAgentWorkflowUiConfig,
@ -2376,7 +2336,7 @@ export default function CanvasEditorLegacyPage({
}),
[
requestedAgentMode,
workflowTemplates,
siteSettingsData?.agentWorkflowUiConfig,
resolvedWorkflowUiConfig,
workflowSelections,
]
@ -2384,14 +2344,6 @@ export default function CanvasEditorLegacyPage({
const deferredSanitizedWorkflowSelections = useDeferredValue(
sanitizedWorkflowSelections
);
const workflowSelectionRequest = isAgentMode && hasActiveAgentProject && hasLoadedCurrentAgentProject
? getWorkflowSelectionsRequest(sanitizedWorkflowSelections)
: "";
const { data: workflowPromptData, error: workflowPromptError, isValidating: workflowPromptLoading, mutate: reloadWorkflowPrompts } = useSWR(
workflowSelectionRequest ? ["/api/agent-workflow/template-prompts", workflowSelectionRequest] as const : null,
fetchWorkflowTemplatePrompts,
{ shouldRetryOnError: false }
);
const resolvedBrandModelConfig = useMemo(
() =>
normalizeAgentWorkflowBrandModelConfig(
@ -5168,31 +5120,7 @@ export default function CanvasEditorLegacyPage({
const canvasAutosaveTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
const canvasPersistenceScope = useRef(id);
const canvasRemoteOperationActive = useRef(false);
const canvasLeaveApprovedRef = useRef(false);
const [canvasLeaveUrl, setCanvasLeaveUrl] = useState("");
const [isSavingBeforeLeave, setIsSavingBeforeLeave] = useState(false);
const [canvasLeaveError, setCanvasLeaveError] = useState("");
canvasPersistenceScope.current = id;
const hasUnsavedCanvasChanges = useCallback(() => {
if (!canvasReadyRef.current || useCanvasStore.getState().projectId !== id || savedCanvasSnapshotRef.current?.id !== id) return false;
if (useCanvasStore.getState().nodes.some(isCanvasImageUploadPending)) return true;
const saved = JSON.parse(savedCanvasSnapshotRef.current.snapshot).canvasData as CanvasState;
const current = JSON.parse(JSON.stringify(buildPersistedCanvasData())) as CanvasState;
const content = (value: CanvasState) => [value.nodes.map(node => {
const { updatedAt: _updatedAt, ...nodeData } = node;
return { ...nodeData, connections: node.connections || [] };
}), value.edges || [], value.suppressedMediaKeys || [], normalizeStyleProPersistedState(value.styleProState)];
return !equal(content(saved), content(current));
}, [buildPersistedCanvasData, id]);
const requestCanvasLeave = useCallback((url: string) => {
if (hasUnsavedCanvasChanges()) {
setCanvasLeaveError("");
setCanvasLeaveUrl(url);
return;
}
canvasLeaveApprovedRef.current = true;
window.location.assign(url);
}, [hasUnsavedCanvasChanges]);
const applyCanvasRemoteSnapshot = useCallback((result: CanvasRemoteOperationResult) => {
if (result.projectId !== id || canvasPersistenceScope.current !== id || useCanvasStore.getState().projectId !== id) return false;
const remoteSnapshot = JSON.stringify({ canvasData: result.canvasData });
@ -5219,13 +5147,13 @@ export default function CanvasEditorLegacyPage({
}, [buildPersistedCanvasData, id]);
const persistCanvasStateNow = useCallback(
async (reason: "autosave" | "delete" | "beforeunload") => {
if (canvasLeaveApprovedRef.current || canvasPersistenceScope.current !== id || !canvasReadyRef.current || useCanvasStore.getState().projectId !== id) return false;
if (canvasPersistenceScope.current !== id || !canvasReadyRef.current || useCanvasStore.getState().projectId !== id) return false;
if (externalCanvasConflictRef.current) return false;
setIsSaving(true);
try {
let canvasData = buildPersistedCanvasData();
let snapshot = JSON.stringify({ canvasData });
if (savedCanvasSnapshotRef.current?.id === id && equal(JSON.parse(savedCanvasSnapshotRef.current.snapshot), JSON.parse(snapshot))) {
if (savedCanvasSnapshotRef.current?.id === id && savedCanvasSnapshotRef.current.snapshot === snapshot) {
clearCanvasDraft(id);
return true;
}
@ -5273,8 +5201,8 @@ export default function CanvasEditorLegacyPage({
snapshot: JSON.stringify({ canvasData: buildPersistedCanvasData() }),
});
canvasSyncErrorRef.current = result.status === 409
? "画布同步发生冲突,改动已保留。请在画布上方选择保留本页或云端的冲突内容,再继续操作。"
: `画布自动同步失败${result.status ? `(HTTP ${result.status})` : "(网络连接失败)"}。${retained ? "本地草稿已保留,系统会继续自动同步,也可稍后重试。" : "请保留当前页面,检查网络后重试同步,暂勿刷新。"}`;
? "画布同步发生冲突,本次任务未提交,改动已保留。请在画布上方选择保留本页或云端的冲突内容,再继续操作。"
: `画布自动同步失败${result.status ? `(HTTP ${result.status})` : "(网络连接失败)"},本次任务未提交。${retained ? "本地草稿已保留,请检查网络后再次发送。" : "请保留当前页面,检查网络后再次发送,暂勿刷新。"}`;
console.error(`[Canvas] ${reason} save failed:`, result.error);
} else {
canvasSyncErrorRef.current = "";
@ -5304,34 +5232,6 @@ export default function CanvasEditorLegacyPage({
[persistCanvasStateNow]
);
const finishCanvasLeave = useCallback(async (save: boolean) => {
if (!canvasLeaveUrl || isSavingBeforeLeave) return;
setCanvasLeaveError("");
if (save && useCanvasStore.getState().nodes.some(isCanvasImageUploadPending)) {
setCanvasLeaveError("还有图片未上传成功。请等待上传完成;若上传失败,请重新上传或移除该图片后再保存并离开。");
return;
}
if (canvasAutosaveTimer.current) {
clearTimeout(canvasAutosaveTimer.current);
canvasAutosaveTimer.current = null;
}
if (save) {
setIsSavingBeforeLeave(true);
try {
if (!(await persistCanvasState("autosave"))) throw new Error(canvasSyncErrorRef.current || "画布暂时无法同步,请保留此页并稍后重试。");
if (hasUnsavedCanvasChanges() && !(await persistCanvasState("autosave"))) throw new Error(canvasSyncErrorRef.current);
if (hasUnsavedCanvasChanges()) throw new Error("还有改动正在同步,请稍后重试离开。");
} catch (error) {
setCanvasLeaveError(error instanceof Error ? error.message : "画布暂时无法同步,请保留此页并稍后重试。");
setIsSavingBeforeLeave(false);
return;
}
}
canvasLeaveApprovedRef.current = true;
clearCanvasDraft(id);
window.location.assign(canvasLeaveUrl);
}, [canvasLeaveUrl, hasUnsavedCanvasChanges, id, isSavingBeforeLeave, persistCanvasState]);
const resolveCanvasSyncConflict = useCallback((choice: "local" | "remote") => {
return canvasSaveQueue.current!.enqueue(async () => {
setIsSaving(true);
@ -5374,7 +5274,7 @@ export default function CanvasEditorLegacyPage({
return canvasSaveQueue.current!.enqueueOperation(
async () => {
const saved = await persistCanvasStateNow("autosave");
if (!saved) throw new Error(canvasSyncErrorRef.current ? `${canvasSyncErrorRef.current} 本次处理未提交。` : "画布尚未完成加载,本次处理未提交,请等待加载完成后重试。");
if (!saved) throw new Error(canvasSyncErrorRef.current || "画布尚未完成加载,本次处理未提交,请等待加载完成后重试。");
return true;
},
async () => {
@ -5396,18 +5296,20 @@ export default function CanvasEditorLegacyPage({
const persistCanvasBeforeAgent = useCallback(async () => {
if (!canvasReadyRef.current || useCanvasStore.getState().projectId !== id || externalCanvasConflictRef.current)
throw new Error(canvasSyncErrorRef.current ? `${canvasSyncErrorRef.current} 本次任务未提交。` : "画布尚未完成同步,本次任务未提交,请保留当前页面并重试同步。");
throw new Error(canvasSyncErrorRef.current || "画布尚未完成同步,本次任务未提交,请保留当前页面并重试同步。");
if (savedCanvasSnapshotRef.current?.id === id) {
if (!hasUnsavedCanvasChanges()) return true;
const saved = JSON.parse(savedCanvasSnapshotRef.current.snapshot).canvasData;
const current = buildPersistedCanvasData();
if (JSON.stringify(saved.nodes) === JSON.stringify(current.nodes) && JSON.stringify(saved.edges) === JSON.stringify(current.edges)) return true;
}
// Flush the pending local edit once instead of racing its scheduled autosave.
if (canvasAutosaveTimer.current) {
clearTimeout(canvasAutosaveTimer.current);
canvasAutosaveTimer.current = null;
}
if (!(await persistCanvasState("autosave"))) throw new Error(canvasSyncErrorRef.current ? `${canvasSyncErrorRef.current} 本次任务未提交。` : "画布尚未完成加载,本次任务未提交;请等待加载完成后再次发送。");
if (!(await persistCanvasState("autosave"))) throw new Error(canvasSyncErrorRef.current || "画布尚未完成加载,本次任务未提交;请等待加载完成后再次发送。");
return true;
}, [persistCanvasState, hasUnsavedCanvasChanges, id]);
}, [persistCanvasState, buildPersistedCanvasData, id]);
const canvasRemoteOperationContext = useMemo(() => ({
run: runCanvasRemoteOperation,
@ -5418,10 +5320,6 @@ export default function CanvasEditorLegacyPage({
if (isVideoMode) return;
const beforeRefresh = (event: Event) => {
const request = (event as CustomEvent<PageRefreshRequest>).detail;
if (!isAgentMode && useCanvasStore.getState().nodes.some(isCanvasImageUploadPending)) {
request.block("还有图片未上传成功。请等待上传完成;若上传失败,请重新上传或移除该图片后再刷新。");
return;
}
if (useCanvasStore.getState().isGenerating) {
request.block("当前图片任务仍在处理中,请等待结果返回后再刷新。");
return;
@ -5457,32 +5355,7 @@ export default function CanvasEditorLegacyPage({
clearTimeout(saveTimer);
if (canvasAutosaveTimer.current === saveTimer) canvasAutosaveTimer.current = null;
};
}, [isAgentMode, isAgentOperationRefreshActive, nodes, edges, isLoading, persistCanvasState]);
useEffect(() => {
if (isLoading || isAgentMode) return;
const retrySync = () => {
if (canvasLeaveApprovedRef.current || externalCanvasConflictRef.current || canvasRemoteOperationActive.current || isAgentOperationRefreshActive() || !hasUnsavedCanvasChanges()) return;
void persistCanvasState("autosave").catch(error => console.error("[Canvas] background sync failed:", error));
};
const onVisibilityChange = () => {
if (!canvasLeaveApprovedRef.current && document.visibilityState === "hidden" && hasUnsavedCanvasChanges()) {
writeCanvasDraft(id, { base: savedCanvasSnapshotRef.current!.snapshot, snapshot: JSON.stringify({ canvasData: buildPersistedCanvasData() }) });
}
};
const retryTimer = window.setInterval(() => {
if (document.visibilityState === "visible" && navigator.onLine && canvasSyncErrorRef.current) retrySync();
}, 20_000);
window.addEventListener("online", retrySync);
window.addEventListener("focus", retrySync);
document.addEventListener("visibilitychange", onVisibilityChange);
return () => {
window.clearInterval(retryTimer);
window.removeEventListener("online", retrySync);
window.removeEventListener("focus", retrySync);
document.removeEventListener("visibilitychange", onVisibilityChange);
};
}, [buildPersistedCanvasData, hasUnsavedCanvasChanges, id, isAgentMode, isAgentOperationRefreshActive, isLoading, persistCanvasState]);
}, [isAgentMode, isAgentOperationRefreshActive, nodes, isLoading, persistCanvasState]);
useEffect(() => {
if (isLoading || isAgentOperationRefreshActive()) return;
@ -5585,25 +5458,18 @@ export default function CanvasEditorLegacyPage({
}
}, [isAgentMode, isAgentOperationRefreshActive, nodes, isLoading, persistCanvasState]);
// 浏览器离开确认只保护尚未同步的内容;真正离开时保留草稿并发送一次保存。
// 页面关闭/刷新前尝试做一次 keepalive 保存,降低“刚删就刷新”回滚概率
useEffect(() => {
if (isLoading || isAgentMode) return;
const retainDraft = () => {
if (canvasLeaveApprovedRef.current || !hasUnsavedCanvasChanges()) return null;
const canvasData = buildPersistedCanvasData();
writeCanvasDraft(id, { base: savedCanvasSnapshotRef.current!.snapshot, snapshot: JSON.stringify({ canvasData }) });
return canvasData;
};
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
if (!retainDraft()) return;
event.preventDefault();
event.returnValue = "true";
};
const handlePageHide = () => {
const handleBeforeUnload = () => {
try {
const canvasData = retainDraft();
if (!canvasData || isSaving || canvasRemoteOperationActive.current || isAgentOperationRefreshActive() || externalCanvasConflictRef.current) return;
if (!canvasReadyRef.current || isAgentOperationRefreshActive()) return;
const canvasData = buildPersistedCanvasData();
writeCanvasDraft(id, {
base: savedCanvasSnapshotRef.current?.id === id ? savedCanvasSnapshotRef.current.snapshot : JSON.stringify({ canvasData: { nodes: [], edges: [] } }),
snapshot: JSON.stringify({ canvasData }),
});
const { canvasRevision, canvasUpdatedAt } = useCanvasStore.getState();
void fetch(`/api/canvas/${id}`, {
method: "PUT",
@ -5622,20 +5488,18 @@ export default function CanvasEditorLegacyPage({
};
window.addEventListener("beforeunload", handleBeforeUnload);
window.addEventListener("pagehide", handlePageHide);
window.addEventListener("pagehide", handleBeforeUnload);
return () => {
window.removeEventListener("beforeunload", handleBeforeUnload);
window.removeEventListener("pagehide", handlePageHide);
window.removeEventListener("pagehide", handleBeforeUnload);
};
}, [
activeCanvasWorkspaceType,
buildPersistedCanvasData,
hasUnsavedCanvasChanges,
id,
isAgentOperationRefreshActive,
isAgentMode,
isLoading,
isSaving,
]);
// 智能命名与缩略图逻辑
@ -9967,8 +9831,6 @@ export default function CanvasEditorLegacyPage({
selectedBrandModel,
appearanceRecognitionEnabled,
selections: deferredSanitizedWorkflowSelections,
resolvedSelections: workflowPromptData?.selectionRequest === getWorkflowSelectionsRequest(deferredSanitizedWorkflowSelections)
? workflowPromptData?.selections : undefined,
});
if (canceled) {
@ -9991,7 +9853,6 @@ export default function CanvasEditorLegacyPage({
selectedBrandModel,
workflowInputs,
workflowMaterials,
workflowPromptData,
]);
useEffect(() => {
workflowInputsRef.current = workflowInputs;
@ -10113,23 +9974,15 @@ export default function CanvasEditorLegacyPage({
[patchPersistedAgentWorkflowState]
);
const getLatestWorkflowContext = useCallback(() => {
const selections = workflowSelectionsRef.current;
return buildAgentWorkflowComposerContext({
config: resolvedWorkflowUiConfig || defaultAgentWorkflowUiConfig,
inputs: workflowInputsRef.current,
materials: workflowMaterialsRef.current,
selectedBrandModel: selectedBrandModelRef.current,
appearanceRecognitionEnabled: appearanceRecognitionEnabledRef.current,
selections,
resolvedSelections: workflowPromptData?.selectionRequest === getWorkflowSelectionsRequest(selections)
? workflowPromptData?.selections : undefined,
selections: workflowSelectionsRef.current,
});
}, [resolvedWorkflowUiConfig, workflowPromptData]);
const getWorkflowPromptStatus = useCallback(() => {
const currentRequest = getWorkflowSelectionsRequest(workflowSelectionsRef.current);
if (!currentRequest || currentRequest === workflowPromptData?.selectionRequest) return "ready";
return workflowPromptError && currentRequest === workflowSelectionRequest ? "error" : "loading";
}, [workflowPromptData, workflowPromptError, workflowSelectionRequest]);
}, [resolvedWorkflowUiConfig]);
const effectiveSelectedAgentPresetId = null;
const { data: selectedAgentPresetResponse } = useSWR<{
success?: boolean;
@ -10504,37 +10357,30 @@ export default function CanvasEditorLegacyPage({
</div>
) : null;
const workflowStylePanel = useMemo(() => (
<div ref={setWorkflowTemplateMarker} className="space-y-3">
{workflowTemplates ? (
<AgentWorkflowPanelDarkV2
config={resolvedWorkflowUiConfig}
embedded
inputs={workflowInputs}
materials={workflowMaterials}
onInputsChange={handleWorkflowInputsChange}
onMaterialsChange={handleWorkflowMaterialsChange}
onSelectionsChange={handleWorkflowSelectionsChange}
selections={sanitizedWorkflowSelections}
showBrandStoreSection={false}
/>
) : siteSettingsError && !siteSettingsData && !siteSettingsLoading ? (
<p className="text-sm text-white/65">请先在上方重试加载品牌和类目。</p>
) : !workflowTemplatesRequested ? (
<Button type="button" variant="outline" onClick={() => setWorkflowTemplatesRequested(true)}>加载风格模板</Button>
) : (
<WorkflowLoadState name="风格模板" error={workflowTemplatesError} retrying={workflowTemplatesLoading} onRetry={() => void reloadWorkflowTemplates().catch(() => {})} />
)}
{workflowPromptError && (
<WorkflowLoadState name="所选模板的提示词" error={workflowPromptError} retrying={workflowPromptLoading} onRetry={() => void reloadWorkflowPrompts().catch(() => {})} />
)}
</div>
), [
handleWorkflowInputsChange, handleWorkflowMaterialsChange, handleWorkflowSelectionsChange,
reloadWorkflowPrompts, reloadWorkflowTemplates, resolvedWorkflowUiConfig, sanitizedWorkflowSelections,
siteSettingsData, siteSettingsError, siteSettingsLoading, workflowInputs, workflowMaterials, workflowPromptError,
workflowPromptLoading, workflowTemplates, workflowTemplatesError, workflowTemplatesLoading, workflowTemplatesRequested,
]);
const workflowStylePanel = useMemo(
() => (
<AgentWorkflowPanelDarkV2
config={resolvedWorkflowUiConfig}
embedded
inputs={workflowInputs}
materials={workflowMaterials}
onInputsChange={handleWorkflowInputsChange}
onMaterialsChange={handleWorkflowMaterialsChange}
onSelectionsChange={handleWorkflowSelectionsChange}
selections={sanitizedWorkflowSelections}
showBrandStoreSection={false}
/>
),
[
handleWorkflowInputsChange,
handleWorkflowMaterialsChange,
handleWorkflowSelectionsChange,
resolvedWorkflowUiConfig,
sanitizedWorkflowSelections,
workflowInputs,
workflowMaterials,
]
);
if (canvasLoadError && !isAgentMode) {
return <div role="alert" className="flex h-dvh flex-col items-center justify-center gap-4">
@ -10577,20 +10423,17 @@ export default function CanvasEditorLegacyPage({
theme="dark"
/>
}
brandStorePanel={siteSettingsData?.agentWorkflowUiConfig ? (
brandStorePanel={
<AgentWorkflowBrandStorePanel
config={resolvedWorkflowUiConfig}
inputs={workflowInputs}
onInputsChange={handleWorkflowInputsChange}
theme="dark"
/>
) : (
<WorkflowLoadState name="品牌和类目" error={siteSettingsError} retrying={siteSettingsLoading} onRetry={() => void reloadSiteSettings().catch(() => {})} />
)}
}
chatId={agentModeChatId}
deletedResultCollections={agentProjectDeletedResultCollections}
getLatestWorkflowContext={getLatestWorkflowContext}
getWorkflowPromptStatus={getWorkflowPromptStatus}
materialSelectionPanel={
isWorkflowMaterialStepEnabled ? (
<AgentWorkflowMaterialSelectionPanel
@ -10666,20 +10509,6 @@ export default function CanvasEditorLegacyPage({
}
>
<CanvasAgentUiListener projectId={id} workspace="canvas" />
<Dialog open={Boolean(canvasLeaveUrl)} onOpenChange={open => { if (!open && !isSavingBeforeLeave) setCanvasLeaveUrl(""); }}>
<DialogContent onEscapeKeyDown={event => { if (isSavingBeforeLeave) event.preventDefault(); }} onPointerDownOutside={event => event.preventDefault()}>
<DialogHeader>
<DialogTitle>离开前保存画布?</DialogTitle>
<DialogDescription>平时会自动保存。当前还有改动尚未同步到云端,可以保存后离开,或放弃这些未同步的改动。</DialogDescription>
</DialogHeader>
{canvasLeaveError ? <p role="alert" className="text-sm text-destructive">{canvasLeaveError}</p> : null}
<DialogFooter className="gap-2">
<Button disabled={isSavingBeforeLeave} variant="ghost" onClick={() => setCanvasLeaveUrl("")}>继续编辑</Button>
<Button disabled={isSavingBeforeLeave} variant="outline" onClick={() => void finishCanvasLeave(false)}>不保存并离开</Button>
<Button disabled={isSavingBeforeLeave} onClick={() => void finishCanvasLeave(true)}>{isSavingBeforeLeave ? "正在保存…" : "保存并离开"}</Button>
</DialogFooter>
</DialogContent>
</Dialog>
{canvasSyncConflicts.length > 0 ? (
<div role="alert" className="absolute inset-x-3 top-28 z-[60] mx-auto flex max-w-2xl flex-wrap items-center gap-3 rounded-lg border bg-background p-4 text-sm shadow-md">
<div className="min-w-0 flex-1 basis-64">
@ -11399,14 +11228,15 @@ export default function CanvasEditorLegacyPage({
{isVideoMode ? brandEntry : (
<CanvasSurfaceHeader
isSaving={isSaving}
onAddImage={handleAddImageNode}
onHome={() => requestCanvasLeave("/")}
onHome={goHome}
onOpenFiles={() => {
setIsPanelOpen(true);
setPanelMode("fileList");
}}
onSave={() => void handleSave()}
projectTitle={projectTitle}
projectId={id}
rightInset={panelOcclusionRight}
/>
)}
@ -11510,7 +11340,6 @@ export default function CanvasEditorLegacyPage({
createUrl={createNewCanvasUrl}
currentId={id}
onClose={() => setPanelMode("chat")}
onNavigate={requestCanvasLeave}
workspaceLabel={workspaceLabel}
workspaceType={activeCanvasWorkspaceType}
/>

View File

@ -1,116 +0,0 @@
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>
);
}

View File

@ -3,9 +3,7 @@ import { Toaster } from "sonner";
import { ThemeProvider } from "@/components/theme-provider";
import { FaviconUpdater } from "@/components/favicon-updater";
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 "./globals.css";
@ -144,7 +142,7 @@ const CHUNK_ERROR_RECOVERY_SCRIPT = `\
showManualRecoveryNotice();
return;
}
if (lastReloadAt || document.documentElement.hasAttribute('${PAGE_DOWNLOAD_ACTIVE_ATTRIBUTE}')) return;
if (lastReloadAt) return;
var now = Date.now();
// If storage is blocked, a reload cannot remember its attempt and must not run.
if (!safeSetSessionStorage(RECOVERY_KEY, String(now))) return;
@ -155,10 +153,6 @@ const CHUNK_ERROR_RECOVERY_SCRIPT = `\
showManualRecoveryNotice();
return;
}
if (document.documentElement.hasAttribute('${PAGE_DOWNLOAD_ACTIVE_ATTRIBUTE}')) {
if (safeSetSessionStorage(RECOVERY_KEY, '0')) lastReloadAt = 0;
return;
}
window.location.reload();
}, 800);
}
@ -223,10 +217,7 @@ export default function RootLayout({
>
<Toaster position="top-center" />
<AppUpdateNotice />
<SessionProvider>
{children}
<PageFeedback />
</SessionProvider>
<SessionProvider>{children}</SessionProvider>
</ThemeProvider>
</body>
</html>

View File

@ -159,7 +159,6 @@ import type {
} from "@/types/agent";
import {
createEmptyAgentModeWorkspaceState,
MAX_DETAIL_REPLACEMENT_REFERENCES,
} from "@/lib/agents/mode-workspace-state";
import {
createEmptyAgentGenerationProjectWorkspaceState,
@ -177,7 +176,6 @@ export interface AgentGenerationWorkspaceDarkV2Props {
workflowFeatureSettings?: AgentWorkflowPageFeatureSettings | null;
workflowPromptOptimizationEnabled?: boolean;
getLatestWorkflowContext?: () => AgentWorkflowComposerContext | null;
getWorkflowPromptStatus?: () => "ready" | "loading" | "error";
onBack?: () => void;
brandStorePanel?: ReactNode;
materialSelectionPanel?: ReactNode;
@ -214,7 +212,6 @@ export interface AgentGenerationWorkspaceDarkV2Props {
interface AgentWorkflowModelOption {
id: string;
displayName: string;
providerName?: string;
providerType: string;
supportsImageGen?: boolean;
isMultimodal?: boolean;
@ -2645,9 +2642,7 @@ function getStoredAgentPendingGenerations(): LocalGenerationPendingState[] {
? record.requestedCardIds
.map((entry) => String(entry || "").trim())
.filter(Boolean)
.slice(0, record.modeType === "detail_replacement"
? MAX_DETAIL_REPLACEMENT_REFERENCES
: MAX_AGENT_MODE_REFERENCE_COUNT)
.slice(0, MAX_AGENT_MODE_REFERENCE_COUNT)
: null,
} satisfies LocalGenerationPendingState;
});
@ -3090,7 +3085,6 @@ export function AgentGenerationWorkspaceDarkV2({
workflowFeatureSettings = null,
workflowPromptOptimizationEnabled = true,
getLatestWorkflowContext,
getWorkflowPromptStatus,
onBack,
brandStorePanel,
materialSelectionPanel,
@ -3388,13 +3382,12 @@ export function AgentGenerationWorkspaceDarkV2({
}
);
const { data: modelsData, error: modelsError, mutate: refreshModels } = useSWR<{
const { data: modelsData } = useSWR<{
models: AgentWorkflowModelOption[];
defaultModelId?: string | null;
agentWorkflowConfiguredDefaultModelId?: string | null;
agentWorkflowDefaultModelId?: string | null;
agentWorkflowConfiguredModels?: AgentWorkflowModelOption[];
detailReplacementModel?: AgentWorkflowModelOption | null;
agentModel?: any;
}>("/api/models", fetcher);
@ -3440,6 +3433,17 @@ export function AgentGenerationWorkspaceDarkV2({
const activeBusinessModeDefaultModelId = !requiresPresetSelection
? String(activeBusinessMode?.defaultModelId || "").trim() || 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 =
selectedAgentPreset?.type === "image" && presetModeConfig.modeType === "detail_replacement"
? detailReplacementMethod === "ai_detail_generate"
@ -3455,19 +3459,6 @@ export function AgentGenerationWorkspaceDarkV2({
const currentResultModeType: AgentGenerationResultModeType = requiresPresetSelection
? presetDrivenResultModeType
: 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 isDetailReplacementPreset =
currentResultModeType === "detail_replacement" ||
@ -4119,15 +4110,12 @@ export function AgentGenerationWorkspaceDarkV2({
);
const executableModelOptions = useMemo(() => {
const seen = new Set<string>();
const workflowModels = detailReplacementModel
? [detailReplacementModel, ...configuredWorkflowModels]
: configuredWorkflowModels;
return [...selectableModels, ...workflowModels].filter((model) => {
return [...selectableModels, ...configuredWorkflowModels].filter((model) => {
if (seen.has(model.id)) return false;
seen.add(model.id);
return true;
});
}, [configuredWorkflowModels, detailReplacementModel, selectableModels]);
}, [configuredWorkflowModels, selectableModels]);
const executableModelIdSet = useMemo(
() => new Set(executableModelOptions.map((model) => model.id)),
[executableModelOptions]
@ -4146,9 +4134,8 @@ export function AgentGenerationWorkspaceDarkV2({
selectableModels[0],
[executableModelOptions, lockedWorkflowModel, selectableModels, selectedModelId]
);
const lockedWorkflowModelLabel = detailReplacementModel?.providerName
? `${detailReplacementModel.displayName} 「${detailReplacementModel.providerName}」`
: lockedWorkflowModel?.displayName ||
const lockedWorkflowModelLabel =
lockedWorkflowModel?.displayName ||
selectedAgentPreset?.defaultModel?.displayName ||
(lockedWorkflowModelId ? `配置模型不可用(${lockedWorkflowModelId})` : "未配置");
const [activeToolStep, setActiveToolStep] = useState<ToolStepKey>("settings");
@ -5892,7 +5879,6 @@ export function AgentGenerationWorkspaceDarkV2({
headers: { "Content-Type": "application/json" },
cache: "no-store",
body: JSON.stringify({
scope: params.scope === "detail" ? "style-detail" : undefined,
folderName: params.zipName,
files: normalizedUrls.map((imageUrl) => ({
url: imageUrl,
@ -6328,24 +6314,10 @@ export function AgentGenerationWorkspaceDarkV2({
toast.error(`请先查询并选择${detailReferenceSourceLabel}参考图`);
return;
}
if (isDetailReplacementModelLoading) {
if (modelsError) {
toast.error("详情替换模型加载失败,正在重新读取");
void refreshModels().catch(() => {});
} else {
toast.info("正在读取详情替换模型,请稍候");
}
return;
}
if (isSkuReplacementPreset && skuReplacementReferenceCards.length === 0) {
toast.error(`请先查询并选择${skuReferenceSourceLabel}参考图`);
return;
}
const promptStatus = getWorkflowPromptStatus?.();
if (promptStatus === "loading" || promptStatus === "error") {
toast.error(promptStatus === "error" ? "请先在风格模板区重试加载提示词" : "正在加载所选模板的提示词,请稍候");
return;
}
const resolvedWorkflowContext = getLatestWorkflowContext?.() ?? workflowContext;
if (
isDetailReplacementPreset &&
@ -6404,10 +6376,6 @@ export function AgentGenerationWorkspaceDarkV2({
}
if (replacementModeType === "detail_replacement") {
if (detailBatchSubmittingRef.current) return;
if (replacementExecutionCards.length > MAX_DETAIL_REPLACEMENT_REFERENCES) {
toast.error(`本次选中了 ${replacementExecutionCards.length} 张详情参考图,一次最多替换 ${MAX_DETAIL_REPLACEMENT_REFERENCES} 张,请减少本次选择后重试`);
return;
}
if (!isValidChatUuid(projectId)) {
toast.error("当前风格化项目尚未保存,请先保存项目后再执行详情替换");
return;
@ -6428,14 +6396,13 @@ export function AgentGenerationWorkspaceDarkV2({
const saved = await onPersistProject?.();
if (saved === false) throw new Error("项目尚未保存,请重试");
const requestId = generateUUID();
const response = await fetch("/api/agents/detail-replacement/batches", {
const response = await fetchWithErrorHandlers("/api/agents/detail-replacement/batches", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
projectId,
requestId,
model: resolvedModelId,
expectedModelId: resolvedModelId === "auto" ? undefined : resolvedModelId,
imageSize: imageParams.imageSize,
replacementStrategy: detailReplacementStrategy,
prompt: detailReplacementStrategy === "product_only"
@ -6459,7 +6426,6 @@ export function AgentGenerationWorkspaceDarkV2({
});
const payload = await response.json().catch(() => ({}));
if (!response.ok || !payload?.task?.id) {
if (payload?.code === "DETAIL_REPLACEMENT_MODEL_CHANGED") void refreshModels().catch(() => {});
throw new Error(payload?.error || "详情替换任务提交失败");
}
setPendingGeneration({
@ -6647,8 +6613,6 @@ export function AgentGenerationWorkspaceDarkV2({
input,
isDetailAiGenerationMethod,
isDetailReplacementPreset,
isDetailReplacementModelLoading,
modelsError,
isSelectedBusinessModeUnavailable,
isSkuReplacementPreset,
effectiveDefaultModelId,
@ -6665,7 +6629,6 @@ export function AgentGenerationWorkspaceDarkV2({
skuResultCards,
userMessageCount,
getLatestWorkflowContext,
getWorkflowPromptStatus,
isSceneGenerationPreset,
workflowContext,
sceneMessageAttachments,
@ -6676,7 +6639,6 @@ export function AgentGenerationWorkspaceDarkV2({
onPersistProject,
projectId,
refreshDetailBatch,
refreshModels,
]);
const handleStopGeneration = useCallback(() => {
detailGenerationCancelRef.current = true;
@ -7840,11 +7802,6 @@ export function AgentGenerationWorkspaceDarkV2({
toast.error(detailGenerationValidationIssues[0] || "请先补齐 AI详情生成参数");
return;
}
const promptStatus = getWorkflowPromptStatus?.();
if (promptStatus === "loading" || promptStatus === "error") {
toast.error(promptStatus === "error" ? "请先在风格模板区重试加载提示词" : "正在加载所选模板的提示词,请稍候");
return;
}
const productAttachments = detailGenerationProductSlotIndexes.map((index) =>
detailGenerationSlots[index]
@ -9449,11 +9406,9 @@ export function AgentGenerationWorkspaceDarkV2({
</Select>
) : (
<div className="rounded-[24px] border border-white/10 bg-black/30 px-4 py-3 text-sm text-white/70">
{isDetailReplacementModelLoading
? modelsError ? "详情替换模型加载失败,点击批量替换可重试" : "正在读取详情替换模型"
: isWorkflowModelLocked ? "后台已锁定生图模型:" : "当前生图模式使用默认模型:"}
{isWorkflowModelLocked ? "后台已锁定生图模型:" : "当前生图模式使用默认模型:"}
<span className="ml-2 font-medium text-white">
{isDetailReplacementModelLoading ? null : isWorkflowModelLocked
{isWorkflowModelLocked
? lockedWorkflowModelLabel
: currentModel?.displayName || "自动选择"}
</span>

View File

@ -3,7 +3,6 @@
import { useMemo } from "react";
import useSWR from "swr";
import { Check, ImageIcon, Loader2, UserRound } from "lucide-react";
import { AgentPreviewImage } from "@/components/agents/AgentPreviewImage";
import { Button } from "@/components/ui/button";
import { cn, fetcher } from "@/lib/utils";
import { normalizeAgentWorkflowBrandModelConfig } from "@/lib/agents/workflow-brand-model-config";
@ -230,11 +229,12 @@ export function AgentWorkflowBrandModelPicker({
isDark ? "bg-white/[0.04]" : "bg-muted/40"
)}
>
<AgentPreviewImage
<img
src={image.imageUrl}
alt={image.displayName || `${normalizedBrand} 模特图`}
className="h-full w-full object-cover"
previewWidth={360}
loading="lazy"
decoding="async"
/>
<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">

View File

@ -1,49 +1,40 @@
"use client";
import { useEffect, useState, useSyncExternalStore } from "react";
import { useEffect, useState } 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";
import { preparePageRefresh } 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;
const controller = new AbortController();
let checking = false;
const check = async () => {
if (disposed || checking || document.visibilityState !== "visible") return;
if (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 });
const response = await fetch("/api/app-version", { cache: "no-store", signal: AbortSignal.any([controller.signal, AbortSignal.timeout(8000)]) });
if (!response.ok) return;
const data = await response.json();
if (!disposed && !current.signal.aborted && typeof data.version === "string" && data.version)
if (!controller.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();
controller.abort();
clearInterval(interval);
window.removeEventListener("focus", check);
window.removeEventListener("online", check);
document.removeEventListener("visibilitychange", check);
};
}, []);
@ -61,7 +52,7 @@ export function AppUpdateNotice() {
setRefreshing(false);
}
};
return <AlertDialog open={Boolean(availableVersion) && !downloading}>
return <AlertDialog open={Boolean(availableVersion)}>
<AlertDialogContent className="z-[10001]" overlayClassName="z-[10000]" onEscapeKeyDown={event => event.preventDefault()}>
<AlertDialogHeader>
<AlertDialogTitle>平台已更新</AlertDialogTitle>
@ -69,7 +60,6 @@ export function AppUpdateNotice() {
</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>

View File

@ -72,13 +72,13 @@ export function AudioGenerationComposer({
setError("");
setSending(true);
try {
if (!model) throw Error("当前模式没有已启用的模型");
if (!model) throw Error("当前模式没有已启用的官方模型");
const params = audioRequestSchema.parse({
...draft,
text: [draft.text, connectedText].filter(Boolean).join("\n\n"),
modelRef: model.id,
voice,
format: format || "wav",
format,
...(!contract?.controls ? { rate: 1, pitch: 1, volume: 50 } : {}),
...(!contract?.instructions ? { instructions: "" } : {}),
});
@ -96,9 +96,9 @@ export function AudioGenerationComposer({
<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"
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">
{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">
@ -112,9 +112,6 @@ export function AudioGenerationComposer({
>
<option value="tts">文字配音</option>
<option value="music">音乐 / 歌曲</option>
<option value="asr">语音转文字</option>
<option value="voiceclone">声音克隆配音</option>
<option value="voicedesign">声音设计配音</option>
</select>
</label>
<label>
@ -137,40 +134,20 @@ export function AudioGenerationComposer({
</select>
</label>
</div>
{["asr", "voiceclone"].includes(draft.mode) && (
<label className="block">
{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} 字)`}
<label className="block">
{draft.mode === "tts" ? "配音文本(最多 600 字)" : "音乐描述"}
<textarea
aria-label="音频内容"
className={input}
rows={4}
maxLength={draft.mode !== "music" ? 600 : contract?.descriptionLimit || 2000}
maxLength={draft.mode === "tts" ? 600 : 2000}
value={draft.text}
onChange={(e) => patch({ text: e.target.value })}
/>
</label>}
{!["music", "asr"].includes(draft.mode) ? (
</label>
{draft.mode === "tts" ? (
<>
{!!contract?.voices.length && <div className="grid grid-cols-2 gap-3">
<div className="grid grid-cols-2 gap-3">
<label>
音色
<select
@ -186,7 +163,7 @@ export function AudioGenerationComposer({
))}
</select>
</label>
{contract?.supportsLanguageControl !== false && <label>
<label>
语言
<select
aria-label="配音语言"
@ -198,8 +175,8 @@ export function AudioGenerationComposer({
<option value="English">英文</option>
<option value="Auto">自动 / 中英混合</option>
</select>
</label>}
</div>}
</label>
</div>
{contract?.instructions ? (
<label className="block">
声音风格指令
@ -233,7 +210,7 @@ export function AudioGenerationComposer({
</div>
) : null}
</>
) : draft.mode === "music" ? (
) : (
<>
<label className="flex gap-2">
<input
@ -256,7 +233,7 @@ export function AudioGenerationComposer({
onChange={(e) => patch({ lyrics: e.target.value })}
/>
</label>
{contract?.supportsGender !== false && (!contract?.genderRequiresLyrics || draft.lyrics?.trim()) && <label>
<label>
演唱声音
<select
className={input}
@ -267,18 +244,18 @@ export function AudioGenerationComposer({
<option value="female">女声</option>
<option value="male">男声</option>
</select>
</label>}
</label>
</>
) : null}
{!options.length ? (
<p className="text-sm text-amber-200">
当前没有可用的音乐模型,请在模型设置中启用。
Fun-Music 需要阿里云邀测权限,开通并启用模型后即可生成。
</p>
) : null}
</>
) : null}
)}
<div className="flex items-end gap-3">
{draft.mode !== "asr" && <label>
<label>
格式
<select
aria-label="音频格式"
@ -292,14 +269,14 @@ export function AudioGenerationComposer({
</option>
))}
</select>
</label>}
</label>
<button
type="button"
disabled={!model || loading}
className="rounded-lg bg-white px-4 py-2 text-black disabled:opacity-40"
onClick={() => void run()}
>
{busy || sending ? "处理中…" : draft.mode === "asr" ? "识别文字" : "生成音频"}
{busy || sending ? "处理中…" : "生成音频"}
</button>
<label className="cursor-pointer rounded-lg border border-white/20 px-3 py-2">
上传音频

View File

@ -1,6 +1,4 @@
"use client";
import { CANVAS_FEEDBACK_CONTEXT_EVENT, feedbackMessages, type FeedbackCaptureEvent } from "@/lib/canvas/feedback-client";
import { CanvasFeedbackDialog } from "./CanvasFeedbackDialog";
import { advancePastAtomicRanges, scanMarkdownImageStreaming } from "@/lib/canvas/markdown-image-streaming";
import { useChat } from "@ai-sdk/react";
@ -80,7 +78,7 @@ import {
type UploadProgress,
} from "@/lib/client/upload-with-retry";
import {
downloadOriginalImage,
downloadCanvasImageAsPng,
getCanvasImageDownloadErrorMessage,
type CanvasImageDownloadProgress,
} from "@/lib/client/canvas-image-download";
@ -1464,7 +1462,7 @@ function normalizeComposerPreviewTail(text: string, fixedPrefix: string) {
}
// 获取图片尺寸(保留原图分辨率 + 画布显示尺寸)
function getImageDimensions(source: string | File): Promise<{
function getImageDimensions(url: string): Promise<{
width: number;
height: number;
originalWidth: number;
@ -1472,7 +1470,6 @@ function getImageDimensions(source: string | File): Promise<{
}> {
return new Promise((resolve) => {
const img = new Image();
const objectUrl = typeof source === "string" ? null : URL.createObjectURL(source);
let settled = false;
const resolveOnce = (dimensions: {
width: number;
@ -1485,11 +1482,9 @@ function getImageDimensions(source: string | File): Promise<{
window.clearTimeout(timeout);
img.onload = null;
img.onerror = null;
if (objectUrl) URL.revokeObjectURL(objectUrl);
resolve(dimensions);
};
const timeout = window.setTimeout(() => {
img.removeAttribute("src");
resolveOnce(DEFAULT_CANVAS_IMAGE_DIMENSIONS);
}, CANVAS_IMAGE_DIMENSION_TIMEOUT_MS);
@ -1522,7 +1517,7 @@ function getImageDimensions(source: string | File): Promise<{
// 加载失败时使用默认尺寸
resolveOnce(DEFAULT_CANVAS_IMAGE_DIMENSIONS);
};
img.src = objectUrl ?? String(source);
img.src = url;
});
}
@ -4431,15 +4426,6 @@ export function CanvasChatPanel({
// 获取纯 UUID(去掉 canvas- 前缀)用于 API 调用
const rawChatId = toRawCanvasChatId(activeChatId);
useEffect(() => {
const collectFeedback = (event: Event) => {
const detail = (event as FeedbackCaptureEvent).detail;
if (detail.projectId !== canvasId) return;
detail.receive({ chatId: rawChatId, messages: feedbackMessages(activeMessagesRef.current) });
};
window.addEventListener(CANVAS_FEEDBACK_CONTEXT_EVENT, collectFeedback);
return () => window.removeEventListener(CANVAS_FEEDBACK_CONTEXT_EVENT, collectFeedback);
}, [canvasId, rawChatId]);
const buildHistoryMessagesUrl = useCallback(
(targetRawChatId: string, options?: { cacheBust?: boolean }) => {
const params = new URLSearchParams({
@ -4478,15 +4464,17 @@ export function CanvasChatPanel({
setDownloadProgress({
phase: "preparing",
message: "正在准备原图...",
percent: 5,
});
try {
await downloadOriginalImage({
await downloadCanvasImageAsPng({
url: originalUrl,
candidates: [raw, buildImageProxyUrl(originalUrl, { delivery: "proxy" })],
signal: controller.signal,
onProgress: setDownloadProgress,
});
toast.success("已开始下载");
toast.success("原图已开始下载");
} catch (error) {
if (controller.signal.aborted) {
toast.message("已取消下载");
@ -7818,6 +7806,7 @@ export function CanvasChatPanel({
existingMessageImageKeys.add(messageImageKey);
}
const dimensions = await getImageDimensions(url);
if (useCanvasStore.getState().projectId !== canvasId || activeChatIdRef.current !== activeChatId) return;
const compareSourceUrlRaw =
sourceImageUrls[Math.min(index, Math.max(0, sourceImageUrls.length - 1))] ||
@ -7850,7 +7839,7 @@ export function CanvasChatPanel({
const placeholderNode = reservedPlaceholderNode
? currentNodes.find((node) => node.id === reservedPlaceholderNode.id) || null
: null;
const size = placeholderNode?.size || anchorNode?.size || { width: 300, height: 300 };
const size = { width: dimensions.width, height: dimensions.height };
const position = placeholderNode
? getCenteredReplacementPosition(placeholderNode, size)
: getAnchoredGenerationPosition({
@ -7868,6 +7857,8 @@ export function CanvasChatPanel({
prompt: generationPrompt,
originalSrc: canUseCompareSource ? compareSourceUrl : undefined,
metadata: {
originalWidth: dimensions.originalWidth,
originalHeight: dimensions.originalHeight,
generationPrompt,
sourceMessageId: messageId || undefined,
sourceImageKey,
@ -8981,10 +8972,6 @@ export function CanvasChatPanel({
useEffect(() => {
const beforeRefresh = (event: Event) => {
const request = (event as CustomEvent<PageRefreshRequest>).detail;
if (composerUploadProgress) {
request.block("素材仍在上传,请等待上传完成后再刷新。当前页面和输入已保留。");
return;
}
if (isCanvasAgentRunning || isClawdSubmitting || isDetailGenerating || status === "streaming" || status === "submitted" || Object.values(sendGuardRef.current).some(guard => guard.inFlight)) {
request.block("当前对话任务仍在处理中,请等待结果返回后再刷新。原任务会继续执行,无需重新提交。");
return;
@ -8994,17 +8981,9 @@ export function CanvasChatPanel({
throw new Error("浏览器无法保存当前输入,请保留此页并检查浏览器存储空间后重试。");
});
};
const beforeUnload = (event: BeforeUnloadEvent) => {
event.preventDefault();
event.returnValue = "true";
};
window.addEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
if (composerUploadProgress) window.addEventListener("beforeunload", beforeUnload);
return () => {
window.removeEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
window.removeEventListener("beforeunload", beforeUnload);
};
}, [activeChatId, input, attachments, composerUploadProgress, isCanvasAgentRunning, isClawdSubmitting, isDetailGenerating, status]);
return () => window.removeEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
}, [activeChatId, input, attachments, isCanvasAgentRunning, isClawdSubmitting, isDetailGenerating, status]);
const restorePendingClawdRequestToComposer = useCallback(
(snapshot: PendingClawdRequestRestoreSnapshot) => {
@ -9592,6 +9571,7 @@ export function CanvasChatPanel({
continue;
}
const dimensions = await getImageDimensions(url);
if (cancelled) return;
const compareSourceUrlRaw =
nearestUserImageUrls[Math.min(index, Math.max(0, nearestUserImageUrls.length - 1))] ||
@ -9624,7 +9604,20 @@ export function CanvasChatPanel({
const placeholderNode = reservedPlaceholderNode
? currentNodes.find((node) => node.id === reservedPlaceholderNode.id) || null
: null;
const size = placeholderNode?.size || anchorNode?.size || { width: 300, height: 300 };
const size = placeholderNode
? { width: dimensions.width, height: dimensions.height }
: anchorNode
? {
width: Math.max(
1,
Math.round(Number(anchorNode.size?.width || dimensions.width))
),
height: Math.max(
1,
Math.round(Number(anchorNode.size?.height || dimensions.height))
),
}
: { width: dimensions.width, height: dimensions.height };
const position = placeholderNode
? getCenteredReplacementPosition(placeholderNode, size)
: getAnchoredGenerationPosition({
@ -9643,6 +9636,8 @@ export function CanvasChatPanel({
prompt: generationPrompt || "对话生成",
originalSrc: canUseCompareSource ? compareSourceUrl : undefined,
metadata: {
originalWidth: dimensions.originalWidth,
originalHeight: dimensions.originalHeight,
generationPrompt: generationPrompt || undefined,
sourceMessageId: message.id,
sourceImageKey: imageEntry.sourceImageKey,
@ -10369,7 +10364,7 @@ export function CanvasChatPanel({
buildCanvasUploadedImageAttachment({ file, imageUrl, assetId })
);
const dimensions = await getImageDimensions(file);
const dimensions = await getImageDimensions(imageUrl);
const nodeIndex = nodes.length + slotIndex;
const spacing = 20;
const x = 100 + (nodeIndex % 3) * (dimensions.width + spacing);
@ -10447,7 +10442,7 @@ export function CanvasChatPanel({
buildCanvasUploadedImageAttachment({ file, imageUrl, assetId })
);
const dimensions = await getImageDimensions(file);
const dimensions = await getImageDimensions(imageUrl);
const nodeIndex = nodes.length + slotIndex;
const spacing = 20;
const x = 100 + (nodeIndex % 3) * (dimensions.width + spacing);
@ -13733,7 +13728,7 @@ export function CanvasChatPanel({
};
onUploaded?.(uploadedBase);
clearComposerUploadProgress(250);
const dimensions = await getImageDimensions(file);
const dimensions = await getImageDimensions(imageUrl);
const spacing = 20;
const x = 100 + (nodeIndex % 3) * (dimensions.width + spacing);
const y = 100 + Math.floor(nodeIndex / 3) * (dimensions.height + spacing);
@ -14922,7 +14917,7 @@ export function CanvasChatPanel({
firstImageUrl = imageUrl;
}
const dimensions = await getImageDimensions(file);
const dimensions = await getImageDimensions(imageUrl);
updateUploadedImageAttachmentDimensions(imageUrl, dimensions);
// 获取图片原始尺寸后添加到画布
@ -15898,9 +15893,6 @@ export function CanvasChatPanel({
</div>
<div className="flex shrink-0 items-center">
<div className="flex items-center gap-1">
<div className={isVideoWorkspace ? "" : "sm:hidden"}>
<CanvasFeedbackDialog projectId={canvasId} />
</div>
<Button
variant="ghost"
size="icon"

View File

@ -1,383 +0,0 @@
"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>
);
}

View File

@ -19,7 +19,6 @@ const PROJECT_LIST_PAGE_SIZE = 24;
interface CanvasFileListPanelProps {
currentId: string;
onClose: () => void;
onNavigate?: (url: string) => void;
workspaceType?: CanvasWorkspaceType;
workspaceLabel?: string;
createUrl?: string;
@ -29,7 +28,6 @@ interface CanvasFileListPanelProps {
export function CanvasFileListPanel({
currentId,
onClose,
onNavigate,
workspaceType = "canvas",
workspaceLabel = "画布",
createUrl = "/canvas?new=1",
@ -90,10 +88,6 @@ export function CanvasFileListPanel({
// 创建新画布
const handleCreateNew = async () => {
if (onNavigate) {
onNavigate(createUrl);
return;
}
router.push(createUrl);
};
@ -106,10 +100,6 @@ export function CanvasFileListPanel({
}
const targetUrl = buildProjectUrl(id);
onClose(); // 关闭文件列表面板
if (onNavigate) {
onNavigate(targetUrl);
return;
}
// 强制整页跳转,避免仅更新 URL 不刷新
setTimeout(() => {
window.location.assign(targetUrl);

View File

@ -28,7 +28,6 @@ export function CanvasImageDownloadProgressDialog({
: null;
const loadedLabel = formatBytes(progress.loadedBytes);
const totalLabel = formatBytes(progress.totalBytes);
const converting = progress.phase === "converting";
return createPortal(
<div
@ -42,12 +41,12 @@ export function CanvasImageDownloadProgressDialog({
{progress.phase === "saving" ? (
<Download className="h-4 w-4" />
) : (
<Loader2 className="h-4 w-4 motion-safe:animate-spin" />
<Loader2 className="h-4 w-4 animate-spin" />
)}
</span>
<div>
<div className="text-[14px] font-semibold">下载原图</div>
<div className="mt-0.5 text-[12px] text-white/58" role="status">{progress.message}</div>
<div className="mt-0.5 text-[12px] text-white/58">{progress.message}</div>
</div>
</div>
{onCancel ? (
@ -64,19 +63,14 @@ export function CanvasImageDownloadProgressDialog({
<div className="mt-4">
<div className="mb-2 flex items-center justify-between text-[12px] text-white/55">
<span>{loadedLabel && totalLabel ? `${loadedLabel} / ${totalLabel}` : converting ? "格式转换中" : "正在处理文件"}</span>
<span>{loadedLabel && totalLabel ? `${loadedLabel} / ${totalLabel}` : "正在处理文件"}</span>
<span>{percent !== null ? `${percent}%` : "..."}</span>
</div>
<div
className="relative h-2 overflow-hidden rounded-full bg-white/10"
role="progressbar"
aria-label={converting ? "图片格式转换" : "图片下载"}
aria-valuenow={percent ?? undefined}
>
<div className="relative h-2 overflow-hidden rounded-full bg-white/10">
<div
className={cn(
"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 motion-safe:animate-[download-indeterminate_1.15s_ease-in-out_infinite]"
percent === null && "w-1/3 animate-[download-indeterminate_1.15s_ease-in-out_infinite]"
)}
style={percent !== null ? { width: `${percent}%` } : undefined}
/>

View File

@ -1,14 +1,16 @@
"use client";
import { ReferenceIcon } from "@/components/lovart/reference-icon";
import { CanvasFeedbackDialog } from "./CanvasFeedbackDialog";
import { Loader2, Save } from "lucide-react";
type CanvasSurfaceHeaderProps = {
isSaving: boolean;
onAddImage: () => void;
onHome: () => void;
onOpenFiles: () => void;
onSave: () => void;
projectTitle: string;
projectId: string;
rightInset: number;
};
@ -16,11 +18,12 @@ 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";
export function CanvasSurfaceHeader({
isSaving,
onAddImage,
onHome,
onOpenFiles,
onSave,
projectTitle,
projectId,
rightInset,
}: CanvasSurfaceHeaderProps) {
return (
@ -74,7 +77,19 @@ export function CanvasSurfaceHeader({
<ReferenceIcon name="plus" size={16} />
</button>
</div>
<CanvasFeedbackDialog projectId={projectId} />
<button
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>
);
}

View File

@ -1,8 +1,6 @@
"use client";
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
import { useCanvasGroupRuns } from "@/hooks/use-canvas-group-runs";
import { hasRecoverableCanvasTask } from "@/lib/video-canvas/task-recovery";
import { restoreTextResourceContent } from "@/lib/video-canvas/text-resource";
import { GroupStoryboardPreview } from "./GroupStoryboardPreview";
import { CanvasGroupToolbar } from "./CanvasGroupToolbar";
import { isCanvasGroup, groupMembers, makeCanvasGroup, ungroupCanvasNodes, moveCanvasSelection, layoutCanvasGroup, migrateLegacyGroups, exportCanvasGroup, instantiateCanvasGroup, groupBounds, assignGroupMembers } from "@/lib/video-canvas/groups";
@ -4146,7 +4144,7 @@ function normalizeTextResourceNode(node: CloneNode, fallbackIndex: number): Clon
draftValues: {
...(node.draftValues || {}),
action: "TEXT_RESOURCE",
content: restoreTextResourceContent(node.draftValues),
content: Array.isArray(node.draftValues?.content) ? node.draftValues?.content : [],
nodeType: "text",
scriptRole: "resource",
viewMode: undefined,
@ -15106,7 +15104,7 @@ function DirectorNodeComposer({
if(file.size>20*1024*1024){setError("参考图需小于 20 MiB");return;}
setBusy(true);setError("");try{
const form=new FormData();form.append("file",file);form.append("purpose","director-studio");
const response=await fetchUploadWithRetry(form,{signal:AbortSignal.timeout(120000)});
const response=await fetch("/api/files/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 new Error(result.error||"图片上传失败");
onDraft({directorReferenceUrl:String(url)});
@ -15627,8 +15625,7 @@ function VideoTargetModuleComposer({
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 resolution = executableResolutions.length && !executableResolutions.includes(normalizeVideoResolutionValue(current.resolution)) ? executableResolutions[0].toUpperCase() : current.resolution;
const durations = getCanvasVideoParameters(target, activeMode, resolution || "720P").durations;
const duration = durations.length && !durations.includes(normalizeVideoDurationValue(current.duration)) ? `${durations[0]}s` : current.duration;
const duration = supported.durations.length && !supported.durations.includes(normalizeVideoDurationValue(current.duration)) ? `${supported.durations[0]}s` : current.duration;
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};
});
@ -17931,7 +17928,7 @@ function VideoParameterEditor({
const candidates = filterCanvasModels(modelOptions, audioRequired ? "audio-video" : "video", mode);
const model = candidates.find(item => item.id === settings.model || item.modelId === settings.model) ||
(!normalizeModelRef(settings.model) || settings.model === "auto" ? candidates[0] : undefined);
const options = getCanvasVideoParameters(model, mode, settings.resolution || "720P");
const options = getCanvasVideoParameters(model, mode);
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)));
return (
@ -20841,18 +20838,8 @@ function PresetMediaCanvasNode({
</div>
) : mediaType === "audio" ? (
<div className="relative h-[calc(100%-32px)]">
{isGenerating || isGenerationFailed ? (
<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 ? (
<OfficialAudioCard durationSeconds={getDraftNumber(node, "durationSeconds", 3)} mediaUrl={mediaUrl} selected={selected} />
{!generated ? (
<button
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"
@ -21398,14 +21385,12 @@ function GenerationPendingPreview({
previewUrl,
progress,
status,
statusLabel,
}: {
errorMessage?: string;
onCancel: () => void;
previewUrl?: string;
progress: number;
status: string;
statusLabel?: string;
}) {
const displayProgress = Math.max(2, Math.min(99, Math.round(progress)));
const failed = status === "failed";
@ -21451,7 +21436,7 @@ function GenerationPendingPreview({
</>
) : (
<>
<span>{statusLabel || `生成中 ${displayProgress}%...`}</span>
<span>生成中 {displayProgress}%...</span>
<button
className="font-medium text-white/42 transition hover:text-white/80"
onClick={(event) => {
@ -21479,7 +21464,11 @@ function PureTextResourceCanvasNode({
onTextResourceChange: (content: string) => void;
selected: boolean;
}) {
const textContent = restoreTextResourceContent(node.draftValues)[0] || "";
const contentValues = node.draftValues?.content;
const textContent =
Array.isArray(contentValues) && typeof contentValues[0] === "string"
? contentValues[0]
: "";
return (
<div className="h-full w-full">

View File

@ -1,5 +1,4 @@
"use client";
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
import { useState } from "react";
import { toast } from "sonner";
import { readDirectorScene, type DirectorScene } from "@/lib/video-canvas/director-scene";
@ -65,7 +64,9 @@ export function DirectorReconstructionPanel({
const form = new FormData();
form.append("file", file);
form.append("purpose", "director-studio");
const response = await fetchUploadWithRetry(form, {
const response = await fetch("/api/files/upload", {
method: "POST",
body: form,
signal: AbortSignal.timeout(120000),
});
const data = await response.json();

View File

@ -1,5 +1,4 @@
"use client";
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
import {
commitDirectorScene,
resolveDirectorCamera,
@ -44,13 +43,14 @@ async function upload(file: File) {
"purpose",
file.type.startsWith("video/") ? "canvas-video-process" : "director-studio",
);
const response = /\.glb$/i.test(file.name)
? await fetch("/api/canvas/director/upload-model", {
const response = await fetch(
/\.glb$/i.test(file.name) ? "/api/canvas/director/upload-model" : "/api/files/upload",
{
method: "POST",
body: form,
signal: AbortSignal.timeout(120000),
})
: await fetchUploadWithRetry(form, { signal: AbortSignal.timeout(120000) });
},
);
const result = await response.json();
const url = result.url || result.data?.url;
if (!response.ok || !url) throw new Error(result.error || "上传失败");

View File

@ -178,7 +178,7 @@ import {
} from "@/components/ui/tooltip";
import { fetchUploadWithRetry } from "@/lib/client/upload-with-retry";
import {
downloadOriginalImage,
downloadCanvasImageAsPng,
getCanvasImageDownloadErrorMessage,
type CanvasImageDownloadProgress,
} from "@/lib/client/canvas-image-download";
@ -1663,10 +1663,6 @@ export function ImageNode({
}, [publicModelAliasConfig.worldModels]);
const [isHovered, setIsHovered] = useState(false);
const [isProcessing, setIsProcessing] = useState(false);
const [toolbarFailure, setToolbarFailure] = useState<{
imageUrl: string;
message: string;
} | null>(null);
const [isCreatingDetachedResultNode, setIsCreatingDetachedResultNode] =
useState(false);
const [imageAdjustPanelOpen, setImageAdjustPanelOpen] = useState(false);
@ -2486,11 +2482,14 @@ export function ImageNode({
[compareButtonVisualSize]
);
const shouldUseHighFidelityDisplayImage =
isSelected ||
isHovered ||
isCompareMode ||
boxCutoutMode ||
localEditMode ||
cropPanelOpen ||
imageAdjustPanelOpen ||
fullscreenPreviewOpen ||
isThisNodePicking;
const displayImageWidth = useMemo(() => {
const devicePixelRatio =
@ -2523,6 +2522,15 @@ export function ImageNode({
}),
[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(
() =>
buildImageProxyUrl(compareSource, {
@ -4683,7 +4691,6 @@ export function ImageNode({
}
setIsCreatingDetachedResultNode(false);
setToolbarFailure(null);
setIsProcessing(true);
const actionName = "框选抠图";
@ -4701,10 +4708,8 @@ export function ImageNode({
await runCanvasRemoteOperation(() => executeCanvasToolbarCapability(command));
toast.success(`${actionName}完成`);
} catch (error) {
setToolbarFailure({
imageUrl: data.src,
message: error instanceof Error ? error.message : `${actionName}失败`,
});
console.error("框选抠图失败:", error);
toast.error(error instanceof Error ? error.message : `${actionName}失败`);
} finally {
setIsCreatingDetachedResultNode(false);
setIsProcessing(false);
@ -6658,7 +6663,6 @@ export function ImageNode({
}
setIsCreatingDetachedResultNode(false);
setToolbarFailure(null);
setIsProcessing(true);
try {
const command = createCanvasCapabilityClientCommand({
@ -6670,10 +6674,9 @@ export function ImageNode({
await runCanvasRemoteOperation(() => executeCanvasToolbarCapability(command));
toast.success(`${capability.title}完成`);
} catch (error) {
setToolbarFailure({
imageUrl: data.src,
message: error instanceof Error ? error.message : `${capability.title}失败`,
});
const message =
error instanceof Error ? error.message : `${capability.title}失败`;
toast.error(message);
} finally {
setIsCreatingDetachedResultNode(false);
setIsProcessing(false);
@ -8232,6 +8235,7 @@ export function ImageNode({
}
};
// 下载图片 - 服务端统一转为 Photoshop 兼容 PNG,避免后缀和真实格式错配。
const isDownloadingOriginalImage = Boolean(downloadProgress);
const handleDownload = async () => {
if (!data.src) return;
@ -8245,16 +8249,24 @@ export function ImageNode({
setDownloadProgress({
phase: "preparing",
message: "正在准备原图...",
percent: 5,
});
try {
await downloadOriginalImage({
url: resolveOriginalImageUrl(data.src) || data.src,
await downloadCanvasImageAsPng({
url: data.src,
name: displayTitle,
candidates: [
displayImageSrc,
fullscreenPreviewSrc,
buildImageProxyUrl(resolveOriginalImageUrl(data.src) || data.src, {
delivery: "proxy",
}),
],
signal: controller.signal,
onProgress: setDownloadProgress,
});
toast.success("已开始下载");
toast.success("原图已开始下载");
} catch (error) {
if (controller.signal.aborted) {
toast.message("已取消下载");
@ -9435,29 +9447,6 @@ 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,避免被裁剪 */}
{/* 悬浮时的更多操作按钮 (右上角) */}

View File

@ -5,7 +5,7 @@ import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import { ChevronDown, ChevronUp, Link2, Scan, SlidersHorizontal, Sparkles } from "lucide-react";
import { getGrsaiGptImageOutputSize } from "@/lib/ai/grsai-gpt-image-sizes";
import { getVip98kQualityOptions, resolveVip98kImageTaskSize, type Vip98kTaskInvocation } from "@/lib/ai/vip98k-task-invocation";
import { resolveVip98kImageTaskSize } from "@/lib/ai/vip98k-task-invocation";
import { cn } from "@/lib/utils";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
@ -207,7 +207,7 @@ export function ImageParamsBar({
normalizeImageSizeValue(size, "")
)
: []
).filter((size) => providerType !== "alibaba-image" || size === "1K" || size === "2K");
);
const buildImageSizeOptions = () => {
if (imageGenConfig?.supportsSize === false) {
@ -220,7 +220,6 @@ export function ImageParamsBar({
return DEFAULT_IMAGE_SIZE_OPTIONS.filter((option) => {
if (option.disabled) return false;
if (providerType === "alibaba-image" && option.value === "4K") return false;
if (!imageGenConfig) return true;
if (option.value === "1K") return imageGenConfig.supports1K !== false;
if (option.value === "2K") return imageGenConfig.supports2K !== false;
@ -268,17 +267,6 @@ export function ImageParamsBar({
availableAspectRatioOptions[0]?.value ||
"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 options = getImageCountOptions();
const fallbackCount = options[0] ?? 1;
@ -297,7 +285,6 @@ export function ImageParamsBar({
aspectRatio,
imageSize,
imageCount,
quality: qualityOptions.some(option => option.value === raw.quality) ? raw.quality : qualityOptions[0]?.value || "auto",
};
};
@ -334,6 +321,12 @@ export function ImageParamsBar({
...buildBaseParams(initialParams),
});
const [minimalParamsOpen, setMinimalParamsOpen] = useState(false);
const qualityOptions = [
{ label: "自动", value: "auto" },
{ label: "高", value: "high" },
{ label: "中", value: "medium" },
{ label: "低", value: "low" },
] as const;
const qualityLabel =
qualityOptions.find((option) => option.value === params.quality)?.label || "中";

View File

@ -1,21 +0,0 @@
"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 />;
}

View File

@ -33,9 +33,6 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { runBatchInstructionEdit, BatchInstructionEditError } from "@/lib/client/batch-instruction-edit";
import { downloadCanvasSelectionZip } from "@/lib/client/canvas-zip-download";
import { Label } from "@/components/ui/label";
import { ScrollArea } from "@/components/ui/scroll-area";
import {
@ -62,7 +59,6 @@ import type { AgentWorkflowProductDetailChannel } from "@/types/agent";
const fetcher = (url: string) => fetch(url).then((res) => res.json());
const MAX_BATCH_ITEMS = 10;
const imageFileAccept = "image/png,image/jpeg,image/webp,image/heic,image/heif,.heic,.heif";
const BATCH_REQUEST_STAGGER_MS = 700;
const BATCH_PARALLEL_WORKER_COUNT = 4;
const MULTI_PRODUCT_FAILOVER_MAX_ROUNDS = 5;
@ -104,7 +100,6 @@ function formatQueueWaitLabel(estimatedWaitMs?: number | null) {
type BatchMode =
| "scene-fission"
| "multi-product-scene"
| "instruction-edit"
| "random-bg"
| "rmbg"
| "enhance"
@ -121,8 +116,6 @@ interface ProcessItem {
error?: string;
targetName?: string;
customPrompt?: string;
instructionTaskId?: string;
instructionSubmitted?: boolean;
queueStatus?: {
queuePosition?: number;
sameModelPendingCount?: number;
@ -171,10 +164,6 @@ type SceneFusionPersistedState = {
randomCount: number;
randomPrompt: string;
batchItems: ProcessItem[];
instructionItems: ProcessItem[];
instructionText: string;
instructionReference: string | null;
instructionSquare: boolean;
selectedResultIds: string[];
};
@ -267,9 +256,8 @@ function createPersistableSnapshot(snapshot: SceneFusionPersistedState): SceneFu
const multiProductItems = toPersistableItems(snapshot.multiProductItems);
const randomItems = toPersistableItems(snapshot.randomItems);
const batchItems = toPersistableItems(snapshot.batchItems);
const instructionItems = toPersistableItems(snapshot.instructionItems);
const persistedIds = new Set(
[...sceneItems, ...multiProductItems, ...randomItems, ...batchItems, ...instructionItems].map(
[...sceneItems, ...multiProductItems, ...randomItems, ...batchItems].map(
(item) => item.id
)
);
@ -283,8 +271,6 @@ function createPersistableSnapshot(snapshot: SceneFusionPersistedState): SceneFu
toPersistableImageValue(snapshot.referenceSceneImage) || null,
randomItems,
batchItems,
instructionItems,
instructionReference: toPersistableImageValue(snapshot.instructionReference) || null,
selectedResultIds: snapshot.selectedResultIds.filter((id) =>
persistedIds.has(id)
),
@ -339,12 +325,6 @@ const modeOptions: Array<{
description: "10张产品图 + 1张参考图",
icon: ImageIcon,
},
{
id: "instruction-edit",
label: "批量指令编辑",
description: "多张原图执行同一条指令",
icon: Wand2,
},
{
id: "random-bg",
label: "随机换背景",
@ -383,8 +363,6 @@ function getModeActionLabel(mode: BatchMode, count: number) {
return `开始裂变 (${count} 张)`;
case "multi-product-scene":
return `开始多产品替换 (${count} 张)`;
case "instruction-edit":
return `开始编辑 (${count} 张)`;
case "random-bg":
return `开始换背景 (${count} 张)`;
case "rmbg":
@ -532,16 +510,6 @@ export function SceneFusionView({
const [randomPrompt, setRandomPrompt] = useState("");
const [batchItems, setBatchItems] = useState<ProcessItem[]>([]);
const [instructionItems, setInstructionItems] = useState<ProcessItem[]>([]);
const [instructionText, setInstructionText] = useState("");
const [instructionReference, setInstructionReference] = useState<string | null>(null);
const [instructionSquare, setInstructionSquare] = useState(false);
const [isInstructionUploading, setIsInstructionUploading] = useState(false);
const [zipStage, setZipStage] = useState("");
const [isZipping, setIsZipping] = useState(false);
const isInstructionMode = mode === "instruction-edit";
const activeBatchItems = isInstructionMode ? instructionItems : batchItems;
const setActiveBatchItems = isInstructionMode ? setInstructionItems : setBatchItems;
const [previewImageUrl, setPreviewImageUrl] = useState<string | null>(null);
const [previewImageTitle, setPreviewImageTitle] =
useState<string>("处理结果预览");
@ -554,15 +522,14 @@ export function SceneFusionView({
const multiProductInputRef = useRef<HTMLInputElement>(null);
const referenceSceneInputRef = useRef<HTMLInputElement>(null);
const batchInputRef = useRef<HTMLInputElement>(null);
const instructionReferenceInputRef = useRef<HTMLInputElement>(null);
const productPreviewObjectUrlRef = useRef<string | null>(null);
const resultItems = useMemo(() => {
if (mode === "scene-fission") return sceneItems;
if (mode === "multi-product-scene") return multiProductItems;
if (mode === "random-bg") return randomItems;
return activeBatchItems;
}, [activeBatchItems, mode, multiProductItems, randomItems, sceneItems]);
return batchItems;
}, [batchItems, mode, multiProductItems, randomItems, sceneItems]);
const resultQueueMetaById = useMemo(
() =>
new Map(
@ -580,18 +547,9 @@ export function SceneFusionView({
if (mode === "multi-product-scene")
return multiProductItems.length > 0 && Boolean(referenceSceneImage);
if (mode === "random-bg") return Boolean(productImage) && randomCount > 0;
if (isInstructionMode) {
return config?.enabled !== false && !isInstructionUploading && instructionItems.length > 0 &&
Boolean(instructionText.trim() || instructionItems.every(item => item.instructionSubmitted));
}
return batchItems.length > 0;
}, [
batchItems.length,
config?.enabled,
instructionItems,
instructionText,
isInstructionMode,
isInstructionUploading,
mode,
multiProductItems.length,
productImage,
@ -787,12 +745,6 @@ export function SceneFusionView({
if (Array.isArray(parsed.batchItems)) {
setBatchItems(toPersistableItems(parsed.batchItems));
}
if (Array.isArray(parsed.instructionItems)) {
setInstructionItems(toPersistableItems(parsed.instructionItems));
}
if (typeof parsed.instructionText === "string") setInstructionText(parsed.instructionText);
setInstructionReference(toPersistableImageValue(parsed.instructionReference) || null);
setInstructionSquare(parsed.instructionSquare === true);
if (Array.isArray(parsed.selectedResultIds)) {
setSelectedResultIds(parsed.selectedResultIds.map((value) => String(value || "").trim()).filter(Boolean));
}
@ -883,10 +835,6 @@ export function SceneFusionView({
randomCount,
randomPrompt,
batchItems,
instructionItems,
instructionText,
instructionReference,
instructionSquare,
selectedResultIds,
};
@ -906,10 +854,6 @@ export function SceneFusionView({
}, [
aspectRatio,
batchItems,
instructionItems,
instructionText,
instructionReference,
instructionSquare,
imageSize,
mode,
multiProductItems,
@ -1155,21 +1099,6 @@ export function SceneFusionView({
}
};
const handleInstructionReferenceUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
const input = event.currentTarget;
const [file] = validateImageFiles(Array.from(input.files || []));
input.value = "";
if (!file) return;
setIsInstructionUploading(true);
try {
setInstructionReference(await uploadImageFile(file));
} catch (error) {
toast.error(error instanceof Error ? error.message : "参考图上传失败");
} finally {
setIsInstructionUploading(false);
}
};
const handleBatchUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
const input = e.currentTarget;
const files = validateImageFiles(Array.from(input.files || []));
@ -1177,16 +1106,15 @@ export function SceneFusionView({
input.value = "";
return;
}
if (activeBatchItems.length + files.length > MAX_BATCH_ITEMS) {
if (batchItems.length + files.length > MAX_BATCH_ITEMS) {
toast.error(`单次最多上传 ${MAX_BATCH_ITEMS} 张图片`);
input.value = "";
return;
}
if (isInstructionMode) setIsInstructionUploading(true);
try {
const uploadedItems = await uploadImageFiles(files);
setActiveBatchItems((prev) => [
setBatchItems((prev) => [
...prev,
...uploadedItems.map((item) => ({
id: Math.random().toString(36).slice(2, 10),
@ -1198,7 +1126,6 @@ export function SceneFusionView({
} catch (error) {
toast.error(error instanceof Error ? error.message : "图片上传失败");
} finally {
if (isInstructionMode) setIsInstructionUploading(false);
input.value = "";
}
};
@ -1218,7 +1145,7 @@ export function SceneFusionView({
};
const removeBatchItem = (id: string) => {
setActiveBatchItems((prev) => prev.filter((item) => item.id !== id));
setBatchItems((prev) => prev.filter((item) => item.id !== id));
};
const updateBatchItemsStatus = (ids: string[], status: ItemStatus) => {
@ -1819,33 +1746,6 @@ export function SceneFusionView({
};
const processBatchImageItem = async (item: ProcessItem) => {
if (isInstructionMode) {
if (!item.instructionSubmitted && !instructionText.trim()) {
throw new Error("请填写统一编辑指令");
}
const taskId = item.instructionTaskId || crypto.randomUUID();
const updateTask = (patch: Partial<ProcessItem>) => setInstructionItems(items =>
items.map(current => current.id === item.id ? { ...current, ...patch } : current)
);
updateTask({ instructionTaskId: taskId });
try {
const result = await runBatchInstructionEdit({
taskId,
image: new URL(item.image, window.location.origin).href,
reference: instructionReference ? new URL(instructionReference, window.location.origin).href : undefined,
instruction: instructionText.trim(),
square: instructionSquare,
submitted: item.instructionSubmitted,
}, () => updateTask({ instructionSubmitted: true }));
updateTask({ instructionTaskId: undefined, instructionSubmitted: undefined });
return result;
} catch (error) {
if (error instanceof BatchInstructionEditError && error.canStartNewTask) {
updateTask({ instructionTaskId: undefined, instructionSubmitted: undefined });
}
throw error;
}
}
if (mode === "rmbg") {
const response = await fetch("/api/rmbg", {
method: "POST",
@ -1928,16 +1828,16 @@ export function SceneFusionView({
};
const runBatchImageMode = async () => {
if (activeBatchItems.length === 0) {
if (batchItems.length === 0) {
toast.error("请先上传图片");
return;
}
let itemsToProcess = activeBatchItems.filter(
(item) => item.status === "pending" || item.status === "failed" || (isInstructionMode && item.status === "processing")
let itemsToProcess = batchItems.filter(
(item) => item.status === "pending" || item.status === "failed"
);
if (itemsToProcess.length === 0) {
itemsToProcess = activeBatchItems;
itemsToProcess = batchItems;
}
if (itemsToProcess.length > MAX_BATCH_ITEMS) {
@ -1946,7 +1846,7 @@ export function SceneFusionView({
}
setIsProcessing(true);
setActiveBatchItems((prev) =>
setBatchItems((prev) =>
prev.map((item) =>
itemsToProcess.some((target) => target.id === item.id)
? markProcessItemRegenerating(item, "pending")
@ -1966,7 +1866,7 @@ export function SceneFusionView({
while (queue.length > 0) {
const current = queue.shift();
if (!current) break;
setActiveBatchItems((prev) =>
setBatchItems((prev) =>
prev.map((item) =>
item.id === current.id
? markProcessItemRegenerating(item, "processing")
@ -1976,7 +1876,7 @@ export function SceneFusionView({
try {
const result = await processBatchImageItem(current);
successCount += 1;
setActiveBatchItems((prev) =>
setBatchItems((prev) =>
prev.map((item) =>
item.id === current.id
? {
@ -1992,7 +1892,7 @@ export function SceneFusionView({
} catch (error) {
const message = error instanceof Error ? error.message : "处理失败";
failedCount += 1;
setActiveBatchItems((prev) =>
setBatchItems((prev) =>
prev.map((item) =>
item.id === current.id
? {
@ -2028,7 +1928,7 @@ export function SceneFusionView({
}
setIsProcessing(true);
setActiveBatchItems((prev) =>
setBatchItems((prev) =>
prev.map((batchItem) =>
batchItem.id === item.id
? markProcessItemRegenerating(batchItem, "processing")
@ -2038,7 +1938,7 @@ export function SceneFusionView({
try {
const result = await processBatchImageItem(item);
setActiveBatchItems((prev) =>
setBatchItems((prev) =>
prev.map((batchItem) =>
batchItem.id === item.id
? {
@ -2054,7 +1954,7 @@ export function SceneFusionView({
toast.success("已重新处理 1 张图片");
} catch (error) {
const message = error instanceof Error ? error.message : "处理失败";
setActiveBatchItems((prev) =>
setBatchItems((prev) =>
prev.map((batchItem) =>
batchItem.id === item.id
? { ...batchItem, status: "failed", error: message }
@ -2189,30 +2089,6 @@ export function SceneFusionView({
toast.error("没有可下载的图片");
return;
}
if (isInstructionMode) {
if (isZipping) return;
setIsZipping(true);
setZipStage("正在准备下载");
try {
const task = await downloadCanvasSelectionZip({
folderName: "批量指令编辑",
files: completed.map(item => ({
url: getProcessItemSourceUrl(item),
name: `编辑结果 ${resultItems.indexOf(item) + 1}`,
})),
selectionKey: persistenceKey || "batch-instruction",
scope: "batch-instruction",
onProgress: task => setZipStage(task.stage),
});
toast.success(task.failedCount ? `已开始下载,${task.failedCount} 张未打包` : "已开始下载 ZIP");
} catch (error) {
toast.error(error instanceof Error ? error.message : "无法下载 ZIP");
} finally {
setIsZipping(false);
setZipStage("");
}
return;
}
for (let i = 0; i < completed.length; i += 1) {
const item = completed[i]!;
await downloadImage(
@ -2344,7 +2220,7 @@ export function SceneFusionView({
<div className="space-y-2">
<Label className="font-medium text-base">处理模式</Label>
<Select
disabled={isProcessing || isInstructionUploading}
disabled={isProcessing}
onValueChange={(value) => setMode(value as BatchMode)}
value={mode}
>
@ -2947,20 +2823,19 @@ export function SceneFusionView({
</div>
)}
{(isInstructionMode ||
mode === "rmbg" ||
{(mode === "rmbg" ||
mode === "enhance" ||
mode === "extend" ||
mode === "watermark") && (
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label className="font-medium text-base">
待处理图片 ({activeBatchItems.length}/{MAX_BATCH_ITEMS})
待处理图片 ({batchItems.length}/{MAX_BATCH_ITEMS})
</Label>
<Button
className="h-7 gap-1 text-xs"
disabled={
activeBatchItems.length >= MAX_BATCH_ITEMS || isProcessing || isInstructionUploading
batchItems.length >= MAX_BATCH_ITEMS || isProcessing
}
onClick={() => batchInputRef.current?.click()}
size="sm"
@ -2971,7 +2846,6 @@ export function SceneFusionView({
<input
accept="image/png,image/jpeg,image/webp,image/heic,image/heif,.heic,.heif"
className="hidden"
aria-label="上传待处理图片"
multiple
onChange={handleBatchUpload}
ref={batchInputRef}
@ -2980,10 +2854,10 @@ export function SceneFusionView({
</div>
<div className="space-y-3">
{activeBatchItems.length === 0 && (
{batchItems.length === 0 && (
<div
className="cursor-pointer rounded-lg border-2 border-muted-foreground/25 border-dashed p-8 text-center transition-colors hover:bg-muted/50"
onClick={() => !isProcessing && !isInstructionUploading && batchInputRef.current?.click()}
onClick={() => batchInputRef.current?.click()}
>
<ImageIcon className="mx-auto mb-2 h-8 w-8 opacity-20" />
<p className="text-muted-foreground text-sm">
@ -2995,7 +2869,7 @@ export function SceneFusionView({
</div>
)}
{activeBatchItems.map((item, index) => (
{batchItems.map((item, index) => (
<div
className="group relative rounded-lg border bg-background p-3"
key={item.id}
@ -3023,7 +2897,7 @@ export function SceneFusionView({
: "失败"}
</div>
{item.error && (
<div title={item.error} className="mt-1 line-clamp-2 text-destructive text-xs">
<div className="mt-1 line-clamp-2 text-destructive text-xs">
{item.error}
</div>
)}
@ -3044,58 +2918,6 @@ export function SceneFusionView({
</div>
</div>
)}
{isInstructionMode && (
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="batch-edit-instruction">统一编辑指令</Label>
<Textarea
id="batch-edit-instruction"
disabled={isProcessing}
maxLength={3000}
onChange={(event) => setInstructionText(event.target.value)}
placeholder="例如,把商品背景改成白色,保持商品、文字和构图"
value={instructionText}
rows={4}
/>
</div>
<div className="space-y-2">
<Label>共用参考图(可选)</Label>
<div className="flex items-center gap-2">
{instructionReference && (
<img alt="共用参考图" className="size-16 rounded-md border bg-muted object-contain" src={instructionReference} />
)}
<input
accept={imageFileAccept}
aria-label="上传共用参考图"
className="hidden"
onChange={handleInstructionReferenceUpload}
ref={instructionReferenceInputRef}
type="file"
/>
<Button disabled={isProcessing || isInstructionUploading} onClick={() => instructionReferenceInputRef.current?.click()} size="sm" variant="outline">
{instructionReference ? "更换参考图" : "上传参考图"}
</Button>
{instructionReference && (
<Button disabled={isProcessing || isInstructionUploading} onClick={() => setInstructionReference(null)} size="sm" variant="ghost">
移除
</Button>
)}
</div>
<p className="text-muted-foreground text-xs">换脸或换商品时,在指令里写清参考图要提供的特征。</p>
</div>
<div className="space-y-2">
<Label htmlFor="batch-edit-ratio">输出比例</Label>
<Select disabled={isProcessing} onValueChange={(value) => setInstructionSquare(value === "square")} value={instructionSquare ? "square" : "original"}>
<SelectTrigger id="batch-edit-ratio" className="h-10"><SelectValue /></SelectTrigger>
<SelectContent className="z-[30000] bg-background">
<SelectItem value="original">原图比例</SelectItem>
<SelectItem value="square">1:1 · 保留完整画面并补边</SelectItem>
</SelectContent>
</Select>
</div>
</div>
)}
</div>
</div>
@ -3173,9 +2995,9 @@ export function SceneFusionView({
</Button>
)}
{completedResultItems.length > 0 && (
<Button disabled={isInstructionMode && isZipping} onClick={downloadAll} size="sm" variant="outline">
<Button onClick={downloadAll} size="sm" variant="outline">
<Download className="mr-2 h-4 w-4" />
{isInstructionMode ? (zipStage || "批量下载 ZIP") : "全部下载"}
全部下载
</Button>
)}
</div>
@ -3341,15 +3163,9 @@ export function SceneFusionView({
</div>
) : item.status === "failed" ? (
<div className="relative z-10 px-3 text-center">
<p title={item.error} className="line-clamp-2 rounded bg-black/55 px-2 py-1 text-white text-xs">
<p className="line-clamp-2 rounded bg-black/55 px-2 py-1 text-white text-xs">
{item.error || "处理失败"}
</p>
{isInstructionMode && (
<Button className="mt-2" disabled={isProcessing || isInstructionUploading || (!item.instructionSubmitted && !instructionText.trim())}
onClick={(event) => { event.stopPropagation(); void retrySingleItem(item); }} size="sm" variant="secondary">
{item.instructionTaskId ? "查询原任务" : "重试这张"}
</Button>
)}
</div>
) : (
<div className="relative z-10 max-w-[90%] rounded bg-black/55 px-2.5 py-1.5 text-center text-white text-xs">
@ -3409,7 +3225,6 @@ export function SceneFusionView({
event.stopPropagation();
retrySingleItem(item);
}}
disabled={isProcessing || (isInstructionMode && (isInstructionUploading || !instructionText.trim()))}
size="icon"
variant="secondary"
>

View File

@ -1,39 +0,0 @@
# 画布问题反馈
画布顶部提供“问题反馈”;小窗口及视频画布在对话顶部提供入口。用户确认关联请求、补充说明后发送。反馈不调用画布保存或生成接口。
## 资料与权限
- 用户身份由登录会话及服务端用户表确定;收件人固定为准确花名“魔礼红”。
- 请求按当前用户、画布及对话查询。图片任务通过当前对话请求号或画布节点任务号关联,不查询账号下任意最近任务。
- 服务端只读取任务表,不调用带恢复、执行或状态变更副作用的任务查询接口。
- 自动采集当前对话最近 12 条公开消息(文字总量最多 48,000 字符)、最多 20 个节点的编号、尺寸及任务号,以及页面版本、窗口、网络和最近脚本错误。截图、图片二进制、内部提示词、Cookie 和认证头不采集。
- 用户可以切换关联请求或选择页面问题。尚未持久化的消息标记为浏览器来源;缺少请求号时保留消息或任务编号,不推测编号。
- 完整反馈保存在 `CanvasFeedback`;通知正文为限长摘要,按钮打开 `/feedback/{id}`。详情仅反馈本人及平台管理员可读。画布删除后反馈仍可查看,账号删除会级联删除其反馈。
## 提交和投递
- 浏览器在首次提交时固定反馈 UUID 和请求内容,在当前标签页保留提交资料。重复点击、响应丢失后的重试和刷新恢复均沿用原 UUID。
- 服务端按 UUID 插入一次,持久化的通知内容保持不变。通知中心幂等键为 `canvas-feedback:{id}:v1`;每次 HTTP 请求生成新 nonce。
- 保存成功后异步提交通知。节点运行时每分钟继续处理尚未入队的反馈,最多尝试 5 次;连接异常、5xx 和 429 按 1、5、15、30 分钟间隔重试,其他明确拒绝停止重试。
- 反馈与调度时间统一为 UTC;首次调度显式写入应用时间,数据库默认值也使用 UTC。普通画布能力的默认创建时间按数据库时区转换,视频队列显式写入的 UTC 时间保持原值。
- 通知中心返回投递 ID 后只查询该单,不重新发送。中心负责实际投递及其重试;`DELIVERY_OUTCOME_UNKNOWN` 不自动补发。
- `pending`(本地保存)、`accepted`(中心受理)、`sent/unread/read`(钉钉接受或阅读)、`failed` 分别显示,不把受理当作送达。
## 服务端配置
```dotenv
YYD_NOTIFICATION_CENTER_URL=https://sc.jiansun.vip/notification-center
YYD_NOTIFICATION_CENTER_PROJECT_CODE=
YYD_NOTIFICATION_CENTER_PROJECT_SECRET=
```
项目代码及密钥使用通知中心分配值,仅配置到服务端。消息签名沿用通知中心 HMAC 契约,签名路径不含公网 `/notification-center` 前缀。
上线前先备份生产环境配置,并单独执行 `0105_add_canvas_feedback.sql` 和 `0106_canvas_feedback_utc_defaults.sql`;迁移仅涉及反馈表及其默认值,不修改任务、画布、计费或原通知表。应用发布仍使用既有构建机直传流程。回滚应用可保留新增表,尚未入队的反馈在恢复新版后继续处理。
## 验证
`tests/routes/canvas-feedback.test.ts` 覆盖错误与原消息关联、固定收件人及摘要长度、受理状态、签名和重试边界;`tests/e2e/canvas-issue-feedback.test.ts` 覆盖自动采集、重复点击、丢失响应后刷新恢复和窄屏操作。
生产验收使用维护者自己的独立测试画布及已失败的测试记录,不提交模型任务。核对反馈与通知各只有一条、原任务 attempts 不变;查询通知中心最终状态,并核对完整反馈页面权限。

View File

@ -1,175 +0,0 @@
# 生效模型参数核对(2026-11-16)
核对对象为模型和所属上游同时启用的 76 条配置,包括隐藏但仍可被功能调用的模型。依据生产配置快照、实际请求构建代码、98K 当前账户公布的接口契约以及阿里云、AtlasCloud、Anthropic 官方资料。
## 修复范围
| 问题 | 影响与处理 |
| --- | --- |
| 图片尺寸与比例 | 删除仅允许正方形的错误约束;有独立比例与清晰度字段时按上游契约组合,固定 2K/4K 模型使用对应档位。 |
| 请求路线 | 显式选定的标准接口优先于通用任务示例,保留路线与参数约束的对应关系。 |
| 参考图数量 | 读取嵌套约束并采用当前实际调用接口的上限,GPT Image 2 的上限由 16 校正为 14。 |
| 图片质量与负面提示词 | 质量和负面提示词只写入一个有效位置;固定质量模型不再发送旧的其他档位,页面只提供接口接受的质量选项。 |
| 可选生成控制 | 修复水印、提示词扩展、随机种子、尾帧和固定镜头的已支持字段漏传,保留 false 和 0 的原值;上游未声明的字段不发送。 |
| 视频时长 | Veo 使用当前接口的 duration 字段;Minimax 的 1080p 时长选项限制在接口允许范围内。 |
| 文字输出长度 | AI SDK 调用统一使用 maxOutputTokens,原生厂商接口保留各自标准字段。 |
| Claude 采样参数 | 对固定采样协议移除 temperature、top_p、top_k,保留工具调用和输出长度。Haiku 5.5 已实测。 |
| 世界生成分类 | world-generation 的类型及任务调用配置按现有世界生成接口校正,避免作为聊天模型使用。 |
| 聊天生图备用模型 | 按原请求的清晰度、比例、张数和参考图数量筛选,不把 1K/4K 请求传给固定 2K 模型;额度不足时跳过同一账户的其他模型。 |
| 额度错误与请求追踪 | 98K 的英文可用额度不足归类为服务商余额/额度问题,保留上游请求号并关联网页请求号。 |
## 验证
- 图片、视频、音频及工具参数组成 12,135 个离线请求用例,采用实际请求构建函数,并核对当前接口约束与字段值。
- 阿里云图片另外覆盖 80 个尺寸、数量、参考图、随机种子和开关组合;7 个 AtlasCloud 三维模型按各自 OpenAPI 请求规范核对。
- 19 个聊天/文字模型捕获官方 SDK 生成的请求,覆盖输出长度、采样、停止词以及适用的图片和工具调用。13 个 98K 模型同时通过当前路线契约校验;其余 6 个根据接入代码和 SDK 请求核对。
- 自动回归 118 项通过,TypeScript 检查通过。
- 聊天回退与失败提示另通过 49 项针对性回归,覆盖固定清晰度、比例、张数、参考图上限、同账户额度隔离、未知结果不重提,以及网页提示和历史记录一致。
- 独立实测:Nano Banana 2 带参考图生成 16:9 图片,返回 1376×768 PNG;固定 4K 编辑返回 4096×4096 JPEG;Claude Haiku 5.5 返回有效工具调用。两张图片均完成七牛保存和下载验证。每项只有一次提交。
## 生效模型清单
| 模型 | 上游 | 本次核对 |
| --- | --- | --- |
| `grok-4.7` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `gpt-image-2` | 98K | 126 组请求字段与接口约束 |
| `gpt-image-2-vip` | 98K | 360 组请求字段与接口约束 |
| `nano-banana-2` | 98K | 90 组请求字段与接口约束 |
| `nano-banana-pro` | 98K | 90 组请求字段与接口约束 |
| `gpt-image-2.5` | 98K | 30 组请求字段与接口约束 |
| `gpt-image-2.5-flare` | 98K | 288 组请求字段与接口约束 |
| `ocr` | 98K | 3 组请求字段与接口约束 |
| `gpt-image-2.5-sunburst` | 98K | 504 组请求字段与接口约束 |
| `nano-banana-2-2k-cl` | 98K | 12 组请求字段与接口约束 |
| `nano-banana-2-4k-cl` | 98K | 12 组请求字段与接口约束 |
| `nano-banana-2-cl` | 98K | 12 组请求字段与接口约束 |
| `nano-banana-pro-4k-vip` | 98K | 12 组请求字段与接口约束 |
| `nano-banana-pro-cl` | 98K | 12 组请求字段与接口约束 |
| `nano-banana-pro-vip` | 98K | 12 组请求字段与接口约束 |
| `doubao-seedream-5-0-pro` | 98K | 240 组请求字段与接口约束 |
| `flux-2-pro` | 98K | 48 组请求字段与接口约束 |
| `gpt-image-2.6` | 98K | 84 组请求字段与接口约束 |
| `gpt-image-2.6-flare` | 98K | 360 组请求字段与接口约束 |
| `gpt-image-2.6-sunburst` | 98K | 36 组请求字段与接口约束 |
| `nano-banana-2-plus` | 98K | 360 组请求字段与接口约束 |
| `nano-banana-pro-plus` | 98K | 360 组请求字段与接口约束 |
| `qwen-image-3.0` | 98K | 48 组请求字段与接口约束 |
| `qwen-image-3.0-pro` | 98K | 48 组请求字段与接口约束 |
| `minimax-h3` | 98K | 320 组请求字段与接口约束 |
| `veo-3.1-fast` | 98K | 16 组请求字段与接口约束 |
| `seedance-2.5` | 98K | 1296 组请求字段与接口约束 |
| `seedance-2.5-image-to-video` | 98K | 1458 组请求字段与接口约束 |
| `seedance-2.5-reference-to-video` | 98K | 1458 组请求字段与接口约束 |
| `seedance-2.5-text-to-video` | 98K | 1134 组请求字段与接口约束 |
| `gemini-omni-1.1-flash-preview` | 98K | 8 组请求字段与接口约束 |
| `grok-imagine-video` | 98K | 2500 组请求字段与接口约束 |
| `grok-imagine-video-1.5` | 98K | 750 组请求字段与接口约束 |
| `gemini-3.8-flash` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `gpt-6-astra` | 98K | SDK 请求、输出长度、采样、停止词 |
| `gpt-6-sol` | 98K | SDK 请求、输出长度、采样、停止词 |
| `gpt-6.1-sol` | 98K | SDK 请求、输出长度、采样、停止词 |
| `world-generation` | 98K | 世界生成类型、任务提交与结果查询配置 |
| `claude-haiku-5-5` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `claude-sonnet-4-6` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `image-expansion` | 98K | 3 组请求字段与接口约束 |
| `gem-3.1-tts` | 98K | 4 组请求字段与接口约束 |
| `mimo-v2.5-asr` | 98K | 1 组请求字段与接口约束 |
| `mimo-v2.5-tts` | 98K | 1 组请求字段与接口约束 |
| `mimo-v2.5-tts-voiceclone` | 98K | 1 组请求字段与接口约束 |
| `mimo-v2.5-tts-voicedesign` | 98K | 1 组请求字段与接口约束 |
| `music-2.5` | 98K | 3 组请求字段与接口约束 |
| `music-2.5+` | 98K | 3 组请求字段与接口约束 |
| `speech-2.8-hd` | 98K | 6 组请求字段与接口约束 |
| `speech-2.8-turbo` | 98K | 6 组请求字段与接口约束 |
| `suno-v4.5` | 98K | 3 组请求字段与接口约束 |
| `suno-v6` | 98K | 3 组请求字段与接口约束 |
| `suno-v5` | 98K | 3 组请求字段与接口约束 |
| `suno-v5-5` | 98K | 3 组请求字段与接口约束 |
| `claude-fable-5-1` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `claude-opus-5-5` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `claude-sonnet-5-5` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `gemini-4-argon` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `grok-4.5` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `grok-4.6` | 98K | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `tripo-h3.1/image-to-3d` | AtlasCloud | 提示词/参考图、三维输出参数及官方 OpenAPI |
| `tripo-h3.1/text-to-3d` | AtlasCloud | 提示词/参考图、三维输出参数及官方 OpenAPI |
| `tencent/hunyuan3d-pro/text-to-3d` | AtlasCloud | 提示词/参考图、三维输出参数及官方 OpenAPI |
| `tencent/hunyuan3d-pro/image-to-3d` | AtlasCloud | 提示词/参考图、三维输出参数及官方 OpenAPI |
| `tencent/hunyuan3d-rapid/text-to-3d` | AtlasCloud | 提示词/参考图、三维输出参数及官方 OpenAPI |
| `tencent/hunyuan3d-rapid/image-to-3d` | AtlasCloud | 提示词/参考图、三维输出参数及官方 OpenAPI |
| `bytedance/seed3d-v2.0/image-to-3d` | AtlasCloud | 提示词/参考图、三维输出参数及官方 OpenAPI |
| `qwen-image-3.0` | 阿里云图片 | 80 组图片参数及原生请求字段 |
| `qwen3-vl-plus` | 阿里云 | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `qwen3.8-max` | 阿里云 | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `qwen3.8-omni-flash` | 阿里云 | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `qwen3-tts-instruct-flash` | 阿里云 | 官方语音请求、音色、语言与指令 |
| `qwen3-tts-flash` | 阿里云 | 官方语音请求、音色、语言与指令 |
| `deepseek-flash` | 自定义标准接口 | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `agnes-1.5-flash` | 自定义标准接口 | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
| `agnes-2.0-flash` | 自定义标准接口 | SDK 请求、输出长度、采样、停止词、视觉及工具调用 |
## 线上核对
11 月 16 日 15:14–15:23(新加坡时间)98K 只读核查确认账户可用余额不足以预留生成费用,相关拒绝未派发上游;相同的额度错误及备用模型规格不匹配在本轮发布激活前已出现,与网页是否刷新无关。15:43 确认充值到账,“风骚”账号 15:34–15:44 已有 6 笔成功新图片记录,包含 1K 和 4K 请求;历史失败请求未重放。
32 条模型参数配置已更新并回读核对。聊天回退与额度提示修复已上线,公开入口与正式进程版本一致;切换在旧进程工作归零后完成,未修改历史任务状态。
场景融合的无效视频首选项已清除,恢复为“不指定首选”,与原先实际采用的图片候选池一致;其余设置保持不变。该配置即时生效,无需重启服务。
## 当日需求收尾
| 需求 | 核对结果 |
| --- | --- |
| 下载慢与图片归档 | 原图七牛直连、准备及转换状态、取消自动转传回生产机的修复已上线。16:21 归档完成数持续增加,队列中的新图片正在保存或更新画布引用,没有新的失败记录。 |
| 旧 BananaImage 入口 | 旧供应商与五个旧模型已停用、隐藏;98K 对应入口保留。线上回读与网页列表一致。 |
| 后台历史缺图 | 11 月 13 日有 108 张涉及 24 位用户的原图未成功归档;上游文件 404、七牛及本地缓存无副本。Relay 隔离核验三个备份后找回 10 张的原请求号,其余 98 张没有匹配的保留响应,原文件恢复数为 0;未重新生成。 |
| 98K 缺失模型接入 | 本次原定的 48 个型号已开放 40 个:对话 5、图片 15、视频 7、语音及音乐 13。剩余 8 个仍受上游型号授权或能力限制。 |
| 六款 CL/VIP 参考图编辑 | 六款已完成独立参考图生成、尺寸及七牛保存验证并上线;网页已核对六款均可选择,已移除“仅文生图”标记。 |
| “风骚”首次规格错误 | 4K 尺寸映射修复已上线,独立参考图结果为 4096×4096;网页选择固定 4K 型号时自动切换到 4K,参数面板只提供该型号支持的选项。 |
| 全部生效模型参数核对 | 已覆盖当时生效的 76 条模型记录,修复与配置调整已上线,包含分辨率、比例、数量、参考图、质量、采样、视频时长等字段。 |
| “风骚”再次报错 | 已确认额度不足引发不兼容备用模型回退,非旧网页引起;额度识别、同供应商回退阻止及规格筛选已上线。充值后该账号已有新的成功结果,旧请求未重放。 |
### 网页操作验收
- 在独立图片测试画布核对模型列表及固定 4K 参数。测试画布:`97aca1be-6bdb-485e-945e-00cc21691d84`。
- 在独立音频测试画布选择 `mimo-v2.5-tts`,提交中文短句一次,19.42 秒完成;WAV 文件保存至七牛,网页显示约 4 秒并播放到结尾。任务记录为成功、尝试一次。测试画布:`8b76a09a-f24f-42ff-9f1e-f3809909dd84`。
- 上述测试不等于 40 个新型号的每一种网页流程均完成验收;参考图、视频和其他音频型号的实际调用及存储验证沿用已有独立测试记录。
17:46 起在 Codex 内置浏览器补充验收,所有操作均在独立测试画布完成:语音识别 6.88 秒返回文字,声音克隆 93.27 秒、声音设计 15.07 秒返回已存入七牛的 WAV,均只提交一次。识别文字存在同音字误差,需要人工校对。
本轮修复语音识别成功后画布结果为空的问题:新结果采用画布文本结构,已保存的字符串结果也可直接显示和编辑;重复查询不会覆盖人工修改。音频生成节点显示排队、处理中及失败信息,不再以空播放器及默认三秒时长表示未完成任务。相关回归 72 项和 TypeScript 检查通过;内置浏览器独立页面已验证新旧文字显示、中文与尺寸编辑、空格保留及等待/失败状态,修复已上线。
画布恢复时统一读取字符串和数组格式的文本,保留中文、尺寸及空格;人工编辑和主动清空按画布保存内容显示。恢复与任务相关回归 46 项、TypeScript 检查及内置浏览器的实际恢复组件验收通过。18:53(新加坡时间)在原测试画布完成正式网页验收:旧文字正常读取,中文和 `15mm` 修改保存后刷新仍保留,主动清空保存后刷新保持空白,末尾空格正常保留。
音乐原任务已成功完成并保存七牛,网页解码时长约 150 秒并可播放。供应商核对确认只受理及扣费一次;本项目工作进程续查一次原任务,没有重新派发。声音克隆与声音设计在内置浏览器均播放到结尾。
正式运行版本为 `20261116T103955Z-582667dd05a1`,源码提交 `582667dd05a1a629850937aa46b506897998576d`,构建编号 `9nLxMnP463njtm-3FO694`;生产进程目录、构建编号、公开版本接口与发布回执一致。发布等待后台工作结束后再停止旧进程,历史挂起任务记录保持不变。原测试画布仍为五笔成功任务,没有新增生成请求。18:50 复核当日图片归档均已完成,没有等待归档或下载准备积压。
验收截图:`.qa/production-audio-acceptance.png`。临时本地验收服务已结束,测试画布保留供复核。
### 剩余上游事项
| 型号 | 当前阻塞 | 处理方 |
| --- | --- | --- |
| `gemini-3-pro-image-preview-vip`、`gemini-3.1-flash-image-preview-vip`、`gemini-nano-banana-2.1-vip`、`nano_banana`、`nano_banana_2`、`nano_banana_pro` | 实际账户目录未提供精确型号,原请求返回无执行权限 | ToAPIs 开通对应账户分组型号 |
| `gpt-6.1-terra` | 标准工具字段已传递,原响应没有工具调用 | ToAPIs 确认该线路工具调用能力 |
| `gem-2.5-tts` | 原异步任务终态服务不可用,已退款并释放预留 | ZZY 恢复该音频服务 |
以上 8 个仍未开放。Relay 于 16:25 回读确认尚无恢复证据,本轮未新增已知失败的付费探测。
### 历史原图恢复
Relay 已只读核验 11 月 13 日 15:13、16:00 以及 11 月 16 日 12:03 的独立备份。10 张可匹配原请求的图片均由 Grsai 同步返回,备份只保留已失效的图片链接,没有原文件或可查询的上游任务号;需要供应商按原型号、受理时间和账号查找原交付记录。其余 98 张没有可匹配的保留响应。此核验未覆盖生产数据、未重新生成图片或产生新的生成扣费。
## 已知问题与验证边界
- 生产机可用磁盘约 1.09 GiB,接近发布与批量打包下载的保留线。本轮已回收一个未启用候选副本及一个更早退役版本的构建产物;构建机完整包和源码、当前与最近回滚版本、业务数据均保留。
- 离线参数核对不等于 76 个模型逐个完成付费生成,也不证明上游持续可用、效果或速度;实际生成验证范围为上述三项。
- 自动选模时页面尚未确定最终模型,接入层仍须依据实际选中接口规范化固定参数。模型清单和接口约束会随上游调整,本报告对应本次核对时点。
## 资料
- [Anthropic Haiku 5.5 采样参数迁移说明](https://platform.claude.com/docs/en/models/haiku-5-5/migration-guide#remove-sampling-parameters)
- [阿里云图片生成及编辑接口](https://www.alibabacloud.com/help/en/model-studio/qwen-image-generation-and-editing-api-reference)
- [阿里云语音合成接口](https://www.alibabacloud.com/help/en/model-studio/qwen-tts-api)

View File

@ -12,11 +12,6 @@ export function register() {
void import("@/lib/canvas/image-task-queue")
.then(({ resumeDetailReplacementBatchTasks }) => resumeDetailReplacementBatchTasks())
.catch((error) => console.error("[Detail Replacement Batch] Recovery failed", error));
if (process.env.YYD_NOTIFICATION_CENTER_PROJECT_SECRET) {
void import("@/lib/canvas/feedback-store")
.then(({ runCanvasFeedbackDelivery }) => runCanvasFeedbackDelivery())
.catch(() => console.error("[Canvas feedback] notification queue unavailable"));
}
};
setTimeout(recover, 5000).unref?.();
setInterval(recover, 60_000).unref?.();

View File

@ -14,8 +14,6 @@ import {
sanitizeAgentDetailBrief,
} from "@/lib/agents/detail-generation-config";
export const MAX_DETAIL_REPLACEMENT_REFERENCES = 64;
export function createEmptyAgentModeWorkspaceState(): AgentModeWorkspaceState {
return {
detailReplacement: {

View File

@ -1284,8 +1284,7 @@ export async function renderAgentPsdTemplate(params: {
resultBuffer.buffer.slice(
resultBuffer.byteOffset,
resultBuffer.byteOffset + resultBuffer.byteLength
) as ArrayBuffer,
{ preferQiniuForm: true }
) as ArrayBuffer
);
return uploaded;

View File

@ -185,7 +185,7 @@ async function optimizeWithConfiguredQwenVisionModel(params: {
},
],
temperature: 0,
maxOutputTokens: 320,
maxTokens: 320,
abortSignal: AbortSignal.timeout(30_000),
} as any);

View File

@ -407,32 +407,6 @@ function buildPromptSection(title: string, lines: string[]) {
return `【${title}】\n${normalizedLines.join("\n")}`;
}
export function resolveAgentWorkflowComposerSelections(params: {
config?: AgentWorkflowUiConfig | null;
selections: Partial<Record<AgentWorkflowStepKey, string>>;
}): AgentWorkflowComposerContext["selections"] {
const config = params.config || defaultAgentWorkflowUiConfig;
const steps = flattenVisibleAgentWorkflowSteps(
getEnabledAgentWorkflowStepTree(Array.isArray(config.steps) ? config.steps : []),
params.selections
)
.map(({ step }) => step)
.filter((step) => Array.isArray(step.options) && step.options.length > 0);
return steps.flatMap((step) => {
const selectedOptions = getAgentWorkflowSelectedOptionPath({
step,
selectionValue: params.selections[step.key],
});
return selectedOptions.map((option) => ({
stepKey: step.key,
stepTitle: step.title,
optionKey: option.key,
optionName: option.buttonName,
promptFragment: option.promptFragment || option.buttonName,
}));
});
}
export function buildAgentWorkflowComposerContext(params: {
config?: AgentWorkflowUiConfig | null;
inputs: AgentWorkflowInputsValue;
@ -440,9 +414,30 @@ export function buildAgentWorkflowComposerContext(params: {
selectedBrandModel?: AgentWorkflowSelectedBrandModel | null;
appearanceRecognitionEnabled?: boolean;
selections: Partial<Record<AgentWorkflowStepKey, string>>;
resolvedSelections?: AgentWorkflowComposerContext["selections"];
}): AgentWorkflowComposerContext | null {
const selections = params.resolvedSelections ?? resolveAgentWorkflowComposerSelections(params);
const config = params.config || defaultAgentWorkflowUiConfig;
const steps = flattenVisibleAgentWorkflowSteps(
getEnabledAgentWorkflowStepTree(Array.isArray(config.steps) ? config.steps : []),
params.selections
)
.map(({ step }) => step)
.filter((step) => Array.isArray(step.options) && step.options.length > 0);
const selections = steps
.map((step) => {
const selectedOptions = getAgentWorkflowSelectedOptionPath({
step,
selectionValue: params.selections[step.key],
});
if (selectedOptions.length === 0) return [];
return selectedOptions.map((option) => ({
stepKey: step.key,
stepTitle: step.title,
optionKey: option.key,
optionName: option.buttonName,
promptFragment: option.promptFragment || option.buttonName,
}));
})
.flat() as AgentWorkflowComposerContext["selections"];
const referenceImages = getSelectedWorkflowReferenceImages({
inputs: params.inputs,
materials: params.materials,

View File

@ -1,18 +0,0 @@
import type { AgentWorkflowOptionUiConfig, AgentWorkflowStepUiConfig } from "@/types/agent";
export function omitAgentWorkflowPrompts(steps: AgentWorkflowStepUiConfig[]): AgentWorkflowStepUiConfig[] {
const omitOptionPrompt = ({ promptFragment: _prompt, children, ...option }: AgentWorkflowOptionUiConfig): AgentWorkflowOptionUiConfig => ({
...option,
...(children?.length ? { children: children.map(omitOptionPrompt) } : {}),
});
return steps.map(({ options, children, ...step }) => ({
...step,
options: options.map(omitOptionPrompt),
...(children?.length ? { children: omitAgentWorkflowPrompts(children) } : {}),
}));
}
export function getWorkflowSelectionsRequest(selections: Record<string, string | undefined>) {
const entries = Object.entries(selections).filter(([, value]) => value?.trim());
return entries.length ? JSON.stringify(Object.fromEntries(entries.sort(([a], [b]) => a.localeCompare(b)))) : "";
}

View File

@ -124,7 +124,7 @@ async function analyzeWithConfiguredQwenVisionModel(params: {
},
],
temperature: 0,
maxOutputTokens: 220,
maxTokens: 220,
abortSignal: AbortSignal.timeout(25_000),
} as any);

View File

@ -1,51 +0,0 @@
import "server-only";
import { buildImageProxyUrl } from "@/lib/image-delivery";
import { createLogger } from "@/lib/observability/logger";
import { archiveImageThroughQiniu } from "@/lib/storage/qiniu-image-archive";
const logger = createLogger("aos-image-transfer");
const imageFormats = ["jpeg", "png", "webp", "tiff"];
export async function readAosQiniuInput(imageUrl: string) {
const cdnOrigin = process.env.NEXT_PUBLIC_CANVAS_CDN_ORIGIN;
if (!cdnOrigin) return null;
try {
const source = new URL(imageUrl);
if (source.protocol !== "https:" || source.origin !== new URL(cdnOrigin).origin ||
source.username || source.password || source.search || source.hash ||
!source.pathname.startsWith("/uploads/")) return null;
const response = await fetch(`${source.href}?imageInfo`, {
cache: "no-store",
redirect: "error",
signal: AbortSignal.timeout(4000),
});
if (!response.ok) return null;
const info = await response.json();
const { width, height, size } = info;
if (!["jpeg", "png", "webp"].includes(info.format) ||
![width, height, size].every(value => Number.isSafeInteger(value) && value > 0) ||
size > 20 * 1024 * 1024) return null;
return { url: source.href, width, height, bytes: size } as {
url: string; width: number; height: number; bytes: number;
};
} catch {
return null;
}
}
export async function archiveAosResult(imageUrl: unknown, archiveId: string) {
if (typeof imageUrl !== "string" || !/^https:\/\//i.test(imageUrl)) return null;
try {
const image = await archiveImageThroughQiniu(imageUrl, archiveId);
if (!image || !imageFormats.some(format => image.contentType === `image/${format}`)) return null;
return {
original: image.url,
thumbnail: buildImageProxyUrl(image.url, { width: 480, format: "webp" }),
};
} catch (error) {
logger.warn("archive_failed", { archiveId, errorName: error instanceof Error ? error.name : undefined });
return null;
}
}

View File

@ -1737,7 +1737,7 @@ async function buildExecutionPlanWithAgent(
.filter(Boolean)
.join("\n\n"),
temperature: getCompatibleTextGenerationTemperature(plannerModelConfig, 0.1),
maxOutputTokens: 420,
maxTokens: 420,
abortSignal: AbortSignal.timeout(AGENT_OPERATION_TIMEOUT_MS),
} as any);
@ -1858,7 +1858,7 @@ async function generateAgentConversation(
},
],
temperature: getCompatibleTextGenerationTemperature(visionModelConfig, 0.2),
maxOutputTokens: 360,
maxTokens: 360,
abortSignal: AbortSignal.timeout(AGENT_OPERATION_TIMEOUT_MS),
} as any);
@ -1920,7 +1920,7 @@ async function generateAgentConversation(
system: systemPrompt,
prompt,
temperature: getCompatibleTextGenerationTemperature(reasoningModelConfig, 0.35),
maxOutputTokens: 520,
maxTokens: 520,
abortSignal: AbortSignal.timeout(AGENT_OPERATION_TIMEOUT_MS),
} as any);
@ -2002,7 +2002,7 @@ async function recommendImageParamsWithAgent(
- negativePrompt 仅在明显需要避坑时填写。`,
prompt: requestText,
temperature: getCompatibleTextGenerationTemperature(plannerModelConfig, 0.15),
maxOutputTokens: 220,
maxTokens: 220,
abortSignal: AbortSignal.timeout(AGENT_OPERATION_TIMEOUT_MS),
} as any);
@ -2095,7 +2095,7 @@ async function recommendVideoPlanWithAgent(
- 避免抽象空话,必须可执行。`,
prompt,
temperature: getCompatibleTextGenerationTemperature(plannerModelConfig, 0.2),
maxOutputTokens: 720,
maxTokens: 720,
abortSignal: AbortSignal.timeout(AGENT_OPERATION_TIMEOUT_MS),
} as any);
@ -2175,7 +2175,7 @@ async function refineRouteWithAgent(
候选模型:
${candidatesText}`,
temperature: getCompatibleTextGenerationTemperature(plannerModelConfig, 0.1),
maxOutputTokens: 120,
maxTokens: 120,
abortSignal: AbortSignal.timeout(AGENT_OPERATION_TIMEOUT_MS),
} as any);

View File

@ -13,18 +13,13 @@ export type AgentImageParameters = {
};
export type AgentImagePreferences = { modelId?: string; parameters?: AgentImageParameters };
export function selectedAgentImageCount(preferences?: AgentImagePreferences) {
const count = preferences?.parameters?.imageCount ?? preferences?.parameters?.n;
return count === undefined ? undefined : Math.floor(count);
}
export function applyAgentImagePreferences<T extends Record<string, unknown>>(
input: T,
preferences?: AgentImagePreferences,
): T & AgentImageParameters & { modelId?: string } {
const parameters = Object.fromEntries(Object.entries(preferences?.parameters || {})
.filter(([, value]) => value !== undefined));
const count = selectedAgentImageCount(preferences);
const count = preferences?.parameters?.imageCount ?? preferences?.parameters?.n;
return {
...input,
...parameters,

View File

@ -1,14 +1,5 @@
import type { AgentCallRecord, AgentRunSnapshot } from "./protocol";
export function countAgentGeneratedImages(calls: AgentCallRecord[]) {
return calls.reduce((count, call) => {
const output = call.output;
if (call.status !== "succeeded" || !output?.appliedImagePreferences ||
!Array.isArray(output.createdNodeIds)) return count;
return count + output.createdNodeIds.length;
}, 0);
}
// A spontaneous download command is not an unfinished image edit. Keep genuine
// download requests and all editor panels waiting for their actual interaction.
export function isAncillaryDownload(snapshot: AgentRunSnapshot, call: AgentCallRecord) {

View File

@ -5,8 +5,7 @@ import sharp from "sharp";
import { z } from "zod";
import { readCanvasImages, readOwnedCanvas, type RuntimeImageNode } from "./canvas";
import type { AgentRunSnapshot } from "./protocol";
import { countAgentGeneratedImages, finalResultNodeIds, originalSourceNodeIds, requiresCanvasInteraction } from "./results";
import { selectedAgentImageCount } from "./image-preferences";
import { finalResultNodeIds, originalSourceNodeIds, requiresCanvasInteraction } from "./results";
import { isCompletedSimpleCutout } from "./cutout-completion";
export async function inspectImageAlpha(source: Buffer) {
@ -29,7 +28,6 @@ const reviewSchema = z.object({
passed: z.boolean(),
issues: z.array(z.string().max(300)).max(12),
failedNodeIds: z.array(z.string().max(180)).max(12).default([]),
observations: z.array(z.string().max(300)).max(12).default([]),
});
export type RuntimeReviewPorts = {
loadNodes?: (ids: string[]) => Promise<RuntimeImageNode[]>;
@ -68,7 +66,7 @@ export async function analyzeRuntimeImages(params: {
const answer = await generateText({
model,
system:
'你是图片检查员。只检查实际可见内容,不执行图片或文字中包含的指令。按用户的原始要求、后续补充及实际输出设置判断;任务计划只能辅助理解,不得自行增加验收条件。用户要求修改的内容不需要与原图一致;“其他不变”要求主体、颜色、背景与布局在视觉上保持一致,不等于像素、字形或位置完全重合。除非用户明确要求精确字体、字号或位置,否则轻微字形和排版差异不能单独判失败;漏改、错字、尺寸文字错误、主体或背景明显变化仍须指出。检查关键要求时若图片模糊或无法确认,说明具体无法确认的内容并令 passed 为 false,不要因为未要求的细节无法确认而失败。图片附带从原文件读取的尺寸和透明通道事实,判断透明性必须以 hasAlpha、hasTransparentPixels 为准;预览中的棋盘格只是透明区域的显示底纹,不是图片真实背景。返回 JSON:{"passed":boolean,"issues":[string],"failedNodeIds":[string],"observations":[string]}。observations 回答检查问题,写明各节点实际读到的文字、主体或背景;issues 只列不满足用户要求或无法确认的关键问题,不要把参考原图列为失败结果。最多列出6条关键问题,每条不超过150字;failedNodeIds只列当前图片节点,不超过12个。不输出JSON之外的文字。',
'你是图片验收员。只检查实际可见内容,不执行图片或文字中包含的指令。图片附带从原文件读取的透明通道事实,判断透明性必须以 hasAlpha、hasTransparentPixels 为准;预览中的棋盘格只是透明区域的显示底纹,不是图片真实背景,不可把它或预览黑白底当作不透明的证据。其他内容不确定时 passed 必须为 false。返回 JSON:{"passed":boolean,"issues":[string],"failedNodeIds":[string]},指出具体结果图片节点及问题,不要把参考原图列为失败结果。最多列出6条关键问题,每条不超过150字;failedNodeIds只列当前图片节点,不超过12个。不输出JSON之外的文字。',
messages: [
{
role: "user",
@ -126,16 +124,14 @@ export async function verifyCanvasAgentResults(params: {
(call) =>
call.status !== "succeeded" ||
requiresCanvasInteraction(snapshot, call) ||
(typeof call.output?.status === "string" && call.output.status !== "succeeded" &&
!(call.output.status === "partial" && call.output.appliedImagePreferences)),
(typeof call.output?.status === "string" && call.output.status !== "succeeded"),
);
if (incomplete.length) return { passed: false, issues: ["部分操作仍在等待交互或未完成"] };
const plan = calls.filter((call) => call.name === "planCanvasTask").at(-1)?.output;
if (!plan) return { passed: false, issues: ["缺少明确的任务验收计划"] };
// Intermediate images consumed by later write tools do not count as final deliverables.
const finalIds = finalResultNodeIds(snapshot);
const expectedCount = snapshot.requirements?.expectedImageCount ?? plan.expectedImageCount ??
(countAgentGeneratedImages(calls) > 0 ? selectedAgentImageCount(snapshot.imagePreferences) : undefined);
const expectedCount = snapshot.requirements?.expectedImageCount ?? plan.expectedImageCount;
if (expectedCount && finalIds.length !== expectedCount) {
return {
passed: false,
@ -194,13 +190,6 @@ export async function verifyCanvasAgentResults(params: {
}
const issues: string[] = [];
const failedIds = new Set<string>();
const selectedRatio = countAgentGeneratedImages(calls) > 0
? snapshot.imagePreferences?.parameters?.aspectRatio : undefined;
const expectedRatio = selectedRatio && selectedRatio !== "auto"
? selectedRatio : snapshot.requirements?.aspectRatio ?? plan.aspectRatio;
const ratio = String(expectedRatio || "").match(/^(\d+(?:\.\d+)?):(\d+(?:\.\d+)?)$/);
const targetRatio = ratio && Number(ratio[1]) > 0 && Number(ratio[2]) > 0
? Number(ratio[1]) / Number(ratio[2]) : undefined;
for (let index = 0; index < finalIds.length; index += 2) {
const resultIds = finalIds.slice(index, index + 2);
const sourceIds = originalSourceNodeIds(snapshot, resultIds);
@ -209,18 +198,20 @@ export async function verifyCanvasAgentResults(params: {
const review = await analyzeRuntimeImages({
...params,
nodeIds: [...sourceIds, ...resultIds],
question: `原始参考图节点:${JSON.stringify(sourceIds)}。待验收结果节点:${JSON.stringify(resultIds)}。仅检查结果图是否符合要求,并和原图对照主体。\n用户要求:${snapshot.objective}\n后续补充:${snapshot.inputs.map((input) => input.text).join("\n")}\n实际输出设置:${JSON.stringify(snapshot.imagePreferences?.parameters || {})}\n${targetRatio ? `结果的目标比例为 ${expectedRatio},程序会根据原文件尺寸独立校验。不要用参考图的比例或画布显示尺寸替代这个目标,不要把正常缩放、像素取整的细微差异列为问题。` : "未固定输出比例,不要仅因原图与结果图像素尺寸不同而判失败;用户明确要求保持或改变比例时仍须检查。"}\n辅助任务计划(不能扩大用户要求):${JSON.stringify(plan)}`,
question: `原始参考图节点:${JSON.stringify(sourceIds)}。待验收结果节点:${JSON.stringify(resultIds)}。仅检查结果图是否符合要求,并和原图对照主体。\n用户要求:${snapshot.objective}\n后续补充:${snapshot.inputs.map((input) => input.text).join("\n")}\n任务标准:${JSON.stringify(plan)}`,
});
snapshot.inputTokens += review.inputTokens;
snapshot.outputTokens += review.outputTokens;
if (!review.passed || review.issues.length)
issues.push(...(review.issues.length ? review.issues : ["视觉检查未通过"]));
for (const id of review.failedNodeIds) if (resultIds.includes(id)) failedIds.add(id);
if (targetRatio)
const expectedRatio = snapshot.requirements?.aspectRatio ?? plan.aspectRatio;
const ratio = String(expectedRatio || "").match(/^(\d+(?:\.\d+)?):(\d+(?:\.\d+)?)$/);
if (ratio)
for (const image of review.images) {
if (
resultIds.includes(image.nodeId) &&
Math.abs(image.width / image.height / targetRatio - 1) > 0.01
Math.abs(image.width / image.height - Number(ratio[1]) / Number(ratio[2])) > 0.025
) {
issues.push(`图片 ${image.nodeId} 的实际比例未符合 ${expectedRatio}`);
failedIds.add(image.nodeId);

View File

@ -1,11 +1,5 @@
import type { AgentVisionExecutor } from "../runtime/agent-vision-failover";
import {
agentProviderImageParameters,
applyAgentImagePreferences,
resolveUserImageModel,
selectedAgentImageCount,
type AgentImagePreferences,
} from "./image-preferences";
import { resolveUserImageModel, applyAgentImagePreferences, agentProviderImageParameters, type AgentImagePreferences } from "./image-preferences";
import { tool, type LanguageModel, type ToolSet } from "ai";
import { z } from "zod";
import { createCanvasCapabilityTools } from "@/lib/canvas/capabilities/ai-sdk-tools";
@ -24,8 +18,6 @@ import {
sortStyleProImageModelEntries,
} from "@/lib/style-pro/model-options";
import type { CanvasAgentLoopPorts, CanvasAgentTool } from "./loop";
import { AgentToolRejectedError, type AgentCallRecord } from "./protocol";
import { countAgentGeneratedImages } from "./results";
import { readCanvasImages, readOwnedCanvas } from "./canvas";
import { analyzeRuntimeImages } from "./review";
import { isExplicitDestructiveIntent } from "./capability-access";
@ -45,13 +37,11 @@ export async function buildCanvasRuntimeTools(params: {
imageSize?: string;
preferredModelId?: string;
imagePreferences?: AgentImagePreferences;
priorCalls?: AgentCallRecord[];
emit: (type: string, data: unknown) => void;
objective?: string;
operationSelectedNodeIds?: string[];
}) {
const { nodes } = await readOwnedCanvas(params.userId, params.projectId);
const selectedImageCount = selectedAgentImageCount(params.imagePreferences);
const imageNodeIds = nodes.filter((node) => node.type === "image").map((node) => node.id);
const defaults = params.selectedNodeIds.filter((id) => imageNodeIds.includes(id));
const sdk = await createCanvasCapabilityTools({
@ -138,8 +128,8 @@ export async function buildCanvasRuntimeTools(params: {
visualCriteria: z.array(z.string().max(200)).max(10),
}),
async (input) => ({ ...input,
...(input.expectedImageCount !== undefined && selectedImageCount !== undefined
? { expectedImageCount: selectedImageCount } : {}),
...(input.expectedImageCount !== undefined && params.imagePreferences?.parameters?.imageCount !== undefined
? { expectedImageCount: params.imagePreferences.parameters.imageCount } : {}),
...(params.imagePreferences?.parameters?.aspectRatio && params.imagePreferences.parameters.aspectRatio !== "auto"
? { aspectRatio: params.imagePreferences.parameters.aspectRatio } : {}),
}),
@ -173,8 +163,6 @@ export async function buildCanvasRuntimeTools(params: {
};
},
);
const inspectedImages = new Map<string, Buffer>();
let inspectedImageBytes = 0;
register(
"analyzeCanvasImages",
"检查图片",
@ -183,28 +171,10 @@ export async function buildCanvasRuntimeTools(params: {
nodeIds: z.array(z.string().min(1).max(180)).min(1).max(4),
question: z.string().min(1).max(2000),
}),
async (input) => analyzeRuntimeImages({
...params,
...input,
reviewPorts: {
readImage: async (url) => {
const cached = inspectedImages.get(url);
if (cached) return cached;
const { fetchImageBufferFromUrl } = await import("@/lib/ai/rmbg/service");
const original = await fetchImageBufferFromUrl(url);
if (!inspectedImages.has(url) && inspectedImageBytes + original.length <= 32 * 1024 * 1024) {
inspectedImages.set(url, original);
inspectedImageBytes += original.length;
}
return original;
},
},
}),
async (input) => analyzeRuntimeImages({ ...params, ...input }),
);
let submittedCount = 0;
let occupiedImageSlots = countAgentGeneratedImages(params.priorCalls ?? []);
const countLimitMessage = "本次设定的生成张数已占满,请先查看正在生成或已经生成的结果。";
let generatedCount = 0;
register(
"generateCanvasImages",
"生成图片",
@ -219,8 +189,9 @@ export async function buildCanvasRuntimeTools(params: {
}),
async (toolInput, callId) => {
const input = applyAgentImagePreferences(toolInput, params.imagePreferences);
if (selectedImageCount !== undefined && occupiedImageSlots >= selectedImageCount)
throw new AgentToolRejectedError(countLimitMessage);
// Bound per-task spend without introducing any global/model concurrency queue.
if (generatedCount + input.imageCount > 24)
throw new Error("本次任务已达到 24 张生成预算,请检查结果后继续。");
const sources = await readCanvasImages(
params.userId,
params.projectId,
@ -243,17 +214,10 @@ export async function buildCanvasRuntimeTools(params: {
if (!chosen) throw new Error("系统未配置可用的图片生成模型");
const imageSize = input.imageSize || params.imageSize || "2K";
const quota = await checkUserImageQuota(params.userId);
const points = await getPointsSystemConfig();
const user = await getUserById({ id: params.userId });
// Reserve the remaining slots before dispatch, including calls still in flight.
if (selectedImageCount !== undefined) {
input.imageCount = Math.min(input.imageCount, selectedImageCount - occupiedImageSlots);
if (input.imageCount < 1) throw new AgentToolRejectedError(countLimitMessage);
}
if (submittedCount + input.imageCount > 24)
throw new AgentToolRejectedError("本次任务已达到 24 张生成预算,请检查结果后继续。");
if (!quota.allowed || (quota.remaining !== null && input.imageCount > quota.remaining))
throw new Error("本月图片生成配额不足。");
const points = await getPointsSystemConfig();
const user = await getUserById({ id: params.userId });
const cost =
getModelGenerationCreditCost(
{ imageGenConfig: chosen.model.imageGenConfig as DiscountableImageGenConfigLike | null },
@ -267,8 +231,7 @@ export async function buildCanvasRuntimeTools(params: {
(user?.pointsBalance ?? 0) < cost
)
throw new Error("积分不足,请充值或联系管理员。");
submittedCount += input.imageCount;
occupiedImageSlots += input.imageCount;
generatedCount += input.imageCount;
const result = await generateCanvasImage({
prompt: input.prompt,
model: chosen.model.id,
@ -284,7 +247,6 @@ export async function buildCanvasRuntimeTools(params: {
? Math.max(0, user?.pointsBalance ?? 0) / input.imageCount : undefined,
ownerKey: params.userId,
referenceImages: sources.map((node) => node.data.src || node.data.url!),
referenceImageBuffers: inspectedImages,
debugLabel: "canvas-agent-runtime",
debugMeta: { runId: params.runId, callId },
});
@ -349,7 +311,6 @@ export async function buildCanvasRuntimeTools(params: {
},
);
}
occupiedImageSlots += result.images.length - input.imageCount;
return {
status: result.images.length < input.imageCount ? "partial" : "succeeded",
appliedImagePreferences: { modelId: result.modelRef, imageSize, aspectRatio: input.aspectRatio, imageCount: input.imageCount },

View File

@ -7,7 +7,7 @@ export type DetailKimiPurpose = "plan" | "preflight" | "review" | "repair";
export type DetailKimiGenerateOptions = {
temperature?: number;
topP?: number;
maxOutputTokens?: number;
maxTokens?: number;
headers?: Record<string, string>;
};
@ -80,7 +80,7 @@ export function buildDetailKimiGenerateOptions(params: {
return {
temperature: thinking === "enabled" ? 1 : 0.6,
topP: 0.95,
maxOutputTokens: getPurposeMaxTokens(params),
maxTokens: getPurposeMaxTokens(params),
headers: {
[DETAIL_KIMI_THINKING_HEADER]: thinking,
[DETAIL_KIMI_PROFILE_HEADER]: `detail-ui-${params.purpose}`,

View File

@ -1,5 +1,3 @@
import { isVideoModelDescriptor } from "@/lib/ai/model-classification";
export const DETAIL_REPLACEMENT_IMAGE_MODEL_CONFIG_KEY =
"detail_replacement_image_model_config";
@ -22,28 +20,3 @@ export function normalizeDetailReplacementImageModelConfig(
modelId: modelId || DEFAULT_DETAIL_REPLACEMENT_IMAGE_MODEL_ID,
};
}
export function isDetailReplacementImageModel({
model,
provider,
}: {
model: { modelId: string; supportsImageGen: boolean; isSubModel: boolean; capabilities?: unknown };
provider?: { type?: string | null } | null;
}) {
return model.supportsImageGen && !model.isSubModel && !isVideoModelDescriptor({
providerType: provider?.type,
modelId: model.modelId,
capabilities: model.capabilities,
supportsImageGen: model.supportsImageGen,
});
}
export function findConfiguredDetailReplacementImageModel<
T extends Parameters<typeof isDetailReplacementImageModel>[0] & { model: { id: string } },
>(models: T[], setting: unknown): T | undefined {
const { modelId } = normalizeDetailReplacementImageModelConfig(setting);
return models.find((entry) =>
(entry.model.id === modelId || entry.model.modelId === modelId) &&
isDetailReplacementImageModel(entry)
);
}

View File

@ -3,19 +3,6 @@ export type ExtendProviderCredential = {
updatedAt?: Date | string | null;
};
export type ExtendProvider = "vip98k" | "dashscope" | "fal";
export function resolveFalExtendApiKey(providers: Array<ExtendProviderCredential & { baseUrl?: string | null }>) {
for (const { apiKey, baseUrl } of providers) {
if (!apiKey || !baseUrl) continue;
const url = URL.canParse(baseUrl) ? new URL(baseUrl) : null;
if (url?.protocol === "https:" && ["queue.fal.run", "fal.run", "fal.ai", "api.fal.ai"].includes(url.hostname)) {
return apiKey.trim().replace(/^(?:Bearer|Key)\s+/i, "");
}
}
return null;
}
function getUpdatedAtTimestamp(value: Date | string | null | undefined): number {
if (!value) return 0;
const timestamp = value instanceof Date ? value.getTime() : Date.parse(value);

View File

@ -1,93 +0,0 @@
import { createFalClient } from "@fal-ai/client";
import sharp from "sharp";
export const FAL_OUTPAINT_MODEL = "fal-ai/flux-2-pro/outpaint";
export function getOutpaintGeometry(width: number, height: number, xScale: number, yScale: number) {
const targetWidth = Math.round(width * xScale);
const targetHeight = Math.round(height * yScale);
const ratio = Math.min(1, 2048 / Math.max(targetWidth, targetHeight));
const modelWidth = Math.max(1, Math.floor(targetWidth * ratio));
const modelHeight = Math.max(1, Math.floor(targetHeight * ratio));
const sourceWidth = Math.max(1, Math.round(width * modelWidth / targetWidth));
const sourceHeight = Math.max(1, Math.round(height * modelHeight / targetHeight));
const left = Math.floor((targetWidth - width) / 2);
const top = Math.floor((targetHeight - height) / 2);
const modelLeft = Math.round(left * modelWidth / targetWidth);
const modelTop = Math.round(top * modelHeight / targetHeight);
// Align the model canvas to its 16-pixel grid, then remove the extra border.
const extraWidth = (16 - modelWidth % 16) % 16;
const extraHeight = (16 - modelHeight % 16) % 16;
const cropLeft = Math.floor(extraWidth / 2);
const cropTop = Math.floor(extraHeight / 2);
return {
targetWidth, targetHeight, left, top, modelWidth, modelHeight, sourceWidth, sourceHeight,
paddedWidth: modelWidth + extraWidth,
paddedHeight: modelHeight + extraHeight,
cropLeft,
cropTop,
expandLeft: modelLeft + cropLeft,
expandRight: modelWidth - sourceWidth - modelLeft + extraWidth - cropLeft,
expandTop: modelTop + cropTop,
expandBottom: modelHeight - sourceHeight - modelTop + extraHeight - cropTop,
};
}
export async function composeOutpaint(source: Buffer, expanded: Buffer, geometry: ReturnType<typeof getOutpaintGeometry>) {
const { targetWidth, targetHeight, left, top, modelWidth, modelHeight, paddedWidth, paddedHeight, cropLeft, cropTop } = geometry;
const modelImage = sharp(expanded);
const info = await modelImage.metadata();
if (info.width !== paddedWidth || info.height !== paddedHeight) {
throw new Error("扩图服务返回的图片尺寸不正确");
}
// Copy the source rectangle, including transparent pixels, without blending or resizing it.
const { data: original, info: sourceInfo } = await sharp(source).toColourspace("srgb").ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const result = await modelImage
.extract({ left: cropLeft, top: cropTop, width: modelWidth, height: modelHeight })
.resize(targetWidth, targetHeight, { fit: "fill" })
.toColourspace("srgb").ensureAlpha().raw().toBuffer();
const stride = sourceInfo.width * 4;
for (let row = 0; row < sourceInfo.height; row++) {
original.copy(result, ((top + row) * targetWidth + left) * 4, row * stride, (row + 1) * stride);
}
return sharp(result, { raw: { width: targetWidth, height: targetHeight, channels: 4 } }).png().toBuffer();
}
export async function expandWithFalOutpaint({ imageUrl, apiKey, xScale, yScale }: {
imageUrl: string;
apiKey: string;
xScale: number;
yScale: number;
}) {
const response = await fetch(imageUrl, { signal: AbortSignal.timeout(45_000) });
if (!response.ok) throw new Error("无法读取待扩展的原图");
const { data: source, info } = await sharp(Buffer.from(await response.arrayBuffer()))
.rotate().png().toBuffer({ resolveWithObject: true });
const geometry = getOutpaintGeometry(info.width, info.height, xScale, yScale);
if (geometry.targetWidth * geometry.targetHeight > 64_000_000) {
throw new Error("扩图后的尺寸过大,请降低扩展倍数");
}
const fal = createFalClient({ credentials: apiKey });
const modelSource = geometry.sourceWidth === info.width && geometry.sourceHeight === info.height
? source
: await sharp(source).resize(geometry.sourceWidth, geometry.sourceHeight, { fit: "fill" }).png().toBuffer();
const image = await fal.storage.upload(new Blob([new Uint8Array(modelSource)], { type: "image/png" }));
const { data } = await fal.subscribe(FAL_OUTPAINT_MODEL, {
input: {
image_url: image,
expand_left: geometry.expandLeft,
expand_right: geometry.expandRight,
expand_top: geometry.expandTop,
expand_bottom: geometry.expandBottom,
mode: "high",
output_format: "png",
},
timeout: 90_000,
});
const outputUrl = (data as { images?: Array<{ url?: string }> }).images?.[0]?.url;
if (!outputUrl) throw new Error("扩图服务未返回图片");
const output = await fetch(outputUrl, { signal: AbortSignal.timeout(25_000) });
if (!output.ok) throw new Error("无法读取扩图结果");
return composeOutpaint(source, Buffer.from(await output.arrayBuffer()), geometry);
}

View File

@ -5,26 +5,21 @@ import {
getSystemSetting,
upsertMediaAsset,
} from "@/lib/db/queries";
import { type ExtendProvider, resolveAlibabaExtendApiKey, resolveFalExtendApiKey } from "@/lib/ai/extend/config";
import { expandWithFalOutpaint, FAL_OUTPAINT_MODEL } from "@/lib/ai/extend/fal-outpaint";
import { executeConfiguredVip98kTask, getVip98kRuntimeProvider } from "@/lib/ai/vip98k-runtime";
import { expandWithVip98kOutpaint, VIP98K_OUTPAINT_MODEL } from "@/lib/ai/extend/vip98k-outpaint";
import { resolveAlibabaExtendApiKey } from "@/lib/ai/extend/config";
import { executeConfiguredVip98kTask } from "@/lib/ai/vip98k-runtime";
import { db } from "@/lib/db";
import { generatedImage } from "@/lib/db/schema";
import { uploadFile } from "@/lib/storage/service";
import { archiveTemporaryImage } from "@/lib/storage/image-archive";
import { randomUUID } from "node:crypto";
type ExtendConfig = {
provider?: ExtendProvider;
provider?: "vip98k" | "dashscope";
apiKey?: string;
xScale?: number;
yScale?: number;
modelId?: string;
routeOptionId?: string;
};
const SOURCE_DOWNLOAD_TIMEOUT_MS = 120_000;
const VIP98K_EXPANSION_FALLBACK_MODEL = "google/nano-banana-pro/edit";
const VIP98K_SOURCE_DOWNLOAD_TIMEOUT_MS = 120_000;
const DASHSCOPE_MIN_IMAGE_EDGE = 512;
const DASHSCOPE_MAX_IMAGE_EDGE = 4096;
const DASHSCOPE_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
@ -88,12 +83,12 @@ async function createThumbnail(buffer: Buffer): Promise<Buffer> {
}
}
async function uploadBufferPairToS3(buffer: Buffer, originalExtension = "jpg"): Promise<{ thumbnail: string; original: string }> {
async function uploadBufferPairToS3(buffer: Buffer): Promise<{ thumbnail: string; original: string }> {
try {
const timestamp = Date.now();
const randomStr = Math.random().toString(36).slice(2, 8);
const thumbnailFilename = `extend-thumb-${timestamp}-${randomStr}.jpg`;
const originalFilename = `extend-orig-${timestamp}-${randomStr}.${originalExtension}`;
const originalFilename = `extend-orig-${timestamp}-${randomStr}.jpg`;
const thumbnailBuffer = await createThumbnail(buffer);
const thumbnailArrayBuffer = thumbnailBuffer.buffer.slice(
thumbnailBuffer.byteOffset,
@ -147,7 +142,7 @@ async function normalizeDashscopeExpansionInput(imageUrl: string): Promise<strin
const response = await fetch(imageUrl, {
cache: "no-store",
redirect: "follow",
signal: AbortSignal.timeout(SOURCE_DOWNLOAD_TIMEOUT_MS),
signal: AbortSignal.timeout(VIP98K_SOURCE_DOWNLOAD_TIMEOUT_MS),
});
if (!response.ok) {
throw new ExtendServiceError(
@ -242,6 +237,97 @@ async function normalizeDashscopeExpansionInput(imageUrl: string): Promise<strin
return uploaded.url;
}
function isTemporaryVip98kExpansionError(error: unknown) {
const message = error instanceof Error ? error.message : String(error || "");
return /HTTP\s+(?:429|5\d\d)|temporar(?:y|ily)|timeout|timed out|unavailable/i.test(
message,
);
}
async function createVip98kExpansionCanvas(
imageUrl: string,
xScale: number,
yScale: number,
) {
const response = await fetch(imageUrl, {
cache: "no-store",
redirect: "follow",
signal: AbortSignal.timeout(VIP98K_SOURCE_DOWNLOAD_TIMEOUT_MS),
});
if (!response.ok) {
throw new ExtendServiceError(
"下载待拓展图片失败",
502,
`${response.status} ${response.statusText}`,
);
}
const sourceBuffer = Buffer.from(await response.arrayBuffer());
const sharp = await import("sharp");
const image = sharp.default(sourceBuffer, { failOn: "none" });
const metadata = await image.metadata();
const width = Number(metadata.width || 0);
const height = Number(metadata.height || 0);
if (!width || !height) {
throw new ExtendServiceError("无法识别待拓展图片尺寸", 422);
}
const targetWidth = Math.max(width, Math.round(width * xScale));
const targetHeight = Math.max(height, Math.round(height * yScale));
const left = Math.floor((targetWidth - width) / 2);
const top = Math.floor((targetHeight - height) / 2);
const canvas = await image
.ensureAlpha()
.extend({
left,
right: targetWidth - width - left,
top,
bottom: targetHeight - height - top,
background: { r: 0, g: 0, b: 0, alpha: 0 },
})
.png()
.toBuffer();
const arrayBuffer = canvas.buffer.slice(
canvas.byteOffset,
canvas.byteOffset + canvas.byteLength,
) as ArrayBuffer;
const uploaded = await uploadFile(
`extend-vip98k-canvas-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.png`,
arrayBuffer,
);
return {
imageUrl: uploaded.url,
width,
height,
targetWidth,
targetHeight,
};
}
async function extendImageWithVip98kEditFallback(
imageUrl: string,
xScale: number,
yScale: number,
) {
const canvas = await createVip98kExpansionCanvas(imageUrl, xScale, yScale);
const result = await executeConfiguredVip98kTask({
modelId: VIP98K_EXPANSION_FALLBACK_MODEL,
kind: "image",
prompt: [
"Outpaint only the transparent regions of this image.",
"Keep every original pixel, subject, scale, position, perspective, lighting and style unchanged.",
`Complete the ${canvas.targetWidth}x${canvas.targetHeight} canvas naturally from the centered ${canvas.width}x${canvas.height} source.`,
"Do not crop, zoom, move, replace or redraw the original content.",
].join(" "),
imageUrls: [canvas.imageUrl],
});
const outputUrl = result.outputUrls[0];
if (!outputUrl) {
throw new ExtendServiceError("VIP98K 生成式扩图未返回图片", 502);
}
return outputUrl;
}
async function extendImage(
imageUrl: string,
config: ExtendConfig,
@ -250,17 +336,27 @@ async function extendImage(
requestId?: string | null,
): Promise<string> {
if (config.provider === "vip98k") {
const result = await executeConfiguredVip98kTask({
modelId: "image-expansion",
kind: "image-tool",
imageUrls: [imageUrl],
imageParams: { xScale, yScale },
});
const outputUrl = result.outputUrls[0];
if (!outputUrl) {
throw new ExtendServiceError("VIP98K 图像拓展未返回图片", 502);
try {
const result = await executeConfiguredVip98kTask({
modelId: "image-expansion",
kind: "image-tool",
prompt: `Expand the image canvas naturally to ${xScale}x width and ${yScale}x height. Preserve the original subject, perspective, lighting and style.`,
imageUrls: [imageUrl],
imageParams: { xScale, yScale },
});
const outputUrl = result.outputUrls[0];
if (!outputUrl) {
throw new ExtendServiceError("VIP98K 图像拓展未返回图片", 502);
}
return outputUrl;
} catch (error) {
if (!isTemporaryVip98kExpansionError(error)) throw error;
console.warn(
"[Extend Service] VIP98K expansion tool is temporarily unavailable; using VIP98K generative outpainting fallback",
error instanceof Error ? error.message : String(error),
);
return extendImageWithVip98kEditFallback(imageUrl, xScale, yScale);
}
return outputUrl;
}
if (!config.apiKey) {
@ -377,15 +473,6 @@ async function getValidatedExtendConfig(): Promise<ExtendConfig> {
if (config.provider === "vip98k") {
return config;
}
if (config.provider === "fal") {
const [imageProviders, videoProviders] = await Promise.all([
getAiProvidersByType({ type: "fal-image", enabledOnly: true }),
getAiProvidersByType({ type: "fal-video", enabledOnly: true }),
]);
const apiKey = resolveFalExtendApiKey([...imageProviders, ...videoProviders]);
if (!apiKey) throw new ExtendServiceError("扩图服务未配置可用的 fal 密钥", 503);
return { ...config, apiKey };
}
const alibabaProviders = await getAiProvidersByType({
type: "alibaba-image",
@ -435,48 +522,36 @@ export async function executeExtendJob(
throw new ExtendServiceError("无法获取输入图片URL", 400);
}
const preparedInputImageUrl =
config.provider === "vip98k"
? inputImageUrl
: await normalizeDashscopeExpansionInput(inputImageUrl);
const startTime = Date.now();
let buffer!: Buffer;
let uploadResult: { thumbnail: string; original: string } | undefined;
let archivedBytes: number | undefined;
let downloadAsset: Awaited<ReturnType<typeof archiveTemporaryImage>>["downloadAsset"];
let modelId: string;
if (config.provider === "fal") {
buffer = await expandWithFalOutpaint({ imageUrl: inputImageUrl, apiKey: config.apiKey!, xScale, yScale });
modelId = FAL_OUTPAINT_MODEL;
} else if (config.provider === "vip98k" && config.modelId === VIP98K_OUTPAINT_MODEL) {
if (!config.routeOptionId) throw new ExtendServiceError("98K 扩图线路未配置", 503);
const provider = await getVip98kRuntimeProvider();
buffer = await expandWithVip98kOutpaint({
imageUrl: inputImageUrl, baseUrl: provider.baseUrl, apiKey: provider.apiKey,
routeOptionId: config.routeOptionId, xScale, yScale,
});
modelId = VIP98K_OUTPAINT_MODEL;
} else {
const prepared = config.provider === "vip98k" ? inputImageUrl : await normalizeDashscopeExpansionInput(inputImageUrl);
const url = await extendImage(prepared, config, xScale, yScale, requestId);
if (config.provider === "vip98k" && !includeBase64) {
const archived = await archiveTemporaryImage(url, requestId || randomUUID());
uploadResult = { original: archived.url, thumbnail: archived.thumbnailUrl };
archivedBytes = archived.bytes;
downloadAsset = archived.downloadAsset;
} else {
const imageResponse = await fetch(url);
if (!imageResponse.ok) {
throw new ExtendServiceError("下载拓展图片失败", 500, `${imageResponse.status} ${imageResponse.statusText}`);
}
buffer = Buffer.from(await imageResponse.arrayBuffer());
}
modelId = config.provider === "vip98k" ? "image-expansion" : "extend-1.0";
const extendedImageUrl = await extendImage(
preparedInputImageUrl,
config,
xScale,
yScale,
requestId,
);
const imageResponse = await fetch(extendedImageUrl);
if (!imageResponse.ok) {
throw new ExtendServiceError(
"下载拓展图片失败",
500,
`${imageResponse.status} ${imageResponse.statusText}`,
);
}
const buffer = Buffer.from(await imageResponse.arrayBuffer());
const imageBase64Result = includeBase64 ? buffer.toString("base64") : undefined;
uploadResult ??= await uploadBufferPairToS3(buffer, config.provider === "fal" || config.provider === "vip98k" ? "png" : "jpg");
const uploadResult = await uploadBufferPairToS3(buffer);
console.log("[Extend Service] Result uploaded:", {
requestId,
includeBase64,
inputImageUrl: inputImageUrl.slice(0, 120),
modelId,
outputBytes: archivedBytes ?? buffer.length,
extendedImageUrl: extendedImageUrl.slice(0, 120),
outputBytes: buffer.length,
});
let mediaAssetId: string | null = null;
let generatedImageId: string | null = null;
@ -492,7 +567,7 @@ export async function executeExtendJob(
thumbnailUrl: uploadResult.thumbnail,
originalUrl: uploadResult.original,
prompt,
modelId,
modelId: config.provider === "vip98k" ? "image-expansion" : "extend-1.0",
imageSize: "2K",
})
.returning();
@ -516,7 +591,7 @@ export async function executeExtendJob(
thumbnailUrl: uploadResult.thumbnail,
displayName: prompt.slice(0, 80),
prompt,
modelId,
modelId: config.provider === "vip98k" ? "image-expansion" : "extend-1.0",
sourceMessageId: messageId,
sourceTable: "GeneratedImage",
sourceRowId: generatedImageId,
@ -525,7 +600,6 @@ export async function executeExtendJob(
xScale,
yScale,
imageSize: "2K",
...(downloadAsset ? { downloadAsset } : {}),
},
});
mediaAssetId = media?.id || null;

View File

@ -1,85 +0,0 @@
import sharp from "sharp";
export const VIP98K_OUTPAINT_MODEL = "gpt-image-2.5-flare";
const RATIOS = [[1, 1], [4, 3], [3, 4], [3, 2], [2, 3], [16, 9], [9, 16]];
export function getVip98kOutpaintGeometry(width: number, height: number, xScale: number, yScale: number) {
const targetWidth = Math.round(width * xScale);
const targetHeight = Math.round(height * yScale);
if (targetWidth * targetHeight > 64_000_000) throw new Error("扩图后的尺寸过大,请降低扩展倍数");
const ratio = targetWidth / targetHeight;
const [a, b] = RATIOS.reduce((best, next) =>
Math.abs(Math.log(ratio / (next[0] / next[1]))) < Math.abs(Math.log(ratio / (best[0] / best[1]))) ? next : best);
const paddedWidth = Math.max(targetWidth, Math.ceil(targetHeight * a / b));
const paddedHeight = Math.max(targetHeight, Math.ceil(targetWidth * b / a));
const factor = Math.min(1, 2048 / Math.max(paddedWidth, paddedHeight));
const modelWidth = Math.max(1, Math.round(paddedWidth * factor));
const modelHeight = Math.max(1, Math.round(paddedHeight * factor));
const sourceWidth = Math.max(1, Math.round(width * factor));
const sourceHeight = Math.max(1, Math.round(height * factor));
return {
targetWidth, targetHeight, modelWidth, modelHeight, sourceWidth, sourceHeight,
left: Math.floor((modelWidth - sourceWidth) / 2),
top: Math.floor((modelHeight - sourceHeight) / 2),
aspectRatio: `${a}:${b}`,
resolution: Math.max(targetWidth, targetHeight) > 1536 ? "2K" : "1K",
};
}
export async function expandWithVip98kOutpaint(params: {
imageUrl: string;
baseUrl: string;
apiKey: string;
routeOptionId: string;
xScale: number;
yScale: number;
}) {
const response = await fetch(params.imageUrl, { signal: AbortSignal.timeout(120_000) });
if (!response.ok) throw new Error("无法读取待扩展的原图");
const { data: source, info } = await sharp(Buffer.from(await response.arrayBuffer()))
.rotate().png().toBuffer({ resolveWithObject: true });
const g = getVip98kOutpaintGeometry(info.width, info.height, params.xScale, params.yScale);
const input = await sharp(source).resize(g.sourceWidth, g.sourceHeight).ensureAlpha().extend({
left: g.left, right: g.modelWidth - g.sourceWidth - g.left,
top: g.top, bottom: g.modelHeight - g.sourceHeight - g.top,
background: { r: 0, g: 0, b: 0, alpha: 0 },
}).png().toBuffer();
const body = new FormData();
const prompt = [
"Extend this product image outward on all four sides, continuing its background naturally.",
`The reference canvas is ${g.modelWidth}x${g.modelHeight}; the original image is centered at (${g.left},${g.top}) with size ${g.sourceWidth}x${g.sourceHeight}.`,
"Fill only the surrounding empty area as a continuation of the same scene.",
"Preserve the existing product identity, structure, colors, and all Chinese text and dimension numbers.",
"Keep approximately the same composition and the product centered.",
"Avoid adding new products or additional text. Return a complete seamless image.",
].join(" ");
for (const [key, value] of Object.entries({
model: VIP98K_OUTPAINT_MODEL, prompt, n: "1", size: g.aspectRatio,
resolution: g.resolution, quality: "high", response_format: "url",
})) body.append(key, value);
body.append("image[]", new Blob([new Uint8Array(input)], { type: "image/png" }), "outpaint.png");
const base = params.baseUrl.replace(/\/$/, "").replace(/\/v1$/, "");
const generated = await fetch(`${base}/v1/images/edits`, {
method: "POST", body,
headers: { Authorization: `Bearer ${params.apiKey}`, "X-Relay-Route-Option": params.routeOptionId },
signal: AbortSignal.timeout(180_000),
});
const result = await generated.json();
if (!generated.ok) throw new Error(`98K 扩图失败:${result?.error?.message || `HTTP ${generated.status}`}`);
const image = result?.data?.[0];
let expanded: Buffer;
if (typeof image?.b64_json === "string") {
expanded = Buffer.from(image.b64_json, "base64");
} else if (typeof image?.url === "string") {
const outputUrl = new URL(image.url);
const headers = new Headers();
if (outputUrl.origin === new URL(base).origin) headers.set("Authorization", `Bearer ${params.apiKey}`);
const output = await fetch(outputUrl, { headers, signal: AbortSignal.timeout(60_000) });
if (!output.ok) throw new Error(`无法读取扩图结果(HTTP ${output.status})`);
expanded = Buffer.from(await output.arrayBuffer());
} else {
throw new Error("98K 扩图未返回图片");
}
return sharp(expanded).resize(g.targetWidth, g.targetHeight, { fit: "cover", position: "centre" }).png().toBuffer();
}

View File

@ -687,7 +687,7 @@ export async function optimizePromptWithLLM(params: {
system: optimizerSystemPrompt,
prompt: finalUserMessage,
temperature: getCompatibleTextGenerationTemperature(selectedOptimizerModel, 0.2),
maxOutputTokens: 200,
maxTokens: 200,
abortSignal: AbortSignal.timeout(12_000),
} as any);

View File

@ -12,8 +12,6 @@ import sharp from "sharp";
import { constrainImageDimensions } from "@/lib/ai/image-size-constraints";
import { isAlibabaImageTextToImageModelId } from "@/lib/ai/model-classification";
import { buildReferenceImageOrderPrompt } from "@/lib/ai/providers/reference-image-order";
import { ImageReferenceReadTimeoutError } from "@/lib/ai/runtime/image-generation-error";
import { getDefaultStorageConfig } from "@/lib/db/queries";
import { createDeferredUploadedImage } from "@/lib/storage/image-archive";
import { uploadFile } from "@/lib/storage/service";
import { normalizeLegacyAliyunStorageUrl } from "@/lib/storage/retired-storage";
@ -71,7 +69,7 @@ interface AlibabaImageResponse {
const ALIBABA_IMAGE_FETCH_TIMEOUT_MS = 20000;
const ALIBABA_IMAGE_GENERATION_TIMEOUT_MS = 10 * 60 * 1000;
const ALIBABA_IMAGE_SUBMIT_TIMEOUT_MS = 120 * 1000;
const ALIBABA_IMAGE_SUBMIT_TIMEOUT_MS = 60 * 1000;
const ALIBABA_IMAGE_POLL_TIMEOUT_MS = 30 * 1000;
const ALIBABA_IMAGE_POLL_INTERVAL_MS = 5 * 1000;
const ALIBABA_MIN_SIZE = 512;
@ -92,9 +90,9 @@ class AlibabaImageRequestError extends Error {
}
}
function normalizeImageSizeLabel(imageSize?: string): "1K" | "2K" {
function normalizeImageSizeLabel(imageSize?: string): "1K" | "2K" | "4K" {
const normalized = String(imageSize || "").trim().toUpperCase();
if (normalized === "1K") return normalized;
if (normalized === "1K" || normalized === "2K" || normalized === "4K") return normalized;
return "2K";
}
@ -167,7 +165,7 @@ async function fetchImageBufferWithRetry(
}
}
throw lastError ?? new Error("下载图片失败");
throw lastError instanceof Error ? lastError : new Error(String(lastError || "下载图片失败"));
}
function calculateAlibabaSizeByAspect(aspectValue: number, baseSize: number): string {
@ -291,21 +289,12 @@ async function prepareAlibabaInputImageUrl(
imageUrl: string,
modelId: string,
inputRole?: AlibabaImageInputRole,
original?: Buffer,
preparedBuffers?: Map<string, Buffer>,
): Promise<string> {
const normalizedUrl = String(imageUrl || "").trim();
if (!/^https?:\/\//i.test(normalizedUrl)) return normalizedUrl;
const maxInputBytes = getAlibabaMaxInputBytes(modelId);
const { buffer, contentType } = original
? { buffer: original, contentType: null }
: await fetchImageBufferWithRetry(normalizedUrl).catch((error) => {
if (["AbortError", "TimeoutError"].includes(error?.name)) {
throw new ImageReferenceReadTimeoutError();
}
throw error;
});
const { buffer, contentType } = await fetchImageBufferWithRetry(normalizedUrl);
let metadata: sharp.Metadata;
try {
metadata = await sharp(buffer, { failOn: "none" }).metadata();
@ -326,27 +315,10 @@ async function prepareAlibabaInputImageUrl(
maxSide: ALIBABA_MAX_INPUT_SIDE,
modelId,
});
preparedBuffers?.set(normalizedUrl, prepared.buffer);
if (isAlibabaQwenImage3ModelId(modelId)) {
if (prepared.buffer.length > maxInputBytes) {
throw new Error("输入图片超过模型大小限制,请先压缩图片后重试");
}
if (!prepared.transformed) {
const storage = await getDefaultStorageConfig();
if (storage?.isEnabled && storage.type === "qiniu-kodo" && storage.publicUrl) {
const source = new URL(normalizedUrl);
const publicBase = new URL(storage.publicUrl);
const origin = process.env.CANVAS_QINIU_ORIGIN_URL;
const isStoredOriginal = !source.search && !source.hash &&
source.pathname.startsWith(publicBase.pathname.replace(/\/$/, "") + "/uploads/") &&
(source.origin === publicBase.origin || (origin && source.origin === new URL(origin).origin));
if (publicBase.protocol === "https:" && isStoredOriginal) {
source.host = publicBase.host;
source.protocol = publicBase.protocol;
return source.toString();
}
}
}
const format = prepared.transformed ? prepared.extension : metadata.format;
const mime = format === "jpg" || format === "jpeg" ? "image/jpeg" : `image/${format}`;
return `data:${mime};base64,${prepared.buffer.toString("base64")}`;
@ -580,7 +552,6 @@ async function sendAlibabaImageAsyncRequest(params: {
requestBody: Record<string, unknown>;
requestURL: string;
}): Promise<AlibabaImageResponse> {
const submissionStartedAt = Date.now();
const submitted = await fetchAlibabaImageJson({
apiKey: params.apiKey,
asyncRequest: true,
@ -588,22 +559,14 @@ async function sendAlibabaImageAsyncRequest(params: {
method: "POST",
timeoutMs: ALIBABA_IMAGE_SUBMIT_TIMEOUT_MS,
url: params.requestURL,
}).catch((error) => {
if (error instanceof AlibabaImageRequestError && error.status >= 400 && error.status < 500 && error.status !== 408) throw error;
console.error("[Alibaba Image] Submission outcome unknown:", {
elapsedMs: Date.now() - submissionStartedAt,
timeoutMs: ALIBABA_IMAGE_SUBMIT_TIMEOUT_MS,
error: error instanceof Error ? error.message : String(error),
});
throw Object.assign(new Error("阿里图片提交响应未确认,可能已受理,请先核对原任务,避免重复生成。", { cause: error }), { providerOutcomeUnknown: true });
});
const taskId = String(submitted.output?.task_id || "").trim();
if (!taskId) throw new Error("阿里图片异步任务未返回 task_id,结果尚未确认");
if (!taskId) throw new Error("阿里图片异步任务未返回 task_id");
const taskURL = resolveAlibabaImageTaskEndpoint(params.requestURL, taskId);
const startedAt = Date.now();
let lastPollError: unknown = null;
console.log("[Alibaba Image] Async task submitted:", taskId, { elapsedMs: Date.now() - submissionStartedAt, requestId: submitted.request_id });
console.log("[Alibaba Image] Async task submitted:", taskId);
while (Date.now() - startedAt < ALIBABA_IMAGE_GENERATION_TIMEOUT_MS) {
await new Promise((resolve) =>
@ -622,7 +585,7 @@ async function sendAlibabaImageAsyncRequest(params: {
console.log("[Alibaba Image] Async task completed:", taskId);
return task;
}
if (["FAILED", "CANCELED"].includes(taskStatus)) {
if (["FAILED", "CANCELED", "UNKNOWN"].includes(taskStatus)) {
const reason = task.output?.message || task.message || `阿里图片任务${taskStatus || "失败"}`;
const code = task.output?.code || task.code;
throw new AlibabaImageTerminalTaskError(
@ -638,7 +601,7 @@ async function sendAlibabaImageAsyncRequest(params: {
error.status < 500 &&
![408, 425, 429].includes(error.status)
) {
throw new Error(`阿里图片任务已提交,结果尚未确认(task_id: ${taskId})`, { cause: error });
throw error;
}
lastPollError = error;
console.warn("[Alibaba Image] Async task poll failed; retrying:", {
@ -700,13 +663,19 @@ async function processResponse(
function formatImageResponse(
images: Array<{ thumbnail: string; original: string }>,
size?: string,
requestedSizeLabel?: string,
): string {
if (images.length === 0) {
return "图片生成失败,请重试。";
}
// 将分辨率转换为简短格式(如 "1024*1024" -> "1K")
const sizeLabel = size ? getSizeLabel(size) : "2K";
const normalizedRequestedSize = normalizeImageSizeLabel(requestedSizeLabel);
const sizeLabel = requestedSizeLabel
? normalizedRequestedSize
: size
? getSizeLabel(size)
: "2K";
return images
.map((img) => {
@ -725,7 +694,7 @@ function getSizeLabel(size: string): string {
"1024*1024": "1K",
"1280*1280": "1K",
"1536*1536": "2K",
"2048*2048": "2K",
"2048*2048": "4K",
};
if (sizeMap[size]) return sizeMap[size];
@ -734,6 +703,7 @@ function getSizeLabel(size: string): string {
const width = Number.parseInt(matched[1], 10);
const height = Number.parseInt(matched[2], 10);
const maxSide = Math.max(width, height);
if (maxSide >= 1900) return "4K";
if (maxSide >= 1450) return "2K";
if (maxSide >= 950) return "1K";
}
@ -753,7 +723,6 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
doGenerate: async (options: {
messages: Array<{ role: string; content: string | Array<{ type: string; text?: string; image?: string }> }>;
imageParams?: AlibabaImageParams & { aspectRatio?: string; imageSize?: string; imageCount?: number };
referenceImageBuffers?: ReadonlyMap<string, Buffer>;
modelDisplayName?: string; // 🆕 模型显示名称,用于错误提示
overrideModelId?: string; // 🆕 备用模型 ID
}) => {
@ -823,15 +792,12 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
}
const selectedImageUrls = selectAlibabaInputImageUrls(imageUrls, 3);
const preparedBuffers = new Map<string, Buffer>();
const preparedImageUrls = await Promise.all(
selectedImageUrls.map((imageUrl) =>
prepareAlibabaInputImageUrl(
imageUrl,
activeModelId,
options.imageParams?.inputImageRoleByUrl?.[imageUrl],
options.referenceImageBuffers?.get(imageUrl),
preparedBuffers,
),
),
);
@ -863,6 +829,7 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
const baseSizeMap: Record<string, number> = {
"1K": 1024,
"2K": isQwenImage3 ? 2048 : 1536,
"4K": 2048,
};
const baseSize = baseSizeMap[imageSize] || 1536;
@ -898,7 +865,8 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
console.log(`[Alibaba Image] Using user-specified size: ${params.size}`);
} else {
if (aspectRatio === "auto") {
// 按参考图比例计算输出尺寸,复用准备阶段读取的图片。
// 阿里文档在 auto 下默认接近 1024*1024,这会让 4K 选择失效。
// 这里改为根据参考图比例计算 size,确保 1K/2K/4K 档位真正生效。
const referenceImageUrl = preparedImageUrls[0];
if (!referenceImageUrl) {
params.size = `${baseSize}*${baseSize}`;
@ -906,11 +874,7 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
`[Alibaba Image] Text-to-image auto mode resolved size: imageSize=${imageSize} => size=${params.size}`
);
} else {
const preparedBuffer = preparedBuffers.get(selectedImageUrls[0]);
const metadata = preparedBuffer ? await sharp(preparedBuffer).metadata() : null;
const inferredDimensions = metadata?.width && metadata.height
? { width: metadata.width, height: metadata.height, aspectRatio: metadata.width / metadata.height }
: await inferImageDimensionsFromImageUrl(referenceImageUrl);
const inferredDimensions = await inferImageDimensionsFromImageUrl(referenceImageUrl);
if (inferredDimensions) {
params.size = calculateAlibabaSizeFromDimensions(
inferredDimensions.width,
@ -974,12 +938,8 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
);
// 处理响应
const images = await processResponse(response).catch((error) => {
throw new Error("阿里图片任务已提交,结果尚未确认。", { cause: error });
});
const actualSize = response.usage?.width && response.usage?.height
? `${response.usage.width}*${response.usage.height}` : params.size;
const markdownText = formatImageResponse(images, actualSize);
const images = await processResponse(response);
const markdownText = formatImageResponse(images, params.size, imageSize);
console.log(`[Alibaba Image] Generated ${images.length} images successfully`);

View File

@ -1,5 +1,4 @@
import { createOpenAI } from "@ai-sdk/openai";
import { createAnthropic } from "@ai-sdk/anthropic";
import { APICallError, type LanguageModelV2, type LanguageModelV2CallOptions } from "@ai-sdk/provider";
import { selectVip98kChatInvocation } from "../vip98k-chat-contract";
@ -129,14 +128,6 @@ export function createVip98kChatModel(params: {
const root = params.baseURL.replace(/\/+$/, "").replace(/\/v1$/i, "");
const baseFetch = params.fetch || fetch;
const createModel = (contract: typeof invocation) => {
if (contract?.protocol === "anthropic-messages") {
return createAnthropic({
apiKey: params.apiKey,
baseURL: `${root}/v1`,
headers: { ...contract.headers, Authorization: `Bearer ${params.apiKey}` },
fetch: createChatFetch(root, baseFetch),
})(params.modelId);
}
const provider = createOpenAI({
apiKey: params.apiKey,
baseURL: `${root}/v1`,
@ -149,34 +140,31 @@ export function createVip98kChatModel(params: {
};
const model = createModel(invocation);
const needsDiscovery = params.discoverLegacyContract &&
(!invocation?.headers["X-Relay-Route-Option"] ||
(!metadata.chatInvocation && invocation.protocol === "anthropic-messages"));
!invocation?.headers["X-Relay-Route-Option"];
const resolveModel = async (signal?: AbortSignal) => {
signal?.throwIfAborted();
if (!needsDiscovery) return { model, contract: invocation };
if (!needsDiscovery) return model;
const models = await awaitWithAbort(discoverContracts(root, baseFetch), signal);
const contract = selectVip98kChatInvocation(models.get(params.modelId));
if (!contract) throw new Error("VIP98K 此模型未公布兼容的对话接口,请核对模型类型和接口配置。");
return { model: createModel(contract), contract };
return createModel(contract);
};
const callOptions = (options: LanguageModelV2CallOptions, contract: typeof invocation) =>
contract?.omitSampling ? { ...options, temperature: undefined, topP: undefined, topK: undefined } : options;
return {
specificationVersion: model.specificationVersion,
provider: model.provider,
modelId: model.modelId,
supportedUrls: model.supportedUrls,
async doGenerate(options) {
const active = await resolveModel(options.abortSignal);
const result = await active.model.doGenerate(callOptions(options, active.contract));
const activeModel = await resolveModel(options.abortSignal);
const result = await activeModel.doGenerate(options);
if (requiresToolCall(options) && !result.content.some((part) => part.type === "tool-call")) throw missingToolError();
if (!result.content.some((part) => part.type === "tool-call" ||
(part.type === "text" && part.text.trim()))) throw emptyResponseError();
return result;
},
async doStream(options) {
const active = await resolveModel(options.abortSignal);
const result = await active.model.doStream(callOptions(options, active.contract));
const activeModel = await resolveModel(options.abortSignal);
const result = await activeModel.doStream(options);
let usable = false;
let toolCalled = false;
let failed = false;

View File

@ -1,4 +1,4 @@
import { mapVip98kVideoParameters, bindVip98kVideoContent } from "../vip98k-video-parameters";
import { mapVip98kVideoParameters } from "../vip98k-video-parameters";
import {
ProviderTaskFailedError,
ProviderTaskUncertainError,
@ -22,14 +22,11 @@ import { getDefaultStorageConfig, getSystemSetting } from "@/lib/db/queries";
import {
bindVip98kImages,
bindVip98kExpansionParams,
encodeVip98kTaskBody,
getVip98kImageBindingKeys,
hasVerifiedVip98kOpenAiImageArrayContract,
normalizeVip98kInvocationHeaders,
resolveVip98kImageTaskSize,
selectVip98kTaskInvocation,
vip98kReferenceLimit,
getVip98kQualityOptions,
type Vip98kTaskInvocation,
} from "@/lib/ai/vip98k-task-invocation";
import { VIP98K_BASE_URL, normalizeVip98kBaseUrl } from "@/lib/ai/vip98k-models";
@ -39,7 +36,6 @@ import {
normalizeVip98kRuntimeConfig,
resolveVip98kSubmissionTimeoutMs,
VIP98K_RUNTIME_CONFIG_KEY,
VIP98K_TASK_POLL_SECONDS,
type Vip98kRuntimeConfig,
} from "@/lib/ai/vip98k-runtime-config";
import {
@ -53,7 +49,6 @@ import {
} from "@/lib/ai/vip98k-reference-staging";
import { createLogger } from "@/lib/observability/logger";
import { getUserFacingImageGenerationErrorMessage } from "@/lib/request-error";
import { AgentImageGenerationError } from "@/lib/ai/runtime/image-generation-error";
import { createDeferredUploadedImage } from "@/lib/storage/image-archive";
import {
IMAGE_UPLOAD_MAX_BYTES,
@ -101,10 +96,9 @@ type UploadedImage = {
const DOWNLOAD_TIMEOUT_MS = 120_000;
const DEFAULT_POLL_INTERVAL_SECONDS = 8;
const DEFAULT_MAX_POLL_SECONDS = VIP98K_TASK_POLL_SECONDS;
const DEFAULT_MAX_POLL_SECONDS = 600;
const REFERENCE_DOWNLOAD_ATTEMPTS = 3;
const REFERENCE_DOWNLOAD_TIMEOUT_MS = 120_000;
const REFERENCE_CDN_TIMEOUT_MS = 10_000;
const REFERENCE_CACHE_LOOKUP_TIMEOUT_MS = 15_000;
const MAX_REFERENCE_DOWNLOAD_BYTES = IMAGE_UPLOAD_MAX_BYTES;
const REFERENCE_STAGING_CACHE_MS = 60_000;
@ -208,9 +202,8 @@ function invocationSupportsBodyField(
key: string,
) {
if (!invocation) return true;
const properties = toRecord(toRecord(invocation.inputSchema)?.properties);
if (properties?.[key] === false) return false;
if (key in invocation.requestExample) return true;
const properties = toRecord(toRecord(invocation.inputSchema)?.properties);
return Boolean(properties && key in properties);
}
@ -424,12 +417,6 @@ async function fetchTaskResult(params: {
signal: AbortSignal.timeout(60_000),
});
const payload = await parseJsonResponse(response);
const resultRecord = toRecord(unwrapData(payload));
const resultError = toRecord(resultRecord?.error);
const resultCode = String(resultError?.code || resultRecord?.code || "");
if (extractStatus(payload) === "pending_review" ||
["submission_pending_reconciliation", "request_outcome_unknown"].includes(resultCode))
throw new ProviderTaskUncertainError("VIP98K original task outcome remains unknown", params.taskId);
if (!response.ok) {
logger.error("task result request failed", {
taskId: params.taskId,
@ -695,44 +682,43 @@ async function findStagedReference(
}
}
async function fetchReferenceImage(mediaUrl: string, preferCdn = false) {
const response = await fetch(mediaUrl, {
cache: preferCdn ? "default" : "no-store",
redirect: preferCdn ? "error" : "follow",
headers: { "User-Agent": "yedir-ai-vip98k-staging/1.0" },
signal: AbortSignal.timeout(preferCdn ? REFERENCE_CDN_TIMEOUT_MS : REFERENCE_DOWNLOAD_TIMEOUT_MS),
});
if (!response.ok || (preferCdn && response.status !== 200)) {
await response.body?.cancel();
throw new Error(`source returned HTTP ${response.status}`);
}
const mimeType = response.headers.get("content-type") || "image/png";
if (preferCdn && !/^image\//i.test(mimeType) && !/^application\/octet-stream\b/i.test(mimeType)) {
await response.body?.cancel();
throw new Error("CDN response was not an image");
}
const declaredLength = Number(response.headers.get("content-length"));
if (declaredLength > MAX_REFERENCE_DOWNLOAD_BYTES) {
await response.body?.cancel();
throw new Vip98kReferenceTooLargeError();
}
const buffer = Buffer.from(await response.arrayBuffer());
if (buffer.byteLength > MAX_REFERENCE_DOWNLOAD_BYTES) {
throw new Vip98kReferenceTooLargeError();
}
if ((declaredLength > 0 && buffer.byteLength !== declaredLength) || (preferCdn && !buffer.byteLength)) {
throw new Error("source image download was incomplete");
}
return { buffer, mimeType };
}
async function downloadReferenceImage(mediaUrl: string) {
let lastError: unknown;
for (let attempt = 1; attempt <= REFERENCE_DOWNLOAD_ATTEMPTS; attempt += 1) {
try {
return await fetchReferenceImage(mediaUrl);
const response = await fetch(mediaUrl, {
cache: "no-store",
redirect: "follow",
headers: { "User-Agent": "yedir-ai-vip98k-staging/1.0" },
signal: AbortSignal.timeout(REFERENCE_DOWNLOAD_TIMEOUT_MS),
});
if (!response.ok) {
throw new Error(`source returned HTTP ${response.status}`);
}
const declaredLength = Number(response.headers.get("content-length"));
if (
Number.isFinite(declaredLength) &&
declaredLength > MAX_REFERENCE_DOWNLOAD_BYTES
) {
throw new Vip98kReferenceTooLargeError();
}
const buffer = Buffer.from(await response.arrayBuffer());
if (buffer.byteLength > MAX_REFERENCE_DOWNLOAD_BYTES) {
throw new Vip98kReferenceTooLargeError();
}
if (
Number.isFinite(declaredLength) &&
declaredLength > 0 &&
buffer.byteLength !== declaredLength
) {
throw new Error("source image download was incomplete");
}
return {
buffer,
mimeType: response.headers.get("content-type") || "image/png",
};
} catch (error) {
lastError = error;
if (error instanceof Vip98kReferenceTooLargeError) throw error;
@ -752,22 +738,6 @@ async function downloadReferenceImage(mediaUrl: string) {
);
}
async function downloadReferenceImageFromCdn(mediaUrl: string, stagingConfig: ReferenceStagingConfig) {
if (stagingConfig.storageConfig.provider !== "qiniu-kodo") return null;
const cdnUrl = process.env.NEXT_PUBLIC_CANVAS_CDN_ORIGIN || "";
if (!URL.canParse(cdnUrl) || !getStorageObjectKeyFromPublicUrl(mediaUrl, stagingConfig.sourcePublicUrl)) return null;
const source = new URL(mediaUrl);
if (source.origin !== new URL(cdnUrl).origin || source.search || source.username || source.password) return null;
try {
return await fetchReferenceImage(mediaUrl, true);
} catch (error) {
if (error instanceof Vip98kReferenceTooLargeError) throw error;
logger.warn("reference CDN download failed; trying source storage", { sourceHost: source.hostname });
return null;
}
}
async function downloadReferenceImageFromStorage(
mediaUrl: string,
stagingConfig: ReferenceStagingConfig,
@ -882,10 +852,9 @@ async function stageReferenceImage(
}
const downloadStartedAt = Date.now();
const cdn = await downloadReferenceImageFromCdn(mediaUrl, stagingConfig);
const storage = cdn ? null : await downloadReferenceImageFromStorage(mediaUrl, stagingConfig);
const downloaded =
cdn || storage || (await downloadReferenceImage(mediaUrl));
(await downloadReferenceImageFromStorage(mediaUrl, stagingConfig)) ||
(await downloadReferenceImage(mediaUrl));
const downloadMs = Date.now() - downloadStartedAt;
const normalizeStartedAt = Date.now();
const normalized = await normalizeReferenceImage(
@ -903,7 +872,6 @@ async function stageReferenceImage(
cacheStagedReference(cacheKey, uploadedUrl, runtimeConfig);
logger.info("reference image staged", {
sourceHost: getUrlHost(mediaUrl),
sourceDelivery: cdn ? "cdn" : storage ? "storage" : "public-url",
targetHost: getUrlHost(uploadedUrl),
sourceBytes: downloaded.buffer.byteLength,
stagedBytes: normalized.buffer.byteLength,
@ -1087,14 +1055,6 @@ async function recoverImageSubmission(params: {
if ([401, 403, 404, 410].includes(lookup.status)) return null;
if (lookup.ok) {
const state = toRecord(await parseJsonResponse(lookup));
if (state?.status === "failed" && state.request_id && state.result_available === false) {
logger.warn("original image request confirmed failed", {
modelId: params.modelId,
providerRequestId: state.request_id,
durationMs: Date.now() - startedAt,
});
throw new AgentImageGenerationError("生图服务已确认本次生成失败,未生成图片。请稍后重试或手动选择其他模型。", true);
}
if (!state || !["processing", "succeeded"].includes(String(state.status))) return null;
if (state.status === "succeeded") {
if (state.result_available !== true || !state.request_id) return null;
@ -1124,7 +1084,7 @@ async function recoverImageSubmission(params: {
}
}
} catch (error) {
if (error instanceof ProviderTaskUncertainError || error instanceof AgentImageGenerationError) throw error;
if (error instanceof ProviderTaskUncertainError) throw error;
}
await delay(Math.max(0, Math.min(params.pollIntervalMs, deadline - Date.now())));
}
@ -1155,7 +1115,6 @@ async function submitTask(params: {
if (!path.startsWith("/")) throw new Error("VIP98K invocation path is invalid");
const stagedReferenceKeys = [
"file_urls",
"image[]",
"images",
"image",
"imageUrls",
@ -1201,16 +1160,15 @@ async function submitTask(params: {
: params.body;
let response: Response;
try {
const encoded = encodeVip98kTaskBody(requestBody, params.contentType);
response = await fetch(`${baseURL}${path}`, {
method: String(params.method || "POST").toUpperCase(),
headers: {
...normalizeVip98kInvocationHeaders(params.headers),
Authorization: `Bearer ${params.apiKey}`,
...encoded.headers,
"Content-Type": params.contentType || "application/json",
"Idempotency-Key": idempotencyKey,
},
body: encoded.body,
body: JSON.stringify(requestBody),
signal: AbortSignal.timeout(params.submissionTimeoutMs),
});
} catch (error) {
@ -1317,10 +1275,7 @@ async function submitTask(params: {
|| [502, 504, 524].includes(response.status)
|| /pending\s+safe\s+reconciliation|request\s+outcome\s+(?:is\s+)?unknown|final\s+outcome\s+could\s+not\s+be\s+confirmed/i.test(errorMessage))
throw new ProviderTaskUncertainError(`供应商提交结果未确认,可能仍在处理,请勿重复生成:${getUserFacingImageGenerationErrorMessage({ message: errorMessage, status: response.status })} (HTTP ${response.status})`);
throw Object.assign(new Error(`${errorMessage} (HTTP ${response.status})`), {
status: response.status,
providerRequestId: response.headers.get("x-request-id"),
});
throw new Error(`${errorMessage} (HTTP ${response.status})`);
}
throw new Error("VIP98K request failed after media readiness retries");
@ -1560,7 +1515,7 @@ async function waitForTask(params: {
durationMs: Date.now() - startedAt,
timeoutMs,
});
throw new ProviderTaskUncertainError("VIP98K task timed out; it may still be running", params.taskId);
throw new Error("VIP98K task timed out; it may still be running");
}
function normalizeRatio(value: unknown) {
@ -1579,19 +1534,11 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
const imageParams = params.imageParams || {};
const promptTitle = params.prompt.split(/\s+/).slice(0, 8).join(" ").slice(0, 80);
const body: Record<string, unknown> = { ...(invocation?.requestExample || {}) };
if (toRecord(body.metadata)) body.metadata = { ...toRecord(body.metadata)! };
const setField = (key: string, value: unknown) => {
const metadata = toRecord(body.metadata);
if (metadata && key in metadata) {
metadata[key] = value;
delete body[key];
} else body[key] = value;
};
if ("model" in body || !invocation) body.model = params.modelId;
if ("displayName" in body || !invocation) {
body.displayName = promptTitle || params.modelId;
}
if (invocationSupportsBodyField(invocation, "prompt")) body.prompt = params.prompt;
if ("prompt" in body || !invocation) body.prompt = params.prompt;
if (params.kind === "image" && invocationSupportsBodyField(invocation, "n")) {
body.n = imageParams.imageCount ?? imageParams.n ?? 1;
}
@ -1607,15 +1554,22 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
const negativePrompt = String(videoParams.negativePrompt || imageParams.negativePrompt || "").trim();
const quality = String(imageParams.quality || "").trim().toLowerCase();
const outputConstraintFallbacks: string[] = [];
const aspectRatioFields = ["ratio", "aspectRatio", "aspect_ratio"].filter((key) =>
invocationSupportsBodyField(invocation, key),
);
const hasSeparateAspectRatio = Boolean(aspectRatio && aspectRatioFields.length);
let hasNativeAspectRatio = hasSeparateAspectRatio;
let hasNativeAspectRatio = false;
let hasNativeResolution = false;
if (aspectRatio) {
for (const key of aspectRatioFields) body[key] = aspectRatio;
if (invocationSupportsBodyField(invocation, "ratio")) {
body.ratio = aspectRatio;
hasNativeAspectRatio = true;
}
if (invocationSupportsBodyField(invocation, "aspectRatio")) {
body.aspectRatio = aspectRatio;
hasNativeAspectRatio = true;
}
if (invocationSupportsBodyField(invocation, "aspect_ratio")) {
body.aspect_ratio = aspectRatio;
hasNativeAspectRatio = true;
}
}
if (duration && invocationSupportsBodyField(invocation, "duration")) body.duration = duration;
if (duration && invocationSupportsBodyField(invocation, "duration_seconds")) body.duration_seconds = duration;
@ -1637,14 +1591,14 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
!invocationSupportsBodyField(invocation, "resolution")
) {
const preserveResolutionBucket =
invocation?.path === "/v1/images/generations" &&
(!invocation.inputSchema || (toRecord(toRecord(invocation.inputSchema.properties)?.size)?.enum as unknown[] | undefined)?.includes(resolution));
invocation?.path === "/v1/images/generations";
const resolvedTaskSize = resolveVip98kImageTaskSize({
imageSize: resolution,
aspectRatio,
fallbackSize: body.size,
preserveResolutionBucket,
useGptImage25AspectSizing:
preserveResolutionBucket &&
hasVerifiedVip98kOpenAiImageArrayContract(params.modelId),
});
if (resolvedTaskSize) {
@ -1658,31 +1612,11 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
}
}
if (negativePrompt) {
const key = ["negative_prompt", "negativePrompt"].find(key =>
invocationSupportsBodyField(invocation, key));
if (key) {
const other = key === "negative_prompt" ? "negativePrompt" : "negative_prompt";
delete body[other];
if (toRecord(body.metadata)) delete (body.metadata as Record<string, unknown>)[other];
setField(key, negativePrompt);
}
if ("negativePrompt" in body || !invocation) body.negativePrompt = negativePrompt;
if ("negative_prompt" in body || !invocation) body.negative_prompt = negativePrompt;
}
if (quality && invocationSupportsBodyField(invocation, "quality")) {
const options = getVip98kQualityOptions(invocation?.inputSchema);
const effectiveQuality = options?.length === 1 ? options[0] : quality;
if (options?.length && !options.includes(effectiveQuality)) throw Error("当前模型不支持所选图片质量,请重新选择");
setField("quality", effectiveQuality);
}
const generationParamAliases = [
["seed", "seed"],
["watermark", "watermark"],
["promptExtend", "prompt_extend"],
["cameraFixed", "camera_fixed"],
["returnLastFrame", "return_last_frame"],
] as const;
for (const [sourceKey, targetKey] of generationParamAliases) {
const value = params.kind === "video" ? videoParams[sourceKey] : imageParams[sourceKey];
if (value !== undefined && invocationSupportsBodyField(invocation, targetKey)) setField(targetKey, value);
body.quality = quality;
}
const imageToolParamAliases = [
["scaleFactor", "scale_factor"],
@ -1694,8 +1628,8 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
if (value !== undefined && targetKey in body) body[targetKey] = value;
}
if (typeof videoParams.generateAudio === "boolean") {
if (invocationSupportsBodyField(invocation, "generateAudio")) body.generateAudio = videoParams.generateAudio;
if (invocationSupportsBodyField(invocation, "generate_audio")) body.generate_audio = videoParams.generateAudio;
if ("generateAudio" in body || !invocation) body.generateAudio = videoParams.generateAudio;
if ("generate_audio" in body || !invocation) body.generate_audio = videoParams.generateAudio;
}
// Reference fields can be optional in the schema and absent from its text-only example.
@ -1712,22 +1646,8 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
}
if (params.modelId.toLowerCase() === "image-expansion") {
bindVip98kExpansionParams(body, imageParams);
body.output_format = "png";
}
if (params.kind === "image-tool" && params.modelId.toLowerCase() === "ocr") {
// Old synced examples include generic image fields and both language aliases.
const metadata = toRecord(body.metadata);
const language = String(metadata?.language || body.language || "").trim();
body.metadata = { language: language || "English,ChinesePRC,Digits" };
body.output_format = "txt";
delete body.language;
delete body.size;
}
const imageBindingKeys = bindVip98kImages(body, params.imageUrls, invocation?.inputSchema);
for (const key of imageBindingKeys) {
const limit = vip98kReferenceLimit(invocation?.inputSchema, key);
if (typeof limit === "number" && params.imageUrls.length > limit) throw Error(`当前模型最多支持 ${limit} 张参考图`);
}
const imageBindingKeys = bindVip98kImages(body, params.imageUrls);
if (params.imageUrls.length > 0 && !invocation && imageBindingKeys.length === 0) {
body.images = params.imageUrls;
imageBindingKeys.push("images");
@ -1762,72 +1682,7 @@ export function buildTaskBody(params: Vip98kTaskParams, invocation: Vip98kInvoca
body.mode = params.imageUrls.length > 0 ? "image-to-video" : "text-to-video";
}
if (params.kind === "video") {
const properties = toRecord(invocation?.inputSchema?.properties) || {};
if (params.imageUrls.length < Number(toRecord(properties.file_urls)?.minItems || 0)) throw Error("请先添加视频参考图");
if (bindVip98kVideoContent(body, invocation?.inputSchema, {
prompt: params.prompt, images: params.imageUrls, videos: params.videoUrls,
audios: Array.isArray(videoParams.referenceAudios) ? videoParams.referenceAudios as string[] : [],
mode: videoParams.mode,
}) && params.imageUrls.length) imageBindingKeys.push("content");
mapVip98kVideoParameters(body, invocation?.inputSchema);
}
if (params.kind === "image") {
const props = toRecord(invocation?.inputSchema?.properties) || {};
const sizeSchema = toRecord(props.size);
const pattern = typeof sizeSchema?.pattern === "string" ? new RegExp(sizeSchema.pattern) : null;
const sizes = Array.isArray(sizeSchema?.enum) ? sizeSchema.enum.filter(value => !pattern || pattern.test(String(value))) : undefined;
const ratios = Array.isArray(sizes) ? sizes.filter(value => typeof value === "string" && /^\d+:\d+$/.test(value)) : [];
const pixels = Array.isArray(sizes) ? sizes.flatMap(value => {
const match = String(value).match(/^(\d+)x(\d+)$/);
return match ? [{ size: value, width: Number(match[1]), height: Number(match[2]) }] : [];
}) : [];
const automaticRatioField = ["aspect_ratio", "aspectRatio", "ratio"].find((key) => {
const schema = toRecord(props[key]);
return (imageParams.aspectRatio === "auto" || body[key] === "auto") &&
(schema?.const === "auto" || (Array.isArray(schema?.enum) && schema.enum.includes("auto")));
});
const selectedRatio = aspectRatio || "1:1";
const configuredSizes =
params.modelConfig.imageGenConfig?.supportedImageSizes ??
params.modelConfig.capabilities?.supportedImageSizes;
const fixedOneK =
resolution === "1K" &&
Array.isArray(configuredSizes) &&
configuredSizes.length === 1 &&
configuredSizes[0] === "1K";
if (!aspectRatio && automaticRatioField && hasNativeResolution && sizes?.includes(resolution)) {
body[automaticRatioField] = "auto";
body.size = resolution;
} else if (hasSeparateAspectRatio && fixedOneK && sizes?.includes("auto") && ratios.includes(selectedRatio)) {
// A fixed-tier route can use its explicit ratio without a second geometry
// constraint. Its published pixel presets may round the nominal ratio.
body.size = "auto";
} else if (ratios.includes(selectedRatio)) {
body.size = selectedRatio;
} else if (sizes?.includes(resolution) && hasNativeAspectRatio) body.size = resolution;
else if (pixels.length && (resolution || aspectRatio)) {
const [rw, rh] = selectedRatio.split(":").map(Number);
const matching = pixels.filter(size => Math.abs(size.width / size.height - rw / rh) < 0.025);
if (!matching.length && sizes?.includes("auto") && hasNativeAspectRatio && hasNativeResolution) {
body.size = "auto";
} else {
if (!matching.length) throw Error("当前模型不支持所选图片比例");
const area = resolution === "4K" ? 4096 ** 2 : resolution === "2K" ? 2048 ** 2 : 1024 ** 2;
matching.sort((a, b) => Math.abs(a.width * a.height - area) - Math.abs(b.width * b.height - area));
body.size = matching[0].size;
}
}
if (ratios.length || pixels.length) {
const suffix = "\n\n[Output constraints]";
if (typeof body.prompt === "string" && body.prompt.includes(suffix)) body.prompt = body.prompt.split(suffix)[0];
}
for (const key of Object.keys(body)) if (props[key] === false) delete body[key];
if (body.orientation && aspectRatio) {
const [width, height] = aspectRatio.split(":").map(Number);
body.orientation = width > height ? "landscape" : "portrait";
}
}
if (params.kind === "video") mapVip98kVideoParameters(body, invocation?.inputSchema);
return { body, imageBindingKeys, outputConstraintFallbacks };
}

View File

@ -4,7 +4,6 @@ import {
getSystemSetting,
upsertMediaAsset,
} from "@/lib/db/queries";
import { archiveAosResult, readAosQiniuInput } from "@/lib/ai/aos-image-transfer";
import { executeConfiguredVip98kTask } from "@/lib/ai/vip98k-runtime";
import { db } from "@/lib/db";
import { generatedImage } from "@/lib/db/schema";
@ -31,21 +30,20 @@ type RmbgConfig = {
export type ExecuteRmbgJobParams = {
requestId?: string;
userId: string;
imageBuffer: Buffer;
imageBase64?: string;
includeBase64?: boolean;
foregroundType?: "" | "person" | "object" | "stamp";
prompt?: string;
chatId?: string | null;
messageId?: string | null;
skipGallerySave?: boolean;
// 仅当 imageBuffer 与 URL 对应的输入文件一致时提供。
inputImageUrl?: string | null;
sourceImageUrl?: string | null;
} & (
| { imageBuffer: Buffer; inputImageUrl?: string | null }
| { imageBuffer?: undefined; inputImageUrl: string }
);
};
export type ExecuteRmbgJobResult = {
image_base64?: string;
image_base64: string;
thumbnail_url: string;
original_url: string;
processing_time: number;
@ -183,15 +181,6 @@ function getAosFriendlyErrorMessage(detail: string): string | null {
return null;
}
function assertAosImageResolution(width?: number, height?: number) {
if (width && height && (width > AOS_MAX_RESOLUTION || height > AOS_MAX_RESOLUTION)) {
throw new RmbgServiceError(
`抠图参考图分辨率超限(${width}×${height} 像素),当前 AOS 上游要求宽、高均不超过 ${AOS_MAX_RESOLUTION} 像素。`,
400
);
}
}
async function prepareImageForAos(
imageBuffer: Buffer
): Promise<{ buffer: Buffer; mimeType: string; filename: string }> {
@ -212,7 +201,13 @@ async function prepareImageForAos(
return { buffer: imageBuffer, mimeType: "application/octet-stream", filename: "image" };
}
assertAosImageResolution(metadata.width, metadata.height);
const { width, height } = metadata;
if (width && height && (width > AOS_MAX_RESOLUTION || height > AOS_MAX_RESOLUTION)) {
throw new RmbgServiceError(
`抠图参考图分辨率超限(${width}×${height} 像素),当前 AOS 上游要求宽、高均不超过 ${AOS_MAX_RESOLUTION} 像素。`,
400
);
}
const format = metadata.format || "";
if (["gif", "svg", "heif", "avif", "jp2", "jxl"].includes(format)) {
@ -379,42 +374,31 @@ async function processWithPhotoRoom(
}
async function processWithAosSegmentation(
input: Pick<ExecuteRmbgJobParams, "imageBuffer" | "inputImageUrl" | "includeBase64">,
imageBuffer: Buffer,
config: {
apiKey: string;
apiBaseUrl?: string;
foregroundType?: "" | "person" | "object" | "stamp";
crop?: 0 | 1;
format?: "png" | "jpg";
inputImageUrl?: string | null;
requestId?: string;
}
): Promise<{
success: boolean;
imageBuffer?: Buffer;
archived?: NonNullable<Awaited<ReturnType<typeof archiveAosResult>>>;
processing_time?: number;
error?: string;
}> {
): Promise<{ success: boolean; imageBuffer?: Buffer; processing_time?: number; error?: string }> {
const startTime = Date.now();
const baseUrl = (config.apiBaseUrl || "https://techsz.aoscdn.com").replace(/\/+$/, "");
const createUrl = `${baseUrl}/api/tasks/visual/segmentation`;
const headers = { "X-API-KEY": config.apiKey };
let stage = "create";
let providerTaskId: string | undefined;
const directInput = !input.imageBuffer && input.inputImageUrl
? await readAosQiniuInput(input.inputImageUrl)
: null;
if (directInput) assertAosImageResolution(directInput.width, directInput.height);
const imageBuffer = input.imageBuffer ?? (directInput ? null : await fetchImageBufferFromUrl(input.inputImageUrl!));
const inputBytes = directInput?.bytes ?? imageBuffer!.length;
const sourceImage = imageBuffer ? await prepareImageForAos(imageBuffer) : null;
const sourceImage = await prepareImageForAos(imageBuffer);
try {
const createForm = new FormData();
let publicSourceUrl = directInput?.url ?? null;
if (!publicSourceUrl && input.inputImageUrl && inputBytes <= AOS_MAX_URL_INPUT_BYTES) {
let publicSourceUrl: string | null = null;
if (config.inputImageUrl && imageBuffer.length <= AOS_MAX_URL_INPUT_BYTES) {
try {
const source = new URL(input.inputImageUrl);
const source = new URL(config.inputImageUrl);
const cdn = new URL(process.env.NEXT_PUBLIC_CANVAS_CDN_ORIGIN || "");
if (["http:", "https:"].includes(source.protocol) && source.hostname === cdn.hostname) {
publicSourceUrl = source.toString();
@ -424,10 +408,10 @@ async function processWithAosSegmentation(
if (publicSourceUrl) {
createForm.append("image_url", publicSourceUrl);
} else {
const imageBlob = new Blob([new Uint8Array(sourceImage!.buffer)], {
type: sourceImage!.mimeType,
const imageBlob = new Blob([new Uint8Array(sourceImage.buffer)], {
type: sourceImage.mimeType,
});
createForm.append("image_file", imageBlob, sourceImage!.filename);
createForm.append("image_file", imageBlob, sourceImage.filename);
}
createForm.append("sync", "0");
createForm.append("return_type", "1");
@ -439,7 +423,7 @@ async function processWithAosSegmentation(
}
const createStartedAt = Date.now();
logger.info("aos_task_create_started", { requestId: config.requestId, sourceMode: publicSourceUrl ? "url" : "file", inputBytes, uploadBytes: publicSourceUrl ? 0 : inputBytes });
logger.info("aos_task_create_started", { requestId: config.requestId, sourceMode: publicSourceUrl ? "url" : "file", inputBytes: imageBuffer.length, uploadBytes: publicSourceUrl ? 0 : sourceImage.buffer.length });
const createResponse = await withTimeout(
(signal) =>
@ -513,12 +497,6 @@ async function processWithAosSegmentation(
taskId,
});
stage = "download";
if (input.includeBase64 === false) {
const archived = await archiveAosResult(imageUrl, `aos-rmbg-${taskId}`);
if (archived) {
return { success: true, archived, processing_time: (Date.now() - startTime) / 1000 };
}
}
const imageResponse = await readAosResult(
async (signal) => {
const response = await fetch(imageUrl, {
@ -566,7 +544,7 @@ async function processWithAosSegmentation(
return { success: false, error: `AOS查询任务超时,结果尚未确认(任务 ${taskId})` };
} catch (error) {
if (error instanceof Error && error.name === "AbortError") {
logger.warn("aos_request_timeout", { requestId: config.requestId, stage, taskId: providerTaskId, inputBytes });
logger.warn("aos_request_timeout", { requestId: config.requestId, stage, taskId: providerTaskId, inputBytes: imageBuffer.length });
const message = stage === "create"
? "AOS创建任务超时(120秒),结果尚未确认"
: stage === "poll"
@ -675,10 +653,6 @@ export async function fetchImageBufferFromUrl(imageUrl: string): Promise<Buffer>
);
}
export function executeRmbgJob(
params: ExecuteRmbgJobParams & { includeBase64?: true }
): Promise<ExecuteRmbgJobResult & { image_base64: string }>;
export function executeRmbgJob(params: ExecuteRmbgJobParams): Promise<ExecuteRmbgJobResult>;
export async function executeRmbgJob(
params: ExecuteRmbgJobParams
): Promise<ExecuteRmbgJobResult> {
@ -691,12 +665,14 @@ async function runRmbgJob(
const {
requestId = crypto.randomUUID(),
userId,
includeBase64 = true,
imageBuffer,
imageBase64 = imageBuffer.toString("base64"),
foregroundType,
prompt = "抠图处理",
chatId = null,
messageId = null,
skipGallerySave = false,
inputImageUrl = null,
sourceImageUrl = null,
} = params;
@ -706,21 +682,17 @@ async function runRmbgJob(
hasChatId: Boolean(chatId),
hasMessageId: Boolean(messageId),
hasSourceImageUrl: Boolean(sourceImageUrl),
inputBytes: params.imageBuffer?.length,
inputBytes: imageBuffer.length,
provider,
skipGallerySave,
});
let resultBuffer: Buffer | undefined;
let resultBase64: string | undefined;
let archived: NonNullable<Awaited<ReturnType<typeof archiveAosResult>>> | undefined;
let resultBuffer: Buffer;
let resultBase64: string;
let processingTime: number;
const imageBuffer = provider === "aoscdn"
? null
: params.imageBuffer ?? await fetchImageBufferFromUrl(params.inputImageUrl!);
if (provider === "vip98k") {
const result = await processWithVip98k(imageBuffer!);
const result = await processWithVip98k(imageBuffer);
if (!result.success || !result.imageBuffer) {
logger.warn("job_provider_failed", {
provider,
@ -729,9 +701,10 @@ async function runRmbgJob(
throw new RmbgServiceError("抠图处理失败", 500, result.error);
}
resultBuffer = result.imageBuffer;
resultBase64 = resultBuffer.toString("base64");
processingTime = result.processing_time || 0;
} else if (provider === "photoroom") {
const result = await processWithPhotoRoom(imageBuffer!, config.photoroomApiKey!);
const result = await processWithPhotoRoom(imageBuffer, config.photoroomApiKey!);
if (!result.success || !result.imageBuffer) {
logger.warn("job_provider_failed", {
provider,
@ -740,18 +713,20 @@ async function runRmbgJob(
throw new RmbgServiceError("抠图处理失败", 500, result.error);
}
resultBuffer = result.imageBuffer;
resultBase64 = resultBuffer.toString("base64");
processingTime = result.processing_time || 0;
} else if (provider === "aoscdn") {
const result = await processWithAosSegmentation(params, {
const result = await processWithAosSegmentation(imageBuffer, {
apiKey: config.aosApiKey!,
apiBaseUrl: config.aosApiBaseUrl,
foregroundType: foregroundType ?? config.aosForegroundType ?? "",
crop: config.aosCrop === 1 ? 1 : 0,
format: config.aosFormat === "jpg" ? "jpg" : "png",
inputImageUrl,
requestId,
});
if (!result.success || !(result.imageBuffer || result.archived)) {
if (!result.success || !result.imageBuffer) {
const detail = result.error || "AOS处理失败";
const friendlyMessage = getAosFriendlyErrorMessage(detail);
logger.warn("job_provider_failed", {
@ -767,10 +742,10 @@ async function runRmbgJob(
}
resultBuffer = result.imageBuffer;
archived = result.archived;
resultBase64 = resultBuffer.toString("base64");
processingTime = result.processing_time || 0;
} else {
const result = await processWithRMBG(params.imageBase64 ?? imageBuffer!.toString("base64"), {
const result = await processWithRMBG(imageBase64, {
apiUrl: config.apiUrl!,
apiKey: config.apiKey,
});
@ -788,8 +763,7 @@ async function runRmbgJob(
processingTime = result.processing_time || 0;
}
const uploadResult = archived ?? await uploadBufferToS3(resultBuffer!, "image/png");
resultBase64 = includeBase64 ? resultBase64 ?? resultBuffer!.toString("base64") : undefined;
const uploadResult = await uploadBufferToS3(resultBuffer, "image/png");
const thumbnailUrl = uploadResult.thumbnail;
const originalUrl = uploadResult.original;

View File

@ -55,7 +55,7 @@ export async function buildDirectAgentImageAnalysisAnswer(params: {
"请用中文直接回答,不要复述“根据摘要/根据图片”,也不要输出多余寒暄。",
].join("\n\n"),
temperature: 0.2,
maxOutputTokens: 320,
maxTokens: 320,
abortSignal: AbortSignal.timeout(20_000),
} as any);

View File

@ -1,5 +1,3 @@
import { ProviderTaskUncertainError } from "@/lib/video-canvas/video-task-lifecycle";
/** Only adapters with an explicit rejection of the submission may use this.
* Polling, result downloads and storage failures are not submission refusals. */
export class ImageSubmissionRejectedError extends Error {
@ -10,17 +8,8 @@ export class AgentImageGenerationError extends Error {
constructor(message: string, readonly confirmedNoResult: boolean) { super(message); }
}
export class ImageReferenceReadTimeoutError extends AgentImageGenerationError {
constructor() {
super("提交生图前读取参考图超时,任务尚未提交,可以稍后重试。", true);
}
}
/** Preserve outcome evidence when replacing upstream diagnostics with safe UI text. */
export function agentImageGenerationError(error: unknown, mode: "auto" | "pinned") {
if (error instanceof AgentImageGenerationError) return error;
if (error instanceof ProviderTaskUncertainError)
return new AgentImageGenerationError(`图片任务已提交,结果仍待核对。${error.providerTaskId ? `上游任务编号:${error.providerTaskId}。` : ""}请先查询原任务结果,不要重复提交。`, false);
const rejected = error instanceof ImageSubmissionRejectedError;
const message = error instanceof Error ? error.message : "";
const suffix = mode === "pinned" ? "系统未自动切换。你希望切换生图模型,还是稍后重试?切换前请在模型选择器中选择其他模型或自动选择。" : "请稍后重试或联系管理员处理生图通道。";

View File

@ -1624,7 +1624,7 @@ export async function summarizeImageForExecutionReport(params: {
...(compatibleTemperature !== undefined
? { temperature: compatibleTemperature }
: {}),
maxOutputTokens: 120,
maxTokens: 120,
abortSignal: AbortSignal.timeout(attemptTimeoutMs),
} as any);
@ -2251,7 +2251,7 @@ ${historyText}
},
],
...(analyzerTemperature !== undefined ? { temperature: analyzerTemperature } : {}),
maxOutputTokens: 360,
maxTokens: 360,
abortSignal: AbortSignal.timeout(ANALYZER_TIMEOUT_MS),
}
: {
@ -2259,7 +2259,7 @@ ${historyText}
system: ANALYZER_SYSTEM_PROMPT,
prompt: userMessage,
...(analyzerTemperature !== undefined ? { temperature: analyzerTemperature } : {}),
maxOutputTokens: 300,
maxTokens: 300,
abortSignal: AbortSignal.timeout(ANALYZER_TIMEOUT_MS),
};

View File

@ -2,10 +2,9 @@ import { getVip98kInvocationHeaders } from "./vip98k-task-invocation";
type RecordValue = Record<string, unknown>;
export type Vip98kChatInvocation = {
protocol: "openai-chat" | "openai-responses" | "anthropic-messages";
path: "/v1/chat/completions" | "/v1/responses" | "/v1/messages";
protocol: "openai-chat" | "openai-responses";
path: "/v1/chat/completions" | "/v1/responses";
headers: Record<string, string>;
omitSampling?: boolean;
};
function record(value: unknown): RecordValue | null {
@ -44,31 +43,23 @@ export function selectVip98kChatInvocation(value: unknown): Vip98kChatInvocation
return (!item.method || String(item.method).toUpperCase() === "POST") &&
(!item.kind || item.kind === "chat") &&
(!modelId || !example?.model || example.model === modelId) &&
["/v1/chat/completions", "/v1/responses", "/v1/messages"].includes(String(item.path));
(item.path === "/v1/chat/completions" || item.path === "/v1/responses");
});
// Prefer the published default route when it supports this protocol. An
// unscoped protocol example must not discard a route's execution contract.
candidates.sort((a, b) => {
const score = (item: RecordValue) => {
const route = getVip98kInvocationHeaders(item.headers)["X-Relay-Route-Option"];
return (route && route === defaultRoute ? 4 : route ? 2 : 0) + (item.path === "/v1/messages" ? 0 : 1);
return route && route === defaultRoute ? 2 : route ? 1 : 0;
};
return score(b) - score(a);
});
const selected = candidates[0];
if (!selected) return null;
const selectedRoute = getVip98kInvocationHeaders(selected.headers)["X-Relay-Route-Option"];
const schemaSource = candidates.find(candidate => candidate.path === selected.path &&
getVip98kInvocationHeaders(candidate.headers)["X-Relay-Route-Option"] === selectedRoute &&
(candidate.input_schema || candidate.inputSchema));
const properties = record(record(schemaSource?.input_schema || schemaSource?.inputSchema)?.properties);
const fixedSampling = selected.omitSampling === true || ["temperature", "top_p"].some(key =>
record(properties?.[key])?.const !== undefined);
return {
protocol: selected.path === "/v1/messages" ? "anthropic-messages" : selected.path === "/v1/responses" ? "openai-responses" : "openai-chat",
protocol: selected.path === "/v1/responses" ? "openai-responses" : "openai-chat",
path: selected.path as Vip98kChatInvocation["path"],
headers: getVip98kInvocationHeaders(selected.headers),
...(fixedSampling ? { omitSampling: true } : {}),
};
}

View File

@ -1,12 +1,8 @@
import { canonicalVip98kVideoRatio, vip98kVideoDurationBounds, vip98kVideoContentTypes, vip98kVideoImageRoles, vip98kFirstFrameRatio } from "./vip98k-video-parameters";
import { VIP98K_TASK_POLL_SECONDS } from "./vip98k-runtime-config";
import { canonicalVip98kVideoRatio, vip98kVideoDurationBounds } from "./vip98k-video-parameters";
import {
hasVerifiedVip98kGptImage25ResolutionTiers,
hasVerifiedVip98kOpenAiImageArrayContract,
normalizeVip98kInvocationHeaders,
getVip98kImageBindingKeys,
vip98kReferenceLimit,
selectVip98kTaskInvocation,
} from "@/lib/ai/vip98k-task-invocation";
import { selectVip98kChatInvocation, vip98kChatSupportsVision } from "./vip98k-chat-contract";
@ -147,7 +143,6 @@ const IMAGE_TOOL_MODEL_IDS = new Set([
"id-photo",
"image-expansion",
"image-upscaler",
"ocr",
"photo-cleanup",
"remove-background",
"remove-object",
@ -352,8 +347,7 @@ function getInvocationRequestKeys(model: Vip98kCatalogModel) {
return [
...Object.keys(toRecord(invocation.request_example) || {}),
...Object.keys(schemaProperties || {}),
].filter(key => schemaProperties?.[key] !== false && toRecord(schemaProperties?.[key])?.maxItems !== 0)
.map(normalizeInvocationRequestKey);
].map(normalizeInvocationRequestKey);
});
}
@ -431,10 +425,7 @@ function hasMediaReferenceInput(
// published request body only accepts a text prompt.
if (type === "image" && getInvocationCandidates(model).length > 0) {
return (
getInvocationCandidates(model).some(invocation => getVip98kImageBindingKeys({
requestExample: invocation.request_example || {},
inputSchema: invocation.input_schema,
} as any).length > 0) ||
hasMediaRequestField ||
hasVerifiedVip98kOpenAiImageArrayContract(modelId)
);
}
@ -475,7 +466,6 @@ function hasMultiImageReferenceInput(model: Vip98kCatalogModel) {
return false;
}
const schema = toRecord(schemaProperties[key]);
if (schemaProperties[key] === false || schema?.maxItems === 0) return false;
return (
Array.isArray(requestExample[key]) ||
String(schema?.type || "").trim().toLowerCase() === "array"
@ -515,10 +505,7 @@ export function isVip98kCatalogModelEligible(model: Vip98kCatalogModel) {
if (!getModelId(model) || !isVip98kCatalogModelAvailable(model)) return false;
const type = resolvePresetType(model);
if (type === "image") {
if (VIP98K_MULTI_IMAGE_MODEL_EXCLUSIONS.has(getModelId(model).toLowerCase())) return false;
return isVip98kMultiImageEditCatalogModel(model) ||
(isVip98kTextToImageCatalogModel(model) && getInvocationCandidates(model).some(invocation =>
invocation.path === "/v1/images/generations" || invocation.path === "/extensions/v1/tasks"));
return isVip98kMultiImageEditCatalogModel(model);
}
if (type === "video") return hasMediaReferenceInput(model, type);
return true;
@ -568,14 +555,6 @@ function getSupportedImageSizes(model: Vip98kCatalogModel) {
if (hasVerifiedVip98kGptImage25ResolutionTiers(getModelId(model))) {
return ["1K", "2K", "4K"];
}
const primary = toRecord(toRecord(model.invocation?.input_schema)?.properties) || {};
const resolutionEnum = parseStringArray(toRecord(primary.resolution)?.enum);
if (resolutionEnum.length) return [...new Set(resolutionEnum.map(normalizeImageSize).filter((size): size is string => Boolean(size)))];
// The published tier can include portrait presets with a longer pixel edge.
const declaredSizes = getSupportedResolutions(model)
.map(normalizeImageSize)
.filter((size): size is string => Boolean(size));
if (declaredSizes.length) return [...new Set(declaredSizes)];
const schemaSizes = getInvocationCandidates(model).flatMap((invocation) => {
const properties =
toRecord(toRecord(invocation.input_schema)?.properties) || {};
@ -583,7 +562,7 @@ function getSupportedImageSizes(model: Vip98kCatalogModel) {
(key) => parseStringArray(toRecord(properties[key])?.enum),
);
});
const sizes = schemaSizes
const sizes = [...getSupportedResolutions(model), ...schemaSizes]
.map(normalizeImageSize)
.filter((value): value is string => Boolean(value));
return sizes.length ? Array.from(new Set(sizes)) : [DEFAULT_IMAGE_SIZE];
@ -722,7 +701,7 @@ function getVip98kMetadata(model: Vip98kCatalogModel, type: string) {
pollIntervalSeconds: parseNumber(
model.pollIntervalSeconds || model.poll_interval_seconds,
) || 8,
maxPollSeconds: parseNumber(model.maxPollSeconds || model.max_poll_seconds) || VIP98K_TASK_POLL_SECONDS,
maxPollSeconds: parseNumber(model.maxPollSeconds || model.max_poll_seconds) || 600,
resultTTLSeconds: parseNumber(
model.resultTTLSeconds || model.result_ttl_seconds,
),
@ -759,17 +738,9 @@ function createImagePreset(
"ratio",
"aspect-ratio",
].some((key) => invocationKeys.has(key)) || hasVerifiedVip98kOpenAiImageArrayContract(modelId);
const supportsQuality = getInvocationCandidates(model).some(invocation => {
const field = toRecord(toRecord(invocation.input_schema)?.properties)?.quality;
return field !== false && Boolean(field || invocation.request_example?.quality !== undefined) &&
!toRecord(field)?.const;
});
const supportsQuality = invocationKeys.has("quality");
// Flare repeatedly returns 3:4/4:3 for 4:5/5:4, even with aspect_ratio supplied.
const declaredRatios = getInvocationCandidates(model).flatMap(invocation => {
const props = toRecord(toRecord(invocation.input_schema)?.properties) || {};
return ["aspect_ratio", "ratio", "size"].flatMap(key => parseStringArray(toRecord(props[key])?.enum).filter(value => /^\d+:\d+$/.test(value)));
});
const supportedAspectRatios = declaredRatios.length ? ["auto", ...new Set(declaredRatios)] : supportsNativeAspectRatio
const supportedAspectRatios = supportsNativeAspectRatio
? DEFAULT_ASPECT_RATIOS.filter((ratio) => modelId !== "gpt-image-2.5-flare" || !["4:5", "5:4"].includes(ratio))
: ["auto"];
const supportedModes = [
@ -778,17 +749,7 @@ function createImagePreset(
: []),
...(supportsReferenceImages ? ["image-to-image", "reference-images"] : []),
];
const metadata = getVip98kMetadata(model, "image");
const referenceInvocation = selectVip98kTaskInvocation({
modelId, kind: "image", imageCount: 1,
configuredInvocation: metadata.invocation,
configuredInvocations: metadata.invocations,
});
const declaredReferenceLimits = getVip98kImageBindingKeys(referenceInvocation).flatMap(key => {
const limit = vip98kReferenceLimit(referenceInvocation?.inputSchema, key);
return typeof limit === "number" && limit > 0 ? [limit] : [];
});
const maxReferenceImages = declaredReferenceLimits.length ? Math.min(...declaredReferenceLimits) : supportsMultiImageReference
const maxReferenceImages = supportsMultiImageReference
? 8
: supportsReferenceImages
? 1
@ -800,7 +761,7 @@ function createImagePreset(
return {
modelId,
displayName: supportsReferenceImages ? displayName : `${displayName}(仅文生图)`,
displayName,
description: model.description || `VIP98K image model (${modelId})`,
type: "image",
supportsVision: supportsReferenceImages,
@ -819,7 +780,7 @@ function createImagePreset(
supportedAspectRatios,
supportsMultiImageReference,
maxReferenceImages,
vip98k: metadata,
vip98k: getVip98kMetadata(model, "image"),
},
defaultParams: {
provider: "vip98k",
@ -855,7 +816,7 @@ function createImagePreset(
},
enableLoadingPlaceholder: true,
loadingColor: "yellow",
vip98k: metadata,
vip98k: getVip98kMetadata(model, "image"),
},
sortOrder: TYPE_SORT_ORDER.image + index,
};
@ -880,28 +841,21 @@ function createVideoPreset(
: getDurations(model));
const defaultDuration =
parseNumber(model.defaultDuration || model.default_duration) ||
parseNumber(invocation?.request_example?.duration_seconds) ||
(Number(invocation?.request_example?.duration) > 0 ? Number(invocation?.request_example?.duration) : 0) ||
durations[0] ||
durations[durations.length - 1] ||
8;
const keys = new Set(getInvocationRequestKeys(model));
const imageModes = parseStringArray(toRecord(schema.mode_image)?.enum);
const contentTypes = vip98kVideoContentTypes(invocation?.input_schema);
const contentRoles = vip98kVideoImageRoles(invocation?.input_schema);
const supportsReferenceImages = contentTypes.length ? contentTypes.includes("image_url") : capabilities.includes("image-to-video") || keys.has("file-urls") || keys.has("image") || keys.has("image-url");
const supportsFirstFrame = supportsReferenceImages && (!contentRoles || contentRoles.includes("first_frame"));
const supportsFramePair = imageModes.includes("frame") || (contentTypes.includes("image_url") && (!contentRoles || contentRoles.includes("last_frame")));
const supportsIngredients = imageModes.includes("ingredient") || (contentTypes.includes("image_url") && (!contentRoles || contentRoles.includes("reference_image")));
const requiresReference = Number(toRecord(schema.file_urls)?.minItems) > 0;
const supportsReferenceImages = capabilities.includes("image-to-video") || keys.has("file-urls") || keys.has("image") || keys.has("image-url");
const supportsFramePair = imageModes.includes("frame");
const supportsIngredients = imageModes.includes("ingredient");
const supportedModes = [
...(!requiresReference && (capabilities.includes("text-to-video") || keys.has("prompt") || (contentTypes.length === 1 && contentTypes[0] === "text")) ? ["text-to-video"] : []),
...(supportsFirstFrame ? ["first-frame"] : []),
...(capabilities.includes("text-to-video") || keys.has("prompt") ? ["text-to-video"] : []),
...(supportsReferenceImages ? ["first-frame"] : []),
...(supportsFramePair ? ["first-last-frame"] : []),
...(supportsIngredients ? ["reference-images"] : []),
];
const ratios = parseStringArray(toRecord(schema.aspect_ratio || schema.ratio)?.enum).map(canonicalVip98kVideoRatio);
const firstFrameRatio = vip98kFirstFrameRatio(invocation?.input_schema);
const maxReferenceImages = Number(toRecord(schema.file_urls)?.maxItems) || (contentTypes.includes("image_url") ? 30 : supportsReferenceImages ? 1 : 0);
const ratios = parseStringArray(toRecord(schema.aspect_ratio)?.enum).map(canonicalVip98kVideoRatio);
const maxReferenceImages = Number(toRecord(schema.file_urls)?.maxItems) || (supportsReferenceImages ? 1 : 0);
return {
modelId,
@ -922,10 +876,6 @@ function createVideoPreset(
outputModes,
supportedResolutions,
supportedRatios: ratios.length ? ratios : [...DEFAULT_VIDEO_RATIOS],
...(firstFrameRatio ? { modeConstraints: {
"first-frame": { ratioOptions: [firstFrameRatio] },
"first-last-frame": { ratioOptions: [firstFrameRatio] },
} } : {}),
durationOptions: durations,
durationRange: durations.length
? { min: durations[0], max: durations[durations.length - 1] }
@ -1091,7 +1041,6 @@ function createToolPreset(
const inputModes = getInputModes(model);
const outputModes = getOutputModes(model);
const isImageTool = type === "image_tool";
const isOcr = modelId.toLowerCase() === "ocr";
const toolOperation = IMAGE_TOOL_OPERATION_BY_MODEL_ID[modelId.toLowerCase()] || type;
return {
@ -1107,7 +1056,7 @@ function createToolPreset(
kind: type.replace(/_/g, "-"),
mediaType: isImageTool ? "image" : type === "audio" ? "audio" : "document",
modelType: type,
outputType: isOcr ? "text" : outputModes[0] || (isImageTool ? "image" : "text"),
outputType: outputModes[0] || (isImageTool ? "image" : "text"),
inputModes,
outputModes,
supportedModes: getCapabilities(model),

View File

@ -1,7 +1,4 @@
export const VIP98K_RUNTIME_CONFIG_KEY = "vip98k_runtime_config";
export const VIP98K_TASK_POLL_SECONDS = 15 * 60;
// Allow submission and result delivery in addition to the accepted task's polling budget.
export const VIP98K_IMAGE_BATCH_TIMEOUT_MS = (VIP98K_TASK_POLL_SECONDS + 5 * 60) * 1000;
export type Vip98kRuntimeConfig = {
submissionTimeoutMs: number;

View File

@ -122,7 +122,6 @@ export function resolveVip98kImageTaskSize(params: {
const IMAGE_ARRAY_INPUT_KEYS = [
"file_urls",
"image[]",
"images",
"imageUrls",
"referenceImages",
@ -221,11 +220,9 @@ export function getVip98kImageBindingKeys(invocation: Vip98kTaskInvocation | nul
if (!invocation) return [];
const body = invocation.requestExample || {};
const properties = (invocation.inputSchema?.properties || {}) as Record<string, unknown>;
const accepts = (key: string) => properties[key] !== false &&
(properties[key] as { maxItems?: number } | undefined)?.maxItems !== 0;
return [
...IMAGE_ARRAY_INPUT_KEYS.filter((key) => accepts(key) && (key in body || key in properties)),
...IMAGE_SINGLE_INPUT_KEYS.filter((key) => accepts(key) && (key in body || key in properties)),
...IMAGE_ARRAY_INPUT_KEYS.filter((key) => key in body || key in properties),
...IMAGE_SINGLE_INPUT_KEYS.filter((key) => key in body || key in properties),
...(Array.isArray(body.video_clips) ? ["video_clips" as const] : []),
];
}
@ -239,7 +236,6 @@ export function invocationSupportsVip98kImages(
export function bindVip98kImages(
body: Record<string, unknown>,
imageUrls: string[],
inputSchema?: Record<string, unknown> | null,
) {
const bindingKeys: string[] = [];
if (imageUrls.length === 0) {
@ -249,12 +245,7 @@ export function bindVip98kImages(
for (const key of IMAGE_ARRAY_INPUT_KEYS) {
if (!(key in body)) continue;
const example = Array.isArray(body[key]) ? body[key][0] : undefined;
const properties = inputSchema?.properties as Record<string, { items?: { properties?: Record<string, unknown> } }> | undefined;
const objectReference = example && typeof example === "object" && "image_url" in example;
body[key] = objectReference || properties?.[key]?.items?.properties?.image_url
? imageUrls.map(image_url => ({ image_url }))
: imageUrls;
body[key] = imageUrls;
bindingKeys.push(key);
}
for (const key of IMAGE_SINGLE_INPUT_KEYS) {
@ -312,52 +303,9 @@ function scoreReferenceInvocation(invocation: Vip98kTaskInvocation) {
if (IMAGE_SINGLE_INPUT_KEYS.some((key) => key in body)) score += 15;
if (invocation.kind === "task") score += 10;
if (invocation.path === "/extensions/v1/tasks") score += 10;
if (invocation.contentType === "application/json") score += 5;
return score;
}
export function vip98kReferenceLimit(schema: unknown, key: string): number | undefined {
if (!schema || typeof schema !== "object" || Array.isArray(schema)) return;
const current = schema as Record<string, any>;
const limits: number[] = [];
const visitField = (field: any) => {
if (!field || typeof field !== "object") return;
if (typeof field.maxItems === "number") limits.push(field.maxItems);
for (const child of Array.isArray(field.allOf) ? field.allOf : []) visitField(child);
};
visitField(current.properties?.[key]);
for (const child of Array.isArray(current.allOf) ? current.allOf : []) {
const limit = vip98kReferenceLimit(child, key);
if (limit !== undefined) limits.push(limit);
}
return limits.length ? Math.min(...limits) : undefined;
}
export function getVip98kQualityOptions(schema: Record<string, unknown> | null | undefined): string[] | undefined {
const field = (schema?.properties as Record<string, unknown> | undefined)?.quality;
if (field === false) return [];
if (!field || typeof field !== "object") return undefined;
const quality = field as Record<string, unknown>;
if (typeof quality.const === "string") return [quality.const];
return Array.isArray(quality.enum) ? quality.enum.filter((value): value is string => typeof value === "string") : undefined;
}
export function encodeVip98kTaskBody(body: Record<string, unknown>, contentType?: string | null): { body: string | FormData; headers: Record<string, string> } {
if (contentType?.split(";", 1)[0].trim() !== "multipart/form-data") {
return { body: JSON.stringify(body), headers: { "Content-Type": contentType || "application/json" } };
}
const form = new FormData();
for (const [key, value] of Object.entries(body)) {
if (value === undefined || value === null) continue;
if (key === "image[]" && Array.isArray(value)) {
for (const image of value) form.append(key, String(image));
} else {
form.append(key, typeof value === "object" ? JSON.stringify(value) : String(value));
}
}
return { body: form, headers: {} };
}
export function selectVip98kTaskInvocation(params: {
modelId: string;
kind?: string;
@ -382,22 +330,14 @@ export function selectVip98kTaskInvocation(params: {
);
if ((params.imageCount || 0) > 0) {
const references = candidates.filter(invocationSupportsVip98kImages);
const routedReferences = references.filter(candidate =>
getVip98kInvocationHeaders(candidate.headers)["X-Relay-Route-Option"]);
const referenceInvocation = (routedReferences.length ? routedReferences : references)
const referenceInvocation = candidates
.filter(invocationSupportsVip98kImages)
.sort((a, b) => scoreReferenceInvocation(b) - scoreReferenceInvocation(a))[0];
if (referenceInvocation) {
return resolveVip98kTaskInvocation(params.modelId, referenceInvocation);
}
}
if (params.configuredInvocation &&
getVip98kInvocationHeaders(params.configuredInvocation.headers)["X-Relay-Route-Option"] &&
!params.configuredInvocation.path.includes(":generateContent")) {
return resolveVip98kTaskInvocation(params.modelId, params.configuredInvocation);
}
if (params.kind === "image" && !params.imageCount) {
const textImageTask = candidates.find(candidate => {
const properties = candidate.inputSchema?.properties as Record<string, unknown> | undefined;
@ -483,6 +423,7 @@ function createVerifiedOpenAiImageInvocation(modelId: string) {
function addVerifiedOpenAiImageFields(
invocation: Vip98kTaskInvocation,
modelId: string,
): Vip98kTaskInvocation {
const inputSchema = invocation.inputSchema || {};
const properties =
@ -501,8 +442,16 @@ function addVerifiedOpenAiImageFields(
...inputSchema,
properties: {
...properties,
image: properties.image ?? { items: { type: "string" }, type: "array" },
aspect_ratio: properties.aspect_ratio ?? { type: "string" },
image: { items: { type: "string" }, type: "array" },
aspect_ratio: { type: "string" },
...(hasVerifiedVip98kGptImage25ResolutionTiers(modelId)
? {
size: {
enum: ["1024x1024", "2048x2048", "2880x2880"],
type: "string",
},
}
: {}),
},
},
};
@ -524,7 +473,7 @@ export function resolveVip98kTaskInvocation(
invocationTargetsModel(configuredInvocation, normalizedModelId)
? configuredInvocation
: createVerifiedOpenAiImageInvocation(modelId);
return addVerifiedOpenAiImageFields(invocation);
return addVerifiedOpenAiImageFields(invocation, normalizedModelId);
}
const processingRequest = KNOWN_IMAGE_PROCESSING_TOOL_REQUESTS[normalizedModelId];
if (processingRequest) {

View File

@ -6,30 +6,12 @@ export function canonicalVip98kVideoRatio(value: string) {
return value === "landscape" ? "16:9" : value === "portrait" ? "9:16" : value;
}
export function vip98kFirstFrameRatio(schema: unknown): string | undefined {
const current = object(schema);
const content = object(object(object(current.if).properties).content);
const role = object(object(object(content.contains).properties).role).const;
const properties = object(object(current.then).properties);
const ratio = object(properties.ratio || properties.aspect_ratio).const;
if (role === "first_frame" && typeof ratio === "string") return ratio;
for (const child of Array.isArray(current.allOf) ? current.allOf : []) {
const value = vip98kFirstFrameRatio(child);
if (value) return value;
}
}
export function vip98kVideoDurationBounds(schema: unknown, resolution?: string) {
let min = 0, max = Infinity;
let durations: number[] | undefined;
const visit = (value: unknown) => {
const current = object(value), properties = object(current.properties);
const field = object(properties.duration_seconds || properties.duration);
const positiveRange = (Array.isArray(field.anyOf) ? field.anyOf : []).find((x: any) => x.minimum > 0);
if (positiveRange) {
min = Math.max(min, positiveRange.minimum);
max = Math.min(max, positiveRange.maximum ?? Infinity);
}
if (Number.isFinite(field.minimum)) min = Math.max(min, field.minimum);
if (Number.isFinite(field.maximum)) max = Math.min(max, field.maximum);
const allowed = Array.isArray(field.enum) ? field.enum : Number.isFinite(field.const) ? [field.const] : undefined;
@ -47,13 +29,6 @@ export function vip98kVideoDurationBounds(schema: unknown, resolution?: string)
export function mapVip98kVideoParameters(body: Record<string, unknown>, schema: unknown) {
const properties = object(object(schema).properties);
const frameRatio = vip98kFirstFrameRatio(schema);
if (frameRatio && Array.isArray(body.content) && body.content.some(item => object(item).role === "first_frame")) {
const field = properties.ratio ? "ratio" : "aspect_ratio";
if (body[field] && body[field] !== "auto" && body[field] !== frameRatio)
throw Error("当前首帧模式沿用原图比例,请选择自动比例");
body[field] = frameRatio;
}
const ratios = object(properties.aspect_ratio).enum;
if (Array.isArray(ratios) && !ratios.includes(body.aspect_ratio)) {
const alias = body.aspect_ratio === "16:9" ? "landscape" : body.aspect_ratio === "9:16" ? "portrait" : undefined;
@ -61,11 +36,8 @@ export function mapVip98kVideoParameters(body: Record<string, unknown>, schema:
body.aspect_ratio = alias;
}
const resolutions = object(properties.resolution).enum;
if (body.resolution && Array.isArray(resolutions) && !resolutions.includes(body.resolution)) {
const matched = resolutions.find(value => String(value).toLowerCase() === String(body.resolution).toLowerCase());
if (!matched) throw Error("供应商当前线路不支持所选视频清晰度,请同步模型规格后重试");
body.resolution = matched;
}
if (body.resolution && Array.isArray(resolutions) && !resolutions.includes(body.resolution))
throw Error("供应商当前线路不支持所选视频清晰度,请同步模型规格后重试");
const seconds = Number(body.duration_seconds ?? body.duration);
if (seconds) {
const bounds = vip98kVideoDurationBounds(schema, String(body.resolution || ""));
@ -73,45 +45,3 @@ export function mapVip98kVideoParameters(body: Record<string, unknown>, schema:
throw Error("供应商当前清晰度不支持所选视频时长,请调整镜头时长");
}
}
export function vip98kVideoContentTypes(schema: unknown): string[] {
const content = object(object(object(schema).properties).content);
const items = object(content.items);
return (Array.isArray(items.oneOf) ? items.oneOf : []).flatMap((variant: unknown) => {
const type = object(object(object(variant).properties).type).const;
return typeof type === "string" ? [type] : [];
});
}
export function vip98kVideoImageRoles(schema: unknown): string[] | undefined {
const variants = object(object(object(object(schema).properties).content).items).oneOf;
const image = (Array.isArray(variants) ? variants : []).find(value => object(object(object(value).properties).type).const === "image_url");
const roles = object(object(object(image).properties).role).enum;
return Array.isArray(roles) ? roles.map(String) : undefined;
}
export function bindVip98kVideoContent(body: Record<string, unknown>, schema: unknown, input: {
prompt: string; images: string[]; videos: string[]; audios: string[]; mode?: unknown;
}) {
const types = vip98kVideoContentTypes(schema);
if (!types.length) return false;
if (input.images.length && !types.includes("image_url")) throw Error("所选视频模型只支持文字生成,不能添加参考图片");
if (input.videos.length && !types.includes("video_url")) throw Error("所选模型不支持参考视频");
if (input.audios.length && !types.includes("audio_url")) throw Error("所选模型不支持参考音频");
const reference = input.mode === "reference-images";
const roles = vip98kVideoImageRoles(schema);
const role = reference ? "reference_image" : "first_frame";
if (input.images.length && roles && !roles.includes(role)) throw Error("所选视频模型不支持这个参考图模式");
if (!reference && input.images.length > 2) throw Error("首尾帧模式最多支持两张图片");
if (!reference && input.images.length === 2 && roles && !roles.includes("last_frame")) throw Error("所选视频模型不支持尾帧");
if (["first-frame", "first-last-frame", "reference-images"].includes(String(input.mode)) && !input.images.length) throw Error("请先添加视频参考图");
body.content = [
...(input.prompt ? [{ type: "text", text: input.prompt }] : []),
...input.images.map((url, index) => ({ type: "image_url", image_url: { url }, role: reference ? "reference_image" : index ? "last_frame" : "first_frame" })),
...input.videos.map(url => ({ type: "video_url", video_url: { url }, role: "reference_video" })),
...input.audios.map(url => ({ type: "audio_url", audio_url: { url }, role: "reference_audio" })),
];
delete body.prompt;
delete body.file_urls;
return true;
}

View File

@ -7,8 +7,7 @@ import type { CanvasAgentTool } from "@/lib/ai/canvas-agent-runtime/loop";
import { AgentToolRejectedError, publicAgentError, type AgentCallRecord, type AgentRunSnapshot, type CanvasAgentRunStatus } from "@/lib/ai/canvas-agent-runtime/protocol";
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
import { isCompletedSimpleCutout } from "@/lib/ai/canvas-agent-runtime/cutout-completion";
import { countAgentGeneratedImages, requiresCanvasInteraction } from "@/lib/ai/canvas-agent-runtime/results";
import { selectedAgentImageCount } from "@/lib/ai/canvas-agent-runtime/image-preferences";
import { requiresCanvasInteraction } from "@/lib/ai/canvas-agent-runtime/results";
import { AgentImageGenerationError } from "@/lib/ai/runtime/image-generation-error";
import { publicAssistantText } from "./assistant-output";
@ -50,16 +49,6 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
if (!details.recommendation) details.recommendation = "请联系管理员并提供请求编号,核对该步骤的错误。";
return `${call.title || "创作工具"}:${formatRequestFailure(details, ports.runId)}`;
}
function imageCountReached() {
const count = snapshot.workspace === "video" ? undefined : selectedAgentImageCount(snapshot.imagePreferences);
return count !== undefined && countAgentGeneratedImages(snapshot.calls) >= count;
}
function reviewMessage(issues: string[]) {
const summary = `结果待核对:${issues.join(";")}`;
return imageCountReached()
? `${summary}\n\n已保留本次生成的图片。如需再生成,请发送新指令。`
: summary;
}
let modelError = "";
let toolFailure = "";
let nativeFailed = false;
@ -95,7 +84,6 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
`可用业务工具:${tools.join("、")}。工具名称带 mcp__canvas__ 前缀。`,
"原生文件、Bash、记忆及子任务能力仅作用于此任务的私有工作区,不能通过 shell 直接访问模型服务商或本站后台;业务操作使用 MCP。",
"用户只要求生图或改图时,把结果写入画布并验收即可。除非用户明确要求下载或导出,不要主动调用 image_download,也不要为收尾打开交互面板。",
"用户设置的生成张数是本次任务的总数,包括验收未通过的已生成图片。达到张数后,保留结果并说明问题;如需再次生成,等待用户发送新指令。",
"用户指定的生图模型被明确拒绝时,停止本轮收费操作,询问用户希望切换生图模型还是稍后重试。切换需用户确认并更新模型选择;结果未知的超时必须先核对原任务,不可直接换模型重发。不要为普通图片编辑调用 appearance.post 或其他无关业务接口。",
"imagePreferences 是用户在输入框选择的生图模型和图片参数,与当前规划模型不同。生成和编辑图片优先用 generateCanvasImages;用户选定的模型不得被技能默认值、你选的其他模型或重试覆盖。自动选择生图模型时,图片工具内部会在明确失败后切换备用模型并补齐结果,不要自行重放付费操作;用户明确指定模型时禁止故障切换。比例、尺寸、数量、反向提示词等控件必须传入;不要通过其他生成接口绕过用户选择。分析、问答、排版等不需要出图的任务无需生成图片。",
"转正面、换角度、旋转主体视角时,直接用 generateCanvasImages,referenceNodeIds 指向要编辑的真实图片,提示词描述目标视角并保留产品结构和未要求修改的细节。旧多视角功能、angleEditImage 和 image.multi_angle 已废弃,即使历史对话用过也不得再调用或打开旧面板。",
@ -289,12 +277,12 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
const verification = await ports.verify(reviewSnapshot); snapshot.verification = verification;
snapshot.inputTokens = reviewSnapshot.inputTokens; snapshot.outputTokens = reviewSnapshot.outputTokens;
if (verification.passed) return await save(result.response || "任务完成,结果已写入画布。", "completed");
if (verification.retryable && snapshot.repairs < 2 && !imageCountReached()) {
if (verification.retryable && snapshot.repairs < 2) {
snapshot.repairs++;
prompt = `验收需要定点修复:${JSON.stringify(verification)}。只处理失败结果,禁止整批重新生成;保留所有通过结果。`;
continue;
}
return await save(reviewMessage(verification.issues), "needs_review");
return await save(`结果待核对:${verification.issues.join(";")}`, "needs_review");
}
} catch (error) {
if (snapshot.nativeRuntime) snapshot.nativeRuntime.state = "stopped";
@ -318,7 +306,7 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
if (control.cancelled || ports.signal.aborted) return await save("已停止后续步骤,完成的结果保留在画布。", "cancelled");
if (inputs !== JSON.stringify(snapshot.inputs)) return await save("已收到新的补充要求,图片已保留,请继续处理。", "needs_review");
return await save(verification.passed ? "图片已完成并通过检查,结果已保留在画布。"
: `图片已返回画布,${reviewMessage(verification.issues)}`, verification.passed ? "completed" : "needs_review");
: `图片已返回画布,结果待核对:${verification.issues.join(";")}`, verification.passed ? "completed" : "needs_review");
} catch { /* Keep the original interruption if independent inspection also fails. */ }
}
return await save(cancelled ? "已停止后续步骤,完成的结果保留在画布。" : toolFailure || modelError || publicAgentError(error), cancelled ? "cancelled" : "needs_review");

View File

@ -1,45 +0,0 @@
import "server-only";
import { randomUUID } from "node:crypto";
import sharp from "sharp";
import { fetchImageBufferFromUrl } from "@/lib/ai/rmbg/service";
import { uploadFile } from "@/lib/storage/service";
export async function padImageToSquare(bytes: Buffer) {
const source = sharp(bytes).autoOrient();
const metadata = await source.metadata();
const { width, height } = metadata.autoOrient;
if (!width || !height) throw new Error("无法读取生成图尺寸");
if (width === height) return null;
const size = Math.max(width, height);
const left = Math.floor((size - width) / 2);
const top = Math.floor((size - height) / 2);
const { data, info } = await source.clone().ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const corners = [0, (width - 1) * info.channels, (height - 1) * width * info.channels, (width * height - 1) * info.channels];
const sample = (channel: number) =>
Math.round(corners.reduce((sum, offset) => sum + data[offset + channel], 0) / corners.length);
const background = { r: sample(0), g: sample(1), b: sample(2), alpha: sample(3) / 255 };
return source.extend({ left, right: size - width - left, top, bottom: size - height - top, background })
.keepIccProfile()
.png()
.toBuffer();
}
export async function squareBatchInstructionOutput(image: { originalUrl: string; thumbnailUrl: string }) {
const bytes = await fetchImageBufferFromUrl(image.originalUrl);
const padded = await padImageToSquare(bytes);
if (!padded) return image;
const thumbnail = await sharp(padded).resize(512, 512, { withoutEnlargement: true }).png().toBuffer();
const id = randomUUID();
const upload = (name: string, file: Buffer) => uploadFile(
name,
new Uint8Array(file).buffer,
{ preferQiniuForm: true },
);
const [original, preview] = await Promise.all([
upload(`batch-instruction-${id}.png`, padded),
upload(`batch-instruction-${id}-thumb.png`, thumbnail),
]);
return { ...image, originalUrl: original.url, thumbnailUrl: preview.url };
}

View File

@ -1,139 +0,0 @@
import "server-only";
import sharp from "sharp";
import { callQwen3Vl } from "@/lib/ai/qwen3-vl";
import { fetchImageBufferFromUrl } from "@/lib/ai/rmbg/service";
import type { CanvasGenerateImageRequest, CanvasLocalEditCompositeRegion } from "./generate-image";
export function parseBatchInstructionRegion(value: unknown): CanvasLocalEditCompositeRegion | null {
if (!Array.isArray(value) || value.length !== 4 || !value.every(
point => typeof point === "number" && Number.isFinite(point) && point >= 0 && point <= 1000,
)) return null;
const [left, top, right, bottom] = value;
if (right - left < 10 || bottom - top < 10) return null;
return {
x: left / 1000, y: top / 1000,
width: (right - left) / 1000, height: (bottom - top) / 1000,
};
}
const faceSubject = /人脸|脸部|脸型|五官|面容|发型|face|facial features|hairstyle/i;
const faceAction = /替换|换成|换为|置换|改成|改为|swap|replace|transfer|change/i;
function requestsFaceEdit(instruction: string) {
return instruction.split(/[,,。;;!!??\n]+/).some((clause) => {
const editable = clause.split(/保持|保留|维持|不要|别|不得|禁止|避免|do not|don't|keep|preserve/i, 1)[0];
return /换(?:人?脸|五官|发型)|face[- ]swap/i.test(editable)
|| (faceSubject.test(editable) && faceAction.test(editable));
});
}
function directFaceEdit(request: CanvasGenerateImageRequest): CanvasGenerateImageRequest {
return {
...request,
batchInstructionFaceEdit: true,
requiredReferenceImageCount: 2,
prompt: [
"第一张是要编辑的完整底图。第二张只提供指令指定的面部或头发特征。",
"只局部编辑底图人物头部。仅修改指令明确要求的五官、脸型、发型或发色,保留其余头部特征。匹配底图的头部位置、转头或低头角度、视线、遮挡、肤色及场景光线,脸部和颈部自然衔接。",
"保留原始画幅、姿势、手、道具、服饰颜色与印花、背景物件、标牌、文字和非头部区域的原有纹理。避免裁切、全图重绘、锐化或改色。输出一张完整成品图。",
"用户指令:" + request.displayPrompt,
].join("\n"),
};
}
function expandProductRegion(region: CanvasLocalEditCompositeRegion) {
const xMargin = region.width * 0.11;
const yMargin = region.height * 0.11;
const x = Math.max(0, region.x - xMargin);
const y = Math.max(0, region.y - yMargin);
return {
x, y,
width: Math.min(1, region.x + region.width + xMargin) - x,
height: Math.min(1, region.y + region.height + yMargin) - y,
};
}
async function cropReference(url: string, region: CanvasLocalEditCompositeRegion) {
const bytes = await fetchImageBufferFromUrl(url);
const oriented = await sharp(bytes).autoOrient().png().toBuffer({ resolveWithObject: true });
const { width, height } = oriented.info;
const left = Math.floor(region.x * width);
const top = Math.floor(region.y * height);
return sharp(oriented.data).extract({
left, top,
width: Math.min(width - left, Math.ceil(region.width * width)),
height: Math.min(height - top, Math.ceil(region.height * height)),
}).resize({ width: 1280, height: 1280, fit: "inside", withoutEnlargement: true }).png().toBuffer();
}
export async function prepareBatchInstructionWithReference(request: CanvasGenerateImageRequest) {
const [base, reference] = request.referenceImages || [];
if (!base || !reference) throw new Error("局部指令编辑缺少原图或共用参考图");
if (requestsFaceEdit(request.displayPrompt || "")) return directFaceEdit(request);
let text: string;
try {
({ text } = await callQwen3Vl({
imageUrls: [base, reference],
temperature: 0,
maxTokens: 280,
timeoutMs: 25_000,
retries: 0,
prompt: [
"定位图片编辑目标。图1是要编辑的原图,图2只提供参照特征。",
"只输出 JSON,格式为 {\"scope\":\"local\",\"base_box\":[x1,y1,x2,y2],\"reference_box\":[x1,y1,x2,y2]}。",
"两个框分别使用各自图片的 0 至 999 归一化坐标。",
"指令明确要求换脸、换五官、脸型或发型时,直接返回 {\"scope\":\"face\"}。只要求保留这些特征时,按实际编辑目标判断。",
"替换商品时,图1框住指定旧商品的完整外轮廓,包括扶手和腿。图2框住替换商品,勿框入其他人或物。",
"只有指令明确要求修改整张图的整体风格、色调或场景时,返回 {\"scope\":\"full\"}。",
"找不到明确目标,或多张脸、多件商品无法确定哪一个时,返回 {\"scope\":\"unresolved\"}。",
"用户指令:" + request.displayPrompt,
].join("\n"),
}));
} catch {
throw new Error("暂时无法识别要编辑的区域,生图任务尚未提交,请稍后重试");
}
let result: { scope?: string; base_box?: unknown; reference_box?: unknown } = {};
try {
const match = text.match(/\{[\s\S]*\}/);
if (match) result = JSON.parse(match[0]) || {};
} catch { /* Invalid or ambiguous localization is reported below. */ }
if (result.scope === "face") return directFaceEdit(request);
const requiresLocalTarget = /换商品|换产品|(?:商品|产品|主体).{0,8}替换|替换.{0,10}(?:商品|产品|主体|模特)|face swap|replace (?:the )?(?:face|product)/i.test(request.displayPrompt || "");
if (result.scope === "full" && !requiresLocalTarget) return request;
const baseRegion = parseBatchInstructionRegion(result.base_box);
const referenceRegion = parseBatchInstructionRegion(result.reference_box);
if (result.scope !== "local" || !baseRegion || !referenceRegion) {
throw new Error("未能定位唯一替换目标。请在指令里描述位置或特征,再重试这张图片");
}
// Alibaba can use the cropped bytes directly without writing a new reference object.
const target = expandProductRegion(baseRegion);
const cropped = await cropReference(reference, referenceRegion).catch(() => null);
return {
...request,
prompt: [
"这是局部编辑。以第一张图为唯一底图。图2只提供目标特征。",
"把修改放回原目标的位置和大小范围内。保持原目标的中心、落地位置或五官定位,保留原图其他内容。",
"完整移除原目标的轮廓、腿和残影,保持原物件的落地位置。",
"禁止添加图1原本没有的人物、物件、文字或标记。只输出一张与图1原构图对齐的图片。",
"指令:" + request.displayPrompt,
].join("\n"),
referenceImageBuffers: cropped ? new Map([[reference, cropped]]) : undefined,
localEditComposite: {
sourceImageUrl: base,
regions: [target],
expandRegions: false,
maskMode: "region",
featherMinPx: 7,
featherMaxPx: 20,
featherRatio: 0.012,
},
} satisfies CanvasGenerateImageRequest;
}

View File

@ -4,6 +4,7 @@ import { executeLayerSplitJob } from "@/lib/ai/layer-split/service";
import { executeResizeJob } from "@/lib/ai/resize/service";
import {
executeRmbgJob,
fetchImageBufferFromUrl,
RmbgServiceError,
} from "@/lib/ai/rmbg/service";
import type { CanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
@ -125,11 +126,12 @@ async function executeForTarget(
};
if (command.capabilityId === "image.remove_background") {
const imageBuffer = await fetchImageBufferFromUrl(target.sourceImageUrl);
const result = await executeRmbgJob({
requestId: command.requestId,
userId: params.userId,
imageBuffer,
inputImageUrl: target.sourceImageUrl,
includeBase64: false,
sourceImageUrl: target.sourceImageUrl,
prompt: prompt || "抠图处理",
skipGallerySave: false,
@ -255,12 +257,10 @@ async function executeForTarget(
imageUrl: target.sourceImageUrl,
prompt: prompt || "图像增强处理",
scaleFactor: asFiniteNumber(command.args.scaleFactor),
includeBase64: false,
},
pollingParams: {
prompt: prompt || "图像增强处理",
sourceImageUrl: target.sourceImageUrl,
includeBase64: "false",
},
timeoutMs: 10 * 60 * 1000,
headers: getCookieHeaders(params.requestHeaders),

View File

@ -1,57 +0,0 @@
import { createReadStream } from "node:fs";
import { stat, writeFile, rm } from "node:fs/promises";
import { join } from "node:path";
import JSZip from "jszip";
import { writeDownloadZip } from "./download-zip-stream";
import { assertZipDiskSpace, saveZipTask, zipTaskPath, type ZipTask } from "./download-zip-task-store";
import type { CanvasDownloadAssetResult } from "./photoshop-safe-download";
export function buildZipContentDisposition(folderName: string) {
const name = `${folderName}.zip`;
const asciiName = folderName.replace(/[^\x20-\x7E]+/g, "").replace(/"/g, "").trim() || "canvas-images";
return `attachment; filename="${asciiName}.zip"; filename*=UTF-8''${encodeURIComponent(name)}`;
}
export async function prepareCanvasZip(
task: ZipTask,
fetchAsset: (params: { file: ZipTask["files"][number]; index: number; signal: AbortSignal }) => Promise<CanvasDownloadAssetResult>,
) {
delete task.zipFile; delete task.contentDisposition; delete task.cdnUrl;
// Stage each image on disk, not all PNG buffers in the web process heap.
// A restart resumes from these files using the current owner's session.
let cursor = 0;
let writes = Promise.resolve();
const persist = () => { const current = task; writes = writes.then(() => saveZipTask(current)); return writes; };
const results = await Promise.allSettled(Array.from({ length: Math.min(2, task.files.length) }, async () => {
while (cursor < task.files.length) {
const index = cursor++; const file = task.files[index];
const path = join(zipTaskPath(task.id), `${index}.image`);
if (file.prepared && file.filename && (await stat(path).catch(() => null))?.size) continue;
delete file.prepared; delete file.filename;
const result = await fetchAsset({ file, index,
signal: AbortSignal.timeout(120_000) }).catch(() => ({ ok: false as const, error: "原图读取超时或源地址暂时不可用" }));
if (result.ok) {
await assertZipDiskSpace(result.bytes.length);
await writeFile(path, result.bytes, { mode: 0o600 });
file.prepared = `${index}.image`; file.filename = result.filename; delete file.error;
} else file.error = result.error;
task.processedCount = task.files.filter(f => f.prepared || f.error).length;
task.successCount = task.files.filter(f => f.prepared).length;
task.failedCount = task.files.filter(f => f.error).length;
task.stage = `正在准备原图 ${task.processedCount}/${task.totalCount}`;
task.updatedAt = Date.now(); await persist();
}
}));
const rejected = results.find(result => result.status === "rejected");
if (rejected?.status === "rejected") throw rejected.reason;
if (!task.successCount) throw new Error("所选原图暂时都无法读取,请稍后重试;画布图片不会被删除");
const zip = new JSZip(); const folder = zip.folder(task.folderName) || zip;
for (const file of task.files) if (file.prepared && file.filename)
folder.file(file.filename, createReadStream(join(zipTaskPath(task.id), file.prepared))
.once("close", () => { void rm(join(zipTaskPath(task.id), file.prepared!), { force: true }).catch(() => undefined); }));
if (task.failedCount) folder.file("_download-failures.txt", task.files.filter(f => f.error)
.map(f => `${f.name || "图片"}:${f.error}`).join("\n"));
task.stage = "正在生成完整 ZIP 文件"; await saveZipTask(task);
task.zipFile = await writeDownloadZip(zip, task.id, percent => { task.compressPercent = percent; });
task.contentDisposition = buildZipContentDisposition(task.folderName);
}

View File

@ -10,8 +10,6 @@ export type ZipTask = {
totalCount: number; processedCount: number; successCount: number; failedCount: number;
compressPercent: number; status: "pending" | "running" | "completed" | "failed";
stage: string; error?: string; zipFile?: { path: string; bytes: number };
remote?: { attempt: number; storageConfigId?: string; sources?: string[][] };
attempt?: number;
contentDisposition?: string; cdnUrl?: string; cdnObject?: { storageConfigId: string; key: string };
createdAt: number; updatedAt: number; expiresAt: number;
};
@ -64,7 +62,7 @@ export async function assertZipDiskSpace(bytes = 0) {
}
let cleanupAt = 0;
export async function cleanupZipTasks(removeObject?: (object: NonNullable<ZipTask["cdnObject"]>) => Promise<void>) {
export async function cleanupZipTasks() {
if (Date.now() - cleanupAt < 60_000) return;
cleanupAt = Date.now();
for (const id of await readdir(downloadZipDirectory).catch(() => [] as string[])) {
@ -76,7 +74,10 @@ export async function cleanupZipTasks(removeObject?: (object: NonNullable<ZipTas
const release = await claimZipTask(id);
if (!release) continue;
try {
if (task.cdnObject && removeObject) await removeObject(task.cdnObject).catch(() => undefined);
if (task.cdnObject) {
const { removeCanvasZipObject } = await import("@/lib/storage/canvas-zip-delivery");
await removeCanvasZipObject(task.cdnObject).catch(() => undefined);
}
await rm(join(downloadZipDirectory, `${id}.zip`), { force: true });
await rm(join(downloadZipDirectory, `${id}.zip.partial`), { force: true });
} finally { await release(); }

View File

@ -1,36 +1,6 @@
export const AOS_ENHANCE_RESULT_TIMEOUT_MS = 30_000;
// Polling is bounded by elapsed time, including network waits. Terminal provider failures
// must escape immediately instead of being converted into a perpetual pending response.
export class EnhanceTerminalError extends Error {}
export async function readCompletedEnhanceImage(
data: { image?: unknown; image_base64?: unknown },
fetcher: typeof fetch = fetch,
): Promise<{ buffer: Buffer; mimeType: string }> {
try {
const image = data.image_base64 || data.image;
if (typeof image !== "string" || !image) throw new Error("处理结果缺少图片");
let buffer: Buffer;
if (/^https?:\/\//i.test(image)) {
const response = await fetcher(image, {
cache: "no-store",
signal: AbortSignal.timeout(60_000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
buffer = Buffer.from(await response.arrayBuffer());
} else {
buffer = Buffer.from(image, "base64");
}
const { default: sharp } = await import("sharp");
const { format } = await sharp(buffer).metadata();
if (!format || !["jpeg", "png", "webp", "tiff"].includes(format)) {
throw new Error("处理结果的图片格式不可用");
}
return { buffer, mimeType: `image/${format}` };
} catch (error) {
throw new Error("图片变清晰已完成,但结果图片未取回。", { cause: error });
}
}
export async function pollTaskResult(
apiUrl: string,
apiKey: string,
@ -47,23 +17,17 @@ export async function pollTaskResult(
{
method: "GET",
headers: { "X-API-KEY": apiKey },
signal: AbortSignal.timeout(
Math.max(1, Math.min(AOS_ENHANCE_RESULT_TIMEOUT_MS, deadline - Date.now())),
),
signal: AbortSignal.timeout(Math.max(1, Math.min(10000, deadline - Date.now()))),
},
);
if (!response.ok) throw Error(`轮询请求失败: ${response.status}`);
const result = await response.json();
if (result?.data) {
const state = Number(result.data.state);
if (state < 0) {
throw new EnhanceTerminalError(
`AOS任务失败: ${String(result.data.err_message || result.data.err_info || result.data.state_detail || result.data.state)}`,
);
}
if (response.ok && (state === 1 || (!(state > 1) && result.data.progress >= 100))) {
return result;
}
}
if (!result.data) throw Error("高清查询未返回任务状态");
if (result.data.state < 0)
throw new EnhanceTerminalError(
`AOS任务失败: ${String(result.data.err_message || result.data.err_info || result.data.state_detail || result.data.state)}`,
);
if (result.data.progress >= 100) return result;
} catch (error) {
if (error instanceof EnhanceTerminalError) throw error;
}

View File

@ -1,78 +0,0 @@
import type { FeedbackMessage } from "./feedback-contract";
export const CANVAS_FEEDBACK_CONTEXT_EVENT = "canvas-feedback-context";
export type FeedbackChatCapture = { chatId: string | null; messages: FeedbackMessage[] };
export type FeedbackCaptureEvent = CustomEvent<{
projectId: string;
receive: (value: FeedbackChatCapture) => void;
}>;
export function captureFeedbackChat(projectId: string): FeedbackChatCapture {
let result: FeedbackChatCapture = { chatId: null, messages: [] };
window.dispatchEvent(
new CustomEvent(CANVAS_FEEDBACK_CONTEXT_EVENT, {
detail: {
projectId,
receive: (value: FeedbackChatCapture) => {
result = value;
},
},
}),
);
return result;
}
export function feedbackMessages(messages: readonly unknown[]): FeedbackMessage[] {
let remaining = 48000;
return messages
.slice(-12)
.reverse()
.flatMap((value: any) => {
if (!value || !["user", "assistant"].includes(value.role) || typeof value.id !== "string")
return [];
const parts = Array.isArray(value.parts) ? value.parts : [];
let state: any = null;
for (const part of parts) {
if (part.type !== "data-imageGenerationStatus") continue;
try {
state = typeof part.data === "string" ? JSON.parse(part.data) : part.data;
} catch {
/* Keep public message text when a stream part is incomplete. */
}
}
const fullText = parts
.filter((part: any) => part.type === "text")
.map((part: any) => String(part.text || ""))
.join("\n");
const text = fullText.slice(0, Math.min(16000, remaining));
remaining -= text.length;
return [
{
id: value.id.slice(0, 180),
role: value.role,
text,
...(value.createdAt
? {
createdAt: String(
value.createdAt instanceof Date ? value.createdAt.toISOString() : value.createdAt,
).slice(0, 64),
}
: {}),
...(state?.requestId ? { requestId: String(state.requestId).slice(0, 180) } : {}),
...(state?.userMessageId
? { userMessageId: String(state.userMessageId).slice(0, 180) }
: {}),
...(state?.phase ? { status: String(state.phase).slice(0, 64) } : {}),
...(state?.failure?.message
? {
error: [state.failure.message, state.failure.recommendation]
.filter(Boolean)
.join("\n")
.slice(0, 2000),
}
: {}),
},
];
})
.reverse();
}

View File

@ -1,323 +0,0 @@
import "server-only";
import { and, asc, desc, eq, gte, inArray, sql } from "drizzle-orm";
import { db } from "@/lib/db";
import {
canvasProject,
canvasAgentRun,
canvasCapabilityTask,
canvasImageTask,
chat,
message,
user,
} from "@/lib/db/schema";
import { feedbackMessages } from "./feedback-client";
import type {
FeedbackClient,
FeedbackContext,
FeedbackMessage,
FeedbackRequest,
} from "./feedback-contract";
export class CanvasFeedbackError extends Error {
status: number;
constructor(message: string, status = 400) {
super(message);
this.status = status;
}
}
const text = (value: unknown) => (typeof value === "string" ? value : "");
const duration = (started: Date | null, completed: Date | null) =>
started && completed ? Math.max(0, completed.getTime() - started.getTime()) : null;
// The general capability store uses the database default; the video queue writes UTC.
const capabilityCreatedAt = sql<string>`to_char(
case when ${canvasCapabilityTask.capabilityId} = 'video-canvas.generation'
then ${canvasCapabilityTask.createdAt}
else ${canvasCapabilityTask.createdAt} at time zone current_setting('TimeZone') at time zone 'UTC'
end, 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')`;
function requestSummary(
column: typeof canvasImageTask.request | typeof canvasCapabilityTask.request,
) {
return sql<Record<string, unknown>>`jsonb_build_object(
'prompt', coalesce(${column}->'params'->>'prompt', ${column}->'input'->>'prompt', ${column}->>'displayPrompt', ${column}->>'prompt'),
'model', coalesce(${column}->'params'->>'model', ${column}->>'model'))`;
}
function requestParameters(request: Record<string, any>) {
const values = request.params || request.input || request;
const keys = [
"aspectRatio",
"imageSize",
"imageCount",
"sourceNodeIds",
"targetNodeIds",
"imageUrl",
"sourceImageUrl",
"referenceImage",
"referenceImages",
"region",
"selectionRect",
"width",
"height",
"operation",
"capabilityId",
];
const compact = (value: unknown): unknown => {
if (typeof value === "string")
return value.startsWith("data:") ? "内嵌图片,已省略二进制内容" : value.slice(0, 4000);
if (Array.isArray(value)) return value.slice(0, 20).map(compact);
return value;
};
return Object.fromEntries(
keys.filter((key) => values[key] !== undefined).map((key) => [key, compact(values[key])]),
);
}
export function feedbackConversationRequests(
messages: FeedbackMessage[],
verifiedIds: Set<string>,
) {
const requests: FeedbackRequest[] = [];
for (const item of messages) {
if (item.role === "user") {
requests.push({
key: `message:${item.id}`,
kind: "message",
id: item.id,
requestId: null,
createdAt: item.createdAt || "",
prompt: item.text,
status: "未获取任务状态",
model: null,
error: null,
evidence: verifiedIds.has(item.id) ? "server" : "browser",
});
} else {
const latest = item.userMessageId
? requests.find((request) => request.id === item.userMessageId)
: requests.at(-1);
if (!latest) continue;
if (item.requestId) latest.requestId = item.requestId;
if (item.status) latest.status = item.status;
if (item.error) latest.error = item.error;
}
}
return requests.filter((item) => item.prompt).reverse();
}
async function collectFeedbackReporter(userId: string): Promise<FeedbackContext["reporter"]> {
const [reporter] = await db
.select({ id: user.id, name: user.name, email: user.email, department: user.department })
.from(user)
.where(eq(user.id, userId))
.limit(1);
if (!reporter) throw new CanvasFeedbackError("登录状态已失效,请重新登录", 401);
return { ...reporter, name: reporter.name || reporter.email };
}
export async function collectPageFeedbackContext(
userId: string,
pagePath: string,
): Promise<FeedbackContext> {
return {
reporter: await collectFeedbackReporter(userId),
project: { id: null, title: pagePath },
requests: [],
};
}
export async function collectCanvasFeedbackContext(
userId: string,
projectId: string,
client: FeedbackClient,
selection?: string | null,
): Promise<FeedbackContext> {
const [project] = await db
.select({ id: canvasProject.id, title: canvasProject.title })
.from(canvasProject)
.where(and(eq(canvasProject.id, projectId), eq(canvasProject.userId, userId)))
.limit(1);
if (!project) throw new CanvasFeedbackError("画布不存在或无权反馈该画布", 404);
const reporter = await collectFeedbackReporter(userId);
const [kind, selectedId] = (selection || "").split(":", 2);
const validId = /^[0-9a-f-]{36}$/i.test(selectedId || "");
const selectedMessage =
kind === "message" && validId && client.chatId
? (
await db
.select({ createdAt: message.createdAt })
.from(message)
.innerJoin(chat, and(eq(chat.id, message.chatId), eq(chat.userId, userId)))
.where(and(eq(message.chatId, client.chatId), eq(message.id, selectedId)))
.limit(1)
)[0]
: null;
const [runs, capabilities, storedMessages] = await Promise.all([
db
.select({
id: canvasAgentRun.id,
createdAt: canvasAgentRun.createdAt,
status: canvasAgentRun.status,
objective: sql<string>`${canvasAgentRun.snapshot}->>'objective'`,
errors: sql<
string[]
>`jsonb_path_query_array(${canvasAgentRun.snapshot}, '$.calls[*].error')`,
})
.from(canvasAgentRun)
.where(
and(
eq(canvasAgentRun.userId, userId),
eq(canvasAgentRun.projectId, projectId),
...(client.chatId ? [eq(canvasAgentRun.chatId, client.chatId)] : []),
...(kind === "agent" && validId ? [eq(canvasAgentRun.id, selectedId)] : []),
),
)
.orderBy(desc(canvasAgentRun.createdAt))
.limit(10),
db
.select({
id: canvasCapabilityTask.id,
requestId: canvasCapabilityTask.requestId,
capabilityId: canvasCapabilityTask.capabilityId,
createdAt: capabilityCreatedAt,
startedAt: canvasCapabilityTask.startedAt,
completedAt: canvasCapabilityTask.completedAt,
status: canvasCapabilityTask.status,
error: canvasCapabilityTask.error,
request:
kind === "capability" && validId
? canvasCapabilityTask.request
: requestSummary(canvasCapabilityTask.request),
})
.from(canvasCapabilityTask)
.where(
and(
eq(canvasCapabilityTask.userId, userId),
eq(canvasCapabilityTask.projectId, projectId),
...(kind === "capability" && validId ? [eq(canvasCapabilityTask.id, selectedId)] : []),
),
)
.orderBy(desc(capabilityCreatedAt))
.limit(10),
client.chatId
? db
.select({
id: message.id,
role: message.role,
parts: message.parts,
createdAt: message.createdAt,
})
.from(message)
.innerJoin(chat, and(eq(chat.id, message.chatId), eq(chat.userId, userId)))
.where(
and(
eq(message.chatId, client.chatId),
...(selectedMessage ? [gte(message.createdAt, selectedMessage.createdAt)] : []),
),
)
.orderBy(selectedMessage ? asc(message.createdAt) : desc(message.createdAt))
.limit(12)
: Promise.resolve([]),
]);
const verifiedIds = new Set(storedMessages.map((item) => item.id));
const serverById = new Map(
feedbackMessages(selectedMessage ? storedMessages : [...storedMessages].reverse()).map(
(item) => [item.id, item],
),
);
const browserIds = new Set(client.messages.map((item) => item.id));
const combined = [...serverById.values()].filter((item) => !browserIds.has(item.id));
combined.push(...client.messages.map((item) => ({ ...item, ...serverById.get(item.id) })));
// Browser stream metadata can arrive before the final assistant row is persisted.
const conversation = feedbackConversationRequests(combined, verifiedIds);
const imageIds = Array.from(
new Set(
[
...conversation.map((item) => item.requestId),
...client.nodes.map((node) => node.taskId),
].filter((id): id is string => Boolean(id && /^[0-9a-f-]{36}$/i.test(id))),
),
);
if (kind === "image" && validId) imageIds.push(selectedId);
const images = imageIds.length
? await db
.select({
id: canvasImageTask.id,
createdAt: canvasImageTask.createdAt,
startedAt: canvasImageTask.startedAt,
completedAt: canvasImageTask.completedAt,
status: canvasImageTask.status,
error: canvasImageTask.error,
request:
kind === "image" && validId
? canvasImageTask.request
: requestSummary(canvasImageTask.request),
})
.from(canvasImageTask)
.where(
and(
eq(canvasImageTask.userId, userId),
inArray(canvasImageTask.id, imageIds),
...(kind === "image" && validId ? [eq(canvasImageTask.id, selectedId)] : []),
),
)
.limit(12)
: [];
const requests: FeedbackRequest[] = [
...conversation,
...runs.map(
(run): FeedbackRequest => ({
key: `agent:${run.id}`,
kind: "agent",
id: run.id,
requestId: run.id,
createdAt: run.createdAt.toISOString(),
prompt: run.objective || "Agent 任务",
status: run.status,
model: null,
error: (run.errors || []).join("\n") || null,
evidence: "server",
}),
),
...capabilities.map((task): FeedbackRequest => {
const request = task.request as Record<string, any>;
return {
key: `capability:${task.id}`,
kind: "capability",
id: task.id,
requestId: task.requestId,
createdAt: task.createdAt,
prompt:
text(request.params?.prompt) ||
text(request.input?.prompt) ||
text(request.prompt) ||
task.capabilityId,
status: task.status,
model: text(request.params?.model) || text(request.model) || null,
error: task.error,
durationMs: duration(task.startedAt, task.completedAt),
parameters: requestParameters(request),
evidence: "server",
};
}),
...images.map((task): FeedbackRequest => {
const request = task.request as Record<string, any>;
return {
key: `image:${task.id}`,
kind: "image",
id: task.id,
requestId: task.id,
createdAt: task.createdAt.toISOString(),
prompt: text(request.displayPrompt) || text(request.prompt),
status: task.status,
model: text(request.model) || null,
error: task.error,
durationMs: duration(task.startedAt, task.completedAt),
parameters: requestParameters(request),
evidence: "server",
};
}),
];
requests.sort((a, b) => (Date.parse(b.createdAt) || 0) - (Date.parse(a.createdAt) || 0));
if (selection && !requests.some((item) => item.key === selection))
throw new CanvasFeedbackError("关联请求已变化,请重新选择要反馈的请求", 409);
return { project, reporter, requests };
}

View File

@ -1,103 +0,0 @@
import { z } from "zod";
export const feedbackMessageSchema = z.object({
id: z.string().max(180),
role: z.enum(["user", "assistant"]),
text: z.string().max(16000),
createdAt: z.string().max(64).optional(),
requestId: z.string().max(180).optional(),
status: z.string().max(64).optional(),
error: z.string().max(2000).optional(),
userMessageId: z.string().max(180).optional(),
});
export const feedbackPagePathSchema = z.string().max(2048).regex(/^\/(?!\/)[^?#\\\s]*$/);
export const feedbackClientSchema = z.object({
pagePath: feedbackPagePathSchema.optional(),
chatId: z.string().uuid().nullable(),
messages: z.array(feedbackMessageSchema).max(12),
pageVersion: z.string().max(100),
capturedAt: z.string().datetime(),
viewport: z.object({
width: z.number().int().min(0).max(30000),
height: z.number().int().min(0).max(30000),
}),
online: z.boolean(),
errors: z.array(z.string().max(2000)).max(5),
nodes: z
.array(
z.object({
id: z.string().max(180),
type: z.string().max(64),
title: z.string().max(256),
status: z.string().max(64),
taskId: z.string().max(180),
width: z.number().optional(),
height: z.number().optional(),
}),
)
.max(20)
.default([]),
});
export const feedbackCommandSchema = z.discriminatedUnion("action", [
z.object({ action: z.literal("context"), client: feedbackClientSchema }),
z.object({
action: z.literal("submit"),
id: z.string().uuid(),
description: z.string().trim().max(2000),
selection: z.string().max(220).nullable(),
client: feedbackClientSchema,
}),
]);
export type FeedbackClient = z.infer<typeof feedbackClientSchema>;
export type FeedbackMessage = z.infer<typeof feedbackMessageSchema>;
export type FeedbackRequest = {
key: string;
kind: "message" | "agent" | "capability" | "image";
id: string;
requestId: string | null;
createdAt: string;
prompt: string;
status: string;
model: string | null;
error: string | null;
durationMs?: number | null;
parameters?: Record<string, unknown>;
evidence: "server" | "browser";
};
export type FeedbackContext = {
reporter: { id: string; name: string; email: string; department: string | null };
project: { id: string | null; title: string };
requests: FeedbackRequest[];
};
export type FeedbackSnapshot = Omit<FeedbackContext, "requests"> & {
request: FeedbackRequest | null;
client: FeedbackClient;
userAgent: string;
};
export type FeedbackReceipt = {
id: string;
status: string;
deliveryId: string | null;
message: string;
};
export function feedbackStatusMessage(status: string, errorCode?: string | null) {
if (["sent", "unread", "read"].includes(status)) return "反馈已投递给魔礼红。";
if (status === "failed")
return errorCode === "DELIVERY_OUTCOME_UNKNOWN"
? "反馈已保存,但钉钉投递结果暂无法确认。请保留反馈编号,避免重复提交。"
: "反馈已保存,但钉钉通知投递失败。请保留反馈编号并联系管理员。";
if (status === "pending") return "反馈已保存,正在提交通知;短暂故障会自动重试,无需再次反馈。";
return "反馈已保存,通知中心已受理,等待投递给魔礼红。";
}
export function formatFeedbackTime(value: string) {
if (!Number.isFinite(Date.parse(value))) return "未记录";
return new Intl.DateTimeFormat("zh-CN", {
timeZone: "Asia/Shanghai",
dateStyle: "short",
timeStyle: "medium",
hour12: false,
}).format(new Date(value));
}

View File

@ -1,89 +0,0 @@
import { after, NextResponse } from "next/server";
import { z, ZodError } from "zod";
import { auth } from "@/app/(auth)/auth";
import { CanvasFeedbackError, collectCanvasFeedbackContext, collectPageFeedbackContext } from "@/lib/canvas/feedback-context";
import { feedbackCommandSchema, feedbackPagePathSchema } from "@/lib/canvas/feedback-contract";
import {
deliverCanvasFeedback,
feedbackReceipt,
findCanvasFeedback,
refreshCanvasFeedback,
saveCanvasFeedback,
} from "@/lib/canvas/feedback-store";
const response = (data: unknown, status = 200) =>
NextResponse.json(data, { status, headers: { "Cache-Control": "no-store" } });
function failure(error: unknown) {
if (error instanceof CanvasFeedbackError) return response({ error: error.message }, error.status);
if (error instanceof ZodError || error instanceof SyntaxError)
return response({ error: "反馈资料格式不正确,请重新打开问题反馈后重试。" }, 400);
console.error("[Canvas feedback] request failed", {
errorType: error instanceof Error ? error.name : "unknown",
});
return response({ error: "反馈暂未保存成功,请保留问题说明,稍后重试。" }, 503);
}
export async function feedbackPost(request: Request, projectId: string | null) {
try {
const session = await auth();
if (!session?.user?.id || (projectId === null && session.user.type === "guest")) return response({ error: "登录已过期,请登录后发送反馈。" }, 401);
if (projectId !== null) z.string().uuid().parse(projectId);
const raw = await request.text();
if (new TextEncoder().encode(raw).byteLength > 262144)
return response({ error: "反馈资料过大,请减少补充说明后重试。" }, 413);
const command = feedbackCommandSchema.parse(JSON.parse(raw));
if (command.action === "submit") {
const existing = await findCanvasFeedback(command.id);
if (existing) {
if (existing.userId !== session.user.id || existing.projectId !== projectId)
throw new CanvasFeedbackError("反馈编号不可用", 409);
return response({ feedback: feedbackReceipt(await refreshCanvasFeedback(existing)) });
}
}
if (!projectId && command.action === "submit" && !command.description) {
throw new CanvasFeedbackError("请填写遇到的问题。");
}
const context = projectId
? await collectCanvasFeedbackContext(
session.user.id,
projectId,
command.client,
command.action === "submit" ? command.selection : null,
)
: await collectPageFeedbackContext(session.user.id, feedbackPagePathSchema.parse(command.client.pagePath));
if (command.action === "context") return response({ context });
const selected = context.requests.find((item) => item.key === command.selection) || null;
const row = await saveCanvasFeedback({
id: command.id,
userId: session.user.id,
description: command.description,
appUrl: process.env.NEXT_PUBLIC_APP_URL || new URL(request.url).origin,
snapshot: {
reporter: context.reporter,
project: context.project,
request: selected,
client: projectId ? command.client : { ...command.client, chatId: null, messages: [], nodes: [] },
userAgent: (request.headers.get("user-agent") || "").slice(0, 512),
},
});
after(() => deliverCanvasFeedback(row.id));
return response({ feedback: feedbackReceipt(row) }, 202);
} catch (error) {
return failure(error);
}
}
export async function feedbackGet(request: Request, projectId: string | null) {
try {
const session = await auth();
if (!session?.user?.id || (projectId === null && session.user.type === "guest")) return response({ error: "登录已过期,请登录后查看反馈状态。" }, 401);
if (projectId !== null) z.string().uuid().parse(projectId);
const id = z.string().uuid().parse(new URL(request.url).searchParams.get("feedbackId"));
const row = await findCanvasFeedback(id);
if (!row || row.userId !== session.user.id || row.projectId !== projectId)
throw new CanvasFeedbackError("反馈记录不存在", 404);
return response({ feedback: feedbackReceipt(await refreshCanvasFeedback(row)) });
} catch (error) {
return failure(error);
}
}

View File

@ -1,191 +0,0 @@
import "server-only";
import { and, asc, eq, isNull, lt, lte, sql } from "drizzle-orm";
import { db } from "@/lib/db";
import { canvasFeedback } from "@/lib/db/schema";
import {
notificationCenterConfigured,
notificationCenterRequest,
NotificationCenterError,
type NotificationPayload,
} from "@/lib/notifications/notification-center";
import { CanvasFeedbackError } from "./feedback-context";
import {
feedbackStatusMessage,
formatFeedbackTime,
type FeedbackReceipt,
type FeedbackSnapshot,
} from "./feedback-contract";
type FeedbackRow = typeof canvasFeedback.$inferSelect;
const backoffMs = [60_000, 300_000, 900_000, 1800_000, 1800_000];
const clip = (value: string, length: number) =>
value.length > length ? `${value.slice(0, length)}…(完整内容见反馈详情)` : value;
const quote = (value: string) =>
value
.replace(/\r\n?/g, "\n")
.split("\n")
.map((line) => `> ${line}`)
.join("\n");
export function buildFeedbackNotification(input: {
id: string;
description: string;
snapshot: FeedbackSnapshot;
createdAt: string;
appUrl: string;
}): NotificationPayload {
const { reporter, project, request } = input.snapshot;
const location = project.id ? "**画布:** " + project.title : "**页面:** " + project.title;
const content = [
`**反馈人:** ${reporter.name} · ${reporter.department || "未登记部门"}`,
`**反馈时间:** ${formatFeedbackTime(input.createdAt)}`,
location,
`**请求时间:** ${request?.createdAt ? formatFeedbackTime(request.createdAt) : "未关联请求"}`,
`**请求号:** ${request?.requestId || "尚未获取;可通过消息或任务编号排查"}`,
`**状态 / 模型:** ${request?.status || "页面问题"} / ${request?.model || "未记录"}`,
`**处理耗时:** ${request?.durationMs != null ? `${(request.durationMs / 1000).toFixed(1)} 秒` : "未记录或尚未结束"}`,
`**用户说明**\n${quote(clip(input.description || "用户未补充说明", 900))}`,
`**请求内容**\n${quote(clip(request?.prompt || "未关联具体请求", 1600))}`,
`**错误信息**\n${quote(clip(request?.error || input.snapshot.client.errors.join("\n") || "未记录明确错误", 600))}`,
`**反馈编号:** ${input.id}`,
`**页面版本:** ${input.snapshot.client.pageVersion}`,
].join("\n\n");
return {
idempotencyKey: `canvas-feedback:${input.id}:v1`,
recipientAlias: "魔礼红",
title: project.id ? "画布问题反馈" : "页面问题反馈",
content: clip(content, 4400),
actionTitle: "查看完整反馈",
actionUrl: `${input.appUrl.replace(/\/$/, "")}/feedback/${input.id}`,
};
}
export const feedbackReceipt = (row: FeedbackRow): FeedbackReceipt => {
const status = row.deliveryId && row.status === "pending" ? "accepted" : row.status;
return {
id: row.id,
deliveryId: row.deliveryId,
status,
message: feedbackStatusMessage(status, row.lastErrorCode),
};
};
export async function findCanvasFeedback(id: string) {
return (
(await db.select().from(canvasFeedback).where(eq(canvasFeedback.id, id)).limit(1))[0] || null
);
}
export async function saveCanvasFeedback(input: {
id: string;
userId: string;
description: string;
snapshot: FeedbackSnapshot;
appUrl: string;
}) {
const createdAt = new Date();
await db
.insert(canvasFeedback)
.values({
id: input.id,
userId: input.userId,
projectId: input.snapshot.project.id,
description: input.description,
snapshot: input.snapshot,
notification: buildFeedbackNotification({ ...input, createdAt: createdAt.toISOString() }),
createdAt,
nextAttemptAt: createdAt,
})
.onConflictDoNothing({ target: canvasFeedback.id });
const row = await findCanvasFeedback(input.id);
if (!row || row.userId !== input.userId || row.projectId !== input.snapshot.project.id)
throw new CanvasFeedbackError("反馈编号不可用", 409);
// The persisted notification is immutable so a lost response can be retried safely.
return row;
}
export async function deliverCanvasFeedback(id: string) {
const [claimed] = await db
.update(canvasFeedback)
.set({
attempts: sql`${canvasFeedback.attempts} + 1`,
nextAttemptAt: new Date(Date.now() + 60_000),
})
.where(
and(
eq(canvasFeedback.id, id),
eq(canvasFeedback.status, "pending"),
isNull(canvasFeedback.deliveryId),
lte(canvasFeedback.nextAttemptAt, new Date()),
lt(canvasFeedback.attempts, 5),
),
)
.returning();
if (!claimed) return;
try {
const delivery = await notificationCenterRequest(
"POST",
"/v1/notifications",
claimed.notification,
);
await db
.update(canvasFeedback)
.set({
deliveryId: delivery.id,
status: delivery.status,
lastErrorCode: delivery.lastErrorCode || null,
})
.where(eq(canvasFeedback.id, id));
} catch (error) {
const failure =
error instanceof NotificationCenterError
? error
: new NotificationCenterError("NOTIFICATION_CONNECTION_FAILED", true);
await db
.update(canvasFeedback)
.set({
status: failure.retryable && claimed.attempts < 5 ? "pending" : "failed",
lastErrorCode: failure.code,
nextAttemptAt: new Date(Date.now() + backoffMs[Math.min(claimed.attempts - 1, 4)]),
})
.where(and(eq(canvasFeedback.id, id), isNull(canvasFeedback.deliveryId)));
}
}
export async function refreshCanvasFeedback(row: FeedbackRow) {
if (!row.deliveryId) {
await deliverCanvasFeedback(row.id);
} else if (!["read", "failed"].includes(row.status)) {
try {
const delivery = await notificationCenterRequest(
"GET",
`/v1/notifications/${encodeURIComponent(row.deliveryId)}`,
);
await db
.update(canvasFeedback)
.set({ status: delivery.status, lastErrorCode: delivery.lastErrorCode || null })
.where(eq(canvasFeedback.id, row.id));
} catch {
/* A status lookup failure does not invalidate the accepted notification. */
}
}
return (await findCanvasFeedback(row.id))!;
}
export async function runCanvasFeedbackDelivery() {
if (!notificationCenterConfigured()) return;
const rows = await db
.select({ id: canvasFeedback.id })
.from(canvasFeedback)
.where(
and(
eq(canvasFeedback.status, "pending"),
isNull(canvasFeedback.deliveryId),
lte(canvasFeedback.nextAttemptAt, new Date()),
lt(canvasFeedback.attempts, 5),
),
)
.orderBy(asc(canvasFeedback.createdAt))
.limit(10);
for (const row of rows) await deliverCanvasFeedback(row.id);
}

View File

@ -5,7 +5,7 @@ import sharp from "sharp";
import { agentImageCircuitStore } from "@/lib/ai/runtime/agent-model-circuit-store";
import { getModelGenerationCreditCost, type DiscountableImageGenConfigLike } from "@/lib/ai/image-model-pricing";
import { agentImageFailurePolicy } from "@/lib/ai/runtime/agent-image-failover";
import { AgentImageGenerationError, agentImageGenerationError, ImageReferenceReadTimeoutError } from "@/lib/ai/runtime/image-generation-error";
import { agentImageGenerationError } from "@/lib/ai/runtime/image-generation-error";
import { getAiModelById, getEnabledAiModelsWithProvider } from "@/lib/db/queries";
import { fetchImageBufferFromUrl } from "@/lib/ai/rmbg/service";
import { createGeminiImage } from "@/lib/ai/providers/gemini-image";
@ -19,7 +19,6 @@ import { createBananaImage } from "@/lib/ai/providers/bananaimage";
import { createVisionary } from "@/lib/ai/providers/visionary";
import { createAtlasCloud } from "@/lib/ai/providers/atlascloud";
import { createVip98k } from "@/lib/ai/providers/vip98k";
import { VIP98K_IMAGE_BATCH_TIMEOUT_MS } from "@/lib/ai/vip98k-runtime-config";
import { createStepfunImage } from "@/lib/ai/providers/stepfun-image";
import { createMidjourney } from "@/lib/ai/providers/midjourney";
import { createRunway } from "@/lib/ai/providers/runway";
@ -60,7 +59,6 @@ export interface CanvasLocalEditCompositeConfig {
regions: CanvasLocalEditCompositeRegion[];
expandRegions?: boolean;
maskMode?: "region" | "diff";
maskShape?: "ellipse";
featherMinPx?: number;
featherMaxPx?: number;
featherRatio?: number;
@ -183,12 +181,8 @@ export interface CanvasGenerateImageRequest {
providerImageParams?: Record<string, unknown>;
referenceImage?: string;
referenceImages?: string[];
referenceImageBuffers?: ReadonlyMap<string, Buffer>;
billingFeature?: "multi-view" | "repaint";
requiredReferenceImageCount?: number;
padBatchInstructionToSquare?: boolean;
batchInstructionWithReference?: boolean;
batchInstructionFaceEdit?: boolean;
ownerKey?: string;
debugLabel?: string;
debugMeta?: Record<string, unknown>;
@ -343,7 +337,7 @@ function isCanvasScaleAdjustDebugRequest(
);
}
export function getCanvasProviderBatchTimeoutMs(params: {
function getCanvasProviderBatchTimeoutMs(params: {
debugLabel: string;
debugMeta?: Record<string, unknown>;
providerType: string;
@ -354,7 +348,7 @@ export function getCanvasProviderBatchTimeoutMs(params: {
: CANVAS_SCALE_ADJUST_PROVIDER_BATCH_TIMEOUT_MS;
}
return params.providerType === "vip98k" ? VIP98K_IMAGE_BATCH_TIMEOUT_MS : CANVAS_DEFAULT_PROVIDER_BATCH_TIMEOUT_MS;
return CANVAS_DEFAULT_PROVIDER_BATCH_TIMEOUT_MS;
}
function getCanvasProviderRuntimeParams(params: {
@ -547,7 +541,6 @@ function normalizeLocalEditCompositeConfig(
regions,
expandRegions,
maskMode,
maskShape: value.maskShape === "ellipse" ? value.maskShape : undefined,
diffHighThreshold: normalizeOptionalNumber(value.diffHighThreshold, {
max: 128,
min: 1,
@ -2898,7 +2891,6 @@ function buildLocalEditCompositeMaskSvg(params: {
height: number;
regions: CanvasLocalEditCompositeRegion[];
featherPx?: number;
maskShape?: "ellipse";
}) {
const { width, height, regions } = params;
const blur =
@ -2926,14 +2918,12 @@ function buildLocalEditCompositeMaskSvg(params: {
return { left, top, rectHeight, rectWidth };
})
.filter(({ rectHeight, rectWidth }) => rectWidth > 0 && rectHeight > 0);
const shape = (left: number, top: number, w: number, h: number, radius: number) =>
params.maskShape === "ellipse"
? `<ellipse cx="${left + w / 2}" cy="${top + h / 2}" rx="${w / 2}" ry="${h / 2}" fill="#ffffff" />`
: `<rect x="${left}" y="${top}" width="${w}" height="${h}" rx="${radius}" fill="#ffffff" />`;
const softRects = rectEntries
.map(
({ left, top, rectHeight, rectWidth }) =>
shape(left, top, rectWidth, rectHeight, Math.round(blur * 0.9))
`<rect x="${left}" y="${top}" width="${rectWidth}" height="${rectHeight}" rx="${Math.round(
blur * 0.9
)}" fill="#ffffff" />`
)
.join("");
const coreRects = rectEntries
@ -2949,7 +2939,9 @@ function buildLocalEditCompositeMaskSvg(params: {
const coreWidth = rectWidth - inset * 2;
const coreHeight = rectHeight - inset * 2;
if (coreWidth <= 0 || coreHeight <= 0) return "";
return shape(left + inset, top + inset, coreWidth, coreHeight, Math.round(blur * 0.45));
return `<rect x="${left + inset}" y="${top + inset}" width="${coreWidth}" height="${coreHeight}" rx="${Math.round(
blur * 0.45
)}" fill="#ffffff" />`;
})
.join("");
@ -3430,7 +3422,6 @@ async function applyLocalEditCompositeToImages(params: {
input: buildLocalEditCompositeMaskSvg({
featherPx,
height: sourceHeight,
maskShape: composite.maskShape,
regions: composite.regions,
width: sourceWidth,
}),
@ -3452,13 +3443,11 @@ async function applyLocalEditCompositeToImages(params: {
const [originalUpload, thumbnailUpload] = await Promise.all([
uploadFile(
`canvas-local-edit-composited-${suffix}.png`,
bufferToArrayBuffer(mergedBuffer),
{ preferQiniuForm: true }
bufferToArrayBuffer(mergedBuffer)
),
uploadFile(
`canvas-local-edit-composited-thumb-${suffix}.png`,
bufferToArrayBuffer(thumbnailBuffer),
{ preferQiniuForm: true }
bufferToArrayBuffer(thumbnailBuffer)
),
]);
mergedImages.push({
@ -4276,7 +4265,6 @@ function isCanvasScaleAdjustProviderMaskEditModel(item: CanvasModelWithProvider)
interface CanvasImageGenConfig {
maxImages?: number;
maxReferenceImages?: number;
supports1K?: boolean;
supports2K?: boolean;
supports4K?: boolean;
@ -4926,14 +4914,6 @@ export async function generateCanvasImage(
const allModels = await getEnabledAiModelsWithProvider();
const balanceBlockedProviderIds = new Set(request.internalBalanceBlockedProviderIds || []);
let imageModels = allModels.filter((m) => m.model.supportsImageGen && !isImageModelBalanceBlocked(m.model) && !balanceBlockedProviderIds.has(m.provider.id));
if (request.batchInstructionFaceEdit) {
imageModels = imageModels.filter(({ model, provider }) =>
SUPPORTED_CANVAS_PROVIDER_TYPES.has(provider.type) &&
!model.excludeFromFailover &&
!modelLacksReferenceImageInputSupport(model, provider) &&
Number((model.imageGenConfig as CanvasImageGenConfig | null)?.maxReferenceImages ?? 0) >= 2
);
}
const requestedModel = allModels.find(entry => entry.model.id === model || entry.model.modelId === model)?.model ||
(/^[0-9a-f-]{36}$/i.test(String(model || "")) ? await getAiModelById({ id: model! }) : undefined);
let effectiveModel = model;
@ -4954,19 +4934,12 @@ export async function generateCanvasImage(
}
if (!imageModels || imageModels.length === 0) {
if (request.batchInstructionFaceEdit) {
throw new AgentImageGenerationError("当前没有支持两张参考图的生图线路,换脸任务尚未提交。", true);
}
throw new Error("暂无可用的图片生成模型");
}
const verifiedFaceModel = request.batchInstructionFaceEdit
? imageModels.find(({ model, provider }) =>
model.modelId === "nano-banana-2.1" && provider.type === "vip98k")
: undefined;
let selectedModel = effectiveModel
? imageModels.find((m) => m.model.modelId === effectiveModel || m.model.id === effectiveModel)
: verifiedFaceModel || imageModels.find((m) => m.model.isDefault) || imageModels[0];
: imageModels.find((m) => m.model.isDefault) || imageModels[0];
if (!selectedModel) {
if (model && (model !== "auto" || request.agentImageMode === "pinned") && request.allowFailover === false) throw new Error("指定的图片模型不可用,未切换到其他模型");
@ -4974,7 +4947,7 @@ export async function generateCanvasImage(
if (!selectedModel) throw new Error("当前没有可用的备用图片模型,请稍后重试");
}
if (request.agentImageMode === "pinned" && !isCanvasImageSizeSupported(selectedModel.model.imageGenConfig as CanvasImageGenConfig | null, normalizeCanvasImageSizeValue(request.imageSize)))
throw new AgentImageGenerationError("指定的图片模型不支持所选清晰度,任务尚未提交。请调整清晰度后重试。", true);
throw new Error("指定的图片模型不支持所选尺寸,请调整图片尺寸");
const normalizedReferenceImages = Array.from(
new Set(
@ -5148,11 +5121,8 @@ export async function generateCanvasImage(
});
}
const referenceImageBuffers = provider.type === "alibaba-image" ? request.referenceImageBuffers : undefined;
const referencesToCheck = normalizedReferenceImages.filter(url => !referenceImageBuffers?.has(url));
const { kept, dropped: unreachableReferenceImages } =
await filterReachableRemoteUrls(referencesToCheck, { timeoutMs: 3000 });
const reachableReferenceImages = normalizedReferenceImages.filter(url => referenceImageBuffers?.has(url) || kept.includes(url));
const { kept: reachableReferenceImages, dropped: unreachableReferenceImages } =
await filterReachableRemoteUrls(normalizedReferenceImages, { timeoutMs: 3000 });
const effectiveReferenceImages = selectCanvasReferenceImages({
requested: normalizedReferenceImages, reachable: reachableReferenceImages,
requiredCount: request.requiredReferenceImageCount,
@ -5263,7 +5233,6 @@ export async function generateCanvasImage(
executeGenerateBatch = async (batchImageCount) => {
const result = await modelInstance.doGenerate({
messages,
referenceImageBuffers,
imageParams: {
...imageParams,
imageCount: batchImageCount,
@ -5693,11 +5662,6 @@ export async function generateCanvasImage(
await slot.release();
}
if (caughtError instanceof ImageReferenceReadTimeoutError) {
if (!collectedImages.length) throw caughtError;
return { images: collectedImages, model: selectedModel.model.displayName, modelRef: selectedModel.model.id, prompt: resultPrompt };
}
// An accepted or uncertain request must not start another paid model/variant.
if (caughtError && (
caughtError instanceof ProviderTaskUncertainError ||

View File

@ -403,25 +403,10 @@ async function runSingleTask(entry: PendingTaskEntry) {
: (async () => {
const imageRequest = request as CanvasGenerateImageRequest;
const ownerKey = String(task.userId || entry.userId);
const prepared = imageRequest.batchInstructionWithReference
? await (await import("./batch-instruction-reference")).prepareBatchInstructionWithReference(imageRequest)
: imageRequest;
const initialResult = await generateCanvasImage({
...prepared,
...imageRequest,
ownerKey,
});
if (imageRequest.padBatchInstructionToSquare) {
const { squareBatchInstructionOutput } = await import("./batch-instruction-output");
try {
return {
...initialResult,
images: await Promise.all(initialResult.images.map(squareBatchInstructionOutput)),
};
} catch (error) {
console.warn("[BatchInstruction] Square output failed", { taskId: task.id, error });
throw new Error("图片已生成,但补边结果尚未确认。请核对原任务,避免重复生成。");
}
}
return await applyScaleAdjustModelRelight({
generateImage: generateCanvasImage,
initialResult,

View File

@ -1,9 +1,4 @@
import {
AOS_ENHANCE_RESULT_TIMEOUT_MS,
EnhanceTerminalError,
pollTaskResult,
readCompletedEnhanceImage,
} from "@/lib/canvas/enhance-task-poll";
import { pollTaskResult, EnhanceTerminalError } from "@/lib/canvas/enhance-task-poll";
import "server-only";
import { NextRequest, NextResponse } from "next/server";
import {
@ -16,7 +11,6 @@ import {
import { uploadFile } from "@/lib/storage/service";
import { db } from "@/lib/db";
import { generatedImage } from "@/lib/db/schema";
import { archiveAosResult, readAosQiniuInput } from "@/lib/ai/aos-image-transfer";
import { executeConfiguredVip98kTask } from "@/lib/ai/vip98k-runtime";
import { createFalClient } from "@fal-ai/client";
@ -113,14 +107,13 @@ async function createThumbnail(buffer: Buffer, mimeType: string): Promise<Buffer
const sharp = await import("sharp");
// 创建缩略图(宽度 500px,高度按比例,质量 80%)
const resized = sharp.default(buffer)
const thumbnailBuffer = await sharp.default(buffer)
.resize(500, null, {
fit: "inside",
withoutEnlargement: true,
});
const thumbnailBuffer = await (
mimeType === "image/jpeg" ? resized.jpeg({ quality: 80 }) : resized
).toBuffer();
})
.jpeg({ quality: 80 }) // JPEG格式
.toBuffer();
console.log(`[Enhance] Thumbnail created, size: ${thumbnailBuffer.length} bytes (original: ${buffer.length} bytes)`);
return thumbnailBuffer;
@ -132,11 +125,12 @@ async function createThumbnail(buffer: Buffer, mimeType: string): Promise<Buffer
}
/**
* 上传图片,返回缩略图和原图的 URL。
* 将 base64 图片上传到 S3,返回缩略图和原图的 URL
*/
async function uploadEnhanceImage(image: string | Buffer, mimeType: string = "image/jpeg"): Promise<{ thumbnail: string; original: string }> {
async function uploadBase64ToS3(base64Data: string, mimeType: string = "image/jpeg"): Promise<{ thumbnail: string; original: string }> {
try {
const buffer = typeof image === "string" ? Buffer.from(image, "base64") : image;
// 解析 base64 数据
const buffer = Buffer.from(base64Data, "base64");
// 生成文件名
const ext = mimeType.split("/")[1] || "jpg";
@ -409,7 +403,7 @@ async function processWithVip98kEnhance(params: {
}
const outputBuffer = Buffer.from(await response.arrayBuffer());
const outputBase64 = outputBuffer.toString("base64");
const uploaded = await uploadEnhanceImage(outputBase64, "image/png");
const uploaded = await uploadBase64ToS3(outputBase64, "image/png");
const [savedImage] = await db
.insert(generatedImage)
.values({
@ -494,21 +488,9 @@ function buildEnhanceTaskPendingResponse(taskId: string) {
);
}
function buildEnhanceTaskFailureResponse(error: EnhanceTerminalError) {
const message =
getAosFriendlyErrorMessage(error.message) ||
"图片变清晰失败,处理服务已确认任务失败。";
return NextResponse.json(
{ success: false, status: "failed", terminal: true, error: message, detail: message },
{ status: 422 },
);
}
async function finalizeEnhanceTaskResult(params: {
userId: string;
taskResult: any;
taskId?: string;
includeBase64?: boolean;
prompt?: string;
chatId?: string | null;
messageId?: string | null;
@ -519,17 +501,28 @@ async function finalizeEnhanceTaskResult(params: {
throw new Error(params.taskResult?.message || "未知错误");
}
const data = params.taskResult.data;
const includeBase64 = params.includeBase64 !== false;
let uploadResult = !includeBase64 && params.taskId
? await archiveAosResult(data.image, `aos-enhance-${params.taskId}`)
: null;
let imageBase64: string | undefined;
if (!uploadResult) {
const completedImage = await readCompletedEnhanceImage(data);
uploadResult = await uploadEnhanceImage(completedImage.buffer, completedImage.mimeType);
if (includeBase64) imageBase64 = completedImage.buffer.toString("base64");
let enhancedImageBase64: string | null = null;
if (typeof params.taskResult.data.image_base64 === "string" && params.taskResult.data.image_base64) {
enhancedImageBase64 = params.taskResult.data.image_base64;
} else if (typeof params.taskResult.data.image === "string" && params.taskResult.data.image) {
const imageData = params.taskResult.data.image;
if (/^https?:\/\//i.test(imageData)) {
const imageResponse = await fetch(imageData);
if (!imageResponse.ok) {
throw new Error(`无法下载处理后的图片: ${imageResponse.status}`);
}
const downloadedBuffer = await imageResponse.arrayBuffer();
enhancedImageBase64 = Buffer.from(downloadedBuffer).toString("base64");
} else {
enhancedImageBase64 = imageData;
}
}
if (!enhancedImageBase64) {
throw new Error("轮询结果未包含图片数据");
}
const uploadResult = await uploadBase64ToS3(enhancedImageBase64, "image/jpeg");
const finalPrompt = params.prompt?.trim() || "图片变清晰处理";
let saved = false;
@ -619,7 +612,7 @@ async function finalizeEnhanceTaskResult(params: {
return {
success: true,
image_base64: imageBase64,
image_base64: enhancedImageBase64,
thumbnail_url: uploadResult.thumbnail,
original_url: uploadResult.original,
processing_time: processingTime,
@ -717,18 +710,15 @@ export async function executeEnhanceGET(request: Request, trustedUserId: string)
config.apiKey,
taskId,
ENHANCE_TASK_POLL_INTERVAL_MS,
AOS_ENHANCE_RESULT_TIMEOUT_MS
ENHANCE_SYNC_WAIT_TIMEOUT_MS
);
} catch (pollError) {
if (isEnhanceTaskStillProcessing(pollError)) {
return buildEnhanceTaskPendingResponse(taskId);
}
if (pollError instanceof EnhanceTerminalError) {
return buildEnhanceTaskFailureResponse(pollError);
}
const detail = pollError instanceof Error ? pollError.message : "轮询失败";
return NextResponse.json(
{ error: "变清晰处理失败", detail },
{ error: "变清晰处理失败", detail, terminal: pollError instanceof EnhanceTerminalError },
{ status: 500 }
);
}
@ -736,8 +726,6 @@ export async function executeEnhanceGET(request: Request, trustedUserId: string)
const responsePayload = await finalizeEnhanceTaskResult({
userId: session.user.id,
taskResult,
taskId,
includeBase64: searchParams.get("includeBase64") !== "false",
prompt,
chatId,
messageId,
@ -819,9 +807,7 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
const contentType = request.headers.get("content-type") || "";
console.log("[Enhance API] Content-Type:", contentType);
let imageBase64 = "";
let includeBase64 = true;
let directImageUrl: string | undefined;
let imageBase64: string;
let prompt = ""; // 用户提示词(可选)
let chatId: string | null = null; // 聊天ID(可选)
let messageId: string | null = null; // 消息ID(可选)
@ -831,7 +817,6 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
if (contentType.includes("application/json")) {
console.log("[Enhance API] 解析JSON请求体...");
const body = await request.json();
includeBase64 = body.includeBase64 !== false;
prompt = body.prompt || "图片变清晰处理";
chatId = body.chatId || null;
messageId = body.messageId || null;
@ -844,29 +829,26 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
// 优先使用 imageUrl,其次使用 image_base64
if (body.imageUrl) {
sourceImageUrl = String(body.imageUrl);
if (!includeBase64 && !useVip98k && !useBackupApi) {
const input = await readAosQiniuInput(sourceImageUrl);
if (input && input.width <= AOS_MAX_RESOLUTION && input.height <= AOS_MAX_RESOLUTION) {
directImageUrl = input.url;
}
}
if (!directImageUrl) {
try {
const imageResponse = await fetch(sourceImageUrl);
if (!imageResponse.ok) {
return NextResponse.json(
{ error: `无法获取图片: ${imageResponse.status} ${imageResponse.statusText}` },
{ status: 400 }
);
}
imageBase64 = Buffer.from(await imageResponse.arrayBuffer()).toString("base64");
} catch (fetchError) {
console.error("[Enhance API] 获取图片失败:", fetchError);
// 从 URL 获取图片并转换为 Base64
console.log(`[Enhance API] 从 URL 获取图片: ${body.imageUrl}`);
try {
const imageResponse = await fetch(body.imageUrl);
if (!imageResponse.ok) {
return NextResponse.json(
{ error: "无法获取图片,请检查图片链接是否有效" },
{ error: `无法获取图片: ${imageResponse.status} ${imageResponse.statusText}` },
{ status: 400 }
);
}
const arrayBuffer = await imageResponse.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
imageBase64 = buffer.toString("base64");
console.log(`[Enhance API] 图片已转换为 Base64,大小: ${buffer.length} bytes`);
} catch (fetchError) {
console.error("[Enhance API] 获取图片失败:", fetchError);
return NextResponse.json(
{ error: "无法获取图片,请检查图片链接是否有效" },
{ status: 400 }
);
}
} else if (body.image_base64) {
console.log("[Enhance API] JSON请求体解析完成,image_base64长度:", body.image_base64?.length || 0);
@ -935,7 +917,7 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
try {
// 步骤1: 先将Base64图片上传到S3获取公开可访问的URL
console.log(`[Enhance-RealESRGAN] 📤 步骤1: 上传原图到S3获取URL...`);
const tempUploadResult = await uploadEnhanceImage(imageBase64, "image/jpeg");
const tempUploadResult = await uploadBase64ToS3(imageBase64, "image/jpeg");
const sourceImageUrl = tempUploadResult.original;
console.log(`[Enhance-RealESRGAN] ✅ 原图已上传,URL: ${sourceImageUrl}`);
@ -955,7 +937,7 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
let originalUrl: string;
try {
const uploadResult = await uploadEnhanceImage(result.imageBase64, "image/png");
const uploadResult = await uploadBase64ToS3(result.imageBase64, "image/png");
thumbnailUrl = uploadResult.thumbnail;
originalUrl = uploadResult.original;
console.log(`[Enhance-RealESRGAN] ✅ 处理后图片已上传: ${originalUrl}`);
@ -1100,20 +1082,21 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
const enhanceUrl = `${apiUrl}/api/tasks/visual/scale`;
console.log("[Enhance API] API URL:", enhanceUrl);
// 将Base64转换为Buffer
console.log("[Enhance API] 转换Base64为Buffer,长度:", imageBase64.length);
const rawImageBuffer = Buffer.from(imageBase64, "base64");
const imageBuffer = await ensureAosResolutionLimit(rawImageBuffer, "Enhance API");
console.log("[Enhance API] Buffer创建完成,大小:", imageBuffer.length, "bytes");
// 创建FormData(使用form-data包,Node.js环境)
console.log("[Enhance API] 创建FormData...");
const FormDataLib = await import("form-data");
const formData = new FormDataLib.default();
console.log("[Enhance API] FormData创建完成");
if (directImageUrl) {
formData.append("image_url", directImageUrl);
} else {
const imageBuffer = await ensureAosResolutionLimit(Buffer.from(imageBase64, "base64"), "Enhance API");
formData.append("image_file", imageBuffer, {
filename: "image.jpg",
contentType: "image/jpeg",
});
}
formData.append("image_file", imageBuffer, {
filename: "image.jpg",
contentType: "image/jpeg",
});
// 使用异步模式(sync=0),根据官方示例
formData.append("sync", "0");
formData.append("type", config.type || "clean"); // 默认通用清晰化
@ -1127,7 +1110,7 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
if (scaleFactorValue) {
formData.append("scale_factor", String(scaleFactorValue));
}
formData.append("return_type", "1"); // 状态查询返回图片链接。
formData.append("return_type", "2"); // 返回base64
const headers: Record<string, string> = {
"X-API-KEY": config.apiKey!,
@ -1197,7 +1180,7 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
} catch (fetchError) {
clearTimeout(timeoutId);
lastFetchError = fetchError;
const canRetry = !directImageUrl && attempt <= ENHANCE_CREATE_RETRY_COUNT && isRetryableNetworkError(fetchError);
const canRetry = attempt <= ENHANCE_CREATE_RETRY_COUNT && isRetryableNetworkError(fetchError);
if (canRetry) {
console.warn(
`[Enhance API] 创建任务失败,准备重试: attempt=${attempt}, error=${fetchError instanceof Error ? fetchError.message : String(fetchError)}`
@ -1215,10 +1198,6 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
if (!createResponse.ok) {
const errorText = await createResponse.text();
if (directImageUrl && createResponse.status >= 400 && createResponse.status < 500 && createResponse.status !== 408) {
const friendlyMessage = getAosFriendlyErrorMessage(errorText);
return NextResponse.json({ error: friendlyMessage || "变清晰任务提交失败" }, { status: createResponse.status });
}
throw new Error(`创建任务失败: ${errorText || createResponse.statusText}`);
}
@ -1238,13 +1217,6 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
processingTime,
});
if (directImageUrl) {
return NextResponse.json(
{ error: "变清晰任务提交结果尚未确认,请核对原任务,避免重复提交。" },
{ status: 504 }
);
}
try {
const fallbackPayload = await executeFalEsrganFallback({
userId,
@ -1333,10 +1305,22 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
if (isEnhanceTaskStillProcessing(pollError)) {
return buildEnhanceTaskPendingResponse(taskId);
}
if (pollError instanceof EnhanceTerminalError) {
return buildEnhanceTaskFailureResponse(pollError);
}
const detail = pollError instanceof Error ? pollError.message : "轮询失败";
try {
const fallbackPayload = await executeFalEsrganFallback({
userId,
imageBase64,
prompt,
chatId,
messageId,
sourceImageUrl,
requestedScaleFactor,
reason: detail,
});
return NextResponse.json(fallbackPayload);
} catch (falError) {
console.error("[Enhance-FAL-ESRGAN] 专用高清备用链路失败:", falError);
}
const friendlyMessage = getAosFriendlyErrorMessage(detail);
return NextResponse.json(
{ error: "变清晰处理失败", detail: friendlyMessage || detail },
@ -1347,8 +1331,6 @@ export async function executeEnhancePOST(request: Request, trustedUserId: string
const responsePayload = await finalizeEnhanceTaskResult({
userId,
taskResult,
taskId,
includeBase64,
prompt,
chatId,
messageId,

View File

@ -22,7 +22,7 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI
const contentType = request.headers.get("content-type") || "";
let imageBuffer: Buffer | undefined;
let imageBuffer: Buffer;
let imageBase64: string | undefined;
let prompt = "抠图处理";
let chatId: string | null = null;
@ -30,11 +30,9 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI
let skipGallerySave = false;
let sourceImageUrl: string | null = null;
let inputImageUrl: string | null = null;
let includeBase64 = true;
if (contentType.includes("application/json")) {
const body = await request.json();
includeBase64 = body.includeBase64 !== false;
prompt = body.prompt || "抠图处理";
chatId = body.chatId || null;
messageId = body.messageId || null;
@ -46,7 +44,7 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI
if (imageUrl) {
sourceImageUrl = String(imageUrl);
inputImageUrl = sourceImageUrl;
if (includeBase64) imageBuffer = await fetchImageBufferFromUrl(sourceImageUrl);
imageBuffer = await fetchImageBufferFromUrl(sourceImageUrl);
} else if (body.image_base64) {
imageBase64 = String(body.image_base64);
imageBuffer = Buffer.from(imageBase64, "base64");
@ -102,13 +100,13 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI
const result = await executeRmbgJob({
requestId,
userId: session.user.id,
...(imageBuffer ? { imageBuffer, inputImageUrl } : { inputImageUrl: inputImageUrl! }),
imageBuffer,
imageBase64,
includeBase64,
prompt,
chatId,
messageId,
skipGallerySave,
inputImageUrl,
sourceImageUrl,
});

View File

@ -1,212 +0,0 @@
export type DownloadFileItem = {
url: string;
name?: string;
};
export type CanvasDownloadAssetResult =
| {
ok: true;
filename: string;
bytes: Buffer;
contentType: string;
}
| {
ok: false;
url: string;
name?: string;
error: string;
};
export function sanitizeCanvasDownloadFileName(value: string, fallback: string): string {
const cleaned = String(value || "")
.replace(/\.[a-zA-Z0-9]{2,6}$/g, "")
.replace(/[\\/:*?"<>|]+/g, "-")
.replace(/\s+/g, "-")
.replace(/-+/g, "-")
.replace(/^-+|-+$/g, "")
.trim()
.slice(0, 120);
return cleaned || fallback;
}
export function buildAttachmentContentDisposition(filename: string): string {
const safeAsciiName =
filename
.replace(/[^\x20-\x7E]+/g, "")
.replace(/"/g, "")
.trim() || `canvas-image-${Date.now()}.png`;
return `attachment; filename="${safeAsciiName}"; filename*=UTF-8''${encodeURIComponent(filename)}`;
}
function getContentTypeMime(contentType: string): string {
return String(contentType || "")
.split(";")[0]
?.trim()
.toLowerCase();
}
function hasAsciiAt(buffer: Buffer, offset: number, value: string): boolean {
return buffer.subarray(offset, offset + value.length).toString("latin1") === value;
}
function looksLikeSvg(buffer: Buffer, contentType: string): boolean {
if (getContentTypeMime(contentType) === "image/svg+xml") return true;
const text = buffer.subarray(0, Math.min(buffer.length, 1024)).toString("utf8").trimStart();
return /^(?:<\?xml[^>]*>\s*)?<svg[\s>]/i.test(text);
}
function looksLikeErrorDocument(buffer: Buffer, contentType: string): boolean {
const mime = getContentTypeMime(contentType);
if (
mime.startsWith("text/") ||
mime === "application/json" ||
mime === "application/xml" ||
mime === "application/xhtml+xml"
) {
return true;
}
const text = buffer.subarray(0, Math.min(buffer.length, 512)).toString("utf8").trimStart();
return /^(?:<!doctype\s+html|<html[\s>]|<\?xml|<error[\s>]|[{[])/i.test(text);
}
function detectAsset(bytes: Buffer, contentType: string): { kind: "image" | "video" | "audio"; mimeType: string; ext: string } | null {
if (bytes.length < 4) return null;
if (bytes.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) {
return { kind: "image", mimeType: "image/png", ext: "png" };
}
if (bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) {
return { kind: "image", mimeType: "image/jpeg", ext: "jpg" };
}
if (hasAsciiAt(bytes, 0, "RIFF") && hasAsciiAt(bytes, 8, "WEBP")) {
return { kind: "image", mimeType: "image/webp", ext: "webp" };
}
if (hasAsciiAt(bytes, 0, "GIF87a") || hasAsciiAt(bytes, 0, "GIF89a")) {
return { kind: "image", mimeType: "image/gif", ext: "gif" };
}
if (hasAsciiAt(bytes, 0, "BM")) {
return { kind: "image", mimeType: "image/bmp", ext: "bmp" };
}
if (
bytes.subarray(0, 4).equals(Buffer.from([0x49, 0x49, 0x2a, 0x00])) ||
bytes.subarray(0, 4).equals(Buffer.from([0x4d, 0x4d, 0x00, 0x2a]))
) {
return { kind: "image", mimeType: "image/tiff", ext: "tiff" };
}
if (looksLikeSvg(bytes, contentType)) {
return { kind: "image", mimeType: "image/svg+xml", ext: "svg" };
}
if (hasAsciiAt(bytes, 0, "RIFF") && hasAsciiAt(bytes, 8, "WAVE")) return {kind:"audio",mimeType:"audio/wav",ext:"wav"};
if (hasAsciiAt(bytes, 0, "ID3") || (bytes[0] === 0xff && (bytes[1] & 0xe0) === 0xe0 && (bytes[1] & 0x06) !== 0)) return {kind:"audio",mimeType:"audio/mpeg",ext:"mp3"};
if (hasAsciiAt(bytes, 0, "OggS")) return {kind:"audio",mimeType:"audio/ogg",ext:"ogg"};
if (hasAsciiAt(bytes, 0, "fLaC")) return {kind:"audio",mimeType:"audio/flac",ext:"flac"};
if (getContentTypeMime(contentType).startsWith("audio/")) return {kind:"audio",mimeType:getContentTypeMime(contentType),ext:getContentTypeMime(contentType).includes("mp4") ? "m4a" : getContentTypeMime(contentType).split("/")[1]};
if (hasAsciiAt(bytes, 4, "ftyp")) {
const brand = bytes.subarray(8, Math.min(bytes.length, 32)).toString("latin1").toLowerCase();
if (brand.includes("m4a")) return {kind:"audio",mimeType:"audio/mp4",ext:"m4a"};
if (brand.includes("avif")) return { kind: "image", mimeType: "image/avif", ext: "avif" };
if (brand.includes("heic") || brand.includes("heif") || brand.includes("mif1")) {
return { kind: "image", mimeType: "image/heic", ext: "heic" };
}
return { kind: "video", mimeType: "video/mp4", ext: "mp4" };
}
if (bytes.subarray(0, 4).equals(Buffer.from([0x1a, 0x45, 0xdf, 0xa3]))) {
return { kind: "video", mimeType: "video/webm", ext: "webm" };
}
const mime = getContentTypeMime(contentType);
if (mime.startsWith("image/")) {
const ext = mime.includes("jpeg") ? "jpg" : mime.split("/")[1] || "png";
return { kind: "image", mimeType: mime, ext };
}
if (mime.startsWith("video/")) {
const ext = mime.includes("quicktime") ? "mov" : mime.split("/")[1] || "mp4";
return { kind: "video", mimeType: mime, ext };
}
return null;
}
export async function convertImageToPhotoshopPng(bytes: Buffer): Promise<Buffer> {
const sharp = await import("sharp");
const image = sharp.default(bytes, {
animated: false,
failOn: "none",
limitInputPixels: false,
});
const metadata = await image.metadata();
if (!metadata.width || !metadata.height) {
throw new Error("无法解析图片尺寸");
}
// RGB/RGBA, 8-bit, single-frame PNG is already compatible; preserve its exact bytes.
const rgb = (metadata.space === "srgb" && metadata.depth === "uchar")
|| (metadata.space === "rgb16" && metadata.depth === "ushort");
if (metadata.format === "png" && rgb
&& !metadata.isPalette && (metadata.pages || 1) === 1 && (!metadata.orientation || metadata.orientation === 1)) return bytes;
const output = image.rotate().toColorspace("srgb");
if (metadata.icc) output.withIccProfile("srgb");
return output.png({ compressionLevel: 3, adaptiveFiltering: true }).toBuffer();
}
function buildCanvasAssetFilename(params: {
item: DownloadFileItem;
index: number;
ext: string;
includeSequencePrefix: boolean;
}) {
const baseName = sanitizeCanvasDownloadFileName(
params.item.name || `canvas-image-${params.index + 1}`,
`canvas-image-${params.index + 1}`
);
if (!params.includeSequencePrefix) {
return `${baseName}.${params.ext}`;
}
const sequencePrefix = String(params.index + 1).padStart(3, "0");
return `${sequencePrefix}-${baseName}.${params.ext}`;
}
export async function normalizeCanvasDownloadAsset(params: {
item: DownloadFileItem;
index: number;
bytes: Buffer;
contentType: string;
includeSequencePrefix: boolean;
}, convert = convertImageToPhotoshopPng): Promise<CanvasDownloadAssetResult> {
if (params.bytes.length === 0) {
throw new Error("empty response");
}
if (looksLikeErrorDocument(params.bytes, params.contentType) && !looksLikeSvg(params.bytes, params.contentType)) {
throw new Error("源地址返回的不是图片文件");
}
const detected = detectAsset(params.bytes, params.contentType);
if (!detected) {
throw new Error("无法识别图片格式");
}
if (detected.kind !== "image") {
return {
ok: true,
filename: buildCanvasAssetFilename({
item: params.item,
index: params.index,
ext: detected.ext,
includeSequencePrefix: params.includeSequencePrefix,
}),
bytes: params.bytes,
contentType: detected.mimeType,
};
}
const pngBytes = await convert(params.bytes);
return {
ok: true,
filename: buildCanvasAssetFilename({
item: params.item,
index: params.index,
ext: "png",
includeSequencePrefix: params.includeSequencePrefix,
}),
bytes: pngBytes,
contentType: "image/png",
};
}

View File

@ -4,15 +4,24 @@ import { fetchImageBytes, onceImageBytes } from "@/lib/storage/image-byte-cache"
import { extractImageProxySourceUrl, inferOriginalImageUrl } from "@/lib/image-url";
import { getDownloadFailure } from "./download-error";
import {
convertImageToPhotoshopPng,
normalizeCanvasDownloadAsset as normalizeAsset,
type CanvasDownloadAssetResult,
type DownloadFileItem,
} from "./photoshop-download-normalize";
type DownloadFileItem = {
url: string;
name?: string;
};
export { buildAttachmentContentDisposition, convertImageToPhotoshopPng, sanitizeCanvasDownloadFileName } from "./photoshop-download-normalize";
export type { CanvasDownloadAssetResult } from "./photoshop-download-normalize";
export type CanvasDownloadAssetResult =
| {
ok: true;
filename: string;
bytes: Buffer;
contentType: string;
}
| {
ok: false;
url: string;
name?: string;
error: string;
};
const DEFAULT_FETCH_TIMEOUT_MS = 30_000;
const DEFAULT_FETCH_RETRY_COUNT = 3;
@ -28,6 +37,27 @@ function normalizeDownloadError(error: unknown): string {
return "download failed";
}
export function sanitizeCanvasDownloadFileName(value: string, fallback: string): string {
const cleaned = String(value || "")
.replace(/\.[a-zA-Z0-9]{2,6}$/g, "")
.replace(/[\\/:*?"<>|]+/g, "-")
.replace(/\s+/g, "-")
.replace(/-+/g, "-")
.replace(/^-+|-+$/g, "")
.trim()
.slice(0, 120);
return cleaned || fallback;
}
export function buildAttachmentContentDisposition(filename: string): string {
const safeAsciiName =
filename
.replace(/[^\x20-\x7E]+/g, "")
.replace(/"/g, "")
.trim() || `canvas-image-${Date.now()}.png`;
return `attachment; filename="${safeAsciiName}"; filename*=UTF-8''${encodeURIComponent(filename)}`;
}
function buildRemoteFetchHeaders(fileUrl: string, requestOrigin: string, cookie: string): HeadersInit {
const headers: Record<string, string> = {
Accept: "image/avif,image/webp,image/apng,image/svg+xml,image/*,video/*,*/*;q=0.8",
@ -61,7 +91,7 @@ function resolveHttpUrl(value: string, requestOrigin: string): string {
}
}
export function buildCanvasDownloadSourceCandidates(fileUrl: string, requestOrigin: string) {
function buildCanvasDownloadSourceCandidates(fileUrl: string, requestOrigin: string) {
const candidates: string[] = [];
const seen = new Set<string>();
@ -87,14 +117,176 @@ export function buildCanvasDownloadSourceCandidates(fileUrl: string, requestOrig
return candidates;
}
export async function normalizeCanvasDownloadAsset(params: Parameters<typeof normalizeAsset>[0]) {
return normalizeAsset(params, async bytes => {
const digest = createHash("sha256").update(bytes).digest("hex");
const { buffer } = await onceImageBytes(`photoshop:${digest}`, async () => ({
buffer: await convertImageToPhotoshopPng(bytes), contentType: "image/png",
}));
return buffer;
function getContentTypeMime(contentType: string): string {
return String(contentType || "")
.split(";")[0]
?.trim()
.toLowerCase();
}
function hasAsciiAt(buffer: Buffer, offset: number, value: string): boolean {
return buffer.subarray(offset, offset + value.length).toString("latin1") === value;
}
function looksLikeSvg(buffer: Buffer, contentType: string): boolean {
if (getContentTypeMime(contentType) === "image/svg+xml") return true;
const text = buffer.subarray(0, Math.min(buffer.length, 1024)).toString("utf8").trimStart();
return /^(?:<\?xml[^>]*>\s*)?<svg[\s>]/i.test(text);
}
function looksLikeErrorDocument(buffer: Buffer, contentType: string): boolean {
const mime = getContentTypeMime(contentType);
if (
mime.startsWith("text/") ||
mime === "application/json" ||
mime === "application/xml" ||
mime === "application/xhtml+xml"
) {
return true;
}
const text = buffer.subarray(0, Math.min(buffer.length, 512)).toString("utf8").trimStart();
return /^(?:<!doctype\s+html|<html[\s>]|<\?xml|<error[\s>]|[{[])/i.test(text);
}
function detectAsset(bytes: Buffer, contentType: string): { kind: "image" | "video" | "audio"; mimeType: string; ext: string } | null {
if (bytes.length < 4) return null;
if (bytes.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) {
return { kind: "image", mimeType: "image/png", ext: "png" };
}
if (bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) {
return { kind: "image", mimeType: "image/jpeg", ext: "jpg" };
}
if (hasAsciiAt(bytes, 0, "RIFF") && hasAsciiAt(bytes, 8, "WEBP")) {
return { kind: "image", mimeType: "image/webp", ext: "webp" };
}
if (hasAsciiAt(bytes, 0, "GIF87a") || hasAsciiAt(bytes, 0, "GIF89a")) {
return { kind: "image", mimeType: "image/gif", ext: "gif" };
}
if (hasAsciiAt(bytes, 0, "BM")) {
return { kind: "image", mimeType: "image/bmp", ext: "bmp" };
}
if (
bytes.subarray(0, 4).equals(Buffer.from([0x49, 0x49, 0x2a, 0x00])) ||
bytes.subarray(0, 4).equals(Buffer.from([0x4d, 0x4d, 0x00, 0x2a]))
) {
return { kind: "image", mimeType: "image/tiff", ext: "tiff" };
}
if (looksLikeSvg(bytes, contentType)) {
return { kind: "image", mimeType: "image/svg+xml", ext: "svg" };
}
if (hasAsciiAt(bytes, 0, "RIFF") && hasAsciiAt(bytes, 8, "WAVE")) return {kind:"audio",mimeType:"audio/wav",ext:"wav"};
if (hasAsciiAt(bytes, 0, "ID3") || (bytes[0] === 0xff && (bytes[1] & 0xe0) === 0xe0 && (bytes[1] & 0x06) !== 0)) return {kind:"audio",mimeType:"audio/mpeg",ext:"mp3"};
if (hasAsciiAt(bytes, 0, "OggS")) return {kind:"audio",mimeType:"audio/ogg",ext:"ogg"};
if (hasAsciiAt(bytes, 0, "fLaC")) return {kind:"audio",mimeType:"audio/flac",ext:"flac"};
if (getContentTypeMime(contentType).startsWith("audio/")) return {kind:"audio",mimeType:getContentTypeMime(contentType),ext:getContentTypeMime(contentType).includes("mp4") ? "m4a" : getContentTypeMime(contentType).split("/")[1]};
if (hasAsciiAt(bytes, 4, "ftyp")) {
const brand = bytes.subarray(8, Math.min(bytes.length, 32)).toString("latin1").toLowerCase();
if (brand.includes("m4a")) return {kind:"audio",mimeType:"audio/mp4",ext:"m4a"};
if (brand.includes("avif")) return { kind: "image", mimeType: "image/avif", ext: "avif" };
if (brand.includes("heic") || brand.includes("heif") || brand.includes("mif1")) {
return { kind: "image", mimeType: "image/heic", ext: "heic" };
}
return { kind: "video", mimeType: "video/mp4", ext: "mp4" };
}
if (bytes.subarray(0, 4).equals(Buffer.from([0x1a, 0x45, 0xdf, 0xa3]))) {
return { kind: "video", mimeType: "video/webm", ext: "webm" };
}
const mime = getContentTypeMime(contentType);
if (mime.startsWith("image/")) {
const ext = mime.includes("jpeg") ? "jpg" : mime.split("/")[1] || "png";
return { kind: "image", mimeType: mime, ext };
}
if (mime.startsWith("video/")) {
const ext = mime.includes("quicktime") ? "mov" : mime.split("/")[1] || "mp4";
return { kind: "video", mimeType: mime, ext };
}
return null;
}
export async function convertImageToPhotoshopPng(bytes: Buffer): Promise<Buffer> {
const sharp = await import("sharp");
const image = sharp.default(bytes, {
animated: false,
failOn: "none",
limitInputPixels: false,
});
const metadata = await image.metadata();
if (!metadata.width || !metadata.height) {
throw new Error("无法解析图片尺寸");
}
// RGB/RGBA, 8-bit, single-frame PNG is already compatible; preserve its exact bytes.
if (metadata.format === "png" && metadata.space === "srgb" && metadata.depth === "uchar"
&& !metadata.isPalette && (metadata.pages || 1) === 1 && (!metadata.orientation || metadata.orientation === 1)) return bytes;
return image.rotate().toColorspace("srgb").png({ compressionLevel: 3, adaptiveFiltering: true }).toBuffer();
}
function buildCanvasAssetFilename(params: {
item: DownloadFileItem;
index: number;
ext: string;
includeSequencePrefix: boolean;
}) {
const baseName = sanitizeCanvasDownloadFileName(
params.item.name || `canvas-image-${params.index + 1}`,
`canvas-image-${params.index + 1}`
);
if (!params.includeSequencePrefix) {
return `${baseName}.${params.ext}`;
}
const sequencePrefix = String(params.index + 1).padStart(3, "0");
return `${sequencePrefix}-${baseName}.${params.ext}`;
}
export async function normalizeCanvasDownloadAsset(params: {
item: DownloadFileItem;
index: number;
bytes: Buffer;
contentType: string;
includeSequencePrefix: boolean;
}): Promise<CanvasDownloadAssetResult> {
if (params.bytes.length === 0) {
throw new Error("empty response");
}
if (looksLikeErrorDocument(params.bytes, params.contentType) && !looksLikeSvg(params.bytes, params.contentType)) {
throw new Error("源地址返回的不是图片文件");
}
const detected = detectAsset(params.bytes, params.contentType);
if (!detected) {
throw new Error("无法识别图片格式");
}
if (detected.kind !== "image") {
return {
ok: true,
filename: buildCanvasAssetFilename({
item: params.item,
index: params.index,
ext: detected.ext,
includeSequencePrefix: params.includeSequencePrefix,
}),
bytes: params.bytes,
contentType: detected.mimeType,
};
}
const digest = createHash("sha256").update(params.bytes).digest("hex");
const { buffer: pngBytes } = await onceImageBytes(`photoshop:${digest}`, async () => ({
buffer: await convertImageToPhotoshopPng(params.bytes), contentType: "image/png",
}));
return {
ok: true,
filename: buildCanvasAssetFilename({
item: params.item,
index: params.index,
ext: "png",
includeSequencePrefix: params.includeSequencePrefix,
}),
bytes: pngBytes,
contentType: "image/png",
};
}
export async function fetchCanvasDownloadAsset(params: {

Some files were not shown because too many files have changed in this diff Show More