Skip to content

Commit

Permalink
Merge branch 'main' into @hramos/FixWarnings/Encamina.Enmarcha.Core
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoRamosEs committed May 8, 2024
2 parents 72f91fc + 4640318 commit 230c964
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IMetadataProcessor
/// <returns>
/// A read only collection of answers processed with given metadata options.
/// </returns>
Task<IReadOnlyCollection<IAnswer>> ProcessAnswersAsync(IEnumerable<IAnswer> answers, MetadataOptions metadataOptions, CancellationToken cancellationToken);
Task<IReadOnlyCollection<IAnswer>> ProcessAnswersAsync(IEnumerable<IAnswer> answers, MetadataOptions? metadataOptions, CancellationToken cancellationToken);

/// <summary>
/// Process a message to obtain metadata options from it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public interface IQuestionRequestOptions
/// <summary>
/// Gets the metadata options.
/// </summary>
MetadataOptions MetadataOptions { get; }
MetadataOptions? MetadataOptions { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public QuestionRequestOptions(IEnumerable<string> sources)
public virtual IReadOnlyCollection<string> Sources { get; } = Array.Empty<string>();

/// <inheritdoc/>
public virtual MetadataOptions MetadataOptions { get; init; }
public virtual MetadataOptions? MetadataOptions { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface ITextTranslationRequest
/// <summary>
/// Gets the language to translate from.
/// </summary>
CultureInfo FromLanguage { get; init; }
CultureInfo? FromLanguage { get; init; }

/// <summary>
/// Gets the collection of languages to translate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Encamina.Enmarcha.AI.TextsTranslation.Abstractions;
public class TextTranslationRequest : ITextTranslationRequest
{
/// <inheritdoc/>
public virtual CultureInfo FromLanguage { get; init; }
public virtual CultureInfo? FromLanguage { get; init; }

/// <inheritdoc/>
public virtual ICollection<CultureInfo> ToLanguages { get; init; } = new List<CultureInfo>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ public virtual bool TryGetByName(string dialogName, out Dialog? dialog)
public virtual T? GetByType<T>(Func<Dialog, bool>? filterExpression = null) where T : Dialog => GetDialogByType<T>(filterExpression);

/// <inheritdoc/>
public virtual bool TryGetByType(Type dialogType, out Dialog dialog, Func<Dialog, bool>? filterExpression = null)
public virtual bool TryGetByType(Type dialogType, out Dialog? dialog, Func<Dialog, bool>? filterExpression = null)
{
dialog = GetDialogByType(dialogType, filterExpression);
return dialog != null;
}

/// <inheritdoc/>
public virtual bool TryGetByType<T>(out T dialog, Func<Dialog, bool>? filterExpression = null) where T : Dialog
public virtual bool TryGetByType<T>(out T? dialog, Func<Dialog, bool>? filterExpression = null) where T : Dialog
{
dialog = GetDialogByType<T>(filterExpression);
return dialog != null;
Expand Down Expand Up @@ -169,7 +169,7 @@ private static IDictionary<string, Type> BuildFromIntent(IEnumerable<Dialog> dia
return result;
}

private Dialog? GetDialogByType(Type dialogType, Func<Dialog, bool> filterPredicate)
private Dialog? GetDialogByType(Type? dialogType, Func<Dialog, bool>? filterPredicate)
{
if (dialogType == null)
{
Expand All @@ -180,7 +180,7 @@ private static IDictionary<string, Type> BuildFromIntent(IEnumerable<Dialog> dia
return (serviceScope.ServiceProvider.GetServices(dialogType) as IEnumerable<Dialog>)?.FirstOrDefault(d => filterPredicate == null || filterPredicate(d));
}

private T? GetDialogByType<T>(Func<Dialog, bool> filterPredicate) where T : Dialog
private T? GetDialogByType<T>(Func<Dialog, bool>? filterPredicate) where T : Dialog
{
using var serviceScope = serviceProvider.CreateScope();
return serviceScope.ServiceProvider.GetServices<T>()?.FirstOrDefault(d => filterPredicate == null || filterPredicate(d));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface IDialogTypeProvider
/// <returns>
/// Returns <see langword="true"/> if a valid <see cref="Dialog"/> is found by its type; otherwise, returns <see langword="false"/>.
/// </returns>
bool TryGetByType(Type dialogType, out Dialog dialog, Func<Dialog, bool>? filterExpression = null);
bool TryGetByType(Type dialogType, out Dialog? dialog, Func<Dialog, bool>? filterExpression = null);

/// <summary>
/// Tries to get a <see cref="Dialog"/> by its type from a generic type parameter.
Expand All @@ -57,7 +57,7 @@ public interface IDialogTypeProvider
/// <returns>
/// Returns <see langword="true"/> if a valid <see cref="Dialog"/> is found by its type; otherwise, returns <see langword="false"/>.
/// </returns>
bool TryGetByType<T>(out T dialog, Func<Dialog, bool>? filterExpression = null) where T : Dialog;
bool TryGetByType<T>(out T? dialog, Func<Dialog, bool>? filterExpression = null) where T : Dialog;
}

#pragma warning restore S2360 // Optional parameters should not be used
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class NamedComponentDialogBase : ComponentDialog, INameableDialog
/// <summary>
/// Initializes a new instance of the <see cref="NamedComponentDialogBase"/> class.
/// </summary>
protected NamedComponentDialogBase() : this(null!)
protected NamedComponentDialogBase() : this(null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NamedComponentDialogBase"/> class.
/// </summary>
/// <param name="dialogId">The unique identifier for this dialog.</param>
protected NamedComponentDialogBase(string dialogId) : base(dialogId)
protected NamedComponentDialogBase(string? dialogId) : base(dialogId)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public abstract class NamedDialogBase : Dialog, INameableDialog
/// <summary>
/// Initializes a new instance of the <see cref="NamedDialogBase"/> class.
/// </summary>
protected NamedDialogBase() : this(null!)
protected NamedDialogBase() : this(null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NamedDialogBase"/> class.
/// </summary>
/// <param name="dialogId">The unique identifier for this dialog.</param>
protected NamedDialogBase(string dialogId) : base(dialogId)
protected NamedDialogBase(string? dialogId) : base(dialogId)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Encamina.Enmarcha.AI.Abstractions;
#pragma warning disable S2360 // Optional parameters should not be used

using Encamina.Enmarcha.AI.Abstractions;
using Encamina.Enmarcha.AI.QuestionsAnswering.Abstractions;

using Encamina.Enmarcha.Bot.Skills.QuestionAnswering;
Expand All @@ -11,8 +13,6 @@

using ExceptionMessages = Encamina.Enmarcha.Bot.Skills.QuestionAnswering.Resources.ExceptionMessages;

#pragma warning disable S2360 // Optional parameters should not be used

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public QuestionAnsweringDialog(string id, QuestionAnsweringDialogServices questi
public override string Name => string.IsNullOrWhiteSpace(configurationOptions.DialogName) ? base.Name : configurationOptions.DialogName;

/// <inheritdoc/>
public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object? options = null, CancellationToken cancellationToken = default)
public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default)
{
ResourceResponse? response = null;

Expand All @@ -85,7 +85,7 @@ public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc,
return await dc.EndDialogAsync(response, cancellationToken);
}

private static IActivity BuildMessageActivity(string message, bool withVerbose, IEnumerable<IAnswer> answers)
private static IActivity BuildMessageActivity(string message, bool withVerbose, IEnumerable<IAnswer>? answers)
{
var activity = MessageFactory.Text(message, message);

Expand All @@ -102,7 +102,7 @@ private static bool VerboseFromTurnContextValue(ITurnContext turnContext)
return turnContext.Activity.Value is JObject activityValue && (activityValue[Constants.ActivityValueVerbose]?.Value<bool>() ?? false);
}

private async Task<QuestionRequest> BuildQuestionRequestAsync(string question, string userId, MetadataOptions metadataOptions, IEnumerable<string> sources, CancellationToken cancellationToken)
private async Task<QuestionRequest> BuildQuestionRequestAsync(string question, string userId, MetadataOptions? metadataOptions, IEnumerable<string> sources, CancellationToken cancellationToken)
{
var questionRequestOptions = new QuestionRequestOptions(sources)
{
Expand All @@ -119,7 +119,7 @@ private async Task<QuestionRequest> BuildQuestionRequestAsync(string question, s
};
}

private async Task<IEnumerable<IAnswer>> ProcessQuestionResultAsync(QuestionResult result, MetadataOptions metadataOptions, IEnumerable<string> sources, CancellationToken cancellationToken)
private async Task<IEnumerable<IAnswer>> ProcessQuestionResultAsync(QuestionResult result, MetadataOptions? metadataOptions, IEnumerable<string> sources, CancellationToken cancellationToken)
{
IEnumerable<IAnswer> answers = result.Answers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected BotCloudAdapterWithErrorHandlerBase(IBotAdapterOptions<BotCloudAdapter
/// <summary>
/// Gets this adapter's options.
/// </summary>
protected virtual IBotAdapterOptions<BotCloudAdapterWithErrorHandlerBase> Options { get; init; }
protected virtual IBotAdapterOptions<BotCloudAdapterWithErrorHandlerBase>? Options { get; init; }

/// <summary>
/// An error handler that can catch exceptions in the middleware or application.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Globalization;
#pragma warning disable S2360 // Optional parameters should not be used

using System.Globalization;

using Encamina.Enmarcha.AI;

Expand All @@ -11,7 +13,6 @@
using Encamina.Enmarcha.Bot.Responses;

using Encamina.Enmarcha.Core.Extensions;
#pragma warning disable S2360 // Optional parameters should not be used

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
Expand Down Expand Up @@ -71,7 +72,7 @@ public static IServiceCollection AddBotState<TBotState>(this IServiceCollection
}

/// <summary>
/// Adds a bot transcript logger middleware as singleton using a memory trasncript store.
/// Adds a bot transcript logger middleware as singleton using a memory transcript store.
/// </summary>
/// <remarks>This extension method uses the <see cref="MemoryTranscriptStore"/> transcript store.</remarks>
/// <param name="services"> The <see cref="IServiceCollection"/> to add services to.</param>
Expand All @@ -84,7 +85,7 @@ public static IServiceCollection AddBotTranscriptLoggerMiddlewareWithInMemorySto
}

/// <summary>
/// Adds a bot transcript logger middleware as singleton using a Blob container as trasncript logger (and store).
/// Adds a bot transcript logger middleware as singleton using a Blob container as transcript logger (and store).
/// </summary>
/// <remarks>This extension method uses the <see cref="BlobsTranscriptStore"/> transcript store.</remarks>
/// <param name="services"> The <see cref="IServiceCollection"/> to add services to.</param>
Expand Down Expand Up @@ -114,13 +115,13 @@ public static IServiceCollection AddBotTranscriptLoggerMiddlewareWithBlobStore<T
}

/// <summary>
/// Adds a bot transcript logger middleware as singleton using a file storage as trasncript store.
/// Adds a bot transcript logger middleware as singleton using a file storage as transcript store.
/// </summary>
/// <remarks>This extension method uses the <see cref="MemoryTranscriptStore"/> transcript store.</remarks>
/// <param name="services"> The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="folder">A folder path to place the transcript files.</param>
/// <param name="overwriteTranscriptFiles">
/// A flag to indicate if transcript files should be overriten or not. This is usually helpful for unit test scenariuos. Default value is <see langword="false"/>.
/// A flag to indicate if transcript files should be overwritten or not. This is usually helpful for unit test scenarios. Default value is <see langword="false"/>.
/// </param>
/// <param name="serviceLifetime">The lifetime for the <see cref="MemoryTranscriptStore"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
Expand Down Expand Up @@ -226,7 +227,7 @@ public static IServiceCollection AddBotStorageBlob(this IServiceCollection servi
/// <summary>
/// Adds a bot middleware.
/// </summary>
/// <typeparam name="TBotMiddleware">The type of a specific bot middlerware. It must implement interface <see cref="IMiddleware"/>.</typeparam>
/// <typeparam name="TBotMiddleware">The type of a specific bot middleware. It must implement interface <see cref="IMiddleware"/>.</typeparam>
/// <param name="services"> The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="serviceLifetime">The lifetime for the <typeparamref name="TBotMiddleware"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
Expand All @@ -238,9 +239,9 @@ public static IServiceCollection AddBotMiddleware<TBotMiddleware>(this IServiceC
}

/// <summary>
/// Adds a bot middlerware from an implementation factory.
/// Adds a bot middleware from an implementation factory.
/// </summary>
/// <typeparam name="TBotMiddleware">The type of a specific bot middlerware. It must implement interface <see cref="IMiddleware"/>.</typeparam>
/// <typeparam name="TBotMiddleware">The type of a specific bot middleware. It must implement interface <see cref="IMiddleware"/>.</typeparam>
/// <param name="services"> The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="implementationFactory">The factory that creates the service.</param>
/// <param name="serviceLifetime">The lifetime for the <typeparamref name="TBotMiddleware"/>.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public bool IsEnabled(LogLevel logLevel)
}

/// <inheritdoc />
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception?, string> formatter)
{
if (formatter == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class DetectedLanguageTranslatorMiddleware : IMiddleware
/// A cognitive service provider to locate a language detection and a text translation services from the given names.
/// </param>
public DetectedLanguageTranslatorMiddleware(CultureInfo translateToLanguage, IEnumerable<CultureInfo> languageExceptions, string languageDetectionServiceName, string textTranslationServiceName, ICognitiveServiceProvider cognitiveServiceProvider)
: this(translateToLanguage, languageExceptions, cognitiveServiceProvider?.GetLanguageDetectionService(languageDetectionServiceName), cognitiveServiceProvider?.GetTextTranslationService(textTranslationServiceName))
: this(translateToLanguage, languageExceptions, cognitiveServiceProvider.GetLanguageDetectionService(languageDetectionServiceName), cognitiveServiceProvider.GetTextTranslationService(textTranslationServiceName))
{
}

Expand Down Expand Up @@ -66,7 +66,7 @@ public DetectedLanguageTranslatorMiddleware(CultureInfo translateToLanguage, IEn
/// any of the exception languages, then it will be translated into the translation language before passing it down the pipeline.
/// </para>
/// <para>
/// When returning, it also process outgoint activities to translate back.
/// When returning, it also process outgoing activities to translate back.
/// </para>
/// </summary>
/// <param name="turnContext">The context object for this turn.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Encamina.Enmarcha.Bot.Middlewares;

/// <summary>
/// Middleware to automatically translate messages sent to consumers duting start activities into the language
/// Middleware to automatically translate messages sent to consumers during start activities into the language
/// received as <see cref="Activity.Locale"/>.
/// </summary>
/// <remarks>
Expand All @@ -31,7 +31,7 @@ public class StartActivityTranslatorMiddleware : IMiddleware
/// A cognitive service provider to locate a language detection and a text translation services from the given names.
/// </param>
public StartActivityTranslatorMiddleware(string textTranslationServiceName, ICognitiveServiceProvider cognitiveServiceProvider)
: this(cognitiveServiceProvider?.GetTextTranslationService(textTranslationServiceName))
: this(cognitiveServiceProvider.GetTextTranslationService(textTranslationServiceName))
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Encamina.Enmarcha.Bot/Middlewares/TranslatorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class TranslatorUtils
/// <returns>
/// The trasnslated text in the given <paramref name="language"/> or the given default value from <paramref name="default"/>.
/// </returns>
internal static string GetTranslation(IDictionary<string, string> translations, CultureInfo language, string @default = @"")
internal static string GetTranslation(IDictionary<string, string>? translations, CultureInfo language, string @default = @"")
{
return translations != null && (translations.TryGetValue(language.Name, out var value) || translations.TryGetValue(language.Parent.Name, out value)) ? value : @default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class TableStorageResponseProvider : IIntentResponsesProvider
/// <param name="defaultLocale">Default locale.</param>
/// <param name="intentCounterSeparator">
/// An optional value that represents the intent counter separation (i.e., a value that helps separating the intent
/// label from a numeric value that represens its order or instance number). Defaults to '<c>-</c>'.
/// label from a numeric value that represents its order or instance number). Defaults to '<c>-</c>'.
/// </param>
/// <param name="cacheAbsoluteExpirationSeconds">
/// An optional value for absolute expiration time in seconds for the cache. Defaults to '<c>86400</c>' seconds.
Expand Down Expand Up @@ -66,11 +66,13 @@ public virtual Task<IReadOnlyCollection<Response>> GetResponsesAsync(string inte
/// <inheritdoc/>
public virtual async Task<IReadOnlyCollection<Response>> GetResponsesAsync(string intent, CultureInfo culture, CancellationToken cancellationToken)
{
var intentsByLocale = await memoryCache?.GetOrCreate(CacheKey, async cacheEntry =>
var intentsByLocale = memoryCache != null
? await memoryCache.GetOrCreateAsync(CacheKey, async cacheEntry =>
{
cacheEntry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(cacheAbsoluteExpirationSeconds);
return await InitAsync(cancellationToken);
}) ?? await InitAsync(cancellationToken);
cacheEntry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(cacheAbsoluteExpirationSeconds);
return await InitAsync(cancellationToken);
})
: await InitAsync(cancellationToken);

return (intentsByLocale.TryGetValue(culture.Name.ToUpperInvariant(), out var responsesByIntent) ||
intentsByLocale.TryGetValue(culture.Parent.Name.ToUpperInvariant(), out responsesByIntent) ||
Expand Down Expand Up @@ -147,4 +149,4 @@ private sealed class ResponsesTableEntity : ITableEntity

public string Intent => RowKey;
}
}
}

0 comments on commit 230c964

Please sign in to comment.