mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-22 00:05:51 +00:00
feat: add Grok (xAI) API adapter with custom model mapping (#152)
Add xAI Grok as a new API provider. Reuses OpenAI-compatible message/tool
converters and stream adapter with Grok-specific client and model mapping.
Default model mapping:
opus → grok-4.20-reasoning
sonnet → grok-3-mini-fast
haiku → grok-3-mini-fast
Users can customize mapping via:
- GROK_MODEL env var (override all)
- GROK_MODEL_MAP env var (JSON family map, e.g. {"opus":"grok-4"})
- GROK_DEFAULT_{FAMILY}_MODEL env vars
Activation: CLAUDE_CODE_USE_GROK=1 or modelType: "grok" in settings.json
Also integrates with /provider command for runtime switching.
This commit is contained in:
44
src/services/api/grok/__tests__/client.test.ts
Normal file
44
src/services/api/grok/__tests__/client.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, test, beforeEach, afterEach } from 'bun:test'
|
||||
import { getGrokClient, clearGrokClientCache } from '../client.js'
|
||||
|
||||
describe('getGrokClient', () => {
|
||||
const originalEnv = { ...process.env }
|
||||
|
||||
beforeEach(() => {
|
||||
clearGrokClientCache()
|
||||
process.env.GROK_API_KEY = 'test-key'
|
||||
delete process.env.GROK_BASE_URL
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
clearGrokClientCache()
|
||||
process.env = { ...originalEnv }
|
||||
})
|
||||
|
||||
test('creates client with default base URL', () => {
|
||||
const client = getGrokClient()
|
||||
expect(client).toBeDefined()
|
||||
expect(client.baseURL).toBe('https://api.x.ai/v1')
|
||||
})
|
||||
|
||||
test('uses GROK_BASE_URL when set', () => {
|
||||
process.env.GROK_BASE_URL = 'https://custom.grok.api/v1'
|
||||
clearGrokClientCache()
|
||||
const client = getGrokClient()
|
||||
expect(client.baseURL).toBe('https://custom.grok.api/v1')
|
||||
})
|
||||
|
||||
test('returns cached client on second call', () => {
|
||||
const client1 = getGrokClient()
|
||||
const client2 = getGrokClient()
|
||||
expect(client1).toBe(client2)
|
||||
})
|
||||
|
||||
test('clearGrokClientCache resets cache', () => {
|
||||
const client1 = getGrokClient()
|
||||
clearGrokClientCache()
|
||||
process.env.GROK_BASE_URL = 'https://other.api/v1'
|
||||
const client2 = getGrokClient()
|
||||
expect(client1).not.toBe(client2)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user