archidocs 1.2.0 → 1.3.0

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 (68) hide show
  1. package/README.md +62 -13
  2. package/dist/cli/commands.d.ts +225 -4
  3. package/dist/cli/commands.js +129 -4
  4. package/dist/cli/init.d.ts +1 -1
  5. package/dist/cli/init.js +2 -2
  6. package/dist/cli/main.js +9 -14
  7. package/dist/core/compare.d.ts +2 -13
  8. package/dist/core/compare.js +2 -35
  9. package/dist/core/index.d.ts +2 -5
  10. package/dist/core/index.js +2 -5
  11. package/dist/core/rules.d.ts +10 -0
  12. package/dist/core/rules.js +186 -7
  13. package/dist/core/views/compare.d.ts +12 -0
  14. package/dist/core/views/compare.js +37 -0
  15. package/dist/core/{deployment-views.d.ts → views/deployment.d.ts} +4 -5
  16. package/dist/core/{deployment-views.js → views/deployment.js} +5 -7
  17. package/dist/core/{dynamic-views.d.ts → views/dynamic.d.ts} +1 -3
  18. package/dist/core/{dynamic-views.js → views/dynamic.js} +4 -8
  19. package/dist/core/views/generated.d.ts +18 -0
  20. package/dist/core/views/generated.js +92 -0
  21. package/dist/core/views/ids.d.ts +36 -0
  22. package/dist/core/views/ids.js +51 -0
  23. package/dist/core/views/index.d.ts +13 -0
  24. package/dist/core/views/index.js +12 -0
  25. package/dist/core/views/lens.d.ts +27 -0
  26. package/dist/core/views/lens.js +112 -0
  27. package/dist/core/views/model.d.ts +21 -0
  28. package/dist/core/views/model.js +69 -0
  29. package/dist/core/views/resolve.d.ts +42 -0
  30. package/dist/core/views/resolve.js +81 -0
  31. package/dist/core/{save-as-view.d.ts → views/save-as-view.d.ts} +2 -2
  32. package/dist/core/{save-as-view.js → views/save-as-view.js} +13 -10
  33. package/dist/core/{saved-views.d.ts → views/saved.d.ts} +4 -18
  34. package/dist/core/{saved-views.js → views/saved.js} +8 -103
  35. package/dist/core/views/types.d.ts +91 -0
  36. package/dist/core/views/types.js +2 -0
  37. package/dist/mcp/errors.d.ts +37 -0
  38. package/dist/mcp/errors.js +35 -0
  39. package/dist/mcp/index.d.ts +2 -1
  40. package/dist/mcp/index.js +1 -0
  41. package/dist/mcp/server.d.ts +1 -2
  42. package/dist/mcp/server.js +12 -12
  43. package/dist/render/layout.d.ts +20 -3
  44. package/dist/render/layout.js +49 -12
  45. package/dist/render/sequence-svg.d.ts +1 -1
  46. package/dist/render/sequence.d.ts +1 -1
  47. package/dist/render/svg.d.ts +1 -1
  48. package/dist/render/svg.js +4 -4
  49. package/dist/schema/adr.js +18 -13
  50. package/dist/schema/workspace.js +226 -130
  51. package/dist/viewer/assets/index-BXa5R96a.js +138 -0
  52. package/dist/viewer/assets/index-YFA-pYjQ.css +1 -0
  53. package/dist/viewer/assets/{layout.worker-Bg-OaLVN.js → layout.worker-BRHAGJrD.js} +1 -1
  54. package/dist/viewer/assets/{png-BDKIXtm5.js → png-CAaXWR-k.js} +1 -1
  55. package/dist/viewer/assets/{svg-Bulv5YWn.js → svg-BPVHfD36.js} +2 -2
  56. package/dist/viewer/index.html +2 -2
  57. package/guide/SKILL.md +1 -1
  58. package/guide/topics/bootstrap.md +1 -1
  59. package/guide/topics/relationships.md +1 -1
  60. package/package.json +4 -3
  61. package/schemas/adr.schema.json +18 -15
  62. package/schemas/model.schema.json +637 -1144
  63. package/schemas/project.schema.json +8 -4
  64. package/schemas/workspace.schema.json +39 -20
  65. package/dist/core/views.d.ts +0 -126
  66. package/dist/core/views.js +0 -217
  67. package/dist/viewer/assets/index-DvKgCpBr.css +0 -1
  68. package/dist/viewer/assets/index-gucix3xm.js +0 -113
@@ -0,0 +1,37 @@
1
+ import { unionBundle, withoutSource } from "../compare.js";
2
+ import { diff } from "../diff.js";
3
+ import { ModelIndex } from "./model.js";
4
+ import { resolveStructural } from "./resolve.js";
5
+ /**
6
+ * A View for Compare: laid out over the union of both sides, with every node
7
+ * and edge marked. A node or edge only the later side's View shows is
8
+ * added, one only the earlier side's shows is removed. One both show is
9
+ * changed when its Element changed, or for a collapsed box when anything
10
+ * inside it was added, removed or changed; an edge when its Relationships
11
+ * did. A View that exists at one side only is drawn from that side.
12
+ * Undefined when neither side has the View. The options are `resolveView`'s.
13
+ */
14
+ export function compareView(before, after, id, options = {}) {
15
+ const both = unionBundle(before, after);
16
+ const model = new ModelIndex(both);
17
+ const view = resolveStructural(both, model, id, options);
18
+ if (!view)
19
+ return undefined;
20
+ const [b, a] = [before, after].map((side) => resolveStructural(side, new ModelIndex(side), id, options));
21
+ const changed = new Set(diff(before, after).filter((c) => c.kind === "element").map((c) => c.id));
22
+ const holdsChange = (nodeId) => [...changed].some((c) => c !== nodeId && model.ancestry(c).includes(nodeId));
23
+ const sides = (list, key) => [b, a].map((v) => new Map((v ? list(v) : []).map((x) => [key(x), x])));
24
+ const [bNodes, aNodes] = sides((v) => v.nodes, (n) => n.id);
25
+ const edgeKey = (e) => `${e.from}\0${e.to}`;
26
+ const [bEdges, aEdges] = sides((v) => v.edges, edgeKey);
27
+ const mark = (inBefore, inAfter, isChanged) => inAfter && !inBefore ? "added" : inBefore && !inAfter ? "removed" : isChanged() ? "changed" : "unchanged";
28
+ for (const node of view.nodes) {
29
+ node.change = mark(bNodes.has(node.id), aNodes.has(node.id), () => changed.has(node.id) || (node.collapsed && holdsChange(node.id)));
30
+ }
31
+ const relationships = (e) => (e?.relationships.map(withoutSource).sort() ?? []).join("\n");
32
+ for (const edge of view.edges) {
33
+ const key = edgeKey(edge);
34
+ edge.change = mark(bEdges.has(key), aEdges.has(key), () => relationships(bEdges.get(key)) !== relationships(aEdges.get(key)));
35
+ }
36
+ return view;
37
+ }
@@ -1,5 +1,6 @@
1
- import type { Bundle, BundleDeployment } from "../schema/bundle.ts";
2
- import { type ResolvedView } from "./views.ts";
1
+ import type { Bundle, BundleDeployment } from "../../schema/bundle.ts";
2
+ import type { ModelIndex } from "./model.ts";
3
+ import type { ResolvedView } from "./types.ts";
3
4
  /** Which Deployments a Deployment View draws, and which Services it leaves out altogether. */
4
5
  export interface DeploymentScope {
5
6
  /** A Deployment of the Environment is drawn when this accepts it; one that isn't counts as absent from the View. */
@@ -7,8 +8,6 @@ export interface DeploymentScope {
7
8
  /** A Service this rejects is left out, with its Relationships. */
8
9
  keep?: (service: string) => boolean;
9
10
  }
10
- /** A Deployment's node id: its Service and Target, as `<service>@<target>`. */
11
- export declare const deploymentNodeId: (d: Pick<BundleDeployment, "service" | "target">) => string;
12
11
  /**
13
12
  * Resolves the Deployment View of one Environment:
14
13
  * - every Target holding a drawn Deployment is a container, nested in its parent Targets;
@@ -22,7 +21,7 @@ export declare const deploymentNodeId: (d: Pick<BundleDeployment, "service" | "t
22
21
  * logical node outside every Target. Persons are not drawn.
23
22
  * Nodes are sorted by id, edges by `from`, then `to`.
24
23
  */
25
- export declare function resolveDeployment(bundle: Bundle, head: {
24
+ export declare function resolveDeployment(bundle: Bundle, model: ModelIndex, head: {
26
25
  id: string;
27
26
  title: string;
28
27
  }, environment: string, scope?: DeploymentScope): ResolvedView;
@@ -1,7 +1,6 @@
1
- import { lifter } from "./query.js";
2
- import { ModelIndex } from "./views.js";
1
+ import { cmp, lifter } from "../query.js";
3
2
  /** A Deployment's node id: its Service and Target, as `<service>@<target>`. */
4
- export const deploymentNodeId = (d) => `${d.service}@${d.target}`;
3
+ const deploymentNodeId = (d) => `${d.service}@${d.target}`;
5
4
  /**
6
5
  * Resolves the Deployment View of one Environment:
7
6
  * - every Target holding a drawn Deployment is a container, nested in its parent Targets;
@@ -15,8 +14,7 @@ export const deploymentNodeId = (d) => `${d.service}@${d.target}`;
15
14
  * logical node outside every Target. Persons are not drawn.
16
15
  * Nodes are sorted by id, edges by `from`, then `to`.
17
16
  */
18
- export function resolveDeployment(bundle, head, environment, scope = {}) {
19
- const model = new ModelIndex(bundle);
17
+ export function resolveDeployment(bundle, model, head, environment, scope = {}) {
20
18
  const targets = new Map(bundle.targets.map((t) => [t.id, t]));
21
19
  const keep = scope.keep ?? (() => true);
22
20
  const deployed = scope.deployed ?? (() => true);
@@ -38,7 +36,7 @@ export function resolveDeployment(bundle, head, environment, scope = {}) {
38
36
  nodes.set(parent, { id: parent, kind: "system", name: s.name, parent: d.target, element: system, collapsed: false, external: "external" in s && s.external });
39
37
  }
40
38
  const service = model.byId.get(d.service);
41
- nodes.set(deploymentNodeId(d), { id: deploymentNodeId(d), kind: "deployment", name: service.name, parent, element: d.service, collapsed: false, external: "external" in service && service.external });
39
+ nodes.set(deploymentNodeId(d), { id: deploymentNodeId(d), kind: "deployment", name: service.name, parent, element: d.service, target: d.target, collapsed: false, external: "external" in service && service.external });
42
40
  byService.set(d.service, [...(byService.get(d.service) ?? []), { d, chain }]);
43
41
  }
44
42
  const lift = lifter(bundle, "service");
@@ -87,6 +85,7 @@ export function resolveDeployment(bundle, head, environment, scope = {}) {
87
85
  ...head,
88
86
  kind: "deployment",
89
87
  direction: "TB",
88
+ environment,
90
89
  nodes: [...nodes.values()].sort((a, b) => cmp(a.id, b.id)),
91
90
  edges: [...edges.values()].sort((a, b) => cmp(a.from, b.from) || cmp(a.to, b.to)),
92
91
  };
@@ -105,4 +104,3 @@ function sharedDepth(a, b) {
105
104
  i++;
106
105
  return i;
107
106
  }
108
- const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
@@ -1,4 +1,4 @@
1
- import type { Bundle, BundleDynamicView, BundleRelationship } from "../schema/bundle.ts";
1
+ import type { Bundle, BundleDynamicView, BundleRelationship } from "../../schema/bundle.ts";
2
2
  /** How a Step reads: a consumer calling its provider, the provider's reply, or an event a publisher sends one subscriber. */
3
3
  export type StepKind = "call" | "return" | "event";
4
4
  /**
@@ -53,8 +53,6 @@ export interface DynamicResolution {
53
53
  * `ancestor-descendant-participants` report them).
54
54
  */
55
55
  export declare function resolveDynamic(bundle: Bundle, def: BundleDynamicView): DynamicResolution;
56
- /** Resolves a Saved Dynamic View by id; undefined for any other id. */
57
- export declare function resolveDynamicView(bundle: Bundle, id: string): ResolvedDynamicView | undefined;
58
56
  /** Whether an Element takes part in a Dynamic View: as a participant, or as the broker an event Step runs via. */
59
57
  export declare function takesPart(view: ResolvedDynamicView, element: string): boolean;
60
58
  /**
@@ -1,4 +1,5 @@
1
- import { within } from "./query.js";
1
+ import { within } from "../query.js";
2
+ import { formatViewId } from "./ids.js";
2
3
  const kindName = { person: "Person", system: "System", service: "Service", component: "Component" };
3
4
  const nameOf = (r) => r.id ?? `${r.from} → ${r.to}`;
4
5
  const names = (rs) => rs.map(nameOf).join(", ");
@@ -54,11 +55,6 @@ export function resolveDynamic(bundle, def) {
54
55
  });
55
56
  return { view: { id: def.id, kind: "dynamic", title: def.title, participants, steps }, problems };
56
57
  }
57
- /** Resolves a Saved Dynamic View by id; undefined for any other id. */
58
- export function resolveDynamicView(bundle, id) {
59
- const def = bundle.views.find((v) => v.id === id);
60
- return def?.kind === "dynamic" ? resolveDynamic(bundle, def).view : undefined;
61
- }
62
58
  /** Whether an Element takes part in a Dynamic View: as a participant, or as the broker an event Step runs via. */
63
59
  export function takesPart(view, element) {
64
60
  return view.participants.includes(element) || view.steps.some((s) => s.via === element);
@@ -142,11 +138,11 @@ export function stepViews(bundle, step) {
142
138
  const [a, b] = [byId.get(from), byId.get(to)];
143
139
  const views = [];
144
140
  if (a?.kind === "component" && b?.kind === "component" && a.parent === b.parent && a.parent !== null)
145
- views.push(`c3/${a.parent}`);
141
+ views.push(formatViewId({ form: "focused", level: "c3", element: a.parent }));
146
142
  for (const id of [from, to, ...(step.via === undefined ? [] : [step.via])]) {
147
143
  const s = system(id);
148
144
  if (s)
149
- views.push(`c2/${s.id}`);
145
+ views.push(formatViewId({ form: "focused", level: "c2", element: s.id }));
150
146
  }
151
147
  views.push("c2");
152
148
  if (ancestry(from)[0] !== ancestry(to)[0])
@@ -0,0 +1,18 @@
1
+ import type { Bundle } from "../../schema/bundle.ts";
2
+ import { type ModelIndex } from "./model.ts";
3
+ import type { ResolvedView, ViewKind, ViewSummary } from "./types.ts";
4
+ /**
5
+ * Resolves a Generated View by id: `c1`, `c2`, `c2/<system>`, `c3/<service>`
6
+ * (only for a Service with Components) or `deployment/<env>`. Undefined for
7
+ * any other id, and for one naming an Element or Environment that isn't there.
8
+ */
9
+ export declare function resolveGenerated(bundle: Bundle, model: ModelIndex, id: string, expand: readonly string[]): ResolvedView | undefined;
10
+ /**
11
+ * Every Generated View's id, in the order `c1`, `c2`, then `c2/<system>` and
12
+ * `c3/<service>` by id, then `deployment/<env>` by Environment id.
13
+ */
14
+ export declare function generatedViewIds(bundle: Bundle): string[];
15
+ /** A Generated View's id, kind and title without resolving it; `id` must be one `generatedViewIds` gave. */
16
+ export declare function summarise(id: string, bundle: Bundle): ViewSummary & {
17
+ kind: ViewKind;
18
+ };
@@ -0,0 +1,92 @@
1
+ import { resolveDeployment } from "./deployment.js";
2
+ import { formatViewId, parseViewId } from "./ids.js";
3
+ import { build } from "./model.js";
4
+ /**
5
+ * Resolves a Generated View by id: `c1`, `c2`, `c2/<system>`, `c3/<service>`
6
+ * (only for a Service with Components) or `deployment/<env>`. Undefined for
7
+ * any other id, and for one naming an Element or Environment that isn't there.
8
+ */
9
+ export function resolveGenerated(bundle, model, id, expand) {
10
+ const ref = parseViewId(id);
11
+ if (ref === undefined || ref.form === "saved")
12
+ return undefined;
13
+ if (ref.form === "landscape")
14
+ return landscape(model, ref.level, ref.level === "c1" ? "C1 Landscape" : "C2 Landscape", ref.level === "c1" ? [] : expand);
15
+ if (ref.form === "deployment") {
16
+ const env = bundle.environments.find((e) => e.id === ref.environment);
17
+ return env ? resolveDeployment(bundle, model, { id, title: `${env.name} (Deployment)` }, env.id) : undefined;
18
+ }
19
+ const focus = model.byId.get(ref.element);
20
+ if (!focus)
21
+ return undefined;
22
+ if (ref.level === "c2" && focus.kind === "system")
23
+ return focused(model, { id, kind: "c2", title: `${focus.name} (C2)` }, focus, expand);
24
+ if (ref.level === "c3" && focus.kind === "service" && model.children(focus.id).length > 0)
25
+ return focused(model, { id, kind: "c3", title: `${focus.name} (C3)` }, focus, expand);
26
+ return undefined;
27
+ }
28
+ /**
29
+ * Every Generated View's id, in the order `c1`, `c2`, then `c2/<system>` and
30
+ * `c3/<service>` by id, then `deployment/<env>` by Environment id.
31
+ */
32
+ export function generatedViewIds(bundle) {
33
+ const parents = new Set(bundle.elements.map((e) => e.parent));
34
+ const ids = (level, keep) => [...new Set(bundle.elements.filter(keep).map((e) => formatViewId({ form: "focused", level, element: e.id })))].sort();
35
+ const deployment = bundle.environments.map((e) => formatViewId({ form: "deployment", environment: e.id })).sort();
36
+ return ["c1", "c2", ...ids("c2", (e) => e.kind === "system"), ...ids("c3", (e) => e.kind === "service" && parents.has(e.id)), ...deployment];
37
+ }
38
+ /** A Generated View's id, kind and title without resolving it; `id` must be one `generatedViewIds` gave. */
39
+ export function summarise(id, bundle) {
40
+ const ref = parseViewId(id);
41
+ if (ref.form === "landscape")
42
+ return { id, kind: ref.level, title: ref.level === "c1" ? "C1 Landscape" : "C2 Landscape" };
43
+ if (ref.form === "deployment")
44
+ return { id, kind: "deployment", title: `${bundle.environments.find((e) => e.id === ref.environment).name} (Deployment)` };
45
+ if (ref.form === "saved")
46
+ throw new Error(`"${id}" is not a Generated View id`);
47
+ return { id, kind: ref.level, title: `${bundle.elements.find((e) => e.id === ref.element).name} (${ref.level.toUpperCase()})` };
48
+ }
49
+ /** Every top-level Element, each standing for everything beneath it, with the `expand`ed boxes open. */
50
+ function landscape(model, kind, title, expand) {
51
+ const shown = new Set(model.elements.filter((e) => e.parent === null).map((e) => e.id));
52
+ open(model, shown, expand);
53
+ return build(model, { id: kind, kind, title }, shown, () => true);
54
+ }
55
+ /** Adds the children of each `expand`ed System or Service that is shown, repeating so a Service opens once its System has. */
56
+ function open(model, shown, expand) {
57
+ const pending = new Set(expand.filter((id) => ["system", "service"].includes(model.byId.get(id)?.kind ?? "")));
58
+ for (let grew = true; grew;) {
59
+ grew = false;
60
+ for (const id of pending) {
61
+ if (!shown.has(id))
62
+ continue;
63
+ for (const child of model.children(id))
64
+ shown.add(child.id);
65
+ pending.delete(id);
66
+ grew = true;
67
+ }
68
+ }
69
+ }
70
+ /**
71
+ * One Element opened as a container around its children, plus its direct
72
+ * neighbours: every Element outside it with a Relationship to anything
73
+ * inside it, collapsed to its highest ancestor that does not contain the
74
+ * focus. Only Relationships touching the focus become edges.
75
+ */
76
+ function focused(model, head, focus, expand) {
77
+ const inside = (id) => model.ancestry(id).includes(focus.id);
78
+ const touches = (r) => inside(r.from) || inside(r.to);
79
+ const shown = new Set([focus.id, ...model.children(focus.id).map((c) => c.id)]);
80
+ for (const r of model.relationships) {
81
+ if (!touches(r))
82
+ continue;
83
+ for (const end of [r.from, r.to]) {
84
+ if (inside(end) || !model.byId.has(end))
85
+ continue;
86
+ const separating = model.ancestry(end).filter((a) => !model.ancestry(focus.id).includes(a));
87
+ shown.add(separating.at(-1));
88
+ }
89
+ }
90
+ open(model, shown, expand);
91
+ return build(model, head, shown, touches);
92
+ }
@@ -0,0 +1,36 @@
1
+ import type { Bundle } from "../../schema/bundle.ts";
2
+ /**
3
+ * A View id taken apart. The grammar:
4
+ * - `c1`, `c2`: the two Landscapes;
5
+ * - `c2/<system>`, `c3/<service>`: one Element's Generated View;
6
+ * - `deployment/<environment>`: one Environment's Generated Deployment View;
7
+ * - `project:view-id`: a Saved View, by its Qualified id.
8
+ */
9
+ export type ViewRef = {
10
+ form: "landscape";
11
+ level: "c1" | "c2";
12
+ } | {
13
+ form: "focused";
14
+ level: "c2" | "c3";
15
+ element: string;
16
+ } | {
17
+ form: "deployment";
18
+ environment: string;
19
+ } | {
20
+ form: "saved";
21
+ id: string;
22
+ };
23
+ /**
24
+ * Reads a View id by its form alone, without a Bundle: it says what the id
25
+ * would name, not whether that View exists. Undefined for a string in none of
26
+ * the forms.
27
+ */
28
+ export declare function parseViewId(id: string): ViewRef | undefined;
29
+ /** Writes a View id; the inverse of `parseViewId`. */
30
+ export declare function formatViewId(ref: ViewRef): string;
31
+ /**
32
+ * An Element's own Generated View: `c2/<system>` for a System, `c3/<service>`
33
+ * for a Service with Components, else the `c2/<system>` of the System it sits
34
+ * in. Undefined for an unknown Element or one outside any System.
35
+ */
36
+ export declare function ownViewId(bundle: Bundle, element: string): string | undefined;
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Reads a View id by its form alone, without a Bundle: it says what the id
3
+ * would name, not whether that View exists. Undefined for a string in none of
4
+ * the forms.
5
+ */
6
+ export function parseViewId(id) {
7
+ if (id === "c1" || id === "c2")
8
+ return { form: "landscape", level: id };
9
+ const slash = id.indexOf("/");
10
+ const scope = slash < 0 ? undefined : id.slice(0, slash);
11
+ const rest = id.slice(slash + 1);
12
+ if (scope === "c2" || scope === "c3")
13
+ return rest === "" ? undefined : { form: "focused", level: scope, element: rest };
14
+ if (scope === "deployment")
15
+ return rest === "" ? undefined : { form: "deployment", environment: rest };
16
+ return slash < 0 && id.includes(":") ? { form: "saved", id } : undefined;
17
+ }
18
+ /** Writes a View id; the inverse of `parseViewId`. */
19
+ export function formatViewId(ref) {
20
+ if (ref.form === "landscape")
21
+ return ref.level;
22
+ if (ref.form === "focused")
23
+ return `${ref.level}/${ref.element}`;
24
+ if (ref.form === "deployment")
25
+ return `deployment/${ref.environment}`;
26
+ return ref.id;
27
+ }
28
+ /**
29
+ * An Element's own Generated View: `c2/<system>` for a System, `c3/<service>`
30
+ * for a Service with Components, else the `c2/<system>` of the System it sits
31
+ * in. Undefined for an unknown Element or one outside any System.
32
+ */
33
+ export function ownViewId(bundle, element) {
34
+ const byId = new Map(bundle.elements.map((e) => [e.id, e]));
35
+ const own = byId.get(element);
36
+ if (!own)
37
+ return undefined;
38
+ if (own.kind === "service" && bundle.elements.some((e) => e.parent === element))
39
+ return formatViewId({ form: "focused", level: "c3", element });
40
+ const seen = new Set();
41
+ for (let e = own; !seen.has(e.id);) {
42
+ if (e.kind === "system")
43
+ return formatViewId({ form: "focused", level: "c2", element: e.id });
44
+ seen.add(e.id);
45
+ const parent = e.parent === null ? undefined : byId.get(e.parent);
46
+ if (!parent)
47
+ break;
48
+ e = parent;
49
+ }
50
+ return undefined;
51
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The View module: any View id in, the View out. This index is the only
3
+ * file of the folder to import from outside it; the resolvers behind it are
4
+ * private. It runs in Node and in the browser.
5
+ */
6
+ export { resolveView, checkView, listViews, generatedViews, pickedView, type AnyView } from "./resolve.ts";
7
+ export { compareView } from "./compare.ts";
8
+ export { saveAsView, type Screen, type SavedScreen, type SavedViewDefinition } from "./save-as-view.ts";
9
+ export { parseViewId, formatViewId, ownViewId, type ViewRef } from "./ids.ts";
10
+ export { describeLens } from "./lens.ts";
11
+ export { stepViews, dynamicViewsUsing, type DynamicResolution, type DynamicProblem, type ResolvedDynamicView, type ResolvedStep, type StepKind } from "./dynamic.ts";
12
+ export type { SavedResolution, BadExpand, UnresolvedPath } from "./saved.ts";
13
+ export { MAX_VIEW_NODES, type CompareMark, type ImpactSide, type LensChoice, type ListedKind, type NodeMark, type ResolveOptions, type ResolvedView, type ViewDirection, type ViewEdge, type ViewKind, type ViewNode, type ViewSummary, } from "./types.ts";
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The View module: any View id in, the View out. This index is the only
3
+ * file of the folder to import from outside it; the resolvers behind it are
4
+ * private. It runs in Node and in the browser.
5
+ */
6
+ export { resolveView, checkView, listViews, generatedViews, pickedView } from "./resolve.js";
7
+ export { compareView } from "./compare.js";
8
+ export { saveAsView } from "./save-as-view.js";
9
+ export { parseViewId, formatViewId, ownViewId } from "./ids.js";
10
+ export { describeLens } from "./lens.js";
11
+ export { stepViews, dynamicViewsUsing } from "./dynamic.js";
12
+ export { MAX_VIEW_NODES, } from "./types.js";
@@ -0,0 +1,27 @@
1
+ import type { Bundle, BundleLens } from "../../schema/bundle.ts";
2
+ import { type Path } from "../paths.ts";
3
+ import type { ModelIndex } from "./model.ts";
4
+ import type { ResolvedView } from "./types.ts";
5
+ /**
6
+ * A Lens in words, as the viewer's Lens chip, MCP's `get_view` and a
7
+ * picture's key say it: `Impact of <name>, <upstream | downstream | both
8
+ * ways>`, `Team <name>` or `Paths from <name> to <name>`, adding `, all up to
9
+ * <n> hops` when a path Lens doesn't keep the shortest. Elements, Teams and
10
+ * Environments go by name, or by id when the Bundle doesn't know it.
11
+ */
12
+ export declare function describeLens(bundle: Bundle, lens: BundleLens): string;
13
+ /** Every Element on any of the paths. */
14
+ export declare const nodesOn: (paths: Path[]) => Set<string>;
15
+ /**
16
+ * Marks every node and lights every edge for the Lens. An Element the Lens
17
+ * reaches marks the node that holds it, or that it holds, at its nearest
18
+ * distance. An impact Lens lights an edge carrying the impact one hop
19
+ * closer to its origin; a path Lens, an edge carrying a hop of a path; a
20
+ * Team Lens, an edge between two hit nodes.
21
+ */
22
+ export declare function applyLens(bundle: Bundle, model: ModelIndex, view: ResolvedView, lens: BundleLens): {
23
+ unresolved?: {
24
+ from: string;
25
+ to: string;
26
+ };
27
+ };
@@ -0,0 +1,112 @@
1
+ import { getImpact } from "../impact.js";
2
+ import { pathsBetween } from "../paths.js";
3
+ /**
4
+ * A Lens in words, as the viewer's Lens chip, MCP's `get_view` and a
5
+ * picture's key say it: `Impact of <name>, <upstream | downstream | both
6
+ * ways>`, `Team <name>` or `Paths from <name> to <name>`, adding `, all up to
7
+ * <n> hops` when a path Lens doesn't keep the shortest. Elements, Teams and
8
+ * Environments go by name, or by id when the Bundle doesn't know it.
9
+ */
10
+ export function describeLens(bundle, lens) {
11
+ const name = (id) => [...bundle.elements, ...bundle.teams, ...bundle.environments].find((x) => x.id === id)?.name ?? id;
12
+ if ("impact" in lens)
13
+ return `Impact of ${name(lens.impact)}, ${lens.direction === "both" ? "both ways" : lens.direction}`;
14
+ if ("team" in lens)
15
+ return `Team ${name(lens.team)}`;
16
+ const { from, to, paths = "shortest" } = lens.path;
17
+ return `Paths from ${name(from)} to ${name(to)}${paths === "shortest" ? "" : `, all up to ${paths} hops`}`;
18
+ }
19
+ /** Every Element on any of the paths. */
20
+ export const nodesOn = (paths) => new Set(paths.flatMap((p) => p.hops.flatMap((h) => [h.from, h.to])));
21
+ /**
22
+ * Marks every node and lights every edge for the Lens. An Element the Lens
23
+ * reaches marks the node that holds it, or that it holds, at its nearest
24
+ * distance. An impact Lens lights an edge carrying the impact one hop
25
+ * closer to its origin; a path Lens, an edge carrying a hop of a path; a
26
+ * Team Lens, an edge between two hit nodes.
27
+ */
28
+ export function applyLens(bundle, model, view, lens) {
29
+ view.lens = lens;
30
+ /** Element ids the Lens reaches, with their distance when it has one. */
31
+ const reached = new Map();
32
+ /** For an impact Lens, which side of the origin each reached Element is on, at its nearest distance. */
33
+ const sides = new Map();
34
+ const carried = new Set();
35
+ let unresolved;
36
+ if ("impact" in lens) {
37
+ if (model.byId.has(lens.impact)) {
38
+ reached.set(lens.impact, 0);
39
+ sides.set(lens.impact, "origin");
40
+ const impact = getImpact(bundle, lens.impact, lens.direction);
41
+ const rings = [...(impact.upstream ?? []).map((ring) => ["upstream", ring]), ...(impact.downstream ?? []).map((ring) => ["downstream", ring])];
42
+ for (const [side, ring] of rings) {
43
+ for (const e of ring.elements) {
44
+ if (ring.distance < (reached.get(e.id) ?? Infinity)) {
45
+ reached.set(e.id, ring.distance);
46
+ sides.set(e.id, side);
47
+ }
48
+ for (const r of e.relationships)
49
+ carried.add(r);
50
+ }
51
+ }
52
+ }
53
+ }
54
+ else if ("team" in lens) {
55
+ for (const e of model.elements)
56
+ if (e.owner === lens.team)
57
+ reached.set(e.id, undefined);
58
+ for (const t of bundle.targets)
59
+ if (t.owner === lens.team)
60
+ reached.set(t.id, undefined);
61
+ }
62
+ else {
63
+ const paths = pathsBetween(bundle, lens.path.from, lens.path.to, lens.path.paths ?? "shortest");
64
+ for (const id of nodesOn(paths))
65
+ reached.set(id, undefined);
66
+ for (const p of paths)
67
+ for (const h of p.hops)
68
+ for (const r of h.relationships)
69
+ carried.add(r);
70
+ if (paths.length === 0 && model.byId.has(lens.path.from) && model.byId.has(lens.path.to))
71
+ unresolved = { from: lens.path.from, to: lens.path.to };
72
+ }
73
+ // Each reached Element marks the nearest node holding it; an impact or path Lens also marks the nodes inside it.
74
+ // A node stands for its own id, or for the Element it draws: a Deployment View draws one Service as several Deployments.
75
+ const standing = new Map();
76
+ for (const n of view.nodes)
77
+ standing.set(n.element ?? n.id, [...(standing.get(n.element ?? n.id) ?? []), n.id]);
78
+ const marks = new Map(view.nodes.map((n) => [n.id, { hit: false }]));
79
+ const mark = (node, d, side) => {
80
+ const m = marks.get(node);
81
+ if (!m)
82
+ return;
83
+ m.hit = true;
84
+ if (d !== undefined && d < (m.distance ?? Infinity)) {
85
+ m.distance = d;
86
+ if (side !== undefined)
87
+ m.side = side;
88
+ }
89
+ };
90
+ for (const [id, d] of reached) {
91
+ const holder = model.ancestry(id).find((a) => standing.has(a));
92
+ for (const n of holder === undefined ? [] : standing.get(holder))
93
+ mark(n, d, sides.get(id));
94
+ if (!("team" in lens))
95
+ for (const n of view.nodes)
96
+ if ((n.element ?? n.id) !== id && model.ancestry(n.element ?? n.id).includes(id))
97
+ mark(n.id, d, sides.get(id));
98
+ }
99
+ for (const n of view.nodes)
100
+ n.mark = marks.get(n.id);
101
+ const markOf = new Map(view.nodes.map((n) => [n.id, n.mark]));
102
+ for (const e of view.edges) {
103
+ const [from, to] = [markOf.get(e.from), markOf.get(e.to)];
104
+ if ("team" in lens)
105
+ e.lit = from.hit && to.hit;
106
+ else if ("impact" in lens)
107
+ e.lit = e.relationships.some((r) => carried.has(r)) && from.distance !== undefined && to.distance !== undefined && Math.abs(from.distance - to.distance) === 1;
108
+ else
109
+ e.lit = e.relationships.some((r) => carried.has(r)) && from.hit && to.hit;
110
+ }
111
+ return unresolved ? { unresolved } : {};
112
+ }
@@ -0,0 +1,21 @@
1
+ import type { Bundle, BundleElement, BundleRelationship } from "../../schema/bundle.ts";
2
+ import type { ResolvedView } from "./types.ts";
3
+ /** Lookups over a Bundle's Elements. */
4
+ export declare class ModelIndex {
5
+ readonly elements: BundleElement[];
6
+ readonly relationships: BundleRelationship[];
7
+ readonly byId: Map<string, BundleElement>;
8
+ private readonly childrenOf;
9
+ constructor(bundle: Bundle);
10
+ children(id: string): BundleElement[];
11
+ /** The Element itself, then its parent, and so on up to the top level. Unknown ids yield only themselves. */
12
+ ancestry(id: string): string[];
13
+ }
14
+ /**
15
+ * Builds a View from the set of Elements it shows: each Relationship's ends
16
+ * are lifted to their nearest shown ancestor-or-self, and the Relationships
17
+ * landing on one pair form one edge. An edge is kept when `keep` accepts the
18
+ * declared Relationship and its ends are two different nodes. It draws
19
+ * left to right.
20
+ */
21
+ export declare function build(model: ModelIndex, head: Pick<ResolvedView, "id" | "kind" | "title">, shown: Set<string>, keep: (r: BundleRelationship) => boolean): ResolvedView;
@@ -0,0 +1,69 @@
1
+ import { cmp } from "../query.js";
2
+ /** Lookups over a Bundle's Elements. */
3
+ export class ModelIndex {
4
+ elements;
5
+ relationships;
6
+ byId;
7
+ childrenOf = new Map();
8
+ constructor(bundle) {
9
+ this.elements = bundle.elements;
10
+ this.relationships = bundle.relationships;
11
+ this.byId = new Map(bundle.elements.map((e) => [e.id, e]));
12
+ for (const e of bundle.elements) {
13
+ if (e.parent === null)
14
+ continue;
15
+ const list = this.childrenOf.get(e.parent) ?? [];
16
+ list.push(e);
17
+ this.childrenOf.set(e.parent, list);
18
+ }
19
+ }
20
+ children(id) {
21
+ return this.childrenOf.get(id) ?? [];
22
+ }
23
+ /** The Element itself, then its parent, and so on up to the top level. Unknown ids yield only themselves. */
24
+ ancestry(id) {
25
+ const out = [id];
26
+ const seen = new Set(out);
27
+ for (let p = this.byId.get(id)?.parent; p != null && !seen.has(p) && this.byId.has(p); p = this.byId.get(p)?.parent) {
28
+ out.push(p);
29
+ seen.add(p);
30
+ }
31
+ return out;
32
+ }
33
+ }
34
+ /**
35
+ * Builds a View from the set of Elements it shows: each Relationship's ends
36
+ * are lifted to their nearest shown ancestor-or-self, and the Relationships
37
+ * landing on one pair form one edge. An edge is kept when `keep` accepts the
38
+ * declared Relationship and its ends are two different nodes. It draws
39
+ * left to right.
40
+ */
41
+ export function build(model, head, shown, keep) {
42
+ const lift = (id) => model.ancestry(id).find((a) => shown.has(a));
43
+ const edges = new Map();
44
+ for (const r of model.relationships) {
45
+ if (!keep(r))
46
+ continue;
47
+ const from = lift(r.from);
48
+ const to = lift(r.to);
49
+ if (from === undefined || to === undefined || from === to)
50
+ continue;
51
+ const key = `${from}\0${to}`;
52
+ const edge = edges.get(key) ?? { from, to, relationships: [] };
53
+ edge.relationships.push(r);
54
+ edges.set(key, edge);
55
+ }
56
+ const nodes = [...shown]
57
+ .map((id) => model.byId.get(id))
58
+ .filter((e) => e !== undefined)
59
+ .map((e) => ({
60
+ id: e.id,
61
+ kind: e.kind,
62
+ name: e.name,
63
+ parent: model.ancestry(e.id).slice(1).find((a) => shown.has(a)) ?? null,
64
+ collapsed: model.children(e.id).some((c) => !shown.has(c.id)),
65
+ external: "external" in e && e.external,
66
+ }))
67
+ .sort((a, b) => cmp(a.id, b.id));
68
+ return { ...head, direction: "LR", nodes, edges: [...edges.values()].sort((a, b) => cmp(a.from, b.from) || cmp(a.to, b.to)) };
69
+ }