import apiClient from './apiClient.js'; class AIAdvisorView { constructor(section) { this.section = section; this.queryForm = section?.querySelector('[data-query-form]'); this.sentimentForm = section?.querySelector('[data-sentiment-form]'); this.queryOutput = section?.querySelector('[data-query-output]'); this.sentimentOutput = section?.querySelector('[data-sentiment-output]'); } init() { if (this.queryForm) { this.queryForm.addEventListener('submit', async (event) => { event.preventDefault(); const formData = new FormData(this.queryForm); await this.handleQuery(formData); }); } if (this.sentimentForm) { this.sentimentForm.addEventListener('submit', async (event) => { event.preventDefault(); const formData = new FormData(this.sentimentForm); await this.handleSentiment(formData); }); } } async handleQuery(formData) { const query = formData.get('query') || ''; if (!query.trim()) return; if (this.queryOutput) { this.queryOutput.innerHTML = '
Processing query...
'; } const result = await apiClient.runQuery({ query }); if (!result.ok) { if (this.queryOutput) { this.queryOutput.innerHTML = ``; } return; } // Backend returns {success: true, type: ..., message: ..., data: ...} const data = result.data || {}; if (this.queryOutput) { this.queryOutput.innerHTML = `Type: ${data.type || 'general'}
${data.message || 'Query processed'}
${data.data ? `${JSON.stringify(data.data, null, 2)}` : ''}
Analyzing sentiment...
'; } const result = await apiClient.analyzeSentiment({ text }); if (!result.ok) { if (this.sentimentOutput) { this.sentimentOutput.innerHTML = ``; } return; } // Backend returns {success: true, sentiment: ..., confidence: ..., details: ...} const data = result.data || {}; const sentiment = data.sentiment || 'neutral'; const confidence = data.confidence || 0; if (this.sentimentOutput) { this.sentimentOutput.innerHTML = `Label: ${sentiment}
Confidence: ${(confidence * 100).toFixed(1)}%
${data.details ? `${JSON.stringify(data.details, null, 2)}` : ''}