Skip to content

Commit 65d36eb

Browse files
committed
fix: show uploaded image if user didn't overwrite existing values
1 parent 6c93c8c commit 65d36eb

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

custom/VisionAction.vue

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ const isDataSaved = ref(false);
238238
const regeneratingFieldsStatus = ref<Record<string, Record<string, boolean>>>({});
239239
const overwriteExistingValues = ref<boolean>(false);
240240
241+
const listOfImageThatWasNotGeneratedPerRecord = ref<Record<string, string[]>>({});
242+
241243
const openDialog = async () => {
242244
window.addEventListener('beforeunload', beforeUnloadHandler);
243245
if (props.meta.askConfirmationBeforeGenerating) {
@@ -544,6 +546,14 @@ async function saveData() {
544546
if (!value) {
545547
continue;
546548
}
549+
if (!overwriteExistingValues.value) {
550+
const imageURL = selected.value.find(rec => rec[primaryKey] === item[primaryKey])[key];
551+
const originalImageUrl = listOfImageThatWasNotGeneratedPerRecord.value[item[primaryKey]][key].originalImage;
552+
if (originalImageUrl === imageURL) {
553+
reqData.find(rec => rec[primaryKey] === item[primaryKey])[key] = undefined;
554+
continue;
555+
}
556+
}
547557
const p = uploadImage(value, item[primaryKey], key).then(result => {
548558
item[key] = result;
549559
});
@@ -553,7 +563,6 @@ async function saveData() {
553563
}
554564
await Promise.all(imagesToUpload);
555565
}
556-
557566
const res = await callAdminForthApi({
558567
path: `/plugin/${props.meta.pluginInstanceId}/update_fields`,
559568
method: 'POST',
@@ -729,7 +738,14 @@ async function runAiAction({
729738
for (const [key, value] of Object.entries(carouselSaveImages.value[index])) {
730739
if (props.meta.outputImageFields?.includes(key)) {
731740
carouselSaveImages.value[index][key] = [jobResponse.job.result[key]];
732-
}
741+
if (jobResponse.job.recordMeta?.[`${key}_meta`]) {
742+
carouselSaveImages.value[index][key] = [jobResponse.job.recordMeta[`${key}_meta`].originalImage];
743+
if (!listOfImageThatWasNotGeneratedPerRecord.value[recordId]) {
744+
listOfImageThatWasNotGeneratedPerRecord.value[recordId] = [];
745+
}
746+
listOfImageThatWasNotGeneratedPerRecord.value[recordId][key] = jobResponse.job.recordMeta[`${key}_meta`];
747+
}
748+
}
733749
}
734750
}
735751
//marking that we received response for this record

index.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,19 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
260260
}
261261
const fieldTasks = Object.keys(this.options?.generateImages || {}).map(async (key) => {
262262
if ( record[key] && filterFilledFields ) {
263-
return { key, images: [] };
263+
const plugin = this.adminforth.activatedPlugins.find(p =>
264+
p.resourceConfig!.resourceId === this.resourceConfig.resourceId &&
265+
p.pluginOptions.pathColumnName === key
266+
);
267+
const image_url = await plugin.pluginOptions.storageAdapter.getDownloadUrl(record[key]);
268+
return {
269+
key,
270+
images: [image_url],
271+
meta: {
272+
skippedToNotOverwrite: true,
273+
originalImage: image_url,
274+
}
275+
};
264276
}
265277
const prompt = (await this.compileGenerationFieldTemplates(record, customPrompt))[key];
266278
let images;
@@ -312,18 +324,22 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
312324
});
313325

314326
const fieldResults = await Promise.all(fieldTasks);
315-
const recordResult: Record<string, string[]> = {};
327+
const recordResult: Record<string, any> = {};
328+
const recordMeta: Record<string, any> = {};
316329

317-
fieldResults.forEach(({ key, images }) => {
330+
fieldResults.forEach(({ key, images, meta }) => {
318331
recordResult[key] = images;
332+
if (meta) {
333+
recordMeta[`${key}_meta`] = meta;
334+
}
319335
});
320336

321337
const result = recordResult;
322338

323339
if (!isError) {
324340
this.totalCalls++;
325341
this.totalDuration += (+new Date() - start) / 1000;
326-
jobs.set(jobId, { status: 'completed', result });
342+
jobs.set(jobId, { status: 'completed', result, recordMeta });
327343
return { ok: true }
328344
} else {
329345
return { ok: false, error: 'Error during image generation' };

0 commit comments

Comments
 (0)