import { after, NextResponse } from "next/server"; import { z, ZodError } from "zod"; import { auth } from "@/app/(auth)/auth"; import { CanvasFeedbackError, collectCanvasFeedbackContext, collectPageFeedbackContext } from "@/lib/canvas/feedback-context"; import { feedbackCommandSchema, feedbackPagePathSchema } from "@/lib/canvas/feedback-contract"; import { deliverCanvasFeedback, feedbackReceipt, findCanvasFeedback, refreshCanvasFeedback, saveCanvasFeedback, } from "@/lib/canvas/feedback-store"; const response = (data: unknown, status = 200) => NextResponse.json(data, { status, headers: { "Cache-Control": "no-store" } }); function failure(error: unknown) { if (error instanceof CanvasFeedbackError) return response({ error: error.message }, error.status); if (error instanceof ZodError || error instanceof SyntaxError) return response({ error: "反馈资料格式不正确,请重新打开问题反馈后重试。" }, 400); console.error("[Canvas feedback] request failed", { errorType: error instanceof Error ? error.name : "unknown", }); return response({ error: "反馈暂未保存成功,请保留问题说明,稍后重试。" }, 503); } export async function feedbackPost(request: Request, projectId: string | null) { try { const session = await auth(); if (!session?.user?.id || (projectId === null && session.user.type === "guest")) return response({ error: "登录已过期,请登录后发送反馈。" }, 401); if (projectId !== null) z.string().uuid().parse(projectId); const raw = await request.text(); if (new TextEncoder().encode(raw).byteLength > 262144) return response({ error: "反馈资料过大,请减少补充说明后重试。" }, 413); const command = feedbackCommandSchema.parse(JSON.parse(raw)); if (command.action === "submit") { const existing = await findCanvasFeedback(command.id); if (existing) { if (existing.userId !== session.user.id || existing.projectId !== projectId) throw new CanvasFeedbackError("反馈编号不可用", 409); return response({ feedback: feedbackReceipt(await refreshCanvasFeedback(existing)) }); } } if (!projectId && command.action === "submit" && !command.description) { throw new CanvasFeedbackError("请填写遇到的问题。"); } const context = projectId ? await collectCanvasFeedbackContext( session.user.id, projectId, command.client, command.action === "submit" ? command.selection : null, ) : await collectPageFeedbackContext(session.user.id, feedbackPagePathSchema.parse(command.client.pagePath)); if (command.action === "context") return response({ context }); const selected = context.requests.find((item) => item.key === command.selection) || null; const row = await saveCanvasFeedback({ id: command.id, userId: session.user.id, description: command.description, appUrl: process.env.NEXT_PUBLIC_APP_URL || new URL(request.url).origin, snapshot: { reporter: context.reporter, project: context.project, request: selected, client: projectId ? command.client : { ...command.client, chatId: null, messages: [], nodes: [] }, userAgent: (request.headers.get("user-agent") || "").slice(0, 512), }, }); after(() => deliverCanvasFeedback(row.id)); return response({ feedback: feedbackReceipt(row) }, 202); } catch (error) { return failure(error); } } export async function feedbackGet(request: Request, projectId: string | null) { try { const session = await auth(); if (!session?.user?.id || (projectId === null && session.user.type === "guest")) return response({ error: "登录已过期,请登录后查看反馈状态。" }, 401); if (projectId !== null) z.string().uuid().parse(projectId); const id = z.string().uuid().parse(new URL(request.url).searchParams.get("feedbackId")); const row = await findCanvasFeedback(id); if (!row || row.userId !== session.user.id || row.projectId !== projectId) throw new CanvasFeedbackError("反馈记录不存在", 404); return response({ feedback: feedbackReceipt(await refreshCanvasFeedback(row)) }); } catch (error) { return failure(error); } }