Initial 智念AIGC platform
This commit is contained in:
64
app/api/generations/image/route.ts
Normal file
64
app/api/generations/image/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { getGenerationJob, listGenerationJobs } from "@/lib/server/data-store";
|
||||
import { jsonError, jsonOk, readJsonBody } from "@/lib/server/api";
|
||||
import { requestOrigin } from "@/lib/server/runtime";
|
||||
import { submitImageJob } from "@/lib/server/generation-service";
|
||||
import { assemblePrompt, type PromptAssemblyInput, type PromptMaterial } from "@/lib/prompt/assembler";
|
||||
import type { EnabledImageCapability } from "@/lib/types";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const jobs = (await listGenerationJobs()).filter((job) => job.capability !== "video.generate");
|
||||
return jsonOk({ jobs });
|
||||
} catch (error) {
|
||||
return jsonError(error, 500);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await readJsonBody<{
|
||||
capability?: EnabledImageCapability;
|
||||
prompt?: string;
|
||||
imageUrls?: string[];
|
||||
materials?: PromptMaterial[];
|
||||
promptAssembly?: PromptAssemblyInput;
|
||||
inputAssetIds?: string[];
|
||||
scale?: number;
|
||||
width?: number;
|
||||
height?: number;
|
||||
min_ratio?: number;
|
||||
max_ratio?: number;
|
||||
force_single?: boolean;
|
||||
}>(request);
|
||||
const capability = body.capability || "image.generate";
|
||||
const assembled = body.promptAssembly
|
||||
? assemblePrompt({ ...body.promptAssembly, mode: "image", materials: body.materials || body.promptAssembly.materials || [] })
|
||||
: undefined;
|
||||
const materialImages = (body.materials || assembled?.materials || [])
|
||||
.filter((material) => material.type === "image")
|
||||
.map((material) => material.url);
|
||||
const job = await submitImageJob({
|
||||
capability,
|
||||
prompt: body.prompt || assembled?.prompt,
|
||||
imageUrls: body.imageUrls || materialImages,
|
||||
inputAssetIds: body.inputAssetIds || (body.materials || []).map((material) => material.id).filter(Boolean) as string[],
|
||||
scale: asNumber(body.scale),
|
||||
width: asNumber(body.width),
|
||||
height: asNumber(body.height),
|
||||
min_ratio: asNumber(body.min_ratio),
|
||||
max_ratio: asNumber(body.max_ratio),
|
||||
force_single: Boolean(body.force_single)
|
||||
}, requestOrigin(request));
|
||||
return jsonOk({ job: await getGenerationJob(job.id) }, { status: 202 });
|
||||
} catch (error) {
|
||||
return jsonError(error);
|
||||
}
|
||||
}
|
||||
|
||||
function asNumber(value: unknown): number | undefined {
|
||||
if (value === undefined || value === null || value === "") return undefined;
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? parsed : undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user