From 1d2dfab86d3393d6d050915c7abfe0e88cf401f1 Mon Sep 17 00:00:00 2001 From: Heatherm Huang Date: Tue, 7 Jul 2026 15:35:36 +0800 Subject: [PATCH] fix: allow composite grok messages routing --- .../internal/handler/composite_platform_test.go | 12 ++++++++++++ .../internal/handler/openai_gateway_handler.go | 6 +++++- backend/internal/server/routes/gateway_test.go | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/backend/internal/handler/composite_platform_test.go b/backend/internal/handler/composite_platform_test.go index 95dfa92d5f..30f837f3c5 100644 --- a/backend/internal/handler/composite_platform_test.go +++ b/backend/internal/handler/composite_platform_test.go @@ -22,6 +22,18 @@ func TestCompositeTargetPlatformAllowedResolvesKnownAllowedModel(t *testing.T) { require.Equal(t, service.PlatformOpenAI, platform) } +func TestOpenAICompatibleMessagesTargetAllowsCompositeGrokModel(t *testing.T) { + gin.SetMode(gin.TestMode) + c, _ := gin.CreateTestContext(httptest.NewRecorder()) + c.Request = httptest.NewRequest("POST", "/v1/messages", nil) + apiKey := &service.APIKey{Group: &service.Group{Platform: service.PlatformComposite}} + + require.True(t, openAICompatibleMessagesTargetAllowed(c, apiKey, "grok-4.3")) + platform, ok := service.ResolvedTargetPlatformFromContext(c.Request.Context()) + require.True(t, ok) + require.Equal(t, service.PlatformGrok, platform) +} + func TestCompositeTargetPlatformAllowedRejectsWrongOrUnknownModel(t *testing.T) { gin.SetMode(gin.TestMode) diff --git a/backend/internal/handler/openai_gateway_handler.go b/backend/internal/handler/openai_gateway_handler.go index 9fd18f6cb8..f623f3dfff 100644 --- a/backend/internal/handler/openai_gateway_handler.go +++ b/backend/internal/handler/openai_gateway_handler.go @@ -147,6 +147,10 @@ func allowOpenAICompatibleMessagesDispatch(apiKey *service.APIKey) bool { return apiKey.Group.AllowMessagesDispatch } +func openAICompatibleMessagesTargetAllowed(c *gin.Context, apiKey *service.APIKey, model string) bool { + return compositeTargetPlatformAllowed(c, apiKey, model, service.PlatformOpenAI, service.PlatformGrok) +} + // NewOpenAIGatewayHandler creates a new OpenAIGatewayHandler func NewOpenAIGatewayHandler( gatewayService *service.OpenAIGatewayService, @@ -873,7 +877,7 @@ func (h *OpenAIGatewayHandler) Messages(c *gin.Context) { } reqModel := modelResult.String() ensureCompositeTargetPlatform(c, apiKey, reqModel) - if !compositeTargetPlatformAllowed(c, apiKey, reqModel, service.PlatformOpenAI) { + if !openAICompatibleMessagesTargetAllowed(c, apiKey, reqModel) { h.anthropicErrorResponse(c, http.StatusBadRequest, "invalid_request_error", "Model is not supported by this OpenAI-compatible endpoint for composite groups") return } diff --git a/backend/internal/server/routes/gateway_test.go b/backend/internal/server/routes/gateway_test.go index e37a446bef..fbf56959f3 100644 --- a/backend/internal/server/routes/gateway_test.go +++ b/backend/internal/server/routes/gateway_test.go @@ -193,6 +193,21 @@ func TestGatewayRoutesCompositeVideoStatusUsesGrokHandler(t *testing.T) { } } +func TestGatewayRoutesCompositeMessagesWithGrokModelUsesOpenAIGateway(t *testing.T) { + router := newGatewayRoutesTestRouter(service.PlatformComposite) + + req := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(`{"model":"grok-4.3","messages":[{"role":"user","content":"hi"}]}`)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + + router.ServeHTTP(w, req) + + require.NotEqual(t, http.StatusNotFound, w.Code) + require.NotContains(t, w.Body.String(), "not supported") + require.NotContains(t, w.Body.String(), "OpenAI-compatible endpoint") + require.NotContains(t, w.Body.String(), "composite groups") +} + func TestGatewayRoutesNonGrokVideosAreRejectedAtPlatformGate(t *testing.T) { router := newGatewayRoutesTestRouter(service.PlatformOpenAI)