@ai-sdk/xai 2.0.48 → 2.0.49

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
 
@@ -452,14 +457,15 @@ var XaiChatLanguageModel = class {
452
457
  };
453
458
  }
454
459
  async doGenerate(options) {
455
- var _a, _b, _c, _d, _e;
460
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
456
461
  const { args: body, warnings } = await this.getArgs(options);
462
+ const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
457
463
  const {
458
464
  responseHeaders,
459
465
  value: response,
460
466
  rawValue: rawResponse
461
467
  } = await postJsonToApi({
462
- url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
468
+ url,
463
469
  headers: combineHeaders(this.config.headers(), options.headers),
464
470
  body,
465
471
  failedResponseHandler: xaiFailedResponseHandler,
@@ -469,6 +475,27 @@ var XaiChatLanguageModel = class {
469
475
  abortSignal: options.abortSignal,
470
476
  fetch: this.config.fetch
471
477
  });
478
+ if (response.error != null) {
479
+ throw new APICallError({
480
+ message: response.error,
481
+ url,
482
+ requestBodyValues: body,
483
+ statusCode: 200,
484
+ responseHeaders,
485
+ responseBody: JSON.stringify(rawResponse),
486
+ isRetryable: response.code === "The service is currently unavailable"
487
+ });
488
+ }
489
+ if (!response.choices || response.choices.length === 0) {
490
+ throw new APICallError({
491
+ message: "No choices returned from the API",
492
+ url,
493
+ requestBodyValues: body,
494
+ statusCode: 200,
495
+ responseHeaders,
496
+ responseBody: JSON.stringify(rawResponse)
497
+ });
498
+ }
472
499
  const choice = response.choices[0];
473
500
  const content = [];
474
501
  if (choice.message.content != null && choice.message.content.length > 0) {
@@ -498,12 +525,12 @@ var XaiChatLanguageModel = class {
498
525
  }
499
526
  }
500
527
  if (response.citations != null) {
501
- for (const url of response.citations) {
528
+ for (const url2 of response.citations) {
502
529
  content.push({
503
530
  type: "source",
504
531
  sourceType: "url",
505
532
  id: this.config.generateId(),
506
- url
533
+ url: url2
507
534
  });
508
535
  }
509
536
  }
@@ -511,11 +538,11 @@ var XaiChatLanguageModel = class {
511
538
  content,
512
539
  finishReason: mapXaiFinishReason(choice.finish_reason),
513
540
  usage: {
514
- inputTokens: response.usage.prompt_tokens,
515
- outputTokens: response.usage.completion_tokens,
516
- totalTokens: response.usage.total_tokens,
517
- reasoningTokens: (_c = (_b = response.usage.completion_tokens_details) == null ? void 0 : _b.reasoning_tokens) != null ? _c : void 0,
518
- cachedInputTokens: (_e = (_d = response.usage.prompt_tokens_details) == null ? void 0 : _d.cached_tokens) != null ? _e : void 0
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
519
546
  },
520
547
  request: { body },
521
548
  response: {
@@ -536,12 +563,47 @@ var XaiChatLanguageModel = class {
536
563
  include_usage: true
537
564
  }
538
565
  };
566
+ const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
539
567
  const { responseHeaders, value: response } = await postJsonToApi({
540
- url: `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`,
568
+ url,
541
569
  headers: combineHeaders(this.config.headers(), options.headers),
542
570
  body,
543
571
  failedResponseHandler: xaiFailedResponseHandler,
544
- successfulResponseHandler: createEventSourceResponseHandler(xaiChatChunkSchema),
572
+ successfulResponseHandler: async ({ response: response2 }) => {
573
+ const responseHeaders2 = extractResponseHeaders(response2);
574
+ const contentType = response2.headers.get("content-type");
575
+ if (contentType == null ? void 0 : contentType.includes("application/json")) {
576
+ const responseBody = await response2.text();
577
+ const parsedError = await safeParseJSON({
578
+ text: responseBody,
579
+ schema: xaiStreamErrorSchema
580
+ });
581
+ if (parsedError.success) {
582
+ throw new APICallError({
583
+ message: parsedError.value.error,
584
+ url,
585
+ requestBodyValues: body,
586
+ statusCode: 200,
587
+ responseHeaders: responseHeaders2,
588
+ responseBody,
589
+ isRetryable: parsedError.value.code === "The service is currently unavailable"
590
+ });
591
+ }
592
+ throw new APICallError({
593
+ message: "Invalid JSON response",
594
+ url,
595
+ requestBodyValues: body,
596
+ statusCode: 200,
597
+ responseHeaders: responseHeaders2,
598
+ responseBody
599
+ });
600
+ }
601
+ return createEventSourceResponseHandler(xaiChatChunkSchema)({
602
+ response: response2,
603
+ url,
604
+ requestBodyValues: body
605
+ });
606
+ },
545
607
  abortSignal: options.abortSignal,
546
608
  fetch: this.config.fetch
547
609
  });
@@ -581,12 +643,12 @@ var XaiChatLanguageModel = class {
581
643
  isFirstChunk = false;
582
644
  }
583
645
  if (value.citations != null) {
584
- for (const url of value.citations) {
646
+ for (const url2 of value.citations) {
585
647
  controller.enqueue({
586
648
  type: "source",
587
649
  sourceType: "url",
588
650
  id: self.config.generateId(),
589
- url
651
+ url: url2
590
652
  });
591
653
  }
592
654
  }
@@ -728,10 +790,12 @@ var xaiChatResponseSchema = z3.object({
728
790
  index: z3.number(),
729
791
  finish_reason: z3.string().nullish()
730
792
  })
731
- ),
732
- object: z3.literal("chat.completion"),
733
- usage: xaiUsageSchema,
734
- citations: z3.array(z3.string().url()).nullish()
793
+ ).nullish(),
794
+ object: z3.literal("chat.completion").nullish(),
795
+ usage: xaiUsageSchema.nullish(),
796
+ citations: z3.array(z3.string().url()).nullish(),
797
+ code: z3.string().nullish(),
798
+ error: z3.string().nullish()
735
799
  });
736
800
  var xaiChatChunkSchema = z3.object({
737
801
  id: z3.string().nullish(),
@@ -761,6 +825,10 @@ var xaiChatChunkSchema = z3.object({
761
825
  usage: xaiUsageSchema.nullish(),
762
826
  citations: z3.array(z3.string().url()).nullish()
763
827
  });
828
+ var xaiStreamErrorSchema = z3.object({
829
+ code: z3.string(),
830
+ error: z3.string()
831
+ });
764
832
 
765
833
  // src/responses/xai-responses-language-model.ts
766
834
  import {
@@ -2025,7 +2093,7 @@ var xaiTools = {
2025
2093
  };
2026
2094
 
2027
2095
  // src/version.ts
2028
- var VERSION = true ? "2.0.48" : "0.0.0-test";
2096
+ var VERSION = true ? "2.0.49" : "0.0.0-test";
2029
2097
 
2030
2098
  // src/xai-provider.ts
2031
2099
  var xaiErrorStructure = {