-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
588 lines (490 loc) · 18.8 KB
/
index.js
File metadata and controls
588 lines (490 loc) · 18.8 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
#!/usr/bin/env node
const { Command } = require('commander');
const chalk = require('chalk');
const inquirer = require('inquirer');
const fs = require('fs').promises;
const path = require('path');
const axios = require('axios');
const ora = require('ora');
require('dotenv').config();
const program = new Command();
// GitHub API configuration
const GITHUB_BASE_URL = 'https://api.github.com/repos/Now-AI-Foundry/Now-SC-Base-Prompts/contents/Prompts';
const GITHUB_API_URL = 'https://api.github.com';
const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1/chat/completions';
const DEFAULT_MODEL = 'google/gemini-2.0-flash-exp:free';
// Directory structure template
const DIRECTORY_STRUCTURE = {
'00_Inbox': {
'calls': {
'internal': {},
'external': {}
},
'emails': {},
'notes': {}
},
'01_Customers': {},
'10_PromptTemplates': {},
'20_Demo_Library': {},
'30_CommunicationTemplates': {},
'99_Assets': {
'Project_Overview': {},
'Communications': {},
'POC_Documents': {}
}
};
// Create directory structure recursively
async function createDirectoryStructure(basePath, structure, customerName = null) {
for (const [dirName, subDirs] of Object.entries(structure)) {
let fullPath = path.join(basePath, dirName);
// Handle customer placeholder
if (dirName === '01_Customers' && customerName) {
const customerPath = path.join(fullPath, customerName);
await fs.mkdir(customerPath, { recursive: true });
} else {
await fs.mkdir(fullPath, { recursive: true });
}
// Recursively create subdirectories
if (Object.keys(subDirs).length > 0) {
await createDirectoryStructure(fullPath, subDirs);
}
}
}
// Fetch prompts from GitHub
async function fetchPrompts() {
try {
const response = await axios.get(GITHUB_BASE_URL);
const files = response.data.filter(item => item.type === 'file' && item.name.endsWith('.md'));
const prompts = [];
for (const file of files) {
const contentResponse = await axios.get(file.download_url);
prompts.push({
name: file.name,
content: contentResponse.data
});
}
return prompts;
} catch (error) {
throw new Error(`Failed to fetch prompts from GitHub: ${error.message}`);
}
}
// Save prompts to the prompt templates directory
async function savePrompts(projectPath, prompts) {
const promptsPath = path.join(projectPath, '10_PromptTemplates');
for (const prompt of prompts) {
const filePath = path.join(promptsPath, prompt.name);
await fs.writeFile(filePath, prompt.content, 'utf8');
}
}
// Fetch and save communication templates
async function fetchAndSaveCommunicationTemplates(projectPath) {
const templates = [
{
url: 'https://raw.githubusercontent.com/Now-AI-Foundry/Now-SC-Base-Prompts/main/Templates/servicenow_poc_status_template.html',
fileName: 'servicenow_poc_status_template.html'
}
];
const templatesPath = path.join(projectPath, '30_CommunicationTemplates');
for (const template of templates) {
try {
const response = await axios.get(template.url);
const filePath = path.join(templatesPath, template.fileName);
await fs.writeFile(filePath, response.data, 'utf8');
} catch (error) {
console.warn(chalk.yellow(`\nWarning: Failed to download template from ${template.url}. Skipping.`));
}
}
}
// Execute a prompt using OpenRouter API
async function executePrompt(promptContent, userInput = '') {
const apiKey = process.env.OPENROUTER_API_KEY;
if (!apiKey) {
throw new Error('OPENROUTER_API_KEY environment variable is not set');
}
try {
const response = await axios.post(
OPENROUTER_API_URL,
{
model: DEFAULT_MODEL,
messages: [
{
role: 'system',
content: promptContent
},
{
role: 'user',
content: userInput || 'Please provide guidance based on the system prompt.'
}
]
},
{
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://github.com/now-sc-cli',
'X-Title': 'Now-SC CLI Tool'
}
}
);
return response.data.choices[0].message.content;
} catch (error) {
if (error.response) {
throw new Error(`OpenRouter API error: ${error.response.data.error?.message || error.response.statusText}`);
}
throw new Error(`Failed to execute prompt: ${error.message}`);
}
}
// GitHub organization for new repositories
const GITHUB_ORG = 'Now-AI-Foundry';
// Create GitHub repository under the organization
async function createGitHubRepo(repoName, description) {
const token = process.env.GITHUB_PAT;
if (!token) {
throw new Error('GITHUB_PAT environment variable is not set');
}
try {
const response = await axios.post(
`${GITHUB_API_URL}/orgs/${GITHUB_ORG}/repos`,
{
name: repoName,
description: description,
private: true,
auto_init: false
},
{
headers: {
'Authorization': `token ${token}`,
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json'
}
}
);
return response.data;
} catch (error) {
if (error.response && error.response.status === 422) {
throw new Error(`Repository "${repoName}" already exists in ${GITHUB_ORG} organization`);
}
throw new Error(`Failed to create GitHub repository: ${error.message}`);
}
}
// Get GitHub username
async function getGitHubUsername() {
const token = process.env.GITHUB_PAT;
if (!token) {
return null;
}
try {
const response = await axios.get(`${GITHUB_API_URL}/user`, {
headers: {
'Authorization': `token ${token}`,
'Accept': 'application/vnd.github.v3+json'
}
});
return response.data.login;
} catch (error) {
return null;
}
}
// Initialize git repository without pushing
async function initializeGitRepo(projectPath, repoUrl) {
const { exec } = require('child_process');
const util = require('util');
const execPromise = util.promisify(exec);
const commands = [
'git init',
`git remote add origin ${repoUrl}`,
'git branch -M main'
];
for (const cmd of commands) {
try {
await execPromise(cmd, { cwd: projectPath });
} catch (error) {
throw error;
}
}
}
// Initialize command
program
.name('now-sc')
.description('CLI tool for bootstrapping presales projects for solution consultants')
.version('1.0.0');
// Init command - create new project
program
.command('init')
.description('Initialize a new presales project')
.option('-n, --name <name>', 'Project name')
.option('-c, --customer <customer>', 'Customer name')
.option('--no-github', 'Skip GitHub repository creation')
.action(async (options) => {
try {
let projectName = options.name;
let customerName = options.customer;
// Interactive prompts if options not provided
if (!projectName || !customerName) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'projectName',
message: 'What is the project name?',
default: 'presales-project',
when: !projectName
},
{
type: 'input',
name: 'customerName',
message: 'What is the customer name?',
when: !customerName,
validate: (input) => input.trim() !== '' || 'Customer name is required'
}
]);
projectName = projectName || answers.projectName;
customerName = customerName || answers.customerName;
}
const projectPath = path.join(process.cwd(), projectName);
// Check if directory already exists
try {
await fs.access(projectPath);
const { overwrite } = await inquirer.prompt([
{
type: 'confirm',
name: 'overwrite',
message: `Directory ${projectName} already exists. Overwrite?`,
default: false
}
]);
if (!overwrite) {
console.log(chalk.yellow('Project initialization cancelled.'));
return;
}
await fs.rm(projectPath, { recursive: true, force: true });
} catch (error) {
// Directory doesn't exist, which is what we want
}
const spinner = ora('Creating project structure...').start();
// Create base directory
await fs.mkdir(projectPath, { recursive: true });
// Create directory structure
await createDirectoryStructure(projectPath, DIRECTORY_STRUCTURE, customerName);
spinner.text = 'Fetching base prompts from GitHub...';
// Fetch and save prompts
const prompts = await fetchPrompts();
await savePrompts(projectPath, prompts);
spinner.text = 'Fetching communication templates...';
await fetchAndSaveCommunicationTemplates(projectPath);
// Create a README file
const readmeContent = `# ${projectName}
## Customer: ${customerName}
This project was bootstrapped with Now-SC CLI tool.
## Directory Structure
- **00_Inbox/** - Raw meeting notes and transcripts
- calls/internal - Internal call recordings and notes
- calls/external - External call recordings and notes
- emails - Email communications
- notes - General notes
- **01_Customers/${customerName}/** - Customer-specific information
- **10_PromptTemplates/** - Ready-to-use prompt templates
- **20_Demo_Library/** - Demo materials and resources
- **99_Assets/** - Processed and synthesized outputs
- Project_Overview - High-level project summaries
- Communications - Prepared communications
- POC_Documents - Proof of concept documentation
## Using Prompts
To execute a prompt, use:
\`\`\`bash
now-sc prompt
\`\`\`
Make sure you have set the OPENROUTER_API_KEY environment variable.
`;
await fs.writeFile(path.join(projectPath, 'README.md'), readmeContent);
// Create .env.example file
const envExample = `# OpenRouter API Key
# Get your API key from https://openrouter.ai/
OPENROUTER_API_KEY=your_api_key_here
`;
await fs.writeFile(path.join(projectPath, '.env.example'), envExample);
// Create .gitignore file
const gitignoreContent = `node_modules/
.env
.DS_Store
*.log
`;
await fs.writeFile(path.join(projectPath, '.gitignore'), gitignoreContent);
spinner.succeed(chalk.green(`Project "${projectName}" created successfully!`));
// Create GitHub repository if not skipped
if (options.github !== false && process.env.GITHUB_PAT) {
spinner.start(`Creating GitHub repository in ${GITHUB_ORG} organization...`);
try {
const repoName = projectName.replace(/[^a-zA-Z0-9-_]/g, '-');
const repoDescription = `Presales project for ${customerName}`;
const repo = await createGitHubRepo(repoName, repoDescription);
spinner.text = 'Initializing Git repository...';
await initializeGitRepo(projectPath, repo.clone_url);
spinner.succeed(chalk.green(`GitHub repository created in ${GITHUB_ORG}!`));
console.log(chalk.cyan(`Repository URL: ${repo.html_url}`));
console.log(chalk.gray('Git initialized with remote origin set.'));
console.log(chalk.gray('To push your code: git add . && git commit -m "Initial commit" && git push -u origin main'));
} catch (error) {
spinner.fail(chalk.red(`GitHub repository creation failed: ${error.message}`));
console.log(chalk.yellow('You can create the repository manually later.'));
}
} else if (options.github === false) {
console.log(chalk.gray('\nSkipped GitHub repository creation.'));
} else if (!process.env.GITHUB_PAT) {
console.log(chalk.yellow('\nNote: GITHUB_PAT environment variable not set. Skipping GitHub repository creation.'));
console.log(chalk.gray('To enable automatic repository creation, set your GitHub Personal Access Token:'));
console.log(chalk.gray(' export GITHUB_PAT=your_token_here'));
}
console.log('\n' + chalk.cyan('Project structure created:'));
console.log(chalk.gray(` ${projectPath}/`));
console.log(chalk.gray(' ├── 00_Inbox/'));
console.log(chalk.gray(' ├── 01_Customers/' + customerName + '/'));
console.log(chalk.gray(' ├── 10_PromptTemplates/'));
console.log(chalk.gray(' ├── 20_Demo_Library/'));
console.log(chalk.gray(' └── 99_Assets/'));
console.log('\n' + chalk.yellow('Next steps:'));
console.log(chalk.gray(' 1. cd ' + projectName));
console.log(chalk.gray(' 2. Set your OPENROUTER_API_KEY environment variable'));
console.log(chalk.gray(' 3. Run "now-sc prompt" to execute prompts'));
} catch (error) {
console.error(chalk.red('Error:'), error.message);
process.exit(1);
}
});
// Prompt command - execute prompts
program
.command('prompt')
.description('Execute a prompt template')
.action(async () => {
try {
// Check for API key
if (!process.env.OPENROUTER_API_KEY) {
console.error(chalk.red('Error: OPENROUTER_API_KEY environment variable is not set'));
console.log(chalk.yellow('Please set your OpenRouter API key:'));
console.log(chalk.gray(' export OPENROUTER_API_KEY=your_api_key_here'));
process.exit(1);
}
// Find prompt templates directory
const promptsPath = path.join(process.cwd(), '10_PromptTemplates');
try {
await fs.access(promptsPath);
} catch (error) {
console.error(chalk.red('Error: No prompt templates directory found in current directory'));
console.log(chalk.yellow('Make sure you are in a project created with "now-sc init"'));
process.exit(1);
}
// List available prompts
const files = await fs.readdir(promptsPath);
const promptFiles = files.filter(f => f.endsWith('.md'));
if (promptFiles.length === 0) {
console.error(chalk.red('Error: No prompt templates found'));
process.exit(1);
}
// Let user select a prompt
const { selectedPrompt } = await inquirer.prompt([
{
type: 'list',
name: 'selectedPrompt',
message: 'Select a prompt template:',
choices: promptFiles.map(f => ({
name: f.replace('.md', '').replace(/_/g, ' '),
value: f
}))
}
]);
// Read the prompt content
const promptContent = await fs.readFile(path.join(promptsPath, selectedPrompt), 'utf8');
// Show prompt preview
console.log('\n' + chalk.cyan('Prompt Preview:'));
console.log(chalk.gray('─'.repeat(50)));
console.log(chalk.gray(promptContent.substring(0, 200) + '...'));
console.log(chalk.gray('─'.repeat(50)));
// Get user input
const { userInput } = await inquirer.prompt([
{
type: 'editor',
name: 'userInput',
message: 'Enter your input for this prompt (press Enter to open editor):'
}
]);
const spinner = ora('Executing prompt...').start();
try {
const result = await executePrompt(promptContent, userInput);
spinner.succeed(chalk.green('Prompt executed successfully!'));
console.log('\n' + chalk.cyan('Response:'));
console.log(chalk.gray('─'.repeat(50)));
console.log(result);
console.log(chalk.gray('─'.repeat(50)));
// Ask if user wants to save the output
const { saveOutput } = await inquirer.prompt([
{
type: 'confirm',
name: 'saveOutput',
message: 'Would you like to save this output?',
default: true
}
]);
if (saveOutput) {
const { outputLocation } = await inquirer.prompt([
{
type: 'list',
name: 'outputLocation',
message: 'Where would you like to save the output?',
choices: [
{ name: 'Project Overview', value: '99_Assets/Project_Overview' },
{ name: 'Communications', value: '99_Assets/Communications' },
{ name: 'POC Documents', value: '99_Assets/POC_Documents' },
{ name: 'Notes', value: '00_Inbox/notes' },
{ name: 'Other (specify)', value: 'other' }
]
}
]);
let savePath = outputLocation;
if (outputLocation === 'other') {
const { customPath } = await inquirer.prompt([
{
type: 'input',
name: 'customPath',
message: 'Enter the path (relative to project root):',
default: '99_Assets'
}
]);
savePath = customPath;
}
const { filename } = await inquirer.prompt([
{
type: 'input',
name: 'filename',
message: 'Enter filename (without extension):',
default: `${selectedPrompt.replace('.md', '')}_${new Date().toISOString().split('T')[0]}`,
validate: (input) => input.trim() !== '' || 'Filename is required'
}
]);
const fullPath = path.join(process.cwd(), savePath, `${filename}.md`);
await fs.mkdir(path.dirname(fullPath), { recursive: true });
const outputContent = `# ${filename.replace(/_/g, ' ')}
**Date:** ${new Date().toLocaleString()}
**Prompt Template:** ${selectedPrompt}
**Model:** ${DEFAULT_MODEL}
## User Input
${userInput}
## Response
${result}
`;
await fs.writeFile(fullPath, outputContent);
console.log(chalk.green(`✓ Output saved to: ${fullPath}`));
}
} catch (error) {
spinner.fail(chalk.red('Failed to execute prompt'));
throw error;
}
} catch (error) {
console.error(chalk.red('Error:'), error.message);
process.exit(1);
}
});
// Parse command line arguments
program.parse(process.argv);
// Show help if no command provided
if (!process.argv.slice(2).length) {
program.outputHelp();
}