mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-08 04:54:32 +00:00
cache and re-use note translations
This commit is contained in:
parent
7db48ffa8d
commit
871a4d3fb1
2 changed files with 49 additions and 19 deletions
|
@ -10,22 +10,28 @@ import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
||||||
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
||||||
import { GetterService } from '@/server/api/GetterService.js';
|
import { GetterService } from '@/server/api/GetterService.js';
|
||||||
import { RoleService } from '@/core/RoleService.js';
|
import { RoleService } from '@/core/RoleService.js';
|
||||||
import { ApiError } from '../../error.js';
|
import type { MiMeta, MiNote } from '@/models/_.js';
|
||||||
import { MiMeta } from '@/models/_.js';
|
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
|
import { CacheService } from '@/core/CacheService.js';
|
||||||
|
import { hasText } from '@/models/Note.js';
|
||||||
|
import { ApiLoggerService } from '@/server/api/ApiLoggerService.js';
|
||||||
|
import { ApiError } from '../../error.js';
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ['notes'],
|
tags: ['notes'],
|
||||||
|
|
||||||
|
// TODO allow unauthenticated if default template allows?
|
||||||
|
// Maybe a value 'optional' that allows unauthenticated OR a token w/ appropriate role.
|
||||||
|
// This will allow unauthenticated requests without leaking post data to restricted clients.
|
||||||
requireCredential: true,
|
requireCredential: true,
|
||||||
kind: 'read:account',
|
kind: 'read:account',
|
||||||
|
|
||||||
res: {
|
res: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
optional: true, nullable: false,
|
optional: false, nullable: false,
|
||||||
properties: {
|
properties: {
|
||||||
sourceLang: { type: 'string' },
|
sourceLang: { type: 'string', optional: true, nullable: false },
|
||||||
text: { type: 'string' },
|
text: { type: 'string', optional: true, nullable: false },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -45,6 +51,11 @@ export const meta = {
|
||||||
code: 'CANNOT_TRANSLATE_INVISIBLE_NOTE',
|
code: 'CANNOT_TRANSLATE_INVISIBLE_NOTE',
|
||||||
id: 'ea29f2ca-c368-43b3-aaf1-5ac3e74bbe5d',
|
id: 'ea29f2ca-c368-43b3-aaf1-5ac3e74bbe5d',
|
||||||
},
|
},
|
||||||
|
translationFailed: {
|
||||||
|
message: 'Failed to translate note. Please try again later or contact an administrator for assistance.',
|
||||||
|
code: 'TRANSLATION_FAILED',
|
||||||
|
id: '4e7a1a4f-521c-4ba2-b10a-69e5e2987b2f',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
// 10 calls per 5 seconds
|
// 10 calls per 5 seconds
|
||||||
|
@ -73,6 +84,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
private getterService: GetterService,
|
private getterService: GetterService,
|
||||||
private httpRequestService: HttpRequestService,
|
private httpRequestService: HttpRequestService,
|
||||||
private roleService: RoleService,
|
private roleService: RoleService,
|
||||||
|
private readonly cacheService: CacheService,
|
||||||
|
private readonly loggerService: ApiLoggerService,
|
||||||
) {
|
) {
|
||||||
super(meta, paramDef, async (ps, me) => {
|
super(meta, paramDef, async (ps, me) => {
|
||||||
const policies = await this.roleService.getUserPolicies(me.id);
|
const policies = await this.roleService.getUserPolicies(me.id);
|
||||||
|
@ -89,8 +102,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
throw new ApiError(meta.errors.cannotTranslateInvisibleNote);
|
throw new ApiError(meta.errors.cannotTranslateInvisibleNote);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (note.text == null) {
|
if (!hasText(note)) {
|
||||||
return;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const canDeeplFree = this.serverSettings.deeplFreeMode && !!this.serverSettings.deeplFreeInstance;
|
const canDeeplFree = this.serverSettings.deeplFreeMode && !!this.serverSettings.deeplFreeInstance;
|
||||||
|
@ -101,13 +114,32 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
let targetLang = ps.targetLang;
|
let targetLang = ps.targetLang;
|
||||||
if (targetLang.includes('-')) targetLang = targetLang.split('-')[0];
|
if (targetLang.includes('-')) targetLang = targetLang.split('-')[0];
|
||||||
|
|
||||||
|
let response = await this.cacheService.getCachedTranslation(note, targetLang);
|
||||||
|
if (!response) {
|
||||||
|
response = await this.fetchTranslation(note, targetLang);
|
||||||
|
if (!response) {
|
||||||
|
throw new ApiError(meta.errors.translationFailed);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.cacheService.setCachedTranslation(note, targetLang, response);
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async fetchTranslation(note: MiNote & { text: string }, targetLang: string) {
|
||||||
|
// Load-bearing try/catch - removing this will shift indentation and cause ~80 lines of upstream merge conflicts
|
||||||
|
try {
|
||||||
|
// Ignore deeplFreeInstance unless deeplFreeMode is set
|
||||||
|
const deeplFreeInstance = this.serverSettings.deeplFreeMode ? this.serverSettings.deeplFreeInstance : null;
|
||||||
|
|
||||||
// DeepL/DeepLX handling
|
// DeepL/DeepLX handling
|
||||||
if (canDeepl) {
|
if (this.serverSettings.deeplAuthKey || deeplFreeInstance) {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (this.serverSettings.deeplAuthKey) params.append('auth_key', this.serverSettings.deeplAuthKey);
|
if (this.serverSettings.deeplAuthKey) params.append('auth_key', this.serverSettings.deeplAuthKey);
|
||||||
params.append('text', note.text);
|
params.append('text', note.text);
|
||||||
params.append('target_lang', targetLang);
|
params.append('target_lang', targetLang);
|
||||||
const endpoint = canDeeplFree ? this.serverSettings.deeplFreeInstance as string : this.serverSettings.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
|
const endpoint = deeplFreeInstance ?? this.serverSettings.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
|
||||||
|
|
||||||
const res = await this.httpRequestService.send(endpoint, {
|
const res = await this.httpRequestService.send(endpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -151,8 +183,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
}
|
}
|
||||||
|
|
||||||
// LibreTranslate handling
|
// LibreTranslate handling
|
||||||
if (canLibre) {
|
if (this.serverSettings.libreTranslateURL) {
|
||||||
const res = await this.httpRequestService.send(this.serverSettings.libreTranslateURL as string, {
|
const res = await this.httpRequestService.send(this.serverSettings.libreTranslateURL, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
@ -182,8 +214,10 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
text: json.translatedText,
|
text: json.translatedText,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
this.loggerService.logger.error('Unhandled error from translation API: ', { e });
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return null;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28709,15 +28709,11 @@ export type operations = {
|
||||||
200: {
|
200: {
|
||||||
content: {
|
content: {
|
||||||
'application/json': {
|
'application/json': {
|
||||||
sourceLang: string;
|
sourceLang?: string;
|
||||||
text: string;
|
text?: string;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
/** @description OK (without any results) */
|
|
||||||
204: {
|
|
||||||
content: never;
|
|
||||||
};
|
|
||||||
/** @description Client error */
|
/** @description Client error */
|
||||||
400: {
|
400: {
|
||||||
content: {
|
content: {
|
||||||
|
|
Loading…
Add table
Reference in a new issue