gateway: a WebSocket upgrade on /v1/responses is answered 426 "magpie relays Responses over HTTP only", not 404 (#1005, Clarence12138) — Pi's Responses WebSocket extension opens GET /v1/responses with Upgrade: websocket, and only POST was routed there, so the catch-all's 404 ("magpie serves …") read as a broken gateway and the extension retried five times a session before falling back to SSE. GET /v1/responses and /responses now tell an upgrade 426 with the reason and the way that works (POST, streamed as SSE) — the answer a client falls back to HTTP on at once, as the Codex backend path already gives (TestCodexWebSocketUpgradeRequired) — and a plain GET 405 with Allow: POST. Relaying Responses over a WebSocket itself is not done here (a long-lived connection whose model is named per response.create, outside the per-request routing, failover, key limits and usage the gateway has). TestResponsesWebSocketUpgradeRequired fails without the change (404) and passes; go test ./internal/gateway, go vet (darwin, windows), gofmt -l and the linux build pass. Not tried with Pi's extension itself. docs/reference.md and gateway-routing.md say it.
This commit is contained in:
+1
-1
@@ -786,7 +786,7 @@ It exposes:
|
||||
| Path | API |
|
||||
| ------------------------ | -------------------------- |
|
||||
| `/v1/chat/completions` | OpenAI chat completions |
|
||||
| `/v1/responses` | OpenAI Responses |
|
||||
| `/v1/responses` | OpenAI Responses (HTTP, streamed as SSE; a WebSocket upgrade is answered 426) |
|
||||
| `/v1/messages` | Anthropic Messages |
|
||||
| `/v1/messages/count_tokens` | Anthropic token counting |
|
||||
| `/v1beta/models/{model}:generateContent` | Google Gemini (also `:streamGenerateContent`, `:countTokens`) |
|
||||
|
||||
@@ -16,7 +16,7 @@ served by a plugin are in [Provider and plugin ownership](provider-plugins.md).
|
||||
| Part | Responsibility | Source |
|
||||
| --- | --- | --- |
|
||||
| Address and caller | `DefaultAddr`. The local token `magpie` and the per-agent `magpie-<agent>` (`TokenFor`). `agentOf` tells which agent asked, first by token, then by User-Agent. | [`gateway.go`](../../internal/gateway/gateway.go) |
|
||||
| HTTP surface | `Handler` routes `/v1/chat/completions`, `/v1/responses`, `/v1/messages` (each also without `/v1`), `/v1/models`, Gemini's `/v1beta/models/…`, the Codex backend path, `/mcp/{name}` and the `/v1/magpie/*` status endpoints. Every route is wrapped in `counted`, `callerGuard`, `withCaller` and `keyLimited`. | [`gateway.go`](../../internal/gateway/gateway.go) |
|
||||
| HTTP surface | `Handler` routes `/v1/chat/completions`, `/v1/responses`, `/v1/messages` (each also without `/v1`), `/v1/models`, Gemini's `/v1beta/models/…`, the Codex backend path, `/mcp/{name}` and the `/v1/magpie/*` status endpoints. A GET on `/v1/responses` (a client opening Responses over a WebSocket) is answered 426, a plain one 405: Responses is relayed over HTTP only. Every route is wrapped in `counted`, `callerGuard`, `withCaller` and `keyLimited`. | [`gateway.go`](../../internal/gateway/gateway.go) |
|
||||
| Model resolution | `serve` resolves the model the agent asked for: Claude tier stand-ins, a routing group (`provider.GroupFor`), auto stand-ins, then `provider.Resolve`. An unknown model gets a 404 naming why: a switched-off provider, a disabled group or an empty group. | [`gateway.go`](../../internal/gateway/gateway.go), [`internal/provider/group.go`](../../internal/provider/group.go) |
|
||||
| Candidates | `plan` lists one provider's candidates: one per key or account on, then each `Fallback` model, with resting ones last (`restLast`). `planGroup` and `planLevel` weigh a group's members together. A nested group plans by its own routing (#576). A member's own fallbacks are not the group's. | [`fallback.go`](../../internal/gateway/fallback.go) |
|
||||
| Order of keys and accounts | `Routing`: smart (""), `order`, `rotate`, `usage` (least used, with an `usageHalfLife` of 1h), `pace` or `weight` (`weightedFirst`). A failure rests a candidate (`failure`, `failureOf`). | [`routing.go`](../../internal/gateway/routing.go), [`weighted.go`](../../internal/gateway/weighted.go), [`internal/provider/routing.go`](../../internal/provider/routing.go) |
|
||||
|
||||
@@ -497,6 +497,8 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("POST /chat/completions", s.handle(provider.Chat))
|
||||
mux.HandleFunc("POST /v1/responses", s.handle(provider.Responses))
|
||||
mux.HandleFunc("POST /responses", s.handle(provider.Responses))
|
||||
mux.HandleFunc("GET /v1/responses", responsesOverHTTP)
|
||||
mux.HandleFunc("GET /responses", responsesOverHTTP)
|
||||
mux.HandleFunc("POST /v1/messages", s.handle(provider.Anthropic))
|
||||
mux.HandleFunc("POST /messages", s.handle(provider.Anthropic))
|
||||
mux.HandleFunc("POST /v1/systemone", s.serveSystemOne)
|
||||
@@ -526,6 +528,21 @@ func (s *Server) Handler() http.Handler {
|
||||
return s.counted(callerGuard(withCaller(keyLimited(mux))))
|
||||
}
|
||||
|
||||
// responsesOverHTTP answers a GET on /v1/responses, which is how a client
|
||||
// opens Responses over a WebSocket (#1005): magpie relays Responses over
|
||||
// HTTP alone, its stream as SSE, so the upgrade is told 426 — the answer a
|
||||
// client falls back to HTTP on at once (Codex does, as on CodexPath) — in
|
||||
// place of the 404 every unknown path gets, which read as a broken
|
||||
// gateway. A plain GET is told to POST.
|
||||
func responsesOverHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.EqualFold(r.Header.Get("Upgrade"), "websocket") {
|
||||
writeError(w, provider.Responses, http.StatusUpgradeRequired, "magpie relays Responses over HTTP only: send POST /v1/responses (streamed as SSE); Responses over a WebSocket is not served")
|
||||
return
|
||||
}
|
||||
w.Header().Set("Allow", "POST")
|
||||
writeError(w, provider.Responses, http.StatusMethodNotAllowed, "POST /v1/responses")
|
||||
}
|
||||
|
||||
func (s *Server) info(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, map[string]any{"name": "magpie", "version": Version, "models": len(provider.Catalog()), "window": Window,
|
||||
"apis": []string{"/v1/chat/completions", "/v1/responses", "/v1/messages", "/v1/systemone", "/v1beta/models/{model}:generateContent", "/v1/images/generations", "/v1/images/edits", "/v1/videos", "/v1/embeddings", "/v1/rerank", "/v1/magpie/quotas", "/v1/magpie/quotas/history", "/v1/magpie/route"}})
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// A client opening Responses over a WebSocket (Pi's Responses WS
|
||||
// extension, #1005) is told 426 with the reason, not the 404 of an unknown
|
||||
// path, and nothing goes upstream; a plain GET is told to POST.
|
||||
func TestResponsesWebSocketUpgradeRequired(t *testing.T) {
|
||||
setup(t, provider.Responses, &fake{t: t})
|
||||
for _, path := range []string{"/v1/responses", "/responses"} {
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", path, nil)
|
||||
req.Header.Set("Upgrade", "websocket")
|
||||
req.Header.Set("Connection", "Upgrade")
|
||||
req.Header.Set("Sec-WebSocket-Version", "13")
|
||||
req.Header.Set("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==")
|
||||
New().Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusUpgradeRequired || !strings.Contains(rec.Body.String(), "POST /v1/responses") {
|
||||
t.Errorf("%s upgrade: %d %s", path, rec.Code, rec.Body)
|
||||
}
|
||||
rec = httptest.NewRecorder()
|
||||
New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
|
||||
if rec.Code != http.StatusMethodNotAllowed || rec.Header().Get("Allow") != "POST" {
|
||||
t.Errorf("%s GET: %d %q", path, rec.Code, rec.Header().Get("Allow"))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user