Skip to content

Commit 581e63e

Browse files
committed
chore: refactor AI action handling and remove deprecated analyze functions
1 parent 540c122 commit 581e63e

File tree

1 file changed

+56
-132
lines changed

1 file changed

+56
-132
lines changed

custom/visionAction.vue

Lines changed: 56 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
<script lang="ts" setup>
6868
import { callAdminForthApi } from '@/utils';
69-
import { ref, watch } from 'vue'
69+
import { Ref, ref, watch } from 'vue'
7070
import { Dialog, Button } from '@/afcl';
7171
import VisionTable from './visionTable.vue'
7272
import adminforth from '@/adminforth';
@@ -131,13 +131,25 @@ const openDialog = async () => {
131131
isLoading.value = true;
132132
const tasks = [];
133133
if (props.meta.isFieldsForAnalizeFromImages) {
134-
tasks.push(analyzeFields());
134+
tasks.push(runAiAction({
135+
endpoint: 'analyze',
136+
actionType: 'analyze',
137+
responseFlag: isAiResponseReceivedAnalize,
138+
}));
135139
}
136140
if (props.meta.isFieldsForAnalizePlain) {
137-
tasks.push(analyzeFieldsNoImages());
141+
tasks.push(runAiAction({
142+
endpoint: 'analyze_no_images',
143+
actionType: 'analyze_no_images',
144+
responseFlag: isAiResponseReceivedAnalize,
145+
}));
138146
}
139147
if (props.meta.isImageGeneration) {
140-
tasks.push(generateImages());
148+
tasks.push(runAiAction({
149+
endpoint: 'initial_image_generate',
150+
actionType: 'generate_images',
151+
responseFlag: isAiResponseReceivedImage,
152+
}));
141153
}
142154
await Promise.all(tasks);
143155
isLoading.value = false;
@@ -160,6 +172,7 @@ const closeDialog = () => {
160172
tableColumnsIndexes.value = [];
161173
isError.value = false;
162174
isCriticalError.value = false;
175+
isImageGenerationError.value = false;
163176
errorMessage.value = '';
164177
}
165178
@@ -329,111 +342,6 @@ async function convertImages(fieldName, img) {
329342
}
330343
331344
332-
async function analyzeFields() {
333-
isAiResponseReceivedAnalize.value = props.checkboxes.map(() => false);
334-
try {
335-
const res = await callAdminForthApi({
336-
path: `/plugin/${props.meta.pluginInstanceId}/analyze`,
337-
method: 'POST',
338-
body: {
339-
selectedIds: props.checkboxes,
340-
},
341-
});
342-
343-
if (res?.error) {
344-
adminforth.alert({
345-
message: res.error,
346-
variant: 'danger',
347-
timeout: 'unlimited',
348-
});
349-
350-
console.error('Failed to analyze image(s):', res.error);
351-
isError.value = true;
352-
//isCriticalError.value = true;
353-
errorMessage.value = `Failed to fetch analyze image(s). Please, try to re-run the action.`;
354-
} else {
355-
res.result.forEach((item, idx) => {
356-
const pk = selected.value[idx]?.[primaryKey]
357-
358-
if (pk) {
359-
selected.value[idx] = {
360-
...selected.value[idx],
361-
...item,
362-
isChecked: true,
363-
[primaryKey]: pk
364-
}
365-
}
366-
})
367-
}
368-
} catch (error) {
369-
adminforth.alert({
370-
message: error,
371-
variant: 'danger',
372-
timeout: 'unlimited',
373-
});
374-
375-
console.error('Failed to analyze image(s):', error);
376-
isError.value = true;
377-
//isCriticalError.value = true;
378-
errorMessage.value = res.error;
379-
}
380-
isAiResponseReceivedAnalize.value = props.checkboxes.map(() => true);
381-
}
382-
383-
384-
async function analyzeFieldsNoImages() {
385-
isAiResponseReceivedAnalize.value = props.checkboxes.map(() => false);
386-
try {
387-
const res = await callAdminForthApi({
388-
path: `/plugin/${props.meta.pluginInstanceId}/analyze_no_images`,
389-
method: 'POST',
390-
body: {
391-
selectedIds: props.checkboxes,
392-
},
393-
});
394-
if(res?.error) {
395-
adminforth.alert({
396-
message: res.error,
397-
variant: 'danger',
398-
timeout: 'unlimited',
399-
});
400-
console.error('Failed to analyze fields:', res.error);
401-
isError.value = true;
402-
//isCriticalError.value = true;
403-
errorMessage.value = res.error;
404-
} else {
405-
res.result.forEach((item, idx) => {
406-
const pk = selected.value[idx]?.[primaryKey]
407-
408-
if (pk) {
409-
selected.value[idx] = {
410-
...selected.value[idx],
411-
...item,
412-
isChecked: true,
413-
[primaryKey]: pk
414-
}
415-
}
416-
})
417-
}
418-
} catch (error) {
419-
adminforth.alert({
420-
message: error,
421-
variant: 'danger',
422-
timeout: 'unlimited',
423-
});
424-
console.error('Failed to analyze fields:', error);
425-
isError.value = true;
426-
//isCriticalError.value = true;
427-
errorMessage.value = `Failed to analyze fields. Please, try to re-run the action.`;
428-
}
429-
if(!props.meta.isFieldsForAnalizeFromImages) {
430-
isAiResponseReceivedAnalize.value = props.checkboxes.map(() => true);
431-
}
432-
}
433-
434-
435-
436-
437345
async function saveData() {
438346
if (!selected.value?.length) {
439347
adminforth.alert({ message: 'No items selected', variant: 'warning' });
@@ -496,57 +404,73 @@ async function saveData() {
496404
}
497405
}
498406
499-
async function generateImages() {
500-
isAiResponseReceivedImage.value = props.checkboxes.map(() => false);
501-
let res;
502-
let error = null;
407+
408+
async function runAiAction({
409+
endpoint,
410+
actionType,
411+
responseFlag,
412+
updateOnSuccess = true,
413+
}: {
414+
endpoint: string;
415+
actionType: 'analyze' | 'analyze_no_images' | 'generate_images';
416+
responseFlag: Ref<boolean[]>;
417+
updateOnSuccess?: boolean;
418+
}) {
419+
let res: any;
420+
let error: any = null;
503421
504422
try {
423+
responseFlag.value = props.checkboxes.map(() => false);
424+
505425
res = await callAdminForthApi({
506-
path: `/plugin/${props.meta.pluginInstanceId}/initial_image_generate`,
426+
path: `/plugin/${props.meta.pluginInstanceId}/${endpoint}`,
507427
method: 'POST',
508428
body: {
509429
selectedIds: props.checkboxes,
510430
},
511431
});
432+
433+
if (actionType !== 'analyze_no_images' || !props.meta.isFieldsForAnalizeFromImages) {
434+
responseFlag.value = props.checkboxes.map(() => true);
435+
}
512436
} catch (e) {
513-
console.error('Error generating images:', e);
514-
isError.value = true;
515-
isImageGenerationError.value = true;
516-
errorMessage.value = `Failed to generate images. Please, try to re-run the action.`;
437+
console.error(`Error during ${actionType}:`, e);
438+
error = `Failed to ${actionType.replace('_', ' ')}. Please, try to re-run the action.`;
517439
}
518-
isAiResponseReceivedImage.value = props.checkboxes.map(() => true);
519440
520441
if (res?.error) {
521442
error = res.error;
522443
}
523-
if (!res) {
524-
error = 'Error generating images, something went wrong';
525-
isError.value = true;
526-
isImageGenerationError.value = true;
527-
errorMessage.value = `Failed to generate images. Please, try to re-run the action.`;
444+
if (!res && !error) {
445+
error = `Error: ${actionType} request returned empty response.`;
528446
}
529447
530-
if (error) {
448+
if (error) {
531449
adminforth.alert({
532450
message: error,
533451
variant: 'danger',
534452
timeout: 'unlimited',
535453
});
536454
isError.value = true;
537-
isImageGenerationError.value = true;
455+
if (actionType === 'generate_images') {
456+
isImageGenerationError.value = true;
457+
}
538458
errorMessage.value = error;
539-
} else {
540-
res.result.forEach((item, idx) => {
541-
const pk = selected.value[idx]?.[primaryKey]
459+
return;
460+
}
461+
462+
if (updateOnSuccess) {
463+
res.result.forEach((item: any, idx: number) => {
464+
const pk = selected.value[idx]?.[primaryKey];
542465
if (pk) {
543466
selected.value[idx] = {
544467
...selected.value[idx],
545468
...item,
546-
[primaryKey]: pk
547-
}
469+
isChecked: true,
470+
[primaryKey]: pk,
471+
};
548472
}
549-
})
473+
});
550474
}
551475
}
552476

0 commit comments

Comments
 (0)