100 lines
3.6 KiB
TypeScript
100 lines
3.6 KiB
TypeScript
/**
|
|
* Knowledge Graph Module for @claude-flow/memory
|
|
*
|
|
* Builds a graph from MemoryEntry.references, computes PageRank,
|
|
* detects communities via label propagation, and provides
|
|
* graph-aware ranking for search results.
|
|
*
|
|
* Pure TypeScript - no external graph libraries.
|
|
* @module v3/memory/memory-graph
|
|
*/
|
|
import { EventEmitter } from 'node:events';
|
|
import type { IMemoryBackend, MemoryEntry, SearchResult } from './types.js';
|
|
export type EdgeType = 'reference' | 'similar' | 'temporal' | 'co-accessed' | 'causal';
|
|
export interface MemoryGraphConfig {
|
|
similarityThreshold?: number;
|
|
pageRankDamping?: number;
|
|
pageRankIterations?: number;
|
|
pageRankConvergence?: number;
|
|
maxNodes?: number;
|
|
enableAutoEdges?: boolean;
|
|
communityAlgorithm?: 'louvain' | 'label-propagation';
|
|
}
|
|
export interface GraphNode {
|
|
id: string;
|
|
category: string;
|
|
confidence: number;
|
|
accessCount: number;
|
|
createdAt: number;
|
|
}
|
|
export interface GraphEdge {
|
|
targetId: string;
|
|
type: EdgeType;
|
|
weight: number;
|
|
}
|
|
export interface RankedResult {
|
|
entry: MemoryEntry;
|
|
score: number;
|
|
pageRank: number;
|
|
combinedScore: number;
|
|
community?: string;
|
|
}
|
|
export interface GraphStats {
|
|
nodeCount: number;
|
|
edgeCount: number;
|
|
avgDegree: number;
|
|
communityCount: number;
|
|
pageRankComputed: boolean;
|
|
maxPageRank: number;
|
|
minPageRank: number;
|
|
}
|
|
/**
|
|
* Knowledge graph built from memory entry references.
|
|
* Supports PageRank, community detection (label propagation),
|
|
* and graph-aware result ranking blending vector similarity with structural importance.
|
|
*/
|
|
export declare class MemoryGraph extends EventEmitter {
|
|
private nodes;
|
|
private edges;
|
|
private reverseEdges;
|
|
private pageRanks;
|
|
private communities;
|
|
private config;
|
|
private dirty;
|
|
constructor(config?: MemoryGraphConfig);
|
|
/** Build graph from all entries in a backend. Creates nodes and reference edges. */
|
|
buildFromBackend(backend: IMemoryBackend, namespace?: string): Promise<void>;
|
|
/** Add a node from a MemoryEntry. Skips silently at maxNodes capacity. */
|
|
addNode(entry: MemoryEntry): void;
|
|
/** Add a directed edge. Skips if either node missing. Updates weight to max if exists. */
|
|
addEdge(sourceId: string, targetId: string, type: EdgeType, weight?: number): void;
|
|
/** Remove a node and all associated edges (both directions). */
|
|
removeNode(id: string): void;
|
|
/** Add similarity edges by searching backend. Returns count of edges added. */
|
|
addSimilarityEdges(backend: IMemoryBackend, entryId: string): Promise<number>;
|
|
/**
|
|
* Compute PageRank via power iteration with dangling node redistribution.
|
|
* Returns map of node ID to PageRank score.
|
|
*/
|
|
computePageRank(): Map<string, number>;
|
|
/** Detect communities using label propagation. Returns map of nodeId to communityId. */
|
|
detectCommunities(): Map<string, string>;
|
|
/**
|
|
* Rank search results blending vector similarity and PageRank.
|
|
* @param alpha - Weight for vector score (default 0.7). PageRank weight is (1 - alpha).
|
|
*/
|
|
rankWithGraph(searchResults: SearchResult[], alpha?: number): RankedResult[];
|
|
/** Get top N nodes by PageRank score. */
|
|
getTopNodes(n: number): Array<{
|
|
id: string;
|
|
pageRank: number;
|
|
community: string;
|
|
}>;
|
|
/** BFS neighbors from a node up to given depth. Excludes the start node. */
|
|
getNeighbors(id: string, depth?: number): Set<string>;
|
|
/** Get statistics about the current graph state. */
|
|
getStats(): GraphStats;
|
|
private hasEdge;
|
|
private shuffleArray;
|
|
}
|
|
//# sourceMappingURL=memory-graph.d.ts.map
|