knowledgegraph

package
v0.0.208 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package knowledgegraph turns a codebaseindex scan into a typed, confidence-tagged knowledge graph persisted in the semantic memory store. Extraction is deterministic and local first (components, packages, files, symbols, imports); an optional LLM pass can add inferred concept nodes and edges. Inspired by github.com/Graphify-Labs/graphify.

Index

Constants

Node and edge kinds are re-exported from semanticmemory so callers use one vocabulary end to end.

Variables

This section is empty.

Functions

func EdgeID

func EdgeID(namespace, sourceID, targetID, kind string) string

EdgeID builds a stable edge ID from its endpoints and kind. FNV-1a is used for compactness; the ID is a dedup key, not a security primitive.

func Enhance

func Enhance(g *Graph, fn EnhanceFunc) error

Enhance asks a model for concept nodes and semantic edges over the existing graph outline. Everything it adds is tagged inferred. A malformed response is an error for the caller to warn about, never a partial mutation.

func LocalID

func LocalID(id string) string

LocalID strips the namespace prefix from a stored node or edge ID.

func NodeID

func NodeID(namespace, local string) string

NodeID builds a stable, namespace-qualified node ID so rebuilds upsert in place and separate graphs never collide on the primary key.

func NodeLabel

func NodeLabel(n *semanticmemory.GraphNode) string

String renders a node for display.

func Rebuild

func Rebuild(ctx context.Context, store *semanticmemory.Store, g *Graph) error

Rebuild replaces the stored graph for a namespace with the given one.

func Save

func Save(ctx context.Context, store *semanticmemory.Store, g *Graph) error

Save persists a graph into the semantic memory store, upserting all nodes and edges and refreshing degree counts. It does not delete stale nodes; use Rebuild for a clean slate.

Types

type EnhanceFunc

type EnhanceFunc func(prompt string) (string, error)

EnhanceFunc matches the index enhancement hook signature: it takes a prompt and returns the model's response.

type ExportEdge

type ExportEdge struct {
	Source     string `json:"source"`
	Target     string `json:"target"`
	Kind       string `json:"kind"`
	Confidence string `json:"confidence"`
	Evidence   string `json:"evidence,omitempty"`
}

ExportEdge is the JSON export shape for an edge.

type ExportGraph

type ExportGraph struct {
	Namespace string       `json:"namespace"`
	Nodes     []ExportNode `json:"nodes"`
	Edges     []ExportEdge `json:"edges"`
}

ExportGraph is the top-level graph.json shape.

type ExportNode

type ExportNode struct {
	ID      string `json:"id"`
	Kind    string `json:"kind"`
	Name    string `json:"name"`
	Path    string `json:"path,omitempty"`
	Package string `json:"package,omitempty"`
	Summary string `json:"summary,omitempty"`
	Degree  int    `json:"degree"`
}

ExportNode is the JSON export shape for a node (graphify-style graph.json).

type Graph

type Graph struct {
	Namespace string
	Nodes     map[string]*semanticmemory.GraphNode
	Edges     map[string]*semanticmemory.GraphEdge
}

Graph is an in-memory knowledge graph under construction.

func Build

func Build(scan *codebaseindex.ScanResult, namespace string) *Graph

Build constructs a knowledge graph from a codebaseindex scan. Everything it emits is deterministic: structural edges are extracted from the scan, and `uses` edges between files and uniquely-named types are inferred from symbol name references.

func NewGraph

func NewGraph(namespace string) *Graph

NewGraph creates an empty graph for a namespace.

func (*Graph) AddEdge

func (g *Graph) AddEdge(sourceID, targetID, kind, confidence, evidence string)

AddEdge inserts an edge between two node IDs (already namespace-qualified). Duplicate (source, target, kind) edges collapse; the first confidence wins unless the existing one was inferred and the new one is extracted.

func (*Graph) AddNode

func (g *Graph) AddNode(localID, kind, name, path, pkg, summary string) *semanticmemory.GraphNode

AddNode inserts or replaces a node. The node's ID and namespace are set from the local ID and graph namespace.

type Querier

type Querier struct {
	// contains filtered or unexported fields
}

Querier runs read-only graph questions against the semantic memory store.

func NewQuerier

func NewQuerier(store *semanticmemory.Store, namespace string) *Querier

NewQuerier binds a querier to a store and namespace.

func (*Querier) Explain

func (q *Querier) Explain(ctx context.Context, name string) (string, error)

Explain renders a node and its connections, grouped by edge kind, with confidence tags. Mirrors `graphify explain`.

func (*Querier) Export

func (q *Querier) Export(ctx context.Context) (*ExportGraph, error)

Export dumps the namespace graph in the graphify-style graph.json shape.

func (*Querier) FindNodes

func (q *Querier) FindNodes(ctx context.Context, query string, limit int) ([]semanticmemory.GraphNode, error)

FindNodes returns the best matching nodes for a plain-text query.

func (*Querier) Path

func (q *Querier) Path(ctx context.Context, fromName, toName string) (string, error)

Path finds the shortest connection between two nodes (edges traversed in either direction) and renders the hop chain.

func (*Querier) PathHops

func (q *Querier) PathHops(ctx context.Context, fromName, toName string) ([]string, error)

PathHops returns the hop chain between two nodes for structured output. An empty slice means no path; a nil slice means both names are the same node.

func (*Querier) Query

func (q *Querier) Query(ctx context.Context, question string) (string, error)

Query answers a plain-language question with a scoped subgraph: the best matching nodes plus their immediate connections, as compact markdown.

func (*Querier) Resolve

func (q *Querier) Resolve(ctx context.Context, name string) (*semanticmemory.GraphNode, error)

Resolve exposes node resolution for callers that need the matched node itself (e.g. JSON output paths).

func (*Querier) ScopedExport

func (q *Querier) ScopedExport(ctx context.Context, seeds []semanticmemory.GraphNode, hops int) (*ExportGraph, error)

ScopedExport dumps the subgraph reachable within `hops` of the given seed nodes, in the same shape as Export. Used for --json query/explain output.

func (*Querier) Stats

func (q *Querier) Stats(ctx context.Context) (string, error)

Stats summarizes the graph: counts by kind and the highest-degree hub nodes (graphify's "god nodes").

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL