cloudflare支持responses - #1568
Conversation
feat: add responses support for cloudflare
WalkthroughAdded RelayModeResponses support in the Cloudflare channel adaptor: new import for OpenAI handlers, updated request URL routing to /ai/v1/responses, pass-through conversion for OpenAI responses requests, and response handling for both streaming and non-streaming via OpenAI response handlers. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant CloudflareAdaptor
participant OpenAIHandlers
participant CloudflareAPI
Client->>CloudflareAdaptor: Request (RelayModeResponses)
CloudflareAdaptor->>CloudflareAPI: POST /ai/v1/responses
alt streaming
CloudflareAPI-->>CloudflareAdaptor: SSE stream
CloudflareAdaptor->>OpenAIHandlers: OaiResponsesStreamHandler(stream)
OpenAIHandlers-->>Client: Streamed chunks
else non-streaming
CloudflareAPI-->>CloudflareAdaptor: JSON response
CloudflareAdaptor->>OpenAIHandlers: OaiResponsesHandler(response)
OpenAIHandlers-->>Client: Final response
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
✨ 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. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
@mrhaoji 请帮忙 review & merge,感谢! |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
relay/channel/cloudflare/adaptor.go (1)
110-115: Responses mode handling: good reuse of OpenAI handlers; note signature asymmetry vs other handlers.Implementation is correct for both streaming and non-streaming. Minor readability nit: handler return order differs from Cloudflare handlers (usage, err vs err, usage), forcing mixed assignment forms here. Consider unifying handler signatures across channel packages to reduce cognitive overhead.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
relay/channel/cloudflare/adaptor.go(4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
relay/channel/cloudflare/adaptor.go (2)
relay/constant/relay_mode.go (1)
RelayModeResponses(51-51)relay/channel/openai/relay_responses.go (2)
OaiResponsesStreamHandler(50-97)OaiResponsesHandler(18-48)
🔇 Additional comments (2)
relay/channel/cloudflare/adaptor.go (2)
11-11: Import looks good and is used correctly in DoResponse.
37-38: Confirmed Cloudflare adapter usesinfo.ApiVersionas the account ID
All existing Cloudflare endpoints (chat/completions, embeddings, run) useinfo.ApiVersion—populated from theapi_versionconfig key—as the account identifier. The new/ai/v1/responsespath follows the same pattern, so no change is needed.
| func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) { | ||
| // TODO implement me | ||
| return nil, errors.New("not implemented") | ||
| return request, nil | ||
| } |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Pass-through conversion works; consider enforcing model mapping and stream flag.
To reduce vendor mismatch issues, set the target model and stream flag explicitly before forwarding to Cloudflare, if dto.OpenAIResponsesRequest includes these fields.
Assuming dto.OpenAIResponsesRequest has Model and Stream fields, update as below:
-func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
- return request, nil
-}
+func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
+ // Ensure the upstream vendor model ID is used.
+ if info != nil && info.UpstreamModelName != "" {
+ request.Model = info.UpstreamModelName
+ }
+ // Keep the stream flag consistent with routing.
+ if info != nil {
+ request.Stream = info.IsStream
+ }
+ return request, nil
+}If you're unsure about the DTO fields, run:
🏁 Script executed:
#!/bin/bash
# Show the OpenAIResponsesRequest struct to confirm Model/Stream fields
rg -n -A6 -B2 "type OpenAIResponsesRequest struct" dto | sed -n '1,200p'Length of output: 797
Enforce explicit model mapping in ConvertOpenAIResponsesRequest
The OpenAIResponsesRequest DTO includes a Model field but does not have a Stream flag. You can map the upstream model name, but the stream‐flag suggestion isn’t applicable unless you first extend the DTO.
• File: relay/channel/cloudflare/adaptor.go
• Method: ConvertOpenAIResponsesRequest
Proposed diff:
func (a *Adaptor) ConvertOpenAIResponsesRequest(
c *gin.Context,
info *relaycommon.RelayInfo,
request dto.OpenAIResponsesRequest,
) (any, error) {
- return request, nil
+ // Ensure the upstream vendor model ID is used.
+ if info != nil && info.UpstreamModelName != "" {
+ request.Model = info.UpstreamModelName
+ }
+ return request, nil
}If you need to preserve or control streaming on this path, add a Stream bool field to the DTO and map info.IsStream accordingly.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) { | |
| // TODO implement me | |
| return nil, errors.New("not implemented") | |
| return request, nil | |
| } | |
| func (a *Adaptor) ConvertOpenAIResponsesRequest( | |
| c *gin.Context, | |
| info *relaycommon.RelayInfo, | |
| request dto.OpenAIResponsesRequest, | |
| ) (any, error) { | |
| // Ensure the upstream vendor model ID is used. | |
| if info != nil && info.UpstreamModelName != "" { | |
| request.Model = info.UpstreamModelName | |
| } | |
| return request, nil | |
| } |
🤖 Prompt for AI Agents
In relay/channel/cloudflare/adaptor.go around lines 62 to 64, the
ConvertOpenAIResponsesRequest currently returns the incoming DTO as-is and does
not explicitly map the upstream model name or handle streaming; update this
method to explicitly set the request.Model from info.Model (or other upstream
mapping) before returning, and if streaming must be controlled on this path then
add a Stream bool to dto.OpenAIResponsesRequest and set it from info.IsStream
inside this method so the request contains an explicit model mapping and an
explicit stream flag when needed.
cloudflare有些开源模型只支持reponses模式
Summary by CodeRabbit