UNPKG

@quantumai/quantum-cli-core

Version:

Quantum CLI Core - Multi-LLM Collaboration System

121 lines 4.04 kB
/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { UNCERTAINTY_PATTERNS, COMPLEXITY_PATTERNS, CRITICAL_KEYWORDS, } from './patterns.js'; import { UncertaintyLevel, QueryType, } from '../types.js'; export class UncertaintyDetector { detect(response, context) { const scores = { linguistic: this.calculateLinguisticScore(response), complexity: this.calculateComplexityScore(response), critical: this.calculateCriticalityScore(response, context), }; const level = this.aggregateScores(scores); const reasons = this.extractReasons(response, scores); const suggestedAction = this.recommendAction(level, context); return { level, reasons, suggestedAction, }; } calculateLinguisticScore(response) { let score = 0; let _matchCount = 0; for (const pattern of UNCERTAINTY_PATTERNS) { if (pattern.pattern.test(response)) { score += pattern.weight; _matchCount++; } } // Normalize by the number of patterns matched return _matchCount > 0 ? score / _matchCount : 0; } calculateComplexityScore(response) { let score = 0; let _matchCount = 0; for (const pattern of COMPLEXITY_PATTERNS) { if (pattern.pattern.test(response)) { score += pattern.weight; _matchCount++; } } // Check response length as complexity indicator const wordCount = response.split(/\s+/).length; if (wordCount > 200) { score += 0.2; } return _matchCount > 0 ? Math.min(score / _matchCount, 1.0) : 0; } calculateCriticalityScore(response, context) { let score = 0; for (const pattern of CRITICAL_KEYWORDS) { if (pattern.pattern.test(response)) { score += pattern.weight; } } // Context-based criticality if (context?.type === QueryType.SECURITY) { score += 0.3; } if (context?.domain && /production|critical|security/i.test(context.domain)) { score += 0.2; } return Math.min(score, 1.0); } aggregateScores(scores) { // Weighted average with emphasis on criticality const weightedScore = scores.linguistic * 0.25 + scores.complexity * 0.25 + scores.critical * 0.5; // Higher weight for critical if (weightedScore >= 0.8) { return UncertaintyLevel.CRITICAL; } else if (weightedScore >= 0.6) { return UncertaintyLevel.HIGH; } else if (weightedScore >= 0.4) { return UncertaintyLevel.MEDIUM; } else { return UncertaintyLevel.LOW; } } extractReasons(response, scores) { const reasons = []; if (scores.linguistic > 0.5) { reasons.push('uncertainty_language'); } if (scores.complexity > 0.5) { reasons.push('high_complexity'); } if (scores.critical > 0.5) { reasons.push('critical_context'); } // Check for specific patterns if (/trade-offs|pros and cons/i.test(response)) { reasons.push('multiple_valid_approaches'); } if (/security|vulnerability/i.test(response)) { reasons.push('security_implications'); } return reasons; } recommendAction(level, context) { if (level === UncertaintyLevel.CRITICAL) { return 'compare'; } if (level === UncertaintyLevel.HIGH) { return 'verify'; } if (context?.type === QueryType.SECURITY && level >= UncertaintyLevel.MEDIUM) { return 'verify'; } return 'accept'; } } //# sourceMappingURL=uncertainty-detector.js.map