feat: add responses support for cloudflare - #1569
Conversation
WalkthroughAdds RelayModeResponses support in the Cloudflare adaptor: maps requests to /ai/v1/responses, converts OpenAI responses requests by passing through, and routes to appropriate OpenAI response handlers for streaming and non-streaming cases. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant CloudflareAdaptor
participant OpenAIHandlers as openai.Response Handlers
participant CloudflareAPI as Cloudflare /ai/v1/responses
Client->>CloudflareAdaptor: Request (RelayModeResponses, stream?)
CloudflareAdaptor->>CloudflareAPI: POST /ai/v1/responses
alt stream == true
CloudflareAPI-->>CloudflareAdaptor: Stream chunks
CloudflareAdaptor->>OpenAIHandlers: OaiResponsesStreamHandler
OpenAIHandlers-->>Client: SSE stream
else
CloudflareAPI-->>CloudflareAdaptor: JSON response
CloudflareAdaptor->>OpenAIHandlers: OaiResponsesHandler
OpenAIHandlers-->>Client: JSON
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: Return order inconsistency vs other branches; consider thin wrappers to normalizeHere you assign usage, err = openai.* which is correct for those handlers, but other branches use err, usage = cf*. Mixed conventions are easy to break later. Consider thin local wrappers to normalize to (err, usage) in this adaptor.
Apply this diff to use local wrappers in the switch:
- if info.IsStream { - usage, err = openai.OaiResponsesStreamHandler(c, info, resp) - } else { - usage, err = openai.OaiResponsesHandler(c, info, resp) - } + if info.IsStream { + err, usage = cfResponsesStreamHandler(c, info, resp) + } else { + err, usage = cfResponsesHandler(c, info, resp) + }Add these helpers in this file:
func cfResponsesHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response) (*types.NewAPIError, any) { usage, err := openai.OaiResponsesHandler(c, info, resp) return err, usage } func cfResponsesStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response) (*types.NewAPIError, any) { usage, err := openai.OaiResponsesStreamHandler(c, info, resp) return err, usage }Note: OaiResponsesHandler already closes resp.Body; the stream handler scans/streams and computes usage.
📜 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)
37-38: Confirmed Cloudflare Responses endpointMapping
RelayModeResponsesto/client/v4/accounts/{account_id}/ai/v1/responsesis correct per the official Cloudflare Workers AI REST API docs (https://developers.cloudflare.com/workers-ai/get-started/rest-api/). No changes needed.
63-63: Stream flag handling in Cloudflare adaptor verifiedGenRelayInfoResponses explicitly sets
info.IsStream = req.StreamforRelayModeResponses, and the Cloudflare adaptor’sDoResponsecorrectly dispatches toOaiResponsesStreamHandlerwhenIsStreamis true. No further changes needed.
| "net/http" | ||
| "one-api/dto" | ||
| "one-api/relay/channel" | ||
| "one-api/relay/channel/openai" |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Avoid cross-channel coupling; consider moving Responses handlers to a neutral package
Importing the openai channel from the cloudflare adaptor couples providers. It works, but increases the risk of import cycles and makes layering less clean. Prefer extracting OaiResponses* handlers into a shared helper package (e.g., relay/channel/responses or relay/common/responses) and depend on that from both channels.
Run this to ensure there’s no import cycle from the openai package back to cloudflare:
🏁 Script executed:
#!/bin/bash
# Show where OaiResponses* handlers live
rg -n 'func OaiResponses(Stream)?Handler' -A 2 -B 2
# Check whether the openai channel imports cloudflare
rg -n 'one-api/relay/channel/cloudflare' relay/channel/openai || trueLength of output: 964
Decouple Response Handlers into a Shared Package
I ran ripgrep against the OpenAI channel and confirmed that relay/channel/openai/relay_responses.go defines both OaiResponsesHandler and OaiResponsesStreamHandler, and that it does not import the Cloudflare channel (no import cycles detected). To keep clean layering and avoid cross-channel coupling, please extract these handlers into a neutral package (e.g. relay/channel/responses) and update both adaptors to depend on that.
Key locations to update:
relay/channel/cloudflare/adaptor.go
• Removeimport "one-api/relay/channel/openai"
• Importone-api/relay/channel/responsesinstead
• Change calls toopenai.OaiResponsesHandler→responses.OaiResponsesHandler(and likewise for the stream variant)relay/channel/openai/relay_responses.go
• Move this file (or its handler functions) intorelay/channel/responses
• Update package declarations and imports accordingly
Example diff for the Cloudflare adaptor:
--- a/relay/channel/cloudflare/adaptor.go
+++ b/relay/channel/cloudflare/adaptor.go
@@ -11,7 +11,7 @@ import (
- "one-api/relay/channel/openai"
+ "one-api/relay/channel/responses"
)
// … inside DoResponse switch:
- usage, err := openai.OaiResponsesHandler(c, info, resp)
+ usage, err := responses.OaiResponsesHandler(c, info, resp)This refactor preserves functionality, prevents accidental import cycles, and promotes better separation of concerns.
🤖 Prompt for AI Agents
In relay/channel/cloudflare/adaptor.go around line 11 and
relay/channel/openai/relay_responses.go (and any references to those handlers),
the Cloudflare adaptor imports openai handlers which couples channels; remove
the import "one-api/relay/channel/openai" from the Cloudflare adaptor and
replace it with import "one-api/relay/channel/responses", update all calls
openai.OaiResponsesHandler and openai.OaiResponsesStreamHandler to
responses.OaiResponsesHandler and responses.OaiResponsesStreamHandler, and move
relay/channel/openai/relay_responses.go into a new package directory
relay/channel/responses (update the file's package declaration and any imports)
so both adaptors import the neutral responses package instead of openai.
…ponses feat: add responses support for cloudflare
cloudflare有些开源模型只支持reponses模式
Summary by CodeRabbit
New Features
Notes