Add batch instruction editing #111
70
app/api/canvas/batch-instruction/route.ts
Normal file
70
app/api/canvas/batch-instruction/route.ts
Normal file
@ -0,0 +1,70 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { auth } from "@/app/(auth)/auth";
|
||||
import { createCanvasImageTask, enqueueCanvasImageTask } from "@/lib/canvas/image-task-queue";
|
||||
import { getSystemSetting } from "@/lib/db/queries";
|
||||
import type { CanvasGenerateImageRequest } from "@/lib/canvas/generate-image";
|
||||
|
||||
const imageUrl = z.string().trim().max(8192).refine(value => {
|
||||
try {
|
||||
const url = new URL(value);
|
||||
return (url.protocol === "https:" || url.protocol === "http:") &&
|
||||
!url.username && !url.password;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
const submission = z.object({
|
||||
taskId: z.string().uuid(),
|
||||
image: imageUrl,
|
||||
reference: imageUrl.optional(),
|
||||
instruction: z.string().trim().min(1).max(3000),
|
||||
square: z.boolean(),
|
||||
});
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) return NextResponse.json({ error: "未登录" }, { status: 401 });
|
||||
const parsed = submission.safeParse(await request.json().catch(() => null));
|
||||
if (!parsed.success) return NextResponse.json({ error: "请提供原图和 1 至 3000 字的编辑指令" }, { status: 400 });
|
||||
|
||||
const { taskId, image, reference, instruction, square } = parsed.data;
|
||||
if (reference === image) return NextResponse.json({ error: "共用参考图和待编辑原图相同" }, { status: 400 });
|
||||
|
||||
try {
|
||||
const setting = await getSystemSetting({ key: "scene_fusion_config" });
|
||||
if ((setting?.value as { enabled?: boolean } | null)?.enabled === false) {
|
||||
return NextResponse.json({ error: "批量处理功能已被管理员禁用" }, { status: 403 });
|
||||
}
|
||||
|
||||
const referenceImages = reference ? [image, reference] : [image];
|
||||
const imageRequest: CanvasGenerateImageRequest = {
|
||||
prompt: [
|
||||
"编辑第一张图。保留第一张图的完整构图、原始宽高比和所有未要求修改的内容。",
|
||||
reference && "第二张图只提供指令指定的人脸、商品或其他特征。以第一张图为底图,保持第一张图中其余人物、姿势、衣着、商品、背景和光线,除非指令明确要求更改。",
|
||||
"保持原图整幅画面的四边,避免裁切或拉伸。只输出一张编辑后的图片。",
|
||||
"编辑指令:" + instruction,
|
||||
].filter(Boolean).join("\n"),
|
||||
displayPrompt: instruction,
|
||||
aspectRatio: "auto",
|
||||
imageCount: 1,
|
||||
referenceImages,
|
||||
requiredReferenceImageCount: referenceImages.length,
|
||||
agentImageMode: "auto",
|
||||
promptExtend: false,
|
||||
padBatchInstructionToSquare: square || undefined,
|
||||
debugLabel: "batch-instruction-edit",
|
||||
};
|
||||
const task = await createCanvasImageTask({
|
||||
userId: session.user.id,
|
||||
request: { ...imageRequest },
|
||||
taskId,
|
||||
});
|
||||
if (task.status === "pending") await enqueueCanvasImageTask(task.id);
|
||||
return NextResponse.json({ taskId: task.id }, { status: 202 });
|
||||
} catch (error) {
|
||||
console.error("[BatchInstruction] Task submission failed", { taskId, error });
|
||||
return NextResponse.json({ error: "暂时无法确认是否提交成功,请查询原任务" }, { status: 503 });
|
||||
}
|
||||
}
|
||||
@ -154,7 +154,7 @@ export async function POST(request: NextRequest) {
|
||||
await mkdir(zipTaskPath(id), { mode: 0o700 });
|
||||
});
|
||||
const now = Date.now();
|
||||
const remote = body.scope === "style-detail" &&
|
||||
const remote = (body.scope === "style-detail" || body.scope === "batch-instruction") &&
|
||||
process.env.CANVAS_ZIP_WORKER_URL && process.env.CANVAS_ZIP_WORKER_TOKEN
|
||||
? { attempt: 0 } : undefined;
|
||||
task = { id, userId: session.user.id, folderName: sanitizeFolderName(String(body.folderName || "")), files, remote,
|
||||
|
||||
@ -33,6 +33,9 @@ import {
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { runBatchInstructionEdit, BatchInstructionEditError } from "@/lib/client/batch-instruction-edit";
|
||||
import { downloadCanvasSelectionZip } from "@/lib/client/canvas-zip-download";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||||
import {
|
||||
@ -59,6 +62,7 @@ import type { AgentWorkflowProductDetailChannel } from "@/types/agent";
|
||||
|
||||
const fetcher = (url: string) => fetch(url).then((res) => res.json());
|
||||
const MAX_BATCH_ITEMS = 10;
|
||||
const imageFileAccept = "image/png,image/jpeg,image/webp,image/heic,image/heif,.heic,.heif";
|
||||
const BATCH_REQUEST_STAGGER_MS = 700;
|
||||
const BATCH_PARALLEL_WORKER_COUNT = 4;
|
||||
const MULTI_PRODUCT_FAILOVER_MAX_ROUNDS = 5;
|
||||
@ -100,6 +104,7 @@ function formatQueueWaitLabel(estimatedWaitMs?: number | null) {
|
||||
type BatchMode =
|
||||
| "scene-fission"
|
||||
| "multi-product-scene"
|
||||
| "instruction-edit"
|
||||
| "random-bg"
|
||||
| "rmbg"
|
||||
| "enhance"
|
||||
@ -116,6 +121,8 @@ interface ProcessItem {
|
||||
error?: string;
|
||||
targetName?: string;
|
||||
customPrompt?: string;
|
||||
instructionTaskId?: string;
|
||||
instructionSubmitted?: boolean;
|
||||
queueStatus?: {
|
||||
queuePosition?: number;
|
||||
sameModelPendingCount?: number;
|
||||
@ -164,6 +171,10 @@ type SceneFusionPersistedState = {
|
||||
randomCount: number;
|
||||
randomPrompt: string;
|
||||
batchItems: ProcessItem[];
|
||||
instructionItems: ProcessItem[];
|
||||
instructionText: string;
|
||||
instructionReference: string | null;
|
||||
instructionSquare: boolean;
|
||||
selectedResultIds: string[];
|
||||
};
|
||||
|
||||
@ -256,8 +267,9 @@ function createPersistableSnapshot(snapshot: SceneFusionPersistedState): SceneFu
|
||||
const multiProductItems = toPersistableItems(snapshot.multiProductItems);
|
||||
const randomItems = toPersistableItems(snapshot.randomItems);
|
||||
const batchItems = toPersistableItems(snapshot.batchItems);
|
||||
const instructionItems = toPersistableItems(snapshot.instructionItems);
|
||||
const persistedIds = new Set(
|
||||
[...sceneItems, ...multiProductItems, ...randomItems, ...batchItems].map(
|
||||
[...sceneItems, ...multiProductItems, ...randomItems, ...batchItems, ...instructionItems].map(
|
||||
(item) => item.id
|
||||
)
|
||||
);
|
||||
@ -271,6 +283,8 @@ function createPersistableSnapshot(snapshot: SceneFusionPersistedState): SceneFu
|
||||
toPersistableImageValue(snapshot.referenceSceneImage) || null,
|
||||
randomItems,
|
||||
batchItems,
|
||||
instructionItems,
|
||||
instructionReference: toPersistableImageValue(snapshot.instructionReference) || null,
|
||||
selectedResultIds: snapshot.selectedResultIds.filter((id) =>
|
||||
persistedIds.has(id)
|
||||
),
|
||||
@ -325,6 +339,12 @@ const modeOptions: Array<{
|
||||
description: "10张产品图 + 1张参考图",
|
||||
icon: ImageIcon,
|
||||
},
|
||||
{
|
||||
id: "instruction-edit",
|
||||
label: "批量指令编辑",
|
||||
description: "多张原图执行同一条指令",
|
||||
icon: Wand2,
|
||||
},
|
||||
{
|
||||
id: "random-bg",
|
||||
label: "随机换背景",
|
||||
@ -363,6 +383,8 @@ function getModeActionLabel(mode: BatchMode, count: number) {
|
||||
return `开始裂变 (${count} 张)`;
|
||||
case "multi-product-scene":
|
||||
return `开始多产品替换 (${count} 张)`;
|
||||
case "instruction-edit":
|
||||
return `开始编辑 (${count} 张)`;
|
||||
case "random-bg":
|
||||
return `开始换背景 (${count} 张)`;
|
||||
case "rmbg":
|
||||
@ -510,6 +532,16 @@ export function SceneFusionView({
|
||||
const [randomPrompt, setRandomPrompt] = useState("");
|
||||
|
||||
const [batchItems, setBatchItems] = useState<ProcessItem[]>([]);
|
||||
const [instructionItems, setInstructionItems] = useState<ProcessItem[]>([]);
|
||||
const [instructionText, setInstructionText] = useState("");
|
||||
const [instructionReference, setInstructionReference] = useState<string | null>(null);
|
||||
const [instructionSquare, setInstructionSquare] = useState(false);
|
||||
const [isInstructionUploading, setIsInstructionUploading] = useState(false);
|
||||
const [zipStage, setZipStage] = useState("");
|
||||
const [isZipping, setIsZipping] = useState(false);
|
||||
const isInstructionMode = mode === "instruction-edit";
|
||||
const activeBatchItems = isInstructionMode ? instructionItems : batchItems;
|
||||
const setActiveBatchItems = isInstructionMode ? setInstructionItems : setBatchItems;
|
||||
const [previewImageUrl, setPreviewImageUrl] = useState<string | null>(null);
|
||||
const [previewImageTitle, setPreviewImageTitle] =
|
||||
useState<string>("处理结果预览");
|
||||
@ -522,14 +554,15 @@ export function SceneFusionView({
|
||||
const multiProductInputRef = useRef<HTMLInputElement>(null);
|
||||
const referenceSceneInputRef = useRef<HTMLInputElement>(null);
|
||||
const batchInputRef = useRef<HTMLInputElement>(null);
|
||||
const instructionReferenceInputRef = useRef<HTMLInputElement>(null);
|
||||
const productPreviewObjectUrlRef = useRef<string | null>(null);
|
||||
|
||||
const resultItems = useMemo(() => {
|
||||
if (mode === "scene-fission") return sceneItems;
|
||||
if (mode === "multi-product-scene") return multiProductItems;
|
||||
if (mode === "random-bg") return randomItems;
|
||||
return batchItems;
|
||||
}, [batchItems, mode, multiProductItems, randomItems, sceneItems]);
|
||||
return activeBatchItems;
|
||||
}, [activeBatchItems, mode, multiProductItems, randomItems, sceneItems]);
|
||||
const resultQueueMetaById = useMemo(
|
||||
() =>
|
||||
new Map(
|
||||
@ -547,9 +580,18 @@ export function SceneFusionView({
|
||||
if (mode === "multi-product-scene")
|
||||
return multiProductItems.length > 0 && Boolean(referenceSceneImage);
|
||||
if (mode === "random-bg") return Boolean(productImage) && randomCount > 0;
|
||||
if (isInstructionMode) {
|
||||
return config?.enabled !== false && !isInstructionUploading && instructionItems.length > 0 &&
|
||||
Boolean(instructionText.trim() || instructionItems.every(item => item.instructionSubmitted));
|
||||
}
|
||||
return batchItems.length > 0;
|
||||
}, [
|
||||
batchItems.length,
|
||||
config?.enabled,
|
||||
instructionItems,
|
||||
instructionText,
|
||||
isInstructionMode,
|
||||
isInstructionUploading,
|
||||
mode,
|
||||
multiProductItems.length,
|
||||
productImage,
|
||||
@ -745,6 +787,12 @@ export function SceneFusionView({
|
||||
if (Array.isArray(parsed.batchItems)) {
|
||||
setBatchItems(toPersistableItems(parsed.batchItems));
|
||||
}
|
||||
if (Array.isArray(parsed.instructionItems)) {
|
||||
setInstructionItems(toPersistableItems(parsed.instructionItems));
|
||||
}
|
||||
if (typeof parsed.instructionText === "string") setInstructionText(parsed.instructionText);
|
||||
setInstructionReference(toPersistableImageValue(parsed.instructionReference) || null);
|
||||
setInstructionSquare(parsed.instructionSquare === true);
|
||||
if (Array.isArray(parsed.selectedResultIds)) {
|
||||
setSelectedResultIds(parsed.selectedResultIds.map((value) => String(value || "").trim()).filter(Boolean));
|
||||
}
|
||||
@ -835,6 +883,10 @@ export function SceneFusionView({
|
||||
randomCount,
|
||||
randomPrompt,
|
||||
batchItems,
|
||||
instructionItems,
|
||||
instructionText,
|
||||
instructionReference,
|
||||
instructionSquare,
|
||||
selectedResultIds,
|
||||
};
|
||||
|
||||
@ -854,6 +906,10 @@ export function SceneFusionView({
|
||||
}, [
|
||||
aspectRatio,
|
||||
batchItems,
|
||||
instructionItems,
|
||||
instructionText,
|
||||
instructionReference,
|
||||
instructionSquare,
|
||||
imageSize,
|
||||
mode,
|
||||
multiProductItems,
|
||||
@ -1099,6 +1155,21 @@ export function SceneFusionView({
|
||||
}
|
||||
};
|
||||
|
||||
const handleInstructionReferenceUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const input = event.currentTarget;
|
||||
const [file] = validateImageFiles(Array.from(input.files || []));
|
||||
input.value = "";
|
||||
if (!file) return;
|
||||
setIsInstructionUploading(true);
|
||||
try {
|
||||
setInstructionReference(await uploadImageFile(file));
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "参考图上传失败");
|
||||
} finally {
|
||||
setIsInstructionUploading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleBatchUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const input = e.currentTarget;
|
||||
const files = validateImageFiles(Array.from(input.files || []));
|
||||
@ -1106,15 +1177,16 @@ export function SceneFusionView({
|
||||
input.value = "";
|
||||
return;
|
||||
}
|
||||
if (batchItems.length + files.length > MAX_BATCH_ITEMS) {
|
||||
if (activeBatchItems.length + files.length > MAX_BATCH_ITEMS) {
|
||||
toast.error(`单次最多上传 ${MAX_BATCH_ITEMS} 张图片`);
|
||||
input.value = "";
|
||||
return;
|
||||
}
|
||||
|
||||
if (isInstructionMode) setIsInstructionUploading(true);
|
||||
try {
|
||||
const uploadedItems = await uploadImageFiles(files);
|
||||
setBatchItems((prev) => [
|
||||
setActiveBatchItems((prev) => [
|
||||
...prev,
|
||||
...uploadedItems.map((item) => ({
|
||||
id: Math.random().toString(36).slice(2, 10),
|
||||
@ -1126,6 +1198,7 @@ export function SceneFusionView({
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "图片上传失败");
|
||||
} finally {
|
||||
if (isInstructionMode) setIsInstructionUploading(false);
|
||||
input.value = "";
|
||||
}
|
||||
};
|
||||
@ -1145,7 +1218,7 @@ export function SceneFusionView({
|
||||
};
|
||||
|
||||
const removeBatchItem = (id: string) => {
|
||||
setBatchItems((prev) => prev.filter((item) => item.id !== id));
|
||||
setActiveBatchItems((prev) => prev.filter((item) => item.id !== id));
|
||||
};
|
||||
|
||||
const updateBatchItemsStatus = (ids: string[], status: ItemStatus) => {
|
||||
@ -1746,6 +1819,33 @@ export function SceneFusionView({
|
||||
};
|
||||
|
||||
const processBatchImageItem = async (item: ProcessItem) => {
|
||||
if (isInstructionMode) {
|
||||
if (!item.instructionSubmitted && !instructionText.trim()) {
|
||||
throw new Error("请填写统一编辑指令");
|
||||
}
|
||||
const taskId = item.instructionTaskId || crypto.randomUUID();
|
||||
const updateTask = (patch: Partial<ProcessItem>) => setInstructionItems(items =>
|
||||
items.map(current => current.id === item.id ? { ...current, ...patch } : current)
|
||||
);
|
||||
updateTask({ instructionTaskId: taskId });
|
||||
try {
|
||||
const result = await runBatchInstructionEdit({
|
||||
taskId,
|
||||
image: new URL(item.image, window.location.origin).href,
|
||||
reference: instructionReference ? new URL(instructionReference, window.location.origin).href : undefined,
|
||||
instruction: instructionText.trim(),
|
||||
square: instructionSquare,
|
||||
submitted: item.instructionSubmitted,
|
||||
}, () => updateTask({ instructionSubmitted: true }));
|
||||
updateTask({ instructionTaskId: undefined, instructionSubmitted: undefined });
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (error instanceof BatchInstructionEditError && error.canStartNewTask) {
|
||||
updateTask({ instructionTaskId: undefined, instructionSubmitted: undefined });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
if (mode === "rmbg") {
|
||||
const response = await fetch("/api/rmbg", {
|
||||
method: "POST",
|
||||
@ -1828,16 +1928,16 @@ export function SceneFusionView({
|
||||
};
|
||||
|
||||
const runBatchImageMode = async () => {
|
||||
if (batchItems.length === 0) {
|
||||
if (activeBatchItems.length === 0) {
|
||||
toast.error("请先上传图片");
|
||||
return;
|
||||
}
|
||||
|
||||
let itemsToProcess = batchItems.filter(
|
||||
(item) => item.status === "pending" || item.status === "failed"
|
||||
let itemsToProcess = activeBatchItems.filter(
|
||||
(item) => item.status === "pending" || item.status === "failed" || (isInstructionMode && item.status === "processing")
|
||||
);
|
||||
if (itemsToProcess.length === 0) {
|
||||
itemsToProcess = batchItems;
|
||||
itemsToProcess = activeBatchItems;
|
||||
}
|
||||
|
||||
if (itemsToProcess.length > MAX_BATCH_ITEMS) {
|
||||
@ -1846,7 +1946,7 @@ export function SceneFusionView({
|
||||
}
|
||||
|
||||
setIsProcessing(true);
|
||||
setBatchItems((prev) =>
|
||||
setActiveBatchItems((prev) =>
|
||||
prev.map((item) =>
|
||||
itemsToProcess.some((target) => target.id === item.id)
|
||||
? markProcessItemRegenerating(item, "pending")
|
||||
@ -1866,7 +1966,7 @@ export function SceneFusionView({
|
||||
while (queue.length > 0) {
|
||||
const current = queue.shift();
|
||||
if (!current) break;
|
||||
setBatchItems((prev) =>
|
||||
setActiveBatchItems((prev) =>
|
||||
prev.map((item) =>
|
||||
item.id === current.id
|
||||
? markProcessItemRegenerating(item, "processing")
|
||||
@ -1876,7 +1976,7 @@ export function SceneFusionView({
|
||||
try {
|
||||
const result = await processBatchImageItem(current);
|
||||
successCount += 1;
|
||||
setBatchItems((prev) =>
|
||||
setActiveBatchItems((prev) =>
|
||||
prev.map((item) =>
|
||||
item.id === current.id
|
||||
? {
|
||||
@ -1892,7 +1992,7 @@ export function SceneFusionView({
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "处理失败";
|
||||
failedCount += 1;
|
||||
setBatchItems((prev) =>
|
||||
setActiveBatchItems((prev) =>
|
||||
prev.map((item) =>
|
||||
item.id === current.id
|
||||
? {
|
||||
@ -1928,7 +2028,7 @@ export function SceneFusionView({
|
||||
}
|
||||
|
||||
setIsProcessing(true);
|
||||
setBatchItems((prev) =>
|
||||
setActiveBatchItems((prev) =>
|
||||
prev.map((batchItem) =>
|
||||
batchItem.id === item.id
|
||||
? markProcessItemRegenerating(batchItem, "processing")
|
||||
@ -1938,7 +2038,7 @@ export function SceneFusionView({
|
||||
|
||||
try {
|
||||
const result = await processBatchImageItem(item);
|
||||
setBatchItems((prev) =>
|
||||
setActiveBatchItems((prev) =>
|
||||
prev.map((batchItem) =>
|
||||
batchItem.id === item.id
|
||||
? {
|
||||
@ -1954,7 +2054,7 @@ export function SceneFusionView({
|
||||
toast.success("已重新处理 1 张图片");
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "处理失败";
|
||||
setBatchItems((prev) =>
|
||||
setActiveBatchItems((prev) =>
|
||||
prev.map((batchItem) =>
|
||||
batchItem.id === item.id
|
||||
? { ...batchItem, status: "failed", error: message }
|
||||
@ -2089,6 +2189,30 @@ export function SceneFusionView({
|
||||
toast.error("没有可下载的图片");
|
||||
return;
|
||||
}
|
||||
if (isInstructionMode) {
|
||||
if (isZipping) return;
|
||||
setIsZipping(true);
|
||||
setZipStage("正在准备下载");
|
||||
try {
|
||||
const task = await downloadCanvasSelectionZip({
|
||||
folderName: "批量指令编辑",
|
||||
files: completed.map(item => ({
|
||||
url: getProcessItemSourceUrl(item),
|
||||
name: `编辑结果 ${resultItems.indexOf(item) + 1}`,
|
||||
})),
|
||||
selectionKey: persistenceKey || "batch-instruction",
|
||||
scope: "batch-instruction",
|
||||
onProgress: task => setZipStage(task.stage),
|
||||
});
|
||||
toast.success(task.failedCount ? `已开始下载,${task.failedCount} 张未打包` : "已开始下载 ZIP");
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "无法下载 ZIP");
|
||||
} finally {
|
||||
setIsZipping(false);
|
||||
setZipStage("");
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < completed.length; i += 1) {
|
||||
const item = completed[i]!;
|
||||
await downloadImage(
|
||||
@ -2220,7 +2344,7 @@ export function SceneFusionView({
|
||||
<div className="space-y-2">
|
||||
<Label className="font-medium text-base">处理模式</Label>
|
||||
<Select
|
||||
disabled={isProcessing}
|
||||
disabled={isProcessing || isInstructionUploading}
|
||||
onValueChange={(value) => setMode(value as BatchMode)}
|
||||
value={mode}
|
||||
>
|
||||
@ -2823,19 +2947,20 @@ export function SceneFusionView({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{(mode === "rmbg" ||
|
||||
{(isInstructionMode ||
|
||||
mode === "rmbg" ||
|
||||
mode === "enhance" ||
|
||||
mode === "extend" ||
|
||||
mode === "watermark") && (
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label className="font-medium text-base">
|
||||
待处理图片 ({batchItems.length}/{MAX_BATCH_ITEMS})
|
||||
待处理图片 ({activeBatchItems.length}/{MAX_BATCH_ITEMS})
|
||||
</Label>
|
||||
<Button
|
||||
className="h-7 gap-1 text-xs"
|
||||
disabled={
|
||||
batchItems.length >= MAX_BATCH_ITEMS || isProcessing
|
||||
activeBatchItems.length >= MAX_BATCH_ITEMS || isProcessing || isInstructionUploading
|
||||
}
|
||||
onClick={() => batchInputRef.current?.click()}
|
||||
size="sm"
|
||||
@ -2846,6 +2971,7 @@ export function SceneFusionView({
|
||||
<input
|
||||
accept="image/png,image/jpeg,image/webp,image/heic,image/heif,.heic,.heif"
|
||||
className="hidden"
|
||||
aria-label="上传待处理图片"
|
||||
multiple
|
||||
onChange={handleBatchUpload}
|
||||
ref={batchInputRef}
|
||||
@ -2854,10 +2980,10 @@ export function SceneFusionView({
|
||||
</div>
|
||||
|
||||
<div className="space-y-3">
|
||||
{batchItems.length === 0 && (
|
||||
{activeBatchItems.length === 0 && (
|
||||
<div
|
||||
className="cursor-pointer rounded-lg border-2 border-muted-foreground/25 border-dashed p-8 text-center transition-colors hover:bg-muted/50"
|
||||
onClick={() => batchInputRef.current?.click()}
|
||||
onClick={() => !isProcessing && !isInstructionUploading && batchInputRef.current?.click()}
|
||||
>
|
||||
<ImageIcon className="mx-auto mb-2 h-8 w-8 opacity-20" />
|
||||
<p className="text-muted-foreground text-sm">
|
||||
@ -2869,7 +2995,7 @@ export function SceneFusionView({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{batchItems.map((item, index) => (
|
||||
{activeBatchItems.map((item, index) => (
|
||||
<div
|
||||
className="group relative rounded-lg border bg-background p-3"
|
||||
key={item.id}
|
||||
@ -2897,7 +3023,7 @@ export function SceneFusionView({
|
||||
: "失败"}
|
||||
</div>
|
||||
{item.error && (
|
||||
<div className="mt-1 line-clamp-2 text-destructive text-xs">
|
||||
<div title={item.error} className="mt-1 line-clamp-2 text-destructive text-xs">
|
||||
{item.error}
|
||||
</div>
|
||||
)}
|
||||
@ -2918,6 +3044,58 @@ export function SceneFusionView({
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isInstructionMode && (
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="batch-edit-instruction">统一编辑指令</Label>
|
||||
<Textarea
|
||||
id="batch-edit-instruction"
|
||||
disabled={isProcessing}
|
||||
maxLength={3000}
|
||||
onChange={(event) => setInstructionText(event.target.value)}
|
||||
placeholder="例如,把商品背景改成白色,保持商品、文字和构图"
|
||||
value={instructionText}
|
||||
rows={4}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label>共用参考图(可选)</Label>
|
||||
<div className="flex items-center gap-2">
|
||||
{instructionReference && (
|
||||
<img alt="共用参考图" className="size-16 rounded-md border bg-muted object-contain" src={instructionReference} />
|
||||
)}
|
||||
<input
|
||||
accept={imageFileAccept}
|
||||
aria-label="上传共用参考图"
|
||||
className="hidden"
|
||||
onChange={handleInstructionReferenceUpload}
|
||||
ref={instructionReferenceInputRef}
|
||||
type="file"
|
||||
/>
|
||||
<Button disabled={isProcessing || isInstructionUploading} onClick={() => instructionReferenceInputRef.current?.click()} size="sm" variant="outline">
|
||||
{instructionReference ? "更换参考图" : "上传参考图"}
|
||||
</Button>
|
||||
{instructionReference && (
|
||||
<Button disabled={isProcessing || isInstructionUploading} onClick={() => setInstructionReference(null)} size="sm" variant="ghost">
|
||||
移除
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
<p className="text-muted-foreground text-xs">换脸或换商品时,在指令里写清参考图要提供的特征。</p>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="batch-edit-ratio">输出比例</Label>
|
||||
<Select disabled={isProcessing} onValueChange={(value) => setInstructionSquare(value === "square")} value={instructionSquare ? "square" : "original"}>
|
||||
<SelectTrigger id="batch-edit-ratio" className="h-10"><SelectValue /></SelectTrigger>
|
||||
<SelectContent className="z-[30000] bg-background">
|
||||
<SelectItem value="original">原图比例</SelectItem>
|
||||
<SelectItem value="square">1:1 · 保留完整画面并补边</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -2995,9 +3173,9 @@ export function SceneFusionView({
|
||||
</Button>
|
||||
)}
|
||||
{completedResultItems.length > 0 && (
|
||||
<Button onClick={downloadAll} size="sm" variant="outline">
|
||||
<Button disabled={isInstructionMode && isZipping} onClick={downloadAll} size="sm" variant="outline">
|
||||
<Download className="mr-2 h-4 w-4" />
|
||||
全部下载
|
||||
{isInstructionMode ? (zipStage || "批量下载 ZIP") : "全部下载"}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
@ -3163,9 +3341,15 @@ export function SceneFusionView({
|
||||
</div>
|
||||
) : item.status === "failed" ? (
|
||||
<div className="relative z-10 px-3 text-center">
|
||||
<p className="line-clamp-2 rounded bg-black/55 px-2 py-1 text-white text-xs">
|
||||
<p title={item.error} className="line-clamp-2 rounded bg-black/55 px-2 py-1 text-white text-xs">
|
||||
{item.error || "处理失败"}
|
||||
</p>
|
||||
{isInstructionMode && (
|
||||
<Button className="mt-2" disabled={isProcessing || isInstructionUploading || (!item.instructionSubmitted && !instructionText.trim())}
|
||||
onClick={(event) => { event.stopPropagation(); void retrySingleItem(item); }} size="sm" variant="secondary">
|
||||
{item.instructionTaskId ? "查询原任务" : "重试这张"}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<div className="relative z-10 max-w-[90%] rounded bg-black/55 px-2.5 py-1.5 text-center text-white text-xs">
|
||||
@ -3225,6 +3409,7 @@ export function SceneFusionView({
|
||||
event.stopPropagation();
|
||||
retrySingleItem(item);
|
||||
}}
|
||||
disabled={isProcessing || (isInstructionMode && (isInstructionUploading || !instructionText.trim()))}
|
||||
size="icon"
|
||||
variant="secondary"
|
||||
>
|
||||
|
||||
45
lib/canvas/batch-instruction-output.ts
Normal file
45
lib/canvas/batch-instruction-output.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import "server-only";
|
||||
|
||||
import { randomUUID } from "node:crypto";
|
||||
import sharp from "sharp";
|
||||
import { fetchImageBufferFromUrl } from "@/lib/ai/rmbg/service";
|
||||
import { uploadFile } from "@/lib/storage/service";
|
||||
|
||||
export async function padImageToSquare(bytes: Buffer) {
|
||||
const source = sharp(bytes).autoOrient();
|
||||
const metadata = await source.metadata();
|
||||
const { width, height } = metadata.autoOrient;
|
||||
if (!width || !height) throw new Error("无法读取生成图尺寸");
|
||||
if (width === height) return null;
|
||||
|
||||
const size = Math.max(width, height);
|
||||
const left = Math.floor((size - width) / 2);
|
||||
const top = Math.floor((size - height) / 2);
|
||||
const { data, info } = await source.clone().ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
||||
const corners = [0, (width - 1) * info.channels, (height - 1) * width * info.channels, (width * height - 1) * info.channels];
|
||||
const sample = (channel: number) =>
|
||||
Math.round(corners.reduce((sum, offset) => sum + data[offset + channel], 0) / corners.length);
|
||||
const background = { r: sample(0), g: sample(1), b: sample(2), alpha: sample(3) / 255 };
|
||||
return source.extend({ left, right: size - width - left, top, bottom: size - height - top, background })
|
||||
.keepIccProfile()
|
||||
.png()
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
export async function squareBatchInstructionOutput(image: { originalUrl: string; thumbnailUrl: string }) {
|
||||
const bytes = await fetchImageBufferFromUrl(image.originalUrl);
|
||||
const padded = await padImageToSquare(bytes);
|
||||
if (!padded) return image;
|
||||
const thumbnail = await sharp(padded).resize(512, 512, { withoutEnlargement: true }).png().toBuffer();
|
||||
const id = randomUUID();
|
||||
const upload = (name: string, file: Buffer) => uploadFile(
|
||||
name,
|
||||
new Uint8Array(file).buffer,
|
||||
{ preferQiniuForm: true },
|
||||
);
|
||||
const [original, preview] = await Promise.all([
|
||||
upload(`batch-instruction-${id}.png`, padded),
|
||||
upload(`batch-instruction-${id}-thumb.png`, thumbnail),
|
||||
]);
|
||||
return { ...image, originalUrl: original.url, thumbnailUrl: preview.url };
|
||||
}
|
||||
@ -185,6 +185,7 @@ export interface CanvasGenerateImageRequest {
|
||||
referenceImageBuffers?: ReadonlyMap<string, Buffer>;
|
||||
billingFeature?: "multi-view" | "repaint";
|
||||
requiredReferenceImageCount?: number;
|
||||
padBatchInstructionToSquare?: boolean;
|
||||
ownerKey?: string;
|
||||
debugLabel?: string;
|
||||
debugMeta?: Record<string, unknown>;
|
||||
|
||||
@ -407,6 +407,18 @@ async function runSingleTask(entry: PendingTaskEntry) {
|
||||
...imageRequest,
|
||||
ownerKey,
|
||||
});
|
||||
if (imageRequest.padBatchInstructionToSquare) {
|
||||
const { squareBatchInstructionOutput } = await import("./batch-instruction-output");
|
||||
try {
|
||||
return {
|
||||
...initialResult,
|
||||
images: await Promise.all(initialResult.images.map(squareBatchInstructionOutput)),
|
||||
};
|
||||
} catch (error) {
|
||||
console.warn("[BatchInstruction] Square output failed", { taskId: task.id, error });
|
||||
throw new Error("图片已生成,但补边结果尚未确认。请核对原任务,避免重复生成。");
|
||||
}
|
||||
}
|
||||
return await applyScaleAdjustModelRelight({
|
||||
generateImage: generateCanvasImage,
|
||||
initialResult,
|
||||
|
||||
72
lib/client/batch-instruction-edit.ts
Normal file
72
lib/client/batch-instruction-edit.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import type { ParsedImage } from "@/lib/canvas/generate-image";
|
||||
|
||||
const statusEndpoint = "/api/canvas/generate/image";
|
||||
type InstructionTask = {
|
||||
status: "waiting" | "pending" | "running" | "succeeded" | "failed";
|
||||
error?: string;
|
||||
failure?: { outcome?: "failed" | "unknown" };
|
||||
result?: { images?: ParsedImage[] };
|
||||
};
|
||||
|
||||
export class BatchInstructionEditError extends Error {
|
||||
constructor(message: string, readonly canStartNewTask: boolean) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export async function runBatchInstructionEdit(input: {
|
||||
taskId: string;
|
||||
image: string;
|
||||
reference?: string;
|
||||
instruction: string;
|
||||
square: boolean;
|
||||
submitted?: boolean;
|
||||
}, onSubmitted: () => void) {
|
||||
const taskPath = `${statusEndpoint}?taskId=${encodeURIComponent(input.taskId)}`;
|
||||
if (!input.submitted) {
|
||||
// A retry with the same identifier can only find or create the original task.
|
||||
const response = await fetch("/api/canvas/batch-instruction", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(input),
|
||||
signal: AbortSignal.timeout(20_000),
|
||||
}).catch(() => null);
|
||||
if (response?.ok) onSubmitted();
|
||||
if (response && (response.status === 400 || response.status === 403)) {
|
||||
const body = await response.json().catch(() => null);
|
||||
throw new BatchInstructionEditError(body?.error || "任务未提交", true);
|
||||
}
|
||||
}
|
||||
|
||||
const deadline = Date.now() + 20 * 60_000;
|
||||
let failures = 0;
|
||||
while (Date.now() < deadline && failures < 5) {
|
||||
const response = await fetch(taskPath, {
|
||||
cache: "no-store",
|
||||
signal: AbortSignal.timeout(12_000),
|
||||
}).catch(() => null);
|
||||
const task = response?.ok
|
||||
? (await response.json().catch(() => null))?.task as InstructionTask | undefined
|
||||
: undefined;
|
||||
if (task) {
|
||||
failures = 0;
|
||||
onSubmitted();
|
||||
if (task.status === "failed") {
|
||||
throw new BatchInstructionEditError(
|
||||
task.error || "图片编辑失败",
|
||||
task.failure?.outcome === "failed",
|
||||
);
|
||||
}
|
||||
if (task.status === "succeeded") {
|
||||
const image = task.result?.images?.[0];
|
||||
if (!image?.originalUrl) throw new BatchInstructionEditError("任务已完成,但暂时无法取回图片", false);
|
||||
return { result: image.thumbnailUrl || image.originalUrl, originalUrl: image.originalUrl };
|
||||
}
|
||||
} else {
|
||||
failures++;
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, 2500));
|
||||
}
|
||||
|
||||
throw new BatchInstructionEditError("暂时无法确认结果,请继续查询原任务", false);
|
||||
}
|
||||
@ -20,16 +20,17 @@ async function taskRequest(url: string, init?: RequestInit) {
|
||||
}
|
||||
export async function downloadCanvasSelectionZip(params: {
|
||||
folderName: string; files: Array<{ url: string; name: string }>; selectionKey: string;
|
||||
scope?: "batch-instruction";
|
||||
onProgress: (task: CanvasZipTaskView) => void;
|
||||
}) {
|
||||
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify([params.selectionKey, params.files])));
|
||||
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify([params.selectionKey, params.files, ...(params.scope ? [params.scope] : [])])));
|
||||
const key = "canvas-zip:" + Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, "0")).join("");
|
||||
let taskId = "";
|
||||
try { taskId = sessionStorage.getItem(key) || ""; } catch { /* Storage is optional. */ }
|
||||
if (!/^[0-9a-f-]{36}$/i.test(taskId)) taskId = crypto.randomUUID();
|
||||
try { sessionStorage.setItem(key, taskId); } catch { /* Downloads work without browser persistence. */ }
|
||||
let task = await taskRequest(endpoint, { method: "POST", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ taskId, folderName: params.folderName, files: params.files, retry: true }) });
|
||||
body: JSON.stringify({ taskId, folderName: params.folderName, files: params.files, ...(params.scope ? { scope: params.scope } : {}), retry: true }) });
|
||||
taskId = task.id;
|
||||
try { sessionStorage.setItem(key, taskId); } catch {}
|
||||
const deadline = Date.now() + 30 * 60_000;
|
||||
|
||||
@ -13,6 +13,7 @@ export default defineConfig({
|
||||
/routes\/canvas-download-recovery\.test\.ts/,
|
||||
/routes\/canvas-download-rate-limit\.test\.ts/,
|
||||
/routes\/canvas-zip-download\.test\.ts/,
|
||||
/routes\/batch-instruction-edit\.test\.ts/,
|
||||
/routes\/image-archive-flow\.test\.ts/,
|
||||
/routes\/image-archive-lanes\.test\.ts/,
|
||||
/routes\/runtime-active-work\.test\.ts/,
|
||||
|
||||
60
tests/routes/batch-instruction-edit.test.ts
Normal file
60
tests/routes/batch-instruction-edit.test.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { test, expect } from "@playwright/test";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import sharp from "sharp";
|
||||
import { serverLoader } from "../helpers/canvas-download-server-loader";
|
||||
|
||||
test("square padding keeps every pixel at the same scale in both orientations", async () => {
|
||||
const { padImageToSquare } = serverLoader({
|
||||
"@/lib/ai/rmbg/service": {},
|
||||
"@/lib/storage/service": {},
|
||||
})("lib/canvas/batch-instruction-output.ts");
|
||||
|
||||
for (const [width, height] of [[401, 301], [277, 408]]) {
|
||||
const pixels = Buffer.alloc(width * height * 4);
|
||||
for (let index = 0; index < pixels.length; index++) {
|
||||
pixels[index] = (index + Math.floor(index / (4 * width))) % 251;
|
||||
}
|
||||
const source = await sharp(pixels, { raw: { width, height, channels: 4 } }).png().toBuffer();
|
||||
const padded = await padImageToSquare(source);
|
||||
const side = Math.max(width, height);
|
||||
const restored = await sharp(padded).extract({
|
||||
width, height,
|
||||
left: Math.floor((side - width) / 2),
|
||||
top: Math.floor((side - height) / 2),
|
||||
}).raw().toBuffer();
|
||||
const dimensions = await sharp(padded).metadata();
|
||||
expect([dimensions.width, dimensions.height]).toEqual([side, side]);
|
||||
expect(Buffer.compare(restored, pixels)).toBe(0);
|
||||
}
|
||||
});
|
||||
|
||||
test("instruction requests preserve base and shared reference roles and task identifier", async () => {
|
||||
let request: Record<string, any> | undefined;
|
||||
let submitted = "";
|
||||
const id = randomUUID();
|
||||
const { POST } = serverLoader({
|
||||
"@/app/(auth)/auth": { auth: async () => ({ user: { id: "test-owner" } }) },
|
||||
"@/lib/db/queries": { getSystemSetting: async () => null },
|
||||
"@/lib/canvas/image-task-queue": {
|
||||
createCanvasImageTask: async (params: { request: Record<string, unknown>; taskId: string }) => {
|
||||
request = params.request;
|
||||
return { id: params.taskId, status: "pending" };
|
||||
},
|
||||
enqueueCanvasImageTask: async (taskId: string) => { submitted = taskId; },
|
||||
},
|
||||
})("app/api/canvas/batch-instruction/route.ts");
|
||||
const base = "https://media.example/base.png";
|
||||
const ref = "https://media.example/reference.png";
|
||||
const response = await POST(new Request("https://app.example/api/canvas/batch-instruction", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ taskId: id, image: base, reference: ref, instruction: "只换五官", square: true }),
|
||||
}));
|
||||
expect(response.status).toBe(202);
|
||||
expect(submitted).toBe(id);
|
||||
expect(request).toMatchObject({
|
||||
referenceImages: [base, ref], requiredReferenceImageCount: 2, imageCount: 1,
|
||||
aspectRatio: "auto", padBatchInstructionToSquare: true,
|
||||
});
|
||||
expect(request?.prompt).toContain("第二张图只提供指令指定");
|
||||
expect(request?.prompt).toContain("只换五官");
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user