feat: 为 Vertex 渠道添加 Claude 请求转换开关 - #2467
Conversation
添加可配置开关以控制 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>
WalkthroughThis PR introduces a feature flag Changes
Sequence DiagramsequenceDiagram
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
VertexClaudeToGeminiis enabled, the upstream model name contains "claude" butRequestModeis set toRequestModeGemini. The code at lines 242-244 checks only the model name and callsCommonClaudeHeadersOperation, 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
handleChannelOtherSettingsChangeConsider 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
📒 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
VertexClaudeToGeminifield is properly integrated into theChannelOtherSettingsstruct 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
VertexClaudeToGeminiis enabled is straightforward and maintains backward compatibility.
87-93: LGTM! Consistent mode selection logic.The
Initmethod correctly setsRequestModebased 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_geminifield is added tooriginInputswith a safe default offalse, 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_geminifrom parsed settings with appropriate fallbacks tofalsein 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_geminiin 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
添加可配置开关以控制 Vertex 渠道中 Claude 模型请求的处理方式:
后端修改:
前端修改:
🤖 Generated with Claude Code
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.