```javascript const mathKeywords = ['calculate', 'solve', 'equation', 'formula', 'math']; const chineseKeywords = ['chinese', 'mandarin', 'character', 'pinyin', 'translation']; const systemKeywords = ['error', 'fix', 'trouble', 'problem', 'crash']; export async function processInput(input, activeModule, context) { // Analyze input for potential module switching const lowerInput = input.toLowerCase(); // Detect module switch requests if (!activeModule) { if (mathKeywords.some(kw => lowerInput.includes(kw))) { return { response: "Would you like to switch to Math Reference module?", contextUpdate: { suggestedModule: 'math' } }; } if (chineseKeywords.some(kw => lowerInput.includes(kw))) { return { response: "Would you like to switch to Chinese Vocabulary module?", contextUpdate: { suggestedModule: 'chinese' } }; } if (systemKeywords.some(kw => lowerInput.includes(kw))) { return { response: "Would you like to switch to System Troubleshooting module?", contextUpdate: { suggestedModule: 'troubleshooting' } }; } } // Process based on active module switch (activeModule) { case 'math': return processMathInput(input, context); case 'chinese': return processChineseInput(input, context); case 'troubleshooting': return processSystemInput(input, context); default: return processGeneralInput(input, context); } } async function processMathInput(input, context) { // Implement secure math processing const safeEval = await import('mathjs'); try { const result = safeEval.evaluate(input); return { response: `The result is: ${result}`, contextUpdate: { lastCalculation: input, result } }; } catch { return { response: "I couldn't process that math expression. Could you rephrase?", contextUpdate: { error: 'math_processing' } }; } } async function processChineseInput(input, context) { // Mock Chinese processing - in real app would call API return { response: `Chinese vocabulary processing for: "${input}" (mock response)`, contextUpdate: { lastQuery: input } }; } async function processSystemInput(input, context) { // Mock troubleshooting - in real app would use knowledge base const solutions = { 'slow': 'Try freeing up memory by closing unused applications.', 'crash': 'Check for software updates and restart your device.', 'error': 'Could you specify the error message you received?' }; const solution = Object.entries(solutions).find(([key]) => input.toLowerCase().includes(key) ); return { response: solution ? solution[1] : "I'll need more details to help troubleshoot. What exactly is happening?", contextUpdate: { issue: input } }; } async function processGeneralInput(input, context) { // General conversation handling return { response: `I'm your AI assistant. You said: "${input}". How can I help further?`, contextUpdate: { lastTopic: input } }; } ```