From 1d7abc95b89cc3feb46a954b08c6c9a8ce3beeb2 Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Fri, 19 Sep 2025 23:32:52 +0800 Subject: [PATCH] fix(gemini-web): ensure colon spacing in JSON output for compatibility --- internal/client/gemini-web/convert_ext.go | 39 ++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/internal/client/gemini-web/convert_ext.go b/internal/client/gemini-web/convert_ext.go index fb7cdde0..db5a1e50 100644 --- a/internal/client/gemini-web/convert_ext.go +++ b/internal/client/gemini-web/convert_ext.go @@ -1,6 +1,7 @@ package geminiwebapi import ( + "bytes" "encoding/json" "fmt" "math" @@ -137,5 +138,41 @@ func ConvertOutputToGemini(output *ModelOutput, modelName string, promptText str if err != nil { return nil, fmt.Errorf("failed to marshal gemini response: %w", err) } - return b, nil + return ensureColonSpacing(b), nil +} + +// ensureColonSpacing inserts a single space after JSON key-value colons while +// leaving string content untouched. This matches the relaxed formatting used by +// Gemini responses and keeps downstream text-processing tools compatible with +// the proxy output. +func ensureColonSpacing(b []byte) []byte { + if len(b) == 0 { + return b + } + var out bytes.Buffer + out.Grow(len(b) + len(b)/8) + inString := false + escaped := false + for i := 0; i < len(b); i++ { + ch := b[i] + out.WriteByte(ch) + if escaped { + escaped = false + continue + } + switch ch { + case '\\': + escaped = true + case '"': + inString = !inString + case ':': + if !inString && i+1 < len(b) { + next := b[i+1] + if next != ' ' && next != '\n' && next != '\r' && next != '\t' { + out.WriteByte(' ') + } + } + } + } + return out.Bytes() }