Enable more analyzers and various code tweaks/cleanup (#738)

This commit is contained in:
Stephen Toub
2025-09-17 21:48:25 -04:00
committed by GitHub
Unverified
parent f3264966ff
commit 5e5761b288
326 changed files with 2402 additions and 3642 deletions
+8 -10
View File
@@ -40,7 +40,7 @@ public abstract class BaseSample : TextWriter
public BaseSample Console => this;
/// <inheritdoc />
public override Encoding Encoding => System.Text.Encoding.UTF8;
public override Encoding Encoding => Encoding.UTF8;
/// <summary>
/// Initializes a new instance of the <see cref="BaseSample"/> class, setting up logging, configuration, and
@@ -79,10 +79,8 @@ public abstract class BaseSample : TextWriter
/// Writes a user message to the console.
/// </summary>
/// <param name="message">The text of the message to be sent. Cannot be null or empty.</param>
protected void WriteUserMessage(string message)
{
protected void WriteUserMessage(string message) =>
this.WriteMessageOutput(new ChatMessage(ChatRole.User, message));
}
/// <summary>
/// Processes and writes the latest agent chat response to the console, including metadata and content details.
@@ -124,9 +122,9 @@ public abstract class BaseSample : TextWriter
{
string authorExpression = message.Role == ChatRole.User ? string.Empty : FormatAuthor();
string contentExpression = message.Text.Trim();
bool isCode = false; //message.AdditionalProperties?.ContainsKey(OpenAIAssistantAgent.CodeInterpreterMetadataKey) ?? false;
string codeMarker = isCode ? "\n [CODE]\n" : " ";
Console.WriteLine($"\n# {message.Role}{authorExpression}:{codeMarker}{contentExpression}");
const bool IsCode = false; //message.AdditionalProperties?.ContainsKey(OpenAIAssistantAgent.CodeInterpreterMetadataKey) ?? false;
const string CodeMarker = IsCode ? "\n [CODE]\n" : " ";
Console.WriteLine($"\n# {message.Role}{authorExpression}:{CodeMarker}{contentExpression}");
// Provide visibility for inner content (that isn't TextContent).
foreach (AIContent item in message.Contents)
@@ -166,9 +164,9 @@ public abstract class BaseSample : TextWriter
string authorExpression = update.Role == ChatRole.User ? string.Empty : FormatAuthor();
string contentExpression = string.IsNullOrWhiteSpace(update.Text) ? string.Empty : update.Text;
bool isCode = false; //message.AdditionalProperties?.ContainsKey(OpenAIAssistantAgent.CodeInterpreterMetadataKey) ?? false;
string codeMarker = isCode ? "\n [CODE]\n" : " ";
Console.WriteLine($"\n# {update.Role}{authorExpression}:{codeMarker}{contentExpression}");
const bool IsCode = false; //message.AdditionalProperties?.ContainsKey(OpenAIAssistantAgent.CodeInterpreterMetadataKey) ?? false;
const string CodeMarker = IsCode ? "\n [CODE]\n" : " ";
Console.WriteLine($"\n# {update.Role}{authorExpression}:{CodeMarker}{contentExpression}");
// Provide visibility for inner content (that isn't TextContent).
foreach (AIContent item in update.Contents)
@@ -24,36 +24,25 @@ public abstract class OrchestrationSample : BaseSample
/// <param name="name">An optional name for the agent.</param>
/// <param name="functions">A set of <see cref="AIFunction"/> instances to be used as tools by the agent.</param>
/// <returns>A new <see cref="ChatClientAgent"/> instance configured with the provided parameters.</returns>
protected ChatClientAgent CreateAgent(string instructions, string? description = null, string? name = null, params AIFunction[] functions)
{
// Get the chat client to use for the agent.
using IChatClient chatClient = CreateChatClient();
ChatClientAgentOptions options =
new()
{
Name = name,
Description = description,
Instructions = instructions,
ChatOptions = new() { Tools = functions, ToolMode = ChatToolMode.Auto }
};
return new ChatClientAgent(chatClient, options);
}
protected static ChatClientAgent CreateAgent(string instructions, string? description = null, string? name = null, params AIFunction[] functions) =>
new(CreateChatClient(), new ChatClientAgentOptions()
{
Name = name,
Description = description,
Instructions = instructions,
ChatOptions = new() { Tools = functions, ToolMode = ChatToolMode.Auto }
});
/// <summary>
/// Creates and configures a new <see cref="IChatClient"/> instance using the OpenAI client and test configuration.
/// </summary>
/// <returns>A configured <see cref="IChatClient"/> instance ready for use with agents.</returns>
protected IChatClient CreateChatClient()
{
return new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
.AsIChatClient()
.AsBuilder()
.UseFunctionInvocation()
.Build();
}
protected static IChatClient CreateChatClient() => new OpenAIClient(TestConfiguration.OpenAI.ApiKey)
.GetChatClient(TestConfiguration.OpenAI.ChatModelId)
.AsIChatClient()
.AsBuilder()
.UseFunctionInvocation()
.Build();
/// <summary>
/// Display the provided history.
@@ -129,7 +118,7 @@ public abstract class OrchestrationSample : BaseSample
/// </summary>
/// <param name="response">The collection of <see cref="ChatMessage"/> objects to process.</param>
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
public ValueTask ResponseCallback(IEnumerable<ChatMessage> response)
public ValueTask ResponseCallbackAsync(IEnumerable<ChatMessage> response)
{
WriteStreamedResponse(this.StreamedResponses);
this.StreamedResponses.Clear();
@@ -144,7 +133,7 @@ public abstract class OrchestrationSample : BaseSample
/// </summary>
/// <param name="streamedResponse">The <see cref="AgentRunResponseUpdate"/> to process.</param>
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
public ValueTask StreamingResultCallback(AgentRunResponseUpdate streamedResponse)
public ValueTask StreamingResultCallbackAsync(AgentRunResponseUpdate streamedResponse)
{
this.StreamedResponses.Add(streamedResponse);
return default;
@@ -153,16 +142,16 @@ public abstract class OrchestrationSample : BaseSample
/// <summary>
/// Initializes a new instance of the <see cref="BaseSample"/> class, setting up logging, configuration, and
/// optionally redirecting <see cref="System.Console"/> output to the test output.
/// optionally redirecting <see cref="Console"/> output to the test output.
/// </summary>
/// <remarks>This constructor initializes logging using an <see cref="XunitLogger"/> and sets up
/// configuration from multiple sources, including a JSON file, environment variables, and user secrets.
/// If <paramref name="redirectSystemConsoleOutput"/> is <see langword="true"/>, calls to <see cref="System.Console"/>
/// If <paramref name="redirectSystemConsoleOutput"/> is <see langword="true"/>, calls to <see cref="Console"/>
/// will be redirected to the test output provided by <paramref name="output"/>.
/// </remarks>
/// <param name="output">The <see cref="ITestOutputHelper"/> instance used to write test output.</param>
/// <param name="redirectSystemConsoleOutput">
/// A value indicating whether <see cref="System.Console"/> output should be redirected to the test output. <see langword="true"/> to redirect; otherwise, <see langword="false"/>.
/// A value indicating whether <see cref="Console"/> output should be redirected to the test output. <see langword="true"/> to redirect; otherwise, <see langword="false"/>.
/// </param>
protected OrchestrationSample(ITestOutputHelper output, bool redirectSystemConsoleOutput = true)
: base(output, redirectSystemConsoleOutput)
@@ -60,10 +60,8 @@ public sealed class TestConfiguration
/// Initializes the configuration system with the specified configuration root.
/// </summary>
/// <param name="configRoot">The root of the configuration hierarchy used to initialize the system. Must not be <see langword="null"/>.</param>
public static void Initialize(IConfigurationRoot configRoot)
{
public static void Initialize(IConfigurationRoot configRoot) =>
s_instance = new TestConfiguration(configRoot);
}
#region Private Members
@@ -12,36 +12,28 @@ public static class TextOutputHelperExtensions
/// </summary>
/// <param name="testOutputHelper">Target <see cref="ITestOutputHelper"/></param>
/// <param name="target">Target object to write</param>
public static void WriteLine(this ITestOutputHelper testOutputHelper, object target)
{
public static void WriteLine(this ITestOutputHelper testOutputHelper, object target) =>
testOutputHelper.WriteLine(target.ToString());
}
/// <summary>
/// Current interface ITestOutputHelper does not have a WriteLine method that takes no parameters. This extension method adds it to make it analogous to Console.WriteLine when used in Console apps.
/// </summary>
/// <param name="testOutputHelper">Target <see cref="ITestOutputHelper"/></param>
public static void WriteLine(this ITestOutputHelper testOutputHelper)
{
public static void WriteLine(this ITestOutputHelper testOutputHelper) =>
testOutputHelper.WriteLine(string.Empty);
}
/// <summary>
/// Current interface ITestOutputHelper does not have a Write method that takes no parameters. This extension method adds it to make it analogous to Console.Write when used in Console apps.
/// </summary>
/// <param name="testOutputHelper">Target <see cref="ITestOutputHelper"/></param>
public static void Write(this ITestOutputHelper testOutputHelper)
{
public static void Write(this ITestOutputHelper testOutputHelper) =>
testOutputHelper.WriteLine(string.Empty);
}
/// <summary>
/// Current interface ITestOutputHelper does not have a Write method. This extension method adds it to make it analogous to Console.Write when used in Console apps.
/// </summary>
/// <param name="testOutputHelper">Target <see cref="ITestOutputHelper"/></param>
/// <param name="target">Target object to write</param>
public static void Write(this ITestOutputHelper testOutputHelper, object target)
{
public static void Write(this ITestOutputHelper testOutputHelper, object target) =>
testOutputHelper.WriteLine(target.ToString());
}
}