Skip to content

Commit 47a225b

Browse files
Copilotadamsitnik
andauthored
Replace custom IAsyncEnumerable extensions with System.Linq.AsyncEnumerable (#7039)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
1 parent 30d910f commit 47a225b

File tree

12 files changed

+20
-120
lines changed

12 files changed

+20
-120
lines changed

eng/packages/General.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<PackageVersion Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
3333
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
3434
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
35+
<PackageVersion Include="System.Linq.AsyncEnumerable" Version="10.0.0" />
3536
<PackageVersion Include="System.Memory" Version="4.5.5" />
3637
<PackageVersion Include="System.Private.Uri" Version="4.3.2" />
3738
<PackageVersion Include="System.Runtime.Caching" Version="$(SystemRuntimeCachingVersion)" />

src/Libraries/Microsoft.Extensions.DataIngestion/Microsoft.Extensions.DataIngestion.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@
2828
<PackageReference Include="System.Numerics.Tensors" />
2929
</ItemGroup>
3030

31+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net10.0'))">
32+
<PackageReference Include="System.Linq.AsyncEnumerable" />
33+
</ItemGroup>
34+
3135
</Project>

src/Libraries/Microsoft.Extensions.DataIngestion/Utils/Batching.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6-
#if NET10_0_OR_GREATER
76
using System.Linq;
8-
#endif
97
using System.Runtime.CompilerServices;
108
using System.Threading;
119
using System.Threading.Tasks;
@@ -69,40 +67,4 @@ internal static async IAsyncEnumerable<IngestionChunk<string>> ProcessAsync<TMet
6967
}
7068
}
7169
}
72-
73-
#if !NET10_0_OR_GREATER
74-
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods
75-
private static IAsyncEnumerable<TSource[]> Chunk<TSource>(this IAsyncEnumerable<TSource> source, int count)
76-
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods
77-
{
78-
_ = Throw.IfNull(source);
79-
_ = Throw.IfLessThanOrEqual(count, 0);
80-
81-
return CoreAsync(source, count);
82-
83-
static async IAsyncEnumerable<TSource[]> CoreAsync(IAsyncEnumerable<TSource> source, int count,
84-
[EnumeratorCancellation] CancellationToken cancellationToken = default)
85-
{
86-
var buffer = new TSource[count];
87-
int index = 0;
88-
89-
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
90-
{
91-
buffer[index++] = item;
92-
93-
if (index == count)
94-
{
95-
index = 0;
96-
yield return buffer;
97-
}
98-
}
99-
100-
if (index > 0)
101-
{
102-
Array.Resize(ref buffer, index);
103-
yield return buffer;
104-
}
105-
}
106-
}
107-
#endif
10870
}

src/ProjectTemplates/GeneratedContent.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<TemplatePackageVersion_OllamaSharp>5.4.8</TemplatePackageVersion_OllamaSharp>
5555
<TemplatePackageVersion_OpenTelemetry>1.13.0</TemplatePackageVersion_OpenTelemetry>
5656
<TemplatePackageVersion_PdfPig>0.1.11</TemplatePackageVersion_PdfPig>
57-
<TemplatePackageVersion_SystemLinqAsync>6.0.3</TemplatePackageVersion_SystemLinqAsync>
57+
<TemplatePackageVersion_SystemLinqAsyncEnumerable>10.0.0</TemplatePackageVersion_SystemLinqAsyncEnumerable>
5858
</PropertyGroup>
5959

6060
<PropertyGroup>
@@ -83,7 +83,7 @@
8383
TemplatePackageVersion_OllamaSharp=$(TemplatePackageVersion_OllamaSharp);
8484
TemplatePackageVersion_OpenTelemetry=$(TemplatePackageVersion_OpenTelemetry);
8585
TemplatePackageVersion_PdfPig=$(TemplatePackageVersion_PdfPig);
86-
TemplatePackageVersion_SystemLinqAsync=$(TemplatePackageVersion_SystemLinqAsync);
86+
TemplatePackageVersion_SystemLinqAsyncEnumerable=$(TemplatePackageVersion_SystemLinqAsyncEnumerable);
8787

8888
<!-- Other properties -->
8989
LocalChatTemplateVariant=$(_LocalChatTemplateVariant);

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData-CSharp.Web/ChatWithCustomData-CSharp.Web.csproj.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<PackageReference Include="Microsoft.ML.Tokenizers.Data.Cl100kBase" Version="${TemplatePackageVersion_MicrosoftMLTokenizers}" />
4141
<PackageReference Include="Microsoft.ML.Tokenizers.Data.O200kBase" Version="${TemplatePackageVersion_MicrosoftMLTokenizers}" />
4242
<!--#if (IsNET9) -->
43-
<PackageReference Include="System.Linq.Async" Version="${TemplatePackageVersion_SystemLinqAsync}" />
43+
<PackageReference Include="System.Linq.AsyncEnumerable" Version="${TemplatePackageVersion_SystemLinqAsyncEnumerable}" />
4444
<!--#endif -->
4545
<!--#if (IsAzureAISearch && IsAspire)
4646
<PackageReference Include="Aspire.Azure.Search.Documents" Version="${TemplatePackageVersion_Aspire}" />

test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/DistributedCachingChatClientTest.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public async Task StreamingCachesSuccessResultsAsync(bool conversationIdSet)
276276
GetStreamingResponseAsyncCallback = delegate
277277
{
278278
innerCallCount++;
279-
return ToAsyncEnumerableAsync(actualUpdate);
279+
return actualUpdate.ToAsyncEnumerable();
280280
}
281281
};
282282
using var outer = new DistributedCachingChatClient(testClient, _storage)
@@ -320,7 +320,7 @@ public async Task StreamingCoalescesConsecutiveTextChunksAsync(bool? coalesce)
320320

321321
using var testClient = new TestChatClient
322322
{
323-
GetStreamingResponseAsyncCallback = delegate { return ToAsyncEnumerableAsync(expectedResponse); }
323+
GetStreamingResponseAsyncCallback = delegate { return expectedResponse.ToAsyncEnumerable(); }
324324
};
325325
using var outer = new DistributedCachingChatClient(testClient, _storage)
326326
{
@@ -398,7 +398,7 @@ public async Task StreamingCoalescingPropagatesMetadataAsync()
398398

399399
using var testClient = new TestChatClient
400400
{
401-
GetStreamingResponseAsyncCallback = delegate { return ToAsyncEnumerableAsync(expectedResponse); }
401+
GetStreamingResponseAsyncCallback = delegate { return expectedResponse.ToAsyncEnumerable(); }
402402
};
403403
using var outer = new DistributedCachingChatClient(testClient, _storage)
404404
{
@@ -772,9 +772,6 @@ private static async Task<List<T>> ToListAsync<T>(IAsyncEnumerable<T> values)
772772
return result;
773773
}
774774

775-
private static IAsyncEnumerable<T> ToAsyncEnumerableAsync<T>(IEnumerable<T> values)
776-
=> ToAsyncEnumerableAsync(Task.CompletedTask, values);
777-
778775
private static IAsyncEnumerable<T> ToAsyncEnumerableAsync<T>(Task preTask, IEnumerable<T> valueFactories)
779776
=> ToAsyncEnumerableAsync(preTask, valueFactories.Select<T, Func<T>>(v => () => v));
780777

test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/ReducingChatClientTests.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.Extensions.DependencyInjection;
@@ -61,7 +62,7 @@ public async Task GetResponseAsync_CallsReducerBeforeInnerClient(bool streaming)
6162
{
6263
// Verify that the inner client receives the reduced messages
6364
Assert.Same(reducedMessages, messages);
64-
return ToAsyncEnumerable(expectedUpdates);
65+
return expectedUpdates.ToAsyncEnumerable();
6566
}
6667
};
6768

@@ -163,15 +164,6 @@ public async Task UseChatReducer_WithConfigureCallback()
163164
Assert.IsType<ReducingChatClient>(configuredClient);
164165
}
165166

166-
private static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(IEnumerable<T> items)
167-
{
168-
foreach (var item in items)
169-
{
170-
await Task.Yield();
171-
yield return item;
172-
}
173-
}
174-
175167
private sealed class TestReducer : IChatReducer
176168
{
177169
public IEnumerable<ChatMessage>? ReducedMessages { get; set; }

test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
<PackageReference Include="OpenTelemetry.Exporter.InMemory" />
3232
</ItemGroup>
3333

34+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net10.0'))">
35+
<PackageReference Include="System.Linq.AsyncEnumerable" />
36+
</ItemGroup>
37+
3438
<ItemGroup>
3539
<ProjectReference Include="..\..\..\src\Libraries\Microsoft.Extensions.Diagnostics.Testing\Microsoft.Extensions.Diagnostics.Testing.csproj" />
3640
<ProjectReference Include="..\..\..\src\Libraries\Microsoft.Extensions.AI.Abstractions\Microsoft.Extensions.AI.Abstractions.csproj" />

test/Libraries/Microsoft.Extensions.DataIngestion.Tests/Chunkers/DocumentChunkerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Threading.Tasks;
78
using Xunit;
89

test/Libraries/Microsoft.Extensions.DataIngestion.Tests/Chunkers/HeaderChunkerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Threading.Tasks;
78
using Microsoft.ML.Tokenizers;
89
using Xunit;

0 commit comments

Comments
 (0)