@@ -27,19 +27,20 @@ export default class UploadPlugin extends AdminForthPlugin {
2727 this . totalCalls = 0 ;
2828 this . totalDuration = 0 ;
2929 if ( this . options . generation ?. rateLimit ?. limit ) {
30- this . rateLimiter = new RateLimiter ( this . options . generation . rateLimit ?. limit )
30+ this . rateLimiter = new RateLimiter ( this . options . generation . rateLimit ?. limit )
3131 }
3232 }
3333
3434 private async generateImages ( jobId : string , prompt : string , recordId : any , adminUser : any , headers : any ) {
3535 if ( this . options . generation . rateLimit ?. limit ) {
3636 // rate limit
37- const { error } = RateLimiter . checkRateLimit (
38- this . pluginInstanceId ,
39- this . options . generation . rateLimit ?. limit ,
40- this . adminforth . auth . getClientIp ( headers ) ,
41- ) ;
42- if ( error ) {
37+ // const { error } = RateLimiter.checkRateLimit(
38+ // this.pluginInstanceId,
39+ // this.options.generation.rateLimit?.limit,
40+ // this.adminforth.auth.getClientIp(headers),
41+ // );
42+ if ( ! await this . rateLimiter . consume ( `${ this . pluginInstanceId } -${ this . adminforth . auth . getClientIp ( headers ) } ` ) ) {
43+ jobs . set ( jobId , { status : "failed" , error : this . options . generation . rateLimit . errorMessage } ) ;
4344 return { error : this . options . generation . rateLimit . errorMessage } ;
4445 }
4546 }
@@ -426,62 +427,6 @@ export default class UploadPlugin extends AdminForthPlugin {
426427 path : `/plugin/${ this . pluginInstanceId } /create-image-generation-job` ,
427428 handler : async ( { body, adminUser, headers } ) => {
428429 const { prompt, recordId } = body ;
429- if ( this . rateLimiter ) {
430- // rate limit
431- // const { error } = RateLimiter.checkRateLimit(
432- // this.pluginInstanceId,
433- // this.options.generation.rateLimit?.limit,
434- // this.adminforth.auth.getClientIp(headers),
435- // );
436- if ( ! await this . rateLimiter . consume ( `${ this . pluginInstanceId } -${ this . adminforth . auth . getClientIp ( headers ) } ` ) ) {
437- return { error : this . options . generation . rateLimit . errorMessage } ;
438- }
439- }
440- let attachmentFiles = [ ] ;
441- if ( this . options . generation . attachFiles ) {
442- // TODO - does it require additional allowed action to check this record id has access to get the image?
443- // or should we mention in docs that user should do validation in method itself
444- const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get (
445- [ Filters . EQ ( this . resourceConfig . columns . find ( ( column : any ) => column . primaryKey ) ?. name , recordId ) ]
446- ) ;
447-
448- if ( ! record ) {
449- return { error : `Record with id ${ recordId } not found` } ;
450- }
451-
452- attachmentFiles = await this . options . generation . attachFiles ( { record, adminUser } ) ;
453- // if files is not array, make it array
454- if ( ! Array . isArray ( attachmentFiles ) ) {
455- attachmentFiles = [ attachmentFiles ] ;
456- }
457-
458- }
459-
460- let error : string | undefined = undefined ;
461-
462- const STUB_MODE = false ;
463-
464- const images = await Promise . all (
465- ( new Array ( this . options . generation . countToGenerate ) ) . fill ( 0 ) . map ( async ( ) => {
466- if ( STUB_MODE ) {
467- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
468- return `https://picsum.photos/200/300?random=${ Math . floor ( Math . random ( ) * 1000 ) } ` ;
469- }
470- const start = + new Date ( ) ;
471- let resp ;
472- try {
473- resp = await this . options . generation . adapter . generate (
474- {
475- prompt,
476- inputFiles : attachmentFiles ,
477- n : 1 ,
478- size : this . options . generation . outputSize ,
479- }
480- )
481- } catch ( e : any ) {
482- error = `No response from image generation provider: ${ e . message } . Please check your prompt or try again later.` ;
483- return ;
484- }
485430
486431 const jobId = randomUUID ( ) ;
487432 jobs . set ( jobId , { status : "in_progress" } ) ;
0 commit comments