@handit.ai/cli
Version:
AI-Powered Agent Instrumentation & Monitoring CLI Tool
128 lines (100 loc) ⢠4.56 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const ora = require('ora').default;
const { CodeGenerator } = require('./codeGenerator');
const { IterativeCodeGenerator } = require('./iterativeGenerator');
/**
* Generate instrumented code for all selected functions (LEGACY - use iterative instead)
*/
async function generateInstrumentedCode(selectedFunctionIds, allNodes, language, agentName, projectRoot) {
const spinner = ora('Generating instrumented code with AI...').start();
try {
// Filter nodes to only selected functions
const selectedNodes = allNodes.filter(node => selectedFunctionIds.includes(node.id));
// Initialize code generator
const generator = new CodeGenerator(language, agentName);
// Generate Handit service file
await generator.generateHanditService(projectRoot);
// Generate instrumented code for each function
const instrumentedFunctions = [];
for (const node of selectedNodes) {
spinner.text = `Generating instrumentation for ${node.name}...`;
// Get original function code
const originalCode = await generator.getOriginalFunctionCode(node);
// Generate instrumented version
const { changes, originalArray, instrumentedArray } = await generator.generateInstrumentedFunction(node, originalCode, selectedNodes);
instrumentedFunctions.push({
node,
originalCode,
instrumentedCode: changes,
originalArray,
instrumentedArray,
filePath: path.resolve(node.file)
});
}
spinner.succeed(`Generated instrumented code for ${instrumentedFunctions.length} functions`);
// Display the generated code
await displayGeneratedCode(instrumentedFunctions, language);
return instrumentedFunctions;
} catch (error) {
spinner.fail('Failed to generate instrumented code');
throw error;
}
}
/**
* Generate instrumented code iteratively with user confirmation
*/
async function generateInstrumentedCodeIteratively(selectedFunctionIds, allNodes, language, agentName, projectRoot, apiToken = null) {
const generator = new IterativeCodeGenerator(language, agentName, projectRoot, apiToken);
return await generator.generateIteratively(selectedFunctionIds, allNodes, apiToken);
}
/**
* Display the generated instrumented code to the user
*/
async function displayGeneratedCode(instrumentedFunctions, language) {
console.log(chalk.blue.bold('\nš¤ Generated Instrumented Code'));
console.log(chalk.gray('Here\'s the AI-generated code with Handit.ai tracing:\n'));
for (const func of instrumentedFunctions) {
console.log(chalk.cyan.bold(`š ${func.node.file} - ${func.node.name} (line ${func.node.line})`));
console.log(chalk.gray('ā'.repeat(80)));
// Display the instrumented code with syntax highlighting
console.log(chalk.white(func.instrumentedCode));
console.log(chalk.gray('ā'.repeat(80)));
console.log(''); // Empty line for spacing
}
// Show setup instructions
console.log(chalk.yellow.bold('š Setup Instructions:'));
console.log(chalk.gray('1. Install Handit.ai SDK:'));
if (language === 'javascript') {
console.log(chalk.white(' npm install @handit.ai/node'));
} else {
console.log(chalk.white(' pip install handit-sdk'));
}
console.log(chalk.gray('2. Set your Handit API key:'));
console.log(chalk.white(' export HANDIT_API_KEY="your-api-key-here"'));
console.log(chalk.gray('3. Replace the original functions with the instrumented versions above'));
console.log(chalk.gray('4. Test your agent to start collecting traces\n'));
}
/**
* Save generated code to files (optional)
*/
async function saveInstrumentedCode(instrumentedFunctions, projectRoot) {
const outputDir = path.join(projectRoot, 'handit-instrumented');
await fs.ensureDir(outputDir);
for (const func of instrumentedFunctions) {
const fileName = `${func.node.name}_instrumented.${func.node.file.endsWith('.py') ? 'py' : 'js'}`;
const filePath = path.join(outputDir, fileName);
const content = `// Instrumented version of ${func.node.name} from ${func.node.file}
// Generated by Handit CLI
${func.instrumentedCode}`;
await fs.writeFile(filePath, content);
}
console.log(chalk.green(`ā Saved instrumented code to ${outputDir}/`));
return outputDir;
}
module.exports = {
generateInstrumentedCode,
generateInstrumentedCodeIteratively,
saveInstrumentedCode
};