class AiChat extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
Smart coding help and automation
`;
this.initializeChat();
}
initializeChat() {
this.currentContext = 'general';
this.chatContainer = this.shadowRoot.getElementById('chatContainer');
this.messageInput = this.shadowRoot.getElementById('messageInput');
this.sendButton = this.shadowRoot.getElementById('sendButton');
// Add event listeners
this.sendButton.addEventListener('click', () => this.sendMessage());
this.messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
this.sendMessage();
}
});
// Context buttons
const contextButtons = this.shadowRoot.querySelectorAll('.context-button');
contextButtons.forEach(button => {
button.addEventListener('click', () => {
this.setContext(button.dataset.context);
});
});
// Command buttons
const commandButtons = this.shadowRoot.querySelectorAll('.command-button');
commandButtons.forEach(button => {
button.addEventListener('click', () => {
this.insertCommand(button.dataset.command);
});
});
// Initial welcome message
this.addMessage('ai', `👋 Welcome to DevAssistant!
I can help with:
- API Integrations (REST, GraphQL, WebSockets)
- Debugging (Chrome DevTools, Wireshark)
- Security (Burp Suite, OWASP ZAP)
- Automation (Selenium, Puppeteer)
Try commands like:
• /explain [concept] - Get detailed explanations
• /code [language] - Generate code samples
• /api [service] - Get API integration help
• /fix [error] - Debug and fix issues
Or click the context buttons above!`);
}
addMessage(sender, text) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message');
if (sender === 'user') {
messageDiv.classList.add('user-message');
messageDiv.innerHTML = `${this.escapeHtml(text)}
`;
} else {
messageDiv.classList.add('ai-message');
messageDiv.innerHTML = `${this.formatMessage(text)}
`;
}
this.chatContainer.appendChild(messageDiv);
this.chatContainer.scrollTop = this.chatContainer.scrollHeight;
}
formatMessage(text) {
// Simple formatting for code blocks
return text.replace(/