MCPcopy Create free account
hub / github.com/bhouston/behave-graph / readGraphFromJSON

Function readGraphFromJSON

packages/core/src/Graphs/IO/readGraphFromJSON.ts:30–163  ·  view source on GitHub ↗
({
  graphJson,
  registry
}: {
  graphJson: GraphJSON;
  registry: IRegistry;
})

Source from the content-addressed store, hash-verified

28// Purpose:
29// - loads a node graph
30export function readGraphFromJSON({
31 graphJson,
32 registry
33}: {
34 graphJson: GraphJSON;
35 registry: IRegistry;
36}): GraphInstance {
37 const graphName = graphJson?.name || '';
38 const graphMetadata = graphJson?.metadata || {};
39
40 let variables: GraphVariables = {};
41 let customEvents: GraphCustomEvents = {};
42
43 if ('variables' in graphJson) {
44 variables = readVariablesJSON(registry.values, graphJson.variables ?? []);
45 }
46 if ('customEvents' in graphJson) {
47 customEvents = readCustomEventsJSON(
48 registry.values,
49 graphJson.customEvents ?? []
50 );
51 }
52
53 const nodesJson = graphJson?.nodes ?? [];
54
55 if (nodesJson.length === 0) {
56 Logger.warning('readGraphFromJSON: no nodes specified');
57 }
58
59 const graphApi = makeGraphApi({
60 ...registry,
61 variables,
62 customEvents
63 });
64
65 const nodes: GraphNodes = {};
66 // create new BehaviorNode instances for each node in the json.
67 for (let i = 0; i < nodesJson.length; i += 1) {
68 const nodeJson = nodesJson[i];
69 const node = readNodeJSON({
70 graph: graphApi,
71 registry,
72 nodeJson
73 });
74 const id = nodeJson.id;
75
76 if (id in nodes) {
77 throw new Error(
78 `can not create new node with id ${id} as one with that id already exists.`
79 );
80 }
81
82 nodes[id] = node;
83 }
84
85 // connect up the graph edges from BehaviorNode inputs to outputs. This is required to follow execution
86 Object.entries(nodes).forEach(([nodeId, node]) => {
87 // initialize the inputs by resolving to the reference nodes.

Callers 5

useGraphRunnerFunction · 0.90
execGraphFunction · 0.90

Calls 5

readVariablesJSONFunction · 0.85
readCustomEventsJSONFunction · 0.85
makeGraphApiFunction · 0.85
readNodeJSONFunction · 0.85
warningMethod · 0.80

Tested by

no test coverage detected