From cf7461ffeb4f53f3f50c9c692a2e4fdcecf17e82 Mon Sep 17 00:00:00 2001 From: Agando Date: Thu, 9 Jul 2026 06:00:32 +0200 Subject: auto: 2026-07-09_06:00 changes --- modules/core/module-smartctx.js | 112 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'modules/core/module-smartctx.js') diff --git a/modules/core/module-smartctx.js b/modules/core/module-smartctx.js index 0bfc91a..fc3882d 100644 --- a/modules/core/module-smartctx.js +++ b/modules/core/module-smartctx.js @@ -10,6 +10,7 @@ 'contexts': 'contexts — List all available context profiles', 'context-new': 'context-new — Create a new context profile', 'context-delete': 'context-delete — Delete a context profile', + 'context-auto': 'context-auto [on|off] — Toggle auto-routing (detects intent from user input)', 'smart-ask': 'smart-ask — Ask a question using the active context' }; } @@ -55,6 +56,7 @@ this.commands['contexts'] = () => this.listContexts(); this.commands['context-new'] = (args) => this.createContext(args); this.commands['context-delete'] = (args) => this.deleteContext(args); + this.commands['context-auto'] = (args) => this.toggleAutoRouting(args); this.commands['smart-ask'] = (args) => this.smartAsk(args); } setContext(args) { @@ -124,6 +126,116 @@ this.terminal.print(`Profile '${args[0]}' not found`, "error"); } } + // ─── Intent Detection Engine ─── + _getIntentPatterns() { + return { + 'coding': [ + /^code\b/i, /\b(edit|fix|write|create|implement|refactor|build)\s+(module|function|class|file|code|script)/i, + /\b(debug|bug|error|fix)\s+(code|module|function|script)/i, /\b(implement|write|create)\s+(a\s+)?(function|class|module|command)/i, + /\b(refactor|rewrite|optimize)\s+(code|module|function)/i, /\b(add\s+)(feature|command|functionality)/i + ], + 'debug': [ + /^debug\b/i, /\b(error|crash|bug|fail|traceback|exception|broken|not working)\b/i, + /\b(why|what happened|diagnose|investigate|find out why)\b/i, /\b(check|inspect|examine)\s+(log|error|issue|problem)/i + ], + 'research': [ + /^research\b/i, /\b(search|find|look up|research|investigate|analyze)\b/i, + /\b(what is|how does|tell me about|explain)\b.*\?$/i, /\b(find|search)\s+(for\s+)?(information|docs|documentation|article)/i + ], + 'creative': [ + /^brainstorm\b/i, /\b(create|design|imagine|generate|compose|draft)\s+(story|poem|art|design|idea|concept|mindmap)/i, + /\b(creative|inspire|innovative|novel)\b/i + ], + 'system-admin': [ + /^(config|setup|install|deploy|backup|restore|security|monitor|maintenance)\b/i, + /\b(configure|setup|install|deploy|backup|restore)\s+(system|server|module|service)/i, + /\b(security|permission|access|block|allow)\b/i + ], + 'chat': [ + /^(hi|hello|hey|how are you|what's up|good morning|good evening)\b/i, + /^.{0,30}\?$/ // short questions + ] + }; + } + autoDetectIntent(text) { + if (!text || typeof text !== 'string') return { profile: null, confidence: 0 }; + const patterns = this._getIntentPatterns(); + const scores = {}; + const lower = text.toLowerCase(); + + for (const [profile, regexes] of Object.entries(patterns)) { + let matches = 0; + for (const re of regexes) { + if (re.test(text) || re.test(lower)) matches++; + } + if (matches > 0) scores[profile] = matches; + } + + // Code snippet detection (bias toward coding) + if (/[{([\s]*['"`]/.test(text) || /\b(function|class|const|let|var|import|export|=>)\b/.test(text)) { + scores['coding'] = (scores['coding'] || 0) + 2; + } + + // Very short text → chat + if (text.length < 15 && !scores['coding'] && !scores['debug']) { + scores['chat'] = (scores['chat'] || 0) + 1; + } + + // Find best match + let bestProfile = null; + let bestScore = 0; + let totalScore = 0; + for (const [profile, score] of Object.entries(scores)) { + totalScore += score; + if (score > bestScore) { bestScore = score; bestProfile = profile; } + } + + // Calculate confidence (0-100) + const confidence = totalScore > 0 ? Math.round((bestScore / totalScore) * 100) : 0; + + return { profile: bestProfile, confidence }; + } + autoRouteContext(text) { + if (!text) return { switched: false, from: this.activeProfile, to: this.activeProfile, confidence: 0, reason: 'no input' }; + + // Check if auto-routing is enabled + const enabled = localStorage.getItem('webx11_context_auto') !== 'off'; + if (!enabled) return { switched: false, from: this.activeProfile, to: this.activeProfile, confidence: 0, reason: 'auto-routing disabled' }; + + const result = this.autoDetectIntent(text); + if (!result.profile || result.confidence < 70) { + return { switched: false, from: this.activeProfile, to: this.activeProfile, confidence: result.confidence, reason: 'confidence too low (<70%)' }; + } + + // Don't switch if already on the best profile + if (result.profile === this.activeProfile) { + return { switched: false, from: this.activeProfile, to: this.activeProfile, confidence: result.confidence, reason: 'already on best profile' }; + } + + // Check if the target profile exists + if (!this.contextProfiles[result.profile]) { + return { switched: false, from: this.activeProfile, to: this.activeProfile, confidence: result.confidence, reason: 'profile "' + result.profile + '" not found' }; + } + + // Switch + const from = this.activeProfile; + this.setContext([result.profile]); + return { switched: true, from: from, to: result.profile, confidence: result.confidence, reason: 'intent detected' }; + } + toggleAutoRouting(args) { + const val = args && args[0] ? args[0].toLowerCase() : null; + if (val === 'on' || val === 'true' || val === '1') { + localStorage.setItem('webx11_context_auto', 'on'); + this.terminal.print('✅ Auto-routing enabled — context will switch based on intent', 'success'); + } else if (val === 'off' || val === 'false' || val === '0') { + localStorage.setItem('webx11_context_auto', 'off'); + this.terminal.print('⛔ Auto-routing disabled — use context to switch manually', 'warning'); + } else { + const current = localStorage.getItem('webx11_context_auto') !== 'off'; + this.terminal.print('Auto-routing: ' + (current ? 'ON' : 'OFF'), 'info'); + this.terminal.print('Usage: context-auto on|off', 'info'); + } + } async smartAsk(args) { if (!args[0]) { this.terminal.print("Usage: smart-ask ", "warning"); return; -- cgit v1.2.3