UNPKG

vibe-coder-mcp

Version:

Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.

326 lines (325 loc) 13.5 kB
export function detectFullContext(comment, structuralContext) { const domains = [ ...detectDomainContext(comment), ...detectNamingContext(structuralContext?.name), ...detectNamingContext(structuralContext?.parentClass) ]; return { structural: structuralContext || { type: 'function' }, domains: [...new Set(domains)], confidence: calculateContextConfidence(comment, domains) }; } function detectDomainContext(comment) { const contexts = []; const lowerComment = comment.toLowerCase(); if (/\b(auth|login|password|token|session|credential|jwt|oauth|signin|signup|logout)\b/.test(lowerComment)) { contexts.push('authentication'); } if (/\b(database|db|query|sql|table|record|entity|repository|orm|migration|schema)\b/.test(lowerComment)) { contexts.push('database'); } if (/\b(api|endpoint|request|response|http|rest|graphql|route|controller)\b/.test(lowerComment)) { contexts.push('api'); } if (/\b(cache|redis|memory|store|expire|ttl|invalidate|evict)\b/.test(lowerComment)) { contexts.push('cache'); } if (/\b(service|manager|handler|processor|worker|job|task)\b/.test(lowerComment)) { contexts.push('service'); } if (/\b(validate|validation|verify|check|sanitize|clean|format)\b/.test(lowerComment)) { contexts.push('validation'); } if (/\b(file|directory|path|read|write|upload|download|stream)\b/.test(lowerComment)) { contexts.push('file'); } return contexts; } function detectNamingContext(name) { if (!name) return []; const contexts = []; const lowerName = name.toLowerCase(); if (/auth|login|password|token|session|jwt|credential/.test(lowerName)) { contexts.push('authentication'); } if (/repository|dao|entity|model|query|db|table|record/.test(lowerName)) { contexts.push('database'); } if (/controller|router|endpoint|handler|middleware/.test(lowerName)) { contexts.push('api'); } if (/service|manager|processor|worker|job/.test(lowerName)) { contexts.push('service'); } if (/cache|redis|memory|store/.test(lowerName)) { contexts.push('cache'); } return contexts; } function calculateContextConfidence(comment, domains) { if (domains.length === 0) return 0.1; const wordCount = comment.split(/\s+/).length; const domainTermCount = domains.length; return Math.min(0.9, (domainTermCount / wordCount) * 2); } export function extractSemanticKeywords(comment, context) { const keywords = []; const actionVerbs = comment.match(/\b(creates?|handles?|manages?|processes?|validates?|generates?|calculates?|performs?|executes?|returns?|gets?|sets?|builds?|parses?|formats?|converts?|transforms?|filters?|sorts?|searches?|finds?|loads?|saves?|updates?|deletes?|removes?|adds?|inserts?)\b/gi); if (actionVerbs) { keywords.push(...actionVerbs.map(verb => verb.toLowerCase())); } const domainTerms = extractDomainTerms(comment, context); keywords.push(...domainTerms); const purposeMatches = comment.match(/\b(for|to|that|which)\s+(\w+(?:\s+\w+){0,2})/gi); if (purposeMatches) { purposeMatches.forEach(match => { const words = match.split(/\s+/).slice(1); keywords.push(...words.map(word => word.toLowerCase())); }); } const importantNouns = comment.match(/\b[A-Z][a-z]+(?:[A-Z][a-z]+)*\b/g); if (importantNouns) { keywords.push(...importantNouns.map(noun => noun.toLowerCase())); } return [...new Set(keywords)]; } export function compressSemanticContent(comment, keywords) { let compressed = comment; compressed = compressed .replace(/\bthis (function|method|class|property|file|module|component)\b/gi, '') .replace(/\bis used (to|for)\b/gi, '') .replace(/\bprovides? (a|an|the)?\s*/gi, '') .replace(/\breturn[s]?\s+(a|an|the)\s+/gi, 'returns ') .replace(/\brepresents? (a|an|the)?\s*/gi, '') .replace(/\bcontains? (a|an|the)?\s*/gi, 'has ') .replace(/\bimplements? (a|an|the)?\s*/gi, 'implements ') .replace(/\bdefines? (a|an|the)?\s*/gi, 'defines ') .replace(/\s+/g, ' ') .trim(); if (compressed.length > 50) { const sentences = compressed.split(/[.!?]+/); if (sentences.length > 1) { const prioritized = sentences .map(sentence => ({ text: sentence.trim(), score: calculateSemanticScore(sentence, keywords), length: sentence.trim().length })) .filter(item => item.text.length > 0) .sort((a, b) => { if (b.score !== a.score) return b.score - a.score; return a.length - b.length; }); if (prioritized.length > 0) { compressed = prioritized[0].text; } } } return compressed; } export function selectBestKeywords(comment, maxLength, context) { if (comment.length <= maxLength) return comment; const meaningfulTerms = extractMeaningfulTerms(comment); const semanticCore = preserveSemanticCore(meaningfulTerms); const contextEnhanced = enhanceWithContext(semanticCore, comment, context); const optimized = applySelectiveAbbreviations(contextEnhanced, maxLength); const result = validateAndFinalize(optimized, comment, maxLength); return result; } function extractMeaningfulTerms(comment) { const words = comment.toLowerCase() .replace(/[^\w\s-]/g, ' ') .split(/\s+/) .filter(word => word.length > 0); const meaningfulTerms = { actions: [], objects: [], descriptors: [], domains: [] }; const actionVerbs = [ 'validates', 'manages', 'processes', 'handles', 'creates', 'generates', 'executes', 'retrieves', 'stores', 'updates', 'deletes', 'checks', 'verifies', 'authenticates', 'authorizes', 'encrypts', 'decrypts', 'compresses', 'decompresses', 'parses', 'formats', 'transforms', 'converts', 'filters', 'sorts', 'searches', 'finds', 'loads', 'saves', 'sends', 'receives', 'connects', 'disconnects', 'initializes', 'configures', 'optimizes', 'caches', 'invalidates', 'refreshes', 'synchronizes' ]; const objectNouns = [ 'user', 'users', 'credentials', 'password', 'token', 'tokens', 'session', 'sessions', 'data', 'record', 'records', 'file', 'files', 'query', 'queries', 'request', 'requests', 'response', 'responses', 'connection', 'connections', 'configuration', 'config', 'settings', 'options', 'parameters', 'metadata', 'schema', 'table', 'database', 'cache', 'memory', 'storage', 'repository', 'service', 'api', 'endpoint', 'route' ]; const descriptors = [ 'secure', 'encrypted', 'cached', 'optimized', 'validated', 'authenticated', 'authorized', 'compressed', 'formatted', 'parsed', 'filtered', 'sorted', 'synchronized', 'asynchronous', 'concurrent', 'parallel', 'distributed', 'scalable', 'reliable', 'efficient', 'fast', 'slow', 'large', 'small' ]; const domainTerms = [ 'auth', 'authentication', 'database', 'db', 'sql', 'api', 'http', 'rest', 'graphql', 'json', 'xml', 'html', 'css', 'javascript', 'typescript', 'python', 'java', 'security', 'encryption', 'validation' ]; for (const word of words) { if (actionVerbs.includes(word)) { meaningfulTerms.actions.push(word); } else if (objectNouns.includes(word)) { meaningfulTerms.objects.push(word); } else if (descriptors.includes(word)) { meaningfulTerms.descriptors.push(word); } else if (domainTerms.includes(word)) { meaningfulTerms.domains.push(word); } } return meaningfulTerms; } function preserveSemanticCore(terms) { const core = []; if (terms.actions.length > 0) { core.push(terms.actions[0]); } if (terms.objects.length > 0) { core.push(...terms.objects.slice(0, 2)); } if (terms.actions.length === 0 && terms.descriptors.length > 0) { core.push(terms.descriptors[0]); } return core; } function enhanceWithContext(core, comment, _context) { const enhanced = [...core]; const contextTerms = detectDomainContext(comment); for (const contextTerm of contextTerms) { const isRedundant = enhanced.some(term => term.includes(contextTerm) || contextTerm.includes(term)); if (!isRedundant && enhanced.length < 4) { const abbreviatedContext = getContextAbbreviation(contextTerm); if (abbreviatedContext && abbreviatedContext !== contextTerm) { enhanced.push(abbreviatedContext); } } } return enhanced; } function applySelectiveAbbreviations(terms, maxLength) { const currentLength = terms.join(' ').length; if (currentLength <= maxLength) { return terms; } const abbreviated = terms.map(term => { const abbrev = getSelectiveAbbreviation(term); return abbrev || term; }); return abbreviated; } function getContextAbbreviation(contextTerm) { const abbreviations = { 'authentication': 'auth', 'database': 'db', 'configuration': 'config', 'repository': 'repo', 'application': 'app' }; return abbreviations[contextTerm] || null; } function getSelectiveAbbreviation(term) { if (term.length <= 8) return null; const abbreviations = { 'authentication': 'auth', 'configuration': 'config', 'repository': 'repo', 'application': 'app', 'management': 'mgmt', 'processing': 'proc', 'generation': 'gen', 'initialization': 'init', 'validation': 'valid' }; return abbreviations[term] || null; } function validateAndFinalize(terms, originalComment, maxLength) { const result = terms.join(' '); if (result.length > maxLength) { const reduced = reduceToFit(terms, maxLength); return reduced; } if (!hasSemanticMeaning(result, originalComment)) { return intelligentTruncation(originalComment, maxLength); } return result; } function reduceToFit(terms, maxLength) { const priorityOrder = [...terms]; while (priorityOrder.length > 1 && priorityOrder.join(' ').length > maxLength) { priorityOrder.pop(); } return priorityOrder.join(' '); } function hasSemanticMeaning(result, original) { const words = result.split(' ').filter(w => w.length > 2); if (words.length < 2) return false; const originalWords = original.toLowerCase().split(/\s+/); const hasRelevantTerm = words.some(word => originalWords.some(orig => orig.includes(word) || word.includes(orig))); return hasRelevantTerm; } function intelligentTruncation(text, maxLength) { if (text.length <= maxLength) return text; const truncated = text.substring(0, maxLength); const lastSpace = truncated.lastIndexOf(' '); if (lastSpace > maxLength * 0.7) { return truncated.substring(0, lastSpace); } return truncated; } function extractDomainTerms(comment, context) { const terms = []; const progTerms = comment.match(/\b(API|HTTP|HTTPS|REST|GraphQL|JSON|XML|YAML|database|DB|cache|auth|authentication|authorization|config|configuration|util|utility|helper|service|controller|model|view|component|module|library|framework|middleware|router|handler|processor|manager|builder|factory|adapter|wrapper|decorator|observer|strategy|command|query|repository|entity|DTO|DAO)\b/gi); if (progTerms) { terms.push(...progTerms.map(term => term.toLowerCase())); } if (context?.type === 'class') { const classTerms = comment.match(/\b(manager|handler|processor|builder|factory|adapter|wrapper|controller|service|repository|entity|model|view|component)\b/gi); if (classTerms) { terms.push(...classTerms.map(term => term.toLowerCase())); } } if (context?.type === 'method' || context?.type === 'function') { const functionTerms = comment.match(/\b(validate|process|handle|manage|create|build|parse|format|convert|transform|filter|sort|search|find|load|save|update|delete|remove|add|insert|get|set|fetch|send|receive|execute|run|start|stop|init|initialize|cleanup|destroy)\b/gi); if (functionTerms) { terms.push(...functionTerms.map(term => term.toLowerCase())); } } if (context?.type === 'file') { const fileTerms = comment.match(/\b(test|spec|config|configuration|utility|helper|service|controller|model|view|component|module|library|types|interface|constants|enum)\b/gi); if (fileTerms) { terms.push(...fileTerms.map(term => term.toLowerCase())); } } return terms; } function calculateSemanticScore(sentence, keywords) { let score = 0; const lowerSentence = sentence.toLowerCase(); keywords.forEach(keyword => { if (lowerSentence.includes(keyword)) { score += 1; } }); if (/^\s*(creates?|handles?|manages?|processes?|validates?|generates?|calculates?|performs?|executes?|returns?|gets?|sets?)/i.test(sentence)) { score += 0.5; } return score; }