@adminforth/bulk-ai-flow 1.14.4 → 1.14.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.js +20 -15
  2. package/index.ts +10 -7
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@ const jobs = new Map();
17
17
  export default class BulkAiFlowPlugin extends AdminForthPlugin {
18
18
  constructor(options) {
19
19
  super(options, import.meta.url);
20
+ this.rateLimiters = {};
20
21
  this.options = options;
21
22
  // for calculating average time
22
23
  this.totalCalls = 0;
@@ -47,18 +48,22 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
47
48
  return this.compileTemplates(this.options.generateImages, record, v => String(v.prompt));
48
49
  }
49
50
  checkRateLimit(field, fieldNameRateLimit, headers) {
50
- if (fieldNameRateLimit) {
51
- // rate limit
52
- // const { error } = RateLimiter.checkRateLimit(
53
- // field,
54
- // fieldNameRateLimit,
55
- // this.adminforth.auth.getClientIp(headers),
56
- // );
57
- const rateLimiter = new RateLimiter(fieldNameRateLimit);
58
- if (!rateLimiter.consume(`${field}-${this.adminforth.auth.getClientIp(headers)}`)) {
59
- return { error: "Rate limit exceeded" };
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ if (fieldNameRateLimit) {
53
+ // rate limit
54
+ // const { error } = RateLimiter.checkRateLimit(
55
+ // field,
56
+ // fieldNameRateLimit,
57
+ // this.adminforth.auth.getClientIp(headers),
58
+ // );
59
+ if (!this.rateLimiters[field]) {
60
+ this.rateLimiters[field] = new RateLimiter(fieldNameRateLimit);
61
+ }
62
+ if (!(yield this.rateLimiters[field].consume(`${field}-${this.adminforth.auth.getClientIp(headers)}`))) {
63
+ return { error: "Rate limit exceeded" };
64
+ }
60
65
  }
61
- }
66
+ });
62
67
  }
63
68
  analyze_image(jobId, recordId, adminUser, headers) {
64
69
  return __awaiter(this, void 0, void 0, function* () {
@@ -274,7 +279,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
274
279
  var _a;
275
280
  const Id = recordId;
276
281
  let isError = false;
277
- if (this.checkRateLimit(fieldName, this.options.generateImages[fieldName].rateLimit, headers)) {
282
+ if (yield this.checkRateLimit(fieldName, this.options.generateImages[fieldName].rateLimit, headers)) {
278
283
  jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
279
284
  return { error: "Rate limit exceeded" };
280
285
  }
@@ -707,17 +712,17 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
707
712
  var _b, _c, _d;
708
713
  const actionType = body.actionType;
709
714
  if (actionType === 'analyze' && ((_b = this.options.rateLimits) === null || _b === void 0 ? void 0 : _b.fillFieldsFromImages)) {
710
- if (this.checkRateLimit("fillFieldsFromImages", this.options.rateLimits.fillFieldsFromImages, headers)) {
715
+ if (yield this.checkRateLimit("fillFieldsFromImages", this.options.rateLimits.fillFieldsFromImages, headers)) {
711
716
  return { ok: false, error: "Rate limit exceeded for image analyze" };
712
717
  }
713
718
  }
714
719
  if (actionType === 'analyze_no_images' && ((_c = this.options.rateLimits) === null || _c === void 0 ? void 0 : _c.fillPlainFields)) {
715
- if (this.checkRateLimit("fillPlainFields", this.options.rateLimits.fillPlainFields, headers)) {
720
+ if (yield this.checkRateLimit("fillPlainFields", this.options.rateLimits.fillPlainFields, headers)) {
716
721
  return { ok: false, error: "Rate limit exceeded for plain field analyze" };
717
722
  }
718
723
  }
719
724
  if (actionType === 'generate_images' && ((_d = this.options.rateLimits) === null || _d === void 0 ? void 0 : _d.generateImages)) {
720
- if (this.checkRateLimit("generateImages", this.options.rateLimits.generateImages, headers)) {
725
+ if (yield this.checkRateLimit("generateImages", this.options.rateLimits.generateImages, headers)) {
721
726
  return { ok: false, error: "Rate limit exceeded for image generation" };
722
727
  }
723
728
  }
package/index.ts CHANGED
@@ -13,6 +13,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
13
13
  uploadPlugin: AdminForthPlugin;
14
14
  totalCalls: number;
15
15
  totalDuration: number;
16
+ rateLimiters: Record<string, RateLimiter> = {};
16
17
 
17
18
  constructor(options: PluginOptions) {
18
19
  super(options, import.meta.url);
@@ -54,7 +55,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
54
55
  return this.compileTemplates(this.options.generateImages, record, v => String(v.prompt));
55
56
  }
56
57
 
57
- private checkRateLimit(field: string,fieldNameRateLimit: string | undefined, headers: Record<string, string | string[] | undefined>): { error?: string } | void {
58
+ private async checkRateLimit(field: string, fieldNameRateLimit: string | undefined, headers: Record<string, string | string[] | undefined>): Promise<void | { error?: string; }> {
58
59
  if (fieldNameRateLimit) {
59
60
  // rate limit
60
61
  // const { error } = RateLimiter.checkRateLimit(
@@ -62,8 +63,10 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
62
63
  // fieldNameRateLimit,
63
64
  // this.adminforth.auth.getClientIp(headers),
64
65
  // );
65
- const rateLimiter = new RateLimiter(fieldNameRateLimit);
66
- if (!rateLimiter.consume(`${field}-${this.adminforth.auth.getClientIp(headers)}`)) {
66
+ if (!this.rateLimiters[field]) {
67
+ this.rateLimiters[field] = new RateLimiter(fieldNameRateLimit);
68
+ }
69
+ if (!await this.rateLimiters[field].consume(`${field}-${this.adminforth.auth.getClientIp(headers)}`)) {
67
70
  return { error: "Rate limit exceeded" };
68
71
  }
69
72
  }
@@ -271,7 +274,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
271
274
  private async regenerateImage(jobId: string, recordId: string, fieldName: string, prompt: string, adminUser: any, headers: Record<string, string | string[] | undefined>) {
272
275
  const Id = recordId;
273
276
  let isError = false;
274
- if (this.checkRateLimit(fieldName, this.options.generateImages[fieldName].rateLimit, headers)) {
277
+ if (await this.checkRateLimit(fieldName, this.options.generateImages[fieldName].rateLimit, headers)) {
275
278
  jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
276
279
  return { error: "Rate limit exceeded" };
277
280
  }
@@ -739,17 +742,17 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
739
742
  handler: async ({ body, adminUser, headers }) => {
740
743
  const actionType = body.actionType;
741
744
  if (actionType === 'analyze' && this.options.rateLimits?.fillFieldsFromImages) {
742
- if (this.checkRateLimit("fillFieldsFromImages" ,this.options.rateLimits.fillFieldsFromImages, headers)) {
745
+ if (await this.checkRateLimit("fillFieldsFromImages" ,this.options.rateLimits.fillFieldsFromImages, headers)) {
743
746
  return {ok: false, error: "Rate limit exceeded for image analyze" };
744
747
  }
745
748
  }
746
749
  if (actionType === 'analyze_no_images' && this.options.rateLimits?.fillPlainFields) {
747
- if (this.checkRateLimit("fillPlainFields" ,this.options.rateLimits.fillPlainFields, headers)) {
750
+ if (await this.checkRateLimit("fillPlainFields" ,this.options.rateLimits.fillPlainFields, headers)) {
748
751
  return {ok: false, error: "Rate limit exceeded for plain field analyze" };
749
752
  }
750
753
  }
751
754
  if (actionType === 'generate_images' && this.options.rateLimits?.generateImages) {
752
- if (this.checkRateLimit("generateImages" ,this.options.rateLimits.generateImages, headers)) {
755
+ if (await this.checkRateLimit("generateImages" ,this.options.rateLimits.generateImages, headers)) {
753
756
  return {ok: false, error: "Rate limit exceeded for image generation" };
754
757
  }
755
758
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/bulk-ai-flow",
3
- "version": "1.14.4",
3
+ "version": "1.14.5",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },