-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor-cortex-cli.js
More file actions
executable file
·462 lines (382 loc) · 15.3 KB
/
Copy pathcursor-cortex-cli.js
File metadata and controls
executable file
·462 lines (382 loc) · 15.3 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
#!/usr/bin/env node
import { exec } from 'child_process';
import readline from 'readline';
import path from 'path';
// Main function to handle CLI operations
async function main() {
// Parse command-line arguments for direct tool invocation
const args = process.argv.slice(2);
// Check if a specific tool is being called directly
if (args.length > 0) {
const toolName = args[0];
const toolArgs = args.slice(1);
// Convert arguments to a format suitable for the MCP tool
const formattedArgs = toolArgs.map(arg => {
// Handle --param=value format
if (arg.startsWith('--') && arg.includes('=')) {
const [param, value] = arg.slice(2).split('=');
return `--${param}="${value}"`;
}
// Handle individual parameters (assume they are values if they don't start with --)
else if (!arg.startsWith('--')) {
return `"${arg}"`;
}
return arg;
}).join(' ');
// Execute the command directly
exec(`node index.js ${toolName} ${formattedArgs}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
if (stderr) {
console.error(stderr);
}
console.log(stdout);
process.exit(0);
});
// Exit early - we're not launching the interactive CLI
return;
}
// If no direct tool invocation, launch the interactive CLI
await launchInteractiveCLI();
}
// Launch the interactive CLI
async function launchInteractiveCLI() {
// Create readline interface for user interaction
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Helper function to get current branch name
async function getCurrentBranch() {
return new Promise((resolve) => {
exec('git branch --show-current', (error, stdout, stderr) => {
if (error) {
console.warn('Warning: Not in a git repository. Using "main" as default branch.');
resolve('main');
} else {
if (stderr) console.warn('Git stderr:', stderr);
resolve(stdout.trim() || 'main');
}
});
});
}
// Helper function to get current project name from directory
function getProjectName() {
// Get the current directory name as project name
const dirName = path.basename(process.cwd());
return dirName || 'default-project';
}
// Helper to prompt user with a question
function prompt(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
}
// Helper to prompt for multi-line input
async function promptMultiLine(question) {
console.log(question);
console.log('(Type "END" on a new line to finish)');
let lines = [];
let line;
while (true) {
line = await prompt('> ');
if (line === 'END') break;
lines.push(line);
}
return lines.join('\n');
}
// Function to capture tacit knowledge
async function captureTacitKnowledge() {
console.log('\n=== Tacit Knowledge Capture ===\n');
const currentBranch = await getCurrentBranch();
const projectName = getProjectName();
console.log(`Project: ${projectName}`);
console.log(`Branch: ${currentBranch}`);
const title = await prompt('Knowledge Title: ');
const author = await prompt('Author: ');
const tags = await prompt('Tags (comma-separated): ');
console.log('\nProblem Statement:');
const problemStatement = await promptMultiLine('Describe the problem or situation that required expertise:');
console.log('\nEnvironment/Conditions:');
const environment = await promptMultiLine('Describe relevant environmental factors (systems, versions, etc.):');
console.log('\nConstraints:');
const constraints = await promptMultiLine('List any limitations or constraints that influenced the approach:');
console.log('\nApproach:');
const approach = await promptMultiLine('Explain your approach to solving the problem:');
console.log('\nOutcome:');
const outcome = await promptMultiLine('Describe the result of applying this knowledge:');
console.log('\nRelated Documentation:');
const relatedDocumentation = await promptMultiLine('Links to related documentation, tickets, or resources:');
// Call the MCP tool
try {
const command = `node index.js create_tacit_knowledge --title="${title}" --author="${author}" --projectName="${projectName}" --branchName="${currentBranch}" ${tags ? `--tags="${tags}"` : ''} --problemStatement="${problemStatement}" ${environment ? `--environment="${environment}"` : ''} ${constraints ? `--constraints="${constraints}"` : ''} --approach="${approach}" --outcome="${outcome}" ${relatedDocumentation ? `--relatedDocumentation="${relatedDocumentation}"` : ''}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) console.warn('Command stderr:', stderr);
console.log('\n' + stdout);
});
} catch (error) {
console.error('Failed to execute command:', error);
}
}
// Function to create completion checklist
async function createCompletionChecklist() {
console.log('\n=== Completion Checklist Creation ===\n');
const currentBranch = await getCurrentBranch();
const projectName = getProjectName();
console.log(`Project: ${projectName}`);
console.log(`Branch: ${currentBranch}`);
const featureName = await prompt('Feature/Module Name: ');
const owner = await prompt('Owner: ');
const jiraTicket = await prompt('Jira Ticket (if applicable): ');
console.log('\nObjectives:');
console.log('(Enter one objective per line)');
const objectives = await promptMultiLine('Main objectives of this feature:');
console.log('\nRequirements:');
console.log('(Enter one requirement per line)');
const requirements = await promptMultiLine('Key requirements that need to be met:');
console.log('\nTest Criteria:');
console.log('(Enter one test criterion per line)');
const testCriteria = await promptMultiLine('Criteria for successful testing:');
console.log('\nKnowledge Items:');
console.log('(Enter one knowledge item per line)');
const knowledgeItems = await promptMultiLine('List of knowledge items that should be documented:');
// Call the MCP tool
try {
const command = `node index.js create_completion_checklist --projectName="${projectName}" --featureName="${featureName}" --owner="${owner}" --requirements="${requirements}" --objectives="${objectives}" ${testCriteria ? `--testCriteria="${testCriteria}"` : ''} ${knowledgeItems ? `--knowledgeItems="${knowledgeItems}"` : ''} ${jiraTicket ? `--jiraTicket="${jiraTicket}"` : ''}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) console.warn('Command stderr:', stderr);
console.log('\n' + stdout);
});
} catch (error) {
console.error('Failed to execute command:', error);
}
}
// Function to update branch note
async function updateBranchNote() {
console.log('\n=== Update Branch Note ===\n');
const currentBranch = await getCurrentBranch();
const projectName = getProjectName();
// Using the current branch without asking if it's available (which it always should be now)
const branchName = currentBranch;
console.log(`Project: ${projectName}`);
console.log(`Branch: ${branchName}`);
console.log('\nChange Message:');
const message = await promptMultiLine('Describe the changes made:');
// Call the MCP tool
try {
const command = `node index.js update_branch_note --branchName="${branchName}" --projectName="${projectName}" --message="${message}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) console.warn('Command stderr:', stderr);
console.log('\n' + stdout);
});
} catch (error) {
console.error('Failed to execute command:', error);
}
}
// Function to filter branch notes
async function filterBranchNotes() {
console.log('\n=== Filter Branch Notes ===\n');
const currentBranch = await getCurrentBranch();
const projectName = getProjectName();
console.log(`Project: ${projectName}`);
console.log(`Branch: ${currentBranch}`);
console.log('\nFilter Options:');
console.log('1. Show uncommitted work only');
console.log('2. Filter by date range');
console.log('3. Filter by commit hash');
console.log('4. Show raw file (including COMMIT separators)');
const filterChoice = await prompt('\nSelect a filter option (1-4): ');
let filterArgs = '';
switch (filterChoice) {
case '1':
filterArgs = '--uncommittedOnly=true';
break;
case '2':
const afterDate = await prompt('After date (YYYY-MM-DD, leave empty for no limit): ');
const beforeDate = await prompt('Before date (YYYY-MM-DD, leave empty for no limit): ');
if (afterDate) {
filterArgs += ` --afterDate="${afterDate}"`;
}
if (beforeDate) {
filterArgs += ` --beforeDate="${beforeDate}"`;
}
break;
case '3':
const commitHash = await prompt('Commit hash (full or partial): ');
if (commitHash) {
filterArgs += ` --commitHash="${commitHash}"`;
}
break;
case '4':
filterArgs = '--mode=raw';
break;
default:
console.log('Invalid option selected. Showing the full file.');
filterArgs = '--mode=raw';
break;
}
// Call the MCP tool
try {
const command = `node index.js read_branch_notes --branchName="${currentBranch}" --projectName="${projectName}" ${filterArgs}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) console.warn('Command stderr:', stderr);
console.log('\n' + stdout);
});
} catch (error) {
console.error('Failed to execute command:', error);
}
}
// Function to generate Jira comment
async function generateJiraComment() {
console.log('\n=== Generate Jira Comment ===\n');
const currentBranch = await getCurrentBranch();
const projectName = getProjectName();
const ticketId = await prompt('Jira Ticket ID: ');
// Using the current branch without asking if it's available
const branchName = currentBranch;
console.log(`Project: ${projectName}`);
console.log(`Branch: ${branchName}`);
const jiraBaseUrl = await prompt('Jira Base URL (optional): ');
// Call the MCP tool
try {
const command = `node index.js generate_jira_comment --ticketId="${ticketId}" --branchName="${branchName}" --projectName="${projectName}" ${jiraBaseUrl ? `--jiraBaseUrl="${jiraBaseUrl}"` : ''}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) console.warn('Command stderr:', stderr);
console.log('\n' + stdout);
});
} catch (error) {
console.error('Failed to execute command:', error);
}
}
// Function to archive branch note
async function archiveBranchNote() {
console.log('\n=== Archive Branch Note ===\n');
const currentBranch = await getCurrentBranch();
const projectName = getProjectName();
console.log(`Project: ${projectName}`);
console.log(`Branch: ${currentBranch}`);
const confirmArchive = await prompt('Are you sure you want to archive the branch note? This will move it to an archive file and delete the original. (y/n): ');
if (confirmArchive.toLowerCase() !== 'y') {
console.log('Archive cancelled.');
return;
}
const customDate = await prompt('Enter archive date (YYYY-MM-DD) or press Enter for today: ');
const dateArg = customDate ? `--archiveDate="${customDate}"` : '';
// Call the MCP tool
try {
const command = `node index.js archive_branch_note --branchName="${currentBranch}" --projectName="${projectName}" ${dateArg}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) console.warn('Command stderr:', stderr);
console.log('\n' + stdout);
});
} catch (error) {
console.error('Failed to execute command:', error);
}
}
// Function to clear branch note
async function clearBranchNote() {
console.log('\n=== Clear Branch Note ===\n');
const currentBranch = await getCurrentBranch();
const projectName = getProjectName();
console.log(`Project: ${projectName}`);
console.log(`Branch: ${currentBranch}`);
const confirmClear = await prompt('Are you sure you want to clear the branch note? This will remove all entries. (y/n): ');
if (confirmClear.toLowerCase() !== 'y') {
console.log('Clear operation cancelled.');
return;
}
const createArchive = await prompt('Create an archive before clearing? (y/n): ');
const keepHeader = await prompt('Keep the branch note header? (y/n): ');
// Call the MCP tool
try {
const command = `node index.js clear_branch_note --branchName="${currentBranch}" --projectName="${projectName}" --createArchive=${createArchive.toLowerCase() === 'y'} --keepHeader=${keepHeader.toLowerCase() === 'y'}`;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) console.warn('Command stderr:', stderr);
console.log('\n' + stdout);
});
} catch (error) {
console.error('Failed to execute command:', error);
}
}
// Main menu function
async function mainMenu() {
while (true) {
console.log('\n=== Cursor-Cortex Knowledge Management System ===');
console.log('1. Capture Tacit Knowledge');
console.log('2. Create Completion Checklist');
console.log('3. Update Branch Note');
console.log('4. Filter Branch Notes');
console.log('5. Generate Jira Comment');
console.log('6. Archive Branch Note');
console.log('7. Clear Branch Note');
console.log('0. Exit');
const choice = await prompt('\nSelect an option: ');
switch (choice) {
case '1':
await captureTacitKnowledge();
break;
case '2':
await createCompletionChecklist();
break;
case '3':
await updateBranchNote();
break;
case '4':
await filterBranchNotes();
break;
case '5':
await generateJiraComment();
break;
case '6':
await archiveBranchNote();
break;
case '7':
await clearBranchNote();
break;
case '0':
console.log('Exiting Cursor-Cortex CLI.');
rl.close();
process.exit(0);
break;
default:
console.log('Invalid option. Please try again.');
}
}
}
// Start the application
await mainMenu();
}
// Start the application
main();