Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions custom/imageGenerator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ import { callAdminForthApi } from '@/utils';
import { useI18n } from 'vue-i18n';
import adminforth from '@/adminforth';
import { ProgressBar } from '@/afcl';
import * as Handlebars from 'handlebars';

const { t: $t } = useI18n();

Expand Down Expand Up @@ -213,28 +214,9 @@ onMounted(async () => {
}
// iterate over all variables in template and replace them with their values from props.record[field].
// if field is not present in props.record[field] then replace it with empty string and drop warning
const regex = /{{(.*?)}}/g;
const matches = template.match(regex);
if (matches) {
matches.forEach((match) => {
const field = match.replace(/{{|}}/g, '').trim();
if (field in context) {
return;
} else if (field in props.record) {
context[field] = minifyField(props.record[field]);
} else {
adminforth.alert({
message: $t('Field {{field}} defined in template but not found in record', { field }),
variant: 'warning',
timeout: 15,
});
}
});
}

prompt.value = template.replace(regex, (_, field) => {
return context[field.trim()] || '';
});
const tpl = Handlebars.compile(template);
const compiledTemplate = tpl(props.record);
prompt.value = compiledTemplate;

const recordId = props.record[props.meta.recorPkFieldName];
if (!recordId) return;
Expand Down
65 changes: 65 additions & 0 deletions custom/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions custom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"@iconify-prerendered/vue-mdi": "^0.25.1718880438",
"handlebars": "^4.7.8",
"medium-zoom": "^1.1.0"
}
}
30 changes: 20 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ export default class UploadPlugin extends AdminForthPlugin {
throw new Error(`Column with name "${pathColumnName}" not found in resource "${resourceConfig.label}"`);
}

if (this.options.generation?.fieldsForContext) {
this.options.generation?.fieldsForContext.forEach((field: string) => {
if (!resourceConfig.columns.find((column: any) => column.name === field)) {
const similar = suggestIfTypo(resourceConfig.columns.map((column: any) => column.name), field);
throw new Error(`Field "${field}" specified in fieldsForContext not found in
resource "${resourceConfig.label}". ${similar ? `Did you mean "${similar}"?` : ''}`);
}
});
}

const pluginFrontendOptions = {
allowedExtensions: this.options.allowedFileExtensions,
maxFileSize: this.options.maxFileSize,
Expand Down Expand Up @@ -257,6 +247,26 @@ export default class UploadPlugin extends AdminForthPlugin {

validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: any) {
this.adminforth = adminforth;

if (this.options.generation) {
const template = this.options.generation?.generationPrompt;
const regex = /{{(.*?)}}/g;
const matches = template.match(regex);
if (matches) {
matches.forEach((match) => {
const field = match.replace(/{{|}}/g, '').trim();
if (!resourceConfig.columns.find((column: any) => column.name === field)) {
const similar = suggestIfTypo(resourceConfig.columns.map((column: any) => column.name), field);
throw new Error(`Field "${field}" specified in generationPrompt not found in resource "${resourceConfig.label}". ${similar ? `Did you mean "${similar}"?` : ''}`);
} else {
let column = resourceConfig.columns.find((column: any) => column.name === field);
if (column.backendOnly === true) {
throw new Error(`Field "${field}" specified in generationPrompt is marked as backendOnly in resource "${resourceConfig.label}". Please remove backendOnly or choose another field.`);
}
}
});
}
}
// called here because modifyResourceConfig can be called in build time where there is no environment and AWS secrets
this.setupLifecycleRule();
}
Expand Down
6 changes: 0 additions & 6 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ export type PluginOptions = {
*/
outputSize?: string,

/**
* Fields for conetext which will be used to generate the image.
* If specified, the plugin will use fields from the record to provide additional context to the AI model.
*/
fieldsForContext?: string[],

/**
* The number of images to generate
* in one request
Expand Down