-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (169 loc) · 5.54 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
#!/usr/bin/env node
import { program } from "commander";
import { GoogleGenerativeAI } from "@google/generative-ai";
import readline from "readline";
import { marked } from "marked";
import chalk from "chalk";
import ora from "ora";
import fs from "fs";
import os from "os";
import path from "path";
// Define the config file path
const configPath = path.join(os.homedir(), ".asktocmd-config.json");
function saveAPIKey(apiKey) {
const config = { GEMINI_API_KEY: apiKey };
fs.writeFileSync(configPath, JSON.stringify(config));
console.log(chalk.green("API key saved successfully!"));
process.exit(0);
}
function getAPIKey() {
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath));
return config.GEMINI_API_KEY;
} else {
return null;
}
}
// Initialize Gemini API with the stored API key
function initGenAI() {
const apiKey = getAPIKey();
if (!apiKey) {
console.log(
chalk.red(
"No API key found. Please set it using 'asktocmd set-api-key <API-KEY>'.Get your api-key from https://aistudio.google.com/app/apikey"
)
);
process.exit(1);
}
return new GoogleGenerativeAI(apiKey);
}
// Move genAI initialization to when it's needed
let genAI;
const SYSTEM_PROMPT = `You are an AI assistant specifically designed to help with command-line interface (CLI) commands and operations. You should:
1. Provide clear, accurate explanations of CLI commands and their usage
2. Suggest appropriate commands based on user needs
3. Explain command options and flags
4. Help troubleshoot common CLI issues
5. Share best practices for command-line operations
6. Format output to be easily readable in a terminal
Only provide assistance related to command-line interfaces and operations. For non-CLI questions, inform users that you can only help with CLI-related topics.`;
function formatMarkdown(markdown) {
const tokens = marked.lexer(markdown);
let formattedText = "";
for (const token of tokens) {
switch (token.type) {
case "paragraph":
formattedText += token.text + "\n\n";
break;
case "heading":
formattedText +=
chalk.bold(chalk.blue("#".repeat(token.depth) + " " + token.text)) +
"\n\n";
break;
case "list":
token.items.forEach((item, index) => {
formattedText += chalk.yellow(`${index + 1}. `) + item.text + "\n";
});
formattedText += "\n";
break;
case "code":
formattedText += chalk.blue("\n" + token.text + "\n") + "\n";
break;
case "blockquote":
formattedText += chalk.green("> " + token.text) + "\n\n";
break;
case "hr":
formattedText += chalk.gray("---") + "\n\n";
break;
case "space":
formattedText += "\n";
break;
}
}
formattedText = formattedText.replace(/\*\*(.*?)\*\*/g, chalk.bold("$1"));
formattedText = formattedText.replace(/\*(.*?)\*/g, chalk.italic("$1"));
formattedText = formattedText.replace(/`(.*?)`/g, chalk.cyan("$1"));
return formattedText.trim();
}
async function askAI(query) {
const spinner = ora("Thinking...").start();
try {
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const prompt = `${SYSTEM_PROMPT}\n\nUser query: ${query}\n\nResponse:`;
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text().trim();
spinner.stop();
if (
text
.toLowerCase()
.includes("askcmd can only assist with command-line related questions")
) {
return chalk.red(
"I'm sorry, but I can only provide information about command-line interfaces and operations. Could you please ask a CLI-related question?"
);
}
const formattedText = formatMarkdown(text);
return formattedText;
} catch (error) {
spinner.stop();
console.error(chalk.red("Error:"), error);
return chalk.red(
"Sorry, I encountered an error while processing your request."
);
}
}
// Command to save the API key
program
.command("set-api-key <apiKey>")
.description("Save your Gemini API key")
.action((apiKey) => {
saveAPIKey(apiKey);
});
program
.version("1.0.7")
.description(chalk.bold("AskToCmd AI CLI Helper"))
.option("-i, --interactive", "Run in interactive mode")
.argument("[query]", "Query for the AI assistant")
.action(async (query, options) => {
if (options.interactive) {
console.log(
chalk.green.bold(
"Welcome to Command-Focused AI CLI Helper! Type 'exit' to quit."
)
);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const askQuestion = () => {
rl.question(chalk.yellow("Ask about a command: "), async (input) => {
if (input.toLowerCase() === "exit") {
console.log(
chalk.green("Thank you for using Command-Focused AI CLI Helper!")
);
rl.close();
return;
}
// Initialize genAI only when needed
if (!genAI) {
genAI = initGenAI();
}
const response = await askAI(input);
console.log("\n" + chalk.cyan("AI Assistant:"), response, "\n");
askQuestion();
});
};
askQuestion();
} else if (query) {
// Initialize genAI only when needed
if (!genAI) {
genAI = initGenAI();
}
const response = await askAI(query);
console.log(response);
} else {
program.help();
}
});
program.parse(process.argv);