feat: enhance text protocol conversion and advanced custom routing (#5825)

* refactor: consolidate relay protocol converters

* refactor relayconvert text converters

* feat: refine relay converters and advanced custom routing

* refactor: enhance logging and add thought signature handling for Gemini requests

* refactor: enhance channel cache and pricing endpoint handling for advanced custom models

* feat: preserve billing usage semantics

* feat: add protocol-aware billing usage

* Delete useless files

* chore: update action versions in workflow files

* chore: update Docker action versions in workflow files

* fix: harden billing usage settlement and hot-path route matching

- estimate Gemini completion tokens locally when billable usageMetadata is
  prompt-only but output content was received (e.g. client aborts the stream
  before the final chunk), and rebuild the attached billing_usage as estimated
  so settlement does not bill zero output tokens
- guard NewClaudeMessagesBillingUsage against all-zero ClaudeUsage, matching
  the OpenAI/Gemini constructors, so a zero billing_usage cannot override a
  non-zero top-level usage during settlement
- cache compiled advanced-custom route model regexes; they run on the request
  hot path and were recompiled per request
- move the effectiveBillingUsage remap to PostTextConsumeQuota only, and
  document that calculateTextQuotaSummary expects remapped usage
- document the updatePricingLock -> channelSyncLock lock ordering that
  InitChannelCache/CacheUpdateChannel rely on, and the aux-struct pitfall in
  GeminiChatResponse.UnmarshalJSON
This commit is contained in:
Calcium-Ion
2026-07-11 20:44:12 +08:00
committed by GitHub
parent 1250fb2eb5
commit c36418c863
106 changed files with 13345 additions and 4307 deletions
+69 -52
View File
@@ -17,6 +17,7 @@ import (
relaycommon "github.com/QuantumNous/new-api/relay/common"
relayconstant "github.com/QuantumNous/new-api/relay/constant"
"github.com/QuantumNous/new-api/service"
"github.com/QuantumNous/new-api/service/relayconvert"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/samber/lo"
@@ -48,20 +49,19 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
if err != nil {
return nil, err
}
if converter == dto.AdvancedCustomConverterNone {
if converter == relayconvert.ConverterNone {
return a.convertOpenAICompatibleRequest(c, info, request)
}
switch converter {
case dto.AdvancedCustomConverterOpenAIChatCompletionsToAnthropicMessages:
return a.claudeAdaptor.ConvertOpenAIRequest(c, info, request)
case dto.AdvancedCustomConverterOpenAIChatCompletionsToOpenAIResponses:
if request == nil {
return nil, errors.New("request is nil")
case relayconvert.ConverterOpenAIChatToClaudeMessages,
relayconvert.ConverterOpenAIChatToOpenAIResponses,
relayconvert.ConverterOpenAIChatToGeminiContent:
result, err := service.ConvertRequestByID(c, info, converter, request)
if err != nil {
return nil, err
}
return service.ChatCompletionsRequestToResponsesRequest(request)
case dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent:
return a.geminiAdaptor.ConvertOpenAIRequest(c, info, request)
return result.Value, nil
default:
return nil, fmt.Errorf("converter %q does not support OpenAI chat completions requests", converter)
}
@@ -74,10 +74,18 @@ func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayIn
}
switch converter {
case dto.AdvancedCustomConverterNone:
case relayconvert.ConverterNone:
return a.claudeAdaptor.ConvertClaudeRequest(c, info, request)
case dto.AdvancedCustomConverterAnthropicMessagesToOpenAIChatCompletions:
return a.convertClaudeToOpenAICompatibleRequest(c, info, request)
case relayconvert.ConverterClaudeMessagesToOpenAIChat:
result, err := service.ConvertRequestByID(c, info, converter, request)
if err != nil {
return nil, err
}
chatRequest, ok := result.Value.(*dto.GeneralOpenAIRequest)
if !ok {
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
}
return a.convertOpenAICompatibleRequest(c, info, chatRequest)
default:
return nil, fmt.Errorf("converter %q does not support Anthropic Messages requests", converter)
}
@@ -90,10 +98,18 @@ func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayIn
}
switch converter {
case dto.AdvancedCustomConverterNone:
case relayconvert.ConverterNone:
return a.geminiAdaptor.ConvertGeminiRequest(c, info, request)
case dto.AdvancedCustomConverterGeminiGenerateContentToOpenAIChatCompletions:
return a.convertGeminiToOpenAICompatibleRequest(c, info, request)
case relayconvert.ConverterGeminiContentToOpenAIChat:
result, err := service.ConvertRequestByID(c, info, converter, request)
if err != nil {
return nil, err
}
chatRequest, ok := result.Value.(*dto.GeneralOpenAIRequest)
if !ok {
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
}
return a.convertOpenAICompatibleRequest(c, info, chatRequest)
default:
return nil, fmt.Errorf("converter %q does not support Gemini generateContent requests", converter)
}
@@ -105,14 +121,28 @@ func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommo
return nil, err
}
switch converter {
case dto.AdvancedCustomConverterNone:
case relayconvert.ConverterNone:
return a.convertOpenAICompatibleResponsesRequest(c, info, request)
case dto.AdvancedCustomConverterOpenAIResponsesToOpenAIChatCompletions:
chatReq, err := service.ResponsesRequestToChatCompletionsRequest(&request)
case relayconvert.ConverterOpenAIResponsesToOpenAIChat:
result, err := service.ConvertRequestByID(c, info, converter, request)
if err != nil {
return nil, err
}
return a.convertOpenAICompatibleRequest(c, info, chatReq)
chatRequest, ok := result.Value.(*dto.GeneralOpenAIRequest)
if !ok {
return nil, fmt.Errorf("expected OpenAI chat completions request, got %T", result.Value)
}
return a.convertOpenAICompatibleRequest(c, info, chatRequest)
case relayconvert.ConverterOpenAIResponsesToGemini:
result, err := service.ConvertRequestByID(c, info, converter, request)
if err != nil {
return nil, err
}
geminiRequest, ok := result.Value.(*dto.GeminiChatRequest)
if !ok {
return nil, fmt.Errorf("expected Gemini generateContent request, got %T", result.Value)
}
return geminiRequest, nil
default:
return nil, fmt.Errorf("converter %q does not support OpenAI Responses requests", converter)
}
@@ -123,7 +153,7 @@ func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.Rela
if err != nil {
return nil, err
}
if converter != dto.AdvancedCustomConverterNone {
if converter != relayconvert.ConverterNone {
return nil, fmt.Errorf("converter %q does not support embedding requests", converter)
}
return a.convertOpenAICompatibleEmbeddingRequest(c, info, request)
@@ -134,7 +164,7 @@ func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInf
if err != nil {
return nil, err
}
if converter != dto.AdvancedCustomConverterNone {
if converter != relayconvert.ConverterNone {
return nil, fmt.Errorf("converter %q does not support audio requests", converter)
}
return a.convertOpenAICompatibleAudioRequest(c, info, request)
@@ -145,7 +175,7 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
if err != nil {
return nil, err
}
if converter != dto.AdvancedCustomConverterNone {
if converter != relayconvert.ConverterNone {
return nil, fmt.Errorf("converter %q does not support image requests", converter)
}
return a.convertOpenAICompatibleImageRequest(c, info, request)
@@ -194,7 +224,7 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
if err := a.resolve(c, info); err != nil {
return nil, err
}
if !a.converted && a.converter != dto.AdvancedCustomConverterNone {
if !a.converted && a.converter != relayconvert.ConverterNone {
return nil, errors.New("advanced custom converter routes cannot be used with pass-through request body")
}
@@ -215,21 +245,23 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycom
}
switch a.converter {
case dto.AdvancedCustomConverterNone:
case relayconvert.ConverterNone:
return a.doNativeResponse(c, resp, info)
case dto.AdvancedCustomConverterAnthropicMessagesToOpenAIChatCompletions,
dto.AdvancedCustomConverterGeminiGenerateContentToOpenAIChatCompletions:
case relayconvert.ConverterClaudeMessagesToOpenAIChat,
relayconvert.ConverterGeminiContentToOpenAIChat:
return a.openaiAdaptor.DoResponse(c, resp, info)
case dto.AdvancedCustomConverterOpenAIChatCompletionsToAnthropicMessages:
case relayconvert.ConverterOpenAIChatToClaudeMessages:
return a.claudeAdaptor.DoResponse(c, resp, info)
case dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent:
case relayconvert.ConverterOpenAIChatToGeminiContent:
return a.geminiAdaptor.DoResponse(c, resp, info)
case dto.AdvancedCustomConverterOpenAIChatCompletionsToOpenAIResponses:
case relayconvert.ConverterOpenAIResponsesToGemini:
return a.geminiAdaptor.DoResponse(c, resp, info)
case relayconvert.ConverterOpenAIChatToOpenAIResponses:
if info.IsStream {
return openai.OaiResponsesToChatStreamHandler(c, info, resp)
}
return openai.OaiResponsesToChatHandler(c, info, resp)
case dto.AdvancedCustomConverterOpenAIResponsesToOpenAIChatCompletions:
case relayconvert.ConverterOpenAIResponsesToOpenAIChat:
if info.IsStream {
return openai.OaiChatToResponsesStreamHandler(c, info, resp)
}
@@ -286,18 +318,18 @@ func (a *Adaptor) resolve(c *gin.Context, info *relaycommon.RelayInfo) error {
}
incomingPath := incomingRequestPath(c, info)
route, ok := config.MatchPath(incomingPath)
route, ok := config.MatchPathForModel(incomingPath, info.OriginModelName)
if ok {
route.Converter = strings.TrimSpace(route.Converter)
if route.Converter == "" {
route.Converter = dto.AdvancedCustomConverterNone
route.Converter = relayconvert.ConverterNone
}
a.route = route
a.converter = route.Converter
a.resolved = true
return nil
}
return fmt.Errorf("advanced custom channel does not support request path: %s", incomingPath)
return fmt.Errorf("advanced custom channel does not support request path %s for model %s", incomingPath, info.OriginModelName)
}
func incomingRequestPath(c *gin.Context, info *relaycommon.RelayInfo) string {
@@ -391,7 +423,8 @@ func applyUpstreamPathTemplate(upstreamPath string, info *relaycommon.RelayInfo)
func shouldUseGeminiStreamURL(converter string, info *relaycommon.RelayInfo) bool {
return info != nil &&
info.IsStream &&
converter == dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent
(converter == relayconvert.ConverterOpenAIChatToGeminiContent ||
converter == relayconvert.ConverterOpenAIResponsesToGemini)
}
func useGeminiStreamGenerateContentURL(parsedURL *url.URL) {
@@ -406,8 +439,8 @@ func useGeminiStreamGenerateContentURL(parsedURL *url.URL) {
}
func shouldApplyClaudeHeaders(converter string, info *relaycommon.RelayInfo) bool {
return converter == dto.AdvancedCustomConverterOpenAIChatCompletionsToAnthropicMessages ||
(converter == dto.AdvancedCustomConverterNone && info != nil && info.RelayFormat == types.RelayFormatClaude)
return converter == relayconvert.ConverterOpenAIChatToClaudeMessages ||
(converter == relayconvert.ConverterNone && info != nil && info.RelayFormat == types.RelayFormatClaude)
}
func applyClaudeHeaders(c *gin.Context, header *http.Header, info *relaycommon.RelayInfo) {
@@ -443,22 +476,6 @@ func (a *Adaptor) convertOpenAICompatibleRequest(c *gin.Context, info *relaycomm
return converted, err
}
func (a *Adaptor) convertClaudeToOpenAICompatibleRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.ClaudeRequest) (any, error) {
old := info.ChannelType
info.ChannelType = constant.ChannelTypeOpenAI
converted, err := a.openaiAdaptor.ConvertClaudeRequest(c, info, request)
info.ChannelType = old
return converted, err
}
func (a *Adaptor) convertGeminiToOpenAICompatibleRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
old := info.ChannelType
info.ChannelType = constant.ChannelTypeOpenAI
converted, err := a.openaiAdaptor.ConvertGeminiRequest(c, info, request)
info.ChannelType = old
return converted, err
}
func (a *Adaptor) convertOpenAICompatibleResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
old := info.ChannelType
info.ChannelType = constant.ChannelTypeOpenAI
+350 -18
View File
@@ -1,6 +1,8 @@
package advancedcustom
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"net/url"
@@ -11,6 +13,8 @@ import (
"github.com/QuantumNous/new-api/dto"
relaycommon "github.com/QuantumNous/new-api/relay/common"
relayconstant "github.com/QuantumNous/new-api/relay/constant"
"github.com/QuantumNous/new-api/service/relayconvert"
"github.com/QuantumNous/new-api/setting/model_setting"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
@@ -24,7 +28,7 @@ func TestAdaptorUsesExactRouteAndQueryAuth(t *testing.T) {
{
IncomingPath: "/v1/messages",
UpstreamPath: "https://upstream.example/v1/chat/completions?existing=1",
Converter: dto.AdvancedCustomConverterAnthropicMessagesToOpenAIChatCompletions,
Converter: relayconvert.ConverterClaudeMessagesToOpenAIChat,
Auth: &dto.AdvancedCustomRouteAuth{
Type: dto.AdvancedCustomAuthTypeQuery,
Name: "api_key",
@@ -54,7 +58,7 @@ func TestAdaptorJoinsUpstreamPathWithChannelBaseURL(t *testing.T) {
{
IncomingPath: "/v1/chat/completions",
UpstreamPath: "/proxy/v1/chat/completions?existing=1",
Converter: dto.AdvancedCustomConverterNone,
Converter: relayconvert.ConverterNone,
Auth: &dto.AdvancedCustomRouteAuth{
Type: dto.AdvancedCustomAuthTypeQuery,
Name: "api_key",
@@ -84,7 +88,7 @@ func TestAdaptorReturnsErrorWhenUpstreamPathNeedsMissingBaseURL(t *testing.T) {
{
IncomingPath: "/v1/chat/completions",
UpstreamPath: "/v1/chat/completions",
Converter: dto.AdvancedCustomConverterNone,
Converter: relayconvert.ConverterNone,
},
},
})
@@ -102,7 +106,7 @@ func TestAdaptorSetupRequestHeaderUsesDefaultBearerAuth(t *testing.T) {
{
IncomingPath: "/v1/chat/completions",
UpstreamPath: "https://upstream.example/v1/chat/completions",
Converter: dto.AdvancedCustomConverterNone,
Converter: relayconvert.ConverterNone,
},
},
})
@@ -120,7 +124,7 @@ func TestAdaptorSetupRequestHeaderUsesConfiguredHeaderAuth(t *testing.T) {
{
IncomingPath: "/v1/chat/completions",
UpstreamPath: "https://upstream.example/v1/chat/completions",
Converter: dto.AdvancedCustomConverterNone,
Converter: relayconvert.ConverterNone,
Auth: &dto.AdvancedCustomRouteAuth{
Type: dto.AdvancedCustomAuthTypeHeader,
Name: "x-api-key",
@@ -144,7 +148,7 @@ func TestAdaptorSetupRequestHeaderAddsClaudeDefaultHeaders(t *testing.T) {
{
IncomingPath: "/v1/messages",
UpstreamPath: "https://api.anthropic.com/v1/messages",
Converter: dto.AdvancedCustomConverterNone,
Converter: relayconvert.ConverterNone,
Auth: &dto.AdvancedCustomRouteAuth{
Type: dto.AdvancedCustomAuthTypeHeader,
Name: "x-api-key",
@@ -169,7 +173,7 @@ func TestAdaptorReturnsErrorWhenNoRouteMatchesPath(t *testing.T) {
{
IncomingPath: "/v1/messages",
UpstreamPath: "https://upstream.example/v1/chat/completions",
Converter: dto.AdvancedCustomConverterAnthropicMessagesToOpenAIChatCompletions,
Converter: relayconvert.ConverterClaudeMessagesToOpenAIChat,
},
},
})
@@ -187,7 +191,7 @@ func TestAdaptorReplacesModelPlaceholderInRouteURL(t *testing.T) {
{
IncomingPath: "/v1/chat/completions",
UpstreamPath: "https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent",
Converter: dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent,
Converter: relayconvert.ConverterOpenAIChatToGeminiContent,
Auth: &dto.AdvancedCustomRouteAuth{
Type: dto.AdvancedCustomAuthTypeQuery,
Name: "key",
@@ -215,7 +219,7 @@ func TestAdaptorSwitchesGeminiGenerateContentURLForStream(t *testing.T) {
{
IncomingPath: "/v1/chat/completions",
UpstreamPath: "https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?existing=1",
Converter: dto.AdvancedCustomConverterOpenAIChatCompletionsToGeminiGenerateContent,
Converter: relayconvert.ConverterOpenAIChatToGeminiContent,
Auth: &dto.AdvancedCustomRouteAuth{
Type: dto.AdvancedCustomAuthTypeQuery,
Name: "key",
@@ -264,7 +268,7 @@ func TestAdaptorMatchesGeminiIncomingPathTemplate(t *testing.T) {
{
IncomingPath: "/v1beta/models/{model}:generateContent",
UpstreamPath: "https://upstream.example/v1/chat/completions",
Converter: dto.AdvancedCustomConverterGeminiGenerateContentToOpenAIChatCompletions,
Converter: relayconvert.ConverterGeminiContentToOpenAIChat,
},
},
})
@@ -287,13 +291,17 @@ func TestAdaptorConvertsResponsesRequestToOpenAIChatUpstream(t *testing.T) {
{
IncomingPath: "/v1/responses",
UpstreamPath: "/v1/chat/completions",
Converter: dto.AdvancedCustomConverterOpenAIResponsesToOpenAIChatCompletions,
Converter: relayconvert.ConverterOpenAIResponsesToOpenAIChat,
},
},
})
info.RelayMode = relayconstant.RelayModeResponses
info.RequestURLPath = "/v1/responses"
c := advancedCustomGinContext("/v1/responses")
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", nil)
c.Request.Header.Set("Content-Type", "application/json")
converted, err := adaptor.ConvertOpenAIResponsesRequest(c, info, dto.OpenAIResponsesRequest{
Model: "gpt-test",
@@ -318,15 +326,339 @@ func TestAdaptorConvertsResponsesRequestToOpenAIChatUpstream(t *testing.T) {
assert.Equal(t, "/v1/chat/completions", parsedURL.Path)
}
func TestAdaptorSelectsDuplicateResponsesRoutesByModel(t *testing.T) {
config := &dto.AdvancedCustomConfig{
Routes: []dto.AdvancedCustomRoute{
{
IncomingPath: "/v1/responses",
UpstreamPath: "/v1/chat/completions",
Converter: relayconvert.ConverterOpenAIResponsesToOpenAIChat,
Models: []string{"gpt-test"},
},
{
IncomingPath: "/v1/responses",
UpstreamPath: "/v1beta/models/{model}:generateContent",
Converter: relayconvert.ConverterOpenAIResponsesToGemini,
Models: []string{"gemini-test"},
},
},
}
chatAdaptor := &Adaptor{}
chatInfo := advancedCustomRelayInfo(config)
chatInfo.RelayFormat = types.RelayFormatOpenAIResponses
chatInfo.RelayMode = relayconstant.RelayModeResponses
chatInfo.RequestURLPath = "/v1/responses"
chatInfo.OriginModelName = "gpt-test"
chatInfo.UpstreamModelName = "gpt-test"
chatConverted, err := chatAdaptor.ConvertOpenAIResponsesRequest(advancedCustomGinContext("/v1/responses"), chatInfo, dto.OpenAIResponsesRequest{
Model: "gpt-test",
Input: mustAdvancedCustomRawMessage(t, "hello"),
})
require.NoError(t, err)
_, ok := chatConverted.(*dto.GeneralOpenAIRequest)
require.True(t, ok)
geminiAdaptor := &Adaptor{}
geminiInfo := advancedCustomRelayInfo(config)
geminiInfo.RelayFormat = types.RelayFormatOpenAIResponses
geminiInfo.RelayMode = relayconstant.RelayModeResponses
geminiInfo.RequestURLPath = "/v1/responses"
geminiInfo.OriginModelName = "gemini-test"
geminiInfo.UpstreamModelName = "gemini-test"
geminiInfo.IsStream = true
geminiConverted, err := geminiAdaptor.ConvertOpenAIResponsesRequest(advancedCustomGinContext("/v1/responses"), geminiInfo, dto.OpenAIResponsesRequest{
Model: "gemini-test",
Input: mustAdvancedCustomRawMessage(t, "hello"),
})
require.NoError(t, err)
_, ok = geminiConverted.(*dto.GeminiChatRequest)
require.True(t, ok)
requestURL, err := geminiAdaptor.GetRequestURL(geminiInfo)
require.NoError(t, err)
parsedURL, err := url.Parse(requestURL)
require.NoError(t, err)
assert.Equal(t, "/v1beta/models/gemini-test:streamGenerateContent", parsedURL.Path)
assert.Equal(t, "sse", parsedURL.Query().Get("alt"))
}
func TestAdaptorResponsesToGeminiUsesResponsesBridge(t *testing.T) {
adaptor := &Adaptor{}
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
Routes: []dto.AdvancedCustomRoute{
{
IncomingPath: "/v1/responses",
UpstreamPath: "/v1beta/models/{model}:generateContent",
Converter: relayconvert.ConverterOpenAIResponsesToGemini,
Models: []string{"gemini-test"},
},
},
})
info.RelayFormat = types.RelayFormatOpenAIResponses
info.RelayMode = relayconstant.RelayModeResponses
info.RequestURLPath = "/v1/responses"
info.OriginModelName = "gemini-test"
info.UpstreamModelName = "gemini-test"
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", nil)
c.Request.Header.Set("Content-Type", "application/json")
payload := dto.GeminiChatResponse{
Candidates: []dto.GeminiChatCandidate{
{
Content: dto.GeminiChatContent{
Role: "model",
Parts: []dto.GeminiPart{
{Text: "hello"},
},
},
},
},
UsageMetadata: dto.GeminiUsageMetadata{
PromptTokenCount: 2,
CandidatesTokenCount: 3,
TotalTokenCount: 5,
},
}
body, err := common.Marshal(payload)
require.NoError(t, err)
usage, newAPIError := adaptor.DoResponse(c, &http.Response{
Body: io.NopCloser(bytes.NewReader(body)),
}, info)
require.Nil(t, newAPIError)
require.NotNil(t, usage)
got := recorder.Body.String()
assert.Contains(t, got, `"object":"response"`)
assert.Contains(t, got, `"type":"output_text"`)
assert.Contains(t, got, `"text":"hello"`)
assert.NotContains(t, got, `"candidates"`)
}
func TestAdaptorResponsesToGeminiAddsThoughtSignatureForFunctionCallHistory(t *testing.T) {
geminiSettings := model_setting.GetGeminiSettings()
originalThoughtSignatureEnabled := geminiSettings.FunctionCallThoughtSignatureEnabled
geminiSettings.FunctionCallThoughtSignatureEnabled = true
t.Cleanup(func() {
geminiSettings.FunctionCallThoughtSignatureEnabled = originalThoughtSignatureEnabled
})
adaptor := &Adaptor{}
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
Routes: []dto.AdvancedCustomRoute{
{
IncomingPath: "/v1/responses",
UpstreamPath: "/v1beta/models/{model}:generateContent",
Converter: relayconvert.ConverterOpenAIResponsesToGemini,
Models: []string{"gemini-test"},
},
},
})
info.RelayFormat = types.RelayFormatOpenAIResponses
info.RelayMode = relayconstant.RelayModeResponses
info.RequestURLPath = "/v1/responses"
info.OriginModelName = "gemini-test"
info.UpstreamModelName = "gemini-test"
converted, err := adaptor.ConvertOpenAIResponsesRequest(advancedCustomGinContext("/v1/responses"), info, dto.OpenAIResponsesRequest{
Model: "gemini-test",
Input: mustAdvancedCustomRawMessage(t, []map[string]any{
{
"role": "user",
"content": "hi",
},
{
"type": "function_call",
"call_id": "call_1",
"name": "glob",
"arguments": map[string]any{"query": "*"},
},
{
"type": "function_call_output",
"call_id": "call_1",
"output": []map[string]any{{"path": "report.md"}},
},
}),
Tools: mustAdvancedCustomRawMessage(t, []map[string]any{
{"type": "function", "name": "glob", "parameters": map[string]any{"type": "object"}},
}),
})
require.NoError(t, err)
geminiReq, ok := converted.(*dto.GeminiChatRequest)
require.True(t, ok)
require.Len(t, geminiReq.Contents, 3)
require.Len(t, geminiReq.Contents[1].Parts, 1)
require.NotNil(t, geminiReq.Contents[1].Parts[0].FunctionCall)
assert.NotEmpty(t, geminiReq.Contents[1].Parts[0].ThoughtSignature)
require.Len(t, geminiReq.Contents[2].Parts, 1)
require.NotNil(t, geminiReq.Contents[2].Parts[0].FunctionResponse)
assert.Empty(t, geminiReq.Contents[2].Parts[0].ThoughtSignature)
}
func TestAdaptorConvertsOpenAIChatRequestToResponsesUpstream(t *testing.T) {
adaptor := &Adaptor{}
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
Routes: []dto.AdvancedCustomRoute{
{
IncomingPath: "/v1/chat/completions",
UpstreamPath: "/v1/responses",
Converter: relayconvert.ConverterOpenAIChatToOpenAIResponses,
},
},
})
c := advancedCustomGinContext("/v1/chat/completions")
converted, err := adaptor.ConvertOpenAIRequest(c, info, &dto.GeneralOpenAIRequest{
Model: "gpt-test",
Messages: []dto.Message{
{Role: "user", Content: "hello"},
},
})
require.NoError(t, err)
responsesReq, ok := converted.(*dto.OpenAIResponsesRequest)
require.True(t, ok)
assert.Equal(t, "gpt-test", responsesReq.Model)
assert.NotEmpty(t, responsesReq.Input)
}
func TestAdaptorConvertsOpenAIChatRequestToClaudeUpstream(t *testing.T) {
adaptor := &Adaptor{}
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
Routes: []dto.AdvancedCustomRoute{
{
IncomingPath: "/v1/chat/completions",
UpstreamPath: "/v1/messages",
Converter: relayconvert.ConverterOpenAIChatToClaudeMessages,
},
},
})
c := advancedCustomGinContext("/v1/chat/completions")
converted, err := adaptor.ConvertOpenAIRequest(c, info, &dto.GeneralOpenAIRequest{
Model: "claude-test",
Messages: []dto.Message{
{Role: "user", Content: "hello"},
},
})
require.NoError(t, err)
claudeReq, ok := converted.(*dto.ClaudeRequest)
require.True(t, ok)
assert.Equal(t, "claude-test", claudeReq.Model)
require.Len(t, claudeReq.Messages, 1)
assert.Equal(t, "user", claudeReq.Messages[0].Role)
}
func TestAdaptorConvertsOpenAIChatRequestToGeminiUpstream(t *testing.T) {
adaptor := &Adaptor{}
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
Routes: []dto.AdvancedCustomRoute{
{
IncomingPath: "/v1/chat/completions",
UpstreamPath: "/v1beta/models/{model}:generateContent",
Converter: relayconvert.ConverterOpenAIChatToGeminiContent,
},
},
})
info.UpstreamModelName = "gemini-2.5-flash"
c := advancedCustomGinContext("/v1/chat/completions")
converted, err := adaptor.ConvertOpenAIRequest(c, info, &dto.GeneralOpenAIRequest{
Model: "gemini-2.5-flash",
Messages: []dto.Message{
{Role: "user", Content: "hello"},
},
})
require.NoError(t, err)
geminiReq, ok := converted.(*dto.GeminiChatRequest)
require.True(t, ok)
require.Len(t, geminiReq.Contents, 1)
assert.Equal(t, "user", geminiReq.Contents[0].Role)
}
func TestAdaptorConvertsClaudeRequestToOpenAIChatUpstream(t *testing.T) {
adaptor := &Adaptor{}
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
Routes: []dto.AdvancedCustomRoute{
{
IncomingPath: "/v1/messages",
UpstreamPath: "/v1/chat/completions",
Converter: relayconvert.ConverterClaudeMessagesToOpenAIChat,
},
},
})
info.RelayFormat = types.RelayFormatClaude
info.RequestURLPath = "/v1/messages"
c := advancedCustomGinContext("/v1/messages")
converted, err := adaptor.ConvertClaudeRequest(c, info, &dto.ClaudeRequest{
Model: "gpt-test",
Messages: []dto.ClaudeMessage{
{Role: "user", Content: "hello"},
},
})
require.NoError(t, err)
chatReq, ok := converted.(*dto.GeneralOpenAIRequest)
require.True(t, ok)
assert.Equal(t, "gpt-test", chatReq.Model)
require.Len(t, chatReq.Messages, 1)
assert.Equal(t, "user", chatReq.Messages[0].Role)
}
func TestAdaptorConvertsGeminiRequestToOpenAIChatUpstream(t *testing.T) {
adaptor := &Adaptor{}
info := advancedCustomRelayInfo(&dto.AdvancedCustomConfig{
Routes: []dto.AdvancedCustomRoute{
{
IncomingPath: "/v1beta/models/{model}:generateContent",
UpstreamPath: "/v1/chat/completions",
Converter: relayconvert.ConverterGeminiContentToOpenAIChat,
},
},
})
info.RelayFormat = types.RelayFormatGemini
info.RequestURLPath = "/v1beta/models/gemini-2.5-flash:generateContent"
info.UpstreamModelName = "gpt-test"
c := advancedCustomGinContext("/v1beta/models/gemini-2.5-flash:generateContent")
converted, err := adaptor.ConvertGeminiRequest(c, info, &dto.GeminiChatRequest{
Contents: []dto.GeminiChatContent{
{
Role: "user",
Parts: []dto.GeminiPart{
{Text: "hello"},
},
},
},
})
require.NoError(t, err)
chatReq, ok := converted.(*dto.GeneralOpenAIRequest)
require.True(t, ok)
assert.Equal(t, "gpt-test", chatReq.Model)
require.Len(t, chatReq.Messages, 1)
assert.Equal(t, "user", chatReq.Messages[0].Role)
}
func advancedCustomRelayInfo(config *dto.AdvancedCustomConfig) *relaycommon.RelayInfo {
return &relaycommon.RelayInfo{
RelayFormat: types.RelayFormatOpenAI,
RelayMode: relayconstant.RelayModeChatCompletions,
RequestURLPath: "/v1/chat/completions",
RelayFormat: types.RelayFormatOpenAI,
RelayMode: relayconstant.RelayModeChatCompletions,
RequestURLPath: "/v1/chat/completions",
OriginModelName: "gpt-test",
ChannelMeta: &relaycommon.ChannelMeta{
ApiKey: "sk-test",
ChannelBaseUrl: "https://fallback.example",
ChannelType: constant.ChannelTypeAdvancedCustom,
ApiKey: "sk-test",
ChannelBaseUrl: "https://fallback.example",
ChannelType: constant.ChannelTypeAdvancedCustom,
UpstreamModelName: "gpt-test",
ChannelOtherSettings: dto.ChannelOtherSettings{
AdvancedCustom: config,
},