-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·333 lines (285 loc) · 10.2 KB
/
index.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
#!/usr/bin/env node
// Usage: npx create-codebolt-agent my-app
const spawn = require('cross-spawn');
const fs = require('fs');
const path = require('path');
const { Command } = require('commander');
const inquirer = require('inquirer');
const yaml = require('js-yaml');
const { v4: uuidv4 } = require('uuid');
const chalk = require('chalk');
const program = new Command();
program.option('-n, --name <name>', 'name of the project');
program.parse(process.argv);
const options = program.opts();
let projectName = '';
projectName = options.name || process.argv[2];
// Get the list of available templates
const templateDir = path.resolve(__dirname, 'template');
const templates = fs.readdirSync(templateDir).filter(file => fs.statSync(path.join(templateDir, file)).isDirectory());
const agentymlpath = path.join(__dirname, 'template/basic', 'codeboltagent.yaml');
let agentYamlData = fs.readFileSync(agentymlpath, 'utf8');
// Parse the YAML file
const parsedYaml = yaml.load(agentYamlData);
const currentPath = process.cwd(); // Gets the current working directory
const prompts = [
{
type: 'input',
name: 'projectName',
message: 'Please enter the name of your application:',
default: projectName,
},
{
type: 'input',
name: 'unique_id',
message: 'Please enter the unique_id:',
default: projectName.replace(/[^a-zA-Z0-9]/g, ''),
validate: function (input) {
if (/\s/.test(input)) {
return 'unique_id should not contain any spaces';
}
return true;
}
},
{
type: 'input',
name: 'installPath',
message: 'Please enter the path to install the application:',
default: (answers) => path.join(currentPath, answers.projectName || 'defaultProjectName'),
},
{
type: 'list',
name: 'template',
message: 'Please select a template for your application:',
choices: templates,
},
{
type: 'input',
name: 'agentDescription',
message: 'Please enter a description for your agent:',
default: 'My Codebolt Agent',
},
{
type: 'input',
name: 'tags',
message: 'Please Enter agent tags by comma separated:',
default: 'test',
},
{
type: 'confirm',
name: 'worksonblankcode',
message: 'Works on blank code:',
default: parsedYaml.metadata.agent_routing.worksonblankcode,
},
{
type: 'confirm',
name: 'worksonexistingcode',
message: 'Works on existing code:',
default: parsedYaml.metadata.agent_routing.worksonexistingcode,
},
{
type: 'checkbox',
name: 'supportedlanguages',
message: 'Supported Languages:',
choices: parsedYaml.metadata.agent_routing.supportedlanguages,
validate: function (input) {
if (input.length === 0) {
return 'You must select at least one language';
}
return true;
}
},
{
type: 'checkbox',
name: 'supportedframeworks',
message: 'Supported Frameworks:',
choices: parsedYaml.metadata.agent_routing.supportedframeworks,
validate: function (input) {
if (input.length === 0) {
return 'You must select at least one framework';
}
return true;
}
},
];
async function askForActions(actionsData) {
const initialPrompt = [
{
type: 'confirm',
name: 'addActions',
message: 'Do you want to add actions?',
default: true,
}
];
const initialRes = await inquirer.prompt(initialPrompt);
let addMoreActions = initialRes.addActions;
while (addMoreActions) {
const actionPrompt = [
{
type: 'input',
name: 'actionName',
message: 'Please Enter Action Name:',
default: parsedYaml.actions[0].name
},
{
type: 'input',
name: 'description',
message: 'Please Enter Action Description:',
default: parsedYaml.actions[0].description
},
{
type: 'input',
name: 'detailDescription',
message: 'Please Enter Detail Description (optional):',
default: parsedYaml.actions[0].detailDescription,
},
{
type: 'input',
name: 'actionPrompt',
message: 'Please Enter Action Prompt (optional):',
default: parsedYaml.actions[0].actionPrompt,
},
{
type: 'confirm',
name: 'addMoreActions',
message: 'Do you want to add more actions?',
default: false,
}
];
const actionRes = await inquirer.prompt(actionPrompt);
actionsData.push({
name: actionRes.actionName,
description: actionRes.description,
detailDescription: actionRes.detailDescription,
actionPrompt: actionRes.actionPrompt,
});
addMoreActions = actionRes.addMoreActions;
}
return actionsData;
}
async function askForInstructions(sdlc) {
let addMoreInstructions = true;
while (addMoreInstructions) {
// Prompt for SDLC step name
const stepPrompt = [
{
type: 'list',
name: 'name',
message: 'Please Enter SDLC Step Name:',
choices: parsedYaml.metadata.sdlc_steps_managed.map(item => item.name),
}
];
const stepRes = await inquirer.prompt(stepPrompt);
let instructions = [];
// Prompt for multiple instructions
let addMoreExamples = true;
while (addMoreExamples) {
const instructionPrompt = [
{
type: 'input',
name: 'example_instruction',
message: 'Please Enter Instruction Description:',
default: 'Generate a new React component',
},
{
type: 'confirm',
name: 'addMoreExamples',
message: 'Do you want to add another instruction?',
default: false,
}
];
const instructionRes = await inquirer.prompt(instructionPrompt);
instructions.push(instructionRes.example_instruction);
addMoreExamples = instructionRes.addMoreExamples;
}
sdlc.push({
name: stepRes.name,
example_instructions: instructions,
});
const addMoreStepsPrompt = [
{
type: 'confirm',
name: 'addMoreInstructions',
message: 'Do you want to add more SDLC steps?',
default: false,
}
];
const addMoreStepsRes = await inquirer.prompt(addMoreStepsPrompt);
addMoreInstructions = addMoreStepsRes.addMoreInstructions;
}
return sdlc;
}
console.log(chalk.blue(
" _____ _ _ _ _ \n"+
" / __ \\ | | | | | | | \n"+
" | / \\/ ___ __| | ___| |__ ___ | | |_ \n"+
" | | / _ \\ / _` |/ _ \\ '_ \\ / _ \\| | __| \n"+
" | \\__/\\ (_) | (_| | __/ |_) | (_) | | |_ \n"+
" \\____/\\___/ \\__,_|\\___|_.__/ \\___/|_|\\__| \n"));
inquirer.prompt(prompts).then(async answers => {
let sdlc = [];
let actionsData = [];
console.log(chalk.yellow(`\n------------------------------SDLC Steps-----------------------------------------\n`));
console.log(chalk.green("SDLC Steps are the software development steps that your Agent will handle like code generation, deployment, testing etc. \n These are used by Universal Agent Router to route the user request to the correct Agent Invocation.\n Also add sample Instructions that the user should give to invoke that SLDC Step functionality.\n"));
let sdlcInstruction = await askForInstructions(sdlc)
console.log(chalk.yellow(`\n------------------------------Action Steps-----------------------------------------\n`));
console.log(chalk.green("Actions are the functionality that your Agent provides to the user. These are like functionality shortcuts that the user can invoke using \\ command.\n"));
let actions = await askForActions(actionsData)
projectName = answers.projectName.trim();
const installPath = answers.installPath.trim() === '.' ? process.cwd() : path.resolve(process.cwd(), answers.installPath.trim());
const selectedTemplate = answers.template;
answers.sdlc_steps_managed = sdlcInstruction
answers.actions = actions
createProject(projectName, installPath, selectedTemplate, answers);
});
function createProject(projectName, installPath, selectedTemplate, answers ) {
const projectDir = path.resolve(installPath);
fs.mkdirSync(projectDir, { recursive: true });
// Copy the selected template to the project directory
const templatePath = path.join(templateDir, selectedTemplate);
fs.cpSync(templatePath, projectDir, { recursive: true });
fs.renameSync(
path.join(projectDir, 'gitignore'),
path.join(projectDir, '.gitignore')
);
const agentYamlPath = path.join(projectDir, 'codeboltagent.yaml');
let agentYaml = fs.readFileSync(agentYamlPath, 'utf8');
let agentYamlObj = yaml.load(agentYaml);
agentYamlObj.title = projectName;
agentYamlObj.description = answers.agentDescription;
agentYamlObj.tags = answers.tags.split(',').map(tag => tag.trim());
agentYamlObj.unique_id = answers.unique_id
agentYamlObj.metadata.agent_routing = {
worksonblankcode: answers.worksonblankcode,
worksonexistingcode: answers.worksonexistingcode,
supportedlanguages: answers.supportedlanguages,
supportedframeworks: answers.supportedframeworks,
};
agentYamlObj.metadata.sdlc_steps_managed = answers.sdlc_steps_managed.map(step => ({
name: step.name,
example_instructions: step.example_instructions,
}));
agentYamlObj.actions = parsedYaml.actions.map(action => ({
name: action.name,
description: action.description,
detailDescription: action.detailDescription,
actionPrompt: action.actionPrompt,
}));
agentYaml = yaml.dump(agentYamlObj);
fs.writeFileSync(agentYamlPath, agentYaml, 'utf8');
const projectPackageJson = require(path.join(projectDir, 'package.json'));
// Update the project's package.json with the new project name
projectPackageJson.name = projectName;
fs.writeFileSync(
path.join(projectDir, 'package.json'),
JSON.stringify(projectPackageJson, null, 2)
);
// Run `npm install` in the project directory to install
// the dependencies. We are using a third-party library
// called `cross-spawn` for cross-platform support.
// (Node has issues spawning child processes in Windows).
spawn.sync('npm', ['install'], { stdio: 'inherit', cwd: installPath });
spawn.sync('git', ['init'], { stdio: 'inherit', cwd: installPath });
console.log('Success! Your new project is ready.');
console.log(`Created ${projectName} at ${projectDir}`);
}