diff --git a/internal/translator/antigravity/claude/antigravity_claude_request.go b/internal/translator/antigravity/claude/antigravity_claude_request.go index 57352bbe99..5d32846945 100644 --- a/internal/translator/antigravity/claude/antigravity_claude_request.go +++ b/internal/translator/antigravity/claude/antigravity_claude_request.go @@ -657,10 +657,15 @@ func convertClaudeRequestToAntigravity(modelName string, inputRawJSON []byte, _ functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, "id", toolCallID) functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, "name", util.MapSanitizedFunctionName(functionNameMap, funcName)) + responsePath := "response.result" + if contentResult.Get("is_error").Type == gjson.True { + responsePath = "response.error" + } + responseData := "" if functionResponseResult.Type == gjson.String { responseData = functionResponseResult.String() - functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, "response.result", responseData) + functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, responsePath, responseData) } else if functionResponseResult.IsArray() { frResults := functionResponseResult.Array() nonImageItems := make([][]byte, 0, len(frResults)) @@ -685,11 +690,11 @@ func convertClaudeRequestToAntigravity(modelName string, inputRawJSON []byte, _ } if len(nonImageItems) == 1 { - functionResponseJSON = translatorcommon.SetGeminiFunctionResponseRaw(functionResponseJSON, "response.result", string(nonImageItems[0])) + functionResponseJSON = translatorcommon.SetGeminiFunctionResponseRaw(functionResponseJSON, responsePath, string(nonImageItems[0])) } else if len(nonImageItems) > 1 { - functionResponseJSON = translatorcommon.SetGeminiFunctionResponseRaw(functionResponseJSON, "response.result", string(translatorcommon.JoinRawArray(nonImageItems))) + functionResponseJSON = translatorcommon.SetGeminiFunctionResponseRaw(functionResponseJSON, responsePath, string(translatorcommon.JoinRawArray(nonImageItems))) } else { - functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, "response.result", "") + functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, responsePath, "") } // Place image data inside functionResponse.parts as inlineData @@ -712,16 +717,16 @@ func convertClaudeRequestToAntigravity(modelName string, inputRawJSON []byte, _ imagePartJSON := []byte(`{}`) imagePartJSON, _ = sjson.SetRawBytes(imagePartJSON, "inlineData", inlineDataJSON) functionResponseJSON, _ = sjson.SetRawBytes(functionResponseJSON, "parts", translatorcommon.JoinRawArray([][]byte{imagePartJSON})) - functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, "response.result", "") + functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, responsePath, "") } else { - functionResponseJSON = translatorcommon.SetGeminiFunctionResponseResult(functionResponseJSON, "response.result", functionResponseResult) + functionResponseJSON = translatorcommon.SetGeminiFunctionResponseResult(functionResponseJSON, responsePath, functionResponseResult) } } else if functionResponseResult.Raw != "" { - functionResponseJSON = translatorcommon.SetGeminiFunctionResponseResult(functionResponseJSON, "response.result", functionResponseResult) + functionResponseJSON = translatorcommon.SetGeminiFunctionResponseResult(functionResponseJSON, responsePath, functionResponseResult) } else { // Content field is missing entirely — .Raw is empty which // causes sjson.SetRaw to produce invalid JSON (e.g. "result":}). - functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, "response.result", "") + functionResponseJSON, _ = sjson.SetBytes(functionResponseJSON, responsePath, "") } partJSON := []byte(`{}`) diff --git a/internal/translator/antigravity/claude/antigravity_claude_request_test.go b/internal/translator/antigravity/claude/antigravity_claude_request_test.go index 4a60074f4a..bbb232f074 100644 --- a/internal/translator/antigravity/claude/antigravity_claude_request_test.go +++ b/internal/translator/antigravity/claude/antigravity_claude_request_test.go @@ -3924,3 +3924,108 @@ func TestConvertClaudeRequestToAntigravity_FunctionResponseJSONRef(t *testing.T) t.Fatalf("expected result to contain ref target, got %q", result.String()) } } + +func TestConvertClaudeRequestToAntigravity_PreservesToolResultErrors(t *testing.T) { + tests := []struct { + name string + fields string + wantResponse string + wantImage bool + }{ + { + name: "success_without_flag", + fields: `"content":"permission denied"`, + wantResponse: `{"result":"permission denied"}`, + }, + { + name: "explicit_success", + fields: `"is_error":false,"content":{"ok":true}`, + wantResponse: `{"result":{"ok":true}}`, + }, + { + name: "non_bool_is_error_string_treated_as_success", + fields: `"is_error":"true","content":"permission denied"`, + wantResponse: `{"result":"permission denied"}`, + }, + { + name: "non_bool_is_error_number_treated_as_success", + fields: `"is_error":1,"content":"permission denied"`, + wantResponse: `{"result":"permission denied"}`, + }, + { + name: "failed_string", + fields: `"is_error":true,"content":"permission denied"`, + wantResponse: `{"error":"permission denied"}`, + }, + { + name: "failed_structured_result", + fields: `"is_error":true,"content":{"code":"EACCES","retryable":false}`, + wantResponse: `{"error":{"code":"EACCES","retryable":false}}`, + }, + { + name: "failed_text_and_image", + fields: `"is_error":true,"content":[{"type":"text","text":"permission denied"},{"type":"image","source":{"type":"base64","media_type":"image/png","data":"aGVsbG8="}}]`, + wantResponse: `{"error":{"type":"text","text":"permission denied"}}`, + wantImage: true, + }, + { + name: "failed_image_only_array", + fields: `"is_error":true,"content":[{"type":"image","source":{"type":"base64","media_type":"image/png","data":"aGVsbG8="}}]`, + wantResponse: `{"error":""}`, + wantImage: true, + }, + { + name: "failed_single_image_object", + fields: `"is_error":true,"content":{"type":"image","source":{"type":"base64","media_type":"image/png","data":"aGVsbG8="}}`, + wantResponse: `{"error":""}`, + wantImage: true, + }, + { + name: "failed_multiple_blocks", + fields: `"is_error":true,"content":[{"type":"text","text":"first"},{"type":"text","text":"second"}]`, + wantResponse: `{"error":[{"type":"text","text":"first"},{"type":"text","text":"second"}]}`, + }, + { + name: "failed_without_content", + fields: `"is_error":true`, + wantResponse: `{"error":""}`, + }, + { + name: "failed_schema_ref_remains_opaque", + fields: `"is_error":true,"content":{"$ref":"#/definitions/Error"}`, + wantResponse: `{"error":"{\"$ref\":\"#/definitions/Error\"}"}`, + }, + } + + for _, stream := range []bool{false, true} { + for _, tt := range tests { + t.Run(fmt.Sprintf("stream=%t/%s", stream, tt.name), func(t *testing.T) { + input := []byte(fmt.Sprintf(`{"model":"claude-sonnet-4-5","messages":[ + {"role":"assistant","content":[{"type":"tool_use","id":"call_1","name":"Read","input":{}}]}, + {"role":"user","content":[{"type":"tool_result","tool_use_id":"call_1",%s}]} + ]}`, tt.fields)) + output, errTranslate := ConvertClaudeRequestToAntigravity("claude-sonnet-4-5", input, stream) + if errTranslate != nil { + t.Fatal(errTranslate) + } + if !gjson.ValidBytes(output) { + t.Fatalf("invalid translated JSON: %s", output) + } + response := gjson.GetBytes(output, "request.contents.1.parts.0.functionResponse") + if response.Get("id").String() != "call_1" || response.Get("name").String() != "Read" { + t.Fatalf("tool result lost its call identity: %s", response.Raw) + } + if got := response.Get("response").Raw; got != tt.wantResponse { + t.Fatalf("functionResponse.response = %s, want %s", got, tt.wantResponse) + } + if tt.wantImage { + if got := response.Get("parts.0.inlineData").Raw; got != `{"mimeType":"image/png","data":"aGVsbG8="}` { + t.Fatalf("functionResponse.parts[0].inlineData = %s", got) + } + } else if response.Get("parts").Exists() { + t.Fatalf("unexpected functionResponse.parts: %s", response.Get("parts").Raw) + } + }) + } + } +}