Commit Graph
3 Commits
Author SHA1 Message Date
e1d4dca967 gateway: a Chat reply's leading <thinking> block is read as thinking
A Chat reply may open its reasoning with <thinking>…</thinking> in the
text. thoughtTags held only <thought> and <think>, so neither tag matched:
chatDecoder.text took the block for the answer's own text and no thinking
block went out (#5106). In a recorded day on one OpenAI-compatible host,
47 of 2832 turns carried a literal <thinking in the answer's text, 44 of
them at offset 0 (neighbads).

<thinking> is added beside the two, closed by </thinking>. It shares no
whole prefix with <think> (they part at the seventh byte, 'i' against
'>'), so the wait in text covers the shared lead and no lead can name two
opening tags. markedThought reads the same map, so a Gemini-marked chunk
opening with <thinking> is thought's text too.

afterThought, which strips a leading block from the titles,
descriptions and predictions Codex asks for (codex_titles.go), now reads
the same map instead of its own <think>, so a <thinking>- or
<thought>-led reply no longer leaves the block in a title (added in the
port; the patch left it as a sibling).

Run (mote): go test -tags nogui -count=5 -run 'TestChatThoughtTags|
TestMarkedThought|ThoughtTag|TestAfterThought|Title|Description'
./internal/gateway/ ok; gofmt -l clean; go vet -tags nogui
./internal/gateway/ clean.
Broken by hand: chat.go put back to origin/main, the four <thinking>
cases of TestChatThoughtTags FAIL and TestAfterThoughtCloseInCode FAILs
on the <thinking> and <thought> leads.
Not tried: a live model that sends the tag (the reporter's model sends
it in about 1.7% of turns, not on demand). The plugins repo's think-tags
middleware keeps its own list and is not changed here.

The tag and the whole/split cases are neighbads' patch on their fork;
ggbdpq added the open-tag-alone and never-closed cases.

Fixes #5106

Generated-by: GLM-5.3-Flash (ZCode)
Co-authored-by: neighbads <16126598+neighbads@users.noreply.github.com>
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-06 10:04:22 +08:00
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
yetone d4a1b9baa7 gateway: a </think> inside code no longer ends a reply's thinking; a plugin's text keeps its <think> (#2634)
Kane-Kuroneko's Qoder replies through Codex began mid-sentence at a
backtick: the Chat decoder took any reply whose text began with <think>
as inline thinking up to the first literal </think>, even one inside an
inline-code example ("使用 `<think>...</think>` 标签"), for every Chat
upstream.

- The close is looked for only outside Markdown code (mdCode in
  chat.go): an inline span (taken to end with its line, so a stray
  backtick can't swallow the answer) or a fenced block. Backtick runs and
  a close split across chunks are held, as the tag tail was.
- A plugin's Chat reply is read as OpenCode reads it, its text as it came
  (decoderFor, used by streamTranslated, the continuation path and the
  search path): the plugin knows its vendor and gives reasoning apart.
  Qoder's plugin sends reasoning_content and has no splitter of its own.
- A reply that already gave thinking in reasoning_content/reasoning has
  no leading-tag lookup in its text.
- The line breaks after a close are left out however they're chunked:
  one character a delta used to leave "\n" at the head of the answer.
- Codex titles/descriptions/predictions (codex_titles.go) strip a leading
  block with the same close rule (afterThought), the sibling cut.
MiniMax (2ab69d0f) and Gemini's <thought> keep working: their existing
tests pass unchanged.

Tests use the reporter's literal bytes, whole, a character a delta and a
byte a delta: TestChatThinkCloseInCode, TestThinkExampleReachesClientsWhole
(Codex /v1/responses and Claude Code /v1/messages against a fake Chat
upstream), TestChatThinkTagAfterReasoningField, TestPluginThinkTagIsText
(the fake plugin as qoder, through Bun, with #2634's historical
boundary), TestAfterThoughtCloseInCode.

Broken by hand, each watched go red: close matched inside code ->
TestChatThinkCloseInCode, TestThinkExampleReachesClientsWhole,
TestAfterThoughtCloseInCode; reasoning-field rule off ->
TestChatThinkTagAfterReasoningField; plugin scope off ->
TestPluginThinkTagIsText; newline trim off -> TestChatThinkCloseInCode,
TestThinkExampleReachesClientsWhole, plus the existing MiniMax tests.

Run: gofmt -l clean; go vet ./... (darwin), GOOS=linux and GOOS=windows
go vet -tags nogui ./... clean; go test -timeout 45m ./... on this change plus the next
commit's library edit (the worktree held both while it ran): 64
packages ok, internal/gateway ok (524s), internal/library ok; internal/gui's
real-window darwin tests were killed at their 30s bar under a load
average of ~295, and internal/provider failed to link on a GOCACHE file
another run trimmed. Rerun alone: internal/provider ok (206s),
internal/gui ok (165s). No GUI files touched. Not tried
against real Qoder or MiniMax accounts.

Not changed: the plugins repo's think-tags middleware
(packages/think-tags) also ends a block at the first indexOf("</think>")
when a user turns it on.
2026-10-17 08:49:42 +08:00