UNPKG

anylang

Version:

A translator's kit that uses the free APIs of Google Translate, Yandex, Bing, ChatGPT, and other LLMs

78 lines (77 loc) 2.95 kB
import { TranslatorInstanceMembers } from '../translators/Translator'; import { IScheduler, ISchedulerTranslateOptions } from '.'; interface SchedulerConfig { /** * Number of attempts for retry request */ translateRetryAttemptLimit?: number; /** * If true - rejected requests will use direct translate */ isAllowDirectTranslateBadChunks?: boolean; /** * Length of string for direct translate. * * null for disable the condition */ directTranslateLength?: number | null; /** * Delay for translate a chunk. The bigger the more requests will collect */ translatePoolDelay?: number; /** * When chunk collect this size, it's will be instant add to a translate queue * * null for disable the condition */ chunkSizeForInstantTranslate?: number | null; /** * Pause between handle task batches * * It may be useful to await accumulating a task batches in queue to consider priority better and don't translate first task batch immediately * * WARNING: this option must be used only for consider priority better! Set small value always (10-50ms) * * When this option is disabled (by default) and you call translate method for texts with priority 1 and then immediately for text with priority 2, first request will have less delay for translate and will translate first, even with lower priority, because worker will translate first task immediately after delay defined by option `translatePoolDelay` */ taskBatchHandleDelay?: null | number; } /** * Module for scheduling and optimization of translate a text streams * * - It can union many translate requests to one * - You can group any requests by context * - It's configurable. You can set retry limit and edge for direct translate */ export declare class Scheduler implements IScheduler { private readonly semafor; private readonly translator; private readonly config; constructor(translator: TranslatorInstanceMembers, config?: SchedulerConfig); private readonly abortedContexts; abort(context: string): Promise<void>; private contextCounter; translate(text: string, from: string, to: string, options?: ISchedulerTranslateOptions): Promise<string>; private directTranslate; private splitAndTranslate; private makeTask; private readonly taskContainersStorage; private addToTaskContainer; private readonly timersMap; private updateDelayForAddToTranslateQueue; /** * Tasks queue with items sorted by priority * It must be handled from end to start */ private translateQueue; private addToTranslateQueue; /** * Return first item from queue and delete it from queue * Items is sorted by priority */ private readonly getItemFromTranslateQueue; private workerState; private runWorker; private taskErrorHandler; } export {};