@ai-sdk/xai 2.0.47 → 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 {
@@ -1845,28 +1913,29 @@ var XaiResponsesLanguageModel = class {
1845
1913
  if (event.type === "response.output_item.added" || event.type === "response.output_item.done") {
1846
1914
  const part = event.item;
1847
1915
  if (part.type === "web_search_call" || part.type === "x_search_call" || part.type === "code_interpreter_call" || part.type === "code_execution_call" || part.type === "view_image_call" || part.type === "view_x_video_call" || part.type === "custom_tool_call") {
1848
- if (!seenToolCalls.has(part.id)) {
1916
+ const webSearchSubTools = [
1917
+ "web_search",
1918
+ "web_search_with_snippets",
1919
+ "browse_page"
1920
+ ];
1921
+ const xSearchSubTools = [
1922
+ "x_user_search",
1923
+ "x_keyword_search",
1924
+ "x_semantic_search",
1925
+ "x_thread_fetch"
1926
+ ];
1927
+ let toolName = (_e = part.name) != null ? _e : "";
1928
+ if (webSearchSubTools.includes((_f = part.name) != null ? _f : "") || part.type === "web_search_call") {
1929
+ toolName = webSearchToolName != null ? webSearchToolName : "web_search";
1930
+ } else if (xSearchSubTools.includes((_g = part.name) != null ? _g : "") || part.type === "x_search_call") {
1931
+ toolName = xSearchToolName != null ? xSearchToolName : "x_search";
1932
+ } else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
1933
+ toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
1934
+ }
1935
+ const toolInput = part.type === "custom_tool_call" ? (_h = part.input) != null ? _h : "" : (_i = part.arguments) != null ? _i : "";
1936
+ const shouldEmit = part.type === "custom_tool_call" ? event.type === "response.output_item.done" : !seenToolCalls.has(part.id);
1937
+ if (shouldEmit && !seenToolCalls.has(part.id)) {
1849
1938
  seenToolCalls.add(part.id);
1850
- const webSearchSubTools = [
1851
- "web_search",
1852
- "web_search_with_snippets",
1853
- "browse_page"
1854
- ];
1855
- const xSearchSubTools = [
1856
- "x_user_search",
1857
- "x_keyword_search",
1858
- "x_semantic_search",
1859
- "x_thread_fetch"
1860
- ];
1861
- let toolName = (_e = part.name) != null ? _e : "";
1862
- if (webSearchSubTools.includes((_f = part.name) != null ? _f : "") || part.type === "web_search_call") {
1863
- toolName = webSearchToolName != null ? webSearchToolName : "web_search";
1864
- } else if (xSearchSubTools.includes((_g = part.name) != null ? _g : "") || part.type === "x_search_call") {
1865
- toolName = xSearchToolName != null ? xSearchToolName : "x_search";
1866
- } else if (part.name === "code_execution" || part.type === "code_interpreter_call" || part.type === "code_execution_call") {
1867
- toolName = codeExecutionToolName != null ? codeExecutionToolName : "code_execution";
1868
- }
1869
- const toolInput = part.type === "custom_tool_call" ? (_h = part.input) != null ? _h : "" : (_i = part.arguments) != null ? _i : "";
1870
1939
  controller.enqueue({
1871
1940
  type: "tool-input-start",
1872
1941
  id: part.id,
@@ -1901,12 +1970,12 @@ var XaiResponsesLanguageModel = class {
1901
1970
  type: "text-start",
1902
1971
  id: blockId
1903
1972
  });
1973
+ controller.enqueue({
1974
+ type: "text-delta",
1975
+ id: blockId,
1976
+ delta: contentPart.text
1977
+ });
1904
1978
  }
1905
- controller.enqueue({
1906
- type: "text-delta",
1907
- id: blockId,
1908
- delta: contentPart.text
1909
- });
1910
1979
  }
1911
1980
  if (contentPart.annotations) {
1912
1981
  for (const annotation of contentPart.annotations) {
@@ -2024,7 +2093,7 @@ var xaiTools = {
2024
2093
  };
2025
2094
 
2026
2095
  // src/version.ts
2027
- var VERSION = true ? "2.0.47" : "0.0.0-test";
2096
+ var VERSION = true ? "2.0.49" : "0.0.0-test";
2028
2097
 
2029
2098
  // src/xai-provider.ts
2030
2099
  var xaiErrorStructure = {