Skip to content

Commit

Permalink
Add x-ms-ai-sensitivity extension (#2750)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucGenetier authored Nov 22, 2024
1 parent 1d00757 commit 07a817b
Show file tree
Hide file tree
Showing 9 changed files with 493 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Microsoft.PowerFx.Connectors
{
public static class Constants
{
public const string XMsAiSensitivity = "x-ms-ai-sensitivity";
public const string XMsBodyName = "x-bodyName";
public const string XMsCapabilities = "x-ms-capabilities";
public const string XMsDynamicList = "x-ms-dynamic-list";
Expand All @@ -17,8 +18,8 @@ public static class Constants
public const string XMsKeyOrder = "x-ms-keyOrder";
public const string XMsKeyType = "x-ms-keyType";
public const string XMsMediaKind = "x-ms-media-kind";
public const string XMsNotificationUrl = "x-ms-notification-url";
public const string XMsNotificationContent = "x-ms-notification-content";
public const string XMsNotificationUrl = "x-ms-notification-url";
public const string XMsPageable = "x-ms-pageable";
public const string XMsPermission = "x-ms-permission";
public const string XMsRelationships = "x-ms-relationships";
Expand Down
11 changes: 11 additions & 0 deletions src/libraries/Microsoft.PowerFx.Connectors/OpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ enumName is OpenApiString enumNameStr

internal static bool? GetNotificationUrl(this ISwaggerExtensions schema) => schema.Extensions.TryGetValue(XMsNotificationUrl, out IOpenApiExtension openApiExt) && openApiExt is OpenApiBoolean openApiBool ? openApiBool.Value : null;

internal static string GetAiSensitivity(this ISwaggerExtensions schema) => schema.Extensions.TryGetValue(XMsAiSensitivity, out IOpenApiExtension openApiExt) && openApiExt is OpenApiString openApiStr ? openApiStr.Value : null;

internal static (bool IsPresent, string Value) GetString(this IDictionary<string, IOpenApiAny> apiObj, string str) => apiObj.TryGetValue(str, out IOpenApiAny openApiAny) && openApiAny is OpenApiString openApiStr ? (true, openApiStr.Value) : (false, null);

internal static void WhenPresent(this IDictionary<string, IOpenApiAny> apiObj, string propName, Action<string> action)
Expand Down Expand Up @@ -932,6 +934,15 @@ public static Visibility ToVisibility(this string visibility)
: Visibility.Unknown;
}

public static AiSensitivity ToAiSensitivity(this string aiSensitivity)
{
return string.IsNullOrEmpty(aiSensitivity)
? AiSensitivity.None
: Enum.TryParse(aiSensitivity, true, out AiSensitivity ais)
? ais
: AiSensitivity.Unknown;
}

public static MediaKind ToMediaKind(this string mediaKind)
{
return string.IsNullOrEmpty(mediaKind)
Expand Down
20 changes: 20 additions & 0 deletions src/libraries/Microsoft.PowerFx.Connectors/Public/AiSensitivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

namespace Microsoft.PowerFx.Connectors
{
public enum AiSensitivity : int
{
// "x-ms-ai-sensitivity" is not corresponding to any valid value (normally, only "low", "high")
Unknown = -1,

// "x-ms-ai-sensitivity" is not defined
None = 0,

// "x-ms-ai-sensitivity" is "low"
Low,

// "x-ms-ai-sensitivity" is "high"
High
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class ConnectorSchema : SupportsConnectorErrors

public bool? NotificationUrl => ConnectorType.NotificationUrl;

public AiSensitivity AiSensitivity => ConnectorType.AiSensitivity;

internal ConnectorSchema(ISwaggerParameter openApiParameter, ISwaggerExtensions bodyExtensions, bool useHiddenTypes, ConnectorCompatibility compatibility)
{
Schema = openApiParameter.Schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public class ConnectorType : SupportsConnectorErrors
// Supports x-ms-notification-url
public bool? NotificationUrl { get; }

// Supports x-ms-ai-sensitivity
public AiSensitivity AiSensitivity { get; }

internal RecordType HiddenRecordType { get; }

// Supports x-ms-dynamic-values or -list locally
Expand Down Expand Up @@ -128,6 +131,7 @@ internal ConnectorType(ISwaggerSchema schema, ISwaggerParameter openApiParameter
Binary = schema.Format == "binary" || schema.Format == "no_format";
MediaKind = openApiParameter?.GetMediaKind().ToMediaKind() ?? (Binary ? MediaKind.File : MediaKind.NotBinary);
NotificationUrl = openApiParameter?.GetNotificationUrl();
AiSensitivity = openApiParameter?.GetAiSensitivity().ToAiSensitivity() ?? AiSensitivity.Unknown;

if (schema != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Swagger\PetStore.json" />
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Swagger\PowerAppsForMakers.json" />
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Swagger\PowerPlatformForAdmins.json" />
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Swagger\SendMail.json" />
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Swagger\SQL Server.json" />
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Swagger\SalesForce.json" />
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Swagger\ServiceNow.json" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,7 @@ public void ExposeInternalParamsWithoutDefaultValueTest(ConnectorCompatibility c
{
Compatibility = compatibility,
AllowUnsupportedFunctions = true,
IncludeInternalFunctions = true,
IncludeInternalFunctions = true,
IncludeWebhookFunctions = true,
ExposeInternalParamsWithoutDefaultValue = exposeInternalParamsWithoutDefaultValue
};
Expand Down Expand Up @@ -1837,6 +1837,36 @@ public async Task SendEmail()
Assert.Single(functions.Where(x => x.Name == "SendEmailV3"));
}

[Fact]
public async Task AiSensitivityTest()
{
using LoggingTestServer testConnector = new LoggingTestServer(@"Swagger\SendMail.json", _output);
OpenApiDocument apiDoc = testConnector._apiDocument;

ConnectorSettings connectorSettings = new ConnectorSettings("exob")
{
Compatibility = ConnectorCompatibility.SwaggerCompatibility,
AllowUnsupportedFunctions = true,
IncludeInternalFunctions = true,
ReturnUnknownRecordFieldsAsUntypedObjects = true
};

List<ConnectorFunction> functions = OpenApiParser.GetFunctions(connectorSettings, apiDoc).OrderBy(f => f.Name).ToList();

ConnectorFunction sendmail = functions.First(f => f.Name == "SendEmailV3");
IEnumerable<ConnectorParameter> parameters = sendmail.RequiredParameters.Union(sendmail.OptionalParameters);

string unknownAiSensitivity = string.Join(", ", parameters.Where(p => p.AiSensitivity == AiSensitivity.Unknown).Select(p => p.Name));
string noAiSensitivity = string.Join(", ", parameters.Where(p => p.AiSensitivity == AiSensitivity.None).Select(p => p.Name));
string lowAiSensitivity = string.Join(", ", parameters.Where(p => p.AiSensitivity == AiSensitivity.Low).Select(p => p.Name));
string highAiSensitivity = string.Join(", ", parameters.Where(p => p.AiSensitivity == AiSensitivity.High).Select(p => p.Name));

Assert.Equal(string.Empty, unknownAiSensitivity);
Assert.Equal("subject, text, toname, ccname, bccname, files, filenames", noAiSensitivity);
Assert.Equal(string.Empty, lowAiSensitivity);
Assert.Equal("to, cc, bcc", highAiSensitivity);
}

[Fact]
public async Task ExcelOnlineTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void PublicSurfaceTest_Connectors()
var allowed = new HashSet<string>()
{
"Microsoft.PowerFx.ConfigExtensions",
"Microsoft.PowerFx.Connectors.AiSensitivity",
"Microsoft.PowerFx.Connectors.BaseRuntimeConnectorContext",
"Microsoft.PowerFx.Connectors.CdpDataSource",
"Microsoft.PowerFx.Connectors.CdpExtensions",
Expand Down
Loading

0 comments on commit 07a817b

Please sign in to comment.