2026-01-10 10:35:21 +08:00
|
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
|
import { applyMindmapOps, type MindmapOp } from "@/lib/mindmap/mindmapOps";
|
2026-01-17 10:12:53 +08:00
|
|
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
|
|
|
|
import { getAuthedConvexClient } from "@/lib/convex/route";
|
|
|
|
|
|
import { api } from "@/lib/convex/api";
|
2026-01-10 10:35:21 +08:00
|
|
|
|
|
|
|
|
|
|
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;
|
2026-01-17 10:12:53 +08:00
|
|
|
|
|
|
|
|
|
|
if (isConvexEnabled()) {
|
2026-01-18 19:01:31 +08:00
|
|
|
|
const { client } = await getAuthedConvexClient();
|
2026-01-17 10:12:53 +08:00
|
|
|
|
|
|
|
|
|
|
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) {
|
2026-01-18 19:01:31 +08:00
|
|
|
|
return NextResponse.json({ error: "ops 过多(最大 80)" }, { status: 400 });
|
2026-01-17 10:12:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2026-01-18 19:01:31 +08:00
|
|
|
|
const current = await client.query(api.mindmaps.get, { docId, mindmapId });
|
2026-01-17 10:12:53 +08:00
|
|
|
|
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 });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
2026-01-10 10:35:21 +08:00
|
|
|
|
}
|
2026-01-18 19:01:31 +08:00
|
|
|
|
|