From ecf49d574b8145bfba0dfddaaf2049920336fbd8 Mon Sep 17 00:00:00 2001 From: sukakcoding <102297965+sukakcoding@users.noreply.github.com> Date: Mon, 15 Dec 2025 00:59:46 +0800 Subject: [PATCH] fix: handle malformed json in function response parsing --- .../gemini/antigravity_gemini_request.go | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/internal/translator/antigravity/gemini/antigravity_gemini_request.go b/internal/translator/antigravity/gemini/antigravity_gemini_request.go index 30bc3830..9f5ff571 100644 --- a/internal/translator/antigravity/gemini/antigravity_gemini_request.go +++ b/internal/translator/antigravity/gemini/antigravity_gemini_request.go @@ -183,7 +183,27 @@ func fixCLIToolResponse(input string) (string, error) { var responseMap map[string]interface{} errUnmarshal := json.Unmarshal([]byte(response.Raw), &responseMap) if errUnmarshal != nil { - log.Warnf("failed to unmarshal function response: %v\n", errUnmarshal) + log.Debugf("unmarshal function response failed, using fallback: %v", errUnmarshal) + funcResp := response.Get("functionResponse") + if funcResp.Exists() { + fr := map[string]interface{}{ + "name": funcResp.Get("name").String(), + "response": map[string]interface{}{ + "result": funcResp.Get("response").String(), + }, + } + if id := funcResp.Get("id").String(); id != "" { + fr["id"] = id + } + responseParts = append(responseParts, map[string]interface{}{"functionResponse": fr}) + continue + } + responseParts = append(responseParts, map[string]interface{}{ + "functionResponse": map[string]interface{}{ + "name": "unknown", + "response": map[string]interface{}{"result": response.String()}, + }, + }) continue } responseParts = append(responseParts, responseMap) @@ -268,7 +288,27 @@ func fixCLIToolResponse(input string) (string, error) { var responseMap map[string]interface{} errUnmarshal := json.Unmarshal([]byte(response.Raw), &responseMap) if errUnmarshal != nil { - log.Warnf("failed to unmarshal function response: %v\n", errUnmarshal) + log.Debugf("unmarshal function response failed, using fallback: %v", errUnmarshal) + funcResp := response.Get("functionResponse") + if funcResp.Exists() { + fr := map[string]interface{}{ + "name": funcResp.Get("name").String(), + "response": map[string]interface{}{ + "result": funcResp.Get("response").String(), + }, + } + if id := funcResp.Get("id").String(); id != "" { + fr["id"] = id + } + responseParts = append(responseParts, map[string]interface{}{"functionResponse": fr}) + continue + } + responseParts = append(responseParts, map[string]interface{}{ + "functionResponse": map[string]interface{}{ + "name": "unknown", + "response": map[string]interface{}{"result": response.String()}, + }, + }) continue } responseParts = append(responseParts, responseMap)