@ai-sdk/xai 3.0.0-beta.51 → 3.0.0-beta.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,24 @@
1
1
  # @ai-sdk/xai
2
2
 
3
+ ## 3.0.0-beta.53
4
+
5
+ ### Patch Changes
6
+
7
+ - 3bd2689: feat: extended token usage
8
+ - Updated dependencies [3bd2689]
9
+ - @ai-sdk/openai-compatible@2.0.0-beta.46
10
+ - @ai-sdk/provider@3.0.0-beta.26
11
+ - @ai-sdk/provider-utils@4.0.0-beta.45
12
+
13
+ ## 3.0.0-beta.52
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [53f3368]
18
+ - @ai-sdk/provider@3.0.0-beta.25
19
+ - @ai-sdk/openai-compatible@2.0.0-beta.45
20
+ - @ai-sdk/provider-utils@4.0.0-beta.44
21
+
3
22
  ## 3.0.0-beta.51
4
23
 
5
24
  ### Patch Changes
package/dist/index.js CHANGED
@@ -335,6 +335,27 @@ function prepareTools({
335
335
  }
336
336
  }
337
337
 
338
+ // src/convert-xai-chat-usage.ts
339
+ function convertXaiChatUsage(usage) {
340
+ var _a, _b, _c, _d;
341
+ const cacheReadTokens = (_b = (_a = usage.prompt_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0;
342
+ const reasoningTokens = (_d = (_c = usage.completion_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0;
343
+ return {
344
+ inputTokens: {
345
+ total: usage.prompt_tokens,
346
+ noCache: usage.prompt_tokens - cacheReadTokens,
347
+ cacheRead: cacheReadTokens,
348
+ cacheWrite: void 0
349
+ },
350
+ outputTokens: {
351
+ total: usage.completion_tokens,
352
+ text: usage.completion_tokens - reasoningTokens,
353
+ reasoning: reasoningTokens
354
+ },
355
+ raw: usage
356
+ };
357
+ }
358
+
338
359
  // src/xai-chat-language-model.ts
339
360
  var XaiChatLanguageModel = class {
340
361
  constructor(modelId, config) {
@@ -459,7 +480,7 @@ var XaiChatLanguageModel = class {
459
480
  };
460
481
  }
461
482
  async doGenerate(options) {
462
- var _a, _b, _c, _d, _e;
483
+ var _a;
463
484
  const { args: body, warnings } = await this.getArgs(options);
464
485
  const {
465
486
  responseHeaders,
@@ -517,13 +538,7 @@ var XaiChatLanguageModel = class {
517
538
  return {
518
539
  content,
519
540
  finishReason: mapXaiFinishReason(choice.finish_reason),
520
- usage: {
521
- inputTokens: response.usage.prompt_tokens,
522
- outputTokens: response.usage.completion_tokens,
523
- totalTokens: response.usage.total_tokens,
524
- reasoningTokens: (_c = (_b = response.usage.completion_tokens_details) == null ? void 0 : _b.reasoning_tokens) != null ? _c : void 0,
525
- cachedInputTokens: (_e = (_d = response.usage.prompt_tokens_details) == null ? void 0 : _d.cached_tokens) != null ? _e : void 0
526
- },
541
+ usage: convertXaiChatUsage(response.usage),
527
542
  request: { body },
528
543
  response: {
529
544
  ...getResponseMetadata(response),
@@ -553,13 +568,7 @@ var XaiChatLanguageModel = class {
553
568
  fetch: this.config.fetch
554
569
  });
555
570
  let finishReason = "unknown";
556
- const usage = {
557
- inputTokens: void 0,
558
- outputTokens: void 0,
559
- totalTokens: void 0,
560
- reasoningTokens: void 0,
561
- cachedInputTokens: void 0
562
- };
571
+ let usage = void 0;
563
572
  let isFirstChunk = true;
564
573
  const contentBlocks = {};
565
574
  const lastReasoningDeltas = {};
@@ -571,7 +580,6 @@ var XaiChatLanguageModel = class {
571
580
  controller.enqueue({ type: "stream-start", warnings });
572
581
  },
573
582
  transform(chunk, controller) {
574
- var _a2, _b, _c, _d;
575
583
  if (options.includeRawChunks) {
576
584
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
577
585
  }
@@ -598,11 +606,7 @@ var XaiChatLanguageModel = class {
598
606
  }
599
607
  }
600
608
  if (value.usage != null) {
601
- usage.inputTokens = value.usage.prompt_tokens;
602
- usage.outputTokens = value.usage.completion_tokens;
603
- usage.totalTokens = value.usage.total_tokens;
604
- usage.reasoningTokens = (_b = (_a2 = value.usage.completion_tokens_details) == null ? void 0 : _a2.reasoning_tokens) != null ? _b : void 0;
605
- usage.cachedInputTokens = (_d = (_c = value.usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : void 0;
609
+ usage = convertXaiChatUsage(value.usage);
606
610
  }
607
611
  const choice = value.choices[0];
608
612
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -772,6 +776,189 @@ var xaiChatChunkSchema = import_v43.z.object({
772
776
  // src/responses/xai-responses-language-model.ts
773
777
  var import_provider_utils7 = require("@ai-sdk/provider-utils");
774
778
 
779
+ // src/responses/convert-to-xai-responses-input.ts
780
+ async function convertToXaiResponsesInput({
781
+ prompt
782
+ }) {
783
+ var _a, _b, _c, _d, _e;
784
+ const input = [];
785
+ const inputWarnings = [];
786
+ for (const message of prompt) {
787
+ switch (message.role) {
788
+ case "system": {
789
+ input.push({
790
+ role: "system",
791
+ content: message.content
792
+ });
793
+ break;
794
+ }
795
+ case "user": {
796
+ let userContent = "";
797
+ for (const block of message.content) {
798
+ switch (block.type) {
799
+ case "text": {
800
+ userContent += block.text;
801
+ break;
802
+ }
803
+ case "file": {
804
+ inputWarnings.push({
805
+ type: "other",
806
+ message: `xAI Responses API does not support ${block.type} in user messages`
807
+ });
808
+ break;
809
+ }
810
+ default: {
811
+ const _exhaustiveCheck = block;
812
+ inputWarnings.push({
813
+ type: "other",
814
+ message: "xAI Responses API does not support this content type in user messages"
815
+ });
816
+ }
817
+ }
818
+ }
819
+ input.push({
820
+ role: "user",
821
+ content: userContent
822
+ });
823
+ break;
824
+ }
825
+ case "assistant": {
826
+ for (const part of message.content) {
827
+ switch (part.type) {
828
+ case "text": {
829
+ const id = typeof ((_b = (_a = part.providerOptions) == null ? void 0 : _a.xai) == null ? void 0 : _b.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
830
+ input.push({
831
+ role: "assistant",
832
+ content: part.text,
833
+ id
834
+ });
835
+ break;
836
+ }
837
+ case "tool-call": {
838
+ if (part.providerExecuted) {
839
+ break;
840
+ }
841
+ const id = typeof ((_d = (_c = part.providerOptions) == null ? void 0 : _c.xai) == null ? void 0 : _d.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
842
+ input.push({
843
+ type: "function_call",
844
+ id: id != null ? id : part.toolCallId,
845
+ call_id: part.toolCallId,
846
+ name: part.toolName,
847
+ arguments: JSON.stringify(part.input),
848
+ status: "completed"
849
+ });
850
+ break;
851
+ }
852
+ case "tool-result": {
853
+ break;
854
+ }
855
+ case "reasoning":
856
+ case "file": {
857
+ inputWarnings.push({
858
+ type: "other",
859
+ message: `xAI Responses API does not support ${part.type} in assistant messages`
860
+ });
861
+ break;
862
+ }
863
+ default: {
864
+ const _exhaustiveCheck = part;
865
+ inputWarnings.push({
866
+ type: "other",
867
+ message: "xAI Responses API does not support this content type in assistant messages"
868
+ });
869
+ }
870
+ }
871
+ }
872
+ break;
873
+ }
874
+ case "tool": {
875
+ for (const part of message.content) {
876
+ const output = part.output;
877
+ let outputValue;
878
+ switch (output.type) {
879
+ case "text":
880
+ case "error-text":
881
+ outputValue = output.value;
882
+ break;
883
+ case "execution-denied":
884
+ outputValue = (_e = output.reason) != null ? _e : "tool execution denied";
885
+ break;
886
+ case "json":
887
+ case "error-json":
888
+ outputValue = JSON.stringify(output.value);
889
+ break;
890
+ case "content":
891
+ outputValue = output.value.map((item) => {
892
+ if (item.type === "text") {
893
+ return item.text;
894
+ }
895
+ return "";
896
+ }).join("");
897
+ break;
898
+ default: {
899
+ const _exhaustiveCheck = output;
900
+ outputValue = "";
901
+ }
902
+ }
903
+ input.push({
904
+ type: "function_call_output",
905
+ call_id: part.toolCallId,
906
+ output: outputValue
907
+ });
908
+ }
909
+ break;
910
+ }
911
+ default: {
912
+ const _exhaustiveCheck = message;
913
+ inputWarnings.push({
914
+ type: "other",
915
+ message: "unsupported message role"
916
+ });
917
+ }
918
+ }
919
+ }
920
+ return { input, inputWarnings };
921
+ }
922
+
923
+ // src/responses/convert-xai-responses-usage.ts
924
+ function convertXaiResponsesUsage(usage) {
925
+ var _a, _b, _c, _d;
926
+ const cacheReadTokens = (_b = (_a = usage.input_tokens_details) == null ? void 0 : _a.cached_tokens) != null ? _b : 0;
927
+ const reasoningTokens = (_d = (_c = usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens) != null ? _d : 0;
928
+ return {
929
+ inputTokens: {
930
+ total: usage.input_tokens,
931
+ noCache: usage.input_tokens - cacheReadTokens,
932
+ cacheRead: cacheReadTokens,
933
+ cacheWrite: void 0
934
+ },
935
+ outputTokens: {
936
+ total: usage.output_tokens,
937
+ text: usage.output_tokens - reasoningTokens,
938
+ reasoning: reasoningTokens
939
+ },
940
+ raw: usage
941
+ };
942
+ }
943
+
944
+ // src/responses/map-xai-responses-finish-reason.ts
945
+ function mapXaiResponsesFinishReason(finishReason) {
946
+ switch (finishReason) {
947
+ case "stop":
948
+ case "completed":
949
+ return "stop";
950
+ case "length":
951
+ return "length";
952
+ case "tool_calls":
953
+ case "function_call":
954
+ return "tool-calls";
955
+ case "content_filter":
956
+ return "content-filter";
957
+ default:
958
+ return "unknown";
959
+ }
960
+ }
961
+
775
962
  // src/responses/xai-responses-api.ts
776
963
  var import_v44 = require("zod/v4");
777
964
  var annotationSchema = import_v44.z.union([
@@ -1031,24 +1218,6 @@ var xaiResponsesChunkSchema = import_v44.z.union([
1031
1218
  })
1032
1219
  ]);
1033
1220
 
1034
- // src/responses/map-xai-responses-finish-reason.ts
1035
- function mapXaiResponsesFinishReason(finishReason) {
1036
- switch (finishReason) {
1037
- case "stop":
1038
- case "completed":
1039
- return "stop";
1040
- case "length":
1041
- return "length";
1042
- case "tool_calls":
1043
- case "function_call":
1044
- return "tool-calls";
1045
- case "content_filter":
1046
- return "content-filter";
1047
- default:
1048
- return "unknown";
1049
- }
1050
- }
1051
-
1052
1221
  // src/responses/xai-responses-options.ts
1053
1222
  var import_v45 = require("zod/v4");
1054
1223
  var xaiResponsesProviderOptions = import_v45.z.object({
@@ -1068,150 +1237,6 @@ var xaiResponsesProviderOptions = import_v45.z.object({
1068
1237
  previousResponseId: import_v45.z.string().optional()
1069
1238
  });
1070
1239
 
1071
- // src/responses/convert-to-xai-responses-input.ts
1072
- async function convertToXaiResponsesInput({
1073
- prompt
1074
- }) {
1075
- var _a, _b, _c, _d, _e;
1076
- const input = [];
1077
- const inputWarnings = [];
1078
- for (const message of prompt) {
1079
- switch (message.role) {
1080
- case "system": {
1081
- input.push({
1082
- role: "system",
1083
- content: message.content
1084
- });
1085
- break;
1086
- }
1087
- case "user": {
1088
- let userContent = "";
1089
- for (const block of message.content) {
1090
- switch (block.type) {
1091
- case "text": {
1092
- userContent += block.text;
1093
- break;
1094
- }
1095
- case "file": {
1096
- inputWarnings.push({
1097
- type: "other",
1098
- message: `xAI Responses API does not support ${block.type} in user messages`
1099
- });
1100
- break;
1101
- }
1102
- default: {
1103
- const _exhaustiveCheck = block;
1104
- inputWarnings.push({
1105
- type: "other",
1106
- message: "xAI Responses API does not support this content type in user messages"
1107
- });
1108
- }
1109
- }
1110
- }
1111
- input.push({
1112
- role: "user",
1113
- content: userContent
1114
- });
1115
- break;
1116
- }
1117
- case "assistant": {
1118
- for (const part of message.content) {
1119
- switch (part.type) {
1120
- case "text": {
1121
- const id = typeof ((_b = (_a = part.providerOptions) == null ? void 0 : _a.xai) == null ? void 0 : _b.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
1122
- input.push({
1123
- role: "assistant",
1124
- content: part.text,
1125
- id
1126
- });
1127
- break;
1128
- }
1129
- case "tool-call": {
1130
- if (part.providerExecuted) {
1131
- break;
1132
- }
1133
- const id = typeof ((_d = (_c = part.providerOptions) == null ? void 0 : _c.xai) == null ? void 0 : _d.itemId) === "string" ? part.providerOptions.xai.itemId : void 0;
1134
- input.push({
1135
- type: "function_call",
1136
- id: id != null ? id : part.toolCallId,
1137
- call_id: part.toolCallId,
1138
- name: part.toolName,
1139
- arguments: JSON.stringify(part.input),
1140
- status: "completed"
1141
- });
1142
- break;
1143
- }
1144
- case "tool-result": {
1145
- break;
1146
- }
1147
- case "reasoning":
1148
- case "file": {
1149
- inputWarnings.push({
1150
- type: "other",
1151
- message: `xAI Responses API does not support ${part.type} in assistant messages`
1152
- });
1153
- break;
1154
- }
1155
- default: {
1156
- const _exhaustiveCheck = part;
1157
- inputWarnings.push({
1158
- type: "other",
1159
- message: "xAI Responses API does not support this content type in assistant messages"
1160
- });
1161
- }
1162
- }
1163
- }
1164
- break;
1165
- }
1166
- case "tool": {
1167
- for (const part of message.content) {
1168
- const output = part.output;
1169
- let outputValue;
1170
- switch (output.type) {
1171
- case "text":
1172
- case "error-text":
1173
- outputValue = output.value;
1174
- break;
1175
- case "execution-denied":
1176
- outputValue = (_e = output.reason) != null ? _e : "tool execution denied";
1177
- break;
1178
- case "json":
1179
- case "error-json":
1180
- outputValue = JSON.stringify(output.value);
1181
- break;
1182
- case "content":
1183
- outputValue = output.value.map((item) => {
1184
- if (item.type === "text") {
1185
- return item.text;
1186
- }
1187
- return "";
1188
- }).join("");
1189
- break;
1190
- default: {
1191
- const _exhaustiveCheck = output;
1192
- outputValue = "";
1193
- }
1194
- }
1195
- input.push({
1196
- type: "function_call_output",
1197
- call_id: part.toolCallId,
1198
- output: outputValue
1199
- });
1200
- }
1201
- break;
1202
- }
1203
- default: {
1204
- const _exhaustiveCheck = message;
1205
- inputWarnings.push({
1206
- type: "other",
1207
- message: "unsupported message role"
1208
- });
1209
- }
1210
- }
1211
- }
1212
- return { input, inputWarnings };
1213
- }
1214
-
1215
1240
  // src/responses/xai-responses-prepare-tools.ts
1216
1241
  var import_provider3 = require("@ai-sdk/provider");
1217
1242
  var import_provider_utils6 = require("@ai-sdk/provider-utils");
@@ -1556,7 +1581,7 @@ var XaiResponsesLanguageModel = class {
1556
1581
  };
1557
1582
  }
1558
1583
  async doGenerate(options) {
1559
- var _a, _b, _c, _d, _e, _f, _g, _h;
1584
+ var _a, _b, _c, _d, _e, _f, _g;
1560
1585
  const {
1561
1586
  args: body,
1562
1587
  warnings,
@@ -1653,12 +1678,7 @@ var XaiResponsesLanguageModel = class {
1653
1678
  return {
1654
1679
  content,
1655
1680
  finishReason: mapXaiResponsesFinishReason(response.status),
1656
- usage: {
1657
- inputTokens: response.usage.input_tokens,
1658
- outputTokens: response.usage.output_tokens,
1659
- totalTokens: response.usage.total_tokens,
1660
- reasoningTokens: (_h = response.usage.output_tokens_details) == null ? void 0 : _h.reasoning_tokens
1661
- },
1681
+ usage: convertXaiResponsesUsage(response.usage),
1662
1682
  request: { body },
1663
1683
  response: {
1664
1684
  ...getResponseMetadata(response),
@@ -1693,11 +1713,7 @@ var XaiResponsesLanguageModel = class {
1693
1713
  fetch: this.config.fetch
1694
1714
  });
1695
1715
  let finishReason = "unknown";
1696
- const usage = {
1697
- inputTokens: void 0,
1698
- outputTokens: void 0,
1699
- totalTokens: void 0
1700
- };
1716
+ let usage = void 0;
1701
1717
  let isFirstChunk = true;
1702
1718
  const contentBlocks = {};
1703
1719
  const seenToolCalls = /* @__PURE__ */ new Set();
@@ -1709,7 +1725,7 @@ var XaiResponsesLanguageModel = class {
1709
1725
  controller.enqueue({ type: "stream-start", warnings });
1710
1726
  },
1711
1727
  transform(chunk, controller) {
1712
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
1728
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
1713
1729
  if (options.includeRawChunks) {
1714
1730
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1715
1731
  }
@@ -1799,10 +1815,7 @@ var XaiResponsesLanguageModel = class {
1799
1815
  if (event.type === "response.done" || event.type === "response.completed") {
1800
1816
  const response2 = event.response;
1801
1817
  if (response2.usage) {
1802
- usage.inputTokens = response2.usage.input_tokens;
1803
- usage.outputTokens = response2.usage.output_tokens;
1804
- usage.totalTokens = response2.usage.total_tokens;
1805
- usage.reasoningTokens = (_c = response2.usage.output_tokens_details) == null ? void 0 : _c.reasoning_tokens;
1818
+ usage = convertXaiResponsesUsage(response2.usage);
1806
1819
  }
1807
1820
  if (response2.status) {
1808
1821
  finishReason = mapXaiResponsesFinishReason(response2.status);
@@ -1825,15 +1838,15 @@ var XaiResponsesLanguageModel = class {
1825
1838
  "x_semantic_search",
1826
1839
  "x_thread_fetch"
1827
1840
  ];
1828
- let toolName = (_d = part.name) != null ? _d : "";
1829
- if (webSearchSubTools.includes((_e = part.name) != null ? _e : "")) {
1841
+ let toolName = (_c = part.name) != null ? _c : "";
1842
+ if (webSearchSubTools.includes((_d = part.name) != null ? _d : "")) {
1830
1843
  toolName = webSearchToolName != null ? webSearchToolName : "web_search";
1831
- } else if (xSearchSubTools.includes((_f = part.name) != null ? _f : "")) {
1844
+ } else if (xSearchSubTools.includes((_e = part.name) != null ? _e : "")) {
1832
1845
  toolName = xSearchToolName != null ? xSearchToolName : "x_search";
1833
1846
  } else if (part.name === "code_execution") {
1834
1847
  toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
1835
1848
  }
1836
- const toolInput = part.type === "custom_tool_call" ? (_g = part.input) != null ? _g : "" : (_h = part.arguments) != null ? _h : "";
1849
+ const toolInput = part.type === "custom_tool_call" ? (_f = part.input) != null ? _f : "" : (_g = part.arguments) != null ? _g : "";
1837
1850
  controller.enqueue({
1838
1851
  type: "tool-input-start",
1839
1852
  id: part.id,
@@ -1883,7 +1896,7 @@ var XaiResponsesLanguageModel = class {
1883
1896
  sourceType: "url",
1884
1897
  id: self.config.generateId(),
1885
1898
  url: annotation.url,
1886
- title: (_i = annotation.title) != null ? _i : annotation.url
1899
+ title: (_h = annotation.title) != null ? _h : annotation.url
1887
1900
  });
1888
1901
  }
1889
1902
  }
@@ -1988,7 +2001,7 @@ var xaiTools = {
1988
2001
  };
1989
2002
 
1990
2003
  // src/version.ts
1991
- var VERSION = true ? "3.0.0-beta.51" : "0.0.0-test";
2004
+ var VERSION = true ? "3.0.0-beta.53" : "0.0.0-test";
1992
2005
 
1993
2006
  // src/xai-provider.ts
1994
2007
  var xaiErrorStructure = {