Skip to content

Commit 51c231a

Browse files
Peter Steinbergersteipete
authored andcommitted
fix(anthropic-vertex): correct multi-region endpoints
1 parent d865c9b commit 51c231a

6 files changed

Lines changed: 80 additions & 3 deletions

File tree

‎extensions/anthropic-vertex/index.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ describe("anthropic-vertex provider plugin", () => {
102102
expect(result.provider.models[4]?.thinkingLevelMap).toEqual({ xhigh: null, max: "max" });
103103
});
104104

105+
it.each([
106+
{ region: "global", baseUrl: "https://aiplatform.googleapis.com" },
107+
{ region: "us", baseUrl: "https://aiplatform.us.rep.googleapis.com" },
108+
{ region: "eu", baseUrl: "https://aiplatform.eu.rep.googleapis.com" },
109+
{ region: "us-east5", baseUrl: "https://us-east5-aiplatform.googleapis.com" },
110+
])("publishes the SDK endpoint for the $region location", ({ region, baseUrl }) => {
111+
expect(
112+
buildAnthropicVertexProvider({
113+
env: { GOOGLE_CLOUD_LOCATION: region },
114+
}).baseUrl,
115+
).toBe(baseUrl);
116+
});
117+
105118
it.each(["global", "us", "eu"])("publishes Opus 5 for the %s endpoint", (region) => {
106119
const provider = buildAnthropicVertexProvider({
107120
env: { GOOGLE_CLOUD_LOCATION: region },
@@ -194,7 +207,7 @@ describe("anthropic-vertex provider plugin", () => {
194207
name: "Claude Sonnet 5",
195208
api: "anthropic-messages",
196209
provider: "anthropic-vertex",
197-
baseUrl: "https://us-aiplatform.googleapis.com",
210+
baseUrl: "https://aiplatform.us.rep.googleapis.com",
198211
reasoning: true,
199212
input: ["text", "image"],
200213
contextWindow: 1_000_000,
@@ -235,7 +248,7 @@ describe("anthropic-vertex provider plugin", () => {
235248
name: "Claude Opus 5",
236249
api: "anthropic-messages",
237250
provider: "anthropic-vertex",
238-
baseUrl: "https://us-aiplatform.googleapis.com",
251+
baseUrl: "https://aiplatform.us.rep.googleapis.com",
239252
reasoning: false,
240253
input: ["text"],
241254
cost: { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 },

‎extensions/anthropic-vertex/provider-catalog.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ export function buildAnthropicVertexProvider(params?: {
238238
const baseUrl =
239239
normalizeLowercaseStringOrEmpty(region) === "global"
240240
? "https://aiplatform.googleapis.com"
241-
: `https://${region}-aiplatform.googleapis.com`;
241+
: region === "us" || region === "eu"
242+
? `https://aiplatform.${region}.rep.googleapis.com`
243+
: `https://${region}-aiplatform.googleapis.com`;
242244

243245
return {
244246
baseUrl,

‎extensions/anthropic-vertex/region.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ describe("anthropic vertex region helpers", () => {
2525
).toBe("europe-west4");
2626
});
2727

28+
it.each(["us", "eu"])("parses the %s multi-region Vertex endpoint", (region) => {
29+
expect(
30+
resolveAnthropicVertexRegionFromBaseUrl(`https://aiplatform.${region}.rep.googleapis.com`),
31+
).toBe(region);
32+
});
33+
2834
it("treats the global Vertex endpoint as global", () => {
2935
expect(resolveAnthropicVertexRegionFromBaseUrl("https://aiplatform.googleapis.com")).toBe(
3036
"global",

‎extensions/anthropic-vertex/stream-runtime.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,24 @@ describe("createAnthropicVertexStreamFn", () => {
589589
});
590590

591591
describe("createAnthropicVertexStreamFnForModel", () => {
592+
it.each(["us", "eu"])("preserves the %s multi-region SDK endpoint", (region) => {
593+
const { deps, anthropicVertexCtorMock, googleAuthClient } = createStreamDeps();
594+
const streamFn = createAnthropicVertexStreamFnForModel(
595+
{ baseUrl: `https://aiplatform.${region}.rep.googleapis.com` },
596+
{ GOOGLE_CLOUD_PROJECT_ID: "vertex-project" } as NodeJS.ProcessEnv,
597+
deps,
598+
);
599+
600+
void streamFn(makeModel({ id: "claude-sonnet-5", maxTokens: 128_000 }), { messages: [] }, {});
601+
602+
expect(anthropicVertexCtorMock).toHaveBeenCalledWith({
603+
googleAuth: googleAuthClient,
604+
projectId: "vertex-project",
605+
region,
606+
baseURL: `https://aiplatform.${region}.rep.googleapis.com/v1`,
607+
});
608+
});
609+
592610
it("derives project and region from the model and env", () => {
593611
const { deps, anthropicVertexCtorMock, googleAuthClient } = createStreamDeps();
594612
const streamFn = createAnthropicVertexStreamFnForModel(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { resolveAnthropicEphemeralCacheControl } from "./anthropic-payload-policy.js";
3+
4+
describe("resolveAnthropicEphemeralCacheControl", () => {
5+
afterEach(() => {
6+
vi.unstubAllEnvs();
7+
});
8+
9+
it.each([
10+
"https://aiplatform.googleapis.com",
11+
"https://us-east5-aiplatform.googleapis.com",
12+
"https://aiplatform.us.rep.googleapis.com",
13+
"https://aiplatform.eu.rep.googleapis.com",
14+
])("preserves env-configured long retention for the official %s endpoint", (baseUrl) => {
15+
vi.stubEnv("OPENCLAW_CACHE_RETENTION", "long");
16+
17+
expect(resolveAnthropicEphemeralCacheControl(baseUrl, undefined)).toEqual({
18+
type: "ephemeral",
19+
ttl: "1h",
20+
});
21+
});
22+
23+
it("keeps env-configured long retention restricted for custom proxy endpoints", () => {
24+
vi.stubEnv("OPENCLAW_CACHE_RETENTION", "long");
25+
26+
expect(
27+
resolveAnthropicEphemeralCacheControl("https://proxy.example.test/vertex", undefined),
28+
).toEqual({ type: "ephemeral" });
29+
});
30+
31+
it("preserves explicitly configured long retention for custom proxy endpoints", () => {
32+
expect(
33+
resolveAnthropicEphemeralCacheControl("https://proxy.example.test/vertex", "long"),
34+
).toEqual({ type: "ephemeral", ttl: "1h" });
35+
});
36+
});

‎packages/ai/src/transports/anthropic-payload-policy.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ function isLongTtlEligibleEndpoint(baseUrl: string | undefined): boolean {
5656
return (
5757
hostname === "api.anthropic.com" ||
5858
hostname === "aiplatform.googleapis.com" ||
59+
hostname === "aiplatform.us.rep.googleapis.com" ||
60+
hostname === "aiplatform.eu.rep.googleapis.com" ||
5961
hostname.endsWith("-aiplatform.googleapis.com")
6062
);
6163
}

0 commit comments

Comments
 (0)