mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-09-03 06:35:00 +08:00
* fix(executor): prepend empty user turn for model-first requests targeting Gemini/Antigravity (#4959) When forwarding sliced conversation histories or tool calls across OpenAI Responses, OpenAI Chat Completions, Claude Messages, and native Gemini, native Gemini and Antigravity Gemini endpoints require that conversation contents begin with a user turn. Normalize leading turns at the executor boundary rather than the translator layer: - Prepend an empty user turn ({"role":"user","parts":[{"text":""}]}) for Gemini, Gemini Vertex, AI Studio, and Antigravity Gemini generation and CountTokens requests if the first turn is 'model'. - Keep Antigravity Claude requests untouched to avoid adapter 400 errors. - Ensure normalization runs after payload rules so payload index overrides target the original turns. - Use no-copy GJSON inspection to keep overhead zero on valid user-first requests. * fix(executor): inject Antigravity leading user after reasoning replay (#4959) Replay can insert a model functionCall at contents[0] for sliced tool-result history. Run the empty-user prepend on the final requestPayload, after sanitize and prepareAntigravityGeminiReasoningReplayPayload.
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package helps
|
|
|
|
import (
|
|
translatorcommon "github.com/router-for-me/CLIProxyAPI/v7/internal/translator/common"
|
|
"github.com/router-for-me/CLIProxyAPI/v7/internal/util"
|
|
"github.com/tidwall/gjson"
|
|
"github.com/tidwall/sjson"
|
|
)
|
|
|
|
var emptyGeminiUserTurnJSON = []byte(`{"role":"user","parts":[{"text":""}]}`)
|
|
|
|
// EnsureGeminiLeadingUserContent ensures that the contents array at the given path
|
|
// starts with a user turn when sending to Gemini/Antigravity upstreams.
|
|
func EnsureGeminiLeadingUserContent(payload []byte, path string) []byte {
|
|
firstRole := gjson.GetBytes(payload, path+".0.role")
|
|
if firstRole.String() != "model" {
|
|
return payload
|
|
}
|
|
contents := util.GetGJSONBytesNoCopy(payload, path)
|
|
if !contents.IsArray() {
|
|
return payload
|
|
}
|
|
contentArray := contents.Array()
|
|
if len(contentArray) == 0 {
|
|
return payload
|
|
}
|
|
|
|
contentItems := make([][]byte, 0, len(contentArray)+1)
|
|
contentItems = append(contentItems, emptyGeminiUserTurnJSON)
|
|
for _, content := range contentArray {
|
|
contentItems = append(contentItems, []byte(content.Raw))
|
|
}
|
|
|
|
out, errSet := sjson.SetRawBytes(payload, path, translatorcommon.JoinRawArray(contentItems))
|
|
if errSet != nil {
|
|
return payload
|
|
}
|
|
return out
|
|
}
|