Skip to content

Commit 82b07e7

Browse files
Set anthropic-beta headers directly into this.requestOptions
1 parent c9c434d commit 82b07e7

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

core/llm/llms/Anthropic.ts

+18-25
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,8 @@ class Anthropic extends BaseLLM {
180180

181181
const msgs = this.convertMessages(messages);
182182

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);
194185

195186
// Create the request body
196187
const requestBody = {
@@ -209,7 +200,12 @@ class Anthropic extends BaseLLM {
209200

210201
const response = await this.fetch(new URL("messages", this.apiBase), {
211202
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+
},
213209
body: JSON.stringify(requestBody),
214210
signal,
215211
});
@@ -285,38 +281,35 @@ class Anthropic extends BaseLLM {
285281
}
286282
break;
287283
case "content_block_stop":
288-
lastToolUseId = undefined;
289-
lastToolUseName = undefined;
284+
lastToolUseId = undefined;
285+
lastToolUseName = undefined;
290286
break;
291287
default:
292288
break;
293289
}
294290
}
295291
}
296292

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+
301297
const betaValues = new Set<string>();
302298

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
307302
.split(",")
308303
.map((v) => v.trim())
309304
.forEach((v) => betaValues.add(v));
310305
}
311306

312-
// Add caching header if we should
313307
if (shouldCacheSystemMessage || this.cacheBehavior?.cacheConversation) {
314308
betaValues.add("prompt-caching-2024-07-31");
315309
}
316310

317-
// Update the header if we have values
318311
if (betaValues.size > 0) {
319-
headers["anthropic-beta"] = Array.from(betaValues).join(",");
312+
this.requestOptions.headers["anthropic-beta"] = Array.from(betaValues).join(",");
320313
}
321314
}
322315
}

0 commit comments

Comments
 (0)