mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-30 22:24:55 +08:00
- Decode numeric protobuf Schema.Type enum values (1..7) to lowercase JSON Schema type strings via *ojson.Doc in NormalizeJSONSchemaNumericConstraints while preserving literals and special property keys. - Normalize schemas prior to nested wrapper cleaning, Claude 5.5 structured output prompt fallback, Interactions response_format emission, and snake_case Gemini pass-through schema fields. - Add regression tests covering schema cleaners, Antigravity executor paths, and Gemini-ingress translators. Closes: #8032
91 lines
3.3 KiB
Go
91 lines
3.3 KiB
Go
package helps
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/MoeForge/moego/ojson"
|
|
"github.com/MoeForge/moego/regexp"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/thinking"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
|
|
)
|
|
|
|
var antigravityClaude55Model = regexp.MustCompile(`^claude-(opus|sonnet)-5-5(?:-(low|medium|high|max|reasoning))?(?:-[0-9]{8})?(?:\[[^\[\]]+\])?$`)
|
|
|
|
// PrepareAntigravityClaudeStructuredOutput replaces native output schemas with
|
|
// best-effort prompt instructions for Claude 5.5, which rejects forced tool use.
|
|
// Call after sensitive-word obfuscation and before schema cleaning or payload rules.
|
|
// The boolean tells the request builder not to restore VALIDATED tool mode.
|
|
func PrepareAntigravityClaudeStructuredOutput(model string, payload []byte) ([]byte, bool) {
|
|
if len(payload) == 0 {
|
|
return payload, false
|
|
}
|
|
doc := ojson.NewDoc(payload)
|
|
if !PrepareAntigravityClaudeStructuredOutputDoc(model, doc) {
|
|
return payload, false
|
|
}
|
|
return doc.Bytes(), true
|
|
}
|
|
|
|
// PrepareAntigravityClaudeStructuredOutputDoc replaces native output schemas with
|
|
// best-effort prompt instructions in place on doc for Claude 5.5.
|
|
func PrepareAntigravityClaudeStructuredOutputDoc(model string, doc *ojson.Doc) bool {
|
|
if doc == nil || doc.Len() == 0 {
|
|
return false
|
|
}
|
|
model = strings.ToLower(thinking.ParseSuffix(strings.TrimSpace(model)).ModelName)
|
|
if !antigravityClaude55Model.MatchString(model) {
|
|
return false
|
|
}
|
|
|
|
containers := []string{"request.generationConfig", "request.generation_config"}
|
|
schemaKeys := []string{"responseSchema", "responseJsonSchema", "response_schema", "response_json_schema"}
|
|
var schema ojson.Result
|
|
for _, container := range containers {
|
|
for _, key := range schemaKeys {
|
|
if candidate := doc.Get(container + "." + key); candidate.IsObject() {
|
|
schema = candidate
|
|
break
|
|
}
|
|
}
|
|
if schema.Exists() {
|
|
break
|
|
}
|
|
}
|
|
if !schema.Exists() {
|
|
return false
|
|
}
|
|
|
|
// Normalize protobuf-encoded numeric constraints/types before embedding as JSON Schema.
|
|
formatDoc := ojson.NewDocString(`{"type":"json_schema"}`)
|
|
_ = formatDoc.SetRaw("schema", util.NormalizeJSONSchemaNumericConstraints(schema.Raw))
|
|
format := ojson.Parse(formatDoc.String())
|
|
instruction := common.BuildClaudeStructuredOutputInstruction(format)
|
|
systemPath := "request.systemInstruction"
|
|
if !doc.Get(systemPath).Exists() && doc.Get("request.system_instruction").Exists() {
|
|
systemPath = "request.system_instruction"
|
|
}
|
|
if errSet := doc.Set(systemPath+".parts.-1.text", instruction); errSet != nil {
|
|
return false
|
|
}
|
|
|
|
// Remove only native output leaves; other config fields and aliases stay put.
|
|
for _, container := range containers {
|
|
for _, key := range append(schemaKeys, "responseMimeType", "response_mime_type") {
|
|
_ = util.DeleteJSONDocIfExists(doc, container+"."+key)
|
|
}
|
|
}
|
|
for _, container := range []string{"request.toolConfig", "request.tool_config"} {
|
|
for _, key := range []string{"functionCallingConfig", "function_calling_config"} {
|
|
path := container + "." + key
|
|
switch strings.ToUpper(doc.Get(path + ".mode").String()) {
|
|
case "ANY", "VALIDATED":
|
|
_ = doc.Set(path+".mode", "AUTO")
|
|
_ = util.DeleteJSONDocIfExists(doc, path+".allowedFunctionNames")
|
|
_ = util.DeleteJSONDocIfExists(doc, path+".allowed_function_names")
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|