|
| 1 | +/** |
| 2 | + * Example usage of progressive enhancement NLP API. |
| 3 | + * |
| 4 | + * Shows how the API gracefully falls back from: |
| 5 | + * CodeT5 → MiniLM → compromise |
| 6 | + */ |
| 7 | + |
| 8 | +import { |
| 9 | + analyzeCode, |
| 10 | + explainVulnerability, |
| 11 | + generateCode, |
| 12 | + getNLPCapabilities, |
| 13 | + getSentiment, |
| 14 | + semanticSimilarity, |
| 15 | + tokenize, |
| 16 | +} from './nlp.mts' |
| 17 | + |
| 18 | +/** |
| 19 | + * Example: Analyze package code for security issues. |
| 20 | + */ |
| 21 | +export async function analyzePackageSecurity(packageCode: string) { |
| 22 | + // Check what capabilities are available. |
| 23 | + const capabilities = await getNLPCapabilities() |
| 24 | + |
| 25 | + console.log('NLP Capabilities:') |
| 26 | + console.log(` Baseline (compromise): ${capabilities.baseline}`) |
| 27 | + console.log(` Enhanced (MiniLM): ${capabilities.enhanced}`) |
| 28 | + console.log(` CodeT5: ${capabilities.codet5}`) |
| 29 | + console.log() |
| 30 | + |
| 31 | + // Analyze code (uses CodeT5 if available, otherwise baseline). |
| 32 | + const analysis = await analyzeCode(packageCode, 'javascript') |
| 33 | + console.log('Code Analysis:') |
| 34 | + console.log(` Summary: ${analysis.summary}`) |
| 35 | + console.log(` Complexity: ${analysis.complexity}`) |
| 36 | + console.log() |
| 37 | + |
| 38 | + // If vulnerability found, explain it. |
| 39 | + if (packageCode.includes('eval(')) { |
| 40 | + const explanation = await explainVulnerability('Code Injection', packageCode) |
| 41 | + console.log('Vulnerability Explanation:') |
| 42 | + console.log(` ${explanation}`) |
| 43 | + console.log() |
| 44 | + } |
| 45 | + |
| 46 | + return analysis |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Example: Find similar packages using semantic search. |
| 51 | + */ |
| 52 | +export async function findSimilarPackages( |
| 53 | + query: string, |
| 54 | + packages: Array<{ name: string; description: string }> |
| 55 | +) { |
| 56 | + const capabilities = await getNLPCapabilities() |
| 57 | + |
| 58 | + console.log(`Searching for packages similar to: "${query}"`) |
| 59 | + console.log(`Using: ${capabilities.enhanced ? 'MiniLM embeddings' : 'compromise term overlap'}`) |
| 60 | + console.log() |
| 61 | + |
| 62 | + // Calculate similarity scores. |
| 63 | + const results = await Promise.all( |
| 64 | + packages.map(async pkg => ({ |
| 65 | + name: pkg.name, |
| 66 | + score: await semanticSimilarity(query, pkg.description), |
| 67 | + })) |
| 68 | + ) |
| 69 | + |
| 70 | + // Sort by similarity. |
| 71 | + results.sort((a, b) => b.score - a.score) |
| 72 | + |
| 73 | + console.log('Top 5 Similar Packages:') |
| 74 | + for (const result of results.slice(0, 5)) { |
| 75 | + console.log(` ${result.name}: ${(result.score * 100).toFixed(1)}% match`) |
| 76 | + } |
| 77 | + |
| 78 | + return results |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Example: Generate fix code for vulnerability. |
| 83 | + */ |
| 84 | +export async function generateVulnerabilityFix( |
| 85 | + vulnerableCode: string, |
| 86 | + vulnerabilityType: string |
| 87 | +) { |
| 88 | + const capabilities = await getNLPCapabilities() |
| 89 | + |
| 90 | + if (!capabilities.codet5) { |
| 91 | + console.log('⚠️ CodeT5 not available - code generation unavailable') |
| 92 | + console.log(' Install AI models for enhanced features') |
| 93 | + return null |
| 94 | + } |
| 95 | + |
| 96 | + console.log(`Generating fix for ${vulnerabilityType} vulnerability...`) |
| 97 | + |
| 98 | + const fixDescription = `Fix ${vulnerabilityType} in this code: ${vulnerableCode}` |
| 99 | + const fixedCode = await generateCode(fixDescription, 'javascript') |
| 100 | + |
| 101 | + if (fixedCode) { |
| 102 | + console.log('Generated Fix:') |
| 103 | + console.log(fixedCode) |
| 104 | + } else { |
| 105 | + console.log('❌ Code generation failed') |
| 106 | + } |
| 107 | + |
| 108 | + return fixedCode |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Example: Analyze package metadata sentiment. |
| 113 | + */ |
| 114 | +export async function analyzePackageMetadata(packageName: string, readme: string) { |
| 115 | + console.log(`Analyzing ${packageName} metadata...`) |
| 116 | + console.log() |
| 117 | + |
| 118 | + // Tokenize README. |
| 119 | + const tokens = await tokenize(readme) |
| 120 | + console.log(`README tokens: ${tokens.length}`) |
| 121 | + |
| 122 | + // Get sentiment. |
| 123 | + const sentiment = await getSentiment(readme) |
| 124 | + console.log(`Sentiment: ${sentiment.label} (${sentiment.score.toFixed(2)})`) |
| 125 | + |
| 126 | + return { |
| 127 | + sentiment, |
| 128 | + tokens: tokens.length, |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Example: Progressive enhancement in action. |
| 134 | + */ |
| 135 | +export async function demonstrateProgressiveEnhancement() { |
| 136 | + console.log('='.repeat(60)) |
| 137 | + console.log('Progressive Enhancement NLP Demo') |
| 138 | + console.log('='.repeat(60)) |
| 139 | + console.log() |
| 140 | + |
| 141 | + const capabilities = await getNLPCapabilities() |
| 142 | + |
| 143 | + // Feature tier indicator. |
| 144 | + const tier = capabilities.codet5 |
| 145 | + ? 'PREMIUM (CodeT5 + MiniLM + compromise)' |
| 146 | + : capabilities.enhanced |
| 147 | + ? 'ENHANCED (MiniLM + compromise)' |
| 148 | + : 'BASELINE (compromise only)' |
| 149 | + |
| 150 | + console.log(`Current Tier: ${tier}`) |
| 151 | + console.log() |
| 152 | + |
| 153 | + console.log('Available Features:') |
| 154 | + console.log(` ✓ Tokenization: ${capabilities.features.tokenization}`) |
| 155 | + console.log(` ${capabilities.features.embeddings ? '✓' : '✗'} Embeddings: ${capabilities.features.embeddings}`) |
| 156 | + console.log(` ✓ Semantic Similarity: ${capabilities.features.semanticSimilarity}`) |
| 157 | + console.log(` ✓ Named Entities: ${capabilities.features.namedEntities}`) |
| 158 | + console.log(` ✓ Sentiment: ${capabilities.features.sentiment}`) |
| 159 | + console.log(` ✓ Code Analysis: ${capabilities.features.codeAnalysis}`) |
| 160 | + console.log(` ✓ Vulnerability Explanation: ${capabilities.features.vulnerabilityExplanation}`) |
| 161 | + console.log(` ${capabilities.features.codeGeneration ? '✓' : '✗'} Code Generation: ${capabilities.features.codeGeneration}`) |
| 162 | + console.log() |
| 163 | + |
| 164 | + if (!capabilities.enhanced) { |
| 165 | + console.log('💡 Tip: Install MiniLM model for semantic search') |
| 166 | + } |
| 167 | + if (!capabilities.codet5) { |
| 168 | + console.log('💡 Tip: Install CodeT5 model for code generation') |
| 169 | + } |
| 170 | + |
| 171 | + console.log() |
| 172 | + console.log('='.repeat(60)) |
| 173 | +} |
0 commit comments