Commit Graph
3 Commits
Author SHA1 Message Date
yetone e57e785633 gateway: a rewritten body is written back by marshalJSON, not json.Marshal (#3394)
withReasoningText runs on every replayed request to DeepSeek and the
like (#1104), and wrote the whole body back with json.Marshal twice:
once for the input list, once for the body around it. encoding/json
writes a json.RawMessage through its scanner, a call a byte, so a long
conversation was compacted twice a request. marshalJSON (marshalRaw,
#1532) writes the same bytes, checked by jsonCheck and copied, and
falls back to json.Marshal for any other value; TestMarshalRawIsEncodingJSON
holds the two equal. The same body rewrites in affinity.go,
anthropic.go, codex_backend.go and limit.go took json.Marshal of a
map[string]json.RawMessage too, and use marshalJSON now. Nothing a
body goes out as changes.

Run:
- Real DeepSeek account in a sandbox HOME, magpie serve on 3482, 12
  streamed /v1/responses requests of a synthetic 1.5MB conversation with
  Codex 0.162.0's real tools and a codex_cli_rs/0.162.1 User-Agent, CPU
  time from ps, two rounds each: b97e0aa2a 0.74s and 0.75s, this 0.67s and
  0.66s, about 62ms -> 55ms a request. All 48 answered 200.
- go test ./internal/gateway -count 1 -timeout 45m at this tree: ok
  (370s).
- No new test: the change is the encoder, and TestMarshalRawIsEncodingJSON
  already fails if marshalJSON's bytes differ from json.Marshal's.
- gofmt -l clean; GOOS=linux and windows go vet -tags nogui ./internal/gateway ok.
2026-10-25 00:59:10 +08:00
yetone 2122b5a8a2 gateway: a provider's floor on max_tokens said with >= or > is read, so a one-token probe to Command Code is asked again at 16 instead of failing with a 400 (#260 HuHong-Tao: "Expected a value >= 16, but got 1 instead." — Claude Desktop's gateway check reported the model unavailable)
tokenFloor read the error from the body magpie had re-encoded for the agent, where Go's JSON encoder had escaped > as >, so the >= and > wordings never matched and the request wasn't retried. The pattern now also takes the escaped sign (a vendor may send it that way too), a trailing = meaning the number is the floor itself; and writeJSON no longer escapes <, > and &, as withFields already didn't, so the agent sees the vendor's words as written — the same JSON either way. Tests: TestTokenFloor gains the escaped and > cases; TestTooFewTokensSaidWithSigns goes through the gateway on /v1/messages (streaming) and /v1/chat/completions with Command Code's wording (asked again at 16), "max_tokens must be > 2" (at 3) and a vendor that sends > itself. Both fail without the change (a 400 after one request). Not tried against Command Code itself.
2026-09-30 11:30:11 +08:00
yetone 9e7853906c gateway: a request for a reply of a token or two reaches a provider that answers no less (#64) — an app checking a model is up asks for max_tokens 1, which some providers turn away with 400 "max_tokens must be greater than 2"; the least the provider named is read from that error, and the request is sent once more asking for it (max_tokens, max_completion_tokens, max_output_tokens or Gemini's maxOutputTokens, whichever it set), before any of the reply reaches the app; the routing page says so 2026-09-25 21:40:20 +08:00