let selectedProfileId = localStorage.getItem('selectedProfileId') || 'default'; let editingProfile = null; // Profile functions async function loadProfiles() { try { const response = await apiRequest('/profiles'); if (!response || !Object.keys(response).length) { throw new Error('No profiles available'); } const profiles = Object.values(response); const select = document.getElementById('profileSelect'); // Actualizar el selector select.innerHTML = profiles.map(profile => ` `).join(''); // Intentar seleccionar el perfil guardado o el predeterminado const savedProfile = profiles.find(p => p.id === selectedProfileId); if (savedProfile) { await selectProfile(savedProfile.id); } else { // Si no se encuentra el perfil guardado, usar el primero disponible selectedProfileId = profiles[0].id; select.value = selectedProfileId; await selectProfile(selectedProfileId); } localStorage.setItem('selectedProfileId', selectedProfileId); } catch (error) { console.error('Error loading profiles:', error); showError('Error loading profiles. Using default profile.'); await loadDefaultProfile(); } } async function loadDefaultProfile() { try { currentProfile = await apiRequest('/profiles/default'); selectedProfileId = 'default'; localStorage.setItem('selectedProfileId', 'default'); const select = document.getElementById('profileSelect'); select.innerHTML = ``; select.value = 'default'; // Actualizar la visualización del perfil updateProfileDisplay(); } catch (error) { console.error('Error loading default profile:', error); showError('Failed to load default profile'); } } async function selectProfile(profileId) { try { const response = await apiRequest(`/profiles/${profileId}`); if (!response || response.error) { throw new Error(response?.error || 'Profile not found'); } currentProfile = response; selectedProfileId = profileId; localStorage.setItem('selectedProfileId', profileId); // Actualizar la visualización del perfil updateProfileDisplay(); } catch (error) { console.error('Failed to load profile:', error); showError(`Failed to load profile: ${error.message}`); // Intentar cargar el perfil por defecto si falla if (profileId !== 'default') { await loadDefaultProfile(); } } } function updateProfileDisplay() { const profileConfig = document.getElementById('profileConfig'); if (!profileConfig || !currentProfile) return; profileConfig.innerHTML = `
${currentProfile.id}
${currentProfile.name}
${currentProfile.llm_settings?.model || 'Not set'}
${currentProfile.llm_settings?.temperature || 'Not set'}
${currentProfile.llm_settings?.api_key ? '********' : 'Not set'}
`; } async function changeProfile() { const select = document.getElementById('profileSelect'); await selectProfile(select.value); } // Eliminar la función updateWorkDirDisplay y selectWorkDir // Profile editor modal function showProfileEditor(profile = null) { editingProfile = profile; const modal = document.createElement('div'); modal.className = 'modal active'; const editableInputClass = "mt-1 block w-full rounded-md border-2 border-gray-300 bg-green-50 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500"; const readonlyInputClass = "mt-1 block w-full rounded-md border-2 border-gray-200 bg-gray-100 px-3 py-2 shadow-sm"; modal.innerHTML = ` `; document.body.appendChild(modal); } async function saveProfile(event) { event.preventDefault(); const form = event.target; const formData = new FormData(form); const profileData = { id: formData.get('id'), name: formData.get('name'), llm_settings: { model: formData.get('llm_model'), api_key: formData.get('api_key'), temperature: parseFloat(formData.get('temperature')) } }; try { if (editingProfile) { await apiRequest(`/profiles/${editingProfile.id}`, { method: 'PUT', body: JSON.stringify(profileData) }); } else { await apiRequest('/profiles', { method: 'POST', body: JSON.stringify(profileData) }); } await loadProfiles(); closeModal(event.target); showSuccess(`Perfil ${editingProfile ? 'actualizado' : 'creado'} correctamente`); } catch (error) { showError(`Error al ${editingProfile ? 'actualizar' : 'crear'} el perfil`); } } // static/js/profile.js async function editProfile() { if (!currentProfile) { showError('No profile selected'); return; } const content = `
`; const modal = createModal('Edit Profile', content, true); modal.querySelector('[onclick="saveModal(this)"]').onclick = async () => { await saveProfile(modal); }; } async function saveProfile(modal) { const form = modal.querySelector('#profileForm'); const formData = new FormData(form); const profileData = { id: formData.get('id'), name: formData.get('name'), llm_settings: { model: formData.get('llm_model'), api_key: formData.get('api_key'), temperature: parseFloat(formData.get('temperature')) } }; try { if (editingProfile) { await apiRequest(`/profiles/${editingProfile.id}`, { method: 'PUT', body: JSON.stringify(profileData) }); } else { await apiRequest('/profiles', { method: 'POST', body: JSON.stringify(profileData) }); } await loadProfiles(); closeModal(modal); showSuccess(`Perfil ${editingProfile ? 'actualizado' : 'creado'} correctamente`); } catch (error) { showError(`Error al ${editingProfile ? 'actualizar' : 'crear'} el perfil`); } } function newProfile() { const editableInputClass = "mt-1 block w-full rounded-md border-2 border-gray-300 bg-green-50 px-3 py-2 shadow-sm focus:border-indigo-500 focus:ring-indigo-500"; const readonlyInputClass = "mt-1 block w-full rounded-md border-2 border-gray-200 bg-gray-100 px-3 py-2 shadow-sm"; const content = `
`; const modal = createModal('New Profile', content, true); editingProfile = null; modal.querySelector('[onclick="saveModal(this)"]').onclick = async () => { await saveProfile(modal); }; } async function onProfileChange(event) { const newProfileId = event.target.value; if (newProfileId !== selectedProfileId) { selectedProfileId = newProfileId; localStorage.setItem('selectedProfileId', selectedProfileId); await selectProfile(selectedProfileId); } }