3
3
using System . Text . Json ;
4
4
using System . Text . Json . Serialization ;
5
5
using System . Text . Json . Serialization . Metadata ;
6
-
7
- using Azure ;
8
- using Azure . Core ;
9
- using Azure . Core . Pipeline ;
6
+ using System . ClientModel . Primitives ;
10
7
11
8
namespace AIShell . OpenAI . Agent ;
12
9
@@ -134,69 +131,25 @@ public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions option
134
131
}
135
132
}
136
133
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
-
169
134
/// <summary>
170
- /// Used for configuring the retry policy for Azure.OpenAI.Client .
135
+ /// Initializes a new instance of the <see cref="ChatRetryPolicy"/> class .
171
136
/// </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 )
173
140
{
174
141
private const string RetryAfterHeaderName = "Retry-After" ;
175
142
private const string RetryAfterMsHeaderName = "retry-after-ms" ;
176
143
private const string XRetryAfterMsHeaderName = "x-ms-retry-after-ms" ;
177
144
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 ) ) ;
194
147
195
- private bool ShouldRetryImpl ( HttpMessage message , Exception ? exception )
148
+ private bool ShouldRetryImpl ( PipelineMessage message , Exception exception )
196
149
{
197
150
bool result = base . ShouldRetry ( message , exception ) ;
198
151
199
- if ( result && message . HasResponse )
152
+ if ( result && message . Response is not null )
200
153
{
201
154
TimeSpan ? retryAfter = GetRetryAfterHeaderValue ( message . Response . Headers ) ;
202
155
if ( retryAfter > TimeSpan . FromSeconds ( 5 ) )
@@ -209,22 +162,22 @@ private bool ShouldRetryImpl(HttpMessage message, Exception? exception)
209
162
return result ;
210
163
}
211
164
212
- private static TimeSpan ? GetRetryAfterHeaderValue ( ResponseHeaders headers )
165
+ private static TimeSpan ? GetRetryAfterHeaderValue ( PipelineResponseHeaders headers )
213
166
{
214
167
if ( headers . TryGetValue ( RetryAfterMsHeaderName , out var retryAfterValue ) ||
215
168
headers . TryGetValue ( XRetryAfterMsHeaderName , out retryAfterValue ) )
216
169
{
217
- if ( int . TryParse ( retryAfterValue , out var delaySeconds ) )
170
+ if ( int . TryParse ( retryAfterValue , out var delayInMS ) )
218
171
{
219
- return TimeSpan . FromMilliseconds ( delaySeconds ) ;
172
+ return TimeSpan . FromMilliseconds ( delayInMS ) ;
220
173
}
221
174
}
222
175
223
176
if ( headers . TryGetValue ( RetryAfterHeaderName , out retryAfterValue ) )
224
177
{
225
- if ( int . TryParse ( retryAfterValue , out var delaySeconds ) )
178
+ if ( int . TryParse ( retryAfterValue , out var delayInSec ) )
226
179
{
227
- return TimeSpan . FromSeconds ( delaySeconds ) ;
180
+ return TimeSpan . FromSeconds ( delayInSec ) ;
228
181
}
229
182
230
183
if ( DateTimeOffset . TryParse ( retryAfterValue , out DateTimeOffset delayTime ) )
0 commit comments