Skip to content

Commit 5989e7c

Browse files
committed
feat: add handlebars and remove fields for context
1 parent 20f9f5f commit 5989e7c

File tree

5 files changed

+85
-38
lines changed

5 files changed

+85
-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
@@ -213,28 +214,9 @@ onMounted(async () => {
213214
}
214215
// iterate over all variables in template and replace them with their values from props.record[field].
215216
// if field is not present in props.record[field] then replace it with empty string and drop warning
216-
const regex = /{{(.*?)}}/g;
217-
const matches = template.match(regex);
218-
if (matches) {
219-
matches.forEach((match) => {
220-
const field = match.replace(/{{|}}/g, '').trim();
221-
if (field in context) {
222-
return;
223-
} else if (field in props.record) {
224-
context[field] = minifyField(props.record[field]);
225-
} else {
226-
adminforth.alert({
227-
message: $t('Field {{field}} defined in template but not found in record', { field }),
228-
variant: 'warning',
229-
timeout: 15,
230-
});
231-
}
232-
});
233-
}
234-
235-
prompt.value = template.replace(regex, (_, field) => {
236-
return context[field.trim()] || '';
237-
});
217+
const tpl = Handlebars.compile(template);
218+
const compiledTemplate = tpl(props.record);
219+
prompt.value = compiledTemplate;
238220
239221
const recordId = props.record[props.meta.recorPkFieldName];
240222
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: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,6 @@ export default class UploadPlugin extends AdminForthPlugin {
5656
throw new Error(`Column with name "${pathColumnName}" not found in resource "${resourceConfig.label}"`);
5757
}
5858

59-
if (this.options.generation?.fieldsForContext) {
60-
this.options.generation?.fieldsForContext.forEach((field: string) => {
61-
if (!resourceConfig.columns.find((column: any) => column.name === field)) {
62-
const similar = suggestIfTypo(resourceConfig.columns.map((column: any) => column.name), field);
63-
throw new Error(`Field "${field}" specified in fieldsForContext not found in
64-
resource "${resourceConfig.label}". ${similar ? `Did you mean "${similar}"?` : ''}`);
65-
}
66-
});
67-
}
68-
6959
const pluginFrontendOptions = {
7060
allowedExtensions: this.options.allowedFileExtensions,
7161
maxFileSize: this.options.maxFileSize,
@@ -257,6 +247,21 @@ export default class UploadPlugin extends AdminForthPlugin {
257247

258248
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: any) {
259249
this.adminforth = adminforth;
250+
251+
if (this.options.generation) {
252+
const template = this.options.generation?.generationPrompt;
253+
const regex = /{{(.*?)}}/g;
254+
const matches = template.match(regex);
255+
if (matches) {
256+
matches.forEach((match) => {
257+
const field = match.replace(/{{|}}/g, '').trim();
258+
if (!resourceConfig.columns.find((column: any) => column.name === field)) {
259+
const similar = suggestIfTypo(resourceConfig.columns.map((column: any) => column.name), field);
260+
throw new Error(`Field "${field}" specified in generationPrompt not found in resource "${resourceConfig.label}". ${similar ? `Did you mean "${similar}"?` : ''}`);
261+
}
262+
});
263+
}
264+
}
260265
// called here because modifyResourceConfig can be called in build time where there is no environment and AWS secrets
261266
this.setupLifecycleRule();
262267
}

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)