Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions relay/channel/cloudflare/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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.

relaycommon "one-api/relay/common"
"one-api/relay/constant"
"one-api/types"
Expand All @@ -33,6 +34,8 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
return fmt.Sprintf("%s/client/v4/accounts/%s/ai/v1/chat/completions", info.BaseUrl, info.ApiVersion), nil
case constant.RelayModeEmbeddings:
return fmt.Sprintf("%s/client/v4/accounts/%s/ai/v1/embeddings", info.BaseUrl, info.ApiVersion), nil
case constant.RelayModeResponses:
return fmt.Sprintf("%s/client/v4/accounts/%s/ai/v1/responses", info.BaseUrl, info.ApiVersion), nil
default:
return fmt.Sprintf("%s/client/v4/accounts/%s/ai/run/%s", info.BaseUrl, info.ApiVersion, info.UpstreamModelName), nil
}
Expand All @@ -57,8 +60,7 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
}

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) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
Expand Down Expand Up @@ -105,6 +107,12 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycom
} else {
err, usage = cfHandler(c, info, resp)
}
case constant.RelayModeResponses:
if info.IsStream {
usage, err = openai.OaiResponsesStreamHandler(c, info, resp)
} else {
usage, err = openai.OaiResponsesHandler(c, info, resp)
}
case constant.RelayModeAudioTranslation:
fallthrough
case constant.RelayModeAudioTranscription:
Expand Down