ai-expert
Version:
AI Expert CLI - Advanced management system for specialized Claude assistants
80 lines • 3.82 kB
JavaScript
import { ExpertBuilder } from '../../services/expert-builder.js';
import { execSync, spawn } from 'child_process';
import chalk from 'chalk';
export function queryCommand(storage) {
const builder = new ExpertBuilder(storage);
return {
async execute(expertSlug, question, options = {}) {
try {
const expert = await storage.getExpert(expertSlug);
if (!expert) {
console.error(chalk.red(`❌ Expert '${expertSlug}' not found`));
// Show available experts
const experts = await storage.listExperts();
if (experts.length > 0) {
console.log(chalk.yellow('\nAvailable experts:'));
for (const e of experts.slice(0, 5)) {
console.log(` - ${chalk.cyan(e.slug)}: ${e.description}`);
}
}
return;
}
// Update usage stats
expert.usageCount = (expert.usageCount || 0) + 1;
expert.lastUsed = new Date().toISOString();
await storage.saveExpert(expert);
// Build the full prompt
const systemPrompt = await builder.buildExpertPrompt(expert);
// Interactive mode
if (options.interactive || !question) {
console.log(chalk.cyan(`🤖 Starting session with ${expert.name}`));
console.log(chalk.gray('💡 Interactive mode - Type your questions'));
console.log(chalk.gray('─'.repeat(60)) + '\n');
const initialMessage = `EXPERT: ${expert.name}\n\n${systemPrompt}\n\n${'─'.repeat(60)}\n\nI'm ready to help you with ${expert.description}.`;
if (question) {
initialMessage + `\n\nQUESTION: ${question}`;
}
// Start Claude in interactive mode
const claude = spawn('claude', [], {
stdio: ['pipe', 'inherit', 'inherit']
});
claude.stdin.write(initialMessage);
claude.stdin.end();
await new Promise((resolve, reject) => {
claude.on('close', resolve);
claude.on('error', reject);
});
return;
}
// Non-interactive mode
const fullQuery = `${systemPrompt}\n\nUSER: ${question}`;
try {
// Escape the query for shell
const escapedQuery = fullQuery
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\$/g, '\\$')
.replace(/`/g, '\\`');
const result = execSync(`claude --print "${escapedQuery}"`, {
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024 // 10MB buffer
});
console.log(result.trim());
}
catch (error) {
if (error.code === 'ENOENT') {
console.error(chalk.red('❌ Claude CLI not found.'));
console.error(chalk.yellow('Please install Claude CLI: https://claude.ai/code'));
}
else {
console.error(chalk.red('❌ Error executing Claude:'), error.message);
}
}
}
catch (error) {
console.error(chalk.red('❌ Error:'), error.message);
}
}
};
}
//# sourceMappingURL=query.js.map