Files
magpie/internal/provider/stream_required.go
T
yetone 1f99f6f0b5 provider, gateway: a backend that only streams is tested and served (Cyrus_4433 on X: claude-relay-service "400 · Stream must be set to true")
A CRS Responses endpoint, like Codex's own backend, turns away a request that
doesn't ask for stream:true. The editor's Test models and protocol detection
send one small body, so every model of such a relay was red; an agent that sent
stream:false got the same 400 through the gateway.

Probe: probeAt asks once more with stream:true when a 400/422 says the stream
must be set (provider.StreamRequired, WithStream). Chosen over streaming every
probe so a vendor that takes one body keeps being asked for one; every probe
(model test, per-model check, detect, key check) goes through probeAt.
Gateway: passthrough marks the model stream-only on that 400 for a request
asking one body and returns done=false; attempt then translates on the same
API (always streamed upstream, whole reply to the client), and asks so at once
from then on, as a refused custom tool is.

Run (mote): new tests built from the reporter's literal error, red on the old
code (TestProbeStreamsWhereTheBackendOnlyStreams, TestDetectFindsAStreamOnlyBackend,
TestWholeReplyFromAStreamOnlyBackend), green on the new; go vet darwin, windows,
linux nogui; go test ./internal/provider ./internal/gateway ok. Each fix broken
by hand (probe retry stashed, gateway hook stashed): red. Not tried: a real CRS
relay; the rest of ./... (started after the tag).
2026-11-11 10:37:13 +08:00

37 lines
1.4 KiB
Go

package provider
import (
"encoding/json"
"github.com/yetone/magpie/internal/lazyre"
)
// A backend that only streams (claude-relay-service's Responses endpoint,
// like Codex's own ChatGPT backend) turns a request without stream:true
// away with a 400: "Stream must be set to true" (Cyrus_4433 on X: every
// model of a CRS relay red in the editor's test).
//
// The probe asks once more streamed when it hears this, rather than
// streaming every probe: a vendor that takes one body keeps being asked
// for one, and only a backend that says so is asked otherwise.
var streamRequired = lazyre.New(`(?i)\bstream(ing)?\b[^.\n]{0,40}\b(must|need|needs|should|has to|have to)\b[^.\n]{0,30}\btrue\b|\bonly\s+(supports?|accepts?|allows?|serves?)\s+(requests\s+(with|in)\s+)?stream|\brequires?\s+stream|\bstream(ing)?\s+(is\s+)?(required|mandatory)\b`)
// StreamRequired is whether a 400's words say the request must ask for a
// stream.
func StreamRequired(msg []byte) bool { return streamRequired.Match(msg) }
// WithStream is body, a JSON object that doesn't ask for a stream, asking
// for one, and whether it changed. Its other fields keep their bytes.
func WithStream(body []byte) ([]byte, bool) {
var m map[string]json.RawMessage
if json.Unmarshal(body, &m) != nil || m == nil || streams(body) {
return body, false
}
m["stream"] = json.RawMessage("true")
out, err := json.Marshal(m)
if err != nil {
return body, false
}
return out, true
}