@ai-sdk/xai 2.0.56 → 2.0.58

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.58
4
+
5
+ ### Patch Changes
6
+
7
+ - 2be480e: fix(provider/xai): correct usage token calculation for reasoning models
8
+
9
+ ## 2.0.57
10
+
11
+ ### Patch Changes
12
+
13
+ - b5e7a31: fix (provider/xai): handle new reasoning text chunk parts
14
+
3
15
  ## 2.0.56
4
16
 
5
17
  ### Patch Changes
package/dist/index.js CHANGED
@@ -468,7 +468,7 @@ var XaiChatLanguageModel = class {
468
468
  };
469
469
  }
470
470
  async doGenerate(options) {
471
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
471
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
472
472
  const { args: body, warnings } = await this.getArgs(options);
473
473
  const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
474
474
  const {
@@ -550,10 +550,10 @@ var XaiChatLanguageModel = class {
550
550
  finishReason: mapXaiFinishReason(choice.finish_reason),
551
551
  usage: {
552
552
  inputTokens: (_b = response.usage) == null ? void 0 : _b.prompt_tokens,
553
- outputTokens: (_c = response.usage) == null ? void 0 : _c.completion_tokens,
554
- totalTokens: (_d = response.usage) == null ? void 0 : _d.total_tokens,
555
- reasoningTokens: (_g = (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens_details) == null ? void 0 : _f.reasoning_tokens) != null ? _g : void 0,
556
- cachedInputTokens: (_j = (_i = (_h = response.usage) == null ? void 0 : _h.prompt_tokens_details) == null ? void 0 : _i.cached_tokens) != null ? _j : void 0
553
+ outputTokens: ((_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : 0) + ((_g = (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens_details) == null ? void 0 : _f.reasoning_tokens) != null ? _g : 0),
554
+ totalTokens: (_h = response.usage) == null ? void 0 : _h.total_tokens,
555
+ reasoningTokens: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0,
556
+ cachedInputTokens: (_n = (_m = (_l = response.usage) == null ? void 0 : _l.prompt_tokens_details) == null ? void 0 : _m.cached_tokens) != null ? _n : void 0
557
557
  },
558
558
  request: { body },
559
559
  response: {
@@ -638,7 +638,7 @@ var XaiChatLanguageModel = class {
638
638
  controller.enqueue({ type: "stream-start", warnings });
639
639
  },
640
640
  transform(chunk, controller) {
641
- var _a2, _b, _c, _d;
641
+ var _a2, _b, _c, _d, _e, _f, _g;
642
642
  if (options.includeRawChunks) {
643
643
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
644
644
  }
@@ -666,10 +666,10 @@ var XaiChatLanguageModel = class {
666
666
  }
667
667
  if (value.usage != null) {
668
668
  usage.inputTokens = value.usage.prompt_tokens;
669
- usage.outputTokens = value.usage.completion_tokens;
669
+ usage.outputTokens = ((_a2 = value.usage.completion_tokens) != null ? _a2 : 0) + ((_c = (_b = value.usage.completion_tokens_details) == null ? void 0 : _b.reasoning_tokens) != null ? _c : 0);
670
670
  usage.totalTokens = value.usage.total_tokens;
671
- usage.reasoningTokens = (_b = (_a2 = value.usage.completion_tokens_details) == null ? void 0 : _a2.reasoning_tokens) != null ? _b : void 0;
672
- usage.cachedInputTokens = (_d = (_c = value.usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : void 0;
671
+ usage.reasoningTokens = (_e = (_d = value.usage.completion_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : void 0;
672
+ usage.cachedInputTokens = (_g = (_f = value.usage.prompt_tokens_details) == null ? void 0 : _f.cached_tokens) != null ? _g : void 0;
673
673
  }
674
674
  const choice = value.choices[0];
675
675
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -1054,6 +1054,20 @@ var xaiResponsesChunkSchema = import_v44.z.union([
1054
1054
  summary_index: import_v44.z.number(),
1055
1055
  text: import_v44.z.string()
1056
1056
  }),
1057
+ import_v44.z.object({
1058
+ type: import_v44.z.literal("response.reasoning_text.delta"),
1059
+ item_id: import_v44.z.string(),
1060
+ output_index: import_v44.z.number(),
1061
+ content_index: import_v44.z.number(),
1062
+ delta: import_v44.z.string()
1063
+ }),
1064
+ import_v44.z.object({
1065
+ type: import_v44.z.literal("response.reasoning_text.done"),
1066
+ item_id: import_v44.z.string(),
1067
+ output_index: import_v44.z.number(),
1068
+ content_index: import_v44.z.number(),
1069
+ text: import_v44.z.string()
1070
+ }),
1057
1071
  import_v44.z.object({
1058
1072
  type: import_v44.z.literal("response.web_search_call.in_progress"),
1059
1073
  item_id: import_v44.z.string(),
@@ -1923,6 +1937,35 @@ var XaiResponsesLanguageModel = class {
1923
1937
  if (event.type === "response.reasoning_summary_text.done") {
1924
1938
  return;
1925
1939
  }
1940
+ if (event.type === "response.reasoning_text.delta") {
1941
+ const blockId = `reasoning-${event.item_id}`;
1942
+ if (activeReasoning[event.item_id] == null) {
1943
+ activeReasoning[event.item_id] = {};
1944
+ controller.enqueue({
1945
+ type: "reasoning-start",
1946
+ id: blockId,
1947
+ providerMetadata: {
1948
+ xai: {
1949
+ itemId: event.item_id
1950
+ }
1951
+ }
1952
+ });
1953
+ }
1954
+ controller.enqueue({
1955
+ type: "reasoning-delta",
1956
+ id: blockId,
1957
+ delta: event.delta,
1958
+ providerMetadata: {
1959
+ xai: {
1960
+ itemId: event.item_id
1961
+ }
1962
+ }
1963
+ });
1964
+ return;
1965
+ }
1966
+ if (event.type === "response.reasoning_text.done") {
1967
+ return;
1968
+ }
1926
1969
  if (event.type === "response.output_text.delta") {
1927
1970
  const blockId = `text-${event.item_id}`;
1928
1971
  if (contentBlocks[blockId] == null) {
@@ -1986,6 +2029,17 @@ var XaiResponsesLanguageModel = class {
1986
2029
  const part = event.item;
1987
2030
  if (part.type === "reasoning") {
1988
2031
  if (event.type === "response.output_item.done") {
2032
+ if (activeReasoning[part.id] == null) {
2033
+ controller.enqueue({
2034
+ type: "reasoning-start",
2035
+ id: `reasoning-${part.id}`,
2036
+ providerMetadata: {
2037
+ xai: {
2038
+ itemId: part.id
2039
+ }
2040
+ }
2041
+ });
2042
+ }
1989
2043
  controller.enqueue({
1990
2044
  type: "reasoning-end",
1991
2045
  id: `reasoning-${part.id}`,
@@ -2183,7 +2237,7 @@ var xaiTools = {
2183
2237
  };
2184
2238
 
2185
2239
  // src/version.ts
2186
- var VERSION = true ? "2.0.56" : "0.0.0-test";
2240
+ var VERSION = true ? "2.0.58" : "0.0.0-test";
2187
2241
 
2188
2242
  // src/xai-provider.ts
2189
2243
  var xaiErrorStructure = {