mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Rate-limit Poe message requests
This commit is contained in:
@ -14,7 +14,7 @@ import {
|
|||||||
secret_state,
|
secret_state,
|
||||||
writeSecret,
|
writeSecret,
|
||||||
} from "./secrets.js";
|
} from "./secrets.js";
|
||||||
import { delay, splitRecursive } from "./utils.js";
|
import { RateLimiter, delay, splitRecursive } from "./utils.js";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
is_get_status_poe,
|
is_get_status_poe,
|
||||||
@ -66,6 +66,8 @@ let is_get_status_poe = false;
|
|||||||
let is_poe_button_press = false;
|
let is_poe_button_press = false;
|
||||||
let abortControllerSuggest = null;
|
let abortControllerSuggest = null;
|
||||||
|
|
||||||
|
const rateLimiter = new RateLimiter((60 / 10 * 1000)); // 10 requests per minute
|
||||||
|
|
||||||
function loadPoeSettings(settings) {
|
function loadPoeSettings(settings) {
|
||||||
if (settings.poe_settings) {
|
if (settings.poe_settings) {
|
||||||
Object.assign(poe_settings, settings.poe_settings);
|
Object.assign(poe_settings, settings.poe_settings);
|
||||||
@ -334,6 +336,8 @@ async function sendMessage(prompt, withStreaming, withSuggestions, signal) {
|
|||||||
signal = new AbortController().signal;
|
signal = new AbortController().signal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await rateLimiter.waitForResolve(signal);
|
||||||
|
|
||||||
const body = JSON.stringify({
|
const body = JSON.stringify({
|
||||||
bot: poe_settings.bot,
|
bot: poe_settings.bot,
|
||||||
streaming: withStreaming && poe_settings.streaming,
|
streaming: withStreaming && poe_settings.streaming,
|
||||||
|
@ -435,3 +435,39 @@ export function getCharaFilename() {
|
|||||||
export function escapeRegex(string) {
|
export function escapeRegex(string) {
|
||||||
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
|
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class RateLimiter {
|
||||||
|
constructor(intervalMillis) {
|
||||||
|
this._intervalMillis = intervalMillis;
|
||||||
|
this._lastResolveTime = 0;
|
||||||
|
this._pendingResolve = Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
_waitRemainingTime(abortSignal) {
|
||||||
|
const currentTime = Date.now();
|
||||||
|
const elapsedTime = currentTime - this._lastResolveTime;
|
||||||
|
const remainingTime = Math.max(0, this._intervalMillis - elapsedTime);
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const timeoutId = setTimeout(() => {
|
||||||
|
resolve();
|
||||||
|
}, remainingTime);
|
||||||
|
|
||||||
|
if (abortSignal) {
|
||||||
|
abortSignal.addEventListener('abort', () => {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
reject(new Error('Aborted'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async waitForResolve(abortSignal) {
|
||||||
|
await this._pendingResolve;
|
||||||
|
this._pendingResolve = this._waitRemainingTime(abortSignal);
|
||||||
|
|
||||||
|
// Update the last resolve time
|
||||||
|
this._lastResolveTime = Date.now() + this._intervalMillis;
|
||||||
|
console.debug(`RateLimiter.waitForResolve() ${this._lastResolveTime}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user