@principal-ai/subsystems-react 0.56.18 → 0.56.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -71,12 +71,29 @@ export function GraphLayerStyle() {
71
71
  return <style>{GRAPH_LAYER_CSS}</style>;
72
72
  }
73
73
 
74
+ /**
75
+ * Overrides for a non-interactive (presentation) canvas; spread after
76
+ * `GRAPH_NAV_PROPS`. Leaves `nodesDraggable` etc. as the shared defaults.
77
+ */
78
+ export const STATIC_NAV_PROPS = {
79
+ panOnDrag: false,
80
+ panOnScroll: false,
81
+ zoomOnScroll: false,
82
+ zoomOnPinch: false,
83
+ zoomOnDoubleClick: false,
84
+ elementsSelectable: false,
85
+ preventScrolling: false,
86
+ } satisfies Partial<ReactFlowProps>;
87
+
74
88
  /** Background dots + zoom/fit/interactive widget; render inside `<ReactFlow>`. */
75
- export function GraphChrome() {
89
+ export function GraphChrome({
90
+ showBackground = true,
91
+ showControls = true,
92
+ }: { showBackground?: boolean; showControls?: boolean } = {}) {
76
93
  return (
77
94
  <>
78
- <Background variant={BackgroundVariant.Dots} gap={16} size={1} />
79
- <Controls showZoom showFitView showInteractive />
95
+ {showBackground && <Background variant={BackgroundVariant.Dots} gap={16} size={1} />}
96
+ {showControls && <Controls showZoom showFitView showInteractive />}
80
97
  </>
81
98
  );
82
99
  }
@@ -43,6 +43,7 @@ import {
43
43
  describeConstructBreakdown,
44
44
  reorderTrails,
45
45
  reorderTargetIndex,
46
+ trailLayoutOrder,
46
47
  } from './model';
47
48
  import type { SubsystemComponent, SubsystemComponentEdge, SubsystemTrail } from './model';
48
49
 
@@ -1123,6 +1124,40 @@ describe('reorderTrails', () => {
1123
1124
  });
1124
1125
  });
1125
1126
 
1127
+ describe('trailLayoutOrder', () => {
1128
+ const trails: SubsystemTrail[] = [
1129
+ {
1130
+ id: 't1',
1131
+ title: 'T1',
1132
+ steps: [
1133
+ { from: 'c', to: 'd', mechanism: 'call' },
1134
+ { from: 'd', to: 'a', mechanism: 'call' },
1135
+ ],
1136
+ },
1137
+ { id: 't2', title: 'T2', steps: [{ from: 'b', to: 'ext', mechanism: 'http' }] },
1138
+ ];
1139
+
1140
+ test('ranks aliases by first appearance across trails, then the rest in given order', () => {
1141
+ const order = trailLayoutOrder(trails, ['a', 'b', 'c', 'd', 'e', 'external:ext']);
1142
+ expect([...order.entries()]).toEqual([
1143
+ ['c', 0],
1144
+ ['d', 1],
1145
+ ['a', 2],
1146
+ ['b', 3],
1147
+ ['external:ext', 4],
1148
+ ['e', 5],
1149
+ ]);
1150
+ });
1151
+
1152
+ test('skips trail aliases the graph does not draw and keeps ranks dense', () => {
1153
+ const order = trailLayoutOrder(trails, ['a', 'd']);
1154
+ expect([...order.entries()]).toEqual([['d', 0], ['a', 1]]);
1155
+ });
1156
+
1157
+ test('no trails: plain given order', () => {
1158
+ expect([...trailLayoutOrder(undefined, ['y', 'x']).entries()]).toEqual([['y', 0], ['x', 1]]);
1159
+ });
1160
+ });
1126
1161
  describe('reorderTargetIndex', () => {
1127
1162
  // Boundaries are the n+1 gaps between rows: 0 is above row 0, n is below
1128
1163
  // the last row. Dragging row `from` to boundary `b` lands it at b, minus one
@@ -1144,6 +1144,12 @@ export interface SubsystemGraphNodeData extends Record<string, unknown> {
1144
1144
  dimmed?: boolean;
1145
1145
  /** Diagnostics badge — see `SubsystemNodeIssue`. Absent when no findings. */
1146
1146
  issue?: SubsystemNodeIssue;
1147
+ /**
1148
+ * Preferred rank among same-layer nodes (lower = earlier along the cross
1149
+ * axis). Set by the layout from trail order; ELK may still deviate to avoid
1150
+ * crossings.
1151
+ */
1152
+ layoutOrder?: number;
1147
1153
  }
1148
1154
 
1149
1155
  /**
@@ -1801,6 +1807,33 @@ export function convertSubsystemToGroups(
1801
1807
  }));
1802
1808
  }
1803
1809
 
1810
+ /**
1811
+ * Rank node ids for same-layer ordering: every alias in first-appearance order
1812
+ * across the trails' steps (`from` before `to`, trail by trail), then any
1813
+ * remaining id in the order given. Ids the trails name but the graph lacks are
1814
+ * skipped so ranks stay dense.
1815
+ */
1816
+ export function trailLayoutOrder(
1817
+ trails: readonly SubsystemTrail[] | undefined,
1818
+ nodeIds: readonly string[],
1819
+ ): Map<string, number> {
1820
+ const known = new Set(nodeIds);
1821
+ const order = new Map<string, number>();
1822
+ const rank = (alias: string) => {
1823
+ // A target that is not a component is drawn as an `external:` stub.
1824
+ const id = known.has(alias) ? alias : `external:${alias}`;
1825
+ if (known.has(id) && !order.has(id)) order.set(id, order.size);
1826
+ };
1827
+ for (const trail of trails ?? []) {
1828
+ for (const step of trail.steps) {
1829
+ rank(step.from);
1830
+ rank(step.to);
1831
+ }
1832
+ }
1833
+ for (const id of nodeIds) rank(id);
1834
+ return order;
1835
+ }
1836
+
1804
1837
  /**
1805
1838
  * Convert a subsystem graph document into React Flow edges. Edges whose target
1806
1839
  * is an external label (not a component alias) point at a synthetic stub so the
@@ -2028,6 +2061,15 @@ export async function buildSubsystemGraph(
2028
2061
  }
2029
2062
  }
2030
2063
 
2064
+ // Same-layer order follows the trails: a component ranks by the first step
2065
+ // that mentions it, so a trail reads top-to-bottom (or left-to-right) as it
2066
+ // fans out. Components no trail touches follow in declaration order.
2067
+ const order = trailLayoutOrder(doc.trails, nodes.map((n) => n.id));
2068
+ for (const n of nodes) {
2069
+ const rank = order.get(n.id);
2070
+ if (rank !== undefined) (n.data as SubsystemGraphNodeData).layoutOrder = rank;
2071
+ }
2072
+
2031
2073
  // ELK auto-layout: nested compound parents (process → module → leaves).
2032
2074
  let placedNodes = nodes;
2033
2075
  let labelPositions = new Map<string, { x: number; y: number }>();
@@ -103,7 +103,8 @@ export interface ElkLayoutOptions {
103
103
  /**
104
104
  * Order same-layer nodes by their source line (ascending) instead of leaving
105
105
  * it to the crossing-minimizer. Nodes must carry a `data.component.line`
106
- * (used when set; nodes without one keep ELK's neutral ordering). Off by
106
+ * (used when set; nodes without one keep ELK's neutral ordering). A
107
+ * `data.layoutOrder` number on a node always wins over the line. Off by
107
108
  * default — the generic layered layout is tuned for crossings, and line order
108
109
  * only makes sense for graphs that model a source region top-to-bottom.
109
110
  * @default false
@@ -503,8 +504,8 @@ export function getElkOptions(options: ElkLayoutOptions): LayoutOptions {
503
504
  const baseOptions: LayoutOptions = {
504
505
  'elk.algorithm': 'layered',
505
506
  'elk.direction': direction,
506
- // Keep same-layer ordering stable (not reversed / randomized) so a
507
- // line-ordered rewrite or ELK's own ordering is predictable.
507
+ // Honour per-node `elk.position` hints (see `computeElkLayout`) as the
508
+ // preferred same-layer order; the sweep may still deviate to cut crossings.
508
509
  'elk.layered.crossingMinimization.semiInteractive': 'true',
509
510
  // Spacing
510
511
  'elk.spacing.nodeNode': String(nodeSpacing),
@@ -530,9 +531,7 @@ export function getElkOptions(options: ElkLayoutOptions): LayoutOptions {
530
531
  'elk.layered.thoroughness': '50',
531
532
  };
532
533
 
533
- // Model order (position hints) so a per-node `data.line` can drive same-layer
534
- // ordering. ELK honours the `y` of each node as a position hint against the
535
- // model order.
534
+ // Line order also wants model order respected where positions tie.
536
535
  if (orderByLine) {
537
536
  baseOptions['elk.layered.fixedAlignment'] = 'NONE';
538
537
  baseOptions['elk.layered.considerModelOrder.strategy'] = 'NODES_AND_EDGES';
@@ -807,12 +806,20 @@ export async function computeElkLayout(
807
806
  const nd = node.data as { component?: { layer?: number }; layer?: number } | undefined;
808
807
  layer = nd?.layer ?? nd?.component?.layer;
809
808
 
810
- // Optional source-line ordering: seed each node's `y` hint from its line so
811
- // ELK's model-order placement puts lower lines further down within a layer.
812
- let y = node.position.y;
813
- if (orderByLine) {
814
- const line = (node.data as { component?: { line?: number } } | undefined)?.component?.line;
815
- if (typeof line === 'number') y = line;
809
+ // Same-layer order hint. Semi-interactive crossing minimization reads the
810
+ // `elk.position` PROPERTY, not the node's x/y coordinates, so a hint only
811
+ // takes effect when written there. `data.layoutOrder` is an explicit rank
812
+ // from the caller; `orderByLine` falls back to the component's line. Nodes
813
+ // with neither get no property and keep ELK's own ordering. The same value
814
+ // goes on both axes so the hint reads the same whichever axis the layout
815
+ // direction makes the in-layer one.
816
+ let orderHint: number | undefined;
817
+ const hintData = node.data as
818
+ | { layoutOrder?: number; component?: { line?: number } }
819
+ | undefined;
820
+ if (typeof hintData?.layoutOrder === 'number') orderHint = hintData.layoutOrder;
821
+ else if (orderByLine && typeof hintData?.component?.line === 'number') {
822
+ orderHint = hintData.component.line;
816
823
  }
817
824
 
818
825
  return {
@@ -820,11 +827,12 @@ export async function computeElkLayout(
820
827
  width,
821
828
  height,
822
829
  x: node.position.x,
823
- y,
830
+ y: node.position.y,
824
831
  // Add ports on each side for edge connections
825
832
  ports: cardinalPorts(node.id),
826
833
  properties: {
827
834
  'portConstraints': 'FIXED_SIDE',
835
+ ...(orderHint !== undefined ? { 'elk.position': `(${orderHint},${orderHint})` } : {}),
828
836
  ...(layer !== undefined ? { 'layering.layer': String(layer) } : {}),
829
837
  ...(partitionByNode ? { partition: String(partitionByNode.get(node.id) ?? 0) } : {}),
830
838
  },