@ai-sdk/xai 4.0.25 → 4.0.26

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,23 @@
1
1
  # @ai-sdk/xai
2
2
 
3
+ ## 4.0.26
4
+
5
+ ### Patch Changes
6
+
7
+ - 79e133c: async APIs for generateVideo (poll, webhook)
8
+
9
+ Adds an asynchronous start/status flow to the experimental video model
10
+ interface (`VideoModelV4`): models may now implement `doStart`, `doStatus`,
11
+ and `handleWebhookOption` instead of (or in addition to) `doGenerate`, and
12
+ `experimental_generateVideo` accepts `poll` and `webhook` options to
13
+ orchestrate completion via polling or webhooks. Polling configuration can use
14
+ a custom delay implementation for durable workflow compatibility.
15
+
16
+ - Updated dependencies [79e133c]
17
+ - @ai-sdk/provider@4.0.5
18
+ - @ai-sdk/openai-compatible@3.0.21
19
+ - @ai-sdk/provider-utils@5.0.19
20
+
3
21
  ## 4.0.25
4
22
 
5
23
  ### Patch Changes
package/dist/index.js CHANGED
@@ -3510,7 +3510,7 @@ var xaiTools = {
3510
3510
  };
3511
3511
 
3512
3512
  // src/version.ts
3513
- var VERSION = true ? "4.0.25" : "0.0.0-test";
3513
+ var VERSION = true ? "4.0.26" : "0.0.0-test";
3514
3514
 
3515
3515
  // src/files/xai-files.ts
3516
3516
  import {
@@ -3622,7 +3622,6 @@ import {
3622
3622
  combineHeaders as combineHeaders5,
3623
3623
  convertUint8ArrayToBase64,
3624
3624
  createJsonResponseHandler as createJsonResponseHandler5,
3625
- delay,
3626
3625
  extractResponseHeaders as extractResponseHeaders2,
3627
3626
  getFromApi as getFromApi2,
3628
3627
  getTopLevelMediaType as getTopLevelMediaType3,
@@ -3763,9 +3762,7 @@ var XaiVideoModel = class {
3763
3762
  get provider() {
3764
3763
  return this.config.provider;
3765
3764
  }
3766
- async doGenerate(options) {
3767
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
3768
- const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
3765
+ async buildRequestBody(options) {
3769
3766
  const warnings = [];
3770
3767
  const xaiOptions = await parseProviderOptions7({
3771
3768
  provider: "xai",
@@ -3921,6 +3918,20 @@ var XaiVideoModel = class {
3921
3918
  }
3922
3919
  }
3923
3920
  }
3921
+ return {
3922
+ body,
3923
+ warnings,
3924
+ xaiOptions,
3925
+ isEdit,
3926
+ isExtension,
3927
+ hasReferenceImages,
3928
+ effectiveMode
3929
+ };
3930
+ }
3931
+ async doStart(options) {
3932
+ var _a, _b, _c, _d;
3933
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
3934
+ const { body, warnings, isEdit, isExtension } = await this.buildRequestBody(options);
3924
3935
  const baseURL = (_d = this.config.baseURL) != null ? _d : "https://api.x.ai/v1";
3925
3936
  let endpoint;
3926
3937
  if (isEdit) {
@@ -3930,7 +3941,7 @@ var XaiVideoModel = class {
3930
3941
  } else {
3931
3942
  endpoint = `${baseURL}/videos/generations`;
3932
3943
  }
3933
- const { value: createResponse } = await postJsonToApi4({
3944
+ const { value: createResponse, responseHeaders } = await postJsonToApi4({
3934
3945
  url: endpoint,
3935
3946
  headers: combineHeaders5(this.config.headers(), options.headers),
3936
3947
  body,
@@ -3945,82 +3956,103 @@ var XaiVideoModel = class {
3945
3956
  if (!requestId) {
3946
3957
  throw new AISDKError({
3947
3958
  name: "XAI_VIDEO_GENERATION_ERROR",
3948
- message: `No request_id returned from xAI API. Response: ${JSON.stringify(createResponse)}`
3959
+ message: `No request_id returned from xAI API.`
3949
3960
  });
3950
3961
  }
3951
- const pollIntervalMs = (_e = xaiOptions == null ? void 0 : xaiOptions.pollIntervalMs) != null ? _e : 5e3;
3952
- const pollTimeoutMs = (_f = xaiOptions == null ? void 0 : xaiOptions.pollTimeoutMs) != null ? _f : 6e5;
3953
- const startTime = Date.now();
3954
- let responseHeaders;
3955
- while (true) {
3956
- await delay(pollIntervalMs, { abortSignal: options.abortSignal });
3957
- if (Date.now() - startTime > pollTimeoutMs) {
3958
- throw new AISDKError({
3959
- name: "XAI_VIDEO_GENERATION_TIMEOUT",
3960
- message: `Video generation timed out after ${pollTimeoutMs}ms`
3961
- });
3962
+ return {
3963
+ operation: { requestId },
3964
+ warnings,
3965
+ response: {
3966
+ timestamp: currentDate,
3967
+ modelId: this.modelId,
3968
+ headers: responseHeaders
3962
3969
  }
3963
- const { value: statusResponse, responseHeaders: pollHeaders } = await getFromApi2({
3964
- url: `${baseURL}/videos/${requestId}`,
3965
- validateUrl: false,
3966
- headers: combineHeaders5(this.config.headers(), options.headers),
3967
- successfulResponseHandler: xaiVideoStatusResponseHandler,
3968
- failedResponseHandler: xaiFailedResponseHandler,
3969
- abortSignal: options.abortSignal,
3970
- fetch: this.config.fetch
3971
- });
3972
- responseHeaders = pollHeaders;
3973
- if (statusResponse.status === "done" || statusResponse.status == null && ((_g = statusResponse.video) == null ? void 0 : _g.url)) {
3974
- if (((_h = statusResponse.video) == null ? void 0 : _h.respect_moderation) === false) {
3975
- throw new AISDKError({
3976
- name: "XAI_VIDEO_MODERATION_ERROR",
3977
- message: "Video generation was blocked due to a content policy violation."
3978
- });
3970
+ };
3971
+ }
3972
+ async doStatus(options) {
3973
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
3974
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
3975
+ const { requestId } = options.operation;
3976
+ const baseURL = (_d = this.config.baseURL) != null ? _d : "https://api.x.ai/v1";
3977
+ const { value: statusResponse, responseHeaders } = await getFromApi2({
3978
+ url: `${baseURL}/videos/${requestId}`,
3979
+ validateUrl: false,
3980
+ headers: combineHeaders5(this.config.headers(), options.headers),
3981
+ successfulResponseHandler: xaiVideoStatusResponseHandler,
3982
+ failedResponseHandler: xaiFailedResponseHandler,
3983
+ abortSignal: options.abortSignal,
3984
+ fetch: this.config.fetch
3985
+ });
3986
+ if (statusResponse.status === "expired") {
3987
+ return {
3988
+ status: "error",
3989
+ error: "Video generation request expired.",
3990
+ response: {
3991
+ timestamp: currentDate,
3992
+ modelId: this.modelId,
3993
+ headers: responseHeaders
3979
3994
  }
3980
- if (!((_i = statusResponse.video) == null ? void 0 : _i.url)) {
3981
- throw new AISDKError({
3982
- name: "XAI_VIDEO_GENERATION_ERROR",
3983
- message: "Video generation completed but no video URL was returned."
3984
- });
3995
+ };
3996
+ }
3997
+ if (statusResponse.status === "failed") {
3998
+ const errorDetails = (_g = (_e = statusResponse.error) == null ? void 0 : _e.message) != null ? _g : (_f = statusResponse.error) == null ? void 0 : _f.code;
3999
+ return {
4000
+ status: "error",
4001
+ error: errorDetails != null ? `Video generation failed: ${errorDetails}` : "Video generation failed.",
4002
+ response: {
4003
+ timestamp: currentDate,
4004
+ modelId: this.modelId,
4005
+ headers: responseHeaders
3985
4006
  }
3986
- return {
3987
- videos: [
3988
- {
3989
- type: "url",
3990
- url: statusResponse.video.url,
3991
- mediaType: "video/mp4"
3992
- }
3993
- ],
3994
- warnings,
3995
- response: {
3996
- timestamp: currentDate,
3997
- modelId: this.modelId,
3998
- headers: responseHeaders
3999
- },
4000
- providerMetadata: {
4001
- xai: {
4002
- requestId,
4003
- videoUrl: statusResponse.video.url,
4004
- ...statusResponse.video.duration != null ? { duration: statusResponse.video.duration } : {},
4005
- ...((_j = statusResponse.usage) == null ? void 0 : _j.cost_in_usd_ticks) != null ? { costInUsdTicks: statusResponse.usage.cost_in_usd_ticks } : {},
4006
- ...statusResponse.progress != null ? { progress: statusResponse.progress } : {}
4007
- }
4008
- }
4009
- };
4010
- }
4011
- if (statusResponse.status === "expired") {
4007
+ };
4008
+ }
4009
+ if (statusResponse.status === "done" || statusResponse.status == null && ((_h = statusResponse.video) == null ? void 0 : _h.url)) {
4010
+ if (((_i = statusResponse.video) == null ? void 0 : _i.respect_moderation) === false) {
4012
4011
  throw new AISDKError({
4013
- name: "XAI_VIDEO_GENERATION_EXPIRED",
4014
- message: "Video generation request expired."
4012
+ name: "XAI_VIDEO_MODERATION_ERROR",
4013
+ message: "Video generation was blocked due to a content policy violation."
4015
4014
  });
4016
4015
  }
4017
- if (statusResponse.status === "failed") {
4016
+ if (!((_j = statusResponse.video) == null ? void 0 : _j.url)) {
4018
4017
  throw new AISDKError({
4019
- name: "XAI_VIDEO_GENERATION_FAILED",
4020
- message: "Video generation failed."
4018
+ name: "XAI_VIDEO_GENERATION_ERROR",
4019
+ message: "Video generation completed but no video URL was returned."
4021
4020
  });
4022
4021
  }
4022
+ return {
4023
+ status: "completed",
4024
+ videos: [
4025
+ {
4026
+ type: "url",
4027
+ url: statusResponse.video.url,
4028
+ mediaType: "video/mp4"
4029
+ }
4030
+ ],
4031
+ warnings: [],
4032
+ response: {
4033
+ timestamp: currentDate,
4034
+ modelId: this.modelId,
4035
+ headers: responseHeaders
4036
+ },
4037
+ providerMetadata: {
4038
+ xai: {
4039
+ requestId,
4040
+ videoUrl: statusResponse.video.url,
4041
+ ...statusResponse.video.duration != null ? { duration: statusResponse.video.duration } : {},
4042
+ ...((_k = statusResponse.usage) == null ? void 0 : _k.cost_in_usd_ticks) != null ? { costInUsdTicks: statusResponse.usage.cost_in_usd_ticks } : {},
4043
+ ...statusResponse.progress != null ? { progress: statusResponse.progress } : {}
4044
+ }
4045
+ }
4046
+ };
4023
4047
  }
4048
+ return {
4049
+ status: "pending",
4050
+ response: {
4051
+ timestamp: currentDate,
4052
+ modelId: this.modelId,
4053
+ headers: responseHeaders
4054
+ }
4055
+ };
4024
4056
  }
4025
4057
  };
4026
4058
  var xaiCreateVideoResponseSchema = z19.object({