model
code_context_agent.tools.graph.model ¶
Core data structures for code graph analysis.
This module defines the fundamental types for representing code as a graph: - NodeType/EdgeType enums for classification - CodeNode/CodeEdge Pydantic models for graph elements - CodeGraph class wrapping NetworkX MultiDiGraph
NodeType ¶
Bases: Enum
Types of nodes in the code graph.
EdgeType ¶
Bases: Enum
Types of relationships between code elements.
CodeNode ¶
Bases: FrozenModel
A node in the code graph representing a code element.
Attributes:
| Name | Type | Description |
|---|---|---|
id | str | Unique identifier (typically "file_path:symbol_name" or "file_path:line") |
name | str | Human-readable display name |
node_type | NodeType | Classification of the code element |
file_path | str | Absolute path to the source file |
line_start | int | Starting line number (0-indexed) |
line_end | int | Ending line number (0-indexed) |
metadata | dict[str, Any] | Additional properties (docstring, visibility, rule_id, etc.) |
to_dict ¶
CodeEdge ¶
Bases: FrozenModel
An edge in the code graph representing a relationship.
Attributes:
| Name | Type | Description |
|---|---|---|
source | str | Source node ID |
target | str | Target node ID |
edge_type | EdgeType | Classification of the relationship |
weight | float | Edge weight for algorithms (default 1.0) |
metadata | dict[str, Any] | Additional properties (line where relationship occurs, etc.) |
to_dict ¶
CodeGraph ¶
Multi-layer code graph supporting multiple relationship types.
Wraps a NetworkX MultiDiGraph to support: - Multiple edge types between the same node pair - Node/edge attributes for metadata - Filtered views for specific relationship types
Initialize an empty code graph.
Source code in src/code_context_agent/tools/graph/model.py
add_node ¶
Add a node to the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node | CodeNode | The CodeNode to add | required |
Source code in src/code_context_agent/tools/graph/model.py
add_edge ¶
Add an edge to the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge | CodeEdge | The CodeEdge to add | required |
Source code in src/code_context_agent/tools/graph/model.py
has_node ¶
has_edge ¶
Check if an edge exists in the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source | str | Source node ID | required |
target | str | Target node ID | required |
edge_type | EdgeType | None | Optional edge type to check for specifically | None |
Source code in src/code_context_agent/tools/graph/model.py
get_node_data ¶
Get the data associated with a node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id | str | The node ID to look up | required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None | Dictionary of node attributes or None if not found |
Source code in src/code_context_agent/tools/graph/model.py
get_nodes_by_type ¶
Get all node IDs of a specific type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_type | NodeType | The type to filter by | required |
Returns:
| Type | Description |
|---|---|
list[str] | List of node IDs matching the type |
Source code in src/code_context_agent/tools/graph/model.py
get_edges_by_type ¶
Get all edges of a specific type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_type | EdgeType | The type to filter by | required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, dict[str, Any]]] | List of (source, target, data) tuples |
Source code in src/code_context_agent/tools/graph/model.py
get_view ¶
Get a filtered view of the graph for analysis algorithms.
Creates a simple DiGraph (not Multi) with only the specified edge types. Multiple edges between the same nodes are aggregated by summing weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_types | list[EdgeType] | None | List of edge types to include (None = all types) | None |
Returns:
| Type | Description |
|---|---|
DiGraph | A NetworkX DiGraph suitable for analysis algorithms |
Source code in src/code_context_agent/tools/graph/model.py
nodes ¶
Return nodes, optionally with data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | bool | If True, return (node_id, data) tuples | False |
Returns:
| Type | Description |
|---|---|
Any | Node view from underlying NetworkX graph |
Source code in src/code_context_agent/tools/graph/model.py
edges ¶
Return edges, optionally with data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | bool | If True, return (source, target, data) tuples | False |
Returns:
| Type | Description |
|---|---|
Any | Edge view from underlying NetworkX graph |
Source code in src/code_context_agent/tools/graph/model.py
to_node_link_data ¶
Export graph as node-link JSON format.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary suitable for JSON serialization |
from_node_link_data classmethod ¶
Create a CodeGraph from node-link JSON format.
Handles both old NetworkX format ("links" key) and new 3.6+ format ("edges" key) for backward compatibility with saved graphs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | Dictionary from node_link_data or JSON | required |
Returns:
| Type | Description |
|---|---|
CodeGraph | New CodeGraph instance |
Source code in src/code_context_agent/tools/graph/model.py
describe ¶
Get a quick summary of the graph.
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Dictionary with node count, edge count, type distributions, and density. |