@ai-sdk/xai 2.0.57 → 2.0.59

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.mjs CHANGED
@@ -457,7 +457,7 @@ var XaiChatLanguageModel = class {
457
457
  };
458
458
  }
459
459
  async doGenerate(options) {
460
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
460
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
461
461
  const { args: body, warnings } = await this.getArgs(options);
462
462
  const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
463
463
  const {
@@ -539,10 +539,10 @@ var XaiChatLanguageModel = class {
539
539
  finishReason: mapXaiFinishReason(choice.finish_reason),
540
540
  usage: {
541
541
  inputTokens: (_b = response.usage) == null ? void 0 : _b.prompt_tokens,
542
- outputTokens: (_c = response.usage) == null ? void 0 : _c.completion_tokens,
543
- totalTokens: (_d = response.usage) == null ? void 0 : _d.total_tokens,
544
- reasoningTokens: (_g = (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens_details) == null ? void 0 : _f.reasoning_tokens) != null ? _g : void 0,
545
- cachedInputTokens: (_j = (_i = (_h = response.usage) == null ? void 0 : _h.prompt_tokens_details) == null ? void 0 : _i.cached_tokens) != null ? _j : void 0
542
+ 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),
543
+ totalTokens: (_h = response.usage) == null ? void 0 : _h.total_tokens,
544
+ reasoningTokens: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0,
545
+ cachedInputTokens: (_n = (_m = (_l = response.usage) == null ? void 0 : _l.prompt_tokens_details) == null ? void 0 : _m.cached_tokens) != null ? _n : void 0
546
546
  },
547
547
  request: { body },
548
548
  response: {
@@ -627,7 +627,7 @@ var XaiChatLanguageModel = class {
627
627
  controller.enqueue({ type: "stream-start", warnings });
628
628
  },
629
629
  transform(chunk, controller) {
630
- var _a2, _b, _c, _d;
630
+ var _a2, _b, _c, _d, _e, _f, _g;
631
631
  if (options.includeRawChunks) {
632
632
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
633
633
  }
@@ -655,10 +655,10 @@ var XaiChatLanguageModel = class {
655
655
  }
656
656
  if (value.usage != null) {
657
657
  usage.inputTokens = value.usage.prompt_tokens;
658
- usage.outputTokens = value.usage.completion_tokens;
658
+ 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);
659
659
  usage.totalTokens = value.usage.total_tokens;
660
- usage.reasoningTokens = (_b = (_a2 = value.usage.completion_tokens_details) == null ? void 0 : _a2.reasoning_tokens) != null ? _b : void 0;
661
- usage.cachedInputTokens = (_d = (_c = value.usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : void 0;
660
+ usage.reasoningTokens = (_e = (_d = value.usage.completion_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : void 0;
661
+ usage.cachedInputTokens = (_g = (_f = value.usage.prompt_tokens_details) == null ? void 0 : _f.cached_tokens) != null ? _g : void 0;
662
662
  }
663
663
  const choice = value.choices[0];
664
664
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -1151,6 +1151,19 @@ var xaiResponsesChunkSchema = z4.union([
1151
1151
  output_index: z4.number(),
1152
1152
  code: z4.string()
1153
1153
  }),
1154
+ // Function call arguments streaming events (standard function tools)
1155
+ z4.object({
1156
+ type: z4.literal("response.function_call_arguments.delta"),
1157
+ item_id: z4.string(),
1158
+ output_index: z4.number(),
1159
+ delta: z4.string()
1160
+ }),
1161
+ z4.object({
1162
+ type: z4.literal("response.function_call_arguments.done"),
1163
+ item_id: z4.string(),
1164
+ output_index: z4.number(),
1165
+ arguments: z4.string()
1166
+ }),
1154
1167
  z4.object({
1155
1168
  type: z4.literal("response.done"),
1156
1169
  response: xaiResponsesResponseSchema
@@ -1887,6 +1900,7 @@ var XaiResponsesLanguageModel = class {
1887
1900
  let isFirstChunk = true;
1888
1901
  const contentBlocks = {};
1889
1902
  const seenToolCalls = /* @__PURE__ */ new Set();
1903
+ const ongoingToolCalls = {};
1890
1904
  const activeReasoning = {};
1891
1905
  const self = this;
1892
1906
  return {
@@ -2032,6 +2046,20 @@ var XaiResponsesLanguageModel = class {
2032
2046
  }
2033
2047
  return;
2034
2048
  }
2049
+ if (event.type === "response.function_call_arguments.delta") {
2050
+ const toolCall = ongoingToolCalls[event.output_index];
2051
+ if (toolCall != null) {
2052
+ controller.enqueue({
2053
+ type: "tool-input-delta",
2054
+ id: toolCall.toolCallId,
2055
+ delta: event.delta
2056
+ });
2057
+ }
2058
+ return;
2059
+ }
2060
+ if (event.type === "response.function_call_arguments.done") {
2061
+ return;
2062
+ }
2035
2063
  if (event.type === "response.output_item.added" || event.type === "response.output_item.done") {
2036
2064
  const part = event.item;
2037
2065
  if (part.type === "reasoning") {
@@ -2143,18 +2171,18 @@ var XaiResponsesLanguageModel = class {
2143
2171
  }
2144
2172
  }
2145
2173
  } else if (part.type === "function_call") {
2146
- if (!seenToolCalls.has(part.call_id)) {
2147
- seenToolCalls.add(part.call_id);
2174
+ if (event.type === "response.output_item.added") {
2175
+ ongoingToolCalls[event.output_index] = {
2176
+ toolName: part.name,
2177
+ toolCallId: part.call_id
2178
+ };
2148
2179
  controller.enqueue({
2149
2180
  type: "tool-input-start",
2150
2181
  id: part.call_id,
2151
2182
  toolName: part.name
2152
2183
  });
2153
- controller.enqueue({
2154
- type: "tool-input-delta",
2155
- id: part.call_id,
2156
- delta: part.arguments
2157
- });
2184
+ } else if (event.type === "response.output_item.done") {
2185
+ ongoingToolCalls[event.output_index] = void 0;
2158
2186
  controller.enqueue({
2159
2187
  type: "tool-input-end",
2160
2188
  id: part.call_id
@@ -2244,7 +2272,7 @@ var xaiTools = {
2244
2272
  };
2245
2273
 
2246
2274
  // src/version.ts
2247
- var VERSION = true ? "2.0.57" : "0.0.0-test";
2275
+ var VERSION = true ? "2.0.59" : "0.0.0-test";
2248
2276
 
2249
2277
  // src/xai-provider.ts
2250
2278
  var xaiErrorStructure = {