From 00f33f5f3ab617541b8331b9de5c2d6740962adb Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 4 Aug 2025 02:51:00 +0800 Subject: [PATCH] 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. --- internal/api/gemini-handlers.go | 32 ++++++++++++++++++++++-------- internal/api/translator/request.go | 1 + 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/internal/api/gemini-handlers.go b/internal/api/gemini-handlers.go index 1ae70c7d..0da2ca6d 100644 --- a/internal/api/gemini-handlers.go +++ b/internal/api/gemini-handlers.go @@ -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 { diff --git a/internal/api/translator/request.go b/internal/api/translator/request.go index 73bbbcf7..bd6720ba 100644 --- a/internal/api/translator/request.go +++ b/internal/api/translator/request.go @@ -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") }