mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
Add Gemini-to-Gemini request normalization and passthrough support
- Introduced a `ConvertGeminiRequestToGemini` function to normalize Gemini v1beta requests by ensuring valid or default roles. - Added passthrough response handlers for both streamed and non-streamed Gemini responses. - Registered translators for Gemini-to-Gemini traffic in the initialization process. - Updated `gemini-cli` request normalization to align with the new Gemini translator logic.
This commit is contained in:
@@ -49,6 +49,33 @@ func ConvertGeminiRequestToGeminiCLI(_ string, rawJSON []byte, _ bool) []byte {
|
||||
}
|
||||
rawJSON = []byte(template)
|
||||
|
||||
// Normalize roles in request.contents: default to valid values if missing/invalid
|
||||
contents := gjson.GetBytes(rawJSON, "request.contents")
|
||||
if contents.Exists() {
|
||||
prevRole := ""
|
||||
idx := 0
|
||||
contents.ForEach(func(_ gjson.Result, value gjson.Result) bool {
|
||||
role := value.Get("role").String()
|
||||
valid := role == "user" || role == "model"
|
||||
if role == "" || !valid {
|
||||
var newRole string
|
||||
if prevRole == "" {
|
||||
newRole = "user"
|
||||
} else if prevRole == "user" {
|
||||
newRole = "model"
|
||||
} else {
|
||||
newRole = "user"
|
||||
}
|
||||
path := fmt.Sprintf("request.contents.%d.role", idx)
|
||||
rawJSON, _ = sjson.SetBytes(rawJSON, path, newRole)
|
||||
role = newRole
|
||||
}
|
||||
prevRole = role
|
||||
idx++
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
return rawJSON
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user