Files
magpie/docs
yetone 5fa908c087 gateway, provider: Claude Desktop's requests to ModelScope go on its Responses API, which reports the cache and reasoning that its Chat API leaves out (#1578, sparrowbits)
What changed
- provider.ResponsesTellsCache: ModelScope's API-Inference (the
  "modelscope" preset, or a provider whose Responses URL is
  api-inference.modelscope.cn) with both a Chat and a Responses URL.
  ResponsesFirst is true there for every model, so `usable` puts
  Responses before Chat. A translated request (Claude Desktop / Claude
  Code's /v1/messages, a Gemini client) goes to /responses, and
  Provider.Native wires Pi, omp and droid to Responses. A Chat client's
  request is still relayed on Chat, and Codex's on Responses, as before.
- forwardTranslated: when Responses is first only for the cache report,
  a 404 there marks the model unfit on Responses and asks it on Chat. A
  ModelScope model with no Responses route then works as it did before.
  This fallback is scoped to ResponsesTellsCache.
- docs/subsystems/gateway-routing.md: the "One try" row names it.

Why
The reporter's usage.jsonl covers 202 Claude Desktop requests, all
/v1/messages -> /v1/chat/completions: 0 cache_read over 34.9M input
tokens, and reasoning 0/202. Codex on /v1/responses with the same model,
on the same day, read 94% from cache. Their direct calls show why.
ModelScope's /responses answers
input_tokens_details.cached_tokens = 12288 on the second identical
prompt. Its /chat/completions answers only prompt_tokens /
completion_tokens / total_tokens, though it is served from the same
cache. magpie already maps Chat's prompt_tokens_details.cached_tokens
(and prompt_cache_hit_tokens, cached_tokens, cache_read_input_tokens) to
cache_read_input_tokens (cUsage.usage). The new test
TestOtherChatAndResponsesKeepChat pins that. So the missing figure was
not a mapping gap: ModelScope's Chat API never sends one. The fix is to
ask on the API that does.

Scope: other presets with both Chat and Responses URLs keep Chat.
- DeepSeek, xAI, Zhipu, MiMo, Qianfan, Tencent TokenHub, Volcengine's
  plans, OpenCode Zen/Go, Command Code, PipeLLM and CherryIn have an
  Anthropic URL, so Claude-format requests are relayed on Messages as
  they are.
- Groq's and Volcengine Ark's Chat APIs report
  prompt_tokens_details.cached_tokens, and Groq's Responses API is a
  partial beta.
- OpenAI keeps its existing OpenAI-model rule.
- Nothing is known of a custom relay's Responses API.

What was run
- New tests in internal/gateway/responses_tells_cache_test.go, against
  an httptest fake that answers with the reporter's literal usage
  bodies:
  - TestModelScopeMessagesTellTheCache: streamed and whole Messages go
    to /v1/responses, and the client gets cache_read_input_tokens 12288,
    input_tokens 108 and output_tokens 32.
  - TestModelScopeModelNotOnResponsesGoesToChat: a 404 on Responses
    goes to Chat, and stays on Chat for the next request.
  - TestOtherChatAndResponsesKeepChat
  - TestModelScopeNativeIsResponses
- Broken by hand:
  - Without the ResponsesTellsCache check in ResponsesFirst,
    TestModelScopeMessagesTellTheCache fails ("Messages went to
    [/v1/chat/completions], want /v1/responses"), and so do
    TestModelScopeNativeIsResponses and the fallback test.
  - Without the 404 fallback, TestModelScopeModelNotOnResponsesGoesToChat
    fails with "404 ModelScope: Not Found".
- gofmt -l is clean. go vet, and GOOS=linux and GOOS=windows go vet
  -tags nogui, pass on internal/gateway and internal/provider.
- Full suite: go test -tags nogui ./... under a temp HOME at base 1d016ee3f: rc=0, 48 packages ok.

Not tried
- Not tried against the real ModelScope API: no ModelScope token here.
  The fake is built from the reporter's bodies.
- Which ModelScope models lack a /responses route, and what ModelScope
  answers for them, isn't known. A 404 is assumed; any other error
  reaches the client as ModelScope said it.
- Reasoning is counted from Responses' output_tokens_details, but the
  reporter's excerpt doesn't show that field, so the test doesn't assert
  on it.
2026-10-11 13:46:38 +08:00
..