@adminforth/rich-editor 1.0.11 → 1.0.12

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.
package/ChangeLog.md CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
2
 
3
- ## [1.0.15]
4
-
5
- -- Add image uploads support
3
+ ## [1.0.12] - 2021-10-07
4
+ ### Added
5
+ - Rate limiting for openai requests support
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { AdminForthPlugin, Filters } from "adminforth";
10
+ import { AdminForthPlugin, Filters, getClinetIp, RateLimiter } from "adminforth";
11
11
  import * as cheerio from 'cheerio';
12
12
  // options:
13
13
  // attachments: {
@@ -203,14 +203,23 @@ export default class RichEditorPlugin extends AdminForthPlugin {
203
203
  server.endpoint({
204
204
  method: 'POST',
205
205
  path: `/plugin/${this.pluginInstanceId}/doComplete`,
206
- handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body }) {
207
- var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
206
+ handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, headers }) {
207
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
208
208
  const { record } = body;
209
+ if ((_b = this.options.completion.rateLimit) === null || _b === void 0 ? void 0 : _b.limit) {
210
+ // rate limit
211
+ const { error } = RateLimiter.checkRateLimit(this.pluginInstanceId, (_c = this.options.completion.rateLimit) === null || _c === void 0 ? void 0 : _c.limit, getClinetIp(headers));
212
+ if (error) {
213
+ return {
214
+ completion: [],
215
+ };
216
+ }
217
+ }
209
218
  const recordNoField = Object.assign({}, record);
210
219
  delete recordNoField[this.options.htmlFieldName];
211
220
  let currentVal = record[this.options.htmlFieldName];
212
- const promptLimit = ((_b = this.options.completion.expert) === null || _b === void 0 ? void 0 : _b.promptInputLimit) || 500;
213
- const inputContext = this.generateRecordContext(recordNoField, ((_d = (_c = this.options.completion.expert) === null || _c === void 0 ? void 0 : _c.recordContext) === null || _d === void 0 ? void 0 : _d.maxFields) || 5, ((_f = (_e = this.options.completion.expert) === null || _e === void 0 ? void 0 : _e.recordContext) === null || _f === void 0 ? void 0 : _f.maxFieldLength) || 300, ((_h = (_g = this.options.completion.expert) === null || _g === void 0 ? void 0 : _g.recordContext) === null || _h === void 0 ? void 0 : _h.splitParts) || 5);
221
+ const promptLimit = ((_d = this.options.completion.expert) === null || _d === void 0 ? void 0 : _d.promptInputLimit) || 500;
222
+ const inputContext = this.generateRecordContext(recordNoField, ((_f = (_e = this.options.completion.expert) === null || _e === void 0 ? void 0 : _e.recordContext) === null || _f === void 0 ? void 0 : _f.maxFields) || 5, ((_h = (_g = this.options.completion.expert) === null || _g === void 0 ? void 0 : _g.recordContext) === null || _h === void 0 ? void 0 : _h.maxFieldLength) || 300, ((_k = (_j = this.options.completion.expert) === null || _j === void 0 ? void 0 : _j.recordContext) === null || _k === void 0 ? void 0 : _k.splitParts) || 5);
214
223
  if (currentVal && currentVal.length > promptLimit) {
215
224
  currentVal = currentVal.slice(-promptLimit);
216
225
  }
@@ -228,7 +237,7 @@ export default class RichEditorPlugin extends AdminForthPlugin {
228
237
  "Be short, clear and precise. No quotes. Don't talk to me. Just write text\n";
229
238
  }
230
239
  process.env.HEAVY_DEBUG && console.log('🪲 OpenAI Prompt 🧠', content);
231
- const stop = ((_j = this.options.completion.expert) === null || _j === void 0 ? void 0 : _j.stop) || ['.'];
240
+ const stop = ((_l = this.options.completion.expert) === null || _l === void 0 ? void 0 : _l.stop) || ['.'];
232
241
  const resp = yield fetch('https://api.openai.com/v1/chat/completions', {
233
242
  method: 'POST',
234
243
  headers: {
@@ -243,8 +252,8 @@ export default class RichEditorPlugin extends AdminForthPlugin {
243
252
  content,
244
253
  }
245
254
  ],
246
- temperature: ((_k = this.options.completion.expert) === null || _k === void 0 ? void 0 : _k.temperature) || 0.7,
247
- max_tokens: ((_l = this.options.completion.expert) === null || _l === void 0 ? void 0 : _l.maxTokens) || 50,
255
+ temperature: ((_m = this.options.completion.expert) === null || _m === void 0 ? void 0 : _m.temperature) || 0.7,
256
+ max_tokens: ((_o = this.options.completion.expert) === null || _o === void 0 ? void 0 : _o.maxTokens) || 50,
248
257
  stop,
249
258
  })
250
259
  });
package/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  import type { IAdminForth, IHttpServer, AdminForthResource, AdminUser, AfterSaveFunction } from "adminforth";
3
3
  import type { PluginOptions } from './types.js';
4
- import { AdminForthPlugin, Filters } from "adminforth";
4
+ import { AdminForthPlugin, Filters, getClinetIp, RateLimiter } from "adminforth";
5
5
  import * as cheerio from 'cheerio';
6
6
 
7
7
 
@@ -257,8 +257,23 @@ export default class RichEditorPlugin extends AdminForthPlugin {
257
257
  server.endpoint({
258
258
  method: 'POST',
259
259
  path: `/plugin/${this.pluginInstanceId}/doComplete`,
260
- handler: async ({ body }) => {
260
+ handler: async ({ body, headers }) => {
261
261
  const { record } = body;
262
+
263
+ if (this.options.completion.rateLimit?.limit) {
264
+ // rate limit
265
+ const { error } = RateLimiter.checkRateLimit(
266
+ this.pluginInstanceId,
267
+ this.options.completion.rateLimit?.limit,
268
+ getClinetIp(headers),
269
+ );
270
+ if (error) {
271
+ return {
272
+ completion: [],
273
+ }
274
+ }
275
+ }
276
+
262
277
  const recordNoField = {...record};
263
278
  delete recordNoField[this.options.htmlFieldName];
264
279
  let currentVal = record[this.options.htmlFieldName] as string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/rich-editor",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/types.ts CHANGED
@@ -130,7 +130,25 @@ export interface PluginOptions {
130
130
  splitParts?: number;
131
131
 
132
132
  }
133
- }
133
+ }
134
+
135
+ /**
136
+ * Since AI generation can be expensive, we can limit the number of requests per IP.
137
+ * Completion will simply stop working when limit is reached so user will not be bothered with error messages.
138
+ */
139
+ rateLimit?: {
140
+
141
+ /**
142
+ * E.g. 5/1d - 5 requests per day
143
+ * 3/1h - 3 requests per hour
144
+ */
145
+ limit: string,
146
+
147
+ /**
148
+ * Message shown to user when rate limit is reached
149
+ */
150
+ errorMessage: string,
151
+ },
134
152
 
135
153
  }
136
154