using Jibo.Cloud.Application.Abstractions; using Jibo.Cloud.Application.Services; using Jibo.Cloud.Infrastructure.Audio; using Jibo.Cloud.Infrastructure.Calendar; using Jibo.Cloud.Infrastructure.Commute; using Jibo.Cloud.Infrastructure.Content; using Jibo.Cloud.Infrastructure.Holidays; using Jibo.Cloud.Infrastructure.Media; using Jibo.Cloud.Infrastructure.News; using Jibo.Cloud.Infrastructure.Persistence; using Jibo.Cloud.Infrastructure.Telemetry; using Jibo.Cloud.Infrastructure.Weather; using Jibo.Runtime.Abstractions; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Jibo.Cloud.Infrastructure.DependencyInjection; public static class ServiceCollectionExtensions { public static IServiceCollection AddOpenJiboCloud(this IServiceCollection services, IConfiguration? configuration = null) { var sttOptions = new BufferedAudioSttOptions(); if (configuration is not null) { services.Configure(configuration.GetSection("OpenJibo:Telemetry")); services.Configure(configuration.GetSection("OpenJibo:ProtocolTelemetry")); services.Configure(configuration.GetSection("OpenJibo:TurnTelemetry")); configuration.GetSection("OpenJibo:Stt").Bind(sttOptions); } var openWeatherOptions = new OpenWeatherOptions(); if (configuration is not null) configuration.GetSection("OpenJibo:Weather:OpenWeather").Bind(openWeatherOptions); if (string.IsNullOrWhiteSpace(openWeatherOptions.ApiKey)) openWeatherOptions.ApiKey = Environment.GetEnvironmentVariable("OPENWEATHER_API_KEY"); var newsApiOptions = new NewsApiOptions(); if (configuration is not null) configuration.GetSection("OpenJibo:News:NewsApi").Bind(newsApiOptions); if (string.IsNullOrWhiteSpace(newsApiOptions.ApiKey)) newsApiOptions.ApiKey = Environment.GetEnvironmentVariable("NEWSAPI_KEY"); var holidayOptions = new HolidayCalendarOptions(); if (configuration is not null) configuration.GetSection("OpenJibo:Holiday").Bind(holidayOptions); services.AddSingleton(sttOptions); services.AddSingleton(openWeatherOptions); services.AddSingleton(newsApiOptions); services.AddSingleton(holidayOptions); services.AddHttpClient(); services.AddHttpClient(); services.AddSingleton(provider => new NagerDateHolidayCalendarProvider(provider.GetRequiredService())); services.AddSingleton(provider => new CloudStateCalendarReportProvider(provider.GetRequiredService())); services.AddSingleton(provider => new CloudStateCommuteReportProvider(provider.GetRequiredService())); var statePersistencePath = configuration?["OpenJibo:State:PersistencePath"] ?? Path.Combine(AppContext.BaseDirectory, "App_Data", "cloud-state.json"); var personalMemoryPersistencePath = configuration?["OpenJibo:PersonalMemory:PersistencePath"] ?? Path.Combine(AppContext.BaseDirectory, "App_Data", "personal-memory.json"); var stateBackendKind = ParseBackendKind(configuration?["OpenJibo:State:Backend"]); var personalMemoryBackendKind = ParseBackendKind(configuration?["OpenJibo:PersonalMemory:Backend"]); var stateConnectionString = configuration?["OpenJibo:State:ConnectionString"] ?? Environment.GetEnvironmentVariable("OPENJIBO_STATE_STORAGE_CONNECTION_STRING") ?? Environment.GetEnvironmentVariable("OPENJIBO_STATE_SQL_CONNECTION_STRING"); var personalMemoryConnectionString = configuration?["OpenJibo:PersonalMemory:ConnectionString"] ?? Environment.GetEnvironmentVariable( "OPENJIBO_PERSONAL_MEMORY_STORAGE_CONNECTION_STRING") ?? Environment.GetEnvironmentVariable( "OPENJIBO_PERSONAL_MEMORY_SQL_CONNECTION_STRING"); var mediaOptions = new MediaContentStoreOptions(); if (configuration is not null) configuration.GetSection("OpenJibo:Media").Bind(mediaOptions); if (string.IsNullOrWhiteSpace(mediaOptions.ConnectionString)) mediaOptions.ConnectionString = Environment.GetEnvironmentVariable("OPENJIBO_MEDIA_STORAGE_CONNECTION_STRING"); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(provider => { var snapshotFactory = provider.GetRequiredService(); var holidayCalendarProvider = provider.GetRequiredService(); return new InMemoryCloudStateStore( snapshotFactory.Create(statePersistencePath, stateBackendKind, "cloud-state", stateConnectionString), holidayCalendarProvider); }); services.AddSingleton(provider => { var snapshotFactory = provider.GetRequiredService(); return new InMemoryPersonalMemoryStore(snapshotFactory.Create(personalMemoryPersistencePath, personalMemoryBackendKind, "personal-memory", personalMemoryConnectionString)); }); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(provider => { var factory = provider.GetRequiredService(); return factory.Create(mediaOptions.DirectoryPath, mediaOptions.Backend, mediaOptions.ContainerName, mediaOptions.ConnectionString); }); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); return services; } private static PersistenceBackendKind ParseBackendKind(string? value) { return Enum.TryParse(value, true, out var backendKind) ? backendKind : PersistenceBackendKind.File; } }