Compare commits
No commits in common. "main" and "codex/platform-failure-recovery-20261104" have entirely different histories.
main
...
codex/plat
@ -1,47 +0,0 @@
|
|||||||
import { saveMessages } from "@/lib/db/queries";
|
|
||||||
import type { ImageGenerationStatusStreamPayload } from "@/lib/canvas/image-generation-status";
|
|
||||||
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
|
||||||
import { safeWriteStreamChunk, type StreamWriter, writeAssistantTextChunk, writeStructuredDataChunk } from "./stream";
|
|
||||||
|
|
||||||
export async function reportImageGenerationFailure(params: {
|
|
||||||
writer: StreamWriter;
|
|
||||||
chatId: string;
|
|
||||||
userMessageId: string;
|
|
||||||
messageId: string;
|
|
||||||
error: unknown;
|
|
||||||
label: string;
|
|
||||||
}) {
|
|
||||||
const failure = getRequestFailureDetails(params.error, "图片生成失败");
|
|
||||||
const status: ImageGenerationStatusStreamPayload = {
|
|
||||||
phase: "failed",
|
|
||||||
userMessageId: params.userMessageId,
|
|
||||||
requestId: params.messageId,
|
|
||||||
failure,
|
|
||||||
};
|
|
||||||
const text = formatRequestFailure(failure, params.messageId);
|
|
||||||
const parts = [
|
|
||||||
{ type: "text", text },
|
|
||||||
{ type: "data-imageGenerationStatus", data: status },
|
|
||||||
];
|
|
||||||
writeStructuredDataChunk(params.writer, "data-imageGenerationStatus", status, `${params.label}:status`);
|
|
||||||
safeWriteStreamChunk(params.writer, {
|
|
||||||
type: "data-appendMessage",
|
|
||||||
data: JSON.stringify({ id: params.messageId, role: "assistant", parts }),
|
|
||||||
}, params.label);
|
|
||||||
writeAssistantTextChunk(params.writer, "<!-- IMAGE_GENERATION_END -->", `${params.label}:end`);
|
|
||||||
// History carries the same terminal state when the browser loses the stream.
|
|
||||||
await saveMessages({ messages: [{
|
|
||||||
id: params.messageId,
|
|
||||||
chatId: params.chatId,
|
|
||||||
role: "assistant",
|
|
||||||
parts,
|
|
||||||
attachments: [],
|
|
||||||
createdAt: new Date(),
|
|
||||||
videoStorageStatus: null,
|
|
||||||
temporaryVideoUrl: null,
|
|
||||||
permanentVideoUrl: null,
|
|
||||||
archivedAt: null,
|
|
||||||
videoMetadata: null,
|
|
||||||
}] });
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
@ -208,7 +208,6 @@ export function writeImageGenerationStatusChunk(
|
|||||||
writer: StreamWriter,
|
writer: StreamWriter,
|
||||||
params: {
|
params: {
|
||||||
phase: "queued" | "processing" | "finalizing" | "failed" | "completed";
|
phase: "queued" | "processing" | "finalizing" | "failed" | "completed";
|
||||||
userMessageId?: string;
|
|
||||||
providerType?: string | null;
|
providerType?: string | null;
|
||||||
modelName?: string | null;
|
modelName?: string | null;
|
||||||
imageCount?: number | null;
|
imageCount?: number | null;
|
||||||
@ -224,7 +223,6 @@ export function writeImageGenerationStatusChunk(
|
|||||||
"data-imageGenerationStatus",
|
"data-imageGenerationStatus",
|
||||||
{
|
{
|
||||||
phase: params.phase,
|
phase: params.phase,
|
||||||
userMessageId: params.userMessageId,
|
|
||||||
providerType: normalizeImageLoadingProviderType(params.providerType),
|
providerType: normalizeImageLoadingProviderType(params.providerType),
|
||||||
modelName: String(params.modelName || "").trim() || undefined,
|
modelName: String(params.modelName || "").trim() || undefined,
|
||||||
imageCount:
|
imageCount:
|
||||||
|
|||||||
@ -426,8 +426,6 @@ import {
|
|||||||
type VideoModeType,
|
type VideoModeType,
|
||||||
} from "./_lib/video-models";
|
} from "./_lib/video-models";
|
||||||
|
|
||||||
import { reportImageGenerationFailure } from "./_lib/image-generation-failure";
|
|
||||||
|
|
||||||
const chatApiLogger = createLogger("chat.route");
|
const chatApiLogger = createLogger("chat.route");
|
||||||
const createUIMessageStream: typeof createAiUIMessageStream = (options) =>
|
const createUIMessageStream: typeof createAiUIMessageStream = (options) =>
|
||||||
createAiUIMessageStream({ ...options, execute: (writer) => trackRuntimeWork(() => options.execute(writer)) });
|
createAiUIMessageStream({ ...options, execute: (writer) => trackRuntimeWork(() => options.execute(writer)) });
|
||||||
@ -448,6 +446,16 @@ function assertGeneratedImageMarkdownHasImage(markdown: string, modelLabel: stri
|
|||||||
throw new Error(`${modelLabel} 未返回可用图片:${reason.slice(0, 160)}`);
|
throw new Error(`${modelLabel} 未返回可用图片:${reason.slice(0, 160)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPublicImageGenerationErrorText(error: unknown, requestId: string) {
|
||||||
|
const message = error instanceof Error ? error.message : String(error || "");
|
||||||
|
const publicMessage = getUserFacingImageGenerationErrorMessage(error);
|
||||||
|
if (!message || (publicMessage === message && INTERNAL_IMAGE_GENERATION_FAILURE_PATTERN.test(message))) {
|
||||||
|
return `图片生成失败,请稍后重试。若连续失败,请联系管理员。\n\n请求编号:${requestId}`;
|
||||||
|
}
|
||||||
|
return `${publicMessage}\n\n请求编号:${requestId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const AGENT_REPLACEMENT_PARALLEL_WORKER_COUNT = Math.max(
|
const AGENT_REPLACEMENT_PARALLEL_WORKER_COUNT = Math.max(
|
||||||
1,
|
1,
|
||||||
Math.min(8, Number(process.env.AGENT_REPLACEMENT_PARALLEL_WORKER_COUNT || 6) || 6),
|
Math.min(8, Number(process.env.AGENT_REPLACEMENT_PARALLEL_WORKER_COUNT || 6) || 6),
|
||||||
@ -5680,7 +5688,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
modelName: resolvedModelRef,
|
modelName: resolvedModelRef,
|
||||||
imageCount: replacementReferences.length,
|
imageCount: replacementReferences.length,
|
||||||
@ -6023,7 +6030,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase:
|
phase:
|
||||||
queueStatus.status === "running"
|
queueStatus.status === "running"
|
||||||
? "processing"
|
? "processing"
|
||||||
@ -6179,7 +6185,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: successCount > 0 ? "completed" : "failed",
|
phase: successCount > 0 ? "completed" : "failed",
|
||||||
modelName: resolvedModelDisplayName,
|
modelName: resolvedModelDisplayName,
|
||||||
imageCount: successCount,
|
imageCount: successCount,
|
||||||
@ -6412,7 +6417,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
modelName:
|
modelName:
|
||||||
selectedAgentPreset.defaultModel?.displayName ||
|
selectedAgentPreset.defaultModel?.displayName ||
|
||||||
@ -6964,7 +6968,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase:
|
phase:
|
||||||
queueStatus.status === "running"
|
queueStatus.status === "running"
|
||||||
? "processing"
|
? "processing"
|
||||||
@ -7290,7 +7293,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase:
|
phase:
|
||||||
queueStatus.status === "running"
|
queueStatus.status === "running"
|
||||||
? "processing"
|
? "processing"
|
||||||
@ -7519,7 +7521,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase:
|
phase:
|
||||||
queueStatus.status === "running"
|
queueStatus.status === "running"
|
||||||
? "processing"
|
? "processing"
|
||||||
@ -7838,7 +7839,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: successCount > 0 ? "completed" : "failed",
|
phase: successCount > 0 ? "completed" : "failed",
|
||||||
modelName:
|
modelName:
|
||||||
selectedAgentPreset.defaultModel?.displayName ||
|
selectedAgentPreset.defaultModel?.displayName ||
|
||||||
@ -8048,7 +8048,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: requestedModelProviderType,
|
providerType: requestedModelProviderType,
|
||||||
modelName: finalModelId,
|
modelName: finalModelId,
|
||||||
@ -8087,7 +8086,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
...directImageStatusPayload,
|
...directImageStatusPayload,
|
||||||
},
|
},
|
||||||
@ -8180,7 +8178,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: requestedModelProviderType,
|
providerType: requestedModelProviderType,
|
||||||
modelName: directImageResult.model,
|
modelName: directImageResult.model,
|
||||||
@ -8201,16 +8198,42 @@ export async function POST(request: Request) {
|
|||||||
);
|
);
|
||||||
} catch (directToolError) {
|
} catch (directToolError) {
|
||||||
chatApiLogger.error("[Chat API] Direct agent image generation failed:", directToolError);
|
chatApiLogger.error("[Chat API] Direct agent image generation failed:", directToolError);
|
||||||
const errorMessage = await reportImageGenerationFailure({
|
const errorMessage = getDirectAgentToolErrorMessage(
|
||||||
writer, chatId: id, userMessageId: message.id,
|
"generate_image",
|
||||||
messageId: assistantMessageId, error: directToolError,
|
directToolError,
|
||||||
label: "agent:generate_image:error",
|
);
|
||||||
|
await saveAssistantTextOnlyMessage({
|
||||||
|
chatId: id,
|
||||||
|
messageId: assistantMessageId,
|
||||||
|
text: errorMessage,
|
||||||
});
|
});
|
||||||
|
writeAssistantTextChunk(
|
||||||
|
writer,
|
||||||
|
errorMessage,
|
||||||
|
"agent:generate_image:error",
|
||||||
|
assistantMessageId,
|
||||||
|
);
|
||||||
|
if (contextPayload?.source === "canvas") {
|
||||||
|
writeImageGenerationStatusChunk(
|
||||||
|
writer,
|
||||||
|
{
|
||||||
|
phase: "failed",
|
||||||
|
providerType: requestedModelProviderType,
|
||||||
|
modelName: finalModelId,
|
||||||
|
},
|
||||||
|
"agent:generate_image:status-failed",
|
||||||
|
);
|
||||||
|
}
|
||||||
writeAgentProgressChunk(
|
writeAgentProgressChunk(
|
||||||
writer,
|
writer,
|
||||||
buildDirectAgentImageGenerateFailedProgress(errorMessage),
|
buildDirectAgentImageGenerateFailedProgress(errorMessage),
|
||||||
"agent:generate_image:progress-failed",
|
"agent:generate_image:progress-failed",
|
||||||
);
|
);
|
||||||
|
writeAssistantTextChunk(
|
||||||
|
writer,
|
||||||
|
"<!-- IMAGE_GENERATION_END -->",
|
||||||
|
"agent:generate_image:end-error",
|
||||||
|
);
|
||||||
} finally {
|
} finally {
|
||||||
stopDirectImageKeepalive();
|
stopDirectImageKeepalive();
|
||||||
if (stopDirectImageStatus) {
|
if (stopDirectImageStatus) {
|
||||||
@ -9172,7 +9195,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: (languageModel as any).provider,
|
providerType: (languageModel as any).provider,
|
||||||
modelName: currentModelConfig?.displayName || actualModelId,
|
modelName: currentModelConfig?.displayName || actualModelId,
|
||||||
@ -9260,7 +9282,6 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.warn("[Chat API] Record Gemini fallback health failure failed:", healthError);
|
chatApiLogger.warn("[Chat API] Record Gemini fallback health failure failed:", healthError);
|
||||||
});
|
});
|
||||||
chatApiLogger.error(`[Chat API] ❌ Fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ Fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -9335,7 +9356,6 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.warn("[Chat API] Record Gemini cross-provider health failure failed:", healthError);
|
chatApiLogger.warn("[Chat API] Record Gemini cross-provider health failure failed:", healthError);
|
||||||
});
|
});
|
||||||
chatApiLogger.error(`[Chat API] ❌ Gemini cross-provider fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ Gemini cross-provider fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -9403,7 +9423,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: (languageModel as any).provider,
|
providerType: (languageModel as any).provider,
|
||||||
modelName: usedModelDisplayName,
|
modelName: usedModelDisplayName,
|
||||||
@ -9468,14 +9487,6 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
chatApiLogger.error("[Chat API] Gemini generation error:", error);
|
chatApiLogger.error("[Chat API] Gemini generation error:", error);
|
||||||
let finalError = error;
|
|
||||||
if (!shouldFailoverAiModelError(error)) {
|
|
||||||
await reportImageGenerationFailure({
|
|
||||||
writer, chatId: id, userMessageId: message.id,
|
|
||||||
messageId: generateUUID(), error, label: "gemini:error",
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
chatApiLogger.info("[Chat API] Attempting failover to Volcano Vision...");
|
chatApiLogger.info("[Chat API] Attempting failover to Volcano Vision...");
|
||||||
|
|
||||||
// 故障转移:尝试使用火山引擎模型
|
// 故障转移:尝试使用火山引擎模型
|
||||||
@ -9553,7 +9564,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: "volcano-vision",
|
providerType: "volcano-vision",
|
||||||
modelName: volcanoModel.model.displayName || volcanoModel.model.modelId,
|
modelName: volcanoModel.model.displayName || volcanoModel.model.modelId,
|
||||||
@ -9619,14 +9629,47 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.info("[Chat API] No Volcano Vision model available for failover");
|
chatApiLogger.info("[Chat API] No Volcano Vision model available for failover");
|
||||||
} catch (failoverError) {
|
} catch (failoverError) {
|
||||||
chatApiLogger.error("[Chat API] Failover to Volcano Vision also failed:", failoverError);
|
chatApiLogger.error("[Chat API] Failover to Volcano Vision also failed:", failoverError);
|
||||||
finalError = failoverError;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果故障转移也失败,返回错误消息
|
// 如果故障转移也失败,返回错误消息
|
||||||
await reportImageGenerationFailure({
|
const errorMessageId = generateUUID();
|
||||||
writer, chatId: id, userMessageId: message.id,
|
const errorText = getPublicImageGenerationErrorText(error, errorMessageId);
|
||||||
messageId: generateUUID(), error: finalError, label: "gemini:error",
|
writeAssistantTextChunk(
|
||||||
});
|
writer,
|
||||||
|
errorText,
|
||||||
|
"gemini:error",
|
||||||
|
errorMessageId,
|
||||||
|
);
|
||||||
|
|
||||||
|
// 🆕 图片生成结束标记(失败)
|
||||||
|
writeAssistantTextChunk(
|
||||||
|
writer,
|
||||||
|
"<!-- IMAGE_GENERATION_END -->",
|
||||||
|
"gemini:image-end-failed",
|
||||||
|
);
|
||||||
|
|
||||||
|
// 尝试保存错误消息
|
||||||
|
try {
|
||||||
|
await saveMessages({
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
id: errorMessageId,
|
||||||
|
chatId: id,
|
||||||
|
role: "assistant",
|
||||||
|
parts: [{ type: "text", text: errorText }],
|
||||||
|
attachments: [],
|
||||||
|
createdAt: new Date(),
|
||||||
|
videoStorageStatus: null,
|
||||||
|
temporaryVideoUrl: null,
|
||||||
|
permanentVideoUrl: null,
|
||||||
|
archivedAt: null,
|
||||||
|
videoMetadata: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
} catch (saveError) {
|
||||||
|
chatApiLogger.error("[Chat API] Failed to save error message:", saveError);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
stopKeepalive?.();
|
stopKeepalive?.();
|
||||||
}
|
}
|
||||||
@ -9824,7 +9867,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "queued",
|
phase: "queued",
|
||||||
providerType: "fal-image",
|
providerType: "fal-image",
|
||||||
modelName: modelDisplayName,
|
modelName: modelDisplayName,
|
||||||
@ -9931,7 +9973,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: (languageModel as any).provider,
|
providerType: (languageModel as any).provider,
|
||||||
modelName: alibabaModelConfig?.displayName || actualModelId,
|
modelName: alibabaModelConfig?.displayName || actualModelId,
|
||||||
@ -10029,7 +10070,6 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.warn("[Chat API] Record image fallback health failure failed:", healthError);
|
chatApiLogger.warn("[Chat API] Record image fallback health failure failed:", healthError);
|
||||||
});
|
});
|
||||||
chatApiLogger.error(`[Chat API] ❌ Alibaba fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ Alibaba fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -10164,7 +10204,6 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.warn("[Chat API] Record image cross-provider health failure failed:", healthError);
|
chatApiLogger.warn("[Chat API] Record image cross-provider health failure failed:", healthError);
|
||||||
});
|
});
|
||||||
chatApiLogger.error(`[Chat API] ❌ Cross-provider failover failed: ${candidate.model.displayName} (${candidate.provider.type})`, crossError);
|
chatApiLogger.error(`[Chat API] ❌ Cross-provider failover failed: ${candidate.model.displayName} (${candidate.provider.type})`, crossError);
|
||||||
if (!shouldFailoverAiModelError(crossError)) throw crossError;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -10232,7 +10271,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: (languageModel as any).provider,
|
providerType: (languageModel as any).provider,
|
||||||
modelName: usedModelDisplayName,
|
modelName: usedModelDisplayName,
|
||||||
@ -10297,10 +10335,43 @@ export async function POST(request: Request) {
|
|||||||
chatApiLogger.error("[Chat API] Alibaba generation error:", error);
|
chatApiLogger.error("[Chat API] Alibaba generation error:", error);
|
||||||
|
|
||||||
// 返回错误消息
|
// 返回错误消息
|
||||||
await reportImageGenerationFailure({
|
const errorMessageId = generateUUID();
|
||||||
writer, chatId: id, userMessageId: message.id,
|
const errorText = getPublicImageGenerationErrorText(error, errorMessageId);
|
||||||
messageId: generateUUID(), error, label: "alibaba:error",
|
writeAssistantTextChunk(
|
||||||
});
|
writer,
|
||||||
|
errorText,
|
||||||
|
"alibaba:error",
|
||||||
|
errorMessageId,
|
||||||
|
);
|
||||||
|
|
||||||
|
// 🆕 图片生成结束标记(失败)
|
||||||
|
writeAssistantTextChunk(
|
||||||
|
writer,
|
||||||
|
"<!-- IMAGE_GENERATION_END -->",
|
||||||
|
"alibaba:image-end-failed",
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await saveMessages({
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
id: errorMessageId,
|
||||||
|
chatId: id,
|
||||||
|
role: "assistant",
|
||||||
|
parts: [{ type: "text", text: errorText }],
|
||||||
|
attachments: [],
|
||||||
|
createdAt: new Date(),
|
||||||
|
videoStorageStatus: null,
|
||||||
|
temporaryVideoUrl: null,
|
||||||
|
permanentVideoUrl: null,
|
||||||
|
archivedAt: null,
|
||||||
|
videoMetadata: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
} catch (saveError) {
|
||||||
|
chatApiLogger.error("[Chat API] Failed to save error message:", saveError);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
stopKeepalive?.();
|
stopKeepalive?.();
|
||||||
}
|
}
|
||||||
@ -10429,7 +10500,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: "volcano-vision",
|
providerType: "volcano-vision",
|
||||||
modelName: volcanoModelConfig?.displayName || actualModelId,
|
modelName: volcanoModelConfig?.displayName || actualModelId,
|
||||||
@ -10475,7 +10545,6 @@ export async function POST(request: Request) {
|
|||||||
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
||||||
}
|
}
|
||||||
chatApiLogger.error(`[Chat API] ❌ Volcano fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ Volcano fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!fallbackSuccess) {
|
if (!fallbackSuccess) {
|
||||||
@ -10642,7 +10711,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
writer,
|
writer,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: "volcano-vision",
|
providerType: "volcano-vision",
|
||||||
modelName: usedModelDisplayName,
|
modelName: usedModelDisplayName,
|
||||||
@ -10707,10 +10775,44 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
chatApiLogger.error("[Chat API] Volcano Vision generation error:", error);
|
chatApiLogger.error("[Chat API] Volcano Vision generation error:", error);
|
||||||
await reportImageGenerationFailure({
|
const errorMessageId = generateUUID();
|
||||||
writer, chatId: id, userMessageId: message.id,
|
const errorText = getPublicImageGenerationErrorText(error, errorMessageId);
|
||||||
messageId: generateUUID(), error, label: "volcano:error",
|
writeAssistantTextChunk(
|
||||||
});
|
writer,
|
||||||
|
errorText,
|
||||||
|
"volcano:error",
|
||||||
|
errorMessageId,
|
||||||
|
);
|
||||||
|
|
||||||
|
// 🆕 图片生成结束标记(失败)
|
||||||
|
writeAssistantTextChunk(
|
||||||
|
writer,
|
||||||
|
"<!-- IMAGE_GENERATION_END -->",
|
||||||
|
"volcano:image-end-failed",
|
||||||
|
);
|
||||||
|
|
||||||
|
// 尝试保存错误消息
|
||||||
|
try {
|
||||||
|
await saveMessages({
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
id: errorMessageId,
|
||||||
|
chatId: id,
|
||||||
|
role: "assistant",
|
||||||
|
parts: [{ type: "text", text: errorText }],
|
||||||
|
attachments: [], // 必须提供 attachments 字段
|
||||||
|
createdAt: new Date(),
|
||||||
|
videoStorageStatus: null,
|
||||||
|
temporaryVideoUrl: null,
|
||||||
|
permanentVideoUrl: null,
|
||||||
|
archivedAt: null,
|
||||||
|
videoMetadata: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
} catch (saveError) {
|
||||||
|
chatApiLogger.error("[Chat API] Failed to save error message:", saveError);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
stopKeepalive?.();
|
stopKeepalive?.();
|
||||||
}
|
}
|
||||||
@ -11283,7 +11385,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
dataStream as unknown as StreamWriter,
|
dataStream as unknown as StreamWriter,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "processing",
|
phase: "processing",
|
||||||
providerType: currentModelConfig?.providerId ? resolvedProviderType || providerType : providerType,
|
providerType: currentModelConfig?.providerId ? resolvedProviderType || providerType : providerType,
|
||||||
modelName: currentModelConfig?.displayName || actualModelId,
|
modelName: currentModelConfig?.displayName || actualModelId,
|
||||||
@ -11375,7 +11476,6 @@ export async function POST(request: Request) {
|
|||||||
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
setImageFallbackModelCooldown(candidate.model.id, fallbackErrorMessage);
|
||||||
}
|
}
|
||||||
chatApiLogger.error(`[Chat API] ❌ OpenAI-compatible fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
chatApiLogger.error(`[Chat API] ❌ OpenAI-compatible fallback model ${candidate.model.displayName} (${candidate.provider.type}) failed:`, fallbackError);
|
||||||
if (!shouldFailoverAiModelError(fallbackError)) throw fallbackError;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -11547,7 +11647,6 @@ export async function POST(request: Request) {
|
|||||||
writeImageGenerationStatusChunk(
|
writeImageGenerationStatusChunk(
|
||||||
dataStream as unknown as StreamWriter,
|
dataStream as unknown as StreamWriter,
|
||||||
{
|
{
|
||||||
userMessageId: message.id,
|
|
||||||
phase: "completed",
|
phase: "completed",
|
||||||
providerType: resolvedProviderType || providerType,
|
providerType: resolvedProviderType || providerType,
|
||||||
modelName: usedModelDisplayName,
|
modelName: usedModelDisplayName,
|
||||||
@ -11607,10 +11706,54 @@ export async function POST(request: Request) {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
chatApiLogger.error("[Chat API] OpenAI-compatible image model generation error:", error);
|
chatApiLogger.error("[Chat API] OpenAI-compatible image model generation error:", error);
|
||||||
|
|
||||||
await reportImageGenerationFailure({
|
const errorMessageId = generateUUID();
|
||||||
writer: dataStream as unknown as StreamWriter,
|
const errorText = getPublicImageGenerationErrorText(error, errorMessageId);
|
||||||
chatId: id, userMessageId: message.id,
|
|
||||||
messageId: generateUUID(), error, label: "openai-compat:error",
|
dataStream.write({
|
||||||
|
type: "data-appendMessage",
|
||||||
|
data: JSON.stringify({
|
||||||
|
id: errorMessageId,
|
||||||
|
role: "assistant",
|
||||||
|
parts: [{ type: "text", text: errorText }],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
dataStream.write({
|
||||||
|
type: "data-appendMessage",
|
||||||
|
data: JSON.stringify({
|
||||||
|
id: generateUUID(),
|
||||||
|
role: "assistant",
|
||||||
|
parts: [{ type: "text", text: "<!-- IMAGE_GENERATION_END -->" }],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
if (contextPayload?.source === "canvas") {
|
||||||
|
writeImageGenerationStatusChunk(
|
||||||
|
dataStream as unknown as StreamWriter,
|
||||||
|
{
|
||||||
|
phase: "failed",
|
||||||
|
providerType: resolvedProviderType || providerType,
|
||||||
|
modelName: currentModelConfig?.displayName || actualModelId,
|
||||||
|
imageCount: effectiveImageParams?.imageCount || 1,
|
||||||
|
},
|
||||||
|
"openai-compat:image-status-failed",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await saveMessages({
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
id: errorMessageId,
|
||||||
|
role: "assistant",
|
||||||
|
parts: [{ type: "text", text: errorText }],
|
||||||
|
createdAt: new Date(),
|
||||||
|
attachments: [],
|
||||||
|
chatId: id,
|
||||||
|
videoStorageStatus: null,
|
||||||
|
temporaryVideoUrl: null,
|
||||||
|
permanentVideoUrl: null,
|
||||||
|
archivedAt: null,
|
||||||
|
videoMetadata: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,8 +8,6 @@ import { ArrowLeft, Download, ChevronLeft, ChevronRight, ChevronsLeft, ChevronsR
|
|||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import { formatBeijingTime } from "@/lib/utils";
|
import { formatBeijingTime } from "@/lib/utils";
|
||||||
import { toast } from "sonner";
|
|
||||||
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
|
||||||
|
|
||||||
type FeaturedImage = {
|
type FeaturedImage = {
|
||||||
id: string;
|
id: string;
|
||||||
@ -76,11 +74,18 @@ export default function FeaturedPage() {
|
|||||||
|
|
||||||
const handleDownload = async (url: string, prompt: string) => {
|
const handleDownload = async (url: string, prompt: string) => {
|
||||||
try {
|
try {
|
||||||
await downloadOriginalImage({ url, name: "featured-image" });
|
const response = await fetch(url);
|
||||||
toast.success("原图已开始下载");
|
const blob = await response.blob();
|
||||||
|
const blobUrl = window.URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = blobUrl;
|
||||||
|
link.download = `featured-${Date.now()}.png`;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
window.URL.revokeObjectURL(blobUrl);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Download failed:", error);
|
console.error("Download failed:", error);
|
||||||
toast.error(getCanvasImageDownloadErrorMessage(error));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -13,7 +13,6 @@ import { requestLongTaskResult } from "@/lib/client/long-task";
|
|||||||
import { buildImageProxyUrl, getImageDownloadExtension } from "@/lib/image-delivery";
|
import { buildImageProxyUrl, getImageDownloadExtension } from "@/lib/image-delivery";
|
||||||
import { inferOriginalImageUrl } from "@/lib/image-url";
|
import { inferOriginalImageUrl } from "@/lib/image-url";
|
||||||
import { formatBeijingTime } from "@/lib/utils";
|
import { formatBeijingTime } from "@/lib/utils";
|
||||||
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
|
||||||
|
|
||||||
type GalleryImage = {
|
type GalleryImage = {
|
||||||
id: string;
|
id: string;
|
||||||
@ -60,7 +59,7 @@ function buildGalleryDownloadFilename(url: string, alt: string, contentType: str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${safeBase}.${getImageDownloadExtension(contentType) || extension}`;
|
return `${safeBase}-${Date.now()}.${getImageDownloadExtension(contentType) || extension}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function GalleryPage() {
|
export default function GalleryPage() {
|
||||||
@ -120,11 +119,25 @@ export default function GalleryPage() {
|
|||||||
const parsed = new URL(originalUrl, window.location.origin);
|
const parsed = new URL(originalUrl, window.location.origin);
|
||||||
const source = parsed.pathname === "/api/image-proxy" ? originalUrl : inferOriginalImageUrl(originalUrl);
|
const source = parsed.pathname === "/api/image-proxy" ? originalUrl : inferOriginalImageUrl(originalUrl);
|
||||||
|
|
||||||
await downloadOriginalImage({ url: source, name: buildGalleryDownloadFilename(originalUrl, alt, "") });
|
const response = await fetch(
|
||||||
|
buildImageProxyUrl(source, { delivery: "original" })
|
||||||
|
);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`download failed: ${response.status}`);
|
||||||
|
}
|
||||||
|
const blob = await response.blob();
|
||||||
|
const blobUrl = window.URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = blobUrl;
|
||||||
|
link.download = buildGalleryDownloadFilename(originalUrl, alt, blob.type);
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
window.setTimeout(() => window.URL.revokeObjectURL(blobUrl), 1000);
|
||||||
toast.success("图片已开始下载");
|
toast.success("图片已开始下载");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Download failed:", error);
|
console.error("Download failed:", error);
|
||||||
toast.error(getCanvasImageDownloadErrorMessage(error));
|
toast.error(error instanceof Error && error.message.includes("无法确认原图") ? error.message : "原图下载失败,请重试");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
export function GET() {
|
|
||||||
return NextResponse.json({ version: process.env.NEXT_PUBLIC_APP_RELEASE || "development" }, {
|
|
||||||
headers: { "Cache-Control": "no-store, max-age=0" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@ -1,5 +1,5 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { buildThreeAssetUrl } from "@/lib/canvas/three-asset-url";
|
import { buildThreeAssetUrl } from "@/lib/canvas/three-asset-url";
|
||||||
|
|
||||||
@ -182,18 +182,6 @@ async function handleRequest(request: Request, method: "GET" | "HEAD") {
|
|||||||
return NextResponse.json({ success: false, error: "仅支持 http/https 模型地址" }, { status: 400 });
|
return NextResponse.json({ success: false, error: "仅支持 http/https 模型地址" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalizeLegacyAliyunStorageUrl(targetUrl.toString())) {
|
|
||||||
try {
|
|
||||||
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
|
||||||
targetUrl = new URL(await resolveLegacyAliyunStorageUrl(targetUrl.toString()));
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof Error && error.message.includes("HTTP 410")) {
|
|
||||||
return NextResponse.json({ success: false, error: RETIRED_STORAGE_MESSAGE, code: "storage_retired" }, { status: 410 });
|
|
||||||
}
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isAllowedModelHost(targetUrl.hostname)) {
|
if (!isAllowedModelHost(targetUrl.hostname)) {
|
||||||
return NextResponse.json({ success: false, error: "不允许的模型地址域名" }, { status: 403 });
|
return NextResponse.json({ success: false, error: "不允许的模型地址域名" }, { status: 403 });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@ import { NextResponse } from "next/server";
|
|||||||
import { ZodError } from "zod";
|
import { ZodError } from "zod";
|
||||||
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { RmbgServiceError } from "@/lib/ai/rmbg/service";
|
|
||||||
import { canvasCapabilityClientCommandSchema } from "@/lib/canvas/capabilities/client-command";
|
import { canvasCapabilityClientCommandSchema } from "@/lib/canvas/capabilities/client-command";
|
||||||
import { CanvasCapabilityRuntimeError } from "@/lib/canvas/capabilities/context";
|
import { CanvasCapabilityRuntimeError } from "@/lib/canvas/capabilities/context";
|
||||||
import { getCanvasCapabilityManifest } from "@/lib/canvas/capabilities/registry";
|
import { getCanvasCapabilityManifest } from "@/lib/canvas/capabilities/registry";
|
||||||
@ -91,7 +90,7 @@ export async function POST(
|
|||||||
const failure = getRequestFailureDetails(error, "执行画布能力失败");
|
const failure = getRequestFailureDetails(error, "执行画布能力失败");
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, error: formatRequestFailure(failure, requestId), failure, requestId },
|
{ success: false, error: formatRequestFailure(failure, requestId), failure, requestId },
|
||||||
{ status: error instanceof RmbgServiceError ? error.status : 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,100 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { executeBoxCutoutPOST } from "@/lib/canvas/operations/box-cutout";
|
import {
|
||||||
|
BoxCutoutServiceError,
|
||||||
|
executeBoxCutoutJob,
|
||||||
|
} from "@/lib/ai/box-cutout/service";
|
||||||
|
import { RmbgServiceError } from "@/lib/ai/rmbg/service";
|
||||||
|
import { getPointsSystemConfig } from "@/lib/db/queries";
|
||||||
|
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
||||||
|
import { getImageToolCreditCost } from "@/lib/points/config";
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 60;
|
export const maxDuration = 60;
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
type BoxCutoutRequest = {
|
||||||
const session = await auth();
|
imageUrl?: string;
|
||||||
if (!session?.user?.id) {
|
cropRect?: {
|
||||||
return NextResponse.json({ success: false, error: "未授权,请先登录" }, { status: 401 });
|
x?: number;
|
||||||
|
y?: number;
|
||||||
|
width?: number;
|
||||||
|
height?: number;
|
||||||
|
};
|
||||||
|
paddingRatio?: number;
|
||||||
|
prompt?: string;
|
||||||
|
chatId?: string | null;
|
||||||
|
messageId?: string | null;
|
||||||
|
skipGallerySave?: boolean;
|
||||||
|
sourceNodeId?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session?.user?.id) {
|
||||||
|
return NextResponse.json({ success: false, error: "未授权,请先登录" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = (await request.json()) as BoxCutoutRequest;
|
||||||
|
const rawImageUrl = String(body.imageUrl || "").trim();
|
||||||
|
if (!rawImageUrl) {
|
||||||
|
return NextResponse.json({ success: false, error: "缺少 imageUrl 参数" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolvedImageUrl = new URL(rawImageUrl, request.url).toString();
|
||||||
|
const pointsConfig = await getPointsSystemConfig();
|
||||||
|
const pointsCharge = await prepareFeaturePointsCharge({
|
||||||
|
userId: session.user.id,
|
||||||
|
unitCost: getImageToolCreditCost(pointsConfig, "box-cutout"),
|
||||||
|
reason: "image_box_cutout",
|
||||||
|
modelId: "box-cutout",
|
||||||
|
note: "框选抠图消耗积分",
|
||||||
|
pointsConfig,
|
||||||
|
});
|
||||||
|
if (!pointsCharge.ok) {
|
||||||
|
return NextResponse.json(pointsCharge.payload, { status: pointsCharge.status });
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await executeBoxCutoutJob({
|
||||||
|
userId: session.user.id,
|
||||||
|
imageUrl: resolvedImageUrl,
|
||||||
|
cropRect: body.cropRect || {},
|
||||||
|
paddingRatio: body.paddingRatio,
|
||||||
|
prompt: body.prompt || "框选抠图",
|
||||||
|
chatId: body.chatId || null,
|
||||||
|
messageId: body.messageId || null,
|
||||||
|
skipGallerySave: body.skipGallerySave === true,
|
||||||
|
sourceNodeId: body.sourceNodeId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (pointsCharge.chargeable) {
|
||||||
|
try {
|
||||||
|
await pointsCharge.consume();
|
||||||
|
} catch (pointsError) {
|
||||||
|
console.error("[Points] Failed to consume box cutout points:", pointsError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(result);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof BoxCutoutServiceError || error instanceof RmbgServiceError) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
error: error.detail || error.message,
|
||||||
|
},
|
||||||
|
{ status: error.status },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error("[BoxCutout] 处理失败:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
error: error instanceof Error ? error.message : "框选抠图失败",
|
||||||
|
},
|
||||||
|
{ status: 500 },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return executeBoxCutoutPOST(request, session.user.id);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
import { resolveCanvasCdnDownload } from "@/lib/storage/canvas-cdn-download";
|
import { resolveCanvasCdnDownload } from "@/lib/storage/canvas-cdn-download";
|
||||||
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
import { scheduleImageDownloadPreparation } from "@/lib/storage/image-download-preparation";
|
import { prepareImageArchiveDownload } from "@/lib/storage/image-download-preparation";
|
||||||
import { randomUUID } from "node:crypto";
|
|
||||||
import { formatDownloadFailure, getDownloadFailure, type DownloadFailure } from "@/lib/canvas/download-error";
|
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
|
||||||
import { after, NextRequest, NextResponse } from "next/server";
|
import { after, NextRequest, NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { getDownloadPreparationQueue, type DownloadPreparationInput } from "@/lib/canvas/download-preparation";
|
import { getDownloadPreparationQueue, type DownloadPreparationInput } from "@/lib/canvas/download-preparation";
|
||||||
@ -16,22 +13,6 @@ import {
|
|||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 120;
|
export const maxDuration = 120;
|
||||||
const logger = createLogger("canvas.download");
|
|
||||||
|
|
||||||
function failed(failure: DownloadFailure, requestId: string) {
|
|
||||||
logger.warn("request_failed", { requestId, code: failure.code, status: failure.status });
|
|
||||||
return NextResponse.json({ success: false, error: formatDownloadFailure(failure, requestId), failure, requestId }, {
|
|
||||||
status: failure.status,
|
|
||||||
headers: { "Cache-Control": "no-store", ...(failure.status === 429 ? { "Retry-After": "3" } : {}) },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function preparing(requestId: string) {
|
|
||||||
return NextResponse.json({ delivery: "preparing", retryAfter: 2, requestId,
|
|
||||||
message: "正在准备原图,完成后将自动开始下载" }, {
|
|
||||||
status: 202, headers: { "Retry-After": "2", "Cache-Control": "no-store" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function prepareDownload(input: DownloadPreparationInput, signal: AbortSignal) {
|
async function prepareDownload(input: DownloadPreparationInput, signal: AbortSignal) {
|
||||||
// A proxy candidate for the same original must not repeat the same slow
|
// A proxy candidate for the same original must not repeat the same slow
|
||||||
@ -40,25 +21,22 @@ async function prepareDownload(input: DownloadPreparationInput, signal: AbortSig
|
|||||||
const source = extractImageProxySourceUrl(url) || url;
|
const source = extractImageProxySourceUrl(url) || url;
|
||||||
return inferOriginalImageUrl(source) || source;
|
return inferOriginalImageUrl(source) || source;
|
||||||
}))];
|
}))];
|
||||||
let lastError: unknown;
|
for (let attempt = 0; attempt < 3; attempt++) {
|
||||||
for (let attempt = 0; attempt < 2; attempt++) {
|
|
||||||
signal.throwIfAborted();
|
signal.throwIfAborted();
|
||||||
for (const url of sources) {
|
for (const url of sources) {
|
||||||
signal.throwIfAborted();
|
signal.throwIfAborted();
|
||||||
const result = await fetchCanvasDownloadAsset({
|
const result = await fetchCanvasDownloadAsset({
|
||||||
file: { url, name: "original" }, index: 0,
|
file: { url, name: "original" }, index: 0,
|
||||||
requestOrigin: input.requestOrigin, cookie: input.cookie,
|
requestOrigin: input.requestOrigin, cookie: input.cookie,
|
||||||
includeSequencePrefix: false, timeoutMs: 60_000, retryCount: 1, signal,
|
includeSequencePrefix: false, timeoutMs: 12_000, retryCount: 1, signal,
|
||||||
});
|
});
|
||||||
if (result.ok) return { buffer: result.bytes, contentType: result.contentType };
|
if (result.ok) return { buffer: result.bytes, contentType: result.contentType };
|
||||||
lastError = result.error;
|
|
||||||
}
|
}
|
||||||
// Each pass resolves the latest archive URL inside fetchCanvasDownloadAsset.
|
// Each pass resolves the latest archive URL inside fetchCanvasDownloadAsset.
|
||||||
// No AI/model request is involved and a completed source fetch is shared.
|
// No AI/model request is involved and a completed source fetch is shared.
|
||||||
if (attempt < 1 && getDownloadFailure(lastError).retryable) await new Promise(resolve => setTimeout(resolve, 1_000));
|
if (attempt < 2) await new Promise(resolve => setTimeout(resolve, 1_000));
|
||||||
else break;
|
|
||||||
}
|
}
|
||||||
throw new Error(String(lastError || "Original image sources unavailable"));
|
throw new Error("Original image sources unavailable");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRequestOrigin(request: NextRequest): string {
|
function getRequestOrigin(request: NextRequest): string {
|
||||||
@ -98,43 +76,31 @@ function resolveDownloadUrls(body: Record<string, unknown>, origin: string): str
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
const requestId = randomUUID();
|
|
||||||
try {
|
try {
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
return failed(getDownloadFailure("未登录"), requestId);
|
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = (await request.json().catch(() => ({}))) as Record<string, unknown>;
|
const body = (await request.json().catch(() => ({}))) as Record<string, unknown>;
|
||||||
const requestOrigin = getRequestOrigin(request);
|
const requestOrigin = getRequestOrigin(request);
|
||||||
const candidates = resolveDownloadUrls(body, requestOrigin);
|
const candidates = resolveDownloadUrls(body, requestOrigin);
|
||||||
if (candidates.length && candidates.every(isRetiredStorageUrl)) {
|
if (candidates.length && candidates.every(isRetiredStorageUrl)) {
|
||||||
return failed(getDownloadFailure(RETIRED_STORAGE_MESSAGE), requestId);
|
return NextResponse.json({ success: false, error: RETIRED_STORAGE_MESSAGE, code: "storage_retired" }, { status: 410 });
|
||||||
}
|
}
|
||||||
const urls = candidates.filter(url => !isRetiredStorageUrl(url));
|
const urls = candidates.filter(url => !isRetiredStorageUrl(url));
|
||||||
const requestedUrls = [...urls];
|
const requestedUrls = [...urls];
|
||||||
if (urls.length === 0) {
|
if (urls.length === 0) {
|
||||||
return failed(getDownloadFailure("图片地址无效"), requestId);
|
return NextResponse.json({ success: false, error: "图片地址无效" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const name = sanitizeCanvasDownloadFileName(String(body.name || ""), "canvas-image");
|
const name = sanitizeCanvasDownloadFileName(String(body.name || ""), "canvas-image");
|
||||||
if (body.preferDirect === true) {
|
if (body.preferDirect === true) {
|
||||||
const { task, direct, canPrepareDirect } = await resolveCanvasCdnDownload(urls, `${name}.png`).catch((error) => {
|
const { task, direct } = await resolveCanvasCdnDownload(urls, `${name}.png`).catch(() => ({ task: null, direct: null }));
|
||||||
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 (direct) return NextResponse.json(direct, { headers: { "Cache-Control": "no-store" } });
|
||||||
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);
|
|
||||||
}
|
|
||||||
if (task?.targetUrl) {
|
if (task?.targetUrl) {
|
||||||
if (!urls.includes(task.targetUrl)) urls.unshift(task.targetUrl);
|
if (!urls.includes(task.targetUrl)) urls.unshift(task.targetUrl);
|
||||||
scheduleImageDownloadPreparation(task.id);
|
after(() => prepareImageArchiveDownload(task.id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const cookie = request.headers.get("cookie") || "";
|
const cookie = request.headers.get("cookie") || "";
|
||||||
@ -143,10 +109,13 @@ export async function POST(request: NextRequest) {
|
|||||||
userId: session.user.id, urls: requestedUrls, requestOrigin, cookie,
|
userId: session.user.id, urls: requestedUrls, requestOrigin, cookie,
|
||||||
});
|
});
|
||||||
if (result.status === "preparing") {
|
if (result.status === "preparing") {
|
||||||
return preparing(result.requestId);
|
return NextResponse.json({ delivery: "preparing", retryAfter: 3,
|
||||||
|
message: "正在准备原图,完成后将自动下载" }, {
|
||||||
|
status: 202, headers: { "Retry-After": "3", "Cache-Control": "no-store" },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (result.status === "failed") {
|
if (result.status === "failed") {
|
||||||
return failed(result.failure, result.requestId);
|
return NextResponse.json({ success: false, error: result.error }, { status: 502 });
|
||||||
}
|
}
|
||||||
return new NextResponse(new Uint8Array(result.asset.buffer), { headers: {
|
return new NextResponse(new Uint8Array(result.asset.buffer), { headers: {
|
||||||
"Content-Type": result.asset.contentType,
|
"Content-Type": result.asset.contentType,
|
||||||
@ -167,14 +136,17 @@ export async function POST(request: NextRequest) {
|
|||||||
includeSequencePrefix: false,
|
includeSequencePrefix: false,
|
||||||
});
|
});
|
||||||
if (result.ok) break;
|
if (result.ok) break;
|
||||||
failures.push(result.error);
|
failures.push(`${url}: ${result.error}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result?.ok) {
|
if (!result?.ok) {
|
||||||
if (failures.length > 0) {
|
if (failures.length > 0) {
|
||||||
logger.warn("all_sources_failed", { requestId, failures });
|
console.warn("[CanvasDownloadImage] All candidates failed:", failures);
|
||||||
}
|
}
|
||||||
return failed(getDownloadFailure(result?.error), requestId);
|
return NextResponse.json(
|
||||||
|
{ success: false, error: result?.error || "图片源文件暂时不可访问" },
|
||||||
|
{ status: 502 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new NextResponse(result.bytes, {
|
return new NextResponse(result.bytes, {
|
||||||
@ -187,7 +159,7 @@ export async function POST(request: NextRequest) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("download_failed", error, { requestId });
|
console.error("[CanvasDownloadImage] Failed:", error);
|
||||||
return failed(getDownloadFailure(error), requestId);
|
return NextResponse.json({ success: false, error: "图片下载失败" }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,13 +10,11 @@ import JSZip from "jszip";
|
|||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { fetchCanvasDownloadAsset } from "@/lib/canvas/photoshop-safe-download";
|
import { fetchCanvasDownloadAsset } from "@/lib/canvas/photoshop-safe-download";
|
||||||
import { publishCanvasZip } from "@/lib/storage/canvas-zip-delivery";
|
import { publishCanvasZip } from "@/lib/storage/canvas-zip-delivery";
|
||||||
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
export const maxDuration = 300;
|
export const maxDuration = 300;
|
||||||
type DownloadFileItem = { url: string; name?: string };
|
type DownloadFileItem = { url: string; name?: string };
|
||||||
const MAX_DOWNLOAD_FILES = 200;
|
const MAX_DOWNLOAD_FILES = 200;
|
||||||
const TASK_TTL_MS = 24 * 60 * 60_000;
|
const TASK_TTL_MS = 60 * 60_000;
|
||||||
const PREPARATION_TTL_MS = 6 * 60 * 60_000;
|
|
||||||
const running = (globalThis as typeof globalThis & { __canvasZipRunning?: Set<string> });
|
const running = (globalThis as typeof globalThis & { __canvasZipRunning?: Set<string> });
|
||||||
const active = running.__canvasZipRunning ??= new Set<string>();
|
const active = running.__canvasZipRunning ??= new Set<string>();
|
||||||
function sanitizeFolderName(value: string): string {
|
function sanitizeFolderName(value: string): string {
|
||||||
@ -100,71 +98,63 @@ async function processTask(id: string, userId: string, requestOrigin: string, co
|
|||||||
task = await loadZipTask(id, userId);
|
task = await loadZipTask(id, userId);
|
||||||
if (!task || task.status === "completed" || task.status === "failed") return;
|
if (!task || task.status === "completed" || task.status === "failed") return;
|
||||||
await assertZipDiskSpace();
|
await assertZipDiskSpace();
|
||||||
const existingZip = task.zipFile && task.contentDisposition && await stat(task.zipFile.path).catch(() => null);
|
task.status = "running"; task.stage = "正在准备原图"; task.updatedAt = Date.now();
|
||||||
const reuseZip = existingZip && existingZip.size === task.zipFile!.bytes;
|
|
||||||
task.status = "running"; task.stage = reuseZip ? "正在准备加速下载" : "正在准备原图"; task.updatedAt = Date.now();
|
|
||||||
await saveZipTask(task);
|
await saveZipTask(task);
|
||||||
if (!reuseZip) {
|
// Stage each image on disk, not all PNG buffers in the web process heap.
|
||||||
delete task.zipFile; delete task.contentDisposition; delete task.cdnUrl;
|
// A restart resumes from these files using the current owner's session.
|
||||||
// Stage each image on disk, not all PNG buffers in the web process heap.
|
let cursor = 0;
|
||||||
// A restart resumes from these files using the current owner's session.
|
let writes = Promise.resolve();
|
||||||
let cursor = 0;
|
const persist = () => { const current = task!; writes = writes.then(() => saveZipTask(current)); return writes; };
|
||||||
let writes = Promise.resolve();
|
const results = await Promise.allSettled(Array.from({ length: Math.min(2, task.files.length) }, async () => {
|
||||||
const persist = () => { const current = task!; writes = writes.then(() => saveZipTask(current)); return writes; };
|
while (cursor < task!.files.length) {
|
||||||
const results = await Promise.allSettled(Array.from({ length: Math.min(2, task.files.length) }, async () => {
|
const index = cursor++; const file = task!.files[index];
|
||||||
while (cursor < task!.files.length) {
|
const path = join(zipTaskPath(id), `${index}.image`);
|
||||||
const index = cursor++; const file = task!.files[index];
|
if (file.prepared && file.filename && (await stat(path).catch(() => null))?.size) continue;
|
||||||
const path = join(zipTaskPath(id), `${index}.image`);
|
delete file.prepared; delete file.filename;
|
||||||
if (file.prepared && file.filename && (await stat(path).catch(() => null))?.size) continue;
|
const result = await fetchCanvasDownloadAsset({ file, index, requestOrigin, cookie,
|
||||||
delete file.prepared; delete file.filename;
|
signal: AbortSignal.timeout(120_000) }).catch(() => ({ ok: false as const, error: "原图读取超时或源地址暂时不可用" }));
|
||||||
const result = await fetchCanvasDownloadAsset({ file, index, requestOrigin, cookie,
|
if (result.ok) {
|
||||||
signal: AbortSignal.timeout(120_000) }).catch(() => ({ ok: false as const, error: "原图读取超时或源地址暂时不可用" }));
|
await assertZipDiskSpace(result.bytes.length);
|
||||||
if (result.ok) {
|
await writeFile(path, result.bytes, { mode: 0o600 });
|
||||||
await assertZipDiskSpace(result.bytes.length);
|
file.prepared = `${index}.image`; file.filename = result.filename; delete file.error;
|
||||||
await writeFile(path, result.bytes, { mode: 0o600 });
|
} else file.error = result.error;
|
||||||
file.prepared = `${index}.image`; file.filename = result.filename; delete file.error;
|
task!.processedCount = task!.files.filter(f => f.prepared || f.error).length;
|
||||||
} else file.error = result.error;
|
task!.successCount = task!.files.filter(f => f.prepared).length;
|
||||||
task!.processedCount = task!.files.filter(f => f.prepared || f.error).length;
|
task!.failedCount = task!.files.filter(f => f.error).length;
|
||||||
task!.successCount = task!.files.filter(f => f.prepared).length;
|
task!.stage = `正在准备原图 ${task!.processedCount}/${task!.totalCount}`;
|
||||||
task!.failedCount = task!.files.filter(f => f.error).length;
|
task!.updatedAt = Date.now(); await persist();
|
||||||
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;
|
||||||
const rejected = results.find(result => result.status === "rejected");
|
if (!task.successCount) throw new Error("所选原图暂时都无法读取,请稍后重试;画布图片不会被删除");
|
||||||
if (rejected?.status === "rejected") throw rejected.reason;
|
const zip = new JSZip(); const folder = zip.folder(task.folderName) || zip;
|
||||||
if (!task.successCount) throw new Error("所选原图暂时都无法读取,请稍后重试;画布图片不会被删除");
|
for (const file of task.files) if (file.prepared && file.filename)
|
||||||
const zip = new JSZip(); const folder = zip.folder(task.folderName) || zip;
|
folder.file(file.filename, createReadStream(join(zipTaskPath(id), file.prepared))
|
||||||
for (const file of task.files) if (file.prepared && file.filename)
|
.once("close", () => { void rm(join(zipTaskPath(id), file.prepared!), { force: true }).catch(() => undefined); }));
|
||||||
folder.file(file.filename, createReadStream(join(zipTaskPath(id), file.prepared))
|
if (task.failedCount) folder.file("_download-failures.txt", task.files.filter(f => f.error)
|
||||||
.once("close", () => { void rm(join(zipTaskPath(id), file.prepared!), { force: true }).catch(() => undefined); }));
|
.map(f => `${f.name || "图片"}:${f.error}`).join("\n"));
|
||||||
if (task.failedCount) folder.file("_download-failures.txt", task.files.filter(f => f.error)
|
task.stage = "正在生成完整 ZIP 文件"; await saveZipTask(task);
|
||||||
.map(f => `${f.name || "图片"}:${f.error}`).join("\n"));
|
task.zipFile = await writeDownloadZip(zip, task.id, percent => { task!.compressPercent = percent; });
|
||||||
task.stage = "正在生成完整 ZIP 文件"; await saveZipTask(task);
|
task.contentDisposition = buildContentDisposition(task.folderName);
|
||||||
task.zipFile = await writeDownloadZip(zip, task.id, percent => { task!.compressPercent = percent; });
|
|
||||||
task.contentDisposition = buildContentDisposition(task.folderName);
|
|
||||||
}
|
|
||||||
task.stage = "正在准备下载链接"; await saveZipTask(task);
|
task.stage = "正在准备下载链接"; await saveZipTask(task);
|
||||||
const cdn = await publishCanvasZip({ id: task.id, ...task.zipFile!, disposition: task.contentDisposition!,
|
try {
|
||||||
onProgress: async bytes => {
|
const cdn = await publishCanvasZip({ id: task.id, ...task.zipFile, disposition: task.contentDisposition });
|
||||||
task!.stage = `正在传送至七牛 ${Math.round(bytes / task!.zipFile!.bytes * 100)}%`;
|
if (cdn) {
|
||||||
task!.updatedAt = Date.now(); await saveZipTask(task!);
|
task.cdnObject = { storageConfigId: cdn.storageConfigId, key: cdn.key };
|
||||||
} });
|
if (cdn.url) task.cdnUrl = cdn.url;
|
||||||
if (cdn) {
|
}
|
||||||
task.cdnObject = { storageConfigId: cdn.storageConfigId, key: cdn.key };
|
} catch { console.warn("[CanvasDownloadZipTask] CDN delivery unavailable; complete local ZIP retained", { taskId: id }); }
|
||||||
if (!cdn.url) throw new Error(cdn.error || `ZIP 已打包,但加速下载未就绪,请再次点击下载继续传送原 ZIP。下载编号:${task.id}`);
|
|
||||||
task.cdnUrl = cdn.url;
|
|
||||||
}
|
|
||||||
task.status = "completed"; task.stage = task.failedCount ? `已打包 ${task.successCount} 张,${task.failedCount} 张未能读取` : "打包完成";
|
task.status = "completed"; task.stage = task.failedCount ? `已打包 ${task.successCount} 张,${task.failedCount} 张未能读取` : "打包完成";
|
||||||
task.compressPercent = 100; task.updatedAt = Date.now(); task.expiresAt = Date.now() + TASK_TTL_MS;
|
task.compressPercent = 100; task.updatedAt = Date.now(); task.expiresAt = Date.now() + TASK_TTL_MS;
|
||||||
await saveZipTask(task);
|
await saveZipTask(task);
|
||||||
// The complete ZIP is enough for retries; release staged duplicate bytes.
|
// The complete ZIP is enough for retries; release staged duplicate bytes.
|
||||||
await Promise.all(task.files.filter(f => f.prepared).map(f => rm(join(zipTaskPath(id), f.prepared!), { force: true }).catch(() => undefined)));
|
await Promise.all(task.files.filter(f => f.prepared).map(f => rm(join(zipTaskPath(id), f.prepared!), { force: true }).catch(() => undefined)));
|
||||||
console.info("[CanvasDownloadZipTask] Complete", { taskId: id, count: task.successCount, failed: task.failedCount,
|
console.info("[CanvasDownloadZipTask] Complete", { taskId: id, count: task.successCount, failed: task.failedCount,
|
||||||
bytes: task.zipFile!.bytes, delivery: task.cdnUrl ? "cdn" : "local" });
|
bytes: task.zipFile.bytes, delivery: task.cdnUrl ? "cdn" : "local" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (task) {
|
if (task) {
|
||||||
task.status = "failed"; task.stage = task.zipFile ? "ZIP 已打包,加速下载未就绪" : "打包未完成";
|
task.status = "failed"; task.stage = "打包未完成";
|
||||||
task.error = error instanceof Error ? error.message : "打包失败,请稍后重试";
|
task.error = error instanceof Error ? error.message : "打包失败,请稍后重试";
|
||||||
task.updatedAt = Date.now(); task.expiresAt = Date.now() + TASK_TTL_MS; await saveZipTask(task).catch(() => undefined);
|
task.updatedAt = Date.now(); task.expiresAt = Date.now() + TASK_TTL_MS; await saveZipTask(task).catch(() => undefined);
|
||||||
}
|
}
|
||||||
@ -172,7 +162,7 @@ async function processTask(id: string, userId: string, requestOrigin: string, co
|
|||||||
} finally { await release?.().catch(() => undefined); active.delete(id); }
|
} finally { await release?.().catch(() => undefined); active.delete(id); }
|
||||||
}
|
}
|
||||||
function schedule(task: ZipTask, request: NextRequest) {
|
function schedule(task: ZipTask, request: NextRequest) {
|
||||||
const run = () => trackRuntimeWork(() => processTask(task.id, task.userId, getRequestOrigin(request), request.headers.get("cookie") || ""));
|
const run = () => processTask(task.id, task.userId, getRequestOrigin(request), request.headers.get("cookie") || "");
|
||||||
after(run);
|
after(run);
|
||||||
}
|
}
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
@ -197,17 +187,14 @@ export async function POST(request: NextRequest) {
|
|||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
task = { id, userId: session.user.id, folderName: sanitizeFolderName(String(body.folderName || "")), files,
|
task = { id, userId: session.user.id, folderName: sanitizeFolderName(String(body.folderName || "")), files,
|
||||||
totalCount: files.length, processedCount: 0, successCount: 0, failedCount: 0, compressPercent: 0,
|
totalCount: files.length, processedCount: 0, successCount: 0, failedCount: 0, compressPercent: 0,
|
||||||
status: "pending", stage: "等待后台准备", createdAt: now, updatedAt: now, expiresAt: now + PREPARATION_TTL_MS };
|
status: "pending", stage: "等待后台准备", createdAt: now, updatedAt: now, expiresAt: now + 6 * TASK_TTL_MS };
|
||||||
await saveZipTask(task);
|
await saveZipTask(task);
|
||||||
} else if ((task.status === "failed" || task.status === "completed" && task.cdnObject && !task.cdnUrl) && body.retry === true) {
|
} else if (task.status === "failed" && body.retry === true) {
|
||||||
task.status = "pending"; task.error = undefined;
|
task.status = "pending"; task.error = undefined;
|
||||||
await assertZipTaskCapacity(session.user.id);
|
await assertZipTaskCapacity(session.user.id);
|
||||||
if (!task.zipFile) {
|
for (const file of task.files) delete file.error;
|
||||||
for (const file of task.files) delete file.error;
|
task.processedCount = task.successCount = task.files.filter(file => file.prepared).length;
|
||||||
task.processedCount = task.successCount = task.files.filter(file => file.prepared).length;
|
task.failedCount = 0; task.updatedAt = Date.now(); task.expiresAt = Date.now() + 6 * TASK_TTL_MS;
|
||||||
task.failedCount = 0;
|
|
||||||
}
|
|
||||||
task.updatedAt = Date.now(); task.expiresAt = Date.now() + PREPARATION_TTL_MS;
|
|
||||||
await saveZipTask(task);
|
await saveZipTask(task);
|
||||||
}
|
}
|
||||||
if (task.status === "pending" || task.status === "running") schedule(task, request);
|
if (task.status === "pending" || task.status === "running") schedule(task, request);
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { formatDownloadFailure, getDownloadFailure } from "@/lib/canvas/download-error";
|
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
|
||||||
import { buildImageProxyUrl, isAlibabaModelImageUrl } from "@/lib/image-delivery";
|
|
||||||
|
|
||||||
export const runtime = "nodejs";
|
export const runtime = "nodejs";
|
||||||
const logger = createLogger("image.download");
|
|
||||||
|
|
||||||
const DEFAULT_QUALITY = 82;
|
const DEFAULT_QUALITY = 82;
|
||||||
const MAX_WIDTH = 4096;
|
const MAX_WIDTH = 4096;
|
||||||
@ -243,7 +239,6 @@ function resolveDirectImageTarget(
|
|||||||
requestedQuality: number | null,
|
requestedQuality: number | null,
|
||||||
requestedFormat: string | null
|
requestedFormat: string | null
|
||||||
): URL | null {
|
): URL | null {
|
||||||
if (isAlibabaModelImageUrl(target.toString())) return null;
|
|
||||||
if (!isBrowserImageRequest(request)) {
|
if (!isBrowserImageRequest(request)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -272,17 +267,8 @@ function redirectToDirectImage(target: URL): NextResponse {
|
|||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const { searchParams } = request.nextUrl;
|
const { searchParams } = request.nextUrl;
|
||||||
const requestId = crypto.randomUUID();
|
|
||||||
const downloadFailure = (error: unknown) => {
|
|
||||||
const failure = getDownloadFailure(error);
|
|
||||||
logger.warn("original_download_failed", error, { requestId, code: failure.code, status: failure.status });
|
|
||||||
return NextResponse.json({ error: formatDownloadFailure(failure, requestId), failure, requestId }, {
|
|
||||||
status: failure.status, headers: { "Cache-Control": "no-store" },
|
|
||||||
});
|
|
||||||
};
|
|
||||||
const rawUrl = (searchParams.get("url") || "").trim();
|
const rawUrl = (searchParams.get("url") || "").trim();
|
||||||
if (isRetiredStorageUrl(rawUrl)) {
|
if (isRetiredStorageUrl(rawUrl)) {
|
||||||
if (searchParams.get("original") === "1") return downloadFailure(RETIRED_STORAGE_MESSAGE);
|
|
||||||
return NextResponse.json({ error: RETIRED_STORAGE_MESSAGE, code: "storage_retired" }, { status: 410 });
|
return NextResponse.json({ error: RETIRED_STORAGE_MESSAGE, code: "storage_retired" }, { status: 410 });
|
||||||
}
|
}
|
||||||
if (!rawUrl) {
|
if (!rawUrl) {
|
||||||
@ -300,24 +286,6 @@ export async function GET(request: NextRequest) {
|
|||||||
return NextResponse.json({ error: "Only http/https is supported" }, { status: 400 });
|
return NextResponse.json({ error: "Only http/https is supported" }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalizeLegacyAliyunStorageUrl(target.toString())) {
|
|
||||||
try {
|
|
||||||
if (searchParams.get("original") === "1") {
|
|
||||||
const { buildImageProxyUrl } = await import("@/lib/image-delivery");
|
|
||||||
buildImageProxyUrl(target.toString(), { delivery: "original" });
|
|
||||||
}
|
|
||||||
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
|
||||||
target = new URL(await resolveLegacyAliyunStorageUrl(target.toString()));
|
|
||||||
} catch (error) {
|
|
||||||
if (searchParams.get("original") === "1") return downloadFailure(error);
|
|
||||||
const failure = getDownloadFailure(error);
|
|
||||||
return NextResponse.json({ error: formatDownloadFailure(failure, requestId), failure, requestId,
|
|
||||||
code: failure.code === "STORAGE_RETIRED" ? "storage_retired" : failure.code }, {
|
|
||||||
status: failure.status, headers: { "Cache-Control": "no-store" },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isBlockedHost(target.hostname)) {
|
if (isBlockedHost(target.hostname)) {
|
||||||
return NextResponse.json({ error: "Blocked target host" }, { status: 403 });
|
return NextResponse.json({ error: "Blocked target host" }, { status: 403 });
|
||||||
}
|
}
|
||||||
@ -338,37 +306,6 @@ export async function GET(request: NextRequest) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!original && isAlibabaModelImageUrl(target.toString())) {
|
|
||||||
try {
|
|
||||||
const { getArchivedImageUrlMappings } = await import("@/lib/storage/image-archive-db");
|
|
||||||
const mappings = await getArchivedImageUrlMappings([target.toString()]);
|
|
||||||
const archived = mappings[target.toString()];
|
|
||||||
if (archived) {
|
|
||||||
const preview = buildImageProxyUrl(archived, {
|
|
||||||
width: width || undefined,
|
|
||||||
quality,
|
|
||||||
format: ["webp", "avif", "jpeg", "png"].includes(requestedFormat || "") ? requestedFormat as OutputFormat : undefined,
|
|
||||||
});
|
|
||||||
if (/^https?:\/\//i.test(preview)) return redirectToDirectImage(new URL(preview));
|
|
||||||
target = new URL(archived);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
logger.warn("preview_archive_lookup_failed", error, { requestId });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (original && searchParams.get("download") === "resolve") {
|
|
||||||
try {
|
|
||||||
const { buildImageProxyUrl } = await import("@/lib/image-delivery");
|
|
||||||
buildImageProxyUrl(target.toString(), { delivery: "original" });
|
|
||||||
const { resolveOriginalCdnDownload } = await import("@/lib/storage/original-image-download");
|
|
||||||
const direct = await resolveOriginalCdnDownload(target.toString(), searchParams.get("name") || "", request.signal);
|
|
||||||
return NextResponse.json(direct || { delivery: "proxy" }, { headers: { "Cache-Control": "no-store" } });
|
|
||||||
} catch (error) {
|
|
||||||
return downloadFailure(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!forceProxy && !original) {
|
if (!forceProxy && !original) {
|
||||||
const directImageTarget = resolveDirectImageTarget(
|
const directImageTarget = resolveDirectImageTarget(
|
||||||
request,
|
request,
|
||||||
@ -409,62 +346,45 @@ export async function GET(request: NextRequest) {
|
|||||||
|
|
||||||
const fetchSignal = AbortSignal.any([
|
const fetchSignal = AbortSignal.any([
|
||||||
request.signal,
|
request.signal,
|
||||||
AbortSignal.timeout(original ? 60_000 : isAlibabaModelImageUrl(target.toString()) || width && width <= 256 ? SMALL_PREVIEW_FETCH_TIMEOUT_MS : FETCH_TIMEOUT_MS),
|
AbortSignal.timeout(width && width <= 256 ? SMALL_PREVIEW_FETCH_TIMEOUT_MS : FETCH_TIMEOUT_MS),
|
||||||
]);
|
]);
|
||||||
const upstreamFailure = (error: unknown) => {
|
const upstreamFailure = (error: unknown) => {
|
||||||
if (request.signal.aborted) return new NextResponse(null, { status: 499 });
|
if (request.signal.aborted) return new NextResponse(null, { status: 499 });
|
||||||
if (original) return downloadFailure(error);
|
|
||||||
const timedOut = (error instanceof Error && error.name === "TimeoutError") || fetchSignal.reason?.name === "TimeoutError";
|
const timedOut = (error instanceof Error && error.name === "TimeoutError") || fetchSignal.reason?.name === "TimeoutError";
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: timedOut ? "读取源图片超时,请重试" : "源图片传输中断,请重试" },
|
{ error: timedOut ? "读取源图片超时,请重试" : "源图片传输中断,请重试" },
|
||||||
{ status: timedOut ? 504 : 502 },
|
{ status: timedOut ? 504 : 502 },
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
let contentType: string;
|
let upstreamResponse: Response;
|
||||||
let originalBuffer: Buffer;
|
try {
|
||||||
if (!original && isAlibabaModelImageUrl(target.toString())) {
|
upstreamResponse = await fetch(target.toString(), {
|
||||||
try {
|
signal: fetchSignal,
|
||||||
const { fetchImageBytes } = await import("@/lib/storage/image-byte-cache");
|
headers: {
|
||||||
const data = await fetchImageBytes(target.toString(), { timeoutMs: SMALL_PREVIEW_FETCH_TIMEOUT_MS });
|
Accept: original ? "*/*" : "image/avif,image/webp,image/apng,image/*,*/*;q=0.8",
|
||||||
originalBuffer = data.buffer;
|
},
|
||||||
contentType = data.contentType;
|
});
|
||||||
if (request.signal.aborted) return new NextResponse(null, { status: 499 });
|
} catch (error) {
|
||||||
if (!contentType.toLowerCase().startsWith("image/")) {
|
return upstreamFailure(error);
|
||||||
return NextResponse.json({ error: "Upstream is not an image" }, { status: 400 });
|
}
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
return upstreamFailure(error);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let upstreamResponse: Response;
|
|
||||||
try {
|
|
||||||
upstreamResponse = await fetch(target.toString(), {
|
|
||||||
signal: fetchSignal,
|
|
||||||
headers: {
|
|
||||||
Accept: original ? "*/*" : "image/avif,image/webp,image/apng,image/*,*/*;q=0.8",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
return upstreamFailure(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!upstreamResponse.ok) {
|
if (!upstreamResponse.ok) {
|
||||||
if (original) return downloadFailure(`HTTP ${upstreamResponse.status}`);
|
return NextResponse.json(
|
||||||
return NextResponse.json(
|
{ error: `Upstream responded with ${upstreamResponse.status}` },
|
||||||
{ error: `Upstream responded with ${upstreamResponse.status}` },
|
{ status: 502 }
|
||||||
{ status: 502 }
|
);
|
||||||
);
|
}
|
||||||
}
|
|
||||||
contentType = upstreamResponse.headers.get("content-type") || "application/octet-stream";
|
const contentType = upstreamResponse.headers.get("content-type") || "application/octet-stream";
|
||||||
if (!contentType.toLowerCase().startsWith("image/")) {
|
if (!contentType.toLowerCase().startsWith("image/")) {
|
||||||
if (original) return downloadFailure("Source is not an image");
|
return NextResponse.json({ error: "Upstream is not an image" }, { status: 400 });
|
||||||
return NextResponse.json({ error: "Upstream is not an image" }, { status: 400 });
|
}
|
||||||
}
|
|
||||||
try {
|
let originalBuffer: Buffer;
|
||||||
originalBuffer = Buffer.from(await upstreamResponse.arrayBuffer());
|
try {
|
||||||
} catch (error) {
|
originalBuffer = Buffer.from(await upstreamResponse.arrayBuffer());
|
||||||
return upstreamFailure(error);
|
} catch (error) {
|
||||||
}
|
return upstreamFailure(error);
|
||||||
}
|
}
|
||||||
if (original) {
|
if (original) {
|
||||||
return buildImageResponse(originalBuffer, {
|
return buildImageResponse(originalBuffer, {
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { preprocessProductImageWithRmbg } from "@/lib/scene-fusion/preprocess-product";
|
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { auth } from "@/app/(auth)/auth";
|
import { auth } from "@/app/(auth)/auth";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
@ -70,6 +69,7 @@ const SCENE_FUSION_REQUEST_STAGGER_MS = 350;
|
|||||||
const SCENE_FUSION_AGENT_TIMEOUT_MS = 20000;
|
const SCENE_FUSION_AGENT_TIMEOUT_MS = 20000;
|
||||||
const SCENE_FUSION_AGENT_MAX_WORKERS = 3;
|
const SCENE_FUSION_AGENT_MAX_WORKERS = 3;
|
||||||
const SCENE_FUSION_PROCESS_MAX_WORKERS = 4;
|
const SCENE_FUSION_PROCESS_MAX_WORKERS = 4;
|
||||||
|
const SCENE_FUSION_PRE_RMBG_TIMEOUT_MS = 60_000;
|
||||||
const SCENE_FUSION_ECHO_HASH_TIMEOUT_MS = 20_000;
|
const SCENE_FUSION_ECHO_HASH_TIMEOUT_MS = 20_000;
|
||||||
const SCENE_FUSION_ECHO_HASH_THRESHOLD = 8;
|
const SCENE_FUSION_ECHO_HASH_THRESHOLD = 8;
|
||||||
const SCENE_FUSION_REVIEW_TIMEOUT_MS = 25_000;
|
const SCENE_FUSION_REVIEW_TIMEOUT_MS = 25_000;
|
||||||
@ -236,6 +236,41 @@ function isSceneFusionReviewRejectError(message: string): boolean {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function buildPreRmbgCandidateUrls(request: Request): string[] {
|
||||||
|
const urls: string[] = [];
|
||||||
|
const requestUrl = new URL(request.url);
|
||||||
|
const requestHost = requestUrl.hostname.toLowerCase();
|
||||||
|
const requestProtocol = requestUrl.protocol.replace(":", "");
|
||||||
|
if (!(requestProtocol === "https" && (requestHost === "localhost" || requestHost === "127.0.0.1"))) {
|
||||||
|
urls.push(new URL("/api/rmbg", request.url).toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
const host = request.headers.get("host")?.trim();
|
||||||
|
const protoHint = request.headers
|
||||||
|
.get("x-forwarded-proto")
|
||||||
|
?.split(",")[0]
|
||||||
|
?.trim()
|
||||||
|
.toLowerCase();
|
||||||
|
const protocol = protoHint === "https" || protoHint === "http" ? protoHint : requestUrl.protocol.replace(":", "");
|
||||||
|
|
||||||
|
if (host) {
|
||||||
|
const normalizedHost = host.split(":")[0]?.toLowerCase() || "";
|
||||||
|
const isLocalHost = normalizedHost === "localhost" || normalizedHost === "127.0.0.1";
|
||||||
|
if (!(protocol === "https" && isLocalHost)) {
|
||||||
|
urls.push(`${protocol}://${host}/api/rmbg`);
|
||||||
|
}
|
||||||
|
if (isLocalHost) {
|
||||||
|
urls.push(`http://${host}/api/rmbg`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const localPort = requestUrl.port || process.env.PORT || "3125";
|
||||||
|
urls.push(`http://127.0.0.1:${localPort}/api/rmbg`);
|
||||||
|
urls.push(`http://localhost:${localPort}/api/rmbg`);
|
||||||
|
|
||||||
|
return Array.from(new Set(urls));
|
||||||
|
}
|
||||||
|
|
||||||
function normalizeUrlForCompare(input: string): string {
|
function normalizeUrlForCompare(input: string): string {
|
||||||
try {
|
try {
|
||||||
const parsed = new URL(input);
|
const parsed = new URL(input);
|
||||||
@ -953,6 +988,102 @@ async function getSceneFusionConfig() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function preprocessProductImageWithRmbg(
|
||||||
|
request: Request,
|
||||||
|
requestId: string,
|
||||||
|
productImageUrl: string,
|
||||||
|
preRmbgEnabled: boolean
|
||||||
|
): Promise<{
|
||||||
|
productImageUrl: string;
|
||||||
|
usedPreRmbg: boolean;
|
||||||
|
provider?: string;
|
||||||
|
reason?: string;
|
||||||
|
}> {
|
||||||
|
if (!preRmbgEnabled) {
|
||||||
|
return {
|
||||||
|
productImageUrl,
|
||||||
|
usedPreRmbg: false,
|
||||||
|
reason: "disabled_by_config",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const cookie = request.headers.get("cookie");
|
||||||
|
if (!cookie) {
|
||||||
|
return {
|
||||||
|
productImageUrl,
|
||||||
|
usedPreRmbg: false,
|
||||||
|
reason: "missing_cookie",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const candidateUrls = buildPreRmbgCandidateUrls(request);
|
||||||
|
let lastReason = "no_candidate_url";
|
||||||
|
|
||||||
|
logger.info("pre_rmbg_started", {
|
||||||
|
requestId,
|
||||||
|
candidateCount: candidateUrls.length,
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const rmbgUrl of candidateUrls) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), SCENE_FUSION_PRE_RMBG_TIMEOUT_MS);
|
||||||
|
try {
|
||||||
|
const response = await fetch(rmbgUrl, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
cookie,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
imageUrl: productImageUrl,
|
||||||
|
prompt: "场景裂变预处理-抠图",
|
||||||
|
skipGallerySave: true,
|
||||||
|
}),
|
||||||
|
signal: controller.signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
const payload = await response.json().catch(() => null) as Record<string, unknown> | null;
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
lastReason = `request_failed_${response.status}`;
|
||||||
|
logger.warn("pre_rmbg_candidate_failed", {
|
||||||
|
requestId,
|
||||||
|
status: response.status,
|
||||||
|
detail: payload?.detail || payload?.error || "unknown",
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cleanedUrl = String(payload?.original_url || payload?.thumbnail_url || "").trim();
|
||||||
|
if (!cleanedUrl || cleanedUrl === productImageUrl) {
|
||||||
|
lastReason = cleanedUrl ? "same_as_source" : "empty_result";
|
||||||
|
logger.warn("pre_rmbg_candidate_invalid", {
|
||||||
|
requestId,
|
||||||
|
reason: lastReason,
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
productImageUrl: cleanedUrl,
|
||||||
|
usedPreRmbg: true,
|
||||||
|
provider: String(payload?.provider || "unknown"),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
lastReason = error instanceof Error ? error.message : "exception";
|
||||||
|
logger.warn("pre_rmbg_candidate_exception", error, { requestId });
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
productImageUrl,
|
||||||
|
usedPreRmbg: false,
|
||||||
|
reason: lastReason,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建缩略图
|
* 创建缩略图
|
||||||
*/
|
*/
|
||||||
@ -2305,13 +2436,12 @@ export async function POST(request: Request) {
|
|||||||
productImageUrl = await uploadBase64ToS3(productImage, "scene-fusion-product");
|
productImageUrl = await uploadBase64ToS3(productImage, "scene-fusion-product");
|
||||||
}
|
}
|
||||||
const shouldForcePreRmbg = resolvedSceneMode === "random-bg";
|
const shouldForcePreRmbg = resolvedSceneMode === "random-bg";
|
||||||
const preprocessedProduct = await preprocessProductImageWithRmbg({
|
const preprocessedProduct = await preprocessProductImageWithRmbg(
|
||||||
request,
|
request,
|
||||||
requestId,
|
requestId,
|
||||||
userId: session.user.id,
|
|
||||||
productImageUrl,
|
productImageUrl,
|
||||||
preRmbgEnabled: shouldForcePreRmbg ? true : config.preRmbgEnabled,
|
shouldForcePreRmbg ? true : config.preRmbgEnabled
|
||||||
});
|
);
|
||||||
const fusionProductImageUrl = preprocessedProduct.productImageUrl;
|
const fusionProductImageUrl = preprocessedProduct.productImageUrl;
|
||||||
logger.info("pre_rmbg_resolved", {
|
logger.info("pre_rmbg_resolved", {
|
||||||
requestId,
|
requestId,
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { createCanvasSaveQueue } from "@/lib/client/canvas-save-queue";
|
import { createCanvasSaveQueue } from "@/lib/client/canvas-save-queue";
|
||||||
import { readCanvasDraft, writeCanvasDraft, clearCanvasDraft } from "@/lib/client/canvas-draft";
|
import { CanvasRemoteOperationContext } from "@/components/canvas/CanvasRemoteOperationContext";
|
||||||
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 {
|
import {
|
||||||
AlertCircle,
|
AlertCircle,
|
||||||
AlertTriangle,
|
AlertTriangle,
|
||||||
@ -172,7 +168,6 @@ import {
|
|||||||
type VideoCanvasSymbolMarkerId,
|
type VideoCanvasSymbolMarkerId,
|
||||||
} from "@/lib/canvas/video-canvas-markers";
|
} from "@/lib/canvas/video-canvas-markers";
|
||||||
import {
|
import {
|
||||||
isCanvasImageUploadPending,
|
|
||||||
LOCAL_UPLOAD_PENDING_METADATA_KEY,
|
LOCAL_UPLOAD_PENDING_METADATA_KEY,
|
||||||
LOCAL_UPLOAD_PREVIEW_METADATA_KEY,
|
LOCAL_UPLOAD_PREVIEW_METADATA_KEY,
|
||||||
} from "@/lib/canvas/local-upload-state";
|
} from "@/lib/canvas/local-upload-state";
|
||||||
@ -1934,12 +1929,6 @@ export default function CanvasEditorLegacyPage({
|
|||||||
() => !initialProjectForCurrentRoute
|
() => !initialProjectForCurrentRoute
|
||||||
);
|
);
|
||||||
const [isSaving, setIsSaving] = useState(false);
|
const [isSaving, setIsSaving] = useState(false);
|
||||||
const [canvasLoadError, setCanvasLoadError] = useState("");
|
|
||||||
const [canvasLoadAttempt, setCanvasLoadAttempt] = useState(0);
|
|
||||||
const canvasReadyRef = useRef(Boolean(initialProjectForCurrentRoute));
|
|
||||||
const savedCanvasSnapshotRef = useRef<{ id: string; snapshot: string } | null>(null);
|
|
||||||
const canvasSyncErrorRef = useRef("");
|
|
||||||
const [canvasSyncConflicts, setCanvasSyncConflicts] = useState<string[]>([]);
|
|
||||||
const [isEditingTitle, setIsEditingTitle] = useState(false);
|
const [isEditingTitle, setIsEditingTitle] = useState(false);
|
||||||
const [isAddImageDialogOpen, setIsAddImageDialogOpen] = useState(false);
|
const [isAddImageDialogOpen, setIsAddImageDialogOpen] = useState(false);
|
||||||
const [editTitle, setEditTitle] = useState("");
|
const [editTitle, setEditTitle] = useState("");
|
||||||
@ -3376,32 +3365,6 @@ export default function CanvasEditorLegacyPage({
|
|||||||
return () => window.clearTimeout(timeoutId);
|
return () => window.clearTimeout(timeoutId);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const loadPersistedCanvasState = useCallback((input: CanvasState | null | undefined) => {
|
|
||||||
const canvasData = input || { nodes: [], edges: [], selectedNodeIds: [], viewport: { x: 0, y: 0, zoom: 1 }, tool: "select" as const };
|
|
||||||
const base = JSON.stringify({ canvasData });
|
|
||||||
savedCanvasSnapshotRef.current = { id, snapshot: base };
|
|
||||||
externalCanvasConflictRef.current = false;
|
|
||||||
setCanvasSyncConflicts([]);
|
|
||||||
const draft = readCanvasDraft(id);
|
|
||||||
if (draft) {
|
|
||||||
const merged = mergeCanvasSnapshots(draft.base, draft.snapshot, base);
|
|
||||||
if (merged.conflicts.length) {
|
|
||||||
externalCanvasConflictRef.current = true;
|
|
||||||
savedCanvasSnapshotRef.current = { id, snapshot: draft.base };
|
|
||||||
setCanvasSyncConflicts(merged.conflicts);
|
|
||||||
canvasSyncErrorRef.current = "画布同步发生冲突,改动已保留。请在画布上方选择保留本页或云端的冲突内容,再继续操作。";
|
|
||||||
useCanvasStore.getState().loadCanvas(JSON.parse(merged.snapshot).canvasData);
|
|
||||||
toast.warning(canvasSyncErrorRef.current);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
useCanvasStore.getState().loadCanvas(JSON.parse(merged.snapshot).canvasData);
|
|
||||||
if (merged.snapshot !== base) writeCanvasDraft(id, { base, snapshot: merged.snapshot });
|
|
||||||
else clearCanvasDraft(id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
useCanvasStore.getState().loadCanvas(canvasData);
|
|
||||||
}, [id]);
|
|
||||||
|
|
||||||
const hydrateCanvasProject = useCallback(
|
const hydrateCanvasProject = useCallback(
|
||||||
(project: CanvasInitialProject) => {
|
(project: CanvasInitialProject) => {
|
||||||
externalCanvasConflictRef.current = false;
|
externalCanvasConflictRef.current = false;
|
||||||
@ -3411,9 +3374,9 @@ export default function CanvasEditorLegacyPage({
|
|||||||
useCanvasStore
|
useCanvasStore
|
||||||
.getState()
|
.getState()
|
||||||
.setCanvasRevision(projectUpdatedAt, projectUpdatedAt);
|
.setCanvasRevision(projectUpdatedAt, projectUpdatedAt);
|
||||||
loadPersistedCanvasState(project.canvasData);
|
|
||||||
|
|
||||||
if (project.canvasData) {
|
if (project.canvasData) {
|
||||||
|
useCanvasStore.getState().loadCanvas(project.canvasData);
|
||||||
const persistedWorkflow = project.canvasData.workflowState;
|
const persistedWorkflow = project.canvasData.workflowState;
|
||||||
if (!requestedAgentMode && persistedWorkflow) {
|
if (!requestedAgentMode && persistedWorkflow) {
|
||||||
const nextAppearanceRecognitionEnabled =
|
const nextAppearanceRecognitionEnabled =
|
||||||
@ -3516,7 +3479,7 @@ export default function CanvasEditorLegacyPage({
|
|||||||
|
|
||||||
setWorkflowHydrationSource("empty");
|
setWorkflowHydrationSource("empty");
|
||||||
},
|
},
|
||||||
[id, requestedAgentMode, loadPersistedCanvasState]
|
[id, requestedAgentMode]
|
||||||
);
|
);
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
@ -3524,7 +3487,6 @@ export default function CanvasEditorLegacyPage({
|
|||||||
if (lastLoadedCanvasIdRef.current === id) return;
|
if (lastLoadedCanvasIdRef.current === id) return;
|
||||||
|
|
||||||
hydrateCanvasProject(initialProjectForCurrentRoute);
|
hydrateCanvasProject(initialProjectForCurrentRoute);
|
||||||
canvasReadyRef.current = true;
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
lastLoadedCanvasIdRef.current = id;
|
lastLoadedCanvasIdRef.current = id;
|
||||||
}, [hydrateCanvasProject, id, initialProjectForCurrentRoute]);
|
}, [hydrateCanvasProject, id, initialProjectForCurrentRoute]);
|
||||||
@ -3532,7 +3494,6 @@ export default function CanvasEditorLegacyPage({
|
|||||||
// 加载画布数据
|
// 加载画布数据
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("[Canvas] Loading project:", id);
|
console.log("[Canvas] Loading project:", id);
|
||||||
let cancelled = false;
|
|
||||||
|
|
||||||
const loadProject = async () => {
|
const loadProject = async () => {
|
||||||
if (lastLoadedCanvasIdRef.current === id) {
|
if (lastLoadedCanvasIdRef.current === id) {
|
||||||
@ -3541,8 +3502,6 @@ export default function CanvasEditorLegacyPage({
|
|||||||
}
|
}
|
||||||
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
canvasReadyRef.current = false;
|
|
||||||
setCanvasLoadError("");
|
|
||||||
if (!requestedAgentMode) {
|
if (!requestedAgentMode) {
|
||||||
setWorkflowHydrationSource("pending");
|
setWorkflowHydrationSource("pending");
|
||||||
}
|
}
|
||||||
@ -3575,16 +3534,18 @@ export default function CanvasEditorLegacyPage({
|
|||||||
setLoadedAgentProjectId(null);
|
setLoadedAgentProjectId(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 🆕 先清空当前画布状态,确保切换时不会显示旧数据
|
||||||
|
useCanvasStore.getState().clearCanvas();
|
||||||
|
|
||||||
const result = await canvasService.getProject(id);
|
const result = await canvasService.getProject(id);
|
||||||
if (cancelled) return;
|
|
||||||
|
|
||||||
if (result.success && result.data) {
|
if (result.success && result.data) {
|
||||||
useCanvasStore.getState().setProject(id, result.data.title);
|
useCanvasStore.getState().setProject(id, result.data.title);
|
||||||
useCanvasStore
|
useCanvasStore
|
||||||
.getState()
|
.getState()
|
||||||
.setCanvasRevision(String(result.data.updatedAt || ""), String(result.data.updatedAt || ""));
|
.setCanvasRevision(String(result.data.updatedAt || ""), String(result.data.updatedAt || ""));
|
||||||
loadPersistedCanvasState(result.data.canvasData);
|
|
||||||
if (result.data.canvasData) {
|
if (result.data.canvasData) {
|
||||||
|
useCanvasStore.getState().loadCanvas(result.data.canvasData);
|
||||||
const persistedWorkflow = result.data.canvasData.workflowState;
|
const persistedWorkflow = result.data.canvasData.workflowState;
|
||||||
if (!requestedAgentMode && persistedWorkflow) {
|
if (!requestedAgentMode && persistedWorkflow) {
|
||||||
const nextAppearanceRecognitionEnabled =
|
const nextAppearanceRecognitionEnabled =
|
||||||
@ -3701,15 +3662,8 @@ export default function CanvasEditorLegacyPage({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.status !== 404) {
|
// 画布不存在时,创建一个新画布
|
||||||
setCanvasLoadError(result.error || "画布暂时无法读取,请检查网络后重试加载。现有内容未被覆盖。");
|
|
||||||
setIsLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only an explicit missing project can create a new empty canvas.
|
|
||||||
console.log("[Canvas] 画布不存在,创建新画布:", id);
|
console.log("[Canvas] 画布不存在,创建新画布:", id);
|
||||||
useCanvasStore.getState().clearCanvas();
|
|
||||||
useCanvasStore
|
useCanvasStore
|
||||||
.getState()
|
.getState()
|
||||||
.setProject(
|
.setProject(
|
||||||
@ -3736,20 +3690,15 @@ export default function CanvasEditorLegacyPage({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (cancelled) return;
|
|
||||||
canvasReadyRef.current = true;
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
lastLoadedCanvasIdRef.current = id;
|
lastLoadedCanvasIdRef.current = id;
|
||||||
};
|
};
|
||||||
|
|
||||||
loadProject();
|
loadProject();
|
||||||
return () => { cancelled = true; };
|
|
||||||
}, [
|
}, [
|
||||||
activeCanvasWorkspaceType,
|
activeCanvasWorkspaceType,
|
||||||
defaultCanvasTitle,
|
defaultCanvasTitle,
|
||||||
id,
|
id,
|
||||||
canvasLoadAttempt,
|
|
||||||
loadPersistedCanvasState,
|
|
||||||
openLoginDialog,
|
openLoginDialog,
|
||||||
requestedAgentMode,
|
requestedAgentMode,
|
||||||
]);
|
]);
|
||||||
@ -5122,69 +5071,15 @@ export default function CanvasEditorLegacyPage({
|
|||||||
const canvasAutosaveTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
const canvasAutosaveTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
const canvasPersistenceScope = useRef(id);
|
const canvasPersistenceScope = useRef(id);
|
||||||
const canvasRemoteOperationActive = useRef(false);
|
const canvasRemoteOperationActive = useRef(false);
|
||||||
const canvasLeaveApprovedRef = useRef(false);
|
|
||||||
const [canvasLeaveUrl, setCanvasLeaveUrl] = useState("");
|
|
||||||
const [isSavingBeforeLeave, setIsSavingBeforeLeave] = useState(false);
|
|
||||||
const [canvasLeaveError, setCanvasLeaveError] = useState("");
|
|
||||||
canvasPersistenceScope.current = id;
|
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 });
|
|
||||||
const merged = mergeCanvasSnapshots(
|
|
||||||
savedCanvasSnapshotRef.current?.id === id ? savedCanvasSnapshotRef.current.snapshot : remoteSnapshot,
|
|
||||||
JSON.stringify({ canvasData: buildPersistedCanvasData() }),
|
|
||||||
remoteSnapshot,
|
|
||||||
);
|
|
||||||
if (merged.changed) useCanvasStore.getState().loadCanvas(JSON.parse(merged.snapshot).canvasData);
|
|
||||||
useCanvasStore.getState().setCanvasRevision(result.updatedAt, result.updatedAt);
|
|
||||||
if (merged.conflicts.length) {
|
|
||||||
canvasSyncErrorRef.current = "画布同步发生冲突,改动已保留。请在画布上方选择保留本页或云端的冲突内容,再继续操作。";
|
|
||||||
if (!externalCanvasConflictRef.current) toast.warning(canvasSyncErrorRef.current);
|
|
||||||
externalCanvasConflictRef.current = true;
|
|
||||||
setCanvasSyncConflicts(merged.conflicts);
|
|
||||||
writeCanvasDraft(id, { base: savedCanvasSnapshotRef.current?.snapshot || remoteSnapshot, snapshot: merged.snapshot });
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
savedCanvasSnapshotRef.current = { id, snapshot: remoteSnapshot };
|
|
||||||
externalCanvasConflictRef.current = false;
|
|
||||||
setCanvasSyncConflicts([]);
|
|
||||||
canvasSyncErrorRef.current = "";
|
|
||||||
return true;
|
|
||||||
}, [buildPersistedCanvasData, id]);
|
|
||||||
const persistCanvasStateNow = useCallback(
|
const persistCanvasStateNow = useCallback(
|
||||||
async (reason: "autosave" | "delete" | "beforeunload") => {
|
async (reason: "autosave" | "delete" | "beforeunload") => {
|
||||||
if (canvasLeaveApprovedRef.current || canvasPersistenceScope.current !== id || !canvasReadyRef.current || useCanvasStore.getState().projectId !== id) return false;
|
if (canvasPersistenceScope.current !== id) return false;
|
||||||
if (externalCanvasConflictRef.current) return false;
|
|
||||||
setIsSaving(true);
|
setIsSaving(true);
|
||||||
try {
|
try {
|
||||||
let canvasData = buildPersistedCanvasData();
|
const canvasData = buildPersistedCanvasData();
|
||||||
let snapshot = JSON.stringify({ canvasData });
|
|
||||||
if (savedCanvasSnapshotRef.current?.id === id && equal(JSON.parse(savedCanvasSnapshotRef.current.snapshot), JSON.parse(snapshot))) {
|
|
||||||
clearCanvasDraft(id);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const { canvasRevision, canvasUpdatedAt } = useCanvasStore.getState();
|
const { canvasRevision, canvasUpdatedAt } = useCanvasStore.getState();
|
||||||
let result = await canvasService.saveCanvasState(
|
const result = await canvasService.saveCanvasState(
|
||||||
id,
|
id,
|
||||||
canvasData,
|
canvasData,
|
||||||
activeCanvasWorkspaceType,
|
activeCanvasWorkspaceType,
|
||||||
@ -5194,53 +5089,23 @@ export default function CanvasEditorLegacyPage({
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (canvasPersistenceScope.current !== id) return false;
|
if (canvasPersistenceScope.current !== id) return false;
|
||||||
if (result.status === 409 && savedCanvasSnapshotRef.current?.id === id) {
|
if (!result.success) {
|
||||||
const remote = await canvasService.getProject(id);
|
console.error(`[Canvas] ${reason} save failed:`, result.error);
|
||||||
if (canvasPersistenceScope.current !== id) return false;
|
if (result.status === 409) {
|
||||||
if (remote.success && remote.data?.canvasData) {
|
externalCanvasConflictRef.current = true;
|
||||||
const remoteSnapshot = JSON.stringify({ canvasData: remote.data.canvasData });
|
console.warn("[Canvas] stale save revision handled without user prompt", {
|
||||||
const merged = mergeCanvasSnapshots(
|
reason,
|
||||||
savedCanvasSnapshotRef.current.snapshot,
|
canvasUpdatedAt: result.canvasUpdatedAt,
|
||||||
JSON.stringify({ canvasData: buildPersistedCanvasData() }),
|
});
|
||||||
remoteSnapshot,
|
if (!externalCanvasConflictNotifiedRef.current) {
|
||||||
);
|
externalCanvasConflictNotifiedRef.current = true;
|
||||||
if (!merged.conflicts.length) {
|
toast.warning("画布已被外部操作更新,本地改动未覆盖远端;请刷新后继续编辑");
|
||||||
canvasData = JSON.parse(merged.snapshot).canvasData;
|
|
||||||
snapshot = merged.snapshot;
|
|
||||||
savedCanvasSnapshotRef.current = { id, snapshot: remoteSnapshot };
|
|
||||||
useCanvasStore.getState().loadCanvas(canvasData);
|
|
||||||
const revision = String(remote.data.updatedAt);
|
|
||||||
useCanvasStore.getState().setCanvasRevision(revision, revision);
|
|
||||||
result = await canvasService.saveCanvasState(id, canvasData, activeCanvasWorkspaceType, {
|
|
||||||
baseCanvasRevision: revision, baseCanvasUpdatedAt: revision,
|
|
||||||
});
|
|
||||||
if (canvasPersistenceScope.current !== id) return false;
|
|
||||||
} else {
|
|
||||||
externalCanvasConflictRef.current = true;
|
|
||||||
setCanvasSyncConflicts(merged.conflicts);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if (result.data?.updatedAt) {
|
||||||
if (!result.success) {
|
|
||||||
const retained = writeCanvasDraft(id, {
|
|
||||||
base: savedCanvasSnapshotRef.current?.id === id ? savedCanvasSnapshotRef.current.snapshot : JSON.stringify({ canvasData: { nodes: [], edges: [] } }),
|
|
||||||
snapshot: JSON.stringify({ canvasData: buildPersistedCanvasData() }),
|
|
||||||
});
|
|
||||||
canvasSyncErrorRef.current = result.status === 409
|
|
||||||
? "画布同步发生冲突,改动已保留。请在画布上方选择保留本页或云端的冲突内容,再继续操作。"
|
|
||||||
: `画布自动同步失败${result.status ? `(HTTP ${result.status})` : "(网络连接失败)"}。${retained ? "本地草稿已保留,系统会继续自动同步,也可稍后重试。" : "请保留当前页面,检查网络后重试同步,暂勿刷新。"}`;
|
|
||||||
console.error(`[Canvas] ${reason} save failed:`, result.error);
|
|
||||||
} else {
|
|
||||||
canvasSyncErrorRef.current = "";
|
|
||||||
savedCanvasSnapshotRef.current = { id, snapshot };
|
|
||||||
clearCanvasDraft(id);
|
|
||||||
externalCanvasConflictRef.current = false;
|
externalCanvasConflictRef.current = false;
|
||||||
setCanvasSyncConflicts([]);
|
|
||||||
externalCanvasConflictNotifiedRef.current = false;
|
externalCanvasConflictNotifiedRef.current = false;
|
||||||
if (result.data?.canvasData && result.data?.updatedAt) {
|
useCanvasStore
|
||||||
return applyCanvasRemoteSnapshot({ projectId: id, canvasData: result.data.canvasData, updatedAt: String(result.data.updatedAt) });
|
|
||||||
}
|
|
||||||
if (result.data?.updatedAt) useCanvasStore
|
|
||||||
.getState()
|
.getState()
|
||||||
.setCanvasRevision(String(result.data.updatedAt), String(result.data.updatedAt));
|
.setCanvasRevision(String(result.data.updatedAt), String(result.data.updatedAt));
|
||||||
}
|
}
|
||||||
@ -5249,7 +5114,7 @@ export default function CanvasEditorLegacyPage({
|
|||||||
if (canvasPersistenceScope.current === id) setIsSaving(false);
|
if (canvasPersistenceScope.current === id) setIsSaving(false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[activeCanvasWorkspaceType, applyCanvasRemoteSnapshot, buildPersistedCanvasData, id]
|
[activeCanvasWorkspaceType, buildPersistedCanvasData, id]
|
||||||
);
|
);
|
||||||
|
|
||||||
const persistCanvasState = useCallback(
|
const persistCanvasState = useCallback(
|
||||||
@ -5258,139 +5123,35 @@ export default function CanvasEditorLegacyPage({
|
|||||||
[persistCanvasStateNow]
|
[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);
|
|
||||||
try {
|
|
||||||
const remote = await canvasService.getProject(id);
|
|
||||||
if (canvasPersistenceScope.current !== id || useCanvasStore.getState().projectId !== id) return;
|
|
||||||
if (!remote.success || !remote.data?.canvasData || savedCanvasSnapshotRef.current?.id !== id) {
|
|
||||||
throw new Error(remote.error || "画布暂时无法读取,改动已保留,请稍后重试同步。");
|
|
||||||
}
|
|
||||||
const remoteSnapshot = JSON.stringify({ canvasData: remote.data.canvasData });
|
|
||||||
const merged = mergeCanvasSnapshots(
|
|
||||||
savedCanvasSnapshotRef.current.snapshot,
|
|
||||||
JSON.stringify({ canvasData: buildPersistedCanvasData() }),
|
|
||||||
remoteSnapshot,
|
|
||||||
choice,
|
|
||||||
);
|
|
||||||
savedCanvasSnapshotRef.current = { id, snapshot: remoteSnapshot };
|
|
||||||
useCanvasStore.getState().loadCanvas(JSON.parse(merged.snapshot).canvasData);
|
|
||||||
const revision = String(remote.data.updatedAt);
|
|
||||||
useCanvasStore.getState().setCanvasRevision(revision, revision);
|
|
||||||
externalCanvasConflictRef.current = false;
|
|
||||||
canvasSyncErrorRef.current = "";
|
|
||||||
setCanvasSyncConflicts([]);
|
|
||||||
if (!(await persistCanvasStateNow("autosave"))) throw new Error(canvasSyncErrorRef.current);
|
|
||||||
toast.success("画布已合并同步,可以继续操作");
|
|
||||||
} catch (error) {
|
|
||||||
toast.error(error instanceof Error ? error.message : "画布同步失败,改动已保留,请稍后重试。");
|
|
||||||
} finally {
|
|
||||||
if (canvasPersistenceScope.current === id) setIsSaving(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, [buildPersistedCanvasData, id, persistCanvasStateNow]);
|
|
||||||
|
|
||||||
const runCanvasRemoteOperation = useCallback(
|
const runCanvasRemoteOperation = useCallback(
|
||||||
(operation: CanvasRemoteOperation) => {
|
(operation: () => Promise<void>) => {
|
||||||
if (canvasAutosaveTimer.current) {
|
if (canvasAutosaveTimer.current) {
|
||||||
clearTimeout(canvasAutosaveTimer.current);
|
clearTimeout(canvasAutosaveTimer.current);
|
||||||
canvasAutosaveTimer.current = null;
|
canvasAutosaveTimer.current = null;
|
||||||
}
|
}
|
||||||
return canvasSaveQueue.current!.enqueueOperation(
|
return canvasSaveQueue.current!.enqueueOperation(
|
||||||
async () => {
|
() => persistCanvasStateNow("autosave"),
|
||||||
const saved = await persistCanvasStateNow("autosave");
|
|
||||||
if (!saved) throw new Error(canvasSyncErrorRef.current ? `${canvasSyncErrorRef.current} 本次处理未提交。` : "画布尚未完成加载,本次处理未提交,请等待加载完成后重试。");
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
async () => {
|
async () => {
|
||||||
canvasRemoteOperationActive.current = true;
|
canvasRemoteOperationActive.current = true;
|
||||||
try {
|
try {
|
||||||
const result = await operation();
|
await operation();
|
||||||
if (result && result.projectId === id && canvasPersistenceScope.current === id) {
|
|
||||||
savedCanvasSnapshotRef.current = { id, snapshot: JSON.stringify({ canvasData: result.canvasData }) };
|
|
||||||
canvasSyncErrorRef.current = "";
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
canvasRemoteOperationActive.current = false;
|
canvasRemoteOperationActive.current = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
[persistCanvasStateNow, id]
|
[persistCanvasStateNow]
|
||||||
);
|
);
|
||||||
|
|
||||||
const persistCanvasBeforeAgent = useCallback(async () => {
|
const persistCanvasBeforeAgent = useCallback(() => {
|
||||||
if (!canvasReadyRef.current || useCanvasStore.getState().projectId !== id || externalCanvasConflictRef.current)
|
|
||||||
throw new Error(canvasSyncErrorRef.current ? `${canvasSyncErrorRef.current} 本次任务未提交。` : "画布尚未完成同步,本次任务未提交,请保留当前页面并重试同步。");
|
|
||||||
if (savedCanvasSnapshotRef.current?.id === id) {
|
|
||||||
if (!hasUnsavedCanvasChanges()) return true;
|
|
||||||
}
|
|
||||||
// Flush the pending local edit once instead of racing its scheduled autosave.
|
// Flush the pending local edit once instead of racing its scheduled autosave.
|
||||||
if (canvasAutosaveTimer.current) {
|
if (canvasAutosaveTimer.current) {
|
||||||
clearTimeout(canvasAutosaveTimer.current);
|
clearTimeout(canvasAutosaveTimer.current);
|
||||||
canvasAutosaveTimer.current = null;
|
canvasAutosaveTimer.current = null;
|
||||||
}
|
}
|
||||||
if (!(await persistCanvasState("autosave"))) throw new Error(canvasSyncErrorRef.current ? `${canvasSyncErrorRef.current} 本次任务未提交。` : "画布尚未完成加载,本次任务未提交;请等待加载完成后再次发送。");
|
return persistCanvasState("autosave");
|
||||||
return true;
|
}, [persistCanvasState]);
|
||||||
}, [persistCanvasState, hasUnsavedCanvasChanges, id]);
|
|
||||||
|
|
||||||
const canvasRemoteOperationContext = useMemo(() => ({
|
|
||||||
run: runCanvasRemoteOperation,
|
|
||||||
applySnapshot: applyCanvasRemoteSnapshot,
|
|
||||||
}), [runCanvasRemoteOperation, applyCanvasRemoteSnapshot]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
request.save(async () => {
|
|
||||||
if (isAgentMode) {
|
|
||||||
if (agentProjectId && hasLoadedCurrentAgentProject && !(await persistAgentProject("switch"))) throw new Error("生图项目同步失败,请保留此页并重试刷新。");
|
|
||||||
} else if (canvasReadyRef.current && !(await persistCanvasState("autosave"))) {
|
|
||||||
throw new Error(canvasSyncErrorRef.current || "画布尚未完成同步,请保留此页并重试刷新。");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
window.addEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
|
||||||
return () => window.removeEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
|
||||||
}, [isVideoMode, isAgentMode, agentProjectId, hasLoadedCurrentAgentProject, persistAgentProject, persistCanvasState]);
|
|
||||||
|
|
||||||
// 自动保存逻辑
|
// 自动保存逻辑
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -5411,32 +5172,7 @@ export default function CanvasEditorLegacyPage({
|
|||||||
clearTimeout(saveTimer);
|
clearTimeout(saveTimer);
|
||||||
if (canvasAutosaveTimer.current === saveTimer) canvasAutosaveTimer.current = null;
|
if (canvasAutosaveTimer.current === saveTimer) canvasAutosaveTimer.current = null;
|
||||||
};
|
};
|
||||||
}, [isAgentMode, isAgentOperationRefreshActive, nodes, edges, isLoading, persistCanvasState]);
|
}, [isAgentMode, isAgentOperationRefreshActive, nodes, 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]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isLoading || isAgentOperationRefreshActive()) return;
|
if (isLoading || isAgentOperationRefreshActive()) return;
|
||||||
@ -5460,7 +5196,6 @@ export default function CanvasEditorLegacyPage({
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const beforeRead = useCanvasStore.getState();
|
|
||||||
const revisionResponse = await fetch(`/api/canvas/${id}/revision`, {
|
const revisionResponse = await fetch(`/api/canvas/${id}/revision`, {
|
||||||
cache: "no-store",
|
cache: "no-store",
|
||||||
});
|
});
|
||||||
@ -5481,11 +5216,7 @@ export default function CanvasEditorLegacyPage({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = await canvasService.getProject(id);
|
const result = await canvasService.getProject(id);
|
||||||
const current = useCanvasStore.getState();
|
if (cancelled || canvasRemoteOperationActive.current || !result.success || !result.data) return;
|
||||||
if (
|
|
||||||
cancelled || canvasRemoteOperationActive.current || !result.success || !result.data ||
|
|
||||||
current.projectId !== id || current.nodes !== beforeRead.nodes || current.edges !== beforeRead.edges
|
|
||||||
) return;
|
|
||||||
useCanvasStore.getState().markAgentOperationRefresh();
|
useCanvasStore.getState().markAgentOperationRefresh();
|
||||||
hydrateCanvasProject(result.data);
|
hydrateCanvasProject(result.data);
|
||||||
lastLocalCanvasMutationAtRef.current = Date.now();
|
lastLocalCanvasMutationAtRef.current = Date.now();
|
||||||
@ -5539,25 +5270,14 @@ export default function CanvasEditorLegacyPage({
|
|||||||
}
|
}
|
||||||
}, [isAgentMode, isAgentOperationRefreshActive, nodes, isLoading, persistCanvasState]);
|
}, [isAgentMode, isAgentOperationRefreshActive, nodes, isLoading, persistCanvasState]);
|
||||||
|
|
||||||
// 浏览器离开确认只保护尚未同步的内容;真正离开时保留草稿并发送一次保存。
|
// 页面关闭/刷新前尝试做一次 keepalive 保存,降低“刚删就刷新”回滚概率
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isLoading || isAgentMode) return;
|
if (isLoading || isAgentMode) return;
|
||||||
|
|
||||||
const retainDraft = () => {
|
const handleBeforeUnload = () => {
|
||||||
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 = () => {
|
|
||||||
try {
|
try {
|
||||||
const canvasData = retainDraft();
|
if (isAgentOperationRefreshActive()) return;
|
||||||
if (!canvasData || isSaving || canvasRemoteOperationActive.current || isAgentOperationRefreshActive() || externalCanvasConflictRef.current) return;
|
const canvasData = buildPersistedCanvasData();
|
||||||
const { canvasRevision, canvasUpdatedAt } = useCanvasStore.getState();
|
const { canvasRevision, canvasUpdatedAt } = useCanvasStore.getState();
|
||||||
void fetch(`/api/canvas/${id}`, {
|
void fetch(`/api/canvas/${id}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
@ -5576,20 +5296,18 @@ export default function CanvasEditorLegacyPage({
|
|||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener("beforeunload", handleBeforeUnload);
|
window.addEventListener("beforeunload", handleBeforeUnload);
|
||||||
window.addEventListener("pagehide", handlePageHide);
|
window.addEventListener("pagehide", handleBeforeUnload);
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("beforeunload", handleBeforeUnload);
|
window.removeEventListener("beforeunload", handleBeforeUnload);
|
||||||
window.removeEventListener("pagehide", handlePageHide);
|
window.removeEventListener("pagehide", handleBeforeUnload);
|
||||||
};
|
};
|
||||||
}, [
|
}, [
|
||||||
activeCanvasWorkspaceType,
|
activeCanvasWorkspaceType,
|
||||||
buildPersistedCanvasData,
|
buildPersistedCanvasData,
|
||||||
hasUnsavedCanvasChanges,
|
|
||||||
id,
|
id,
|
||||||
isAgentOperationRefreshActive,
|
isAgentOperationRefreshActive,
|
||||||
isAgentMode,
|
isAgentMode,
|
||||||
isLoading,
|
isLoading,
|
||||||
isSaving,
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 智能命名与缩略图逻辑
|
// 智能命名与缩略图逻辑
|
||||||
@ -5673,12 +5391,21 @@ export default function CanvasEditorLegacyPage({
|
|||||||
|
|
||||||
// 保存画布 (手动触发保留作为备用,但UI上移除)
|
// 保存画布 (手动触发保留作为备用,但UI上移除)
|
||||||
const handleSave = useCallback(async () => {
|
const handleSave = useCallback(async () => {
|
||||||
if (await persistCanvasState("autosave")) {
|
setIsSaving(true);
|
||||||
|
const canvasData = buildPersistedCanvasData();
|
||||||
|
const result = await canvasService.saveCanvasState(
|
||||||
|
id,
|
||||||
|
canvasData,
|
||||||
|
activeCanvasWorkspaceType
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
toast.success("保存成功");
|
toast.success("保存成功");
|
||||||
} else {
|
} else {
|
||||||
toast.error(canvasSyncErrorRef.current || "画布同步失败,请检查网络后重试");
|
toast.error(result.error || "保存失败");
|
||||||
}
|
}
|
||||||
}, [persistCanvasState]);
|
setIsSaving(false);
|
||||||
|
}, [activeCanvasWorkspaceType, buildPersistedCanvasData, id]);
|
||||||
|
|
||||||
// 更新标题
|
// 更新标题
|
||||||
const handleUpdateTitle = async () => {
|
const handleUpdateTitle = async () => {
|
||||||
@ -10472,14 +10199,6 @@ export default function CanvasEditorLegacyPage({
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (canvasLoadError && !isAgentMode) {
|
|
||||||
return <div role="alert" className="flex h-dvh flex-col items-center justify-center gap-4">
|
|
||||||
<p>{canvasLoadError}</p>
|
|
||||||
<Button onClick={() => setCanvasLoadAttempt(value => value + 1)}>重新加载画布</Button>
|
|
||||||
{loginDialogElement}
|
|
||||||
</div>;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isAgentMode) {
|
if (isAgentMode) {
|
||||||
if (!hasActiveAgentProject) {
|
if (!hasActiveAgentProject) {
|
||||||
return (
|
return (
|
||||||
@ -10583,7 +10302,7 @@ export default function CanvasEditorLegacyPage({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CanvasRemoteOperationContext.Provider value={canvasRemoteOperationContext}>
|
<CanvasRemoteOperationContext.Provider value={runCanvasRemoteOperation}>
|
||||||
<TooltipProvider>
|
<TooltipProvider>
|
||||||
<div
|
<div
|
||||||
data-canvas-panel-open={isPanelOpen ? "true" : "false"}
|
data-canvas-panel-open={isPanelOpen ? "true" : "false"}
|
||||||
@ -10599,30 +10318,6 @@ export default function CanvasEditorLegacyPage({
|
|||||||
}
|
}
|
||||||
>
|
>
|
||||||
<CanvasAgentUiListener projectId={id} workspace="canvas" />
|
<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">
|
|
||||||
<p className="font-medium">画布同步发生冲突</p>
|
|
||||||
<p className="mt-1 text-muted-foreground">本页和云端的同一内容有不同修改。请选择保留哪边的冲突内容,其余图片和修改会合并保留。</p>
|
|
||||||
</div>
|
|
||||||
<Button disabled={isSaving} onClick={() => void resolveCanvasSyncConflict("local")}>保留本页修改</Button>
|
|
||||||
<Button disabled={isSaving} variant="outline" onClick={() => void resolveCanvasSyncConflict("remote")}>保留云端修改</Button>
|
|
||||||
</div>
|
|
||||||
) : null}
|
|
||||||
{/* 1. Canvas Layer (Full Screen) - 使用 key 强制在 id 变化时重新挂载 */}
|
{/* 1. Canvas Layer (Full Screen) - 使用 key 强制在 id 变化时重新挂载 */}
|
||||||
<div
|
<div
|
||||||
className="absolute inset-0 z-0"
|
className="absolute inset-0 z-0"
|
||||||
@ -11332,12 +11027,14 @@ export default function CanvasEditorLegacyPage({
|
|||||||
|
|
||||||
{isVideoMode ? brandEntry : (
|
{isVideoMode ? brandEntry : (
|
||||||
<CanvasSurfaceHeader
|
<CanvasSurfaceHeader
|
||||||
|
isSaving={isSaving}
|
||||||
onAddImage={handleAddImageNode}
|
onAddImage={handleAddImageNode}
|
||||||
onHome={() => requestCanvasLeave("/")}
|
onHome={goHome}
|
||||||
onOpenFiles={() => {
|
onOpenFiles={() => {
|
||||||
setIsPanelOpen(true);
|
setIsPanelOpen(true);
|
||||||
setPanelMode("fileList");
|
setPanelMode("fileList");
|
||||||
}}
|
}}
|
||||||
|
onSave={() => void handleSave()}
|
||||||
projectTitle={projectTitle}
|
projectTitle={projectTitle}
|
||||||
rightInset={panelOcclusionRight}
|
rightInset={panelOcclusionRight}
|
||||||
/>
|
/>
|
||||||
@ -11442,7 +11139,6 @@ export default function CanvasEditorLegacyPage({
|
|||||||
createUrl={createNewCanvasUrl}
|
createUrl={createNewCanvasUrl}
|
||||||
currentId={id}
|
currentId={id}
|
||||||
onClose={() => setPanelMode("chat")}
|
onClose={() => setPanelMode("chat")}
|
||||||
onNavigate={requestCanvasLeave}
|
|
||||||
workspaceLabel={workspaceLabel}
|
workspaceLabel={workspaceLabel}
|
||||||
workspaceType={activeCanvasWorkspaceType}
|
workspaceType={activeCanvasWorkspaceType}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -48,13 +48,8 @@ export default function CanvasStyleProPage() {
|
|||||||
const canvasData = result.data.canvasData;
|
const canvasData = result.data.canvasData;
|
||||||
const latestState = useCanvasStore.getState();
|
const latestState = useCanvasStore.getState();
|
||||||
setProject(result.data.id, result.data.title);
|
setProject(result.data.id, result.data.title);
|
||||||
if (
|
if (latestState.nodes.length === 0 && latestState.edges.length === 0) {
|
||||||
latestState.projectId !== id ||
|
|
||||||
(latestState.nodes.length === 0 && latestState.edges.length === 0)
|
|
||||||
) {
|
|
||||||
loadCanvas(canvasData);
|
loadCanvas(canvasData);
|
||||||
const revision = String(result.data.updatedAt || "");
|
|
||||||
useCanvasStore.getState().setCanvasRevision(revision, revision);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { Toaster } from "sonner";
|
|||||||
import { ThemeProvider } from "@/components/theme-provider";
|
import { ThemeProvider } from "@/components/theme-provider";
|
||||||
import { FaviconUpdater } from "@/components/favicon-updater";
|
import { FaviconUpdater } from "@/components/favicon-updater";
|
||||||
import { ImageDeliveryOptimizer } from "@/components/image-delivery-optimizer";
|
import { ImageDeliveryOptimizer } from "@/components/image-delivery-optimizer";
|
||||||
import { AppUpdateNotice } from "@/components/app-update-notice";
|
|
||||||
import { getSiteSettings } from "@/lib/db/queries";
|
import { getSiteSettings } from "@/lib/db/queries";
|
||||||
|
|
||||||
import "./globals.css";
|
import "./globals.css";
|
||||||
@ -216,7 +215,6 @@ export default function RootLayout({
|
|||||||
enableSystem
|
enableSystem
|
||||||
>
|
>
|
||||||
<Toaster position="top-center" />
|
<Toaster position="top-center" />
|
||||||
<AppUpdateNotice />
|
|
||||||
<SessionProvider>{children}</SessionProvider>
|
<SessionProvider>{children}</SessionProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
|
||||||
|
|
||||||
import { useChat } from "@ai-sdk/react";
|
import { useChat } from "@ai-sdk/react";
|
||||||
import { DefaultChatTransport } from "ai";
|
import { DefaultChatTransport } from "ai";
|
||||||
@ -576,11 +575,31 @@ export function AgentGenerationWorkspace({
|
|||||||
toast.error("图片地址无效");
|
toast.error("图片地址无效");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const filename = buildDownloadFilename(raw);
|
||||||
try {
|
try {
|
||||||
await downloadOriginalImage({ url: raw, name: buildDownloadFilename(raw) });
|
const response = await fetch(raw);
|
||||||
toast.success("原图已开始下载");
|
if (!response.ok) {
|
||||||
} catch (error) {
|
throw new Error(`download failed: ${response.status}`);
|
||||||
toast.error(getCanvasImageDownloadErrorMessage(error));
|
}
|
||||||
|
const blob = await response.blob();
|
||||||
|
const blobUrl = window.URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = blobUrl;
|
||||||
|
link.download = filename;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
window.URL.revokeObjectURL(blobUrl);
|
||||||
|
} catch {
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = raw;
|
||||||
|
link.target = "_blank";
|
||||||
|
link.rel = "noopener noreferrer";
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
toast.message("已打开原图,可右键另存为");
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
|
||||||
|
|
||||||
import { useChat } from "@ai-sdk/react";
|
import { useChat } from "@ai-sdk/react";
|
||||||
import { BEFORE_PAGE_REFRESH, type PageRefreshRequest } from "@/lib/client/page-refresh";
|
|
||||||
import { appendGenerationMessage, mergeGenerationHistory, parseAppendedGenerationMessage, recoverReplacementRun } from "@/lib/agents/replacement-run-recovery";
|
import { appendGenerationMessage, mergeGenerationHistory, parseAppendedGenerationMessage, recoverReplacementRun } from "@/lib/agents/replacement-run-recovery";
|
||||||
import { getSceneRunResult } from "@/lib/agents/scene-run-recovery";
|
import { getSceneRunResult } from "@/lib/agents/scene-run-recovery";
|
||||||
import type { DetailReplacementBatchCard } from "@/lib/agents/detail-replacement-batch";
|
import type { DetailReplacementBatchCard } from "@/lib/agents/detail-replacement-batch";
|
||||||
@ -4880,14 +4878,6 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
return recoverReplacementRun({ messages, ...pendingGeneration });
|
return recoverReplacementRun({ messages, ...pendingGeneration });
|
||||||
}, [messages, pendingGeneration]);
|
}, [messages, pendingGeneration]);
|
||||||
const transportIsGenerating = status === "streaming" || status === "submitted";
|
const transportIsGenerating = status === "streaming" || status === "submitted";
|
||||||
useEffect(() => {
|
|
||||||
const beforeRefresh = (event: Event) => {
|
|
||||||
if (transportIsGenerating || pendingGeneration)
|
|
||||||
(event as CustomEvent<PageRefreshRequest>).detail.block("当前生图任务仍在处理中,请等待结果返回后再刷新。原任务无需重新提交。");
|
|
||||||
};
|
|
||||||
window.addEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
|
||||||
return () => window.removeEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
|
||||||
}, [transportIsGenerating, pendingGeneration]);
|
|
||||||
const pendingGenerationProgressCount = useMemo(() => {
|
const pendingGenerationProgressCount = useMemo(() => {
|
||||||
if (!pendingGeneration) {
|
if (!pendingGeneration) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -6803,11 +6793,31 @@ export function AgentGenerationWorkspaceDarkV2({
|
|||||||
toast.error("图片地址无效");
|
toast.error("图片地址无效");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const filename = buildDownloadFilename(raw);
|
||||||
try {
|
try {
|
||||||
await downloadOriginalImage({ url: raw, name: buildDownloadFilename(raw) });
|
const response = await fetch(raw);
|
||||||
toast.success("原图已开始下载");
|
if (!response.ok) {
|
||||||
} catch (error) {
|
throw new Error(`download failed: ${response.status}`);
|
||||||
toast.error(getCanvasImageDownloadErrorMessage(error));
|
}
|
||||||
|
const blob = await response.blob();
|
||||||
|
const blobUrl = window.URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = blobUrl;
|
||||||
|
link.download = filename;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
window.URL.revokeObjectURL(blobUrl);
|
||||||
|
} catch {
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = raw;
|
||||||
|
link.target = "_blank";
|
||||||
|
link.rel = "noopener noreferrer";
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
toast.message("已打开原图,可右键另存为");
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
const uploadBase64ResultToStorage = useCallback(async (base64Url: string) => {
|
const uploadBase64ResultToStorage = useCallback(async (base64Url: string) => {
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
|
||||||
|
|
||||||
import { useChat } from "@ai-sdk/react";
|
import { useChat } from "@ai-sdk/react";
|
||||||
import { DefaultChatTransport } from "ai";
|
import { DefaultChatTransport } from "ai";
|
||||||
@ -726,11 +725,30 @@ export function AgentGenerationWorkspaceNew({
|
|||||||
toast.error("图片地址无效");
|
toast.error("图片地址无效");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const filename = buildDownloadFilename(raw);
|
||||||
try {
|
try {
|
||||||
await downloadOriginalImage({ url: raw, name: buildDownloadFilename(raw) });
|
const response = await fetch(raw);
|
||||||
toast.success("原图已开始下载");
|
if (!response.ok) {
|
||||||
} catch (error) {
|
throw new Error(`download failed: ${response.status}`);
|
||||||
toast.error(getCanvasImageDownloadErrorMessage(error));
|
}
|
||||||
|
const blob = await response.blob();
|
||||||
|
const blobUrl = window.URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = blobUrl;
|
||||||
|
link.download = filename;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
window.URL.revokeObjectURL(blobUrl);
|
||||||
|
} catch {
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = raw;
|
||||||
|
link.target = "_blank";
|
||||||
|
link.rel = "noopener noreferrer";
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
toast.message("已打开原图,可右键另存为");
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@ -1,76 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { Button } from "@/components/ui/button";
|
|
||||||
import { AlertDialog, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription, AlertDialogFooter } from "@/components/ui/alert-dialog";
|
|
||||||
import { preparePageRefresh } from "@/lib/client/page-refresh";
|
|
||||||
|
|
||||||
export function AppUpdateNotice() {
|
|
||||||
const [availableVersion, setAvailableVersion] = useState("");
|
|
||||||
const [refreshing, setRefreshing] = useState(false);
|
|
||||||
const [error, setError] = useState("");
|
|
||||||
useEffect(() => {
|
|
||||||
let disposed = false;
|
|
||||||
let controller: AbortController | undefined;
|
|
||||||
let checking = false;
|
|
||||||
const check = async () => {
|
|
||||||
if (disposed || checking || document.visibilityState !== "visible") return;
|
|
||||||
checking = true;
|
|
||||||
controller = new AbortController();
|
|
||||||
const current = controller;
|
|
||||||
const timeout = window.setTimeout(() => current.abort(), 8000);
|
|
||||||
try {
|
|
||||||
const response = await fetch("/api/app-version", { cache: "no-store", signal: current.signal });
|
|
||||||
if (!response.ok) return;
|
|
||||||
const data = await response.json();
|
|
||||||
if (!disposed && !current.signal.aborted && typeof data.version === "string" && data.version)
|
|
||||||
setAvailableVersion(data.version !== (process.env.NEXT_PUBLIC_APP_RELEASE || "development") ? data.version : "");
|
|
||||||
} catch {
|
|
||||||
// A failed version read leaves the current workspace available.
|
|
||||||
} finally {
|
|
||||||
window.clearTimeout(timeout);
|
|
||||||
checking = false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
void check();
|
|
||||||
const interval = window.setInterval(check, 60_000);
|
|
||||||
window.addEventListener("focus", check);
|
|
||||||
window.addEventListener("online", check);
|
|
||||||
document.addEventListener("visibilitychange", check);
|
|
||||||
return () => {
|
|
||||||
disposed = true;
|
|
||||||
controller?.abort();
|
|
||||||
clearInterval(interval);
|
|
||||||
window.removeEventListener("focus", check);
|
|
||||||
window.removeEventListener("online", check);
|
|
||||||
document.removeEventListener("visibilitychange", check);
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const refresh = async () => {
|
|
||||||
if (refreshing) return;
|
|
||||||
setRefreshing(true);
|
|
||||||
setError("");
|
|
||||||
try {
|
|
||||||
await preparePageRefresh();
|
|
||||||
window.location.reload();
|
|
||||||
setRefreshing(false);
|
|
||||||
} catch (cause) {
|
|
||||||
setError(cause instanceof Error ? cause.message : "当前内容同步失败,请保留此页并重试。");
|
|
||||||
setRefreshing(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return <AlertDialog open={Boolean(availableVersion)}>
|
|
||||||
<AlertDialogContent className="z-[10001]" overlayClassName="z-[10000]" onEscapeKeyDown={event => event.preventDefault()}>
|
|
||||||
<AlertDialogHeader>
|
|
||||||
<AlertDialogTitle>平台已更新</AlertDialogTitle>
|
|
||||||
<AlertDialogDescription>需要刷新页面后继续使用。点击后会先同步当前画布并保留输入;正在处理的任务完成后再刷新。</AlertDialogDescription>
|
|
||||||
</AlertDialogHeader>
|
|
||||||
{error ? <p role="alert" className="text-sm text-destructive">{error}</p> : null}
|
|
||||||
<AlertDialogFooter>
|
|
||||||
{error ? <Button variant="outline" disabled={refreshing} onClick={() => { setAvailableVersion(""); setError(""); }}>返回处理</Button> : null}
|
|
||||||
<Button disabled={refreshing} onClick={() => void refresh()}>{refreshing ? "正在同步内容…" : "同步并刷新"}</Button>
|
|
||||||
</AlertDialogFooter>
|
|
||||||
</AlertDialogContent>
|
|
||||||
</AlertDialog>;
|
|
||||||
}
|
|
||||||
@ -2,13 +2,9 @@
|
|||||||
|
|
||||||
import { advancePastAtomicRanges, scanMarkdownImageStreaming } from "@/lib/canvas/markdown-image-streaming";
|
import { advancePastAtomicRanges, scanMarkdownImageStreaming } from "@/lib/canvas/markdown-image-streaming";
|
||||||
import { useChat } from "@ai-sdk/react";
|
import { useChat } from "@ai-sdk/react";
|
||||||
import { useStickToBottom } from "use-stick-to-bottom";
|
|
||||||
import { DefaultChatTransport } from "ai";
|
import { DefaultChatTransport } from "ai";
|
||||||
import { shouldUseCanvasAgentRuntime } from "@/lib/ai/canvas-agent-runtime/routing";
|
import { shouldUseCanvasAgentRuntime } from "@/lib/ai/canvas-agent-runtime/routing";
|
||||||
import { CanvasAgentProgressMessage } from "@/components/canvas/chat-panel/canvas-agent-progress";
|
import { CanvasAgentProgressMessage } from "@/components/canvas/chat-panel/canvas-agent-progress";
|
||||||
import { useCanvasRemoteOperation, useCanvasRemoteSnapshot } from "@/components/canvas/CanvasRemoteOperationContext";
|
|
||||||
import { readCanvasComposerDraft, writeCanvasComposerDraft, clearCanvasComposerDraft } from "@/lib/client/canvas-draft";
|
|
||||||
import { BEFORE_PAGE_REFRESH, type PageRefreshRequest } from "@/lib/client/page-refresh";
|
|
||||||
import { canvasHistoryForChat } from "@/lib/client/canvas-history-response";
|
import { canvasHistoryForChat } from "@/lib/client/canvas-history-response";
|
||||||
import { useCanvasAgentRun } from "@/components/canvas/chat-panel/use-canvas-agent-run";
|
import { useCanvasAgentRun } from "@/components/canvas/chat-panel/use-canvas-agent-run";
|
||||||
import { canvasAgentProgress, type CanvasAgentProgress } from "@/lib/ai/canvas-agent-runtime/progress";
|
import { canvasAgentProgress, type CanvasAgentProgress } from "@/lib/ai/canvas-agent-runtime/progress";
|
||||||
@ -71,7 +67,6 @@ import { toast } from "sonner";
|
|||||||
import { useCanvasStore } from "@/store/canvas";
|
import { useCanvasStore } from "@/store/canvas";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { getGatewayErrorPageMessage } from "@/lib/request-error";
|
import { getGatewayErrorPageMessage } from "@/lib/request-error";
|
||||||
import { findImageGenerationFailure } from "@/lib/canvas/image-generation-status";
|
|
||||||
import { fetcher, fetchWithErrorHandlers, generateUUID } from "@/lib/utils";
|
import { fetcher, fetchWithErrorHandlers, generateUUID } from "@/lib/utils";
|
||||||
import {
|
import {
|
||||||
fetchUploadWithRetry,
|
fetchUploadWithRetry,
|
||||||
@ -86,8 +81,7 @@ import { canvasService } from "@/services/canvas";
|
|||||||
import {
|
import {
|
||||||
inferOriginalImageUrl,
|
inferOriginalImageUrl,
|
||||||
} from "@/lib/image-url";
|
} from "@/lib/image-url";
|
||||||
import { buildImageFallbackUrl, buildImageProxyUrl } from "@/lib/image-delivery";
|
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
import { ProgressiveImagePreview, type LoadedImagePreview } from "@/components/progressive-image-preview";
|
|
||||||
import {
|
import {
|
||||||
isImageGenerationModelDescriptor,
|
isImageGenerationModelDescriptor,
|
||||||
isVideoModelDescriptor,
|
isVideoModelDescriptor,
|
||||||
@ -3247,8 +3241,6 @@ export function CanvasChatPanel({
|
|||||||
onRequireLogin,
|
onRequireLogin,
|
||||||
onBeforeAgentRequest,
|
onBeforeAgentRequest,
|
||||||
}: CanvasChatPanelProps) {
|
}: CanvasChatPanelProps) {
|
||||||
const runCanvasRemoteOperation = useCanvasRemoteOperation();
|
|
||||||
const applyCanvasRemoteSnapshot = useCanvasRemoteSnapshot();
|
|
||||||
const isWorkspacePresentation = presentation === "workspace";
|
const isWorkspacePresentation = presentation === "workspace";
|
||||||
const isVideoWorkspace = workspaceVariant === "video";
|
const isVideoWorkspace = workspaceVariant === "video";
|
||||||
const propCanvasToolbarConfig = useMemo(
|
const propCanvasToolbarConfig = useMemo(
|
||||||
@ -3347,7 +3339,6 @@ export function CanvasChatPanel({
|
|||||||
const activeChatIdRef = useRef<string>(requestedChatSessionId || chatId);
|
const activeChatIdRef = useRef<string>(requestedChatSessionId || chatId);
|
||||||
const activeMessagesRef = useRef<ChatMessage[]>([]);
|
const activeMessagesRef = useRef<ChatMessage[]>([]);
|
||||||
const inFlightChatIdRef = useRef<string | null>(null);
|
const inFlightChatIdRef = useRef<string | null>(null);
|
||||||
const imageGenerationUserMessageIdsRef = useRef<Record<string, string>>({});
|
|
||||||
const inFlightRawChatIdRef = useRef<string | null>(null);
|
const inFlightRawChatIdRef = useRef<string | null>(null);
|
||||||
const [chatSessions, setChatSessions] = useState<CanvasChatSession[]>([]);
|
const [chatSessions, setChatSessions] = useState<CanvasChatSession[]>([]);
|
||||||
const [isSessionMenuOpen, setIsSessionMenuOpen] = useState(false);
|
const [isSessionMenuOpen, setIsSessionMenuOpen] = useState(false);
|
||||||
@ -3369,15 +3360,15 @@ export function CanvasChatPanel({
|
|||||||
const latestCanvasSkillTagsRef = useRef<CanvasSkillTag[]>([]);
|
const latestCanvasSkillTagsRef = useRef<CanvasSkillTag[]>([]);
|
||||||
const pendingCanvasSkillTagOrderSaveRef = useRef<string[] | null>(null);
|
const pendingCanvasSkillTagOrderSaveRef = useRef<string[] | null>(null);
|
||||||
const suppressSkillTagClickRef = useRef(false);
|
const suppressSkillTagClickRef = useRef(false);
|
||||||
const { scrollRef: messagesScrollRef, contentRef: messagesContentRef, scrollToBottom: scrollMessagesToBottom, isAtBottom: isMessageListAtBottom } = useStickToBottom({ initial: false, resize: "instant" });
|
const [messageViewportKey, setMessageViewportKey] = useState(0);
|
||||||
const initialScrollChatRef = useRef<string | null>(null);
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
const clawdFolderInputRef = useRef<HTMLInputElement | null>(null);
|
const clawdFolderInputRef = useRef<HTMLInputElement | null>(null);
|
||||||
const videoSlotInputRefs = useRef<Array<HTMLInputElement | null>>([]);
|
const videoSlotInputRefs = useRef<Array<HTMLInputElement | null>>([]);
|
||||||
const agentSlotInputRefs = useRef<Array<HTMLInputElement | null>>([]);
|
const agentSlotInputRefs = useRef<Array<HTMLInputElement | null>>([]);
|
||||||
const detailSlotInputRefs = useRef<Array<HTMLInputElement | null>>([]);
|
const detailSlotInputRefs = useRef<Array<HTMLInputElement | null>>([]);
|
||||||
const [clawdTableFiles, setClawdTableFiles] = useState<File[]>([]);
|
const [clawdTableFiles, setClawdTableFiles] = useState<File[]>([]);
|
||||||
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
|
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||||
const detailModeSwitchRef = useRef(false);
|
const detailModeSwitchRef = useRef(false);
|
||||||
const hiddenPromptRef = useRef<string | null>(null);
|
const hiddenPromptRef = useRef<string | null>(null);
|
||||||
const autoContextRef = useRef<string | null>(null);
|
const autoContextRef = useRef<string | null>(null);
|
||||||
@ -3406,6 +3397,7 @@ export function CanvasChatPanel({
|
|||||||
const hasInitializedVideoWorkspaceRef = useRef(false);
|
const hasInitializedVideoWorkspaceRef = useRef(false);
|
||||||
const sessionMenuRef = useRef<HTMLDivElement>(null);
|
const sessionMenuRef = useRef<HTMLDivElement>(null);
|
||||||
const clawdHistoryMenuRef = useRef<HTMLDivElement>(null);
|
const clawdHistoryMenuRef = useRef<HTMLDivElement>(null);
|
||||||
|
const historyScrollTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
const forceHistoryHydrationRef = useRef<string | null>(null);
|
const forceHistoryHydrationRef = useRef<string | null>(null);
|
||||||
const copiedUserMessageRef = useRef<CopiedUserMessagePayload | null>(null);
|
const copiedUserMessageRef = useRef<CopiedUserMessagePayload | null>(null);
|
||||||
const composerUserEditedAtRef = useRef(0);
|
const composerUserEditedAtRef = useRef(0);
|
||||||
@ -3442,7 +3434,6 @@ export function CanvasChatPanel({
|
|||||||
const [fullscreenPreview, setFullscreenPreview] = useState<{
|
const [fullscreenPreview, setFullscreenPreview] = useState<{
|
||||||
url: string;
|
url: string;
|
||||||
alt?: string;
|
alt?: string;
|
||||||
preview: LoadedImagePreview;
|
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
const [downloadProgress, setDownloadProgress] =
|
const [downloadProgress, setDownloadProgress] =
|
||||||
useState<CanvasImageDownloadProgress | null>(null);
|
useState<CanvasImageDownloadProgress | null>(null);
|
||||||
@ -3480,6 +3471,16 @@ export function CanvasChatPanel({
|
|||||||
const [downloadingOriginalUrl, setDownloadingOriginalUrl] = useState("");
|
const [downloadingOriginalUrl, setDownloadingOriginalUrl] = useState("");
|
||||||
const [addingClawdImageUrl, setAddingClawdImageUrl] = useState("");
|
const [addingClawdImageUrl, setAddingClawdImageUrl] = useState("");
|
||||||
const downloadAbortControllerRef = useRef<AbortController | null>(null);
|
const downloadAbortControllerRef = useRef<AbortController | null>(null);
|
||||||
|
const fullscreenPreviewSrc = useMemo(() => {
|
||||||
|
if (!fullscreenPreview?.url) return "";
|
||||||
|
const originalUrl = inferOriginalImageUrl(fullscreenPreview.url);
|
||||||
|
return buildImageProxyUrl(originalUrl || fullscreenPreview.url, {
|
||||||
|
delivery: "proxy",
|
||||||
|
width: 2400,
|
||||||
|
quality: 90,
|
||||||
|
format: "webp",
|
||||||
|
});
|
||||||
|
}, [fullscreenPreview]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return () => {
|
return () => {
|
||||||
if (composerUploadClearTimeoutRef.current) {
|
if (composerUploadClearTimeoutRef.current) {
|
||||||
@ -3649,20 +3650,17 @@ export function CanvasChatPanel({
|
|||||||
|
|
||||||
const setStreamImageGenerationStateForChat = useCallback(
|
const setStreamImageGenerationStateForChat = useCallback(
|
||||||
(chatSessionId: string, next: ImageGenerationStatusStreamPayload | null) => {
|
(chatSessionId: string, next: ImageGenerationStatusStreamPayload | null) => {
|
||||||
const userMessageId = imageGenerationUserMessageIdsRef.current[chatSessionId];
|
|
||||||
if (next?.userMessageId && userMessageId && next.userMessageId !== userMessageId) return;
|
|
||||||
setStreamImageGenerationStateByChatId((prev) => ({
|
setStreamImageGenerationStateByChatId((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
[chatSessionId]: next ? { ...next, userMessageId: next.userMessageId || userMessageId } : null,
|
[chatSessionId]: next,
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
const clearStreamImageGenerationStateForChat = useCallback((chatSessionId: string, preserveFailure = false) => {
|
const clearStreamImageGenerationStateForChat = useCallback((chatSessionId: string) => {
|
||||||
setStreamImageGenerationStateByChatId((prev) => {
|
setStreamImageGenerationStateByChatId((prev) => {
|
||||||
if (!(chatSessionId in prev)) return prev;
|
if (!(chatSessionId in prev)) return prev;
|
||||||
if (preserveFailure && prev[chatSessionId]?.phase === "failed") return prev;
|
|
||||||
const next = { ...prev };
|
const next = { ...prev };
|
||||||
delete next[chatSessionId];
|
delete next[chatSessionId];
|
||||||
return next;
|
return next;
|
||||||
@ -5035,15 +5033,6 @@ export function CanvasChatPanel({
|
|||||||
console.log('[CanvasChat] 已插入图片标签:', url.substring(0, 50));
|
console.log('[CanvasChat] 已插入图片标签:', url.substring(0, 50));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const setComposerEditorRef = useCallback((editor: HTMLDivElement | null) => {
|
|
||||||
textareaRef.current = editor as unknown as HTMLTextAreaElement;
|
|
||||||
if (!editor) return;
|
|
||||||
const draft = readCanvasComposerDraft<PickedObjectAttachment>(activeChatId);
|
|
||||||
const text = activeChatIdRef.current === activeChatId ? inputRef.current || draft?.text : draft?.text;
|
|
||||||
if (text) editor.textContent = text;
|
|
||||||
draft?.attachments.forEach((attachment, index) => insertImageTagInEditor(attachment.url, attachment.name || "参考图片", index));
|
|
||||||
}, [activeChatId, insertImageTagInEditor]);
|
|
||||||
|
|
||||||
const handleCopyUserMessage = useCallback(async (message: ChatMessage) => {
|
const handleCopyUserMessage = useCallback(async (message: ChatMessage) => {
|
||||||
const payload = getUserMessageCopyPayload(message);
|
const payload = getUserMessageCopyPayload(message);
|
||||||
if (!payload.text && payload.attachments.length === 0) {
|
if (!payload.text && payload.attachments.length === 0) {
|
||||||
@ -6108,7 +6097,7 @@ export function CanvasChatPanel({
|
|||||||
const activeComposerMode = unifiedImageAgent && (rawComposerMode === "image" || rawComposerMode === "video")
|
const activeComposerMode = unifiedImageAgent && (rawComposerMode === "image" || rawComposerMode === "video")
|
||||||
? "agent" : rawComposerMode;
|
? "agent" : rawComposerMode;
|
||||||
const isImageAgentComposer = unifiedImageAgent && activeComposerMode === "agent";
|
const isImageAgentComposer = unifiedImageAgent && activeComposerMode === "agent";
|
||||||
const [imageAgentEnabled, setImageAgentEnabled] = useState(true);
|
const [imageAgentEnabled, setImageAgentEnabled] = useState(false);
|
||||||
const imageAgentEnabledRef = useRef(imageAgentEnabled);
|
const imageAgentEnabledRef = useRef(imageAgentEnabled);
|
||||||
imageAgentEnabledRef.current = imageAgentEnabled;
|
imageAgentEnabledRef.current = imageAgentEnabled;
|
||||||
const isVideoToolbarDark = isVideoWorkspace || activeComposerMode === "video";
|
const isVideoToolbarDark = isVideoWorkspace || activeComposerMode === "video";
|
||||||
@ -7991,12 +7980,8 @@ export function CanvasChatPanel({
|
|||||||
|
|
||||||
const store = useCanvasStore.getState();
|
const store = useCanvasStore.getState();
|
||||||
store.markAgentOperationRefresh();
|
store.markAgentOperationRefresh();
|
||||||
if (applyCanvasRemoteSnapshot) {
|
store.loadCanvas(result.data.canvasData);
|
||||||
if (!applyCanvasRemoteSnapshot({ projectId: targetProjectId, canvasData: result.data.canvasData, updatedAt: String(result.data.updatedAt || "") })) return false;
|
store.setCanvasRevision(String(result.data.updatedAt || ""), String(result.data.updatedAt || ""));
|
||||||
} else {
|
|
||||||
store.loadCanvas(result.data.canvasData);
|
|
||||||
store.setCanvasRevision(String(result.data.updatedAt || ""), String(result.data.updatedAt || ""));
|
|
||||||
}
|
|
||||||
if (createdNodeIds.length > 0) {
|
if (createdNodeIds.length > 0) {
|
||||||
const refreshedStore = useCanvasStore.getState();
|
const refreshedStore = useCanvasStore.getState();
|
||||||
refreshedStore.selectNodes(createdNodeIds);
|
refreshedStore.selectNodes(createdNodeIds);
|
||||||
@ -8024,7 +8009,7 @@ export function CanvasChatPanel({
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[applyCanvasRemoteSnapshot, applyCanvasViewOperation, canvasId, focusCanvasOnNode, mutate]
|
[applyCanvasViewOperation, canvasId, focusCanvasOnNode, mutate]
|
||||||
);
|
);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -8041,8 +8026,6 @@ export function CanvasChatPanel({
|
|||||||
api: "/api/chat",
|
api: "/api/chat",
|
||||||
fetch: fetchWithErrorHandlers,
|
fetch: fetchWithErrorHandlers,
|
||||||
async prepareSendMessagesRequest(request) {
|
async prepareSendMessagesRequest(request) {
|
||||||
const userMessageId = request.messages.findLast((item) => item.role === "user")?.id;
|
|
||||||
if (userMessageId) imageGenerationUserMessageIdsRef.current[request.id] = userMessageId;
|
|
||||||
// 从 canvas-{uuid} 格式中提取纯 UUID,后端 schema 要求纯 UUID
|
// 从 canvas-{uuid} 格式中提取纯 UUID,后端 schema 要求纯 UUID
|
||||||
const rawId = request.id.startsWith('canvas-')
|
const rawId = request.id.startsWith('canvas-')
|
||||||
? request.id.replace('canvas-', '')
|
? request.id.replace('canvas-', '')
|
||||||
@ -8155,27 +8138,20 @@ export function CanvasChatPanel({
|
|||||||
const payload: ImageGenerationStatusStreamPayload =
|
const payload: ImageGenerationStatusStreamPayload =
|
||||||
typeof dataPart.data === "string" ? JSON.parse(dataPart.data) : dataPart.data;
|
typeof dataPart.data === "string" ? JSON.parse(dataPart.data) : dataPart.data;
|
||||||
if (!payload || typeof payload !== "object") return;
|
if (!payload || typeof payload !== "object") return;
|
||||||
const userMessageId = imageGenerationUserMessageIdsRef.current[eventChatId];
|
|
||||||
if (payload.userMessageId && userMessageId && payload.userMessageId !== userMessageId) return;
|
|
||||||
if (payload.phase === "completed") {
|
if (payload.phase === "completed") {
|
||||||
setStreamImageGenerationStateForChat(eventChatId, {
|
setStreamImageGenerationStateForChat(eventChatId, {
|
||||||
phase: "finalizing",
|
phase: "finalizing",
|
||||||
userMessageId: payload.userMessageId,
|
|
||||||
providerType: payload.providerType,
|
providerType: payload.providerType,
|
||||||
modelName: getAgentSafeModelName(payload.modelName),
|
modelName: getAgentSafeModelName(payload.modelName),
|
||||||
imageCount: Number(payload.imageCount || 0) || undefined,
|
imageCount: Number(payload.imageCount || 0) || undefined,
|
||||||
});
|
});
|
||||||
window.setTimeout(() => {
|
window.setTimeout(() => {
|
||||||
if (payload.userMessageId && imageGenerationUserMessageIdsRef.current[eventChatId] !== payload.userMessageId) return;
|
|
||||||
clearStreamImageGenerationStateForChat(eventChatId);
|
clearStreamImageGenerationStateForChat(eventChatId);
|
||||||
}, 30_000);
|
}, 30_000);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setStreamImageGenerationStateForChat(eventChatId, {
|
setStreamImageGenerationStateForChat(eventChatId, {
|
||||||
phase: payload.phase || "processing",
|
phase: payload.phase || "processing",
|
||||||
userMessageId: payload.userMessageId,
|
|
||||||
requestId: payload.requestId,
|
|
||||||
failure: payload.failure,
|
|
||||||
providerType: payload.providerType,
|
providerType: payload.providerType,
|
||||||
modelName: getAgentSafeModelName(payload.modelName),
|
modelName: getAgentSafeModelName(payload.modelName),
|
||||||
imageCount: Number(payload.imageCount || 0) || undefined,
|
imageCount: Number(payload.imageCount || 0) || undefined,
|
||||||
@ -8443,11 +8419,6 @@ export function CanvasChatPanel({
|
|||||||
)
|
)
|
||||||
: false;
|
: false;
|
||||||
const normalizedParts = Array.isArray(messageData.parts) ? messageData.parts : [];
|
const normalizedParts = Array.isArray(messageData.parts) ? messageData.parts : [];
|
||||||
const failureStatus = findImageGenerationFailure(
|
|
||||||
[{ role: messageData.role, parts: normalizedParts }],
|
|
||||||
imageGenerationUserMessageIdsRef.current[eventChatId],
|
|
||||||
);
|
|
||||||
if (failureStatus) setStreamImageGenerationStateForChat(eventChatId, failureStatus);
|
|
||||||
const normalizedAttachments = Array.isArray(messageData.attachments)
|
const normalizedAttachments = Array.isArray(messageData.attachments)
|
||||||
? messageData.attachments
|
? messageData.attachments
|
||||||
: [];
|
: [];
|
||||||
@ -8516,7 +8487,7 @@ export function CanvasChatPanel({
|
|||||||
(hasImageGenEndMarker || incomingImageUrls.length > 0 || incomingVideoUrls.length > 0) &&
|
(hasImageGenEndMarker || incomingImageUrls.length > 0 || incomingVideoUrls.length > 0) &&
|
||||||
!shouldDeferClearForActiveMedia
|
!shouldDeferClearForActiveMedia
|
||||||
) {
|
) {
|
||||||
clearStreamImageGenerationStateForChat(eventChatId, true);
|
clearStreamImageGenerationStateForChat(eventChatId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isEventForActiveChat) {
|
if (!isEventForActiveChat) {
|
||||||
@ -8842,6 +8813,12 @@ export function CanvasChatPanel({
|
|||||||
hasHydratedHistoryRef.current = true;
|
hasHydratedHistoryRef.current = true;
|
||||||
|
|
||||||
setMessages(sortCanvasMessagesByCreatedAt(dbMessages));
|
setMessages(sortCanvasMessagesByCreatedAt(dbMessages));
|
||||||
|
|
||||||
|
if (dbMessages.length > 0) {
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
messagesEndRef.current?.scrollIntoView({ behavior: "auto", block: "end" });
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const reloadMessagesOnce = async () => {
|
const reloadMessagesOnce = async () => {
|
||||||
@ -8954,7 +8931,7 @@ export function CanvasChatPanel({
|
|||||||
onRunChange: (run) => {
|
onRunChange: (run) => {
|
||||||
// The live stream owns its message; polling only recovers disconnected replies.
|
// The live stream owns its message; polling only recovers disconnected replies.
|
||||||
if (status === "submitted" || status === "streaming") return;
|
if (status === "submitted" || status === "streaming") return;
|
||||||
if (!run?.assistantMessageId || !run.assistantText || run.chatId !== toRawCanvasChatId(activeChatIdRef.current)) return;
|
if (!run?.assistantMessageId || !run.assistantText || run.chatId !== activeChatIdRef.current) return;
|
||||||
// Recover the same assistant reply after a stream disconnect or refresh.
|
// Recover the same assistant reply after a stream disconnect or refresh.
|
||||||
setMessages(current => {
|
setMessages(current => {
|
||||||
const index = current.findIndex(message => message.id === run.assistantMessageId);
|
const index = current.findIndex(message => message.id === run.assistantMessageId);
|
||||||
@ -8969,33 +8946,6 @@ export function CanvasChatPanel({
|
|||||||
});
|
});
|
||||||
const activeCanvasAgentRun = canvasAgentTask.run;
|
const activeCanvasAgentRun = canvasAgentTask.run;
|
||||||
const isCanvasAgentRunning = activeCanvasAgentRun?.status === "running";
|
const isCanvasAgentRunning = activeCanvasAgentRun?.status === "running";
|
||||||
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;
|
|
||||||
}
|
|
||||||
request.save(async () => {
|
|
||||||
if ((input.trim() || attachments.length) && !writeCanvasComposerDraft(activeChatId, { text: input, attachments }))
|
|
||||||
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]);
|
|
||||||
|
|
||||||
const restorePendingClawdRequestToComposer = useCallback(
|
const restorePendingClawdRequestToComposer = useCallback(
|
||||||
(snapshot: PendingClawdRequestRestoreSnapshot) => {
|
(snapshot: PendingClawdRequestRestoreSnapshot) => {
|
||||||
@ -9139,7 +9089,6 @@ export function CanvasChatPanel({
|
|||||||
},
|
},
|
||||||
} as ChatMessage;
|
} as ChatMessage;
|
||||||
|
|
||||||
void scrollMessagesToBottom({ animation: "instant" });
|
|
||||||
sendMessage(launchUserMessage as any);
|
sendMessage(launchUserMessage as any);
|
||||||
setMessages((previousMessages) =>
|
setMessages((previousMessages) =>
|
||||||
sortCanvasMessagesByCreatedAt([
|
sortCanvasMessagesByCreatedAt([
|
||||||
@ -9159,7 +9108,7 @@ export function CanvasChatPanel({
|
|||||||
if (editableDiv) {
|
if (editableDiv) {
|
||||||
editableDiv.innerHTML = "";
|
editableDiv.innerHTML = "";
|
||||||
}
|
}
|
||||||
}, [activeChatId, rawChatId, selectedModelId, status, sendMessage, setMessages, clearSelection, launchAutoSendTick, activeComposerMode, scrollMessagesToBottom]);
|
}, [activeChatId, rawChatId, selectedModelId, status, sendMessage, setMessages, clearSelection, launchAutoSendTick, activeComposerMode]);
|
||||||
|
|
||||||
// 当历史消息加载完成后,更新消息列表(支持同一会话的增量刷新)
|
// 当历史消息加载完成后,更新消息列表(支持同一会话的增量刷新)
|
||||||
const loadedHistorySignatureRef = useRef<string>("");
|
const loadedHistorySignatureRef = useRef<string>("");
|
||||||
@ -9179,6 +9128,7 @@ export function CanvasChatPanel({
|
|||||||
hiddenPromptRef.current = null;
|
hiddenPromptRef.current = null;
|
||||||
autoContextRef.current = null;
|
autoContextRef.current = null;
|
||||||
pendingLaunchRef.current = null;
|
pendingLaunchRef.current = null;
|
||||||
|
setMessageViewportKey((prev) => prev + 1);
|
||||||
setMessages([]);
|
setMessages([]);
|
||||||
setInput("");
|
setInput("");
|
||||||
setAttachments([]);
|
setAttachments([]);
|
||||||
@ -9193,20 +9143,11 @@ export function CanvasChatPanel({
|
|||||||
}, [activeChatId, setMessages]);
|
}, [activeChatId, setMessages]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const draft = readCanvasComposerDraft<PickedObjectAttachment>(activeChatId);
|
if (historyScrollTimerRef.current) {
|
||||||
if (!draft || (input && input !== draft.text)) return;
|
clearTimeout(historyScrollTimerRef.current);
|
||||||
if (!input) setInput(draft.text);
|
historyScrollTimerRef.current = null;
|
||||||
setAttachments(draft.attachments);
|
}
|
||||||
const frame = requestAnimationFrame(() => {
|
|
||||||
const editor = textareaRef.current as HTMLDivElement | null;
|
|
||||||
if (!editor || getComposerPlainTextFromElement(editor).trim()) return;
|
|
||||||
editor.textContent = draft.text;
|
|
||||||
draft.attachments.forEach((attachment, index) => insertImageTagInEditor(attachment.url, attachment.name || "参考图片", index));
|
|
||||||
});
|
|
||||||
return () => cancelAnimationFrame(frame);
|
|
||||||
}, [activeChatId, activeComposerMode, input, insertImageTagInEditor]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!historyData || !rawChatId) return;
|
if (!historyData || !rawChatId) return;
|
||||||
const historyMessages = historyData.messages || [];
|
const historyMessages = historyData.messages || [];
|
||||||
const isClawdHistory = isClawdMode || historyMessages.some(isClawdChatMessage);
|
const isClawdHistory = isClawdMode || historyMessages.some(isClawdChatMessage);
|
||||||
@ -9247,14 +9188,29 @@ export function CanvasChatPanel({
|
|||||||
missingPendingMessage ? [...historyMessages, pendingMessage] : historyMessages
|
missingPendingMessage ? [...historyMessages, pendingMessage] : historyMessages
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
setMessageViewportKey((prev) => prev + 1);
|
||||||
console.log("[CanvasChat] 已同步历史消息:", historyMessages.length, signature);
|
console.log("[CanvasChat] 已同步历史消息:", historyMessages.length, signature);
|
||||||
|
|
||||||
|
if (historyMessages.length > 0) {
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
messagesEndRef.current?.scrollIntoView({ behavior: "auto", block: "end" });
|
||||||
|
});
|
||||||
|
historyScrollTimerRef.current = setTimeout(() => {
|
||||||
|
messagesEndRef.current?.scrollIntoView({ behavior: "auto", block: "end" });
|
||||||
|
historyScrollTimerRef.current = null;
|
||||||
|
}, 160);
|
||||||
|
return () => {
|
||||||
|
if (historyScrollTimerRef.current) {
|
||||||
|
clearTimeout(historyScrollTimerRef.current);
|
||||||
|
historyScrollTimerRef.current = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}, [activeChatId, activeClawdRequestUi?.userMessageId, historyData, isClawdMode, isClawdSubmitting, rawChatId, setMessages, status]);
|
}, [activeChatId, activeClawdRequestUi?.userMessageId, historyData, isClawdMode, isClawdSubmitting, rawChatId, setMessages, status]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const streamState = streamImageGenerationStateByChatId[activeChatId];
|
const streamState = streamImageGenerationStateByChatId[activeChatId];
|
||||||
if (!rawChatId || !streamState || (streamState.phase === "failed" && streamState.failure?.outcome !== "unknown")) return;
|
if (!rawChatId || !streamState) return;
|
||||||
|
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
const poll = async () => {
|
const poll = async () => {
|
||||||
@ -9278,13 +9234,6 @@ export function CanvasChatPanel({
|
|||||||
const streamState = streamImageGenerationStateByChatId[activeChatId];
|
const streamState = streamImageGenerationStateByChatId[activeChatId];
|
||||||
if (!streamState || messages.length === 0) return;
|
if (!streamState || messages.length === 0) return;
|
||||||
|
|
||||||
const failure = findImageGenerationFailure(messages, streamState.userMessageId || "");
|
|
||||||
if (failure && streamState.phase !== "failed") {
|
|
||||||
setStreamImageGenerationStateForChat(activeChatId, failure);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (streamState.phase === "failed" && streamState.failure?.outcome !== "unknown") return;
|
|
||||||
|
|
||||||
const lastUserIndex = [...messages]
|
const lastUserIndex = [...messages]
|
||||||
.map((message) => message.role)
|
.map((message) => message.role)
|
||||||
.lastIndexOf("user");
|
.lastIndexOf("user");
|
||||||
@ -9301,7 +9250,8 @@ export function CanvasChatPanel({
|
|||||||
.join("\n");
|
.join("\n");
|
||||||
return (
|
return (
|
||||||
extractAssistantImageUrls(textContent).length > 0 ||
|
extractAssistantImageUrls(textContent).length > 0 ||
|
||||||
extractVideoUrls(textContent).length > 0
|
extractVideoUrls(textContent).length > 0 ||
|
||||||
|
/失败|超时|重试|error|failed/i.test(textContent)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -9322,7 +9272,6 @@ export function CanvasChatPanel({
|
|||||||
messages,
|
messages,
|
||||||
streamImageGenerationStateByChatId,
|
streamImageGenerationStateByChatId,
|
||||||
clearStreamImageGenerationStateForChat,
|
clearStreamImageGenerationStateForChat,
|
||||||
setStreamImageGenerationStateForChat,
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -9829,13 +9778,11 @@ export function CanvasChatPanel({
|
|||||||
});
|
});
|
||||||
}, [activeChatSessionMode, messages, nodes, updateNode]);
|
}, [activeChatSessionMode, messages, nodes, updateNode]);
|
||||||
|
|
||||||
// Open each conversation at its latest message; later updates respect manual scrolling.
|
// 滚动到底部
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (messages.length === 0 && !isClawdSubmitting) return;
|
if (messages.length === 0 && !isClawdSubmitting) return;
|
||||||
if (initialScrollChatRef.current === activeChatId) return;
|
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
||||||
initialScrollChatRef.current = activeChatId;
|
}, [isClawdSubmitting, messages]);
|
||||||
void scrollMessagesToBottom({ animation: "instant" });
|
|
||||||
}, [activeChatId, isClawdSubmitting, messages.length, scrollMessagesToBottom]);
|
|
||||||
|
|
||||||
// 标记是否已经为当前画布设置过标题(避免重复调用)
|
// 标记是否已经为当前画布设置过标题(避免重复调用)
|
||||||
const hasSetCanvasTitle = useRef(false);
|
const hasSetCanvasTitle = useRef(false);
|
||||||
@ -12604,7 +12551,6 @@ export function CanvasChatPanel({
|
|||||||
const supplement = typeof overrideInput === "string" ? overrideInput.trim() : input.trim();
|
const supplement = typeof overrideInput === "string" ? overrideInput.trim() : input.trim();
|
||||||
if (isCanvasAgentRunning && !isClawdMode && imageAgentEnabled) {
|
if (isCanvasAgentRunning && !isClawdMode && imageAgentEnabled) {
|
||||||
if (supplement && await canvasAgentTask.control("steer", supplement)) {
|
if (supplement && await canvasAgentTask.control("steer", supplement)) {
|
||||||
void scrollMessagesToBottom({ animation: "instant" });
|
|
||||||
setInput("");
|
setInput("");
|
||||||
const editor = textareaRef.current as HTMLDivElement | null;
|
const editor = textareaRef.current as HTMLDivElement | null;
|
||||||
if (editor) editor.innerHTML = "";
|
if (editor) editor.innerHTML = "";
|
||||||
@ -12902,7 +12848,6 @@ export function CanvasChatPanel({
|
|||||||
requestChatId,
|
requestChatId,
|
||||||
clawdRequestRestoreSnapshot
|
clawdRequestRestoreSnapshot
|
||||||
);
|
);
|
||||||
void scrollMessagesToBottom({ animation: "instant" });
|
|
||||||
startClawdRequestUi(requestChatId, userMessageId, nextProcessingHint);
|
startClawdRequestUi(requestChatId, userMessageId, nextProcessingHint);
|
||||||
setMessages((previousMessages) =>
|
setMessages((previousMessages) =>
|
||||||
sortCanvasMessagesByCreatedAt([
|
sortCanvasMessagesByCreatedAt([
|
||||||
@ -13391,26 +13336,6 @@ export function CanvasChatPanel({
|
|||||||
});
|
});
|
||||||
inFlightChatIdRef.current = activeChatId;
|
inFlightChatIdRef.current = activeChatId;
|
||||||
inFlightRawChatIdRef.current = rawChatId;
|
inFlightRawChatIdRef.current = rawChatId;
|
||||||
imageGenerationUserMessageIdsRef.current[activeChatId] = userMessageId;
|
|
||||||
if (shouldUseCanvasAgentRuntime({
|
|
||||||
composerMode: effectiveComposerMode,
|
|
||||||
agentPresetId: effectiveAgentEntryMode === "preset" ? selectedAgentPreset?.id : undefined,
|
|
||||||
workspaceVariant: isVideoWorkspace ? "video" : "canvas",
|
|
||||||
contextPayload: contextPayloadRef.current || undefined,
|
|
||||||
}) && onBeforeAgentRequest) {
|
|
||||||
try {
|
|
||||||
if (!(await onBeforeAgentRequest())) throw new Error("画布自动同步失败,本次任务未提交。输入内容已保留,请检查网络后再次发送。");
|
|
||||||
} catch (error) {
|
|
||||||
releaseSendGuard(activeChatId);
|
|
||||||
inFlightChatIdRef.current = null;
|
|
||||||
inFlightRawChatIdRef.current = null;
|
|
||||||
contextPayloadRef.current = null;
|
|
||||||
writeCanvasComposerDraft(activeChatId, { text: composerInput, attachments: effectiveAttachments });
|
|
||||||
toast.error(error instanceof Error ? error.message : "画布自动同步失败,本次任务未提交,输入内容已保留。");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
clearCanvasComposerDraft(activeChatId);
|
|
||||||
clearStreamImageGenerationStateForChat(activeChatId);
|
clearStreamImageGenerationStateForChat(activeChatId);
|
||||||
clearAgentProgressStateForChat(activeChatId);
|
clearAgentProgressStateForChat(activeChatId);
|
||||||
if (shouldShowOptimisticImageGeneration) {
|
if (shouldShowOptimisticImageGeneration) {
|
||||||
@ -13435,7 +13360,6 @@ export function CanvasChatPanel({
|
|||||||
|
|
||||||
// 使用与 multimodal-input 相同的格式发送消息
|
// 使用与 multimodal-input 相同的格式发送消息
|
||||||
try {
|
try {
|
||||||
void scrollMessagesToBottom({ animation: "instant" });
|
|
||||||
sendMessage(optimisticUserMessage as any);
|
sendMessage(optimisticUserMessage as any);
|
||||||
setMessages((previousMessages) =>
|
setMessages((previousMessages) =>
|
||||||
sortCanvasMessagesByCreatedAt([
|
sortCanvasMessagesByCreatedAt([
|
||||||
@ -13474,9 +13398,6 @@ export function CanvasChatPanel({
|
|||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
isCanvasAgentRunning,
|
isCanvasAgentRunning,
|
||||||
scrollMessagesToBottom,
|
|
||||||
onBeforeAgentRequest,
|
|
||||||
isVideoWorkspace,
|
|
||||||
canvasAgentTask.control,
|
canvasAgentTask.control,
|
||||||
activeChatId,
|
activeChatId,
|
||||||
activeComposerMode,
|
activeComposerMode,
|
||||||
@ -13614,19 +13535,23 @@ export function CanvasChatPanel({
|
|||||||
|
|
||||||
console.log('[CanvasChat] 更新画布:', rawId);
|
console.log('[CanvasChat] 更新画布:', rawId);
|
||||||
|
|
||||||
if (!runCanvasRemoteOperation) return;
|
const updateResponse = await fetch(`/api/canvas/${rawId}`, {
|
||||||
await runCanvasRemoteOperation(async () => {
|
method: 'PUT',
|
||||||
if (useCanvasStore.getState().projectId !== rawId) return;
|
headers: { 'Content-Type': 'application/json' },
|
||||||
const result = await canvasService.updateProject(rawId, { title, thumbnail: firstImageUrl });
|
body: JSON.stringify({
|
||||||
if (!result.success) throw new Error(result.error || "画布名称同步失败");
|
title,
|
||||||
|
thumbnail: firstImageUrl,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('[CanvasChat] 画布更新响应:', updateResponse.status);
|
||||||
} else {
|
} else {
|
||||||
console.error('[CanvasChat] auto-title API 请求失败:', response.status);
|
console.error('[CanvasChat] auto-title API 请求失败:', response.status);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[CanvasChat] 自动命名失败:', error);
|
console.error('[CanvasChat] 自动命名失败:', error);
|
||||||
}
|
}
|
||||||
}, [chatId, runCanvasRemoteOperation]);
|
}, [chatId]);
|
||||||
|
|
||||||
const addUploadedImageAttachmentToComposer = useCallback(
|
const addUploadedImageAttachmentToComposer = useCallback(
|
||||||
(uploaded: ComposerUploadedImage) => {
|
(uploaded: ComposerUploadedImage) => {
|
||||||
@ -15180,7 +15105,7 @@ export function CanvasChatPanel({
|
|||||||
!hasGenerationEnd &&
|
!hasGenerationEnd &&
|
||||||
Boolean(currentModel || activeComposerMode === "agent"));
|
Boolean(currentModel || activeComposerMode === "agent"));
|
||||||
const hasActiveTypewriter = Object.keys(activeTypingSections).length > 0;
|
const hasActiveTypewriter = Object.keys(activeTypingSections).length > 0;
|
||||||
const shouldRenderImageLoading = shouldShowImageLoading && streamImageGenerationState?.phase !== "failed" && !hasActiveTypewriter;
|
const shouldRenderImageLoading = shouldShowImageLoading && !hasActiveTypewriter;
|
||||||
const loadingImageCount = streamImageGenerationState?.imageCount || imageParams.imageCount || 1;
|
const loadingImageCount = streamImageGenerationState?.imageCount || imageParams.imageCount || 1;
|
||||||
const loadingProviderType =
|
const loadingProviderType =
|
||||||
streamImageGenerationState?.providerType ||
|
streamImageGenerationState?.providerType ||
|
||||||
@ -15243,9 +15168,9 @@ export function CanvasChatPanel({
|
|||||||
phase === "queued"
|
phase === "queued"
|
||||||
? "等待开始生成"
|
? "等待开始生成"
|
||||||
: phase === "finalizing"
|
: phase === "finalizing"
|
||||||
? "正在保存生成结果"
|
? "正在润饰细节"
|
||||||
: phase === "failed"
|
: phase === "failed"
|
||||||
? streamState.failure?.message || "生成失败"
|
? "生成失败"
|
||||||
: "正在创建图片";
|
: "正在创建图片";
|
||||||
const status = phase === "failed" ? "failed" : "generating";
|
const status = phase === "failed" ? "failed" : "generating";
|
||||||
const currentNodes = useCanvasStore.getState().nodes;
|
const currentNodes = useCanvasStore.getState().nodes;
|
||||||
@ -15301,10 +15226,14 @@ export function CanvasChatPanel({
|
|||||||
canvasGenerationPlaceholder: true,
|
canvasGenerationPlaceholder: true,
|
||||||
chatId: activeChatId,
|
chatId: activeChatId,
|
||||||
detailProgressLabel: label,
|
detailProgressLabel: label,
|
||||||
generationProgress: undefined,
|
generationProgress:
|
||||||
generationFailure: streamState.failure,
|
phase === "queued"
|
||||||
generationRequestId: streamState.requestId,
|
? 12
|
||||||
generationUserMessageId: streamState.userMessageId,
|
: phase === "finalizing"
|
||||||
|
? 96
|
||||||
|
: phase === "failed"
|
||||||
|
? 100
|
||||||
|
: undefined,
|
||||||
modelName: streamState.modelName || currentModel?.displayName || undefined,
|
modelName: streamState.modelName || currentModel?.displayName || undefined,
|
||||||
providerType: streamState.providerType || currentModel?.providerType || undefined,
|
providerType: streamState.providerType || currentModel?.providerType || undefined,
|
||||||
placementMode: "anchored-generation",
|
placementMode: "anchored-generation",
|
||||||
@ -15398,7 +15327,6 @@ export function CanvasChatPanel({
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
message.role === "assistant" &&
|
message.role === "assistant" &&
|
||||||
!rawParts.some((part: any) => part.type === "data-imageGenerationStatus" && part.data?.failure) &&
|
|
||||||
isInternalCanvasImageGenerationFailureText(cleanedParts)
|
isInternalCanvasImageGenerationFailureText(cleanedParts)
|
||||||
) {
|
) {
|
||||||
return acc;
|
return acc;
|
||||||
@ -15708,7 +15636,7 @@ export function CanvasChatPanel({
|
|||||||
processedMessageIds.current.clear();
|
processedMessageIds.current.clear();
|
||||||
hasHydratedHistoryRef.current = true;
|
hasHydratedHistoryRef.current = true;
|
||||||
setMessages(sortCanvasMessagesByCreatedAt(fresh.messages));
|
setMessages(sortCanvasMessagesByCreatedAt(fresh.messages));
|
||||||
void scrollMessagesToBottom({ animation: "instant" });
|
setMessageViewportKey((previous) => previous + 1);
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
console.error("[CanvasChat] Failed to reopen current conversation:", error);
|
console.error("[CanvasChat] Failed to reopen current conversation:", error);
|
||||||
toast.error("聊天记录暂时读取失败,请重试");
|
toast.error("聊天记录暂时读取失败,请重试");
|
||||||
@ -15722,7 +15650,7 @@ export function CanvasChatPanel({
|
|||||||
replaceChatSessionQuery(sessionId);
|
replaceChatSessionQuery(sessionId);
|
||||||
setIsSessionMenuOpen(false);
|
setIsSessionMenuOpen(false);
|
||||||
toast.success("已切换聊天窗口");
|
toast.success("已切换聊天窗口");
|
||||||
}, [activeChatId, isClawdSubmitting, mutateHistoryData, releaseSendGuard, replaceChatSessionQuery, scrollMessagesToBottom, setMessages, status]);
|
}, [activeChatId, isClawdSubmitting, mutateHistoryData, releaseSendGuard, replaceChatSessionQuery, setMessages, status]);
|
||||||
|
|
||||||
const switchToClawdHistorySession = useCallback(
|
const switchToClawdHistorySession = useCallback(
|
||||||
(historyItem: ClawdHistoryItem) => {
|
(historyItem: ClawdHistoryItem) => {
|
||||||
@ -15884,8 +15812,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(activeClawdSkillTag.iconUrl)) {
|
if (event.currentTarget.src !== activeClawdSkillTag.iconUrl) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(activeClawdSkillTag.iconUrl);
|
event.currentTarget.src = activeClawdSkillTag.iconUrl || "";
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -16139,17 +16067,11 @@ export function CanvasChatPanel({
|
|||||||
{isDetailEmptyState ? (
|
{isDetailEmptyState ? (
|
||||||
<div className="flex-1 min-h-0" />
|
<div className="flex-1 min-h-0" />
|
||||||
) : (
|
) : (
|
||||||
<div key={activeChatId} className="relative flex min-h-0 flex-1 flex-col">
|
|
||||||
<div
|
<div
|
||||||
ref={messagesScrollRef}
|
key={`${activeChatId}-${messageViewportKey}`}
|
||||||
data-testid="canvas-chat-messages"
|
|
||||||
role="region"
|
|
||||||
aria-label="对话消息"
|
|
||||||
// biome-ignore lint/a11y/noNoninteractiveTabindex: The scroll region needs PageUp/PageDown keyboard input.
|
|
||||||
tabIndex={0}
|
|
||||||
className={cn(
|
className={cn(
|
||||||
"flex-1 min-h-0 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
|
"flex-1 min-h-0 overflow-x-hidden",
|
||||||
isPlainCanvasEmptyState ? "overflow-hidden" : "overflow-auto",
|
isPlainCanvasEmptyState ? "overflow-hidden" : "overflow-y-auto",
|
||||||
isWorkspacePresentation ? "p-5" : "p-3",
|
isWorkspacePresentation ? "p-5" : "p-3",
|
||||||
isPlainCanvasEmptyState
|
isPlainCanvasEmptyState
|
||||||
? "pt-0"
|
? "pt-0"
|
||||||
@ -16158,7 +16080,7 @@ export function CanvasChatPanel({
|
|||||||
: "pt-3"
|
: "pt-3"
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div ref={messagesContentRef} className={cn(
|
<div className={cn(
|
||||||
"space-y-5 overflow-x-hidden pr-1",
|
"space-y-5 overflow-x-hidden pr-1",
|
||||||
isPlainCanvasEmptyState ? "h-full pb-0" : "pb-24"
|
isPlainCanvasEmptyState ? "h-full pb-0" : "pb-24"
|
||||||
)}>
|
)}>
|
||||||
@ -16209,8 +16131,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(attachment.url)) {
|
if (event.currentTarget.src !== attachment.url) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(attachment.url);
|
event.currentTarget.src = attachment.url;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -16286,8 +16208,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(attachment.url)) {
|
if (event.currentTarget.src !== attachment.url) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(attachment.url);
|
event.currentTarget.src = attachment.url;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -16472,8 +16394,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(tag.iconUrl)) {
|
if (event.currentTarget.src !== tag.iconUrl) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(tag.iconUrl);
|
event.currentTarget.src = tag.iconUrl || "";
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -16773,8 +16695,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(attachment.url)) {
|
if (event.currentTarget.src !== attachment.url) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(attachment.url);
|
event.currentTarget.src = attachment.url;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -17110,23 +17032,15 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(url)) {
|
if (event.currentTarget.src !== url) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(url);
|
event.currentTarget.src = url;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onClick={(event) => setFullscreenPreview({ url, preview: {
|
onClick={() => setFullscreenPreview({ url })}
|
||||||
src: event.currentTarget.currentSrc || event.currentTarget.src,
|
|
||||||
width: event.currentTarget.naturalWidth,
|
|
||||||
height: event.currentTarget.naturalHeight,
|
|
||||||
} })}
|
|
||||||
onDoubleClick={(e) => {
|
onDoubleClick={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
setFullscreenPreview({ url, preview: {
|
setFullscreenPreview({ url });
|
||||||
src: e.currentTarget.currentSrc || e.currentTarget.src,
|
|
||||||
width: e.currentTarget.naturalWidth,
|
|
||||||
height: e.currentTarget.naturalHeight,
|
|
||||||
} });
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<div className="absolute bottom-2 right-2 flex items-center gap-1.5">
|
<div className="absolute bottom-2 right-2 flex items-center gap-1.5">
|
||||||
@ -17361,22 +17275,9 @@ export function CanvasChatPanel({
|
|||||||
/>
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
|
<div ref={messagesEndRef} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{!isMessageListAtBottom && renderableVisibleMessages.length > 0 ? (
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
variant="outline"
|
|
||||||
size="icon"
|
|
||||||
aria-label="回到最新消息"
|
|
||||||
title="回到最新消息"
|
|
||||||
className="absolute bottom-3 left-1/2 z-10 -translate-x-1/2 rounded-full shadow-sm"
|
|
||||||
onClick={() => void scrollMessagesToBottom({ animation: "instant" })}
|
|
||||||
>
|
|
||||||
<ChevronDown className="h-4 w-4" />
|
|
||||||
</Button>
|
|
||||||
) : null}
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{fullscreenPreview && createPortal(
|
{fullscreenPreview && createPortal(
|
||||||
@ -17385,7 +17286,7 @@ export function CanvasChatPanel({
|
|||||||
onClick={() => setFullscreenPreview(null)}
|
onClick={() => setFullscreenPreview(null)}
|
||||||
>
|
>
|
||||||
<button
|
<button
|
||||||
className="absolute top-4 right-4 z-10 p-2 rounded-full bg-white/10 hover:bg-white/20 transition-colors"
|
className="absolute top-4 right-4 p-2 rounded-full bg-white/10 hover:bg-white/20 transition-colors"
|
||||||
onClick={() => setFullscreenPreview(null)}
|
onClick={() => setFullscreenPreview(null)}
|
||||||
aria-label="关闭预览"
|
aria-label="关闭预览"
|
||||||
>
|
>
|
||||||
@ -17394,7 +17295,7 @@ export function CanvasChatPanel({
|
|||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
className="absolute top-4 right-16 z-10 inline-flex items-center gap-1 rounded-full bg-white/10 px-3 py-2 text-sm text-white transition-colors hover:bg-white/20 disabled:cursor-not-allowed disabled:opacity-65"
|
className="absolute top-4 right-16 inline-flex items-center gap-1 rounded-full bg-white/10 px-3 py-2 text-sm text-white transition-colors hover:bg-white/20 disabled:cursor-not-allowed disabled:opacity-65"
|
||||||
disabled={isDownloadingOriginalImage}
|
disabled={isDownloadingOriginalImage}
|
||||||
onClick={(event) => {
|
onClick={(event) => {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
@ -17411,11 +17312,13 @@ export function CanvasChatPanel({
|
|||||||
{isDownloadingOriginalImage ? "下载中" : "下载原图"}
|
{isDownloadingOriginalImage ? "下载中" : "下载原图"}
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<ProgressiveImagePreview
|
<img
|
||||||
src={inferOriginalImageUrl(fullscreenPreview.url) || fullscreenPreview.url}
|
src={fullscreenPreviewSrc}
|
||||||
preview={fullscreenPreview.preview}
|
|
||||||
alt={fullscreenPreview.alt || "图片预览"}
|
alt={fullscreenPreview.alt || "图片预览"}
|
||||||
className="max-w-[95vw] max-h-[95vh] object-contain"
|
className="max-w-[95vw] max-h-[95vh] object-contain"
|
||||||
|
decoding="async"
|
||||||
|
draggable={false}
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="absolute bottom-4 left-1/2 -translate-x-1/2 text-white/60 text-sm">
|
<div className="absolute bottom-4 left-1/2 -translate-x-1/2 text-white/60 text-sm">
|
||||||
@ -17982,8 +17885,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(slotAttachment.url)) {
|
if (event.currentTarget.src !== slotAttachment.url) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(slotAttachment.url);
|
event.currentTarget.src = slotAttachment.url;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -18307,8 +18210,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(activeClawdSkillTag.iconUrl)) {
|
if (event.currentTarget.src !== activeClawdSkillTag.iconUrl) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(activeClawdSkillTag.iconUrl);
|
event.currentTarget.src = activeClawdSkillTag.iconUrl || "";
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -18567,8 +18470,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(item.url)) {
|
if (event.currentTarget.src !== item.url) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(item.url);
|
event.currentTarget.src = item.url;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -18623,7 +18526,7 @@ export function CanvasChatPanel({
|
|||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
ref={setComposerEditorRef}
|
ref={textareaRef as any}
|
||||||
contentEditable={!isSubmitting || (isCanvasAgentRunning && !canvasAgentTask.stopPending)}
|
contentEditable={!isSubmitting || (isCanvasAgentRunning && !canvasAgentTask.stopPending)}
|
||||||
role="textbox"
|
role="textbox"
|
||||||
tabIndex={isSubmitting ? -1 : 0}
|
tabIndex={isSubmitting ? -1 : 0}
|
||||||
@ -18657,7 +18560,6 @@ export function CanvasChatPanel({
|
|||||||
setTagPreview(null);
|
setTagPreview(null);
|
||||||
}}
|
}}
|
||||||
onInput={(e) => {
|
onInput={(e) => {
|
||||||
clearCanvasComposerDraft(activeChatId);
|
|
||||||
// 从 contenteditable 中提取纯文本(排除附件标签文本)
|
// 从 contenteditable 中提取纯文本(排除附件标签文本)
|
||||||
const target = e.target as HTMLDivElement;
|
const target = e.target as HTMLDivElement;
|
||||||
const clone = target.cloneNode(true) as HTMLDivElement;
|
const clone = target.cloneNode(true) as HTMLDivElement;
|
||||||
@ -18753,8 +18655,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(slotAttachment.url)) {
|
if (event.currentTarget.src !== slotAttachment.url) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(slotAttachment.url);
|
event.currentTarget.src = slotAttachment.url;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -18821,8 +18723,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(slotAttachment.url)) {
|
if (event.currentTarget.src !== slotAttachment.url) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(slotAttachment.url);
|
event.currentTarget.src = slotAttachment.url;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -19521,8 +19423,8 @@ export function CanvasChatPanel({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(tagPreview.url)) {
|
if (event.currentTarget.src !== tagPreview.url) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(tagPreview.url);
|
event.currentTarget.src = tagPreview.url;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -19,7 +19,6 @@ const PROJECT_LIST_PAGE_SIZE = 24;
|
|||||||
interface CanvasFileListPanelProps {
|
interface CanvasFileListPanelProps {
|
||||||
currentId: string;
|
currentId: string;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
onNavigate?: (url: string) => void;
|
|
||||||
workspaceType?: CanvasWorkspaceType;
|
workspaceType?: CanvasWorkspaceType;
|
||||||
workspaceLabel?: string;
|
workspaceLabel?: string;
|
||||||
createUrl?: string;
|
createUrl?: string;
|
||||||
@ -29,7 +28,6 @@ interface CanvasFileListPanelProps {
|
|||||||
export function CanvasFileListPanel({
|
export function CanvasFileListPanel({
|
||||||
currentId,
|
currentId,
|
||||||
onClose,
|
onClose,
|
||||||
onNavigate,
|
|
||||||
workspaceType = "canvas",
|
workspaceType = "canvas",
|
||||||
workspaceLabel = "画布",
|
workspaceLabel = "画布",
|
||||||
createUrl = "/canvas?new=1",
|
createUrl = "/canvas?new=1",
|
||||||
@ -90,10 +88,6 @@ export function CanvasFileListPanel({
|
|||||||
|
|
||||||
// 创建新画布
|
// 创建新画布
|
||||||
const handleCreateNew = async () => {
|
const handleCreateNew = async () => {
|
||||||
if (onNavigate) {
|
|
||||||
onNavigate(createUrl);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
router.push(createUrl);
|
router.push(createUrl);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -106,10 +100,6 @@ export function CanvasFileListPanel({
|
|||||||
}
|
}
|
||||||
const targetUrl = buildProjectUrl(id);
|
const targetUrl = buildProjectUrl(id);
|
||||||
onClose(); // 关闭文件列表面板
|
onClose(); // 关闭文件列表面板
|
||||||
if (onNavigate) {
|
|
||||||
onNavigate(targetUrl);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 强制整页跳转,避免仅更新 URL 不刷新
|
// 强制整页跳转,避免仅更新 URL 不刷新
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.assign(targetUrl);
|
window.location.assign(targetUrl);
|
||||||
|
|||||||
@ -1,26 +1,11 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { createContext, useContext } from "react";
|
import { createContext, useContext } from "react";
|
||||||
import type { CanvasState } from "@/types/canvas";
|
|
||||||
|
|
||||||
export type CanvasRemoteOperationResult = {
|
|
||||||
projectId: string;
|
|
||||||
canvasData: CanvasState;
|
|
||||||
updatedAt: string;
|
|
||||||
};
|
|
||||||
export type CanvasRemoteOperation = (() => Promise<void>) | (() => Promise<CanvasRemoteOperationResult | undefined>);
|
|
||||||
|
|
||||||
export const CanvasRemoteOperationContext = createContext<
|
export const CanvasRemoteOperationContext = createContext<
|
||||||
{
|
((operation: () => Promise<void>) => Promise<void>) | null
|
||||||
run: (operation: CanvasRemoteOperation) => Promise<void>;
|
|
||||||
applySnapshot: (result: CanvasRemoteOperationResult) => boolean;
|
|
||||||
} | null
|
|
||||||
>(null);
|
>(null);
|
||||||
|
|
||||||
export function useCanvasRemoteOperation() {
|
export function useCanvasRemoteOperation() {
|
||||||
return useContext(CanvasRemoteOperationContext)?.run || null;
|
return useContext(CanvasRemoteOperationContext);
|
||||||
}
|
|
||||||
|
|
||||||
export function useCanvasRemoteSnapshot() {
|
|
||||||
return useContext(CanvasRemoteOperationContext)?.applySnapshot || null;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,10 +2,14 @@
|
|||||||
|
|
||||||
import { ReferenceIcon } from "@/components/lovart/reference-icon";
|
import { ReferenceIcon } from "@/components/lovart/reference-icon";
|
||||||
|
|
||||||
|
import { Loader2, Save } from "lucide-react";
|
||||||
|
|
||||||
type CanvasSurfaceHeaderProps = {
|
type CanvasSurfaceHeaderProps = {
|
||||||
|
isSaving: boolean;
|
||||||
onAddImage: () => void;
|
onAddImage: () => void;
|
||||||
onHome: () => void;
|
onHome: () => void;
|
||||||
onOpenFiles: () => void;
|
onOpenFiles: () => void;
|
||||||
|
onSave: () => void;
|
||||||
projectTitle: string;
|
projectTitle: string;
|
||||||
rightInset: number;
|
rightInset: number;
|
||||||
};
|
};
|
||||||
@ -14,9 +18,11 @@ const iconButtonClass =
|
|||||||
"inline-flex h-[28px] w-[28px] items-center justify-center rounded-[8px] p-0 text-[#858585] transition-colors hover:bg-black/5 focus-visible:outline-2 focus-visible:outline-offset-2 dark:hover:bg-white/10";
|
"inline-flex h-[28px] w-[28px] items-center justify-center rounded-[8px] p-0 text-[#858585] transition-colors hover:bg-black/5 focus-visible:outline-2 focus-visible:outline-offset-2 dark:hover:bg-white/10";
|
||||||
|
|
||||||
export function CanvasSurfaceHeader({
|
export function CanvasSurfaceHeader({
|
||||||
|
isSaving,
|
||||||
onAddImage,
|
onAddImage,
|
||||||
onHome,
|
onHome,
|
||||||
onOpenFiles,
|
onOpenFiles,
|
||||||
|
onSave,
|
||||||
projectTitle,
|
projectTitle,
|
||||||
rightInset,
|
rightInset,
|
||||||
}: CanvasSurfaceHeaderProps) {
|
}: CanvasSurfaceHeaderProps) {
|
||||||
@ -71,6 +77,19 @@ export function CanvasSurfaceHeader({
|
|||||||
<ReferenceIcon name="plus" size={16} />
|
<ReferenceIcon name="plus" size={16} />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
<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>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -152,7 +152,16 @@ export function shouldRetryCanvasViewOperationAfterRefresh(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export type { ImageGenerationStatusStreamPayload } from "@/lib/canvas/image-generation-status";
|
export type ImageGenerationStatusStreamPayload = {
|
||||||
|
phase?: "queued" | "processing" | "finalizing" | "failed" | "completed";
|
||||||
|
providerType?: string;
|
||||||
|
modelName?: string;
|
||||||
|
imageCount?: number;
|
||||||
|
queuePosition?: number;
|
||||||
|
sameModelRunningCount?: number;
|
||||||
|
concurrencyLimit?: number;
|
||||||
|
estimatedWaitMs?: number;
|
||||||
|
};
|
||||||
|
|
||||||
export type AgentTargetLockRefPreview = {
|
export type AgentTargetLockRefPreview = {
|
||||||
url: string;
|
url: string;
|
||||||
|
|||||||
@ -448,7 +448,7 @@ function extractChatMediaUrls(text: string, kind: ChatMediaKind): string[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const markdownLinkPattern = /(?<!!)\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi;
|
const markdownLinkPattern = /\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi;
|
||||||
while ((match = markdownLinkPattern.exec(text)) !== null) {
|
while ((match = markdownLinkPattern.exec(text)) !== null) {
|
||||||
const label = String(match[1] || "");
|
const label = String(match[1] || "");
|
||||||
const hintedKind = /音频|语音|声音|audio|mp3/i.test(label)
|
const hintedKind = /音频|语音|声音|audio|mp3/i.test(label)
|
||||||
@ -491,7 +491,7 @@ export function stripMediaMarkup(text: string): string {
|
|||||||
.replace(/<audio[\s\S]*?<\/audio>/gi, "")
|
.replace(/<audio[\s\S]*?<\/audio>/gi, "")
|
||||||
.replace(/<audio\b[^>]*\/?\s*>/gi, "")
|
.replace(/<audio\b[^>]*\/?\s*>/gi, "")
|
||||||
.replace(/<a[\s\S]*?<\/a>/gi, "")
|
.replace(/<a[\s\S]*?<\/a>/gi, "")
|
||||||
.replace(/(?<!!)\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi, (match, label, url) => {
|
.replace(/\[([^\]]*)]\((https?:\/\/[^)\s]+)\)/gi, (match, label, url) => {
|
||||||
const hintedMedia = /音频|语音|声音|audio|mp3|视频|影片|播放|video/i.test(
|
const hintedMedia = /音频|语音|声音|audio|mp3|视频|影片|播放|video/i.test(
|
||||||
String(label || "")
|
String(label || "")
|
||||||
);
|
);
|
||||||
|
|||||||
@ -6,8 +6,6 @@ import { ArrowUp, MessageSquare, X } from "lucide-react";
|
|||||||
import { useCallback, useEffect, useRef, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { CanvasAgentRunPanel } from "./canvas-agent-run-panel";
|
import { CanvasAgentRunPanel } from "./canvas-agent-run-panel";
|
||||||
import { BEFORE_PAGE_REFRESH, type PageRefreshRequest } from "@/lib/client/page-refresh";
|
|
||||||
import { readCanvasComposerDraft, writeCanvasComposerDraft, clearCanvasComposerDraft } from "@/lib/client/canvas-draft";
|
|
||||||
import {
|
import {
|
||||||
videoAgentSettingsSchema,
|
videoAgentSettingsSchema,
|
||||||
type VideoAgentSettings,
|
type VideoAgentSettings,
|
||||||
@ -96,22 +94,6 @@ export function VideoCanvasAgentPanel(props: {
|
|||||||
};
|
};
|
||||||
}, [props.projectId, setMessages]);
|
}, [props.projectId, setMessages]);
|
||||||
const busy = backgroundRunning || preparing || status === "submitted" || status === "streaming";
|
const busy = backgroundRunning || preparing || status === "submitted" || status === "streaming";
|
||||||
useEffect(() => {
|
|
||||||
const draft = readCanvasComposerDraft(`video-agent-${props.projectId}`);
|
|
||||||
if (draft) setInput(draft.text);
|
|
||||||
}, [props.projectId]);
|
|
||||||
useEffect(() => {
|
|
||||||
const beforeRefresh = (event: Event) => {
|
|
||||||
const request = (event as CustomEvent<PageRefreshRequest>).detail;
|
|
||||||
if (busy) request.block("当前视频任务仍在处理中,请等待结果返回后再刷新。原任务无需重新提交。");
|
|
||||||
request.save(async () => {
|
|
||||||
if (input.trim() && !writeCanvasComposerDraft(`video-agent-${props.projectId}`, { text: input, attachments: [] }))
|
|
||||||
throw new Error("浏览器无法保存当前输入,请保留此页并检查浏览器存储空间后重试。");
|
|
||||||
});
|
|
||||||
};
|
|
||||||
window.addEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
|
||||||
return () => window.removeEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
|
||||||
}, [busy, input, props.projectId]);
|
|
||||||
const submit = useCallback(
|
const submit = useCallback(
|
||||||
async (resumeRunId?: string) => {
|
async (resumeRunId?: string) => {
|
||||||
if (busy || loading) return;
|
if (busy || loading) return;
|
||||||
@ -123,7 +105,6 @@ export function VideoCanvasAgentPanel(props: {
|
|||||||
try {
|
try {
|
||||||
await latest.current.props.beforeSend();
|
await latest.current.props.beforeSend();
|
||||||
if (!mounted.current) return;
|
if (!mounted.current) return;
|
||||||
clearCanvasComposerDraft(`video-agent-${props.projectId}`);
|
|
||||||
resumeRef.current = resumeRunId;
|
resumeRef.current = resumeRunId;
|
||||||
setInput("");
|
setInput("");
|
||||||
await sendMessage({ text });
|
await sendMessage({ text });
|
||||||
@ -317,7 +298,7 @@ export function VideoCanvasAgentPanel(props: {
|
|||||||
<textarea
|
<textarea
|
||||||
aria-label="视频创作要求"
|
aria-label="视频创作要求"
|
||||||
value={input}
|
value={input}
|
||||||
onChange={(event) => { clearCanvasComposerDraft(`video-agent-${props.projectId}`); setInput(event.target.value); }}
|
onChange={(event) => setInput(event.target.value)}
|
||||||
maxLength={2000}
|
maxLength={2000}
|
||||||
rows={3}
|
rows={3}
|
||||||
placeholder="例如:先分析选中素材,写分镜,再生成一条5秒商品展示视频"
|
placeholder="例如:先分析选中素材,写分镜,再生成一条5秒商品展示视频"
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { CanvasPreviewImage } from "./image-node/canvas-preview-image";
|
|
||||||
import { canvasImagePreviewWidth } from "@/lib/canvas/image-preview-width";
|
|
||||||
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -62,7 +60,6 @@ import { DetailUiDomRenderer } from "@/components/canvas/DetailUiDomRenderer";
|
|||||||
import { ReferenceImageToolbar } from "@/components/canvas/ReferenceImageToolbar";
|
import { ReferenceImageToolbar } from "@/components/canvas/ReferenceImageToolbar";
|
||||||
import { CanvasToolbarIcon } from "@/components/canvas/CanvasToolbarIcon";
|
import { CanvasToolbarIcon } from "@/components/canvas/CanvasToolbarIcon";
|
||||||
import { CanvasImageDownloadProgressDialog } from "@/components/canvas/CanvasImageDownloadProgressDialog";
|
import { CanvasImageDownloadProgressDialog } from "@/components/canvas/CanvasImageDownloadProgressDialog";
|
||||||
import { ProgressiveImagePreview, type LoadedImagePreview } from "@/components/progressive-image-preview";
|
|
||||||
import {
|
import {
|
||||||
areImageAdjustmentsEqual,
|
areImageAdjustmentsEqual,
|
||||||
buildImageAdjustmentFilterStyle,
|
buildImageAdjustmentFilterStyle,
|
||||||
@ -189,7 +186,7 @@ import {
|
|||||||
normalizeDetailUiSubjectAnalysis,
|
normalizeDetailUiSubjectAnalysis,
|
||||||
} from "@/lib/detail-ui-subject-analysis";
|
} from "@/lib/detail-ui-subject-analysis";
|
||||||
import type { DetailUiScreenOverlaySnapshot } from "@/lib/detail-ui-surface-adapter";
|
import type { DetailUiScreenOverlaySnapshot } from "@/lib/detail-ui-surface-adapter";
|
||||||
import { buildImageFallbackUrl, buildImageProxyUrl } from "@/lib/image-delivery";
|
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
import {
|
import {
|
||||||
DEFAULT_PUBLIC_THREED_MODEL_ALIAS_CONFIG,
|
DEFAULT_PUBLIC_THREED_MODEL_ALIAS_CONFIG,
|
||||||
getDefaultThreeDModelAliasId,
|
getDefaultThreeDModelAliasId,
|
||||||
@ -1714,7 +1711,6 @@ export function ImageNode({
|
|||||||
const [isUploading, setIsUploading] = useState(false);
|
const [isUploading, setIsUploading] = useState(false);
|
||||||
const [annotatorOpen, setAnnotatorOpen] = useState(false);
|
const [annotatorOpen, setAnnotatorOpen] = useState(false);
|
||||||
const [fullscreenPreviewOpen, setFullscreenPreviewOpen] = useState(false);
|
const [fullscreenPreviewOpen, setFullscreenPreviewOpen] = useState(false);
|
||||||
const [fullscreenPreviewImage, setFullscreenPreviewImage] = useState<LoadedImagePreview | null>(null);
|
|
||||||
const [downloadProgress, setDownloadProgress] =
|
const [downloadProgress, setDownloadProgress] =
|
||||||
useState<CanvasImageDownloadProgress | null>(null);
|
useState<CanvasImageDownloadProgress | null>(null);
|
||||||
const downloadAbortControllerRef = useRef<AbortController | null>(null);
|
const downloadAbortControllerRef = useRef<AbortController | null>(null);
|
||||||
@ -2504,13 +2500,13 @@ export function ImageNode({
|
|||||||
const densityMultiplier = shouldUseHighFidelityDisplayImage ? 1.35 : 1.05;
|
const densityMultiplier = shouldUseHighFidelityDisplayImage ? 1.35 : 1.05;
|
||||||
const minWidth = shouldUseHighFidelityDisplayImage ? 960 : 640;
|
const minWidth = shouldUseHighFidelityDisplayImage ? 960 : 640;
|
||||||
const maxWidth = shouldUseHighFidelityDisplayImage ? 4096 : 2600;
|
const maxWidth = shouldUseHighFidelityDisplayImage ? 4096 : 2600;
|
||||||
return canvasImagePreviewWidth(Math.max(
|
return Math.max(
|
||||||
minWidth,
|
minWidth,
|
||||||
Math.min(
|
Math.min(
|
||||||
maxWidth,
|
maxWidth,
|
||||||
Math.round((node.size.width || 320) * zoomMultiplier * devicePixelRatio * densityMultiplier)
|
Math.round((node.size.width || 320) * zoomMultiplier * devicePixelRatio * densityMultiplier)
|
||||||
)
|
)
|
||||||
), maxWidth);
|
);
|
||||||
}, [node.size.width, shouldUseHighFidelityDisplayImage, viewportZoom]);
|
}, [node.size.width, shouldUseHighFidelityDisplayImage, viewportZoom]);
|
||||||
const displayImageQuality = shouldUseHighFidelityDisplayImage ? 92 : 88;
|
const displayImageQuality = shouldUseHighFidelityDisplayImage ? 92 : 88;
|
||||||
const displayImageSrc = useMemo(
|
const displayImageSrc = useMemo(
|
||||||
@ -4685,44 +4681,106 @@ export function ImageNode({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!projectId || !runCanvasRemoteOperation) {
|
setIsCreatingDetachedResultNode(true);
|
||||||
toast.error("画布还在加载,请稍后再试");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setIsCreatingDetachedResultNode(false);
|
|
||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
const actionName = "框选抠图";
|
const actionName = "框选抠图";
|
||||||
|
const newNodeId = addNode({
|
||||||
try {
|
type: "image",
|
||||||
const command = createCanvasCapabilityClientCommand({
|
position: {
|
||||||
capabilityId: "image.box_cutout",
|
x: node.position.x + node.size.width + 20,
|
||||||
nodeIds: [node.id],
|
y: node.position.y,
|
||||||
projectId,
|
},
|
||||||
source: "toolbar",
|
size: { width: node.size.width, height: node.size.height },
|
||||||
args: {
|
data: {
|
||||||
...boxCutoutSelection,
|
src: "",
|
||||||
|
status: "generating",
|
||||||
|
prompt: `${actionName}处理中...`,
|
||||||
|
title: `${displayTitle}-${actionName}`,
|
||||||
|
originalSrc: data.src,
|
||||||
|
metadata: {
|
||||||
|
cropRect: boxCutoutSelection,
|
||||||
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
connections: [node.id],
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await imageGenerationService.boxCutout({
|
||||||
|
imageUrl: data.src,
|
||||||
|
cropRect: boxCutoutSelection,
|
||||||
|
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
||||||
|
sourceNodeId: node.id,
|
||||||
});
|
});
|
||||||
await runCanvasRemoteOperation(() => executeCanvasToolbarCapability(command));
|
|
||||||
toast.success(`${actionName}完成`);
|
if (result.success && result.data?.url) {
|
||||||
|
updateNode(newNodeId, {
|
||||||
|
data: {
|
||||||
|
src: result.data.url,
|
||||||
|
status: "completed",
|
||||||
|
prompt: `${actionName}结果`,
|
||||||
|
title: `${displayTitle}-${actionName}`,
|
||||||
|
originalSrc: data.src,
|
||||||
|
metadata: {
|
||||||
|
cropRect: boxCutoutSelection,
|
||||||
|
cropRectApplied: result.data.cropRectApplied,
|
||||||
|
paddedCropRectApplied: result.data.paddedCropRectApplied,
|
||||||
|
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
||||||
|
editMode: "box-cutout",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
toast.success(`${actionName}完成`);
|
||||||
|
} else {
|
||||||
|
updateNode(newNodeId, {
|
||||||
|
data: {
|
||||||
|
src: "",
|
||||||
|
status: "failed",
|
||||||
|
prompt: `${actionName}失败`,
|
||||||
|
title: `${displayTitle}-${actionName}-失败`,
|
||||||
|
originalSrc: data.src,
|
||||||
|
metadata: {
|
||||||
|
cropRect: boxCutoutSelection,
|
||||||
|
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
toast.error(result.error || `${actionName}失败`);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("框选抠图失败:", error);
|
console.error("框选抠图失败:", error);
|
||||||
toast.error(error instanceof Error ? error.message : `${actionName}失败`);
|
updateNode(newNodeId, {
|
||||||
|
data: {
|
||||||
|
src: "",
|
||||||
|
status: "failed",
|
||||||
|
prompt: `${actionName}失败`,
|
||||||
|
title: `${displayTitle}-${actionName}-失败`,
|
||||||
|
originalSrc: data.src,
|
||||||
|
metadata: {
|
||||||
|
cropRect: boxCutoutSelection,
|
||||||
|
paddingRatio: BOX_CUTOUT_DEFAULT_PADDING_RATIO,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
toast.error(`${actionName}失败,请重试`);
|
||||||
} finally {
|
} finally {
|
||||||
setIsCreatingDetachedResultNode(false);
|
setIsCreatingDetachedResultNode(false);
|
||||||
setIsProcessing(false);
|
setIsProcessing(false);
|
||||||
resetBoxCutoutState();
|
resetBoxCutoutState();
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
|
addNode,
|
||||||
boxCutoutSelection,
|
boxCutoutSelection,
|
||||||
data.src,
|
data.src,
|
||||||
|
displayTitle,
|
||||||
isProcessing,
|
isProcessing,
|
||||||
node.id,
|
node.id,
|
||||||
projectId,
|
node.position.x,
|
||||||
|
node.position.y,
|
||||||
|
node.size.height,
|
||||||
|
node.size.width,
|
||||||
resetBoxCutoutState,
|
resetBoxCutoutState,
|
||||||
runCanvasRemoteOperation,
|
updateNode,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const animateAddToChat = useCallback(async (previewUrl?: string) => {
|
const animateAddToChat = useCallback(async (previewUrl?: string) => {
|
||||||
@ -8401,16 +8459,15 @@ export function ImageNode({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="absolute inset-0 bg-slate-50 dark:bg-zinc-900" />
|
<div className="absolute inset-0 bg-slate-50 dark:bg-zinc-900" />
|
||||||
<CanvasPreviewImage
|
<img
|
||||||
sourceKey={data.src}
|
|
||||||
alt={displayTitle}
|
alt={displayTitle}
|
||||||
className="absolute inset-0 h-full w-full object-cover"
|
className="absolute inset-0 h-full w-full object-cover"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
draggable={false}
|
draggable={false}
|
||||||
loading="eager"
|
loading="lazy"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(data.src)) {
|
if (event.currentTarget.src !== data.src) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(data.src);
|
event.currentTarget.src = data.src;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onLoad={handleDisplayImageLoad}
|
onLoad={handleDisplayImageLoad}
|
||||||
@ -8431,16 +8488,15 @@ export function ImageNode({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="absolute inset-0 bg-slate-50 dark:bg-zinc-900" />
|
<div className="absolute inset-0 bg-slate-50 dark:bg-zinc-900" />
|
||||||
<CanvasPreviewImage
|
<img
|
||||||
sourceKey={compareSource}
|
|
||||||
alt={`${displayTitle}-原图`}
|
alt={`${displayTitle}-原图`}
|
||||||
className="absolute inset-0 h-full w-full object-cover"
|
className="absolute inset-0 h-full w-full object-cover"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
draggable={false}
|
draggable={false}
|
||||||
loading="eager"
|
loading="lazy"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(compareSource)) {
|
if (event.currentTarget.src !== compareSource) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(compareSource);
|
event.currentTarget.src = compareSource;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
src={displayCompareSrc}
|
src={displayCompareSrc}
|
||||||
@ -8482,8 +8538,7 @@ export function ImageNode({
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<CanvasPreviewImage
|
<img
|
||||||
sourceKey={data.src}
|
|
||||||
alt={displayTitle}
|
alt={displayTitle}
|
||||||
className={cn(
|
className={cn(
|
||||||
"h-full w-full bg-slate-50 object-contain dark:bg-zinc-900",
|
"h-full w-full bg-slate-50 object-contain dark:bg-zinc-900",
|
||||||
@ -8495,7 +8550,7 @@ export function ImageNode({
|
|||||||
)}
|
)}
|
||||||
decoding="async"
|
decoding="async"
|
||||||
draggable={false}
|
draggable={false}
|
||||||
loading="eager"
|
loading="lazy"
|
||||||
onClick={
|
onClick={
|
||||||
isThisNodePicking && !boxCutoutMode
|
isThisNodePicking && !boxCutoutMode
|
||||||
? handlePickerClick
|
? handlePickerClick
|
||||||
@ -8504,17 +8559,12 @@ export function ImageNode({
|
|||||||
onDoubleClick={(e) => {
|
onDoubleClick={(e) => {
|
||||||
if (!isThisNodePicking && !boxCutoutMode && !localEditMode) {
|
if (!isThisNodePicking && !boxCutoutMode && !localEditMode) {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
setFullscreenPreviewImage({
|
|
||||||
src: e.currentTarget.currentSrc || e.currentTarget.src,
|
|
||||||
width: e.currentTarget.naturalWidth,
|
|
||||||
height: e.currentTarget.naturalHeight,
|
|
||||||
});
|
|
||||||
setFullscreenPreviewOpen(true);
|
setFullscreenPreviewOpen(true);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(data.src)) {
|
if (event.currentTarget.src !== data.src) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(data.src);
|
event.currentTarget.src = data.src;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onLoad={handleDisplayImageLoad}
|
onLoad={handleDisplayImageLoad}
|
||||||
@ -9425,8 +9475,6 @@ export function ImageNode({
|
|||||||
) : (
|
) : (
|
||||||
<ImageNodeEmptyState
|
<ImageNodeEmptyState
|
||||||
detailProgressLabel={detailProgressLabel}
|
detailProgressLabel={detailProgressLabel}
|
||||||
failure={data.metadata?.generationFailure}
|
|
||||||
requestId={data.metadata?.generationRequestId}
|
|
||||||
fileInputRef={fileInputRef}
|
fileInputRef={fileInputRef}
|
||||||
generationProgress={generationProgress}
|
generationProgress={generationProgress}
|
||||||
onImageUpload={handleImageUpload}
|
onImageUpload={handleImageUpload}
|
||||||
@ -11916,7 +11964,6 @@ export function ImageNode({
|
|||||||
{/* 双击放大预览弹窗 */}
|
{/* 双击放大预览弹窗 */}
|
||||||
{fullscreenPreviewOpen &&
|
{fullscreenPreviewOpen &&
|
||||||
data.src &&
|
data.src &&
|
||||||
fullscreenPreviewImage &&
|
|
||||||
createPortal(
|
createPortal(
|
||||||
<div
|
<div
|
||||||
className="fixed inset-0 z-[9999] flex cursor-zoom-out items-center justify-center bg-black/90"
|
className="fixed inset-0 z-[9999] flex cursor-zoom-out items-center justify-center bg-black/90"
|
||||||
@ -11924,7 +11971,7 @@ export function ImageNode({
|
|||||||
>
|
>
|
||||||
{/* 关闭按钮 */}
|
{/* 关闭按钮 */}
|
||||||
<button
|
<button
|
||||||
className="absolute top-4 right-4 z-10 rounded-full bg-slate-950 p-2 transition-colors hover:bg-black"
|
className="absolute top-4 right-4 rounded-full bg-slate-950 p-2 transition-colors hover:bg-black"
|
||||||
onClick={() => setFullscreenPreviewOpen(false)}
|
onClick={() => setFullscreenPreviewOpen(false)}
|
||||||
>
|
>
|
||||||
<svg
|
<svg
|
||||||
@ -11942,7 +11989,7 @@ export function ImageNode({
|
|||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
className="absolute top-4 right-16 z-10 inline-flex items-center gap-1.5 rounded-full bg-slate-950 px-3 py-2 text-sm text-white transition-colors hover:bg-black disabled:cursor-not-allowed disabled:opacity-60"
|
className="absolute top-4 right-16 inline-flex items-center gap-1.5 rounded-full bg-slate-950 px-3 py-2 text-sm text-white transition-colors hover:bg-black disabled:cursor-not-allowed disabled:opacity-60"
|
||||||
disabled={isDownloadingOriginalImage}
|
disabled={isDownloadingOriginalImage}
|
||||||
onClick={(event) => {
|
onClick={(event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@ -11961,11 +12008,13 @@ export function ImageNode({
|
|||||||
</button>
|
</button>
|
||||||
|
|
||||||
{/* 图片 */}
|
{/* 图片 */}
|
||||||
<ProgressiveImagePreview
|
<img
|
||||||
alt={data.prompt || "图片预览"}
|
alt={data.prompt || "图片预览"}
|
||||||
className="max-h-[95vh] max-w-[95vw] object-contain"
|
className="max-h-[95vh] max-w-[95vw] object-contain"
|
||||||
src={resolveOriginalImageUrl(data.src) || data.src}
|
decoding="async"
|
||||||
preview={fullscreenPreviewImage}
|
draggable={false}
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
src={fullscreenPreviewSrc}
|
||||||
style={activeImageAdjustmentsStyle}
|
style={activeImageAdjustmentsStyle}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@ -1,32 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { forwardRef, type ImgHTMLAttributes, useState } from "react";
|
|
||||||
|
|
||||||
type Props = Omit<ImgHTMLAttributes<HTMLImageElement>, "src"> & { src: string; sourceKey: string };
|
|
||||||
|
|
||||||
export const CanvasPreviewImage = forwardRef<HTMLImageElement, Props>(function CanvasPreviewImage(
|
|
||||||
{ src, sourceKey, onLoad, style, ...props }, ref,
|
|
||||||
) {
|
|
||||||
const [displayed, setDisplayed] = useState({ sourceKey, src });
|
|
||||||
const visibleSrc = displayed.sourceKey === sourceKey ? displayed.src : src;
|
|
||||||
const sources = visibleSrc === src ? [src] : [visibleSrc, src];
|
|
||||||
return <>{sources.map(candidate => {
|
|
||||||
const visible = candidate === visibleSrc;
|
|
||||||
return <img
|
|
||||||
{...props}
|
|
||||||
key={candidate}
|
|
||||||
ref={visible ? ref : undefined}
|
|
||||||
src={candidate}
|
|
||||||
alt={visible ? props.alt : ""}
|
|
||||||
aria-hidden={visible ? props["aria-hidden"] : true}
|
|
||||||
style={visible ? style : { ...style, position: "absolute", inset: 0, opacity: 0, pointerEvents: "none" }}
|
|
||||||
onLoad={event => {
|
|
||||||
const image = event.currentTarget;
|
|
||||||
onLoad?.(event);
|
|
||||||
void image.decode().then(() => {
|
|
||||||
if (image.isConnected && candidate === src) setDisplayed({ sourceKey, src: candidate });
|
|
||||||
}).catch(() => undefined);
|
|
||||||
}}
|
|
||||||
/>;
|
|
||||||
})}</>;
|
|
||||||
});
|
|
||||||
@ -3,14 +3,11 @@
|
|||||||
import { AlertCircle, ImagePlus, Loader2 } from "lucide-react";
|
import { AlertCircle, ImagePlus, Loader2 } from "lucide-react";
|
||||||
import type React from "react";
|
import type React from "react";
|
||||||
import { GenerationDotPlaceholder } from "@/components/generation-dot-placeholder";
|
import { GenerationDotPlaceholder } from "@/components/generation-dot-placeholder";
|
||||||
import type { RequestFailureDetails } from "@/lib/request-error";
|
|
||||||
|
|
||||||
interface ImageNodeEmptyStateProps {
|
interface ImageNodeEmptyStateProps {
|
||||||
detailProgressLabel?: string;
|
detailProgressLabel?: string;
|
||||||
fileInputRef: React.Ref<HTMLInputElement>;
|
fileInputRef: React.Ref<HTMLInputElement>;
|
||||||
generationProgress?: number | null;
|
generationProgress?: number | null;
|
||||||
failure?: RequestFailureDetails;
|
|
||||||
requestId?: string;
|
|
||||||
onImageUpload: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
onImageUpload: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
||||||
onSelectFile: (event: React.MouseEvent<HTMLButtonElement>) => void;
|
onSelectFile: (event: React.MouseEvent<HTMLButtonElement>) => void;
|
||||||
status?: string;
|
status?: string;
|
||||||
@ -55,8 +52,6 @@ export function ImageNodeEmptyState({
|
|||||||
detailProgressLabel,
|
detailProgressLabel,
|
||||||
fileInputRef,
|
fileInputRef,
|
||||||
generationProgress,
|
generationProgress,
|
||||||
failure,
|
|
||||||
requestId,
|
|
||||||
onImageUpload,
|
onImageUpload,
|
||||||
onSelectFile,
|
onSelectFile,
|
||||||
status,
|
status,
|
||||||
@ -94,7 +89,7 @@ export function ImageNodeEmptyState({
|
|||||||
if (status === "failed") {
|
if (status === "failed") {
|
||||||
const failedTitle = detailProgressLabel || "生成失败";
|
const failedTitle = detailProgressLabel || "生成失败";
|
||||||
return (
|
return (
|
||||||
<div role="alert" className="flex h-full w-full flex-col items-center justify-center overflow-auto bg-[#fff7f8] px-6 text-center dark:bg-red-950/40">
|
<div className="flex h-full w-full select-none flex-col items-center justify-center bg-[#fff7f8] px-6 text-center dark:bg-red-950/40">
|
||||||
<span className="mb-3 flex h-12 w-12 items-center justify-center rounded-[18px] border border-[#f2d7dd] bg-white text-[#d24f63] shadow-[0_0_0_1px_rgba(242,215,221,0.22)]">
|
<span className="mb-3 flex h-12 w-12 items-center justify-center rounded-[18px] border border-[#f2d7dd] bg-white text-[#d24f63] shadow-[0_0_0_1px_rgba(242,215,221,0.22)]">
|
||||||
<AlertCircle className="h-5 w-5" />
|
<AlertCircle className="h-5 w-5" />
|
||||||
</span>
|
</span>
|
||||||
@ -102,9 +97,8 @@ export function ImageNodeEmptyState({
|
|||||||
{failedTitle}
|
{failedTitle}
|
||||||
</span>
|
</span>
|
||||||
<span className="mt-1 text-xs text-[#b77480]">
|
<span className="mt-1 text-xs text-[#b77480]">
|
||||||
{failure?.recommendation || (failedTitle.includes("上传") ? "请重新选择文件" : "请查看对话中的错误原因和处理建议")}
|
{failedTitle.includes("上传") ? "请重新选择文件" : "请重新上传或再试一次"}
|
||||||
</span>
|
</span>
|
||||||
{requestId ? <span className="mt-2 select-text break-all text-xs text-[#b77480]">请求编号:{requestId}</span> : null}
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ import {
|
|||||||
splitOpenClawChartBlocks,
|
splitOpenClawChartBlocks,
|
||||||
} from "@/components/elements/openclaw-chart-card";
|
} from "@/components/elements/openclaw-chart-card";
|
||||||
import { ResponsePdfDownloadButton } from "@/components/elements/response-pdf-download";
|
import { ResponsePdfDownloadButton } from "@/components/elements/response-pdf-download";
|
||||||
import { buildImageFallbackUrl, buildImageProxyUrl } from "@/lib/image-delivery";
|
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
import { splitMarkdownImages } from "@/lib/canvas/markdown-image-segments";
|
import { splitMarkdownImages } from "@/lib/canvas/markdown-image-segments";
|
||||||
import { stableStreamingMarkdown } from "@/lib/canvas/markdown-image-streaming";
|
import { stableStreamingMarkdown } from "@/lib/canvas/markdown-image-streaming";
|
||||||
import { getGatewayErrorPageMessage } from "@/lib/request-error";
|
import { getGatewayErrorPageMessage } from "@/lib/request-error";
|
||||||
@ -442,8 +442,8 @@ function MarkdownTableImagePreview({
|
|||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(src)) {
|
if (event.currentTarget.src !== src) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(src);
|
event.currentTarget.src = src;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -473,8 +473,8 @@ function MarkdownTableImagePreview({
|
|||||||
decoding="async"
|
decoding="async"
|
||||||
onClick={(event) => event.stopPropagation()}
|
onClick={(event) => event.stopPropagation()}
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (event.currentTarget.src !== buildImageFallbackUrl(originalSrc)) {
|
if (event.currentTarget.src !== originalSrc) {
|
||||||
event.currentTarget.src = buildImageFallbackUrl(originalSrc);
|
event.currentTarget.src = originalSrc;
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect, useMemo, useState } from "react";
|
||||||
import { BloubAvatar } from "@/components/bloub-avatar";
|
import { BloubAvatar } from "@/components/bloub-avatar";
|
||||||
import type { StateId } from "@/lib/bloub-official/states";
|
import type { StateId } from "@/lib/bloub-official/states";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
@ -25,17 +26,193 @@ interface GenerationDotPlaceholderProps {
|
|||||||
const PHASE_TITLES: Record<GenerationPhase, string> = {
|
const PHASE_TITLES: Record<GenerationPhase, string> = {
|
||||||
idle: "正在创建图片",
|
idle: "正在创建图片",
|
||||||
queued: "等待开始生成",
|
queued: "等待开始生成",
|
||||||
processing: "等待模型返回图片",
|
processing: "生成初稿中",
|
||||||
finalizing: "正在保存生成结果",
|
finalizing: "正在润饰细节",
|
||||||
failed: "生成失败",
|
failed: "生成失败",
|
||||||
completed: "生成完成",
|
completed: "生成完成",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const GENERATION_STATUS_TITLES = [
|
||||||
|
"正在理解画面关系",
|
||||||
|
"正在读取参考图片",
|
||||||
|
"正在分析主体轮廓",
|
||||||
|
"正在识别关键元素",
|
||||||
|
"正在整理构图线索",
|
||||||
|
"正在确认画面比例",
|
||||||
|
"正在匹配视觉风格",
|
||||||
|
"正在提取材质细节",
|
||||||
|
"正在梳理光影方向",
|
||||||
|
"正在构建初始画面",
|
||||||
|
"正在生成第一版构图",
|
||||||
|
"正在安排主体位置",
|
||||||
|
"正在校准画面重心",
|
||||||
|
"正在补全背景层次",
|
||||||
|
"正在保持主体一致",
|
||||||
|
"正在优化边缘结构",
|
||||||
|
"正在调整透视关系",
|
||||||
|
"正在细化空间层次",
|
||||||
|
"正在增强画面清晰度",
|
||||||
|
"正在统一色彩倾向",
|
||||||
|
"正在检查参考一致性",
|
||||||
|
"正在处理局部细节",
|
||||||
|
"正在补充纹理信息",
|
||||||
|
"正在修正不自然区域",
|
||||||
|
"正在平衡明暗对比",
|
||||||
|
"正在优化产品质感",
|
||||||
|
"正在调整阴影落点",
|
||||||
|
"正在整理背景干扰",
|
||||||
|
"正在增强真实感",
|
||||||
|
"正在重绘细小结构",
|
||||||
|
"正在合成高清结果",
|
||||||
|
"正在检查主体完整性",
|
||||||
|
"正在优化画面边缘",
|
||||||
|
"正在处理反光细节",
|
||||||
|
"正在提升材质表现",
|
||||||
|
"正在微调构图节奏",
|
||||||
|
"正在清理多余噪点",
|
||||||
|
"正在强化视觉焦点",
|
||||||
|
"正在校正颜色偏差",
|
||||||
|
"正在保留参考特征",
|
||||||
|
"正在生成高分辨率版本",
|
||||||
|
"正在压实画面层次",
|
||||||
|
"正在修饰局部瑕疵",
|
||||||
|
"正在检查文字与图形",
|
||||||
|
"正在稳定画面结构",
|
||||||
|
"正在优化细节密度",
|
||||||
|
"正在完善背景过渡",
|
||||||
|
"正在增强主体质感",
|
||||||
|
"正在统一整体光感",
|
||||||
|
"正在检查比例关系",
|
||||||
|
"正在修正边缘锯齿",
|
||||||
|
"正在提升清晰观感",
|
||||||
|
"正在处理最后细节",
|
||||||
|
"正在准备预览结果",
|
||||||
|
"正在同步生成进度",
|
||||||
|
"正在确认输出尺寸",
|
||||||
|
"正在整理结果文件",
|
||||||
|
"正在校验图片可用性",
|
||||||
|
"正在生成最终预览",
|
||||||
|
"正在完成高清渲染",
|
||||||
|
"正在保存生成结果",
|
||||||
|
"正在写入画布节点",
|
||||||
|
"正在同步到对话框",
|
||||||
|
"正在检查结果完整性",
|
||||||
|
"正在收尾生成任务",
|
||||||
|
"正在等待服务商返回",
|
||||||
|
"正在保持连接状态",
|
||||||
|
"正在确认生成结果",
|
||||||
|
"正在拉取图片地址",
|
||||||
|
"正在准备展示图片",
|
||||||
|
"正在完成最后处理",
|
||||||
|
"正在刷新画布内容",
|
||||||
|
"正在整理图片信息",
|
||||||
|
"正在完成本次生成",
|
||||||
|
"正在即将展示结果",
|
||||||
|
];
|
||||||
|
|
||||||
|
const LONG_WAIT_TITLE_THRESHOLD_MS = 5 * 60 * 1000;
|
||||||
|
const LONG_WAIT_STATUS_TITLES = [
|
||||||
|
"服务繁忙,排队处理中",
|
||||||
|
"高峰期生成较慢,请耐心等候",
|
||||||
|
"仍在保持连接,等待模型返回",
|
||||||
|
"结果返回后会自动展示",
|
||||||
|
];
|
||||||
|
const STATUS_TITLE_DURATIONS_MS = GENERATION_STATUS_TITLES.map((_, index) => {
|
||||||
|
const rhythm = [3600, 5200, 4100, 6100, 3300, 4700, 5700, 3900, 6800, 4400];
|
||||||
|
return rhythm[index % rhythm.length];
|
||||||
|
});
|
||||||
|
const TYPEWRITER_START_DELAY_MS = 120;
|
||||||
|
const TYPEWRITER_BASE_STEP_MS = 46;
|
||||||
|
|
||||||
function clampProgress(value: number) {
|
function clampProgress(value: number) {
|
||||||
if (!Number.isFinite(value)) return 0;
|
if (!Number.isFinite(value)) return 0;
|
||||||
return Math.max(0, Math.min(100, Math.round(value)));
|
return Math.max(0, Math.min(100, Math.round(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function estimateProgress(phase: GenerationPhase, elapsedMs: number) {
|
||||||
|
if (phase === "completed" || phase === "failed") return 100;
|
||||||
|
if (phase === "finalizing") return 96;
|
||||||
|
if (phase === "queued") {
|
||||||
|
return clampProgress(6 + Math.min(12, elapsedMs / 3500));
|
||||||
|
}
|
||||||
|
if (phase === "processing") {
|
||||||
|
const eased = 1 - Math.exp(-elapsedMs / 85_000);
|
||||||
|
return clampProgress(18 + eased * 78);
|
||||||
|
}
|
||||||
|
return clampProgress(Math.min(8, elapsedMs / 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getStatusTitleState(elapsedMs: number, initialTitle?: string) {
|
||||||
|
const titles = initialTitle
|
||||||
|
? [
|
||||||
|
initialTitle,
|
||||||
|
...GENERATION_STATUS_TITLES.filter((item) => item !== initialTitle),
|
||||||
|
]
|
||||||
|
: GENERATION_STATUS_TITLES;
|
||||||
|
const durations = initialTitle
|
||||||
|
? [4200, ...STATUS_TITLE_DURATIONS_MS]
|
||||||
|
: STATUS_TITLE_DURATIONS_MS;
|
||||||
|
|
||||||
|
let cursor = Math.max(0, elapsedMs);
|
||||||
|
for (let index = 0; index < titles.length; index += 1) {
|
||||||
|
const duration = durations[index] || 4600;
|
||||||
|
if (cursor < duration) {
|
||||||
|
return {
|
||||||
|
elapsedInTitle: cursor,
|
||||||
|
index,
|
||||||
|
title:
|
||||||
|
titles[index] ||
|
||||||
|
titles[titles.length - 1] ||
|
||||||
|
PHASE_TITLES.processing,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
cursor -= duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
elapsedInTitle: durations[durations.length - 1] || 4600,
|
||||||
|
index: titles.length - 1,
|
||||||
|
title: titles[titles.length - 1] || PHASE_TITLES.processing,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLongWaitTitleState(elapsedMs: number, initialTitle?: string) {
|
||||||
|
const titles = initialTitle
|
||||||
|
? [
|
||||||
|
initialTitle,
|
||||||
|
...LONG_WAIT_STATUS_TITLES.filter((item) => item !== initialTitle),
|
||||||
|
]
|
||||||
|
: LONG_WAIT_STATUS_TITLES;
|
||||||
|
const elapsedAfterThreshold = Math.max(
|
||||||
|
0,
|
||||||
|
elapsedMs - LONG_WAIT_TITLE_THRESHOLD_MS
|
||||||
|
);
|
||||||
|
const duration = 7600;
|
||||||
|
const index = Math.floor(elapsedAfterThreshold / duration) % titles.length;
|
||||||
|
|
||||||
|
return {
|
||||||
|
elapsedInTitle: elapsedAfterThreshold % duration,
|
||||||
|
index,
|
||||||
|
title: titles[index] || titles[0] || PHASE_TITLES.processing,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTypewriterText(text: string, elapsedInTitle: number, index: number) {
|
||||||
|
const chars = Array.from(text);
|
||||||
|
if (chars.length === 0) return "";
|
||||||
|
const step = TYPEWRITER_BASE_STEP_MS + (index % 5) * 8;
|
||||||
|
const visibleCount = Math.max(
|
||||||
|
1,
|
||||||
|
Math.min(
|
||||||
|
chars.length,
|
||||||
|
Math.floor(
|
||||||
|
Math.max(0, elapsedInTitle - TYPEWRITER_START_DELAY_MS) / step
|
||||||
|
) + 1
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return chars.slice(0, visibleCount).join("");
|
||||||
|
}
|
||||||
|
|
||||||
function getBloubState(phase: GenerationPhase): StateId {
|
function getBloubState(phase: GenerationPhase): StateId {
|
||||||
if (phase === "failed") return "alert";
|
if (phase === "failed") return "alert";
|
||||||
if (phase === "completed") return "wink";
|
if (phase === "completed") return "wink";
|
||||||
@ -51,10 +228,39 @@ export function GenerationDotPlaceholder({
|
|||||||
showProgress = true,
|
showProgress = true,
|
||||||
title,
|
title,
|
||||||
}: GenerationDotPlaceholderProps) {
|
}: GenerationDotPlaceholderProps) {
|
||||||
const resolvedProgress = typeof progress === "number" && Number.isFinite(progress)
|
const [elapsedMs, setElapsedMs] = useState(0);
|
||||||
? clampProgress(progress) : null;
|
|
||||||
const resolvedTitle = title || PHASE_TITLES[phase];
|
useEffect(() => {
|
||||||
|
const startedAt = Date.now();
|
||||||
|
const timer = window.setInterval(() => {
|
||||||
|
setElapsedMs(Date.now() - startedAt);
|
||||||
|
}, 90);
|
||||||
|
return () => window.clearInterval(timer);
|
||||||
|
}, [phase]);
|
||||||
|
|
||||||
|
const resolvedProgress = clampProgress(
|
||||||
|
typeof progress === "number" ? progress : estimateProgress(phase, elapsedMs)
|
||||||
|
);
|
||||||
|
const isLongWaitTitle =
|
||||||
|
(phase === "processing" || phase === "finalizing") &&
|
||||||
|
elapsedMs >= LONG_WAIT_TITLE_THRESHOLD_MS;
|
||||||
|
const statusTitleState = isLongWaitTitle
|
||||||
|
? getLongWaitTitleState(elapsedMs, title)
|
||||||
|
: getStatusTitleState(elapsedMs, title);
|
||||||
|
const rotatingTitle = getTypewriterText(
|
||||||
|
statusTitleState.title,
|
||||||
|
statusTitleState.elapsedInTitle,
|
||||||
|
statusTitleState.index
|
||||||
|
);
|
||||||
|
const resolvedTitle =
|
||||||
|
phase === "processing" || phase === "finalizing"
|
||||||
|
? rotatingTitle
|
||||||
|
: title || PHASE_TITLES[phase];
|
||||||
const playSequence = phase === "processing" || phase === "finalizing";
|
const playSequence = phase === "processing" || phase === "finalizing";
|
||||||
|
const progressStyle = useMemo(
|
||||||
|
() => ({ width: `${resolvedProgress}%` }),
|
||||||
|
[resolvedProgress]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@ -75,6 +281,10 @@ export function GenerationDotPlaceholder({
|
|||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{resolvedTitle}
|
{resolvedTitle}
|
||||||
|
<span
|
||||||
|
aria-hidden="true"
|
||||||
|
className="ml-1 inline-block h-[1em] w-[0.08em] translate-y-[0.15em] animate-[generationCursorBlink_1s_steps(1,end)_infinite] bg-current"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex min-h-0 flex-1 items-center justify-center py-3">
|
<div className="flex min-h-0 flex-1 items-center justify-center py-3">
|
||||||
@ -87,13 +297,13 @@ export function GenerationDotPlaceholder({
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{showProgress && phase !== "completed" ? (
|
{showProgress ? (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<div className="flex items-center justify-between text-[12px] font-medium text-[#7d838b] dark:text-zinc-400">
|
<div className="flex items-center justify-between text-[12px] font-medium text-[#7d838b] dark:text-zinc-400">
|
||||||
<span>{phase === "failed" ? "已停止" : resolvedProgress === null ? "等待结果,返回后自动展示" : "生成进度"}</span>
|
<span>{phase === "failed" ? "已停止" : "生成进度"}</span>
|
||||||
{resolvedProgress !== null ? <span>{resolvedProgress}%</span> : null}
|
<span>{resolvedProgress}%</span>
|
||||||
</div>
|
</div>
|
||||||
{resolvedProgress !== null ? <div role="progressbar" aria-valuenow={resolvedProgress} aria-valuemin={0} aria-valuemax={100} className="h-1.5 overflow-hidden rounded-full bg-white/80 shadow-[inset_0_0_0_1px_rgba(0,0,0,0.035)] dark:bg-white/10 dark:shadow-none">
|
<div className="h-1.5 overflow-hidden rounded-full bg-white/80 shadow-[inset_0_0_0_1px_rgba(0,0,0,0.035)] dark:bg-white/10 dark:shadow-none">
|
||||||
<div
|
<div
|
||||||
className={cn(
|
className={cn(
|
||||||
"h-full rounded-full transition-[width] duration-700 ease-out",
|
"h-full rounded-full transition-[width] duration-700 ease-out",
|
||||||
@ -101,12 +311,25 @@ export function GenerationDotPlaceholder({
|
|||||||
? "bg-[#c7ccd3] dark:bg-zinc-600"
|
? "bg-[#c7ccd3] dark:bg-zinc-600"
|
||||||
: "bg-[#0a0a0c] dark:bg-[#3ecf8e]"
|
: "bg-[#0a0a0c] dark:bg-[#3ecf8e]"
|
||||||
)}
|
)}
|
||||||
style={{ width: `${resolvedProgress}%` }}
|
style={progressStyle}
|
||||||
/>
|
/>
|
||||||
</div> : null}
|
</div>
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<style jsx>{`
|
||||||
|
@keyframes generationCursorBlink {
|
||||||
|
0%,
|
||||||
|
48% {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
49%,
|
||||||
|
100% {
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`}</style>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -918,9 +918,9 @@ export function ImageAnnotator({
|
|||||||
|
|
||||||
setIsRemovingBg(true);
|
setIsRemovingBg(true);
|
||||||
try {
|
try {
|
||||||
const input = /^https?:\/\//i.test(originalImageUrl)
|
// 使用原图URL获取原图数据,而不是canvas的预览图
|
||||||
? { imageUrl: originalImageUrl }
|
const base64Data = await imageUrlToBase64(originalImageUrl);
|
||||||
: { image_base64: await imageUrlToBase64(originalImageUrl) };
|
logger.debug("rmbg_source_prepared", { base64Length: base64Data.length });
|
||||||
|
|
||||||
// 调用抠图API
|
// 调用抠图API
|
||||||
const response = await fetch("/api/rmbg", {
|
const response = await fetch("/api/rmbg", {
|
||||||
@ -929,7 +929,7 @@ export function ImageAnnotator({
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
...input,
|
image_base64: base64Data,
|
||||||
prompt: "画图标注-抠图处理", // 可选:添加提示词
|
prompt: "画图标注-抠图处理", // 可选:添加提示词
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
@ -41,11 +41,45 @@ const PHASE_HINT: Record<NonNullable<ImageGenerationLoadingProps["phase"]>, stri
|
|||||||
idle: "任务已创建,正在初始化生成参数。",
|
idle: "任务已创建,正在初始化生成参数。",
|
||||||
queued: "任务已进入队列,请稍候。",
|
queued: "任务已进入队列,请稍候。",
|
||||||
processing: "模型正在生成图片,请稍候。",
|
processing: "模型正在生成图片,请稍候。",
|
||||||
finalizing: "图片已返回,正在保存并同步到对话。",
|
finalizing: "图片已生成完成,正在同步到对话。",
|
||||||
failed: "本次处理已停止,请查看对话中的原因和建议。",
|
failed: "生成失败,请重试。",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const LONG_PROCESSING_HINTS = [
|
||||||
|
"正在读取参考图与文字需求。",
|
||||||
|
"正在分析主体、场景和构图关系。",
|
||||||
|
"正在提取画面中的关键视觉特征。",
|
||||||
|
"正在确认图片比例和输出尺寸。",
|
||||||
|
"正在匹配当前模型的最佳生成参数。",
|
||||||
|
"正在把参考图信息提交给模型。",
|
||||||
|
"正在等待模型完成第一阶段推理。",
|
||||||
|
"正在生成初始画面结构。",
|
||||||
|
"正在保持主体特征和参考一致。",
|
||||||
|
"正在补全画面中的空间层次。",
|
||||||
|
"正在调整光影方向和明暗关系。",
|
||||||
|
"正在优化主体边缘与背景融合。",
|
||||||
|
"正在细化材质、纹理和局部细节。",
|
||||||
|
"正在减少画面噪点和不自然区域。",
|
||||||
|
"正在提升清晰度与整体质感。",
|
||||||
|
"正在检查生成结果的完整性。",
|
||||||
|
"正在处理高分辨率输出。",
|
||||||
|
"正在等待服务商返回最终图片。",
|
||||||
|
"正在保存图片并同步到对话。",
|
||||||
|
"正在准备把结果显示到画布。",
|
||||||
|
];
|
||||||
const LONG_WAIT_SECONDS = 5 * 60;
|
const LONG_WAIT_SECONDS = 5 * 60;
|
||||||
|
const LONG_WAIT_HINT =
|
||||||
|
"服务繁忙,模型可能正在排队或自动尝试备用通道,请耐心等候,结果返回后会自动同步到画布。";
|
||||||
|
|
||||||
|
function getLongProcessingHint(phase: NonNullable<ImageGenerationLoadingProps["phase"]>, elapsedSeconds: number) {
|
||||||
|
if (phase !== "processing" && phase !== "finalizing") return PHASE_HINT[phase];
|
||||||
|
if (elapsedSeconds >= LONG_WAIT_SECONDS) return LONG_WAIT_HINT;
|
||||||
|
const index = Math.min(
|
||||||
|
LONG_PROCESSING_HINTS.length - 1,
|
||||||
|
Math.floor(Math.max(0, elapsedSeconds) / 15)
|
||||||
|
);
|
||||||
|
return LONG_PROCESSING_HINTS[index];
|
||||||
|
}
|
||||||
|
|
||||||
function formatImageQueueWaitLabel(estimatedWaitMs?: number) {
|
function formatImageQueueWaitLabel(estimatedWaitMs?: number) {
|
||||||
const numeric = Number(estimatedWaitMs || 0);
|
const numeric = Number(estimatedWaitMs || 0);
|
||||||
@ -104,8 +138,16 @@ export const ImageGenerationLoading = memo(
|
|||||||
const isLongWait =
|
const isLongWait =
|
||||||
resolvedElapsedSeconds >= LONG_WAIT_SECONDS &&
|
resolvedElapsedSeconds >= LONG_WAIT_SECONDS &&
|
||||||
(phase === "processing" || phase === "finalizing");
|
(phase === "processing" || phase === "finalizing");
|
||||||
const elapsedLabel = resolvedElapsedSeconds > 0 ? `已等待 ${resolvedElapsedSeconds}s` : "";
|
const estimateRangeLabel = isLongWait
|
||||||
const phaseHint = PHASE_HINT[phase];
|
? "单次最长约10分钟"
|
||||||
|
: providerType === "grsai-gpt-image"
|
||||||
|
? "2~5分钟"
|
||||||
|
: "60~120秒";
|
||||||
|
const elapsedLabel =
|
||||||
|
resolvedElapsedSeconds > 0
|
||||||
|
? `已等待 ${resolvedElapsedSeconds}s`
|
||||||
|
: `预计 ${estimateRangeLabel}`;
|
||||||
|
const phaseHint = getLongProcessingHint(phase, resolvedElapsedSeconds);
|
||||||
const queueLine =
|
const queueLine =
|
||||||
phase === "queued" && Number.isFinite(queuePosition)
|
phase === "queued" && Number.isFinite(queuePosition)
|
||||||
? Math.max(0, Number(queuePosition || 0)) > 0
|
? Math.max(0, Number(queuePosition || 0)) > 0
|
||||||
@ -147,12 +189,12 @@ export const ImageGenerationLoading = memo(
|
|||||||
}
|
}
|
||||||
compact={count > 1}
|
compact={count > 1}
|
||||||
phase={phase}
|
phase={phase}
|
||||||
title={isLongWait && phase === "processing" ? "模型尚未返回结果,请勿重复提交" : undefined}
|
title={isLongWait ? "服务繁忙,排队处理中" : undefined}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className="space-y-0.5 text-[#9aa3b2]">
|
<div className="space-y-0.5 text-[#9aa3b2]">
|
||||||
<div className="text-[13px] font-medium">
|
<div className="text-[13px] font-medium">
|
||||||
使用 {resolvedModelName} 生成图片
|
使用 {resolvedModelName} 生成图片,预计 {estimateRangeLabel}
|
||||||
</div>
|
</div>
|
||||||
{queueLine ? <div className="text-[13px]">{queueLine}</div> : null}
|
{queueLine ? <div className="text-[13px]">{queueLine}</div> : null}
|
||||||
{queueDetailLine ? <div className="text-[13px]">{queueDetailLine}</div> : null}
|
{queueDetailLine ? <div className="text-[13px]">{queueDetailLine}</div> : null}
|
||||||
|
|||||||
@ -1,12 +1,11 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
|
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { X, Download, Edit, Target, Paintbrush, Sparkles, Scissors, Expand, Maximize2, Move3D, Droplet } from "lucide-react";
|
import { X, Download, Edit, Target, Paintbrush, Sparkles, Scissors, Expand, Maximize2, Move3D, Droplet } from "lucide-react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
import { buildImageProxyUrl, getImageDownloadExtension } from "@/lib/image-delivery";
|
||||||
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
|
||||||
|
|
||||||
const ImageAnnotator = dynamic(
|
const ImageAnnotator = dynamic(
|
||||||
() => import("./image-annotator").then((module) => module.ImageAnnotator),
|
() => import("./image-annotator").then((module) => module.ImageAnnotator),
|
||||||
@ -24,6 +23,25 @@ const AngleEditDialog = dynamic(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function triggerBrowserDownload(
|
||||||
|
url: string,
|
||||||
|
filename?: string,
|
||||||
|
options?: { openInNewTab?: boolean }
|
||||||
|
) {
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = url;
|
||||||
|
if (filename) {
|
||||||
|
link.download = filename;
|
||||||
|
}
|
||||||
|
if (options?.openInNewTab) {
|
||||||
|
link.target = "_blank";
|
||||||
|
}
|
||||||
|
link.rel = "noopener noreferrer";
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
}
|
||||||
|
|
||||||
export function ImagePreview({
|
export function ImagePreview({
|
||||||
src,
|
src,
|
||||||
alt,
|
alt,
|
||||||
@ -220,12 +238,24 @@ export function ImagePreview({
|
|||||||
toast.error("下载失败,请重试");
|
toast.error("下载失败,请重试");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const filenameBase = `generated-image-${Date.now()}`;
|
||||||
try {
|
try {
|
||||||
await downloadOriginalImage({ url: safeOriginalUrl });
|
const response = await fetch(
|
||||||
toast.success("原图已开始下载");
|
buildImageProxyUrl(safeOriginalUrl, { delivery: "original" })
|
||||||
|
);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`下载失败(${response.status})`);
|
||||||
|
}
|
||||||
|
const blob = await response.blob();
|
||||||
|
const extension = getImageDownloadExtension(blob.type);
|
||||||
|
const filename = extension ? `${filenameBase}.${extension}` : filenameBase;
|
||||||
|
const url = window.URL.createObjectURL(blob);
|
||||||
|
triggerBrowserDownload(url, filename);
|
||||||
|
window.URL.revokeObjectURL(url);
|
||||||
|
toast.success("下载成功");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("下载失败:", error);
|
console.error("下载失败:", error);
|
||||||
toast.error(getCanvasImageDownloadErrorMessage(error));
|
toast.error(error instanceof Error && error.message.includes("无法确认原图") ? error.message : "原图下载失败,请重试");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -307,18 +337,9 @@ export function ImagePreview({
|
|||||||
}}
|
}}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
decoding="async"
|
decoding="async"
|
||||||
onLoad={(event) => {
|
|
||||||
event.currentTarget.style.aspectRatio = "";
|
|
||||||
event.currentTarget.parentElement?.style.removeProperty("width");
|
|
||||||
event.currentTarget.alt = "Generated Image";
|
|
||||||
}}
|
|
||||||
onError={(event) => {
|
onError={(event) => {
|
||||||
if (!normalizeLegacyAliyunStorageUrl(thumbnail || original) && event.currentTarget.src !== (thumbnail || original)) {
|
if (event.currentTarget.src !== (thumbnail || original)) {
|
||||||
event.currentTarget.src = thumbnail || original;
|
event.currentTarget.src = thumbnail || original;
|
||||||
} else if (!compactGrid) {
|
|
||||||
event.currentTarget.style.aspectRatio = "1";
|
|
||||||
event.currentTarget.parentElement?.style.setProperty("width", "100%");
|
|
||||||
event.currentTarget.alt = "预览暂不可用,可尝试下载原图";
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onClick={() => setIsOpen(true)}
|
onClick={() => setIsOpen(true)}
|
||||||
@ -610,7 +631,7 @@ export function ImagePreview({
|
|||||||
onClick={(e: React.MouseEvent) => e.stopPropagation()}
|
onClick={(e: React.MouseEvent) => e.stopPropagation()}
|
||||||
>
|
>
|
||||||
<img
|
<img
|
||||||
src={normalizeLegacyAliyunStorageUrl(original) ? buildImageProxyUrl(original) : original}
|
src={original}
|
||||||
alt="Generated Image - Original"
|
alt="Generated Image - Original"
|
||||||
className={`max-h-[90vh] max-w-full rounded-lg object-contain ${
|
className={`max-h-[90vh] max-w-full rounded-lg object-contain ${
|
||||||
isAnnotationMode ? "cursor-crosshair" : ""
|
isAnnotationMode ? "cursor-crosshair" : ""
|
||||||
|
|||||||
@ -9,7 +9,6 @@ import { Button } from "./ui/button";
|
|||||||
import { X, Download, Target, Paintbrush } from "lucide-react";
|
import { X, Download, Target, Paintbrush } from "lucide-react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { ImageAnnotator } from "./image-annotator";
|
import { ImageAnnotator } from "./image-annotator";
|
||||||
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
|
||||||
|
|
||||||
export const PreviewAttachment = ({
|
export const PreviewAttachment = ({
|
||||||
attachment,
|
attachment,
|
||||||
@ -74,11 +73,18 @@ export const PreviewAttachment = ({
|
|||||||
// 下载图片函数
|
// 下载图片函数
|
||||||
const handleDownload = async () => {
|
const handleDownload = async () => {
|
||||||
try {
|
try {
|
||||||
await downloadOriginalImage({ url, name: name ?? "image" });
|
const response = await fetch(url);
|
||||||
toast.success("原图已开始下载");
|
const blob = await response.blob();
|
||||||
|
const downloadUrl = window.URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = downloadUrl;
|
||||||
|
link.download = name ?? `image-${Date.now()}.jpg`;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
window.URL.revokeObjectURL(downloadUrl);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("下载失败:", error);
|
console.error("下载失败:", error);
|
||||||
toast.error(getCanvasImageDownloadErrorMessage(error));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,74 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { useEffect, useState, type CSSProperties, type MouseEventHandler } from "react";
|
|
||||||
import { buildImageProxyUrl, getFullscreenPreviewSize } from "@/lib/image-delivery";
|
|
||||||
|
|
||||||
export type LoadedImagePreview = { src: string; width: number; height: number };
|
|
||||||
|
|
||||||
/** Keep the already displayed image visible while its larger preview loads. */
|
|
||||||
export function ProgressiveImagePreview({
|
|
||||||
src,
|
|
||||||
preview,
|
|
||||||
alt,
|
|
||||||
className,
|
|
||||||
style,
|
|
||||||
onClick,
|
|
||||||
}: {
|
|
||||||
src: string;
|
|
||||||
preview: LoadedImagePreview;
|
|
||||||
alt: string;
|
|
||||||
className?: string;
|
|
||||||
style?: CSSProperties;
|
|
||||||
onClick?: MouseEventHandler<HTMLImageElement>;
|
|
||||||
}) {
|
|
||||||
const size = getFullscreenPreviewSize({
|
|
||||||
imageWidth: preview.width,
|
|
||||||
imageHeight: preview.height,
|
|
||||||
viewportWidth: typeof window === "undefined" ? 1280 : window.innerWidth,
|
|
||||||
viewportHeight: typeof window === "undefined" ? 900 : window.innerHeight,
|
|
||||||
pixelRatio: typeof window === "undefined" ? 1 : window.devicePixelRatio,
|
|
||||||
});
|
|
||||||
const fullSrc = buildImageProxyUrl(src, {
|
|
||||||
delivery: "proxy",
|
|
||||||
width: size.requestWidth,
|
|
||||||
quality: 86,
|
|
||||||
format: "webp",
|
|
||||||
});
|
|
||||||
const [loadedSrc, setLoadedSrc] = useState("");
|
|
||||||
const [failedSrc, setFailedSrc] = useState("");
|
|
||||||
const ready = fullSrc === preview.src || loadedSrc === fullSrc;
|
|
||||||
const failed = failedSrc === fullSrc;
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (fullSrc === preview.src) return;
|
|
||||||
const image = new Image();
|
|
||||||
image.decoding = "async";
|
|
||||||
image.onload = () => setLoadedSrc(fullSrc);
|
|
||||||
image.onerror = () => setFailedSrc(fullSrc);
|
|
||||||
image.src = fullSrc;
|
|
||||||
return () => {
|
|
||||||
image.onload = null;
|
|
||||||
image.onerror = null;
|
|
||||||
image.removeAttribute("src");
|
|
||||||
};
|
|
||||||
}, [fullSrc, preview.src]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="relative flex items-center justify-center" onClick={(event) => event.stopPropagation()}>
|
|
||||||
<img
|
|
||||||
alt={alt}
|
|
||||||
className={className}
|
|
||||||
decoding="async"
|
|
||||||
draggable={false}
|
|
||||||
onClick={onClick}
|
|
||||||
src={ready ? fullSrc : preview.src}
|
|
||||||
style={{ width: size.displayWidth, ...style }}
|
|
||||||
/>
|
|
||||||
{!ready && (
|
|
||||||
<div role="status" className="pointer-events-none absolute bottom-4 max-w-full rounded-lg bg-black/80 px-3 py-2 text-center text-sm text-white">
|
|
||||||
{failed ? "高清预览加载失败,已保留当前预览;可关闭后重试或下载原图" : "正在加载高清预览…"}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -1,5 +1,4 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { downloadOriginalImage, getCanvasImageDownloadErrorMessage } from "@/lib/client/canvas-image-download";
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ArrowLeft,
|
ArrowLeft,
|
||||||
@ -2074,9 +2073,18 @@ export function SceneFusionView({
|
|||||||
|
|
||||||
const downloadImage = async (url: string, filename: string) => {
|
const downloadImage = async (url: string, filename: string) => {
|
||||||
try {
|
try {
|
||||||
await downloadOriginalImage({ url, name: filename });
|
const response = await fetch(url);
|
||||||
} catch (error) {
|
const blob = await response.blob();
|
||||||
toast.error(getCanvasImageDownloadErrorMessage(error));
|
const blobUrl = URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = blobUrl;
|
||||||
|
link.download = filename;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
setTimeout(() => URL.revokeObjectURL(blobUrl), 100);
|
||||||
|
} catch {
|
||||||
|
window.open(url, "_blank");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -29,10 +29,10 @@ AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName
|
|||||||
|
|
||||||
const AlertDialogContent = React.forwardRef<
|
const AlertDialogContent = React.forwardRef<
|
||||||
React.ElementRef<typeof AlertDialogPrimitive.Content>,
|
React.ElementRef<typeof AlertDialogPrimitive.Content>,
|
||||||
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content> & { overlayClassName?: string }
|
React.ComponentPropsWithoutRef<typeof AlertDialogPrimitive.Content>
|
||||||
>(({ className, overlayClassName, ...props }, ref) => (
|
>(({ className, ...props }, ref) => (
|
||||||
<AlertDialogPortal>
|
<AlertDialogPortal>
|
||||||
<AlertDialogOverlay className={overlayClassName} />
|
<AlertDialogOverlay />
|
||||||
<AlertDialogPrimitive.Content
|
<AlertDialogPrimitive.Content
|
||||||
ref={ref}
|
ref={ref}
|
||||||
className={cn(
|
className={cn(
|
||||||
|
|||||||
@ -92,16 +92,8 @@ public 先从当前运行版本创建写入时复制副本,保留未纳入 Git
|
|||||||
|
|
||||||
原生产 checkout 的支线及未提交改动仍保留,不能据其 Git HEAD 判断已发布版本。
|
原生产 checkout 的支线及未提交改动仍保留,不能据其 Git HEAD 判断已发布版本。
|
||||||
实际发布证据是:运行 cwd、`.next/BUILD_ID`、该目录 `release.json` 和 `activation.json`。
|
实际发布证据是:运行 cwd、`.next/BUILD_ID`、该目录 `release.json` 和 `activation.json`。
|
||||||
`next.config.ts` 将本流程的发布目录编号写入前后端版本标识;`/api/app-version` 返回该编号且不缓存,发布烟测会核对它与运行目录一致。网页每分钟及切回页面时检查更新,发现版本变化后提示同步并刷新;画布同步失败或当前任务仍在处理时保留页面。首次接入前已经打开的旧网页需先刷新一次,后续发布即可自动收到提示。
|
|
||||||
维护电脑的 `.git/releases/<发布ID>/` 保留提交、构建日志、烟测、传输校验和及操作回执。
|
维护电脑的 `.git/releases/<发布ID>/` 保留提交、构建日志、烟测、传输校验和及操作回执。
|
||||||
服务器回滚配置可能含环境元数据,目录权限为 0700、文件为 0600,不放入公开目录或 Git。
|
服务器回滚配置可能含环境元数据,目录权限为 0700、文件为 0600,不放入公开目录或 Git。
|
||||||
|
|
||||||
仓库历史全量 lint 仍有已记录的问题,不能声称全部质量门禁通过。
|
仓库历史全量 lint 仍有已记录的问题,不能声称全部质量门禁通过。
|
||||||
每次 PR 须记录本次改动的验证、历史失败和未覆盖项目;当前流程不会自动触发生产发布。
|
每次 PR 须记录本次改动的验证、历史失败和未覆盖项目;当前流程不会自动触发生产发布。
|
||||||
|
|
||||||
## ZIP 下载交付
|
|
||||||
|
|
||||||
默认存储为七牛时,完整 ZIP 通过分片上传至七牛,确认下载地址可用后才提供下载。
|
|
||||||
大文件使用三个并发分片,上传进度显示在原打包任务中。传送或链接检查失败会返回具体阶段、
|
|
||||||
下载编号及重试建议,保留已生成的 ZIP;再次点击下载复用原文件,已上传的完整对象不重复上传。
|
|
||||||
后台 ZIP 任务参与发布期间的工作等待。完成或失败的任务及文件保留 24 小时,待处理任务保留 6 小时。
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCallback, useEffect, useRef, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
import { BEFORE_PAGE_REFRESH, type PageRefreshRequest } from "@/lib/client/page-refresh";
|
|
||||||
import { mergeCanvasSnapshots, type CanvasConflictChoice } from "@/lib/video-canvas/merge-snapshot";
|
import { mergeCanvasSnapshots, type CanvasConflictChoice } from "@/lib/video-canvas/merge-snapshot";
|
||||||
import {
|
import {
|
||||||
CanvasRevisionConflict,
|
CanvasRevisionConflict,
|
||||||
@ -150,16 +149,6 @@ export function useVideoCanvasPersistence(
|
|||||||
return !queue.dirty && !queue.failed;
|
return !queue.dirty && !queue.failed;
|
||||||
}, [ready]);
|
}, [ready]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const beforeRefresh = (event: Event) => {
|
|
||||||
(event as CustomEvent<PageRefreshRequest>).detail.save(async () => {
|
|
||||||
if (!(await saveAndConfirm())) throw new Error("视频画布尚未同步完成,本地草稿已保留,请重试刷新。");
|
|
||||||
});
|
|
||||||
};
|
|
||||||
window.addEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
|
||||||
return () => window.removeEventListener(BEFORE_PAGE_REFRESH, beforeRefresh);
|
|
||||||
}, [saveAndConfirm]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const onKeyDown = (event: KeyboardEvent) => {
|
const onKeyDown = (event: KeyboardEvent) => {
|
||||||
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "s") {
|
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "s") {
|
||||||
|
|||||||
@ -4,9 +4,6 @@ import {
|
|||||||
executeRmbgJob,
|
executeRmbgJob,
|
||||||
fetchImageBufferFromUrl,
|
fetchImageBufferFromUrl,
|
||||||
} from "@/lib/ai/rmbg/service";
|
} from "@/lib/ai/rmbg/service";
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
|
||||||
|
|
||||||
const logger = createLogger("box-cutout-service");
|
|
||||||
|
|
||||||
export type NormalizedCropRect = {
|
export type NormalizedCropRect = {
|
||||||
x: number;
|
x: number;
|
||||||
@ -16,7 +13,6 @@ export type NormalizedCropRect = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export interface ExecuteBoxCutoutJobParams {
|
export interface ExecuteBoxCutoutJobParams {
|
||||||
requestId?: string;
|
|
||||||
userId: string;
|
userId: string;
|
||||||
imageUrl: string;
|
imageUrl: string;
|
||||||
cropRect: Partial<NormalizedCropRect>;
|
cropRect: Partial<NormalizedCropRect>;
|
||||||
@ -147,7 +143,6 @@ function applyPadding(
|
|||||||
export async function executeBoxCutoutJob(
|
export async function executeBoxCutoutJob(
|
||||||
params: ExecuteBoxCutoutJobParams,
|
params: ExecuteBoxCutoutJobParams,
|
||||||
): Promise<ExecuteBoxCutoutJobResult> {
|
): Promise<ExecuteBoxCutoutJobResult> {
|
||||||
const requestId = params.requestId || crypto.randomUUID();
|
|
||||||
const imageUrl = String(params.imageUrl || "").trim();
|
const imageUrl = String(params.imageUrl || "").trim();
|
||||||
if (!imageUrl) {
|
if (!imageUrl) {
|
||||||
throw new BoxCutoutServiceError("缺少 imageUrl 参数", 400);
|
throw new BoxCutoutServiceError("缺少 imageUrl 参数", 400);
|
||||||
@ -155,7 +150,7 @@ export async function executeBoxCutoutJob(
|
|||||||
|
|
||||||
const cropRect = sanitizeNormalizedCropRect(params.cropRect);
|
const cropRect = sanitizeNormalizedCropRect(params.cropRect);
|
||||||
if (!cropRect) {
|
if (!cropRect) {
|
||||||
throw new BoxCutoutServiceError("框选范围无效,请重新框选需要抠出的区域后提交。", 400);
|
throw new BoxCutoutServiceError("缺少有效的 cropRect 参数", 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
const paddingRatio = normalizeBoxCutoutPaddingRatio(params.paddingRatio);
|
const paddingRatio = normalizeBoxCutoutPaddingRatio(params.paddingRatio);
|
||||||
@ -175,7 +170,7 @@ export async function executeBoxCutoutJob(
|
|||||||
|
|
||||||
const cropRectPixels = toPixelRect(cropRect, sourceWidth, sourceHeight);
|
const cropRectPixels = toPixelRect(cropRect, sourceWidth, sourceHeight);
|
||||||
if (cropRectPixels.width < MIN_CROP_EDGE_PX || cropRectPixels.height < MIN_CROP_EDGE_PX) {
|
if (cropRectPixels.width < MIN_CROP_EDGE_PX || cropRectPixels.height < MIN_CROP_EDGE_PX) {
|
||||||
throw new BoxCutoutServiceError("框选区域过小,宽、高均须至少 24 像素,请扩大框选范围后重新提交。", 400);
|
throw new BoxCutoutServiceError("框选区域过小,请重新框选", 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
const paddedCropRect = applyPadding(cropRect, paddingRatio);
|
const paddedCropRect = applyPadding(cropRect, paddingRatio);
|
||||||
@ -189,18 +184,7 @@ export async function executeBoxCutoutJob(
|
|||||||
})
|
})
|
||||||
.toBuffer();
|
.toBuffer();
|
||||||
|
|
||||||
logger.info("crop_prepared", {
|
|
||||||
requestId,
|
|
||||||
sourceNodeId: params.sourceNodeId,
|
|
||||||
sourceWidth,
|
|
||||||
sourceHeight,
|
|
||||||
cropRectPixels,
|
|
||||||
paddedCropRectPixels,
|
|
||||||
inputBytes: croppedBuffer.length,
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await executeRmbgJob({
|
const result = await executeRmbgJob({
|
||||||
requestId,
|
|
||||||
userId: params.userId,
|
userId: params.userId,
|
||||||
imageBuffer: croppedBuffer,
|
imageBuffer: croppedBuffer,
|
||||||
imageBase64: croppedBuffer.toString("base64"),
|
imageBase64: croppedBuffer.toString("base64"),
|
||||||
|
|||||||
@ -9,7 +9,6 @@ import {
|
|||||||
summarizeAgentCapabilities,
|
summarizeAgentCapabilities,
|
||||||
} from "./capability-access";
|
} from "./capability-access";
|
||||||
import type { CanvasAgentTool } from "./loop";
|
import type { CanvasAgentTool } from "./loop";
|
||||||
import { AgentToolRejectedError } from "./protocol";
|
|
||||||
import type { CanvasOperationPlan } from "@/types/canvas/agent-protocol";
|
import type { CanvasOperationPlan } from "@/types/canvas/agent-protocol";
|
||||||
|
|
||||||
export function registerImageCanvasAccessTools(params: {
|
export function registerImageCanvasAccessTools(params: {
|
||||||
@ -60,10 +59,10 @@ export function registerImageCanvasAccessTools(params: {
|
|||||||
(item) => item.workspace === "canvas" && item.id === input.featureId,
|
(item) => item.workspace === "canvas" && item.id === input.featureId,
|
||||||
);
|
);
|
||||||
if (!feature?.authorized || !feature.id.startsWith("canvas.ui."))
|
if (!feature?.authorized || !feature.id.startsWith("canvas.ui."))
|
||||||
throw new AgentToolRejectedError("请使用清单中的图片画布界面入口,媒体能力请使用对应专用工具;本次未打开面板或提交处理。");
|
throw new Error("请使用清单中的图片画布界面入口,媒体能力请使用对应专用工具");
|
||||||
const { nodes } = await readOwnedCanvas(params.userId, params.projectId);
|
const { nodes } = await readOwnedCanvas(params.userId, params.projectId);
|
||||||
if (input.nodeIds.some((nodeId) => !nodes.some((node) => node.id === nodeId)))
|
if (input.nodeIds.some((nodeId) => !nodes.some((node) => node.id === nodeId)))
|
||||||
throw new AgentToolRejectedError("目标节点不属于当前画布,本次未打开面板或提交处理。");
|
throw new Error("目标节点不属于当前画布");
|
||||||
params.emit("data-canvasAgentUiCommand", {
|
params.emit("data-canvasAgentUiCommand", {
|
||||||
projectId: params.projectId,
|
projectId: params.projectId,
|
||||||
requestId: `${params.runId}:${id}`,
|
requestId: `${params.runId}:${id}`,
|
||||||
|
|||||||
@ -72,7 +72,6 @@ export async function processVideoCanvasImage(
|
|||||||
const result = await executeRmbgJob({
|
const result = await executeRmbgJob({
|
||||||
userId: params.userId,
|
userId: params.userId,
|
||||||
imageBuffer: await fetchImageBufferFromUrl(imageUrl),
|
imageBuffer: await fetchImageBufferFromUrl(imageUrl),
|
||||||
inputImageUrl: imageUrl,
|
|
||||||
sourceImageUrl: imageUrl,
|
sourceImageUrl: imageUrl,
|
||||||
prompt: params.prompt,
|
prompt: params.prompt,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
* - qwen-image-edit-plus 系列:支持 1-6 张输出,自定义分辨率,提示词优化
|
* - qwen-image-edit-plus 系列:支持 1-6 张输出,自定义分辨率,提示词优化
|
||||||
* - qwen-image-edit:仅支持 1 张输出
|
* - qwen-image-edit:仅支持 1 张输出
|
||||||
*
|
*
|
||||||
* API 文档:https://help.aliyun.com/en/model-studio/qwen-image-generation-and-editing-api-reference
|
* API 文档:https://help.aliyun.com/zh/model-studio/developer-reference/qwen-vl-plus
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
@ -14,7 +14,6 @@ import { isAlibabaImageTextToImageModelId } from "@/lib/ai/model-classification"
|
|||||||
import { buildReferenceImageOrderPrompt } from "@/lib/ai/providers/reference-image-order";
|
import { buildReferenceImageOrderPrompt } from "@/lib/ai/providers/reference-image-order";
|
||||||
import { createDeferredUploadedImage } from "@/lib/storage/image-archive";
|
import { createDeferredUploadedImage } from "@/lib/storage/image-archive";
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
import { normalizeLegacyAliyunStorageUrl } from "@/lib/storage/retired-storage";
|
|
||||||
import {
|
import {
|
||||||
isAlibabaQwenImage3ModelId,
|
isAlibabaQwenImage3ModelId,
|
||||||
resolveAlibabaImageAsyncEndpoint,
|
resolveAlibabaImageAsyncEndpoint,
|
||||||
@ -47,8 +46,6 @@ interface AlibabaImageResponse {
|
|||||||
output?: {
|
output?: {
|
||||||
task_id?: string;
|
task_id?: string;
|
||||||
task_status?: string;
|
task_status?: string;
|
||||||
code?: string;
|
|
||||||
message?: string;
|
|
||||||
choices?: Array<{
|
choices?: Array<{
|
||||||
finish_reason: string;
|
finish_reason: string;
|
||||||
message: {
|
message: {
|
||||||
@ -125,21 +122,11 @@ async function fetchImageBufferWithRetry(
|
|||||||
timeoutMs = ALIBABA_IMAGE_FETCH_TIMEOUT_MS,
|
timeoutMs = ALIBABA_IMAGE_FETCH_TIMEOUT_MS,
|
||||||
attempts = 2
|
attempts = 2
|
||||||
): Promise<{ buffer: Buffer; contentType: string | null }> {
|
): Promise<{ buffer: Buffer; contentType: string | null }> {
|
||||||
if (imageUrl.startsWith("data:image/")) {
|
|
||||||
const match = imageUrl.match(/^data:(image\/[a-z0-9.+-]+);base64,(.+)$/i);
|
|
||||||
if (!match) throw new Error("输入图片数据格式无效");
|
|
||||||
return { buffer: Buffer.from(match[2], "base64"), contentType: match[1] };
|
|
||||||
}
|
|
||||||
let sourceUrl = imageUrl;
|
|
||||||
if (normalizeLegacyAliyunStorageUrl(sourceUrl)) {
|
|
||||||
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
|
||||||
sourceUrl = await resolveLegacyAliyunStorageUrl(sourceUrl);
|
|
||||||
}
|
|
||||||
let lastError: unknown = null;
|
let lastError: unknown = null;
|
||||||
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
||||||
const ctx = createAbortSignal(timeoutMs);
|
const ctx = createAbortSignal(timeoutMs);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(sourceUrl, {
|
const response = await fetch(imageUrl, {
|
||||||
cache: "no-store",
|
cache: "no-store",
|
||||||
redirect: "follow",
|
redirect: "follow",
|
||||||
signal: ctx.signal,
|
signal: ctx.signal,
|
||||||
@ -181,7 +168,7 @@ function calculateAlibabaSizeByAspect(aspectValue: number, baseSize: number): st
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function inferAspectRatioFromImageUrl(imageUrl: string): Promise<number | null> {
|
async function inferAspectRatioFromImageUrl(imageUrl: string): Promise<number | null> {
|
||||||
if (!/^(?:https?:\/\/|data:image\/)/i.test(imageUrl)) return null;
|
if (!/^https?:\/\//i.test(imageUrl)) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { buffer } = await fetchImageBufferWithRetry(imageUrl);
|
const { buffer } = await fetchImageBufferWithRetry(imageUrl);
|
||||||
@ -199,7 +186,7 @@ async function inferAspectRatioFromImageUrl(imageUrl: string): Promise<number |
|
|||||||
async function inferImageDimensionsFromImageUrl(
|
async function inferImageDimensionsFromImageUrl(
|
||||||
imageUrl: string,
|
imageUrl: string,
|
||||||
): Promise<{ width: number; height: number; aspectRatio: number } | null> {
|
): Promise<{ width: number; height: number; aspectRatio: number } | null> {
|
||||||
if (!/^(?:https?:\/\/|data:image\/)/i.test(imageUrl)) return null;
|
if (!/^https?:\/\//i.test(imageUrl)) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { buffer } = await fetchImageBufferWithRetry(imageUrl);
|
const { buffer } = await fetchImageBufferWithRetry(imageUrl);
|
||||||
@ -315,14 +302,6 @@ async function prepareAlibabaInputImageUrl(
|
|||||||
maxSide: ALIBABA_MAX_INPUT_SIDE,
|
maxSide: ALIBABA_MAX_INPUT_SIDE,
|
||||||
modelId,
|
modelId,
|
||||||
});
|
});
|
||||||
if (isAlibabaQwenImage3ModelId(modelId)) {
|
|
||||||
if (prepared.buffer.length > maxInputBytes) {
|
|
||||||
throw new Error("输入图片超过模型大小限制,请先压缩图片后重试");
|
|
||||||
}
|
|
||||||
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")}`;
|
|
||||||
}
|
|
||||||
if (!prepared.transformed) {
|
if (!prepared.transformed) {
|
||||||
return normalizedUrl;
|
return normalizedUrl;
|
||||||
}
|
}
|
||||||
@ -586,10 +565,8 @@ async function sendAlibabaImageAsyncRequest(params: {
|
|||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
if (["FAILED", "CANCELED", "UNKNOWN"].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(
|
throw new AlibabaImageTerminalTaskError(
|
||||||
`${reason}${code ? `(${code})` : ""}(task_id: ${taskId})`,
|
task.message || `阿里图片任务${taskStatus || "失败"}(task_id: ${taskId})`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
lastPollError = null;
|
lastPollError = null;
|
||||||
|
|||||||
@ -8,7 +8,6 @@ import { executeConfiguredVip98kTask } from "@/lib/ai/vip98k-runtime";
|
|||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { generatedImage } from "@/lib/db/schema";
|
import { generatedImage } from "@/lib/db/schema";
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
import { createLogger } from "@/lib/observability/logger";
|
||||||
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
|
|
||||||
const logger = createLogger("rmbg-service");
|
const logger = createLogger("rmbg-service");
|
||||||
@ -28,7 +27,6 @@ type RmbgConfig = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type ExecuteRmbgJobParams = {
|
export type ExecuteRmbgJobParams = {
|
||||||
requestId?: string;
|
|
||||||
userId: string;
|
userId: string;
|
||||||
imageBuffer: Buffer;
|
imageBuffer: Buffer;
|
||||||
imageBase64?: string;
|
imageBase64?: string;
|
||||||
@ -37,8 +35,6 @@ export type ExecuteRmbgJobParams = {
|
|||||||
chatId?: string | null;
|
chatId?: string | null;
|
||||||
messageId?: string | null;
|
messageId?: string | null;
|
||||||
skipGallerySave?: boolean;
|
skipGallerySave?: boolean;
|
||||||
// 仅当 imageBuffer 与 URL 对应的输入文件一致时提供。
|
|
||||||
inputImageUrl?: string | null;
|
|
||||||
sourceImageUrl?: string | null;
|
sourceImageUrl?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -57,7 +53,7 @@ export class RmbgServiceError extends Error {
|
|||||||
detail?: string;
|
detail?: string;
|
||||||
|
|
||||||
constructor(message: string, status = 500, detail?: string) {
|
constructor(message: string, status = 500, detail?: string) {
|
||||||
super(detail && detail !== message ? `${message}:${detail}` : message);
|
super(message);
|
||||||
this.name = "RmbgServiceError";
|
this.name = "RmbgServiceError";
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.detail = detail;
|
this.detail = detail;
|
||||||
@ -65,11 +61,7 @@ export class RmbgServiceError extends Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
// https://picwish.cn/background-removal-api-doc
|
|
||||||
const AOS_MAX_RESOLUTION = 4096;
|
const AOS_MAX_RESOLUTION = 4096;
|
||||||
const AOS_MAX_INPUT_BYTES = 50 * 1024 * 1024;
|
|
||||||
// The shared AOS state -5 documents a separate limit for image_url downloads.
|
|
||||||
const AOS_MAX_URL_INPUT_BYTES = 30 * 1024 * 1024;
|
|
||||||
const AOS_CREATE_TIMEOUT_MS = 120_000;
|
const AOS_CREATE_TIMEOUT_MS = 120_000;
|
||||||
const AOS_POLL_TIMEOUT_MS = 60_000;
|
const AOS_POLL_TIMEOUT_MS = 60_000;
|
||||||
const AOS_DOWNLOAD_TIMEOUT_MS = 60_000;
|
const AOS_DOWNLOAD_TIMEOUT_MS = 60_000;
|
||||||
@ -155,18 +147,6 @@ const withTimeout = async <T,>(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async function readAosResult<T>(read: (signal: AbortSignal) => Promise<T>, timeoutMs: number): Promise<T> {
|
|
||||||
for (let attempt = 0; ; attempt++) {
|
|
||||||
try {
|
|
||||||
return await withTimeout(read, timeoutMs);
|
|
||||||
} catch (error) {
|
|
||||||
const name = error instanceof Error ? error.name : "";
|
|
||||||
if (attempt >= 2 || (name !== "TypeError" && name !== "AbortError")) throw error;
|
|
||||||
await sleep(1000 * (attempt + 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAosFriendlyErrorMessage(detail: string): string | null {
|
function getAosFriendlyErrorMessage(detail: string): string | null {
|
||||||
const resolutionMatch = detail.match(/resolution excess,\s*width=(\d+),\s*height=(\d+)/i);
|
const resolutionMatch = detail.match(/resolution excess,\s*width=(\d+),\s*height=(\d+)/i);
|
||||||
if (resolutionMatch) {
|
if (resolutionMatch) {
|
||||||
@ -184,42 +164,50 @@ function getAosFriendlyErrorMessage(detail: string): string | null {
|
|||||||
async function prepareImageForAos(
|
async function prepareImageForAos(
|
||||||
imageBuffer: Buffer
|
imageBuffer: Buffer
|
||||||
): Promise<{ buffer: Buffer; mimeType: string; filename: string }> {
|
): Promise<{ buffer: Buffer; mimeType: string; filename: string }> {
|
||||||
if (imageBuffer.length > AOS_MAX_INPUT_BYTES) {
|
|
||||||
throw new RmbgServiceError(
|
|
||||||
`抠图参考图文件大小超限(${(imageBuffer.length / 1024 / 1024).toFixed(2)} MB),当前 AOS 上游最大支持 50 MB。`,
|
|
||||||
400
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let metadata: import("sharp").Metadata;
|
|
||||||
try {
|
try {
|
||||||
const sharp = await import("sharp");
|
const sharp = await import("sharp");
|
||||||
metadata = await sharp.default(imageBuffer, { limitInputPixels: false }).metadata();
|
const image = sharp.default(imageBuffer);
|
||||||
|
const metadata = await image.metadata();
|
||||||
|
const width = metadata.width;
|
||||||
|
const height = metadata.height;
|
||||||
|
const format = (metadata.format || "png").toLowerCase();
|
||||||
|
|
||||||
|
const ext = format === "jpeg" ? "jpg" : format;
|
||||||
|
const filenameExt =
|
||||||
|
ext === "jpg" || ext === "png" || ext === "webp" || ext === "tiff" ? ext : "png";
|
||||||
|
const mimeType =
|
||||||
|
ext === "jpg"
|
||||||
|
? "image/jpeg"
|
||||||
|
: ext === "webp"
|
||||||
|
? "image/webp"
|
||||||
|
: ext === "tiff"
|
||||||
|
? "image/tiff"
|
||||||
|
: "image/png";
|
||||||
|
|
||||||
|
if (!width || !height || (width <= AOS_MAX_RESOLUTION && height <= AOS_MAX_RESOLUTION)) {
|
||||||
|
return {
|
||||||
|
buffer: imageBuffer,
|
||||||
|
mimeType,
|
||||||
|
filename: `image.${filenameExt}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const resizedBuffer = await image
|
||||||
|
.resize(AOS_MAX_RESOLUTION, AOS_MAX_RESOLUTION, {
|
||||||
|
fit: "inside",
|
||||||
|
withoutEnlargement: true,
|
||||||
|
})
|
||||||
|
.toBuffer();
|
||||||
|
|
||||||
|
return {
|
||||||
|
buffer: resizedBuffer,
|
||||||
|
mimeType,
|
||||||
|
filename: `image.${filenameExt}`,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// AOS also accepts BMP/RAW/RGB formats that Sharp cannot identify.
|
logger.warn("aos_image_preprocess_failed", error);
|
||||||
logger.warn("aos_image_metadata_unavailable", error);
|
return { buffer: imageBuffer, mimeType: "image/png", filename: "image.png" };
|
||||||
return { buffer: imageBuffer, mimeType: "application/octet-stream", filename: "image" };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)) {
|
|
||||||
const name = format === "heif" ? "HEIF/AVIF" : format.toUpperCase();
|
|
||||||
throw new RmbgServiceError(`抠图参考图格式不支持(${name}),当前 AOS 上游不支持此格式。`, 400);
|
|
||||||
}
|
|
||||||
const ext = format === "jpeg" ? "jpg" : format;
|
|
||||||
return {
|
|
||||||
buffer: imageBuffer,
|
|
||||||
mimeType: format ? `image/${format}` : "application/octet-stream",
|
|
||||||
filename: ext ? `image.${ext}` : "image",
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processWithRMBG(
|
async function processWithRMBG(
|
||||||
@ -381,38 +369,20 @@ async function processWithAosSegmentation(
|
|||||||
foregroundType?: "" | "person" | "object" | "stamp";
|
foregroundType?: "" | "person" | "object" | "stamp";
|
||||||
crop?: 0 | 1;
|
crop?: 0 | 1;
|
||||||
format?: "png" | "jpg";
|
format?: "png" | "jpg";
|
||||||
inputImageUrl?: string | null;
|
|
||||||
requestId?: string;
|
|
||||||
}
|
}
|
||||||
): Promise<{ success: boolean; imageBuffer?: Buffer; processing_time?: number; error?: string }> {
|
): Promise<{ success: boolean; imageBuffer?: Buffer; processing_time?: number; error?: string }> {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
const baseUrl = (config.apiBaseUrl || "https://techsz.aoscdn.com").replace(/\/+$/, "");
|
const baseUrl = (config.apiBaseUrl || "https://techsz.aoscdn.com").replace(/\/+$/, "");
|
||||||
const createUrl = `${baseUrl}/api/tasks/visual/segmentation`;
|
const createUrl = `${baseUrl}/api/tasks/visual/segmentation`;
|
||||||
const headers = { "X-API-KEY": config.apiKey };
|
const headers = { "X-API-KEY": config.apiKey };
|
||||||
let stage = "create";
|
|
||||||
let providerTaskId: string | undefined;
|
|
||||||
const sourceImage = await prepareImageForAos(imageBuffer);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const normalizedImage = await prepareImageForAos(imageBuffer);
|
||||||
const createForm = new FormData();
|
const createForm = new FormData();
|
||||||
let publicSourceUrl: string | null = null;
|
const imageBlob = new Blob([new Uint8Array(normalizedImage.buffer)], {
|
||||||
if (config.inputImageUrl && imageBuffer.length <= AOS_MAX_URL_INPUT_BYTES) {
|
type: normalizedImage.mimeType,
|
||||||
try {
|
});
|
||||||
const source = new URL(config.inputImageUrl);
|
createForm.append("image_file", imageBlob, normalizedImage.filename);
|
||||||
const cdn = new URL(process.env.NEXT_PUBLIC_CANVAS_CDN_ORIGIN || "");
|
|
||||||
if (["http:", "https:"].includes(source.protocol) && source.hostname === cdn.hostname) {
|
|
||||||
publicSourceUrl = source.toString();
|
|
||||||
}
|
|
||||||
} catch { /* Uploaded files and other sources use the file API. */ }
|
|
||||||
}
|
|
||||||
if (publicSourceUrl) {
|
|
||||||
createForm.append("image_url", publicSourceUrl);
|
|
||||||
} else {
|
|
||||||
const imageBlob = new Blob([new Uint8Array(sourceImage.buffer)], {
|
|
||||||
type: sourceImage.mimeType,
|
|
||||||
});
|
|
||||||
createForm.append("image_file", imageBlob, sourceImage.filename);
|
|
||||||
}
|
|
||||||
createForm.append("sync", "0");
|
createForm.append("sync", "0");
|
||||||
createForm.append("return_type", "1");
|
createForm.append("return_type", "1");
|
||||||
createForm.append("output_type", "2");
|
createForm.append("output_type", "2");
|
||||||
@ -422,9 +392,6 @@ async function processWithAosSegmentation(
|
|||||||
createForm.append("type", config.foregroundType);
|
createForm.append("type", config.foregroundType);
|
||||||
}
|
}
|
||||||
|
|
||||||
const createStartedAt = Date.now();
|
|
||||||
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(
|
const createResponse = await withTimeout(
|
||||||
(signal) =>
|
(signal) =>
|
||||||
fetch(createUrl, {
|
fetch(createUrl, {
|
||||||
@ -444,37 +411,28 @@ async function processWithAosSegmentation(
|
|||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: `AOS任务创建失败(HTTP ${createResponse.status}): ${createPayload?.message || createResponse.statusText}`,
|
error: `AOS任务创建失败: ${createPayload?.message || createResponse.statusText}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const taskId = createPayload.data.task_id as string;
|
const taskId = createPayload.data.task_id as string;
|
||||||
providerTaskId = taskId;
|
|
||||||
logger.info("aos_task_created", { requestId: config.requestId, taskId, createMs: Date.now() - createStartedAt });
|
|
||||||
const taskUrl = `${createUrl}/${taskId}`;
|
const taskUrl = `${createUrl}/${taskId}`;
|
||||||
const maxPollCount = 60;
|
const maxPollCount = 60;
|
||||||
|
|
||||||
for (let pollCount = 0; pollCount < maxPollCount; pollCount += 1) {
|
for (let pollCount = 0; pollCount < maxPollCount; pollCount += 1) {
|
||||||
await sleep(1000);
|
await sleep(1000);
|
||||||
stage = "poll";
|
|
||||||
|
|
||||||
const pollResponse = await readAosResult(
|
const pollResponse = await withTimeout(
|
||||||
async (signal) => {
|
(signal) =>
|
||||||
const response = await fetch(taskUrl, {
|
fetch(taskUrl, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers,
|
headers,
|
||||||
signal,
|
signal,
|
||||||
});
|
}),
|
||||||
const payload = await response.json().catch((error) => {
|
|
||||||
if (error instanceof Error && error.name !== "SyntaxError") throw error;
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
return { ok: response.ok, status: response.status, statusText: response.statusText, payload };
|
|
||||||
},
|
|
||||||
AOS_POLL_TIMEOUT_MS
|
AOS_POLL_TIMEOUT_MS
|
||||||
);
|
);
|
||||||
|
|
||||||
const pollPayload = pollResponse.payload;
|
const pollPayload = await pollResponse.json().catch(() => null);
|
||||||
if (!pollResponse.ok || !pollPayload?.data) {
|
if (!pollResponse.ok || !pollPayload?.data) {
|
||||||
logger.warn("aos_task_poll_failed", {
|
logger.warn("aos_task_poll_failed", {
|
||||||
hasPayload: pollPayload != null,
|
hasPayload: pollPayload != null,
|
||||||
@ -482,7 +440,7 @@ async function processWithAosSegmentation(
|
|||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: `AOS任务查询失败(HTTP ${pollResponse.status},任务 ${taskId}): ${pollPayload?.message || pollResponse.statusText}`,
|
error: `AOS任务查询失败: ${pollPayload?.message || pollResponse.statusText}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,15 +454,12 @@ async function processWithAosSegmentation(
|
|||||||
pollCount,
|
pollCount,
|
||||||
taskId,
|
taskId,
|
||||||
});
|
});
|
||||||
stage = "download";
|
const imageResponse = await withTimeout(
|
||||||
const imageResponse = await readAosResult(
|
(signal) =>
|
||||||
async (signal) => {
|
fetch(imageUrl, {
|
||||||
const response = await fetch(imageUrl, {
|
|
||||||
method: "GET",
|
method: "GET",
|
||||||
signal,
|
signal,
|
||||||
});
|
}),
|
||||||
return { ok: response.ok, status: response.status, statusText: response.statusText, buffer: response.ok ? Buffer.from(await response.arrayBuffer()) : null };
|
|
||||||
},
|
|
||||||
AOS_DOWNLOAD_TIMEOUT_MS
|
AOS_DOWNLOAD_TIMEOUT_MS
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -515,11 +470,11 @@ async function processWithAosSegmentation(
|
|||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: `AOS结果下载失败(HTTP ${imageResponse.status},任务 ${taskId}): ${imageResponse.statusText}`,
|
error: `AOS结果下载失败: ${imageResponse.status} ${imageResponse.statusText}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const resultBuffer = imageResponse.buffer!;
|
const resultBuffer = Buffer.from(await imageResponse.arrayBuffer());
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
imageBuffer: resultBuffer,
|
imageBuffer: resultBuffer,
|
||||||
@ -535,30 +490,19 @@ async function processWithAosSegmentation(
|
|||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: `AOS任务失败(状态 ${state},任务 ${taskId}): ${errMessage || stateDetail || pollPayload?.message || "未提供详情"}`,
|
error: `AOS任务失败: ${errMessage || stateDetail || pollPayload?.message || "状态异常"}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.warn("aos_task_timeout", { requestId: config.requestId, taskId, maxPollCount });
|
logger.warn("aos_task_timeout", { maxPollCount });
|
||||||
return { success: false, error: `AOS查询任务超时,结果尚未确认(任务 ${taskId})` };
|
return { success: false, error: "AOS任务超时(最多60秒)" };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error && error.name === "AbortError") {
|
if (error instanceof Error && error.name === "AbortError") {
|
||||||
logger.warn("aos_request_timeout", { requestId: config.requestId, stage, taskId: providerTaskId, inputBytes: imageBuffer.length });
|
return { success: false, error: "AOS请求超时,请稍后重试" };
|
||||||
const message = stage === "create"
|
|
||||||
? "AOS创建任务超时(120秒),结果尚未确认"
|
|
||||||
: stage === "poll"
|
|
||||||
? `AOS查询任务超时,结果尚未确认(任务 ${providerTaskId})`
|
|
||||||
: `AOS结果下载超时,任务已完成(任务 ${providerTaskId})`;
|
|
||||||
return { success: false, error: message };
|
|
||||||
}
|
}
|
||||||
logger.warn("aos_provider_request_error", error, { requestId: config.requestId, stage, taskId: providerTaskId });
|
logger.warn("aos_provider_request_error", error);
|
||||||
const detail = error instanceof Error ? error.message : "未知错误";
|
return { success: false, error: error instanceof Error ? error.message : "未知错误" };
|
||||||
return { success: false, error: stage === "create"
|
|
||||||
? `AOS创建任务连接失败,结果尚未确认:${detail}`
|
|
||||||
: stage === "poll"
|
|
||||||
? `AOS查询任务连接失败,结果尚未确认(任务 ${providerTaskId}):${detail}`
|
|
||||||
: `AOS结果下载连接失败,任务已完成(任务 ${providerTaskId}):${detail}` };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -655,15 +599,8 @@ export async function fetchImageBufferFromUrl(imageUrl: string): Promise<Buffer>
|
|||||||
|
|
||||||
export async function executeRmbgJob(
|
export async function executeRmbgJob(
|
||||||
params: ExecuteRmbgJobParams
|
params: ExecuteRmbgJobParams
|
||||||
): Promise<ExecuteRmbgJobResult> {
|
|
||||||
return trackRuntimeWork(() => runRmbgJob(params));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function runRmbgJob(
|
|
||||||
params: ExecuteRmbgJobParams
|
|
||||||
): Promise<ExecuteRmbgJobResult> {
|
): Promise<ExecuteRmbgJobResult> {
|
||||||
const {
|
const {
|
||||||
requestId = crypto.randomUUID(),
|
|
||||||
userId,
|
userId,
|
||||||
imageBuffer,
|
imageBuffer,
|
||||||
imageBase64 = imageBuffer.toString("base64"),
|
imageBase64 = imageBuffer.toString("base64"),
|
||||||
@ -672,13 +609,11 @@ async function runRmbgJob(
|
|||||||
chatId = null,
|
chatId = null,
|
||||||
messageId = null,
|
messageId = null,
|
||||||
skipGallerySave = false,
|
skipGallerySave = false,
|
||||||
inputImageUrl = null,
|
|
||||||
sourceImageUrl = null,
|
sourceImageUrl = null,
|
||||||
} = params;
|
} = params;
|
||||||
|
|
||||||
const { provider, config } = await getValidatedRmbgConfig();
|
const { provider, config } = await getValidatedRmbgConfig();
|
||||||
logger.info("job_started", {
|
logger.info("job_started", {
|
||||||
requestId,
|
|
||||||
hasChatId: Boolean(chatId),
|
hasChatId: Boolean(chatId),
|
||||||
hasMessageId: Boolean(messageId),
|
hasMessageId: Boolean(messageId),
|
||||||
hasSourceImageUrl: Boolean(sourceImageUrl),
|
hasSourceImageUrl: Boolean(sourceImageUrl),
|
||||||
@ -722,8 +657,6 @@ async function runRmbgJob(
|
|||||||
foregroundType: foregroundType ?? config.aosForegroundType ?? "",
|
foregroundType: foregroundType ?? config.aosForegroundType ?? "",
|
||||||
crop: config.aosCrop === 1 ? 1 : 0,
|
crop: config.aosCrop === 1 ? 1 : 0,
|
||||||
format: config.aosFormat === "jpg" ? "jpg" : "png",
|
format: config.aosFormat === "jpg" ? "jpg" : "png",
|
||||||
inputImageUrl,
|
|
||||||
requestId,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.success || !result.imageBuffer) {
|
if (!result.success || !result.imageBuffer) {
|
||||||
@ -860,7 +793,6 @@ async function runRmbgJob(
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.info("job_completed", {
|
logger.info("job_completed", {
|
||||||
requestId,
|
|
||||||
generatedImageId: savedImage.id,
|
generatedImageId: savedImage.id,
|
||||||
mediaAssetId,
|
mediaAssetId,
|
||||||
processingSeconds: Number(processingTime.toFixed(2)),
|
processingSeconds: Number(processingTime.toFixed(2)),
|
||||||
@ -883,7 +815,6 @@ async function runRmbgJob(
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.info("job_completed", {
|
logger.info("job_completed", {
|
||||||
requestId,
|
|
||||||
processingSeconds: Number(processingTime.toFixed(2)),
|
processingSeconds: Number(processingTime.toFixed(2)),
|
||||||
provider,
|
provider,
|
||||||
savedToGallery: false,
|
savedToGallery: false,
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { agentModelFault, type AgentModelFault } from "./agent-model-failover";
|
|||||||
export function agentImageFailurePolicy(error: unknown): { retry: boolean; pause: AgentModelFault | null } {
|
export function agentImageFailurePolicy(error: unknown): { retry: boolean; pause: AgentModelFault | null } {
|
||||||
const value = error as { message?: string; providerOutcomeUnknown?: boolean; status?: number; statusCode?: number } | null;
|
const value = error as { message?: string; providerOutcomeUnknown?: boolean; status?: number; statusCode?: number } | null;
|
||||||
const message = String(value?.message || "").toLowerCase();
|
const message = String(value?.message || "").toLowerCase();
|
||||||
if (value?.providerOutcomeUnknown || ["AbortError", "TimeoutError"].includes(String((error as { name?: string })?.name || ""))) return { retry: false, pause: null };
|
|
||||||
// A submitted image job may still succeed. Retain its task and reconcile it;
|
// A submitted image job may still succeed. Retain its task and reconcile it;
|
||||||
// never submit another paid job just because a poll/connection timed out.
|
// never submit another paid job just because a poll/connection timed out.
|
||||||
const unknown = value?.providerOutcomeUnknown || /request_outcome_unknown|may still be running|outcome.unknown|timed out|timeout|超时|fetch failed|econnreset|socket hang up/.test(message);
|
const unknown = value?.providerOutcomeUnknown || /request_outcome_unknown|may still be running|outcome.unknown|timed out|timeout|超时|fetch failed|econnreset|socket hang up/.test(message);
|
||||||
|
|||||||
@ -516,7 +516,6 @@ export async function executeDirectAgentImageEditTool(
|
|||||||
const result = await executeRmbgJob({
|
const result = await executeRmbgJob({
|
||||||
userId: params.userId,
|
userId: params.userId,
|
||||||
imageBuffer: sourceBuffer,
|
imageBuffer: sourceBuffer,
|
||||||
inputImageUrl: params.sourceImageUrl,
|
|
||||||
prompt: params.requestTextContent || "抠图处理",
|
prompt: params.requestTextContent || "抠图处理",
|
||||||
chatId: params.chatId,
|
chatId: params.chatId,
|
||||||
messageId: params.messageId,
|
messageId: params.messageId,
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import {
|
|||||||
isMediaInputFailure,
|
isMediaInputFailure,
|
||||||
isNonProviderRequestStatus,
|
isNonProviderRequestStatus,
|
||||||
} from "@/lib/ai/runtime/request-failure-classification";
|
} from "@/lib/ai/runtime/request-failure-classification";
|
||||||
import { getRequestFailureDetails, isGatewayResponseTimeout, isProviderBalanceError, isRateLimitError } from "@/lib/request-error";
|
import { isGatewayResponseTimeout, isProviderBalanceError, isRateLimitError } from "@/lib/request-error";
|
||||||
|
|
||||||
export type AiModelHealthEventType =
|
export type AiModelHealthEventType =
|
||||||
| "success"
|
| "success"
|
||||||
@ -344,12 +344,7 @@ export function classifyAiModelHealthFailure(error: unknown): {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function shouldFailoverAiModelError(error: unknown) {
|
export function shouldFailoverAiModelError(error: unknown) {
|
||||||
if (
|
if (error instanceof ProviderTaskUncertainError || isGatewayResponseTimeout(error)) return false;
|
||||||
error instanceof ProviderTaskUncertainError ||
|
|
||||||
isGatewayResponseTimeout(error) ||
|
|
||||||
getRequestFailureDetails(error).outcome === "unknown" ||
|
|
||||||
["AbortError", "TimeoutError"].includes(String((error as { name?: string })?.name || ""))
|
|
||||||
) return false;
|
|
||||||
const classification = classifyAiModelHealthFailure(error);
|
const classification = classifyAiModelHealthFailure(error);
|
||||||
const normalized = getErrorMessage(error).toLowerCase();
|
const normalized = getErrorMessage(error).toLowerCase();
|
||||||
const imageProviderTaskFailed =
|
const imageProviderTaskFailed =
|
||||||
|
|||||||
@ -4,8 +4,7 @@ import { join } from "node:path";
|
|||||||
import type { NativeEvent, NativeResult } from "@/services/zcode-agent/runner.mjs";
|
import type { NativeEvent, NativeResult } from "@/services/zcode-agent/runner.mjs";
|
||||||
import { runNativeZcode } from "./native-runtime";
|
import { runNativeZcode } from "./native-runtime";
|
||||||
import type { CanvasAgentTool } from "@/lib/ai/canvas-agent-runtime/loop";
|
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 { AgentToolRejectedError, publicAgentError, 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 { isCompletedSimpleCutout } from "@/lib/ai/canvas-agent-runtime/cutout-completion";
|
||||||
import { requiresCanvasInteraction } from "@/lib/ai/canvas-agent-runtime/results";
|
import { requiresCanvasInteraction } from "@/lib/ai/canvas-agent-runtime/results";
|
||||||
import { AgentImageGenerationError } from "@/lib/ai/runtime/image-generation-error";
|
import { AgentImageGenerationError } from "@/lib/ai/runtime/image-generation-error";
|
||||||
@ -38,17 +37,6 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
|
|||||||
snapshot.message = message;
|
snapshot.message = message;
|
||||||
await ports.checkpoint(structuredClone(snapshot), status); return status;
|
await ports.checkpoint(structuredClone(snapshot), status); return status;
|
||||||
}
|
}
|
||||||
function failureMessage(call: AgentCallRecord) {
|
|
||||||
if (call.confirmedNoResult) return `${call.error}\n\n请求编号:${ports.runId}`;
|
|
||||||
const details = getRequestFailureDetails(call.error || "任务结果尚未确认");
|
|
||||||
if (call.uncertain || call.status === "started") {
|
|
||||||
if (details.outcome !== "unknown") details.message += "结果尚未确认。";
|
|
||||||
details.outcome = "unknown";
|
|
||||||
details.recommendation = "请联系管理员按请求编号核对原任务结果;不要换模型、新建对话或重新提交,以免重复生成。";
|
|
||||||
}
|
|
||||||
if (!details.recommendation) details.recommendation = "请联系管理员并提供请求编号,核对该步骤的错误。";
|
|
||||||
return `${call.title || "创作工具"}:${formatRequestFailure(details, ports.runId)}`;
|
|
||||||
}
|
|
||||||
let modelError = "";
|
let modelError = "";
|
||||||
let toolFailure = "";
|
let toolFailure = "";
|
||||||
let nativeFailed = false;
|
let nativeFailed = false;
|
||||||
@ -56,11 +44,11 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
|
|||||||
const seenTextEvents = new Set<string>();
|
const seenTextEvents = new Set<string>();
|
||||||
let lastTextMessage = "";
|
let lastTextMessage = "";
|
||||||
async function appendText(delta: string) {
|
async function appendText(delta: string) {
|
||||||
const text = publicAssistantText(delta).slice(0, Math.max(0, 100_000 - (snapshot.assistantText?.length || 0)));
|
delta = publicAssistantText(delta).slice(0, Math.max(0, 100_000 - (snapshot.assistantText?.length || 0)));
|
||||||
if (!text) return;
|
if (!delta) return;
|
||||||
snapshot.assistantText = (snapshot.assistantText || "") + text;
|
snapshot.assistantText = (snapshot.assistantText || "") + delta;
|
||||||
await ports.checkpoint(structuredClone(snapshot), "running");
|
await ports.checkpoint(structuredClone(snapshot), "running");
|
||||||
ports.onAssistantText?.(text);
|
ports.onAssistantText?.(delta);
|
||||||
}
|
}
|
||||||
async function finishVerifiedCutout() {
|
async function finishVerifiedCutout() {
|
||||||
snapshot.nativeRuntime!.state = "stopped";
|
snapshot.nativeRuntime!.state = "stopped";
|
||||||
@ -68,8 +56,8 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
|
|||||||
return save(verifiedCompletion, "completed");
|
return save(verifiedCompletion, "completed");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const unconfirmed = snapshot.calls.find(call => call.status === "started" || call.uncertain);
|
if (snapshot.calls.some(call => call.status === "started" || call.uncertain))
|
||||||
if (unconfirmed) return await save(failureMessage(unconfirmed), "needs_review");
|
return await save("存在结果未确认的工具调用,请先核对画布,避免重复生成。", "needs_review");
|
||||||
snapshot.nativeRuntime = { ...snapshot.nativeRuntime, engine: "zcode", state: "starting" };
|
snapshot.nativeRuntime = { ...snapshot.nativeRuntime, engine: "zcode", state: "starting" };
|
||||||
const schemas = new Map(Object.entries(ports.definitions).map(([name, definition]) => [name, asSchema(definition.inputSchema!)]));
|
const schemas = new Map(Object.entries(ports.definitions).map(([name, definition]) => [name, asSchema(definition.inputSchema!)]));
|
||||||
const toolDescriptors = Object.entries(ports.tools).map(([name, entry]) => ({ name,
|
const toolDescriptors = Object.entries(ports.tools).map(([name, entry]) => ({ name,
|
||||||
@ -123,7 +111,6 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
|
|||||||
await save("正在准备处理任务");
|
await save("正在准备处理任务");
|
||||||
}),
|
}),
|
||||||
onEvent: async (event: NativeEvent | NativeResult) => {
|
onEvent: async (event: NativeEvent | NativeResult) => {
|
||||||
if (attemptController.signal.aborted) return;
|
|
||||||
if (event.type === "model.streaming" && event.payload?.kind === "text_delta" && typeof event.payload.delta === "string") {
|
if (event.type === "model.streaming" && event.payload?.kind === "text_delta" && typeof event.payload.delta === "string") {
|
||||||
// Child sessions and reasoning/tool-argument deltas are private.
|
// Child sessions and reasoning/tool-argument deltas are private.
|
||||||
if (snapshot.nativeRuntime?.sessionId && event.sessionId !== snapshot.nativeRuntime.sessionId) return;
|
if (snapshot.nativeRuntime?.sessionId && event.sessionId !== snapshot.nativeRuntime.sessionId) return;
|
||||||
@ -168,11 +155,9 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
|
|||||||
if (!entry.readOnly && !snapshot.calls.some(call => call.name === "planCanvasTask" && call.status === "succeeded"))
|
if (!entry.readOnly && !snapshot.calls.some(call => call.name === "planCanvasTask" && call.status === "succeeded"))
|
||||||
throw new Error("请先制定任务计划再执行画布修改");
|
throw new Error("请先制定任务计划再执行画布修改");
|
||||||
if (!entry.readOnly && snapshot.verification?.retryable && snapshot.verification.failedNodeIds?.length) {
|
if (!entry.readOnly && snapshot.verification?.retryable && snapshot.verification.failedNodeIds?.length) {
|
||||||
const targets = [input.nodeId, ...(Array.isArray(input.nodeIds) ? input.nodeIds : [])]
|
const targets = [input.nodeId, ...(Array.isArray(input.nodeIds) ? input.nodeIds : []),
|
||||||
.filter((id): id is string => typeof id === "string");
|
...(Array.isArray(input.referenceNodeIds) ? input.referenceNodeIds : [])].filter((id): id is string => typeof id === "string");
|
||||||
const references = Array.isArray(input.referenceNodeIds) ? input.referenceNodeIds : [];
|
if (!targets.length || targets.some(id => !snapshot.verification!.failedNodeIds!.includes(id)))
|
||||||
const failedIds = snapshot.verification.failedNodeIds;
|
|
||||||
if (targets.length ? targets.some(id => !failedIds.includes(id)) : !references.some(id => failedIds.includes(String(id))))
|
|
||||||
throw new Error("修复只能处理未通过验收的节点,禁止重复生成已通过的结果");
|
throw new Error("修复只能处理未通过验收的节点,禁止重复生成已通过的结果");
|
||||||
}
|
}
|
||||||
snapshot.calls.push({ id: callId, name, title: entry.title, input, status: "started" });
|
snapshot.calls.push({ id: callId, name, title: entry.title, input, status: "started" });
|
||||||
@ -220,9 +205,8 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
|
|||||||
call.status = "failed"; call.error = publicAgentError(error); call.rejected = error instanceof AgentToolRejectedError;
|
call.status = "failed"; call.error = publicAgentError(error); call.rejected = error instanceof AgentToolRejectedError;
|
||||||
call.confirmedNoResult = error instanceof AgentImageGenerationError && error.confirmedNoResult;
|
call.confirmedNoResult = error instanceof AgentImageGenerationError && error.confirmedNoResult;
|
||||||
call.uncertain = !entry.readOnly && !call.rejected && !call.confirmedNoResult;
|
call.uncertain = !entry.readOnly && !call.rejected && !call.confirmedNoResult;
|
||||||
if (call.uncertain || call.confirmedNoResult) toolFailure = failureMessage(call);
|
if (call.confirmedNoResult) toolFailure = call.error;
|
||||||
await save(toolFailure || call.error);
|
await save(call.error);
|
||||||
if (call.uncertain || call.confirmedNoResult) attemptController.abort();
|
|
||||||
});
|
});
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@ -244,15 +228,15 @@ export async function runZcodeCanvasTask(ports: Ports): Promise<CanvasAgentRunSt
|
|||||||
if (toolFailure) return await save(toolFailure, "needs_review");
|
if (toolFailure) return await save(toolFailure, "needs_review");
|
||||||
// A failed read is not an uncertain paid operation. Independently verify
|
// A failed read is not an uncertain paid operation. Independently verify
|
||||||
// actual outputs below instead of failing a task because an earlier read timed out.
|
// actual outputs below instead of failing a task because an earlier read timed out.
|
||||||
const failedWrite = snapshot.calls.find(call => (call.status === "failed" && !call.rejected && !call.confirmedNoResult && !ports.tools[call.name]?.readOnly) || call.uncertain);
|
if (snapshot.calls.some(call => (call.status === "failed" && !call.rejected && !call.confirmedNoResult && !ports.tools[call.name]?.readOnly) || call.uncertain))
|
||||||
if (failedWrite) return await save(failureMessage(failedWrite), "needs_review");
|
return await save("部分工具结果待确认,已保留执行记录,请核对后继续。", "needs_review");
|
||||||
const unresolvedRead = snapshot.calls.find((call, index) => call.status === "failed" && !call.rejected && ports.tools[call.name]?.readOnly &&
|
const unresolvedRead = snapshot.calls.some((call, index) => call.status === "failed" && !call.rejected && ports.tools[call.name]?.readOnly &&
|
||||||
!snapshot.calls.slice(index + 1).some(later => later.name === call.name && later.status === "succeeded" &&
|
!snapshot.calls.slice(index + 1).some(later => later.name === call.name && later.status === "succeeded" &&
|
||||||
JSON.stringify(later.input) === JSON.stringify(call.input)));
|
JSON.stringify(later.input) === JSON.stringify(call.input)));
|
||||||
// Without a real result to independently inspect, a viewport/UI action
|
// Without a real result to independently inspect, a viewport/UI action
|
||||||
// cannot turn a failed analysis into a completed task.
|
// cannot turn a failed analysis into a completed task.
|
||||||
if (unresolvedRead && !snapshot.resultNodeIds.length)
|
if (unresolvedRead && !snapshot.resultNodeIds.length)
|
||||||
return await save(failureMessage(unresolvedRead), "needs_review");
|
return await save("图片或资料暂未读取成功,已保留记录,请稍后继续核对。", "needs_review");
|
||||||
if (question || snapshot.calls.some(call => requiresCanvasInteraction(snapshot, call)))
|
if (question || snapshot.calls.some(call => requiresCanvasInteraction(snapshot, call)))
|
||||||
return await save(question || "请在已打开的工具面板中完成操作,然后继续任务。", "needs_review");
|
return await save(question || "请在已打开的工具面板中完成操作,然后继续任务。", "needs_review");
|
||||||
const supplements = snapshot.inputs.filter(input => !startedInputs.has(input.id));
|
const supplements = snapshot.inputs.filter(input => !startedInputs.has(input.id));
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
import { executeBoxCutoutJob } from "@/lib/ai/box-cutout/service";
|
import {
|
||||||
|
executeBoxCutoutJob,
|
||||||
|
} from "@/lib/ai/box-cutout/service";
|
||||||
import { executeExtendJob } from "@/lib/ai/extend/service";
|
import { executeExtendJob } from "@/lib/ai/extend/service";
|
||||||
import { executeLayerSplitJob } from "@/lib/ai/layer-split/service";
|
import { executeLayerSplitJob } from "@/lib/ai/layer-split/service";
|
||||||
import { executeResizeJob } from "@/lib/ai/resize/service";
|
import { executeResizeJob } from "@/lib/ai/resize/service";
|
||||||
import {
|
import {
|
||||||
executeRmbgJob,
|
executeRmbgJob,
|
||||||
fetchImageBufferFromUrl,
|
fetchImageBufferFromUrl,
|
||||||
RmbgServiceError,
|
|
||||||
} from "@/lib/ai/rmbg/service";
|
} from "@/lib/ai/rmbg/service";
|
||||||
import type { CanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
import type { CanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
||||||
import type { CanvasCapabilityTarget } from "@/lib/canvas/capabilities/context";
|
import type { CanvasCapabilityTarget } from "@/lib/canvas/capabilities/context";
|
||||||
import { resolveCanvasRuntimeEndpoint } from "@/lib/canvas/capabilities/internal-endpoint";
|
import { resolveCanvasRuntimeEndpoint } from "@/lib/canvas/capabilities/internal-endpoint";
|
||||||
import { executeBoxCutoutPOST } from "@/lib/canvas/operations/box-cutout";
|
|
||||||
import { generateCanvasImage } from "@/lib/canvas/generate-image";
|
import { generateCanvasImage } from "@/lib/canvas/generate-image";
|
||||||
import { requestLongTaskResult } from "@/lib/client/long-task";
|
import { requestLongTaskResult } from "@/lib/client/long-task";
|
||||||
import {
|
import {
|
||||||
@ -128,10 +128,8 @@ async function executeForTarget(
|
|||||||
if (command.capabilityId === "image.remove_background") {
|
if (command.capabilityId === "image.remove_background") {
|
||||||
const imageBuffer = await fetchImageBufferFromUrl(target.sourceImageUrl);
|
const imageBuffer = await fetchImageBufferFromUrl(target.sourceImageUrl);
|
||||||
const result = await executeRmbgJob({
|
const result = await executeRmbgJob({
|
||||||
requestId: command.requestId,
|
|
||||||
userId: params.userId,
|
userId: params.userId,
|
||||||
imageBuffer,
|
imageBuffer,
|
||||||
inputImageUrl: target.sourceImageUrl,
|
|
||||||
sourceImageUrl: target.sourceImageUrl,
|
sourceImageUrl: target.sourceImageUrl,
|
||||||
prompt: prompt || "抠图处理",
|
prompt: prompt || "抠图处理",
|
||||||
skipGallerySave: false,
|
skipGallerySave: false,
|
||||||
@ -187,7 +185,8 @@ async function executeForTarget(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (command.capabilityId === "image.box_cutout") {
|
if (command.capabilityId === "image.box_cutout") {
|
||||||
const job = {
|
const result = await executeBoxCutoutJob({
|
||||||
|
userId: params.userId,
|
||||||
imageUrl: target.sourceImageUrl,
|
imageUrl: target.sourceImageUrl,
|
||||||
sourceNodeId: target.nodeId,
|
sourceNodeId: target.nodeId,
|
||||||
cropRect: {
|
cropRect: {
|
||||||
@ -198,30 +197,13 @@ async function executeForTarget(
|
|||||||
},
|
},
|
||||||
paddingRatio: asFiniteNumber(command.args.paddingRatio),
|
paddingRatio: asFiniteNumber(command.args.paddingRatio),
|
||||||
prompt: prompt || "框选抠图",
|
prompt: prompt || "框选抠图",
|
||||||
};
|
});
|
||||||
let result;
|
|
||||||
if (command.source === "toolbar") {
|
|
||||||
const response = await executeBoxCutoutPOST(new Request(params.requestUrl, {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json", "X-Request-ID": command.requestId },
|
|
||||||
body: JSON.stringify(job),
|
|
||||||
}), params.userId);
|
|
||||||
result = await response.json();
|
|
||||||
if (!response.ok || !result.success) {
|
|
||||||
throw new RmbgServiceError(result.detail || result.message || result.error || "框选抠图未能完成", response.status);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result = await executeBoxCutoutJob({ ...job, requestId: command.requestId, userId: params.userId });
|
|
||||||
}
|
|
||||||
return [{
|
return [{
|
||||||
...common,
|
...common,
|
||||||
assetId: result.mediaAssetId || undefined,
|
assetId: result.mediaAssetId || undefined,
|
||||||
metadata: {
|
metadata: {
|
||||||
editMode: "box-cutout",
|
|
||||||
cropRectApplied: result.cropRectApplied,
|
cropRectApplied: result.cropRectApplied,
|
||||||
paddedCropRectApplied: result.paddedCropRectApplied,
|
paddedCropRectApplied: result.paddedCropRectApplied,
|
||||||
cropRectPixels: result.cropRectPixels,
|
|
||||||
paddedCropRectPixels: result.paddedCropRectPixels,
|
|
||||||
},
|
},
|
||||||
title: `${target.title}-框选抠图`,
|
title: `${target.title}-框选抠图`,
|
||||||
url: result.original_url,
|
url: result.original_url,
|
||||||
|
|||||||
@ -1,31 +0,0 @@
|
|||||||
export type DownloadFailure = {
|
|
||||||
code: string;
|
|
||||||
message: string;
|
|
||||||
recommendation: string;
|
|
||||||
status: number;
|
|
||||||
retryable: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function getDownloadFailure(error: unknown): DownloadFailure {
|
|
||||||
const text = error && typeof error === "object" && "message" in error
|
|
||||||
? `${"name" in error ? error.name : "Error"}: ${error.message}` : String(error || "");
|
|
||||||
const failure = (code: string, message: string, recommendation: string, status: number, retryable = false) =>
|
|
||||||
({ code, message, recommendation, status, retryable });
|
|
||||||
if (/旧存储已停用|storage_retired/.test(text)) return failure("STORAGE_RETIRED", "原图所在的旧存储已停用。", "请重新上传原图;重试下载无法恢复该文件。", 410);
|
|
||||||
if (/HTTP (?:404|410)|SOURCE_NOT_FOUND/i.test(text)) return failure("SOURCE_NOT_FOUND", "原图链接已失效或源文件已被删除。", "请重新上传原图,或联系管理员核对归档副本;无需重新生成图片。", 404);
|
|
||||||
if (/HTTP (?:401|403)|SOURCE_FORBIDDEN/i.test(text)) return failure("SOURCE_FORBIDDEN", "图片源站拒绝访问原图。", "请联系管理员检查原图访问权限或链接有效期,并提供下载编号。", 403);
|
|
||||||
if (/queue.*busy|正在准备的原图较多|DOWNLOAD_BUSY/i.test(text)) return failure("DOWNLOAD_BUSY", "正在准备的下载较多。", "请等待当前下载完成后再试,避免同时重复点击。", 429, true);
|
|
||||||
if (/TimeoutError|AbortError|timeout|timed out|deadline|超时|HTTP (?:408|504|524)/i.test(text)) return failure("SOURCE_TIMEOUT", "读取原图或准备下载文件超时。", "请稍后再次下载;持续失败时联系管理员并提供下载编号,已归档的原图无需重新生成。", 504, true);
|
|
||||||
if (/fetch failed|failed to fetch|ECONN|ENOTFOUND|EAI_AGAIN|network|HTTP (?:429|5\d\d)/i.test(text)) return failure("SOURCE_UNAVAILABLE", "暂时无法连接图片源站。", "请稍后重试下载;持续失败时联系管理员检查取图线路,并提供下载编号。", 503, true);
|
|
||||||
if (/not.*image|error document|不是图片|无法识别图片|empty response|image is empty|Invalid prepared original/i.test(text)) return failure("INVALID_IMAGE", "源地址没有返回可用的图片文件。", "请重新上传可打开的原图;若原图正常,请联系管理员并提供下载编号。", 422);
|
|
||||||
if (/sharp|pngload|jpegload|unsupported image|无法解析图片|convert|转换/i.test(text)) return failure("IMAGE_CONVERSION_FAILED", "原图无法转换为下载所需的 PNG 文件。", "请保留原图并联系管理员提供下载编号,或重新上传 PNG/JPEG 图片。", 422);
|
|
||||||
if (/upload|存储桶|保存下载|STORAGE_WRITE_FAILED/i.test(text)) return failure("DOWNLOAD_STORAGE_FAILED", "下载文件保存失败,已归档的原图仍然保留。", "请稍后重试;持续失败时联系管理员检查存储,并提供下载编号。", 503, true);
|
|
||||||
if (/未登录/.test(text)) return failure("LOGIN_REQUIRED", "登录状态已失效。", "请重新登录后下载,当前图片不会因此删除。", 401);
|
|
||||||
if (/无法确认原图/.test(text)) return failure("ORIGINAL_IMAGE_UNCONFIRMED", "图片链接包含处理参数,无法确认原图。", "请选择未裁剪、未缩放的原图链接,或重新上传原图。", 400);
|
|
||||||
if (/图片地址无效/.test(text)) return failure("INVALID_IMAGE_URL", "图片地址无效。", "请刷新画布后重新选择图片;仍然失败时请重新上传原图。", 400);
|
|
||||||
return failure("DOWNLOAD_FAILED", "下载准备未能完成。", "请联系管理员并提供下载编号,以便核对原图、转换和存储的具体错误;无需重新生成。", 500, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function formatDownloadFailure(failure: DownloadFailure, requestId?: string) {
|
|
||||||
return `${failure.message}${failure.recommendation}${requestId ? `\n下载编号:${requestId}` : ""}`;
|
|
||||||
}
|
|
||||||
@ -1,10 +1,6 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { createHash, randomUUID } from "node:crypto";
|
import { createHash } from "node:crypto";
|
||||||
import { cacheImageBytes, readImageBytes, type ImageBytes } from "@/lib/storage/image-byte-cache";
|
import { cacheImageBytes, readImageBytes, type ImageBytes } from "@/lib/storage/image-byte-cache";
|
||||||
import { formatDownloadFailure, getDownloadFailure, type DownloadFailure } from "./download-error";
|
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
|
||||||
|
|
||||||
const logger = createLogger("canvas.download");
|
|
||||||
|
|
||||||
export type DownloadPreparationInput = {
|
export type DownloadPreparationInput = {
|
||||||
userId: string;
|
userId: string;
|
||||||
@ -13,18 +9,17 @@ export type DownloadPreparationInput = {
|
|||||||
cookie: string;
|
cookie: string;
|
||||||
};
|
};
|
||||||
type Result =
|
type Result =
|
||||||
| { status: "preparing"; requestId: string }
|
| { status: "preparing" }
|
||||||
| { status: "ready"; asset: ImageBytes }
|
| { status: "ready"; asset: ImageBytes }
|
||||||
| { status: "failed"; error: string; failure: DownloadFailure; requestId: string };
|
| { status: "failed"; error: string };
|
||||||
type Job = {
|
type Job = {
|
||||||
requestId: string;
|
|
||||||
userId: string;
|
userId: string;
|
||||||
input: DownloadPreparationInput;
|
input: DownloadPreparationInput;
|
||||||
status: "queued" | "running" | "ready" | "failed";
|
status: "queued" | "running" | "ready" | "failed";
|
||||||
updatedAt: number;
|
updatedAt: number;
|
||||||
asset?: ImageBytes;
|
asset?: ImageBytes;
|
||||||
failure?: DownloadFailure;
|
|
||||||
};
|
};
|
||||||
|
const FAILURE_MESSAGE = "原图暂时无法读取,图片仍保留在画布中,请稍后重试";
|
||||||
const MAX_MEMORY_BYTES = 128 * 1024 * 1024;
|
const MAX_MEMORY_BYTES = 128 * 1024 * 1024;
|
||||||
|
|
||||||
// A download is independent of node autosave and of the archive worker's batch.
|
// A download is independent of node autosave and of the archive worker's batch.
|
||||||
@ -74,7 +69,7 @@ export function createDownloadPreparationQueue(deps: {
|
|||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
reject(new Error("Download preparation deadline"));
|
reject(new Error("Download preparation deadline"));
|
||||||
}, deps.timeoutMs || 150_000);
|
}, deps.timeoutMs || 90_000);
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
if (asset.contentType !== "image/png" || !asset.buffer.length || asset.buffer.length > MAX_MEMORY_BYTES) {
|
if (asset.contentType !== "image/png" || !asset.buffer.length || asset.buffer.length > MAX_MEMORY_BYTES) {
|
||||||
@ -86,10 +81,8 @@ export function createDownloadPreparationQueue(deps: {
|
|||||||
const stored = await deps.read(key).catch(() => null);
|
const stored = await deps.read(key).catch(() => null);
|
||||||
if (stored?.contentType === "image/png" && stored.buffer.length) job.asset = undefined;
|
if (stored?.contentType === "image/png" && stored.buffer.length) job.asset = undefined;
|
||||||
job.status = "ready";
|
job.status = "ready";
|
||||||
} catch (error) {
|
} catch {
|
||||||
job.status = "failed";
|
job.status = "failed";
|
||||||
job.failure = getDownloadFailure(error);
|
|
||||||
logger.warn("preparation_failed", error, { requestId: job.requestId, code: job.failure.code });
|
|
||||||
} finally {
|
} finally {
|
||||||
if (timer) clearTimeout(timer);
|
if (timer) clearTimeout(timer);
|
||||||
// Do not retain authentication data after a preparation finishes.
|
// Do not retain authentication data after a preparation finishes.
|
||||||
@ -112,11 +105,8 @@ export function createDownloadPreparationQueue(deps: {
|
|||||||
prune();
|
prune();
|
||||||
const current = jobs.get(key);
|
const current = jobs.get(key);
|
||||||
if (current?.status === "ready" && current.asset) return { status: "ready", asset: current.asset };
|
if (current?.status === "ready" && current.asset) return { status: "ready", asset: current.asset };
|
||||||
if (current?.status === "failed") {
|
if (current?.status === "failed") return { status: "failed", error: FAILURE_MESSAGE };
|
||||||
const failure = current.failure || getDownloadFailure(null);
|
if (current && current.status !== "ready") return { status: "preparing" };
|
||||||
return { status: "failed", error: formatDownloadFailure(failure, current.requestId), failure, requestId: current.requestId };
|
|
||||||
}
|
|
||||||
if (current && current.status !== "ready") return { status: "preparing", requestId: current.requestId };
|
|
||||||
if (jobs.size >= 64) {
|
if (jobs.size >= 64) {
|
||||||
for (const [oldKey, job] of jobs) {
|
for (const [oldKey, job] of jobs) {
|
||||||
if (job.status === "ready" || job.status === "failed") jobs.delete(oldKey);
|
if (job.status === "ready" || job.status === "failed") jobs.delete(oldKey);
|
||||||
@ -125,14 +115,11 @@ export function createDownloadPreparationQueue(deps: {
|
|||||||
}
|
}
|
||||||
if (jobs.size >= 64 || [...jobs.values()].filter(job => job.userId === input.userId &&
|
if (jobs.size >= 64 || [...jobs.values()].filter(job => job.userId === input.userId &&
|
||||||
(job.status === "queued" || job.status === "running")).length >= 4) {
|
(job.status === "queued" || job.status === "running")).length >= 4) {
|
||||||
const requestId = randomUUID();
|
return { status: "failed", error: "正在准备的原图较多,请等待当前下载完成后再试" };
|
||||||
const failure = getDownloadFailure("DOWNLOAD_BUSY");
|
|
||||||
return { status: "failed", error: formatDownloadFailure(failure, requestId), failure, requestId };
|
|
||||||
}
|
}
|
||||||
const requestId = randomUUID();
|
jobs.set(key, { userId: input.userId, input, status: "queued", updatedAt: now() });
|
||||||
jobs.set(key, { requestId, userId: input.userId, input, status: "queued", updatedAt: now() });
|
|
||||||
deps.schedule(drain);
|
deps.schedule(drain);
|
||||||
return { status: "preparing", requestId };
|
return { status: "preparing" };
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,8 +44,7 @@ import {
|
|||||||
} from "@/lib/ai/runtime/model-health";
|
} from "@/lib/ai/runtime/model-health";
|
||||||
import { uploadFile } from "@/lib/storage/service";
|
import { uploadFile } from "@/lib/storage/service";
|
||||||
import { isImageModelBalanceBlocked, resolveConfiguredImageBalanceFallback } from "@/lib/ai/runtime/image-balance";
|
import { isImageModelBalanceBlocked, resolveConfiguredImageBalanceFallback } from "@/lib/ai/runtime/image-balance";
|
||||||
import { getRequestFailureDetails, isProviderBalanceError } from "@/lib/request-error";
|
import { isProviderBalanceError } from "@/lib/request-error";
|
||||||
import { normalizeLegacyAliyunStorageUrl } from "@/lib/storage/retired-storage";
|
|
||||||
|
|
||||||
export interface CanvasLocalEditCompositeRegion {
|
export interface CanvasLocalEditCompositeRegion {
|
||||||
x: number;
|
x: number;
|
||||||
@ -4633,10 +4632,10 @@ function findCanvasFailoverModel(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function generateCanvasImage(
|
export async function generateCanvasImage(
|
||||||
input: CanvasGenerateImageRequest
|
request: CanvasGenerateImageRequest
|
||||||
): Promise<CanvasGenerateImageResult> {
|
): Promise<CanvasGenerateImageResult> {
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
const request = input.agentImageMode === "pinned" ? { ...input, allowFailover: false } : input;
|
if (request.agentImageMode === "pinned") request = { ...request, allowFailover: false };
|
||||||
const prompt = String(request.prompt || "").trim();
|
const prompt = String(request.prompt || "").trim();
|
||||||
if (!prompt) {
|
if (!prompt) {
|
||||||
throw new Error("请输入提示词");
|
throw new Error("请输入提示词");
|
||||||
@ -4874,16 +4873,11 @@ export async function generateCanvasImage(
|
|||||||
typeof request.ownerKey === "string" && request.ownerKey.trim()
|
typeof request.ownerKey === "string" && request.ownerKey.trim()
|
||||||
? request.ownerKey.trim()
|
? request.ownerKey.trim()
|
||||||
: undefined;
|
: undefined;
|
||||||
const resolveReference = async (url: string) => {
|
const referenceImage =
|
||||||
if (!normalizeLegacyAliyunStorageUrl(url)) return url;
|
typeof request.referenceImage === "string" ? request.referenceImage : undefined;
|
||||||
const { resolveLegacyAliyunStorageUrl } = await import("@/lib/storage/image-archive-db");
|
const referenceImages = Array.isArray(request.referenceImages)
|
||||||
return resolveLegacyAliyunStorageUrl(url);
|
|
||||||
};
|
|
||||||
const referenceImage = typeof request.referenceImage === "string"
|
|
||||||
? await resolveReference(request.referenceImage) : undefined;
|
|
||||||
const referenceImages = await Promise.all((Array.isArray(request.referenceImages)
|
|
||||||
? request.referenceImages
|
? request.referenceImages
|
||||||
: []).map(resolveReference));
|
: [];
|
||||||
const debugLabel = String(request.debugLabel || "").trim() || "canvas-image";
|
const debugLabel = String(request.debugLabel || "").trim() || "canvas-image";
|
||||||
const debugMeta =
|
const debugMeta =
|
||||||
request.debugMeta && typeof request.debugMeta === "object"
|
request.debugMeta && typeof request.debugMeta === "object"
|
||||||
@ -5663,11 +5657,7 @@ export async function generateCanvasImage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// An accepted or uncertain request must not start another paid model/variant.
|
// An accepted or uncertain request must not start another paid model/variant.
|
||||||
if (caughtError && (
|
if (caughtError instanceof ProviderTaskUncertainError) throw caughtError;
|
||||||
caughtError instanceof ProviderTaskUncertainError ||
|
|
||||||
getRequestFailureDetails(caughtError).outcome === "unknown" ||
|
|
||||||
["AbortError", "TimeoutError"].includes(String((caughtError as { name?: string })?.name || ""))
|
|
||||||
)) throw caughtError;
|
|
||||||
|
|
||||||
const retryVariants = Array.isArray(request.retryVariants)
|
const retryVariants = Array.isArray(request.retryVariants)
|
||||||
? request.retryVariants
|
? request.retryVariants
|
||||||
|
|||||||
@ -1,34 +0,0 @@
|
|||||||
import type { RequestFailureDetails } from "@/lib/request-error";
|
|
||||||
|
|
||||||
export type ImageGenerationStatusStreamPayload = {
|
|
||||||
phase?: "queued" | "processing" | "finalizing" | "failed" | "completed";
|
|
||||||
userMessageId?: string;
|
|
||||||
requestId?: string;
|
|
||||||
failure?: RequestFailureDetails;
|
|
||||||
providerType?: string;
|
|
||||||
modelName?: string;
|
|
||||||
imageCount?: number;
|
|
||||||
queuePosition?: number;
|
|
||||||
sameModelRunningCount?: number;
|
|
||||||
concurrencyLimit?: number;
|
|
||||||
estimatedWaitMs?: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function findImageGenerationFailure(
|
|
||||||
messages: { role: string; parts?: unknown[] }[],
|
|
||||||
userMessageId: string,
|
|
||||||
): ImageGenerationStatusStreamPayload | null {
|
|
||||||
if (!userMessageId) return null;
|
|
||||||
for (const message of [...messages].reverse()) {
|
|
||||||
if (message.role !== "assistant") continue;
|
|
||||||
for (const part of message.parts || []) {
|
|
||||||
const candidate = part as { type?: string; data?: ImageGenerationStatusStreamPayload };
|
|
||||||
if (candidate.type !== "data-imageGenerationStatus") continue;
|
|
||||||
const status = candidate.data;
|
|
||||||
if (status?.phase === "failed" && status.failure && status.userMessageId === userMessageId) {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
const PREVIEW_WIDTHS = [640, 960, 1280, 1600, 2048, 2600, 3200, 4096];
|
|
||||||
|
|
||||||
export function canvasImagePreviewWidth(width: number, maxWidth: number) {
|
|
||||||
return Math.min(maxWidth, PREVIEW_WIDTHS.find(value => value >= width) || 4096);
|
|
||||||
}
|
|
||||||
@ -1,96 +0,0 @@
|
|||||||
import "server-only";
|
|
||||||
import { NextResponse } from "next/server";
|
|
||||||
|
|
||||||
import {
|
|
||||||
BoxCutoutServiceError,
|
|
||||||
executeBoxCutoutJob,
|
|
||||||
} from "@/lib/ai/box-cutout/service";
|
|
||||||
import { RmbgServiceError } from "@/lib/ai/rmbg/service";
|
|
||||||
import { getPointsSystemConfig } from "@/lib/db/queries";
|
|
||||||
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
|
||||||
import { getImageToolCreditCost } from "@/lib/points/config";
|
|
||||||
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
|
||||||
|
|
||||||
type BoxCutoutRequest = {
|
|
||||||
imageUrl?: string;
|
|
||||||
cropRect?: {
|
|
||||||
x?: number;
|
|
||||||
y?: number;
|
|
||||||
width?: number;
|
|
||||||
height?: number;
|
|
||||||
};
|
|
||||||
paddingRatio?: number;
|
|
||||||
prompt?: string;
|
|
||||||
chatId?: string | null;
|
|
||||||
messageId?: string | null;
|
|
||||||
skipGallerySave?: boolean;
|
|
||||||
sourceNodeId?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function executeBoxCutoutPOST(request: Request, userId: string) {
|
|
||||||
const requestId = request.headers.get("x-request-id") || crypto.randomUUID();
|
|
||||||
try {
|
|
||||||
if (!userId) {
|
|
||||||
return NextResponse.json({ success: false, error: "未授权,请先登录" }, { status: 401 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const body = (await request.json()) as BoxCutoutRequest;
|
|
||||||
const rawImageUrl = String(body.imageUrl || "").trim();
|
|
||||||
if (!rawImageUrl) {
|
|
||||||
throw new BoxCutoutServiceError("缺少原图链接,请重新选择图片后提交。", 400);
|
|
||||||
}
|
|
||||||
|
|
||||||
const resolvedImageUrl = new URL(rawImageUrl, request.url).toString();
|
|
||||||
const pointsConfig = await getPointsSystemConfig();
|
|
||||||
const pointsCharge = await prepareFeaturePointsCharge({
|
|
||||||
userId,
|
|
||||||
unitCost: getImageToolCreditCost(pointsConfig, "box-cutout"),
|
|
||||||
reason: "image_box_cutout",
|
|
||||||
modelId: "box-cutout",
|
|
||||||
note: "框选抠图消耗积分",
|
|
||||||
pointsConfig,
|
|
||||||
});
|
|
||||||
if (!pointsCharge.ok) {
|
|
||||||
return NextResponse.json(pointsCharge.payload, { status: pointsCharge.status });
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await executeBoxCutoutJob({
|
|
||||||
requestId,
|
|
||||||
userId,
|
|
||||||
imageUrl: resolvedImageUrl,
|
|
||||||
cropRect: body.cropRect || {},
|
|
||||||
paddingRatio: body.paddingRatio,
|
|
||||||
prompt: body.prompt || "框选抠图",
|
|
||||||
chatId: body.chatId || null,
|
|
||||||
messageId: body.messageId || null,
|
|
||||||
skipGallerySave: body.skipGallerySave === true,
|
|
||||||
sourceNodeId: body.sourceNodeId,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (pointsCharge.chargeable) {
|
|
||||||
try {
|
|
||||||
await pointsCharge.consume();
|
|
||||||
} catch (pointsError) {
|
|
||||||
console.error("[Points] Failed to consume box cutout points:", pointsError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({ ...result, requestId });
|
|
||||||
} catch (error) {
|
|
||||||
const failure = getRequestFailureDetails(error, "框选抠图未能完成");
|
|
||||||
const payload = {
|
|
||||||
success: false,
|
|
||||||
error: formatRequestFailure(failure, requestId),
|
|
||||||
detail: error instanceof Error ? error.message : "框选抠图未能完成",
|
|
||||||
code: failure.code,
|
|
||||||
failure,
|
|
||||||
requestId,
|
|
||||||
};
|
|
||||||
if (error instanceof BoxCutoutServiceError || error instanceof RmbgServiceError) {
|
|
||||||
return NextResponse.json(payload, { status: error.status });
|
|
||||||
}
|
|
||||||
|
|
||||||
console.error("[BoxCutout] 处理失败:", error);
|
|
||||||
return NextResponse.json(payload, { status: 500 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -9,11 +9,9 @@ import {
|
|||||||
import { getPointsSystemConfig } from "@/lib/db/queries";
|
import { getPointsSystemConfig } from "@/lib/db/queries";
|
||||||
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
import { prepareFeaturePointsCharge } from "@/lib/points/feature-charge";
|
||||||
import { getImageToolCreditCost } from "@/lib/points/config";
|
import { getImageToolCreditCost } from "@/lib/points/config";
|
||||||
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
|
||||||
|
|
||||||
|
|
||||||
export async function executeRemoveBackgroundPOST(request: Request, trustedUserId: string) {
|
export async function executeRemoveBackgroundPOST(request: Request, trustedUserId: string) {
|
||||||
const requestId = request.headers.get("x-request-id") || crypto.randomUUID();
|
|
||||||
try {
|
try {
|
||||||
const session = { user: { id: trustedUserId } };
|
const session = { user: { id: trustedUserId } };
|
||||||
if (!session?.user?.id) {
|
if (!session?.user?.id) {
|
||||||
@ -29,7 +27,6 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI
|
|||||||
let messageId: string | null = null;
|
let messageId: string | null = null;
|
||||||
let skipGallerySave = false;
|
let skipGallerySave = false;
|
||||||
let sourceImageUrl: string | null = null;
|
let sourceImageUrl: string | null = null;
|
||||||
let inputImageUrl: string | null = null;
|
|
||||||
|
|
||||||
if (contentType.includes("application/json")) {
|
if (contentType.includes("application/json")) {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
@ -43,7 +40,6 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI
|
|||||||
|
|
||||||
if (imageUrl) {
|
if (imageUrl) {
|
||||||
sourceImageUrl = String(imageUrl);
|
sourceImageUrl = String(imageUrl);
|
||||||
inputImageUrl = sourceImageUrl;
|
|
||||||
imageBuffer = await fetchImageBufferFromUrl(sourceImageUrl);
|
imageBuffer = await fetchImageBufferFromUrl(sourceImageUrl);
|
||||||
} else if (body.image_base64) {
|
} else if (body.image_base64) {
|
||||||
imageBase64 = String(body.image_base64);
|
imageBase64 = String(body.image_base64);
|
||||||
@ -98,7 +94,6 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = await executeRmbgJob({
|
const result = await executeRmbgJob({
|
||||||
requestId,
|
|
||||||
userId: session.user.id,
|
userId: session.user.id,
|
||||||
imageBuffer,
|
imageBuffer,
|
||||||
imageBase64,
|
imageBase64,
|
||||||
@ -106,7 +101,6 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI
|
|||||||
chatId,
|
chatId,
|
||||||
messageId,
|
messageId,
|
||||||
skipGallerySave,
|
skipGallerySave,
|
||||||
inputImageUrl,
|
|
||||||
sourceImageUrl,
|
sourceImageUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -120,29 +114,22 @@ export async function executeRemoveBackgroundPOST(request: Request, trustedUserI
|
|||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
requestId,
|
|
||||||
...result,
|
...result,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof RmbgServiceError) {
|
if (error instanceof RmbgServiceError) {
|
||||||
const failure = getRequestFailureDetails(error);
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
success: false,
|
error: error.message,
|
||||||
error: formatRequestFailure(failure, requestId),
|
|
||||||
detail: error.detail || error.message,
|
detail: error.detail || error.message,
|
||||||
code: failure.code,
|
|
||||||
failure,
|
|
||||||
requestId,
|
|
||||||
},
|
},
|
||||||
{ status: error.status }
|
{ status: error.status }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.error("[RMBG API] 处理错误:", error);
|
console.error("[RMBG API] 处理错误:", error);
|
||||||
const failure = getRequestFailureDetails(error, "抠图请求未能完成");
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ success: false, error: formatRequestFailure(failure, requestId), detail: error instanceof Error ? error.message : "未知错误", code: failure.code, failure, requestId },
|
{ error: "服务器内部错误", detail: error instanceof Error ? error.message : "未知错误" },
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@ import { createHash } from "node:crypto";
|
|||||||
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "@/lib/storage/retired-storage";
|
||||||
import { fetchImageBytes, onceImageBytes } from "@/lib/storage/image-byte-cache";
|
import { fetchImageBytes, onceImageBytes } from "@/lib/storage/image-byte-cache";
|
||||||
import { extractImageProxySourceUrl, inferOriginalImageUrl } from "@/lib/image-url";
|
import { extractImageProxySourceUrl, inferOriginalImageUrl } from "@/lib/image-url";
|
||||||
import { getDownloadFailure } from "./download-error";
|
|
||||||
|
|
||||||
type DownloadFileItem = {
|
type DownloadFileItem = {
|
||||||
url: string;
|
url: string;
|
||||||
@ -32,7 +31,6 @@ function sleep(ms: number) {
|
|||||||
|
|
||||||
function normalizeDownloadError(error: unknown): string {
|
function normalizeDownloadError(error: unknown): string {
|
||||||
if (error instanceof Error) return error.message || "download failed";
|
if (error instanceof Error) return error.message || "download failed";
|
||||||
if (error && typeof error === "object" && "message" in error && typeof error.message === "string") return error.message;
|
|
||||||
if (typeof error === "string") return error;
|
if (typeof error === "string") return error;
|
||||||
return "download failed";
|
return "download failed";
|
||||||
}
|
}
|
||||||
@ -332,7 +330,6 @@ export async function fetchCanvasDownloadAsset(params: {
|
|||||||
try {
|
try {
|
||||||
const data = await fetchImageBytes(candidateUrl, {
|
const data = await fetchImageBytes(candidateUrl, {
|
||||||
timeoutMs,
|
timeoutMs,
|
||||||
signal: params.signal,
|
|
||||||
headers: buildRemoteFetchHeaders(candidateUrl, params.requestOrigin, params.cookie),
|
headers: buildRemoteFetchHeaders(candidateUrl, params.requestOrigin, params.cookie),
|
||||||
// Authenticated local assets must never share a cache across users.
|
// Authenticated local assets must never share a cache across users.
|
||||||
scope: new URL(candidateUrl).origin === params.requestOrigin && params.cookie
|
scope: new URL(candidateUrl).origin === params.requestOrigin && params.cookie
|
||||||
@ -347,13 +344,11 @@ export async function fetchCanvasDownloadAsset(params: {
|
|||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
params.signal?.throwIfAborted();
|
params.signal?.throwIfAborted();
|
||||||
const message = normalizeDownloadError(error);
|
lastError = normalizeDownloadError(error);
|
||||||
if (!lastError || getDownloadFailure(message).retryable || !getDownloadFailure(lastError).retryable) lastError = message;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Archiving may have completed during the failed fetch. Resolve aliases
|
// Archiving may have completed during the failed fetch. Resolve aliases
|
||||||
// again before retrying, rather than spending every attempt on the old URL.
|
// again before retrying, rather than spending every attempt on the old URL.
|
||||||
if (!getDownloadFailure(lastError).retryable) break;
|
|
||||||
if (attempt < retryCount) await sleep(350 * attempt);
|
if (attempt < retryCount) await sleep(350 * attempt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -40,6 +40,22 @@ function clamp(value: number, min: number, max: number) {
|
|||||||
return Math.min(max, Math.max(min, value));
|
return Math.min(max, Math.max(min, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function executeScaleAdjustRmbgJob(
|
||||||
|
params: Parameters<typeof executeRmbgJob>[0]
|
||||||
|
) {
|
||||||
|
let lastError: unknown;
|
||||||
|
for (let attempt = 1; attempt <= 3; attempt += 1) {
|
||||||
|
try {
|
||||||
|
return await executeRmbgJob(params);
|
||||||
|
} catch (error) {
|
||||||
|
lastError = error;
|
||||||
|
if (attempt === 3) break;
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, attempt * 800));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw lastError;
|
||||||
|
}
|
||||||
|
|
||||||
function rectToPixelBox(
|
function rectToPixelBox(
|
||||||
rect: CanvasScaleAdjustRect,
|
rect: CanvasScaleAdjustRect,
|
||||||
sourceWidth: number,
|
sourceWidth: number,
|
||||||
@ -204,7 +220,7 @@ async function buildPersonExclusionAlpha(params: {
|
|||||||
userId: string;
|
userId: string;
|
||||||
}) {
|
}) {
|
||||||
try {
|
try {
|
||||||
const person = await executeRmbgJob({
|
const person = await executeScaleAdjustRmbgJob({
|
||||||
foregroundType: "person",
|
foregroundType: "person",
|
||||||
imageBuffer: params.sourceBuffer,
|
imageBuffer: params.sourceBuffer,
|
||||||
prompt: "比例调整人物保护蒙版",
|
prompt: "比例调整人物保护蒙版",
|
||||||
@ -314,7 +330,7 @@ export async function buildScaleAdjustProductLockAssets(params: {
|
|||||||
.png({ compressionLevel: 6 })
|
.png({ compressionLevel: 6 })
|
||||||
.toBuffer();
|
.toBuffer();
|
||||||
semanticSourceCropBuffers.push(cropBuffer);
|
semanticSourceCropBuffers.push(cropBuffer);
|
||||||
const subject = await executeRmbgJob({
|
const subject = await executeScaleAdjustRmbgJob({
|
||||||
imageBuffer: cropBuffer,
|
imageBuffer: cropBuffer,
|
||||||
prompt: "比例调整完整产品蒙版",
|
prompt: "比例调整完整产品蒙版",
|
||||||
skipGallerySave: true,
|
skipGallerySave: true,
|
||||||
|
|||||||
@ -1,55 +0,0 @@
|
|||||||
export type CanvasDraft = { base: string; snapshot: string };
|
|
||||||
const key = (projectId: string) => `canvas-unsynced:${projectId}`;
|
|
||||||
|
|
||||||
export function readCanvasDraft(projectId: string): CanvasDraft | null {
|
|
||||||
try {
|
|
||||||
const value = JSON.parse(localStorage.getItem(key(projectId)) || "null");
|
|
||||||
return typeof value?.base === "string" && typeof value?.snapshot === "string" ? value : null;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function writeCanvasDraft(projectId: string, draft: CanvasDraft): boolean {
|
|
||||||
try {
|
|
||||||
localStorage.setItem(key(projectId), JSON.stringify(draft));
|
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function clearCanvasDraft(projectId: string) {
|
|
||||||
try {
|
|
||||||
localStorage.removeItem(key(projectId));
|
|
||||||
} catch {
|
|
||||||
// Keep the current in-memory canvas when browser storage is unavailable.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const composerKey = (chatId: string) => `canvas-unsent-message:${chatId}`;
|
|
||||||
export function writeCanvasComposerDraft(chatId: string, draft: { text: string; attachments: unknown[] }) {
|
|
||||||
try {
|
|
||||||
localStorage.setItem(composerKey(chatId), JSON.stringify(draft));
|
|
||||||
return true;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function readCanvasComposerDraft<T>(chatId: string): { text: string; attachments: T[] } | null {
|
|
||||||
try {
|
|
||||||
const value = JSON.parse(localStorage.getItem(composerKey(chatId)) || "null");
|
|
||||||
return typeof value?.text === "string" && Array.isArray(value?.attachments) ? value : null;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function clearCanvasComposerDraft(chatId: string) {
|
|
||||||
try {
|
|
||||||
localStorage.removeItem(composerKey(chatId));
|
|
||||||
} catch {
|
|
||||||
// Keep the current editor contents when browser storage is unavailable.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,6 +1,5 @@
|
|||||||
import { buildImageProxyUrl, getImageDownloadExtension } from "@/lib/image-delivery";
|
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
import { inferOriginalImageUrl } from "@/lib/image-url";
|
import { inferOriginalImageUrl } from "@/lib/image-url";
|
||||||
import { formatDownloadFailure, getDownloadFailure, type DownloadFailure } from "@/lib/canvas/download-error";
|
|
||||||
|
|
||||||
export type CanvasImageDownloadPhase = "preparing" | "downloading" | "saving";
|
export type CanvasImageDownloadPhase = "preparing" | "downloading" | "saving";
|
||||||
|
|
||||||
@ -27,8 +26,6 @@ export class CanvasImageDownloadError extends Error {
|
|||||||
code: CanvasImageDownloadErrorCode;
|
code: CanvasImageDownloadErrorCode;
|
||||||
retryAfterSeconds?: number;
|
retryAfterSeconds?: number;
|
||||||
status?: number;
|
status?: number;
|
||||||
failure?: DownloadFailure;
|
|
||||||
requestId?: string;
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
message: string,
|
message: string,
|
||||||
@ -36,8 +33,6 @@ export class CanvasImageDownloadError extends Error {
|
|||||||
code: CanvasImageDownloadErrorCode;
|
code: CanvasImageDownloadErrorCode;
|
||||||
retryAfterSeconds?: number;
|
retryAfterSeconds?: number;
|
||||||
status?: number;
|
status?: number;
|
||||||
failure?: DownloadFailure;
|
|
||||||
requestId?: string;
|
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
super(message);
|
super(message);
|
||||||
@ -45,14 +40,11 @@ export class CanvasImageDownloadError extends Error {
|
|||||||
this.code = options.code;
|
this.code = options.code;
|
||||||
this.retryAfterSeconds = options.retryAfterSeconds;
|
this.retryAfterSeconds = options.retryAfterSeconds;
|
||||||
this.status = options.status;
|
this.status = options.status;
|
||||||
this.failure = options.failure;
|
|
||||||
this.requestId = options.requestId;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCanvasImageDownloadErrorMessage(error: unknown) {
|
export function getCanvasImageDownloadErrorMessage(error: unknown) {
|
||||||
if (error instanceof CanvasImageDownloadError) {
|
if (error instanceof CanvasImageDownloadError) {
|
||||||
if (error.failure) return formatDownloadFailure(error.failure, error.requestId);
|
|
||||||
if (error.code === "rate_limited") {
|
if (error.code === "rate_limited") {
|
||||||
return error.retryAfterSeconds
|
return error.retryAfterSeconds
|
||||||
? `图片下载请求过于频繁,请 ${error.retryAfterSeconds} 秒后重试`
|
? `图片下载请求过于频繁,请 ${error.retryAfterSeconds} 秒后重试`
|
||||||
@ -62,10 +54,10 @@ export function getCanvasImageDownloadErrorMessage(error: unknown) {
|
|||||||
return "原图暂时无法读取,图片仍保留在画布中,请稍后重试";
|
return "原图暂时无法读取,图片仍保留在画布中,请稍后重试";
|
||||||
}
|
}
|
||||||
if (error.code === "preparation_timeout") {
|
if (error.code === "preparation_timeout") {
|
||||||
return `原图仍在后台准备中,请稍后再次点击下载${error.requestId ? `\n下载编号:${error.requestId}` : ""}`;
|
return "原图仍在后台准备中,请稍后再次点击下载";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return formatDownloadFailure(getDownloadFailure(error));
|
return error instanceof Error ? error.message : "图片下载失败,请稍后重试";
|
||||||
}
|
}
|
||||||
|
|
||||||
type DownloadCanvasImageParams = {
|
type DownloadCanvasImageParams = {
|
||||||
@ -79,8 +71,6 @@ type DownloadCanvasImageParams = {
|
|||||||
type DownloadErrorPayload = {
|
type DownloadErrorPayload = {
|
||||||
error?: string;
|
error?: string;
|
||||||
retryAfter?: number;
|
retryAfter?: number;
|
||||||
failure?: DownloadFailure;
|
|
||||||
requestId?: string;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const MAX_RATE_LIMIT_RETRIES = 1;
|
const MAX_RATE_LIMIT_RETRIES = 1;
|
||||||
@ -176,63 +166,6 @@ function triggerBlobDownload(blob: Blob, filename: string) {
|
|||||||
window.setTimeout(() => URL.revokeObjectURL(blobUrl), 1000);
|
window.setTimeout(() => URL.revokeObjectURL(blobUrl), 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function triggerCdnDownload(result: { downloadUrl: string; filename: string; bytes?: number }, signal?: AbortSignal) {
|
|
||||||
if (typeof result.downloadUrl !== "string" || new URL(result.downloadUrl).protocol !== "https:") {
|
|
||||||
throw new Error("下载链接无效");
|
|
||||||
}
|
|
||||||
signal?.throwIfAborted();
|
|
||||||
const frame = document.createElement("iframe");
|
|
||||||
frame.hidden = true;
|
|
||||||
frame.src = result.downloadUrl;
|
|
||||||
document.body.appendChild(frame);
|
|
||||||
window.setTimeout(() => frame.remove(), 10 * 60_000);
|
|
||||||
return { filename: result.filename, bytes: result.bytes || 0 };
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Keep the source format and bytes for chat, gallery and reference downloads. */
|
|
||||||
export async function downloadOriginalImage({ url, name, signal, onProgress }: DownloadCanvasImageParams): Promise<CanvasImageDownloadResult> {
|
|
||||||
const source = String(url || "").trim();
|
|
||||||
if (!source) throw new Error("图片地址无效");
|
|
||||||
const proxy = buildImageProxyUrl(source, { delivery: "original" });
|
|
||||||
const filenameBase = sanitizeDownloadName(name || inferDownloadNameFromUrl(source), "image");
|
|
||||||
const target = new URL(proxy, window.location.origin);
|
|
||||||
onProgress?.({ phase: "preparing", message: "正在准备原图..." });
|
|
||||||
const checkedFetch = async (input: string) => {
|
|
||||||
let response: Response;
|
|
||||||
try { response = await fetch(input, { signal }); }
|
|
||||||
catch (error) {
|
|
||||||
if (signal?.aborted) throw error;
|
|
||||||
const failure = getDownloadFailure(error);
|
|
||||||
throw new CanvasImageDownloadError(failure.message, { code: "request_failed", failure });
|
|
||||||
}
|
|
||||||
if (!response.ok) {
|
|
||||||
const payload = await response.json().catch(() => null) as DownloadErrorPayload | null;
|
|
||||||
throw new CanvasImageDownloadError(payload?.error || "原图下载失败", { code: "request_failed",
|
|
||||||
status: response.status, failure: payload?.failure || getDownloadFailure(`HTTP ${response.status}`), requestId: payload?.requestId });
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
};
|
|
||||||
if (target.origin === window.location.origin && target.pathname === "/api/image-proxy") {
|
|
||||||
target.searchParams.set("download", "resolve");
|
|
||||||
target.searchParams.set("name", filenameBase);
|
|
||||||
const metadata = await checkedFetch(target.pathname + target.search);
|
|
||||||
const delivery = await metadata.json();
|
|
||||||
if (delivery.delivery === "cdn") {
|
|
||||||
const result = triggerCdnDownload(delivery, signal);
|
|
||||||
onProgress?.({ phase: "downloading", message: "已开始下载,可在浏览器下载列表查看进度" });
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
if (delivery.delivery !== "proxy") throw new Error("下载链接无效");
|
|
||||||
}
|
|
||||||
const response = await checkedFetch(proxy);
|
|
||||||
const blob = await readResponseBlobWithProgress(response, onProgress);
|
|
||||||
if (!blob.type.toLowerCase().startsWith("image/")) throw new Error("Source is not an image");
|
|
||||||
const extension = getImageDownloadExtension(blob.type);
|
|
||||||
const filename = extension ? `${filenameBase}.${extension}` : filenameBase;
|
|
||||||
triggerBlobDownload(blob, filename);
|
|
||||||
return { filename, bytes: blob.size };
|
|
||||||
}
|
|
||||||
|
|
||||||
function toDownloadCandidates(url: string, candidates: string[] = []) {
|
function toDownloadCandidates(url: string, candidates: string[] = []) {
|
||||||
const raw = String(url || "").trim();
|
const raw = String(url || "").trim();
|
||||||
const originalUrl = String(inferOriginalImageUrl(raw) || raw).trim();
|
const originalUrl = String(inferOriginalImageUrl(raw) || raw).trim();
|
||||||
@ -324,8 +257,7 @@ export async function downloadCanvasImageAsPng({
|
|||||||
let preparationPolls = 0;
|
let preparationPolls = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
signal?.throwIfAborted();
|
signal?.throwIfAborted();
|
||||||
try {
|
response = await fetch("/api/canvas/download-image", {
|
||||||
response = await fetch("/api/canvas/download-image", {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@ -336,12 +268,7 @@ export async function downloadCanvasImageAsPng({
|
|||||||
preparation: "async",
|
preparation: "async",
|
||||||
}),
|
}),
|
||||||
signal,
|
signal,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
if (signal?.aborted) throw error;
|
|
||||||
const failure = getDownloadFailure(error);
|
|
||||||
throw new CanvasImageDownloadError(failure.message, { code: "request_failed", failure });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.status === 202) {
|
if (response.status === 202) {
|
||||||
rateLimitRetries = 0;
|
rateLimitRetries = 0;
|
||||||
@ -351,7 +278,7 @@ export async function downloadCanvasImageAsPng({
|
|||||||
throw new CanvasImageDownloadError("下载准备状态无效", { code: "request_failed", status: 202 });
|
throw new CanvasImageDownloadError("下载准备状态无效", { code: "request_failed", status: 202 });
|
||||||
}
|
}
|
||||||
if (Date.now() >= preparationDeadline || preparationPolls >= 60) {
|
if (Date.now() >= preparationDeadline || preparationPolls >= 60) {
|
||||||
throw new CanvasImageDownloadError("原图仍在准备中", { code: "preparation_timeout", status: 202, requestId: preparing.requestId });
|
throw new CanvasImageDownloadError("原图仍在准备中", { code: "preparation_timeout", status: 202 });
|
||||||
}
|
}
|
||||||
onProgress?.({ phase: "preparing", message: preparing.message || "正在准备原图,完成后将自动下载" });
|
onProgress?.({ phase: "preparing", message: preparing.message || "正在准备原图,完成后将自动下载" });
|
||||||
const delaySeconds = Math.max(preparationPolls > 5 ? 5 : 1, Math.min(5, Number(preparing.retryAfter) || 3));
|
const delaySeconds = Math.max(preparationPolls > 5 ? 5 : 1, Math.min(5, Number(preparing.retryAfter) || 3));
|
||||||
@ -372,8 +299,6 @@ export async function downloadCanvasImageAsPng({
|
|||||||
code: "rate_limited",
|
code: "rate_limited",
|
||||||
retryAfterSeconds,
|
retryAfterSeconds,
|
||||||
status: response.status,
|
status: response.status,
|
||||||
failure: errorPayload?.failure,
|
|
||||||
requestId: errorPayload?.requestId,
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -396,18 +321,22 @@ export async function downloadCanvasImageAsPng({
|
|||||||
{
|
{
|
||||||
code: status === 502 ? "source_unavailable" : "request_failed",
|
code: status === 502 ? "source_unavailable" : "request_failed",
|
||||||
status,
|
status,
|
||||||
failure: errorPayload?.failure,
|
|
||||||
requestId: errorPayload?.requestId,
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.headers.get("content-type")?.includes("application/json")) {
|
if (response.headers.get("content-type")?.includes("application/json")) {
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
if (result.delivery !== "cdn") throw new Error("下载链接无效");
|
if (result.delivery !== "cdn" || typeof result.downloadUrl !== "string"
|
||||||
const downloaded = triggerCdnDownload(result, signal);
|
|| new URL(result.downloadUrl).protocol !== "https:") throw new Error("下载链接无效");
|
||||||
|
if (signal?.aborted) throw signal.reason || new DOMException("已取消下载", "AbortError");
|
||||||
|
const frame = document.createElement("iframe");
|
||||||
|
frame.hidden = true;
|
||||||
|
frame.src = result.downloadUrl;
|
||||||
|
document.body.appendChild(frame);
|
||||||
|
window.setTimeout(() => frame.remove(), 10 * 60_000);
|
||||||
onProgress?.({ phase: "downloading", message: "已开始下载,可在浏览器下载列表查看进度" });
|
onProgress?.({ phase: "downloading", message: "已开始下载,可在浏览器下载列表查看进度" });
|
||||||
return downloaded;
|
return { filename: result.filename, bytes: result.bytes || 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
const filename =
|
const filename =
|
||||||
|
|||||||
@ -63,5 +63,4 @@ export async function executeCanvasToolbarCapability(
|
|||||||
}
|
}
|
||||||
current.loadCanvas(JSON.parse(merged.snapshot).canvasData);
|
current.loadCanvas(JSON.parse(merged.snapshot).canvasData);
|
||||||
current.setCanvasRevision(String(result.data.updatedAt), String(result.data.updatedAt));
|
current.setCanvasRevision(String(result.data.updatedAt), String(result.data.updatedAt));
|
||||||
return { projectId, canvasData: result.data.canvasData, updatedAt: String(result.data.updatedAt) };
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +0,0 @@
|
|||||||
export const BEFORE_PAGE_REFRESH = "app:before-page-refresh";
|
|
||||||
export type PageRefreshRequest = {
|
|
||||||
save: (action: () => Promise<void>) => void;
|
|
||||||
block: (message: string) => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function preparePageRefresh() {
|
|
||||||
const actions: Array<() => Promise<void>> = [];
|
|
||||||
let blocked = "";
|
|
||||||
window.dispatchEvent(new CustomEvent<PageRefreshRequest>(BEFORE_PAGE_REFRESH, {
|
|
||||||
detail: { save: action => actions.push(action), block: message => { blocked ||= message; } },
|
|
||||||
}));
|
|
||||||
if (blocked) throw new Error(blocked);
|
|
||||||
for (const action of actions) await action();
|
|
||||||
}
|
|
||||||
@ -1,17 +1,5 @@
|
|||||||
import { normalizeLegacyAliyunStorageUrl } from "./storage/retired-storage";
|
|
||||||
|
|
||||||
const IMAGE_PROXY_PATH = "/api/image-proxy";
|
const IMAGE_PROXY_PATH = "/api/image-proxy";
|
||||||
|
|
||||||
/** Error fallbacks must keep unavailable legacy sources behind archive resolution. */
|
|
||||||
export function buildImageFallbackUrl(value: string | null | undefined): string {
|
|
||||||
const source = value || "";
|
|
||||||
const legacy = normalizeLegacyAliyunStorageUrl(source);
|
|
||||||
const modelImage = isAlibabaModelImageUrl(source);
|
|
||||||
if (!legacy && !modelImage) return source;
|
|
||||||
const proxy = buildImageProxyUrl(legacy || source, modelImage ? { width: 960, quality: 86, format: "webp" } : {});
|
|
||||||
return typeof window === "undefined" ? proxy : new URL(proxy, window.location.origin).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
type ImageProxyFormat = "webp" | "avif" | "jpeg" | "png";
|
type ImageProxyFormat = "webp" | "avif" | "jpeg" | "png";
|
||||||
|
|
||||||
export type ImageProxyOptions = {
|
export type ImageProxyOptions = {
|
||||||
@ -21,26 +9,6 @@ export type ImageProxyOptions = {
|
|||||||
delivery?: "display" | "proxy" | "original";
|
delivery?: "display" | "proxy" | "original";
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getFullscreenPreviewSize({
|
|
||||||
imageWidth,
|
|
||||||
imageHeight,
|
|
||||||
viewportWidth,
|
|
||||||
viewportHeight,
|
|
||||||
pixelRatio = 1,
|
|
||||||
}: {
|
|
||||||
imageWidth: number;
|
|
||||||
imageHeight: number;
|
|
||||||
viewportWidth: number;
|
|
||||||
viewportHeight: number;
|
|
||||||
pixelRatio?: number;
|
|
||||||
}) {
|
|
||||||
const ratio = imageWidth > 0 && imageHeight > 0 ? imageWidth / imageHeight : 1;
|
|
||||||
const displayWidth = Math.min(viewportWidth * 0.95, viewportHeight * 0.95 * ratio);
|
|
||||||
const requiredWidth = displayWidth * Math.max(1, pixelRatio || 1);
|
|
||||||
const requestWidth = [640, 960, 1280, 1600, 2048, 2560, 3200, 4096].find((width) => width >= requiredWidth) || 4096;
|
|
||||||
return { displayWidth, requestWidth };
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Preserve the actual response format when naming an original download. */
|
/** Preserve the actual response format when naming an original download. */
|
||||||
export function getImageDownloadExtension(contentType: string) {
|
export function getImageDownloadExtension(contentType: string) {
|
||||||
const mime = contentType.split(";")[0]?.trim().toLowerCase();
|
const mime = contentType.split(";")[0]?.trim().toLowerCase();
|
||||||
@ -52,8 +20,6 @@ export function getImageDownloadExtension(contentType: string) {
|
|||||||
"image/avif": "avif",
|
"image/avif": "avif",
|
||||||
"image/gif": "gif",
|
"image/gif": "gif",
|
||||||
"image/svg+xml": "svg",
|
"image/svg+xml": "svg",
|
||||||
"image/bmp": "bmp",
|
|
||||||
"image/tiff": "tiff",
|
|
||||||
};
|
};
|
||||||
return extensions[mime] || "";
|
return extensions[mime] || "";
|
||||||
}
|
}
|
||||||
@ -91,15 +57,6 @@ function isAliyunOssImageHost(hostname: string): boolean {
|
|||||||
return normalized.endsWith(".aliyuncs.com") && normalized.includes(".oss-");
|
return normalized.endsWith(".aliyuncs.com") && normalized.includes(".oss-");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Model result buckets allow downloading objects but deny OSS image processing. */
|
|
||||||
export function isAlibabaModelImageUrl(value: string): boolean {
|
|
||||||
try {
|
|
||||||
return /^dashscope(?:-[a-z0-9-]+)?\.oss-[a-z0-9-]+\.aliyuncs\.com$/i.test(new URL(value).hostname);
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isTrustedDirectStorageImageHost(hostname: string): boolean {
|
function isTrustedDirectStorageImageHost(hostname: string): boolean {
|
||||||
const normalized = hostname.trim().toLowerCase();
|
const normalized = hostname.trim().toLowerCase();
|
||||||
let configuredCdnHost = "";
|
let configuredCdnHost = "";
|
||||||
@ -226,8 +183,8 @@ export function buildImageProxyUrl(url: string, options: ImageProxyOptions = {})
|
|||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The same-origin endpoint can resolve a native CDN download without relying
|
// An original download must go through the same-origin byte-preserving
|
||||||
// on cross-origin fetch, while retaining a byte-preserving proxy fallback.
|
// proxy even for trusted storage hosts: a direct fetch may fail CORS.
|
||||||
if (options.delivery === "original") {
|
if (options.delivery === "original") {
|
||||||
const params = new URLSearchParams({ url: source, original: "1" });
|
const params = new URLSearchParams({ url: source, original: "1" });
|
||||||
return `${IMAGE_PROXY_PATH}?${params.toString()}`;
|
return `${IMAGE_PROXY_PATH}?${params.toString()}`;
|
||||||
@ -241,7 +198,7 @@ export function buildImageProxyUrl(url: string, options: ImageProxyOptions = {})
|
|||||||
return trustedDirectStorageUrl;
|
return trustedDirectStorageUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.delivery !== "proxy" && !normalizeLegacyAliyunStorageUrl(source) && !isAlibabaModelImageUrl(source)) {
|
if (options.delivery !== "proxy") {
|
||||||
const directDisplayUrl = resolveAliyunOssDisplayUrl(source, options);
|
const directDisplayUrl = resolveAliyunOssDisplayUrl(source, options);
|
||||||
if (directDisplayUrl) {
|
if (directDisplayUrl) {
|
||||||
return directDisplayUrl;
|
return directDisplayUrl;
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { scheduleImageDownloadPreparation } from "@/lib/storage/image-download-preparation";
|
import { preparePendingImageDownloads } from "@/lib/storage/image-download-preparation";
|
||||||
import "server-only";
|
import "server-only";
|
||||||
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
||||||
|
|
||||||
@ -32,7 +32,6 @@ export async function runImageArchiveJob() {
|
|||||||
console.log(`[ImageArchiveJob] Start image archive job (batchSize=${batchSize})`);
|
console.log(`[ImageArchiveJob] Start image archive job (batchSize=${batchSize})`);
|
||||||
|
|
||||||
await recoverStaleImageArchiveTasks();
|
await recoverStaleImageArchiveTasks();
|
||||||
scheduleImageDownloadPreparation();
|
|
||||||
const storeTasks = await getImageArchiveTasksToStore({ limit: batchSize });
|
const storeTasks = await getImageArchiveTasksToStore({ limit: batchSize });
|
||||||
let storeSucceeded = 0;
|
let storeSucceeded = 0;
|
||||||
let storeFailed = 0;
|
let storeFailed = 0;
|
||||||
@ -46,7 +45,6 @@ export async function runImageArchiveJob() {
|
|||||||
id: task.id,
|
id: task.id,
|
||||||
targetUrl: archived.url,
|
targetUrl: archived.url,
|
||||||
});
|
});
|
||||||
scheduleImageDownloadPreparation(task.id);
|
|
||||||
storeSucceeded += 1;
|
storeSucceeded += 1;
|
||||||
console.log("[ImageArchiveJob] stored", {
|
console.log("[ImageArchiveJob] stored", {
|
||||||
taskId: task.id,
|
taskId: task.id,
|
||||||
@ -110,6 +108,7 @@ export async function runImageArchiveJob() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const downloadTasks = await preparePendingImageDownloads();
|
||||||
const stats = await getImageArchiveTaskStats();
|
const stats = await getImageArchiveTaskStats();
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
@ -125,8 +124,8 @@ export async function runImageArchiveJob() {
|
|||||||
failed: replaceFailed,
|
failed: replaceFailed,
|
||||||
},
|
},
|
||||||
stats,
|
stats,
|
||||||
downloadTasks: 0,
|
downloadTasks,
|
||||||
pendingDownloads: await hasPendingImageDownloads(),
|
pendingDownloads: await hasPendingImageDownloads(),
|
||||||
};
|
};
|
||||||
}, { durable: true });
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,14 +3,13 @@ import { unlinkSync, writeFileSync } from "node:fs";
|
|||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
const state = globalThis as typeof globalThis & { __aiChatbotActiveWork?: number; __aiChatbotDurableWork?: number };
|
const state = globalThis as typeof globalThis & { __aiChatbotActiveWork?: number };
|
||||||
const path = join(process.platform === "win32" ? tmpdir() : "/tmp", `ai-chatbot-active-work-${process.pid}.json`);
|
const path = join(process.platform === "win32" ? tmpdir() : "/tmp", `ai-chatbot-active-work-${process.pid}.json`);
|
||||||
|
|
||||||
function update(delta: number, durable: boolean) {
|
function update(delta: number) {
|
||||||
state.__aiChatbotActiveWork = (state.__aiChatbotActiveWork || 0) + delta;
|
state.__aiChatbotActiveWork = (state.__aiChatbotActiveWork || 0) + delta;
|
||||||
if (durable) state.__aiChatbotDurableWork = (state.__aiChatbotDurableWork || 0) + delta;
|
|
||||||
try {
|
try {
|
||||||
writeFileSync(path, JSON.stringify({ pid: process.pid, active: state.__aiChatbotActiveWork, durable: state.__aiChatbotDurableWork || 0 }), { mode: 0o600 });
|
writeFileSync(path, JSON.stringify({ pid: process.pid, active: state.__aiChatbotActiveWork }), { mode: 0o600 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
try { unlinkSync(path); } catch {}
|
try { unlinkSync(path); } catch {}
|
||||||
console.warn("[Active Work] Failed to record running work:", error);
|
console.warn("[Active Work] Failed to record running work:", error);
|
||||||
@ -18,7 +17,7 @@ function update(delta: number, durable: boolean) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Includes generation that continues after the client connection closes. */
|
/** Includes generation that continues after the client connection closes. */
|
||||||
export async function trackRuntimeWork<T>(execute: () => T | Promise<T>, options: { durable?: boolean } = {}): Promise<T> {
|
export async function trackRuntimeWork<T>(execute: () => T | Promise<T>): Promise<T> {
|
||||||
update(1, options.durable === true);
|
update(1);
|
||||||
try { return await execute(); } finally { update(-1, options.durable === true); }
|
try { return await execute(); } finally { update(-1); }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -228,75 +228,9 @@ export function getRequestFailureDetails(
|
|||||||
const status = getRequestErrorStatus(error);
|
const status = getRequestErrorStatus(error);
|
||||||
const detail = (code: string, message: string, recommendation: string, outcome: "failed" | "unknown" = "failed") =>
|
const detail = (code: string, message: string, recommendation: string, outcome: "failed" | "unknown" = "failed") =>
|
||||||
({ code, message, recommendation, outcome });
|
({ code, message, recommendation, outcome });
|
||||||
if (/抠图参考图文件大小超限/.test(text)) {
|
|
||||||
return detail("RMBG_INPUT_TOO_LARGE", getRequestErrorMessage(error) || "抠图参考图超过 AOS 的 50 MB 上限。", "请手动压缩至 50 MB 以内后重新提交。本次未提交上游抠图任务。");
|
|
||||||
}
|
|
||||||
if (/抠图参考图分辨率超限/.test(text)) {
|
|
||||||
return detail("RMBG_INPUT_RESOLUTION_EXCEEDED", getRequestErrorMessage(error) || "抠图参考图超过 AOS 的 4096 像素上限。", "请手动将宽、高均调整至 4096 像素以内,再重新提交。本次未提交上游抠图任务。");
|
|
||||||
}
|
|
||||||
if (/抠图参考图格式不支持/.test(text)) {
|
|
||||||
return detail("RMBG_INPUT_FORMAT_UNSUPPORTED", getRequestErrorMessage(error) || "AOS 不支持此参考图格式。", "请导出为 JPG、PNG 或 WebP 后重新提交。本次未提交上游抠图任务。");
|
|
||||||
}
|
|
||||||
const aosResolution = text.match(/(?:resolution excess,\s*width=(\d+),\s*height=(\d+)|图片分辨率超限((\d+)x(\d+)))/i);
|
|
||||||
if (aosResolution) {
|
|
||||||
return detail("RMBG_PROVIDER_RESOLUTION_EXCEEDED", `AOS 拒绝了参考图:分辨率为 ${aosResolution[1] || aosResolution[3]}×${aosResolution[2] || aosResolution[4]} 像素,宽、高均须不超过 4096 像素。`, "请手动调整尺寸后重新提交。");
|
|
||||||
}
|
|
||||||
if (/AOS.*invalid file|图片文件无法识别/i.test(text)) {
|
|
||||||
return detail("RMBG_PROVIDER_INVALID_FILE", "AOS 无法识别参考图,文件可能损坏或格式不受支持。", "请确认原图可以正常打开,并导出为 JPG、PNG 或 WebP 后重新提交。");
|
|
||||||
}
|
|
||||||
const aosState = text.match(/AOS任务失败(状态 (-\d+),/);
|
|
||||||
if (aosState) {
|
|
||||||
const state = Number(aosState[1]);
|
|
||||||
if (state === -5) return detail("RMBG_PROVIDER_URL_TOO_LARGE", "AOS 拒绝读取参考图:链接取图大小超过上游的 30 MB 上限。", "请联系管理员核对文件提交方式;不要连续点击。上游文件上传接口允许最大 50 MB。");
|
|
||||||
if (state === -7) return detail("RMBG_PROVIDER_INVALID_FILE", "AOS 无法识别参考图,文件可能损坏或格式不受支持。", "请确认原图可以正常打开,并导出为 JPG、PNG 或 WebP 后重新提交。");
|
|
||||||
if (state === -3) return detail("RMBG_PROVIDER_SOURCE_DOWNLOAD_FAILED", "AOS 下载参考图失败,未能读取原图。", "请确认原图链接可打开;若原图正常,请联系管理员检查上游取图线路,并提供请求编号。");
|
|
||||||
if (state === -2) return detail("RMBG_PROVIDER_RESULT_STORAGE_FAILED", "AOS 已完成处理,但上游保存结果图片失败。", "请联系管理员核对原任务结果,避免立即重复抠图。", "unknown");
|
|
||||||
if (state === -8) return detail("RMBG_PROVIDER_PROCESSING_TIMEOUT", "AOS 抠图处理超时,上游已返回失败状态。", "请稍后重试;若持续发生,请联系管理员并提供请求编号。");
|
|
||||||
if (state === -11) return detail("RMBG_PROVIDER_EMPTY_RESULT", "AOS 抠图结果为空,未能得到有效主体。", "请检查图片中的主体是否清晰,尝试使用主体更明确的图片。");
|
|
||||||
if ([-17, -16, -14, -10].includes(state)) return detail("RMBG_PROVIDER_INPUT_REJECTED", `AOS 拒绝处理参考图,图片内容未通过上游检查(状态 ${state})。`, "请更换符合上游要求的参考图;如认为判断有误,请联系管理员核对请求编号。");
|
|
||||||
if (state === -15) return detail("RMBG_PROVIDER_RESOURCE_UNAVAILABLE", "AOS 处理资源不足,未能完成本次抠图。", "请稍后再试;持续失败时联系管理员检查上游服务。");
|
|
||||||
return detail("RMBG_PROVIDER_TASK_FAILED", getRequestErrorMessage(error) || `AOS 抠图任务失败(状态 ${state})。`, "请联系管理员并提供请求编号,核对上游任务详情。");
|
|
||||||
}
|
|
||||||
if (/AOS任务创建失败(HTTP 413)/.test(text)) {
|
|
||||||
return detail("RMBG_PROVIDER_FILE_TOO_LARGE", "AOS 拒绝了参考图:文件超过上游大小限制(文件上传最大 50 MB)。", "请手动压缩文件后重新提交;若文件已小于限制,请联系管理员核对上游返回。");
|
|
||||||
}
|
|
||||||
if (/AOS任务创建失败(HTTP (400|401|404))/.test(text)) {
|
|
||||||
return detail("RMBG_PROVIDER_REQUEST_REJECTED", getRequestErrorMessage(error) || "AOS 拒绝了抠图请求。", "请联系管理员核对上游参数、服务权限及接口配置,并提供请求编号。");
|
|
||||||
}
|
|
||||||
if (/AOS任务创建失败(HTTP (200|5\d\d))/.test(text)) {
|
|
||||||
return detail("RMBG_PROVIDER_CREATE_UNCONFIRMED", getRequestErrorMessage(error) || "AOS 未能确认抠图任务是否创建成功。", "请联系管理员核对原请求,避免连续提交造成重复处理。", "unknown");
|
|
||||||
}
|
|
||||||
const aosReadFailure = text.match(/AOS(任务查询|结果下载)失败(HTTP (\d+)/);
|
|
||||||
if (aosReadFailure) {
|
|
||||||
return aosReadFailure[1] === "任务查询"
|
|
||||||
? detail("RMBG_PROVIDER_POLL_FAILED", `AOS 查询抠图任务失败(HTTP ${aosReadFailure[2]}),原任务结果尚未确认。`, "请联系管理员核对原任务,不要重复提交。", "unknown")
|
|
||||||
: detail("RMBG_PROVIDER_RESULT_DOWNLOAD_FAILED", `抠图已完成,但结果图片下载失败(HTTP ${aosReadFailure[2]})。`, "请联系管理员按请求编号取回原任务结果,避免重复抠图。", "unknown");
|
|
||||||
}
|
|
||||||
if (isGatewayResponseTimeout(error)) {
|
if (isGatewayResponseTimeout(error)) {
|
||||||
return detail("RESULT_UNCONFIRMED", "模型服务响应超时(HTTP 524),生成结果尚未确认。", "请先查看历史记录,避免重复提交。", "unknown");
|
return detail("RESULT_UNCONFIRMED", "模型服务响应超时(HTTP 524),生成结果尚未确认。", "请先查看历史记录,避免重复提交。", "unknown");
|
||||||
}
|
}
|
||||||
if (/AOS创建任务超时/.test(text)) {
|
|
||||||
return detail("RMBG_CREATE_TIMEOUT", "抠图服务创建任务超时(120秒),结果尚未确认。", "请先查看原任务或联系管理员核对;不要连续点击,以免重复处理。", "unknown");
|
|
||||||
}
|
|
||||||
if (/AOS创建任务连接失败/.test(text)) {
|
|
||||||
return detail("RMBG_CREATE_CONNECTION_FAILED", "抠图服务创建任务时连接中断,结果尚未确认。", "请先查看原任务或联系管理员核对;不要连续点击,以免重复处理。", "unknown");
|
|
||||||
}
|
|
||||||
if (/AOS查询任务连接失败/.test(text)) {
|
|
||||||
return detail("RMBG_POLL_CONNECTION_FAILED", "抠图服务查询任务时连接中断,原任务可能仍在处理。", "请保留当前页面并联系管理员核对原任务,避免重新提交。", "unknown");
|
|
||||||
}
|
|
||||||
if (/AOS查询任务超时/.test(text)) {
|
|
||||||
return detail("RMBG_POLL_TIMEOUT", "抠图服务查询任务超时,原任务可能仍在处理。", "请保留当前页面并联系管理员核对原任务,避免重新提交。", "unknown");
|
|
||||||
}
|
|
||||||
if (/AOS结果下载连接失败/.test(text)) {
|
|
||||||
return detail("RMBG_RESULT_DOWNLOAD_CONNECTION_FAILED", "抠图已完成,但结果图片传输中断。", "请联系管理员按请求编号取回原任务结果,避免重复抠图。", "unknown");
|
|
||||||
}
|
|
||||||
if (/AOS结果下载超时/.test(text)) {
|
|
||||||
return detail("RMBG_RESULT_DOWNLOAD_TIMEOUT", "抠图已完成,但结果图片下载超时。", "请联系管理员按请求编号取回原任务结果,避免重复抠图。", "unknown");
|
|
||||||
}
|
|
||||||
const errorName = isRecord(error) ? String(error.name || "") : "";
|
|
||||||
if (["AbortError", "TimeoutError"].includes(errorName) || /operation was aborted/i.test(text)) {
|
|
||||||
return detail("RESULT_UNCONFIRMED", errorName === "TimeoutError" ? "请求等待超时,任务结果尚未确认。" : "本次请求连接已中止,任务结果尚未确认。", "请先核对原任务或历史记录;持续没有结果时联系管理员,避免重复生成。", "unknown");
|
|
||||||
}
|
|
||||||
if ((isRecord(error) && error.providerOutcomeUnknown === true) || /结果(?:尚)?未确认|结果暂未确认|request_outcome_unknown|任务执行中断|may still be running/i.test(text)) {
|
if ((isRecord(error) && error.providerOutcomeUnknown === true) || /结果(?:尚)?未确认|结果暂未确认|request_outcome_unknown|任务执行中断|may still be running/i.test(text)) {
|
||||||
return detail("RESULT_UNCONFIRMED", "任务结果尚未确认,可能仍在处理。", "请先查看原任务或历史记录;持续没有结果时联系管理员,避免重复生成。", "unknown");
|
return detail("RESULT_UNCONFIRMED", "任务结果尚未确认,可能仍在处理。", "请先查看原任务或历史记录;持续没有结果时联系管理员,避免重复生成。", "unknown");
|
||||||
}
|
}
|
||||||
@ -309,15 +243,9 @@ export function getRequestFailureDetails(
|
|||||||
if (/content.?polic|moderation|safety|policy_rejected|内容(?:敏感|审核|限制)|不能生成像那样|不能生成那样|违规/i.test(text)) {
|
if (/content.?polic|moderation|safety|policy_rejected|内容(?:敏感|审核|限制)|不能生成像那样|不能生成那样|违规/i.test(text)) {
|
||||||
return detail("CONTENT_POLICY_REJECTED", "模型服务因内容审核限制拒绝了本次请求。", "请调整描述或参考图中的受限内容后再提交;重复提交相同内容通常无法解决。");
|
return detail("CONTENT_POLICY_REJECTED", "模型服务因内容审核限制拒绝了本次请求。", "请调整描述或参考图中的受限内容后再提交;重复提交相同内容通常无法解决。");
|
||||||
}
|
}
|
||||||
if (/旧存储已停用|storage_retired/i.test(text)) {
|
|
||||||
return detail("SOURCE_STORAGE_RETIRED", "参考文件所在的旧存储已停用,且没有可用的归档副本。", "请重新上传原始文件后再提交;切换模型或重复提交无法恢复该文件。");
|
|
||||||
}
|
|
||||||
if (/上传到存储桶失败|存储.*失败|归档.*失败|archive.*failed|storage.*failed/i.test(text)) {
|
if (/上传到存储桶失败|存储.*失败|归档.*失败|archive.*failed|storage.*failed/i.test(text)) {
|
||||||
return detail("RESULT_STORAGE_FAILED", "处理结果保存失败,生成结果可能已经存在。", "请保留当前页面并联系管理员提供请求编号,先不要重复生成。", "unknown");
|
return detail("RESULT_STORAGE_FAILED", "处理结果保存失败,生成结果可能已经存在。", "请保留当前页面并联系管理员提供请求编号,先不要重复生成。", "unknown");
|
||||||
}
|
}
|
||||||
if (/failed to download (?:the )?image url|failed to download.*reference.*image/i.test(text)) {
|
|
||||||
return detail("PROVIDER_REFERENCE_FETCH_FAILED", "模型服务未能读取参考图。", "请重新上传参考图或切换模型后再提交;若原图仍可打开,请联系管理员检查模型取图线路,并提供请求编号。");
|
|
||||||
}
|
|
||||||
if (/参考图.*(?:下载失败|不可|失效)|无法获取图片|图片(?:源文件|链接).*不可|获取图片.*失败|get image url failed/i.test(text)) {
|
if (/参考图.*(?:下载失败|不可|失效)|无法获取图片|图片(?:源文件|链接).*不可|获取图片.*失败|get image url failed/i.test(text)) {
|
||||||
return detail("SOURCE_IMAGE_UNAVAILABLE", "请求中的图片无法读取,链接可能已失效或没有访问权限。", "请重新上传原图后再提交;若原图仍可打开,请联系管理员检查取图服务。");
|
return detail("SOURCE_IMAGE_UNAVAILABLE", "请求中的图片无法读取,链接可能已失效或没有访问权限。", "请重新上传原图后再提交;若原图仍可打开,请联系管理员检查取图服务。");
|
||||||
}
|
}
|
||||||
@ -330,9 +258,6 @@ export function getRequestFailureDetails(
|
|||||||
if (/neither a task ID nor output URLs|已完成.*未取回|completed without.*output/i.test(text)) {
|
if (/neither a task ID nor output URLs|已完成.*未取回|completed without.*output/i.test(text)) {
|
||||||
return detail("RESULT_UNCONFIRMED", "模型服务没有返回可读取的任务结果,生成状态尚未确认。", "请保留当前任务并联系管理员核对,避免重复生成。", "unknown");
|
return detail("RESULT_UNCONFIRMED", "模型服务没有返回可读取的任务结果,生成状态尚未确认。", "请保留当前任务并联系管理员核对,避免重复生成。", "unknown");
|
||||||
}
|
}
|
||||||
if (/(?:抠图|去水印|图片增强|图片|扩图)处理失败/.test(text)) {
|
|
||||||
return detail("PROCESSING_FAILED", "图片处理服务执行失败,未返回具体原因。", "请联系管理员并提供请求编号,先核对原任务结果,避免重复处理。", "unknown");
|
|
||||||
}
|
|
||||||
if (/service_error|upstream_server_error|excessive system load|generate(?: image)? failed/i.test(text)) {
|
if (/service_error|upstream_server_error|excessive system load|generate(?: image)? failed/i.test(text)) {
|
||||||
return detail("PROVIDER_EXECUTION_FAILED", "模型服务未能完成本次处理。", "请稍后重试;若连续失败,请切换模型或联系管理员,并提供请求编号。");
|
return detail("PROVIDER_EXECUTION_FAILED", "模型服务未能完成本次处理。", "请稍后重试;若连续失败,请切换模型或联系管理员,并提供请求编号。");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,37 +0,0 @@
|
|||||||
import { executeRemoveBackgroundPOST } from "@/lib/canvas/operations/rmbg";
|
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
|
||||||
|
|
||||||
const logger = createLogger("scene-fusion");
|
|
||||||
|
|
||||||
export async function preprocessProductImageWithRmbg(params: {
|
|
||||||
request: Request;
|
|
||||||
requestId: string;
|
|
||||||
userId: string;
|
|
||||||
productImageUrl: string;
|
|
||||||
preRmbgEnabled: boolean;
|
|
||||||
}): Promise<{ productImageUrl: string; usedPreRmbg: boolean; provider?: string; reason?: string }> {
|
|
||||||
const { request, requestId, userId, productImageUrl, preRmbgEnabled } = params;
|
|
||||||
if (!preRmbgEnabled) return { productImageUrl, usedPreRmbg: false, reason: "disabled_by_config" };
|
|
||||||
|
|
||||||
logger.info("pre_rmbg_started", { requestId });
|
|
||||||
try {
|
|
||||||
const response = await executeRemoveBackgroundPOST(new Request(new URL("/api/rmbg", request.url), {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json", "X-Request-ID": `${requestId}:pre-rmbg` },
|
|
||||||
body: JSON.stringify({ imageUrl: productImageUrl, prompt: "场景裂变预处理-抠图", skipGallerySave: true }),
|
|
||||||
}), userId);
|
|
||||||
const payload = await response.json();
|
|
||||||
if (!response.ok) {
|
|
||||||
logger.warn("pre_rmbg_failed", { requestId, status: response.status, code: payload?.failure?.code });
|
|
||||||
return { productImageUrl, usedPreRmbg: false, reason: String(payload?.error || `request_failed_${response.status}`) };
|
|
||||||
}
|
|
||||||
const cleanedUrl = String(payload?.original_url || payload?.thumbnail_url || "").trim();
|
|
||||||
if (!cleanedUrl || cleanedUrl === productImageUrl) {
|
|
||||||
return { productImageUrl, usedPreRmbg: false, reason: cleanedUrl ? "same_as_source" : "empty_result" };
|
|
||||||
}
|
|
||||||
return { productImageUrl: cleanedUrl, usedPreRmbg: true, provider: String(payload?.provider || "unknown") };
|
|
||||||
} catch (error) {
|
|
||||||
logger.warn("pre_rmbg_failed", error, { requestId });
|
|
||||||
return { productImageUrl, usedPreRmbg: false, reason: error instanceof Error ? error.message : "exception" };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -10,21 +10,17 @@ export async function resolveCanvasCdnDownload(urls: string[], filename: string)
|
|||||||
})));
|
})));
|
||||||
const task = await findImageArchiveDownloadTask(sources, true);
|
const task = await findImageArchiveDownloadTask(sources, true);
|
||||||
const asset = (task?.metadata as { downloadAsset?: { status?: string; url?: string; contentType?: string; bytes?: number } } | null)?.downloadAsset;
|
const asset = (task?.metadata as { downloadAsset?: { status?: string; url?: string; contentType?: string; bytes?: number } } | null)?.downloadAsset;
|
||||||
if (!task) return { task, direct: null, canPrepareDirect: false };
|
if (asset?.status !== "ready" || !asset.url || asset.contentType !== "image/png") return { task, direct: null };
|
||||||
const config = await getDefaultStorageConfig();
|
const config = await getDefaultStorageConfig();
|
||||||
if (config?.type !== "qiniu-kodo" || !config.publicUrl || !config.isEnabled) return { task, direct: null, canPrepareDirect: false };
|
if (config?.type !== "qiniu-kodo" || !config.publicUrl || !config.isEnabled) return { task, direct: null };
|
||||||
const ready = asset?.status === "ready" && asset.url && asset.contentType === "image/png";
|
const url = new URL(asset.url);
|
||||||
const source = ready ? asset.url : task.targetUrl;
|
|
||||||
if (!source) return { task, direct: null, canPrepareDirect: false };
|
|
||||||
const url = new URL(source);
|
|
||||||
const base = new URL(config.publicUrl);
|
const base = new URL(config.publicUrl);
|
||||||
const origin = process.env.CANVAS_QINIU_ORIGIN_URL;
|
const origin = process.env.CANVAS_QINIU_ORIGIN_URL;
|
||||||
if (origin && url.origin === new URL(origin).origin) url.host = base.host;
|
if (origin && url.origin === new URL(origin).origin) url.host = base.host;
|
||||||
if (base.protocol !== "https:" || url.origin !== base.origin
|
if (base.protocol !== "https:" || url.origin !== base.origin
|
||||||
|| !(url.pathname.startsWith(base.pathname.replace(/\/$/, "") + "/"))) return { task, direct: null, canPrepareDirect: false };
|
|| !(url.pathname.startsWith(base.pathname.replace(/\/$/, "") + "/"))) return { task, direct: null };
|
||||||
if (!ready) return { task, direct: null, canPrepareDirect: true };
|
|
||||||
// attname is interpreted by Kodo/CDN as Content-Disposition, including Chinese
|
// attname is interpreted by Kodo/CDN as Content-Disposition, including Chinese
|
||||||
// filenames. Keep all processing/auth parameters intact for correct cache keys.
|
// filenames. Keep all processing/auth parameters intact for correct cache keys.
|
||||||
url.searchParams.set("attname", filename);
|
url.searchParams.set("attname", filename);
|
||||||
return { task, direct: { delivery: "cdn", downloadUrl: url.toString(), filename, bytes: asset?.bytes || 0 }, canPrepareDirect: true };
|
return { task, direct: { delivery: "cdn", downloadUrl: url.toString(), filename, bytes: asset.bytes || 0 } };
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,72 +1,11 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { createReadStream } from "node:fs";
|
import { createReadStream } from "node:fs";
|
||||||
import { open } from "node:fs/promises";
|
import { PutObjectCommand } from "@aws-sdk/client-s3";
|
||||||
import { AbortMultipartUploadCommand, CompleteMultipartUploadCommand, CreateMultipartUploadCommand, HeadObjectCommand, PutObjectCommand, UploadPartCommand, type S3Client } from "@aws-sdk/client-s3";
|
|
||||||
import { getDefaultStorageConfig, getStorageConfigById } from "@/lib/db/queries";
|
import { getDefaultStorageConfig, getStorageConfigById } from "@/lib/db/queries";
|
||||||
import { createS3Client, toDbS3UploadConfig, buildS3PublicUrl } from "./service";
|
import { createS3Client, toDbS3UploadConfig, buildS3PublicUrl } from "./service";
|
||||||
|
|
||||||
type ZipPublication = { id: string; path: string; bytes: number; disposition: string; onProgress?: (bytes: number) => Promise<void> };
|
/** Only a completely written archive is uploaded. Local GET remains a fallback. */
|
||||||
const PART_BYTES = 8 * 1024 ** 2;
|
export async function publishCanvasZip(params: { id: string; path: string; bytes: number; disposition: string }) {
|
||||||
|
|
||||||
async function uploadZip(client: S3Client, bucket: string, key: string, params: ZipPublication) {
|
|
||||||
const metadata = { Bucket: bucket, Key: key, ContentType: "application/zip", ContentDisposition: params.disposition, CacheControl: "public, max-age=1800" };
|
|
||||||
if (params.bytes <= 2 * PART_BYTES) {
|
|
||||||
const body = createReadStream(params.path);
|
|
||||||
try {
|
|
||||||
await client.send(new PutObjectCommand({ ...metadata, Body: body, ContentLength: params.bytes }), { abortSignal: AbortSignal.timeout(90_000) });
|
|
||||||
await params.onProgress?.(params.bytes);
|
|
||||||
} finally { body.destroy(); }
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const initiated = await client.send(new CreateMultipartUploadCommand(metadata), { abortSignal: AbortSignal.timeout(15_000) });
|
|
||||||
if (!initiated.UploadId) throw new Error("七牛未返回分片上传编号");
|
|
||||||
const upload = { Bucket: bucket, Key: key, UploadId: initiated.UploadId };
|
|
||||||
const handle = await open(params.path, "r").catch(async error => {
|
|
||||||
await client.send(new AbortMultipartUploadCommand(upload), { abortSignal: AbortSignal.timeout(10_000) }).catch(() => undefined);
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
const parts: Array<{ PartNumber: number; ETag: string }> = [];
|
|
||||||
let cursor = 0;
|
|
||||||
let uploaded = 0;
|
|
||||||
let progress = Promise.resolve();
|
|
||||||
const controller = new AbortController();
|
|
||||||
try {
|
|
||||||
const workers = await Promise.allSettled(Array.from({ length: 3 }, async () => {
|
|
||||||
try {
|
|
||||||
while (!controller.signal.aborted) {
|
|
||||||
const index = cursor++;
|
|
||||||
const start = index * PART_BYTES;
|
|
||||||
if (start >= params.bytes) return;
|
|
||||||
const body = Buffer.allocUnsafe(Math.min(PART_BYTES, params.bytes - start));
|
|
||||||
let read = 0;
|
|
||||||
while (read < body.length) {
|
|
||||||
const result = await handle.read(body, read, body.length - read, start + read);
|
|
||||||
if (!result.bytesRead) throw new Error("完整 ZIP 文件读取中断,请重试下载");
|
|
||||||
read += result.bytesRead;
|
|
||||||
}
|
|
||||||
const part = await client.send(new UploadPartCommand({ ...upload, PartNumber: index + 1, Body: body, ContentLength: body.length }), {
|
|
||||||
abortSignal: AbortSignal.any([controller.signal, AbortSignal.timeout(180_000)]),
|
|
||||||
});
|
|
||||||
if (!part.ETag) throw new Error("七牛未确认上传分片");
|
|
||||||
parts.push({ PartNumber: index + 1, ETag: part.ETag });
|
|
||||||
uploaded += body.length;
|
|
||||||
const bytes = uploaded;
|
|
||||||
progress = progress.then(() => params.onProgress?.(bytes));
|
|
||||||
await progress;
|
|
||||||
}
|
|
||||||
} catch (error) { controller.abort(); throw error; }
|
|
||||||
}));
|
|
||||||
const failure = workers.find(worker => worker.status === "rejected");
|
|
||||||
if (failure?.status === "rejected") throw failure.reason;
|
|
||||||
await client.send(new CompleteMultipartUploadCommand({ ...upload, MultipartUpload: { Parts: parts.sort((a, b) => a.PartNumber - b.PartNumber) } }), { abortSignal: AbortSignal.timeout(30_000) });
|
|
||||||
} catch (error) {
|
|
||||||
await client.send(new AbortMultipartUploadCommand(upload), { abortSignal: AbortSignal.timeout(10_000) }).catch(() => undefined);
|
|
||||||
throw error;
|
|
||||||
} finally { await handle.close(); }
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Publish the complete ZIP before offering the accelerated download. */
|
|
||||||
export async function publishCanvasZip(params: ZipPublication) {
|
|
||||||
const raw = await getDefaultStorageConfig();
|
const raw = await getDefaultStorageConfig();
|
||||||
const config = toDbS3UploadConfig(raw);
|
const config = toDbS3UploadConfig(raw);
|
||||||
if (config?.provider !== "qiniu-kodo" || !config.publicUrl?.startsWith("https://")) return null;
|
if (config?.provider !== "qiniu-kodo" || !config.publicUrl?.startsWith("https://")) return null;
|
||||||
@ -74,37 +13,20 @@ export async function publishCanvasZip(params: ZipPublication) {
|
|||||||
const client = createS3Client(config);
|
const client = createS3Client(config);
|
||||||
// Qiniu's S3 endpoint expects an ordinary known-length body, without AWS trailers.
|
// Qiniu's S3 endpoint expects an ordinary known-length body, without AWS trailers.
|
||||||
client.config.requestChecksumCalculation = async () => "WHEN_REQUIRED";
|
client.config.requestChecksumCalculation = async () => "WHEN_REQUIRED";
|
||||||
let phase: "check" | "upload" | "verify" = "check";
|
const body = createReadStream(params.path);
|
||||||
const startedAt = Date.now();
|
|
||||||
let uploadedBytes = 0;
|
|
||||||
try {
|
try {
|
||||||
const existing = await client.send(new HeadObjectCommand({ Bucket: config.bucket, Key: key }), { abortSignal: AbortSignal.timeout(15_000) }).catch(error => {
|
await client.send(new PutObjectCommand({ Bucket: config.bucket, Key: key, Body: body,
|
||||||
if (error.$metadata?.httpStatusCode === 404) return null;
|
ContentLength: params.bytes, ContentType: "application/zip", ContentDisposition: params.disposition,
|
||||||
throw error;
|
CacheControl: "public, max-age=1800" }), { abortSignal: AbortSignal.timeout(90_000) });
|
||||||
});
|
const url = buildS3PublicUrl(config, key);
|
||||||
if (existing?.ContentLength !== params.bytes) {
|
|
||||||
phase = "upload";
|
|
||||||
await uploadZip(client, config.bucket, key, { ...params, onProgress: async bytes => {
|
|
||||||
uploadedBytes = bytes;
|
|
||||||
await params.onProgress?.(bytes);
|
|
||||||
} });
|
|
||||||
}
|
|
||||||
phase = "verify";
|
|
||||||
const target = new URL(buildS3PublicUrl(config, key));
|
|
||||||
target.searchParams.set("v", String(params.bytes));
|
|
||||||
const url = target.toString();
|
|
||||||
const response = await fetch(url, { method: "HEAD", redirect: "error", signal: AbortSignal.timeout(10_000) });
|
const response = await fetch(url, { method: "HEAD", redirect: "error", signal: AbortSignal.timeout(10_000) });
|
||||||
const length = Number(response.headers.get("content-length"));
|
const length = Number(response.headers.get("content-length"));
|
||||||
if (!response.ok || length !== params.bytes) throw new Error("CDN object is not ready");
|
if (!response.ok || length !== params.bytes) throw new Error("CDN object is not ready");
|
||||||
return { url, storageConfigId: raw!.id, key };
|
return { url, storageConfigId: raw!.id, key };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const kind = (error as Error).name;
|
console.warn("[CanvasDownloadZipTask] CDN upload/check failed; using complete local file", { taskId: params.id, kind: (error as Error).name });
|
||||||
const status = (error as { $metadata?: { httpStatusCode?: number } }).$metadata?.httpStatusCode;
|
return { url: null, storageConfigId: raw!.id, key };
|
||||||
console.warn("[CanvasDownloadZipTask] CDN publication failed", { taskId: params.id, phase, kind, status, bytes: params.bytes, uploadedBytes, elapsedMs: Date.now() - startedAt });
|
} finally { body.destroy(); client.destroy(); }
|
||||||
const reason = status === 401 || status === 403 ? `七牛拒绝访问(HTTP ${status}),请管理员检查存储权限` : phase === "check" ? "暂时无法确认七牛文件状态" : phase === "verify" ? "七牛下载链接尚未可用" : /AbortError|TimeoutError/.test(kind) ? "传送至七牛时超时" : "传送至七牛失败";
|
|
||||||
const advice = status === 401 || status === 403 ? "存储权限恢复后再次点击下载即可继续传送原 ZIP" : "请再次点击下载以继续传送原 ZIP";
|
|
||||||
return { url: null, storageConfigId: raw!.id, key, error: `ZIP 已打包完成,但${reason}。${advice};不会重新生成图片。若持续失败,请联系管理员并提供下载编号:${params.id}` };
|
|
||||||
} finally { client.destroy(); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeCanvasZipObject(object: { storageConfigId: string; key: string }) {
|
export async function removeCanvasZipObject(object: { storageConfigId: string; key: string }) {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
import { isRetiredStorageUrl } from "./retired-storage";
|
||||||
|
|
||||||
import { createHash } from "node:crypto";
|
import { createHash } from "node:crypto";
|
||||||
import {
|
import {
|
||||||
@ -22,8 +22,6 @@ export function getImageArchiveDisplayHoldMs() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function imageArchiveSourceKey(source: string) {
|
export function imageArchiveSourceKey(source: string) {
|
||||||
const legacy = normalizeLegacyAliyunStorageUrl(source);
|
|
||||||
if (legacy) return legacy;
|
|
||||||
if (!source.startsWith("data:")) return source;
|
if (!source.startsWith("data:")) return source;
|
||||||
const configured =
|
const configured =
|
||||||
process.env.NEXT_PUBLIC_APP_URL || process.env.AUTH_URL || process.env.NEXTAUTH_URL;
|
process.env.NEXT_PUBLIC_APP_URL || process.env.AUTH_URL || process.env.NEXTAUTH_URL;
|
||||||
@ -41,7 +39,7 @@ export async function enqueueImageArchiveTask(params: {
|
|||||||
metadata?: Record<string, unknown> | null;
|
metadata?: Record<string, unknown> | null;
|
||||||
}) {
|
}) {
|
||||||
const sourceUrl = String(params.sourceUrl || "").trim();
|
const sourceUrl = String(params.sourceUrl || "").trim();
|
||||||
if (!isGeneratedImageSource(sourceUrl) || isRetiredStorageUrl(sourceUrl) || normalizeLegacyAliyunStorageUrl(sourceUrl)) return null;
|
if (!isGeneratedImageSource(sourceUrl) || isRetiredStorageUrl(sourceUrl)) return null;
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const archiveAfterAt = params.archiveAfterAt || now;
|
const archiveAfterAt = params.archiveAfterAt || now;
|
||||||
@ -314,18 +312,6 @@ export async function getArchivedImageUrlMappings(
|
|||||||
return replacements;
|
return replacements;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Legacy OSS is unavailable; only an existing durable copy can serve its URLs. */
|
|
||||||
export async function resolveLegacyAliyunStorageUrl(source: string): Promise<string> {
|
|
||||||
const legacy = normalizeLegacyAliyunStorageUrl(source);
|
|
||||||
if (!legacy) return source;
|
|
||||||
const mappings = await getArchivedImageUrlMappings([source, legacy]);
|
|
||||||
const target = mappings[source] || mappings[legacy];
|
|
||||||
if (!target || normalizeLegacyAliyunStorageUrl(target) || isRetiredStorageUrl(target)) {
|
|
||||||
throw new Error(`HTTP 410: ${RETIRED_STORAGE_MESSAGE}`);
|
|
||||||
}
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalize late writes too: an archive can finish before a task/canvas save arrives.
|
// Normalize late writes too: an archive can finish before a task/canvas save arrives.
|
||||||
export async function resolveArchivedImageUrls<T>(
|
export async function resolveArchivedImageUrls<T>(
|
||||||
value: T,
|
value: T,
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
// Shared by the server and canvas; no storage or network work happens here.
|
// Shared by the server and canvas; no storage or network work happens here.
|
||||||
export type ArchivedImageUrlMap = Record<string, string>;
|
export type ArchivedImageUrlMap = Record<string, string>;
|
||||||
const legacyOssUrlPattern = /https?:\/\/demo-sparkai\.oss-cn-shanghai\.aliyuncs\.com\/[^\s"'<>`\\)\]}|]+/gi;
|
|
||||||
|
|
||||||
export function isGeneratedImageSource(value: string) {
|
export function isGeneratedImageSource(value: string) {
|
||||||
return (
|
return (
|
||||||
@ -12,7 +11,6 @@ export function collectImageArchiveSources(value: unknown): string[] {
|
|||||||
const urls = new Set<string>();
|
const urls = new Set<string>();
|
||||||
const visit = (item: unknown) => {
|
const visit = (item: unknown) => {
|
||||||
if (typeof item === "string") {
|
if (typeof item === "string") {
|
||||||
for (const match of item.matchAll(legacyOssUrlPattern)) urls.add(match[0].replace(/,+$/, ""));
|
|
||||||
if (isGeneratedImageSource(item)) urls.add(item);
|
if (isGeneratedImageSource(item)) urls.add(item);
|
||||||
else
|
else
|
||||||
for (const match of item.matchAll(/!\[[^\]]*\]\(([^)]+)\)/g)) {
|
for (const match of item.matchAll(/!\[[^\]]*\]\(([^)]+)\)/g)) {
|
||||||
@ -37,10 +35,6 @@ export function replaceArchivedImageUrls<T>(value: T, replacements: ArchivedImag
|
|||||||
.map((part) => replacements[part] || part)
|
.map((part) => replacements[part] || part)
|
||||||
.join("|||");
|
.join("|||");
|
||||||
return ``;
|
return ``;
|
||||||
}).replace(legacyOssUrlPattern, (url) => {
|
|
||||||
if (replacements[url]) return replacements[url];
|
|
||||||
const source = url.replace(/,+$/, "");
|
|
||||||
return (replacements[source] || source) + url.slice(source.length);
|
|
||||||
}) as T;
|
}) as T;
|
||||||
}
|
}
|
||||||
if (!value || typeof value !== "object" || value instanceof Date) return value;
|
if (!value || typeof value !== "object" || value instanceof Date) return value;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { isRetiredStorageUrl, normalizeLegacyAliyunStorageUrl, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
||||||
import { createHash, randomUUID } from "node:crypto";
|
import { createHash, randomUUID } from "node:crypto";
|
||||||
import { mkdir, readFile, writeFile, rename, readdir, stat, statfs, unlink } from "node:fs/promises";
|
import { mkdir, readFile, writeFile, rename, readdir, stat, statfs, unlink } from "node:fs/promises";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
@ -81,25 +81,18 @@ function isMediaResponse(data: ImageBytes) {
|
|||||||
if (/^<\?xml/i.test(prefix) && !/<svg[\s>]/i.test(prefix)) return false;
|
if (/^<\?xml/i.test(prefix) && !/<svg[\s>]/i.test(prefix)) return false;
|
||||||
return data.buffer.length > 0;
|
return data.buffer.length > 0;
|
||||||
}
|
}
|
||||||
export async function fetchImageBytes(url: string, options: { headers?: HeadersInit; timeoutMs?: number; scope?: string; signal?: AbortSignal } = {}) {
|
export async function fetchImageBytes(url: string, options: { headers?: HeadersInit; timeoutMs?: number; scope?: string } = {}) {
|
||||||
if (isRetiredStorageUrl(url)) throw new Error(`HTTP 410: ${RETIRED_STORAGE_MESSAGE}`);
|
if (isRetiredStorageUrl(url)) throw new Error(`HTTP 410: ${RETIRED_STORAGE_MESSAGE}`);
|
||||||
let sourceUrl = url;
|
return onceImageBytes(`source:${options.scope || ""}:${url}`, async () => {
|
||||||
if (normalizeLegacyAliyunStorageUrl(url)) {
|
if (url.startsWith("data:")) {
|
||||||
const { resolveLegacyAliyunStorageUrl } = await import("./image-archive-db");
|
const match = url.match(/^data:(image\/[a-z0-9.+-]+);base64,([a-z0-9+/=]+)$/i);
|
||||||
sourceUrl = await resolveLegacyAliyunStorageUrl(url);
|
|
||||||
}
|
|
||||||
return onceImageBytes(`source:${options.scope || ""}:${sourceUrl}`, async () => {
|
|
||||||
if (sourceUrl.startsWith("data:")) {
|
|
||||||
const match = sourceUrl.match(/^data:(image\/[a-z0-9.+-]+);base64,([a-z0-9+/=]+)$/i);
|
|
||||||
if (!match) throw new Error("Invalid inline generated image");
|
if (!match) throw new Error("Invalid inline generated image");
|
||||||
return { buffer: Buffer.from(match[2], "base64"), contentType: match[1] };
|
return { buffer: Buffer.from(match[2], "base64"), contentType: match[1] };
|
||||||
}
|
}
|
||||||
const response = await fetch(sourceUrl, {
|
const response = await fetch(url, {
|
||||||
headers: options.headers, cache: "no-store", redirect: "follow",
|
headers: options.headers, cache: "no-store", redirect: "follow",
|
||||||
// The deadline covers the body as well as the response headers.
|
// The deadline covers the body as well as the response headers.
|
||||||
signal: options.signal
|
signal: AbortSignal.timeout(options.timeoutMs || 60_000),
|
||||||
? AbortSignal.any([options.signal, AbortSignal.timeout(options.timeoutMs || 60_000)])
|
|
||||||
: AbortSignal.timeout(options.timeoutMs || 60_000),
|
|
||||||
});
|
});
|
||||||
if (!response.ok) { await response.body?.cancel(); throw new Error(`HTTP ${response.status}`); }
|
if (!response.ok) { await response.body?.cancel(); throw new Error(`HTTP ${response.status}`); }
|
||||||
const buffer = Buffer.from(await response.arrayBuffer());
|
const buffer = Buffer.from(await response.arrayBuffer());
|
||||||
|
|||||||
@ -2,21 +2,14 @@ import "server-only";
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { fetchImageBytes, cacheImageBytes } from "./image-byte-cache";
|
import { fetchImageBytes, cacheImageBytes } from "./image-byte-cache";
|
||||||
import { normalizeCanvasDownloadAsset } from "@/lib/canvas/photoshop-safe-download";
|
import { normalizeCanvasDownloadAsset } from "@/lib/canvas/photoshop-safe-download";
|
||||||
import { claimImageDownloadTask, saveImageDownloadAsset, getImageDownloadTasks, hasPendingImageDownloads } from "./image-archive-db";
|
import { claimImageDownloadTask, saveImageDownloadAsset, getImageDownloadTasks } from "./image-archive-db";
|
||||||
import { uploadFile } from "./service";
|
import { uploadFile } from "./service";
|
||||||
import { getDownloadFailure } from "@/lib/canvas/download-error";
|
|
||||||
import { createLogger } from "@/lib/observability/logger";
|
|
||||||
import { trackRuntimeWork } from "@/lib/observability/active-work";
|
|
||||||
|
|
||||||
const logger = createLogger("canvas.download");
|
|
||||||
|
|
||||||
export async function prepareImageArchiveDownload(id: string) {
|
export async function prepareImageArchiveDownload(id: string) {
|
||||||
const task = await claimImageDownloadTask(id);
|
const task = await claimImageDownloadTask(id);
|
||||||
if (!task?.targetUrl) return;
|
if (!task?.targetUrl) return;
|
||||||
let phase: "read" | "convert" | "store" = "read";
|
|
||||||
try {
|
try {
|
||||||
const source = await fetchImageBytes(task.targetUrl);
|
const source = await fetchImageBytes(task.targetUrl);
|
||||||
phase = "convert";
|
|
||||||
const asset = await normalizeCanvasDownloadAsset({
|
const asset = await normalizeCanvasDownloadAsset({
|
||||||
item: { url: task.targetUrl }, index: 0, bytes: source.buffer,
|
item: { url: task.targetUrl }, index: 0, bytes: source.buffer,
|
||||||
contentType: source.contentType, includeSequencePrefix: false,
|
contentType: source.contentType, includeSequencePrefix: false,
|
||||||
@ -24,22 +17,15 @@ export async function prepareImageArchiveDownload(id: string) {
|
|||||||
if (!asset.ok) throw new Error(asset.error);
|
if (!asset.ok) throw new Error(asset.error);
|
||||||
// An already compatible PNG shares the original object; other formats get
|
// An already compatible PNG shares the original object; other formats get
|
||||||
// one prepared PNG, without changing the original dimensions or transparency.
|
// one prepared PNG, without changing the original dimensions or transparency.
|
||||||
phase = "store";
|
|
||||||
const url = source.buffer.equals(asset.bytes) && asset.contentType === "image/png"
|
const url = source.buffer.equals(asset.bytes) && asset.contentType === "image/png"
|
||||||
? task.targetUrl
|
? task.targetUrl
|
||||||
: (await uploadFile(`canvas-download-${randomUUID()}.${asset.filename.split(".").pop() || "png"}`, Uint8Array.from(asset.bytes).buffer)).url;
|
: (await uploadFile(`canvas-download-${randomUUID()}.${asset.filename.split(".").pop() || "png"}`, Uint8Array.from(asset.bytes).buffer)).url;
|
||||||
await saveImageDownloadAsset(id, { status: "ready", url, bytes: asset.bytes.length,
|
await saveImageDownloadAsset(id, { status: "ready", url, bytes: asset.bytes.length,
|
||||||
contentType: asset.contentType, preparedAt: new Date().toISOString() });
|
contentType: asset.contentType, preparedAt: new Date().toISOString() });
|
||||||
await cacheImageBytes(`source::${url}`, { buffer: asset.bytes, contentType: asset.contentType });
|
await cacheImageBytes(`source::${url}`, { buffer: asset.bytes, contentType: asset.contentType });
|
||||||
} catch (error) {
|
} catch {
|
||||||
const attempts = Number((task.metadata as { downloadAsset?: { attempts?: number } } | null)?.downloadAsset?.attempts || 1);
|
const attempts = Number((task.metadata as { downloadAsset?: { attempts?: number } } | null)?.downloadAsset?.attempts || 1);
|
||||||
const failure = phase === "store"
|
await saveImageDownloadAsset(id, { status: attempts >= 12 ? "abandoned" : "failed", attempts, retryAt: new Date(Date.now() + 10 * 60_000).toISOString() });
|
||||||
? getDownloadFailure("STORAGE_WRITE_FAILED")
|
|
||||||
: phase === "convert" && getDownloadFailure(error).code === "DOWNLOAD_FAILED"
|
|
||||||
? getDownloadFailure("图片转换失败") : getDownloadFailure(error);
|
|
||||||
logger.warn("archive_download_preparation_failed", error, { requestId: id, code: failure.code });
|
|
||||||
await saveImageDownloadAsset(id, { status: !failure.retryable || attempts >= 12 ? "abandoned" : "failed", attempts,
|
|
||||||
failure, retryAt: new Date(Date.now() + 60_000).toISOString() });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export async function preparePendingImageDownloads() {
|
export async function preparePendingImageDownloads() {
|
||||||
@ -50,45 +36,3 @@ export async function preparePendingImageDownloads() {
|
|||||||
}));
|
}));
|
||||||
return tasks.length;
|
return tasks.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
const state = globalThis as typeof globalThis & {
|
|
||||||
__imageDownloadWorker?: { running: boolean; scanPending: boolean; priority: Set<string>; retry?: ReturnType<typeof setTimeout> };
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Download preparation runs independently of the archive URL replacement batch. */
|
|
||||||
export function scheduleImageDownloadPreparation(id?: string) {
|
|
||||||
const worker = state.__imageDownloadWorker ??= { running: false, scanPending: false, priority: new Set() };
|
|
||||||
if (id) worker.priority.add(id);
|
|
||||||
else worker.scanPending = true;
|
|
||||||
if (worker.running) return;
|
|
||||||
if (worker.retry) { clearTimeout(worker.retry); worker.retry = undefined; }
|
|
||||||
worker.running = true;
|
|
||||||
setImmediate(() => {
|
|
||||||
void trackRuntimeWork(async () => {
|
|
||||||
try {
|
|
||||||
for (;;) {
|
|
||||||
const priority = [...worker.priority].slice(0, 2);
|
|
||||||
for (const taskId of priority) worker.priority.delete(taskId);
|
|
||||||
if (priority.length) {
|
|
||||||
await Promise.all(priority.map(prepareImageArchiveDownload));
|
|
||||||
} else if (!worker.scanPending || await preparePendingImageDownloads() === 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
logger.error("download_worker_failed", error);
|
|
||||||
} finally {
|
|
||||||
worker.running = false;
|
|
||||||
// Eligible retry times live in the database, including interrupted leases.
|
|
||||||
if (worker.priority.size) {
|
|
||||||
scheduleImageDownloadPreparation([...worker.priority][0]);
|
|
||||||
} else if (worker.scanPending && await hasPendingImageDownloads().catch(() => true)) {
|
|
||||||
worker.retry = setTimeout(() => scheduleImageDownloadPreparation(), 60_000);
|
|
||||||
worker.retry.unref?.();
|
|
||||||
} else {
|
|
||||||
worker.scanPending = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { isRetiredStorageUrl, isLegacyAliyunStorageConfig, normalizeLegacyAliyunStorageUrl } from "./retired-storage";
|
import { isRetiredStorageUrl } from "./retired-storage";
|
||||||
|
|
||||||
import { and, asc, desc, eq, inArray, lte, sql } from "drizzle-orm";
|
import { and, asc, desc, eq, inArray, lte, sql } from "drizzle-orm";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
@ -19,7 +19,6 @@ function isUsableS3Config(config: StorageConfig | null | undefined): config is S
|
|||||||
config.isEnabled &&
|
config.isEnabled &&
|
||||||
!isRetiredStorageUrl(config.endpoint) &&
|
!isRetiredStorageUrl(config.endpoint) &&
|
||||||
!isRetiredStorageUrl(config.publicUrl) &&
|
!isRetiredStorageUrl(config.publicUrl) &&
|
||||||
!isLegacyAliyunStorageConfig(config) &&
|
|
||||||
String(config.endpoint || "").trim() &&
|
String(config.endpoint || "").trim() &&
|
||||||
String(config.bucket || "").trim() &&
|
String(config.bucket || "").trim() &&
|
||||||
String(config.accessKeyId || "").trim() &&
|
String(config.accessKeyId || "").trim() &&
|
||||||
@ -84,7 +83,7 @@ export async function enqueueStorageMigrationTask({
|
|||||||
migrateAfterAt: Date;
|
migrateAfterAt: Date;
|
||||||
cleanupAfterAt: Date;
|
cleanupAfterAt: Date;
|
||||||
}) {
|
}) {
|
||||||
if (!sourceUrl || !sourcePath || !sourceStorageConfigId || isRetiredStorageUrl(sourceUrl) || normalizeLegacyAliyunStorageUrl(sourceUrl)) return;
|
if (!sourceUrl || !sourcePath || !sourceStorageConfigId || isRetiredStorageUrl(sourceUrl)) return;
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.insert(storageMigrationTask)
|
.insert(storageMigrationTask)
|
||||||
|
|||||||
@ -1,45 +0,0 @@
|
|||||||
import "server-only";
|
|
||||||
import { getDefaultStorageConfig } from "@/lib/db/queries";
|
|
||||||
import { findImageArchiveDownloadTask } from "./image-archive-db";
|
|
||||||
import { inferOriginalImageUrl } from "@/lib/image-url";
|
|
||||||
import { getImageDownloadExtension } from "@/lib/image-delivery";
|
|
||||||
import { sanitizeCanvasDownloadFileName } from "@/lib/canvas/photoshop-safe-download";
|
|
||||||
|
|
||||||
/** Resolve the archived original without waiting for a separate PNG conversion. */
|
|
||||||
export async function resolveOriginalCdnDownload(source: string, name: string, signal: AbortSignal) {
|
|
||||||
const config = await getDefaultStorageConfig();
|
|
||||||
if (config?.type !== "qiniu-kodo" || !config.isEnabled || !config.publicUrl) return null;
|
|
||||||
const base = new URL(config.publicUrl);
|
|
||||||
const origin = process.env.CANVAS_QINIU_ORIGIN_URL;
|
|
||||||
let target = new URL(source);
|
|
||||||
if (target.origin !== base.origin && (!origin || target.origin !== new URL(origin).origin)) {
|
|
||||||
const task = await findImageArchiveDownloadTask([...new Set([source, inferOriginalImageUrl(source)])]);
|
|
||||||
target = new URL(task?.targetUrl || source);
|
|
||||||
}
|
|
||||||
if (origin && target.origin === new URL(origin).origin && !target.searchParams.has("token")) {
|
|
||||||
target.host = base.host;
|
|
||||||
target.protocol = base.protocol;
|
|
||||||
}
|
|
||||||
if (base.protocol !== "https:" || target.origin !== base.origin ||
|
|
||||||
!target.pathname.startsWith(base.pathname.replace(/\/$/, "") + "/")) return null;
|
|
||||||
// A Kodo download token signs the complete URL; adding attname would invalidate it.
|
|
||||||
if (target.searchParams.has("token")) return null;
|
|
||||||
|
|
||||||
// Only metadata crosses the application server. The browser receives the file
|
|
||||||
// from the CDN, including when cross-origin fetch is disabled by storage CORS.
|
|
||||||
const response = await fetch(target, {
|
|
||||||
method: "HEAD", cache: "no-store",
|
|
||||||
signal: AbortSignal.any([signal, AbortSignal.timeout(10_000)]),
|
|
||||||
});
|
|
||||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
||||||
const contentType = response.headers.get("content-type") || "";
|
|
||||||
if (!contentType.toLowerCase().startsWith("image/")) throw new Error("Source is not an image");
|
|
||||||
const extension = getImageDownloadExtension(contentType);
|
|
||||||
if (!extension) return null;
|
|
||||||
const basename = sanitizeCanvasDownloadFileName(name || target.pathname.split("/").pop() || "image", "image")
|
|
||||||
.replace(/\.(?:png|jpe?g|webp|avif|gif|svg|bmp|tiff?)$/i, "");
|
|
||||||
const filename = `${basename}.${extension}`;
|
|
||||||
target.searchParams.set("attname", filename);
|
|
||||||
return { delivery: "cdn" as const, downloadUrl: target.toString(), filename,
|
|
||||||
bytes: Number(response.headers.get("content-length")) || 0 };
|
|
||||||
}
|
|
||||||
@ -2,29 +2,6 @@
|
|||||||
export const RETIRED_STORAGE_URL_PATTERN = '^https?://s3[.]1080p[.]hk([/:?#]|$)';
|
export const RETIRED_STORAGE_URL_PATTERN = '^https?://s3[.]1080p[.]hk([/:?#]|$)';
|
||||||
export const RETIRED_STORAGE_MESSAGE = '旧存储已停用,源文件已无法恢复,请重新上传。';
|
export const RETIRED_STORAGE_MESSAGE = '旧存储已停用,源文件已无法恢复,请重新上传。';
|
||||||
|
|
||||||
export function normalizeLegacyAliyunStorageUrl(value: string | null | undefined): string | null {
|
|
||||||
if (!value) return null;
|
|
||||||
try {
|
|
||||||
let url = new URL(value, 'https://local.invalid');
|
|
||||||
if (url.pathname === '/api/image-proxy' || url.pathname === '/api/canvas/3d/model-proxy') {
|
|
||||||
url = new URL(url.searchParams.get('url') || '');
|
|
||||||
}
|
|
||||||
if (url.hostname !== 'demo-sparkai.oss-cn-shanghai.aliyuncs.com') return null;
|
|
||||||
url.protocol = 'https:';
|
|
||||||
url.hash = '';
|
|
||||||
for (const name of ['Signature', 'OSSAccessKeyId', 'Expires', 'security-token']) url.searchParams.delete(name);
|
|
||||||
url.searchParams.sort();
|
|
||||||
return url.toString();
|
|
||||||
} catch { return null; }
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isLegacyAliyunStorageConfig(config: { bucket?: string | null; endpoint?: string | null; publicUrl?: string | null }): boolean {
|
|
||||||
if (normalizeLegacyAliyunStorageUrl(config.publicUrl)) return true;
|
|
||||||
try {
|
|
||||||
return config.bucket === 'demo-sparkai' && new URL(config.endpoint || '').hostname.endsWith('.aliyuncs.com');
|
|
||||||
} catch { return false; }
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isRetiredStorageUrl(value: string | null | undefined): boolean {
|
export function isRetiredStorageUrl(value: string | null | undefined): boolean {
|
||||||
if (!value) return false;
|
if (!value) return false;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import "server-only";
|
import "server-only";
|
||||||
import { isRetiredStorageUrl, isLegacyAliyunStorageConfig, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
import { isRetiredStorageUrl, RETIRED_STORAGE_MESSAGE } from "./retired-storage";
|
||||||
|
|
||||||
import { DeleteObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
import { DeleteObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
||||||
import { put } from "@vercel/blob";
|
import { put } from "@vercel/blob";
|
||||||
@ -150,7 +150,7 @@ export function getContentType(filename: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function createS3Client(config: S3UploadConfig): S3Client {
|
export function createS3Client(config: S3UploadConfig): S3Client {
|
||||||
if (isRetiredStorageUrl(config.endpoint) || isRetiredStorageUrl(config.publicUrl) || isLegacyAliyunStorageConfig(config)) {
|
if (isRetiredStorageUrl(config.endpoint) || isRetiredStorageUrl(config.publicUrl)) {
|
||||||
throw new Error(RETIRED_STORAGE_MESSAGE);
|
throw new Error(RETIRED_STORAGE_MESSAGE);
|
||||||
}
|
}
|
||||||
const normalizedEndpoint = normalizeEndpoint(config.endpoint);
|
const normalizedEndpoint = normalizeEndpoint(config.endpoint);
|
||||||
@ -343,7 +343,6 @@ export function toDbS3UploadConfig(raw: unknown): S3UploadConfig | null {
|
|||||||
!value.isEnabled ||
|
!value.isEnabled ||
|
||||||
isRetiredStorageUrl(endpoint) ||
|
isRetiredStorageUrl(endpoint) ||
|
||||||
isRetiredStorageUrl(String(value.publicUrl || "")) ||
|
isRetiredStorageUrl(String(value.publicUrl || "")) ||
|
||||||
isLegacyAliyunStorageConfig({ bucket, endpoint, publicUrl: String(value.publicUrl || "") }) ||
|
|
||||||
!endpoint ||
|
!endpoint ||
|
||||||
!bucket ||
|
!bucket ||
|
||||||
!accessKeyId ||
|
!accessKeyId ||
|
||||||
@ -371,8 +370,7 @@ function buildEnvS3Config(): S3UploadConfig | null {
|
|||||||
const accessKeyId = process.env.S3_ACCESS_KEY_ID?.trim() ?? "";
|
const accessKeyId = process.env.S3_ACCESS_KEY_ID?.trim() ?? "";
|
||||||
const secretAccessKey = process.env.S3_SECRET_ACCESS_KEY?.trim() ?? "";
|
const secretAccessKey = process.env.S3_SECRET_ACCESS_KEY?.trim() ?? "";
|
||||||
|
|
||||||
if (endpoint && !isRetiredStorageUrl(endpoint) && !isRetiredStorageUrl(process.env.S3_PUBLIC_URL) &&
|
if (endpoint && !isRetiredStorageUrl(endpoint) && !isRetiredStorageUrl(process.env.S3_PUBLIC_URL) && bucket && accessKeyId && secretAccessKey) {
|
||||||
!isLegacyAliyunStorageConfig({ bucket, endpoint, publicUrl: process.env.S3_PUBLIC_URL }) && bucket && accessKeyId && secretAccessKey) {
|
|
||||||
return {
|
return {
|
||||||
endpoint,
|
endpoint,
|
||||||
region: process.env.S3_REGION?.trim() || "auto",
|
region: process.env.S3_REGION?.trim() || "auto",
|
||||||
|
|||||||
@ -107,5 +107,5 @@ export function mergeCanvasSnapshots(
|
|||||||
(e: any) => ids.has(e.source) && ids.has(e.target),
|
(e: any) => ids.has(e.source) && ids.has(e.target),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return { snapshot: JSON.stringify(snapshot), conflicts, changed: !same(snapshot, local) };
|
return { snapshot: JSON.stringify(snapshot), conflicts };
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,4 @@
|
|||||||
import type { NextConfig } from "next";
|
import type { NextConfig } from "next";
|
||||||
import { basename, dirname } from "node:path";
|
|
||||||
|
|
||||||
const appRelease = [basename(process.cwd()), basename(dirname(process.cwd()))]
|
|
||||||
.find(name => /^\d{8}T\d{6}Z-[a-f0-9]{12}$/.test(name)) || "development";
|
|
||||||
|
|
||||||
function resolvePascalEditorFrameSource() {
|
function resolvePascalEditorFrameSource() {
|
||||||
const rawUrl = (
|
const rawUrl = (
|
||||||
@ -49,7 +45,6 @@ function resolveDevMediaOrigins(isDev: boolean) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const nextConfig: NextConfig = {
|
const nextConfig: NextConfig = {
|
||||||
env: { NEXT_PUBLIC_APP_RELEASE: appRelease },
|
|
||||||
distDir: process.env.NEXT_DIST_DIR?.trim() || ".next",
|
distDir: process.env.NEXT_DIST_DIR?.trim() || ".next",
|
||||||
poweredByHeader: false,
|
poweredByHeader: false,
|
||||||
allowedDevOrigins: ["127.0.0.1"],
|
allowedDevOrigins: ["127.0.0.1"],
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { defineConfig } from "@playwright/test";
|
|||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
testDir: "./tests/routes",
|
testDir: "./tests/routes",
|
||||||
testMatch: /(?:canvas-agent-runtime(?:-store|-review|-video|-image-preferences|-image-failover|-progress)?|canvas-agent-project-tools|agent-capability-access|zcode-agent-task)\.test\.tsx?/,
|
testMatch: /(?:canvas-agent-runtime(?:-store|-review|-video|-image-preferences|-image-failover|-progress)?|canvas-agent-project-tools|agent-capability-access)\.test\.tsx?/,
|
||||||
fullyParallel: true,
|
fullyParallel: true,
|
||||||
retries: 0,
|
retries: 0,
|
||||||
workers: 2,
|
workers: 2,
|
||||||
|
|||||||
@ -3,15 +3,6 @@ import { defineConfig } from "@playwright/test";
|
|||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
testDir: "./tests",
|
testDir: "./tests",
|
||||||
testMatch: [
|
testMatch: [
|
||||||
/routes\/rmbg-delivery\.test\.ts/,
|
|
||||||
/routes\/legacy-oss-recovery\.test\.ts/,
|
|
||||||
/routes\/retired-storage\.test\.ts/,
|
|
||||||
/routes\/original-image-download\.test\.ts/,
|
|
||||||
/routes\/canvas-download-speed\.test\.ts/,
|
|
||||||
/routes\/canvas-download-recovery\.test\.ts/,
|
|
||||||
/routes\/canvas-download-rate-limit\.test\.ts/,
|
|
||||||
/routes\/canvas-zip-download\.test\.ts/,
|
|
||||||
/routes\/image-archive-flow\.test\.ts/,
|
|
||||||
/routes\/runtime-active-work\.test\.ts/,
|
/routes\/runtime-active-work\.test\.ts/,
|
||||||
/routes\/canvas-capability-task-recovery\.test\.ts/,
|
/routes\/canvas-capability-task-recovery\.test\.ts/,
|
||||||
/routes\/canvas-toolbar-capability\.test\.ts/,
|
/routes\/canvas-toolbar-capability\.test\.ts/,
|
||||||
@ -21,7 +12,6 @@ export default defineConfig({
|
|||||||
/routes\/video-canvas-media-import\.test\.ts/,
|
/routes\/video-canvas-media-import\.test\.ts/,
|
||||||
/routes\/video-canvas-toolbar-jobs\.test\.ts/,
|
/routes\/video-canvas-toolbar-jobs\.test\.ts/,
|
||||||
/routes\/image-provider-error-classification\.test\.ts/,
|
/routes\/image-provider-error-classification\.test\.ts/,
|
||||||
/routes\/image-generation-status\.test\.ts/,
|
|
||||||
/routes\/canvas-agent-runtime-image-failover\.test\.ts/,
|
/routes\/canvas-agent-runtime-image-failover\.test\.ts/,
|
||||||
/routes\/video-canvas-audio-remote\.test\.ts/,
|
/routes\/video-canvas-audio-remote\.test\.ts/,
|
||||||
/routes\/video-canvas-groups-upscale\.test\.ts/,
|
/routes\/video-canvas-groups-upscale\.test\.ts/,
|
||||||
@ -55,7 +45,6 @@ export default defineConfig({
|
|||||||
/routes\/replacement-product-identity\.test\.ts/,
|
/routes\/replacement-product-identity\.test\.ts/,
|
||||||
/routes\/detail-replacement-batch\.test\.ts/,
|
/routes\/detail-replacement-batch\.test\.ts/,
|
||||||
/routes\/alibaba-image-input\.test\.ts/,
|
/routes\/alibaba-image-input\.test\.ts/,
|
||||||
/routes\/alibaba-task-errors\.test\.ts/,
|
|
||||||
/routes\/model3d-style-pro-regressions\.test\.ts/,
|
/routes\/model3d-style-pro-regressions\.test\.ts/,
|
||||||
/routes\/clawd-request-lifecycle\.test\.ts/,
|
/routes\/clawd-request-lifecycle\.test\.ts/,
|
||||||
/routes\/canvas-skill-tag-order\.test\.ts/,
|
/routes\/canvas-skill-tag-order\.test\.ts/,
|
||||||
|
|||||||
5
proxy.ts
5
proxy.ts
@ -54,7 +54,6 @@ function isHighFrequencyReadApi(pathname: string, method: string) {
|
|||||||
if (/^\/api\/canvas\/[^/]+$/.test(pathname)) return true;
|
if (/^\/api\/canvas\/[^/]+$/.test(pathname)) return true;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
"/api/app-version",
|
|
||||||
"/api/ai-wash/config",
|
"/api/ai-wash/config",
|
||||||
"/api/models",
|
"/api/models",
|
||||||
"/api/canvas",
|
"/api/canvas",
|
||||||
@ -817,10 +816,6 @@ export async function proxy(request: NextRequest, event: NextFetchEvent) {
|
|||||||
return NextResponse.next();
|
return NextResponse.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pathname === "/api/app-version" && (request.method === "GET" || request.method === "HEAD")) {
|
|
||||||
return NextResponse.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pascal 服务到服务调用由对应 Route 再次验证共享密钥。
|
// Pascal 服务到服务调用由对应 Route 再次验证共享密钥。
|
||||||
if (isPascalInternalProxyRequest(request)) {
|
if (isPascalInternalProxyRequest(request)) {
|
||||||
return NextResponse.next();
|
return NextResponse.next();
|
||||||
|
|||||||
@ -1,17 +1,12 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
import { basename, dirname } from "node:path";
|
|
||||||
|
|
||||||
const DEFAULT_BASE_URL = `http://127.0.0.1:${process.env.PORT || "3001"}`;
|
const DEFAULT_BASE_URL = `http://127.0.0.1:${process.env.PORT || "3001"}`;
|
||||||
const BASE_URL = stripTrailingSlash(
|
const BASE_URL = stripTrailingSlash(
|
||||||
process.env.SMOKE_BASE_URL || process.argv[2] || DEFAULT_BASE_URL
|
process.env.SMOKE_BASE_URL || process.argv[2] || DEFAULT_BASE_URL
|
||||||
);
|
);
|
||||||
const REQUEST_TIMEOUT_MS = Number(process.env.SMOKE_TIMEOUT_MS || 20_000);
|
const REQUEST_TIMEOUT_MS = Number(process.env.SMOKE_TIMEOUT_MS || 20_000);
|
||||||
const EXPECTED_APP_RELEASE = process.env.SMOKE_EXPECTED_APP_RELEASE ||
|
|
||||||
[basename(process.cwd()), basename(dirname(process.cwd()))].find(name => /^\d{8}T\d{6}Z-[a-f0-9]{12}$/.test(name));
|
|
||||||
|
|
||||||
const checks = [
|
const checks = [
|
||||||
{ path: "/api/app-version", method: "GET", expect: { status: [200] } },
|
|
||||||
{
|
{
|
||||||
path: "/",
|
path: "/",
|
||||||
method: "HEAD",
|
method: "HEAD",
|
||||||
@ -79,12 +74,6 @@ async function runCheck(check) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (check.path === "/api/app-version") {
|
|
||||||
const data = await response.json();
|
|
||||||
if (!data.version || (EXPECTED_APP_RELEASE && data.version !== EXPECTED_APP_RELEASE) || !response.headers.get("cache-control")?.includes("no-store"))
|
|
||||||
return { ok: false, message: "deployed app version or cache policy does not match the release" };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
check.expect.location &&
|
check.expect.location &&
|
||||||
!matchesLocation(location, check.expect.location)
|
!matchesLocation(location, check.expect.location)
|
||||||
|
|||||||
@ -95,19 +95,18 @@ function normalizeRevisionText(value: unknown) {
|
|||||||
return String(value || "").trim();
|
return String(value || "").trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
function syncCanvasStoreRevisionFromUpdatedAt(projectId: string, updatedAt: unknown): void {
|
function syncCanvasStoreRevisionFromUpdatedAt(updatedAt: unknown): void {
|
||||||
if (typeof window === "undefined") return;
|
if (typeof window === "undefined") return;
|
||||||
const revision = normalizeRevisionText(updatedAt);
|
const revision = normalizeRevisionText(updatedAt);
|
||||||
if (!revision) return;
|
if (!revision) return;
|
||||||
const current = useCanvasStore.getState();
|
useCanvasStore.getState().setCanvasRevision(revision, revision);
|
||||||
if (current.projectId === projectId) current.setCanvasRevision(revision, revision);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function syncCanvasStoreRevisionFromProject(
|
function syncCanvasStoreRevisionFromProject(
|
||||||
project?: { id: string; canvasData?: CanvasState | null; updatedAt?: Date | string } | null
|
project?: { canvasData?: CanvasState | null; updatedAt?: Date | string } | null
|
||||||
): void {
|
): void {
|
||||||
if (!project?.canvasData) return;
|
if (!project?.canvasData) return;
|
||||||
syncCanvasStoreRevisionFromUpdatedAt(project.id, project.updatedAt);
|
syncCanvasStoreRevisionFromUpdatedAt(project.updatedAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NormalizedCropRect {
|
export interface NormalizedCropRect {
|
||||||
@ -204,9 +203,12 @@ export const canvasService = {
|
|||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/canvas/${id}`);
|
const response = await fetch(`/api/canvas/${id}`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return { ...data, status: response.status };
|
if (data?.success) {
|
||||||
|
syncCanvasStoreRevisionFromProject(data.data);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return { success: false, error: '画布读取失败,请检查网络后重试;现有内容未被覆盖', status: 0 };
|
return { success: false, error: '获取画布失败' };
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -333,7 +335,7 @@ export const canvasService = {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (data?.success) {
|
if (data?.success) {
|
||||||
syncCanvasStoreRevisionFromUpdatedAt(id, data?.data?.updatedAt);
|
syncCanvasStoreRevisionFromUpdatedAt(data?.data?.updatedAt);
|
||||||
}
|
}
|
||||||
return data as ApiResponse;
|
return data as ApiResponse;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@ -1,624 +0,0 @@
|
|||||||
import { expect, test, type Page } from "@playwright/test";
|
|
||||||
|
|
||||||
test.setTimeout(60_000);
|
|
||||||
const projectId = "8d3b4bac-4539-4caf-8b49-b063f218de19";
|
|
||||||
const imageOrigin = "https://scqnycdn.jiansun.vip";
|
|
||||||
const imageNode = (id: string) => ({ id, type: "image", position: { x: 160, y: 130 }, size: { width: 300, height: 200 },
|
|
||||||
data: { status: "completed", src: `${imageOrigin}/uploads/${id}.png`, title: id } });
|
|
||||||
|
|
||||||
async function setup(page: Page, failRead = false) {
|
|
||||||
const state = {
|
|
||||||
canvasData: { nodes: [imageNode("source")], edges: [], selectedNodeIds: [], viewport: { x: 0, y: 0, zoom: 1 }, tool: "select" },
|
|
||||||
revision: 1, failSave: false, failRead, conflictNextSave: false, conflictSameNode: false,
|
|
||||||
writes: [] as any[], metadataWrites: 0, submissions: [] as any[], capabilitySubmissions: [] as any[],
|
|
||||||
appVersion: "development", holdRequest: false, finishRequest: null as (() => void) | null,
|
|
||||||
nativeResultNext: false,
|
|
||||||
holdUpload: false, uploadStarted: 0, finishUpload: null as (() => void) | null,
|
|
||||||
};
|
|
||||||
const revision = () => new Date(Date.UTC(2026, 10, 6, 0, 0, state.revision)).toISOString();
|
|
||||||
const project = () => ({ id: projectId, title: "同步验证", canvasData: JSON.parse(JSON.stringify(state.canvasData, (_key, value) =>
|
|
||||||
value && typeof value === "object" && !Array.isArray(value)
|
|
||||||
? Object.fromEntries(Object.entries(value).sort(([a], [b]) => a.localeCompare(b)))
|
|
||||||
: value)), updatedAt: revision() });
|
|
||||||
const errors: string[] = [];
|
|
||||||
page.on("pageerror", error => errors.push(error.message));
|
|
||||||
await page.context().addInitScript(() => localStorage.setItem("theme", "light"));
|
|
||||||
await page.context().route("**/api/**", async route => {
|
|
||||||
const request = route.request();
|
|
||||||
const path = new URL(request.url()).pathname;
|
|
||||||
let data: unknown = {};
|
|
||||||
let status = 200;
|
|
||||||
if (path.startsWith("/api/files/") && request.method() === "POST" && state.holdUpload) {
|
|
||||||
state.uploadStarted++;
|
|
||||||
await new Promise<void>(resolve => { state.finishUpload = resolve; });
|
|
||||||
}
|
|
||||||
if (path === `/api/canvas/${projectId}`) {
|
|
||||||
if (request.method() === "GET") {
|
|
||||||
status = state.failRead ? 500 : 200;
|
|
||||||
data = state.failRead ? { success: false, error: "画布暂时无法读取,现有内容未被覆盖。" } : { success: true, data: project() };
|
|
||||||
} else if (request.method() === "PUT") {
|
|
||||||
const body = request.postDataJSON();
|
|
||||||
state.writes.push(body);
|
|
||||||
const previousCanvasRevision = revision();
|
|
||||||
if (body.canvasData) {
|
|
||||||
if (state.conflictNextSave) {
|
|
||||||
state.conflictNextSave = false;
|
|
||||||
if (state.conflictSameNode) state.canvasData.nodes[0].position.x = 500;
|
|
||||||
else state.canvasData.nodes.push(imageNode("remote-result"));
|
|
||||||
state.revision++;
|
|
||||||
}
|
|
||||||
if (state.failSave) {
|
|
||||||
status = 503; data = { success: false, error: "同步服务暂时不可用" };
|
|
||||||
} else if ((body.baseCanvasUpdatedAt || body.baseCanvasRevision) !== revision()) {
|
|
||||||
status = 409; data = { success: false, code: "STALE_CANVAS_REVISION", error: "画布已更新", canvasUpdatedAt: revision() };
|
|
||||||
} else {
|
|
||||||
state.canvasData = body.canvasData;
|
|
||||||
state.revision++;
|
|
||||||
data = { success: true, previousCanvasRevision, data: project() };
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state.metadataWrites++;
|
|
||||||
state.revision++;
|
|
||||||
data = { success: true, previousCanvasRevision, data: project() };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (path === `/api/canvas/${projectId}/capabilities` && request.method() === "POST") {
|
|
||||||
state.capabilitySubmissions.push(request.postDataJSON());
|
|
||||||
state.canvasData.nodes.push({ ...imageNode(`enhanced-${state.capabilitySubmissions.length}`), position: { x: 520, y: 130 } });
|
|
||||||
state.revision++;
|
|
||||||
data = { success: true, task: { status: "succeeded" } };
|
|
||||||
} else if (path === "/api/canvas/agent-chat" && request.method() === "POST") {
|
|
||||||
state.submissions.push(request.postDataJSON());
|
|
||||||
let operation = "";
|
|
||||||
if (state.nativeResultNext) {
|
|
||||||
state.nativeResultNext = false;
|
|
||||||
state.canvasData.nodes.push({ ...imageNode("agent-result"), position: { x: 520, y: 130 } });
|
|
||||||
state.revision++;
|
|
||||||
operation = `data: ${JSON.stringify({ type: "data-canvasOperation", data: { status: "applied", projectId, createdNodeIds: ["agent-result"] } })}\n\n`;
|
|
||||||
}
|
|
||||||
if (state.holdRequest) await new Promise<void>(resolve => { state.finishRequest = resolve; });
|
|
||||||
await route.fulfill({ headers: { "content-type": "text/event-stream", "x-vercel-ai-ui-message-stream": "v1" }, body:
|
|
||||||
'data: {"type":"start","messageId":"sync-assistant"}\n\n' + operation + 'data: {"type":"text-start","id":"text"}\n\ndata: {"type":"text-delta","id":"text","delta":"已收到请求"}\n\ndata: {"type":"text-end","id":"text"}\n\ndata: {"type":"finish"}\n\ndata: [DONE]\n\n' });
|
|
||||||
return;
|
|
||||||
} else if (path === "/api/app-version") data = { version: state.appVersion };
|
|
||||||
else if (path === "/api/files/direct-upload") {
|
|
||||||
data = request.postDataJSON().action === "complete"
|
|
||||||
? { url: `${imageOrigin}/uploads/uploaded.png`, pathname: "uploads/uploaded.png" }
|
|
||||||
: { mode: "completed", key: "uploads/uploaded.png" };
|
|
||||||
} else if (path === "/api/files/upload") data = { url: `${imageOrigin}/uploads/uploaded.png` };
|
|
||||||
else if (path === "/api/canvas/auto-title") data = { title: "参考图片" };
|
|
||||||
else if (path.includes("validate-session")) data = { valid: true, success: true };
|
|
||||||
else if (path === "/api/auth/session") data = { user: { id: "sync-user", name: "同步验证", type: "admin" }, expires: "2099-01-01" };
|
|
||||||
else if (path === "/api/site-settings") data = { canvasEnabled: true, agentEnabled: true, membershipEnabled: false };
|
|
||||||
else if (path === "/api/models") data = { models: [{ id: "image-test", modelId: "gpt-image-2", providerType: "openai", name: "GPT Image", type: "image", enabled: true, supportsImageGen: true }] };
|
|
||||||
else if (path === "/api/vote") data = [];
|
|
||||||
else if (path.includes("messages")) data = { messages: [] };
|
|
||||||
else if (path.includes("sessions")) data = { sessions: [] };
|
|
||||||
else if (path.startsWith("/api/canvas/")) data = { success: true, runs: [] };
|
|
||||||
await route.fulfill({ status, contentType: "application/json", body: JSON.stringify(data) });
|
|
||||||
});
|
|
||||||
await page.context().route(`${imageOrigin}/**`, route => route.fulfill({ contentType: "image/svg+xml",
|
|
||||||
body: '<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200"><rect width="300" height="200" fill="#43b9bd"/></svg>' }));
|
|
||||||
await page.goto(`/canvas/${projectId}`, { waitUntil: "domcontentloaded" });
|
|
||||||
if (!failRead) await expect(page.getByRole("heading", { name: /Hi, 同步验证/ })).toBeVisible();
|
|
||||||
return { state, errors };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function moveImage(page: Page) {
|
|
||||||
const node = page.locator('[data-canvas-node-id="source"]');
|
|
||||||
await expect(node).toBeVisible();
|
|
||||||
const box = (await node.boundingBox())!;
|
|
||||||
await page.mouse.move(box.x + 100, box.y + 80);
|
|
||||||
await page.mouse.down();
|
|
||||||
await page.mouse.move(box.x + 190, box.y + 120, { steps: 5 });
|
|
||||||
await page.mouse.up();
|
|
||||||
}
|
|
||||||
|
|
||||||
const unloadWouldBeBlocked = (page: Page) => page.evaluate(() => {
|
|
||||||
const event = new Event("beforeunload", { cancelable: true });
|
|
||||||
Object.defineProperty(event, "returnValue", { value: "", writable: true });
|
|
||||||
window.dispatchEvent(event);
|
|
||||||
return event.defaultPrevented && Boolean(event.returnValue);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("background saving recovers when the network returns without a manual save", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await expect(page.getByRole("button", { name: "保存", exact: true })).toHaveCount(0);
|
|
||||||
state.failSave = true;
|
|
||||||
await moveImage(page);
|
|
||||||
await expect.poll(() => state.writes.filter(body => body.canvasData).length).toBeGreaterThan(0);
|
|
||||||
expect(state.canvasData.nodes[0].position.x).toBe(160);
|
|
||||||
state.failSave = false;
|
|
||||||
await page.evaluate(() => window.dispatchEvent(new Event("online")));
|
|
||||||
await expect.poll(() => state.canvasData.nodes[0].position.x).toBeGreaterThan(160);
|
|
||||||
await expect.poll(() => unloadWouldBeBlocked(page)).toBe(false);
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("selecting a saved image does not require another save before an Agent message", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await page.locator('[data-canvas-node-id="source"]').click();
|
|
||||||
state.failSave = true;
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await editor.fill("分析这张已经保存的图片");
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect.poll(() => state.submissions.length).toBe(1);
|
|
||||||
expect(state.canvasData.nodes.map(node => node.id)).toEqual(["source"]);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("only unsaved content blocks closing and beforeunload does not issue duplicate saves", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
expect(await unloadWouldBeBlocked(page)).toBe(false);
|
|
||||||
await page.locator('[data-canvas-node-id="source"]').click();
|
|
||||||
expect(await unloadWouldBeBlocked(page)).toBe(false);
|
|
||||||
state.failSave = true;
|
|
||||||
await moveImage(page);
|
|
||||||
const writes = state.writes.length;
|
|
||||||
expect(await unloadWouldBeBlocked(page)).toBe(true);
|
|
||||||
expect(state.writes).toHaveLength(writes);
|
|
||||||
const draft = await page.evaluate(id => localStorage.getItem(`canvas-unsynced:${id}`), projectId);
|
|
||||||
expect(JSON.parse(JSON.parse(draft!).snapshot).canvasData.nodes[0].position.x).toBeGreaterThan(160);
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("pagehide and visibilitychange issue only one closing save", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
state.failSave = true;
|
|
||||||
await moveImage(page);
|
|
||||||
const writes = state.writes.length;
|
|
||||||
await page.evaluate(() => {
|
|
||||||
window.dispatchEvent(new Event("pagehide"));
|
|
||||||
Object.defineProperty(document, "visibilityState", { value: "hidden", configurable: true });
|
|
||||||
document.dispatchEvent(new Event("visibilitychange"));
|
|
||||||
});
|
|
||||||
await expect.poll(() => state.writes.length).toBe(writes + 1);
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("leaving with pending changes can continue editing or save before navigating", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await page.route(url => url.pathname === "/", route => route.fulfill({ contentType: "text/html; charset=utf-8", body: "<p>已返回首页</p>" }));
|
|
||||||
await moveImage(page);
|
|
||||||
await page.getByRole("button", { name: "返回首页", exact: true }).click();
|
|
||||||
const dialog = page.getByRole("dialog", { name: "离开前保存画布?" });
|
|
||||||
await expect(dialog).toBeVisible();
|
|
||||||
await page.screenshot({ path: test.info().outputPath("leave-dialog-desktop.png"), animations: "disabled" });
|
|
||||||
await page.setViewportSize({ width: 390, height: 844 });
|
|
||||||
await page.screenshot({ path: test.info().outputPath("leave-dialog-mobile.png"), animations: "disabled" });
|
|
||||||
await page.setViewportSize({ width: 1440, height: 900 });
|
|
||||||
await dialog.getByRole("button", { name: "继续编辑", exact: true }).click();
|
|
||||||
await expect(dialog).toHaveCount(0);
|
|
||||||
await moveImage(page);
|
|
||||||
await page.getByRole("button", { name: "返回首页", exact: true }).click();
|
|
||||||
await dialog.getByRole("button", { name: "保存并离开", exact: true }).click();
|
|
||||||
await expect(page.getByText("已返回首页")).toBeVisible();
|
|
||||||
expect(state.canvasData.nodes[0].position.x).toBeGreaterThan(160);
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("failed saving keeps the leave dialog and canvas until a successful retry", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await page.route(url => url.pathname === "/", route => route.fulfill({ contentType: "text/html; charset=utf-8", body: "<p>已返回首页</p>" }));
|
|
||||||
state.failSave = true;
|
|
||||||
await moveImage(page);
|
|
||||||
await page.getByRole("button", { name: "返回首页", exact: true }).click();
|
|
||||||
const dialog = page.getByRole("dialog", { name: "离开前保存画布?" });
|
|
||||||
await dialog.getByRole("button", { name: "保存并离开", exact: true }).click();
|
|
||||||
await expect(dialog.getByRole("alert")).toContainText("HTTP 503");
|
|
||||||
await expect(page.locator('[data-canvas-node-id="source"]')).toBeVisible();
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
state.failSave = false;
|
|
||||||
await dialog.getByRole("button", { name: "保存并离开", exact: true }).click();
|
|
||||||
await expect(page.getByText("已返回首页")).toBeVisible();
|
|
||||||
expect(state.canvasData.nodes[0].position.x).toBeGreaterThan(160);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("discarding pending changes leaves without saving or restoring the discarded draft", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await page.route(url => url.pathname === "/", route => route.fulfill({ contentType: "text/html; charset=utf-8", body: "<p>已返回首页</p>" }));
|
|
||||||
state.failSave = true;
|
|
||||||
await moveImage(page);
|
|
||||||
expect(await unloadWouldBeBlocked(page)).toBe(true);
|
|
||||||
const writes = state.writes.length;
|
|
||||||
await page.getByRole("button", { name: "返回首页", exact: true }).click();
|
|
||||||
await page.getByRole("button", { name: "不保存并离开", exact: true }).click();
|
|
||||||
await expect(page.getByText("已返回首页")).toBeVisible();
|
|
||||||
expect(state.writes).toHaveLength(writes);
|
|
||||||
expect(state.canvasData.nodes[0].position.x).toBe(160);
|
|
||||||
expect(await page.evaluate(id => localStorage.getItem(`canvas-unsynced:${id}`), projectId)).toBeNull();
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("creating another project also offers to preserve pending canvas changes", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
state.failSave = true;
|
|
||||||
await moveImage(page);
|
|
||||||
await page.getByRole("button", { name: "查看项目文件", exact: true }).click();
|
|
||||||
await page.getByRole("button", { name: "新建画布", exact: true }).first().click();
|
|
||||||
await expect(page.getByRole("dialog", { name: "离开前保存画布?" })).toBeVisible();
|
|
||||||
await page.getByRole("button", { name: "继续编辑", exact: true }).click();
|
|
||||||
await expect(page).toHaveURL(new RegExp(`/canvas/${projectId}$`));
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("an enhanced result can be moved and archived before the next save without blocking Agent", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await page.locator('[data-canvas-node-id="source"]').click();
|
|
||||||
await page.locator('[data-tool-key="enhance"]').click();
|
|
||||||
const result = page.locator('[data-canvas-node-id="enhanced-1"]');
|
|
||||||
await expect(result).toBeVisible();
|
|
||||||
const box = (await result.boundingBox())!;
|
|
||||||
await page.mouse.move(box.x + 100, box.y + 80);
|
|
||||||
await page.mouse.down();
|
|
||||||
await page.mouse.move(box.x + 165, box.y + 110, { steps: 5 });
|
|
||||||
await page.mouse.up();
|
|
||||||
state.canvasData.nodes.find(node => node.id === "enhanced-1")!.data.src = `${imageOrigin}/uploads/archived-result.png`;
|
|
||||||
state.revision++;
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await editor.fill("保留增强结果的位置,继续分析");
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect.poll(() => state.submissions.length).toBe(1);
|
|
||||||
const saved = state.canvasData.nodes.find(node => node.id === "enhanced-1")!;
|
|
||||||
expect(saved.position.x).toBeGreaterThan(520);
|
|
||||||
expect(saved.data.src).toContain("archived-result.png");
|
|
||||||
expect(state.capabilitySubmissions).toHaveLength(1);
|
|
||||||
await page.reload();
|
|
||||||
await expect(page.locator('[data-canvas-node-id="enhanced-1"]')).toBeVisible();
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("toolbar save failures show the cause and recover without a paid submission", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await moveImage(page);
|
|
||||||
state.failSave = true;
|
|
||||||
await page.locator('[data-tool-key="enhance"]').click();
|
|
||||||
await expect(page.getByText(/画布自动同步失败(HTTP 503)/)).toBeVisible();
|
|
||||||
await expect(page.getByText(/本次处理未提交/)).toBeVisible();
|
|
||||||
expect(state.capabilitySubmissions).toHaveLength(0);
|
|
||||||
state.failSave = false;
|
|
||||||
await page.locator('[data-tool-key="enhance"]').click();
|
|
||||||
await expect(page.locator('[data-canvas-node-id="enhanced-1"]')).toBeVisible();
|
|
||||||
expect(state.capabilitySubmissions).toHaveLength(1);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("native Agent results update the save baseline before moving and archiving the result", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
state.nativeResultNext = true;
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await editor.fill("先分析再处理图片");
|
|
||||||
await editor.press("Enter");
|
|
||||||
const result = page.locator('[data-canvas-node-id="agent-result"]');
|
|
||||||
await expect(result).toBeVisible();
|
|
||||||
await expect(editor).toBeEditable();
|
|
||||||
const box = (await result.boundingBox())!;
|
|
||||||
await page.mouse.move(box.x + 100, box.y + 80);
|
|
||||||
await page.mouse.down();
|
|
||||||
await page.mouse.move(box.x + 165, box.y + 110, { steps: 5 });
|
|
||||||
await page.mouse.up();
|
|
||||||
state.canvasData.nodes.find(node => node.id === "agent-result")!.data.src = `${imageOrigin}/uploads/agent-archived.png`;
|
|
||||||
state.revision++;
|
|
||||||
await editor.fill("保留位置,继续分析");
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect.poll(() => state.submissions.length).toBe(2);
|
|
||||||
const saved = state.canvasData.nodes.find(node => node.id === "agent-result")!;
|
|
||||||
expect(saved.position.x).toBeGreaterThan(520);
|
|
||||||
expect(saved.data.src).toContain("agent-archived.png");
|
|
||||||
expect(state.capabilitySubmissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("upload naming and consecutive Agent messages sync automatically without manual saves", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await expect(editor).toBeVisible();
|
|
||||||
await editor.evaluate(element => {
|
|
||||||
const transfer = new DataTransfer();
|
|
||||||
transfer.items.add(new File([new Uint8Array([137, 80, 78, 71])], "参考图.png", { type: "image/png" }));
|
|
||||||
element.dispatchEvent(new ClipboardEvent("paste", { clipboardData: transfer, bubbles: true, cancelable: true }));
|
|
||||||
});
|
|
||||||
await expect.poll(() => state.metadataWrites).toBe(1);
|
|
||||||
expect(state.canvasData.nodes).toHaveLength(2);
|
|
||||||
for (const prompt of ["请先分析参考图", "继续调整为白色背景"]) {
|
|
||||||
await editor.fill(prompt);
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect.poll(() => state.submissions.length).toBe(prompt.startsWith("请") ? 1 : 2);
|
|
||||||
await expect(editor).toBeEditable();
|
|
||||||
}
|
|
||||||
await page.reload();
|
|
||||||
await expect(page.locator('[data-canvas-node-id]')).toHaveCount(2);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a save conflict merges a remote result with local movement before one Agent submission", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await moveImage(page);
|
|
||||||
state.conflictNextSave = true;
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await editor.fill("保留我移动后的位置,再分析图片");
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect.poll(() => state.submissions.length).toBe(1);
|
|
||||||
expect(state.canvasData.nodes.map(node => node.id)).toEqual(["source", "remote-result"]);
|
|
||||||
expect(state.canvasData.nodes[0].position.x).toBeGreaterThan(160);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("failed synchronization preserves the unsent message and canvas draft across refresh", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await moveImage(page);
|
|
||||||
state.failSave = true;
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
const prompt = "保持产品结构,只更换背景";
|
|
||||||
await editor.fill(prompt);
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect(page.getByText(/本地草稿已保留/).first()).toBeVisible();
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
await expect(editor).toHaveText(prompt);
|
|
||||||
await page.reload();
|
|
||||||
await expect(editor).toHaveText(prompt);
|
|
||||||
await expect(page.locator('[data-canvas-node-id="source"]')).toBeVisible();
|
|
||||||
await expect(page.getByRole("heading", { name: /Hi, 同步验证/ })).toBeVisible();
|
|
||||||
state.failSave = false;
|
|
||||||
await expect(page.getByRole("button", { name: "发送", exact: true })).toBeEnabled();
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect.poll(() => state.submissions.length).toBe(1);
|
|
||||||
expect(state.canvasData.nodes[0].position.x).toBeGreaterThan(160);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("conflicting edits keep both canvases and do not submit an Agent task", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await moveImage(page);
|
|
||||||
state.conflictSameNode = true;
|
|
||||||
state.conflictNextSave = true;
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await editor.fill("请处理这张图片");
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect(page.getByText(/画布同步发生冲突/).first()).toBeVisible();
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(state.canvasData.nodes[0].position.x).toBe(500);
|
|
||||||
const draft = await page.evaluate(id => JSON.parse(localStorage.getItem(`canvas-unsynced:${id}`) || "null"), projectId);
|
|
||||||
expect(JSON.parse(draft.snapshot).canvasData.nodes[0].position.x).toBeGreaterThan(160);
|
|
||||||
expect(JSON.parse(draft.snapshot).canvasData.nodes[0].position.x).not.toBe(500);
|
|
||||||
await expect(editor).toHaveText("请处理这张图片");
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const choice of ["local", "remote"] as const) {
|
|
||||||
test(`an existing conflicting draft can keep the ${choice} edit and resume without losing other images`, async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await moveImage(page);
|
|
||||||
state.conflictSameNode = true;
|
|
||||||
state.conflictNextSave = true;
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await editor.fill("继续分析图片");
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect(page.getByRole("button", { name: "保留本页修改", exact: true })).toBeVisible();
|
|
||||||
state.canvasData.nodes.push(imageNode("remote-addition"));
|
|
||||||
state.revision++;
|
|
||||||
await page.reload();
|
|
||||||
await expect(page.locator('[data-canvas-node-id="remote-addition"]')).toBeVisible();
|
|
||||||
if (choice === "local") {
|
|
||||||
await page.screenshot({ path: test.info().outputPath("conflict-recovery-desktop.png"), animations: "disabled" });
|
|
||||||
await page.setViewportSize({ width: 390, height: 844 });
|
|
||||||
await page.screenshot({ path: test.info().outputPath("conflict-recovery-mobile.png"), animations: "disabled" });
|
|
||||||
await page.setViewportSize({ width: 1440, height: 900 });
|
|
||||||
}
|
|
||||||
await page.getByRole("button", { name: choice === "local" ? "保留本页修改" : "保留云端修改", exact: true }).click();
|
|
||||||
await expect(page.getByRole("button", { name: "保留本页修改", exact: true })).toHaveCount(0);
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect.poll(() => state.submissions.length).toBe(1);
|
|
||||||
const x = state.canvasData.nodes.find(node => node.id === "source")!.position.x;
|
|
||||||
if (choice === "remote") expect(x).toBe(500);
|
|
||||||
else { expect(x).toBeGreaterThan(160); expect(x).not.toBe(500); }
|
|
||||||
expect(state.canvasData.nodes.map(node => node.id)).toEqual(["source", "remote-addition"]);
|
|
||||||
expect(state.capabilitySubmissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
test("a failed canvas read cannot create or save an empty replacement", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page, true);
|
|
||||||
await expect(page.getByRole("alert").filter({ hasText: "画布暂时无法读取" })).toBeVisible();
|
|
||||||
await page.reload();
|
|
||||||
await expect(page.getByRole("alert").filter({ hasText: "画布暂时无法读取" })).toBeVisible();
|
|
||||||
expect(state.writes).toHaveLength(0);
|
|
||||||
state.failRead = false;
|
|
||||||
await page.getByRole("button", { name: "重新加载画布" }).click();
|
|
||||||
await expect(page.locator('[data-canvas-node-id="source"]')).toBeVisible();
|
|
||||||
expect(state.canvasData.nodes).toHaveLength(1);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
async function showUpdate(page: Page, state: { appVersion: string }) {
|
|
||||||
state.appVersion = "20261106T120000Z-nextrelease1";
|
|
||||||
await page.evaluate(() => window.dispatchEvent(new Event("focus")));
|
|
||||||
await expect(page.getByRole("alertdialog")).toContainText("平台已更新");
|
|
||||||
}
|
|
||||||
|
|
||||||
async function countRefreshes(page: Page, state: { appVersion: string }) {
|
|
||||||
let navigations = 0;
|
|
||||||
await page.route(url => url.pathname === `/canvas/${projectId}`, route => {
|
|
||||||
if (route.request().isNavigationRequest()) { navigations++; state.appVersion = "development"; }
|
|
||||||
return route.continue();
|
|
||||||
});
|
|
||||||
return () => navigations;
|
|
||||||
}
|
|
||||||
|
|
||||||
test("unfinished image uploads remain on the page even after their canvas placeholder was saved", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
const refreshes = await countRefreshes(page, state);
|
|
||||||
state.holdUpload = true;
|
|
||||||
await page.mouse.click(100, 90);
|
|
||||||
await page.evaluate(() => {
|
|
||||||
const bytes = Uint8Array.from(atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+j2d0AAAAASUVORK5CYII="), c => c.charCodeAt(0));
|
|
||||||
const transfer = new DataTransfer();
|
|
||||||
transfer.items.add(new File([bytes], "上传保护.png", { type: "image/png" }));
|
|
||||||
window.dispatchEvent(new ClipboardEvent("paste", { clipboardData: transfer, bubbles: true, cancelable: true }));
|
|
||||||
});
|
|
||||||
await expect.poll(() => state.uploadStarted).toBeGreaterThan(0);
|
|
||||||
await expect.poll(() => state.canvasData.nodes.filter(node => node.data.src === "").length).toBe(1);
|
|
||||||
expect(await unloadWouldBeBlocked(page)).toBe(true);
|
|
||||||
await page.getByRole("button", { name: "返回首页", exact: true }).click();
|
|
||||||
const dialog = page.getByRole("dialog", { name: "离开前保存画布?" });
|
|
||||||
await dialog.getByRole("button", { name: "保存并离开", exact: true }).click();
|
|
||||||
await expect(dialog.getByRole("alert")).toContainText("还有图片未上传成功");
|
|
||||||
await dialog.getByRole("button", { name: "继续编辑", exact: true }).click();
|
|
||||||
await showUpdate(page, state);
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect(page.getByRole("alertdialog")).toContainText("还有图片未上传成功");
|
|
||||||
expect(refreshes()).toBe(0);
|
|
||||||
state.holdUpload = false;
|
|
||||||
state.finishUpload?.();
|
|
||||||
await expect.poll(() => state.canvasData.nodes.some(node => node.data.src.includes("uploaded.png"))).toBe(true);
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect.poll(refreshes).toBe(1);
|
|
||||||
expect(state.canvasData.nodes).toHaveLength(2);
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a composer upload blocks release refresh and closing until the image and input are retained", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
const refreshes = await countRefreshes(page, state);
|
|
||||||
state.holdUpload = true;
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await editor.fill("上传后保留这条输入");
|
|
||||||
await editor.evaluate(element => {
|
|
||||||
const transfer = new DataTransfer();
|
|
||||||
transfer.items.add(new File([new Uint8Array([137, 80, 78, 71])], "聊天上传保护.png", { type: "image/png" }));
|
|
||||||
element.dispatchEvent(new ClipboardEvent("paste", { clipboardData: transfer, bubbles: true, cancelable: true }));
|
|
||||||
});
|
|
||||||
await expect.poll(() => state.uploadStarted).toBeGreaterThan(0);
|
|
||||||
await expect(page.getByText("聊天上传保护.png", { exact: true })).toBeVisible();
|
|
||||||
expect(await unloadWouldBeBlocked(page)).toBe(true);
|
|
||||||
await showUpdate(page, state);
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect(page.getByRole("alertdialog")).toContainText("素材仍在上传");
|
|
||||||
expect(refreshes()).toBe(0);
|
|
||||||
state.holdUpload = false;
|
|
||||||
state.finishUpload?.();
|
|
||||||
await expect.poll(() => state.canvasData.nodes.some(node => node.data.src.includes("uploaded.png"))).toBe(true);
|
|
||||||
await expect(page.getByText("聊天上传保护.png", { exact: true })).toHaveCount(1);
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect.poll(refreshes).toBe(1);
|
|
||||||
await expect(editor).toContainText("上传后保留这条输入");
|
|
||||||
await expect(editor.getByText("聊天上传保护.png", { exact: true })).toBeVisible();
|
|
||||||
expect(state.canvasData.nodes).toHaveLength(2);
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("an older browser without AbortSignal static helpers receives the release notice", async ({ page }) => {
|
|
||||||
await page.context().addInitScript(() => {
|
|
||||||
Object.defineProperty(AbortSignal, "any", { value: undefined, configurable: true });
|
|
||||||
Object.defineProperty(AbortSignal, "timeout", { value: undefined, configurable: true });
|
|
||||||
});
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
await showUpdate(page, state);
|
|
||||||
await expect(page.getByRole("button", { name: "同步并刷新", exact: true })).toBeVisible();
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a release is detected by the minute timer without requiring a focus change", async ({ page }) => {
|
|
||||||
await page.clock.install();
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
state.appVersion = "20261106T120000Z-nextrelease1";
|
|
||||||
await page.clock.fastForward(60_001);
|
|
||||||
await expect(page.getByRole("alertdialog")).toContainText("平台已更新");
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a new release requires confirmation and refreshes after preserving canvas and input", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
const refreshes = await countRefreshes(page, state);
|
|
||||||
await moveImage(page);
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await editor.fill("刷新后还要保留这条输入");
|
|
||||||
await showUpdate(page, state);
|
|
||||||
await page.keyboard.press("Escape");
|
|
||||||
await expect(page.getByRole("alertdialog")).toBeVisible();
|
|
||||||
expect(refreshes()).toBe(0);
|
|
||||||
await page.screenshot({ path: test.info().outputPath("update-notice-desktop.png"), animations: "disabled" });
|
|
||||||
await page.setViewportSize({ width: 390, height: 844 });
|
|
||||||
await page.screenshot({ path: test.info().outputPath("update-notice-mobile.png"), animations: "disabled" });
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect.poll(refreshes).toBe(1);
|
|
||||||
await expect(editor).toHaveText("刷新后还要保留这条输入");
|
|
||||||
await expect(page.getByRole("alertdialog")).toHaveCount(0);
|
|
||||||
expect(state.canvasData.nodes[0].position.x).toBeGreaterThan(160);
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("an update cannot interrupt or resubmit a running Agent task", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
const refreshes = await countRefreshes(page, state);
|
|
||||||
state.holdRequest = true;
|
|
||||||
const editor = page.getByLabel("描述你的设计需求", { exact: true });
|
|
||||||
await editor.fill("先分析这张图片");
|
|
||||||
await editor.press("Enter");
|
|
||||||
await expect.poll(() => state.submissions.length).toBe(1);
|
|
||||||
await showUpdate(page, state);
|
|
||||||
const saves = state.writes.length;
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect(page.getByRole("alertdialog")).toContainText("当前对话任务仍在处理中");
|
|
||||||
expect(refreshes()).toBe(0);
|
|
||||||
expect(state.writes).toHaveLength(saves);
|
|
||||||
state.finishRequest?.();
|
|
||||||
await expect(editor).toBeEditable();
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect.poll(refreshes).toBe(1);
|
|
||||||
expect(state.submissions).toHaveLength(1);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("an update keeps the current page when automatic synchronization fails", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page);
|
|
||||||
const refreshes = await countRefreshes(page, state);
|
|
||||||
state.failSave = true;
|
|
||||||
await moveImage(page);
|
|
||||||
await showUpdate(page, state);
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect(page.getByRole("alertdialog")).toContainText("HTTP 503");
|
|
||||||
await expect(page.getByRole("alertdialog")).not.toContainText("本次任务未提交");
|
|
||||||
await expect(page.getByRole("alertdialog")).not.toContainText("再次发送");
|
|
||||||
expect(refreshes()).toBe(0);
|
|
||||||
expect(state.submissions).toHaveLength(0);
|
|
||||||
await page.screenshot({ path: test.info().outputPath("update-error-desktop.png"), animations: "disabled" });
|
|
||||||
await page.setViewportSize({ width: 390, height: 844 });
|
|
||||||
await page.screenshot({ path: test.info().outputPath("update-error-mobile.png"), animations: "disabled" });
|
|
||||||
await page.setViewportSize({ width: 1440, height: 900 });
|
|
||||||
await page.getByRole("button", { name: "返回处理", exact: true }).click();
|
|
||||||
await expect(page.getByRole("alertdialog")).toHaveCount(0);
|
|
||||||
await expect(page.locator('[data-canvas-node-id="source"]')).toBeVisible();
|
|
||||||
state.failSave = false;
|
|
||||||
await showUpdate(page, state);
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect.poll(refreshes).toBe(1);
|
|
||||||
expect(state.canvasData.nodes[0].position.x).toBeGreaterThan(160);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a release refresh never saves an empty canvas after its initial read failed", async ({ page }) => {
|
|
||||||
const { state, errors } = await setup(page, true);
|
|
||||||
const refreshes = await countRefreshes(page, state);
|
|
||||||
await expect(page.getByRole("alert").filter({ hasText: "画布暂时无法读取" })).toBeVisible();
|
|
||||||
await showUpdate(page, state);
|
|
||||||
state.failRead = false;
|
|
||||||
await page.getByRole("button", { name: "同步并刷新", exact: true }).click();
|
|
||||||
await expect.poll(refreshes).toBe(1);
|
|
||||||
await expect(page.locator('[data-canvas-node-id="source"]')).toBeVisible();
|
|
||||||
expect(state.canvasData.nodes).toHaveLength(1);
|
|
||||||
expect(state.writes.filter(body => body.canvasData?.nodes.length === 0)).toHaveLength(0);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
@ -1,198 +0,0 @@
|
|||||||
import { expect, test, type Page } from "@playwright/test";
|
|
||||||
|
|
||||||
const projectId="f1827b8f-b810-44d8-9927-c2ef255eed78";
|
|
||||||
const paragraph="保持产品结构与材质一致,先分析参考图片,再安排场景与光线,逐项确认处理要求。";
|
|
||||||
const makeMessage=(id:string,role:string,text:string,offset:number)=>({id,role,parts:[{type:"text",text}],createdAt:new Date(Date.UTC(2026,10,6,0,0,offset)).toISOString()});
|
|
||||||
|
|
||||||
async function setup(page:Page) {
|
|
||||||
const state={history:Array.from({length:16},(_,i)=>makeMessage(`history-${i}`,i%2?"assistant":"user",`历史记录 ${i+1}\n\n${new Array(4).fill(paragraph).join("\n\n")}`,i)),historyReads:0,run:null as any};
|
|
||||||
const errors:string[]=[];page.on("pageerror",error=>errors.push(error.message));
|
|
||||||
await page.context().addInitScript(()=>{
|
|
||||||
localStorage.setItem("theme","light");
|
|
||||||
const originalFetch=window.fetch.bind(window);
|
|
||||||
(window as any).agentScrollStream={};
|
|
||||||
window.fetch=async(input,init)=>{
|
|
||||||
const url=typeof input==="string"?input:input instanceof URL?input.href:input.url;
|
|
||||||
if(new URL(url,location.origin).pathname!=="/api/canvas/agent-chat"||init?.method!=="POST")return originalFetch(input,init);
|
|
||||||
const request=JSON.parse(String(init.body));
|
|
||||||
const stream=(window as any).agentScrollStream;
|
|
||||||
stream.request=request;stream.text="";
|
|
||||||
const encoder=new TextEncoder();
|
|
||||||
return new Response(new ReadableStream({start(controller){
|
|
||||||
const emit=(value:unknown)=>controller.enqueue(encoder.encode(`data: ${JSON.stringify(value)}\n\n`));
|
|
||||||
emit({type:"start",messageId:"stream-assistant"});emit({type:"text-start",id:"text"});
|
|
||||||
stream.append=(text:string)=>{stream.text+=text;emit({type:"text-delta",id:"text",delta:text});};
|
|
||||||
stream.finish=()=>{emit({type:"text-end",id:"text"});emit({type:"finish"});controller.enqueue(encoder.encode("data: [DONE]\n\n"));controller.close();};
|
|
||||||
}}),{headers:{"Content-Type":"text/event-stream","x-vercel-ai-ui-message-stream":"v1"}});
|
|
||||||
};
|
|
||||||
});
|
|
||||||
await page.context().route("**/api/**",async route=>{
|
|
||||||
const request=route.request(),url=new URL(request.url()),path=url.pathname;
|
|
||||||
let data:unknown={};
|
|
||||||
if(path===`/api/canvas/${projectId}`)data={success:true,data:{id:projectId,title:"滚动验证",updatedAt:"2026-11-06T00:00:00.000Z",canvasData:{nodes:[],edges:[],viewport:{x:0,y:0,zoom:1}}}};
|
|
||||||
else if(path==="/api/auth/session")data={user:{id:"scroll-user",name:"滚动验证",type:"admin"},expires:"2099-01-01"};
|
|
||||||
else if(path.includes("validate-session"))data={success:true,valid:true};
|
|
||||||
else if(path==="/api/site-settings")data={canvasEnabled:true,agentEnabled:true,membershipEnabled:false};
|
|
||||||
else if(path==="/api/app-version")data={version:"development"};
|
|
||||||
else if(path==="/api/models")data={models:[{id:"image-test",modelId:"gpt-image-2",providerType:"openai",name:"GPT Image",type:"image",enabled:true,supportsImageGen:true}]};
|
|
||||||
else if(path==="/api/vote")data=[];
|
|
||||||
else if(path==="/api/canvas/agent-runs")data={run:state.run};
|
|
||||||
else if(path.includes("messages")){state.historyReads++;data={messages:url.searchParams.get("chatId")===projectId?state.history:[]};}
|
|
||||||
else if(path.includes("sessions"))data={sessions:[]};
|
|
||||||
else if(path.startsWith("/api/canvas/"))data={success:true,runs:[]};
|
|
||||||
await route.fulfill({contentType:"application/json",body:JSON.stringify(data)});
|
|
||||||
});
|
|
||||||
await page.goto(`/canvas/${projectId}`,{waitUntil:"domcontentloaded"});
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("历史记录 16");
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeLessThan(4);
|
|
||||||
return {state,errors};
|
|
||||||
}
|
|
||||||
const metrics=(page:Page)=>page.getByTestId("canvas-chat-messages").evaluate(element=>({top:element.scrollTop,bottom:element.scrollHeight-element.clientHeight-element.scrollTop}));
|
|
||||||
async function startStream(page:Page){
|
|
||||||
const editor=page.getByLabel("描述你的设计需求",{exact:true});await editor.fill("先分析和规划,再处理图片。");await editor.press("Enter");
|
|
||||||
await expect.poll(()=>page.evaluate(()=>Boolean((window as any).agentScrollStream.append))).toBe(true);
|
|
||||||
await page.evaluate(text=>(window as any).agentScrollStream.append(text),`开始分析\n\n${new Array(8).fill(paragraph).join("\n\n")}\n\n首次输出已显示\n\n`);
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("首次输出已显示");
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeLessThan(4);
|
|
||||||
}
|
|
||||||
async function scrollUp(page:Page){
|
|
||||||
const box=(await page.getByTestId("canvas-chat-messages").boundingBox())!;
|
|
||||||
await page.mouse.move(box.x+box.width/2,box.y+box.height/2);await page.mouse.wheel(0,-650);
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeGreaterThan(350);
|
|
||||||
await page.waitForTimeout(100);
|
|
||||||
return (await metrics(page)).top;
|
|
||||||
}
|
|
||||||
|
|
||||||
test("streaming Agent output preserves the position after the reader scrolls up",async({page})=>{
|
|
||||||
const {errors}=await setup(page);await startStream(page);const top=await scrollUp(page);
|
|
||||||
await page.evaluate(text=>(window as any).agentScrollStream.append(text),`新增分析步骤\n\n${new Array(5).fill(paragraph).join("\n\n")}`);
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("新增分析步骤");
|
|
||||||
await page.waitForTimeout(400);
|
|
||||||
expect(Math.abs((await metrics(page)).top-top)).toBeLessThan(4);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("returning to the latest message resumes following streamed output",async({page})=>{
|
|
||||||
const {errors}=await setup(page);await startStream(page);await scrollUp(page);
|
|
||||||
await expect(page.getByRole("button",{name:"回到最新消息",exact:true})).toBeVisible();
|
|
||||||
await page.screenshot({path:test.info().outputPath("reading-history-desktop.png"),animations:"disabled"});
|
|
||||||
await page.getByRole("button",{name:"回到最新消息",exact:true}).click();
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeLessThan(4);
|
|
||||||
await page.evaluate(text=>(window as any).agentScrollStream.append(text),`继续处理\n\n${new Array(5).fill(paragraph).join("\n\n")}`);
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("继续处理");
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeLessThan(4);
|
|
||||||
await scrollUp(page);
|
|
||||||
const box=(await page.getByTestId("canvas-chat-messages").boundingBox())!;
|
|
||||||
await page.mouse.move(box.x+box.width/2,box.y+box.height/2);await page.mouse.wheel(0,20000);
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeLessThan(4);
|
|
||||||
await page.evaluate(text=>(window as any).agentScrollStream.append(text),`已回到最新消息\n\n${paragraph}`);
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("已回到最新消息");
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeLessThan(4);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("finishing a streamed request and reloading its history preserve the reader and viewport",async({page})=>{
|
|
||||||
const {state,errors}=await setup(page);await startStream(page);const top=await scrollUp(page);
|
|
||||||
await page.getByTestId("canvas-chat-messages").evaluate(element=>(window as any).readingViewport=element);
|
|
||||||
const stream=await page.evaluate(()=>({request:(window as any).agentScrollStream.request,text:(window as any).agentScrollStream.text}));
|
|
||||||
state.history.push(stream.request.message,{...makeMessage("stream-assistant","assistant",stream.text,100),createdAt:new Date(Date.now()+100).toISOString()});
|
|
||||||
const reads=state.historyReads;
|
|
||||||
await page.evaluate(()=>(window as any).agentScrollStream.finish());
|
|
||||||
await expect(page.getByLabel("描述你的设计需求",{exact:true})).toBeEditable();
|
|
||||||
await expect.poll(()=>state.historyReads).toBeGreaterThan(reads);
|
|
||||||
await page.waitForTimeout(3800);
|
|
||||||
expect(await page.getByTestId("canvas-chat-messages").evaluate(element=>element===(window as any).readingViewport)).toBe(true);
|
|
||||||
expect(Math.abs((await metrics(page)).top-top)).toBeLessThan(4);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("background Agent progress restores text in the original chat without taking over scrolling",async({page})=>{
|
|
||||||
const {state,errors}=await setup(page);const top=await scrollUp(page);
|
|
||||||
const text=state.history.at(-1)!.parts[0].text+"\n\n后台任务继续分析参考图片。";
|
|
||||||
state.run={id:"native-scroll-run",chatId:projectId,projectId,assistantMessageId:state.history.at(-1)!.id,assistantText:text,status:"running",message:"正在分析图片",steps:2,toolCount:2,resultNodeIds:[],inputs:[],updatedAt:new Date().toISOString(),cancelRequested:false,resumable:false};
|
|
||||||
await page.evaluate(run=>window.dispatchEvent(new CustomEvent("canvas-agent-run:update",{detail:run})),state.run);
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("后台任务继续分析参考图片。");
|
|
||||||
await page.waitForTimeout(100);
|
|
||||||
expect(Math.abs((await metrics(page)).top-top)).toBeLessThan(4);
|
|
||||||
state.run={...state.run,status:"completed",assistantText:text+"\n\n后台任务处理结束。"};
|
|
||||||
await page.evaluate(run=>window.dispatchEvent(new CustomEvent("canvas-agent-run:update",{detail:run})),state.run);
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("后台任务处理结束。");
|
|
||||||
expect(Math.abs((await metrics(page)).top-top)).toBeLessThan(4);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("background history refresh preserves the same scroll container and reading position",async({page})=>{
|
|
||||||
await page.clock.install();
|
|
||||||
const {state,errors}=await setup(page);const top=await scrollUp(page);
|
|
||||||
await page.getByTestId("canvas-chat-messages").evaluate(element=>(window as any).readingViewport=element);
|
|
||||||
state.history.at(-1)!.parts[0].text+="\n\n历史记录已从服务端更新。";
|
|
||||||
const reads=state.historyReads;
|
|
||||||
await page.clock.fastForward(11000);
|
|
||||||
await page.evaluate(()=>window.dispatchEvent(new Event("focus")));
|
|
||||||
await expect.poll(()=>state.historyReads).toBeGreaterThan(reads);
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("历史记录已从服务端更新。");
|
|
||||||
expect(await page.getByTestId("canvas-chat-messages").evaluate(element=>element===(window as any).readingViewport)).toBe(true);
|
|
||||||
expect(Math.abs((await metrics(page)).top-top)).toBeLessThan(4);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a generated preview finishing its load does not pull a reader back to the bottom",async({page})=>{
|
|
||||||
let ready=false;let requests=0;const waiting:Array<()=>void>=[];
|
|
||||||
await page.context().route('https://scqnycdn.jiansun.vip/**',async route=>{
|
|
||||||
requests++;
|
|
||||||
if(!ready)await new Promise<void>(resolve=>waiting.push(resolve));
|
|
||||||
await route.fulfill({contentType:'image/svg+xml',body:'<svg xmlns="http://www.w3.org/2000/svg" width="800" height="1200"><rect width="800" height="1200" fill="#43b9bd"/></svg>'});
|
|
||||||
});
|
|
||||||
const {errors}=await setup(page);await startStream(page);
|
|
||||||
await page.evaluate(()=>(window as any).agentScrollStream.append('\n\n\n\n'));
|
|
||||||
await expect.poll(()=>requests).toBeGreaterThan(0);
|
|
||||||
const top=await scrollUp(page);ready=true;waiting.forEach(release=>release());
|
|
||||||
await expect.poll(()=>page.getByTestId('canvas-chat-messages').locator('img').evaluateAll(images=>images.some(image=>(image as HTMLImageElement).naturalWidth>0))).toBe(true);
|
|
||||||
await page.waitForTimeout(200);
|
|
||||||
expect(Math.abs((await metrics(page)).top-top)).toBeLessThan(4);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("keyboard scrolling pauses following and a new conversation starts at its own latest message",async({page})=>{
|
|
||||||
const {errors}=await setup(page);await startStream(page);
|
|
||||||
await page.getByTestId("canvas-chat-messages").press("PageUp");
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeGreaterThan(100);
|
|
||||||
const top=(await metrics(page)).top;
|
|
||||||
await page.evaluate(()=>(window as any).agentScrollStream.append("\n\n键盘上翻后的新进度。"));
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("键盘上翻后的新进度。");
|
|
||||||
await page.waitForTimeout(200);
|
|
||||||
expect((await metrics(page)).top).toBeLessThanOrEqual(top+4);
|
|
||||||
await page.getByRole("button",{name:"新建聊天窗口",exact:true}).click();
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).not.toContainText("历史记录 1");
|
|
||||||
await expect(page.getByLabel("描述你的设计需求",{exact:true})).toBeEditable();
|
|
||||||
await startStream(page);
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeLessThan(4);
|
|
||||||
expect(errors).toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test.describe("mobile touch reading",()=>{
|
|
||||||
test.use({viewport:{width:390,height:844},isMobile:true,hasTouch:true});
|
|
||||||
test("touch scrolling keeps history readable while the Agent continues streaming",async({page})=>{
|
|
||||||
const {errors}=await setup(page);await startStream(page);
|
|
||||||
const box=(await page.getByTestId("canvas-chat-messages").boundingBox())!;
|
|
||||||
const cdp=await page.context().newCDPSession(page);
|
|
||||||
const x=box.x+box.width/2,y=box.y+70,delta=Math.min(260,box.height-120);
|
|
||||||
await cdp.send("Input.dispatchTouchEvent",{type:"touchStart",touchPoints:[{x,y}]});
|
|
||||||
for(let i=1;i<=8;i++){
|
|
||||||
await cdp.send("Input.dispatchTouchEvent",{type:"touchMove",touchPoints:[{x,y:y+delta*i/8}]});
|
|
||||||
await page.waitForTimeout(25);
|
|
||||||
}
|
|
||||||
await cdp.send("Input.dispatchTouchEvent",{type:"touchEnd",touchPoints:[]});
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeGreaterThan(100);
|
|
||||||
const top=(await metrics(page)).top;
|
|
||||||
await page.evaluate(()=>(window as any).agentScrollStream.append("\n\n手机查看历史时的新进度。"));
|
|
||||||
await expect(page.getByTestId("canvas-chat-messages")).toContainText("手机查看历史时的新进度。");
|
|
||||||
await page.waitForTimeout(300);
|
|
||||||
expect((await metrics(page)).top).toBeLessThanOrEqual(top+4);
|
|
||||||
await expect(page.getByRole("button",{name:"回到最新消息",exact:true})).toBeVisible();
|
|
||||||
await page.screenshot({path:test.info().outputPath("reading-history-mobile.png"),animations:"disabled"});
|
|
||||||
await page.getByRole("button",{name:"回到最新消息",exact:true}).click();
|
|
||||||
await expect.poll(async()=>(await metrics(page)).bottom).toBeLessThan(4);
|
|
||||||
expect(errors).toEqual([]);await cdp.detach();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -1,119 +0,0 @@
|
|||||||
import { expect, test, type Page } from "@playwright/test";
|
|
||||||
|
|
||||||
test.setTimeout(30_000);
|
|
||||||
const browserErrors = new WeakMap<Page, string[]>();
|
|
||||||
test.beforeEach(({ page }) => {
|
|
||||||
const errors: string[] = [];
|
|
||||||
browserErrors.set(page, errors);
|
|
||||||
page.on("pageerror", error => errors.push(error.message));
|
|
||||||
});
|
|
||||||
test.afterEach(({ page }) => expect(browserErrors.get(page)).toEqual([]));
|
|
||||||
|
|
||||||
const projectId = "24cce650-7cfb-48bb-aa65-b04f490ac978";
|
|
||||||
const imageOrigin = "https://scqnycdn.jiansun.vip";
|
|
||||||
const failure = {
|
|
||||||
phase: "failed", requestId: "failed-request", userMessageId: "",
|
|
||||||
failure: { code: "SOURCE_IMAGE_UNAVAILABLE", outcome: "failed", message: "参考图片无法读取。", recommendation: "请重新上传原图后再提交。" },
|
|
||||||
};
|
|
||||||
|
|
||||||
async function setup(page: Page, lostStream = false) {
|
|
||||||
const submissions: { path: string; body: any }[] = [];
|
|
||||||
let messages: any[] = [];
|
|
||||||
const canvasData = {
|
|
||||||
nodes: [{ id: "preview-image", type: "image", position: { x: 220, y: 160 }, size: { width: 640, height: 420 },
|
|
||||||
data: { status: "completed", src: `${imageOrigin}/uploads/feedback-preview.png`, title: "参考图片" } }],
|
|
||||||
edges: [], viewport: { x: 0, y: 0, zoom: 1 },
|
|
||||||
};
|
|
||||||
await page.context().addInitScript(() => localStorage.setItem("theme", "light"));
|
|
||||||
await page.context().route("**/api/**", async route => {
|
|
||||||
const request = route.request();
|
|
||||||
const path = new URL(request.url()).pathname;
|
|
||||||
let data: unknown = {};
|
|
||||||
if (["/api/chat", "/api/canvas/agent-chat"].includes(path) && request.method() === "POST") {
|
|
||||||
const body = request.postDataJSON();
|
|
||||||
submissions.push({ path, body });
|
|
||||||
const terminal = { ...failure, userMessageId: body.message.id };
|
|
||||||
const text = `${terminal.failure.message}${terminal.failure.recommendation}\n\n请求编号:${terminal.requestId}`;
|
|
||||||
const assistant = { id: "failed-assistant", role: "assistant", createdAt: new Date(Date.now() + 100).toISOString(),
|
|
||||||
parts: [{ type: "text", text }, { type: "data-imageGenerationStatus", data: terminal }] };
|
|
||||||
messages = [{ ...body.message, createdAt: new Date().toISOString() }, assistant];
|
|
||||||
const chunks: any[] = [{ type: "start", messageId: "stream-assistant" },
|
|
||||||
{ type: "data-imageGenerationStatus", data: JSON.stringify({ phase: "processing", userMessageId: body.message.id }) }];
|
|
||||||
if (!lostStream) chunks.push(
|
|
||||||
{ type: "data-imageGenerationStatus", data: JSON.stringify(terminal) },
|
|
||||||
{ type: "data-appendMessage", data: JSON.stringify(assistant) },
|
|
||||||
{ type: "data-appendMessage", data: JSON.stringify({ id: "end", role: "assistant", parts: [{ type: "text", text: "<!-- IMAGE_GENERATION_END -->" }] }) },
|
|
||||||
);
|
|
||||||
chunks.push({ type: "finish" });
|
|
||||||
await route.fulfill({ headers: { "content-type": "text/event-stream", "x-vercel-ai-ui-message-stream": "v1" },
|
|
||||||
body: `${chunks.map(chunk => `data: ${JSON.stringify(chunk)}\n\n`).join("")}data: [DONE]\n\n` });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (path.includes("validate-session")) data = { valid: true, success: true };
|
|
||||||
else if (path === "/api/auth/session") data = { user: { id: "feedback-user", name: "测试", type: "admin" }, expires: "2099-01-01" };
|
|
||||||
else if (path === "/api/site-settings") data = { canvasEnabled: true, agentEnabled: true, membershipEnabled: false };
|
|
||||||
else if (path === "/api/vote") data = [];
|
|
||||||
else if (path === "/api/models") data = { models: [{ id: "image-test", modelId: "gpt-image-2", providerType: "openai", name: "GPT Image", type: "image", enabled: true, supportsImageGen: true, imageGenConfig: { creditCostPerImage: 15 } }] };
|
|
||||||
else if (path === `/api/canvas/${projectId}` && request.method() === "GET") data = { success: true, data: { id: projectId, title: "反馈验收", canvasData } };
|
|
||||||
else if (path.includes("messages")) data = { messages };
|
|
||||||
else if (path.includes("sessions")) data = { sessions: [] };
|
|
||||||
else if (path.startsWith("/api/canvas/")) data = { success: true, runs: [] };
|
|
||||||
await route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(data) });
|
|
||||||
});
|
|
||||||
await page.context().route(`${imageOrigin}/**`, async route => {
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 200));
|
|
||||||
await route.fulfill({ contentType: "image/svg+xml", body: '<svg xmlns="http://www.w3.org/2000/svg" width="640" height="420"><rect width="640" height="420" fill="#43b9bd"/></svg>' });
|
|
||||||
});
|
|
||||||
await page.goto(`/canvas/${projectId}`, { waitUntil: "domcontentloaded" });
|
|
||||||
await expect(page.getByRole("button", { name: "关闭 Agent,直接使用生图模型", exact: true })).toBeVisible();
|
|
||||||
return submissions;
|
|
||||||
}
|
|
||||||
|
|
||||||
test("image workspaces start with Agent enabled and send requests to its planning runtime", async ({ page }) => {
|
|
||||||
const submissions = await setup(page);
|
|
||||||
await page.getByLabel("描述你的设计需求", { exact: true }).fill("请先分析参考图片,再规划处理步骤。");
|
|
||||||
await page.getByLabel("描述你的设计需求", { exact: true }).press("Enter");
|
|
||||||
await expect.poll(() => submissions.length).toBe(1);
|
|
||||||
expect(submissions[0].path).toBe("/api/canvas/agent-chat");
|
|
||||||
expect(submissions[0].body.composerMode).toBe("agent");
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const lostStream of [false, true]) {
|
|
||||||
test(`image failure ends waiting and shows its cause${lostStream ? " after recovering history" : " from the stream"}`, async ({ page }) => {
|
|
||||||
const submissions = await setup(page, lostStream);
|
|
||||||
await page.getByRole("button", { name: "关闭 Agent,直接使用生图模型", exact: true }).click();
|
|
||||||
await page.getByLabel("描述你的设计需求", { exact: true }).fill("将参考图改成白色背景。");
|
|
||||||
await page.getByLabel("描述你的设计需求", { exact: true }).press("Enter");
|
|
||||||
const failedNode = page.locator('[data-canvas-node-id] [role="alert"]');
|
|
||||||
await expect(failedNode).toContainText("参考图片无法读取。");
|
|
||||||
await expect(failedNode).toContainText("请重新上传原图后再提交。");
|
|
||||||
await expect(failedNode).toContainText("请求编号:failed-request");
|
|
||||||
await expect(page.getByTestId("message-assistant-image-loading")).toHaveCount(0);
|
|
||||||
expect(submissions).toHaveLength(1);
|
|
||||||
expect(submissions[0].path).toBe("/api/chat");
|
|
||||||
await page.screenshot({ path: test.info().outputPath("terminal-failure.png") });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
test("zoom and hover keep a loaded canvas image visible while a new preview loads", async ({ page }) => {
|
|
||||||
await setup(page);
|
|
||||||
const node = page.locator('[data-canvas-node-id="preview-image"]');
|
|
||||||
await expect.poll(() => node.locator("img").first().evaluate((image: HTMLImageElement) => image.naturalWidth)).toBeGreaterThan(0);
|
|
||||||
await page.evaluate(() => {
|
|
||||||
(window as any).blankPreviewFrames = 0;
|
|
||||||
const inspect = () => {
|
|
||||||
const node = document.querySelector('[data-canvas-node-id="preview-image"]');
|
|
||||||
if (node && !Array.from(node.querySelectorAll("img")).some(image => Number(getComputedStyle(image).opacity) > 0 && image.naturalWidth > 0)) (window as any).blankPreviewFrames++;
|
|
||||||
requestAnimationFrame(inspect);
|
|
||||||
};
|
|
||||||
requestAnimationFrame(inspect);
|
|
||||||
});
|
|
||||||
await page.mouse.move(500, 350);
|
|
||||||
for (let i = 0; i < 18; i++) {
|
|
||||||
await page.mouse.wheel(0, i < 9 ? -100 : 100);
|
|
||||||
await page.waitForTimeout(50);
|
|
||||||
}
|
|
||||||
await page.mouse.move(100, 100);
|
|
||||||
await page.waitForTimeout(500);
|
|
||||||
expect(await page.evaluate(() => (window as any).blankPreviewFrames)).toBe(0);
|
|
||||||
});
|
|
||||||
@ -50,11 +50,9 @@ export function serverLoader(mocks: Record<string, unknown>, env: Record<string,
|
|||||||
setImmediate,
|
setImmediate,
|
||||||
AbortSignal,
|
AbortSignal,
|
||||||
AbortController,
|
AbortController,
|
||||||
structuredClone,
|
|
||||||
ReadableStream,
|
ReadableStream,
|
||||||
Headers,
|
Headers,
|
||||||
Blob,
|
Blob,
|
||||||
FormData,
|
|
||||||
Request,
|
Request,
|
||||||
Date: mocks.Date || Date,
|
Date: mocks.Date || Date,
|
||||||
document: mocks.document,
|
document: mocks.document,
|
||||||
|
|||||||
@ -127,9 +127,7 @@ test("Agent cannot open the retired angle panel through a generic UI command", a
|
|||||||
const events: unknown[] = [], definitions: any = {}, executable: any = {};
|
const events: unknown[] = [], definitions: any = {}, executable: any = {};
|
||||||
const load = serverLoader({ "@/lib/canvas/agent-mutations": {}, "./canvas": {} });
|
const load = serverLoader({ "@/lib/canvas/agent-mutations": {}, "./canvas": {} });
|
||||||
load("lib/ai/canvas-agent-runtime/image-access-tools.ts").registerImageCanvasAccessTools({ userId: "u", projectId: "p", runId: "r", selectedNodeIds: ["reference"], definitions, executable, emit: (...args: unknown[]) => events.push(args) });
|
load("lib/ai/canvas-agent-runtime/image-access-tools.ts").registerImageCanvasAccessTools({ userId: "u", projectId: "p", runId: "r", selectedNodeIds: ["reference"], definitions, executable, emit: (...args: unknown[]) => events.push(args) });
|
||||||
const error = await executable.openCanvasFeature.execute({ featureId: "canvas.ui.imageMultiAngle" }, "old-panel").catch((failure: unknown) => failure);
|
await expect(executable.openCanvasFeature.execute({ featureId: "canvas.ui.imageMultiAngle" }, "old-panel")).rejects.toThrow("图片画布界面入口");
|
||||||
expect(error).toBeInstanceOf(load("lib/ai/canvas-agent-runtime/protocol.ts").AgentToolRejectedError);
|
|
||||||
expect(error.message).toContain("图片画布界面入口");
|
|
||||||
expect(events).toHaveLength(0);
|
expect(events).toHaveLength(0);
|
||||||
});
|
});
|
||||||
test("access manifest covers all configured video functions and node/UI actions without granting fake backends", () => {
|
test("access manifest covers all configured video functions and node/UI actions without granting fake backends", () => {
|
||||||
|
|||||||
@ -1,59 +0,0 @@
|
|||||||
import { expect, test } from "@playwright/test";
|
|
||||||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
|
||||||
import sharp from "sharp";
|
|
||||||
|
|
||||||
test("Alibaba terminal task errors preserve the nested provider reason and do not resubmit", async () => {
|
|
||||||
const methods: string[] = [];
|
|
||||||
const loader = serverLoader({
|
|
||||||
"@/lib/storage/image-archive": {}, "@/lib/storage/service": {},
|
|
||||||
setTimeout: (callback: () => void, ms: number) => setTimeout(callback, ms === 5000 ? 0 : ms),
|
|
||||||
fetch: async (_url: string, init: RequestInit) => {
|
|
||||||
methods.push(init.method || "GET");
|
|
||||||
return Response.json(methods.length === 1
|
|
||||||
? { output: { task_id: "original-task", task_status: "PENDING" } }
|
|
||||||
: { output: { task_id: "original-task", task_status: "FAILED", code: "InvalidParameter", message: "Failed to download image URL after retries." } });
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const provider = loader("lib/ai/providers/alibaba-image.ts").createAlibabaImage({ apiKey: "fixture-key", baseURL: "https://dashscope.example/api/v1/services/aigc/multimodal-generation/generation" });
|
|
||||||
let error: unknown;
|
|
||||||
try { await provider.chat("qwen-image-3.0").doGenerate({ messages: [{ role: "user", content: "fixture" }], imageParams: { aspectRatio: "1:1" } }); } catch (caught) { error = caught; }
|
|
||||||
expect(String(error)).toContain("Failed to download image URL after retries.");
|
|
||||||
expect(String(error)).toContain("InvalidParameter");
|
|
||||||
expect(String(error)).toContain("original-task");
|
|
||||||
expect(methods).toEqual(["POST", "GET"]);
|
|
||||||
const failure = loader("lib/request-error.ts").getRequestFailureDetails(error);
|
|
||||||
expect(failure).toMatchObject({ code: "PROVIDER_REFERENCE_FETCH_FAILED", message: "模型服务未能读取参考图。" });
|
|
||||||
expect(failure.recommendation).toContain("模型取图线路");
|
|
||||||
expect(failure.recommendation).not.toContain("稍后重试");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("Qwen 3 sends reference bytes directly with the right MIME and keeps auto resolution", async () => {
|
|
||||||
const png = await sharp({ create: { width: 32, height: 64, channels: 3, background: "blue" } }).png().toBuffer();
|
|
||||||
const jpeg = await sharp(png).jpeg().toBuffer();
|
|
||||||
const webp = await sharp(png).webp().toBuffer();
|
|
||||||
const originals = [png, jpeg, webp];
|
|
||||||
let submitted: any;
|
|
||||||
let posts = 0;
|
|
||||||
const loader = serverLoader({
|
|
||||||
"@/lib/storage/image-archive": {}, "@/lib/storage/service": { uploadFile() { throw new Error("Reference transport must not upload an intermediate file"); } },
|
|
||||||
setTimeout: (callback: () => void, ms: number) => setTimeout(callback, ms === 5000 ? 0 : ms),
|
|
||||||
fetch: async (url: string, init: RequestInit = {}) => {
|
|
||||||
if (init.method === "POST") { posts++; submitted = JSON.parse(String(init.body)); return Response.json({ output: { task_id: "inline-task", task_status: "PENDING" } }); }
|
|
||||||
if (url.includes("/tasks/")) return Response.json({ output: { task_status: "FAILED", message: "transport-inspection-complete" } });
|
|
||||||
const index = Number(new URL(url).pathname.slice(1));
|
|
||||||
return new Response(originals[index], { headers: { "content-type": ["image/png", "image/jpeg", "image/webp"][index] } });
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const provider = loader("lib/ai/providers/alibaba-image.ts").createAlibabaImage({ apiKey: "fixture-key", baseURL: "https://dashscope.example" });
|
|
||||||
await expect(provider.chat("qwen-image-3.0").doGenerate({ messages: [{ role: "user", content: [
|
|
||||||
{ type: "text", text: "fixture" }, ...originals.map((_bytes, index) => ({ type: "image", image: `https://source.example/${index}` })),
|
|
||||||
] }], imageParams: { aspectRatio: "auto", imageSize: "2K" } })).rejects.toThrow("transport-inspection-complete");
|
|
||||||
expect(posts).toBe(1);
|
|
||||||
expect(submitted.parameters.size).toBe("1024*2048");
|
|
||||||
const images = submitted.input.messages[0].content.filter((part: any) => part.image).map((part: any) => part.image);
|
|
||||||
expect(images).toHaveLength(3);
|
|
||||||
images.forEach((value: string, index: number) => {
|
|
||||||
expect(value.split(";", 1)[0]).toBe(["data:image/png", "data:image/jpeg", "data:image/webp"][index]);
|
|
||||||
expect(Buffer.from(value.split(",")[1], "base64").equals(originals[index])).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -71,8 +71,6 @@ test("a submitted task with an unknown outcome is not submitted again through a
|
|||||||
await expect(h.generate(h.request)).rejects.toThrow();
|
await expect(h.generate(h.request)).rejects.toThrow();
|
||||||
expect(h.calls.map(c => c.id)).toEqual(["primary"]); expect(h.paused.size).toBe(0);
|
expect(h.calls.map(c => c.id)).toEqual(["primary"]); expect(h.paused.size).toBe(0);
|
||||||
expect(agentImageFailurePolicy(Object.assign(new Error("timeout"), { providerOutcomeUnknown: true })).retry).toBe(false);
|
expect(agentImageFailurePolicy(Object.assign(new Error("timeout"), { providerOutcomeUnknown: true })).retry).toBe(false);
|
||||||
expect(agentImageFailurePolicy(Object.assign(new Error("任务查询失败"), { providerOutcomeUnknown: true })).retry).toBe(false);
|
|
||||||
expect(agentImageFailurePolicy(Object.assign(new Error("This operation was aborted"), { name: "AbortError" })).retry).toBe(false);
|
|
||||||
expect(agentImageFailurePolicy(new Error("VIP98K task failed: timeout")).retry).toBe(true);
|
expect(agentImageFailurePolicy(new Error("VIP98K task failed: timeout")).retry).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -102,7 +102,7 @@ test("mixed-model batches bill and label only the delivered images using their a
|
|||||||
test("headless image features cannot substitute their default when a pinned model disappears", async () => {
|
test("headless image features cannot substitute their default when a pinned model disappears", async () => {
|
||||||
let generated = 0;
|
let generated = 0;
|
||||||
const mocks: Record<string, any> = {
|
const mocks: Record<string, any> = {
|
||||||
"@/lib/canvas/operations/box-cutout": {}, "@/lib/ai/box-cutout/service": {}, "@/lib/ai/extend/service": {}, "@/lib/ai/layer-split/service": {}, "@/lib/ai/resize/service": {}, "@/lib/ai/rmbg/service": {},
|
"@/lib/ai/box-cutout/service": {}, "@/lib/ai/extend/service": {}, "@/lib/ai/layer-split/service": {}, "@/lib/ai/resize/service": {}, "@/lib/ai/rmbg/service": {},
|
||||||
"@/lib/canvas/capabilities/internal-endpoint": {}, "@/lib/client/long-task": {},
|
"@/lib/canvas/capabilities/internal-endpoint": {}, "@/lib/client/long-task": {},
|
||||||
"@/lib/canvas/generate-image": { generateCanvasImage: async () => { generated++; return { images: [] }; } },
|
"@/lib/canvas/generate-image": { generateCanvasImage: async () => { generated++; return { images: [] }; } },
|
||||||
"@/lib/style-pro/model-options": { getStyleProImageModelEntries: async () => [{ model: { id: "default", modelId: "default", isDefault: true } }], sortStyleProImageModelEntries: (entries: any[]) => entries, findStyleProImageModelByRef: () => null },
|
"@/lib/style-pro/model-options": { getStyleProImageModelEntries: async () => [{ model: { id: "default", modelId: "default", isDefault: true } }], sortStyleProImageModelEntries: (entries: any[]) => entries, findStyleProImageModelByRef: () => null },
|
||||||
|
|||||||
@ -1,15 +1,4 @@
|
|||||||
import { expect, test } from "@playwright/test";
|
import { expect, test } from "@playwright/test";
|
||||||
|
|
||||||
test("image metadata containing video or audio paths remains an image", () => {
|
|
||||||
for (const folder of ["video-canvas-demo", "audio-preview"]) {
|
|
||||||
const url = `https://demo-sparkai.oss-cn-shanghai.aliyuncs.com/${folder}/cover.png`;
|
|
||||||
const markdown = ``;
|
|
||||||
expect(extractVideoUrls(markdown)).toEqual([]);
|
|
||||||
expect(extractAudioUrls(markdown)).toEqual([]);
|
|
||||||
expect(extractAssistantImageUrls(markdown)).toContain(url);
|
|
||||||
expect(stripMediaMarkup(markdown)).toBe(markdown);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
import {
|
import {
|
||||||
collectMessageImageUrls,
|
collectMessageImageUrls,
|
||||||
extractAssistantImageUrls,
|
extractAssistantImageUrls,
|
||||||
@ -808,7 +797,6 @@ test.describe("canvas chat pure helpers", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("canvas service save uses store revision fallback when options are omitted", async () => {
|
test("canvas service save uses store revision fallback when options are omitted", async () => {
|
||||||
useCanvasStore.getState().setProject("11111111-1111-4111-8111-111111111111");
|
|
||||||
const canvasData = {
|
const canvasData = {
|
||||||
nodes: [],
|
nodes: [],
|
||||||
edges: [],
|
edges: [],
|
||||||
@ -886,7 +874,7 @@ test.describe("canvas chat pure helpers", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("canvas revision advances after applying the fetched snapshot and saving the current project", async () => {
|
test("canvas service keeps store revision aligned after project load and save", async () => {
|
||||||
const canvasData = {
|
const canvasData = {
|
||||||
nodes: [],
|
nodes: [],
|
||||||
edges: [],
|
edges: [],
|
||||||
@ -916,7 +904,6 @@ test.describe("canvas chat pure helpers", () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
useCanvasStore.getState().setProject("11111111-1111-4111-8111-111111111111");
|
|
||||||
useCanvasStore.getState().setCanvasRevision(null, null);
|
useCanvasStore.getState().setCanvasRevision(null, null);
|
||||||
Object.defineProperty(globalThis, "window", {
|
Object.defineProperty(globalThis, "window", {
|
||||||
configurable: true,
|
configurable: true,
|
||||||
@ -931,11 +918,7 @@ test.describe("canvas chat pure helpers", () => {
|
|||||||
}) as Response) as typeof fetch;
|
}) as Response) as typeof fetch;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await canvasService.getProject("11111111-1111-4111-8111-111111111111");
|
await canvasService.getProject("11111111-1111-4111-8111-111111111111");
|
||||||
expect(useCanvasStore.getState().canvasRevision).toBeNull();
|
|
||||||
useCanvasStore.getState().loadCanvas(result.data!.canvasData);
|
|
||||||
const revision = String(result.data!.updatedAt);
|
|
||||||
useCanvasStore.getState().setCanvasRevision(revision, revision);
|
|
||||||
expect(useCanvasStore.getState().canvasRevision).toBe(
|
expect(useCanvasStore.getState().canvasRevision).toBe(
|
||||||
"2026-06-22T00:01:00.000Z"
|
"2026-06-22T00:01:00.000Z"
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,159 +0,0 @@
|
|||||||
import { expect, test } from "@playwright/test";
|
|
||||||
import { randomUUID } from "node:crypto";
|
|
||||||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
|
||||||
|
|
||||||
const auth = { auth: async () => ({ user: { id: "owner" } }) };
|
|
||||||
const request = () => ({
|
|
||||||
url: "https://app.example/api/canvas/download-image", headers: new Headers(),
|
|
||||||
json: async () => ({ url: "https://provider.example/original.jpg", preferDirect: true, preparation: "async" }),
|
|
||||||
});
|
|
||||||
|
|
||||||
test("download failures distinguish permanent sources, transport, conversion and storage with actionable advice", () => {
|
|
||||||
const { getDownloadFailure, formatDownloadFailure } = serverLoader({})("lib/canvas/download-error.ts");
|
|
||||||
for (const [error, code, status] of [
|
|
||||||
["HTTP 404", "SOURCE_NOT_FOUND", 404], ["HTTP 403", "SOURCE_FORBIDDEN", 403],
|
|
||||||
["TimeoutError: slow body", "SOURCE_TIMEOUT", 504], ["fetch failed", "SOURCE_UNAVAILABLE", 503],
|
|
||||||
["source returned an error document", "INVALID_IMAGE", 422], ["unsupported image format", "IMAGE_CONVERSION_FAILED", 422],
|
|
||||||
["STORAGE_WRITE_FAILED", "DOWNLOAD_STORAGE_FAILED", 503], ["DOWNLOAD_BUSY", "DOWNLOAD_BUSY", 429],
|
|
||||||
]) {
|
|
||||||
const failure = getDownloadFailure(new Error(error as string));
|
|
||||||
expect(failure).toMatchObject({ code, status });
|
|
||||||
expect(failure.recommendation).toBeTruthy();
|
|
||||||
expect(formatDownloadFailure(failure, "feedback-id")).toContain("下载编号:feedback-id");
|
|
||||||
if (status === 404) expect(failure.recommendation).not.toContain("稍后重试");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test("failed preparation polls retain the same cause and feedback ID without rerunning the load", async () => {
|
|
||||||
const { createDownloadPreparationQueue } = serverLoader({})("lib/canvas/download-preparation.ts");
|
|
||||||
const runs: Array<() => Promise<void>> = [];
|
|
||||||
let loads = 0;
|
|
||||||
const queue = createDownloadPreparationQueue({
|
|
||||||
load: async () => { loads++; throw new Error("HTTP 404 https://source.example/private?token=private"); },
|
|
||||||
read: async () => null, write: async () => {}, schedule: (run: () => Promise<void>) => runs.push(run),
|
|
||||||
});
|
|
||||||
const input = { userId: "owner", urls: ["https://source.example/original"], requestOrigin: "https://app.example", cookie: "session=private" };
|
|
||||||
const accepted = await queue.request(input);
|
|
||||||
await runs[0]();
|
|
||||||
for (let i = 0; i < 3; i++) {
|
|
||||||
const failed = await queue.request(input);
|
|
||||||
expect(failed).toMatchObject({ status: "failed", requestId: accepted.requestId, failure: { code: "SOURCE_NOT_FOUND", status: 404 } });
|
|
||||||
expect(failed.error).not.toContain("token=private");
|
|
||||||
}
|
|
||||||
expect(loads).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("download queue saturation returns a retryable 429 instead of a source 502", async () => {
|
|
||||||
const { createDownloadPreparationQueue } = serverLoader({})("lib/canvas/download-preparation.ts");
|
|
||||||
const queue = createDownloadPreparationQueue({ load: async () => { throw new Error("not started"); }, read: async () => null, write: async () => {}, schedule() {} });
|
|
||||||
for (let i = 0; i < 4; i++) await queue.request({ userId: "owner", urls: [`https://source.example/${i}`], requestOrigin: "https://app.example", cookie: "" });
|
|
||||||
expect(await queue.request({ userId: "owner", urls: ["https://source.example/fifth"], requestOrigin: "https://app.example", cookie: "" }))
|
|
||||||
.toMatchObject({ status: "failed", failure: { code: "DOWNLOAD_BUSY", status: 429, retryable: true } });
|
|
||||||
});
|
|
||||||
|
|
||||||
test("cold archived downloads wait for CDN preparation and never buffer the file through the app", async () => {
|
|
||||||
const queued: string[] = [];
|
|
||||||
let ready = false;
|
|
||||||
const route = serverLoader({
|
|
||||||
"@/app/(auth)/auth": auth,
|
|
||||||
"next/server": { NextResponse: Response, after() {} },
|
|
||||||
"@/lib/storage/canvas-cdn-download": { resolveCanvasCdnDownload: async () => ({
|
|
||||||
task: { id: "archive-task", targetUrl: "https://cdn.example/original.jpg", metadata: { downloadAsset: { status: "pending" } } },
|
|
||||||
canPrepareDirect: true,
|
|
||||||
direct: ready ? { delivery: "cdn", downloadUrl: "https://cdn.example/prepared.png", filename: "original.png", bytes: 4000000 } : null,
|
|
||||||
}) },
|
|
||||||
"@/lib/storage/image-download-preparation": { scheduleImageDownloadPreparation: (id: string) => queued.push(id) },
|
|
||||||
"@/lib/canvas/photoshop-safe-download": { sanitizeCanvasDownloadFileName: () => "original", fetchCanvasDownloadAsset: () => { throw new Error("Must use CDN delivery"); } },
|
|
||||||
})("app/api/canvas/download-image/route.ts");
|
|
||||||
const first = await route.POST(request());
|
|
||||||
expect(first.status).toBe(202);
|
|
||||||
expect(await first.json()).toMatchObject({ delivery: "preparing", requestId: "archive-task" });
|
|
||||||
ready = true;
|
|
||||||
const second = await route.POST(request());
|
|
||||||
expect(second.status).toBe(200);
|
|
||||||
expect(await second.json()).toMatchObject({ delivery: "cdn", bytes: 4000000 });
|
|
||||||
expect(queued).toEqual(["archive-task"]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("an expired archived source returns its permanent cause and download task ID", async () => {
|
|
||||||
const errors = serverLoader({})("lib/canvas/download-error.ts");
|
|
||||||
const route = serverLoader({
|
|
||||||
"@/app/(auth)/auth": auth, "next/server": { NextResponse: Response, after() {} },
|
|
||||||
"@/lib/storage/canvas-cdn-download": { resolveCanvasCdnDownload: async () => ({ task: {
|
|
||||||
id: "failed-archive", targetUrl: "https://cdn.example/missing.png",
|
|
||||||
metadata: { downloadAsset: { status: "abandoned", failure: errors.getDownloadFailure("HTTP 404") } },
|
|
||||||
}, canPrepareDirect: true, direct: null }) },
|
|
||||||
"@/lib/storage/image-download-preparation": { scheduleImageDownloadPreparation() { throw new Error("Do not retry a permanent failure"); } },
|
|
||||||
})("app/api/canvas/download-image/route.ts");
|
|
||||||
const response = await route.POST(request());
|
|
||||||
expect(response.status).toBe(404);
|
|
||||||
expect(await response.json()).toMatchObject({ requestId: "failed-archive", failure: { code: "SOURCE_NOT_FOUND" } });
|
|
||||||
});
|
|
||||||
|
|
||||||
test("the download browser keeps the structured cause, advice and feedback ID", async () => {
|
|
||||||
const failure = { code: "SOURCE_FORBIDDEN", message: "图片源站拒绝访问原图。", recommendation: "请联系管理员检查访问权限。", status: 403, retryable: false };
|
|
||||||
const client = serverLoader({ fetch: async () => Response.json({ failure, requestId: "real-feedback" }, { status: 403 }) })("lib/client/canvas-image-download.ts");
|
|
||||||
let caught: unknown;
|
|
||||||
try { await client.downloadCanvasImageAsPng({ url: "https://source.example/original" }); } catch (error) { caught = error; }
|
|
||||||
expect(client.getCanvasImageDownloadErrorMessage(caught)).toBe("图片源站拒绝访问原图。请联系管理员检查访问权限。\n下载编号:real-feedback");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("the preparation deadline cancels the source body as well as its headers", async () => {
|
|
||||||
const cache = serverLoader({ fetch: async (_url: string, options: { signal: AbortSignal }) => ({
|
|
||||||
ok: true, headers: new Headers({ "content-type": "image/png" }),
|
|
||||||
arrayBuffer: () => new Promise((_resolve, reject) => options.signal.addEventListener("abort", () => reject(options.signal.reason), { once: true })),
|
|
||||||
}) })("lib/storage/image-byte-cache.ts");
|
|
||||||
const controller = new AbortController();
|
|
||||||
const fetching = cache.fetchImageBytes(`https://source.example/${randomUUID()}`, { timeoutMs: 10000, signal: controller.signal });
|
|
||||||
setTimeout(() => controller.abort(new Error("Preparation canceled")), 20);
|
|
||||||
await expect(fetching).rejects.toThrow("Preparation canceled");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("requested downloads are prioritized with two workers before scanning background assets", async () => {
|
|
||||||
let active = 0; let maximum = 0; let scans = 0;
|
|
||||||
const releases: Array<() => void> = [];
|
|
||||||
const workRecords: Array<{ active: number; durable: number }> = [];
|
|
||||||
const claimed: string[] = []; const saved: string[] = [];
|
|
||||||
const bytes = Buffer.from("compatible PNG fixture");
|
|
||||||
const worker = serverLoader({
|
|
||||||
"node:fs": { writeFileSync: (_path: string, value: string) => workRecords.push(JSON.parse(value)), unlinkSync() {} },
|
|
||||||
"./image-archive-db": {
|
|
||||||
claimImageDownloadTask: async (id: string) => { claimed.push(id); return { id, targetUrl: `https://cdn.example/${id}.png`, metadata: {} }; },
|
|
||||||
saveImageDownloadAsset: async (id: string) => saved.push(id),
|
|
||||||
getImageDownloadTasks: async () => { scans++; return []; }, hasPendingImageDownloads: async () => false,
|
|
||||||
},
|
|
||||||
"./image-byte-cache": { fetchImageBytes: async () => { active++; maximum = Math.max(maximum, active); await new Promise<void>(resolve => releases.push(resolve)); active--; return { buffer: bytes, contentType: "image/png" }; }, cacheImageBytes: async () => {} },
|
|
||||||
"@/lib/canvas/photoshop-safe-download": { normalizeCanvasDownloadAsset: async () => ({ ok: true, bytes, contentType: "image/png" }) },
|
|
||||||
"./service": { uploadFile() { throw new Error("Compatible PNG must retain its object"); } },
|
|
||||||
})("lib/storage/image-download-preparation.ts");
|
|
||||||
worker.scheduleImageDownloadPreparation("one"); worker.scheduleImageDownloadPreparation("two"); worker.scheduleImageDownloadPreparation("three");
|
|
||||||
await expect.poll(() => active).toBe(2);
|
|
||||||
expect(workRecords.at(-1)).toMatchObject({ active: 1, durable: 0 });
|
|
||||||
expect(scans).toBe(0);
|
|
||||||
releases.splice(0).forEach(release => release());
|
|
||||||
await expect.poll(() => claimed.length).toBe(3);
|
|
||||||
releases.splice(0).forEach(release => release());
|
|
||||||
await expect.poll(() => saved.length).toBe(3);
|
|
||||||
expect(maximum).toBe(2); expect(scans).toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("archive URL replacement cannot block the download worker wakeup", async () => {
|
|
||||||
let scheduled = false; let finish!: () => void; let replacing = false;
|
|
||||||
const gate = new Promise<void>(resolve => { finish = resolve; });
|
|
||||||
const job = serverLoader({
|
|
||||||
"@/lib/observability/active-work": { trackRuntimeWork: (execute: () => Promise<unknown>) => execute() },
|
|
||||||
"@/lib/storage/image-download-preparation": { scheduleImageDownloadPreparation() { scheduled = true; } },
|
|
||||||
"@/lib/storage/image-archive-db": { recoverStaleImageArchiveTasks: async () => {}, getImageArchiveTasksToStore: async () => [],
|
|
||||||
getImageArchiveTasksToReplace: async () => [{ id: "archive", sourceUrl: "https://source.example/a", targetUrl: "https://cdn.example/a" }],
|
|
||||||
markImageArchiveTaskReplacing: async () => true, markImageArchiveTaskCompleted: async () => {}, hasPendingImageDownloads: async () => false,
|
|
||||||
getImageArchiveTaskStats: async () => ({ pendingStore: 0, pendingReplace: 0 }),
|
|
||||||
},
|
|
||||||
"@/lib/storage/image-url-replacement": { replaceImageUrlInDatabase: async () => { replacing = true; await gate; return { changed: 1 }; } },
|
|
||||||
"@/lib/storage/image-archive": {},
|
|
||||||
})("lib/jobs/image-archive-job.ts");
|
|
||||||
const running = job.runImageArchiveJob();
|
|
||||||
await expect.poll(() => replacing).toBe(true);
|
|
||||||
expect(scheduled).toBe(true);
|
|
||||||
finish();
|
|
||||||
expect((await running).replace.succeeded).toBe(1);
|
|
||||||
});
|
|
||||||
@ -236,6 +236,8 @@ test.describe("canvas scale adjust product lock", () => {
|
|||||||
const rmbg = readSource("lib/ai/rmbg/service.ts");
|
const rmbg = readSource("lib/ai/rmbg/service.ts");
|
||||||
|
|
||||||
expect(productLock).toContain('foregroundType: "person"');
|
expect(productLock).toContain('foregroundType: "person"');
|
||||||
|
expect(productLock).toContain("attempt <= 3");
|
||||||
|
expect(productLock).toContain("executeScaleAdjustRmbgJob({");
|
||||||
expect(productLock).toContain("regionPersonExclusionAlpha");
|
expect(productLock).toContain("regionPersonExclusionAlpha");
|
||||||
expect(productLock).toContain("overlapPixelCount >= overlapLimit");
|
expect(productLock).toContain("overlapPixelCount >= overlapLimit");
|
||||||
expect(productLock).toContain("目标产品被人物遮挡");
|
expect(productLock).toContain("目标产品被人物遮挡");
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import { executeCanvasToolbarCapability } from "@/lib/client/canvas-toolbar-capa
|
|||||||
import { createCanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
import { createCanvasCapabilityClientCommand } from "@/lib/canvas/capabilities/client-command";
|
||||||
import { useCanvasStore } from "@/store/canvas";
|
import { useCanvasStore } from "@/store/canvas";
|
||||||
import { canvasService } from "@/services/canvas";
|
import { canvasService } from "@/services/canvas";
|
||||||
import { formatRequestFailure, getRequestFailureDetails } from "@/lib/request-error";
|
|
||||||
|
|
||||||
const originalFetch = globalThis.fetch;
|
const originalFetch = globalThis.fetch;
|
||||||
const node = (id: string) => ({ id, type: "image", position: { x: 0, y: 0 },
|
const node = (id: string) => ({ id, type: "image", position: { x: 0, y: 0 },
|
||||||
@ -87,49 +86,6 @@ test("a lost submission response polls the original task and never resubmits", a
|
|||||||
expect(useCanvasStore.getState().nodes.map(n => n.id)).toEqual(["source", "result"]);
|
expect(useCanvasStore.getState().nodes.map(n => n.id)).toEqual(["source", "result"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("a recovered matting rejection shows its dimensions and advice without changing the canvas or resubmitting", async () => {
|
|
||||||
const submitted = createCanvasCapabilityClientCommand({ projectId: "project-a", capabilityId: "image.remove_background", nodeIds: ["source"], source: "toolbar" });
|
|
||||||
const failure = getRequestFailureDetails("抠图参考图分辨率超限(5056×3392 像素),当前 AOS 上游要求宽、高均不超过 4096 像素。");
|
|
||||||
const message = formatRequestFailure(failure, submitted.requestId);
|
|
||||||
let posts = 0;
|
|
||||||
globalThis.fetch = (async (url, options) => {
|
|
||||||
if (options?.method === "POST") { posts++; return new Response(null, { status: 504 }); }
|
|
||||||
expect(String(url)).toContain(encodeURIComponent(submitted.requestId));
|
|
||||||
return Response.json({ success: true, task: { status: "failed", failure, error: message } });
|
|
||||||
}) as typeof fetch;
|
|
||||||
await expect(executeCanvasToolbarCapability(submitted)).rejects.toThrow(message);
|
|
||||||
expect(posts).toBe(1);
|
|
||||||
expect(useCanvasStore.getState().nodes.map(n => n.id)).toEqual(["source"]);
|
|
||||||
expect(useCanvasStore.getState().canvasRevision).toBe("r1");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a lost box-cutout response recovers the same selection into its original canvas with one submission", async () => {
|
|
||||||
const submitted = createCanvasCapabilityClientCommand({ projectId: "project-a", capabilityId: "image.box_cutout", nodeIds: ["source"], source: "toolbar", args: { x: 0.25, y: 0.2, width: 0.4, height: 0.5, paddingRatio: 0.08 } });
|
|
||||||
let posts = 0, polls = 0;
|
|
||||||
const selected = { ...node("cutout"), data: { src: "https://example.com/cutout.png", metadata: { editMode: "box-cutout", cropRectApplied: submitted.args } } };
|
|
||||||
globalThis.fetch = (async (url, options) => {
|
|
||||||
if (options?.method === "POST") {
|
|
||||||
posts++;
|
|
||||||
expect(JSON.parse(String(options.body))).toMatchObject({ capabilityId: "image.box_cutout", requestId: submitted.requestId, args: submitted.args });
|
|
||||||
useCanvasStore.getState().moveNode("source", { x: 20, y: 30 });
|
|
||||||
return new Response(null, { status: 524 });
|
|
||||||
}
|
|
||||||
if (String(url).includes("/capabilities?")) {
|
|
||||||
polls++;
|
|
||||||
expect(String(url)).toContain(encodeURIComponent(submitted.requestId));
|
|
||||||
return Response.json({ success: true, task: { status: "succeeded" } });
|
|
||||||
}
|
|
||||||
return Response.json({ success: true, data: { updatedAt: "r2", canvasData: { nodes: [node("source"), selected], edges: [] } } });
|
|
||||||
}) as typeof fetch;
|
|
||||||
await executeCanvasToolbarCapability(submitted);
|
|
||||||
expect({ posts, polls }).toEqual({ posts: 1, polls: 1 });
|
|
||||||
const state = useCanvasStore.getState();
|
|
||||||
expect(state.projectId).toBe("project-a");
|
|
||||||
expect(state.nodes.map(n => n.id)).toEqual(["source", "cutout"]);
|
|
||||||
expect(state.nodes[0].position).toEqual({ x: 20, y: 30 });
|
|
||||||
expect(state.nodes[1].data).toEqual(selected.data);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("switching projects during processing does not load the old result into the new project", async () => {
|
test("switching projects during processing does not load the old result into the new project", async () => {
|
||||||
globalThis.fetch = (async (url) => {
|
globalThis.fetch = (async (url) => {
|
||||||
if (String(url).endsWith("/capabilities")) {
|
if (String(url).endsWith("/capabilities")) {
|
||||||
@ -163,71 +119,6 @@ test("metadata changes never adopt an unseen remote canvas revision", async () =
|
|||||||
expect(useCanvasStore.getState().canvasRevision).toBe("r1");
|
expect(useCanvasStore.getState().canvasRevision).toBe("r1");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("reading a result without applying it cannot authorize an old snapshot to overwrite the result", async () => {
|
|
||||||
const previousWindow = Object.getOwnPropertyDescriptor(globalThis, "window");
|
|
||||||
Object.defineProperty(globalThis, "window", { configurable: true, value: {} });
|
|
||||||
let remoteNodes = [node("source"), node("result")];
|
|
||||||
globalThis.fetch = (async (_url, options) => {
|
|
||||||
if (options?.method === "PUT") {
|
|
||||||
const body = JSON.parse(String(options.body));
|
|
||||||
if (body.baseCanvasRevision !== "r2") return Response.json({ success: false, error: "画布已更新" }, { status: 409 });
|
|
||||||
remoteNodes = body.canvasData.nodes;
|
|
||||||
return Response.json({ success: true, data: { updatedAt: "r3" } });
|
|
||||||
}
|
|
||||||
return Response.json({ success: true, data: { id: "project-a", updatedAt: "r2", canvasData: { nodes: remoteNodes, edges: [] } } });
|
|
||||||
}) as typeof fetch;
|
|
||||||
try {
|
|
||||||
await canvasService.getProject("project-a");
|
|
||||||
const saved = await canvasService.saveCanvasState("project-a", useCanvasStore.getState().getCanvasState());
|
|
||||||
expect(saved).toMatchObject({ success: false, status: 409 });
|
|
||||||
expect(remoteNodes.map(n => n.id)).toEqual(["source", "result"]);
|
|
||||||
expect(useCanvasStore.getState().canvasRevision).toBe("r1");
|
|
||||||
} finally {
|
|
||||||
if (previousWindow) Object.defineProperty(globalThis, "window", previousWindow);
|
|
||||||
else Reflect.deleteProperty(globalThis, "window");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a delayed read cannot change the revision of a newly selected project", async () => {
|
|
||||||
const previousWindow = Object.getOwnPropertyDescriptor(globalThis, "window");
|
|
||||||
Object.defineProperty(globalThis, "window", { configurable: true, value: {} });
|
|
||||||
let complete!: (response: Response) => void;
|
|
||||||
globalThis.fetch = (() => new Promise<Response>(resolve => { complete = resolve; })) as typeof fetch;
|
|
||||||
try {
|
|
||||||
const pending = canvasService.getProject("project-a");
|
|
||||||
useCanvasStore.getState().setProject("project-b");
|
|
||||||
useCanvasStore.getState().loadCanvas({ nodes: [node("other") as any], edges: [] });
|
|
||||||
useCanvasStore.getState().setCanvasRevision("r9", "r9");
|
|
||||||
complete(Response.json({ success: true, data: { id: "project-a", updatedAt: "r2", canvasData: { nodes: [node("result")], edges: [] } } }));
|
|
||||||
await pending;
|
|
||||||
expect(useCanvasStore.getState().canvasRevision).toBe("r9");
|
|
||||||
expect(useCanvasStore.getState().nodes.map(n => n.id)).toEqual(["other"]);
|
|
||||||
} finally {
|
|
||||||
if (previousWindow) Object.defineProperty(globalThis, "window", previousWindow);
|
|
||||||
else Reflect.deleteProperty(globalThis, "window");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a delayed save cannot change the revision of a newly selected project", async () => {
|
|
||||||
const previousWindow = Object.getOwnPropertyDescriptor(globalThis, "window");
|
|
||||||
Object.defineProperty(globalThis, "window", { configurable: true, value: {} });
|
|
||||||
let complete!: (response: Response) => void;
|
|
||||||
globalThis.fetch = (() => new Promise<Response>(resolve => { complete = resolve; })) as typeof fetch;
|
|
||||||
try {
|
|
||||||
const pending = canvasService.saveCanvasState("project-a", useCanvasStore.getState().getCanvasState());
|
|
||||||
useCanvasStore.getState().setProject("project-b");
|
|
||||||
useCanvasStore.getState().loadCanvas({ nodes: [node("other") as any], edges: [] });
|
|
||||||
useCanvasStore.getState().setCanvasRevision("r9", "r9");
|
|
||||||
complete(Response.json({ success: true, data: { updatedAt: "r2" } }));
|
|
||||||
await pending;
|
|
||||||
expect(useCanvasStore.getState().canvasRevision).toBe("r9");
|
|
||||||
expect(useCanvasStore.getState().nodes.map(n => n.id)).toEqual(["other"]);
|
|
||||||
} finally {
|
|
||||||
if (previousWindow) Object.defineProperty(globalThis, "window", previousWindow);
|
|
||||||
else Reflect.deleteProperty(globalThis, "window");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test("a result merge conflict retains both the local edit and its original revision", async () => {
|
test("a result merge conflict retains both the local edit and its original revision", async () => {
|
||||||
globalThis.fetch = (async (url) => {
|
globalThis.fetch = (async (url) => {
|
||||||
if (String(url).endsWith("/capabilities")) {
|
if (String(url).endsWith("/capabilities")) {
|
||||||
|
|||||||
@ -7,9 +7,6 @@ import { randomUUID } from "node:crypto";
|
|||||||
import JSZip from "jszip";
|
import JSZip from "jszip";
|
||||||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
||||||
import { parseDownloadByteRange } from "@/lib/canvas/download-byte-range";
|
import { parseDownloadByteRange } from "@/lib/canvas/download-byte-range";
|
||||||
import { AbortMultipartUploadCommand, CompleteMultipartUploadCommand, CreateMultipartUploadCommand, HeadObjectCommand, PutObjectCommand, UploadPartCommand } from "@aws-sdk/client-s3";
|
|
||||||
|
|
||||||
const runtimeWork = { trackRuntimeWork: async (run: () => Promise<void>) => run() };
|
|
||||||
|
|
||||||
test("ranges reject invalid offsets and preserve normal, open and suffix byte ranges", () => {
|
test("ranges reject invalid offsets and preserve normal, open and suffix byte ranges", () => {
|
||||||
expect(parseDownloadByteRange("bytes=0-3", 20)).toEqual({start:0,end:3});
|
expect(parseDownloadByteRange("bytes=0-3", 20)).toEqual({start:0,end:3});
|
||||||
@ -23,7 +20,7 @@ test("multi-select ZIP admission is immediate; complete archives survive restart
|
|||||||
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-test-')); let owner:string|null='owner';let fetches=0;
|
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-test-')); let owner:string|null='owner';let fetches=0;
|
||||||
const callbacks:Array<()=>Promise<void>>=[];
|
const callbacks:Array<()=>Promise<void>>=[];
|
||||||
const mocks={
|
const mocks={
|
||||||
'@/lib/observability/active-work':runtimeWork,'@/app/(auth)/auth': {auth:async()=>owner?{user:{id:owner}}:null},
|
'@/app/(auth)/auth': {auth:async()=>owner?{user:{id:owner}}:null},
|
||||||
'next/server': {NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
'next/server': {NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
||||||
'@/lib/canvas/photoshop-safe-download':{fetchCanvasDownloadAsset:async({index}:any)=>{
|
'@/lib/canvas/photoshop-safe-download':{fetchCanvasDownloadAsset:async({index}:any)=>{
|
||||||
fetches++;return {ok:true,filename:`${index}-花瓶.png`,bytes:Buffer.from(`image-${index}`),contentType:'image/png'};
|
fetches++;return {ok:true,filename:`${index}-花瓶.png`,bytes:Buffer.from(`image-${index}`),contentType:'image/png'};
|
||||||
@ -59,7 +56,7 @@ test("multi-select ZIP admission is immediate; complete archives survive restart
|
|||||||
test("all-source failures never produce an empty successful ZIP; retries reuse the task and resolve sources again", async()=>{
|
test("all-source failures never produce an empty successful ZIP; retries reuse the task and resolve sources again", async()=>{
|
||||||
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-retry-'));const callbacks:Array<()=>Promise<void>>=[];let available=false;
|
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-retry-'));const callbacks:Array<()=>Promise<void>>=[];let available=false;
|
||||||
const route=serverLoader({
|
const route=serverLoader({
|
||||||
'@/lib/observability/active-work':runtimeWork,'@/app/(auth)/auth':{auth:async()=>({user:{id:'owner'}})},
|
'@/app/(auth)/auth':{auth:async()=>({user:{id:'owner'}})},
|
||||||
'next/server':{NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
'next/server':{NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
||||||
'@/lib/canvas/photoshop-safe-download':{fetchCanvasDownloadAsset:async()=>available?{ok:true,filename:'one.png',bytes:Buffer.from('one'),contentType:'image/png'}:{ok:false,error:'expired provider URL'}},
|
'@/lib/canvas/photoshop-safe-download':{fetchCanvasDownloadAsset:async()=>available?{ok:true,filename:'one.png',bytes:Buffer.from('one'),contentType:'image/png'}:{ok:false,error:'expired provider URL'}},
|
||||||
'@/lib/storage/canvas-zip-delivery':{publishCanvasZip:async()=>null},
|
'@/lib/storage/canvas-zip-delivery':{publishCanvasZip:async()=>null},
|
||||||
@ -111,7 +108,7 @@ test("the browser polls a complete ZIP, preserves an idempotent task after a los
|
|||||||
test("partial source failures are explicit; CDN redirects retain an authenticated local range fallback",async()=>{
|
test("partial source failures are explicit; CDN redirects retain an authenticated local range fallback",async()=>{
|
||||||
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-partial-'));const callbacks:Array<()=>Promise<void>>=[];let owner='owner';
|
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-partial-'));const callbacks:Array<()=>Promise<void>>=[];let owner='owner';
|
||||||
const route=serverLoader({
|
const route=serverLoader({
|
||||||
'@/lib/observability/active-work':runtimeWork,'@/app/(auth)/auth':{auth:async()=>({user:{id:owner}})},
|
'@/app/(auth)/auth':{auth:async()=>({user:{id:owner}})},
|
||||||
'next/server':{NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
'next/server':{NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
||||||
'@/lib/canvas/photoshop-safe-download':{fetchCanvasDownloadAsset:async({index}:any)=>index?{ok:false,error:'source expired'}:{ok:true,filename:'original.png',bytes:Buffer.from('original'),contentType:'image/png'}},
|
'@/lib/canvas/photoshop-safe-download':{fetchCanvasDownloadAsset:async({index}:any)=>index?{ok:false,error:'source expired'}:{ok:true,filename:'original.png',bytes:Buffer.from('original'),contentType:'image/png'}},
|
||||||
'@/lib/storage/canvas-zip-delivery':{publishCanvasZip:async()=>({url:'https://cdn.example/complete.zip',key:'canvas-downloads/x.zip',storageConfigId:'config'})},
|
'@/lib/storage/canvas-zip-delivery':{publishCanvasZip:async()=>({url:'https://cdn.example/complete.zip',key:'canvas-downloads/x.zip',storageConfigId:'config'})},
|
||||||
@ -131,7 +128,7 @@ test("partial source failures are explicit; CDN redirects retain an authenticate
|
|||||||
test("recovery validates staged files instead of treating missing bytes as successful images",async()=>{
|
test("recovery validates staged files instead of treating missing bytes as successful images",async()=>{
|
||||||
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-missing-'));const callbacks:Array<()=>Promise<void>>=[];const id=randomUUID();
|
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-missing-'));const callbacks:Array<()=>Promise<void>>=[];const id=randomUUID();
|
||||||
const load=serverLoader({
|
const load=serverLoader({
|
||||||
'@/lib/observability/active-work':runtimeWork,'@/app/(auth)/auth':{auth:async()=>({user:{id:'owner'}})},
|
'@/app/(auth)/auth':{auth:async()=>({user:{id:'owner'}})},
|
||||||
'next/server':{NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
'next/server':{NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
||||||
'@/lib/canvas/photoshop-safe-download':{fetchCanvasDownloadAsset:async()=>({ok:false,error:'source expired'})},
|
'@/lib/canvas/photoshop-safe-download':{fetchCanvasDownloadAsset:async()=>({ok:false,error:'source expired'})},
|
||||||
'@/lib/storage/canvas-zip-delivery':{publishCanvasZip:async()=>null},
|
'@/lib/storage/canvas-zip-delivery':{publishCanvasZip:async()=>null},
|
||||||
@ -160,7 +157,7 @@ test("large ZIP writing checks current free space as staged input bytes are rele
|
|||||||
test("slow expired-object cleanup cannot keep the ZIP admission request open",async()=>{
|
test("slow expired-object cleanup cannot keep the ZIP admission request open",async()=>{
|
||||||
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-cleanup-'));const callbacks:Array<()=>Promise<void>>=[];
|
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-cleanup-'));const callbacks:Array<()=>Promise<void>>=[];
|
||||||
const mocks:Record<string,unknown>={
|
const mocks:Record<string,unknown>={
|
||||||
'@/lib/observability/active-work':runtimeWork,'@/app/(auth)/auth':{auth:async()=>({user:{id:'owner'}})},
|
'@/app/(auth)/auth':{auth:async()=>({user:{id:'owner'}})},
|
||||||
'next/server':{NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
'next/server':{NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
||||||
'@/lib/canvas/photoshop-safe-download':{},'@/lib/storage/canvas-zip-delivery':{},
|
'@/lib/canvas/photoshop-safe-download':{},'@/lib/storage/canvas-zip-delivery':{},
|
||||||
};
|
};
|
||||||
@ -172,127 +169,3 @@ test("slow expired-object cleanup cannot keep the ZIP admission request open",as
|
|||||||
expect(result).not.toBe('blocked');expect((result as Response).status).toBe(202);
|
expect(result).not.toBe('blocked');expect((result as Response).status).toBe(202);
|
||||||
}finally{await rm(dir,{recursive:true,force:true});}
|
}finally{await rm(dir,{recursive:true,force:true});}
|
||||||
});
|
});
|
||||||
|
|
||||||
test("large ZIPs use bounded parallel parts and publish only the complete ordered archive", async () => {
|
|
||||||
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-parts-'));
|
|
||||||
const bytes=Buffer.alloc(25*1024**2+19,0x5a);bytes[16*1024**2]=0xff;
|
|
||||||
const file=join(dir,'large.zip');await writeFile(file,bytes);
|
|
||||||
const parts=new Map<number,Buffer>();let inFlight=0;let peak=0;let finished=false;let destroyed=false;let trailers='';
|
|
||||||
const progress:number[]=[];
|
|
||||||
const client={config:{requestChecksumCalculation:async()=>''},destroy(){destroyed=true;},async send(command:any){
|
|
||||||
if(command instanceof HeadObjectCommand)throw Object.assign(new Error('missing'),{$metadata:{httpStatusCode:404}});
|
|
||||||
if(command instanceof CreateMultipartUploadCommand){expect(command.input.ContentDisposition).toContain('attachment');return {UploadId:'upload'};}
|
|
||||||
if(command instanceof UploadPartCommand){
|
|
||||||
const input=command.input;inFlight++;peak=Math.max(peak,inFlight);expect(input.ContentLength).toBeLessThanOrEqual(8*1024**2);
|
|
||||||
await new Promise(resolve=>setTimeout(resolve,input.PartNumber===1?30:20));
|
|
||||||
expect(Buffer.isBuffer(input.Body)).toBe(true);
|
|
||||||
parts.set(input.PartNumber!,Buffer.from(input.Body as Buffer));inFlight--;return {ETag:`part-${input.PartNumber}`};
|
|
||||||
}
|
|
||||||
expect(command).toBeInstanceOf(CompleteMultipartUploadCommand);
|
|
||||||
expect(command.input.MultipartUpload.Parts.map((p:any)=>p.PartNumber)).toEqual([1,2,3,4]);
|
|
||||||
expect(Buffer.concat(command.input.MultipartUpload.Parts.map((p:any)=>parts.get(p.PartNumber)!)).equals(bytes)).toBe(true);
|
|
||||||
finished=true;trailers=await client.config.requestChecksumCalculation();return {};
|
|
||||||
}};
|
|
||||||
const publish=serverLoader({
|
|
||||||
'@/lib/db/queries':{getDefaultStorageConfig:async()=>({id:'storage'})},
|
|
||||||
'./service':{toDbS3UploadConfig:()=>({provider:'qiniu-kodo',bucket:'bucket',publicUrl:'https://cdn.example'}),createS3Client:()=>client,buildS3PublicUrl:(_:any,key:string)=>'https://cdn.example/'+key},
|
|
||||||
fetch:async(url:string,init:any)=>{expect(finished).toBe(true);expect(init.method).toBe('HEAD');expect(new URL(url).searchParams.get('v')).toBe(String(bytes.length));return new Response(null,{headers:{'content-length':String(bytes.length)}});},
|
|
||||||
})('lib/storage/canvas-zip-delivery.ts').publishCanvasZip;
|
|
||||||
try{
|
|
||||||
const result=await publish({id:randomUUID(),path:file,bytes:bytes.length,disposition:'attachment; filename="large.zip"',onProgress:async(n:number)=>{progress.push(n);}});
|
|
||||||
expect(result.url).toContain('https://cdn.example/canvas-downloads/');expect(peak).toBeGreaterThan(1);expect(peak).toBeLessThanOrEqual(3);expect(progress.at(-1)).toBe(bytes.length);
|
|
||||||
expect(progress).toEqual([...progress].sort((a,b)=>a-b));expect(trailers).toBe('WHEN_REQUIRED');expect(destroyed).toBe(true);
|
|
||||||
}finally{await rm(dir,{recursive:true,force:true});}
|
|
||||||
});
|
|
||||||
|
|
||||||
test("failed multipart uploads abort their parts and retain the original ZIP with actionable advice",async()=>{
|
|
||||||
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-abort-'));const file=join(dir,'large.zip');const bytes=17*1024**2;
|
|
||||||
const handle=await (await import('node:fs/promises')).open(file,'w');await handle.truncate(bytes);await handle.close();
|
|
||||||
let aborted=false;let completed=false;
|
|
||||||
const client={config:{},destroy(){},async send(command:any){
|
|
||||||
if(command instanceof HeadObjectCommand)throw Object.assign(new Error('missing'),{$metadata:{httpStatusCode:404}});
|
|
||||||
if(command instanceof CreateMultipartUploadCommand)return {UploadId:'upload'};
|
|
||||||
if(command instanceof UploadPartCommand){if(command.input.PartNumber===2)throw new DOMException('part timeout','TimeoutError');return {ETag:'part'};}
|
|
||||||
if(command instanceof AbortMultipartUploadCommand){aborted=true;return {};}
|
|
||||||
completed=true;return {};
|
|
||||||
}};
|
|
||||||
const publish=serverLoader({
|
|
||||||
'@/lib/db/queries':{getDefaultStorageConfig:async()=>({id:'storage'})},
|
|
||||||
'./service':{toDbS3UploadConfig:()=>({provider:'qiniu-kodo',bucket:'bucket',publicUrl:'https://cdn.example'}),createS3Client:()=>client},
|
|
||||||
})('lib/storage/canvas-zip-delivery.ts').publishCanvasZip;
|
|
||||||
try{
|
|
||||||
const result=await publish({id:randomUUID(),path:file,bytes,disposition:'attachment'});
|
|
||||||
expect(result.url).toBeNull();expect(result.error).toContain('传送至七牛时超时');expect(result.error).toContain('继续传送原 ZIP');
|
|
||||||
expect(aborted).toBe(true);expect(completed).toBe(false);expect((await (await import('node:fs/promises')).stat(file)).size).toBe(bytes);
|
|
||||||
}finally{await rm(dir,{recursive:true,force:true});}
|
|
||||||
});
|
|
||||||
|
|
||||||
test("retrying CDN verification reuses an already uploaded ZIP without uploading it again",async()=>{
|
|
||||||
let uploads=0;let available=false;
|
|
||||||
const client={config:{},destroy(){},async send(command:any){
|
|
||||||
if(command instanceof HeadObjectCommand)return {ContentLength:100};
|
|
||||||
if(command instanceof PutObjectCommand||command instanceof CreateMultipartUploadCommand)uploads++;
|
|
||||||
throw new Error('Unexpected upload');
|
|
||||||
}};
|
|
||||||
const publish=serverLoader({
|
|
||||||
'@/lib/db/queries':{getDefaultStorageConfig:async()=>({id:'storage'})},
|
|
||||||
'./service':{toDbS3UploadConfig:()=>({provider:'qiniu-kodo',bucket:'bucket',publicUrl:'https://cdn.example'}),createS3Client:()=>client,buildS3PublicUrl:(_:any,key:string)=>'https://cdn.example/'+key},
|
|
||||||
fetch:async()=>new Response(null,{status:available?200:404,headers:{'content-length':available?'100':'0'}}),
|
|
||||||
})('lib/storage/canvas-zip-delivery.ts').publishCanvasZip;
|
|
||||||
const params={id:randomUUID(),path:'unused-already-published.zip',bytes:100,disposition:'attachment'};
|
|
||||||
const first=await publish(params);expect(first.url).toBeNull();expect(first.error).toContain('七牛下载链接尚未可用');
|
|
||||||
available=true;expect((await publish(params)).url).toContain('https://cdn.example/');expect(uploads).toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("CDN failures never silently start local downloads; retries reuse the ZIP and keep it resumable beyond one hour",async()=>{
|
|
||||||
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-cdn-retry-'));const callbacks:Array<()=>Promise<void>>=[];
|
|
||||||
let fetches=0;let publications=0;let available=false;let now=Date.now();
|
|
||||||
let activeWork=0;
|
|
||||||
const runtimeWork={trackRuntimeWork:async(run:()=>Promise<void>)=>{activeWork++;try{return await run();}finally{activeWork--;}}};
|
|
||||||
class Clock extends Date{static now(){return now;}}
|
|
||||||
const load=serverLoader({
|
|
||||||
Date:Clock,'@/lib/observability/active-work':runtimeWork,'@/app/(auth)/auth':{auth:async()=>({user:{id:'owner'}})},
|
|
||||||
'next/server':{NextResponse,after:(run:()=>Promise<void>)=>callbacks.push(run)},
|
|
||||||
'@/lib/canvas/photoshop-safe-download':{fetchCanvasDownloadAsset:async()=>{fetches++;return {ok:true,filename:'original.png',bytes:Buffer.from('original'),contentType:'image/png'};}},
|
|
||||||
'@/lib/storage/canvas-zip-delivery':{publishCanvasZip:async()=>{expect(activeWork).toBe(1);publications++;return {url:available?'https://cdn.example/complete.zip':null,key:'canvas-downloads/key.zip',storageConfigId:'storage',error:'ZIP 已打包,传送至七牛时超时;请再次点击下载继续原 ZIP,不会重新生成图片。'};},removeCanvasZipObject:async()=>{}},
|
|
||||||
},{CANVAS_DOWNLOAD_ZIP_DIR:dir});
|
|
||||||
const route=load('app/api/canvas/download-zip-task/route.ts');const id=randomUUID();
|
|
||||||
const post=()=>new NextRequest('http://localhost/api/canvas/download-zip-task',{method:'POST',body:JSON.stringify({taskId:id,retry:true,files:[{url:'https://cdn.example/original.png'}]})});
|
|
||||||
const request=(suffix='')=>new NextRequest(`http://localhost/api/canvas/download-zip-task?taskId=${id}${suffix}`,{headers:{range:'bytes=0-3'}});
|
|
||||||
try{
|
|
||||||
await route.POST(post());while(callbacks.length)await callbacks.shift()!();
|
|
||||||
const failed=(await(await route.GET(request())).json()).task;
|
|
||||||
expect(failed).toMatchObject({id,status:'failed',successCount:1,downloadUrl:''});expect(failed.error).toContain('不会重新生成图片');
|
|
||||||
expect((await route.GET(request('&download=1'))).status).toBe(409);
|
|
||||||
const before=await readFile(join(dir,id,'task.json'),'utf8');const original=Buffer.from(await readFile(JSON.parse(before).zipFile.path));
|
|
||||||
available=true;await route.POST(post());while(callbacks.length)await callbacks.shift()!();
|
|
||||||
expect(fetches).toBe(1);expect(publications).toBe(2);
|
|
||||||
const manifest=JSON.parse(await readFile(join(dir,id,'task.json'),'utf8'));expect(await readFile(manifest.zipFile.path)).toEqual(original);
|
|
||||||
delete manifest.cdnUrl;
|
|
||||||
await load('lib/canvas/download-zip-task-store.ts').saveZipTask(manifest);
|
|
||||||
await route.POST(post());while(callbacks.length)await callbacks.shift()!();
|
|
||||||
expect(fetches).toBe(1);expect(publications).toBe(3);
|
|
||||||
expect(activeWork).toBe(0);
|
|
||||||
now+=2*60*60_000;
|
|
||||||
expect((await route.GET(request('&download=1'))).status).toBe(307);
|
|
||||||
const local=await route.GET(request('&download=1&local=1'));expect(local.status).toBe(206);expect(Buffer.from(await local.arrayBuffer())).toEqual(original.subarray(0,4));
|
|
||||||
}finally{await rm(dir,{recursive:true,force:true});}
|
|
||||||
});
|
|
||||||
|
|
||||||
test("small ZIP delivery preserves the complete file and download filename",async()=>{
|
|
||||||
const dir=await mkdtemp(join(tmpdir(),'canvas-zip-small-'));const file=join(dir,'small.zip');const bytes=Buffer.from('PK small complete archive');await writeFile(file,bytes);
|
|
||||||
let written=false;
|
|
||||||
const client={config:{},destroy(){},async send(command:any){
|
|
||||||
if(command instanceof HeadObjectCommand)throw Object.assign(new Error('missing'),{$metadata:{httpStatusCode:404}});
|
|
||||||
expect(command).toBeInstanceOf(PutObjectCommand);expect(command.input.ContentLength).toBe(bytes.length);expect(command.input.ContentDisposition).toContain('small.zip');
|
|
||||||
const chunks:Buffer[]=[];for await(const chunk of command.input.Body)chunks.push(Buffer.from(chunk));
|
|
||||||
expect(Buffer.concat(chunks).equals(bytes)).toBe(true);written=true;return {};
|
|
||||||
}};
|
|
||||||
const publish=serverLoader({
|
|
||||||
'@/lib/db/queries':{getDefaultStorageConfig:async()=>({id:'storage'})},
|
|
||||||
'./service':{toDbS3UploadConfig:()=>({provider:'qiniu-kodo',bucket:'bucket',publicUrl:'https://cdn.example'}),createS3Client:()=>client,buildS3PublicUrl:(_:any,key:string)=>'https://cdn.example/'+key},
|
|
||||||
fetch:async()=>{expect(written).toBe(true);return new Response(null,{headers:{'content-length':String(bytes.length)}});},
|
|
||||||
})('lib/storage/canvas-zip-delivery.ts').publishCanvasZip;
|
|
||||||
try{expect((await publish({id:randomUUID(),path:file,bytes:bytes.length,disposition:'attachment; filename="small.zip"'})).url).toContain('https://cdn.example/');}
|
|
||||||
finally{await rm(dir,{recursive:true,force:true});}
|
|
||||||
});
|
|
||||||
|
|||||||
@ -224,7 +224,7 @@ test("bucket failure retains the provider URL and retry task", async () => {
|
|||||||
let stored = 0;
|
let stored = 0;
|
||||||
let replaced = 0;
|
let replaced = 0;
|
||||||
const module = serverLoader({
|
const module = serverLoader({
|
||||||
"@/lib/storage/image-download-preparation": { scheduleImageDownloadPreparation() {} },
|
"@/lib/storage/image-download-preparation": { preparePendingImageDownloads: async () => 0 },
|
||||||
"@/lib/storage/image-archive": {
|
"@/lib/storage/image-archive": {
|
||||||
archiveTemporaryImage: async () => {
|
archiveTemporaryImage: async () => {
|
||||||
throw new Error("Bucket unavailable");
|
throw new Error("Bucket unavailable");
|
||||||
@ -414,7 +414,7 @@ test("successful background archive replaces saved references immediately", asyn
|
|||||||
let completed = false;
|
let completed = false;
|
||||||
const calls: string[][] = [];
|
const calls: string[][] = [];
|
||||||
const module = serverLoader({
|
const module = serverLoader({
|
||||||
"@/lib/storage/image-download-preparation": { scheduleImageDownloadPreparation() {} },
|
"@/lib/storage/image-download-preparation": { preparePendingImageDownloads: async () => 0 },
|
||||||
"@/lib/storage/image-archive": {
|
"@/lib/storage/image-archive": {
|
||||||
archiveTemporaryImage: async () => ({ url: permanent, bytes: png.length }),
|
archiveTemporaryImage: async () => ({ url: permanent, bytes: png.length }),
|
||||||
},
|
},
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user