humanbehavior-js
Version:
SDK for HumanBehavior session and event recording
243 lines (198 loc) ⢠6.88 kB
text/typescript
/**
* AI-Enhanced HumanBehavior SDK Auto-Installation CLI
*
* Usage: npx humanbehavior-js ai-auto-install [api-key]
*
* This tool uses AI to intelligently detect frameworks, analyze code patterns,
* and generate optimal integration code that's both future-proof and backward-compatible.
*/
import { AIEnhancedInstallationWizard, AIBrowserInstallationWizard } from '../ai/ai-install-wizard';
import { ManualFrameworkInstallationWizard } from '../ai/manual-framework-wizard';
import { AutoInstallationWizard } from '../core/install-wizard';
import { RemoteAIService } from '../services/remote-ai-service';
import * as clack from '@clack/prompts';
import * as fs from 'fs';
import * as path from 'path';
interface AICLIOptions {
apiKey?: string;
projectPath?: string;
yes?: boolean;
dryRun?: boolean;
framework?: string;
}
class AIAutoInstallCLI {
private options: AICLIOptions;
constructor(options: AICLIOptions) {
this.options = options;
}
async run() {
clack.intro('š¤ AI-Enhanced HumanBehavior SDK Auto-Installation');
try {
// Get API key
const apiKey = await this.getApiKey();
if (!apiKey) {
clack.cancel('API key is required');
process.exit(1);
}
// Get project path
const projectPath = this.options.projectPath || process.cwd();
// Choose framework
const framework = await this.chooseFramework();
if (!framework) {
clack.cancel('Installation cancelled.');
process.exit(0);
}
// Confirm installation
if (!this.options.yes) {
const confirmed = await this.confirmInstallation(projectPath, framework);
if (!confirmed) {
clack.cancel('Installation cancelled.');
process.exit(0);
}
}
// Run installation
const spinner = clack.spinner();
spinner.start('š Analyzing your project with AI...');
const wizard = new ManualFrameworkInstallationWizard(apiKey, projectPath, framework);
const result = await wizard.install();
spinner.stop('Analysis complete!');
// Display results
this.displayResults(result, framework);
} catch (error) {
clack.cancel(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`);
process.exit(1);
}
}
private async getApiKey(): Promise<string> {
if (this.options.apiKey) {
return this.options.apiKey;
}
const apiKey = await clack.text({
message: 'Enter your HumanBehavior API key:',
placeholder: 'hb_...',
validate: (value) => {
if (!value) return 'API key is required';
if (!value.startsWith('hb_')) return 'API key should start with "hb_"';
return undefined;
}
});
return apiKey;
}
private async confirmInstallation(projectPath: string, framework: string): Promise<boolean> {
const confirmed = await clack.confirm({
message: `Ready to install HumanBehavior SDK in ${projectPath} for ${framework}?`
});
return confirmed;
}
private async chooseFramework(): Promise<string> {
const framework = await clack.select({
message: 'Select your framework:',
options: [
{ label: 'React', value: 'react' },
{ label: 'Next.js', value: 'nextjs' },
{ label: 'Vue', value: 'vue' },
{ label: 'Angular', value: 'angular' },
{ label: 'Svelte', value: 'svelte' },
{ label: 'Nuxt.js', value: 'nuxt' },
{ label: 'Remix', value: 'remix' },
{ label: 'Astro', value: 'astro' },
{ label: 'Gatsby', value: 'gatsby' },
{ label: 'Vanilla JS/TS', value: 'vanilla' }
]
});
return framework;
}
private displayResults(result: any, framework: string) {
if (result.success) {
clack.outro('š Installation completed successfully!');
// Display framework info
clack.note(`Framework detected: ${result.framework.name} (${result.framework.type})`, 'Framework Info');
// Display modifications
if (result.modifications && result.modifications.length > 0) {
const modifications = result.modifications.map((mod: any) =>
`${mod.action}: ${mod.filePath} - ${mod.description}`
);
clack.note(modifications.join('\n'), 'Files Modified');
}
// Display next steps
if (result.nextSteps && result.nextSteps.length > 0) {
clack.note(result.nextSteps.join('\n'), 'Next Steps');
}
// Display AI insights if available
if (result.aiAnalysis) {
clack.note(`Confidence: ${Math.round(result.aiAnalysis.confidence * 100)}%`, 'AI Analysis');
if (result.aiAnalysis.recommendations && result.aiAnalysis.recommendations.length > 0) {
clack.note(result.aiAnalysis.recommendations.join('\n'), 'AI Recommendations');
}
}
} else {
clack.cancel('Installation failed');
if (result.errors && result.errors.length > 0) {
clack.note(result.errors.join('\n'), 'Errors');
}
}
}
}
function parseArgs(): AICLIOptions {
const args = process.argv.slice(2);
const options: AICLIOptions = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case '--help':
case '-h':
showHelp();
process.exit(0);
break;
case '--yes':
case '-y':
options.yes = true;
break;
case '--dry-run':
options.dryRun = true;
break;
case '--project':
case '-p':
options.projectPath = args[++i];
break;
case '--framework':
case '-f':
options.framework = args[++i];
break;
default:
if (!options.apiKey && !arg.startsWith('-')) {
options.apiKey = arg;
}
break;
}
}
return options;
}
function showHelp() {
console.log(`
š¤ HumanBehavior SDK AI Auto-Installation
Usage: npx humanbehavior-js ai-auto-install [api-key] [options]
Options:
-h, --help Show this help message
-y, --yes Skip all prompts and use defaults
--dry-run Show what would be changed without making changes
-p, --project <path> Specify project directory
-f, --framework <name> Specify framework manually
Examples:
npx humanbehavior-js ai-auto-install
npx humanbehavior-js ai-auto-install hb_your_api_key_here
npx humanbehavior-js ai-auto-install --project ./my-app --ai
npx humanbehavior-js ai-auto-install --framework react --yes
`);
}
// Main execution
if (import.meta.url === `file://${process.argv[1]}`) {
const options = parseArgs();
const cli = new AIAutoInstallCLI(options);
cli.run().catch((error) => {
clack.cancel(`Unexpected error: ${error.message}`);
process.exit(1);
});
}
export { AIAutoInstallCLI };