@statelyai/graph 0.5.0 → 0.7.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.
@@ -1,4 +1,4 @@
1
- import { c as Graph, d as GraphEdge, p as GraphNode } from "./types-FBZCrmnG.mjs";
1
+ import { h as GraphNode, p as GraphEdge, u as Graph } from "./types-DkKjaQW3.mjs";
2
2
 
3
3
  //#region src/queries.d.ts
4
4
 
package/dist/queries.mjs CHANGED
@@ -565,7 +565,7 @@ function getRelativeDistanceMap(graph, parentId) {
565
565
  if (parentId !== null) {
566
566
  const pi = idx.nodeById.get(parentId);
567
567
  if (pi !== void 0) sourceId = graph.nodes[pi].initialNodeId ?? null;
568
- } else sourceId = graph.initialNodeId;
568
+ } else sourceId = graph.initialNodeId ?? null;
569
569
  if (!sourceId) return {};
570
570
  const siblingSet = new Set(idx.childNodes.get(parentId) ?? []);
571
571
  if (!siblingSet.has(sourceId)) return {};
@@ -6,7 +6,7 @@ declare const NodeSchema: z.ZodObject<{
6
6
  id: z.ZodString;
7
7
  parentId: z.ZodOptional<z.ZodNullable<z.ZodString>>;
8
8
  initialNodeId: z.ZodOptional<z.ZodNullable<z.ZodString>>;
9
- label: z.ZodString;
9
+ label: z.ZodOptional<z.ZodString>;
10
10
  data: z.ZodAny;
11
11
  x: z.ZodOptional<z.ZodNumber>;
12
12
  y: z.ZodOptional<z.ZodNumber>;
@@ -21,7 +21,8 @@ declare const EdgeSchema: z.ZodObject<{
21
21
  id: z.ZodString;
22
22
  sourceId: z.ZodString;
23
23
  targetId: z.ZodString;
24
- label: z.ZodString;
24
+ label: z.ZodOptional<z.ZodNullable<z.ZodString>>;
25
+ weight: z.ZodOptional<z.ZodNumber>;
25
26
  data: z.ZodAny;
26
27
  x: z.ZodOptional<z.ZodNumber>;
27
28
  y: z.ZodOptional<z.ZodNumber>;
@@ -36,13 +37,13 @@ declare const GraphSchema: z.ZodObject<{
36
37
  directed: "directed";
37
38
  undirected: "undirected";
38
39
  }>;
39
- initialNodeId: z.ZodNullable<z.ZodString>;
40
+ initialNodeId: z.ZodOptional<z.ZodNullable<z.ZodString>>;
40
41
  nodes: z.ZodArray<z.ZodObject<{
41
42
  type: z.ZodLiteral<"node">;
42
43
  id: z.ZodString;
43
44
  parentId: z.ZodOptional<z.ZodNullable<z.ZodString>>;
44
45
  initialNodeId: z.ZodOptional<z.ZodNullable<z.ZodString>>;
45
- label: z.ZodString;
46
+ label: z.ZodOptional<z.ZodString>;
46
47
  data: z.ZodAny;
47
48
  x: z.ZodOptional<z.ZodNumber>;
48
49
  y: z.ZodOptional<z.ZodNumber>;
@@ -57,7 +58,8 @@ declare const GraphSchema: z.ZodObject<{
57
58
  id: z.ZodString;
58
59
  sourceId: z.ZodString;
59
60
  targetId: z.ZodString;
60
- label: z.ZodString;
61
+ label: z.ZodOptional<z.ZodNullable<z.ZodString>>;
62
+ weight: z.ZodOptional<z.ZodNumber>;
61
63
  data: z.ZodAny;
62
64
  x: z.ZodOptional<z.ZodNumber>;
63
65
  y: z.ZodOptional<z.ZodNumber>;
package/dist/schemas.mjs CHANGED
@@ -7,7 +7,7 @@ const NodeSchema = z.object({
7
7
  id: z.string(),
8
8
  parentId: z.string().nullable().optional(),
9
9
  initialNodeId: z.string().nullable().optional(),
10
- label: z.string(),
10
+ label: z.string().optional(),
11
11
  data: z.any(),
12
12
  x: z.number().optional(),
13
13
  y: z.number().optional(),
@@ -22,7 +22,8 @@ const EdgeSchema = z.object({
22
22
  id: z.string(),
23
23
  sourceId: z.string(),
24
24
  targetId: z.string(),
25
- label: z.string(),
25
+ label: z.string().nullable().optional(),
26
+ weight: z.number().optional(),
26
27
  data: z.any(),
27
28
  x: z.number().optional(),
28
29
  y: z.number().optional(),
@@ -34,7 +35,7 @@ const EdgeSchema = z.object({
34
35
  const GraphSchema = z.object({
35
36
  id: z.string(),
36
37
  type: z.enum(["directed", "undirected"]),
37
- initialNodeId: z.string().nullable(),
38
+ initialNodeId: z.string().nullable().optional(),
38
39
  nodes: z.array(NodeSchema),
39
40
  edges: z.array(EdgeSchema),
40
41
  data: z.any(),
@@ -45,7 +45,13 @@ interface EdgeConfig<TEdgeData = any> {
45
45
  /**
46
46
  * The label of the edge.
47
47
  */
48
- label?: string;
48
+ label?: string | null;
49
+ /**
50
+ * Optional numeric weight for the edge.
51
+ * Used by pathfinding, MST, and other weighted algorithms.
52
+ * When `getWeight` is not provided, algorithms default to `edge.weight ?? 1`.
53
+ */
54
+ weight?: number;
49
55
  data?: TEdgeData;
50
56
  x?: number;
51
57
  y?: number;
@@ -57,7 +63,7 @@ interface EdgeConfig<TEdgeData = any> {
57
63
  interface Graph<TNodeData = any, TEdgeData = any, TGraphData = any> {
58
64
  id: string;
59
65
  type: 'directed' | 'undirected';
60
- initialNodeId: string | null;
66
+ initialNodeId?: string | null;
61
67
  nodes: GraphNode<TNodeData>[];
62
68
  edges: GraphEdge<TEdgeData>[];
63
69
  data: TGraphData;
@@ -69,7 +75,7 @@ interface GraphNode<TNodeData = any> {
69
75
  id: string;
70
76
  parentId?: string | null;
71
77
  initialNodeId?: string | null;
72
- label: string;
78
+ label?: string;
73
79
  data: TNodeData;
74
80
  x?: number;
75
81
  y?: number;
@@ -84,7 +90,13 @@ interface GraphEdge<TEdgeData = any> {
84
90
  id: string;
85
91
  sourceId: string;
86
92
  targetId: string;
87
- label: string;
93
+ label?: string | null;
94
+ /**
95
+ * Optional numeric weight for the edge.
96
+ * Used by pathfinding, MST, and other weighted algorithms.
97
+ * When `getWeight` is not provided, algorithms default to `edge.weight ?? 1`.
98
+ */
99
+ weight?: number;
88
100
  data: TEdgeData;
89
101
  x?: number;
90
102
  y?: number;
@@ -139,7 +151,7 @@ interface PathOptions<TEdgeData = any> {
139
151
  from?: string;
140
152
  /** Target node ID. If omitted → paths to all reachable nodes */
141
153
  to?: string;
142
- /** Edge weight function. Default: every edge = 1. */
154
+ /** Edge weight function. Default: `(e) => e.weight ?? 1`. */
143
155
  getWeight?: (edge: GraphEdge<TEdgeData>) => number;
144
156
  }
145
157
  interface SinglePathOptions<TEdgeData = any> {
@@ -147,8 +159,21 @@ interface SinglePathOptions<TEdgeData = any> {
147
159
  from?: string;
148
160
  /** Target node ID. Required for single-path queries. */
149
161
  to: string;
150
- /** Edge weight function. Default: every edge = 1. */
162
+ /** Edge weight function. Default: `(e) => e.weight ?? 1`. */
163
+ getWeight?: (edge: GraphEdge<TEdgeData>) => number;
164
+ }
165
+ interface AStarOptions<TEdgeData = any> {
166
+ /** Source node ID. */
167
+ from: string;
168
+ /** Target node ID. */
169
+ to: string;
170
+ /** Edge weight function. Default: `(e) => e.weight ?? 1`. */
151
171
  getWeight?: (edge: GraphEdge<TEdgeData>) => number;
172
+ /**
173
+ * Heuristic function estimating cost from a node to the target.
174
+ * Must be admissible (never overestimates the actual cost).
175
+ */
176
+ heuristic: (nodeId: string) => number;
152
177
  }
153
178
  interface TraversalOptions {
154
179
  /** Source node ID. Default: graph.initialNodeId, else sole inDegree-0 node */
@@ -157,13 +182,13 @@ interface TraversalOptions {
157
182
  interface MSTOptions<TEdgeData = any> {
158
183
  /** Algorithm to use. Default: 'prim'. */
159
184
  algorithm?: 'prim' | 'kruskal';
160
- /** Edge weight function. Default: every edge = 1. */
185
+ /** Edge weight function. Default: `(e) => e.weight ?? 1`. */
161
186
  getWeight?: (edge: GraphEdge<TEdgeData>) => number;
162
187
  }
163
188
  interface AllPairsShortestPathsOptions<TEdgeData = any> {
164
189
  /** Algorithm to use. Default: 'dijkstra'. */
165
190
  algorithm?: 'floyd-warshall' | 'dijkstra';
166
- /** Edge weight function. Default: every edge = 1. */
191
+ /** Edge weight function. Default: `(e) => e.weight ?? 1`. */
167
192
  getWeight?: (edge: GraphEdge<TEdgeData>) => number;
168
193
  }
169
194
  interface NodeChange<TNodeData = any> {
@@ -245,6 +270,33 @@ interface VisualGraphFormatConverter<TSerial, N = any, E = any, G = any> {
245
270
  /** Convert from the serialized format to a VisualGraph. */
246
271
  from(input: TSerial): VisualGraph<N, E, G>;
247
272
  }
273
+ interface WalkOptions<TEdgeData = any> {
274
+ /** Start node ID. Default: graph.initialNodeId, else sole inDegree-0 node */
275
+ from?: string;
276
+ /** Seed for deterministic RNG. Omit for Math.random. */
277
+ seed?: number;
278
+ /** Guard: only traverse edges where filter returns true. */
279
+ filter?: (edge: GraphEdge<TEdgeData>, context: WalkContext) => boolean;
280
+ /** Callback fired after each step. */
281
+ onStep?: (step: GraphStep<any, TEdgeData>, context: WalkContext) => void;
282
+ }
283
+ interface WeightedWalkOptions<TEdgeData = any> extends WalkOptions<TEdgeData> {
284
+ /** Edge weight function. Default: `(e) => e.weight ?? 1`. */
285
+ getWeight?: (edge: GraphEdge<TEdgeData>) => number;
286
+ }
287
+ interface WalkContext {
288
+ currentNodeId: string;
289
+ visitedNodes: Set<string>;
290
+ visitedEdges: Set<string>;
291
+ stepCount: number;
292
+ }
293
+ interface CoverageStats {
294
+ nodeCoverage: number;
295
+ edgeCoverage: number;
296
+ visitedNodes: string[];
297
+ visitedEdges: string[];
298
+ totalSteps: number;
299
+ }
248
300
  interface TransitionOptions<TState, TEvent> {
249
301
  /** Initial state to begin BFS exploration from. */
250
302
  initialState: TState;
@@ -262,4 +314,4 @@ interface TransitionOptions<TState, TEvent> {
262
314
  id?: string;
263
315
  }
264
316
  //#endregion
265
- export { TraversalOptions as C, VisualGraphFormatConverter as D, VisualGraphConfig as E, VisualNode as O, TransitionOptions as S, VisualGraph as T, MSTOptions as _, EntitiesConfig as a, PathOptions as b, Graph as c, GraphEdge as d, GraphFormatConverter as f, GraphStep as g, GraphPath as h, EdgeConfig as i, GraphConfig as l, GraphPatch as m, DeleteNodeOptions as n, EntitiesUpdate as o, GraphNode as p, EdgeChange as r, EntityRect as s, AllPairsShortestPathsOptions as t, GraphDiff as u, NodeChange as v, VisualEdge as w, SinglePathOptions as x, NodeConfig as y };
317
+ export { VisualNode as A, SinglePathOptions as C, VisualGraph as D, VisualEdge as E, WalkOptions as M, WeightedWalkOptions as N, VisualGraphConfig as O, PathOptions as S, TraversalOptions as T, GraphPath as _, EdgeChange as a, NodeChange as b, EntitiesUpdate as c, GraphConfig as d, GraphDiff as f, GraphPatch as g, GraphNode as h, DeleteNodeOptions as i, WalkContext as j, VisualGraphFormatConverter as k, EntityRect as l, GraphFormatConverter as m, AllPairsShortestPathsOptions as n, EdgeConfig as o, GraphEdge as p, CoverageStats as r, EntitiesConfig as s, AStarOptions as t, Graph as u, GraphStep as v, TransitionOptions as w, NodeConfig as x, MSTOptions as y };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@statelyai/graph",
3
3
  "type": "module",
4
- "version": "0.5.0",
4
+ "version": "0.7.0",
5
5
  "description": "A TypeScript-first graph library with plain JSON-serializable objects",
6
6
  "author": "David Khourshid <david@stately.ai>",
7
7
  "license": "MIT",