feat: add Grok support #183
🎯 Changes#
Adding Grok to the adapter set.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm run test:pr.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
New Features
- Added Grok (xAI) adapter with support for chat completions, image generation, and text summarization.
- Supports multiple Grok models including Grok 4.1, 4, 3, and 3-mini variants.
- Enables streaming responses and structured output capabilities.
- Integrated into example applications for immediate use.
Documentation
- Added comprehensive Grok adapter documentation covering installation, setup, configuration, and usage examples.
- 1
View your CI Pipeline Execution ↗ for commit 12fa430885c79b543850fb9c15e071f51b77a092
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 2m 50s View ↗ nx run-many --targets=build --exclude=examples/**✅ Succeeded 1m 6s View ↗
☁️ Nx Cloud last updated this comment at
2026-01-07 09:22:04UTC@tanstack/ai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@183@tanstack/ai-anthropic
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@183@tanstack/ai-client
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@183@tanstack/ai-devtools-core
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@183@tanstack/ai-gemini
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@183@tanstack/ai-grok
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok@183@tanstack/ai-ollama
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@183@tanstack/ai-openai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@183@tanstack/ai-preact
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-preact@183@tanstack/ai-react
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@183@tanstack/ai-react-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react-ui@183@tanstack/ai-solid
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@183@tanstack/ai-solid-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid-ui@183@tanstack/ai-svelte
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@183@tanstack/ai-vue
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@183@tanstack/ai-vue-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue-ui@183@tanstack/react-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@183@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@183commit:
12fa430Jaaneek(GitHub profile, opens in a new tab) requested changes
@@ -0,0 +12,21 @@12+ max_input_tokens?: number13+ max_output_tokens?: number14+ context_window?: number15+ knowledge_cutoff?: string16+ pricing?: {17+ input: {18+ normal: number19+ cached?: number20+ }21+ output: {22+ normal: number23+ }24+ }25+ /**26+ * Type-level description of which provider options this model supports.27+ */28+ providerOptions?: TProviderOptions29+}30+31+const GROK_4_1_FAST_REASONING = {32+ name: 'grok-4.1-fast-reasoning',Suggested change32- name: 'grok-4.1-fast-reasoning',32+ name: 'grok-4-1-fast-reasoning',@@ -0,0 +31,21 @@31+const GROK_4_1_FAST_REASONING = {32+ name: 'grok-4.1-fast-reasoning',33+ context_window: 2_000_000,34+ supports: {35+ input: ['text', 'image'],36+ output: ['text'],37+ capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],38+ },39+ pricing: {40+ input: {41+ normal: 0.2,42+ cached: 0.05,43+ },44+ output: {45+ normal: 0.5,46+ },47+ },48+} as const satisfies ModelMeta<GrokTextProviderOptions>49+50+const GROK_4_1_FAST_NON_REASONING = {51+ name: 'grok-4.1-fast-non-reasoning',Suggested change51- name: 'grok-4.1-fast-non-reasoning',51+ name: 'grok-4-1-fast-non-reasoning',Jaaneek(GitHub profile, opens in a new tab) requested changes
@@ -0,0 +185,21 @@185+ context_window: 32_768,186+ supports: {187+ input: ['text', 'image'],188+ output: ['text'],189+ capabilities: ['structured_outputs', 'tool_calling'],190+ },191+ pricing: {192+ input: {193+ normal: 2,194+ },195+ output: {196+ normal: 10,197+ },198+ },199+} as const satisfies ModelMeta<GrokTextProviderOptions>200+201+const GROK_2_IMAGE = {202+ name: 'grok-2-image-1212',203+ supports: {204+ input: ['text'],205+ output: ['text'],Suggested change205- output: ['text'],205+ output: ['image'],Jaaneek(GitHub profile, opens in a new tab) requested changes
@@ -0,0 +102,21 @@102+const GROK_4_FAST_NON_REASONING = {103+ name: 'grok-4-fast-non-reasoning',104+ context_window: 2_000_000,105+ supports: {106+ input: ['text', 'image'],107+ output: ['text'],108+ capabilities: ['structured_outputs', 'tool_calling'],109+ },110+ pricing: {111+ input: {112+ normal: 0.2,113+ cached: 0.05,114+ },115+ output: {116+ normal: 0.5,117+ },118+ },119+} as const satisfies ModelMeta120+121+const GROK_4_0709 = {122+ name: 'grok-4-0709',Can we just name it grok-4? its an alias for grok-4-0709
jherr changed the title
Actionable comments posted: 8
🤖 Fix all issues with AI agents
In @packages/typescript/ai-grok/package.json: - Around line 49-51: The peer dependency for "@tanstack/ai" in package.json uses the incorrect specifier "workspace:^"; update the version specifier to "workspace:*" so it follows monorepo convention. Locate the "peerDependencies" block in packages/typescript/ai-grok/package.json and replace the "@tanstack/ai": "workspace:^" entry with "@tanstack/ai": "workspace:*". - Around line 45-48: The package.json is missing vitest in devDependencies even though the "test:lib" script runs "vitest run"; add "vitest": "^4.0.14" to the "devDependencies" object in packages/typescript/ai-grok/package.json so the test tool is installed consistently with other adapter packages (match the version used by ai-vue, ai, ai-solid, ai-gemini, ai-anthropic, ai-client). In @packages/typescript/ai-grok/README.md: - Around line 27-42: The example uses the old pattern of passing model to generate(); update it so the model is passed into the adapter factory (grokText) instead and remove the model field from generate() call: call grokText('grok-3') (or equivalent factory signature) to create the adapter and then call generate({ adapter, messages }) without a separate model parameter, ensuring the adapter instance carries the model info. - Around line 62-79: The example uses the old pattern of passing model to generateImages; update it to pass the model into the adapter factory instead: call grokImage with the model option (e.g., grokImage({ model: 'grok-2-image-1212' })) and remove the model property from the generateImages() call so generateImages is invoked with adapter, prompt, numberOfImages, size, etc.; update the README example to reflect grokImage(...) and generateImages({ adapter, prompt, numberOfImages, size }). - Around line 44-60: The README example uses the old pattern of passing model to summarize(); update it to the new adapter architecture by creating the adapter with the model (call grokSummarize with the model name, e.g. grokSummarize('grok-3')) and then call summarize() without the model option, keeping text and style as before; locate grokSummarize and summarize in the snippet and move the model argument into the grokSummarize(...) adapter factory call and remove the model property from the summarize(...) invocation. - Around line 81-87: The README example for createGrokText is missing the required model positional parameter; update the example call to pass the model name as the first argument and the API key as the second argument (e.g., call createGrokText with a model identifier like "grok-1" followed by the API key) so the function signature (createGrokText(model, apiKey)) is satisfied. In @packages/typescript/ai-grok/src/adapters/text.ts: - Around line 397-406: The image branch only treats part.source.value as a URL; update the block that builds the Grok image part (the code that pushes { type: 'image_url', image_url: { url: ..., detail: ... } }) to handle both ContentPartSource types by checking part.source.type: if 'url' use part.source.value directly, if 'data' convert the base64 payload into a data URI (e.g., "data:<mime>;base64,<value>") before assigning to image_url.url; derive the MIME from part.metadata or part.source (fall back to "image/jpeg") and preserve the existing GrokImageMetadata.detail logic when creating the final parts entry.🧹 Nitpick comments (13)
docs/adapters/grok.md (1)
1-232: Documentation looks comprehensive and well-structured.The documentation covers all essential aspects including installation, usage patterns, configuration, API reference, and limitations. The implementation notes section explaining the Chat Completions vs Responses API choice is particularly helpful.
Minor grammar note: On line 163, consider hyphenating "tool-calling" for consistency with standard technical writing conventions (per static analysis hint).
packages/typescript/ai-grok/src/tools/function-tool.ts (1)
19-44: Implementation looks correct for Grok strict mode compatibility.The conversion logic properly handles schema transformations for Grok's strict mode requirements. The default empty schema fallback is a good defensive pattern.
Minor note:
additionalProperties = falseon line 34 is redundant sincemakeGrokStructuredOutputCompatiblealready sets this property (line 98 in schema-converter.ts). Consider removing for clarity, though it doesn't cause harm.packages/typescript/ai-grok/src/utils/client.ts (1)
40-45: Consider usingslice(2)for more consistent random suffix length.substring(7)onMath.random().toString(36)can occasionally produce short strings. Usingslice(2)would skip the "0." prefix and give a more consistent length.🔎 Proposed fix
export function generateId(prefix: string): string { - return `${prefix}-${Date.now()}-${Math.random().toString(36).substring(7)}` + return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2)}` }testing/panel/src/lib/model-selection.ts (1)
1-1: Consider extracting the Provider type to eliminate duplication.The
Providertype is duplicated inexamples/ts-react-chat/src/lib/model-selection.ts(line 1). Consider extracting this to a shared location to maintain a single source of truth and prevent the types from diverging during future updates.testing/panel/src/routes/api.chat.ts (1)
55-55: Consider extracting the Provider type to eliminate duplication.The
Providertype is defined in multiple locations (testing/panel/src/lib/model-selection.tsandexamples/ts-react-chat/src/lib/model-selection.ts). Extracting this to a shared type definition would ensure consistency across the codebase and reduce maintenance burden.packages/typescript/ai-grok/src/message-types.ts (1)
43-55: Consider using type aliases for empty metadata types.Biome flags these empty interfaces. Since they currently have no properties and TypeScript interfaces with no members are equivalent to
{}, consider converting to type aliases. However, if you anticipate extending these via declaration merging in the future, keeping them as interfaces is reasonable.🔎 Proposed fix using type aliases
-/** - * Metadata for Grok video content parts. - * Note: Video support in Grok is limited; check current API capabilities. - */ -export interface GrokVideoMetadata {} +/** + * Metadata for Grok video content parts. + * Note: Video support in Grok is limited; check current API capabilities. + */ +export type GrokVideoMetadata = Record<string, never> -/** - * Metadata for Grok document content parts. - * Note: Direct document support may vary; PDFs often need to be converted to images. - */ -export interface GrokDocumentMetadata {} +/** + * Metadata for Grok document content parts. + * Note: Direct document support may vary; PDFs often need to be converted to images. + */ +export type GrokDocumentMetadata = Record<string, never> -/** - * Metadata for Grok text content parts. - * Currently no specific metadata options for text in Grok. - */ -export interface GrokTextMetadata {} +/** + * Metadata for Grok text content parts. + * Currently no specific metadata options for text in Grok. + */ +export type GrokTextMetadata = Record<string, never>packages/typescript/ai-grok/src/image/image-provider-options.ts (1)
94-106: Consider removing or documenting the unused_modelparameter.The
_modelparameter is unused. If it's reserved for future model-specific validation, consider adding a brief comment. Otherwise, it could be removed.🔎 Option: Add documentation comment
/** * Validates that the number of images is within bounds for the model. + * @param _model - Reserved for future model-specific validation */ export function validateNumberOfImages( _model: string,packages/typescript/ai-grok/src/model-meta.ts (1)
216-226: Consider maintaining model order consistency.The array includes
GROK_3.namebeforeGROK_3_MINI.name(lines 223-224), but the constants are defined in reverse order (GROK_3_MINI at line 140, GROK_3 at line 159). While functionally correct, aligning the array order with the constant definitions or sorting by capability/release date would improve maintainability.packages/typescript/ai-grok/src/adapters/text.ts (5)
80-87: Replace console.error with structured logging or remove verbose error output.The error handling logs the full error details including stack traces to
console.error. In production, this could expose sensitive information and creates noise. Consider using a structured logger or removing the verbose logging since the error is re-thrown anyway.🔎 Proposed fix
} catch (error: unknown) { - const err = error as Error - console.error('>>> chatStream: Fatal error during response creation <<<') - console.error('>>> Error message:', err.message) - console.error('>>> Error stack:', err.stack) - console.error('>>> Full error:', err) throw error }
129-139: Consider handling empty response content gracefully.If
response.choices[0]?.message.contentisnullor empty,rawTextbecomes an empty string, which will fail JSON parsing with a less informative error. Consider adding explicit validation.🔎 Proposed fix
// Extract text content from the response const rawText = response.choices[0]?.message.content || '' + if (!rawText) { + throw new Error('Structured output response contained no content') + } + // Parse the JSON response let parsed: unknown try { parsed = JSON.parse(rawText) } catch {
149-154: Inconsistent error logging verbosity.The
structuredOutputmethod logs less detail thanchatStream(only message, not stack/full error). Consider making error handling consistent across methods, preferably by removing the console.error calls entirely as suggested earlier.
267-272: Minor: finishReason logic could be simplified.The condition
toolCallsInProgress.size > 0is checked twice (lines 234 and 269). Consider extracting to a variable for clarity.🔎 Proposed fix
// Handle finish reason if (choice.finish_reason) { + const hasToolCalls = choice.finish_reason === 'tool_calls' || toolCallsInProgress.size > 0 + // Emit all completed tool calls - if ( - choice.finish_reason === 'tool_calls' || - toolCallsInProgress.size > 0 - ) { + if (hasToolCalls) { for (const [index, toolCall] of toolCallsInProgress) { // ... existing code ... } } yield { type: 'done', // ... - finishReason: - choice.finish_reason === 'tool_calls' || - toolCallsInProgress.size > 0 - ? 'tool_calls' - : 'stop', + finishReason: hasToolCalls ? 'tool_calls' : 'stop', } }
275-288: Stream error handling uses console.log instead of console.error.Line 277 uses
console.logfor error output while the catch blocks inchatStreamandstructuredOutputuseconsole.error. For consistency and proper log levels, errors should useconsole.erroror be removed entirely.🔎 Proposed fix
} catch (error: unknown) { const err = error as Error & { code?: string } - console.log('[Grok Adapter] Stream ended with error:', err.message) yield { type: 'error',📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 9cb56b9084c18fa49393b93be933d490e531556c and 9038ecd6cbb84c6679471533b6b7ebaed6413888.
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (31)
.changeset/add-grok-adapter.mddocs/adapters/grok.mddocs/guides/migration.mdexamples/ts-react-chat/package.jsonexamples/ts-react-chat/src/lib/model-selection.tsexamples/ts-react-chat/src/routes/api.tanchat.tspackages/typescript/ai-grok/CHANGELOG.mdpackages/typescript/ai-grok/README.mdpackages/typescript/ai-grok/package.jsonpackages/typescript/ai-grok/src/adapters/image.tspackages/typescript/ai-grok/src/adapters/summarize.tspackages/typescript/ai-grok/src/adapters/text.tspackages/typescript/ai-grok/src/image/image-provider-options.tspackages/typescript/ai-grok/src/index.tspackages/typescript/ai-grok/src/message-types.tspackages/typescript/ai-grok/src/model-meta.tspackages/typescript/ai-grok/src/text/text-provider-options.tspackages/typescript/ai-grok/src/tools/function-tool.tspackages/typescript/ai-grok/src/tools/index.tspackages/typescript/ai-grok/src/tools/tool-converter.tspackages/typescript/ai-grok/src/utils/client.tspackages/typescript/ai-grok/src/utils/index.tspackages/typescript/ai-grok/src/utils/schema-converter.tspackages/typescript/ai-grok/tests/grok-adapter.test.tspackages/typescript/ai-grok/tsconfig.jsonpackages/typescript/ai-grok/vite.config.tspackages/typescript/smoke-tests/adapters/package.jsonpackages/typescript/smoke-tests/adapters/src/adapters/index.tstesting/panel/package.jsontesting/panel/src/lib/model-selection.tstesting/panel/src/routes/api.chat.ts
🧰 Additional context used
📓 Path-based instructions (7)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.{ts,tsx}: Use tree-shakeable adapter architecture for provider implementations - export specialized adapters (text, embedding, summarize, image) as separate imports from/adapterssubpath rather than monolithic adapters Use Zod for runtime schema validation and type inference, particularly for tool input/output definitions withtoolDefinition()and Zod schema inference Implement isomorphic tool system usingtoolDefinition()with.server()and.client()implementations for dual-environment execution Use type-safe per-model configuration with provider options typed based on selected model to ensure compile-time safety Implement stream processing with StreamProcessor for handling chunked responses and support partial JSON parsing for streaming AI responsesFiles:
packages/typescript/ai-grok/src/tools/index.tstesting/panel/src/routes/api.chat.tspackages/typescript/ai-grok/src/text/text-provider-options.tspackages/typescript/ai-grok/src/utils/client.tspackages/typescript/ai-grok/src/utils/schema-converter.tsexamples/ts-react-chat/src/lib/model-selection.tsexamples/ts-react-chat/src/routes/api.tanchat.tspackages/typescript/ai-grok/tests/grok-adapter.test.tspackages/typescript/ai-grok/src/message-types.tspackages/typescript/ai-grok/src/tools/function-tool.tspackages/typescript/ai-grok/src/utils/index.tspackages/typescript/ai-grok/src/tools/tool-converter.tspackages/typescript/ai-grok/src/adapters/image.tspackages/typescript/ai-grok/src/adapters/summarize.tspackages/typescript/ai-grok/vite.config.tspackages/typescript/smoke-tests/adapters/src/adapters/index.tspackages/typescript/ai-grok/src/model-meta.tspackages/typescript/ai-grok/src/index.tstesting/panel/src/lib/model-selection.tspackages/typescript/ai-grok/src/adapters/text.tspackages/typescript/ai-grok/src/image/image-provider-options.ts
**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (CLAUDE.md)
Use camelCase for function and variable names throughout the codebase
Files:
packages/typescript/ai-grok/src/tools/index.tstesting/panel/src/routes/api.chat.tspackages/typescript/ai-grok/src/text/text-provider-options.tspackages/typescript/ai-grok/src/utils/client.tspackages/typescript/ai-grok/src/utils/schema-converter.tsexamples/ts-react-chat/src/lib/model-selection.tsexamples/ts-react-chat/src/routes/api.tanchat.tspackages/typescript/ai-grok/tests/grok-adapter.test.tspackages/typescript/ai-grok/src/message-types.tspackages/typescript/ai-grok/src/tools/function-tool.tspackages/typescript/ai-grok/src/utils/index.tspackages/typescript/ai-grok/src/tools/tool-converter.tspackages/typescript/ai-grok/src/adapters/image.tspackages/typescript/ai-grok/src/adapters/summarize.tspackages/typescript/ai-grok/vite.config.tspackages/typescript/smoke-tests/adapters/src/adapters/index.tspackages/typescript/ai-grok/src/model-meta.tspackages/typescript/ai-grok/src/index.tstesting/panel/src/lib/model-selection.tspackages/typescript/ai-grok/src/adapters/text.tspackages/typescript/ai-grok/src/image/image-provider-options.ts
examples/**
📄 CodeRabbit inference engine (CLAUDE.md)
Examples are not built by Nx and should be run independently from their directories with
pnpm devorpnpm install && pnpm devFiles:
examples/ts-react-chat/package.jsonexamples/ts-react-chat/src/lib/model-selection.tsexamples/ts-react-chat/src/routes/api.tanchat.ts
**/*.test.ts
📄 CodeRabbit inference engine (CLAUDE.md)
Write unit tests using Vitest alongside source files with
.test.tsnaming conventionFiles:
packages/typescript/ai-grok/tests/grok-adapter.test.ts
packages/typescript/*/src/adapters/*.ts
📄 CodeRabbit inference engine (CLAUDE.md)
Create individual adapter implementations for each provider capability (text, embed, summarize, image) with separate exports to enable tree-shaking
Files:
packages/typescript/ai-grok/src/adapters/image.tspackages/typescript/ai-grok/src/adapters/summarize.tspackages/typescript/ai-grok/src/adapters/text.ts
packages/typescript/*/src/model-meta.ts
📄 CodeRabbit inference engine (CLAUDE.md)
Maintain model metadata files that define provider options and capabilities per model for per-model type safety
Files:
packages/typescript/ai-grok/src/model-meta.ts
packages/typescript/*/src/index.ts
📄 CodeRabbit inference engine (CLAUDE.md)
Export tree-shakeable adapters with clear subpath exports in package.json (e.g.,
@tanstack/ai/adapters,@tanstack/ai-openai/adapters) to minimize bundle sizeFiles:
packages/typescript/ai-grok/src/index.ts
🧠 Learnings (17)
📓 Common learnings
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Implement framework integrations using the headless `tanstack/ai-client` for state management with framework-specific hooks (useChat) on top📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to packages/typescript/*/src/index.ts : Export tree-shakeable adapters with clear subpath exports in package.json (e.g., `tanstack/ai/adapters`, `tanstack/ai-openai/adapters`) to minimize bundle sizeApplied to files:
.changeset/add-grok-adapter.mdpackages/typescript/ai-grok/src/tools/index.tstesting/panel/src/routes/api.chat.tsexamples/ts-react-chat/package.jsonexamples/ts-react-chat/src/routes/api.tanchat.tspackages/typescript/ai-grok/tests/grok-adapter.test.tspackages/typescript/ai-grok/README.mdpackages/typescript/ai-grok/src/tools/function-tool.tspackages/typescript/ai-grok/src/utils/index.tspackages/typescript/smoke-tests/adapters/package.jsonpackages/typescript/ai-grok/CHANGELOG.mdpackages/typescript/ai-grok/package.jsonpackages/typescript/ai-grok/src/adapters/image.tspackages/typescript/ai-grok/tsconfig.jsonpackages/typescript/ai-grok/vite.config.tspackages/typescript/smoke-tests/adapters/src/adapters/index.tspackages/typescript/ai-grok/src/index.tspackages/typescript/ai-grok/src/adapters/text.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to packages/typescript/*/src/adapters/*.ts : Create individual adapter implementations for each provider capability (text, embed, summarize, image) with separate exports to enable tree-shakingApplied to files:
.changeset/add-grok-adapter.mdpackages/typescript/ai-grok/src/tools/index.tsdocs/adapters/grok.mdtesting/panel/src/routes/api.chat.tspackages/typescript/ai-grok/src/text/text-provider-options.tsexamples/ts-react-chat/src/lib/model-selection.tsexamples/ts-react-chat/src/routes/api.tanchat.tspackages/typescript/ai-grok/tests/grok-adapter.test.tspackages/typescript/ai-grok/README.mdpackages/typescript/ai-grok/src/tools/function-tool.tspackages/typescript/ai-grok/src/utils/index.tspackages/typescript/smoke-tests/adapters/package.jsonpackages/typescript/ai-grok/src/tools/tool-converter.tspackages/typescript/ai-grok/package.jsonpackages/typescript/ai-grok/src/adapters/image.tspackages/typescript/ai-grok/tsconfig.jsonpackages/typescript/ai-grok/src/adapters/summarize.tspackages/typescript/smoke-tests/adapters/src/adapters/index.tspackages/typescript/ai-grok/src/index.tstesting/panel/src/lib/model-selection.tspackages/typescript/ai-grok/src/adapters/text.tspackages/typescript/ai-grok/src/image/image-provider-options.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to **/*.{ts,tsx} : Use tree-shakeable adapter architecture for provider implementations - export specialized adapters (text, embedding, summarize, image) as separate imports from `/adapters` subpath rather than monolithic adaptersApplied to files:
packages/typescript/ai-grok/src/tools/index.tstesting/panel/src/routes/api.chat.tsexamples/ts-react-chat/src/routes/api.tanchat.tspackages/typescript/ai-grok/tests/grok-adapter.test.tspackages/typescript/ai-grok/README.mdpackages/typescript/ai-grok/package.jsonpackages/typescript/ai-grok/src/adapters/image.tspackages/typescript/ai-grok/src/adapters/summarize.tspackages/typescript/smoke-tests/adapters/src/adapters/index.tspackages/typescript/ai-grok/src/index.tspackages/typescript/ai-grok/src/adapters/text.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to **/*.{ts,tsx} : Implement isomorphic tool system using `toolDefinition()` with `.server()` and `.client()` implementations for dual-environment executionApplied to files:
packages/typescript/ai-grok/src/tools/index.tspackages/typescript/ai-grok/src/tools/function-tool.tspackages/typescript/ai-grok/src/tools/tool-converter.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to packages/typescript/*/src/model-meta.ts : Maintain model metadata files that define provider options and capabilities per model for per-model type safetyApplied to files:
testing/panel/src/routes/api.chat.tspackages/typescript/ai-grok/src/text/text-provider-options.tsexamples/ts-react-chat/src/lib/model-selection.tspackages/typescript/ai-grok/src/message-types.tspackages/typescript/ai-grok/package.jsonpackages/typescript/ai-grok/src/adapters/image.tspackages/typescript/ai-grok/tsconfig.jsonpackages/typescript/ai-grok/src/model-meta.tspackages/typescript/ai-grok/src/index.tstesting/panel/src/lib/model-selection.tspackages/typescript/ai-grok/src/adapters/text.tspackages/typescript/ai-grok/src/image/image-provider-options.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to **/*.{ts,tsx} : Implement stream processing with StreamProcessor for handling chunked responses and support partial JSON parsing for streaming AI responsesApplied to files:
testing/panel/src/routes/api.chat.tspackages/typescript/ai-grok/src/adapters/text.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to **/*.{ts,tsx} : Use type-safe per-model configuration with provider options typed based on selected model to ensure compile-time safetyApplied to files:
packages/typescript/ai-grok/src/text/text-provider-options.tsexamples/ts-react-chat/src/lib/model-selection.tspackages/typescript/ai-grok/src/model-meta.tstesting/panel/src/lib/model-selection.tspackages/typescript/ai-grok/src/image/image-provider-options.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to packages/*/package.json : Use `workspace:*` protocol for internal package dependencies in package.json (e.g., `"tanstack/ai": "workspace:*"`)Applied to files:
examples/ts-react-chat/package.jsonpackages/typescript/smoke-tests/adapters/package.jsonpackages/typescript/ai-grok/package.jsontesting/panel/package.json
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to **/*.{ts,tsx} : Use Zod for runtime schema validation and type inference, particularly for tool input/output definitions with `toolDefinition()` and Zod schema inferenceApplied to files:
packages/typescript/ai-grok/src/utils/schema-converter.tspackages/typescript/ai-grok/src/tools/function-tool.tspackages/typescript/ai-grok/tsconfig.json
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to **/*.test.ts : Write unit tests using Vitest alongside source files with `.test.ts` naming conventionApplied to files:
packages/typescript/ai-grok/tests/grok-adapter.test.tspackages/typescript/ai-grok/tsconfig.jsonpackages/typescript/ai-grok/vite.config.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Maintain type safety through multimodal content support (image, audio, video, document) with model capability awarenessApplied to files:
packages/typescript/ai-grok/src/message-types.ts
📚 Learning: 2025-12-27T20:22:51.232Z
Learnt from: harry-whorlow Repo: TanStack/ai PR: 117 File: packages/typescript/ai-ollama/src/meta/model-meta-gpt-oss.ts:92-97 Timestamp: 2025-12-27T20:22:51.232Z Learning: In the ai-ollama package's model-meta files (packages/typescript/ai-ollama/src/meta/model-meta-*.ts), capability-related comments follow a standard template format across all files for consistency, even if the comment text doesn't precisely match individual model capabilities. This is an intentional design choice to maintain uniformity across the codebase.Applied to files:
packages/typescript/ai-grok/README.mdpackages/typescript/ai-grok/src/model-meta.tstesting/panel/src/lib/model-selection.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Implement framework integrations using the headless `tanstack/ai-client` for state management with framework-specific hooks (useChat) on topApplied to files:
packages/typescript/ai-grok/README.md
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to .eslintrc* : Use ESLint with custom TanStack config for linting all TypeScript and JavaScript filesApplied to files:
packages/typescript/ai-grok/package.jsonpackages/typescript/ai-grok/tsconfig.jsonpackages/typescript/ai-grok/vite.config.ts
📚 Learning: 2025-12-13T17:09:09.794Z
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Use Nx workspace with affected commands to optimize testing and building only changed packages and their dependentsApplied to files:
testing/panel/package.json
📚 Learning: 2025-12-27T21:39:29.563Z
Learnt from: harry-whorlow Repo: TanStack/ai PR: 117 File: packages/typescript/ai-ollama/src/meta/model-meta-llama-guard3.ts:70-75 Timestamp: 2025-12-27T21:39:29.563Z Learning: The standard template comments in ai-ollama model-meta files (like "Models with text, image, audio, video (no document)") should not be modified to match individual model capabilities, as they are intentionally kept uniform across all model-meta-*.ts files for consistency, regardless of what each specific model actually supports.Applied to files:
packages/typescript/ai-grok/src/model-meta.ts
🧬 Code graph analysis (4)
examples/ts-react-chat/src/lib/model-selection.ts (1)
testing/panel/src/lib/model-selection.ts (1)
Provider(1-1)
packages/typescript/ai-grok/tests/grok-adapter.test.ts (4)
packages/typescript/ai-grok/src/adapters/text.ts (2)
createGrokText(466-474)grokText(500-506)
packages/typescript/ai-grok/src/index.ts (6)
createGrokText(8-8)grokText(9-9)createGrokImage(27-27)grokImage(28-28)createGrokSummarize(17-17)grokSummarize(18-18)
packages/typescript/ai-grok/src/adapters/image.ts (2)
createGrokImage(138-144)grokImage(170-176)
packages/typescript/ai-grok/src/adapters/summarize.ts (2)
createGrokSummarize(136-142)grokSummarize(168-174)
testing/panel/src/lib/model-selection.ts (1)
examples/ts-react-chat/src/lib/model-selection.ts (1)
Provider(1-1)
packages/typescript/ai-grok/src/image/image-provider-options.ts (1)
packages/typescript/ai-grok/src/index.ts (2)
GrokImageProviderOptions(33-33)GrokImageModelProviderOptionsByName(34-34)
🪛 Biome (2.1.2)
packages/typescript/ai-grok/src/message-types.ts
[error] 43-43: An empty interface is equivalent to {}.
Safe fix: Use a type alias instead.
(lint/suspicious/noEmptyInterface)
[error] 49-49: An empty interface is equivalent to {}.
Safe fix: Use a type alias instead.
(lint/suspicious/noEmptyInterface)
[error] 55-55: An empty interface is equivalent to {}.
Safe fix: Use a type alias instead.
(lint/suspicious/noEmptyInterface)
🪛 LanguageTool
docs/adapters/grok.md
[grammar] ~163-~163: Use a hyphen to join words. Context: ...hich is essential for TanStack AI's tool calling functionality. The Responses API...
(QB_NEW_EN_HYPHEN)
🔇 Additional comments (59)
docs/guides/migration.md (1)
438-438: LGTM!Adding a trailing blank line is a standard formatting convention.
packages/typescript/smoke-tests/adapters/package.json (1)
17-17: LGTM!The dependency addition follows the correct
workspace:*protocol and maintains alphabetical ordering.packages/typescript/ai-grok/tsconfig.json (1)
1-9: LGTM!The TypeScript configuration follows the standard pattern for TanStack packages and correctly extends the root configuration.
packages/typescript/ai-grok/CHANGELOG.md (1)
1-7: The changelog correctly documents version 0.0.3 as shown in package.json. While 0.0.3 is unconventional for a typical initial release, it's the intentional version for this package and is consistent across all configuration files.packages/typescript/ai-grok/vite.config.ts (1)
1-36: LGTM!The Vite/Vitest configuration follows TanStack conventions with appropriate test setup, coverage configuration, and ESM-only build output.
packages/typescript/ai-grok/src/utils/schema-converter.ts (1)
12-35:transformNullsToUndefinedimplementation looks correct.The recursive null-to-undefined transformation with key omission handles the Grok structured output response normalization appropriately.
packages/typescript/ai-grok/src/utils/client.ts (2)
11-16: LGTM!The client creation correctly uses the OpenAI SDK with xAI's base URL, providing a clean abstraction for Grok API access.
22-38: Environment detection looks correct for isomorphic usage.The implementation properly handles both browser (
globalThis.window.env) and Node.js (process.env) environments, with a clear error message when the key is missing.packages/typescript/ai-grok/src/text/text-provider-options.ts (2)
13-52: Well-documented provider options types.The interfaces are cleanly structured with appropriate JSDoc documentation explaining each option's purpose and valid ranges.
72-77: Placeholder validation is acceptable for initial implementation.The no-op validation function is fine for now since Grok API will handle validation. Consider adding client-side validation for common errors (e.g., temperature range) in a follow-up to provide faster feedback.
testing/panel/package.json (1)
16-16: LGTM!The Grok package dependency follows the
workspace:*protocol consistently with other internal packages. Based on learnings, this is the correct approach for internal package dependencies.packages/typescript/ai-grok/src/tools/index.ts (1)
1-5: LGTM!The barrel export pattern enables tree-shaking by re-exporting individual utilities. This aligns with the tree-shakeable adapter architecture as per coding guidelines.
examples/ts-react-chat/package.json (1)
17-17: LGTM!The Grok package dependency correctly uses the
workspace:*protocol, consistent with other internal dependencies. Based on learnings, this is the correct approach.examples/ts-react-chat/src/lib/model-selection.ts (2)
1-1: LGTM!The Provider union type extension follows the established pattern and correctly includes 'grok' as a new provider option.
70-86: No changes required — Grok model identifiers are correct.All three model names (
grok-3,grok-3-mini,grok-2-vision-1212) match xAI's official API documentation as of 2025..changeset/add-grok-adapter.md (1)
5-5: Update changeset description and implementation to reflect actual available Grok models as of the PR date.The changeset claims "Grok 4.1, Grok 4, Grok 3" support, but as of January 2025, xAI's official API only provides Grok 2 family models. Grok 3 had not been released and missed its end-of-2024 deadline. The implementation references
grok-3andgrok-3-minimodel IDs that do not exist in the actual xAI API, which will cause failures when users attempt to use them. Either remove references to unreleased models or ensure the changeset and implementation match what is actually available in xAI's current API.⛔ Skipped due to learnings
Learnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Implement framework integrations using the headless `tanstack/ai-client` for state management with framework-specific hooks (useChat) on topLearnt from: CR Repo: TanStack/ai PR: 0 File: CLAUDE.md:0-0 Timestamp: 2025-12-13T17:09:09.794Z Learning: Applies to packages/typescript/*/src/adapters/*.ts : Create individual adapter implementations for each provider capability (text, embed, summarize, image) with separate exports to enable tree-shakingpackages/typescript/ai-grok/src/tools/tool-converter.ts (1)
1-17: LGTM!The tool conversion logic is clean and straightforward. The function correctly maps standard Tools to Grok's OpenAI-compatible function tool format using the delegated converter.
packages/typescript/ai-grok/tests/grok-adapter.test.ts (1)
1-99: LGTM!The test coverage appropriately validates adapter creation for all three Grok adapter types. Each test suite confirms:
- Explicit API key configuration works correctly
- Environment variable-based API key resolution functions as expected
- Proper error handling when API keys are missing
- Adapter properties (kind, name, model) are set correctly
The cleanup via
afterEachensures test isolation.testing/panel/src/lib/model-selection.ts (1)
71-86: LGTM!The Grok model options follow the existing pattern and provide a reasonable selection of Grok models (standard, mini, and vision variants).
testing/panel/src/routes/api.chat.ts (1)
176-179: LGTM!The Grok adapter configuration follows the established pattern used by other providers (Anthropic, Gemini, Ollama, OpenAI) and correctly uses
createChatOptionswith thegrokTextadapter.packages/typescript/ai-grok/src/utils/index.ts (1)
1-10: LGTM!The utility exports are well-organized and follow the tree-shakeable architecture. Grouping client utilities and schema converters separately makes the module structure clear and maintainable.
packages/typescript/smoke-tests/adapters/src/adapters/index.ts (4)
8-8: LGTM!The import follows the established tree-shakeable pattern, importing individual adapters (
grokImage,grokSummarize,grokText) rather than a monolithic adapter. This aligns with the coding guidelines.
81-84: LGTM!The Grok model constants follow the same pattern as other providers with environment variable overrides and sensible defaults.
168-186: LGTM!The
createGrokAdaptersfunction follows the established factory pattern used by other providers, correctly checking for theXAI_API_KEYand returningnullwhen unavailable.
217-222: LGTM!The Grok adapter registration is consistent with other providers in the registry.
examples/ts-react-chat/src/routes/api.tanchat.ts (3)
12-12: LGTM!The import follows the tree-shakeable pattern for the Grok text adapter.
22-22: LGTM!The
Providertype correctly extended to include'grok'.
94-97: LGTM!The Grok adapter configuration follows the established pattern. The simpler config (without additional
modelOptionsortemperature) is appropriate for the "thin shim approach" mentioned in the PR commits.packages/typescript/ai-grok/src/message-types.ts (3)
14-24: LGTM!The
GrokImageMetadatainterface properly defines thedetailoption for image processing control, consistent with the OpenAI-compatible API approach.
30-37: LGTM!The
GrokAudioMetadatainterface correctly defines supported audio formats.
61-67: LGTM!The composite
GrokMessageMetadataByModalityinterface properly maps modality types to their metadata types for type inference.packages/typescript/ai-grok/src/adapters/image.ts (5)
41-57: LGTM!The
GrokImageAdapterclass properly extendsBaseImageAdapterwith correct type parameters. The constructor correctly initializes the OpenAI-compatible client.
59-78: LGTM!The
generateImagesmethod follows a good pattern: validate inputs first, then build and execute the request. The validation calls ensure early failure with descriptive errors.
80-92: LGTM!The
buildRequestmethod correctly constructs the API payload, spreadingmodelOptionslast to allow user overrides of default values.
94-116: LGTM!The
transformResponsemethod properly handles the response transformation, including optional usage data with null-safe access.
138-176: LGTM!Both factory functions follow the established pattern:
createGrokImagefor explicit API key andgrokImagefor environment-based key detection. The JSDoc examples are helpful.packages/typescript/ai-grok/src/adapters/summarize.ts (5)
36-47: LGTM!The thin wrapper approach delegating to
GrokTextAdapteris a clean design that maximizes code reuse.
78-91: LGTM!The
summarizeStreammethod correctly delegates to the text adapter's streaming interface.
93-119: LGTM!The
buildSummarizationPromptmethod properly handles all summarization style options and builds a well-structured system prompt.
136-174: LGTM!Both factory functions follow the established pattern for explicit and environment-based API key handling.
49-76: The code is correct as written —chunk.contentprovides cumulative text, not deltas.The text adapter accumulates content deltas internally (line 188 in
processGrokStreamChunks:accumulatedContent += deltaContent) and yields chunks with the full accumulated text in thecontentfield (line 195). Thedeltafield is also available for reference if needed. Assigningsummary = chunk.contenton each iteration is the correct approach.packages/typescript/ai-grok/src/index.ts (1)
1-55: LGTM!The index barrel properly exports all public API surface with tree-shakeable structure. Types are correctly exported with the
typekeyword, and runtime constants are exported separately. This follows the coding guidelines for clear subpath exports.packages/typescript/ai-grok/src/image/image-provider-options.ts (3)
11-54: LGTM!The type definitions are well-structured with clear JSDoc documentation. The type maps enable per-model type safety as recommended in the coding guidelines.
68-89: LGTM!The
validateImageSizefunction properly validates sizes against model-specific constraints with descriptive error messages.
108-117: LGTM!The
validatePromptfunction properly validates prompt constraints with clear error messages.packages/typescript/ai-grok/src/model-meta.ts (6)
1-24: LGTM! Well-structured interface for model metadata.The
ModelMetainterface provides comprehensive type safety for model definitions with appropriate optional fields for capabilities that vary across models.
237-247: Type mapping follows the established pattern correctly.The
GrokModelInputModalitiesByNametype properly maps each model name to its input modalities using computed property types, enabling type-safe multimodal message construction. Based on learnings, this aligns with the standard template approach for model metadata files.
253-255: Consider extending provider options for models with unique capabilities.Currently all chat models map to the same
GrokProviderOptionstype. If any Grok models support unique parameters (e.g., reasoning-specific options for reasoning models), you may want to differentiate the options per model for stricter type safety. This is a future consideration as xAI's API evolves.
261-276: LGTM! Provider options follow OpenAI-compatible conventions.The
GrokProviderOptionsinterface correctly documents the OpenAI-compatible parameters with appropriate JSDoc comments for developer guidance.
286-298: LGTM! Type resolution helpers are well-designed.The
ResolveProviderOptionsandResolveInputModalitiesconditional types provide proper fallback behavior for unknown models while maintaining type safety for known models. This enables both strict typing for known models and graceful handling of custom/new models.
26-211: Grok model metadata is accurate and verified against current xAI documentation.All model constants, context windows, pricing, and capabilities match the current xAI API specifications as of January 2026. The structured approach with
as const satisfies ModelMetais appropriate for maintainability.packages/typescript/ai-grok/src/adapters/text.ts (8)
1-32: LGTM! Imports are well-organized and tree-shakeable.The imports correctly separate type-only imports (using
import type) from value imports, enabling proper tree-shaking. This follows the coding guidelines for tree-shakeable adapter architecture.
50-66: LGTM! Class structure follows the adapter pattern correctly.The
GrokTextAdapterclass properly extendsBaseTextAdapterwith correct generic type parameters for model-specific type safety. Thekindandnameproperties useas constfor literal typing.
157-289: Stream chunk processing implementation is thorough.The
processGrokStreamChunksmethod correctly handles:- Content delta accumulation
- Tool call streaming with argument chunking
- Finish reason handling with proper tool_calls detection
- Error recovery with graceful error chunk emission
The use of a Map to track in-progress tool calls by index is appropriate for handling interleaved tool call deltas.
294-342: LGTM! Options mapping is well-implemented.The
mapTextOptionsToGrokmethod correctly:- Validates provider options
- Converts tools to provider format
- Handles system prompts by joining them
- Maps common options (temperature, max_tokens, top_p) to OpenAI-compatible format
- Enables usage reporting via
stream_options
344-413: LGTM! Message conversion handles all message types correctly.The
convertMessageToGrokmethod properly handles:- Tool messages with
tool_call_id - Assistant messages with optional
tool_calls - User messages with multimodal content (text and image)
- Content serialization for non-string tool results
The optimization to use simple string format for text-only user messages is a good practice.
415-448: LGTM! Content normalization helpers are clean and correct.Both
normalizeContentandextractTextContentproperly handle all three content variants (null, string, ContentPart array) with appropriate type narrowing.
466-506: LGTM! Factory functions provide good DX with proper type inference.The
createGrokTextandgrokTextfactory functions:- Preserve generic type parameter for model-specific typing
- Provide clear JSDoc documentation with examples
- Support both explicit API key and environment variable patterns
This follows the tree-shakeable adapter architecture guidelines.
109-112: Potential issue:outputSchema.requiredmay be undefined.The code passes
outputSchema.required || []butmakeGrokStructuredOutputCompatiblereceives it as the second parameter. IfoutputSchemadoesn't have arequiredproperty defined, the fallback to empty array[]will make all properties optional in the transformed schema, which may not match the intended behavior.#!/bin/bash # Check the signature and implementation of makeGrokStructuredOutputCompatible ast-grep --pattern 'function makeGrokStructuredOutputCompatible($_) { $$$ }' # Also search with export rg -n "makeGrokStructuredOutputCompatible" --type ts -A 10@@ -0,0 +28,21 @@28+ "lint:fix": "eslint ./src --fix",29+ "test:build": "publint --strict",30+ "test:eslint": "eslint ./src",31+ "test:lib": "vitest run",32+ "test:lib:dev": "pnpm test:lib --watch",33+ "test:types": "tsc"34+ },35+ "keywords": [36+ "ai",37+ "grok",38+ "xai",39+ "tanstack",40+ "adapter"41+ ],42+ "dependencies": {43+ "openai": "^6.9.1"44+ },45+ "devDependencies": {46+ "@vitest/coverage-v8": "4.0.14",47+ "vite": "^7.2.7"48+ },⚠️ Potential issue | 🟡 Minor
🧩 Analysis chain
🏁 Script executed:
# Read the full package.json for ai-grok to see the test:lib script cat packages/typescript/ai-grok/package.jsonRepository: TanStack/ai
Length of output: 1187
🏁 Script executed:
# Check if vitest is listed as a devDependency in other packages in typescript folder fd -t f 'package.json' packages/typescript --exec grep -l '"vitest"' {} \; # Check root package.json grep '"vitest"' package.json | head -3Repository: TanStack/ai
Length of output: 328
🏁 Script executed:
# Check the actual structure - is there a monorepo setup with hoisted dependencies? head -30 package.jsonRepository: TanStack/ai
Length of output: 1366
Add
vitestto devDependencies.The
test:libscript runsvitest run, butvitestis not listed indevDependencies. All other adapter packages in the workspace (ai-vue, ai, ai-solid, ai-gemini, ai-anthropic, ai-client) explicitly includevitest(^4.0.14). Add it to maintain consistency and ensure reliable tooling availability during local development.🤖 Prompt for AI Agents
In @packages/typescript/ai-grok/package.json around lines 45 - 48, The package.json is missing vitest in devDependencies even though the "test:lib" script runs "vitest run"; add "vitest": "^4.0.14" to the "devDependencies" object in packages/typescript/ai-grok/package.json so the test tool is installed consistently with other adapter packages (match the version used by ai-vue, ai, ai-solid, ai-gemini, ai-anthropic, ai-client).@@ -0,0 +31,21 @@31+ "test:lib": "vitest run",32+ "test:lib:dev": "pnpm test:lib --watch",33+ "test:types": "tsc"34+ },35+ "keywords": [36+ "ai",37+ "grok",38+ "xai",39+ "tanstack",40+ "adapter"41+ ],42+ "dependencies": {43+ "openai": "^6.9.1"44+ },45+ "devDependencies": {46+ "@vitest/coverage-v8": "4.0.14",47+ "vite": "^7.2.7"48+ },49+ "peerDependencies": {50+ "@tanstack/ai": "workspace:^",51+ "zod": "^4.0.0"⚠️ Potential issue | 🔴 Critical
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash # Check peer dependency patterns in all ai-* packages echo "=== Checking @tanstack/ai peer dependencies across ai-* packages ===" fd -t f 'package.json' packages/typescript -x grep -H '@tanstack/ai' {} \; | grep -E 'workspace:' | sort echo -e "\n=== Checking all workspace: patterns across packages/typescript ===" fd -t f 'package.json' packages/typescript -x grep -H 'workspace:' {} \; | sortRepository: TanStack/ai
Length of output: 166
Change
workspace:^toworkspace:*for the@tanstack/aipeer dependency.Per project convention, internal package dependencies in the monorepo use
workspace:*(notworkspace:^). Update the peer dependency version specifier to match the pattern used across the monorepo.🤖 Prompt for AI Agents
In @packages/typescript/ai-grok/package.json around lines 49 - 51, The peer dependency for "@tanstack/ai" in package.json uses the incorrect specifier "workspace:^"; update the version specifier to "workspace:*" so it follows monorepo convention. Locate the "peerDependencies" block in packages/typescript/ai-grok/package.json and replace the "@tanstack/ai": "workspace:^" entry with "@tanstack/ai": "workspace:*".@@ -0,0 +22,21 @@22+23+## Usage24+25+### Text/Chat Adapter26+27+```typescript28+import { grokText } from '@tanstack/ai-grok'29+import { generate } from '@tanstack/ai'30+31+const adapter = grokText()32+33+const result = await generate({34+ adapter,35+ model: 'grok-3',36+ messages: [37+ { role: 'user', content: 'Explain quantum computing in simple terms' },38+ ],39+})40+41+console.log(result.text)42+```⚠️ Potential issue | 🟠 Major
Correct the API usage pattern to match the new adapter architecture.
The example shows the old API pattern where the model is passed separately to
generate(). According to the migration guide, the model should be passed to the adapter factory function, not as a separate parameter.🔎 Corrected example
import { grokText } from '@tanstack/ai-grok' import { generate } from '@tanstack/ai' -const adapter = grokText() +const adapter = grokText('grok-3') const result = await generate({ adapter, - model: 'grok-3', messages: [ { role: 'user', content: 'Explain quantum computing in simple terms' }, ], })Based on the migration guide: "Model is passed to adapter factory - The model name is now passed directly to the adapter function" and "No separate
modelparameter - The model is stored on the adapter, so you don't need to pass it separately."📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change27-```typescript28-import { grokText } from '@tanstack/ai-grok'29-import { generate } from '@tanstack/ai'30-31-const adapter = grokText()32-33-const result = await generate({34- adapter,35- model: 'grok-3',36- messages: [37- { role: 'user', content: 'Explain quantum computing in simple terms' },38- ],39-})40-41-console.log(result.text)42-```27+import { grokText } from '@tanstack/ai-grok'28+import { generate } from '@tanstack/ai'29+30+const adapter = grokText('grok-3')31+32+const result = await generate({33+ adapter,34+ messages: [35+ { role: 'user', content: 'Explain quantum computing in simple terms' },36+ ],37+})38+39+console.log(result.text)🤖 Prompt for AI Agents
In @packages/typescript/ai-grok/README.md around lines 27 - 42, The example uses the old pattern of passing model to generate(); update it so the model is passed into the adapter factory (grokText) instead and remove the model field from generate() call: call grokText('grok-3') (or equivalent factory signature) to create the adapter and then call generate({ adapter, messages }) without a separate model parameter, ensuring the adapter instance carries the model info.@@ -0,0 +40,21 @@40+41+console.log(result.text)42+```43+44+### Summarization Adapter45+46+```typescript47+import { grokSummarize } from '@tanstack/ai-grok'48+import { summarize } from '@tanstack/ai'49+50+const adapter = grokSummarize()51+52+const result = await summarize({53+ adapter,54+ model: 'grok-3',55+ text: 'Long article text...',56+ style: 'bullet-points',57+})58+59+console.log(result.summary)60+```⚠️ Potential issue | 🟠 Major
Correct the API usage pattern to match the new adapter architecture.
The example shows the old API pattern where the model is passed separately to
summarize(). The model should be passed to the adapter factory function.🔎 Corrected example
import { grokSummarize } from '@tanstack/ai-grok' import { summarize } from '@tanstack/ai' -const adapter = grokSummarize() +const adapter = grokSummarize('grok-3') const result = await summarize({ adapter, - model: 'grok-3', text: 'Long article text...', style: 'bullet-points', })🤖 Prompt for AI Agents
In @packages/typescript/ai-grok/README.md around lines 44 - 60, The README example uses the old pattern of passing model to summarize(); update it to the new adapter architecture by creating the adapter with the model (call grokSummarize with the model name, e.g. grokSummarize('grok-3')) and then call summarize() without the model option, keeping text and style as before; locate grokSummarize and summarize in the snippet and move the model argument into the grokSummarize(...) adapter factory call and remove the model property from the summarize(...) invocation.@@ -0,0 +59,21 @@59+console.log(result.summary)60+```61+62+### Image Generation Adapter63+64+```typescript65+import { grokImage } from '@tanstack/ai-grok'66+import { generateImages } from '@tanstack/ai'67+68+const adapter = grokImage()69+70+const result = await generateImages({71+ adapter,72+ model: 'grok-2-image-1212',73+ prompt: 'A beautiful sunset over mountains',74+ numberOfImages: 1,75+ size: '1024x1024',76+})77+78+console.log(result.images[0].url)79+```⚠️ Potential issue | 🟠 Major
Correct the API usage pattern to match the new adapter architecture.
The example shows the old API pattern where the model is passed separately to
generateImages(). The model should be passed to the adapter factory function.🔎 Corrected example
import { grokImage } from '@tanstack/ai-grok' import { generateImages } from '@tanstack/ai' -const adapter = grokImage() +const adapter = grokImage('grok-2-image-1212') const result = await generateImages({ adapter, - model: 'grok-2-image-1212', prompt: 'A beautiful sunset over mountains', numberOfImages: 1, size: '1024x1024', })📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change62-### Image Generation Adapter63-64-```typescript65-import { grokImage } from '@tanstack/ai-grok'66-import { generateImages } from '@tanstack/ai'67-68-const adapter = grokImage()69-70-const result = await generateImages({71- adapter,72- model: 'grok-2-image-1212',73- prompt: 'A beautiful sunset over mountains',74- numberOfImages: 1,75- size: '1024x1024',76-})77-78-console.log(result.images[0].url)79-```62+import { grokImage } from '@tanstack/ai-grok'63+import { generateImages } from '@tanstack/ai'64+65+const adapter = grokImage('grok-2-image-1212')66+67+const result = await generateImages({68+ adapter,69+ prompt: 'A beautiful sunset over mountains',70+ numberOfImages: 1,71+ size: '1024x1024',72+})73+74+console.log(result.images[0].url)🤖 Prompt for AI Agents
In @packages/typescript/ai-grok/README.md around lines 62 - 79, The example uses the old pattern of passing model to generateImages; update it to pass the model into the adapter factory instead: call grokImage with the model option (e.g., grokImage({ model: 'grok-2-image-1212' })) and remove the model property from the generateImages() call so generateImages is invoked with adapter, prompt, numberOfImages, size, etc.; update the README example to reflect grokImage(...) and generateImages({ adapter, prompt, numberOfImages, size }).@@ -0,0 +67,21 @@67+68+const adapter = grokImage()69+70+const result = await generateImages({71+ adapter,72+ model: 'grok-2-image-1212',73+ prompt: 'A beautiful sunset over mountains',74+ numberOfImages: 1,75+ size: '1024x1024',76+})77+78+console.log(result.images[0].url)79+```80+81+### With Explicit API Key82+83+```typescript84+import { createGrokText } from '@tanstack/ai-grok'85+86+const adapter = createGrokText('xai-your-api-key-here')87+```⚠️ Potential issue | 🟡 Minor
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash # Search for createGrokText function definition ast-grep --pattern 'export function createGrokText($$$) { $$$ }'Repository: TanStack/ai
Length of output: 166
🏁 Script executed:
#!/bin/bash # Also search for any export of createGrokText with different patterns rg -A 10 'createGrokText' packages/typescript/ai-grok --type ts --type tsx -B 2Repository: TanStack/ai
Length of output: 82
🏁 Script executed:
#!/bin/bash # Check the README file to see the full context and other examples cat -n packages/typescript/ai-grok/README.md | sed -n '70,100p'Repository: TanStack/ai
Length of output: 964
🏁 Script executed:
#!/bin/bash # Find TypeScript files in ai-grok package fd -e ts -e js packages/typescript/ai-grok/src --type fRepository: TanStack/ai
Length of output: 484
🏁 Script executed:
#!/bin/bash # Search for createGrokText in the codebase rg 'createGrokText' packages/typescript/ai-grok --type ts --type js -B 3 -A 10Repository: TanStack/ai
Length of output: 5897
🏁 Script executed:
#!/bin/bash # Look for index or main export files find packages/typescript/ai-grok -name 'index.ts' -o -name 'index.js' -o -name '*.ts' | head -20Repository: TanStack/ai
Length of output: 878
🏁 Script executed:
#!/bin/bash # Read the createGrokText function implementation cat -n packages/typescript/ai-grok/src/adapters/text.ts | sed -n '30,80p'Repository: TanStack/ai
Length of output: 1817
🏁 Script executed:
#!/bin/bash # Find the createGrokText function definition - search further down cat -n packages/typescript/ai-grok/src/adapters/text.ts | sed -n '100,160p'Repository: TanStack/ai
Length of output: 2474
🏁 Script executed:
#!/bin/bash # Find the createGrokText export function - search further down in the file cat -n packages/typescript/ai-grok/src/adapters/text.ts | sed -n '220,280p'Repository: TanStack/ai
Length of output: 2423
🏁 Script executed:
#!/bin/bash # Find the createGrokText export function - search near the end of the file wc -l packages/typescript/ai-grok/src/adapters/text.tsRepository: TanStack/ai
Length of output: 107
🏁 Script executed:
#!/bin/bash # Read the last part of the file where createGrokText should be defined tail -100 packages/typescript/ai-grok/src/adapters/text.ts | head -80Repository: TanStack/ai
Length of output: 2230
Correct the incomplete API key example by adding the required model parameter.
The example passes only the API key when the function requires both a model and an API key as positional parameters.
🔎 Corrected example
import { createGrokText } from '@tanstack/ai-grok' -const adapter = createGrokText('xai-your-api-key-here') +const adapter = createGrokText('grok-3', 'xai-your-api-key-here')🤖 Prompt for AI Agents
In @packages/typescript/ai-grok/README.md around lines 81 - 87, The README example for createGrokText is missing the required model positional parameter; update the example call to pass the model name as the first argument and the API key as the second argument (e.g., call createGrokText with a model identifier like "grok-1" followed by the API key) so the function signature (createGrokText(model, apiKey)) is satisfied.@@ -0,0 +386,21 @@386+ role: 'user',387+ content: contentParts[0].content,388+ }389+ }390+391+ // Otherwise, use array format for multimodal392+ const parts: Array<OpenAI_SDK.Chat.Completions.ChatCompletionContentPart> =393+ []394+ for (const part of contentParts) {395+ if (part.type === 'text') {396+ parts.push({ type: 'text', text: part.content })397+ } else if (part.type === 'image') {398+ const imageMetadata = part.metadata as GrokImageMetadata | undefined399+ parts.push({400+ type: 'image_url',401+ image_url: {402+ url: part.source.value,403+ detail: imageMetadata?.detail || 'auto',404+ },405+ })406+ }⚠️ Potential issue | 🟠 Major
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash # Search for ContentPart type definition rg -n "type ContentPart\|interface ContentPart" --type ts -A 20Repository: TanStack/ai
Length of output: 37
🏁 Script executed:
#!/bin/bash # Search for image source type definitions rg -n "type.*Source\|interface.*Source" --type ts -B 2 -A 10 | head -100Repository: TanStack/ai
Length of output: 37
🏁 Script executed:
#!/bin/bash # Check the text.ts file to understand context better cat -n packages/typescript/ai-grok/src/adapters/text.ts | sed -n '390,415p'Repository: TanStack/ai
Length of output: 922
🏁 Script executed:
#!/bin/bash # Search for GrokImageMetadata and related types rg -n "GrokImageMetadata\|ImageMetadata" --type ts -B 2 -A 10Repository: TanStack/ai
Length of output: 37
🏁 Script executed:
#!/bin/bash # Look for imports in text.ts to see where ContentPart comes from head -50 packages/typescript/ai-grok/src/adapters/text.tsRepository: TanStack/ai
Length of output: 1443
🏁 Script executed:
#!/bin/bash # Search for ContentPart in the codebase (case-insensitive, broader search) rg "ContentPart" --type ts | head -20Repository: TanStack/ai
Length of output: 1824
🏁 Script executed:
#!/bin/bash # Look for source type definitions in the codebase rg "type.*Source\|source.*:" --type ts -A 3 | head -50Repository: TanStack/ai
Length of output: 37
🏁 Script executed:
#!/bin/bash # Check if there are other image handling patterns in other adapters find packages/typescript -name "*.ts" -path "*/adapters/*" -type f | head -10Repository: TanStack/ai
Length of output: 591
🏁 Script executed:
#!/bin/bash # Find and display ContentPartSource type definition rg -n "export interface ContentPartSource|export type ContentPartSource" --type ts -A 15 packages/typescript/ai/src/types.tsRepository: TanStack/ai
Length of output: 461
🏁 Script executed:
#!/bin/bash # Find and display ContentPart type definition for image rg -n "source: ContentPartSource" --type ts -B 5 -A 2 packages/typescript/ai/src/types.tsRepository: TanStack/ai
Length of output: 1399
🏁 Script executed:
#!/bin/bash # Get more context around ContentPart type definitions sed -n '1,500p' packages/typescript/ai/src/types.ts | tail -200Repository: TanStack/ai
Length of output: 6817
🏁 Script executed:
#!/bin/bash # Check OpenAI adapter's image handling for comparison find packages/typescript -name "text.ts" -path "*/adapters/*" -exec grep -l "image" {} \;Repository: TanStack/ai
Length of output: 310
🏁 Script executed:
#!/bin/bash # Look at OpenAI adapter's text handling for images rg -n "part.type === 'image'" --type ts -B 3 -A 10 packages/typescript/ai-openai/src/adapters/text.tsRepository: TanStack/ai
Length of output: 37
🏁 Script executed:
#!/bin/bash # Search for image_url handling in OpenAI adapter rg -n "image_url\|image" --type ts -A 5 -B 2 packages/typescript/ai-openai/src/adapters/text.ts | head -60Repository: TanStack/ai
Length of output: 37
🏁 Script executed:
#!/bin/bash # Check if there's any base64 or data URI handling in the codebase rg -n "base64\|data:" --type ts | grep -i imageRepository: TanStack/ai
Length of output: 37
🏁 Script executed:
#!/bin/bash # Look for ContentPartSource handling patterns rg -n "source.type" --type ts -B 2 -A 5 | head -80Repository: TanStack/ai
Length of output: 5181
Add support for base64 image sources in addition to URL sources.
The
ContentPartSourceinterface supports bothtype: 'data'(base64-encoded) andtype: 'url'. The current implementation only handles URL sources directly. Whenpart.source.type === 'data', the value is base64-encoded and must be converted to a data URI (e.g.,data:image/jpeg;base64,<value>) before passing to OpenAI's image_url field.See how Anthropic and Gemini adapters handle this by checking
part.source.typeand converting accordingly.🤖 Prompt for AI Agents
In @packages/typescript/ai-grok/src/adapters/text.ts around lines 397 - 406, The image branch only treats part.source.value as a URL; update the block that builds the Grok image part (the code that pushes { type: 'image_url', image_url: { url: ..., detail: ... } }) to handle both ContentPartSource types by checking part.source.type: if 'url' use part.source.value directly, if 'data' convert the base64 payload into a data URI (e.g., "data:<mime>;base64,<value>") before assigning to image_url.url; derive the MIME from part.metadata or part.source (fall back to "image/jpeg") and preserve the existing GrokImageMetadata.detail logic when creating the final parts entry.@@ -0,0 +71,21 @@71+ properties[propName] = {72+ ...prop,73+ items: makeGrokStructuredOutputCompatible(74+ prop.items,75+ prop.items.required || [],76+ ),77+ }78+ } else if (wasOptional) {79+ // Make optional fields nullable by adding null to the type80+ if (prop.type && !Array.isArray(prop.type)) {81+ properties[propName] = {82+ ...prop,83+ type: [prop.type, 'null'],84+ }85+ } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {86+ properties[propName] = {87+ ...prop,88+ type: [...prop.type, 'null'],89+ }90+ }91+ }⚠️ Potential issue | 🟠 Major
Optional nested objects/arrays won't be made nullable.
The
else ifchain means that if a property is anobjectorarray, thewasOptionalcheck on line 78 is never reached. This results in optional nested objects/arrays not havingnulladded to their type union, which could cause issues when Grok returnsnullfor an optional nested field.🔎 Proposed fix to handle nullable nested types
// Recursively transform nested objects/arrays if (prop.type === 'object' && prop.properties) { - properties[propName] = makeGrokStructuredOutputCompatible( + let transformed = makeGrokStructuredOutputCompatible( prop, prop.required || [], ) + if (wasOptional) { + transformed = { ...transformed, type: ['object', 'null'] } + } + properties[propName] = transformed } else if (prop.type === 'array' && prop.items) { - properties[propName] = { + let transformed = { ...prop, items: makeGrokStructuredOutputCompatible( prop.items, prop.items.required || [], ), } + if (wasOptional) { + transformed = { ...transformed, type: ['array', 'null'] } + } + properties[propName] = transformed } else if (wasOptional) {AlemTuzlak approved these changes
AlemTuzlak merged this7c40941
AlemTuzlak closed this
Sign in to join this conversation.
Important
Review skipped#
Review was skipped due to path filters
:no_entry: Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yamlCodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including
**/dist/**will override the default block on thedistdirectory, by removing the pattern from both the lists.You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.📝 Walkthrough
Walkthrough#
This PR introduces comprehensive support for xAI's Grok models by adding a new
@tanstack/ai-grokpackage to the TanStack AI framework. The package provides three adapters (text/chat, summarization, image generation), complete with model metadata, provider configuration, utility helpers for schema transformation and API client setup, and integration examples across documentation, demo apps, and test suites.Changes#
packages/typescript/ai-grok/src/adapters/text.ts,image.ts,summarize.tsGrokTextAdapter,GrokImageAdapter, andGrokSummarizeAdapterclasses extending base adapters. Text adapter handles streaming chat and structured output with message/schema conversion; image adapter validates and transforms image generation requests; summarize adapter wraps text adapter with custom prompting. Factory functions (createGrok*,grok*) provide explicit API key or environment-based initialization.packages/typescript/ai-grok/src/model-meta.ts,message-types.tsGROK_CHAT_MODELSandGROK_IMAGE_MODELSarrays, and provides conditional types for resolving model-specific provider options and input modalities. Introduces modality metadata interfaces (image, audio, video, document, text).packages/typescript/ai-grok/src/text/text-provider-options.ts,image/image-provider-options.tsGrokTextProviderOptions,GrokImageProviderOptions) with fields like temperature, quality, response format, and image sizes. Exports validation functions for image size, number of images, and prompt length constraints.packages/typescript/ai-grok/src/utils/client.ts,schema-converter.ts,tools/function-tool.ts,tools/tool-converter.tscreateGrokClient(OpenAI SDK wrapper),getGrokApiKeyFromEnv(XAI_API_KEY retrieval),generateId(unique ID generation),makeGrokStructuredOutputCompatible(recursive schema transformation for JSON mode),transformNullsToUndefined(null→undefined conversion), andconvertToolsToProviderFormat(tool standardization).packages/typescript/ai-grok/package.json,tsconfig.json,vite.config.ts,src/index.ts@tanstack/aiandzod, OpenAI SDK dependency, TypeScript/Vite build config, and central barrel export consolidating all adapters, types, and utilities for tree-shaking.packages/typescript/ai-grok/tests/grok-adapter.test.ts.changeset/add-grok-adapter.md,docs/adapters/grok.md,docs/guides/migration.mdexamples/ts-react-chat/package.json,src/lib/model-selection.ts,src/routes/api.tanchat.ts@tanstack/ai-grokworkspace dependency, extendsProvidertype to include'grok', introduces Grok model options (grok-3, grok-3-mini, grok-2-vision-1212), and registers Grok in chat route with default model fallback.packages/typescript/smoke-tests/adapters/package.json,src/adapters/index.ts,testing/panel/package.json,src/lib/model-selection.ts,src/routes/api.chat.tsgrokTextadapter.Sequence Diagrams#
Estimated code review effort#
🎯 4 (Complex) | ⏱️ ~65 minutes
This PR introduces a substantial new package with interconnected adapters, dense type system logic for model metadata and conditional type resolution, complex schema transformation utilities, and integration across multiple example applications. The effort is elevated by the density of new type definitions, streaming logic in the text adapter, recursive schema conversion, and the need to verify consistency across the adapter hierarchy and factory function patterns.
Possibly related PRs#
Suggested reviewers#
Poem#
Pre-merge checks and finishing touches#
✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share