agentflow-core 0.1.3 → 0.1.4

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.
@@ -224,6 +224,58 @@ function createGraphBuilder(config) {
224
224
  return builder;
225
225
  }
226
226
 
227
+ // src/loader.ts
228
+ function toNodesMap(raw) {
229
+ if (raw instanceof Map) return raw;
230
+ if (Array.isArray(raw)) {
231
+ return new Map(raw);
232
+ }
233
+ if (raw !== null && typeof raw === "object") {
234
+ return new Map(Object.entries(raw));
235
+ }
236
+ return /* @__PURE__ */ new Map();
237
+ }
238
+ function loadGraph(input) {
239
+ const raw = typeof input === "string" ? JSON.parse(input) : input;
240
+ const nodes = toNodesMap(raw.nodes);
241
+ return {
242
+ id: raw.id ?? "",
243
+ rootNodeId: raw.rootNodeId ?? raw.rootId ?? "",
244
+ nodes,
245
+ edges: raw.edges ?? [],
246
+ startTime: raw.startTime ?? 0,
247
+ endTime: raw.endTime ?? null,
248
+ status: raw.status ?? "completed",
249
+ trigger: raw.trigger ?? "unknown",
250
+ agentId: raw.agentId ?? "unknown",
251
+ events: raw.events ?? [],
252
+ traceId: raw.traceId,
253
+ spanId: raw.spanId,
254
+ parentSpanId: raw.parentSpanId
255
+ };
256
+ }
257
+ function graphToJson(graph) {
258
+ const nodesObj = {};
259
+ for (const [id, node] of graph.nodes) {
260
+ nodesObj[id] = node;
261
+ }
262
+ return {
263
+ id: graph.id,
264
+ rootNodeId: graph.rootNodeId,
265
+ nodes: nodesObj,
266
+ edges: graph.edges,
267
+ startTime: graph.startTime,
268
+ endTime: graph.endTime,
269
+ status: graph.status,
270
+ trigger: graph.trigger,
271
+ agentId: graph.agentId,
272
+ events: graph.events,
273
+ traceId: graph.traceId,
274
+ spanId: graph.spanId,
275
+ parentSpanId: graph.parentSpanId
276
+ };
277
+ }
278
+
227
279
  // src/runner.ts
228
280
  import { spawnSync } from "child_process";
229
281
  import { existsSync, mkdirSync, readdirSync, statSync, writeFileSync } from "fs";
@@ -259,27 +311,6 @@ function deriveAgentId(command) {
259
311
  function fileTimestamp() {
260
312
  return (/* @__PURE__ */ new Date()).toISOString().replace(/:/g, "-").replace(/\.\d+Z$/, "");
261
313
  }
262
- function graphToJson(graph) {
263
- const nodesObj = {};
264
- for (const [id, node] of graph.nodes) {
265
- nodesObj[id] = node;
266
- }
267
- return {
268
- id: graph.id,
269
- rootNodeId: graph.rootNodeId,
270
- nodes: nodesObj,
271
- edges: graph.edges,
272
- startTime: graph.startTime,
273
- endTime: graph.endTime,
274
- status: graph.status,
275
- trigger: graph.trigger,
276
- agentId: graph.agentId,
277
- events: graph.events,
278
- traceId: graph.traceId,
279
- spanId: graph.spanId,
280
- parentSpanId: graph.parentSpanId
281
- };
282
- }
283
314
  async function runTraced(config) {
284
315
  const {
285
316
  command,
@@ -398,5 +429,7 @@ async function runTraced(config) {
398
429
 
399
430
  export {
400
431
  createGraphBuilder,
432
+ loadGraph,
433
+ graphToJson,
401
434
  runTraced
402
435
  };
package/dist/cli.cjs CHANGED
@@ -235,6 +235,29 @@ function createGraphBuilder(config) {
235
235
  return builder;
236
236
  }
237
237
 
238
+ // src/loader.ts
239
+ function graphToJson(graph) {
240
+ const nodesObj = {};
241
+ for (const [id, node] of graph.nodes) {
242
+ nodesObj[id] = node;
243
+ }
244
+ return {
245
+ id: graph.id,
246
+ rootNodeId: graph.rootNodeId,
247
+ nodes: nodesObj,
248
+ edges: graph.edges,
249
+ startTime: graph.startTime,
250
+ endTime: graph.endTime,
251
+ status: graph.status,
252
+ trigger: graph.trigger,
253
+ agentId: graph.agentId,
254
+ events: graph.events,
255
+ traceId: graph.traceId,
256
+ spanId: graph.spanId,
257
+ parentSpanId: graph.parentSpanId
258
+ };
259
+ }
260
+
238
261
  // src/runner.ts
239
262
  function globToRegex(pattern) {
240
263
  const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
@@ -267,27 +290,6 @@ function deriveAgentId(command) {
267
290
  function fileTimestamp() {
268
291
  return (/* @__PURE__ */ new Date()).toISOString().replace(/:/g, "-").replace(/\.\d+Z$/, "");
269
292
  }
270
- function graphToJson(graph) {
271
- const nodesObj = {};
272
- for (const [id, node] of graph.nodes) {
273
- nodesObj[id] = node;
274
- }
275
- return {
276
- id: graph.id,
277
- rootNodeId: graph.rootNodeId,
278
- nodes: nodesObj,
279
- edges: graph.edges,
280
- startTime: graph.startTime,
281
- endTime: graph.endTime,
282
- status: graph.status,
283
- trigger: graph.trigger,
284
- agentId: graph.agentId,
285
- events: graph.events,
286
- traceId: graph.traceId,
287
- spanId: graph.spanId,
288
- parentSpanId: graph.parentSpanId
289
- };
290
- }
291
293
  async function runTraced(config) {
292
294
  const {
293
295
  command,
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  runTraced
4
- } from "./chunk-DGLK6IBP.js";
4
+ } from "./chunk-TT5DLU73.js";
5
5
 
6
6
  // src/cli.ts
7
7
  import { basename } from "path";
package/dist/index.cjs CHANGED
@@ -33,7 +33,9 @@ __export(index_exports, {
33
33
  getStats: () => getStats,
34
34
  getSubtree: () => getSubtree,
35
35
  getTraceTree: () => getTraceTree,
36
+ graphToJson: () => graphToJson,
36
37
  groupByTraceId: () => groupByTraceId,
38
+ loadGraph: () => loadGraph,
37
39
  runTraced: () => runTraced,
38
40
  stitchTrace: () => stitchTrace
39
41
  });
@@ -265,6 +267,58 @@ function createGraphBuilder(config) {
265
267
  return builder;
266
268
  }
267
269
 
270
+ // src/loader.ts
271
+ function toNodesMap(raw) {
272
+ if (raw instanceof Map) return raw;
273
+ if (Array.isArray(raw)) {
274
+ return new Map(raw);
275
+ }
276
+ if (raw !== null && typeof raw === "object") {
277
+ return new Map(Object.entries(raw));
278
+ }
279
+ return /* @__PURE__ */ new Map();
280
+ }
281
+ function loadGraph(input) {
282
+ const raw = typeof input === "string" ? JSON.parse(input) : input;
283
+ const nodes = toNodesMap(raw.nodes);
284
+ return {
285
+ id: raw.id ?? "",
286
+ rootNodeId: raw.rootNodeId ?? raw.rootId ?? "",
287
+ nodes,
288
+ edges: raw.edges ?? [],
289
+ startTime: raw.startTime ?? 0,
290
+ endTime: raw.endTime ?? null,
291
+ status: raw.status ?? "completed",
292
+ trigger: raw.trigger ?? "unknown",
293
+ agentId: raw.agentId ?? "unknown",
294
+ events: raw.events ?? [],
295
+ traceId: raw.traceId,
296
+ spanId: raw.spanId,
297
+ parentSpanId: raw.parentSpanId
298
+ };
299
+ }
300
+ function graphToJson(graph) {
301
+ const nodesObj = {};
302
+ for (const [id, node] of graph.nodes) {
303
+ nodesObj[id] = node;
304
+ }
305
+ return {
306
+ id: graph.id,
307
+ rootNodeId: graph.rootNodeId,
308
+ nodes: nodesObj,
309
+ edges: graph.edges,
310
+ startTime: graph.startTime,
311
+ endTime: graph.endTime,
312
+ status: graph.status,
313
+ trigger: graph.trigger,
314
+ agentId: graph.agentId,
315
+ events: graph.events,
316
+ traceId: graph.traceId,
317
+ spanId: graph.spanId,
318
+ parentSpanId: graph.parentSpanId
319
+ };
320
+ }
321
+
268
322
  // src/runner.ts
269
323
  var import_node_child_process = require("child_process");
270
324
  var import_node_fs = require("fs");
@@ -300,27 +354,6 @@ function deriveAgentId(command) {
300
354
  function fileTimestamp() {
301
355
  return (/* @__PURE__ */ new Date()).toISOString().replace(/:/g, "-").replace(/\.\d+Z$/, "");
302
356
  }
303
- function graphToJson(graph) {
304
- const nodesObj = {};
305
- for (const [id, node] of graph.nodes) {
306
- nodesObj[id] = node;
307
- }
308
- return {
309
- id: graph.id,
310
- rootNodeId: graph.rootNodeId,
311
- nodes: nodesObj,
312
- edges: graph.edges,
313
- startTime: graph.startTime,
314
- endTime: graph.endTime,
315
- status: graph.status,
316
- trigger: graph.trigger,
317
- agentId: graph.agentId,
318
- events: graph.events,
319
- traceId: graph.traceId,
320
- spanId: graph.spanId,
321
- parentSpanId: graph.parentSpanId
322
- };
323
- }
324
357
  async function runTraced(config) {
325
358
  const {
326
359
  command,
@@ -654,7 +687,9 @@ function getStats(graph) {
654
687
  getStats,
655
688
  getSubtree,
656
689
  getTraceTree,
690
+ graphToJson,
657
691
  groupByTraceId,
692
+ loadGraph,
658
693
  runTraced,
659
694
  stitchTrace
660
695
  });
package/dist/index.d.cts CHANGED
@@ -276,6 +276,58 @@ interface MutableExecutionNode {
276
276
  */
277
277
  declare function createGraphBuilder(config?: AgentFlowConfig): GraphBuilder;
278
278
 
279
+ /**
280
+ * Load and deserialize execution graphs from JSON.
281
+ *
282
+ * Handles all serialization formats produced by the runner, graph-builder,
283
+ * and third-party tools:
284
+ * - `nodes` as a plain object `{ "node_001": { ... } }` (runner.ts output)
285
+ * - `nodes` as an array of `[id, node]` pairs (Map JSON serialization)
286
+ * - `nodes` already a Map (in-memory passthrough)
287
+ *
288
+ * @module
289
+ */
290
+
291
+ /**
292
+ * Deserialize a JSON object (or JSON string) into a valid `ExecutionGraph`.
293
+ *
294
+ * Use this whenever you read a trace file from disk or receive one over the
295
+ * network. It normalizes `nodes` into a proper `Map` regardless of the
296
+ * serialization format.
297
+ *
298
+ * @param input - A parsed JSON object, or a JSON string to be parsed.
299
+ * @returns A valid `ExecutionGraph` ready for use with query functions.
300
+ * @throws {Error} If the input cannot be parsed or is missing required fields.
301
+ *
302
+ * @example
303
+ * ```ts
304
+ * import { readFileSync } from 'fs';
305
+ * import { loadGraph, getStats } from 'agentflow-core';
306
+ *
307
+ * const graph = loadGraph(readFileSync('trace.json', 'utf8'));
308
+ * console.log(getStats(graph));
309
+ * ```
310
+ */
311
+ declare function loadGraph(input: string | Record<string, unknown>): ExecutionGraph;
312
+ /**
313
+ * Serialize an `ExecutionGraph` to a plain JSON-safe object.
314
+ *
315
+ * The inverse of `loadGraph`. `nodes` is written as a plain object keyed by
316
+ * node ID, which is the most readable format for trace files on disk.
317
+ *
318
+ * @param graph - The execution graph to serialize.
319
+ * @returns A plain object safe to pass to `JSON.stringify`.
320
+ *
321
+ * @example
322
+ * ```ts
323
+ * import { writeFileSync } from 'fs';
324
+ * import { graphToJson } from 'agentflow-core';
325
+ *
326
+ * writeFileSync('trace.json', JSON.stringify(graphToJson(graph), null, 2));
327
+ * ```
328
+ */
329
+ declare function graphToJson(graph: ExecutionGraph): Record<string, unknown>;
330
+
279
331
  /**
280
332
  * CLI runner that wraps any command with automatic AgentFlow tracing.
281
333
  *
@@ -445,4 +497,4 @@ declare function getDepth(graph: ExecutionGraph): number;
445
497
  */
446
498
  declare function getStats(graph: ExecutionGraph): GraphStats;
447
499
 
448
- export { type Adapter, type AgentFlowConfig, type DistributedTrace, type EdgeType, type ExecutionEdge, type ExecutionGraph, type ExecutionNode, type GraphBuilder, type GraphStats, type GraphStatus, type MutableExecutionNode, type NodeStatus, type NodeType, type RunConfig, type RunResult, type StartNodeOptions, type TraceEvent, type TraceEventType, type Writer, createGraphBuilder, findWaitingOn, getChildren, getCriticalPath, getDepth, getDuration, getFailures, getHungNodes, getNode, getParent, getStats, getSubtree, getTraceTree, groupByTraceId, runTraced, stitchTrace };
500
+ export { type Adapter, type AgentFlowConfig, type DistributedTrace, type EdgeType, type ExecutionEdge, type ExecutionGraph, type ExecutionNode, type GraphBuilder, type GraphStats, type GraphStatus, type MutableExecutionNode, type NodeStatus, type NodeType, type RunConfig, type RunResult, type StartNodeOptions, type TraceEvent, type TraceEventType, type Writer, createGraphBuilder, findWaitingOn, getChildren, getCriticalPath, getDepth, getDuration, getFailures, getHungNodes, getNode, getParent, getStats, getSubtree, getTraceTree, graphToJson, groupByTraceId, loadGraph, runTraced, stitchTrace };
package/dist/index.d.ts CHANGED
@@ -276,6 +276,58 @@ interface MutableExecutionNode {
276
276
  */
277
277
  declare function createGraphBuilder(config?: AgentFlowConfig): GraphBuilder;
278
278
 
279
+ /**
280
+ * Load and deserialize execution graphs from JSON.
281
+ *
282
+ * Handles all serialization formats produced by the runner, graph-builder,
283
+ * and third-party tools:
284
+ * - `nodes` as a plain object `{ "node_001": { ... } }` (runner.ts output)
285
+ * - `nodes` as an array of `[id, node]` pairs (Map JSON serialization)
286
+ * - `nodes` already a Map (in-memory passthrough)
287
+ *
288
+ * @module
289
+ */
290
+
291
+ /**
292
+ * Deserialize a JSON object (or JSON string) into a valid `ExecutionGraph`.
293
+ *
294
+ * Use this whenever you read a trace file from disk or receive one over the
295
+ * network. It normalizes `nodes` into a proper `Map` regardless of the
296
+ * serialization format.
297
+ *
298
+ * @param input - A parsed JSON object, or a JSON string to be parsed.
299
+ * @returns A valid `ExecutionGraph` ready for use with query functions.
300
+ * @throws {Error} If the input cannot be parsed or is missing required fields.
301
+ *
302
+ * @example
303
+ * ```ts
304
+ * import { readFileSync } from 'fs';
305
+ * import { loadGraph, getStats } from 'agentflow-core';
306
+ *
307
+ * const graph = loadGraph(readFileSync('trace.json', 'utf8'));
308
+ * console.log(getStats(graph));
309
+ * ```
310
+ */
311
+ declare function loadGraph(input: string | Record<string, unknown>): ExecutionGraph;
312
+ /**
313
+ * Serialize an `ExecutionGraph` to a plain JSON-safe object.
314
+ *
315
+ * The inverse of `loadGraph`. `nodes` is written as a plain object keyed by
316
+ * node ID, which is the most readable format for trace files on disk.
317
+ *
318
+ * @param graph - The execution graph to serialize.
319
+ * @returns A plain object safe to pass to `JSON.stringify`.
320
+ *
321
+ * @example
322
+ * ```ts
323
+ * import { writeFileSync } from 'fs';
324
+ * import { graphToJson } from 'agentflow-core';
325
+ *
326
+ * writeFileSync('trace.json', JSON.stringify(graphToJson(graph), null, 2));
327
+ * ```
328
+ */
329
+ declare function graphToJson(graph: ExecutionGraph): Record<string, unknown>;
330
+
279
331
  /**
280
332
  * CLI runner that wraps any command with automatic AgentFlow tracing.
281
333
  *
@@ -445,4 +497,4 @@ declare function getDepth(graph: ExecutionGraph): number;
445
497
  */
446
498
  declare function getStats(graph: ExecutionGraph): GraphStats;
447
499
 
448
- export { type Adapter, type AgentFlowConfig, type DistributedTrace, type EdgeType, type ExecutionEdge, type ExecutionGraph, type ExecutionNode, type GraphBuilder, type GraphStats, type GraphStatus, type MutableExecutionNode, type NodeStatus, type NodeType, type RunConfig, type RunResult, type StartNodeOptions, type TraceEvent, type TraceEventType, type Writer, createGraphBuilder, findWaitingOn, getChildren, getCriticalPath, getDepth, getDuration, getFailures, getHungNodes, getNode, getParent, getStats, getSubtree, getTraceTree, groupByTraceId, runTraced, stitchTrace };
500
+ export { type Adapter, type AgentFlowConfig, type DistributedTrace, type EdgeType, type ExecutionEdge, type ExecutionGraph, type ExecutionNode, type GraphBuilder, type GraphStats, type GraphStatus, type MutableExecutionNode, type NodeStatus, type NodeType, type RunConfig, type RunResult, type StartNodeOptions, type TraceEvent, type TraceEventType, type Writer, createGraphBuilder, findWaitingOn, getChildren, getCriticalPath, getDepth, getDuration, getFailures, getHungNodes, getNode, getParent, getStats, getSubtree, getTraceTree, graphToJson, groupByTraceId, loadGraph, runTraced, stitchTrace };
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import {
2
2
  createGraphBuilder,
3
+ graphToJson,
4
+ loadGraph,
3
5
  runTraced
4
- } from "./chunk-DGLK6IBP.js";
6
+ } from "./chunk-TT5DLU73.js";
5
7
 
6
8
  // src/graph-stitch.ts
7
9
  function groupByTraceId(graphs) {
@@ -219,7 +221,9 @@ export {
219
221
  getStats,
220
222
  getSubtree,
221
223
  getTraceTree,
224
+ graphToJson,
222
225
  groupByTraceId,
226
+ loadGraph,
223
227
  runTraced,
224
228
  stitchTrace
225
229
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentflow-core",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Universal execution tracing for AI agent systems",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",