Enhance Gemini request handling for contents support and improve error logging

- Added conditional logic to process `contents` in Gemini request templates, ensuring fallback behavior.
- Introduced detailed debug logs for quota errors and request issues.
- Updated handling of `rawJson` to construct templates more dynamically.
This commit is contained in:
Luis Pater
2025-08-04 02:51:00 +08:00
parent 3c4dc07980
commit 00f33f5f3a
2 changed files with 25 additions and 8 deletions

View File

@@ -152,10 +152,17 @@ outLoop:
return
}
template := `{"project":"","request":{},"model":""}`
template, _ = sjson.SetRaw(template, "request", string(rawJson))
template, _ = sjson.Set(template, "model", gjson.Get(template, "request.model").String())
template, _ = sjson.Delete(template, "request.model")
template := ""
parsed := gjson.Parse(string(rawJson))
contents := parsed.Get("request.contents")
if contents.Exists() {
template = string(rawJson)
} else {
template = `{"project":"","request":{},"model":""}`
template, _ = sjson.SetRaw(template, "request", string(rawJson))
template, _ = sjson.Set(template, "model", gjson.Get(template, "request.model").String())
template, _ = sjson.Delete(template, "request.model")
}
template, errFixCLIToolResponse := translator.FixCLIToolResponse(template)
if errFixCLIToolResponse != nil {
@@ -233,8 +240,10 @@ outLoop:
case err, okError := <-errChan:
if okError {
if err.StatusCode == 429 && h.cfg.QuotaExceeded.SwitchProject {
log.Debugf("quota exceeded, switch client")
continue outLoop
} else {
log.Debugf("error code :%d, error: %v", err.StatusCode, err.Error.Error())
c.Status(err.StatusCode)
_, _ = fmt.Fprint(c.Writer, err.Error.Error())
flusher.Flush()
@@ -342,10 +351,17 @@ func (h *APIHandlers) geminiGenerateContent(c *gin.Context, rawJson []byte) {
return
}
template := `{"project":"","request":{},"model":""}`
template, _ = sjson.SetRaw(template, "request", string(rawJson))
template, _ = sjson.Set(template, "model", gjson.Get(template, "request.model").String())
template, _ = sjson.Delete(template, "request.model")
template := ""
parsed := gjson.Parse(string(rawJson))
contents := parsed.Get("request.contents")
if contents.Exists() {
template = string(rawJson)
} else {
template = `{"project":"","request":{},"model":""}`
template, _ = sjson.SetRaw(template, "request", string(rawJson))
template, _ = sjson.Set(template, "model", gjson.Get(template, "request.model").String())
template, _ = sjson.Delete(template, "request.model")
}
template, errFixCLIToolResponse := translator.FixCLIToolResponse(template)
if errFixCLIToolResponse != nil {

View File

@@ -242,6 +242,7 @@ func FixCLIToolResponse(input string) (string, error) {
// Extract the contents array which contains the conversation messages
contents := parsed.Get("request.contents")
if !contents.Exists() {
// log.Debugf(input)
return input, fmt.Errorf("contents not found in input")
}