v3/@claude-flow/cli/src/ruvector/README.md
Version: 0.1.95
Package: ruvector
Repository: https://github.com/ruvnet/ruvector
RuVector is a high-performance vector database for Node.js with automatic native/WASM fallback. It provides self-learning intelligence for Claude Code with Q-learning optimization, vector memory, and automatic agent routing.
npm install ruvector
Add to Claude Code:
claude mcp add ruvector-mcp -- npx ruvector mcp-server
The hooks API provides 30+ MCP tools for intelligent agent routing, code analysis, and self-learning capabilities.
hooks_routeRoutes a task to the best agent based on learned patterns.
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
task | string | Yes | Task description to route |
file | string | No | File path for context-aware routing |
Return Type:
interface RouteResult {
success: boolean;
task: string;
file?: string;
agent: string; // Recommended agent (e.g., "typescript-developer")
confidence: number; // Confidence score 0.0-1.0
reason: string; // Explanation for routing decision
alternates?: string[]; // Alternative agent suggestions
sonaPatterns?: number; // Number of SONA patterns used (if engine enabled)
engineRouted?: boolean; // Whether full engine was used
}
Example Usage:
// MCP Tool Call
const result = await mcp.call('hooks_route', {
task: 'implement user authentication',
file: 'src/auth/login.ts'
});
// CLI Usage
npx ruvector hooks route "implement user login"
Error Handling:
hooks_route_enhancedEnhanced routing using AST complexity, coverage, and diff analysis signals.
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
task | string | Yes | Task description |
file | string | No | File context for analysis |
Return Type:
interface EnhancedRouteResult {
success: boolean;
agent: string;
confidence: number;
signals: {
complexity?: number; // Cyclomatic complexity
coverage?: number; // Test coverage percentage
riskScore?: number; // Change risk assessment
diffCategory?: string; // feature/bugfix/refactor
};
explanation: string;
}
Example Usage:
const result = await mcp.call('hooks_route_enhanced', {
task: 'refactor authentication module',
file: 'src/auth/handlers.ts'
});
hooks_ast_analyzeParses file AST and extracts symbols, imports, and complexity metrics.
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
file | string | Yes | File path to analyze |
Return Type:
interface FileAnalysis {
file: string;
language: string; // TypeScript, JavaScript, Python, etc.
imports: ImportInfo[]; // Import statements
exports: ExportInfo[]; // Export statements
functions: FunctionInfo[]; // Function definitions
classes: ClassInfo[]; // Class definitions
variables: string[]; // Variable declarations
types: string[]; // Type definitions
complexity: number; // Overall complexity score
lines: number; // Total lines
parseTime: number; // Parse duration in ms
}
interface FunctionInfo {
name: string;
params: string[];
returnType?: string;
async: boolean;
exported: boolean;
startLine: number;
endLine: number;
complexity: number;
calls: string[]; // Functions called within
}
interface ClassInfo {
name: string;
extends?: string;
implements: string[];
methods: FunctionInfo[];
properties: string[];
exported: boolean;
startLine: number;
endLine: number;
}
interface ImportInfo {
source: string;
default?: string;
named: string[];
namespace?: string;
type: 'esm' | 'commonjs' | 'dynamic';
}
Example Usage:
const analysis = await mcp.call('hooks_ast_analyze', {
file: 'src/api/routes.ts'
});
// CLI Usage
npx ruvector hooks ast-analyze src/api/routes.ts --json
Error Handling:
{ success: false, error: message } if file not foundhooks_ast_complexityCalculates cyclomatic and cognitive complexity metrics for files.
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
files | string[] | Yes | Array of file paths to analyze |
threshold | number | No | Warn if complexity exceeds (default: 10) |
Return Type:
interface ComplexityResult {
success: boolean;
files: Array<{
file: string;
complexity: number;
functions: Array<{
name: string;
complexity: number;
exceeds: boolean;
}>;
}>;
warnings: string[];
averageComplexity: number;
}
Example Usage:
const result = await mcp.call('hooks_ast_complexity', {
files: ['src/auth/*.ts'],
threshold: 15
});
hooks_diff_analyzeAnalyzes git diff with semantic embeddings and risk scoring.
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
commit | string | No | Commit hash (defaults to staged changes) |
Return Type:
interface CommitAnalysis {
hash: string;
message: string;
author: string;
date: string;
files: DiffAnalysis[];
totalAdditions: number;
totalDeletions: number;
riskScore: number; // 0.0-1.0 risk assessment
embedding?: number[]; // Semantic embedding
}
interface DiffAnalysis {
file: string;
hunks: DiffHunk[];
totalAdditions: number;
totalDeletions: number;
complexity: number;
riskScore: number;
category: 'feature' | 'bugfix' | 'refactor' | 'docs' | 'test' | 'config' | 'unknown';
embedding?: number[];
}
interface DiffHunk {
file: string;
oldStart: number;
oldLines: number;
newStart: number;
newLines: number;
content: string;
additions: string[];
deletions: string[];
}
Example Usage:
// Analyze staged changes
const staged = await mcp.call('hooks_diff_analyze', {});
// Analyze specific commit
const commit = await mcp.call('hooks_diff_analyze', {
commit: 'abc123'
});
// CLI Usage
npx ruvector hooks diff-analyze --json
npx ruvector hooks diff-analyze abc123 --json
Error Handling:
hooks_diff_classifyClassifies change type based on diff patterns.
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
commit | string | No | Commit hash (defaults to HEAD) |
Return Type:
interface ClassifyResult {
success: boolean;
category: 'feature' | 'bugfix' | 'refactor' | 'docs' | 'test' | 'config' | 'unknown';
confidence: number;
indicators: string[]; // Patterns that led to classification
}
Example Usage:
const classification = await mcp.call('hooks_diff_classify', {
commit: 'HEAD~1'
});
hooks_coverage_routeGets coverage-aware agent routing for a file.
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
file | string | Yes | File to analyze |
Return Type:
interface CoverageRouteResult {
success: boolean;
file: string;
route: boolean; // Whether to route to tester
reason: string; // Explanation
coverage: number; // Coverage percentage (0-100)
weights: {
coder: number; // Weight for coder agent
tester: number; // Weight for tester agent
reviewer: number; // Weight for reviewer agent
};
}
Example Usage:
const routing = await mcp.call('hooks_coverage_route', {
file: 'src/services/auth.ts'
});
// CLI Usage
npx ruvector hooks coverage-route src/services/auth.ts
Error Handling:
hooks_coverage_suggestSuggests tests for files based on coverage data.
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
files | string[] | Yes | Files to analyze |
Return Type:
interface TestSuggestion {
file: string;
testFile: string; // Suggested test file path
reason: string;
priority: 'high' | 'medium' | 'low';
coverage: number; // Current coverage %
uncoveredFunctions: string[];
}
interface SuggestResult {
success: boolean;
suggestions: TestSuggestion[];
overall: {
lines: number;
functions: number;
branches: number;
};
}
Example Usage:
const suggestions = await mcp.call('hooks_coverage_suggest', {
files: ['src/api/*.ts', 'src/services/*.ts']
});
hooks_graph_mincutFinds optimal code boundaries using MinCut algorithm (Stoer-Wagner).
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
files | string[] | Yes | Files to analyze for module boundaries |
Return Type:
interface Partition {
groups: string[][]; // Two groups of files
cutWeight: number; // Weight of edges between groups
modularity: number; // Modularity score
}
interface MinCutResult {
success: boolean;
partition: Partition;
bridges: Array<{ // Critical connections
from: string;
to: string;
}>;
articulationPoints: string[]; // Critical nodes
}
Example Usage:
const boundaries = await mcp.call('hooks_graph_mincut', {
files: ['src/**/*.ts']
});
// CLI Usage
npx ruvector hooks graph-mincut src/**/*.ts
Error Handling:
hooks_graph_clusterDetects code communities using spectral or Louvain clustering.
Input Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
files | string[] | Yes | Files to analyze |
method | string | No | 'spectral' or 'louvain' (default: 'louvain') |
clusters | number | No | Number of clusters for spectral (default: 3) |
Return Type:
interface ClusterResult {
success: boolean;
method: string;
clusters: Map<string, number>; // file -> cluster ID
numClusters: number;
modularity: number;
// For spectral method only:
eigenvalues?: number[];
coordinates?: Map<string, number[]>;
}
Example Usage:
// Louvain community detection (automatic cluster count)
const louvain = await mcp.call('hooks_graph_cluster', {
files: ['src/**/*.ts'],
method: 'louvain'
});
// Spectral clustering (specify cluster count)
const spectral = await mcp.call('hooks_graph_cluster', {
files: ['src/**/*.ts'],
method: 'spectral',
clusters: 5
});
// CLI Usage
npx ruvector hooks graph-cluster src/**/*.ts --method louvain
npx ruvector hooks graph-cluster src/**/*.ts --method spectral --clusters 5
| Tool | Description |
|---|---|
hooks_remember | Store context in vector memory |
hooks_recall | Search vector memory for relevant context |
| Tool | Description |
|---|---|
hooks_learn | Combined learning: record experience and get recommendation |
hooks_batch_learn | Record multiple experiences in batch |
hooks_learning_config | Configure learning algorithms (9 algorithms available) |
hooks_learning_stats | Get learning statistics |
| Tool | Description |
|---|---|
hooks_trajectory_begin | Begin tracking execution trajectory |
hooks_trajectory_step | Add step to current trajectory |
hooks_trajectory_end | End trajectory with quality score |
| Tool | Description |
|---|---|
hooks_security_scan | Parallel vulnerability pattern detection |
| Tool | Description |
|---|---|
hooks_attention_info | Get available attention mechanisms |
hooks_gnn_info | Get GNN layer capabilities |
hooks_rag_context | RAG-enhanced context retrieval |
import { VectorDb } from 'ruvector';
const db = new VectorDb({
dimensions: 384, // Must match embedding model
maxElements: 10000,
storagePath: './vectors.db'
});
// Insert
await db.insert({
id: 'doc1',
vector: new Float32Array(384),
metadata: { title: 'Document 1' }
});
// Search
const results = await db.search({
vector: queryVector,
k: 5,
threshold: 0.7
});
// Get
const doc = await db.get('doc1');
// Delete
await db.delete('doc1');
// AST Parser
import { getCodeParser } from 'ruvector/dist/core/ast-parser';
const parser = getCodeParser();
await parser.init();
const analysis = await parser.analyze('file.ts');
// Diff Embeddings
import diffEmbeddings from 'ruvector/dist/core/diff-embeddings';
const commit = await diffEmbeddings.analyzeCommit('HEAD');
const similar = await diffEmbeddings.findSimilarCommits(diff, 50, 5);
// Coverage Router
import coverage from 'ruvector/dist/core/coverage-router';
const data = coverage.getFileCoverage('file.ts');
const tests = coverage.suggestTests(['file.ts']);
// Graph Algorithms
import graph from 'ruvector/dist/core/graph-algorithms';
const g = graph.buildGraph(nodes, edges);
const partition = graph.minCut(g);
const clusters = graph.louvainCommunities(g);
| Operation | Latency | Notes |
|---|---|---|
| ONNX inference | ~400ms | Initial embedding |
| HNSW search | ~0.045ms | 8,800x faster than inference |
| Memory cache | ~0.01ms | 40,000x speedup |
| Native Rust | <0.5ms | p50 latency |
| WASM fallback | 10-50ms | Universal compatibility |
| Variable | Default | Description |
|---|---|---|
RUVECTOR_INTELLIGENCE_ENABLED | true | Enable/disable intelligence |
RUVECTOR_LEARNING_RATE | 0.1 | Q-learning rate (0.0-1.0) |
RUVECTOR_MEMORY_BACKEND | rvlite | Memory storage backend |
INTELLIGENCE_MODE | treatment | A/B testing mode |
# Initialize hooks
npx ruvector hooks init --pretrain --build-agents quality
# Verify setup
npx ruvector hooks verify
npx ruvector hooks doctor --fix
# Analysis
npx ruvector hooks ast-analyze <file> --json
npx ruvector hooks ast-complexity <files> --threshold 10
npx ruvector hooks diff-analyze [commit] --json
npx ruvector hooks diff-classify [commit]
npx ruvector hooks coverage-route <file>
npx ruvector hooks coverage-suggest <files>
npx ruvector hooks graph-mincut <files>
npx ruvector hooks graph-cluster <files> --method louvain
# Memory
npx ruvector hooks remember "context" -t project
npx ruvector hooks recall "query"
npx ruvector hooks route "task description"
# Stats and export
npx ruvector hooks stats
npx ruvector hooks export -o backup.json
npx ruvector hooks import backup.json --merge
@modelcontextprotocol/sdk: ^1.0.0@ruvector/attention: ^0.1.3@ruvector/core: ^0.1.25@ruvector/gnn: ^0.1.22@ruvector/sona: ^0.1.4@xenova/transformers: ^2.17.2