Skip to content

.Net: Prepare code for A2A SDK and AsyncEnumerable NuGet update #12842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
<PackageVersion Include="System.IO.Packaging" Version="8.0.1" />
<PackageVersion Include="System.Linq.Async" Version="6.0.3" />
<PackageVersion Include="System.Linq.AsyncEnumerable" Version="10.0.0-preview.5.25277.114" />
<PackageVersion Include="System.Memory.Data" Version="8.0.1" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Numerics.Tensors" Version="9.0.7" />
Expand Down
5 changes: 4 additions & 1 deletion dotnet/samples/Concepts/Concepts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.ML.Tokenizers" />
<PackageReference Include="Microsoft.OpenApi" />
<PackageReference Include="System.Linq.Async" />
<PackageReference Include="System.Numerics.Tensors" />
<PackageReference Include="AWSSDK.Core" />
<PackageReference Include="AWSSDK.SecurityToken" />
Expand Down Expand Up @@ -97,6 +96,10 @@
<ProjectReference Include="..\..\src\SemanticKernel.Core\SemanticKernel.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Linq/AsyncEnumerable.cs" Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" />
<PackageReference Include="System.Linq.AsyncEnumerable" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" />
<PackageReference Include="System.Linq.AsyncEnumerable" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="System.Linq.AsyncEnumerable" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Agents\Abstractions\Agents.Abstractions.csproj" />
<ProjectReference Include="..\..\..\src\Agents\Core\Agents.Core.csproj" />
Expand Down
1 change: 0 additions & 1 deletion dotnet/samples/GettingStarted/GettingStarted.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.ML.Tokenizers" />
<PackageReference Include="System.Linq.Async" />
<PackageReference Include="System.Numerics.Tensors" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="OpenTelemetry.Exporter.Console" />
<PackageReference Include="System.Linq.AsyncEnumerable" />
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<PackageReference Include="xunit" />
<PackageReference Include="xunit.abstractions" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="System.Linq.AsyncEnumerable" />
</ItemGroup>

<Import Project="$(RepoRoot)/dotnet/src/InternalUtilities/samples/SamplesInternalUtilities.props" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.ML.Tokenizers" />
<PackageReference Include="System.Linq.Async" />
<PackageReference Include="System.Numerics.Tensors" />
<PackageReference Include="System.Linq.AsyncEnumerable" />
</ItemGroup>

<Import Project="$(RepoRoot)/dotnet/src/InternalUtilities/samples/SamplesInternalUtilities.props" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="System.Linq.Async" />
<PackageReference Include="System.Text.Json" />
<PackageReference Include="System.Linq.AsyncEnumerable" />
</ItemGroup>

<Import Project="$(RepoRoot)/dotnet/src/InternalUtilities/samples/SamplesInternalUtilities.props" />
Expand Down
1 change: 0 additions & 1 deletion dotnet/samples/LearnResources/LearnResources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" />
<PackageReference Include="System.Linq.Async" />
</ItemGroup>

<Import Project="$(RepoRoot)/dotnet/src/InternalUtilities/samples/SamplesInternalUtilities.props" />
Expand Down
1 change: 1 addition & 0 deletions dotnet/src/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Setting errors for SDK projects under dotnet folder
[*.cs]
dotnet_diagnostic.CS1998.severity = suggestion # Async method lacks 'await' operators and will run synchronously
dotnet_diagnostic.CA2007.severity = error # Do not directly await a Task
dotnet_diagnostic.VSTHRD111.severity = error # Use .ConfigureAwait(bool)
dotnet_diagnostic.IDE1006.severity = error # Naming rule violations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Linq.Async" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>

Expand Down
18 changes: 13 additions & 5 deletions dotnet/src/Agents/Abstractions/AggregatorChannel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading;
Expand Down Expand Up @@ -56,7 +55,11 @@ protected internal override IAsyncEnumerable<ChatMessageContent> GetHistoryAsync
/// <inheritdoc/>
protected internal override async IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamingAsync(AggregatorAgent agent, IList<ChatMessageContent> messages, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
int initialCount = await this._chat.GetChatMessagesAsync(cancellationToken).CountAsync(cancellationToken).ConfigureAwait(false);
int initialCount = 0;
await foreach (var _ in this._chat.GetChatMessagesAsync(cancellationToken).ConfigureAwait(false))
{
initialCount++;
}

await foreach (StreamingChatMessageContent message in this._chat.InvokeStreamingAsync(cancellationToken).ConfigureAwait(false))
{
Expand All @@ -66,12 +69,17 @@ protected internal override async IAsyncEnumerable<StreamingChatMessageContent>
}
}

ChatMessageContent[] history = await this._chat.GetChatMessagesAsync(cancellationToken).ToArrayAsync(cancellationToken).ConfigureAwait(false);
if (history.Length > initialCount)
List<ChatMessageContent> history = [];
await foreach (var item in this._chat.GetChatMessagesAsync(cancellationToken).ConfigureAwait(false))
{
history.Add(item);
}

if (history.Count > initialCount)
{
if (agent.Mode == AggregatorMode.Flat)
{
for (int index = history.Length - 1; index >= initialCount; --index)
for (int index = history.Count - 1; index >= initialCount; --index)
{
messages.Add(history[index]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Linq;
using Microsoft.SemanticKernel.ChatCompletion;

namespace Microsoft.SemanticKernel.Agents.Extensions;
Expand All @@ -26,8 +25,11 @@ public static IEnumerable<ChatMessageContent> ToDescending(this ChatHistory hist
/// Enumerates a history in descending order asynchronously.
/// </summary>
/// <param name="history">The chat history to sort.</param>
public static IAsyncEnumerable<ChatMessageContent> ToDescendingAsync(this ChatHistory history)
public static async IAsyncEnumerable<ChatMessageContent> ToDescendingAsync(this ChatHistory history)
{
return history.ToDescending().ToAsyncEnumerable();
for (int index = history.Count; index > 0; --index)
{
yield return history[index - 1];
}
}
}
22 changes: 15 additions & 7 deletions dotnet/src/Agents/AzureAI/Internal/AgentThreadActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ public static async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync(Persis
throw new KernelException($"Agent Failure - Run terminated: {run.Status} [{run.Id}]: {run.LastError?.Message ?? "Unknown"}");
}

RunStep[] steps = await client.GetStepsAsync(run, cancellationToken: cancellationToken).ToArrayAsync(cancellationToken).ConfigureAwait(false);
List<RunStep> steps = [];
await foreach (var step in client.GetStepsAsync(run, cancellationToken: cancellationToken).ConfigureAwait(false))
{
steps.Add(step);
}

// Is tool action required?
if (run.Status == RunStatus.RequiresAction)
Expand All @@ -204,7 +208,7 @@ await functionProcessor.InvokeFunctionCallsAsync(
functionOptions,
kernel,
isStreaming: false,
cancellationToken).ToArrayAsync(cancellationToken).ConfigureAwait(false);
cancellationToken).ConfigureAwait(false);

// Capture function-call for message processing
foreach (FunctionResultContent functionCall in functionResults)
Expand Down Expand Up @@ -458,10 +462,14 @@ public static async IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamin

if (run.Status == RunStatus.RequiresAction)
{
RunStep[] activeSteps =
await client.GetStepsAsync(run, cancellationToken)
.Where(step => step.Status == RunStepStatus.InProgress)
.ToArrayAsync(cancellationToken).ConfigureAwait(false);
List<RunStep> activeSteps = [];
await foreach (var step in client.GetStepsAsync(run, cancellationToken).ConfigureAwait(false))
{
if (step.Status == RunStepStatus.InProgress)
{
activeSteps.Add(step);
}
}

// Capture map between the tool call and its associated step
Dictionary<string, string> toolMap = [];
Expand Down Expand Up @@ -489,7 +497,7 @@ await functionProcessor.InvokeFunctionCallsAsync(
functionOptions,
kernel,
isStreaming: true,
cancellationToken).ToArrayAsync(cancellationToken).ConfigureAwait(false);
cancellationToken).ConfigureAwait(false);

// Process tool output
ToolOutput[] toolOutputs = GenerateToolOutputs(functionResults);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static async IAsyncEnumerable<ChatMessageContent> InternalInvokeAsync(
}

List<FunctionCallContent> functionCallContents = [];
await foreach (var responseEvent in invokeAgentResponse.Completion.ToAsyncEnumerable().ConfigureAwait(false))
foreach (var responseEvent in invokeAgentResponse.Completion)
{
if (responseEvent is BedrockAgentRuntimeEventStreamException bedrockAgentRuntimeEventStreamException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ async Task<FunctionResult> InvokeAgentAsync(Kernel kernel, KernelFunction functi
}

var response = agent.InvokeAsync(new ChatMessageContent(AuthorRole.User, queryString), null, options, cancellationToken);
var responseItems = await response.ToArrayAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
var chatMessages = responseItems.Select(i => i.Message).ToArray();

List<ChatMessageContent> chatMessages = [];
await foreach (var item in response.WithCancellation(cancellationToken).ConfigureAwait(false))
{
chatMessages.Add(item.Message);
}
return new FunctionResult(function, chatMessages, kernel.Culture);
}

Expand Down
22 changes: 15 additions & 7 deletions dotnet/src/Agents/OpenAI/Internal/AssistantThreadActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ public static async IAsyncEnumerable<ChatMessageContent> GetMessagesAsync(Assist
throw new KernelException($"Agent Failure - Run terminated: {run.Status} [{run.Id}]: {run.LastError?.Message ?? "Unknown"}");
}

RunStep[] steps = await client.GetRunStepsAsync(run.ThreadId, run.Id, cancellationToken: cancellationToken).ToArrayAsync(cancellationToken).ConfigureAwait(false);
List<RunStep> steps = [];
await foreach (var step in client.GetRunStepsAsync(run.ThreadId, run.Id, cancellationToken: cancellationToken).ConfigureAwait(false))
{
steps.Add(step);
}

// Is tool action required?
if (run.Status == RunStatus.RequiresAction)
Expand All @@ -187,7 +191,7 @@ await functionProcessor.InvokeFunctionCallsAsync(
functionOptions,
kernel,
isStreaming: false,
cancellationToken).ToArrayAsync(cancellationToken).ConfigureAwait(false);
cancellationToken).ConfigureAwait(false);

// Capture function-call for message processing
foreach (FunctionResultContent functionCall in functionResults)
Expand Down Expand Up @@ -453,10 +457,14 @@ public static async IAsyncEnumerable<StreamingChatMessageContent> InvokeStreamin

if (run.Status == RunStatus.RequiresAction)
{
RunStep[] activeSteps =
await client.GetRunStepsAsync(run.ThreadId, run.Id, cancellationToken: cancellationToken)
.Where(step => step.Status == RunStepStatus.InProgress)
.ToArrayAsync(cancellationToken).ConfigureAwait(false);
List<RunStep> activeSteps = [];
await foreach (var step in client.GetRunStepsAsync(run.ThreadId, run.Id, cancellationToken: cancellationToken).ConfigureAwait(false))
{
if (step.Status == RunStepStatus.InProgress)
{
activeSteps.Add(step);
}
}

// Capture map between the tool call and its associated step
Dictionary<string, string> toolMap = [];
Expand All @@ -483,7 +491,7 @@ await functionProcessor.InvokeFunctionCallsAsync(
functionOptions,
kernel,
isStreaming: true,
cancellationToken).ToArrayAsync(cancellationToken).ConfigureAwait(false);
cancellationToken).ConfigureAwait(false);

// Process tool output
ToolOutput[] toolOutputs = GenerateToolOutputs(functionResults);
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/Agents/OpenAI/Internal/ResponseThreadActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ await functionProcessor.InvokeFunctionCallsAsync(
functionOptions,
agent.GetKernel(options),
isStreaming: false,
cancellationToken).ToArrayAsync(cancellationToken).ConfigureAwait(false);
cancellationToken).ConfigureAwait(false);
var functionOutputItems = functionResults.Select(fr => ResponseItem.CreateFunctionCallOutputItem(fr.CallId, fr.Result?.ToString() ?? string.Empty)).ToList();

// If store is enabled we only need to send the function output items
Expand Down Expand Up @@ -259,7 +259,7 @@ await functionProcessor.InvokeFunctionCallsAsync(
functionOptions,
agent.GetKernel(options),
isStreaming: true,
cancellationToken).ToArrayAsync(cancellationToken).ConfigureAwait(false);
cancellationToken).ConfigureAwait(false);
var functionOutputItems = functionResults.Select(fr => ResponseItem.CreateFunctionCallOutputItem(fr.CallId, fr.Result?.ToString() ?? string.Empty)).ToList();

// If store is enabled we only need to send the function output items
Expand Down
20 changes: 12 additions & 8 deletions dotnet/src/Agents/Orchestration/AgentActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -123,14 +122,19 @@ async Task HandleMessageAsync(ChatMessageContent message)

private async Task InvokeAsync(IList<ChatMessageContent> input, AgentInvokeOptions options, CancellationToken cancellationToken)
{
AgentResponseItem<ChatMessageContent>? lastResponse =
await this.Agent.InvokeAsync(
input,
this.Thread,
options,
cancellationToken).LastOrDefaultAsync(cancellationToken).ConfigureAwait(false);
var last = default(AgentResponseItem<ChatMessageContent>)!;
var hasLast = false;

await foreach (var item in this.Agent.InvokeAsync(input, this.Thread, options, cancellationToken).ConfigureAwait(false))
{
hasLast = true;
last = item;
}

this.Thread ??= lastResponse?.Thread;
if (this.Thread is null && hasLast)
{
this.Thread = last.Thread;
}
}

private async Task InvokeStreamingAsync(IList<ChatMessageContent> input, AgentInvokeOptions options, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.SemanticKernel.Agents.Runtime.Abstractions.Tests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions dotnet/src/Agents/UnitTests/Agents.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageReference Include="Microsoft.Extensions.Http.Resilience" VersionOverride="9.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" VersionOverride="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" VersionOverride="9.0.5" />
<PackageReference Include="System.Linq.AsyncEnumerable" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
Expand Down
1 change: 0 additions & 1 deletion dotnet/src/Agents/Yaml/Agents.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="SemanticKernel.Agents.UnitTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="System.Linq.Async" />
<PackageReference Include="xRetry" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
Expand Down
Loading
Loading