Skip to content

Commit edeed24

Browse files
authored
Change polling behavior (#114)
* Remove max_attempts parameter from wait method * Set default polling interval to 500ms
1 parent 64c2282 commit edeed24

File tree

2 files changed

+4
-16
lines changed

2 files changed

+4
-16
lines changed

index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ declare module 'replicate' {
8585
identifier: `${string}/${string}:${string}`,
8686
options: {
8787
input: object;
88-
wait?: { interval?: number; max_attempts?: number };
88+
wait?: { interval?: number };
8989
webhook?: string;
9090
webhook_events_filter?: WebhookEventType[];
9191
signal?: AbortSignal;
@@ -106,7 +106,6 @@ declare module 'replicate' {
106106
prediction: Prediction,
107107
options: {
108108
interval?: number;
109-
max_attempts?: number;
110109
},
111110
stop?: (prediction: Prediction) => Promise<boolean>
112111
): Promise<Prediction>;

index.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ class Replicate {
8181
* @param {object} options
8282
* @param {object} options.input - Required. An object with the model inputs
8383
* @param {object} [options.wait] - Options for waiting for the prediction to finish
84-
* @param {number} [options.wait.interval] - Polling interval in milliseconds. Defaults to 250
85-
* @param {number} [options.wait.max_attempts] - Maximum number of polling attempts. Defaults to no limit
84+
* @param {number} [options.wait.interval] - Polling interval in milliseconds. Defaults to 500
8685
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
8786
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
8887
* @param {AbortSignal} [options.signal] - AbortSignal to cancel the prediction
@@ -247,8 +246,7 @@ class Replicate {
247246
* @async
248247
* @param {object} prediction - Prediction object
249248
* @param {object} options - Options
250-
* @param {number} [options.interval] - Polling interval in milliseconds. Defaults to 250
251-
* @param {number} [options.max_attempts] - Maximum number of polling attempts. Defaults to no limit
249+
* @param {number} [options.interval] - Polling interval in milliseconds. Defaults to 500
252250
* @param {Function} [stop] - Async callback function that is called after each polling attempt. Receives the prediction object as an argument. Return false to cancel polling.
253251
* @throws {Error} If the prediction doesn't complete within the maximum number of attempts
254252
* @throws {Error} If the prediction failed
@@ -271,9 +269,7 @@ class Replicate {
271269
// eslint-disable-next-line no-promise-executor-return
272270
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
273271

274-
let attempts = 0;
275-
const interval = options.interval || 250;
276-
const max_attempts = options.max_attempts || null;
272+
const interval = options.interval || 500;
277273

278274
let updatedPrediction = await this.predictions.get(id);
279275

@@ -287,13 +283,6 @@ class Replicate {
287283
break;
288284
}
289285

290-
attempts += 1;
291-
if (max_attempts && attempts > max_attempts) {
292-
throw new Error(
293-
`Prediction ${id} did not finish after ${max_attempts} attempts`
294-
);
295-
}
296-
297286
await sleep(interval);
298287
updatedPrediction = await this.predictions.get(prediction.id);
299288
/* eslint-enable no-await-in-loop */

0 commit comments

Comments
 (0)