From eb4ccd173e35b459661fff4a4f079f6f96fccc56 Mon Sep 17 00:00:00 2001 From: wucm667 Date: Fri, 16 Oct 2026 20:32:00 +0800 Subject: [PATCH] fix(openai): apply reasoning mappings to synthesized effort --- .../internal/handler/composite_platform.go | 7 ---- .../handler/composite_platform_test.go | 36 ++++++++++++++----- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/backend/internal/handler/composite_platform.go b/backend/internal/handler/composite_platform.go index 6e613aa3e4..764a6a7f9d 100644 --- a/backend/internal/handler/composite_platform.go +++ b/backend/internal/handler/composite_platform.go @@ -163,13 +163,6 @@ func bindOpenAIReasoningEffortPolicyForMessagesRequest(c *gin.Context, apiKey *s return } bindRequestedReasoningEffort(c, body, strings.TrimSpace(gjson.GetBytes(body, "model").String())) - // The Messages bridge synthesizes a default OpenAI effort when - // output_config.effort is omitted. Bind the group policy only for an - // explicit client value so the ceiling does not alter that default. - effort := gjson.GetBytes(body, "output_config.effort") - if !effort.Exists() || effort.Type != gjson.String || strings.TrimSpace(effort.String()) == "" { - return - } maxEffort, mappings, overLimit, ok := openAIReasoningEffortPolicyForRequest(c, apiKey) if !ok { return diff --git a/backend/internal/handler/composite_platform_test.go b/backend/internal/handler/composite_platform_test.go index 7704a129a0..4dc504e6c3 100644 --- a/backend/internal/handler/composite_platform_test.go +++ b/backend/internal/handler/composite_platform_test.go @@ -133,14 +133,34 @@ func TestOpenAIReasoningEffortPolicyForCompositeTarget(t *testing.T) { require.True(t, changed) require.JSONEq(t, `{"reasoning":{"effort":"medium"}}`, string(bound)) - omittedCtx, _ := gin.CreateTestContext(httptest.NewRecorder()) - omittedCtx.Request = httptest.NewRequest("POST", "/v1/messages", nil) - omittedCtx.Request = omittedCtx.Request.WithContext(service.WithResolvedTargetPlatform(omittedCtx.Request.Context(), service.PlatformOpenAI)) - bindOpenAIReasoningEffortPolicyForMessagesRequest(omittedCtx, apiKey, []byte(`{"model":"gpt-5"}`)) - omitted, changed, err := service.ApplyOpenAIReasoningEffortPolicyFromContext(omittedCtx.Request.Context(), body) - require.NoError(t, err) - require.False(t, changed) - require.Equal(t, body, omitted) + synthesizedGroup := &service.Group{ + Platform: service.PlatformComposite, + ReasoningEffortMappings: []service.ReasoningEffortMapping{ + {From: "medium", To: "low"}, + {From: "none", To: "low"}, + }, + } + synthesizedAPIKey := &service.APIKey{Group: synthesizedGroup} + for _, tc := range []struct { + name string + request string + response string + want string + }{ + {name: "default medium", request: `{"model":"gpt-5"}`, response: `{"reasoning":{"effort":"medium"}}`, want: "low"}, + {name: "disabled thinking none", request: `{"model":"gpt-5","thinking":{"type":"disabled"}}`, response: `{"reasoning":{"effort":"none"}}`, want: "low"}, + } { + t.Run(tc.name, func(t *testing.T) { + omittedCtx, _ := gin.CreateTestContext(httptest.NewRecorder()) + omittedCtx.Request = httptest.NewRequest("POST", "/v1/messages", nil) + omittedCtx.Request = omittedCtx.Request.WithContext(service.WithResolvedTargetPlatform(omittedCtx.Request.Context(), service.PlatformOpenAI)) + bindOpenAIReasoningEffortPolicyForMessagesRequest(omittedCtx, synthesizedAPIKey, []byte(tc.request)) + got, changed, err := service.ApplyOpenAIReasoningEffortPolicyFromContext(omittedCtx.Request.Context(), []byte(tc.response)) + require.NoError(t, err) + require.True(t, changed) + require.Equal(t, tc.want, gjson.GetBytes(got, "reasoning.effort").String()) + }) + } denyGroup := *group denyGroup.MaxReasoningEffortOverLimit = service.ReasoningEffortOverLimitDeny