fix(claude translator): guard tool schema properties

This commit is contained in:
lollipopkit🏳️‍⚧️
2025-11-15 19:14:13 +08:00
parent 772fa69515
commit c73b3fa43b

View File

@@ -204,7 +204,7 @@ func ConvertClaudeRequestToCodex(modelName string, inputRawJSON []byte, _ bool)
} }
tool, _ = sjson.Set(tool, "name", name) tool, _ = sjson.Set(tool, "name", name)
} }
tool, _ = sjson.SetRaw(tool, "parameters", toolResult.Get("input_schema").Raw) tool, _ = sjson.SetRaw(tool, "parameters", normalizeToolParameters(toolResult.Get("input_schema").Raw))
tool, _ = sjson.Delete(tool, "input_schema") tool, _ = sjson.Delete(tool, "input_schema")
tool, _ = sjson.Delete(tool, "parameters.$schema") tool, _ = sjson.Delete(tool, "parameters.$schema")
tool, _ = sjson.Set(tool, "strict", false) tool, _ = sjson.Set(tool, "strict", false)
@@ -334,3 +334,22 @@ func buildReverseMapFromClaudeOriginalToShort(original []byte) map[string]string
} }
return m return m
} }
// normalizeToolParameters ensures object schemas contain at least an empty properties map.
func normalizeToolParameters(raw string) string {
raw = strings.TrimSpace(raw)
if raw == "" || raw == "null" || !gjson.Valid(raw) {
return `{"type":"object","properties":{}}`
}
schema := raw
result := gjson.Parse(raw)
schemaType := result.Get("type").String()
if schemaType == "" {
schema, _ = sjson.Set(schema, "type", "object")
schemaType = "object"
}
if schemaType == "object" && !result.Get("properties").Exists() {
schema, _ = sjson.SetRaw(schema, "properties", `{}`)
}
return schema
}