explain-builder module locates a specific file or function in the knowledge graph and builds a rich ExplainContext containing the target node, its children, its 1-hop neighbors, and its architectural layer. It also generates a formatted prompt that instructs an LLM to produce a thorough explanation.
ExplainContext
The data structure returned bybuildExplainContext.
Name of the analyzed project.
The original path string passed to
buildExplainContext (e.g. "src/auth.ts" or "src/auth.ts:login").The node that matches the given path.
null when no match is found in the graph.Nodes contained by
targetNode via edges of type "contains". Empty when targetNode is null.1-hop neighbors of
targetNode and childNodes that are not themselves children or the target. Excludes self-references.All edges where at least one endpoint is
targetNode or a child node.The first architectural layer whose
nodeIds contains targetNode.id. null if no layer matches.buildExplainContext
Builds a context for explaining a specific file or function.The full knowledge graph produced by the analysis pipeline.
Target to explain. Supports two formats:
- File path —
"src/auth.ts"matches any node whosefilePathequals this string. - File + function —
"src/auth.ts:login"matches a node whosefilePathis"src/auth.ts"and whosenameis"login".
"://" are never treated as path:function format.How it works
- Path parsing — If the path contains
:and no://, the part before the last:is treated as a file path and the part after is treated as a function name. - Node lookup — First tries the
path:functionmatch. Falls back to matching only onfilePathif no function match is found. - Child collection — Nodes linked from
targetNodeby"contains"edges becomechildNodes. - 1-hop neighbor collection — All edges touching
targetNodeor any child node are collected. Endpoints not already in the target/child set becomeconnectedNodes. - Layer lookup — The first layer whose
nodeIdsincludestargetNode.idis returned.
targetNode is null and all array fields are empty.
formatExplainPrompt
Formats anExplainContext as a structured prompt for LLM consumption.
An
ExplainContext produced by buildExplainContext.Not-found output
Whenctx.targetNode is null, the function returns a friendly error message that explains why the path may not be in the graph and suggests running /understand first.
Found output structure
When a target node is found, the returned string contains:| Section | Content |
|---|---|
| Header | Node name, type, complexity, file path, line range, summary |
## Architectural Layer | Layer name and description (omitted if no layer matched) |
## Internal Components | Child nodes with type and summary |
## Connected Components | 1-hop neighbors with type and summary |
## Relationships | source --[type]--> target lines (excludes "contains" edges) |
## Language Notes | Language-specific notes from the node (omitted if absent) |
## Instructions | Fixed 5-point prompt asking the LLM to explain the component |