class ChatComponent extends HTMLElement { constructor() { super(); this.client = null; this.messages = [ { role: "assistant", content: "Hello! I'm your AI assistant. How can I help you today?" } ]; } connectedCallback() { this.attachShadow({ mode: 'open' }); this.render(); this.initializeOpenAI(); this.setupEventListeners(); } render() { this.shadowRoot.innerHTML = `
`; } initializeOpenAI() { // Note: In a real implementation, the API key should be handled securely on the server-side // This is just for demonstration purposes try { // Load Feather icons const script = document.createElement('script'); script.src = "https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"; script.onload = () => { if (this.shadowRoot) { window.feather.replace(); } }; this.shadowRoot.appendChild(script); } catch (error) { console.error('Error initializing OpenAI:', error); } } setupEventListeners() { const form = this.shadowRoot.getElementById('chatForm'); const input = this.shadowRoot.getElementById('messageInput'); form.addEventListener('submit', async (e) => { e.preventDefault(); const message = input.value.trim(); if (message) { this.sendMessage(message); input.value = ''; } }); // Load initial messages this.loadMessages(); } loadMessages() { const container = this.shadowRoot.getElementById('messagesContainer'); container.innerHTML = ''; this.messages.forEach(msg => { this.displayMessage(msg); }); container.scrollTop = container.scrollHeight; } displayMessage(message) { const container = this.shadowRoot.getElementById('messagesContainer'); const messageDiv = document.createElement('div'); messageDiv.className = `message message-${message.role}`; let icon = 'user'; let name = 'You'; if (message.role === 'assistant') { icon = 'cpu'; name = 'AI Assistant'; } messageDiv.innerHTML = `
${name}
${message.content}
`; container.appendChild(messageDiv); window.feather.replace(); container.scrollTop = container.scrollHeight; } showTypingIndicator() { const container = this.shadowRoot.getElementById('messagesContainer'); const typingDiv = document.createElement('div'); typingDiv.className = 'typing-indicator'; typingDiv.id = 'typingIndicator'; typingDiv.innerHTML = `
`; container.appendChild(typingDiv); container.scrollTop = container.scrollHeight; } hideTypingIndicator() { const indicator = this.shadowRoot.getElementById('typingIndicator'); if (indicator) { indicator.remove(); } } async sendMessage(content) { // Add user message to UI const userMessage = { role: 'user', content }; this.messages.push(userMessage); this.displayMessage(userMessage); // Show typing indicator this.showTypingIndicator(); try { // In a real implementation, this would call the actual API // For now, we'll simulate a response const response = await this.getAIResponse(content); // Add AI response to UI const aiMessage = { role: 'assistant', content: response }; this.messages.push(aiMessage); this.hideTypingIndicator(); this.displayMessage(aiMessage); } catch (error) { console.error('Error getting AI response:', error); this.hideTypingIndicator(); const errorMessage = { role: 'assistant', content: 'Sorry, I encountered an error. Please try again.' }; this.messages.push(errorMessage); this.displayMessage(errorMessage); } } async getAIResponse(prompt) { // Simulate API delay await new Promise(resolve => setTimeout(resolve, 1000 + Math.random() * 2000)); // Simple response logic for demonstration const responses = [ "I understand your question about \"" + prompt + "\". Based on my analysis, this is an important topic that requires careful consideration.", "Thanks for asking about \"" + prompt + "\". I've processed your request and here's what I found...", "Regarding \"" + prompt + "\", I can provide some insights. This is a complex matter with several factors to consider.", "I've analyzed your query about \"" + prompt + "\". Here's what I recommend based on current best practices.", "That's an interesting question about \"" + prompt + "\". Let me break this down for you..." ]; return responses[Math.floor(Math.random() * responses.length)] + " This is a simulated response. In a real implementation, this would connect to the Llama API."; } } customElements.define('chat-component', ChatComponent);