@ai-sdk/xai 3.0.11 → 3.0.12

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,11 @@
1
1
  # @ai-sdk/xai
2
2
 
3
+ ## 3.0.12
4
+
5
+ ### Patch Changes
6
+
7
+ - e7bdbc7: fix(provider/xai): handle error responses returned with 200 status
8
+
3
9
  ## 3.0.11
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -34,10 +34,11 @@ 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_provider4 = require("@ai-sdk/provider");
37
+ var import_provider5 = require("@ai-sdk/provider");
38
38
  var import_provider_utils11 = require("@ai-sdk/provider-utils");
39
39
 
40
40
  // src/xai-chat-language-model.ts
41
+ var import_provider3 = require("@ai-sdk/provider");
41
42
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
42
43
  var import_v43 = require("zod/v4");
43
44
 
@@ -488,12 +489,13 @@ var XaiChatLanguageModel = class {
488
489
  async doGenerate(options) {
489
490
  var _a, _b;
490
491
  const { args: body, warnings } = await this.getArgs(options);
492
+ const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
491
493
  const {
492
494
  responseHeaders,
493
495
  value: response,
494
496
  rawValue: rawResponse
495
497
  } = await (0, import_provider_utils3.postJsonToApi)({
496
- url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
498
+ url,
497
499
  headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
498
500
  body,
499
501
  failedResponseHandler: xaiFailedResponseHandler,
@@ -503,6 +505,17 @@ var XaiChatLanguageModel = class {
503
505
  abortSignal: options.abortSignal,
504
506
  fetch: this.config.fetch
505
507
  });
508
+ if (response.error != null) {
509
+ throw new import_provider3.APICallError({
510
+ message: response.error,
511
+ url,
512
+ requestBodyValues: body,
513
+ statusCode: 200,
514
+ responseHeaders,
515
+ responseBody: JSON.stringify(rawResponse),
516
+ isRetryable: response.code === "The service is currently unavailable"
517
+ });
518
+ }
506
519
  const choice = response.choices[0];
507
520
  const content = [];
508
521
  if (choice.message.content != null && choice.message.content.length > 0) {
@@ -532,12 +545,12 @@ var XaiChatLanguageModel = class {
532
545
  }
533
546
  }
534
547
  if (response.citations != null) {
535
- for (const url of response.citations) {
548
+ for (const url2 of response.citations) {
536
549
  content.push({
537
550
  type: "source",
538
551
  sourceType: "url",
539
552
  id: this.config.generateId(),
540
- url
553
+ url: url2
541
554
  });
542
555
  }
543
556
  }
@@ -548,6 +561,7 @@ var XaiChatLanguageModel = class {
548
561
  raw: (_b = choice.finish_reason) != null ? _b : void 0
549
562
  },
550
563
  usage: convertXaiChatUsage(response.usage),
564
+ // defined when there is no error
551
565
  request: { body },
552
566
  response: {
553
567
  ...getResponseMetadata(response),
@@ -567,12 +581,47 @@ var XaiChatLanguageModel = class {
567
581
  include_usage: true
568
582
  }
569
583
  };
584
+ const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
570
585
  const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
571
- url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
586
+ url,
572
587
  headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
573
588
  body,
574
589
  failedResponseHandler: xaiFailedResponseHandler,
575
- successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(xaiChatChunkSchema),
590
+ successfulResponseHandler: async ({ response: response2 }) => {
591
+ const responseHeaders2 = (0, import_provider_utils3.extractResponseHeaders)(response2);
592
+ const contentType = response2.headers.get("content-type");
593
+ if (contentType == null ? void 0 : contentType.includes("application/json")) {
594
+ const responseBody = await response2.text();
595
+ const parsedError = await (0, import_provider_utils3.safeParseJSON)({
596
+ text: responseBody,
597
+ schema: xaiStreamErrorSchema
598
+ });
599
+ if (parsedError.success) {
600
+ throw new import_provider3.APICallError({
601
+ message: parsedError.value.error,
602
+ url,
603
+ requestBodyValues: body,
604
+ statusCode: 200,
605
+ responseHeaders: responseHeaders2,
606
+ responseBody,
607
+ isRetryable: parsedError.value.code === "The service is currently unavailable"
608
+ });
609
+ }
610
+ throw new import_provider3.APICallError({
611
+ message: "Invalid JSON response",
612
+ url,
613
+ requestBodyValues: body,
614
+ statusCode: 200,
615
+ responseHeaders: responseHeaders2,
616
+ responseBody
617
+ });
618
+ }
619
+ return (0, import_provider_utils3.createEventSourceResponseHandler)(xaiChatChunkSchema)({
620
+ response: response2,
621
+ url,
622
+ requestBodyValues: body
623
+ });
624
+ },
576
625
  abortSignal: options.abortSignal,
577
626
  fetch: this.config.fetch
578
627
  });
@@ -608,12 +657,12 @@ var XaiChatLanguageModel = class {
608
657
  isFirstChunk = false;
609
658
  }
610
659
  if (value.citations != null) {
611
- for (const url of value.citations) {
660
+ for (const url2 of value.citations) {
612
661
  controller.enqueue({
613
662
  type: "source",
614
663
  sourceType: "url",
615
664
  id: self.config.generateId(),
616
- url
665
+ url: url2
617
666
  });
618
667
  }
619
668
  }
@@ -754,10 +803,12 @@ var xaiChatResponseSchema = import_v43.z.object({
754
803
  index: import_v43.z.number(),
755
804
  finish_reason: import_v43.z.string().nullish()
756
805
  })
757
- ),
758
- object: import_v43.z.literal("chat.completion"),
759
- usage: xaiUsageSchema,
760
- citations: import_v43.z.array(import_v43.z.string().url()).nullish()
806
+ ).nullish(),
807
+ object: import_v43.z.literal("chat.completion").nullish(),
808
+ usage: xaiUsageSchema.nullish(),
809
+ citations: import_v43.z.array(import_v43.z.string().url()).nullish(),
810
+ code: import_v43.z.string().nullish(),
811
+ error: import_v43.z.string().nullish()
761
812
  });
762
813
  var xaiChatChunkSchema = import_v43.z.object({
763
814
  id: import_v43.z.string().nullish(),
@@ -787,6 +838,10 @@ var xaiChatChunkSchema = import_v43.z.object({
787
838
  usage: xaiUsageSchema.nullish(),
788
839
  citations: import_v43.z.array(import_v43.z.string().url()).nullish()
789
840
  });
841
+ var xaiStreamErrorSchema = import_v43.z.object({
842
+ code: import_v43.z.string(),
843
+ error: import_v43.z.string()
844
+ });
790
845
 
791
846
  // src/responses/xai-responses-language-model.ts
792
847
  var import_provider_utils7 = require("@ai-sdk/provider-utils");
@@ -1284,7 +1339,7 @@ var xaiResponsesProviderOptions = import_v45.z.object({
1284
1339
  });
1285
1340
 
1286
1341
  // src/responses/xai-responses-prepare-tools.ts
1287
- var import_provider3 = require("@ai-sdk/provider");
1342
+ var import_provider4 = require("@ai-sdk/provider");
1288
1343
  var import_provider_utils6 = require("@ai-sdk/provider-utils");
1289
1344
 
1290
1345
  // src/tool/web-search.ts
@@ -1528,7 +1583,7 @@ async function prepareResponsesTools({
1528
1583
  }
1529
1584
  default: {
1530
1585
  const _exhaustiveCheck = type;
1531
- throw new import_provider3.UnsupportedFunctionalityError({
1586
+ throw new import_provider4.UnsupportedFunctionalityError({
1532
1587
  functionality: `tool choice type: ${_exhaustiveCheck}`
1533
1588
  });
1534
1589
  }
@@ -2055,7 +2110,7 @@ var xaiTools = {
2055
2110
  };
2056
2111
 
2057
2112
  // src/version.ts
2058
- var VERSION = true ? "3.0.11" : "0.0.0-test";
2113
+ var VERSION = true ? "3.0.12" : "0.0.0-test";
2059
2114
 
2060
2115
  // src/xai-provider.ts
2061
2116
  var xaiErrorStructure = {
@@ -2111,7 +2166,7 @@ function createXai(options = {}) {
2111
2166
  provider.chat = createChatLanguageModel;
2112
2167
  provider.responses = createResponsesLanguageModel;
2113
2168
  provider.embeddingModel = (modelId) => {
2114
- throw new import_provider4.NoSuchModelError({ modelId, modelType: "embeddingModel" });
2169
+ throw new import_provider5.NoSuchModelError({ modelId, modelType: "embeddingModel" });
2115
2170
  };
2116
2171
  provider.textEmbeddingModel = provider.embeddingModel;
2117
2172
  provider.imageModel = createImageModel;