knowledgegraph

package
v0.0.242 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 12 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 RebuildWithProgress added in v0.0.215

func RebuildWithProgress(ctx context.Context, store *semanticmemory.Store, g *Graph, progress ProgressFunc) error

RebuildWithProgress replaces a graph and reports its persistence phases.

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 NeighborPage added in v0.0.220

type NeighborPage struct {
	Focus      ExportNode  `json:"focus"`
	Offset     int         `json:"offset"`
	NextOffset int         `json:"next_offset,omitempty"`
	HasMore    bool        `json:"has_more"`
	Graph      ExportGraph `json:"graph"`
}

NeighborPage is a bounded direct-neighborhood view. Clients cache returned pages and fetch the next offset only when the user asks to explore further.

type Overview added in v0.0.220

type Overview struct {
	Namespace      string         `json:"namespace"`
	Nodes          []ExportNode   `json:"nodes"`
	NodeKindCounts map[string]int `json:"node_kind_counts"`
}

Overview is the small architectural map shown before a visualizer loads any symbol-level detail. NodeKindCounts describes the complete graph without transferring every node.

type ProgressEvent added in v0.0.215

type ProgressEvent struct {
	Phase     string
	Current   string
	Completed int
	Total     int
}

ProgressEvent describes graph persistence work. Total covers nodes and edges, while Current identifies the graph object most recently written.

type ProgressFunc added in v0.0.215

type ProgressFunc func(ProgressEvent)

ProgressFunc receives optional graph persistence milestones.

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) Annotate added in v0.0.227

Annotate adds durable human guidance to an existing node.

func (*Querier) Annotations added in v0.0.227

Annotations returns durable human guidance for a node.

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) Namespace added in v0.0.219

func (q *Querier) Namespace() string

Namespace returns the graph namespace this querier is bound to.

func (*Querier) NeighborPage added in v0.0.220

func (q *Querier) NeighborPage(ctx context.Context, focus semanticmemory.GraphNode, limit, offset int) (*NeighborPage, error)

NeighborPage returns one bounded page of direct neighbors without scanning or serializing the complete graph.

func (*Querier) NodeByID added in v0.0.219

func (q *Querier) NodeByID(ctx context.Context, id string) (*semanticmemory.GraphNode, error)

NodeByID returns a graph node by either its stable stored ID or its local export ID. It rejects nodes from another namespace.

func (*Querier) Overview added in v0.0.220

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

Overview returns component nodes (or packages for an index without component classification), plus complete kind counts. It is the cheap first request for visualizing a large graph.

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