0.6 rust重构01

This commit is contained in:
lix-2026
2026-04-14 13:22:29 +08:00
parent 71fb1aee7e
commit 84a8454fa9
401 changed files with 5841 additions and 1512 deletions
@@ -3,32 +3,104 @@ import { isConvexEnabled } from "@/lib/convex/enabled";
import { getAuthedConvexClient } from "@/lib/convex/route";
import { api } from "@/lib/convex/api";
import { getBlocksFromDocumentContent, replaceBlockInTree, withBlocksWrittenBack } from "@/lib/blocks";
import {
assertBlockId,
assertDocumentId,
assertNextBlock,
buildDocumentBridgeContext,
buildDocumentCommandEnvelope,
documentBridgeErrorResponse,
} from "@/lib/documents/bridge";
import { recordBridgeCommandArtifacts } from "@/lib/documents/bridge-log";
type PatchPayload = {
sourceDocumentId: string;
workspaceId?: string | null;
blockId: string;
nextBlock: unknown;
};
export async function POST(request: Request) {
const { sourceDocumentId, blockId, nextBlock }: PatchPayload = await request.json();
if (!sourceDocumentId || !blockId || !nextBlock) {
return NextResponse.json({ error: "缺少参数" }, { status: 400 });
}
if (isConvexEnabled()) {
const { client } = await getAuthedConvexClient();
const doc = await client.query(api.documents.getContent, { id: sourceDocumentId });
if (!doc) return NextResponse.json({ error: "页面不存在或无权限" }, { status: 404 });
try {
const { sourceDocumentId, workspaceId, blockId, nextBlock }: PatchPayload = await request.json();
const normalizedDocumentId = assertDocumentId(sourceDocumentId);
const normalizedWorkspaceId = workspaceId?.trim() || null;
const normalizedBlockId = assertBlockId(blockId);
assertNextBlock(nextBlock);
const blocks = getBlocksFromDocumentContent(doc.content);
const replaced = replaceBlockInTree(blocks, blockId, nextBlock as any);
if (!replaced.ok) return NextResponse.json({ error: "块不存在或无权限" }, { status: 404 });
const bridgeContext = await buildDocumentBridgeContext({
request,
workspaceId: normalizedWorkspaceId,
});
const envelope = buildDocumentCommandEnvelope({
name: "blocks.patch",
payload: {
documentId: normalizedDocumentId,
workspaceId: normalizedWorkspaceId,
blockId: normalizedBlockId,
nextBlock,
},
context: bridgeContext,
target: {
workspaceId: normalizedWorkspaceId,
pageId: normalizedDocumentId,
blockId: normalizedBlockId,
},
});
const payload = withBlocksWrittenBack(doc.content, replaced.nextBlocks);
await client.mutation(api.documents.updateContent, { id: sourceDocumentId, content: payload });
return NextResponse.json({ ok: true });
const { client } = await getAuthedConvexClient();
const doc = await client.query(api.documents.getContent, { id: normalizedDocumentId });
if (!doc) {
return NextResponse.json(
{
error: "页面不存在或无权限",
meta: {
requestId: bridgeContext.requestId,
traceId: bridgeContext.traceId,
commandId: envelope.commandId,
commandName: envelope.name,
},
},
{ status: 404 },
);
}
const blocks = getBlocksFromDocumentContent(doc.content);
const replaced = replaceBlockInTree(blocks, normalizedBlockId, nextBlock as any);
if (!replaced.ok) {
return NextResponse.json(
{
error: "块不存在或无权限",
meta: {
requestId: bridgeContext.requestId,
traceId: bridgeContext.traceId,
commandId: envelope.commandId,
commandName: envelope.name,
},
},
{ status: 404 },
);
}
const payload = withBlocksWrittenBack(doc.content, replaced.nextBlocks);
await client.mutation(api.documents.updateContent, { id: normalizedDocumentId, content: payload });
await recordBridgeCommandArtifacts({
context: bridgeContext,
envelope,
});
return NextResponse.json({
ok: true,
meta: {
requestId: bridgeContext.requestId,
traceId: bridgeContext.traceId,
commandId: envelope.commandId,
commandName: envelope.name,
},
});
} catch (error) {
return documentBridgeErrorResponse(error);
}
}
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });