feat: migrate xAI provider to Responses API with reusable transform utils#11962
feat: migrate xAI provider to Responses API with reusable transform utils#11962carlesso wants to merge 3 commits intoRooCodeInc:mainfrom
Conversation
…tilities Migrate the xAI provider from the deprecated Chat Completions API to the Responses API, add grok-4.20 as the new default model, and introduce shared transform utilities for Responses API that other providers can adopt. New shared utilities (src/api/transform/): - responses-api-stream.ts: processResponsesApiStream() handles core Responses API stream events (text, reasoning, tool calls, usage) and createUsageNormalizer() provides configurable token/cost extraction. Designed for reuse by openai-native, openai-codex, or any future Responses API provider. - responses-api-input.ts: convertToResponsesApiInput() converts directly from Anthropic message format to Responses API input format, avoiding the intermediate Chat Completions conversion step. Handles input_text, input_image, function_call, and function_call_output mappings. xAI provider (src/api/providers/xai.ts): - Switch from client.chat.completions.create() to client.responses.create() - Use shared transform utilities for stream handling and input conversion - Enable reasoning traces via include: ["reasoning.encrypted_content"] - System prompt via instructions field, store: false for privacy - completePrompt() also migrated to Responses API Model updates (packages/types/src/providers/xai.ts): - Add grok-4.20 as the new default model (2M context, $2/$6 pricing) - Remove grok-4.20-beta-0309-reasoning and grok-4.20-beta-0309-non-reasoning
|
@roomote do a full review please and leave inline comments. |
Fixed the reported issues. All local checks (tests, lint, type-check) passed. |
There was a problem hiding this comment.
Good migration to the Responses API overall. The shared transform utilities are a solid pattern. I found several issues that need addressing -- mostly around tool schema handling, missing metadata pass-through, and a bug in the input converter for assistant string content.
Issues found:
-
mapResponseToolsbypasses base provider schema hardening (src/api/providers/xai.ts:52-64) - Doesn't useconvertToolSchemaForOpenAI(), doesn't handle MCP tools, setsstrict: falsefor all tools. -
metadata?.tool_choiceis ignored (src/api/providers/xai.ts:89) - Always uses"auto"instead of respecting caller'stool_choice. Also missingparallel_tool_calls. -
Assistant string content uses
input_textinstead ofoutput_text(src/api/transform/responses-api-input.ts:20-26) - For assistant role with string content, wraps asinput_textbut should useoutput_text/messageformat. -
modelInfoparameter unused (src/api/transform/responses-api-stream.ts:113) -createUsageNormalizeracceptsmodelInfobut never references it. -
Missing
cacheWriteTokensextraction (src/api/transform/responses-api-stream.ts:133) - Normalizer doesn't extractcache_creation_input_tokens. -
Pervasive
anytypes - Both shared utilities useanyextensively. Not blocking but worth noting. -
Inconsistent test function (
src/api/providers/__tests__/xai.spec.ts:85) - Usestest()while all others useit().
Will fix all of these.
|
@roomote fix it all |
- Use base provider convertToolSchemaForOpenAI() for tool schema hardening - Handle MCP tools with isMcpTool() (strict: false for MCP, true otherwise) - Respect metadata.tool_choice instead of always using "auto" - Add parallel_tool_calls pass-through from metadata - Fix assistant string content to use output_text format (not input_text) - Remove unused modelInfo param from createUsageNormalizer - Add cacheWriteTokens extraction to usage normalizer - Fix test() -> it() inconsistency in xai.spec.ts - Update tests to match all changes
Fixed the reported issues. Restored |
…eWriteTokens, simplify completePrompt
3c1500c to
6951133
Compare
Related GitHub Issue
Closes: #11961
Roo Code Task Context (Optional)
N/A
Description
Migrate the xAI provider from the deprecated Chat Completions API to the Responses API, add grok-4.20 as the new default model, and introduce shared transform utilities for Responses API that other providers can adopt.
New shared utilities (src/api/transform/):
xAI provider (src/api/providers/xai.ts):
Model updates (packages/types/src/providers/xai.ts):
Test Procedure
Tested locally
Pre-Submission Checklist
Screenshots / Videos
N/A
Documentation Updates
Does this PR necessitate updates to user-facing documentation?
Additional Notes
N/A
Get in Touch
Discord:
ecarlessoInteractively review PR in Roo Code Cloud