@flow-state-dev/core/graph
Type Aliases
AddEdgeInput
type AddEdgeInput = object;
Input to add() — id/createdAt/validUntil are framework-assigned.
Properties
confidence?
confidence?: number;
from
from: NodeRef;
source?
source?: string[];
to
to: NodeRef;
type
type: string;
validFrom?
validFrom?: string | null;
Edge
type Edge = z.infer<typeof edgeSchema>;
A directed, typed, bi-temporal edge between two nodes.
EdgeBackingRef
type EdgeBackingRef = object;
The minimal ref surface the edge API needs (a ResourceRef satisfies this structurally).
Properties
state
readonly state: object & Record<string, unknown>;
Type Declaration
edges?
edges?: Edge[];
Methods
updateState()
updateState(updater): Promise<void>;
Parameters
updater
(state) => any
Returns
Promise<void>
EdgeSlotConfig
type EdgeSlotConfig = object;
Declares that a resource carries a typed-edge graph in its state. true = defaults.
Properties
maxEdges?
maxEdges?: number;
Max stored edges. On overflow, add() culls lowest-confidence edges down to this many.
vocabulary?
vocabulary?: string[];
Curated relation vocabulary. When set, add() throws on an out-of-vocab type.
NodeRef
type NodeRef = string;
A node identifier. Bare string; convention is "<namespace>:<key>" for cross-resource edges.
ResourceEdgeApi
type ResourceEdgeApi = object;
The .edges accessor attached to a ResourceRef/ResourceContext whose config declared edges.
Methods
add()
add(input): Promise<{
confidence: number;
createdAt: string;
from: string;
id: string;
source: string[];
to: string;
type: string;
validFrom: string | null;
validUntil: string | null;
}>;
Add an edge. Assigns a crypto-strong id + createdAt. Enforces vocabulary + maxEdges. Returns the stored edge.
Parameters
input
Returns
Promise<{
confidence: number;
createdAt: string;
from: string;
id: string;
source: string[];
to: string;
type: string;
validFrom: string | null;
validUntil: string | null;
}>
all()
all(opts?): object[];
All stored edges; pass { at } to filter to edges active at that instant.
Parameters
opts?
at?
string
Returns
object[]
egoGraph()
egoGraph(node, opts?): object;
Ego graph within depth hops.
Parameters
node
string
opts?
Returns
object
edges
edges: Edge[];
nodes
nodes: NodeRef[];
neighbors()
neighbors(node, opts?): object[];
Immediate neighbours of a node.
Parameters
node
string
opts?
Returns
object[]
pruneDangling()
pruneDangling(knownNodes): Promise<number>;
Drop edges whose endpoints are not in knownNodes (referential cleanup). Returns removed count.
Parameters
knownNodes
Iterable<string>
Returns
Promise<number>
remove()
remove(edgeId): Promise<void>;
Hard-remove an edge by id.
Parameters
edgeId
string
Returns
Promise<void>
shortestPath()
shortestPath(
from,
to,
opts?
): object[] | null;
Shortest edge path between two nodes; null if unreachable.
Parameters
from
string
to
string
opts?
Returns
object[] | null
supersede()
supersede(edgeId, at?): Promise<void>;
Supersede (do NOT delete) an edge: set validUntil = at (default now). No-op if id absent.
Parameters
edgeId
string
at?
string
Returns
Promise<void>
TraversalOpts
type TraversalOpts = object;
Options shared by every traversal function.
Properties
at?
at?: string;
ISO time; only edges active at this instant (bi-temporal). Default now.
depth?
depth?: number;
Max hops. Default 2; clamped to MAX_DEPTH.
direction?
direction?: "out" | "in" | "both";
Edge direction to follow. Default "both".
relationTypes?
relationTypes?: string[];
Restrict to these edge types (matched on edge.type).
Variables
edgeListSchema
const edgeListSchema: z.ZodArray<z.ZodObject<{
confidence: z.ZodDefault<z.ZodNumber>;
createdAt: z.ZodString;
from: z.ZodString;
id: z.ZodString;
source: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
to: z.ZodString;
type: z.ZodString;
validFrom: z.ZodDefault<z.ZodNullable<z.ZodString>>;
validUntil: z.ZodDefault<z.ZodNullable<z.ZodString>>;
}, "strip", z.ZodTypeAny, {
confidence: number;
createdAt: string;
from: string;
id: string;
source: string[];
to: string;
type: string;
validFrom: string | null;
validUntil: string | null;
}, {
confidence?: number;
createdAt: string;
from: string;
id: string;
source?: string[];
to: string;
type: string;
validFrom?: string | null;
validUntil?: string | null;
}>, "many">;
A list of edges.
edgeSchema
const edgeSchema: z.ZodObject<{
confidence: z.ZodDefault<z.ZodNumber>;
createdAt: z.ZodString;
from: z.ZodString;
id: z.ZodString;
source: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
to: z.ZodString;
type: z.ZodString;
validFrom: z.ZodDefault<z.ZodNullable<z.ZodString>>;
validUntil: z.ZodDefault<z.ZodNullable<z.ZodString>>;
}, "strip", z.ZodTypeAny, {
confidence: number;
createdAt: string;
from: string;
id: string;
source: string[];
to: string;
type: string;
validFrom: string | null;
validUntil: string | null;
}, {
confidence?: number;
createdAt: string;
from: string;
id: string;
source?: string[];
to: string;
type: string;
validFrom?: string | null;
validUntil?: string | null;
}>;
A directed, typed, bi-temporal edge between two nodes.
Bi-temporal: validFrom/validUntil describe when the relation holds in
the modelled world (null = open-ended / currently valid), independent of
createdAt (when the edge was recorded).
MAX_DEPTH
const MAX_DEPTH: 6 = 6;
Hard upper bound on traversal depth, to bound cost regardless of caller input.
Functions
activeAt()
function activeAt(edges, at?): object[];
Edges valid at at (default: now). Active = validFrom is null or <= at, AND validUntil is null or > at.
Parameters
edges
object[]
at?
string
Returns
object[]
createResourceEdgeApi()
function createResourceEdgeApi(ref, slot): ResourceEdgeApi;
Build the .edges API over a resource ref. slot is the resolved
EdgeSlotConfig ({} when declared as true). Read methods operate
synchronously on the current ref.state.edges; mutators persist via
ref.updateState.
Parameters
ref
slot
Returns
egoGraph()
function egoGraph(
edges,
node,
opts?
): object;
All nodes + edges within depth hops of node (the ego graph / neighborhood). Includes node itself in nodes.
Parameters
edges
object[]
node
string
opts?
Returns
object
edges
edges: object[];
nodes
nodes: string[];
neighbors()
function neighbors(
edges,
node,
opts?
): object[];
Immediate edges touching node in the requested direction (after active+relationType filtering).
Parameters
edges
object[]
node
string
opts?
Returns
object[]
nodeRef()
function nodeRef(namespace, key): string;
Build a namespaced node ref. Lowercases the key for canonicalisation. e.g. nodeRef("ticker","NVDA") -> "ticker:nvda".
Parameters
namespace
string
key
string
Returns
string
parseNodeRef()
function parseNodeRef(ref): object;
Split a namespaced ref. "ticker:nvda" -> { namespace: "ticker", key: "nvda" }; "user" -> { key: "user" } (namespace undefined). Only the FIRST colon splits (keys may contain colons).
Parameters
ref
string
Returns
object
key
key: string;
namespace?
namespace?: string;
shortestPath()
function shortestPath(
edges,
from,
to,
opts?
): object[] | null;
Shortest edge path from -> to (BFS over the active, type-filtered graph). null if unreachable within depth. The returned array is the ordered chain of edges connecting from to to.
Parameters
edges
object[]
from
string
to
string
opts?
Returns
object[] | null
traverse()
function traverse(
edges,
start,
opts?
): object[];
Edges visited from start up to depth, in BFS order.
Parameters
edges
object[]
start
string
opts?
Returns
object[]