mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
feature: Improves schema flattening and tool use handling
Updates schema flattening logic to handle multiple non-null types, providing a more descriptive "Accepts" hint. Removes redundant tracking of the current tool name in `Params` as it's no longer needed for streaming limits, simplifying the structure.
This commit is contained in:
@@ -26,7 +26,6 @@ type Params struct {
|
|||||||
ResponseType int
|
ResponseType int
|
||||||
ResponseIndex int
|
ResponseIndex int
|
||||||
HasContent bool // Tracks whether any content (text, thinking, or tool use) has been output
|
HasContent bool // Tracks whether any content (text, thinking, or tool use) has been output
|
||||||
CurrentToolName string // Tracks the current function name for streaming limits
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// toolUseIDCounter provides a process-wide unique counter for tool use identifiers.
|
// toolUseIDCounter provides a process-wide unique counter for tool use identifiers.
|
||||||
@@ -234,7 +233,6 @@ func ConvertGeminiResponseToClaude(_ context.Context, _ string, originalRequestR
|
|||||||
}
|
}
|
||||||
(*param).(*Params).ResponseType = 3
|
(*param).(*Params).ResponseType = 3
|
||||||
(*param).(*Params).HasContent = true
|
(*param).(*Params).HasContent = true
|
||||||
(*param).(*Params).CurrentToolName = fcName
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,21 +222,30 @@ func flattenTypeArrays(jsonStr string) string {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
hasNull, firstType := false, ""
|
hasNull := false
|
||||||
|
var nonNullTypes []string
|
||||||
for _, item := range res.Array() {
|
for _, item := range res.Array() {
|
||||||
s := item.String()
|
s := item.String()
|
||||||
if s == "null" {
|
if s == "null" {
|
||||||
hasNull = true
|
hasNull = true
|
||||||
} else if firstType == "" {
|
} else if s != "" {
|
||||||
firstType = s
|
nonNullTypes = append(nonNullTypes, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if firstType == "" {
|
|
||||||
firstType = "string"
|
firstType := "string"
|
||||||
|
if len(nonNullTypes) > 0 {
|
||||||
|
firstType = nonNullTypes[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonStr, _ = sjson.Set(jsonStr, p, firstType)
|
jsonStr, _ = sjson.Set(jsonStr, p, firstType)
|
||||||
|
|
||||||
|
parentPath := trimSuffix(p, ".type")
|
||||||
|
if len(nonNullTypes) > 1 {
|
||||||
|
hint := "Accepts: " + strings.Join(nonNullTypes, " | ")
|
||||||
|
jsonStr = appendHint(jsonStr, parentPath, hint)
|
||||||
|
}
|
||||||
|
|
||||||
if hasNull {
|
if hasNull {
|
||||||
parts := strings.Split(p, ".")
|
parts := strings.Split(p, ".")
|
||||||
if len(parts) >= 3 && parts[len(parts)-3] == "properties" {
|
if len(parts) >= 3 && parts[len(parts)-3] == "properties" {
|
||||||
|
|||||||
@@ -451,6 +451,26 @@ func TestCleanJSONSchemaForGemini_SingleEnumNoHint(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCleanJSONSchemaForGemini_MultipleNonNullTypes(t *testing.T) {
|
||||||
|
input := `{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"value": {
|
||||||
|
"type": ["string", "integer", "boolean"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
result := CleanJSONSchemaForGemini(input)
|
||||||
|
|
||||||
|
if !strings.Contains(result, "Accepts:") {
|
||||||
|
t.Errorf("Expected multiple types hint, got: %s", result)
|
||||||
|
}
|
||||||
|
if !strings.Contains(result, "string") || !strings.Contains(result, "integer") || !strings.Contains(result, "boolean") {
|
||||||
|
t.Errorf("Expected all types in hint, got: %s", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func compareJSON(t *testing.T, expectedJSON, actualJSON string) {
|
func compareJSON(t *testing.T, expectedJSON, actualJSON string) {
|
||||||
var expMap, actMap map[string]interface{}
|
var expMap, actMap map[string]interface{}
|
||||||
errExp := json.Unmarshal([]byte(expectedJSON), &expMap)
|
errExp := json.Unmarshal([]byte(expectedJSON), &expMap)
|
||||||
|
|||||||
Reference in New Issue
Block a user