-
Notifications
You must be signed in to change notification settings - Fork 4
/
aic.js
executable file
·101 lines (82 loc) · 2.42 KB
/
aic.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
#!/usr/bin/env node
import inquirer from "inquirer";
import ora from "ora";
import gradient from "gradient-string";
import AIClient from "./lib/aiClient/aiClient.js";
import Logger from "./lib/logger.js";
import { getConfig, getCommand } from "./lib/cli.js";
import { marked } from "marked";
import { markedTerminal } from "marked-terminal";
const logger = new Logger("chat");
function markdownToConsole(text) {
let terminalHighlightedCode = "";
try {
marked.use(markedTerminal());
terminalHighlightedCode = marked(text); //console.log(terminalHighlightedCode);
} catch (error) {
console.error("An error occurred while highlighting code:", error);
}
return terminalHighlightedCode;
}
async function askQuestion(question, client) {
const spinner = ora(gradient.cristal("Thinking...")).start();
try {
const response = await client.generateResponse(question);
spinner.succeed(gradient.cristal("Ready."));
console.log(markdownToConsole(response.trim()));
} catch (error) {
spinner.fail(gradient.cristal("Failed to get response."));
logger.error({ error: error.message }, "Error occurred.");
}
}
async function chatPrompt() {
const { result } = await inquirer.prompt([
{
type: "input",
name: "result",
message: ">",
},
]);
return result;
}
async function startConversation(client, initialQuestion) {
let continueConversation = true;
let question = initialQuestion;
while (continueConversation) {
if (!question) {
question = await chatPrompt();
}
if (shouldExitConversation(question)) {
continueConversation = false;
break;
}
await askQuestion(question, client);
question = null;
}
console.log(gradient.morning("Goodbye!"));
process.exit(0);
}
function shouldExitConversation(question) {
const exitWords = new Set(["exit", "goodbye", "quit"]);
return !question || exitWords.has(question.toLowerCase().trim());
}
async function main(startConvo = true) {
try {
const command = await getCommand();
const config = await getConfig(command);
const client = new AIClient(config);
if (startConvo) {
await startConversation(client, command);
} else {
await askQuestion(command, client);
}
process.exit(0);
} catch (error) {
logger.error(`An error occurred: ${error.message}`);
process.exit(1);
}
}
if (!`file://${process.argv[1]}`.includes('!')) {
main();
}
export default main;