packages/features/calAIPhone/providers/retellAI/services/README.md
This directory contains the refactored RetellAI services that follow the Single Responsibility Principle (SRP) to fix the previous GOD CLASS violation.
BEFORE: Single RetellAIService class (849 lines) handling all responsibilities
AFTER: Focused services composed together (~150 lines each)
Responsibility: LLM and Agent configuration management
setupAIConfiguration() - Create LLM and AgentdeleteAIConfiguration() - Clean up AI resourcesupdateLLMConfiguration() - Update LLM settingsgetLLMDetails() - Retrieve LLM informationResponsibility: Agent CRUD operations
createOutboundAgent() - Create new agentsupdateAgent() - Update agent propertiesdeleteAgent() - Remove agentslistAgents() - Get agent listsgetAgentWithDetails() - Get detailed agent infoResponsibility: Stripe payments and subscriptions
generatePhoneNumberCheckoutSession() - Create Stripe checkoutcancelPhoneNumberSubscription() - Cancel subscriptionsResponsibility: Phone call operations
createPhoneCall() - Initiate callscreateTestCall() - Create test calls with credit validationResponsibility: Phone number management with transaction integrity
importPhoneNumber() - Import with compensating transactionsdeletePhoneNumber() - Remove phone numbersupdatePhoneNumberWithAgents() - Assign agentsimport {
AIConfigurationService,
AgentService,
BillingService,
CallService,
PhoneNumberService
} from './services';
// Initialize dependencies
const repository = new RetellSDKClient({ apiKey: 'your-key' });
const agentRepository = new PrismaAgentRepositoryAdapter();
const phoneNumberRepository = new PrismaPhoneNumberRepositoryAdapter();
const transactionManager = new PrismaTransactionAdapter();
// Use individual services
const aiConfigService = new AIConfigurationService(repository);
const agentService = new AgentService(repository, agentRepository);
const billingService = new BillingService(phoneNumberRepository, repository);
// Setup AI configuration
const { llmId, agentId } = await aiConfigService.setupAIConfiguration({
calApiKey: 'cal_live_123...',
timeZone: 'America/New_York',
eventTypeId: 12345,
});
// Create agent
const agent = await agentService.createOutboundAgent({
name: 'Support Agent',
userId: 1,
userTimeZone: 'America/New_York',
setupAIConfiguration: () => aiConfigService.setupAIConfiguration({...})
});
import { RetellAIService } from "../RetellAIService";
// Initialize main service
const service = new RetellAIService(repository, agentRepository, phoneNumberRepository, transactionManager);
// Use composed interface - same as before but internally organized
const { llmId, agentId } = await service.setupAIConfiguration({
calApiKey: "cal_live_123...",
timeZone: "America/New_York",
});
const phoneNumber = await service.importPhoneNumber({
phone_number: "+1234567890",
termination_uri: "https://example.com/webhook",
userId: 1,
agentId: "agent-123",
});
✅ Single Responsibility - Each service has one clear purpose
✅ Easier Testing - Test services in isolation with focused mocks
✅ Better Maintainability - Changes to billing don't affect phone operations
✅ Reusability - Compose services differently for different use cases
✅ Code Navigation - ~150 lines per service vs 849 lines
✅ Backward Compatibility - Existing code continues to work
Each service can be tested independently:
// Test individual service
const aiConfigService = new AIConfigurationService(mockRepository);
await aiConfigService.setupAIConfiguration(config);
// Test main service with all composed services
const service = new RetellAIService(mockRepository, mockAgentRepo, mockPhoneRepo, mockTransaction);
For new code: Use RetellAIService or individual services for specific needs
For existing code: No changes needed - RetellAIService maintains same API
The refactoring eliminates the GOD CLASS antipattern while preserving all functionality and improving code organization.