File size: 3,081 Bytes
eb854b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
```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 }
  };
}
```