// frontend/static/js/profile.js // Profile functions async function loadProfiles() { try { const profiles = await apiRequest('/profiles'); updateProfileSelector(profiles); // Select first profile if none selected if (!currentProfile) { const defaultProfile = profiles.find(p => p.id === 'default') || profiles[0]; if (defaultProfile) { await selectProfile(defaultProfile.id); } } } catch (error) { showError('Failed to load profiles'); } } function updateProfileSelector(profiles) { const select = document.getElementById('profileSelect'); select.innerHTML = profiles.map(profile => ` `).join(''); } 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 let editingProfile = null; function showProfileEditor(profile = null) { editingProfile = profile; const modal = document.createElement('div'); modal.className = 'modal active'; 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(`Profile ${editingProfile ? 'updated' : 'created'} successfully`); } catch (error) { showError(`Failed to ${editingProfile ? 'update' : 'create'} profile`); } } function editProfile() { if (!currentProfile) { showError('No profile selected'); return; } showProfileEditor(currentProfile); } function newProfile() { showProfileEditor(); }