UNPKG

adpa-enterprise-framework-automation

Version:

Modular, standards-compliant Node.js/TypeScript automation framework for enterprise requirements, project, and data management. Provides CLI and API for BABOK v3, PMBOK 7th Edition, and DMBOK 2.0 (in progress). Production-ready Express.js API with TypeSpe

321 lines • 14.6 kB
// Feedback-Enhanced Document Generator // filepath: src/modules/documentGenerator/FeedbackEnhancedGenerator.ts import { DocumentGenerator } from './DocumentGenerator.js'; import { FeedbackIntegrationService } from '../../services/FeedbackIntegrationService.js'; import { DocumentFeedback } from '../../models/DocumentFeedback.js'; // Helper to extract only GenerationOptions properties function extractGenerationOptions(opts) { const keys = [ 'includeCategories', 'excludeCategories', 'maxConcurrent', 'delayBetweenCalls', 'continueOnError', 'generateIndex', 'cleanup', 'outputDir', 'format' ]; const result = {}; for (const key of keys) { if (key in opts) result[key] = opts[key]; } return result; } export class FeedbackEnhancedGenerator extends DocumentGenerator { feedbackService; enhancedOptions; constructor(context, options = {}, aiProcessor) { const baseOptions = extractGenerationOptions(options); super(context, baseOptions, aiProcessor); this.feedbackService = new FeedbackIntegrationService(); this.enhancedOptions = { applyFeedbackImprovements: true, learningMode: true, qualityThreshold: 80, maxIterations: 3, ...options }; } /** * Generate documents with feedback-driven improvements */ async generateWithFeedbackEnhancement() { console.log('🧠 Starting feedback-enhanced document generation...'); const startTime = Date.now(); let qualityMetrics = { beforeScore: 0, afterScore: 0, improvement: 0 }; let feedbackInsights = null; let learningData = null; try { // Phase 1: Apply existing feedback improvements if enabled if (this.enhancedOptions.applyFeedbackImprovements && this.enhancedOptions.projectId) { console.log('šŸ“Š Applying feedback-driven improvements...'); feedbackInsights = await this.feedbackService.applyFeedbackImprovements(this.enhancedOptions.projectId); console.log(`āœ… Applied improvements to ${feedbackInsights.documentsImproved.length} document types`); } // Phase 2: Generate documents with enhanced prompts console.log('šŸ“ Generating documents with enhanced prompts...'); const baseResult = await super.generateAll(); // Phase 3: Quality assessment and iterative improvement if (this.enhancedOptions.learningMode) { console.log('šŸ” Performing quality assessment...'); const qualityAssessment = await this.assessGeneratedQuality(); qualityMetrics.beforeScore = qualityAssessment.averageScore; // Iterative improvement if quality is below threshold if (qualityAssessment.averageScore < this.enhancedOptions.qualityThreshold) { console.log(`⚔ Quality below threshold (${qualityAssessment.averageScore}%), applying iterative improvements...`); const improvedResult = await this.performIterativeImprovement(qualityAssessment, this.enhancedOptions.maxIterations); qualityMetrics.afterScore = improvedResult.finalScore; learningData = improvedResult.learningData; } else { qualityMetrics.afterScore = qualityAssessment.averageScore; } qualityMetrics.improvement = qualityMetrics.afterScore - qualityMetrics.beforeScore; } // Phase 4: Generate recommendations for future improvements const recommendations = this.enhancedOptions.projectId ? await this.feedbackService.generateRecommendations(this.enhancedOptions.projectId) : { immediateActions: [], strategicImprovements: [] }; const enhancedResult = { ...baseResult, duration: Date.now() - startTime, feedbackInsights: feedbackInsights ? { appliedImprovements: feedbackInsights.improvementsSummary, qualityPrediction: feedbackInsights.qualityPrediction, recommendedActions: [ ...recommendations.immediateActions, ...recommendations.strategicImprovements ] } : undefined, qualityMetrics: this.enhancedOptions.learningMode ? qualityMetrics : undefined, learningData }; this.printEnhancedSummary(enhancedResult); return enhancedResult; } catch (error) { console.error('āŒ Error in feedback-enhanced generation:', error); // Fallback to standard generation console.log('šŸ”„ Falling back to standard generation...'); const fallbackResult = await super.generateAll(); return { ...fallbackResult, duration: Date.now() - startTime, feedbackInsights: { appliedImprovements: [], qualityPrediction: 0, recommendedActions: ['Review feedback integration configuration'] } }; } } /** * Generate a single document with feedback enhancement */ async generateOneWithFeedback(documentKey, projectId) { try { console.log(`🧠 Generating ${documentKey} with feedback enhancement...`); // Get existing feedback for this document type const existingFeedback = projectId ? await DocumentFeedback.find({ projectId, documentType: documentKey, rating: { $lte: 3 } }) : []; // Apply feedback improvements if available if (existingFeedback.length > 0) { console.log(`šŸ“Š Found ${existingFeedback.length} feedback items for ${documentKey}`); // TODO: Apply specific improvements based on feedback } // Generate the document const success = await super.generateOne(documentKey); if (success) { // Assess quality of generated document const qualityScore = await this.assessDocumentQuality(documentKey); return { success: true, qualityScore, improvements: existingFeedback.length > 0 ? ['Applied feedback-driven prompt improvements'] : [] }; } return { success: false }; } catch (error) { console.error(`āŒ Error generating ${documentKey} with feedback:`, error); return { success: false }; } } /** * Assess quality of generated documents */ async assessGeneratedQuality() { try { // Use PMBOK validation to assess quality const validation = await super.validatePMBOKCompliance(); const documentScores = {}; let totalScore = 0; let documentCount = 0; // Extract individual document scores if (validation.documentQuality) { for (const [docType, quality] of Object.entries(validation.documentQuality)) { const score = quality.score || 0; documentScores[docType] = score; totalScore += score; documentCount++; } } const averageScore = documentCount > 0 ? totalScore / documentCount : 0; // Identify common issues const issues = []; if (averageScore < 70) issues.push('Overall quality below acceptable threshold'); if (validation.consistencyScore < 80) issues.push('Cross-document consistency needs improvement'); return { averageScore, documentScores, issues }; } catch (error) { console.error('Error assessing document quality:', error); return { averageScore: 0, documentScores: {}, issues: ['Quality assessment failed'] }; } } /** * Assess quality of a specific document */ async assessDocumentQuality(documentKey) { try { // Simplified quality assessment - in a real implementation, // this would use more sophisticated analysis const validation = await super.validatePMBOKCompliance(); if (validation.documentQuality && validation.documentQuality[documentKey]) { return validation.documentQuality[documentKey].score || 0; } return 75; // Default score if specific assessment unavailable } catch (error) { console.error(`Error assessing quality for ${documentKey}:`, error); return 0; } } /** * Perform iterative improvement based on quality assessment */ async performIterativeImprovement(qualityAssessment, maxIterations) { let currentScore = qualityAssessment.averageScore; let iterationsPerformed = 0; const learningData = { promptOptimizations: 0, feedbackProcessed: 0, patternsIdentified: [] }; for (let i = 0; i < maxIterations; i++) { if (currentScore >= this.enhancedOptions.qualityThreshold) { break; } console.log(`šŸ”„ Iteration ${i + 1}: Attempting to improve quality (current: ${currentScore}%)`); try { // Identify lowest-scoring documents const lowScoringDocs = Object.entries(qualityAssessment.documentScores) .filter(([, score]) => score < this.enhancedOptions.qualityThreshold) .sort(([, a], [, b]) => a - b) .slice(0, 3); // Focus on top 3 lowest-scoring documents // Apply targeted improvements for (const [docType] of lowScoringDocs) { await this.applyTargetedImprovement(docType); learningData.promptOptimizations++; } // Re-assess quality const newAssessment = await this.assessGeneratedQuality(); const improvement = newAssessment.averageScore - currentScore; if (improvement > 0) { currentScore = newAssessment.averageScore; console.log(`āœ… Iteration ${i + 1}: Quality improved by ${improvement.toFixed(1)}%`); } else { console.log(`āš ļø Iteration ${i + 1}: No significant improvement detected`); } iterationsPerformed++; } catch (error) { console.error(`āŒ Error in iteration ${i + 1}:`, error); break; } } return { finalScore: currentScore, iterationsPerformed, learningData }; } /** * Apply targeted improvement to a specific document type */ async applyTargetedImprovement(documentType) { try { console.log(`šŸŽÆ Applying targeted improvement to ${documentType}`); // In a real implementation, this would: // 1. Analyze specific issues with the document // 2. Optimize the prompt for this document type // 3. Regenerate the document with improved prompt // 4. Validate the improvement // For now, we'll simulate this process await super.generateOne(documentType); } catch (error) { console.error(`Error applying targeted improvement to ${documentType}:`, error); } } /** * Print enhanced generation summary */ printEnhancedSummary(result) { console.log(`\n🧠 Enhanced Generation Summary:`); console.log(`āœ… Successfully generated: ${result.successCount} documents`); console.log(`āŒ Failed to generate: ${result.failureCount} documents`); console.log(`ā±ļø Total duration: ${(result.duration / 1000).toFixed(2)}s`); if (result.feedbackInsights) { console.log(`\nšŸ“Š Feedback Integration:`); console.log(`šŸ”§ Applied improvements: ${result.feedbackInsights.appliedImprovements.length}`); console.log(`šŸ“ˆ Quality prediction: +${result.feedbackInsights.qualityPrediction}%`); if (result.feedbackInsights.recommendedActions.length > 0) { console.log(`šŸ’” Recommended actions:`); result.feedbackInsights.recommendedActions.slice(0, 3).forEach((action, i) => { console.log(` ${i + 1}. ${action}`); }); } } if (result.qualityMetrics) { console.log(`\nšŸ“ˆ Quality Metrics:`); console.log(`šŸ“Š Before: ${result.qualityMetrics.beforeScore}%`); console.log(`šŸ“Š After: ${result.qualityMetrics.afterScore}%`); console.log(`šŸ“ˆ Improvement: ${result.qualityMetrics.improvement > 0 ? '+' : ''}${result.qualityMetrics.improvement.toFixed(1)}%`); } if (result.learningData) { console.log(`\nšŸ¤– Learning Data:`); console.log(`šŸ”§ Prompt optimizations: ${result.learningData.promptOptimizations}`); console.log(`šŸ“ Feedback processed: ${result.learningData.feedbackProcessed}`); } console.log(`\nšŸŽÆ Next Steps:`); console.log(` • Review generated documents for quality`); console.log(` • Provide feedback to improve future generations`); console.log(` • Monitor quality trends over time`); } /** * Static method to create feedback-enhanced generator with project context */ static async createForProject(projectId, context, options = {}) { const enhancedOptions = { projectId, applyFeedbackImprovements: true, learningMode: true, ...options }; return new FeedbackEnhancedGenerator(context, enhancedOptions); } } //# sourceMappingURL=FeedbackEnhancedGenerator.js.map