Skip to content

fix(xai): map x_search, fill tool descriptions, and isolate protocol cache - #182

Closed
ChenYCL wants to merge 1 commit into
caidaoli:masterfrom
ChenYCL:feat/xai-compat-session-protocol
Closed

ChenYCL wants to merge 1 commit into
caidaoli:masterfrom
ChenYCL:feat/xai-compat-session-protocol

Conversation

@ChenYCL

@ChenYCL ChenYCL commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Four local overlay fixes that belong on official master:

  1. Grok x_search → web_search
    Grok Responses emit x_search / x_search_call. Codex/OpenAI clients only accept web_search, web_search_2025_08_26, function, mcp, file_search, computer_use_preview, code_interpreter. Rewrite those types on the client-bound xAI OAuth Codex stream so serde does not fail.

  2. GPT-5 max_completion_tokens
    Anthropic → OpenAI Chat Completions still copied max_tokens. Azure GPT-5.x / o-series reject it. Convert gpt-5* / o1* / o3* / o4* to max_completion_tokens, and retry once on unsupported_parameter + max_tokens.

  3. Protocol capability per session
    Auto protocol learning is keyed by session (Claude Code session/thread, OpenCode session, Responses execution, then token hash). One desktop session that probed Codex on Grok no longer pins another session.

  4. Missing Claude tools[].description
    Grok's CLI proxy requires the key. Fill from name (or type) when the field is absent. Existing descriptions stay.

Test

go test -tags sonic ./internal/app -run 'TestRewriteXAIResponsesToolTypesForClient|TestWrapXAIClientToolTypeRewriteSSE|TestOpenAIRetryBodyForMaxTokens|TestProtocolCapabilityCacheIsSessionScoped|TestProtocolCapabilitySessionIDFromHeaders|TestProtocolCapabilityCacheClearChannelsKeepsOtherChannels'
go test -tags sonic ./internal/protocol/cliproxy/util -run TestEnsureClaudeToolDescription
go test -tags sonic ./internal/protocol/cliproxy/openai/claude -run TestConvertClaudeRequestToOpenAI_GPT5UsesMaxCompletionTokens

…cache

Grok CLI Responses emit type x_search, which Codex/OpenAI serde rejects.
Rewrite x_search/x_search_call to web_search on the client-bound stream.
GPT-5 Chat Completions reject max_tokens; emit max_completion_tokens on
convert and retry once after unsupported_parameter. Protocol capability
learning is keyed by session so one client cannot poison another.
Anthropic tools without description get a name-based fill for Grok serde.
caidaoli added a commit that referenced this pull request Oct 16, 2026
…tokens, keep learned protocol on request-scoped misses

- xAI Responses: map x_search_call/x_search to web_search_call/web_search
  unless the client declared x_search itself; SSE event lines follow.
- xAI Anthropic Messages: fill missing tools[].description only for
  cli-chat-proxy.grok.com / api.x.ai hosts.
- OpenAI Chat: rename max_tokens to max_completion_tokens for gpt-5/o
  families before BodyRules; retry once on explicit max_tokens rejection.
- Protocol capability cache: a fallback after a local translation miss
  no longer overwrites the learned protocol.

Reworks PR #182 at the root causes.

Co-authored-by: ChenYCL <aa34913@gmail.com>
@caidaoli

Copy link
Copy Markdown
Owner

Thanks for this — all four fixes landed in ac80274, reworked at their root causes (you're credited as co-author):

  1. x_search → web_search: rewritten only when the client did not declare x_search itself; SSE event lines follow the payload type.
  2. max_completion_tokens: renamed before send for gpt-5/o-series OpenAI Chat requests (before channel BodyRules), plus a one-shot retry when the upstream explicitly rejects max_tokens. Kept in the app wire layer; the cliproxy snapshot stays untouched.
  3. Protocol cache: a fallback after a local translation miss no longer overwrites the learned protocol, instead of introducing a per-session cache.
  4. Tool descriptions: filled only for xAI hosts (cli-chat-proxy.grok.com / api.x.ai); official Anthropic server tools reject extra fields.

Not ported: hiding internal x_search custom_tool_call traces (CLIProxyAPI f0f13cfd). Closing in favor of that commit.

@caidaoli caidaoli closed this Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants