Your Name f1ffeed9f2
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
feat: add canvas layer split workflow
2026-03-24 02:07:45 +08:00

61 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/app/(auth)/auth";
import { executeLayerSplitJob, LayerSplitServiceError } from "@/lib/ai/layer-split/service";
export const runtime = "nodejs";
export const maxDuration = 300;
export async function POST(request: NextRequest) {
try {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: "未授权,请先登录" }, { status: 401 });
}
const contentType = request.headers.get("content-type") || "";
if (!contentType.includes("application/json")) {
return NextResponse.json(
{ error: "不支持的 Content-Type,请使用 application/json" },
{ status: 400 }
);
}
const body = await request.json();
const imageUrl = String(body.imageUrl || body.image_url || "").trim();
if (!imageUrl) {
return NextResponse.json({ error: "缺少 imageUrl 参数" }, { status: 400 });
}
const result = await executeLayerSplitJob({
userId: session.user.id,
imageUrl,
prompt: String(body.prompt || "").trim() || undefined,
chatId: String(body.chatId || "").trim() || null,
messageId: String(body.messageId || "").trim() || null,
sourceNodeId: String(body.sourceNodeId || "").trim() || null,
skipGallerySave: body.skipGallerySave === true,
});
return NextResponse.json({
success: true,
...result,
});
} catch (error) {
if (error instanceof LayerSplitServiceError) {
return NextResponse.json(
{
error: error.message,
detail: error.detail || error.message,
},
{ status: error.status }
);
}
console.error("[LayerSplit API] 处理错误:", error);
return NextResponse.json(
{ error: "服务器内部错误", detail: error instanceof Error ? error.message : "未知错误" },
{ status: 500 }
);
}
}