-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
601 lines (507 loc) · 17.7 KB
/
main.js
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const remote = require('@electron/remote/main');
const { autoUpdater } = require('electron-updater');
const log = require('electron-log');
remote.initialize();
const path = require('path');
const fs = require('fs').promises;
const { encode } = require('gpt-tokenizer');
const Store = require('electron-store');
const store = new Store();
// Configure logging
log.transports.file.level = 'info';
autoUpdater.logger = log;
log.info('PromptLab starting...');
const DEFAULT_IGNORE_PATTERNS = [
// Common
'# Common',
'node_modules',
'dist',
'build',
'.next',
'.vite',
'.git',
'.env',
'.env.*',
'',
// Python
'# Python',
'__pycache__',
'venv',
'*.pyc',
// IDE
'# IDE',
'.idea',
'.vscode',
// System
'# System',
'.DS_Store',
'Thumbs.db',
];
// Near the top with other store initialization
const DEFAULT_WINDOW_BOUNDS = {
width: 1400,
height: 800,
x: undefined,
y: undefined
};
function createWindow() {
// Get stored window bounds
const windowBounds = store.get('windowBounds', DEFAULT_WINDOW_BOUNDS);
const win = new BrowserWindow({
...windowBounds,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true,
webSecurity: true,
allowRunningInsecureContent: false,
experimentalFeatures: false
},
backgroundColor: '#111827',
frame: false,
transparent: process.platform !== 'linux',
icon: path.join(__dirname, 'assets/icons/256x256.png')
});
// Save window position and size when it changes
['move', 'resize'].forEach(event => {
win.on(event, () => {
if (!win.isMaximized()) {
const bounds = win.getBounds();
store.set('windowBounds', bounds);
}
});
});
// Save maximized state
win.on('maximize', () => {
store.set('windowMaximized', true);
});
win.on('unmaximize', () => {
store.set('windowMaximized', false);
// Save current bounds after unmaximize
const bounds = win.getBounds();
store.set('windowBounds', bounds);
});
remote.enable(win.webContents);
win.loadFile('index.html');
// Restore maximized state if it was maximized when closed
if (store.get('windowMaximized', false)) {
win.maximize();
}
return win;
}
// Auto-updater events
autoUpdater.on('checking-for-update', () => {
log.info('Checking for update...');
});
autoUpdater.on('update-available', (info) => {
log.info('Update available:', info);
});
autoUpdater.on('update-not-available', (info) => {
log.info('Update not available:', info);
});
autoUpdater.on('error', (err) => {
log.error('Error in auto-updater:', err);
});
autoUpdater.on('download-progress', (progressObj) => {
let logMessage = `Download speed: ${progressObj.bytesPerSecond}`;
logMessage = `${logMessage} - Downloaded ${progressObj.percent}%`;
logMessage = `${logMessage} (${progressObj.transferred}/${progressObj.total})`;
log.info(logMessage);
});
autoUpdater.on('update-downloaded', (info) => {
log.info('Update downloaded:', info);
// Install on next restart
autoUpdater.quitAndInstall(false, true);
});
app.whenReady().then(() => {
createWindow();
autoUpdater.checkForUpdatesAndNotify();
});
// File system handlers
ipcMain.handle('select-folder', async () => {
const result = await dialog.showOpenDialog({
properties: ['openDirectory']
});
return result.filePaths;
});
function isFileIgnoredWithFilters(filePath, filtersList) {
if (!filtersList || filtersList.length === 0) return false;
return filtersList.some(pattern => {
// Skip empty patterns and comments
if (!pattern || pattern.startsWith('#')) return false;
try {
// Clean up the pattern
pattern = pattern.trim();
// Handle directory patterns (ending with /)
if (pattern.endsWith('/')) {
pattern = pattern.slice(0, -1);
}
// Convert glob pattern to regex
let regexPattern = pattern
// Escape special regex characters except * and ?
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
// Convert ** to special placeholder
.replace(/\*\*/g, '__GLOBSTAR__')
// Convert * to non-separator match
.replace(/\*/g, '[^/]*')
// Convert ? to single non-separator char
.replace(/\?/g, '[^/]')
// Convert globstar back
.replace(/__GLOBSTAR__/g, '.*');
// Make the pattern match anywhere in the path
if (!pattern.startsWith('/')) {
regexPattern = `(^|/|\\\\)${regexPattern}(/|$|\\\\)`;
}
const regex = new RegExp(regexPattern);
return regex.test(filePath);
} catch (error) {
return false;
}
});
}
async function isTextFile(filePath) {
try {
// Read the first 8KB of the file
const fd = await fs.open(filePath, 'r');
const buffer = Buffer.alloc(8192); // 8KB buffer
const { bytesRead } = await fd.read(buffer, 0, 8192, 0);
await fd.close();
// If file is empty, consider it text
if (bytesRead === 0) return true;
// Check for NULL bytes in the content
// Files with NULL bytes are likely binary
for (let i = 0; i < bytesRead; i++) {
if (buffer[i] === 0) return false;
}
// Try to decode as UTF-8
try {
buffer.slice(0, bytesRead).toString('utf8');
return true;
} catch (e) {
return false;
}
} catch (error) {
console.error(`Error checking if ${filePath} is text:`, error);
return false;
}
}
async function countTokens(filePath) {
try {
// First check if it's a text file
if (!await isTextFile(filePath)) {
return 0;
}
const content = await fs.readFile(filePath, 'utf-8');
return encode(content).length;
} catch (error) {
console.error(`Error counting tokens for ${filePath}:`, error);
return 0;
}
}
ipcMain.handle('read-directory', async (event, folderPath, filters = []) => {
// Use default patterns if no filters provided
const activeFilters = filters.length > 0
? filters
: DEFAULT_IGNORE_PATTERNS;
async function getAllFiles(dir) {
let files;
try {
files = await fs.readdir(dir, { withFileTypes: true });
} catch (error) {
if (error.code === 'EACCES' || error.code === 'ENOENT') {
return [];
}
throw error;
}
const paths = [];
for (const file of files) {
const filePath = path.join(dir, file.name);
const relativePath = path.relative(folderPath, filePath).replace(/\\/g, '/');
try {
if (file.isDirectory()) {
const subDirFiles = await getAllFiles(filePath);
paths.push(...subDirFiles);
} else {
// Check if file should be ignored
if (!isFileIgnoredWithFilters(relativePath, activeFilters)) {
try {
await fs.access(filePath, fs.constants.R_OK);
// Count tokens for each file that passes the filters
const tokens = await countTokens(filePath);
paths.push({
path: relativePath,
tokens,
isText: tokens > 0 // If tokens were counted, it's a text file
});
} catch (accessError) {
if (accessError.code === 'EACCES' || accessError.code === 'ENOENT') {
continue;
}
throw accessError;
}
}
}
} catch (error) {
if (error.code === 'EACCES' || error.code === 'ENOENT') {
continue;
}
throw error;
}
}
return paths;
}
try {
const files = await getAllFiles(folderPath);
return files;
} catch (error) {
throw error;
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
ipcMain.handle('load-global-settings', async () => {
return store.get('globalSettings', {
lastOpenedRepo: null
});
});
ipcMain.handle('save-global-settings', async (event, settings) => {
try {
// Ensure the path exists and is absolute
if (settings.lastOpenedRepo) {
const absolutePath = path.resolve(settings.lastOpenedRepo);
settings.lastOpenedRepo = absolutePath;
}
store.set('globalSettings', settings);
} catch (error) {
console.error('Error saving global settings:', error);
throw error;
}
});
ipcMain.handle('load-repo-data', async (event, repoPath) => {
const repoData = store.get(`repos.${repoPath}`, {});
// Initialize filters with defaults if they don't exist
// This ensures each repo starts with default filters but can be customized
if (!repoData.filters) {
repoData.filters = DEFAULT_IGNORE_PATTERNS;
store.set(`repos.${repoPath}`, repoData);
}
return repoData;
});
ipcMain.handle('save-repo-data', async (event, repoPath, data) => {
store.set(`repos.${repoPath}`, data);
});
ipcMain.handle('update-recent-repos', async (event, repoPath) => {
const recentRepos = store.get('recentRepos', []);
const updatedRepos = [
repoPath,
...recentRepos.filter(repo => repo !== repoPath)
].slice(0, 10); // Keep only last 10 repos
store.set('recentRepos', updatedRepos);
return updatedRepos;
});
// Add a new handler specifically for UI settings
ipcMain.handle('load-ui-settings', async () => {
const settings = store.get('uiSettings', {
sidebarWidth: 288, // Default width
instructionsHeight: 256 // Default height
});
return settings;
});
ipcMain.handle('save-ui-settings', async (event, settings) => {
// Only allow specific UI-related settings
const allowedKeys = ['sidebarWidth', 'instructionsHeight'];
const sanitizedSettings = {};
for (const key of allowedKeys) {
if (key in settings) {
sanitizedSettings[key] = settings[key];
}
}
// Get current settings
const currentSettings = store.get('uiSettings', {});
// Merge and save
const newSettings = {
...currentSettings,
...sanitizedSettings
};
store.set('uiSettings', newSettings);
});
ipcMain.handle('load-templates', async () => {
return store.get('templates', []);
});
ipcMain.handle('save-templates', async (event, templates) => {
store.set('templates', templates);
});
ipcMain.handle('read-file', async (event, filePath) => {
try {
const currentRepo = store.get('globalSettings', {}).lastOpenedRepo;
if (!currentRepo) {
throw new Error('No repository is currently open');
}
// Correctly join the repo path with the relative file path
const fullPath = path.join(currentRepo, filePath);
// Verify the file is within the repo directory (security check)
if (!fullPath.startsWith(currentRepo)) {
throw new Error('Invalid file path');
}
const content = await fs.readFile(fullPath, 'utf-8');
return content;
} catch (error) {
console.error(`Error reading file ${filePath}:`, error);
throw error;
}
});
// Modify the generateFileTree function to only include selected files
async function generateFileTree(rootPath, selectedFiles) {
const tree = {};
// Helper to add a path to the tree
const addToTree = (pathParts, isFile = false) => {
let current = tree;
pathParts.forEach((part, index) => {
if (!current[part]) {
current[part] = {
isFile: isFile && index === pathParts.length - 1,
children: {}
};
}
current = current[part].children;
});
};
// Add all selected files and their parent directories to the tree
selectedFiles.forEach(file => {
const pathParts = file.path.split('/');
// Add all parent directories
for (let i = 1; i <= pathParts.length; i++) {
addToTree(pathParts.slice(0, i), i === pathParts.length);
}
});
// Helper to render the tree structure
const renderTree = (node, prefix = '', isLast = true, parentPrefix = '') => {
let result = '';
const entries = Object.entries(node);
entries.forEach(([name, data], index) => {
const isLastEntry = index === entries.length - 1;
const newPrefix = parentPrefix + (isLast ? ' ' : '│ ');
result += `${prefix}${isLastEntry ? '└── ' : '├── '}${name}${data.isFile ? '' : '/'}\n`;
if (!data.isFile && Object.keys(data.children).length > 0) {
result += renderTree(data.children, newPrefix + '', isLastEntry, newPrefix);
}
});
return result;
};
return renderTree(tree);
}
// Update the generate-prompt handler
ipcMain.handle('generate-prompt', async (event, { selectedFiles, instructions, activeTemplates, currentRepo, includeFileTree = false }) => {
try {
const sections = [];
// Add templates
if (activeTemplates?.length > 0) {
sections.push(activeTemplates.map(t => t.content).join('\n\n'));
}
// Add instructions
if (instructions?.trim()) {
sections.push(`Instructions:\n${instructions.trim()}`);
}
sections.push("---")
// Add file tree if requested (now only for selected files)
if (includeFileTree && selectedFiles?.length > 0) {
try {
const fileTree = await generateFileTree(currentRepo, selectedFiles.filter(f => f.selected));
sections.push(`Project Structure:\n\`\`\`\n${fileTree}\`\`\`\n---`);
} catch (error) {
console.error('Error generating file tree:', error);
sections.push('Error generating project structure\n---');
}
}
sections.push("=== codebase ===")
// Rest of the function remains the same...
const getFileContent = async (filePath) => {
const fullPath = path.join(currentRepo, filePath);
const content = await fs.readFile(fullPath, 'utf-8');
return `${filePath}\n\`\`\`\n${content}\n\`\`\``;
};
// Add file contents
if (selectedFiles?.length > 0) {
for (const file of selectedFiles) {
try {
const fullPath = path.join(currentRepo, file.path);
// Security check
if (!fullPath.startsWith(currentRepo)) {
throw new Error('Invalid file path');
}
sections.push(await getFileContent(file.path));
} catch (error) {
console.error(`Error reading file ${file.path}:`, error);
sections.push(`${file.path}\nError reading file: ${error.message}`);
}
}
}
const finalText = sections.join('\n\n');
const tokenCount = encode(finalText).length;
return {
text: finalText,
tokens: tokenCount
};
} catch (error) {
console.error('Error generating prompt:', error);
throw error;
}
});
ipcMain.handle('apply-code-changes', async (event, changes) => {
const currentRepo = store.get('globalSettings', {}).lastOpenedRepo;
if (!currentRepo) {
throw new Error('No repository is currently open');
}
const results = [];
for (const change of changes) {
const fullPath = path.join(currentRepo, change.path);
// Security check - ensure file is within repo
if (!fullPath.startsWith(currentRepo)) {
results.push({
path: change.path,
success: false,
error: 'Invalid file path'
});
continue;
}
try {
switch (change.operation.toLowerCase()) {
case 'create':
// Ensure directory exists
await fs.mkdir(path.dirname(fullPath), { recursive: true });
await fs.writeFile(fullPath, change.code);
break;
case 'update':
case 'modify':
await fs.writeFile(fullPath, change.code);
break;
case 'delete':
await fs.unlink(fullPath);
break;
default:
throw new Error(`Unknown operation: ${change.operation}`);
}
results.push({
path: change.path,
success: true,
summary: change.summary
});
} catch (error) {
results.push({
path: change.path,
success: false,
error: error.message
});
}
}
return results;
});