let selectedProfileId = localStorage.getItem('selectedProfileId') || 'default'; let editingProfile = null; // Profile functions async function loadProfiles() { try { const response = await apiRequest('/profiles'); const profiles = Object.values(response); // Actualizar el selector manteniendo el valor seleccionado const select = document.getElementById('profileSelect'); select.innerHTML = profiles.map(profile => ` `).join(''); // Establecer el valor seleccionado después de actualizar las opciones if (response[selectedProfileId]) { select.value = selectedProfileId; await selectProfile(selectedProfileId); } else { selectedProfileId = 'default'; select.value = 'default'; await selectProfile('default'); } // Asegurarse de que el evento change no sobrescriba la selección select.addEventListener('change', onProfileChange, { once: true }); } catch (error) { showError('Error al cargar los perfiles'); } } async function selectProfile(profileId) { try { currentProfile = await apiRequest(`/profiles/${profileId}`); updateWorkDirDisplay(); } catch (error) { showError('Failed to load profile'); } } async function changeProfile() { const select = document.getElementById('profileSelect'); await selectProfile(select.value); } async function selectWorkDir() { try { const response = await apiRequest('/select-directory'); if (response.path) { await apiRequest(`/profiles/${currentProfile.id}`, { method: 'PUT', body: JSON.stringify({ ...currentProfile, work_dir: response.path }) }); await selectProfile(currentProfile.id); showSuccess('Work directory updated successfully'); } } catch (error) { showError('Failed to update work directory'); } } // 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'), work_dir: formData.get('work_dir'), 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'), work_dir: formData.get('work_dir'), 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); } }