From d65ddead11ad9e14a6b7eb522c5e2fceb6e5df53 Mon Sep 17 00:00:00 2001
From: lloydzhou <lloydzhou@qq.com>
Date: Mon, 1 Jul 2024 09:41:01 +0000
Subject: [PATCH] fix: anthropic client using common getHeaders

---
 app/client/api.ts                 | 7 +++++--
 app/client/platforms/anthropic.ts | 6 ++----
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/app/client/api.ts b/app/client/api.ts
index 7bee546b4..502c74698 100644
--- a/app/client/api.ts
+++ b/app/client/api.ts
@@ -162,14 +162,17 @@ export function getHeaders() {
   const modelConfig = useChatStore.getState().currentSession().mask.modelConfig;
   const isGoogle = modelConfig.model.startsWith("gemini");
   const isAzure = accessStore.provider === ServiceProvider.Azure;
-  const authHeader = isAzure ? "api-key" : "Authorization";
+  const isAnthropic = accessStore.provider === ServiceProvider.Anthropic;
+  const authHeader = isAzure ? "api-key" : isAnthropic ? 'x-api-key' : "Authorization";
   const apiKey = isGoogle
     ? accessStore.googleApiKey
     : isAzure
     ? accessStore.azureApiKey
+    : isAnthropic
+    ? accessStore.anthropicApiKey
     : accessStore.openaiApiKey;
   const clientConfig = getClientConfig();
-  const makeBearer = (s: string) => `${isAzure ? "" : "Bearer "}${s.trim()}`;
+  const makeBearer = (s: string) => `${isAzure || isAnthropic ? "" : "Bearer "}${s.trim()}`;
   const validString = (x: string) => x && x.length > 0;
 
   // when using google api in app, not set auth header
diff --git a/app/client/platforms/anthropic.ts b/app/client/platforms/anthropic.ts
index e90c8f057..b8eca6946 100644
--- a/app/client/platforms/anthropic.ts
+++ b/app/client/platforms/anthropic.ts
@@ -1,5 +1,5 @@
 import { ACCESS_CODE_PREFIX, Anthropic, ApiPath } from "@/app/constant";
-import { ChatOptions, LLMApi, MultimodalContent } from "../api";
+import { ChatOptions, getHeaders, LLMApi, MultimodalContent,  } from "../api";
 import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
 import { getClientConfig } from "@/app/config/client";
 import { DEFAULT_API_HOST } from "@/app/constant";
@@ -190,9 +190,7 @@ export class ClaudeApi implements LLMApi {
       body: JSON.stringify(requestBody),
       signal: controller.signal,
       headers: {
-        "Content-Type": "application/json",
-        Accept: "application/json",
-        "x-api-key": accessStore.anthropicApiKey,
+        ...getHeaders(),  // get common headers
         "anthropic-version": accessStore.anthropicApiVersion,
         Authorization: getAuthKey(accessStore.anthropicApiKey),
       },