@principal-ai/subsystems-react 0.56.13 → 0.56.14

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.
Files changed (38) hide show
  1. package/dist/index.d.ts +6 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +3 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/subsystem/C4ProjectionView.d.ts +60 -0
  6. package/dist/subsystem/C4ProjectionView.d.ts.map +1 -0
  7. package/dist/subsystem/C4ProjectionView.js +336 -0
  8. package/dist/subsystem/C4ProjectionView.js.map +1 -0
  9. package/dist/subsystem/C4SketchView.d.ts +23 -0
  10. package/dist/subsystem/C4SketchView.d.ts.map +1 -0
  11. package/dist/subsystem/C4SketchView.js +119 -0
  12. package/dist/subsystem/C4SketchView.js.map +1 -0
  13. package/dist/subsystem/__fixtures__/c4Drilldown.d.ts +4 -27
  14. package/dist/subsystem/__fixtures__/c4Drilldown.d.ts.map +1 -1
  15. package/dist/subsystem/__fixtures__/c4Drilldown.js +4 -56
  16. package/dist/subsystem/__fixtures__/c4Drilldown.js.map +1 -1
  17. package/dist/subsystem/__fixtures__/c4Sketch.d.ts +9 -0
  18. package/dist/subsystem/__fixtures__/c4Sketch.d.ts.map +1 -0
  19. package/dist/subsystem/__fixtures__/c4Sketch.js +89 -0
  20. package/dist/subsystem/__fixtures__/c4Sketch.js.map +1 -0
  21. package/dist/subsystem/c4Sketch.d.ts +74 -0
  22. package/dist/subsystem/c4Sketch.d.ts.map +1 -0
  23. package/dist/subsystem/c4Sketch.js +9 -0
  24. package/dist/subsystem/c4Sketch.js.map +1 -0
  25. package/dist/subsystem/c4TrailFocus.d.ts +30 -0
  26. package/dist/subsystem/c4TrailFocus.d.ts.map +1 -0
  27. package/dist/subsystem/c4TrailFocus.js +57 -0
  28. package/dist/subsystem/c4TrailFocus.js.map +1 -0
  29. package/package.json +1 -1
  30. package/src/index.ts +14 -0
  31. package/src/stories/Canvases/C4/C4Sketch.stories.tsx +36 -0
  32. package/src/stories/Canvases/C4/C4ToComponentDrill.stories.tsx +18 -429
  33. package/src/subsystem/C4ProjectionView.tsx +595 -0
  34. package/src/subsystem/C4SketchView.tsx +241 -0
  35. package/src/subsystem/__fixtures__/c4Drilldown.ts +7 -58
  36. package/src/subsystem/__fixtures__/c4Sketch.ts +94 -0
  37. package/src/subsystem/c4Sketch.ts +78 -0
  38. package/src/subsystem/c4TrailFocus.ts +62 -0
@@ -0,0 +1,241 @@
1
+ /**
2
+ * C4SketchView — a standalone authored C4 hypothesis.
3
+ *
4
+ * Pure presentation: the host loads the sketch and passes it in. There is no
5
+ * model comparison, grounding, or promotion here; a sketch is a diagram in its
6
+ * own right.
7
+ */
8
+
9
+ import { useCallback, useMemo, useState } from 'react';
10
+ import type { CSSProperties, ReactNode } from 'react';
11
+ import { useTheme } from '@principal-ade/industry-theme';
12
+ import { PencilRuler } from 'lucide-react';
13
+ import { IndustryMarkdownSlide } from 'themed-markdown';
14
+ import { C4Graph } from './C4Graph';
15
+ import { GraphWithSidebar } from './GraphWithSidebar';
16
+ import type { C4Sketch } from './c4Sketch';
17
+
18
+ export interface C4SketchViewProps {
19
+ sketch: C4Sketch;
20
+ /** Controlled open-container ids. Omit to let the view own them. */
21
+ openContainerIds?: readonly string[];
22
+ onOpenContainerIdsChange?: (ids: string[]) => void;
23
+ /** Extra controls in the header. */
24
+ headerActions?: ReactNode;
25
+ /** Sidebar width persistence key. */
26
+ persistKey?: string;
27
+ /** Outer style for the flex fill. */
28
+ style?: CSSProperties;
29
+ }
30
+
31
+ export function C4SketchView({
32
+ sketch,
33
+ openContainerIds,
34
+ onOpenContainerIdsChange,
35
+ headerActions,
36
+ persistKey,
37
+ style,
38
+ }: C4SketchViewProps) {
39
+ const { theme } = useTheme();
40
+ const [internalOpenIds, setInternalOpenIds] = useState<string[]>(() =>
41
+ [...(openContainerIds ?? sketch.model.openContainerIds ?? [])].sort(),
42
+ );
43
+ const resolvedOpenIds = openContainerIds
44
+ ? [...openContainerIds]
45
+ : internalOpenIds;
46
+ const setOpenIds = useCallback(
47
+ (next: string[]) => {
48
+ if (!openContainerIds) setInternalOpenIds(next);
49
+ onOpenContainerIdsChange?.(next);
50
+ },
51
+ [onOpenContainerIdsChange, openContainerIds],
52
+ );
53
+
54
+ const model = useMemo(
55
+ () => ({ ...sketch.model, openContainerIds: resolvedOpenIds }),
56
+ [resolvedOpenIds, sketch.model],
57
+ );
58
+
59
+ const toggleContainer = useCallback(
60
+ (id: string) => {
61
+ setOpenIds(
62
+ resolvedOpenIds.includes(id)
63
+ ? resolvedOpenIds.filter((openId) => openId !== id)
64
+ : [...resolvedOpenIds, id].sort(),
65
+ );
66
+ },
67
+ [resolvedOpenIds, setOpenIds],
68
+ );
69
+
70
+ const openAll = useCallback(() => {
71
+ setOpenIds(
72
+ sketch.model.nodes
73
+ .filter((node) => node.kind === 'container')
74
+ .map((node) => node.id)
75
+ .sort(),
76
+ );
77
+ }, [setOpenIds, sketch.model.nodes]);
78
+
79
+ const collapseAll = useCallback(() => setOpenIds([]), [setOpenIds]);
80
+
81
+ const notes = sketch.notes?.trim();
82
+ const notesPanel = notes ? (
83
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
84
+ <div
85
+ style={{
86
+ fontSize: 10,
87
+ fontFamily: theme.fonts.monospace,
88
+ letterSpacing: 0.4,
89
+ color: theme.colors.textSecondary,
90
+ }}
91
+ >
92
+ ABOUT
93
+ </div>
94
+ <IndustryMarkdownSlide
95
+ content={notes}
96
+ slideIdPrefix={`c4-sketch-notes-${sketch.id}`}
97
+ slideIndex={0}
98
+ isVisible={true}
99
+ theme={theme}
100
+ disableScroll={true}
101
+ disableBasePadding
102
+ enableKeyboardScrolling={false}
103
+ autoFocusOnVisible={false}
104
+ />
105
+ </div>
106
+ ) : null;
107
+
108
+ return (
109
+ <div
110
+ style={{
111
+ width: '100%',
112
+ height: '100%',
113
+ display: 'flex',
114
+ flexDirection: 'column',
115
+ background: theme.colors.background,
116
+ ...style,
117
+ }}
118
+ >
119
+ <header
120
+ style={{
121
+ flexShrink: 0,
122
+ display: 'flex',
123
+ alignItems: 'center',
124
+ gap: 12,
125
+ flexWrap: 'wrap',
126
+ padding: '12px 16px',
127
+ background: theme.colors.background,
128
+ borderBottom: `1px solid ${theme.colors.border}`,
129
+ }}
130
+ >
131
+ <div
132
+ style={{
133
+ minWidth: 240,
134
+ flex: 1,
135
+ display: 'flex',
136
+ alignItems: 'center',
137
+ gap: 8,
138
+ flexWrap: 'wrap',
139
+ }}
140
+ >
141
+ <h2
142
+ style={{
143
+ margin: 0,
144
+ minWidth: 0,
145
+ fontSize: theme.fontSizes[2],
146
+ fontWeight: 600,
147
+ color: theme.colors.text,
148
+ fontFamily: theme.fonts.monospace,
149
+ overflowWrap: 'anywhere',
150
+ }}
151
+ >
152
+ {sketch.title}
153
+ </h2>
154
+ <span
155
+ style={{
156
+ display: 'inline-flex',
157
+ alignItems: 'center',
158
+ gap: 4,
159
+ padding: '1px 7px',
160
+ borderRadius: 4,
161
+ border: '1px solid #e3b341',
162
+ color: '#e3b341',
163
+ fontFamily: theme.fonts.monospace,
164
+ fontSize: 10,
165
+ letterSpacing: 0.4,
166
+ flexShrink: 0,
167
+ }}
168
+ >
169
+ <PencilRuler size={10} aria-hidden="true" />
170
+ SKETCH
171
+ </span>
172
+ </div>
173
+ <div
174
+ style={{
175
+ marginLeft: 'auto',
176
+ flexShrink: 0,
177
+ display: 'flex',
178
+ alignItems: 'center',
179
+ gap: 6,
180
+ flexWrap: 'wrap',
181
+ }}
182
+ >
183
+ {headerActions}
184
+ <button
185
+ type="button"
186
+ onClick={openAll}
187
+ style={smallButton(theme, { width: 132 })}
188
+ >
189
+ Open all
190
+ </button>
191
+ <button
192
+ type="button"
193
+ onClick={collapseAll}
194
+ style={smallButton(theme, { width: 132 })}
195
+ >
196
+ Collapse all
197
+ </button>
198
+ </div>
199
+ </header>
200
+
201
+ <div style={{ flex: 1, minHeight: 0 }}>
202
+ <GraphWithSidebar
203
+ hidden={!notes}
204
+ stretchHeader
205
+ defaultWidth={320}
206
+ minWidth={240}
207
+ maxWidth={480}
208
+ persistKey={persistKey ?? `c4-sketch-notes-${sketch.id}`}
209
+ header={notesPanel}
210
+ >
211
+ <C4Graph model={model} onToggleContainer={toggleContainer} />
212
+ </GraphWithSidebar>
213
+ </div>
214
+ </div>
215
+ );
216
+ }
217
+
218
+ function smallButton(
219
+ theme: ReturnType<typeof useTheme>['theme'],
220
+ opts: { width?: number } = {},
221
+ ) {
222
+ return {
223
+ flexShrink: 0,
224
+ display: 'inline-flex',
225
+ alignItems: 'center',
226
+ justifyContent: 'center',
227
+ gap: 5,
228
+ ...(opts.width ? { width: opts.width } : {}),
229
+ height: 34,
230
+ padding: '0 10px',
231
+ boxSizing: 'border-box',
232
+ whiteSpace: 'nowrap',
233
+ borderRadius: 6,
234
+ border: `1px solid ${theme.colors.border}`,
235
+ background: 'transparent',
236
+ color: theme.colors.textSecondary,
237
+ fontFamily: theme.fonts.body,
238
+ fontSize: theme.fontSizes[1],
239
+ cursor: 'pointer',
240
+ } as const;
241
+ }
@@ -753,65 +753,14 @@ export function rollupEdgeTrailRefs(opts: {
753
753
  }
754
754
 
755
755
  /**
756
- * The C4 elements a trail touches — every element that owns a step endpoint.
757
- *
758
- * This is the reverse of the memberships above: given a step's `from`/`to`
759
- * alias, which C4 box(es) hold it. Because a container's own process-claim
760
- * membership AND the authored components nested inside it both claim the
761
- * aliases, BOTH end up in the set: the container is the visible card at
762
- * container view, its components are the visible cards when it is open, so
763
- * either view stays honest.
764
- *
765
- * The complement of the result (all element ids minus these) is what a
766
- * trail-driven emphasis pass should dim. Pure, and the spec the story's
767
- * "players stay bright" is asserted against.
768
- */
769
- export function deriveTrailPlayerElements(
770
- steps: SubsystemTrail['steps'],
771
- members: Readonly<Record<string, readonly string[]>>,
772
- ): Set<string> {
773
- const byAlias = new Map<string, string[]>();
774
- for (const [elementId, aliases] of Object.entries(members)) {
775
- for (const alias of aliases) {
776
- const list = byAlias.get(alias);
777
- if (list) list.push(elementId);
778
- else byAlias.set(alias, [elementId]);
779
- }
780
- }
781
- const players = new Set<string>();
782
- for (const step of steps) {
783
- for (const id of byAlias.get(step.from) ?? []) players.add(id);
784
- for (const id of byAlias.get(step.to) ?? []) players.add(id);
785
- }
786
- return players;
787
- }
788
-
789
- /**
790
- * The C4 edges that carry the current focus — the line-level half of the
791
- * emphasis pair above.
792
- *
793
- * An edge is lit iff its authored `trailRefs` cover the source: the whole
794
- * trail, or one specific step of it. A step that is entirely internal to a
795
- * container folds into no C4 line (the rollup only keeps cross-boundary
796
- * hops), so its source lights NOTHING — every line dims, which is honest:
797
- * no drawn edge represents that hop.
756
+ * Trail-focus helpers live in production code now (`c4TrailFocus.ts`); keep
757
+ * these re-exports so the fixture's tests and the drill story can import them
758
+ * from the same place they always did.
798
759
  */
799
- export function deriveLitC4Edges(
800
- source: { trailId: string; stepIndex?: number },
801
- edges: readonly C4Edge[],
802
- ): Set<string> {
803
- const lit = new Set<string>();
804
- for (const edge of edges) {
805
- for (const ref of edge.trailRefs ?? []) {
806
- if (ref.trailId !== source.trailId) continue;
807
- if (source.stepIndex == null || ref.stepIndices.includes(source.stepIndex)) {
808
- lit.add(edge.id);
809
- break;
810
- }
811
- }
812
- }
813
- return lit;
814
- }
760
+ export {
761
+ deriveLitC4Edges,
762
+ deriveTrailPlayerElements,
763
+ } from '../c4TrailFocus';
815
764
 
816
765
  /**
817
766
  * Process claims the accepted C4 element set backs NOT at all — the set the
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Storybook fixture for an authored C4 sketch.
3
+ *
4
+ * A standalone hypothesis: containers, a component, an external, a person,
5
+ * and two authored lines. No model or grounding is involved.
6
+ */
7
+
8
+ import type { C4Sketch } from '../c4Sketch';
9
+
10
+ const PURL = 'pkg:github/acme/widget';
11
+
12
+ export const SKETCH_FIXTURE: C4Sketch = {
13
+ id: 'c4s-story',
14
+ title: 'Acme widget architecture',
15
+ repoKeys: [PURL],
16
+ author: 'storybook',
17
+ notes:
18
+ '## About this sketch\n\nThis is an **authored hypothesis** for the Acme widget. It describes the host and worker containers, the store inside the host, and the GitHub integration.\n\n- The worker calls the host over RPC.\n- The host calls GitHub over HTTPS.\n- Nothing here is verified or promoted.',
19
+ createdAt: '2026-10-25T00:00:00.000Z',
20
+ updatedAt: '2026-10-25T00:00:00.000Z',
21
+ model: {
22
+ view: 'container',
23
+ system: { id: 'system:acme', label: 'Acme' },
24
+ nodes: [
25
+ {
26
+ kind: 'container',
27
+ id: 'container:host',
28
+ label: 'Host',
29
+ containerKind: 'application',
30
+ technology: 'Bun',
31
+ process: 'acme/host',
32
+ state: 'proposed',
33
+ description: 'The long-lived host process.',
34
+ },
35
+ {
36
+ kind: 'container',
37
+ id: 'container:worker',
38
+ label: 'Worker',
39
+ containerKind: 'application',
40
+ technology: 'Bun',
41
+ process: 'acme/worker',
42
+ state: 'proposed',
43
+ description: 'The background worker.',
44
+ },
45
+ {
46
+ kind: 'component',
47
+ id: 'component:store',
48
+ label: 'Store',
49
+ container: 'container:host',
50
+ technology: 'Postgres',
51
+ members: ['store', 'ghost'],
52
+ state: 'proposed',
53
+ description: 'Persistence inside the host.',
54
+ },
55
+ {
56
+ kind: 'external-system',
57
+ id: 'external:github',
58
+ label: 'GitHub',
59
+ technology: 'REST',
60
+ members: ['github'],
61
+ state: 'proposed',
62
+ description: 'External source control.',
63
+ },
64
+ {
65
+ kind: 'person',
66
+ id: 'person:dev',
67
+ label: 'Developer',
68
+ state: 'proposed',
69
+ description: 'Uses the system.',
70
+ },
71
+ ],
72
+ edges: [
73
+ {
74
+ id: 'e:host-github',
75
+ source: 'container:host',
76
+ target: 'external:github',
77
+ label: 'HTTPS',
78
+ protocol: 'HTTPS',
79
+ mechanisms: ['calls'],
80
+ count: 1,
81
+ },
82
+ {
83
+ id: 'e:worker-host',
84
+ source: 'container:worker',
85
+ target: 'container:host',
86
+ label: 'RPC',
87
+ protocol: 'RPC',
88
+ mechanisms: ['calls'],
89
+ count: 1,
90
+ },
91
+ ],
92
+ },
93
+ };
94
+
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Authored C4 sketches — the types shared by the sketch view and the host
3
+ * stores.
4
+ *
5
+ * These live in `subsystems-react` so the pure view and its Storybook fixtures
6
+ * can use them without importing Studio's cross-process contract.
7
+ */
8
+
9
+ import type { C4Model } from './c4';
10
+
11
+ /**
12
+ * A C4 sketch — an authored, unverified architecture hypothesis.
13
+ *
14
+ * Unlike the accepted element store (approved containers only) and a model
15
+ * projection (derived from `process` keys), a sketch carries containers,
16
+ * components, externals, persons, AND edges. It can exist before any subsystem
17
+ * model does. Element state is forced to `proposed` by the host store: a sketch
18
+ * never claims acceptance.
19
+ */
20
+ export interface C4Sketch {
21
+ id: string;
22
+ title: string;
23
+ /** Repos the sketch claims to describe; can be empty for greenfield. */
24
+ repoKeys: string[];
25
+ /** Authored nodes + edges. */
26
+ model: C4Model;
27
+ author?: string;
28
+ notes?: string;
29
+ createdAt: string;
30
+ updatedAt: string;
31
+ }
32
+
33
+ /** Lightweight row for listing sketches without their nodes/edges. */
34
+ export interface C4SketchSummary {
35
+ id: string;
36
+ title: string;
37
+ repoKeys: string[];
38
+ author?: string;
39
+ createdAt: string;
40
+ updatedAt: string;
41
+ counts: {
42
+ containers: number;
43
+ components: number;
44
+ externals: number;
45
+ persons: number;
46
+ edges: number;
47
+ };
48
+ }
49
+
50
+ /** Which C4 source a model projection or tab reads. */
51
+ export type SubsystemC4Source = 'auto' | 'accepted' | 'draft';
52
+
53
+ /**
54
+ * A drawable C4 projection for one subsystem model.
55
+ *
56
+ * `accepted` uses the repo's approved containers; `draft` derives provisional
57
+ * containers from the model's `process` keys. `memberships` is element id →
58
+ * component aliases, so a view can build construct graphs without re-deriving
59
+ * the join.
60
+ */
61
+ export interface SubsystemC4Projection {
62
+ model: C4Model;
63
+ source: 'accepted' | 'draft';
64
+ memberships: Record<string, string[]>;
65
+ counts: {
66
+ containers: number;
67
+ externals: number;
68
+ edges: number;
69
+ components: number;
70
+ unassignedComponents: number;
71
+ unbackedProcesses: number;
72
+ };
73
+ repos: string[];
74
+ /** Aliases of components that state no `process`. */
75
+ unassignedComponents: string[];
76
+ /** Container-shaped process keys no accepted container claims (accepted source only). */
77
+ unbackedProcesses: string[];
78
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * C4 trail focus — the pure read-side helpers shared by the C4 projection view
3
+ * and its stories.
4
+ *
5
+ * These do not own state: a host decides which trail/step is focused and asks
6
+ * which elements and lines that focus touches.
7
+ */
8
+
9
+ import type { C4Edge } from './c4';
10
+ import type { SubsystemTrailStep } from './model';
11
+
12
+ /**
13
+ * The C4 elements a trail touches — every element that owns a step endpoint.
14
+ *
15
+ * Because a container's process-claim membership AND the authored components
16
+ * nested inside it can both claim an alias, both end up in the set: the
17
+ * container is the visible card at container view, its components are the
18
+ * visible cards when it is open, so either view stays honest.
19
+ */
20
+ export function deriveTrailPlayerElements(
21
+ steps: readonly SubsystemTrailStep[],
22
+ members: Readonly<Record<string, readonly string[]>>,
23
+ ): Set<string> {
24
+ const byAlias = new Map<string, string[]>();
25
+ for (const [elementId, aliases] of Object.entries(members)) {
26
+ for (const alias of aliases) {
27
+ const list = byAlias.get(alias);
28
+ if (list) list.push(elementId);
29
+ else byAlias.set(alias, [elementId]);
30
+ }
31
+ }
32
+ const players = new Set<string>();
33
+ for (const step of steps) {
34
+ for (const id of byAlias.get(step.from) ?? []) players.add(id);
35
+ for (const id of byAlias.get(step.to) ?? []) players.add(id);
36
+ }
37
+ return players;
38
+ }
39
+
40
+ /**
41
+ * The C4 edges that carry a focus — a whole trail, or one specific step.
42
+ *
43
+ * An edge is lit iff its authored `trailRefs` cover the source. A step entirely
44
+ * internal to a container folds into no C4 line, so its focus lights nothing —
45
+ * which is honest: no drawn edge represents that hop.
46
+ */
47
+ export function deriveLitC4Edges(
48
+ source: { trailId: string; stepIndex?: number },
49
+ edges: readonly C4Edge[],
50
+ ): Set<string> {
51
+ const lit = new Set<string>();
52
+ for (const edge of edges) {
53
+ for (const ref of edge.trailRefs ?? []) {
54
+ if (ref.trailId !== source.trailId) continue;
55
+ if (source.stepIndex == null || ref.stepIndices.includes(source.stepIndex)) {
56
+ lit.add(edge.id);
57
+ break;
58
+ }
59
+ }
60
+ }
61
+ return lit;
62
+ }