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" ) ;
4
4
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" ) ;
11
11
12
- const packageJSON = require ( ' ./package.json' ) ;
12
+ const packageJSON = require ( " ./package.json" ) ;
13
13
14
14
/**
15
15
* Replicate API client library
@@ -43,7 +43,7 @@ class Replicate {
43
43
this . auth = options . auth || process . env . REPLICATE_API_TOKEN ;
44
44
this . userAgent =
45
45
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" ;
47
47
this . fetch = options . fetch || globalThis . fetch ;
48
48
49
49
this . collections = {
@@ -54,7 +54,7 @@ class Replicate {
54
54
this . deployments = {
55
55
predictions : {
56
56
create : deployments . predictions . create . bind ( this ) ,
57
- }
57
+ } ,
58
58
} ;
59
59
60
60
this . hardware = {
@@ -131,26 +131,30 @@ class Replicate {
131
131
132
132
const { signal } = options ;
133
133
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
143
149
}
144
-
145
- return false ; // continue polling
146
- } ) ;
150
+ ) ;
147
151
148
152
// Call progress callback with the completed prediction object
149
153
if ( progress ) {
150
154
progress ( prediction ) ;
151
155
}
152
156
153
- if ( prediction . status === ' failed' ) {
157
+ if ( prediction . status === " failed" ) {
154
158
throw new Error ( `Prediction failed: ${ prediction . error } ` ) ;
155
159
}
156
160
@@ -177,27 +181,23 @@ class Replicate {
177
181
url = route ;
178
182
} else {
179
183
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 } /`
182
186
) ;
183
187
}
184
188
185
- const {
186
- method = 'GET' ,
187
- params = { } ,
188
- data,
189
- } = options ;
189
+ const { method = "GET" , params = { } , data } = options ;
190
190
191
191
for ( const [ key , value ] of Object . entries ( params ) ) {
192
192
url . searchParams . append ( key , value ) ;
193
193
}
194
194
195
195
const headers = new Headers ( ) ;
196
196
if ( auth ) {
197
- headers . append ( ' Authorization' , `Token ${ auth } ` ) ;
197
+ headers . append ( " Authorization" , `Token ${ auth } ` ) ;
198
198
}
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 ) ;
201
201
if ( options . headers ) {
202
202
for ( const [ key , value ] of options . headers . entries ( ) ) {
203
203
headers . append ( key , value ) ;
@@ -210,22 +210,25 @@ class Replicate {
210
210
body : data ? JSON . stringify ( data ) : undefined ,
211
211
} ;
212
212
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 ;
216
217
217
218
// Workaround to fix `TypeError: Illegal invocation` error in Cloudflare Workers
218
219
// https://github.com/replicate/replicate-javascript/issues/134
219
220
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
+ } ) ;
221
224
222
225
if ( ! response . ok ) {
223
226
const request = new Request ( url , init ) ;
224
227
const responseText = await response . text ( ) ;
225
228
throw new ApiError (
226
229
`Request to ${ url } failed with status ${ response . status } ${ response . statusText } : ${ responseText } .` ,
227
230
request ,
228
- response ,
231
+ response
229
232
) ;
230
233
}
231
234
@@ -243,11 +246,12 @@ class Replicate {
243
246
* @param {Function } endpoint - Function that returns a promise for the next page of results
244
247
* @yields {object[]} Each page of results
245
248
*/
246
- async * paginate ( endpoint ) {
249
+ async * paginate ( endpoint ) {
247
250
const response = await endpoint ( ) ;
248
251
yield response . results ;
249
252
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 ( ) ) ;
251
255
yield * this . paginate ( nextPage ) ;
252
256
}
253
257
}
@@ -271,13 +275,13 @@ class Replicate {
271
275
async wait ( prediction , options , stop ) {
272
276
const { id } = prediction ;
273
277
if ( ! id ) {
274
- throw new Error ( ' Invalid prediction' ) ;
278
+ throw new Error ( " Invalid prediction" ) ;
275
279
}
276
280
277
281
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"
281
285
) {
282
286
return prediction ;
283
287
}
@@ -290,12 +294,12 @@ class Replicate {
290
294
let updatedPrediction = await this . predictions . get ( id ) ;
291
295
292
296
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"
296
300
) {
297
301
/* eslint-disable no-await-in-loop */
298
- if ( stop && await stop ( updatedPrediction ) === true ) {
302
+ if ( stop && ( await stop ( updatedPrediction ) ) === true ) {
299
303
break ;
300
304
}
301
305
@@ -304,7 +308,7 @@ class Replicate {
304
308
/* eslint-enable no-await-in-loop */
305
309
}
306
310
307
- if ( updatedPrediction . status === ' failed' ) {
311
+ if ( updatedPrediction . status === " failed" ) {
308
312
throw new Error ( `Prediction failed: ${ updatedPrediction . error } ` ) ;
309
313
}
310
314
0 commit comments