Spaces:
Running
Running
File size: 8,799 Bytes
afe04f9 |
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
class CustomChatInterface extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
:host {
display: flex;
flex-direction: column;
height: 100%;
position: relative;
}
.chat-history {
flex: 1;
overflow-y: auto;
padding: 1.5rem;
display: flex;
flex-direction: column;
gap: 1.5rem;
}
.message {
display: flex;
gap: 1rem;
max-width: 85%;
animation: fadeIn 0.3s ease-out;
}
.message.user {
align-self: flex-end;
flex-direction: row-reverse;
}
.message.ai {
align-self: flex-start;
}
.avatar {
width: 40px;
height: 40px;
border-radius: 0.5rem;
flex-shrink: 0;
}
.msg-bubble {
padding: 1rem;
border-radius: 1rem;
font-size: 0.95rem;
line-height: 1.5;
position: relative;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
.user .msg-bubble {
background: #6366f1;
color: white;
border-bottom-right-radius: 0.25rem;
}
.ai .msg-bubble {
background: #1e293b;
color: #e2e8f0;
border: 1px solid #334155;
border-bottom-left-radius: 0.25rem;
}
.input-area {
padding: 1.5rem;
background: #0f172a;
border-top: 1px solid #334155;
display: flex;
gap: 1rem;
align-items: flex-end;
}
textarea {
flex: 1;
background: #1e293b;
border: 1px solid #334155;
color: white;
padding: 0.75rem;
border-radius: 0.5rem;
resize: none;
height: 50px;
min-height: 50px;
max-height: 150px;
font-family: inherit;
}
textarea:focus {
outline: none;
border-color: #6366f1;
}
.send-btn {
background: #6366f1;
color: white;
border: none;
width: 50px;
height: 50px;
border-radius: 0.5rem;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background 0.2s;
}
.send-btn:hover {
background: #4f46e5;
}
.send-btn:disabled {
background: #334155;
cursor: not-allowed;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.typing-indicator span {
display: inline-block;
width: 6px;
height: 6px;
background-color: #94a3b8;
border-radius: 50%;
animation: typing 0.6s infinite;
margin: 0 2px;
}
@keyframes typing {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-3px); }
}
</style>
<div class="chat-history" id="chat-history">
<!-- Messages will be injected here -->
<div class="message ai">
<img src="http://static.photos/technology/200x200/5" class="avatar" alt="AI">
<div class="msg-bubble">
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?
</div>
</div>
</div>
<div class="input-area">
<textarea id="msg-input" placeholder="Type a message... (Shift+Enter for new line)"></textarea>
<button id="send-btn" class="send-btn">
<i data-feather="send"></i>
</button>
</div>
`;
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, '<br>')
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\*(.*?)\*/g, '<em>$1</em>');
msgDiv.innerHTML = `
<img src="${avatarUrl}" class="avatar" alt="${role}">
<div class="msg-bubble message-content">${formattedText}</div>
`;
chatHistory.appendChild(msgDiv);
chatHistory.scrollTop = chatHistory.scrollHeight;
};
const showTyping = () => {
const typingDiv = document.createElement('div');
typingDiv.className = 'message ai typing-msg';
typingDiv.innerHTML = `
<img src="http://static.photos/technology/200x200/5" class="avatar" alt="AI">
<div class="msg-bubble typing-indicator">
<span></span><span></span><span></span>
</div>
`;
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); |