diff --git a/my-blog/static/js/ask-ai.js b/my-blog/static/js/ask-ai.js
index b269d10..7afc9f7 100644
--- a/my-blog/static/js/ask-ai.js
+++ b/my-blog/static/js/ask-ai.js
@@ -1,360 +1,286 @@
/**
- * Ask AI functionality - Pure JavaScript, no jQuery dependency
+ * Ask AI functionality - Based on original working implementation
*/
-class AskAI {
- constructor() {
- this.isReady = false;
- this.aiProfile = null;
- this.init();
+
+// Global variables for AI functionality
+let aiProfileData = null;
+
+// Original functions from working implementation
+function toggleAskAI() {
+ const panel = document.getElementById('askAiPanel');
+ const isVisible = panel.style.display !== 'none';
+ panel.style.display = isVisible ? 'none' : 'block';
+
+ if (!isVisible) {
+ checkAuthenticationStatus();
}
+}
- init() {
- this.setupEventListeners();
- this.checkAuthOnLoad();
- }
-
- setupEventListeners() {
- // Listen for AI ready signal
- window.addEventListener('aiChatReady', () => {
- this.isReady = true;
- console.log('AI Chat is ready');
- });
-
- // Listen for AI profile updates
- window.addEventListener('aiProfileLoaded', (event) => {
- this.aiProfile = event.detail;
- console.log('AI profile loaded:', this.aiProfile);
- this.updateButton();
- });
-
- // Listen for AI responses
- window.addEventListener('aiResponseReceived', (event) => {
- this.handleAIResponse(event.detail);
- });
-
- // Keyboard shortcuts
- document.addEventListener('keydown', (e) => {
- if (e.key === 'Escape') {
- this.hide();
- }
- if (e.key === 'Enter' && e.target.id === 'aiQuestion' && !e.shiftKey) {
- e.preventDefault();
- this.ask();
- }
- });
-
- // Monitor authentication changes
- this.observeAuth();
- }
-
- toggle() {
- const panel = document.getElementById('askAiPanel');
- const isVisible = panel.style.display !== 'none';
+function checkAuthenticationStatus() {
+ const userSections = document.querySelectorAll('.user-section');
+ const isAuthenticated = userSections.length > 0;
+
+ if (isAuthenticated) {
+ // User is authenticated - show Ask AI UI
+ document.getElementById('authCheck').style.display = 'none';
+ document.getElementById('chatForm').style.display = 'block';
+ document.getElementById('chatHistory').style.display = 'block';
- if (isVisible) {
- this.hide();
- } else {
- this.show();
- }
- }
-
- show() {
- const panel = document.getElementById('askAiPanel');
- panel.style.display = 'block';
- this.checkAuth();
- }
-
- hide() {
- const panel = document.getElementById('askAiPanel');
- panel.style.display = 'none';
- }
-
- checkAuth() {
- const userSections = document.querySelectorAll('.user-section');
- const isAuthenticated = userSections.length > 0;
-
- const authCheck = document.getElementById('authCheck');
- const chatForm = document.getElementById('chatForm');
+ // Show initial greeting if chat history is empty
const chatHistory = document.getElementById('chatHistory');
-
- if (isAuthenticated) {
- authCheck.style.display = 'none';
- chatForm.style.display = 'block';
- chatHistory.style.display = 'block';
-
- if (chatHistory.children.length === 0) {
- this.showGreeting();
- }
-
- setTimeout(() => {
- document.getElementById('aiQuestion').focus();
- }, 50);
- } else {
- authCheck.style.display = 'block';
- chatForm.style.display = 'none';
- chatHistory.style.display = 'none';
+ if (chatHistory.children.length === 0) {
+ showInitialGreeting();
}
- }
-
- checkAuthOnLoad() {
+
+ // Focus on input
setTimeout(() => {
- this.checkAuth();
- }, 500);
+ document.getElementById('aiQuestion').focus();
+ }, 50);
+ } else {
+ // User not authenticated - show auth message
+ document.getElementById('authCheck').style.display = 'block';
+ document.getElementById('chatForm').style.display = 'none';
+ document.getElementById('chatHistory').style.display = 'none';
}
+}
- observeAuth() {
- const observer = new MutationObserver(() => {
- const userSections = document.querySelectorAll('.user-section');
- if (userSections.length > 0) {
- this.checkAuth();
- observer.disconnect();
- }
- });
+function askQuestion() {
+ const question = document.getElementById('aiQuestion').value;
+ if (!question.trim()) return;
+
+ const askButton = document.getElementById('askButton');
+ askButton.disabled = true;
+ askButton.textContent = 'Posting...';
+
+ try {
+ // Add user message to chat
+ addUserMessage(question);
- observer.observe(document.body, {
- childList: true,
- subtree: true
- });
+ // Clear input
+ document.getElementById('aiQuestion').value = '';
+
+ // Show loading
+ showLoadingMessage();
+
+ // Post question via OAuth app
+ window.dispatchEvent(new CustomEvent('postAIQuestion', {
+ detail: { question: question }
+ }));
+
+ } catch (error) {
+ console.error('Failed to ask question:', error);
+ showErrorMessage('Sorry, I encountered an error. Please try again.');
+ } finally {
+ askButton.disabled = false;
+ askButton.textContent = 'Ask';
}
+}
- updateButton() {
- const button = document.getElementById('askAiButton');
- if (this.aiProfile && this.aiProfile.displayName) {
- const textNode = button.childNodes[2];
- if (textNode) {
- textNode.textContent = this.aiProfile.displayName;
- }
+function addUserMessage(question) {
+ const chatHistory = document.getElementById('chatHistory');
+ const userSection = document.querySelector('.user-section');
+
+ let userAvatar = 'π€';
+ let userDisplay = 'You';
+ let userHandle = 'user';
+
+ if (userSection) {
+ const avatarImg = userSection.querySelector('.user-avatar');
+ const displayName = userSection.querySelector('.user-display-name');
+ const handle = userSection.querySelector('.user-handle');
+
+ if (avatarImg && avatarImg.src) {
+ userAvatar = ``;
+ }
+ if (displayName?.textContent) {
+ userDisplay = displayName.textContent;
+ }
+ if (handle?.textContent) {
+ userHandle = handle.textContent.replace('@', '');
}
}
-
- showGreeting() {
- if (!this.aiProfile) return;
-
- const chatHistory = document.getElementById('chatHistory');
- const greetingDiv = document.createElement('div');
- greetingDiv.className = 'chat-message ai-message comment-style initial-greeting';
-
- const avatarElement = this.aiProfile.avatar
- ? `
`
- : 'π€';
-
- greetingDiv.innerHTML = `
-