GraphBuilder is a stateful builder class that accumulates nodes and edges from file analysis results and produces a complete KnowledgeGraph with a single call to build().
Constructor
Human-readable name for the project. Written into
KnowledgeGraph.project.name.Git commit SHA at the time of analysis. Written into
KnowledgeGraph.project.gitCommitHash.Methods
addFile()
id is set to file:<filePath>. The name is derived from the last path segment. Language detection is performed automatically from the file extension and the detected language is added to the internal language set.
Relative file path (e.g.
src/server/index.ts). Used as part of the node id and stored on the node.Metadata for the file node.
addFileWithAnalysis()
function and class nodes extracted from a full structural analysis. For each function and class, a contains edge is automatically created from the file node.
This method is used by the agent pipeline when deep per-file analysis has been performed.
Node IDs assigned:
- File:
file:<filePath> - Function:
func:<filePath>:<functionName> - Class:
class:<filePath>:<className>
Relative path to the source file.
Structural analysis result from a parser or tree-sitter plugin.
Extended metadata including per-symbol summaries.
addImportEdge()
imports edge between two file nodes with a default weight of 0.7.
Relative path of the importing file. The source node ID will be
file:<fromFile>.Relative path of the imported file. The target node ID will be
file:<toFile>.addCallEdge()
calls edge between two function nodes with a default weight of 0.8.
Relative path of the file containing the calling function.
Name of the calling function. The source node ID will be
func:<callerFile>:<callerFunc>.Relative path of the file containing the called function.
Name of the called function. The target node ID will be
func:<calleeFile>:<calleeFunc>.build()
KnowledgeGraph and returns it. Does not mutate the builder — you can continue calling addFile* methods and call build() again.
What build() sets automatically:
version: always"1.0.0"project.languages: sorted array of all detected languagesproject.frameworks:[](not populated byGraphBuilder)project.description:""(not populated byGraphBuilder)project.analyzedAt:new Date().toISOString()at call timelayers:[](populated separately bydetectLayers)tour:[](populated separately by tour generation)
KnowledgeGraph
Language detection
GraphBuilder automatically detects the programming language from the file extension and tracks it internally. The languages array on the built graph contains only languages actually seen in the added files.
Supported extension mappings:
| Extensions | Language |
|---|---|
.ts, .tsx | typescript |
.js, .jsx, .mjs, .cjs | javascript |
.py | python |
.rb | ruby |
.go | go |
.rs | rust |
.java | java |
.kt | kotlin |
.swift | swift |
.c, .h | c |
.cpp, .hpp | cpp |
.cs | csharp |
.php | php |
.lua | lua |
.sh, .bash, .zsh | shell |
.json | json |
.yaml, .yml | yaml |
.toml | toml |
.xml | xml |
.html | html |
.css | css |
.scss | scss |
.less | less |
.md | markdown |
.sql | sql |
languages list.
Usage example
Persistence utilities
The core package also exports utility functions for saving and loading graphs to/from disk. These write to and read from.understand-anything/knowledge-graph.json in the given project root directory.
saveGraph()
graph as pretty-printed JSON to <projectRoot>/.understand-anything/knowledge-graph.json. Creates the .understand-anything/ directory if it does not exist.
loadGraph()
<projectRoot>/.understand-anything/knowledge-graph.json. Returns null if the file does not exist.
By default (validate not set or true), the loaded data is validated against KnowledgeGraphSchema. Throws an Error with a descriptive message if validation fails. Pass { validate: false } to skip validation and return the raw parsed object.
saveMeta()
meta as JSON to <projectRoot>/.understand-anything/meta.json. Used by the pipeline to persist staleness-check data.
loadMeta()
<projectRoot>/.understand-anything/meta.json. Returns null if the file does not exist.