ai-persona-hub
Version:
AI Profile CLI - Create custom AI profiles run against dynamic LLM providers
153 lines • 7.52 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.chatCommand = chatCommand;
const inquirer_1 = __importDefault(require("inquirer"));
const chalk_1 = __importDefault(require("chalk"));
const profile_manager_1 = require("../services/profile-manager");
const config_1 = require("../utils/config");
const ai_client_1 = require("../services/ai-client");
const chat_history_manager_1 = require("../services/chat-history-manager");
const chat_input_handler_1 = require("../services/chat-input-handler");
async function chatCommand(profileName) {
let selectedProfileName = profileName;
if (!selectedProfileName) {
const profileManager = new profile_manager_1.ProfileManager();
const profiles = await profileManager.listProfiles();
if (profiles.length === 0) {
console.log(chalk_1.default.yellow('No profiles found. Create one with:'));
console.log(chalk_1.default.white('cgem create'));
process.exit(0);
}
if (profiles.length === 1) {
selectedProfileName = profiles[0].id;
console.log(chalk_1.default.blue(`Using profile: ${profiles[0].name}`));
}
else {
const choices = profiles.map(profile => {
const lastUsed = profile.lastUsed
? `Last used: ${new Date(profile.lastUsed).toLocaleDateString()}`
: 'Never used';
const promptPreview = profile.systemPrompt.slice(0, 60);
const preview = profile.systemPrompt.length > 60
? `${promptPreview}...`
: promptPreview;
return {
name: `${profile.name} - ${lastUsed}\n ${chalk_1.default.gray(preview)}`,
value: profile.id,
short: profile.name,
};
});
const { selectedProfile } = await inquirer_1.default.prompt([
{
type: 'list',
name: 'selectedProfile',
message: 'Select a profile to chat with:',
choices,
pageSize: 10,
},
]);
selectedProfileName = selectedProfile;
}
}
try {
const profileManager = new profile_manager_1.ProfileManager();
const configManager = new config_1.ConfigManager();
if (!selectedProfileName) {
console.error(chalk_1.default.red('❌ No profile selected'));
process.exit(1);
}
const profile = await profileManager.getProfile(selectedProfileName);
if (!profile) {
console.error(chalk_1.default.red(`❌ Profile '${selectedProfileName}' not found`));
console.log(chalk_1.default.gray('List available profiles with: cgem list'));
process.exit(1);
}
const currentProvider = configManager.getCurrentProvider();
const currentModel = configManager.getCurrentModel();
if (!currentProvider || !currentModel) {
console.error(chalk_1.default.red('❌ No AI model configured'));
console.log(chalk_1.default.gray('Configure a model first with: cgem model'));
process.exit(1);
}
const apiKey = configManager.getProviderApiKey(currentProvider);
if (!apiKey) {
console.error(chalk_1.default.red(`❌ ${currentProvider} API key not found`));
console.log(chalk_1.default.gray(`Set your API key with environment variable or in ~/.cgem/config.json:`));
switch (currentProvider) {
case 'openai':
console.log(chalk_1.default.gray('Environment: OPENAI_API_KEY'));
break;
case 'anthropic':
console.log(chalk_1.default.gray('Environment: ANTHROPIC_API_KEY'));
break;
case 'google':
console.log(chalk_1.default.gray('Environment: GOOGLE_GENERATIVE_AI_API_KEY'));
break;
}
process.exit(1);
}
const aiClient = new ai_client_1.AIClient({
provider: currentProvider,
model: currentModel,
maxTokens: profile.maxTokens,
}, profile, apiKey);
// Initialize chat history manager and load history
const chatHistoryManager = new chat_history_manager_1.ChatHistoryManager();
const chatHistory = await chatHistoryManager.loadChatHistory(profile.id);
// Initialize input handler with history
const inputHandler = new chat_input_handler_1.ChatInputHandler(chatHistory.userMessages);
await profileManager.updateLastUsed(selectedProfileName);
console.log(chalk_1.default.blue(`\n💬 Starting conversation with ${chalk_1.default.white(profile.name)}`));
console.log(chalk_1.default.gray(`Provider: ${currentProvider}`));
console.log(chalk_1.default.gray(`Model: ${currentModel}`));
console.log(chalk_1.default.gray('Type "exit" or "quit" to end the conversation'));
console.log(chalk_1.default.gray('Use ↑ and ↓ arrows to navigate through previous messages\n'));
try {
while (true) {
const userMessage = await inputHandler.promptForInput();
if (!userMessage.trim()) {
console.log(chalk_1.default.yellow('Please enter a message'));
continue;
}
if (userMessage.toLowerCase() === 'exit' ||
userMessage.toLowerCase() === 'quit') {
console.log(chalk_1.default.gray('\nGoodbye! 👋'));
break;
}
// Save the user message to history
await chatHistoryManager.addUserMessage(profile.id, userMessage.trim());
try {
console.log(chalk_1.default.blue('AI:'), chalk_1.default.gray('thinking...\n'));
const response = await aiClient.sendMessage(userMessage, (chunk) => {
process.stdout.write(chunk);
});
if (!response) {
console.log(chalk_1.default.red('\n❌ No response from AI'));
continue;
}
console.log('\n');
}
catch (error) {
console.error(chalk_1.default.red('\n❌ Error getting AI response:'));
console.error(chalk_1.default.red(error instanceof Error ? error.message : 'Unknown error'));
console.log(chalk_1.default.gray('You can continue the conversation or type "exit" to quit.\n'));
}
}
}
finally {
// Clean up input handler
inputHandler.close();
// Note: Conversation history is now automatically managed by agent memory
// The ChatHistoryManager still tracks user input history for the input handler
}
}
catch (error) {
console.error(chalk_1.default.red('❌ Error starting chat:'));
console.error(chalk_1.default.red(error instanceof Error ? error.message : 'Unknown error'));
process.exit(1);
}
}
//# sourceMappingURL=chat.js.map