mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Update AIContextProviders to use Microsoft.Extensions.Compliance.Redaction (#4854)
* Update providers to use Microsoft.Extensions.Compliance.Redaction * Fix formatting. * Fix readme
This commit is contained in:
committed by
GitHub
Unverified
parent
cc85bbc2dc
commit
2c000b032d
@@ -0,0 +1,30 @@
|
||||
# Redaction
|
||||
|
||||
Log data redaction utilities built on `Microsoft.Extensions.Compliance.Redaction.Redactor`.
|
||||
|
||||
Provides `ReplacingRedactor`, an internal `Redactor` implementation that replaces
|
||||
any input with a fixed replacement string (e.g. `"<redacted>"`).
|
||||
|
||||
To use this in your project, add the following to your `.csproj` file:
|
||||
|
||||
```xml
|
||||
<PropertyGroup>
|
||||
<InjectSharedRedaction>true</InjectSharedRedaction>
|
||||
</PropertyGroup>
|
||||
```
|
||||
|
||||
You will also need to add a package reference to `Microsoft.Extensions.Compliance.Abstractions`:
|
||||
|
||||
```xml
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Compliance.Abstractions" />
|
||||
</ItemGroup>
|
||||
```
|
||||
|
||||
And finally, this also depends on the shared Throw class, so when using redaction, InjectSharedThrow should also be enabled:
|
||||
|
||||
```xml
|
||||
<PropertyGroup>
|
||||
<InjectSharedThrow>true</InjectSharedThrow>
|
||||
</PropertyGroup>
|
||||
```
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System;
|
||||
using Microsoft.Extensions.Compliance.Redaction;
|
||||
using Microsoft.Shared.Diagnostics;
|
||||
|
||||
namespace Microsoft.Agents.AI;
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="Redactor"/> that replaces the entire input with a fixed replacement string.
|
||||
/// </summary>
|
||||
internal sealed class ReplacingRedactor : Redactor
|
||||
{
|
||||
private readonly string _replacementText;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReplacingRedactor"/> class.
|
||||
/// </summary>
|
||||
/// <param name="replacementText">The text to substitute for any input value.</param>
|
||||
/// <exception cref="ArgumentNullException">Thrown when <paramref name="replacementText"/> is <see langword="null"/>.</exception>
|
||||
public ReplacingRedactor(string replacementText)
|
||||
{
|
||||
this._replacementText = Throw.IfNull(replacementText);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int GetRedactedLength(ReadOnlySpan<char> input) => this._replacementText.Length;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int Redact(ReadOnlySpan<char> source, Span<char> destination)
|
||||
{
|
||||
this._replacementText.AsSpan().CopyTo(destination);
|
||||
return this._replacementText.Length;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user