/** * Persona Promotion Service * Handles tracking and promoting personas between environments (testing → staging → production) */ import type { TavusPersona } from './tavus-client'; export interface PersonaPromotionInfo { persona_name: string; canonical_name: string; // Normalized name for comparison environments: { testing?: { persona_id: string; persona: TavusPersona; last_updated: string; }; staging?: { persona_id: string; persona: TavusPersona; last_updated: string; }; production?: { persona_id: string; persona: TavusPersona; last_updated: string; }; }; promotion_status: 'testing-only' | 'staging-ready' | 'production-ready' | 'fully-promoted' | 'needs-sync'; last_promotion_date?: string; differences?: { system_prompt_diff?: boolean; voice_settings_diff?: boolean; other_settings_diff?: boolean; }; } export interface PromotionWorkflow { source_environment: 'testing' | 'staging'; target_environment: 'staging' | 'production'; persona_name: string; promotion_type: 'create' | 'update' | 'sync'; changes_summary: string[]; } export class PersonaPromotionService { /** * Normalize persona name for comparison across environments */ private normalizePersonaName(name: string): string { return name.toLowerCase() .trim() .replace(/[^\w\s]/g, '') // Remove special characters .replace(/\s+/g, '-'); // Replace spaces with hyphens } /** * Compare personas across all environments to identify promotion opportunities */ async analyzePersonaPromotion(environmentData: { testing: TavusPersona[]; staging: TavusPersona[]; production: TavusPersona[]; }): Promise { const promotionMap = new Map(); // Process all personas from all environments for (const [envName, personas] of Object.entries(environmentData)) { for (const persona of personas) { const canonicalName = this.normalizePersonaName(persona.persona_name); if (!promotionMap.has(canonicalName)) { promotionMap.set(canonicalName, { persona_name: persona.persona_name, canonical_name: canonicalName, environments: {}, promotion_status: 'testing-only' }); } const info = promotionMap.get(canonicalName); if (!info) continue; const envKey = envName as 'testing' | 'staging' | 'production'; info.environments[envKey] = { persona_id: persona.persona_id, persona: persona, last_updated: persona.updated_at || persona.created_at || new Date().toISOString() }; } } // Analyze promotion status for each persona for (const info of promotionMap.values()) { info.promotion_status = this.determinePromotionStatus(info); info.differences = this.analyzePersonaDifferences(info); } return Array.from(promotionMap.values()); } /** * Determine the promotion status of a persona based on which environments it exists in */ private determinePromotionStatus(info: PersonaPromotionInfo): PersonaPromotionInfo['promotion_status'] { const hasTesting = !!info.environments.testing; const hasStaging = !!info.environments.staging; const hasProduction = !!info.environments.production; if (hasProduction && hasStaging && hasTesting) { // Check if they're in sync const differences = this.analyzePersonaDifferences(info); if (differences && Object.values(differences).some(diff => diff)) { return 'needs-sync'; } return 'fully-promoted'; } if (hasStaging && hasProduction) { return 'fully-promoted'; } if (hasStaging) { return 'production-ready'; } if (hasProduction && !hasStaging) { // Persona exists in production but not staging (unusual) return 'needs-sync'; } return 'staging-ready'; // Only in testing } /** * Analyze differences between the same persona across environments */ private analyzePersonaDifferences(info: PersonaPromotionInfo): PersonaPromotionInfo['differences'] { const environments = Object.values(info.environments).filter(env => env); if (environments.length <= 1) { return undefined; // No comparison possible } const differences: PersonaPromotionInfo['differences'] = {}; // Compare system prompts const prompts = environments.map(env => env.persona.system_prompt || ''); if (new Set(prompts).size > 1) { differences.system_prompt_diff = true; } // Compare voice settings const voiceSettings = environments.map(env => `${env.persona.voice_engine || ''}|${env.persona.voice_id || ''}|${env.persona.language || ''}` ); if (new Set(voiceSettings).size > 1) { differences.voice_settings_diff = true; } // Compare other settings (context, greeting, etc.) const otherSettings = environments.map(env => `${env.persona.context || ''}|${env.persona.greeting || ''}` ); if (new Set(otherSettings).size > 1) { differences.other_settings_diff = true; } return Object.keys(differences).length > 0 ? differences : undefined; } /** * Generate promotion workflows for personas ready to be promoted */ generatePromotionWorkflows(promotionInfo: PersonaPromotionInfo[]): PromotionWorkflow[] { const workflows: PromotionWorkflow[] = []; for (const info of promotionInfo) { if (info.promotion_status === 'staging-ready' && info.environments.testing) { workflows.push({ source_environment: 'testing', target_environment: 'staging', persona_name: info.persona_name, promotion_type: 'create', changes_summary: ['New persona ready for staging deployment'] }); } if (info.promotion_status === 'production-ready' && info.environments.staging) { workflows.push({ source_environment: 'staging', target_environment: 'production', persona_name: info.persona_name, promotion_type: 'create', changes_summary: ['Persona ready for production deployment'] }); } if (info.promotion_status === 'needs-sync' && info.differences) { const changes: string[] = []; if (info.differences.system_prompt_diff) changes.push('System prompt differences detected'); if (info.differences.voice_settings_diff) changes.push('Voice settings differences detected'); if (info.differences.other_settings_diff) changes.push('Other settings differences detected'); // Determine sync direction (usually from testing → staging → production) if (info.environments.testing && info.environments.staging) { workflows.push({ source_environment: 'testing', target_environment: 'staging', persona_name: info.persona_name, promotion_type: 'sync', changes_summary: changes }); } if (info.environments.staging && info.environments.production) { workflows.push({ source_environment: 'staging', target_environment: 'production', persona_name: info.persona_name, promotion_type: 'sync', changes_summary: changes }); } } } return workflows; } /** * Get promotion summary statistics */ getPromotionSummary(promotionInfo: PersonaPromotionInfo[]): { total_personas: number; testing_only: number; staging_ready: number; production_ready: number; fully_promoted: number; needs_sync: number; promotion_workflows_available: number; } { const summary = { total_personas: promotionInfo.length, testing_only: 0, staging_ready: 0, production_ready: 0, fully_promoted: 0, needs_sync: 0, promotion_workflows_available: 0 }; for (const info of promotionInfo) { switch (info.promotion_status) { case 'testing-only': summary.testing_only++; break; case 'staging-ready': summary.staging_ready++; summary.promotion_workflows_available++; break; case 'production-ready': summary.production_ready++; summary.promotion_workflows_available++; break; case 'fully-promoted': summary.fully_promoted++; break; case 'needs-sync': summary.needs_sync++; summary.promotion_workflows_available++; break; } } return summary; } }