From 6c89d8d35cfc3fabd38fdfc8463203ee42ed1c2d Mon Sep 17 00:00:00 2001 From: fjl5 Date: Mon, 13 Apr 2026 17:30:49 +0800 Subject: [PATCH] =?UTF-8?q?add=20prompt=5Fcache=5Fkey=20injection=20for=20?= =?UTF-8?q?messages=E2=86=92responses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/openai_gateway_messages.go | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/internal/service/openai_gateway_messages.go b/backend/internal/service/openai_gateway_messages.go index a72b9bbf..2a0a72eb 100644 --- a/backend/internal/service/openai_gateway_messages.go +++ b/backend/internal/service/openai_gateway_messages.go @@ -121,6 +121,28 @@ func (s *OpenAIGatewayService) ForwardAsAnthropic( } } + // For API key accounts (including OpenAI-compatible upstream gateways), + // ensure promptCacheKey is also propagated via the request body so that + // upstreams using the Responses API can derive a stable session identifier + // from prompt_cache_key. This makes our Anthropic /v1/messages compatibility + // path behave more like a native Responses client. + if account.Type == AccountTypeAPIKey { + if trimmedKey := strings.TrimSpace(promptCacheKey); trimmedKey != "" { + var reqBody map[string]any + if err := json.Unmarshal(responsesBody, &reqBody); err != nil { + return nil, fmt.Errorf("unmarshal for prompt cache key injection: %w", err) + } + if existing, ok := reqBody["prompt_cache_key"].(string); !ok || strings.TrimSpace(existing) == "" { + reqBody["prompt_cache_key"] = trimmedKey + updated, err := json.Marshal(reqBody) + if err != nil { + return nil, fmt.Errorf("remarshal after prompt cache key injection: %w", err) + } + responsesBody = updated + } + } + } + // 5. Get access token token, _, err := s.GetAccessToken(ctx, account) if err != nil {