mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Update analyzers for .NET 10 SDK (#2611)
This commit is contained in:
@@ -43,7 +43,10 @@ internal sealed class BackgroundJobRunner
|
||||
}
|
||||
catch (Exception e) when (e is not OperationCanceledException and not SystemException)
|
||||
{
|
||||
this._logger.LogError(e, "Error running background job {BackgroundJobError}.", e.Message);
|
||||
if (this._logger.IsEnabled(LogLevel.Error))
|
||||
{
|
||||
this._logger.LogError(e, "Error running background job {BackgroundJobError}.", e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -73,7 +73,10 @@ internal class ChannelHandler : IChannelHandler
|
||||
}
|
||||
catch (Exception e) when (this._purviewSettings.IgnoreExceptions)
|
||||
{
|
||||
this._logger.LogError(e, "Error queuing job: {ExceptionMessage}", e.Message);
|
||||
if (this._logger.IsEnabled(LogLevel.Error))
|
||||
{
|
||||
this._logger.LogError(e, "Error queuing job: {ExceptionMessage}", e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,16 +38,12 @@ public class PurviewAppLocation
|
||||
/// <exception cref="InvalidOperationException">Thrown when an invalid location type is provided.</exception>
|
||||
internal PolicyLocation GetPolicyLocation()
|
||||
{
|
||||
switch (this.LocationType)
|
||||
return this.LocationType switch
|
||||
{
|
||||
case PurviewLocationType.Application:
|
||||
return new PolicyLocation($"{Constants.ODataGraphNamespace}.policyLocationApplication", this.LocationValue);
|
||||
case PurviewLocationType.Uri:
|
||||
return new PolicyLocation($"{Constants.ODataGraphNamespace}.policyLocationUrl", this.LocationValue);
|
||||
case PurviewLocationType.Domain:
|
||||
return new PolicyLocation($"{Constants.ODataGraphNamespace}.policyLocationDomain", this.LocationValue);
|
||||
default:
|
||||
throw new InvalidOperationException("Invalid location type.");
|
||||
}
|
||||
PurviewLocationType.Application => new($"{Constants.ODataGraphNamespace}.policyLocationApplication", this.LocationValue),
|
||||
PurviewLocationType.Uri => new($"{Constants.ODataGraphNamespace}.policyLocationUrl", this.LocationValue),
|
||||
PurviewLocationType.Domain => new($"{Constants.ODataGraphNamespace}.policyLocationDomain", this.LocationValue),
|
||||
_ => throw new InvalidOperationException("Invalid location type."),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ internal sealed class PurviewClient : IPurviewClient
|
||||
this._tokenCredential = tokenCredential;
|
||||
this._httpClient = httpClient;
|
||||
|
||||
this._scopes = new string[] { $"https://{purviewSettings.GraphBaseUri.Host}/.default" };
|
||||
this._scopes = [$"https://{purviewSettings.GraphBaseUri.Host}/.default"];
|
||||
this._graphUri = purviewSettings.GraphBaseUri.ToString().TrimEnd('/');
|
||||
this._logger = logger ?? NullLogger.Instance;
|
||||
}
|
||||
@@ -176,7 +176,11 @@ internal sealed class PurviewClient : IPurviewClient
|
||||
throw new PurviewRequestException(DeserializeError);
|
||||
}
|
||||
|
||||
this._logger.LogError("Failed to process content. Status code: {StatusCode}", response.StatusCode);
|
||||
if (this._logger.IsEnabled(LogLevel.Error))
|
||||
{
|
||||
this._logger.LogError("Failed to process content. Status code: {StatusCode}", response.StatusCode);
|
||||
}
|
||||
|
||||
throw CreateExceptionForStatusCode(response.StatusCode, "processContent");
|
||||
}
|
||||
}
|
||||
@@ -241,7 +245,11 @@ internal sealed class PurviewClient : IPurviewClient
|
||||
throw new PurviewRequestException(DeserializeError);
|
||||
}
|
||||
|
||||
this._logger.LogError("Failed to retrieve protection scopes. Status code: {StatusCode}", response.StatusCode);
|
||||
if (this._logger.IsEnabled(LogLevel.Error))
|
||||
{
|
||||
this._logger.LogError("Failed to retrieve protection scopes. Status code: {StatusCode}", response.StatusCode);
|
||||
}
|
||||
|
||||
throw CreateExceptionForStatusCode(response.StatusCode, "protectionScopes/compute");
|
||||
}
|
||||
}
|
||||
@@ -304,7 +312,11 @@ internal sealed class PurviewClient : IPurviewClient
|
||||
throw new PurviewRequestException(DeserializeError);
|
||||
}
|
||||
|
||||
this._logger.LogError("Failed to create content activities. Status code: {StatusCode}", response.StatusCode);
|
||||
if (this._logger.IsEnabled(LogLevel.Error))
|
||||
{
|
||||
this._logger.LogError("Failed to create content activities. Status code: {StatusCode}", response.StatusCode);
|
||||
}
|
||||
|
||||
throw CreateExceptionForStatusCode(response.StatusCode, "contentActivities");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,13 +73,20 @@ internal sealed class PurviewWrapper : IDisposable
|
||||
(bool shouldBlockPrompt, resolvedUserId) = await this._scopedProcessor.ProcessMessagesAsync(messages, options?.ConversationId, Activity.UploadText, this._purviewSettings, null, cancellationToken).ConfigureAwait(false);
|
||||
if (shouldBlockPrompt)
|
||||
{
|
||||
this._logger.LogInformation("Prompt blocked by policy. Sending message: {Message}", this._purviewSettings.BlockedPromptMessage);
|
||||
if (this._logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
this._logger.LogInformation("Prompt blocked by policy. Sending message: {Message}", this._purviewSettings.BlockedPromptMessage);
|
||||
}
|
||||
|
||||
return new ChatResponse(new ChatMessage(ChatRole.System, this._purviewSettings.BlockedPromptMessage));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Error processing prompt: {ExceptionMessage}", ex.Message);
|
||||
if (this._logger.IsEnabled(LogLevel.Error))
|
||||
{
|
||||
this._logger.LogError(ex, "Error processing prompt: {ExceptionMessage}", ex.Message);
|
||||
}
|
||||
|
||||
if (!this._purviewSettings.IgnoreExceptions)
|
||||
{
|
||||
@@ -94,13 +101,20 @@ internal sealed class PurviewWrapper : IDisposable
|
||||
(bool shouldBlockResponse, _) = await this._scopedProcessor.ProcessMessagesAsync(response.Messages, options?.ConversationId, Activity.UploadText, this._purviewSettings, resolvedUserId, cancellationToken).ConfigureAwait(false);
|
||||
if (shouldBlockResponse)
|
||||
{
|
||||
this._logger.LogInformation("Response blocked by policy. Sending message: {Message}", this._purviewSettings.BlockedResponseMessage);
|
||||
if (this._logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
this._logger.LogInformation("Response blocked by policy. Sending message: {Message}", this._purviewSettings.BlockedResponseMessage);
|
||||
}
|
||||
|
||||
return new ChatResponse(new ChatMessage(ChatRole.System, this._purviewSettings.BlockedResponseMessage));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Error processing response: {ExceptionMessage}", ex.Message);
|
||||
if (this._logger.IsEnabled(LogLevel.Error))
|
||||
{
|
||||
this._logger.LogError(ex, "Error processing response: {ExceptionMessage}", ex.Message);
|
||||
}
|
||||
|
||||
if (!this._purviewSettings.IgnoreExceptions)
|
||||
{
|
||||
@@ -132,13 +146,20 @@ internal sealed class PurviewWrapper : IDisposable
|
||||
|
||||
if (shouldBlockPrompt)
|
||||
{
|
||||
this._logger.LogInformation("Prompt blocked by policy. Sending message: {Message}", this._purviewSettings.BlockedPromptMessage);
|
||||
if (this._logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
this._logger.LogInformation("Prompt blocked by policy. Sending message: {Message}", this._purviewSettings.BlockedPromptMessage);
|
||||
}
|
||||
|
||||
return new AgentRunResponse(new ChatMessage(ChatRole.System, this._purviewSettings.BlockedPromptMessage));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Error processing prompt: {ExceptionMessage}", ex.Message);
|
||||
if (this._logger.IsEnabled(LogLevel.Error))
|
||||
{
|
||||
this._logger.LogError(ex, "Error processing prompt: {ExceptionMessage}", ex.Message);
|
||||
}
|
||||
|
||||
if (!this._purviewSettings.IgnoreExceptions)
|
||||
{
|
||||
@@ -154,13 +175,20 @@ internal sealed class PurviewWrapper : IDisposable
|
||||
|
||||
if (shouldBlockResponse)
|
||||
{
|
||||
this._logger.LogInformation("Response blocked by policy. Sending message: {Message}", this._purviewSettings.BlockedResponseMessage);
|
||||
if (this._logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
this._logger.LogInformation("Response blocked by policy. Sending message: {Message}", this._purviewSettings.BlockedResponseMessage);
|
||||
}
|
||||
|
||||
return new AgentRunResponse(new ChatMessage(ChatRole.System, this._purviewSettings.BlockedResponseMessage));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError(ex, "Error processing response: {ExceptionMessage}", ex.Message);
|
||||
if (this._logger.IsEnabled(LogLevel.Error))
|
||||
{
|
||||
this._logger.LogError(ex, "Error processing response: {ExceptionMessage}", ex.Message);
|
||||
}
|
||||
|
||||
if (!this._purviewSettings.IgnoreExceptions)
|
||||
{
|
||||
|
||||
@@ -242,23 +242,16 @@ internal sealed class ScopedContentProcessor : IScopedContentProcessor
|
||||
/// </summary>
|
||||
/// <param name="pcResponse">The process content response which may contain DLP actions.</param>
|
||||
/// <param name="actionInfos">DLP actions returned from protection scopes.</param>
|
||||
/// <returns>The process content response with the protection scopes DLP actions added. Actions are deduplicated.</returns>
|
||||
/// <returns>The process content response with the protection scopes DLP actions added.</returns>
|
||||
private static ProcessContentResponse CombinePolicyActions(ProcessContentResponse pcResponse, List<DlpActionInfo>? actionInfos)
|
||||
{
|
||||
if (actionInfos == null || actionInfos.Count == 0)
|
||||
if (actionInfos?.Count > 0)
|
||||
{
|
||||
return pcResponse;
|
||||
pcResponse.PolicyActions = pcResponse.PolicyActions is null ?
|
||||
actionInfos :
|
||||
[.. pcResponse.PolicyActions, .. actionInfos];
|
||||
}
|
||||
|
||||
if (pcResponse.PolicyActions == null)
|
||||
{
|
||||
pcResponse.PolicyActions = actionInfos;
|
||||
return pcResponse;
|
||||
}
|
||||
|
||||
List<DlpActionInfo> pcActionInfos = new(pcResponse.PolicyActions);
|
||||
pcActionInfos.AddRange(actionInfos);
|
||||
pcResponse.PolicyActions = pcActionInfos;
|
||||
return pcResponse;
|
||||
}
|
||||
|
||||
@@ -339,20 +332,14 @@ internal sealed class ScopedContentProcessor : IScopedContentProcessor
|
||||
/// <returns>The protection scopes activity.</returns>
|
||||
private static ProtectionScopeActivities TranslateActivity(Activity activity)
|
||||
{
|
||||
switch (activity)
|
||||
return activity switch
|
||||
{
|
||||
case Activity.Unknown:
|
||||
return ProtectionScopeActivities.None;
|
||||
case Activity.UploadText:
|
||||
return ProtectionScopeActivities.UploadText;
|
||||
case Activity.UploadFile:
|
||||
return ProtectionScopeActivities.UploadFile;
|
||||
case Activity.DownloadText:
|
||||
return ProtectionScopeActivities.DownloadText;
|
||||
case Activity.DownloadFile:
|
||||
return ProtectionScopeActivities.DownloadFile;
|
||||
default:
|
||||
return ProtectionScopeActivities.UnknownFutureValue;
|
||||
}
|
||||
Activity.Unknown => ProtectionScopeActivities.None,
|
||||
Activity.UploadText => ProtectionScopeActivities.UploadText,
|
||||
Activity.UploadFile => ProtectionScopeActivities.UploadFile,
|
||||
Activity.DownloadText => ProtectionScopeActivities.DownloadText,
|
||||
Activity.DownloadFile => ProtectionScopeActivities.DownloadFile,
|
||||
_ => ProtectionScopeActivities.UnknownFutureValue,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user