Commit Graph
4 Commits
Author SHA1 Message Date
ggbdpq 8807f621f2 gateway: a relay's DeepSeek model reads max_tokens as its own (#5030)
deepseekTokenField named a chat request's reply length max_tokens for
the DeepSeek preset and api.deepseek.com alone; a relay on its own
domain serving a DeepSeek model got max_completion_tokens as it came,
DeepSeek behind it ignored it and answered with its default, 8K
without thinking (#5030, NOMOAX) — the gate the tool-schema
normalization's had too (#4852, ethanfly). DeepSeek's quirks are told
by the model named in the body now, as Grok's are (grokModel): a
model a relay names deepseek-whatever gets the field DeepSeek reads,
whoever serves it; another vendor's model through the same relay goes
as it came.

Generated-by: GLM-5.3-Flash (ZCode)

Reviewed and ported. The model's name decides only on a host that isn't
DeepSeek's: chatTokenField runs after deepseekTokenField in forwardOnce,
so OpenAI's own hosts still get max_completion_tokens and a relay whose
400 names max_completion_tokens is still asked that way from then on.
The model test (provider/test.go probe) already sends max_tokens to a
host that isn't OpenAI's, so it needs nothing. The tool-schema \0 gate,
the same narrow gate, is #4852's, open.

Run on mote (darwin arm64, Go 1.26.8): go test -tags nogui -count=1
-run 'TokenField|MaxTokens|MaxCompletionTokens|DeepSeek'
./internal/gateway/ ok.
Broken by hand: deepseek_schema.go back at main, test kept:
TestDeepSeekRelayChatGetsMaxTokens FAIL "upstream got
{...max_completion_tokens:393216...}".
Not tried: a real request through AI Flash API.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-06 05:42:22 +08:00
yetone 62020b9c26 gateway: a chat request to DeepSeek that asks max_completion_tokens reaches it as max_tokens, not ignored at DeepSeek's 8K default (#2355, h4rk8s)
DeepSeek's chat API reads the reply length from max_tokens only (its
create-chat-completion docs list no max_completion_tokens). A Chat client
that asks OpenAI's way, as DSH's pi-ai does for a custom provider at
magpie's address, had the field passed through as it came, so DeepSeek
fell back to its default: the reporter's non-thinking turn asked 393216
and stopped at length after 8192.

deepseekTokenField (deepseek_schema.go), in forwardOnce beside
deepseekToolPatterns, renames the top-level key in place when the
provider is the deepseek preset or api.deepseek.com and the wire is Chat.
Only the key's bytes change. A request that names max_tokens too goes as
it is, since DeepSeek reads that one. Other vendors' chat APIs are not
touched (OpenAI's and Azure's want max_completion_tokens; Bedrock/Azure
keep asCompletionTokens). A request translated from another protocol
already went out as max_tokens (buildChat), so passthrough was the only
path that missed it.

Tested: TestDeepSeekChatGetsMaxTokens, through New().Handler() with the
deepseek preset on a fake upstream: max_completion_tokens (also with
spaces around the colon) arrives as max_tokens; max_tokens alone and both
fields go as sent; a custom provider gets max_completion_tokens
unchanged; a message whose text says "max_completion_tokens" keeps it.
Broken by hand: the forwardOnce call removed -> FAIL "upstream got
{...max_completion_tokens":393216...}"; the DeepSeek scope dropped -> FAIL
on relay/m; the max_tokens-present guard dropped -> FAIL on the both-
fields case. go test -run 'DeepSeek|MaxOutput|Tokens|Bedrock|Azure|Chat'
./internal/gateway ok; gofmt clean; vet darwin/linux/windows on gateway
ok. Not tried against the real DeepSeek API: the owner's key answers 402
Insufficient Balance to both fields.

Full suite: SUITE_LINE
2026-10-15 23:08:03 +08:00
yetone 97b4174a50 gateway: a Grok model is sent a tool's ^[^\s[\]]+$ as Rust's regex reads it, not turned away as "is not a regex" (#2317, x121381)
x121381 runs grok-4.6 from a third-party API key in Claude Code (Claude
for Windows). Every turn came back 400 from upstream: "Invalid request
content: Schema validation failed: /properties/breakpoints/items/oneOf/1/
properties/class_name/allOf/1/pattern: "^[^\\s[\\]]+$" is not a "regex""
(and the same for selector). The pattern is valid ECMAScript, which JSON
Schema's pattern is; a "[" inside a class is a plain character there. In
Rust's regex syntax it opens a nested class, so the class is never closed
and the pattern doesn't parse; "&&", "--" and "~~" in a class are set
operations there too.

grokToolPatterns (forwardOnce, beside deepseekToolPatterns) escapes those
characters inside a class of every tool schema's "pattern" when the model
named in the body is Grok's (grokModel: grok-*, xai/grok-*, x-ai/grok-*)
or the provider is api.x.ai. The escaped character is the same character
to ECMAScript, so the tool means what it meant. Patterns are spliced in
place (toolPatterns, the walker deepseekToolPatterns used, now taking the
fix as a function), so the rest of the body, its key order and a cache's
prefix, stays byte for byte; other models' tools go as they came.

Tests: TestECMAClassForRust (the reporter's pattern, POSIX-looking
[[:alpha:]], &&, --, ~~, escapes and text outside a class);
TestGrokToolPatterns2317 builds the schema path the error names
(breakpoints.items.oneOf[1].class_name/selector.allOf[1].pattern) and
checks only the two patterns change, for grok-4.6, xai/grok-4.7,
x-ai/grok-4 and api.x.ai by any model name;
TestGrokToolPatternsOtherModelsAsTheyCame keeps gpt/claude/deepseek/kimi
bodies and a Grok body with nothing to fix byte for byte.

Run: gofmt -l internal/gateway clean; go vet -tags nogui ./internal/gateway
clean. NOT run: go test (the disk had 208 MB free, under the 1 GB the
session needs before a test run), and nothing broken by hand yet. Before
landing: go test -tags nogui -p 1 -run 'TestECMAClassForRust|TestGrokToolPatterns|TestDeepSeekToolPatterns|TestUnicodeNullEscape' ./internal/gateway/,
then drop the grokToolPatterns call in forwardOnce / the in-class "["
case and watch TestGrokToolPatterns2317 go red. Not tried against xAI or
the reporter's relay; that Rust regex syntax is what xAI checks with is
read from the error, not from xAI's docs.

Full suite: SUITE_LINE
2026-10-15 23:08:02 +08:00
Luis Araya f362d755af perf(gateway): normalize null escapes in DeepSeek tool patterns via gjson (#490)
* fix(gateway): normalize null escapes in DeepSeek tool patterns via gjson

Claude Code sends an Artifact tool schema containing ECMAScript null
escape '^[^\0]*$', which DeepSeek's schema validator rejects with HTTP 400
('Invalid schema for function ... is not a regex').

Normalize standalone \0 escapes to \u0000 in tool schemas when routing to
DeepSeek presets or custom providers targeting api.deepseek.com. Use gjson
to splice only the normalized tools array into the body, avoiding full
unmarshal/marshal overhead on multi-megabyte payloads.

Closes #370

* perf(gateway): replace pattern strings in place via sjson

* perf(gateway): splice pattern replacements using absolute gjson Index

Replace sjson path traversal with backward splicing using gjson's absolute
Index and Raw length. This drops the sjson dependency entirely, correctly
handles arbitrary property names (such as a\\b, :x, @prefix, or |pipe), and
prevents HTML-entity escaping on replacement strings.

* perf(gateway): construct fresh buffer for pattern replacements

Sort targets ascending by Index and copy forward into a fresh allocation.
This avoids reusing the caller's backing array when body has spare capacity,
preventing in-place mutation and corruption of the original slice.
2026-10-02 20:43:21 +08:00