@statelyai/graph 0.1.0 → 0.3.1

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 (52) hide show
  1. package/README.md +65 -15
  2. package/dist/{adjacency-list-CXpOCibq.mjs → adjacency-list-ITO40kmn.mjs} +34 -1
  3. package/dist/{algorithms-R35X6ro4.mjs → algorithms-NWSB2RWj.mjs} +753 -19
  4. package/dist/algorithms.d.mts +488 -11
  5. package/dist/algorithms.mjs +2 -2
  6. package/dist/converter-CchokMDg.mjs +67 -0
  7. package/dist/edge-list-CgX6bBIF.mjs +71 -0
  8. package/dist/formats/adjacency-list/index.d.mts +44 -0
  9. package/dist/formats/adjacency-list/index.mjs +3 -0
  10. package/dist/formats/converter/index.d.mts +61 -0
  11. package/dist/formats/converter/index.mjs +3 -0
  12. package/dist/formats/cytoscape/index.d.mts +83 -0
  13. package/dist/formats/cytoscape/index.mjs +135 -0
  14. package/dist/formats/d3/index.d.mts +68 -0
  15. package/dist/formats/d3/index.mjs +111 -0
  16. package/dist/formats/dot/index.d.mts +63 -0
  17. package/dist/formats/dot/index.mjs +288 -0
  18. package/dist/formats/edge-list/index.d.mts +43 -0
  19. package/dist/formats/edge-list/index.mjs +3 -0
  20. package/dist/formats/gexf/index.d.mts +9 -0
  21. package/dist/formats/gexf/index.mjs +249 -0
  22. package/dist/formats/gml/index.d.mts +65 -0
  23. package/dist/formats/gml/index.mjs +291 -0
  24. package/dist/formats/graphml/index.d.mts +9 -0
  25. package/dist/{graphml-CUTNRXqd.mjs → formats/graphml/index.mjs} +18 -4
  26. package/dist/formats/jgf/index.d.mts +79 -0
  27. package/dist/formats/jgf/index.mjs +134 -0
  28. package/dist/formats/mermaid/index.d.mts +381 -0
  29. package/dist/formats/mermaid/index.mjs +2237 -0
  30. package/dist/formats/tgf/index.d.mts +54 -0
  31. package/dist/formats/tgf/index.mjs +111 -0
  32. package/dist/index.d.mts +332 -21
  33. package/dist/index.mjs +117 -13
  34. package/dist/{indexing-BHg1VhqN.mjs → indexing-eNDrXdDA.mjs} +31 -2
  35. package/dist/queries.d.mts +430 -9
  36. package/dist/queries.mjs +472 -9
  37. package/dist/{types-XV3S5Jnh.d.mts → types-BDXC1O5b.d.mts} +37 -2
  38. package/package.json +43 -17
  39. package/dist/adjacency-list-DW-lAUe8.d.mts +0 -10
  40. package/dist/dot-BRtq3e3c.mjs +0 -59
  41. package/dist/dot-HmJeUMsj.d.mts +0 -6
  42. package/dist/edge-list-BRujEnnU.mjs +0 -39
  43. package/dist/edge-list-CJmfoNu2.d.mts +0 -10
  44. package/dist/formats/adjacency-list.d.mts +0 -2
  45. package/dist/formats/adjacency-list.mjs +0 -3
  46. package/dist/formats/dot.d.mts +0 -2
  47. package/dist/formats/dot.mjs +0 -3
  48. package/dist/formats/edge-list.d.mts +0 -2
  49. package/dist/formats/edge-list.mjs +0 -3
  50. package/dist/formats/graphml.d.mts +0 -2
  51. package/dist/formats/graphml.mjs +0 -3
  52. package/dist/graphml-CMjPzSfY.d.mts +0 -7
@@ -0,0 +1,54 @@
1
+ import { c as Graph, f as GraphFormatConverter } from "../../types-BDXC1O5b.mjs";
2
+
3
+ //#region src/formats/tgf/index.d.ts
4
+
5
+ /**
6
+ * Converts a graph to TGF (Trivial Graph Format) string.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import { createGraph } from '@statelyai/graph';
11
+ * import { toTGF } from '@statelyai/graph/formats/tgf';
12
+ *
13
+ * const graph = createGraph({
14
+ * nodes: [{ id: 'a', label: 'A' }, { id: 'b', label: 'B' }],
15
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b', label: 'go' }],
16
+ * });
17
+ *
18
+ * const tgf = toTGF(graph);
19
+ * // "a A\nb B\n#\na b go"
20
+ * ```
21
+ */
22
+ declare function toTGF(graph: Graph): string;
23
+ /**
24
+ * Parses a TGF (Trivial Graph Format) string into a graph.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { fromTGF } from '@statelyai/graph/formats/tgf';
29
+ *
30
+ * const graph = fromTGF('a A\nb B\n#\na b go');
31
+ * // graph.nodes = [{ id: 'a', label: 'A' }, { id: 'b', label: 'B' }]
32
+ * ```
33
+ */
34
+ declare function fromTGF(tgf: string): Graph;
35
+ /**
36
+ * Bidirectional converter for TGF (Trivial Graph Format).
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { createGraph } from '@statelyai/graph';
41
+ * import { tgfConverter } from '@statelyai/graph/formats/tgf';
42
+ *
43
+ * const graph = createGraph({
44
+ * nodes: [{ id: 'a' }, { id: 'b' }],
45
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
46
+ * });
47
+ *
48
+ * const tgf = tgfConverter.to(graph);
49
+ * const roundTripped = tgfConverter.from(tgf);
50
+ * ```
51
+ */
52
+ declare const tgfConverter: GraphFormatConverter<string>;
53
+ //#endregion
54
+ export { fromTGF, tgfConverter, toTGF };
@@ -0,0 +1,111 @@
1
+ import { n as createFormatConverter } from "../../converter-CchokMDg.mjs";
2
+
3
+ //#region src/formats/tgf/index.ts
4
+ /**
5
+ * Converts a graph to TGF (Trivial Graph Format) string.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { createGraph } from '@statelyai/graph';
10
+ * import { toTGF } from '@statelyai/graph/formats/tgf';
11
+ *
12
+ * const graph = createGraph({
13
+ * nodes: [{ id: 'a', label: 'A' }, { id: 'b', label: 'B' }],
14
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b', label: 'go' }],
15
+ * });
16
+ *
17
+ * const tgf = toTGF(graph);
18
+ * // "a A\nb B\n#\na b go"
19
+ * ```
20
+ */
21
+ function toTGF(graph) {
22
+ const lines = [];
23
+ for (const node of graph.nodes) lines.push(node.label ? `${node.id} ${node.label}` : node.id);
24
+ lines.push("#");
25
+ for (const edge of graph.edges) {
26
+ const parts = [edge.sourceId, edge.targetId];
27
+ if (edge.label) parts.push(edge.label);
28
+ lines.push(parts.join(" "));
29
+ }
30
+ return lines.join("\n");
31
+ }
32
+ /**
33
+ * Parses a TGF (Trivial Graph Format) string into a graph.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { fromTGF } from '@statelyai/graph/formats/tgf';
38
+ *
39
+ * const graph = fromTGF('a A\nb B\n#\na b go');
40
+ * // graph.nodes = [{ id: 'a', label: 'A' }, { id: 'b', label: 'B' }]
41
+ * ```
42
+ */
43
+ function fromTGF(tgf) {
44
+ if (typeof tgf !== "string") throw new Error("TGF: expected a string");
45
+ const lines = tgf.split("\n");
46
+ const sepIdx = lines.indexOf("#");
47
+ const nodeLines = sepIdx >= 0 ? lines.slice(0, sepIdx) : lines;
48
+ const edgeLines = sepIdx >= 0 ? lines.slice(sepIdx + 1) : [];
49
+ const nodes = [];
50
+ for (const line of nodeLines) {
51
+ const trimmed = line.trim();
52
+ if (!trimmed) continue;
53
+ const spaceIdx = trimmed.indexOf(" ");
54
+ const id = spaceIdx >= 0 ? trimmed.slice(0, spaceIdx) : trimmed;
55
+ const label = spaceIdx >= 0 ? trimmed.slice(spaceIdx + 1) : "";
56
+ nodes.push({
57
+ type: "node",
58
+ id,
59
+ parentId: null,
60
+ initialNodeId: null,
61
+ label,
62
+ data: void 0
63
+ });
64
+ }
65
+ const edges = [];
66
+ for (const line of edgeLines) {
67
+ const trimmed = line.trim();
68
+ if (!trimmed) continue;
69
+ const parts = trimmed.split(" ");
70
+ const sourceId = parts[0];
71
+ const targetId = parts[1];
72
+ const label = parts.slice(2).join(" ");
73
+ edges.push({
74
+ type: "edge",
75
+ id: `e${edges.length}`,
76
+ sourceId,
77
+ targetId,
78
+ label,
79
+ data: void 0
80
+ });
81
+ }
82
+ return {
83
+ id: "",
84
+ type: "directed",
85
+ initialNodeId: null,
86
+ nodes,
87
+ edges,
88
+ data: void 0
89
+ };
90
+ }
91
+ /**
92
+ * Bidirectional converter for TGF (Trivial Graph Format).
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * import { createGraph } from '@statelyai/graph';
97
+ * import { tgfConverter } from '@statelyai/graph/formats/tgf';
98
+ *
99
+ * const graph = createGraph({
100
+ * nodes: [{ id: 'a' }, { id: 'b' }],
101
+ * edges: [{ id: 'e0', sourceId: 'a', targetId: 'b' }],
102
+ * });
103
+ *
104
+ * const tgf = tgfConverter.to(graph);
105
+ * const roundTripped = tgfConverter.from(tgf);
106
+ * ```
107
+ */
108
+ const tgfConverter = createFormatConverter(toTGF, fromTGF);
109
+
110
+ //#endregion
111
+ export { fromTGF, tgfConverter, toTGF };
package/dist/index.d.mts CHANGED
@@ -1,33 +1,135 @@
1
- import { C as VisualGraph, S as VisualEdge, T as VisualNode, _ as NodeChange, a as EntitiesConfig, b as SinglePathOptions, c as Graph, d as GraphEdge, f as GraphNode, g as MSTOptions, h as GraphStep, i as EdgeConfig, l as GraphConfig, m as GraphPath, n as DeleteNodeOptions, o as EntitiesUpdate, p as GraphPatch, r as EdgeChange, s as EntityRect, t as AllPairsShortestPathsOptions, u as GraphDiff, v as NodeConfig, w as VisualGraphConfig, x as TraversalOptions, y as PathOptions } from "./types-XV3S5Jnh.mjs";
2
- import { bfs, dfs, genCycles, genPostorders, genPreorders, genShortestPaths, genSimplePaths, getAllPairsShortestPaths, getConnectedComponents, getCycles, getMinimumSpanningTree, getPostorder, getPostorders, getPreorder, getPreorders, getShortestPath, getShortestPaths, getSimplePath, getSimplePaths, getStronglyConnectedComponents, getTopologicalSort, hasPath, isAcyclic, isConnected, isTree } from "./algorithms.mjs";
3
- import { n as toAdjacencyList, t as fromAdjacencyList } from "./adjacency-list-DW-lAUe8.mjs";
4
- import { t as toDOT } from "./dot-HmJeUMsj.mjs";
5
- import { n as toEdgeList, t as fromEdgeList } from "./edge-list-CJmfoNu2.mjs";
6
- import { n as toGraphML, t as fromGraphML } from "./graphml-CMjPzSfY.mjs";
7
- import { EdgeSchema, GraphSchema, NodeSchema } from "./schemas.mjs";
8
- import { getAncestors, getChildren, getDegree, getDepth, getDescendants, getEdgeBetween, getEdgesOf, getInDegree, getInEdges, getLCA, getNeighbors, getOutDegree, getOutEdges, getParent, getPredecessors, getRoots, getSiblings, getSinks, getSources, getSuccessors, isCompound, isLeaf } from "./queries.mjs";
1
+ import { C as TraversalOptions, D as VisualNode, E as VisualGraphConfig, S as TransitionOptions, T as VisualGraph, _ as MSTOptions, a as EntitiesConfig, b as PathOptions, c as Graph, d as GraphEdge, f as GraphFormatConverter, g as GraphStep, h as GraphPath, i as EdgeConfig, l as GraphConfig, m as GraphPatch, n as DeleteNodeOptions, o as EntitiesUpdate, p as GraphNode, r as EdgeChange, s as EntityRect, t as AllPairsShortestPathsOptions, u as GraphDiff, v as NodeChange, w as VisualEdge, x as SinglePathOptions, y as NodeConfig } from "./types-BDXC1O5b.mjs";
2
+ import { bfs, dfs, genCycles, genPostorders, genPreorders, genShortestPaths, genSimplePaths, getAllPairsShortestPaths, getConnectedComponents, getCycles, getMinimumSpanningTree, getPostorder, getPostorders, getPreorder, getPreorders, getShortestPath, getShortestPaths, getSimplePath, getSimplePaths, getStronglyConnectedComponents, getTopologicalSort, hasPath, isAcyclic, isConnected, isTree, joinPaths } from "./algorithms.mjs";
3
+ import { createFormatConverter } from "./formats/converter/index.mjs";
4
+ import { getAncestors, getChildren, getDegree, getDepth, getDescendants, getEdgeBetween, getEdgesOf, getInDegree, getInEdges, getLCA, getNeighbors, getOutDegree, getOutEdges, getParent, getPredecessors, getRelativeDistance, getRelativeDistanceMap, getRoots, getSiblings, getSinks, getSources, getSuccessors, isCompound, isLeaf } from "./queries.mjs";
9
5
 
10
6
  //#region src/graph.d.ts
11
- /** Create a graph from a config. Resolves defaults for all fields. */
7
+
8
+ /**
9
+ * Create a graph from a config. Resolves defaults for all fields.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const graph = createGraph({
14
+ * nodes: [{ id: 'a' }, { id: 'b' }],
15
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
16
+ * });
17
+ * ```
18
+ */
12
19
  declare function createGraph<N = any, E = any, G = any>(config?: GraphConfig<N, E, G>): Graph<N, E, G>;
13
- /** Create a visual graph with required position/size on all nodes and edges. */
20
+ /**
21
+ * Create a visual graph with required position/size on all nodes and edges.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const graph = createVisualGraph({
26
+ * nodes: [{ id: 'a', x: 0, y: 0, width: 100, height: 50 }],
27
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'a', x: 0, y: 0, width: 0, height: 0 }],
28
+ * });
29
+ * // graph.nodes[0].x === 0
30
+ * ```
31
+ */
14
32
  declare function createVisualGraph<N = any, E = any, G = any>(config?: VisualGraphConfig<N, E, G>): VisualGraph<N, E, G>;
15
- /** Get a node by id, or `undefined` if not found. */
33
+ /**
34
+ * Create a graph by BFS exploration of a transition function.
35
+ * Each unique state becomes a node; each (state, event) -> nextState becomes an edge.
36
+ *
37
+ * - Node IDs are determined by `serializeState` (default: `JSON.stringify`).
38
+ * - Edge IDs use the format `sourceId|serializedEvent|targetId` for uniqueness
39
+ * and debuggability. Edge labels are just the serialized event string.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const graph = createGraphFromTransition(
44
+ * (state, event) => {
45
+ * if (state === 'green' && event === 'TIMER') return 'yellow';
46
+ * if (state === 'yellow' && event === 'TIMER') return 'red';
47
+ * if (state === 'red' && event === 'TIMER') return 'green';
48
+ * return state;
49
+ * },
50
+ * {
51
+ * initialState: 'green',
52
+ * events: ['TIMER'],
53
+ * serializeState: (s) => s,
54
+ * serializeEvent: (e) => e,
55
+ * },
56
+ * );
57
+ * // graph.nodes.length === 3
58
+ * ```
59
+ */
60
+ declare function createGraphFromTransition<TState, TEvent>(transition: (state: TState, event: TEvent) => TState, options: TransitionOptions<TState, TEvent>): Graph<TState, TEvent>;
61
+ /**
62
+ * Get a node by id, or `undefined` if not found.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * const graph = createGraph({ nodes: [{ id: 'a' }] });
67
+ * const node = getNode(graph, 'a'); // GraphNode
68
+ * const missing = getNode(graph, 'z'); // undefined
69
+ * ```
70
+ */
16
71
  declare function getNode<N>(graph: Graph<N>, id: string): GraphNode<N> | undefined;
17
- /** Get an edge by id, or `undefined` if not found. */
72
+ /**
73
+ * Get an edge by id, or `undefined` if not found.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * const graph = createGraph({
78
+ * nodes: [{ id: 'a' }, { id: 'b' }],
79
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
80
+ * });
81
+ * const edge = getEdge(graph, 'e1'); // GraphEdge
82
+ * const missing = getEdge(graph, 'z'); // undefined
83
+ * ```
84
+ */
18
85
  declare function getEdge<E>(graph: Graph<any, E>, id: string): GraphEdge<E> | undefined;
19
- /** Check if a node exists in the graph. */
86
+ /**
87
+ * Check if a node exists in the graph.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * const graph = createGraph({ nodes: [{ id: 'a' }] });
92
+ * hasNode(graph, 'a'); // true
93
+ * hasNode(graph, 'z'); // false
94
+ * ```
95
+ */
20
96
  declare function hasNode(graph: Graph, id: string): boolean;
21
- /** Check if an edge exists in the graph. */
97
+ /**
98
+ * Check if an edge exists in the graph.
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * const graph = createGraph({
103
+ * nodes: [{ id: 'a' }, { id: 'b' }],
104
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
105
+ * });
106
+ * hasEdge(graph, 'e1'); // true
107
+ * hasEdge(graph, 'z'); // false
108
+ * ```
109
+ */
22
110
  declare function hasEdge(graph: Graph, id: string): boolean;
23
111
  /**
24
112
  * **Mutable.** Add a node to the graph. Mutates `graph.nodes` in place.
25
113
  * @returns The resolved node that was added.
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * const graph = createGraph();
118
+ * const node = addNode(graph, { id: 'a', label: 'Node A' });
119
+ * // graph.nodes.length === 1
120
+ * ```
26
121
  */
27
122
  declare function addNode<N>(graph: Graph<N>, config: NodeConfig<N>): GraphNode<N>;
28
123
  /**
29
124
  * **Mutable.** Add an edge to the graph. Mutates `graph.edges` in place.
30
125
  * @returns The resolved edge that was added.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * const graph = createGraph({ nodes: [{ id: 'a' }, { id: 'b' }] });
130
+ * const edge = addEdge(graph, { id: 'e1', sourceId: 'a', targetId: 'b' });
131
+ * // graph.edges.length === 1
132
+ * ```
31
133
  */
32
134
  declare function addEdge<E>(graph: Graph<any, E>, config: EdgeConfig<E>): GraphEdge<E>;
33
135
  /**
@@ -36,45 +138,134 @@ declare function addEdge<E>(graph: Graph<any, E>, config: EdgeConfig<E>): GraphE
36
138
  *
37
139
  * By default, children are deleted recursively.
38
140
  * With `{ reparent: true }`, children are re-parented to the deleted node's parent.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * const graph = createGraph({
145
+ * nodes: [{ id: 'a' }, { id: 'b' }],
146
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
147
+ * });
148
+ * deleteNode(graph, 'a');
149
+ * // graph.nodes.length === 1, edge e1 also removed
150
+ * ```
39
151
  */
40
152
  declare function deleteNode(graph: Graph, id: string, opts?: DeleteNodeOptions): void;
41
153
  /**
42
154
  * **Mutable.** Delete an edge. Mutates `graph.edges` in place.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * const graph = createGraph({
159
+ * nodes: [{ id: 'a' }, { id: 'b' }],
160
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
161
+ * });
162
+ * deleteEdge(graph, 'e1');
163
+ * // graph.edges.length === 0
164
+ * ```
43
165
  */
44
166
  declare function deleteEdge(graph: Graph, id: string): void;
45
167
  /**
46
168
  * **Mutable.** Update a node in place.
47
169
  * @returns The updated node.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * const graph = createGraph({ nodes: [{ id: 'a', label: 'old' }] });
174
+ * const updated = updateNode(graph, 'a', { label: 'new' });
175
+ * // updated.label === 'new'
176
+ * ```
48
177
  */
49
178
  declare function updateNode<N>(graph: Graph<N>, id: string, update: Partial<Omit<NodeConfig<N>, 'id'>>): GraphNode<N>;
50
179
  /**
51
180
  * **Mutable.** Update an edge in place.
52
181
  * @returns The updated edge.
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * const graph = createGraph({
186
+ * nodes: [{ id: 'a' }, { id: 'b' }],
187
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b', label: 'old' }],
188
+ * });
189
+ * const updated = updateEdge(graph, 'e1', { label: 'new' });
190
+ * // updated.label === 'new'
191
+ * ```
53
192
  */
54
193
  declare function updateEdge<E>(graph: Graph<any, E>, id: string, update: Partial<Omit<EdgeConfig<E>, 'id'>>): GraphEdge<E>;
55
194
  /**
56
195
  * **Mutable.** Add multiple nodes and edges to the graph.
57
196
  * Nodes are added first, then edges (so edges can reference new nodes).
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * const graph = createGraph();
201
+ * addEntities(graph, {
202
+ * nodes: [{ id: 'a' }, { id: 'b' }],
203
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
204
+ * });
205
+ * // graph.nodes.length === 2, graph.edges.length === 1
206
+ * ```
58
207
  */
59
208
  declare function addEntities<N, E>(graph: Graph<N, E>, entities: EntitiesConfig<N, E>): void;
60
209
  /**
61
210
  * **Mutable.** Delete entities by id(s). Automatically detects whether each id
62
211
  * is a node or edge. Node deletions cascade to children and connected edges.
212
+ *
213
+ * @example
214
+ * ```ts
215
+ * const graph = createGraph({
216
+ * nodes: [{ id: 'a' }, { id: 'b' }],
217
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
218
+ * });
219
+ * deleteEntities(graph, ['a', 'e1']);
220
+ * // graph.nodes.length === 1, graph.edges.length === 0
221
+ * ```
63
222
  */
64
223
  declare function deleteEntities(graph: Graph, ids: string | string[], opts?: DeleteNodeOptions): void;
65
224
  /**
66
225
  * **Mutable.** Update multiple nodes and edges in place.
67
226
  * Each entry must include an `id` to identify which entity to update.
227
+ *
228
+ * @example
229
+ * ```ts
230
+ * const graph = createGraph({
231
+ * nodes: [{ id: 'a', label: 'old' }],
232
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'a', label: 'old' }],
233
+ * });
234
+ * updateEntities(graph, {
235
+ * nodes: [{ id: 'a', label: 'new' }],
236
+ * edges: [{ id: 'e1', label: 'new' }],
237
+ * });
238
+ * ```
68
239
  */
69
240
  declare function updateEntities<N, E>(graph: Graph<N, E>, updates: EntitiesUpdate<N, E>): void;
70
241
  /**
71
242
  * OOP wrapper around a plain `Graph` object.
72
243
  * Delegates to the standalone mutable functions.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * const instance = new GraphInstance({
248
+ * nodes: [{ id: 'a' }, { id: 'b' }],
249
+ * edges: [{ id: 'e1', sourceId: 'a', targetId: 'b' }],
250
+ * });
251
+ * instance.addNode({ id: 'c' });
252
+ * instance.hasNode('c'); // true
253
+ * instance.toJSON(); // plain Graph object
254
+ * ```
73
255
  */
74
256
  declare class GraphInstance<N = any, E = any, G = any> {
75
257
  graph: Graph<N, E, G>;
76
258
  constructor(config?: GraphConfig<N, E, G>);
77
- /** Wrap an existing plain graph object. */
259
+ /**
260
+ * Wrap an existing plain graph object.
261
+ *
262
+ * @example
263
+ * ```ts
264
+ * const graph = createGraph({ nodes: [{ id: 'a' }] });
265
+ * const instance = GraphInstance.from(graph);
266
+ * instance.hasNode('a'); // true
267
+ * ```
268
+ */
78
269
  static from<N = any, E = any, G = any>(graph: Graph<N, E, G>): GraphInstance<N, E, G>;
79
270
  get id(): string;
80
271
  get type(): "directed" | "undirected";
@@ -98,24 +289,99 @@ declare class GraphInstance<N = any, E = any, G = any> {
98
289
  }
99
290
  //#endregion
100
291
  //#region src/indexing.d.ts
101
- /** Clear the cached index. Call this if you mutate graph.nodes/edges directly. */
292
+ /**
293
+ * Clear the cached index. Call this if you mutate graph.nodes/edges directly.
294
+ *
295
+ * @example
296
+ * ```ts
297
+ * import { createGraph, invalidateIndex, getIndex } from '@statelyai/graph';
298
+ *
299
+ * const graph = createGraph({ nodes: [{ id: 'a' }], edges: [] });
300
+ * // manually mutate nodes array
301
+ * graph.nodes.push({ type: 'node', id: 'b', parentId: null, initialNodeId: null, label: '', data: undefined });
302
+ * invalidateIndex(graph); // forces rebuild on next getIndex()
303
+ * ```
304
+ */
102
305
  declare function invalidateIndex(graph: Graph): void;
103
306
  //#endregion
104
307
  //#region src/diff.d.ts
105
- /** Compute a structured diff from graph `a` to graph `b` by matching IDs. */
308
+ /**
309
+ * Compute a structured diff from graph `a` to graph `b` by matching IDs.
310
+ *
311
+ * @example
312
+ * ```ts
313
+ * import { createGraph, getDiff } from '@statelyai/graph';
314
+ *
315
+ * const a = createGraph({ nodes: [{ id: 'n1' }], edges: [] });
316
+ * const b = createGraph({ nodes: [{ id: 'n1', label: 'hello' }, { id: 'n2' }], edges: [] });
317
+ *
318
+ * const diff = getDiff(a, b);
319
+ * // diff.nodes.added → [{ id: 'n2' }]
320
+ * // diff.nodes.updated → [{ id: 'n1', old: { label: '' }, new: { label: 'hello' } }]
321
+ * ```
322
+ */
106
323
  declare function getDiff<N, E>(a: Graph<N, E>, b: Graph<N, E>): GraphDiff<N, E>;
107
- /** Check if a diff has zero changes. */
324
+ /**
325
+ * Check if a diff has zero changes.
326
+ *
327
+ * @example
328
+ * ```ts
329
+ * import { createGraph, getDiff, isEmptyDiff } from '@statelyai/graph';
330
+ *
331
+ * const g = createGraph({ nodes: [{ id: 'n1' }], edges: [] });
332
+ * const diff = getDiff(g, g);
333
+ * isEmptyDiff(diff); // true
334
+ * ```
335
+ */
108
336
  declare function isEmptyDiff(diff: GraphDiff): boolean;
109
- /** Invert a diff: swap added↔removed, swap old↔new in updates. */
337
+ /**
338
+ * Invert a diff: swap added/removed, swap old/new in updates.
339
+ *
340
+ * @example
341
+ * ```ts
342
+ * import { createGraph, getDiff, invertDiff } from '@statelyai/graph';
343
+ *
344
+ * const a = createGraph({ nodes: [{ id: 'n1' }], edges: [] });
345
+ * const b = createGraph({ nodes: [{ id: 'n2' }], edges: [] });
346
+ *
347
+ * const diff = getDiff(a, b);
348
+ * const inv = invertDiff(diff);
349
+ * // inv.nodes.added contains n1 (was removed)
350
+ * // inv.nodes.removed contains n2 (was added)
351
+ * ```
352
+ */
110
353
  declare function invertDiff<N, E>(diff: GraphDiff<N, E>): GraphDiff<N, E>;
111
354
  /**
112
355
  * Compute an ordered patch list from graph `a` to graph `b`.
113
356
  * Order: delete edges → delete nodes → add nodes → add edges → update nodes → update edges.
357
+ *
358
+ * @example
359
+ * ```ts
360
+ * import { createGraph, getPatches } from '@statelyai/graph';
361
+ *
362
+ * const a = createGraph({ nodes: [{ id: 'n1' }], edges: [] });
363
+ * const b = createGraph({ nodes: [{ id: 'n1' }, { id: 'n2' }], edges: [] });
364
+ *
365
+ * const patches = getPatches(a, b);
366
+ * // patches → [{ op: 'addNode', node: { id: 'n2' } }]
367
+ * ```
114
368
  */
115
369
  declare function getPatches<N, E>(a: Graph<N, E>, b: Graph<N, E>): GraphPatch<N, E>[];
116
370
  /**
117
371
  * **Mutable.** Apply patches to a graph in order.
118
372
  * Delegates to addNode/deleteNode/updateNode/addEdge/deleteEdge/updateEdge.
373
+ *
374
+ * @example
375
+ * ```ts
376
+ * import { createGraph, getPatches, applyPatches } from '@statelyai/graph';
377
+ *
378
+ * const a = createGraph({ nodes: [{ id: 'n1' }], edges: [] });
379
+ * const b = createGraph({ nodes: [{ id: 'n1' }, { id: 'n2' }], edges: [] });
380
+ *
381
+ * const patches = getPatches(a, b);
382
+ * applyPatches(a, patches);
383
+ * // a now contains both n1 and n2
384
+ * ```
119
385
  */
120
386
  declare function applyPatches<N, E>(graph: Graph<N, E>, patches: GraphPatch<N, E>[]): void;
121
387
  /**
@@ -123,9 +389,35 @@ declare function applyPatches<N, E>(graph: Graph<N, E>, patches: GraphPatch<N, E
123
389
  * Order: add nodes → update edges → delete edges → delete nodes → add edges → update nodes.
124
390
  * This avoids cascading deletes removing edges that are being updated,
125
391
  * and ensures new nodes exist before edges reference them.
392
+ *
393
+ * @example
394
+ * ```ts
395
+ * import { createGraph, getDiff, toPatches } from '@statelyai/graph';
396
+ *
397
+ * const a = createGraph({ nodes: [{ id: 'n1' }], edges: [] });
398
+ * const b = createGraph({ nodes: [{ id: 'n2' }], edges: [] });
399
+ *
400
+ * const diff = getDiff(a, b);
401
+ * const patches = toPatches(diff);
402
+ * // patches → [{ op: 'addNode', ... }, { op: 'deleteNode', ... }]
403
+ * ```
126
404
  */
127
405
  declare function toPatches<N, E>(diff: GraphDiff<N, E>): GraphPatch<N, E>[];
128
- /** Group a patch list into a structured diff. */
406
+ /**
407
+ * Group a patch list into a structured diff.
408
+ *
409
+ * @example
410
+ * ```ts
411
+ * import { createGraph, getPatches, toDiff } from '@statelyai/graph';
412
+ *
413
+ * const a = createGraph({ nodes: [{ id: 'n1' }], edges: [] });
414
+ * const b = createGraph({ nodes: [{ id: 'n1' }, { id: 'n2' }], edges: [] });
415
+ *
416
+ * const patches = getPatches(a, b);
417
+ * const diff = toDiff(patches);
418
+ * // diff.nodes.added → [{ id: 'n2' }]
419
+ * ```
420
+ */
129
421
  declare function toDiff<N, E>(patches: GraphPatch<N, E>[]): GraphDiff<N, E>;
130
422
  //#endregion
131
423
  //#region src/transforms.d.ts
@@ -136,7 +428,26 @@ declare function toDiff<N, E>(patches: GraphPatch<N, E>[]): GraphDiff<N, E>;
136
428
  * - Edges originating from a compound node expand to all leaf descendants.
137
429
  * - Only leaf nodes (nodes with no children) appear in the result.
138
430
  * - Duplicate edges (same source + target) are deduplicated.
431
+ *
432
+ * @example
433
+ * ```ts
434
+ * import { createGraph, flatten } from '@statelyai/graph';
435
+ *
436
+ * const graph = createGraph({
437
+ * nodes: [
438
+ * { id: 'parent', initialNodeId: 'child1' },
439
+ * { id: 'child1', parentId: 'parent' },
440
+ * { id: 'child2', parentId: 'parent' },
441
+ * { id: 'other' },
442
+ * ],
443
+ * edges: [{ id: 'e1', sourceId: 'other', targetId: 'parent' }],
444
+ * });
445
+ *
446
+ * const flat = flatten(graph);
447
+ * // flat.nodes → [child1, child2, other] (leaf nodes only)
448
+ * // flat.edges → edge from 'other' → 'child1' (resolved via initialNodeId)
449
+ * ```
139
450
  */
140
451
  declare function flatten<N, E, G>(graph: Graph<N, E, G>): Graph<N, E, G>;
141
452
  //#endregion
142
- export { type AllPairsShortestPathsOptions, type DeleteNodeOptions, type EdgeChange, type EdgeConfig, EdgeSchema, type EntitiesConfig, type EntitiesUpdate, type Graph, type GraphConfig, type GraphDiff, type GraphEdge, GraphInstance, type GraphNode, type GraphPatch, type GraphPath, GraphSchema, type GraphStep, type MSTOptions, type NodeChange, type NodeConfig, NodeSchema, type PathOptions, type EntityRect as Positioned, type SinglePathOptions, type TraversalOptions, type VisualEdge, type VisualGraph, type VisualGraphConfig, type VisualNode, addEdge, addEntities, addNode, applyPatches, bfs, createGraph, createVisualGraph, deleteEdge, deleteEntities, deleteNode, dfs, flatten, fromAdjacencyList, fromEdgeList, fromGraphML, genCycles, genPostorders, genPreorders, genShortestPaths, genSimplePaths, getAllPairsShortestPaths, getAncestors, getChildren, getConnectedComponents, getCycles, getDegree, getDepth, getDescendants, getDiff, getEdge, getEdgeBetween, getEdgesOf, getInDegree, getInEdges, getLCA, getMinimumSpanningTree, getNeighbors, getNode, getOutDegree, getOutEdges, getParent, getPatches, getPostorder, getPostorders, getPredecessors, getPreorder, getPreorders, getRoots, getShortestPath, getShortestPaths, getSiblings, getSimplePath, getSimplePaths, getSinks, getSources, getStronglyConnectedComponents, getSuccessors, getTopologicalSort, hasEdge, hasNode, hasPath, invalidateIndex, invertDiff, isAcyclic, isCompound, isConnected, isEmptyDiff, isLeaf, isTree, toAdjacencyList, toDOT, toDiff, toEdgeList, toGraphML, toPatches, updateEdge, updateEntities, updateNode };
453
+ export { type AllPairsShortestPathsOptions, type DeleteNodeOptions, type EdgeChange, type EdgeConfig, type EntitiesConfig, type EntitiesUpdate, type Graph, type GraphConfig, type GraphDiff, type GraphEdge, type GraphFormatConverter, GraphInstance, type GraphNode, type GraphPatch, type GraphPath, type GraphStep, type MSTOptions, type NodeChange, type NodeConfig, type PathOptions, type EntityRect as Positioned, type SinglePathOptions, type TransitionOptions, type TraversalOptions, type VisualEdge, type VisualGraph, type VisualGraphConfig, type VisualNode, addEdge, addEntities, addNode, applyPatches, bfs, createFormatConverter, createGraph, createGraphFromTransition, createVisualGraph, deleteEdge, deleteEntities, deleteNode, dfs, flatten, genCycles, genPostorders, genPreorders, genShortestPaths, genSimplePaths, getAllPairsShortestPaths, getAncestors, getChildren, getConnectedComponents, getCycles, getDegree, getDepth, getDescendants, getDiff, getEdge, getEdgeBetween, getEdgesOf, getInDegree, getInEdges, getLCA, getMinimumSpanningTree, getNeighbors, getNode, getOutDegree, getOutEdges, getParent, getPatches, getPostorder, getPostorders, getPredecessors, getPreorder, getPreorders, getRelativeDistance, getRelativeDistanceMap, getRoots, getShortestPath, getShortestPaths, getSiblings, getSimplePath, getSimplePaths, getSinks, getSources, getStronglyConnectedComponents, getSuccessors, getTopologicalSort, hasEdge, hasNode, hasPath, invalidateIndex, invertDiff, isAcyclic, isCompound, isConnected, isEmptyDiff, isLeaf, isTree, joinPaths, toDiff, toPatches, updateEdge, updateEntities, updateNode };