Files
magpie/internal
yetone fc16214dac gateway: a <think> block after a line end at a Chat reply's head is thinking (jenryai on X)
jenryai on X (tweet 2115754572383932513) said a Grok reverse proxy added
as a custom provider gives its thinking as the answer's text. The Chat
decoder takes a leading <think>…</think> as thinking, but only after
spaces and "\n". grok2api_python (xLmiler, a Grok relay) writes its
grok-3-search results as "\r\n<think>{results}</think>\r\n" at the head
of the reply, so the "\r" made the whole block text, on every
translated route (Claude Code's Messages, Codex's Responses, streamed
or not). The lead now skips " \t\r\n" (leadSpace), the line breaks after
the close are trimmed with their "\r", and afterThought (Codex titles)
skips the same lead. Scoped by what is in the stream: a <think> later in
the text is still the model's words, and a plugin's Chat text is still
read as it came.

Checked against the relays' own source, the others already worked here
and are not changed: chenyme/grok2api (Go, and its Python 1.x
reasoning_content), jiujiu532/grok2api (reasoning_content),
TQZHR/grok2api's "<think>\n…\n</think>\n" and grok2api_python's
"<think>" + token for its reasoning models were all run through
/v1/messages and /v1/responses and came out as thinking. The plugins
repo's think-tags middleware already skips /^\s*/ before the tag.

Tests: TestGrokRelayThinkAfterLineEnd sends grok2api_python's literal
chunk shape (json.dumps spacing, \u-escaped Chinese, no finish_reason
before [DONE]) to /v1/messages streamed and not, and to /v1/responses;
TestChatThinkTags gains a "\r" + "\n<think>" split and a tab lead;
TestAfterThoughtCloseInCode a "\r\n" lead. Broken by hand: with
chat.go's change stashed, all three routes of the new test, both new
TestChatThinkTags cases and the afterThought case are red (the answer
was "\r\n<think>\r\n<details>…</think>\r\nMagpie is a gateway.").

Run (mote, at this commit on f6263439a): gofmt -l clean; go vet
./internal/gateway for darwin, GOOS=linux and GOOS=windows ok;
go test ./internal/gateway -timeout 45m -count=1: ok (472s).

Not tried: the reporter's own relay and client (not named yet), a real
grok2api_python deployment, and other packages' suites (only
internal/gateway changed). The reporter's "流口水" (slow/stuttering, or
rambling) has no cause found in magpie; the relays' tool-call sieves
hold text back on their side.
2026-10-29 19:34:39 +08:00
..
…
…
…
…
…
…