import { NextResponse } from "next/server"; import { auth } from "@/app/(auth)/auth"; import { applyUiScreenPatch, buildAutoUiScreenPatch, type UiScreenPatch, } from "@/lib/ui-screen/patch"; import { normalizeUiScreenDocument } from "@/lib/ui-screen/schema"; import { validateUiScreenDocument } from "@/lib/ui-screen/validation"; export const runtime = "nodejs"; export const maxDuration = 60; type UiScreenReviewRequest = { document?: unknown; applyAutoPatch?: boolean; }; export async function POST(request: Request) { try { const session = await auth(); if (!session?.user?.id) { return NextResponse.json({ success: false, error: "未登录" }, { status: 401 }); } const body = (await request.json()) as UiScreenReviewRequest; const document = normalizeUiScreenDocument(body.document); if (!document) { return NextResponse.json( { success: false, error: "缺少有效的 ui-screen/v1 文档" }, { status: 400 } ); } const validation = validateUiScreenDocument(document); let patch: UiScreenPatch | undefined; let patchedDocument = document; if (body.applyAutoPatch) { patch = buildAutoUiScreenPatch({ document, validation }); patchedDocument = applyUiScreenPatch(document, patch); } return NextResponse.json({ success: true, data: { validation, patch, patchedDocument: body.applyAutoPatch ? patchedDocument : undefined, }, }); } catch (error) { console.error("[UiScreenReview] Failed:", error); return NextResponse.json( { success: false, error: error instanceof Error ? error.message : "UI Screen 质检失败", }, { status: 500 } ); } }