| | | 1 | | using System.Text; |
| | | 2 | | using EHonda.KicktippAi.Core; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 5 | | using Microsoft.Extensions.FileProviders; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using OpenTelemetry.Exporter; |
| | | 8 | | using OpenTelemetry.Resources; |
| | | 9 | | using OpenTelemetry.Trace; |
| | | 10 | | using OpenAiIntegration; |
| | | 11 | | using Orchestrator.Infrastructure.Factories; |
| | | 12 | | using Orchestrator.Services; |
| | | 13 | | |
| | | 14 | | namespace Orchestrator.Infrastructure; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Extension methods for registering Orchestrator services in dependency injection. |
| | | 18 | | /// </summary> |
| | | 19 | | public static class ServiceRegistrationExtensions |
| | | 20 | | { |
| | | 21 | | private const string LangfuseIngestionVersionHeaderName = "x-langfuse-ingestion-version"; |
| | | 22 | | private const string LangfuseIngestionVersionHeaderValue = "4"; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Registers all shared infrastructure services (factories, logging). |
| | | 26 | | /// </summary> |
| | | 27 | | /// <remarks> |
| | | 28 | | /// This method is idempotent - calling it multiple times has no additional effect. |
| | | 29 | | /// </remarks> |
| | | 30 | | public static IServiceCollection AddOrchestratorInfrastructure(this IServiceCollection services) |
| | | 31 | | { |
| | | 32 | | // Add logging (idempotent via TryAdd internally) |
| | 1 | 33 | | services.AddLogging(builder => |
| | 1 | 34 | | { |
| | 1 | 35 | | builder.SetMinimumLevel(LogLevel.Information); |
| | 1 | 36 | | }); |
| | | 37 | | |
| | | 38 | | // Add memory cache for Kicktipp client |
| | 1 | 39 | | services.AddMemoryCache(); |
| | | 40 | | |
| | | 41 | | // Add HTTP client factory for Kicktipp |
| | 1 | 42 | | services.AddHttpClient(); |
| | | 43 | | |
| | | 44 | | // Register factories (idempotent) |
| | 1 | 45 | | services.TryAddSingleton<IFirebaseServiceFactory, FirebaseServiceFactory>(); |
| | 1 | 46 | | services.TryAddSingleton<IKicktippClientFactory, KicktippClientFactory>(); |
| | 1 | 47 | | services.TryAddSingleton<IOpenAiServiceFactory, OpenAiServiceFactory>(); |
| | 1 | 48 | | services.TryAddSingleton<IContextProviderFactory, ContextProviderFactory>(); |
| | 1 | 49 | | services.TryAddTransient<MatchOutcomeCollectionService>(); |
| | | 50 | | |
| | | 51 | | // Register Langfuse/OTel tracing (no-op if credentials are absent) |
| | 1 | 52 | | services.AddLangfuseTracing(); |
| | | 53 | | |
| | 1 | 54 | | return services; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Registers the OpenTelemetry tracing pipeline with the Langfuse OTLP endpoint. |
| | | 59 | | /// If <c>LANGFUSE_PUBLIC_KEY</c> or <c>LANGFUSE_SECRET_KEY</c> are not set, |
| | | 60 | | /// no pipeline is registered and all <see cref="System.Diagnostics.ActivitySource.StartActivity(string)"/> |
| | | 61 | | /// calls return <c>null</c> (graceful degradation). |
| | | 62 | | /// </summary> |
| | | 63 | | /// <remarks> |
| | | 64 | | /// <para> |
| | | 65 | | /// Uses the standard <c>AddOpenTelemetry()</c> API from <c>OpenTelemetry.Extensions.Hosting</c>, |
| | | 66 | | /// which registers the <see cref="TracerProvider"/> as a DI-managed singleton and an |
| | | 67 | | /// <see cref="Microsoft.Extensions.Hosting.IHostedService"/> that triggers provider construction. |
| | | 68 | | /// Since this app uses Spectre.Console.Cli (no <c>IHost</c>), the <see cref="TypeRegistrar"/> |
| | | 69 | | /// manually starts hosted services after building the <c>ServiceProvider</c>. |
| | | 70 | | /// </para> |
| | | 71 | | /// <para> |
| | | 72 | | /// This method is idempotent — multiple calls have no additional effect. |
| | | 73 | | /// </para> |
| | | 74 | | /// </remarks> |
| | | 75 | | public static IServiceCollection AddLangfuseTracing(this IServiceCollection services) |
| | | 76 | | { |
| | | 77 | | // Idempotency: skip if tracing has already been registered. |
| | 1 | 78 | | if (_langfuseTracingRegistered) |
| | 1 | 79 | | return services; |
| | | 80 | | |
| | 1 | 81 | | var publicKey = Environment.GetEnvironmentVariable("LANGFUSE_PUBLIC_KEY"); |
| | 1 | 82 | | var secretKey = Environment.GetEnvironmentVariable("LANGFUSE_SECRET_KEY"); |
| | | 83 | | |
| | 1 | 84 | | if (string.IsNullOrEmpty(publicKey) || string.IsNullOrEmpty(secretKey)) |
| | | 85 | | { |
| | | 86 | | // No credentials — skip OTel registration entirely |
| | 1 | 87 | | return services; |
| | | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | _langfuseTracingRegistered = true; |
| | | 91 | | |
| | 1 | 92 | | var baseUrl = Environment.GetEnvironmentVariable("LANGFUSE_BASE_URL") ?? "https://cloud.langfuse.com"; |
| | 1 | 93 | | var headers = BuildLangfuseOtlpHeaders(publicKey, secretKey); |
| | | 94 | | |
| | | 95 | | // NOTE: Setting options.Endpoint programmatically sets AppendSignalPathToEndpoint = false, |
| | | 96 | | // so the full URL including /v1/traces must be provided. |
| | 1 | 97 | | services.AddOpenTelemetry() |
| | 0 | 98 | | .ConfigureResource(r => r.AddService("KicktippAi")) |
| | 1 | 99 | | .WithTracing(tracing => tracing |
| | 1 | 100 | | .AddSource(Telemetry.Source.Name) |
| | 1 | 101 | | .AddProcessor(new LangfuseBaggageSpanProcessor()) |
| | 1 | 102 | | .AddOtlpExporter(options => |
| | 1 | 103 | | { |
| | 0 | 104 | | options.Endpoint = new Uri($"{baseUrl}/api/public/otel/v1/traces"); |
| | 0 | 105 | | options.Protocol = OtlpExportProtocol.HttpProtobuf; |
| | 0 | 106 | | options.Headers = headers; |
| | 0 | 107 | | })); |
| | | 108 | | |
| | 1 | 109 | | return services; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | internal static string BuildLangfuseOtlpHeaders(string publicKey, string secretKey) |
| | | 113 | | { |
| | 1 | 114 | | var authorization = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{publicKey}:{secretKey}")); |
| | 1 | 115 | | return string.Join(",", |
| | 1 | 116 | | $"Authorization=Basic {authorization}", |
| | 1 | 117 | | $"{LangfuseIngestionVersionHeaderName}={LangfuseIngestionVersionHeaderValue}"); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | private static bool _langfuseTracingRegistered; |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Registers services specific to the ListKpiCommand. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <remarks> |
| | | 126 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 127 | | /// </remarks> |
| | | 128 | | public static IServiceCollection AddListKpiCommandServices(this IServiceCollection services) |
| | | 129 | | { |
| | 1 | 130 | | services.AddOrchestratorInfrastructure(); |
| | | 131 | | |
| | | 132 | | // ListKpiCommand only needs Firebase factory (uses IKpiRepository) |
| | | 133 | | // No command-specific keyed services needed - factory pattern handles runtime config |
| | | 134 | | |
| | 1 | 135 | | return services; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Service key for the KPI documents file provider. |
| | | 140 | | /// </summary> |
| | | 141 | | public const string KpiDocumentsFileProviderKey = "kpi-documents"; |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Service key for the transfers documents file provider. |
| | | 145 | | /// </summary> |
| | | 146 | | public const string TransfersDocumentsFileProviderKey = "transfers-documents"; |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Registers services specific to the UploadKpiCommand. |
| | | 150 | | /// </summary> |
| | | 151 | | /// <remarks> |
| | | 152 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 153 | | /// </remarks> |
| | | 154 | | public static IServiceCollection AddUploadKpiCommandServices(this IServiceCollection services) |
| | | 155 | | { |
| | 1 | 156 | | services.AddOrchestratorInfrastructure(); |
| | | 157 | | |
| | | 158 | | // UploadKpiCommand only needs Firebase factory (uses IKpiRepository) |
| | | 159 | | // Register keyed file provider for KPI documents directory |
| | 1 | 160 | | services.TryAddKeyedSingleton<IFileProvider>( |
| | 1 | 161 | | KpiDocumentsFileProviderKey, |
| | 0 | 162 | | (_, _) => SolutionRelativeFileProvider.Create("kpi-documents")); |
| | | 163 | | |
| | 1 | 164 | | return services; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Registers services specific to the CostCommand. |
| | | 169 | | /// </summary> |
| | | 170 | | /// <remarks> |
| | | 171 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 172 | | /// </remarks> |
| | | 173 | | public static IServiceCollection AddCostCommandServices(this IServiceCollection services) |
| | | 174 | | { |
| | 1 | 175 | | services.AddOrchestratorInfrastructure(); |
| | | 176 | | |
| | | 177 | | // CostCommand needs Firebase factory (uses IPredictionRepository, FirestoreDb) |
| | | 178 | | // No command-specific keyed services needed - factory pattern handles runtime config |
| | | 179 | | |
| | 1 | 180 | | return services; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Registers services specific to the MatchdayCommand. |
| | | 185 | | /// </summary> |
| | | 186 | | /// <remarks> |
| | | 187 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 188 | | /// </remarks> |
| | | 189 | | public static IServiceCollection AddMatchdayCommandServices(this IServiceCollection services) |
| | | 190 | | { |
| | 1 | 191 | | services.AddOrchestratorInfrastructure(); |
| | | 192 | | |
| | | 193 | | // MatchdayCommand needs all factories: |
| | | 194 | | // - Firebase (IPredictionRepository, IContextRepository) |
| | | 195 | | // - Kicktipp (IKicktippClient) |
| | | 196 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 197 | | // Factory pattern handles runtime config based on settings |
| | | 198 | | |
| | 1 | 199 | | return services; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Registers services specific to the RandomMatchCommand. |
| | | 204 | | /// </summary> |
| | | 205 | | /// <remarks> |
| | | 206 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 207 | | /// </remarks> |
| | | 208 | | public static IServiceCollection AddRandomMatchCommandServices(this IServiceCollection services) |
| | | 209 | | { |
| | 1 | 210 | | services.AddOrchestratorInfrastructure(); |
| | | 211 | | |
| | | 212 | | // RandomMatchCommand needs the same factories as MatchdayCommand: |
| | | 213 | | // - Firebase (IPredictionRepository, IContextRepository) |
| | | 214 | | // - Kicktipp (IKicktippClient) |
| | | 215 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 216 | | |
| | 1 | 217 | | return services; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Registers services specific to the BonusCommand. |
| | | 222 | | /// </summary> |
| | | 223 | | /// <remarks> |
| | | 224 | | /// This method is idempotent and ensures infrastructure is registered. |
| | | 225 | | /// </remarks> |
| | | 226 | | public static IServiceCollection AddBonusCommandServices(this IServiceCollection services) |
| | | 227 | | { |
| | 1 | 228 | | services.AddOrchestratorInfrastructure(); |
| | | 229 | | |
| | | 230 | | // BonusCommand needs all factories: |
| | | 231 | | // - Firebase (IPredictionRepository, IKpiRepository) |
| | | 232 | | // - Kicktipp (IKicktippClient) |
| | | 233 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 234 | | |
| | 1 | 235 | | return services; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// Registers services specific to the VerifyMatchdayCommand. |
| | | 240 | | /// </summary> |
| | | 241 | | public static IServiceCollection AddVerifyMatchdayCommandServices(this IServiceCollection services) |
| | | 242 | | { |
| | 1 | 243 | | services.AddOrchestratorInfrastructure(); |
| | | 244 | | |
| | | 245 | | // VerifyMatchdayCommand needs: |
| | | 246 | | // - Firebase (IPredictionRepository, IContextRepository) |
| | | 247 | | // - Kicktipp (IKicktippClient) |
| | | 248 | | |
| | 1 | 249 | | return services; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Registers services specific to the VerifyBonusCommand. |
| | | 254 | | /// </summary> |
| | | 255 | | public static IServiceCollection AddVerifyBonusCommandServices(this IServiceCollection services) |
| | | 256 | | { |
| | 1 | 257 | | services.AddOrchestratorInfrastructure(); |
| | | 258 | | |
| | | 259 | | // VerifyBonusCommand needs: |
| | | 260 | | // - Firebase (IPredictionRepository, IKpiRepository) |
| | | 261 | | // - Kicktipp (IKicktippClient) |
| | | 262 | | |
| | 1 | 263 | | return services; |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Registers services specific to the CollectContextKicktippCommand. |
| | | 268 | | /// </summary> |
| | | 269 | | public static IServiceCollection AddCollectContextKicktippCommandServices(this IServiceCollection services) |
| | | 270 | | { |
| | 1 | 271 | | services.AddOrchestratorInfrastructure(); |
| | | 272 | | |
| | | 273 | | // CollectContextKicktippCommand needs: |
| | | 274 | | // - Firebase (IContextRepository, IMatchOutcomeRepository) |
| | | 275 | | // - Kicktipp (IKicktippClient) |
| | | 276 | | |
| | 1 | 277 | | return services; |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Registers services specific to the ContextChangesCommand. |
| | | 282 | | /// </summary> |
| | | 283 | | public static IServiceCollection AddContextChangesCommandServices(this IServiceCollection services) |
| | | 284 | | { |
| | 1 | 285 | | services.AddOrchestratorInfrastructure(); |
| | | 286 | | |
| | | 287 | | // ContextChangesCommand only needs Firebase (IContextRepository) |
| | | 288 | | |
| | 1 | 289 | | return services; |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Registers services specific to the UploadTransfersCommand. |
| | | 294 | | /// </summary> |
| | | 295 | | public static IServiceCollection AddUploadTransfersCommandServices(this IServiceCollection services) |
| | | 296 | | { |
| | 1 | 297 | | services.AddOrchestratorInfrastructure(); |
| | | 298 | | |
| | | 299 | | // UploadTransfersCommand needs Firebase (IContextRepository) |
| | | 300 | | // Register keyed file provider for transfers documents directory |
| | 1 | 301 | | services.TryAddKeyedSingleton<IFileProvider>( |
| | 1 | 302 | | TransfersDocumentsFileProviderKey, |
| | 0 | 303 | | (_, _) => SolutionRelativeFileProvider.Create("transfers-documents")); |
| | | 304 | | |
| | 1 | 305 | | return services; |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | /// <summary> |
| | | 309 | | /// Registers services specific to the AnalyzeMatchDetailedCommand. |
| | | 310 | | /// </summary> |
| | | 311 | | public static IServiceCollection AddAnalyzeMatchDetailedCommandServices(this IServiceCollection services) |
| | | 312 | | { |
| | 1 | 313 | | services.AddOrchestratorInfrastructure(); |
| | | 314 | | |
| | | 315 | | // AnalyzeMatchDetailedCommand needs: |
| | | 316 | | // - Firebase (IContextRepository) |
| | | 317 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 318 | | |
| | 1 | 319 | | return services; |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | /// <summary> |
| | | 323 | | /// Registers services specific to the AnalyzeMatchComparisonCommand. |
| | | 324 | | /// </summary> |
| | | 325 | | public static IServiceCollection AddAnalyzeMatchComparisonCommandServices(this IServiceCollection services) |
| | | 326 | | { |
| | 1 | 327 | | services.AddOrchestratorInfrastructure(); |
| | | 328 | | |
| | | 329 | | // AnalyzeMatchComparisonCommand needs: |
| | | 330 | | // - Firebase (IContextRepository) |
| | | 331 | | // - OpenAI (IPredictionService, ITokenUsageTracker) |
| | | 332 | | |
| | 1 | 333 | | return services; |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Registers all command services. Useful for production setup. |
| | | 338 | | /// </summary> |
| | | 339 | | public static IServiceCollection AddAllCommandServices(this IServiceCollection services) |
| | | 340 | | { |
| | | 341 | | // Infrastructure is added by each command method, but we call it first for clarity |
| | 1 | 342 | | services.AddOrchestratorInfrastructure(); |
| | | 343 | | |
| | | 344 | | // Register all command-specific services |
| | 1 | 345 | | services.AddListKpiCommandServices(); |
| | 1 | 346 | | services.AddUploadKpiCommandServices(); |
| | 1 | 347 | | services.AddCostCommandServices(); |
| | 1 | 348 | | services.AddMatchdayCommandServices(); |
| | 1 | 349 | | services.AddRandomMatchCommandServices(); |
| | 1 | 350 | | services.AddBonusCommandServices(); |
| | 1 | 351 | | services.AddVerifyMatchdayCommandServices(); |
| | 1 | 352 | | services.AddVerifyBonusCommandServices(); |
| | 1 | 353 | | services.AddCollectContextKicktippCommandServices(); |
| | 1 | 354 | | services.AddContextChangesCommandServices(); |
| | 1 | 355 | | services.AddUploadTransfersCommandServices(); |
| | 1 | 356 | | services.AddAnalyzeMatchDetailedCommandServices(); |
| | 1 | 357 | | services.AddAnalyzeMatchComparisonCommandServices(); |
| | | 358 | | |
| | 1 | 359 | | return services; |
| | | 360 | | } |
| | | 361 | | } |