fix(agents): prevent /v1beta duplication in Gemini PDF URL (#34369)

Strip trailing /v1beta from baseUrl before appending the version
segment, so callers that already include /v1beta in their base URL
(e.g. subagent-registry) no longer produce /v1beta/v1beta/models/…
which results in a 404 from the Gemini API.

Closes #34312

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
sline
2026-03-11 00:52:49 +08:00
committed by GitHub
parent 936607ca22
commit bfeea5d23f
2 changed files with 23 additions and 4 deletions

View File

@@ -137,10 +137,9 @@ export async function geminiAnalyzePdf(params: {
}
parts.push({ text: params.prompt });
const baseUrl = (params.baseUrl ?? "https://generativelanguage.googleapis.com").replace(
/\/+$/,
"",
);
const baseUrl = (params.baseUrl ?? "https://generativelanguage.googleapis.com")
.replace(/\/+$/, "")
.replace(/\/v1beta$/, "");
const url = `${baseUrl}/v1beta/models/${encodeURIComponent(params.modelId)}:generateContent?key=${encodeURIComponent(apiKey)}`;
const res = await fetch(url, {

View File

@@ -711,6 +711,26 @@ describe("native PDF provider API calls", () => {
"apiKey required",
);
});
it("geminiAnalyzePdf does not duplicate /v1beta when baseUrl already includes it", async () => {
const { geminiAnalyzePdf } = await import("./pdf-native-providers.js");
const fetchMock = mockFetchResponse({
ok: true,
json: async () => ({
candidates: [{ content: { parts: [{ text: "ok" }] } }],
}),
});
await geminiAnalyzePdf(
makeGeminiAnalyzeParams({
baseUrl: "https://generativelanguage.googleapis.com/v1beta",
}),
);
const [url] = fetchMock.mock.calls[0];
expect(url).toContain("/v1beta/models/");
expect(url).not.toContain("/v1beta/v1beta");
});
});
// ---------------------------------------------------------------------------