class CustomChatInterface extends HTMLElement { connectedCallback() { this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = `
AI
Hello! I am Synapse, your AI coding assistant. I can help you search the web, analyze documents, or generate code. What's on your mind today?
`; const chatHistory = this.shadowRoot.getElementById('chat-history'); const msgInput = this.shadowRoot.getElementById('msg-input'); const sendBtn = this.shadowRoot.getElementById('send-btn'); let isProcessing = false; const addMessage = (role, text) => { const msgDiv = document.createElement('div'); msgDiv.className = `message ${role}`; const avatarUrl = role === 'user' ? 'http://static.photos/people/200x200/12' : 'http://static.photos/technology/200x200/5'; // Simple markdown-like parsing for bold/newlines const formattedText = text .replace(/\n/g, '
') .replace(/\*\*(.*?)\*\*/g, '$1') .replace(/\*(.*?)\*/g, '$1'); msgDiv.innerHTML = ` ${role}
${formattedText}
`; chatHistory.appendChild(msgDiv); chatHistory.scrollTop = chatHistory.scrollHeight; }; const showTyping = () => { const typingDiv = document.createElement('div'); typingDiv.className = 'message ai typing-msg'; typingDiv.innerHTML = ` AI
`; chatHistory.appendChild(typingDiv); chatHistory.scrollTop = chatHistory.scrollHeight; return typingDiv; }; const handleSend = async () => { const text = msgInput.value.trim(); if (!text || isProcessing) return; addMessage('user', text); msgInput.value = ''; msgInput.style.height = '50px'; // Reset height isProcessing = true; sendBtn.disabled = true; // Save to Memory if(window.MemoryDB) window.MemoryDB.save('user', text); // Show typing indicator const typingEl = showTyping(); // Get Response try { const response = await window.Agent.respond(text); typingEl.remove(); addMessage('ai', response); if(window.MemoryDB) window.MemoryDB.save('assistant', response); } catch (error) { typingEl.remove(); addMessage('ai', "Sorry, I encountered an error connecting to the neural net."); } isProcessing = false; sendBtn.disabled = false; msgInput.focus(); }; sendBtn.addEventListener('click', handleSend); msgInput.addEventListener('keypress', (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }); // Auto-resize textarea msgInput.addEventListener('input', function() { this.style.height = 'auto'; this.style.height = (this.scrollHeight) + 'px'; }); // Listen for clear memory event window.appEvents.addEventListener('memory-cleared', () => { chatHistory.innerHTML = ''; addMessage('ai', 'Session memory cleared. Ready for a new topic.'); }); feather.replace(); } } customElements.define('custom-chat-interface', CustomChatInterface);