sophia-code
Version:
Production-ready agentic CLI code editor with AI-powered coding assistance, planning, and multi-agent delegation. Enterprise-grade security and reliability.
135 lines (115 loc) • 4.26 kB
JavaScript
/**
* Sophia CLI - AI Coding Assistant
* Entry point for the sophia command
*/
const path = require('path');
const { spawn } = require('child_process');
const fs = require('fs');
const chalk = require('chalk');
// Get the package root directory
const packageRoot = path.resolve(__dirname, '..');
const pythonSrc = path.join(packageRoot, 'src');
const pythonCli = path.join(pythonSrc, 'cli.py');
const venvPath = path.join(packageRoot, '.venv');
const venvPython = process.platform === 'win32'
? path.join(venvPath, 'Scripts', 'python.exe')
: path.join(venvPath, 'bin', 'python');
function checkPythonSetup() {
// Check if virtual environment exists
if (!fs.existsSync(venvPath)) {
console.error(chalk.red('❌ Error: Python virtual environment not found.'));
console.log(chalk.yellow('💡 Setting up Python environment...'));
const setupScript = process.platform === 'win32' ? 'setup.bat' : './setup.sh';
const setupProcess = spawn(setupScript, [], {
cwd: packageRoot,
stdio: 'inherit',
shell: true
});
setupProcess.on('close', (code) => {
if (code === 0) {
console.log(chalk.green('✅ Python environment setup complete!'));
runSophia();
} else {
console.error(chalk.red('❌ Failed to set up Python environment.'));
process.exit(1);
}
});
return false;
}
// Check if CLI exists
if (!fs.existsSync(pythonCli)) {
console.error(chalk.red('❌ Error: Sophia CLI not found.'));
console.log(chalk.yellow('💡 Please reinstall sophia-coder: npm install -g sophia-coder'));
process.exit(1);
}
return true;
}
function runSophia() {
const args = process.argv.slice(2);
// Check for API key (but let Python CLI handle the interactive setup)
// Only check for help/config commands that don't need API key
const noApiKeyNeeded = args.includes('--config') || args.includes('--help') || args.includes('--version') || args.includes('-h');
if (!process.env.GROQ_API_KEY && !noApiKeyNeeded) {
console.log(chalk.yellow('⚠️ GROQ_API_KEY not found in environment variables.'));
console.log(chalk.cyan(' Sophia will help you set it up interactively...'));
console.log('');
}
// Run the Python CLI
const pythonProcess = spawn(venvPython, [pythonCli, ...args], {
cwd: packageRoot,
stdio: 'inherit',
env: {
...process.env,
PYTHONPATH: pythonSrc
}
});
pythonProcess.on('close', (code) => {
process.exit(code);
});
pythonProcess.on('error', (error) => {
if (error.code === 'ENOENT') {
console.error(chalk.red('❌ Error: Python not found in virtual environment.'));
console.log(chalk.yellow('💡 Try reinstalling: npm install -g sophia-coder'));
} else {
console.error(chalk.red(`❌ Error: ${error.message}`));
}
process.exit(1);
});
// Handle Ctrl+C gracefully
process.on('SIGINT', () => {
pythonProcess.kill('SIGINT');
});
}
function main() {
// Add some branding for first-time users
if (process.argv.includes('--version')) {
const packageJson = require(path.join(packageRoot, 'package.json'));
console.log(chalk.blue(`🤖 Sophia CLI v${packageJson.version}`));
console.log(chalk.gray(' AI Coding Assistant powered by Sophosic-Coder'));
return;
}
if (process.argv.includes('--help') || process.argv.includes('-h')) {
console.log(chalk.blue('🤖 Sophia - AI Coding Assistant'));
console.log(chalk.blue('====================================='));
console.log('');
console.log('Usage:');
console.log(' sophia # Start interactive mode');
console.log(' sophia --help # Show help');
console.log(' sophia --version # Show version');
console.log(' sophia --config # Show configuration');
console.log('');
console.log('Environment Variables:');
console.log(' GROQ_API_KEY # Required: Your Groq API key');
console.log('');
console.log('Get your API key from: https://console.groq.com/');
console.log('');
return;
}
if (checkPythonSetup()) {
runSophia();
}
}
if (require.main === module) {
main();
}