@warlock.js/ai-xai 4.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +125 -0
- package/cjs/index.cjs +225 -0
- package/cjs/index.cjs.map +1 -0
- package/esm/config.type.d.mts +108 -0
- package/esm/config.type.d.mts.map +1 -0
- package/esm/index.d.mts +4 -0
- package/esm/index.mjs +4 -0
- package/esm/known-models.d.mts +83 -0
- package/esm/known-models.d.mts.map +1 -0
- package/esm/known-models.mjs +96 -0
- package/esm/known-models.mjs.map +1 -0
- package/esm/sdk.d.mts +108 -0
- package/esm/sdk.d.mts.map +1 -0
- package/esm/sdk.mjs +126 -0
- package/esm/sdk.mjs.map +1 -0
- package/llms-full.txt +128 -0
- package/llms.txt +9 -0
- package/package.json +39 -0
- package/skills/setup-xai/SKILL.md +118 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: setup-xai
|
|
3
|
+
description: 'Wire @warlock.js/ai-xai — new XaiSDK({apiKey, baseURL?, provider?, pricing?}) for xAI Grok, .model({name, vision?, reasoning?, structuredOutput?}) for a ModelContract, .embedder({name, dimensions?}) and .image({name, pricing?}) delegated to the OpenAI-compatible adapter. Thin wrapper over OpenAISDK: uses the xAI OpenAI-compatible endpoint (https://api.x.ai/v1) and injects xAI''s own vision/reasoning inference so Grok names resolve to the right capabilities. Triggers: `XaiSDK`, `grok`, `grok-4`, `grok-3`, `grok-3-mini`, `grok-2-vision`, `.model`, `x.ai`, `api.x.ai`, `XAI_API_KEY`, `reasoning_effort` on Grok, "wire xai/grok into a warlock agent", "configure grok-4", "use grok reasoning effort", "send an image to grok-4 / grok-2-vision"; typical import `import { XaiSDK } from "@warlock.js/ai-xai"`. Skip: agent wiring — `@warlock.js/ai/run-ai-agent/SKILL.md`; the underlying OpenAI-compatible adapter and its full capability/streaming/error surface — `@warlock.js/ai-openai/skills/setup-openai/SKILL.md`; adapter comparison — `@warlock.js/ai/pick-ai-provider/SKILL.md`; competing adapters `@warlock.js/ai-anthropic`, `@warlock.js/ai-google`, `@warlock.js/ai-ollama`; raw `openai` SDK pointed at x.ai by hand.'
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `@warlock.js/ai-xai`
|
|
7
|
+
|
|
8
|
+
Provider adapter for xAI's **Grok** models. xAI speaks the OpenAI Chat Completions protocol, so `XaiSDK` is a **thin wrapper** over [`OpenAISDK`](../../../ai-openai/skills/setup-openai/SKILL.md) — it does not reimplement the wire layer. It constructs one internal `OpenAISDK` pointed at xAI's `baseURL` and labeled `provider: "xai"`, then delegates `model()` / `embedder()` / `image()` / `count()`, injecting xAI's own capability inference so Grok model names resolve correctly. Pair with `@warlock.js/ai` for the agent / tool / system-prompt surface.
|
|
9
|
+
|
|
10
|
+
## Construction
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { XaiSDK } from "@warlock.js/ai-xai";
|
|
14
|
+
|
|
15
|
+
const xai = new XaiSDK({ apiKey: process.env.XAI_API_KEY! });
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
- `baseURL` defaults to `https://api.x.ai/v1` (the xAI OpenAI-compatible endpoint). Override only to target a proxy/gateway.
|
|
19
|
+
- `provider` defaults to `"xai"` — flows through to `ModelContract.provider`, `AgentReport.model.provider`, and logs. Set it to relabel a gateway upstream.
|
|
20
|
+
- Every other upstream `ClientOptions` value (`timeout`, `maxRetries`, `defaultHeaders`, `fetch`, …) is forwarded verbatim to the wrapped client.
|
|
21
|
+
|
|
22
|
+
`XaiSDK` is a class (not a factory) — it holds a long-lived wrapped `OpenAISDK` (which holds a long-lived `OpenAI` client). Construct one per account and reuse it.
|
|
23
|
+
|
|
24
|
+
## Producing a model
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
xai.model({ name: "grok-4" }) // vision + reasoning auto-true
|
|
28
|
+
xai.model({ name: "grok-3-mini" }) // reasoning auto-true, vision false
|
|
29
|
+
xai.model({ name: "grok-2-vision" }) // vision auto-true
|
|
30
|
+
xai.model({ name: "grok-4", temperature: 0.2 }) // sampling controls
|
|
31
|
+
xai.model({ name: "grok-3", vision: true }) // explicit capability override
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Returns a `ModelContract` you pass straight into `ai.agent({ model })`.
|
|
35
|
+
|
|
36
|
+
## Models & capabilities — what's auto-set
|
|
37
|
+
|
|
38
|
+
This adapter ships xAI's OWN name lists (Grok ids don't match OpenAI's `gpt-*` / `o*` prefixes). Capabilities are injected per model before delegating; an explicit value always wins over inference.
|
|
39
|
+
|
|
40
|
+
| Model | `vision` | `reasoning` |
|
|
41
|
+
| --- | --- | --- |
|
|
42
|
+
| `grok-4` | true | true (reasoning-first) |
|
|
43
|
+
| `grok-3` | false | false |
|
|
44
|
+
| `grok-3-mini` | false | true (the "think" variant) |
|
|
45
|
+
| `grok-2-vision` | true | false |
|
|
46
|
+
| `grok-2` | false | false |
|
|
47
|
+
|
|
48
|
+
- `vision` — inferred `true` for the `grok-4` and `grok-2-vision` prefixes; `false` otherwise.
|
|
49
|
+
- `reasoning` — inferred `true` for `grok-4` and `grok-3-mini`; drives whether `reasoning.effort` is forwarded as `reasoning_effort` on the wire.
|
|
50
|
+
- `structuredOutput` / `promptCaching` and the image/PDF/audio wire mapping all come from the wrapped OpenAI adapter unchanged — see [`setup-openai`](../../../ai-openai/skills/setup-openai/SKILL.md).
|
|
51
|
+
|
|
52
|
+
**Override `vision`, `reasoning`, or `structuredOutput` explicitly** via `.model({ name, vision?, reasoning?, structuredOutput? })`.
|
|
53
|
+
|
|
54
|
+
> Model availability changes over time — pass any current Grok id through `.model({ name })`; the prefix inference covers dated / `-latest` / `-fast` / `-beta` variants. Don't assume a model exists; check xAI's docs.
|
|
55
|
+
|
|
56
|
+
## Reasoning (grok-4 / grok-3-mini)
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
const model = xai.model({ name: "grok-4" }); // reasoning auto-true
|
|
60
|
+
await model.complete(messages, { reasoning: { effort: "high" } }); // → reasoning_effort: "high"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`reasoning.effort` (`"low" | "medium" | "high"`) maps verbatim to the OpenAI-compatible `reasoning_effort` param. When `capabilities.reasoning` is `false` (e.g. `grok-3`), the option is dropped — the adapter never forwards it. Pin `reasoning: true` to force it for a custom/gateway id.
|
|
64
|
+
|
|
65
|
+
## Multipart messages (image input)
|
|
66
|
+
|
|
67
|
+
Image attachments reach the wire only on a vision-capable model (`grok-4`, `grok-2-vision`); the agent's modality gate throws otherwise. Mapping to OpenAI `image_url` parts is identical to the OpenAI adapter — see [`setup-openai`](../../../ai-openai/skills/setup-openai/SKILL.md).
|
|
68
|
+
|
|
69
|
+
## Uses the OpenAI-compatible endpoint
|
|
70
|
+
|
|
71
|
+
All wire work — request/response mapping, streaming, tool-call accumulation, structured output, usage extraction — is the wrapped `OpenAISDK`'s, unchanged. The wrapper only: (1) sets the xAI `baseURL` + `provider` defaults, and (2) injects xAI's vision/reasoning inference per model. For streaming, structured output, and the full multipart surface, read [`@warlock.js/ai-openai/skills/setup-openai/SKILL.md`](../../../ai-openai/skills/setup-openai/SKILL.md).
|
|
72
|
+
|
|
73
|
+
## Embeddings — not available on xAI
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
xai.embedder({ name: "text-embedding-3-small" }); // delegates, but xAI has no embeddings API yet
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
xAI does **not** currently expose a public embeddings endpoint. `embedder()` is wired for protocol parity, but a call to `embed()` / `embedMany()` fails upstream. Point an embedder at a dedicated provider (e.g. `@warlock.js/ai-openai`) for vectors.
|
|
80
|
+
|
|
81
|
+
## Image generation — delegated guard
|
|
82
|
+
|
|
83
|
+
`image()` delegates to the wrapped OpenAI Images adapter, which only recognizes `gpt-image-*` / `dall-e-*` ids and rejects anything else at construction. xAI's image generation ("Grok Imagine") is a separate, non-OpenAI-Images-compatible surface, so use `@warlock.js/ai-openai`'s `image()` for OpenAI-Images generation.
|
|
84
|
+
|
|
85
|
+
## Pricing — per-model registry
|
|
86
|
+
|
|
87
|
+
`pricing` is a registry keyed by model name, rates in **USD per 1,000,000 tokens** (`ModelPricing`: `input`, `output`, optional `cachedInput` / `cachedOutput`). Resolution is delegated: per-model `pricing` > SDK registry > `undefined` (no cost computed).
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
const xai = new XaiSDK({
|
|
91
|
+
apiKey,
|
|
92
|
+
pricing: {
|
|
93
|
+
"grok-4": { input: 3, output: 15 },
|
|
94
|
+
"grok-3-mini": { input: 0.3, output: 0.5 },
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const { usage } = await ai.agent({ model: xai.model({ name: "grok-4" }) }).execute("hi");
|
|
99
|
+
usage.cost; // per-channel USD breakdown of THIS run, or undefined when unpriced
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
> Pricing numbers above are illustrative placeholders — set the current xAI rates yourself.
|
|
103
|
+
|
|
104
|
+
## Errors
|
|
105
|
+
|
|
106
|
+
Raw xAI / OpenAI-SDK errors are wrapped into the typed `@warlock.js/ai` `AIError` hierarchy by the wrapped adapter's error wrapper (dispatch keys on `APIError.status + code`). See [`@warlock.js/ai/handle-ai-errors/SKILL.md`](@warlock.js/ai/handle-ai-errors/SKILL.md).
|
|
107
|
+
|
|
108
|
+
## When NOT to use this skill
|
|
109
|
+
|
|
110
|
+
- Direct calls to the `openai` SDK pointed at `api.x.ai` by hand — without going through `@warlock.js/ai` agents.
|
|
111
|
+
- Non-Grok models — OpenAI: `@warlock.js/ai-openai`. Anthropic: `@warlock.js/ai-anthropic`. Gemini: `@warlock.js/ai-google`. Ollama: `@warlock.js/ai-ollama`.
|
|
112
|
+
|
|
113
|
+
## See also
|
|
114
|
+
|
|
115
|
+
- [`@warlock.js/ai-openai/skills/setup-openai/SKILL.md`](../../../ai-openai/skills/setup-openai/SKILL.md) — the wrapped adapter; full wire / streaming / structured-output / multipart surface
|
|
116
|
+
- [`@warlock.js/ai/run-ai-agent/SKILL.md`](@warlock.js/ai/run-ai-agent/SKILL.md) — passing the model into `ai.agent({...})`
|
|
117
|
+
- [`@warlock.js/ai/pick-ai-provider/SKILL.md`](@warlock.js/ai/pick-ai-provider/SKILL.md) — adapter comparison
|
|
118
|
+
```
|