File size: 7,224 Bytes
3e001b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61e6d0f
 
3e001b4
61e6d0f
 
3e001b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// 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 =