Skip to content

fix(vertex): honor custom base_url as gateway prefix - #4200

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
yyhhyyyyyy:fix/vertex-gateway-base-url
Apr 30, 2026
Merged

fix(vertex): honor custom base_url as gateway prefix#4200
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
yyhhyyyyyy:fix/vertex-gateway-base-url

Conversation

@yyhhyyyyyy

@yyhhyyyyyy yyhhyyyyyy commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

当前 Vertex 渠道即使配置了自定义 base_url,请求仍会走硬编码的官方上游地址,导致如 Cloudflare AI Gateway 这类自定义网关无法生效。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

  1. 修复前:未真正经过 Cloudflare AI Gateway
ChannelMeta.BaseURL:
https://gateway.ai.cloudflare.com/v1/<CF_ACCOUNT_ID>/<CF_GATEWAY_ID>/<CF_PROVIDER_SLUG>

fullRequestURL:
https://aiplatform.googleapis.com/v1/projects/<VERTEX_PROJECT_ID>/locations/global/publishers/google/models/gemini-3.1-pro-preview:generateContent

final response:
200 OK
object: "chat.completion"
model: "gemini-3.1-pro-preview"

2.修复后:已成功经过 Cloudflare AI Gateway

ChannelMeta.BaseURL:
https://gateway.ai.cloudflare.com/v1/<CF_ACCOUNT_ID>/<CF_GATEWAY_ID>/<CF_PROVIDER_SLUG>

fullRequestURL:
https://gateway.ai.cloudflare.com/v1/<CF_ACCOUNT_ID>/<CF_GATEWAY_ID>/<CF_PROVIDER_SLUG>/v1/projects/<VERTEX_PROJECT_ID>/locations/global/publishers/google/models/gemini-3.1-pro-preview:generateContent

final response:
200 OK
object: "chat.completion"
model: "gemini-3.1-pro-preview"

Summary by CodeRabbit

  • Refactor
    • Consolidated Vertex AI endpoint URL construction logic across internal request handlers, improving code maintainability and reducing duplication.

@coderabbitai

coderabbitai Bot commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This PR introduces a centralized URL builder system for Vertex endpoints in relay/channel/vertex/url_builder.go, consolidating scattered URL construction logic. The adaptor.go files are updated to use these builders instead of inline URL formatting, reducing duplication and ensuring consistent URL generation across different request types.

Changes

Cohort / File(s) Summary
URL Builder Implementation
relay/channel/vertex/url_builder.go
New file introducing BuildAPIBaseURL, BuildPublisherModelURL, BuildGoogleModelURL, BuildAnthropicModelURL, and BuildOpenSourceChatCompletionsURL for centralized Vertex endpoint construction. Handles URL normalization, region defaults, and API versioning.
Task Adaptor URL Construction
relay/channel/task/vertex/adaptor.go
Delegates predictLongRunning URL construction to BuildGoogleModelURL. Introduces buildFetchOperationURL to extract region/project/modelName from operation names and eliminate duplicated inline parsing logic.
Channel Adaptor URL Construction
relay/channel/vertex/adaptor.go
Replaces hardcoded Gemini/Claude/OpenSource URL strings with calls to BuildGoogleModelURL, BuildAnthropicModelURL, and BuildOpenSourceChatCompletionsURL. Simplifies getRequestUrl by removing region == "global" conditional URL formatting.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 URLs were scattered, now they're neat,
With builders tall and functions sweet!
No more duplication's clutter,
Just refactored code that's butter. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly describes the main change: honoring a custom base_url configuration as a gateway prefix in the Vertex channel. The title is directly related to the primary objective of the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@relay/channel/task/vertex/adaptor.go`:
- Around line 233-241: The code currently allows a missing project when baseURL
is provided, causing malformed endpoints; update the logic in the adaptor where
extractProjectFromOperationName(upstreamName) is used to require a non-empty
project regardless of baseURL: validate project (from
extractProjectFromOperationName) with strings.TrimSpace and return an error like
"cannot extract project from operation name" if empty, before calling
vertexcore.BuildGoogleModelURL (which uses vertexcore.DefaultAPIVersion,
project, region, modelName, "fetchPredictOperation"); keep the existing
modelName check and ensure both modelName and project are validated and fail
fast rather than relying on BuildAPIBaseURL behavior.

In `@relay/channel/vertex/url_builder.go`:
- Around line 28-32: The code currently appends "/"+version to any normalized
base URL; change this in the normalizeVertexBaseURL usage so you only append
when the normalized base does not already include the version: trim spaces and
slashes from both normalized and version, normalize trailing slashes on
normalized, then check if normalized already has the version as the final path
segment (e.g., strings.HasSuffix(normalized, "/"+version) or normalized ==
version) and only do normalized = normalized + "/" + version when that check is
false; update the logic around normalizeVertexBaseURL, baseURL, and version to
perform these defensive checks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 64bc7259-1c4b-4253-926b-6a759a848ff7

📥 Commits

Reviewing files that changed from the base of the PR and between b4df995 and 59f0b6f.

📒 Files selected for processing (5)
  • relay/channel/task/vertex/adaptor.go
  • relay/channel/task/vertex/adaptor_test.go
  • relay/channel/vertex/adaptor.go
  • relay/channel/vertex/url_builder.go
  • relay/channel/vertex/url_builder_test.go

Comment thread relay/channel/task/vertex/adaptor.go
Comment thread relay/channel/vertex/url_builder.go Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
relay/channel/task/vertex/adaptor.go (1)

85-99: ⚠️ Potential issue | 🟡 Minor

Fail fast when credentials are missing project_id before building submit URL.

At Line 98, URL construction proceeds even if adc.ProjectID is empty. With a custom gateway base URL, that can generate an invalid path and surface as a confusing upstream error instead of a clear local validation error.

Suggested patch
 func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) {
 	adc := &vertexcore.Credentials{}
 	if err := common.Unmarshal([]byte(a.apiKey), adc); err != nil {
 		return "", fmt.Errorf("failed to decode credentials: %w", err)
 	}
+	if strings.TrimSpace(adc.ProjectID) == "" {
+		return "", fmt.Errorf("missing project_id in credentials")
+	}
 	modelName := info.UpstreamModelName
 	if modelName == "" {
 		modelName = "veo-3.0-generate-001"
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@relay/channel/task/vertex/adaptor.go` around lines 85 - 99, The code calls
vertexcore.BuildGoogleModelURL using adc.ProjectID even when adc.ProjectID may
be empty, which can produce invalid URLs; update the adaptor code (the block
that decodes credentials into adc and then calls BuildGoogleModelURL) to
validate adc.ProjectID after unmarshalling and return a clear error (e.g.,
"missing project_id in credentials") if it's empty before calling
vertexcore.BuildGoogleModelURL or proceeding with URL construction, so callers
get a fast, descriptive failure instead of an upstream error.
🧹 Nitpick comments (1)
relay/channel/vertex/url_builder_test.go (1)

83-194: Consider consolidating adaptor URL tests into a table-driven structure.

The setup/assert pattern is repeated across multiple tests, which makes expansion noisier than needed.

♻️ Refactor sketch
+func assertRequestURL(t *testing.T, info *relaycommon.RelayInfo, want string) {
+	t.Helper()
+	adaptor := &Adaptor{}
+	adaptor.Init(info)
+	got, err := adaptor.GetRequestURL(info)
+	if err != nil {
+		t.Fatalf("GetRequestURL returned error: %v", err)
+	}
+	if got != want {
+		t.Fatalf("GetRequestURL() = %q, want %q", got, want)
+	}
+}
+
-func TestAdaptorGetRequestURLUsesCustomBaseForGemini(t *testing.T) {
-	t.Parallel()
-	...
-}
+func TestAdaptorGetRequestURLCases(t *testing.T) {
+	t.Parallel()
+	tests := []struct {
+		name string
+		info *relaycommon.RelayInfo
+		want string
+	}{
+		// gemini/claude/open-source/api-key cases...
+	}
+	for _, tc := range tests {
+		tc := tc
+		t.Run(tc.name, func(t *testing.T) {
+			t.Parallel()
+			assertRequestURL(t, tc.info, tc.want)
+		})
+	}
+}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@relay/channel/vertex/url_builder_test.go` around lines 83 - 194, Consolidate
the four near-duplicate tests (TestAdaptorGetRequestURLUsesCustomBaseForGemini,
TestAdaptorGetRequestURLUsesCustomBaseForClaude,
TestAdaptorGetRequestURLUsesCustomBaseForOpenSource,
TestAdaptorGetRequestURLPreservesAPIKeyQueryWithCustomBase) into a single
table-driven test that iterates test cases describing RelayInfo/ChannelMeta
inputs and expected URLs; for each case create an Adaptor, call
adaptor.Init(info) and adaptor.GetRequestURL(info), assert error nil and compare
got vs want. Reuse mustMarshalCredentials where needed and include fields
IsStream, OriginModelName, ChannelMeta.ApiVersion, ChannelBaseUrl, ApiKey and
ChannelOtherSettings.VertexKeyType in the table rows so each scenario (gemini
streaming, claude, open-source, API-key query) is covered. Ensure each case
t.Run with a descriptive name and keep t.Parallel() per subtest.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@relay/channel/task/vertex/adaptor.go`:
- Around line 85-99: The code calls vertexcore.BuildGoogleModelURL using
adc.ProjectID even when adc.ProjectID may be empty, which can produce invalid
URLs; update the adaptor code (the block that decodes credentials into adc and
then calls BuildGoogleModelURL) to validate adc.ProjectID after unmarshalling
and return a clear error (e.g., "missing project_id in credentials") if it's
empty before calling vertexcore.BuildGoogleModelURL or proceeding with URL
construction, so callers get a fast, descriptive failure instead of an upstream
error.

---

Nitpick comments:
In `@relay/channel/vertex/url_builder_test.go`:
- Around line 83-194: Consolidate the four near-duplicate tests
(TestAdaptorGetRequestURLUsesCustomBaseForGemini,
TestAdaptorGetRequestURLUsesCustomBaseForClaude,
TestAdaptorGetRequestURLUsesCustomBaseForOpenSource,
TestAdaptorGetRequestURLPreservesAPIKeyQueryWithCustomBase) into a single
table-driven test that iterates test cases describing RelayInfo/ChannelMeta
inputs and expected URLs; for each case create an Adaptor, call
adaptor.Init(info) and adaptor.GetRequestURL(info), assert error nil and compare
got vs want. Reuse mustMarshalCredentials where needed and include fields
IsStream, OriginModelName, ChannelMeta.ApiVersion, ChannelBaseUrl, ApiKey and
ChannelOtherSettings.VertexKeyType in the table rows so each scenario (gemini
streaming, claude, open-source, API-key query) is covered. Ensure each case
t.Run with a descriptive name and keep t.Parallel() per subtest.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4df6d6d2-ea15-4cc6-8ed4-40b9fb47fbad

📥 Commits

Reviewing files that changed from the base of the PR and between 59f0b6f and 3e52e8a.

📒 Files selected for processing (4)
  • relay/channel/task/vertex/adaptor.go
  • relay/channel/task/vertex/adaptor_test.go
  • relay/channel/vertex/url_builder.go
  • relay/channel/vertex/url_builder_test.go
✅ Files skipped from review due to trivial changes (1)
  • relay/channel/task/vertex/adaptor_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • relay/channel/vertex/url_builder.go

@yyhhyyyyyy
yyhhyyyyyy force-pushed the fix/vertex-gateway-base-url branch from ca44275 to 987b7ec Compare April 30, 2026 07:09

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
relay/channel/task/vertex/adaptor.go (1)

84-99: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Validate adc.ProjectID before building predictLongRunning URL.

If credentials are missing project_id, this path can generate an invalid predict endpoint (especially with custom baseURL) and fail downstream with a less actionable error. Fail fast here.

Suggested fix
 func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) {
 	adc := &vertexcore.Credentials{}
 	if err := common.Unmarshal([]byte(a.apiKey), adc); err != nil {
 		return "", fmt.Errorf("failed to decode credentials: %w", err)
 	}
+	if strings.TrimSpace(adc.ProjectID) == "" {
+		return "", fmt.Errorf("missing project_id in vertex credentials")
+	}
 	modelName := info.UpstreamModelName
 	if modelName == "" {
 		modelName = "veo-3.0-generate-001"
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@relay/channel/task/vertex/adaptor.go` around lines 84 - 99,
TaskAdaptor.BuildRequestURL should validate adc.ProjectID after decoding
credentials and before calling vertexcore.BuildGoogleModelURL: check that
strings.TrimSpace(adc.ProjectID) is not empty and, if it is, return a
descriptive error (e.g., "missing project_id in credentials") instead of
constructing the URL; ensure this early return prevents calling
vertexcore.BuildGoogleModelURL with an empty project ID and include reference to
a.baseURL in the error context if helpful for debugging.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@relay/channel/task/vertex/adaptor.go`:
- Around line 84-99: TaskAdaptor.BuildRequestURL should validate adc.ProjectID
after decoding credentials and before calling vertexcore.BuildGoogleModelURL:
check that strings.TrimSpace(adc.ProjectID) is not empty and, if it is, return a
descriptive error (e.g., "missing project_id in credentials") instead of
constructing the URL; ensure this early return prevents calling
vertexcore.BuildGoogleModelURL with an empty project ID and include reference to
a.baseURL in the error context if helpful for debugging.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c0061f49-d7d8-4a01-9192-c08bfe66fb25

📥 Commits

Reviewing files that changed from the base of the PR and between 3e52e8a and 987b7ec.

📒 Files selected for processing (3)
  • relay/channel/task/vertex/adaptor.go
  • relay/channel/vertex/adaptor.go
  • relay/channel/vertex/url_builder.go

@Calcium-Ion
Calcium-Ion merged commit 5114ad0 into QuantumNous:main Apr 30, 2026
1 check passed
Jinxuans referenced this pull request in TokFlux-Org/TokFlux May 9, 2026
fix(vertex): honor custom base_url as gateway prefix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants