@ai-sdk/xai 2.0.51 → 2.0.53

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.53
4
+
5
+ ### Patch Changes
6
+
7
+ - c23ae93: feat(provider/xai): add image support to responses api
8
+
9
+ ## 2.0.52
10
+
11
+ ### Patch Changes
12
+
13
+ - a2b5824: feat (provider/xai): add support for encrypted reasoning content
14
+
3
15
  ## 2.0.51
4
16
 
5
17
  ### Patch Changes
package/dist/index.js CHANGED
@@ -34,8 +34,8 @@ module.exports = __toCommonJS(src_exports);
34
34
 
35
35
  // src/xai-provider.ts
36
36
  var import_openai_compatible = require("@ai-sdk/openai-compatible");
37
- var import_provider5 = require("@ai-sdk/provider");
38
- var import_provider_utils11 = require("@ai-sdk/provider-utils");
37
+ var import_provider6 = require("@ai-sdk/provider");
38
+ var import_provider_utils12 = require("@ai-sdk/provider-utils");
39
39
 
40
40
  // src/xai-chat-language-model.ts
41
41
  var import_provider3 = require("@ai-sdk/provider");
@@ -862,7 +862,7 @@ var xaiStreamErrorSchema = import_v43.z.object({
862
862
  });
863
863
 
864
864
  // src/responses/xai-responses-language-model.ts
865
- var import_provider_utils7 = require("@ai-sdk/provider-utils");
865
+ var import_provider_utils8 = require("@ai-sdk/provider-utils");
866
866
 
867
867
  // src/responses/xai-responses-api.ts
868
868
  var import_v44 = require("zod/v4");
@@ -942,7 +942,8 @@ var outputItemSchema = import_v44.z.discriminatedUnion("type", [
942
942
  type: import_v44.z.literal("reasoning"),
943
943
  id: import_v44.z.string(),
944
944
  summary: import_v44.z.array(reasoningSummaryPartSchema),
945
- status: import_v44.z.string()
945
+ status: import_v44.z.string(),
946
+ encrypted_content: import_v44.z.string().nullish()
946
947
  })
947
948
  ]);
948
949
  var xaiResponsesUsageSchema = import_v44.z.object({
@@ -1189,6 +1190,8 @@ var xaiResponsesProviderOptions = import_v45.z.object({
1189
1190
  });
1190
1191
 
1191
1192
  // src/responses/convert-to-xai-responses-input.ts
1193
+ var import_provider4 = require("@ai-sdk/provider");
1194
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
1192
1195
  async function convertToXaiResponsesInput({
1193
1196
  prompt
1194
1197
  }) {
@@ -1205,18 +1208,23 @@ async function convertToXaiResponsesInput({
1205
1208
  break;
1206
1209
  }
1207
1210
  case "user": {
1208
- let userContent = "";
1211
+ const contentParts = [];
1209
1212
  for (const block of message.content) {
1210
1213
  switch (block.type) {
1211
1214
  case "text": {
1212
- userContent += block.text;
1215
+ contentParts.push({ type: "input_text", text: block.text });
1213
1216
  break;
1214
1217
  }
1215
1218
  case "file": {
1216
- inputWarnings.push({
1217
- type: "other",
1218
- message: `xAI Responses API does not support ${block.type} in user messages`
1219
- });
1219
+ if (block.mediaType.startsWith("image/")) {
1220
+ const mediaType = block.mediaType === "image/*" ? "image/jpeg" : block.mediaType;
1221
+ const imageUrl = block.data instanceof URL ? block.data.toString() : `data:${mediaType};base64,${(0, import_provider_utils4.convertToBase64)(block.data)}`;
1222
+ contentParts.push({ type: "input_image", image_url: imageUrl });
1223
+ } else {
1224
+ throw new import_provider4.UnsupportedFunctionalityError({
1225
+ functionality: `file part media type ${block.mediaType}`
1226
+ });
1227
+ }
1220
1228
  break;
1221
1229
  }
1222
1230
  default: {
@@ -1230,7 +1238,7 @@ async function convertToXaiResponsesInput({
1230
1238
  }
1231
1239
  input.push({
1232
1240
  role: "user",
1233
- content: userContent
1241
+ content: contentParts
1234
1242
  });
1235
1243
  break;
1236
1244
  }
@@ -1330,14 +1338,14 @@ async function convertToXaiResponsesInput({
1330
1338
  }
1331
1339
 
1332
1340
  // src/responses/xai-responses-prepare-tools.ts
1333
- var import_provider4 = require("@ai-sdk/provider");
1334
- var import_provider_utils6 = require("@ai-sdk/provider-utils");
1341
+ var import_provider5 = require("@ai-sdk/provider");
1342
+ var import_provider_utils7 = require("@ai-sdk/provider-utils");
1335
1343
 
1336
1344
  // src/tool/web-search.ts
1337
- var import_provider_utils4 = require("@ai-sdk/provider-utils");
1345
+ var import_provider_utils5 = require("@ai-sdk/provider-utils");
1338
1346
  var import_v46 = require("zod/v4");
1339
- var webSearchArgsSchema = (0, import_provider_utils4.lazySchema)(
1340
- () => (0, import_provider_utils4.zodSchema)(
1347
+ var webSearchArgsSchema = (0, import_provider_utils5.lazySchema)(
1348
+ () => (0, import_provider_utils5.zodSchema)(
1341
1349
  import_v46.z.object({
1342
1350
  allowedDomains: import_v46.z.array(import_v46.z.string()).max(5).optional(),
1343
1351
  excludedDomains: import_v46.z.array(import_v46.z.string()).max(5).optional(),
@@ -1345,8 +1353,8 @@ var webSearchArgsSchema = (0, import_provider_utils4.lazySchema)(
1345
1353
  })
1346
1354
  )
1347
1355
  );
1348
- var webSearchOutputSchema = (0, import_provider_utils4.lazySchema)(
1349
- () => (0, import_provider_utils4.zodSchema)(
1356
+ var webSearchOutputSchema = (0, import_provider_utils5.lazySchema)(
1357
+ () => (0, import_provider_utils5.zodSchema)(
1350
1358
  import_v46.z.object({
1351
1359
  query: import_v46.z.string(),
1352
1360
  sources: import_v46.z.array(
@@ -1359,19 +1367,19 @@ var webSearchOutputSchema = (0, import_provider_utils4.lazySchema)(
1359
1367
  })
1360
1368
  )
1361
1369
  );
1362
- var webSearchToolFactory = (0, import_provider_utils4.createProviderDefinedToolFactoryWithOutputSchema)({
1370
+ var webSearchToolFactory = (0, import_provider_utils5.createProviderDefinedToolFactoryWithOutputSchema)({
1363
1371
  id: "xai.web_search",
1364
1372
  name: "web_search",
1365
- inputSchema: (0, import_provider_utils4.lazySchema)(() => (0, import_provider_utils4.zodSchema)(import_v46.z.object({}))),
1373
+ inputSchema: (0, import_provider_utils5.lazySchema)(() => (0, import_provider_utils5.zodSchema)(import_v46.z.object({}))),
1366
1374
  outputSchema: webSearchOutputSchema
1367
1375
  });
1368
1376
  var webSearch = (args = {}) => webSearchToolFactory(args);
1369
1377
 
1370
1378
  // src/tool/x-search.ts
1371
- var import_provider_utils5 = require("@ai-sdk/provider-utils");
1379
+ var import_provider_utils6 = require("@ai-sdk/provider-utils");
1372
1380
  var import_v47 = require("zod/v4");
1373
- var xSearchArgsSchema = (0, import_provider_utils5.lazySchema)(
1374
- () => (0, import_provider_utils5.zodSchema)(
1381
+ var xSearchArgsSchema = (0, import_provider_utils6.lazySchema)(
1382
+ () => (0, import_provider_utils6.zodSchema)(
1375
1383
  import_v47.z.object({
1376
1384
  allowedXHandles: import_v47.z.array(import_v47.z.string()).max(10).optional(),
1377
1385
  excludedXHandles: import_v47.z.array(import_v47.z.string()).max(10).optional(),
@@ -1382,8 +1390,8 @@ var xSearchArgsSchema = (0, import_provider_utils5.lazySchema)(
1382
1390
  })
1383
1391
  )
1384
1392
  );
1385
- var xSearchOutputSchema = (0, import_provider_utils5.lazySchema)(
1386
- () => (0, import_provider_utils5.zodSchema)(
1393
+ var xSearchOutputSchema = (0, import_provider_utils6.lazySchema)(
1394
+ () => (0, import_provider_utils6.zodSchema)(
1387
1395
  import_v47.z.object({
1388
1396
  query: import_v47.z.string(),
1389
1397
  posts: import_v47.z.array(
@@ -1397,10 +1405,10 @@ var xSearchOutputSchema = (0, import_provider_utils5.lazySchema)(
1397
1405
  })
1398
1406
  )
1399
1407
  );
1400
- var xSearchToolFactory = (0, import_provider_utils5.createProviderDefinedToolFactoryWithOutputSchema)({
1408
+ var xSearchToolFactory = (0, import_provider_utils6.createProviderDefinedToolFactoryWithOutputSchema)({
1401
1409
  id: "xai.x_search",
1402
1410
  name: "x_search",
1403
- inputSchema: (0, import_provider_utils5.lazySchema)(() => (0, import_provider_utils5.zodSchema)(import_v47.z.object({}))),
1411
+ inputSchema: (0, import_provider_utils6.lazySchema)(() => (0, import_provider_utils6.zodSchema)(import_v47.z.object({}))),
1404
1412
  outputSchema: xSearchOutputSchema
1405
1413
  });
1406
1414
  var xSearch = (args = {}) => xSearchToolFactory(args);
@@ -1422,7 +1430,7 @@ async function prepareResponsesTools({
1422
1430
  if (tool.type === "provider-defined") {
1423
1431
  switch (tool.id) {
1424
1432
  case "xai.web_search": {
1425
- const args = await (0, import_provider_utils6.validateTypes)({
1433
+ const args = await (0, import_provider_utils7.validateTypes)({
1426
1434
  value: tool.args,
1427
1435
  schema: webSearchArgsSchema
1428
1436
  });
@@ -1435,7 +1443,7 @@ async function prepareResponsesTools({
1435
1443
  break;
1436
1444
  }
1437
1445
  case "xai.x_search": {
1438
- const args = await (0, import_provider_utils6.validateTypes)({
1446
+ const args = await (0, import_provider_utils7.validateTypes)({
1439
1447
  value: tool.args,
1440
1448
  schema: xSearchArgsSchema
1441
1449
  });
@@ -1570,7 +1578,7 @@ async function prepareResponsesTools({
1570
1578
  }
1571
1579
  default: {
1572
1580
  const _exhaustiveCheck = type;
1573
- throw new import_provider4.UnsupportedFunctionalityError({
1581
+ throw new import_provider5.UnsupportedFunctionalityError({
1574
1582
  functionality: `tool choice type: ${_exhaustiveCheck}`
1575
1583
  });
1576
1584
  }
@@ -1604,7 +1612,7 @@ var XaiResponsesLanguageModel = class {
1604
1612
  }) {
1605
1613
  var _a, _b, _c, _d, _e;
1606
1614
  const warnings = [];
1607
- const options = (_a = await (0, import_provider_utils7.parseProviderOptions)({
1615
+ const options = (_a = await (0, import_provider_utils8.parseProviderOptions)({
1608
1616
  provider: "xai",
1609
1617
  providerOptions,
1610
1618
  schema: xaiResponsesProviderOptions
@@ -1694,12 +1702,12 @@ var XaiResponsesLanguageModel = class {
1694
1702
  responseHeaders,
1695
1703
  value: response,
1696
1704
  rawValue: rawResponse
1697
- } = await (0, import_provider_utils7.postJsonToApi)({
1705
+ } = await (0, import_provider_utils8.postJsonToApi)({
1698
1706
  url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
1699
- headers: (0, import_provider_utils7.combineHeaders)(this.config.headers(), options.headers),
1707
+ headers: (0, import_provider_utils8.combineHeaders)(this.config.headers(), options.headers),
1700
1708
  body,
1701
1709
  failedResponseHandler: xaiFailedResponseHandler,
1702
- successfulResponseHandler: (0, import_provider_utils7.createJsonResponseHandler)(
1710
+ successfulResponseHandler: (0, import_provider_utils8.createJsonResponseHandler)(
1703
1711
  xaiResponsesResponseSchema
1704
1712
  ),
1705
1713
  abortSignal: options.abortSignal,
@@ -1771,6 +1779,32 @@ var XaiResponsesLanguageModel = class {
1771
1779
  });
1772
1780
  break;
1773
1781
  }
1782
+ case "reasoning": {
1783
+ const summaryTexts = part.summary.map((s) => s.text).filter((text) => text && text.length > 0);
1784
+ if (summaryTexts.length > 0) {
1785
+ const reasoningText = summaryTexts.join("");
1786
+ if (part.encrypted_content || part.id) {
1787
+ content.push({
1788
+ type: "reasoning",
1789
+ text: reasoningText,
1790
+ providerMetadata: {
1791
+ xai: {
1792
+ ...part.encrypted_content && {
1793
+ reasoningEncryptedContent: part.encrypted_content
1794
+ },
1795
+ ...part.id && { itemId: part.id }
1796
+ }
1797
+ }
1798
+ });
1799
+ } else {
1800
+ content.push({
1801
+ type: "reasoning",
1802
+ text: reasoningText
1803
+ });
1804
+ }
1805
+ }
1806
+ break;
1807
+ }
1774
1808
  default: {
1775
1809
  break;
1776
1810
  }
@@ -1808,12 +1842,12 @@ var XaiResponsesLanguageModel = class {
1808
1842
  ...args,
1809
1843
  stream: true
1810
1844
  };
1811
- const { responseHeaders, value: response } = await (0, import_provider_utils7.postJsonToApi)({
1845
+ const { responseHeaders, value: response } = await (0, import_provider_utils8.postJsonToApi)({
1812
1846
  url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/responses`,
1813
- headers: (0, import_provider_utils7.combineHeaders)(this.config.headers(), options.headers),
1847
+ headers: (0, import_provider_utils8.combineHeaders)(this.config.headers(), options.headers),
1814
1848
  body,
1815
1849
  failedResponseHandler: xaiFailedResponseHandler,
1816
- successfulResponseHandler: (0, import_provider_utils7.createEventSourceResponseHandler)(
1850
+ successfulResponseHandler: (0, import_provider_utils8.createEventSourceResponseHandler)(
1817
1851
  xaiResponsesChunkSchema
1818
1852
  ),
1819
1853
  abortSignal: options.abortSignal,
@@ -1828,6 +1862,7 @@ var XaiResponsesLanguageModel = class {
1828
1862
  let isFirstChunk = true;
1829
1863
  const contentBlocks = {};
1830
1864
  const seenToolCalls = /* @__PURE__ */ new Set();
1865
+ const activeReasoning = {};
1831
1866
  const self = this;
1832
1867
  return {
1833
1868
  stream: response.pipeThrough(
@@ -1859,7 +1894,12 @@ var XaiResponsesLanguageModel = class {
1859
1894
  const blockId = `reasoning-${event.item_id}`;
1860
1895
  controller.enqueue({
1861
1896
  type: "reasoning-start",
1862
- id: blockId
1897
+ id: blockId,
1898
+ providerMetadata: {
1899
+ xai: {
1900
+ itemId: event.item_id
1901
+ }
1902
+ }
1863
1903
  });
1864
1904
  }
1865
1905
  if (event.type === "response.reasoning_summary_text.delta") {
@@ -1867,16 +1907,17 @@ var XaiResponsesLanguageModel = class {
1867
1907
  controller.enqueue({
1868
1908
  type: "reasoning-delta",
1869
1909
  id: blockId,
1870
- delta: event.delta
1910
+ delta: event.delta,
1911
+ providerMetadata: {
1912
+ xai: {
1913
+ itemId: event.item_id
1914
+ }
1915
+ }
1871
1916
  });
1872
1917
  return;
1873
1918
  }
1874
1919
  if (event.type === "response.reasoning_summary_text.done") {
1875
- const blockId = `reasoning-${event.item_id}`;
1876
- controller.enqueue({
1877
- type: "reasoning-end",
1878
- id: blockId
1879
- });
1920
+ return;
1880
1921
  }
1881
1922
  if (event.type === "response.output_text.delta") {
1882
1923
  const blockId = `text-${event.item_id}`;
@@ -1939,6 +1980,24 @@ var XaiResponsesLanguageModel = class {
1939
1980
  }
1940
1981
  if (event.type === "response.output_item.added" || event.type === "response.output_item.done") {
1941
1982
  const part = event.item;
1983
+ if (part.type === "reasoning") {
1984
+ if (event.type === "response.output_item.done") {
1985
+ controller.enqueue({
1986
+ type: "reasoning-end",
1987
+ id: `reasoning-${part.id}`,
1988
+ providerMetadata: {
1989
+ xai: {
1990
+ ...part.encrypted_content && {
1991
+ reasoningEncryptedContent: part.encrypted_content
1992
+ },
1993
+ ...part.id && { itemId: part.id }
1994
+ }
1995
+ }
1996
+ });
1997
+ delete activeReasoning[part.id];
1998
+ }
1999
+ return;
2000
+ }
1942
2001
  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") {
1943
2002
  const webSearchSubTools = [
1944
2003
  "web_search",
@@ -2065,13 +2124,13 @@ var XaiResponsesLanguageModel = class {
2065
2124
  };
2066
2125
 
2067
2126
  // src/tool/code-execution.ts
2068
- var import_provider_utils8 = require("@ai-sdk/provider-utils");
2127
+ var import_provider_utils9 = require("@ai-sdk/provider-utils");
2069
2128
  var import_v48 = require("zod/v4");
2070
2129
  var codeExecutionOutputSchema = import_v48.z.object({
2071
2130
  output: import_v48.z.string().describe("the output of the code execution"),
2072
2131
  error: import_v48.z.string().optional().describe("any error that occurred")
2073
2132
  });
2074
- var codeExecutionToolFactory = (0, import_provider_utils8.createProviderDefinedToolFactoryWithOutputSchema)({
2133
+ var codeExecutionToolFactory = (0, import_provider_utils9.createProviderDefinedToolFactoryWithOutputSchema)({
2075
2134
  id: "xai.code_execution",
2076
2135
  name: "code_execution",
2077
2136
  inputSchema: import_v48.z.object({}).describe("no input parameters"),
@@ -2080,13 +2139,13 @@ var codeExecutionToolFactory = (0, import_provider_utils8.createProviderDefinedT
2080
2139
  var codeExecution = (args = {}) => codeExecutionToolFactory(args);
2081
2140
 
2082
2141
  // src/tool/view-image.ts
2083
- var import_provider_utils9 = require("@ai-sdk/provider-utils");
2142
+ var import_provider_utils10 = require("@ai-sdk/provider-utils");
2084
2143
  var import_v49 = require("zod/v4");
2085
2144
  var viewImageOutputSchema = import_v49.z.object({
2086
2145
  description: import_v49.z.string().describe("description of the image"),
2087
2146
  objects: import_v49.z.array(import_v49.z.string()).optional().describe("objects detected in the image")
2088
2147
  });
2089
- var viewImageToolFactory = (0, import_provider_utils9.createProviderDefinedToolFactoryWithOutputSchema)({
2148
+ var viewImageToolFactory = (0, import_provider_utils10.createProviderDefinedToolFactoryWithOutputSchema)({
2090
2149
  id: "xai.view_image",
2091
2150
  name: "view_image",
2092
2151
  inputSchema: import_v49.z.object({}).describe("no input parameters"),
@@ -2095,14 +2154,14 @@ var viewImageToolFactory = (0, import_provider_utils9.createProviderDefinedToolF
2095
2154
  var viewImage = (args = {}) => viewImageToolFactory(args);
2096
2155
 
2097
2156
  // src/tool/view-x-video.ts
2098
- var import_provider_utils10 = require("@ai-sdk/provider-utils");
2157
+ var import_provider_utils11 = require("@ai-sdk/provider-utils");
2099
2158
  var import_v410 = require("zod/v4");
2100
2159
  var viewXVideoOutputSchema = import_v410.z.object({
2101
2160
  transcript: import_v410.z.string().optional().describe("transcript of the video"),
2102
2161
  description: import_v410.z.string().describe("description of the video content"),
2103
2162
  duration: import_v410.z.number().optional().describe("duration in seconds")
2104
2163
  });
2105
- var viewXVideoToolFactory = (0, import_provider_utils10.createProviderDefinedToolFactoryWithOutputSchema)({
2164
+ var viewXVideoToolFactory = (0, import_provider_utils11.createProviderDefinedToolFactoryWithOutputSchema)({
2106
2165
  id: "xai.view_x_video",
2107
2166
  name: "view_x_video",
2108
2167
  inputSchema: import_v410.z.object({}).describe("no input parameters"),
@@ -2120,7 +2179,7 @@ var xaiTools = {
2120
2179
  };
2121
2180
 
2122
2181
  // src/version.ts
2123
- var VERSION = true ? "2.0.51" : "0.0.0-test";
2182
+ var VERSION = true ? "2.0.53" : "0.0.0-test";
2124
2183
 
2125
2184
  // src/xai-provider.ts
2126
2185
  var xaiErrorStructure = {
@@ -2129,12 +2188,12 @@ var xaiErrorStructure = {
2129
2188
  };
2130
2189
  function createXai(options = {}) {
2131
2190
  var _a;
2132
- const baseURL = (0, import_provider_utils11.withoutTrailingSlash)(
2191
+ const baseURL = (0, import_provider_utils12.withoutTrailingSlash)(
2133
2192
  (_a = options.baseURL) != null ? _a : "https://api.x.ai/v1"
2134
2193
  );
2135
- const getHeaders = () => (0, import_provider_utils11.withUserAgentSuffix)(
2194
+ const getHeaders = () => (0, import_provider_utils12.withUserAgentSuffix)(
2136
2195
  {
2137
- Authorization: `Bearer ${(0, import_provider_utils11.loadApiKey)({
2196
+ Authorization: `Bearer ${(0, import_provider_utils12.loadApiKey)({
2138
2197
  apiKey: options.apiKey,
2139
2198
  environmentVariableName: "XAI_API_KEY",
2140
2199
  description: "xAI API key"
@@ -2148,7 +2207,7 @@ function createXai(options = {}) {
2148
2207
  provider: "xai.chat",
2149
2208
  baseURL,
2150
2209
  headers: getHeaders,
2151
- generateId: import_provider_utils11.generateId,
2210
+ generateId: import_provider_utils12.generateId,
2152
2211
  fetch: options.fetch
2153
2212
  });
2154
2213
  };
@@ -2157,7 +2216,7 @@ function createXai(options = {}) {
2157
2216
  provider: "xai.responses",
2158
2217
  baseURL,
2159
2218
  headers: getHeaders,
2160
- generateId: import_provider_utils11.generateId,
2219
+ generateId: import_provider_utils12.generateId,
2161
2220
  fetch: options.fetch
2162
2221
  });
2163
2222
  };
@@ -2176,7 +2235,7 @@ function createXai(options = {}) {
2176
2235
  provider.chat = createChatLanguageModel;
2177
2236
  provider.responses = createResponsesLanguageModel;
2178
2237
  provider.textEmbeddingModel = (modelId) => {
2179
- throw new import_provider5.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
2238
+ throw new import_provider6.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
2180
2239
  };
2181
2240
  provider.imageModel = createImageModel;
2182
2241
  provider.image = createImageModel;