|
| 1 | +import { Lang } from "@/types/client"; |
| 2 | + |
| 3 | +import { |
| 4 | + TranslateDetailSuccessResponse, |
| 5 | + TranslateOptions, |
| 6 | +} from "@/types/providers/yandextranslate"; |
| 7 | +import { TranslateError } from "@/errors"; |
| 8 | +import YandexTranslateProvider from "./yandextranslate"; |
| 9 | + |
| 10 | +/** |
| 11 | + * The total limit of characters per request is 10k chars |
| 12 | + */ |
| 13 | +export default class YandexGPTProvider extends YandexTranslateProvider { |
| 14 | + YAGPT_ORIGIN = "https://neuro.translate.yandex.ru"; |
| 15 | + |
| 16 | + getSid() { |
| 17 | + const timestamp = Date.now().toString(16); |
| 18 | + |
| 19 | + return ( |
| 20 | + timestamp + |
| 21 | + Array.from({ length: 16 - timestamp.length }, () => |
| 22 | + Math.floor(Math.random() * 16).toString(16), |
| 23 | + ).join("") |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + isSupportedLLMOptions(options: TranslateOptions) { |
| 28 | + return ![ |
| 29 | + TranslateOptions.detailAlign, |
| 30 | + TranslateOptions.detailAlignAndDetectedLang, |
| 31 | + ].includes(options); |
| 32 | + } |
| 33 | + |
| 34 | + async llmRawRequest( |
| 35 | + text: string | string[], |
| 36 | + lang: Lang = "en-ru", |
| 37 | + options: TranslateOptions = TranslateOptions.default, |
| 38 | + format: "text" | "html" = "text", |
| 39 | + ) { |
| 40 | + if (lang !== "en-ru") { |
| 41 | + throw new TranslateError("LLM service support only en-ru lang"); |
| 42 | + } |
| 43 | + |
| 44 | + if (!this.isSupportedLLMOptions(options)) { |
| 45 | + throw new TranslateError( |
| 46 | + "Detail align options not supported by LLM service", |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + if (!Array.isArray(text)) { |
| 51 | + text = [text]; |
| 52 | + } |
| 53 | + |
| 54 | + const sid = this.getSid(); |
| 55 | + const params = new URLSearchParams({ |
| 56 | + lang, |
| 57 | + id: `${sid}-0-0`, |
| 58 | + srv: "yabrowser-tr-text-app", |
| 59 | + tr_model: "llm_text", |
| 60 | + format, |
| 61 | + }).toString(); |
| 62 | + |
| 63 | + const textArray: readonly [string, string][] = text.map((val) => [ |
| 64 | + "text", |
| 65 | + val, |
| 66 | + ]); |
| 67 | + const body = new URLSearchParams([ |
| 68 | + ["options", options.toString()], |
| 69 | + ["lang", lang], |
| 70 | + ...textArray, |
| 71 | + ]); |
| 72 | + |
| 73 | + const res = await this.request<TranslateDetailSuccessResponse>( |
| 74 | + `/translate?${params}`, |
| 75 | + body, |
| 76 | + { |
| 77 | + Origin: this.YAGPT_ORIGIN, |
| 78 | + Referer: `${this.YAGPT_ORIGIN}/`, |
| 79 | + }, |
| 80 | + ); |
| 81 | + |
| 82 | + if (!this.isSuccessProviderRes<TranslateDetailSuccessResponse>(res)) { |
| 83 | + throw new TranslateError(res.data); |
| 84 | + } |
| 85 | + |
| 86 | + const { lang: resLang, text: resText = [""], align, detected } = res.data; |
| 87 | + |
| 88 | + return { |
| 89 | + lang: resLang, |
| 90 | + translations: resText, |
| 91 | + align, |
| 92 | + detected, |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * You can use this method if you need also get a detected language or align |
| 98 | + */ |
| 99 | + async rawTranslate( |
| 100 | + text: string | string[], |
| 101 | + lang: Lang = "en-ru", |
| 102 | + options: TranslateOptions = TranslateOptions.default, |
| 103 | + format: "text" | "html" = "text", |
| 104 | + ) { |
| 105 | + if (lang === "en-ru" && this.isSupportedLLMOptions(options)) { |
| 106 | + return await this.llmRawRequest(text, lang, options, format); |
| 107 | + } |
| 108 | + |
| 109 | + return await super.rawTranslate(text, lang, options, format); |
| 110 | + } |
| 111 | +} |
0 commit comments