89 lines
2.8 KiB
TypeScript
89 lines
2.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { desc, eq } from "drizzle-orm";
|
|
import { auth } from "@/app/(auth)/auth";
|
|
import { db } from "@/lib/db";
|
|
import { canvasProject } from "@/lib/db/schema";
|
|
import {
|
|
extractAgentProjectStateFromCanvasData,
|
|
isAgentProjectRow,
|
|
} from "@/lib/agents/agent-project-compat";
|
|
import { normalizeAgentGenerationProjectPersistedState } from "@/lib/agents/generation-project-state";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
export const revalidate = 0;
|
|
|
|
function mapAgentProjectRow(row: typeof canvasProject.$inferSelect) {
|
|
return {
|
|
id: row.id,
|
|
userId: row.userId,
|
|
title: row.title,
|
|
description: row.description || null,
|
|
thumbnail: row.thumbnail || null,
|
|
projectState: extractAgentProjectStateFromCanvasData(row.canvasData),
|
|
createdAt: row.createdAt,
|
|
updatedAt: row.updatedAt,
|
|
};
|
|
}
|
|
|
|
export async function GET() {
|
|
try {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ success: false, error: "未登录" }, { status: 401 });
|
|
}
|
|
|
|
const projects = await db
|
|
.select()
|
|
.from(canvasProject)
|
|
.where(eq(canvasProject.userId, session.user.id))
|
|
.orderBy(desc(canvasProject.updatedAt));
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: projects.filter(isAgentProjectRow).map(mapAgentProjectRow),
|
|
});
|
|
} catch (error) {
|
|
console.error("[AgentProjects API] 获取项目列表失败:", error);
|
|
return NextResponse.json({ success: false, error: "获取生图项目列表失败" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
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().catch(() => ({}));
|
|
const title = String(body.title || "").trim() || "未命名生图项目";
|
|
const description = String(body.description || "").trim() || null;
|
|
const thumbnail = String(body.thumbnail || "").trim() || null;
|
|
const projectState = normalizeAgentGenerationProjectPersistedState(body.projectState);
|
|
|
|
const [project] = await db
|
|
.insert(canvasProject)
|
|
.values({
|
|
userId: session.user.id,
|
|
title,
|
|
description,
|
|
thumbnail,
|
|
workspaceType: "agent",
|
|
canvasData: {
|
|
nodes: [],
|
|
edges: [],
|
|
selectedNodeIds: [],
|
|
viewport: { x: 0, y: 0, zoom: 1 },
|
|
tool: "select",
|
|
agentProjectState: projectState,
|
|
} as any,
|
|
})
|
|
.returning();
|
|
|
|
return NextResponse.json({ success: true, data: mapAgentProjectRow(project) });
|
|
} catch (error) {
|
|
console.error("[AgentProjects API] 创建项目失败:", error);
|
|
return NextResponse.json({ success: false, error: "创建生图项目失败" }, { status: 500 });
|
|
}
|
|
}
|