Skip to content

Commit 540c122

Browse files
committed
chore: reduce handlebars handler lenght
1 parent b113c52 commit 540c122

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

index.ts

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,34 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
2222
}
2323

2424
// Compile Handlebars templates in outputFields using record fields as context
25-
private compileOutputFieldsTemplates(record: any): Record<string, string> {
25+
private compileTemplates<T extends Record<string, any>>(
26+
source: T,
27+
record: any,
28+
valueSelector: (value: T[keyof T]) => string
29+
): Record<string, string> {
2630
const compiled: Record<string, string> = {};
27-
for (const [key, templateStr] of Object.entries(this.options.fillFieldsFromImages)) {
31+
for (const [key, value] of Object.entries(source)) {
32+
const templateStr = valueSelector(value);
2833
try {
29-
const tpl = Handlebars.compile(String(templateStr));
34+
const tpl = Handlebars.compile(templateStr);
3035
compiled[key] = tpl(record);
3136
} catch {
32-
compiled[key] = String(templateStr);
37+
compiled[key] = templateStr;
3338
}
3439
}
3540
return compiled;
3641
}
3742

38-
private compileOutputFieldsTemplatesNoImage(record: any): Record<string, string> {
39-
const compiled: Record<string, string> = {};
40-
for (const [key, templateStr] of Object.entries(this.options.fillPlainFields)) {
41-
try {
42-
const tpl = Handlebars.compile(String(templateStr));
43-
compiled[key] = tpl(record);
44-
} catch {
45-
compiled[key] = String(templateStr);
46-
}
47-
}
48-
return compiled;
43+
private compileOutputFieldsTemplates(record: any) {
44+
return this.compileTemplates(this.options.fillFieldsFromImages, record, v => String(v));
45+
}
46+
47+
private compileOutputFieldsTemplatesNoImage(record: any) {
48+
return this.compileTemplates(this.options.fillPlainFields, record, v => String(v));
4949
}
5050

5151
private compileGenerationFieldTemplates(record: any) {
52-
const compiled: Record<string, any> = {};
53-
for (const key in this.options.generateImages) {
54-
try {
55-
const tpl = Handlebars.compile(String(this.options.generateImages[key].prompt));
56-
compiled[key] = tpl(record);
57-
} catch {
58-
compiled[key] = String(this.options.generateImages[key].prompt);
59-
}
60-
}
61-
return compiled;
52+
return this.compileTemplates(this.options.generateImages, record, v => String(v.prompt));
6253
}
6354

6455
private checkRateLimit(field: string,fieldNameRateLimit: string | undefined, headers: Record<string, string | string[] | undefined>): { error?: string } | void {
@@ -294,7 +285,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
294285
Each object must contain the following fields: ${JSON.stringify(compiledOutputFields)} Use the exact field names.
295286
If it's number field - return only number.`;
296287
//send prompt to OpenAI and get response
297-
const { content: chatResponse, finishReason } = await this.options.textCompleteAdapter.complete(prompt, [], 500);
288+
const { content: chatResponse } = await this.options.textCompleteAdapter.complete(prompt, [], 500);
298289

299290
const resp: any = (chatResponse as any).response;
300291
const topLevelError = (chatResponse as any).error;
@@ -494,7 +485,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
494485
if (this.options.generateImages[key].adapter) {
495486
generationAdapter = this.options.generateImages[key].adapter;
496487
} else {
497-
generationAdapter = this.options.imageGenerationAdapter;``
488+
generationAdapter = this.options.imageGenerationAdapter;
498489
}
499490
const resp = await generationAdapter.generate(
500491
{

0 commit comments

Comments
 (0)