File size: 4,720 Bytes
6172dec
d01ed8e
 
 
 
6172dec
 
 
 
d01ed8e
6172dec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d01ed8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6172dec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d01ed8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8c53c5
6172dec
d8c53c5
 
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

// OpenAI API key (replace with your actual key)
const OPENAI_API_KEY = 'your-api-key-here';
const OPENAI_API_URL = 'https://api.openai.com/v1/chat/completions';

// Speech recognition setup
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
let recognition = null;
let isRecognizing = false;
let conversationHistory = [];

if (SpeechRecognition) {
    recognition = new SpeechRecognition();
    recognition.continuous = false;
    recognition.interimResults = false;
    recognition.lang = 'en-US';

    recognition.onresult = (event) => {
        const transcript = event.results[0][0].transcript;
        document.getElementById('user-input').value = transcript;
        document.getElementById('voice-feedback').classList.add('hidden');
        document.getElementById('recording-indicator').classList.add('hidden');
        document.getElementById('send-btn').disabled = false;
    };

    recognition.onerror = (event) => {
        console.error('Speech recognition error', event.error);
        document.getElementById('voice-feedback').textContent = `Error: ${event.error}`;
        setTimeout(() => {
            document.getElementById('voice-feedback').classList.add('hidden');
        }, 3000);
        document.getElementById('recording-indicator').classList.add('hidden');
    };

    recognition.onend = () => {
        isRecognizing = false;
        document.getElementById('voice-btn').classList.remove('text-red-400');
    };
}

// Process user message with OpenAI
async function processMessage(message) {
    const responseWindow = document.getElementById('response-window');
    const loadingMessage = document.createElement('p');
    loadingMessage.className = 'text-blue-300 mb-4';
    loadingMessage.innerHTML = '> Processing your thought patterns...';
    responseWindow.appendChild(loadingMessage);
    responseWindow.scrollTop = responseWindow.scrollHeight;

    try {
        conversationHistory.push({ role: 'user', content: message });

        const response = await axios.post(OPENAI_API_URL, {
            model: 'gpt-3.5-turbo',
            messages: [
                { role: 'system', content: 'You are Brian the Mind Mirror clarity assistant, a helpful AI companion designed to help organize thoughts and provide clarity. Respond in a thoughtful but concise manner.' },
                ...conversationHistory
            ],
            temperature: 0.7,
            max_tokens: 150
        }, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${OPENAI_API_KEY}`
            }
        });

        const aiReply = response.data.choices[0].message.content;
        conversationHistory.push({ role: 'assistant', content: aiReply });

        const replyElement = document.createElement('p');
        replyElement.innerHTML = aiReply.split('\n').join('<br>');
        responseWindow.replaceChild(replyElement, loadingMessage);
    } catch (error) {
        console.error('OpenAI API error:', error);
        loadingMessage.textContent = '> System error occurred, please try again later';
    }
}

document.addEventListener('DOMContentLoaded', function() {
    const voiceBtn = document.getElementById('voice-btn');
    const sendBtn = document.getElementById('send-btn');
    const userInput = document.getElementById('user-input');

    // Voice input handler
    voiceBtn.addEventListener('click', () => {
        if (!recognition) {
            alert('Speech recognition not supported in your browser');
            return;
        }

        if (!isRecognizing) {
            recognition.start();
            isRecognizing = true;
            document.getElementById('voice-feedback').classList.remove('hidden');
            document.getElementById('recording-indicator').classList.remove('hidden');
            document.getElementById('voice-btn').classList.add('text-red-400');
        } else {
            recognition.stop();
        }
    });

    // Text input handler
    userInput.addEventListener('input', () => {
        sendBtn.disabled = userInput.value.trim() === '';
    });

    // Send message handler
    sendBtn.addEventListener('click', () => {
        const message = userInput.value.trim();
        if (message) {
            processMessage(message);
            userInput.value = '';
            sendBtn.disabled = true;
        }
    });

    // Handle Enter key
    userInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter' && !e.shiftKey) {
            e.preventDefault();
            sendBtn.click();
        }
    });
});
class GalaxyBackground {
constructor(container) {
        this.container = container;
        this.scene = null;