@@ -4,7 +4,7 @@ import type { PluginOptions } from './types.js';
4
4
import Handlebars from 'handlebars' ;
5
5
import { RateLimiter } from "adminforth" ;
6
6
7
-
7
+ const STUB_MODE = true ;
8
8
export default class BulkAiFlowPlugin extends AdminForthPlugin {
9
9
options : PluginOptions ;
10
10
uploadPlugin : AdminForthPlugin ;
@@ -140,6 +140,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
140
140
isFieldsForAnalizePlain : this . options . fillPlainFields ? Object . keys ( this . options . fillPlainFields ) . length > 0 : false ,
141
141
isImageGeneration : this . options . generateImages ? Object . keys ( this . options . generateImages ) . length > 0 : false ,
142
142
isAttachFiles : this . options . attachFiles ? true : false ,
143
+ disabledWhenNoCheckboxes : true ,
143
144
}
144
145
}
145
146
@@ -226,20 +227,23 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
226
227
method : 'POST' ,
227
228
path : `/plugin/${ this . pluginInstanceId } /analyze` ,
228
229
handler : async ( { body, adminUser, headers } ) => {
229
- const selectedIds = body . selectedIds || [ ] ;
230
+ const selectedId = body . selectedId || [ ] ;
230
231
if ( typeof ( this . options . rateLimits ?. fillFieldsFromImages ) === 'string' ) {
231
232
if ( this . checkRateLimit ( "fillFieldsFromImages" , this . options . rateLimits . fillFieldsFromImages , headers ) ) {
232
233
return { error : "Rate limit exceeded" } ;
233
234
}
234
235
}
235
- const tasks = selectedIds . map ( async ( ID ) => {
236
+ // const tasks = selectedIds.map(async (ID) => {
236
237
// Fetch the record using the provided ID
237
238
const primaryKeyColumn = this . resourceConfig . columns . find ( ( col ) => col . primaryKey ) ;
238
- const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( primaryKeyColumn . name , ID ) ] ) ;
239
+ const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( primaryKeyColumn . name , selectedId ) ] ) ;
239
240
240
241
//recieve image URLs to analyze
241
242
const attachmentFiles = await this . options . attachFiles ( { record : record } ) ;
242
- if ( attachmentFiles . length !== 0 ) {
243
+ if ( STUB_MODE ) {
244
+ await new Promise ( ( resolve ) => setTimeout ( resolve , Math . floor ( Math . random ( ) * 8000 ) + 1000 ) ) ;
245
+ return { } ;
246
+ } else if ( attachmentFiles . length !== 0 ) {
243
247
//create prompt for OpenAI
244
248
const compiledOutputFields = this . compileOutputFieldsTemplates ( record ) ;
245
249
const prompt = `Analyze the following image(s) and return a single JSON in format like: {'param1': 'value1', 'param2': 'value2'}.
@@ -263,53 +267,52 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
263
267
264
268
//parse response and update record
265
269
const resData = JSON . parse ( textOutput ) ;
266
-
267
- return resData ;
270
+ const result = resData ;
271
+ return { result } ;
268
272
} ;
269
- } ) ;
270
-
271
- const result = await Promise . all ( tasks ) ;
272
-
273
- return { result } ;
274
273
}
275
274
} ) ;
276
275
277
276
server . endpoint ( {
278
277
method : 'POST' ,
279
278
path : `/plugin/${ this . pluginInstanceId } /analyze_no_images` ,
280
279
handler : async ( { body, adminUser, headers } ) => {
281
- const selectedIds = body . selectedIds || [ ] ;
280
+ const selectedIds = body . selectedId || [ ] ;
282
281
if ( typeof ( this . options . rateLimits ?. fillPlainFields ) === 'string' ) {
283
282
if ( this . checkRateLimit ( "fillPlainFields" , this . options . rateLimits . fillPlainFields , headers ) ) {
284
283
return { error : "Rate limit exceeded" } ;
285
284
}
286
285
}
287
- const tasks = selectedIds . map ( async ( ID ) => {
288
- const primaryKeyColumn = this . resourceConfig . columns . find ( ( col ) => col . primaryKey ) ;
289
- const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( primaryKeyColumn . name , ID ) ] ) ;
290
-
291
- const compiledOutputFields = this . compileOutputFieldsTemplatesNoImage ( record ) ;
292
- const prompt = `Analyze the following fields and return a single JSON in format like: {'param1': 'value1', 'param2': 'value2'}.
293
- Do NOT return array of objects. Do NOT include any Markdown, code blocks, explanations, or extra text. Only return valid JSON.
294
- Each object must contain the following fields: ${ JSON . stringify ( compiledOutputFields ) } Use the exact field names.
295
- If it's number field - return only number.` ;
296
- //send prompt to OpenAI and get response
297
- const numberOfTokens = this . options . fillPlainFieldsMaxTokens ? this . options . fillPlainFieldsMaxTokens : 1000 ;
298
- const { content : chatResponse } = await this . options . textCompleteAdapter . complete ( prompt , [ ] , numberOfTokens ) ;
299
-
300
- const resp : any = ( chatResponse as any ) . response ;
301
- const topLevelError = ( chatResponse as any ) . error ;
302
- if ( topLevelError || resp ?. error ) {
303
- throw new Error ( `ERROR: ${ JSON . stringify ( topLevelError || resp ?. error ) } ` ) ;
304
- }
305
- const resData = JSON . parse ( chatResponse ) ;
286
+ //const tasks = selectedIds.map(async (ID) => {
287
+ if ( STUB_MODE ) {
288
+ await new Promise ( ( resolve ) => setTimeout ( resolve , Math . floor ( Math . random ( ) * 8000 ) + 1000 ) ) ;
289
+ return { } ;
290
+ } else {
291
+ const primaryKeyColumn = this . resourceConfig . columns . find ( ( col ) => col . primaryKey ) ;
292
+ const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( primaryKeyColumn . name , selectedId ) ] ) ;
306
293
307
- return resData ;
308
- } ) ;
294
+ const compiledOutputFields = this . compileOutputFieldsTemplatesNoImage ( record ) ;
295
+ const prompt = `Analyze the following fields and return a single JSON in format like: {'param1': 'value1', 'param2': 'value2'}.
296
+ Do NOT return array of objects. Do NOT include any Markdown, code blocks, explanations, or extra text. Only return valid JSON.
297
+ Each object must contain the following fields: ${ JSON . stringify ( compiledOutputFields ) } Use the exact field names.
298
+ If it's number field - return only number.` ;
299
+ //send prompt to OpenAI and get response
300
+ const numberOfTokens = this . options . fillPlainFieldsMaxTokens ? this . options . fillPlainFieldsMaxTokens : 1000 ;
301
+ const { content : chatResponse } = await this . options . textCompleteAdapter . complete ( prompt , [ ] , numberOfTokens ) ;
309
302
310
- const result = await Promise . all ( tasks ) ;
303
+ const resp : any = ( chatResponse as any ) . response ;
304
+ const topLevelError = ( chatResponse as any ) . error ;
305
+ if ( topLevelError || resp ?. error ) {
306
+ throw new Error ( `ERROR: ${ JSON . stringify ( topLevelError || resp ?. error ) } ` ) ;
307
+ }
308
+ const resData = JSON . parse ( chatResponse ) ;
309
+
310
+ //return resData;
311
+ const result = resData ;
311
312
312
- return { result } ;
313
+ return { result } ;
314
+ }
315
+ // });
313
316
}
314
317
} ) ;
315
318
server . endpoint ( {
@@ -443,7 +446,6 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
443
446
return { error : "Rate limit exceeded" } ;
444
447
}
445
448
const start = + new Date ( ) ;
446
- const STUB_MODE = false ;
447
449
const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( this . resourceConfig . columns . find ( c => c . primaryKey ) ?. name , Id ) ] ) ;
448
450
let attachmentFiles
449
451
if ( ! this . options . attachFiles ) {
@@ -458,7 +460,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
458
460
}
459
461
if ( STUB_MODE ) {
460
462
await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
461
- return `https://picsum.photos/200/300?random= ${ Math . floor ( Math . random ( ) * 1000 ) } ` ;
463
+ return `https://pic.re/image ` ;
462
464
}
463
465
464
466
let generationAdapter ;
@@ -483,20 +485,21 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
483
485
return { images } ;
484
486
}
485
487
} ) ;
488
+
489
+
486
490
server . endpoint ( {
487
491
method : 'POST' ,
488
492
path : `/plugin/${ this . pluginInstanceId } /initial_image_generate` ,
489
493
handler : async ( { body, headers } ) => {
490
- const selectedIds = body . selectedIds || [ ] ;
491
- const STUB_MODE = false ;
494
+ const selectedId = body . selectedId || [ ] ;
492
495
if ( typeof ( this . options . rateLimits ?. generateImages ) === 'string' ) {
493
496
if ( this . checkRateLimit ( "generateImages" , this . options . rateLimits . generateImages , headers ) ) {
494
497
return { error : "Rate limit exceeded" } ;
495
498
}
496
499
}
497
500
const start = + new Date ( ) ;
498
- const tasks = selectedIds . map ( async ( ID ) => {
499
- const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( this . resourceConfig . columns . find ( c => c . primaryKey ) ?. name , ID ) ] ) ;
501
+ // const tasks = selectedIds.map(async (ID) => {
502
+ const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( this . resourceConfig . columns . find ( c => c . primaryKey ) ?. name , selectedId ) ] ) ;
500
503
let attachmentFiles
501
504
if ( ! this . options . attachFiles ) {
502
505
attachmentFiles = [ ] ;
@@ -510,8 +513,8 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
510
513
return { key, images : [ ] } ;
511
514
} else {
512
515
if ( STUB_MODE ) {
513
- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
514
- images = `https://picsum.photos/200/300?random= ${ Math . floor ( Math . random ( ) * 1000 ) } ` ;
516
+ await new Promise ( ( resolve ) => setTimeout ( resolve , Math . floor ( Math . random ( ) * 8000 ) + 1000 ) ) ;
517
+ images = `https://pic.re/image ` ;
515
518
} else {
516
519
let generationAdapter ;
517
520
if ( this . options . generateImages [ key ] . adapter ) {
@@ -540,16 +543,17 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
540
543
recordResult [ key ] = images ;
541
544
} ) ;
542
545
543
- return recordResult ;
544
- } ) ;
545
- const result = await Promise . all ( tasks ) ;
546
+ // });
547
+ const result = recordResult ;
546
548
547
549
this . totalCalls ++ ;
548
550
this . totalDuration += ( + new Date ( ) - start ) / 1000 ;
549
551
550
552
return { result } ;
551
553
}
552
554
} ) ;
555
+
556
+
553
557
server . endpoint ( {
554
558
method : 'POST' ,
555
559
path : `/plugin/${ this . pluginInstanceId } /get_generation_prompts` ,
0 commit comments