2026-04-23 07:38:34 +08:00
|
|
|
import { getAuthedConvexClient } from "@/lib/convex/route";
|
2026-04-22 05:57:06 +08:00
|
|
|
import {
|
2026-04-23 07:38:34 +08:00
|
|
|
buildDocumentBridgeContext,
|
|
|
|
|
buildDocumentQueryEnvelope,
|
|
|
|
|
documentBridgeErrorResponse,
|
|
|
|
|
} from "@/lib/documents/bridge";
|
|
|
|
|
import {
|
|
|
|
|
executeRustBridgeQueryTransport,
|
|
|
|
|
resolveRustBridgeQueryPlan,
|
|
|
|
|
} from "@/lib/documents/rust-runtime";
|
|
|
|
|
import { mapSidebarDatasetListQueryResultToInitialData } from "@/lib/sidebar-data";
|
2026-04-22 05:57:06 +08:00
|
|
|
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
2026-04-23 07:38:34 +08:00
|
|
|
function toSseFrame(event: string, data: unknown) {
|
|
|
|
|
return `event: ${event}\ndata: ${JSON.stringify(data ?? null)}\n\n`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 05:57:06 +08:00
|
|
|
export async function GET(request: Request) {
|
|
|
|
|
try {
|
|
|
|
|
const requestUrl = new URL(request.url);
|
|
|
|
|
const workspaceId = String(requestUrl.searchParams.get("workspaceId") || "").trim();
|
|
|
|
|
const cursor = String(requestUrl.searchParams.get("cursor") || "").trim() || null;
|
|
|
|
|
|
|
|
|
|
if (!workspaceId) {
|
|
|
|
|
return Response.json({ error: "缺少 workspaceId" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 07:38:34 +08:00
|
|
|
const { client } = await getAuthedConvexClient();
|
|
|
|
|
const context = await buildDocumentBridgeContext({
|
|
|
|
|
request,
|
|
|
|
|
workspaceId,
|
2026-04-22 05:57:06 +08:00
|
|
|
});
|
|
|
|
|
|
2026-04-23 07:38:34 +08:00
|
|
|
const sidebarEnvelope = buildDocumentQueryEnvelope({
|
|
|
|
|
name: "sidebar.dataset.list",
|
|
|
|
|
payload: {
|
|
|
|
|
workspaceId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const sidebarPlan = await resolveRustBridgeQueryPlan({
|
|
|
|
|
context,
|
|
|
|
|
envelope: sidebarEnvelope,
|
|
|
|
|
});
|
|
|
|
|
const sidebarDataset = await executeRustBridgeQueryTransport({
|
|
|
|
|
client,
|
|
|
|
|
plan: sidebarPlan,
|
|
|
|
|
});
|
2026-04-22 05:57:06 +08:00
|
|
|
|
2026-04-23 07:38:34 +08:00
|
|
|
const overviewEnvelope = buildDocumentQueryEnvelope({
|
|
|
|
|
name: "bridge.workspace.overview",
|
|
|
|
|
payload: {
|
|
|
|
|
workspaceId,
|
|
|
|
|
limit: 20,
|
|
|
|
|
cursor,
|
|
|
|
|
commandStatus: null,
|
|
|
|
|
eventStatus: null,
|
|
|
|
|
targetPageId: null,
|
|
|
|
|
targetBlockId: null,
|
|
|
|
|
aggregateType: null,
|
|
|
|
|
aggregateId: null,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const overviewPlan = await resolveRustBridgeQueryPlan({
|
|
|
|
|
context,
|
|
|
|
|
envelope: overviewEnvelope,
|
|
|
|
|
});
|
|
|
|
|
const overview = await executeRustBridgeQueryTransport({
|
|
|
|
|
client,
|
|
|
|
|
plan: overviewPlan,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
kind: "snapshot",
|
|
|
|
|
stream: "workspace",
|
|
|
|
|
projection: "sidebar_tree",
|
|
|
|
|
workspaceId,
|
|
|
|
|
rootNodeId: null,
|
|
|
|
|
cursor,
|
|
|
|
|
requestId: context.requestId,
|
|
|
|
|
traceId: context.traceId,
|
|
|
|
|
data: mapSidebarDatasetListQueryResultToInitialData(sidebarDataset),
|
|
|
|
|
snapshot: {
|
|
|
|
|
dataset: sidebarDataset,
|
|
|
|
|
tree: sidebarDataset.kernel_sidebar_projection ?? sidebarDataset.kernelSidebarProjection ?? null,
|
|
|
|
|
},
|
|
|
|
|
overview,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new Response(toSseFrame("snapshot", payload), {
|
|
|
|
|
status: 200,
|
|
|
|
|
headers: {
|
|
|
|
|
"content-type": "text/event-stream; charset=utf-8",
|
|
|
|
|
"cache-control": "no-store",
|
|
|
|
|
},
|
2026-04-22 05:57:06 +08:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return documentBridgeErrorResponse(error);
|
|
|
|
|
}
|
|
|
|
|
}
|