@@ -552,28 +552,31 @@ private async IAsyncEnumerable<TResponse> StreamAsync<TResponse>(
552552 HttpCompletionOption . ResponseHeadersRead ,
553553 cancellationToken ) ;
554554 using StreamReader reader = new ( await response . Content . ReadAsStreamAsync ( cancellationToken ) , Encoding . UTF8 ) ;
555- while ( ! reader . EndOfStream )
555+ while ( await reader . ReadLineAsync ( ) is { } line )
556556 {
557557 if ( cancellationToken . IsCancellationRequested )
558+ {
558559 throw new TaskCanceledException ( ) ;
560+ }
561+
562+ if ( line . StartsWith ( "data:" ) == false )
563+ {
564+ continue ;
565+ }
559566
560- var line = await reader . ReadLineAsync ( ) ;
561- if ( line != null && line . StartsWith ( "data :" ) )
567+ var data = line [ "data:" . Length .. ] ;
568+ if ( data . StartsWith ( "{ \" code \" :" ) )
562569 {
563- var data = line [ "data:" . Length ..] ;
564- if ( data . StartsWith ( "{\" code\" :" ) )
565- {
566- var error =
567- JsonSerializer . Deserialize < DashScopeError > ( data , DashScopeDefaults . SerializationOptions ) ! ;
568- throw new DashScopeException (
569- message . RequestUri ? . ToString ( ) ,
570- ( int ) response . StatusCode ,
571- error ,
572- error . Message ) ;
573- }
574-
575- yield return JsonSerializer . Deserialize < TResponse > ( data , DashScopeDefaults . SerializationOptions ) ! ;
570+ var error =
571+ JsonSerializer . Deserialize < DashScopeError > ( data , DashScopeDefaults . SerializationOptions ) ! ;
572+ throw new DashScopeException (
573+ message . RequestUri ? . ToString ( ) ,
574+ ( int ) response . StatusCode ,
575+ error ,
576+ error . Message ) ;
576577 }
578+
579+ yield return JsonSerializer . Deserialize < TResponse > ( data , DashScopeDefaults . SerializationOptions ) ! ;
577580 }
578581 }
579582
0 commit comments