using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Routing; namespace MarketAlly.AIPlugin.ClaudeCode; /// /// Extension methods for registering Claude Code services /// public static class ServiceCollectionExtensions { /// /// Adds Claude Code integration services to the dependency injection container /// /// The service collection /// Configuration instance /// The service collection for chaining public static IServiceCollection AddClaudeCodeIntegration( this IServiceCollection services, IConfiguration configuration) { // Register configuration services.Configure(configuration.GetSection("ClaudeCode")); services.Configure(configuration.GetSection("ClaudeCode:RateLimiting")); services.Configure(configuration.GetSection("ClaudeCode:Learning")); // Register core services services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddHttpClient(); // Register learning dependencies (if available) services.AddLearningIntegration(); return services; } /// /// Adds Claude Code integration services with configuration action /// /// The service collection /// Configuration action /// The service collection for chaining public static IServiceCollection AddClaudeCodeIntegration( this IServiceCollection services, Action configureOptions) { services.Configure(configureOptions); // Register core services services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddHttpClient(); // Register learning dependencies (if available) services.AddLearningIntegration(); return services; } /// /// Maps Claude Code endpoints to the application /// /// The web application /// Base route pattern (default: "api/claude-code") /// The web application for chaining public static WebApplication MapClaudeCodeEndpoints( this WebApplication app, string pattern = "api/claude-code") { app.MapControllers(); return app; } /// /// Adds learning integration if the learning package is available /// /// The service collection /// The service collection for chaining private static IServiceCollection AddLearningIntegration(this IServiceCollection services) { try { // Try to register learning services (will work if MarketAlly.AIPlugin.Learning is available) var learningAssembly = AppDomain.CurrentDomain.GetAssemblies() .FirstOrDefault(a => a.GetName().Name == "MarketAlly.AIPlugin.Learning"); if (learningAssembly != null) { // Add learning services using reflection to avoid hard dependency var serviceType = learningAssembly.GetType("MarketAlly.AIPlugin.Learning.ServiceCollectionExtensions"); var method = serviceType?.GetMethod("AddLearningServices"); method?.Invoke(null, new object[] { services }); } } catch { // Learning package not available - continue without learning features } return services; } } /// /// Configuration options for Claude Code integration /// public class ClaudeCodeOptions { /// /// Base URL for the Aizia instance /// public string BaseUrl { get; set; } = "https://localhost:44314"; /// /// API key for authentication /// public string? ApiKey { get; set; } /// /// Tenant ID for multi-tenant scenarios /// public string? TenantId { get; set; } /// /// Maximum number of retries for failed requests /// public int MaxRetries { get; set; } = 3; /// /// Base delay between retries /// public TimeSpan BaseRetryDelay { get; set; } = TimeSpan.FromSeconds(1); /// /// Maximum delay between retries /// public TimeSpan MaxRetryDelay { get; set; } = TimeSpan.FromMinutes(5); /// /// Backoff multiplier for exponential backoff /// public double BackoffMultiplier { get; set; } = 2.0; /// /// Enable detailed logging /// public bool EnableDetailedLogging { get; set; } = false; /// /// Enable rate limiting features /// public bool EnableRateLimiting { get; set; } = true; /// /// Enable learning features /// public bool EnableLearning { get; set; } = true; } /// /// Rate limiting configuration options /// public class RateLimitOptions { /// /// Enable automatic retry when rate limited /// public bool AutoRetry { get; set; } = true; /// /// Maximum time to wait for rate limit reset /// public TimeSpan MaxRetryWait { get; set; } = TimeSpan.FromMinutes(5); /// /// Show warnings when remaining requests fall below this threshold /// public int ShowWarningsAt { get; set; } = 10; /// /// Enable progress indicators during waits /// public bool EnableProgressIndicator { get; set; } = true; /// /// Respect rate limits and implement automatic handling /// public bool RespectRateLimits { get; set; } = true; } /// /// Learning configuration options /// public class LearningOptions { /// /// Enable advanced learning features /// public bool EnableAdvancedLearning { get; set; } = true; /// /// Learning mode (conservative, moderate, aggressive) /// public string LearningMode { get; set; } = "moderate"; /// /// Maximum number of historical insights to consider /// public int MaxHistoricalInsights { get; set; } = 50; /// /// Confidence threshold for recommendations /// public double ConfidenceThreshold { get; set; } = 0.7; /// /// Enable predictive analytics /// public bool EnablePredictiveAnalytics { get; set; } = true; /// /// Session timeout in minutes /// public int SessionTimeoutMinutes { get; set; } = 60; }