Skip to content

Commit 06f5b1f

Browse files
grokifyclaude
andcommitted
feat(graph): add core graph types
Add Node, Edge, and Graph types for the graph database: - Node: entity with ID, Type, Label, and extensible Attrs map - Edge: relationship with From, To, Type, Confidence, and score - Graph: in-memory collection with CRUD operations - Confidence enum: EXTRACTED, INFERRED, AMBIGUOUS - Type constants for common node and edge types Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ca4c2fd commit 06f5b1f

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

pkg/graph/edge.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Package graph provides core types for the GraphFS graph database.
2+
package graph
3+
4+
// Edge represents a relationship between two nodes.
5+
type Edge struct {
6+
// From is the source node ID.
7+
From string `json:"from"`
8+
9+
// To is the target node ID.
10+
To string `json:"to"`
11+
12+
// Type categorizes the relationship (e.g., "calls", "imports", "implements").
13+
Type string `json:"type"`
14+
15+
// Confidence indicates how the edge was determined.
16+
Confidence Confidence `json:"confidence"`
17+
18+
// ConfidenceScore is a 0.0-1.0 score for INFERRED edges.
19+
// Only meaningful when Confidence is ConfidenceInferred.
20+
ConfidenceScore float64 `json:"confidence_score,omitempty"`
21+
22+
// Attrs holds additional attributes as key-value pairs.
23+
Attrs map[string]string `json:"attrs,omitempty"`
24+
}
25+
26+
// Confidence indicates how an edge relationship was determined.
27+
type Confidence string
28+
29+
const (
30+
// ConfidenceExtracted means the edge was directly extracted from source
31+
// (e.g., import statement, function call in AST).
32+
ConfidenceExtracted Confidence = "EXTRACTED"
33+
34+
// ConfidenceInferred means the edge was inferred by an LLM or heuristic,
35+
// with an associated confidence score.
36+
ConfidenceInferred Confidence = "INFERRED"
37+
38+
// ConfidenceAmbiguous means the relationship is uncertain and should be
39+
// reviewed by a human.
40+
ConfidenceAmbiguous Confidence = "AMBIGUOUS"
41+
)
42+
43+
// EdgeType constants for common relationship types.
44+
const (
45+
EdgeTypeCalls = "calls"
46+
EdgeTypeImports = "imports"
47+
EdgeTypeImplements = "implements"
48+
EdgeTypeExtends = "extends"
49+
EdgeTypeUses = "uses"
50+
EdgeTypeContains = "contains"
51+
EdgeTypeDependsOn = "depends_on"
52+
EdgeTypeReferences = "references"
53+
)

pkg/graph/graph.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Package graph provides core types for the GraphFS graph database.
2+
package graph
3+
4+
// Graph represents a complete graph with nodes and edges.
5+
type Graph struct {
6+
// Nodes maps node ID to Node.
7+
Nodes map[string]*Node `json:"nodes"`
8+
9+
// Edges holds all edges in the graph.
10+
Edges []*Edge `json:"edges"`
11+
}
12+
13+
// NewGraph creates an empty graph.
14+
func NewGraph() *Graph {
15+
return &Graph{
16+
Nodes: make(map[string]*Node),
17+
Edges: make([]*Edge, 0),
18+
}
19+
}
20+
21+
// AddNode adds a node to the graph. If a node with the same ID exists,
22+
// it will be overwritten.
23+
func (g *Graph) AddNode(n *Node) {
24+
g.Nodes[n.ID] = n
25+
}
26+
27+
// AddEdge adds an edge to the graph.
28+
func (g *Graph) AddEdge(e *Edge) {
29+
g.Edges = append(g.Edges, e)
30+
}
31+
32+
// GetNode returns a node by ID, or nil if not found.
33+
func (g *Graph) GetNode(id string) *Node {
34+
return g.Nodes[id]
35+
}
36+
37+
// NodeCount returns the number of nodes in the graph.
38+
func (g *Graph) NodeCount() int {
39+
return len(g.Nodes)
40+
}
41+
42+
// EdgeCount returns the number of edges in the graph.
43+
func (g *Graph) EdgeCount() int {
44+
return len(g.Edges)
45+
}

pkg/graph/node.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Package graph provides core types for the GraphFS graph database.
2+
package graph
3+
4+
// Node represents an entity in the graph.
5+
type Node struct {
6+
// ID is the unique, stable identifier for this node.
7+
// Should be deterministic (e.g., based on content hash or path+symbol).
8+
ID string `json:"id"`
9+
10+
// Type categorizes the node (e.g., "function", "file", "class", "module").
11+
Type string `json:"type"`
12+
13+
// Label is a human-readable name for display.
14+
Label string `json:"label,omitempty"`
15+
16+
// Attrs holds additional attributes as key-value pairs.
17+
Attrs map[string]string `json:"attrs,omitempty"`
18+
}
19+
20+
// NodeType constants for common node types.
21+
const (
22+
NodeTypeFunction = "function"
23+
NodeTypeMethod = "method"
24+
NodeTypeClass = "class"
25+
NodeTypeStruct = "struct"
26+
NodeTypeFile = "file"
27+
NodeTypePackage = "package"
28+
NodeTypeModule = "module"
29+
NodeTypeVariable = "variable"
30+
NodeTypeConstant = "constant"
31+
NodeTypeInterface = "interface"
32+
)

0 commit comments

Comments
 (0)