using Jibo.Cloud.Application.Abstractions; using Jibo.Cloud.Application.Services; using Jibo.Cloud.Infrastructure.Audio; using Jibo.Cloud.Infrastructure.Content; 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.DependencyInjection; using Microsoft.Extensions.Configuration; 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"); } services.AddSingleton(sttOptions); services.AddSingleton(openWeatherOptions); services.AddSingleton(newsApiOptions); services.AddHttpClient(); services.AddHttpClient(); var statePersistencePath = configuration?["OpenJibo:State:PersistencePath"] ?? Path.Combine(AppContext.BaseDirectory, "App_Data", "cloud-state.json"); services.AddSingleton(_ => new InMemoryCloudStateStore(statePersistencePath)); 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(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); return services; } }