@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/dist/index.mjs CHANGED
@@ -13,12 +13,17 @@ import {
13
13
  } from "@ai-sdk/provider-utils";
14
14
 
15
15
  // src/xai-chat-language-model.ts
16
+ import {
17
+ APICallError
18
+ } from "@ai-sdk/provider";
16
19
  import {
17
20
  combineHeaders,
18
21
  createEventSourceResponseHandler,
19
22
  createJsonResponseHandler,
23
+ extractResponseHeaders,
20
24
  parseProviderOptions,
21
- postJsonToApi
25
+ postJsonToApi,
26
+ safeParseJSON
22
27
  } from "@ai-sdk/provider-utils";
23
28
  import { z as z3 } from "zod/v4";
24
29
 
@@ -473,12 +478,13 @@ var XaiChatLanguageModel = class {
473
478
  async doGenerate(options) {
474
479
  var _a, _b;
475
480
  const { args: body, warnings } = await this.getArgs(options);
481
+ const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
476
482
  const {
477
483
  responseHeaders,
478
484
  value: response,
479
485
  rawValue: rawResponse
480
486
  } = await postJsonToApi({
481
- url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
487
+ url,
482
488
  headers: combineHeaders(this.config.headers(), options.headers),
483
489
  body,
484
490
  failedResponseHandler: xaiFailedResponseHandler,
@@ -488,6 +494,17 @@ var XaiChatLanguageModel = class {
488
494
  abortSignal: options.abortSignal,
489
495
  fetch: this.config.fetch
490
496
  });
497
+ if (response.error != null) {
498
+ throw new APICallError({
499
+ message: response.error,
500
+ url,
501
+ requestBodyValues: body,
502
+ statusCode: 200,
503
+ responseHeaders,
504
+ responseBody: JSON.stringify(rawResponse),
505
+ isRetryable: response.code === "The service is currently unavailable"
506
+ });
507
+ }
491
508
  const choice = response.choices[0];
492
509
  const content = [];
493
510
  if (choice.message.content != null && choice.message.content.length > 0) {
@@ -517,12 +534,12 @@ var XaiChatLanguageModel = class {
517
534
  }
518
535
  }
519
536
  if (response.citations != null) {
520
- for (const url of response.citations) {
537
+ for (const url2 of response.citations) {
521
538
  content.push({
522
539
  type: "source",
523
540
  sourceType: "url",
524
541
  id: this.config.generateId(),
525
- url
542
+ url: url2
526
543
  });
527
544
  }
528
545
  }
@@ -533,6 +550,7 @@ var XaiChatLanguageModel = class {
533
550
  raw: (_b = choice.finish_reason) != null ? _b : void 0
534
551
  },
535
552
  usage: convertXaiChatUsage(response.usage),
553
+ // defined when there is no error
536
554
  request: { body },
537
555
  response: {
538
556
  ...getResponseMetadata(response),
@@ -552,12 +570,47 @@ var XaiChatLanguageModel = class {
552
570
  include_usage: true
553
571
  }
554
572
  };
573
+ const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
555
574
  const { responseHeaders, value: response } = await postJsonToApi({
556
- url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
575
+ url,
557
576
  headers: combineHeaders(this.config.headers(), options.headers),
558
577
  body,
559
578
  failedResponseHandler: xaiFailedResponseHandler,
560
- successfulResponseHandler: createEventSourceResponseHandler(xaiChatChunkSchema),
579
+ successfulResponseHandler: async ({ response: response2 }) => {
580
+ const responseHeaders2 = extractResponseHeaders(response2);
581
+ const contentType = response2.headers.get("content-type");
582
+ if (contentType == null ? void 0 : contentType.includes("application/json")) {
583
+ const responseBody = await response2.text();
584
+ const parsedError = await safeParseJSON({
585
+ text: responseBody,
586
+ schema: xaiStreamErrorSchema
587
+ });
588
+ if (parsedError.success) {
589
+ throw new APICallError({
590
+ message: parsedError.value.error,
591
+ url,
592
+ requestBodyValues: body,
593
+ statusCode: 200,
594
+ responseHeaders: responseHeaders2,
595
+ responseBody,
596
+ isRetryable: parsedError.value.code === "The service is currently unavailable"
597
+ });
598
+ }
599
+ throw new APICallError({
600
+ message: "Invalid JSON response",
601
+ url,
602
+ requestBodyValues: body,
603
+ statusCode: 200,
604
+ responseHeaders: responseHeaders2,
605
+ responseBody
606
+ });
607
+ }
608
+ return createEventSourceResponseHandler(xaiChatChunkSchema)({
609
+ response: response2,
610
+ url,
611
+ requestBodyValues: body
612
+ });
613
+ },
561
614
  abortSignal: options.abortSignal,
562
615
  fetch: this.config.fetch
563
616
  });
@@ -593,12 +646,12 @@ var XaiChatLanguageModel = class {
593
646
  isFirstChunk = false;
594
647
  }
595
648
  if (value.citations != null) {
596
- for (const url of value.citations) {
649
+ for (const url2 of value.citations) {
597
650
  controller.enqueue({
598
651
  type: "source",
599
652
  sourceType: "url",
600
653
  id: self.config.generateId(),
601
- url
654
+ url: url2
602
655
  });
603
656
  }
604
657
  }
@@ -739,10 +792,12 @@ var xaiChatResponseSchema = z3.object({
739
792
  index: z3.number(),
740
793
  finish_reason: z3.string().nullish()
741
794
  })
742
- ),
743
- object: z3.literal("chat.completion"),
744
- usage: xaiUsageSchema,
745
- citations: z3.array(z3.string().url()).nullish()
795
+ ).nullish(),
796
+ object: z3.literal("chat.completion").nullish(),
797
+ usage: xaiUsageSchema.nullish(),
798
+ citations: z3.array(z3.string().url()).nullish(),
799
+ code: z3.string().nullish(),
800
+ error: z3.string().nullish()
746
801
  });
747
802
  var xaiChatChunkSchema = z3.object({
748
803
  id: z3.string().nullish(),
@@ -772,6 +827,10 @@ var xaiChatChunkSchema = z3.object({
772
827
  usage: xaiUsageSchema.nullish(),
773
828
  citations: z3.array(z3.string().url()).nullish()
774
829
  });
830
+ var xaiStreamErrorSchema = z3.object({
831
+ code: z3.string(),
832
+ error: z3.string()
833
+ });
775
834
 
776
835
  // src/responses/xai-responses-language-model.ts
777
836
  import {
@@ -2056,7 +2115,7 @@ var xaiTools = {
2056
2115
  };
2057
2116
 
2058
2117
  // src/version.ts
2059
- var VERSION = true ? "3.0.11" : "0.0.0-test";
2118
+ var VERSION = true ? "3.0.12" : "0.0.0-test";
2060
2119
 
2061
2120
  // src/xai-provider.ts
2062
2121
  var xaiErrorStructure = {