diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 681349e..6ec5b94 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,9 +14,9 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Build - run: dotnet build src/Cnblogs.DashScope.AspNetCore -c Release + run: dotnet build src/Cnblogs.DashScope.AspNetCore -c Release -p:TargetFrameworks=net6.0 - name: Test - run: dotnet test test/Cnblogs.DashScope.Sdk.UnitTests -c Release + run: dotnet test test/Cnblogs.DashScope.Sdk.UnitTests -c Release -p:TargetFrameworks=net6.0 test-net8: runs-on: ubuntu-latest container: mcr.microsoft.com/dotnet/sdk:8.0 @@ -24,7 +24,7 @@ jobs: - name: Checkout uses: actions/checkout@v4 - name: Build - run: dotnet build src/Cnblogs.DashScope.AI -c Release + run: dotnet build src/Cnblogs.DashScope.AI -c Release -p:TargetFrameworks=net8.0 - name: Test - run: dotnet test test/Cnblogs.DashScope.AI.UnitTests -c Release + run: dotnet test test/Cnblogs.DashScope.AI.UnitTests -c Release -p:TargetFrameworks=net8.0 && dotnet test test/Cnblogs.DashScope.Sdk.UnitTests -c Release -p:TargetFrameworks=net8.0 diff --git a/Directory.Build.props b/Directory.Build.props index 3eaabd9..c98ba0c 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - net6.0 + net6.0;net8.0 enable enable Cnblogs diff --git a/sample/Cnblogs.DashScope.Sample/Cnblogs.DashScope.Sample.csproj b/sample/Cnblogs.DashScope.Sample/Cnblogs.DashScope.Sample.csproj index 00a1318..1929eb1 100644 --- a/sample/Cnblogs.DashScope.Sample/Cnblogs.DashScope.Sample.csproj +++ b/sample/Cnblogs.DashScope.Sample/Cnblogs.DashScope.Sample.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net8.0 enable enable false diff --git a/src/Cnblogs.DashScope.AI/Cnblogs.DashScope.AI.csproj b/src/Cnblogs.DashScope.AI/Cnblogs.DashScope.AI.csproj index d75ae37..039cbfe 100644 --- a/src/Cnblogs.DashScope.AI/Cnblogs.DashScope.AI.csproj +++ b/src/Cnblogs.DashScope.AI/Cnblogs.DashScope.AI.csproj @@ -1,5 +1,6 @@  + net8.0 Cnblogs.DashScope.AI true Cnblogs;Dashscope;Microsoft.Extensions.AI;Sdk;Embedding; diff --git a/src/Cnblogs.DashScope.Core/Cnblogs.DashScope.Core.csproj b/src/Cnblogs.DashScope.Core/Cnblogs.DashScope.Core.csproj index 7dd23dd..c5fab03 100644 --- a/src/Cnblogs.DashScope.Core/Cnblogs.DashScope.Core.csproj +++ b/src/Cnblogs.DashScope.Core/Cnblogs.DashScope.Core.csproj @@ -13,6 +13,9 @@ + + + diff --git a/src/Cnblogs.DashScope.Core/DashScopeClientCore.cs b/src/Cnblogs.DashScope.Core/DashScopeClientCore.cs index 72c8c69..5041955 100644 --- a/src/Cnblogs.DashScope.Core/DashScopeClientCore.cs +++ b/src/Cnblogs.DashScope.Core/DashScopeClientCore.cs @@ -552,28 +552,31 @@ private async IAsyncEnumerable StreamAsync( HttpCompletionOption.ResponseHeadersRead, cancellationToken); using StreamReader reader = new(await response.Content.ReadAsStreamAsync(cancellationToken), Encoding.UTF8); - while (!reader.EndOfStream) + while (await reader.ReadLineAsync() is { } line) { if (cancellationToken.IsCancellationRequested) + { throw new TaskCanceledException(); + } + + if (line.StartsWith("data:") == false) + { + continue; + } - var line = await reader.ReadLineAsync(); - if (line != null && line.StartsWith("data:")) + var data = line["data:".Length..]; + if (data.StartsWith("{\"code\":")) { - var data = line["data:".Length..]; - if (data.StartsWith("{\"code\":")) - { - var error = - JsonSerializer.Deserialize(data, DashScopeDefaults.SerializationOptions)!; - throw new DashScopeException( - message.RequestUri?.ToString(), - (int)response.StatusCode, - error, - error.Message); - } - - yield return JsonSerializer.Deserialize(data, DashScopeDefaults.SerializationOptions)!; + var error = + JsonSerializer.Deserialize(data, DashScopeDefaults.SerializationOptions)!; + throw new DashScopeException( + message.RequestUri?.ToString(), + (int)response.StatusCode, + error, + error.Message); } + + yield return JsonSerializer.Deserialize(data, DashScopeDefaults.SerializationOptions)!; } } diff --git a/test/Cnblogs.DashScope.AI.UnitTests/Cnblogs.DashScope.AI.UnitTests.csproj b/test/Cnblogs.DashScope.AI.UnitTests/Cnblogs.DashScope.AI.UnitTests.csproj index 82cd87a..063acd8 100644 --- a/test/Cnblogs.DashScope.AI.UnitTests/Cnblogs.DashScope.AI.UnitTests.csproj +++ b/test/Cnblogs.DashScope.AI.UnitTests/Cnblogs.DashScope.AI.UnitTests.csproj @@ -1,7 +1,7 @@  - net8.0 + net8.0 enable enable false diff --git a/test/Cnblogs.DashScope.Sdk.UnitTests/Cnblogs.DashScope.Sdk.UnitTests.csproj b/test/Cnblogs.DashScope.Sdk.UnitTests/Cnblogs.DashScope.Sdk.UnitTests.csproj index 11240e0..cd35c5f 100644 --- a/test/Cnblogs.DashScope.Sdk.UnitTests/Cnblogs.DashScope.Sdk.UnitTests.csproj +++ b/test/Cnblogs.DashScope.Sdk.UnitTests/Cnblogs.DashScope.Sdk.UnitTests.csproj @@ -1,7 +1,6 @@ - net6.0 false true @@ -13,7 +12,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/Cnblogs.DashScope.Tests.Shared/Cnblogs.DashScope.Tests.Shared.csproj b/test/Cnblogs.DashScope.Tests.Shared/Cnblogs.DashScope.Tests.Shared.csproj index f4a861c..62e5330 100644 --- a/test/Cnblogs.DashScope.Tests.Shared/Cnblogs.DashScope.Tests.Shared.csproj +++ b/test/Cnblogs.DashScope.Tests.Shared/Cnblogs.DashScope.Tests.Shared.csproj @@ -1,7 +1,6 @@ - net6.0 enable enable false @@ -9,7 +8,7 @@ - +