Skip to content

Commit 1ddca48

Browse files
authored
Merge pull request #17 from devforth/AdminForth/605
feat: add handlebars and remove fields for context
2 parents 57c87de + 1ec8a19 commit 1ddca48

File tree

5 files changed

+90
-38
lines changed

5 files changed

+90
-38
lines changed

custom/imageGenerator.vue

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ import { callAdminForthApi } from '@/utils';
181181
import { useI18n } from 'vue-i18n';
182182
import adminforth from '@/adminforth';
183183
import { ProgressBar } from '@/afcl';
184+
import * as Handlebars from 'handlebars';
184185
185186
const { t: $t } = useI18n();
186187
@@ -214,28 +215,9 @@ onMounted(async () => {
214215
}
215216
// iterate over all variables in template and replace them with their values from props.record[field].
216217
// if field is not present in props.record[field] then replace it with empty string and drop warning
217-
const regex = /{{(.*?)}}/g;
218-
const matches = template.match(regex);
219-
if (matches) {
220-
matches.forEach((match) => {
221-
const field = match.replace(/{{|}}/g, '').trim();
222-
if (field in context) {
223-
return;
224-
} else if (field in props.record) {
225-
context[field] = minifyField(props.record[field]);
226-
} else {
227-
adminforth.alert({
228-
message: $t('Field {{field}} defined in template but not found in record', { field }),
229-
variant: 'warning',
230-
timeout: 15,
231-
});
232-
}
233-
});
234-
}
235-
236-
prompt.value = template.replace(regex, (_, field) => {
237-
return context[field.trim()] || '';
238-
});
218+
const tpl = Handlebars.compile(template);
219+
const compiledTemplate = tpl(props.record);
220+
prompt.value = compiledTemplate;
239221
240222
const recordId = props.record[props.meta.recorPkFieldName];
241223
if (!recordId) return;

custom/package-lock.json

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

custom/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"license": "ISC",
1212
"dependencies": {
1313
"@iconify-prerendered/vue-mdi": "^0.25.1718880438",
14+
"handlebars": "^4.7.8",
1415
"medium-zoom": "^1.1.0"
1516
}
1617
}

index.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,6 @@ export default class UploadPlugin extends AdminForthPlugin {
219219
throw new Error(`Column with name "${pathColumnName}" not found in resource "${resourceConfig.label}"`);
220220
}
221221

222-
if (this.options.generation?.fieldsForContext) {
223-
this.options.generation?.fieldsForContext.forEach((field: string) => {
224-
if (!resourceConfig.columns.find((column: any) => column.name === field)) {
225-
const similar = suggestIfTypo(resourceConfig.columns.map((column: any) => column.name), field);
226-
throw new Error(`Field "${field}" specified in fieldsForContext not found in
227-
resource "${resourceConfig.label}". ${similar ? `Did you mean "${similar}"?` : ''}`);
228-
}
229-
});
230-
}
231-
232222
const pluginFrontendOptions = {
233223
allowedExtensions: this.options.allowedFileExtensions,
234224
maxFileSize: this.options.maxFileSize,
@@ -386,6 +376,26 @@ export default class UploadPlugin extends AdminForthPlugin {
386376

387377
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: any) {
388378
this.adminforth = adminforth;
379+
380+
if (this.options.generation) {
381+
const template = this.options.generation?.generationPrompt;
382+
const regex = /{{(.*?)}}/g;
383+
const matches = template.match(regex);
384+
if (matches) {
385+
matches.forEach((match) => {
386+
const field = match.replace(/{{|}}/g, '').trim();
387+
if (!resourceConfig.columns.find((column: any) => column.name === field)) {
388+
const similar = suggestIfTypo(resourceConfig.columns.map((column: any) => column.name), field);
389+
throw new Error(`Field "${field}" specified in generationPrompt not found in resource "${resourceConfig.label}". ${similar ? `Did you mean "${similar}"?` : ''}`);
390+
} else {
391+
let column = resourceConfig.columns.find((column: any) => column.name === field);
392+
if (column.backendOnly === true) {
393+
throw new Error(`Field "${field}" specified in generationPrompt is marked as backendOnly in resource "${resourceConfig.label}". Please remove backendOnly or choose another field.`);
394+
}
395+
}
396+
});
397+
}
398+
}
389399
// called here because modifyResourceConfig can be called in build time where there is no environment and AWS secrets
390400
this.setupLifecycleRule();
391401
}

types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ export type PluginOptions = {
102102
*/
103103
outputSize?: string,
104104

105-
/**
106-
* Fields for conetext which will be used to generate the image.
107-
* If specified, the plugin will use fields from the record to provide additional context to the AI model.
108-
*/
109-
fieldsForContext?: string[],
110-
111105
/**
112106
* The number of images to generate
113107
* in one request

0 commit comments

Comments
 (0)