Kylrix Note now features a robust, pluggable AI system that allows for easy integration of multiple AI providers. The system is designed to be extensible, fault-tolerant, and easily configurable.
- AIProvider: Abstract base class for all AI providers
- AIProviderRegistry: Interface for managing providers
- GenerationRequest/Result: Standardized interfaces for AI operations
- AIServiceConfig: Configuration for load balancing and failover
- DefaultAIProviderRegistry: Manages provider registration and health checks
- AIService: Main service that orchestrates multiple providers
- Supports multiple load balancing strategies (round-robin, performance-based, etc.)
- Automatic failover and retry logic
- Status: ✅ Active (Primary)
- Purpose: Development and fallback provider
- Features: Simulated responses, no API key required
- Use case: Development, testing, and when real AI providers are unavailable
- Status: 🔧 Disabled (Available but not active)
- Purpose: Google Gemini AI integration
- Features: Real AI generation, streaming support, multiple models
- Use case: Production AI generation when enabled
The system is currently configured with:
- Primary Provider: Mock (for development safety)
- Secondary Provider: Gemini (disabled, but ready when needed)
- Load Balancing: Round-robin
- Retry Attempts: 2
- Timeout: 30 seconds
import { aiService } from '@/lib/ai-service';
const result = await aiService.generateContent(
"Write about sustainable energy",
"research"
);import { aiProviderRegistry } from '@/lib/ai';
// Enable Gemini provider
aiProviderRegistry.setProviderEnabled('gemini', true);
// Check provider health
const health = await aiService.getServiceHealth();
console.log('Available providers:', health.availableProviders);To add a new AI provider:
- Create Provider Class
export class NewAIProvider extends AIProvider {
readonly id = 'new-provider';
readonly name = 'New AI Provider';
// ... implement required methods
}- Register Provider
import { aiProviderRegistry } from '@/lib/ai';
import { NewAIProvider } from './providers/new-provider';
const provider = new NewAIProvider(config);
aiProviderRegistry.register(provider);- Configure Service
aiService.updateConfig({
primaryProvider: 'new-provider',
fallbackProviders: ['mock', 'gemini']
});- topic: Topic exploration and outlines
- brainstorm: Creative idea generation
- research: Research summaries and analysis
- custom: Custom user prompts
- Automatic health checking
- Usage metrics tracking
- Configuration validation
- Graceful error handling
- Load balancing support
- round-robin: Rotate through providers
- random: Random provider selection
- performance: Use fastest providers first
- least-used: Use providers with lowest usage
interface AIProviderConfig {
apiKey?: string;
baseUrl?: string;
model?: string;
defaultOptions?: GenerationOptions;
enabled: boolean;
}The system provides health monitoring:
const health = await aiService.getServiceHealth();
// Returns: { status, availableProviders, totalProviders, metrics }Each provider tracks:
- Total requests
- Success/failure rates
- Average response time
- Token usage
- Last used timestamp
- API Keys: Stored securely in environment variables
- Validation: All inputs validated before processing
- Rate Limiting: Handled by individual providers
- Error Isolation: Provider failures don't crash the system
- Fallback System: Always has working provider available
- Mock provider is primary (safe, no API costs)
- Real providers disabled by default
- Easy testing and development
- Real AI providers enabled
- Mock provider as fallback
- Full monitoring and metrics
- Automatic failover
Planned improvements:
- OpenAI provider integration
- Anthropic Claude provider
- Provider-specific caching
- Real-time provider switching UI
- Advanced prompt optimization
- Multi-provider result comparison
This new system replaces the previous direct Gemini integration. The API surface remains the same for existing components, ensuring backward compatibility while providing much more flexibility and reliability.