diff --git a/internal/gateway/anthropic.go b/internal/gateway/anthropic.go index 8d8a8b3bf..67f66b8fd 100644 --- a/internal/gateway/anthropic.go +++ b/internal/gateway/anthropic.go @@ -414,6 +414,8 @@ func buildAnthropic(r *Request, model string) []byte { schema := t.Schema if len(schema) == 0 { schema = json.RawMessage(`{"type":"object","properties":{}}`) + } else { + schema = objectSchema(schema) } tools = append(tools, map[string]any{"name": t.Name, "description": t.Description, "input_schema": schema}) } @@ -780,3 +782,24 @@ var idClock = time.Now func newID() string { return fmt.Sprintf("%x%08x", idClock().UnixNano(), idSeq.Add(1)) } + +// objectSchema is a tool's input schema with no anyOf, oneOf or allOf at +// its root, which Anthropic's API refuses ("input_schema does not support +// oneOf, allOf, or anyOf at the top level"): Codex's codex_app +// automation_update has one, and Claude models behind Factory answered 400 +// to every request offering it (#646). A schema that has none is sent as +// it came. +func objectSchema(schema json.RawMessage) json.RawMessage { + if !bytes.Contains(schema, []byte(`Of"`)) { + return schema + } + var m map[string]any + if json.Unmarshal(schema, &m) != nil || !provider.ObjectRoot(m) { + return schema + } + b, err := json.Marshal(m) + if err != nil { + return schema + } + return b +} diff --git a/internal/gateway/anthropic_schema_test.go b/internal/gateway/anthropic_schema_test.go new file mode 100644 index 000000000..ac38a8d7a --- /dev/null +++ b/internal/gateway/anthropic_schema_test.go @@ -0,0 +1,59 @@ +package gateway + +import ( + "encoding/json" + "testing" +) + +// A tool offered to an Anthropic-protocol vendor never has anyOf, oneOf or +// allOf at its input_schema's root, which Anthropic refuses: Codex's +// codex_app automation_update has an anyOf of objects (#646). +func TestAnthropicToolSchemaRoot(t *testing.T) { + for name, schema := range map[string]string{ + "anyOf": `{"anyOf":[{"type":"object","properties":{"mode":{"const":"create"},"name":{"type":"string"}},"required":["mode","name"]},{"type":"object","properties":{"mode":{"const":"delete"},"id":{"type":"string"}},"required":["mode","id"]}]}`, + "oneOf": `{"type":"object","oneOf":[{"$ref":"#/$defs/a"}],"$defs":{"a":{"type":"object","properties":{"mode":{"type":"string"}},"required":["mode"]}}}`, + "allOf": `{"allOf":[{"type":"object","properties":{"a":{"type":"string"}},"required":["a"]},{"type":"object","properties":{"b":{"type":"number"}},"required":["b"]}]}`, + } { + r := &Request{Tools: []Tool{{Name: "automation_update", Schema: json.RawMessage(schema)}}} + var out struct { + Tools []struct { + InputSchema map[string]any `json:"input_schema"` + } `json:"tools"` + } + if err := json.Unmarshal(buildAnthropic(r, "claude-opus-5-5"), &out); err != nil || len(out.Tools) != 1 { + t.Fatalf("%s: %v %+v", name, err, out) + } + s := out.Tools[0].InputSchema + for _, k := range []string{"anyOf", "oneOf", "allOf"} { + if _, ok := s[k]; ok { + t.Fatalf("%s: %s still at the root: %v", name, k, s) + } + } + props, _ := s["properties"].(map[string]any) + if s["type"] != "object" || len(props) == 0 { + t.Fatalf("%s: root %v", name, s) + } + req, _ := s["required"].([]any) + switch name { + case "anyOf": + if len(props) != 3 || len(req) != 1 || req[0] != "mode" { + t.Fatalf("anyOf merged as %v", s) + } + case "allOf": + if len(props) != 2 || len(req) != 2 { + t.Fatalf("allOf merged as %v", s) + } + } + } + plain := `{"type":"object","properties":{"x":{"anyOf":[{"type":"string"},{"type":"null"}]}}}` + r := &Request{Tools: []Tool{{Name: "t", Schema: json.RawMessage(plain)}}} + var out struct { + Tools []struct { + InputSchema json.RawMessage `json:"input_schema"` + } `json:"tools"` + } + json.Unmarshal(buildAnthropic(r, "claude-opus-5-5"), &out) + if string(out.Tools[0].InputSchema) != plain { + t.Fatalf("a nested anyOf was changed: %s", out.Tools[0].InputSchema) + } +} diff --git a/internal/provider/grok.go b/internal/provider/grok.go index d39c433b7..7209892fc 100644 --- a/internal/provider/grok.go +++ b/internal/provider/grok.go @@ -339,17 +339,25 @@ func grokFlat(ns map[string]any) []any { // objectRoot makes a function's parameters an object at the root, which // Grok's backend wants ("tool parameter root must be an object type"): // Codex's codex_app automation_update takes an anyOf of objects (Fate on -// Discord). The object branches' properties are merged, a field each of -// them requires stays required, and the other branches go. It reports -// whether it changed anything. +// Discord). It reports whether it changed anything. func objectRoot(fn map[string]any) bool { ps, _ := fn["parameters"].(map[string]any) - if ps == nil { - return false - } + return ps != nil && ObjectRoot(ps) +} + +// ObjectRoot makes a tool's input schema a plain object at the root, with +// no anyOf, oneOf or allOf there: Grok's backend wants one, and so do +// Anthropic's models behind Factory ("input_schema does not support +// oneOf, allOf, or anyOf at the top level", #646). allOf's branches are +// all merged, properties and required alike. Of anyOf's and oneOf's object +// branches the properties are merged, a field each of them requires stays +// required, and the other branches go. It reports whether it changed +// anything. +func ObjectRoot(ps map[string]any) bool { _, any1 := ps["anyOf"] _, one := ps["oneOf"] - if ps["type"] == "object" && !any1 && !one { + _, all := ps["allOf"] + if ps["type"] == "object" && !any1 && !one && !all { return false } props, _ := ps["properties"].(map[string]any) @@ -360,6 +368,22 @@ func objectRoot(fn map[string]any) bool { if r, ok := ps["required"].([]any); ok { required = r } + list, _ := ps["allOf"].([]any) + for _, b := range list { + bm, _ := b.(map[string]any) + if bm = grokRef(ps, bm); bm == nil { + continue + } + bp, _ := bm["properties"].(map[string]any) + for k, v := range bp { + if _, ok := props[k]; !ok { + props[k] = v + } + } + br, _ := bm["required"].([]any) + required = append(required, br...) + } + delete(ps, "allOf") var branches []map[string]any for _, k := range []string{"anyOf", "oneOf"} { list, _ := ps[k].([]any) diff --git a/internal/provider/migrate_factory.go b/internal/provider/migrate_factory.go index 66c7d1416..911f41357 100644 --- a/internal/provider/migrate_factory.go +++ b/internal/provider/migrate_factory.go @@ -13,7 +13,7 @@ import ( func init() { movers["factory"] = &mover{ pkg: "@magpie-community/opencode-factory-auth", - min: "0.1.12", // a failure's status and its sign-in mark as the built-in's; Claude 5's system-role metadata as Factory takes it; Claude Code's standalone model-switch system updates; tool results quoting Claude Code's fixed phrases, and Claude Code's directory updates carrying a model; the sign-in renewed ahead of time through auth.refresh; Claude Code with a global CLAUDE.md or a SessionStart hook, and Claude Desktop's model line (#634) + min: "0.1.13", // a tool whose input_schema has anyOf/oneOf/allOf at its root (Codex's automation_update) wrapped for Claude models (#646); a failure's status and its sign-in mark as the built-in's; Claude 5's system-role metadata as Factory takes it; Claude Code's standalone model-switch system updates; tool results quoting Claude Code's fixed phrases, and Claude Code's directory updates carrying a model; the sign-in renewed ahead of time through auth.refresh; Claude Code with a global CLAUDE.md or a SessionStart hook, and Claude Desktop's model line (#634) agents: []string{"factory"}, out: func() ([]Moving, error) { ls := factoryLogins()