-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjectNavigator.js
More file actions
615 lines (528 loc) · 20.6 KB
/
ProjectNavigator.js
File metadata and controls
615 lines (528 loc) · 20.6 KB
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
const fs = require('fs');
const path = require('path');
const os = require('os');
/**
* Project Navigator - Extracted from StreamTelegramBot
* Handles project selection and directory management
*/
class ProjectNavigator {
constructor(bot, options, mainBot) {
this.bot = bot;
this.options = options;
this.mainBot = mainBot; // Reference to main bot for delegation
this.projectCache = new Map(); // shortId -> fullPath
this.projectCacheCounter = 0;
// State for new project creation
this.projectCreationState = {
inProgress: false,
chatId: null
};
}
/**
* Get Claude projects from ~/.claude.json
*/
getClaudeProjects() {
try {
const claudeConfigPath = path.join(os.homedir(), '.claude.json');
if (!fs.existsSync(claudeConfigPath)) {
console.log('⚠️ ~/.claude.json not found');
return [];
}
const claudeConfig = JSON.parse(fs.readFileSync(claudeConfigPath, 'utf8'));
if (!claudeConfig.projects) {
console.log('⚠️ No projects section found in ~/.claude.json');
return [];
}
// Get all directories and filter existing ones
const projectDirs = Object.keys(claudeConfig.projects).filter(dir => {
return fs.existsSync(dir) && fs.statSync(dir).isDirectory();
});
// Sort by last used if available
return projectDirs.sort((a, b) => {
const aTime = claudeConfig.projects[a]?.lastUsed || 0;
const bTime = claudeConfig.projects[b]?.lastUsed || 0;
return bTime - aTime; // Newest first
});
} catch (error) {
console.error('Error reading Claude config:', error.message);
return [];
}
}
/**
* Show project selection with inline keyboard
*/
async showProjectSelection(chatId, page = 0) {
const projects = this.getClaudeProjects();
if (projects.length === 0) {
await this.mainBot.safeSendMessage(chatId,
`📁 **Current Directory**\n${this.options.workingDirectory}\n\n` +
'❌ No Claude projects found\n' +
'💡 Open projects in Claude Code first'
);
return;
}
// Pagination settings
const projectsPerPage = 12;
const startIndex = page * projectsPerPage;
const endIndex = startIndex + projectsPerPage;
const currentPageProjects = projects.slice(startIndex, endIndex);
const totalPages = Math.ceil(projects.length / projectsPerPage);
// Clear old cache and create new short IDs
this.projectCache.clear();
this.projectCacheCounter = 0;
// Create inline buttons for projects with short IDs
const buttons = currentPageProjects.map(projectPath => {
const projectName = path.basename(projectPath);
const isCurrentDir = projectPath === this.options.workingDirectory;
const buttonText = isCurrentDir ? `✅ ${projectName}` : `📁 ${projectName}`;
// Create short ID and cache the full path
const shortId = `p${this.projectCacheCounter++}`;
this.projectCache.set(shortId, projectPath);
return [{
text: buttonText,
callback_data: `setdir:${shortId}`
}];
});
// Add pagination buttons if needed
if (totalPages > 1) {
const paginationRow = [];
if (page > 0) {
paginationRow.push({
text: '⬅️ Previous',
callback_data: `setdir:page:${page - 1}`
});
}
// Page indicator
paginationRow.push({
text: `${page + 1}/${totalPages}`,
callback_data: 'setdir:pageinfo'
});
if (page < totalPages - 1) {
paginationRow.push({
text: 'Next ➡️',
callback_data: `setdir:page:${page + 1}`
});
}
buttons.push(paginationRow);
}
// Add create new project and refresh buttons
buttons.push([{
text: '➕ Create New Project',
callback_data: 'setdir:create_new'
}, {
text: '🔄 Refresh Projects',
callback_data: 'setdir:refresh'
}]);
const keyboard = { inline_keyboard: buttons };
const pageInfo = totalPages > 1 ? `\n📄 Page ${page + 1}/${totalPages}` : '';
await this.mainBot.safeSendMessage(chatId,
`📁 *Current Directory*\n${this.options.workingDirectory}\n\n` +
'📋 **Select Claude Project:**\n' +
`(Showing ${startIndex + 1}-${Math.min(endIndex, projects.length)} of ${projects.length} projects)${pageInfo}`,
{ reply_markup: keyboard }
);
}
/**
* Handle directory change from callback
*/
async handleDirectoryChange(dirAction, chatId, messageId = null) {
try {
// Get actual path from cache or use directly
let actualPath = dirAction;
// Check if it's a cached short ID
if (this.projectCache.has(dirAction)) {
actualPath = this.projectCache.get(dirAction);
}
// Check if directory exists
if (!fs.existsSync(actualPath)) {
const errorMsg = `❌ Directory not found: ${actualPath}`;
if (messageId) {
await this.mainBot.safeEditMessage(chatId, messageId, errorMsg);
} else {
await this.mainBot.safeSendMessage(chatId, errorMsg);
}
return;
}
// Check if it's actually a directory
const stats = fs.statSync(actualPath);
if (!stats.isDirectory()) {
const errorMsg = `❌ Not a directory: ${actualPath}`;
if (messageId) {
await this.mainBot.safeEditMessage(chatId, messageId, errorMsg);
} else {
await this.mainBot.safeSendMessage(chatId, errorMsg);
}
return;
}
// Update working directory
this.options.workingDirectory = actualPath;
// Update UnifiedWebServer project root to match new directory
if (this.mainBot && this.mainBot.unifiedWebServer) {
this.mainBot.unifiedWebServer.updateProjectRoot(actualPath);
}
// IMPORTANT: Save the new project to config immediately
// This ensures the bot remembers the selected project after restart
await this.saveCurrentProjectToConfig(actualPath);
const successMsg =
'✅ **Directory Changed**\n\n' +
`📁 **New Directory:**\n\`${actualPath}\`\n\n` +
'💡 New sessions will use this directory\n' +
'🔄 Use /new to start fresh session here';
if (messageId) {
await this.mainBot.safeEditMessage(chatId, messageId, successMsg);
} else {
await this.mainBot.safeSendMessage(chatId, successMsg);
}
console.log(`[ProjectNavigator] Directory changed to: ${actualPath}`);
} catch (error) {
const errorMsg = `❌ Error: ${error.message}`;
if (messageId) {
await this.mainBot.safeEditMessage(chatId, messageId, errorMsg);
} else {
await this.mainBot.safeSendMessage(chatId, errorMsg);
}
}
}
/**
* Handle setdir callback routing
*/
async handleSetdirCallback(dirAction, chatId, messageId) {
if (dirAction === 'refresh') {
await this.bot.deleteMessage(chatId, messageId);
await this.showProjectSelection(chatId);
} else if (dirAction === 'create_new') {
await this.showCreateNewProjectInterface(chatId, messageId);
} else if (dirAction === 'cancel_create') {
// Cancel project creation
this.projectCreationState.inProgress = false;
this.projectCreationState.chatId = null;
await this.bot.deleteMessage(chatId, messageId);
await this.showProjectSelection(chatId);
} else if (dirAction.startsWith('page:')) {
const page = parseInt(dirAction.split(':')[1]);
await this.bot.deleteMessage(chatId, messageId);
await this.showProjectSelection(chatId, page);
} else if (dirAction === 'pageinfo') {
// Do nothing for page info button
return;
} else {
await this.handleDirectoryChange(dirAction, chatId, messageId);
}
}
/**
* Show create new project interface
*/
async showCreateNewProjectInterface(chatId, messageId) {
const projectsHomeDir = this.getProjectsHomeDirectory();
if (!projectsHomeDir) {
await this.mainBot.safeEditMessage(chatId, messageId,
'❌ **Projects Home Directory Not Configured**\n\n' +
'Please add `projectsHomeDirectory` to your bot configuration file.\n\n' +
'**Example:**\n```json\n{\n "projectsHomeDirectory": "/home/user/projects"\n}\n```',
{
reply_markup: {
inline_keyboard: [[
{ text: '🔙 Back to Projects', callback_data: 'setdir:refresh' }
]]
}
}
);
return;
}
// Check if projects home directory exists
if (!fs.existsSync(projectsHomeDir)) {
try {
fs.mkdirSync(projectsHomeDir, { recursive: true });
} catch (error) {
await this.mainBot.safeEditMessage(chatId, messageId,
`❌ **Cannot Create Projects Directory**\n\n` +
`**Path:** \`${projectsHomeDir}\`\n` +
`**Error:** ${error.message}\n\n` +
'Please check the path and permissions.',
{
reply_markup: {
inline_keyboard: [[
{ text: '🔙 Back to Projects', callback_data: 'setdir:refresh' }
]]
}
}
);
return;
}
}
// Set state for text input
this.projectCreationState.inProgress = true;
this.projectCreationState.chatId = chatId;
await this.mainBot.safeEditMessage(chatId, messageId,
'➕ **Create New Project**\n\n' +
`**Projects Directory:** \`${projectsHomeDir}\`\n\n` +
'💡 **Enter project name:**\n' +
'• Use alphanumeric characters and dashes\n' +
'• No spaces or special characters\n' +
'• Example: `my-awesome-project`\n\n' +
'📝 Type the project name in your next message...',
{
reply_markup: {
inline_keyboard: [[
{ text: '❌ Cancel', callback_data: 'setdir:cancel_create' }
]]
}
}
);
}
/**
* Get projects home directory from config
*/
getProjectsHomeDirectory() {
if (this.mainBot && this.mainBot.configManager) {
return this.mainBot.configManager.get('projectsHomeDirectory');
}
// Fallback to reading from config file directly
if (this.mainBot && this.mainBot.configFilePath) {
try {
const configData = fs.readFileSync(this.mainBot.configFilePath, 'utf8');
const config = JSON.parse(configData);
return config.projectsHomeDirectory;
} catch (error) {
console.error('[ProjectNavigator] Error reading config for projectsHomeDirectory:', error.message);
return null;
}
}
return null;
}
/**
* Handle text input for project creation
*/
async handleTextInput(chatId, text) {
if (this.projectCreationState.inProgress && this.projectCreationState.chatId === chatId) {
// Reset state
this.projectCreationState.inProgress = false;
this.projectCreationState.chatId = null;
// Process project creation
await this.createNewProject(chatId, text);
return true; // Indicate that we handled this text input
}
return false; // We didn't handle this text input
}
/**
* Create new project directory and initialize Claude Code files
*/
async createNewProject(chatId, projectName) {
try {
// Validate project name
const validationResult = this.validateProjectName(projectName);
if (!validationResult.valid) {
await this.mainBot.safeSendMessage(chatId,
'❌ **Invalid Project Name**\n\n' +
`**Error:** ${validationResult.error}\n\n` +
'💡 **Valid project names:**\n' +
'• Use alphanumeric characters and dashes only\n' +
'• Must start with letter or number\n' +
'• 3-50 characters long\n' +
'• Examples: `my-app`, `web-project-2024`',
{
reply_markup: {
inline_keyboard: [[
{ text: '➕ Try Again', callback_data: 'setdir:create_new' },
{ text: '📂 Projects', callback_data: 'setdir:refresh' }
]]
}
}
);
return;
}
const projectsHomeDir = this.getProjectsHomeDirectory();
const newProjectPath = path.join(projectsHomeDir, projectName);
// Check if project already exists
if (fs.existsSync(newProjectPath)) {
await this.mainBot.safeSendMessage(chatId,
'❌ **Project Already Exists**\n\n' +
`A project named \`${projectName}\` already exists.\n\n` +
'💡 Choose a different name or select the existing project.',
{
reply_markup: {
inline_keyboard: [[
{ text: '➕ Try Different Name', callback_data: 'setdir:create_new' },
{ text: '📂 Projects', callback_data: 'setdir:refresh' }
]]
}
}
);
return;
}
// Create project directory
fs.mkdirSync(newProjectPath, { recursive: true });
// Initialize Claude Code project structure
await this.initializeClaudeCodeProject(newProjectPath, projectName);
// Update working directory to the new project
this.options.workingDirectory = newProjectPath;
// Update UnifiedWebServer project root to match new directory
if (this.mainBot && this.mainBot.unifiedWebServer) {
this.mainBot.unifiedWebServer.updateProjectRoot(newProjectPath);
}
// Save to config
await this.saveCurrentProjectToConfig(newProjectPath);
await this.mainBot.safeSendMessage(chatId,
'✅ **Project Created Successfully**\n\n' +
`📁 **Project:** \`${projectName}\`\n` +
`📂 **Path:** \`${newProjectPath}\`\n\n` +
'🚀 **What was created:**\n' +
'• Project directory structure\n' +
'• Claude Code session files\n' +
'• Basic README.md file\n' +
'• .gitignore file\n\n' +
'💡 **Next steps:**\n' +
'• Start coding in your new project\n' +
'• Use /new to begin a Claude session',
{
reply_markup: {
inline_keyboard: [[
{ text: '🔄 New Session', callback_data: 'new_session' },
{ text: '📂 Projects', callback_data: 'setdir:refresh' }
]]
}
}
);
} catch (error) {
console.error('[ProjectNavigator] Error creating new project:', error);
await this.mainBot.safeSendMessage(chatId,
'❌ **Project Creation Failed**\n\n' +
`**Error:** ${error.message}\n\n` +
'💡 This might happen due to:\n' +
'• Permission issues\n' +
'• Disk space problems\n' +
'• Invalid directory path',
{
reply_markup: {
inline_keyboard: [[
{ text: '➕ Try Again', callback_data: 'setdir:create_new' },
{ text: '📂 Projects', callback_data: 'setdir:refresh' }
]]
}
}
);
}
}
/**
* Validate project name
*/
validateProjectName(projectName) {
if (!projectName || projectName.trim().length === 0) {
return { valid: false, error: 'Project name cannot be empty' };
}
const name = projectName.trim();
if (name.length < 3 || name.length > 50) {
return { valid: false, error: 'Project name must be 3-50 characters long' };
}
if (!/^[a-zA-Z0-9][a-zA-Z0-9-]*$/.test(name)) {
return { valid: false, error: 'Project name must start with letter/number and contain only letters, numbers, and dashes' };
}
if (name.startsWith('-') || name.endsWith('-')) {
return { valid: false, error: 'Project name cannot start or end with dash' };
}
if (name.includes('--')) {
return { valid: false, error: 'Project name cannot contain consecutive dashes' };
}
return { valid: true };
}
/**
* Initialize Claude Code project structure
*/
async initializeClaudeCodeProject(projectPath, projectName) {
// Create basic README.md
const readmeContent = `# ${projectName}\n\nA new project created with Claude Code Telegram Bot.\n\n## Getting Started\n\nThis project was automatically initialized with:\n- Basic project structure\n- Claude Code session support\n- Git ignore file\n\n## Usage\n\nUse the Telegram bot to:\n- Start coding sessions with /new\n- Manage git operations\n- Browse and edit files\n- Run development tasks\n\n## Development\n\nAdd your project-specific instructions here.\n`;
fs.writeFileSync(path.join(projectPath, 'README.md'), readmeContent, 'utf8');
// Create basic .gitignore
const gitignoreContent = `# Dependencies\nnode_modules/\n\n# Logs\nlogs\n*.log\nnpm-debug.log*\nyarn-debug.log*\nyarn-error.log*\n\n# Environment variables\n.env\n.env.local\n.env.development.local\n.env.test.local\n.env.production.local\n\n# IDE\n.vscode/\n.idea/\n*.swp\n*.swo\n*~\n\n# OS\n.DS_Store\nThumbs.db\n\n# Build output\ndist/\nbuild/\n\n# Claude Code temporary files\n.claude-temp/\n`;
fs.writeFileSync(path.join(projectPath, '.gitignore'), gitignoreContent, 'utf8');
// Update ~/.claude.json to add this project
await this.addProjectToClaudeConfig(projectPath);
}
/**
* Add project to ~/.claude.json configuration
*/
async addProjectToClaudeConfig(projectPath) {
try {
const claudeConfigPath = path.join(os.homedir(), '.claude.json');
let claudeConfig = {};
if (fs.existsSync(claudeConfigPath)) {
const configData = fs.readFileSync(claudeConfigPath, 'utf8');
claudeConfig = JSON.parse(configData);
}
if (!claudeConfig.projects) {
claudeConfig.projects = {};
}
// Add the new project with basic configuration
claudeConfig.projects[projectPath] = {
allowedTools: [],
dontCrawlDirectory: false,
hasClaudeMdExternalIncludesApproved: false,
hasClaudeMdExternalIncludesWarningShown: false,
hasTrustDialogAccepted: false,
history: [],
lastTotalWebSearchRequests: 0,
mcpContextUris: [],
mcpServers: {},
projectOnboardingSeenCount: 0,
lastUsed: Date.now()
};
// Write back to file
fs.writeFileSync(claudeConfigPath, JSON.stringify(claudeConfig, null, 2), 'utf8');
console.log(`[ProjectNavigator] Added project to Claude config: ${projectPath}`);
} catch (error) {
console.error('[ProjectNavigator] Error updating Claude config:', error.message);
// Don't throw - project creation should succeed even if Claude config update fails
}
}
/**
* Save current project to config file for persistence
*/
async saveCurrentProjectToConfig(projectPath) {
// Save the project as currentProject in config using ConfigManager
if (this.mainBot && this.mainBot.configManager) {
try {
// Update the currentProject using ConfigManager (handles disk persistence)
this.mainBot.configManager.setCurrentProject(projectPath);
// Ensure projectSessions exists - ConfigManager handles this automatically
const projectSessions = this.mainBot.configManager.getProjectSessions();
// Note: We don't create a session here, just set the current project
// Sessions will be created/updated when the user actually starts using the project
console.log(`[ProjectNavigator] Saved current project to config: ${projectPath}`);
} catch (error) {
console.error('[ProjectNavigator] Error saving current project to config:', error.message);
}
} else if (this.mainBot && this.mainBot.configFilePath) {
// Fallback to old method if ConfigManager not available
try {
const fs = require('fs');
const configData = fs.readFileSync(this.mainBot.configFilePath, 'utf8');
const config = JSON.parse(configData);
config.currentProject = projectPath;
if (!config.projectSessions) {
config.projectSessions = {};
}
fs.writeFileSync(this.mainBot.configFilePath, JSON.stringify(config, null, 2));
console.log(`[ProjectNavigator] Saved current project to config (fallback): ${projectPath}`);
} catch (error) {
console.error('[ProjectNavigator] Error saving current project to config (fallback):', error.message);
}
}
}
/**
* Cleanup project cache
*/
cleanup() {
this.projectCache.clear();
console.log('[ProjectNavigator] Cache cleared');
}
/**
* Get stats for debugging
*/
getStats() {
return {
cachedProjects: this.projectCache.size,
currentDirectory: this.options.workingDirectory
};
}
}
module.exports = ProjectNavigator;