-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.cjs
134 lines (118 loc) · 4.29 KB
/
cli.cjs
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
#!/usr/bin/env node
const { RsnChat } = require("./build/cjs/index.cjs");
const colors = require("colors");
const readline = require("readline");
const axios = require("axios");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const asciiArt = `
##
#### #### ## ##
## # #### # ## ### # ## # ### #####
## # ## ## ## ## ##### ## ##
#### #### ## ## ## ## ## #### ##
## # #### ## ## ## ## ## ## ## ##
## # ## ## ## ### ## ## ## ## ##
## # #### ## ## #### ## ## ## # ###
`;
async function fetchActiveModels() {
try {
const response = await axios.get("https://api.rnilaweera.lk/api/models");
if (response.status === 200) {
return response.data.chat_models
.filter((model) => model.status === "active")
.map((model) => model.name.toUpperCase());
} else {
console.error(colors.red("Failed to fetch models. Using defaults."));
return ["GPT", "GEMINI", "GROK-2", "GROK-2-MINI"];
}
} catch (error) {
console.error(colors.red("Error fetching models:"), error.message);
return ["GPT", "GEMINI", "GROK-2", "GROK-2-MINI"];
}
}
function promptApiKey() {
console.log(colors.bold.green(asciiArt));
rl.question(colors.bold.green("Enter your API key: "), async (apiKey) => {
try {
const response = await axios.post(
"https://api.rnilaweera.lk/api/auth/validate",
{ apikey: apiKey }
);
if (response.status === 200) {
const username = response.data.name;
const rsnChat = new RsnChat(apiKey);
startChat(rsnChat, username);
} else {
handleErrorResponse(response.status);
}
} catch (error) {
if (error.response && error.response.status) {
handleErrorResponse(error.response.status);
} else {
console.error(colors.red("Error validating API key:"), error.message);
promptApiKey();
}
}
});
}
function handleErrorResponse(status) {
if (status === 401) {
console.error(colors.red("Invalid API key. Please try again."));
} else if (status === 503) {
console.error(colors.red("Service Unavailable - Maintain Mode"));
} else {
console.error(
colors.red(`Unexpected response from the server. Status: ${status}. Please try again.`)
);
}
promptApiKey();
}
async function startChat(rsnChat, username) {
console.log(colors.bold.yellow(`\nWelcome to ${username}'s RsnChat CLI!\n`));
const availableModels = await fetchActiveModels();
function promptChatMethod() {
console.log(colors.cyan(`Available chat models: ${availableModels.join(", ")}`));
rl.question(colors.cyan('Select a chat model or type "exit" to quit: '), (method) => {
method = method.toUpperCase().trim();
if (method === "EXIT") {
console.log(colors.yellow("\nExiting RsnChat CLI. Goodbye!\n"));
rl.close();
} else if (availableModels.includes(method)) {
promptUser(method, rsnChat);
} else {
console.error(colors.red("Invalid selection. Try again."));
promptChatMethod();
}
});
}
function promptUser(method, rsnChat) {
rl.question(colors.green(`Enter your ${method} prompt (or type "exit" to quit): `), async (prompt) => {
if (prompt.toLowerCase().trim() === "exit") {
console.log(colors.yellow("\nExiting RsnChat CLI. Goodbye!\n"));
rl.close();
} else {
try {
const response = await rsnChat.chat(prompt, method.toLowerCase());
console.log(colors.bold.blue(`${method} Response:`), response.message);
} catch (error) {
console.error(colors.red(`Error calling ${method}:`), error.message);
}
rl.question(colors.cyan(`Do you want to select another model or continue chatting with "${method}"? (type "exit" to quit, or "another" for a new model): `), (choice) => {
if (choice.toLowerCase().trim() === "exit") {
console.log(colors.yellow("\nExiting RsnChat CLI. Goodbye!\n"));
rl.close();
} else if (choice.toLowerCase().trim() === "another") {
promptChatMethod();
} else {
promptUser(method, rsnChat);
}
});
}
});
}
promptChatMethod();
}
promptApiKey();