UNPKG

@ooples/token-optimizer-mcp

Version:

Intelligent context window optimization for Claude Code - store content externally via caching and compression, freeing up your context window for what matters

291 lines 8.36 kB
/** * Knowledge Graph Tool - 91% token reduction through intelligent graph caching * * Features: * - Build knowledge graphs from entities and relations * - Query graphs with pattern matching * - Find paths between entities (shortest, all, widest) * - Detect communities using Louvain, label propagation, modularity * - Rank nodes using PageRank, betweenness, closeness, eigenvector centrality * - Infer missing relations with confidence scoring * - Visualize graphs with force, hierarchical, circular, radial layouts * - Export graphs in multiple formats * - Merge multiple graphs * * Token Reduction Strategy: * - Graph structure caching (93% reduction, 1-hour TTL) * - Query result caching (90% reduction, 10-min TTL) * - Community detection caching (94% reduction, 30-min TTL) * - Ranking caching (92% reduction, 15-min TTL) */ import { CacheEngine } from '../../core/cache-engine.js'; import { TokenCounter } from '../../core/token-counter.js'; import { MetricsCollector } from '../../core/metrics.js'; export interface KnowledgeGraphOptions { operation: 'build-graph' | 'query' | 'find-paths' | 'detect-communities' | 'infer-relations' | 'visualize' | 'export-graph' | 'merge-graphs'; entities?: Array<{ id: string; type: string; properties: Record<string, any>; }>; relations?: Array<{ from: string; to: string; type: string; properties?: Record<string, any>; }>; pattern?: { nodes: Array<{ id?: string; type?: string; properties?: Record<string, any>; }>; edges: Array<{ from: string; to: string; type?: string; }>; }; sourceId?: string; targetId?: string; maxHops?: number; algorithm?: 'shortest' | 'all' | 'widest'; communityAlgorithm?: 'louvain' | 'label-propagation' | 'modularity'; minCommunitySize?: number; rankingAlgorithm?: 'pagerank' | 'betweenness' | 'closeness' | 'eigenvector'; confidenceThreshold?: number; maxInferences?: number; layout?: 'force' | 'hierarchical' | 'circular' | 'radial'; maxNodes?: number; includeLabels?: boolean; imageWidth?: number; imageHeight?: number; format?: 'json' | 'graphml' | 'dot' | 'csv' | 'cytoscape'; graphs?: Array<{ id: string; nodes: any[]; edges: any[]; }>; mergeStrategy?: 'union' | 'intersection' | 'override'; graphId?: string; useCache?: boolean; cacheTTL?: number; } export interface KnowledgeGraphResult { success: boolean; data: { graph?: { id: string; nodeCount: number; edgeCount: number; types: string[]; density?: number; avgDegree?: number; }; matches?: Array<{ nodes: Array<{ id: string; type: string; properties: Record<string, any>; }>; edges: Array<{ from: string; to: string; type: string; }>; score: number; }>; paths?: Array<{ nodes: string[]; edges: Array<{ from: string; to: string; type: string; }>; length: number; cost: number; }>; communities?: Array<{ id: number; members: string[]; size: number; density: number; modularity?: number; }>; rankings?: Array<{ nodeId: string; rank: number; score: number; }>; inferredRelations?: Array<{ from: string; to: string; type: string; confidence: number; evidence: string[]; }>; visualization?: { format: 'svg' | 'json'; data: string | object; width?: number; height?: number; }; export?: { format: string; data: string; size: number; }; merged?: { id: string; nodeCount: number; edgeCount: number; sourceGraphs: string[]; }; }; metadata: { tokensUsed?: number; tokensSaved?: number; cacheHit: boolean; queryTime: number; }; } export declare class KnowledgeGraphTool { private cache; private tokenCounter; private metrics; private graphs; constructor(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector); /** * Main execution method following Phase 1 architecture */ run(options: KnowledgeGraphOptions): Promise<KnowledgeGraphResult>; /** * Execute the requested operation */ private executeOperation; /** * Operation 1: Build knowledge graph from entities and relations */ private buildGraph; /** * Operation 2: Query graph with pattern matching */ private queryGraph; /** * Operation 3: Find paths between entities */ private findPaths; /** * Operation 4: Detect communities in the graph */ private detectCommunities; /** * Operation 5: Infer missing relations */ private inferRelations; /** * Operation 6: Visualize graph */ private visualizeGraph; /** * Operation 7: Export graph in various formats */ private exportGraph; /** * Operation 8: Merge multiple graphs */ private mergeGraphs; private generateCacheKey; private generateGraphId; private getDefaultGraphId; private generateNodeCombinations; private calculateMatchScore; private reconstructPath; private getPathEdges; private findAllPaths; private findWidestPath; private louvainCommunityDetection; private labelPropagation; private modularityCommunities; private calculateCommunityDensity; private calculateModularity; private getCommonNeighbors; private countShortPaths; private calculateInferenceConfidence; private inferRelationType; private calculatePageRank; private forceDirectedLayout; private hierarchicalLayout; private circularLayout; private radialLayout; private exportAsJSON; private exportAsGraphML; private exportAsDOT; private exportAsCSV; private exportAsCytoscape; private escapeXML; } export declare function getKnowledgeGraphTool(cache: CacheEngine, tokenCounter: TokenCounter, metrics: MetricsCollector): KnowledgeGraphTool; export declare const KNOWLEDGE_GRAPH_TOOL_DEFINITION: { name: string; description: string; inputSchema: { type: string; properties: { operation: { type: string; enum: string[]; description: string; }; entities: { type: string; description: string; }; relations: { type: string; description: string; }; pattern: { type: string; description: string; }; sourceId: { type: string; description: string; }; targetId: { type: string; description: string; }; algorithm: { type: string; description: string; }; layout: { type: string; enum: string[]; description: string; }; format: { type: string; enum: string[]; description: string; }; graphId: { type: string; description: string; }; useCache: { type: string; description: string; default: boolean; }; cacheTTL: { type: string; description: string; }; }; required: string[]; }; }; //# sourceMappingURL=knowledge-graph.d.ts.map