fix(translator): validate complete JSON before using SetRaw for tool output

Tool outputs like "[1,2,3]\n\n[Process exited with code 0]" were being
partially parsed by gjson.Parse() which would extract just "[1,2,3]" and
lose the trailing text. This caused invalid JSON in the Gemini request.

Fix: Use gjson.Valid() to verify the ENTIRE string is valid JSON before
treating it as raw JSON. If validation fails, treat as a string with
proper escaping via sjson.Set().

Co-Authored-By: Claude (gemini-claude-opus-4-5-thinking) <noreply@anthropic.com>
This commit is contained in:
RCD
2026-01-30 17:19:56 +07:00
co-authored by Claude
parent a7b7856135
commit ae7946402a
@@ -293,8 +293,11 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte
// Set the raw JSON output directly (preserves string encoding)
if outputRaw != "" && outputRaw != "null" {
output := gjson.Parse(outputRaw)
if output.Type == gjson.JSON {
// Only treat as JSON if the ENTIRE string is valid JSON.
// gjson.Parse will parse partial JSON (e.g., "[1,2,3]\n\nmore text" parses as [1,2,3]),
// so we use gjson.Valid to verify the complete string is valid JSON.
if gjson.Valid(outputRaw) {
output := gjson.Parse(outputRaw)
functionResponse, _ = sjson.SetRaw(functionResponse, "functionResponse.response.result", output.Raw)
} else {
functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.result", outputRaw)