AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(diagram): balance wide architecture layouts
tianma-ifb64772a46bc7
diff --git a/apps/api/src/mcp-tools.ts b/apps/api/src/mcp-tools.tsindex 1df147f2..5ad9b131 100644--- a/apps/api/src/mcp-tools.ts+++ b/apps/api/src/mcp-tools.ts@@ -330,7 +330,7 @@ const MCP_TOOL_DEFINITIONS = [{name: "create_diagram_memo",description:- "Create an editable visual diagram memo from a semantic graph; EdgeEver generates node sizes, coordinates, edge IDs, and a deterministic layout. Mind maps and flowcharts always start with the default plain theme; any supplied theme is ignored for those kinds. For mind maps, omit node type and use parentId for hierarchy. Flowchart node types are process, decision, start, or end. Architecture node types are client, frontend, service, database, storage, queue, security, external, or boundary; boundary nodes may contain nodes through parentId but cannot be edge endpoints.",+ "Create an editable visual diagram memo from a semantic graph; EdgeEver generates node sizes, coordinates, edge IDs, and a deterministic layout. Mind maps and flowcharts always start with the default plain theme; any supplied theme is ignored for those kinds. For mind maps, omit node type and use parentId for hierarchy. Flowchart node types are process, decision, start, or end. Architecture node types are client, frontend, service, database, storage, queue, security, external, or boundary; boundary nodes contain components through those components' parentId and cannot be edge endpoints. Give every architecture boundary at least one contained component, or omit the boundary rather than creating an empty section.",inputSchema: {type: "object",required: ["notebookId", "kind", "nodes"],diff --git a/apps/web/src/lib/diagram-layout.test.mjs b/apps/web/src/lib/diagram-layout.test.mjsindex ec8c4803..e55bae6f 100644--- a/apps/web/src/lib/diagram-layout.test.mjs+++ b/apps/web/src/lib/diagram-layout.test.mjs@@ -180,6 +180,67 @@ describe("diagram auto layout", () => {expect(compactArchitectureNodeSize("boundary", { width: 640, height: 360 })).toEqual({ width: 640, height: 360 });});+ test("turns a long ungrouped architecture pipeline downward on creation and auto layout", () => {+ const nodes = Array.from({ length: 17 }, (_, index) => ({+ id: `service-${index}`,+ label: `Service ${index}`,+ type: "service",+ }));+ const edges = nodes.slice(1).map((node, index) => ({ source: nodes[index].id, target: node.id }));+ const document = compileDiagramIr({ kind: "architecture", nodes, edges });+ const bounds = (geometry) => ({+ width: Math.max(...nodes.map((node) => geometry[node.id].x + geometry[node.id].width))+ - Math.min(...nodes.map((node) => geometry[node.id].x)),+ height: Math.max(...nodes.map((node) => geometry[node.id].y + geometry[node.id].height))+ - Math.min(...nodes.map((node) => geometry[node.id].y)),+ });+ const created = Object.fromEntries(document.nodes.map((node) => [node.id, node]));+ expect(bounds(created).width).toBeLessThanOrEqual(1480);+ expect(bounds(created).height).toBeGreaterThan(bounds(created).width);+ const relayout = computeDiagramLayoutResult(document);+ expect(bounds(relayout.nodes).width).toBeLessThanOrEqual(1480);+ expect(relayout.nodes["service-0"].y).toBeLessThan(relayout.nodes["service-16"].y);+ });++ test("keeps a long architecture pipeline inside one boundary within the width limit", () => {+ const nodes = [{ id: "system", label: "System", type: "boundary" }];+ const edges = [];+ for (let index = 0; index < 12; index += 1) {+ const id = `service-${index}`;+ nodes.push({ id, label: `Service ${index}`, type: "service", parentId: "system" });+ if (index > 0) edges.push({ source: `service-${index - 1}`, target: id });+ }+ const document = compileDiagramIr({ kind: "architecture", nodes, edges });+ const system = document.nodes.find((node) => node.id === "system");+ expect(system.width).toBeLessThanOrEqual(1480);+ expect(system.height).toBeGreaterThan(system.width);+ for (const node of document.nodes.filter((item) => item.parentId === "system")) {+ expect(node.x).toBeGreaterThan(system.x);+ expect(node.x + node.width).toBeLessThan(system.x + system.width);+ }+ });++ test("places unassigned architecture boundaries after the pipeline without covering nodes", () => {+ const services = Array.from({ length: 14 }, (_, index) => ({+ id: `service-${index}`,+ label: `Service ${index}`,+ type: "service",+ }));+ const boundaries = ["访问层", "业务服务层", "数据与基础设施层"].map((label, index) => ({+ id: `boundary-${index}`,+ label,+ type: "boundary",+ }));+ const edges = services.slice(1).map((node, index) => ({ source: services[index].id, target: node.id }));+ const document = compileDiagramIr({ kind: "architecture", nodes: [...services, ...boundaries], edges });+ const result = computeDiagramLayoutResult(document);+ const bottom = Math.max(...services.map((node) => result.nodes[node.id].y + result.nodes[node.id].height));+ for (const boundary of boundaries) expect(result.nodes[boundary.id].y).toBeGreaterThan(bottom);+ const width = Math.max(...Object.values(result.nodes).map((node) => node.x + node.width))+ - Math.min(...Object.values(result.nodes).map((node) => node.x));+ expect(width).toBeLessThanOrEqual(1480);+ });+test("separates generated architecture boundaries and keeps their children inside", () => {const document = compileDiagramIr({kind: "architecture",diff --git a/packages/shared/src/diagram-layout.ts b/packages/shared/src/diagram-layout.tsindex b3268dc5..0805936f 100644--- a/packages/shared/src/diagram-layout.ts+++ b/packages/shared/src/diagram-layout.ts@@ -502,6 +502,23 @@ const layoutDagreGraph = (}));};+const layoutArchitectureGraph = (+ nodes: Array<{ id: string; width: number; height: number }>,+ edges: Array<{ source: string; target: string }>,+ direction: "left-to-right" | "top-to-bottom",+ spacing: { rank: number; node: number },+ margin: { x: number; y: number },+ maxWidth = ARCHITECTURE_LAYOUT_ROW_WIDTH,+): DiagramLayoutPositions => {+ const positions = layoutDagreGraph(nodes, edges, direction, spacing, margin);+ if (direction === "top-to-bottom" || nodes.length < 2) return positions;+ const width = (candidate: DiagramLayoutPositions) => Math.max(...nodes.map((node) => candidate[node.id].x + node.width))+ - Math.min(...nodes.map((node) => candidate[node.id].x));+ if (width(positions) <= maxWidth) return positions;+ const vertical = layoutDagreGraph(nodes, edges, "top-to-bottom", spacing, margin);+ return width(vertical) < width(positions) ? vertical : positions;+};+const wrapArchitectureGroups = (document: DiagramDocument,positions: DiagramLayoutPositions,@@ -596,6 +613,51 @@ const placeUnpositionedArchitectureNodes = (return positions;};+const placeEmptyArchitectureBoundaries = (+ document: DiagramDocument,+ positions: DiagramLayoutPositions,+) => {+ const nodeById = new Map(document.nodes.map((node) => [node.id, node]));+ const empty = document.nodes.filter((node) => (+ node.shape === "boundary"+ && !node.parentId+ && !document.nodes.some((member) => (+ member.shape !== "boundary" && architectureTopLevelBoundaryId(member.id, nodeById) === node.id+ ))+ )).sort((left, right) => left.y - right.y || left.x - right.x || left.id.localeCompare(right.id));+ if (empty.length === 0) return positions;++ const emptyIds = new Set(empty.map((node) => node.id));+ const placed = document.nodes.filter((node) => (+ positions[node.id]+ && !emptyIds.has(node.id)+ && !empty.some((boundary) => architectureTopLevelBoundaryId(node.id, nodeById) === boundary.id)+ ));+ const contentBottom = placed.length > 0+ ? Math.max(...placed.map((node) => positions[node.id].y + node.height))+ : ARCHITECTURE_ORIGIN - ARCHITECTURE_GROUP_VERTICAL_GAP;+ let cursorX = ARCHITECTURE_ORIGIN;+ let cursorY = contentBottom + ARCHITECTURE_GROUP_VERTICAL_GAP;+ let rowHeight = 0;+ for (const boundary of empty) {+ if (cursorX > ARCHITECTURE_ORIGIN+ && cursorX + boundary.width > ARCHITECTURE_ORIGIN + ARCHITECTURE_LAYOUT_ROW_WIDTH) {+ cursorX = ARCHITECTURE_ORIGIN;+ cursorY += rowHeight + ARCHITECTURE_GROUP_VERTICAL_GAP;+ rowHeight = 0;+ }+ const deltaX = cursorX - positions[boundary.id].x;+ const deltaY = cursorY - positions[boundary.id].y;+ for (const node of document.nodes) {+ if (node.id !== boundary.id && architectureTopLevelBoundaryId(node.id, nodeById) !== boundary.id) continue;+ positions[node.id] = { x: positions[node.id].x + deltaX, y: positions[node.id].y + deltaY };+ }+ cursorX += boundary.width + ARCHITECTURE_GROUP_HORIZONTAL_GAP;+ rowHeight = Math.max(rowHeight, boundary.height);+ }+ return positions;+};+const computeDagreLayout = (document: DiagramDocument,options: DiagramLayoutOptions,@@ -733,14 +795,31 @@ const computeArchitectureLayout = (document: DiagramDocument, options: DiagramLareturn members.length > 0 ? [{ boundary, members }] : [];});if (groups.length === 0) {- return computeDagreLayout(document, { ...options, direction }, true, ARCHITECTURE_LAYOUT_SPACING);+ const positions = layoutArchitectureGraph(+ document.nodes.filter((node) => node.shape !== "boundary"),+ document.edges,+ direction,+ ARCHITECTURE_LAYOUT_SPACING,+ { x: 32, y: 32 },+ );+ for (const boundary of document.nodes.filter((node) => node.shape === "boundary")) {+ positions[boundary.id] = { x: boundary.x, y: boundary.y };+ }+ return placeEmptyArchitectureBoundaries(document, positions);}const innerByGroup = new Map<string, DiagramLayoutPositions>();const groupMetaNodes = groups.map((group) => {const memberIds = new Set(group.members.map((node) => node.id));const intraEdges = document.edges.filter((edge) => memberIds.has(edge.source) && memberIds.has(edge.target));- const inner = layoutDagreGraph(group.members, intraEdges, direction, ARCHITECTURE_LAYOUT_SPACING, { x: 0, y: 0 });+ const inner = layoutArchitectureGraph(+ group.members,+ intraEdges,+ direction,+ ARCHITECTURE_LAYOUT_SPACING,+ { x: 0, y: 0 },+ ARCHITECTURE_LAYOUT_ROW_WIDTH - ARCHITECTURE_GROUP_PAD_X * 2,+ );const left = Math.min(...group.members.map((node) => inner[node.id].x));const top = Math.min(...group.members.map((node) => inner[node.id].y));const normalized = Object.fromEntries(group.members.map((node) => [node.id, {@@ -790,12 +869,13 @@ const computeArchitectureLayout = (document: DiagramDocument, options: DiagramLa}const metaMargin = { x: ARCHITECTURE_ORIGIN, y: ARCHITECTURE_ORIGIN };- const metaLr = layoutDagreGraph(metaNodes, metaEdges, direction, ARCHITECTURE_META_SPACING, metaMargin);- const lrWidth = Math.max(...metaNodes.map((node) => metaLr[node.id].x + node.width))- - Math.min(...metaNodes.map((node) => metaLr[node.id].x));- const metaPositions = groups.length > 1 && lrWidth > ARCHITECTURE_LAYOUT_ROW_WIDTH- ? layoutDagreGraph(metaNodes, metaEdges, "top-to-bottom", ARCHITECTURE_META_SPACING, metaMargin)- : metaLr;+ const metaPositions = layoutArchitectureGraph(+ metaNodes,+ metaEdges,+ direction,+ ARCHITECTURE_META_SPACING,+ metaMargin,+ );const positions: DiagramLayoutPositions = Object.fromEntries(document.nodes.filter((node) => node.shape === "boundary").map((node) => [node.id, { x: node.x, y: node.y }]),@@ -817,7 +897,7 @@ const computeArchitectureLayout = (document: DiagramDocument, options: DiagramLawrapArchitectureGroups(document, positions);placeUnpositionedArchitectureNodes(document, positions);- return positions;+ return placeEmptyArchitectureBoundaries(document, positions);};const irNodeShape = (kind: DiagramKind, type: DiagramIrNodeType | undefined): DiagramNodeShape => {