Skip to content

feat: 为 Vertex 渠道添加 Claude 请求转换开关 - #2467

Closed
XTFG wants to merge 1 commit into
QuantumNous:mainfrom
XTFG:feature/vertex-claude-to-gemini-switch
Closed

feat: 为 Vertex 渠道添加 Claude 请求转换开关#2467
XTFG wants to merge 1 commit into
QuantumNous:mainfrom
XTFG:feature/vertex-claude-to-gemini-switch

Conversation

@XTFG

@XTFG XTFG commented Dec 18, 2025

Copy link
Copy Markdown

添加可配置开关以控制 Vertex 渠道中 Claude 模型请求的处理方式:

  • 开关关闭(默认):使用原生 Anthropic 端点和格式
  • 开关开启:将 Claude 请求转换为 Gemini 格式并发送到 Google 端点

后端修改:

  • dto/channel_settings.go: 添加 VertexClaudeToGemini 字段
  • relay/channel/vertex/adaptor.go:
    • Init 方法根据开关设置 RequestMode
    • ConvertClaudeRequest 方法在开关开启时调用 Gemini 转换

前端修改:

  • EditChannelModal.jsx: 添加开关 UI 控件和相关数据处理逻辑

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added a new configuration toggle for Vertex channels to enable Claude-to-Gemini conversion, providing users with control over Claude request processing.

✏️ Tip: You can customize this high-level summary in your review settings.

添加可配置开关以控制 Vertex 渠道中 Claude 模型请求的处理方式:
- 开关关闭(默认):使用原生 Anthropic 端点和格式
- 开关开启:将 Claude 请求转换为 Gemini 格式并发送到 Google 端点

后端修改:
- dto/channel_settings.go: 添加 VertexClaudeToGemini 字段
- relay/channel/vertex/adaptor.go:
  - Init 方法根据开关设置 RequestMode
  - ConvertClaudeRequest 方法在开关开启时调用 Gemini 转换

前端修改:
- EditChannelModal.jsx: 添加开关 UI 控件和相关数据处理逻辑

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 18, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This PR introduces a feature flag VertexClaudeToGemini that enables Claude requests in Vertex channels to be converted to Gemini format. The flag is added to the data model, implemented as conditional routing in the Vertex adaptor, and exposed as a toggle in the channel editing UI.

Changes

Cohort / File(s) Summary
Data Model
dto/channel_settings.go
Added VertexClaudeToGemini boolean field to ChannelOtherSettings struct, mapped to vertex_claude_to_gemini JSON key
Adaptor Logic
relay/channel/vertex/adaptor.go
Modified ConvertClaudeRequest to route Claude requests to Gemini adaptor when flag is enabled; updated Init to set RequestMode to Gemini for Claude models when flag is active
Frontend UI
web/src/components/table/channels/modals/EditChannelModal.jsx
Added form switch for vertex_claude_to_gemini setting visible for Vertex channels (type 41); integrated state loading from existing channel data, form submission, and settings propagation

Sequence Diagram

sequenceDiagram
    participant Client
    participant VertexAdaptor as Vertex Adaptor
    participant GeminiAdaptor as Gemini Adaptor
    participant ClaudeAdaptor as Claude Adaptor

    Client->>VertexAdaptor: Claude request (model: claude-*)
    alt VertexClaudeToGemini enabled
        VertexAdaptor->>VertexAdaptor: Init: Set RequestMode to Gemini
        VertexAdaptor->>GeminiAdaptor: ConvertClaudeRequest → delegate
        GeminiAdaptor->>GeminiAdaptor: Convert to Gemini format
        GeminiAdaptor-->>Client: Gemini response
    else VertexClaudeToGemini disabled
        VertexAdaptor->>VertexAdaptor: Init: Keep RequestMode as Claude
        VertexAdaptor->>ClaudeAdaptor: ConvertClaudeRequest → native path
        ClaudeAdaptor->>ClaudeAdaptor: Map to Vertex Claude model
        ClaudeAdaptor-->>Client: Vertex Claude response
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Verify that the Gemini adaptor correctly handles delegated Claude requests from the Vertex adaptor
  • Confirm the feature flag properly gates both the Init initialization logic and the ConvertClaudeRequest routing decision
  • Ensure the frontend form switch correctly scopes the setting to Vertex channels only and prevents leakage to other channel types
  • Check that existing Claude-to-Vertex request mapping remains unchanged when the flag is disabled

Possibly related PRs

Suggested reviewers

  • Calcium-Ion

Poem

🐰 A clever flag hops into the code,
Claude finds a Gemini path on the road,
Routing switches, feature gates align,
With a Vertex-to-Gemini design! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding a Claude-to-Gemini conversion toggle for Vertex channels, which aligns with the comprehensive modifications across backend (DTO, adaptor) and frontend (modal UI) components.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

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

@XTFG XTFG closed this Dec 18, 2025
@XTFG
XTFG deleted the feature/vertex-claude-to-gemini-switch branch December 18, 2025 16:50

@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: 0

Caution

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

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

242-244: Add RequestMode check before setting Claude headers for Gemini requests.

When VertexClaudeToGemini is enabled, the upstream model name contains "claude" but RequestMode is set to RequestModeGemini. The code at lines 242-244 checks only the model name and calls CommonClaudeHeadersOperation, which sets Claude-specific headers (anthropic-beta and other Claude settings) for requests being routed to Gemini endpoints. This causes incompatible headers to be sent.

Update the condition to also verify the request mode:

if strings.Contains(info.UpstreamModelName, "claude") && a.RequestMode == RequestModeClaude {
    claude.CommonClaudeHeadersOperation(c, req, info)
}
🧹 Nitpick comments (1)
web/src/components/table/channels/modals/EditChannelModal.jsx (1)

1743-1759: Good UI integration, consider adding a usage warning.

The switch component is properly implemented with:

  • Correct type gating (type === 41)
  • Clear labels and help text
  • Proper state management via handleChannelOtherSettingsChange

Consider adding a warning Banner (similar to other channels) to inform users that enabling this changes the request format and endpoint, which could affect existing integrations:

💡 Suggested enhancement
 {inputs.type === 41 && (
+  <>
+    <Banner
+      type='info'
+      description={t(
+        '开启此选项将改变 Claude 模型的请求格式和端点,可能影响现有集成。请在测试后再启用。',
+      )}
+      className='!rounded-lg mb-3'
+    />
     <Form.Switch
       field='vertex_claude_to_gemini'
       label={t('Claude 模型使用 Gemini 格式')}
       checkedText={t('开')}
       uncheckedText={t('关')}
       onChange={(value) =>
         handleChannelOtherSettingsChange(
           'vertex_claude_to_gemini',
           value,
         )
       }
       extraText={t(
         '开启后,Claude 模型请求将转换为 Gemini 格式并发送到 Google 端点,而不是使用原生 Anthropic 端点',
       )}
     />
+  </>
 )}
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b35ae9f and 6328285.

📒 Files selected for processing (3)
  • dto/channel_settings.go (1 hunks)
  • relay/channel/vertex/adaptor.go (2 hunks)
  • web/src/components/table/channels/modals/EditChannelModal.jsx (7 hunks)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2025-08-08T17:12:43.157Z
Learnt from: RedwindA
Repo: QuantumNous/new-api PR: 1537
File: relay/gemini_handler.go:330-342
Timestamp: 2025-08-08T17:12:43.157Z
Learning: In the new-api repository, the `GeminiEmbeddingHandler` function in `relay/gemini_handler.go` is designed specifically for native Gemini embedding requests and therefore does not require the `ConvertGeminiRequest` step that is used in the chat handler. The embedding requests are already in the native Gemini format and don't need conversion.

Applied to files:

  • relay/channel/vertex/adaptor.go
📚 Learning: 2025-08-05T17:14:17.246Z
Learnt from: neotf
Repo: QuantumNous/new-api PR: 1511
File: setting/ratio_setting/model_ratio.go:118-123
Timestamp: 2025-08-05T17:14:17.246Z
Learning: Claude models handle "-thinking" variants differently from Gemini models. For Claude models, only the base model (without "-thinking") gets an entry in defaultModelRatio map. The "-thinking" variants rely on the Claude relay handler stripping the suffix using strings.TrimSuffix(textRequest.Model, "-thinking") before looking up the ratio, so they automatically use the base model's ratio.

Applied to files:

  • relay/channel/vertex/adaptor.go
📚 Learning: 2025-08-27T02:15:25.448Z
Learnt from: AAEE86
Repo: QuantumNous/new-api PR: 1658
File: web/src/components/table/channels/modals/EditChannelModal.jsx:555-569
Timestamp: 2025-08-27T02:15:25.448Z
Learning: In EditChannelModal.jsx, the database stores mapped keys (display names) in the models field after applying model mapping transformations. When loading a channel, data.models contains the mapped keys, not the original model names. The filtering logic if (data.models.includes(key)) in the initialization is correct.

Applied to files:

  • web/src/components/table/channels/modals/EditChannelModal.jsx
🧬 Code graph analysis (1)
relay/channel/vertex/adaptor.go (3)
dto/channel_settings.go (1)
  • ChannelOtherSettings (26-35)
relay/channel/adapter.go (1)
  • Adaptor (15-32)
relay/channel/claude/adaptor.go (1)
  • Adaptor (24-26)
🔇 Additional comments (6)
dto/channel_settings.go (1)

28-29: LGTM! Clean struct field addition.

The new VertexClaudeToGemini field is properly integrated into the ChannelOtherSettings struct with appropriate JSON tags and clear documentation.

relay/channel/vertex/adaptor.go (2)

59-64: LGTM! Clean feature flag implementation.

The conditional delegation to the Gemini adaptor when VertexClaudeToGemini is enabled is straightforward and maintains backward compatibility.


87-93: LGTM! Consistent mode selection logic.

The Init method correctly sets RequestMode based on the feature flag, ensuring that URL generation and response handling use the appropriate Gemini or Claude paths.

web/src/components/table/channels/modals/EditChannelModal.jsx (3)

158-159: LGTM! Appropriate default value.

The vertex_claude_to_gemini field is added to originInputs with a safe default of false, ensuring backward compatibility and explicit opt-in behavior.


567-568: LGTM! Robust settings parsing with proper fallbacks.

The loading logic correctly reads vertex_claude_to_gemini from parsed settings with appropriate fallbacks to false in error cases and when settings are absent.

Also applies to: 584-584, 594-594


1201-1202: LGTM! Consistent settings persistence pattern.

The settings handling correctly:

  • Persists vertex_claude_to_gemini in the settings JSON for Vertex channels (type 41)
  • Cleans up the field for non-Vertex channels
  • Removes the top-level field from the payload before submission

This follows the established pattern for other Vertex-specific settings.

Also applies to: 1205-1205, 1232-1232

@XTFG
XTFG restored the feature/vertex-claude-to-gemini-switch branch December 18, 2025 16:56
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