Skip to content

Commit d72e352

Browse files
committed
fix: fix rate limits
1 parent 04c32b2 commit d72e352

File tree

2 files changed

+78
-20
lines changed

2 files changed

+78
-20
lines changed

custom/VisionAction.vue

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,35 @@ async function runAiAction({
406406
let errorMessage = '';
407407
const jobsIds: { jobId: any; recordId: any; }[] = [];
408408
responseFlag.value = props.checkboxes.map(() => false);
409-
409+
let isRateLimitExceeded = false;
410+
try {
411+
const rateLimitRes = await callAdminForthApi({
412+
path: `/plugin/${props.meta.pluginInstanceId}/update-rate-limits`,
413+
method: 'POST',
414+
body: {
415+
actionType: actionType,
416+
},
417+
});
418+
if (rateLimitRes?.error) {
419+
isRateLimitExceeded = true;
420+
adminforth.alert({
421+
message: `Rate limit exceeded for "${actionType.replace('_', ' ')}" action. Please try again later.`,
422+
variant: 'danger',
423+
timeout: 'unlimited',
424+
});
425+
return;
426+
}
427+
} catch (e) {
428+
adminforth.alert({
429+
message: `Error checking rate limit for "${actionType.replace('_', ' ')}" action.`,
430+
variant: 'danger',
431+
timeout: 'unlimited',
432+
});
433+
isRateLimitExceeded = true;
434+
}
435+
if (isRateLimitExceeded) {
436+
return;
437+
};
410438
//creating jobs
411439
const tasks = props.checkboxes.map(async (checkbox, i) => {
412440
try {

index.ts

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
7070
private async analyze_image(jobId: string, recordId: string, adminUser: any, headers: Record<string, string | string[] | undefined>) {
7171
const selectedId = recordId;
7272
let isError = false;
73-
if (typeof(this.options.rateLimits?.fillFieldsFromImages) === 'string'){
74-
if (this.checkRateLimit("fillFieldsFromImages" ,this.options.rateLimits.fillFieldsFromImages, headers)) {
75-
jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
76-
return { error: "Rate limit exceeded" };
77-
}
78-
}
73+
// if (typeof(this.options.rateLimits?.fillFieldsFromImages) === 'string'){
74+
// if (this.checkRateLimit("fillFieldsFromImages" ,this.options.rateLimits.fillFieldsFromImages, headers)) {
75+
// jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
76+
// return { error: "Rate limit exceeded" };
77+
// }
78+
// }
7979
// Fetch the record using the provided ID
8080
const primaryKeyColumn = this.resourceConfig.columns.find((col) => col.primaryKey);
8181
const record = await this.adminforth.resource(this.resourceConfig.resourceId).get([Filters.EQ(primaryKeyColumn.name, selectedId)] );
@@ -121,18 +121,22 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
121121
jobs.set(jobId, { status: 'completed', result });
122122
return { ok: true };
123123
}
124-
};
124+
} else {
125+
jobs.set(jobId, { status: 'failed', error: "No attachment files found" });
126+
return { ok: false, error: "No attachment files found" };
127+
}
128+
125129
}
126130

127131
private async analyzeNoImages(jobId: string, recordId: string, adminUser: any, headers: Record<string, string | string[] | undefined>) {
128132
const selectedId = recordId;
129133
let isError = false;
130-
if (typeof(this.options.rateLimits?.fillPlainFields) === 'string'){
131-
if (this.checkRateLimit("fillPlainFields", this.options.rateLimits.fillPlainFields, headers)) {
132-
jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
133-
return { error: "Rate limit exceeded" };
134-
}
135-
}
134+
// if (typeof(this.options.rateLimits?.fillPlainFields) === 'string'){
135+
// if (this.checkRateLimit("fillPlainFields", this.options.rateLimits.fillPlainFields, headers)) {
136+
// jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
137+
// return { error: "Rate limit exceeded" };
138+
// }
139+
// }
136140
if (STUB_MODE) {
137141
await new Promise((resolve) => setTimeout(resolve, Math.floor(Math.random() * 20000) + 1000));
138142
jobs.set(jobId, { status: 'completed', result: {} });
@@ -173,12 +177,12 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
173177
private async initialImageGenerate(jobId: string, recordId: string, adminUser: any, headers: Record<string, string | string[] | undefined>) {
174178
const selectedId = recordId;
175179
let isError = false;
176-
if (typeof(this.options.rateLimits?.generateImages) === 'string'){
177-
if (this.checkRateLimit("generateImages", this.options.rateLimits.generateImages, headers)) {
178-
jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
179-
return { error: "Rate limit exceeded" };
180-
}
181-
}
180+
// if (typeof(this.options.rateLimits?.generateImages) === 'string'){
181+
// if (this.checkRateLimit("generateImages", this.options.rateLimits.generateImages, headers)) {
182+
// jobs.set(jobId, { status: 'failed', error: "Rate limit exceeded" });
183+
// return { error: "Rate limit exceeded" };
184+
// }
185+
// }
182186
const start = +new Date();
183187
const record = await this.adminforth.resource(this.resourceConfig.resourceId).get([Filters.EQ(this.resourceConfig.columns.find(c => c.primaryKey)?.name, selectedId)]);
184188
let attachmentFiles
@@ -679,5 +683,31 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
679683
});
680684

681685

686+
server.endpoint({
687+
method: 'POST',
688+
path: `/plugin/${this.pluginInstanceId}/update-rate-limits`,
689+
handler: async ({ body, adminUser, headers }) => {
690+
const actionType = body.actionType;
691+
if (actionType === 'analyze' && this.options.rateLimits?.fillFieldsFromImages) {
692+
if (this.checkRateLimit("fillFieldsFromImages" ,this.options.rateLimits.fillFieldsFromImages, headers)) {
693+
return {ok: false, error: "Rate limit exceeded for image analyze" };
694+
}
695+
}
696+
if (actionType === 'analyze_no_images' && this.options.rateLimits?.fillPlainFields) {
697+
if (this.checkRateLimit("fillPlainFields" ,this.options.rateLimits.fillPlainFields, headers)) {
698+
return {ok: false, error: "Rate limit exceeded for plain field analyze" };
699+
}
700+
}
701+
if (actionType === 'generate_images' && this.options.rateLimits?.generateImages) {
702+
if (this.checkRateLimit("generateImages" ,this.options.rateLimits.generateImages, headers)) {
703+
return {ok: false, error: "Rate limit exceeded for image generation" };
704+
}
705+
}
706+
707+
return { ok: true };
708+
}
709+
});
710+
711+
682712
}
683713
}

0 commit comments

Comments
 (0)