mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Enable more analyzers and various code tweaks/cleanup (#738)
This commit is contained in:
@@ -11,7 +11,7 @@ namespace SampleHelpers;
|
||||
internal static class SampleEnvironment
|
||||
{
|
||||
public static string? GetEnvironmentVariable(string key)
|
||||
=> SampleEnvironment.GetEnvironmentVariable(key, EnvironmentVariableTarget.Process);
|
||||
=> GetEnvironmentVariable(key, EnvironmentVariableTarget.Process);
|
||||
|
||||
public static string? GetEnvironmentVariable(string key, EnvironmentVariableTarget target)
|
||||
{
|
||||
@@ -70,73 +70,73 @@ internal static class SampleEnvironment
|
||||
// Methods that directly call System.Environment
|
||||
|
||||
public static IDictionary GetEnvironmentVariables()
|
||||
=> System.Environment.GetEnvironmentVariables();
|
||||
=> SystemEnvironment.GetEnvironmentVariables();
|
||||
|
||||
public static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target)
|
||||
=> System.Environment.GetEnvironmentVariables(target);
|
||||
=> SystemEnvironment.GetEnvironmentVariables(target);
|
||||
|
||||
public static void SetEnvironmentVariable(string variable, string? value)
|
||||
=> System.Environment.SetEnvironmentVariable(variable, value);
|
||||
=> SystemEnvironment.SetEnvironmentVariable(variable, value);
|
||||
|
||||
public static void SetEnvironmentVariable(string variable, string? value, EnvironmentVariableTarget target)
|
||||
=> System.Environment.SetEnvironmentVariable(variable, value, target);
|
||||
=> SystemEnvironment.SetEnvironmentVariable(variable, value, target);
|
||||
|
||||
public static string[] GetCommandLineArgs()
|
||||
=> System.Environment.GetCommandLineArgs();
|
||||
=> SystemEnvironment.GetCommandLineArgs();
|
||||
|
||||
public static string CommandLine
|
||||
=> System.Environment.CommandLine;
|
||||
=> SystemEnvironment.CommandLine;
|
||||
|
||||
public static string CurrentDirectory
|
||||
{
|
||||
get => System.Environment.CurrentDirectory;
|
||||
set => System.Environment.CurrentDirectory = value;
|
||||
get => SystemEnvironment.CurrentDirectory;
|
||||
set => SystemEnvironment.CurrentDirectory = value;
|
||||
}
|
||||
|
||||
public static string ExpandEnvironmentVariables(string name)
|
||||
=> System.Environment.ExpandEnvironmentVariables(name);
|
||||
=> SystemEnvironment.ExpandEnvironmentVariables(name);
|
||||
|
||||
public static string GetFolderPath(System.Environment.SpecialFolder folder)
|
||||
=> System.Environment.GetFolderPath(folder);
|
||||
public static string GetFolderPath(SystemEnvironment.SpecialFolder folder)
|
||||
=> SystemEnvironment.GetFolderPath(folder);
|
||||
|
||||
public static string GetFolderPath(System.Environment.SpecialFolder folder, System.Environment.SpecialFolderOption option)
|
||||
=> System.Environment.GetFolderPath(folder, option);
|
||||
public static string GetFolderPath(SystemEnvironment.SpecialFolder folder, SystemEnvironment.SpecialFolderOption option)
|
||||
=> SystemEnvironment.GetFolderPath(folder, option);
|
||||
|
||||
public static int ProcessorCount
|
||||
=> System.Environment.ProcessorCount;
|
||||
=> SystemEnvironment.ProcessorCount;
|
||||
|
||||
public static bool Is64BitProcess
|
||||
=> System.Environment.Is64BitProcess;
|
||||
=> SystemEnvironment.Is64BitProcess;
|
||||
|
||||
public static bool Is64BitOperatingSystem
|
||||
=> System.Environment.Is64BitOperatingSystem;
|
||||
=> SystemEnvironment.Is64BitOperatingSystem;
|
||||
|
||||
public static string MachineName
|
||||
=> System.Environment.MachineName;
|
||||
=> SystemEnvironment.MachineName;
|
||||
|
||||
public static string NewLine
|
||||
=> System.Environment.NewLine;
|
||||
=> SystemEnvironment.NewLine;
|
||||
|
||||
public static OperatingSystem OSVersion
|
||||
=> System.Environment.OSVersion;
|
||||
=> SystemEnvironment.OSVersion;
|
||||
|
||||
public static string StackTrace
|
||||
=> System.Environment.StackTrace;
|
||||
=> SystemEnvironment.StackTrace;
|
||||
|
||||
public static int SystemPageSize
|
||||
=> System.Environment.SystemPageSize;
|
||||
=> SystemEnvironment.SystemPageSize;
|
||||
|
||||
public static bool HasShutdownStarted
|
||||
=> System.Environment.HasShutdownStarted;
|
||||
=> SystemEnvironment.HasShutdownStarted;
|
||||
|
||||
#if NET9_0_OR_GREATER
|
||||
public static int ProcessId
|
||||
=> System.Environment.ProcessId;
|
||||
=> SystemEnvironment.ProcessId;
|
||||
|
||||
public static string? ProcessPath
|
||||
=> System.Environment.ProcessPath;
|
||||
=> SystemEnvironment.ProcessPath;
|
||||
|
||||
public static bool IsPrivilegedProcess
|
||||
=> System.Environment.IsPrivilegedProcess;
|
||||
=> SystemEnvironment.IsPrivilegedProcess;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ internal static partial class Throw
|
||||
public static string IfNullOrWhitespace([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string paramName = "")
|
||||
{
|
||||
#if !NETCOREAPP3_1_OR_GREATER
|
||||
if (argument == null)
|
||||
if (argument is null)
|
||||
{
|
||||
ArgumentNullException(paramName);
|
||||
}
|
||||
@@ -134,7 +134,7 @@ internal static partial class Throw
|
||||
|
||||
if (string.IsNullOrWhiteSpace(argument))
|
||||
{
|
||||
if (argument == null)
|
||||
if (argument is null)
|
||||
{
|
||||
ArgumentNullException(paramName);
|
||||
}
|
||||
@@ -159,7 +159,7 @@ internal static partial class Throw
|
||||
public static string IfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string paramName = "")
|
||||
{
|
||||
#if !NETCOREAPP3_1_OR_GREATER
|
||||
if (argument == null)
|
||||
if (argument is null)
|
||||
{
|
||||
ArgumentNullException(paramName);
|
||||
}
|
||||
@@ -167,7 +167,7 @@ internal static partial class Throw
|
||||
|
||||
if (string.IsNullOrEmpty(argument))
|
||||
{
|
||||
if (argument == null)
|
||||
if (argument is null)
|
||||
{
|
||||
ArgumentNullException(paramName);
|
||||
}
|
||||
@@ -215,7 +215,7 @@ internal static partial class Throw
|
||||
where T : struct, Enum
|
||||
{
|
||||
#if NET5_0_OR_GREATER
|
||||
if (!Enum.IsDefined<T>(argument))
|
||||
if (!Enum.IsDefined(argument))
|
||||
#else
|
||||
if (!Enum.IsDefined(typeof(T), argument))
|
||||
#endif
|
||||
@@ -248,7 +248,7 @@ internal static partial class Throw
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static IEnumerable<T> IfNullOrEmpty<T>([NotNull] IEnumerable<T>? argument, [CallerArgumentExpression(nameof(argument))] string paramName = "")
|
||||
{
|
||||
if (argument == null)
|
||||
if (argument is null)
|
||||
{
|
||||
ArgumentNullException(paramName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user