vibe-coder-mcp
Version:
Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.
49 lines (48 loc) • 2.52 kB
JavaScript
import { generateEmbedding, cosineSimilarity } from '../../utils/embeddingHelper.js';
import { toolEmbeddingStore } from './embeddingStore.js';
import logger from '../../logger.js';
const SEMANTIC_MATCH_THRESHOLD = 0.70;
const DESCRIPTION_WEIGHT = 0.6;
const USE_CASE_WEIGHT = 0.4;
export async function findBestSemanticMatch(request) {
logger.debug(`Starting semantic match for request: "${request.substring(0, 50)}..."`);
try {
const requestEmbedding = await generateEmbedding(request);
if (requestEmbedding.length === 0) {
logger.warn('Request embedding failed, cannot perform semantic match.');
return null;
}
let bestMatch = null;
let highestScore = -1;
for (const [toolName, embeddings] of toolEmbeddingStore.entries()) {
if (embeddings.descriptionEmbedding.length === 0 && embeddings.useCaseEmbeddings.length === 0) {
logger.debug(`Skipping tool ${toolName} as it has no embeddings.`);
continue;
}
const descSimilarity = embeddings.descriptionEmbedding.length > 0
? cosineSimilarity(requestEmbedding, embeddings.descriptionEmbedding)
: 0;
const useCaseSimilarities = embeddings.useCaseEmbeddings.map(ucVec => cosineSimilarity(requestEmbedding, ucVec));
const maxUseCaseSimilarity = useCaseSimilarities.length > 0 ? Math.max(...useCaseSimilarities) : 0;
const score = (descSimilarity * DESCRIPTION_WEIGHT) + (maxUseCaseSimilarity * USE_CASE_WEIGHT);
logger.debug(`Tool: ${toolName}, Desc Sim: ${descSimilarity.toFixed(3)}, Max UC Sim: ${maxUseCaseSimilarity.toFixed(3)}, Weighted Score: ${score.toFixed(3)}`);
if (score > highestScore) {
highestScore = score;
bestMatch = { toolName, confidence: score, matchedPattern: 'semantic_match' };
}
}
if (bestMatch && highestScore >= SEMANTIC_MATCH_THRESHOLD) {
logger.info(`Semantic match found: ${bestMatch.toolName} with score ${highestScore.toFixed(3)}`);
bestMatch.confidence = highestScore;
return bestMatch;
}
else {
logger.info(`No semantic match found above threshold ${SEMANTIC_MATCH_THRESHOLD}. Highest score: ${highestScore.toFixed(3)}`);
return null;
}
}
catch (error) {
logger.error({ err: error }, 'Error during semantic matching');
return null;
}
}