// Global State Management class AppState { constructor() { this.currentProject = null; this.selectedModel = null; this.conversationHistory = []; this.developmentProgress = 0; this.isCodingActive = false; this.memoryStatus = { scratchpad: 0, mainplan: 0 }; this.availableModels = []; } updateProgress(progress) { this.developmentProgress = Math.min(100, Math.max(0, progress)); this.updateMemoryStatus(); this.emitStateChange(); } updateMemoryStatus() { // Calculate memory usage based on progress this.memoryStatus.scratchpad = Math.min(this.developmentProgress * 1.5, 100); this.memoryStatus.mainplan = Math.min(this.developmentProgress * 0.8, 100); } addConversation(role, content) { this.conversationHistory.push({ role, content, timestamp: new Date(), model: this.selectedModel }); // Save to raw conversations this.saveRawConversation(role, content); this.emitStateChange(); } saveRawConversation(role, content) { // This would be implemented in the backend console.log('Saving raw conversation:', { role, content }); } emitStateChange() { // Dispatch custom event for state changes const event = new CustomEvent('appStateChange', { detail: { progress: this.developmentProgress, memory: this.memoryStatus } }); document.dispatchEvent(event); } } // Initialize global state const appState = new AppState(); // API Client class APIClient { constructor() { this.baseURL = window.location.origin; } async request(endpoint, options = {}) { try { const response = await fetch(`${this.baseURL}${endpoint}`, { headers: { 'Content-Type': 'application/json', ...options.headers }, ...options }); if (!response.ok) { throw new Error(`API Error: ${response.statusText}`); } return await response.json(); } catch (error) { console.error('API Request failed:', error); throw error; } } async getModels() { return await this.request('/api/models'); } async startCoding(projectDescription, modelId) { return await this.request('/api/start-coding', { method: 'POST', body: JSON.stringify({ project_description: projectDescription, model_id: modelId }) }); } async fixError() { return await this.request('/api/fix-error', { method: 'POST' }); } async getMemoryStatus() { return await this.request('/api/memory-status'); } async loadScratchpad() { return await this.request('/api/scratchpad'); } async loadMainPlan() { return await this.request('/api/main-plan'); } } // Initialize API Client const apiClient = new APIClient(); // Update API base URL to use Flask backend apiClient.baseURL = 'http://localhost:5000'; // UI Controller class UIController { constructor() { this.initializeEventListeners(); this.setupStateListeners(); } initializeEventListeners() { // Model selection document.getElementById('modelSelect').addEventListener('change', (e) => { const modelId = e.target.value; const model = appState.availableModels.find(m => m.id === modelId); if (model) { appState.selectedModel = model; this.updateModelInfo(model); } }); // Filter buttons document.getElementById('filterFree').addEventListener('click', () => { this.filterModels('free'); }); document.getElementById('filterPaid').addEventListener('click', () => { this.filterModels('paid'); }); // Action buttons document.getElementById('startCoding').addEventListener('click', () => { this.handleStartCoding(); }); document.getElementById('fixError').addEventListener('click', () => { this.handleFixError(); }); // Tool buttons document.getElementById('loadScratchpad').addEventListener('click', () => { this.loadScratchpad(); }); document.getElementById('loadMainPlan').addEventListener('click', () => { this.loadMainPlan(); }); document.getElementById('clearMemory').addEventListener('click', () => { this.clearMemory(); }); document.getElementById('exportProject').addEventListener('click', () => { this.exportProject(); }); // Theme toggle document.getElementById('themeToggle').addEventListener('click', () => { this.toggleTheme(); }); // Clear chat document.getElementById('clearChat').addEventListener('click', () => { this.clearChat(); }); } setupStateListeners() { document.addEventListener('appStateChange', (e) => { this.updateProgressUI(e.detail.progress); this.updateMemoryUI(e.detail.memory); }); } async filterModels(filterType) { try { const models = await apiClient.getModels(); const filteredModels = models.filter(model => filterType === 'free' ? model.pricing === 'free' : model.pricing === 'paid' ); this.updateModelSelect(filteredModels); appState.availableModels = models; } catch (error) { this.showError('Failed to load models'); } } updateModelSelect(models) { const select = document.getElementById('modelSelect'); select.innerHTML = ''; models.forEach(model => { const option = document.createElement('option'); option.value = model.id; option.textContent = `${model.name} (${model.pricing})`; select.appendChild(option); }); if (models.length > 0) { select.value = models[0].id; appState.selectedModel = models[0]; this.updateModelInfo(models[0]); } } updateModelInfo(model) { const modelInfo = document.getElementById('modelInfo'); modelInfo.textContent = `${model.context_length} context | ${model.pricing}`; } updateProgressUI(progress) { const progressBar = document.getElementById('progressBar'); const progressStatus = document.getElementById('progressStatus'); progressBar.style.width = `${progress}%`; progressStatus.textContent = `${Math.round(progress)}%`; } updateMemoryUI(memory) { document.getElementById('scratchpadProgress').style.width = `${memory.scratchpad}%`; document.getElementById('scratchpadStatus').textContent =