程序员阿江(Relakkes) cbef0360e2 fix(grok): align the Responses request with what xAI actually accepts
Four gaps against the official Grok CLI surface, found by comparing this
adapter with sub2api's and each confirmed by a probe against the live
endpoint.

The first is not a parity gap but a hard failure. A named tool_choice was
translated into Chat Completions syntax, `{type:'function',function:{name}}`,
which the Responses endpoint refuses:

    422 data did not match any variant of untagged enum ModelToolChoice

so every request where the caller pins a tool failed outright. Responses
names the function inline. Only the Responses transform changes; the Chat
transform keeps the nested form, which is correct there.

The rest:

- A tool_choice sent with no tools, or one whose target the BatchTool
  filter removed, is dropped rather than left selecting a tool the upstream
  never receives — and a tool list that filters down to empty is omitted
  instead of sent as `[]`.
- `prompt_cache_key` and the `x-grok-conv-id` header carry one identity per
  conversation, reusing resolvePromptCacheKey (the Claude Code session id)
  so a turn stops re-billing the whole prefix. Both survive the 401 refresh
  retry: header assembly is folded into one helper so the first attempt and
  the retry cannot drift apart.
- `x-grok-client-mode: interactive` matches what the official client sends.

Probes, in order: baseline 200; the three new fields together 200; the
inline tool_choice 200; the nested form 422.

Also lands the adapter-level regression test for the mid-stream socket
reset fixed in the previous commit — it drives a real reset through
buildGrokFetch and the SSE transform, so it needs both changes present.
2026-07-27 02:13:11 +08:00
..