@@ -59,6 +59,19 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
5959 return await this . compileTemplates ( customPrompt ? JSON . parse ( customPrompt ) : this . options . generateImages , record , v => String ( customPrompt ? v : v . prompt ) ) ;
6060 }
6161
62+ private removeFromPromptFilledFields ( compiledOutputFields : Record < string , string > , record : Record < string , any > ) : Record < string , string > {
63+ const newCompiledOutputFields : Record < string , string > = { } ;
64+ for ( const [ key , value ] of Object . entries ( record ) ) {
65+ if ( compiledOutputFields [ key ] ) {
66+ if ( value !== null && value !== undefined && value !== '' ) {
67+ continue ;
68+ }
69+ newCompiledOutputFields [ key ] = compiledOutputFields [ key ] ;
70+ }
71+ }
72+ return newCompiledOutputFields ;
73+ }
74+
6275 private async checkRateLimit ( field : string , fieldNameRateLimit : string | undefined , headers : Record < string , string | string [ ] | undefined > ) : Promise < void | { error ?: string ; } > {
6376 if ( fieldNameRateLimit ) {
6477 // rate limit
@@ -94,7 +107,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
94107 return prompt ;
95108 }
96109
97- private async analyze_image ( jobId : string , recordId : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string ) {
110+ private async analyze_image ( jobId : string , recordId : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string , filterFilledFields : boolean = true ) {
98111 const selectedId = recordId ;
99112 let isError = false ;
100113 // Fetch the record using the provided ID
@@ -125,8 +138,15 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
125138 }
126139 //create prompt for OpenAI
127140 const compiledOutputFields = await this . compileOutputFieldsTemplates ( record , customPrompt ) ;
128- const prompt = this . getPromptForImageAnalysis ( compiledOutputFields ) ;
141+ const filteredCompiledOutputFields = filterFilledFields ? this . removeFromPromptFilledFields ( compiledOutputFields , record ) : compiledOutputFields ;
142+
143+ if ( Object . keys ( filteredCompiledOutputFields ) . length === 0 ) {
144+ jobs . set ( jobId , { status : 'completed' , result : { } } ) ;
145+ return { ok : true } ;
146+ }
129147
148+ const prompt = this . getPromptForImageAnalysis ( filteredCompiledOutputFields ) ;
149+
130150 //send prompt to OpenAI and get response
131151 let chatResponse ;
132152 try {
@@ -168,7 +188,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
168188
169189 }
170190
171- private async analyzeNoImages ( jobId : string , recordId : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string ) {
191+ private async analyzeNoImages ( jobId : string , recordId : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string , filterFilledFields : boolean = true ) {
172192 const selectedId = recordId ;
173193 let isError = false ;
174194 if ( STUB_MODE ) {
@@ -181,7 +201,14 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
181201 const record = await this . adminforth . resource ( this . resourceConfig . resourceId ) . get ( [ Filters . EQ ( primaryKeyColumn . name , selectedId ) ] ) ;
182202
183203 const compiledOutputFields = await this . compileOutputFieldsTemplatesNoImage ( record , customPrompt ) ;
184- const prompt = this . getPromptForPlainFields ( compiledOutputFields ) ;
204+
205+ const filteredCompiledOutputFields = filterFilledFields ? this . removeFromPromptFilledFields ( compiledOutputFields , record ) : compiledOutputFields ;
206+
207+ if ( Object . keys ( filteredCompiledOutputFields ) . length === 0 ) {
208+ jobs . set ( jobId , { status : 'completed' , result : { } } ) ;
209+ return { ok : true } ;
210+ }
211+ const prompt = this . getPromptForPlainFields ( filteredCompiledOutputFields ) ;
185212 //send prompt to OpenAI and get response
186213 const numberOfTokens = this . options . fillPlainFieldsMaxTokens ? this . options . fillPlainFieldsMaxTokens : 1000 ;
187214 let resp : any ;
@@ -212,7 +239,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
212239 }
213240 }
214241
215- private async initialImageGenerate ( jobId : string , recordId : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string ) {
242+ private async initialImageGenerate ( jobId : string , recordId : string , adminUser : any , headers : Record < string , string | string [ ] | undefined > , customPrompt ? : string , filterFilledFields : boolean = true ) {
216243 const selectedId = recordId ;
217244 let isError = false ;
218245 const start = + new Date ( ) ;
@@ -232,6 +259,9 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
232259 }
233260 }
234261 const fieldTasks = Object . keys ( this . options ?. generateImages || { } ) . map ( async ( key ) => {
262+ if ( record [ key ] && filterFilledFields ) {
263+ return { key, images : [ ] } ;
264+ }
235265 const prompt = ( await this . compileGenerationFieldTemplates ( record , customPrompt ) ) [ key ] ;
236266 let images ;
237267 if ( this . options . attachFiles && attachmentFiles . length === 0 ) {
@@ -837,7 +867,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
837867 method : 'POST' ,
838868 path : `/plugin/${ this . pluginInstanceId } /create-job` ,
839869 handler : async ( { body, adminUser, headers } ) => {
840- const { actionType, recordId, customPrompt } = body ;
870+ const { actionType, recordId, customPrompt, filterFilledFields } = body ;
841871 const jobId = randomUUID ( ) ;
842872 jobs . set ( jobId , { status : "in_progress" } ) ;
843873 if ( ! actionType ) {
@@ -850,13 +880,13 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
850880 } else {
851881 switch ( actionType ) {
852882 case 'generate_images' :
853- this . initialImageGenerate ( jobId , recordId , adminUser , headers , customPrompt ) ;
883+ this . initialImageGenerate ( jobId , recordId , adminUser , headers , customPrompt , filterFilledFields ) ;
854884 break ;
855885 case 'analyze_no_images' :
856- this . analyzeNoImages ( jobId , recordId , adminUser , headers , customPrompt ) ;
886+ this . analyzeNoImages ( jobId , recordId , adminUser , headers , customPrompt , filterFilledFields ) ;
857887 break ;
858888 case 'analyze' :
859- this . analyze_image ( jobId , recordId , adminUser , headers , customPrompt ) ;
889+ this . analyze_image ( jobId , recordId , adminUser , headers , customPrompt , filterFilledFields ) ;
860890 break ;
861891 case 'regenerate_images' :
862892 if ( ! body . prompt || ! body . fieldName ) {
0 commit comments