67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import { NextResponse } from "next/server";
|
||
import { applyMindmapOps, type MindmapOp } from "@/lib/mindmap/mindmapOps";
|
||
import { isConvexEnabled } from "@/lib/convex/enabled";
|
||
import { getAuthedConvexClient } from "@/lib/convex/route";
|
||
import { api } from "@/lib/convex/api";
|
||
|
||
const defaultMindmapData = {
|
||
data: { text: "中心主题" },
|
||
children: [],
|
||
};
|
||
|
||
type RequestPayload = {
|
||
ops: MindmapOp[];
|
||
actor?: { kind?: string; provider?: string; model?: string };
|
||
reason?: string;
|
||
};
|
||
|
||
export async function POST(
|
||
request: Request,
|
||
{ params }: { params: Promise<{ docId: string; mindmapId: string }> },
|
||
) {
|
||
const { docId, mindmapId } = await params;
|
||
|
||
if (isConvexEnabled()) {
|
||
const { client } = await getAuthedConvexClient();
|
||
|
||
const payload = (await request.json().catch(() => null)) as RequestPayload | null;
|
||
const ops = Array.isArray(payload?.ops) ? payload!.ops : [];
|
||
if (!ops.length) {
|
||
return NextResponse.json({ error: "缺少 ops" }, { status: 400 });
|
||
}
|
||
if (ops.length > 80) {
|
||
return NextResponse.json({ error: "ops 过多(最大 80)" }, { status: 400 });
|
||
}
|
||
|
||
try {
|
||
const current = await client.query(api.mindmaps.get, { docId, mindmapId });
|
||
const baseData = current?.data ?? defaultMindmapData;
|
||
const { data: nextData, applied, errors } = applyMindmapOps(baseData, ops);
|
||
|
||
await client.mutation(api.mindmaps.put, {
|
||
docId,
|
||
mindmapId,
|
||
data: nextData,
|
||
});
|
||
|
||
return NextResponse.json({
|
||
ok: true,
|
||
applied,
|
||
errors,
|
||
data: nextData,
|
||
meta: {
|
||
documentId: docId,
|
||
mindmapId,
|
||
actor: payload?.actor ?? null,
|
||
reason: payload?.reason ?? null,
|
||
},
|
||
});
|
||
} catch (error) {
|
||
return NextResponse.json({ error: (error as Error).message }, { status: 400 });
|
||
}
|
||
}
|
||
|
||
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
||
}
|
||
|