refactor(translator): enhance signature handling in Claude and Gemini requests, streamline cache usage and remove unnecessary tests

This commit is contained in:
hkfires
2026-01-21 20:21:49 +08:00
parent d9c6317c84
commit c8884f5e25
5 changed files with 52 additions and 90 deletions

View File

@@ -99,44 +99,36 @@ func ConvertGeminiRequestToAntigravity(modelName string, inputRawJSON []byte, _
}
// Gemini-specific handling for non-Claude models:
// - Remove thinking parts entirely.
// - Add skip_thought_signature_validator to functionCall parts so upstream can bypass signature validation.
// - Also mark thinking parts with the same sentinel when present (we keep the parts; we only annotate them).
if !strings.Contains(modelName, "claude") {
const skipSentinel = "skip_thought_signature_validator"
gjson.GetBytes(rawJSON, "request.contents").ForEach(func(contentIdx, content gjson.Result) bool {
if content.Get("role").String() != "model" {
return true
}
partsResult := content.Get("parts")
if !partsResult.IsArray() {
return true
}
parts := partsResult.Array()
newParts := make([]interface{}, 0, len(parts))
for _, part := range parts {
if part.Get("thought").Bool() {
continue
}
partRaw := part.Raw
if part.Get("functionCall").Exists() {
existingSig := part.Get("thoughtSignature").String()
if existingSig == "" || len(existingSig) < 50 {
updatedPart, errSet := sjson.Set(partRaw, "thoughtSignature", skipSentinel)
if errSet != nil {
log.WithError(errSet).Debug("failed to set thoughtSignature on functionCall part")
} else {
partRaw = updatedPart
if content.Get("role").String() == "model" {
// First pass: collect indices of thinking parts to mark with skip sentinel
var thinkingIndicesToSkipSignature []int64
content.Get("parts").ForEach(func(partIdx, part gjson.Result) bool {
// Collect indices of thinking blocks to mark with skip sentinel
if part.Get("thought").Bool() {
thinkingIndicesToSkipSignature = append(thinkingIndicesToSkipSignature, partIdx.Int())
}
// Add skip sentinel to functionCall parts
if part.Get("functionCall").Exists() {
existingSig := part.Get("thoughtSignature").String()
if existingSig == "" || len(existingSig) < 50 {
rawJSON, _ = sjson.SetBytes(rawJSON, fmt.Sprintf("request.contents.%d.parts.%d.thoughtSignature", contentIdx.Int(), partIdx.Int()), skipSentinel)
}
}
return true
})
// Add skip_thought_signature_validator sentinel to thinking blocks in reverse order to preserve indices
for i := len(thinkingIndicesToSkipSignature) - 1; i >= 0; i-- {
idx := thinkingIndicesToSkipSignature[i]
rawJSON, _ = sjson.SetBytes(rawJSON, fmt.Sprintf("request.contents.%d.parts.%d.thoughtSignature", contentIdx.Int(), idx), skipSentinel)
}
newParts = append(newParts, gjson.Parse(partRaw).Value())
}
rawJSON, _ = sjson.SetBytes(rawJSON, fmt.Sprintf("request.contents.%d.parts", contentIdx.Int()), newParts)
return true
})
}