Skip to content

feat: add responses support for cloudflare - #1569

Merged
Calcium-Ion merged 1 commit into
QuantumNous:alphafrom
duyazhe:codex/cloudflare-responses
Aug 12, 2025
Merged

feat: add responses support for cloudflare#1569
Calcium-Ion merged 1 commit into
QuantumNous:alphafrom
duyazhe:codex/cloudflare-responses

Conversation

@duyazhe

@duyazhe duyazhe commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

cloudflare有些开源模型只支持reponses模式

Summary by CodeRabbit

  • New Features

    • Added support for the Responses API in the Cloudflare relay, enabling both streaming and non‑streaming response handling.
    • Requests are now routed to the responses endpoint, improving compatibility with OpenAI-style workflows.
    • Streaming now uses dedicated handlers for more reliable, real-time output.
  • Notes

    • No changes required for existing integrations; current functionality remains unaffected.
    • This update prepares the relay for broader response-handling scenarios.

@coderabbitai

coderabbitai Bot commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Adds 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

Cohort / File(s) Summary
Cloudflare adaptor (responses mode support)
relay/channel/cloudflare/adaptor.go
- Imported openai handlers for responses
- Extended GetRequestURL to route RelayModeResponses to /ai/v1/responses
- Implemented ConvertOpenAIResponsesRequest as passthrough
- Updated DoResponse to handle RelayModeResponses with streaming and non-streaming handlers

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Poem

A rabbit taps keys with twitchy delight,
New “responses” hop onto the Cloudflare site.
Stream or not, we route just so,
Handlers ready—onward we go!
Tiny paws, tidy code, neat replies—
Carrots cached, and tests arise. 🥕✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • 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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@duyazhe

duyazhe commented Aug 11, 2025

Copy link
Copy Markdown
Contributor Author

@mrhaoji 请帮忙 review & merge,感谢!

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

🧹 Nitpick comments (1)
relay/channel/cloudflare/adaptor.go (1)

110-115: Return order inconsistency vs other branches; consider thin wrappers to normalize

Here 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

📥 Commits

Reviewing files that changed from the base of the PR and between e74c6f5 and c87a741.

📒 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 endpoint

Mapping RelayModeResponses to /client/v4/accounts/{account_id}/ai/v1/responses is 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 verified

GenRelayInfoResponses explicitly sets info.IsStream = req.Stream for RelayModeResponses, and the Cloudflare adaptor’s DoResponse correctly dispatches to OaiResponsesStreamHandler when IsStream is true. No further changes needed.

"net/http"
"one-api/dto"
"one-api/relay/channel"
"one-api/relay/channel/openai"

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.

💡 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 || true

Length 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
    • Remove import "one-api/relay/channel/openai"
    • Import one-api/relay/channel/responses instead
    • Change calls to openai.OaiResponsesHandlerresponses.OaiResponsesHandler (and likewise for the stream variant)
  • relay/channel/openai/relay_responses.go
    • Move this file (or its handler functions) into relay/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.

@Calcium-Ion
Calcium-Ion merged commit 38c3349 into QuantumNous:alpha Aug 12, 2025
3 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Apr 22, 2026
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
…ponses

feat: add responses support for cloudflare
@coderabbitai coderabbitai Bot mentioned this pull request Jul 19, 2026
11 tasks
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