@adminforth/bulk-ai-flow 1.14.3 → 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 -10
  2. package/index.ts +15 -11
  3. package/package.json +2 -2
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,13 +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(field, fieldNameRateLimit, this.adminforth.auth.getClientIp(headers));
53
- if (error) {
54
- 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
+ }
55
65
  }
56
- }
66
+ });
57
67
  }
58
68
  analyze_image(jobId, recordId, adminUser, headers) {
59
69
  return __awaiter(this, void 0, void 0, function* () {
@@ -269,7 +279,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
269
279
  var _a;
270
280
  const Id = recordId;
271
281
  let isError = false;
272
- if (this.checkRateLimit(fieldName, this.options.generateImages[fieldName].rateLimit, headers)) {
282
+ if (yield this.checkRateLimit(fieldName, this.options.generateImages[fieldName].rateLimit, headers)) {
273
283
  jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
274
284
  return { error: "Rate limit exceeded" };
275
285
  }
@@ -702,17 +712,17 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
702
712
  var _b, _c, _d;
703
713
  const actionType = body.actionType;
704
714
  if (actionType === 'analyze' && ((_b = this.options.rateLimits) === null || _b === void 0 ? void 0 : _b.fillFieldsFromImages)) {
705
- if (this.checkRateLimit("fillFieldsFromImages", this.options.rateLimits.fillFieldsFromImages, headers)) {
715
+ if (yield this.checkRateLimit("fillFieldsFromImages", this.options.rateLimits.fillFieldsFromImages, headers)) {
706
716
  return { ok: false, error: "Rate limit exceeded for image analyze" };
707
717
  }
708
718
  }
709
719
  if (actionType === 'analyze_no_images' && ((_c = this.options.rateLimits) === null || _c === void 0 ? void 0 : _c.fillPlainFields)) {
710
- if (this.checkRateLimit("fillPlainFields", this.options.rateLimits.fillPlainFields, headers)) {
720
+ if (yield this.checkRateLimit("fillPlainFields", this.options.rateLimits.fillPlainFields, headers)) {
711
721
  return { ok: false, error: "Rate limit exceeded for plain field analyze" };
712
722
  }
713
723
  }
714
724
  if (actionType === 'generate_images' && ((_d = this.options.rateLimits) === null || _d === void 0 ? void 0 : _d.generateImages)) {
715
- if (this.checkRateLimit("generateImages", this.options.rateLimits.generateImages, headers)) {
725
+ if (yield this.checkRateLimit("generateImages", this.options.rateLimits.generateImages, headers)) {
716
726
  return { ok: false, error: "Rate limit exceeded for image generation" };
717
727
  }
718
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,15 +55,18 @@ 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
- const { error } = RateLimiter.checkRateLimit(
61
- field,
62
- fieldNameRateLimit,
63
- this.adminforth.auth.getClientIp(headers),
64
- );
65
- if (error) {
61
+ // const { error } = RateLimiter.checkRateLimit(
62
+ // field,
63
+ // fieldNameRateLimit,
64
+ // this.adminforth.auth.getClientIp(headers),
65
+ // );
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)}`)) {
66
70
  return { error: "Rate limit exceeded" };
67
71
  }
68
72
  }
@@ -270,7 +274,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
270
274
  private async regenerateImage(jobId: string, recordId: string, fieldName: string, prompt: string, adminUser: any, headers: Record<string, string | string[] | undefined>) {
271
275
  const Id = recordId;
272
276
  let isError = false;
273
- if (this.checkRateLimit(fieldName, this.options.generateImages[fieldName].rateLimit, headers)) {
277
+ if (await this.checkRateLimit(fieldName, this.options.generateImages[fieldName].rateLimit, headers)) {
274
278
  jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
275
279
  return { error: "Rate limit exceeded" };
276
280
  }
@@ -738,17 +742,17 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
738
742
  handler: async ({ body, adminUser, headers }) => {
739
743
  const actionType = body.actionType;
740
744
  if (actionType === 'analyze' && this.options.rateLimits?.fillFieldsFromImages) {
741
- if (this.checkRateLimit("fillFieldsFromImages" ,this.options.rateLimits.fillFieldsFromImages, headers)) {
745
+ if (await this.checkRateLimit("fillFieldsFromImages" ,this.options.rateLimits.fillFieldsFromImages, headers)) {
742
746
  return {ok: false, error: "Rate limit exceeded for image analyze" };
743
747
  }
744
748
  }
745
749
  if (actionType === 'analyze_no_images' && this.options.rateLimits?.fillPlainFields) {
746
- if (this.checkRateLimit("fillPlainFields" ,this.options.rateLimits.fillPlainFields, headers)) {
750
+ if (await this.checkRateLimit("fillPlainFields" ,this.options.rateLimits.fillPlainFields, headers)) {
747
751
  return {ok: false, error: "Rate limit exceeded for plain field analyze" };
748
752
  }
749
753
  }
750
754
  if (actionType === 'generate_images' && this.options.rateLimits?.generateImages) {
751
- if (this.checkRateLimit("generateImages" ,this.options.rateLimits.generateImages, headers)) {
755
+ if (await this.checkRateLimit("generateImages" ,this.options.rateLimits.generateImages, headers)) {
752
756
  return {ok: false, error: "Rate limit exceeded for image generation" };
753
757
  }
754
758
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/bulk-ai-flow",
3
- "version": "1.14.3",
3
+ "version": "1.14.5",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@types/handlebars": "^4.0.40",
30
- "adminforth": "^2.4.0-next.127",
30
+ "adminforth": "^2.4.0-next.222",
31
31
  "handlebars": "^4.7.8"
32
32
  },
33
33
  "release": {