1- const ApiError = require ( ' ./lib/error' ) ;
2- const ModelVersionIdentifier = require ( ' ./lib/identifier' ) ;
3- const { withAutomaticRetries } = require ( ' ./lib/util' ) ;
1+ const ApiError = require ( " ./lib/error" ) ;
2+ const ModelVersionIdentifier = require ( " ./lib/identifier" ) ;
3+ const { withAutomaticRetries } = require ( " ./lib/util" ) ;
44
5- const collections = require ( ' ./lib/collections' ) ;
6- const deployments = require ( ' ./lib/deployments' ) ;
7- const hardware = require ( ' ./lib/hardware' ) ;
8- const models = require ( ' ./lib/models' ) ;
9- const predictions = require ( ' ./lib/predictions' ) ;
10- const trainings = require ( ' ./lib/trainings' ) ;
5+ const collections = require ( " ./lib/collections" ) ;
6+ const deployments = require ( " ./lib/deployments" ) ;
7+ const hardware = require ( " ./lib/hardware" ) ;
8+ const models = require ( " ./lib/models" ) ;
9+ const predictions = require ( " ./lib/predictions" ) ;
10+ const trainings = require ( " ./lib/trainings" ) ;
1111
12- const packageJSON = require ( ' ./package.json' ) ;
12+ const packageJSON = require ( " ./package.json" ) ;
1313
1414/**
1515 * Replicate API client library
@@ -43,7 +43,7 @@ class Replicate {
4343 this . auth = options . auth || process . env . REPLICATE_API_TOKEN ;
4444 this . userAgent =
4545 options . userAgent || `replicate-javascript/${ packageJSON . version } ` ;
46- this . baseUrl = options . baseUrl || ' https://api.replicate.com/v1' ;
46+ this . baseUrl = options . baseUrl || " https://api.replicate.com/v1" ;
4747 this . fetch = options . fetch || globalThis . fetch ;
4848
4949 this . collections = {
@@ -54,7 +54,7 @@ class Replicate {
5454 this . deployments = {
5555 predictions : {
5656 create : deployments . predictions . create . bind ( this ) ,
57- }
57+ } ,
5858 } ;
5959
6060 this . hardware = {
@@ -131,26 +131,30 @@ class Replicate {
131131
132132 const { signal } = options ;
133133
134- prediction = await this . wait ( prediction , wait || { } , async ( updatedPrediction ) => {
135- // Call progress callback with the updated prediction object
136- if ( progress ) {
137- progress ( updatedPrediction ) ;
138- }
139-
140- if ( signal && signal . aborted ) {
141- await this . predictions . cancel ( updatedPrediction . id ) ;
142- return true ; // stop polling
134+ prediction = await this . wait (
135+ prediction ,
136+ wait || { } ,
137+ async ( updatedPrediction ) => {
138+ // Call progress callback with the updated prediction object
139+ if ( progress ) {
140+ progress ( updatedPrediction ) ;
141+ }
142+
143+ if ( signal && signal . aborted ) {
144+ await this . predictions . cancel ( updatedPrediction . id ) ;
145+ return true ; // stop polling
146+ }
147+
148+ return false ; // continue polling
143149 }
144-
145- return false ; // continue polling
146- } ) ;
150+ ) ;
147151
148152 // Call progress callback with the completed prediction object
149153 if ( progress ) {
150154 progress ( prediction ) ;
151155 }
152156
153- if ( prediction . status === ' failed' ) {
157+ if ( prediction . status === " failed" ) {
154158 throw new Error ( `Prediction failed: ${ prediction . error } ` ) ;
155159 }
156160
@@ -177,27 +181,23 @@ class Replicate {
177181 url = route ;
178182 } else {
179183 url = new URL (
180- route . startsWith ( '/' ) ? route . slice ( 1 ) : route ,
181- baseUrl . endsWith ( '/' ) ? baseUrl : `${ baseUrl } /`
184+ route . startsWith ( "/" ) ? route . slice ( 1 ) : route ,
185+ baseUrl . endsWith ( "/" ) ? baseUrl : `${ baseUrl } /`
182186 ) ;
183187 }
184188
185- const {
186- method = 'GET' ,
187- params = { } ,
188- data,
189- } = options ;
189+ const { method = "GET" , params = { } , data } = options ;
190190
191191 for ( const [ key , value ] of Object . entries ( params ) ) {
192192 url . searchParams . append ( key , value ) ;
193193 }
194194
195195 const headers = new Headers ( ) ;
196196 if ( auth ) {
197- headers . append ( ' Authorization' , `Token ${ auth } ` ) ;
197+ headers . append ( " Authorization" , `Token ${ auth } ` ) ;
198198 }
199- headers . append ( ' Content-Type' , ' application/json' ) ;
200- headers . append ( ' User-Agent' , userAgent ) ;
199+ headers . append ( " Content-Type" , " application/json" ) ;
200+ headers . append ( " User-Agent" , userAgent ) ;
201201 if ( options . headers ) {
202202 for ( const [ key , value ] of options . headers . entries ( ) ) {
203203 headers . append ( key , value ) ;
@@ -210,22 +210,25 @@ class Replicate {
210210 body : data ? JSON . stringify ( data ) : undefined ,
211211 } ;
212212
213- const shouldRetry = method === 'GET' ?
214- ( response ) => ( response . status === 429 || response . status >= 500 ) :
215- ( response ) => ( response . status === 429 ) ;
213+ const shouldRetry =
214+ method === "GET"
215+ ? ( response ) => response . status === 429 || response . status >= 500
216+ : ( response ) => response . status === 429 ;
216217
217218 // Workaround to fix `TypeError: Illegal invocation` error in Cloudflare Workers
218219 // https://github.com/replicate/replicate-javascript/issues/134
219220 const _fetch = this . fetch ; // eslint-disable-line no-underscore-dangle
220- const response = await withAutomaticRetries ( async ( ) => _fetch ( url , init ) , { shouldRetry } ) ;
221+ const response = await withAutomaticRetries ( async ( ) => _fetch ( url , init ) , {
222+ shouldRetry,
223+ } ) ;
221224
222225 if ( ! response . ok ) {
223226 const request = new Request ( url , init ) ;
224227 const responseText = await response . text ( ) ;
225228 throw new ApiError (
226229 `Request to ${ url } failed with status ${ response . status } ${ response . statusText } : ${ responseText } .` ,
227230 request ,
228- response ,
231+ response
229232 ) ;
230233 }
231234
@@ -243,11 +246,12 @@ class Replicate {
243246 * @param {Function } endpoint - Function that returns a promise for the next page of results
244247 * @yields {object[]} Each page of results
245248 */
246- async * paginate ( endpoint ) {
249+ async * paginate ( endpoint ) {
247250 const response = await endpoint ( ) ;
248251 yield response . results ;
249252 if ( response . next ) {
250- const nextPage = ( ) => this . request ( response . next , { method : 'GET' } ) . then ( ( r ) => r . json ( ) ) ;
253+ const nextPage = ( ) =>
254+ this . request ( response . next , { method : "GET" } ) . then ( ( r ) => r . json ( ) ) ;
251255 yield * this . paginate ( nextPage ) ;
252256 }
253257 }
@@ -271,13 +275,13 @@ class Replicate {
271275 async wait ( prediction , options , stop ) {
272276 const { id } = prediction ;
273277 if ( ! id ) {
274- throw new Error ( ' Invalid prediction' ) ;
278+ throw new Error ( " Invalid prediction" ) ;
275279 }
276280
277281 if (
278- prediction . status === ' succeeded' ||
279- prediction . status === ' failed' ||
280- prediction . status === ' canceled'
282+ prediction . status === " succeeded" ||
283+ prediction . status === " failed" ||
284+ prediction . status === " canceled"
281285 ) {
282286 return prediction ;
283287 }
@@ -290,12 +294,12 @@ class Replicate {
290294 let updatedPrediction = await this . predictions . get ( id ) ;
291295
292296 while (
293- updatedPrediction . status !== ' succeeded' &&
294- updatedPrediction . status !== ' failed' &&
295- updatedPrediction . status !== ' canceled'
297+ updatedPrediction . status !== " succeeded" &&
298+ updatedPrediction . status !== " failed" &&
299+ updatedPrediction . status !== " canceled"
296300 ) {
297301 /* eslint-disable no-await-in-loop */
298- if ( stop && await stop ( updatedPrediction ) === true ) {
302+ if ( stop && ( await stop ( updatedPrediction ) ) === true ) {
299303 break ;
300304 }
301305
@@ -304,7 +308,7 @@ class Replicate {
304308 /* eslint-enable no-await-in-loop */
305309 }
306310
307- if ( updatedPrediction . status === ' failed' ) {
311+ if ( updatedPrediction . status === " failed" ) {
308312 throw new Error ( `Prediction failed: ${ updatedPrediction . error } ` ) ;
309313 }
310314
0 commit comments