mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
fix(gemini-web): ensure colon spacing in JSON output for compatibility
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package geminiwebapi
|
package geminiwebapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
@@ -137,5 +138,41 @@ func ConvertOutputToGemini(output *ModelOutput, modelName string, promptText str
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to marshal gemini response: %w", err)
|
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()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user