Files
wehub-resource-sync 23f7624596
ADR-166 MCP Bridge Security Lock / Static-source security lock (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / Compose default binds loopback + Mongo has auth (push) Failing after 2s
CodeQL Advanced / Analyze (rust) (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / plugin-agent-federation bindHost default (push) Failing after 1s
ADR-166 MCP Bridge Security Lock / Runtime behavior — 401 + terminal gate + fail-closed (push) Failing after 4s
business-pods-smoke / smoke (push) Failing after 1s
all-plugins-smoke / smoke-all (push) Failing after 2s
CI/CD Pipeline / Security & Code Quality (push) Failing after 1s
CI/CD Pipeline / Test Suite (ubuntu-latest) (push) Failing after 1s
CI/CD Pipeline / Build & Package (macos-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (ubuntu-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (windows-latest) (push) Has been skipped
CI/CD Pipeline / Documentation & Examples (push) Failing after 1s
Clone Tracker (14-day rolling) / Snapshot clones for ruflo ecosystem (push) Failing after 1s
CodeQL Advanced / Analyze (actions) (push) Failing after 1s
CodeQL Advanced / Analyze (javascript-typescript) (push) Failing after 1s
federation-peer-rust / stable-noop (push) Failing after 1s
metaharness-ci / score (push) Failing after 1s
metaharness-ci / router-compat (push) Failing after 0s
metaharness-ci / similarity-tests (push) Failing after 0s
no-agentbbs-smoke / smoke-without-agentbbs (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (windows-latest) (push) Has been skipped
codex-integration-audit / Codex integration audit (push) Failing after 1s
helpers-manifest-guard / guard (push) Failing after 1s
🔗 Cross-Agent Integration Tests / 🤝 Agent Coordination Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🧠 Memory Sharing Integration (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🛡️ Fault Tolerance Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / ⚡ Performance Integration Tests (push) Has been skipped
metaharness-ci / mcp-scan (push) Failing after 1s
metaharness-ci / eject-dryrun (push) Failing after 1s
metaharness-ci / metaharness-real-data (push) Failing after 0s
no-cli-optdep-bloat-2561 / guard (push) Failing after 1s
no-metaharness-smoke / smoke-without-metaharness (push) Failing after 1s
no-phantom-agentic-flow-subpath / guard (push) Failing after 1s
🔄 Automated Rollback Manager / 🚨 Failure Detection (push) Failing after 1s
V3 CI/CD Pipeline / Plugin hooks smoke / ubuntu-latest / Node 22 (push) Failing after 1s
V3 CI/CD Pipeline / ruflo-graph-intelligence build + test smoke (#2044, ADR-123) (push) Failing after 1s
CVE Audit Gate / Audit root (critical-blocking) (push) Failing after 2s
cost-tracker-smoke / smoke (push) Failing after 3s
oia-audit-weekly / audit (push) Failing after 2s
ruflo-agent-smoke / ruflo-agent structural smoke (push) Failing after 1s
📊 Status Badges Update / 📊 Update Status Badges (push) Failing after 1s
V3 CI/CD Pipeline / Static regression guards (#2267 YAML + (push) Failing after 1s
V3 CI/CD Pipeline / Test V3 Packages (push) Failing after 0s
V3 CI/CD Pipeline / agent_execute provider routing smoke (#2042) (push) Failing after 0s
CVE Audit Gate / Audit v3 (critical-blocking) (push) Failing after 1s
federation-peer-rust / stable-native (push) Failing after 2s
🔗 Cross-Agent Integration Tests / 🚀 Integration Test Setup (push) Failing after 2s
neural-trader-smoke / runtime-smoke (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (macos-latest) (push) Has been skipped
V3 CI/CD Pipeline / Build V3 (ubuntu-latest) (push) Has been skipped
V3 CI/CD Pipeline / Type Check V3 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 24 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 22 (push) Failing after 2s
V3 CI/CD Pipeline / browser rvf create flag smoke (#2015) (push) Failing after 0s
V3 CI/CD Pipeline / Dependency review (#2046) (push) Has been skipped
V3 CI/CD Pipeline / Supply-chain audit (#2046) (push) Failing after 0s
V3 CI/CD Pipeline / witness marker drift smoke (#2021) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader portfolio CG smoke (#2068, ADR-126 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader backtest signing smoke (#2068, ADR-126 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / kg-extract type-import classification smoke (#2049) (push) Failing after 0s
V3 CI/CD Pipeline / witness verify precondition smoke (#1880) (push) Failing after 2s
V3 CI/CD Pipeline / neural-trader pipeline risk-gate smoke (#2068, ADR-126 Phase 5) (push) Failing after 0s
V3 CI/CD Pipeline / neural-trader feature attribution smoke (#2068, ADR-126 Phase 6) (push) Failing after 0s
V3 CI/CD Pipeline / plugin-registry signature verification smoke (#1922, CWE-347) (push) Failing after 4s
V3 CI/CD Pipeline / memory stats legacy-DB smoke (#2120) (push) Failing after 4s
V3 CI/CD Pipeline / github deprecated actions smoke (#2089, ADR-127 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / graph query + pathfinder smoke (ADR-130 P2+P5) (push) Has been skipped
V3 CI/CD Pipeline / graph trajectory hooks smoke (ADR-130 P3) (push) Has been skipped
V3 CI/CD Pipeline / graph plugin adapter smoke (ADR-130 P4) (push) Has been skipped
V3 CI/CD Pipeline / graph benchmark (ADR-130 P6) (push) Has been skipped
V3 CI/CD Pipeline / statusline generator delegation smoke (#2195) (push) Failing after 1s
V3 CI/CD Pipeline / wizard init regression guard (#2206 (push) Failing after 1s
V3 CI/CD Pipeline / memory no-stray-db smoke (ADR-125 P7) (push) Failing after 1s
V3 CI/CD Pipeline / github-safe injection smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github actions pin smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github attribution opt-in smoke (#2089, ADR-127 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / pre-bash hook safety smoke (#2017) (push) Failing after 1s
V3 CI/CD Pipeline / Memory import smoke / ubuntu-latest (push) Failing after 0s
V3 CI/CD Pipeline / MCP protocol smoke / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / ruvllm WASM auto-init smoke (#2086) (push) Failing after 4s
V3 CI/CD Pipeline / MCP paired-tool round-trip smoke (#1889) (push) Failing after 1s
V3 CI/CD Pipeline / Plugin package install-safety (#1902/#1903/#1904) (push) Failing after 1s
V3 CI/CD Pipeline / Tool description discoverability (ADR-112) (push) Failing after 3s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (22) (push) Failing after 1s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (24) (push) Failing after 1s
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Vector-index dimension audit (#1947) (push) Failing after 0s
V3 CI/CD Pipeline / Hook-command install safety (#1921) (push) Failing after 1s
V3 CI/CD Pipeline / ToolOutputGuardrail smoke (ADR-131, (push) Failing after 1s
V3 CI/CD Pipeline / init-bundle invariants smoke (#2095, ADR-128 Phase 5) (push) Failing after 1s
V3 CI/CD Pipeline / wasm provider bridge smoke (ADR-129 P1) (push) Failing after 2s
V3 CI/CD Pipeline / wasm gallery CRUD smoke (ADR-129 P3) (push) Failing after 1s
V3 CI/CD Pipeline / wasm plugin bridge smoke (ADR-129 P4) (push) Failing after 0s
V3 CI/CD Pipeline / wasm compose smoke (ADR-129 P2) (push) Failing after 4s
V3 CI/CD Pipeline / graph schema smoke (ADR-130 P1) (push) Failing after 0s
Validate Marketplace / validate (push) Failing after 1s
🔍 Verification Pipeline / 🚀 Setup Verification (push) Failing after 1s
🔍 Verification Pipeline / 🛡️ Security Verification (push) Has been skipped
🔍 Verification Pipeline / 📝 Code Quality (push) Has been skipped
🔍 Verification Pipeline / 🧪 Test Verification (${{ matrix.os }}, Node ${{ matrix.node }}) (push) Has been skipped
🔍 Verification Pipeline / 🏗️ Build Verification (push) Has been skipped
🔍 Verification Pipeline / 📚 Documentation Verification (push) Has been skipped
CVE Audit Gate / High-severity report (warn only) (push) Has been cancelled
🔄 Automated Rollback Manager / 🔄 Execute Rollback (push) Has been cancelled
🔄 Automated Rollback Manager / ✅ Post-Rollback Verification (push) Has been cancelled
🔄 Automated Rollback Manager / 📊 Rollback Monitoring (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / windows-latest (push) Has been cancelled
🔄 Automated Rollback Manager / ⏳ Manual Rollback Approval (push) Has been cancelled
V3 CI/CD Pipeline / MCP protocol smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Memory import smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / ubuntu-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Publish to npm (alpha) (push) Has been cancelled
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / macos-latest / Node 22 (push) Has been cancelled
V3 CI/CD Pipeline / Plugin hooks smoke / macos-latest / Node 22 (push) Has been cancelled
CI/CD Pipeline / Deploy & Release (push) Has been cancelled
CI/CD Pipeline / CI Status (push) Has been cancelled
🔗 Cross-Agent Integration Tests / 📊 Integration Test Report (push) Has been cancelled
🔄 Automated Rollback Manager / 🔍 Pre-Rollback Validation (push) Has been cancelled
🔍 Verification Pipeline / ⚡ Performance Verification (push) Has been cancelled
🔍 Verification Pipeline / 📊 Verification Report (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:02:19 +08:00

577 lines
18 KiB
TypeScript

/**
* RuVector PostgreSQL Bridge - Semantic Code Search Example
*
* This example demonstrates:
* - Embedding code snippets for semantic search
* - Natural language code queries
* - Ranking and relevance scoring
* - Hybrid search (semantic + keyword)
*
* Run with: npx ts-node examples/ruvector/semantic-search.ts
*
* @module @claude-flow/plugins/examples/ruvector/semantic-search
*/
import {
createRuVectorBridge,
type RuVectorBridge,
type VectorRecord,
type VectorSearchOptions,
} from '../../src/integrations/ruvector/index.js';
// ============================================================================
// Configuration
// ============================================================================
const config = {
connection: {
host: process.env.POSTGRES_HOST || 'localhost',
port: parseInt(process.env.POSTGRES_PORT || '5432', 10),
database: process.env.POSTGRES_DB || 'vectors',
user: process.env.POSTGRES_USER || 'postgres',
password: process.env.POSTGRES_PASSWORD || 'postgres',
},
dimensions: 768, // Code embedding dimension (e.g., CodeBERT, StarCoder)
};
// ============================================================================
// Simulated Embedding Model
// ============================================================================
/**
* Simulated code embedding function.
* In production, use a proper code embedding model like:
* - CodeBERT
* - StarCoder
* - Code-LLaMA embeddings
* - OpenAI text-embedding-3-small
*/
class CodeEmbedder {
private dimension: number;
private vocabulary: Map<string, number[]> = new Map();
constructor(dimension: number) {
this.dimension = dimension;
this.initializeVocabulary();
}
/**
* Initialize semantic vocabulary for code concepts.
* This is a simplified simulation - real models use neural networks.
*/
private initializeVocabulary(): void {
const concepts = [
'function', 'class', 'variable', 'loop', 'condition',
'array', 'object', 'string', 'number', 'boolean',
'async', 'await', 'promise', 'callback', 'error',
'import', 'export', 'module', 'interface', 'type',
'auth', 'user', 'password', 'token', 'login',
'database', 'query', 'sql', 'insert', 'select',
'api', 'http', 'request', 'response', 'endpoint',
'test', 'expect', 'mock', 'assert', 'describe',
];
concepts.forEach((concept, idx) => {
const embedding = this.createConceptEmbedding(idx, concepts.length);
this.vocabulary.set(concept, embedding);
});
}
/**
* Create a deterministic embedding for a concept.
*/
private createConceptEmbedding(idx: number, total: number): number[] {
const embedding = new Array(this.dimension).fill(0);
const base = (idx / total) * Math.PI * 2;
for (let i = 0; i < this.dimension; i++) {
embedding[i] = Math.sin(base + (i / this.dimension) * Math.PI) * 0.5;
if (i % 2 === 0) {
embedding[i] += Math.cos(base * 2 + i * 0.01) * 0.3;
}
}
return this.normalize(embedding);
}
/**
* Embed code snippet.
*/
embed(code: string): number[] {
const tokens = this.tokenize(code);
const embedding = new Array(this.dimension).fill(0);
// Accumulate embeddings from vocabulary matches
let matchCount = 0;
for (const token of tokens) {
const tokenLower = token.toLowerCase();
if (this.vocabulary.has(tokenLower)) {
const tokenEmbedding = this.vocabulary.get(tokenLower)!;
for (let i = 0; i < this.dimension; i++) {
embedding[i] += tokenEmbedding[i];
}
matchCount++;
}
}
// Add noise for tokens not in vocabulary
const unknownRatio = (tokens.length - matchCount) / tokens.length;
for (let i = 0; i < this.dimension; i++) {
embedding[i] += (Math.random() * 2 - 1) * unknownRatio * 0.1;
}
return this.normalize(embedding);
}
/**
* Embed a natural language query.
*/
embedQuery(query: string): number[] {
return this.embed(query);
}
/**
* Tokenize code into meaningful tokens.
*/
private tokenize(code: string): string[] {
return code
.replace(/[^\w\s]/g, ' ')
.split(/\s+/)
.filter(t => t.length > 1);
}
/**
* Normalize vector to unit length.
*/
private normalize(vec: number[]): number[] {
const magnitude = Math.sqrt(vec.reduce((sum, v) => sum + v * v, 0));
if (magnitude === 0) return vec;
return vec.map(v => v / magnitude);
}
}
// ============================================================================
// Sample Code Repository
// ============================================================================
const codeSnippets = [
{
id: 'auth-login',
language: 'typescript',
filename: 'auth/login.ts',
code: `
async function login(username: string, password: string): Promise<AuthToken> {
const user = await findUserByUsername(username);
if (!user) {
throw new AuthenticationError('User not found');
}
const isValid = await verifyPassword(password, user.passwordHash);
if (!isValid) {
throw new AuthenticationError('Invalid password');
}
const token = generateJWT({ userId: user.id, role: user.role });
await saveSession(user.id, token);
return { token, expiresIn: 3600 };
}`,
tags: ['authentication', 'login', 'jwt', 'security'],
},
{
id: 'auth-middleware',
language: 'typescript',
filename: 'middleware/auth.ts',
code: `
export function authMiddleware(req: Request, res: Response, next: NextFunction) {
const token = req.headers.authorization?.replace('Bearer ', '');
if (!token) {
return res.status(401).json({ error: 'No token provided' });
}
try {
const decoded = verifyJWT(token);
req.user = decoded;
next();
} catch (error) {
return res.status(403).json({ error: 'Invalid token' });
}
}`,
tags: ['middleware', 'authentication', 'jwt', 'express'],
},
{
id: 'db-query',
language: 'typescript',
filename: 'database/query.ts',
code: `
async function executeQuery<T>(sql: string, params: unknown[]): Promise<T[]> {
const client = await pool.connect();
try {
const result = await client.query(sql, params);
return result.rows;
} catch (error) {
logger.error('Database query failed', { sql, error });
throw new DatabaseError('Query execution failed');
} finally {
client.release();
}
}`,
tags: ['database', 'postgresql', 'query', 'async'],
},
{
id: 'api-users',
language: 'typescript',
filename: 'api/users.ts',
code: `
router.get('/users/:id', async (req: Request, res: Response) => {
const { id } = req.params;
const user = await userService.findById(id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.json({
id: user.id,
username: user.username,
email: user.email,
createdAt: user.createdAt,
});
});`,
tags: ['api', 'rest', 'users', 'express'],
},
{
id: 'test-auth',
language: 'typescript',
filename: 'tests/auth.test.ts',
code: `
describe('Authentication', () => {
it('should login with valid credentials', async () => {
const result = await login('testuser', 'password123');
expect(result.token).toBeDefined();
expect(result.expiresIn).toBe(3600);
});
it('should reject invalid password', async () => {
await expect(login('testuser', 'wrong'))
.rejects
.toThrow('Invalid password');
});
});`,
tags: ['test', 'jest', 'authentication', 'unit-test'],
},
{
id: 'array-utils',
language: 'typescript',
filename: 'utils/array.ts',
code: `
export function chunk<T>(array: T[], size: number): T[][] {
const chunks: T[][] = [];
for (let i = 0; i < array.length; i += size) {
chunks.push(array.slice(i, i + size));
}
return chunks;
}
export function unique<T>(array: T[]): T[] {
return [...new Set(array)];
}
export function groupBy<T>(array: T[], key: keyof T): Record<string, T[]> {
return array.reduce((acc, item) => {
const groupKey = String(item[key]);
acc[groupKey] = acc[groupKey] || [];
acc[groupKey].push(item);
return acc;
}, {} as Record<string, T[]>);
}`,
tags: ['utility', 'array', 'functional'],
},
{
id: 'error-handler',
language: 'typescript',
filename: 'middleware/error.ts',
code: `
export function errorHandler(
error: Error,
req: Request,
res: Response,
next: NextFunction
) {
logger.error('Unhandled error', { error, path: req.path });
if (error instanceof ValidationError) {
return res.status(400).json({ error: error.message });
}
if (error instanceof AuthenticationError) {
return res.status(401).json({ error: error.message });
}
res.status(500).json({ error: 'Internal server error' });
}`,
tags: ['error-handling', 'middleware', 'express'],
},
{
id: 'cache-service',
language: 'typescript',
filename: 'services/cache.ts',
code: `
class CacheService {
private cache: Map<string, { value: unknown; expiry: number }> = new Map();
async get<T>(key: string): Promise<T | null> {
const entry = this.cache.get(key);
if (!entry) return null;
if (Date.now() > entry.expiry) {
this.cache.delete(key);
return null;
}
return entry.value as T;
}
async set(key: string, value: unknown, ttlSeconds: number): Promise<void> {
this.cache.set(key, {
value,
expiry: Date.now() + ttlSeconds * 1000,
});
}
}`,
tags: ['cache', 'memory', 'service'],
},
];
// ============================================================================
// Main Example
// ============================================================================
async function main(): Promise<void> {
console.log('RuVector PostgreSQL Bridge - Semantic Code Search Example');
console.log('=========================================================\n');
const bridge: RuVectorBridge = createRuVectorBridge({
connectionString: `postgresql://${config.connection.user}:${config.connection.password}@${config.connection.host}:${config.connection.port}/${config.connection.database}`,
});
const embedder = new CodeEmbedder(config.dimensions);
try {
await bridge.connect();
console.log('Connected to PostgreSQL\n');
// ========================================================================
// 1. Create Code Collection
// ========================================================================
console.log('1. Creating code collection...');
await bridge.createCollection('code_snippets', {
dimensions: config.dimensions,
distanceMetric: 'cosine',
indexType: 'hnsw',
indexParams: { m: 16, efConstruction: 100 },
});
console.log(' Collection created!\n');
// ========================================================================
// 2. Index Code Snippets
// ========================================================================
console.log('2. Indexing code snippets...');
for (const snippet of codeSnippets) {
const embedding = embedder.embed(snippet.code);
await bridge.insert('code_snippets', {
id: snippet.id,
embedding,
metadata: {
language: snippet.language,
filename: snippet.filename,
tags: snippet.tags,
codePreview: snippet.code.slice(0, 100) + '...',
},
});
console.log(` Indexed: ${snippet.filename}`);
}
console.log(' All snippets indexed!\n');
// ========================================================================
// 3. Natural Language Code Search
// ========================================================================
console.log('3. Natural language search examples:\n');
const queries = [
'How do I authenticate a user with username and password?',
'Show me how to handle errors in Express middleware',
'I need to query a PostgreSQL database',
'Unit test example for login functionality',
'How to implement caching with expiration?',
];
for (const query of queries) {
console.log(` Query: "${query}"`);
const queryEmbedding = embedder.embedQuery(query);
const results = await bridge.search('code_snippets', queryEmbedding, {
k: 2,
includeMetadata: true,
includeDistance: true,
});
console.log(' Results:');
results.forEach((result, i) => {
const similarity = 1 - (result.distance ?? 0);
console.log(` ${i + 1}. ${result.metadata?.filename} (similarity: ${(similarity * 100).toFixed(1)}%)`);
console.log(` Tags: ${(result.metadata?.tags as string[])?.join(', ')}`);
});
console.log();
}
// ========================================================================
// 4. Tag-Filtered Search
// ========================================================================
console.log('4. Tag-filtered search (authentication + middleware)...\n');
const authQuery = 'verify user credentials and protect routes';
const authEmbedding = embedder.embedQuery(authQuery);
const authResults = await bridge.search('code_snippets', authEmbedding, {
k: 5,
includeMetadata: true,
includeDistance: true,
filter: {
tags: { $contains: 'authentication' },
},
});
console.log(` Query: "${authQuery}"`);
console.log(' Filtered to: authentication tag');
console.log(' Results:');
authResults.forEach((result, i) => {
const similarity = 1 - (result.distance ?? 0);
console.log(` ${i + 1}. ${result.metadata?.filename} (similarity: ${(similarity * 100).toFixed(1)}%)`);
});
console.log();
// ========================================================================
// 5. Hybrid Search (Semantic + Keyword)
// ========================================================================
console.log('5. Hybrid search (semantic + keyword matching)...\n');
const hybridQuery = 'async function database';
const hybridEmbedding = embedder.embedQuery(hybridQuery);
// First, get semantic results
const semanticResults = await bridge.search('code_snippets', hybridEmbedding, {
k: 10,
includeMetadata: true,
includeDistance: true,
});
// Then, boost results that contain the keyword
const keyword = 'async';
const hybridResults = semanticResults
.map(result => {
const code = codeSnippets.find(s => s.id === result.id)?.code ?? '';
const keywordMatches = (code.match(new RegExp(keyword, 'gi')) || []).length;
const semanticScore = 1 - (result.distance ?? 0);
const keywordBoost = Math.min(keywordMatches * 0.05, 0.2);
const hybridScore = semanticScore * 0.7 + keywordBoost + 0.1;
return { ...result, hybridScore };
})
.sort((a, b) => b.hybridScore - a.hybridScore)
.slice(0, 3);
console.log(` Query: "${hybridQuery}"`);
console.log(' Results (semantic 70% + keyword boost 30%):');
hybridResults.forEach((result, i) => {
console.log(` ${i + 1}. ${result.metadata?.filename} (hybrid score: ${(result.hybridScore * 100).toFixed(1)}%)`);
});
console.log();
// ========================================================================
// 6. Similar Code Detection
// ========================================================================
console.log('6. Finding similar code to a reference snippet...\n');
const referenceId = 'auth-login';
const reference = await bridge.get('code_snippets', referenceId);
if (reference) {
const similarResults = await bridge.search('code_snippets', reference.embedding, {
k: 4,
includeMetadata: true,
includeDistance: true,
});
console.log(` Reference: ${reference.metadata?.filename}`);
console.log(' Similar code:');
similarResults
.filter(r => r.id !== referenceId) // Exclude self
.forEach((result, i) => {
const similarity = 1 - (result.distance ?? 0);
console.log(` ${i + 1}. ${result.metadata?.filename} (similarity: ${(similarity * 100).toFixed(1)}%)`);
});
}
console.log();
// ========================================================================
// 7. Relevance Feedback (Re-ranking)
// ========================================================================
console.log('7. Relevance feedback / re-ranking demo...\n');
// User marks 'auth-middleware' as relevant for authentication queries
const relevantId = 'auth-middleware';
const relevantDoc = await bridge.get('code_snippets', relevantId);
if (relevantDoc) {
// Combine original query with relevant document embedding
const originalQuery = 'protect API endpoints';
const originalEmbedding = embedder.embedQuery(originalQuery);
// Rocchio algorithm: query' = alpha * query + beta * relevant
const alpha = 0.7;
const beta = 0.3;
const expandedEmbedding = originalEmbedding.map(
(v, i) => alpha * v + beta * relevantDoc.embedding[i]
);
// Normalize
const magnitude = Math.sqrt(expandedEmbedding.reduce((s, v) => s + v * v, 0));
const normalizedEmbedding = expandedEmbedding.map(v => v / magnitude);
const rerankedResults = await bridge.search('code_snippets', normalizedEmbedding, {
k: 3,
includeMetadata: true,
includeDistance: true,
});
console.log(` Original query: "${originalQuery}"`);
console.log(` Relevant feedback: ${relevantDoc.metadata?.filename}`);
console.log(' Re-ranked results:');
rerankedResults.forEach((result, i) => {
const similarity = 1 - (result.distance ?? 0);
console.log(` ${i + 1}. ${result.metadata?.filename} (similarity: ${(similarity * 100).toFixed(1)}%)`);
});
}
// ========================================================================
// Cleanup
// ========================================================================
console.log('\n' + '='.repeat(60));
console.log('Semantic code search example completed!');
console.log('='.repeat(60));
} catch (error) {
console.error('Error:', error);
throw error;
} finally {
await bridge.disconnect();
console.log('\nDisconnected from PostgreSQL.');
}
}
main().catch(console.error);