File size: 7,737 Bytes
e8438a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33a9c37
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MTOR - Intelligent, Script-Driven Support</title>
    <link rel="stylesheet" href="/static/style.css">
</head>
<body>

    <div class="main-container">
        <div class="title-container">
            <h1>πŸ€– MTOR - Intelligent, Script-Driven Support</h1>
        </div>
        <div class="subtitle">Your Personalized AI IT Support Assistant</div>
        <div class="description"><em>Facing a tech issue? Describe it below and let MTOR handle it for you.</em></div>

        <div class="input-label">πŸ’¬ <strong>Describe your IT issue:</strong></div>
        <textarea id="userInput" placeholder="e.g. My VPN keeps disconnecting every 10 minutes..." rows="6"></textarea>
        <button id="submitBtn" onclick="resolveIssue()">πŸš€ Resolve Now</button>
        
        <div id="loading" style="display: none;"><em>MTOR is resolving your issue... ⏳</em></div>

        <div id="responseSection" style="display: none;">
            <div class="success-box">βœ… **AI Response:**</div>
            <div id="aiResponseText"></div>

            <div id="batDownloadSection" class="download-section" style="display: none;">
                <p>⚑ <strong>Auto-Fix Script Available!</strong> Download and run this file to fix your issue automatically.</p>
                <button id="batBtn" class="download-btn">⬇️ Download Windows Fix (.bat)</button>
            </div>

            <div id="shDownloadSection" class="download-section" style="display: none;">
                <p>⚑ <strong>Auto-Fix Script Available!</strong> Download and run this file in your terminal to fix your issue automatically.</p>
                <button id="shBtn" class="download-btn">⬇️ Download Mac/Linux Fix (.sh)</button>
            </div>
        </div>

        <div id="feedbackSection" class="feedback-section" style="display: none;">
            <h3>πŸ™‹ Was this solution helpful?</h3>
            <div class="feedback-buttons">
                <button onclick="handleFeedback(true)">βœ… Yes, issue resolved</button>
                <button onclick="handleFeedback(false)">❌ No, not helpful</button>
            </div>
            <div id="feedbackResult"></div>
        </div>
    </div>

    <script>
        // Global variables to store generated scripts
        let batScript = "";
        let shScript = "";
        let currentUserInput = "";

        async function resolveIssue() {
            const inputField = document.getElementById("userInput");
            currentUserInput = inputField.value.trim();
            
            if (!currentUserInput) return;

            // Update UI State
            document.getElementById("submitBtn").disabled = true;
            document.getElementById("loading").style.display = "block";
            document.getElementById("responseSection").style.display = "none";
            document.getElementById("feedbackSection").style.display = "none";
            document.getElementById("batDownloadSection").style.display = "none";
            document.getElementById("shDownloadSection").style.display = "none";
            document.getElementById("feedbackResult").innerHTML = "";

            try {
                // Send request to your FastAPI backend
                const response = await fetch('/chat', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ message: currentUserInput })
                });
                
                const data = await response.json();
                let finalResponse = data.reply;

                // --- SCRIPT EXTRACTION (Regex) ---
                const batRegex = /<SCRIPT_BAT>([\s\S]*?)<\/SCRIPT_BAT>/;
                const shRegex = /<SCRIPT_SH>([\s\S]*?)<\/SCRIPT_SH>/;

                const batMatch = finalResponse.match(batRegex);
                if (batMatch) {
                    batScript = batMatch[1].trim();
                    document.getElementById("batDownloadSection").style.display = "block";
                    document.getElementById("batBtn").onclick = () => downloadFile(batScript, "supportx_autofix.bat");
                }

                const shMatch = finalResponse.match(shRegex);
                if (shMatch) {
                    shScript = shMatch[1].trim();
                    document.getElementById("shDownloadSection").style.display = "block";
                    document.getElementById("shBtn").onclick = () => downloadFile(shScript, "supportx_autofix.sh");
                }

                // Clean the text for display (remove the tags)
                let displayText = finalResponse.replace(batRegex, '').replace(shRegex, '').trim();
                
                // Show response
                document.getElementById("aiResponseText").innerText = displayText;
                document.getElementById("responseSection").style.display = "block";
                document.getElementById("feedbackSection").style.display = "block";

            } catch (error) {
                alert("Error connecting to MTOR backend. Is FastAPI running?");
            } finally {
                document.getElementById("submitBtn").disabled = false;
                document.getElementById("loading").style.display = "none";
            }
        }

        async function handleFeedback(isResolved) {
            const feedbackContainer = document.getElementById("feedbackResult");
            
            if (isResolved) {
                feedbackContainer.innerHTML = "<div class='success-box'>πŸŽ‰ Great! We're glad your issue is resolved. Thank you!</div>";
                document.querySelector('.feedback-buttons').style.display = "none";
            } else {
                // Generate Ticket ID
                const ticketId = "TKT-" + Math.random().toString(36).substring(2, 8).toUpperCase();
                feedbackContainer.innerHTML = `
                    <div class='warning-box'>⚠️ We're escalating this issue to IT support.</div>
                    <div class='ticket-id'>πŸ“„ Ticket Created: ${ticketId}</div>
                    <div id='escalationLoading'><em>Notifying IT Agent... ⏳</em></div>
                `;
                document.querySelector('.feedback-buttons').style.display = "none";

                // Call backend to trigger Notification Agent
                try {
                    const response = await fetch('/escalate', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({ issue: currentUserInput, ticket_id: ticketId })
                    });
                    const data = await response.json();
                    
                    document.getElementById('escalationLoading').style.display = "none";
                    feedbackContainer.innerHTML += `<div class='info-box'>πŸ“¨ **Notification Agent Response:**<br><br>${data.reply}</div>`;
                } catch (error) {
                    document.getElementById('escalationLoading').innerHTML = "Failed to notify agent.";
                }
            }
        }

        // Helper function to trigger browser file download
        function downloadFile(content, filename) {
            const blob = new Blob([content], { type: 'text/plain' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = filename;
            a.click();
            URL.revokeObjectURL(url);
        }
    </script>
</body>
</html>