33using System . Text . Json ;
44using System . Text . Json . Serialization ;
55using System . Text . Json . Serialization . Metadata ;
6-
7- using Azure ;
8- using Azure . Core ;
9- using Azure . Core . Pipeline ;
6+ using System . ClientModel . Primitives ;
107
118namespace AIShell . OpenAI . Agent ;
129
@@ -134,69 +131,25 @@ public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions option
134131 }
135132}
136133
137- #nullable enable
138-
139- /// <summary>
140- /// Used for setting user key for the Azure.OpenAI.Client.
141- /// </summary>
142- internal sealed class UserKeyPolicy : HttpPipelineSynchronousPolicy
143- {
144- private readonly string _name ;
145- private readonly AzureKeyCredential _credential ;
146-
147- /// <summary>
148- /// Initializes a new instance of the <see cref="UserKeyPolicy"/> class.
149- /// </summary>
150- /// <param name="credential">The <see cref="AzureKeyCredential"/> used to authenticate requests.</param>
151- /// <param name="name">The name of the key header used for the credential.</param>
152- public UserKeyPolicy ( AzureKeyCredential credential , string name )
153- {
154- ArgumentNullException . ThrowIfNull ( credential ) ;
155- ArgumentException . ThrowIfNullOrEmpty ( name ) ;
156-
157- _credential = credential ;
158- _name = name ;
159- }
160-
161- /// <inheritdoc/>
162- public override void OnSendingRequest ( HttpMessage message )
163- {
164- base . OnSendingRequest ( message ) ;
165- message . Request . Headers . SetValue ( _name , _credential . Key ) ;
166- }
167- }
168-
169134/// <summary>
170- /// Used for configuring the retry policy for Azure.OpenAI.Client .
135+ /// Initializes a new instance of the <see cref="ChatRetryPolicy"/> class .
171136/// </summary>
172- internal sealed class ChatRetryPolicy : RetryPolicy
137+ /// <param name="maxRetries">The maximum number of retries to attempt.</param>
138+ /// <param name="delayStrategy">The delay to use for computing the interval between retry attempts.</param>
139+ internal sealed class ChatRetryPolicy ( int maxRetries = 2 ) : ClientRetryPolicy ( maxRetries )
173140{
174141 private const string RetryAfterHeaderName = "Retry-After" ;
175142 private const string RetryAfterMsHeaderName = "retry-after-ms" ;
176143 private const string XRetryAfterMsHeaderName = "x-ms-retry-after-ms" ;
177144
178- /// <summary>
179- /// Initializes a new instance of the <see cref="ChatRetryPolicy"/> class.
180- /// </summary>
181- /// <param name="maxRetries">The maximum number of retries to attempt.</param>
182- /// <param name="delayStrategy">The delay to use for computing the interval between retry attempts.</param>
183- public ChatRetryPolicy ( int maxRetries = 2 , DelayStrategy ? delayStrategy = default ) : base (
184- maxRetries ,
185- delayStrategy ?? DelayStrategy . CreateExponentialDelayStrategy (
186- initialDelay : TimeSpan . FromSeconds ( 0.8 ) ,
187- maxDelay : TimeSpan . FromSeconds ( 5 ) ) )
188- {
189- // By default, we retry 2 times at most, and use a delay strategy that waits 5 seconds at most between retries.
190- }
191-
192- protected override bool ShouldRetry ( HttpMessage message , Exception ? exception ) => ShouldRetryImpl ( message , exception ) ;
193- protected override ValueTask < bool > ShouldRetryAsync ( HttpMessage message , Exception ? exception ) => new ( ShouldRetryImpl ( message , exception ) ) ;
145+ protected override bool ShouldRetry ( PipelineMessage message , Exception exception ) => ShouldRetryImpl ( message , exception ) ;
146+ protected override ValueTask < bool > ShouldRetryAsync ( PipelineMessage message , Exception exception ) => new ( ShouldRetryImpl ( message , exception ) ) ;
194147
195- private bool ShouldRetryImpl ( HttpMessage message , Exception ? exception )
148+ private bool ShouldRetryImpl ( PipelineMessage message , Exception exception )
196149 {
197150 bool result = base . ShouldRetry ( message , exception ) ;
198151
199- if ( result && message . HasResponse )
152+ if ( result && message . Response is not null )
200153 {
201154 TimeSpan ? retryAfter = GetRetryAfterHeaderValue ( message . Response . Headers ) ;
202155 if ( retryAfter > TimeSpan . FromSeconds ( 5 ) )
@@ -209,22 +162,22 @@ private bool ShouldRetryImpl(HttpMessage message, Exception? exception)
209162 return result ;
210163 }
211164
212- private static TimeSpan ? GetRetryAfterHeaderValue ( ResponseHeaders headers )
165+ private static TimeSpan ? GetRetryAfterHeaderValue ( PipelineResponseHeaders headers )
213166 {
214167 if ( headers . TryGetValue ( RetryAfterMsHeaderName , out var retryAfterValue ) ||
215168 headers . TryGetValue ( XRetryAfterMsHeaderName , out retryAfterValue ) )
216169 {
217- if ( int . TryParse ( retryAfterValue , out var delaySeconds ) )
170+ if ( int . TryParse ( retryAfterValue , out var delayInMS ) )
218171 {
219- return TimeSpan . FromMilliseconds ( delaySeconds ) ;
172+ return TimeSpan . FromMilliseconds ( delayInMS ) ;
220173 }
221174 }
222175
223176 if ( headers . TryGetValue ( RetryAfterHeaderName , out retryAfterValue ) )
224177 {
225- if ( int . TryParse ( retryAfterValue , out var delaySeconds ) )
178+ if ( int . TryParse ( retryAfterValue , out var delayInSec ) )
226179 {
227- return TimeSpan . FromSeconds ( delaySeconds ) ;
180+ return TimeSpan . FromSeconds ( delayInSec ) ;
228181 }
229182
230183 if ( DateTimeOffset . TryParse ( retryAfterValue , out DateTimeOffset delayTime ) )
0 commit comments