@ai-sdk/xai 2.0.35 → 2.0.37

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/xai
2
2
 
3
+ ## 2.0.37
4
+
5
+ ### Patch Changes
6
+
7
+ - 864881c: Fix Responses API validation errors for server-side tools (web_search, x_search, code_execution). Add missing custom_tool_call type and streaming event schemas.
8
+
9
+ ## 2.0.36
10
+
11
+ ### Patch Changes
12
+
13
+ - 97b820b: fix(xai): responses model fixes
14
+
3
15
  ## 2.0.35
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -57,11 +57,17 @@ declare const xaiErrorDataSchema: z.ZodObject<{
57
57
  type XaiErrorData = z.infer<typeof xaiErrorDataSchema>;
58
58
 
59
59
  type XaiResponsesModelId = 'grok-4' | 'grok-4-fast' | 'grok-4-fast-non-reasoning' | (string & {});
60
+ /**
61
+ * @see https://docs.x.ai/docs/api-reference#create-new-response
62
+ */
60
63
  declare const xaiResponsesProviderOptions: z.ZodObject<{
61
64
  reasoningEffort: z.ZodOptional<z.ZodEnum<{
62
65
  low: "low";
63
66
  high: "high";
67
+ medium: "medium";
64
68
  }>>;
69
+ store: z.ZodOptional<z.ZodBoolean>;
70
+ previousResponseId: z.ZodOptional<z.ZodString>;
65
71
  }, z.core.$strip>;
66
72
  type XaiResponsesProviderOptions = z.infer<typeof xaiResponsesProviderOptions>;
67
73
 
package/dist/index.d.ts CHANGED
@@ -57,11 +57,17 @@ declare const xaiErrorDataSchema: z.ZodObject<{
57
57
  type XaiErrorData = z.infer<typeof xaiErrorDataSchema>;
58
58
 
59
59
  type XaiResponsesModelId = 'grok-4' | 'grok-4-fast' | 'grok-4-fast-non-reasoning' | (string & {});
60
+ /**
61
+ * @see https://docs.x.ai/docs/api-reference#create-new-response
62
+ */
60
63
  declare const xaiResponsesProviderOptions: z.ZodObject<{
61
64
  reasoningEffort: z.ZodOptional<z.ZodEnum<{
62
65
  low: "low";
63
66
  high: "high";
67
+ medium: "medium";
64
68
  }>>;
69
+ store: z.ZodOptional<z.ZodBoolean>;
70
+ previousResponseId: z.ZodOptional<z.ZodString>;
65
71
  }, z.core.$strip>;
66
72
  type XaiResponsesProviderOptions = z.infer<typeof xaiResponsesProviderOptions>;
67
73
 
package/dist/index.js CHANGED
@@ -150,12 +150,14 @@ function convertToXaiChatMessages(prompt) {
150
150
  function getResponseMetadata({
151
151
  id,
152
152
  model,
153
- created
153
+ created,
154
+ created_at
154
155
  }) {
156
+ const unixTime = created != null ? created : created_at;
155
157
  return {
156
158
  id: id != null ? id : void 0,
157
159
  modelId: model != null ? model : void 0,
158
- timestamp: created != null ? new Date(created * 1e3) : void 0
160
+ timestamp: unixTime != null ? new Date(unixTime * 1e3) : void 0
159
161
  };
160
162
  }
161
163
 
@@ -793,12 +795,18 @@ var messageContentPartSchema = import_v44.z.object({
793
795
  logprobs: import_v44.z.array(import_v44.z.any()).optional(),
794
796
  annotations: import_v44.z.array(annotationSchema).optional()
795
797
  });
798
+ var reasoningSummaryPartSchema = import_v44.z.object({
799
+ type: import_v44.z.string(),
800
+ text: import_v44.z.string()
801
+ });
796
802
  var toolCallSchema = import_v44.z.object({
797
- name: import_v44.z.string(),
798
- arguments: import_v44.z.string(),
799
- call_id: import_v44.z.string(),
803
+ name: import_v44.z.string().optional(),
804
+ arguments: import_v44.z.string().optional(),
805
+ input: import_v44.z.string().optional(),
806
+ call_id: import_v44.z.string().optional(),
800
807
  id: import_v44.z.string(),
801
- status: import_v44.z.string()
808
+ status: import_v44.z.string(),
809
+ action: import_v44.z.any().optional()
802
810
  });
803
811
  var outputItemSchema = import_v44.z.discriminatedUnion("type", [
804
812
  import_v44.z.object({
@@ -825,6 +833,10 @@ var outputItemSchema = import_v44.z.discriminatedUnion("type", [
825
833
  type: import_v44.z.literal("view_x_video_call"),
826
834
  ...toolCallSchema.shape
827
835
  }),
836
+ import_v44.z.object({
837
+ type: import_v44.z.literal("custom_tool_call"),
838
+ ...toolCallSchema.shape
839
+ }),
828
840
  import_v44.z.object({
829
841
  type: import_v44.z.literal("message"),
830
842
  role: import_v44.z.string(),
@@ -838,6 +850,12 @@ var outputItemSchema = import_v44.z.discriminatedUnion("type", [
838
850
  arguments: import_v44.z.string(),
839
851
  call_id: import_v44.z.string(),
840
852
  id: import_v44.z.string()
853
+ }),
854
+ import_v44.z.object({
855
+ type: import_v44.z.literal("reasoning"),
856
+ id: import_v44.z.string(),
857
+ summary: import_v44.z.array(reasoningSummaryPartSchema),
858
+ status: import_v44.z.string()
841
859
  })
842
860
  ]);
843
861
  var xaiResponsesUsageSchema = import_v44.z.object({
@@ -920,6 +938,94 @@ var xaiResponsesChunkSchema = import_v44.z.union([
920
938
  annotation_index: import_v44.z.number(),
921
939
  annotation: annotationSchema
922
940
  }),
941
+ import_v44.z.object({
942
+ type: import_v44.z.literal("response.reasoning_summary_part.added"),
943
+ item_id: import_v44.z.string(),
944
+ output_index: import_v44.z.number(),
945
+ summary_index: import_v44.z.number(),
946
+ part: reasoningSummaryPartSchema
947
+ }),
948
+ import_v44.z.object({
949
+ type: import_v44.z.literal("response.reasoning_summary_part.done"),
950
+ item_id: import_v44.z.string(),
951
+ output_index: import_v44.z.number(),
952
+ summary_index: import_v44.z.number(),
953
+ part: reasoningSummaryPartSchema
954
+ }),
955
+ import_v44.z.object({
956
+ type: import_v44.z.literal("response.reasoning_summary_text.delta"),
957
+ item_id: import_v44.z.string(),
958
+ output_index: import_v44.z.number(),
959
+ summary_index: import_v44.z.number(),
960
+ delta: import_v44.z.string()
961
+ }),
962
+ import_v44.z.object({
963
+ type: import_v44.z.literal("response.reasoning_summary_text.done"),
964
+ item_id: import_v44.z.string(),
965
+ output_index: import_v44.z.number(),
966
+ summary_index: import_v44.z.number(),
967
+ text: import_v44.z.string()
968
+ }),
969
+ import_v44.z.object({
970
+ type: import_v44.z.literal("response.web_search_call.in_progress"),
971
+ item_id: import_v44.z.string(),
972
+ output_index: import_v44.z.number()
973
+ }),
974
+ import_v44.z.object({
975
+ type: import_v44.z.literal("response.web_search_call.searching"),
976
+ item_id: import_v44.z.string(),
977
+ output_index: import_v44.z.number()
978
+ }),
979
+ import_v44.z.object({
980
+ type: import_v44.z.literal("response.web_search_call.completed"),
981
+ item_id: import_v44.z.string(),
982
+ output_index: import_v44.z.number()
983
+ }),
984
+ import_v44.z.object({
985
+ type: import_v44.z.literal("response.x_search_call.in_progress"),
986
+ item_id: import_v44.z.string(),
987
+ output_index: import_v44.z.number()
988
+ }),
989
+ import_v44.z.object({
990
+ type: import_v44.z.literal("response.x_search_call.searching"),
991
+ item_id: import_v44.z.string(),
992
+ output_index: import_v44.z.number()
993
+ }),
994
+ import_v44.z.object({
995
+ type: import_v44.z.literal("response.x_search_call.completed"),
996
+ item_id: import_v44.z.string(),
997
+ output_index: import_v44.z.number()
998
+ }),
999
+ import_v44.z.object({
1000
+ type: import_v44.z.literal("response.code_execution_call.in_progress"),
1001
+ item_id: import_v44.z.string(),
1002
+ output_index: import_v44.z.number()
1003
+ }),
1004
+ import_v44.z.object({
1005
+ type: import_v44.z.literal("response.code_execution_call.executing"),
1006
+ item_id: import_v44.z.string(),
1007
+ output_index: import_v44.z.number()
1008
+ }),
1009
+ import_v44.z.object({
1010
+ type: import_v44.z.literal("response.code_execution_call.completed"),
1011
+ item_id: import_v44.z.string(),
1012
+ output_index: import_v44.z.number()
1013
+ }),
1014
+ import_v44.z.object({
1015
+ type: import_v44.z.literal("response.code_interpreter_call.in_progress"),
1016
+ item_id: import_v44.z.string(),
1017
+ output_index: import_v44.z.number()
1018
+ }),
1019
+ import_v44.z.object({
1020
+ type: import_v44.z.literal("response.code_interpreter_call.executing"),
1021
+ item_id: import_v44.z.string(),
1022
+ output_index: import_v44.z.number()
1023
+ }),
1024
+ import_v44.z.object({
1025
+ type: import_v44.z.literal("response.code_interpreter_call.completed"),
1026
+ item_id: import_v44.z.string(),
1027
+ output_index: import_v44.z.number()
1028
+ }),
923
1029
  import_v44.z.object({
924
1030
  type: import_v44.z.literal("response.done"),
925
1031
  response: xaiResponsesResponseSchema
@@ -951,7 +1057,20 @@ function mapXaiResponsesFinishReason(finishReason) {
951
1057
  // src/responses/xai-responses-options.ts
952
1058
  var import_v45 = require("zod/v4");
953
1059
  var xaiResponsesProviderOptions = import_v45.z.object({
954
- reasoningEffort: import_v45.z.enum(["low", "high"]).optional()
1060
+ /**
1061
+ * Constrains how hard a reasoning model thinks before responding.
1062
+ * Possible values are `low` (uses fewer reasoning tokens), `medium` and `high` (uses more reasoning tokens).
1063
+ */
1064
+ reasoningEffort: import_v45.z.enum(["low", "medium", "high"]).optional(),
1065
+ /**
1066
+ * Whether to store the input message(s) and model response for later retrieval.
1067
+ * @default true
1068
+ */
1069
+ store: import_v45.z.boolean().optional(),
1070
+ /**
1071
+ * The ID of the previous response from the model.
1072
+ */
1073
+ previousResponseId: import_v45.z.string().optional()
955
1074
  });
956
1075
 
957
1076
  // src/responses/convert-to-xai-responses-input.ts
@@ -1412,7 +1531,16 @@ var XaiResponsesLanguageModel = class {
1412
1531
  temperature,
1413
1532
  top_p: topP,
1414
1533
  seed,
1415
- reasoning_effort: options.reasoningEffort
1534
+ ...options.reasoningEffort != null && {
1535
+ reasoning: { effort: options.reasoningEffort }
1536
+ },
1537
+ ...options.store === false && {
1538
+ store: options.store,
1539
+ include: ["reasoning.encrypted_content"]
1540
+ },
1541
+ ...options.previousResponseId != null && {
1542
+ previous_response_id: options.previousResponseId
1543
+ }
1416
1544
  };
1417
1545
  if (xaiTools2 && xaiTools2.length > 0) {
1418
1546
  baseArgs.tools = xaiTools2;
@@ -1429,7 +1557,7 @@ var XaiResponsesLanguageModel = class {
1429
1557
  };
1430
1558
  }
1431
1559
  async doGenerate(options) {
1432
- var _a, _b, _c;
1560
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1433
1561
  const {
1434
1562
  args: body,
1435
1563
  warnings,
@@ -1465,20 +1593,21 @@ var XaiResponsesLanguageModel = class {
1465
1593
  "x_thread_fetch"
1466
1594
  ];
1467
1595
  for (const part of response.output) {
1468
- if (part.type === "web_search_call" || part.type === "x_search_call" || part.type === "code_interpreter_call" || part.type === "code_execution_call" || part.type === "view_image_call" || part.type === "view_x_video_call") {
1469
- let toolName = part.name;
1470
- if (webSearchSubTools.includes(part.name)) {
1596
+ if (part.type === "web_search_call" || part.type === "x_search_call" || part.type === "code_interpreter_call" || part.type === "code_execution_call" || part.type === "view_image_call" || part.type === "view_x_video_call" || part.type === "custom_tool_call") {
1597
+ let toolName = (_b = part.name) != null ? _b : "";
1598
+ if (webSearchSubTools.includes((_c = part.name) != null ? _c : "")) {
1471
1599
  toolName = webSearchToolName != null ? webSearchToolName : "web_search";
1472
- } else if (xSearchSubTools.includes(part.name)) {
1600
+ } else if (xSearchSubTools.includes((_d = part.name) != null ? _d : "")) {
1473
1601
  toolName = xSearchToolName != null ? xSearchToolName : "x_search";
1474
1602
  } else if (part.name === "code_execution") {
1475
1603
  toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
1476
1604
  }
1605
+ const toolInput = part.type === "custom_tool_call" ? (_e = part.input) != null ? _e : "" : (_f = part.arguments) != null ? _f : "";
1477
1606
  content.push({
1478
1607
  type: "tool-call",
1479
1608
  toolCallId: part.id,
1480
1609
  toolName,
1481
- input: part.arguments,
1610
+ input: toolInput,
1482
1611
  providerExecuted: true
1483
1612
  });
1484
1613
  continue;
@@ -1500,7 +1629,7 @@ var XaiResponsesLanguageModel = class {
1500
1629
  sourceType: "url",
1501
1630
  id: this.config.generateId(),
1502
1631
  url: annotation.url,
1503
- title: (_b = annotation.title) != null ? _b : annotation.url
1632
+ title: (_g = annotation.title) != null ? _g : annotation.url
1504
1633
  });
1505
1634
  }
1506
1635
  }
@@ -1529,7 +1658,7 @@ var XaiResponsesLanguageModel = class {
1529
1658
  inputTokens: response.usage.input_tokens,
1530
1659
  outputTokens: response.usage.output_tokens,
1531
1660
  totalTokens: response.usage.total_tokens,
1532
- reasoningTokens: (_c = response.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens
1661
+ reasoningTokens: (_h = response.usage.output_tokens_details) == null ? void 0 : _h.reasoning_tokens
1533
1662
  },
1534
1663
  request: { body },
1535
1664
  response: {
@@ -1581,7 +1710,7 @@ var XaiResponsesLanguageModel = class {
1581
1710
  controller.enqueue({ type: "stream-start", warnings });
1582
1711
  },
1583
1712
  transform(chunk, controller) {
1584
- var _a2, _b, _c, _d;
1713
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
1585
1714
  if (options.includeRawChunks) {
1586
1715
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1587
1716
  }
@@ -1600,6 +1729,29 @@ var XaiResponsesLanguageModel = class {
1600
1729
  }
1601
1730
  return;
1602
1731
  }
1732
+ if (event.type === "response.reasoning_summary_part.added") {
1733
+ const blockId = `reasoning-${event.item_id}`;
1734
+ controller.enqueue({
1735
+ type: "reasoning-start",
1736
+ id: blockId
1737
+ });
1738
+ }
1739
+ if (event.type === "response.reasoning_summary_text.delta") {
1740
+ const blockId = `reasoning-${event.item_id}`;
1741
+ controller.enqueue({
1742
+ type: "reasoning-delta",
1743
+ id: blockId,
1744
+ delta: event.delta
1745
+ });
1746
+ return;
1747
+ }
1748
+ if (event.type === "response.reasoning_summary_text.done") {
1749
+ const blockId = `reasoning-${event.item_id}`;
1750
+ controller.enqueue({
1751
+ type: "reasoning-end",
1752
+ id: blockId
1753
+ });
1754
+ }
1603
1755
  if (event.type === "response.output_text.delta") {
1604
1756
  const blockId = `text-${event.item_id}`;
1605
1757
  if (contentBlocks[blockId] == null) {
@@ -1660,7 +1812,7 @@ var XaiResponsesLanguageModel = class {
1660
1812
  }
1661
1813
  if (event.type === "response.output_item.added" || event.type === "response.output_item.done") {
1662
1814
  const part = event.item;
1663
- if (part.type === "web_search_call" || part.type === "x_search_call" || part.type === "code_interpreter_call" || part.type === "code_execution_call" || part.type === "view_image_call" || part.type === "view_x_video_call") {
1815
+ if (part.type === "web_search_call" || part.type === "x_search_call" || part.type === "code_interpreter_call" || part.type === "code_execution_call" || part.type === "view_image_call" || part.type === "view_x_video_call" || part.type === "custom_tool_call") {
1664
1816
  if (!seenToolCalls.has(part.id)) {
1665
1817
  seenToolCalls.add(part.id);
1666
1818
  const webSearchSubTools = [
@@ -1674,14 +1826,15 @@ var XaiResponsesLanguageModel = class {
1674
1826
  "x_semantic_search",
1675
1827
  "x_thread_fetch"
1676
1828
  ];
1677
- let toolName = part.name;
1678
- if (webSearchSubTools.includes(part.name)) {
1829
+ let toolName = (_d = part.name) != null ? _d : "";
1830
+ if (webSearchSubTools.includes((_e = part.name) != null ? _e : "")) {
1679
1831
  toolName = webSearchToolName != null ? webSearchToolName : "web_search";
1680
- } else if (xSearchSubTools.includes(part.name)) {
1832
+ } else if (xSearchSubTools.includes((_f = part.name) != null ? _f : "")) {
1681
1833
  toolName = xSearchToolName != null ? xSearchToolName : "x_search";
1682
1834
  } else if (part.name === "code_execution") {
1683
1835
  toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
1684
1836
  }
1837
+ const toolInput = part.type === "custom_tool_call" ? (_g = part.input) != null ? _g : "" : (_h = part.arguments) != null ? _h : "";
1685
1838
  controller.enqueue({
1686
1839
  type: "tool-input-start",
1687
1840
  id: part.id,
@@ -1690,7 +1843,7 @@ var XaiResponsesLanguageModel = class {
1690
1843
  controller.enqueue({
1691
1844
  type: "tool-input-delta",
1692
1845
  id: part.id,
1693
- delta: part.arguments
1846
+ delta: toolInput
1694
1847
  });
1695
1848
  controller.enqueue({
1696
1849
  type: "tool-input-end",
@@ -1700,7 +1853,7 @@ var XaiResponsesLanguageModel = class {
1700
1853
  type: "tool-call",
1701
1854
  toolCallId: part.id,
1702
1855
  toolName,
1703
- input: part.arguments,
1856
+ input: toolInput,
1704
1857
  providerExecuted: true
1705
1858
  });
1706
1859
  }
@@ -1731,7 +1884,7 @@ var XaiResponsesLanguageModel = class {
1731
1884
  sourceType: "url",
1732
1885
  id: self.config.generateId(),
1733
1886
  url: annotation.url,
1734
- title: (_d = annotation.title) != null ? _d : annotation.url
1887
+ title: (_i = annotation.title) != null ? _i : annotation.url
1735
1888
  });
1736
1889
  }
1737
1890
  }
@@ -1839,7 +1992,7 @@ var xaiTools = {
1839
1992
  };
1840
1993
 
1841
1994
  // src/version.ts
1842
- var VERSION = true ? "2.0.35" : "0.0.0-test";
1995
+ var VERSION = true ? "2.0.37" : "0.0.0-test";
1843
1996
 
1844
1997
  // src/xai-provider.ts
1845
1998
  var xaiErrorStructure = {