@devangkumar/dvai 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +37 -93
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { GoogleGenerativeAI } from "@google/generative-ai";
|
|
2
2
|
import { Command } from "commander";
|
|
3
|
-
import chalk from "chalk";
|
|
4
3
|
import ora from "ora";
|
|
5
4
|
import inquirer from "inquirer";
|
|
6
5
|
|
|
@@ -11,110 +10,55 @@ const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
|
|
|
11
10
|
|
|
12
11
|
const program = new Command();
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
async function startChat(initialHistory = []) {
|
|
14
|
+
let chatHistory = initialHistory;
|
|
15
|
+
while (true) {
|
|
16
|
+
const { userInput } = await inquirer.prompt([{
|
|
17
|
+
type: "input",
|
|
18
|
+
name: "userInput",
|
|
19
|
+
message: ">",
|
|
20
|
+
}]);
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*/
|
|
22
|
-
async function getAIResponse(prompt, chatHistory = []) {
|
|
23
|
-
const spinner = ora({
|
|
24
|
-
text: chalk.cyan("AI is thinking..."),
|
|
25
|
-
color: "cyan",
|
|
26
|
-
spinner: "dots"
|
|
27
|
-
}).start();
|
|
22
|
+
const cmd = userInput.toLowerCase();
|
|
23
|
+
if (cmd === "exit" || cmd === "quit" || cmd === "bye") break;
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
25
|
+
const spinner = ora({ text: "", isSilent: true }).start();
|
|
26
|
+
try {
|
|
27
|
+
const chat = model.startChat({ history: chatHistory });
|
|
28
|
+
const result = await chat.sendMessage(userInput);
|
|
29
|
+
spinner.stop();
|
|
30
|
+
const response = result.response.text();
|
|
31
|
+
process.stdout.write(response + "\n\n");
|
|
32
|
+
chatHistory.push({ role: "user", parts: [{ text: userInput }] });
|
|
33
|
+
chatHistory.push({ role: "model", parts: [{ text: response }] });
|
|
34
|
+
} catch (error) {
|
|
35
|
+
spinner.stop();
|
|
36
|
+
console.error("Error:", error.message);
|
|
37
|
+
}
|
|
41
38
|
}
|
|
42
|
-
} catch (error) {
|
|
43
|
-
spinner.stop();
|
|
44
|
-
console.error(chalk.red("\n✖ AI Error:"), error.message);
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
39
|
}
|
|
48
40
|
|
|
49
|
-
/**
|
|
50
|
-
* Handle Single Question
|
|
51
|
-
*/
|
|
52
41
|
program
|
|
53
|
-
.
|
|
42
|
+
.name("myai")
|
|
43
|
+
.version("1.0.3")
|
|
44
|
+
.argument("[prompt]", "Prompt to send to AI")
|
|
54
45
|
.action(async (prompt) => {
|
|
46
|
+
// If no prompt, start chat immediately
|
|
55
47
|
if (!prompt) {
|
|
56
|
-
|
|
48
|
+
await startChat();
|
|
57
49
|
return;
|
|
58
50
|
}
|
|
59
51
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
* Handle Interactive Chat
|
|
70
|
-
*/
|
|
71
|
-
program
|
|
72
|
-
.command("chat")
|
|
73
|
-
.description("Start an interactive chat session")
|
|
74
|
-
.action(async () => {
|
|
75
|
-
console.clear();
|
|
76
|
-
console.log(chalk.cyan.bold("╔════════════════════════════════════════════╗"));
|
|
77
|
-
console.log(chalk.cyan.bold("║ WELCOME TO MYAI TERMINAL ║"));
|
|
78
|
-
console.log(chalk.cyan.bold("╚════════════════════════════════════════════╝"));
|
|
79
|
-
console.log(chalk.dim("Type 'exit' or 'quit' to end the session.\n"));
|
|
80
|
-
|
|
81
|
-
let chatHistory = [];
|
|
82
|
-
|
|
83
|
-
while (true) {
|
|
84
|
-
const { userInput } = await inquirer.prompt([
|
|
85
|
-
{
|
|
86
|
-
type: "input",
|
|
87
|
-
name: "userInput",
|
|
88
|
-
message: chalk.green("? You:"),
|
|
89
|
-
validate: (input) => input.trim() !== "" || "Please enter a prompt."
|
|
90
|
-
}
|
|
91
|
-
]);
|
|
92
|
-
|
|
93
|
-
if (userInput.toLowerCase() === "exit" || userInput.toLowerCase() === "quit") {
|
|
94
|
-
console.log(chalk.yellow("\nGoodbye! 👋\n"));
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const response = await getAIResponse(userInput, chatHistory);
|
|
99
|
-
|
|
100
|
-
if (response) {
|
|
101
|
-
console.log(chalk.blue.bold("\nAI:"));
|
|
102
|
-
console.log(chalk.white(response));
|
|
103
|
-
console.log("\n");
|
|
104
|
-
|
|
105
|
-
// Update history
|
|
106
|
-
chatHistory.push({ role: "user", parts: [{ text: userInput }] });
|
|
107
|
-
chatHistory.push({ role: "model", parts: [{ text: response }] });
|
|
108
|
-
}
|
|
52
|
+
// Otherwise, handle single question
|
|
53
|
+
const spinner = ora({ text: "", isSilent: true }).start();
|
|
54
|
+
try {
|
|
55
|
+
const result = await model.generateContent(prompt);
|
|
56
|
+
spinner.stop();
|
|
57
|
+
process.stdout.write(result.response.text() + "\n");
|
|
58
|
+
} catch (error) {
|
|
59
|
+
spinner.stop();
|
|
60
|
+
console.error("Error:", error.message);
|
|
109
61
|
}
|
|
110
62
|
});
|
|
111
63
|
|
|
112
|
-
function displayWelcome() {
|
|
113
|
-
console.log(chalk.cyan.bold("\n✨ MyAI Terminal Assistant ✨"));
|
|
114
|
-
console.log(chalk.white("Usage:"));
|
|
115
|
-
console.log(chalk.dim(" myai \"your question\" ") + chalk.white("- Ask a quick question"));
|
|
116
|
-
console.log(chalk.dim(" myai chat ") + chalk.white("- Start interactive session"));
|
|
117
|
-
console.log(chalk.dim(" myai --help ") + chalk.white("- Show help\n"));
|
|
118
|
-
}
|
|
119
|
-
|
|
120
64
|
program.parse();
|