mirror of
https://github.com/bitbaum/substrata.git
synced 2026-12-04 04:07:32 +01:00
* Explore entity connections and their evidence from profiles * Open the connection map on the entire entity registry * Give connections stable identities and expose all registry kinds
71 lines
2.4 KiB
TypeScript
71 lines
2.4 KiB
TypeScript
/**
|
|
* The derived graph, as the app has always consumed it.
|
|
*
|
|
* The joins themselves now live in `lib/relations`, declared once with an
|
|
* inverse and an evidence state each. This module is the adapter that keeps the
|
|
* existing shape — `/api/graph`, the country dossier, chat retrieval and the
|
|
* profile module all read it — and it inherits three fixes by construction:
|
|
* a policy instrument is a policy node rather than a capital one, `makes` and
|
|
* `produced by` cannot disagree because they are one declaration, and every
|
|
* edge says how well the join itself is evidenced.
|
|
*/
|
|
import type { Connection } from './relations/types';
|
|
import { ENTITY_KINDS, entityId, type EntityKind } from './entities/types';
|
|
import { resolveEntity } from './entities/registry';
|
|
import { connectionsOf } from './relations/registry';
|
|
|
|
export type GraphKind = EntityKind;
|
|
|
|
export type GraphNode = {
|
|
kind: GraphKind;
|
|
id: string;
|
|
label: string;
|
|
href: string;
|
|
};
|
|
|
|
export type GraphEdge = Pick<Connection, 'relationId' | 'direction' | 'kind'> & {
|
|
from: GraphNode;
|
|
to: GraphNode;
|
|
rel: string;
|
|
/** How well this join is stood up — not how well either end is. */
|
|
evidence: string;
|
|
sources: string[];
|
|
/** The sentence in `sources[0]` that carries the join, verbatim, when it has one. */
|
|
quote?: string;
|
|
scope?: string;
|
|
};
|
|
|
|
export const GRAPH_KINDS: readonly GraphKind[] = ENTITY_KINDS;
|
|
|
|
export function neighbors(kind: GraphKind, id: string): GraphEdge[] {
|
|
const self = resolveEntity(entityId(kind, id.toLowerCase()));
|
|
if (!self) return [];
|
|
const from: GraphNode = { kind, id: self.key, label: self.name, href: self.href };
|
|
|
|
return connectionsOf(self.id).flatMap((connection) => {
|
|
const other = resolveEntity(connection.other);
|
|
if (!other) return [];
|
|
return [
|
|
{
|
|
relationId: connection.relationId,
|
|
direction: connection.direction,
|
|
kind: connection.kind,
|
|
from,
|
|
to: {
|
|
kind: other.kind,
|
|
id: other.key,
|
|
// Labels come from the registry, so a country is its name rather than
|
|
// the ISO code the old graph printed.
|
|
label: other.name,
|
|
href: other.href,
|
|
},
|
|
rel: connection.label,
|
|
evidence: connection.evidence,
|
|
sources: connection.sources,
|
|
...(connection.quote ? { quote: connection.quote } : {}),
|
|
...(connection.scope ? { scope: connection.scope } : {}),
|
|
},
|
|
];
|
|
});
|
|
}
|