diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 571b48b90..052d51508 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -8615,7 +8615,21 @@ func withMistralCacheKey(p provider.Provider, in http.Header, body []byte) []byt return body } sum := sha256.Sum256([]byte("mistral\x00" + id)) - return withFields(body, map[string]any{cacheKeyField: hex.EncodeToString(sum[:])}) + // written in ahead of the first member, so the client's own bytes + // (and the order its messages were sent in, for the cache) stay as sent + i := bytes.IndexByte(body, '{') + if i < 0 || len(bytes.TrimSpace(body[:i])) > 0 { + return body + } + rest := bytes.TrimLeft(body[i+1:], " \t\r\n") + sep := "," + if len(rest) == 0 || rest[0] == '}' { + sep = "" + } + out := make([]byte, 0, len(body)+96) + out = append(out, body[:i+1]...) + out = append(out, `"`+cacheKeyField+`":"`+hex.EncodeToString(sum[:])+`"`+sep...) + return append(out, body[i+1:]...) } // cacheKeyOf is the prompt cache key of the conversation req is a turn diff --git a/internal/gateway/mistral_extra_fields_test.go b/internal/gateway/mistral_extra_fields_test.go index 3924e847c..b804d6f3d 100644 --- a/internal/gateway/mistral_extra_fields_test.go +++ b/internal/gateway/mistral_extra_fields_test.go @@ -14,7 +14,7 @@ import ( // field it doesn't know: 422 extra_forbidden, every such field listed. func mistralRefuse(body []byte) (int, string) { known := []string{"model", "messages", "stream", "stream_options", "max_tokens", "temperature", "top_p", "tools", "tool_choice", - "parallel_tool_calls", "response_format", "stop", "random_seed", "presence_penalty", "frequency_penalty", "n", "reasoning_effort"} + "parallel_tool_calls", "response_format", "stop", "random_seed", "presence_penalty", "frequency_penalty", "n", "reasoning_effort", "prompt_cache_key"} var m map[string]json.RawMessage json.Unmarshal(body, &m) var detail []string