feat: 自定义请求头 - #1447
Conversation
WalkthroughA new feature was introduced allowing custom HTTP headers to be specified and overridden on a per-channel basis. This involved adding a Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant UI (EditChannelModal)
participant Backend (API)
participant DB
participant Middleware
participant Relay
participant Upstream API
User->>UI (EditChannelModal): Enters header override JSON
UI (EditChannelModal)->>Backend (API): Submits channel config with header_override
Backend (API)->>DB: Stores header_override in Channel record
User->>Backend (API): Initiates request via selected channel
Backend (API)->>Middleware: Sets up context, includes header_override
Middleware->>Relay: Passes RelayInfo with header_override
Relay->>Upstream API: Applies header overrides to request headers
Upstream API-->>Relay: Processes request
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~15 minutes Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
relay/channel/api_request.go (1)
36-47: Consider improving error handling for header value conversion.The header override implementation is functionally correct, but the type conversion silently ignores errors. Consider logging conversion failures to help with debugging invalid header configurations.
if len(info.HeaderOverride) > 0 { for k, v := range info.HeaderOverride { - value, _ := common2.Any2Type[string](v) + value, err := common2.Any2Type[string](v) + if err != nil && common2.DebugEnabled { + println("Failed to convert header override value for key", k, ":", err.Error()) + continue + } req.Set(k, value) } }relay/channel/gemini/adaptor.go (1)
109-122: Solid implementation with proper error handling.The EdgeOne gateway handling is well-implemented with appropriate error handling and URL manipulation. The logic correctly:
- Checks for the presence of the "OE-Key" header override
- Handles URL parsing errors gracefully
- Properly manipulates query parameters
Consider simplifying the conditional check:
- if _, ok := info.HeaderOverride["OE-Key"]; len(info.HeaderOverride) > 0 && ok { + if _, ok := info.HeaderOverride["OE-Key"]; ok {The length check is redundant since we're already verifying key existence with
ok.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
constant/context_key.go(1 hunks)middleware/distributor.go(1 hunks)model/channel.go(2 hunks)relay/channel/api_request.go(1 hunks)relay/channel/gemini/adaptor.go(2 hunks)relay/common/relay_info.go(3 hunks)web/src/components/table/channels/modals/EditChannelModal.jsx(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
relay/common/relay_info.go (1)
Learnt from: 9Ninety
PR: #1273
File: relay/channel/gemini/relay-gemini.go:97-116
Timestamp: 2025-06-21T03:37:41.726Z
Learning: In relay/channel/gemini/relay-gemini.go, the thinking budget calculation logic (including the MaxOutputTokens multiplication) was introduced in PR #1247. PR #1273 focused specifically on decoupling the thoughts summary feature from thinking budget settings and did not modify the existing thinking budget behavior.
middleware/distributor.go (2)
Learnt from: feitianbubu
PR: #1228
File: router/main.go:28-36
Timestamp: 2025-06-15T12:38:11.806Z
Learning: gin.Context implements context.Context interface since Gin v1.8.0, providing the methods Deadline(), Done(), Err(), and Value(). When using Gin v1.8.0 or later, gin.Context can be passed directly to functions expecting context.Context without needing to extract c.Request.Context().
Learnt from: feitianbubu
PR: #1228
File: router/main.go:28-36
Timestamp: 2025-06-15T12:38:11.806Z
Learning: gin.Context implements context.Context interface since Gin v1.8.0, providing the methods Deadline(), Done(), Err(), and Value(). When using Gin v1.8.0 or later, gin.Context can be passed directly to functions expecting context.Context without needing to extract c.Request.Context().
🔇 Additional comments (9)
constant/context_key.go (1)
27-27: LGTM! Well-named constant addition.The new context key follows the established naming convention and is appropriately placed among other channel-related constants.
middleware/distributor.go (1)
258-258: LGTM! Consistent with existing context setup pattern.The header override context setup follows the same pattern as other channel-related context keys like
ParamOverrideandChannelSetting.model/channel.go (2)
47-47: LGTM! Consistent field addition.The
HeaderOverridefield follows the same pattern as other JSON configuration fields likeParamOverrideandStatusCodeMapping.
823-832: LGTM! Well-implemented getter method.The
GetHeaderOverride()method follows the established pattern used byGetParamOverride()and includes proper error handling with logging.relay/common/relay_info.go (2)
102-102: LGTM! Consistent field addition to RelayInfo struct.The
HeaderOverridefield follows the same pattern and type as the existingParamOverridefield.
220-220: LGTM! Proper initialization following established pattern.The header override initialization follows the same pattern as
ParamOverrideand correctly extracts the value from the Gin context.Also applies to: 259-259
relay/channel/gemini/adaptor.go (2)
9-9: LGTM! Import addition is appropriate.The
net/urlimport is correctly added to support the new URL parsing functionality for EdgeOne gateway handling.
108-108: Good refactoring to support conditional URL modification.The URL construction is extracted into a variable to enable conditional processing while maintaining the existing functionality.
web/src/components/table/channels/modals/EditChannelModal.jsx (1)
1553-1571: Well-implemented form field following established patterns.The header override field implementation is excellent:
- Consistent with existing form field patterns in the component
- Proper internationalization support
- Good UX with placeholder example, template filler, and clear functionality
- Appropriate placement in the Advanced Settings section
- Template value aligns with backend expectations (OE-Key for EdgeOne gateway)
The implementation maintains consistency with similar fields like
param_overrideandstatus_code_mapping.
增加覆写请求头,增加对edgeone ai网关的支持
Fixes #1313
Summary by CodeRabbit
New Features
User Interface