Skip to content

Commit 094e0de

Browse files
fix(google): preserve Vertex project env precedence
1 parent f7668c2 commit 094e0de

4 files changed

Lines changed: 65 additions & 4 deletions

File tree

‎extensions/google/transport-stream.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,48 @@ describe("google transport stream", () => {
12441244
});
12451245
});
12461246

1247+
it("prefers GCLOUD_PROJECT over GOOGLE_CLOUD_PROJECT_ID for Google Vertex credential marker requests", async () => {
1248+
await withTempDir("openclaw-google-vertex-project-precedence-", async (tempDir) => {
1249+
vi.stubEnv("GOOGLE_APPLICATION_CREDENTIALS", "");
1250+
vi.stubEnv("HOME", path.join(tempDir, "home"));
1251+
vi.stubEnv("APPDATA", "");
1252+
vi.stubEnv("GOOGLE_CLOUD_PROJECT", "");
1253+
vi.stubEnv("GCLOUD_PROJECT", "gcloud-project");
1254+
vi.stubEnv("GOOGLE_CLOUD_PROJECT_ID", "vertex-project-id");
1255+
vi.stubEnv("GOOGLE_CLOUD_LOCATION", "us-central1");
1256+
googleAuthGetAccessTokenMock.mockResolvedValueOnce("ya29.transport-token");
1257+
const tokenFetchMock = vi.fn();
1258+
guardedFetchMock.mockResolvedValueOnce(
1259+
buildSseResponse([
1260+
{
1261+
candidates: [{ content: { parts: [{ text: "ok" }] }, finishReason: "STOP" }],
1262+
},
1263+
]),
1264+
);
1265+
1266+
const streamFn = createGoogleVertexTransportStreamFn();
1267+
const stream = await Promise.resolve(
1268+
streamFn(
1269+
buildGoogleVertexModel(),
1270+
{
1271+
messages: [{ role: "user", content: "hello", timestamp: 0 }],
1272+
} as Parameters<typeof streamFn>[1],
1273+
{
1274+
apiKey: "gcp-vertex-credentials",
1275+
fetch: tokenFetchMock,
1276+
} as Parameters<typeof streamFn>[2],
1277+
),
1278+
);
1279+
await stream.result();
1280+
1281+
expect(tokenFetchMock).not.toHaveBeenCalled();
1282+
const guardedCall = requireMockCall(guardedFetchMock, 0, "guarded fetch");
1283+
expect(guardedCall[0]).toBe(
1284+
"https://us-central1-aiplatform.googleapis.com/v1/projects/gcloud-project/locations/us-central1/publishers/google/models/gemini-3.1-pro-preview:streamGenerateContent?alt=sse",
1285+
);
1286+
});
1287+
});
1288+
12471289
it("strips redundant google provider prefixes from Google Vertex model paths", async () => {
12481290
const tempDir = await mkdtemp(path.join(os.tmpdir(), "openclaw-google-vertex-prefix-"));
12491291
vi.stubEnv("HOME", path.join(tempDir, "home"));

‎extensions/google/transport-stream.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ function resolveGoogleVertexProject(options: GoogleTransportOptions | undefined)
345345
const project =
346346
normalizeOptionalString((options as { project?: unknown } | undefined)?.project) ||
347347
normalizeOptionalString(process.env.GOOGLE_CLOUD_PROJECT) ||
348-
normalizeOptionalString(process.env.GOOGLE_CLOUD_PROJECT_ID) ||
349-
normalizeOptionalString(process.env.GCLOUD_PROJECT);
348+
normalizeOptionalString(process.env.GCLOUD_PROJECT) ||
349+
normalizeOptionalString(process.env.GOOGLE_CLOUD_PROJECT_ID);
350350
if (!project) {
351351
throw new Error(
352352
"Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GOOGLE_CLOUD_PROJECT_ID/GCLOUD_PROJECT or pass project in options.",

‎packages/ai/src/providers/google-vertex.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,23 @@ describe("streamGoogleVertex", () => {
8484
vertexai: true,
8585
});
8686
});
87+
88+
it("prefers GCLOUD_PROJECT over GOOGLE_CLOUD_PROJECT_ID for ADC-backed Vertex clients", async () => {
89+
vi.stubEnv("GOOGLE_CLOUD_API_KEY", "");
90+
vi.stubEnv("GOOGLE_CLOUD_PROJECT", "");
91+
vi.stubEnv("GCLOUD_PROJECT", "gcloud-project");
92+
vi.stubEnv("GOOGLE_CLOUD_PROJECT_ID", "vertex-project-id");
93+
vi.stubEnv("GOOGLE_CLOUD_LOCATION", "us-central1");
94+
95+
const stream = streamGoogleVertex(model, context, { apiKey: "gcp-vertex-credentials" });
96+
const result = await stream.result();
97+
98+
expect(result.stopReason).toBe("stop");
99+
expect(googleGenAIOptions[0]).toMatchObject({
100+
apiVersion: "v1",
101+
location: "us-central1",
102+
project: "gcloud-project",
103+
vertexai: true,
104+
});
105+
});
87106
});

‎packages/ai/src/providers/google-vertex.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ function resolveProject(options?: GoogleVertexOptions): string {
159159
const project =
160160
options?.project ||
161161
process.env.GOOGLE_CLOUD_PROJECT ||
162-
process.env.GOOGLE_CLOUD_PROJECT_ID ||
163-
process.env.GCLOUD_PROJECT;
162+
process.env.GCLOUD_PROJECT ||
163+
process.env.GOOGLE_CLOUD_PROJECT_ID;
164164
if (!project) {
165165
throw new Error(
166166
"Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GOOGLE_CLOUD_PROJECT_ID/GCLOUD_PROJECT or pass project in options.",

0 commit comments

Comments
 (0)