From d82985eee150adb395d43de0aace338a4e847b02 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 11 Oct 2026 19:59:24 +0800 Subject: [PATCH] agent: a routing group whose members are all best on Responses is asked there by Pi, omp, droid and dsh, so its tool-call arguments stream (#1594, ObsidianArch02) magpieModels gave a routing group no native API, since its members may each want another. Pi, omp and droid then asked every group on Chat Completions, and dsh put it on its Chat route, even a group of GPT members all served on Responses. The gateway translated each answer from Responses to Chat, and that translation holds a tool call's arguments until the call ends (#613), so Pi's Code Mode saw no code until the whole call had been generated. A group now has APIs ["responses"] when every member, nested groups' included, is served best on Responses (allResponses over the group's members, by Provider.Native). Any other group keeps none: one with a Chat member, and a Messages-only group too, as the gateway knows whether a Claude thinks adaptively by its own model id, not the group's. dsh moves a start on such a group to its Responses route (dshStartOnItsRoute); a dsh session resumed on it asks for the model once, as for any model that moved routes. The rest of the request (a protocol picked per group, conversion shown per request) is not in this commit. Verified: - TestPiModelsAskedOnTheirNativeAPI: auto-gpt-5-5, a GPT group and a group of that group get openai-responses; a group with a Chat member and one of Messages-only members get none. Without the route.go change it fails for the three Responses groups. - TestOmpModelsAskedOnTheirNativeAPI: the same for omp. - TestGroupOnResponsesRelaysArgumentDeltas (gateway): a group of two Responses-only members asked on /v1/responses relays each function_call_arguments.delta as the vendor sent it, on /responses. It guards what this change relies on, and passed before it too. - go test ./internal/agent ./internal/provider, gofmt -l, GOOS=linux and windows go vet -tags nogui. - Not tried with a real Pi against a real group. --- docs/subsystems/agent-wiring.md | 2 +- internal/agent/omp_api_test.go | 11 +++- internal/agent/pi_api_test.go | 23 ++++++- internal/agent/route.go | 22 ++++++- .../gateway/group_responses_relay_test.go | 65 +++++++++++++++++++ 5 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 internal/gateway/group_responses_relay_test.go diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index ca253c0ad..7b22699a2 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -40,7 +40,7 @@ something else shows up as drift. - A sync brings magpie's models up to date, not where the agent reaches magpie. Every `Sync` that `SyncCatalog` runs (each start and update of magpie, a provider added) goes through `synced`, which passes each file written through `keptAway` (`edit.Filtered`, [`keptaway.go`](../../internal/agent/keptaway.go)). In a file without the gateway's own address, whose magpie the user pointed at an address on another machine (a NAS's, or this machine's LAN address), and with no address a WSL distro was seen at, that address stays in place of `gateway.URL()`, and so does the key that went with it in place of magpie's own (coeo91 on Discord: the base URL went back to 127.0.0.1 after an update or restart). An address on this machine's loopback follows the gateway; with more than one address or key gone, the write is left as it is. Picking a model in magpie still points the agent at this magpie. ZCode and WorkBuddy keep such an address in their own writers too (`zcodeAddress`, `buddyWriteAt`). - JetBrains Air's `magpie-opencode.json` keeps user-added fields in `provider.magpie`, including nested options, on sync and model picks (`air` in [`air.go`](../../internal/agent/air.go), using `theirsKept` in [`sync.go`](../../internal/agent/sync.go)). The gateway fields and complete model list are still replaced, so removed models leave the list. - Copilot in JetBrains IDEs keeps its Bring Your Own Key providers in the Copilot language server's `byok.json`, shared with Xcode's and Eclipse's Copilot (`copilotJetBrains` in [`copilotjetbrains.go`](../../internal/agent/copilotjetbrains.go)). magpie writes its own three keys there, `magpie-provider-config`, `magpie-api-key` and `magpie-models-config`, in the bytes the server itself writes, and only when they differ; the user's providers and keys stay. A magpie model the user hid in Manage Models (`isRegistered: false`) stays hidden through a sync. A new file is made 0600 in a 0700 folder, as the server makes it. -- dsh (DeepSeek Harness) sets one API per llm-pi-ai route, so magpie writes a route per protocol (`dshKinds` in [`dsh.go`](../../internal/agent/dsh.go); 01huadalang on Discord): `magpie` (Chat Completions) for most models, `magpie-responses` (`openai-responses`) for a model served on Responses, `magpie-anthropic` (`anthropic-messages`, the gateway root, `forceAdaptiveThinking` where `AdaptiveThinking`) for one served on Messages, by the model's `APIs` (`dshRouteOf`). A route with no models is taken out. An api the user picked for a route in dsh's Models page is kept, and so are keys they added there. The start (`agent-default-model`) names its model's route, and Sync moves a start on one of magpie's routes to the route its model is now on (`dshStartOnItsRoute`), its effort and marks kept. Check asks the route the start names to list its model. A dsh session resumed on a model that has since moved routes asks for the model to be picked again, since dsh finds a model by route and id. +- dsh (DeepSeek Harness) sets one API per llm-pi-ai route, so magpie writes a route per protocol (`dshKinds` in [`dsh.go`](../../internal/agent/dsh.go); 01huadalang on Discord): `magpie` (Chat Completions) for most models, `magpie-responses` (`openai-responses`) for a model served on Responses, `magpie-anthropic` (`anthropic-messages`, the gateway root, `forceAdaptiveThinking` where `AdaptiveThinking`) for one served on Messages, by the model's `APIs` (`dshRouteOf`). A model's `APIs` is the one its provider serves it on best (`Provider.Native`, in `magpieModels`, [`route.go`](../../internal/agent/route.go)), and Pi, omp and droid read it the same way. A routing group has `responses` when every member is best on Responses (`allResponses`, #1594), so its tool-call arguments are relayed as they stream; any other group has none and stays on Chat, a Messages-only group too, as a Claude member's thinking is known by its own model. A route with no models is taken out. An api the user picked for a route in dsh's Models page is kept, and so are keys they added there. The start (`agent-default-model`) names its model's route, and Sync moves a start on one of magpie's routes to the route its model is now on (`dshStartOnItsRoute`), its effort and marks kept. Check asks the route the start names to list its model. A dsh session resumed on a model that has since moved routes asks for the model to be picked again, since dsh finds a model by route and id. - Ante's `catalog.json` keeps user-added fields in `providers.magpie` the same way (`ante` in [`ante.go`](../../internal/agent/ante.go)); its `auth`, `http_headers`, `extra_body` and `stream_idle_timeout_secs` are what a gateway shared on the local network or reached from WSL needs to send a key. Ante starts a session on the `provider` and `model` of `settings.json`, not on what its catalog holds (#1210), so magpie sets that pair and puts back what it found there; a pick the user makes in Ante while magpie is wired is theirs and stands. - AstrBot keeps its providers in `data/cmd_config.json` under its root (`$ASTRBOT_ROOT`, else `~/.astrbot`, the desktop app's, else `~/astrbot`), written with a UTF-8 BOM, which magpie keeps (`astrbot` in [`astrbot.go`](../../internal/agent/astrbot.go)). magpie adds a `magpie` entry to `provider_sources` and one `magpie/` entry per model to `provider`, keeps the user's own entries byte for byte and keeps what the user changes in magpie's (timeout, proxy, enable), and sets the default chat model (`agent_runner.config.model.provider_id`, or `provider_settings.default_provider_id` before the agent runner), putting back what it found there. A pick the user makes while magpie is wired is theirs and stands. The first connect copies the file to `cmd_config.json.before-magpie`. AstrBot reads the file only at start-up and its WebUI writes it back whole on save, so the agent's notice says to restart AstrBot before saving in the WebUI. A Docker install's root is inside its container, which magpie doesn't reach. - jcode keeps its settings in `config.toml` in `$JCODE_HOME`, else `~/.jcode` (`jcode` in [`jcode.go`](../../internal/agent/jcode.go)). magpie adds a named profile, `[providers.magpie]`, as `jcode provider add` writes one: `type = "openai-compatible"`, the gateway's `/v1`, an inline `api_key` naming jcode (jcode sends nothing else that tells it apart) and `requires_api_key = true`, since jcode sends no key to a loopback URL without it. Its `models` list has one entry per model, with `context_window`, `input` and `reasoning` for a model with effort levels. A `reasoning_effort` the user set on one stays while the model has that level. The model jcode starts on is `[provider] default_provider = "magpie"` and `default_model`. What was there is stashed and put back on Disconnect. A model of jcode's own picked while it is on magpie takes magpie out. Each key is set alone, so the user's other keys and tables stay, and the `[[providers.magpie.models]]` tables jcode writes when it saves the file itself are replaced by the one list. A sync with nothing to change doesn't write. Disconnect drops the profile whole, its sub-tables included. jcode reads the file when it starts, so the notice says to restart open sessions. diff --git a/internal/agent/omp_api_test.go b/internal/agent/omp_api_test.go index a6c6928a2..5db8b8a76 100644 --- a/internal/agent/omp_api_test.go +++ b/internal/agent/omp_api_test.go @@ -15,8 +15,9 @@ import ( // omp asks each model on the API its provider serves it on, as Pi does: a // Claude over Chat lost its thinking's signatures between tool turns. A // Messages-only provider's models go on anthropic-messages at the gateway, -// a Responses-only one's on openai-responses, and a Chat provider's and a -// group's stay on the provider's openai-completions, keyless. +// a Responses-only one's on openai-responses, and a Chat provider's stay on +// the provider's openai-completions, keyless. A group goes on +// openai-responses when all its members are best there (#1594), else stays. func TestOmpModelsAskedOnTheirNativeAPI(t *testing.T) { home := syncHome(t) was := ompVersion @@ -31,6 +32,9 @@ func TestOmpModelsAskedOnTheirNativeAPI(t *testing.T) { t.Fatal(err) } } + if err := provider.SaveGroup(provider.Group{ID: "mixed", Name: "Mixed", Members: []string{"resp/grok-5", "relay/glm-4.6"}}); err != nil { + t.Fatal(err) + } var live []catalog.Model for _, id := range []string{"claude-sonnet-5", "claude-sonnet-4-5", "kimi-k3"} { live = append(live, catalog.Model{ID: id, Efforts: []string{"low", "high", "max"}}) @@ -64,7 +68,8 @@ func TestOmpModelsAskedOnTheirNativeAPI(t *testing.T) { "anth/claude-sonnet-5": "anthropic-messages", "anth/claude-sonnet-4-5": "anthropic-messages", "anth/kimi-k3": "anthropic-messages", - "group/auto-gpt-5-5": nil, + "group/auto-gpt-5-5": "openai-responses", + "group/mixed": nil, } { e, ok := entries[id] if !ok { diff --git a/internal/agent/pi_api_test.go b/internal/agent/pi_api_test.go index 22bf66c67..4ab53e501 100644 --- a/internal/agent/pi_api_test.go +++ b/internal/agent/pi_api_test.go @@ -12,8 +12,9 @@ import ( // Pi asks each model on the API its provider serves it on, so the gateway // relays the request as it is: a Responses-only provider's models and GPT // on OpenAI's API go on openai-responses; a Messages-only provider's on -// anthropic-messages at the gateway; a Chat provider's and a group's stay -// on the provider's openai-completions. +// anthropic-messages at the gateway; a Chat provider's stay on the +// provider's openai-completions. A group whose members are all best on +// Responses goes there too (#1594); any other group stays on Chat. func TestPiModelsAskedOnTheirNativeAPI(t *testing.T) { home := syncHome(t) for _, p := range []provider.Provider{ @@ -25,6 +26,18 @@ func TestPiModelsAskedOnTheirNativeAPI(t *testing.T) { t.Fatal(err) } } + // a group goes on Responses only when every member is best served there + // (#1594): one with a Chat member, or of Messages ones, stays on Chat + for _, g := range []provider.Group{ + {ID: "gpts", Name: "GPTs", Members: []string{"resp/gpt-5.5", "openai/gpt-5.5", "resp/grok-5"}}, + {ID: "mixed", Name: "Mixed", Members: []string{"resp/grok-5", "relay/glm-4.6"}}, + {ID: "claudes", Name: "Claudes", Members: []string{"anth/claude-sonnet-5", "anth/kimi-k3"}}, + {ID: "outer", Name: "Outer", Members: []string{"group/gpts", "openai/gpt-5.5"}}, + } { + if err := provider.SaveGroup(g); err != nil { + t.Fatal(err) + } + } if err := pi(home).Field("model").Set("magpie/resp/grok-5"); err != nil { t.Fatal(err) } @@ -49,7 +62,11 @@ func TestPiModelsAskedOnTheirNativeAPI(t *testing.T) { "relay/glm-4.6": nil, "anth/claude-sonnet-5": "anthropic-messages", "anth/kimi-k3": "anthropic-messages", - "group/auto-gpt-5-5": nil, + "group/auto-gpt-5-5": "openai-responses", + "group/gpts": "openai-responses", + "group/outer": "openai-responses", + "group/mixed": nil, + "group/claudes": nil, } { got, ok := apis[id] if !ok { diff --git a/internal/agent/route.go b/internal/agent/route.go index ffda429b0..c4e0f1623 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -129,11 +129,17 @@ func magpieModels(agent string) []catalog.Model { m.ImageInput = &yes } // APIs is the one to ask it on for the gateway to relay the request - // as it is; none for a group, whose members may each want another + // as it is. A group's members may each want another, so a group has + // one only when every member is served best on Responses (#1594, + // ObsidianArch02): Pi, omp, droid and dsh then ask it there, and a + // GPT group's tool-call arguments stream as the member sends them + // instead of being held for a Chat answer if e.Group == "" { if n := e.Provider.Native(e.Model); n != "" { m.APIs = []string{string(n)} } + } else if allResponses(members(e.ID)) { + m.APIs = []string{string(provider.Responses)} } // the service tiers Codex is offered on it, as the gateway's own // lists offer them: Fast in the Codex CLI magpie writes @@ -144,6 +150,20 @@ func magpieModels(agent string) []catalog.Model { return out } +// allResponses reports whether a group's members are all served best on +// Responses, so the gateway relays a Responses request to whichever it +// picks. A group with none, or one member on another API, is false: a +// Messages-only group stays on Chat too, as the thinking a Claude member +// takes is known by its own model, not by the group's id. +func allResponses(ms []provider.Member) bool { + for _, m := range ms { + if m.Provider.Native(m.Model) != provider.Responses { + return false + } + } + return len(ms) > 0 +} + // maxTokens is the output limit an agent is handed for m, kept within the // context window it is handed with it: models.dev lists some models' output // above their window (deepseek-chat's 384000 against 128000). An unknown diff --git a/internal/gateway/group_responses_relay_test.go b/internal/gateway/group_responses_relay_test.go new file mode 100644 index 000000000..cb68306a4 --- /dev/null +++ b/internal/gateway/group_responses_relay_test.go @@ -0,0 +1,65 @@ +package gateway + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// A group whose members are all served on Responses is asked there by Pi, +// omp, droid and dsh (#1594, ObsidianArch02). The gateway relays such a +// request to the member it picks as it is: a tool call's arguments reach +// the agent delta by delta, as the vendor sent them, not held until the +// call ends as a Chat answer translated from Responses has them. +func TestGroupOnResponsesRelaysArgumentDeltas(t *testing.T) { + fresh(t) + var mu sync.Mutex + var paths []string + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + io.ReadAll(r.Body) + mu.Lock() + paths = append(paths, r.URL.Path) + mu.Unlock() + w.Header().Set("Content-Type", "text/event-stream") + io.WriteString(w, sse( + `event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1","model":"gpt-5.5","usage":{"input_tokens":0,"output_tokens":0}}}`, + `event: response.output_item.added`+"\n"+`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"codemode","arguments":""}}`, + `event: response.function_call_arguments.delta`+"\n"+`data: {"type":"response.function_call_arguments.delta","item_id":"fc_1","output_index":0,"delta":"{\"code\":"}`, + `event: response.function_call_arguments.delta`+"\n"+`data: {"type":"response.function_call_arguments.delta","item_id":"fc_1","output_index":0,"delta":"\"print(1)\"}"}`, + `event: response.function_call_arguments.done`+"\n"+`data: {"type":"response.function_call_arguments.done","item_id":"fc_1","output_index":0,"arguments":"{\"code\":\"print(1)\"}"}`, + `event: response.output_item.done`+"\n"+`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"codemode","arguments":"{\"code\":\"print(1)\"}"}}`, + `event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r1","model":"gpt-5.5","status":"completed","usage":{"input_tokens":10,"output_tokens":5}}}`, + )) + })) + defer up.Close() + for _, id := range []string{"r1", "r2"} { + if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k", Responses: up.URL + "/v1", Models: []string{"gpt-5.5"}}); err != nil { + t.Fatal(err) + } + } + if err := provider.SaveGroup(provider.Group{Name: "GPTs", Members: []string{"r1/gpt-5.5", "r2/gpt-5.5"}, Routing: provider.Ordered}); err != nil { + t.Fatal(err) + } + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", + strings.NewReader(`{"model":"group/gpts","input":"hi","stream":true,"tools":[{"type":"function","name":"codemode","parameters":{"type":"object"}}]}`))) + if rec.Code != 200 { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + out := rec.Body.String() + for _, d := range []string{`"delta":"{\"code\":"`, `"delta":"\"print(1)\"}"`} { + if !strings.Contains(out, d) { + t.Fatalf("the argument delta %s didn't reach the agent:\n%s", d, out) + } + } + mu.Lock() + defer mu.Unlock() + if len(paths) != 1 || !strings.HasSuffix(paths[0], "/responses") { + t.Fatalf("the member was asked on %v", paths) + } +}