@ai-sdk/replicate 3.0.0-beta.5 → 3.0.0-beta.51

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
@@ -1,47 +1,36 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- VERSION: () => VERSION,
24
- createReplicate: () => createReplicate,
25
- replicate: () => replicate
26
- });
27
- module.exports = __toCommonJS(index_exports);
28
-
29
1
  // src/replicate-provider.ts
30
- var import_provider2 = require("@ai-sdk/provider");
31
- var import_provider_utils4 = require("@ai-sdk/provider-utils");
2
+ import {
3
+ NoSuchModelError
4
+ } from "@ai-sdk/provider";
5
+ import {
6
+ loadApiKey,
7
+ withUserAgentSuffix
8
+ } from "@ai-sdk/provider-utils";
32
9
 
33
10
  // src/replicate-image-model.ts
34
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
35
- var import_v42 = require("zod/v4");
11
+ import {
12
+ combineHeaders,
13
+ convertImageModelFileToDataUri,
14
+ createBinaryResponseHandler,
15
+ createJsonResponseHandler,
16
+ getFromApi,
17
+ parseProviderOptions,
18
+ postJsonToApi,
19
+ resolve,
20
+ serializeModelOptions,
21
+ WORKFLOW_SERIALIZE,
22
+ WORKFLOW_DESERIALIZE
23
+ } from "@ai-sdk/provider-utils";
24
+ import { z as z3 } from "zod/v4";
36
25
 
37
26
  // src/replicate-error.ts
38
- var import_provider_utils = require("@ai-sdk/provider-utils");
39
- var import_v4 = require("zod/v4");
40
- var replicateErrorSchema = import_v4.z.object({
41
- detail: import_v4.z.string().optional(),
42
- error: import_v4.z.string().optional()
27
+ import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
28
+ import { z } from "zod/v4";
29
+ var replicateErrorSchema = z.object({
30
+ detail: z.string().optional(),
31
+ error: z.string().optional()
43
32
  });
44
- var replicateFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
33
+ var replicateFailedResponseHandler = createJsonErrorResponseHandler({
45
34
  errorSchema: replicateErrorSchema,
46
35
  errorToMessage: (error) => {
47
36
  var _a, _b;
@@ -49,10 +38,57 @@ var replicateFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
49
38
  }
50
39
  });
51
40
 
41
+ // src/replicate-image-model-options.ts
42
+ import {
43
+ lazySchema,
44
+ zodSchema
45
+ } from "@ai-sdk/provider-utils";
46
+ import { z as z2 } from "zod/v4";
47
+ var replicateImageModelOptionsSchema = lazySchema(
48
+ () => zodSchema(
49
+ z2.object({
50
+ /**
51
+ * Maximum time in seconds to wait for the prediction to complete in sync mode.
52
+ * By default, Replicate uses sync mode with a 60-second timeout.
53
+ *
54
+ * - When not specified: Uses default 60-second sync wait (`prefer: wait`)
55
+ * - When set to a positive number: Uses that duration (`prefer: wait=N`)
56
+ */
57
+ maxWaitTimeInSeconds: z2.number().positive().nullish(),
58
+ /**
59
+ * Guidance scale for classifier-free guidance.
60
+ * Higher values make the output more closely match the prompt.
61
+ */
62
+ guidance_scale: z2.number().nullish(),
63
+ /**
64
+ * Number of denoising steps. More steps = higher quality but slower.
65
+ */
66
+ num_inference_steps: z2.number().nullish(),
67
+ /**
68
+ * Negative prompt to guide what to avoid in the generation.
69
+ */
70
+ negative_prompt: z2.string().nullish(),
71
+ /**
72
+ * Output image format.
73
+ */
74
+ output_format: z2.enum(["png", "jpg", "webp"]).nullish(),
75
+ /**
76
+ * Output image quality (1-100). Only applies to jpg and webp.
77
+ */
78
+ output_quality: z2.number().min(1).max(100).nullish(),
79
+ /**
80
+ * Strength of the transformation for img2img (0-1).
81
+ * Lower values keep more of the original image.
82
+ */
83
+ strength: z2.number().min(0).max(1).nullish()
84
+ }).passthrough()
85
+ )
86
+ );
87
+
52
88
  // src/replicate-image-model.ts
53
89
  var FLUX_2_MODEL_PATTERN = /^black-forest-labs\/flux-2-/;
54
90
  var MAX_FLUX_2_INPUT_IMAGES = 8;
55
- var ReplicateImageModel = class {
91
+ var ReplicateImageModel = class _ReplicateImageModel {
56
92
  constructor(modelId, config) {
57
93
  this.modelId = modelId;
58
94
  this.config = config;
@@ -67,6 +103,15 @@ var ReplicateImageModel = class {
67
103
  get isFlux2Model() {
68
104
  return FLUX_2_MODEL_PATTERN.test(this.modelId);
69
105
  }
106
+ static [WORKFLOW_SERIALIZE](model) {
107
+ return serializeModelOptions({
108
+ modelId: model.modelId,
109
+ config: model.config
110
+ });
111
+ }
112
+ static [WORKFLOW_DESERIALIZE](options) {
113
+ return new _ReplicateImageModel(options.modelId, options.config);
114
+ }
70
115
  async doGenerate({
71
116
  prompt,
72
117
  n,
@@ -83,7 +128,7 @@ var ReplicateImageModel = class {
83
128
  const warnings = [];
84
129
  const [modelId, version] = this.modelId.split(":");
85
130
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
86
- const replicateOptions = await (0, import_provider_utils2.parseProviderOptions)({
131
+ const replicateOptions = await parseProviderOptions({
87
132
  provider: "replicate",
88
133
  providerOptions,
89
134
  schema: replicateImageModelOptionsSchema
@@ -93,7 +138,7 @@ var ReplicateImageModel = class {
93
138
  if (this.isFlux2Model) {
94
139
  for (let i = 0; i < Math.min(files.length, MAX_FLUX_2_INPUT_IMAGES); i++) {
95
140
  const key = i === 0 ? "input_image" : `input_image_${i + 1}`;
96
- imageInputs[key] = (0, import_provider_utils2.convertImageModelFileToDataUri)(files[i]);
141
+ imageInputs[key] = convertImageModelFileToDataUri(files[i]);
97
142
  }
98
143
  if (files.length > MAX_FLUX_2_INPUT_IMAGES) {
99
144
  warnings.push({
@@ -102,7 +147,7 @@ var ReplicateImageModel = class {
102
147
  });
103
148
  }
104
149
  } else {
105
- imageInputs = { image: (0, import_provider_utils2.convertImageModelFileToDataUri)(files[0]) };
150
+ imageInputs = { image: convertImageModelFileToDataUri(files[0]) };
106
151
  if (files.length > 1) {
107
152
  warnings.push({
108
153
  type: "other",
@@ -119,7 +164,7 @@ var ReplicateImageModel = class {
119
164
  message: "Flux-2 models do not support mask input. The mask will be ignored."
120
165
  });
121
166
  } else {
122
- maskInput = (0, import_provider_utils2.convertImageModelFileToDataUri)(mask);
167
+ maskInput = convertImageModelFileToDataUri(mask);
123
168
  }
124
169
  }
125
170
  const { maxWaitTimeInSeconds, ...inputOptions } = replicateOptions != null ? replicateOptions : {};
@@ -127,13 +172,13 @@ var ReplicateImageModel = class {
127
172
  const {
128
173
  value: { output },
129
174
  responseHeaders
130
- } = await (0, import_provider_utils2.postJsonToApi)({
175
+ } = await postJsonToApi({
131
176
  url: (
132
177
  // different endpoints for versioned vs unversioned models:
133
178
  version != null ? `${this.config.baseURL}/predictions` : `${this.config.baseURL}/models/${modelId}/predictions`
134
179
  ),
135
- headers: (0, import_provider_utils2.combineHeaders)(
136
- await (0, import_provider_utils2.resolve)(this.config.headers),
180
+ headers: combineHeaders(
181
+ this.config.headers ? await resolve(this.config.headers) : void 0,
137
182
  headers,
138
183
  preferHeader
139
184
  ),
@@ -151,7 +196,7 @@ var ReplicateImageModel = class {
151
196
  // for versioned models, include the version in the body:
152
197
  ...version != null ? { version } : {}
153
198
  },
154
- successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
199
+ successfulResponseHandler: createJsonResponseHandler(
155
200
  replicateImageResponseSchema
156
201
  ),
157
202
  failedResponseHandler: replicateFailedResponseHandler,
@@ -161,9 +206,9 @@ var ReplicateImageModel = class {
161
206
  const outputArray = Array.isArray(output) ? output : [output];
162
207
  const images = await Promise.all(
163
208
  outputArray.map(async (url) => {
164
- const { value: image } = await (0, import_provider_utils2.getFromApi)({
209
+ const { value: image } = await getFromApi({
165
210
  url,
166
- successfulResponseHandler: (0, import_provider_utils2.createBinaryResponseHandler)(),
211
+ successfulResponseHandler: createBinaryResponseHandler(),
167
212
  failedResponseHandler: replicateFailedResponseHandler,
168
213
  abortSignal,
169
214
  fetch: this.config.fetch
@@ -182,54 +227,50 @@ var ReplicateImageModel = class {
182
227
  };
183
228
  }
184
229
  };
185
- var replicateImageResponseSchema = import_v42.z.object({
186
- output: import_v42.z.union([import_v42.z.array(import_v42.z.string()), import_v42.z.string()])
230
+ var replicateImageResponseSchema = z3.object({
231
+ output: z3.union([z3.array(z3.string()), z3.string()])
187
232
  });
188
- var replicateImageModelOptionsSchema = (0, import_provider_utils2.lazySchema)(
189
- () => (0, import_provider_utils2.zodSchema)(
190
- import_v42.z.object({
191
- /**
192
- * Maximum time in seconds to wait for the prediction to complete in sync mode.
193
- * By default, Replicate uses sync mode with a 60-second timeout.
194
- *
195
- * - When not specified: Uses default 60-second sync wait (`prefer: wait`)
196
- * - When set to a positive number: Uses that duration (`prefer: wait=N`)
197
- */
198
- maxWaitTimeInSeconds: import_v42.z.number().positive().nullish(),
199
- /**
200
- * Guidance scale for classifier-free guidance.
201
- * Higher values make the output more closely match the prompt.
202
- */
203
- guidance_scale: import_v42.z.number().nullish(),
204
- /**
205
- * Number of denoising steps. More steps = higher quality but slower.
206
- */
207
- num_inference_steps: import_v42.z.number().nullish(),
208
- /**
209
- * Negative prompt to guide what to avoid in the generation.
210
- */
211
- negative_prompt: import_v42.z.string().nullish(),
212
- /**
213
- * Output image format.
214
- */
215
- output_format: import_v42.z.enum(["png", "jpg", "webp"]).nullish(),
216
- /**
217
- * Output image quality (1-100). Only applies to jpg and webp.
218
- */
219
- output_quality: import_v42.z.number().min(1).max(100).nullish(),
220
- /**
221
- * Strength of the transformation for img2img (0-1).
222
- * Lower values keep more of the original image.
223
- */
224
- strength: import_v42.z.number().min(0).max(1).nullish()
225
- }).passthrough()
233
+
234
+ // src/replicate-video-model.ts
235
+ import {
236
+ AISDKError
237
+ } from "@ai-sdk/provider";
238
+ import {
239
+ combineHeaders as combineHeaders2,
240
+ convertImageModelFileToDataUri as convertImageModelFileToDataUri2,
241
+ createJsonResponseHandler as createJsonResponseHandler2,
242
+ delay,
243
+ getFromApi as getFromApi2,
244
+ isSameOrigin,
245
+ parseProviderOptions as parseProviderOptions2,
246
+ postJsonToApi as postJsonToApi2,
247
+ resolve as resolve2
248
+ } from "@ai-sdk/provider-utils";
249
+ import { z as z5 } from "zod/v4";
250
+
251
+ // src/replicate-video-model-options.ts
252
+ import { lazySchema as lazySchema2, zodSchema as zodSchema2 } from "@ai-sdk/provider-utils";
253
+ import { z as z4 } from "zod/v4";
254
+ var replicateVideoModelOptionsSchema = lazySchema2(
255
+ () => zodSchema2(
256
+ z4.object({
257
+ pollIntervalMs: z4.number().positive().nullish(),
258
+ pollTimeoutMs: z4.number().positive().nullish(),
259
+ maxWaitTimeInSeconds: z4.number().positive().nullish(),
260
+ guidance_scale: z4.number().nullish(),
261
+ num_inference_steps: z4.number().nullish(),
262
+ motion_bucket_id: z4.number().nullish(),
263
+ cond_aug: z4.number().nullish(),
264
+ decoding_t: z4.number().nullish(),
265
+ video_length: z4.string().nullish(),
266
+ sizing_strategy: z4.string().nullish(),
267
+ frames_per_second: z4.number().nullish(),
268
+ prompt_optimizer: z4.boolean().nullish()
269
+ }).loose()
226
270
  )
227
271
  );
228
272
 
229
273
  // src/replicate-video-model.ts
230
- var import_provider = require("@ai-sdk/provider");
231
- var import_provider_utils3 = require("@ai-sdk/provider-utils");
232
- var import_v43 = require("zod/v4");
233
274
  var ReplicateVideoModel = class {
234
275
  constructor(modelId, config) {
235
276
  this.modelId = modelId;
@@ -245,7 +286,7 @@ var ReplicateVideoModel = class {
245
286
  var _a, _b, _c, _d, _e, _f, _g;
246
287
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
247
288
  const warnings = [];
248
- const replicateOptions = await (0, import_provider_utils3.parseProviderOptions)({
289
+ const replicateOptions = await parseProviderOptions2({
249
290
  provider: "replicate",
250
291
  providerOptions: options.providerOptions,
251
292
  schema: replicateVideoModelOptionsSchema
@@ -259,7 +300,7 @@ var ReplicateVideoModel = class {
259
300
  if (options.image.type === "url") {
260
301
  input.image = options.image.url;
261
302
  } else {
262
- input.image = (0, import_provider_utils3.convertImageModelFileToDataUri)(options.image);
303
+ input.image = convertImageModelFileToDataUri2(options.image);
263
304
  }
264
305
  }
265
306
  if (options.aspectRatio) {
@@ -328,10 +369,10 @@ var ReplicateVideoModel = class {
328
369
  const maxWaitTimeInSeconds = replicateOptions == null ? void 0 : replicateOptions.maxWaitTimeInSeconds;
329
370
  const preferHeader = maxWaitTimeInSeconds != null ? { prefer: `wait=${maxWaitTimeInSeconds}` } : { prefer: "wait" };
330
371
  const predictionUrl = version != null ? `${this.config.baseURL}/predictions` : `${this.config.baseURL}/models/${modelId}/predictions`;
331
- const { value: prediction, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({
372
+ const { value: prediction, responseHeaders } = await postJsonToApi2({
332
373
  url: predictionUrl,
333
- headers: (0, import_provider_utils3.combineHeaders)(
334
- await (0, import_provider_utils3.resolve)(this.config.headers),
374
+ headers: combineHeaders2(
375
+ await resolve2(this.config.headers),
335
376
  options.headers,
336
377
  preferHeader
337
378
  ),
@@ -339,7 +380,7 @@ var ReplicateVideoModel = class {
339
380
  input,
340
381
  ...version != null ? { version } : {}
341
382
  },
342
- successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
383
+ successfulResponseHandler: createJsonResponseHandler2(
343
384
  replicatePredictionSchema
344
385
  ),
345
386
  failedResponseHandler: replicateFailedResponseHandler,
@@ -353,22 +394,25 @@ var ReplicateVideoModel = class {
353
394
  const startTime = Date.now();
354
395
  while (finalPrediction.status === "starting" || finalPrediction.status === "processing") {
355
396
  if (Date.now() - startTime > pollTimeoutMs) {
356
- throw new import_provider.AISDKError({
397
+ throw new AISDKError({
357
398
  name: "REPLICATE_VIDEO_GENERATION_TIMEOUT",
358
399
  message: `Video generation timed out after ${pollTimeoutMs}ms`
359
400
  });
360
401
  }
361
- await (0, import_provider_utils3.delay)(pollIntervalMs);
402
+ await delay(pollIntervalMs);
362
403
  if ((_f = options.abortSignal) == null ? void 0 : _f.aborted) {
363
- throw new import_provider.AISDKError({
404
+ throw new AISDKError({
364
405
  name: "REPLICATE_VIDEO_GENERATION_ABORTED",
365
406
  message: "Video generation request was aborted"
366
407
  });
367
408
  }
368
- const { value: statusPrediction } = await (0, import_provider_utils3.getFromApi)({
369
- url: finalPrediction.urls.get,
370
- headers: await (0, import_provider_utils3.resolve)(this.config.headers),
371
- successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
409
+ const pollUrl = finalPrediction.urls.get;
410
+ const { value: statusPrediction } = await getFromApi2({
411
+ url: pollUrl,
412
+ // The polling URL comes from the provider response; only send
413
+ // credentials when it stays on the provider's own origin.
414
+ headers: isSameOrigin(pollUrl, this.config.baseURL) ? await resolve2(this.config.headers) : void 0,
415
+ successfulResponseHandler: createJsonResponseHandler2(
372
416
  replicatePredictionSchema
373
417
  ),
374
418
  failedResponseHandler: replicateFailedResponseHandler,
@@ -379,20 +423,20 @@ var ReplicateVideoModel = class {
379
423
  }
380
424
  }
381
425
  if (finalPrediction.status === "failed") {
382
- throw new import_provider.AISDKError({
426
+ throw new AISDKError({
383
427
  name: "REPLICATE_VIDEO_GENERATION_FAILED",
384
428
  message: `Video generation failed: ${(_g = finalPrediction.error) != null ? _g : "Unknown error"}`
385
429
  });
386
430
  }
387
431
  if (finalPrediction.status === "canceled") {
388
- throw new import_provider.AISDKError({
432
+ throw new AISDKError({
389
433
  name: "REPLICATE_VIDEO_GENERATION_CANCELED",
390
434
  message: "Video generation was canceled"
391
435
  });
392
436
  }
393
437
  const videoUrl = finalPrediction.output;
394
438
  if (!videoUrl) {
395
- throw new import_provider.AISDKError({
439
+ throw new AISDKError({
396
440
  name: "REPLICATE_VIDEO_GENERATION_ERROR",
397
441
  message: "No video URL in response"
398
442
  });
@@ -425,45 +469,27 @@ var ReplicateVideoModel = class {
425
469
  };
426
470
  }
427
471
  };
428
- var replicatePredictionSchema = import_v43.z.object({
429
- id: import_v43.z.string(),
430
- status: import_v43.z.enum(["starting", "processing", "succeeded", "failed", "canceled"]),
431
- output: import_v43.z.string().nullish(),
432
- error: import_v43.z.string().nullish(),
433
- urls: import_v43.z.object({
434
- get: import_v43.z.string()
472
+ var replicatePredictionSchema = z5.object({
473
+ id: z5.string(),
474
+ status: z5.enum(["starting", "processing", "succeeded", "failed", "canceled"]),
475
+ output: z5.string().nullish(),
476
+ error: z5.string().nullish(),
477
+ urls: z5.object({
478
+ get: z5.string()
435
479
  }),
436
- metrics: import_v43.z.object({
437
- predict_time: import_v43.z.number().nullish()
480
+ metrics: z5.object({
481
+ predict_time: z5.number().nullish()
438
482
  }).nullish()
439
483
  });
440
- var replicateVideoModelOptionsSchema = (0, import_provider_utils3.lazySchema)(
441
- () => (0, import_provider_utils3.zodSchema)(
442
- import_v43.z.object({
443
- pollIntervalMs: import_v43.z.number().positive().nullish(),
444
- pollTimeoutMs: import_v43.z.number().positive().nullish(),
445
- maxWaitTimeInSeconds: import_v43.z.number().positive().nullish(),
446
- guidance_scale: import_v43.z.number().nullish(),
447
- num_inference_steps: import_v43.z.number().nullish(),
448
- motion_bucket_id: import_v43.z.number().nullish(),
449
- cond_aug: import_v43.z.number().nullish(),
450
- decoding_t: import_v43.z.number().nullish(),
451
- video_length: import_v43.z.string().nullish(),
452
- sizing_strategy: import_v43.z.string().nullish(),
453
- frames_per_second: import_v43.z.number().nullish(),
454
- prompt_optimizer: import_v43.z.boolean().nullish()
455
- }).loose()
456
- )
457
- );
458
484
 
459
485
  // src/version.ts
460
- var VERSION = true ? "3.0.0-beta.5" : "0.0.0-test";
486
+ var VERSION = true ? "3.0.0-beta.51" : "0.0.0-test";
461
487
 
462
488
  // src/replicate-provider.ts
463
489
  function createReplicate(options = {}) {
464
- const getHeaders = () => (0, import_provider_utils4.withUserAgentSuffix)(
490
+ const getHeaders = () => withUserAgentSuffix(
465
491
  {
466
- Authorization: `Bearer ${(0, import_provider_utils4.loadApiKey)({
492
+ Authorization: `Bearer ${loadApiKey({
467
493
  apiKey: options.apiToken,
468
494
  environmentVariableName: "REPLICATE_API_TOKEN",
469
495
  description: "Replicate"
@@ -491,7 +517,7 @@ function createReplicate(options = {}) {
491
517
  });
492
518
  };
493
519
  const embeddingModel = (modelId) => {
494
- throw new import_provider2.NoSuchModelError({
520
+ throw new NoSuchModelError({
495
521
  modelId,
496
522
  modelType: "embeddingModel"
497
523
  });
@@ -501,7 +527,7 @@ function createReplicate(options = {}) {
501
527
  image: createImageModel,
502
528
  imageModel: createImageModel,
503
529
  languageModel: (modelId) => {
504
- throw new import_provider2.NoSuchModelError({
530
+ throw new NoSuchModelError({
505
531
  modelId,
506
532
  modelType: "languageModel"
507
533
  });
@@ -513,10 +539,9 @@ function createReplicate(options = {}) {
513
539
  };
514
540
  }
515
541
  var replicate = createReplicate();
516
- // Annotate the CommonJS export names for ESM import in node:
517
- 0 && (module.exports = {
542
+ export {
518
543
  VERSION,
519
544
  createReplicate,
520
545
  replicate
521
- });
546
+ };
522
547
  //# sourceMappingURL=index.js.map