@@ -180,17 +180,8 @@ class Anthropic extends BaseLLM {
180
180
181
181
const msgs = this . convertMessages ( messages ) ;
182
182
183
- // Merge default headers with custom headers
184
- const headers : any = {
185
- "Content-Type" : "application/json" ,
186
- Accept : "application/json" ,
187
- "anthropic-version" : "2023-06-01" ,
188
- "x-api-key" : this . apiKey as string ,
189
- ...this . requestOptions ?. headers ,
190
- } ;
191
-
192
- // Handle the special case for anthropic-beta
193
- this . setBetaHeaders ( headers , shouldCacheSystemMessage ) ;
183
+ // Set anthropic-beta headers directly into this.requestOptions
184
+ this . setBetaHeaders ( shouldCacheSystemMessage ) ;
194
185
195
186
// Create the request body
196
187
const requestBody = {
@@ -209,7 +200,12 @@ class Anthropic extends BaseLLM {
209
200
210
201
const response = await this . fetch ( new URL ( "messages" , this . apiBase ) , {
211
202
method : "POST" ,
212
- headers,
203
+ headers : {
204
+ "Content-Type" : "application/json" ,
205
+ Accept : "application/json" ,
206
+ "anthropic-version" : "2023-06-01" ,
207
+ "x-api-key" : this . apiKey as string
208
+ } ,
213
209
body : JSON . stringify ( requestBody ) ,
214
210
signal,
215
211
} ) ;
@@ -285,38 +281,35 @@ class Anthropic extends BaseLLM {
285
281
}
286
282
break ;
287
283
case "content_block_stop" :
288
- lastToolUseId = undefined ;
289
- lastToolUseName = undefined ;
284
+ lastToolUseId = undefined ;
285
+ lastToolUseName = undefined ;
290
286
break ;
291
287
default :
292
288
break ;
293
289
}
294
290
}
295
291
}
296
292
297
- private setBetaHeaders (
298
- headers : any ,
299
- shouldCacheSystemMessage : boolean | undefined ,
300
- ) {
293
+ private setBetaHeaders ( shouldCacheSystemMessage : boolean | undefined ) {
294
+ if ( ! this . requestOptions ) this . requestOptions = { } ;
295
+ if ( ! this . requestOptions . headers ) this . requestOptions . headers = { } ;
296
+
301
297
const betaValues = new Set < string > ( ) ;
302
298
303
- // Add from existing header if present
304
- const existingBeta = headers [ "anthropic-beta" ] ;
305
- if ( existingBeta && typeof existingBeta === "string" ) {
306
- existingBeta
299
+ const existingBetaHeaders = this . requestOptions . headers [ "anthropic-beta" ] ;
300
+ if ( existingBetaHeaders ) {
301
+ existingBetaHeaders
307
302
. split ( "," )
308
303
. map ( ( v ) => v . trim ( ) )
309
304
. forEach ( ( v ) => betaValues . add ( v ) ) ;
310
305
}
311
306
312
- // Add caching header if we should
313
307
if ( shouldCacheSystemMessage || this . cacheBehavior ?. cacheConversation ) {
314
308
betaValues . add ( "prompt-caching-2024-07-31" ) ;
315
309
}
316
310
317
- // Update the header if we have values
318
311
if ( betaValues . size > 0 ) {
319
- headers [ "anthropic-beta" ] = Array . from ( betaValues ) . join ( "," ) ;
312
+ this . requestOptions . headers [ "anthropic-beta" ] = Array . from ( betaValues ) . join ( "," ) ;
320
313
}
321
314
}
322
315
}
0 commit comments