mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 12:20:52 +08:00
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:
@@ -152,10 +152,17 @@ outLoop:
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
template := `{"project":"","request":{},"model":""}`
|
template := ""
|
||||||
template, _ = sjson.SetRaw(template, "request", string(rawJson))
|
parsed := gjson.Parse(string(rawJson))
|
||||||
template, _ = sjson.Set(template, "model", gjson.Get(template, "request.model").String())
|
contents := parsed.Get("request.contents")
|
||||||
template, _ = sjson.Delete(template, "request.model")
|
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)
|
template, errFixCLIToolResponse := translator.FixCLIToolResponse(template)
|
||||||
if errFixCLIToolResponse != nil {
|
if errFixCLIToolResponse != nil {
|
||||||
@@ -233,8 +240,10 @@ outLoop:
|
|||||||
case err, okError := <-errChan:
|
case err, okError := <-errChan:
|
||||||
if okError {
|
if okError {
|
||||||
if err.StatusCode == 429 && h.cfg.QuotaExceeded.SwitchProject {
|
if err.StatusCode == 429 && h.cfg.QuotaExceeded.SwitchProject {
|
||||||
|
log.Debugf("quota exceeded, switch client")
|
||||||
continue outLoop
|
continue outLoop
|
||||||
} else {
|
} else {
|
||||||
|
log.Debugf("error code :%d, error: %v", err.StatusCode, err.Error.Error())
|
||||||
c.Status(err.StatusCode)
|
c.Status(err.StatusCode)
|
||||||
_, _ = fmt.Fprint(c.Writer, err.Error.Error())
|
_, _ = fmt.Fprint(c.Writer, err.Error.Error())
|
||||||
flusher.Flush()
|
flusher.Flush()
|
||||||
@@ -342,10 +351,17 @@ func (h *APIHandlers) geminiGenerateContent(c *gin.Context, rawJson []byte) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
template := `{"project":"","request":{},"model":""}`
|
template := ""
|
||||||
template, _ = sjson.SetRaw(template, "request", string(rawJson))
|
parsed := gjson.Parse(string(rawJson))
|
||||||
template, _ = sjson.Set(template, "model", gjson.Get(template, "request.model").String())
|
contents := parsed.Get("request.contents")
|
||||||
template, _ = sjson.Delete(template, "request.model")
|
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)
|
template, errFixCLIToolResponse := translator.FixCLIToolResponse(template)
|
||||||
if errFixCLIToolResponse != nil {
|
if errFixCLIToolResponse != nil {
|
||||||
|
|||||||
@@ -242,6 +242,7 @@ func FixCLIToolResponse(input string) (string, error) {
|
|||||||
// Extract the contents array which contains the conversation messages
|
// Extract the contents array which contains the conversation messages
|
||||||
contents := parsed.Get("request.contents")
|
contents := parsed.Get("request.contents")
|
||||||
if !contents.Exists() {
|
if !contents.Exists() {
|
||||||
|
// log.Debugf(input)
|
||||||
return input, fmt.Errorf("contents not found in input")
|
return input, fmt.Errorf("contents not found in input")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user