@aaswe/codebase-ai
Version:
AI-Assisted Software Engineering (AASWE) - Rich codebase context for IDE LLMs
303 lines • 11.4 kB
JavaScript
"use strict";
/**
* Automatic Analysis Service
*
* Main orchestration service for automatic project analysis and TTL generation.
* Coordinates all automatic analysis components and provides a unified interface.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutomaticAnalysisService = void 0;
const events_1 = require("events");
const logger_1 = __importDefault(require("../../utils/logger"));
const TriggerOrchestrator_1 = require("./TriggerOrchestrator");
/**
* Automatic Analysis Service
*
* Provides a unified interface for automatic project analysis and TTL generation.
* Manages the complete lifecycle from trigger detection to knowledge graph population.
*/
class AutomaticAnalysisService extends events_1.EventEmitter {
config;
triggerOrchestrator;
isInitialized = false;
isActive = false;
analysisStats = {
total: 0,
successful: 0,
failed: 0
};
constructor(config = {}) {
super();
this.config = {
projectRoot: process.cwd(),
enableAutoTriggers: true,
enableNPMHooks: true,
enableInstallationDetection: true,
enableFileWatching: true,
analysisDelay: 2000,
preserveBusinessContext: true,
generateTTL: true,
populateKnowledgeGraph: true,
updateMCPContext: true,
...config
};
}
/**
* Initialize the automatic analysis system
*/
async initialize() {
if (this.isInitialized) {
return;
}
try {
logger_1.default.info('Initializing Automatic Analysis Service', {
projectRoot: this.config.projectRoot,
enableAutoTriggers: this.config.enableAutoTriggers
});
// Initialize trigger orchestrator
const triggerConfig = {
projectRoot: this.config.projectRoot,
enableNPMHooks: this.config.enableNPMHooks,
enableInstallationDetection: this.config.enableInstallationDetection,
enableFileWatching: this.config.enableFileWatching,
analysisDelay: this.config.analysisDelay
};
this.triggerOrchestrator = new TriggerOrchestrator_1.TriggerOrchestrator(triggerConfig);
await this.triggerOrchestrator.initialize();
// Setup event listeners
this.setupEventListeners();
this.isInitialized = true;
logger_1.default.info('Automatic Analysis Service initialized successfully');
}
catch (error) {
logger_1.default.error('Failed to initialize Automatic Analysis Service', { error });
throw error;
}
}
/**
* Start automatic analysis
*/
async start() {
if (!this.isInitialized) {
await this.initialize();
}
if (this.isActive) {
logger_1.default.warn('Automatic analysis is already active');
return;
}
try {
logger_1.default.info('Starting automatic analysis');
this.isActive = true;
this.emit('service_started');
logger_1.default.info('Automatic analysis started successfully');
}
catch (error) {
logger_1.default.error('Failed to start automatic analysis', { error });
this.isActive = false;
throw error;
}
}
/**
* Stop automatic analysis
*/
async stop() {
if (!this.isActive) {
return;
}
try {
logger_1.default.info('Stopping automatic analysis');
this.isActive = false;
this.emit('service_stopped');
logger_1.default.info('Automatic analysis stopped successfully');
}
catch (error) {
logger_1.default.error('Failed to stop automatic analysis', { error });
throw error;
}
}
/**
* Manually trigger analysis
*/
async triggerAnalysis(reason = 'manual') {
if (!this.triggerOrchestrator) {
throw new Error('Automatic Analysis Service not initialized');
}
logger_1.default.info('Manual analysis trigger requested', { reason });
return await this.triggerOrchestrator.triggerAnalysis(reason, 'high');
}
/**
* Perform initial project analysis (for setup)
*/
async performInitialAnalysis() {
if (!this.triggerOrchestrator) {
throw new Error('Automatic Analysis Service not initialized');
}
logger_1.default.info('Performing initial project analysis');
try {
const triggerId = await this.triggerOrchestrator.triggerAnalysis('initial_setup', 'high');
logger_1.default.debug('Initial setup analysis triggered', { triggerId });
// Wait for analysis to complete
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('Initial analysis timeout'));
}, 300000); // 5 minute timeout
const onCompleted = (result) => {
clearTimeout(timeout);
this.removeListener('analysis_failed', onFailed);
resolve(result);
};
const onFailed = (event) => {
clearTimeout(timeout);
this.removeListener('analysis_completed', onCompleted);
reject(new Error(`Initial analysis failed: ${event.error}`));
};
this.once('analysis_completed', onCompleted);
this.once('analysis_failed', onFailed);
});
}
catch (error) {
logger_1.default.error('Failed to perform initial analysis', { error });
throw error;
}
}
/**
* Get service status
*/
getStatus() {
return {
isActive: this.isActive,
isInitialized: this.isInitialized,
config: { ...this.config },
triggerOrchestrator: this.triggerOrchestrator?.getStatus(),
lastAnalysis: this.triggerOrchestrator?.getStatus().lastAnalysis,
totalAnalyses: this.analysisStats.total,
successfulAnalyses: this.analysisStats.successful,
failedAnalyses: this.analysisStats.failed
};
}
/**
* Shutdown the service
*/
async shutdown() {
logger_1.default.info('Shutting down Automatic Analysis Service');
try {
// Stop if active
if (this.isActive) {
await this.stop();
}
// Shutdown trigger orchestrator
if (this.triggerOrchestrator) {
await this.triggerOrchestrator.shutdown();
}
// Remove all listeners
this.removeAllListeners();
this.isInitialized = false;
logger_1.default.info('Automatic Analysis Service shutdown completed');
}
catch (error) {
logger_1.default.error('Error during Automatic Analysis Service shutdown', { error });
throw error;
}
}
// Private methods
setupEventListeners() {
if (!this.triggerOrchestrator)
return;
// Analysis completion events
this.triggerOrchestrator.on('analysis_completed', (result) => {
this.analysisStats.total++;
this.analysisStats.successful++;
logger_1.default.info('Automatic analysis completed', {
analysisId: result.analysisId,
duration: result.duration,
analyzedFiles: result.summary.analyzedFiles,
ttlFilesGenerated: result.summary.ttlFilesGenerated
});
this.emit('analysis_completed', result);
});
this.triggerOrchestrator.on('analysis_failed', (event) => {
this.analysisStats.total++;
this.analysisStats.failed++;
logger_1.default.error('Automatic analysis failed', {
analysisId: event.analysisId,
error: event.error
});
this.emit('analysis_failed', event);
});
// Trigger events
this.triggerOrchestrator.on('trigger_queued', (trigger) => {
logger_1.default.debug('Analysis trigger queued', {
triggerId: trigger.id,
type: trigger.type,
reason: trigger.reason
});
this.emit('trigger_queued', trigger);
});
this.triggerOrchestrator.on('trigger_processing', (trigger) => {
logger_1.default.debug('Processing analysis trigger', {
triggerId: trigger.id,
type: trigger.type
});
this.emit('trigger_processing', trigger);
});
this.triggerOrchestrator.on('trigger_completed', (event) => {
logger_1.default.debug('Analysis trigger completed', {
triggerId: event.trigger.id,
success: event.result.success,
duration: event.result.duration
});
this.emit('trigger_completed', event);
});
this.triggerOrchestrator.on('trigger_failed', (event) => {
logger_1.default.warn('Analysis trigger failed', {
triggerId: event.trigger.id,
error: event.error
});
this.emit('trigger_failed', event);
});
}
/**
* Static method to create and initialize service
*/
static async create(config = {}) {
const service = new AutomaticAnalysisService(config);
await service.initialize();
return service;
}
/**
* Static method for quick setup (used by CLI and postinstall)
*/
static async quickSetup(projectRoot = process.cwd()) {
logger_1.default.info('Performing quick automatic analysis setup', { projectRoot });
try {
// Create and initialize service
const service = await AutomaticAnalysisService.create({
projectRoot,
enableAutoTriggers: true,
enableNPMHooks: true,
enableInstallationDetection: true,
enableFileWatching: true
});
// Start the service
await service.start();
// Perform initial analysis
const result = await service.performInitialAnalysis();
logger_1.default.info('Quick setup completed successfully', {
projectRoot,
analysisId: result.analysisId,
analyzedFiles: result.summary.analyzedFiles,
ttlFilesGenerated: result.summary.ttlFilesGenerated
});
return { service, result };
}
catch (error) {
logger_1.default.error('Quick setup failed', { projectRoot, error });
throw error;
}
}
}
exports.AutomaticAnalysisService = AutomaticAnalysisService;
//# sourceMappingURL=AutomaticAnalysisService.js.map