import { FileBackupStorage, TavusClient, type TavusAccount } from '@buzztrail/accounts-backup'; import * as path from 'path'; export class TavusBackupJob { private fileStorage: FileBackupStorage; constructor() { // Use the backup-manager's public directory so GUI can access files immediately // Use absolute path from project root to ensure GUI and backup service use same location const projectRoot = path.resolve(__dirname, '../../../../../..'); const backupPath = path.join(projectRoot, 'apps/backup-manager/public/backup-data'); this.fileStorage = new FileBackupStorage(backupPath); } async runBackup(options: { environment: string; apiKey: string; customerId?: string; customerName?: string; }): Promise<{ success: boolean; backupId?: string; recordCount: number; error?: string; }> { try { console.log(`🚀 Starting Tavus backup for ${options.environment} environment...`); // Create Tavus account configuration const account: TavusAccount = { id: options.customerId || `customer-${options.environment}`, name: options.customerName || `${options.environment.charAt(0).toUpperCase() + options.environment.slice(1)} Account`, apiKey: options.apiKey, environment: options.environment as 'production' | 'staging' | 'testing' }; // Create Tavus client and fetch data // For testing environment, use real Tavus API to backup real personas // Only use fake server in pure development/mock scenarios const baseUrl = undefined; // Always use real Tavus API for backups console.log(`🚀 BACKUP JOB: Creating TavusClient for ${account.name} (${account.environment}) using baseUrl: ${baseUrl || 'https://tavusapi.com'}`); const client = new TavusClient(account, baseUrl); console.log(`🚀 BACKUP JOB: Calling createFullBackup() for ${account.name}`); const backupData = await client.createFullBackup(); console.log(`🚀 BACKUP JOB: Got backup data with ${backupData.personas.length} personas`); // Generate customer info const customerId = options.customerId || `customer-${options.environment}-${Date.now()}`; const customerName = options.customerName || `${options.environment.charAt(0).toUpperCase() + options.environment.slice(1)} Account`; // Save backup to file storage const backupMetadata = await this.fileStorage.saveTavusBackup( customerId, customerName, options.environment, backupData ); console.log(`✅ Backup completed successfully: ${backupMetadata.backup_id}`); console.log(`📊 Backed up ${backupData.personas.length} personas`); return { success: true, backupId: backupMetadata.backup_id, recordCount: backupData.personas.length }; } catch (error) { console.error(`❌ Backup failed:`, error); return { success: false, recordCount: 0, error: error instanceof Error ? error.message : 'Unknown error' }; } } }