Skip to content

cloudflare支持responses - #1568

Closed
duyazhe wants to merge 2 commits into
QuantumNous:alphafrom
duyazhe:main
Closed

cloudflare支持responses#1568
duyazhe wants to merge 2 commits into
QuantumNous:alphafrom
duyazhe:main

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 channel, including the /ai/v1/responses endpoint.
    • Enables both streaming and non‑streaming response handling for improved compatibility with OpenAI-style clients.
    • Provides a smoother developer experience when integrating response streams in Cloudflare deployments.

@coderabbitai

coderabbitai Bot commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Added 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

Cohort / File(s) Summary of Changes
Cloudflare adaptor – Responses support
relay/channel/cloudflare/adaptor.go
Added OpenAI channel import; extended GetRequestURL with RelayModeResponses to /ai/v1/responses; implemented ConvertOpenAIResponsesRequest as pass-through; updated DoResponse to handle RelayModeResponses using OaiResponsesStreamHandler (streaming) and OaiResponsesHandler (non-streaming).

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Poem

I twitch my ears at new response flows,
A gentle stream or solid prose.
Through Cloudflare paths the packets hop,
OpenAI handlers take the stop.
I thump my paw—requests align,
Responses brewed, byte-fine, divine.
Carrots for code; ship time! 🥕🚀

✨ 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,感谢!

@duyazhe duyazhe closed this Aug 11, 2025

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

📥 Commits

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

📒 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 uses info.ApiVersion as the account ID
All existing Cloudflare endpoints (chat/completions, embeddings, run) use info.ApiVersion—populated from the api_version config key—as the account identifier. The new /ai/v1/responses path follows the same pattern, so no change is needed.

Comment on lines 62 to 64
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
}

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

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.

Suggested change
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.

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.

1 participant