Graph

Traverse the code graph for a single code version

Graph gives you a traversable view of a single code version — its functions, contracts, and declarations as nodes, and the relationships between them (calls, references, containment) as edges — plus lazy access to the underlying file contents and source segments. Construct it, call build() once to fetch everything over HTTP, then query and traverse it as much as you like without any further network calls.

import { BevorClient, Graph } from "@bevorai/sdk";

const client = new BevorClient({ apiKey: "YOUR_API_KEY" });
const graph = new Graph(client, codeId);
await graph.build();

If you've already fetched the nodes, edges, and files another way — an offline snapshot, say — skip the HTTP round trip with Graph.fromSnapshot(client, codeId, { nodes, edges, files }) instead of build(). isBuilt tells you whether either has happened.

Looking up nodes and files

Once built, getNode(nodeId) and getFile(fileId) are plain map lookups, and getAllNodes() / getAllFiles() give you everything. getRootNodes() narrows that to nodes with no incoming edges — useful as traversal starting points. Nodes have both a full id and a shorter display id; getShortFromId/getIdFromShort convert between them, and exists/existsShortId check either form without throwing.

const contract = graph
	.getRootNodes()
	.find((node) => node.node_type === "contract_declaration");
console.log(graph.getShortFromId(contract.id));

for (const funcId of graph.getNodesInFile(contract.file_id)) {
	const func = graph.getNode(funcId);
	// ...
}

languages (a getter) tells you which languages are actually present in the snapshot — handy for a monorepo mixing, say, Solidity and Rust — and getNodeLanguage(nodeId) gives you a single node's language.

Traversing edges

getEdgesForNode(nodeId, { direction: "out" | "in", edgeType }) is the main traversal primitive — omit direction for both, or edgeType for all edge kinds. Each returned edge is a copy with direction set relative to the node you asked about. getEdges({ edgeType }) does the same without a starting node, when you just want every edge of a given type.

const callers = graph.getEdgesForNode(func.id, { direction: "in", edgeType: "calls" });
const callees = graph.getEdgesForNode(func.id, { direction: "out", edgeType: "calls" });

A few higher-level traversals are built on top of that: getDistance(a, b) is shortest path length treating the graph as undirected (-1 if disconnected); isDescendant(child, parent) walks incoming calls edges to check reachability; getCallChain(nodeId) returns the full call chain as a CallNode tree; traverseUpToNodeType(nodeId, type) walks incoming edges until it finds a node of that type (e.g. "which contract is this function defined in?"); and getDescendantByMerkle(nodeId, merkleHash) walks outgoing edges looking for a descendant matching a content hash. getEdgeById(edgeId) resolves a specific edge by its row id when you already have one.

Reconstructing source

Reading a node's actual source code is lazy and cached: getFileContent(fileId) fetches (and caches) a file's full text the first time it's needed. reconstructChunk(nodeId, { withDocstring }) slices out just that node's segment — and with withDocstring: true, prefixes it with the node's docstring, resolving an @inheritdoc-style reference to the referenced node's own docstring where needed.

const snippet = await graph.reconstructChunk(func.id, { withDocstring: true });

Graph.reconstruct(node, source, docstring) is the lower-level static primitive reconstructChunk is built on, for when you already have the raw file bytes in hand as a Uint8Array — it slices by byte offset and sanitizes the result (long hex literals and large string literals get redacted rather than dumped in full).

Language-specific type sets

getCallableTypes, getDeclarationTypes, getExcludeTypes, and getContainerTypes each take a Language and return the set of AST node-type strings that classify as that category — functions/modifiers/constructors, enums/structs/events, statements that aren't treated as their own nodes, and contracts/libraries/interfaces, respectively. These are mostly used internally during traversal; currently defined for Solidity, with other languages returning an empty set.

Call Chains

getCallChain(nodeId) returns a CallNode — a tree rooted at that node, with each level holding its own children for every call it makes. Each node carries its own identity plus a children array of the same shape, so the structure mirrors the actual call graph rather than a flattened list — walking into children at any depth traces one full execution path from the root down.

const callChain = graph.getCallChain(func.id);

console.log(callChain.toText()); // flat, indented text representation for machines
const data = callChain.toDict(); // nested JSON — data.children for one level down

On this page