@ai-sdk/xai 4.0.0-beta.21 → 4.0.0-beta.23

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
@@ -30,7 +30,11 @@ import { z as z3 } from "zod/v4";
30
30
  import {
31
31
  UnsupportedFunctionalityError
32
32
  } from "@ai-sdk/provider";
33
- import { convertToBase64 } from "@ai-sdk/provider-utils";
33
+ import {
34
+ convertToBase64,
35
+ isProviderReference,
36
+ resolveProviderReference
37
+ } from "@ai-sdk/provider-utils";
34
38
  function convertToXaiChatMessages(prompt) {
35
39
  var _a;
36
40
  const messages = [];
@@ -54,6 +58,17 @@ function convertToXaiChatMessages(prompt) {
54
58
  return { type: "text", text: part.text };
55
59
  }
56
60
  case "file": {
61
+ if (isProviderReference(part.data)) {
62
+ return {
63
+ type: "file",
64
+ file: {
65
+ file_id: resolveProviderReference({
66
+ reference: part.data,
67
+ provider: "xai"
68
+ })
69
+ }
70
+ };
71
+ }
57
72
  if (part.mediaType.startsWith("image/")) {
58
73
  const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
59
74
  return {
@@ -1066,7 +1081,11 @@ import {
1066
1081
  import {
1067
1082
  UnsupportedFunctionalityError as UnsupportedFunctionalityError3
1068
1083
  } from "@ai-sdk/provider";
1069
- import { convertToBase64 as convertToBase642 } from "@ai-sdk/provider-utils";
1084
+ import {
1085
+ convertToBase64 as convertToBase642,
1086
+ isProviderReference as isProviderReference2,
1087
+ resolveProviderReference as resolveProviderReference2
1088
+ } from "@ai-sdk/provider-utils";
1070
1089
  async function convertToXaiResponsesInput({
1071
1090
  prompt
1072
1091
  }) {
@@ -1091,7 +1110,15 @@ async function convertToXaiResponsesInput({
1091
1110
  break;
1092
1111
  }
1093
1112
  case "file": {
1094
- if (block.mediaType.startsWith("image/")) {
1113
+ if (isProviderReference2(block.data)) {
1114
+ contentParts.push({
1115
+ type: "input_file",
1116
+ file_id: resolveProviderReference2({
1117
+ reference: block.data,
1118
+ provider: "xai"
1119
+ })
1120
+ });
1121
+ } else if (block.mediaType.startsWith("image/")) {
1095
1122
  const mediaType = block.mediaType === "image/*" ? "image/jpeg" : block.mediaType;
1096
1123
  const imageUrl = block.data instanceof URL ? block.data.toString() : `data:${mediaType};base64,${convertToBase642(block.data)}`;
1097
1124
  contentParts.push({ type: "input_image", image_url: imageUrl });
@@ -2778,43 +2805,190 @@ var xaiTools = {
2778
2805
  };
2779
2806
 
2780
2807
  // src/version.ts
2781
- var VERSION = true ? "4.0.0-beta.21" : "0.0.0-test";
2808
+ var VERSION = true ? "4.0.0-beta.23" : "0.0.0-test";
2782
2809
 
2783
- // src/xai-video-model.ts
2784
- import {
2785
- AISDKError
2786
- } from "@ai-sdk/provider";
2810
+ // src/files/xai-files.ts
2787
2811
  import {
2788
2812
  combineHeaders as combineHeaders4,
2789
- convertUint8ArrayToBase64,
2813
+ convertBase64ToUint8Array,
2790
2814
  createJsonResponseHandler as createJsonResponseHandler4,
2791
- delay,
2792
- getFromApi as getFromApi2,
2793
2815
  parseProviderOptions as parseProviderOptions4,
2794
- postJsonToApi as postJsonToApi4
2816
+ postFormDataToApi
2795
2817
  } from "@ai-sdk/provider-utils";
2796
- import { z as z16 } from "zod/v4";
2797
2818
 
2798
- // src/xai-video-options.ts
2819
+ // src/files/xai-files-api.ts
2799
2820
  import { lazySchema as lazySchema5, zodSchema as zodSchema5 } from "@ai-sdk/provider-utils";
2800
2821
  import { z as z15 } from "zod/v4";
2801
- var xaiVideoModelOptionsSchema = lazySchema5(
2822
+ var xaiFilesResponseSchema = lazySchema5(
2802
2823
  () => zodSchema5(
2803
2824
  z15.object({
2804
- pollIntervalMs: z15.number().positive().nullish(),
2805
- pollTimeoutMs: z15.number().positive().nullish(),
2806
- resolution: z15.enum(["480p", "720p"]).nullish(),
2807
- videoUrl: z15.string().nullish()
2825
+ id: z15.string(),
2826
+ object: z15.string().nullish(),
2827
+ bytes: z15.number().nullish(),
2828
+ created_at: z15.number().nullish(),
2829
+ filename: z15.string().nullish(),
2830
+ purpose: z15.string().nullish(),
2831
+ status: z15.string().nullish()
2832
+ })
2833
+ )
2834
+ );
2835
+
2836
+ // src/files/xai-files-options.ts
2837
+ import { lazySchema as lazySchema6, zodSchema as zodSchema6 } from "@ai-sdk/provider-utils";
2838
+ import { z as z16 } from "zod/v4";
2839
+ var xaiFilesOptionsSchema = lazySchema6(
2840
+ () => zodSchema6(
2841
+ z16.object({
2842
+ teamId: z16.string().optional(),
2843
+ filePath: z16.string().optional()
2808
2844
  }).passthrough()
2809
2845
  )
2810
2846
  );
2811
2847
 
2848
+ // src/files/xai-files.ts
2849
+ var XaiFiles = class {
2850
+ constructor(config) {
2851
+ this.config = config;
2852
+ this.specificationVersion = "v4";
2853
+ }
2854
+ get provider() {
2855
+ return this.config.provider;
2856
+ }
2857
+ async uploadFile({
2858
+ data,
2859
+ mediaType,
2860
+ filename,
2861
+ providerOptions
2862
+ }) {
2863
+ var _a, _b;
2864
+ const xaiOptions = await parseProviderOptions4({
2865
+ provider: "xai",
2866
+ providerOptions,
2867
+ schema: xaiFilesOptionsSchema
2868
+ });
2869
+ const fileBytes = data instanceof Uint8Array ? data : convertBase64ToUint8Array(data);
2870
+ const blob = new Blob([fileBytes], {
2871
+ type: mediaType
2872
+ });
2873
+ const formData = new FormData();
2874
+ if (filename != null) {
2875
+ formData.append("file", blob, filename);
2876
+ } else {
2877
+ formData.append("file", blob);
2878
+ }
2879
+ if ((xaiOptions == null ? void 0 : xaiOptions.teamId) != null) {
2880
+ formData.append("team_id", xaiOptions.teamId);
2881
+ }
2882
+ const { value: response } = await postFormDataToApi({
2883
+ url: `${this.config.baseURL}/files`,
2884
+ headers: combineHeaders4(this.config.headers()),
2885
+ formData,
2886
+ failedResponseHandler: xaiFailedResponseHandler,
2887
+ successfulResponseHandler: createJsonResponseHandler4(
2888
+ xaiFilesResponseSchema
2889
+ ),
2890
+ fetch: this.config.fetch
2891
+ });
2892
+ return {
2893
+ warnings: [],
2894
+ providerReference: { xai: response.id },
2895
+ ...((_a = response.filename) != null ? _a : filename) ? { filename: (_b = response.filename) != null ? _b : filename } : {},
2896
+ ...mediaType != null ? { mediaType } : {},
2897
+ providerMetadata: {
2898
+ xai: {
2899
+ ...response.filename != null ? { filename: response.filename } : {},
2900
+ ...response.bytes != null ? { bytes: response.bytes } : {},
2901
+ ...response.created_at != null ? { createdAt: response.created_at } : {}
2902
+ }
2903
+ }
2904
+ };
2905
+ }
2906
+ };
2907
+
2908
+ // src/xai-video-model.ts
2909
+ import {
2910
+ AISDKError
2911
+ } from "@ai-sdk/provider";
2912
+ import {
2913
+ combineHeaders as combineHeaders5,
2914
+ convertUint8ArrayToBase64,
2915
+ createJsonResponseHandler as createJsonResponseHandler5,
2916
+ delay,
2917
+ getFromApi as getFromApi2,
2918
+ parseProviderOptions as parseProviderOptions5,
2919
+ postJsonToApi as postJsonToApi4
2920
+ } from "@ai-sdk/provider-utils";
2921
+ import { z as z18 } from "zod/v4";
2922
+
2923
+ // src/xai-video-options.ts
2924
+ import { lazySchema as lazySchema7, zodSchema as zodSchema7 } from "@ai-sdk/provider-utils";
2925
+ import { z as z17 } from "zod/v4";
2926
+ var nonEmptyStringSchema = z17.string().min(1);
2927
+ var resolutionSchema = z17.enum(["480p", "720p"]);
2928
+ var modeSchema = z17.enum(["edit-video", "extend-video", "reference-to-video"]);
2929
+ var baseFields = {
2930
+ pollIntervalMs: z17.number().positive().nullish(),
2931
+ pollTimeoutMs: z17.number().positive().nullish(),
2932
+ resolution: resolutionSchema.nullish()
2933
+ };
2934
+ var editVideoSchema = z17.object({
2935
+ ...baseFields,
2936
+ mode: z17.literal("edit-video"),
2937
+ videoUrl: nonEmptyStringSchema,
2938
+ referenceImageUrls: z17.undefined().optional()
2939
+ });
2940
+ var extendVideoSchema = z17.object({
2941
+ ...baseFields,
2942
+ mode: z17.literal("extend-video"),
2943
+ videoUrl: nonEmptyStringSchema,
2944
+ referenceImageUrls: z17.undefined().optional()
2945
+ });
2946
+ var referenceToVideoSchema = z17.object({
2947
+ ...baseFields,
2948
+ mode: z17.literal("reference-to-video"),
2949
+ referenceImageUrls: z17.array(nonEmptyStringSchema).min(1).max(7),
2950
+ videoUrl: z17.undefined().optional()
2951
+ });
2952
+ var autoDetectSchema = z17.object({
2953
+ ...baseFields,
2954
+ mode: z17.undefined().optional(),
2955
+ videoUrl: nonEmptyStringSchema.optional(),
2956
+ referenceImageUrls: z17.array(nonEmptyStringSchema).min(1).max(7).optional()
2957
+ });
2958
+ var xaiVideoModelOptions = z17.union([
2959
+ editVideoSchema,
2960
+ extendVideoSchema,
2961
+ referenceToVideoSchema,
2962
+ autoDetectSchema
2963
+ ]);
2964
+ var runtimeSchema = z17.object({
2965
+ mode: modeSchema.optional(),
2966
+ videoUrl: nonEmptyStringSchema.optional(),
2967
+ referenceImageUrls: z17.array(nonEmptyStringSchema).min(1).max(7).optional(),
2968
+ ...baseFields
2969
+ }).passthrough();
2970
+ var xaiVideoModelOptionsSchema = lazySchema7(
2971
+ () => zodSchema7(runtimeSchema)
2972
+ );
2973
+
2812
2974
  // src/xai-video-model.ts
2813
2975
  var RESOLUTION_MAP = {
2814
2976
  "1280x720": "720p",
2815
2977
  "854x480": "480p",
2816
2978
  "640x480": "480p"
2817
2979
  };
2980
+ function resolveVideoMode(options) {
2981
+ if ((options == null ? void 0 : options.mode) != null) {
2982
+ return options.mode;
2983
+ }
2984
+ if ((options == null ? void 0 : options.videoUrl) != null) {
2985
+ return "edit-video";
2986
+ }
2987
+ if ((options == null ? void 0 : options.referenceImageUrls) != null && options.referenceImageUrls.length > 0) {
2988
+ return "reference-to-video";
2989
+ }
2990
+ return void 0;
2991
+ }
2818
2992
  var XaiVideoModel = class {
2819
2993
  constructor(modelId, config) {
2820
2994
  this.modelId = modelId;
@@ -2829,12 +3003,15 @@ var XaiVideoModel = class {
2829
3003
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2830
3004
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
2831
3005
  const warnings = [];
2832
- const xaiOptions = await parseProviderOptions4({
3006
+ const xaiOptions = await parseProviderOptions5({
2833
3007
  provider: "xai",
2834
3008
  providerOptions: options.providerOptions,
2835
3009
  schema: xaiVideoModelOptionsSchema
2836
3010
  });
2837
- const isEdit = (xaiOptions == null ? void 0 : xaiOptions.videoUrl) != null;
3011
+ const effectiveMode = resolveVideoMode(xaiOptions);
3012
+ const isEdit = effectiveMode === "edit-video";
3013
+ const isExtension = effectiveMode === "extend-video";
3014
+ const hasReferenceImages = effectiveMode === "reference-to-video";
2838
3015
  if (options.fps != null) {
2839
3016
  warnings.push({
2840
3017
  type: "unsupported",
@@ -2877,19 +3054,36 @@ var XaiVideoModel = class {
2877
3054
  details: "xAI video editing does not support custom resolution."
2878
3055
  });
2879
3056
  }
3057
+ if (isExtension && options.aspectRatio != null) {
3058
+ warnings.push({
3059
+ type: "unsupported",
3060
+ feature: "aspectRatio",
3061
+ details: "xAI video extension does not support custom aspect ratio."
3062
+ });
3063
+ }
3064
+ if (isExtension && ((xaiOptions == null ? void 0 : xaiOptions.resolution) != null || options.resolution != null)) {
3065
+ warnings.push({
3066
+ type: "unsupported",
3067
+ feature: "resolution",
3068
+ details: "xAI video extension does not support custom resolution."
3069
+ });
3070
+ }
2880
3071
  const body = {
2881
3072
  model: this.modelId,
2882
3073
  prompt: options.prompt
2883
3074
  };
2884
- if (!isEdit && options.duration != null) {
3075
+ const allowDuration = !isEdit;
3076
+ const allowAspectRatio = !isEdit && !isExtension;
3077
+ const allowResolution = !isEdit && !isExtension;
3078
+ if (allowDuration && options.duration != null) {
2885
3079
  body.duration = options.duration;
2886
3080
  }
2887
- if (!isEdit && options.aspectRatio != null) {
3081
+ if (allowAspectRatio && options.aspectRatio != null) {
2888
3082
  body.aspect_ratio = options.aspectRatio;
2889
3083
  }
2890
- if (!isEdit && (xaiOptions == null ? void 0 : xaiOptions.resolution) != null) {
3084
+ if (allowResolution && (xaiOptions == null ? void 0 : xaiOptions.resolution) != null) {
2891
3085
  body.resolution = xaiOptions.resolution;
2892
- } else if (!isEdit && options.resolution != null) {
3086
+ } else if (allowResolution && options.resolution != null) {
2893
3087
  const mapped = RESOLUTION_MAP[options.resolution];
2894
3088
  if (mapped != null) {
2895
3089
  body.resolution = mapped;
@@ -2901,7 +3095,10 @@ var XaiVideoModel = class {
2901
3095
  });
2902
3096
  }
2903
3097
  }
2904
- if ((xaiOptions == null ? void 0 : xaiOptions.videoUrl) != null) {
3098
+ if (isEdit) {
3099
+ body.video = { url: xaiOptions.videoUrl };
3100
+ }
3101
+ if (isExtension) {
2905
3102
  body.video = { url: xaiOptions.videoUrl };
2906
3103
  }
2907
3104
  if (options.image != null) {
@@ -2914,25 +3111,40 @@ var XaiVideoModel = class {
2914
3111
  };
2915
3112
  }
2916
3113
  }
3114
+ if (hasReferenceImages) {
3115
+ body.reference_images = xaiOptions.referenceImageUrls.map((url) => ({
3116
+ url
3117
+ }));
3118
+ }
2917
3119
  if (xaiOptions != null) {
2918
3120
  for (const [key, value] of Object.entries(xaiOptions)) {
2919
3121
  if (![
3122
+ "mode",
2920
3123
  "pollIntervalMs",
2921
3124
  "pollTimeoutMs",
2922
3125
  "resolution",
2923
- "videoUrl"
3126
+ "videoUrl",
3127
+ "referenceImageUrls"
2924
3128
  ].includes(key)) {
2925
3129
  body[key] = value;
2926
3130
  }
2927
3131
  }
2928
3132
  }
2929
3133
  const baseURL = (_d = this.config.baseURL) != null ? _d : "https://api.x.ai/v1";
3134
+ let endpoint;
3135
+ if (isEdit) {
3136
+ endpoint = `${baseURL}/videos/edits`;
3137
+ } else if (isExtension) {
3138
+ endpoint = `${baseURL}/videos/extensions`;
3139
+ } else {
3140
+ endpoint = `${baseURL}/videos/generations`;
3141
+ }
2930
3142
  const { value: createResponse } = await postJsonToApi4({
2931
- url: `${baseURL}/videos/${isEdit ? "edits" : "generations"}`,
2932
- headers: combineHeaders4(this.config.headers(), options.headers),
3143
+ url: endpoint,
3144
+ headers: combineHeaders5(this.config.headers(), options.headers),
2933
3145
  body,
2934
3146
  failedResponseHandler: xaiFailedResponseHandler,
2935
- successfulResponseHandler: createJsonResponseHandler4(
3147
+ successfulResponseHandler: createJsonResponseHandler5(
2936
3148
  xaiCreateVideoResponseSchema
2937
3149
  ),
2938
3150
  abortSignal: options.abortSignal,
@@ -2959,8 +3171,8 @@ var XaiVideoModel = class {
2959
3171
  }
2960
3172
  const { value: statusResponse, responseHeaders: pollHeaders } = await getFromApi2({
2961
3173
  url: `${baseURL}/videos/${requestId}`,
2962
- headers: combineHeaders4(this.config.headers(), options.headers),
2963
- successfulResponseHandler: createJsonResponseHandler4(
3174
+ headers: combineHeaders5(this.config.headers(), options.headers),
3175
+ successfulResponseHandler: createJsonResponseHandler5(
2964
3176
  xaiVideoStatusResponseSchema
2965
3177
  ),
2966
3178
  failedResponseHandler: xaiFailedResponseHandler,
@@ -3000,7 +3212,8 @@ var XaiVideoModel = class {
3000
3212
  requestId,
3001
3213
  videoUrl: statusResponse.video.url,
3002
3214
  ...statusResponse.video.duration != null ? { duration: statusResponse.video.duration } : {},
3003
- ...((_j = statusResponse.usage) == null ? void 0 : _j.cost_in_usd_ticks) != null ? { costInUsdTicks: statusResponse.usage.cost_in_usd_ticks } : {}
3215
+ ...((_j = statusResponse.usage) == null ? void 0 : _j.cost_in_usd_ticks) != null ? { costInUsdTicks: statusResponse.usage.cost_in_usd_ticks } : {},
3216
+ ...statusResponse.progress != null ? { progress: statusResponse.progress } : {}
3004
3217
  }
3005
3218
  }
3006
3219
  };
@@ -3011,22 +3224,33 @@ var XaiVideoModel = class {
3011
3224
  message: "Video generation request expired."
3012
3225
  });
3013
3226
  }
3227
+ if (statusResponse.status === "failed") {
3228
+ throw new AISDKError({
3229
+ name: "XAI_VIDEO_GENERATION_FAILED",
3230
+ message: "Video generation failed."
3231
+ });
3232
+ }
3014
3233
  }
3015
3234
  }
3016
3235
  };
3017
- var xaiCreateVideoResponseSchema = z16.object({
3018
- request_id: z16.string().nullish()
3236
+ var xaiCreateVideoResponseSchema = z18.object({
3237
+ request_id: z18.string().nullish()
3019
3238
  });
3020
- var xaiVideoStatusResponseSchema = z16.object({
3021
- status: z16.string().nullish(),
3022
- video: z16.object({
3023
- url: z16.string(),
3024
- duration: z16.number().nullish(),
3025
- respect_moderation: z16.boolean().nullish()
3239
+ var xaiVideoStatusResponseSchema = z18.object({
3240
+ status: z18.string().nullish(),
3241
+ video: z18.object({
3242
+ url: z18.string(),
3243
+ duration: z18.number().nullish(),
3244
+ respect_moderation: z18.boolean().nullish()
3245
+ }).nullish(),
3246
+ model: z18.string().nullish(),
3247
+ usage: z18.object({
3248
+ cost_in_usd_ticks: z18.number().nullish()
3026
3249
  }).nullish(),
3027
- model: z16.string().nullish(),
3028
- usage: z16.object({
3029
- cost_in_usd_ticks: z16.number().nullish()
3250
+ progress: z18.number().nullish(),
3251
+ error: z18.object({
3252
+ code: z18.string().nullish(),
3253
+ message: z18.string().nullish()
3030
3254
  }).nullish()
3031
3255
  });
3032
3256
 
@@ -3081,6 +3305,12 @@ function createXai(options = {}) {
3081
3305
  fetch: options.fetch
3082
3306
  });
3083
3307
  };
3308
+ const createFiles = () => new XaiFiles({
3309
+ provider: "xai.files",
3310
+ baseURL,
3311
+ headers: getHeaders,
3312
+ fetch: options.fetch
3313
+ });
3084
3314
  const provider = (modelId) => createResponsesLanguageModel(modelId);
3085
3315
  provider.specificationVersion = "v4";
3086
3316
  provider.languageModel = createResponsesLanguageModel;
@@ -3094,6 +3324,7 @@ function createXai(options = {}) {
3094
3324
  provider.image = createImageModel;
3095
3325
  provider.videoModel = createVideoModel;
3096
3326
  provider.video = createVideoModel;
3327
+ provider.files = createFiles;
3097
3328
  provider.tools = xaiTools;
3098
3329
  return provider;
3099
3330
  }