@ai-sdk/xai 2.0.65 → 2.0.67

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.67
4
+
5
+ ### Patch Changes
6
+
7
+ - 21ff967: fix (provider/xai): handle mid-stream error chunks
8
+
9
+ ## 2.0.66
10
+
11
+ ### Patch Changes
12
+
13
+ - 92e25ae: fix (provider/xai): add response.incomplete and response.failed streaming event handling
14
+
3
15
  ## 2.0.65
4
16
 
5
17
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1209,6 +1209,30 @@ var xaiResponsesChunkSchema = import_v44.z.union([
1209
1209
  output_index: import_v44.z.number(),
1210
1210
  arguments: import_v44.z.string()
1211
1211
  }),
1212
+ import_v44.z.object({
1213
+ type: import_v44.z.literal("response.incomplete"),
1214
+ response: import_v44.z.object({
1215
+ incomplete_details: import_v44.z.object({ reason: import_v44.z.string() }).nullish(),
1216
+ usage: xaiResponsesUsageSchema.nullish()
1217
+ })
1218
+ }),
1219
+ import_v44.z.object({
1220
+ type: import_v44.z.literal("response.failed"),
1221
+ response: import_v44.z.object({
1222
+ error: import_v44.z.object({
1223
+ code: import_v44.z.string().nullish(),
1224
+ message: import_v44.z.string()
1225
+ }).nullish(),
1226
+ incomplete_details: import_v44.z.object({ reason: import_v44.z.string() }).nullish(),
1227
+ usage: xaiResponsesUsageSchema.nullish()
1228
+ })
1229
+ }),
1230
+ import_v44.z.object({
1231
+ type: import_v44.z.literal("error"),
1232
+ code: import_v44.z.string().nullish(),
1233
+ message: import_v44.z.string(),
1234
+ param: import_v44.z.string().nullish()
1235
+ }),
1212
1236
  import_v44.z.object({
1213
1237
  type: import_v44.z.literal("response.done"),
1214
1238
  response: xaiResponsesResponseSchema
@@ -1226,6 +1250,7 @@ function mapXaiResponsesFinishReason(finishReason) {
1226
1250
  case "completed":
1227
1251
  return "stop";
1228
1252
  case "length":
1253
+ case "max_output_tokens":
1229
1254
  return "length";
1230
1255
  case "tool_calls":
1231
1256
  case "function_call":
@@ -1936,7 +1961,7 @@ var XaiResponsesLanguageModel = class {
1936
1961
  controller.enqueue({ type: "stream-start", warnings });
1937
1962
  },
1938
1963
  transform(chunk, controller) {
1939
- var _a2, _b, _c, _d, _e, _f, _g, _h;
1964
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1940
1965
  if (options.includeRawChunks) {
1941
1966
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1942
1967
  }
@@ -2058,7 +2083,7 @@ var XaiResponsesLanguageModel = class {
2058
2083
  }
2059
2084
  return;
2060
2085
  }
2061
- if (event.type === "response.done" || event.type === "response.completed") {
2086
+ if (event.type === "response.done" || event.type === "response.completed" || event.type === "response.incomplete") {
2062
2087
  const response2 = event.response;
2063
2088
  if (response2.usage) {
2064
2089
  const converted = convertXaiResponsesUsage(response2.usage);
@@ -2068,11 +2093,33 @@ var XaiResponsesLanguageModel = class {
2068
2093
  usage.reasoningTokens = converted.reasoningTokens;
2069
2094
  usage.cachedInputTokens = converted.cachedInputTokens;
2070
2095
  }
2071
- if (response2.status) {
2096
+ if (event.type === "response.incomplete") {
2097
+ const reason = "incomplete_details" in response2 ? (_c = response2.incomplete_details) == null ? void 0 : _c.reason : void 0;
2098
+ finishReason = reason ? mapXaiResponsesFinishReason(reason) : "other";
2099
+ } else if ("status" in response2 && response2.status) {
2072
2100
  finishReason = hasFunctionCall ? "tool-calls" : mapXaiResponsesFinishReason(response2.status);
2073
2101
  }
2074
2102
  return;
2075
2103
  }
2104
+ if (event.type === "response.failed") {
2105
+ const reason = (_d = event.response.incomplete_details) == null ? void 0 : _d.reason;
2106
+ finishReason = reason ? mapXaiResponsesFinishReason(reason) : "error";
2107
+ if (event.response.usage) {
2108
+ const converted = convertXaiResponsesUsage(
2109
+ event.response.usage
2110
+ );
2111
+ usage.inputTokens = converted.inputTokens;
2112
+ usage.outputTokens = converted.outputTokens;
2113
+ usage.totalTokens = converted.totalTokens;
2114
+ usage.reasoningTokens = converted.reasoningTokens;
2115
+ usage.cachedInputTokens = converted.cachedInputTokens;
2116
+ }
2117
+ return;
2118
+ }
2119
+ if (event.type === "error") {
2120
+ controller.enqueue({ type: "error", error: event });
2121
+ return;
2122
+ }
2076
2123
  if (event.type === "response.function_call_arguments.delta") {
2077
2124
  const toolCall = ongoingToolCalls[event.output_index];
2078
2125
  if (toolCall != null) {
@@ -2130,15 +2177,15 @@ var XaiResponsesLanguageModel = class {
2130
2177
  "x_semantic_search",
2131
2178
  "x_thread_fetch"
2132
2179
  ];
2133
- let toolName = (_c = part.name) != null ? _c : "";
2134
- if (webSearchSubTools.includes((_d = part.name) != null ? _d : "") || part.type === "web_search_call") {
2180
+ let toolName = (_e = part.name) != null ? _e : "";
2181
+ if (webSearchSubTools.includes((_f = part.name) != null ? _f : "") || part.type === "web_search_call") {
2135
2182
  toolName = webSearchToolName != null ? webSearchToolName : "web_search";
2136
- } else if (xSearchSubTools.includes((_e = part.name) != null ? _e : "") || part.type === "x_search_call") {
2183
+ } else if (xSearchSubTools.includes((_g = part.name) != null ? _g : "") || part.type === "x_search_call") {
2137
2184
  toolName = xSearchToolName != null ? xSearchToolName : "x_search";
2138
2185
  } else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
2139
2186
  toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
2140
2187
  }
2141
- const toolInput = part.type === "custom_tool_call" ? (_f = part.input) != null ? _f : "" : (_g = part.arguments) != null ? _g : "";
2188
+ const toolInput = part.type === "custom_tool_call" ? (_h = part.input) != null ? _h : "" : (_i = part.arguments) != null ? _i : "";
2142
2189
  const shouldEmit = part.type === "custom_tool_call" ? event.type === "response.output_item.done" : !seenToolCalls.has(part.id);
2143
2190
  if (shouldEmit && !seenToolCalls.has(part.id)) {
2144
2191
  seenToolCalls.add(part.id);
@@ -2191,7 +2238,7 @@ var XaiResponsesLanguageModel = class {
2191
2238
  sourceType: "url",
2192
2239
  id: self.config.generateId(),
2193
2240
  url: annotation.url,
2194
- title: (_h = annotation.title) != null ? _h : annotation.url
2241
+ title: (_j = annotation.title) != null ? _j : annotation.url
2195
2242
  });
2196
2243
  }
2197
2244
  }
@@ -2300,7 +2347,7 @@ var xaiTools = {
2300
2347
  };
2301
2348
 
2302
2349
  // src/version.ts
2303
- var VERSION = true ? "2.0.65" : "0.0.0-test";
2350
+ var VERSION = true ? "2.0.67" : "0.0.0-test";
2304
2351
 
2305
2352
  // src/xai-provider.ts
2306
2353
  var xaiErrorStructure = {