From 35f5a3a48218bdcd99718a9da02503f7079804af Mon Sep 17 00:00:00 2001 From: Chenyme <118253778+chenyme@users.noreply.github.com> Date: Fri, 24 Jul 2026 16:26:33 +0800 Subject: [PATCH] fix: enhance nullable function parameter handling and add tests for local and external references --- .../cli/responses_tool_declarations.go | 50 +++++++++++++++-- .../provider/cli/responses_tools_test.go | 53 +++++++++++++++++++ 2 files changed, 98 insertions(+), 5 deletions(-) diff --git a/backend/internal/infra/provider/cli/responses_tool_declarations.go b/backend/internal/infra/provider/cli/responses_tool_declarations.go index 081cbe2d..68574873 100644 --- a/backend/internal/infra/provider/cli/responses_tool_declarations.go +++ b/backend/internal/infra/provider/cli/responses_tool_declarations.go @@ -326,7 +326,7 @@ func normalizeBuildFunctionParametersRoot(value any, param string) (any, bool, e } if len(filtered) == 1 { branch, ok := filtered[0].(map[string]any) - if !ok || !isObjectRootSchema(branch) { + if !ok || !isObjectRootSchema(branch, normalized, nil) { return nil, false, invalidBuildFunctionParametersRoot(param) } if len(normalized) == 1 { @@ -340,7 +340,7 @@ func normalizeBuildFunctionParametersRoot(value any, param string) (any, bool, e } for _, rawBranch := range filtered { branch, ok := rawBranch.(map[string]any) - if !ok || !isObjectRootSchema(branch) { + if !ok || !isObjectRootSchema(branch, normalized, nil) { return nil, false, invalidBuildFunctionParametersRoot(param) } } @@ -384,7 +384,7 @@ func isNullOnlySchema(value any) bool { return true } -func isObjectRootSchema(schema map[string]any) bool { +func isObjectRootSchema(schema, root map[string]any, visited map[string]struct{}) bool { rawType, hasType := schema["type"] if rawType == "object" { return true @@ -400,8 +400,48 @@ func isObjectRootSchema(schema map[string]any) bool { if hasType { return false } - _, hasProperties := schema["properties"] - return hasProperties + if _, hasProperties := schema["properties"]; hasProperties { + return true + } + ref, ok := schema["$ref"].(string) + if !ok { + return false + } + if visited == nil { + visited = make(map[string]struct{}) + } + if _, seen := visited[ref]; seen { + return false + } + resolved, ok := resolveLocalSchemaRef(root, ref) + if !ok { + return false + } + visited[ref] = struct{}{} + return isObjectRootSchema(resolved, root, visited) +} + +func resolveLocalSchemaRef(root map[string]any, ref string) (map[string]any, bool) { + if ref == "#" { + return root, true + } + if !strings.HasPrefix(ref, "#/") { + return nil, false + } + var current any = root + for _, encoded := range strings.Split(strings.TrimPrefix(ref, "#/"), "/") { + segment := strings.ReplaceAll(strings.ReplaceAll(encoded, "~1", "/"), "~0", "~") + object, ok := current.(map[string]any) + if !ok { + return nil, false + } + current, ok = object[segment] + if !ok { + return nil, false + } + } + resolved, ok := current.(map[string]any) + return resolved, ok } func invalidBuildFunctionParametersRoot(param string) error { diff --git a/backend/internal/infra/provider/cli/responses_tools_test.go b/backend/internal/infra/provider/cli/responses_tools_test.go index e2852728..4bb7a27c 100644 --- a/backend/internal/infra/provider/cli/responses_tools_test.go +++ b/backend/internal/infra/provider/cli/responses_tools_test.go @@ -313,6 +313,59 @@ func TestNormalizeResponsesRequestRejectsNullableNonObjectFunctionRoot(t *testin } } +func TestNormalizeResponsesRequestRemovesNullableLocalRefFunctionRoot(t *testing.T) { + normalized, compatibility, err := normalizeResponsesRequest([]byte(`{ + "model":"public","input":"hello", + "tools":[{"type":"function","name":"lookup","parameters":{ + "$defs":{"Args":{ + "type":"object", + "properties":{"query":{"type":"string"}}, + "required":["query"] + }}, + "anyOf":[{"$ref":"#/$defs/Args"},{"type":"null"}] + }}] + }`), "grok-4.5") + if err != nil { + t.Fatal(err) + } + if compatibility == nil || !strings.Contains(compatibility.warningHeader(), "function_parameters_nullable_root_normalized") { + t.Fatalf("compatibility = %#v", compatibility) + } + var payload map[string]any + if err := json.Unmarshal(normalized, &payload); err != nil { + t.Fatal(err) + } + parameters := payload["tools"].([]any)[0].(map[string]any)["parameters"].(map[string]any) + if parameters["type"] != "object" { + t.Fatalf("upstream parameters = %#v", parameters) + } + branches := parameters["anyOf"].([]any) + if len(branches) != 1 || branches[0].(map[string]any)["$ref"] != "#/$defs/Args" { + t.Fatalf("upstream branches = %#v", branches) + } + visibleParameters := compatibility.visibleTools[0].(map[string]any)["parameters"].(map[string]any) + visibleBranches := visibleParameters["anyOf"].([]any) + if len(visibleBranches) != 2 || visibleParameters["type"] != nil { + t.Fatalf("visible parameters were mutated = %#v", visibleParameters) + } +} + +func TestNormalizeResponsesRequestRejectsNullableExternalRefFunctionRoot(t *testing.T) { + _, _, err := normalizeResponsesRequest([]byte(`{ + "model":"public","input":"hello", + "tools":[{"type":"function","name":"lookup","parameters":{ + "anyOf":[ + {"$ref":"https://example.com/schema.json#/$defs/Args"}, + {"type":"null"} + ] + }}] + }`), "grok-4.5") + requestErr, ok := err.(*responsesRequestError) + if !ok || requestErr.Param != "tools[0].parameters" || requestErr.Code != "invalid_parameter" { + t.Fatalf("error = %#v", err) + } +} + func TestNormalizeBuildFunctionParametersRootVariants(t *testing.T) { tests := []struct { name string