import { NextRequest, NextResponse } from "next/server"; import { z } from "zod"; import { auth } from "@/app/(auth)/auth"; import { ChatSDKError } from "@/lib/errors"; import { forkAgentPreset } from "@/lib/db/queries"; const forkSchema = z.object({ name: z.string().trim().min(1).max(128).optional(), }); export async function POST( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const session = await auth(); if (!session?.user?.id) { return NextResponse.json({ success: false, error: "未登录" }, { status: 401 }); } const { id } = await params; const body = await request.json().catch(() => ({})); const data = forkSchema.parse(body); const preset = await forkAgentPreset({ id, userId: session.user.id, name: data.name, }); return NextResponse.json({ success: true, data: preset }); } catch (error) { if (error instanceof ChatSDKError) { return error.toResponse(); } if (error instanceof z.ZodError) { return NextResponse.json( { success: false, error: error.message }, { status: 400 } ); } return NextResponse.json( { success: false, error: "复制风格化失败" }, { status: 500 } ); } }