/** * SlideGenerator - Creates slides using expert principles from the knowledge base * * This generator fully leverages the knowledge base for: * - Word limits (from KB, not hardcoded) * - Slide templates (keynote vs business) * - Story frameworks (SCQA, Sparkline) * - Expert principles (Duarte, Minto, etc.) * - Anti-pattern avoidance * * CRITICAL: Every decision queries the KnowledgeGateway */ import { KnowledgeGateway, getKnowledgeGateway, PresentationType, SlideTemplate, ColorPalette } from '../kb/KnowledgeGateway.js'; export interface ContentSection { header: string; level: number; content: string; bullets: string[]; metrics: Array<{ value: string; label: string; context?: string }>; } export interface ContentAnalysis { detectedType: PresentationType; title: string; sections: ContentSection[]; keyMessages: string[]; dataPoints: Array<{ value: string; label: string; context?: string }>; scqa: { situation?: string; complication?: string; question?: string; answer?: string; }; sparkline: { whatIs?: string; whatCouldBe?: string; starMoment?: string; callToAction?: string; }; } export interface Slide { index: number; type: string; title: string; content: SlideContent; notes?: string; template?: string; } export interface SlideContent { subtitle?: string; statement?: string; body?: string; bullets?: string[]; metrics?: Array<{ value: string; label: string; trend?: 'up' | 'down' | 'neutral' }>; quote?: { text: string; attribution?: string }; callToAction?: string; image?: { url: string; alt: string; caption?: string }; } export class SlideGenerator { private kb!: KnowledgeGateway; private mode: 'keynote' | 'business' = 'keynote'; private presentationType: PresentationType = 'ted_keynote'; private createdTitles = new Set(); private sectionDividerCount = 0; async initialize(): Promise { this.kb = await getKnowledgeGateway(); } /** * Generate slides from analyzed content */ async generate(analysis: ContentAnalysis, type?: PresentationType): Promise { await this.initialize(); this.presentationType = type || analysis.detectedType; this.mode = this.kb.getModeForType(this.presentationType); this.createdTitles.clear(); this.sectionDividerCount = 0; console.log(` ✓ Using ${this.mode} mode for ${this.presentationType}`); const slides: Slide[] = []; let index = 0; // 1. Title slide (always first) slides.push(this.createTitleSlide(index++, analysis)); // 2. Agenda slide (business mode with 3+ sections) if (this.mode === 'business' && analysis.sections.length >= 3) { slides.push(this.createAgendaSlide(index++, analysis)); } // 3. SCQA slides (if available) if (analysis.scqa.situation) { slides.push(this.createSituationSlide(index++, analysis.scqa.situation)); } if (analysis.scqa.complication) { slides.push(this.createProblemSlide(index++, analysis.scqa.complication)); } // 4. Content slides from sections for (const section of analysis.sections) { const sectionSlides = this.createSectionSlides(index, section); for (const slide of sectionSlides) { slide.index = index++; slides.push(slide); } } // 5. Key metrics slide (if data points) if (analysis.dataPoints.length > 0) { slides.push(this.createMetricsSlide(index++, analysis.dataPoints)); } // 6. Solution/Answer slide if (analysis.scqa.answer) { slides.push(this.createSolutionSlide(index++, analysis.scqa.answer)); } // 7. Call to action slide if (analysis.sparkline.callToAction) { slides.push(this.createCTASlide(index++, analysis.sparkline.callToAction)); } // 8. Thank you slide (always last) slides.push(this.createThankYouSlide(index++)); console.log(` ✓ Generated ${slides.length} slides`); return slides; } /** * Create title slide */ private createTitleSlide(index: number, analysis: ContentAnalysis): Slide { const wordLimits = this.kb.getWordLimits(this.mode); return { index, type: 'title', title: this.truncateToWordLimit(analysis.title, wordLimits.max), content: { subtitle: analysis.keyMessages[0] ? this.truncateToWordLimit(analysis.keyMessages[0], 15) : undefined }, template: 'Title Impact' }; } /** * Create agenda slide (business mode) */ private createAgendaSlide(index: number, analysis: ContentAnalysis): Slide { const bulletLimits = this.kb.getBulletLimits(this.mode); // Get section headers as agenda items const agendaItems = analysis.sections .filter(s => s.level === 2) .slice(0, bulletLimits.maxBullets) .map(s => this.cleanText(s.header)); return { index, type: 'agenda', title: 'Agenda', content: { bullets: agendaItems }, template: 'Detailed Findings' }; } /** * Create situation/context slide */ private createSituationSlide(index: number, situation: string): Slide { if (this.isDuplicate('Situation')) return this.createSkipSlide(index); const wordLimits = this.kb.getWordLimits(this.mode); return { index, type: 'context', title: this.mode === 'business' ? 'Current Situation' : 'Where We Are', content: { statement: this.truncateToWordLimit(this.cleanText(situation), wordLimits.max) }, template: 'Single Statement' }; } /** * Create problem/complication slide */ private createProblemSlide(index: number, complication: string): Slide { if (this.isDuplicate('Problem')) return this.createSkipSlide(index); const wordLimits = this.kb.getWordLimits(this.mode); return { index, type: 'problem', title: this.mode === 'business' ? 'The Challenge' : 'What\'s At Stake', content: { statement: this.truncateToWordLimit(this.cleanText(complication), wordLimits.max) }, template: 'Single Statement' }; } /** * Create slides for a content section */ private createSectionSlides(baseIndex: number, section: ContentSection): Slide[] { const slides: Slide[] = []; // Skip duplicate titles if (this.isDuplicate(section.header)) { return slides; } const wordLimits = this.kb.getWordLimits(this.mode); const bulletLimits = this.kb.getBulletLimits(this.mode); const maxDividers = 4; // From KB quality metrics // Create section divider for major sections (limit to 4) if (section.level === 2 && this.sectionDividerCount < maxDividers) { // Only create divider if section has substantial content if (section.content.length > 50 || section.bullets.length > 0) { slides.push({ index: baseIndex, type: 'section', title: this.cleanText(section.header), content: {}, template: 'Section Divider' }); this.sectionDividerCount++; } } // Create content slide(s) if (section.bullets.length > 0) { // Clean and limit bullets const cleanedBullets = section.bullets .map(b => this.cleanText(b)) .filter(b => b.length > 0) .map(b => this.truncateToWordLimit(b, bulletLimits.maxWordsPerBullet)) .slice(0, bulletLimits.maxBullets); if (cleanedBullets.length > 0) { slides.push({ index: baseIndex + slides.length, type: 'bullets', title: this.createActionTitle(section.header, cleanedBullets), content: { bullets: cleanedBullets }, template: 'Detailed Findings' }); } } else if (section.content.length > 0) { // Body text slide slides.push({ index: baseIndex + slides.length, type: 'body', title: this.cleanText(section.header), content: { body: this.truncateToWordLimit(this.cleanText(section.content), wordLimits.max) }, template: 'Single Statement' }); } // Create metrics slide if section has data if (section.metrics.length > 0) { const cleanedMetrics = section.metrics.map(m => ({ value: this.cleanText(m.value), label: this.cleanMetricLabel(m.label), trend: undefined as 'up' | 'down' | 'neutral' | undefined })); slides.push({ index: baseIndex + slides.length, type: 'metrics', title: `Key Metrics: ${this.cleanText(section.header)}`, content: { metrics: cleanedMetrics }, template: 'Data Insight' }); } return slides; } /** * Create metrics slide */ private createMetricsSlide( index: number, dataPoints: Array<{ value: string; label: string; context?: string }> ): Slide { const cleanedMetrics = dataPoints.slice(0, 4).map(d => ({ value: this.cleanText(d.value), label: this.cleanMetricLabel(d.label), trend: undefined as 'up' | 'down' | 'neutral' | undefined })); return { index, type: 'metrics', title: 'Key Metrics', content: { metrics: cleanedMetrics }, template: 'Data Insight' }; } /** * Create solution/answer slide */ private createSolutionSlide(index: number, answer: string): Slide { if (this.isDuplicate('Solution')) return this.createSkipSlide(index); const wordLimits = this.kb.getWordLimits(this.mode); return { index, type: 'solution', title: this.mode === 'business' ? 'Our Recommendation' : 'The Answer', content: { statement: this.truncateToWordLimit(this.cleanText(answer), wordLimits.max) }, template: 'Single Statement' }; } /** * Create call to action slide */ private createCTASlide(index: number, callToAction: string): Slide { const wordLimits = this.kb.getWordLimits(this.mode); return { index, type: 'cta', title: 'Next Steps', content: { callToAction: this.truncateToWordLimit(this.cleanText(callToAction), wordLimits.max) }, template: 'Call to Action' }; } /** * Create thank you slide */ private createThankYouSlide(index: number): Slide { return { index, type: 'thank_you', title: 'Thank You', content: { subtitle: 'Questions?' }, template: 'Title Impact' }; } /** * Create a skip placeholder (for duplicates) */ private createSkipSlide(index: number): Slide { return { index, type: 'skip', title: '', content: {} }; } /** * Create an action title (business mode) * Per Minto/McKinsey: Title should communicate the conclusion, not the topic */ private createActionTitle(header: string, bullets: string[]): string { if (this.mode === 'keynote') { return this.cleanText(header); } // For business mode, try to create an action title // "Q3 Results" → "Q3 revenue exceeded targets by 23%" // If we can't generate a good one, use the header const cleanHeader = this.cleanText(header); // If bullets have clear insights, use them if (bullets.length > 0 && bullets[0].length > 10) { const insight = bullets[0]; if (insight.includes('increase') || insight.includes('decrease') || insight.includes('grew') || insight.includes('declined') || insight.includes('achieve') || insight.includes('exceed')) { return this.truncateToWordLimit(insight, 15); } } return cleanHeader; } /** * Check if a title has already been created (de-duplication) */ private isDuplicate(title: string): boolean { const normalized = title.toLowerCase().trim(); if (this.createdTitles.has(normalized)) { return true; } this.createdTitles.add(normalized); return false; } /** * Clean text by removing all content markers and normalizing */ private cleanText(text: string): string { if (!text) return ''; return text .replace(/\[HEADER\]\s*/g, '') .replace(/\[BULLET\]\s*/g, '') .replace(/\[NUMBERED\]\s*/g, '') .replace(/\[EMPHASIS\]/g, '') .replace(/\[\/EMPHASIS\]/g, '') .replace(/\[CODE BLOCK\]/g, '') .replace(/\[IMAGE\]/g, '') .replace(/\*\*/g, '') .replace(/\*/g, '') .replace(/#{1,6}\s*/g, '') .replace(/\s+/g, ' ') .trim(); } /** * Clean metric labels (strip table syntax) * Fixes the "Century Interactive |" garbage label issue */ private cleanMetricLabel(label: string): string { if (!label) return ''; return label .replace(/\|/g, ' ') // Remove pipes .replace(/-{3,}/g, '') // Remove separator rows .replace(/\s{2,}/g, ' ') // Normalize whitespace .replace(/^\s*\d+\.\s*/, '') // Remove leading numbers .trim(); } /** * Truncate text to word limit at sentence boundaries * Per Knaflic: Don't cut mid-thought */ private truncateToWordLimit(text: string, maxWords: number): string { const cleaned = this.cleanText(text); const words = cleaned.split(/\s+/); if (words.length <= maxWords) { return cleaned; } // Try to find a sentence boundary within limit const sentences = cleaned.match(/[^.!?]+[.!?]?/g) || [cleaned]; let result = ''; for (const sentence of sentences) { const testResult = result + sentence; if (testResult.split(/\s+/).length <= maxWords) { result = testResult; } else { break; } } // If we got at least one sentence, use it if (result.trim().length > 0) { return result.trim(); } // Otherwise, truncate at word boundary with ellipsis return words.slice(0, maxWords).join(' ') + '...'; } } /** * Initialize and return a SlideGenerator instance */ export async function initSlideGenerator(): Promise { const generator = new SlideGenerator(); await generator.initialize(); return generator; }