@runpod/ai-sdk-provider 1.2.0 → 1.3.0

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.js CHANGED
@@ -27,7 +27,7 @@ module.exports = __toCommonJS(index_exports);
27
27
 
28
28
  // src/runpod-provider.ts
29
29
  var import_openai_compatible = require("@ai-sdk/openai-compatible");
30
- var import_provider_utils5 = require("@ai-sdk/provider-utils");
30
+ var import_provider_utils6 = require("@ai-sdk/provider-utils");
31
31
 
32
32
  // src/runpod-image-model.ts
33
33
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
@@ -69,6 +69,10 @@ var runpodTranscriptionFailedResponseHandler = (0, import_provider_utils.createJ
69
69
  errorSchema: runpodImageErrorSchema,
70
70
  errorToMessage: extractErrorMessage
71
71
  });
72
+ var runpodVideoFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
73
+ errorSchema: runpodImageErrorSchema,
74
+ errorToMessage: extractErrorMessage
75
+ });
72
76
 
73
77
  // src/runpod-image-model.ts
74
78
  var SUPPORTED_ASPECT_RATIOS = {
@@ -101,6 +105,50 @@ var SUPPORTED_SIZES = /* @__PURE__ */ new Set([
101
105
  "1024*768",
102
106
  "768*1024"
103
107
  ]);
108
+ var Z_IMAGE_TURBO_SUPPORTED_SIZES = /* @__PURE__ */ new Set([
109
+ "1328*1328",
110
+ // 1:1
111
+ "1472*1140",
112
+ // 4:3
113
+ "1140*1472",
114
+ // 3:4
115
+ "512*512",
116
+ "768*768",
117
+ "1024*1024",
118
+ "1280*1280",
119
+ "1536*1536",
120
+ "512*768",
121
+ "768*512",
122
+ "1024*768",
123
+ "768*1024",
124
+ "768*432",
125
+ "1024*576",
126
+ "1280*720",
127
+ "1536*864",
128
+ "432*768",
129
+ "576*1024",
130
+ "720*1280",
131
+ "864*1536"
132
+ ]);
133
+ var Z_IMAGE_TURBO_ASPECT_RATIOS = {
134
+ "1:1": "1328*1328",
135
+ "4:3": "1472*1140",
136
+ "3:4": "1140*1472",
137
+ "3:2": "768*512",
138
+ "2:3": "512*768",
139
+ "16:9": "1280*720",
140
+ "9:16": "720*1280"
141
+ };
142
+ var MODEL_SUPPORTED_SIZES = {
143
+ "tongyi-mai/z-image-turbo": Z_IMAGE_TURBO_SUPPORTED_SIZES,
144
+ "z-image-turbo": Z_IMAGE_TURBO_SUPPORTED_SIZES
145
+ // alias, not advertised
146
+ };
147
+ var MODEL_SUPPORTED_ASPECT_RATIOS = {
148
+ "tongyi-mai/z-image-turbo": Z_IMAGE_TURBO_ASPECT_RATIOS,
149
+ "z-image-turbo": Z_IMAGE_TURBO_ASPECT_RATIOS
150
+ // alias, not advertised
151
+ };
104
152
  var WAN_ASPECT_RATIOS = {
105
153
  "1:1": "1280*1280",
106
154
  // 1,638,400 pixels
@@ -206,23 +254,27 @@ var RunpodImageModel = class {
206
254
  }
207
255
  } else if (size) {
208
256
  const runpodSizeCandidate = size.replace("x", "*");
209
- if (!SUPPORTED_SIZES.has(runpodSizeCandidate)) {
257
+ const supportedSizes = MODEL_SUPPORTED_SIZES[this.modelId] ?? SUPPORTED_SIZES;
258
+ if (!supportedSizes.has(runpodSizeCandidate)) {
210
259
  throw new import_provider.InvalidArgumentError({
211
260
  argument: "size",
212
261
  message: `Size ${size} is not supported by Runpod. Supported sizes: ${Array.from(
213
- SUPPORTED_SIZES
262
+ supportedSizes
214
263
  ).map((s) => s.replace("*", "x")).join(", ")}`
215
264
  });
216
265
  }
217
266
  runpodSize = runpodSizeCandidate;
218
267
  } else if (aspectRatio) {
219
- if (!SUPPORTED_ASPECT_RATIOS[aspectRatio]) {
268
+ const supportedAspectRatios = MODEL_SUPPORTED_ASPECT_RATIOS[this.modelId] ?? SUPPORTED_ASPECT_RATIOS;
269
+ if (!supportedAspectRatios[aspectRatio]) {
220
270
  throw new import_provider.InvalidArgumentError({
221
271
  argument: "aspectRatio",
222
- message: `Aspect ratio ${aspectRatio} is not supported by Runpod. Supported aspect ratios: ${Object.keys(SUPPORTED_ASPECT_RATIOS).join(", ")}`
272
+ message: `Aspect ratio ${aspectRatio} is not supported by Runpod. Supported aspect ratios: ${Object.keys(
273
+ supportedAspectRatios
274
+ ).join(", ")}`
223
275
  });
224
276
  }
225
- runpodSize = SUPPORTED_ASPECT_RATIOS[aspectRatio];
277
+ runpodSize = supportedAspectRatios[aspectRatio];
226
278
  } else {
227
279
  runpodSize = "1328*1328";
228
280
  }
@@ -525,6 +577,25 @@ var RunpodImageModel = class {
525
577
  }
526
578
  return qwenEdit2511Payload;
527
579
  }
580
+ const isZImageTurbo = this.modelId === "tongyi-mai/z-image-turbo" || this.modelId === "z-image-turbo";
581
+ if (isZImageTurbo) {
582
+ const zImageTurboPayload = {
583
+ prompt,
584
+ size: runpodSize,
585
+ seed: seed ?? -1,
586
+ output_format: runpodOptions?.output_format ?? "png",
587
+ enable_safety_checker: runpodOptions?.enable_safety_checker ?? true,
588
+ ...runpodOptions
589
+ };
590
+ if (standardizedImages && standardizedImages.length > 0) {
591
+ if (standardizedImages.length === 1) {
592
+ zImageTurboPayload.image = standardizedImages[0];
593
+ } else {
594
+ zImageTurboPayload.images = standardizedImages;
595
+ }
596
+ }
597
+ return zImageTurboPayload;
598
+ }
528
599
  const isWanModel = this.modelId.includes("wan-2");
529
600
  if (isWanModel) {
530
601
  return {
@@ -938,6 +1009,210 @@ var RunpodTranscriptionModel = class {
938
1009
  }
939
1010
  };
940
1011
 
1012
+ // src/runpod-video-model.ts
1013
+ var import_provider_utils5 = require("@ai-sdk/provider-utils");
1014
+ var import_zod4 = require("zod");
1015
+ function isRecord3(value) {
1016
+ return typeof value === "object" && value !== null;
1017
+ }
1018
+ var runpodVideoJobResponseSchema = import_zod4.z.object({
1019
+ id: import_zod4.z.string(),
1020
+ status: import_zod4.z.string().optional()
1021
+ });
1022
+ var runpodVideoStatusSchema = import_zod4.z.object({
1023
+ id: import_zod4.z.string().optional(),
1024
+ status: import_zod4.z.string(),
1025
+ output: import_zod4.z.unknown().optional(),
1026
+ error: import_zod4.z.string().optional()
1027
+ });
1028
+ var RunpodVideoModel = class {
1029
+ constructor(modelId, config) {
1030
+ this.modelId = modelId;
1031
+ this.config = config;
1032
+ this.specificationVersion = "v3";
1033
+ this.maxVideosPerCall = 1;
1034
+ }
1035
+ get provider() {
1036
+ return this.config.provider;
1037
+ }
1038
+ getRunpodRunUrl() {
1039
+ const baseURL = (0, import_provider_utils5.withoutTrailingSlash)(this.config.baseURL) ?? this.config.baseURL;
1040
+ if (baseURL.endsWith("/run") || baseURL.endsWith("/runsync")) {
1041
+ return baseURL;
1042
+ }
1043
+ return `${baseURL}/run`;
1044
+ }
1045
+ async doGenerate(options) {
1046
+ const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
1047
+ const warnings = [];
1048
+ if (options.n > 1) {
1049
+ warnings.push({
1050
+ type: "unsupported",
1051
+ feature: "n > 1",
1052
+ details: "Runpod video models only support generating 1 video per call. Only 1 video will be generated."
1053
+ });
1054
+ }
1055
+ const { providerOptions, abortSignal, headers } = options;
1056
+ const runpodOptions = this.extractRunpodOptions(providerOptions);
1057
+ const input = this.buildInputPayload(options, runpodOptions);
1058
+ const requestBody = { input };
1059
+ const url = this.getRunpodRunUrl();
1060
+ const effectiveBaseURL = (0, import_provider_utils5.withoutTrailingSlash)(this.config.baseURL) ?? this.config.baseURL;
1061
+ const { value: response, responseHeaders } = await (0, import_provider_utils5.postJsonToApi)({
1062
+ url,
1063
+ headers: {
1064
+ ...this.config.headers(),
1065
+ ...headers
1066
+ },
1067
+ body: requestBody,
1068
+ failedResponseHandler: runpodVideoFailedResponseHandler,
1069
+ successfulResponseHandler: (0, import_provider_utils5.createJsonResponseHandler)(
1070
+ runpodVideoJobResponseSchema
1071
+ ),
1072
+ abortSignal,
1073
+ fetch: this.config.fetch
1074
+ });
1075
+ const typedResponse = response;
1076
+ const jobId = typedResponse.id;
1077
+ if (!jobId) {
1078
+ throw new Error("Runpod video response did not include a job id.");
1079
+ }
1080
+ const pollOptions = {
1081
+ maxAttempts: runpodOptions.maxPollAttempts ?? 120,
1082
+ pollIntervalMillis: runpodOptions.pollIntervalMillis ?? 5e3
1083
+ };
1084
+ const result = await this.pollForCompletion(
1085
+ jobId,
1086
+ abortSignal,
1087
+ pollOptions,
1088
+ effectiveBaseURL
1089
+ );
1090
+ const videoUrl = this.extractVideoUrl(result.output);
1091
+ const providerMetadata = {
1092
+ runpod: {
1093
+ jobId
1094
+ }
1095
+ };
1096
+ return {
1097
+ videos: [{ type: "url", url: videoUrl, mediaType: "video/mp4" }],
1098
+ warnings,
1099
+ response: {
1100
+ timestamp: currentDate,
1101
+ modelId: this.modelId,
1102
+ headers: responseHeaders
1103
+ },
1104
+ providerMetadata
1105
+ };
1106
+ }
1107
+ buildInputPayload(options, runpodOptions) {
1108
+ const apiOptions = Object.fromEntries(
1109
+ Object.entries(runpodOptions).filter(
1110
+ ([key]) => key !== "maxPollAttempts" && key !== "pollIntervalMillis"
1111
+ )
1112
+ );
1113
+ const input = {
1114
+ ...apiOptions
1115
+ };
1116
+ if (options.prompt) {
1117
+ input.prompt = options.prompt;
1118
+ }
1119
+ if (options.duration !== void 0) {
1120
+ input.duration = options.duration;
1121
+ }
1122
+ if (options.fps !== void 0) {
1123
+ input.fps = options.fps;
1124
+ }
1125
+ if (options.seed !== void 0) {
1126
+ input.seed = options.seed;
1127
+ }
1128
+ if (options.resolution) {
1129
+ input.size = options.resolution.replace("x", "*");
1130
+ } else if (options.aspectRatio) {
1131
+ input.aspect_ratio = options.aspectRatio;
1132
+ }
1133
+ if (options.image) {
1134
+ input.image = this.convertFileToRunpodFormat(options.image);
1135
+ }
1136
+ return input;
1137
+ }
1138
+ convertFileToRunpodFormat(file) {
1139
+ if (file.type === "url") {
1140
+ return file.url;
1141
+ }
1142
+ const mediaType = file.mediaType;
1143
+ const data = file.data;
1144
+ if (typeof data === "string") {
1145
+ return `data:${mediaType};base64,${data}`;
1146
+ }
1147
+ const base64 = this.uint8ArrayToBase64(data);
1148
+ return `data:${mediaType};base64,${base64}`;
1149
+ }
1150
+ uint8ArrayToBase64(data) {
1151
+ if (typeof Buffer !== "undefined") {
1152
+ return Buffer.from(data).toString("base64");
1153
+ }
1154
+ let binary = "";
1155
+ for (let i = 0; i < data.length; i++) {
1156
+ binary += String.fromCharCode(data[i]);
1157
+ }
1158
+ return btoa(binary);
1159
+ }
1160
+ extractRunpodOptions(providerOptions) {
1161
+ if (!providerOptions) return {};
1162
+ const runpod2 = providerOptions.runpod;
1163
+ if (isRecord3(runpod2)) {
1164
+ return runpod2;
1165
+ }
1166
+ return {};
1167
+ }
1168
+ extractVideoUrl(output) {
1169
+ if (isRecord3(output)) {
1170
+ if (typeof output.video_url === "string") return output.video_url;
1171
+ if (typeof output.result === "string") return output.result;
1172
+ if (typeof output.url === "string") return output.url;
1173
+ }
1174
+ if (typeof output === "string") {
1175
+ return output;
1176
+ }
1177
+ throw new Error(
1178
+ `Runpod video generation completed but no video URL was found in the output: ${JSON.stringify(output)}`
1179
+ );
1180
+ }
1181
+ async pollForCompletion(jobId, abortSignal, pollOptions, effectiveBaseURL) {
1182
+ const maxAttempts = pollOptions?.maxAttempts ?? 120;
1183
+ const pollInterval = pollOptions?.pollIntervalMillis ?? 5e3;
1184
+ const baseURL = effectiveBaseURL ?? this.config.baseURL;
1185
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
1186
+ if (abortSignal?.aborted) {
1187
+ throw new Error("Video generation was aborted");
1188
+ }
1189
+ const { value: statusResponse } = await (0, import_provider_utils5.getFromApi)({
1190
+ url: `${baseURL}/status/${jobId}`,
1191
+ headers: this.config.headers(),
1192
+ successfulResponseHandler: (0, import_provider_utils5.createJsonResponseHandler)(
1193
+ runpodVideoStatusSchema
1194
+ ),
1195
+ failedResponseHandler: runpodVideoFailedResponseHandler,
1196
+ abortSignal,
1197
+ fetch: this.config.fetch
1198
+ });
1199
+ const typedStatusResponse = statusResponse;
1200
+ if (typedStatusResponse.status === "COMPLETED") {
1201
+ return typedStatusResponse;
1202
+ }
1203
+ if (typedStatusResponse.status === "FAILED") {
1204
+ throw new Error(
1205
+ `Video generation failed: ${typedStatusResponse.error || "Unknown error"}`
1206
+ );
1207
+ }
1208
+ await new Promise((resolve) => setTimeout(resolve, pollInterval));
1209
+ }
1210
+ throw new Error(
1211
+ `Video generation timed out after ${maxAttempts} attempts (${maxAttempts * pollInterval / 1e3}s)`
1212
+ );
1213
+ }
1214
+ };
1215
+
941
1216
  // src/runpod-provider.ts
942
1217
  var MODEL_ID_TO_ENDPOINT_URL = {
943
1218
  "qwen/qwen3-32b-awq": "https://api.runpod.ai/v2/qwen3-32b-awq/openai/v1",
@@ -961,6 +1236,10 @@ var IMAGE_MODEL_ID_TO_ENDPOINT_URL = {
961
1236
  "black-forest-labs/flux-1-dev": "https://api.runpod.ai/v2/black-forest-labs-flux-1-dev",
962
1237
  // Alibaba Wan 2.6 (t2i)
963
1238
  "alibaba/wan-2.6": "https://api.runpod.ai/v2/wan-2-6-t2i",
1239
+ // Tongyi Z-Image Turbo (t2i)
1240
+ "tongyi-mai/z-image-turbo": "https://api.runpod.ai/v2/z-image-turbo",
1241
+ "z-image-turbo": "https://api.runpod.ai/v2/z-image-turbo",
1242
+ // alias, not advertised
964
1243
  // Nano Banana (edit only)
965
1244
  "google/nano-banana-edit": "https://api.runpod.ai/v2/nano-banana-edit",
966
1245
  "nano-banana-edit": "https://api.runpod.ai/v2/nano-banana-edit",
@@ -977,6 +1256,23 @@ var SPEECH_MODEL_ID_TO_ENDPOINT_URL = {
977
1256
  var TRANSCRIPTION_MODEL_ID_TO_ENDPOINT_URL = {
978
1257
  "pruna/whisper-v3-large": "https://api.runpod.ai/v2/whisper-v3-large"
979
1258
  };
1259
+ var VIDEO_MODEL_ID_TO_ENDPOINT_URL = {
1260
+ "pruna/p-video": "https://api.runpod.ai/v2/p-video",
1261
+ "vidu/q3-t2v": "https://api.runpod.ai/v2/vidu-q3-t2v",
1262
+ "vidu/q3-i2v": "https://api.runpod.ai/v2/vidu-q3-i2v",
1263
+ "kwaivgi/kling-v2.6-std-motion-control": "https://api.runpod.ai/v2/kling-v2-6-std-motion-control",
1264
+ "kwaivgi/kling-video-o1-r2v": "https://api.runpod.ai/v2/kling-video-o1-r2v",
1265
+ "kwaivgi/kling-v2.1-i2v-pro": "https://api.runpod.ai/v2/kling-v2-1-i2v-pro",
1266
+ "alibaba/wan-2.6-t2v": "https://api.runpod.ai/v2/wan-2-6-t2v",
1267
+ "alibaba/wan-2.6-i2v": "https://api.runpod.ai/v2/wan-2-6-i2v",
1268
+ "alibaba/wan-2.5": "https://api.runpod.ai/v2/wan-2-5",
1269
+ "alibaba/wan-2.2-t2v-720-lora": "https://api.runpod.ai/v2/wan-2-2-t2v-720-lora",
1270
+ "alibaba/wan-2.2-i2v-720": "https://api.runpod.ai/v2/wan-2-2-i2v-720",
1271
+ "alibaba/wan-2.1-i2v-720": "https://api.runpod.ai/v2/wan-2-1-i2v-720",
1272
+ "bytedance/seedance-v1.5-pro-i2v": "https://api.runpod.ai/v2/seedance-v1-5-pro-i2v",
1273
+ "openai/sora-2-pro-i2v": "https://api.runpod.ai/v2/sora-2-pro-i2v",
1274
+ "openai/sora-2-i2v": "https://api.runpod.ai/v2/sora-2-i2v"
1275
+ };
980
1276
  var MODEL_ID_TO_OPENAI_NAME = {
981
1277
  "qwen/qwen3-32b-awq": "Qwen/Qwen3-32B-AWQ",
982
1278
  "deepcogito/cogito-671b-v2.1-fp8": "deepcogito/cogito-671b-v2.1-FP8",
@@ -1005,7 +1301,7 @@ function parseRunpodConsoleEndpointId(modelIdOrUrl) {
1005
1301
  }
1006
1302
  function createRunpod(options = {}) {
1007
1303
  const getHeaders = () => ({
1008
- Authorization: `Bearer ${(0, import_provider_utils5.loadApiKey)({
1304
+ Authorization: `Bearer ${(0, import_provider_utils6.loadApiKey)({
1009
1305
  apiKey: options.apiKey,
1010
1306
  environmentVariableName: "RUNPOD_API_KEY",
1011
1307
  description: "Runpod"
@@ -1035,7 +1331,7 @@ function createRunpod(options = {}) {
1035
1331
  }
1036
1332
  return {
1037
1333
  provider: `runpod.${modelType}`,
1038
- url: ({ path }) => `${(0, import_provider_utils5.withoutTrailingSlash)(baseURL)}${path}`,
1334
+ url: ({ path }) => `${(0, import_provider_utils6.withoutTrailingSlash)(baseURL)}${path}`,
1039
1335
  headers: getHeaders,
1040
1336
  fetch: runpodFetch
1041
1337
  };
@@ -1092,6 +1388,18 @@ function createRunpod(options = {}) {
1092
1388
  fetch: runpodFetch
1093
1389
  });
1094
1390
  };
1391
+ const createVideoModel = (modelId) => {
1392
+ const endpointIdFromConsole = parseRunpodConsoleEndpointId(modelId);
1393
+ const normalizedModelId = endpointIdFromConsole ?? modelId;
1394
+ const mappedBaseURL = VIDEO_MODEL_ID_TO_ENDPOINT_URL[normalizedModelId];
1395
+ const baseURL = mappedBaseURL ?? (normalizedModelId.startsWith("http") ? normalizedModelId : `https://api.runpod.ai/v2/${normalizedModelId}`);
1396
+ return new RunpodVideoModel(normalizedModelId, {
1397
+ provider: "runpod.video",
1398
+ baseURL,
1399
+ headers: getHeaders,
1400
+ fetch: options.fetch
1401
+ });
1402
+ };
1095
1403
  const provider = (modelId) => createChatModel(modelId);
1096
1404
  provider.completionModel = createCompletionModel;
1097
1405
  provider.languageModel = createChatModel;
@@ -1102,6 +1410,8 @@ function createRunpod(options = {}) {
1102
1410
  provider.speech = createSpeechModel;
1103
1411
  provider.transcriptionModel = createTranscriptionModel;
1104
1412
  provider.transcription = createTranscriptionModel;
1413
+ provider.videoModel = createVideoModel;
1414
+ provider.video = createVideoModel;
1105
1415
  return provider;
1106
1416
  }
1107
1417
  var runpod = createRunpod();