-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathai.js
executable file
·155 lines (131 loc) · 3.84 KB
/
ai.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
#!/usr/bin/env node
import inquirer from "inquirer";
import ora from "ora";
import gradient from "gradient-string";
import JSONScript from "jsonscriptlib";
import AIClient from "./lib/aiClient/aiClient.js";
import Logger from "./lib/logger.js";
import { getCommand, getConfig, cliCommands } from "./lib/cli.js";
const logger = new Logger("command");
async function executeScript(script) {
try {
const result = await script.execute();
if (result.error) {
logger.error({ error: result.error }, "Execution Error");
} else {
result.results.forEach((res) => {
if (res.type === "cmd") {
//console.log(res.result);
} else if (res.type === "file") {
console.log(`File ${res.result}`);
}
});
}
} catch (error) {
console.log(error);
}
}
async function generateScript(command, config) {
const spinner = ora(gradient.cristal("Thinking...")).start();
const client = new AIClient(config);
const jsonScript = await client.generateScript(command);
spinner.succeed(gradient.cristal("Ready."));
console.log();
logger.info({ jsonScript: JSON.parse(jsonScript) }, "JSON Response logged");
return jsonScript;
}
function displayExecutionDescription(script) {
console.log(gradient.cristal("Execution Description:"));
console.log(
script.executionDescription
.map((line) => gradient.teen(line.trim()))
.join("\n"),
);
console.log();
}
function displayExecutionPlan(script) {
console.log(gradient.cristal("Execution Plan:"));
script.executionPlan.forEach((line) => {
line = line.trim();
if (line.startsWith("Create file:")) {
const coloredLine = line.replace(/^(Create file:)/, "");
console.log(
gradient.passion("Create file:") + gradient.teen(coloredLine),
);
} else {
console.log(gradient.teen(line));
}
});
console.log();
}
function displayExecutionDetails(script, config) {
if (config.showExecutionDescription) {
displayExecutionDescription(script);
}
if (config.showExecutionPlan) {
displayExecutionPlan(script);
}
}
async function promptUser() {
const { proceedOption } = await inquirer.prompt([
{
type: "list",
name: "proceedOption",
message: "Do you want to proceed with the execution?",
choices: [
{ name: "Yes", value: "yes" },
{ name: "No", value: "no" },
{ name: "New Solution", value: "newSolution" },
],
},
]);
return proceedOption;
}
async function executeWithRetries(
command,
config,
continuePrompt,
maxRetries = 3,
) {
let retryCount = 0;
while (retryCount <= maxRetries) {
try {
await executeScriptFlow(command, config, continuePrompt);
} catch (error) {
handleError(error, retryCount, maxRetries);
retryCount += 1;
}
}
}
async function executeScriptFlow(command, config, continuePrompt) {
const jsonScript = await generateScript(command, config);
const script = new JSONScript(JSON.parse(jsonScript));
displayExecutionDetails(script, config);
const shouldPromptUser = continuePrompt && command[0] !== "!";
const proceedOption = shouldPromptUser ? await promptUser() : "yes";
if (proceedOption === "yes") {
await executeScript(script);
process.exit();
} else if (proceedOption === "no") {
logger.info("Execution aborted by user.");
process.exit();
}
}
function handleError(error, retryCount, maxRetries) {
logger.error(`An error occurred: ${error.message}`);
if (retryCount >= maxRetries) {
logger.error("Max retries reached. Aborting execution.");
process.exit(1);
}
}
async function main(continuePrompt = true) {
const command = await getCommand();
if(await cliCommands(command)) {
const config = await getConfig(command);
await executeWithRetries(command, config, continuePrompt);
}
}
if (!`file://${process.argv[1]}`.includes('!')) {
main();
}
export default main;