feat: 收口 Rust Web 3000 主链
This commit is contained in:
@@ -1,165 +1,21 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { isConvexEnabled } from "@/lib/convex/enabled";
|
||||
import { getAuthedConvexClient } from "@/lib/convex/route";
|
||||
import {
|
||||
buildDocumentBridgeContextWithActor,
|
||||
buildDocumentQueryEnvelope,
|
||||
} from "@/lib/documents/bridge";
|
||||
import {
|
||||
executeRustBridgeQueryTransport,
|
||||
resolveRustBridgeQueryPlan,
|
||||
} from "@/lib/documents/rust-runtime";
|
||||
import type { SidebarDatasetListQueryResult } from "@/lib/sidebar-data";
|
||||
import { attachKernelFileTreeProjection, resolveKernelFileTreeProjection } from "@/lib/server/kernel-file-tree";
|
||||
import {
|
||||
streamTreeFrames,
|
||||
type TreeStreamOverview,
|
||||
type TreeStreamSnapshotPayload,
|
||||
} from "@/lib/tree-stream/server";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
export const runtime = "nodejs";
|
||||
|
||||
function readNumberParam(url: URL, name: string): number | null {
|
||||
const raw = url.searchParams.get(name);
|
||||
if (!raw?.trim()) {
|
||||
return null;
|
||||
}
|
||||
const parsed = Number(raw);
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
function encodeSseFrame(event: string, payload: unknown) {
|
||||
return `event: ${event}\ndata: ${JSON.stringify(payload)}\n\n`;
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
if (!isConvexEnabled()) {
|
||||
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
||||
}
|
||||
|
||||
const requestUrl = new URL(request.url);
|
||||
const workspaceId = requestUrl.searchParams.get("workspaceId")?.trim();
|
||||
if (!workspaceId) {
|
||||
return NextResponse.json({ error: "缺少 workspaceId" }, { status: 400 });
|
||||
}
|
||||
|
||||
const { auth, client } = await getAuthedConvexClient();
|
||||
const actor = {
|
||||
actorType: "user",
|
||||
actorId: auth.userId,
|
||||
sessionId: null,
|
||||
};
|
||||
const context = buildDocumentBridgeContextWithActor({
|
||||
request,
|
||||
actor,
|
||||
workspaceId,
|
||||
source: {
|
||||
channel: "next_mnote_web_stream",
|
||||
client: "wolai-frontend",
|
||||
const directUrl = new URL("/api/tree/events", request.url);
|
||||
directUrl.search = new URL(request.url).search;
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "Next /api/mnote-web/stream compat alias 已退场,请直接使用 /api/tree/events。",
|
||||
redirectTo: directUrl.toString(),
|
||||
},
|
||||
});
|
||||
|
||||
const loadOverview = async (): Promise<TreeStreamOverview> => {
|
||||
const envelope = buildDocumentQueryEnvelope({
|
||||
name: "bridge.workspace.overview",
|
||||
payload: {
|
||||
workspaceId,
|
||||
limit: 50,
|
||||
cursor: null,
|
||||
commandStatus: null,
|
||||
eventStatus: null,
|
||||
targetPageId: null,
|
||||
targetBlockId: null,
|
||||
aggregateType: null,
|
||||
aggregateId: null,
|
||||
{
|
||||
status: 410,
|
||||
headers: {
|
||||
"x-mnote-compat-boundary": "mnote-web-stream-alias-retired",
|
||||
},
|
||||
});
|
||||
const plan = await resolveRustBridgeQueryPlan({
|
||||
context,
|
||||
envelope,
|
||||
});
|
||||
return executeRustBridgeQueryTransport<TreeStreamOverview>({
|
||||
client,
|
||||
plan,
|
||||
});
|
||||
};
|
||||
|
||||
const loadSnapshot = async (): Promise<TreeStreamSnapshotPayload> => {
|
||||
const envelope = buildDocumentQueryEnvelope({
|
||||
name: "sidebar.dataset.list",
|
||||
payload: {
|
||||
workspaceId,
|
||||
},
|
||||
});
|
||||
const plan = await resolveRustBridgeQueryPlan({
|
||||
context,
|
||||
envelope,
|
||||
});
|
||||
const dataset = await executeRustBridgeQueryTransport<SidebarDatasetListQueryResult>({
|
||||
client,
|
||||
plan,
|
||||
});
|
||||
const datasetWithFileTree = attachKernelFileTreeProjection({
|
||||
dataset,
|
||||
projection: await resolveKernelFileTreeProjection({
|
||||
client,
|
||||
request,
|
||||
workspaceId,
|
||||
actor,
|
||||
dataset,
|
||||
rootNodeId: requestUrl.searchParams.get("rootNodeId")?.trim() || null,
|
||||
depth: readNumberParam(requestUrl, "depth"),
|
||||
}),
|
||||
});
|
||||
return {
|
||||
requestId: context.requestId,
|
||||
traceId: context.traceId,
|
||||
data: datasetWithFileTree,
|
||||
snapshot: {
|
||||
dataset: datasetWithFileTree,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
async start(controller) {
|
||||
try {
|
||||
for await (const frame of streamTreeFrames({
|
||||
workspaceId,
|
||||
rootNodeId: requestUrl.searchParams.get("rootNodeId"),
|
||||
initialCursor: requestUrl.searchParams.get("cursor"),
|
||||
pollMs: readNumberParam(requestUrl, "pollMs") ?? undefined,
|
||||
maxPolls: readNumberParam(requestUrl, "maxPolls"),
|
||||
loadOverview,
|
||||
loadSnapshot,
|
||||
})) {
|
||||
if (request.signal.aborted) {
|
||||
break;
|
||||
}
|
||||
controller.enqueue(encoder.encode(encodeSseFrame(frame.event, frame.payload)));
|
||||
}
|
||||
controller.close();
|
||||
} catch (error) {
|
||||
controller.error(error);
|
||||
}
|
||||
},
|
||||
cancel() {
|
||||
return undefined;
|
||||
},
|
||||
});
|
||||
|
||||
return new NextResponse(stream, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"content-type": "text/event-stream; charset=utf-8",
|
||||
"cache-control": "no-store",
|
||||
connection: "keep-alive",
|
||||
"x-upstream": "next-tree-stream-compat",
|
||||
"x-mnote-web-owner": "mnote-web",
|
||||
"x-mnote-tree-stream-owner": "rust-web",
|
||||
"x-mnote-compat-boundary": "mnote-web-stream-alias",
|
||||
},
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user