mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-27 20:45:49 +00:00
fix: keep Cloudflare Anthropic provider auth header
This commit is contained in:
61
src/llm/providers/anthropic.test.ts
Normal file
61
src/llm/providers/anthropic.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { Context, Model } from "../types.js";
|
||||
|
||||
const anthropicMockState = vi.hoisted(() => ({
|
||||
configs: [] as unknown[],
|
||||
}));
|
||||
|
||||
vi.mock("@anthropic-ai/sdk", () => ({
|
||||
default: class MockAnthropic {
|
||||
messages = {
|
||||
create: vi.fn(() => {
|
||||
throw new Error("stop after constructor");
|
||||
}),
|
||||
};
|
||||
|
||||
constructor(config: unknown) {
|
||||
anthropicMockState.configs.push(config);
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
import { streamAnthropic } from "./anthropic.js";
|
||||
|
||||
describe("Anthropic provider", () => {
|
||||
beforeEach(() => {
|
||||
anthropicMockState.configs = [];
|
||||
});
|
||||
|
||||
it("keeps Cloudflare AI Gateway upstream provider auth on the Anthropic API key", async () => {
|
||||
const model = {
|
||||
id: "claude-sonnet-4-6",
|
||||
provider: "cloudflare-ai-gateway",
|
||||
api: "anthropic-messages",
|
||||
baseUrl:
|
||||
"https://gateway.ai.cloudflare.com/v1/account/gateway/anthropic/v1/messages",
|
||||
maxTokens: 4096,
|
||||
headers: {
|
||||
"cf-aig-authorization": "Bearer gateway-token",
|
||||
},
|
||||
} satisfies Model<"anthropic-messages">;
|
||||
const context = {
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
} satisfies Context;
|
||||
|
||||
streamAnthropic(model, context, {
|
||||
apiKey: "sk-ant-provider",
|
||||
});
|
||||
|
||||
await vi.waitFor(() => expect(anthropicMockState.configs).toHaveLength(1));
|
||||
const config = anthropicMockState.configs[0] as {
|
||||
apiKey?: string | null;
|
||||
authToken?: string | null;
|
||||
defaultHeaders?: Record<string, string | null>;
|
||||
};
|
||||
|
||||
expect(config.apiKey).toBe("sk-ant-provider");
|
||||
expect(config.authToken).toBeNull();
|
||||
expect(config.defaultHeaders?.["x-api-key"]).toBeUndefined();
|
||||
expect(config.defaultHeaders?.["cf-aig-authorization"]).toBe("Bearer gateway-token");
|
||||
});
|
||||
});
|
||||
@@ -833,7 +833,7 @@ function createClient(
|
||||
|
||||
if (model.provider === "cloudflare-ai-gateway") {
|
||||
const client = new Anthropic({
|
||||
apiKey: null,
|
||||
apiKey,
|
||||
authToken: null,
|
||||
baseURL: resolveCloudflareBaseUrl(model),
|
||||
dangerouslyAllowBrowser: true,
|
||||
@@ -841,8 +841,6 @@ function createClient(
|
||||
{
|
||||
accept: "application/json",
|
||||
"anthropic-dangerous-direct-browser-access": "true",
|
||||
"cf-aig-authorization": `Bearer ${apiKey}`,
|
||||
"x-api-key": null,
|
||||
Authorization: null,
|
||||
...(betaFeatures.length > 0 ? { "anthropic-beta": betaFeatures.join(",") } : {}),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user