/** * AI Models Page - Hugging Face Integration * Fixed version with proper error handling */ import { APIHelper } from '../../shared/js/utils/api-helper.js'; import { modelsClient } from '../../shared/js/core/models-client.js'; import { api } from '../../shared/js/core/api-client.js'; import logger from '../../shared/js/utils/logger.js'; class ModelsPage { constructor() { this.models = []; this.refreshInterval = null; } async init() { try { console.log('[Models] Initializing...'); this.bindEvents(); await this.loadModels(); this.refreshInterval = setInterval(() => this.loadModels(), 60000); this.showToast('Models page ready', 'success'); } catch (error) { console.error('[Models] Init error:', error); this.showToast('Failed to load models', 'error'); } } bindEvents() { // Refresh button const refreshBtn = document.getElementById('refresh-btn'); if (refreshBtn) { refreshBtn.addEventListener('click', () => { this.loadModels(); }); } // Tab switching document.querySelectorAll('.tab-btn').forEach(btn => { btn.addEventListener('click', (e) => { const tabId = e.currentTarget.dataset.tab; this.switchTab(tabId); }); }); // Test model button const runTestBtn = document.getElementById('run-test-btn'); if (runTestBtn) { runTestBtn.addEventListener('click', () => { this.runTest(); }); } // Clear test button const clearTestBtn = document.getElementById('clear-test-btn'); if (clearTestBtn) { clearTestBtn.addEventListener('click', () => { this.clearTest(); }); } // Example buttons document.querySelectorAll('.example-btn').forEach(btn => { btn.addEventListener('click', (e) => { const text = e.currentTarget.dataset.text; const testInput = document.getElementById('test-input'); if (testInput) { testInput.value = text; } }); }); // Re-initialize all button const reinitBtn = document.getElementById('reinit-all-btn'); if (reinitBtn) { reinitBtn.addEventListener('click', () => { this.reinitializeAll(); }); } } switchTab(tabId) { // Remove active class from all tabs and contents document.querySelectorAll('.tab-btn').forEach(btn => { btn.classList.remove('active'); }); document.querySelectorAll('.tab-content').forEach(content => { content.classList.remove('active'); }); // Add active class to selected tab and content const selectedBtn = document.querySelector(`[data-tab="${tabId}"]`); const selectedContent = document.getElementById(`tab-${tabId}`); if (selectedBtn) { selectedBtn.classList.add('active'); } if (selectedContent) { selectedContent.classList.add('active'); } console.log(`[Models] Switched to tab: ${tabId}`); } async loadModels() { const container = document.getElementById('models-grid') || document.getElementById('models-container') || document.querySelector('.models-list'); // Show loading state if (container) { container.innerHTML = `
Loading AI models...
Models will be loaded on demand when needed for AI features.
${model.category}