xutilities-cli
Version:
A Command-Line Interface Tool For xUtilities (An Unblocked Games, Apps, & More Website Used When Bored At Work Or School) Used In-Case xUtilities On The Web Is Blocked/Banned.
76 lines (65 loc) • 2.25 kB
JavaScript
import axios from 'axios';
import inquirer from 'inquirer';
import chalk from 'chalk';
// Function to clear the terminal and scrollback buffer
const clearTerminal = () => {
process.stdout.write('\x1B[2J\x1B[3J\x1B[H'); // Clear terminal and scrollback buffer
};
// Function to display the chat interface
const displayChat = (chatHistory) => {
clearTerminal();
console.log(chalk.green.bold('\nHi, I\'m Cognia! How may I help you?\n'));
console.log(chalk.yellow('Type "exit" or "quit" to return to the main menu.\n'));
// Display chat history
chatHistory.forEach((msg) => {
if (msg.sender === 'user') {
console.log(chalk.green(`You: ${msg.text}`));
} else {
console.log(chalk.blue(`Cognia: ${msg.text}`));
}
});
};
export const startConversation = async () => {
let userId = Math.random().toString(36).substring(2, 15);
let chatHistory = [];
// Initial display
displayChat(chatHistory);
while (true) {
const { message } = await inquirer.prompt([
{
type: 'input',
name: 'message',
message: 'Type your message:',
prefix: chalk.yellow('?'), // Add the "?" prefix
validate: (input) => {
if (input.trim() === '') {
return 'Message cannot be empty!';
}
return true;
},
},
]);
// Exit command
if (message.toLowerCase() === 'exit' || message.toLowerCase() === 'quit') {
clearTerminal();
console.log(chalk.yellow('Returning to the main menu...\n'));
break;
}
// Add user message to chat history IMMEDIATELY
chatHistory.push({ sender: 'user', text: message });
displayChat(chatHistory); // Redisplay chat with the user's message
try {
// Send message to the chatbot API
const response = await axios.post('https://entire-sheelah-xutilities-ae6f96d7.koyeb.app/api/chat', {
message,
userId,
});
const botResponse = response.data.response;
// Add bot response to chat history
chatHistory.push({ sender: 'bot', text: botResponse });
displayChat(chatHistory); // Redisplay chat with the bot's response
} catch (error) {
console.error(chalk.red('Error:', error.message));
}
}
};