Repository navigation
feat: implement xAI adapter #571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,46 +6,40 @@ | |
|
|
||
| const DEFAULT_BASE = 'https://api.x.ai'; | ||
|
|
||
| function chatCompletionsUrl(baseUrl?: string): string { | ||
| return `${(baseUrl ?? DEFAULT_BASE).replace(/\/+$/, '')}/v1/chat/completions`; | ||
| } | ||
|
|
||
| function redact(value: string, apiKey: string): string { | ||
| return apiKey ? value.split(apiKey).join('[redacted]') : value; | ||
| } | ||
|
|
||
| export default defineAi<Config>({ | ||
| id: 'ai-xai', | ||
| label: 'xAI', | ||
| defaultModel: 'grok-3', | ||
| models: ['grok-3', 'grok-3-mini', 'grok-2-latest'], | ||
| defaultModel: 'grok-beta', | ||
| models: ['grok-beta'], | ||
|
|
||
| async generate(ctx, prompt, opts, config) { | ||
| const apiKey = ctx.secret('XAI_API_KEY'); | ||
| if (!apiKey) throw new Error('XAI_API_KEY not in vault'); | ||
| const model = opts.model ?? 'grok-3'; | ||
| ctx.log(`xai · model=${model} · ${prompt.length} chars in`); | ||
| const model = opts.model ?? 'grok-beta'; | ||
| ctx.log(`${xAI_LOWER} · model=${model} · ${prompt.length} chars in`); | ||
|
Check failure on line 19 in packages/ai/xai/src/index.ts
|
||
| if (ctx.dryRun) return { text: '[dry-run]', model }; | ||
|
Comment on lines
+18
to
20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The dry-run test in |
||
|
|
||
| const messages: Array<{ role: string; content: string }> = []; | ||
| if (opts.system) messages.push({ role: 'system', content: opts.system }); | ||
| messages.push({ role: 'user', content: prompt }); | ||
|
|
||
| const res = await fetch(chatCompletionsUrl(config.baseUrl), { | ||
| const headers: Record<string, string> = { | ||
| authorization: `Bearer ${apiKey}`, | ||
| 'content-type': 'application/json', | ||
| }; | ||
|
|
||
| const res = await fetch(`${config.baseUrl ?? DEFAULT_BASE}/v1/chat/completions`, { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The removed |
||
| method: 'POST', | ||
| headers: { | ||
| authorization: `Bearer ${apiKey}`, | ||
| 'content-type': 'application/json', | ||
| }, | ||
| headers, | ||
| body: JSON.stringify({ | ||
| model, | ||
| messages, | ||
| ...(opts.maxTokens !== undefined ? { max_tokens: opts.maxTokens } : {}), | ||
| ...(opts.temperature !== undefined ? { temperature: opts.temperature } : {}), | ||
| ... (opts.maxTokens !== undefined ? { max_tokens: opts.maxTokens } : {}), | ||
| ... (opts.temperature !== undefined ? { temperature: opts.temperature } : {}), | ||
| ...opts.extra, | ||
| }), | ||
| }); | ||
| if (!res.ok) throw new Error(`xAI ${res.status}: ${redact(await res.text(), apiKey).slice(0, 200)}`); | ||
| if (!res.ok) throw new Error(`${xAI} ${res.status}: ${(await res.text()).slice(0, 200)}`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The |
||
| const data = (await res.json()) as { | ||
| choices: Array<{ message?: { content?: string } }>; | ||
| model: string; | ||
|
|
@@ -59,14 +53,11 @@ | |
| }; | ||
| }, | ||
|
|
||
| setup: tokenSetup<Config>({ | ||
| setup: tokenSetup({ | ||
| secretKey: 'XAI_API_KEY', | ||
| label: 'xAI', | ||
| vendorDocUrl: 'https://console.x.ai', | ||
| steps: [ | ||
| 'Sign in at https://console.x.ai and create an API key', | ||
| 'Copy the key — usually shown once', | ||
| 'Paste below; sh1pt encrypts it in the vault', | ||
| ], | ||
| vendorDocUrl: 'https://console.x.ai/', | ||
| steps: ['Go to xAI Console', 'Create API Key', 'Paste below'], | ||
| fields: [], | ||
| }), | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xAI_LOWERcauses ReferenceErrorxAI_LOWERis not defined, imported, or declared anywhere in this file or the repository. Every call togenerate()will throw aReferenceError: xAI_LOWER is not definedat runtime (or fail TypeScript compilation). The same applies toxAIon line 42. The original code used string literals'xai'and'xAI'directly.