@ai-sdk/alibaba 1.0.1 → 1.0.3

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
@@ -3,14 +3,19 @@ import {
3
3
  NoSuchModelError
4
4
  } from "@ai-sdk/provider";
5
5
  import {
6
- createJsonErrorResponseHandler,
6
+ createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
7
7
  loadApiKey,
8
8
  withoutTrailingSlash,
9
9
  withUserAgentSuffix
10
10
  } from "@ai-sdk/provider-utils";
11
- import { z as z3 } from "zod/v4";
11
+ import { z as z4 } from "zod/v4";
12
12
 
13
13
  // src/alibaba-chat-language-model.ts
14
+ import {
15
+ getResponseMetadata,
16
+ mapOpenAICompatibleFinishReason,
17
+ prepareTools
18
+ } from "@ai-sdk/openai-compatible/internal";
14
19
  import {
15
20
  InvalidResponseDataError
16
21
  } from "@ai-sdk/provider";
@@ -24,15 +29,10 @@ import {
24
29
  postJsonToApi
25
30
  } from "@ai-sdk/provider-utils";
26
31
  import { z as z2 } from "zod/v4";
27
- import {
28
- getResponseMetadata,
29
- mapOpenAICompatibleFinishReason,
30
- prepareTools
31
- } from "@ai-sdk/openai-compatible/internal";
32
32
 
33
33
  // src/alibaba-chat-options.ts
34
34
  import { z } from "zod/v4";
35
- var alibabaProviderOptions = z.object({
35
+ var alibabaLanguageModelOptions = z.object({
36
36
  /**
37
37
  * Enable thinking/reasoning mode for supported models.
38
38
  * When enabled, the model generates reasoning content before the response.
@@ -52,6 +52,23 @@ var alibabaProviderOptions = z.object({
52
52
  parallelToolCalls: z.boolean().optional()
53
53
  });
54
54
 
55
+ // src/convert-alibaba-usage.ts
56
+ import { convertOpenAICompatibleChatUsage } from "@ai-sdk/openai-compatible/internal";
57
+ function convertAlibabaUsage(usage) {
58
+ var _a, _b, _c, _d;
59
+ const baseUsage = convertOpenAICompatibleChatUsage(usage);
60
+ const cacheWriteTokens = (_b = (_a = usage == null ? void 0 : usage.prompt_tokens_details) == null ? void 0 : _a.cache_creation_input_tokens) != null ? _b : 0;
61
+ const noCacheTokens = ((_c = baseUsage.inputTokens.total) != null ? _c : 0) - ((_d = baseUsage.inputTokens.cacheRead) != null ? _d : 0) - cacheWriteTokens;
62
+ return {
63
+ ...baseUsage,
64
+ inputTokens: {
65
+ ...baseUsage.inputTokens,
66
+ cacheWrite: cacheWriteTokens,
67
+ noCache: noCacheTokens
68
+ }
69
+ };
70
+ }
71
+
55
72
  // src/convert-to-alibaba-chat-messages.ts
56
73
  import {
57
74
  UnsupportedFunctionalityError
@@ -197,23 +214,6 @@ function convertToAlibabaChatMessages({
197
214
  return messages;
198
215
  }
199
216
 
200
- // src/convert-alibaba-usage.ts
201
- import { convertOpenAICompatibleChatUsage } from "@ai-sdk/openai-compatible/internal";
202
- function convertAlibabaUsage(usage) {
203
- var _a, _b, _c, _d;
204
- const baseUsage = convertOpenAICompatibleChatUsage(usage);
205
- const cacheWriteTokens = (_b = (_a = usage == null ? void 0 : usage.prompt_tokens_details) == null ? void 0 : _a.cache_creation_input_tokens) != null ? _b : 0;
206
- const noCacheTokens = ((_c = baseUsage.inputTokens.total) != null ? _c : 0) - ((_d = baseUsage.inputTokens.cacheRead) != null ? _d : 0) - cacheWriteTokens;
207
- return {
208
- ...baseUsage,
209
- inputTokens: {
210
- ...baseUsage.inputTokens,
211
- cacheWrite: cacheWriteTokens,
212
- noCache: noCacheTokens
213
- }
214
- };
215
- }
216
-
217
217
  // src/get-cache-control.ts
218
218
  var MAX_CACHE_BREAKPOINTS = 4;
219
219
  function getCacheControl(providerMetadata) {
@@ -286,7 +286,7 @@ var AlibabaLanguageModel = class {
286
286
  const alibabaOptions = await parseProviderOptions({
287
287
  provider: "alibaba",
288
288
  providerOptions,
289
- schema: alibabaProviderOptions
289
+ schema: alibabaLanguageModelOptions
290
290
  });
291
291
  if (frequencyPenalty != null) {
292
292
  warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
@@ -677,24 +677,310 @@ var alibabaChatChunkSchema = z2.object({
677
677
  // Usage only appears in final chunk
678
678
  });
679
679
 
680
+ // src/alibaba-video-model.ts
681
+ import {
682
+ AISDKError
683
+ } from "@ai-sdk/provider";
684
+ import {
685
+ combineHeaders as combineHeaders2,
686
+ convertUint8ArrayToBase64,
687
+ createJsonErrorResponseHandler,
688
+ createJsonResponseHandler as createJsonResponseHandler2,
689
+ delay,
690
+ getFromApi,
691
+ lazySchema,
692
+ parseProviderOptions as parseProviderOptions2,
693
+ postJsonToApi as postJsonToApi2,
694
+ resolve,
695
+ zodSchema
696
+ } from "@ai-sdk/provider-utils";
697
+ import { z as z3 } from "zod/v4";
698
+ var alibabaVideoModelOptionsSchema = lazySchema(
699
+ () => zodSchema(
700
+ z3.object({
701
+ negativePrompt: z3.string().nullish(),
702
+ audioUrl: z3.string().nullish(),
703
+ promptExtend: z3.boolean().nullish(),
704
+ shotType: z3.enum(["single", "multi"]).nullish(),
705
+ watermark: z3.boolean().nullish(),
706
+ audio: z3.boolean().nullish(),
707
+ referenceUrls: z3.array(z3.string()).nullish(),
708
+ pollIntervalMs: z3.number().positive().nullish(),
709
+ pollTimeoutMs: z3.number().positive().nullish()
710
+ }).passthrough()
711
+ )
712
+ );
713
+ var alibabaVideoErrorSchema = z3.object({
714
+ code: z3.string().nullish(),
715
+ message: z3.string(),
716
+ request_id: z3.string().nullish()
717
+ });
718
+ var alibabaVideoFailedResponseHandler = createJsonErrorResponseHandler({
719
+ errorSchema: alibabaVideoErrorSchema,
720
+ errorToMessage: (data) => data.message
721
+ });
722
+ var alibabaVideoCreateTaskSchema = z3.object({
723
+ output: z3.object({
724
+ task_status: z3.string(),
725
+ task_id: z3.string()
726
+ }).nullish(),
727
+ request_id: z3.string().nullish()
728
+ });
729
+ var alibabaVideoTaskStatusSchema = z3.object({
730
+ output: z3.object({
731
+ task_id: z3.string(),
732
+ task_status: z3.string(),
733
+ video_url: z3.string().nullish(),
734
+ submit_time: z3.string().nullish(),
735
+ scheduled_time: z3.string().nullish(),
736
+ end_time: z3.string().nullish(),
737
+ orig_prompt: z3.string().nullish(),
738
+ actual_prompt: z3.string().nullish(),
739
+ code: z3.string().nullish(),
740
+ message: z3.string().nullish()
741
+ }).nullish(),
742
+ usage: z3.object({
743
+ duration: z3.number().nullish(),
744
+ output_video_duration: z3.number().nullish(),
745
+ SR: z3.number().nullish(),
746
+ size: z3.string().nullish()
747
+ }).nullish(),
748
+ request_id: z3.string().nullish()
749
+ });
750
+ function detectMode(modelId) {
751
+ if (modelId.includes("-i2v")) return "i2v";
752
+ if (modelId.includes("-r2v")) return "r2v";
753
+ return "t2v";
754
+ }
755
+ var AlibabaVideoModel = class {
756
+ constructor(modelId, config) {
757
+ this.modelId = modelId;
758
+ this.config = config;
759
+ this.specificationVersion = "v3";
760
+ this.maxVideosPerCall = 1;
761
+ }
762
+ get provider() {
763
+ return this.config.provider;
764
+ }
765
+ async doGenerate(options) {
766
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
767
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
768
+ const warnings = [];
769
+ const mode = detectMode(this.modelId);
770
+ const alibabaOptions = await parseProviderOptions2({
771
+ provider: "alibaba",
772
+ providerOptions: options.providerOptions,
773
+ schema: alibabaVideoModelOptionsSchema
774
+ });
775
+ const input = {};
776
+ if (options.prompt != null) {
777
+ input.prompt = options.prompt;
778
+ }
779
+ if ((alibabaOptions == null ? void 0 : alibabaOptions.negativePrompt) != null) {
780
+ input.negative_prompt = alibabaOptions.negativePrompt;
781
+ }
782
+ if ((alibabaOptions == null ? void 0 : alibabaOptions.audioUrl) != null) {
783
+ input.audio_url = alibabaOptions.audioUrl;
784
+ }
785
+ if (mode === "i2v" && options.image != null) {
786
+ if (options.image.type === "url") {
787
+ input.img_url = options.image.url;
788
+ } else {
789
+ const base64Data = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase64(options.image.data);
790
+ input.img_url = base64Data;
791
+ }
792
+ }
793
+ if (mode === "r2v" && (alibabaOptions == null ? void 0 : alibabaOptions.referenceUrls) != null) {
794
+ input.reference_urls = alibabaOptions.referenceUrls;
795
+ }
796
+ const parameters = {};
797
+ if (options.duration != null) {
798
+ parameters.duration = options.duration;
799
+ }
800
+ if (options.seed != null) {
801
+ parameters.seed = options.seed;
802
+ }
803
+ if (options.resolution != null) {
804
+ if (mode === "i2v") {
805
+ const resolutionMap = {
806
+ "1280x720": "720P",
807
+ "720x1280": "720P",
808
+ "960x960": "720P",
809
+ "1088x832": "720P",
810
+ "832x1088": "720P",
811
+ "1920x1080": "1080P",
812
+ "1080x1920": "1080P",
813
+ "1440x1440": "1080P",
814
+ "1632x1248": "1080P",
815
+ "1248x1632": "1080P",
816
+ "832x480": "480P",
817
+ "480x832": "480P",
818
+ "624x624": "480P"
819
+ };
820
+ parameters.resolution = resolutionMap[options.resolution] || options.resolution;
821
+ } else {
822
+ parameters.size = options.resolution.replace("x", "*");
823
+ }
824
+ }
825
+ if ((alibabaOptions == null ? void 0 : alibabaOptions.promptExtend) != null) {
826
+ parameters.prompt_extend = alibabaOptions.promptExtend;
827
+ }
828
+ if ((alibabaOptions == null ? void 0 : alibabaOptions.shotType) != null) {
829
+ parameters.shot_type = alibabaOptions.shotType;
830
+ }
831
+ if ((alibabaOptions == null ? void 0 : alibabaOptions.watermark) != null) {
832
+ parameters.watermark = alibabaOptions.watermark;
833
+ }
834
+ if ((alibabaOptions == null ? void 0 : alibabaOptions.audio) != null) {
835
+ parameters.audio = alibabaOptions.audio;
836
+ }
837
+ if (options.aspectRatio) {
838
+ warnings.push({
839
+ type: "unsupported",
840
+ feature: "aspectRatio",
841
+ details: "Alibaba video models use explicit size/resolution dimensions. Use the resolution option or providerOptions.alibaba for size control."
842
+ });
843
+ }
844
+ if (options.fps) {
845
+ warnings.push({
846
+ type: "unsupported",
847
+ feature: "fps",
848
+ details: "Alibaba video models do not support custom FPS."
849
+ });
850
+ }
851
+ if (options.n != null && options.n > 1) {
852
+ warnings.push({
853
+ type: "unsupported",
854
+ feature: "n",
855
+ details: "Alibaba video models only support generating 1 video per call."
856
+ });
857
+ }
858
+ const { value: createResponse } = await postJsonToApi2({
859
+ url: `${this.config.baseURL}/api/v1/services/aigc/video-generation/video-synthesis`,
860
+ headers: combineHeaders2(
861
+ await resolve(this.config.headers),
862
+ options.headers,
863
+ {
864
+ "X-DashScope-Async": "enable"
865
+ }
866
+ ),
867
+ body: {
868
+ model: this.modelId,
869
+ input,
870
+ parameters
871
+ },
872
+ successfulResponseHandler: createJsonResponseHandler2(
873
+ alibabaVideoCreateTaskSchema
874
+ ),
875
+ failedResponseHandler: alibabaVideoFailedResponseHandler,
876
+ abortSignal: options.abortSignal,
877
+ fetch: this.config.fetch
878
+ });
879
+ const taskId = (_d = createResponse.output) == null ? void 0 : _d.task_id;
880
+ if (!taskId) {
881
+ throw new AISDKError({
882
+ name: "ALIBABA_VIDEO_GENERATION_ERROR",
883
+ message: `No task_id returned from Alibaba API. Response: ${JSON.stringify(createResponse)}`
884
+ });
885
+ }
886
+ const pollIntervalMs = (_e = alibabaOptions == null ? void 0 : alibabaOptions.pollIntervalMs) != null ? _e : 5e3;
887
+ const pollTimeoutMs = (_f = alibabaOptions == null ? void 0 : alibabaOptions.pollTimeoutMs) != null ? _f : 6e5;
888
+ const startTime = Date.now();
889
+ let finalResponse;
890
+ let responseHeaders;
891
+ while (true) {
892
+ await delay(pollIntervalMs, { abortSignal: options.abortSignal });
893
+ if (Date.now() - startTime > pollTimeoutMs) {
894
+ throw new AISDKError({
895
+ name: "ALIBABA_VIDEO_GENERATION_TIMEOUT",
896
+ message: `Video generation timed out after ${pollTimeoutMs}ms`
897
+ });
898
+ }
899
+ const { value: statusResponse, responseHeaders: pollHeaders } = await getFromApi({
900
+ url: `${this.config.baseURL}/api/v1/tasks/${taskId}`,
901
+ headers: combineHeaders2(
902
+ await resolve(this.config.headers),
903
+ options.headers
904
+ ),
905
+ successfulResponseHandler: createJsonResponseHandler2(
906
+ alibabaVideoTaskStatusSchema
907
+ ),
908
+ failedResponseHandler: alibabaVideoFailedResponseHandler,
909
+ abortSignal: options.abortSignal,
910
+ fetch: this.config.fetch
911
+ });
912
+ responseHeaders = pollHeaders;
913
+ const taskStatus = (_g = statusResponse.output) == null ? void 0 : _g.task_status;
914
+ if (taskStatus === "SUCCEEDED") {
915
+ finalResponse = statusResponse;
916
+ break;
917
+ }
918
+ if (taskStatus === "FAILED" || taskStatus === "CANCELED") {
919
+ throw new AISDKError({
920
+ name: "ALIBABA_VIDEO_GENERATION_FAILED",
921
+ message: `Video generation ${taskStatus.toLowerCase()}. Task ID: ${taskId}. ${(_i = (_h = statusResponse.output) == null ? void 0 : _h.message) != null ? _i : ""}`
922
+ });
923
+ }
924
+ }
925
+ const videoUrl = (_j = finalResponse == null ? void 0 : finalResponse.output) == null ? void 0 : _j.video_url;
926
+ if (!videoUrl) {
927
+ throw new AISDKError({
928
+ name: "ALIBABA_VIDEO_GENERATION_ERROR",
929
+ message: `No video URL in response. Task ID: ${taskId}`
930
+ });
931
+ }
932
+ return {
933
+ videos: [
934
+ {
935
+ type: "url",
936
+ url: videoUrl,
937
+ mediaType: "video/mp4"
938
+ }
939
+ ],
940
+ warnings,
941
+ response: {
942
+ timestamp: currentDate,
943
+ modelId: this.modelId,
944
+ headers: responseHeaders
945
+ },
946
+ providerMetadata: {
947
+ alibaba: {
948
+ taskId,
949
+ videoUrl,
950
+ ...((_k = finalResponse == null ? void 0 : finalResponse.output) == null ? void 0 : _k.actual_prompt) ? { actualPrompt: finalResponse.output.actual_prompt } : {},
951
+ ...(finalResponse == null ? void 0 : finalResponse.usage) ? {
952
+ usage: {
953
+ duration: finalResponse.usage.duration,
954
+ outputVideoDuration: finalResponse.usage.output_video_duration,
955
+ resolution: finalResponse.usage.SR,
956
+ size: finalResponse.usage.size
957
+ }
958
+ } : {}
959
+ }
960
+ }
961
+ };
962
+ }
963
+ };
964
+
680
965
  // src/version.ts
681
- var VERSION = "1.0.1";
966
+ var VERSION = "1.0.3";
682
967
 
683
968
  // src/alibaba-provider.ts
684
- var alibabaErrorDataSchema = z3.object({
685
- error: z3.object({
686
- message: z3.string(),
687
- code: z3.string().nullish(),
688
- type: z3.string().nullish()
969
+ var alibabaErrorDataSchema = z4.object({
970
+ error: z4.object({
971
+ message: z4.string(),
972
+ code: z4.string().nullish(),
973
+ type: z4.string().nullish()
689
974
  })
690
975
  });
691
- var alibabaFailedResponseHandler = createJsonErrorResponseHandler({
976
+ var alibabaFailedResponseHandler = createJsonErrorResponseHandler2({
692
977
  errorSchema: alibabaErrorDataSchema,
693
978
  errorToMessage: (data) => data.error.message
694
979
  });
695
980
  function createAlibaba(options = {}) {
696
- var _a;
981
+ var _a, _b;
697
982
  const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://dashscope-intl.aliyuncs.com/compatible-mode/v1";
983
+ const videoBaseURL = (_b = withoutTrailingSlash(options.videoBaseURL)) != null ? _b : "https://dashscope-intl.aliyuncs.com";
698
984
  const getHeaders = () => withUserAgentSuffix(
699
985
  {
700
986
  Authorization: `Bearer ${loadApiKey({
@@ -716,6 +1002,12 @@ function createAlibaba(options = {}) {
716
1002
  includeUsage: (_a2 = options.includeUsage) != null ? _a2 : true
717
1003
  });
718
1004
  };
1005
+ const createVideoModel = (modelId) => new AlibabaVideoModel(modelId, {
1006
+ provider: "alibaba.video",
1007
+ baseURL: videoBaseURL,
1008
+ headers: getHeaders,
1009
+ fetch: options.fetch
1010
+ });
719
1011
  const provider = function(modelId) {
720
1012
  if (new.target) {
721
1013
  throw new Error(
@@ -727,6 +1019,8 @@ function createAlibaba(options = {}) {
727
1019
  provider.specificationVersion = "v3";
728
1020
  provider.languageModel = createLanguageModel;
729
1021
  provider.chatModel = createLanguageModel;
1022
+ provider.video = createVideoModel;
1023
+ provider.videoModel = createVideoModel;
730
1024
  provider.imageModel = (modelId) => {
731
1025
  throw new NoSuchModelError({ modelId, modelType: "imageModel" });
732
1026
  };