MarketAlly.AIPlugin.Extensions/MarketAlly.AIPlugin.ClaudeCode/ServiceCollectionExtensions.cs

229 lines
7.4 KiB
C#
Executable File

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
namespace MarketAlly.AIPlugin.ClaudeCode;
/// <summary>
/// Extension methods for registering Claude Code services
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds Claude Code integration services to the dependency injection container
/// </summary>
/// <param name="services">The service collection</param>
/// <param name="configuration">Configuration instance</param>
/// <returns>The service collection for chaining</returns>
public static IServiceCollection AddClaudeCodeIntegration(
this IServiceCollection services,
IConfiguration configuration)
{
// Register configuration
services.Configure<ClaudeCodeOptions>(configuration.GetSection("ClaudeCode"));
services.Configure<RateLimitOptions>(configuration.GetSection("ClaudeCode:RateLimiting"));
services.Configure<LearningOptions>(configuration.GetSection("ClaudeCode:Learning"));
// Register core services
services.AddScoped<IClaudeCodeService, ClaudeCodeService>();
services.AddScoped<IContextClaudeService, ContextClaudeService>();
services.AddScoped<IChatService, ChatService>();
services.AddHttpClient<IRateLimitAwareHttpClient, RateLimitAwareHttpClient>();
// Register learning dependencies (if available)
services.AddLearningIntegration();
return services;
}
/// <summary>
/// Adds Claude Code integration services with configuration action
/// </summary>
/// <param name="services">The service collection</param>
/// <param name="configureOptions">Configuration action</param>
/// <returns>The service collection for chaining</returns>
public static IServiceCollection AddClaudeCodeIntegration(
this IServiceCollection services,
Action<ClaudeCodeOptions> configureOptions)
{
services.Configure(configureOptions);
// Register core services
services.AddScoped<IClaudeCodeService, ClaudeCodeService>();
services.AddScoped<IContextClaudeService, ContextClaudeService>();
services.AddScoped<IChatService, ChatService>();
services.AddHttpClient<IRateLimitAwareHttpClient, RateLimitAwareHttpClient>();
// Register learning dependencies (if available)
services.AddLearningIntegration();
return services;
}
/// <summary>
/// Maps Claude Code endpoints to the application
/// </summary>
/// <param name="app">The web application</param>
/// <param name="pattern">Base route pattern (default: "api/claude-code")</param>
/// <returns>The web application for chaining</returns>
public static WebApplication MapClaudeCodeEndpoints(
this WebApplication app,
string pattern = "api/claude-code")
{
app.MapControllers();
return app;
}
/// <summary>
/// Adds learning integration if the learning package is available
/// </summary>
/// <param name="services">The service collection</param>
/// <returns>The service collection for chaining</returns>
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;
}
}
/// <summary>
/// Configuration options for Claude Code integration
/// </summary>
public class ClaudeCodeOptions
{
/// <summary>
/// Base URL for the Aizia instance
/// </summary>
public string BaseUrl { get; set; } = "https://localhost:44314";
/// <summary>
/// API key for authentication
/// </summary>
public string? ApiKey { get; set; }
/// <summary>
/// Tenant ID for multi-tenant scenarios
/// </summary>
public string? TenantId { get; set; }
/// <summary>
/// Maximum number of retries for failed requests
/// </summary>
public int MaxRetries { get; set; } = 3;
/// <summary>
/// Base delay between retries
/// </summary>
public TimeSpan BaseRetryDelay { get; set; } = TimeSpan.FromSeconds(1);
/// <summary>
/// Maximum delay between retries
/// </summary>
public TimeSpan MaxRetryDelay { get; set; } = TimeSpan.FromMinutes(5);
/// <summary>
/// Backoff multiplier for exponential backoff
/// </summary>
public double BackoffMultiplier { get; set; } = 2.0;
/// <summary>
/// Enable detailed logging
/// </summary>
public bool EnableDetailedLogging { get; set; } = false;
/// <summary>
/// Enable rate limiting features
/// </summary>
public bool EnableRateLimiting { get; set; } = true;
/// <summary>
/// Enable learning features
/// </summary>
public bool EnableLearning { get; set; } = true;
}
/// <summary>
/// Rate limiting configuration options
/// </summary>
public class RateLimitOptions
{
/// <summary>
/// Enable automatic retry when rate limited
/// </summary>
public bool AutoRetry { get; set; } = true;
/// <summary>
/// Maximum time to wait for rate limit reset
/// </summary>
public TimeSpan MaxRetryWait { get; set; } = TimeSpan.FromMinutes(5);
/// <summary>
/// Show warnings when remaining requests fall below this threshold
/// </summary>
public int ShowWarningsAt { get; set; } = 10;
/// <summary>
/// Enable progress indicators during waits
/// </summary>
public bool EnableProgressIndicator { get; set; } = true;
/// <summary>
/// Respect rate limits and implement automatic handling
/// </summary>
public bool RespectRateLimits { get; set; } = true;
}
/// <summary>
/// Learning configuration options
/// </summary>
public class LearningOptions
{
/// <summary>
/// Enable advanced learning features
/// </summary>
public bool EnableAdvancedLearning { get; set; } = true;
/// <summary>
/// Learning mode (conservative, moderate, aggressive)
/// </summary>
public string LearningMode { get; set; } = "moderate";
/// <summary>
/// Maximum number of historical insights to consider
/// </summary>
public int MaxHistoricalInsights { get; set; } = 50;
/// <summary>
/// Confidence threshold for recommendations
/// </summary>
public double ConfidenceThreshold { get; set; } = 0.7;
/// <summary>
/// Enable predictive analytics
/// </summary>
public bool EnablePredictiveAnalytics { get; set; } = true;
/// <summary>
/// Session timeout in minutes
/// </summary>
public int SessionTimeoutMinutes { get; set; } = 60;
}