feat: add task workflow and asset downloads
This commit is contained in:
42
app/api/assets/[id]/download/route.ts
Normal file
42
app/api/assets/[id]/download/route.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { getAsset } from "@/lib/server/data-store";
|
||||
import { jsonError } from "@/lib/server/api";
|
||||
import { readAssetForDownload } from "@/lib/server/storage";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(_request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
const asset = await getAsset(id);
|
||||
if (!asset) return jsonError("资产不存在", 404);
|
||||
const file = await readAssetForDownload(asset);
|
||||
if (!file) return jsonError("资产文件不可下载", 404);
|
||||
return new Response(new Uint8Array(file.bytes), {
|
||||
headers: {
|
||||
"Content-Type": file.contentType,
|
||||
"Content-Length": String(file.bytes.length),
|
||||
"Content-Disposition": contentDisposition(asset.name || `${asset.id}${extensionForContentType(file.contentType)}`),
|
||||
"Cache-Control": "private, no-store"
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
return jsonError(error, 500);
|
||||
}
|
||||
}
|
||||
|
||||
function contentDisposition(fileName: string): string {
|
||||
const clean = fileName.replace(/[\r\n/\\]/g, "_").trim() || "download";
|
||||
const ascii = clean.replace(/[^\x20-\x7E]/g, "_").replace(/"/g, "_");
|
||||
return `attachment; filename="${ascii}"; filename*=UTF-8''${encodeURIComponent(clean)}`;
|
||||
}
|
||||
|
||||
function extensionForContentType(contentType: string): string {
|
||||
const normalized = contentType.split(";")[0]?.trim().toLowerCase();
|
||||
if (normalized === "image/png") return ".png";
|
||||
if (normalized === "image/jpeg") return ".jpg";
|
||||
if (normalized === "image/webp") return ".webp";
|
||||
if (normalized === "image/svg+xml") return ".svg";
|
||||
if (normalized === "video/mp4") return ".mp4";
|
||||
if (normalized === "audio/mpeg") return ".mp3";
|
||||
return "";
|
||||
}
|
||||
@@ -1,17 +1,14 @@
|
||||
import { deleteAsset, deleteGenerationJob, getAsset, getGenerationJob } from "@/lib/server/data-store";
|
||||
import { jsonError, jsonOk } from "@/lib/server/api";
|
||||
import { requestOrigin } from "@/lib/server/runtime";
|
||||
import { syncImageJob } from "@/lib/server/generation-service";
|
||||
import { deleteStoredAsset } from "@/lib/server/storage";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
export async function GET(_request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
const existing = await getGenerationJob(id);
|
||||
if (!existing) return jsonError(new Error("Generation job not found."), 404);
|
||||
const job = await syncImageJob(id, requestOrigin(request));
|
||||
const job = await getGenerationJob(id);
|
||||
if (!job) return jsonError(new Error("Generation job not found."), 404);
|
||||
return jsonOk({ job });
|
||||
} catch (error) {
|
||||
return jsonError(error, 500);
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import { deleteAsset, deleteGenerationJob, getAsset, getGenerationJob } from "@/lib/server/data-store";
|
||||
import { jsonError, jsonOk } from "@/lib/server/api";
|
||||
import { requestOrigin } from "@/lib/server/runtime";
|
||||
import { syncVideoJob } from "@/lib/server/video-generation-service";
|
||||
import { deleteStoredAsset } from "@/lib/server/storage";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
export async function GET(_request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const { id } = await context.params;
|
||||
const job = await syncVideoJob(id, requestOrigin(request));
|
||||
const job = await getGenerationJob(id);
|
||||
if (!job) return jsonError(new Error("Generation job not found."), 404);
|
||||
return jsonOk({ job });
|
||||
} catch (error) {
|
||||
return jsonError(error, 500);
|
||||
|
||||
24
app/api/internal/worker/tick/route.ts
Normal file
24
app/api/internal/worker/tick/route.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { jsonError, jsonOk, readJsonBody } from "@/lib/server/api";
|
||||
import { assertInternalWorkerToken, PublicApiAuthError } from "@/lib/server/public-api-auth";
|
||||
import { runWorkerTick } from "@/lib/server/task-manager";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
assertInternalWorkerToken(request);
|
||||
const body = await readJsonBody<{
|
||||
workerId?: string;
|
||||
limit?: number;
|
||||
}>(request);
|
||||
const result = await runWorkerTick({
|
||||
request,
|
||||
workerId: body.workerId,
|
||||
limit: typeof body.limit === "number" ? body.limit : undefined
|
||||
});
|
||||
return jsonOk(result);
|
||||
} catch (error) {
|
||||
if (error instanceof PublicApiAuthError) return jsonError(error.message, error.status);
|
||||
return jsonError(error, 500);
|
||||
}
|
||||
}
|
||||
66
app/api/v1/assets/route.ts
Normal file
66
app/api/v1/assets/route.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { createAsset, listAssets } from "@/lib/server/data-store";
|
||||
import { jsonOk, readJsonBody } from "@/lib/server/api";
|
||||
import { authenticatePublicApiRequest } from "@/lib/server/public-api-auth";
|
||||
import { publicApiError } from "@/lib/server/public-api-response";
|
||||
import { DEFAULT_OWNER_ID, requestOrigin } from "@/lib/server/runtime";
|
||||
import { saveUploadAsset } from "@/lib/server/storage";
|
||||
import type { AssetKind } from "@/lib/types";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
authenticatePublicApiRequest(request);
|
||||
return jsonOk({ assets: await listAssets(DEFAULT_OWNER_ID) });
|
||||
} catch (error) {
|
||||
return publicApiError(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const client = authenticatePublicApiRequest(request);
|
||||
const contentType = request.headers.get("content-type") || "";
|
||||
if (contentType.includes("multipart/form-data")) {
|
||||
const form = await request.formData();
|
||||
const files = form.getAll("files").filter((item): item is File => item instanceof File);
|
||||
if (!files.length) throw new Error("No files uploaded.");
|
||||
const assets = await Promise.all(files.map(async (file) => saveUploadAsset({
|
||||
ownerId: DEFAULT_OWNER_ID,
|
||||
bytes: await awaitFileBytes(file),
|
||||
fileName: file.name,
|
||||
contentType: file.type || "application/octet-stream",
|
||||
origin: requestOrigin(request),
|
||||
tags: ["upload", "public-api", `api-client:${client.id}`]
|
||||
})));
|
||||
return jsonOk({ assets }, { status: 201 });
|
||||
}
|
||||
|
||||
const body = await readJsonBody<{
|
||||
url?: string;
|
||||
name?: string;
|
||||
kind?: AssetKind;
|
||||
tags?: string[];
|
||||
}>(request);
|
||||
if (!body.url) throw new Error("url is required");
|
||||
const asset = await createAsset({
|
||||
ownerId: DEFAULT_OWNER_ID,
|
||||
kind: body.kind || "image",
|
||||
name: body.name || "外部素材",
|
||||
url: body.url,
|
||||
source: "external",
|
||||
tags: [...(body.tags || []), "external", "public-api", `api-client:${client.id}`],
|
||||
metadata: {
|
||||
registeredFrom: "public-api",
|
||||
externalClientId: client.id
|
||||
}
|
||||
});
|
||||
return jsonOk({ asset }, { status: 201 });
|
||||
} catch (error) {
|
||||
return publicApiError(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function awaitFileBytes(file: File): Promise<Buffer> {
|
||||
return Buffer.from(await file.arrayBuffer());
|
||||
}
|
||||
45
app/api/v1/capabilities/route.ts
Normal file
45
app/api/v1/capabilities/route.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { getEffectiveImageEngine, getEvolinkImageSettings } from "@/lib/evolink/image-client";
|
||||
import { getVisibleImageCapabilities } from "@/lib/jimeng/capabilities";
|
||||
import { jsonOk } from "@/lib/server/api";
|
||||
import { authenticatePublicApiRequest } from "@/lib/server/public-api-auth";
|
||||
import { publicApiError } from "@/lib/server/public-api-response";
|
||||
import { getSeedanceConfig } from "@/lib/seedance/client";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
authenticatePublicApiRequest(request);
|
||||
const evolink = getEvolinkImageSettings();
|
||||
return jsonOk({
|
||||
capabilities: [
|
||||
...getVisibleImageCapabilities().map((capability) => {
|
||||
const engine = getEffectiveImageEngine(capability.id);
|
||||
return {
|
||||
id: capability.id,
|
||||
label: capability.label,
|
||||
kind: "image",
|
||||
engine,
|
||||
provider: engine === "evolink" ? "evolink" : "volcengine-visual",
|
||||
reqKey: engine === "evolink" ? evolink.model : capability.reqKey
|
||||
};
|
||||
}),
|
||||
{
|
||||
id: "video.generate",
|
||||
label: "Seedance 视频生成",
|
||||
kind: "video",
|
||||
engine: "seedance",
|
||||
provider: "seedance",
|
||||
reqKey: getSeedanceConfig().model,
|
||||
limits: {
|
||||
durationSeconds: { min: 4, max: 15 },
|
||||
ratios: ["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"],
|
||||
resolutions: ["480p", "720p", "1080p"]
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
} catch (error) {
|
||||
return publicApiError(error);
|
||||
}
|
||||
}
|
||||
26
app/api/v1/jobs/[id]/cancel/route.ts
Normal file
26
app/api/v1/jobs/[id]/cancel/route.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { clearGenerationJobLock, getGenerationJob, updateGenerationJob } from "@/lib/server/data-store";
|
||||
import { jsonError, jsonOk } from "@/lib/server/api";
|
||||
import { authenticatePublicApiRequest } from "@/lib/server/public-api-auth";
|
||||
import { publicApiError } from "@/lib/server/public-api-response";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function POST(request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const client = authenticatePublicApiRequest(request);
|
||||
const { id } = await context.params;
|
||||
const job = await getGenerationJob(id);
|
||||
if (!job || job.externalClientId !== client.id) return jsonError("Job not found.", 404);
|
||||
if (["succeeded", "failed", "expired", "cancelled"].includes(job.status)) {
|
||||
return jsonOk({ job });
|
||||
}
|
||||
await updateGenerationJob(id, {
|
||||
status: "cancelled",
|
||||
completedAt: new Date().toISOString()
|
||||
});
|
||||
const cancelled = await clearGenerationJobLock(id);
|
||||
return jsonOk({ job: cancelled });
|
||||
} catch (error) {
|
||||
return publicApiError(error);
|
||||
}
|
||||
}
|
||||
18
app/api/v1/jobs/[id]/route.ts
Normal file
18
app/api/v1/jobs/[id]/route.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { getGenerationJob } from "@/lib/server/data-store";
|
||||
import { jsonError, jsonOk } from "@/lib/server/api";
|
||||
import { authenticatePublicApiRequest } from "@/lib/server/public-api-auth";
|
||||
import { publicApiError } from "@/lib/server/public-api-response";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(request: Request, context: { params: Promise<{ id: string }> }) {
|
||||
try {
|
||||
const client = authenticatePublicApiRequest(request);
|
||||
const { id } = await context.params;
|
||||
const job = await getGenerationJob(id);
|
||||
if (!job || job.externalClientId !== client.id) return jsonError("Job not found.", 404);
|
||||
return jsonOk({ job });
|
||||
} catch (error) {
|
||||
return publicApiError(error);
|
||||
}
|
||||
}
|
||||
65
app/api/v1/jobs/route.ts
Normal file
65
app/api/v1/jobs/route.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { listGenerationJobsFiltered } from "@/lib/server/data-store";
|
||||
import { jsonOk, readJsonBody } from "@/lib/server/api";
|
||||
import { authenticatePublicApiRequest } from "@/lib/server/public-api-auth";
|
||||
import { createPublicGenerationJob, type PublicJobCreateBody } from "@/lib/server/public-api-jobs";
|
||||
import { publicApiError } from "@/lib/server/public-api-response";
|
||||
import { DEFAULT_OWNER_ID, requestOrigin } from "@/lib/server/runtime";
|
||||
import type { GenerationCapability, GenerationStatus } from "@/lib/types";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const client = authenticatePublicApiRequest(request);
|
||||
const url = new URL(request.url);
|
||||
const jobs = await listGenerationJobsFiltered({
|
||||
ownerId: DEFAULT_OWNER_ID,
|
||||
externalClientId: client.id,
|
||||
status: parseStatus(url.searchParams.get("status")),
|
||||
capability: parseCapability(url.searchParams.get("capability")),
|
||||
limit: parseLimit(url.searchParams.get("limit")),
|
||||
before: url.searchParams.get("before") || undefined
|
||||
});
|
||||
return jsonOk({ jobs });
|
||||
} catch (error) {
|
||||
return publicApiError(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const client = authenticatePublicApiRequest(request);
|
||||
const body = await readJsonBody<PublicJobCreateBody>(request);
|
||||
const result = await createPublicGenerationJob({
|
||||
client,
|
||||
body,
|
||||
request,
|
||||
origin: requestOrigin(request)
|
||||
});
|
||||
return jsonOk({ job: result.job, reused: result.reused }, { status: result.reused ? 200 : 202 });
|
||||
} catch (error) {
|
||||
return publicApiError(error);
|
||||
}
|
||||
}
|
||||
|
||||
function parseStatus(value: string | null): GenerationStatus | undefined {
|
||||
if (!value) return undefined;
|
||||
if (["queued", "running", "succeeded", "failed", "expired", "cancelled"].includes(value)) {
|
||||
return value as GenerationStatus;
|
||||
}
|
||||
throw new Error(`Unsupported status filter: ${value}`);
|
||||
}
|
||||
|
||||
function parseCapability(value: string | null): GenerationCapability | undefined {
|
||||
if (!value) return undefined;
|
||||
if (["image.generate", "image.inpaint", "image.upscale", "video.generate"].includes(value)) {
|
||||
return value as GenerationCapability;
|
||||
}
|
||||
throw new Error(`Unsupported capability filter: ${value}`);
|
||||
}
|
||||
|
||||
function parseLimit(value: string | null): number {
|
||||
const parsed = Number(value || 50);
|
||||
if (!Number.isFinite(parsed)) return 50;
|
||||
return Math.max(1, Math.min(200, Math.trunc(parsed)));
|
||||
}
|
||||
47
app/api/v1/openapi.json/route.ts
Normal file
47
app/api/v1/openapi.json/route.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { jsonOk } from "@/lib/server/api";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET() {
|
||||
return jsonOk({
|
||||
openapi: "3.1.0",
|
||||
info: {
|
||||
title: "智念AIGC平台 Public API",
|
||||
version: "1.0.0"
|
||||
},
|
||||
security: [{ bearerApiKey: [] }, { headerApiKey: [] }],
|
||||
components: {
|
||||
securitySchemes: {
|
||||
bearerApiKey: { type: "http", scheme: "bearer" },
|
||||
headerApiKey: { type: "apiKey", in: "header", name: "X-Zhinian-Api-Key" }
|
||||
}
|
||||
},
|
||||
paths: {
|
||||
"/api/v1/capabilities": {
|
||||
get: { summary: "List generation capabilities", responses: { "200": { description: "Capabilities" } } }
|
||||
},
|
||||
"/api/v1/assets": {
|
||||
get: { summary: "List assets", responses: { "200": { description: "Assets" } } },
|
||||
post: { summary: "Upload files or register an external asset URL", responses: { "201": { description: "Created asset" } } }
|
||||
},
|
||||
"/api/v1/jobs": {
|
||||
get: { summary: "List jobs", responses: { "200": { description: "Jobs" } } },
|
||||
post: { summary: "Create a queued generation job", responses: { "202": { description: "Queued job" }, "409": { description: "Idempotency conflict" } } }
|
||||
},
|
||||
"/api/v1/jobs/{id}": {
|
||||
get: {
|
||||
summary: "Get one job",
|
||||
parameters: [{ name: "id", in: "path", required: true, schema: { type: "string" } }],
|
||||
responses: { "200": { description: "Job" }, "404": { description: "Not found" } }
|
||||
}
|
||||
},
|
||||
"/api/v1/jobs/{id}/cancel": {
|
||||
post: {
|
||||
summary: "Cancel a queued or running job",
|
||||
parameters: [{ name: "id", in: "path", required: true, schema: { type: "string" } }],
|
||||
responses: { "200": { description: "Cancelled job" }, "404": { description: "Not found" } }
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user