Skip to content

Commit c83d89d

Browse files
committed
feat: add template field validation and typo suggestion for image generation prompts
1 parent b5d4bd1 commit c83d89d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

index.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AdminForthPlugin, Filters } from "adminforth";
22
import type { IAdminForth, IHttpServer, AdminForthComponentDeclaration, AdminForthResource } from "adminforth";
3+
import { suggestIfTypo } from "adminforth";
34
import type { PluginOptions } from './types.js';
45
import Handlebars from 'handlebars';
56
import { RateLimiter } from "adminforth";
@@ -461,6 +462,53 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
461462
`);
462463
}
463464
}
465+
if (this.options.fillFieldsFromImages || this.options.fillPlainFields || this.options.generateImages) {
466+
let matches: string[] = [];
467+
const regex = /{{(.*?)}}/g;
468+
469+
if (this.options.fillFieldsFromImages) {
470+
for (const [key, value] of Object.entries((this.options.fillFieldsFromImages ))) {
471+
const template = value;
472+
const templateMatches = template.match(regex);
473+
if (templateMatches) {
474+
matches.push(...templateMatches);
475+
}
476+
}
477+
}
478+
if (this.options.fillPlainFields) {
479+
for (const [key, value] of Object.entries((this.options.fillPlainFields))) {
480+
const template = value;
481+
const templateMatches = template.match(regex);
482+
if (templateMatches) {
483+
matches.push(...templateMatches);
484+
}
485+
}
486+
}
487+
if (this.options.generateImages) {
488+
for (const [key, value] of Object.entries((this.options.generateImages ))) {
489+
const template = value.prompt;
490+
const templateMatches = template.match(regex);
491+
if (templateMatches) {
492+
matches.push(...templateMatches);
493+
}
494+
}
495+
}
496+
497+
if (matches) {
498+
matches.forEach((match) => {
499+
const field = match.replace(/{{|}}/g, '').trim();
500+
if (!resourceConfig.columns.find((column: any) => column.name === field)) {
501+
const similar = suggestIfTypo(resourceConfig.columns.map((column: any) => column.name), field);
502+
throw new Error(`Field "${field}" specified in generationPrompt not found in resource "${resourceConfig.label}". ${similar ? `Did you mean "${similar}"?` : ''}`);
503+
} else {
504+
let column = resourceConfig.columns.find((column: any) => column.name === field);
505+
if (column.backendOnly === true) {
506+
throw new Error(`Field "${field}" specified in generationPrompt is marked as backendOnly in resource "${resourceConfig.label}". Please remove backendOnly or choose another field.`);
507+
}
508+
}
509+
});
510+
}
511+
}
464512
}
465513
}
466514

0 commit comments

Comments
 (0)