@ai-sdk/xai 4.0.0-beta.7 → 4.0.0-beta.75

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +660 -9
  2. package/README.md +2 -0
  3. package/dist/index.d.ts +213 -68
  4. package/dist/index.js +2074 -781
  5. package/dist/index.js.map +1 -1
  6. package/docs/01-xai.mdx +445 -54
  7. package/package.json +15 -15
  8. package/src/convert-to-xai-chat-messages.ts +48 -27
  9. package/src/convert-xai-chat-usage.ts +3 -3
  10. package/src/files/xai-files-api.ts +16 -0
  11. package/src/files/xai-files-options.ts +19 -0
  12. package/src/files/xai-files.ts +94 -0
  13. package/src/index.ts +9 -4
  14. package/src/map-xai-finish-reason.ts +2 -2
  15. package/src/realtime/index.ts +2 -0
  16. package/src/realtime/xai-realtime-event-mapper.ts +399 -0
  17. package/src/realtime/xai-realtime-model-options.ts +3 -0
  18. package/src/realtime/xai-realtime-model.ts +101 -0
  19. package/src/remove-additional-properties.ts +24 -0
  20. package/src/responses/convert-to-xai-responses-input.ts +100 -23
  21. package/src/responses/convert-xai-responses-usage.ts +3 -3
  22. package/src/responses/map-xai-responses-finish-reason.ts +3 -2
  23. package/src/responses/xai-responses-api.ts +31 -1
  24. package/src/responses/{xai-responses-options.ts → xai-responses-language-model-options.ts} +12 -7
  25. package/src/responses/xai-responses-language-model.ts +157 -60
  26. package/src/responses/xai-responses-prepare-tools.ts +10 -8
  27. package/src/tool/code-execution.ts +2 -2
  28. package/src/tool/file-search.ts +2 -2
  29. package/src/tool/mcp-server.ts +2 -2
  30. package/src/tool/view-image.ts +2 -2
  31. package/src/tool/view-x-video.ts +2 -2
  32. package/src/tool/web-search.ts +4 -2
  33. package/src/tool/x-search.ts +2 -2
  34. package/src/{xai-chat-options.ts → xai-chat-language-model-options.ts} +28 -13
  35. package/src/xai-chat-language-model.ts +65 -29
  36. package/src/xai-chat-prompt.ts +2 -1
  37. package/src/xai-error.ts +13 -3
  38. package/src/xai-image-model.ts +28 -11
  39. package/src/xai-prepare-tools.ts +9 -8
  40. package/src/xai-provider.ts +115 -19
  41. package/src/xai-speech-model-options.ts +55 -0
  42. package/src/xai-speech-model.ts +167 -0
  43. package/src/xai-transcription-model-options.ts +70 -0
  44. package/src/xai-transcription-model.ts +166 -0
  45. package/src/xai-video-model-options.ts +145 -0
  46. package/src/xai-video-model.ts +129 -22
  47. package/dist/index.d.mts +0 -377
  48. package/dist/index.mjs +0 -3070
  49. package/dist/index.mjs.map +0 -1
  50. package/src/xai-video-options.ts +0 -23
  51. /package/src/{xai-image-options.ts → xai-image-model-options.ts} +0 -0
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -10,6 +10,7 @@ export const webSearchArgsSchema = lazySchema(() =>
10
10
  z.object({
11
11
  allowedDomains: z.array(z.string()).max(5).optional(),
12
12
  excludedDomains: z.array(z.string()).max(5).optional(),
13
+ enableImageSearch: z.boolean().optional(),
13
14
  enableImageUnderstanding: z.boolean().optional(),
14
15
  }),
15
16
  ),
@@ -30,7 +31,7 @@ const webSearchOutputSchema = lazySchema(() =>
30
31
  ),
31
32
  );
32
33
 
33
- const webSearchToolFactory = createProviderToolFactoryWithOutputSchema<
34
+ const webSearchToolFactory = createProviderExecutedToolFactory<
34
35
  {},
35
36
  {
36
37
  query: string;
@@ -43,6 +44,7 @@ const webSearchToolFactory = createProviderToolFactoryWithOutputSchema<
43
44
  {
44
45
  allowedDomains?: string[];
45
46
  excludedDomains?: string[];
47
+ enableImageSearch?: boolean;
46
48
  enableImageUnderstanding?: boolean;
47
49
  }
48
50
  >({
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -34,7 +34,7 @@ const xSearchOutputSchema = lazySchema(() =>
34
34
  ),
35
35
  );
36
36
 
37
- const xSearchToolFactory = createProviderToolFactoryWithOutputSchema<
37
+ const xSearchToolFactory = createProviderExecutedToolFactory<
38
38
  {},
39
39
  {
40
40
  query: string;
@@ -2,18 +2,10 @@ import { z } from 'zod/v4';
2
2
 
3
3
  // https://docs.x.ai/docs/models
4
4
  export type XaiChatModelId =
5
- | 'grok-4-1-fast-reasoning'
6
- | 'grok-4-1-fast-non-reasoning'
7
- | 'grok-4-fast-non-reasoning'
8
- | 'grok-4-fast-reasoning'
9
- | 'grok-code-fast-1'
10
- | 'grok-4'
11
- | 'grok-4-0709'
12
- | 'grok-4-latest'
13
- | 'grok-3'
14
- | 'grok-3-latest'
15
- | 'grok-3-mini'
16
- | 'grok-3-mini-latest'
5
+ | 'grok-4.20-non-reasoning'
6
+ | 'grok-4.20-reasoning'
7
+ | 'grok-4.3'
8
+ | 'grok-latest'
17
9
  | (string & {});
18
10
 
19
11
  // search source schemas
@@ -58,7 +50,21 @@ const searchSourceSchema = z.discriminatedUnion('type', [
58
50
 
59
51
  // xai-specific provider options
60
52
  export const xaiLanguageModelChatOptions = z.object({
61
- reasoningEffort: z.enum(['low', 'high']).optional(),
53
+ /**
54
+ * Constrains how hard a reasoning model thinks before responding.
55
+ *
56
+ * - `none`: Disables reasoning entirely (supported by `grok-4.3` and newer
57
+ * reasoning models). When set, no thinking tokens are used.
58
+ * - `low` (default): Uses some reasoning tokens, but still fast.
59
+ * - `medium`: More thinking for less-latency-sensitive applications.
60
+ * - `high`: Uses more reasoning tokens for deeper thinking.
61
+ *
62
+ * Note: Not every Grok model accepts every value. Refer to xAI's docs for
63
+ * the values supported by your selected model.
64
+ *
65
+ * @see https://docs.x.ai/docs/guides/reasoning
66
+ */
67
+ reasoningEffort: z.enum(['none', 'low', 'medium', 'high']).optional(),
62
68
  logprobs: z.boolean().optional(),
63
69
  topLogprobs: z.number().int().min(0).max(8).optional(),
64
70
 
@@ -70,6 +76,15 @@ export const xaiLanguageModelChatOptions = z.object({
70
76
  */
71
77
  parallel_function_calling: z.boolean().optional(),
72
78
 
79
+ /**
80
+ * @deprecated xAI has deprecated Live Search (`search_parameters`) in favor
81
+ * of the Agent Tools API. Requests using this option now return a "Live
82
+ * search is deprecated" error. Use the `web_search` / `x_search` tools
83
+ * instead (e.g. `xai.tools.webSearch()`, `xai.tools.xSearch()`) with
84
+ * `xai.responses(modelId)`.
85
+ *
86
+ * @see https://docs.x.ai/docs/guides/tools/overview
87
+ */
73
88
  searchParameters: z
74
89
  .object({
75
90
  /**
@@ -1,25 +1,30 @@
1
1
  import {
2
2
  APICallError,
3
- LanguageModelV3,
4
- LanguageModelV3CallOptions,
5
- LanguageModelV3Content,
6
- LanguageModelV3FinishReason,
7
- LanguageModelV3GenerateResult,
8
- LanguageModelV3StreamPart,
9
- LanguageModelV3StreamResult,
10
- LanguageModelV3Usage,
11
- SharedV3Warning,
3
+ type LanguageModelV4,
4
+ type LanguageModelV4CallOptions,
5
+ type LanguageModelV4Content,
6
+ type LanguageModelV4FinishReason,
7
+ type LanguageModelV4GenerateResult,
8
+ type LanguageModelV4StreamPart,
9
+ type LanguageModelV4StreamResult,
10
+ type LanguageModelV4Usage,
11
+ type SharedV4Warning,
12
12
  } from '@ai-sdk/provider';
13
13
  import {
14
14
  combineHeaders,
15
15
  createEventSourceResponseHandler,
16
16
  createJsonResponseHandler,
17
17
  extractResponseHeaders,
18
- FetchFunction,
18
+ isCustomReasoning,
19
+ mapReasoningToProviderEffort,
19
20
  parseProviderOptions,
20
- ParseResult,
21
21
  postJsonToApi,
22
22
  safeParseJSON,
23
+ serializeModelOptions,
24
+ WORKFLOW_SERIALIZE,
25
+ WORKFLOW_DESERIALIZE,
26
+ type FetchFunction,
27
+ type ParseResult,
23
28
  } from '@ai-sdk/provider-utils';
24
29
  import { z } from 'zod/v4';
25
30
  import { convertToXaiChatMessages } from './convert-to-xai-chat-messages';
@@ -27,27 +32,41 @@ import { convertXaiChatUsage } from './convert-xai-chat-usage';
27
32
  import { getResponseMetadata } from './get-response-metadata';
28
33
  import { mapXaiFinishReason } from './map-xai-finish-reason';
29
34
  import {
30
- XaiChatModelId,
31
35
  xaiLanguageModelChatOptions,
32
- } from './xai-chat-options';
36
+ type XaiChatModelId,
37
+ } from './xai-chat-language-model-options';
33
38
  import { xaiFailedResponseHandler } from './xai-error';
34
39
  import { prepareTools } from './xai-prepare-tools';
35
40
 
36
41
  type XaiChatConfig = {
37
42
  provider: string;
38
43
  baseURL: string | undefined;
39
- headers: () => Record<string, string | undefined>;
44
+ headers?: () => Record<string, string | undefined>;
40
45
  generateId: () => string;
41
46
  fetch?: FetchFunction;
42
47
  };
43
48
 
44
- export class XaiChatLanguageModel implements LanguageModelV3 {
45
- readonly specificationVersion = 'v3';
49
+ export class XaiChatLanguageModel implements LanguageModelV4 {
50
+ readonly specificationVersion = 'v4';
46
51
 
47
52
  readonly modelId: XaiChatModelId;
48
53
 
49
54
  private readonly config: XaiChatConfig;
50
55
 
56
+ static [WORKFLOW_SERIALIZE](model: XaiChatLanguageModel) {
57
+ return serializeModelOptions({
58
+ modelId: model.modelId,
59
+ config: model.config,
60
+ });
61
+ }
62
+
63
+ static [WORKFLOW_DESERIALIZE](options: {
64
+ modelId: XaiChatModelId;
65
+ config: XaiChatConfig;
66
+ }) {
67
+ return new XaiChatLanguageModel(options.modelId, options.config);
68
+ }
69
+
51
70
  constructor(modelId: XaiChatModelId, config: XaiChatConfig) {
52
71
  this.modelId = modelId;
53
72
  this.config = config;
@@ -71,12 +90,13 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
71
90
  presencePenalty,
72
91
  stopSequences,
73
92
  seed,
93
+ reasoning,
74
94
  responseFormat,
75
95
  providerOptions,
76
96
  tools,
77
97
  toolChoice,
78
- }: LanguageModelV3CallOptions) {
79
- const warnings: SharedV3Warning[] = [];
98
+ }: LanguageModelV4CallOptions) {
99
+ const warnings: SharedV4Warning[] = [];
80
100
 
81
101
  // parse xai-specific provider options
82
102
  const options =
@@ -133,7 +153,23 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
133
153
  temperature,
134
154
  top_p: topP,
135
155
  seed,
136
- reasoning_effort: options.reasoningEffort,
156
+ reasoning_effort:
157
+ options.reasoningEffort ??
158
+ (isCustomReasoning(reasoning)
159
+ ? reasoning === 'none'
160
+ ? undefined
161
+ : mapReasoningToProviderEffort({
162
+ reasoning,
163
+ effortMap: {
164
+ minimal: 'low',
165
+ low: 'low',
166
+ medium: 'medium',
167
+ high: 'high',
168
+ xhigh: 'high',
169
+ },
170
+ warnings,
171
+ })
172
+ : undefined),
137
173
 
138
174
  // parallel function calling
139
175
  parallel_function_calling: options.parallel_function_calling,
@@ -202,8 +238,8 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
202
238
  }
203
239
 
204
240
  async doGenerate(
205
- options: LanguageModelV3CallOptions,
206
- ): Promise<LanguageModelV3GenerateResult> {
241
+ options: LanguageModelV4CallOptions,
242
+ ): Promise<LanguageModelV4GenerateResult> {
207
243
  const { args: body, warnings } = await this.getArgs(options);
208
244
 
209
245
  const url = `${this.config.baseURL ?? 'https://api.x.ai/v1'}/chat/completions`;
@@ -214,7 +250,7 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
214
250
  rawValue: rawResponse,
215
251
  } = await postJsonToApi({
216
252
  url,
217
- headers: combineHeaders(this.config.headers(), options.headers),
253
+ headers: combineHeaders(this.config.headers?.(), options.headers),
218
254
  body,
219
255
  failedResponseHandler: xaiFailedResponseHandler,
220
256
  successfulResponseHandler: createJsonResponseHandler(
@@ -237,7 +273,7 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
237
273
  }
238
274
 
239
275
  const choice = response.choices![0];
240
- const content: Array<LanguageModelV3Content> = [];
276
+ const content: Array<LanguageModelV4Content> = [];
241
277
 
242
278
  // extract text content
243
279
  if (choice.message.content != null && choice.message.content.length > 0) {
@@ -312,8 +348,8 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
312
348
  }
313
349
 
314
350
  async doStream(
315
- options: LanguageModelV3CallOptions,
316
- ): Promise<LanguageModelV3StreamResult> {
351
+ options: LanguageModelV4CallOptions,
352
+ ): Promise<LanguageModelV4StreamResult> {
317
353
  const { args, warnings } = await this.getArgs(options);
318
354
  const body = {
319
355
  ...args,
@@ -327,7 +363,7 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
327
363
 
328
364
  const { responseHeaders, value: response } = await postJsonToApi({
329
365
  url,
330
- headers: combineHeaders(this.config.headers(), options.headers),
366
+ headers: combineHeaders(this.config.headers?.(), options.headers),
331
367
  body,
332
368
  failedResponseHandler: xaiFailedResponseHandler,
333
369
  successfulResponseHandler: async ({ response }) => {
@@ -375,11 +411,11 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
375
411
  fetch: this.config.fetch,
376
412
  });
377
413
 
378
- let finishReason: LanguageModelV3FinishReason = {
414
+ let finishReason: LanguageModelV4FinishReason = {
379
415
  unified: 'other',
380
416
  raw: undefined,
381
417
  };
382
- let usage: LanguageModelV3Usage | undefined = undefined;
418
+ let usage: LanguageModelV4Usage | undefined = undefined;
383
419
  let isFirstChunk = true;
384
420
  const contentBlocks: Record<
385
421
  string,
@@ -394,7 +430,7 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
394
430
  stream: response.pipeThrough(
395
431
  new TransformStream<
396
432
  ParseResult<z.infer<typeof xaiChatChunkSchema>>,
397
- LanguageModelV3StreamPart
433
+ LanguageModelV4StreamPart
398
434
  >({
399
435
  start(controller) {
400
436
  controller.enqueue({ type: 'stream-start', warnings });
@@ -18,7 +18,8 @@ export interface XaiUserMessage {
18
18
 
19
19
  export type XaiUserMessageContent =
20
20
  | { type: 'text'; text: string }
21
- | { type: 'image_url'; image_url: { url: string } };
21
+ | { type: 'image_url'; image_url: { url: string } }
22
+ | { type: 'file'; file: { file_id: string } };
22
23
 
23
24
  export interface XaiAssistantMessage {
24
25
  role: 'assistant';
package/src/xai-error.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils';
2
2
  import { z } from 'zod/v4';
3
3
 
4
- // Add error schema and structure
5
- export const xaiErrorDataSchema = z.object({
4
+ const chatCompletionsErrorSchema = z.object({
6
5
  error: z.object({
7
6
  message: z.string(),
8
7
  type: z.string().nullish(),
@@ -11,9 +10,20 @@ export const xaiErrorDataSchema = z.object({
11
10
  }),
12
11
  });
13
12
 
13
+ const responsesErrorSchema = z.object({
14
+ code: z.string(),
15
+ error: z.string(),
16
+ });
17
+
18
+ export const xaiErrorDataSchema = z.union([
19
+ chatCompletionsErrorSchema,
20
+ responsesErrorSchema,
21
+ ]);
22
+
14
23
  export type XaiErrorData = z.infer<typeof xaiErrorDataSchema>;
15
24
 
16
25
  export const xaiFailedResponseHandler = createJsonErrorResponseHandler({
17
26
  errorSchema: xaiErrorDataSchema,
18
- errorToMessage: data => data.error.message,
27
+ errorToMessage: data =>
28
+ 'code' in data ? `${data.code}: ${data.error}` : data.error.message,
19
29
  });
@@ -1,38 +1,55 @@
1
- import { ImageModelV3, SharedV3Warning } from '@ai-sdk/provider';
1
+ import type { ImageModelV4, SharedV4Warning } from '@ai-sdk/provider';
2
2
  import {
3
3
  combineHeaders,
4
4
  convertImageModelFileToDataUri,
5
5
  createBinaryResponseHandler,
6
6
  createJsonResponseHandler,
7
7
  createStatusCodeErrorResponseHandler,
8
- FetchFunction,
9
8
  getFromApi,
10
9
  parseProviderOptions,
11
10
  postJsonToApi,
11
+ serializeModelOptions,
12
+ WORKFLOW_SERIALIZE,
13
+ WORKFLOW_DESERIALIZE,
14
+ type FetchFunction,
12
15
  } from '@ai-sdk/provider-utils';
13
16
  import { z } from 'zod/v4';
14
17
  import { xaiFailedResponseHandler } from './xai-error';
15
- import { xaiImageModelOptions } from './xai-image-options';
16
- import { XaiImageModelId } from './xai-image-settings';
18
+ import { xaiImageModelOptions } from './xai-image-model-options';
19
+ import type { XaiImageModelId } from './xai-image-settings';
17
20
 
18
21
  interface XaiImageModelConfig {
19
22
  provider: string;
20
23
  baseURL: string | undefined;
21
- headers: () => Record<string, string | undefined>;
24
+ headers?: () => Record<string, string | undefined>;
22
25
  fetch?: FetchFunction;
23
26
  _internal?: {
24
27
  currentDate?: () => Date;
25
28
  };
26
29
  }
27
30
 
28
- export class XaiImageModel implements ImageModelV3 {
29
- readonly specificationVersion = 'v3';
31
+ export class XaiImageModel implements ImageModelV4 {
32
+ readonly specificationVersion = 'v4';
30
33
  readonly maxImagesPerCall = 3;
31
34
 
32
35
  get provider(): string {
33
36
  return this.config.provider;
34
37
  }
35
38
 
39
+ static [WORKFLOW_SERIALIZE](model: XaiImageModel) {
40
+ return serializeModelOptions({
41
+ modelId: model.modelId,
42
+ config: model.config,
43
+ });
44
+ }
45
+
46
+ static [WORKFLOW_DESERIALIZE](options: {
47
+ modelId: XaiImageModelId;
48
+ config: XaiImageModelConfig;
49
+ }) {
50
+ return new XaiImageModel(options.modelId, options.config);
51
+ }
52
+
36
53
  constructor(
37
54
  readonly modelId: XaiImageModelId,
38
55
  private config: XaiImageModelConfig,
@@ -49,10 +66,10 @@ export class XaiImageModel implements ImageModelV3 {
49
66
  abortSignal,
50
67
  files,
51
68
  mask,
52
- }: Parameters<ImageModelV3['doGenerate']>[0]): Promise<
53
- Awaited<ReturnType<ImageModelV3['doGenerate']>>
69
+ }: Parameters<ImageModelV4['doGenerate']>[0]): Promise<
70
+ Awaited<ReturnType<ImageModelV4['doGenerate']>>
54
71
  > {
55
- const warnings: Array<SharedV3Warning> = [];
72
+ const warnings: Array<SharedV4Warning> = [];
56
73
 
57
74
  if (size != null) {
58
75
  warnings.push({
@@ -135,7 +152,7 @@ export class XaiImageModel implements ImageModelV3 {
135
152
  const currentDate = this.config._internal?.currentDate?.() ?? new Date();
136
153
  const { value: response, responseHeaders } = await postJsonToApi({
137
154
  url: `${baseURL}${endpoint}`,
138
- headers: combineHeaders(this.config.headers(), headers),
155
+ headers: combineHeaders(this.config.headers?.(), headers),
139
156
  body,
140
157
  failedResponseHandler: xaiFailedResponseHandler,
141
158
  successfulResponseHandler: createJsonResponseHandler(
@@ -1,16 +1,17 @@
1
1
  import {
2
- LanguageModelV3CallOptions,
3
- SharedV3Warning,
4
2
  UnsupportedFunctionalityError,
3
+ type LanguageModelV4CallOptions,
4
+ type SharedV4Warning,
5
5
  } from '@ai-sdk/provider';
6
- import { XaiToolChoice } from './xai-chat-prompt';
6
+ import { removeAdditionalPropertiesFalse } from './remove-additional-properties';
7
+ import type { XaiToolChoice } from './xai-chat-prompt';
7
8
 
8
9
  export function prepareTools({
9
10
  tools,
10
11
  toolChoice,
11
12
  }: {
12
- tools: LanguageModelV3CallOptions['tools'];
13
- toolChoice?: LanguageModelV3CallOptions['toolChoice'];
13
+ tools: LanguageModelV4CallOptions['tools'];
14
+ toolChoice?: LanguageModelV4CallOptions['toolChoice'];
14
15
  }): {
15
16
  tools:
16
17
  | Array<{
@@ -24,12 +25,12 @@ export function prepareTools({
24
25
  }>
25
26
  | undefined;
26
27
  toolChoice: XaiToolChoice | undefined;
27
- toolWarnings: SharedV3Warning[];
28
+ toolWarnings: SharedV4Warning[];
28
29
  } {
29
30
  // when the tools array is empty, change it to undefined to prevent errors
30
31
  tools = tools?.length ? tools : undefined;
31
32
 
32
- const toolWarnings: SharedV3Warning[] = [];
33
+ const toolWarnings: SharedV4Warning[] = [];
33
34
 
34
35
  if (tools == null) {
35
36
  return { tools: undefined, toolChoice: undefined, toolWarnings };
@@ -58,7 +59,7 @@ export function prepareTools({
58
59
  function: {
59
60
  name: tool.name,
60
61
  description: tool.description,
61
- parameters: tool.inputSchema,
62
+ parameters: removeAdditionalPropertiesFalse(tool.inputSchema),
62
63
  ...(tool.strict != null ? { strict: tool.strict } : {}),
63
64
  },
64
65
  });
@@ -1,65 +1,101 @@
1
1
  import {
2
- type Experimental_VideoModelV3,
3
- ImageModelV3,
4
- LanguageModelV3,
2
+ type Experimental_RealtimeFactoryV4 as RealtimeFactoryV4,
3
+ type Experimental_RealtimeFactoryV4GetTokenOptions as RealtimeFactoryV4GetTokenOptions,
4
+ type Experimental_VideoModelV4,
5
+ type FilesV4,
6
+ type ImageModelV4,
7
+ type LanguageModelV4,
5
8
  NoSuchModelError,
6
- ProviderV3,
9
+ type ProviderV4,
10
+ type SpeechModelV4,
11
+ type TranscriptionModelV4,
7
12
  } from '@ai-sdk/provider';
8
13
  import {
9
- FetchFunction,
10
14
  generateId,
11
15
  loadApiKey,
12
16
  withoutTrailingSlash,
13
17
  withUserAgentSuffix,
18
+ type FetchFunction,
14
19
  } from '@ai-sdk/provider-utils';
15
20
  import { XaiChatLanguageModel } from './xai-chat-language-model';
16
- import { XaiChatModelId } from './xai-chat-options';
21
+ import type { XaiChatModelId } from './xai-chat-language-model-options';
17
22
  import { XaiImageModel } from './xai-image-model';
18
- import { XaiImageModelId } from './xai-image-settings';
23
+ import type { XaiImageModelId } from './xai-image-settings';
19
24
  import { XaiResponsesLanguageModel } from './responses/xai-responses-language-model';
20
- import { XaiResponsesModelId } from './responses/xai-responses-options';
25
+ import type { XaiResponsesModelId } from './responses/xai-responses-language-model-options';
26
+ import { XaiRealtimeModel } from './realtime/xai-realtime-model';
21
27
  import { xaiTools } from './tool';
22
28
  import { VERSION } from './version';
29
+ import { XaiFiles } from './files/xai-files';
23
30
  import { XaiVideoModel } from './xai-video-model';
24
- import { XaiVideoModelId } from './xai-video-settings';
31
+ import type { XaiVideoModelId } from './xai-video-settings';
32
+ import { XaiSpeechModel } from './xai-speech-model';
33
+ import { XaiTranscriptionModel } from './xai-transcription-model';
25
34
 
26
- export interface XaiProvider extends ProviderV3 {
27
- (modelId: XaiResponsesModelId): LanguageModelV3;
35
+ export interface XaiProvider extends ProviderV4 {
36
+ (modelId: XaiResponsesModelId): LanguageModelV4;
28
37
 
29
38
  /**
30
39
  * Creates an Xai language model for text generation.
31
40
  */
32
- languageModel(modelId: XaiResponsesModelId): LanguageModelV3;
41
+ languageModel(modelId: XaiResponsesModelId): LanguageModelV4;
33
42
 
34
43
  /**
35
44
  * Creates an Xai chat model for text generation.
36
45
  */
37
- chat: (modelId: XaiChatModelId) => LanguageModelV3;
46
+ chat: (modelId: XaiChatModelId) => LanguageModelV4;
38
47
 
39
48
  /**
40
49
  * Creates an Xai responses model for text generation.
41
50
  */
42
- responses: (modelId: XaiResponsesModelId) => LanguageModelV3;
51
+ responses: (modelId: XaiResponsesModelId) => LanguageModelV4;
43
52
 
44
53
  /**
45
54
  * Creates an Xai image model for image generation.
46
55
  */
47
- image(modelId: XaiImageModelId): ImageModelV3;
56
+ image(modelId: XaiImageModelId): ImageModelV4;
48
57
 
49
58
  /**
50
59
  * Creates an Xai image model for image generation.
51
60
  */
52
- imageModel(modelId: XaiImageModelId): ImageModelV3;
61
+ imageModel(modelId: XaiImageModelId): ImageModelV4;
53
62
 
54
63
  /**
55
64
  * Creates an Xai video model for video generation.
56
65
  */
57
- video(modelId: XaiVideoModelId): Experimental_VideoModelV3;
66
+ video(modelId: XaiVideoModelId): Experimental_VideoModelV4;
58
67
 
59
68
  /**
60
69
  * Creates an Xai video model for video generation.
61
70
  */
62
- videoModel(modelId: XaiVideoModelId): Experimental_VideoModelV3;
71
+ videoModel(modelId: XaiVideoModelId): Experimental_VideoModelV4;
72
+
73
+ experimental_realtime: RealtimeFactoryV4;
74
+
75
+ /**
76
+ * Creates an xAI model for speech generation (text-to-speech).
77
+ */
78
+ speech(): SpeechModelV4;
79
+
80
+ /**
81
+ * Creates an xAI model for speech generation (text-to-speech).
82
+ */
83
+ speechModel(): SpeechModelV4;
84
+
85
+ /**
86
+ * Creates an xAI model for speech-to-text transcription.
87
+ */
88
+ transcription(): TranscriptionModelV4;
89
+
90
+ /**
91
+ * Creates an xAI model for speech-to-text transcription.
92
+ */
93
+ transcriptionModel(): TranscriptionModelV4;
94
+
95
+ /**
96
+ * Returns the xAI files interface for uploading files.
97
+ */
98
+ files(): FilesV4;
63
99
 
64
100
  /**
65
101
  * Server-side agentic tools for use with the responses API.
@@ -150,10 +186,64 @@ export function createXai(options: XaiProviderSettings = {}): XaiProvider {
150
186
  });
151
187
  };
152
188
 
189
+ const createRealtimeModel = (modelId: string) => {
190
+ return new XaiRealtimeModel(modelId, {
191
+ provider: 'xai.realtime',
192
+ baseURL: baseURL ?? 'https://api.x.ai/v1',
193
+ headers: getHeaders,
194
+ fetch: options.fetch,
195
+ });
196
+ };
197
+
198
+ const createSpeechModel = () => {
199
+ return new XaiSpeechModel('', {
200
+ provider: 'xai.speech',
201
+ baseURL,
202
+ headers: getHeaders,
203
+ fetch: options.fetch,
204
+ });
205
+ };
206
+
207
+ const createTranscriptionModel = () => {
208
+ return new XaiTranscriptionModel('', {
209
+ provider: 'xai.transcription',
210
+ baseURL,
211
+ headers: getHeaders,
212
+ fetch: options.fetch,
213
+ });
214
+ };
215
+
216
+ const experimentalRealtimeFactory = Object.assign(
217
+ (modelId: string) => createRealtimeModel(modelId),
218
+ {
219
+ getToken: async (tokenOptions: RealtimeFactoryV4GetTokenOptions) => {
220
+ const model = createRealtimeModel(tokenOptions.model);
221
+ const secret = await model.doCreateClientSecret({
222
+ sessionConfig: tokenOptions.sessionConfig,
223
+ expiresAfterSeconds: tokenOptions.expiresAfterSeconds,
224
+ });
225
+
226
+ return {
227
+ token: secret.token,
228
+ url: secret.url,
229
+ expiresAt: secret.expiresAt,
230
+ };
231
+ },
232
+ },
233
+ ) as RealtimeFactoryV4;
234
+
235
+ const createFiles = () =>
236
+ new XaiFiles({
237
+ provider: 'xai.files',
238
+ baseURL,
239
+ headers: getHeaders,
240
+ fetch: options.fetch,
241
+ });
242
+
153
243
  const provider = (modelId: XaiResponsesModelId) =>
154
244
  createResponsesLanguageModel(modelId);
155
245
 
156
- provider.specificationVersion = 'v3' as const;
246
+ provider.specificationVersion = 'v4' as const;
157
247
  provider.languageModel = createResponsesLanguageModel;
158
248
  provider.chat = createChatLanguageModel;
159
249
  provider.responses = createResponsesLanguageModel;
@@ -165,6 +255,12 @@ export function createXai(options: XaiProviderSettings = {}): XaiProvider {
165
255
  provider.image = createImageModel;
166
256
  provider.videoModel = createVideoModel;
167
257
  provider.video = createVideoModel;
258
+ provider.experimental_realtime = experimentalRealtimeFactory;
259
+ provider.speechModel = createSpeechModel;
260
+ provider.speech = createSpeechModel;
261
+ provider.transcriptionModel = createTranscriptionModel;
262
+ provider.transcription = createTranscriptionModel;
263
+ provider.files = createFiles;
168
264
  provider.tools = xaiTools;
169
265
 
170
266
  return provider;