@ai-sdk/xai 2.0.0-alpha.9 → 2.0.0-beta.10

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/dist/index.js CHANGED
@@ -30,117 +30,13 @@ var import_openai_compatible = require("@ai-sdk/openai-compatible");
30
30
  var import_provider3 = require("@ai-sdk/provider");
31
31
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
32
32
 
33
- // src/xai-chat-options.ts
34
- var import_zod = require("zod");
35
- function supportsStructuredOutputs(modelId) {
36
- return [
37
- "grok-3",
38
- "grok-3-beta",
39
- "grok-3-latest",
40
- "grok-3-fast",
41
- "grok-3-fast-beta",
42
- "grok-3-fast-latest",
43
- "grok-3-mini",
44
- "grok-3-mini-beta",
45
- "grok-3-mini-latest",
46
- "grok-3-mini-fast",
47
- "grok-3-mini-fast-beta",
48
- "grok-3-mini-fast-latest",
49
- "grok-2-1212",
50
- "grok-2-vision-1212"
51
- ].includes(modelId);
52
- }
53
- var webSourceSchema = import_zod.z.object({
54
- type: import_zod.z.literal("web"),
55
- country: import_zod.z.string().length(2).optional(),
56
- excludedWebsites: import_zod.z.array(import_zod.z.string()).max(5).optional(),
57
- allowedWebsites: import_zod.z.array(import_zod.z.string()).max(5).optional(),
58
- safeSearch: import_zod.z.boolean().optional()
59
- });
60
- var xSourceSchema = import_zod.z.object({
61
- type: import_zod.z.literal("x"),
62
- xHandles: import_zod.z.array(import_zod.z.string()).optional()
63
- });
64
- var newsSourceSchema = import_zod.z.object({
65
- type: import_zod.z.literal("news"),
66
- country: import_zod.z.string().length(2).optional(),
67
- excludedWebsites: import_zod.z.array(import_zod.z.string()).max(5).optional(),
68
- safeSearch: import_zod.z.boolean().optional()
69
- });
70
- var rssSourceSchema = import_zod.z.object({
71
- type: import_zod.z.literal("rss"),
72
- links: import_zod.z.array(import_zod.z.string().url()).max(1)
73
- // currently only supports one RSS link
74
- });
75
- var searchSourceSchema = import_zod.z.discriminatedUnion("type", [
76
- webSourceSchema,
77
- xSourceSchema,
78
- newsSourceSchema,
79
- rssSourceSchema
80
- ]);
81
- var xaiProviderOptions = import_zod.z.object({
82
- /**
83
- * reasoning effort for reasoning models
84
- * only supported by grok-3-mini and grok-3-mini-fast models
85
- */
86
- reasoningEffort: import_zod.z.enum(["low", "high"]).optional(),
87
- searchParameters: import_zod.z.object({
88
- /**
89
- * search mode preference
90
- * - "off": disables search completely
91
- * - "auto": model decides whether to search (default)
92
- * - "on": always enables search
93
- */
94
- mode: import_zod.z.enum(["off", "auto", "on"]),
95
- /**
96
- * whether to return citations in the response
97
- * defaults to true
98
- */
99
- returnCitations: import_zod.z.boolean().optional(),
100
- /**
101
- * start date for search data (ISO8601 format: YYYY-MM-DD)
102
- */
103
- fromDate: import_zod.z.string().optional(),
104
- /**
105
- * end date for search data (ISO8601 format: YYYY-MM-DD)
106
- */
107
- toDate: import_zod.z.string().optional(),
108
- /**
109
- * maximum number of search results to consider
110
- * defaults to 20
111
- */
112
- maxSearchResults: import_zod.z.number().min(1).max(50).optional(),
113
- /**
114
- * data sources to search from
115
- * defaults to ["web", "x"] if not specified
116
- */
117
- sources: import_zod.z.array(searchSourceSchema).optional()
118
- }).optional()
119
- });
120
-
121
- // src/xai-error.ts
122
- var import_provider_utils = require("@ai-sdk/provider-utils");
123
- var import_zod2 = require("zod");
124
- var xaiErrorDataSchema = import_zod2.z.object({
125
- error: import_zod2.z.object({
126
- message: import_zod2.z.string(),
127
- type: import_zod2.z.string().nullish(),
128
- param: import_zod2.z.any().nullish(),
129
- code: import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number()]).nullish()
130
- })
131
- });
132
- var xaiFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
133
- errorSchema: xaiErrorDataSchema,
134
- errorToMessage: (data) => data.error.message
135
- });
136
-
137
33
  // src/xai-chat-language-model.ts
138
34
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
139
- var import_zod3 = require("zod");
35
+ var import_v43 = require("zod/v4");
140
36
 
141
37
  // src/convert-to-xai-chat-messages.ts
142
38
  var import_provider = require("@ai-sdk/provider");
143
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
39
+ var import_provider_utils = require("@ai-sdk/provider-utils");
144
40
  function convertToXaiChatMessages(prompt) {
145
41
  const messages = [];
146
42
  const warnings = [];
@@ -168,7 +64,7 @@ function convertToXaiChatMessages(prompt) {
168
64
  return {
169
65
  type: "image_url",
170
66
  image_url: {
171
- url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${(0, import_provider_utils2.convertToBase64)(part.data)}`
67
+ url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${(0, import_provider_utils.convertToBase64)(part.data)}`
172
68
  }
173
69
  };
174
70
  } else {
@@ -197,7 +93,7 @@ function convertToXaiChatMessages(prompt) {
197
93
  type: "function",
198
94
  function: {
199
95
  name: part.toolName,
200
- arguments: JSON.stringify(part.args)
96
+ arguments: JSON.stringify(part.input)
201
97
  }
202
98
  });
203
99
  break;
@@ -213,10 +109,23 @@ function convertToXaiChatMessages(prompt) {
213
109
  }
214
110
  case "tool": {
215
111
  for (const toolResponse of content) {
112
+ const output = toolResponse.output;
113
+ let contentValue;
114
+ switch (output.type) {
115
+ case "text":
116
+ case "error-text":
117
+ contentValue = output.value;
118
+ break;
119
+ case "content":
120
+ case "json":
121
+ case "error-json":
122
+ contentValue = JSON.stringify(output.value);
123
+ break;
124
+ }
216
125
  messages.push({
217
126
  role: "tool",
218
127
  tool_call_id: toolResponse.toolCallId,
219
- content: JSON.stringify(toolResponse.result)
128
+ content: contentValue
220
129
  });
221
130
  }
222
131
  break;
@@ -260,6 +169,92 @@ function mapXaiFinishReason(finishReason) {
260
169
  }
261
170
  }
262
171
 
172
+ // src/xai-chat-options.ts
173
+ var import_v4 = require("zod/v4");
174
+ var webSourceSchema = import_v4.z.object({
175
+ type: import_v4.z.literal("web"),
176
+ country: import_v4.z.string().length(2).optional(),
177
+ excludedWebsites: import_v4.z.array(import_v4.z.string()).max(5).optional(),
178
+ allowedWebsites: import_v4.z.array(import_v4.z.string()).max(5).optional(),
179
+ safeSearch: import_v4.z.boolean().optional()
180
+ });
181
+ var xSourceSchema = import_v4.z.object({
182
+ type: import_v4.z.literal("x"),
183
+ xHandles: import_v4.z.array(import_v4.z.string()).optional()
184
+ });
185
+ var newsSourceSchema = import_v4.z.object({
186
+ type: import_v4.z.literal("news"),
187
+ country: import_v4.z.string().length(2).optional(),
188
+ excludedWebsites: import_v4.z.array(import_v4.z.string()).max(5).optional(),
189
+ safeSearch: import_v4.z.boolean().optional()
190
+ });
191
+ var rssSourceSchema = import_v4.z.object({
192
+ type: import_v4.z.literal("rss"),
193
+ links: import_v4.z.array(import_v4.z.string().url()).max(1)
194
+ // currently only supports one RSS link
195
+ });
196
+ var searchSourceSchema = import_v4.z.discriminatedUnion("type", [
197
+ webSourceSchema,
198
+ xSourceSchema,
199
+ newsSourceSchema,
200
+ rssSourceSchema
201
+ ]);
202
+ var xaiProviderOptions = import_v4.z.object({
203
+ /**
204
+ * reasoning effort for reasoning models
205
+ * only supported by grok-3-mini and grok-3-mini-fast models
206
+ */
207
+ reasoningEffort: import_v4.z.enum(["low", "high"]).optional(),
208
+ searchParameters: import_v4.z.object({
209
+ /**
210
+ * search mode preference
211
+ * - "off": disables search completely
212
+ * - "auto": model decides whether to search (default)
213
+ * - "on": always enables search
214
+ */
215
+ mode: import_v4.z.enum(["off", "auto", "on"]),
216
+ /**
217
+ * whether to return citations in the response
218
+ * defaults to true
219
+ */
220
+ returnCitations: import_v4.z.boolean().optional(),
221
+ /**
222
+ * start date for search data (ISO8601 format: YYYY-MM-DD)
223
+ */
224
+ fromDate: import_v4.z.string().optional(),
225
+ /**
226
+ * end date for search data (ISO8601 format: YYYY-MM-DD)
227
+ */
228
+ toDate: import_v4.z.string().optional(),
229
+ /**
230
+ * maximum number of search results to consider
231
+ * defaults to 20
232
+ */
233
+ maxSearchResults: import_v4.z.number().min(1).max(50).optional(),
234
+ /**
235
+ * data sources to search from
236
+ * defaults to ["web", "x"] if not specified
237
+ */
238
+ sources: import_v4.z.array(searchSourceSchema).optional()
239
+ }).optional()
240
+ });
241
+
242
+ // src/xai-error.ts
243
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
244
+ var import_v42 = require("zod/v4");
245
+ var xaiErrorDataSchema = import_v42.z.object({
246
+ error: import_v42.z.object({
247
+ message: import_v42.z.string(),
248
+ type: import_v42.z.string().nullish(),
249
+ param: import_v42.z.any().nullish(),
250
+ code: import_v42.z.union([import_v42.z.string(), import_v42.z.number()]).nullish()
251
+ })
252
+ });
253
+ var xaiFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)({
254
+ errorSchema: xaiErrorDataSchema,
255
+ errorToMessage: (data) => data.error.message
256
+ });
257
+
263
258
  // src/xai-prepare-tools.ts
264
259
  var import_provider2 = require("@ai-sdk/provider");
265
260
  function prepareTools({
@@ -281,7 +276,7 @@ function prepareTools({
281
276
  function: {
282
277
  name: tool.name,
283
278
  description: tool.description,
284
- parameters: tool.parameters
279
+ parameters: tool.inputSchema
285
280
  }
286
281
  });
287
282
  }
@@ -488,10 +483,9 @@ var XaiChatLanguageModel = class {
488
483
  for (const toolCall of choice.message.tool_calls) {
489
484
  content.push({
490
485
  type: "tool-call",
491
- toolCallType: "function",
492
486
  toolCallId: toolCall.id,
493
487
  toolName: toolCall.function.name,
494
- args: toolCall.function.arguments
488
+ input: toolCall.function.arguments
495
489
  });
496
490
  }
497
491
  }
@@ -549,6 +543,8 @@ var XaiChatLanguageModel = class {
549
543
  totalTokens: void 0
550
544
  };
551
545
  let isFirstChunk = true;
546
+ const contentBlocks = {};
547
+ const lastReasoningDeltas = {};
552
548
  const self = this;
553
549
  return {
554
550
  stream: response.pipeThrough(
@@ -558,6 +554,9 @@ var XaiChatLanguageModel = class {
558
554
  },
559
555
  transform(chunk, controller) {
560
556
  var _a2, _b;
557
+ if (options.includeRawChunks) {
558
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
559
+ }
561
560
  if (!chunk.success) {
562
561
  controller.enqueue({ type: "error", error: chunk.error });
563
562
  return;
@@ -594,40 +593,79 @@ var XaiChatLanguageModel = class {
594
593
  return;
595
594
  }
596
595
  const delta = choice.delta;
596
+ const choiceIndex = choice.index;
597
597
  if (delta.content != null && delta.content.length > 0) {
598
- let textContent = delta.content;
598
+ const textContent = delta.content;
599
599
  const lastMessage = body.messages[body.messages.length - 1];
600
600
  if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && textContent === lastMessage.content) {
601
601
  return;
602
602
  }
603
- controller.enqueue({ type: "text", text: textContent });
603
+ const blockId = `text-${value.id || choiceIndex}`;
604
+ if (contentBlocks[blockId] == null) {
605
+ contentBlocks[blockId] = { type: "text" };
606
+ controller.enqueue({
607
+ type: "text-start",
608
+ id: blockId
609
+ });
610
+ }
611
+ controller.enqueue({
612
+ type: "text-delta",
613
+ id: blockId,
614
+ delta: textContent
615
+ });
604
616
  }
605
617
  if (delta.reasoning_content != null && delta.reasoning_content.length > 0) {
618
+ const blockId = `reasoning-${value.id || choiceIndex}`;
619
+ if (lastReasoningDeltas[blockId] === delta.reasoning_content) {
620
+ return;
621
+ }
622
+ lastReasoningDeltas[blockId] = delta.reasoning_content;
623
+ if (contentBlocks[blockId] == null) {
624
+ contentBlocks[blockId] = { type: "reasoning" };
625
+ controller.enqueue({
626
+ type: "reasoning-start",
627
+ id: blockId
628
+ });
629
+ }
606
630
  controller.enqueue({
607
- type: "reasoning",
608
- text: delta.reasoning_content
631
+ type: "reasoning-delta",
632
+ id: blockId,
633
+ delta: delta.reasoning_content
609
634
  });
610
635
  }
611
636
  if (delta.tool_calls != null) {
612
637
  for (const toolCall of delta.tool_calls) {
638
+ const toolCallId = toolCall.id;
613
639
  controller.enqueue({
614
- type: "tool-call-delta",
615
- toolCallType: "function",
616
- toolCallId: toolCall.id,
617
- toolName: toolCall.function.name,
618
- argsTextDelta: toolCall.function.arguments
640
+ type: "tool-input-start",
641
+ id: toolCallId,
642
+ toolName: toolCall.function.name
643
+ });
644
+ controller.enqueue({
645
+ type: "tool-input-delta",
646
+ id: toolCallId,
647
+ delta: toolCall.function.arguments
648
+ });
649
+ controller.enqueue({
650
+ type: "tool-input-end",
651
+ id: toolCallId
619
652
  });
620
653
  controller.enqueue({
621
654
  type: "tool-call",
622
- toolCallType: "function",
623
- toolCallId: toolCall.id,
655
+ toolCallId,
624
656
  toolName: toolCall.function.name,
625
- args: toolCall.function.arguments
657
+ input: toolCall.function.arguments
626
658
  });
627
659
  }
628
660
  }
629
661
  },
630
662
  flush(controller) {
663
+ for (const [blockId, block] of Object.entries(contentBlocks)) {
664
+ controller.enqueue({
665
+ type: block.type === "text" ? "text-end" : "reasoning-end",
666
+ id: blockId
667
+ });
668
+ }
631
669
  controller.enqueue({ type: "finish", finishReason, usage });
632
670
  }
633
671
  })
@@ -637,70 +675,70 @@ var XaiChatLanguageModel = class {
637
675
  };
638
676
  }
639
677
  };
640
- var xaiUsageSchema = import_zod3.z.object({
641
- prompt_tokens: import_zod3.z.number(),
642
- completion_tokens: import_zod3.z.number(),
643
- total_tokens: import_zod3.z.number(),
644
- completion_tokens_details: import_zod3.z.object({
645
- reasoning_tokens: import_zod3.z.number().nullish()
678
+ var xaiUsageSchema = import_v43.z.object({
679
+ prompt_tokens: import_v43.z.number(),
680
+ completion_tokens: import_v43.z.number(),
681
+ total_tokens: import_v43.z.number(),
682
+ completion_tokens_details: import_v43.z.object({
683
+ reasoning_tokens: import_v43.z.number().nullish()
646
684
  }).nullish()
647
685
  });
648
- var xaiChatResponseSchema = import_zod3.z.object({
649
- id: import_zod3.z.string().nullish(),
650
- created: import_zod3.z.number().nullish(),
651
- model: import_zod3.z.string().nullish(),
652
- choices: import_zod3.z.array(
653
- import_zod3.z.object({
654
- message: import_zod3.z.object({
655
- role: import_zod3.z.literal("assistant"),
656
- content: import_zod3.z.string().nullish(),
657
- reasoning_content: import_zod3.z.string().nullish(),
658
- tool_calls: import_zod3.z.array(
659
- import_zod3.z.object({
660
- id: import_zod3.z.string(),
661
- type: import_zod3.z.literal("function"),
662
- function: import_zod3.z.object({
663
- name: import_zod3.z.string(),
664
- arguments: import_zod3.z.string()
686
+ var xaiChatResponseSchema = import_v43.z.object({
687
+ id: import_v43.z.string().nullish(),
688
+ created: import_v43.z.number().nullish(),
689
+ model: import_v43.z.string().nullish(),
690
+ choices: import_v43.z.array(
691
+ import_v43.z.object({
692
+ message: import_v43.z.object({
693
+ role: import_v43.z.literal("assistant"),
694
+ content: import_v43.z.string().nullish(),
695
+ reasoning_content: import_v43.z.string().nullish(),
696
+ tool_calls: import_v43.z.array(
697
+ import_v43.z.object({
698
+ id: import_v43.z.string(),
699
+ type: import_v43.z.literal("function"),
700
+ function: import_v43.z.object({
701
+ name: import_v43.z.string(),
702
+ arguments: import_v43.z.string()
665
703
  })
666
704
  })
667
705
  ).nullish()
668
706
  }),
669
- index: import_zod3.z.number(),
670
- finish_reason: import_zod3.z.string().nullish()
707
+ index: import_v43.z.number(),
708
+ finish_reason: import_v43.z.string().nullish()
671
709
  })
672
710
  ),
673
- object: import_zod3.z.literal("chat.completion"),
711
+ object: import_v43.z.literal("chat.completion"),
674
712
  usage: xaiUsageSchema,
675
- citations: import_zod3.z.array(import_zod3.z.string().url()).nullish()
713
+ citations: import_v43.z.array(import_v43.z.string().url()).nullish()
676
714
  });
677
- var xaiChatChunkSchema = import_zod3.z.object({
678
- id: import_zod3.z.string().nullish(),
679
- created: import_zod3.z.number().nullish(),
680
- model: import_zod3.z.string().nullish(),
681
- choices: import_zod3.z.array(
682
- import_zod3.z.object({
683
- delta: import_zod3.z.object({
684
- role: import_zod3.z.enum(["assistant"]).optional(),
685
- content: import_zod3.z.string().nullish(),
686
- reasoning_content: import_zod3.z.string().nullish(),
687
- tool_calls: import_zod3.z.array(
688
- import_zod3.z.object({
689
- id: import_zod3.z.string(),
690
- type: import_zod3.z.literal("function"),
691
- function: import_zod3.z.object({
692
- name: import_zod3.z.string(),
693
- arguments: import_zod3.z.string()
715
+ var xaiChatChunkSchema = import_v43.z.object({
716
+ id: import_v43.z.string().nullish(),
717
+ created: import_v43.z.number().nullish(),
718
+ model: import_v43.z.string().nullish(),
719
+ choices: import_v43.z.array(
720
+ import_v43.z.object({
721
+ delta: import_v43.z.object({
722
+ role: import_v43.z.enum(["assistant"]).optional(),
723
+ content: import_v43.z.string().nullish(),
724
+ reasoning_content: import_v43.z.string().nullish(),
725
+ tool_calls: import_v43.z.array(
726
+ import_v43.z.object({
727
+ id: import_v43.z.string(),
728
+ type: import_v43.z.literal("function"),
729
+ function: import_v43.z.object({
730
+ name: import_v43.z.string(),
731
+ arguments: import_v43.z.string()
694
732
  })
695
733
  })
696
734
  ).nullish()
697
735
  }),
698
- finish_reason: import_zod3.z.string().nullish(),
699
- index: import_zod3.z.number()
736
+ finish_reason: import_v43.z.string().nullish(),
737
+ index: import_v43.z.number()
700
738
  })
701
739
  ),
702
740
  usage: xaiUsageSchema.nullish(),
703
- citations: import_zod3.z.array(import_zod3.z.string().url()).nullish()
741
+ citations: import_v43.z.array(import_v43.z.string().url()).nullish()
704
742
  });
705
743
 
706
744
  // src/xai-provider.ts
@@ -722,7 +760,6 @@ function createXai(options = {}) {
722
760
  ...options.headers
723
761
  });
724
762
  const createLanguageModel = (modelId) => {
725
- const structuredOutputs = supportsStructuredOutputs(modelId);
726
763
  return new XaiChatLanguageModel(modelId, {
727
764
  provider: "xai.chat",
728
765
  baseURL,