Compare commits

..

9 Commits

Author SHA1 Message Date
eea222144b merge upstream 2025-07-18 07:27:25 +02:00
github-actions[bot]
73419e8a61 [skip ci] Update CHANGELOG.md (prepend template) 2025-07-18 00:28:02 +00:00
syuilo
ab602f25aa New translations ja-jp.yml (Spanish) 2025-07-13 04:29:17 +09:00
syuilo
04e4f633f2 New translations ja-jp.yml (Spanish) 2025-07-13 03:33:32 +09:00
syuilo
90fc17b5ab New translations ja-jp.yml (Thai) 2025-07-12 21:23:33 +09:00
syuilo
cbf8669d12 New translations ja-jp.yml (Spanish) 2025-07-12 21:23:32 +09:00
syuilo
6df370d14f New translations ja-jp.yml (Korean) 2025-07-12 17:34:30 +09:00
syuilo
77986d39fc New translations ja-jp.yml (Chinese Traditional) 2025-07-12 16:33:11 +09:00
syuilo
7a36d9e189 New translations ja-jp.yml (Chinese Simplified) 2025-07-12 16:33:10 +09:00
2 changed files with 28 additions and 58 deletions

View File

@@ -1,3 +1,15 @@
## Unreleased
### General
-
### Client
-
### Server
-
## 2025.7.0
### Note

View File

@@ -4,16 +4,15 @@
*/
import { URLSearchParams } from 'node:url';
import { TextDecoder } from 'node:util';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { HttpRequestService } from '@/core/HttpRequestService.js';
import { GetterService } from '@/server/api/GetterService.js';
import { RoleService } from '@/core/RoleService.js';
import { ApiError } from '../../error.js';
import { MiMeta } from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../error.js';
export const meta = {
tags: ['notes'],
@@ -95,73 +94,32 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
let targetLang = ps.targetLang;
if (targetLang.includes('-')) targetLang = targetLang.split('-')[0];
const systemPrompt = this.serverSettings.deeplAuthKey;
const params = new URLSearchParams();
params.append('auth_key', this.serverSettings.deeplAuthKey);
params.append('text', note.text);
params.append('target_lang', targetLang);
const userPrompt = `pls translate to ${targetLang} ${note.text}`;
const endpoint = this.serverSettings.deeplIsPro ? 'https://chatapi.neko.ci/api/qwen/chat' : 'https://chatapi.neko.ci/api/mthreads/chat';
const body = {
prompt: [systemPrompt, userPrompt],
temperature: 0.6,
top_p: 0.8,
max_tokens: 3000,
};
const endpoint = this.serverSettings.deeplIsPro ? 'https://api.deepl.com/v2/translate' : 'https://api-free.deepl.com/v2/translate';
const res = await this.httpRequestService.send(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json, */*',
},
body: JSON.stringify(body),
timeout: 120_000,
body: params.toString(),
});
let translatedText = '';
if (res.body) {
const decoder = new TextDecoder();
let buffer = '';
try {
outer: for await (const chunk of res.body) {
buffer += decoder.decode(chunk, { stream: true });
const messages = buffer.split('\n\n');
buffer = messages.pop() || '';
for (const msg of messages) {
if (msg.includes('event: complete')) {
const dataLine = msg.split('\n').find(line => line.startsWith('data:'));
if (dataLine) {
try {
const data = JSON.parse(dataLine.substring(6));
if (Array.isArray(data) && data.length > 0) {
translatedText = data[0];
res.body.destroy();
break outer;
}
} catch { }
}
}
}
}
} catch { }
}
const fullToHalfMap: { [key: string]: string } = {
'': ',', '。': '.', '': '!', '': '?', '': ':', '': ';',
'': '(', '': ')', '“': '"', '”': '"', '': '\'', '': '\'',
'【': '[', '】': ']', '、': ',', ' ': ' ',
const json = (await res.json()) as {
translations: {
detected_source_language: string;
text: string;
}[];
};
let processedText = translatedText.replace(/[,。!?:;()“”‘’【】、 ]/g, m => fullToHalfMap[m]);
if (processedText.trim().length > 0) {
processedText = '\u200B\n\u200B' + processedText;
}
return {
sourceLang: 'AI',
text: processedText,
sourceLang: json.translations[0].detected_source_language,
text: json.translations[0].text,
};
});
}